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