3 * Copyright (c) 2013, 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.
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 incl_3d_channel
{
41 struct incl_3d_state
{
42 struct hid_sensor_hub_callbacks callbacks
;
43 struct hid_sensor_common common_attributes
;
44 struct hid_sensor_hub_attribute_info incl
[INCLI_3D_CHANNEL_MAX
];
45 u32 incl_val
[INCLI_3D_CHANNEL_MAX
];
52 static const u32 incl_3d_addresses
[INCLI_3D_CHANNEL_MAX
] = {
53 HID_USAGE_SENSOR_ORIENT_TILT_X
,
54 HID_USAGE_SENSOR_ORIENT_TILT_Y
,
55 HID_USAGE_SENSOR_ORIENT_TILT_Z
58 /* Channel definitions */
59 static const struct iio_chan_spec incl_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 incl_3d_adjust_channel_bit_mask(struct iio_chan_spec
*chan
,
97 chan
->scan_type
.sign
= 's';
98 /* Real storage bits will change based on the report desc. */
99 chan
->scan_type
.realbits
= size
* 8;
100 /* Maximum size of a sample to capture is u32 */
101 chan
->scan_type
.storagebits
= sizeof(u32
) * 8;
104 /* Channel read_raw handler */
105 static int incl_3d_read_raw(struct iio_dev
*indio_dev
,
106 struct iio_chan_spec
const *chan
,
110 struct incl_3d_state
*incl_state
= iio_priv(indio_dev
);
118 case IIO_CHAN_INFO_RAW
:
119 hid_sensor_power_state(&incl_state
->common_attributes
, true);
121 incl_state
->incl
[chan
->scan_index
].report_id
;
122 address
= incl_3d_addresses
[chan
->scan_index
];
124 *val
= sensor_hub_input_attr_get_raw_value(
125 incl_state
->common_attributes
.hsdev
,
126 HID_USAGE_SENSOR_INCLINOMETER_3D
, address
,
130 hid_sensor_power_state(&incl_state
->common_attributes
,
134 hid_sensor_power_state(&incl_state
->common_attributes
, false);
135 ret_type
= IIO_VAL_INT
;
137 case IIO_CHAN_INFO_SCALE
:
138 *val
= incl_state
->scale_pre_decml
;
139 *val2
= incl_state
->scale_post_decml
;
140 ret_type
= incl_state
->scale_precision
;
142 case IIO_CHAN_INFO_OFFSET
:
143 *val
= incl_state
->value_offset
;
144 ret_type
= IIO_VAL_INT
;
146 case IIO_CHAN_INFO_SAMP_FREQ
:
147 ret_type
= hid_sensor_read_samp_freq_value(
148 &incl_state
->common_attributes
, val
, val2
);
150 case IIO_CHAN_INFO_HYSTERESIS
:
151 ret_type
= hid_sensor_read_raw_hyst_value(
152 &incl_state
->common_attributes
, val
, val2
);
162 /* Channel write_raw handler */
163 static int incl_3d_write_raw(struct iio_dev
*indio_dev
,
164 struct iio_chan_spec
const *chan
,
169 struct incl_3d_state
*incl_state
= iio_priv(indio_dev
);
173 case IIO_CHAN_INFO_SAMP_FREQ
:
174 ret
= hid_sensor_write_samp_freq_value(
175 &incl_state
->common_attributes
, val
, val2
);
177 case IIO_CHAN_INFO_HYSTERESIS
:
178 ret
= hid_sensor_write_raw_hyst_value(
179 &incl_state
->common_attributes
, val
, val2
);
188 static const struct iio_info incl_3d_info
= {
189 .read_raw
= &incl_3d_read_raw
,
190 .write_raw
= &incl_3d_write_raw
,
193 /* Function to push data to buffer */
194 static void hid_sensor_push_data(struct iio_dev
*indio_dev
, u8
*data
, int len
)
196 dev_dbg(&indio_dev
->dev
, "hid_sensor_push_data\n");
197 iio_push_to_buffers(indio_dev
, (u8
*)data
);
200 /* Callback handler to send event after all samples are received and captured */
201 static int incl_3d_proc_event(struct hid_sensor_hub_device
*hsdev
,
205 struct iio_dev
*indio_dev
= platform_get_drvdata(priv
);
206 struct incl_3d_state
*incl_state
= iio_priv(indio_dev
);
208 dev_dbg(&indio_dev
->dev
, "incl_3d_proc_event\n");
209 if (atomic_read(&incl_state
->common_attributes
.data_ready
))
210 hid_sensor_push_data(indio_dev
,
211 (u8
*)incl_state
->incl_val
,
212 sizeof(incl_state
->incl_val
));
217 /* Capture samples in local storage */
218 static int incl_3d_capture_sample(struct hid_sensor_hub_device
*hsdev
,
220 size_t raw_len
, char *raw_data
,
223 struct iio_dev
*indio_dev
= platform_get_drvdata(priv
);
224 struct incl_3d_state
*incl_state
= iio_priv(indio_dev
);
228 case HID_USAGE_SENSOR_ORIENT_TILT_X
:
229 incl_state
->incl_val
[CHANNEL_SCAN_INDEX_X
] = *(u32
*)raw_data
;
231 case HID_USAGE_SENSOR_ORIENT_TILT_Y
:
232 incl_state
->incl_val
[CHANNEL_SCAN_INDEX_Y
] = *(u32
*)raw_data
;
234 case HID_USAGE_SENSOR_ORIENT_TILT_Z
:
235 incl_state
->incl_val
[CHANNEL_SCAN_INDEX_Z
] = *(u32
*)raw_data
;
245 /* Parse report which is specific to an usage id*/
246 static int incl_3d_parse_report(struct platform_device
*pdev
,
247 struct hid_sensor_hub_device
*hsdev
,
248 struct iio_chan_spec
*channels
,
250 struct incl_3d_state
*st
)
254 ret
= sensor_hub_input_get_attribute_info(hsdev
,
257 HID_USAGE_SENSOR_ORIENT_TILT_X
,
258 &st
->incl
[CHANNEL_SCAN_INDEX_X
]);
261 incl_3d_adjust_channel_bit_mask(&channels
[CHANNEL_SCAN_INDEX_X
],
262 st
->incl
[CHANNEL_SCAN_INDEX_X
].size
);
264 ret
= sensor_hub_input_get_attribute_info(hsdev
,
267 HID_USAGE_SENSOR_ORIENT_TILT_Y
,
268 &st
->incl
[CHANNEL_SCAN_INDEX_Y
]);
271 incl_3d_adjust_channel_bit_mask(&channels
[CHANNEL_SCAN_INDEX_Y
],
272 st
->incl
[CHANNEL_SCAN_INDEX_Y
].size
);
274 ret
= sensor_hub_input_get_attribute_info(hsdev
,
277 HID_USAGE_SENSOR_ORIENT_TILT_Z
,
278 &st
->incl
[CHANNEL_SCAN_INDEX_Z
]);
281 incl_3d_adjust_channel_bit_mask(&channels
[CHANNEL_SCAN_INDEX_Z
],
282 st
->incl
[CHANNEL_SCAN_INDEX_Z
].size
);
284 dev_dbg(&pdev
->dev
, "incl_3d %x:%x, %x:%x, %x:%x\n",
286 st
->incl
[0].report_id
,
287 st
->incl
[1].index
, st
->incl
[1].report_id
,
288 st
->incl
[2].index
, st
->incl
[2].report_id
);
290 st
->scale_precision
= hid_sensor_format_scale(
291 HID_USAGE_SENSOR_INCLINOMETER_3D
,
292 &st
->incl
[CHANNEL_SCAN_INDEX_X
],
293 &st
->scale_pre_decml
, &st
->scale_post_decml
);
295 /* Set Sensitivity field ids, when there is no individual modifier */
296 if (st
->common_attributes
.sensitivity
.index
< 0) {
297 sensor_hub_input_get_attribute_info(hsdev
,
298 HID_FEATURE_REPORT
, usage_id
,
299 HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS
|
300 HID_USAGE_SENSOR_DATA_ORIENTATION
,
301 &st
->common_attributes
.sensitivity
);
302 dev_dbg(&pdev
->dev
, "Sensitivity index:report %d:%d\n",
303 st
->common_attributes
.sensitivity
.index
,
304 st
->common_attributes
.sensitivity
.report_id
);
309 /* Function to initialize the processing for usage id */
310 static int hid_incl_3d_probe(struct platform_device
*pdev
)
313 static char *name
= "incli_3d";
314 struct iio_dev
*indio_dev
;
315 struct incl_3d_state
*incl_state
;
316 struct hid_sensor_hub_device
*hsdev
= pdev
->dev
.platform_data
;
318 indio_dev
= devm_iio_device_alloc(&pdev
->dev
,
319 sizeof(struct incl_3d_state
));
320 if (indio_dev
== NULL
)
323 platform_set_drvdata(pdev
, indio_dev
);
325 incl_state
= iio_priv(indio_dev
);
326 incl_state
->common_attributes
.hsdev
= hsdev
;
327 incl_state
->common_attributes
.pdev
= pdev
;
329 ret
= hid_sensor_parse_common_attributes(hsdev
,
330 HID_USAGE_SENSOR_INCLINOMETER_3D
,
331 &incl_state
->common_attributes
);
333 dev_err(&pdev
->dev
, "failed to setup common attributes\n");
337 indio_dev
->channels
= kmemdup(incl_3d_channels
,
338 sizeof(incl_3d_channels
), GFP_KERNEL
);
339 if (!indio_dev
->channels
) {
340 dev_err(&pdev
->dev
, "failed to duplicate channels\n");
344 ret
= incl_3d_parse_report(pdev
, hsdev
,
345 (struct iio_chan_spec
*)indio_dev
->channels
,
346 HID_USAGE_SENSOR_INCLINOMETER_3D
,
349 dev_err(&pdev
->dev
, "failed to setup attributes\n");
350 goto error_free_dev_mem
;
353 indio_dev
->num_channels
= ARRAY_SIZE(incl_3d_channels
);
354 indio_dev
->dev
.parent
= &pdev
->dev
;
355 indio_dev
->info
= &incl_3d_info
;
356 indio_dev
->name
= name
;
357 indio_dev
->modes
= INDIO_DIRECT_MODE
;
359 ret
= iio_triggered_buffer_setup(indio_dev
, &iio_pollfunc_store_time
,
362 dev_err(&pdev
->dev
, "failed to initialize trigger buffer\n");
363 goto error_free_dev_mem
;
365 atomic_set(&incl_state
->common_attributes
.data_ready
, 0);
366 ret
= hid_sensor_setup_trigger(indio_dev
, name
,
367 &incl_state
->common_attributes
);
369 dev_err(&pdev
->dev
, "trigger setup failed\n");
370 goto error_unreg_buffer_funcs
;
373 ret
= iio_device_register(indio_dev
);
375 dev_err(&pdev
->dev
, "device register failed\n");
376 goto error_remove_trigger
;
379 incl_state
->callbacks
.send_event
= incl_3d_proc_event
;
380 incl_state
->callbacks
.capture_sample
= incl_3d_capture_sample
;
381 incl_state
->callbacks
.pdev
= pdev
;
382 ret
= sensor_hub_register_callback(hsdev
,
383 HID_USAGE_SENSOR_INCLINOMETER_3D
,
384 &incl_state
->callbacks
);
386 dev_err(&pdev
->dev
, "callback reg failed\n");
387 goto error_iio_unreg
;
393 iio_device_unregister(indio_dev
);
394 error_remove_trigger
:
395 hid_sensor_remove_trigger(&incl_state
->common_attributes
);
396 error_unreg_buffer_funcs
:
397 iio_triggered_buffer_cleanup(indio_dev
);
399 kfree(indio_dev
->channels
);
403 /* Function to deinitialize the processing for usage id */
404 static int hid_incl_3d_remove(struct platform_device
*pdev
)
406 struct hid_sensor_hub_device
*hsdev
= pdev
->dev
.platform_data
;
407 struct iio_dev
*indio_dev
= platform_get_drvdata(pdev
);
408 struct incl_3d_state
*incl_state
= iio_priv(indio_dev
);
410 sensor_hub_remove_callback(hsdev
, HID_USAGE_SENSOR_INCLINOMETER_3D
);
411 iio_device_unregister(indio_dev
);
412 hid_sensor_remove_trigger(&incl_state
->common_attributes
);
413 iio_triggered_buffer_cleanup(indio_dev
);
414 kfree(indio_dev
->channels
);
419 static const struct platform_device_id hid_incl_3d_ids
[] = {
421 /* Format: HID-SENSOR-usage_id_in_hex_lowercase */
422 .name
= "HID-SENSOR-200086",
426 MODULE_DEVICE_TABLE(platform
, hid_incl_3d_ids
);
428 static struct platform_driver hid_incl_3d_platform_driver
= {
429 .id_table
= hid_incl_3d_ids
,
431 .name
= KBUILD_MODNAME
,
432 .pm
= &hid_sensor_pm_ops
,
434 .probe
= hid_incl_3d_probe
,
435 .remove
= hid_incl_3d_remove
,
437 module_platform_driver(hid_incl_3d_platform_driver
);
439 MODULE_DESCRIPTION("HID Sensor Inclinometer 3D");
440 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
441 MODULE_LICENSE("GPL");