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/mutex.h>
13 #include <linux/sysfs.h>
14 #include <linux/iio/iio.h>
15 #include <linux/iio/sysfs.h>
16 #include <linux/iio/trigger.h>
18 #include <linux/iio/common/st_sensors.h>
21 #define ST_MAGN_NUMBER_DATA_CHANNELS 3
23 /* DEFAULT VALUE FOR SENSORS */
24 #define ST_MAGN_DEFAULT_OUT_X_H_ADDR 0x03
25 #define ST_MAGN_DEFAULT_OUT_Y_H_ADDR 0x07
26 #define ST_MAGN_DEFAULT_OUT_Z_H_ADDR 0x05
29 #define ST_MAGN_FS_AVL_1300MG 1300
30 #define ST_MAGN_FS_AVL_1900MG 1900
31 #define ST_MAGN_FS_AVL_2000MG 2000
32 #define ST_MAGN_FS_AVL_2500MG 2500
33 #define ST_MAGN_FS_AVL_4000MG 4000
34 #define ST_MAGN_FS_AVL_4700MG 4700
35 #define ST_MAGN_FS_AVL_5600MG 5600
36 #define ST_MAGN_FS_AVL_8000MG 8000
37 #define ST_MAGN_FS_AVL_8100MG 8100
38 #define ST_MAGN_FS_AVL_12000MG 12000
39 #define ST_MAGN_FS_AVL_15000MG 15000
40 #define ST_MAGN_FS_AVL_16000MG 16000
42 /* Special L addresses for Sensor 2 */
43 #define ST_MAGN_2_OUT_X_L_ADDR 0x28
44 #define ST_MAGN_2_OUT_Y_L_ADDR 0x2a
45 #define ST_MAGN_2_OUT_Z_L_ADDR 0x2c
47 /* Special L addresses for sensor 3 */
48 #define ST_MAGN_3_OUT_X_L_ADDR 0x68
49 #define ST_MAGN_3_OUT_Y_L_ADDR 0x6a
50 #define ST_MAGN_3_OUT_Z_L_ADDR 0x6c
52 /* Special L addresses for sensor 4 */
53 #define ST_MAGN_4_OUT_X_L_ADDR 0x08
54 #define ST_MAGN_4_OUT_Y_L_ADDR 0x0a
55 #define ST_MAGN_4_OUT_Z_L_ADDR 0x0c
57 static const struct iio_mount_matrix
*
58 st_magn_get_mount_matrix(const struct iio_dev
*indio_dev
,
59 const struct iio_chan_spec
*chan
)
61 struct st_sensor_data
*mdata
= iio_priv(indio_dev
);
63 return &mdata
->mount_matrix
;
66 static const struct iio_chan_spec_ext_info st_magn_mount_matrix_ext_info
[] = {
67 IIO_MOUNT_MATRIX(IIO_SHARED_BY_ALL
, st_magn_get_mount_matrix
),
71 static const struct iio_chan_spec st_magn_16bit_channels
[] = {
72 ST_SENSORS_LSM_CHANNELS_EXT(IIO_MAGN
,
73 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
74 ST_SENSORS_SCAN_X
, 1, IIO_MOD_X
, 's', IIO_BE
, 16, 16,
75 ST_MAGN_DEFAULT_OUT_X_H_ADDR
,
76 st_magn_mount_matrix_ext_info
),
77 ST_SENSORS_LSM_CHANNELS_EXT(IIO_MAGN
,
78 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
79 ST_SENSORS_SCAN_Y
, 1, IIO_MOD_Y
, 's', IIO_BE
, 16, 16,
80 ST_MAGN_DEFAULT_OUT_Y_H_ADDR
,
81 st_magn_mount_matrix_ext_info
),
82 ST_SENSORS_LSM_CHANNELS_EXT(IIO_MAGN
,
83 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
84 ST_SENSORS_SCAN_Z
, 1, IIO_MOD_Z
, 's', IIO_BE
, 16, 16,
85 ST_MAGN_DEFAULT_OUT_Z_H_ADDR
,
86 st_magn_mount_matrix_ext_info
),
87 IIO_CHAN_SOFT_TIMESTAMP(3)
90 static const struct iio_chan_spec st_magn_2_16bit_channels
[] = {
91 ST_SENSORS_LSM_CHANNELS_EXT(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_2_OUT_X_L_ADDR
,
95 st_magn_mount_matrix_ext_info
),
96 ST_SENSORS_LSM_CHANNELS_EXT(IIO_MAGN
,
97 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
98 ST_SENSORS_SCAN_Y
, 1, IIO_MOD_Y
, 's', IIO_LE
, 16, 16,
99 ST_MAGN_2_OUT_Y_L_ADDR
,
100 st_magn_mount_matrix_ext_info
),
101 ST_SENSORS_LSM_CHANNELS_EXT(IIO_MAGN
,
102 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
103 ST_SENSORS_SCAN_Z
, 1, IIO_MOD_Z
, 's', IIO_LE
, 16, 16,
104 ST_MAGN_2_OUT_Z_L_ADDR
,
105 st_magn_mount_matrix_ext_info
),
106 IIO_CHAN_SOFT_TIMESTAMP(3)
109 static const struct iio_chan_spec st_magn_3_16bit_channels
[] = {
110 ST_SENSORS_LSM_CHANNELS_EXT(IIO_MAGN
,
111 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
112 ST_SENSORS_SCAN_X
, 1, IIO_MOD_X
, 's', IIO_LE
, 16, 16,
113 ST_MAGN_3_OUT_X_L_ADDR
,
114 st_magn_mount_matrix_ext_info
),
115 ST_SENSORS_LSM_CHANNELS_EXT(IIO_MAGN
,
116 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
117 ST_SENSORS_SCAN_Y
, 1, IIO_MOD_Y
, 's', IIO_LE
, 16, 16,
118 ST_MAGN_3_OUT_Y_L_ADDR
,
119 st_magn_mount_matrix_ext_info
),
120 ST_SENSORS_LSM_CHANNELS_EXT(IIO_MAGN
,
121 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
122 ST_SENSORS_SCAN_Z
, 1, IIO_MOD_Z
, 's', IIO_LE
, 16, 16,
123 ST_MAGN_3_OUT_Z_L_ADDR
,
124 st_magn_mount_matrix_ext_info
),
125 IIO_CHAN_SOFT_TIMESTAMP(3)
128 static const struct iio_chan_spec st_magn_4_16bit_channels
[] = {
129 ST_SENSORS_LSM_CHANNELS(IIO_MAGN
,
130 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
131 ST_SENSORS_SCAN_X
, 1, IIO_MOD_X
, 's', IIO_LE
, 16, 16,
132 ST_MAGN_4_OUT_X_L_ADDR
),
133 ST_SENSORS_LSM_CHANNELS(IIO_MAGN
,
134 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
135 ST_SENSORS_SCAN_Y
, 1, IIO_MOD_Y
, 's', IIO_LE
, 16, 16,
136 ST_MAGN_4_OUT_Y_L_ADDR
),
137 ST_SENSORS_LSM_CHANNELS(IIO_MAGN
,
138 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
139 ST_SENSORS_SCAN_Z
, 1, IIO_MOD_Z
, 's', IIO_LE
, 16, 16,
140 ST_MAGN_4_OUT_Z_L_ADDR
),
141 IIO_CHAN_SOFT_TIMESTAMP(3)
144 static const struct st_sensor_settings st_magn_sensors_settings
[] = {
146 .wai
= 0, /* This sensor has no valid WhoAmI report 0 */
147 .wai_addr
= ST_SENSORS_DEFAULT_WAI_ADDRESS
,
148 .sensors_supported
= {
149 [0] = LSM303DLH_MAGN_DEV_NAME
,
151 .ch
= (struct iio_chan_spec
*)st_magn_16bit_channels
,
156 { .hz
= 1, .value
= 0x00 },
157 { .hz
= 2, .value
= 0x01 },
158 { .hz
= 3, .value
= 0x02 },
159 { .hz
= 8, .value
= 0x03 },
160 { .hz
= 15, .value
= 0x04 },
161 { .hz
= 30, .value
= 0x05 },
162 { .hz
= 75, .value
= 0x06 },
163 /* 220 Hz, 0x07 reportedly exist */
177 .num
= ST_MAGN_FS_AVL_1300MG
,
183 .num
= ST_MAGN_FS_AVL_1900MG
,
189 .num
= ST_MAGN_FS_AVL_2500MG
,
195 .num
= ST_MAGN_FS_AVL_4000MG
,
201 .num
= ST_MAGN_FS_AVL_4700MG
,
207 .num
= ST_MAGN_FS_AVL_5600MG
,
213 .num
= ST_MAGN_FS_AVL_8100MG
,
220 .multi_read_bit
= false,
225 .wai_addr
= ST_SENSORS_DEFAULT_WAI_ADDRESS
,
226 .sensors_supported
= {
227 [0] = LSM303DLHC_MAGN_DEV_NAME
,
228 [1] = LSM303DLM_MAGN_DEV_NAME
,
230 .ch
= (struct iio_chan_spec
*)st_magn_16bit_channels
,
235 { .hz
= 1, .value
= 0x00 },
236 { .hz
= 2, .value
= 0x01 },
237 { .hz
= 3, .value
= 0x02 },
238 { .hz
= 8, .value
= 0x03 },
239 { .hz
= 15, .value
= 0x04 },
240 { .hz
= 30, .value
= 0x05 },
241 { .hz
= 75, .value
= 0x06 },
242 { .hz
= 220, .value
= 0x07 },
256 .num
= ST_MAGN_FS_AVL_1300MG
,
262 .num
= ST_MAGN_FS_AVL_1900MG
,
268 .num
= ST_MAGN_FS_AVL_2500MG
,
274 .num
= ST_MAGN_FS_AVL_4000MG
,
280 .num
= ST_MAGN_FS_AVL_4700MG
,
286 .num
= ST_MAGN_FS_AVL_5600MG
,
292 .num
= ST_MAGN_FS_AVL_8100MG
,
299 .multi_read_bit
= false,
304 .wai_addr
= ST_SENSORS_DEFAULT_WAI_ADDRESS
,
305 .sensors_supported
= {
306 [0] = LIS3MDL_MAGN_DEV_NAME
,
307 [1] = LSM9DS1_MAGN_DEV_NAME
,
308 [2] = LSM303C_MAGN_DEV_NAME
,
310 .ch
= (struct iio_chan_spec
*)st_magn_2_16bit_channels
,
315 { .hz
= 1, .value
= 0x00 },
316 { .hz
= 2, .value
= 0x01 },
317 { .hz
= 3, .value
= 0x02 },
318 { .hz
= 5, .value
= 0x03 },
319 { .hz
= 10, .value
= 0x04 },
320 { .hz
= 20, .value
= 0x05 },
321 { .hz
= 40, .value
= 0x06 },
322 { .hz
= 80, .value
= 0x07 },
336 .num
= ST_MAGN_FS_AVL_4000MG
,
341 .num
= ST_MAGN_FS_AVL_8000MG
,
346 .num
= ST_MAGN_FS_AVL_12000MG
,
351 .num
= ST_MAGN_FS_AVL_16000MG
,
362 /* drdy line is routed drdy pin */
364 .addr
= ST_SENSORS_DEFAULT_STAT_ADDR
,
372 .multi_read_bit
= true,
378 .sensors_supported
= {
379 [0] = LSM303AGR_MAGN_DEV_NAME
,
380 [1] = LIS2MDL_MAGN_DEV_NAME
,
381 [2] = IIS2MDC_MAGN_DEV_NAME
,
383 .ch
= (struct iio_chan_spec
*)st_magn_3_16bit_channels
,
388 { .hz
= 10, .value
= 0x00 },
389 { .hz
= 20, .value
= 0x01 },
390 { .hz
= 50, .value
= 0x02 },
391 { .hz
= 100, .value
= 0x03 },
403 .num
= ST_MAGN_FS_AVL_15000MG
,
422 .multi_read_bit
= false,
427 .wai_addr
= ST_SENSORS_DEFAULT_WAI_ADDRESS
,
428 .sensors_supported
= {
429 [0] = LSM9DS0_IMU_DEV_NAME
,
430 [1] = LSM303D_IMU_DEV_NAME
,
432 .ch
= (struct iio_chan_spec
*)st_magn_4_16bit_channels
,
435 .mask
= GENMASK(4, 2),
447 .mask
= GENMASK(1, 0),
453 .mask
= GENMASK(6, 5),
456 .num
= ST_MAGN_FS_AVL_2000MG
,
461 .num
= ST_MAGN_FS_AVL_4000MG
,
466 .num
= ST_MAGN_FS_AVL_8000MG
,
471 .num
= ST_MAGN_FS_AVL_12000MG
,
492 .mask
= GENMASK(2, 0),
499 .multi_read_bit
= true,
504 /* Default magn DRDY is available on INT2 pin */
505 static const struct st_sensors_platform_data default_magn_pdata
= {
509 static int st_magn_read_raw(struct iio_dev
*indio_dev
,
510 struct iio_chan_spec
const *ch
, int *val
,
511 int *val2
, long mask
)
514 struct st_sensor_data
*mdata
= iio_priv(indio_dev
);
517 case IIO_CHAN_INFO_RAW
:
518 err
= st_sensors_read_info_raw(indio_dev
, ch
, val
);
523 case IIO_CHAN_INFO_SCALE
:
525 if ((ch
->scan_index
== ST_SENSORS_SCAN_Z
) &&
526 (mdata
->current_fullscale
->gain2
!= 0))
527 *val2
= mdata
->current_fullscale
->gain2
;
529 *val2
= mdata
->current_fullscale
->gain
;
530 return IIO_VAL_INT_PLUS_MICRO
;
531 case IIO_CHAN_INFO_SAMP_FREQ
:
542 static int st_magn_write_raw(struct iio_dev
*indio_dev
,
543 struct iio_chan_spec
const *chan
, int val
, int val2
, long mask
)
546 case IIO_CHAN_INFO_SCALE
:
547 return st_sensors_set_fullscale_by_gain(indio_dev
, val2
);
548 case IIO_CHAN_INFO_SAMP_FREQ
:
552 return st_sensors_set_odr(indio_dev
, val
);
558 static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL();
559 static ST_SENSORS_DEV_ATTR_SCALE_AVAIL(in_magn_scale_available
);
561 static struct attribute
*st_magn_attributes
[] = {
562 &iio_dev_attr_sampling_frequency_available
.dev_attr
.attr
,
563 &iio_dev_attr_in_magn_scale_available
.dev_attr
.attr
,
567 static const struct attribute_group st_magn_attribute_group
= {
568 .attrs
= st_magn_attributes
,
571 static const struct iio_info magn_info
= {
572 .attrs
= &st_magn_attribute_group
,
573 .read_raw
= &st_magn_read_raw
,
574 .write_raw
= &st_magn_write_raw
,
575 .debugfs_reg_access
= &st_sensors_debugfs_reg_access
,
578 #ifdef CONFIG_IIO_TRIGGER
579 static const struct iio_trigger_ops st_magn_trigger_ops
= {
580 .set_trigger_state
= ST_MAGN_TRIGGER_SET_STATE
,
581 .validate_device
= st_sensors_validate_device
,
583 #define ST_MAGN_TRIGGER_OPS (&st_magn_trigger_ops)
585 #define ST_MAGN_TRIGGER_OPS NULL
589 * st_magn_get_settings() - get sensor settings from device name
590 * @name: device name buffer reference.
592 * Return: valid reference on success, NULL otherwise.
594 const struct st_sensor_settings
*st_magn_get_settings(const char *name
)
596 int index
= st_sensors_get_settings_index(name
,
597 st_magn_sensors_settings
,
598 ARRAY_SIZE(st_magn_sensors_settings
));
602 return &st_magn_sensors_settings
[index
];
604 EXPORT_SYMBOL_NS(st_magn_get_settings
, "IIO_ST_SENSORS");
606 int st_magn_common_probe(struct iio_dev
*indio_dev
)
608 struct st_sensor_data
*mdata
= iio_priv(indio_dev
);
609 struct device
*parent
= indio_dev
->dev
.parent
;
610 struct st_sensors_platform_data
*pdata
= dev_get_platdata(parent
);
613 indio_dev
->modes
= INDIO_DIRECT_MODE
;
614 indio_dev
->info
= &magn_info
;
616 err
= st_sensors_verify_id(indio_dev
);
620 mdata
->num_data_channels
= ST_MAGN_NUMBER_DATA_CHANNELS
;
621 indio_dev
->channels
= mdata
->sensor_settings
->ch
;
622 indio_dev
->num_channels
= ST_SENSORS_NUMBER_ALL_CHANNELS
;
624 err
= iio_read_mount_matrix(parent
, &mdata
->mount_matrix
);
628 mdata
->current_fullscale
= &mdata
->sensor_settings
->fs
.fs_avl
[0];
629 mdata
->odr
= mdata
->sensor_settings
->odr
.odr_avl
[0].hz
;
632 pdata
= (struct st_sensors_platform_data
*)&default_magn_pdata
;
634 err
= st_sensors_init_sensor(indio_dev
, pdata
);
638 err
= st_magn_allocate_ring(indio_dev
);
642 if (mdata
->irq
> 0) {
643 err
= st_sensors_allocate_trigger(indio_dev
,
644 ST_MAGN_TRIGGER_OPS
);
649 return devm_iio_device_register(parent
, indio_dev
);
651 EXPORT_SYMBOL_NS(st_magn_common_probe
, "IIO_ST_SENSORS");
653 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
654 MODULE_DESCRIPTION("STMicroelectronics magnetometers driver");
655 MODULE_LICENSE("GPL v2");
656 MODULE_IMPORT_NS("IIO_ST_SENSORS");