1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * This file is part of STM32 ADC driver
4 *
5 * Copyright (C) 2016, STMicroelectronics - All Rights Reserved
6 * Author: Fabrice Gasnier <fabrice.gasnier@st.com>.
7 *
8 * Inspired from: fsl-imx25-tsadc
9 *
10 */
11
12 #include <linux/bitfield.h>
13 #include <linux/clk.h>
14 #include <linux/interrupt.h>
15 #include <linux/irqchip/chained_irq.h>
16 #include <linux/irqdesc.h>
17 #include <linux/irqdomain.h>
18 #include <linux/mfd/syscon.h>
19 #include <linux/module.h>
20 #include <linux/of_device.h>
21 #include <linux/of_platform.h>
22 #include <linux/platform_device.h>
23 #include <linux/pm_runtime.h>
24 #include <linux/regmap.h>
25 #include <linux/regulator/consumer.h>
26 #include <linux/slab.h>
27 #include <linux/units.h>
28
29 #include "stm32-adc-core.h"
30
31 #define STM32_ADC_CORE_SLEEP_DELAY_MS 2000
32
33 /* SYSCFG registers */
34 #define STM32MP1_SYSCFG_PMCSETR 0x04
35 #define STM32MP1_SYSCFG_PMCCLRR 0x44
36
37 /* SYSCFG bit fields */
38 #define STM32MP1_SYSCFG_ANASWVDD_MASK BIT(9)
39
40 /* SYSCFG capability flags */
41 #define HAS_VBOOSTER BIT(0)
42 #define HAS_ANASWVDD BIT(1)
43
44 /**
45 * struct stm32_adc_common_regs - stm32 common registers
46 * @csr: common status register offset
47 * @ccr: common control register offset
48 * @eoc_msk: array of eoc (end of conversion flag) masks in csr for adc1..n
49 * @ovr_msk: array of ovr (overrun flag) masks in csr for adc1..n
50 * @ier: interrupt enable register offset for each adc
51 * @eocie_msk: end of conversion interrupt enable mask in @ier
52 */
53 struct stm32_adc_common_regs {
54 u32 csr;
55 u32 ccr;
56 u32 eoc_msk[STM32_ADC_MAX_ADCS];
57 u32 ovr_msk[STM32_ADC_MAX_ADCS];
58 u32 ier;
59 u32 eocie_msk;
60 };
61
62 struct stm32_adc_priv;
63
64 /**
65 * struct stm32_adc_priv_cfg - stm32 core compatible configuration data
66 * @regs: common registers for all instances
67 * @clk_sel: clock selection routine
68 * @max_clk_rate_hz: maximum analog clock rate (Hz, from datasheet)
69 * @ipid: adc identification number
70 * @has_syscfg: SYSCFG capability flags
71 * @num_irqs: number of interrupt lines
72 * @num_adcs: maximum number of ADC instances in the common registers
73 */
74 struct stm32_adc_priv_cfg {
75 const struct stm32_adc_common_regs *regs;
76 int (*clk_sel)(struct platform_device *, struct stm32_adc_priv *);
77 u32 max_clk_rate_hz;
78 u32 ipid;
79 unsigned int has_syscfg;
80 unsigned int num_irqs;
81 unsigned int num_adcs;
82 };
83
84 /**
85 * struct stm32_adc_priv - stm32 ADC core private data
86 * @irq: irq(s) for ADC block
87 * @nb_adc_max: actual maximum number of instance per ADC block
88 * @domain: irq domain reference
89 * @aclk: clock reference for the analog circuitry
90 * @bclk: bus clock common for all ADCs, depends on part used
91 * @max_clk_rate: desired maximum clock rate
92 * @booster: booster supply reference
93 * @vdd: vdd supply reference
94 * @vdda: vdda analog supply reference
95 * @vref: regulator reference
96 * @vdd_uv: vdd supply voltage (microvolts)
97 * @vdda_uv: vdda supply voltage (microvolts)
98 * @cfg: compatible configuration data
99 * @common: common data for all ADC instances
100 * @ccr_bak: backup CCR in low power mode
101 * @syscfg: reference to syscon, system control registers
102 */
103 struct stm32_adc_priv {
104 int irq[STM32_ADC_MAX_ADCS];
105 unsigned int nb_adc_max;
106 struct irq_domain *domain;
107 struct clk *aclk;
108 struct clk *bclk;
109 u32 max_clk_rate;
110 struct regulator *booster;
111 struct regulator *vdd;
112 struct regulator *vdda;
113 struct regulator *vref;
114 int vdd_uv;
115 int vdda_uv;
116 const struct stm32_adc_priv_cfg *cfg;
117 struct stm32_adc_common common;
118 u32 ccr_bak;
119 struct regmap *syscfg;
120 };
121
to_stm32_adc_priv(struct stm32_adc_common * com)122 static struct stm32_adc_priv *to_stm32_adc_priv(struct stm32_adc_common *com)
123 {
124 return container_of(com, struct stm32_adc_priv, common);
125 }
126
127 /* STM32F4 ADC internal common clock prescaler division ratios */
128 static int stm32f4_pclk_div[] = {2, 4, 6, 8};
129
130 /**
131 * stm32f4_adc_clk_sel() - Select stm32f4 ADC common clock prescaler
132 * @pdev: platform device
133 * @priv: stm32 ADC core private data
134 * Select clock prescaler used for analog conversions, before using ADC.
135 */
stm32f4_adc_clk_sel(struct platform_device * pdev,struct stm32_adc_priv * priv)136 static int stm32f4_adc_clk_sel(struct platform_device *pdev,
137 struct stm32_adc_priv *priv)
138 {
139 unsigned long rate;
140 u32 val;
141 int i;
142
143 /* stm32f4 has one clk input for analog (mandatory), enforce it here */
144 if (!priv->aclk) {
145 dev_err(&pdev->dev, "No 'adc' clock found\n");
146 return -ENOENT;
147 }
148
149 rate = clk_get_rate(priv->aclk);
150 if (!rate) {
151 dev_err(&pdev->dev, "Invalid clock rate: 0\n");
152 return -EINVAL;
153 }
154
155 for (i = 0; i < ARRAY_SIZE(stm32f4_pclk_div); i++) {
156 if ((rate / stm32f4_pclk_div[i]) <= priv->max_clk_rate)
157 break;
158 }
159 if (i >= ARRAY_SIZE(stm32f4_pclk_div)) {
160 dev_err(&pdev->dev, "adc clk selection failed\n");
161 return -EINVAL;
162 }
163
164 priv->common.rate = rate / stm32f4_pclk_div[i];
165 val = readl_relaxed(priv->common.base + STM32F4_ADC_CCR);
166 val &= ~STM32F4_ADC_ADCPRE_MASK;
167 val |= i << STM32F4_ADC_ADCPRE_SHIFT;
168 writel_relaxed(val, priv->common.base + STM32F4_ADC_CCR);
169
170 dev_dbg(&pdev->dev, "Using analog clock source at %ld kHz\n",
171 priv->common.rate / 1000);
172
173 return 0;
174 }
175
176 /**
177 * struct stm32h7_adc_ck_spec - specification for stm32h7 adc clock
178 * @ckmode: ADC clock mode, Async or sync with prescaler.
179 * @presc: prescaler bitfield for async clock mode
180 * @div: prescaler division ratio
181 */
182 struct stm32h7_adc_ck_spec {
183 u32 ckmode;
184 u32 presc;
185 int div;
186 };
187
188 static const struct stm32h7_adc_ck_spec stm32h7_adc_ckmodes_spec[] = {
189 /* 00: CK_ADC[1..3]: Asynchronous clock modes */
190 { 0, 0, 1 },
191 { 0, 1, 2 },
192 { 0, 2, 4 },
193 { 0, 3, 6 },
194 { 0, 4, 8 },
195 { 0, 5, 10 },
196 { 0, 6, 12 },
197 { 0, 7, 16 },
198 { 0, 8, 32 },
199 { 0, 9, 64 },
200 { 0, 10, 128 },
201 { 0, 11, 256 },
202 /* HCLK used: Synchronous clock modes (1, 2 or 4 prescaler) */
203 { 1, 0, 1 },
204 { 2, 0, 2 },
205 { 3, 0, 4 },
206 };
207
stm32h7_adc_clk_sel(struct platform_device * pdev,struct stm32_adc_priv * priv)208 static int stm32h7_adc_clk_sel(struct platform_device *pdev,
209 struct stm32_adc_priv *priv)
210 {
211 u32 ckmode, presc, val;
212 unsigned long rate;
213 int i, div, duty;
214
215 /* stm32h7 bus clock is common for all ADC instances (mandatory) */
216 if (!priv->bclk) {
217 dev_err(&pdev->dev, "No 'bus' clock found\n");
218 return -ENOENT;
219 }
220
221 /*
222 * stm32h7 can use either 'bus' or 'adc' clock for analog circuitry.
223 * So, choice is to have bus clock mandatory and adc clock optional.
224 * If optional 'adc' clock has been found, then try to use it first.
225 */
226 if (priv->aclk) {
227 /*
228 * Asynchronous clock modes (e.g. ckmode == 0)
229 * From spec: PLL output musn't exceed max rate
230 */
231 rate = clk_get_rate(priv->aclk);
232 if (!rate) {
233 dev_err(&pdev->dev, "Invalid adc clock rate: 0\n");
234 return -EINVAL;
235 }
236
237 /* If duty is an error, kindly use at least /2 divider */
238 duty = clk_get_scaled_duty_cycle(priv->aclk, 100);
239 if (duty < 0)
240 dev_warn(&pdev->dev, "adc clock duty: %d\n", duty);
241
242 for (i = 0; i < ARRAY_SIZE(stm32h7_adc_ckmodes_spec); i++) {
243 ckmode = stm32h7_adc_ckmodes_spec[i].ckmode;
244 presc = stm32h7_adc_ckmodes_spec[i].presc;
245 div = stm32h7_adc_ckmodes_spec[i].div;
246
247 if (ckmode)
248 continue;
249
250 /*
251 * For proper operation, clock duty cycle range is 49%
252 * to 51%. Apply at least /2 prescaler otherwise.
253 */
254 if (div == 1 && (duty < 49 || duty > 51))
255 continue;
256
257 if ((rate / div) <= priv->max_clk_rate)
258 goto out;
259 }
260 }
261
262 /* Synchronous clock modes (e.g. ckmode is 1, 2 or 3) */
263 rate = clk_get_rate(priv->bclk);
264 if (!rate) {
265 dev_err(&pdev->dev, "Invalid bus clock rate: 0\n");
266 return -EINVAL;
267 }
268
269 duty = clk_get_scaled_duty_cycle(priv->bclk, 100);
270 if (duty < 0)
271 dev_warn(&pdev->dev, "bus clock duty: %d\n", duty);
272
273 for (i = 0; i < ARRAY_SIZE(stm32h7_adc_ckmodes_spec); i++) {
274 ckmode = stm32h7_adc_ckmodes_spec[i].ckmode;
275 presc = stm32h7_adc_ckmodes_spec[i].presc;
276 div = stm32h7_adc_ckmodes_spec[i].div;
277
278 if (!ckmode)
279 continue;
280
281 if (div == 1 && (duty < 49 || duty > 51))
282 continue;
283
284 if ((rate / div) <= priv->max_clk_rate)
285 goto out;
286 }
287
288 dev_err(&pdev->dev, "adc clk selection failed\n");
289 return -EINVAL;
290
291 out:
292 /* rate used later by each ADC instance to control BOOST mode */
293 priv->common.rate = rate / div;
294
295 /* Set common clock mode and prescaler */
296 val = readl_relaxed(priv->common.base + STM32H7_ADC_CCR);
297 val &= ~(STM32H7_CKMODE_MASK | STM32H7_PRESC_MASK);
298 val |= ckmode << STM32H7_CKMODE_SHIFT;
299 val |= presc << STM32H7_PRESC_SHIFT;
300 writel_relaxed(val, priv->common.base + STM32H7_ADC_CCR);
301
302 dev_dbg(&pdev->dev, "Using %s clock/%d source at %ld kHz\n",
303 ckmode ? "bus" : "adc", div, priv->common.rate / 1000);
304
305 return 0;
306 }
307
308 /* STM32F4 common registers definitions */
309 static const struct stm32_adc_common_regs stm32f4_adc_common_regs = {
310 .csr = STM32F4_ADC_CSR,
311 .ccr = STM32F4_ADC_CCR,
312 .eoc_msk = { STM32F4_EOC1, STM32F4_EOC2, STM32F4_EOC3 },
313 .ovr_msk = { STM32F4_OVR1, STM32F4_OVR2, STM32F4_OVR3 },
314 .ier = STM32F4_ADC_CR1,
315 .eocie_msk = STM32F4_EOCIE,
316 };
317
318 /* STM32H7 common registers definitions */
319 static const struct stm32_adc_common_regs stm32h7_adc_common_regs = {
320 .csr = STM32H7_ADC_CSR,
321 .ccr = STM32H7_ADC_CCR,
322 .eoc_msk = { STM32H7_EOC_MST, STM32H7_EOC_SLV },
323 .ovr_msk = { STM32H7_OVR_MST, STM32H7_OVR_SLV },
324 .ier = STM32H7_ADC_IER,
325 .eocie_msk = STM32H7_EOCIE,
326 };
327
328 /* STM32MP13 common registers definitions */
329 static const struct stm32_adc_common_regs stm32mp13_adc_common_regs = {
330 .csr = STM32H7_ADC_CSR,
331 .ccr = STM32H7_ADC_CCR,
332 .eoc_msk = { STM32H7_EOC_MST },
333 .ovr_msk = { STM32H7_OVR_MST },
334 .ier = STM32H7_ADC_IER,
335 .eocie_msk = STM32H7_EOCIE,
336 };
337
338 static const unsigned int stm32_adc_offset[STM32_ADC_MAX_ADCS] = {
339 0, STM32_ADC_OFFSET, STM32_ADC_OFFSET * 2,
340 };
341
stm32_adc_eoc_enabled(struct stm32_adc_priv * priv,unsigned int adc)342 static unsigned int stm32_adc_eoc_enabled(struct stm32_adc_priv *priv,
343 unsigned int adc)
344 {
345 u32 ier, offset = stm32_adc_offset[adc];
346
347 ier = readl_relaxed(priv->common.base + offset + priv->cfg->regs->ier);
348
349 return ier & priv->cfg->regs->eocie_msk;
350 }
351
352 /* ADC common interrupt for all instances */
stm32_adc_irq_handler(struct irq_desc * desc)353 static void stm32_adc_irq_handler(struct irq_desc *desc)
354 {
355 struct stm32_adc_priv *priv = irq_desc_get_handler_data(desc);
356 struct irq_chip *chip = irq_desc_get_chip(desc);
357 int i;
358 u32 status;
359
360 chained_irq_enter(chip, desc);
361 status = readl_relaxed(priv->common.base + priv->cfg->regs->csr);
362
363 /*
364 * End of conversion may be handled by using IRQ or DMA. There may be a
365 * race here when two conversions complete at the same time on several
366 * ADCs. EOC may be read 'set' for several ADCs, with:
367 * - an ADC configured to use DMA (EOC triggers the DMA request, and
368 * is then automatically cleared by DR read in hardware)
369 * - an ADC configured to use IRQs (EOCIE bit is set. The handler must
370 * be called in this case)
371 * So both EOC status bit in CSR and EOCIE control bit must be checked
372 * before invoking the interrupt handler (e.g. call ISR only for
373 * IRQ-enabled ADCs).
374 */
375 for (i = 0; i < priv->nb_adc_max; i++) {
376 if ((status & priv->cfg->regs->eoc_msk[i] &&
377 stm32_adc_eoc_enabled(priv, i)) ||
378 (status & priv->cfg->regs->ovr_msk[i]))
379 generic_handle_domain_irq(priv->domain, i);
380 }
381
382 chained_irq_exit(chip, desc);
383 };
384
stm32_adc_domain_map(struct irq_domain * d,unsigned int irq,irq_hw_number_t hwirq)385 static int stm32_adc_domain_map(struct irq_domain *d, unsigned int irq,
386 irq_hw_number_t hwirq)
387 {
388 irq_set_chip_data(irq, d->host_data);
389 irq_set_chip_and_handler(irq, &dummy_irq_chip, handle_level_irq);
390
391 return 0;
392 }
393
stm32_adc_domain_unmap(struct irq_domain * d,unsigned int irq)394 static void stm32_adc_domain_unmap(struct irq_domain *d, unsigned int irq)
395 {
396 irq_set_chip_and_handler(irq, NULL, NULL);
397 irq_set_chip_data(irq, NULL);
398 }
399
400 static const struct irq_domain_ops stm32_adc_domain_ops = {
401 .map = stm32_adc_domain_map,
402 .unmap = stm32_adc_domain_unmap,
403 .xlate = irq_domain_xlate_onecell,
404 };
405
stm32_adc_irq_probe(struct platform_device * pdev,struct stm32_adc_priv * priv)406 static int stm32_adc_irq_probe(struct platform_device *pdev,
407 struct stm32_adc_priv *priv)
408 {
409 struct device_node *np = pdev->dev.of_node;
410 unsigned int i;
411
412 /*
413 * Interrupt(s) must be provided, depending on the compatible:
414 * - stm32f4/h7 shares a common interrupt line.
415 * - stm32mp1, has one line per ADC
416 */
417 for (i = 0; i < priv->cfg->num_irqs; i++) {
418 priv->irq[i] = platform_get_irq(pdev, i);
419 if (priv->irq[i] < 0)
420 return priv->irq[i];
421 }
422
423 priv->domain = irq_domain_add_simple(np, STM32_ADC_MAX_ADCS, 0,
424 &stm32_adc_domain_ops,
425 priv);
426 if (!priv->domain) {
427 dev_err(&pdev->dev, "Failed to add irq domain\n");
428 return -ENOMEM;
429 }
430
431 for (i = 0; i < priv->cfg->num_irqs; i++)
432 irq_set_chained_handler_and_data(priv->irq[i],
433 stm32_adc_irq_handler, priv);
434
435 return 0;
436 }
437
stm32_adc_irq_remove(struct platform_device * pdev,struct stm32_adc_priv * priv)438 static void stm32_adc_irq_remove(struct platform_device *pdev,
439 struct stm32_adc_priv *priv)
440 {
441 int hwirq;
442 unsigned int i;
443
444 for (hwirq = 0; hwirq < priv->nb_adc_max; hwirq++)
445 irq_dispose_mapping(irq_find_mapping(priv->domain, hwirq));
446 irq_domain_remove(priv->domain);
447
448 for (i = 0; i < priv->cfg->num_irqs; i++)
449 irq_set_chained_handler(priv->irq[i], NULL);
450 }
451
stm32_adc_core_switches_supply_en(struct stm32_adc_priv * priv,struct device * dev)452 static int stm32_adc_core_switches_supply_en(struct stm32_adc_priv *priv,
453 struct device *dev)
454 {
455 int ret;
456
457 /*
458 * On STM32H7 and STM32MP1, the ADC inputs are multiplexed with analog
459 * switches (via PCSEL) which have reduced performances when their
460 * supply is below 2.7V (vdda by default):
461 * - Voltage booster can be used, to get full ADC performances
462 * (increases power consumption).
463 * - Vdd can be used to supply them, if above 2.7V (STM32MP1 only).
464 *
465 * Recommended settings for ANASWVDD and EN_BOOSTER:
466 * - vdda < 2.7V but vdd > 2.7V: ANASWVDD = 1, EN_BOOSTER = 0 (stm32mp1)
467 * - vdda < 2.7V and vdd < 2.7V: ANASWVDD = 0, EN_BOOSTER = 1
468 * - vdda >= 2.7V: ANASWVDD = 0, EN_BOOSTER = 0 (default)
469 */
470 if (priv->vdda_uv < 2700000) {
471 if (priv->syscfg && priv->vdd_uv > 2700000) {
472 ret = regulator_enable(priv->vdd);
473 if (ret < 0) {
474 dev_err(dev, "vdd enable failed %d\n", ret);
475 return ret;
476 }
477
478 ret = regmap_write(priv->syscfg,
479 STM32MP1_SYSCFG_PMCSETR,
480 STM32MP1_SYSCFG_ANASWVDD_MASK);
481 if (ret < 0) {
482 regulator_disable(priv->vdd);
483 dev_err(dev, "vdd select failed, %d\n", ret);
484 return ret;
485 }
486 dev_dbg(dev, "analog switches supplied by vdd\n");
487
488 return 0;
489 }
490
491 if (priv->booster) {
492 /*
493 * This is optional, as this is a trade-off between
494 * analog performance and power consumption.
495 */
496 ret = regulator_enable(priv->booster);
497 if (ret < 0) {
498 dev_err(dev, "booster enable failed %d\n", ret);
499 return ret;
500 }
501 dev_dbg(dev, "analog switches supplied by booster\n");
502
503 return 0;
504 }
505 }
506
507 /* Fallback using vdda (default), nothing to do */
508 dev_dbg(dev, "analog switches supplied by vdda (%d uV)\n",
509 priv->vdda_uv);
510
511 return 0;
512 }
513
stm32_adc_core_switches_supply_dis(struct stm32_adc_priv * priv)514 static void stm32_adc_core_switches_supply_dis(struct stm32_adc_priv *priv)
515 {
516 if (priv->vdda_uv < 2700000) {
517 if (priv->syscfg && priv->vdd_uv > 2700000) {
518 regmap_write(priv->syscfg, STM32MP1_SYSCFG_PMCCLRR,
519 STM32MP1_SYSCFG_ANASWVDD_MASK);
520 regulator_disable(priv->vdd);
521 return;
522 }
523 if (priv->booster)
524 regulator_disable(priv->booster);
525 }
526 }
527
stm32_adc_core_hw_start(struct device * dev)528 static int stm32_adc_core_hw_start(struct device *dev)
529 {
530 struct stm32_adc_common *common = dev_get_drvdata(dev);
531 struct stm32_adc_priv *priv = to_stm32_adc_priv(common);
532 int ret;
533
534 ret = regulator_enable(priv->vdda);
535 if (ret < 0) {
536 dev_err(dev, "vdda enable failed %d\n", ret);
537 return ret;
538 }
539
540 ret = regulator_get_voltage(priv->vdda);
541 if (ret < 0) {
542 dev_err(dev, "vdda get voltage failed, %d\n", ret);
543 goto err_vdda_disable;
544 }
545 priv->vdda_uv = ret;
546
547 ret = stm32_adc_core_switches_supply_en(priv, dev);
548 if (ret < 0)
549 goto err_vdda_disable;
550
551 ret = regulator_enable(priv->vref);
552 if (ret < 0) {
553 dev_err(dev, "vref enable failed\n");
554 goto err_switches_dis;
555 }
556
557 ret = clk_prepare_enable(priv->bclk);
558 if (ret < 0) {
559 dev_err(dev, "bus clk enable failed\n");
560 goto err_regulator_disable;
561 }
562
563 ret = clk_prepare_enable(priv->aclk);
564 if (ret < 0) {
565 dev_err(dev, "adc clk enable failed\n");
566 goto err_bclk_disable;
567 }
568
569 writel_relaxed(priv->ccr_bak, priv->common.base + priv->cfg->regs->ccr);
570
571 return 0;
572
573 err_bclk_disable:
574 clk_disable_unprepare(priv->bclk);
575 err_regulator_disable:
576 regulator_disable(priv->vref);
577 err_switches_dis:
578 stm32_adc_core_switches_supply_dis(priv);
579 err_vdda_disable:
580 regulator_disable(priv->vdda);
581
582 return ret;
583 }
584
stm32_adc_core_hw_stop(struct device * dev)585 static void stm32_adc_core_hw_stop(struct device *dev)
586 {
587 struct stm32_adc_common *common = dev_get_drvdata(dev);
588 struct stm32_adc_priv *priv = to_stm32_adc_priv(common);
589
590 /* Backup CCR that may be lost (depends on power state to achieve) */
591 priv->ccr_bak = readl_relaxed(priv->common.base + priv->cfg->regs->ccr);
592 clk_disable_unprepare(priv->aclk);
593 clk_disable_unprepare(priv->bclk);
594 regulator_disable(priv->vref);
595 stm32_adc_core_switches_supply_dis(priv);
596 regulator_disable(priv->vdda);
597 }
598
stm32_adc_core_switches_probe(struct device * dev,struct stm32_adc_priv * priv)599 static int stm32_adc_core_switches_probe(struct device *dev,
600 struct stm32_adc_priv *priv)
601 {
602 struct device_node *np = dev->of_node;
603 int ret;
604
605 /* Analog switches supply can be controlled by syscfg (optional) */
606 priv->syscfg = syscon_regmap_lookup_by_phandle(np, "st,syscfg");
607 if (IS_ERR(priv->syscfg)) {
608 ret = PTR_ERR(priv->syscfg);
609 if (ret != -ENODEV)
610 return dev_err_probe(dev, ret, "Can't probe syscfg\n");
611
612 priv->syscfg = NULL;
613 }
614
615 /* Booster can be used to supply analog switches (optional) */
616 if (priv->cfg->has_syscfg & HAS_VBOOSTER &&
617 of_property_read_bool(np, "booster-supply")) {
618 priv->booster = devm_regulator_get_optional(dev, "booster");
619 if (IS_ERR(priv->booster)) {
620 ret = PTR_ERR(priv->booster);
621 if (ret != -ENODEV)
622 return dev_err_probe(dev, ret, "can't get booster\n");
623
624 priv->booster = NULL;
625 }
626 }
627
628 /* Vdd can be used to supply analog switches (optional) */
629 if (priv->cfg->has_syscfg & HAS_ANASWVDD &&
630 of_property_read_bool(np, "vdd-supply")) {
631 priv->vdd = devm_regulator_get_optional(dev, "vdd");
632 if (IS_ERR(priv->vdd)) {
633 ret = PTR_ERR(priv->vdd);
634 if (ret != -ENODEV)
635 return dev_err_probe(dev, ret, "can't get vdd\n");
636
637 priv->vdd = NULL;
638 }
639 }
640
641 if (priv->vdd) {
642 ret = regulator_enable(priv->vdd);
643 if (ret < 0) {
644 dev_err(dev, "vdd enable failed %d\n", ret);
645 return ret;
646 }
647
648 ret = regulator_get_voltage(priv->vdd);
649 if (ret < 0) {
650 dev_err(dev, "vdd get voltage failed %d\n", ret);
651 regulator_disable(priv->vdd);
652 return ret;
653 }
654 priv->vdd_uv = ret;
655
656 regulator_disable(priv->vdd);
657 }
658
659 return 0;
660 }
661
stm32_adc_probe_identification(struct platform_device * pdev,struct stm32_adc_priv * priv)662 static int stm32_adc_probe_identification(struct platform_device *pdev,
663 struct stm32_adc_priv *priv)
664 {
665 struct device_node *np = pdev->dev.of_node;
666 struct device_node *child;
667 const char *compat;
668 int ret, count = 0;
669 u32 id, val;
670
671 if (!priv->cfg->ipid)
672 return 0;
673
674 id = FIELD_GET(STM32MP1_IPIDR_MASK,
675 readl_relaxed(priv->common.base + STM32MP1_ADC_IPDR));
676 if (id != priv->cfg->ipid) {
677 dev_err(&pdev->dev, "Unexpected IP version: 0x%x", id);
678 return -EINVAL;
679 }
680
681 for_each_child_of_node(np, child) {
682 ret = of_property_read_string(child, "compatible", &compat);
683 if (ret)
684 continue;
685 /* Count child nodes with stm32 adc compatible */
686 if (strstr(compat, "st,stm32") && strstr(compat, "adc"))
687 count++;
688 }
689
690 val = readl_relaxed(priv->common.base + STM32MP1_ADC_HWCFGR0);
691 priv->nb_adc_max = FIELD_GET(STM32MP1_ADCNUM_MASK, val);
692 if (count > priv->nb_adc_max) {
693 dev_err(&pdev->dev, "Unexpected child number: %d", count);
694 return -EINVAL;
695 }
696
697 val = readl_relaxed(priv->common.base + STM32MP1_ADC_VERR);
698 dev_dbg(&pdev->dev, "ADC version: %lu.%lu\n",
699 FIELD_GET(STM32MP1_MAJREV_MASK, val),
700 FIELD_GET(STM32MP1_MINREV_MASK, val));
701
702 return 0;
703 }
704
stm32_adc_probe(struct platform_device * pdev)705 static int stm32_adc_probe(struct platform_device *pdev)
706 {
707 struct stm32_adc_priv *priv;
708 struct device *dev = &pdev->dev;
709 struct device_node *np = pdev->dev.of_node;
710 const struct of_device_id *of_id;
711
712 struct resource *res;
713 u32 max_rate;
714 int ret;
715
716 if (!pdev->dev.of_node)
717 return -ENODEV;
718
719 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
720 if (!priv)
721 return -ENOMEM;
722 platform_set_drvdata(pdev, &priv->common);
723
724 of_id = of_match_device(dev->driver->of_match_table, dev);
725 if (!of_id)
726 return -ENODEV;
727
728 priv->cfg = (const struct stm32_adc_priv_cfg *)of_id->data;
729 priv->nb_adc_max = priv->cfg->num_adcs;
730 spin_lock_init(&priv->common.lock);
731
732 priv->common.base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
733 if (IS_ERR(priv->common.base))
734 return PTR_ERR(priv->common.base);
735 priv->common.phys_base = res->start;
736
737 priv->vdda = devm_regulator_get(&pdev->dev, "vdda");
738 if (IS_ERR(priv->vdda))
739 return dev_err_probe(&pdev->dev, PTR_ERR(priv->vdda),
740 "vdda get failed\n");
741
742 priv->vref = devm_regulator_get(&pdev->dev, "vref");
743 if (IS_ERR(priv->vref))
744 return dev_err_probe(&pdev->dev, PTR_ERR(priv->vref),
745 "vref get failed\n");
746
747 priv->aclk = devm_clk_get_optional(&pdev->dev, "adc");
748 if (IS_ERR(priv->aclk))
749 return dev_err_probe(&pdev->dev, PTR_ERR(priv->aclk),
750 "Can't get 'adc' clock\n");
751
752 priv->bclk = devm_clk_get_optional(&pdev->dev, "bus");
753 if (IS_ERR(priv->bclk))
754 return dev_err_probe(&pdev->dev, PTR_ERR(priv->bclk),
755 "Can't get 'bus' clock\n");
756
757 ret = stm32_adc_core_switches_probe(dev, priv);
758 if (ret)
759 return ret;
760
761 pm_runtime_get_noresume(dev);
762 pm_runtime_set_active(dev);
763 pm_runtime_set_autosuspend_delay(dev, STM32_ADC_CORE_SLEEP_DELAY_MS);
764 pm_runtime_use_autosuspend(dev);
765 pm_runtime_enable(dev);
766
767 ret = stm32_adc_core_hw_start(dev);
768 if (ret)
769 goto err_pm_stop;
770
771 ret = stm32_adc_probe_identification(pdev, priv);
772 if (ret < 0)
773 goto err_hw_stop;
774
775 ret = regulator_get_voltage(priv->vref);
776 if (ret < 0) {
777 dev_err(&pdev->dev, "vref get voltage failed, %d\n", ret);
778 goto err_hw_stop;
779 }
780 priv->common.vref_mv = ret / 1000;
781 dev_dbg(&pdev->dev, "vref+=%dmV\n", priv->common.vref_mv);
782
783 ret = of_property_read_u32(pdev->dev.of_node, "st,max-clk-rate-hz",
784 &max_rate);
785 if (!ret)
786 priv->max_clk_rate = min(max_rate, priv->cfg->max_clk_rate_hz);
787 else
788 priv->max_clk_rate = priv->cfg->max_clk_rate_hz;
789
790 ret = priv->cfg->clk_sel(pdev, priv);
791 if (ret < 0)
792 goto err_hw_stop;
793
794 ret = stm32_adc_irq_probe(pdev, priv);
795 if (ret < 0)
796 goto err_hw_stop;
797
798 ret = of_platform_populate(np, NULL, NULL, &pdev->dev);
799 if (ret < 0) {
800 dev_err(&pdev->dev, "failed to populate DT children\n");
801 goto err_irq_remove;
802 }
803
804 pm_runtime_mark_last_busy(dev);
805 pm_runtime_put_autosuspend(dev);
806
807 return 0;
808
809 err_irq_remove:
810 stm32_adc_irq_remove(pdev, priv);
811 err_hw_stop:
812 stm32_adc_core_hw_stop(dev);
813 err_pm_stop:
814 pm_runtime_disable(dev);
815 pm_runtime_set_suspended(dev);
816 pm_runtime_put_noidle(dev);
817
818 return ret;
819 }
820
stm32_adc_remove(struct platform_device * pdev)821 static int stm32_adc_remove(struct platform_device *pdev)
822 {
823 struct stm32_adc_common *common = platform_get_drvdata(pdev);
824 struct stm32_adc_priv *priv = to_stm32_adc_priv(common);
825
826 pm_runtime_get_sync(&pdev->dev);
827 of_platform_depopulate(&pdev->dev);
828 stm32_adc_irq_remove(pdev, priv);
829 stm32_adc_core_hw_stop(&pdev->dev);
830 pm_runtime_disable(&pdev->dev);
831 pm_runtime_set_suspended(&pdev->dev);
832 pm_runtime_put_noidle(&pdev->dev);
833
834 return 0;
835 }
836
stm32_adc_core_runtime_suspend(struct device * dev)837 static int stm32_adc_core_runtime_suspend(struct device *dev)
838 {
839 stm32_adc_core_hw_stop(dev);
840
841 return 0;
842 }
843
stm32_adc_core_runtime_resume(struct device * dev)844 static int stm32_adc_core_runtime_resume(struct device *dev)
845 {
846 return stm32_adc_core_hw_start(dev);
847 }
848
stm32_adc_core_runtime_idle(struct device * dev)849 static int stm32_adc_core_runtime_idle(struct device *dev)
850 {
851 pm_runtime_mark_last_busy(dev);
852
853 return 0;
854 }
855
856 static DEFINE_RUNTIME_DEV_PM_OPS(stm32_adc_core_pm_ops,
857 stm32_adc_core_runtime_suspend,
858 stm32_adc_core_runtime_resume,
859 stm32_adc_core_runtime_idle);
860
861 static const struct stm32_adc_priv_cfg stm32f4_adc_priv_cfg = {
862 .regs = &stm32f4_adc_common_regs,
863 .clk_sel = stm32f4_adc_clk_sel,
864 .max_clk_rate_hz = 36000000,
865 .num_irqs = 1,
866 .num_adcs = 3,
867 };
868
869 static const struct stm32_adc_priv_cfg stm32h7_adc_priv_cfg = {
870 .regs = &stm32h7_adc_common_regs,
871 .clk_sel = stm32h7_adc_clk_sel,
872 .max_clk_rate_hz = 36000000,
873 .has_syscfg = HAS_VBOOSTER,
874 .num_irqs = 1,
875 .num_adcs = 2,
876 };
877
878 static const struct stm32_adc_priv_cfg stm32mp1_adc_priv_cfg = {
879 .regs = &stm32h7_adc_common_regs,
880 .clk_sel = stm32h7_adc_clk_sel,
881 .max_clk_rate_hz = 36000000,
882 .has_syscfg = HAS_VBOOSTER | HAS_ANASWVDD,
883 .ipid = STM32MP15_IPIDR_NUMBER,
884 .num_irqs = 2,
885 };
886
887 static const struct stm32_adc_priv_cfg stm32mp13_adc_priv_cfg = {
888 .regs = &stm32mp13_adc_common_regs,
889 .clk_sel = stm32h7_adc_clk_sel,
890 .max_clk_rate_hz = 75 * HZ_PER_MHZ,
891 .ipid = STM32MP13_IPIDR_NUMBER,
892 .num_irqs = 1,
893 };
894
895 static const struct of_device_id stm32_adc_of_match[] = {
896 {
897 .compatible = "st,stm32f4-adc-core",
898 .data = (void *)&stm32f4_adc_priv_cfg
899 }, {
900 .compatible = "st,stm32h7-adc-core",
901 .data = (void *)&stm32h7_adc_priv_cfg
902 }, {
903 .compatible = "st,stm32mp1-adc-core",
904 .data = (void *)&stm32mp1_adc_priv_cfg
905 }, {
906 .compatible = "st,stm32mp13-adc-core",
907 .data = (void *)&stm32mp13_adc_priv_cfg
908 }, {
909 },
910 };
911 MODULE_DEVICE_TABLE(of, stm32_adc_of_match);
912
913 static struct platform_driver stm32_adc_driver = {
914 .probe = stm32_adc_probe,
915 .remove = stm32_adc_remove,
916 .driver = {
917 .name = "stm32-adc-core",
918 .of_match_table = stm32_adc_of_match,
919 .pm = pm_ptr(&stm32_adc_core_pm_ops),
920 },
921 };
922 module_platform_driver(stm32_adc_driver);
923
924 MODULE_AUTHOR("Fabrice Gasnier <fabrice.gasnier@st.com>");
925 MODULE_DESCRIPTION("STMicroelectronics STM32 ADC core driver");
926 MODULE_LICENSE("GPL v2");
927 MODULE_ALIAS("platform:stm32-adc-core");
928