sh_eth: fix EESIPR values for SH77{34|63}
[linux/fpc-iii.git] / drivers / iio / pressure / st_pressure_core.c
blobe19e0787864cf436f2db208e5462f13c31a4cd2f
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 <asm/unaligned.h>
28 #include <linux/iio/common/st_sensors.h>
29 #include "st_pressure.h"
32 * About determining pressure scaling factors
33 * ------------------------------------------
35 * Datasheets specify typical pressure sensitivity so that pressure is computed
36 * according to the following equation :
37 * pressure[mBar] = raw / sensitivity
38 * where :
39 * raw the 24 bits long raw sampled pressure
40 * sensitivity a scaling factor specified by the datasheet in LSB/mBar
42 * IIO ABI expects pressure to be expressed as kPascal, hence pressure should be
43 * computed according to :
44 * pressure[kPascal] = pressure[mBar] / 10
45 * = raw / (sensitivity * 10) (1)
47 * Finally, st_press_read_raw() returns pressure scaling factor as an
48 * IIO_VAL_INT_PLUS_NANO with a zero integral part and "gain" as decimal part.
49 * Therefore, from (1), "gain" becomes :
50 * gain = 10^9 / (sensitivity * 10)
51 * = 10^8 / sensitivity
53 * About determining temperature scaling factors and offsets
54 * ---------------------------------------------------------
56 * Datasheets specify typical temperature sensitivity and offset so that
57 * temperature is computed according to the following equation :
58 * temp[Celsius] = offset[Celsius] + (raw / sensitivity)
59 * where :
60 * raw the 16 bits long raw sampled temperature
61 * offset a constant specified by the datasheet in degree Celsius
62 * (sometimes zero)
63 * sensitivity a scaling factor specified by the datasheet in LSB/Celsius
65 * IIO ABI expects temperature to be expressed as milli degree Celsius such as
66 * user space should compute temperature according to :
67 * temp[mCelsius] = temp[Celsius] * 10^3
68 * = (offset[Celsius] + (raw / sensitivity)) * 10^3
69 * = ((offset[Celsius] * sensitivity) + raw) *
70 * (10^3 / sensitivity) (2)
72 * IIO ABI expects user space to apply offset and scaling factors to raw samples
73 * according to :
74 * temp[mCelsius] = (OFFSET + raw) * SCALE
75 * where :
76 * OFFSET an arbitrary constant exposed by device
77 * SCALE an arbitrary scaling factor exposed by device
79 * Matching OFFSET and SCALE with members of (2) gives :
80 * OFFSET = offset[Celsius] * sensitivity (3)
81 * SCALE = 10^3 / sensitivity (4)
83 * st_press_read_raw() returns temperature scaling factor as an
84 * IIO_VAL_FRACTIONAL with a 10^3 numerator and "gain2" as denominator.
85 * Therefore, from (3), "gain2" becomes :
86 * gain2 = sensitivity
88 * When declared within channel, i.e. for a non zero specified offset,
89 * st_press_read_raw() will return the latter as an IIO_VAL_FRACTIONAL such as :
90 * numerator = OFFSET * 10^3
91 * denominator = 10^3
92 * giving from (4):
93 * numerator = offset[Celsius] * 10^3 * sensitivity
94 * = offset[mCelsius] * gain2
97 #define MCELSIUS_PER_CELSIUS 1000
99 /* Default pressure sensitivity */
100 #define ST_PRESS_LSB_PER_MBAR 4096UL
101 #define ST_PRESS_KPASCAL_NANO_SCALE (100000000UL / \
102 ST_PRESS_LSB_PER_MBAR)
104 /* Default temperature sensitivity */
105 #define ST_PRESS_LSB_PER_CELSIUS 480UL
106 #define ST_PRESS_MILLI_CELSIUS_OFFSET 42500UL
108 /* FULLSCALE */
109 #define ST_PRESS_FS_AVL_1100MB 1100
110 #define ST_PRESS_FS_AVL_1260MB 1260
112 #define ST_PRESS_1_OUT_XL_ADDR 0x28
113 #define ST_TEMP_1_OUT_L_ADDR 0x2b
115 /* LPS001WP pressure resolution */
116 #define ST_PRESS_LPS001WP_LSB_PER_MBAR 16UL
117 /* LPS001WP temperature resolution */
118 #define ST_PRESS_LPS001WP_LSB_PER_CELSIUS 64UL
119 /* LPS001WP pressure gain */
120 #define ST_PRESS_LPS001WP_FS_AVL_PRESS_GAIN \
121 (100000000UL / ST_PRESS_LPS001WP_LSB_PER_MBAR)
122 /* LPS001WP pressure and temp L addresses */
123 #define ST_PRESS_LPS001WP_OUT_L_ADDR 0x28
124 #define ST_TEMP_LPS001WP_OUT_L_ADDR 0x2a
126 /* LPS25H pressure and temp L addresses */
127 #define ST_PRESS_LPS25H_OUT_XL_ADDR 0x28
128 #define ST_TEMP_LPS25H_OUT_L_ADDR 0x2b
130 /* LPS22HB temperature sensitivity */
131 #define ST_PRESS_LPS22HB_LSB_PER_CELSIUS 100UL
133 static const struct iio_chan_spec st_press_1_channels[] = {
135 .type = IIO_PRESSURE,
136 .address = ST_PRESS_1_OUT_XL_ADDR,
137 .scan_index = 0,
138 .scan_type = {
139 .sign = 'u',
140 .realbits = 24,
141 .storagebits = 32,
142 .endianness = IIO_LE,
144 .info_mask_separate =
145 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
148 .type = IIO_TEMP,
149 .address = ST_TEMP_1_OUT_L_ADDR,
150 .scan_index = 1,
151 .scan_type = {
152 .sign = 'u',
153 .realbits = 16,
154 .storagebits = 16,
155 .endianness = IIO_LE,
157 .info_mask_separate =
158 BIT(IIO_CHAN_INFO_RAW) |
159 BIT(IIO_CHAN_INFO_SCALE) |
160 BIT(IIO_CHAN_INFO_OFFSET),
162 IIO_CHAN_SOFT_TIMESTAMP(2)
165 static const struct iio_chan_spec st_press_lps001wp_channels[] = {
167 .type = IIO_PRESSURE,
168 .address = ST_PRESS_LPS001WP_OUT_L_ADDR,
169 .scan_index = 0,
170 .scan_type = {
171 .sign = 'u',
172 .realbits = 16,
173 .storagebits = 16,
174 .endianness = IIO_LE,
176 .info_mask_separate =
177 BIT(IIO_CHAN_INFO_RAW) |
178 BIT(IIO_CHAN_INFO_SCALE),
181 .type = IIO_TEMP,
182 .address = ST_TEMP_LPS001WP_OUT_L_ADDR,
183 .scan_index = 1,
184 .scan_type = {
185 .sign = 'u',
186 .realbits = 16,
187 .storagebits = 16,
188 .endianness = IIO_LE,
190 .info_mask_separate =
191 BIT(IIO_CHAN_INFO_RAW) |
192 BIT(IIO_CHAN_INFO_SCALE),
194 IIO_CHAN_SOFT_TIMESTAMP(2)
197 static const struct iio_chan_spec st_press_lps22hb_channels[] = {
199 .type = IIO_PRESSURE,
200 .address = ST_PRESS_1_OUT_XL_ADDR,
201 .scan_index = 0,
202 .scan_type = {
203 .sign = 'u',
204 .realbits = 24,
205 .storagebits = 32,
206 .endianness = IIO_LE,
208 .info_mask_separate =
209 BIT(IIO_CHAN_INFO_RAW) |
210 BIT(IIO_CHAN_INFO_SCALE),
211 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
214 .type = IIO_TEMP,
215 .address = ST_TEMP_1_OUT_L_ADDR,
216 .scan_index = 1,
217 .scan_type = {
218 .sign = 's',
219 .realbits = 16,
220 .storagebits = 16,
221 .endianness = IIO_LE,
223 .info_mask_separate =
224 BIT(IIO_CHAN_INFO_RAW) |
225 BIT(IIO_CHAN_INFO_SCALE),
226 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
228 IIO_CHAN_SOFT_TIMESTAMP(2)
231 static const struct st_sensor_settings st_press_sensors_settings[] = {
234 * CUSTOM VALUES FOR LPS331AP SENSOR
235 * See LPS331AP datasheet:
236 * http://www2.st.com/resource/en/datasheet/lps331ap.pdf
238 .wai = 0xbb,
239 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
240 .sensors_supported = {
241 [0] = LPS331AP_PRESS_DEV_NAME,
243 .ch = (struct iio_chan_spec *)st_press_1_channels,
244 .num_ch = ARRAY_SIZE(st_press_1_channels),
245 .odr = {
246 .addr = 0x20,
247 .mask = 0x70,
248 .odr_avl = {
249 { .hz = 1, .value = 0x01 },
250 { .hz = 7, .value = 0x05 },
251 { .hz = 13, .value = 0x06 },
252 { .hz = 25, .value = 0x07 },
255 .pw = {
256 .addr = 0x20,
257 .mask = 0x80,
258 .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
259 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
261 .fs = {
262 .addr = 0x23,
263 .mask = 0x30,
264 .fs_avl = {
266 * Pressure and temperature sensitivity values
267 * as defined in table 3 of LPS331AP datasheet.
269 [0] = {
270 .num = ST_PRESS_FS_AVL_1260MB,
271 .gain = ST_PRESS_KPASCAL_NANO_SCALE,
272 .gain2 = ST_PRESS_LSB_PER_CELSIUS,
276 .bdu = {
277 .addr = 0x20,
278 .mask = 0x04,
280 .drdy_irq = {
281 .addr = 0x22,
282 .mask_int1 = 0x04,
283 .mask_int2 = 0x20,
284 .addr_ihl = 0x22,
285 .mask_ihl = 0x80,
286 .addr_od = 0x22,
287 .mask_od = 0x40,
288 .addr_stat_drdy = ST_SENSORS_DEFAULT_STAT_ADDR,
290 .multi_read_bit = true,
291 .bootime = 2,
295 * CUSTOM VALUES FOR LPS001WP SENSOR
297 .wai = 0xba,
298 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
299 .sensors_supported = {
300 [0] = LPS001WP_PRESS_DEV_NAME,
302 .ch = (struct iio_chan_spec *)st_press_lps001wp_channels,
303 .num_ch = ARRAY_SIZE(st_press_lps001wp_channels),
304 .odr = {
305 .addr = 0x20,
306 .mask = 0x30,
307 .odr_avl = {
308 { .hz = 1, .value = 0x01 },
309 { .hz = 7, .value = 0x02 },
310 { .hz = 13, .value = 0x03 },
313 .pw = {
314 .addr = 0x20,
315 .mask = 0x40,
316 .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
317 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
319 .fs = {
320 .fs_avl = {
322 * Pressure and temperature resolution values
323 * as defined in table 3 of LPS001WP datasheet.
325 [0] = {
326 .num = ST_PRESS_FS_AVL_1100MB,
327 .gain = ST_PRESS_LPS001WP_FS_AVL_PRESS_GAIN,
328 .gain2 = ST_PRESS_LPS001WP_LSB_PER_CELSIUS,
332 .bdu = {
333 .addr = 0x20,
334 .mask = 0x04,
336 .drdy_irq = {
337 .addr = 0,
339 .multi_read_bit = true,
340 .bootime = 2,
344 * CUSTOM VALUES FOR LPS25H SENSOR
345 * See LPS25H datasheet:
346 * http://www2.st.com/resource/en/datasheet/lps25h.pdf
348 .wai = 0xbd,
349 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
350 .sensors_supported = {
351 [0] = LPS25H_PRESS_DEV_NAME,
353 .ch = (struct iio_chan_spec *)st_press_1_channels,
354 .num_ch = ARRAY_SIZE(st_press_1_channels),
355 .odr = {
356 .addr = 0x20,
357 .mask = 0x70,
358 .odr_avl = {
359 { .hz = 1, .value = 0x01 },
360 { .hz = 7, .value = 0x02 },
361 { .hz = 13, .value = 0x03 },
362 { .hz = 25, .value = 0x04 },
365 .pw = {
366 .addr = 0x20,
367 .mask = 0x80,
368 .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
369 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
371 .fs = {
372 .fs_avl = {
374 * Pressure and temperature sensitivity values
375 * as defined in table 3 of LPS25H datasheet.
377 [0] = {
378 .num = ST_PRESS_FS_AVL_1260MB,
379 .gain = ST_PRESS_KPASCAL_NANO_SCALE,
380 .gain2 = ST_PRESS_LSB_PER_CELSIUS,
384 .bdu = {
385 .addr = 0x20,
386 .mask = 0x04,
388 .drdy_irq = {
389 .addr = 0x23,
390 .mask_int1 = 0x01,
391 .mask_int2 = 0x10,
392 .addr_ihl = 0x22,
393 .mask_ihl = 0x80,
394 .addr_od = 0x22,
395 .mask_od = 0x40,
396 .addr_stat_drdy = ST_SENSORS_DEFAULT_STAT_ADDR,
398 .multi_read_bit = true,
399 .bootime = 2,
403 * CUSTOM VALUES FOR LPS22HB SENSOR
404 * See LPS22HB datasheet:
405 * http://www2.st.com/resource/en/datasheet/lps22hb.pdf
407 .wai = 0xb1,
408 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
409 .sensors_supported = {
410 [0] = LPS22HB_PRESS_DEV_NAME,
412 .ch = (struct iio_chan_spec *)st_press_lps22hb_channels,
413 .num_ch = ARRAY_SIZE(st_press_lps22hb_channels),
414 .odr = {
415 .addr = 0x10,
416 .mask = 0x70,
417 .odr_avl = {
418 { .hz = 1, .value = 0x01 },
419 { .hz = 10, .value = 0x02 },
420 { .hz = 25, .value = 0x03 },
421 { .hz = 50, .value = 0x04 },
422 { .hz = 75, .value = 0x05 },
425 .pw = {
426 .addr = 0x10,
427 .mask = 0x70,
428 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
430 .fs = {
431 .fs_avl = {
433 * Pressure and temperature sensitivity values
434 * as defined in table 3 of LPS22HB datasheet.
436 [0] = {
437 .num = ST_PRESS_FS_AVL_1260MB,
438 .gain = ST_PRESS_KPASCAL_NANO_SCALE,
439 .gain2 = ST_PRESS_LPS22HB_LSB_PER_CELSIUS,
443 .bdu = {
444 .addr = 0x10,
445 .mask = 0x02,
447 .drdy_irq = {
448 .addr = 0x12,
449 .mask_int1 = 0x04,
450 .mask_int2 = 0x08,
451 .addr_ihl = 0x12,
452 .mask_ihl = 0x80,
453 .addr_od = 0x12,
454 .mask_od = 0x40,
455 .addr_stat_drdy = ST_SENSORS_DEFAULT_STAT_ADDR,
457 .multi_read_bit = true,
461 static int st_press_write_raw(struct iio_dev *indio_dev,
462 struct iio_chan_spec const *ch,
463 int val,
464 int val2,
465 long mask)
467 int err;
469 switch (mask) {
470 case IIO_CHAN_INFO_SAMP_FREQ:
471 if (val2)
472 return -EINVAL;
473 mutex_lock(&indio_dev->mlock);
474 err = st_sensors_set_odr(indio_dev, val);
475 mutex_unlock(&indio_dev->mlock);
476 return err;
477 default:
478 return -EINVAL;
482 static int st_press_read_raw(struct iio_dev *indio_dev,
483 struct iio_chan_spec const *ch, int *val,
484 int *val2, long mask)
486 int err;
487 struct st_sensor_data *press_data = iio_priv(indio_dev);
489 switch (mask) {
490 case IIO_CHAN_INFO_RAW:
491 err = st_sensors_read_info_raw(indio_dev, ch, val);
492 if (err < 0)
493 goto read_error;
495 return IIO_VAL_INT;
496 case IIO_CHAN_INFO_SCALE:
497 switch (ch->type) {
498 case IIO_PRESSURE:
499 *val = 0;
500 *val2 = press_data->current_fullscale->gain;
501 return IIO_VAL_INT_PLUS_NANO;
502 case IIO_TEMP:
503 *val = MCELSIUS_PER_CELSIUS;
504 *val2 = press_data->current_fullscale->gain2;
505 return IIO_VAL_FRACTIONAL;
506 default:
507 err = -EINVAL;
508 goto read_error;
511 case IIO_CHAN_INFO_OFFSET:
512 switch (ch->type) {
513 case IIO_TEMP:
514 *val = ST_PRESS_MILLI_CELSIUS_OFFSET *
515 press_data->current_fullscale->gain2;
516 *val2 = MCELSIUS_PER_CELSIUS;
517 break;
518 default:
519 err = -EINVAL;
520 goto read_error;
523 return IIO_VAL_FRACTIONAL;
524 case IIO_CHAN_INFO_SAMP_FREQ:
525 *val = press_data->odr;
526 return IIO_VAL_INT;
527 default:
528 return -EINVAL;
531 read_error:
532 return err;
535 static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL();
537 static struct attribute *st_press_attributes[] = {
538 &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
539 NULL,
542 static const struct attribute_group st_press_attribute_group = {
543 .attrs = st_press_attributes,
546 static const struct iio_info press_info = {
547 .driver_module = THIS_MODULE,
548 .attrs = &st_press_attribute_group,
549 .read_raw = &st_press_read_raw,
550 .write_raw = &st_press_write_raw,
551 .debugfs_reg_access = &st_sensors_debugfs_reg_access,
554 #ifdef CONFIG_IIO_TRIGGER
555 static const struct iio_trigger_ops st_press_trigger_ops = {
556 .owner = THIS_MODULE,
557 .set_trigger_state = ST_PRESS_TRIGGER_SET_STATE,
558 .validate_device = st_sensors_validate_device,
560 #define ST_PRESS_TRIGGER_OPS (&st_press_trigger_ops)
561 #else
562 #define ST_PRESS_TRIGGER_OPS NULL
563 #endif
565 int st_press_common_probe(struct iio_dev *indio_dev)
567 struct st_sensor_data *press_data = iio_priv(indio_dev);
568 int irq = press_data->get_irq_data_ready(indio_dev);
569 int err;
571 indio_dev->modes = INDIO_DIRECT_MODE;
572 indio_dev->info = &press_info;
573 mutex_init(&press_data->tb.buf_lock);
575 err = st_sensors_power_enable(indio_dev);
576 if (err)
577 return err;
579 err = st_sensors_check_device_support(indio_dev,
580 ARRAY_SIZE(st_press_sensors_settings),
581 st_press_sensors_settings);
582 if (err < 0)
583 goto st_press_power_off;
586 * Skip timestamping channel while declaring available channels to
587 * common st_sensor layer. Look at st_sensors_get_buffer_element() to
588 * see how timestamps are explicitly pushed as last samples block
589 * element.
591 press_data->num_data_channels = press_data->sensor_settings->num_ch - 1;
592 press_data->multiread_bit = press_data->sensor_settings->multi_read_bit;
593 indio_dev->channels = press_data->sensor_settings->ch;
594 indio_dev->num_channels = press_data->sensor_settings->num_ch;
596 press_data->current_fullscale =
597 (struct st_sensor_fullscale_avl *)
598 &press_data->sensor_settings->fs.fs_avl[0];
600 press_data->odr = press_data->sensor_settings->odr.odr_avl[0].hz;
602 /* Some devices don't support a data ready pin. */
603 if (!press_data->dev->platform_data &&
604 press_data->sensor_settings->drdy_irq.addr)
605 press_data->dev->platform_data =
606 (struct st_sensors_platform_data *)&default_press_pdata;
608 err = st_sensors_init_sensor(indio_dev, press_data->dev->platform_data);
609 if (err < 0)
610 goto st_press_power_off;
612 err = st_press_allocate_ring(indio_dev);
613 if (err < 0)
614 goto st_press_power_off;
616 if (irq > 0) {
617 err = st_sensors_allocate_trigger(indio_dev,
618 ST_PRESS_TRIGGER_OPS);
619 if (err < 0)
620 goto st_press_probe_trigger_error;
623 err = iio_device_register(indio_dev);
624 if (err)
625 goto st_press_device_register_error;
627 dev_info(&indio_dev->dev, "registered pressure sensor %s\n",
628 indio_dev->name);
630 return err;
632 st_press_device_register_error:
633 if (irq > 0)
634 st_sensors_deallocate_trigger(indio_dev);
635 st_press_probe_trigger_error:
636 st_press_deallocate_ring(indio_dev);
637 st_press_power_off:
638 st_sensors_power_disable(indio_dev);
640 return err;
642 EXPORT_SYMBOL(st_press_common_probe);
644 void st_press_common_remove(struct iio_dev *indio_dev)
646 struct st_sensor_data *press_data = iio_priv(indio_dev);
648 st_sensors_power_disable(indio_dev);
650 iio_device_unregister(indio_dev);
651 if (press_data->get_irq_data_ready(indio_dev) > 0)
652 st_sensors_deallocate_trigger(indio_dev);
654 st_press_deallocate_ring(indio_dev);
656 EXPORT_SYMBOL(st_press_common_remove);
658 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
659 MODULE_DESCRIPTION("STMicroelectronics pressures driver");
660 MODULE_LICENSE("GPL v2");