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
,
318 .multi_read_bit
= false,
324 .sensors_supported
= {
325 [0] = LSM303AGR_MAGN_DEV_NAME
,
327 .ch
= (struct iio_chan_spec
*)st_magn_3_16bit_channels
,
332 { .hz
= 10, .value
= 0x00 },
333 { .hz
= 20, .value
= 0x01 },
334 { .hz
= 50, .value
= 0x02 },
335 { .hz
= 100, .value
= 0x03 },
347 .num
= ST_MAGN_FS_AVL_15000MG
,
361 .addr_stat_drdy
= ST_SENSORS_DEFAULT_STAT_ADDR
,
363 .multi_read_bit
= false,
368 static int st_magn_read_raw(struct iio_dev
*indio_dev
,
369 struct iio_chan_spec
const *ch
, int *val
,
370 int *val2
, long mask
)
373 struct st_sensor_data
*mdata
= iio_priv(indio_dev
);
376 case IIO_CHAN_INFO_RAW
:
377 err
= st_sensors_read_info_raw(indio_dev
, ch
, val
);
382 case IIO_CHAN_INFO_SCALE
:
384 if ((ch
->scan_index
== ST_SENSORS_SCAN_Z
) &&
385 (mdata
->current_fullscale
->gain2
!= 0))
386 *val2
= mdata
->current_fullscale
->gain2
;
388 *val2
= mdata
->current_fullscale
->gain
;
389 return IIO_VAL_INT_PLUS_MICRO
;
390 case IIO_CHAN_INFO_SAMP_FREQ
:
401 static int st_magn_write_raw(struct iio_dev
*indio_dev
,
402 struct iio_chan_spec
const *chan
, int val
, int val2
, long mask
)
407 case IIO_CHAN_INFO_SCALE
:
408 err
= st_sensors_set_fullscale_by_gain(indio_dev
, val2
);
410 case IIO_CHAN_INFO_SAMP_FREQ
:
413 mutex_lock(&indio_dev
->mlock
);
414 err
= st_sensors_set_odr(indio_dev
, val
);
415 mutex_unlock(&indio_dev
->mlock
);
424 static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL();
425 static ST_SENSORS_DEV_ATTR_SCALE_AVAIL(in_magn_scale_available
);
427 static struct attribute
*st_magn_attributes
[] = {
428 &iio_dev_attr_sampling_frequency_available
.dev_attr
.attr
,
429 &iio_dev_attr_in_magn_scale_available
.dev_attr
.attr
,
433 static const struct attribute_group st_magn_attribute_group
= {
434 .attrs
= st_magn_attributes
,
437 static const struct iio_info magn_info
= {
438 .driver_module
= THIS_MODULE
,
439 .attrs
= &st_magn_attribute_group
,
440 .read_raw
= &st_magn_read_raw
,
441 .write_raw
= &st_magn_write_raw
,
442 .debugfs_reg_access
= &st_sensors_debugfs_reg_access
,
445 #ifdef CONFIG_IIO_TRIGGER
446 static const struct iio_trigger_ops st_magn_trigger_ops
= {
447 .owner
= THIS_MODULE
,
448 .set_trigger_state
= ST_MAGN_TRIGGER_SET_STATE
,
449 .validate_device
= st_sensors_validate_device
,
451 #define ST_MAGN_TRIGGER_OPS (&st_magn_trigger_ops)
453 #define ST_MAGN_TRIGGER_OPS NULL
456 int st_magn_common_probe(struct iio_dev
*indio_dev
)
458 struct st_sensor_data
*mdata
= iio_priv(indio_dev
);
459 int irq
= mdata
->get_irq_data_ready(indio_dev
);
462 indio_dev
->modes
= INDIO_DIRECT_MODE
;
463 indio_dev
->info
= &magn_info
;
464 mutex_init(&mdata
->tb
.buf_lock
);
466 err
= st_sensors_power_enable(indio_dev
);
470 err
= st_sensors_check_device_support(indio_dev
,
471 ARRAY_SIZE(st_magn_sensors_settings
),
472 st_magn_sensors_settings
);
474 goto st_magn_power_off
;
476 mdata
->num_data_channels
= ST_MAGN_NUMBER_DATA_CHANNELS
;
477 mdata
->multiread_bit
= mdata
->sensor_settings
->multi_read_bit
;
478 indio_dev
->channels
= mdata
->sensor_settings
->ch
;
479 indio_dev
->num_channels
= ST_SENSORS_NUMBER_ALL_CHANNELS
;
481 mdata
->current_fullscale
= (struct st_sensor_fullscale_avl
*)
482 &mdata
->sensor_settings
->fs
.fs_avl
[0];
483 mdata
->odr
= mdata
->sensor_settings
->odr
.odr_avl
[0].hz
;
485 err
= st_sensors_init_sensor(indio_dev
, NULL
);
487 goto st_magn_power_off
;
489 err
= st_magn_allocate_ring(indio_dev
);
491 goto st_magn_power_off
;
494 err
= st_sensors_allocate_trigger(indio_dev
,
495 ST_MAGN_TRIGGER_OPS
);
497 goto st_magn_probe_trigger_error
;
500 err
= iio_device_register(indio_dev
);
502 goto st_magn_device_register_error
;
504 dev_info(&indio_dev
->dev
, "registered magnetometer %s\n",
509 st_magn_device_register_error
:
511 st_sensors_deallocate_trigger(indio_dev
);
512 st_magn_probe_trigger_error
:
513 st_magn_deallocate_ring(indio_dev
);
515 st_sensors_power_disable(indio_dev
);
519 EXPORT_SYMBOL(st_magn_common_probe
);
521 void st_magn_common_remove(struct iio_dev
*indio_dev
)
523 struct st_sensor_data
*mdata
= iio_priv(indio_dev
);
525 st_sensors_power_disable(indio_dev
);
527 iio_device_unregister(indio_dev
);
528 if (mdata
->get_irq_data_ready(indio_dev
) > 0)
529 st_sensors_deallocate_trigger(indio_dev
);
531 st_magn_deallocate_ring(indio_dev
);
533 EXPORT_SYMBOL(st_magn_common_remove
);
535 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
536 MODULE_DESCRIPTION("STMicroelectronics magnetometers driver");
537 MODULE_LICENSE("GPL v2");