1 // SPDX-License-Identifier: GPL-2.0
3 * Amlogic Meson Successive Approximation Register (SAR) A/D Converter
5 * Copyright (C) 2017 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
8 #include <linux/bitfield.h>
10 #include <linux/clk-provider.h>
11 #include <linux/delay.h>
13 #include <linux/iio/iio.h>
14 #include <linux/module.h>
15 #include <linux/mutex.h>
16 #include <linux/nvmem-consumer.h>
17 #include <linux/interrupt.h>
19 #include <linux/of_irq.h>
20 #include <linux/of_device.h>
21 #include <linux/platform_device.h>
22 #include <linux/regmap.h>
23 #include <linux/regulator/consumer.h>
24 #include <linux/mfd/syscon.h>
26 #define MESON_SAR_ADC_REG0 0x00
27 #define MESON_SAR_ADC_REG0_PANEL_DETECT BIT(31)
28 #define MESON_SAR_ADC_REG0_BUSY_MASK GENMASK(30, 28)
29 #define MESON_SAR_ADC_REG0_DELTA_BUSY BIT(30)
30 #define MESON_SAR_ADC_REG0_AVG_BUSY BIT(29)
31 #define MESON_SAR_ADC_REG0_SAMPLE_BUSY BIT(28)
32 #define MESON_SAR_ADC_REG0_FIFO_FULL BIT(27)
33 #define MESON_SAR_ADC_REG0_FIFO_EMPTY BIT(26)
34 #define MESON_SAR_ADC_REG0_FIFO_COUNT_MASK GENMASK(25, 21)
35 #define MESON_SAR_ADC_REG0_ADC_BIAS_CTRL_MASK GENMASK(20, 19)
36 #define MESON_SAR_ADC_REG0_CURR_CHAN_ID_MASK GENMASK(18, 16)
37 #define MESON_SAR_ADC_REG0_ADC_TEMP_SEN_SEL BIT(15)
38 #define MESON_SAR_ADC_REG0_SAMPLING_STOP BIT(14)
39 #define MESON_SAR_ADC_REG0_CHAN_DELTA_EN_MASK GENMASK(13, 12)
40 #define MESON_SAR_ADC_REG0_DETECT_IRQ_POL BIT(10)
41 #define MESON_SAR_ADC_REG0_DETECT_IRQ_EN BIT(9)
42 #define MESON_SAR_ADC_REG0_FIFO_CNT_IRQ_MASK GENMASK(8, 4)
43 #define MESON_SAR_ADC_REG0_FIFO_IRQ_EN BIT(3)
44 #define MESON_SAR_ADC_REG0_SAMPLING_START BIT(2)
45 #define MESON_SAR_ADC_REG0_CONTINUOUS_EN BIT(1)
46 #define MESON_SAR_ADC_REG0_SAMPLE_ENGINE_ENABLE BIT(0)
48 #define MESON_SAR_ADC_CHAN_LIST 0x04
49 #define MESON_SAR_ADC_CHAN_LIST_MAX_INDEX_MASK GENMASK(26, 24)
50 #define MESON_SAR_ADC_CHAN_LIST_ENTRY_MASK(_chan) \
51 (GENMASK(2, 0) << ((_chan) * 3))
53 #define MESON_SAR_ADC_AVG_CNTL 0x08
54 #define MESON_SAR_ADC_AVG_CNTL_AVG_MODE_SHIFT(_chan) \
56 #define MESON_SAR_ADC_AVG_CNTL_AVG_MODE_MASK(_chan) \
57 (GENMASK(17, 16) << ((_chan) * 2))
58 #define MESON_SAR_ADC_AVG_CNTL_NUM_SAMPLES_SHIFT(_chan) \
60 #define MESON_SAR_ADC_AVG_CNTL_NUM_SAMPLES_MASK(_chan) \
61 (GENMASK(1, 0) << ((_chan) * 2))
63 #define MESON_SAR_ADC_REG3 0x0c
64 #define MESON_SAR_ADC_REG3_CNTL_USE_SC_DLY BIT(31)
65 #define MESON_SAR_ADC_REG3_CLK_EN BIT(30)
66 #define MESON_SAR_ADC_REG3_BL30_INITIALIZED BIT(28)
67 #define MESON_SAR_ADC_REG3_CTRL_CONT_RING_COUNTER_EN BIT(27)
68 #define MESON_SAR_ADC_REG3_CTRL_SAMPLING_CLOCK_PHASE BIT(26)
69 #define MESON_SAR_ADC_REG3_CTRL_CHAN7_MUX_SEL_MASK GENMASK(25, 23)
70 #define MESON_SAR_ADC_REG3_DETECT_EN BIT(22)
71 #define MESON_SAR_ADC_REG3_ADC_EN BIT(21)
72 #define MESON_SAR_ADC_REG3_PANEL_DETECT_COUNT_MASK GENMASK(20, 18)
73 #define MESON_SAR_ADC_REG3_PANEL_DETECT_FILTER_TB_MASK GENMASK(17, 16)
74 #define MESON_SAR_ADC_REG3_ADC_CLK_DIV_SHIFT 10
75 #define MESON_SAR_ADC_REG3_ADC_CLK_DIV_WIDTH 6
76 #define MESON_SAR_ADC_REG3_BLOCK_DLY_SEL_MASK GENMASK(9, 8)
77 #define MESON_SAR_ADC_REG3_BLOCK_DLY_MASK GENMASK(7, 0)
79 #define MESON_SAR_ADC_DELAY 0x10
80 #define MESON_SAR_ADC_DELAY_INPUT_DLY_SEL_MASK GENMASK(25, 24)
81 #define MESON_SAR_ADC_DELAY_BL30_BUSY BIT(15)
82 #define MESON_SAR_ADC_DELAY_KERNEL_BUSY BIT(14)
83 #define MESON_SAR_ADC_DELAY_INPUT_DLY_CNT_MASK GENMASK(23, 16)
84 #define MESON_SAR_ADC_DELAY_SAMPLE_DLY_SEL_MASK GENMASK(9, 8)
85 #define MESON_SAR_ADC_DELAY_SAMPLE_DLY_CNT_MASK GENMASK(7, 0)
87 #define MESON_SAR_ADC_LAST_RD 0x14
88 #define MESON_SAR_ADC_LAST_RD_LAST_CHANNEL1_MASK GENMASK(23, 16)
89 #define MESON_SAR_ADC_LAST_RD_LAST_CHANNEL0_MASK GENMASK(9, 0)
91 #define MESON_SAR_ADC_FIFO_RD 0x18
92 #define MESON_SAR_ADC_FIFO_RD_CHAN_ID_MASK GENMASK(14, 12)
93 #define MESON_SAR_ADC_FIFO_RD_SAMPLE_VALUE_MASK GENMASK(11, 0)
95 #define MESON_SAR_ADC_AUX_SW 0x1c
96 #define MESON_SAR_ADC_AUX_SW_MUX_SEL_CHAN_SHIFT(_chan) \
97 (8 + (((_chan) - 2) * 3))
98 #define MESON_SAR_ADC_AUX_SW_VREF_P_MUX BIT(6)
99 #define MESON_SAR_ADC_AUX_SW_VREF_N_MUX BIT(5)
100 #define MESON_SAR_ADC_AUX_SW_MODE_SEL BIT(4)
101 #define MESON_SAR_ADC_AUX_SW_YP_DRIVE_SW BIT(3)
102 #define MESON_SAR_ADC_AUX_SW_XP_DRIVE_SW BIT(2)
103 #define MESON_SAR_ADC_AUX_SW_YM_DRIVE_SW BIT(1)
104 #define MESON_SAR_ADC_AUX_SW_XM_DRIVE_SW BIT(0)
106 #define MESON_SAR_ADC_CHAN_10_SW 0x20
107 #define MESON_SAR_ADC_CHAN_10_SW_CHAN1_MUX_SEL_MASK GENMASK(25, 23)
108 #define MESON_SAR_ADC_CHAN_10_SW_CHAN1_VREF_P_MUX BIT(22)
109 #define MESON_SAR_ADC_CHAN_10_SW_CHAN1_VREF_N_MUX BIT(21)
110 #define MESON_SAR_ADC_CHAN_10_SW_CHAN1_MODE_SEL BIT(20)
111 #define MESON_SAR_ADC_CHAN_10_SW_CHAN1_YP_DRIVE_SW BIT(19)
112 #define MESON_SAR_ADC_CHAN_10_SW_CHAN1_XP_DRIVE_SW BIT(18)
113 #define MESON_SAR_ADC_CHAN_10_SW_CHAN1_YM_DRIVE_SW BIT(17)
114 #define MESON_SAR_ADC_CHAN_10_SW_CHAN1_XM_DRIVE_SW BIT(16)
115 #define MESON_SAR_ADC_CHAN_10_SW_CHAN0_MUX_SEL_MASK GENMASK(9, 7)
116 #define MESON_SAR_ADC_CHAN_10_SW_CHAN0_VREF_P_MUX BIT(6)
117 #define MESON_SAR_ADC_CHAN_10_SW_CHAN0_VREF_N_MUX BIT(5)
118 #define MESON_SAR_ADC_CHAN_10_SW_CHAN0_MODE_SEL BIT(4)
119 #define MESON_SAR_ADC_CHAN_10_SW_CHAN0_YP_DRIVE_SW BIT(3)
120 #define MESON_SAR_ADC_CHAN_10_SW_CHAN0_XP_DRIVE_SW BIT(2)
121 #define MESON_SAR_ADC_CHAN_10_SW_CHAN0_YM_DRIVE_SW BIT(1)
122 #define MESON_SAR_ADC_CHAN_10_SW_CHAN0_XM_DRIVE_SW BIT(0)
124 #define MESON_SAR_ADC_DETECT_IDLE_SW 0x24
125 #define MESON_SAR_ADC_DETECT_IDLE_SW_DETECT_SW_EN BIT(26)
126 #define MESON_SAR_ADC_DETECT_IDLE_SW_DETECT_MUX_MASK GENMASK(25, 23)
127 #define MESON_SAR_ADC_DETECT_IDLE_SW_DETECT_VREF_P_MUX BIT(22)
128 #define MESON_SAR_ADC_DETECT_IDLE_SW_DETECT_VREF_N_MUX BIT(21)
129 #define MESON_SAR_ADC_DETECT_IDLE_SW_DETECT_MODE_SEL BIT(20)
130 #define MESON_SAR_ADC_DETECT_IDLE_SW_DETECT_YP_DRIVE_SW BIT(19)
131 #define MESON_SAR_ADC_DETECT_IDLE_SW_DETECT_XP_DRIVE_SW BIT(18)
132 #define MESON_SAR_ADC_DETECT_IDLE_SW_DETECT_YM_DRIVE_SW BIT(17)
133 #define MESON_SAR_ADC_DETECT_IDLE_SW_DETECT_XM_DRIVE_SW BIT(16)
134 #define MESON_SAR_ADC_DETECT_IDLE_SW_IDLE_MUX_SEL_MASK GENMASK(9, 7)
135 #define MESON_SAR_ADC_DETECT_IDLE_SW_IDLE_VREF_P_MUX BIT(6)
136 #define MESON_SAR_ADC_DETECT_IDLE_SW_IDLE_VREF_N_MUX BIT(5)
137 #define MESON_SAR_ADC_DETECT_IDLE_SW_IDLE_MODE_SEL BIT(4)
138 #define MESON_SAR_ADC_DETECT_IDLE_SW_IDLE_YP_DRIVE_SW BIT(3)
139 #define MESON_SAR_ADC_DETECT_IDLE_SW_IDLE_XP_DRIVE_SW BIT(2)
140 #define MESON_SAR_ADC_DETECT_IDLE_SW_IDLE_YM_DRIVE_SW BIT(1)
141 #define MESON_SAR_ADC_DETECT_IDLE_SW_IDLE_XM_DRIVE_SW BIT(0)
143 #define MESON_SAR_ADC_DELTA_10 0x28
144 #define MESON_SAR_ADC_DELTA_10_TEMP_SEL BIT(27)
145 #define MESON_SAR_ADC_DELTA_10_TS_REVE1 BIT(26)
146 #define MESON_SAR_ADC_DELTA_10_CHAN1_DELTA_VALUE_MASK GENMASK(25, 16)
147 #define MESON_SAR_ADC_DELTA_10_TS_REVE0 BIT(15)
148 #define MESON_SAR_ADC_DELTA_10_TS_C_MASK GENMASK(14, 11)
149 #define MESON_SAR_ADC_DELTA_10_TS_VBG_EN BIT(10)
150 #define MESON_SAR_ADC_DELTA_10_CHAN0_DELTA_VALUE_MASK GENMASK(9, 0)
153 * NOTE: registers from here are undocumented (the vendor Linux kernel driver
154 * and u-boot source served as reference). These only seem to be relevant on
157 #define MESON_SAR_ADC_REG11 0x2c
158 #define MESON_SAR_ADC_REG11_BANDGAP_EN BIT(13)
160 #define MESON_SAR_ADC_REG13 0x34
161 #define MESON_SAR_ADC_REG13_12BIT_CALIBRATION_MASK GENMASK(13, 8)
163 #define MESON_SAR_ADC_MAX_FIFO_SIZE 32
164 #define MESON_SAR_ADC_TIMEOUT 100 /* ms */
165 #define MESON_SAR_ADC_VOLTAGE_AND_TEMP_CHANNEL 6
166 #define MESON_SAR_ADC_TEMP_OFFSET 27
168 /* temperature sensor calibration information in eFuse */
169 #define MESON_SAR_ADC_EFUSE_BYTES 4
170 #define MESON_SAR_ADC_EFUSE_BYTE3_UPPER_ADC_VAL GENMASK(6, 0)
171 #define MESON_SAR_ADC_EFUSE_BYTE3_IS_CALIBRATED BIT(7)
173 #define MESON_HHI_DPLL_TOP_0 0x318
174 #define MESON_HHI_DPLL_TOP_0_TSC_BIT4 BIT(9)
176 /* for use with IIO_VAL_INT_PLUS_MICRO */
177 #define MILLION 1000000
179 #define MESON_SAR_ADC_CHAN(_chan) { \
180 .type = IIO_VOLTAGE, \
184 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
185 BIT(IIO_CHAN_INFO_AVERAGE_RAW), \
186 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
187 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_CALIBBIAS) | \
188 BIT(IIO_CHAN_INFO_CALIBSCALE), \
189 .datasheet_name = "SAR_ADC_CH"#_chan, \
192 #define MESON_SAR_ADC_TEMP_CHAN(_chan) { \
195 .address = MESON_SAR_ADC_VOLTAGE_AND_TEMP_CHANNEL, \
196 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
197 BIT(IIO_CHAN_INFO_AVERAGE_RAW), \
198 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) | \
199 BIT(IIO_CHAN_INFO_SCALE), \
200 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_CALIBBIAS) | \
201 BIT(IIO_CHAN_INFO_CALIBSCALE), \
202 .datasheet_name = "TEMP_SENSOR", \
205 static const struct iio_chan_spec meson_sar_adc_iio_channels[] = {
206 MESON_SAR_ADC_CHAN(0),
207 MESON_SAR_ADC_CHAN(1),
208 MESON_SAR_ADC_CHAN(2),
209 MESON_SAR_ADC_CHAN(3),
210 MESON_SAR_ADC_CHAN(4),
211 MESON_SAR_ADC_CHAN(5),
212 MESON_SAR_ADC_CHAN(6),
213 MESON_SAR_ADC_CHAN(7),
214 IIO_CHAN_SOFT_TIMESTAMP(8),
217 static const struct iio_chan_spec meson_sar_adc_and_temp_iio_channels[] = {
218 MESON_SAR_ADC_CHAN(0),
219 MESON_SAR_ADC_CHAN(1),
220 MESON_SAR_ADC_CHAN(2),
221 MESON_SAR_ADC_CHAN(3),
222 MESON_SAR_ADC_CHAN(4),
223 MESON_SAR_ADC_CHAN(5),
224 MESON_SAR_ADC_CHAN(6),
225 MESON_SAR_ADC_CHAN(7),
226 MESON_SAR_ADC_TEMP_CHAN(8),
227 IIO_CHAN_SOFT_TIMESTAMP(9),
230 enum meson_sar_adc_avg_mode {
232 MEAN_AVERAGING = 0x1,
233 MEDIAN_AVERAGING = 0x2,
236 enum meson_sar_adc_num_samples {
243 enum meson_sar_adc_chan7_mux_sel {
245 CHAN7_MUX_VDD_DIV4 = 0x1,
246 CHAN7_MUX_VDD_DIV2 = 0x2,
247 CHAN7_MUX_VDD_MUL3_DIV4 = 0x3,
249 CHAN7_MUX_CH7_INPUT = 0x7,
252 struct meson_sar_adc_param {
253 bool has_bl30_integration;
254 unsigned long clock_rate;
256 unsigned int resolution;
257 const struct regmap_config *regmap_config;
258 u8 temperature_trimming_bits;
259 unsigned int temperature_multiplier;
260 unsigned int temperature_divider;
263 struct meson_sar_adc_data {
264 const struct meson_sar_adc_param *param;
268 struct meson_sar_adc_priv {
269 struct regmap *regmap;
270 struct regulator *vref;
271 const struct meson_sar_adc_param *param;
273 struct clk *core_clk;
274 struct clk *adc_sel_clk;
276 struct clk_gate clk_gate;
277 struct clk *adc_div_clk;
278 struct clk_divider clk_div;
279 struct completion done;
280 /* lock to protect against multiple access to the device */
284 struct regmap *tsc_regmap;
285 bool temperature_sensor_calibrated;
286 u8 temperature_sensor_coefficient;
287 u16 temperature_sensor_adc_val;
290 static const struct regmap_config meson_sar_adc_regmap_config_gxbb = {
294 .max_register = MESON_SAR_ADC_REG13,
297 static const struct regmap_config meson_sar_adc_regmap_config_meson8 = {
301 .max_register = MESON_SAR_ADC_DELTA_10,
304 static unsigned int meson_sar_adc_get_fifo_count(struct iio_dev *indio_dev)
306 struct meson_sar_adc_priv *priv = iio_priv(indio_dev);
309 regmap_read(priv->regmap, MESON_SAR_ADC_REG0, ®val);
311 return FIELD_GET(MESON_SAR_ADC_REG0_FIFO_COUNT_MASK, regval);
314 static int meson_sar_adc_calib_val(struct iio_dev *indio_dev, int val)
316 struct meson_sar_adc_priv *priv = iio_priv(indio_dev);
319 /* use val_calib = scale * val_raw + offset calibration function */
320 tmp = div_s64((s64)val * priv->calibscale, MILLION) + priv->calibbias;
322 return clamp(tmp, 0, (1 << priv->param->resolution) - 1);
325 static int meson_sar_adc_wait_busy_clear(struct iio_dev *indio_dev)
327 struct meson_sar_adc_priv *priv = iio_priv(indio_dev);
331 * NOTE: we need a small delay before reading the status, otherwise
332 * the sample engine may not have started internally (which would
333 * seem to us that sampling is already finished).
336 return regmap_read_poll_timeout_atomic(priv->regmap, MESON_SAR_ADC_REG0, val,
337 !FIELD_GET(MESON_SAR_ADC_REG0_BUSY_MASK, val),
341 static int meson_sar_adc_read_raw_sample(struct iio_dev *indio_dev,
342 const struct iio_chan_spec *chan,
345 struct meson_sar_adc_priv *priv = iio_priv(indio_dev);
346 struct device *dev = indio_dev->dev.parent;
347 int regval, fifo_chan, fifo_val, count;
349 if (!wait_for_completion_timeout(&priv->done,
350 msecs_to_jiffies(MESON_SAR_ADC_TIMEOUT)))
353 count = meson_sar_adc_get_fifo_count(indio_dev);
355 dev_err(dev, "ADC FIFO has %d element(s) instead of one\n", count);
359 regmap_read(priv->regmap, MESON_SAR_ADC_FIFO_RD, ®val);
360 fifo_chan = FIELD_GET(MESON_SAR_ADC_FIFO_RD_CHAN_ID_MASK, regval);
361 if (fifo_chan != chan->address) {
362 dev_err(dev, "ADC FIFO entry belongs to channel %d instead of %lu\n",
363 fifo_chan, chan->address);
367 fifo_val = FIELD_GET(MESON_SAR_ADC_FIFO_RD_SAMPLE_VALUE_MASK, regval);
368 fifo_val &= GENMASK(priv->param->resolution - 1, 0);
369 *val = meson_sar_adc_calib_val(indio_dev, fifo_val);
374 static void meson_sar_adc_set_averaging(struct iio_dev *indio_dev,
375 const struct iio_chan_spec *chan,
376 enum meson_sar_adc_avg_mode mode,
377 enum meson_sar_adc_num_samples samples)
379 struct meson_sar_adc_priv *priv = iio_priv(indio_dev);
380 int val, address = chan->address;
382 val = samples << MESON_SAR_ADC_AVG_CNTL_NUM_SAMPLES_SHIFT(address);
383 regmap_update_bits(priv->regmap, MESON_SAR_ADC_AVG_CNTL,
384 MESON_SAR_ADC_AVG_CNTL_NUM_SAMPLES_MASK(address),
387 val = mode << MESON_SAR_ADC_AVG_CNTL_AVG_MODE_SHIFT(address);
388 regmap_update_bits(priv->regmap, MESON_SAR_ADC_AVG_CNTL,
389 MESON_SAR_ADC_AVG_CNTL_AVG_MODE_MASK(address), val);
392 static void meson_sar_adc_enable_channel(struct iio_dev *indio_dev,
393 const struct iio_chan_spec *chan)
395 struct meson_sar_adc_priv *priv = iio_priv(indio_dev);
399 * the SAR ADC engine allows sampling multiple channels at the same
400 * time. to keep it simple we're only working with one *internal*
401 * channel, which starts counting at index 0 (which means: count = 1).
403 regval = FIELD_PREP(MESON_SAR_ADC_CHAN_LIST_MAX_INDEX_MASK, 0);
404 regmap_update_bits(priv->regmap, MESON_SAR_ADC_CHAN_LIST,
405 MESON_SAR_ADC_CHAN_LIST_MAX_INDEX_MASK, regval);
407 /* map channel index 0 to the channel which we want to read */
408 regval = FIELD_PREP(MESON_SAR_ADC_CHAN_LIST_ENTRY_MASK(0),
410 regmap_update_bits(priv->regmap, MESON_SAR_ADC_CHAN_LIST,
411 MESON_SAR_ADC_CHAN_LIST_ENTRY_MASK(0), regval);
413 regval = FIELD_PREP(MESON_SAR_ADC_DETECT_IDLE_SW_DETECT_MUX_MASK,
415 regmap_update_bits(priv->regmap, MESON_SAR_ADC_DETECT_IDLE_SW,
416 MESON_SAR_ADC_DETECT_IDLE_SW_DETECT_MUX_MASK,
419 regval = FIELD_PREP(MESON_SAR_ADC_DETECT_IDLE_SW_IDLE_MUX_SEL_MASK,
421 regmap_update_bits(priv->regmap, MESON_SAR_ADC_DETECT_IDLE_SW,
422 MESON_SAR_ADC_DETECT_IDLE_SW_IDLE_MUX_SEL_MASK,
425 if (chan->address == MESON_SAR_ADC_VOLTAGE_AND_TEMP_CHANNEL) {
426 if (chan->type == IIO_TEMP)
427 regval = MESON_SAR_ADC_DELTA_10_TEMP_SEL;
431 regmap_update_bits(priv->regmap,
432 MESON_SAR_ADC_DELTA_10,
433 MESON_SAR_ADC_DELTA_10_TEMP_SEL, regval);
437 static void meson_sar_adc_set_chan7_mux(struct iio_dev *indio_dev,
438 enum meson_sar_adc_chan7_mux_sel sel)
440 struct meson_sar_adc_priv *priv = iio_priv(indio_dev);
443 regval = FIELD_PREP(MESON_SAR_ADC_REG3_CTRL_CHAN7_MUX_SEL_MASK, sel);
444 regmap_update_bits(priv->regmap, MESON_SAR_ADC_REG3,
445 MESON_SAR_ADC_REG3_CTRL_CHAN7_MUX_SEL_MASK, regval);
447 usleep_range(10, 20);
450 static void meson_sar_adc_start_sample_engine(struct iio_dev *indio_dev)
452 struct meson_sar_adc_priv *priv = iio_priv(indio_dev);
454 reinit_completion(&priv->done);
456 regmap_update_bits(priv->regmap, MESON_SAR_ADC_REG0,
457 MESON_SAR_ADC_REG0_FIFO_IRQ_EN,
458 MESON_SAR_ADC_REG0_FIFO_IRQ_EN);
460 regmap_update_bits(priv->regmap, MESON_SAR_ADC_REG0,
461 MESON_SAR_ADC_REG0_SAMPLE_ENGINE_ENABLE,
462 MESON_SAR_ADC_REG0_SAMPLE_ENGINE_ENABLE);
464 regmap_update_bits(priv->regmap, MESON_SAR_ADC_REG0,
465 MESON_SAR_ADC_REG0_SAMPLING_START,
466 MESON_SAR_ADC_REG0_SAMPLING_START);
469 static void meson_sar_adc_stop_sample_engine(struct iio_dev *indio_dev)
471 struct meson_sar_adc_priv *priv = iio_priv(indio_dev);
473 regmap_update_bits(priv->regmap, MESON_SAR_ADC_REG0,
474 MESON_SAR_ADC_REG0_FIFO_IRQ_EN, 0);
476 regmap_update_bits(priv->regmap, MESON_SAR_ADC_REG0,
477 MESON_SAR_ADC_REG0_SAMPLING_STOP,
478 MESON_SAR_ADC_REG0_SAMPLING_STOP);
480 /* wait until all modules are stopped */
481 meson_sar_adc_wait_busy_clear(indio_dev);
483 regmap_update_bits(priv->regmap, MESON_SAR_ADC_REG0,
484 MESON_SAR_ADC_REG0_SAMPLE_ENGINE_ENABLE, 0);
487 static int meson_sar_adc_lock(struct iio_dev *indio_dev)
489 struct meson_sar_adc_priv *priv = iio_priv(indio_dev);
492 mutex_lock(&priv->lock);
494 if (priv->param->has_bl30_integration) {
495 /* prevent BL30 from using the SAR ADC while we are using it */
496 regmap_update_bits(priv->regmap, MESON_SAR_ADC_DELAY,
497 MESON_SAR_ADC_DELAY_KERNEL_BUSY,
498 MESON_SAR_ADC_DELAY_KERNEL_BUSY);
503 * wait until BL30 releases it's lock (so we can use the SAR
506 ret = regmap_read_poll_timeout_atomic(priv->regmap, MESON_SAR_ADC_DELAY, val,
507 !(val & MESON_SAR_ADC_DELAY_BL30_BUSY),
510 mutex_unlock(&priv->lock);
518 static void meson_sar_adc_unlock(struct iio_dev *indio_dev)
520 struct meson_sar_adc_priv *priv = iio_priv(indio_dev);
522 if (priv->param->has_bl30_integration)
523 /* allow BL30 to use the SAR ADC again */
524 regmap_update_bits(priv->regmap, MESON_SAR_ADC_DELAY,
525 MESON_SAR_ADC_DELAY_KERNEL_BUSY, 0);
527 mutex_unlock(&priv->lock);
530 static void meson_sar_adc_clear_fifo(struct iio_dev *indio_dev)
532 struct meson_sar_adc_priv *priv = iio_priv(indio_dev);
533 unsigned int count, tmp;
535 for (count = 0; count < MESON_SAR_ADC_MAX_FIFO_SIZE; count++) {
536 if (!meson_sar_adc_get_fifo_count(indio_dev))
539 regmap_read(priv->regmap, MESON_SAR_ADC_FIFO_RD, &tmp);
543 static int meson_sar_adc_get_sample(struct iio_dev *indio_dev,
544 const struct iio_chan_spec *chan,
545 enum meson_sar_adc_avg_mode avg_mode,
546 enum meson_sar_adc_num_samples avg_samples,
549 struct meson_sar_adc_priv *priv = iio_priv(indio_dev);
550 struct device *dev = indio_dev->dev.parent;
553 if (chan->type == IIO_TEMP && !priv->temperature_sensor_calibrated)
556 ret = meson_sar_adc_lock(indio_dev);
560 /* clear the FIFO to make sure we're not reading old values */
561 meson_sar_adc_clear_fifo(indio_dev);
563 meson_sar_adc_set_averaging(indio_dev, chan, avg_mode, avg_samples);
565 meson_sar_adc_enable_channel(indio_dev, chan);
567 meson_sar_adc_start_sample_engine(indio_dev);
568 ret = meson_sar_adc_read_raw_sample(indio_dev, chan, val);
569 meson_sar_adc_stop_sample_engine(indio_dev);
571 meson_sar_adc_unlock(indio_dev);
574 dev_warn(dev, "failed to read sample for channel %lu: %d\n",
582 static int meson_sar_adc_iio_info_read_raw(struct iio_dev *indio_dev,
583 const struct iio_chan_spec *chan,
584 int *val, int *val2, long mask)
586 struct meson_sar_adc_priv *priv = iio_priv(indio_dev);
587 struct device *dev = indio_dev->dev.parent;
591 case IIO_CHAN_INFO_RAW:
592 return meson_sar_adc_get_sample(indio_dev, chan, NO_AVERAGING,
595 case IIO_CHAN_INFO_AVERAGE_RAW:
596 return meson_sar_adc_get_sample(indio_dev, chan,
597 MEAN_AVERAGING, EIGHT_SAMPLES,
600 case IIO_CHAN_INFO_SCALE:
601 if (chan->type == IIO_VOLTAGE) {
602 ret = regulator_get_voltage(priv->vref);
604 dev_err(dev, "failed to get vref voltage: %d\n", ret);
609 *val2 = priv->param->resolution;
610 return IIO_VAL_FRACTIONAL_LOG2;
611 } else if (chan->type == IIO_TEMP) {
612 /* SoC specific multiplier and divider */
613 *val = priv->param->temperature_multiplier;
614 *val2 = priv->param->temperature_divider;
616 /* celsius to millicelsius */
619 return IIO_VAL_FRACTIONAL;
624 case IIO_CHAN_INFO_CALIBBIAS:
625 *val = priv->calibbias;
628 case IIO_CHAN_INFO_CALIBSCALE:
629 *val = priv->calibscale / MILLION;
630 *val2 = priv->calibscale % MILLION;
631 return IIO_VAL_INT_PLUS_MICRO;
633 case IIO_CHAN_INFO_OFFSET:
634 *val = DIV_ROUND_CLOSEST(MESON_SAR_ADC_TEMP_OFFSET *
635 priv->param->temperature_divider,
636 priv->param->temperature_multiplier);
637 *val -= priv->temperature_sensor_adc_val;
645 static int meson_sar_adc_clk_init(struct iio_dev *indio_dev,
648 struct meson_sar_adc_priv *priv = iio_priv(indio_dev);
649 struct device *dev = indio_dev->dev.parent;
650 struct clk_init_data init;
651 const char *clk_parents[1];
653 init.name = devm_kasprintf(dev, GFP_KERNEL, "%s#adc_div", dev_name(dev));
658 init.ops = &clk_divider_ops;
659 clk_parents[0] = __clk_get_name(priv->clkin);
660 init.parent_names = clk_parents;
661 init.num_parents = 1;
663 priv->clk_div.reg = base + MESON_SAR_ADC_REG3;
664 priv->clk_div.shift = MESON_SAR_ADC_REG3_ADC_CLK_DIV_SHIFT;
665 priv->clk_div.width = MESON_SAR_ADC_REG3_ADC_CLK_DIV_WIDTH;
666 priv->clk_div.hw.init = &init;
667 priv->clk_div.flags = 0;
669 priv->adc_div_clk = devm_clk_register(dev, &priv->clk_div.hw);
670 if (WARN_ON(IS_ERR(priv->adc_div_clk)))
671 return PTR_ERR(priv->adc_div_clk);
673 init.name = devm_kasprintf(dev, GFP_KERNEL, "%s#adc_en", dev_name(dev));
677 init.flags = CLK_SET_RATE_PARENT;
678 init.ops = &clk_gate_ops;
679 clk_parents[0] = __clk_get_name(priv->adc_div_clk);
680 init.parent_names = clk_parents;
681 init.num_parents = 1;
683 priv->clk_gate.reg = base + MESON_SAR_ADC_REG3;
684 priv->clk_gate.bit_idx = __ffs(MESON_SAR_ADC_REG3_CLK_EN);
685 priv->clk_gate.hw.init = &init;
687 priv->adc_clk = devm_clk_register(dev, &priv->clk_gate.hw);
688 if (WARN_ON(IS_ERR(priv->adc_clk)))
689 return PTR_ERR(priv->adc_clk);
694 static int meson_sar_adc_temp_sensor_init(struct iio_dev *indio_dev)
696 struct meson_sar_adc_priv *priv = iio_priv(indio_dev);
697 u8 *buf, trimming_bits, trimming_mask, upper_adc_val;
698 struct device *dev = indio_dev->dev.parent;
699 struct nvmem_cell *temperature_calib;
703 temperature_calib = devm_nvmem_cell_get(dev, "temperature_calib");
704 if (IS_ERR(temperature_calib)) {
705 ret = PTR_ERR(temperature_calib);
708 * leave the temperature sensor disabled if no calibration data
709 * was passed via nvmem-cells.
714 return dev_err_probe(dev, ret, "failed to get temperature_calib cell\n");
717 priv->tsc_regmap = syscon_regmap_lookup_by_phandle(dev->of_node, "amlogic,hhi-sysctrl");
718 if (IS_ERR(priv->tsc_regmap))
719 return dev_err_probe(dev, PTR_ERR(priv->tsc_regmap),
720 "failed to get amlogic,hhi-sysctrl regmap\n");
722 read_len = MESON_SAR_ADC_EFUSE_BYTES;
723 buf = nvmem_cell_read(temperature_calib, &read_len);
725 return dev_err_probe(dev, PTR_ERR(buf), "failed to read temperature_calib cell\n");
726 if (read_len != MESON_SAR_ADC_EFUSE_BYTES) {
728 return dev_err_probe(dev, -EINVAL, "invalid read size of temperature_calib cell\n");
731 trimming_bits = priv->param->temperature_trimming_bits;
732 trimming_mask = BIT(trimming_bits) - 1;
734 priv->temperature_sensor_calibrated =
735 buf[3] & MESON_SAR_ADC_EFUSE_BYTE3_IS_CALIBRATED;
736 priv->temperature_sensor_coefficient = buf[2] & trimming_mask;
738 upper_adc_val = FIELD_GET(MESON_SAR_ADC_EFUSE_BYTE3_UPPER_ADC_VAL,
741 priv->temperature_sensor_adc_val = buf[2];
742 priv->temperature_sensor_adc_val |= upper_adc_val << BITS_PER_BYTE;
743 priv->temperature_sensor_adc_val >>= trimming_bits;
750 static int meson_sar_adc_init(struct iio_dev *indio_dev)
752 struct meson_sar_adc_priv *priv = iio_priv(indio_dev);
753 struct device *dev = indio_dev->dev.parent;
757 * make sure we start at CH7 input since the other muxes are only used
758 * for internal calibration.
760 meson_sar_adc_set_chan7_mux(indio_dev, CHAN7_MUX_CH7_INPUT);
762 if (priv->param->has_bl30_integration) {
764 * leave sampling delay and the input clocks as configured by
765 * BL30 to make sure BL30 gets the values it expects when
766 * reading the temperature sensor.
768 regmap_read(priv->regmap, MESON_SAR_ADC_REG3, ®val);
769 if (regval & MESON_SAR_ADC_REG3_BL30_INITIALIZED)
773 meson_sar_adc_stop_sample_engine(indio_dev);
776 * disable this bit as seems to be only relevant for Meson6 (based
777 * on the vendor driver), which we don't support at the moment.
779 regmap_update_bits(priv->regmap, MESON_SAR_ADC_REG0,
780 MESON_SAR_ADC_REG0_ADC_TEMP_SEN_SEL, 0);
782 /* disable all channels by default */
783 regmap_write(priv->regmap, MESON_SAR_ADC_CHAN_LIST, 0x0);
785 regmap_update_bits(priv->regmap, MESON_SAR_ADC_REG3,
786 MESON_SAR_ADC_REG3_CTRL_SAMPLING_CLOCK_PHASE, 0);
787 regmap_update_bits(priv->regmap, MESON_SAR_ADC_REG3,
788 MESON_SAR_ADC_REG3_CNTL_USE_SC_DLY,
789 MESON_SAR_ADC_REG3_CNTL_USE_SC_DLY);
791 /* delay between two samples = (10+1) * 1uS */
792 regmap_update_bits(priv->regmap, MESON_SAR_ADC_DELAY,
793 MESON_SAR_ADC_DELAY_INPUT_DLY_CNT_MASK,
794 FIELD_PREP(MESON_SAR_ADC_DELAY_SAMPLE_DLY_CNT_MASK,
796 regmap_update_bits(priv->regmap, MESON_SAR_ADC_DELAY,
797 MESON_SAR_ADC_DELAY_SAMPLE_DLY_SEL_MASK,
798 FIELD_PREP(MESON_SAR_ADC_DELAY_SAMPLE_DLY_SEL_MASK,
801 /* delay between two samples = (10+1) * 1uS */
802 regmap_update_bits(priv->regmap, MESON_SAR_ADC_DELAY,
803 MESON_SAR_ADC_DELAY_INPUT_DLY_CNT_MASK,
804 FIELD_PREP(MESON_SAR_ADC_DELAY_INPUT_DLY_CNT_MASK,
806 regmap_update_bits(priv->regmap, MESON_SAR_ADC_DELAY,
807 MESON_SAR_ADC_DELAY_INPUT_DLY_SEL_MASK,
808 FIELD_PREP(MESON_SAR_ADC_DELAY_INPUT_DLY_SEL_MASK,
812 * set up the input channel muxes in MESON_SAR_ADC_CHAN_10_SW
813 * (0 = SAR_ADC_CH0, 1 = SAR_ADC_CH1)
815 regval = FIELD_PREP(MESON_SAR_ADC_CHAN_10_SW_CHAN0_MUX_SEL_MASK, 0);
816 regmap_update_bits(priv->regmap, MESON_SAR_ADC_CHAN_10_SW,
817 MESON_SAR_ADC_CHAN_10_SW_CHAN0_MUX_SEL_MASK,
819 regval = FIELD_PREP(MESON_SAR_ADC_CHAN_10_SW_CHAN1_MUX_SEL_MASK, 1);
820 regmap_update_bits(priv->regmap, MESON_SAR_ADC_CHAN_10_SW,
821 MESON_SAR_ADC_CHAN_10_SW_CHAN1_MUX_SEL_MASK,
825 * set up the input channel muxes in MESON_SAR_ADC_AUX_SW
826 * (2 = SAR_ADC_CH2, 3 = SAR_ADC_CH3, ...) and enable
827 * MESON_SAR_ADC_AUX_SW_YP_DRIVE_SW and
828 * MESON_SAR_ADC_AUX_SW_XP_DRIVE_SW like the vendor driver.
831 for (i = 2; i <= 7; i++)
832 regval |= i << MESON_SAR_ADC_AUX_SW_MUX_SEL_CHAN_SHIFT(i);
833 regval |= MESON_SAR_ADC_AUX_SW_YP_DRIVE_SW;
834 regval |= MESON_SAR_ADC_AUX_SW_XP_DRIVE_SW;
835 regmap_write(priv->regmap, MESON_SAR_ADC_AUX_SW, regval);
837 if (priv->temperature_sensor_calibrated) {
838 regmap_update_bits(priv->regmap, MESON_SAR_ADC_DELTA_10,
839 MESON_SAR_ADC_DELTA_10_TS_REVE1,
840 MESON_SAR_ADC_DELTA_10_TS_REVE1);
841 regmap_update_bits(priv->regmap, MESON_SAR_ADC_DELTA_10,
842 MESON_SAR_ADC_DELTA_10_TS_REVE0,
843 MESON_SAR_ADC_DELTA_10_TS_REVE0);
846 * set bits [3:0] of the TSC (temperature sensor coefficient)
847 * to get the correct values when reading the temperature.
849 regval = FIELD_PREP(MESON_SAR_ADC_DELTA_10_TS_C_MASK,
850 priv->temperature_sensor_coefficient);
851 regmap_update_bits(priv->regmap, MESON_SAR_ADC_DELTA_10,
852 MESON_SAR_ADC_DELTA_10_TS_C_MASK, regval);
854 if (priv->param->temperature_trimming_bits == 5) {
855 if (priv->temperature_sensor_coefficient & BIT(4))
856 regval = MESON_HHI_DPLL_TOP_0_TSC_BIT4;
861 * bit [4] (the 5th bit when starting to count at 1)
862 * of the TSC is located in the HHI register area.
864 regmap_update_bits(priv->tsc_regmap,
865 MESON_HHI_DPLL_TOP_0,
866 MESON_HHI_DPLL_TOP_0_TSC_BIT4,
870 regmap_update_bits(priv->regmap, MESON_SAR_ADC_DELTA_10,
871 MESON_SAR_ADC_DELTA_10_TS_REVE1, 0);
872 regmap_update_bits(priv->regmap, MESON_SAR_ADC_DELTA_10,
873 MESON_SAR_ADC_DELTA_10_TS_REVE0, 0);
876 ret = clk_set_parent(priv->adc_sel_clk, priv->clkin);
878 return dev_err_probe(dev, ret, "failed to set adc parent to clkin\n");
880 ret = clk_set_rate(priv->adc_clk, priv->param->clock_rate);
882 return dev_err_probe(dev, ret, "failed to set adc clock rate\n");
887 static void meson_sar_adc_set_bandgap(struct iio_dev *indio_dev, bool on_off)
889 struct meson_sar_adc_priv *priv = iio_priv(indio_dev);
890 const struct meson_sar_adc_param *param = priv->param;
893 if (param->bandgap_reg == MESON_SAR_ADC_REG11)
894 enable_mask = MESON_SAR_ADC_REG11_BANDGAP_EN;
896 enable_mask = MESON_SAR_ADC_DELTA_10_TS_VBG_EN;
898 regmap_update_bits(priv->regmap, param->bandgap_reg, enable_mask,
899 on_off ? enable_mask : 0);
902 static int meson_sar_adc_hw_enable(struct iio_dev *indio_dev)
904 struct meson_sar_adc_priv *priv = iio_priv(indio_dev);
905 struct device *dev = indio_dev->dev.parent;
909 ret = meson_sar_adc_lock(indio_dev);
913 ret = regulator_enable(priv->vref);
915 dev_err(dev, "failed to enable vref regulator\n");
919 ret = clk_prepare_enable(priv->core_clk);
921 dev_err(dev, "failed to enable core clk\n");
925 regval = FIELD_PREP(MESON_SAR_ADC_REG0_FIFO_CNT_IRQ_MASK, 1);
926 regmap_update_bits(priv->regmap, MESON_SAR_ADC_REG0,
927 MESON_SAR_ADC_REG0_FIFO_CNT_IRQ_MASK, regval);
929 meson_sar_adc_set_bandgap(indio_dev, true);
931 regmap_update_bits(priv->regmap, MESON_SAR_ADC_REG3,
932 MESON_SAR_ADC_REG3_ADC_EN,
933 MESON_SAR_ADC_REG3_ADC_EN);
937 ret = clk_prepare_enable(priv->adc_clk);
939 dev_err(dev, "failed to enable adc clk\n");
943 meson_sar_adc_unlock(indio_dev);
948 regmap_update_bits(priv->regmap, MESON_SAR_ADC_REG3,
949 MESON_SAR_ADC_REG3_ADC_EN, 0);
950 meson_sar_adc_set_bandgap(indio_dev, false);
951 clk_disable_unprepare(priv->core_clk);
953 regulator_disable(priv->vref);
955 meson_sar_adc_unlock(indio_dev);
960 static void meson_sar_adc_hw_disable(struct iio_dev *indio_dev)
962 struct meson_sar_adc_priv *priv = iio_priv(indio_dev);
966 * If taking the lock fails we have to assume that BL30 is broken. The
967 * best we can do then is to release the resources anyhow.
969 ret = meson_sar_adc_lock(indio_dev);
971 dev_err(indio_dev->dev.parent, "Failed to lock ADC (%pE)\n", ERR_PTR(ret));
973 clk_disable_unprepare(priv->adc_clk);
975 regmap_update_bits(priv->regmap, MESON_SAR_ADC_REG3,
976 MESON_SAR_ADC_REG3_ADC_EN, 0);
978 meson_sar_adc_set_bandgap(indio_dev, false);
980 clk_disable_unprepare(priv->core_clk);
982 regulator_disable(priv->vref);
985 meson_sar_adc_unlock(indio_dev);
988 static irqreturn_t meson_sar_adc_irq(int irq, void *data)
990 struct iio_dev *indio_dev = data;
991 struct meson_sar_adc_priv *priv = iio_priv(indio_dev);
992 unsigned int cnt, threshold;
995 regmap_read(priv->regmap, MESON_SAR_ADC_REG0, ®val);
996 cnt = FIELD_GET(MESON_SAR_ADC_REG0_FIFO_COUNT_MASK, regval);
997 threshold = FIELD_GET(MESON_SAR_ADC_REG0_FIFO_CNT_IRQ_MASK, regval);
1002 complete(&priv->done);
1007 static int meson_sar_adc_calib(struct iio_dev *indio_dev)
1009 struct meson_sar_adc_priv *priv = iio_priv(indio_dev);
1010 int ret, nominal0, nominal1, value0, value1;
1012 /* use points 25% and 75% for calibration */
1013 nominal0 = (1 << priv->param->resolution) / 4;
1014 nominal1 = (1 << priv->param->resolution) * 3 / 4;
1016 meson_sar_adc_set_chan7_mux(indio_dev, CHAN7_MUX_VDD_DIV4);
1017 usleep_range(10, 20);
1018 ret = meson_sar_adc_get_sample(indio_dev,
1019 &indio_dev->channels[7],
1020 MEAN_AVERAGING, EIGHT_SAMPLES, &value0);
1024 meson_sar_adc_set_chan7_mux(indio_dev, CHAN7_MUX_VDD_MUL3_DIV4);
1025 usleep_range(10, 20);
1026 ret = meson_sar_adc_get_sample(indio_dev,
1027 &indio_dev->channels[7],
1028 MEAN_AVERAGING, EIGHT_SAMPLES, &value1);
1032 if (value1 <= value0) {
1037 priv->calibscale = div_s64((nominal1 - nominal0) * (s64)MILLION,
1039 priv->calibbias = nominal0 - div_s64((s64)value0 * priv->calibscale,
1043 meson_sar_adc_set_chan7_mux(indio_dev, CHAN7_MUX_CH7_INPUT);
1048 static const struct iio_info meson_sar_adc_iio_info = {
1049 .read_raw = meson_sar_adc_iio_info_read_raw,
1052 static const struct meson_sar_adc_param meson_sar_adc_meson8_param = {
1053 .has_bl30_integration = false,
1054 .clock_rate = 1150000,
1055 .bandgap_reg = MESON_SAR_ADC_DELTA_10,
1056 .regmap_config = &meson_sar_adc_regmap_config_meson8,
1058 .temperature_trimming_bits = 4,
1059 .temperature_multiplier = 18 * 10000,
1060 .temperature_divider = 1024 * 10 * 85,
1063 static const struct meson_sar_adc_param meson_sar_adc_meson8b_param = {
1064 .has_bl30_integration = false,
1065 .clock_rate = 1150000,
1066 .bandgap_reg = MESON_SAR_ADC_DELTA_10,
1067 .regmap_config = &meson_sar_adc_regmap_config_meson8,
1069 .temperature_trimming_bits = 5,
1070 .temperature_multiplier = 10,
1071 .temperature_divider = 32,
1074 static const struct meson_sar_adc_param meson_sar_adc_gxbb_param = {
1075 .has_bl30_integration = true,
1076 .clock_rate = 1200000,
1077 .bandgap_reg = MESON_SAR_ADC_REG11,
1078 .regmap_config = &meson_sar_adc_regmap_config_gxbb,
1082 static const struct meson_sar_adc_param meson_sar_adc_gxl_param = {
1083 .has_bl30_integration = true,
1084 .clock_rate = 1200000,
1085 .bandgap_reg = MESON_SAR_ADC_REG11,
1086 .regmap_config = &meson_sar_adc_regmap_config_gxbb,
1090 static const struct meson_sar_adc_param meson_sar_adc_g12a_param = {
1091 .has_bl30_integration = false,
1092 .clock_rate = 1200000,
1093 .bandgap_reg = MESON_SAR_ADC_REG11,
1094 .regmap_config = &meson_sar_adc_regmap_config_gxbb,
1098 static const struct meson_sar_adc_data meson_sar_adc_meson8_data = {
1099 .param = &meson_sar_adc_meson8_param,
1100 .name = "meson-meson8-saradc",
1103 static const struct meson_sar_adc_data meson_sar_adc_meson8b_data = {
1104 .param = &meson_sar_adc_meson8b_param,
1105 .name = "meson-meson8b-saradc",
1108 static const struct meson_sar_adc_data meson_sar_adc_meson8m2_data = {
1109 .param = &meson_sar_adc_meson8b_param,
1110 .name = "meson-meson8m2-saradc",
1113 static const struct meson_sar_adc_data meson_sar_adc_gxbb_data = {
1114 .param = &meson_sar_adc_gxbb_param,
1115 .name = "meson-gxbb-saradc",
1118 static const struct meson_sar_adc_data meson_sar_adc_gxl_data = {
1119 .param = &meson_sar_adc_gxl_param,
1120 .name = "meson-gxl-saradc",
1123 static const struct meson_sar_adc_data meson_sar_adc_gxm_data = {
1124 .param = &meson_sar_adc_gxl_param,
1125 .name = "meson-gxm-saradc",
1128 static const struct meson_sar_adc_data meson_sar_adc_axg_data = {
1129 .param = &meson_sar_adc_gxl_param,
1130 .name = "meson-axg-saradc",
1133 static const struct meson_sar_adc_data meson_sar_adc_g12a_data = {
1134 .param = &meson_sar_adc_g12a_param,
1135 .name = "meson-g12a-saradc",
1138 static const struct of_device_id meson_sar_adc_of_match[] = {
1140 .compatible = "amlogic,meson8-saradc",
1141 .data = &meson_sar_adc_meson8_data,
1143 .compatible = "amlogic,meson8b-saradc",
1144 .data = &meson_sar_adc_meson8b_data,
1146 .compatible = "amlogic,meson8m2-saradc",
1147 .data = &meson_sar_adc_meson8m2_data,
1149 .compatible = "amlogic,meson-gxbb-saradc",
1150 .data = &meson_sar_adc_gxbb_data,
1152 .compatible = "amlogic,meson-gxl-saradc",
1153 .data = &meson_sar_adc_gxl_data,
1155 .compatible = "amlogic,meson-gxm-saradc",
1156 .data = &meson_sar_adc_gxm_data,
1158 .compatible = "amlogic,meson-axg-saradc",
1159 .data = &meson_sar_adc_axg_data,
1161 .compatible = "amlogic,meson-g12a-saradc",
1162 .data = &meson_sar_adc_g12a_data,
1166 MODULE_DEVICE_TABLE(of, meson_sar_adc_of_match);
1168 static int meson_sar_adc_probe(struct platform_device *pdev)
1170 const struct meson_sar_adc_data *match_data;
1171 struct meson_sar_adc_priv *priv;
1172 struct device *dev = &pdev->dev;
1173 struct iio_dev *indio_dev;
1177 indio_dev = devm_iio_device_alloc(dev, sizeof(*priv));
1179 return dev_err_probe(dev, -ENOMEM, "failed allocating iio device\n");
1181 priv = iio_priv(indio_dev);
1182 init_completion(&priv->done);
1184 match_data = of_device_get_match_data(dev);
1186 return dev_err_probe(dev, -ENODEV, "failed to get match data\n");
1188 priv->param = match_data->param;
1190 indio_dev->name = match_data->name;
1191 indio_dev->modes = INDIO_DIRECT_MODE;
1192 indio_dev->info = &meson_sar_adc_iio_info;
1194 base = devm_platform_ioremap_resource(pdev, 0);
1196 return PTR_ERR(base);
1198 priv->regmap = devm_regmap_init_mmio(dev, base, priv->param->regmap_config);
1199 if (IS_ERR(priv->regmap))
1200 return PTR_ERR(priv->regmap);
1202 irq = irq_of_parse_and_map(dev->of_node, 0);
1206 ret = devm_request_irq(dev, irq, meson_sar_adc_irq, IRQF_SHARED, dev_name(dev), indio_dev);
1210 priv->clkin = devm_clk_get(dev, "clkin");
1211 if (IS_ERR(priv->clkin))
1212 return dev_err_probe(dev, PTR_ERR(priv->clkin), "failed to get clkin\n");
1214 priv->core_clk = devm_clk_get(dev, "core");
1215 if (IS_ERR(priv->core_clk))
1216 return dev_err_probe(dev, PTR_ERR(priv->core_clk), "failed to get core clk\n");
1218 priv->adc_clk = devm_clk_get_optional(dev, "adc_clk");
1219 if (IS_ERR(priv->adc_clk))
1220 return dev_err_probe(dev, PTR_ERR(priv->adc_clk), "failed to get adc clk\n");
1222 priv->adc_sel_clk = devm_clk_get_optional(dev, "adc_sel");
1223 if (IS_ERR(priv->adc_sel_clk))
1224 return dev_err_probe(dev, PTR_ERR(priv->adc_sel_clk), "failed to get adc_sel clk\n");
1226 /* on pre-GXBB SoCs the SAR ADC itself provides the ADC clock: */
1227 if (!priv->adc_clk) {
1228 ret = meson_sar_adc_clk_init(indio_dev, base);
1233 priv->vref = devm_regulator_get(dev, "vref");
1234 if (IS_ERR(priv->vref))
1235 return dev_err_probe(dev, PTR_ERR(priv->vref), "failed to get vref regulator\n");
1237 priv->calibscale = MILLION;
1239 if (priv->param->temperature_trimming_bits) {
1240 ret = meson_sar_adc_temp_sensor_init(indio_dev);
1245 if (priv->temperature_sensor_calibrated) {
1246 indio_dev->channels = meson_sar_adc_and_temp_iio_channels;
1247 indio_dev->num_channels =
1248 ARRAY_SIZE(meson_sar_adc_and_temp_iio_channels);
1250 indio_dev->channels = meson_sar_adc_iio_channels;
1251 indio_dev->num_channels =
1252 ARRAY_SIZE(meson_sar_adc_iio_channels);
1255 ret = meson_sar_adc_init(indio_dev);
1259 mutex_init(&priv->lock);
1261 ret = meson_sar_adc_hw_enable(indio_dev);
1265 ret = meson_sar_adc_calib(indio_dev);
1267 dev_warn(dev, "calibration failed\n");
1269 platform_set_drvdata(pdev, indio_dev);
1271 ret = iio_device_register(indio_dev);
1278 meson_sar_adc_hw_disable(indio_dev);
1283 static int meson_sar_adc_remove(struct platform_device *pdev)
1285 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
1287 iio_device_unregister(indio_dev);
1289 meson_sar_adc_hw_disable(indio_dev);
1294 static int meson_sar_adc_suspend(struct device *dev)
1296 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1298 meson_sar_adc_hw_disable(indio_dev);
1303 static int meson_sar_adc_resume(struct device *dev)
1305 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1307 return meson_sar_adc_hw_enable(indio_dev);
1310 static DEFINE_SIMPLE_DEV_PM_OPS(meson_sar_adc_pm_ops,
1311 meson_sar_adc_suspend, meson_sar_adc_resume);
1313 static struct platform_driver meson_sar_adc_driver = {
1314 .probe = meson_sar_adc_probe,
1315 .remove = meson_sar_adc_remove,
1317 .name = "meson-saradc",
1318 .of_match_table = meson_sar_adc_of_match,
1319 .pm = pm_sleep_ptr(&meson_sar_adc_pm_ops),
1323 module_platform_driver(meson_sar_adc_driver);
1325 MODULE_AUTHOR("Martin Blumenstingl <martin.blumenstingl@googlemail.com>");
1326 MODULE_DESCRIPTION("Amlogic Meson SAR ADC driver");
1327 MODULE_LICENSE("GPL v2");