PM / sleep: Asynchronous threads for suspend_noirq
[linux/fpc-iii.git] / drivers / iio / magnetometer / st_magn_core.c
blob52bbcfa1e07795208b45633ced62de0015772b74
1 /*
2 * STMicroelectronics magnetometers driver
4 * Copyright 2012-2013 STMicroelectronics Inc.
6 * Denis Ciocca <denis.ciocca@st.com>
8 * Licensed under the GPL-2.
9 */
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>
27 #include "st_magn.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
36 /* FULLSCALE */
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,
157 .odr = {
158 .addr = ST_MAGN_1_ODR_ADDR,
159 .mask = ST_MAGN_1_ODR_MASK,
160 .odr_avl = {
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, },
171 .pw = {
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,
177 .fs = {
178 .addr = ST_MAGN_1_FS_ADDR,
179 .mask = ST_MAGN_1_FS_MASK,
180 .fs_avl = {
181 [0] = {
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,
187 [1] = {
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,
193 [2] = {
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,
199 [3] = {
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,
205 [4] = {
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,
211 [5] = {
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,
217 [6] = {
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,
226 .bootime = 2,
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,
234 .odr = {
235 .addr = ST_MAGN_2_ODR_ADDR,
236 .mask = ST_MAGN_2_ODR_MASK,
237 .odr_avl = {
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, },
248 .pw = {
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,
254 .fs = {
255 .addr = ST_MAGN_2_FS_ADDR,
256 .mask = ST_MAGN_2_FS_MASK,
257 .fs_avl = {
258 [0] = {
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,
263 [1] = {
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,
268 [2] = {
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,
276 .bootime = 2,
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)
284 int err;
285 struct st_sensor_data *mdata = iio_priv(indio_dev);
287 switch (mask) {
288 case IIO_CHAN_INFO_RAW:
289 err = st_sensors_read_info_raw(indio_dev, ch, val);
290 if (err < 0)
291 goto read_error;
293 return IIO_VAL_INT;
294 case IIO_CHAN_INFO_SCALE:
295 *val = 0;
296 if ((ch->scan_index == ST_SENSORS_SCAN_Z) &&
297 (mdata->current_fullscale->gain2 != 0))
298 *val2 = mdata->current_fullscale->gain2;
299 else
300 *val2 = mdata->current_fullscale->gain;
301 return IIO_VAL_INT_PLUS_MICRO;
302 default:
303 return -EINVAL;
306 read_error:
307 return err;
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)
313 int err;
315 switch (mask) {
316 case IIO_CHAN_INFO_SCALE:
317 err = st_sensors_set_fullscale_by_gain(indio_dev, val2);
318 break;
319 default:
320 err = -EINVAL;
323 return err;
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,
334 NULL,
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)
351 struct st_sensor_data *mdata = iio_priv(indio_dev);
352 int irq = mdata->get_irq_data_ready(indio_dev);
353 int err;
355 indio_dev->modes = INDIO_DIRECT_MODE;
356 indio_dev->info = &magn_info;
358 err = st_sensors_check_device_support(indio_dev,
359 ARRAY_SIZE(st_magn_sensors), st_magn_sensors);
360 if (err < 0)
361 return err;
363 mdata->num_data_channels = ST_MAGN_NUMBER_DATA_CHANNELS;
364 mdata->multiread_bit = mdata->sensor->multi_read_bit;
365 indio_dev->channels = mdata->sensor->ch;
366 indio_dev->num_channels = ST_SENSORS_NUMBER_ALL_CHANNELS;
368 mdata->current_fullscale = (struct st_sensor_fullscale_avl *)
369 &mdata->sensor->fs.fs_avl[0];
370 mdata->odr = mdata->sensor->odr.odr_avl[0].hz;
372 err = st_sensors_init_sensor(indio_dev, pdata);
373 if (err < 0)
374 return err;
376 err = st_magn_allocate_ring(indio_dev);
377 if (err < 0)
378 return err;
380 if (irq > 0) {
381 err = st_sensors_allocate_trigger(indio_dev, NULL);
382 if (err < 0)
383 goto st_magn_probe_trigger_error;
386 err = iio_device_register(indio_dev);
387 if (err)
388 goto st_magn_device_register_error;
390 return 0;
392 st_magn_device_register_error:
393 if (irq > 0)
394 st_sensors_deallocate_trigger(indio_dev);
395 st_magn_probe_trigger_error:
396 st_magn_deallocate_ring(indio_dev);
398 return err;
400 EXPORT_SYMBOL(st_magn_common_probe);
402 void st_magn_common_remove(struct iio_dev *indio_dev)
404 struct st_sensor_data *mdata = iio_priv(indio_dev);
406 iio_device_unregister(indio_dev);
407 if (mdata->get_irq_data_ready(indio_dev) > 0)
408 st_sensors_deallocate_trigger(indio_dev);
410 st_magn_deallocate_ring(indio_dev);
412 EXPORT_SYMBOL(st_magn_common_remove);
414 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
415 MODULE_DESCRIPTION("STMicroelectronics magnetometers driver");
416 MODULE_LICENSE("GPL v2");