1 // SPDX-License-Identifier: GPL-2.0-only
4 * Copyright (c) 2013, Intel Corporation.
7 #include <linux/device.h>
8 #include <linux/platform_device.h>
9 #include <linux/module.h>
10 #include <linux/interrupt.h>
11 #include <linux/irq.h>
12 #include <linux/slab.h>
13 #include <linux/delay.h>
14 #include <linux/hid-sensor-hub.h>
15 #include <linux/iio/iio.h>
16 #include <linux/iio/sysfs.h>
17 #include <linux/iio/buffer.h>
18 #include <linux/iio/trigger_consumer.h>
19 #include <linux/iio/triggered_buffer.h>
20 #include "../common/hid-sensors/hid-sensor-trigger.h"
22 enum incl_3d_channel
{
29 struct incl_3d_state
{
30 struct hid_sensor_hub_callbacks callbacks
;
31 struct hid_sensor_common common_attributes
;
32 struct hid_sensor_hub_attribute_info incl
[INCLI_3D_CHANNEL_MAX
];
33 u32 incl_val
[INCLI_3D_CHANNEL_MAX
];
40 static const u32 incl_3d_addresses
[INCLI_3D_CHANNEL_MAX
] = {
41 HID_USAGE_SENSOR_ORIENT_TILT_X
,
42 HID_USAGE_SENSOR_ORIENT_TILT_Y
,
43 HID_USAGE_SENSOR_ORIENT_TILT_Z
46 /* Channel definitions */
47 static const struct iio_chan_spec incl_3d_channels
[] = {
51 .channel2
= IIO_MOD_X
,
52 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
53 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_OFFSET
) |
54 BIT(IIO_CHAN_INFO_SCALE
) |
55 BIT(IIO_CHAN_INFO_SAMP_FREQ
) |
56 BIT(IIO_CHAN_INFO_HYSTERESIS
),
57 .scan_index
= CHANNEL_SCAN_INDEX_X
,
61 .channel2
= IIO_MOD_Y
,
62 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
63 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_OFFSET
) |
64 BIT(IIO_CHAN_INFO_SCALE
) |
65 BIT(IIO_CHAN_INFO_SAMP_FREQ
) |
66 BIT(IIO_CHAN_INFO_HYSTERESIS
),
67 .scan_index
= CHANNEL_SCAN_INDEX_Y
,
71 .channel2
= IIO_MOD_Z
,
72 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
73 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_OFFSET
) |
74 BIT(IIO_CHAN_INFO_SCALE
) |
75 BIT(IIO_CHAN_INFO_SAMP_FREQ
) |
76 BIT(IIO_CHAN_INFO_HYSTERESIS
),
77 .scan_index
= CHANNEL_SCAN_INDEX_Z
,
81 /* Adjust channel real bits based on report descriptor */
82 static void incl_3d_adjust_channel_bit_mask(struct iio_chan_spec
*chan
,
85 chan
->scan_type
.sign
= 's';
86 /* Real storage bits will change based on the report desc. */
87 chan
->scan_type
.realbits
= size
* 8;
88 /* Maximum size of a sample to capture is u32 */
89 chan
->scan_type
.storagebits
= sizeof(u32
) * 8;
92 /* Channel read_raw handler */
93 static int incl_3d_read_raw(struct iio_dev
*indio_dev
,
94 struct iio_chan_spec
const *chan
,
98 struct incl_3d_state
*incl_state
= iio_priv(indio_dev
);
107 case IIO_CHAN_INFO_RAW
:
108 hid_sensor_power_state(&incl_state
->common_attributes
, true);
109 report_id
= incl_state
->incl
[chan
->scan_index
].report_id
;
110 min
= incl_state
->incl
[chan
->scan_index
].logical_minimum
;
111 address
= incl_3d_addresses
[chan
->scan_index
];
113 *val
= sensor_hub_input_attr_get_raw_value(
114 incl_state
->common_attributes
.hsdev
,
115 HID_USAGE_SENSOR_INCLINOMETER_3D
, address
,
120 hid_sensor_power_state(&incl_state
->common_attributes
,
124 hid_sensor_power_state(&incl_state
->common_attributes
, false);
125 ret_type
= IIO_VAL_INT
;
127 case IIO_CHAN_INFO_SCALE
:
128 *val
= incl_state
->scale_pre_decml
;
129 *val2
= incl_state
->scale_post_decml
;
130 ret_type
= incl_state
->scale_precision
;
132 case IIO_CHAN_INFO_OFFSET
:
133 *val
= incl_state
->value_offset
;
134 ret_type
= IIO_VAL_INT
;
136 case IIO_CHAN_INFO_SAMP_FREQ
:
137 ret_type
= hid_sensor_read_samp_freq_value(
138 &incl_state
->common_attributes
, val
, val2
);
140 case IIO_CHAN_INFO_HYSTERESIS
:
141 ret_type
= hid_sensor_read_raw_hyst_value(
142 &incl_state
->common_attributes
, val
, val2
);
152 /* Channel write_raw handler */
153 static int incl_3d_write_raw(struct iio_dev
*indio_dev
,
154 struct iio_chan_spec
const *chan
,
159 struct incl_3d_state
*incl_state
= iio_priv(indio_dev
);
163 case IIO_CHAN_INFO_SAMP_FREQ
:
164 ret
= hid_sensor_write_samp_freq_value(
165 &incl_state
->common_attributes
, val
, val2
);
167 case IIO_CHAN_INFO_HYSTERESIS
:
168 ret
= hid_sensor_write_raw_hyst_value(
169 &incl_state
->common_attributes
, val
, val2
);
178 static const struct iio_info incl_3d_info
= {
179 .read_raw
= &incl_3d_read_raw
,
180 .write_raw
= &incl_3d_write_raw
,
183 /* Function to push data to buffer */
184 static void hid_sensor_push_data(struct iio_dev
*indio_dev
, u8
*data
, int len
)
186 dev_dbg(&indio_dev
->dev
, "hid_sensor_push_data\n");
187 iio_push_to_buffers(indio_dev
, (u8
*)data
);
190 /* Callback handler to send event after all samples are received and captured */
191 static int incl_3d_proc_event(struct hid_sensor_hub_device
*hsdev
,
195 struct iio_dev
*indio_dev
= platform_get_drvdata(priv
);
196 struct incl_3d_state
*incl_state
= iio_priv(indio_dev
);
198 dev_dbg(&indio_dev
->dev
, "incl_3d_proc_event\n");
199 if (atomic_read(&incl_state
->common_attributes
.data_ready
))
200 hid_sensor_push_data(indio_dev
,
201 (u8
*)incl_state
->incl_val
,
202 sizeof(incl_state
->incl_val
));
207 /* Capture samples in local storage */
208 static int incl_3d_capture_sample(struct hid_sensor_hub_device
*hsdev
,
210 size_t raw_len
, char *raw_data
,
213 struct iio_dev
*indio_dev
= platform_get_drvdata(priv
);
214 struct incl_3d_state
*incl_state
= iio_priv(indio_dev
);
218 case HID_USAGE_SENSOR_ORIENT_TILT_X
:
219 incl_state
->incl_val
[CHANNEL_SCAN_INDEX_X
] = *(u32
*)raw_data
;
221 case HID_USAGE_SENSOR_ORIENT_TILT_Y
:
222 incl_state
->incl_val
[CHANNEL_SCAN_INDEX_Y
] = *(u32
*)raw_data
;
224 case HID_USAGE_SENSOR_ORIENT_TILT_Z
:
225 incl_state
->incl_val
[CHANNEL_SCAN_INDEX_Z
] = *(u32
*)raw_data
;
235 /* Parse report which is specific to an usage id*/
236 static int incl_3d_parse_report(struct platform_device
*pdev
,
237 struct hid_sensor_hub_device
*hsdev
,
238 struct iio_chan_spec
*channels
,
240 struct incl_3d_state
*st
)
244 ret
= sensor_hub_input_get_attribute_info(hsdev
,
247 HID_USAGE_SENSOR_ORIENT_TILT_X
,
248 &st
->incl
[CHANNEL_SCAN_INDEX_X
]);
251 incl_3d_adjust_channel_bit_mask(&channels
[CHANNEL_SCAN_INDEX_X
],
252 st
->incl
[CHANNEL_SCAN_INDEX_X
].size
);
254 ret
= sensor_hub_input_get_attribute_info(hsdev
,
257 HID_USAGE_SENSOR_ORIENT_TILT_Y
,
258 &st
->incl
[CHANNEL_SCAN_INDEX_Y
]);
261 incl_3d_adjust_channel_bit_mask(&channels
[CHANNEL_SCAN_INDEX_Y
],
262 st
->incl
[CHANNEL_SCAN_INDEX_Y
].size
);
264 ret
= sensor_hub_input_get_attribute_info(hsdev
,
267 HID_USAGE_SENSOR_ORIENT_TILT_Z
,
268 &st
->incl
[CHANNEL_SCAN_INDEX_Z
]);
271 incl_3d_adjust_channel_bit_mask(&channels
[CHANNEL_SCAN_INDEX_Z
],
272 st
->incl
[CHANNEL_SCAN_INDEX_Z
].size
);
274 dev_dbg(&pdev
->dev
, "incl_3d %x:%x, %x:%x, %x:%x\n",
276 st
->incl
[0].report_id
,
277 st
->incl
[1].index
, st
->incl
[1].report_id
,
278 st
->incl
[2].index
, st
->incl
[2].report_id
);
280 st
->scale_precision
= hid_sensor_format_scale(
281 HID_USAGE_SENSOR_INCLINOMETER_3D
,
282 &st
->incl
[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_ORIENTATION
,
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_incl_3d_probe(struct platform_device
*pdev
)
303 static char *name
= "incli_3d";
304 struct iio_dev
*indio_dev
;
305 struct incl_3d_state
*incl_state
;
306 struct hid_sensor_hub_device
*hsdev
= pdev
->dev
.platform_data
;
308 indio_dev
= devm_iio_device_alloc(&pdev
->dev
,
309 sizeof(struct incl_3d_state
));
310 if (indio_dev
== NULL
)
313 platform_set_drvdata(pdev
, indio_dev
);
315 incl_state
= iio_priv(indio_dev
);
316 incl_state
->common_attributes
.hsdev
= hsdev
;
317 incl_state
->common_attributes
.pdev
= pdev
;
319 ret
= hid_sensor_parse_common_attributes(hsdev
,
320 HID_USAGE_SENSOR_INCLINOMETER_3D
,
321 &incl_state
->common_attributes
);
323 dev_err(&pdev
->dev
, "failed to setup common attributes\n");
327 indio_dev
->channels
= kmemdup(incl_3d_channels
,
328 sizeof(incl_3d_channels
), GFP_KERNEL
);
329 if (!indio_dev
->channels
) {
330 dev_err(&pdev
->dev
, "failed to duplicate channels\n");
334 ret
= incl_3d_parse_report(pdev
, hsdev
,
335 (struct iio_chan_spec
*)indio_dev
->channels
,
336 HID_USAGE_SENSOR_INCLINOMETER_3D
,
339 dev_err(&pdev
->dev
, "failed to setup attributes\n");
340 goto error_free_dev_mem
;
343 indio_dev
->num_channels
= ARRAY_SIZE(incl_3d_channels
);
344 indio_dev
->dev
.parent
= &pdev
->dev
;
345 indio_dev
->info
= &incl_3d_info
;
346 indio_dev
->name
= name
;
347 indio_dev
->modes
= INDIO_DIRECT_MODE
;
349 ret
= iio_triggered_buffer_setup(indio_dev
, &iio_pollfunc_store_time
,
352 dev_err(&pdev
->dev
, "failed to initialize trigger buffer\n");
353 goto error_free_dev_mem
;
355 atomic_set(&incl_state
->common_attributes
.data_ready
, 0);
356 ret
= hid_sensor_setup_trigger(indio_dev
, name
,
357 &incl_state
->common_attributes
);
359 dev_err(&pdev
->dev
, "trigger setup failed\n");
360 goto error_unreg_buffer_funcs
;
363 ret
= iio_device_register(indio_dev
);
365 dev_err(&pdev
->dev
, "device register failed\n");
366 goto error_remove_trigger
;
369 incl_state
->callbacks
.send_event
= incl_3d_proc_event
;
370 incl_state
->callbacks
.capture_sample
= incl_3d_capture_sample
;
371 incl_state
->callbacks
.pdev
= pdev
;
372 ret
= sensor_hub_register_callback(hsdev
,
373 HID_USAGE_SENSOR_INCLINOMETER_3D
,
374 &incl_state
->callbacks
);
376 dev_err(&pdev
->dev
, "callback reg failed\n");
377 goto error_iio_unreg
;
383 iio_device_unregister(indio_dev
);
384 error_remove_trigger
:
385 hid_sensor_remove_trigger(&incl_state
->common_attributes
);
386 error_unreg_buffer_funcs
:
387 iio_triggered_buffer_cleanup(indio_dev
);
389 kfree(indio_dev
->channels
);
393 /* Function to deinitialize the processing for usage id */
394 static int hid_incl_3d_remove(struct platform_device
*pdev
)
396 struct hid_sensor_hub_device
*hsdev
= pdev
->dev
.platform_data
;
397 struct iio_dev
*indio_dev
= platform_get_drvdata(pdev
);
398 struct incl_3d_state
*incl_state
= iio_priv(indio_dev
);
400 sensor_hub_remove_callback(hsdev
, HID_USAGE_SENSOR_INCLINOMETER_3D
);
401 iio_device_unregister(indio_dev
);
402 hid_sensor_remove_trigger(&incl_state
->common_attributes
);
403 iio_triggered_buffer_cleanup(indio_dev
);
404 kfree(indio_dev
->channels
);
409 static const struct platform_device_id hid_incl_3d_ids
[] = {
411 /* Format: HID-SENSOR-usage_id_in_hex_lowercase */
412 .name
= "HID-SENSOR-200086",
416 MODULE_DEVICE_TABLE(platform
, hid_incl_3d_ids
);
418 static struct platform_driver hid_incl_3d_platform_driver
= {
419 .id_table
= hid_incl_3d_ids
,
421 .name
= KBUILD_MODNAME
,
422 .pm
= &hid_sensor_pm_ops
,
424 .probe
= hid_incl_3d_probe
,
425 .remove
= hid_incl_3d_remove
,
427 module_platform_driver(hid_incl_3d_platform_driver
);
429 MODULE_DESCRIPTION("HID Sensor Inclinometer 3D");
430 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
431 MODULE_LICENSE("GPL");