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_16000MG 16000
48 /* CUSTOM VALUES FOR SENSOR 1 */
49 #define ST_MAGN_1_WAI_EXP 0x3c
50 #define ST_MAGN_1_ODR_ADDR 0x00
51 #define ST_MAGN_1_ODR_MASK 0x1c
52 #define ST_MAGN_1_ODR_AVL_1HZ_VAL 0x00
53 #define ST_MAGN_1_ODR_AVL_2HZ_VAL 0x01
54 #define ST_MAGN_1_ODR_AVL_3HZ_VAL 0x02
55 #define ST_MAGN_1_ODR_AVL_8HZ_VAL 0x03
56 #define ST_MAGN_1_ODR_AVL_15HZ_VAL 0x04
57 #define ST_MAGN_1_ODR_AVL_30HZ_VAL 0x05
58 #define ST_MAGN_1_ODR_AVL_75HZ_VAL 0x06
59 #define ST_MAGN_1_ODR_AVL_220HZ_VAL 0x07
60 #define ST_MAGN_1_PW_ADDR 0x02
61 #define ST_MAGN_1_PW_MASK 0x03
62 #define ST_MAGN_1_PW_ON 0x00
63 #define ST_MAGN_1_PW_OFF 0x03
64 #define ST_MAGN_1_FS_ADDR 0x01
65 #define ST_MAGN_1_FS_MASK 0xe0
66 #define ST_MAGN_1_FS_AVL_1300_VAL 0x01
67 #define ST_MAGN_1_FS_AVL_1900_VAL 0x02
68 #define ST_MAGN_1_FS_AVL_2500_VAL 0x03
69 #define ST_MAGN_1_FS_AVL_4000_VAL 0x04
70 #define ST_MAGN_1_FS_AVL_4700_VAL 0x05
71 #define ST_MAGN_1_FS_AVL_5600_VAL 0x06
72 #define ST_MAGN_1_FS_AVL_8100_VAL 0x07
73 #define ST_MAGN_1_FS_AVL_1300_GAIN_XY 909
74 #define ST_MAGN_1_FS_AVL_1900_GAIN_XY 1169
75 #define ST_MAGN_1_FS_AVL_2500_GAIN_XY 1492
76 #define ST_MAGN_1_FS_AVL_4000_GAIN_XY 2222
77 #define ST_MAGN_1_FS_AVL_4700_GAIN_XY 2500
78 #define ST_MAGN_1_FS_AVL_5600_GAIN_XY 3030
79 #define ST_MAGN_1_FS_AVL_8100_GAIN_XY 4347
80 #define ST_MAGN_1_FS_AVL_1300_GAIN_Z 1020
81 #define ST_MAGN_1_FS_AVL_1900_GAIN_Z 1315
82 #define ST_MAGN_1_FS_AVL_2500_GAIN_Z 1666
83 #define ST_MAGN_1_FS_AVL_4000_GAIN_Z 2500
84 #define ST_MAGN_1_FS_AVL_4700_GAIN_Z 2816
85 #define ST_MAGN_1_FS_AVL_5600_GAIN_Z 3389
86 #define ST_MAGN_1_FS_AVL_8100_GAIN_Z 4878
87 #define ST_MAGN_1_MULTIREAD_BIT false
89 /* CUSTOM VALUES FOR SENSOR 2 */
90 #define ST_MAGN_2_WAI_EXP 0x3d
91 #define ST_MAGN_2_ODR_ADDR 0x20
92 #define ST_MAGN_2_ODR_MASK 0x1c
93 #define ST_MAGN_2_ODR_AVL_1HZ_VAL 0x00
94 #define ST_MAGN_2_ODR_AVL_2HZ_VAL 0x01
95 #define ST_MAGN_2_ODR_AVL_3HZ_VAL 0x02
96 #define ST_MAGN_2_ODR_AVL_5HZ_VAL 0x03
97 #define ST_MAGN_2_ODR_AVL_10HZ_VAL 0x04
98 #define ST_MAGN_2_ODR_AVL_20HZ_VAL 0x05
99 #define ST_MAGN_2_ODR_AVL_40HZ_VAL 0x06
100 #define ST_MAGN_2_ODR_AVL_80HZ_VAL 0x07
101 #define ST_MAGN_2_PW_ADDR 0x22
102 #define ST_MAGN_2_PW_MASK 0x03
103 #define ST_MAGN_2_PW_ON 0x00
104 #define ST_MAGN_2_PW_OFF 0x03
105 #define ST_MAGN_2_FS_ADDR 0x21
106 #define ST_MAGN_2_FS_MASK 0x60
107 #define ST_MAGN_2_FS_AVL_4000_VAL 0x00
108 #define ST_MAGN_2_FS_AVL_8000_VAL 0x01
109 #define ST_MAGN_2_FS_AVL_12000_VAL 0x02
110 #define ST_MAGN_2_FS_AVL_16000_VAL 0x03
111 #define ST_MAGN_2_FS_AVL_4000_GAIN 146
112 #define ST_MAGN_2_FS_AVL_8000_GAIN 292
113 #define ST_MAGN_2_FS_AVL_12000_GAIN 438
114 #define ST_MAGN_2_FS_AVL_16000_GAIN 584
115 #define ST_MAGN_2_MULTIREAD_BIT false
116 #define ST_MAGN_2_OUT_X_L_ADDR 0x28
117 #define ST_MAGN_2_OUT_Y_L_ADDR 0x2a
118 #define ST_MAGN_2_OUT_Z_L_ADDR 0x2c
120 static const struct iio_chan_spec st_magn_16bit_channels
[] = {
121 ST_SENSORS_LSM_CHANNELS(IIO_MAGN
,
122 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
123 ST_SENSORS_SCAN_X
, 1, IIO_MOD_X
, 's', IIO_BE
, 16, 16,
124 ST_MAGN_DEFAULT_OUT_X_H_ADDR
),
125 ST_SENSORS_LSM_CHANNELS(IIO_MAGN
,
126 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
127 ST_SENSORS_SCAN_Y
, 1, IIO_MOD_Y
, 's', IIO_BE
, 16, 16,
128 ST_MAGN_DEFAULT_OUT_Y_H_ADDR
),
129 ST_SENSORS_LSM_CHANNELS(IIO_MAGN
,
130 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
131 ST_SENSORS_SCAN_Z
, 1, IIO_MOD_Z
, 's', IIO_BE
, 16, 16,
132 ST_MAGN_DEFAULT_OUT_Z_H_ADDR
),
133 IIO_CHAN_SOFT_TIMESTAMP(3)
136 static const struct iio_chan_spec st_magn_2_16bit_channels
[] = {
137 ST_SENSORS_LSM_CHANNELS(IIO_MAGN
,
138 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
139 ST_SENSORS_SCAN_X
, 1, IIO_MOD_X
, 's', IIO_LE
, 16, 16,
140 ST_MAGN_2_OUT_X_L_ADDR
),
141 ST_SENSORS_LSM_CHANNELS(IIO_MAGN
,
142 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
143 ST_SENSORS_SCAN_Y
, 1, IIO_MOD_Y
, 's', IIO_LE
, 16, 16,
144 ST_MAGN_2_OUT_Y_L_ADDR
),
145 ST_SENSORS_LSM_CHANNELS(IIO_MAGN
,
146 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
147 ST_SENSORS_SCAN_Z
, 1, IIO_MOD_Z
, 's', IIO_LE
, 16, 16,
148 ST_MAGN_2_OUT_Z_L_ADDR
),
149 IIO_CHAN_SOFT_TIMESTAMP(3)
152 static const struct st_sensor_settings st_magn_sensors_settings
[] = {
154 .wai
= ST_MAGN_1_WAI_EXP
,
155 .sensors_supported
= {
156 [0] = LSM303DLHC_MAGN_DEV_NAME
,
157 [1] = LSM303DLM_MAGN_DEV_NAME
,
159 .ch
= (struct iio_chan_spec
*)st_magn_16bit_channels
,
161 .addr
= ST_MAGN_1_ODR_ADDR
,
162 .mask
= ST_MAGN_1_ODR_MASK
,
164 { 1, ST_MAGN_1_ODR_AVL_1HZ_VAL
, },
165 { 2, ST_MAGN_1_ODR_AVL_2HZ_VAL
, },
166 { 3, ST_MAGN_1_ODR_AVL_3HZ_VAL
, },
167 { 8, ST_MAGN_1_ODR_AVL_8HZ_VAL
, },
168 { 15, ST_MAGN_1_ODR_AVL_15HZ_VAL
, },
169 { 30, ST_MAGN_1_ODR_AVL_30HZ_VAL
, },
170 { 75, ST_MAGN_1_ODR_AVL_75HZ_VAL
, },
171 { 220, ST_MAGN_1_ODR_AVL_220HZ_VAL
, },
175 .addr
= ST_MAGN_1_PW_ADDR
,
176 .mask
= ST_MAGN_1_PW_MASK
,
177 .value_on
= ST_MAGN_1_PW_ON
,
178 .value_off
= ST_MAGN_1_PW_OFF
,
181 .addr
= ST_MAGN_1_FS_ADDR
,
182 .mask
= ST_MAGN_1_FS_MASK
,
185 .num
= ST_MAGN_FS_AVL_1300MG
,
186 .value
= ST_MAGN_1_FS_AVL_1300_VAL
,
187 .gain
= ST_MAGN_1_FS_AVL_1300_GAIN_XY
,
188 .gain2
= ST_MAGN_1_FS_AVL_1300_GAIN_Z
,
191 .num
= ST_MAGN_FS_AVL_1900MG
,
192 .value
= ST_MAGN_1_FS_AVL_1900_VAL
,
193 .gain
= ST_MAGN_1_FS_AVL_1900_GAIN_XY
,
194 .gain2
= ST_MAGN_1_FS_AVL_1900_GAIN_Z
,
197 .num
= ST_MAGN_FS_AVL_2500MG
,
198 .value
= ST_MAGN_1_FS_AVL_2500_VAL
,
199 .gain
= ST_MAGN_1_FS_AVL_2500_GAIN_XY
,
200 .gain2
= ST_MAGN_1_FS_AVL_2500_GAIN_Z
,
203 .num
= ST_MAGN_FS_AVL_4000MG
,
204 .value
= ST_MAGN_1_FS_AVL_4000_VAL
,
205 .gain
= ST_MAGN_1_FS_AVL_4000_GAIN_XY
,
206 .gain2
= ST_MAGN_1_FS_AVL_4000_GAIN_Z
,
209 .num
= ST_MAGN_FS_AVL_4700MG
,
210 .value
= ST_MAGN_1_FS_AVL_4700_VAL
,
211 .gain
= ST_MAGN_1_FS_AVL_4700_GAIN_XY
,
212 .gain2
= ST_MAGN_1_FS_AVL_4700_GAIN_Z
,
215 .num
= ST_MAGN_FS_AVL_5600MG
,
216 .value
= ST_MAGN_1_FS_AVL_5600_VAL
,
217 .gain
= ST_MAGN_1_FS_AVL_5600_GAIN_XY
,
218 .gain2
= ST_MAGN_1_FS_AVL_5600_GAIN_Z
,
221 .num
= ST_MAGN_FS_AVL_8100MG
,
222 .value
= ST_MAGN_1_FS_AVL_8100_VAL
,
223 .gain
= ST_MAGN_1_FS_AVL_8100_GAIN_XY
,
224 .gain2
= ST_MAGN_1_FS_AVL_8100_GAIN_Z
,
228 .multi_read_bit
= ST_MAGN_1_MULTIREAD_BIT
,
232 .wai
= ST_MAGN_2_WAI_EXP
,
233 .sensors_supported
= {
234 [0] = LIS3MDL_MAGN_DEV_NAME
,
236 .ch
= (struct iio_chan_spec
*)st_magn_2_16bit_channels
,
238 .addr
= ST_MAGN_2_ODR_ADDR
,
239 .mask
= ST_MAGN_2_ODR_MASK
,
241 { 1, ST_MAGN_2_ODR_AVL_1HZ_VAL
, },
242 { 2, ST_MAGN_2_ODR_AVL_2HZ_VAL
, },
243 { 3, ST_MAGN_2_ODR_AVL_3HZ_VAL
, },
244 { 5, ST_MAGN_2_ODR_AVL_5HZ_VAL
, },
245 { 10, ST_MAGN_2_ODR_AVL_10HZ_VAL
, },
246 { 20, ST_MAGN_2_ODR_AVL_20HZ_VAL
, },
247 { 40, ST_MAGN_2_ODR_AVL_40HZ_VAL
, },
248 { 80, ST_MAGN_2_ODR_AVL_80HZ_VAL
, },
252 .addr
= ST_MAGN_2_PW_ADDR
,
253 .mask
= ST_MAGN_2_PW_MASK
,
254 .value_on
= ST_MAGN_2_PW_ON
,
255 .value_off
= ST_MAGN_2_PW_OFF
,
258 .addr
= ST_MAGN_2_FS_ADDR
,
259 .mask
= ST_MAGN_2_FS_MASK
,
262 .num
= ST_MAGN_FS_AVL_4000MG
,
263 .value
= ST_MAGN_2_FS_AVL_4000_VAL
,
264 .gain
= ST_MAGN_2_FS_AVL_4000_GAIN
,
267 .num
= ST_MAGN_FS_AVL_8000MG
,
268 .value
= ST_MAGN_2_FS_AVL_8000_VAL
,
269 .gain
= ST_MAGN_2_FS_AVL_8000_GAIN
,
272 .num
= ST_MAGN_FS_AVL_12000MG
,
273 .value
= ST_MAGN_2_FS_AVL_12000_VAL
,
274 .gain
= ST_MAGN_2_FS_AVL_12000_GAIN
,
277 .num
= ST_MAGN_FS_AVL_16000MG
,
278 .value
= ST_MAGN_2_FS_AVL_16000_VAL
,
279 .gain
= ST_MAGN_2_FS_AVL_16000_GAIN
,
283 .multi_read_bit
= ST_MAGN_2_MULTIREAD_BIT
,
288 static int st_magn_read_raw(struct iio_dev
*indio_dev
,
289 struct iio_chan_spec
const *ch
, int *val
,
290 int *val2
, long mask
)
293 struct st_sensor_data
*mdata
= iio_priv(indio_dev
);
296 case IIO_CHAN_INFO_RAW
:
297 err
= st_sensors_read_info_raw(indio_dev
, ch
, val
);
302 case IIO_CHAN_INFO_SCALE
:
304 if ((ch
->scan_index
== ST_SENSORS_SCAN_Z
) &&
305 (mdata
->current_fullscale
->gain2
!= 0))
306 *val2
= mdata
->current_fullscale
->gain2
;
308 *val2
= mdata
->current_fullscale
->gain
;
309 return IIO_VAL_INT_PLUS_MICRO
;
310 case IIO_CHAN_INFO_SAMP_FREQ
:
321 static int st_magn_write_raw(struct iio_dev
*indio_dev
,
322 struct iio_chan_spec
const *chan
, int val
, int val2
, long mask
)
327 case IIO_CHAN_INFO_SCALE
:
328 err
= st_sensors_set_fullscale_by_gain(indio_dev
, val2
);
330 case IIO_CHAN_INFO_SAMP_FREQ
:
333 mutex_lock(&indio_dev
->mlock
);
334 err
= st_sensors_set_odr(indio_dev
, val
);
335 mutex_unlock(&indio_dev
->mlock
);
344 static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL();
345 static ST_SENSORS_DEV_ATTR_SCALE_AVAIL(in_magn_scale_available
);
347 static struct attribute
*st_magn_attributes
[] = {
348 &iio_dev_attr_sampling_frequency_available
.dev_attr
.attr
,
349 &iio_dev_attr_in_magn_scale_available
.dev_attr
.attr
,
353 static const struct attribute_group st_magn_attribute_group
= {
354 .attrs
= st_magn_attributes
,
357 static const struct iio_info magn_info
= {
358 .driver_module
= THIS_MODULE
,
359 .attrs
= &st_magn_attribute_group
,
360 .read_raw
= &st_magn_read_raw
,
361 .write_raw
= &st_magn_write_raw
,
364 int st_magn_common_probe(struct iio_dev
*indio_dev
)
366 struct st_sensor_data
*mdata
= iio_priv(indio_dev
);
367 int irq
= mdata
->get_irq_data_ready(indio_dev
);
370 indio_dev
->modes
= INDIO_DIRECT_MODE
;
371 indio_dev
->info
= &magn_info
;
372 mutex_init(&mdata
->tb
.buf_lock
);
374 st_sensors_power_enable(indio_dev
);
376 err
= st_sensors_check_device_support(indio_dev
,
377 ARRAY_SIZE(st_magn_sensors_settings
),
378 st_magn_sensors_settings
);
382 mdata
->num_data_channels
= ST_MAGN_NUMBER_DATA_CHANNELS
;
383 mdata
->multiread_bit
= mdata
->sensor_settings
->multi_read_bit
;
384 indio_dev
->channels
= mdata
->sensor_settings
->ch
;
385 indio_dev
->num_channels
= ST_SENSORS_NUMBER_ALL_CHANNELS
;
387 mdata
->current_fullscale
= (struct st_sensor_fullscale_avl
*)
388 &mdata
->sensor_settings
->fs
.fs_avl
[0];
389 mdata
->odr
= mdata
->sensor_settings
->odr
.odr_avl
[0].hz
;
391 err
= st_sensors_init_sensor(indio_dev
, NULL
);
395 err
= st_magn_allocate_ring(indio_dev
);
400 err
= st_sensors_allocate_trigger(indio_dev
, NULL
);
402 goto st_magn_probe_trigger_error
;
405 err
= iio_device_register(indio_dev
);
407 goto st_magn_device_register_error
;
409 dev_info(&indio_dev
->dev
, "registered magnetometer %s\n",
414 st_magn_device_register_error
:
416 st_sensors_deallocate_trigger(indio_dev
);
417 st_magn_probe_trigger_error
:
418 st_magn_deallocate_ring(indio_dev
);
422 EXPORT_SYMBOL(st_magn_common_probe
);
424 void st_magn_common_remove(struct iio_dev
*indio_dev
)
426 struct st_sensor_data
*mdata
= iio_priv(indio_dev
);
428 st_sensors_power_disable(indio_dev
);
430 iio_device_unregister(indio_dev
);
431 if (mdata
->get_irq_data_ready(indio_dev
) > 0)
432 st_sensors_deallocate_trigger(indio_dev
);
434 st_magn_deallocate_ring(indio_dev
);
436 EXPORT_SYMBOL(st_magn_common_remove
);
438 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
439 MODULE_DESCRIPTION("STMicroelectronics magnetometers driver");
440 MODULE_LICENSE("GPL v2");