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/mod_devicetable.h>
10 #include <linux/hid-sensor-hub.h>
11 #include <linux/iio/iio.h>
12 #include <linux/iio/buffer.h>
13 #include "../common/hid-sensors/hid-sensor-trigger.h"
15 enum magn_3d_channel
{
19 CHANNEL_SCAN_INDEX_NORTH_MAGN_TILT_COMP
,
20 CHANNEL_SCAN_INDEX_NORTH_TRUE_TILT_COMP
,
21 CHANNEL_SCAN_INDEX_NORTH_MAGN
,
22 CHANNEL_SCAN_INDEX_NORTH_TRUE
,
23 CHANNEL_SCAN_INDEX_TIMESTAMP
,
27 struct common_attributes
{
34 struct magn_3d_state
{
35 struct hid_sensor_hub_callbacks callbacks
;
36 struct hid_sensor_common magn_flux_attributes
;
37 struct hid_sensor_common rot_attributes
;
38 struct hid_sensor_hub_attribute_info magn
[MAGN_3D_CHANNEL_MAX
];
40 /* dynamically sized array to hold sensor values */
42 /* array of pointers to sensor value */
43 u32
*magn_val_addr
[MAGN_3D_CHANNEL_MAX
];
45 struct common_attributes magn_flux_attr
;
46 struct common_attributes rot_attr
;
50 static const u32 magn_3d_addresses
[MAGN_3D_CHANNEL_MAX
] = {
51 HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_X_AXIS
,
52 HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_Y_AXIS
,
53 HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_Z_AXIS
,
54 HID_USAGE_SENSOR_ORIENT_COMP_MAGN_NORTH
,
55 HID_USAGE_SENSOR_ORIENT_COMP_TRUE_NORTH
,
56 HID_USAGE_SENSOR_ORIENT_MAGN_NORTH
,
57 HID_USAGE_SENSOR_ORIENT_TRUE_NORTH
,
58 HID_USAGE_SENSOR_TIME_TIMESTAMP
,
61 static const u32 magn_3d_sensitivity_addresses
[] = {
62 HID_USAGE_SENSOR_DATA_ORIENTATION
,
63 HID_USAGE_SENSOR_ORIENT_MAGN_FLUX
,
66 /* Channel definitions */
67 static const struct iio_chan_spec magn_3d_channels
[] = {
71 .channel2
= IIO_MOD_X
,
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
),
80 .channel2
= IIO_MOD_Y
,
81 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
82 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_OFFSET
) |
83 BIT(IIO_CHAN_INFO_SCALE
) |
84 BIT(IIO_CHAN_INFO_SAMP_FREQ
) |
85 BIT(IIO_CHAN_INFO_HYSTERESIS
),
89 .channel2
= IIO_MOD_Z
,
90 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
91 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_OFFSET
) |
92 BIT(IIO_CHAN_INFO_SCALE
) |
93 BIT(IIO_CHAN_INFO_SAMP_FREQ
) |
94 BIT(IIO_CHAN_INFO_HYSTERESIS
),
98 .channel2
= IIO_MOD_NORTH_MAGN_TILT_COMP
,
99 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
100 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_OFFSET
) |
101 BIT(IIO_CHAN_INFO_SCALE
) |
102 BIT(IIO_CHAN_INFO_SAMP_FREQ
) |
103 BIT(IIO_CHAN_INFO_HYSTERESIS
),
107 .channel2
= IIO_MOD_NORTH_TRUE_TILT_COMP
,
108 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
109 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_OFFSET
) |
110 BIT(IIO_CHAN_INFO_SCALE
) |
111 BIT(IIO_CHAN_INFO_SAMP_FREQ
) |
112 BIT(IIO_CHAN_INFO_HYSTERESIS
),
116 .channel2
= IIO_MOD_NORTH_MAGN
,
117 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
118 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_OFFSET
) |
119 BIT(IIO_CHAN_INFO_SCALE
) |
120 BIT(IIO_CHAN_INFO_SAMP_FREQ
) |
121 BIT(IIO_CHAN_INFO_HYSTERESIS
),
125 .channel2
= IIO_MOD_NORTH_TRUE
,
126 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
127 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_OFFSET
) |
128 BIT(IIO_CHAN_INFO_SCALE
) |
129 BIT(IIO_CHAN_INFO_SAMP_FREQ
) |
130 BIT(IIO_CHAN_INFO_HYSTERESIS
),
132 IIO_CHAN_SOFT_TIMESTAMP(7)
135 /* Adjust channel real bits based on report descriptor */
136 static void magn_3d_adjust_channel_bit_mask(struct iio_chan_spec
*channels
,
137 int channel
, int size
)
139 channels
[channel
].scan_type
.sign
= 's';
140 /* Real storage bits will change based on the report desc. */
141 channels
[channel
].scan_type
.realbits
= size
* 8;
142 /* Maximum size of a sample to capture is u32 */
143 channels
[channel
].scan_type
.storagebits
= sizeof(u32
) * 8;
146 /* Channel read_raw handler */
147 static int magn_3d_read_raw(struct iio_dev
*indio_dev
,
148 struct iio_chan_spec
const *chan
,
152 struct magn_3d_state
*magn_state
= iio_priv(indio_dev
);
161 case IIO_CHAN_INFO_RAW
:
162 hid_sensor_power_state(&magn_state
->magn_flux_attributes
, true);
163 report_id
= magn_state
->magn
[chan
->address
].report_id
;
164 min
= magn_state
->magn
[chan
->address
].logical_minimum
;
165 address
= magn_3d_addresses
[chan
->address
];
167 *val
= sensor_hub_input_attr_get_raw_value(
168 magn_state
->magn_flux_attributes
.hsdev
,
169 HID_USAGE_SENSOR_COMPASS_3D
, address
,
175 hid_sensor_power_state(
176 &magn_state
->magn_flux_attributes
,
180 hid_sensor_power_state(&magn_state
->magn_flux_attributes
,
182 ret_type
= IIO_VAL_INT
;
184 case IIO_CHAN_INFO_SCALE
:
185 switch (chan
->type
) {
187 *val
= magn_state
->magn_flux_attr
.scale_pre_decml
;
188 *val2
= magn_state
->magn_flux_attr
.scale_post_decml
;
189 ret_type
= magn_state
->magn_flux_attr
.scale_precision
;
192 *val
= magn_state
->rot_attr
.scale_pre_decml
;
193 *val2
= magn_state
->rot_attr
.scale_post_decml
;
194 ret_type
= magn_state
->rot_attr
.scale_precision
;
200 case IIO_CHAN_INFO_OFFSET
:
201 switch (chan
->type
) {
203 *val
= magn_state
->magn_flux_attr
.value_offset
;
204 ret_type
= IIO_VAL_INT
;
207 *val
= magn_state
->rot_attr
.value_offset
;
208 ret_type
= IIO_VAL_INT
;
214 case IIO_CHAN_INFO_SAMP_FREQ
:
215 ret_type
= hid_sensor_read_samp_freq_value(
216 &magn_state
->magn_flux_attributes
, val
, val2
);
218 case IIO_CHAN_INFO_HYSTERESIS
:
219 switch (chan
->type
) {
221 ret_type
= hid_sensor_read_raw_hyst_value(
222 &magn_state
->magn_flux_attributes
, val
, val2
);
225 ret_type
= hid_sensor_read_raw_hyst_value(
226 &magn_state
->rot_attributes
, val
, val2
);
240 /* Channel write_raw handler */
241 static int magn_3d_write_raw(struct iio_dev
*indio_dev
,
242 struct iio_chan_spec
const *chan
,
247 struct magn_3d_state
*magn_state
= iio_priv(indio_dev
);
251 case IIO_CHAN_INFO_SAMP_FREQ
:
252 ret
= hid_sensor_write_samp_freq_value(
253 &magn_state
->magn_flux_attributes
, val
, val2
);
255 case IIO_CHAN_INFO_HYSTERESIS
:
256 switch (chan
->type
) {
258 ret
= hid_sensor_write_raw_hyst_value(
259 &magn_state
->magn_flux_attributes
, val
, val2
);
262 ret
= hid_sensor_write_raw_hyst_value(
263 &magn_state
->rot_attributes
, val
, val2
);
276 static const struct iio_info magn_3d_info
= {
277 .read_raw
= &magn_3d_read_raw
,
278 .write_raw
= &magn_3d_write_raw
,
281 /* Callback handler to send event after all samples are received and captured */
282 static int magn_3d_proc_event(struct hid_sensor_hub_device
*hsdev
,
286 struct iio_dev
*indio_dev
= platform_get_drvdata(priv
);
287 struct magn_3d_state
*magn_state
= iio_priv(indio_dev
);
289 dev_dbg(&indio_dev
->dev
, "magn_3d_proc_event\n");
290 if (atomic_read(&magn_state
->magn_flux_attributes
.data_ready
)) {
291 if (!magn_state
->timestamp
)
292 magn_state
->timestamp
= iio_get_time_ns(indio_dev
);
294 iio_push_to_buffers_with_timestamp(indio_dev
,
295 magn_state
->iio_vals
,
296 magn_state
->timestamp
);
297 magn_state
->timestamp
= 0;
303 /* Capture samples in local storage */
304 static int magn_3d_capture_sample(struct hid_sensor_hub_device
*hsdev
,
306 size_t raw_len
, char *raw_data
,
309 struct iio_dev
*indio_dev
= platform_get_drvdata(priv
);
310 struct magn_3d_state
*magn_state
= iio_priv(indio_dev
);
316 case HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_X_AXIS
:
317 case HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_Y_AXIS
:
318 case HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_Z_AXIS
:
319 offset
= (usage_id
- HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_X_AXIS
)
320 + CHANNEL_SCAN_INDEX_X
;
322 case HID_USAGE_SENSOR_ORIENT_COMP_MAGN_NORTH
:
323 case HID_USAGE_SENSOR_ORIENT_COMP_TRUE_NORTH
:
324 case HID_USAGE_SENSOR_ORIENT_MAGN_NORTH
:
325 case HID_USAGE_SENSOR_ORIENT_TRUE_NORTH
:
326 offset
= (usage_id
- HID_USAGE_SENSOR_ORIENT_COMP_MAGN_NORTH
)
327 + CHANNEL_SCAN_INDEX_NORTH_MAGN_TILT_COMP
;
329 case HID_USAGE_SENSOR_TIME_TIMESTAMP
:
330 magn_state
->timestamp
=
331 hid_sensor_convert_timestamp(&magn_state
->magn_flux_attributes
,
338 iio_val
= magn_state
->magn_val_addr
[offset
];
341 *iio_val
= *((u32
*)raw_data
);
348 /* Parse report which is specific to an usage id*/
349 static int magn_3d_parse_report(struct platform_device
*pdev
,
350 struct hid_sensor_hub_device
*hsdev
,
351 struct iio_chan_spec
**channels
,
354 struct magn_3d_state
*st
)
358 struct iio_chan_spec
*_channels
;
360 /* Scan for each usage attribute supported */
361 for (i
= 0; i
< MAGN_3D_CHANNEL_MAX
; i
++) {
363 u32 address
= magn_3d_addresses
[i
];
365 /* Check if usage attribute exists in the sensor hub device */
366 status
= sensor_hub_input_get_attribute_info(hsdev
,
375 if (attr_count
<= 0) {
377 "failed to find any supported usage attributes in report\n");
381 dev_dbg(&pdev
->dev
, "magn_3d Found %d usage attributes\n",
383 dev_dbg(&pdev
->dev
, "magn_3d X: %x:%x Y: %x:%x Z: %x:%x\n",
385 st
->magn
[0].report_id
,
386 st
->magn
[1].index
, st
->magn
[1].report_id
,
387 st
->magn
[2].index
, st
->magn
[2].report_id
);
389 /* Setup IIO channel array */
390 _channels
= devm_kcalloc(&pdev
->dev
, attr_count
,
391 sizeof(struct iio_chan_spec
),
395 "failed to allocate space for iio channels\n");
399 /* attr_count include timestamp channel, and the iio_vals should be aligned to 8byte */
400 st
->iio_vals
= devm_kcalloc(&pdev
->dev
,
401 ((attr_count
+ 1) % 2 + (attr_count
+ 1) / 2) * 2,
402 sizeof(u32
), GFP_KERNEL
);
405 "failed to allocate space for iio values array\n");
409 for (i
= 0, *chan_count
= 0;
410 i
< MAGN_3D_CHANNEL_MAX
&& *chan_count
< attr_count
;
412 if (st
->magn
[i
].index
>= 0) {
413 /* Setup IIO channel struct */
414 (_channels
[*chan_count
]) = magn_3d_channels
[i
];
415 (_channels
[*chan_count
]).scan_index
= *chan_count
;
416 (_channels
[*chan_count
]).address
= i
;
418 if (i
!= CHANNEL_SCAN_INDEX_TIMESTAMP
) {
419 /* Set magn_val_addr to iio value address */
420 st
->magn_val_addr
[i
] = &st
->iio_vals
[*chan_count
];
421 magn_3d_adjust_channel_bit_mask(_channels
,
429 if (*chan_count
<= 0) {
431 "failed to find any magnetic channels setup\n");
435 *channels
= _channels
;
437 dev_dbg(&pdev
->dev
, "magn_3d Setup %d IIO channels\n",
440 st
->magn_flux_attr
.scale_precision
= hid_sensor_format_scale(
441 HID_USAGE_SENSOR_COMPASS_3D
,
442 &st
->magn
[CHANNEL_SCAN_INDEX_X
],
443 &st
->magn_flux_attr
.scale_pre_decml
,
444 &st
->magn_flux_attr
.scale_post_decml
);
445 st
->rot_attr
.scale_precision
446 = hid_sensor_format_scale(
447 HID_USAGE_SENSOR_ORIENT_COMP_MAGN_NORTH
,
448 &st
->magn
[CHANNEL_SCAN_INDEX_NORTH_MAGN_TILT_COMP
],
449 &st
->rot_attr
.scale_pre_decml
,
450 &st
->rot_attr
.scale_post_decml
);
452 if (st
->rot_attributes
.sensitivity
.index
< 0) {
453 sensor_hub_input_get_attribute_info(hsdev
,
454 HID_FEATURE_REPORT
, usage_id
,
455 HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS
|
456 HID_USAGE_SENSOR_ORIENT_COMP_MAGN_NORTH
,
457 &st
->rot_attributes
.sensitivity
);
458 dev_dbg(&pdev
->dev
, "Sensitivity index:report %d:%d\n",
459 st
->rot_attributes
.sensitivity
.index
,
460 st
->rot_attributes
.sensitivity
.report_id
);
466 /* Function to initialize the processing for usage id */
467 static int hid_magn_3d_probe(struct platform_device
*pdev
)
469 struct hid_sensor_hub_device
*hsdev
= dev_get_platdata(&pdev
->dev
);
471 static char *name
= "magn_3d";
472 struct iio_dev
*indio_dev
;
473 struct magn_3d_state
*magn_state
;
474 struct iio_chan_spec
*channels
;
477 indio_dev
= devm_iio_device_alloc(&pdev
->dev
,
478 sizeof(struct magn_3d_state
));
479 if (indio_dev
== NULL
)
482 platform_set_drvdata(pdev
, indio_dev
);
484 magn_state
= iio_priv(indio_dev
);
485 magn_state
->magn_flux_attributes
.hsdev
= hsdev
;
486 magn_state
->magn_flux_attributes
.pdev
= pdev
;
488 ret
= hid_sensor_parse_common_attributes(hsdev
,
489 HID_USAGE_SENSOR_COMPASS_3D
,
490 &magn_state
->magn_flux_attributes
,
491 magn_3d_sensitivity_addresses
,
492 ARRAY_SIZE(magn_3d_sensitivity_addresses
));
494 dev_err(&pdev
->dev
, "failed to setup common attributes\n");
497 magn_state
->rot_attributes
= magn_state
->magn_flux_attributes
;
498 /* sensitivity of rot_attribute is not the same as magn_flux_attributes */
499 magn_state
->rot_attributes
.sensitivity
.index
= -1;
501 ret
= magn_3d_parse_report(pdev
, hsdev
,
502 &channels
, &chan_count
,
503 HID_USAGE_SENSOR_COMPASS_3D
, magn_state
);
505 dev_err(&pdev
->dev
, "failed to parse report\n");
509 indio_dev
->channels
= channels
;
510 indio_dev
->num_channels
= chan_count
;
511 indio_dev
->info
= &magn_3d_info
;
512 indio_dev
->name
= name
;
513 indio_dev
->modes
= INDIO_DIRECT_MODE
;
515 atomic_set(&magn_state
->magn_flux_attributes
.data_ready
, 0);
517 ret
= hid_sensor_setup_trigger(indio_dev
, name
,
518 &magn_state
->magn_flux_attributes
);
520 dev_err(&pdev
->dev
, "trigger setup failed\n");
524 ret
= iio_device_register(indio_dev
);
526 dev_err(&pdev
->dev
, "device register failed\n");
527 goto error_remove_trigger
;
530 magn_state
->callbacks
.send_event
= magn_3d_proc_event
;
531 magn_state
->callbacks
.capture_sample
= magn_3d_capture_sample
;
532 magn_state
->callbacks
.pdev
= pdev
;
533 ret
= sensor_hub_register_callback(hsdev
, HID_USAGE_SENSOR_COMPASS_3D
,
534 &magn_state
->callbacks
);
536 dev_err(&pdev
->dev
, "callback reg failed\n");
537 goto error_iio_unreg
;
543 iio_device_unregister(indio_dev
);
544 error_remove_trigger
:
545 hid_sensor_remove_trigger(indio_dev
, &magn_state
->magn_flux_attributes
);
549 /* Function to deinitialize the processing for usage id */
550 static void hid_magn_3d_remove(struct platform_device
*pdev
)
552 struct hid_sensor_hub_device
*hsdev
= dev_get_platdata(&pdev
->dev
);
553 struct iio_dev
*indio_dev
= platform_get_drvdata(pdev
);
554 struct magn_3d_state
*magn_state
= iio_priv(indio_dev
);
556 sensor_hub_remove_callback(hsdev
, HID_USAGE_SENSOR_COMPASS_3D
);
557 iio_device_unregister(indio_dev
);
558 hid_sensor_remove_trigger(indio_dev
, &magn_state
->magn_flux_attributes
);
561 static const struct platform_device_id hid_magn_3d_ids
[] = {
563 /* Format: HID-SENSOR-usage_id_in_hex_lowercase */
564 .name
= "HID-SENSOR-200083",
568 MODULE_DEVICE_TABLE(platform
, hid_magn_3d_ids
);
570 static struct platform_driver hid_magn_3d_platform_driver
= {
571 .id_table
= hid_magn_3d_ids
,
573 .name
= KBUILD_MODNAME
,
574 .pm
= &hid_sensor_pm_ops
,
576 .probe
= hid_magn_3d_probe
,
577 .remove
= hid_magn_3d_remove
,
579 module_platform_driver(hid_magn_3d_platform_driver
);
581 MODULE_DESCRIPTION("HID Sensor Magnetometer 3D");
582 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
583 MODULE_LICENSE("GPL");
584 MODULE_IMPORT_NS("IIO_HID");