Merge tag 'regmap-fix-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux/fpc-iii.git] / drivers / iio / adc / meson_saradc.c
blob66dc452d643abd8a5319c7b20d5078d3bf320e1e
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Amlogic Meson Successive Approximation Register (SAR) A/D Converter
5 * Copyright (C) 2017 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
6 */
8 #include <linux/bitfield.h>
9 #include <linux/clk.h>
10 #include <linux/clk-provider.h>
11 #include <linux/delay.h>
12 #include <linux/io.h>
13 #include <linux/iio/iio.h>
14 #include <linux/module.h>
15 #include <linux/nvmem-consumer.h>
16 #include <linux/interrupt.h>
17 #include <linux/of.h>
18 #include <linux/of_irq.h>
19 #include <linux/of_device.h>
20 #include <linux/platform_device.h>
21 #include <linux/regmap.h>
22 #include <linux/regulator/consumer.h>
23 #include <linux/mfd/syscon.h>
25 #define MESON_SAR_ADC_REG0 0x00
26 #define MESON_SAR_ADC_REG0_PANEL_DETECT BIT(31)
27 #define MESON_SAR_ADC_REG0_BUSY_MASK GENMASK(30, 28)
28 #define MESON_SAR_ADC_REG0_DELTA_BUSY BIT(30)
29 #define MESON_SAR_ADC_REG0_AVG_BUSY BIT(29)
30 #define MESON_SAR_ADC_REG0_SAMPLE_BUSY BIT(28)
31 #define MESON_SAR_ADC_REG0_FIFO_FULL BIT(27)
32 #define MESON_SAR_ADC_REG0_FIFO_EMPTY BIT(26)
33 #define MESON_SAR_ADC_REG0_FIFO_COUNT_MASK GENMASK(25, 21)
34 #define MESON_SAR_ADC_REG0_ADC_BIAS_CTRL_MASK GENMASK(20, 19)
35 #define MESON_SAR_ADC_REG0_CURR_CHAN_ID_MASK GENMASK(18, 16)
36 #define MESON_SAR_ADC_REG0_ADC_TEMP_SEN_SEL BIT(15)
37 #define MESON_SAR_ADC_REG0_SAMPLING_STOP BIT(14)
38 #define MESON_SAR_ADC_REG0_CHAN_DELTA_EN_MASK GENMASK(13, 12)
39 #define MESON_SAR_ADC_REG0_DETECT_IRQ_POL BIT(10)
40 #define MESON_SAR_ADC_REG0_DETECT_IRQ_EN BIT(9)
41 #define MESON_SAR_ADC_REG0_FIFO_CNT_IRQ_MASK GENMASK(8, 4)
42 #define MESON_SAR_ADC_REG0_FIFO_IRQ_EN BIT(3)
43 #define MESON_SAR_ADC_REG0_SAMPLING_START BIT(2)
44 #define MESON_SAR_ADC_REG0_CONTINUOUS_EN BIT(1)
45 #define MESON_SAR_ADC_REG0_SAMPLE_ENGINE_ENABLE BIT(0)
47 #define MESON_SAR_ADC_CHAN_LIST 0x04
48 #define MESON_SAR_ADC_CHAN_LIST_MAX_INDEX_MASK GENMASK(26, 24)
49 #define MESON_SAR_ADC_CHAN_LIST_ENTRY_MASK(_chan) \
50 (GENMASK(2, 0) << ((_chan) * 3))
52 #define MESON_SAR_ADC_AVG_CNTL 0x08
53 #define MESON_SAR_ADC_AVG_CNTL_AVG_MODE_SHIFT(_chan) \
54 (16 + ((_chan) * 2))
55 #define MESON_SAR_ADC_AVG_CNTL_AVG_MODE_MASK(_chan) \
56 (GENMASK(17, 16) << ((_chan) * 2))
57 #define MESON_SAR_ADC_AVG_CNTL_NUM_SAMPLES_SHIFT(_chan) \
58 (0 + ((_chan) * 2))
59 #define MESON_SAR_ADC_AVG_CNTL_NUM_SAMPLES_MASK(_chan) \
60 (GENMASK(1, 0) << ((_chan) * 2))
62 #define MESON_SAR_ADC_REG3 0x0c
63 #define MESON_SAR_ADC_REG3_CNTL_USE_SC_DLY BIT(31)
64 #define MESON_SAR_ADC_REG3_CLK_EN BIT(30)
65 #define MESON_SAR_ADC_REG3_BL30_INITIALIZED BIT(28)
66 #define MESON_SAR_ADC_REG3_CTRL_CONT_RING_COUNTER_EN BIT(27)
67 #define MESON_SAR_ADC_REG3_CTRL_SAMPLING_CLOCK_PHASE BIT(26)
68 #define MESON_SAR_ADC_REG3_CTRL_CHAN7_MUX_SEL_MASK GENMASK(25, 23)
69 #define MESON_SAR_ADC_REG3_DETECT_EN BIT(22)
70 #define MESON_SAR_ADC_REG3_ADC_EN BIT(21)
71 #define MESON_SAR_ADC_REG3_PANEL_DETECT_COUNT_MASK GENMASK(20, 18)
72 #define MESON_SAR_ADC_REG3_PANEL_DETECT_FILTER_TB_MASK GENMASK(17, 16)
73 #define MESON_SAR_ADC_REG3_ADC_CLK_DIV_SHIFT 10
74 #define MESON_SAR_ADC_REG3_ADC_CLK_DIV_WIDTH 5
75 #define MESON_SAR_ADC_REG3_BLOCK_DLY_SEL_MASK GENMASK(9, 8)
76 #define MESON_SAR_ADC_REG3_BLOCK_DLY_MASK GENMASK(7, 0)
78 #define MESON_SAR_ADC_DELAY 0x10
79 #define MESON_SAR_ADC_DELAY_INPUT_DLY_SEL_MASK GENMASK(25, 24)
80 #define MESON_SAR_ADC_DELAY_BL30_BUSY BIT(15)
81 #define MESON_SAR_ADC_DELAY_KERNEL_BUSY BIT(14)
82 #define MESON_SAR_ADC_DELAY_INPUT_DLY_CNT_MASK GENMASK(23, 16)
83 #define MESON_SAR_ADC_DELAY_SAMPLE_DLY_SEL_MASK GENMASK(9, 8)
84 #define MESON_SAR_ADC_DELAY_SAMPLE_DLY_CNT_MASK GENMASK(7, 0)
86 #define MESON_SAR_ADC_LAST_RD 0x14
87 #define MESON_SAR_ADC_LAST_RD_LAST_CHANNEL1_MASK GENMASK(23, 16)
88 #define MESON_SAR_ADC_LAST_RD_LAST_CHANNEL0_MASK GENMASK(9, 0)
90 #define MESON_SAR_ADC_FIFO_RD 0x18
91 #define MESON_SAR_ADC_FIFO_RD_CHAN_ID_MASK GENMASK(14, 12)
92 #define MESON_SAR_ADC_FIFO_RD_SAMPLE_VALUE_MASK GENMASK(11, 0)
94 #define MESON_SAR_ADC_AUX_SW 0x1c
95 #define MESON_SAR_ADC_AUX_SW_MUX_SEL_CHAN_SHIFT(_chan) \
96 (8 + (((_chan) - 2) * 3))
97 #define MESON_SAR_ADC_AUX_SW_VREF_P_MUX BIT(6)
98 #define MESON_SAR_ADC_AUX_SW_VREF_N_MUX BIT(5)
99 #define MESON_SAR_ADC_AUX_SW_MODE_SEL BIT(4)
100 #define MESON_SAR_ADC_AUX_SW_YP_DRIVE_SW BIT(3)
101 #define MESON_SAR_ADC_AUX_SW_XP_DRIVE_SW BIT(2)
102 #define MESON_SAR_ADC_AUX_SW_YM_DRIVE_SW BIT(1)
103 #define MESON_SAR_ADC_AUX_SW_XM_DRIVE_SW BIT(0)
105 #define MESON_SAR_ADC_CHAN_10_SW 0x20
106 #define MESON_SAR_ADC_CHAN_10_SW_CHAN1_MUX_SEL_MASK GENMASK(25, 23)
107 #define MESON_SAR_ADC_CHAN_10_SW_CHAN1_VREF_P_MUX BIT(22)
108 #define MESON_SAR_ADC_CHAN_10_SW_CHAN1_VREF_N_MUX BIT(21)
109 #define MESON_SAR_ADC_CHAN_10_SW_CHAN1_MODE_SEL BIT(20)
110 #define MESON_SAR_ADC_CHAN_10_SW_CHAN1_YP_DRIVE_SW BIT(19)
111 #define MESON_SAR_ADC_CHAN_10_SW_CHAN1_XP_DRIVE_SW BIT(18)
112 #define MESON_SAR_ADC_CHAN_10_SW_CHAN1_YM_DRIVE_SW BIT(17)
113 #define MESON_SAR_ADC_CHAN_10_SW_CHAN1_XM_DRIVE_SW BIT(16)
114 #define MESON_SAR_ADC_CHAN_10_SW_CHAN0_MUX_SEL_MASK GENMASK(9, 7)
115 #define MESON_SAR_ADC_CHAN_10_SW_CHAN0_VREF_P_MUX BIT(6)
116 #define MESON_SAR_ADC_CHAN_10_SW_CHAN0_VREF_N_MUX BIT(5)
117 #define MESON_SAR_ADC_CHAN_10_SW_CHAN0_MODE_SEL BIT(4)
118 #define MESON_SAR_ADC_CHAN_10_SW_CHAN0_YP_DRIVE_SW BIT(3)
119 #define MESON_SAR_ADC_CHAN_10_SW_CHAN0_XP_DRIVE_SW BIT(2)
120 #define MESON_SAR_ADC_CHAN_10_SW_CHAN0_YM_DRIVE_SW BIT(1)
121 #define MESON_SAR_ADC_CHAN_10_SW_CHAN0_XM_DRIVE_SW BIT(0)
123 #define MESON_SAR_ADC_DETECT_IDLE_SW 0x24
124 #define MESON_SAR_ADC_DETECT_IDLE_SW_DETECT_SW_EN BIT(26)
125 #define MESON_SAR_ADC_DETECT_IDLE_SW_DETECT_MUX_MASK GENMASK(25, 23)
126 #define MESON_SAR_ADC_DETECT_IDLE_SW_DETECT_VREF_P_MUX BIT(22)
127 #define MESON_SAR_ADC_DETECT_IDLE_SW_DETECT_VREF_N_MUX BIT(21)
128 #define MESON_SAR_ADC_DETECT_IDLE_SW_DETECT_MODE_SEL BIT(20)
129 #define MESON_SAR_ADC_DETECT_IDLE_SW_DETECT_YP_DRIVE_SW BIT(19)
130 #define MESON_SAR_ADC_DETECT_IDLE_SW_DETECT_XP_DRIVE_SW BIT(18)
131 #define MESON_SAR_ADC_DETECT_IDLE_SW_DETECT_YM_DRIVE_SW BIT(17)
132 #define MESON_SAR_ADC_DETECT_IDLE_SW_DETECT_XM_DRIVE_SW BIT(16)
133 #define MESON_SAR_ADC_DETECT_IDLE_SW_IDLE_MUX_SEL_MASK GENMASK(9, 7)
134 #define MESON_SAR_ADC_DETECT_IDLE_SW_IDLE_VREF_P_MUX BIT(6)
135 #define MESON_SAR_ADC_DETECT_IDLE_SW_IDLE_VREF_N_MUX BIT(5)
136 #define MESON_SAR_ADC_DETECT_IDLE_SW_IDLE_MODE_SEL BIT(4)
137 #define MESON_SAR_ADC_DETECT_IDLE_SW_IDLE_YP_DRIVE_SW BIT(3)
138 #define MESON_SAR_ADC_DETECT_IDLE_SW_IDLE_XP_DRIVE_SW BIT(2)
139 #define MESON_SAR_ADC_DETECT_IDLE_SW_IDLE_YM_DRIVE_SW BIT(1)
140 #define MESON_SAR_ADC_DETECT_IDLE_SW_IDLE_XM_DRIVE_SW BIT(0)
142 #define MESON_SAR_ADC_DELTA_10 0x28
143 #define MESON_SAR_ADC_DELTA_10_TEMP_SEL BIT(27)
144 #define MESON_SAR_ADC_DELTA_10_TS_REVE1 BIT(26)
145 #define MESON_SAR_ADC_DELTA_10_CHAN1_DELTA_VALUE_MASK GENMASK(25, 16)
146 #define MESON_SAR_ADC_DELTA_10_TS_REVE0 BIT(15)
147 #define MESON_SAR_ADC_DELTA_10_TS_C_MASK GENMASK(14, 11)
148 #define MESON_SAR_ADC_DELTA_10_TS_VBG_EN BIT(10)
149 #define MESON_SAR_ADC_DELTA_10_CHAN0_DELTA_VALUE_MASK GENMASK(9, 0)
152 * NOTE: registers from here are undocumented (the vendor Linux kernel driver
153 * and u-boot source served as reference). These only seem to be relevant on
154 * GXBB and newer.
156 #define MESON_SAR_ADC_REG11 0x2c
157 #define MESON_SAR_ADC_REG11_BANDGAP_EN BIT(13)
159 #define MESON_SAR_ADC_REG13 0x34
160 #define MESON_SAR_ADC_REG13_12BIT_CALIBRATION_MASK GENMASK(13, 8)
162 #define MESON_SAR_ADC_MAX_FIFO_SIZE 32
163 #define MESON_SAR_ADC_TIMEOUT 100 /* ms */
164 #define MESON_SAR_ADC_VOLTAGE_AND_TEMP_CHANNEL 6
165 #define MESON_SAR_ADC_TEMP_OFFSET 27
167 /* temperature sensor calibration information in eFuse */
168 #define MESON_SAR_ADC_EFUSE_BYTES 4
169 #define MESON_SAR_ADC_EFUSE_BYTE3_UPPER_ADC_VAL GENMASK(6, 0)
170 #define MESON_SAR_ADC_EFUSE_BYTE3_IS_CALIBRATED BIT(7)
172 #define MESON_HHI_DPLL_TOP_0 0x318
173 #define MESON_HHI_DPLL_TOP_0_TSC_BIT4 BIT(9)
175 /* for use with IIO_VAL_INT_PLUS_MICRO */
176 #define MILLION 1000000
178 #define MESON_SAR_ADC_CHAN(_chan) { \
179 .type = IIO_VOLTAGE, \
180 .indexed = 1, \
181 .channel = _chan, \
182 .address = _chan, \
183 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
184 BIT(IIO_CHAN_INFO_AVERAGE_RAW), \
185 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
186 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_CALIBBIAS) | \
187 BIT(IIO_CHAN_INFO_CALIBSCALE), \
188 .datasheet_name = "SAR_ADC_CH"#_chan, \
191 #define MESON_SAR_ADC_TEMP_CHAN(_chan) { \
192 .type = IIO_TEMP, \
193 .channel = _chan, \
194 .address = MESON_SAR_ADC_VOLTAGE_AND_TEMP_CHANNEL, \
195 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
196 BIT(IIO_CHAN_INFO_AVERAGE_RAW), \
197 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) | \
198 BIT(IIO_CHAN_INFO_SCALE), \
199 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_CALIBBIAS) | \
200 BIT(IIO_CHAN_INFO_CALIBSCALE), \
201 .datasheet_name = "TEMP_SENSOR", \
204 static const struct iio_chan_spec meson_sar_adc_iio_channels[] = {
205 MESON_SAR_ADC_CHAN(0),
206 MESON_SAR_ADC_CHAN(1),
207 MESON_SAR_ADC_CHAN(2),
208 MESON_SAR_ADC_CHAN(3),
209 MESON_SAR_ADC_CHAN(4),
210 MESON_SAR_ADC_CHAN(5),
211 MESON_SAR_ADC_CHAN(6),
212 MESON_SAR_ADC_CHAN(7),
213 IIO_CHAN_SOFT_TIMESTAMP(8),
216 static const struct iio_chan_spec meson_sar_adc_and_temp_iio_channels[] = {
217 MESON_SAR_ADC_CHAN(0),
218 MESON_SAR_ADC_CHAN(1),
219 MESON_SAR_ADC_CHAN(2),
220 MESON_SAR_ADC_CHAN(3),
221 MESON_SAR_ADC_CHAN(4),
222 MESON_SAR_ADC_CHAN(5),
223 MESON_SAR_ADC_CHAN(6),
224 MESON_SAR_ADC_CHAN(7),
225 MESON_SAR_ADC_TEMP_CHAN(8),
226 IIO_CHAN_SOFT_TIMESTAMP(9),
229 enum meson_sar_adc_avg_mode {
230 NO_AVERAGING = 0x0,
231 MEAN_AVERAGING = 0x1,
232 MEDIAN_AVERAGING = 0x2,
235 enum meson_sar_adc_num_samples {
236 ONE_SAMPLE = 0x0,
237 TWO_SAMPLES = 0x1,
238 FOUR_SAMPLES = 0x2,
239 EIGHT_SAMPLES = 0x3,
242 enum meson_sar_adc_chan7_mux_sel {
243 CHAN7_MUX_VSS = 0x0,
244 CHAN7_MUX_VDD_DIV4 = 0x1,
245 CHAN7_MUX_VDD_DIV2 = 0x2,
246 CHAN7_MUX_VDD_MUL3_DIV4 = 0x3,
247 CHAN7_MUX_VDD = 0x4,
248 CHAN7_MUX_CH7_INPUT = 0x7,
251 struct meson_sar_adc_param {
252 bool has_bl30_integration;
253 unsigned long clock_rate;
254 u32 bandgap_reg;
255 unsigned int resolution;
256 const struct regmap_config *regmap_config;
257 u8 temperature_trimming_bits;
258 unsigned int temperature_multiplier;
259 unsigned int temperature_divider;
262 struct meson_sar_adc_data {
263 const struct meson_sar_adc_param *param;
264 const char *name;
267 struct meson_sar_adc_priv {
268 struct regmap *regmap;
269 struct regulator *vref;
270 const struct meson_sar_adc_param *param;
271 struct clk *clkin;
272 struct clk *core_clk;
273 struct clk *adc_sel_clk;
274 struct clk *adc_clk;
275 struct clk_gate clk_gate;
276 struct clk *adc_div_clk;
277 struct clk_divider clk_div;
278 struct completion done;
279 int calibbias;
280 int calibscale;
281 struct regmap *tsc_regmap;
282 bool temperature_sensor_calibrated;
283 u8 temperature_sensor_coefficient;
284 u16 temperature_sensor_adc_val;
287 static const struct regmap_config meson_sar_adc_regmap_config_gxbb = {
288 .reg_bits = 8,
289 .val_bits = 32,
290 .reg_stride = 4,
291 .max_register = MESON_SAR_ADC_REG13,
294 static const struct regmap_config meson_sar_adc_regmap_config_meson8 = {
295 .reg_bits = 8,
296 .val_bits = 32,
297 .reg_stride = 4,
298 .max_register = MESON_SAR_ADC_DELTA_10,
301 static unsigned int meson_sar_adc_get_fifo_count(struct iio_dev *indio_dev)
303 struct meson_sar_adc_priv *priv = iio_priv(indio_dev);
304 u32 regval;
306 regmap_read(priv->regmap, MESON_SAR_ADC_REG0, &regval);
308 return FIELD_GET(MESON_SAR_ADC_REG0_FIFO_COUNT_MASK, regval);
311 static int meson_sar_adc_calib_val(struct iio_dev *indio_dev, int val)
313 struct meson_sar_adc_priv *priv = iio_priv(indio_dev);
314 int tmp;
316 /* use val_calib = scale * val_raw + offset calibration function */
317 tmp = div_s64((s64)val * priv->calibscale, MILLION) + priv->calibbias;
319 return clamp(tmp, 0, (1 << priv->param->resolution) - 1);
322 static int meson_sar_adc_wait_busy_clear(struct iio_dev *indio_dev)
324 struct meson_sar_adc_priv *priv = iio_priv(indio_dev);
325 int regval, timeout = 10000;
328 * NOTE: we need a small delay before reading the status, otherwise
329 * the sample engine may not have started internally (which would
330 * seem to us that sampling is already finished).
332 do {
333 udelay(1);
334 regmap_read(priv->regmap, MESON_SAR_ADC_REG0, &regval);
335 } while (FIELD_GET(MESON_SAR_ADC_REG0_BUSY_MASK, regval) && timeout--);
337 if (timeout < 0)
338 return -ETIMEDOUT;
340 return 0;
343 static int meson_sar_adc_read_raw_sample(struct iio_dev *indio_dev,
344 const struct iio_chan_spec *chan,
345 int *val)
347 struct meson_sar_adc_priv *priv = iio_priv(indio_dev);
348 int regval, fifo_chan, fifo_val, count;
350 if(!wait_for_completion_timeout(&priv->done,
351 msecs_to_jiffies(MESON_SAR_ADC_TIMEOUT)))
352 return -ETIMEDOUT;
354 count = meson_sar_adc_get_fifo_count(indio_dev);
355 if (count != 1) {
356 dev_err(&indio_dev->dev,
357 "ADC FIFO has %d element(s) instead of one\n", count);
358 return -EINVAL;
361 regmap_read(priv->regmap, MESON_SAR_ADC_FIFO_RD, &regval);
362 fifo_chan = FIELD_GET(MESON_SAR_ADC_FIFO_RD_CHAN_ID_MASK, regval);
363 if (fifo_chan != chan->address) {
364 dev_err(&indio_dev->dev,
365 "ADC FIFO entry belongs to channel %d instead of %lu\n",
366 fifo_chan, chan->address);
367 return -EINVAL;
370 fifo_val = FIELD_GET(MESON_SAR_ADC_FIFO_RD_SAMPLE_VALUE_MASK, regval);
371 fifo_val &= GENMASK(priv->param->resolution - 1, 0);
372 *val = meson_sar_adc_calib_val(indio_dev, fifo_val);
374 return 0;
377 static void meson_sar_adc_set_averaging(struct iio_dev *indio_dev,
378 const struct iio_chan_spec *chan,
379 enum meson_sar_adc_avg_mode mode,
380 enum meson_sar_adc_num_samples samples)
382 struct meson_sar_adc_priv *priv = iio_priv(indio_dev);
383 int val, address = chan->address;
385 val = samples << MESON_SAR_ADC_AVG_CNTL_NUM_SAMPLES_SHIFT(address);
386 regmap_update_bits(priv->regmap, MESON_SAR_ADC_AVG_CNTL,
387 MESON_SAR_ADC_AVG_CNTL_NUM_SAMPLES_MASK(address),
388 val);
390 val = mode << MESON_SAR_ADC_AVG_CNTL_AVG_MODE_SHIFT(address);
391 regmap_update_bits(priv->regmap, MESON_SAR_ADC_AVG_CNTL,
392 MESON_SAR_ADC_AVG_CNTL_AVG_MODE_MASK(address), val);
395 static void meson_sar_adc_enable_channel(struct iio_dev *indio_dev,
396 const struct iio_chan_spec *chan)
398 struct meson_sar_adc_priv *priv = iio_priv(indio_dev);
399 u32 regval;
402 * the SAR ADC engine allows sampling multiple channels at the same
403 * time. to keep it simple we're only working with one *internal*
404 * channel, which starts counting at index 0 (which means: count = 1).
406 regval = FIELD_PREP(MESON_SAR_ADC_CHAN_LIST_MAX_INDEX_MASK, 0);
407 regmap_update_bits(priv->regmap, MESON_SAR_ADC_CHAN_LIST,
408 MESON_SAR_ADC_CHAN_LIST_MAX_INDEX_MASK, regval);
410 /* map channel index 0 to the channel which we want to read */
411 regval = FIELD_PREP(MESON_SAR_ADC_CHAN_LIST_ENTRY_MASK(0),
412 chan->address);
413 regmap_update_bits(priv->regmap, MESON_SAR_ADC_CHAN_LIST,
414 MESON_SAR_ADC_CHAN_LIST_ENTRY_MASK(0), regval);
416 regval = FIELD_PREP(MESON_SAR_ADC_DETECT_IDLE_SW_DETECT_MUX_MASK,
417 chan->address);
418 regmap_update_bits(priv->regmap, MESON_SAR_ADC_DETECT_IDLE_SW,
419 MESON_SAR_ADC_DETECT_IDLE_SW_DETECT_MUX_MASK,
420 regval);
422 regval = FIELD_PREP(MESON_SAR_ADC_DETECT_IDLE_SW_IDLE_MUX_SEL_MASK,
423 chan->address);
424 regmap_update_bits(priv->regmap, MESON_SAR_ADC_DETECT_IDLE_SW,
425 MESON_SAR_ADC_DETECT_IDLE_SW_IDLE_MUX_SEL_MASK,
426 regval);
428 if (chan->address == MESON_SAR_ADC_VOLTAGE_AND_TEMP_CHANNEL) {
429 if (chan->type == IIO_TEMP)
430 regval = MESON_SAR_ADC_DELTA_10_TEMP_SEL;
431 else
432 regval = 0;
434 regmap_update_bits(priv->regmap,
435 MESON_SAR_ADC_DELTA_10,
436 MESON_SAR_ADC_DELTA_10_TEMP_SEL, regval);
440 static void meson_sar_adc_set_chan7_mux(struct iio_dev *indio_dev,
441 enum meson_sar_adc_chan7_mux_sel sel)
443 struct meson_sar_adc_priv *priv = iio_priv(indio_dev);
444 u32 regval;
446 regval = FIELD_PREP(MESON_SAR_ADC_REG3_CTRL_CHAN7_MUX_SEL_MASK, sel);
447 regmap_update_bits(priv->regmap, MESON_SAR_ADC_REG3,
448 MESON_SAR_ADC_REG3_CTRL_CHAN7_MUX_SEL_MASK, regval);
450 usleep_range(10, 20);
453 static void meson_sar_adc_start_sample_engine(struct iio_dev *indio_dev)
455 struct meson_sar_adc_priv *priv = iio_priv(indio_dev);
457 reinit_completion(&priv->done);
459 regmap_update_bits(priv->regmap, MESON_SAR_ADC_REG0,
460 MESON_SAR_ADC_REG0_FIFO_IRQ_EN,
461 MESON_SAR_ADC_REG0_FIFO_IRQ_EN);
463 regmap_update_bits(priv->regmap, MESON_SAR_ADC_REG0,
464 MESON_SAR_ADC_REG0_SAMPLE_ENGINE_ENABLE,
465 MESON_SAR_ADC_REG0_SAMPLE_ENGINE_ENABLE);
467 regmap_update_bits(priv->regmap, MESON_SAR_ADC_REG0,
468 MESON_SAR_ADC_REG0_SAMPLING_START,
469 MESON_SAR_ADC_REG0_SAMPLING_START);
472 static void meson_sar_adc_stop_sample_engine(struct iio_dev *indio_dev)
474 struct meson_sar_adc_priv *priv = iio_priv(indio_dev);
476 regmap_update_bits(priv->regmap, MESON_SAR_ADC_REG0,
477 MESON_SAR_ADC_REG0_FIFO_IRQ_EN, 0);
479 regmap_update_bits(priv->regmap, MESON_SAR_ADC_REG0,
480 MESON_SAR_ADC_REG0_SAMPLING_STOP,
481 MESON_SAR_ADC_REG0_SAMPLING_STOP);
483 /* wait until all modules are stopped */
484 meson_sar_adc_wait_busy_clear(indio_dev);
486 regmap_update_bits(priv->regmap, MESON_SAR_ADC_REG0,
487 MESON_SAR_ADC_REG0_SAMPLE_ENGINE_ENABLE, 0);
490 static int meson_sar_adc_lock(struct iio_dev *indio_dev)
492 struct meson_sar_adc_priv *priv = iio_priv(indio_dev);
493 int val, timeout = 10000;
495 mutex_lock(&indio_dev->mlock);
497 if (priv->param->has_bl30_integration) {
498 /* prevent BL30 from using the SAR ADC while we are using it */
499 regmap_update_bits(priv->regmap, MESON_SAR_ADC_DELAY,
500 MESON_SAR_ADC_DELAY_KERNEL_BUSY,
501 MESON_SAR_ADC_DELAY_KERNEL_BUSY);
504 * wait until BL30 releases it's lock (so we can use the SAR
505 * ADC)
507 do {
508 udelay(1);
509 regmap_read(priv->regmap, MESON_SAR_ADC_DELAY, &val);
510 } while (val & MESON_SAR_ADC_DELAY_BL30_BUSY && timeout--);
512 if (timeout < 0) {
513 mutex_unlock(&indio_dev->mlock);
514 return -ETIMEDOUT;
518 return 0;
521 static void meson_sar_adc_unlock(struct iio_dev *indio_dev)
523 struct meson_sar_adc_priv *priv = iio_priv(indio_dev);
525 if (priv->param->has_bl30_integration)
526 /* allow BL30 to use the SAR ADC again */
527 regmap_update_bits(priv->regmap, MESON_SAR_ADC_DELAY,
528 MESON_SAR_ADC_DELAY_KERNEL_BUSY, 0);
530 mutex_unlock(&indio_dev->mlock);
533 static void meson_sar_adc_clear_fifo(struct iio_dev *indio_dev)
535 struct meson_sar_adc_priv *priv = iio_priv(indio_dev);
536 unsigned int count, tmp;
538 for (count = 0; count < MESON_SAR_ADC_MAX_FIFO_SIZE; count++) {
539 if (!meson_sar_adc_get_fifo_count(indio_dev))
540 break;
542 regmap_read(priv->regmap, MESON_SAR_ADC_FIFO_RD, &tmp);
546 static int meson_sar_adc_get_sample(struct iio_dev *indio_dev,
547 const struct iio_chan_spec *chan,
548 enum meson_sar_adc_avg_mode avg_mode,
549 enum meson_sar_adc_num_samples avg_samples,
550 int *val)
552 struct meson_sar_adc_priv *priv = iio_priv(indio_dev);
553 int ret;
555 if (chan->type == IIO_TEMP && !priv->temperature_sensor_calibrated)
556 return -ENOTSUPP;
558 ret = meson_sar_adc_lock(indio_dev);
559 if (ret)
560 return ret;
562 /* clear the FIFO to make sure we're not reading old values */
563 meson_sar_adc_clear_fifo(indio_dev);
565 meson_sar_adc_set_averaging(indio_dev, chan, avg_mode, avg_samples);
567 meson_sar_adc_enable_channel(indio_dev, chan);
569 meson_sar_adc_start_sample_engine(indio_dev);
570 ret = meson_sar_adc_read_raw_sample(indio_dev, chan, val);
571 meson_sar_adc_stop_sample_engine(indio_dev);
573 meson_sar_adc_unlock(indio_dev);
575 if (ret) {
576 dev_warn(indio_dev->dev.parent,
577 "failed to read sample for channel %lu: %d\n",
578 chan->address, ret);
579 return ret;
582 return IIO_VAL_INT;
585 static int meson_sar_adc_iio_info_read_raw(struct iio_dev *indio_dev,
586 const struct iio_chan_spec *chan,
587 int *val, int *val2, long mask)
589 struct meson_sar_adc_priv *priv = iio_priv(indio_dev);
590 int ret;
592 switch (mask) {
593 case IIO_CHAN_INFO_RAW:
594 return meson_sar_adc_get_sample(indio_dev, chan, NO_AVERAGING,
595 ONE_SAMPLE, val);
597 case IIO_CHAN_INFO_AVERAGE_RAW:
598 return meson_sar_adc_get_sample(indio_dev, chan,
599 MEAN_AVERAGING, EIGHT_SAMPLES,
600 val);
602 case IIO_CHAN_INFO_SCALE:
603 if (chan->type == IIO_VOLTAGE) {
604 ret = regulator_get_voltage(priv->vref);
605 if (ret < 0) {
606 dev_err(indio_dev->dev.parent,
607 "failed to get vref voltage: %d\n",
608 ret);
609 return ret;
612 *val = ret / 1000;
613 *val2 = priv->param->resolution;
614 return IIO_VAL_FRACTIONAL_LOG2;
615 } else if (chan->type == IIO_TEMP) {
616 /* SoC specific multiplier and divider */
617 *val = priv->param->temperature_multiplier;
618 *val2 = priv->param->temperature_divider;
620 /* celsius to millicelsius */
621 *val *= 1000;
623 return IIO_VAL_FRACTIONAL;
624 } else {
625 return -EINVAL;
628 case IIO_CHAN_INFO_CALIBBIAS:
629 *val = priv->calibbias;
630 return IIO_VAL_INT;
632 case IIO_CHAN_INFO_CALIBSCALE:
633 *val = priv->calibscale / MILLION;
634 *val2 = priv->calibscale % MILLION;
635 return IIO_VAL_INT_PLUS_MICRO;
637 case IIO_CHAN_INFO_OFFSET:
638 *val = DIV_ROUND_CLOSEST(MESON_SAR_ADC_TEMP_OFFSET *
639 priv->param->temperature_divider,
640 priv->param->temperature_multiplier);
641 *val -= priv->temperature_sensor_adc_val;
642 return IIO_VAL_INT;
644 default:
645 return -EINVAL;
649 static int meson_sar_adc_clk_init(struct iio_dev *indio_dev,
650 void __iomem *base)
652 struct meson_sar_adc_priv *priv = iio_priv(indio_dev);
653 struct clk_init_data init;
654 const char *clk_parents[1];
656 init.name = devm_kasprintf(&indio_dev->dev, GFP_KERNEL, "%s#adc_div",
657 dev_name(indio_dev->dev.parent));
658 if (!init.name)
659 return -ENOMEM;
661 init.flags = 0;
662 init.ops = &clk_divider_ops;
663 clk_parents[0] = __clk_get_name(priv->clkin);
664 init.parent_names = clk_parents;
665 init.num_parents = 1;
667 priv->clk_div.reg = base + MESON_SAR_ADC_REG3;
668 priv->clk_div.shift = MESON_SAR_ADC_REG3_ADC_CLK_DIV_SHIFT;
669 priv->clk_div.width = MESON_SAR_ADC_REG3_ADC_CLK_DIV_WIDTH;
670 priv->clk_div.hw.init = &init;
671 priv->clk_div.flags = 0;
673 priv->adc_div_clk = devm_clk_register(&indio_dev->dev,
674 &priv->clk_div.hw);
675 if (WARN_ON(IS_ERR(priv->adc_div_clk)))
676 return PTR_ERR(priv->adc_div_clk);
678 init.name = devm_kasprintf(&indio_dev->dev, GFP_KERNEL, "%s#adc_en",
679 dev_name(indio_dev->dev.parent));
680 if (!init.name)
681 return -ENOMEM;
683 init.flags = CLK_SET_RATE_PARENT;
684 init.ops = &clk_gate_ops;
685 clk_parents[0] = __clk_get_name(priv->adc_div_clk);
686 init.parent_names = clk_parents;
687 init.num_parents = 1;
689 priv->clk_gate.reg = base + MESON_SAR_ADC_REG3;
690 priv->clk_gate.bit_idx = __ffs(MESON_SAR_ADC_REG3_CLK_EN);
691 priv->clk_gate.hw.init = &init;
693 priv->adc_clk = devm_clk_register(&indio_dev->dev, &priv->clk_gate.hw);
694 if (WARN_ON(IS_ERR(priv->adc_clk)))
695 return PTR_ERR(priv->adc_clk);
697 return 0;
700 static int meson_sar_adc_temp_sensor_init(struct iio_dev *indio_dev)
702 struct meson_sar_adc_priv *priv = iio_priv(indio_dev);
703 u8 *buf, trimming_bits, trimming_mask, upper_adc_val;
704 struct nvmem_cell *temperature_calib;
705 size_t read_len;
706 int ret;
708 temperature_calib = devm_nvmem_cell_get(indio_dev->dev.parent,
709 "temperature_calib");
710 if (IS_ERR(temperature_calib)) {
711 ret = PTR_ERR(temperature_calib);
714 * leave the temperature sensor disabled if no calibration data
715 * was passed via nvmem-cells.
717 if (ret == -ENODEV)
718 return 0;
720 return dev_err_probe(indio_dev->dev.parent, ret,
721 "failed to get temperature_calib cell\n");
724 priv->tsc_regmap =
725 syscon_regmap_lookup_by_phandle(indio_dev->dev.parent->of_node,
726 "amlogic,hhi-sysctrl");
727 if (IS_ERR(priv->tsc_regmap)) {
728 dev_err(indio_dev->dev.parent,
729 "failed to get amlogic,hhi-sysctrl regmap\n");
730 return PTR_ERR(priv->tsc_regmap);
733 read_len = MESON_SAR_ADC_EFUSE_BYTES;
734 buf = nvmem_cell_read(temperature_calib, &read_len);
735 if (IS_ERR(buf)) {
736 dev_err(indio_dev->dev.parent,
737 "failed to read temperature_calib cell\n");
738 return PTR_ERR(buf);
739 } else if (read_len != MESON_SAR_ADC_EFUSE_BYTES) {
740 kfree(buf);
741 dev_err(indio_dev->dev.parent,
742 "invalid read size of temperature_calib cell\n");
743 return -EINVAL;
746 trimming_bits = priv->param->temperature_trimming_bits;
747 trimming_mask = BIT(trimming_bits) - 1;
749 priv->temperature_sensor_calibrated =
750 buf[3] & MESON_SAR_ADC_EFUSE_BYTE3_IS_CALIBRATED;
751 priv->temperature_sensor_coefficient = buf[2] & trimming_mask;
753 upper_adc_val = FIELD_GET(MESON_SAR_ADC_EFUSE_BYTE3_UPPER_ADC_VAL,
754 buf[3]);
756 priv->temperature_sensor_adc_val = buf[2];
757 priv->temperature_sensor_adc_val |= upper_adc_val << BITS_PER_BYTE;
758 priv->temperature_sensor_adc_val >>= trimming_bits;
760 kfree(buf);
762 return 0;
765 static int meson_sar_adc_init(struct iio_dev *indio_dev)
767 struct meson_sar_adc_priv *priv = iio_priv(indio_dev);
768 int regval, i, ret;
771 * make sure we start at CH7 input since the other muxes are only used
772 * for internal calibration.
774 meson_sar_adc_set_chan7_mux(indio_dev, CHAN7_MUX_CH7_INPUT);
776 if (priv->param->has_bl30_integration) {
778 * leave sampling delay and the input clocks as configured by
779 * BL30 to make sure BL30 gets the values it expects when
780 * reading the temperature sensor.
782 regmap_read(priv->regmap, MESON_SAR_ADC_REG3, &regval);
783 if (regval & MESON_SAR_ADC_REG3_BL30_INITIALIZED)
784 return 0;
787 meson_sar_adc_stop_sample_engine(indio_dev);
790 * disable this bit as seems to be only relevant for Meson6 (based
791 * on the vendor driver), which we don't support at the moment.
793 regmap_update_bits(priv->regmap, MESON_SAR_ADC_REG0,
794 MESON_SAR_ADC_REG0_ADC_TEMP_SEN_SEL, 0);
796 /* disable all channels by default */
797 regmap_write(priv->regmap, MESON_SAR_ADC_CHAN_LIST, 0x0);
799 regmap_update_bits(priv->regmap, MESON_SAR_ADC_REG3,
800 MESON_SAR_ADC_REG3_CTRL_SAMPLING_CLOCK_PHASE, 0);
801 regmap_update_bits(priv->regmap, MESON_SAR_ADC_REG3,
802 MESON_SAR_ADC_REG3_CNTL_USE_SC_DLY,
803 MESON_SAR_ADC_REG3_CNTL_USE_SC_DLY);
805 /* delay between two samples = (10+1) * 1uS */
806 regmap_update_bits(priv->regmap, MESON_SAR_ADC_DELAY,
807 MESON_SAR_ADC_DELAY_INPUT_DLY_CNT_MASK,
808 FIELD_PREP(MESON_SAR_ADC_DELAY_SAMPLE_DLY_CNT_MASK,
809 10));
810 regmap_update_bits(priv->regmap, MESON_SAR_ADC_DELAY,
811 MESON_SAR_ADC_DELAY_SAMPLE_DLY_SEL_MASK,
812 FIELD_PREP(MESON_SAR_ADC_DELAY_SAMPLE_DLY_SEL_MASK,
813 0));
815 /* delay between two samples = (10+1) * 1uS */
816 regmap_update_bits(priv->regmap, MESON_SAR_ADC_DELAY,
817 MESON_SAR_ADC_DELAY_INPUT_DLY_CNT_MASK,
818 FIELD_PREP(MESON_SAR_ADC_DELAY_INPUT_DLY_CNT_MASK,
819 10));
820 regmap_update_bits(priv->regmap, MESON_SAR_ADC_DELAY,
821 MESON_SAR_ADC_DELAY_INPUT_DLY_SEL_MASK,
822 FIELD_PREP(MESON_SAR_ADC_DELAY_INPUT_DLY_SEL_MASK,
823 1));
826 * set up the input channel muxes in MESON_SAR_ADC_CHAN_10_SW
827 * (0 = SAR_ADC_CH0, 1 = SAR_ADC_CH1)
829 regval = FIELD_PREP(MESON_SAR_ADC_CHAN_10_SW_CHAN0_MUX_SEL_MASK, 0);
830 regmap_update_bits(priv->regmap, MESON_SAR_ADC_CHAN_10_SW,
831 MESON_SAR_ADC_CHAN_10_SW_CHAN0_MUX_SEL_MASK,
832 regval);
833 regval = FIELD_PREP(MESON_SAR_ADC_CHAN_10_SW_CHAN1_MUX_SEL_MASK, 1);
834 regmap_update_bits(priv->regmap, MESON_SAR_ADC_CHAN_10_SW,
835 MESON_SAR_ADC_CHAN_10_SW_CHAN1_MUX_SEL_MASK,
836 regval);
839 * set up the input channel muxes in MESON_SAR_ADC_AUX_SW
840 * (2 = SAR_ADC_CH2, 3 = SAR_ADC_CH3, ...) and enable
841 * MESON_SAR_ADC_AUX_SW_YP_DRIVE_SW and
842 * MESON_SAR_ADC_AUX_SW_XP_DRIVE_SW like the vendor driver.
844 regval = 0;
845 for (i = 2; i <= 7; i++)
846 regval |= i << MESON_SAR_ADC_AUX_SW_MUX_SEL_CHAN_SHIFT(i);
847 regval |= MESON_SAR_ADC_AUX_SW_YP_DRIVE_SW;
848 regval |= MESON_SAR_ADC_AUX_SW_XP_DRIVE_SW;
849 regmap_write(priv->regmap, MESON_SAR_ADC_AUX_SW, regval);
851 if (priv->temperature_sensor_calibrated) {
852 regmap_update_bits(priv->regmap, MESON_SAR_ADC_DELTA_10,
853 MESON_SAR_ADC_DELTA_10_TS_REVE1,
854 MESON_SAR_ADC_DELTA_10_TS_REVE1);
855 regmap_update_bits(priv->regmap, MESON_SAR_ADC_DELTA_10,
856 MESON_SAR_ADC_DELTA_10_TS_REVE0,
857 MESON_SAR_ADC_DELTA_10_TS_REVE0);
860 * set bits [3:0] of the TSC (temperature sensor coefficient)
861 * to get the correct values when reading the temperature.
863 regval = FIELD_PREP(MESON_SAR_ADC_DELTA_10_TS_C_MASK,
864 priv->temperature_sensor_coefficient);
865 regmap_update_bits(priv->regmap, MESON_SAR_ADC_DELTA_10,
866 MESON_SAR_ADC_DELTA_10_TS_C_MASK, regval);
868 if (priv->param->temperature_trimming_bits == 5) {
869 if (priv->temperature_sensor_coefficient & BIT(4))
870 regval = MESON_HHI_DPLL_TOP_0_TSC_BIT4;
871 else
872 regval = 0;
875 * bit [4] (the 5th bit when starting to count at 1)
876 * of the TSC is located in the HHI register area.
878 regmap_update_bits(priv->tsc_regmap,
879 MESON_HHI_DPLL_TOP_0,
880 MESON_HHI_DPLL_TOP_0_TSC_BIT4,
881 regval);
883 } else {
884 regmap_update_bits(priv->regmap, MESON_SAR_ADC_DELTA_10,
885 MESON_SAR_ADC_DELTA_10_TS_REVE1, 0);
886 regmap_update_bits(priv->regmap, MESON_SAR_ADC_DELTA_10,
887 MESON_SAR_ADC_DELTA_10_TS_REVE0, 0);
890 ret = clk_set_parent(priv->adc_sel_clk, priv->clkin);
891 if (ret) {
892 dev_err(indio_dev->dev.parent,
893 "failed to set adc parent to clkin\n");
894 return ret;
897 ret = clk_set_rate(priv->adc_clk, priv->param->clock_rate);
898 if (ret) {
899 dev_err(indio_dev->dev.parent,
900 "failed to set adc clock rate\n");
901 return ret;
904 return 0;
907 static void meson_sar_adc_set_bandgap(struct iio_dev *indio_dev, bool on_off)
909 struct meson_sar_adc_priv *priv = iio_priv(indio_dev);
910 const struct meson_sar_adc_param *param = priv->param;
911 u32 enable_mask;
913 if (param->bandgap_reg == MESON_SAR_ADC_REG11)
914 enable_mask = MESON_SAR_ADC_REG11_BANDGAP_EN;
915 else
916 enable_mask = MESON_SAR_ADC_DELTA_10_TS_VBG_EN;
918 regmap_update_bits(priv->regmap, param->bandgap_reg, enable_mask,
919 on_off ? enable_mask : 0);
922 static int meson_sar_adc_hw_enable(struct iio_dev *indio_dev)
924 struct meson_sar_adc_priv *priv = iio_priv(indio_dev);
925 int ret;
926 u32 regval;
928 ret = meson_sar_adc_lock(indio_dev);
929 if (ret)
930 goto err_lock;
932 ret = regulator_enable(priv->vref);
933 if (ret < 0) {
934 dev_err(indio_dev->dev.parent,
935 "failed to enable vref regulator\n");
936 goto err_vref;
939 ret = clk_prepare_enable(priv->core_clk);
940 if (ret) {
941 dev_err(indio_dev->dev.parent, "failed to enable core clk\n");
942 goto err_core_clk;
945 regval = FIELD_PREP(MESON_SAR_ADC_REG0_FIFO_CNT_IRQ_MASK, 1);
946 regmap_update_bits(priv->regmap, MESON_SAR_ADC_REG0,
947 MESON_SAR_ADC_REG0_FIFO_CNT_IRQ_MASK, regval);
949 meson_sar_adc_set_bandgap(indio_dev, true);
951 regmap_update_bits(priv->regmap, MESON_SAR_ADC_REG3,
952 MESON_SAR_ADC_REG3_ADC_EN,
953 MESON_SAR_ADC_REG3_ADC_EN);
955 udelay(5);
957 ret = clk_prepare_enable(priv->adc_clk);
958 if (ret) {
959 dev_err(indio_dev->dev.parent, "failed to enable adc clk\n");
960 goto err_adc_clk;
963 meson_sar_adc_unlock(indio_dev);
965 return 0;
967 err_adc_clk:
968 regmap_update_bits(priv->regmap, MESON_SAR_ADC_REG3,
969 MESON_SAR_ADC_REG3_ADC_EN, 0);
970 meson_sar_adc_set_bandgap(indio_dev, false);
971 clk_disable_unprepare(priv->core_clk);
972 err_core_clk:
973 regulator_disable(priv->vref);
974 err_vref:
975 meson_sar_adc_unlock(indio_dev);
976 err_lock:
977 return ret;
980 static int meson_sar_adc_hw_disable(struct iio_dev *indio_dev)
982 struct meson_sar_adc_priv *priv = iio_priv(indio_dev);
983 int ret;
985 ret = meson_sar_adc_lock(indio_dev);
986 if (ret)
987 return ret;
989 clk_disable_unprepare(priv->adc_clk);
991 regmap_update_bits(priv->regmap, MESON_SAR_ADC_REG3,
992 MESON_SAR_ADC_REG3_ADC_EN, 0);
994 meson_sar_adc_set_bandgap(indio_dev, false);
996 clk_disable_unprepare(priv->core_clk);
998 regulator_disable(priv->vref);
1000 meson_sar_adc_unlock(indio_dev);
1002 return 0;
1005 static irqreturn_t meson_sar_adc_irq(int irq, void *data)
1007 struct iio_dev *indio_dev = data;
1008 struct meson_sar_adc_priv *priv = iio_priv(indio_dev);
1009 unsigned int cnt, threshold;
1010 u32 regval;
1012 regmap_read(priv->regmap, MESON_SAR_ADC_REG0, &regval);
1013 cnt = FIELD_GET(MESON_SAR_ADC_REG0_FIFO_COUNT_MASK, regval);
1014 threshold = FIELD_GET(MESON_SAR_ADC_REG0_FIFO_CNT_IRQ_MASK, regval);
1016 if (cnt < threshold)
1017 return IRQ_NONE;
1019 complete(&priv->done);
1021 return IRQ_HANDLED;
1024 static int meson_sar_adc_calib(struct iio_dev *indio_dev)
1026 struct meson_sar_adc_priv *priv = iio_priv(indio_dev);
1027 int ret, nominal0, nominal1, value0, value1;
1029 /* use points 25% and 75% for calibration */
1030 nominal0 = (1 << priv->param->resolution) / 4;
1031 nominal1 = (1 << priv->param->resolution) * 3 / 4;
1033 meson_sar_adc_set_chan7_mux(indio_dev, CHAN7_MUX_VDD_DIV4);
1034 usleep_range(10, 20);
1035 ret = meson_sar_adc_get_sample(indio_dev,
1036 &indio_dev->channels[7],
1037 MEAN_AVERAGING, EIGHT_SAMPLES, &value0);
1038 if (ret < 0)
1039 goto out;
1041 meson_sar_adc_set_chan7_mux(indio_dev, CHAN7_MUX_VDD_MUL3_DIV4);
1042 usleep_range(10, 20);
1043 ret = meson_sar_adc_get_sample(indio_dev,
1044 &indio_dev->channels[7],
1045 MEAN_AVERAGING, EIGHT_SAMPLES, &value1);
1046 if (ret < 0)
1047 goto out;
1049 if (value1 <= value0) {
1050 ret = -EINVAL;
1051 goto out;
1054 priv->calibscale = div_s64((nominal1 - nominal0) * (s64)MILLION,
1055 value1 - value0);
1056 priv->calibbias = nominal0 - div_s64((s64)value0 * priv->calibscale,
1057 MILLION);
1058 ret = 0;
1059 out:
1060 meson_sar_adc_set_chan7_mux(indio_dev, CHAN7_MUX_CH7_INPUT);
1062 return ret;
1065 static const struct iio_info meson_sar_adc_iio_info = {
1066 .read_raw = meson_sar_adc_iio_info_read_raw,
1069 static const struct meson_sar_adc_param meson_sar_adc_meson8_param = {
1070 .has_bl30_integration = false,
1071 .clock_rate = 1150000,
1072 .bandgap_reg = MESON_SAR_ADC_DELTA_10,
1073 .regmap_config = &meson_sar_adc_regmap_config_meson8,
1074 .resolution = 10,
1075 .temperature_trimming_bits = 4,
1076 .temperature_multiplier = 18 * 10000,
1077 .temperature_divider = 1024 * 10 * 85,
1080 static const struct meson_sar_adc_param meson_sar_adc_meson8b_param = {
1081 .has_bl30_integration = false,
1082 .clock_rate = 1150000,
1083 .bandgap_reg = MESON_SAR_ADC_DELTA_10,
1084 .regmap_config = &meson_sar_adc_regmap_config_meson8,
1085 .resolution = 10,
1086 .temperature_trimming_bits = 5,
1087 .temperature_multiplier = 10,
1088 .temperature_divider = 32,
1091 static const struct meson_sar_adc_param meson_sar_adc_gxbb_param = {
1092 .has_bl30_integration = true,
1093 .clock_rate = 1200000,
1094 .bandgap_reg = MESON_SAR_ADC_REG11,
1095 .regmap_config = &meson_sar_adc_regmap_config_gxbb,
1096 .resolution = 10,
1099 static const struct meson_sar_adc_param meson_sar_adc_gxl_param = {
1100 .has_bl30_integration = true,
1101 .clock_rate = 1200000,
1102 .bandgap_reg = MESON_SAR_ADC_REG11,
1103 .regmap_config = &meson_sar_adc_regmap_config_gxbb,
1104 .resolution = 12,
1107 static const struct meson_sar_adc_data meson_sar_adc_meson8_data = {
1108 .param = &meson_sar_adc_meson8_param,
1109 .name = "meson-meson8-saradc",
1112 static const struct meson_sar_adc_data meson_sar_adc_meson8b_data = {
1113 .param = &meson_sar_adc_meson8b_param,
1114 .name = "meson-meson8b-saradc",
1117 static const struct meson_sar_adc_data meson_sar_adc_meson8m2_data = {
1118 .param = &meson_sar_adc_meson8b_param,
1119 .name = "meson-meson8m2-saradc",
1122 static const struct meson_sar_adc_data meson_sar_adc_gxbb_data = {
1123 .param = &meson_sar_adc_gxbb_param,
1124 .name = "meson-gxbb-saradc",
1127 static const struct meson_sar_adc_data meson_sar_adc_gxl_data = {
1128 .param = &meson_sar_adc_gxl_param,
1129 .name = "meson-gxl-saradc",
1132 static const struct meson_sar_adc_data meson_sar_adc_gxm_data = {
1133 .param = &meson_sar_adc_gxl_param,
1134 .name = "meson-gxm-saradc",
1137 static const struct meson_sar_adc_data meson_sar_adc_axg_data = {
1138 .param = &meson_sar_adc_gxl_param,
1139 .name = "meson-axg-saradc",
1142 static const struct meson_sar_adc_data meson_sar_adc_g12a_data = {
1143 .param = &meson_sar_adc_gxl_param,
1144 .name = "meson-g12a-saradc",
1147 static const struct of_device_id meson_sar_adc_of_match[] = {
1149 .compatible = "amlogic,meson8-saradc",
1150 .data = &meson_sar_adc_meson8_data,
1151 }, {
1152 .compatible = "amlogic,meson8b-saradc",
1153 .data = &meson_sar_adc_meson8b_data,
1154 }, {
1155 .compatible = "amlogic,meson8m2-saradc",
1156 .data = &meson_sar_adc_meson8m2_data,
1157 }, {
1158 .compatible = "amlogic,meson-gxbb-saradc",
1159 .data = &meson_sar_adc_gxbb_data,
1160 }, {
1161 .compatible = "amlogic,meson-gxl-saradc",
1162 .data = &meson_sar_adc_gxl_data,
1163 }, {
1164 .compatible = "amlogic,meson-gxm-saradc",
1165 .data = &meson_sar_adc_gxm_data,
1166 }, {
1167 .compatible = "amlogic,meson-axg-saradc",
1168 .data = &meson_sar_adc_axg_data,
1169 }, {
1170 .compatible = "amlogic,meson-g12a-saradc",
1171 .data = &meson_sar_adc_g12a_data,
1173 { /* sentinel */ }
1175 MODULE_DEVICE_TABLE(of, meson_sar_adc_of_match);
1177 static int meson_sar_adc_probe(struct platform_device *pdev)
1179 const struct meson_sar_adc_data *match_data;
1180 struct meson_sar_adc_priv *priv;
1181 struct iio_dev *indio_dev;
1182 void __iomem *base;
1183 int irq, ret;
1185 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*priv));
1186 if (!indio_dev) {
1187 dev_err(&pdev->dev, "failed allocating iio device\n");
1188 return -ENOMEM;
1191 priv = iio_priv(indio_dev);
1192 init_completion(&priv->done);
1194 match_data = of_device_get_match_data(&pdev->dev);
1195 if (!match_data) {
1196 dev_err(&pdev->dev, "failed to get match data\n");
1197 return -ENODEV;
1200 priv->param = match_data->param;
1202 indio_dev->name = match_data->name;
1203 indio_dev->modes = INDIO_DIRECT_MODE;
1204 indio_dev->info = &meson_sar_adc_iio_info;
1206 base = devm_platform_ioremap_resource(pdev, 0);
1207 if (IS_ERR(base))
1208 return PTR_ERR(base);
1210 priv->regmap = devm_regmap_init_mmio(&pdev->dev, base,
1211 priv->param->regmap_config);
1212 if (IS_ERR(priv->regmap))
1213 return PTR_ERR(priv->regmap);
1215 irq = irq_of_parse_and_map(pdev->dev.of_node, 0);
1216 if (!irq)
1217 return -EINVAL;
1219 ret = devm_request_irq(&pdev->dev, irq, meson_sar_adc_irq, IRQF_SHARED,
1220 dev_name(&pdev->dev), indio_dev);
1221 if (ret)
1222 return ret;
1224 priv->clkin = devm_clk_get(&pdev->dev, "clkin");
1225 if (IS_ERR(priv->clkin)) {
1226 dev_err(&pdev->dev, "failed to get clkin\n");
1227 return PTR_ERR(priv->clkin);
1230 priv->core_clk = devm_clk_get(&pdev->dev, "core");
1231 if (IS_ERR(priv->core_clk)) {
1232 dev_err(&pdev->dev, "failed to get core clk\n");
1233 return PTR_ERR(priv->core_clk);
1236 priv->adc_clk = devm_clk_get(&pdev->dev, "adc_clk");
1237 if (IS_ERR(priv->adc_clk)) {
1238 if (PTR_ERR(priv->adc_clk) == -ENOENT) {
1239 priv->adc_clk = NULL;
1240 } else {
1241 dev_err(&pdev->dev, "failed to get adc clk\n");
1242 return PTR_ERR(priv->adc_clk);
1246 priv->adc_sel_clk = devm_clk_get(&pdev->dev, "adc_sel");
1247 if (IS_ERR(priv->adc_sel_clk)) {
1248 if (PTR_ERR(priv->adc_sel_clk) == -ENOENT) {
1249 priv->adc_sel_clk = NULL;
1250 } else {
1251 dev_err(&pdev->dev, "failed to get adc_sel clk\n");
1252 return PTR_ERR(priv->adc_sel_clk);
1256 /* on pre-GXBB SoCs the SAR ADC itself provides the ADC clock: */
1257 if (!priv->adc_clk) {
1258 ret = meson_sar_adc_clk_init(indio_dev, base);
1259 if (ret)
1260 return ret;
1263 priv->vref = devm_regulator_get(&pdev->dev, "vref");
1264 if (IS_ERR(priv->vref)) {
1265 dev_err(&pdev->dev, "failed to get vref regulator\n");
1266 return PTR_ERR(priv->vref);
1269 priv->calibscale = MILLION;
1271 if (priv->param->temperature_trimming_bits) {
1272 ret = meson_sar_adc_temp_sensor_init(indio_dev);
1273 if (ret)
1274 return ret;
1277 if (priv->temperature_sensor_calibrated) {
1278 indio_dev->channels = meson_sar_adc_and_temp_iio_channels;
1279 indio_dev->num_channels =
1280 ARRAY_SIZE(meson_sar_adc_and_temp_iio_channels);
1281 } else {
1282 indio_dev->channels = meson_sar_adc_iio_channels;
1283 indio_dev->num_channels =
1284 ARRAY_SIZE(meson_sar_adc_iio_channels);
1287 ret = meson_sar_adc_init(indio_dev);
1288 if (ret)
1289 goto err;
1291 ret = meson_sar_adc_hw_enable(indio_dev);
1292 if (ret)
1293 goto err;
1295 ret = meson_sar_adc_calib(indio_dev);
1296 if (ret)
1297 dev_warn(&pdev->dev, "calibration failed\n");
1299 platform_set_drvdata(pdev, indio_dev);
1301 ret = iio_device_register(indio_dev);
1302 if (ret)
1303 goto err_hw;
1305 return 0;
1307 err_hw:
1308 meson_sar_adc_hw_disable(indio_dev);
1309 err:
1310 return ret;
1313 static int meson_sar_adc_remove(struct platform_device *pdev)
1315 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
1317 iio_device_unregister(indio_dev);
1319 return meson_sar_adc_hw_disable(indio_dev);
1322 static int __maybe_unused meson_sar_adc_suspend(struct device *dev)
1324 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1326 return meson_sar_adc_hw_disable(indio_dev);
1329 static int __maybe_unused meson_sar_adc_resume(struct device *dev)
1331 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1333 return meson_sar_adc_hw_enable(indio_dev);
1336 static SIMPLE_DEV_PM_OPS(meson_sar_adc_pm_ops,
1337 meson_sar_adc_suspend, meson_sar_adc_resume);
1339 static struct platform_driver meson_sar_adc_driver = {
1340 .probe = meson_sar_adc_probe,
1341 .remove = meson_sar_adc_remove,
1342 .driver = {
1343 .name = "meson-saradc",
1344 .of_match_table = meson_sar_adc_of_match,
1345 .pm = &meson_sar_adc_pm_ops,
1349 module_platform_driver(meson_sar_adc_driver);
1351 MODULE_AUTHOR("Martin Blumenstingl <martin.blumenstingl@googlemail.com>");
1352 MODULE_DESCRIPTION("Amlogic Meson SAR ADC driver");
1353 MODULE_LICENSE("GPL v2");