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/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"
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;
22 s32 sampled_vals[4] __aligned(16);
23 aligned_s64 timestamp;
32 static const u32 rotation_sensitivity_addresses[] = {
33 HID_USAGE_SENSOR_DATA_ORIENTATION,
34 HID_USAGE_SENSOR_ORIENT_QUATERNION,
37 /* Channel definitions */
38 static const struct iio_chan_spec dev_rot_channels[] = {
42 .channel2 = IIO_MOD_QUATERNION,
43 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
44 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ) |
45 BIT(IIO_CHAN_INFO_OFFSET) |
46 BIT(IIO_CHAN_INFO_SCALE) |
47 BIT(IIO_CHAN_INFO_HYSTERESIS),
50 IIO_CHAN_SOFT_TIMESTAMP(1)
53 /* Adjust channel real bits based on report descriptor */
54 static void dev_rot_adjust_channel_bit_mask(struct iio_chan_spec *chan,
57 chan->scan_type.sign = 's';
58 /* Real storage bits will change based on the report desc. */
59 chan->scan_type.realbits = size * 8;
60 /* Maximum size of a sample to capture is u32 */
61 chan->scan_type.storagebits = sizeof(u32) * 8;
62 chan->scan_type.repeat = 4;
65 /* Channel read_raw handler */
66 static int dev_rot_read_raw(struct iio_dev *indio_dev,
67 struct iio_chan_spec const *chan,
68 int size, int *vals, int *val_len,
71 struct dev_rot_state *rot_state = iio_priv(indio_dev);
79 case IIO_CHAN_INFO_RAW:
81 for (i = 0; i < 4; ++i)
82 vals[i] = rot_state->scan.sampled_vals[i];
83 ret_type = IIO_VAL_INT_MULTIPLE;
88 case IIO_CHAN_INFO_SCALE:
89 vals[0] = rot_state->scale_pre_decml;
90 vals[1] = rot_state->scale_post_decml;
91 return rot_state->scale_precision;
93 case IIO_CHAN_INFO_OFFSET:
94 *vals = rot_state->value_offset;
97 case IIO_CHAN_INFO_SAMP_FREQ:
98 ret_type = hid_sensor_read_samp_freq_value(
99 &rot_state->common_attributes, &vals[0], &vals[1]);
101 case IIO_CHAN_INFO_HYSTERESIS:
102 ret_type = hid_sensor_read_raw_hyst_value(
103 &rot_state->common_attributes, &vals[0], &vals[1]);
113 /* Channel write_raw handler */
114 static int dev_rot_write_raw(struct iio_dev *indio_dev,
115 struct iio_chan_spec const *chan,
120 struct dev_rot_state *rot_state = iio_priv(indio_dev);
124 case IIO_CHAN_INFO_SAMP_FREQ:
125 ret = hid_sensor_write_samp_freq_value(
126 &rot_state->common_attributes, val, val2);
128 case IIO_CHAN_INFO_HYSTERESIS:
129 ret = hid_sensor_write_raw_hyst_value(
130 &rot_state->common_attributes, val, val2);
139 static const struct iio_info dev_rot_info = {
140 .read_raw_multi = &dev_rot_read_raw,
141 .write_raw = &dev_rot_write_raw,
144 /* Callback handler to send event after all samples are received and captured */
145 static int dev_rot_proc_event(struct hid_sensor_hub_device *hsdev,
149 struct iio_dev *indio_dev = platform_get_drvdata(priv);
150 struct dev_rot_state *rot_state = iio_priv(indio_dev);
152 dev_dbg(&indio_dev->dev, "dev_rot_proc_event\n");
153 if (atomic_read(&rot_state->common_attributes.data_ready)) {
154 if (!rot_state->timestamp)
155 rot_state->timestamp = iio_get_time_ns(indio_dev);
157 iio_push_to_buffers_with_timestamp(indio_dev, &rot_state->scan,
158 rot_state->timestamp);
160 rot_state->timestamp = 0;
166 /* Capture samples in local storage */
167 static int dev_rot_capture_sample(struct hid_sensor_hub_device *hsdev,
169 size_t raw_len, char *raw_data,
172 struct iio_dev *indio_dev = platform_get_drvdata(priv);
173 struct dev_rot_state *rot_state = iio_priv(indio_dev);
175 if (usage_id == HID_USAGE_SENSOR_ORIENT_QUATERNION) {
176 if (raw_len / 4 == sizeof(s16)) {
177 rot_state->scan.sampled_vals[0] = ((s16 *)raw_data)[0];
178 rot_state->scan.sampled_vals[1] = ((s16 *)raw_data)[1];
179 rot_state->scan.sampled_vals[2] = ((s16 *)raw_data)[2];
180 rot_state->scan.sampled_vals[3] = ((s16 *)raw_data)[3];
182 memcpy(&rot_state->scan.sampled_vals, raw_data,
183 sizeof(rot_state->scan.sampled_vals));
186 dev_dbg(&indio_dev->dev, "Recd Quat len:%zu::%zu\n", raw_len,
187 sizeof(rot_state->scan.sampled_vals));
188 } else if (usage_id == HID_USAGE_SENSOR_TIME_TIMESTAMP) {
189 rot_state->timestamp = hid_sensor_convert_timestamp(&rot_state->common_attributes,
196 /* Parse report which is specific to an usage id*/
197 static int dev_rot_parse_report(struct platform_device *pdev,
198 struct hid_sensor_hub_device *hsdev,
199 struct iio_chan_spec *channels,
201 struct dev_rot_state *st)
205 ret = sensor_hub_input_get_attribute_info(hsdev,
208 HID_USAGE_SENSOR_ORIENT_QUATERNION,
213 dev_rot_adjust_channel_bit_mask(&channels[0],
214 st->quaternion.size / 4);
216 dev_dbg(&pdev->dev, "dev_rot %x:%x\n", st->quaternion.index,
217 st->quaternion.report_id);
219 dev_dbg(&pdev->dev, "dev_rot: attrib size %d\n",
220 st->quaternion.size);
222 st->scale_precision = hid_sensor_format_scale(
225 &st->scale_pre_decml, &st->scale_post_decml);
230 /* Function to initialize the processing for usage id */
231 static int hid_dev_rot_probe(struct platform_device *pdev)
233 struct hid_sensor_hub_device *hsdev = dev_get_platdata(&pdev->dev);
236 struct iio_dev *indio_dev;
237 struct dev_rot_state *rot_state;
239 indio_dev = devm_iio_device_alloc(&pdev->dev,
240 sizeof(struct dev_rot_state));
241 if (indio_dev == NULL)
244 platform_set_drvdata(pdev, indio_dev);
246 rot_state = iio_priv(indio_dev);
247 rot_state->common_attributes.hsdev = hsdev;
248 rot_state->common_attributes.pdev = pdev;
250 switch (hsdev->usage) {
251 case HID_USAGE_SENSOR_DEVICE_ORIENTATION:
252 name = "dev_rotation";
254 case HID_USAGE_SENSOR_RELATIVE_ORIENTATION:
255 name = "relative_orientation";
257 case HID_USAGE_SENSOR_GEOMAGNETIC_ORIENTATION:
258 name = "geomagnetic_orientation";
264 ret = hid_sensor_parse_common_attributes(hsdev,
266 &rot_state->common_attributes,
267 rotation_sensitivity_addresses,
268 ARRAY_SIZE(rotation_sensitivity_addresses));
270 dev_err(&pdev->dev, "failed to setup common attributes\n");
274 indio_dev->channels = devm_kmemdup(&pdev->dev, dev_rot_channels,
275 sizeof(dev_rot_channels),
277 if (!indio_dev->channels) {
278 dev_err(&pdev->dev, "failed to duplicate channels\n");
282 ret = dev_rot_parse_report(pdev, hsdev,
283 (struct iio_chan_spec *)indio_dev->channels,
284 hsdev->usage, rot_state);
286 dev_err(&pdev->dev, "failed to setup attributes\n");
290 indio_dev->num_channels = ARRAY_SIZE(dev_rot_channels);
291 indio_dev->info = &dev_rot_info;
292 indio_dev->name = name;
293 indio_dev->modes = INDIO_DIRECT_MODE;
295 atomic_set(&rot_state->common_attributes.data_ready, 0);
297 ret = hid_sensor_setup_trigger(indio_dev, name,
298 &rot_state->common_attributes);
300 dev_err(&pdev->dev, "trigger setup failed\n");
304 ret = iio_device_register(indio_dev);
306 dev_err(&pdev->dev, "device register failed\n");
307 goto error_remove_trigger;
310 rot_state->callbacks.send_event = dev_rot_proc_event;
311 rot_state->callbacks.capture_sample = dev_rot_capture_sample;
312 rot_state->callbacks.pdev = pdev;
313 ret = sensor_hub_register_callback(hsdev, hsdev->usage,
314 &rot_state->callbacks);
316 dev_err(&pdev->dev, "callback reg failed\n");
317 goto error_iio_unreg;
323 iio_device_unregister(indio_dev);
324 error_remove_trigger:
325 hid_sensor_remove_trigger(indio_dev, &rot_state->common_attributes);
329 /* Function to deinitialize the processing for usage id */
330 static void hid_dev_rot_remove(struct platform_device *pdev)
332 struct hid_sensor_hub_device *hsdev = dev_get_platdata(&pdev->dev);
333 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
334 struct dev_rot_state *rot_state = iio_priv(indio_dev);
336 sensor_hub_remove_callback(hsdev, hsdev->usage);
337 iio_device_unregister(indio_dev);
338 hid_sensor_remove_trigger(indio_dev, &rot_state->common_attributes);
341 static const struct platform_device_id hid_dev_rot_ids[] = {
343 /* Format: HID-SENSOR-usage_id_in_hex_lowercase */
344 .name = "HID-SENSOR-20008a",
347 /* Relative orientation(AG) sensor */
348 .name = "HID-SENSOR-20008e",
351 /* Geomagnetic orientation(AM) sensor */
352 .name = "HID-SENSOR-2000c1",
356 MODULE_DEVICE_TABLE(platform, hid_dev_rot_ids);
358 static struct platform_driver hid_dev_rot_platform_driver = {
359 .id_table = hid_dev_rot_ids,
361 .name = KBUILD_MODNAME,
362 .pm = &hid_sensor_pm_ops,
364 .probe = hid_dev_rot_probe,
365 .remove = hid_dev_rot_remove,
367 module_platform_driver(hid_dev_rot_platform_driver);
369 MODULE_DESCRIPTION("HID Sensor Device Rotation");
371 MODULE_LICENSE("GPL");
372 MODULE_IMPORT_NS("IIO_HID");