3 * Copyright (c) 2014, Intel Corporation.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 #include <linux/device.h>
16 #include <linux/platform_device.h>
17 #include <linux/module.h>
18 #include <linux/interrupt.h>
19 #include <linux/irq.h>
20 #include <linux/slab.h>
21 #include <linux/hid-sensor-hub.h>
22 #include <linux/iio/iio.h>
23 #include <linux/iio/sysfs.h>
24 #include <linux/iio/buffer.h>
25 #include <linux/iio/trigger_consumer.h>
26 #include <linux/iio/triggered_buffer.h>
27 #include "../common/hid-sensors/hid-sensor-trigger.h"
29 struct dev_rot_state {
30 struct hid_sensor_hub_callbacks callbacks;
31 struct hid_sensor_common common_attributes;
32 struct hid_sensor_hub_attribute_info quaternion;
40 /* Channel definitions */
41 static const struct iio_chan_spec dev_rot_channels[] = {
45 .channel2 = IIO_MOD_QUATERNION,
46 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
47 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ) |
48 BIT(IIO_CHAN_INFO_OFFSET) |
49 BIT(IIO_CHAN_INFO_SCALE) |
50 BIT(IIO_CHAN_INFO_HYSTERESIS)
54 /* Adjust channel real bits based on report descriptor */
55 static void dev_rot_adjust_channel_bit_mask(struct iio_chan_spec *chan,
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;
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,
72 struct dev_rot_state *rot_state = iio_priv(indio_dev);
80 case IIO_CHAN_INFO_RAW:
82 for (i = 0; i < 4; ++i)
83 vals[i] = rot_state->sampled_vals[i];
84 ret_type = IIO_VAL_INT_MULTIPLE;
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;
94 case IIO_CHAN_INFO_OFFSET:
95 *vals = rot_state->value_offset;
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]);
102 case IIO_CHAN_INFO_HYSTERESIS:
103 ret_type = hid_sensor_read_raw_hyst_value(
104 &rot_state->common_attributes, &vals[0], &vals[1]);
114 /* Channel write_raw handler */
115 static int dev_rot_write_raw(struct iio_dev *indio_dev,
116 struct iio_chan_spec const *chan,
121 struct dev_rot_state *rot_state = iio_priv(indio_dev);
125 case IIO_CHAN_INFO_SAMP_FREQ:
126 ret = hid_sensor_write_samp_freq_value(
127 &rot_state->common_attributes, val, val2);
129 case IIO_CHAN_INFO_HYSTERESIS:
130 ret = hid_sensor_write_raw_hyst_value(
131 &rot_state->common_attributes, val, val2);
140 static const struct iio_info dev_rot_info = {
141 .driver_module = THIS_MODULE,
142 .read_raw_multi = &dev_rot_read_raw,
143 .write_raw = &dev_rot_write_raw,
146 /* Function to push data to buffer */
147 static void hid_sensor_push_data(struct iio_dev *indio_dev, u8 *data, int len)
149 dev_dbg(&indio_dev->dev, "hid_sensor_push_data >>\n");
150 iio_push_to_buffers(indio_dev, (u8 *)data);
151 dev_dbg(&indio_dev->dev, "hid_sensor_push_data <<\n");
155 /* Callback handler to send event after all samples are received and captured */
156 static int dev_rot_proc_event(struct hid_sensor_hub_device *hsdev,
160 struct iio_dev *indio_dev = platform_get_drvdata(priv);
161 struct dev_rot_state *rot_state = iio_priv(indio_dev);
163 dev_dbg(&indio_dev->dev, "dev_rot_proc_event\n");
164 if (atomic_read(&rot_state->common_attributes.data_ready))
165 hid_sensor_push_data(indio_dev,
166 (u8 *)rot_state->sampled_vals,
167 sizeof(rot_state->sampled_vals));
172 /* Capture samples in local storage */
173 static int dev_rot_capture_sample(struct hid_sensor_hub_device *hsdev,
175 size_t raw_len, char *raw_data,
178 struct iio_dev *indio_dev = platform_get_drvdata(priv);
179 struct dev_rot_state *rot_state = iio_priv(indio_dev);
181 if (usage_id == HID_USAGE_SENSOR_ORIENT_QUATERNION) {
182 memcpy(rot_state->sampled_vals, raw_data,
183 sizeof(rot_state->sampled_vals));
184 dev_dbg(&indio_dev->dev, "Recd Quat len:%zu::%zu\n", raw_len,
185 sizeof(rot_state->sampled_vals));
191 /* Parse report which is specific to an usage id*/
192 static int dev_rot_parse_report(struct platform_device *pdev,
193 struct hid_sensor_hub_device *hsdev,
194 struct iio_chan_spec *channels,
196 struct dev_rot_state *st)
200 ret = sensor_hub_input_get_attribute_info(hsdev,
203 HID_USAGE_SENSOR_ORIENT_QUATERNION,
208 dev_rot_adjust_channel_bit_mask(&channels[0],
209 st->quaternion.size / 4);
211 dev_dbg(&pdev->dev, "dev_rot %x:%x\n", st->quaternion.index,
212 st->quaternion.report_id);
214 dev_dbg(&pdev->dev, "dev_rot: attrib size %d\n",
215 st->quaternion.size);
217 st->scale_precision = hid_sensor_format_scale(
220 &st->scale_pre_decml, &st->scale_post_decml);
222 /* Set Sensitivity field ids, when there is no individual modifier */
223 if (st->common_attributes.sensitivity.index < 0) {
224 sensor_hub_input_get_attribute_info(hsdev,
225 HID_FEATURE_REPORT, usage_id,
226 HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS |
227 HID_USAGE_SENSOR_DATA_ORIENTATION,
228 &st->common_attributes.sensitivity);
229 dev_dbg(&pdev->dev, "Sensitivity index:report %d:%d\n",
230 st->common_attributes.sensitivity.index,
231 st->common_attributes.sensitivity.report_id);
237 /* Function to initialize the processing for usage id */
238 static int hid_dev_rot_probe(struct platform_device *pdev)
242 struct iio_dev *indio_dev;
243 struct dev_rot_state *rot_state;
244 struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
246 indio_dev = devm_iio_device_alloc(&pdev->dev,
247 sizeof(struct dev_rot_state));
248 if (indio_dev == NULL)
251 platform_set_drvdata(pdev, indio_dev);
253 rot_state = iio_priv(indio_dev);
254 rot_state->common_attributes.hsdev = hsdev;
255 rot_state->common_attributes.pdev = pdev;
257 switch (hsdev->usage) {
258 case HID_USAGE_SENSOR_DEVICE_ORIENTATION:
259 name = "dev_rotation";
261 case HID_USAGE_SENSOR_RELATIVE_ORIENTATION:
262 name = "relative_orientation";
264 case HID_USAGE_SENSOR_GEOMAGNETIC_ORIENTATION:
265 name = "geomagnetic_orientation";
271 ret = hid_sensor_parse_common_attributes(hsdev, hsdev->usage,
272 &rot_state->common_attributes);
274 dev_err(&pdev->dev, "failed to setup common attributes\n");
278 indio_dev->channels = devm_kmemdup(&pdev->dev, dev_rot_channels,
279 sizeof(dev_rot_channels),
281 if (!indio_dev->channels) {
282 dev_err(&pdev->dev, "failed to duplicate channels\n");
286 ret = dev_rot_parse_report(pdev, hsdev,
287 (struct iio_chan_spec *)indio_dev->channels,
288 hsdev->usage, rot_state);
290 dev_err(&pdev->dev, "failed to setup attributes\n");
294 indio_dev->num_channels = ARRAY_SIZE(dev_rot_channels);
295 indio_dev->dev.parent = &pdev->dev;
296 indio_dev->info = &dev_rot_info;
297 indio_dev->name = name;
298 indio_dev->modes = INDIO_DIRECT_MODE;
300 ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
303 dev_err(&pdev->dev, "failed to initialize trigger buffer\n");
306 atomic_set(&rot_state->common_attributes.data_ready, 0);
307 ret = hid_sensor_setup_trigger(indio_dev, name,
308 &rot_state->common_attributes);
310 dev_err(&pdev->dev, "trigger setup failed\n");
311 goto error_unreg_buffer_funcs;
314 ret = iio_device_register(indio_dev);
316 dev_err(&pdev->dev, "device register failed\n");
317 goto error_remove_trigger;
320 rot_state->callbacks.send_event = dev_rot_proc_event;
321 rot_state->callbacks.capture_sample = dev_rot_capture_sample;
322 rot_state->callbacks.pdev = pdev;
323 ret = sensor_hub_register_callback(hsdev, hsdev->usage,
324 &rot_state->callbacks);
326 dev_err(&pdev->dev, "callback reg failed\n");
327 goto error_iio_unreg;
333 iio_device_unregister(indio_dev);
334 error_remove_trigger:
335 hid_sensor_remove_trigger(&rot_state->common_attributes);
336 error_unreg_buffer_funcs:
337 iio_triggered_buffer_cleanup(indio_dev);
341 /* Function to deinitialize the processing for usage id */
342 static int hid_dev_rot_remove(struct platform_device *pdev)
344 struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
345 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
346 struct dev_rot_state *rot_state = iio_priv(indio_dev);
348 sensor_hub_remove_callback(hsdev, hsdev->usage);
349 iio_device_unregister(indio_dev);
350 hid_sensor_remove_trigger(&rot_state->common_attributes);
351 iio_triggered_buffer_cleanup(indio_dev);
356 static const struct platform_device_id hid_dev_rot_ids[] = {
358 /* Format: HID-SENSOR-usage_id_in_hex_lowercase */
359 .name = "HID-SENSOR-20008a",
362 /* Relative orientation(AG) sensor */
363 .name = "HID-SENSOR-20008e",
366 /* Geomagnetic orientation(AM) sensor */
367 .name = "HID-SENSOR-2000c1",
371 MODULE_DEVICE_TABLE(platform, hid_dev_rot_ids);
373 static struct platform_driver hid_dev_rot_platform_driver = {
374 .id_table = hid_dev_rot_ids,
376 .name = KBUILD_MODNAME,
377 .pm = &hid_sensor_pm_ops,
379 .probe = hid_dev_rot_probe,
380 .remove = hid_dev_rot_remove,
382 module_platform_driver(hid_dev_rot_platform_driver);
384 MODULE_DESCRIPTION("HID Sensor Device Rotation");
385 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
386 MODULE_LICENSE("GPL");