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/mod_devicetable.h>
10 #include <linux/slab.h>
11 #include <linux/hid-sensor-hub.h>
12 #include <linux/iio/iio.h>
13 #include <linux/iio/buffer.h>
14 #include "../common/hid-sensors/hid-sensor-trigger.h"
16 enum gyro_3d_channel {
23 #define CHANNEL_SCAN_INDEX_TIMESTAMP GYRO_3D_CHANNEL_MAX
24 struct gyro_3d_state {
25 struct hid_sensor_hub_callbacks callbacks;
26 struct hid_sensor_common common_attributes;
27 struct hid_sensor_hub_attribute_info gyro[GYRO_3D_CHANNEL_MAX];
29 u32 gyro_val[GYRO_3D_CHANNEL_MAX];
30 u64 timestamp __aligned(8);
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 static const u32 gryo_3d_sensitivity_addresses[] = {
46 HID_USAGE_SENSOR_DATA_ANGL_VELOCITY,
49 /* Channel definitions */
50 static const struct iio_chan_spec gyro_3d_channels[] = {
54 .channel2 = IIO_MOD_X,
55 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
56 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
57 BIT(IIO_CHAN_INFO_SCALE) |
58 BIT(IIO_CHAN_INFO_SAMP_FREQ) |
59 BIT(IIO_CHAN_INFO_HYSTERESIS),
60 .scan_index = CHANNEL_SCAN_INDEX_X,
64 .channel2 = IIO_MOD_Y,
65 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
66 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
67 BIT(IIO_CHAN_INFO_SCALE) |
68 BIT(IIO_CHAN_INFO_SAMP_FREQ) |
69 BIT(IIO_CHAN_INFO_HYSTERESIS),
70 .scan_index = CHANNEL_SCAN_INDEX_Y,
74 .channel2 = IIO_MOD_Z,
75 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
76 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
77 BIT(IIO_CHAN_INFO_SCALE) |
78 BIT(IIO_CHAN_INFO_SAMP_FREQ) |
79 BIT(IIO_CHAN_INFO_HYSTERESIS),
80 .scan_index = CHANNEL_SCAN_INDEX_Z,
82 IIO_CHAN_SOFT_TIMESTAMP(CHANNEL_SCAN_INDEX_TIMESTAMP)
85 /* Adjust channel real bits based on report descriptor */
86 static void gyro_3d_adjust_channel_bit_mask(struct iio_chan_spec *channels,
87 int channel, int size)
89 channels[channel].scan_type.sign = 's';
90 /* Real storage bits will change based on the report desc. */
91 channels[channel].scan_type.realbits = size * 8;
92 /* Maximum size of a sample to capture is u32 */
93 channels[channel].scan_type.storagebits = sizeof(u32) * 8;
96 /* Channel read_raw handler */
97 static int gyro_3d_read_raw(struct iio_dev *indio_dev,
98 struct iio_chan_spec const *chan,
102 struct gyro_3d_state *gyro_state = iio_priv(indio_dev);
111 case IIO_CHAN_INFO_RAW:
112 hid_sensor_power_state(&gyro_state->common_attributes, true);
113 report_id = gyro_state->gyro[chan->scan_index].report_id;
114 min = gyro_state->gyro[chan->scan_index].logical_minimum;
115 address = gyro_3d_addresses[chan->scan_index];
117 *val = sensor_hub_input_attr_get_raw_value(
118 gyro_state->common_attributes.hsdev,
119 HID_USAGE_SENSOR_GYRO_3D, address,
125 hid_sensor_power_state(&gyro_state->common_attributes,
129 hid_sensor_power_state(&gyro_state->common_attributes, false);
130 ret_type = IIO_VAL_INT;
132 case IIO_CHAN_INFO_SCALE:
133 *val = gyro_state->scale_pre_decml;
134 *val2 = gyro_state->scale_post_decml;
135 ret_type = gyro_state->scale_precision;
137 case IIO_CHAN_INFO_OFFSET:
138 *val = gyro_state->value_offset;
139 ret_type = IIO_VAL_INT;
141 case IIO_CHAN_INFO_SAMP_FREQ:
142 ret_type = hid_sensor_read_samp_freq_value(
143 &gyro_state->common_attributes, val, val2);
145 case IIO_CHAN_INFO_HYSTERESIS:
146 ret_type = hid_sensor_read_raw_hyst_value(
147 &gyro_state->common_attributes, val, val2);
157 /* Channel write_raw handler */
158 static int gyro_3d_write_raw(struct iio_dev *indio_dev,
159 struct iio_chan_spec const *chan,
164 struct gyro_3d_state *gyro_state = iio_priv(indio_dev);
168 case IIO_CHAN_INFO_SAMP_FREQ:
169 ret = hid_sensor_write_samp_freq_value(
170 &gyro_state->common_attributes, val, val2);
172 case IIO_CHAN_INFO_HYSTERESIS:
173 ret = hid_sensor_write_raw_hyst_value(
174 &gyro_state->common_attributes, val, val2);
183 static const struct iio_info gyro_3d_info = {
184 .read_raw = &gyro_3d_read_raw,
185 .write_raw = &gyro_3d_write_raw,
188 /* Callback handler to send event after all samples are received and captured */
189 static int gyro_3d_proc_event(struct hid_sensor_hub_device *hsdev,
193 struct iio_dev *indio_dev = platform_get_drvdata(priv);
194 struct gyro_3d_state *gyro_state = iio_priv(indio_dev);
196 dev_dbg(&indio_dev->dev, "gyro_3d_proc_event\n");
197 if (atomic_read(&gyro_state->common_attributes.data_ready)) {
198 if (!gyro_state->timestamp)
199 gyro_state->timestamp = iio_get_time_ns(indio_dev);
201 iio_push_to_buffers_with_timestamp(indio_dev, &gyro_state->scan,
202 gyro_state->timestamp);
204 gyro_state->timestamp = 0;
210 /* Capture samples in local storage */
211 static int gyro_3d_capture_sample(struct hid_sensor_hub_device *hsdev,
213 size_t raw_len, char *raw_data,
216 struct iio_dev *indio_dev = platform_get_drvdata(priv);
217 struct gyro_3d_state *gyro_state = iio_priv(indio_dev);
222 case HID_USAGE_SENSOR_ANGL_VELOCITY_X_AXIS:
223 case HID_USAGE_SENSOR_ANGL_VELOCITY_Y_AXIS:
224 case HID_USAGE_SENSOR_ANGL_VELOCITY_Z_AXIS:
225 offset = usage_id - HID_USAGE_SENSOR_ANGL_VELOCITY_X_AXIS;
226 gyro_state->scan.gyro_val[CHANNEL_SCAN_INDEX_X + offset] =
230 case HID_USAGE_SENSOR_TIME_TIMESTAMP:
231 gyro_state->timestamp =
232 hid_sensor_convert_timestamp(&gyro_state->common_attributes,
242 /* Parse report which is specific to an usage id*/
243 static int gyro_3d_parse_report(struct platform_device *pdev,
244 struct hid_sensor_hub_device *hsdev,
245 struct iio_chan_spec *channels,
247 struct gyro_3d_state *st)
252 for (i = 0; i <= CHANNEL_SCAN_INDEX_Z; ++i) {
253 ret = sensor_hub_input_get_attribute_info(hsdev,
256 HID_USAGE_SENSOR_ANGL_VELOCITY_X_AXIS + i,
257 &st->gyro[CHANNEL_SCAN_INDEX_X + i]);
260 gyro_3d_adjust_channel_bit_mask(channels,
261 CHANNEL_SCAN_INDEX_X + i,
262 st->gyro[CHANNEL_SCAN_INDEX_X + i].size);
264 dev_dbg(&pdev->dev, "gyro_3d %x:%x, %x:%x, %x:%x\n",
266 st->gyro[0].report_id,
267 st->gyro[1].index, st->gyro[1].report_id,
268 st->gyro[2].index, st->gyro[2].report_id);
270 st->scale_precision = hid_sensor_format_scale(
271 HID_USAGE_SENSOR_GYRO_3D,
272 &st->gyro[CHANNEL_SCAN_INDEX_X],
273 &st->scale_pre_decml, &st->scale_post_decml);
278 /* Function to initialize the processing for usage id */
279 static int hid_gyro_3d_probe(struct platform_device *pdev)
282 static const char *name = "gyro_3d";
283 struct iio_dev *indio_dev;
284 struct gyro_3d_state *gyro_state;
285 struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
287 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*gyro_state));
290 platform_set_drvdata(pdev, indio_dev);
292 gyro_state = iio_priv(indio_dev);
293 gyro_state->common_attributes.hsdev = hsdev;
294 gyro_state->common_attributes.pdev = pdev;
296 ret = hid_sensor_parse_common_attributes(hsdev,
297 HID_USAGE_SENSOR_GYRO_3D,
298 &gyro_state->common_attributes,
299 gryo_3d_sensitivity_addresses,
300 ARRAY_SIZE(gryo_3d_sensitivity_addresses));
302 dev_err(&pdev->dev, "failed to setup common attributes\n");
306 indio_dev->channels = devm_kmemdup(&pdev->dev, gyro_3d_channels,
307 sizeof(gyro_3d_channels), GFP_KERNEL);
308 if (!indio_dev->channels) {
309 dev_err(&pdev->dev, "failed to duplicate channels\n");
313 ret = gyro_3d_parse_report(pdev, hsdev,
314 (struct iio_chan_spec *)indio_dev->channels,
315 HID_USAGE_SENSOR_GYRO_3D, gyro_state);
317 dev_err(&pdev->dev, "failed to setup attributes\n");
321 indio_dev->num_channels = ARRAY_SIZE(gyro_3d_channels);
322 indio_dev->info = &gyro_3d_info;
323 indio_dev->name = name;
324 indio_dev->modes = INDIO_DIRECT_MODE;
326 atomic_set(&gyro_state->common_attributes.data_ready, 0);
328 ret = hid_sensor_setup_trigger(indio_dev, name,
329 &gyro_state->common_attributes);
331 dev_err(&pdev->dev, "trigger setup failed\n");
335 ret = iio_device_register(indio_dev);
337 dev_err(&pdev->dev, "device register failed\n");
338 goto error_remove_trigger;
341 gyro_state->callbacks.send_event = gyro_3d_proc_event;
342 gyro_state->callbacks.capture_sample = gyro_3d_capture_sample;
343 gyro_state->callbacks.pdev = pdev;
344 ret = sensor_hub_register_callback(hsdev, HID_USAGE_SENSOR_GYRO_3D,
345 &gyro_state->callbacks);
347 dev_err(&pdev->dev, "callback reg failed\n");
348 goto error_iio_unreg;
354 iio_device_unregister(indio_dev);
355 error_remove_trigger:
356 hid_sensor_remove_trigger(indio_dev, &gyro_state->common_attributes);
360 /* Function to deinitialize the processing for usage id */
361 static int hid_gyro_3d_remove(struct platform_device *pdev)
363 struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
364 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
365 struct gyro_3d_state *gyro_state = iio_priv(indio_dev);
367 sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_GYRO_3D);
368 iio_device_unregister(indio_dev);
369 hid_sensor_remove_trigger(indio_dev, &gyro_state->common_attributes);
374 static const struct platform_device_id hid_gyro_3d_ids[] = {
376 /* Format: HID-SENSOR-usage_id_in_hex_lowercase */
377 .name = "HID-SENSOR-200076",
381 MODULE_DEVICE_TABLE(platform, hid_gyro_3d_ids);
383 static struct platform_driver hid_gyro_3d_platform_driver = {
384 .id_table = hid_gyro_3d_ids,
386 .name = KBUILD_MODNAME,
387 .pm = &hid_sensor_pm_ops,
389 .probe = hid_gyro_3d_probe,
390 .remove = hid_gyro_3d_remove,
392 module_platform_driver(hid_gyro_3d_platform_driver);
394 MODULE_DESCRIPTION("HID Sensor Gyroscope 3D");
396 MODULE_LICENSE("GPL");
397 MODULE_IMPORT_NS(IIO_HID);