3 * Copyright (c) 2012, 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
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
19 #include <linux/device.h>
20 #include <linux/platform_device.h>
21 #include <linux/module.h>
22 #include <linux/interrupt.h>
23 #include <linux/irq.h>
24 #include <linux/slab.h>
25 #include <linux/delay.h>
26 #include <linux/hid-sensor-hub.h>
27 #include <linux/iio/iio.h>
28 #include <linux/iio/sysfs.h>
29 #include <linux/iio/buffer.h>
30 #include <linux/iio/trigger_consumer.h>
31 #include <linux/iio/triggered_buffer.h>
32 #include "../common/hid-sensors/hid-sensor-trigger.h"
34 enum gyro_3d_channel
{
41 struct gyro_3d_state
{
42 struct hid_sensor_hub_callbacks callbacks
;
43 struct hid_sensor_common common_attributes
;
44 struct hid_sensor_hub_attribute_info gyro
[GYRO_3D_CHANNEL_MAX
];
45 u32 gyro_val
[GYRO_3D_CHANNEL_MAX
];
52 static const u32 gyro_3d_addresses
[GYRO_3D_CHANNEL_MAX
] = {
53 HID_USAGE_SENSOR_ANGL_VELOCITY_X_AXIS
,
54 HID_USAGE_SENSOR_ANGL_VELOCITY_Y_AXIS
,
55 HID_USAGE_SENSOR_ANGL_VELOCITY_Z_AXIS
58 /* Channel definitions */
59 static const struct iio_chan_spec gyro_3d_channels
[] = {
63 .channel2
= IIO_MOD_X
,
64 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
65 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_OFFSET
) |
66 BIT(IIO_CHAN_INFO_SCALE
) |
67 BIT(IIO_CHAN_INFO_SAMP_FREQ
) |
68 BIT(IIO_CHAN_INFO_HYSTERESIS
),
69 .scan_index
= CHANNEL_SCAN_INDEX_X
,
73 .channel2
= IIO_MOD_Y
,
74 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
75 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_OFFSET
) |
76 BIT(IIO_CHAN_INFO_SCALE
) |
77 BIT(IIO_CHAN_INFO_SAMP_FREQ
) |
78 BIT(IIO_CHAN_INFO_HYSTERESIS
),
79 .scan_index
= CHANNEL_SCAN_INDEX_Y
,
83 .channel2
= IIO_MOD_Z
,
84 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
85 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_OFFSET
) |
86 BIT(IIO_CHAN_INFO_SCALE
) |
87 BIT(IIO_CHAN_INFO_SAMP_FREQ
) |
88 BIT(IIO_CHAN_INFO_HYSTERESIS
),
89 .scan_index
= CHANNEL_SCAN_INDEX_Z
,
93 /* Adjust channel real bits based on report descriptor */
94 static void gyro_3d_adjust_channel_bit_mask(struct iio_chan_spec
*channels
,
95 int channel
, int size
)
97 channels
[channel
].scan_type
.sign
= 's';
98 /* Real storage bits will change based on the report desc. */
99 channels
[channel
].scan_type
.realbits
= size
* 8;
100 /* Maximum size of a sample to capture is u32 */
101 channels
[channel
].scan_type
.storagebits
= sizeof(u32
) * 8;
104 /* Channel read_raw handler */
105 static int gyro_3d_read_raw(struct iio_dev
*indio_dev
,
106 struct iio_chan_spec
const *chan
,
110 struct gyro_3d_state
*gyro_state
= iio_priv(indio_dev
);
120 poll_value
= hid_sensor_read_poll_value(
121 &gyro_state
->common_attributes
);
125 hid_sensor_power_state(&gyro_state
->common_attributes
, true);
126 msleep_interruptible(poll_value
* 2);
127 report_id
= gyro_state
->gyro
[chan
->scan_index
].report_id
;
128 address
= gyro_3d_addresses
[chan
->scan_index
];
130 *val
= sensor_hub_input_attr_get_raw_value(
131 gyro_state
->common_attributes
.hsdev
,
132 HID_USAGE_SENSOR_GYRO_3D
, address
,
136 hid_sensor_power_state(&gyro_state
->common_attributes
,
140 hid_sensor_power_state(&gyro_state
->common_attributes
, false);
141 ret_type
= IIO_VAL_INT
;
143 case IIO_CHAN_INFO_SCALE
:
144 *val
= gyro_state
->scale_pre_decml
;
145 *val2
= gyro_state
->scale_post_decml
;
146 ret_type
= gyro_state
->scale_precision
;
148 case IIO_CHAN_INFO_OFFSET
:
149 *val
= gyro_state
->value_offset
;
150 ret_type
= IIO_VAL_INT
;
152 case IIO_CHAN_INFO_SAMP_FREQ
:
153 ret_type
= hid_sensor_read_samp_freq_value(
154 &gyro_state
->common_attributes
, val
, val2
);
156 case IIO_CHAN_INFO_HYSTERESIS
:
157 ret_type
= hid_sensor_read_raw_hyst_value(
158 &gyro_state
->common_attributes
, val
, val2
);
168 /* Channel write_raw handler */
169 static int gyro_3d_write_raw(struct iio_dev
*indio_dev
,
170 struct iio_chan_spec
const *chan
,
175 struct gyro_3d_state
*gyro_state
= iio_priv(indio_dev
);
179 case IIO_CHAN_INFO_SAMP_FREQ
:
180 ret
= hid_sensor_write_samp_freq_value(
181 &gyro_state
->common_attributes
, val
, val2
);
183 case IIO_CHAN_INFO_HYSTERESIS
:
184 ret
= hid_sensor_write_raw_hyst_value(
185 &gyro_state
->common_attributes
, val
, val2
);
194 static const struct iio_info gyro_3d_info
= {
195 .driver_module
= THIS_MODULE
,
196 .read_raw
= &gyro_3d_read_raw
,
197 .write_raw
= &gyro_3d_write_raw
,
200 /* Function to push data to buffer */
201 static void hid_sensor_push_data(struct iio_dev
*indio_dev
, const void *data
,
204 dev_dbg(&indio_dev
->dev
, "hid_sensor_push_data\n");
205 iio_push_to_buffers(indio_dev
, data
);
208 /* Callback handler to send event after all samples are received and captured */
209 static int gyro_3d_proc_event(struct hid_sensor_hub_device
*hsdev
,
213 struct iio_dev
*indio_dev
= platform_get_drvdata(priv
);
214 struct gyro_3d_state
*gyro_state
= iio_priv(indio_dev
);
216 dev_dbg(&indio_dev
->dev
, "gyro_3d_proc_event\n");
217 if (atomic_read(&gyro_state
->common_attributes
.data_ready
))
218 hid_sensor_push_data(indio_dev
,
219 gyro_state
->gyro_val
,
220 sizeof(gyro_state
->gyro_val
));
225 /* Capture samples in local storage */
226 static int gyro_3d_capture_sample(struct hid_sensor_hub_device
*hsdev
,
228 size_t raw_len
, char *raw_data
,
231 struct iio_dev
*indio_dev
= platform_get_drvdata(priv
);
232 struct gyro_3d_state
*gyro_state
= iio_priv(indio_dev
);
237 case HID_USAGE_SENSOR_ANGL_VELOCITY_X_AXIS
:
238 case HID_USAGE_SENSOR_ANGL_VELOCITY_Y_AXIS
:
239 case HID_USAGE_SENSOR_ANGL_VELOCITY_Z_AXIS
:
240 offset
= usage_id
- HID_USAGE_SENSOR_ANGL_VELOCITY_X_AXIS
;
241 gyro_state
->gyro_val
[CHANNEL_SCAN_INDEX_X
+ offset
] =
252 /* Parse report which is specific to an usage id*/
253 static int gyro_3d_parse_report(struct platform_device
*pdev
,
254 struct hid_sensor_hub_device
*hsdev
,
255 struct iio_chan_spec
*channels
,
257 struct gyro_3d_state
*st
)
262 for (i
= 0; i
<= CHANNEL_SCAN_INDEX_Z
; ++i
) {
263 ret
= sensor_hub_input_get_attribute_info(hsdev
,
266 HID_USAGE_SENSOR_ANGL_VELOCITY_X_AXIS
+ i
,
267 &st
->gyro
[CHANNEL_SCAN_INDEX_X
+ i
]);
270 gyro_3d_adjust_channel_bit_mask(channels
,
271 CHANNEL_SCAN_INDEX_X
+ i
,
272 st
->gyro
[CHANNEL_SCAN_INDEX_X
+ i
].size
);
274 dev_dbg(&pdev
->dev
, "gyro_3d %x:%x, %x:%x, %x:%x\n",
276 st
->gyro
[0].report_id
,
277 st
->gyro
[1].index
, st
->gyro
[1].report_id
,
278 st
->gyro
[2].index
, st
->gyro
[2].report_id
);
280 st
->scale_precision
= hid_sensor_format_scale(
281 HID_USAGE_SENSOR_GYRO_3D
,
282 &st
->gyro
[CHANNEL_SCAN_INDEX_X
],
283 &st
->scale_pre_decml
, &st
->scale_post_decml
);
285 /* Set Sensitivity field ids, when there is no individual modifier */
286 if (st
->common_attributes
.sensitivity
.index
< 0) {
287 sensor_hub_input_get_attribute_info(hsdev
,
288 HID_FEATURE_REPORT
, usage_id
,
289 HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS
|
290 HID_USAGE_SENSOR_DATA_ANGL_VELOCITY
,
291 &st
->common_attributes
.sensitivity
);
292 dev_dbg(&pdev
->dev
, "Sensitivity index:report %d:%d\n",
293 st
->common_attributes
.sensitivity
.index
,
294 st
->common_attributes
.sensitivity
.report_id
);
299 /* Function to initialize the processing for usage id */
300 static int hid_gyro_3d_probe(struct platform_device
*pdev
)
303 static const char *name
= "gyro_3d";
304 struct iio_dev
*indio_dev
;
305 struct gyro_3d_state
*gyro_state
;
306 struct hid_sensor_hub_device
*hsdev
= pdev
->dev
.platform_data
;
307 struct iio_chan_spec
*channels
;
309 indio_dev
= devm_iio_device_alloc(&pdev
->dev
, sizeof(*gyro_state
));
312 platform_set_drvdata(pdev
, indio_dev
);
314 gyro_state
= iio_priv(indio_dev
);
315 gyro_state
->common_attributes
.hsdev
= hsdev
;
316 gyro_state
->common_attributes
.pdev
= pdev
;
318 ret
= hid_sensor_parse_common_attributes(hsdev
,
319 HID_USAGE_SENSOR_GYRO_3D
,
320 &gyro_state
->common_attributes
);
322 dev_err(&pdev
->dev
, "failed to setup common attributes\n");
326 channels
= kmemdup(gyro_3d_channels
, sizeof(gyro_3d_channels
),
329 dev_err(&pdev
->dev
, "failed to duplicate channels\n");
333 ret
= gyro_3d_parse_report(pdev
, hsdev
, channels
,
334 HID_USAGE_SENSOR_GYRO_3D
, gyro_state
);
336 dev_err(&pdev
->dev
, "failed to setup attributes\n");
337 goto error_free_dev_mem
;
340 indio_dev
->channels
= channels
;
341 indio_dev
->num_channels
= ARRAY_SIZE(gyro_3d_channels
);
342 indio_dev
->dev
.parent
= &pdev
->dev
;
343 indio_dev
->info
= &gyro_3d_info
;
344 indio_dev
->name
= name
;
345 indio_dev
->modes
= INDIO_DIRECT_MODE
;
347 ret
= iio_triggered_buffer_setup(indio_dev
, &iio_pollfunc_store_time
,
350 dev_err(&pdev
->dev
, "failed to initialize trigger buffer\n");
351 goto error_free_dev_mem
;
353 atomic_set(&gyro_state
->common_attributes
.data_ready
, 0);
354 ret
= hid_sensor_setup_trigger(indio_dev
, name
,
355 &gyro_state
->common_attributes
);
357 dev_err(&pdev
->dev
, "trigger setup failed\n");
358 goto error_unreg_buffer_funcs
;
361 ret
= iio_device_register(indio_dev
);
363 dev_err(&pdev
->dev
, "device register failed\n");
364 goto error_remove_trigger
;
367 gyro_state
->callbacks
.send_event
= gyro_3d_proc_event
;
368 gyro_state
->callbacks
.capture_sample
= gyro_3d_capture_sample
;
369 gyro_state
->callbacks
.pdev
= pdev
;
370 ret
= sensor_hub_register_callback(hsdev
, HID_USAGE_SENSOR_GYRO_3D
,
371 &gyro_state
->callbacks
);
373 dev_err(&pdev
->dev
, "callback reg failed\n");
374 goto error_iio_unreg
;
380 iio_device_unregister(indio_dev
);
381 error_remove_trigger
:
382 hid_sensor_remove_trigger(&gyro_state
->common_attributes
);
383 error_unreg_buffer_funcs
:
384 iio_triggered_buffer_cleanup(indio_dev
);
386 kfree(indio_dev
->channels
);
390 /* Function to deinitialize the processing for usage id */
391 static int hid_gyro_3d_remove(struct platform_device
*pdev
)
393 struct hid_sensor_hub_device
*hsdev
= pdev
->dev
.platform_data
;
394 struct iio_dev
*indio_dev
= platform_get_drvdata(pdev
);
395 struct gyro_3d_state
*gyro_state
= iio_priv(indio_dev
);
397 sensor_hub_remove_callback(hsdev
, HID_USAGE_SENSOR_GYRO_3D
);
398 iio_device_unregister(indio_dev
);
399 hid_sensor_remove_trigger(&gyro_state
->common_attributes
);
400 iio_triggered_buffer_cleanup(indio_dev
);
401 kfree(indio_dev
->channels
);
406 static struct platform_device_id hid_gyro_3d_ids
[] = {
408 /* Format: HID-SENSOR-usage_id_in_hex_lowercase */
409 .name
= "HID-SENSOR-200076",
413 MODULE_DEVICE_TABLE(platform
, hid_gyro_3d_ids
);
415 static struct platform_driver hid_gyro_3d_platform_driver
= {
416 .id_table
= hid_gyro_3d_ids
,
418 .name
= KBUILD_MODNAME
,
420 .probe
= hid_gyro_3d_probe
,
421 .remove
= hid_gyro_3d_remove
,
423 module_platform_driver(hid_gyro_3d_platform_driver
);
425 MODULE_DESCRIPTION("HID Sensor Gyroscope 3D");
426 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
427 MODULE_LICENSE("GPL");