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 accel_3d_channel
{
28 struct accel_3d_state
{
29 struct hid_sensor_hub_callbacks callbacks
;
30 struct hid_sensor_common common_attributes
;
31 struct hid_sensor_hub_attribute_info accel
[ACCEL_3D_CHANNEL_MAX
];
32 /* Reserve for 3 channels + padding + timestamp */
33 u32 accel_val
[ACCEL_3D_CHANNEL_MAX
+ 3];
41 static const u32 accel_3d_addresses
[ACCEL_3D_CHANNEL_MAX
] = {
42 HID_USAGE_SENSOR_ACCEL_X_AXIS
,
43 HID_USAGE_SENSOR_ACCEL_Y_AXIS
,
44 HID_USAGE_SENSOR_ACCEL_Z_AXIS
47 /* Channel definitions */
48 static const struct iio_chan_spec accel_3d_channels
[] = {
52 .channel2
= IIO_MOD_X
,
53 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
54 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_OFFSET
) |
55 BIT(IIO_CHAN_INFO_SCALE
) |
56 BIT(IIO_CHAN_INFO_SAMP_FREQ
) |
57 BIT(IIO_CHAN_INFO_HYSTERESIS
),
58 .scan_index
= CHANNEL_SCAN_INDEX_X
,
62 .channel2
= IIO_MOD_Y
,
63 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
64 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_OFFSET
) |
65 BIT(IIO_CHAN_INFO_SCALE
) |
66 BIT(IIO_CHAN_INFO_SAMP_FREQ
) |
67 BIT(IIO_CHAN_INFO_HYSTERESIS
),
68 .scan_index
= CHANNEL_SCAN_INDEX_Y
,
72 .channel2
= IIO_MOD_Z
,
73 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
74 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_OFFSET
) |
75 BIT(IIO_CHAN_INFO_SCALE
) |
76 BIT(IIO_CHAN_INFO_SAMP_FREQ
) |
77 BIT(IIO_CHAN_INFO_HYSTERESIS
),
78 .scan_index
= CHANNEL_SCAN_INDEX_Z
,
80 IIO_CHAN_SOFT_TIMESTAMP(3)
83 /* Channel definitions */
84 static const struct iio_chan_spec gravity_channels
[] = {
88 .channel2
= IIO_MOD_X
,
89 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
90 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_OFFSET
) |
91 BIT(IIO_CHAN_INFO_SCALE
) |
92 BIT(IIO_CHAN_INFO_SAMP_FREQ
) |
93 BIT(IIO_CHAN_INFO_HYSTERESIS
),
94 .scan_index
= CHANNEL_SCAN_INDEX_X
,
98 .channel2
= IIO_MOD_Y
,
99 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
100 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_OFFSET
) |
101 BIT(IIO_CHAN_INFO_SCALE
) |
102 BIT(IIO_CHAN_INFO_SAMP_FREQ
) |
103 BIT(IIO_CHAN_INFO_HYSTERESIS
),
104 .scan_index
= CHANNEL_SCAN_INDEX_Y
,
108 .channel2
= IIO_MOD_Z
,
109 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
110 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_OFFSET
) |
111 BIT(IIO_CHAN_INFO_SCALE
) |
112 BIT(IIO_CHAN_INFO_SAMP_FREQ
) |
113 BIT(IIO_CHAN_INFO_HYSTERESIS
),
114 .scan_index
= CHANNEL_SCAN_INDEX_Z
,
118 /* Adjust channel real bits based on report descriptor */
119 static void accel_3d_adjust_channel_bit_mask(struct iio_chan_spec
*channels
,
120 int channel
, int size
)
122 channels
[channel
].scan_type
.sign
= 's';
123 /* Real storage bits will change based on the report desc. */
124 channels
[channel
].scan_type
.realbits
= size
* 8;
125 /* Maximum size of a sample to capture is u32 */
126 channels
[channel
].scan_type
.storagebits
= sizeof(u32
) * 8;
129 /* Channel read_raw handler */
130 static int accel_3d_read_raw(struct iio_dev
*indio_dev
,
131 struct iio_chan_spec
const *chan
,
135 struct accel_3d_state
*accel_state
= iio_priv(indio_dev
);
140 struct hid_sensor_hub_device
*hsdev
=
141 accel_state
->common_attributes
.hsdev
;
146 case IIO_CHAN_INFO_RAW
:
147 hid_sensor_power_state(&accel_state
->common_attributes
, true);
148 report_id
= accel_state
->accel
[chan
->scan_index
].report_id
;
149 min
= accel_state
->accel
[chan
->scan_index
].logical_minimum
;
150 address
= accel_3d_addresses
[chan
->scan_index
];
152 *val
= sensor_hub_input_attr_get_raw_value(
153 accel_state
->common_attributes
.hsdev
,
154 hsdev
->usage
, address
, report_id
,
159 hid_sensor_power_state(&accel_state
->common_attributes
,
163 hid_sensor_power_state(&accel_state
->common_attributes
, false);
164 ret_type
= IIO_VAL_INT
;
166 case IIO_CHAN_INFO_SCALE
:
167 *val
= accel_state
->scale_pre_decml
;
168 *val2
= accel_state
->scale_post_decml
;
169 ret_type
= accel_state
->scale_precision
;
171 case IIO_CHAN_INFO_OFFSET
:
172 *val
= accel_state
->value_offset
;
173 ret_type
= IIO_VAL_INT
;
175 case IIO_CHAN_INFO_SAMP_FREQ
:
176 ret_type
= hid_sensor_read_samp_freq_value(
177 &accel_state
->common_attributes
, val
, val2
);
179 case IIO_CHAN_INFO_HYSTERESIS
:
180 ret_type
= hid_sensor_read_raw_hyst_value(
181 &accel_state
->common_attributes
, val
, val2
);
191 /* Channel write_raw handler */
192 static int accel_3d_write_raw(struct iio_dev
*indio_dev
,
193 struct iio_chan_spec
const *chan
,
198 struct accel_3d_state
*accel_state
= iio_priv(indio_dev
);
202 case IIO_CHAN_INFO_SAMP_FREQ
:
203 ret
= hid_sensor_write_samp_freq_value(
204 &accel_state
->common_attributes
, val
, val2
);
206 case IIO_CHAN_INFO_HYSTERESIS
:
207 ret
= hid_sensor_write_raw_hyst_value(
208 &accel_state
->common_attributes
, val
, val2
);
217 static const struct iio_info accel_3d_info
= {
218 .read_raw
= &accel_3d_read_raw
,
219 .write_raw
= &accel_3d_write_raw
,
222 /* Function to push data to buffer */
223 static void hid_sensor_push_data(struct iio_dev
*indio_dev
, void *data
,
224 int len
, int64_t timestamp
)
226 dev_dbg(&indio_dev
->dev
, "hid_sensor_push_data\n");
227 iio_push_to_buffers_with_timestamp(indio_dev
, data
, timestamp
);
230 /* Callback handler to send event after all samples are received and captured */
231 static int accel_3d_proc_event(struct hid_sensor_hub_device
*hsdev
,
235 struct iio_dev
*indio_dev
= platform_get_drvdata(priv
);
236 struct accel_3d_state
*accel_state
= iio_priv(indio_dev
);
238 dev_dbg(&indio_dev
->dev
, "accel_3d_proc_event\n");
239 if (atomic_read(&accel_state
->common_attributes
.data_ready
)) {
240 if (!accel_state
->timestamp
)
241 accel_state
->timestamp
= iio_get_time_ns(indio_dev
);
243 hid_sensor_push_data(indio_dev
,
244 accel_state
->accel_val
,
245 sizeof(accel_state
->accel_val
),
246 accel_state
->timestamp
);
248 accel_state
->timestamp
= 0;
254 /* Capture samples in local storage */
255 static int accel_3d_capture_sample(struct hid_sensor_hub_device
*hsdev
,
257 size_t raw_len
, char *raw_data
,
260 struct iio_dev
*indio_dev
= platform_get_drvdata(priv
);
261 struct accel_3d_state
*accel_state
= iio_priv(indio_dev
);
266 case HID_USAGE_SENSOR_ACCEL_X_AXIS
:
267 case HID_USAGE_SENSOR_ACCEL_Y_AXIS
:
268 case HID_USAGE_SENSOR_ACCEL_Z_AXIS
:
269 offset
= usage_id
- HID_USAGE_SENSOR_ACCEL_X_AXIS
;
270 accel_state
->accel_val
[CHANNEL_SCAN_INDEX_X
+ offset
] =
274 case HID_USAGE_SENSOR_TIME_TIMESTAMP
:
275 accel_state
->timestamp
=
276 hid_sensor_convert_timestamp(
277 &accel_state
->common_attributes
,
278 *(int64_t *)raw_data
);
287 /* Parse report which is specific to an usage id*/
288 static int accel_3d_parse_report(struct platform_device
*pdev
,
289 struct hid_sensor_hub_device
*hsdev
,
290 struct iio_chan_spec
*channels
,
292 struct accel_3d_state
*st
)
297 for (i
= 0; i
<= CHANNEL_SCAN_INDEX_Z
; ++i
) {
298 ret
= sensor_hub_input_get_attribute_info(hsdev
,
301 HID_USAGE_SENSOR_ACCEL_X_AXIS
+ i
,
302 &st
->accel
[CHANNEL_SCAN_INDEX_X
+ i
]);
305 accel_3d_adjust_channel_bit_mask(channels
,
306 CHANNEL_SCAN_INDEX_X
+ i
,
307 st
->accel
[CHANNEL_SCAN_INDEX_X
+ i
].size
);
309 dev_dbg(&pdev
->dev
, "accel_3d %x:%x, %x:%x, %x:%x\n",
311 st
->accel
[0].report_id
,
312 st
->accel
[1].index
, st
->accel
[1].report_id
,
313 st
->accel
[2].index
, st
->accel
[2].report_id
);
315 st
->scale_precision
= hid_sensor_format_scale(
317 &st
->accel
[CHANNEL_SCAN_INDEX_X
],
318 &st
->scale_pre_decml
, &st
->scale_post_decml
);
320 /* Set Sensitivity field ids, when there is no individual modifier */
321 if (st
->common_attributes
.sensitivity
.index
< 0) {
322 sensor_hub_input_get_attribute_info(hsdev
,
323 HID_FEATURE_REPORT
, usage_id
,
324 HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS
|
325 HID_USAGE_SENSOR_DATA_ACCELERATION
,
326 &st
->common_attributes
.sensitivity
);
327 dev_dbg(&pdev
->dev
, "Sensitivity index:report %d:%d\n",
328 st
->common_attributes
.sensitivity
.index
,
329 st
->common_attributes
.sensitivity
.report_id
);
335 /* Function to initialize the processing for usage id */
336 static int hid_accel_3d_probe(struct platform_device
*pdev
)
340 struct iio_dev
*indio_dev
;
341 struct accel_3d_state
*accel_state
;
342 const struct iio_chan_spec
*channel_spec
;
345 struct hid_sensor_hub_device
*hsdev
= pdev
->dev
.platform_data
;
347 indio_dev
= devm_iio_device_alloc(&pdev
->dev
,
348 sizeof(struct accel_3d_state
));
349 if (indio_dev
== NULL
)
352 platform_set_drvdata(pdev
, indio_dev
);
354 accel_state
= iio_priv(indio_dev
);
355 accel_state
->common_attributes
.hsdev
= hsdev
;
356 accel_state
->common_attributes
.pdev
= pdev
;
358 if (hsdev
->usage
== HID_USAGE_SENSOR_ACCEL_3D
) {
360 channel_spec
= accel_3d_channels
;
361 channel_size
= sizeof(accel_3d_channels
);
362 indio_dev
->num_channels
= ARRAY_SIZE(accel_3d_channels
);
365 channel_spec
= gravity_channels
;
366 channel_size
= sizeof(gravity_channels
);
367 indio_dev
->num_channels
= ARRAY_SIZE(gravity_channels
);
369 ret
= hid_sensor_parse_common_attributes(hsdev
, hsdev
->usage
,
370 &accel_state
->common_attributes
);
372 dev_err(&pdev
->dev
, "failed to setup common attributes\n");
375 indio_dev
->channels
= kmemdup(channel_spec
, channel_size
, GFP_KERNEL
);
377 if (!indio_dev
->channels
) {
378 dev_err(&pdev
->dev
, "failed to duplicate channels\n");
381 ret
= accel_3d_parse_report(pdev
, hsdev
,
382 (struct iio_chan_spec
*)indio_dev
->channels
,
383 hsdev
->usage
, accel_state
);
385 dev_err(&pdev
->dev
, "failed to setup attributes\n");
386 goto error_free_dev_mem
;
389 indio_dev
->dev
.parent
= &pdev
->dev
;
390 indio_dev
->info
= &accel_3d_info
;
391 indio_dev
->name
= name
;
392 indio_dev
->modes
= INDIO_DIRECT_MODE
;
394 ret
= iio_triggered_buffer_setup(indio_dev
, &iio_pollfunc_store_time
,
397 dev_err(&pdev
->dev
, "failed to initialize trigger buffer\n");
398 goto error_free_dev_mem
;
400 atomic_set(&accel_state
->common_attributes
.data_ready
, 0);
401 ret
= hid_sensor_setup_trigger(indio_dev
, name
,
402 &accel_state
->common_attributes
);
404 dev_err(&pdev
->dev
, "trigger setup failed\n");
405 goto error_unreg_buffer_funcs
;
408 ret
= iio_device_register(indio_dev
);
410 dev_err(&pdev
->dev
, "device register failed\n");
411 goto error_remove_trigger
;
414 accel_state
->callbacks
.send_event
= accel_3d_proc_event
;
415 accel_state
->callbacks
.capture_sample
= accel_3d_capture_sample
;
416 accel_state
->callbacks
.pdev
= pdev
;
417 ret
= sensor_hub_register_callback(hsdev
, hsdev
->usage
,
418 &accel_state
->callbacks
);
420 dev_err(&pdev
->dev
, "callback reg failed\n");
421 goto error_iio_unreg
;
427 iio_device_unregister(indio_dev
);
428 error_remove_trigger
:
429 hid_sensor_remove_trigger(&accel_state
->common_attributes
);
430 error_unreg_buffer_funcs
:
431 iio_triggered_buffer_cleanup(indio_dev
);
433 kfree(indio_dev
->channels
);
437 /* Function to deinitialize the processing for usage id */
438 static int hid_accel_3d_remove(struct platform_device
*pdev
)
440 struct hid_sensor_hub_device
*hsdev
= pdev
->dev
.platform_data
;
441 struct iio_dev
*indio_dev
= platform_get_drvdata(pdev
);
442 struct accel_3d_state
*accel_state
= iio_priv(indio_dev
);
444 sensor_hub_remove_callback(hsdev
, hsdev
->usage
);
445 iio_device_unregister(indio_dev
);
446 hid_sensor_remove_trigger(&accel_state
->common_attributes
);
447 iio_triggered_buffer_cleanup(indio_dev
);
448 kfree(indio_dev
->channels
);
453 static const struct platform_device_id hid_accel_3d_ids
[] = {
455 /* Format: HID-SENSOR-usage_id_in_hex_lowercase */
456 .name
= "HID-SENSOR-200073",
458 { /* gravity sensor */
459 .name
= "HID-SENSOR-20007b",
463 MODULE_DEVICE_TABLE(platform
, hid_accel_3d_ids
);
465 static struct platform_driver hid_accel_3d_platform_driver
= {
466 .id_table
= hid_accel_3d_ids
,
468 .name
= KBUILD_MODNAME
,
469 .pm
= &hid_sensor_pm_ops
,
471 .probe
= hid_accel_3d_probe
,
472 .remove
= hid_accel_3d_remove
,
474 module_platform_driver(hid_accel_3d_platform_driver
);
476 MODULE_DESCRIPTION("HID Sensor Accel 3D");
477 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
478 MODULE_LICENSE("GPL");