1 // SPDX-License-Identifier: GPL-2.0-only
3 * STMicroelectronics magnetometers driver
5 * Copyright 2012-2013 STMicroelectronics Inc.
7 * Denis Ciocca <denis.ciocca@st.com>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/slab.h>
13 #include <linux/errno.h>
14 #include <linux/types.h>
15 #include <linux/mutex.h>
16 #include <linux/interrupt.h>
17 #include <linux/i2c.h>
18 #include <linux/gpio.h>
19 #include <linux/irq.h>
20 #include <linux/delay.h>
21 #include <linux/iio/iio.h>
22 #include <linux/iio/sysfs.h>
23 #include <linux/iio/buffer.h>
25 #include <linux/iio/common/st_sensors.h>
28 #define ST_MAGN_NUMBER_DATA_CHANNELS 3
30 /* DEFAULT VALUE FOR SENSORS */
31 #define ST_MAGN_DEFAULT_OUT_X_H_ADDR 0x03
32 #define ST_MAGN_DEFAULT_OUT_Y_H_ADDR 0x07
33 #define ST_MAGN_DEFAULT_OUT_Z_H_ADDR 0x05
36 #define ST_MAGN_FS_AVL_1300MG 1300
37 #define ST_MAGN_FS_AVL_1900MG 1900
38 #define ST_MAGN_FS_AVL_2500MG 2500
39 #define ST_MAGN_FS_AVL_4000MG 4000
40 #define ST_MAGN_FS_AVL_4700MG 4700
41 #define ST_MAGN_FS_AVL_5600MG 5600
42 #define ST_MAGN_FS_AVL_8000MG 8000
43 #define ST_MAGN_FS_AVL_8100MG 8100
44 #define ST_MAGN_FS_AVL_12000MG 12000
45 #define ST_MAGN_FS_AVL_15000MG 15000
46 #define ST_MAGN_FS_AVL_16000MG 16000
48 /* Special L addresses for Sensor 2 */
49 #define ST_MAGN_2_OUT_X_L_ADDR 0x28
50 #define ST_MAGN_2_OUT_Y_L_ADDR 0x2a
51 #define ST_MAGN_2_OUT_Z_L_ADDR 0x2c
53 /* Special L addresses for sensor 3 */
54 #define ST_MAGN_3_OUT_X_L_ADDR 0x68
55 #define ST_MAGN_3_OUT_Y_L_ADDR 0x6a
56 #define ST_MAGN_3_OUT_Z_L_ADDR 0x6c
58 static const struct iio_chan_spec st_magn_16bit_channels
[] = {
59 ST_SENSORS_LSM_CHANNELS(IIO_MAGN
,
60 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
61 ST_SENSORS_SCAN_X
, 1, IIO_MOD_X
, 's', IIO_BE
, 16, 16,
62 ST_MAGN_DEFAULT_OUT_X_H_ADDR
),
63 ST_SENSORS_LSM_CHANNELS(IIO_MAGN
,
64 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
65 ST_SENSORS_SCAN_Y
, 1, IIO_MOD_Y
, 's', IIO_BE
, 16, 16,
66 ST_MAGN_DEFAULT_OUT_Y_H_ADDR
),
67 ST_SENSORS_LSM_CHANNELS(IIO_MAGN
,
68 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
69 ST_SENSORS_SCAN_Z
, 1, IIO_MOD_Z
, 's', IIO_BE
, 16, 16,
70 ST_MAGN_DEFAULT_OUT_Z_H_ADDR
),
71 IIO_CHAN_SOFT_TIMESTAMP(3)
74 static const struct iio_chan_spec st_magn_2_16bit_channels
[] = {
75 ST_SENSORS_LSM_CHANNELS(IIO_MAGN
,
76 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
77 ST_SENSORS_SCAN_X
, 1, IIO_MOD_X
, 's', IIO_LE
, 16, 16,
78 ST_MAGN_2_OUT_X_L_ADDR
),
79 ST_SENSORS_LSM_CHANNELS(IIO_MAGN
,
80 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
81 ST_SENSORS_SCAN_Y
, 1, IIO_MOD_Y
, 's', IIO_LE
, 16, 16,
82 ST_MAGN_2_OUT_Y_L_ADDR
),
83 ST_SENSORS_LSM_CHANNELS(IIO_MAGN
,
84 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
85 ST_SENSORS_SCAN_Z
, 1, IIO_MOD_Z
, 's', IIO_LE
, 16, 16,
86 ST_MAGN_2_OUT_Z_L_ADDR
),
87 IIO_CHAN_SOFT_TIMESTAMP(3)
90 static const struct iio_chan_spec st_magn_3_16bit_channels
[] = {
91 ST_SENSORS_LSM_CHANNELS(IIO_MAGN
,
92 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
93 ST_SENSORS_SCAN_X
, 1, IIO_MOD_X
, 's', IIO_LE
, 16, 16,
94 ST_MAGN_3_OUT_X_L_ADDR
),
95 ST_SENSORS_LSM_CHANNELS(IIO_MAGN
,
96 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
97 ST_SENSORS_SCAN_Y
, 1, IIO_MOD_Y
, 's', IIO_LE
, 16, 16,
98 ST_MAGN_3_OUT_Y_L_ADDR
),
99 ST_SENSORS_LSM_CHANNELS(IIO_MAGN
,
100 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
101 ST_SENSORS_SCAN_Z
, 1, IIO_MOD_Z
, 's', IIO_LE
, 16, 16,
102 ST_MAGN_3_OUT_Z_L_ADDR
),
103 IIO_CHAN_SOFT_TIMESTAMP(3)
106 static const struct st_sensor_settings st_magn_sensors_settings
[] = {
108 .wai
= 0, /* This sensor has no valid WhoAmI report 0 */
109 .wai_addr
= ST_SENSORS_DEFAULT_WAI_ADDRESS
,
110 .sensors_supported
= {
111 [0] = LSM303DLH_MAGN_DEV_NAME
,
113 .ch
= (struct iio_chan_spec
*)st_magn_16bit_channels
,
118 { .hz
= 1, .value
= 0x00 },
119 { .hz
= 2, .value
= 0x01 },
120 { .hz
= 3, .value
= 0x02 },
121 { .hz
= 8, .value
= 0x03 },
122 { .hz
= 15, .value
= 0x04 },
123 { .hz
= 30, .value
= 0x05 },
124 { .hz
= 75, .value
= 0x06 },
125 /* 220 Hz, 0x07 reportedly exist */
139 .num
= ST_MAGN_FS_AVL_1300MG
,
145 .num
= ST_MAGN_FS_AVL_1900MG
,
151 .num
= ST_MAGN_FS_AVL_2500MG
,
157 .num
= ST_MAGN_FS_AVL_4000MG
,
163 .num
= ST_MAGN_FS_AVL_4700MG
,
169 .num
= ST_MAGN_FS_AVL_5600MG
,
175 .num
= ST_MAGN_FS_AVL_8100MG
,
182 .multi_read_bit
= false,
187 .wai_addr
= ST_SENSORS_DEFAULT_WAI_ADDRESS
,
188 .sensors_supported
= {
189 [0] = LSM303DLHC_MAGN_DEV_NAME
,
190 [1] = LSM303DLM_MAGN_DEV_NAME
,
192 .ch
= (struct iio_chan_spec
*)st_magn_16bit_channels
,
197 { .hz
= 1, .value
= 0x00 },
198 { .hz
= 2, .value
= 0x01 },
199 { .hz
= 3, .value
= 0x02 },
200 { .hz
= 8, .value
= 0x03 },
201 { .hz
= 15, .value
= 0x04 },
202 { .hz
= 30, .value
= 0x05 },
203 { .hz
= 75, .value
= 0x06 },
204 { .hz
= 220, .value
= 0x07 },
218 .num
= ST_MAGN_FS_AVL_1300MG
,
224 .num
= ST_MAGN_FS_AVL_1900MG
,
230 .num
= ST_MAGN_FS_AVL_2500MG
,
236 .num
= ST_MAGN_FS_AVL_4000MG
,
242 .num
= ST_MAGN_FS_AVL_4700MG
,
248 .num
= ST_MAGN_FS_AVL_5600MG
,
254 .num
= ST_MAGN_FS_AVL_8100MG
,
261 .multi_read_bit
= false,
266 .wai_addr
= ST_SENSORS_DEFAULT_WAI_ADDRESS
,
267 .sensors_supported
= {
268 [0] = LIS3MDL_MAGN_DEV_NAME
,
269 [1] = LSM9DS1_MAGN_DEV_NAME
,
271 .ch
= (struct iio_chan_spec
*)st_magn_2_16bit_channels
,
276 { .hz
= 1, .value
= 0x00 },
277 { .hz
= 2, .value
= 0x01 },
278 { .hz
= 3, .value
= 0x02 },
279 { .hz
= 5, .value
= 0x03 },
280 { .hz
= 10, .value
= 0x04 },
281 { .hz
= 20, .value
= 0x05 },
282 { .hz
= 40, .value
= 0x06 },
283 { .hz
= 80, .value
= 0x07 },
297 .num
= ST_MAGN_FS_AVL_4000MG
,
302 .num
= ST_MAGN_FS_AVL_8000MG
,
307 .num
= ST_MAGN_FS_AVL_12000MG
,
312 .num
= ST_MAGN_FS_AVL_16000MG
,
323 /* drdy line is routed drdy pin */
325 .addr
= ST_SENSORS_DEFAULT_STAT_ADDR
,
333 .multi_read_bit
= true,
339 .sensors_supported
= {
340 [0] = LSM303AGR_MAGN_DEV_NAME
,
341 [1] = LIS2MDL_MAGN_DEV_NAME
,
343 .ch
= (struct iio_chan_spec
*)st_magn_3_16bit_channels
,
348 { .hz
= 10, .value
= 0x00 },
349 { .hz
= 20, .value
= 0x01 },
350 { .hz
= 50, .value
= 0x02 },
351 { .hz
= 100, .value
= 0x03 },
363 .num
= ST_MAGN_FS_AVL_15000MG
,
382 .multi_read_bit
= false,
387 static int st_magn_read_raw(struct iio_dev
*indio_dev
,
388 struct iio_chan_spec
const *ch
, int *val
,
389 int *val2
, long mask
)
392 struct st_sensor_data
*mdata
= iio_priv(indio_dev
);
395 case IIO_CHAN_INFO_RAW
:
396 err
= st_sensors_read_info_raw(indio_dev
, ch
, val
);
401 case IIO_CHAN_INFO_SCALE
:
403 if ((ch
->scan_index
== ST_SENSORS_SCAN_Z
) &&
404 (mdata
->current_fullscale
->gain2
!= 0))
405 *val2
= mdata
->current_fullscale
->gain2
;
407 *val2
= mdata
->current_fullscale
->gain
;
408 return IIO_VAL_INT_PLUS_MICRO
;
409 case IIO_CHAN_INFO_SAMP_FREQ
:
420 static int st_magn_write_raw(struct iio_dev
*indio_dev
,
421 struct iio_chan_spec
const *chan
, int val
, int val2
, long mask
)
426 case IIO_CHAN_INFO_SCALE
:
427 err
= st_sensors_set_fullscale_by_gain(indio_dev
, val2
);
429 case IIO_CHAN_INFO_SAMP_FREQ
:
432 mutex_lock(&indio_dev
->mlock
);
433 err
= st_sensors_set_odr(indio_dev
, val
);
434 mutex_unlock(&indio_dev
->mlock
);
443 static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL();
444 static ST_SENSORS_DEV_ATTR_SCALE_AVAIL(in_magn_scale_available
);
446 static struct attribute
*st_magn_attributes
[] = {
447 &iio_dev_attr_sampling_frequency_available
.dev_attr
.attr
,
448 &iio_dev_attr_in_magn_scale_available
.dev_attr
.attr
,
452 static const struct attribute_group st_magn_attribute_group
= {
453 .attrs
= st_magn_attributes
,
456 static const struct iio_info magn_info
= {
457 .attrs
= &st_magn_attribute_group
,
458 .read_raw
= &st_magn_read_raw
,
459 .write_raw
= &st_magn_write_raw
,
460 .debugfs_reg_access
= &st_sensors_debugfs_reg_access
,
463 #ifdef CONFIG_IIO_TRIGGER
464 static const struct iio_trigger_ops st_magn_trigger_ops
= {
465 .set_trigger_state
= ST_MAGN_TRIGGER_SET_STATE
,
466 .validate_device
= st_sensors_validate_device
,
468 #define ST_MAGN_TRIGGER_OPS (&st_magn_trigger_ops)
470 #define ST_MAGN_TRIGGER_OPS NULL
473 int st_magn_common_probe(struct iio_dev
*indio_dev
)
475 struct st_sensor_data
*mdata
= iio_priv(indio_dev
);
476 int irq
= mdata
->get_irq_data_ready(indio_dev
);
479 indio_dev
->modes
= INDIO_DIRECT_MODE
;
480 indio_dev
->info
= &magn_info
;
481 mutex_init(&mdata
->tb
.buf_lock
);
483 err
= st_sensors_power_enable(indio_dev
);
487 err
= st_sensors_check_device_support(indio_dev
,
488 ARRAY_SIZE(st_magn_sensors_settings
),
489 st_magn_sensors_settings
);
491 goto st_magn_power_off
;
493 mdata
->num_data_channels
= ST_MAGN_NUMBER_DATA_CHANNELS
;
494 mdata
->multiread_bit
= mdata
->sensor_settings
->multi_read_bit
;
495 indio_dev
->channels
= mdata
->sensor_settings
->ch
;
496 indio_dev
->num_channels
= ST_SENSORS_NUMBER_ALL_CHANNELS
;
498 mdata
->current_fullscale
= (struct st_sensor_fullscale_avl
*)
499 &mdata
->sensor_settings
->fs
.fs_avl
[0];
500 mdata
->odr
= mdata
->sensor_settings
->odr
.odr_avl
[0].hz
;
502 err
= st_sensors_init_sensor(indio_dev
, NULL
);
504 goto st_magn_power_off
;
506 err
= st_magn_allocate_ring(indio_dev
);
508 goto st_magn_power_off
;
511 err
= st_sensors_allocate_trigger(indio_dev
,
512 ST_MAGN_TRIGGER_OPS
);
514 goto st_magn_probe_trigger_error
;
517 err
= iio_device_register(indio_dev
);
519 goto st_magn_device_register_error
;
521 dev_info(&indio_dev
->dev
, "registered magnetometer %s\n",
526 st_magn_device_register_error
:
528 st_sensors_deallocate_trigger(indio_dev
);
529 st_magn_probe_trigger_error
:
530 st_magn_deallocate_ring(indio_dev
);
532 st_sensors_power_disable(indio_dev
);
536 EXPORT_SYMBOL(st_magn_common_probe
);
538 void st_magn_common_remove(struct iio_dev
*indio_dev
)
540 struct st_sensor_data
*mdata
= iio_priv(indio_dev
);
542 st_sensors_power_disable(indio_dev
);
544 iio_device_unregister(indio_dev
);
545 if (mdata
->get_irq_data_ready(indio_dev
) > 0)
546 st_sensors_deallocate_trigger(indio_dev
);
548 st_magn_deallocate_ring(indio_dev
);
550 EXPORT_SYMBOL(st_magn_common_remove
);
552 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
553 MODULE_DESCRIPTION("STMicroelectronics magnetometers driver");
554 MODULE_LICENSE("GPL v2");