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 accel_3d_channel
{
23 #define CHANNEL_SCAN_INDEX_TIMESTAMP ACCEL_3D_CHANNEL_MAX
24 struct accel_3d_state
{
25 struct hid_sensor_hub_callbacks callbacks
;
26 struct hid_sensor_common common_attributes
;
27 struct hid_sensor_hub_attribute_info accel
[ACCEL_3D_CHANNEL_MAX
];
28 /* Ensure timestamp is naturally aligned */
31 aligned_s64 timestamp
;
40 static const u32 accel_3d_addresses
[ACCEL_3D_CHANNEL_MAX
] = {
41 HID_USAGE_SENSOR_ACCEL_X_AXIS
,
42 HID_USAGE_SENSOR_ACCEL_Y_AXIS
,
43 HID_USAGE_SENSOR_ACCEL_Z_AXIS
46 static const u32 accel_3d_sensitivity_addresses
[] = {
47 HID_USAGE_SENSOR_DATA_ACCELERATION
,
50 /* Channel definitions */
51 static const struct iio_chan_spec accel_3d_channels
[] = {
55 .channel2
= IIO_MOD_X
,
56 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
57 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_OFFSET
) |
58 BIT(IIO_CHAN_INFO_SCALE
) |
59 BIT(IIO_CHAN_INFO_SAMP_FREQ
) |
60 BIT(IIO_CHAN_INFO_HYSTERESIS
),
61 .scan_index
= CHANNEL_SCAN_INDEX_X
,
65 .channel2
= IIO_MOD_Y
,
66 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
67 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_OFFSET
) |
68 BIT(IIO_CHAN_INFO_SCALE
) |
69 BIT(IIO_CHAN_INFO_SAMP_FREQ
) |
70 BIT(IIO_CHAN_INFO_HYSTERESIS
),
71 .scan_index
= CHANNEL_SCAN_INDEX_Y
,
75 .channel2
= IIO_MOD_Z
,
76 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
77 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_OFFSET
) |
78 BIT(IIO_CHAN_INFO_SCALE
) |
79 BIT(IIO_CHAN_INFO_SAMP_FREQ
) |
80 BIT(IIO_CHAN_INFO_HYSTERESIS
),
81 .scan_index
= CHANNEL_SCAN_INDEX_Z
,
83 IIO_CHAN_SOFT_TIMESTAMP(CHANNEL_SCAN_INDEX_TIMESTAMP
)
86 /* Channel definitions */
87 static const struct iio_chan_spec gravity_channels
[] = {
91 .channel2
= IIO_MOD_X
,
92 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
93 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_OFFSET
) |
94 BIT(IIO_CHAN_INFO_SCALE
) |
95 BIT(IIO_CHAN_INFO_SAMP_FREQ
) |
96 BIT(IIO_CHAN_INFO_HYSTERESIS
),
97 .scan_index
= CHANNEL_SCAN_INDEX_X
,
101 .channel2
= IIO_MOD_Y
,
102 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
103 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_OFFSET
) |
104 BIT(IIO_CHAN_INFO_SCALE
) |
105 BIT(IIO_CHAN_INFO_SAMP_FREQ
) |
106 BIT(IIO_CHAN_INFO_HYSTERESIS
),
107 .scan_index
= CHANNEL_SCAN_INDEX_Y
,
111 .channel2
= IIO_MOD_Z
,
112 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
113 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_OFFSET
) |
114 BIT(IIO_CHAN_INFO_SCALE
) |
115 BIT(IIO_CHAN_INFO_SAMP_FREQ
) |
116 BIT(IIO_CHAN_INFO_HYSTERESIS
),
117 .scan_index
= CHANNEL_SCAN_INDEX_Z
,
119 IIO_CHAN_SOFT_TIMESTAMP(CHANNEL_SCAN_INDEX_TIMESTAMP
),
122 /* Adjust channel real bits based on report descriptor */
123 static void accel_3d_adjust_channel_bit_mask(struct iio_chan_spec
*channels
,
124 int channel
, int size
)
126 channels
[channel
].scan_type
.sign
= 's';
127 /* Real storage bits will change based on the report desc. */
128 channels
[channel
].scan_type
.realbits
= size
* 8;
129 /* Maximum size of a sample to capture is u32 */
130 channels
[channel
].scan_type
.storagebits
= sizeof(u32
) * 8;
133 /* Channel read_raw handler */
134 static int accel_3d_read_raw(struct iio_dev
*indio_dev
,
135 struct iio_chan_spec
const *chan
,
139 struct accel_3d_state
*accel_state
= iio_priv(indio_dev
);
144 struct hid_sensor_hub_device
*hsdev
=
145 accel_state
->common_attributes
.hsdev
;
150 case IIO_CHAN_INFO_RAW
:
151 hid_sensor_power_state(&accel_state
->common_attributes
, true);
152 report_id
= accel_state
->accel
[chan
->scan_index
].report_id
;
153 min
= accel_state
->accel
[chan
->scan_index
].logical_minimum
;
154 address
= accel_3d_addresses
[chan
->scan_index
];
156 *val
= sensor_hub_input_attr_get_raw_value(
157 accel_state
->common_attributes
.hsdev
,
158 hsdev
->usage
, address
, report_id
,
163 hid_sensor_power_state(&accel_state
->common_attributes
,
167 hid_sensor_power_state(&accel_state
->common_attributes
, false);
168 ret_type
= IIO_VAL_INT
;
170 case IIO_CHAN_INFO_SCALE
:
171 *val
= accel_state
->scale_pre_decml
;
172 *val2
= accel_state
->scale_post_decml
;
173 ret_type
= accel_state
->scale_precision
;
175 case IIO_CHAN_INFO_OFFSET
:
176 *val
= accel_state
->value_offset
;
177 ret_type
= IIO_VAL_INT
;
179 case IIO_CHAN_INFO_SAMP_FREQ
:
180 ret_type
= hid_sensor_read_samp_freq_value(
181 &accel_state
->common_attributes
, val
, val2
);
183 case IIO_CHAN_INFO_HYSTERESIS
:
184 ret_type
= hid_sensor_read_raw_hyst_value(
185 &accel_state
->common_attributes
, val
, val2
);
195 /* Channel write_raw handler */
196 static int accel_3d_write_raw(struct iio_dev
*indio_dev
,
197 struct iio_chan_spec
const *chan
,
202 struct accel_3d_state
*accel_state
= iio_priv(indio_dev
);
206 case IIO_CHAN_INFO_SAMP_FREQ
:
207 ret
= hid_sensor_write_samp_freq_value(
208 &accel_state
->common_attributes
, val
, val2
);
210 case IIO_CHAN_INFO_HYSTERESIS
:
211 ret
= hid_sensor_write_raw_hyst_value(
212 &accel_state
->common_attributes
, val
, val2
);
221 static const struct iio_info accel_3d_info
= {
222 .read_raw
= &accel_3d_read_raw
,
223 .write_raw
= &accel_3d_write_raw
,
226 /* Function to push data to buffer */
227 static void hid_sensor_push_data(struct iio_dev
*indio_dev
, void *data
,
228 int len
, int64_t timestamp
)
230 dev_dbg(&indio_dev
->dev
, "hid_sensor_push_data\n");
231 iio_push_to_buffers_with_timestamp(indio_dev
, data
, timestamp
);
234 /* Callback handler to send event after all samples are received and captured */
235 static int accel_3d_proc_event(struct hid_sensor_hub_device
*hsdev
,
239 struct iio_dev
*indio_dev
= platform_get_drvdata(priv
);
240 struct accel_3d_state
*accel_state
= iio_priv(indio_dev
);
242 dev_dbg(&indio_dev
->dev
, "accel_3d_proc_event\n");
243 if (atomic_read(&accel_state
->common_attributes
.data_ready
)) {
244 if (!accel_state
->timestamp
)
245 accel_state
->timestamp
= iio_get_time_ns(indio_dev
);
247 hid_sensor_push_data(indio_dev
,
249 sizeof(accel_state
->scan
),
250 accel_state
->timestamp
);
252 accel_state
->timestamp
= 0;
258 /* Capture samples in local storage */
259 static int accel_3d_capture_sample(struct hid_sensor_hub_device
*hsdev
,
261 size_t raw_len
, char *raw_data
,
264 struct iio_dev
*indio_dev
= platform_get_drvdata(priv
);
265 struct accel_3d_state
*accel_state
= iio_priv(indio_dev
);
270 case HID_USAGE_SENSOR_ACCEL_X_AXIS
:
271 case HID_USAGE_SENSOR_ACCEL_Y_AXIS
:
272 case HID_USAGE_SENSOR_ACCEL_Z_AXIS
:
273 offset
= usage_id
- HID_USAGE_SENSOR_ACCEL_X_AXIS
;
274 accel_state
->scan
.accel_val
[CHANNEL_SCAN_INDEX_X
+ offset
] =
278 case HID_USAGE_SENSOR_TIME_TIMESTAMP
:
279 accel_state
->timestamp
=
280 hid_sensor_convert_timestamp(
281 &accel_state
->common_attributes
,
282 *(int64_t *)raw_data
);
292 /* Parse report which is specific to an usage id*/
293 static int accel_3d_parse_report(struct platform_device
*pdev
,
294 struct hid_sensor_hub_device
*hsdev
,
295 struct iio_chan_spec
*channels
,
297 struct accel_3d_state
*st
)
302 for (i
= 0; i
<= CHANNEL_SCAN_INDEX_Z
; ++i
) {
303 ret
= sensor_hub_input_get_attribute_info(hsdev
,
306 HID_USAGE_SENSOR_ACCEL_X_AXIS
+ i
,
307 &st
->accel
[CHANNEL_SCAN_INDEX_X
+ i
]);
310 accel_3d_adjust_channel_bit_mask(channels
,
311 CHANNEL_SCAN_INDEX_X
+ i
,
312 st
->accel
[CHANNEL_SCAN_INDEX_X
+ i
].size
);
314 dev_dbg(&pdev
->dev
, "accel_3d %x:%x, %x:%x, %x:%x\n",
316 st
->accel
[0].report_id
,
317 st
->accel
[1].index
, st
->accel
[1].report_id
,
318 st
->accel
[2].index
, st
->accel
[2].report_id
);
320 st
->scale_precision
= hid_sensor_format_scale(
322 &st
->accel
[CHANNEL_SCAN_INDEX_X
],
323 &st
->scale_pre_decml
, &st
->scale_post_decml
);
328 /* Function to initialize the processing for usage id */
329 static int hid_accel_3d_probe(struct platform_device
*pdev
)
331 struct hid_sensor_hub_device
*hsdev
= dev_get_platdata(&pdev
->dev
);
334 struct iio_dev
*indio_dev
;
335 struct accel_3d_state
*accel_state
;
336 const struct iio_chan_spec
*channel_spec
;
339 indio_dev
= devm_iio_device_alloc(&pdev
->dev
,
340 sizeof(struct accel_3d_state
));
341 if (indio_dev
== NULL
)
344 platform_set_drvdata(pdev
, indio_dev
);
346 accel_state
= iio_priv(indio_dev
);
347 accel_state
->common_attributes
.hsdev
= hsdev
;
348 accel_state
->common_attributes
.pdev
= pdev
;
350 if (hsdev
->usage
== HID_USAGE_SENSOR_ACCEL_3D
) {
352 channel_spec
= accel_3d_channels
;
353 channel_size
= sizeof(accel_3d_channels
);
354 indio_dev
->num_channels
= ARRAY_SIZE(accel_3d_channels
);
357 channel_spec
= gravity_channels
;
358 channel_size
= sizeof(gravity_channels
);
359 indio_dev
->num_channels
= ARRAY_SIZE(gravity_channels
);
361 ret
= hid_sensor_parse_common_attributes(hsdev
,
363 &accel_state
->common_attributes
,
364 accel_3d_sensitivity_addresses
,
365 ARRAY_SIZE(accel_3d_sensitivity_addresses
));
367 dev_err(&pdev
->dev
, "failed to setup common attributes\n");
370 indio_dev
->channels
= devm_kmemdup(&pdev
->dev
, channel_spec
,
371 channel_size
, GFP_KERNEL
);
373 if (!indio_dev
->channels
) {
374 dev_err(&pdev
->dev
, "failed to duplicate channels\n");
377 ret
= accel_3d_parse_report(pdev
, hsdev
,
378 (struct iio_chan_spec
*)indio_dev
->channels
,
379 hsdev
->usage
, accel_state
);
381 dev_err(&pdev
->dev
, "failed to setup attributes\n");
385 indio_dev
->info
= &accel_3d_info
;
386 indio_dev
->name
= name
;
387 indio_dev
->modes
= INDIO_DIRECT_MODE
;
389 atomic_set(&accel_state
->common_attributes
.data_ready
, 0);
391 ret
= hid_sensor_setup_trigger(indio_dev
, name
,
392 &accel_state
->common_attributes
);
394 dev_err(&pdev
->dev
, "trigger setup failed\n");
398 ret
= iio_device_register(indio_dev
);
400 dev_err(&pdev
->dev
, "device register failed\n");
401 goto error_remove_trigger
;
404 accel_state
->callbacks
.send_event
= accel_3d_proc_event
;
405 accel_state
->callbacks
.capture_sample
= accel_3d_capture_sample
;
406 accel_state
->callbacks
.pdev
= pdev
;
407 ret
= sensor_hub_register_callback(hsdev
, hsdev
->usage
,
408 &accel_state
->callbacks
);
410 dev_err(&pdev
->dev
, "callback reg failed\n");
411 goto error_iio_unreg
;
417 iio_device_unregister(indio_dev
);
418 error_remove_trigger
:
419 hid_sensor_remove_trigger(indio_dev
, &accel_state
->common_attributes
);
423 /* Function to deinitialize the processing for usage id */
424 static void hid_accel_3d_remove(struct platform_device
*pdev
)
426 struct hid_sensor_hub_device
*hsdev
= dev_get_platdata(&pdev
->dev
);
427 struct iio_dev
*indio_dev
= platform_get_drvdata(pdev
);
428 struct accel_3d_state
*accel_state
= iio_priv(indio_dev
);
430 sensor_hub_remove_callback(hsdev
, hsdev
->usage
);
431 iio_device_unregister(indio_dev
);
432 hid_sensor_remove_trigger(indio_dev
, &accel_state
->common_attributes
);
435 static const struct platform_device_id hid_accel_3d_ids
[] = {
437 /* Format: HID-SENSOR-usage_id_in_hex_lowercase */
438 .name
= "HID-SENSOR-200073",
440 { /* gravity sensor */
441 .name
= "HID-SENSOR-20007b",
445 MODULE_DEVICE_TABLE(platform
, hid_accel_3d_ids
);
447 static struct platform_driver hid_accel_3d_platform_driver
= {
448 .id_table
= hid_accel_3d_ids
,
450 .name
= KBUILD_MODNAME
,
451 .pm
= &hid_sensor_pm_ops
,
453 .probe
= hid_accel_3d_probe
,
454 .remove
= hid_accel_3d_remove
,
456 module_platform_driver(hid_accel_3d_platform_driver
);
458 MODULE_DESCRIPTION("HID Sensor Accel 3D");
459 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
460 MODULE_LICENSE("GPL");
461 MODULE_IMPORT_NS("IIO_HID");