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