3 * Copyright (c) 2012, Intel Corporation.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
19 #include <linux/device.h>
20 #include <linux/platform_device.h>
21 #include <linux/module.h>
22 #include <linux/interrupt.h>
23 #include <linux/irq.h>
24 #include <linux/slab.h>
25 #include <linux/delay.h>
26 #include <linux/hid-sensor-hub.h>
27 #include <linux/iio/iio.h>
28 #include <linux/iio/sysfs.h>
29 #include <linux/iio/buffer.h>
30 #include <linux/iio/trigger_consumer.h>
31 #include <linux/iio/triggered_buffer.h>
32 #include "../common/hid-sensors/hid-sensor-trigger.h"
34 enum magn_3d_channel
{
38 CHANNEL_SCAN_INDEX_NORTH_MAGN_TILT_COMP
,
39 CHANNEL_SCAN_INDEX_NORTH_TRUE_TILT_COMP
,
40 CHANNEL_SCAN_INDEX_NORTH_MAGN
,
41 CHANNEL_SCAN_INDEX_NORTH_TRUE
,
45 struct magn_3d_state
{
46 struct hid_sensor_hub_callbacks callbacks
;
47 struct hid_sensor_common common_attributes
;
48 struct hid_sensor_hub_attribute_info magn
[MAGN_3D_CHANNEL_MAX
];
50 /* dynamically sized array to hold sensor values */
52 /* array of pointers to sensor value */
53 u32
*magn_val_addr
[MAGN_3D_CHANNEL_MAX
];
61 static const u32 magn_3d_addresses
[MAGN_3D_CHANNEL_MAX
] = {
62 HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_X_AXIS
,
63 HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_Y_AXIS
,
64 HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_Z_AXIS
,
65 HID_USAGE_SENSOR_ORIENT_COMP_MAGN_NORTH
,
66 HID_USAGE_SENSOR_ORIENT_COMP_TRUE_NORTH
,
67 HID_USAGE_SENSOR_ORIENT_MAGN_NORTH
,
68 HID_USAGE_SENSOR_ORIENT_TRUE_NORTH
,
71 /* Channel definitions */
72 static const struct iio_chan_spec magn_3d_channels
[] = {
76 .channel2
= IIO_MOD_X
,
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_Y
,
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_Z
,
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_MAGN_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_TRUE_TILT_COMP
,
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_MAGN
,
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 .channel2
= IIO_MOD_NORTH_TRUE
,
131 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
132 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_OFFSET
) |
133 BIT(IIO_CHAN_INFO_SCALE
) |
134 BIT(IIO_CHAN_INFO_SAMP_FREQ
) |
135 BIT(IIO_CHAN_INFO_HYSTERESIS
),
139 /* Adjust channel real bits based on report descriptor */
140 static void magn_3d_adjust_channel_bit_mask(struct iio_chan_spec
*channels
,
141 int channel
, int size
)
143 channels
[channel
].scan_type
.sign
= 's';
144 /* Real storage bits will change based on the report desc. */
145 channels
[channel
].scan_type
.realbits
= size
* 8;
146 /* Maximum size of a sample to capture is u32 */
147 channels
[channel
].scan_type
.storagebits
= sizeof(u32
) * 8;
150 /* Channel read_raw handler */
151 static int magn_3d_read_raw(struct iio_dev
*indio_dev
,
152 struct iio_chan_spec
const *chan
,
156 struct magn_3d_state
*magn_state
= iio_priv(indio_dev
);
166 poll_value
= hid_sensor_read_poll_value(
167 &magn_state
->common_attributes
);
171 hid_sensor_power_state(&magn_state
->common_attributes
, true);
172 msleep_interruptible(poll_value
* 2);
175 magn_state
->magn
[chan
->address
].report_id
;
176 address
= magn_3d_addresses
[chan
->address
];
178 *val
= sensor_hub_input_attr_get_raw_value(
179 magn_state
->common_attributes
.hsdev
,
180 HID_USAGE_SENSOR_COMPASS_3D
, address
,
184 hid_sensor_power_state(&magn_state
->common_attributes
,
188 hid_sensor_power_state(&magn_state
->common_attributes
, false);
189 ret_type
= IIO_VAL_INT
;
191 case IIO_CHAN_INFO_SCALE
:
192 *val
= magn_state
->scale_pre_decml
;
193 *val2
= magn_state
->scale_post_decml
;
194 ret_type
= magn_state
->scale_precision
;
196 case IIO_CHAN_INFO_OFFSET
:
197 *val
= magn_state
->value_offset
;
198 ret_type
= IIO_VAL_INT
;
200 case IIO_CHAN_INFO_SAMP_FREQ
:
201 ret_type
= hid_sensor_read_samp_freq_value(
202 &magn_state
->common_attributes
, val
, val2
);
204 case IIO_CHAN_INFO_HYSTERESIS
:
205 ret_type
= hid_sensor_read_raw_hyst_value(
206 &magn_state
->common_attributes
, val
, val2
);
216 /* Channel write_raw handler */
217 static int magn_3d_write_raw(struct iio_dev
*indio_dev
,
218 struct iio_chan_spec
const *chan
,
223 struct magn_3d_state
*magn_state
= iio_priv(indio_dev
);
227 case IIO_CHAN_INFO_SAMP_FREQ
:
228 ret
= hid_sensor_write_samp_freq_value(
229 &magn_state
->common_attributes
, val
, val2
);
231 case IIO_CHAN_INFO_HYSTERESIS
:
232 ret
= hid_sensor_write_raw_hyst_value(
233 &magn_state
->common_attributes
, val
, val2
);
242 static const struct iio_info magn_3d_info
= {
243 .driver_module
= THIS_MODULE
,
244 .read_raw
= &magn_3d_read_raw
,
245 .write_raw
= &magn_3d_write_raw
,
248 /* Function to push data to buffer */
249 static void hid_sensor_push_data(struct iio_dev
*indio_dev
, const void *data
)
251 dev_dbg(&indio_dev
->dev
, "hid_sensor_push_data\n");
252 iio_push_to_buffers(indio_dev
, data
);
255 /* Callback handler to send event after all samples are received and captured */
256 static int magn_3d_proc_event(struct hid_sensor_hub_device
*hsdev
,
260 struct iio_dev
*indio_dev
= platform_get_drvdata(priv
);
261 struct magn_3d_state
*magn_state
= iio_priv(indio_dev
);
263 dev_dbg(&indio_dev
->dev
, "magn_3d_proc_event\n");
264 if (atomic_read(&magn_state
->common_attributes
.data_ready
))
265 hid_sensor_push_data(indio_dev
, magn_state
->iio_vals
);
270 /* Capture samples in local storage */
271 static int magn_3d_capture_sample(struct hid_sensor_hub_device
*hsdev
,
273 size_t raw_len
, char *raw_data
,
276 struct iio_dev
*indio_dev
= platform_get_drvdata(priv
);
277 struct magn_3d_state
*magn_state
= iio_priv(indio_dev
);
283 case HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_X_AXIS
:
284 case HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_Y_AXIS
:
285 case HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_Z_AXIS
:
286 offset
= (usage_id
- HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_X_AXIS
)
287 + CHANNEL_SCAN_INDEX_X
;
289 case HID_USAGE_SENSOR_ORIENT_COMP_MAGN_NORTH
:
290 case HID_USAGE_SENSOR_ORIENT_COMP_TRUE_NORTH
:
291 case HID_USAGE_SENSOR_ORIENT_MAGN_NORTH
:
292 case HID_USAGE_SENSOR_ORIENT_TRUE_NORTH
:
293 offset
= (usage_id
- HID_USAGE_SENSOR_ORIENT_COMP_MAGN_NORTH
)
294 + CHANNEL_SCAN_INDEX_NORTH_MAGN_TILT_COMP
;
300 iio_val
= magn_state
->magn_val_addr
[offset
];
303 *iio_val
= *((u32
*)raw_data
);
310 /* Parse report which is specific to an usage id*/
311 static int magn_3d_parse_report(struct platform_device
*pdev
,
312 struct hid_sensor_hub_device
*hsdev
,
313 struct iio_chan_spec
**channels
,
316 struct magn_3d_state
*st
)
320 struct iio_chan_spec
*_channels
;
322 /* Scan for each usage attribute supported */
323 for (i
= 0; i
< MAGN_3D_CHANNEL_MAX
; i
++) {
325 u32 address
= magn_3d_addresses
[i
];
327 /* Check if usage attribute exists in the sensor hub device */
328 status
= sensor_hub_input_get_attribute_info(hsdev
,
337 if (attr_count
<= 0) {
339 "failed to find any supported usage attributes in report\n");
343 dev_dbg(&pdev
->dev
, "magn_3d Found %d usage attributes\n",
345 dev_dbg(&pdev
->dev
, "magn_3d X: %x:%x Y: %x:%x Z: %x:%x\n",
347 st
->magn
[0].report_id
,
348 st
->magn
[1].index
, st
->magn
[1].report_id
,
349 st
->magn
[2].index
, st
->magn
[2].report_id
);
351 /* Setup IIO channel array */
352 _channels
= devm_kcalloc(&pdev
->dev
, attr_count
,
353 sizeof(struct iio_chan_spec
),
357 "failed to allocate space for iio channels\n");
361 st
->iio_vals
= devm_kcalloc(&pdev
->dev
, attr_count
,
366 "failed to allocate space for iio values array\n");
370 for (i
= 0, *chan_count
= 0;
371 i
< MAGN_3D_CHANNEL_MAX
&& *chan_count
< attr_count
;
373 if (st
->magn
[i
].index
>= 0) {
374 /* Setup IIO channel struct */
375 (_channels
[*chan_count
]) = magn_3d_channels
[i
];
376 (_channels
[*chan_count
]).scan_index
= *chan_count
;
377 (_channels
[*chan_count
]).address
= i
;
379 /* Set magn_val_addr to iio value address */
380 st
->magn_val_addr
[i
] = &(st
->iio_vals
[*chan_count
]);
381 magn_3d_adjust_channel_bit_mask(_channels
,
388 if (*chan_count
<= 0) {
390 "failed to find any magnetic channels setup\n");
394 *channels
= _channels
;
396 dev_dbg(&pdev
->dev
, "magn_3d Setup %d IIO channels\n",
399 st
->scale_precision
= hid_sensor_format_scale(
400 HID_USAGE_SENSOR_COMPASS_3D
,
401 &st
->magn
[CHANNEL_SCAN_INDEX_X
],
402 &st
->scale_pre_decml
, &st
->scale_post_decml
);
404 /* Set Sensitivity field ids, when there is no individual modifier */
405 if (st
->common_attributes
.sensitivity
.index
< 0) {
406 sensor_hub_input_get_attribute_info(hsdev
,
407 HID_FEATURE_REPORT
, usage_id
,
408 HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS
|
409 HID_USAGE_SENSOR_DATA_ORIENTATION
,
410 &st
->common_attributes
.sensitivity
);
411 dev_dbg(&pdev
->dev
, "Sensitivity index:report %d:%d\n",
412 st
->common_attributes
.sensitivity
.index
,
413 st
->common_attributes
.sensitivity
.report_id
);
419 /* Function to initialize the processing for usage id */
420 static int hid_magn_3d_probe(struct platform_device
*pdev
)
423 static char *name
= "magn_3d";
424 struct iio_dev
*indio_dev
;
425 struct magn_3d_state
*magn_state
;
426 struct hid_sensor_hub_device
*hsdev
= pdev
->dev
.platform_data
;
427 struct iio_chan_spec
*channels
;
430 indio_dev
= devm_iio_device_alloc(&pdev
->dev
,
431 sizeof(struct magn_3d_state
));
432 if (indio_dev
== NULL
)
435 platform_set_drvdata(pdev
, indio_dev
);
437 magn_state
= iio_priv(indio_dev
);
438 magn_state
->common_attributes
.hsdev
= hsdev
;
439 magn_state
->common_attributes
.pdev
= pdev
;
441 ret
= hid_sensor_parse_common_attributes(hsdev
,
442 HID_USAGE_SENSOR_COMPASS_3D
,
443 &magn_state
->common_attributes
);
445 dev_err(&pdev
->dev
, "failed to setup common attributes\n");
449 ret
= magn_3d_parse_report(pdev
, hsdev
,
450 &channels
, &chan_count
,
451 HID_USAGE_SENSOR_COMPASS_3D
, magn_state
);
453 dev_err(&pdev
->dev
, "failed to parse report\n");
457 indio_dev
->channels
= channels
;
458 indio_dev
->num_channels
= chan_count
;
459 indio_dev
->dev
.parent
= &pdev
->dev
;
460 indio_dev
->info
= &magn_3d_info
;
461 indio_dev
->name
= name
;
462 indio_dev
->modes
= INDIO_DIRECT_MODE
;
464 ret
= iio_triggered_buffer_setup(indio_dev
, &iio_pollfunc_store_time
,
467 dev_err(&pdev
->dev
, "failed to initialize trigger buffer\n");
470 atomic_set(&magn_state
->common_attributes
.data_ready
, 0);
471 ret
= hid_sensor_setup_trigger(indio_dev
, name
,
472 &magn_state
->common_attributes
);
474 dev_err(&pdev
->dev
, "trigger setup failed\n");
475 goto error_unreg_buffer_funcs
;
478 ret
= iio_device_register(indio_dev
);
480 dev_err(&pdev
->dev
, "device register failed\n");
481 goto error_remove_trigger
;
484 magn_state
->callbacks
.send_event
= magn_3d_proc_event
;
485 magn_state
->callbacks
.capture_sample
= magn_3d_capture_sample
;
486 magn_state
->callbacks
.pdev
= pdev
;
487 ret
= sensor_hub_register_callback(hsdev
, HID_USAGE_SENSOR_COMPASS_3D
,
488 &magn_state
->callbacks
);
490 dev_err(&pdev
->dev
, "callback reg failed\n");
491 goto error_iio_unreg
;
497 iio_device_unregister(indio_dev
);
498 error_remove_trigger
:
499 hid_sensor_remove_trigger(&magn_state
->common_attributes
);
500 error_unreg_buffer_funcs
:
501 iio_triggered_buffer_cleanup(indio_dev
);
505 /* Function to deinitialize the processing for usage id */
506 static int hid_magn_3d_remove(struct platform_device
*pdev
)
508 struct hid_sensor_hub_device
*hsdev
= pdev
->dev
.platform_data
;
509 struct iio_dev
*indio_dev
= platform_get_drvdata(pdev
);
510 struct magn_3d_state
*magn_state
= iio_priv(indio_dev
);
512 sensor_hub_remove_callback(hsdev
, HID_USAGE_SENSOR_COMPASS_3D
);
513 iio_device_unregister(indio_dev
);
514 hid_sensor_remove_trigger(&magn_state
->common_attributes
);
515 iio_triggered_buffer_cleanup(indio_dev
);
520 static struct platform_device_id hid_magn_3d_ids
[] = {
522 /* Format: HID-SENSOR-usage_id_in_hex_lowercase */
523 .name
= "HID-SENSOR-200083",
527 MODULE_DEVICE_TABLE(platform
, hid_magn_3d_ids
);
529 static struct platform_driver hid_magn_3d_platform_driver
= {
530 .id_table
= hid_magn_3d_ids
,
532 .name
= KBUILD_MODNAME
,
534 .probe
= hid_magn_3d_probe
,
535 .remove
= hid_magn_3d_remove
,
537 module_platform_driver(hid_magn_3d_platform_driver
);
539 MODULE_DESCRIPTION("HID Sensor Magnetometer 3D");
540 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
541 MODULE_LICENSE("GPL");