1 // SPDX-License-Identifier: GPL-2.0-only
4 * Copyright (c) 2012, Intel Corporation.
6 #include <linux/device.h>
7 #include <linux/platform_device.h>
8 #include <linux/module.h>
9 #include <linux/interrupt.h>
10 #include <linux/irq.h>
11 #include <linux/slab.h>
12 #include <linux/delay.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 enum gyro_3d_channel {
28 struct gyro_3d_state {
29 struct hid_sensor_hub_callbacks callbacks;
30 struct hid_sensor_common common_attributes;
31 struct hid_sensor_hub_attribute_info gyro[GYRO_3D_CHANNEL_MAX];
32 u32 gyro_val[GYRO_3D_CHANNEL_MAX];
39 static const u32 gyro_3d_addresses[GYRO_3D_CHANNEL_MAX] = {
40 HID_USAGE_SENSOR_ANGL_VELOCITY_X_AXIS,
41 HID_USAGE_SENSOR_ANGL_VELOCITY_Y_AXIS,
42 HID_USAGE_SENSOR_ANGL_VELOCITY_Z_AXIS
45 /* Channel definitions */
46 static const struct iio_chan_spec gyro_3d_channels[] = {
50 .channel2 = IIO_MOD_X,
51 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
52 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
53 BIT(IIO_CHAN_INFO_SCALE) |
54 BIT(IIO_CHAN_INFO_SAMP_FREQ) |
55 BIT(IIO_CHAN_INFO_HYSTERESIS),
56 .scan_index = CHANNEL_SCAN_INDEX_X,
60 .channel2 = IIO_MOD_Y,
61 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
62 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
63 BIT(IIO_CHAN_INFO_SCALE) |
64 BIT(IIO_CHAN_INFO_SAMP_FREQ) |
65 BIT(IIO_CHAN_INFO_HYSTERESIS),
66 .scan_index = CHANNEL_SCAN_INDEX_Y,
70 .channel2 = IIO_MOD_Z,
71 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
72 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
73 BIT(IIO_CHAN_INFO_SCALE) |
74 BIT(IIO_CHAN_INFO_SAMP_FREQ) |
75 BIT(IIO_CHAN_INFO_HYSTERESIS),
76 .scan_index = CHANNEL_SCAN_INDEX_Z,
80 /* Adjust channel real bits based on report descriptor */
81 static void gyro_3d_adjust_channel_bit_mask(struct iio_chan_spec *channels,
82 int channel, int size)
84 channels[channel].scan_type.sign = 's';
85 /* Real storage bits will change based on the report desc. */
86 channels[channel].scan_type.realbits = size * 8;
87 /* Maximum size of a sample to capture is u32 */
88 channels[channel].scan_type.storagebits = sizeof(u32) * 8;
91 /* Channel read_raw handler */
92 static int gyro_3d_read_raw(struct iio_dev *indio_dev,
93 struct iio_chan_spec const *chan,
97 struct gyro_3d_state *gyro_state = iio_priv(indio_dev);
106 case IIO_CHAN_INFO_RAW:
107 hid_sensor_power_state(&gyro_state->common_attributes, true);
108 report_id = gyro_state->gyro[chan->scan_index].report_id;
109 min = gyro_state->gyro[chan->scan_index].logical_minimum;
110 address = gyro_3d_addresses[chan->scan_index];
112 *val = sensor_hub_input_attr_get_raw_value(
113 gyro_state->common_attributes.hsdev,
114 HID_USAGE_SENSOR_GYRO_3D, address,
120 hid_sensor_power_state(&gyro_state->common_attributes,
124 hid_sensor_power_state(&gyro_state->common_attributes, false);
125 ret_type = IIO_VAL_INT;
127 case IIO_CHAN_INFO_SCALE:
128 *val = gyro_state->scale_pre_decml;
129 *val2 = gyro_state->scale_post_decml;
130 ret_type = gyro_state->scale_precision;
132 case IIO_CHAN_INFO_OFFSET:
133 *val = gyro_state->value_offset;
134 ret_type = IIO_VAL_INT;
136 case IIO_CHAN_INFO_SAMP_FREQ:
137 ret_type = hid_sensor_read_samp_freq_value(
138 &gyro_state->common_attributes, val, val2);
140 case IIO_CHAN_INFO_HYSTERESIS:
141 ret_type = hid_sensor_read_raw_hyst_value(
142 &gyro_state->common_attributes, val, val2);
152 /* Channel write_raw handler */
153 static int gyro_3d_write_raw(struct iio_dev *indio_dev,
154 struct iio_chan_spec const *chan,
159 struct gyro_3d_state *gyro_state = iio_priv(indio_dev);
163 case IIO_CHAN_INFO_SAMP_FREQ:
164 ret = hid_sensor_write_samp_freq_value(
165 &gyro_state->common_attributes, val, val2);
167 case IIO_CHAN_INFO_HYSTERESIS:
168 ret = hid_sensor_write_raw_hyst_value(
169 &gyro_state->common_attributes, val, val2);
178 static const struct iio_info gyro_3d_info = {
179 .read_raw = &gyro_3d_read_raw,
180 .write_raw = &gyro_3d_write_raw,
183 /* Function to push data to buffer */
184 static void hid_sensor_push_data(struct iio_dev *indio_dev, const void *data,
187 dev_dbg(&indio_dev->dev, "hid_sensor_push_data\n");
188 iio_push_to_buffers(indio_dev, data);
191 /* Callback handler to send event after all samples are received and captured */
192 static int gyro_3d_proc_event(struct hid_sensor_hub_device *hsdev,
196 struct iio_dev *indio_dev = platform_get_drvdata(priv);
197 struct gyro_3d_state *gyro_state = iio_priv(indio_dev);
199 dev_dbg(&indio_dev->dev, "gyro_3d_proc_event\n");
200 if (atomic_read(&gyro_state->common_attributes.data_ready))
201 hid_sensor_push_data(indio_dev,
202 gyro_state->gyro_val,
203 sizeof(gyro_state->gyro_val));
208 /* Capture samples in local storage */
209 static int gyro_3d_capture_sample(struct hid_sensor_hub_device *hsdev,
211 size_t raw_len, char *raw_data,
214 struct iio_dev *indio_dev = platform_get_drvdata(priv);
215 struct gyro_3d_state *gyro_state = iio_priv(indio_dev);
220 case HID_USAGE_SENSOR_ANGL_VELOCITY_X_AXIS:
221 case HID_USAGE_SENSOR_ANGL_VELOCITY_Y_AXIS:
222 case HID_USAGE_SENSOR_ANGL_VELOCITY_Z_AXIS:
223 offset = usage_id - HID_USAGE_SENSOR_ANGL_VELOCITY_X_AXIS;
224 gyro_state->gyro_val[CHANNEL_SCAN_INDEX_X + offset] =
235 /* Parse report which is specific to an usage id*/
236 static int gyro_3d_parse_report(struct platform_device *pdev,
237 struct hid_sensor_hub_device *hsdev,
238 struct iio_chan_spec *channels,
240 struct gyro_3d_state *st)
245 for (i = 0; i <= CHANNEL_SCAN_INDEX_Z; ++i) {
246 ret = sensor_hub_input_get_attribute_info(hsdev,
249 HID_USAGE_SENSOR_ANGL_VELOCITY_X_AXIS + i,
250 &st->gyro[CHANNEL_SCAN_INDEX_X + i]);
253 gyro_3d_adjust_channel_bit_mask(channels,
254 CHANNEL_SCAN_INDEX_X + i,
255 st->gyro[CHANNEL_SCAN_INDEX_X + i].size);
257 dev_dbg(&pdev->dev, "gyro_3d %x:%x, %x:%x, %x:%x\n",
259 st->gyro[0].report_id,
260 st->gyro[1].index, st->gyro[1].report_id,
261 st->gyro[2].index, st->gyro[2].report_id);
263 st->scale_precision = hid_sensor_format_scale(
264 HID_USAGE_SENSOR_GYRO_3D,
265 &st->gyro[CHANNEL_SCAN_INDEX_X],
266 &st->scale_pre_decml, &st->scale_post_decml);
268 /* Set Sensitivity field ids, when there is no individual modifier */
269 if (st->common_attributes.sensitivity.index < 0) {
270 sensor_hub_input_get_attribute_info(hsdev,
271 HID_FEATURE_REPORT, usage_id,
272 HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS |
273 HID_USAGE_SENSOR_DATA_ANGL_VELOCITY,
274 &st->common_attributes.sensitivity);
275 dev_dbg(&pdev->dev, "Sensitivity index:report %d:%d\n",
276 st->common_attributes.sensitivity.index,
277 st->common_attributes.sensitivity.report_id);
282 /* Function to initialize the processing for usage id */
283 static int hid_gyro_3d_probe(struct platform_device *pdev)
286 static const char *name = "gyro_3d";
287 struct iio_dev *indio_dev;
288 struct gyro_3d_state *gyro_state;
289 struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
291 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*gyro_state));
294 platform_set_drvdata(pdev, indio_dev);
296 gyro_state = iio_priv(indio_dev);
297 gyro_state->common_attributes.hsdev = hsdev;
298 gyro_state->common_attributes.pdev = pdev;
300 ret = hid_sensor_parse_common_attributes(hsdev,
301 HID_USAGE_SENSOR_GYRO_3D,
302 &gyro_state->common_attributes);
304 dev_err(&pdev->dev, "failed to setup common attributes\n");
308 indio_dev->channels = kmemdup(gyro_3d_channels,
309 sizeof(gyro_3d_channels), GFP_KERNEL);
310 if (!indio_dev->channels) {
311 dev_err(&pdev->dev, "failed to duplicate channels\n");
315 ret = gyro_3d_parse_report(pdev, hsdev,
316 (struct iio_chan_spec *)indio_dev->channels,
317 HID_USAGE_SENSOR_GYRO_3D, gyro_state);
319 dev_err(&pdev->dev, "failed to setup attributes\n");
320 goto error_free_dev_mem;
323 indio_dev->num_channels = ARRAY_SIZE(gyro_3d_channels);
324 indio_dev->dev.parent = &pdev->dev;
325 indio_dev->info = &gyro_3d_info;
326 indio_dev->name = name;
327 indio_dev->modes = INDIO_DIRECT_MODE;
329 ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
332 dev_err(&pdev->dev, "failed to initialize trigger buffer\n");
333 goto error_free_dev_mem;
335 atomic_set(&gyro_state->common_attributes.data_ready, 0);
336 ret = hid_sensor_setup_trigger(indio_dev, name,
337 &gyro_state->common_attributes);
339 dev_err(&pdev->dev, "trigger setup failed\n");
340 goto error_unreg_buffer_funcs;
343 ret = iio_device_register(indio_dev);
345 dev_err(&pdev->dev, "device register failed\n");
346 goto error_remove_trigger;
349 gyro_state->callbacks.send_event = gyro_3d_proc_event;
350 gyro_state->callbacks.capture_sample = gyro_3d_capture_sample;
351 gyro_state->callbacks.pdev = pdev;
352 ret = sensor_hub_register_callback(hsdev, HID_USAGE_SENSOR_GYRO_3D,
353 &gyro_state->callbacks);
355 dev_err(&pdev->dev, "callback reg failed\n");
356 goto error_iio_unreg;
362 iio_device_unregister(indio_dev);
363 error_remove_trigger:
364 hid_sensor_remove_trigger(&gyro_state->common_attributes);
365 error_unreg_buffer_funcs:
366 iio_triggered_buffer_cleanup(indio_dev);
368 kfree(indio_dev->channels);
372 /* Function to deinitialize the processing for usage id */
373 static int hid_gyro_3d_remove(struct platform_device *pdev)
375 struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
376 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
377 struct gyro_3d_state *gyro_state = iio_priv(indio_dev);
379 sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_GYRO_3D);
380 iio_device_unregister(indio_dev);
381 hid_sensor_remove_trigger(&gyro_state->common_attributes);
382 iio_triggered_buffer_cleanup(indio_dev);
383 kfree(indio_dev->channels);
388 static const struct platform_device_id hid_gyro_3d_ids[] = {
390 /* Format: HID-SENSOR-usage_id_in_hex_lowercase */
391 .name = "HID-SENSOR-200076",
395 MODULE_DEVICE_TABLE(platform, hid_gyro_3d_ids);
397 static struct platform_driver hid_gyro_3d_platform_driver = {
398 .id_table = hid_gyro_3d_ids,
400 .name = KBUILD_MODNAME,
401 .pm = &hid_sensor_pm_ops,
403 .probe = hid_gyro_3d_probe,
404 .remove = hid_gyro_3d_remove,
406 module_platform_driver(hid_gyro_3d_platform_driver);
408 MODULE_DESCRIPTION("HID Sensor Gyroscope 3D");
409 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
410 MODULE_LICENSE("GPL");