Denis CIOCCA | 217494e | 2013-06-03 15:58:00 +0100 | [diff] [blame] | 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 | |
Jacek Anaszewski | 67dbf54 | 2013-07-02 11:13:00 +0100 | [diff] [blame] | 31 | #define ST_PRESS_LSB_PER_MBAR 4096UL |
| 32 | #define ST_PRESS_KPASCAL_NANO_SCALE (100000000UL / \ |
| 33 | ST_PRESS_LSB_PER_MBAR) |
Jacek Anaszewski | 1003eb6 | 2013-07-02 11:13:00 +0100 | [diff] [blame] | 34 | #define ST_PRESS_LSB_PER_CELSIUS 480UL |
| 35 | #define ST_PRESS_CELSIUS_NANO_SCALE (1000000000UL / \ |
| 36 | ST_PRESS_LSB_PER_CELSIUS) |
Denis CIOCCA | 217494e | 2013-06-03 15:58:00 +0100 | [diff] [blame] | 37 | #define ST_PRESS_NUMBER_DATA_CHANNELS 1 |
| 38 | |
Denis CIOCCA | 217494e | 2013-06-03 15:58:00 +0100 | [diff] [blame] | 39 | /* FULLSCALE */ |
| 40 | #define ST_PRESS_FS_AVL_1260MB 1260 |
| 41 | |
Denis CIOCCA | 9318784 | 2014-02-20 17:49:00 +0000 | [diff] [blame] | 42 | #define ST_PRESS_1_OUT_XL_ADDR 0x28 |
| 43 | #define ST_TEMP_1_OUT_L_ADDR 0x2b |
| 44 | |
Lee Jones | 302fbd5 | 2013-09-10 13:49:00 +0100 | [diff] [blame] | 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 |
Denis CIOCCA | 217494e | 2013-06-03 15:58:00 +0100 | [diff] [blame] | 67 | |
Lee Jones | 7885a8c | 2013-09-16 17:02:00 +0100 | [diff] [blame] | 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 | |
Denis CIOCCA | 9318784 | 2014-02-20 17:49:00 +0000 | [diff] [blame] | 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[] = { |
Lee Jones | 2f5effc | 2013-09-10 13:49:00 +0100 | [diff] [blame] | 109 | { |
| 110 | .type = IIO_PRESSURE, |
| 111 | .channel2 = IIO_NO_MOD, |
Denis CIOCCA | 9318784 | 2014-02-20 17:49:00 +0000 | [diff] [blame] | 112 | .address = ST_PRESS_1_OUT_XL_ADDR, |
Lee Jones | 2f5effc | 2013-09-10 13:49:00 +0100 | [diff] [blame] | 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 = |
Denis CIOCCA | 217494e | 2013-06-03 15:58:00 +0100 | [diff] [blame] | 121 | BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), |
Lee Jones | 2f5effc | 2013-09-10 13:49:00 +0100 | [diff] [blame] | 122 | .modified = 0, |
| 123 | }, |
| 124 | { |
| 125 | .type = IIO_TEMP, |
| 126 | .channel2 = IIO_NO_MOD, |
Denis CIOCCA | 9318784 | 2014-02-20 17:49:00 +0000 | [diff] [blame] | 127 | .address = ST_TEMP_1_OUT_L_ADDR, |
Lee Jones | 2f5effc | 2013-09-10 13:49:00 +0100 | [diff] [blame] | 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 | }, |
Denis CIOCCA | 217494e | 2013-06-03 15:58:00 +0100 | [diff] [blame] | 141 | IIO_CHAN_SOFT_TIMESTAMP(1) |
| 142 | }; |
| 143 | |
Lee Jones | 7885a8c | 2013-09-16 17:02:00 +0100 | [diff] [blame] | 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 | |
Denis CIOCCA | a7ee883 | 2014-10-03 17:35:35 +0200 | [diff] [blame] | 178 | static const struct st_sensor_settings st_press_sensors_settings[] = { |
Denis CIOCCA | 217494e | 2013-06-03 15:58:00 +0100 | [diff] [blame] | 179 | { |
Lee Jones | 302fbd5 | 2013-09-10 13:49:00 +0100 | [diff] [blame] | 180 | .wai = ST_PRESS_LPS331AP_WAI_EXP, |
Giuseppe Barba | bc27381 | 2015-07-21 10:35:41 +0200 | [diff] [blame] | 181 | .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS, |
Denis CIOCCA | 217494e | 2013-06-03 15:58:00 +0100 | [diff] [blame] | 182 | .sensors_supported = { |
| 183 | [0] = LPS331AP_PRESS_DEV_NAME, |
| 184 | }, |
Denis CIOCCA | 9318784 | 2014-02-20 17:49:00 +0000 | [diff] [blame] | 185 | .ch = (struct iio_chan_spec *)st_press_1_channels, |
| 186 | .num_ch = ARRAY_SIZE(st_press_1_channels), |
Denis CIOCCA | 217494e | 2013-06-03 15:58:00 +0100 | [diff] [blame] | 187 | .odr = { |
Lee Jones | 302fbd5 | 2013-09-10 13:49:00 +0100 | [diff] [blame] | 188 | .addr = ST_PRESS_LPS331AP_ODR_ADDR, |
| 189 | .mask = ST_PRESS_LPS331AP_ODR_MASK, |
Denis CIOCCA | 217494e | 2013-06-03 15:58:00 +0100 | [diff] [blame] | 190 | .odr_avl = { |
Lee Jones | 302fbd5 | 2013-09-10 13:49:00 +0100 | [diff] [blame] | 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, }, |
Denis CIOCCA | 217494e | 2013-06-03 15:58:00 +0100 | [diff] [blame] | 195 | }, |
| 196 | }, |
| 197 | .pw = { |
Lee Jones | 302fbd5 | 2013-09-10 13:49:00 +0100 | [diff] [blame] | 198 | .addr = ST_PRESS_LPS331AP_PW_ADDR, |
| 199 | .mask = ST_PRESS_LPS331AP_PW_MASK, |
Denis CIOCCA | 217494e | 2013-06-03 15:58:00 +0100 | [diff] [blame] | 200 | .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE, |
| 201 | .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE, |
| 202 | }, |
| 203 | .fs = { |
Lee Jones | 302fbd5 | 2013-09-10 13:49:00 +0100 | [diff] [blame] | 204 | .addr = ST_PRESS_LPS331AP_FS_ADDR, |
| 205 | .mask = ST_PRESS_LPS331AP_FS_MASK, |
Denis CIOCCA | 217494e | 2013-06-03 15:58:00 +0100 | [diff] [blame] | 206 | .fs_avl = { |
| 207 | [0] = { |
| 208 | .num = ST_PRESS_FS_AVL_1260MB, |
Lee Jones | 302fbd5 | 2013-09-10 13:49:00 +0100 | [diff] [blame] | 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, |
Denis CIOCCA | 217494e | 2013-06-03 15:58:00 +0100 | [diff] [blame] | 212 | }, |
| 213 | }, |
| 214 | }, |
| 215 | .bdu = { |
Lee Jones | 302fbd5 | 2013-09-10 13:49:00 +0100 | [diff] [blame] | 216 | .addr = ST_PRESS_LPS331AP_BDU_ADDR, |
| 217 | .mask = ST_PRESS_LPS331AP_BDU_MASK, |
Denis CIOCCA | 217494e | 2013-06-03 15:58:00 +0100 | [diff] [blame] | 218 | }, |
| 219 | .drdy_irq = { |
Lee Jones | 302fbd5 | 2013-09-10 13:49:00 +0100 | [diff] [blame] | 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, |
Denis CIOCCA | 217494e | 2013-06-03 15:58:00 +0100 | [diff] [blame] | 223 | }, |
Lee Jones | 302fbd5 | 2013-09-10 13:49:00 +0100 | [diff] [blame] | 224 | .multi_read_bit = ST_PRESS_LPS331AP_MULTIREAD_BIT, |
Denis CIOCCA | 217494e | 2013-06-03 15:58:00 +0100 | [diff] [blame] | 225 | .bootime = 2, |
| 226 | }, |
Lee Jones | 7885a8c | 2013-09-16 17:02:00 +0100 | [diff] [blame] | 227 | { |
| 228 | .wai = ST_PRESS_LPS001WP_WAI_EXP, |
Giuseppe Barba | bc27381 | 2015-07-21 10:35:41 +0200 | [diff] [blame] | 229 | .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS, |
Lee Jones | 7885a8c | 2013-09-16 17:02:00 +0100 | [diff] [blame] | 230 | .sensors_supported = { |
| 231 | [0] = LPS001WP_PRESS_DEV_NAME, |
| 232 | }, |
| 233 | .ch = (struct iio_chan_spec *)st_press_lps001wp_channels, |
| 234 | .num_ch = ARRAY_SIZE(st_press_lps001wp_channels), |
| 235 | .odr = { |
| 236 | .addr = ST_PRESS_LPS001WP_ODR_ADDR, |
| 237 | .mask = ST_PRESS_LPS001WP_ODR_MASK, |
| 238 | .odr_avl = { |
| 239 | { 1, ST_PRESS_LPS001WP_ODR_AVL_1HZ_VAL, }, |
| 240 | { 7, ST_PRESS_LPS001WP_ODR_AVL_7HZ_VAL, }, |
| 241 | { 13, ST_PRESS_LPS001WP_ODR_AVL_13HZ_VAL, }, |
| 242 | }, |
| 243 | }, |
| 244 | .pw = { |
| 245 | .addr = ST_PRESS_LPS001WP_PW_ADDR, |
| 246 | .mask = ST_PRESS_LPS001WP_PW_MASK, |
| 247 | .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE, |
| 248 | .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE, |
| 249 | }, |
| 250 | .fs = { |
| 251 | .addr = 0, |
| 252 | }, |
| 253 | .bdu = { |
| 254 | .addr = ST_PRESS_LPS001WP_BDU_ADDR, |
| 255 | .mask = ST_PRESS_LPS001WP_BDU_MASK, |
| 256 | }, |
| 257 | .drdy_irq = { |
| 258 | .addr = 0, |
| 259 | }, |
| 260 | .multi_read_bit = ST_PRESS_LPS001WP_MULTIREAD_BIT, |
| 261 | .bootime = 2, |
| 262 | }, |
Denis CIOCCA | 9318784 | 2014-02-20 17:49:00 +0000 | [diff] [blame] | 263 | { |
| 264 | .wai = ST_PRESS_LPS25H_WAI_EXP, |
Giuseppe Barba | bc27381 | 2015-07-21 10:35:41 +0200 | [diff] [blame] | 265 | .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS, |
Denis CIOCCA | 9318784 | 2014-02-20 17:49:00 +0000 | [diff] [blame] | 266 | .sensors_supported = { |
| 267 | [0] = LPS25H_PRESS_DEV_NAME, |
| 268 | }, |
| 269 | .ch = (struct iio_chan_spec *)st_press_1_channels, |
| 270 | .num_ch = ARRAY_SIZE(st_press_1_channels), |
| 271 | .odr = { |
| 272 | .addr = ST_PRESS_LPS25H_ODR_ADDR, |
| 273 | .mask = ST_PRESS_LPS25H_ODR_MASK, |
| 274 | .odr_avl = { |
| 275 | { 1, ST_PRESS_LPS25H_ODR_AVL_1HZ_VAL, }, |
| 276 | { 7, ST_PRESS_LPS25H_ODR_AVL_7HZ_VAL, }, |
| 277 | { 13, ST_PRESS_LPS25H_ODR_AVL_13HZ_VAL, }, |
| 278 | { 25, ST_PRESS_LPS25H_ODR_AVL_25HZ_VAL, }, |
| 279 | }, |
| 280 | }, |
| 281 | .pw = { |
| 282 | .addr = ST_PRESS_LPS25H_PW_ADDR, |
| 283 | .mask = ST_PRESS_LPS25H_PW_MASK, |
| 284 | .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE, |
| 285 | .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE, |
| 286 | }, |
| 287 | .fs = { |
| 288 | .addr = ST_PRESS_LPS25H_FS_ADDR, |
| 289 | .mask = ST_PRESS_LPS25H_FS_MASK, |
| 290 | .fs_avl = { |
| 291 | [0] = { |
| 292 | .num = ST_PRESS_FS_AVL_1260MB, |
| 293 | .value = ST_PRESS_LPS25H_FS_AVL_1260_VAL, |
| 294 | .gain = ST_PRESS_LPS25H_FS_AVL_1260_GAIN, |
| 295 | .gain2 = ST_PRESS_LPS25H_FS_AVL_TEMP_GAIN, |
| 296 | }, |
| 297 | }, |
| 298 | }, |
| 299 | .bdu = { |
| 300 | .addr = ST_PRESS_LPS25H_BDU_ADDR, |
| 301 | .mask = ST_PRESS_LPS25H_BDU_MASK, |
| 302 | }, |
| 303 | .drdy_irq = { |
| 304 | .addr = ST_PRESS_LPS25H_DRDY_IRQ_ADDR, |
| 305 | .mask_int1 = ST_PRESS_LPS25H_DRDY_IRQ_INT1_MASK, |
| 306 | .mask_int2 = ST_PRESS_LPS25H_DRDY_IRQ_INT2_MASK, |
| 307 | }, |
| 308 | .multi_read_bit = ST_PRESS_LPS25H_MULTIREAD_BIT, |
| 309 | .bootime = 2, |
| 310 | }, |
Denis CIOCCA | 217494e | 2013-06-03 15:58:00 +0100 | [diff] [blame] | 311 | }; |
| 312 | |
Jonathan Cameron | 2d239c9 | 2014-06-22 20:59:00 +0100 | [diff] [blame] | 313 | static int st_press_write_raw(struct iio_dev *indio_dev, |
| 314 | struct iio_chan_spec const *ch, |
| 315 | int val, |
| 316 | int val2, |
| 317 | long mask) |
| 318 | { |
| 319 | int err; |
| 320 | |
| 321 | switch (mask) { |
| 322 | case IIO_CHAN_INFO_SAMP_FREQ: |
| 323 | if (val2) |
| 324 | return -EINVAL; |
| 325 | mutex_lock(&indio_dev->mlock); |
| 326 | err = st_sensors_set_odr(indio_dev, val); |
| 327 | mutex_unlock(&indio_dev->mlock); |
| 328 | return err; |
| 329 | default: |
| 330 | return -EINVAL; |
| 331 | } |
| 332 | } |
| 333 | |
Denis CIOCCA | 217494e | 2013-06-03 15:58:00 +0100 | [diff] [blame] | 334 | static int st_press_read_raw(struct iio_dev *indio_dev, |
| 335 | struct iio_chan_spec const *ch, int *val, |
| 336 | int *val2, long mask) |
| 337 | { |
| 338 | int err; |
Denis CIOCCA | a1dcf42 | 2014-10-03 17:35:40 +0200 | [diff] [blame] | 339 | struct st_sensor_data *press_data = iio_priv(indio_dev); |
Denis CIOCCA | 217494e | 2013-06-03 15:58:00 +0100 | [diff] [blame] | 340 | |
| 341 | switch (mask) { |
| 342 | case IIO_CHAN_INFO_RAW: |
| 343 | err = st_sensors_read_info_raw(indio_dev, ch, val); |
| 344 | if (err < 0) |
| 345 | goto read_error; |
| 346 | |
| 347 | return IIO_VAL_INT; |
| 348 | case IIO_CHAN_INFO_SCALE: |
| 349 | *val = 0; |
| 350 | |
| 351 | switch (ch->type) { |
| 352 | case IIO_PRESSURE: |
Denis CIOCCA | a1dcf42 | 2014-10-03 17:35:40 +0200 | [diff] [blame] | 353 | *val2 = press_data->current_fullscale->gain; |
Denis CIOCCA | 217494e | 2013-06-03 15:58:00 +0100 | [diff] [blame] | 354 | break; |
| 355 | case IIO_TEMP: |
Denis CIOCCA | a1dcf42 | 2014-10-03 17:35:40 +0200 | [diff] [blame] | 356 | *val2 = press_data->current_fullscale->gain2; |
Denis CIOCCA | 217494e | 2013-06-03 15:58:00 +0100 | [diff] [blame] | 357 | break; |
| 358 | default: |
| 359 | err = -EINVAL; |
| 360 | goto read_error; |
| 361 | } |
| 362 | |
| 363 | return IIO_VAL_INT_PLUS_NANO; |
| 364 | case IIO_CHAN_INFO_OFFSET: |
| 365 | switch (ch->type) { |
| 366 | case IIO_TEMP: |
| 367 | *val = 425; |
| 368 | *val2 = 10; |
| 369 | break; |
| 370 | default: |
| 371 | err = -EINVAL; |
| 372 | goto read_error; |
| 373 | } |
| 374 | |
| 375 | return IIO_VAL_FRACTIONAL; |
Jonathan Cameron | 2d239c9 | 2014-06-22 20:59:00 +0100 | [diff] [blame] | 376 | case IIO_CHAN_INFO_SAMP_FREQ: |
Denis CIOCCA | a1dcf42 | 2014-10-03 17:35:40 +0200 | [diff] [blame] | 377 | *val = press_data->odr; |
Jonathan Cameron | 2d239c9 | 2014-06-22 20:59:00 +0100 | [diff] [blame] | 378 | return IIO_VAL_INT; |
Denis CIOCCA | 217494e | 2013-06-03 15:58:00 +0100 | [diff] [blame] | 379 | default: |
| 380 | return -EINVAL; |
| 381 | } |
| 382 | |
| 383 | read_error: |
| 384 | return err; |
| 385 | } |
| 386 | |
Denis CIOCCA | 217494e | 2013-06-03 15:58:00 +0100 | [diff] [blame] | 387 | static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL(); |
| 388 | |
| 389 | static struct attribute *st_press_attributes[] = { |
| 390 | &iio_dev_attr_sampling_frequency_available.dev_attr.attr, |
Denis CIOCCA | 217494e | 2013-06-03 15:58:00 +0100 | [diff] [blame] | 391 | NULL, |
| 392 | }; |
| 393 | |
| 394 | static const struct attribute_group st_press_attribute_group = { |
| 395 | .attrs = st_press_attributes, |
| 396 | }; |
| 397 | |
| 398 | static const struct iio_info press_info = { |
| 399 | .driver_module = THIS_MODULE, |
| 400 | .attrs = &st_press_attribute_group, |
| 401 | .read_raw = &st_press_read_raw, |
Jonathan Cameron | 2d239c9 | 2014-06-22 20:59:00 +0100 | [diff] [blame] | 402 | .write_raw = &st_press_write_raw, |
Linus Walleij | a0175b9 | 2015-08-12 10:22:41 +0200 | [diff] [blame] | 403 | .debugfs_reg_access = &st_sensors_debugfs_reg_access, |
Denis CIOCCA | 217494e | 2013-06-03 15:58:00 +0100 | [diff] [blame] | 404 | }; |
| 405 | |
| 406 | #ifdef CONFIG_IIO_TRIGGER |
| 407 | static const struct iio_trigger_ops st_press_trigger_ops = { |
| 408 | .owner = THIS_MODULE, |
| 409 | .set_trigger_state = ST_PRESS_TRIGGER_SET_STATE, |
| 410 | }; |
| 411 | #define ST_PRESS_TRIGGER_OPS (&st_press_trigger_ops) |
| 412 | #else |
| 413 | #define ST_PRESS_TRIGGER_OPS NULL |
| 414 | #endif |
| 415 | |
Denis CIOCCA | 0baa3fc | 2014-10-03 17:35:39 +0200 | [diff] [blame] | 416 | int st_press_common_probe(struct iio_dev *indio_dev) |
Denis CIOCCA | 217494e | 2013-06-03 15:58:00 +0100 | [diff] [blame] | 417 | { |
Denis CIOCCA | a1dcf42 | 2014-10-03 17:35:40 +0200 | [diff] [blame] | 418 | struct st_sensor_data *press_data = iio_priv(indio_dev); |
| 419 | int irq = press_data->get_irq_data_ready(indio_dev); |
Lee Jones | a6cc5b2 | 2013-09-16 17:02:00 +0100 | [diff] [blame] | 420 | int err; |
Denis CIOCCA | 217494e | 2013-06-03 15:58:00 +0100 | [diff] [blame] | 421 | |
| 422 | indio_dev->modes = INDIO_DIRECT_MODE; |
| 423 | indio_dev->info = &press_info; |
Alban Bedel | 8e71c04 | 2015-04-20 13:57:18 +0200 | [diff] [blame] | 424 | mutex_init(&press_data->tb.buf_lock); |
Denis CIOCCA | 217494e | 2013-06-03 15:58:00 +0100 | [diff] [blame] | 425 | |
Linus Walleij | ea7e586 | 2014-04-13 20:08:00 +0100 | [diff] [blame] | 426 | st_sensors_power_enable(indio_dev); |
Lee Jones | 7744876 | 2013-09-16 17:02:00 +0100 | [diff] [blame] | 427 | |
Denis CIOCCA | 217494e | 2013-06-03 15:58:00 +0100 | [diff] [blame] | 428 | err = st_sensors_check_device_support(indio_dev, |
Denis CIOCCA | a7ee883 | 2014-10-03 17:35:35 +0200 | [diff] [blame] | 429 | ARRAY_SIZE(st_press_sensors_settings), |
| 430 | st_press_sensors_settings); |
Denis CIOCCA | 217494e | 2013-06-03 15:58:00 +0100 | [diff] [blame] | 431 | if (err < 0) |
Lee Jones | a6cc5b2 | 2013-09-16 17:02:00 +0100 | [diff] [blame] | 432 | return err; |
Denis CIOCCA | 217494e | 2013-06-03 15:58:00 +0100 | [diff] [blame] | 433 | |
Denis CIOCCA | a1dcf42 | 2014-10-03 17:35:40 +0200 | [diff] [blame] | 434 | press_data->num_data_channels = ST_PRESS_NUMBER_DATA_CHANNELS; |
| 435 | press_data->multiread_bit = press_data->sensor_settings->multi_read_bit; |
| 436 | indio_dev->channels = press_data->sensor_settings->ch; |
| 437 | indio_dev->num_channels = press_data->sensor_settings->num_ch; |
Denis CIOCCA | 217494e | 2013-06-03 15:58:00 +0100 | [diff] [blame] | 438 | |
Denis CIOCCA | a1dcf42 | 2014-10-03 17:35:40 +0200 | [diff] [blame] | 439 | if (press_data->sensor_settings->fs.addr != 0) |
| 440 | press_data->current_fullscale = |
| 441 | (struct st_sensor_fullscale_avl *) |
| 442 | &press_data->sensor_settings->fs.fs_avl[0]; |
Lee Jones | 362f2f8 | 2013-09-10 13:49:00 +0100 | [diff] [blame] | 443 | |
Denis CIOCCA | a1dcf42 | 2014-10-03 17:35:40 +0200 | [diff] [blame] | 444 | press_data->odr = press_data->sensor_settings->odr.odr_avl[0].hz; |
Denis CIOCCA | 217494e | 2013-06-03 15:58:00 +0100 | [diff] [blame] | 445 | |
Lee Jones | 38d1c6a | 2013-09-16 17:02:00 +0100 | [diff] [blame] | 446 | /* Some devices don't support a data ready pin. */ |
Denis CIOCCA | a1dcf42 | 2014-10-03 17:35:40 +0200 | [diff] [blame] | 447 | if (!press_data->dev->platform_data && |
| 448 | press_data->sensor_settings->drdy_irq.addr) |
| 449 | press_data->dev->platform_data = |
Denis CIOCCA | 23cde4d | 2013-06-19 09:28:00 +0100 | [diff] [blame] | 450 | (struct st_sensors_platform_data *)&default_press_pdata; |
| 451 | |
Denis CIOCCA | a1dcf42 | 2014-10-03 17:35:40 +0200 | [diff] [blame] | 452 | err = st_sensors_init_sensor(indio_dev, press_data->dev->platform_data); |
Denis CIOCCA | 217494e | 2013-06-03 15:58:00 +0100 | [diff] [blame] | 453 | if (err < 0) |
Lee Jones | a6cc5b2 | 2013-09-16 17:02:00 +0100 | [diff] [blame] | 454 | return err; |
Denis CIOCCA | 217494e | 2013-06-03 15:58:00 +0100 | [diff] [blame] | 455 | |
Denis CIOCCA | 7a137c9 | 2013-09-18 10:00:00 +0100 | [diff] [blame] | 456 | err = st_press_allocate_ring(indio_dev); |
| 457 | if (err < 0) |
| 458 | return err; |
Denis CIOCCA | 217494e | 2013-06-03 15:58:00 +0100 | [diff] [blame] | 459 | |
Denis CIOCCA | 7a137c9 | 2013-09-18 10:00:00 +0100 | [diff] [blame] | 460 | if (irq > 0) { |
Denis CIOCCA | 217494e | 2013-06-03 15:58:00 +0100 | [diff] [blame] | 461 | err = st_sensors_allocate_trigger(indio_dev, |
Lee Jones | a6cc5b2 | 2013-09-16 17:02:00 +0100 | [diff] [blame] | 462 | ST_PRESS_TRIGGER_OPS); |
Denis CIOCCA | 217494e | 2013-06-03 15:58:00 +0100 | [diff] [blame] | 463 | if (err < 0) |
| 464 | goto st_press_probe_trigger_error; |
| 465 | } |
| 466 | |
| 467 | err = iio_device_register(indio_dev); |
| 468 | if (err) |
| 469 | goto st_press_device_register_error; |
| 470 | |
Linus Walleij | 4f544ce | 2014-04-13 20:08:00 +0100 | [diff] [blame] | 471 | dev_info(&indio_dev->dev, "registered pressure sensor %s\n", |
| 472 | indio_dev->name); |
| 473 | |
Denis CIOCCA | 217494e | 2013-06-03 15:58:00 +0100 | [diff] [blame] | 474 | return err; |
| 475 | |
| 476 | st_press_device_register_error: |
Lee Jones | a6cc5b2 | 2013-09-16 17:02:00 +0100 | [diff] [blame] | 477 | if (irq > 0) |
Denis CIOCCA | 217494e | 2013-06-03 15:58:00 +0100 | [diff] [blame] | 478 | st_sensors_deallocate_trigger(indio_dev); |
| 479 | st_press_probe_trigger_error: |
Denis CIOCCA | 7a137c9 | 2013-09-18 10:00:00 +0100 | [diff] [blame] | 480 | st_press_deallocate_ring(indio_dev); |
Lee Jones | a6cc5b2 | 2013-09-16 17:02:00 +0100 | [diff] [blame] | 481 | |
Denis CIOCCA | 217494e | 2013-06-03 15:58:00 +0100 | [diff] [blame] | 482 | return err; |
| 483 | } |
| 484 | EXPORT_SYMBOL(st_press_common_probe); |
| 485 | |
| 486 | void st_press_common_remove(struct iio_dev *indio_dev) |
| 487 | { |
Denis CIOCCA | a1dcf42 | 2014-10-03 17:35:40 +0200 | [diff] [blame] | 488 | struct st_sensor_data *press_data = iio_priv(indio_dev); |
Denis CIOCCA | 217494e | 2013-06-03 15:58:00 +0100 | [diff] [blame] | 489 | |
Linus Walleij | ea7e586 | 2014-04-13 20:08:00 +0100 | [diff] [blame] | 490 | st_sensors_power_disable(indio_dev); |
Lee Jones | 7744876 | 2013-09-16 17:02:00 +0100 | [diff] [blame] | 491 | |
Denis CIOCCA | 217494e | 2013-06-03 15:58:00 +0100 | [diff] [blame] | 492 | iio_device_unregister(indio_dev); |
Denis CIOCCA | a1dcf42 | 2014-10-03 17:35:40 +0200 | [diff] [blame] | 493 | if (press_data->get_irq_data_ready(indio_dev) > 0) |
Denis CIOCCA | 217494e | 2013-06-03 15:58:00 +0100 | [diff] [blame] | 494 | st_sensors_deallocate_trigger(indio_dev); |
Denis CIOCCA | 7a137c9 | 2013-09-18 10:00:00 +0100 | [diff] [blame] | 495 | |
| 496 | st_press_deallocate_ring(indio_dev); |
Denis CIOCCA | 217494e | 2013-06-03 15:58:00 +0100 | [diff] [blame] | 497 | } |
| 498 | EXPORT_SYMBOL(st_press_common_remove); |
| 499 | |
| 500 | MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>"); |
| 501 | MODULE_DESCRIPTION("STMicroelectronics pressures driver"); |
| 502 | MODULE_LICENSE("GPL v2"); |