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 accel_3d_channel
{
41 struct accel_3d_state
{
42 struct hid_sensor_hub_callbacks callbacks
;
43 struct hid_sensor_common common_attributes
;
44 struct hid_sensor_hub_attribute_info accel
[ACCEL_3D_CHANNEL_MAX
];
45 /* Reserve for 3 channels + padding + timestamp */
46 u32 accel_val
[ACCEL_3D_CHANNEL_MAX
+ 3];
54 static const u32 accel_3d_addresses
[ACCEL_3D_CHANNEL_MAX
] = {
55 HID_USAGE_SENSOR_ACCEL_X_AXIS
,
56 HID_USAGE_SENSOR_ACCEL_Y_AXIS
,
57 HID_USAGE_SENSOR_ACCEL_Z_AXIS
60 /* Channel definitions */
61 static const struct iio_chan_spec accel_3d_channels
[] = {
65 .channel2
= IIO_MOD_X
,
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_X
,
75 .channel2
= IIO_MOD_Y
,
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_Y
,
85 .channel2
= IIO_MOD_Z
,
86 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
87 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_OFFSET
) |
88 BIT(IIO_CHAN_INFO_SCALE
) |
89 BIT(IIO_CHAN_INFO_SAMP_FREQ
) |
90 BIT(IIO_CHAN_INFO_HYSTERESIS
),
91 .scan_index
= CHANNEL_SCAN_INDEX_Z
,
93 IIO_CHAN_SOFT_TIMESTAMP(3)
96 /* Channel definitions */
97 static const struct iio_chan_spec gravity_channels
[] = {
101 .channel2
= IIO_MOD_X
,
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_X
,
111 .channel2
= IIO_MOD_Y
,
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_Y
,
121 .channel2
= IIO_MOD_Z
,
122 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
123 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_OFFSET
) |
124 BIT(IIO_CHAN_INFO_SCALE
) |
125 BIT(IIO_CHAN_INFO_SAMP_FREQ
) |
126 BIT(IIO_CHAN_INFO_HYSTERESIS
),
127 .scan_index
= CHANNEL_SCAN_INDEX_Z
,
131 /* Adjust channel real bits based on report descriptor */
132 static void accel_3d_adjust_channel_bit_mask(struct iio_chan_spec
*channels
,
133 int channel
, int size
)
135 channels
[channel
].scan_type
.sign
= 's';
136 /* Real storage bits will change based on the report desc. */
137 channels
[channel
].scan_type
.realbits
= size
* 8;
138 /* Maximum size of a sample to capture is u32 */
139 channels
[channel
].scan_type
.storagebits
= sizeof(u32
) * 8;
142 /* Channel read_raw handler */
143 static int accel_3d_read_raw(struct iio_dev
*indio_dev
,
144 struct iio_chan_spec
const *chan
,
148 struct accel_3d_state
*accel_state
= iio_priv(indio_dev
);
153 struct hid_sensor_hub_device
*hsdev
=
154 accel_state
->common_attributes
.hsdev
;
159 case IIO_CHAN_INFO_RAW
:
160 hid_sensor_power_state(&accel_state
->common_attributes
, true);
161 report_id
= accel_state
->accel
[chan
->scan_index
].report_id
;
162 min
= accel_state
->accel
[chan
->scan_index
].logical_minimum
;
163 address
= accel_3d_addresses
[chan
->scan_index
];
165 *val
= sensor_hub_input_attr_get_raw_value(
166 accel_state
->common_attributes
.hsdev
,
167 hsdev
->usage
, address
, report_id
,
172 hid_sensor_power_state(&accel_state
->common_attributes
,
176 hid_sensor_power_state(&accel_state
->common_attributes
, false);
177 ret_type
= IIO_VAL_INT
;
179 case IIO_CHAN_INFO_SCALE
:
180 *val
= accel_state
->scale_pre_decml
;
181 *val2
= accel_state
->scale_post_decml
;
182 ret_type
= accel_state
->scale_precision
;
184 case IIO_CHAN_INFO_OFFSET
:
185 *val
= accel_state
->value_offset
;
186 ret_type
= IIO_VAL_INT
;
188 case IIO_CHAN_INFO_SAMP_FREQ
:
189 ret_type
= hid_sensor_read_samp_freq_value(
190 &accel_state
->common_attributes
, val
, val2
);
192 case IIO_CHAN_INFO_HYSTERESIS
:
193 ret_type
= hid_sensor_read_raw_hyst_value(
194 &accel_state
->common_attributes
, val
, val2
);
204 /* Channel write_raw handler */
205 static int accel_3d_write_raw(struct iio_dev
*indio_dev
,
206 struct iio_chan_spec
const *chan
,
211 struct accel_3d_state
*accel_state
= iio_priv(indio_dev
);
215 case IIO_CHAN_INFO_SAMP_FREQ
:
216 ret
= hid_sensor_write_samp_freq_value(
217 &accel_state
->common_attributes
, val
, val2
);
219 case IIO_CHAN_INFO_HYSTERESIS
:
220 ret
= hid_sensor_write_raw_hyst_value(
221 &accel_state
->common_attributes
, val
, val2
);
230 static const struct iio_info accel_3d_info
= {
231 .read_raw
= &accel_3d_read_raw
,
232 .write_raw
= &accel_3d_write_raw
,
235 /* Function to push data to buffer */
236 static void hid_sensor_push_data(struct iio_dev
*indio_dev
, void *data
,
237 int len
, int64_t timestamp
)
239 dev_dbg(&indio_dev
->dev
, "hid_sensor_push_data\n");
240 iio_push_to_buffers_with_timestamp(indio_dev
, data
, timestamp
);
243 /* Callback handler to send event after all samples are received and captured */
244 static int accel_3d_proc_event(struct hid_sensor_hub_device
*hsdev
,
248 struct iio_dev
*indio_dev
= platform_get_drvdata(priv
);
249 struct accel_3d_state
*accel_state
= iio_priv(indio_dev
);
251 dev_dbg(&indio_dev
->dev
, "accel_3d_proc_event\n");
252 if (atomic_read(&accel_state
->common_attributes
.data_ready
)) {
253 if (!accel_state
->timestamp
)
254 accel_state
->timestamp
= iio_get_time_ns(indio_dev
);
256 hid_sensor_push_data(indio_dev
,
257 accel_state
->accel_val
,
258 sizeof(accel_state
->accel_val
),
259 accel_state
->timestamp
);
261 accel_state
->timestamp
= 0;
267 /* Capture samples in local storage */
268 static int accel_3d_capture_sample(struct hid_sensor_hub_device
*hsdev
,
270 size_t raw_len
, char *raw_data
,
273 struct iio_dev
*indio_dev
= platform_get_drvdata(priv
);
274 struct accel_3d_state
*accel_state
= iio_priv(indio_dev
);
279 case HID_USAGE_SENSOR_ACCEL_X_AXIS
:
280 case HID_USAGE_SENSOR_ACCEL_Y_AXIS
:
281 case HID_USAGE_SENSOR_ACCEL_Z_AXIS
:
282 offset
= usage_id
- HID_USAGE_SENSOR_ACCEL_X_AXIS
;
283 accel_state
->accel_val
[CHANNEL_SCAN_INDEX_X
+ offset
] =
287 case HID_USAGE_SENSOR_TIME_TIMESTAMP
:
288 accel_state
->timestamp
=
289 hid_sensor_convert_timestamp(
290 &accel_state
->common_attributes
,
291 *(int64_t *)raw_data
);
300 /* Parse report which is specific to an usage id*/
301 static int accel_3d_parse_report(struct platform_device
*pdev
,
302 struct hid_sensor_hub_device
*hsdev
,
303 struct iio_chan_spec
*channels
,
305 struct accel_3d_state
*st
)
310 for (i
= 0; i
<= CHANNEL_SCAN_INDEX_Z
; ++i
) {
311 ret
= sensor_hub_input_get_attribute_info(hsdev
,
314 HID_USAGE_SENSOR_ACCEL_X_AXIS
+ i
,
315 &st
->accel
[CHANNEL_SCAN_INDEX_X
+ i
]);
318 accel_3d_adjust_channel_bit_mask(channels
,
319 CHANNEL_SCAN_INDEX_X
+ i
,
320 st
->accel
[CHANNEL_SCAN_INDEX_X
+ i
].size
);
322 dev_dbg(&pdev
->dev
, "accel_3d %x:%x, %x:%x, %x:%x\n",
324 st
->accel
[0].report_id
,
325 st
->accel
[1].index
, st
->accel
[1].report_id
,
326 st
->accel
[2].index
, st
->accel
[2].report_id
);
328 st
->scale_precision
= hid_sensor_format_scale(
330 &st
->accel
[CHANNEL_SCAN_INDEX_X
],
331 &st
->scale_pre_decml
, &st
->scale_post_decml
);
333 /* Set Sensitivity field ids, when there is no individual modifier */
334 if (st
->common_attributes
.sensitivity
.index
< 0) {
335 sensor_hub_input_get_attribute_info(hsdev
,
336 HID_FEATURE_REPORT
, usage_id
,
337 HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS
|
338 HID_USAGE_SENSOR_DATA_ACCELERATION
,
339 &st
->common_attributes
.sensitivity
);
340 dev_dbg(&pdev
->dev
, "Sensitivity index:report %d:%d\n",
341 st
->common_attributes
.sensitivity
.index
,
342 st
->common_attributes
.sensitivity
.report_id
);
348 /* Function to initialize the processing for usage id */
349 static int hid_accel_3d_probe(struct platform_device
*pdev
)
353 struct iio_dev
*indio_dev
;
354 struct accel_3d_state
*accel_state
;
355 const struct iio_chan_spec
*channel_spec
;
358 struct hid_sensor_hub_device
*hsdev
= pdev
->dev
.platform_data
;
360 indio_dev
= devm_iio_device_alloc(&pdev
->dev
,
361 sizeof(struct accel_3d_state
));
362 if (indio_dev
== NULL
)
365 platform_set_drvdata(pdev
, indio_dev
);
367 accel_state
= iio_priv(indio_dev
);
368 accel_state
->common_attributes
.hsdev
= hsdev
;
369 accel_state
->common_attributes
.pdev
= pdev
;
371 if (hsdev
->usage
== HID_USAGE_SENSOR_ACCEL_3D
) {
373 channel_spec
= accel_3d_channels
;
374 channel_size
= sizeof(accel_3d_channels
);
375 indio_dev
->num_channels
= ARRAY_SIZE(accel_3d_channels
);
378 channel_spec
= gravity_channels
;
379 channel_size
= sizeof(gravity_channels
);
380 indio_dev
->num_channels
= ARRAY_SIZE(gravity_channels
);
382 ret
= hid_sensor_parse_common_attributes(hsdev
, hsdev
->usage
,
383 &accel_state
->common_attributes
);
385 dev_err(&pdev
->dev
, "failed to setup common attributes\n");
388 indio_dev
->channels
= kmemdup(channel_spec
, channel_size
, GFP_KERNEL
);
390 if (!indio_dev
->channels
) {
391 dev_err(&pdev
->dev
, "failed to duplicate channels\n");
394 ret
= accel_3d_parse_report(pdev
, hsdev
,
395 (struct iio_chan_spec
*)indio_dev
->channels
,
396 hsdev
->usage
, accel_state
);
398 dev_err(&pdev
->dev
, "failed to setup attributes\n");
399 goto error_free_dev_mem
;
402 indio_dev
->dev
.parent
= &pdev
->dev
;
403 indio_dev
->info
= &accel_3d_info
;
404 indio_dev
->name
= name
;
405 indio_dev
->modes
= INDIO_DIRECT_MODE
;
407 ret
= iio_triggered_buffer_setup(indio_dev
, &iio_pollfunc_store_time
,
410 dev_err(&pdev
->dev
, "failed to initialize trigger buffer\n");
411 goto error_free_dev_mem
;
413 atomic_set(&accel_state
->common_attributes
.data_ready
, 0);
414 ret
= hid_sensor_setup_trigger(indio_dev
, name
,
415 &accel_state
->common_attributes
);
417 dev_err(&pdev
->dev
, "trigger setup failed\n");
418 goto error_unreg_buffer_funcs
;
421 ret
= iio_device_register(indio_dev
);
423 dev_err(&pdev
->dev
, "device register failed\n");
424 goto error_remove_trigger
;
427 accel_state
->callbacks
.send_event
= accel_3d_proc_event
;
428 accel_state
->callbacks
.capture_sample
= accel_3d_capture_sample
;
429 accel_state
->callbacks
.pdev
= pdev
;
430 ret
= sensor_hub_register_callback(hsdev
, hsdev
->usage
,
431 &accel_state
->callbacks
);
433 dev_err(&pdev
->dev
, "callback reg failed\n");
434 goto error_iio_unreg
;
440 iio_device_unregister(indio_dev
);
441 error_remove_trigger
:
442 hid_sensor_remove_trigger(&accel_state
->common_attributes
);
443 error_unreg_buffer_funcs
:
444 iio_triggered_buffer_cleanup(indio_dev
);
446 kfree(indio_dev
->channels
);
450 /* Function to deinitialize the processing for usage id */
451 static int hid_accel_3d_remove(struct platform_device
*pdev
)
453 struct hid_sensor_hub_device
*hsdev
= pdev
->dev
.platform_data
;
454 struct iio_dev
*indio_dev
= platform_get_drvdata(pdev
);
455 struct accel_3d_state
*accel_state
= iio_priv(indio_dev
);
457 sensor_hub_remove_callback(hsdev
, hsdev
->usage
);
458 iio_device_unregister(indio_dev
);
459 hid_sensor_remove_trigger(&accel_state
->common_attributes
);
460 iio_triggered_buffer_cleanup(indio_dev
);
461 kfree(indio_dev
->channels
);
466 static const struct platform_device_id hid_accel_3d_ids
[] = {
468 /* Format: HID-SENSOR-usage_id_in_hex_lowercase */
469 .name
= "HID-SENSOR-200073",
471 { /* gravity sensor */
472 .name
= "HID-SENSOR-20007b",
476 MODULE_DEVICE_TABLE(platform
, hid_accel_3d_ids
);
478 static struct platform_driver hid_accel_3d_platform_driver
= {
479 .id_table
= hid_accel_3d_ids
,
481 .name
= KBUILD_MODNAME
,
482 .pm
= &hid_sensor_pm_ops
,
484 .probe
= hid_accel_3d_probe
,
485 .remove
= hid_accel_3d_remove
,
487 module_platform_driver(hid_accel_3d_platform_driver
);
489 MODULE_DESCRIPTION("HID Sensor Accel 3D");
490 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
491 MODULE_LICENSE("GPL");