1 // SPDX-License-Identifier: GPL-2.0-only
4 * Copyright (c) 2014, Intel Corporation.
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>
17 #include <linux/iio/trigger_consumer.h>
18 #include <linux/iio/triggered_buffer.h>
19 #include "../common/hid-sensors/hid-sensor-trigger.h"
21 struct dev_rot_state
{
22 struct hid_sensor_hub_callbacks callbacks
;
23 struct hid_sensor_common common_attributes
;
24 struct hid_sensor_hub_attribute_info quaternion
;
32 /* Channel definitions */
33 static const struct iio_chan_spec dev_rot_channels
[] = {
37 .channel2
= IIO_MOD_QUATERNION
,
38 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
39 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_SAMP_FREQ
) |
40 BIT(IIO_CHAN_INFO_OFFSET
) |
41 BIT(IIO_CHAN_INFO_SCALE
) |
42 BIT(IIO_CHAN_INFO_HYSTERESIS
)
46 /* Adjust channel real bits based on report descriptor */
47 static void dev_rot_adjust_channel_bit_mask(struct iio_chan_spec
*chan
,
50 chan
->scan_type
.sign
= 's';
51 /* Real storage bits will change based on the report desc. */
52 chan
->scan_type
.realbits
= size
* 8;
53 /* Maximum size of a sample to capture is u32 */
54 chan
->scan_type
.storagebits
= sizeof(u32
) * 8;
55 chan
->scan_type
.repeat
= 4;
58 /* Channel read_raw handler */
59 static int dev_rot_read_raw(struct iio_dev
*indio_dev
,
60 struct iio_chan_spec
const *chan
,
61 int size
, int *vals
, int *val_len
,
64 struct dev_rot_state
*rot_state
= iio_priv(indio_dev
);
72 case IIO_CHAN_INFO_RAW
:
74 for (i
= 0; i
< 4; ++i
)
75 vals
[i
] = rot_state
->sampled_vals
[i
];
76 ret_type
= IIO_VAL_INT_MULTIPLE
;
81 case IIO_CHAN_INFO_SCALE
:
82 vals
[0] = rot_state
->scale_pre_decml
;
83 vals
[1] = rot_state
->scale_post_decml
;
84 return rot_state
->scale_precision
;
86 case IIO_CHAN_INFO_OFFSET
:
87 *vals
= rot_state
->value_offset
;
90 case IIO_CHAN_INFO_SAMP_FREQ
:
91 ret_type
= hid_sensor_read_samp_freq_value(
92 &rot_state
->common_attributes
, &vals
[0], &vals
[1]);
94 case IIO_CHAN_INFO_HYSTERESIS
:
95 ret_type
= hid_sensor_read_raw_hyst_value(
96 &rot_state
->common_attributes
, &vals
[0], &vals
[1]);
106 /* Channel write_raw handler */
107 static int dev_rot_write_raw(struct iio_dev
*indio_dev
,
108 struct iio_chan_spec
const *chan
,
113 struct dev_rot_state
*rot_state
= iio_priv(indio_dev
);
117 case IIO_CHAN_INFO_SAMP_FREQ
:
118 ret
= hid_sensor_write_samp_freq_value(
119 &rot_state
->common_attributes
, val
, val2
);
121 case IIO_CHAN_INFO_HYSTERESIS
:
122 ret
= hid_sensor_write_raw_hyst_value(
123 &rot_state
->common_attributes
, val
, val2
);
132 static const struct iio_info dev_rot_info
= {
133 .read_raw_multi
= &dev_rot_read_raw
,
134 .write_raw
= &dev_rot_write_raw
,
137 /* Function to push data to buffer */
138 static void hid_sensor_push_data(struct iio_dev
*indio_dev
, u8
*data
, int len
)
140 dev_dbg(&indio_dev
->dev
, "hid_sensor_push_data >>\n");
141 iio_push_to_buffers(indio_dev
, (u8
*)data
);
142 dev_dbg(&indio_dev
->dev
, "hid_sensor_push_data <<\n");
146 /* Callback handler to send event after all samples are received and captured */
147 static int dev_rot_proc_event(struct hid_sensor_hub_device
*hsdev
,
151 struct iio_dev
*indio_dev
= platform_get_drvdata(priv
);
152 struct dev_rot_state
*rot_state
= iio_priv(indio_dev
);
154 dev_dbg(&indio_dev
->dev
, "dev_rot_proc_event\n");
155 if (atomic_read(&rot_state
->common_attributes
.data_ready
))
156 hid_sensor_push_data(indio_dev
,
157 (u8
*)rot_state
->sampled_vals
,
158 sizeof(rot_state
->sampled_vals
));
163 /* Capture samples in local storage */
164 static int dev_rot_capture_sample(struct hid_sensor_hub_device
*hsdev
,
166 size_t raw_len
, char *raw_data
,
169 struct iio_dev
*indio_dev
= platform_get_drvdata(priv
);
170 struct dev_rot_state
*rot_state
= iio_priv(indio_dev
);
172 if (usage_id
== HID_USAGE_SENSOR_ORIENT_QUATERNION
) {
173 memcpy(rot_state
->sampled_vals
, raw_data
,
174 sizeof(rot_state
->sampled_vals
));
175 dev_dbg(&indio_dev
->dev
, "Recd Quat len:%zu::%zu\n", raw_len
,
176 sizeof(rot_state
->sampled_vals
));
182 /* Parse report which is specific to an usage id*/
183 static int dev_rot_parse_report(struct platform_device
*pdev
,
184 struct hid_sensor_hub_device
*hsdev
,
185 struct iio_chan_spec
*channels
,
187 struct dev_rot_state
*st
)
191 ret
= sensor_hub_input_get_attribute_info(hsdev
,
194 HID_USAGE_SENSOR_ORIENT_QUATERNION
,
199 dev_rot_adjust_channel_bit_mask(&channels
[0],
200 st
->quaternion
.size
/ 4);
202 dev_dbg(&pdev
->dev
, "dev_rot %x:%x\n", st
->quaternion
.index
,
203 st
->quaternion
.report_id
);
205 dev_dbg(&pdev
->dev
, "dev_rot: attrib size %d\n",
206 st
->quaternion
.size
);
208 st
->scale_precision
= hid_sensor_format_scale(
211 &st
->scale_pre_decml
, &st
->scale_post_decml
);
213 /* Set Sensitivity field ids, when there is no individual modifier */
214 if (st
->common_attributes
.sensitivity
.index
< 0) {
215 sensor_hub_input_get_attribute_info(hsdev
,
216 HID_FEATURE_REPORT
, usage_id
,
217 HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS
|
218 HID_USAGE_SENSOR_DATA_ORIENTATION
,
219 &st
->common_attributes
.sensitivity
);
220 dev_dbg(&pdev
->dev
, "Sensitivity index:report %d:%d\n",
221 st
->common_attributes
.sensitivity
.index
,
222 st
->common_attributes
.sensitivity
.report_id
);
228 /* Function to initialize the processing for usage id */
229 static int hid_dev_rot_probe(struct platform_device
*pdev
)
233 struct iio_dev
*indio_dev
;
234 struct dev_rot_state
*rot_state
;
235 struct hid_sensor_hub_device
*hsdev
= pdev
->dev
.platform_data
;
237 indio_dev
= devm_iio_device_alloc(&pdev
->dev
,
238 sizeof(struct dev_rot_state
));
239 if (indio_dev
== NULL
)
242 platform_set_drvdata(pdev
, indio_dev
);
244 rot_state
= iio_priv(indio_dev
);
245 rot_state
->common_attributes
.hsdev
= hsdev
;
246 rot_state
->common_attributes
.pdev
= pdev
;
248 switch (hsdev
->usage
) {
249 case HID_USAGE_SENSOR_DEVICE_ORIENTATION
:
250 name
= "dev_rotation";
252 case HID_USAGE_SENSOR_RELATIVE_ORIENTATION
:
253 name
= "relative_orientation";
255 case HID_USAGE_SENSOR_GEOMAGNETIC_ORIENTATION
:
256 name
= "geomagnetic_orientation";
262 ret
= hid_sensor_parse_common_attributes(hsdev
, hsdev
->usage
,
263 &rot_state
->common_attributes
);
265 dev_err(&pdev
->dev
, "failed to setup common attributes\n");
269 indio_dev
->channels
= devm_kmemdup(&pdev
->dev
, dev_rot_channels
,
270 sizeof(dev_rot_channels
),
272 if (!indio_dev
->channels
) {
273 dev_err(&pdev
->dev
, "failed to duplicate channels\n");
277 ret
= dev_rot_parse_report(pdev
, hsdev
,
278 (struct iio_chan_spec
*)indio_dev
->channels
,
279 hsdev
->usage
, rot_state
);
281 dev_err(&pdev
->dev
, "failed to setup attributes\n");
285 indio_dev
->num_channels
= ARRAY_SIZE(dev_rot_channels
);
286 indio_dev
->dev
.parent
= &pdev
->dev
;
287 indio_dev
->info
= &dev_rot_info
;
288 indio_dev
->name
= name
;
289 indio_dev
->modes
= INDIO_DIRECT_MODE
;
291 ret
= iio_triggered_buffer_setup(indio_dev
, &iio_pollfunc_store_time
,
294 dev_err(&pdev
->dev
, "failed to initialize trigger buffer\n");
297 atomic_set(&rot_state
->common_attributes
.data_ready
, 0);
298 ret
= hid_sensor_setup_trigger(indio_dev
, name
,
299 &rot_state
->common_attributes
);
301 dev_err(&pdev
->dev
, "trigger setup failed\n");
302 goto error_unreg_buffer_funcs
;
305 ret
= iio_device_register(indio_dev
);
307 dev_err(&pdev
->dev
, "device register failed\n");
308 goto error_remove_trigger
;
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
;
314 ret
= sensor_hub_register_callback(hsdev
, hsdev
->usage
,
315 &rot_state
->callbacks
);
317 dev_err(&pdev
->dev
, "callback reg failed\n");
318 goto error_iio_unreg
;
324 iio_device_unregister(indio_dev
);
325 error_remove_trigger
:
326 hid_sensor_remove_trigger(&rot_state
->common_attributes
);
327 error_unreg_buffer_funcs
:
328 iio_triggered_buffer_cleanup(indio_dev
);
332 /* Function to deinitialize the processing for usage id */
333 static int hid_dev_rot_remove(struct platform_device
*pdev
)
335 struct hid_sensor_hub_device
*hsdev
= pdev
->dev
.platform_data
;
336 struct iio_dev
*indio_dev
= platform_get_drvdata(pdev
);
337 struct dev_rot_state
*rot_state
= iio_priv(indio_dev
);
339 sensor_hub_remove_callback(hsdev
, hsdev
->usage
);
340 iio_device_unregister(indio_dev
);
341 hid_sensor_remove_trigger(&rot_state
->common_attributes
);
342 iio_triggered_buffer_cleanup(indio_dev
);
347 static const struct platform_device_id hid_dev_rot_ids
[] = {
349 /* Format: HID-SENSOR-usage_id_in_hex_lowercase */
350 .name
= "HID-SENSOR-20008a",
353 /* Relative orientation(AG) sensor */
354 .name
= "HID-SENSOR-20008e",
357 /* Geomagnetic orientation(AM) sensor */
358 .name
= "HID-SENSOR-2000c1",
362 MODULE_DEVICE_TABLE(platform
, hid_dev_rot_ids
);
364 static struct platform_driver hid_dev_rot_platform_driver
= {
365 .id_table
= hid_dev_rot_ids
,
367 .name
= KBUILD_MODNAME
,
368 .pm
= &hid_sensor_pm_ops
,
370 .probe
= hid_dev_rot_probe
,
371 .remove
= hid_dev_rot_remove
,
373 module_platform_driver(hid_dev_rot_platform_driver
);
375 MODULE_DESCRIPTION("HID Sensor Device Rotation");
376 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
377 MODULE_LICENSE("GPL");