1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2017 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
4 * Copyright (C) 2018 BayLibre, SAS
5 * Author: Neil Armstrong <narmstrong@baylibre.com>
7 * Amlogic Meson Successive Approximation Register (SAR) A/D Converter
17 #include <linux/bitops.h>
18 #include <linux/delay.h>
19 #include <linux/math64.h>
20 #include <linux/bitfield.h>
21 #include <power/regulator.h>
23 #define MESON_SAR_ADC_REG0 0x00
24 #define MESON_SAR_ADC_REG0_PANEL_DETECT BIT(31)
25 #define MESON_SAR_ADC_REG0_BUSY_MASK GENMASK(30, 28)
26 #define MESON_SAR_ADC_REG0_DELTA_BUSY BIT(30)
27 #define MESON_SAR_ADC_REG0_AVG_BUSY BIT(29)
28 #define MESON_SAR_ADC_REG0_SAMPLE_BUSY BIT(28)
29 #define MESON_SAR_ADC_REG0_FIFO_FULL BIT(27)
30 #define MESON_SAR_ADC_REG0_FIFO_EMPTY BIT(26)
31 #define MESON_SAR_ADC_REG0_FIFO_COUNT_MASK GENMASK(25, 21)
32 #define MESON_SAR_ADC_REG0_ADC_BIAS_CTRL_MASK GENMASK(20, 19)
33 #define MESON_SAR_ADC_REG0_CURR_CHAN_ID_MASK GENMASK(18, 16)
34 #define MESON_SAR_ADC_REG0_ADC_TEMP_SEN_SEL BIT(15)
35 #define MESON_SAR_ADC_REG0_SAMPLING_STOP BIT(14)
36 #define MESON_SAR_ADC_REG0_CHAN_DELTA_EN_MASK GENMASK(13, 12)
37 #define MESON_SAR_ADC_REG0_DETECT_IRQ_POL BIT(10)
38 #define MESON_SAR_ADC_REG0_DETECT_IRQ_EN BIT(9)
39 #define MESON_SAR_ADC_REG0_FIFO_CNT_IRQ_MASK GENMASK(8, 4)
40 #define MESON_SAR_ADC_REG0_FIFO_IRQ_EN BIT(3)
41 #define MESON_SAR_ADC_REG0_SAMPLING_START BIT(2)
42 #define MESON_SAR_ADC_REG0_CONTINUOUS_EN BIT(1)
43 #define MESON_SAR_ADC_REG0_SAMPLE_ENGINE_ENABLE BIT(0)
45 #define MESON_SAR_ADC_CHAN_LIST 0x04
46 #define MESON_SAR_ADC_CHAN_LIST_MAX_INDEX_MASK GENMASK(26, 24)
47 #define MESON_SAR_ADC_CHAN_LIST_ENTRY_MASK(_chan) \
48 (GENMASK(2, 0) << ((_chan) * 3))
50 #define MESON_SAR_ADC_AVG_CNTL 0x08
51 #define MESON_SAR_ADC_AVG_CNTL_AVG_MODE_SHIFT(_chan) \
53 #define MESON_SAR_ADC_AVG_CNTL_AVG_MODE_MASK(_chan) \
54 (GENMASK(17, 16) << ((_chan) * 2))
55 #define MESON_SAR_ADC_AVG_CNTL_NUM_SAMPLES_SHIFT(_chan) \
57 #define MESON_SAR_ADC_AVG_CNTL_NUM_SAMPLES_MASK(_chan) \
58 (GENMASK(1, 0) << ((_chan) * 2))
60 #define MESON_SAR_ADC_REG3 0x0c
61 #define MESON_SAR_ADC_REG3_CNTL_USE_SC_DLY BIT(31)
62 #define MESON_SAR_ADC_REG3_CLK_EN BIT(30)
63 #define MESON_SAR_ADC_REG3_BL30_INITIALIZED BIT(28)
64 #define MESON_SAR_ADC_REG3_CTRL_CONT_RING_COUNTER_EN BIT(27)
65 #define MESON_SAR_ADC_REG3_CTRL_SAMPLING_CLOCK_PHASE BIT(26)
66 #define MESON_SAR_ADC_REG3_CTRL_CHAN7_MUX_SEL_MASK GENMASK(25, 23)
67 #define MESON_SAR_ADC_REG3_DETECT_EN BIT(22)
68 #define MESON_SAR_ADC_REG3_ADC_EN BIT(21)
69 #define MESON_SAR_ADC_REG3_PANEL_DETECT_COUNT_MASK GENMASK(20, 18)
70 #define MESON_SAR_ADC_REG3_PANEL_DETECT_FILTER_TB_MASK GENMASK(17, 16)
71 #define MESON_SAR_ADC_REG3_ADC_CLK_DIV_SHIFT 10
72 #define MESON_SAR_ADC_REG3_ADC_CLK_DIV_WIDTH 5
73 #define MESON_SAR_ADC_REG3_BLOCK_DLY_SEL_MASK GENMASK(9, 8)
74 #define MESON_SAR_ADC_REG3_BLOCK_DLY_MASK GENMASK(7, 0)
76 #define MESON_SAR_ADC_DELAY 0x10
77 #define MESON_SAR_ADC_DELAY_INPUT_DLY_SEL_MASK GENMASK(25, 24)
78 #define MESON_SAR_ADC_DELAY_BL30_BUSY BIT(15)
79 #define MESON_SAR_ADC_DELAY_KERNEL_BUSY BIT(14)
80 #define MESON_SAR_ADC_DELAY_INPUT_DLY_CNT_MASK GENMASK(23, 16)
81 #define MESON_SAR_ADC_DELAY_SAMPLE_DLY_SEL_MASK GENMASK(9, 8)
82 #define MESON_SAR_ADC_DELAY_SAMPLE_DLY_CNT_MASK GENMASK(7, 0)
84 #define MESON_SAR_ADC_LAST_RD 0x14
85 #define MESON_SAR_ADC_LAST_RD_LAST_CHANNEL1_MASK GENMASK(23, 16)
86 #define MESON_SAR_ADC_LAST_RD_LAST_CHANNEL0_MASK GENMASK(9, 0)
88 #define MESON_SAR_ADC_FIFO_RD 0x18
89 #define MESON_SAR_ADC_FIFO_RD_CHAN_ID_MASK GENMASK(14, 12)
90 #define MESON_SAR_ADC_FIFO_RD_SAMPLE_VALUE_MASK GENMASK(11, 0)
92 #define MESON_SAR_ADC_AUX_SW 0x1c
93 #define MESON_SAR_ADC_AUX_SW_MUX_SEL_CHAN_SHIFT(_chan) \
94 (8 + (((_chan) - 2) * 3))
95 #define MESON_SAR_ADC_AUX_SW_VREF_P_MUX BIT(6)
96 #define MESON_SAR_ADC_AUX_SW_VREF_N_MUX BIT(5)
97 #define MESON_SAR_ADC_AUX_SW_MODE_SEL BIT(4)
98 #define MESON_SAR_ADC_AUX_SW_YP_DRIVE_SW BIT(3)
99 #define MESON_SAR_ADC_AUX_SW_XP_DRIVE_SW BIT(2)
100 #define MESON_SAR_ADC_AUX_SW_YM_DRIVE_SW BIT(1)
101 #define MESON_SAR_ADC_AUX_SW_XM_DRIVE_SW BIT(0)
103 #define MESON_SAR_ADC_CHAN_10_SW 0x20
104 #define MESON_SAR_ADC_CHAN_10_SW_CHAN1_MUX_SEL_MASK GENMASK(25, 23)
105 #define MESON_SAR_ADC_CHAN_10_SW_CHAN1_VREF_P_MUX BIT(22)
106 #define MESON_SAR_ADC_CHAN_10_SW_CHAN1_VREF_N_MUX BIT(21)
107 #define MESON_SAR_ADC_CHAN_10_SW_CHAN1_MODE_SEL BIT(20)
108 #define MESON_SAR_ADC_CHAN_10_SW_CHAN1_YP_DRIVE_SW BIT(19)
109 #define MESON_SAR_ADC_CHAN_10_SW_CHAN1_XP_DRIVE_SW BIT(18)
110 #define MESON_SAR_ADC_CHAN_10_SW_CHAN1_YM_DRIVE_SW BIT(17)
111 #define MESON_SAR_ADC_CHAN_10_SW_CHAN1_XM_DRIVE_SW BIT(16)
112 #define MESON_SAR_ADC_CHAN_10_SW_CHAN0_MUX_SEL_MASK GENMASK(9, 7)
113 #define MESON_SAR_ADC_CHAN_10_SW_CHAN0_VREF_P_MUX BIT(6)
114 #define MESON_SAR_ADC_CHAN_10_SW_CHAN0_VREF_N_MUX BIT(5)
115 #define MESON_SAR_ADC_CHAN_10_SW_CHAN0_MODE_SEL BIT(4)
116 #define MESON_SAR_ADC_CHAN_10_SW_CHAN0_YP_DRIVE_SW BIT(3)
117 #define MESON_SAR_ADC_CHAN_10_SW_CHAN0_XP_DRIVE_SW BIT(2)
118 #define MESON_SAR_ADC_CHAN_10_SW_CHAN0_YM_DRIVE_SW BIT(1)
119 #define MESON_SAR_ADC_CHAN_10_SW_CHAN0_XM_DRIVE_SW BIT(0)
121 #define MESON_SAR_ADC_DETECT_IDLE_SW 0x24
122 #define MESON_SAR_ADC_DETECT_IDLE_SW_DETECT_SW_EN BIT(26)
123 #define MESON_SAR_ADC_DETECT_IDLE_SW_DETECT_MUX_MASK GENMASK(25, 23)
124 #define MESON_SAR_ADC_DETECT_IDLE_SW_DETECT_VREF_P_MUX BIT(22)
125 #define MESON_SAR_ADC_DETECT_IDLE_SW_DETECT_VREF_N_MUX BIT(21)
126 #define MESON_SAR_ADC_DETECT_IDLE_SW_DETECT_MODE_SEL BIT(20)
127 #define MESON_SAR_ADC_DETECT_IDLE_SW_DETECT_YP_DRIVE_SW BIT(19)
128 #define MESON_SAR_ADC_DETECT_IDLE_SW_DETECT_XP_DRIVE_SW BIT(18)
129 #define MESON_SAR_ADC_DETECT_IDLE_SW_DETECT_YM_DRIVE_SW BIT(17)
130 #define MESON_SAR_ADC_DETECT_IDLE_SW_DETECT_XM_DRIVE_SW BIT(16)
131 #define MESON_SAR_ADC_DETECT_IDLE_SW_IDLE_MUX_SEL_MASK GENMASK(9, 7)
132 #define MESON_SAR_ADC_DETECT_IDLE_SW_IDLE_VREF_P_MUX BIT(6)
133 #define MESON_SAR_ADC_DETECT_IDLE_SW_IDLE_VREF_N_MUX BIT(5)
134 #define MESON_SAR_ADC_DETECT_IDLE_SW_IDLE_MODE_SEL BIT(4)
135 #define MESON_SAR_ADC_DETECT_IDLE_SW_IDLE_YP_DRIVE_SW BIT(3)
136 #define MESON_SAR_ADC_DETECT_IDLE_SW_IDLE_XP_DRIVE_SW BIT(2)
137 #define MESON_SAR_ADC_DETECT_IDLE_SW_IDLE_YM_DRIVE_SW BIT(1)
138 #define MESON_SAR_ADC_DETECT_IDLE_SW_IDLE_XM_DRIVE_SW BIT(0)
140 #define MESON_SAR_ADC_DELTA_10 0x28
141 #define MESON_SAR_ADC_DELTA_10_TEMP_SEL BIT(27)
142 #define MESON_SAR_ADC_DELTA_10_TS_REVE1 BIT(26)
143 #define MESON_SAR_ADC_DELTA_10_CHAN1_DELTA_VALUE_MASK GENMASK(25, 16)
144 #define MESON_SAR_ADC_DELTA_10_TS_REVE0 BIT(15)
145 #define MESON_SAR_ADC_DELTA_10_TS_C_SHIFT 11
146 #define MESON_SAR_ADC_DELTA_10_TS_C_MASK GENMASK(14, 11)
147 #define MESON_SAR_ADC_DELTA_10_TS_VBG_EN BIT(10)
148 #define MESON_SAR_ADC_DELTA_10_CHAN0_DELTA_VALUE_MASK GENMASK(9, 0)
151 * NOTE: registers from here are undocumented (the vendor Linux kernel driver
152 * and u-boot source served as reference). These only seem to be relevant on
155 #define MESON_SAR_ADC_REG11 0x2c
156 #define MESON_SAR_ADC_REG11_BANDGAP_EN BIT(13)
158 #define MESON_SAR_ADC_REG13 0x34
159 #define MESON_SAR_ADC_REG13_12BIT_CALIBRATION_MASK GENMASK(13, 8)
161 #define MESON_SAR_ADC_MAX_FIFO_SIZE 32
162 #define MESON_SAR_ADC_TIMEOUT 100 /* ms */
164 #define NUM_CHANNELS 8
166 #define MILLION 1000000
168 struct meson_saradc_data {
172 struct meson_saradc_priv {
173 const struct meson_saradc_data *data;
174 struct regmap *regmap;
184 meson_saradc_get_fifo_count(struct meson_saradc_priv *priv)
188 regmap_read(priv->regmap, MESON_SAR_ADC_REG0, ®val);
190 return FIELD_GET(MESON_SAR_ADC_REG0_FIFO_COUNT_MASK, regval);
193 static int meson_saradc_lock(struct meson_saradc_priv *priv)
195 uint val, timeout = 10000;
197 /* prevent BL30 from using the SAR ADC while we are using it */
198 regmap_update_bits(priv->regmap, MESON_SAR_ADC_DELAY,
199 MESON_SAR_ADC_DELAY_KERNEL_BUSY,
200 MESON_SAR_ADC_DELAY_KERNEL_BUSY);
203 * wait until BL30 releases it's lock (so we can use the SAR ADC)
207 regmap_read(priv->regmap, MESON_SAR_ADC_DELAY, &val);
208 } while (val & MESON_SAR_ADC_DELAY_BL30_BUSY && timeout--);
211 printf("Timeout while waiting for BL30 unlock\n");
218 static void meson_saradc_unlock(struct meson_saradc_priv *priv)
220 /* allow BL30 to use the SAR ADC again */
221 regmap_update_bits(priv->regmap, MESON_SAR_ADC_DELAY,
222 MESON_SAR_ADC_DELAY_KERNEL_BUSY, 0);
225 static void meson_saradc_clear_fifo(struct meson_saradc_priv *priv)
227 unsigned int count, tmp;
229 for (count = 0; count < MESON_SAR_ADC_MAX_FIFO_SIZE; count++) {
230 if (!meson_saradc_get_fifo_count(priv))
233 regmap_read(priv->regmap, MESON_SAR_ADC_FIFO_RD, &tmp);
237 static int meson_saradc_calib_val(struct meson_saradc_priv *priv, int val)
241 /* use val_calib = scale * val_raw + offset calibration function */
242 tmp = div_s64((s64)val * priv->calibscale, MILLION) + priv->calibbias;
244 return clamp(tmp, 0, (1 << priv->data->num_bits) - 1);
247 static int meson_saradc_wait_busy_clear(struct meson_saradc_priv *priv)
249 uint regval, timeout = 10000;
252 * NOTE: we need a small delay before reading the status, otherwise
253 * the sample engine may not have started internally (which would
254 * seem to us that sampling is already finished).
258 regmap_read(priv->regmap, MESON_SAR_ADC_REG0, ®val);
259 } while (FIELD_GET(MESON_SAR_ADC_REG0_BUSY_MASK, regval) && timeout--);
267 static int meson_saradc_read_raw_sample(struct meson_saradc_priv *priv,
268 unsigned int channel, uint *val)
270 uint regval, fifo_chan, fifo_val, count;
273 ret = meson_saradc_wait_busy_clear(priv);
277 count = meson_saradc_get_fifo_count(priv);
279 printf("ADC FIFO has %d element(s) instead of one\n", count);
283 regmap_read(priv->regmap, MESON_SAR_ADC_FIFO_RD, ®val);
284 fifo_chan = FIELD_GET(MESON_SAR_ADC_FIFO_RD_CHAN_ID_MASK, regval);
285 if (fifo_chan != channel) {
286 printf("ADC FIFO entry belongs to channel %u instead of %u\n",
291 fifo_val = FIELD_GET(MESON_SAR_ADC_FIFO_RD_SAMPLE_VALUE_MASK, regval);
292 fifo_val &= GENMASK(priv->data->num_bits - 1, 0);
293 *val = meson_saradc_calib_val(priv, fifo_val);
298 static void meson_saradc_start_sample_engine(struct meson_saradc_priv *priv)
300 regmap_update_bits(priv->regmap, MESON_SAR_ADC_REG0,
301 MESON_SAR_ADC_REG0_FIFO_IRQ_EN,
302 MESON_SAR_ADC_REG0_FIFO_IRQ_EN);
304 regmap_update_bits(priv->regmap, MESON_SAR_ADC_REG0,
305 MESON_SAR_ADC_REG0_SAMPLE_ENGINE_ENABLE,
306 MESON_SAR_ADC_REG0_SAMPLE_ENGINE_ENABLE);
308 regmap_update_bits(priv->regmap, MESON_SAR_ADC_REG0,
309 MESON_SAR_ADC_REG0_SAMPLING_START,
310 MESON_SAR_ADC_REG0_SAMPLING_START);
313 static void meson_saradc_stop_sample_engine(struct meson_saradc_priv *priv)
315 regmap_update_bits(priv->regmap, MESON_SAR_ADC_REG0,
316 MESON_SAR_ADC_REG0_FIFO_IRQ_EN, 0);
318 regmap_update_bits(priv->regmap, MESON_SAR_ADC_REG0,
319 MESON_SAR_ADC_REG0_SAMPLING_STOP,
320 MESON_SAR_ADC_REG0_SAMPLING_STOP);
322 /* wait until all modules are stopped */
323 meson_saradc_wait_busy_clear(priv);
325 regmap_update_bits(priv->regmap, MESON_SAR_ADC_REG0,
326 MESON_SAR_ADC_REG0_SAMPLE_ENGINE_ENABLE, 0);
329 enum meson_saradc_avg_mode {
331 MEAN_AVERAGING = 0x1,
332 MEDIAN_AVERAGING = 0x2,
335 enum meson_saradc_num_samples {
342 static void meson_saradc_set_averaging(struct meson_saradc_priv *priv,
343 unsigned int channel,
344 enum meson_saradc_avg_mode mode,
345 enum meson_saradc_num_samples samples)
349 val = samples << MESON_SAR_ADC_AVG_CNTL_NUM_SAMPLES_SHIFT(channel);
350 regmap_update_bits(priv->regmap, MESON_SAR_ADC_AVG_CNTL,
351 MESON_SAR_ADC_AVG_CNTL_NUM_SAMPLES_MASK(channel),
354 val = mode << MESON_SAR_ADC_AVG_CNTL_AVG_MODE_SHIFT(channel);
355 regmap_update_bits(priv->regmap, MESON_SAR_ADC_AVG_CNTL,
356 MESON_SAR_ADC_AVG_CNTL_AVG_MODE_MASK(channel), val);
359 static void meson_saradc_enable_channel(struct meson_saradc_priv *priv,
360 unsigned int channel)
365 * the SAR ADC engine allows sampling multiple channels at the same
366 * time. to keep it simple we're only working with one *internal*
367 * channel, which starts counting at index 0 (which means: count = 1).
369 regval = FIELD_PREP(MESON_SAR_ADC_CHAN_LIST_MAX_INDEX_MASK, 0);
370 regmap_update_bits(priv->regmap, MESON_SAR_ADC_CHAN_LIST,
371 MESON_SAR_ADC_CHAN_LIST_MAX_INDEX_MASK, regval);
373 /* map channel index 0 to the channel which we want to read */
374 regval = FIELD_PREP(MESON_SAR_ADC_CHAN_LIST_ENTRY_MASK(0), channel);
375 regmap_update_bits(priv->regmap, MESON_SAR_ADC_CHAN_LIST,
376 MESON_SAR_ADC_CHAN_LIST_ENTRY_MASK(0), regval);
378 regval = FIELD_PREP(MESON_SAR_ADC_DETECT_IDLE_SW_DETECT_MUX_MASK,
380 regmap_update_bits(priv->regmap, MESON_SAR_ADC_DETECT_IDLE_SW,
381 MESON_SAR_ADC_DETECT_IDLE_SW_DETECT_MUX_MASK,
384 regval = FIELD_PREP(MESON_SAR_ADC_DETECT_IDLE_SW_IDLE_MUX_SEL_MASK,
386 regmap_update_bits(priv->regmap, MESON_SAR_ADC_DETECT_IDLE_SW,
387 MESON_SAR_ADC_DETECT_IDLE_SW_IDLE_MUX_SEL_MASK,
391 regmap_update_bits(priv->regmap, MESON_SAR_ADC_DELTA_10,
392 MESON_SAR_ADC_DELTA_10_TEMP_SEL, 0);
395 static int meson_saradc_get_sample(struct meson_saradc_priv *priv,
400 ret = meson_saradc_lock(priv);
404 /* clear the FIFO to make sure we're not reading old values */
405 meson_saradc_clear_fifo(priv);
407 meson_saradc_set_averaging(priv, chan, MEAN_AVERAGING, EIGHT_SAMPLES);
409 meson_saradc_enable_channel(priv, chan);
411 meson_saradc_start_sample_engine(priv);
412 ret = meson_saradc_read_raw_sample(priv, chan, val);
413 meson_saradc_stop_sample_engine(priv);
415 meson_saradc_unlock(priv);
418 printf("failed to read sample for channel %d: %d\n",
426 static int meson_saradc_channel_data(struct udevice *dev, int channel,
429 struct meson_saradc_priv *priv = dev_get_priv(dev);
431 if (channel != priv->active_channel) {
432 pr_err("Requested channel is not active!");
436 return meson_saradc_get_sample(priv, channel, data);
439 enum meson_saradc_chan7_mux_sel {
441 CHAN7_MUX_VDD_DIV4 = 0x1,
442 CHAN7_MUX_VDD_DIV2 = 0x2,
443 CHAN7_MUX_VDD_MUL3_DIV4 = 0x3,
445 CHAN7_MUX_CH7_INPUT = 0x7,
448 static void meson_saradc_set_chan7_mux(struct meson_saradc_priv *priv,
449 enum meson_saradc_chan7_mux_sel sel)
453 regval = FIELD_PREP(MESON_SAR_ADC_REG3_CTRL_CHAN7_MUX_SEL_MASK, sel);
454 regmap_update_bits(priv->regmap, MESON_SAR_ADC_REG3,
455 MESON_SAR_ADC_REG3_CTRL_CHAN7_MUX_SEL_MASK, regval);
460 static int meson_saradc_calib(struct meson_saradc_priv *priv)
462 uint nominal0, nominal1, value0, value1;
465 /* use points 25% and 75% for calibration */
466 nominal0 = (1 << priv->data->num_bits) / 4;
467 nominal1 = (1 << priv->data->num_bits) * 3 / 4;
469 meson_saradc_set_chan7_mux(priv, CHAN7_MUX_VDD_DIV4);
471 ret = meson_saradc_get_sample(priv, 7, &value0);
475 meson_saradc_set_chan7_mux(priv, CHAN7_MUX_VDD_MUL3_DIV4);
477 ret = meson_saradc_get_sample(priv, 7, &value1);
481 if (value1 <= value0) {
486 priv->calibscale = div_s64((nominal1 - nominal0) * (s64)MILLION,
488 priv->calibbias = nominal0 - div_s64((s64)value0 * priv->calibscale,
492 meson_saradc_set_chan7_mux(priv, CHAN7_MUX_CH7_INPUT);
497 static int meson_saradc_init(struct meson_saradc_priv *priv)
502 priv->calibscale = MILLION;
505 * make sure we start at CH7 input since the other muxes are only used
506 * for internal calibration.
508 meson_saradc_set_chan7_mux(priv, CHAN7_MUX_CH7_INPUT);
511 * leave sampling delay and the input clocks as configured by
512 * BL30 to make sure BL30 gets the values it expects when
513 * reading the temperature sensor.
515 regmap_read(priv->regmap, MESON_SAR_ADC_REG3, ®val);
516 if (regval & MESON_SAR_ADC_REG3_BL30_INITIALIZED) {
517 regmap_read(priv->regmap, MESON_SAR_ADC_REG3, ®val);
518 if (regval & MESON_SAR_ADC_REG3_ADC_EN)
522 meson_saradc_stop_sample_engine(priv);
524 /* update the channel 6 MUX to select the temperature sensor */
525 regmap_update_bits(priv->regmap, MESON_SAR_ADC_REG0,
526 MESON_SAR_ADC_REG0_ADC_TEMP_SEN_SEL,
527 MESON_SAR_ADC_REG0_ADC_TEMP_SEN_SEL);
529 /* disable all channels by default */
530 regmap_write(priv->regmap, MESON_SAR_ADC_CHAN_LIST, 0x0);
532 regmap_update_bits(priv->regmap, MESON_SAR_ADC_REG3,
533 MESON_SAR_ADC_REG3_CTRL_SAMPLING_CLOCK_PHASE, 0);
534 regmap_update_bits(priv->regmap, MESON_SAR_ADC_REG3,
535 MESON_SAR_ADC_REG3_CNTL_USE_SC_DLY,
536 MESON_SAR_ADC_REG3_CNTL_USE_SC_DLY);
538 /* delay between two samples = (10+1) * 1uS */
539 regmap_update_bits(priv->regmap, MESON_SAR_ADC_DELAY,
540 MESON_SAR_ADC_DELAY_INPUT_DLY_CNT_MASK,
541 FIELD_PREP(MESON_SAR_ADC_DELAY_SAMPLE_DLY_CNT_MASK,
543 regmap_update_bits(priv->regmap, MESON_SAR_ADC_DELAY,
544 MESON_SAR_ADC_DELAY_SAMPLE_DLY_SEL_MASK,
545 FIELD_PREP(MESON_SAR_ADC_DELAY_SAMPLE_DLY_SEL_MASK,
548 /* delay between two samples = (10+1) * 1uS */
549 regmap_update_bits(priv->regmap, MESON_SAR_ADC_DELAY,
550 MESON_SAR_ADC_DELAY_INPUT_DLY_CNT_MASK,
551 FIELD_PREP(MESON_SAR_ADC_DELAY_INPUT_DLY_CNT_MASK,
553 regmap_update_bits(priv->regmap, MESON_SAR_ADC_DELAY,
554 MESON_SAR_ADC_DELAY_INPUT_DLY_SEL_MASK,
555 FIELD_PREP(MESON_SAR_ADC_DELAY_INPUT_DLY_SEL_MASK,
559 * set up the input channel muxes in MESON_SAR_ADC_CHAN_10_SW
560 * (0 = SAR_ADC_CH0, 1 = SAR_ADC_CH1)
562 regval = FIELD_PREP(MESON_SAR_ADC_CHAN_10_SW_CHAN0_MUX_SEL_MASK, 0);
563 regmap_update_bits(priv->regmap, MESON_SAR_ADC_CHAN_10_SW,
564 MESON_SAR_ADC_CHAN_10_SW_CHAN0_MUX_SEL_MASK,
566 regval = FIELD_PREP(MESON_SAR_ADC_CHAN_10_SW_CHAN1_MUX_SEL_MASK, 1);
567 regmap_update_bits(priv->regmap, MESON_SAR_ADC_CHAN_10_SW,
568 MESON_SAR_ADC_CHAN_10_SW_CHAN1_MUX_SEL_MASK,
572 * set up the input channel muxes in MESON_SAR_ADC_AUX_SW
573 * (2 = SAR_ADC_CH2, 3 = SAR_ADC_CH3, ...) and enable
574 * MESON_SAR_ADC_AUX_SW_YP_DRIVE_SW and
575 * MESON_SAR_ADC_AUX_SW_XP_DRIVE_SW like the vendor driver.
578 for (i = 2; i <= 7; i++)
579 regval |= i << MESON_SAR_ADC_AUX_SW_MUX_SEL_CHAN_SHIFT(i);
580 regval |= MESON_SAR_ADC_AUX_SW_YP_DRIVE_SW;
581 regval |= MESON_SAR_ADC_AUX_SW_XP_DRIVE_SW;
582 regmap_write(priv->regmap, MESON_SAR_ADC_AUX_SW, regval);
584 ret = meson_saradc_lock(priv);
588 #if CONFIG_IS_ENABLED(CLK)
589 ret = clk_enable(&priv->core_clk);
594 regval = FIELD_PREP(MESON_SAR_ADC_REG0_FIFO_CNT_IRQ_MASK, 1);
595 regmap_update_bits(priv->regmap, MESON_SAR_ADC_REG0,
596 MESON_SAR_ADC_REG0_FIFO_CNT_IRQ_MASK, regval);
598 regmap_update_bits(priv->regmap, MESON_SAR_ADC_REG11,
599 MESON_SAR_ADC_REG11_BANDGAP_EN,
600 MESON_SAR_ADC_REG11_BANDGAP_EN);
602 regmap_update_bits(priv->regmap, MESON_SAR_ADC_REG3,
603 MESON_SAR_ADC_REG3_ADC_EN,
604 MESON_SAR_ADC_REG3_ADC_EN);
608 #if CONFIG_IS_ENABLED(CLK)
609 ret = clk_enable(&priv->adc_clk);
614 meson_saradc_unlock(priv);
616 ret = meson_saradc_calib(priv);
618 printf("calibration failed\n");
625 static int meson_saradc_start_channel(struct udevice *dev, int channel)
627 struct meson_saradc_priv *priv = dev_get_priv(dev);
629 if (channel < 0 || channel >= NUM_CHANNELS) {
630 printf("Requested channel is invalid!");
634 if (!priv->initialized) {
637 ret = meson_saradc_init(priv);
641 priv->initialized = true;
644 priv->active_channel = channel;
649 static int meson_saradc_stop(struct udevice *dev)
651 struct meson_saradc_priv *priv = dev_get_priv(dev);
653 priv->active_channel = -1;
658 static int meson_saradc_probe(struct udevice *dev)
660 struct adc_uclass_plat *uc_pdata = dev_get_uclass_plat(dev);
661 struct meson_saradc_priv *priv = dev_get_priv(dev);
662 struct udevice *vref;
666 ret = regmap_init_mem(dev_ofnode(dev), &priv->regmap);
670 #if CONFIG_IS_ENABLED(CLK)
671 ret = clk_get_by_name(dev, "core", &priv->core_clk);
675 ret = clk_get_by_name(dev, "adc_clk", &priv->adc_clk);
680 priv->active_channel = -1;
682 ret = device_get_supply_regulator(dev, "vref-supply", &vref);
684 printf("can't get vref-supply: %d\n", ret);
688 vref_uv = regulator_get_value(vref);
690 printf("can't get vref-supply value: %d\n", vref_uv);
694 /* VDD supplied by common vref pin */
695 uc_pdata->vdd_supply = vref;
696 uc_pdata->vdd_microvolts = vref_uv;
697 uc_pdata->vss_microvolts = 0;
702 int meson_saradc_of_to_plat(struct udevice *dev)
704 struct adc_uclass_plat *uc_pdata = dev_get_uclass_plat(dev);
705 struct meson_saradc_priv *priv = dev_get_priv(dev);
707 priv->data = (struct meson_saradc_data *)dev_get_driver_data(dev);
709 uc_pdata->data_mask = GENMASK(priv->data->num_bits - 1, 0);
710 uc_pdata->data_format = ADC_DATA_FORMAT_BIN;
711 uc_pdata->data_timeout_us = MESON_SAR_ADC_TIMEOUT * 1000;
712 uc_pdata->channel_mask = GENMASK(NUM_CHANNELS - 1, 0);
717 static const struct adc_ops meson_saradc_ops = {
718 .start_channel = meson_saradc_start_channel,
719 .channel_data = meson_saradc_channel_data,
720 .stop = meson_saradc_stop,
723 static const struct meson_saradc_data gxbb_saradc_data = {
727 static const struct meson_saradc_data gxl_saradc_data = {
731 static const struct udevice_id meson_saradc_ids[] = {
732 { .compatible = "amlogic,meson-gxbb-saradc",
733 .data = (ulong)&gxbb_saradc_data },
734 { .compatible = "amlogic,meson-gxl-saradc",
735 .data = (ulong)&gxl_saradc_data },
736 { .compatible = "amlogic,meson-gxm-saradc",
737 .data = (ulong)&gxl_saradc_data },
738 { .compatible = "amlogic,meson-g12a-saradc",
739 .data = (ulong)&gxl_saradc_data },
743 U_BOOT_DRIVER(meson_saradc) = {
744 .name = "meson_saradc",
746 .of_match = meson_saradc_ids,
747 .ops = &meson_saradc_ops,
748 .probe = meson_saradc_probe,
749 .of_to_plat = meson_saradc_of_to_plat,
750 .priv_auto = sizeof(struct meson_saradc_priv),