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