1 // SPDX-License-Identifier: GPL-2.0-only
4 * Copyright (c) 2012, Intel Corporation.
6 #include <linux/device.h>
7 #include <linux/platform_device.h>
8 #include <linux/module.h>
9 #include <linux/interrupt.h>
10 #include <linux/irq.h>
11 #include <linux/slab.h>
12 #include <linux/delay.h>
13 #include <linux/hid-sensor-hub.h>
14 #include <linux/iio/iio.h>
15 #include <linux/iio/sysfs.h>
16 #include <linux/iio/buffer.h>
17 #include "../common/hid-sensors/hid-sensor-trigger.h"
19 enum magn_3d_channel
{
23 CHANNEL_SCAN_INDEX_NORTH_MAGN_TILT_COMP
,
24 CHANNEL_SCAN_INDEX_NORTH_TRUE_TILT_COMP
,
25 CHANNEL_SCAN_INDEX_NORTH_MAGN
,
26 CHANNEL_SCAN_INDEX_NORTH_TRUE
,
30 struct common_attributes
{
37 struct magn_3d_state
{
38 struct hid_sensor_hub_callbacks callbacks
;
39 struct hid_sensor_common magn_flux_attributes
;
40 struct hid_sensor_common rot_attributes
;
41 struct hid_sensor_hub_attribute_info magn
[MAGN_3D_CHANNEL_MAX
];
43 /* dynamically sized array to hold sensor values */
45 /* array of pointers to sensor value */
46 u32
*magn_val_addr
[MAGN_3D_CHANNEL_MAX
];
48 struct common_attributes magn_flux_attr
;
49 struct common_attributes rot_attr
;
52 static const u32 magn_3d_addresses
[MAGN_3D_CHANNEL_MAX
] = {
53 HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_X_AXIS
,
54 HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_Y_AXIS
,
55 HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_Z_AXIS
,
56 HID_USAGE_SENSOR_ORIENT_COMP_MAGN_NORTH
,
57 HID_USAGE_SENSOR_ORIENT_COMP_TRUE_NORTH
,
58 HID_USAGE_SENSOR_ORIENT_MAGN_NORTH
,
59 HID_USAGE_SENSOR_ORIENT_TRUE_NORTH
,
62 /* Channel definitions */
63 static const struct iio_chan_spec magn_3d_channels
[] = {
67 .channel2
= IIO_MOD_X
,
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
),
76 .channel2
= IIO_MOD_Y
,
77 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
78 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_OFFSET
) |
79 BIT(IIO_CHAN_INFO_SCALE
) |
80 BIT(IIO_CHAN_INFO_SAMP_FREQ
) |
81 BIT(IIO_CHAN_INFO_HYSTERESIS
),
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
),
94 .channel2
= IIO_MOD_NORTH_MAGN_TILT_COMP
,
95 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
96 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_OFFSET
) |
97 BIT(IIO_CHAN_INFO_SCALE
) |
98 BIT(IIO_CHAN_INFO_SAMP_FREQ
) |
99 BIT(IIO_CHAN_INFO_HYSTERESIS
),
103 .channel2
= IIO_MOD_NORTH_TRUE_TILT_COMP
,
104 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
105 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_OFFSET
) |
106 BIT(IIO_CHAN_INFO_SCALE
) |
107 BIT(IIO_CHAN_INFO_SAMP_FREQ
) |
108 BIT(IIO_CHAN_INFO_HYSTERESIS
),
112 .channel2
= IIO_MOD_NORTH_MAGN
,
113 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
114 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_OFFSET
) |
115 BIT(IIO_CHAN_INFO_SCALE
) |
116 BIT(IIO_CHAN_INFO_SAMP_FREQ
) |
117 BIT(IIO_CHAN_INFO_HYSTERESIS
),
121 .channel2
= IIO_MOD_NORTH_TRUE
,
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
),
130 /* Adjust channel real bits based on report descriptor */
131 static void magn_3d_adjust_channel_bit_mask(struct iio_chan_spec
*channels
,
132 int channel
, int size
)
134 channels
[channel
].scan_type
.sign
= 's';
135 /* Real storage bits will change based on the report desc. */
136 channels
[channel
].scan_type
.realbits
= size
* 8;
137 /* Maximum size of a sample to capture is u32 */
138 channels
[channel
].scan_type
.storagebits
= sizeof(u32
) * 8;
141 /* Channel read_raw handler */
142 static int magn_3d_read_raw(struct iio_dev
*indio_dev
,
143 struct iio_chan_spec
const *chan
,
147 struct magn_3d_state
*magn_state
= iio_priv(indio_dev
);
156 case IIO_CHAN_INFO_RAW
:
157 hid_sensor_power_state(&magn_state
->magn_flux_attributes
, true);
158 report_id
= magn_state
->magn
[chan
->address
].report_id
;
159 min
= magn_state
->magn
[chan
->address
].logical_minimum
;
160 address
= magn_3d_addresses
[chan
->address
];
162 *val
= sensor_hub_input_attr_get_raw_value(
163 magn_state
->magn_flux_attributes
.hsdev
,
164 HID_USAGE_SENSOR_COMPASS_3D
, address
,
170 hid_sensor_power_state(
171 &magn_state
->magn_flux_attributes
,
175 hid_sensor_power_state(&magn_state
->magn_flux_attributes
,
177 ret_type
= IIO_VAL_INT
;
179 case IIO_CHAN_INFO_SCALE
:
180 switch (chan
->type
) {
182 *val
= magn_state
->magn_flux_attr
.scale_pre_decml
;
183 *val2
= magn_state
->magn_flux_attr
.scale_post_decml
;
184 ret_type
= magn_state
->magn_flux_attr
.scale_precision
;
187 *val
= magn_state
->rot_attr
.scale_pre_decml
;
188 *val2
= magn_state
->rot_attr
.scale_post_decml
;
189 ret_type
= magn_state
->rot_attr
.scale_precision
;
195 case IIO_CHAN_INFO_OFFSET
:
196 switch (chan
->type
) {
198 *val
= magn_state
->magn_flux_attr
.value_offset
;
199 ret_type
= IIO_VAL_INT
;
202 *val
= magn_state
->rot_attr
.value_offset
;
203 ret_type
= IIO_VAL_INT
;
209 case IIO_CHAN_INFO_SAMP_FREQ
:
210 ret_type
= hid_sensor_read_samp_freq_value(
211 &magn_state
->magn_flux_attributes
, val
, val2
);
213 case IIO_CHAN_INFO_HYSTERESIS
:
214 switch (chan
->type
) {
216 ret_type
= hid_sensor_read_raw_hyst_value(
217 &magn_state
->magn_flux_attributes
, val
, val2
);
220 ret_type
= hid_sensor_read_raw_hyst_value(
221 &magn_state
->rot_attributes
, val
, val2
);
235 /* Channel write_raw handler */
236 static int magn_3d_write_raw(struct iio_dev
*indio_dev
,
237 struct iio_chan_spec
const *chan
,
242 struct magn_3d_state
*magn_state
= iio_priv(indio_dev
);
246 case IIO_CHAN_INFO_SAMP_FREQ
:
247 ret
= hid_sensor_write_samp_freq_value(
248 &magn_state
->magn_flux_attributes
, val
, val2
);
250 case IIO_CHAN_INFO_HYSTERESIS
:
251 switch (chan
->type
) {
253 ret
= hid_sensor_write_raw_hyst_value(
254 &magn_state
->magn_flux_attributes
, val
, val2
);
257 ret
= hid_sensor_write_raw_hyst_value(
258 &magn_state
->rot_attributes
, val
, val2
);
271 static const struct iio_info magn_3d_info
= {
272 .read_raw
= &magn_3d_read_raw
,
273 .write_raw
= &magn_3d_write_raw
,
276 /* Function to push data to buffer */
277 static void hid_sensor_push_data(struct iio_dev
*indio_dev
, const void *data
)
279 dev_dbg(&indio_dev
->dev
, "hid_sensor_push_data\n");
280 iio_push_to_buffers(indio_dev
, data
);
283 /* Callback handler to send event after all samples are received and captured */
284 static int magn_3d_proc_event(struct hid_sensor_hub_device
*hsdev
,
288 struct iio_dev
*indio_dev
= platform_get_drvdata(priv
);
289 struct magn_3d_state
*magn_state
= iio_priv(indio_dev
);
291 dev_dbg(&indio_dev
->dev
, "magn_3d_proc_event\n");
292 if (atomic_read(&magn_state
->magn_flux_attributes
.data_ready
))
293 hid_sensor_push_data(indio_dev
, magn_state
->iio_vals
);
298 /* Capture samples in local storage */
299 static int magn_3d_capture_sample(struct hid_sensor_hub_device
*hsdev
,
301 size_t raw_len
, char *raw_data
,
304 struct iio_dev
*indio_dev
= platform_get_drvdata(priv
);
305 struct magn_3d_state
*magn_state
= iio_priv(indio_dev
);
311 case HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_X_AXIS
:
312 case HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_Y_AXIS
:
313 case HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_Z_AXIS
:
314 offset
= (usage_id
- HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_X_AXIS
)
315 + CHANNEL_SCAN_INDEX_X
;
317 case HID_USAGE_SENSOR_ORIENT_COMP_MAGN_NORTH
:
318 case HID_USAGE_SENSOR_ORIENT_COMP_TRUE_NORTH
:
319 case HID_USAGE_SENSOR_ORIENT_MAGN_NORTH
:
320 case HID_USAGE_SENSOR_ORIENT_TRUE_NORTH
:
321 offset
= (usage_id
- HID_USAGE_SENSOR_ORIENT_COMP_MAGN_NORTH
)
322 + CHANNEL_SCAN_INDEX_NORTH_MAGN_TILT_COMP
;
328 iio_val
= magn_state
->magn_val_addr
[offset
];
331 *iio_val
= *((u32
*)raw_data
);
338 /* Parse report which is specific to an usage id*/
339 static int magn_3d_parse_report(struct platform_device
*pdev
,
340 struct hid_sensor_hub_device
*hsdev
,
341 struct iio_chan_spec
**channels
,
344 struct magn_3d_state
*st
)
348 struct iio_chan_spec
*_channels
;
350 /* Scan for each usage attribute supported */
351 for (i
= 0; i
< MAGN_3D_CHANNEL_MAX
; i
++) {
353 u32 address
= magn_3d_addresses
[i
];
355 /* Check if usage attribute exists in the sensor hub device */
356 status
= sensor_hub_input_get_attribute_info(hsdev
,
365 if (attr_count
<= 0) {
367 "failed to find any supported usage attributes in report\n");
371 dev_dbg(&pdev
->dev
, "magn_3d Found %d usage attributes\n",
373 dev_dbg(&pdev
->dev
, "magn_3d X: %x:%x Y: %x:%x Z: %x:%x\n",
375 st
->magn
[0].report_id
,
376 st
->magn
[1].index
, st
->magn
[1].report_id
,
377 st
->magn
[2].index
, st
->magn
[2].report_id
);
379 /* Setup IIO channel array */
380 _channels
= devm_kcalloc(&pdev
->dev
, attr_count
,
381 sizeof(struct iio_chan_spec
),
385 "failed to allocate space for iio channels\n");
389 st
->iio_vals
= devm_kcalloc(&pdev
->dev
, attr_count
,
394 "failed to allocate space for iio values array\n");
398 for (i
= 0, *chan_count
= 0;
399 i
< MAGN_3D_CHANNEL_MAX
&& *chan_count
< attr_count
;
401 if (st
->magn
[i
].index
>= 0) {
402 /* Setup IIO channel struct */
403 (_channels
[*chan_count
]) = magn_3d_channels
[i
];
404 (_channels
[*chan_count
]).scan_index
= *chan_count
;
405 (_channels
[*chan_count
]).address
= i
;
407 /* Set magn_val_addr to iio value address */
408 st
->magn_val_addr
[i
] = &(st
->iio_vals
[*chan_count
]);
409 magn_3d_adjust_channel_bit_mask(_channels
,
416 if (*chan_count
<= 0) {
418 "failed to find any magnetic channels setup\n");
422 *channels
= _channels
;
424 dev_dbg(&pdev
->dev
, "magn_3d Setup %d IIO channels\n",
427 st
->magn_flux_attr
.scale_precision
= hid_sensor_format_scale(
428 HID_USAGE_SENSOR_COMPASS_3D
,
429 &st
->magn
[CHANNEL_SCAN_INDEX_X
],
430 &st
->magn_flux_attr
.scale_pre_decml
,
431 &st
->magn_flux_attr
.scale_post_decml
);
432 st
->rot_attr
.scale_precision
433 = hid_sensor_format_scale(
434 HID_USAGE_SENSOR_ORIENT_COMP_MAGN_NORTH
,
435 &st
->magn
[CHANNEL_SCAN_INDEX_NORTH_MAGN_TILT_COMP
],
436 &st
->rot_attr
.scale_pre_decml
,
437 &st
->rot_attr
.scale_post_decml
);
439 /* Set Sensitivity field ids, when there is no individual modifier */
440 if (st
->magn_flux_attributes
.sensitivity
.index
< 0) {
441 sensor_hub_input_get_attribute_info(hsdev
,
442 HID_FEATURE_REPORT
, usage_id
,
443 HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS
|
444 HID_USAGE_SENSOR_DATA_ORIENTATION
,
445 &st
->magn_flux_attributes
.sensitivity
);
446 dev_dbg(&pdev
->dev
, "Sensitivity index:report %d:%d\n",
447 st
->magn_flux_attributes
.sensitivity
.index
,
448 st
->magn_flux_attributes
.sensitivity
.report_id
);
450 if (st
->magn_flux_attributes
.sensitivity
.index
< 0) {
451 sensor_hub_input_get_attribute_info(hsdev
,
452 HID_FEATURE_REPORT
, usage_id
,
453 HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS
|
454 HID_USAGE_SENSOR_ORIENT_MAGN_FLUX
,
455 &st
->magn_flux_attributes
.sensitivity
);
456 dev_dbg(&pdev
->dev
, "Sensitivity index:report %d:%d\n",
457 st
->magn_flux_attributes
.sensitivity
.index
,
458 st
->magn_flux_attributes
.sensitivity
.report_id
);
460 if (st
->rot_attributes
.sensitivity
.index
< 0) {
461 sensor_hub_input_get_attribute_info(hsdev
,
462 HID_FEATURE_REPORT
, usage_id
,
463 HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS
|
464 HID_USAGE_SENSOR_ORIENT_COMP_MAGN_NORTH
,
465 &st
->rot_attributes
.sensitivity
);
466 dev_dbg(&pdev
->dev
, "Sensitivity index:report %d:%d\n",
467 st
->rot_attributes
.sensitivity
.index
,
468 st
->rot_attributes
.sensitivity
.report_id
);
474 /* Function to initialize the processing for usage id */
475 static int hid_magn_3d_probe(struct platform_device
*pdev
)
478 static char *name
= "magn_3d";
479 struct iio_dev
*indio_dev
;
480 struct magn_3d_state
*magn_state
;
481 struct hid_sensor_hub_device
*hsdev
= pdev
->dev
.platform_data
;
482 struct iio_chan_spec
*channels
;
485 indio_dev
= devm_iio_device_alloc(&pdev
->dev
,
486 sizeof(struct magn_3d_state
));
487 if (indio_dev
== NULL
)
490 platform_set_drvdata(pdev
, indio_dev
);
492 magn_state
= iio_priv(indio_dev
);
493 magn_state
->magn_flux_attributes
.hsdev
= hsdev
;
494 magn_state
->magn_flux_attributes
.pdev
= pdev
;
496 ret
= hid_sensor_parse_common_attributes(hsdev
,
497 HID_USAGE_SENSOR_COMPASS_3D
,
498 &magn_state
->magn_flux_attributes
);
500 dev_err(&pdev
->dev
, "failed to setup common attributes\n");
503 magn_state
->rot_attributes
= magn_state
->magn_flux_attributes
;
505 ret
= magn_3d_parse_report(pdev
, hsdev
,
506 &channels
, &chan_count
,
507 HID_USAGE_SENSOR_COMPASS_3D
, magn_state
);
509 dev_err(&pdev
->dev
, "failed to parse report\n");
513 indio_dev
->channels
= channels
;
514 indio_dev
->num_channels
= chan_count
;
515 indio_dev
->info
= &magn_3d_info
;
516 indio_dev
->name
= name
;
517 indio_dev
->modes
= INDIO_DIRECT_MODE
;
519 atomic_set(&magn_state
->magn_flux_attributes
.data_ready
, 0);
521 ret
= hid_sensor_setup_trigger(indio_dev
, name
,
522 &magn_state
->magn_flux_attributes
);
524 dev_err(&pdev
->dev
, "trigger setup failed\n");
528 ret
= iio_device_register(indio_dev
);
530 dev_err(&pdev
->dev
, "device register failed\n");
531 goto error_remove_trigger
;
534 magn_state
->callbacks
.send_event
= magn_3d_proc_event
;
535 magn_state
->callbacks
.capture_sample
= magn_3d_capture_sample
;
536 magn_state
->callbacks
.pdev
= pdev
;
537 ret
= sensor_hub_register_callback(hsdev
, HID_USAGE_SENSOR_COMPASS_3D
,
538 &magn_state
->callbacks
);
540 dev_err(&pdev
->dev
, "callback reg failed\n");
541 goto error_iio_unreg
;
547 iio_device_unregister(indio_dev
);
548 error_remove_trigger
:
549 hid_sensor_remove_trigger(indio_dev
, &magn_state
->magn_flux_attributes
);
553 /* Function to deinitialize the processing for usage id */
554 static int hid_magn_3d_remove(struct platform_device
*pdev
)
556 struct hid_sensor_hub_device
*hsdev
= pdev
->dev
.platform_data
;
557 struct iio_dev
*indio_dev
= platform_get_drvdata(pdev
);
558 struct magn_3d_state
*magn_state
= iio_priv(indio_dev
);
560 sensor_hub_remove_callback(hsdev
, HID_USAGE_SENSOR_COMPASS_3D
);
561 iio_device_unregister(indio_dev
);
562 hid_sensor_remove_trigger(indio_dev
, &magn_state
->magn_flux_attributes
);
567 static const struct platform_device_id hid_magn_3d_ids
[] = {
569 /* Format: HID-SENSOR-usage_id_in_hex_lowercase */
570 .name
= "HID-SENSOR-200083",
574 MODULE_DEVICE_TABLE(platform
, hid_magn_3d_ids
);
576 static struct platform_driver hid_magn_3d_platform_driver
= {
577 .id_table
= hid_magn_3d_ids
,
579 .name
= KBUILD_MODNAME
,
580 .pm
= &hid_sensor_pm_ops
,
582 .probe
= hid_magn_3d_probe
,
583 .remove
= hid_magn_3d_remove
,
585 module_platform_driver(hid_magn_3d_platform_driver
);
587 MODULE_DESCRIPTION("HID Sensor Magnetometer 3D");
588 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
589 MODULE_LICENSE("GPL");