1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Murata ZPA2326 pressure and temperature sensor IIO driver 4 * 5 * Copyright (c) 2016 Parrot S.A. 6 * 7 * Author: Gregor Boirie <gregor.boirie@parrot.com> 8 */ 9 10 /** 11 * DOC: ZPA2326 theory of operations 12 * 13 * This driver supports %INDIO_DIRECT_MODE and %INDIO_BUFFER_TRIGGERED IIO 14 * modes. 15 * A internal hardware trigger is also implemented to dispatch registered IIO 16 * trigger consumers upon "sample ready" interrupts. 17 * 18 * ZPA2326 hardware supports 2 sampling mode: one shot and continuous. 19 * 20 * A complete one shot sampling cycle gets device out of low power mode, 21 * performs pressure and temperature measurements, then automatically switches 22 * back to low power mode. It is meant for on demand sampling with optimal power 23 * saving at the cost of lower sampling rate and higher software overhead. 24 * This is a natural candidate for IIO read_raw hook implementation 25 * (%INDIO_DIRECT_MODE). It is also used for triggered buffering support to 26 * ensure explicit synchronization with external trigger events 27 * (%INDIO_BUFFER_TRIGGERED). 28 * 29 * The continuous mode works according to a periodic hardware measurement 30 * process continuously pushing samples into an internal hardware FIFO (for 31 * pressure samples only). Measurement cycle completion may be signaled by a 32 * "sample ready" interrupt. 33 * Typical software sequence of operations : 34 * - get device out of low power mode, 35 * - setup hardware sampling period, 36 * - at end of period, upon data ready interrupt: pop pressure samples out of 37 * hardware FIFO and fetch temperature sample 38 * - when no longer needed, stop sampling process by putting device into 39 * low power mode. 40 * This mode is used to implement %INDIO_BUFFER_TRIGGERED mode if device tree 41 * declares a valid interrupt line. In this case, the internal hardware trigger 42 * drives acquisition. 43 * 44 * Note that hardware sampling frequency is taken into account only when 45 * internal hardware trigger is attached as the highest sampling rate seems to 46 * be the most energy efficient. 47 * 48 * TODO: 49 * preset pressure threshold crossing / IIO events ; 50 * differential pressure sampling ; 51 * hardware samples averaging. 52 */ 53 54 #include <linux/module.h> 55 #include <linux/kernel.h> 56 #include <linux/delay.h> 57 #include <linux/interrupt.h> 58 #include <linux/regulator/consumer.h> 59 #include <linux/pm_runtime.h> 60 #include <linux/regmap.h> 61 #include <linux/iio/iio.h> 62 #include <linux/iio/sysfs.h> 63 #include <linux/iio/buffer.h> 64 #include <linux/iio/trigger.h> 65 #include <linux/iio/trigger_consumer.h> 66 #include <linux/iio/triggered_buffer.h> 67 #include <linux/unaligned.h> 68 #include "zpa2326.h" 69 70 /* 200 ms should be enough for the longest conversion time in one-shot mode. */ 71 #define ZPA2326_CONVERSION_JIFFIES (HZ / 5) 72 73 /* There should be a 1 ms delay (Tpup) after getting out of reset. */ 74 #define ZPA2326_TPUP_USEC_MIN (1000) 75 #define ZPA2326_TPUP_USEC_MAX (2000) 76 77 /** 78 * struct zpa2326_frequency - Hardware sampling frequency descriptor 79 * @hz : Frequency in Hertz. 80 * @odr: Output Data Rate word as expected by %ZPA2326_CTRL_REG3_REG. 81 */ 82 struct zpa2326_frequency { 83 int hz; 84 u16 odr; 85 }; 86 87 /* 88 * Keep these in strict ascending order: last array entry is expected to 89 * correspond to the highest sampling frequency. 90 */ 91 static const struct zpa2326_frequency zpa2326_sampling_frequencies[] = { 92 { .hz = 1, .odr = 1 << ZPA2326_CTRL_REG3_ODR_SHIFT }, 93 { .hz = 5, .odr = 5 << ZPA2326_CTRL_REG3_ODR_SHIFT }, 94 { .hz = 11, .odr = 6 << ZPA2326_CTRL_REG3_ODR_SHIFT }, 95 { .hz = 23, .odr = 7 << ZPA2326_CTRL_REG3_ODR_SHIFT }, 96 }; 97 98 /* Return the highest hardware sampling frequency available. */ 99 static const struct zpa2326_frequency *zpa2326_highest_frequency(void) 100 { 101 return &zpa2326_sampling_frequencies[ 102 ARRAY_SIZE(zpa2326_sampling_frequencies) - 1]; 103 } 104 105 /** 106 * struct zpa2326_private - Per-device internal private state 107 * @timestamp: Buffered samples ready datum. 108 * @regmap: Underlying I2C / SPI bus adapter used to abstract slave register 109 * accesses. 110 * @result: Allows sampling logic to get completion status of operations 111 * that interrupt handlers perform asynchronously. 112 * @data_ready: Interrupt handler uses this to wake user context up at sampling 113 * operation completion. 114 * @trigger: Optional hardware / interrupt driven trigger used to notify 115 * external devices a new sample is ready. 116 * @waken: Flag indicating whether or not device has just been powered on. 117 * @irq: Optional interrupt line: negative or zero if not declared into 118 * DT, in which case sampling logic keeps polling status register 119 * to detect completion. 120 * @frequency: Current hardware sampling frequency. 121 * @vref: Power / voltage reference. 122 * @vdd: Power supply. 123 */ 124 struct zpa2326_private { 125 s64 timestamp; 126 struct regmap *regmap; 127 int result; 128 struct completion data_ready; 129 struct iio_trigger *trigger; 130 bool waken; 131 int irq; 132 const struct zpa2326_frequency *frequency; 133 struct regulator *vref; 134 struct regulator *vdd; 135 }; 136 137 #define zpa2326_err(idev, fmt, ...) \ 138 dev_err(idev->dev.parent, fmt "\n", ##__VA_ARGS__) 139 140 #define zpa2326_warn(idev, fmt, ...) \ 141 dev_warn(idev->dev.parent, fmt "\n", ##__VA_ARGS__) 142 143 #define zpa2326_dbg(idev, fmt, ...) \ 144 dev_dbg(idev->dev.parent, fmt "\n", ##__VA_ARGS__) 145 146 bool zpa2326_isreg_writeable(struct device *dev, unsigned int reg) 147 { 148 switch (reg) { 149 case ZPA2326_REF_P_XL_REG: 150 case ZPA2326_REF_P_L_REG: 151 case ZPA2326_REF_P_H_REG: 152 case ZPA2326_RES_CONF_REG: 153 case ZPA2326_CTRL_REG0_REG: 154 case ZPA2326_CTRL_REG1_REG: 155 case ZPA2326_CTRL_REG2_REG: 156 case ZPA2326_CTRL_REG3_REG: 157 case ZPA2326_THS_P_LOW_REG: 158 case ZPA2326_THS_P_HIGH_REG: 159 return true; 160 161 default: 162 return false; 163 } 164 } 165 EXPORT_SYMBOL_NS_GPL(zpa2326_isreg_writeable, "IIO_ZPA2326"); 166 167 bool zpa2326_isreg_readable(struct device *dev, unsigned int reg) 168 { 169 switch (reg) { 170 case ZPA2326_REF_P_XL_REG: 171 case ZPA2326_REF_P_L_REG: 172 case ZPA2326_REF_P_H_REG: 173 case ZPA2326_DEVICE_ID_REG: 174 case ZPA2326_RES_CONF_REG: 175 case ZPA2326_CTRL_REG0_REG: 176 case ZPA2326_CTRL_REG1_REG: 177 case ZPA2326_CTRL_REG2_REG: 178 case ZPA2326_CTRL_REG3_REG: 179 case ZPA2326_INT_SOURCE_REG: 180 case ZPA2326_THS_P_LOW_REG: 181 case ZPA2326_THS_P_HIGH_REG: 182 case ZPA2326_STATUS_REG: 183 case ZPA2326_PRESS_OUT_XL_REG: 184 case ZPA2326_PRESS_OUT_L_REG: 185 case ZPA2326_PRESS_OUT_H_REG: 186 case ZPA2326_TEMP_OUT_L_REG: 187 case ZPA2326_TEMP_OUT_H_REG: 188 return true; 189 190 default: 191 return false; 192 } 193 } 194 EXPORT_SYMBOL_NS_GPL(zpa2326_isreg_readable, "IIO_ZPA2326"); 195 196 bool zpa2326_isreg_precious(struct device *dev, unsigned int reg) 197 { 198 switch (reg) { 199 case ZPA2326_INT_SOURCE_REG: 200 case ZPA2326_PRESS_OUT_H_REG: 201 return true; 202 203 default: 204 return false; 205 } 206 } 207 EXPORT_SYMBOL_NS_GPL(zpa2326_isreg_precious, "IIO_ZPA2326"); 208 209 /** 210 * zpa2326_enable_device() - Enable device, i.e. get out of low power mode. 211 * @indio_dev: The IIO device associated with the hardware to enable. 212 * 213 * Required to access complete register space and to perform any sampling 214 * or control operations. 215 * 216 * Return: Zero when successful, a negative error code otherwise. 217 */ 218 static int zpa2326_enable_device(const struct iio_dev *indio_dev) 219 { 220 int err; 221 222 err = regmap_write(((struct zpa2326_private *) 223 iio_priv(indio_dev))->regmap, 224 ZPA2326_CTRL_REG0_REG, ZPA2326_CTRL_REG0_ENABLE); 225 if (err) { 226 zpa2326_err(indio_dev, "failed to enable device (%d)", err); 227 return err; 228 } 229 230 zpa2326_dbg(indio_dev, "enabled"); 231 232 return 0; 233 } 234 235 /** 236 * zpa2326_sleep() - Disable device, i.e. switch to low power mode. 237 * @indio_dev: The IIO device associated with the hardware to disable. 238 * 239 * Only %ZPA2326_DEVICE_ID_REG and %ZPA2326_CTRL_REG0_REG registers may be 240 * accessed once device is in the disabled state. 241 * 242 * Return: Zero when successful, a negative error code otherwise. 243 */ 244 static int zpa2326_sleep(const struct iio_dev *indio_dev) 245 { 246 int err; 247 248 err = regmap_write(((struct zpa2326_private *) 249 iio_priv(indio_dev))->regmap, 250 ZPA2326_CTRL_REG0_REG, 0); 251 if (err) { 252 zpa2326_err(indio_dev, "failed to sleep (%d)", err); 253 return err; 254 } 255 256 zpa2326_dbg(indio_dev, "sleeping"); 257 258 return 0; 259 } 260 261 /** 262 * zpa2326_reset_device() - Reset device to default hardware state. 263 * @indio_dev: The IIO device associated with the hardware to reset. 264 * 265 * Disable sampling and empty hardware FIFO. 266 * Device must be enabled before reset, i.e. not in low power mode. 267 * 268 * Return: Zero when successful, a negative error code otherwise. 269 */ 270 static int zpa2326_reset_device(const struct iio_dev *indio_dev) 271 { 272 int err; 273 274 err = regmap_write(((struct zpa2326_private *) 275 iio_priv(indio_dev))->regmap, 276 ZPA2326_CTRL_REG2_REG, ZPA2326_CTRL_REG2_SWRESET); 277 if (err) { 278 zpa2326_err(indio_dev, "failed to reset device (%d)", err); 279 return err; 280 } 281 282 usleep_range(ZPA2326_TPUP_USEC_MIN, ZPA2326_TPUP_USEC_MAX); 283 284 zpa2326_dbg(indio_dev, "reset"); 285 286 return 0; 287 } 288 289 /** 290 * zpa2326_start_oneshot() - Start a single sampling cycle, i.e. in one shot 291 * mode. 292 * @indio_dev: The IIO device associated with the sampling hardware. 293 * 294 * Device must have been previously enabled and configured for one shot mode. 295 * Device will be switched back to low power mode at end of cycle. 296 * 297 * Return: Zero when successful, a negative error code otherwise. 298 */ 299 static int zpa2326_start_oneshot(const struct iio_dev *indio_dev) 300 { 301 int err; 302 303 err = regmap_write(((struct zpa2326_private *) 304 iio_priv(indio_dev))->regmap, 305 ZPA2326_CTRL_REG0_REG, 306 ZPA2326_CTRL_REG0_ENABLE | 307 ZPA2326_CTRL_REG0_ONE_SHOT); 308 if (err) { 309 zpa2326_err(indio_dev, "failed to start one shot cycle (%d)", 310 err); 311 return err; 312 } 313 314 zpa2326_dbg(indio_dev, "one shot cycle started"); 315 316 return 0; 317 } 318 319 /** 320 * zpa2326_power_on() - Power on device to allow subsequent configuration. 321 * @indio_dev: The IIO device associated with the sampling hardware. 322 * @private: Internal private state related to @indio_dev. 323 * 324 * Sampling will be disabled, preventing strange things from happening in our 325 * back. Hardware FIFO content will be cleared. 326 * When successful, device will be left in the enabled state to allow further 327 * configuration. 328 * 329 * Return: Zero when successful, a negative error code otherwise. 330 */ 331 static int zpa2326_power_on(const struct iio_dev *indio_dev, 332 const struct zpa2326_private *private) 333 { 334 int err; 335 336 err = regulator_enable(private->vref); 337 if (err) 338 return err; 339 340 err = regulator_enable(private->vdd); 341 if (err) 342 goto vref; 343 344 zpa2326_dbg(indio_dev, "powered on"); 345 346 err = zpa2326_enable_device(indio_dev); 347 if (err) 348 goto vdd; 349 350 err = zpa2326_reset_device(indio_dev); 351 if (err) 352 goto sleep; 353 354 return 0; 355 356 sleep: 357 zpa2326_sleep(indio_dev); 358 vdd: 359 regulator_disable(private->vdd); 360 vref: 361 regulator_disable(private->vref); 362 363 zpa2326_dbg(indio_dev, "powered off"); 364 365 return err; 366 } 367 368 /** 369 * zpa2326_power_off() - Power off device, i.e. disable attached power 370 * regulators. 371 * @indio_dev: The IIO device associated with the sampling hardware. 372 * @private: Internal private state related to @indio_dev. 373 * 374 * Return: Zero when successful, a negative error code otherwise. 375 */ 376 static void zpa2326_power_off(const struct iio_dev *indio_dev, 377 const struct zpa2326_private *private) 378 { 379 regulator_disable(private->vdd); 380 regulator_disable(private->vref); 381 382 zpa2326_dbg(indio_dev, "powered off"); 383 } 384 385 /** 386 * zpa2326_config_oneshot() - Setup device for one shot / on demand mode. 387 * @indio_dev: The IIO device associated with the sampling hardware. 388 * @irq: Optional interrupt line the hardware uses to notify new data 389 * samples are ready. Negative or zero values indicate no interrupts 390 * are available, meaning polling is required. 391 * 392 * Output Data Rate is configured for the highest possible rate so that 393 * conversion time and power consumption are reduced to a minimum. 394 * Note that hardware internal averaging machinery (not implemented in this 395 * driver) is not applicable in this mode. 396 * 397 * Device must have been previously enabled before calling 398 * zpa2326_config_oneshot(). 399 * 400 * Return: Zero when successful, a negative error code otherwise. 401 */ 402 static int zpa2326_config_oneshot(const struct iio_dev *indio_dev, 403 int irq) 404 { 405 struct regmap *regs = ((struct zpa2326_private *) 406 iio_priv(indio_dev))->regmap; 407 const struct zpa2326_frequency *freq = zpa2326_highest_frequency(); 408 int err; 409 410 /* Setup highest available Output Data Rate for one shot mode. */ 411 err = regmap_write(regs, ZPA2326_CTRL_REG3_REG, freq->odr); 412 if (err) 413 return err; 414 415 if (irq > 0) { 416 /* Request interrupt when new sample is available. */ 417 err = regmap_write(regs, ZPA2326_CTRL_REG1_REG, 418 (u8)~ZPA2326_CTRL_REG1_MASK_DATA_READY); 419 420 if (err) { 421 dev_err(indio_dev->dev.parent, 422 "failed to setup one shot mode (%d)", err); 423 return err; 424 } 425 } 426 427 zpa2326_dbg(indio_dev, "one shot mode setup @%dHz", freq->hz); 428 429 return 0; 430 } 431 432 /** 433 * zpa2326_clear_fifo() - Clear remaining entries in hardware FIFO. 434 * @indio_dev: The IIO device associated with the sampling hardware. 435 * @min_count: Number of samples present within hardware FIFO. 436 * 437 * @min_count argument is a hint corresponding to the known minimum number of 438 * samples currently living in the FIFO. This allows to reduce the number of bus 439 * accesses by skipping status register read operation as long as we know for 440 * sure there are still entries left. 441 * 442 * Return: Zero when successful, a negative error code otherwise. 443 */ 444 static int zpa2326_clear_fifo(const struct iio_dev *indio_dev, 445 unsigned int min_count) 446 { 447 struct regmap *regs = ((struct zpa2326_private *) 448 iio_priv(indio_dev))->regmap; 449 int err; 450 unsigned int val; 451 452 if (!min_count) { 453 /* 454 * No hint: read status register to determine whether FIFO is 455 * empty or not. 456 */ 457 err = regmap_read(regs, ZPA2326_STATUS_REG, &val); 458 459 if (err < 0) 460 goto err; 461 462 if (val & ZPA2326_STATUS_FIFO_E) 463 /* Fifo is empty: nothing to trash. */ 464 return 0; 465 } 466 467 /* Clear FIFO. */ 468 do { 469 /* 470 * A single fetch from pressure MSB register is enough to pop 471 * values out of FIFO. 472 */ 473 err = regmap_read(regs, ZPA2326_PRESS_OUT_H_REG, &val); 474 if (err < 0) 475 goto err; 476 477 if (min_count) { 478 /* 479 * We know for sure there are at least min_count entries 480 * left in FIFO. Skip status register read. 481 */ 482 min_count--; 483 continue; 484 } 485 486 err = regmap_read(regs, ZPA2326_STATUS_REG, &val); 487 if (err < 0) 488 goto err; 489 490 } while (!(val & ZPA2326_STATUS_FIFO_E)); 491 492 zpa2326_dbg(indio_dev, "FIFO cleared"); 493 494 return 0; 495 496 err: 497 zpa2326_err(indio_dev, "failed to clear FIFO (%d)", err); 498 499 return err; 500 } 501 502 /** 503 * zpa2326_dequeue_pressure() - Retrieve the most recent pressure sample from 504 * hardware FIFO. 505 * @indio_dev: The IIO device associated with the sampling hardware. 506 * @pressure: Sampled pressure output. 507 * 508 * Note that ZPA2326 hardware FIFO stores pressure samples only. 509 * 510 * Return: Zero when successful, a negative error code otherwise. 511 */ 512 static int zpa2326_dequeue_pressure(const struct iio_dev *indio_dev, 513 u32 *pressure) 514 { 515 struct regmap *regs = ((struct zpa2326_private *) 516 iio_priv(indio_dev))->regmap; 517 unsigned int val; 518 int err; 519 int cleared = -1; 520 521 err = regmap_read(regs, ZPA2326_STATUS_REG, &val); 522 if (err < 0) 523 return err; 524 525 *pressure = 0; 526 527 if (val & ZPA2326_STATUS_P_OR) { 528 /* 529 * Fifo overrun : first sample dequeued from FIFO is the 530 * newest. 531 */ 532 zpa2326_warn(indio_dev, "FIFO overflow"); 533 534 err = regmap_bulk_read(regs, ZPA2326_PRESS_OUT_XL_REG, pressure, 535 3); 536 if (err) 537 return err; 538 539 #define ZPA2326_FIFO_DEPTH (16U) 540 /* Hardware FIFO may hold no more than 16 pressure samples. */ 541 return zpa2326_clear_fifo(indio_dev, ZPA2326_FIFO_DEPTH - 1); 542 } 543 544 /* 545 * Fifo has not overflown : retrieve newest sample. We need to pop 546 * values out until FIFO is empty : last fetched pressure is the newest. 547 * In nominal cases, we should find a single queued sample only. 548 */ 549 do { 550 err = regmap_bulk_read(regs, ZPA2326_PRESS_OUT_XL_REG, pressure, 551 3); 552 if (err) 553 return err; 554 555 err = regmap_read(regs, ZPA2326_STATUS_REG, &val); 556 if (err < 0) 557 return err; 558 559 cleared++; 560 } while (!(val & ZPA2326_STATUS_FIFO_E)); 561 562 if (cleared) 563 /* 564 * Samples were pushed by hardware during previous rounds but we 565 * didn't consume them fast enough: inform user. 566 */ 567 zpa2326_dbg(indio_dev, "cleared %d FIFO entries", cleared); 568 569 return 0; 570 } 571 572 /** 573 * zpa2326_fill_sample_buffer() - Enqueue new channel samples to IIO buffer. 574 * @indio_dev: The IIO device associated with the sampling hardware. 575 * @private: Internal private state related to @indio_dev. 576 * 577 * Return: Zero when successful, a negative error code otherwise. 578 */ 579 static int zpa2326_fill_sample_buffer(struct iio_dev *indio_dev, 580 const struct zpa2326_private *private) 581 { 582 struct { 583 u32 pressure; 584 u16 temperature; 585 u64 timestamp; 586 } sample; 587 int err; 588 589 memset(&sample, 0, sizeof(sample)); 590 591 if (test_bit(0, indio_dev->active_scan_mask)) { 592 /* Get current pressure from hardware FIFO. */ 593 err = zpa2326_dequeue_pressure(indio_dev, &sample.pressure); 594 if (err) { 595 zpa2326_warn(indio_dev, "failed to fetch pressure (%d)", 596 err); 597 return err; 598 } 599 } 600 601 if (test_bit(1, indio_dev->active_scan_mask)) { 602 /* Get current temperature. */ 603 err = regmap_bulk_read(private->regmap, ZPA2326_TEMP_OUT_L_REG, 604 &sample.temperature, 2); 605 if (err) { 606 zpa2326_warn(indio_dev, 607 "failed to fetch temperature (%d)", err); 608 return err; 609 } 610 } 611 612 /* 613 * Now push samples using timestamp stored either : 614 * - by hardware interrupt handler if interrupt is available: see 615 * zpa2326_handle_irq(), 616 * - or oneshot completion polling machinery : see 617 * zpa2326_trigger_handler(). 618 */ 619 zpa2326_dbg(indio_dev, "filling raw samples buffer"); 620 621 iio_push_to_buffers_with_timestamp(indio_dev, &sample, 622 private->timestamp); 623 624 return 0; 625 } 626 627 #ifdef CONFIG_PM 628 static int zpa2326_runtime_suspend(struct device *parent) 629 { 630 const struct iio_dev *indio_dev = dev_get_drvdata(parent); 631 632 if (pm_runtime_autosuspend_expiration(parent)) 633 /* Userspace changed autosuspend delay. */ 634 return -EAGAIN; 635 636 zpa2326_power_off(indio_dev, iio_priv(indio_dev)); 637 638 return 0; 639 } 640 641 static int zpa2326_runtime_resume(struct device *parent) 642 { 643 const struct iio_dev *indio_dev = dev_get_drvdata(parent); 644 645 return zpa2326_power_on(indio_dev, iio_priv(indio_dev)); 646 } 647 648 const struct dev_pm_ops zpa2326_pm_ops = { 649 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, 650 pm_runtime_force_resume) 651 SET_RUNTIME_PM_OPS(zpa2326_runtime_suspend, zpa2326_runtime_resume, 652 NULL) 653 }; 654 EXPORT_SYMBOL_NS_GPL(zpa2326_pm_ops, "IIO_ZPA2326"); 655 656 /** 657 * zpa2326_resume() - Request the PM layer to power supply the device. 658 * @indio_dev: The IIO device associated with the sampling hardware. 659 * 660 * Return: 661 * < 0 - a negative error code meaning failure ; 662 * 0 - success, device has just been powered up ; 663 * 1 - success, device was already powered. 664 */ 665 static int zpa2326_resume(const struct iio_dev *indio_dev) 666 { 667 int err; 668 669 err = pm_runtime_get_sync(indio_dev->dev.parent); 670 if (err < 0) { 671 pm_runtime_put(indio_dev->dev.parent); 672 return err; 673 } 674 675 if (err > 0) { 676 /* 677 * Device was already power supplied: get it out of low power 678 * mode and inform caller. 679 */ 680 zpa2326_enable_device(indio_dev); 681 return 1; 682 } 683 684 /* Inform caller device has just been brought back to life. */ 685 return 0; 686 } 687 688 /** 689 * zpa2326_suspend() - Schedule a power down using autosuspend feature of PM 690 * layer. 691 * @indio_dev: The IIO device associated with the sampling hardware. 692 * 693 * Device is switched to low power mode at first to save power even when 694 * attached regulator is a "dummy" one. 695 */ 696 static void zpa2326_suspend(struct iio_dev *indio_dev) 697 { 698 struct device *parent = indio_dev->dev.parent; 699 700 zpa2326_sleep(indio_dev); 701 702 pm_runtime_mark_last_busy(parent); 703 pm_runtime_put_autosuspend(parent); 704 } 705 706 static void zpa2326_init_runtime(struct device *parent) 707 { 708 pm_runtime_get_noresume(parent); 709 pm_runtime_set_active(parent); 710 pm_runtime_enable(parent); 711 pm_runtime_set_autosuspend_delay(parent, 1000); 712 pm_runtime_use_autosuspend(parent); 713 pm_runtime_mark_last_busy(parent); 714 pm_runtime_put_autosuspend(parent); 715 } 716 717 static void zpa2326_fini_runtime(struct device *parent) 718 { 719 pm_runtime_disable(parent); 720 pm_runtime_set_suspended(parent); 721 } 722 #else /* !CONFIG_PM */ 723 static int zpa2326_resume(const struct iio_dev *indio_dev) 724 { 725 zpa2326_enable_device(indio_dev); 726 727 return 0; 728 } 729 730 static void zpa2326_suspend(struct iio_dev *indio_dev) 731 { 732 zpa2326_sleep(indio_dev); 733 } 734 735 #define zpa2326_init_runtime(_parent) 736 #define zpa2326_fini_runtime(_parent) 737 #endif /* !CONFIG_PM */ 738 739 /** 740 * zpa2326_handle_irq() - Process hardware interrupts. 741 * @irq: Interrupt line the hardware uses to notify new data has arrived. 742 * @data: The IIO device associated with the sampling hardware. 743 * 744 * Timestamp buffered samples as soon as possible then schedule threaded bottom 745 * half. 746 * 747 * Return: Always successful. 748 */ 749 static irqreturn_t zpa2326_handle_irq(int irq, void *data) 750 { 751 struct iio_dev *indio_dev = data; 752 753 if (iio_buffer_enabled(indio_dev)) { 754 /* Timestamping needed for buffered sampling only. */ 755 ((struct zpa2326_private *) 756 iio_priv(indio_dev))->timestamp = iio_get_time_ns(indio_dev); 757 } 758 759 return IRQ_WAKE_THREAD; 760 } 761 762 /** 763 * zpa2326_handle_threaded_irq() - Interrupt bottom-half handler. 764 * @irq: Interrupt line the hardware uses to notify new data has arrived. 765 * @data: The IIO device associated with the sampling hardware. 766 * 767 * Mainly ensures interrupt is caused by a real "new sample available" 768 * condition. This relies upon the ability to perform blocking / sleeping bus 769 * accesses to slave's registers. This is why zpa2326_handle_threaded_irq() is 770 * called from within a thread, i.e. not called from hard interrupt context. 771 * 772 * When device is using its own internal hardware trigger in continuous sampling 773 * mode, data are available into hardware FIFO once interrupt has occurred. All 774 * we have to do is to dispatch the trigger, which in turn will fetch data and 775 * fill IIO buffer. 776 * 777 * When not using its own internal hardware trigger, the device has been 778 * configured in one-shot mode either by an external trigger or the IIO read_raw 779 * hook. This means one of the latter is currently waiting for sampling 780 * completion, in which case we must simply wake it up. 781 * 782 * See zpa2326_trigger_handler(). 783 * 784 * Return: 785 * %IRQ_NONE - no consistent interrupt happened ; 786 * %IRQ_HANDLED - there was new samples available. 787 */ 788 static irqreturn_t zpa2326_handle_threaded_irq(int irq, void *data) 789 { 790 struct iio_dev *indio_dev = data; 791 struct zpa2326_private *priv = iio_priv(indio_dev); 792 unsigned int val; 793 bool cont; 794 irqreturn_t ret = IRQ_NONE; 795 796 /* 797 * Are we using our own internal trigger in triggered buffer mode, i.e., 798 * currently working in continuous sampling mode ? 799 */ 800 cont = (iio_buffer_enabled(indio_dev) && 801 iio_trigger_using_own(indio_dev)); 802 803 /* 804 * Device works according to a level interrupt scheme: reading interrupt 805 * status de-asserts interrupt line. 806 */ 807 priv->result = regmap_read(priv->regmap, ZPA2326_INT_SOURCE_REG, &val); 808 if (priv->result < 0) { 809 if (cont) 810 return IRQ_NONE; 811 812 goto complete; 813 } 814 815 /* Data ready is the only interrupt source we requested. */ 816 if (!(val & ZPA2326_INT_SOURCE_DATA_READY)) { 817 /* 818 * Interrupt happened but no new sample available: likely caused 819 * by spurious interrupts, in which case, returning IRQ_NONE 820 * allows to benefit from the generic spurious interrupts 821 * handling. 822 */ 823 zpa2326_warn(indio_dev, "unexpected interrupt status %02x", 824 val); 825 826 if (cont) 827 return IRQ_NONE; 828 829 priv->result = -ENODATA; 830 goto complete; 831 } 832 833 /* New sample available: dispatch internal trigger consumers. */ 834 iio_trigger_poll_nested(priv->trigger); 835 836 if (cont) 837 /* 838 * Internal hardware trigger has been scheduled above : it will 839 * fetch data on its own. 840 */ 841 return IRQ_HANDLED; 842 843 ret = IRQ_HANDLED; 844 845 complete: 846 /* 847 * Wake up direct or externaly triggered buffer mode waiters: see 848 * zpa2326_sample_oneshot() and zpa2326_trigger_handler(). 849 */ 850 complete(&priv->data_ready); 851 852 return ret; 853 } 854 855 /** 856 * zpa2326_wait_oneshot_completion() - Wait for oneshot data ready interrupt. 857 * @indio_dev: The IIO device associated with the sampling hardware. 858 * @private: Internal private state related to @indio_dev. 859 * 860 * Return: Zero when successful, a negative error code otherwise. 861 */ 862 static int zpa2326_wait_oneshot_completion(const struct iio_dev *indio_dev, 863 struct zpa2326_private *private) 864 { 865 unsigned int val; 866 long time_left; 867 868 zpa2326_dbg(indio_dev, "waiting for one shot completion interrupt"); 869 870 time_left = wait_for_completion_interruptible_timeout( 871 &private->data_ready, ZPA2326_CONVERSION_JIFFIES); 872 if (time_left > 0) 873 /* 874 * Interrupt handler completed before timeout: return operation 875 * status. 876 */ 877 return private->result; 878 879 /* Clear all interrupts just to be sure. */ 880 regmap_read(private->regmap, ZPA2326_INT_SOURCE_REG, &val); 881 882 if (!time_left) { 883 /* Timed out. */ 884 zpa2326_warn(indio_dev, "no one shot interrupt occurred (%ld)", 885 time_left); 886 return -ETIME; 887 } 888 889 zpa2326_warn(indio_dev, "wait for one shot interrupt cancelled"); 890 return -ERESTARTSYS; 891 } 892 893 static int zpa2326_init_managed_irq(struct device *parent, 894 struct iio_dev *indio_dev, 895 struct zpa2326_private *private, 896 int irq) 897 { 898 int err; 899 900 private->irq = irq; 901 902 if (irq <= 0) { 903 /* 904 * Platform declared no interrupt line: device will be polled 905 * for data availability. 906 */ 907 dev_info(parent, "no interrupt found, running in polling mode"); 908 return 0; 909 } 910 911 init_completion(&private->data_ready); 912 913 /* Request handler to be scheduled into threaded interrupt context. */ 914 err = devm_request_threaded_irq(parent, irq, zpa2326_handle_irq, 915 zpa2326_handle_threaded_irq, 916 IRQF_TRIGGER_RISING | IRQF_ONESHOT, 917 dev_name(parent), indio_dev); 918 if (err) { 919 dev_err(parent, "failed to request interrupt %d (%d)", irq, 920 err); 921 return err; 922 } 923 924 dev_info(parent, "using interrupt %d", irq); 925 926 return 0; 927 } 928 929 /** 930 * zpa2326_poll_oneshot_completion() - Actively poll for one shot data ready. 931 * @indio_dev: The IIO device associated with the sampling hardware. 932 * 933 * Loop over registers content to detect end of sampling cycle. Used when DT 934 * declared no valid interrupt lines. 935 * 936 * Return: Zero when successful, a negative error code otherwise. 937 */ 938 static int zpa2326_poll_oneshot_completion(const struct iio_dev *indio_dev) 939 { 940 unsigned long tmout = jiffies + ZPA2326_CONVERSION_JIFFIES; 941 struct regmap *regs = ((struct zpa2326_private *) 942 iio_priv(indio_dev))->regmap; 943 unsigned int val; 944 int err; 945 946 zpa2326_dbg(indio_dev, "polling for one shot completion"); 947 948 /* 949 * At least, 100 ms is needed for the device to complete its one-shot 950 * cycle. 951 */ 952 if (msleep_interruptible(100)) 953 return -ERESTARTSYS; 954 955 /* Poll for conversion completion in hardware. */ 956 while (true) { 957 err = regmap_read(regs, ZPA2326_CTRL_REG0_REG, &val); 958 if (err < 0) 959 goto err; 960 961 if (!(val & ZPA2326_CTRL_REG0_ONE_SHOT)) 962 /* One-shot bit self clears at conversion end. */ 963 break; 964 965 if (time_after(jiffies, tmout)) { 966 /* Prevent from waiting forever : let's time out. */ 967 err = -ETIME; 968 goto err; 969 } 970 971 usleep_range(10000, 20000); 972 } 973 974 /* 975 * In oneshot mode, pressure sample availability guarantees that 976 * temperature conversion has also completed : just check pressure 977 * status bit to keep things simple. 978 */ 979 err = regmap_read(regs, ZPA2326_STATUS_REG, &val); 980 if (err < 0) 981 goto err; 982 983 if (!(val & ZPA2326_STATUS_P_DA)) { 984 /* No sample available. */ 985 err = -ENODATA; 986 goto err; 987 } 988 989 return 0; 990 991 err: 992 zpa2326_warn(indio_dev, "failed to poll one shot completion (%d)", err); 993 994 return err; 995 } 996 997 /** 998 * zpa2326_fetch_raw_sample() - Retrieve a raw sample and convert it to CPU 999 * endianness. 1000 * @indio_dev: The IIO device associated with the sampling hardware. 1001 * @type: Type of measurement / channel to fetch from. 1002 * @value: Sample output. 1003 * 1004 * Return: Zero when successful, a negative error code otherwise. 1005 */ 1006 static int zpa2326_fetch_raw_sample(const struct iio_dev *indio_dev, 1007 enum iio_chan_type type, 1008 int *value) 1009 { 1010 struct regmap *regs = ((struct zpa2326_private *) 1011 iio_priv(indio_dev))->regmap; 1012 int err; 1013 u8 v[3]; 1014 1015 switch (type) { 1016 case IIO_PRESSURE: 1017 zpa2326_dbg(indio_dev, "fetching raw pressure sample"); 1018 1019 err = regmap_bulk_read(regs, ZPA2326_PRESS_OUT_XL_REG, v, sizeof(v)); 1020 if (err) { 1021 zpa2326_warn(indio_dev, "failed to fetch pressure (%d)", 1022 err); 1023 return err; 1024 } 1025 1026 *value = get_unaligned_le24(&v[0]); 1027 1028 return IIO_VAL_INT; 1029 1030 case IIO_TEMP: 1031 zpa2326_dbg(indio_dev, "fetching raw temperature sample"); 1032 1033 err = regmap_bulk_read(regs, ZPA2326_TEMP_OUT_L_REG, value, 2); 1034 if (err) { 1035 zpa2326_warn(indio_dev, 1036 "failed to fetch temperature (%d)", err); 1037 return err; 1038 } 1039 1040 /* Temperature is a 16 bits wide little-endian signed int. */ 1041 *value = (int)le16_to_cpup((__le16 *)value); 1042 1043 return IIO_VAL_INT; 1044 1045 default: 1046 return -EINVAL; 1047 } 1048 } 1049 1050 /** 1051 * zpa2326_sample_oneshot() - Perform a complete one shot sampling cycle. 1052 * @indio_dev: The IIO device associated with the sampling hardware. 1053 * @type: Type of measurement / channel to fetch from. 1054 * @value: Sample output. 1055 * 1056 * Return: Zero when successful, a negative error code otherwise. 1057 */ 1058 static int zpa2326_sample_oneshot(struct iio_dev *indio_dev, 1059 enum iio_chan_type type, 1060 int *value) 1061 { 1062 int ret; 1063 struct zpa2326_private *priv; 1064 1065 ret = iio_device_claim_direct_mode(indio_dev); 1066 if (ret) 1067 return ret; 1068 1069 ret = zpa2326_resume(indio_dev); 1070 if (ret < 0) 1071 goto release; 1072 1073 priv = iio_priv(indio_dev); 1074 1075 if (ret > 0) { 1076 /* 1077 * We were already power supplied. Just clear hardware FIFO to 1078 * get rid of samples acquired during previous rounds (if any). 1079 * Sampling operation always generates both temperature and 1080 * pressure samples. The latter are always enqueued into 1081 * hardware FIFO. This may lead to situations were pressure 1082 * samples still sit into FIFO when previous cycle(s) fetched 1083 * temperature data only. 1084 * Hence, we need to clear hardware FIFO content to prevent from 1085 * getting outdated values at the end of current cycle. 1086 */ 1087 if (type == IIO_PRESSURE) { 1088 ret = zpa2326_clear_fifo(indio_dev, 0); 1089 if (ret) 1090 goto suspend; 1091 } 1092 } else { 1093 /* 1094 * We have just been power supplied, i.e. device is in default 1095 * "out of reset" state, meaning we need to reconfigure it 1096 * entirely. 1097 */ 1098 ret = zpa2326_config_oneshot(indio_dev, priv->irq); 1099 if (ret) 1100 goto suspend; 1101 } 1102 1103 /* Start a sampling cycle in oneshot mode. */ 1104 ret = zpa2326_start_oneshot(indio_dev); 1105 if (ret) 1106 goto suspend; 1107 1108 /* Wait for sampling cycle to complete. */ 1109 if (priv->irq > 0) 1110 ret = zpa2326_wait_oneshot_completion(indio_dev, priv); 1111 else 1112 ret = zpa2326_poll_oneshot_completion(indio_dev); 1113 1114 if (ret) 1115 goto suspend; 1116 1117 /* Retrieve raw sample value and convert it to CPU endianness. */ 1118 ret = zpa2326_fetch_raw_sample(indio_dev, type, value); 1119 1120 suspend: 1121 zpa2326_suspend(indio_dev); 1122 release: 1123 iio_device_release_direct_mode(indio_dev); 1124 1125 return ret; 1126 } 1127 1128 /** 1129 * zpa2326_trigger_handler() - Perform an IIO buffered sampling round in one 1130 * shot mode. 1131 * @irq: The software interrupt assigned to @data 1132 * @data: The IIO poll function dispatched by external trigger our device is 1133 * attached to. 1134 * 1135 * Bottom-half handler called by the IIO trigger to which our device is 1136 * currently attached. Allows us to synchronize this device buffered sampling 1137 * either with external events (such as timer expiration, external device sample 1138 * ready, etc...) or with its own interrupt (internal hardware trigger). 1139 * 1140 * When using an external trigger, basically run the same sequence of operations 1141 * as for zpa2326_sample_oneshot() with the following hereafter. Hardware FIFO 1142 * is not cleared since already done at buffering enable time and samples 1143 * dequeueing always retrieves the most recent value. 1144 * 1145 * Otherwise, when internal hardware trigger has dispatched us, just fetch data 1146 * from hardware FIFO. 1147 * 1148 * Fetched data will pushed unprocessed to IIO buffer since samples conversion 1149 * is delegated to userspace in buffered mode (endianness, etc...). 1150 * 1151 * Return: 1152 * %IRQ_NONE - no consistent interrupt happened ; 1153 * %IRQ_HANDLED - there was new samples available. 1154 */ 1155 static irqreturn_t zpa2326_trigger_handler(int irq, void *data) 1156 { 1157 struct iio_dev *indio_dev = ((struct iio_poll_func *) 1158 data)->indio_dev; 1159 struct zpa2326_private *priv = iio_priv(indio_dev); 1160 bool cont; 1161 1162 /* 1163 * We have been dispatched, meaning we are in triggered buffer mode. 1164 * Using our own internal trigger implies we are currently in continuous 1165 * hardware sampling mode. 1166 */ 1167 cont = iio_trigger_using_own(indio_dev); 1168 1169 if (!cont) { 1170 /* On demand sampling : start a one shot cycle. */ 1171 if (zpa2326_start_oneshot(indio_dev)) 1172 goto out; 1173 1174 /* Wait for sampling cycle to complete. */ 1175 if (priv->irq <= 0) { 1176 /* No interrupt available: poll for completion. */ 1177 if (zpa2326_poll_oneshot_completion(indio_dev)) 1178 goto out; 1179 1180 /* Only timestamp sample once it is ready. */ 1181 priv->timestamp = iio_get_time_ns(indio_dev); 1182 } else { 1183 /* Interrupt handlers will timestamp for us. */ 1184 if (zpa2326_wait_oneshot_completion(indio_dev, priv)) 1185 goto out; 1186 } 1187 } 1188 1189 /* Enqueue to IIO buffer / userspace. */ 1190 zpa2326_fill_sample_buffer(indio_dev, priv); 1191 1192 out: 1193 if (!cont) 1194 /* Don't switch to low power if sampling continuously. */ 1195 zpa2326_sleep(indio_dev); 1196 1197 /* Inform attached trigger we are done. */ 1198 iio_trigger_notify_done(indio_dev->trig); 1199 1200 return IRQ_HANDLED; 1201 } 1202 1203 /** 1204 * zpa2326_preenable_buffer() - Prepare device for configuring triggered 1205 * sampling 1206 * modes. 1207 * @indio_dev: The IIO device associated with the sampling hardware. 1208 * 1209 * Basically power up device. 1210 * Called with IIO device's lock held. 1211 * 1212 * Return: Zero when successful, a negative error code otherwise. 1213 */ 1214 static int zpa2326_preenable_buffer(struct iio_dev *indio_dev) 1215 { 1216 int ret = zpa2326_resume(indio_dev); 1217 1218 if (ret < 0) 1219 return ret; 1220 1221 /* Tell zpa2326_postenable_buffer() if we have just been powered on. */ 1222 ((struct zpa2326_private *) 1223 iio_priv(indio_dev))->waken = iio_priv(indio_dev); 1224 1225 return 0; 1226 } 1227 1228 /** 1229 * zpa2326_postenable_buffer() - Configure device for triggered sampling. 1230 * @indio_dev: The IIO device associated with the sampling hardware. 1231 * 1232 * Basically setup one-shot mode if plugging external trigger. 1233 * Otherwise, let internal trigger configure continuous sampling : 1234 * see zpa2326_set_trigger_state(). 1235 * 1236 * If an error is returned, IIO layer will call our postdisable hook for us, 1237 * i.e. no need to explicitly power device off here. 1238 * Called with IIO device's lock held. 1239 * 1240 * Called with IIO device's lock held. 1241 * 1242 * Return: Zero when successful, a negative error code otherwise. 1243 */ 1244 static int zpa2326_postenable_buffer(struct iio_dev *indio_dev) 1245 { 1246 const struct zpa2326_private *priv = iio_priv(indio_dev); 1247 int err; 1248 1249 if (!priv->waken) { 1250 /* 1251 * We were already power supplied. Just clear hardware FIFO to 1252 * get rid of samples acquired during previous rounds (if any). 1253 */ 1254 err = zpa2326_clear_fifo(indio_dev, 0); 1255 if (err) { 1256 zpa2326_err(indio_dev, 1257 "failed to enable buffering (%d)", err); 1258 return err; 1259 } 1260 } 1261 1262 if (!iio_trigger_using_own(indio_dev) && priv->waken) { 1263 /* 1264 * We are using an external trigger and we have just been 1265 * powered up: reconfigure one-shot mode. 1266 */ 1267 err = zpa2326_config_oneshot(indio_dev, priv->irq); 1268 if (err) { 1269 zpa2326_err(indio_dev, 1270 "failed to enable buffering (%d)", err); 1271 return err; 1272 } 1273 } 1274 1275 return 0; 1276 } 1277 1278 static int zpa2326_postdisable_buffer(struct iio_dev *indio_dev) 1279 { 1280 zpa2326_suspend(indio_dev); 1281 1282 return 0; 1283 } 1284 1285 static const struct iio_buffer_setup_ops zpa2326_buffer_setup_ops = { 1286 .preenable = zpa2326_preenable_buffer, 1287 .postenable = zpa2326_postenable_buffer, 1288 .postdisable = zpa2326_postdisable_buffer 1289 }; 1290 1291 /** 1292 * zpa2326_set_trigger_state() - Start / stop continuous sampling. 1293 * @trig: The trigger being attached to IIO device associated with the sampling 1294 * hardware. 1295 * @state: Tell whether to start (true) or stop (false) 1296 * 1297 * Basically enable / disable hardware continuous sampling mode. 1298 * 1299 * Called with IIO device's lock held at postenable() or predisable() time. 1300 * 1301 * Return: Zero when successful, a negative error code otherwise. 1302 */ 1303 static int zpa2326_set_trigger_state(struct iio_trigger *trig, bool state) 1304 { 1305 const struct iio_dev *indio_dev = dev_get_drvdata( 1306 trig->dev.parent); 1307 const struct zpa2326_private *priv = iio_priv(indio_dev); 1308 int err; 1309 1310 if (!state) { 1311 /* 1312 * Switch trigger off : in case of failure, interrupt is left 1313 * disabled in order to prevent handler from accessing released 1314 * resources. 1315 */ 1316 unsigned int val; 1317 1318 /* 1319 * As device is working in continuous mode, handlers may be 1320 * accessing resources we are currently freeing... 1321 * Prevent this by disabling interrupt handlers and ensure 1322 * the device will generate no more interrupts unless explicitly 1323 * required to, i.e. by restoring back to default one shot mode. 1324 */ 1325 disable_irq(priv->irq); 1326 1327 /* 1328 * Disable continuous sampling mode to restore settings for 1329 * one shot / direct sampling operations. 1330 */ 1331 err = regmap_write(priv->regmap, ZPA2326_CTRL_REG3_REG, 1332 zpa2326_highest_frequency()->odr); 1333 if (err) 1334 return err; 1335 1336 /* 1337 * Now that device won't generate interrupts on its own, 1338 * acknowledge any currently active interrupts (may happen on 1339 * rare occasions while stopping continuous mode). 1340 */ 1341 err = regmap_read(priv->regmap, ZPA2326_INT_SOURCE_REG, &val); 1342 if (err < 0) 1343 return err; 1344 1345 /* 1346 * Re-enable interrupts only if we can guarantee the device will 1347 * generate no more interrupts to prevent handlers from 1348 * accessing released resources. 1349 */ 1350 enable_irq(priv->irq); 1351 1352 zpa2326_dbg(indio_dev, "continuous mode stopped"); 1353 } else { 1354 /* 1355 * Switch trigger on : start continuous sampling at required 1356 * frequency. 1357 */ 1358 1359 if (priv->waken) { 1360 /* Enable interrupt if getting out of reset. */ 1361 err = regmap_write(priv->regmap, ZPA2326_CTRL_REG1_REG, 1362 (u8) 1363 ~ZPA2326_CTRL_REG1_MASK_DATA_READY); 1364 if (err) 1365 return err; 1366 } 1367 1368 /* Enable continuous sampling at specified frequency. */ 1369 err = regmap_write(priv->regmap, ZPA2326_CTRL_REG3_REG, 1370 ZPA2326_CTRL_REG3_ENABLE_MEAS | 1371 priv->frequency->odr); 1372 if (err) 1373 return err; 1374 1375 zpa2326_dbg(indio_dev, "continuous mode setup @%dHz", 1376 priv->frequency->hz); 1377 } 1378 1379 return 0; 1380 } 1381 1382 static const struct iio_trigger_ops zpa2326_trigger_ops = { 1383 .set_trigger_state = zpa2326_set_trigger_state, 1384 }; 1385 1386 /** 1387 * zpa2326_init_managed_trigger() - Create interrupt driven / hardware trigger 1388 * allowing to notify external devices a new sample is 1389 * ready. 1390 * @parent: Hardware sampling device @indio_dev is a child of. 1391 * @indio_dev: The IIO device associated with the sampling hardware. 1392 * @private: Internal private state related to @indio_dev. 1393 * @irq: Optional interrupt line the hardware uses to notify new data 1394 * samples are ready. Negative or zero values indicate no interrupts 1395 * are available, meaning polling is required. 1396 * 1397 * Only relevant when DT declares a valid interrupt line. 1398 * 1399 * Return: Zero when successful, a negative error code otherwise. 1400 */ 1401 static int zpa2326_init_managed_trigger(struct device *parent, 1402 struct iio_dev *indio_dev, 1403 struct zpa2326_private *private, 1404 int irq) 1405 { 1406 struct iio_trigger *trigger; 1407 int ret; 1408 1409 if (irq <= 0) 1410 return 0; 1411 1412 trigger = devm_iio_trigger_alloc(parent, "%s-dev%d", 1413 indio_dev->name, 1414 iio_device_id(indio_dev)); 1415 if (!trigger) 1416 return -ENOMEM; 1417 1418 /* Basic setup. */ 1419 trigger->ops = &zpa2326_trigger_ops; 1420 1421 private->trigger = trigger; 1422 1423 /* Register to triggers space. */ 1424 ret = devm_iio_trigger_register(parent, trigger); 1425 if (ret) 1426 dev_err(parent, "failed to register hardware trigger (%d)", 1427 ret); 1428 1429 return ret; 1430 } 1431 1432 static int zpa2326_get_frequency(const struct iio_dev *indio_dev) 1433 { 1434 return ((struct zpa2326_private *)iio_priv(indio_dev))->frequency->hz; 1435 } 1436 1437 static int zpa2326_set_frequency(struct iio_dev *indio_dev, int hz) 1438 { 1439 struct zpa2326_private *priv = iio_priv(indio_dev); 1440 int freq; 1441 int err; 1442 1443 /* Check if requested frequency is supported. */ 1444 for (freq = 0; freq < ARRAY_SIZE(zpa2326_sampling_frequencies); freq++) 1445 if (zpa2326_sampling_frequencies[freq].hz == hz) 1446 break; 1447 if (freq == ARRAY_SIZE(zpa2326_sampling_frequencies)) 1448 return -EINVAL; 1449 1450 /* Don't allow changing frequency if buffered sampling is ongoing. */ 1451 err = iio_device_claim_direct_mode(indio_dev); 1452 if (err) 1453 return err; 1454 1455 priv->frequency = &zpa2326_sampling_frequencies[freq]; 1456 1457 iio_device_release_direct_mode(indio_dev); 1458 1459 return 0; 1460 } 1461 1462 /* Expose supported hardware sampling frequencies (Hz) through sysfs. */ 1463 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("1 5 11 23"); 1464 1465 static struct attribute *zpa2326_attributes[] = { 1466 &iio_const_attr_sampling_frequency_available.dev_attr.attr, 1467 NULL 1468 }; 1469 1470 static const struct attribute_group zpa2326_attribute_group = { 1471 .attrs = zpa2326_attributes, 1472 }; 1473 1474 static int zpa2326_read_raw(struct iio_dev *indio_dev, 1475 struct iio_chan_spec const *chan, 1476 int *val, 1477 int *val2, 1478 long mask) 1479 { 1480 switch (mask) { 1481 case IIO_CHAN_INFO_RAW: 1482 return zpa2326_sample_oneshot(indio_dev, chan->type, val); 1483 1484 case IIO_CHAN_INFO_SCALE: 1485 switch (chan->type) { 1486 case IIO_PRESSURE: 1487 /* 1488 * Pressure resolution is 1/64 Pascal. Scale to kPascal 1489 * as required by IIO ABI. 1490 */ 1491 *val = 1; 1492 *val2 = 64000; 1493 return IIO_VAL_FRACTIONAL; 1494 1495 case IIO_TEMP: 1496 /* 1497 * Temperature follows the equation: 1498 * Temp[degC] = Tempcode * 0.00649 - 176.83 1499 * where: 1500 * Tempcode is composed the raw sampled 16 bits. 1501 * 1502 * Hence, to produce a temperature in milli-degrees 1503 * Celsius according to IIO ABI, we need to apply the 1504 * following equation to raw samples: 1505 * Temp[milli degC] = (Tempcode + Offset) * Scale 1506 * where: 1507 * Offset = -176.83 / 0.00649 1508 * Scale = 0.00649 * 1000 1509 */ 1510 *val = 6; 1511 *val2 = 490000; 1512 return IIO_VAL_INT_PLUS_MICRO; 1513 1514 default: 1515 return -EINVAL; 1516 } 1517 1518 case IIO_CHAN_INFO_OFFSET: 1519 switch (chan->type) { 1520 case IIO_TEMP: 1521 *val = -17683000; 1522 *val2 = 649; 1523 return IIO_VAL_FRACTIONAL; 1524 1525 default: 1526 return -EINVAL; 1527 } 1528 1529 case IIO_CHAN_INFO_SAMP_FREQ: 1530 *val = zpa2326_get_frequency(indio_dev); 1531 return IIO_VAL_INT; 1532 1533 default: 1534 return -EINVAL; 1535 } 1536 } 1537 1538 static int zpa2326_write_raw(struct iio_dev *indio_dev, 1539 const struct iio_chan_spec *chan, 1540 int val, 1541 int val2, 1542 long mask) 1543 { 1544 if ((mask != IIO_CHAN_INFO_SAMP_FREQ) || val2) 1545 return -EINVAL; 1546 1547 return zpa2326_set_frequency(indio_dev, val); 1548 } 1549 1550 static const struct iio_chan_spec zpa2326_channels[] = { 1551 [0] = { 1552 .type = IIO_PRESSURE, 1553 .scan_index = 0, 1554 .scan_type = { 1555 .sign = 'u', 1556 .realbits = 24, 1557 .storagebits = 32, 1558 .endianness = IIO_LE, 1559 }, 1560 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 1561 BIT(IIO_CHAN_INFO_SCALE), 1562 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), 1563 }, 1564 [1] = { 1565 .type = IIO_TEMP, 1566 .scan_index = 1, 1567 .scan_type = { 1568 .sign = 's', 1569 .realbits = 16, 1570 .storagebits = 16, 1571 .endianness = IIO_LE, 1572 }, 1573 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 1574 BIT(IIO_CHAN_INFO_SCALE) | 1575 BIT(IIO_CHAN_INFO_OFFSET), 1576 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), 1577 }, 1578 [2] = IIO_CHAN_SOFT_TIMESTAMP(2), 1579 }; 1580 1581 static const struct iio_info zpa2326_info = { 1582 .attrs = &zpa2326_attribute_group, 1583 .read_raw = zpa2326_read_raw, 1584 .write_raw = zpa2326_write_raw, 1585 }; 1586 1587 static struct iio_dev *zpa2326_create_managed_iiodev(struct device *device, 1588 const char *name, 1589 struct regmap *regmap) 1590 { 1591 struct iio_dev *indio_dev; 1592 1593 /* Allocate space to hold IIO device internal state. */ 1594 indio_dev = devm_iio_device_alloc(device, 1595 sizeof(struct zpa2326_private)); 1596 if (!indio_dev) 1597 return NULL; 1598 1599 /* Setup for userspace synchronous on demand sampling. */ 1600 indio_dev->modes = INDIO_DIRECT_MODE; 1601 indio_dev->channels = zpa2326_channels; 1602 indio_dev->num_channels = ARRAY_SIZE(zpa2326_channels); 1603 indio_dev->name = name; 1604 indio_dev->info = &zpa2326_info; 1605 1606 return indio_dev; 1607 } 1608 1609 int zpa2326_probe(struct device *parent, 1610 const char *name, 1611 int irq, 1612 unsigned int hwid, 1613 struct regmap *regmap) 1614 { 1615 struct iio_dev *indio_dev; 1616 struct zpa2326_private *priv; 1617 int err; 1618 unsigned int id; 1619 1620 indio_dev = zpa2326_create_managed_iiodev(parent, name, regmap); 1621 if (!indio_dev) 1622 return -ENOMEM; 1623 1624 priv = iio_priv(indio_dev); 1625 1626 priv->vref = devm_regulator_get(parent, "vref"); 1627 if (IS_ERR(priv->vref)) 1628 return PTR_ERR(priv->vref); 1629 1630 priv->vdd = devm_regulator_get(parent, "vdd"); 1631 if (IS_ERR(priv->vdd)) 1632 return PTR_ERR(priv->vdd); 1633 1634 /* Set default hardware sampling frequency to highest rate supported. */ 1635 priv->frequency = zpa2326_highest_frequency(); 1636 1637 /* 1638 * Plug device's underlying bus abstraction : this MUST be set before 1639 * registering interrupt handlers since an interrupt might happen if 1640 * power up sequence is not properly applied. 1641 */ 1642 priv->regmap = regmap; 1643 1644 err = devm_iio_triggered_buffer_setup(parent, indio_dev, NULL, 1645 zpa2326_trigger_handler, 1646 &zpa2326_buffer_setup_ops); 1647 if (err) 1648 return err; 1649 1650 err = zpa2326_init_managed_trigger(parent, indio_dev, priv, irq); 1651 if (err) 1652 return err; 1653 1654 err = zpa2326_init_managed_irq(parent, indio_dev, priv, irq); 1655 if (err) 1656 return err; 1657 1658 /* Power up to check device ID and perform initial hardware setup. */ 1659 err = zpa2326_power_on(indio_dev, priv); 1660 if (err) 1661 return err; 1662 1663 /* Read id register to check we are talking to the right slave. */ 1664 err = regmap_read(regmap, ZPA2326_DEVICE_ID_REG, &id); 1665 if (err) 1666 goto sleep; 1667 1668 if (id != hwid) { 1669 dev_err(parent, "found device with unexpected id %02x", id); 1670 err = -ENODEV; 1671 goto sleep; 1672 } 1673 1674 err = zpa2326_config_oneshot(indio_dev, irq); 1675 if (err) 1676 goto sleep; 1677 1678 /* Setup done : go sleeping. Device will be awaken upon user request. */ 1679 err = zpa2326_sleep(indio_dev); 1680 if (err) 1681 goto poweroff; 1682 1683 dev_set_drvdata(parent, indio_dev); 1684 1685 zpa2326_init_runtime(parent); 1686 1687 err = iio_device_register(indio_dev); 1688 if (err) { 1689 zpa2326_fini_runtime(parent); 1690 goto poweroff; 1691 } 1692 1693 return 0; 1694 1695 sleep: 1696 /* Put to sleep just in case power regulators are "dummy" ones. */ 1697 zpa2326_sleep(indio_dev); 1698 poweroff: 1699 zpa2326_power_off(indio_dev, priv); 1700 1701 return err; 1702 } 1703 EXPORT_SYMBOL_NS_GPL(zpa2326_probe, "IIO_ZPA2326"); 1704 1705 void zpa2326_remove(const struct device *parent) 1706 { 1707 struct iio_dev *indio_dev = dev_get_drvdata(parent); 1708 1709 iio_device_unregister(indio_dev); 1710 zpa2326_fini_runtime(indio_dev->dev.parent); 1711 zpa2326_sleep(indio_dev); 1712 zpa2326_power_off(indio_dev, iio_priv(indio_dev)); 1713 } 1714 EXPORT_SYMBOL_NS_GPL(zpa2326_remove, "IIO_ZPA2326"); 1715 1716 MODULE_AUTHOR("Gregor Boirie <gregor.boirie@parrot.com>"); 1717 MODULE_DESCRIPTION("Core driver for Murata ZPA2326 pressure sensor"); 1718 MODULE_LICENSE("GPL v2"); 1719