PM / sleep: Asynchronous threads for suspend_noirq
[linux/fpc-iii.git] / drivers / iio / pressure / st_pressure_core.c
blob58083f9d51c56691281891e4afbbe368d92697ab
1 /*
2 * STMicroelectronics pressures driver
4 * Copyright 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/trigger.h>
25 #include <linux/iio/buffer.h>
26 #include <linux/regulator/consumer.h>
27 #include <asm/unaligned.h>
29 #include <linux/iio/common/st_sensors.h>
30 #include "st_pressure.h"
32 #define ST_PRESS_LSB_PER_MBAR 4096UL
33 #define ST_PRESS_KPASCAL_NANO_SCALE (100000000UL / \
34 ST_PRESS_LSB_PER_MBAR)
35 #define ST_PRESS_LSB_PER_CELSIUS 480UL
36 #define ST_PRESS_CELSIUS_NANO_SCALE (1000000000UL / \
37 ST_PRESS_LSB_PER_CELSIUS)
38 #define ST_PRESS_NUMBER_DATA_CHANNELS 1
40 /* FULLSCALE */
41 #define ST_PRESS_FS_AVL_1260MB 1260
43 /* CUSTOM VALUES FOR LPS331AP SENSOR */
44 #define ST_PRESS_LPS331AP_WAI_EXP 0xbb
45 #define ST_PRESS_LPS331AP_ODR_ADDR 0x20
46 #define ST_PRESS_LPS331AP_ODR_MASK 0x70
47 #define ST_PRESS_LPS331AP_ODR_AVL_1HZ_VAL 0x01
48 #define ST_PRESS_LPS331AP_ODR_AVL_7HZ_VAL 0x05
49 #define ST_PRESS_LPS331AP_ODR_AVL_13HZ_VAL 0x06
50 #define ST_PRESS_LPS331AP_ODR_AVL_25HZ_VAL 0x07
51 #define ST_PRESS_LPS331AP_PW_ADDR 0x20
52 #define ST_PRESS_LPS331AP_PW_MASK 0x80
53 #define ST_PRESS_LPS331AP_FS_ADDR 0x23
54 #define ST_PRESS_LPS331AP_FS_MASK 0x30
55 #define ST_PRESS_LPS331AP_FS_AVL_1260_VAL 0x00
56 #define ST_PRESS_LPS331AP_FS_AVL_1260_GAIN ST_PRESS_KPASCAL_NANO_SCALE
57 #define ST_PRESS_LPS331AP_FS_AVL_TEMP_GAIN ST_PRESS_CELSIUS_NANO_SCALE
58 #define ST_PRESS_LPS331AP_BDU_ADDR 0x20
59 #define ST_PRESS_LPS331AP_BDU_MASK 0x04
60 #define ST_PRESS_LPS331AP_DRDY_IRQ_ADDR 0x22
61 #define ST_PRESS_LPS331AP_DRDY_IRQ_INT1_MASK 0x04
62 #define ST_PRESS_LPS331AP_DRDY_IRQ_INT2_MASK 0x20
63 #define ST_PRESS_LPS331AP_MULTIREAD_BIT true
64 #define ST_PRESS_LPS331AP_TEMP_OFFSET 42500
65 #define ST_PRESS_LPS331AP_OUT_XL_ADDR 0x28
66 #define ST_TEMP_LPS331AP_OUT_L_ADDR 0x2b
68 /* CUSTOM VALUES FOR LPS001WP SENSOR */
69 #define ST_PRESS_LPS001WP_WAI_EXP 0xba
70 #define ST_PRESS_LPS001WP_ODR_ADDR 0x20
71 #define ST_PRESS_LPS001WP_ODR_MASK 0x30
72 #define ST_PRESS_LPS001WP_ODR_AVL_1HZ_VAL 0x01
73 #define ST_PRESS_LPS001WP_ODR_AVL_7HZ_VAL 0x02
74 #define ST_PRESS_LPS001WP_ODR_AVL_13HZ_VAL 0x03
75 #define ST_PRESS_LPS001WP_PW_ADDR 0x20
76 #define ST_PRESS_LPS001WP_PW_MASK 0x40
77 #define ST_PRESS_LPS001WP_BDU_ADDR 0x20
78 #define ST_PRESS_LPS001WP_BDU_MASK 0x04
79 #define ST_PRESS_LPS001WP_MULTIREAD_BIT true
80 #define ST_PRESS_LPS001WP_OUT_L_ADDR 0x28
81 #define ST_TEMP_LPS001WP_OUT_L_ADDR 0x2a
83 static const struct iio_chan_spec st_press_lps331ap_channels[] = {
85 .type = IIO_PRESSURE,
86 .channel2 = IIO_NO_MOD,
87 .address = ST_PRESS_LPS331AP_OUT_XL_ADDR,
88 .scan_index = ST_SENSORS_SCAN_X,
89 .scan_type = {
90 .sign = 'u',
91 .realbits = 24,
92 .storagebits = 24,
93 .endianness = IIO_LE,
95 .info_mask_separate =
96 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
97 .modified = 0,
100 .type = IIO_TEMP,
101 .channel2 = IIO_NO_MOD,
102 .address = ST_TEMP_LPS331AP_OUT_L_ADDR,
103 .scan_index = -1,
104 .scan_type = {
105 .sign = 'u',
106 .realbits = 16,
107 .storagebits = 16,
108 .endianness = IIO_LE,
110 .info_mask_separate =
111 BIT(IIO_CHAN_INFO_RAW) |
112 BIT(IIO_CHAN_INFO_SCALE) |
113 BIT(IIO_CHAN_INFO_OFFSET),
114 .modified = 0,
116 IIO_CHAN_SOFT_TIMESTAMP(1)
119 static const struct iio_chan_spec st_press_lps001wp_channels[] = {
121 .type = IIO_PRESSURE,
122 .channel2 = IIO_NO_MOD,
123 .address = ST_PRESS_LPS001WP_OUT_L_ADDR,
124 .scan_index = ST_SENSORS_SCAN_X,
125 .scan_type = {
126 .sign = 'u',
127 .realbits = 16,
128 .storagebits = 16,
129 .endianness = IIO_LE,
131 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
132 .modified = 0,
135 .type = IIO_TEMP,
136 .channel2 = IIO_NO_MOD,
137 .address = ST_TEMP_LPS001WP_OUT_L_ADDR,
138 .scan_index = -1,
139 .scan_type = {
140 .sign = 'u',
141 .realbits = 16,
142 .storagebits = 16,
143 .endianness = IIO_LE,
145 .info_mask_separate =
146 BIT(IIO_CHAN_INFO_RAW) |
147 BIT(IIO_CHAN_INFO_OFFSET),
148 .modified = 0,
150 IIO_CHAN_SOFT_TIMESTAMP(1)
153 static const struct st_sensors st_press_sensors[] = {
155 .wai = ST_PRESS_LPS331AP_WAI_EXP,
156 .sensors_supported = {
157 [0] = LPS331AP_PRESS_DEV_NAME,
159 .ch = (struct iio_chan_spec *)st_press_lps331ap_channels,
160 .num_ch = ARRAY_SIZE(st_press_lps331ap_channels),
161 .odr = {
162 .addr = ST_PRESS_LPS331AP_ODR_ADDR,
163 .mask = ST_PRESS_LPS331AP_ODR_MASK,
164 .odr_avl = {
165 { 1, ST_PRESS_LPS331AP_ODR_AVL_1HZ_VAL, },
166 { 7, ST_PRESS_LPS331AP_ODR_AVL_7HZ_VAL, },
167 { 13, ST_PRESS_LPS331AP_ODR_AVL_13HZ_VAL, },
168 { 25, ST_PRESS_LPS331AP_ODR_AVL_25HZ_VAL, },
171 .pw = {
172 .addr = ST_PRESS_LPS331AP_PW_ADDR,
173 .mask = ST_PRESS_LPS331AP_PW_MASK,
174 .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
175 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
177 .fs = {
178 .addr = ST_PRESS_LPS331AP_FS_ADDR,
179 .mask = ST_PRESS_LPS331AP_FS_MASK,
180 .fs_avl = {
181 [0] = {
182 .num = ST_PRESS_FS_AVL_1260MB,
183 .value = ST_PRESS_LPS331AP_FS_AVL_1260_VAL,
184 .gain = ST_PRESS_LPS331AP_FS_AVL_1260_GAIN,
185 .gain2 = ST_PRESS_LPS331AP_FS_AVL_TEMP_GAIN,
189 .bdu = {
190 .addr = ST_PRESS_LPS331AP_BDU_ADDR,
191 .mask = ST_PRESS_LPS331AP_BDU_MASK,
193 .drdy_irq = {
194 .addr = ST_PRESS_LPS331AP_DRDY_IRQ_ADDR,
195 .mask_int1 = ST_PRESS_LPS331AP_DRDY_IRQ_INT1_MASK,
196 .mask_int2 = ST_PRESS_LPS331AP_DRDY_IRQ_INT2_MASK,
198 .multi_read_bit = ST_PRESS_LPS331AP_MULTIREAD_BIT,
199 .bootime = 2,
202 .wai = ST_PRESS_LPS001WP_WAI_EXP,
203 .sensors_supported = {
204 [0] = LPS001WP_PRESS_DEV_NAME,
206 .ch = (struct iio_chan_spec *)st_press_lps001wp_channels,
207 .num_ch = ARRAY_SIZE(st_press_lps001wp_channels),
208 .odr = {
209 .addr = ST_PRESS_LPS001WP_ODR_ADDR,
210 .mask = ST_PRESS_LPS001WP_ODR_MASK,
211 .odr_avl = {
212 { 1, ST_PRESS_LPS001WP_ODR_AVL_1HZ_VAL, },
213 { 7, ST_PRESS_LPS001WP_ODR_AVL_7HZ_VAL, },
214 { 13, ST_PRESS_LPS001WP_ODR_AVL_13HZ_VAL, },
217 .pw = {
218 .addr = ST_PRESS_LPS001WP_PW_ADDR,
219 .mask = ST_PRESS_LPS001WP_PW_MASK,
220 .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
221 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
223 .fs = {
224 .addr = 0,
226 .bdu = {
227 .addr = ST_PRESS_LPS001WP_BDU_ADDR,
228 .mask = ST_PRESS_LPS001WP_BDU_MASK,
230 .drdy_irq = {
231 .addr = 0,
233 .multi_read_bit = ST_PRESS_LPS001WP_MULTIREAD_BIT,
234 .bootime = 2,
238 static int st_press_read_raw(struct iio_dev *indio_dev,
239 struct iio_chan_spec const *ch, int *val,
240 int *val2, long mask)
242 int err;
243 struct st_sensor_data *pdata = iio_priv(indio_dev);
245 switch (mask) {
246 case IIO_CHAN_INFO_RAW:
247 err = st_sensors_read_info_raw(indio_dev, ch, val);
248 if (err < 0)
249 goto read_error;
251 return IIO_VAL_INT;
252 case IIO_CHAN_INFO_SCALE:
253 *val = 0;
255 switch (ch->type) {
256 case IIO_PRESSURE:
257 *val2 = pdata->current_fullscale->gain;
258 break;
259 case IIO_TEMP:
260 *val2 = pdata->current_fullscale->gain2;
261 break;
262 default:
263 err = -EINVAL;
264 goto read_error;
267 return IIO_VAL_INT_PLUS_NANO;
268 case IIO_CHAN_INFO_OFFSET:
269 switch (ch->type) {
270 case IIO_TEMP:
271 *val = 425;
272 *val2 = 10;
273 break;
274 default:
275 err = -EINVAL;
276 goto read_error;
279 return IIO_VAL_FRACTIONAL;
280 default:
281 return -EINVAL;
284 read_error:
285 return err;
288 static ST_SENSOR_DEV_ATTR_SAMP_FREQ();
289 static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL();
291 static struct attribute *st_press_attributes[] = {
292 &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
293 &iio_dev_attr_sampling_frequency.dev_attr.attr,
294 NULL,
297 static const struct attribute_group st_press_attribute_group = {
298 .attrs = st_press_attributes,
301 static const struct iio_info press_info = {
302 .driver_module = THIS_MODULE,
303 .attrs = &st_press_attribute_group,
304 .read_raw = &st_press_read_raw,
307 #ifdef CONFIG_IIO_TRIGGER
308 static const struct iio_trigger_ops st_press_trigger_ops = {
309 .owner = THIS_MODULE,
310 .set_trigger_state = ST_PRESS_TRIGGER_SET_STATE,
312 #define ST_PRESS_TRIGGER_OPS (&st_press_trigger_ops)
313 #else
314 #define ST_PRESS_TRIGGER_OPS NULL
315 #endif
317 static void st_press_power_enable(struct iio_dev *indio_dev)
319 struct st_sensor_data *pdata = iio_priv(indio_dev);
320 int err;
322 /* Regulators not mandatory, but if requested we should enable them. */
323 pdata->vdd = devm_regulator_get_optional(&indio_dev->dev, "vdd");
324 if (!IS_ERR(pdata->vdd)) {
325 err = regulator_enable(pdata->vdd);
326 if (err != 0)
327 dev_warn(&indio_dev->dev,
328 "Failed to enable specified Vdd supply\n");
331 pdata->vdd_io = devm_regulator_get_optional(&indio_dev->dev, "vddio");
332 if (!IS_ERR(pdata->vdd_io)) {
333 err = regulator_enable(pdata->vdd_io);
334 if (err != 0)
335 dev_warn(&indio_dev->dev,
336 "Failed to enable specified Vdd_IO supply\n");
340 static void st_press_power_disable(struct iio_dev *indio_dev)
342 struct st_sensor_data *pdata = iio_priv(indio_dev);
344 if (!IS_ERR(pdata->vdd))
345 regulator_disable(pdata->vdd);
347 if (!IS_ERR(pdata->vdd_io))
348 regulator_disable(pdata->vdd_io);
351 int st_press_common_probe(struct iio_dev *indio_dev,
352 struct st_sensors_platform_data *plat_data)
354 struct st_sensor_data *pdata = iio_priv(indio_dev);
355 int irq = pdata->get_irq_data_ready(indio_dev);
356 int err;
358 indio_dev->modes = INDIO_DIRECT_MODE;
359 indio_dev->info = &press_info;
361 st_press_power_enable(indio_dev);
363 err = st_sensors_check_device_support(indio_dev,
364 ARRAY_SIZE(st_press_sensors),
365 st_press_sensors);
366 if (err < 0)
367 return err;
369 pdata->num_data_channels = ST_PRESS_NUMBER_DATA_CHANNELS;
370 pdata->multiread_bit = pdata->sensor->multi_read_bit;
371 indio_dev->channels = pdata->sensor->ch;
372 indio_dev->num_channels = pdata->sensor->num_ch;
374 if (pdata->sensor->fs.addr != 0)
375 pdata->current_fullscale = (struct st_sensor_fullscale_avl *)
376 &pdata->sensor->fs.fs_avl[0];
378 pdata->odr = pdata->sensor->odr.odr_avl[0].hz;
380 /* Some devices don't support a data ready pin. */
381 if (!plat_data && pdata->sensor->drdy_irq.addr)
382 plat_data =
383 (struct st_sensors_platform_data *)&default_press_pdata;
385 err = st_sensors_init_sensor(indio_dev, plat_data);
386 if (err < 0)
387 return err;
389 err = st_press_allocate_ring(indio_dev);
390 if (err < 0)
391 return err;
393 if (irq > 0) {
394 err = st_sensors_allocate_trigger(indio_dev,
395 ST_PRESS_TRIGGER_OPS);
396 if (err < 0)
397 goto st_press_probe_trigger_error;
400 err = iio_device_register(indio_dev);
401 if (err)
402 goto st_press_device_register_error;
404 return err;
406 st_press_device_register_error:
407 if (irq > 0)
408 st_sensors_deallocate_trigger(indio_dev);
409 st_press_probe_trigger_error:
410 st_press_deallocate_ring(indio_dev);
412 return err;
414 EXPORT_SYMBOL(st_press_common_probe);
416 void st_press_common_remove(struct iio_dev *indio_dev)
418 struct st_sensor_data *pdata = iio_priv(indio_dev);
420 st_press_power_disable(indio_dev);
422 iio_device_unregister(indio_dev);
423 if (pdata->get_irq_data_ready(indio_dev) > 0)
424 st_sensors_deallocate_trigger(indio_dev);
426 st_press_deallocate_ring(indio_dev);
428 EXPORT_SYMBOL(st_press_common_remove);
430 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
431 MODULE_DESCRIPTION("STMicroelectronics pressures driver");
432 MODULE_LICENSE("GPL v2");