.. | .. |
---|
22 | 22 | #include <linux/iopoll.h> |
---|
23 | 23 | #include <linux/module.h> |
---|
24 | 24 | #include <linux/platform_device.h> |
---|
| 25 | +#include <linux/pm_runtime.h> |
---|
25 | 26 | #include <linux/of.h> |
---|
26 | 27 | #include <linux/of_device.h> |
---|
27 | 28 | |
---|
.. | .. |
---|
39 | 40 | #define STM32_ADC_MAX_SMP 7 /* SMPx range is [0..7] */ |
---|
40 | 41 | #define STM32_ADC_TIMEOUT_US 100000 |
---|
41 | 42 | #define STM32_ADC_TIMEOUT (msecs_to_jiffies(STM32_ADC_TIMEOUT_US / 1000)) |
---|
| 43 | +#define STM32_ADC_HW_STOP_DELAY_MS 100 |
---|
42 | 44 | |
---|
43 | 45 | #define STM32_DMA_BUFFER_SIZE PAGE_SIZE |
---|
44 | 46 | |
---|
.. | .. |
---|
90 | 92 | * @calfact_s: Calibration offset for single ended channels |
---|
91 | 93 | * @calfact_d: Calibration offset in differential |
---|
92 | 94 | * @lincalfact: Linearity calibration factor |
---|
| 95 | + * @calibrated: Indicates calibration status |
---|
93 | 96 | */ |
---|
94 | 97 | struct stm32_adc_calib { |
---|
95 | 98 | u32 calfact_s; |
---|
96 | 99 | u32 calfact_d; |
---|
97 | 100 | u32 lincalfact[STM32H7_LINCALFACT_NUM]; |
---|
| 101 | + bool calibrated; |
---|
98 | 102 | }; |
---|
99 | 103 | |
---|
100 | 104 | /** |
---|
101 | | - * stm32_adc_regs - stm32 ADC misc registers & bitfield desc |
---|
| 105 | + * struct stm32_adc_regs - stm32 ADC misc registers & bitfield desc |
---|
102 | 106 | * @reg: register offset |
---|
103 | 107 | * @mask: bitfield mask |
---|
104 | 108 | * @shift: left shift |
---|
.. | .. |
---|
110 | 114 | }; |
---|
111 | 115 | |
---|
112 | 116 | /** |
---|
113 | | - * stm32_adc_regspec - stm32 registers definition, compatible dependent data |
---|
| 117 | + * struct stm32_adc_regspec - stm32 registers definition |
---|
114 | 118 | * @dr: data register offset |
---|
115 | 119 | * @ier_eoc: interrupt enable register & eocie bitfield |
---|
| 120 | + * @ier_ovr: interrupt enable register & overrun bitfield |
---|
116 | 121 | * @isr_eoc: interrupt status register & eoc bitfield |
---|
| 122 | + * @isr_ovr: interrupt status register & overrun bitfield |
---|
117 | 123 | * @sqr: reference to sequence registers array |
---|
118 | 124 | * @exten: trigger control register & bitfield |
---|
119 | 125 | * @extsel: trigger selection register & bitfield |
---|
.. | .. |
---|
124 | 130 | struct stm32_adc_regspec { |
---|
125 | 131 | const u32 dr; |
---|
126 | 132 | const struct stm32_adc_regs ier_eoc; |
---|
| 133 | + const struct stm32_adc_regs ier_ovr; |
---|
127 | 134 | const struct stm32_adc_regs isr_eoc; |
---|
| 135 | + const struct stm32_adc_regs isr_ovr; |
---|
128 | 136 | const struct stm32_adc_regs *sqr; |
---|
129 | 137 | const struct stm32_adc_regs exten; |
---|
130 | 138 | const struct stm32_adc_regs extsel; |
---|
.. | .. |
---|
136 | 144 | struct stm32_adc; |
---|
137 | 145 | |
---|
138 | 146 | /** |
---|
139 | | - * stm32_adc_cfg - stm32 compatible configuration data |
---|
| 147 | + * struct stm32_adc_cfg - stm32 compatible configuration data |
---|
140 | 148 | * @regs: registers descriptions |
---|
141 | 149 | * @adc_info: per instance input channels definitions |
---|
142 | 150 | * @trigs: external trigger sources |
---|
143 | 151 | * @clk_required: clock is required |
---|
144 | 152 | * @has_vregready: vregready status flag presence |
---|
145 | | - * @selfcalib: optional routine for self-calibration |
---|
146 | 153 | * @prepare: optional prepare routine (power-up, enable) |
---|
147 | 154 | * @start_conv: routine to start conversions |
---|
148 | 155 | * @stop_conv: routine to stop conversions |
---|
149 | 156 | * @unprepare: optional unprepare routine (disable, power-down) |
---|
| 157 | + * @irq_clear: routine to clear irqs |
---|
150 | 158 | * @smp_cycles: programmable sampling time (ADC clock cycles) |
---|
151 | 159 | */ |
---|
152 | 160 | struct stm32_adc_cfg { |
---|
.. | .. |
---|
155 | 163 | struct stm32_adc_trig_info *trigs; |
---|
156 | 164 | bool clk_required; |
---|
157 | 165 | bool has_vregready; |
---|
158 | | - int (*selfcalib)(struct stm32_adc *); |
---|
159 | | - int (*prepare)(struct stm32_adc *); |
---|
160 | | - void (*start_conv)(struct stm32_adc *, bool dma); |
---|
161 | | - void (*stop_conv)(struct stm32_adc *); |
---|
162 | | - void (*unprepare)(struct stm32_adc *); |
---|
| 166 | + int (*prepare)(struct iio_dev *); |
---|
| 167 | + void (*start_conv)(struct iio_dev *, bool dma); |
---|
| 168 | + void (*stop_conv)(struct iio_dev *); |
---|
| 169 | + void (*unprepare)(struct iio_dev *); |
---|
| 170 | + void (*irq_clear)(struct iio_dev *indio_dev, u32 msk); |
---|
163 | 171 | const unsigned int *smp_cycles; |
---|
164 | 172 | }; |
---|
165 | 173 | |
---|
.. | .. |
---|
181 | 189 | * @rx_buf: dma rx buffer cpu address |
---|
182 | 190 | * @rx_dma_buf: dma rx buffer bus address |
---|
183 | 191 | * @rx_buf_sz: dma rx buffer size |
---|
184 | | - * @difsel bitmask to set single-ended/differential channel |
---|
185 | | - * @pcsel bitmask to preselect channels on some devices |
---|
| 192 | + * @difsel: bitmask to set single-ended/differential channel |
---|
| 193 | + * @pcsel: bitmask to preselect channels on some devices |
---|
186 | 194 | * @smpr_val: sampling time settings (e.g. smpr1 / smpr2) |
---|
187 | 195 | * @cal: optional calibration data on some devices |
---|
188 | 196 | * @chan_name: channel name array |
---|
.. | .. |
---|
252 | 260 | .num_res = ARRAY_SIZE(stm32h7_adc_resolutions), |
---|
253 | 261 | }; |
---|
254 | 262 | |
---|
255 | | -/** |
---|
| 263 | +/* |
---|
256 | 264 | * stm32f4_sq - describe regular sequence registers |
---|
257 | 265 | * - L: sequence len (register & bit field) |
---|
258 | 266 | * - SQ1..SQ16: sequence entries (register & bit field) |
---|
.. | .. |
---|
299 | 307 | {}, /* sentinel */ |
---|
300 | 308 | }; |
---|
301 | 309 | |
---|
302 | | -/** |
---|
| 310 | +/* |
---|
303 | 311 | * stm32f4_smp_bits[] - describe sampling time register index & bit fields |
---|
304 | 312 | * Sorted so it can be indexed by channel number. |
---|
305 | 313 | */ |
---|
.. | .. |
---|
335 | 343 | static const struct stm32_adc_regspec stm32f4_adc_regspec = { |
---|
336 | 344 | .dr = STM32F4_ADC_DR, |
---|
337 | 345 | .ier_eoc = { STM32F4_ADC_CR1, STM32F4_EOCIE }, |
---|
| 346 | + .ier_ovr = { STM32F4_ADC_CR1, STM32F4_OVRIE }, |
---|
338 | 347 | .isr_eoc = { STM32F4_ADC_SR, STM32F4_EOC }, |
---|
| 348 | + .isr_ovr = { STM32F4_ADC_SR, STM32F4_OVR }, |
---|
339 | 349 | .sqr = stm32f4_sq, |
---|
340 | 350 | .exten = { STM32F4_ADC_CR2, STM32F4_EXTEN_MASK, STM32F4_EXTEN_SHIFT }, |
---|
341 | 351 | .extsel = { STM32F4_ADC_CR2, STM32F4_EXTSEL_MASK, |
---|
.. | .. |
---|
390 | 400 | {}, |
---|
391 | 401 | }; |
---|
392 | 402 | |
---|
393 | | -/** |
---|
| 403 | +/* |
---|
394 | 404 | * stm32h7_smp_bits - describe sampling time register index & bit fields |
---|
395 | 405 | * Sorted so it can be indexed by channel number. |
---|
396 | 406 | */ |
---|
.. | .. |
---|
427 | 437 | static const struct stm32_adc_regspec stm32h7_adc_regspec = { |
---|
428 | 438 | .dr = STM32H7_ADC_DR, |
---|
429 | 439 | .ier_eoc = { STM32H7_ADC_IER, STM32H7_EOCIE }, |
---|
| 440 | + .ier_ovr = { STM32H7_ADC_IER, STM32H7_OVRIE }, |
---|
430 | 441 | .isr_eoc = { STM32H7_ADC_ISR, STM32H7_EOC }, |
---|
| 442 | + .isr_ovr = { STM32H7_ADC_ISR, STM32H7_OVR }, |
---|
431 | 443 | .sqr = stm32h7_sq, |
---|
432 | 444 | .exten = { STM32H7_ADC_CFGR, STM32H7_EXTEN_MASK, STM32H7_EXTEN_SHIFT }, |
---|
433 | 445 | .extsel = { STM32H7_ADC_CFGR, STM32H7_EXTSEL_MASK, |
---|
.. | .. |
---|
504 | 516 | adc->cfg->regs->ier_eoc.mask); |
---|
505 | 517 | } |
---|
506 | 518 | |
---|
| 519 | +static void stm32_adc_ovr_irq_enable(struct stm32_adc *adc) |
---|
| 520 | +{ |
---|
| 521 | + stm32_adc_set_bits(adc, adc->cfg->regs->ier_ovr.reg, |
---|
| 522 | + adc->cfg->regs->ier_ovr.mask); |
---|
| 523 | +} |
---|
| 524 | + |
---|
| 525 | +static void stm32_adc_ovr_irq_disable(struct stm32_adc *adc) |
---|
| 526 | +{ |
---|
| 527 | + stm32_adc_clr_bits(adc, adc->cfg->regs->ier_ovr.reg, |
---|
| 528 | + adc->cfg->regs->ier_ovr.mask); |
---|
| 529 | +} |
---|
| 530 | + |
---|
507 | 531 | static void stm32_adc_set_res(struct stm32_adc *adc) |
---|
508 | 532 | { |
---|
509 | 533 | const struct stm32_adc_regs *res = &adc->cfg->regs->res; |
---|
.. | .. |
---|
514 | 538 | stm32_adc_writel(adc, res->reg, val); |
---|
515 | 539 | } |
---|
516 | 540 | |
---|
| 541 | +static int stm32_adc_hw_stop(struct device *dev) |
---|
| 542 | +{ |
---|
| 543 | + struct iio_dev *indio_dev = dev_get_drvdata(dev); |
---|
| 544 | + struct stm32_adc *adc = iio_priv(indio_dev); |
---|
| 545 | + |
---|
| 546 | + if (adc->cfg->unprepare) |
---|
| 547 | + adc->cfg->unprepare(indio_dev); |
---|
| 548 | + |
---|
| 549 | + if (adc->clk) |
---|
| 550 | + clk_disable_unprepare(adc->clk); |
---|
| 551 | + |
---|
| 552 | + return 0; |
---|
| 553 | +} |
---|
| 554 | + |
---|
| 555 | +static int stm32_adc_hw_start(struct device *dev) |
---|
| 556 | +{ |
---|
| 557 | + struct iio_dev *indio_dev = dev_get_drvdata(dev); |
---|
| 558 | + struct stm32_adc *adc = iio_priv(indio_dev); |
---|
| 559 | + int ret; |
---|
| 560 | + |
---|
| 561 | + if (adc->clk) { |
---|
| 562 | + ret = clk_prepare_enable(adc->clk); |
---|
| 563 | + if (ret) |
---|
| 564 | + return ret; |
---|
| 565 | + } |
---|
| 566 | + |
---|
| 567 | + stm32_adc_set_res(adc); |
---|
| 568 | + |
---|
| 569 | + if (adc->cfg->prepare) { |
---|
| 570 | + ret = adc->cfg->prepare(indio_dev); |
---|
| 571 | + if (ret) |
---|
| 572 | + goto err_clk_dis; |
---|
| 573 | + } |
---|
| 574 | + |
---|
| 575 | + return 0; |
---|
| 576 | + |
---|
| 577 | +err_clk_dis: |
---|
| 578 | + if (adc->clk) |
---|
| 579 | + clk_disable_unprepare(adc->clk); |
---|
| 580 | + |
---|
| 581 | + return ret; |
---|
| 582 | +} |
---|
| 583 | + |
---|
517 | 584 | /** |
---|
518 | 585 | * stm32f4_adc_start_conv() - Start conversions for regular channels. |
---|
519 | | - * @adc: stm32 adc instance |
---|
| 586 | + * @indio_dev: IIO device instance |
---|
520 | 587 | * @dma: use dma to transfer conversion result |
---|
521 | 588 | * |
---|
522 | 589 | * Start conversions for regular channels. |
---|
.. | .. |
---|
524 | 591 | * conversions, in IIO buffer modes. Otherwise, use ADC interrupt with direct |
---|
525 | 592 | * DR read instead (e.g. read_raw, or triggered buffer mode without DMA). |
---|
526 | 593 | */ |
---|
527 | | -static void stm32f4_adc_start_conv(struct stm32_adc *adc, bool dma) |
---|
| 594 | +static void stm32f4_adc_start_conv(struct iio_dev *indio_dev, bool dma) |
---|
528 | 595 | { |
---|
| 596 | + struct stm32_adc *adc = iio_priv(indio_dev); |
---|
| 597 | + |
---|
529 | 598 | stm32_adc_set_bits(adc, STM32F4_ADC_CR1, STM32F4_SCAN); |
---|
530 | 599 | |
---|
531 | 600 | if (dma) |
---|
.. | .. |
---|
542 | 611 | stm32_adc_set_bits(adc, STM32F4_ADC_CR2, STM32F4_SWSTART); |
---|
543 | 612 | } |
---|
544 | 613 | |
---|
545 | | -static void stm32f4_adc_stop_conv(struct stm32_adc *adc) |
---|
| 614 | +static void stm32f4_adc_stop_conv(struct iio_dev *indio_dev) |
---|
546 | 615 | { |
---|
| 616 | + struct stm32_adc *adc = iio_priv(indio_dev); |
---|
| 617 | + |
---|
547 | 618 | stm32_adc_clr_bits(adc, STM32F4_ADC_CR2, STM32F4_EXTEN_MASK); |
---|
548 | 619 | stm32_adc_clr_bits(adc, STM32F4_ADC_SR, STM32F4_STRT); |
---|
549 | 620 | |
---|
.. | .. |
---|
552 | 623 | STM32F4_ADON | STM32F4_DMA | STM32F4_DDS); |
---|
553 | 624 | } |
---|
554 | 625 | |
---|
555 | | -static void stm32h7_adc_start_conv(struct stm32_adc *adc, bool dma) |
---|
| 626 | +static void stm32f4_adc_irq_clear(struct iio_dev *indio_dev, u32 msk) |
---|
556 | 627 | { |
---|
| 628 | + struct stm32_adc *adc = iio_priv(indio_dev); |
---|
| 629 | + |
---|
| 630 | + stm32_adc_clr_bits(adc, adc->cfg->regs->isr_eoc.reg, msk); |
---|
| 631 | +} |
---|
| 632 | + |
---|
| 633 | +static void stm32h7_adc_start_conv(struct iio_dev *indio_dev, bool dma) |
---|
| 634 | +{ |
---|
| 635 | + struct stm32_adc *adc = iio_priv(indio_dev); |
---|
557 | 636 | enum stm32h7_adc_dmngt dmngt; |
---|
558 | 637 | unsigned long flags; |
---|
559 | 638 | u32 val; |
---|
.. | .. |
---|
572 | 651 | stm32_adc_set_bits(adc, STM32H7_ADC_CR, STM32H7_ADSTART); |
---|
573 | 652 | } |
---|
574 | 653 | |
---|
575 | | -static void stm32h7_adc_stop_conv(struct stm32_adc *adc) |
---|
| 654 | +static void stm32h7_adc_stop_conv(struct iio_dev *indio_dev) |
---|
576 | 655 | { |
---|
577 | | - struct iio_dev *indio_dev = iio_priv_to_dev(adc); |
---|
| 656 | + struct stm32_adc *adc = iio_priv(indio_dev); |
---|
578 | 657 | int ret; |
---|
579 | 658 | u32 val; |
---|
580 | 659 | |
---|
.. | .. |
---|
589 | 668 | stm32_adc_clr_bits(adc, STM32H7_ADC_CFGR, STM32H7_DMNGT_MASK); |
---|
590 | 669 | } |
---|
591 | 670 | |
---|
592 | | -static int stm32h7_adc_exit_pwr_down(struct stm32_adc *adc) |
---|
| 671 | +static void stm32h7_adc_irq_clear(struct iio_dev *indio_dev, u32 msk) |
---|
593 | 672 | { |
---|
594 | | - struct iio_dev *indio_dev = iio_priv_to_dev(adc); |
---|
| 673 | + struct stm32_adc *adc = iio_priv(indio_dev); |
---|
| 674 | + /* On STM32H7 IRQs are cleared by writing 1 into ISR register */ |
---|
| 675 | + stm32_adc_set_bits(adc, adc->cfg->regs->isr_eoc.reg, msk); |
---|
| 676 | +} |
---|
| 677 | + |
---|
| 678 | +static int stm32h7_adc_exit_pwr_down(struct iio_dev *indio_dev) |
---|
| 679 | +{ |
---|
| 680 | + struct stm32_adc *adc = iio_priv(indio_dev); |
---|
595 | 681 | int ret; |
---|
596 | 682 | u32 val; |
---|
597 | 683 | |
---|
.. | .. |
---|
627 | 713 | stm32_adc_set_bits(adc, STM32H7_ADC_CR, STM32H7_DEEPPWD); |
---|
628 | 714 | } |
---|
629 | 715 | |
---|
630 | | -static int stm32h7_adc_enable(struct stm32_adc *adc) |
---|
| 716 | +static int stm32h7_adc_enable(struct iio_dev *indio_dev) |
---|
631 | 717 | { |
---|
632 | | - struct iio_dev *indio_dev = iio_priv_to_dev(adc); |
---|
| 718 | + struct stm32_adc *adc = iio_priv(indio_dev); |
---|
633 | 719 | int ret; |
---|
634 | 720 | u32 val; |
---|
635 | 721 | |
---|
.. | .. |
---|
650 | 736 | return ret; |
---|
651 | 737 | } |
---|
652 | 738 | |
---|
653 | | -static void stm32h7_adc_disable(struct stm32_adc *adc) |
---|
| 739 | +static void stm32h7_adc_disable(struct iio_dev *indio_dev) |
---|
654 | 740 | { |
---|
655 | | - struct iio_dev *indio_dev = iio_priv_to_dev(adc); |
---|
| 741 | + struct stm32_adc *adc = iio_priv(indio_dev); |
---|
656 | 742 | int ret; |
---|
657 | 743 | u32 val; |
---|
658 | 744 | |
---|
.. | .. |
---|
667 | 753 | |
---|
668 | 754 | /** |
---|
669 | 755 | * stm32h7_adc_read_selfcalib() - read calibration shadow regs, save result |
---|
670 | | - * @adc: stm32 adc instance |
---|
| 756 | + * @indio_dev: IIO device instance |
---|
| 757 | + * Note: Must be called once ADC is enabled, so LINCALRDYW[1..6] are writable |
---|
671 | 758 | */ |
---|
672 | | -static int stm32h7_adc_read_selfcalib(struct stm32_adc *adc) |
---|
| 759 | +static int stm32h7_adc_read_selfcalib(struct iio_dev *indio_dev) |
---|
673 | 760 | { |
---|
674 | | - struct iio_dev *indio_dev = iio_priv_to_dev(adc); |
---|
| 761 | + struct stm32_adc *adc = iio_priv(indio_dev); |
---|
675 | 762 | int i, ret; |
---|
676 | 763 | u32 lincalrdyw_mask, val; |
---|
677 | | - |
---|
678 | | - /* Enable adc so LINCALRDYW1..6 bits are writable */ |
---|
679 | | - ret = stm32h7_adc_enable(adc); |
---|
680 | | - if (ret) |
---|
681 | | - return ret; |
---|
682 | 764 | |
---|
683 | 765 | /* Read linearity calibration */ |
---|
684 | 766 | lincalrdyw_mask = STM32H7_LINCALRDYW6; |
---|
.. | .. |
---|
692 | 774 | 100, STM32_ADC_TIMEOUT_US); |
---|
693 | 775 | if (ret) { |
---|
694 | 776 | dev_err(&indio_dev->dev, "Failed to read calfact\n"); |
---|
695 | | - goto disable; |
---|
| 777 | + return ret; |
---|
696 | 778 | } |
---|
697 | 779 | |
---|
698 | 780 | val = stm32_adc_readl(adc, STM32H7_ADC_CALFACT2); |
---|
.. | .. |
---|
708 | 790 | adc->cal.calfact_s >>= STM32H7_CALFACT_S_SHIFT; |
---|
709 | 791 | adc->cal.calfact_d = (val & STM32H7_CALFACT_D_MASK); |
---|
710 | 792 | adc->cal.calfact_d >>= STM32H7_CALFACT_D_SHIFT; |
---|
| 793 | + adc->cal.calibrated = true; |
---|
711 | 794 | |
---|
712 | | -disable: |
---|
713 | | - stm32h7_adc_disable(adc); |
---|
714 | | - |
---|
715 | | - return ret; |
---|
| 795 | + return 0; |
---|
716 | 796 | } |
---|
717 | 797 | |
---|
718 | 798 | /** |
---|
719 | 799 | * stm32h7_adc_restore_selfcalib() - Restore saved self-calibration result |
---|
720 | | - * @adc: stm32 adc instance |
---|
| 800 | + * @indio_dev: IIO device instance |
---|
721 | 801 | * Note: ADC must be enabled, with no on-going conversions. |
---|
722 | 802 | */ |
---|
723 | | -static int stm32h7_adc_restore_selfcalib(struct stm32_adc *adc) |
---|
| 803 | +static int stm32h7_adc_restore_selfcalib(struct iio_dev *indio_dev) |
---|
724 | 804 | { |
---|
725 | | - struct iio_dev *indio_dev = iio_priv_to_dev(adc); |
---|
| 805 | + struct stm32_adc *adc = iio_priv(indio_dev); |
---|
726 | 806 | int i, ret; |
---|
727 | 807 | u32 lincalrdyw_mask, val; |
---|
728 | 808 | |
---|
.. | .. |
---|
789 | 869 | #define STM32H7_ADC_CALIB_TIMEOUT_US 100000 |
---|
790 | 870 | |
---|
791 | 871 | /** |
---|
792 | | - * stm32h7_adc_selfcalib() - Procedure to calibrate ADC (from power down) |
---|
793 | | - * @adc: stm32 adc instance |
---|
794 | | - * Exit from power down, calibrate ADC, then return to power down. |
---|
| 872 | + * stm32h7_adc_selfcalib() - Procedure to calibrate ADC |
---|
| 873 | + * @indio_dev: IIO device instance |
---|
| 874 | + * Note: Must be called once ADC is out of power down. |
---|
795 | 875 | */ |
---|
796 | | -static int stm32h7_adc_selfcalib(struct stm32_adc *adc) |
---|
| 876 | +static int stm32h7_adc_selfcalib(struct iio_dev *indio_dev) |
---|
797 | 877 | { |
---|
798 | | - struct iio_dev *indio_dev = iio_priv_to_dev(adc); |
---|
| 878 | + struct stm32_adc *adc = iio_priv(indio_dev); |
---|
799 | 879 | int ret; |
---|
800 | 880 | u32 val; |
---|
801 | 881 | |
---|
802 | | - ret = stm32h7_adc_exit_pwr_down(adc); |
---|
803 | | - if (ret) |
---|
804 | | - return ret; |
---|
| 882 | + if (adc->cal.calibrated) |
---|
| 883 | + return true; |
---|
805 | 884 | |
---|
806 | 885 | /* |
---|
807 | 886 | * Select calibration mode: |
---|
.. | .. |
---|
818 | 897 | STM32H7_ADC_CALIB_TIMEOUT_US); |
---|
819 | 898 | if (ret) { |
---|
820 | 899 | dev_err(&indio_dev->dev, "calibration failed\n"); |
---|
821 | | - goto pwr_dwn; |
---|
| 900 | + goto out; |
---|
822 | 901 | } |
---|
823 | 902 | |
---|
824 | 903 | /* |
---|
.. | .. |
---|
835 | 914 | STM32H7_ADC_CALIB_TIMEOUT_US); |
---|
836 | 915 | if (ret) { |
---|
837 | 916 | dev_err(&indio_dev->dev, "calibration failed\n"); |
---|
838 | | - goto pwr_dwn; |
---|
| 917 | + goto out; |
---|
839 | 918 | } |
---|
840 | 919 | |
---|
| 920 | +out: |
---|
841 | 921 | stm32_adc_clr_bits(adc, STM32H7_ADC_CR, |
---|
842 | 922 | STM32H7_ADCALDIF | STM32H7_ADCALLIN); |
---|
843 | | - |
---|
844 | | - /* Read calibration result for future reference */ |
---|
845 | | - ret = stm32h7_adc_read_selfcalib(adc); |
---|
846 | | - |
---|
847 | | -pwr_dwn: |
---|
848 | | - stm32h7_adc_enter_pwr_down(adc); |
---|
849 | 923 | |
---|
850 | 924 | return ret; |
---|
851 | 925 | } |
---|
852 | 926 | |
---|
853 | 927 | /** |
---|
854 | 928 | * stm32h7_adc_prepare() - Leave power down mode to enable ADC. |
---|
855 | | - * @adc: stm32 adc instance |
---|
| 929 | + * @indio_dev: IIO device instance |
---|
856 | 930 | * Leave power down mode. |
---|
857 | 931 | * Configure channels as single ended or differential before enabling ADC. |
---|
858 | 932 | * Enable ADC. |
---|
.. | .. |
---|
861 | 935 | * - Only one input is selected for single ended (e.g. 'vinp') |
---|
862 | 936 | * - Two inputs are selected for differential channels (e.g. 'vinp' & 'vinn') |
---|
863 | 937 | */ |
---|
864 | | -static int stm32h7_adc_prepare(struct stm32_adc *adc) |
---|
| 938 | +static int stm32h7_adc_prepare(struct iio_dev *indio_dev) |
---|
865 | 939 | { |
---|
866 | | - int ret; |
---|
| 940 | + struct stm32_adc *adc = iio_priv(indio_dev); |
---|
| 941 | + int calib, ret; |
---|
867 | 942 | |
---|
868 | | - ret = stm32h7_adc_exit_pwr_down(adc); |
---|
| 943 | + ret = stm32h7_adc_exit_pwr_down(indio_dev); |
---|
869 | 944 | if (ret) |
---|
870 | 945 | return ret; |
---|
871 | 946 | |
---|
| 947 | + ret = stm32h7_adc_selfcalib(indio_dev); |
---|
| 948 | + if (ret < 0) |
---|
| 949 | + goto pwr_dwn; |
---|
| 950 | + calib = ret; |
---|
| 951 | + |
---|
872 | 952 | stm32_adc_writel(adc, STM32H7_ADC_DIFSEL, adc->difsel); |
---|
873 | 953 | |
---|
874 | | - ret = stm32h7_adc_enable(adc); |
---|
| 954 | + ret = stm32h7_adc_enable(indio_dev); |
---|
875 | 955 | if (ret) |
---|
876 | 956 | goto pwr_dwn; |
---|
877 | 957 | |
---|
878 | | - ret = stm32h7_adc_restore_selfcalib(adc); |
---|
| 958 | + /* Either restore or read calibration result for future reference */ |
---|
| 959 | + if (calib) |
---|
| 960 | + ret = stm32h7_adc_restore_selfcalib(indio_dev); |
---|
| 961 | + else |
---|
| 962 | + ret = stm32h7_adc_read_selfcalib(indio_dev); |
---|
879 | 963 | if (ret) |
---|
880 | 964 | goto disable; |
---|
881 | 965 | |
---|
.. | .. |
---|
884 | 968 | return 0; |
---|
885 | 969 | |
---|
886 | 970 | disable: |
---|
887 | | - stm32h7_adc_disable(adc); |
---|
| 971 | + stm32h7_adc_disable(indio_dev); |
---|
888 | 972 | pwr_dwn: |
---|
889 | 973 | stm32h7_adc_enter_pwr_down(adc); |
---|
890 | 974 | |
---|
891 | 975 | return ret; |
---|
892 | 976 | } |
---|
893 | 977 | |
---|
894 | | -static void stm32h7_adc_unprepare(struct stm32_adc *adc) |
---|
| 978 | +static void stm32h7_adc_unprepare(struct iio_dev *indio_dev) |
---|
895 | 979 | { |
---|
896 | | - stm32h7_adc_disable(adc); |
---|
| 980 | + struct stm32_adc *adc = iio_priv(indio_dev); |
---|
| 981 | + |
---|
| 982 | + stm32_adc_writel(adc, STM32H7_ADC_PCSEL, 0); |
---|
| 983 | + stm32h7_adc_disable(indio_dev); |
---|
897 | 984 | stm32h7_adc_enter_pwr_down(adc); |
---|
898 | 985 | } |
---|
899 | 986 | |
---|
.. | .. |
---|
954 | 1041 | |
---|
955 | 1042 | /** |
---|
956 | 1043 | * stm32_adc_get_trig_extsel() - Get external trigger selection |
---|
| 1044 | + * @indio_dev: IIO device structure |
---|
957 | 1045 | * @trig: trigger |
---|
958 | 1046 | * |
---|
959 | 1047 | * Returns trigger extsel value, if trig matches, -EINVAL otherwise. |
---|
.. | .. |
---|
1065 | 1153 | int *res) |
---|
1066 | 1154 | { |
---|
1067 | 1155 | struct stm32_adc *adc = iio_priv(indio_dev); |
---|
| 1156 | + struct device *dev = indio_dev->dev.parent; |
---|
1068 | 1157 | const struct stm32_adc_regspec *regs = adc->cfg->regs; |
---|
1069 | 1158 | long timeout; |
---|
1070 | 1159 | u32 val; |
---|
.. | .. |
---|
1074 | 1163 | |
---|
1075 | 1164 | adc->bufi = 0; |
---|
1076 | 1165 | |
---|
1077 | | - if (adc->cfg->prepare) { |
---|
1078 | | - ret = adc->cfg->prepare(adc); |
---|
1079 | | - if (ret) |
---|
1080 | | - return ret; |
---|
| 1166 | + ret = pm_runtime_get_sync(dev); |
---|
| 1167 | + if (ret < 0) { |
---|
| 1168 | + pm_runtime_put_noidle(dev); |
---|
| 1169 | + return ret; |
---|
1081 | 1170 | } |
---|
1082 | 1171 | |
---|
1083 | 1172 | /* Apply sampling time settings */ |
---|
.. | .. |
---|
1098 | 1187 | |
---|
1099 | 1188 | stm32_adc_conv_irq_enable(adc); |
---|
1100 | 1189 | |
---|
1101 | | - adc->cfg->start_conv(adc, false); |
---|
| 1190 | + adc->cfg->start_conv(indio_dev, false); |
---|
1102 | 1191 | |
---|
1103 | 1192 | timeout = wait_for_completion_interruptible_timeout( |
---|
1104 | 1193 | &adc->completion, STM32_ADC_TIMEOUT); |
---|
.. | .. |
---|
1111 | 1200 | ret = IIO_VAL_INT; |
---|
1112 | 1201 | } |
---|
1113 | 1202 | |
---|
1114 | | - adc->cfg->stop_conv(adc); |
---|
| 1203 | + adc->cfg->stop_conv(indio_dev); |
---|
1115 | 1204 | |
---|
1116 | 1205 | stm32_adc_conv_irq_disable(adc); |
---|
1117 | 1206 | |
---|
1118 | | - if (adc->cfg->unprepare) |
---|
1119 | | - adc->cfg->unprepare(adc); |
---|
| 1207 | + pm_runtime_mark_last_busy(dev); |
---|
| 1208 | + pm_runtime_put_autosuspend(dev); |
---|
1120 | 1209 | |
---|
1121 | 1210 | return ret; |
---|
1122 | 1211 | } |
---|
.. | .. |
---|
1163 | 1252 | } |
---|
1164 | 1253 | } |
---|
1165 | 1254 | |
---|
1166 | | -static irqreturn_t stm32_adc_isr(int irq, void *data) |
---|
| 1255 | +static void stm32_adc_irq_clear(struct iio_dev *indio_dev, u32 msk) |
---|
1167 | 1256 | { |
---|
1168 | | - struct stm32_adc *adc = data; |
---|
1169 | | - struct iio_dev *indio_dev = iio_priv_to_dev(adc); |
---|
| 1257 | + struct stm32_adc *adc = iio_priv(indio_dev); |
---|
| 1258 | + |
---|
| 1259 | + adc->cfg->irq_clear(indio_dev, msk); |
---|
| 1260 | +} |
---|
| 1261 | + |
---|
| 1262 | +static irqreturn_t stm32_adc_threaded_isr(int irq, void *data) |
---|
| 1263 | +{ |
---|
| 1264 | + struct iio_dev *indio_dev = data; |
---|
| 1265 | + struct stm32_adc *adc = iio_priv(indio_dev); |
---|
1170 | 1266 | const struct stm32_adc_regspec *regs = adc->cfg->regs; |
---|
1171 | 1267 | u32 status = stm32_adc_readl(adc, regs->isr_eoc.reg); |
---|
| 1268 | + |
---|
| 1269 | + /* Check ovr status right now, as ovr mask should be already disabled */ |
---|
| 1270 | + if (status & regs->isr_ovr.mask) { |
---|
| 1271 | + /* |
---|
| 1272 | + * Clear ovr bit to avoid subsequent calls to IRQ handler. |
---|
| 1273 | + * This requires to stop ADC first. OVR bit state in ISR, |
---|
| 1274 | + * is propaged to CSR register by hardware. |
---|
| 1275 | + */ |
---|
| 1276 | + adc->cfg->stop_conv(indio_dev); |
---|
| 1277 | + stm32_adc_irq_clear(indio_dev, regs->isr_ovr.mask); |
---|
| 1278 | + dev_err(&indio_dev->dev, "Overrun, stopping: restart needed\n"); |
---|
| 1279 | + return IRQ_HANDLED; |
---|
| 1280 | + } |
---|
| 1281 | + |
---|
| 1282 | + return IRQ_NONE; |
---|
| 1283 | +} |
---|
| 1284 | + |
---|
| 1285 | +static irqreturn_t stm32_adc_isr(int irq, void *data) |
---|
| 1286 | +{ |
---|
| 1287 | + struct iio_dev *indio_dev = data; |
---|
| 1288 | + struct stm32_adc *adc = iio_priv(indio_dev); |
---|
| 1289 | + const struct stm32_adc_regspec *regs = adc->cfg->regs; |
---|
| 1290 | + u32 status = stm32_adc_readl(adc, regs->isr_eoc.reg); |
---|
| 1291 | + |
---|
| 1292 | + if (status & regs->isr_ovr.mask) { |
---|
| 1293 | + /* |
---|
| 1294 | + * Overrun occurred on regular conversions: data for wrong |
---|
| 1295 | + * channel may be read. Unconditionally disable interrupts |
---|
| 1296 | + * to stop processing data and print error message. |
---|
| 1297 | + * Restarting the capture can be done by disabling, then |
---|
| 1298 | + * re-enabling it (e.g. write 0, then 1 to buffer/enable). |
---|
| 1299 | + */ |
---|
| 1300 | + stm32_adc_ovr_irq_disable(adc); |
---|
| 1301 | + stm32_adc_conv_irq_disable(adc); |
---|
| 1302 | + return IRQ_WAKE_THREAD; |
---|
| 1303 | + } |
---|
1172 | 1304 | |
---|
1173 | 1305 | if (status & regs->isr_eoc.mask) { |
---|
1174 | 1306 | /* Reading DR also clears EOC status flag */ |
---|
.. | .. |
---|
1224 | 1356 | const unsigned long *scan_mask) |
---|
1225 | 1357 | { |
---|
1226 | 1358 | struct stm32_adc *adc = iio_priv(indio_dev); |
---|
| 1359 | + struct device *dev = indio_dev->dev.parent; |
---|
1227 | 1360 | int ret; |
---|
| 1361 | + |
---|
| 1362 | + ret = pm_runtime_get_sync(dev); |
---|
| 1363 | + if (ret < 0) { |
---|
| 1364 | + pm_runtime_put_noidle(dev); |
---|
| 1365 | + return ret; |
---|
| 1366 | + } |
---|
1228 | 1367 | |
---|
1229 | 1368 | adc->num_conv = bitmap_weight(scan_mask, indio_dev->masklength); |
---|
1230 | 1369 | |
---|
1231 | 1370 | ret = stm32_adc_conf_scan_seq(indio_dev, scan_mask); |
---|
1232 | | - if (ret) |
---|
1233 | | - return ret; |
---|
| 1371 | + pm_runtime_mark_last_busy(dev); |
---|
| 1372 | + pm_runtime_put_autosuspend(dev); |
---|
1234 | 1373 | |
---|
1235 | | - return 0; |
---|
| 1374 | + return ret; |
---|
1236 | 1375 | } |
---|
1237 | 1376 | |
---|
1238 | 1377 | static int stm32_adc_of_xlate(struct iio_dev *indio_dev, |
---|
.. | .. |
---|
1249 | 1388 | |
---|
1250 | 1389 | /** |
---|
1251 | 1390 | * stm32_adc_debugfs_reg_access - read or write register value |
---|
| 1391 | + * @indio_dev: IIO device structure |
---|
| 1392 | + * @reg: register offset |
---|
| 1393 | + * @writeval: value to write |
---|
| 1394 | + * @readval: value to read |
---|
1252 | 1395 | * |
---|
1253 | 1396 | * To read a value from an ADC register: |
---|
1254 | 1397 | * echo [ADC reg offset] > direct_reg_access |
---|
.. | .. |
---|
1262 | 1405 | unsigned *readval) |
---|
1263 | 1406 | { |
---|
1264 | 1407 | struct stm32_adc *adc = iio_priv(indio_dev); |
---|
| 1408 | + struct device *dev = indio_dev->dev.parent; |
---|
| 1409 | + int ret; |
---|
| 1410 | + |
---|
| 1411 | + ret = pm_runtime_get_sync(dev); |
---|
| 1412 | + if (ret < 0) { |
---|
| 1413 | + pm_runtime_put_noidle(dev); |
---|
| 1414 | + return ret; |
---|
| 1415 | + } |
---|
1265 | 1416 | |
---|
1266 | 1417 | if (!readval) |
---|
1267 | 1418 | stm32_adc_writel(adc, reg, writeval); |
---|
1268 | 1419 | else |
---|
1269 | 1420 | *readval = stm32_adc_readl(adc, reg); |
---|
| 1421 | + |
---|
| 1422 | + pm_runtime_mark_last_busy(dev); |
---|
| 1423 | + pm_runtime_put_autosuspend(dev); |
---|
1270 | 1424 | |
---|
1271 | 1425 | return 0; |
---|
1272 | 1426 | } |
---|
.. | .. |
---|
1375 | 1529 | static int stm32_adc_buffer_postenable(struct iio_dev *indio_dev) |
---|
1376 | 1530 | { |
---|
1377 | 1531 | struct stm32_adc *adc = iio_priv(indio_dev); |
---|
| 1532 | + struct device *dev = indio_dev->dev.parent; |
---|
1378 | 1533 | int ret; |
---|
1379 | 1534 | |
---|
1380 | | - if (adc->cfg->prepare) { |
---|
1381 | | - ret = adc->cfg->prepare(adc); |
---|
1382 | | - if (ret) |
---|
1383 | | - return ret; |
---|
| 1535 | + ret = pm_runtime_get_sync(dev); |
---|
| 1536 | + if (ret < 0) { |
---|
| 1537 | + pm_runtime_put_noidle(dev); |
---|
| 1538 | + return ret; |
---|
1384 | 1539 | } |
---|
1385 | 1540 | |
---|
1386 | 1541 | ret = stm32_adc_set_trig(indio_dev, indio_dev->trig); |
---|
1387 | 1542 | if (ret) { |
---|
1388 | 1543 | dev_err(&indio_dev->dev, "Can't set trigger\n"); |
---|
1389 | | - goto err_unprepare; |
---|
| 1544 | + goto err_pm_put; |
---|
1390 | 1545 | } |
---|
1391 | 1546 | |
---|
1392 | 1547 | ret = stm32_adc_dma_start(indio_dev); |
---|
.. | .. |
---|
1395 | 1550 | goto err_clr_trig; |
---|
1396 | 1551 | } |
---|
1397 | 1552 | |
---|
1398 | | - ret = iio_triggered_buffer_postenable(indio_dev); |
---|
1399 | | - if (ret < 0) |
---|
1400 | | - goto err_stop_dma; |
---|
1401 | | - |
---|
1402 | 1553 | /* Reset adc buffer index */ |
---|
1403 | 1554 | adc->bufi = 0; |
---|
| 1555 | + |
---|
| 1556 | + stm32_adc_ovr_irq_enable(adc); |
---|
1404 | 1557 | |
---|
1405 | 1558 | if (!adc->dma_chan) |
---|
1406 | 1559 | stm32_adc_conv_irq_enable(adc); |
---|
1407 | 1560 | |
---|
1408 | | - adc->cfg->start_conv(adc, !!adc->dma_chan); |
---|
| 1561 | + adc->cfg->start_conv(indio_dev, !!adc->dma_chan); |
---|
1409 | 1562 | |
---|
1410 | 1563 | return 0; |
---|
1411 | 1564 | |
---|
1412 | | -err_stop_dma: |
---|
1413 | | - if (adc->dma_chan) |
---|
1414 | | - dmaengine_terminate_all(adc->dma_chan); |
---|
1415 | 1565 | err_clr_trig: |
---|
1416 | 1566 | stm32_adc_set_trig(indio_dev, NULL); |
---|
1417 | | -err_unprepare: |
---|
1418 | | - if (adc->cfg->unprepare) |
---|
1419 | | - adc->cfg->unprepare(adc); |
---|
| 1567 | +err_pm_put: |
---|
| 1568 | + pm_runtime_mark_last_busy(dev); |
---|
| 1569 | + pm_runtime_put_autosuspend(dev); |
---|
1420 | 1570 | |
---|
1421 | 1571 | return ret; |
---|
1422 | 1572 | } |
---|
.. | .. |
---|
1424 | 1574 | static int stm32_adc_buffer_predisable(struct iio_dev *indio_dev) |
---|
1425 | 1575 | { |
---|
1426 | 1576 | struct stm32_adc *adc = iio_priv(indio_dev); |
---|
1427 | | - int ret; |
---|
| 1577 | + struct device *dev = indio_dev->dev.parent; |
---|
1428 | 1578 | |
---|
1429 | | - adc->cfg->stop_conv(adc); |
---|
| 1579 | + adc->cfg->stop_conv(indio_dev); |
---|
1430 | 1580 | if (!adc->dma_chan) |
---|
1431 | 1581 | stm32_adc_conv_irq_disable(adc); |
---|
1432 | 1582 | |
---|
1433 | | - ret = iio_triggered_buffer_predisable(indio_dev); |
---|
1434 | | - if (ret < 0) |
---|
1435 | | - dev_err(&indio_dev->dev, "predisable failed\n"); |
---|
| 1583 | + stm32_adc_ovr_irq_disable(adc); |
---|
1436 | 1584 | |
---|
1437 | 1585 | if (adc->dma_chan) |
---|
1438 | 1586 | dmaengine_terminate_sync(adc->dma_chan); |
---|
.. | .. |
---|
1440 | 1588 | if (stm32_adc_set_trig(indio_dev, NULL)) |
---|
1441 | 1589 | dev_err(&indio_dev->dev, "Can't clear trigger\n"); |
---|
1442 | 1590 | |
---|
1443 | | - if (adc->cfg->unprepare) |
---|
1444 | | - adc->cfg->unprepare(adc); |
---|
| 1591 | + pm_runtime_mark_last_busy(dev); |
---|
| 1592 | + pm_runtime_put_autosuspend(dev); |
---|
1445 | 1593 | |
---|
1446 | | - return ret; |
---|
| 1594 | + return 0; |
---|
1447 | 1595 | } |
---|
1448 | 1596 | |
---|
1449 | 1597 | static const struct iio_buffer_setup_ops stm32_adc_buffer_setup_ops = { |
---|
.. | .. |
---|
1691 | 1839 | adc->dma_chan = dma_request_chan(dev, "rx"); |
---|
1692 | 1840 | if (IS_ERR(adc->dma_chan)) { |
---|
1693 | 1841 | ret = PTR_ERR(adc->dma_chan); |
---|
1694 | | - if (ret != -ENODEV) { |
---|
1695 | | - if (ret != -EPROBE_DEFER) |
---|
1696 | | - dev_err(dev, |
---|
1697 | | - "DMA channel request failed with %d\n", |
---|
1698 | | - ret); |
---|
1699 | | - return ret; |
---|
1700 | | - } |
---|
| 1842 | + if (ret != -ENODEV) |
---|
| 1843 | + return dev_err_probe(dev, ret, |
---|
| 1844 | + "DMA channel request failed with\n"); |
---|
1701 | 1845 | |
---|
1702 | 1846 | /* DMA is optional: fall back to IRQ mode */ |
---|
1703 | 1847 | adc->dma_chan = NULL; |
---|
.. | .. |
---|
1756 | 1900 | of_match_device(dev->driver->of_match_table, dev)->data; |
---|
1757 | 1901 | |
---|
1758 | 1902 | indio_dev->name = dev_name(&pdev->dev); |
---|
1759 | | - indio_dev->dev.parent = &pdev->dev; |
---|
1760 | 1903 | indio_dev->dev.of_node = pdev->dev.of_node; |
---|
1761 | 1904 | indio_dev->info = &stm32_adc_iio_info; |
---|
1762 | 1905 | indio_dev->modes = INDIO_DIRECT_MODE | INDIO_HARDWARE_TRIGGERED; |
---|
1763 | 1906 | |
---|
1764 | | - platform_set_drvdata(pdev, adc); |
---|
| 1907 | + platform_set_drvdata(pdev, indio_dev); |
---|
1765 | 1908 | |
---|
1766 | 1909 | ret = of_property_read_u32(pdev->dev.of_node, "reg", &adc->offset); |
---|
1767 | 1910 | if (ret != 0) { |
---|
.. | .. |
---|
1770 | 1913 | } |
---|
1771 | 1914 | |
---|
1772 | 1915 | adc->irq = platform_get_irq(pdev, 0); |
---|
1773 | | - if (adc->irq < 0) { |
---|
1774 | | - dev_err(&pdev->dev, "failed to get irq\n"); |
---|
| 1916 | + if (adc->irq < 0) |
---|
1775 | 1917 | return adc->irq; |
---|
1776 | | - } |
---|
1777 | 1918 | |
---|
1778 | | - ret = devm_request_irq(&pdev->dev, adc->irq, stm32_adc_isr, |
---|
1779 | | - 0, pdev->name, adc); |
---|
| 1919 | + ret = devm_request_threaded_irq(&pdev->dev, adc->irq, stm32_adc_isr, |
---|
| 1920 | + stm32_adc_threaded_isr, |
---|
| 1921 | + 0, pdev->name, indio_dev); |
---|
1780 | 1922 | if (ret) { |
---|
1781 | 1923 | dev_err(&pdev->dev, "failed to request IRQ\n"); |
---|
1782 | 1924 | return ret; |
---|
.. | .. |
---|
1793 | 1935 | } |
---|
1794 | 1936 | } |
---|
1795 | 1937 | |
---|
1796 | | - if (adc->clk) { |
---|
1797 | | - ret = clk_prepare_enable(adc->clk); |
---|
1798 | | - if (ret < 0) { |
---|
1799 | | - dev_err(&pdev->dev, "clk enable failed\n"); |
---|
1800 | | - return ret; |
---|
1801 | | - } |
---|
1802 | | - } |
---|
1803 | | - |
---|
1804 | 1938 | ret = stm32_adc_of_get_resolution(indio_dev); |
---|
1805 | 1939 | if (ret < 0) |
---|
1806 | | - goto err_clk_disable; |
---|
1807 | | - stm32_adc_set_res(adc); |
---|
1808 | | - |
---|
1809 | | - if (adc->cfg->selfcalib) { |
---|
1810 | | - ret = adc->cfg->selfcalib(adc); |
---|
1811 | | - if (ret) |
---|
1812 | | - goto err_clk_disable; |
---|
1813 | | - } |
---|
| 1940 | + return ret; |
---|
1814 | 1941 | |
---|
1815 | 1942 | ret = stm32_adc_chan_of_init(indio_dev); |
---|
1816 | 1943 | if (ret < 0) |
---|
1817 | | - goto err_clk_disable; |
---|
| 1944 | + return ret; |
---|
1818 | 1945 | |
---|
1819 | 1946 | ret = stm32_adc_dma_request(dev, indio_dev); |
---|
1820 | 1947 | if (ret < 0) |
---|
1821 | | - goto err_clk_disable; |
---|
| 1948 | + return ret; |
---|
1822 | 1949 | |
---|
1823 | 1950 | if (!adc->dma_chan) |
---|
1824 | 1951 | handler = &stm32_adc_trigger_handler; |
---|
.. | .. |
---|
1831 | 1958 | goto err_dma_disable; |
---|
1832 | 1959 | } |
---|
1833 | 1960 | |
---|
| 1961 | + /* Get stm32-adc-core PM online */ |
---|
| 1962 | + pm_runtime_get_noresume(dev); |
---|
| 1963 | + pm_runtime_set_active(dev); |
---|
| 1964 | + pm_runtime_set_autosuspend_delay(dev, STM32_ADC_HW_STOP_DELAY_MS); |
---|
| 1965 | + pm_runtime_use_autosuspend(dev); |
---|
| 1966 | + pm_runtime_enable(dev); |
---|
| 1967 | + |
---|
| 1968 | + ret = stm32_adc_hw_start(dev); |
---|
| 1969 | + if (ret) |
---|
| 1970 | + goto err_buffer_cleanup; |
---|
| 1971 | + |
---|
1834 | 1972 | ret = iio_device_register(indio_dev); |
---|
1835 | 1973 | if (ret) { |
---|
1836 | 1974 | dev_err(&pdev->dev, "iio dev register failed\n"); |
---|
1837 | | - goto err_buffer_cleanup; |
---|
| 1975 | + goto err_hw_stop; |
---|
1838 | 1976 | } |
---|
| 1977 | + |
---|
| 1978 | + pm_runtime_mark_last_busy(dev); |
---|
| 1979 | + pm_runtime_put_autosuspend(dev); |
---|
1839 | 1980 | |
---|
1840 | 1981 | return 0; |
---|
1841 | 1982 | |
---|
| 1983 | +err_hw_stop: |
---|
| 1984 | + stm32_adc_hw_stop(dev); |
---|
| 1985 | + |
---|
1842 | 1986 | err_buffer_cleanup: |
---|
| 1987 | + pm_runtime_disable(dev); |
---|
| 1988 | + pm_runtime_set_suspended(dev); |
---|
| 1989 | + pm_runtime_put_noidle(dev); |
---|
1843 | 1990 | iio_triggered_buffer_cleanup(indio_dev); |
---|
1844 | 1991 | |
---|
1845 | 1992 | err_dma_disable: |
---|
.. | .. |
---|
1849 | 1996 | adc->rx_buf, adc->rx_dma_buf); |
---|
1850 | 1997 | dma_release_channel(adc->dma_chan); |
---|
1851 | 1998 | } |
---|
1852 | | -err_clk_disable: |
---|
1853 | | - if (adc->clk) |
---|
1854 | | - clk_disable_unprepare(adc->clk); |
---|
1855 | 1999 | |
---|
1856 | 2000 | return ret; |
---|
1857 | 2001 | } |
---|
1858 | 2002 | |
---|
1859 | 2003 | static int stm32_adc_remove(struct platform_device *pdev) |
---|
1860 | 2004 | { |
---|
1861 | | - struct stm32_adc *adc = platform_get_drvdata(pdev); |
---|
1862 | | - struct iio_dev *indio_dev = iio_priv_to_dev(adc); |
---|
| 2005 | + struct iio_dev *indio_dev = platform_get_drvdata(pdev); |
---|
| 2006 | + struct stm32_adc *adc = iio_priv(indio_dev); |
---|
1863 | 2007 | |
---|
| 2008 | + pm_runtime_get_sync(&pdev->dev); |
---|
1864 | 2009 | iio_device_unregister(indio_dev); |
---|
| 2010 | + stm32_adc_hw_stop(&pdev->dev); |
---|
| 2011 | + pm_runtime_disable(&pdev->dev); |
---|
| 2012 | + pm_runtime_set_suspended(&pdev->dev); |
---|
| 2013 | + pm_runtime_put_noidle(&pdev->dev); |
---|
1865 | 2014 | iio_triggered_buffer_cleanup(indio_dev); |
---|
1866 | 2015 | if (adc->dma_chan) { |
---|
1867 | 2016 | dma_free_coherent(adc->dma_chan->device->dev, |
---|
.. | .. |
---|
1869 | 2018 | adc->rx_buf, adc->rx_dma_buf); |
---|
1870 | 2019 | dma_release_channel(adc->dma_chan); |
---|
1871 | 2020 | } |
---|
1872 | | - if (adc->clk) |
---|
1873 | | - clk_disable_unprepare(adc->clk); |
---|
1874 | 2021 | |
---|
1875 | 2022 | return 0; |
---|
1876 | 2023 | } |
---|
| 2024 | + |
---|
| 2025 | +#if defined(CONFIG_PM_SLEEP) |
---|
| 2026 | +static int stm32_adc_suspend(struct device *dev) |
---|
| 2027 | +{ |
---|
| 2028 | + struct iio_dev *indio_dev = dev_get_drvdata(dev); |
---|
| 2029 | + |
---|
| 2030 | + if (iio_buffer_enabled(indio_dev)) |
---|
| 2031 | + stm32_adc_buffer_predisable(indio_dev); |
---|
| 2032 | + |
---|
| 2033 | + return pm_runtime_force_suspend(dev); |
---|
| 2034 | +} |
---|
| 2035 | + |
---|
| 2036 | +static int stm32_adc_resume(struct device *dev) |
---|
| 2037 | +{ |
---|
| 2038 | + struct iio_dev *indio_dev = dev_get_drvdata(dev); |
---|
| 2039 | + int ret; |
---|
| 2040 | + |
---|
| 2041 | + ret = pm_runtime_force_resume(dev); |
---|
| 2042 | + if (ret < 0) |
---|
| 2043 | + return ret; |
---|
| 2044 | + |
---|
| 2045 | + if (!iio_buffer_enabled(indio_dev)) |
---|
| 2046 | + return 0; |
---|
| 2047 | + |
---|
| 2048 | + ret = stm32_adc_update_scan_mode(indio_dev, |
---|
| 2049 | + indio_dev->active_scan_mask); |
---|
| 2050 | + if (ret < 0) |
---|
| 2051 | + return ret; |
---|
| 2052 | + |
---|
| 2053 | + return stm32_adc_buffer_postenable(indio_dev); |
---|
| 2054 | +} |
---|
| 2055 | +#endif |
---|
| 2056 | + |
---|
| 2057 | +#if defined(CONFIG_PM) |
---|
| 2058 | +static int stm32_adc_runtime_suspend(struct device *dev) |
---|
| 2059 | +{ |
---|
| 2060 | + return stm32_adc_hw_stop(dev); |
---|
| 2061 | +} |
---|
| 2062 | + |
---|
| 2063 | +static int stm32_adc_runtime_resume(struct device *dev) |
---|
| 2064 | +{ |
---|
| 2065 | + return stm32_adc_hw_start(dev); |
---|
| 2066 | +} |
---|
| 2067 | +#endif |
---|
| 2068 | + |
---|
| 2069 | +static const struct dev_pm_ops stm32_adc_pm_ops = { |
---|
| 2070 | + SET_SYSTEM_SLEEP_PM_OPS(stm32_adc_suspend, stm32_adc_resume) |
---|
| 2071 | + SET_RUNTIME_PM_OPS(stm32_adc_runtime_suspend, stm32_adc_runtime_resume, |
---|
| 2072 | + NULL) |
---|
| 2073 | +}; |
---|
1877 | 2074 | |
---|
1878 | 2075 | static const struct stm32_adc_cfg stm32f4_adc_cfg = { |
---|
1879 | 2076 | .regs = &stm32f4_adc_regspec, |
---|
.. | .. |
---|
1883 | 2080 | .start_conv = stm32f4_adc_start_conv, |
---|
1884 | 2081 | .stop_conv = stm32f4_adc_stop_conv, |
---|
1885 | 2082 | .smp_cycles = stm32f4_adc_smp_cycles, |
---|
| 2083 | + .irq_clear = stm32f4_adc_irq_clear, |
---|
1886 | 2084 | }; |
---|
1887 | 2085 | |
---|
1888 | 2086 | static const struct stm32_adc_cfg stm32h7_adc_cfg = { |
---|
1889 | 2087 | .regs = &stm32h7_adc_regspec, |
---|
1890 | 2088 | .adc_info = &stm32h7_adc_info, |
---|
1891 | 2089 | .trigs = stm32h7_adc_trigs, |
---|
1892 | | - .selfcalib = stm32h7_adc_selfcalib, |
---|
1893 | 2090 | .start_conv = stm32h7_adc_start_conv, |
---|
1894 | 2091 | .stop_conv = stm32h7_adc_stop_conv, |
---|
1895 | 2092 | .prepare = stm32h7_adc_prepare, |
---|
1896 | 2093 | .unprepare = stm32h7_adc_unprepare, |
---|
1897 | 2094 | .smp_cycles = stm32h7_adc_smp_cycles, |
---|
| 2095 | + .irq_clear = stm32h7_adc_irq_clear, |
---|
1898 | 2096 | }; |
---|
1899 | 2097 | |
---|
1900 | 2098 | static const struct stm32_adc_cfg stm32mp1_adc_cfg = { |
---|
.. | .. |
---|
1902 | 2100 | .adc_info = &stm32h7_adc_info, |
---|
1903 | 2101 | .trigs = stm32h7_adc_trigs, |
---|
1904 | 2102 | .has_vregready = true, |
---|
1905 | | - .selfcalib = stm32h7_adc_selfcalib, |
---|
1906 | 2103 | .start_conv = stm32h7_adc_start_conv, |
---|
1907 | 2104 | .stop_conv = stm32h7_adc_stop_conv, |
---|
1908 | 2105 | .prepare = stm32h7_adc_prepare, |
---|
1909 | 2106 | .unprepare = stm32h7_adc_unprepare, |
---|
1910 | 2107 | .smp_cycles = stm32h7_adc_smp_cycles, |
---|
| 2108 | + .irq_clear = stm32h7_adc_irq_clear, |
---|
1911 | 2109 | }; |
---|
1912 | 2110 | |
---|
1913 | 2111 | static const struct of_device_id stm32_adc_of_match[] = { |
---|
.. | .. |
---|
1924 | 2122 | .driver = { |
---|
1925 | 2123 | .name = "stm32-adc", |
---|
1926 | 2124 | .of_match_table = stm32_adc_of_match, |
---|
| 2125 | + .pm = &stm32_adc_pm_ops, |
---|
1927 | 2126 | }, |
---|
1928 | 2127 | }; |
---|
1929 | 2128 | module_platform_driver(stm32_adc_driver); |
---|