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 "../common/hid-sensors/hid-sensor-trigger.h"
20 enum incl_3d_channel
{
27 struct incl_3d_state
{
28 struct hid_sensor_hub_callbacks callbacks
;
29 struct hid_sensor_common common_attributes
;
30 struct hid_sensor_hub_attribute_info incl
[INCLI_3D_CHANNEL_MAX
];
31 u32 incl_val
[INCLI_3D_CHANNEL_MAX
];
38 static const u32 incl_3d_addresses
[INCLI_3D_CHANNEL_MAX
] = {
39 HID_USAGE_SENSOR_ORIENT_TILT_X
,
40 HID_USAGE_SENSOR_ORIENT_TILT_Y
,
41 HID_USAGE_SENSOR_ORIENT_TILT_Z
44 /* Channel definitions */
45 static const struct iio_chan_spec incl_3d_channels
[] = {
49 .channel2
= IIO_MOD_X
,
50 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
51 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_OFFSET
) |
52 BIT(IIO_CHAN_INFO_SCALE
) |
53 BIT(IIO_CHAN_INFO_SAMP_FREQ
) |
54 BIT(IIO_CHAN_INFO_HYSTERESIS
),
55 .scan_index
= CHANNEL_SCAN_INDEX_X
,
59 .channel2
= IIO_MOD_Y
,
60 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
61 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_OFFSET
) |
62 BIT(IIO_CHAN_INFO_SCALE
) |
63 BIT(IIO_CHAN_INFO_SAMP_FREQ
) |
64 BIT(IIO_CHAN_INFO_HYSTERESIS
),
65 .scan_index
= CHANNEL_SCAN_INDEX_Y
,
69 .channel2
= IIO_MOD_Z
,
70 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
71 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_OFFSET
) |
72 BIT(IIO_CHAN_INFO_SCALE
) |
73 BIT(IIO_CHAN_INFO_SAMP_FREQ
) |
74 BIT(IIO_CHAN_INFO_HYSTERESIS
),
75 .scan_index
= CHANNEL_SCAN_INDEX_Z
,
79 /* Adjust channel real bits based on report descriptor */
80 static void incl_3d_adjust_channel_bit_mask(struct iio_chan_spec
*chan
,
83 chan
->scan_type
.sign
= 's';
84 /* Real storage bits will change based on the report desc. */
85 chan
->scan_type
.realbits
= size
* 8;
86 /* Maximum size of a sample to capture is u32 */
87 chan
->scan_type
.storagebits
= sizeof(u32
) * 8;
90 /* Channel read_raw handler */
91 static int incl_3d_read_raw(struct iio_dev
*indio_dev
,
92 struct iio_chan_spec
const *chan
,
96 struct incl_3d_state
*incl_state
= iio_priv(indio_dev
);
105 case IIO_CHAN_INFO_RAW
:
106 hid_sensor_power_state(&incl_state
->common_attributes
, true);
107 report_id
= incl_state
->incl
[chan
->scan_index
].report_id
;
108 min
= incl_state
->incl
[chan
->scan_index
].logical_minimum
;
109 address
= incl_3d_addresses
[chan
->scan_index
];
111 *val
= sensor_hub_input_attr_get_raw_value(
112 incl_state
->common_attributes
.hsdev
,
113 HID_USAGE_SENSOR_INCLINOMETER_3D
, address
,
118 hid_sensor_power_state(&incl_state
->common_attributes
,
122 hid_sensor_power_state(&incl_state
->common_attributes
, false);
123 ret_type
= IIO_VAL_INT
;
125 case IIO_CHAN_INFO_SCALE
:
126 *val
= incl_state
->scale_pre_decml
;
127 *val2
= incl_state
->scale_post_decml
;
128 ret_type
= incl_state
->scale_precision
;
130 case IIO_CHAN_INFO_OFFSET
:
131 *val
= incl_state
->value_offset
;
132 ret_type
= IIO_VAL_INT
;
134 case IIO_CHAN_INFO_SAMP_FREQ
:
135 ret_type
= hid_sensor_read_samp_freq_value(
136 &incl_state
->common_attributes
, val
, val2
);
138 case IIO_CHAN_INFO_HYSTERESIS
:
139 ret_type
= hid_sensor_read_raw_hyst_value(
140 &incl_state
->common_attributes
, val
, val2
);
150 /* Channel write_raw handler */
151 static int incl_3d_write_raw(struct iio_dev
*indio_dev
,
152 struct iio_chan_spec
const *chan
,
157 struct incl_3d_state
*incl_state
= iio_priv(indio_dev
);
161 case IIO_CHAN_INFO_SAMP_FREQ
:
162 ret
= hid_sensor_write_samp_freq_value(
163 &incl_state
->common_attributes
, val
, val2
);
165 case IIO_CHAN_INFO_HYSTERESIS
:
166 ret
= hid_sensor_write_raw_hyst_value(
167 &incl_state
->common_attributes
, val
, val2
);
176 static const struct iio_info incl_3d_info
= {
177 .read_raw
= &incl_3d_read_raw
,
178 .write_raw
= &incl_3d_write_raw
,
181 /* Function to push data to buffer */
182 static void hid_sensor_push_data(struct iio_dev
*indio_dev
, u8
*data
, int len
)
184 dev_dbg(&indio_dev
->dev
, "hid_sensor_push_data\n");
185 iio_push_to_buffers(indio_dev
, (u8
*)data
);
188 /* Callback handler to send event after all samples are received and captured */
189 static int incl_3d_proc_event(struct hid_sensor_hub_device
*hsdev
,
193 struct iio_dev
*indio_dev
= platform_get_drvdata(priv
);
194 struct incl_3d_state
*incl_state
= iio_priv(indio_dev
);
196 dev_dbg(&indio_dev
->dev
, "incl_3d_proc_event\n");
197 if (atomic_read(&incl_state
->common_attributes
.data_ready
))
198 hid_sensor_push_data(indio_dev
,
199 (u8
*)incl_state
->incl_val
,
200 sizeof(incl_state
->incl_val
));
205 /* Capture samples in local storage */
206 static int incl_3d_capture_sample(struct hid_sensor_hub_device
*hsdev
,
208 size_t raw_len
, char *raw_data
,
211 struct iio_dev
*indio_dev
= platform_get_drvdata(priv
);
212 struct incl_3d_state
*incl_state
= iio_priv(indio_dev
);
216 case HID_USAGE_SENSOR_ORIENT_TILT_X
:
217 incl_state
->incl_val
[CHANNEL_SCAN_INDEX_X
] = *(u32
*)raw_data
;
219 case HID_USAGE_SENSOR_ORIENT_TILT_Y
:
220 incl_state
->incl_val
[CHANNEL_SCAN_INDEX_Y
] = *(u32
*)raw_data
;
222 case HID_USAGE_SENSOR_ORIENT_TILT_Z
:
223 incl_state
->incl_val
[CHANNEL_SCAN_INDEX_Z
] = *(u32
*)raw_data
;
233 /* Parse report which is specific to an usage id*/
234 static int incl_3d_parse_report(struct platform_device
*pdev
,
235 struct hid_sensor_hub_device
*hsdev
,
236 struct iio_chan_spec
*channels
,
238 struct incl_3d_state
*st
)
242 ret
= sensor_hub_input_get_attribute_info(hsdev
,
245 HID_USAGE_SENSOR_ORIENT_TILT_X
,
246 &st
->incl
[CHANNEL_SCAN_INDEX_X
]);
249 incl_3d_adjust_channel_bit_mask(&channels
[CHANNEL_SCAN_INDEX_X
],
250 st
->incl
[CHANNEL_SCAN_INDEX_X
].size
);
252 ret
= sensor_hub_input_get_attribute_info(hsdev
,
255 HID_USAGE_SENSOR_ORIENT_TILT_Y
,
256 &st
->incl
[CHANNEL_SCAN_INDEX_Y
]);
259 incl_3d_adjust_channel_bit_mask(&channels
[CHANNEL_SCAN_INDEX_Y
],
260 st
->incl
[CHANNEL_SCAN_INDEX_Y
].size
);
262 ret
= sensor_hub_input_get_attribute_info(hsdev
,
265 HID_USAGE_SENSOR_ORIENT_TILT_Z
,
266 &st
->incl
[CHANNEL_SCAN_INDEX_Z
]);
269 incl_3d_adjust_channel_bit_mask(&channels
[CHANNEL_SCAN_INDEX_Z
],
270 st
->incl
[CHANNEL_SCAN_INDEX_Z
].size
);
272 dev_dbg(&pdev
->dev
, "incl_3d %x:%x, %x:%x, %x:%x\n",
274 st
->incl
[0].report_id
,
275 st
->incl
[1].index
, st
->incl
[1].report_id
,
276 st
->incl
[2].index
, st
->incl
[2].report_id
);
278 st
->scale_precision
= hid_sensor_format_scale(
279 HID_USAGE_SENSOR_INCLINOMETER_3D
,
280 &st
->incl
[CHANNEL_SCAN_INDEX_X
],
281 &st
->scale_pre_decml
, &st
->scale_post_decml
);
283 /* Set Sensitivity field ids, when there is no individual modifier */
284 if (st
->common_attributes
.sensitivity
.index
< 0) {
285 sensor_hub_input_get_attribute_info(hsdev
,
286 HID_FEATURE_REPORT
, usage_id
,
287 HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS
|
288 HID_USAGE_SENSOR_DATA_ORIENTATION
,
289 &st
->common_attributes
.sensitivity
);
290 dev_dbg(&pdev
->dev
, "Sensitivity index:report %d:%d\n",
291 st
->common_attributes
.sensitivity
.index
,
292 st
->common_attributes
.sensitivity
.report_id
);
297 /* Function to initialize the processing for usage id */
298 static int hid_incl_3d_probe(struct platform_device
*pdev
)
301 static char *name
= "incli_3d";
302 struct iio_dev
*indio_dev
;
303 struct incl_3d_state
*incl_state
;
304 struct hid_sensor_hub_device
*hsdev
= pdev
->dev
.platform_data
;
306 indio_dev
= devm_iio_device_alloc(&pdev
->dev
,
307 sizeof(struct incl_3d_state
));
308 if (indio_dev
== NULL
)
311 platform_set_drvdata(pdev
, indio_dev
);
313 incl_state
= iio_priv(indio_dev
);
314 incl_state
->common_attributes
.hsdev
= hsdev
;
315 incl_state
->common_attributes
.pdev
= pdev
;
317 ret
= hid_sensor_parse_common_attributes(hsdev
,
318 HID_USAGE_SENSOR_INCLINOMETER_3D
,
319 &incl_state
->common_attributes
);
321 dev_err(&pdev
->dev
, "failed to setup common attributes\n");
325 indio_dev
->channels
= kmemdup(incl_3d_channels
,
326 sizeof(incl_3d_channels
), GFP_KERNEL
);
327 if (!indio_dev
->channels
) {
328 dev_err(&pdev
->dev
, "failed to duplicate channels\n");
332 ret
= incl_3d_parse_report(pdev
, hsdev
,
333 (struct iio_chan_spec
*)indio_dev
->channels
,
334 HID_USAGE_SENSOR_INCLINOMETER_3D
,
337 dev_err(&pdev
->dev
, "failed to setup attributes\n");
338 goto error_free_dev_mem
;
341 indio_dev
->num_channels
= ARRAY_SIZE(incl_3d_channels
);
342 indio_dev
->info
= &incl_3d_info
;
343 indio_dev
->name
= name
;
344 indio_dev
->modes
= INDIO_DIRECT_MODE
;
346 atomic_set(&incl_state
->common_attributes
.data_ready
, 0);
348 ret
= hid_sensor_setup_trigger(indio_dev
, name
,
349 &incl_state
->common_attributes
);
351 dev_err(&pdev
->dev
, "trigger setup failed\n");
352 goto error_free_dev_mem
;
355 ret
= iio_device_register(indio_dev
);
357 dev_err(&pdev
->dev
, "device register failed\n");
358 goto error_remove_trigger
;
361 incl_state
->callbacks
.send_event
= incl_3d_proc_event
;
362 incl_state
->callbacks
.capture_sample
= incl_3d_capture_sample
;
363 incl_state
->callbacks
.pdev
= pdev
;
364 ret
= sensor_hub_register_callback(hsdev
,
365 HID_USAGE_SENSOR_INCLINOMETER_3D
,
366 &incl_state
->callbacks
);
368 dev_err(&pdev
->dev
, "callback reg failed\n");
369 goto error_iio_unreg
;
375 iio_device_unregister(indio_dev
);
376 error_remove_trigger
:
377 hid_sensor_remove_trigger(indio_dev
, &incl_state
->common_attributes
);
379 kfree(indio_dev
->channels
);
383 /* Function to deinitialize the processing for usage id */
384 static int hid_incl_3d_remove(struct platform_device
*pdev
)
386 struct hid_sensor_hub_device
*hsdev
= pdev
->dev
.platform_data
;
387 struct iio_dev
*indio_dev
= platform_get_drvdata(pdev
);
388 struct incl_3d_state
*incl_state
= iio_priv(indio_dev
);
390 sensor_hub_remove_callback(hsdev
, HID_USAGE_SENSOR_INCLINOMETER_3D
);
391 iio_device_unregister(indio_dev
);
392 hid_sensor_remove_trigger(indio_dev
, &incl_state
->common_attributes
);
393 kfree(indio_dev
->channels
);
398 static const struct platform_device_id hid_incl_3d_ids
[] = {
400 /* Format: HID-SENSOR-usage_id_in_hex_lowercase */
401 .name
= "HID-SENSOR-200086",
405 MODULE_DEVICE_TABLE(platform
, hid_incl_3d_ids
);
407 static struct platform_driver hid_incl_3d_platform_driver
= {
408 .id_table
= hid_incl_3d_ids
,
410 .name
= KBUILD_MODNAME
,
411 .pm
= &hid_sensor_pm_ops
,
413 .probe
= hid_incl_3d_probe
,
414 .remove
= hid_incl_3d_remove
,
416 module_platform_driver(hid_incl_3d_platform_driver
);
418 MODULE_DESCRIPTION("HID Sensor Inclinometer 3D");
419 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
420 MODULE_LICENSE("GPL");