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/interrupt.h>
16 #include <linux/i2c.h>
17 #include <linux/irq.h>
18 #include <linux/delay.h>
19 #include <linux/iio/iio.h>
20 #include <linux/iio/sysfs.h>
21 #include <linux/iio/buffer.h>
23 #include <linux/iio/common/st_sensors.h>
26 #define ST_MAGN_NUMBER_DATA_CHANNELS 3
28 /* DEFAULT VALUE FOR SENSORS */
29 #define ST_MAGN_DEFAULT_OUT_X_H_ADDR 0x03
30 #define ST_MAGN_DEFAULT_OUT_Y_H_ADDR 0x07
31 #define ST_MAGN_DEFAULT_OUT_Z_H_ADDR 0x05
34 #define ST_MAGN_FS_AVL_1300MG 1300
35 #define ST_MAGN_FS_AVL_1900MG 1900
36 #define ST_MAGN_FS_AVL_2500MG 2500
37 #define ST_MAGN_FS_AVL_4000MG 4000
38 #define ST_MAGN_FS_AVL_4700MG 4700
39 #define ST_MAGN_FS_AVL_5600MG 5600
40 #define ST_MAGN_FS_AVL_8000MG 8000
41 #define ST_MAGN_FS_AVL_8100MG 8100
42 #define ST_MAGN_FS_AVL_12000MG 12000
43 #define ST_MAGN_FS_AVL_15000MG 15000
44 #define ST_MAGN_FS_AVL_16000MG 16000
46 /* Special L addresses for Sensor 2 */
47 #define ST_MAGN_2_OUT_X_L_ADDR 0x28
48 #define ST_MAGN_2_OUT_Y_L_ADDR 0x2a
49 #define ST_MAGN_2_OUT_Z_L_ADDR 0x2c
51 /* Special L addresses for sensor 3 */
52 #define ST_MAGN_3_OUT_X_L_ADDR 0x68
53 #define ST_MAGN_3_OUT_Y_L_ADDR 0x6a
54 #define ST_MAGN_3_OUT_Z_L_ADDR 0x6c
56 static const struct iio_chan_spec st_magn_16bit_channels
[] = {
57 ST_SENSORS_LSM_CHANNELS(IIO_MAGN
,
58 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
59 ST_SENSORS_SCAN_X
, 1, IIO_MOD_X
, 's', IIO_BE
, 16, 16,
60 ST_MAGN_DEFAULT_OUT_X_H_ADDR
),
61 ST_SENSORS_LSM_CHANNELS(IIO_MAGN
,
62 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
63 ST_SENSORS_SCAN_Y
, 1, IIO_MOD_Y
, 's', IIO_BE
, 16, 16,
64 ST_MAGN_DEFAULT_OUT_Y_H_ADDR
),
65 ST_SENSORS_LSM_CHANNELS(IIO_MAGN
,
66 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
67 ST_SENSORS_SCAN_Z
, 1, IIO_MOD_Z
, 's', IIO_BE
, 16, 16,
68 ST_MAGN_DEFAULT_OUT_Z_H_ADDR
),
69 IIO_CHAN_SOFT_TIMESTAMP(3)
72 static const struct iio_chan_spec st_magn_2_16bit_channels
[] = {
73 ST_SENSORS_LSM_CHANNELS(IIO_MAGN
,
74 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
75 ST_SENSORS_SCAN_X
, 1, IIO_MOD_X
, 's', IIO_LE
, 16, 16,
76 ST_MAGN_2_OUT_X_L_ADDR
),
77 ST_SENSORS_LSM_CHANNELS(IIO_MAGN
,
78 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
79 ST_SENSORS_SCAN_Y
, 1, IIO_MOD_Y
, 's', IIO_LE
, 16, 16,
80 ST_MAGN_2_OUT_Y_L_ADDR
),
81 ST_SENSORS_LSM_CHANNELS(IIO_MAGN
,
82 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
83 ST_SENSORS_SCAN_Z
, 1, IIO_MOD_Z
, 's', IIO_LE
, 16, 16,
84 ST_MAGN_2_OUT_Z_L_ADDR
),
85 IIO_CHAN_SOFT_TIMESTAMP(3)
88 static const struct iio_chan_spec st_magn_3_16bit_channels
[] = {
89 ST_SENSORS_LSM_CHANNELS(IIO_MAGN
,
90 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
91 ST_SENSORS_SCAN_X
, 1, IIO_MOD_X
, 's', IIO_LE
, 16, 16,
92 ST_MAGN_3_OUT_X_L_ADDR
),
93 ST_SENSORS_LSM_CHANNELS(IIO_MAGN
,
94 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
95 ST_SENSORS_SCAN_Y
, 1, IIO_MOD_Y
, 's', IIO_LE
, 16, 16,
96 ST_MAGN_3_OUT_Y_L_ADDR
),
97 ST_SENSORS_LSM_CHANNELS(IIO_MAGN
,
98 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
99 ST_SENSORS_SCAN_Z
, 1, IIO_MOD_Z
, 's', IIO_LE
, 16, 16,
100 ST_MAGN_3_OUT_Z_L_ADDR
),
101 IIO_CHAN_SOFT_TIMESTAMP(3)
104 static const struct st_sensor_settings st_magn_sensors_settings
[] = {
106 .wai
= 0, /* This sensor has no valid WhoAmI report 0 */
107 .wai_addr
= ST_SENSORS_DEFAULT_WAI_ADDRESS
,
108 .sensors_supported
= {
109 [0] = LSM303DLH_MAGN_DEV_NAME
,
111 .ch
= (struct iio_chan_spec
*)st_magn_16bit_channels
,
116 { .hz
= 1, .value
= 0x00 },
117 { .hz
= 2, .value
= 0x01 },
118 { .hz
= 3, .value
= 0x02 },
119 { .hz
= 8, .value
= 0x03 },
120 { .hz
= 15, .value
= 0x04 },
121 { .hz
= 30, .value
= 0x05 },
122 { .hz
= 75, .value
= 0x06 },
123 /* 220 Hz, 0x07 reportedly exist */
137 .num
= ST_MAGN_FS_AVL_1300MG
,
143 .num
= ST_MAGN_FS_AVL_1900MG
,
149 .num
= ST_MAGN_FS_AVL_2500MG
,
155 .num
= ST_MAGN_FS_AVL_4000MG
,
161 .num
= ST_MAGN_FS_AVL_4700MG
,
167 .num
= ST_MAGN_FS_AVL_5600MG
,
173 .num
= ST_MAGN_FS_AVL_8100MG
,
180 .multi_read_bit
= false,
185 .wai_addr
= ST_SENSORS_DEFAULT_WAI_ADDRESS
,
186 .sensors_supported
= {
187 [0] = LSM303DLHC_MAGN_DEV_NAME
,
188 [1] = LSM303DLM_MAGN_DEV_NAME
,
190 .ch
= (struct iio_chan_spec
*)st_magn_16bit_channels
,
195 { .hz
= 1, .value
= 0x00 },
196 { .hz
= 2, .value
= 0x01 },
197 { .hz
= 3, .value
= 0x02 },
198 { .hz
= 8, .value
= 0x03 },
199 { .hz
= 15, .value
= 0x04 },
200 { .hz
= 30, .value
= 0x05 },
201 { .hz
= 75, .value
= 0x06 },
202 { .hz
= 220, .value
= 0x07 },
216 .num
= ST_MAGN_FS_AVL_1300MG
,
222 .num
= ST_MAGN_FS_AVL_1900MG
,
228 .num
= ST_MAGN_FS_AVL_2500MG
,
234 .num
= ST_MAGN_FS_AVL_4000MG
,
240 .num
= ST_MAGN_FS_AVL_4700MG
,
246 .num
= ST_MAGN_FS_AVL_5600MG
,
252 .num
= ST_MAGN_FS_AVL_8100MG
,
259 .multi_read_bit
= false,
264 .wai_addr
= ST_SENSORS_DEFAULT_WAI_ADDRESS
,
265 .sensors_supported
= {
266 [0] = LIS3MDL_MAGN_DEV_NAME
,
267 [1] = LSM9DS1_MAGN_DEV_NAME
,
269 .ch
= (struct iio_chan_spec
*)st_magn_2_16bit_channels
,
274 { .hz
= 1, .value
= 0x00 },
275 { .hz
= 2, .value
= 0x01 },
276 { .hz
= 3, .value
= 0x02 },
277 { .hz
= 5, .value
= 0x03 },
278 { .hz
= 10, .value
= 0x04 },
279 { .hz
= 20, .value
= 0x05 },
280 { .hz
= 40, .value
= 0x06 },
281 { .hz
= 80, .value
= 0x07 },
295 .num
= ST_MAGN_FS_AVL_4000MG
,
300 .num
= ST_MAGN_FS_AVL_8000MG
,
305 .num
= ST_MAGN_FS_AVL_12000MG
,
310 .num
= ST_MAGN_FS_AVL_16000MG
,
321 /* drdy line is routed drdy pin */
323 .addr
= ST_SENSORS_DEFAULT_STAT_ADDR
,
331 .multi_read_bit
= true,
337 .sensors_supported
= {
338 [0] = LSM303AGR_MAGN_DEV_NAME
,
339 [1] = LIS2MDL_MAGN_DEV_NAME
,
341 .ch
= (struct iio_chan_spec
*)st_magn_3_16bit_channels
,
346 { .hz
= 10, .value
= 0x00 },
347 { .hz
= 20, .value
= 0x01 },
348 { .hz
= 50, .value
= 0x02 },
349 { .hz
= 100, .value
= 0x03 },
361 .num
= ST_MAGN_FS_AVL_15000MG
,
380 .multi_read_bit
= false,
385 static int st_magn_read_raw(struct iio_dev
*indio_dev
,
386 struct iio_chan_spec
const *ch
, int *val
,
387 int *val2
, long mask
)
390 struct st_sensor_data
*mdata
= iio_priv(indio_dev
);
393 case IIO_CHAN_INFO_RAW
:
394 err
= st_sensors_read_info_raw(indio_dev
, ch
, val
);
399 case IIO_CHAN_INFO_SCALE
:
401 if ((ch
->scan_index
== ST_SENSORS_SCAN_Z
) &&
402 (mdata
->current_fullscale
->gain2
!= 0))
403 *val2
= mdata
->current_fullscale
->gain2
;
405 *val2
= mdata
->current_fullscale
->gain
;
406 return IIO_VAL_INT_PLUS_MICRO
;
407 case IIO_CHAN_INFO_SAMP_FREQ
:
418 static int st_magn_write_raw(struct iio_dev
*indio_dev
,
419 struct iio_chan_spec
const *chan
, int val
, int val2
, long mask
)
424 case IIO_CHAN_INFO_SCALE
:
425 err
= st_sensors_set_fullscale_by_gain(indio_dev
, val2
);
427 case IIO_CHAN_INFO_SAMP_FREQ
:
430 mutex_lock(&indio_dev
->mlock
);
431 err
= st_sensors_set_odr(indio_dev
, val
);
432 mutex_unlock(&indio_dev
->mlock
);
441 static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL();
442 static ST_SENSORS_DEV_ATTR_SCALE_AVAIL(in_magn_scale_available
);
444 static struct attribute
*st_magn_attributes
[] = {
445 &iio_dev_attr_sampling_frequency_available
.dev_attr
.attr
,
446 &iio_dev_attr_in_magn_scale_available
.dev_attr
.attr
,
450 static const struct attribute_group st_magn_attribute_group
= {
451 .attrs
= st_magn_attributes
,
454 static const struct iio_info magn_info
= {
455 .attrs
= &st_magn_attribute_group
,
456 .read_raw
= &st_magn_read_raw
,
457 .write_raw
= &st_magn_write_raw
,
458 .debugfs_reg_access
= &st_sensors_debugfs_reg_access
,
461 #ifdef CONFIG_IIO_TRIGGER
462 static const struct iio_trigger_ops st_magn_trigger_ops
= {
463 .set_trigger_state
= ST_MAGN_TRIGGER_SET_STATE
,
464 .validate_device
= st_sensors_validate_device
,
466 #define ST_MAGN_TRIGGER_OPS (&st_magn_trigger_ops)
468 #define ST_MAGN_TRIGGER_OPS NULL
472 * st_magn_get_settings() - get sensor settings from device name
473 * @name: device name buffer reference.
475 * Return: valid reference on success, NULL otherwise.
477 const struct st_sensor_settings
*st_magn_get_settings(const char *name
)
479 int index
= st_sensors_get_settings_index(name
,
480 st_magn_sensors_settings
,
481 ARRAY_SIZE(st_magn_sensors_settings
));
485 return &st_magn_sensors_settings
[index
];
487 EXPORT_SYMBOL(st_magn_get_settings
);
489 int st_magn_common_probe(struct iio_dev
*indio_dev
)
491 struct st_sensor_data
*mdata
= iio_priv(indio_dev
);
494 indio_dev
->modes
= INDIO_DIRECT_MODE
;
495 indio_dev
->info
= &magn_info
;
497 err
= st_sensors_power_enable(indio_dev
);
501 err
= st_sensors_verify_id(indio_dev
);
503 goto st_magn_power_off
;
505 mdata
->num_data_channels
= ST_MAGN_NUMBER_DATA_CHANNELS
;
506 indio_dev
->channels
= mdata
->sensor_settings
->ch
;
507 indio_dev
->num_channels
= ST_SENSORS_NUMBER_ALL_CHANNELS
;
509 mdata
->current_fullscale
= &mdata
->sensor_settings
->fs
.fs_avl
[0];
510 mdata
->odr
= mdata
->sensor_settings
->odr
.odr_avl
[0].hz
;
512 err
= st_sensors_init_sensor(indio_dev
, NULL
);
514 goto st_magn_power_off
;
516 err
= st_magn_allocate_ring(indio_dev
);
518 goto st_magn_power_off
;
520 if (mdata
->irq
> 0) {
521 err
= st_sensors_allocate_trigger(indio_dev
,
522 ST_MAGN_TRIGGER_OPS
);
524 goto st_magn_probe_trigger_error
;
527 err
= iio_device_register(indio_dev
);
529 goto st_magn_device_register_error
;
531 dev_info(&indio_dev
->dev
, "registered magnetometer %s\n",
536 st_magn_device_register_error
:
538 st_sensors_deallocate_trigger(indio_dev
);
539 st_magn_probe_trigger_error
:
540 st_magn_deallocate_ring(indio_dev
);
542 st_sensors_power_disable(indio_dev
);
546 EXPORT_SYMBOL(st_magn_common_probe
);
548 void st_magn_common_remove(struct iio_dev
*indio_dev
)
550 struct st_sensor_data
*mdata
= iio_priv(indio_dev
);
552 st_sensors_power_disable(indio_dev
);
554 iio_device_unregister(indio_dev
);
556 st_sensors_deallocate_trigger(indio_dev
);
558 st_magn_deallocate_ring(indio_dev
);
560 EXPORT_SYMBOL(st_magn_common_remove
);
562 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
563 MODULE_DESCRIPTION("STMicroelectronics magnetometers driver");
564 MODULE_LICENSE("GPL v2");