Commit | Line | Data |
---|---|---|
217494e5 DC |
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 | ||
67dbf54a JA |
31 | #define ST_PRESS_LSB_PER_MBAR 4096UL |
32 | #define ST_PRESS_KPASCAL_NANO_SCALE (100000000UL / \ | |
33 | ST_PRESS_LSB_PER_MBAR) | |
1003eb67 JA |
34 | #define ST_PRESS_LSB_PER_CELSIUS 480UL |
35 | #define ST_PRESS_CELSIUS_NANO_SCALE (1000000000UL / \ | |
36 | ST_PRESS_LSB_PER_CELSIUS) | |
217494e5 DC |
37 | #define ST_PRESS_NUMBER_DATA_CHANNELS 1 |
38 | ||
217494e5 DC |
39 | /* FULLSCALE */ |
40 | #define ST_PRESS_FS_AVL_1260MB 1260 | |
41 | ||
302fbd50 LJ |
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 | |
217494e5 | 66 | |
2f5effcb LJ |
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 = | |
217494e5 | 80 | BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), |
2f5effcb LJ |
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 | }, | |
217494e5 DC |
100 | IIO_CHAN_SOFT_TIMESTAMP(1) |
101 | }; | |
102 | ||
103 | static const struct st_sensors st_press_sensors[] = { | |
104 | { | |
302fbd50 | 105 | .wai = ST_PRESS_LPS331AP_WAI_EXP, |
217494e5 DC |
106 | .sensors_supported = { |
107 | [0] = LPS331AP_PRESS_DEV_NAME, | |
108 | }, | |
2f5effcb | 109 | .ch = (struct iio_chan_spec *)st_press_lps331ap_channels, |
ea01f2c1 | 110 | .num_ch = ARRAY_SIZE(st_press_lps331ap_channels), |
217494e5 | 111 | .odr = { |
302fbd50 LJ |
112 | .addr = ST_PRESS_LPS331AP_ODR_ADDR, |
113 | .mask = ST_PRESS_LPS331AP_ODR_MASK, | |
217494e5 | 114 | .odr_avl = { |
302fbd50 LJ |
115 | { 1, ST_PRESS_LPS331AP_ODR_AVL_1HZ_VAL, }, |
116 | { 7, ST_PRESS_LPS331AP_ODR_AVL_7HZ_VAL, }, | |
117 | { 13, ST_PRESS_LPS331AP_ODR_AVL_13HZ_VAL, }, | |
118 | { 25, ST_PRESS_LPS331AP_ODR_AVL_25HZ_VAL, }, | |
217494e5 DC |
119 | }, |
120 | }, | |
121 | .pw = { | |
302fbd50 LJ |
122 | .addr = ST_PRESS_LPS331AP_PW_ADDR, |
123 | .mask = ST_PRESS_LPS331AP_PW_MASK, | |
217494e5 DC |
124 | .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE, |
125 | .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE, | |
126 | }, | |
127 | .fs = { | |
302fbd50 LJ |
128 | .addr = ST_PRESS_LPS331AP_FS_ADDR, |
129 | .mask = ST_PRESS_LPS331AP_FS_MASK, | |
217494e5 DC |
130 | .fs_avl = { |
131 | [0] = { | |
132 | .num = ST_PRESS_FS_AVL_1260MB, | |
302fbd50 LJ |
133 | .value = ST_PRESS_LPS331AP_FS_AVL_1260_VAL, |
134 | .gain = ST_PRESS_LPS331AP_FS_AVL_1260_GAIN, | |
135 | .gain2 = ST_PRESS_LPS331AP_FS_AVL_TEMP_GAIN, | |
217494e5 DC |
136 | }, |
137 | }, | |
138 | }, | |
139 | .bdu = { | |
302fbd50 LJ |
140 | .addr = ST_PRESS_LPS331AP_BDU_ADDR, |
141 | .mask = ST_PRESS_LPS331AP_BDU_MASK, | |
217494e5 DC |
142 | }, |
143 | .drdy_irq = { | |
302fbd50 LJ |
144 | .addr = ST_PRESS_LPS331AP_DRDY_IRQ_ADDR, |
145 | .mask_int1 = ST_PRESS_LPS331AP_DRDY_IRQ_INT1_MASK, | |
146 | .mask_int2 = ST_PRESS_LPS331AP_DRDY_IRQ_INT2_MASK, | |
217494e5 | 147 | }, |
302fbd50 | 148 | .multi_read_bit = ST_PRESS_LPS331AP_MULTIREAD_BIT, |
217494e5 DC |
149 | .bootime = 2, |
150 | }, | |
151 | }; | |
152 | ||
153 | static int st_press_read_raw(struct iio_dev *indio_dev, | |
154 | struct iio_chan_spec const *ch, int *val, | |
155 | int *val2, long mask) | |
156 | { | |
157 | int err; | |
158 | struct st_sensor_data *pdata = iio_priv(indio_dev); | |
159 | ||
160 | switch (mask) { | |
161 | case IIO_CHAN_INFO_RAW: | |
162 | err = st_sensors_read_info_raw(indio_dev, ch, val); | |
163 | if (err < 0) | |
164 | goto read_error; | |
165 | ||
166 | return IIO_VAL_INT; | |
167 | case IIO_CHAN_INFO_SCALE: | |
168 | *val = 0; | |
169 | ||
170 | switch (ch->type) { | |
171 | case IIO_PRESSURE: | |
172 | *val2 = pdata->current_fullscale->gain; | |
173 | break; | |
174 | case IIO_TEMP: | |
175 | *val2 = pdata->current_fullscale->gain2; | |
176 | break; | |
177 | default: | |
178 | err = -EINVAL; | |
179 | goto read_error; | |
180 | } | |
181 | ||
182 | return IIO_VAL_INT_PLUS_NANO; | |
183 | case IIO_CHAN_INFO_OFFSET: | |
184 | switch (ch->type) { | |
185 | case IIO_TEMP: | |
186 | *val = 425; | |
187 | *val2 = 10; | |
188 | break; | |
189 | default: | |
190 | err = -EINVAL; | |
191 | goto read_error; | |
192 | } | |
193 | ||
194 | return IIO_VAL_FRACTIONAL; | |
195 | default: | |
196 | return -EINVAL; | |
197 | } | |
198 | ||
199 | read_error: | |
200 | return err; | |
201 | } | |
202 | ||
203 | static ST_SENSOR_DEV_ATTR_SAMP_FREQ(); | |
204 | static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL(); | |
205 | ||
206 | static struct attribute *st_press_attributes[] = { | |
207 | &iio_dev_attr_sampling_frequency_available.dev_attr.attr, | |
208 | &iio_dev_attr_sampling_frequency.dev_attr.attr, | |
209 | NULL, | |
210 | }; | |
211 | ||
212 | static const struct attribute_group st_press_attribute_group = { | |
213 | .attrs = st_press_attributes, | |
214 | }; | |
215 | ||
216 | static const struct iio_info press_info = { | |
217 | .driver_module = THIS_MODULE, | |
218 | .attrs = &st_press_attribute_group, | |
219 | .read_raw = &st_press_read_raw, | |
220 | }; | |
221 | ||
222 | #ifdef CONFIG_IIO_TRIGGER | |
223 | static const struct iio_trigger_ops st_press_trigger_ops = { | |
224 | .owner = THIS_MODULE, | |
225 | .set_trigger_state = ST_PRESS_TRIGGER_SET_STATE, | |
226 | }; | |
227 | #define ST_PRESS_TRIGGER_OPS (&st_press_trigger_ops) | |
228 | #else | |
229 | #define ST_PRESS_TRIGGER_OPS NULL | |
230 | #endif | |
231 | ||
23cde4d6 DC |
232 | int st_press_common_probe(struct iio_dev *indio_dev, |
233 | struct st_sensors_platform_data *plat_data) | |
217494e5 DC |
234 | { |
235 | int err; | |
236 | struct st_sensor_data *pdata = iio_priv(indio_dev); | |
237 | ||
238 | indio_dev->modes = INDIO_DIRECT_MODE; | |
239 | indio_dev->info = &press_info; | |
240 | ||
241 | err = st_sensors_check_device_support(indio_dev, | |
242 | ARRAY_SIZE(st_press_sensors), st_press_sensors); | |
243 | if (err < 0) | |
244 | goto st_press_common_probe_error; | |
245 | ||
246 | pdata->num_data_channels = ST_PRESS_NUMBER_DATA_CHANNELS; | |
247 | pdata->multiread_bit = pdata->sensor->multi_read_bit; | |
248 | indio_dev->channels = pdata->sensor->ch; | |
ea01f2c1 | 249 | indio_dev->num_channels = pdata->sensor->num_ch; |
217494e5 | 250 | |
362f2f86 LJ |
251 | if (pdata->sensor->fs.addr != 0) |
252 | pdata->current_fullscale = (struct st_sensor_fullscale_avl *) | |
253 | &pdata->sensor->fs.fs_avl[0]; | |
254 | ||
217494e5 DC |
255 | pdata->odr = pdata->sensor->odr.odr_avl[0].hz; |
256 | ||
23cde4d6 DC |
257 | if (!plat_data) |
258 | plat_data = | |
259 | (struct st_sensors_platform_data *)&default_press_pdata; | |
260 | ||
261 | err = st_sensors_init_sensor(indio_dev, plat_data); | |
217494e5 DC |
262 | if (err < 0) |
263 | goto st_press_common_probe_error; | |
264 | ||
265 | if (pdata->get_irq_data_ready(indio_dev) > 0) { | |
266 | err = st_press_allocate_ring(indio_dev); | |
267 | if (err < 0) | |
268 | goto st_press_common_probe_error; | |
269 | ||
270 | err = st_sensors_allocate_trigger(indio_dev, | |
271 | ST_PRESS_TRIGGER_OPS); | |
272 | if (err < 0) | |
273 | goto st_press_probe_trigger_error; | |
274 | } | |
275 | ||
276 | err = iio_device_register(indio_dev); | |
277 | if (err) | |
278 | goto st_press_device_register_error; | |
279 | ||
280 | return err; | |
281 | ||
282 | st_press_device_register_error: | |
283 | if (pdata->get_irq_data_ready(indio_dev) > 0) | |
284 | st_sensors_deallocate_trigger(indio_dev); | |
285 | st_press_probe_trigger_error: | |
286 | if (pdata->get_irq_data_ready(indio_dev) > 0) | |
287 | st_press_deallocate_ring(indio_dev); | |
288 | st_press_common_probe_error: | |
289 | return err; | |
290 | } | |
291 | EXPORT_SYMBOL(st_press_common_probe); | |
292 | ||
293 | void st_press_common_remove(struct iio_dev *indio_dev) | |
294 | { | |
295 | struct st_sensor_data *pdata = iio_priv(indio_dev); | |
296 | ||
297 | iio_device_unregister(indio_dev); | |
298 | if (pdata->get_irq_data_ready(indio_dev) > 0) { | |
299 | st_sensors_deallocate_trigger(indio_dev); | |
300 | st_press_deallocate_ring(indio_dev); | |
301 | } | |
217494e5 DC |
302 | } |
303 | EXPORT_SYMBOL(st_press_common_remove); | |
304 | ||
305 | MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>"); | |
306 | MODULE_DESCRIPTION("STMicroelectronics pressures driver"); | |
307 | MODULE_LICENSE("GPL v2"); |