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_10000MG 10000
47 /* CUSTOM VALUES FOR SENSOR 1 */
48 #define ST_MAGN_1_WAI_EXP 0x3c
49 #define ST_MAGN_1_ODR_ADDR 0x00
50 #define ST_MAGN_1_ODR_MASK 0x1c
51 #define ST_MAGN_1_ODR_AVL_1HZ_VAL 0x00
52 #define ST_MAGN_1_ODR_AVL_2HZ_VAL 0x01
53 #define ST_MAGN_1_ODR_AVL_3HZ_VAL 0x02
54 #define ST_MAGN_1_ODR_AVL_8HZ_VAL 0x03
55 #define ST_MAGN_1_ODR_AVL_15HZ_VAL 0x04
56 #define ST_MAGN_1_ODR_AVL_30HZ_VAL 0x05
57 #define ST_MAGN_1_ODR_AVL_75HZ_VAL 0x06
58 #define ST_MAGN_1_ODR_AVL_220HZ_VAL 0x07
59 #define ST_MAGN_1_PW_ADDR 0x02
60 #define ST_MAGN_1_PW_MASK 0x03
61 #define ST_MAGN_1_PW_ON 0x00
62 #define ST_MAGN_1_PW_OFF 0x03
63 #define ST_MAGN_1_FS_ADDR 0x01
64 #define ST_MAGN_1_FS_MASK 0xe0
65 #define ST_MAGN_1_FS_AVL_1300_VAL 0x01
66 #define ST_MAGN_1_FS_AVL_1900_VAL 0x02
67 #define ST_MAGN_1_FS_AVL_2500_VAL 0x03
68 #define ST_MAGN_1_FS_AVL_4000_VAL 0x04
69 #define ST_MAGN_1_FS_AVL_4700_VAL 0x05
70 #define ST_MAGN_1_FS_AVL_5600_VAL 0x06
71 #define ST_MAGN_1_FS_AVL_8100_VAL 0x07
72 #define ST_MAGN_1_FS_AVL_1300_GAIN_XY 1100
73 #define ST_MAGN_1_FS_AVL_1900_GAIN_XY 855
74 #define ST_MAGN_1_FS_AVL_2500_GAIN_XY 670
75 #define ST_MAGN_1_FS_AVL_4000_GAIN_XY 450
76 #define ST_MAGN_1_FS_AVL_4700_GAIN_XY 400
77 #define ST_MAGN_1_FS_AVL_5600_GAIN_XY 330
78 #define ST_MAGN_1_FS_AVL_8100_GAIN_XY 230
79 #define ST_MAGN_1_FS_AVL_1300_GAIN_Z 980
80 #define ST_MAGN_1_FS_AVL_1900_GAIN_Z 760
81 #define ST_MAGN_1_FS_AVL_2500_GAIN_Z 600
82 #define ST_MAGN_1_FS_AVL_4000_GAIN_Z 400
83 #define ST_MAGN_1_FS_AVL_4700_GAIN_Z 355
84 #define ST_MAGN_1_FS_AVL_5600_GAIN_Z 295
85 #define ST_MAGN_1_FS_AVL_8100_GAIN_Z 205
86 #define ST_MAGN_1_MULTIREAD_BIT false
88 /* CUSTOM VALUES FOR SENSOR 2 */
89 #define ST_MAGN_2_WAI_EXP 0x3d
90 #define ST_MAGN_2_ODR_ADDR 0x20
91 #define ST_MAGN_2_ODR_MASK 0x1c
92 #define ST_MAGN_2_ODR_AVL_1HZ_VAL 0x00
93 #define ST_MAGN_2_ODR_AVL_2HZ_VAL 0x01
94 #define ST_MAGN_2_ODR_AVL_3HZ_VAL 0x02
95 #define ST_MAGN_2_ODR_AVL_5HZ_VAL 0x03
96 #define ST_MAGN_2_ODR_AVL_10HZ_VAL 0x04
97 #define ST_MAGN_2_ODR_AVL_20HZ_VAL 0x05
98 #define ST_MAGN_2_ODR_AVL_40HZ_VAL 0x06
99 #define ST_MAGN_2_ODR_AVL_80HZ_VAL 0x07
100 #define ST_MAGN_2_PW_ADDR 0x22
101 #define ST_MAGN_2_PW_MASK 0x03
102 #define ST_MAGN_2_PW_ON 0x00
103 #define ST_MAGN_2_PW_OFF 0x03
104 #define ST_MAGN_2_FS_ADDR 0x21
105 #define ST_MAGN_2_FS_MASK 0x60
106 #define ST_MAGN_2_FS_AVL_4000_VAL 0x00
107 #define ST_MAGN_2_FS_AVL_8000_VAL 0x01
108 #define ST_MAGN_2_FS_AVL_10000_VAL 0x02
109 #define ST_MAGN_2_FS_AVL_4000_GAIN 430
110 #define ST_MAGN_2_FS_AVL_8000_GAIN 230
111 #define ST_MAGN_2_FS_AVL_10000_GAIN 230
112 #define ST_MAGN_2_MULTIREAD_BIT false
113 #define ST_MAGN_2_OUT_X_L_ADDR 0x28
114 #define ST_MAGN_2_OUT_Y_L_ADDR 0x2a
115 #define ST_MAGN_2_OUT_Z_L_ADDR 0x2c
117 static const struct iio_chan_spec st_magn_16bit_channels
[] = {
118 ST_SENSORS_LSM_CHANNELS(IIO_MAGN
,
119 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
120 ST_SENSORS_SCAN_X
, 1, IIO_MOD_X
, 's', IIO_BE
, 16, 16,
121 ST_MAGN_DEFAULT_OUT_X_H_ADDR
),
122 ST_SENSORS_LSM_CHANNELS(IIO_MAGN
,
123 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
124 ST_SENSORS_SCAN_Y
, 1, IIO_MOD_Y
, 's', IIO_BE
, 16, 16,
125 ST_MAGN_DEFAULT_OUT_Y_H_ADDR
),
126 ST_SENSORS_LSM_CHANNELS(IIO_MAGN
,
127 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
128 ST_SENSORS_SCAN_Z
, 1, IIO_MOD_Z
, 's', IIO_BE
, 16, 16,
129 ST_MAGN_DEFAULT_OUT_Z_H_ADDR
),
130 IIO_CHAN_SOFT_TIMESTAMP(3)
133 static const struct iio_chan_spec st_magn_2_16bit_channels
[] = {
134 ST_SENSORS_LSM_CHANNELS(IIO_MAGN
,
135 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
136 ST_SENSORS_SCAN_X
, 1, IIO_MOD_X
, 's', IIO_LE
, 16, 16,
137 ST_MAGN_2_OUT_X_L_ADDR
),
138 ST_SENSORS_LSM_CHANNELS(IIO_MAGN
,
139 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
140 ST_SENSORS_SCAN_Y
, 1, IIO_MOD_Y
, 's', IIO_LE
, 16, 16,
141 ST_MAGN_2_OUT_Y_L_ADDR
),
142 ST_SENSORS_LSM_CHANNELS(IIO_MAGN
,
143 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
144 ST_SENSORS_SCAN_Z
, 1, IIO_MOD_Z
, 's', IIO_LE
, 16, 16,
145 ST_MAGN_2_OUT_Z_L_ADDR
),
146 IIO_CHAN_SOFT_TIMESTAMP(3)
149 static const struct st_sensors st_magn_sensors
[] = {
151 .wai
= ST_MAGN_1_WAI_EXP
,
152 .sensors_supported
= {
153 [0] = LSM303DLHC_MAGN_DEV_NAME
,
154 [1] = LSM303DLM_MAGN_DEV_NAME
,
156 .ch
= (struct iio_chan_spec
*)st_magn_16bit_channels
,
158 .addr
= ST_MAGN_1_ODR_ADDR
,
159 .mask
= ST_MAGN_1_ODR_MASK
,
161 { 1, ST_MAGN_1_ODR_AVL_1HZ_VAL
, },
162 { 2, ST_MAGN_1_ODR_AVL_2HZ_VAL
, },
163 { 3, ST_MAGN_1_ODR_AVL_3HZ_VAL
, },
164 { 8, ST_MAGN_1_ODR_AVL_8HZ_VAL
, },
165 { 15, ST_MAGN_1_ODR_AVL_15HZ_VAL
, },
166 { 30, ST_MAGN_1_ODR_AVL_30HZ_VAL
, },
167 { 75, ST_MAGN_1_ODR_AVL_75HZ_VAL
, },
168 { 220, ST_MAGN_1_ODR_AVL_220HZ_VAL
, },
172 .addr
= ST_MAGN_1_PW_ADDR
,
173 .mask
= ST_MAGN_1_PW_MASK
,
174 .value_on
= ST_MAGN_1_PW_ON
,
175 .value_off
= ST_MAGN_1_PW_OFF
,
178 .addr
= ST_MAGN_1_FS_ADDR
,
179 .mask
= ST_MAGN_1_FS_MASK
,
182 .num
= ST_MAGN_FS_AVL_1300MG
,
183 .value
= ST_MAGN_1_FS_AVL_1300_VAL
,
184 .gain
= ST_MAGN_1_FS_AVL_1300_GAIN_XY
,
185 .gain2
= ST_MAGN_1_FS_AVL_1300_GAIN_Z
,
188 .num
= ST_MAGN_FS_AVL_1900MG
,
189 .value
= ST_MAGN_1_FS_AVL_1900_VAL
,
190 .gain
= ST_MAGN_1_FS_AVL_1900_GAIN_XY
,
191 .gain2
= ST_MAGN_1_FS_AVL_1900_GAIN_Z
,
194 .num
= ST_MAGN_FS_AVL_2500MG
,
195 .value
= ST_MAGN_1_FS_AVL_2500_VAL
,
196 .gain
= ST_MAGN_1_FS_AVL_2500_GAIN_XY
,
197 .gain2
= ST_MAGN_1_FS_AVL_2500_GAIN_Z
,
200 .num
= ST_MAGN_FS_AVL_4000MG
,
201 .value
= ST_MAGN_1_FS_AVL_4000_VAL
,
202 .gain
= ST_MAGN_1_FS_AVL_4000_GAIN_XY
,
203 .gain2
= ST_MAGN_1_FS_AVL_4000_GAIN_Z
,
206 .num
= ST_MAGN_FS_AVL_4700MG
,
207 .value
= ST_MAGN_1_FS_AVL_4700_VAL
,
208 .gain
= ST_MAGN_1_FS_AVL_4700_GAIN_XY
,
209 .gain2
= ST_MAGN_1_FS_AVL_4700_GAIN_Z
,
212 .num
= ST_MAGN_FS_AVL_5600MG
,
213 .value
= ST_MAGN_1_FS_AVL_5600_VAL
,
214 .gain
= ST_MAGN_1_FS_AVL_5600_GAIN_XY
,
215 .gain2
= ST_MAGN_1_FS_AVL_5600_GAIN_Z
,
218 .num
= ST_MAGN_FS_AVL_8100MG
,
219 .value
= ST_MAGN_1_FS_AVL_8100_VAL
,
220 .gain
= ST_MAGN_1_FS_AVL_8100_GAIN_XY
,
221 .gain2
= ST_MAGN_1_FS_AVL_8100_GAIN_Z
,
225 .multi_read_bit
= ST_MAGN_1_MULTIREAD_BIT
,
229 .wai
= ST_MAGN_2_WAI_EXP
,
230 .sensors_supported
= {
231 [0] = LIS3MDL_MAGN_DEV_NAME
,
233 .ch
= (struct iio_chan_spec
*)st_magn_2_16bit_channels
,
235 .addr
= ST_MAGN_2_ODR_ADDR
,
236 .mask
= ST_MAGN_2_ODR_MASK
,
238 { 1, ST_MAGN_2_ODR_AVL_1HZ_VAL
, },
239 { 2, ST_MAGN_2_ODR_AVL_2HZ_VAL
, },
240 { 3, ST_MAGN_2_ODR_AVL_3HZ_VAL
, },
241 { 5, ST_MAGN_2_ODR_AVL_5HZ_VAL
, },
242 { 10, ST_MAGN_2_ODR_AVL_10HZ_VAL
, },
243 { 20, ST_MAGN_2_ODR_AVL_20HZ_VAL
, },
244 { 40, ST_MAGN_2_ODR_AVL_40HZ_VAL
, },
245 { 80, ST_MAGN_2_ODR_AVL_80HZ_VAL
, },
249 .addr
= ST_MAGN_2_PW_ADDR
,
250 .mask
= ST_MAGN_2_PW_MASK
,
251 .value_on
= ST_MAGN_2_PW_ON
,
252 .value_off
= ST_MAGN_2_PW_OFF
,
255 .addr
= ST_MAGN_2_FS_ADDR
,
256 .mask
= ST_MAGN_2_FS_MASK
,
259 .num
= ST_MAGN_FS_AVL_4000MG
,
260 .value
= ST_MAGN_2_FS_AVL_4000_VAL
,
261 .gain
= ST_MAGN_2_FS_AVL_4000_GAIN
,
264 .num
= ST_MAGN_FS_AVL_8000MG
,
265 .value
= ST_MAGN_2_FS_AVL_8000_VAL
,
266 .gain
= ST_MAGN_2_FS_AVL_8000_GAIN
,
269 .num
= ST_MAGN_FS_AVL_10000MG
,
270 .value
= ST_MAGN_2_FS_AVL_10000_VAL
,
271 .gain
= ST_MAGN_2_FS_AVL_10000_GAIN
,
275 .multi_read_bit
= ST_MAGN_2_MULTIREAD_BIT
,
280 static int st_magn_read_raw(struct iio_dev
*indio_dev
,
281 struct iio_chan_spec
const *ch
, int *val
,
282 int *val2
, long mask
)
285 struct st_sensor_data
*mdata
= iio_priv(indio_dev
);
288 case IIO_CHAN_INFO_RAW
:
289 err
= st_sensors_read_info_raw(indio_dev
, ch
, val
);
294 case IIO_CHAN_INFO_SCALE
:
296 if ((ch
->scan_index
== ST_SENSORS_SCAN_Z
) &&
297 (mdata
->current_fullscale
->gain2
!= 0))
298 *val2
= mdata
->current_fullscale
->gain2
;
300 *val2
= mdata
->current_fullscale
->gain
;
301 return IIO_VAL_INT_PLUS_MICRO
;
310 static int st_magn_write_raw(struct iio_dev
*indio_dev
,
311 struct iio_chan_spec
const *chan
, int val
, int val2
, long mask
)
316 case IIO_CHAN_INFO_SCALE
:
317 err
= st_sensors_set_fullscale_by_gain(indio_dev
, val2
);
326 static ST_SENSOR_DEV_ATTR_SAMP_FREQ();
327 static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL();
328 static ST_SENSORS_DEV_ATTR_SCALE_AVAIL(in_magn_scale_available
);
330 static struct attribute
*st_magn_attributes
[] = {
331 &iio_dev_attr_sampling_frequency_available
.dev_attr
.attr
,
332 &iio_dev_attr_in_magn_scale_available
.dev_attr
.attr
,
333 &iio_dev_attr_sampling_frequency
.dev_attr
.attr
,
337 static const struct attribute_group st_magn_attribute_group
= {
338 .attrs
= st_magn_attributes
,
341 static const struct iio_info magn_info
= {
342 .driver_module
= THIS_MODULE
,
343 .attrs
= &st_magn_attribute_group
,
344 .read_raw
= &st_magn_read_raw
,
345 .write_raw
= &st_magn_write_raw
,
348 int st_magn_common_probe(struct iio_dev
*indio_dev
,
349 struct st_sensors_platform_data
*pdata
)
352 struct st_sensor_data
*mdata
= iio_priv(indio_dev
);
354 indio_dev
->modes
= INDIO_DIRECT_MODE
;
355 indio_dev
->info
= &magn_info
;
357 err
= st_sensors_check_device_support(indio_dev
,
358 ARRAY_SIZE(st_magn_sensors
), st_magn_sensors
);
360 goto st_magn_common_probe_error
;
362 mdata
->num_data_channels
= ST_MAGN_NUMBER_DATA_CHANNELS
;
363 mdata
->multiread_bit
= mdata
->sensor
->multi_read_bit
;
364 indio_dev
->channels
= mdata
->sensor
->ch
;
365 indio_dev
->num_channels
= ST_SENSORS_NUMBER_ALL_CHANNELS
;
367 mdata
->current_fullscale
= (struct st_sensor_fullscale_avl
*)
368 &mdata
->sensor
->fs
.fs_avl
[0];
369 mdata
->odr
= mdata
->sensor
->odr
.odr_avl
[0].hz
;
371 err
= st_sensors_init_sensor(indio_dev
, pdata
);
373 goto st_magn_common_probe_error
;
375 if (mdata
->get_irq_data_ready(indio_dev
) > 0) {
376 err
= st_magn_allocate_ring(indio_dev
);
378 goto st_magn_common_probe_error
;
379 err
= st_sensors_allocate_trigger(indio_dev
, NULL
);
381 goto st_magn_probe_trigger_error
;
384 err
= iio_device_register(indio_dev
);
386 goto st_magn_device_register_error
;
390 st_magn_device_register_error
:
391 if (mdata
->get_irq_data_ready(indio_dev
) > 0)
392 st_sensors_deallocate_trigger(indio_dev
);
393 st_magn_probe_trigger_error
:
394 if (mdata
->get_irq_data_ready(indio_dev
) > 0)
395 st_magn_deallocate_ring(indio_dev
);
396 st_magn_common_probe_error
:
399 EXPORT_SYMBOL(st_magn_common_probe
);
401 void st_magn_common_remove(struct iio_dev
*indio_dev
)
403 struct st_sensor_data
*mdata
= iio_priv(indio_dev
);
405 iio_device_unregister(indio_dev
);
406 if (mdata
->get_irq_data_ready(indio_dev
) > 0) {
407 st_sensors_deallocate_trigger(indio_dev
);
408 st_magn_deallocate_ring(indio_dev
);
411 EXPORT_SYMBOL(st_magn_common_remove
);
413 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
414 MODULE_DESCRIPTION("STMicroelectronics magnetometers driver");
415 MODULE_LICENSE("GPL v2");