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 "../common/hid-sensors/hid-sensor-trigger.h"
19 enum gyro_3d_channel
{
26 struct gyro_3d_state
{
27 struct hid_sensor_hub_callbacks callbacks
;
28 struct hid_sensor_common common_attributes
;
29 struct hid_sensor_hub_attribute_info gyro
[GYRO_3D_CHANNEL_MAX
];
30 u32 gyro_val
[GYRO_3D_CHANNEL_MAX
];
37 static const u32 gyro_3d_addresses
[GYRO_3D_CHANNEL_MAX
] = {
38 HID_USAGE_SENSOR_ANGL_VELOCITY_X_AXIS
,
39 HID_USAGE_SENSOR_ANGL_VELOCITY_Y_AXIS
,
40 HID_USAGE_SENSOR_ANGL_VELOCITY_Z_AXIS
43 /* Channel definitions */
44 static const struct iio_chan_spec gyro_3d_channels
[] = {
48 .channel2
= IIO_MOD_X
,
49 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
50 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_OFFSET
) |
51 BIT(IIO_CHAN_INFO_SCALE
) |
52 BIT(IIO_CHAN_INFO_SAMP_FREQ
) |
53 BIT(IIO_CHAN_INFO_HYSTERESIS
),
54 .scan_index
= CHANNEL_SCAN_INDEX_X
,
58 .channel2
= IIO_MOD_Y
,
59 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
60 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_OFFSET
) |
61 BIT(IIO_CHAN_INFO_SCALE
) |
62 BIT(IIO_CHAN_INFO_SAMP_FREQ
) |
63 BIT(IIO_CHAN_INFO_HYSTERESIS
),
64 .scan_index
= CHANNEL_SCAN_INDEX_Y
,
68 .channel2
= IIO_MOD_Z
,
69 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
70 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_OFFSET
) |
71 BIT(IIO_CHAN_INFO_SCALE
) |
72 BIT(IIO_CHAN_INFO_SAMP_FREQ
) |
73 BIT(IIO_CHAN_INFO_HYSTERESIS
),
74 .scan_index
= CHANNEL_SCAN_INDEX_Z
,
78 /* Adjust channel real bits based on report descriptor */
79 static void gyro_3d_adjust_channel_bit_mask(struct iio_chan_spec
*channels
,
80 int channel
, int size
)
82 channels
[channel
].scan_type
.sign
= 's';
83 /* Real storage bits will change based on the report desc. */
84 channels
[channel
].scan_type
.realbits
= size
* 8;
85 /* Maximum size of a sample to capture is u32 */
86 channels
[channel
].scan_type
.storagebits
= sizeof(u32
) * 8;
89 /* Channel read_raw handler */
90 static int gyro_3d_read_raw(struct iio_dev
*indio_dev
,
91 struct iio_chan_spec
const *chan
,
95 struct gyro_3d_state
*gyro_state
= iio_priv(indio_dev
);
104 case IIO_CHAN_INFO_RAW
:
105 hid_sensor_power_state(&gyro_state
->common_attributes
, true);
106 report_id
= gyro_state
->gyro
[chan
->scan_index
].report_id
;
107 min
= gyro_state
->gyro
[chan
->scan_index
].logical_minimum
;
108 address
= gyro_3d_addresses
[chan
->scan_index
];
110 *val
= sensor_hub_input_attr_get_raw_value(
111 gyro_state
->common_attributes
.hsdev
,
112 HID_USAGE_SENSOR_GYRO_3D
, address
,
118 hid_sensor_power_state(&gyro_state
->common_attributes
,
122 hid_sensor_power_state(&gyro_state
->common_attributes
, false);
123 ret_type
= IIO_VAL_INT
;
125 case IIO_CHAN_INFO_SCALE
:
126 *val
= gyro_state
->scale_pre_decml
;
127 *val2
= gyro_state
->scale_post_decml
;
128 ret_type
= gyro_state
->scale_precision
;
130 case IIO_CHAN_INFO_OFFSET
:
131 *val
= gyro_state
->value_offset
;
132 ret_type
= IIO_VAL_INT
;
134 case IIO_CHAN_INFO_SAMP_FREQ
:
135 ret_type
= hid_sensor_read_samp_freq_value(
136 &gyro_state
->common_attributes
, val
, val2
);
138 case IIO_CHAN_INFO_HYSTERESIS
:
139 ret_type
= hid_sensor_read_raw_hyst_value(
140 &gyro_state
->common_attributes
, val
, val2
);
150 /* Channel write_raw handler */
151 static int gyro_3d_write_raw(struct iio_dev
*indio_dev
,
152 struct iio_chan_spec
const *chan
,
157 struct gyro_3d_state
*gyro_state
= iio_priv(indio_dev
);
161 case IIO_CHAN_INFO_SAMP_FREQ
:
162 ret
= hid_sensor_write_samp_freq_value(
163 &gyro_state
->common_attributes
, val
, val2
);
165 case IIO_CHAN_INFO_HYSTERESIS
:
166 ret
= hid_sensor_write_raw_hyst_value(
167 &gyro_state
->common_attributes
, val
, val2
);
176 static const struct iio_info gyro_3d_info
= {
177 .read_raw
= &gyro_3d_read_raw
,
178 .write_raw
= &gyro_3d_write_raw
,
181 /* Function to push data to buffer */
182 static void hid_sensor_push_data(struct iio_dev
*indio_dev
, const void *data
,
185 dev_dbg(&indio_dev
->dev
, "hid_sensor_push_data\n");
186 iio_push_to_buffers(indio_dev
, data
);
189 /* Callback handler to send event after all samples are received and captured */
190 static int gyro_3d_proc_event(struct hid_sensor_hub_device
*hsdev
,
194 struct iio_dev
*indio_dev
= platform_get_drvdata(priv
);
195 struct gyro_3d_state
*gyro_state
= iio_priv(indio_dev
);
197 dev_dbg(&indio_dev
->dev
, "gyro_3d_proc_event\n");
198 if (atomic_read(&gyro_state
->common_attributes
.data_ready
))
199 hid_sensor_push_data(indio_dev
,
200 gyro_state
->gyro_val
,
201 sizeof(gyro_state
->gyro_val
));
206 /* Capture samples in local storage */
207 static int gyro_3d_capture_sample(struct hid_sensor_hub_device
*hsdev
,
209 size_t raw_len
, char *raw_data
,
212 struct iio_dev
*indio_dev
= platform_get_drvdata(priv
);
213 struct gyro_3d_state
*gyro_state
= iio_priv(indio_dev
);
218 case HID_USAGE_SENSOR_ANGL_VELOCITY_X_AXIS
:
219 case HID_USAGE_SENSOR_ANGL_VELOCITY_Y_AXIS
:
220 case HID_USAGE_SENSOR_ANGL_VELOCITY_Z_AXIS
:
221 offset
= usage_id
- HID_USAGE_SENSOR_ANGL_VELOCITY_X_AXIS
;
222 gyro_state
->gyro_val
[CHANNEL_SCAN_INDEX_X
+ offset
] =
233 /* Parse report which is specific to an usage id*/
234 static int gyro_3d_parse_report(struct platform_device
*pdev
,
235 struct hid_sensor_hub_device
*hsdev
,
236 struct iio_chan_spec
*channels
,
238 struct gyro_3d_state
*st
)
243 for (i
= 0; i
<= CHANNEL_SCAN_INDEX_Z
; ++i
) {
244 ret
= sensor_hub_input_get_attribute_info(hsdev
,
247 HID_USAGE_SENSOR_ANGL_VELOCITY_X_AXIS
+ i
,
248 &st
->gyro
[CHANNEL_SCAN_INDEX_X
+ i
]);
251 gyro_3d_adjust_channel_bit_mask(channels
,
252 CHANNEL_SCAN_INDEX_X
+ i
,
253 st
->gyro
[CHANNEL_SCAN_INDEX_X
+ i
].size
);
255 dev_dbg(&pdev
->dev
, "gyro_3d %x:%x, %x:%x, %x:%x\n",
257 st
->gyro
[0].report_id
,
258 st
->gyro
[1].index
, st
->gyro
[1].report_id
,
259 st
->gyro
[2].index
, st
->gyro
[2].report_id
);
261 st
->scale_precision
= hid_sensor_format_scale(
262 HID_USAGE_SENSOR_GYRO_3D
,
263 &st
->gyro
[CHANNEL_SCAN_INDEX_X
],
264 &st
->scale_pre_decml
, &st
->scale_post_decml
);
266 /* Set Sensitivity field ids, when there is no individual modifier */
267 if (st
->common_attributes
.sensitivity
.index
< 0) {
268 sensor_hub_input_get_attribute_info(hsdev
,
269 HID_FEATURE_REPORT
, usage_id
,
270 HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS
|
271 HID_USAGE_SENSOR_DATA_ANGL_VELOCITY
,
272 &st
->common_attributes
.sensitivity
);
273 dev_dbg(&pdev
->dev
, "Sensitivity index:report %d:%d\n",
274 st
->common_attributes
.sensitivity
.index
,
275 st
->common_attributes
.sensitivity
.report_id
);
280 /* Function to initialize the processing for usage id */
281 static int hid_gyro_3d_probe(struct platform_device
*pdev
)
284 static const char *name
= "gyro_3d";
285 struct iio_dev
*indio_dev
;
286 struct gyro_3d_state
*gyro_state
;
287 struct hid_sensor_hub_device
*hsdev
= pdev
->dev
.platform_data
;
289 indio_dev
= devm_iio_device_alloc(&pdev
->dev
, sizeof(*gyro_state
));
292 platform_set_drvdata(pdev
, indio_dev
);
294 gyro_state
= iio_priv(indio_dev
);
295 gyro_state
->common_attributes
.hsdev
= hsdev
;
296 gyro_state
->common_attributes
.pdev
= pdev
;
298 ret
= hid_sensor_parse_common_attributes(hsdev
,
299 HID_USAGE_SENSOR_GYRO_3D
,
300 &gyro_state
->common_attributes
);
302 dev_err(&pdev
->dev
, "failed to setup common attributes\n");
306 indio_dev
->channels
= kmemdup(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");
318 goto error_free_dev_mem
;
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");
332 goto error_free_dev_mem
;
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
);
358 kfree(indio_dev
->channels
);
362 /* Function to deinitialize the processing for usage id */
363 static int hid_gyro_3d_remove(struct platform_device
*pdev
)
365 struct hid_sensor_hub_device
*hsdev
= pdev
->dev
.platform_data
;
366 struct iio_dev
*indio_dev
= platform_get_drvdata(pdev
);
367 struct gyro_3d_state
*gyro_state
= iio_priv(indio_dev
);
369 sensor_hub_remove_callback(hsdev
, HID_USAGE_SENSOR_GYRO_3D
);
370 iio_device_unregister(indio_dev
);
371 hid_sensor_remove_trigger(indio_dev
, &gyro_state
->common_attributes
);
372 kfree(indio_dev
->channels
);
377 static const struct platform_device_id hid_gyro_3d_ids
[] = {
379 /* Format: HID-SENSOR-usage_id_in_hex_lowercase */
380 .name
= "HID-SENSOR-200076",
384 MODULE_DEVICE_TABLE(platform
, hid_gyro_3d_ids
);
386 static struct platform_driver hid_gyro_3d_platform_driver
= {
387 .id_table
= hid_gyro_3d_ids
,
389 .name
= KBUILD_MODNAME
,
390 .pm
= &hid_sensor_pm_ops
,
392 .probe
= hid_gyro_3d_probe
,
393 .remove
= hid_gyro_3d_remove
,
395 module_platform_driver(hid_gyro_3d_platform_driver
);
397 MODULE_DESCRIPTION("HID Sensor Gyroscope 3D");
398 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
399 MODULE_LICENSE("GPL");