iio: pressure-core: st: Expand and rename LPS331AP's channel descriptor
[firefly-linux-kernel-4.4.55.git] / drivers / iio / pressure / st_pressure_core.c
1 /*
2  * STMicroelectronics pressures driver
3  *
4  * Copyright 2013 STMicroelectronics Inc.
5  *
6  * Denis Ciocca <denis.ciocca@st.com>
7  *
8  * Licensed under the GPL-2.
9  */
10
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 <asm/unaligned.h>
27
28 #include <linux/iio/common/st_sensors.h>
29 #include "st_pressure.h"
30
31 #define ST_PRESS_LSB_PER_MBAR                   4096UL
32 #define ST_PRESS_KPASCAL_NANO_SCALE             (100000000UL / \
33                                                  ST_PRESS_LSB_PER_MBAR)
34 #define ST_PRESS_LSB_PER_CELSIUS                480UL
35 #define ST_PRESS_CELSIUS_NANO_SCALE             (1000000000UL / \
36                                                  ST_PRESS_LSB_PER_CELSIUS)
37 #define ST_PRESS_NUMBER_DATA_CHANNELS           1
38
39 /* FULLSCALE */
40 #define ST_PRESS_FS_AVL_1260MB                  1260
41
42 /* CUSTOM VALUES FOR LPS331AP SENSOR */
43 #define ST_PRESS_LPS331AP_WAI_EXP               0xbb
44 #define ST_PRESS_LPS331AP_ODR_ADDR              0x20
45 #define ST_PRESS_LPS331AP_ODR_MASK              0x70
46 #define ST_PRESS_LPS331AP_ODR_AVL_1HZ_VAL       0x01
47 #define ST_PRESS_LPS331AP_ODR_AVL_7HZ_VAL       0x05
48 #define ST_PRESS_LPS331AP_ODR_AVL_13HZ_VAL      0x06
49 #define ST_PRESS_LPS331AP_ODR_AVL_25HZ_VAL      0x07
50 #define ST_PRESS_LPS331AP_PW_ADDR               0x20
51 #define ST_PRESS_LPS331AP_PW_MASK               0x80
52 #define ST_PRESS_LPS331AP_FS_ADDR               0x23
53 #define ST_PRESS_LPS331AP_FS_MASK               0x30
54 #define ST_PRESS_LPS331AP_FS_AVL_1260_VAL       0x00
55 #define ST_PRESS_LPS331AP_FS_AVL_1260_GAIN      ST_PRESS_KPASCAL_NANO_SCALE
56 #define ST_PRESS_LPS331AP_FS_AVL_TEMP_GAIN      ST_PRESS_CELSIUS_NANO_SCALE
57 #define ST_PRESS_LPS331AP_BDU_ADDR              0x20
58 #define ST_PRESS_LPS331AP_BDU_MASK              0x04
59 #define ST_PRESS_LPS331AP_DRDY_IRQ_ADDR         0x22
60 #define ST_PRESS_LPS331AP_DRDY_IRQ_INT1_MASK    0x04
61 #define ST_PRESS_LPS331AP_DRDY_IRQ_INT2_MASK    0x20
62 #define ST_PRESS_LPS331AP_MULTIREAD_BIT         true
63 #define ST_PRESS_LPS331AP_TEMP_OFFSET           42500
64 #define ST_PRESS_LPS331AP_OUT_XL_ADDR           0x28
65 #define ST_TEMP_LPS331AP_OUT_L_ADDR             0x2b
66
67 static const struct iio_chan_spec st_press_lps331ap_channels[] = {
68         {
69                 .type = IIO_PRESSURE,
70                 .channel2 = IIO_NO_MOD,
71                 .address = ST_PRESS_LPS331AP_OUT_XL_ADDR,
72                 .scan_index = ST_SENSORS_SCAN_X,
73                 .scan_type = {
74                         .sign = 'u',
75                         .realbits = 24,
76                         .storagebits = 24,
77                         .endianness = IIO_LE,
78                 },
79                 .info_mask_separate =
80                         BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
81                 .modified = 0,
82         },
83         {
84                 .type = IIO_TEMP,
85                 .channel2 = IIO_NO_MOD,
86                 .address = ST_TEMP_LPS331AP_OUT_L_ADDR,
87                 .scan_index = -1,
88                 .scan_type = {
89                         .sign = 'u',
90                         .realbits = 16,
91                         .storagebits = 16,
92                         .endianness = IIO_LE,
93                 },
94                 .info_mask_separate =
95                         BIT(IIO_CHAN_INFO_RAW) |
96                         BIT(IIO_CHAN_INFO_SCALE) |
97                         BIT(IIO_CHAN_INFO_OFFSET),
98                 .modified = 0,
99         },
100         IIO_CHAN_SOFT_TIMESTAMP(1)
101 };
102
103 static const struct st_sensors st_press_sensors[] = {
104         {
105                 .wai = ST_PRESS_LPS331AP_WAI_EXP,
106                 .sensors_supported = {
107                         [0] = LPS331AP_PRESS_DEV_NAME,
108                 },
109                 .ch = (struct iio_chan_spec *)st_press_lps331ap_channels,
110                 .odr = {
111                         .addr = ST_PRESS_LPS331AP_ODR_ADDR,
112                         .mask = ST_PRESS_LPS331AP_ODR_MASK,
113                         .odr_avl = {
114                                 { 1, ST_PRESS_LPS331AP_ODR_AVL_1HZ_VAL, },
115                                 { 7, ST_PRESS_LPS331AP_ODR_AVL_7HZ_VAL, },
116                                 { 13, ST_PRESS_LPS331AP_ODR_AVL_13HZ_VAL, },
117                                 { 25, ST_PRESS_LPS331AP_ODR_AVL_25HZ_VAL, },
118                         },
119                 },
120                 .pw = {
121                         .addr = ST_PRESS_LPS331AP_PW_ADDR,
122                         .mask = ST_PRESS_LPS331AP_PW_MASK,
123                         .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
124                         .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
125                 },
126                 .fs = {
127                         .addr = ST_PRESS_LPS331AP_FS_ADDR,
128                         .mask = ST_PRESS_LPS331AP_FS_MASK,
129                         .fs_avl = {
130                                 [0] = {
131                                         .num = ST_PRESS_FS_AVL_1260MB,
132                                         .value = ST_PRESS_LPS331AP_FS_AVL_1260_VAL,
133                                         .gain = ST_PRESS_LPS331AP_FS_AVL_1260_GAIN,
134                                         .gain2 = ST_PRESS_LPS331AP_FS_AVL_TEMP_GAIN,
135                                 },
136                         },
137                 },
138                 .bdu = {
139                         .addr = ST_PRESS_LPS331AP_BDU_ADDR,
140                         .mask = ST_PRESS_LPS331AP_BDU_MASK,
141                 },
142                 .drdy_irq = {
143                         .addr = ST_PRESS_LPS331AP_DRDY_IRQ_ADDR,
144                         .mask_int1 = ST_PRESS_LPS331AP_DRDY_IRQ_INT1_MASK,
145                         .mask_int2 = ST_PRESS_LPS331AP_DRDY_IRQ_INT2_MASK,
146                 },
147                 .multi_read_bit = ST_PRESS_LPS331AP_MULTIREAD_BIT,
148                 .bootime = 2,
149         },
150 };
151
152 static int st_press_read_raw(struct iio_dev *indio_dev,
153                         struct iio_chan_spec const *ch, int *val,
154                                                         int *val2, long mask)
155 {
156         int err;
157         struct st_sensor_data *pdata = iio_priv(indio_dev);
158
159         switch (mask) {
160         case IIO_CHAN_INFO_RAW:
161                 err = st_sensors_read_info_raw(indio_dev, ch, val);
162                 if (err < 0)
163                         goto read_error;
164
165                 return IIO_VAL_INT;
166         case IIO_CHAN_INFO_SCALE:
167                 *val = 0;
168
169                 switch (ch->type) {
170                 case IIO_PRESSURE:
171                         *val2 = pdata->current_fullscale->gain;
172                         break;
173                 case IIO_TEMP:
174                         *val2 = pdata->current_fullscale->gain2;
175                         break;
176                 default:
177                         err = -EINVAL;
178                         goto read_error;
179                 }
180
181                 return IIO_VAL_INT_PLUS_NANO;
182         case IIO_CHAN_INFO_OFFSET:
183                 switch (ch->type) {
184                 case IIO_TEMP:
185                         *val = 425;
186                         *val2 = 10;
187                         break;
188                 default:
189                         err = -EINVAL;
190                         goto read_error;
191                 }
192
193                 return IIO_VAL_FRACTIONAL;
194         default:
195                 return -EINVAL;
196         }
197
198 read_error:
199         return err;
200 }
201
202 static ST_SENSOR_DEV_ATTR_SAMP_FREQ();
203 static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL();
204
205 static struct attribute *st_press_attributes[] = {
206         &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
207         &iio_dev_attr_sampling_frequency.dev_attr.attr,
208         NULL,
209 };
210
211 static const struct attribute_group st_press_attribute_group = {
212         .attrs = st_press_attributes,
213 };
214
215 static const struct iio_info press_info = {
216         .driver_module = THIS_MODULE,
217         .attrs = &st_press_attribute_group,
218         .read_raw = &st_press_read_raw,
219 };
220
221 #ifdef CONFIG_IIO_TRIGGER
222 static const struct iio_trigger_ops st_press_trigger_ops = {
223         .owner = THIS_MODULE,
224         .set_trigger_state = ST_PRESS_TRIGGER_SET_STATE,
225 };
226 #define ST_PRESS_TRIGGER_OPS (&st_press_trigger_ops)
227 #else
228 #define ST_PRESS_TRIGGER_OPS NULL
229 #endif
230
231 int st_press_common_probe(struct iio_dev *indio_dev,
232                                 struct st_sensors_platform_data *plat_data)
233 {
234         int err;
235         struct st_sensor_data *pdata = iio_priv(indio_dev);
236
237         indio_dev->modes = INDIO_DIRECT_MODE;
238         indio_dev->info = &press_info;
239
240         err = st_sensors_check_device_support(indio_dev,
241                                 ARRAY_SIZE(st_press_sensors), st_press_sensors);
242         if (err < 0)
243                 goto st_press_common_probe_error;
244
245         pdata->num_data_channels = ST_PRESS_NUMBER_DATA_CHANNELS;
246         pdata->multiread_bit = pdata->sensor->multi_read_bit;
247         indio_dev->channels = pdata->sensor->ch;
248         indio_dev->num_channels = ARRAY_SIZE(st_press_lps331ap_channels);
249
250         if (pdata->sensor->fs.addr != 0)
251                 pdata->current_fullscale = (struct st_sensor_fullscale_avl *)
252                         &pdata->sensor->fs.fs_avl[0];
253
254         pdata->odr = pdata->sensor->odr.odr_avl[0].hz;
255
256         if (!plat_data)
257                 plat_data =
258                         (struct st_sensors_platform_data *)&default_press_pdata;
259
260         err = st_sensors_init_sensor(indio_dev, plat_data);
261         if (err < 0)
262                 goto st_press_common_probe_error;
263
264         if (pdata->get_irq_data_ready(indio_dev) > 0) {
265                 err = st_press_allocate_ring(indio_dev);
266                 if (err < 0)
267                         goto st_press_common_probe_error;
268
269                 err = st_sensors_allocate_trigger(indio_dev,
270                                                         ST_PRESS_TRIGGER_OPS);
271                 if (err < 0)
272                         goto st_press_probe_trigger_error;
273         }
274
275         err = iio_device_register(indio_dev);
276         if (err)
277                 goto st_press_device_register_error;
278
279         return err;
280
281 st_press_device_register_error:
282         if (pdata->get_irq_data_ready(indio_dev) > 0)
283                 st_sensors_deallocate_trigger(indio_dev);
284 st_press_probe_trigger_error:
285         if (pdata->get_irq_data_ready(indio_dev) > 0)
286                 st_press_deallocate_ring(indio_dev);
287 st_press_common_probe_error:
288         return err;
289 }
290 EXPORT_SYMBOL(st_press_common_probe);
291
292 void st_press_common_remove(struct iio_dev *indio_dev)
293 {
294         struct st_sensor_data *pdata = iio_priv(indio_dev);
295
296         iio_device_unregister(indio_dev);
297         if (pdata->get_irq_data_ready(indio_dev) > 0) {
298                 st_sensors_deallocate_trigger(indio_dev);
299                 st_press_deallocate_ring(indio_dev);
300         }
301 }
302 EXPORT_SYMBOL(st_press_common_remove);
303
304 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
305 MODULE_DESCRIPTION("STMicroelectronics pressures driver");
306 MODULE_LICENSE("GPL v2");