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>
22 #define MESON_SAR_ADC_REG0 0x00
23 #define MESON_SAR_ADC_REG0_PANEL_DETECT BIT(31)
24 #define MESON_SAR_ADC_REG0_BUSY_MASK GENMASK(30, 28)
25 #define MESON_SAR_ADC_REG0_DELTA_BUSY BIT(30)
26 #define MESON_SAR_ADC_REG0_AVG_BUSY BIT(29)
27 #define MESON_SAR_ADC_REG0_SAMPLE_BUSY BIT(28)
28 #define MESON_SAR_ADC_REG0_FIFO_FULL BIT(27)
29 #define MESON_SAR_ADC_REG0_FIFO_EMPTY BIT(26)
30 #define MESON_SAR_ADC_REG0_FIFO_COUNT_MASK GENMASK(25, 21)
31 #define MESON_SAR_ADC_REG0_ADC_BIAS_CTRL_MASK GENMASK(20, 19)
32 #define MESON_SAR_ADC_REG0_CURR_CHAN_ID_MASK GENMASK(18, 16)
33 #define MESON_SAR_ADC_REG0_ADC_TEMP_SEN_SEL BIT(15)
34 #define MESON_SAR_ADC_REG0_SAMPLING_STOP BIT(14)
35 #define MESON_SAR_ADC_REG0_CHAN_DELTA_EN_MASK GENMASK(13, 12)
36 #define MESON_SAR_ADC_REG0_DETECT_IRQ_POL BIT(10)
37 #define MESON_SAR_ADC_REG0_DETECT_IRQ_EN BIT(9)
38 #define MESON_SAR_ADC_REG0_FIFO_CNT_IRQ_MASK GENMASK(8, 4)
39 #define MESON_SAR_ADC_REG0_FIFO_IRQ_EN BIT(3)
40 #define MESON_SAR_ADC_REG0_SAMPLING_START BIT(2)
41 #define MESON_SAR_ADC_REG0_CONTINUOUS_EN BIT(1)
42 #define MESON_SAR_ADC_REG0_SAMPLE_ENGINE_ENABLE BIT(0)
44 #define MESON_SAR_ADC_CHAN_LIST 0x04
45 #define MESON_SAR_ADC_CHAN_LIST_MAX_INDEX_MASK GENMASK(26, 24)
46 #define MESON_SAR_ADC_CHAN_LIST_ENTRY_MASK(_chan) \
47 (GENMASK(2, 0) << ((_chan) * 3))
49 #define MESON_SAR_ADC_AVG_CNTL 0x08
50 #define MESON_SAR_ADC_AVG_CNTL_AVG_MODE_SHIFT(_chan) \
52 #define MESON_SAR_ADC_AVG_CNTL_AVG_MODE_MASK(_chan) \
53 (GENMASK(17, 16) << ((_chan) * 2))
54 #define MESON_SAR_ADC_AVG_CNTL_NUM_SAMPLES_SHIFT(_chan) \
56 #define MESON_SAR_ADC_AVG_CNTL_NUM_SAMPLES_MASK(_chan) \
57 (GENMASK(1, 0) << ((_chan) * 2))
59 #define MESON_SAR_ADC_REG3 0x0c
60 #define MESON_SAR_ADC_REG3_CNTL_USE_SC_DLY BIT(31)
61 #define MESON_SAR_ADC_REG3_CLK_EN BIT(30)
62 #define MESON_SAR_ADC_REG3_BL30_INITIALIZED BIT(28)
63 #define MESON_SAR_ADC_REG3_CTRL_CONT_RING_COUNTER_EN BIT(27)
64 #define MESON_SAR_ADC_REG3_CTRL_SAMPLING_CLOCK_PHASE BIT(26)
65 #define MESON_SAR_ADC_REG3_CTRL_CHAN7_MUX_SEL_MASK GENMASK(25, 23)
66 #define MESON_SAR_ADC_REG3_DETECT_EN BIT(22)
67 #define MESON_SAR_ADC_REG3_ADC_EN BIT(21)
68 #define MESON_SAR_ADC_REG3_PANEL_DETECT_COUNT_MASK GENMASK(20, 18)
69 #define MESON_SAR_ADC_REG3_PANEL_DETECT_FILTER_TB_MASK GENMASK(17, 16)
70 #define MESON_SAR_ADC_REG3_ADC_CLK_DIV_SHIFT 10
71 #define MESON_SAR_ADC_REG3_ADC_CLK_DIV_WIDTH 5
72 #define MESON_SAR_ADC_REG3_BLOCK_DLY_SEL_MASK GENMASK(9, 8)
73 #define MESON_SAR_ADC_REG3_BLOCK_DLY_MASK GENMASK(7, 0)
75 #define MESON_SAR_ADC_DELAY 0x10
76 #define MESON_SAR_ADC_DELAY_INPUT_DLY_SEL_MASK GENMASK(25, 24)
77 #define MESON_SAR_ADC_DELAY_BL30_BUSY BIT(15)
78 #define MESON_SAR_ADC_DELAY_KERNEL_BUSY BIT(14)
79 #define MESON_SAR_ADC_DELAY_INPUT_DLY_CNT_MASK GENMASK(23, 16)
80 #define MESON_SAR_ADC_DELAY_SAMPLE_DLY_SEL_MASK GENMASK(9, 8)
81 #define MESON_SAR_ADC_DELAY_SAMPLE_DLY_CNT_MASK GENMASK(7, 0)
83 #define MESON_SAR_ADC_LAST_RD 0x14
84 #define MESON_SAR_ADC_LAST_RD_LAST_CHANNEL1_MASK GENMASK(23, 16)
85 #define MESON_SAR_ADC_LAST_RD_LAST_CHANNEL0_MASK GENMASK(9, 0)
87 #define MESON_SAR_ADC_FIFO_RD 0x18
88 #define MESON_SAR_ADC_FIFO_RD_CHAN_ID_MASK GENMASK(14, 12)
89 #define MESON_SAR_ADC_FIFO_RD_SAMPLE_VALUE_MASK GENMASK(11, 0)
91 #define MESON_SAR_ADC_AUX_SW 0x1c
92 #define MESON_SAR_ADC_AUX_SW_MUX_SEL_CHAN_SHIFT(_chan) \
93 (8 + (((_chan) - 2) * 3))
94 #define MESON_SAR_ADC_AUX_SW_VREF_P_MUX BIT(6)
95 #define MESON_SAR_ADC_AUX_SW_VREF_N_MUX BIT(5)
96 #define MESON_SAR_ADC_AUX_SW_MODE_SEL BIT(4)
97 #define MESON_SAR_ADC_AUX_SW_YP_DRIVE_SW BIT(3)
98 #define MESON_SAR_ADC_AUX_SW_XP_DRIVE_SW BIT(2)
99 #define MESON_SAR_ADC_AUX_SW_YM_DRIVE_SW BIT(1)
100 #define MESON_SAR_ADC_AUX_SW_XM_DRIVE_SW BIT(0)
102 #define MESON_SAR_ADC_CHAN_10_SW 0x20
103 #define MESON_SAR_ADC_CHAN_10_SW_CHAN1_MUX_SEL_MASK GENMASK(25, 23)
104 #define MESON_SAR_ADC_CHAN_10_SW_CHAN1_VREF_P_MUX BIT(22)
105 #define MESON_SAR_ADC_CHAN_10_SW_CHAN1_VREF_N_MUX BIT(21)
106 #define MESON_SAR_ADC_CHAN_10_SW_CHAN1_MODE_SEL BIT(20)
107 #define MESON_SAR_ADC_CHAN_10_SW_CHAN1_YP_DRIVE_SW BIT(19)
108 #define MESON_SAR_ADC_CHAN_10_SW_CHAN1_XP_DRIVE_SW BIT(18)
109 #define MESON_SAR_ADC_CHAN_10_SW_CHAN1_YM_DRIVE_SW BIT(17)
110 #define MESON_SAR_ADC_CHAN_10_SW_CHAN1_XM_DRIVE_SW BIT(16)
111 #define MESON_SAR_ADC_CHAN_10_SW_CHAN0_MUX_SEL_MASK GENMASK(9, 7)
112 #define MESON_SAR_ADC_CHAN_10_SW_CHAN0_VREF_P_MUX BIT(6)
113 #define MESON_SAR_ADC_CHAN_10_SW_CHAN0_VREF_N_MUX BIT(5)
114 #define MESON_SAR_ADC_CHAN_10_SW_CHAN0_MODE_SEL BIT(4)
115 #define MESON_SAR_ADC_CHAN_10_SW_CHAN0_YP_DRIVE_SW BIT(3)
116 #define MESON_SAR_ADC_CHAN_10_SW_CHAN0_XP_DRIVE_SW BIT(2)
117 #define MESON_SAR_ADC_CHAN_10_SW_CHAN0_YM_DRIVE_SW BIT(1)
118 #define MESON_SAR_ADC_CHAN_10_SW_CHAN0_XM_DRIVE_SW BIT(0)
120 #define MESON_SAR_ADC_DETECT_IDLE_SW 0x24
121 #define MESON_SAR_ADC_DETECT_IDLE_SW_DETECT_SW_EN BIT(26)
122 #define MESON_SAR_ADC_DETECT_IDLE_SW_DETECT_MUX_MASK GENMASK(25, 23)
123 #define MESON_SAR_ADC_DETECT_IDLE_SW_DETECT_VREF_P_MUX BIT(22)
124 #define MESON_SAR_ADC_DETECT_IDLE_SW_DETECT_VREF_N_MUX BIT(21)
125 #define MESON_SAR_ADC_DETECT_IDLE_SW_DETECT_MODE_SEL BIT(20)
126 #define MESON_SAR_ADC_DETECT_IDLE_SW_DETECT_YP_DRIVE_SW BIT(19)
127 #define MESON_SAR_ADC_DETECT_IDLE_SW_DETECT_XP_DRIVE_SW BIT(18)
128 #define MESON_SAR_ADC_DETECT_IDLE_SW_DETECT_YM_DRIVE_SW BIT(17)
129 #define MESON_SAR_ADC_DETECT_IDLE_SW_DETECT_XM_DRIVE_SW BIT(16)
130 #define MESON_SAR_ADC_DETECT_IDLE_SW_IDLE_MUX_SEL_MASK GENMASK(9, 7)
131 #define MESON_SAR_ADC_DETECT_IDLE_SW_IDLE_VREF_P_MUX BIT(6)
132 #define MESON_SAR_ADC_DETECT_IDLE_SW_IDLE_VREF_N_MUX BIT(5)
133 #define MESON_SAR_ADC_DETECT_IDLE_SW_IDLE_MODE_SEL BIT(4)
134 #define MESON_SAR_ADC_DETECT_IDLE_SW_IDLE_YP_DRIVE_SW BIT(3)
135 #define MESON_SAR_ADC_DETECT_IDLE_SW_IDLE_XP_DRIVE_SW BIT(2)
136 #define MESON_SAR_ADC_DETECT_IDLE_SW_IDLE_YM_DRIVE_SW BIT(1)
137 #define MESON_SAR_ADC_DETECT_IDLE_SW_IDLE_XM_DRIVE_SW BIT(0)
139 #define MESON_SAR_ADC_DELTA_10 0x28
140 #define MESON_SAR_ADC_DELTA_10_TEMP_SEL BIT(27)
141 #define MESON_SAR_ADC_DELTA_10_TS_REVE1 BIT(26)
142 #define MESON_SAR_ADC_DELTA_10_CHAN1_DELTA_VALUE_MASK GENMASK(25, 16)
143 #define MESON_SAR_ADC_DELTA_10_TS_REVE0 BIT(15)
144 #define MESON_SAR_ADC_DELTA_10_TS_C_SHIFT 11
145 #define MESON_SAR_ADC_DELTA_10_TS_C_MASK GENMASK(14, 11)
146 #define MESON_SAR_ADC_DELTA_10_TS_VBG_EN BIT(10)
147 #define MESON_SAR_ADC_DELTA_10_CHAN0_DELTA_VALUE_MASK GENMASK(9, 0)
150 * NOTE: registers from here are undocumented (the vendor Linux kernel driver
151 * and u-boot source served as reference). These only seem to be relevant on
154 #define MESON_SAR_ADC_REG11 0x2c
155 #define MESON_SAR_ADC_REG11_BANDGAP_EN BIT(13)
157 #define MESON_SAR_ADC_REG13 0x34
158 #define MESON_SAR_ADC_REG13_12BIT_CALIBRATION_MASK GENMASK(13, 8)
160 #define MESON_SAR_ADC_MAX_FIFO_SIZE 32
161 #define MESON_SAR_ADC_TIMEOUT 100 /* ms */
163 #define NUM_CHANNELS 8
165 #define MILLION 1000000
167 struct meson_saradc_data {
171 struct meson_saradc_priv {
172 const struct meson_saradc_data *data;
173 struct regmap *regmap;
183 meson_saradc_get_fifo_count(struct meson_saradc_priv *priv)
187 regmap_read(priv->regmap, MESON_SAR_ADC_REG0, ®val);
189 return FIELD_GET(MESON_SAR_ADC_REG0_FIFO_COUNT_MASK, regval);
192 static int meson_saradc_lock(struct meson_saradc_priv *priv)
194 uint val, timeout = 10000;
196 /* prevent BL30 from using the SAR ADC while we are using it */
197 regmap_update_bits(priv->regmap, MESON_SAR_ADC_DELAY,
198 MESON_SAR_ADC_DELAY_KERNEL_BUSY,
199 MESON_SAR_ADC_DELAY_KERNEL_BUSY);
202 * wait until BL30 releases it's lock (so we can use the SAR ADC)
206 regmap_read(priv->regmap, MESON_SAR_ADC_DELAY, &val);
207 } while (val & MESON_SAR_ADC_DELAY_BL30_BUSY && timeout--);
210 printf("Timeout while waiting for BL30 unlock\n");
217 static void meson_saradc_unlock(struct meson_saradc_priv *priv)
219 /* allow BL30 to use the SAR ADC again */
220 regmap_update_bits(priv->regmap, MESON_SAR_ADC_DELAY,
221 MESON_SAR_ADC_DELAY_KERNEL_BUSY, 0);
224 static void meson_saradc_clear_fifo(struct meson_saradc_priv *priv)
226 unsigned int count, tmp;
228 for (count = 0; count < MESON_SAR_ADC_MAX_FIFO_SIZE; count++) {
229 if (!meson_saradc_get_fifo_count(priv))
232 regmap_read(priv->regmap, MESON_SAR_ADC_FIFO_RD, &tmp);
236 static int meson_saradc_calib_val(struct meson_saradc_priv *priv, int val)
240 /* use val_calib = scale * val_raw + offset calibration function */
241 tmp = div_s64((s64)val * priv->calibscale, MILLION) + priv->calibbias;
243 return clamp(tmp, 0, (1 << priv->data->num_bits) - 1);
246 static int meson_saradc_wait_busy_clear(struct meson_saradc_priv *priv)
248 uint regval, timeout = 10000;
251 * NOTE: we need a small delay before reading the status, otherwise
252 * the sample engine may not have started internally (which would
253 * seem to us that sampling is already finished).
257 regmap_read(priv->regmap, MESON_SAR_ADC_REG0, ®val);
258 } while (FIELD_GET(MESON_SAR_ADC_REG0_BUSY_MASK, regval) && timeout--);
266 static int meson_saradc_read_raw_sample(struct meson_saradc_priv *priv,
267 unsigned int channel, uint *val)
269 uint regval, fifo_chan, fifo_val, count;
272 ret = meson_saradc_wait_busy_clear(priv);
276 count = meson_saradc_get_fifo_count(priv);
278 printf("ADC FIFO has %d element(s) instead of one\n", count);
282 regmap_read(priv->regmap, MESON_SAR_ADC_FIFO_RD, ®val);
283 fifo_chan = FIELD_GET(MESON_SAR_ADC_FIFO_RD_CHAN_ID_MASK, regval);
284 if (fifo_chan != channel) {
285 printf("ADC FIFO entry belongs to channel %u instead of %u\n",
290 fifo_val = FIELD_GET(MESON_SAR_ADC_FIFO_RD_SAMPLE_VALUE_MASK, regval);
291 fifo_val &= GENMASK(priv->data->num_bits - 1, 0);
292 *val = meson_saradc_calib_val(priv, fifo_val);
297 static void meson_saradc_start_sample_engine(struct meson_saradc_priv *priv)
299 regmap_update_bits(priv->regmap, MESON_SAR_ADC_REG0,
300 MESON_SAR_ADC_REG0_FIFO_IRQ_EN,
301 MESON_SAR_ADC_REG0_FIFO_IRQ_EN);
303 regmap_update_bits(priv->regmap, MESON_SAR_ADC_REG0,
304 MESON_SAR_ADC_REG0_SAMPLE_ENGINE_ENABLE,
305 MESON_SAR_ADC_REG0_SAMPLE_ENGINE_ENABLE);
307 regmap_update_bits(priv->regmap, MESON_SAR_ADC_REG0,
308 MESON_SAR_ADC_REG0_SAMPLING_START,
309 MESON_SAR_ADC_REG0_SAMPLING_START);
312 static void meson_saradc_stop_sample_engine(struct meson_saradc_priv *priv)
314 regmap_update_bits(priv->regmap, MESON_SAR_ADC_REG0,
315 MESON_SAR_ADC_REG0_FIFO_IRQ_EN, 0);
317 regmap_update_bits(priv->regmap, MESON_SAR_ADC_REG0,
318 MESON_SAR_ADC_REG0_SAMPLING_STOP,
319 MESON_SAR_ADC_REG0_SAMPLING_STOP);
321 /* wait until all modules are stopped */
322 meson_saradc_wait_busy_clear(priv);
324 regmap_update_bits(priv->regmap, MESON_SAR_ADC_REG0,
325 MESON_SAR_ADC_REG0_SAMPLE_ENGINE_ENABLE, 0);
328 enum meson_saradc_avg_mode {
330 MEAN_AVERAGING = 0x1,
331 MEDIAN_AVERAGING = 0x2,
334 enum meson_saradc_num_samples {
341 static void meson_saradc_set_averaging(struct meson_saradc_priv *priv,
342 unsigned int channel,
343 enum meson_saradc_avg_mode mode,
344 enum meson_saradc_num_samples samples)
348 val = samples << MESON_SAR_ADC_AVG_CNTL_NUM_SAMPLES_SHIFT(channel);
349 regmap_update_bits(priv->regmap, MESON_SAR_ADC_AVG_CNTL,
350 MESON_SAR_ADC_AVG_CNTL_NUM_SAMPLES_MASK(channel),
353 val = mode << MESON_SAR_ADC_AVG_CNTL_AVG_MODE_SHIFT(channel);
354 regmap_update_bits(priv->regmap, MESON_SAR_ADC_AVG_CNTL,
355 MESON_SAR_ADC_AVG_CNTL_AVG_MODE_MASK(channel), val);
358 static void meson_saradc_enable_channel(struct meson_saradc_priv *priv,
359 unsigned int channel)
364 * the SAR ADC engine allows sampling multiple channels at the same
365 * time. to keep it simple we're only working with one *internal*
366 * channel, which starts counting at index 0 (which means: count = 1).
368 regval = FIELD_PREP(MESON_SAR_ADC_CHAN_LIST_MAX_INDEX_MASK, 0);
369 regmap_update_bits(priv->regmap, MESON_SAR_ADC_CHAN_LIST,
370 MESON_SAR_ADC_CHAN_LIST_MAX_INDEX_MASK, regval);
372 /* map channel index 0 to the channel which we want to read */
373 regval = FIELD_PREP(MESON_SAR_ADC_CHAN_LIST_ENTRY_MASK(0), channel);
374 regmap_update_bits(priv->regmap, MESON_SAR_ADC_CHAN_LIST,
375 MESON_SAR_ADC_CHAN_LIST_ENTRY_MASK(0), regval);
377 regval = FIELD_PREP(MESON_SAR_ADC_DETECT_IDLE_SW_DETECT_MUX_MASK,
379 regmap_update_bits(priv->regmap, MESON_SAR_ADC_DETECT_IDLE_SW,
380 MESON_SAR_ADC_DETECT_IDLE_SW_DETECT_MUX_MASK,
383 regval = FIELD_PREP(MESON_SAR_ADC_DETECT_IDLE_SW_IDLE_MUX_SEL_MASK,
385 regmap_update_bits(priv->regmap, MESON_SAR_ADC_DETECT_IDLE_SW,
386 MESON_SAR_ADC_DETECT_IDLE_SW_IDLE_MUX_SEL_MASK,
390 regmap_update_bits(priv->regmap, MESON_SAR_ADC_DELTA_10,
391 MESON_SAR_ADC_DELTA_10_TEMP_SEL, 0);
394 static int meson_saradc_get_sample(struct meson_saradc_priv *priv,
399 ret = meson_saradc_lock(priv);
403 /* clear the FIFO to make sure we're not reading old values */
404 meson_saradc_clear_fifo(priv);
406 meson_saradc_set_averaging(priv, chan, MEAN_AVERAGING, EIGHT_SAMPLES);
408 meson_saradc_enable_channel(priv, chan);
410 meson_saradc_start_sample_engine(priv);
411 ret = meson_saradc_read_raw_sample(priv, chan, val);
412 meson_saradc_stop_sample_engine(priv);
414 meson_saradc_unlock(priv);
417 printf("failed to read sample for channel %d: %d\n",
425 static int meson_saradc_channel_data(struct udevice *dev, int channel,
428 struct meson_saradc_priv *priv = dev_get_priv(dev);
430 if (channel != priv->active_channel) {
431 pr_err("Requested channel is not active!");
435 return meson_saradc_get_sample(priv, channel, data);
438 enum meson_saradc_chan7_mux_sel {
440 CHAN7_MUX_VDD_DIV4 = 0x1,
441 CHAN7_MUX_VDD_DIV2 = 0x2,
442 CHAN7_MUX_VDD_MUL3_DIV4 = 0x3,
444 CHAN7_MUX_CH7_INPUT = 0x7,
447 static void meson_saradc_set_chan7_mux(struct meson_saradc_priv *priv,
448 enum meson_saradc_chan7_mux_sel sel)
452 regval = FIELD_PREP(MESON_SAR_ADC_REG3_CTRL_CHAN7_MUX_SEL_MASK, sel);
453 regmap_update_bits(priv->regmap, MESON_SAR_ADC_REG3,
454 MESON_SAR_ADC_REG3_CTRL_CHAN7_MUX_SEL_MASK, regval);
459 static int meson_saradc_calib(struct meson_saradc_priv *priv)
461 uint nominal0, nominal1, value0, value1;
464 /* use points 25% and 75% for calibration */
465 nominal0 = (1 << priv->data->num_bits) / 4;
466 nominal1 = (1 << priv->data->num_bits) * 3 / 4;
468 meson_saradc_set_chan7_mux(priv, CHAN7_MUX_VDD_DIV4);
470 ret = meson_saradc_get_sample(priv, 7, &value0);
474 meson_saradc_set_chan7_mux(priv, CHAN7_MUX_VDD_MUL3_DIV4);
476 ret = meson_saradc_get_sample(priv, 7, &value1);
480 if (value1 <= value0) {
485 priv->calibscale = div_s64((nominal1 - nominal0) * (s64)MILLION,
487 priv->calibbias = nominal0 - div_s64((s64)value0 * priv->calibscale,
491 meson_saradc_set_chan7_mux(priv, CHAN7_MUX_CH7_INPUT);
496 static int meson_saradc_init(struct meson_saradc_priv *priv)
501 priv->calibscale = MILLION;
504 * make sure we start at CH7 input since the other muxes are only used
505 * for internal calibration.
507 meson_saradc_set_chan7_mux(priv, CHAN7_MUX_CH7_INPUT);
510 * leave sampling delay and the input clocks as configured by
511 * BL30 to make sure BL30 gets the values it expects when
512 * reading the temperature sensor.
514 regmap_read(priv->regmap, MESON_SAR_ADC_REG3, ®val);
515 if (regval & MESON_SAR_ADC_REG3_BL30_INITIALIZED) {
516 regmap_read(priv->regmap, MESON_SAR_ADC_REG3, ®val);
517 if (regval & MESON_SAR_ADC_REG3_ADC_EN)
521 meson_saradc_stop_sample_engine(priv);
523 /* update the channel 6 MUX to select the temperature sensor */
524 regmap_update_bits(priv->regmap, MESON_SAR_ADC_REG0,
525 MESON_SAR_ADC_REG0_ADC_TEMP_SEN_SEL,
526 MESON_SAR_ADC_REG0_ADC_TEMP_SEN_SEL);
528 /* disable all channels by default */
529 regmap_write(priv->regmap, MESON_SAR_ADC_CHAN_LIST, 0x0);
531 regmap_update_bits(priv->regmap, MESON_SAR_ADC_REG3,
532 MESON_SAR_ADC_REG3_CTRL_SAMPLING_CLOCK_PHASE, 0);
533 regmap_update_bits(priv->regmap, MESON_SAR_ADC_REG3,
534 MESON_SAR_ADC_REG3_CNTL_USE_SC_DLY,
535 MESON_SAR_ADC_REG3_CNTL_USE_SC_DLY);
537 /* delay between two samples = (10+1) * 1uS */
538 regmap_update_bits(priv->regmap, MESON_SAR_ADC_DELAY,
539 MESON_SAR_ADC_DELAY_INPUT_DLY_CNT_MASK,
540 FIELD_PREP(MESON_SAR_ADC_DELAY_SAMPLE_DLY_CNT_MASK,
542 regmap_update_bits(priv->regmap, MESON_SAR_ADC_DELAY,
543 MESON_SAR_ADC_DELAY_SAMPLE_DLY_SEL_MASK,
544 FIELD_PREP(MESON_SAR_ADC_DELAY_SAMPLE_DLY_SEL_MASK,
547 /* delay between two samples = (10+1) * 1uS */
548 regmap_update_bits(priv->regmap, MESON_SAR_ADC_DELAY,
549 MESON_SAR_ADC_DELAY_INPUT_DLY_CNT_MASK,
550 FIELD_PREP(MESON_SAR_ADC_DELAY_INPUT_DLY_CNT_MASK,
552 regmap_update_bits(priv->regmap, MESON_SAR_ADC_DELAY,
553 MESON_SAR_ADC_DELAY_INPUT_DLY_SEL_MASK,
554 FIELD_PREP(MESON_SAR_ADC_DELAY_INPUT_DLY_SEL_MASK,
558 * set up the input channel muxes in MESON_SAR_ADC_CHAN_10_SW
559 * (0 = SAR_ADC_CH0, 1 = SAR_ADC_CH1)
561 regval = FIELD_PREP(MESON_SAR_ADC_CHAN_10_SW_CHAN0_MUX_SEL_MASK, 0);
562 regmap_update_bits(priv->regmap, MESON_SAR_ADC_CHAN_10_SW,
563 MESON_SAR_ADC_CHAN_10_SW_CHAN0_MUX_SEL_MASK,
565 regval = FIELD_PREP(MESON_SAR_ADC_CHAN_10_SW_CHAN1_MUX_SEL_MASK, 1);
566 regmap_update_bits(priv->regmap, MESON_SAR_ADC_CHAN_10_SW,
567 MESON_SAR_ADC_CHAN_10_SW_CHAN1_MUX_SEL_MASK,
571 * set up the input channel muxes in MESON_SAR_ADC_AUX_SW
572 * (2 = SAR_ADC_CH2, 3 = SAR_ADC_CH3, ...) and enable
573 * MESON_SAR_ADC_AUX_SW_YP_DRIVE_SW and
574 * MESON_SAR_ADC_AUX_SW_XP_DRIVE_SW like the vendor driver.
577 for (i = 2; i <= 7; i++)
578 regval |= i << MESON_SAR_ADC_AUX_SW_MUX_SEL_CHAN_SHIFT(i);
579 regval |= MESON_SAR_ADC_AUX_SW_YP_DRIVE_SW;
580 regval |= MESON_SAR_ADC_AUX_SW_XP_DRIVE_SW;
581 regmap_write(priv->regmap, MESON_SAR_ADC_AUX_SW, regval);
583 ret = meson_saradc_lock(priv);
587 #if CONFIG_IS_ENABLED(CLK)
588 ret = clk_enable(&priv->core_clk);
593 regval = FIELD_PREP(MESON_SAR_ADC_REG0_FIFO_CNT_IRQ_MASK, 1);
594 regmap_update_bits(priv->regmap, MESON_SAR_ADC_REG0,
595 MESON_SAR_ADC_REG0_FIFO_CNT_IRQ_MASK, regval);
597 regmap_update_bits(priv->regmap, MESON_SAR_ADC_REG11,
598 MESON_SAR_ADC_REG11_BANDGAP_EN,
599 MESON_SAR_ADC_REG11_BANDGAP_EN);
601 regmap_update_bits(priv->regmap, MESON_SAR_ADC_REG3,
602 MESON_SAR_ADC_REG3_ADC_EN,
603 MESON_SAR_ADC_REG3_ADC_EN);
607 #if CONFIG_IS_ENABLED(CLK)
608 ret = clk_enable(&priv->adc_clk);
613 meson_saradc_unlock(priv);
615 ret = meson_saradc_calib(priv);
617 printf("calibration failed\n");
624 static int meson_saradc_start_channel(struct udevice *dev, int channel)
626 struct meson_saradc_priv *priv = dev_get_priv(dev);
628 if (channel < 0 || channel >= NUM_CHANNELS) {
629 printf("Requested channel is invalid!");
633 if (!priv->initialized) {
636 ret = meson_saradc_init(priv);
640 priv->initialized = true;
643 priv->active_channel = channel;
648 static int meson_saradc_stop(struct udevice *dev)
650 struct meson_saradc_priv *priv = dev_get_priv(dev);
652 priv->active_channel = -1;
657 static int meson_saradc_probe(struct udevice *dev)
659 struct meson_saradc_priv *priv = dev_get_priv(dev);
662 ret = regmap_init_mem(dev_ofnode(dev), &priv->regmap);
666 #if CONFIG_IS_ENABLED(CLK)
667 ret = clk_get_by_name(dev, "core", &priv->core_clk);
671 ret = clk_get_by_name(dev, "adc_clk", &priv->adc_clk);
676 priv->active_channel = -1;
681 int meson_saradc_of_to_plat(struct udevice *dev)
683 struct adc_uclass_plat *uc_pdata = dev_get_uclass_plat(dev);
684 struct meson_saradc_priv *priv = dev_get_priv(dev);
686 priv->data = (struct meson_saradc_data *)dev_get_driver_data(dev);
688 uc_pdata->data_mask = GENMASK(priv->data->num_bits - 1, 0);
689 uc_pdata->data_format = ADC_DATA_FORMAT_BIN;
690 uc_pdata->data_timeout_us = MESON_SAR_ADC_TIMEOUT * 1000;
691 uc_pdata->channel_mask = GENMASK(NUM_CHANNELS - 1, 0);
696 static const struct adc_ops meson_saradc_ops = {
697 .start_channel = meson_saradc_start_channel,
698 .channel_data = meson_saradc_channel_data,
699 .stop = meson_saradc_stop,
702 static const struct meson_saradc_data gxbb_saradc_data = {
706 static const struct meson_saradc_data gxl_saradc_data = {
710 static const struct udevice_id meson_saradc_ids[] = {
711 { .compatible = "amlogic,meson-gxbb-saradc",
712 .data = (ulong)&gxbb_saradc_data },
713 { .compatible = "amlogic,meson-gxl-saradc",
714 .data = (ulong)&gxl_saradc_data },
715 { .compatible = "amlogic,meson-gxm-saradc",
716 .data = (ulong)&gxl_saradc_data },
717 { .compatible = "amlogic,meson-g12a-saradc",
718 .data = (ulong)&gxl_saradc_data },
722 U_BOOT_DRIVER(meson_saradc) = {
723 .name = "meson_saradc",
725 .of_match = meson_saradc_ids,
726 .ops = &meson_saradc_ops,
727 .probe = meson_saradc_probe,
728 .of_to_plat = meson_saradc_of_to_plat,
729 .priv_auto = sizeof(struct meson_saradc_priv),