xref: /linux/drivers/iio/pressure/st_pressure_core.c (revision e0bf6c5ca2d3281f231c5f0c9bf145e9513644de)
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 #define ST_PRESS_1_OUT_XL_ADDR			0x28
43 #define ST_TEMP_1_OUT_L_ADDR			0x2b
44 
45 /* CUSTOM VALUES FOR LPS331AP SENSOR */
46 #define ST_PRESS_LPS331AP_WAI_EXP		0xbb
47 #define ST_PRESS_LPS331AP_ODR_ADDR		0x20
48 #define ST_PRESS_LPS331AP_ODR_MASK		0x70
49 #define ST_PRESS_LPS331AP_ODR_AVL_1HZ_VAL	0x01
50 #define ST_PRESS_LPS331AP_ODR_AVL_7HZ_VAL	0x05
51 #define ST_PRESS_LPS331AP_ODR_AVL_13HZ_VAL	0x06
52 #define ST_PRESS_LPS331AP_ODR_AVL_25HZ_VAL	0x07
53 #define ST_PRESS_LPS331AP_PW_ADDR		0x20
54 #define ST_PRESS_LPS331AP_PW_MASK		0x80
55 #define ST_PRESS_LPS331AP_FS_ADDR		0x23
56 #define ST_PRESS_LPS331AP_FS_MASK		0x30
57 #define ST_PRESS_LPS331AP_FS_AVL_1260_VAL	0x00
58 #define ST_PRESS_LPS331AP_FS_AVL_1260_GAIN	ST_PRESS_KPASCAL_NANO_SCALE
59 #define ST_PRESS_LPS331AP_FS_AVL_TEMP_GAIN	ST_PRESS_CELSIUS_NANO_SCALE
60 #define ST_PRESS_LPS331AP_BDU_ADDR		0x20
61 #define ST_PRESS_LPS331AP_BDU_MASK		0x04
62 #define ST_PRESS_LPS331AP_DRDY_IRQ_ADDR		0x22
63 #define ST_PRESS_LPS331AP_DRDY_IRQ_INT1_MASK	0x04
64 #define ST_PRESS_LPS331AP_DRDY_IRQ_INT2_MASK	0x20
65 #define ST_PRESS_LPS331AP_MULTIREAD_BIT		true
66 #define ST_PRESS_LPS331AP_TEMP_OFFSET		42500
67 
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
82 
83 /* CUSTOM VALUES FOR LPS25H SENSOR */
84 #define ST_PRESS_LPS25H_WAI_EXP			0xbd
85 #define ST_PRESS_LPS25H_ODR_ADDR		0x20
86 #define ST_PRESS_LPS25H_ODR_MASK		0x70
87 #define ST_PRESS_LPS25H_ODR_AVL_1HZ_VAL		0x01
88 #define ST_PRESS_LPS25H_ODR_AVL_7HZ_VAL		0x02
89 #define ST_PRESS_LPS25H_ODR_AVL_13HZ_VAL	0x03
90 #define ST_PRESS_LPS25H_ODR_AVL_25HZ_VAL	0x04
91 #define ST_PRESS_LPS25H_PW_ADDR			0x20
92 #define ST_PRESS_LPS25H_PW_MASK			0x80
93 #define ST_PRESS_LPS25H_FS_ADDR			0x00
94 #define ST_PRESS_LPS25H_FS_MASK			0x00
95 #define ST_PRESS_LPS25H_FS_AVL_1260_VAL		0x00
96 #define ST_PRESS_LPS25H_FS_AVL_1260_GAIN	ST_PRESS_KPASCAL_NANO_SCALE
97 #define ST_PRESS_LPS25H_FS_AVL_TEMP_GAIN	ST_PRESS_CELSIUS_NANO_SCALE
98 #define ST_PRESS_LPS25H_BDU_ADDR		0x20
99 #define ST_PRESS_LPS25H_BDU_MASK		0x04
100 #define ST_PRESS_LPS25H_DRDY_IRQ_ADDR		0x23
101 #define ST_PRESS_LPS25H_DRDY_IRQ_INT1_MASK	0x01
102 #define ST_PRESS_LPS25H_DRDY_IRQ_INT2_MASK	0x10
103 #define ST_PRESS_LPS25H_MULTIREAD_BIT		true
104 #define ST_PRESS_LPS25H_TEMP_OFFSET		42500
105 #define ST_PRESS_LPS25H_OUT_XL_ADDR		0x28
106 #define ST_TEMP_LPS25H_OUT_L_ADDR		0x2b
107 
108 static const struct iio_chan_spec st_press_1_channels[] = {
109 	{
110 		.type = IIO_PRESSURE,
111 		.channel2 = IIO_NO_MOD,
112 		.address = ST_PRESS_1_OUT_XL_ADDR,
113 		.scan_index = ST_SENSORS_SCAN_X,
114 		.scan_type = {
115 			.sign = 'u',
116 			.realbits = 24,
117 			.storagebits = 24,
118 			.endianness = IIO_LE,
119 		},
120 		.info_mask_separate =
121 			BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
122 		.modified = 0,
123 	},
124 	{
125 		.type = IIO_TEMP,
126 		.channel2 = IIO_NO_MOD,
127 		.address = ST_TEMP_1_OUT_L_ADDR,
128 		.scan_index = -1,
129 		.scan_type = {
130 			.sign = 'u',
131 			.realbits = 16,
132 			.storagebits = 16,
133 			.endianness = IIO_LE,
134 		},
135 		.info_mask_separate =
136 			BIT(IIO_CHAN_INFO_RAW) |
137 			BIT(IIO_CHAN_INFO_SCALE) |
138 			BIT(IIO_CHAN_INFO_OFFSET),
139 		.modified = 0,
140 	},
141 	IIO_CHAN_SOFT_TIMESTAMP(1)
142 };
143 
144 static const struct iio_chan_spec st_press_lps001wp_channels[] = {
145 	{
146 		.type = IIO_PRESSURE,
147 		.channel2 = IIO_NO_MOD,
148 		.address = ST_PRESS_LPS001WP_OUT_L_ADDR,
149 		.scan_index = ST_SENSORS_SCAN_X,
150 		.scan_type = {
151 			.sign = 'u',
152 			.realbits = 16,
153 			.storagebits = 16,
154 			.endianness = IIO_LE,
155 		},
156 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
157 		.modified = 0,
158 	},
159 	{
160 		.type = IIO_TEMP,
161 		.channel2 = IIO_NO_MOD,
162 		.address = ST_TEMP_LPS001WP_OUT_L_ADDR,
163 		.scan_index = -1,
164 		.scan_type = {
165 			.sign = 'u',
166 			.realbits = 16,
167 			.storagebits = 16,
168 			.endianness = IIO_LE,
169 		},
170 		.info_mask_separate =
171 			BIT(IIO_CHAN_INFO_RAW) |
172 			BIT(IIO_CHAN_INFO_OFFSET),
173 		.modified = 0,
174 	},
175 	IIO_CHAN_SOFT_TIMESTAMP(1)
176 };
177 
178 static const struct st_sensor_settings st_press_sensors_settings[] = {
179 	{
180 		.wai = ST_PRESS_LPS331AP_WAI_EXP,
181 		.sensors_supported = {
182 			[0] = LPS331AP_PRESS_DEV_NAME,
183 		},
184 		.ch = (struct iio_chan_spec *)st_press_1_channels,
185 		.num_ch = ARRAY_SIZE(st_press_1_channels),
186 		.odr = {
187 			.addr = ST_PRESS_LPS331AP_ODR_ADDR,
188 			.mask = ST_PRESS_LPS331AP_ODR_MASK,
189 			.odr_avl = {
190 				{ 1, ST_PRESS_LPS331AP_ODR_AVL_1HZ_VAL, },
191 				{ 7, ST_PRESS_LPS331AP_ODR_AVL_7HZ_VAL, },
192 				{ 13, ST_PRESS_LPS331AP_ODR_AVL_13HZ_VAL, },
193 				{ 25, ST_PRESS_LPS331AP_ODR_AVL_25HZ_VAL, },
194 			},
195 		},
196 		.pw = {
197 			.addr = ST_PRESS_LPS331AP_PW_ADDR,
198 			.mask = ST_PRESS_LPS331AP_PW_MASK,
199 			.value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
200 			.value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
201 		},
202 		.fs = {
203 			.addr = ST_PRESS_LPS331AP_FS_ADDR,
204 			.mask = ST_PRESS_LPS331AP_FS_MASK,
205 			.fs_avl = {
206 				[0] = {
207 					.num = ST_PRESS_FS_AVL_1260MB,
208 					.value = ST_PRESS_LPS331AP_FS_AVL_1260_VAL,
209 					.gain = ST_PRESS_LPS331AP_FS_AVL_1260_GAIN,
210 					.gain2 = ST_PRESS_LPS331AP_FS_AVL_TEMP_GAIN,
211 				},
212 			},
213 		},
214 		.bdu = {
215 			.addr = ST_PRESS_LPS331AP_BDU_ADDR,
216 			.mask = ST_PRESS_LPS331AP_BDU_MASK,
217 		},
218 		.drdy_irq = {
219 			.addr = ST_PRESS_LPS331AP_DRDY_IRQ_ADDR,
220 			.mask_int1 = ST_PRESS_LPS331AP_DRDY_IRQ_INT1_MASK,
221 			.mask_int2 = ST_PRESS_LPS331AP_DRDY_IRQ_INT2_MASK,
222 		},
223 		.multi_read_bit = ST_PRESS_LPS331AP_MULTIREAD_BIT,
224 		.bootime = 2,
225 	},
226 	{
227 		.wai = ST_PRESS_LPS001WP_WAI_EXP,
228 		.sensors_supported = {
229 			[0] = LPS001WP_PRESS_DEV_NAME,
230 		},
231 		.ch = (struct iio_chan_spec *)st_press_lps001wp_channels,
232 		.num_ch = ARRAY_SIZE(st_press_lps001wp_channels),
233 		.odr = {
234 			.addr = ST_PRESS_LPS001WP_ODR_ADDR,
235 			.mask = ST_PRESS_LPS001WP_ODR_MASK,
236 			.odr_avl = {
237 				{ 1, ST_PRESS_LPS001WP_ODR_AVL_1HZ_VAL, },
238 				{ 7, ST_PRESS_LPS001WP_ODR_AVL_7HZ_VAL, },
239 				{ 13, ST_PRESS_LPS001WP_ODR_AVL_13HZ_VAL, },
240 			},
241 		},
242 		.pw = {
243 			.addr = ST_PRESS_LPS001WP_PW_ADDR,
244 			.mask = ST_PRESS_LPS001WP_PW_MASK,
245 			.value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
246 			.value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
247 		},
248 		.fs = {
249 			.addr = 0,
250 		},
251 		.bdu = {
252 			.addr = ST_PRESS_LPS001WP_BDU_ADDR,
253 			.mask = ST_PRESS_LPS001WP_BDU_MASK,
254 		},
255 		.drdy_irq = {
256 			.addr = 0,
257 		},
258 		.multi_read_bit = ST_PRESS_LPS001WP_MULTIREAD_BIT,
259 		.bootime = 2,
260 	},
261 	{
262 		.wai = ST_PRESS_LPS25H_WAI_EXP,
263 		.sensors_supported = {
264 			[0] = LPS25H_PRESS_DEV_NAME,
265 		},
266 		.ch = (struct iio_chan_spec *)st_press_1_channels,
267 		.num_ch = ARRAY_SIZE(st_press_1_channels),
268 		.odr = {
269 			.addr = ST_PRESS_LPS25H_ODR_ADDR,
270 			.mask = ST_PRESS_LPS25H_ODR_MASK,
271 			.odr_avl = {
272 				{ 1, ST_PRESS_LPS25H_ODR_AVL_1HZ_VAL, },
273 				{ 7, ST_PRESS_LPS25H_ODR_AVL_7HZ_VAL, },
274 				{ 13, ST_PRESS_LPS25H_ODR_AVL_13HZ_VAL, },
275 				{ 25, ST_PRESS_LPS25H_ODR_AVL_25HZ_VAL, },
276 			},
277 		},
278 		.pw = {
279 			.addr = ST_PRESS_LPS25H_PW_ADDR,
280 			.mask = ST_PRESS_LPS25H_PW_MASK,
281 			.value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
282 			.value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
283 		},
284 		.fs = {
285 			.addr = ST_PRESS_LPS25H_FS_ADDR,
286 			.mask = ST_PRESS_LPS25H_FS_MASK,
287 			.fs_avl = {
288 				[0] = {
289 					.num = ST_PRESS_FS_AVL_1260MB,
290 					.value = ST_PRESS_LPS25H_FS_AVL_1260_VAL,
291 					.gain = ST_PRESS_LPS25H_FS_AVL_1260_GAIN,
292 					.gain2 = ST_PRESS_LPS25H_FS_AVL_TEMP_GAIN,
293 				},
294 			},
295 		},
296 		.bdu = {
297 			.addr = ST_PRESS_LPS25H_BDU_ADDR,
298 			.mask = ST_PRESS_LPS25H_BDU_MASK,
299 		},
300 		.drdy_irq = {
301 			.addr = ST_PRESS_LPS25H_DRDY_IRQ_ADDR,
302 			.mask_int1 = ST_PRESS_LPS25H_DRDY_IRQ_INT1_MASK,
303 			.mask_int2 = ST_PRESS_LPS25H_DRDY_IRQ_INT2_MASK,
304 		},
305 		.multi_read_bit = ST_PRESS_LPS25H_MULTIREAD_BIT,
306 		.bootime = 2,
307 	},
308 };
309 
310 static int st_press_write_raw(struct iio_dev *indio_dev,
311 			      struct iio_chan_spec const *ch,
312 			      int val,
313 			      int val2,
314 			      long mask)
315 {
316 	int err;
317 
318 	switch (mask) {
319 	case IIO_CHAN_INFO_SAMP_FREQ:
320 		if (val2)
321 			return -EINVAL;
322 		mutex_lock(&indio_dev->mlock);
323 		err = st_sensors_set_odr(indio_dev, val);
324 		mutex_unlock(&indio_dev->mlock);
325 		return err;
326 	default:
327 		return -EINVAL;
328 	}
329 }
330 
331 static int st_press_read_raw(struct iio_dev *indio_dev,
332 			struct iio_chan_spec const *ch, int *val,
333 							int *val2, long mask)
334 {
335 	int err;
336 	struct st_sensor_data *press_data = iio_priv(indio_dev);
337 
338 	switch (mask) {
339 	case IIO_CHAN_INFO_RAW:
340 		err = st_sensors_read_info_raw(indio_dev, ch, val);
341 		if (err < 0)
342 			goto read_error;
343 
344 		return IIO_VAL_INT;
345 	case IIO_CHAN_INFO_SCALE:
346 		*val = 0;
347 
348 		switch (ch->type) {
349 		case IIO_PRESSURE:
350 			*val2 = press_data->current_fullscale->gain;
351 			break;
352 		case IIO_TEMP:
353 			*val2 = press_data->current_fullscale->gain2;
354 			break;
355 		default:
356 			err = -EINVAL;
357 			goto read_error;
358 		}
359 
360 		return IIO_VAL_INT_PLUS_NANO;
361 	case IIO_CHAN_INFO_OFFSET:
362 		switch (ch->type) {
363 		case IIO_TEMP:
364 			*val = 425;
365 			*val2 = 10;
366 			break;
367 		default:
368 			err = -EINVAL;
369 			goto read_error;
370 		}
371 
372 		return IIO_VAL_FRACTIONAL;
373 	case IIO_CHAN_INFO_SAMP_FREQ:
374 		*val = press_data->odr;
375 		return IIO_VAL_INT;
376 	default:
377 		return -EINVAL;
378 	}
379 
380 read_error:
381 	return err;
382 }
383 
384 static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL();
385 
386 static struct attribute *st_press_attributes[] = {
387 	&iio_dev_attr_sampling_frequency_available.dev_attr.attr,
388 	NULL,
389 };
390 
391 static const struct attribute_group st_press_attribute_group = {
392 	.attrs = st_press_attributes,
393 };
394 
395 static const struct iio_info press_info = {
396 	.driver_module = THIS_MODULE,
397 	.attrs = &st_press_attribute_group,
398 	.read_raw = &st_press_read_raw,
399 	.write_raw = &st_press_write_raw,
400 };
401 
402 #ifdef CONFIG_IIO_TRIGGER
403 static const struct iio_trigger_ops st_press_trigger_ops = {
404 	.owner = THIS_MODULE,
405 	.set_trigger_state = ST_PRESS_TRIGGER_SET_STATE,
406 };
407 #define ST_PRESS_TRIGGER_OPS (&st_press_trigger_ops)
408 #else
409 #define ST_PRESS_TRIGGER_OPS NULL
410 #endif
411 
412 int st_press_common_probe(struct iio_dev *indio_dev)
413 {
414 	struct st_sensor_data *press_data = iio_priv(indio_dev);
415 	int irq = press_data->get_irq_data_ready(indio_dev);
416 	int err;
417 
418 	indio_dev->modes = INDIO_DIRECT_MODE;
419 	indio_dev->info = &press_info;
420 
421 	st_sensors_power_enable(indio_dev);
422 
423 	err = st_sensors_check_device_support(indio_dev,
424 					ARRAY_SIZE(st_press_sensors_settings),
425 					st_press_sensors_settings);
426 	if (err < 0)
427 		return err;
428 
429 	press_data->num_data_channels = ST_PRESS_NUMBER_DATA_CHANNELS;
430 	press_data->multiread_bit = press_data->sensor_settings->multi_read_bit;
431 	indio_dev->channels = press_data->sensor_settings->ch;
432 	indio_dev->num_channels = press_data->sensor_settings->num_ch;
433 
434 	if (press_data->sensor_settings->fs.addr != 0)
435 		press_data->current_fullscale =
436 			(struct st_sensor_fullscale_avl *)
437 				&press_data->sensor_settings->fs.fs_avl[0];
438 
439 	press_data->odr = press_data->sensor_settings->odr.odr_avl[0].hz;
440 
441 	/* Some devices don't support a data ready pin. */
442 	if (!press_data->dev->platform_data &&
443 				press_data->sensor_settings->drdy_irq.addr)
444 		press_data->dev->platform_data =
445 			(struct st_sensors_platform_data *)&default_press_pdata;
446 
447 	err = st_sensors_init_sensor(indio_dev, press_data->dev->platform_data);
448 	if (err < 0)
449 		return err;
450 
451 	err = st_press_allocate_ring(indio_dev);
452 	if (err < 0)
453 		return err;
454 
455 	if (irq > 0) {
456 		err = st_sensors_allocate_trigger(indio_dev,
457 						  ST_PRESS_TRIGGER_OPS);
458 		if (err < 0)
459 			goto st_press_probe_trigger_error;
460 	}
461 
462 	err = iio_device_register(indio_dev);
463 	if (err)
464 		goto st_press_device_register_error;
465 
466 	dev_info(&indio_dev->dev, "registered pressure sensor %s\n",
467 		 indio_dev->name);
468 
469 	return err;
470 
471 st_press_device_register_error:
472 	if (irq > 0)
473 		st_sensors_deallocate_trigger(indio_dev);
474 st_press_probe_trigger_error:
475 	st_press_deallocate_ring(indio_dev);
476 
477 	return err;
478 }
479 EXPORT_SYMBOL(st_press_common_probe);
480 
481 void st_press_common_remove(struct iio_dev *indio_dev)
482 {
483 	struct st_sensor_data *press_data = iio_priv(indio_dev);
484 
485 	st_sensors_power_disable(indio_dev);
486 
487 	iio_device_unregister(indio_dev);
488 	if (press_data->get_irq_data_ready(indio_dev) > 0)
489 		st_sensors_deallocate_trigger(indio_dev);
490 
491 	st_press_deallocate_ring(indio_dev);
492 }
493 EXPORT_SYMBOL(st_press_common_remove);
494 
495 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
496 MODULE_DESCRIPTION("STMicroelectronics pressures driver");
497 MODULE_LICENSE("GPL v2");
498