2 * STMicroelectronics magnetometers driver
4 * Copyright 2012-2013 STMicroelectronics Inc.
6 * Denis Ciocca <denis.ciocca@st.com>
8 * Licensed under the GPL-2.
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/errno.h>
15 #include <linux/types.h>
16 #include <linux/mutex.h>
17 #include <linux/interrupt.h>
18 #include <linux/i2c.h>
19 #include <linux/gpio.h>
20 #include <linux/irq.h>
21 #include <linux/delay.h>
22 #include <linux/iio/iio.h>
23 #include <linux/iio/sysfs.h>
24 #include <linux/iio/buffer.h>
26 #include <linux/iio/common/st_sensors.h>
29 #define ST_MAGN_NUMBER_DATA_CHANNELS 3
31 /* DEFAULT VALUE FOR SENSORS */
32 #define ST_MAGN_DEFAULT_OUT_X_H_ADDR 0X03
33 #define ST_MAGN_DEFAULT_OUT_Y_H_ADDR 0X07
34 #define ST_MAGN_DEFAULT_OUT_Z_H_ADDR 0X05
37 #define ST_MAGN_FS_AVL_1300MG 1300
38 #define ST_MAGN_FS_AVL_1900MG 1900
39 #define ST_MAGN_FS_AVL_2500MG 2500
40 #define ST_MAGN_FS_AVL_4000MG 4000
41 #define ST_MAGN_FS_AVL_4700MG 4700
42 #define ST_MAGN_FS_AVL_5600MG 5600
43 #define ST_MAGN_FS_AVL_8000MG 8000
44 #define ST_MAGN_FS_AVL_8100MG 8100
45 #define ST_MAGN_FS_AVL_12000MG 12000
46 #define ST_MAGN_FS_AVL_15000MG 15000
47 #define ST_MAGN_FS_AVL_16000MG 16000
49 /* Special L addresses for Sensor 2 */
50 #define ST_MAGN_2_OUT_X_L_ADDR 0x28
51 #define ST_MAGN_2_OUT_Y_L_ADDR 0x2a
52 #define ST_MAGN_2_OUT_Z_L_ADDR 0x2c
54 /* Special L addresses for sensor 3 */
55 #define ST_MAGN_3_OUT_X_L_ADDR 0x68
56 #define ST_MAGN_3_OUT_Y_L_ADDR 0x6a
57 #define ST_MAGN_3_OUT_Z_L_ADDR 0x6c
59 static const struct iio_chan_spec st_magn_16bit_channels
[] = {
60 ST_SENSORS_LSM_CHANNELS(IIO_MAGN
,
61 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
62 ST_SENSORS_SCAN_X
, 1, IIO_MOD_X
, 's', IIO_BE
, 16, 16,
63 ST_MAGN_DEFAULT_OUT_X_H_ADDR
),
64 ST_SENSORS_LSM_CHANNELS(IIO_MAGN
,
65 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
66 ST_SENSORS_SCAN_Y
, 1, IIO_MOD_Y
, 's', IIO_BE
, 16, 16,
67 ST_MAGN_DEFAULT_OUT_Y_H_ADDR
),
68 ST_SENSORS_LSM_CHANNELS(IIO_MAGN
,
69 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
70 ST_SENSORS_SCAN_Z
, 1, IIO_MOD_Z
, 's', IIO_BE
, 16, 16,
71 ST_MAGN_DEFAULT_OUT_Z_H_ADDR
),
72 IIO_CHAN_SOFT_TIMESTAMP(3)
75 static const struct iio_chan_spec st_magn_2_16bit_channels
[] = {
76 ST_SENSORS_LSM_CHANNELS(IIO_MAGN
,
77 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
78 ST_SENSORS_SCAN_X
, 1, IIO_MOD_X
, 's', IIO_LE
, 16, 16,
79 ST_MAGN_2_OUT_X_L_ADDR
),
80 ST_SENSORS_LSM_CHANNELS(IIO_MAGN
,
81 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
82 ST_SENSORS_SCAN_Y
, 1, IIO_MOD_Y
, 's', IIO_LE
, 16, 16,
83 ST_MAGN_2_OUT_Y_L_ADDR
),
84 ST_SENSORS_LSM_CHANNELS(IIO_MAGN
,
85 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
86 ST_SENSORS_SCAN_Z
, 1, IIO_MOD_Z
, 's', IIO_LE
, 16, 16,
87 ST_MAGN_2_OUT_Z_L_ADDR
),
88 IIO_CHAN_SOFT_TIMESTAMP(3)
91 static const struct iio_chan_spec st_magn_3_16bit_channels
[] = {
92 ST_SENSORS_LSM_CHANNELS(IIO_MAGN
,
93 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
94 ST_SENSORS_SCAN_X
, 1, IIO_MOD_X
, 's', IIO_LE
, 16, 16,
95 ST_MAGN_3_OUT_X_L_ADDR
),
96 ST_SENSORS_LSM_CHANNELS(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_3_OUT_Y_L_ADDR
),
100 ST_SENSORS_LSM_CHANNELS(IIO_MAGN
,
101 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
102 ST_SENSORS_SCAN_Z
, 1, IIO_MOD_Z
, 's', IIO_LE
, 16, 16,
103 ST_MAGN_3_OUT_Z_L_ADDR
),
104 IIO_CHAN_SOFT_TIMESTAMP(3)
107 static const struct st_sensor_settings st_magn_sensors_settings
[] = {
109 .wai
= 0, /* This sensor has no valid WhoAmI report 0 */
110 .wai_addr
= ST_SENSORS_DEFAULT_WAI_ADDRESS
,
111 .sensors_supported
= {
112 [0] = LSM303DLH_MAGN_DEV_NAME
,
114 .ch
= (struct iio_chan_spec
*)st_magn_16bit_channels
,
119 { .hz
= 1, .value
= 0x00 },
120 { .hz
= 2, .value
= 0x01 },
121 { .hz
= 3, .value
= 0x02 },
122 { .hz
= 8, .value
= 0x03 },
123 { .hz
= 15, .value
= 0x04 },
124 { .hz
= 30, .value
= 0x05 },
125 { .hz
= 75, .value
= 0x06 },
126 /* 220 Hz, 0x07 reportedly exist */
140 .num
= ST_MAGN_FS_AVL_1300MG
,
146 .num
= ST_MAGN_FS_AVL_1900MG
,
152 .num
= ST_MAGN_FS_AVL_2500MG
,
158 .num
= ST_MAGN_FS_AVL_4000MG
,
164 .num
= ST_MAGN_FS_AVL_4700MG
,
170 .num
= ST_MAGN_FS_AVL_5600MG
,
176 .num
= ST_MAGN_FS_AVL_8100MG
,
183 .multi_read_bit
= false,
188 .wai_addr
= ST_SENSORS_DEFAULT_WAI_ADDRESS
,
189 .sensors_supported
= {
190 [0] = LSM303DLHC_MAGN_DEV_NAME
,
191 [1] = LSM303DLM_MAGN_DEV_NAME
,
193 .ch
= (struct iio_chan_spec
*)st_magn_16bit_channels
,
198 { .hz
= 1, .value
= 0x00 },
199 { .hz
= 2, .value
= 0x01 },
200 { .hz
= 3, .value
= 0x02 },
201 { .hz
= 8, .value
= 0x03 },
202 { .hz
= 15, .value
= 0x04 },
203 { .hz
= 30, .value
= 0x05 },
204 { .hz
= 75, .value
= 0x06 },
205 { .hz
= 220, .value
= 0x07 },
219 .num
= ST_MAGN_FS_AVL_1300MG
,
225 .num
= ST_MAGN_FS_AVL_1900MG
,
231 .num
= ST_MAGN_FS_AVL_2500MG
,
237 .num
= ST_MAGN_FS_AVL_4000MG
,
243 .num
= ST_MAGN_FS_AVL_4700MG
,
249 .num
= ST_MAGN_FS_AVL_5600MG
,
255 .num
= ST_MAGN_FS_AVL_8100MG
,
262 .multi_read_bit
= false,
267 .wai_addr
= ST_SENSORS_DEFAULT_WAI_ADDRESS
,
268 .sensors_supported
= {
269 [0] = LIS3MDL_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
,
319 /* drdy line is routed drdy pin */
321 .addr
= ST_SENSORS_DEFAULT_STAT_ADDR
,
329 .multi_read_bit
= true,
335 .sensors_supported
= {
336 [0] = LSM303AGR_MAGN_DEV_NAME
,
337 [1] = LIS2MDL_MAGN_DEV_NAME
,
339 .ch
= (struct iio_chan_spec
*)st_magn_3_16bit_channels
,
344 { .hz
= 10, .value
= 0x00 },
345 { .hz
= 20, .value
= 0x01 },
346 { .hz
= 50, .value
= 0x02 },
347 { .hz
= 100, .value
= 0x03 },
359 .num
= ST_MAGN_FS_AVL_15000MG
,
378 .multi_read_bit
= false,
383 static int st_magn_read_raw(struct iio_dev
*indio_dev
,
384 struct iio_chan_spec
const *ch
, int *val
,
385 int *val2
, long mask
)
388 struct st_sensor_data
*mdata
= iio_priv(indio_dev
);
391 case IIO_CHAN_INFO_RAW
:
392 err
= st_sensors_read_info_raw(indio_dev
, ch
, val
);
397 case IIO_CHAN_INFO_SCALE
:
399 if ((ch
->scan_index
== ST_SENSORS_SCAN_Z
) &&
400 (mdata
->current_fullscale
->gain2
!= 0))
401 *val2
= mdata
->current_fullscale
->gain2
;
403 *val2
= mdata
->current_fullscale
->gain
;
404 return IIO_VAL_INT_PLUS_MICRO
;
405 case IIO_CHAN_INFO_SAMP_FREQ
:
416 static int st_magn_write_raw(struct iio_dev
*indio_dev
,
417 struct iio_chan_spec
const *chan
, int val
, int val2
, long mask
)
422 case IIO_CHAN_INFO_SCALE
:
423 err
= st_sensors_set_fullscale_by_gain(indio_dev
, val2
);
425 case IIO_CHAN_INFO_SAMP_FREQ
:
428 mutex_lock(&indio_dev
->mlock
);
429 err
= st_sensors_set_odr(indio_dev
, val
);
430 mutex_unlock(&indio_dev
->mlock
);
439 static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL();
440 static ST_SENSORS_DEV_ATTR_SCALE_AVAIL(in_magn_scale_available
);
442 static struct attribute
*st_magn_attributes
[] = {
443 &iio_dev_attr_sampling_frequency_available
.dev_attr
.attr
,
444 &iio_dev_attr_in_magn_scale_available
.dev_attr
.attr
,
448 static const struct attribute_group st_magn_attribute_group
= {
449 .attrs
= st_magn_attributes
,
452 static const struct iio_info magn_info
= {
453 .attrs
= &st_magn_attribute_group
,
454 .read_raw
= &st_magn_read_raw
,
455 .write_raw
= &st_magn_write_raw
,
456 .debugfs_reg_access
= &st_sensors_debugfs_reg_access
,
459 #ifdef CONFIG_IIO_TRIGGER
460 static const struct iio_trigger_ops st_magn_trigger_ops
= {
461 .set_trigger_state
= ST_MAGN_TRIGGER_SET_STATE
,
462 .validate_device
= st_sensors_validate_device
,
464 #define ST_MAGN_TRIGGER_OPS (&st_magn_trigger_ops)
466 #define ST_MAGN_TRIGGER_OPS NULL
469 int st_magn_common_probe(struct iio_dev
*indio_dev
)
471 struct st_sensor_data
*mdata
= iio_priv(indio_dev
);
472 int irq
= mdata
->get_irq_data_ready(indio_dev
);
475 indio_dev
->modes
= INDIO_DIRECT_MODE
;
476 indio_dev
->info
= &magn_info
;
477 mutex_init(&mdata
->tb
.buf_lock
);
479 err
= st_sensors_power_enable(indio_dev
);
483 err
= st_sensors_check_device_support(indio_dev
,
484 ARRAY_SIZE(st_magn_sensors_settings
),
485 st_magn_sensors_settings
);
487 goto st_magn_power_off
;
489 mdata
->num_data_channels
= ST_MAGN_NUMBER_DATA_CHANNELS
;
490 mdata
->multiread_bit
= mdata
->sensor_settings
->multi_read_bit
;
491 indio_dev
->channels
= mdata
->sensor_settings
->ch
;
492 indio_dev
->num_channels
= ST_SENSORS_NUMBER_ALL_CHANNELS
;
494 mdata
->current_fullscale
= (struct st_sensor_fullscale_avl
*)
495 &mdata
->sensor_settings
->fs
.fs_avl
[0];
496 mdata
->odr
= mdata
->sensor_settings
->odr
.odr_avl
[0].hz
;
498 err
= st_sensors_init_sensor(indio_dev
, NULL
);
500 goto st_magn_power_off
;
502 err
= st_magn_allocate_ring(indio_dev
);
504 goto st_magn_power_off
;
507 err
= st_sensors_allocate_trigger(indio_dev
,
508 ST_MAGN_TRIGGER_OPS
);
510 goto st_magn_probe_trigger_error
;
513 err
= iio_device_register(indio_dev
);
515 goto st_magn_device_register_error
;
517 dev_info(&indio_dev
->dev
, "registered magnetometer %s\n",
522 st_magn_device_register_error
:
524 st_sensors_deallocate_trigger(indio_dev
);
525 st_magn_probe_trigger_error
:
526 st_magn_deallocate_ring(indio_dev
);
528 st_sensors_power_disable(indio_dev
);
532 EXPORT_SYMBOL(st_magn_common_probe
);
534 void st_magn_common_remove(struct iio_dev
*indio_dev
)
536 struct st_sensor_data
*mdata
= iio_priv(indio_dev
);
538 st_sensors_power_disable(indio_dev
);
540 iio_device_unregister(indio_dev
);
541 if (mdata
->get_irq_data_ready(indio_dev
) > 0)
542 st_sensors_deallocate_trigger(indio_dev
);
544 st_magn_deallocate_ring(indio_dev
);
546 EXPORT_SYMBOL(st_magn_common_remove
);
548 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
549 MODULE_DESCRIPTION("STMicroelectronics magnetometers driver");
550 MODULE_LICENSE("GPL v2");