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/mod_devicetable.h>
11 #include <linux/slab.h>
12 #include <linux/hid-sensor-hub.h>
13 #include <linux/iio/iio.h>
14 #include <linux/iio/buffer.h>
15 #include "../common/hid-sensors/hid-sensor-trigger.h"
17 enum incl_3d_channel
{
24 #define CHANNEL_SCAN_INDEX_TIMESTAMP INCLI_3D_CHANNEL_MAX
26 struct incl_3d_state
{
27 struct hid_sensor_hub_callbacks callbacks
;
28 struct hid_sensor_common common_attributes
;
29 struct hid_sensor_hub_attribute_info incl
[INCLI_3D_CHANNEL_MAX
];
31 u32 incl_val
[INCLI_3D_CHANNEL_MAX
];
32 aligned_s64 timestamp
;
41 static const u32 incl_3d_addresses
[INCLI_3D_CHANNEL_MAX
] = {
42 HID_USAGE_SENSOR_ORIENT_TILT_X
,
43 HID_USAGE_SENSOR_ORIENT_TILT_Y
,
44 HID_USAGE_SENSOR_ORIENT_TILT_Z
47 static const u32 incl_3d_sensitivity_addresses
[] = {
48 HID_USAGE_SENSOR_DATA_ORIENTATION
,
49 HID_USAGE_SENSOR_ORIENT_TILT
,
52 /* Channel definitions */
53 static const struct iio_chan_spec incl_3d_channels
[] = {
57 .channel2
= IIO_MOD_X
,
58 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
59 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_OFFSET
) |
60 BIT(IIO_CHAN_INFO_SCALE
) |
61 BIT(IIO_CHAN_INFO_SAMP_FREQ
) |
62 BIT(IIO_CHAN_INFO_HYSTERESIS
),
63 .scan_index
= CHANNEL_SCAN_INDEX_X
,
67 .channel2
= IIO_MOD_Y
,
68 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
69 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_OFFSET
) |
70 BIT(IIO_CHAN_INFO_SCALE
) |
71 BIT(IIO_CHAN_INFO_SAMP_FREQ
) |
72 BIT(IIO_CHAN_INFO_HYSTERESIS
),
73 .scan_index
= CHANNEL_SCAN_INDEX_Y
,
77 .channel2
= IIO_MOD_Z
,
78 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
79 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_OFFSET
) |
80 BIT(IIO_CHAN_INFO_SCALE
) |
81 BIT(IIO_CHAN_INFO_SAMP_FREQ
) |
82 BIT(IIO_CHAN_INFO_HYSTERESIS
),
83 .scan_index
= CHANNEL_SCAN_INDEX_Z
,
85 IIO_CHAN_SOFT_TIMESTAMP(CHANNEL_SCAN_INDEX_TIMESTAMP
),
88 /* Adjust channel real bits based on report descriptor */
89 static void incl_3d_adjust_channel_bit_mask(struct iio_chan_spec
*chan
,
92 chan
->scan_type
.sign
= 's';
93 /* Real storage bits will change based on the report desc. */
94 chan
->scan_type
.realbits
= size
* 8;
95 /* Maximum size of a sample to capture is u32 */
96 chan
->scan_type
.storagebits
= sizeof(u32
) * 8;
99 /* Channel read_raw handler */
100 static int incl_3d_read_raw(struct iio_dev
*indio_dev
,
101 struct iio_chan_spec
const *chan
,
105 struct incl_3d_state
*incl_state
= iio_priv(indio_dev
);
114 case IIO_CHAN_INFO_RAW
:
115 hid_sensor_power_state(&incl_state
->common_attributes
, true);
116 report_id
= incl_state
->incl
[chan
->scan_index
].report_id
;
117 min
= incl_state
->incl
[chan
->scan_index
].logical_minimum
;
118 address
= incl_3d_addresses
[chan
->scan_index
];
120 *val
= sensor_hub_input_attr_get_raw_value(
121 incl_state
->common_attributes
.hsdev
,
122 HID_USAGE_SENSOR_INCLINOMETER_3D
, address
,
127 hid_sensor_power_state(&incl_state
->common_attributes
,
131 hid_sensor_power_state(&incl_state
->common_attributes
, false);
132 ret_type
= IIO_VAL_INT
;
134 case IIO_CHAN_INFO_SCALE
:
135 *val
= incl_state
->scale_pre_decml
;
136 *val2
= incl_state
->scale_post_decml
;
137 ret_type
= incl_state
->scale_precision
;
139 case IIO_CHAN_INFO_OFFSET
:
140 *val
= incl_state
->value_offset
;
141 ret_type
= IIO_VAL_INT
;
143 case IIO_CHAN_INFO_SAMP_FREQ
:
144 ret_type
= hid_sensor_read_samp_freq_value(
145 &incl_state
->common_attributes
, val
, val2
);
147 case IIO_CHAN_INFO_HYSTERESIS
:
148 ret_type
= hid_sensor_read_raw_hyst_value(
149 &incl_state
->common_attributes
, val
, val2
);
159 /* Channel write_raw handler */
160 static int incl_3d_write_raw(struct iio_dev
*indio_dev
,
161 struct iio_chan_spec
const *chan
,
166 struct incl_3d_state
*incl_state
= iio_priv(indio_dev
);
170 case IIO_CHAN_INFO_SAMP_FREQ
:
171 ret
= hid_sensor_write_samp_freq_value(
172 &incl_state
->common_attributes
, val
, val2
);
174 case IIO_CHAN_INFO_HYSTERESIS
:
175 ret
= hid_sensor_write_raw_hyst_value(
176 &incl_state
->common_attributes
, val
, val2
);
185 static const struct iio_info incl_3d_info
= {
186 .read_raw
= &incl_3d_read_raw
,
187 .write_raw
= &incl_3d_write_raw
,
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 if (!incl_state
->timestamp
)
201 incl_state
->timestamp
= iio_get_time_ns(indio_dev
);
203 iio_push_to_buffers_with_timestamp(indio_dev
,
205 incl_state
->timestamp
);
207 incl_state
->timestamp
= 0;
213 /* Capture samples in local storage */
214 static int incl_3d_capture_sample(struct hid_sensor_hub_device
*hsdev
,
216 size_t raw_len
, char *raw_data
,
219 struct iio_dev
*indio_dev
= platform_get_drvdata(priv
);
220 struct incl_3d_state
*incl_state
= iio_priv(indio_dev
);
224 case HID_USAGE_SENSOR_ORIENT_TILT_X
:
225 incl_state
->scan
.incl_val
[CHANNEL_SCAN_INDEX_X
] = *(u32
*)raw_data
;
227 case HID_USAGE_SENSOR_ORIENT_TILT_Y
:
228 incl_state
->scan
.incl_val
[CHANNEL_SCAN_INDEX_Y
] = *(u32
*)raw_data
;
230 case HID_USAGE_SENSOR_ORIENT_TILT_Z
:
231 incl_state
->scan
.incl_val
[CHANNEL_SCAN_INDEX_Z
] = *(u32
*)raw_data
;
233 case HID_USAGE_SENSOR_TIME_TIMESTAMP
:
234 incl_state
->timestamp
=
235 hid_sensor_convert_timestamp(&incl_state
->common_attributes
,
246 /* Parse report which is specific to an usage id*/
247 static int incl_3d_parse_report(struct platform_device
*pdev
,
248 struct hid_sensor_hub_device
*hsdev
,
249 struct iio_chan_spec
*channels
,
251 struct incl_3d_state
*st
)
255 ret
= sensor_hub_input_get_attribute_info(hsdev
,
258 HID_USAGE_SENSOR_ORIENT_TILT_X
,
259 &st
->incl
[CHANNEL_SCAN_INDEX_X
]);
262 incl_3d_adjust_channel_bit_mask(&channels
[CHANNEL_SCAN_INDEX_X
],
263 st
->incl
[CHANNEL_SCAN_INDEX_X
].size
);
265 ret
= sensor_hub_input_get_attribute_info(hsdev
,
268 HID_USAGE_SENSOR_ORIENT_TILT_Y
,
269 &st
->incl
[CHANNEL_SCAN_INDEX_Y
]);
272 incl_3d_adjust_channel_bit_mask(&channels
[CHANNEL_SCAN_INDEX_Y
],
273 st
->incl
[CHANNEL_SCAN_INDEX_Y
].size
);
275 ret
= sensor_hub_input_get_attribute_info(hsdev
,
278 HID_USAGE_SENSOR_ORIENT_TILT_Z
,
279 &st
->incl
[CHANNEL_SCAN_INDEX_Z
]);
282 incl_3d_adjust_channel_bit_mask(&channels
[CHANNEL_SCAN_INDEX_Z
],
283 st
->incl
[CHANNEL_SCAN_INDEX_Z
].size
);
285 dev_dbg(&pdev
->dev
, "incl_3d %x:%x, %x:%x, %x:%x\n",
287 st
->incl
[0].report_id
,
288 st
->incl
[1].index
, st
->incl
[1].report_id
,
289 st
->incl
[2].index
, st
->incl
[2].report_id
);
291 st
->scale_precision
= hid_sensor_format_scale(
292 HID_USAGE_SENSOR_INCLINOMETER_3D
,
293 &st
->incl
[CHANNEL_SCAN_INDEX_X
],
294 &st
->scale_pre_decml
, &st
->scale_post_decml
);
299 /* Function to initialize the processing for usage id */
300 static int hid_incl_3d_probe(struct platform_device
*pdev
)
302 struct hid_sensor_hub_device
*hsdev
= dev_get_platdata(&pdev
->dev
);
304 static char *name
= "incli_3d";
305 struct iio_dev
*indio_dev
;
306 struct incl_3d_state
*incl_state
;
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
,
322 incl_3d_sensitivity_addresses
,
323 ARRAY_SIZE(incl_3d_sensitivity_addresses
));
325 dev_err(&pdev
->dev
, "failed to setup common attributes\n");
329 indio_dev
->channels
= devm_kmemdup(&pdev
->dev
, incl_3d_channels
,
330 sizeof(incl_3d_channels
), GFP_KERNEL
);
331 if (!indio_dev
->channels
) {
332 dev_err(&pdev
->dev
, "failed to duplicate channels\n");
336 ret
= incl_3d_parse_report(pdev
, hsdev
,
337 (struct iio_chan_spec
*)indio_dev
->channels
,
338 HID_USAGE_SENSOR_INCLINOMETER_3D
,
341 dev_err(&pdev
->dev
, "failed to setup attributes\n");
345 indio_dev
->num_channels
= ARRAY_SIZE(incl_3d_channels
);
346 indio_dev
->info
= &incl_3d_info
;
347 indio_dev
->name
= name
;
348 indio_dev
->modes
= INDIO_DIRECT_MODE
;
350 atomic_set(&incl_state
->common_attributes
.data_ready
, 0);
352 ret
= hid_sensor_setup_trigger(indio_dev
, name
,
353 &incl_state
->common_attributes
);
355 dev_err(&pdev
->dev
, "trigger setup failed\n");
359 ret
= iio_device_register(indio_dev
);
361 dev_err(&pdev
->dev
, "device register failed\n");
362 goto error_remove_trigger
;
365 incl_state
->callbacks
.send_event
= incl_3d_proc_event
;
366 incl_state
->callbacks
.capture_sample
= incl_3d_capture_sample
;
367 incl_state
->callbacks
.pdev
= pdev
;
368 ret
= sensor_hub_register_callback(hsdev
,
369 HID_USAGE_SENSOR_INCLINOMETER_3D
,
370 &incl_state
->callbacks
);
372 dev_err(&pdev
->dev
, "callback reg failed\n");
373 goto error_iio_unreg
;
379 iio_device_unregister(indio_dev
);
380 error_remove_trigger
:
381 hid_sensor_remove_trigger(indio_dev
, &incl_state
->common_attributes
);
385 /* Function to deinitialize the processing for usage id */
386 static void hid_incl_3d_remove(struct platform_device
*pdev
)
388 struct hid_sensor_hub_device
*hsdev
= dev_get_platdata(&pdev
->dev
);
389 struct iio_dev
*indio_dev
= platform_get_drvdata(pdev
);
390 struct incl_3d_state
*incl_state
= iio_priv(indio_dev
);
392 sensor_hub_remove_callback(hsdev
, HID_USAGE_SENSOR_INCLINOMETER_3D
);
393 iio_device_unregister(indio_dev
);
394 hid_sensor_remove_trigger(indio_dev
, &incl_state
->common_attributes
);
397 static const struct platform_device_id hid_incl_3d_ids
[] = {
399 /* Format: HID-SENSOR-usage_id_in_hex_lowercase */
400 .name
= "HID-SENSOR-200086",
404 MODULE_DEVICE_TABLE(platform
, hid_incl_3d_ids
);
406 static struct platform_driver hid_incl_3d_platform_driver
= {
407 .id_table
= hid_incl_3d_ids
,
409 .name
= KBUILD_MODNAME
,
410 .pm
= &hid_sensor_pm_ops
,
412 .probe
= hid_incl_3d_probe
,
413 .remove
= hid_incl_3d_remove
,
415 module_platform_driver(hid_incl_3d_platform_driver
);
417 MODULE_DESCRIPTION("HID Sensor Inclinometer 3D");
418 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
419 MODULE_LICENSE("GPL");
420 MODULE_IMPORT_NS(IIO_HID
);