Commit | Line | Data |
---|---|---|
2025cf9e | 1 | // SPDX-License-Identifier: GPL-2.0-only |
fc18dddc SP |
2 | /* |
3 | * HID Sensors Driver | |
4 | * Copyright (c) 2014, Intel Corporation. | |
fc18dddc SP |
5 | */ |
6 | ||
7 | #include <linux/device.h> | |
8 | #include <linux/platform_device.h> | |
9 | #include <linux/module.h> | |
10 | #include <linux/interrupt.h> | |
11 | #include <linux/irq.h> | |
12 | #include <linux/slab.h> | |
13 | #include <linux/hid-sensor-hub.h> | |
14 | #include <linux/iio/iio.h> | |
15 | #include <linux/iio/sysfs.h> | |
16 | #include <linux/iio/buffer.h> | |
fc18dddc SP |
17 | #include "../common/hid-sensors/hid-sensor-trigger.h" |
18 | ||
19 | struct dev_rot_state { | |
20 | struct hid_sensor_hub_callbacks callbacks; | |
21 | struct hid_sensor_common common_attributes; | |
22 | struct hid_sensor_hub_attribute_info quaternion; | |
4a3582c8 | 23 | struct { |
6c3b6153 | 24 | s32 sampled_vals[4] __aligned(16); |
4a3582c8 YX |
25 | u64 timestamp __aligned(8); |
26 | } scan; | |
a78587d3 SH |
27 | int scale_pre_decml; |
28 | int scale_post_decml; | |
29 | int scale_precision; | |
30 | int value_offset; | |
4a3582c8 | 31 | s64 timestamp; |
fc18dddc SP |
32 | }; |
33 | ||
0e41fd51 YX |
34 | static const u32 rotation_sensitivity_addresses[] = { |
35 | HID_USAGE_SENSOR_DATA_ORIENTATION, | |
36 | }; | |
37 | ||
fc18dddc SP |
38 | /* Channel definitions */ |
39 | static const struct iio_chan_spec dev_rot_channels[] = { | |
40 | { | |
41 | .type = IIO_ROT, | |
42 | .modified = 1, | |
43 | .channel2 = IIO_MOD_QUATERNION, | |
44 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), | |
45 | .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ) | | |
a78587d3 SH |
46 | BIT(IIO_CHAN_INFO_OFFSET) | |
47 | BIT(IIO_CHAN_INFO_SCALE) | | |
4a3582c8 YX |
48 | BIT(IIO_CHAN_INFO_HYSTERESIS), |
49 | .scan_index = 0 | |
50 | }, | |
51 | IIO_CHAN_SOFT_TIMESTAMP(1) | |
fc18dddc SP |
52 | }; |
53 | ||
54 | /* Adjust channel real bits based on report descriptor */ | |
55 | static void dev_rot_adjust_channel_bit_mask(struct iio_chan_spec *chan, | |
56 | int size) | |
57 | { | |
58 | chan->scan_type.sign = 's'; | |
59 | /* Real storage bits will change based on the report desc. */ | |
60 | chan->scan_type.realbits = size * 8; | |
61 | /* Maximum size of a sample to capture is u32 */ | |
62 | chan->scan_type.storagebits = sizeof(u32) * 8; | |
63 | chan->scan_type.repeat = 4; | |
64 | } | |
65 | ||
66 | /* Channel read_raw handler */ | |
67 | static int dev_rot_read_raw(struct iio_dev *indio_dev, | |
68 | struct iio_chan_spec const *chan, | |
69 | int size, int *vals, int *val_len, | |
70 | long mask) | |
71 | { | |
72 | struct dev_rot_state *rot_state = iio_priv(indio_dev); | |
73 | int ret_type; | |
74 | int i; | |
75 | ||
76 | vals[0] = 0; | |
77 | vals[1] = 0; | |
78 | ||
79 | switch (mask) { | |
80 | case IIO_CHAN_INFO_RAW: | |
81 | if (size >= 4) { | |
82 | for (i = 0; i < 4; ++i) | |
4a3582c8 | 83 | vals[i] = rot_state->scan.sampled_vals[i]; |
fc18dddc SP |
84 | ret_type = IIO_VAL_INT_MULTIPLE; |
85 | *val_len = 4; | |
86 | } else | |
87 | ret_type = -EINVAL; | |
88 | break; | |
a78587d3 SH |
89 | case IIO_CHAN_INFO_SCALE: |
90 | vals[0] = rot_state->scale_pre_decml; | |
91 | vals[1] = rot_state->scale_post_decml; | |
92 | return rot_state->scale_precision; | |
93 | ||
94 | case IIO_CHAN_INFO_OFFSET: | |
95 | *vals = rot_state->value_offset; | |
96 | return IIO_VAL_INT; | |
97 | ||
fc18dddc SP |
98 | case IIO_CHAN_INFO_SAMP_FREQ: |
99 | ret_type = hid_sensor_read_samp_freq_value( | |
100 | &rot_state->common_attributes, &vals[0], &vals[1]); | |
101 | break; | |
102 | case IIO_CHAN_INFO_HYSTERESIS: | |
103 | ret_type = hid_sensor_read_raw_hyst_value( | |
104 | &rot_state->common_attributes, &vals[0], &vals[1]); | |
105 | break; | |
106 | default: | |
107 | ret_type = -EINVAL; | |
108 | break; | |
109 | } | |
110 | ||
111 | return ret_type; | |
112 | } | |
113 | ||
114 | /* Channel write_raw handler */ | |
115 | static int dev_rot_write_raw(struct iio_dev *indio_dev, | |
116 | struct iio_chan_spec const *chan, | |
117 | int val, | |
118 | int val2, | |
119 | long mask) | |
120 | { | |
121 | struct dev_rot_state *rot_state = iio_priv(indio_dev); | |
122 | int ret; | |
123 | ||
124 | switch (mask) { | |
125 | case IIO_CHAN_INFO_SAMP_FREQ: | |
126 | ret = hid_sensor_write_samp_freq_value( | |
127 | &rot_state->common_attributes, val, val2); | |
128 | break; | |
129 | case IIO_CHAN_INFO_HYSTERESIS: | |
130 | ret = hid_sensor_write_raw_hyst_value( | |
131 | &rot_state->common_attributes, val, val2); | |
132 | break; | |
133 | default: | |
134 | ret = -EINVAL; | |
135 | } | |
136 | ||
137 | return ret; | |
138 | } | |
139 | ||
140 | static const struct iio_info dev_rot_info = { | |
fc18dddc SP |
141 | .read_raw_multi = &dev_rot_read_raw, |
142 | .write_raw = &dev_rot_write_raw, | |
143 | }; | |
144 | ||
fc18dddc SP |
145 | /* Callback handler to send event after all samples are received and captured */ |
146 | static int dev_rot_proc_event(struct hid_sensor_hub_device *hsdev, | |
147 | unsigned usage_id, | |
148 | void *priv) | |
149 | { | |
150 | struct iio_dev *indio_dev = platform_get_drvdata(priv); | |
151 | struct dev_rot_state *rot_state = iio_priv(indio_dev); | |
152 | ||
56ff6be6 | 153 | dev_dbg(&indio_dev->dev, "dev_rot_proc_event\n"); |
4a3582c8 YX |
154 | if (atomic_read(&rot_state->common_attributes.data_ready)) { |
155 | if (!rot_state->timestamp) | |
156 | rot_state->timestamp = iio_get_time_ns(indio_dev); | |
157 | ||
158 | iio_push_to_buffers_with_timestamp(indio_dev, &rot_state->scan, | |
159 | rot_state->timestamp); | |
160 | ||
161 | rot_state->timestamp = 0; | |
162 | } | |
fc18dddc SP |
163 | |
164 | return 0; | |
165 | } | |
166 | ||
167 | /* Capture samples in local storage */ | |
168 | static int dev_rot_capture_sample(struct hid_sensor_hub_device *hsdev, | |
169 | unsigned usage_id, | |
170 | size_t raw_len, char *raw_data, | |
171 | void *priv) | |
172 | { | |
173 | struct iio_dev *indio_dev = platform_get_drvdata(priv); | |
174 | struct dev_rot_state *rot_state = iio_priv(indio_dev); | |
175 | ||
176 | if (usage_id == HID_USAGE_SENSOR_ORIENT_QUATERNION) { | |
6c3b6153 YX |
177 | if (raw_len / 4 == sizeof(s16)) { |
178 | rot_state->scan.sampled_vals[0] = ((s16 *)raw_data)[0]; | |
179 | rot_state->scan.sampled_vals[1] = ((s16 *)raw_data)[1]; | |
180 | rot_state->scan.sampled_vals[2] = ((s16 *)raw_data)[2]; | |
181 | rot_state->scan.sampled_vals[3] = ((s16 *)raw_data)[3]; | |
182 | } else { | |
183 | memcpy(&rot_state->scan.sampled_vals, raw_data, | |
184 | sizeof(rot_state->scan.sampled_vals)); | |
185 | } | |
4a3582c8 | 186 | |
fc18dddc | 187 | dev_dbg(&indio_dev->dev, "Recd Quat len:%zu::%zu\n", raw_len, |
4a3582c8 YX |
188 | sizeof(rot_state->scan.sampled_vals)); |
189 | } else if (usage_id == HID_USAGE_SENSOR_TIME_TIMESTAMP) { | |
190 | rot_state->timestamp = hid_sensor_convert_timestamp(&rot_state->common_attributes, | |
191 | *(s64 *)raw_data); | |
fc18dddc SP |
192 | } |
193 | ||
194 | return 0; | |
195 | } | |
196 | ||
197 | /* Parse report which is specific to an usage id*/ | |
198 | static int dev_rot_parse_report(struct platform_device *pdev, | |
199 | struct hid_sensor_hub_device *hsdev, | |
200 | struct iio_chan_spec *channels, | |
201 | unsigned usage_id, | |
202 | struct dev_rot_state *st) | |
203 | { | |
204 | int ret; | |
205 | ||
206 | ret = sensor_hub_input_get_attribute_info(hsdev, | |
207 | HID_INPUT_REPORT, | |
208 | usage_id, | |
209 | HID_USAGE_SENSOR_ORIENT_QUATERNION, | |
210 | &st->quaternion); | |
211 | if (ret) | |
212 | return ret; | |
213 | ||
214 | dev_rot_adjust_channel_bit_mask(&channels[0], | |
215 | st->quaternion.size / 4); | |
216 | ||
217 | dev_dbg(&pdev->dev, "dev_rot %x:%x\n", st->quaternion.index, | |
218 | st->quaternion.report_id); | |
219 | ||
220 | dev_dbg(&pdev->dev, "dev_rot: attrib size %d\n", | |
221 | st->quaternion.size); | |
222 | ||
a78587d3 SH |
223 | st->scale_precision = hid_sensor_format_scale( |
224 | hsdev->usage, | |
225 | &st->quaternion, | |
226 | &st->scale_pre_decml, &st->scale_post_decml); | |
227 | ||
fc18dddc SP |
228 | return 0; |
229 | } | |
230 | ||
231 | /* Function to initialize the processing for usage id */ | |
232 | static int hid_dev_rot_probe(struct platform_device *pdev) | |
233 | { | |
234 | int ret; | |
50db2cd4 | 235 | char *name; |
fc18dddc SP |
236 | struct iio_dev *indio_dev; |
237 | struct dev_rot_state *rot_state; | |
238 | struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data; | |
fc18dddc SP |
239 | |
240 | indio_dev = devm_iio_device_alloc(&pdev->dev, | |
241 | sizeof(struct dev_rot_state)); | |
242 | if (indio_dev == NULL) | |
243 | return -ENOMEM; | |
244 | ||
245 | platform_set_drvdata(pdev, indio_dev); | |
246 | ||
247 | rot_state = iio_priv(indio_dev); | |
248 | rot_state->common_attributes.hsdev = hsdev; | |
249 | rot_state->common_attributes.pdev = pdev; | |
250 | ||
9ff88edc SH |
251 | switch (hsdev->usage) { |
252 | case HID_USAGE_SENSOR_DEVICE_ORIENTATION: | |
253 | name = "dev_rotation"; | |
254 | break; | |
255 | case HID_USAGE_SENSOR_RELATIVE_ORIENTATION: | |
256 | name = "relative_orientation"; | |
257 | break; | |
00907c7a SH |
258 | case HID_USAGE_SENSOR_GEOMAGNETIC_ORIENTATION: |
259 | name = "geomagnetic_orientation"; | |
260 | break; | |
9ff88edc SH |
261 | default: |
262 | return -EINVAL; | |
263 | } | |
264 | ||
0e41fd51 YX |
265 | ret = hid_sensor_parse_common_attributes(hsdev, |
266 | hsdev->usage, | |
267 | &rot_state->common_attributes, | |
268 | rotation_sensitivity_addresses, | |
269 | ARRAY_SIZE(rotation_sensitivity_addresses)); | |
fc18dddc SP |
270 | if (ret) { |
271 | dev_err(&pdev->dev, "failed to setup common attributes\n"); | |
272 | return ret; | |
273 | } | |
274 | ||
2bd04628 FE |
275 | indio_dev->channels = devm_kmemdup(&pdev->dev, dev_rot_channels, |
276 | sizeof(dev_rot_channels), | |
277 | GFP_KERNEL); | |
278 | if (!indio_dev->channels) { | |
fc18dddc SP |
279 | dev_err(&pdev->dev, "failed to duplicate channels\n"); |
280 | return -ENOMEM; | |
281 | } | |
282 | ||
2bd04628 FE |
283 | ret = dev_rot_parse_report(pdev, hsdev, |
284 | (struct iio_chan_spec *)indio_dev->channels, | |
9ff88edc | 285 | hsdev->usage, rot_state); |
fc18dddc SP |
286 | if (ret) { |
287 | dev_err(&pdev->dev, "failed to setup attributes\n"); | |
288 | return ret; | |
289 | } | |
290 | ||
fc18dddc | 291 | indio_dev->num_channels = ARRAY_SIZE(dev_rot_channels); |
fc18dddc SP |
292 | indio_dev->info = &dev_rot_info; |
293 | indio_dev->name = name; | |
294 | indio_dev->modes = INDIO_DIRECT_MODE; | |
295 | ||
56ff6be6 | 296 | atomic_set(&rot_state->common_attributes.data_ready, 0); |
067fda1c | 297 | |
fc18dddc SP |
298 | ret = hid_sensor_setup_trigger(indio_dev, name, |
299 | &rot_state->common_attributes); | |
300 | if (ret) { | |
301 | dev_err(&pdev->dev, "trigger setup failed\n"); | |
067fda1c | 302 | return ret; |
fc18dddc SP |
303 | } |
304 | ||
305 | ret = iio_device_register(indio_dev); | |
306 | if (ret) { | |
307 | dev_err(&pdev->dev, "device register failed\n"); | |
308 | goto error_remove_trigger; | |
309 | } | |
310 | ||
311 | rot_state->callbacks.send_event = dev_rot_proc_event; | |
312 | rot_state->callbacks.capture_sample = dev_rot_capture_sample; | |
313 | rot_state->callbacks.pdev = pdev; | |
9ff88edc | 314 | ret = sensor_hub_register_callback(hsdev, hsdev->usage, |
fc18dddc SP |
315 | &rot_state->callbacks); |
316 | if (ret) { | |
317 | dev_err(&pdev->dev, "callback reg failed\n"); | |
318 | goto error_iio_unreg; | |
319 | } | |
320 | ||
321 | return 0; | |
322 | ||
323 | error_iio_unreg: | |
324 | iio_device_unregister(indio_dev); | |
325 | error_remove_trigger: | |
067fda1c | 326 | hid_sensor_remove_trigger(indio_dev, &rot_state->common_attributes); |
fc18dddc SP |
327 | return ret; |
328 | } | |
329 | ||
330 | /* Function to deinitialize the processing for usage id */ | |
331 | static int hid_dev_rot_remove(struct platform_device *pdev) | |
332 | { | |
333 | struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data; | |
334 | struct iio_dev *indio_dev = platform_get_drvdata(pdev); | |
335 | struct dev_rot_state *rot_state = iio_priv(indio_dev); | |
336 | ||
9ff88edc | 337 | sensor_hub_remove_callback(hsdev, hsdev->usage); |
fc18dddc | 338 | iio_device_unregister(indio_dev); |
067fda1c | 339 | hid_sensor_remove_trigger(indio_dev, &rot_state->common_attributes); |
fc18dddc SP |
340 | |
341 | return 0; | |
342 | } | |
343 | ||
6b490c6c | 344 | static const struct platform_device_id hid_dev_rot_ids[] = { |
fc18dddc SP |
345 | { |
346 | /* Format: HID-SENSOR-usage_id_in_hex_lowercase */ | |
347 | .name = "HID-SENSOR-20008a", | |
348 | }, | |
9ff88edc SH |
349 | { |
350 | /* Relative orientation(AG) sensor */ | |
351 | .name = "HID-SENSOR-20008e", | |
352 | }, | |
00907c7a SH |
353 | { |
354 | /* Geomagnetic orientation(AM) sensor */ | |
355 | .name = "HID-SENSOR-2000c1", | |
356 | }, | |
fc18dddc SP |
357 | { /* sentinel */ } |
358 | }; | |
359 | MODULE_DEVICE_TABLE(platform, hid_dev_rot_ids); | |
360 | ||
361 | static struct platform_driver hid_dev_rot_platform_driver = { | |
362 | .id_table = hid_dev_rot_ids, | |
363 | .driver = { | |
364 | .name = KBUILD_MODNAME, | |
8af644a7 | 365 | .pm = &hid_sensor_pm_ops, |
fc18dddc SP |
366 | }, |
367 | .probe = hid_dev_rot_probe, | |
368 | .remove = hid_dev_rot_remove, | |
369 | }; | |
370 | module_platform_driver(hid_dev_rot_platform_driver); | |
371 | ||
372 | MODULE_DESCRIPTION("HID Sensor Device Rotation"); | |
373 | MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>"); | |
374 | MODULE_LICENSE("GPL"); |