drm/tests: hdmi: Fix memory leaks in drm_display_mode_from_cea_vic()
[drm/drm-misc.git] / drivers / iio / adc / meson_saradc.c
blobe16b0e28974e8ffc0e2ab5a56c7f8b7e8e8fa87b
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/mutex.h>
16 #include <linux/nvmem-consumer.h>
17 #include <linux/interrupt.h>
18 #include <linux/of.h>
19 #include <linux/of_irq.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 6
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)
158 #define MESON_SAR_ADC_REG11_CMV_SEL BIT(6)
159 #define MESON_SAR_ADC_REG11_VREF_VOLTAGE BIT(5)
160 #define MESON_SAR_ADC_REG11_EOC BIT(1)
161 #define MESON_SAR_ADC_REG11_VREF_SEL BIT(0)
163 #define MESON_SAR_ADC_REG13 0x34
164 #define MESON_SAR_ADC_REG13_12BIT_CALIBRATION_MASK GENMASK(13, 8)
166 #define MESON_SAR_ADC_MAX_FIFO_SIZE 32
167 #define MESON_SAR_ADC_TIMEOUT 100 /* ms */
168 #define MESON_SAR_ADC_VOLTAGE_AND_TEMP_CHANNEL 6
169 #define MESON_SAR_ADC_VOLTAGE_AND_MUX_CHANNEL 7
170 #define MESON_SAR_ADC_TEMP_OFFSET 27
172 /* temperature sensor calibration information in eFuse */
173 #define MESON_SAR_ADC_EFUSE_BYTES 4
174 #define MESON_SAR_ADC_EFUSE_BYTE3_UPPER_ADC_VAL GENMASK(6, 0)
175 #define MESON_SAR_ADC_EFUSE_BYTE3_IS_CALIBRATED BIT(7)
177 #define MESON_HHI_DPLL_TOP_0 0x318
178 #define MESON_HHI_DPLL_TOP_0_TSC_BIT4 BIT(9)
180 /* for use with IIO_VAL_INT_PLUS_MICRO */
181 #define MILLION 1000000
183 #define MESON_SAR_ADC_CHAN(_chan) { \
184 .type = IIO_VOLTAGE, \
185 .indexed = 1, \
186 .channel = _chan, \
187 .address = _chan, \
188 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
189 BIT(IIO_CHAN_INFO_AVERAGE_RAW), \
190 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
191 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_CALIBBIAS) | \
192 BIT(IIO_CHAN_INFO_CALIBSCALE), \
193 .datasheet_name = "SAR_ADC_CH"#_chan, \
196 #define MESON_SAR_ADC_TEMP_CHAN(_chan) { \
197 .type = IIO_TEMP, \
198 .channel = _chan, \
199 .address = MESON_SAR_ADC_VOLTAGE_AND_TEMP_CHANNEL, \
200 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
201 BIT(IIO_CHAN_INFO_AVERAGE_RAW), \
202 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) | \
203 BIT(IIO_CHAN_INFO_SCALE), \
204 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_CALIBBIAS) | \
205 BIT(IIO_CHAN_INFO_CALIBSCALE), \
206 .datasheet_name = "TEMP_SENSOR", \
209 #define MESON_SAR_ADC_MUX(_chan, _sel) { \
210 .type = IIO_VOLTAGE, \
211 .channel = _chan, \
212 .indexed = 1, \
213 .address = MESON_SAR_ADC_VOLTAGE_AND_MUX_CHANNEL, \
214 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
215 BIT(IIO_CHAN_INFO_AVERAGE_RAW), \
216 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
217 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_CALIBBIAS) | \
218 BIT(IIO_CHAN_INFO_CALIBSCALE), \
219 .datasheet_name = "SAR_ADC_MUX_"#_sel, \
222 enum meson_sar_adc_vref_sel {
223 VREF_CALIBATION_VOLTAGE = 0,
224 VREF_VDDA = 1,
227 enum meson_sar_adc_avg_mode {
228 NO_AVERAGING = 0x0,
229 MEAN_AVERAGING = 0x1,
230 MEDIAN_AVERAGING = 0x2,
233 enum meson_sar_adc_num_samples {
234 ONE_SAMPLE = 0x0,
235 TWO_SAMPLES = 0x1,
236 FOUR_SAMPLES = 0x2,
237 EIGHT_SAMPLES = 0x3,
240 enum meson_sar_adc_chan7_mux_sel {
241 CHAN7_MUX_VSS = 0x0,
242 CHAN7_MUX_VDD_DIV4 = 0x1,
243 CHAN7_MUX_VDD_DIV2 = 0x2,
244 CHAN7_MUX_VDD_MUL3_DIV4 = 0x3,
245 CHAN7_MUX_VDD = 0x4,
246 CHAN7_MUX_CH7_INPUT = 0x7,
249 enum meson_sar_adc_channel_index {
250 NUM_CHAN_0,
251 NUM_CHAN_1,
252 NUM_CHAN_2,
253 NUM_CHAN_3,
254 NUM_CHAN_4,
255 NUM_CHAN_5,
256 NUM_CHAN_6,
257 NUM_CHAN_7,
258 NUM_CHAN_TEMP,
259 NUM_MUX_0_VSS,
260 NUM_MUX_1_VDD_DIV4,
261 NUM_MUX_2_VDD_DIV2,
262 NUM_MUX_3_VDD_MUL3_DIV4,
263 NUM_MUX_4_VDD,
266 static enum meson_sar_adc_chan7_mux_sel chan7_mux_values[] = {
267 CHAN7_MUX_VSS,
268 CHAN7_MUX_VDD_DIV4,
269 CHAN7_MUX_VDD_DIV2,
270 CHAN7_MUX_VDD_MUL3_DIV4,
271 CHAN7_MUX_VDD,
274 static const char * const chan7_mux_names[] = {
275 [CHAN7_MUX_VSS] = "gnd",
276 [CHAN7_MUX_VDD_DIV4] = "0.25vdd",
277 [CHAN7_MUX_VDD_DIV2] = "0.5vdd",
278 [CHAN7_MUX_VDD_MUL3_DIV4] = "0.75vdd",
279 [CHAN7_MUX_VDD] = "vdd",
282 static const struct iio_chan_spec meson_sar_adc_iio_channels[] = {
283 MESON_SAR_ADC_CHAN(NUM_CHAN_0),
284 MESON_SAR_ADC_CHAN(NUM_CHAN_1),
285 MESON_SAR_ADC_CHAN(NUM_CHAN_2),
286 MESON_SAR_ADC_CHAN(NUM_CHAN_3),
287 MESON_SAR_ADC_CHAN(NUM_CHAN_4),
288 MESON_SAR_ADC_CHAN(NUM_CHAN_5),
289 MESON_SAR_ADC_CHAN(NUM_CHAN_6),
290 MESON_SAR_ADC_CHAN(NUM_CHAN_7),
291 MESON_SAR_ADC_MUX(NUM_MUX_0_VSS, 0),
292 MESON_SAR_ADC_MUX(NUM_MUX_1_VDD_DIV4, 1),
293 MESON_SAR_ADC_MUX(NUM_MUX_2_VDD_DIV2, 2),
294 MESON_SAR_ADC_MUX(NUM_MUX_3_VDD_MUL3_DIV4, 3),
295 MESON_SAR_ADC_MUX(NUM_MUX_4_VDD, 4),
298 static const struct iio_chan_spec meson_sar_adc_and_temp_iio_channels[] = {
299 MESON_SAR_ADC_CHAN(NUM_CHAN_0),
300 MESON_SAR_ADC_CHAN(NUM_CHAN_1),
301 MESON_SAR_ADC_CHAN(NUM_CHAN_2),
302 MESON_SAR_ADC_CHAN(NUM_CHAN_3),
303 MESON_SAR_ADC_CHAN(NUM_CHAN_4),
304 MESON_SAR_ADC_CHAN(NUM_CHAN_5),
305 MESON_SAR_ADC_CHAN(NUM_CHAN_6),
306 MESON_SAR_ADC_CHAN(NUM_CHAN_7),
307 MESON_SAR_ADC_TEMP_CHAN(NUM_CHAN_TEMP),
308 MESON_SAR_ADC_MUX(NUM_MUX_0_VSS, 0),
309 MESON_SAR_ADC_MUX(NUM_MUX_1_VDD_DIV4, 1),
310 MESON_SAR_ADC_MUX(NUM_MUX_2_VDD_DIV2, 2),
311 MESON_SAR_ADC_MUX(NUM_MUX_3_VDD_MUL3_DIV4, 3),
312 MESON_SAR_ADC_MUX(NUM_MUX_4_VDD, 4),
315 struct meson_sar_adc_param {
316 bool has_bl30_integration;
317 unsigned long clock_rate;
318 u32 bandgap_reg;
319 unsigned int resolution;
320 const struct regmap_config *regmap_config;
321 u8 temperature_trimming_bits;
322 unsigned int temperature_multiplier;
323 unsigned int temperature_divider;
324 u8 disable_ring_counter;
325 bool has_reg11;
326 bool has_vref_select;
327 u8 vref_select;
328 u8 cmv_select;
329 u8 adc_eoc;
330 enum meson_sar_adc_vref_sel vref_volatge;
333 struct meson_sar_adc_data {
334 const struct meson_sar_adc_param *param;
335 const char *name;
338 struct meson_sar_adc_priv {
339 struct regmap *regmap;
340 struct regulator *vref;
341 const struct meson_sar_adc_param *param;
342 struct clk *clkin;
343 struct clk *core_clk;
344 struct clk *adc_sel_clk;
345 struct clk *adc_clk;
346 struct clk_gate clk_gate;
347 struct clk *adc_div_clk;
348 struct clk_divider clk_div;
349 struct completion done;
350 /* lock to protect against multiple access to the device */
351 struct mutex lock;
352 int calibbias;
353 int calibscale;
354 struct regmap *tsc_regmap;
355 bool temperature_sensor_calibrated;
356 u8 temperature_sensor_coefficient;
357 u16 temperature_sensor_adc_val;
358 enum meson_sar_adc_chan7_mux_sel chan7_mux_sel;
361 static const struct regmap_config meson_sar_adc_regmap_config_gxbb = {
362 .reg_bits = 8,
363 .val_bits = 32,
364 .reg_stride = 4,
365 .max_register = MESON_SAR_ADC_REG13,
368 static const struct regmap_config meson_sar_adc_regmap_config_meson8 = {
369 .reg_bits = 8,
370 .val_bits = 32,
371 .reg_stride = 4,
372 .max_register = MESON_SAR_ADC_DELTA_10,
375 static const struct iio_chan_spec *
376 find_channel_by_num(struct iio_dev *indio_dev, int num)
378 int i;
380 for (i = 0; i < indio_dev->num_channels; i++)
381 if (indio_dev->channels[i].channel == num)
382 return &indio_dev->channels[i];
383 return NULL;
386 static unsigned int meson_sar_adc_get_fifo_count(struct iio_dev *indio_dev)
388 struct meson_sar_adc_priv *priv = iio_priv(indio_dev);
389 u32 regval;
391 regmap_read(priv->regmap, MESON_SAR_ADC_REG0, &regval);
393 return FIELD_GET(MESON_SAR_ADC_REG0_FIFO_COUNT_MASK, regval);
396 static int meson_sar_adc_calib_val(struct iio_dev *indio_dev, int val)
398 struct meson_sar_adc_priv *priv = iio_priv(indio_dev);
399 int tmp;
401 /* use val_calib = scale * val_raw + offset calibration function */
402 tmp = div_s64((s64)val * priv->calibscale, MILLION) + priv->calibbias;
404 return clamp(tmp, 0, (1 << priv->param->resolution) - 1);
407 static int meson_sar_adc_wait_busy_clear(struct iio_dev *indio_dev)
409 struct meson_sar_adc_priv *priv = iio_priv(indio_dev);
410 int val;
413 * NOTE: we need a small delay before reading the status, otherwise
414 * the sample engine may not have started internally (which would
415 * seem to us that sampling is already finished).
417 udelay(1);
418 return regmap_read_poll_timeout_atomic(priv->regmap, MESON_SAR_ADC_REG0, val,
419 !FIELD_GET(MESON_SAR_ADC_REG0_BUSY_MASK, val),
420 1, 10000);
423 static void meson_sar_adc_set_chan7_mux(struct iio_dev *indio_dev,
424 enum meson_sar_adc_chan7_mux_sel sel)
426 struct meson_sar_adc_priv *priv = iio_priv(indio_dev);
427 u32 regval;
429 regval = FIELD_PREP(MESON_SAR_ADC_REG3_CTRL_CHAN7_MUX_SEL_MASK, sel);
430 regmap_update_bits(priv->regmap, MESON_SAR_ADC_REG3,
431 MESON_SAR_ADC_REG3_CTRL_CHAN7_MUX_SEL_MASK, regval);
433 usleep_range(10, 20);
435 priv->chan7_mux_sel = sel;
438 static int meson_sar_adc_read_raw_sample(struct iio_dev *indio_dev,
439 const struct iio_chan_spec *chan,
440 int *val)
442 struct meson_sar_adc_priv *priv = iio_priv(indio_dev);
443 struct device *dev = indio_dev->dev.parent;
444 int regval, fifo_chan, fifo_val, count;
446 if (!wait_for_completion_timeout(&priv->done,
447 msecs_to_jiffies(MESON_SAR_ADC_TIMEOUT)))
448 return -ETIMEDOUT;
450 count = meson_sar_adc_get_fifo_count(indio_dev);
451 if (count != 1) {
452 dev_err(dev, "ADC FIFO has %d element(s) instead of one\n", count);
453 return -EINVAL;
456 regmap_read(priv->regmap, MESON_SAR_ADC_FIFO_RD, &regval);
457 fifo_chan = FIELD_GET(MESON_SAR_ADC_FIFO_RD_CHAN_ID_MASK, regval);
458 if (fifo_chan != chan->address) {
459 dev_err(dev, "ADC FIFO entry belongs to channel %d instead of %lu\n",
460 fifo_chan, chan->address);
461 return -EINVAL;
464 fifo_val = FIELD_GET(MESON_SAR_ADC_FIFO_RD_SAMPLE_VALUE_MASK, regval);
465 fifo_val &= GENMASK(priv->param->resolution - 1, 0);
466 *val = meson_sar_adc_calib_val(indio_dev, fifo_val);
468 return 0;
471 static void meson_sar_adc_set_averaging(struct iio_dev *indio_dev,
472 const struct iio_chan_spec *chan,
473 enum meson_sar_adc_avg_mode mode,
474 enum meson_sar_adc_num_samples samples)
476 struct meson_sar_adc_priv *priv = iio_priv(indio_dev);
477 int val, address = chan->address;
479 val = samples << MESON_SAR_ADC_AVG_CNTL_NUM_SAMPLES_SHIFT(address);
480 regmap_update_bits(priv->regmap, MESON_SAR_ADC_AVG_CNTL,
481 MESON_SAR_ADC_AVG_CNTL_NUM_SAMPLES_MASK(address),
482 val);
484 val = mode << MESON_SAR_ADC_AVG_CNTL_AVG_MODE_SHIFT(address);
485 regmap_update_bits(priv->regmap, MESON_SAR_ADC_AVG_CNTL,
486 MESON_SAR_ADC_AVG_CNTL_AVG_MODE_MASK(address), val);
489 static void meson_sar_adc_enable_channel(struct iio_dev *indio_dev,
490 const struct iio_chan_spec *chan)
492 struct meson_sar_adc_priv *priv = iio_priv(indio_dev);
493 u32 regval;
496 * the SAR ADC engine allows sampling multiple channels at the same
497 * time. to keep it simple we're only working with one *internal*
498 * channel, which starts counting at index 0 (which means: count = 1).
500 regval = FIELD_PREP(MESON_SAR_ADC_CHAN_LIST_MAX_INDEX_MASK, 0);
501 regmap_update_bits(priv->regmap, MESON_SAR_ADC_CHAN_LIST,
502 MESON_SAR_ADC_CHAN_LIST_MAX_INDEX_MASK, regval);
504 /* map channel index 0 to the channel which we want to read */
505 regval = FIELD_PREP(MESON_SAR_ADC_CHAN_LIST_ENTRY_MASK(0),
506 chan->address);
507 regmap_update_bits(priv->regmap, MESON_SAR_ADC_CHAN_LIST,
508 MESON_SAR_ADC_CHAN_LIST_ENTRY_MASK(0), regval);
510 regval = FIELD_PREP(MESON_SAR_ADC_DETECT_IDLE_SW_DETECT_MUX_MASK,
511 chan->address);
512 regmap_update_bits(priv->regmap, MESON_SAR_ADC_DETECT_IDLE_SW,
513 MESON_SAR_ADC_DETECT_IDLE_SW_DETECT_MUX_MASK,
514 regval);
516 regval = FIELD_PREP(MESON_SAR_ADC_DETECT_IDLE_SW_IDLE_MUX_SEL_MASK,
517 chan->address);
518 regmap_update_bits(priv->regmap, MESON_SAR_ADC_DETECT_IDLE_SW,
519 MESON_SAR_ADC_DETECT_IDLE_SW_IDLE_MUX_SEL_MASK,
520 regval);
522 if (chan->address == MESON_SAR_ADC_VOLTAGE_AND_TEMP_CHANNEL) {
523 if (chan->type == IIO_TEMP)
524 regval = MESON_SAR_ADC_DELTA_10_TEMP_SEL;
525 else
526 regval = 0;
528 regmap_update_bits(priv->regmap,
529 MESON_SAR_ADC_DELTA_10,
530 MESON_SAR_ADC_DELTA_10_TEMP_SEL, regval);
531 } else if (chan->address == MESON_SAR_ADC_VOLTAGE_AND_MUX_CHANNEL) {
532 enum meson_sar_adc_chan7_mux_sel sel;
534 if (chan->channel == NUM_CHAN_7)
535 sel = CHAN7_MUX_CH7_INPUT;
536 else
537 sel = chan7_mux_values[chan->channel - NUM_MUX_0_VSS];
538 if (sel != priv->chan7_mux_sel)
539 meson_sar_adc_set_chan7_mux(indio_dev, sel);
543 static void meson_sar_adc_start_sample_engine(struct iio_dev *indio_dev)
545 struct meson_sar_adc_priv *priv = iio_priv(indio_dev);
547 reinit_completion(&priv->done);
549 regmap_set_bits(priv->regmap, MESON_SAR_ADC_REG0,
550 MESON_SAR_ADC_REG0_FIFO_IRQ_EN);
552 regmap_set_bits(priv->regmap, MESON_SAR_ADC_REG0,
553 MESON_SAR_ADC_REG0_SAMPLE_ENGINE_ENABLE);
555 regmap_set_bits(priv->regmap, MESON_SAR_ADC_REG0,
556 MESON_SAR_ADC_REG0_SAMPLING_START);
559 static void meson_sar_adc_stop_sample_engine(struct iio_dev *indio_dev)
561 struct meson_sar_adc_priv *priv = iio_priv(indio_dev);
563 regmap_clear_bits(priv->regmap, MESON_SAR_ADC_REG0,
564 MESON_SAR_ADC_REG0_FIFO_IRQ_EN);
566 regmap_set_bits(priv->regmap, MESON_SAR_ADC_REG0,
567 MESON_SAR_ADC_REG0_SAMPLING_STOP);
569 /* wait until all modules are stopped */
570 meson_sar_adc_wait_busy_clear(indio_dev);
572 regmap_clear_bits(priv->regmap, MESON_SAR_ADC_REG0,
573 MESON_SAR_ADC_REG0_SAMPLE_ENGINE_ENABLE);
576 static int meson_sar_adc_lock(struct iio_dev *indio_dev)
578 struct meson_sar_adc_priv *priv = iio_priv(indio_dev);
579 int val, ret;
581 mutex_lock(&priv->lock);
583 if (priv->param->has_bl30_integration) {
584 /* prevent BL30 from using the SAR ADC while we are using it */
585 regmap_set_bits(priv->regmap, MESON_SAR_ADC_DELAY,
586 MESON_SAR_ADC_DELAY_KERNEL_BUSY);
588 udelay(1);
591 * wait until BL30 releases it's lock (so we can use the SAR
592 * ADC)
594 ret = regmap_read_poll_timeout_atomic(priv->regmap, MESON_SAR_ADC_DELAY, val,
595 !(val & MESON_SAR_ADC_DELAY_BL30_BUSY),
596 1, 10000);
597 if (ret) {
598 mutex_unlock(&priv->lock);
599 return ret;
603 return 0;
606 static void meson_sar_adc_unlock(struct iio_dev *indio_dev)
608 struct meson_sar_adc_priv *priv = iio_priv(indio_dev);
610 if (priv->param->has_bl30_integration)
611 /* allow BL30 to use the SAR ADC again */
612 regmap_clear_bits(priv->regmap, MESON_SAR_ADC_DELAY,
613 MESON_SAR_ADC_DELAY_KERNEL_BUSY);
615 mutex_unlock(&priv->lock);
618 static void meson_sar_adc_clear_fifo(struct iio_dev *indio_dev)
620 struct meson_sar_adc_priv *priv = iio_priv(indio_dev);
621 unsigned int count, tmp;
623 for (count = 0; count < MESON_SAR_ADC_MAX_FIFO_SIZE; count++) {
624 if (!meson_sar_adc_get_fifo_count(indio_dev))
625 break;
627 regmap_read(priv->regmap, MESON_SAR_ADC_FIFO_RD, &tmp);
631 static int meson_sar_adc_get_sample(struct iio_dev *indio_dev,
632 const struct iio_chan_spec *chan,
633 enum meson_sar_adc_avg_mode avg_mode,
634 enum meson_sar_adc_num_samples avg_samples,
635 int *val)
637 struct meson_sar_adc_priv *priv = iio_priv(indio_dev);
638 struct device *dev = indio_dev->dev.parent;
639 int ret;
641 if (chan->type == IIO_TEMP && !priv->temperature_sensor_calibrated)
642 return -ENOTSUPP;
644 ret = meson_sar_adc_lock(indio_dev);
645 if (ret)
646 return ret;
648 /* clear the FIFO to make sure we're not reading old values */
649 meson_sar_adc_clear_fifo(indio_dev);
651 meson_sar_adc_set_averaging(indio_dev, chan, avg_mode, avg_samples);
653 meson_sar_adc_enable_channel(indio_dev, chan);
655 meson_sar_adc_start_sample_engine(indio_dev);
656 ret = meson_sar_adc_read_raw_sample(indio_dev, chan, val);
657 meson_sar_adc_stop_sample_engine(indio_dev);
659 meson_sar_adc_unlock(indio_dev);
661 if (ret) {
662 dev_warn(dev, "failed to read sample for channel %lu: %d\n",
663 chan->address, ret);
664 return ret;
667 return IIO_VAL_INT;
670 static int meson_sar_adc_iio_info_read_raw(struct iio_dev *indio_dev,
671 const struct iio_chan_spec *chan,
672 int *val, int *val2, long mask)
674 struct meson_sar_adc_priv *priv = iio_priv(indio_dev);
675 struct device *dev = indio_dev->dev.parent;
676 int ret;
678 switch (mask) {
679 case IIO_CHAN_INFO_RAW:
680 return meson_sar_adc_get_sample(indio_dev, chan, NO_AVERAGING,
681 ONE_SAMPLE, val);
683 case IIO_CHAN_INFO_AVERAGE_RAW:
684 return meson_sar_adc_get_sample(indio_dev, chan,
685 MEAN_AVERAGING, EIGHT_SAMPLES,
686 val);
688 case IIO_CHAN_INFO_SCALE:
689 if (chan->type == IIO_VOLTAGE) {
690 ret = regulator_get_voltage(priv->vref);
691 if (ret < 0) {
692 dev_err(dev, "failed to get vref voltage: %d\n", ret);
693 return ret;
696 *val = ret / 1000;
697 *val2 = priv->param->resolution;
698 return IIO_VAL_FRACTIONAL_LOG2;
699 } else if (chan->type == IIO_TEMP) {
700 /* SoC specific multiplier and divider */
701 *val = priv->param->temperature_multiplier;
702 *val2 = priv->param->temperature_divider;
704 /* celsius to millicelsius */
705 *val *= 1000;
707 return IIO_VAL_FRACTIONAL;
708 } else {
709 return -EINVAL;
712 case IIO_CHAN_INFO_CALIBBIAS:
713 *val = priv->calibbias;
714 return IIO_VAL_INT;
716 case IIO_CHAN_INFO_CALIBSCALE:
717 *val = priv->calibscale / MILLION;
718 *val2 = priv->calibscale % MILLION;
719 return IIO_VAL_INT_PLUS_MICRO;
721 case IIO_CHAN_INFO_OFFSET:
722 *val = DIV_ROUND_CLOSEST(MESON_SAR_ADC_TEMP_OFFSET *
723 priv->param->temperature_divider,
724 priv->param->temperature_multiplier);
725 *val -= priv->temperature_sensor_adc_val;
726 return IIO_VAL_INT;
728 default:
729 return -EINVAL;
733 static int meson_sar_adc_clk_init(struct iio_dev *indio_dev,
734 void __iomem *base)
736 struct meson_sar_adc_priv *priv = iio_priv(indio_dev);
737 struct device *dev = indio_dev->dev.parent;
738 struct clk_init_data init;
739 const char *clk_parents[1];
741 init.name = devm_kasprintf(dev, GFP_KERNEL, "%s#adc_div", dev_name(dev));
742 if (!init.name)
743 return -ENOMEM;
745 init.flags = 0;
746 init.ops = &clk_divider_ops;
747 clk_parents[0] = __clk_get_name(priv->clkin);
748 init.parent_names = clk_parents;
749 init.num_parents = 1;
751 priv->clk_div.reg = base + MESON_SAR_ADC_REG3;
752 priv->clk_div.shift = MESON_SAR_ADC_REG3_ADC_CLK_DIV_SHIFT;
753 priv->clk_div.width = MESON_SAR_ADC_REG3_ADC_CLK_DIV_WIDTH;
754 priv->clk_div.hw.init = &init;
755 priv->clk_div.flags = 0;
757 priv->adc_div_clk = devm_clk_register(dev, &priv->clk_div.hw);
758 if (WARN_ON(IS_ERR(priv->adc_div_clk)))
759 return PTR_ERR(priv->adc_div_clk);
761 init.name = devm_kasprintf(dev, GFP_KERNEL, "%s#adc_en", dev_name(dev));
762 if (!init.name)
763 return -ENOMEM;
765 init.flags = CLK_SET_RATE_PARENT;
766 init.ops = &clk_gate_ops;
767 clk_parents[0] = __clk_get_name(priv->adc_div_clk);
768 init.parent_names = clk_parents;
769 init.num_parents = 1;
771 priv->clk_gate.reg = base + MESON_SAR_ADC_REG3;
772 priv->clk_gate.bit_idx = __ffs(MESON_SAR_ADC_REG3_CLK_EN);
773 priv->clk_gate.hw.init = &init;
775 priv->adc_clk = devm_clk_register(dev, &priv->clk_gate.hw);
776 if (WARN_ON(IS_ERR(priv->adc_clk)))
777 return PTR_ERR(priv->adc_clk);
779 return 0;
782 static int meson_sar_adc_temp_sensor_init(struct iio_dev *indio_dev)
784 struct meson_sar_adc_priv *priv = iio_priv(indio_dev);
785 u8 *buf, trimming_bits, trimming_mask, upper_adc_val;
786 struct device *dev = indio_dev->dev.parent;
787 struct nvmem_cell *temperature_calib;
788 size_t read_len;
789 int ret;
791 temperature_calib = devm_nvmem_cell_get(dev, "temperature_calib");
792 if (IS_ERR(temperature_calib)) {
793 ret = PTR_ERR(temperature_calib);
796 * leave the temperature sensor disabled if no calibration data
797 * was passed via nvmem-cells.
799 if (ret == -ENODEV)
800 return 0;
802 return dev_err_probe(dev, ret, "failed to get temperature_calib cell\n");
805 priv->tsc_regmap = syscon_regmap_lookup_by_phandle(dev->of_node, "amlogic,hhi-sysctrl");
806 if (IS_ERR(priv->tsc_regmap))
807 return dev_err_probe(dev, PTR_ERR(priv->tsc_regmap),
808 "failed to get amlogic,hhi-sysctrl regmap\n");
810 read_len = MESON_SAR_ADC_EFUSE_BYTES;
811 buf = nvmem_cell_read(temperature_calib, &read_len);
812 if (IS_ERR(buf))
813 return dev_err_probe(dev, PTR_ERR(buf), "failed to read temperature_calib cell\n");
814 if (read_len != MESON_SAR_ADC_EFUSE_BYTES) {
815 kfree(buf);
816 return dev_err_probe(dev, -EINVAL, "invalid read size of temperature_calib cell\n");
819 trimming_bits = priv->param->temperature_trimming_bits;
820 trimming_mask = BIT(trimming_bits) - 1;
822 priv->temperature_sensor_calibrated =
823 buf[3] & MESON_SAR_ADC_EFUSE_BYTE3_IS_CALIBRATED;
824 priv->temperature_sensor_coefficient = buf[2] & trimming_mask;
826 upper_adc_val = FIELD_GET(MESON_SAR_ADC_EFUSE_BYTE3_UPPER_ADC_VAL,
827 buf[3]);
829 priv->temperature_sensor_adc_val = buf[2];
830 priv->temperature_sensor_adc_val |= upper_adc_val << BITS_PER_BYTE;
831 priv->temperature_sensor_adc_val >>= trimming_bits;
833 kfree(buf);
835 return 0;
838 static int meson_sar_adc_init(struct iio_dev *indio_dev)
840 struct meson_sar_adc_priv *priv = iio_priv(indio_dev);
841 struct device *dev = indio_dev->dev.parent;
842 int regval, i, ret;
845 * make sure we start at CH7 input since the other muxes are only used
846 * for internal calibration.
848 meson_sar_adc_set_chan7_mux(indio_dev, CHAN7_MUX_CH7_INPUT);
850 if (priv->param->has_bl30_integration) {
852 * leave sampling delay and the input clocks as configured by
853 * BL30 to make sure BL30 gets the values it expects when
854 * reading the temperature sensor.
856 regmap_read(priv->regmap, MESON_SAR_ADC_REG3, &regval);
857 if (regval & MESON_SAR_ADC_REG3_BL30_INITIALIZED)
858 return 0;
861 meson_sar_adc_stop_sample_engine(indio_dev);
864 * disable this bit as seems to be only relevant for Meson6 (based
865 * on the vendor driver), which we don't support at the moment.
867 regmap_clear_bits(priv->regmap, MESON_SAR_ADC_REG0,
868 MESON_SAR_ADC_REG0_ADC_TEMP_SEN_SEL);
870 /* disable all channels by default */
871 regmap_write(priv->regmap, MESON_SAR_ADC_CHAN_LIST, 0x0);
873 regmap_clear_bits(priv->regmap, MESON_SAR_ADC_REG3,
874 MESON_SAR_ADC_REG3_CTRL_SAMPLING_CLOCK_PHASE);
875 regmap_set_bits(priv->regmap, MESON_SAR_ADC_REG3,
876 MESON_SAR_ADC_REG3_CNTL_USE_SC_DLY);
878 /* delay between two samples = (10+1) * 1uS */
879 regmap_update_bits(priv->regmap, MESON_SAR_ADC_DELAY,
880 MESON_SAR_ADC_DELAY_INPUT_DLY_CNT_MASK,
881 FIELD_PREP(MESON_SAR_ADC_DELAY_SAMPLE_DLY_CNT_MASK,
882 10));
883 regmap_update_bits(priv->regmap, MESON_SAR_ADC_DELAY,
884 MESON_SAR_ADC_DELAY_SAMPLE_DLY_SEL_MASK,
885 FIELD_PREP(MESON_SAR_ADC_DELAY_SAMPLE_DLY_SEL_MASK,
886 0));
888 /* delay between two samples = (10+1) * 1uS */
889 regmap_update_bits(priv->regmap, MESON_SAR_ADC_DELAY,
890 MESON_SAR_ADC_DELAY_INPUT_DLY_CNT_MASK,
891 FIELD_PREP(MESON_SAR_ADC_DELAY_INPUT_DLY_CNT_MASK,
892 10));
893 regmap_update_bits(priv->regmap, MESON_SAR_ADC_DELAY,
894 MESON_SAR_ADC_DELAY_INPUT_DLY_SEL_MASK,
895 FIELD_PREP(MESON_SAR_ADC_DELAY_INPUT_DLY_SEL_MASK,
896 1));
899 * set up the input channel muxes in MESON_SAR_ADC_CHAN_10_SW
900 * (0 = SAR_ADC_CH0, 1 = SAR_ADC_CH1)
902 regval = FIELD_PREP(MESON_SAR_ADC_CHAN_10_SW_CHAN0_MUX_SEL_MASK, 0);
903 regmap_update_bits(priv->regmap, MESON_SAR_ADC_CHAN_10_SW,
904 MESON_SAR_ADC_CHAN_10_SW_CHAN0_MUX_SEL_MASK,
905 regval);
906 regval = FIELD_PREP(MESON_SAR_ADC_CHAN_10_SW_CHAN1_MUX_SEL_MASK, 1);
907 regmap_update_bits(priv->regmap, MESON_SAR_ADC_CHAN_10_SW,
908 MESON_SAR_ADC_CHAN_10_SW_CHAN1_MUX_SEL_MASK,
909 regval);
911 regmap_set_bits(priv->regmap, MESON_SAR_ADC_CHAN_10_SW,
912 MESON_SAR_ADC_CHAN_10_SW_CHAN0_XP_DRIVE_SW);
914 regmap_set_bits(priv->regmap, MESON_SAR_ADC_CHAN_10_SW,
915 MESON_SAR_ADC_CHAN_10_SW_CHAN0_YP_DRIVE_SW);
917 regmap_set_bits(priv->regmap, MESON_SAR_ADC_CHAN_10_SW,
918 MESON_SAR_ADC_CHAN_10_SW_CHAN1_XP_DRIVE_SW);
920 regmap_set_bits(priv->regmap, MESON_SAR_ADC_CHAN_10_SW,
921 MESON_SAR_ADC_CHAN_10_SW_CHAN1_YP_DRIVE_SW);
924 * set up the input channel muxes in MESON_SAR_ADC_AUX_SW
925 * (2 = SAR_ADC_CH2, 3 = SAR_ADC_CH3, ...) and enable
926 * MESON_SAR_ADC_AUX_SW_YP_DRIVE_SW and
927 * MESON_SAR_ADC_AUX_SW_XP_DRIVE_SW like the vendor driver.
929 regval = 0;
930 for (i = 2; i <= 7; i++)
931 regval |= i << MESON_SAR_ADC_AUX_SW_MUX_SEL_CHAN_SHIFT(i);
932 regval |= MESON_SAR_ADC_AUX_SW_YP_DRIVE_SW;
933 regval |= MESON_SAR_ADC_AUX_SW_XP_DRIVE_SW;
934 regmap_write(priv->regmap, MESON_SAR_ADC_AUX_SW, regval);
936 if (priv->temperature_sensor_calibrated) {
937 regmap_set_bits(priv->regmap, MESON_SAR_ADC_DELTA_10,
938 MESON_SAR_ADC_DELTA_10_TS_REVE1);
939 regmap_set_bits(priv->regmap, MESON_SAR_ADC_DELTA_10,
940 MESON_SAR_ADC_DELTA_10_TS_REVE0);
943 * set bits [3:0] of the TSC (temperature sensor coefficient)
944 * to get the correct values when reading the temperature.
946 regval = FIELD_PREP(MESON_SAR_ADC_DELTA_10_TS_C_MASK,
947 priv->temperature_sensor_coefficient);
948 regmap_update_bits(priv->regmap, MESON_SAR_ADC_DELTA_10,
949 MESON_SAR_ADC_DELTA_10_TS_C_MASK, regval);
951 if (priv->param->temperature_trimming_bits == 5) {
952 if (priv->temperature_sensor_coefficient & BIT(4))
953 regval = MESON_HHI_DPLL_TOP_0_TSC_BIT4;
954 else
955 regval = 0;
958 * bit [4] (the 5th bit when starting to count at 1)
959 * of the TSC is located in the HHI register area.
961 regmap_update_bits(priv->tsc_regmap,
962 MESON_HHI_DPLL_TOP_0,
963 MESON_HHI_DPLL_TOP_0_TSC_BIT4,
964 regval);
966 } else {
967 regmap_clear_bits(priv->regmap, MESON_SAR_ADC_DELTA_10,
968 MESON_SAR_ADC_DELTA_10_TS_REVE1);
969 regmap_clear_bits(priv->regmap, MESON_SAR_ADC_DELTA_10,
970 MESON_SAR_ADC_DELTA_10_TS_REVE0);
973 regval = FIELD_PREP(MESON_SAR_ADC_REG3_CTRL_CONT_RING_COUNTER_EN,
974 priv->param->disable_ring_counter);
975 regmap_update_bits(priv->regmap, MESON_SAR_ADC_REG3,
976 MESON_SAR_ADC_REG3_CTRL_CONT_RING_COUNTER_EN,
977 regval);
979 if (priv->param->has_reg11) {
980 regval = FIELD_PREP(MESON_SAR_ADC_REG11_EOC, priv->param->adc_eoc);
981 regmap_update_bits(priv->regmap, MESON_SAR_ADC_REG11,
982 MESON_SAR_ADC_REG11_EOC, regval);
984 if (priv->param->has_vref_select) {
985 regval = FIELD_PREP(MESON_SAR_ADC_REG11_VREF_SEL,
986 priv->param->vref_select);
987 regmap_update_bits(priv->regmap, MESON_SAR_ADC_REG11,
988 MESON_SAR_ADC_REG11_VREF_SEL, regval);
991 regval = FIELD_PREP(MESON_SAR_ADC_REG11_VREF_VOLTAGE,
992 priv->param->vref_volatge);
993 regmap_update_bits(priv->regmap, MESON_SAR_ADC_REG11,
994 MESON_SAR_ADC_REG11_VREF_VOLTAGE, regval);
996 regval = FIELD_PREP(MESON_SAR_ADC_REG11_CMV_SEL,
997 priv->param->cmv_select);
998 regmap_update_bits(priv->regmap, MESON_SAR_ADC_REG11,
999 MESON_SAR_ADC_REG11_CMV_SEL, regval);
1002 ret = clk_set_parent(priv->adc_sel_clk, priv->clkin);
1003 if (ret)
1004 return dev_err_probe(dev, ret, "failed to set adc parent to clkin\n");
1006 ret = clk_set_rate(priv->adc_clk, priv->param->clock_rate);
1007 if (ret)
1008 return dev_err_probe(dev, ret, "failed to set adc clock rate\n");
1010 return 0;
1013 static void meson_sar_adc_set_bandgap(struct iio_dev *indio_dev, bool on_off)
1015 struct meson_sar_adc_priv *priv = iio_priv(indio_dev);
1016 const struct meson_sar_adc_param *param = priv->param;
1017 u32 enable_mask;
1019 if (param->bandgap_reg == MESON_SAR_ADC_REG11)
1020 enable_mask = MESON_SAR_ADC_REG11_BANDGAP_EN;
1021 else
1022 enable_mask = MESON_SAR_ADC_DELTA_10_TS_VBG_EN;
1024 regmap_update_bits(priv->regmap, param->bandgap_reg, enable_mask,
1025 on_off ? enable_mask : 0);
1028 static int meson_sar_adc_hw_enable(struct iio_dev *indio_dev)
1030 struct meson_sar_adc_priv *priv = iio_priv(indio_dev);
1031 struct device *dev = indio_dev->dev.parent;
1032 int ret;
1033 u32 regval;
1035 ret = meson_sar_adc_lock(indio_dev);
1036 if (ret) {
1037 dev_err(dev, "failed to lock adc\n");
1038 goto err_lock;
1041 ret = regulator_enable(priv->vref);
1042 if (ret < 0) {
1043 dev_err(dev, "failed to enable vref regulator\n");
1044 goto err_vref;
1047 regval = FIELD_PREP(MESON_SAR_ADC_REG0_FIFO_CNT_IRQ_MASK, 1);
1048 regmap_update_bits(priv->regmap, MESON_SAR_ADC_REG0,
1049 MESON_SAR_ADC_REG0_FIFO_CNT_IRQ_MASK, regval);
1051 meson_sar_adc_set_bandgap(indio_dev, true);
1053 regmap_set_bits(priv->regmap, MESON_SAR_ADC_REG3,
1054 MESON_SAR_ADC_REG3_ADC_EN);
1056 udelay(5);
1058 ret = clk_prepare_enable(priv->adc_clk);
1059 if (ret) {
1060 dev_err(dev, "failed to enable adc clk\n");
1061 goto err_adc_clk;
1064 meson_sar_adc_unlock(indio_dev);
1066 return 0;
1068 err_adc_clk:
1069 regmap_clear_bits(priv->regmap, MESON_SAR_ADC_REG3,
1070 MESON_SAR_ADC_REG3_ADC_EN);
1071 meson_sar_adc_set_bandgap(indio_dev, false);
1072 regulator_disable(priv->vref);
1073 err_vref:
1074 meson_sar_adc_unlock(indio_dev);
1075 err_lock:
1076 return ret;
1079 static void meson_sar_adc_hw_disable(struct iio_dev *indio_dev)
1081 struct meson_sar_adc_priv *priv = iio_priv(indio_dev);
1082 int ret;
1085 * If taking the lock fails we have to assume that BL30 is broken. The
1086 * best we can do then is to release the resources anyhow.
1088 ret = meson_sar_adc_lock(indio_dev);
1089 if (ret)
1090 dev_err(indio_dev->dev.parent, "Failed to lock ADC (%pE)\n", ERR_PTR(ret));
1092 clk_disable_unprepare(priv->adc_clk);
1094 regmap_clear_bits(priv->regmap, MESON_SAR_ADC_REG3,
1095 MESON_SAR_ADC_REG3_ADC_EN);
1097 meson_sar_adc_set_bandgap(indio_dev, false);
1099 regulator_disable(priv->vref);
1101 if (!ret)
1102 meson_sar_adc_unlock(indio_dev);
1105 static irqreturn_t meson_sar_adc_irq(int irq, void *data)
1107 struct iio_dev *indio_dev = data;
1108 struct meson_sar_adc_priv *priv = iio_priv(indio_dev);
1109 unsigned int cnt, threshold;
1110 u32 regval;
1112 regmap_read(priv->regmap, MESON_SAR_ADC_REG0, &regval);
1113 cnt = FIELD_GET(MESON_SAR_ADC_REG0_FIFO_COUNT_MASK, regval);
1114 threshold = FIELD_GET(MESON_SAR_ADC_REG0_FIFO_CNT_IRQ_MASK, regval);
1116 if (cnt < threshold)
1117 return IRQ_NONE;
1119 complete(&priv->done);
1121 return IRQ_HANDLED;
1124 static int meson_sar_adc_calib(struct iio_dev *indio_dev)
1126 struct meson_sar_adc_priv *priv = iio_priv(indio_dev);
1127 int ret, nominal0, nominal1, value0, value1;
1129 /* use points 25% and 75% for calibration */
1130 nominal0 = (1 << priv->param->resolution) / 4;
1131 nominal1 = (1 << priv->param->resolution) * 3 / 4;
1133 meson_sar_adc_set_chan7_mux(indio_dev, CHAN7_MUX_VDD_DIV4);
1134 usleep_range(10, 20);
1135 ret = meson_sar_adc_get_sample(indio_dev,
1136 find_channel_by_num(indio_dev,
1137 NUM_MUX_1_VDD_DIV4),
1138 MEAN_AVERAGING, EIGHT_SAMPLES, &value0);
1139 if (ret < 0)
1140 goto out;
1142 meson_sar_adc_set_chan7_mux(indio_dev, CHAN7_MUX_VDD_MUL3_DIV4);
1143 usleep_range(10, 20);
1144 ret = meson_sar_adc_get_sample(indio_dev,
1145 find_channel_by_num(indio_dev,
1146 NUM_MUX_3_VDD_MUL3_DIV4),
1147 MEAN_AVERAGING, EIGHT_SAMPLES, &value1);
1148 if (ret < 0)
1149 goto out;
1151 if (value1 <= value0) {
1152 ret = -EINVAL;
1153 goto out;
1156 priv->calibscale = div_s64((nominal1 - nominal0) * (s64)MILLION,
1157 value1 - value0);
1158 priv->calibbias = nominal0 - div_s64((s64)value0 * priv->calibscale,
1159 MILLION);
1160 ret = 0;
1161 out:
1162 meson_sar_adc_set_chan7_mux(indio_dev, CHAN7_MUX_CH7_INPUT);
1164 return ret;
1167 static int read_label(struct iio_dev *indio_dev,
1168 struct iio_chan_spec const *chan,
1169 char *label)
1171 if (chan->type == IIO_TEMP)
1172 return sprintf(label, "temp-sensor\n");
1173 if (chan->type == IIO_VOLTAGE && chan->channel >= NUM_MUX_0_VSS)
1174 return sprintf(label, "%s\n",
1175 chan7_mux_names[chan->channel - NUM_MUX_0_VSS]);
1176 if (chan->type == IIO_VOLTAGE)
1177 return sprintf(label, "channel-%d\n", chan->channel);
1178 return 0;
1181 static const struct iio_info meson_sar_adc_iio_info = {
1182 .read_raw = meson_sar_adc_iio_info_read_raw,
1183 .read_label = read_label,
1186 static const struct meson_sar_adc_param meson_sar_adc_meson8_param = {
1187 .has_bl30_integration = false,
1188 .clock_rate = 1150000,
1189 .bandgap_reg = MESON_SAR_ADC_DELTA_10,
1190 .regmap_config = &meson_sar_adc_regmap_config_meson8,
1191 .resolution = 10,
1192 .temperature_trimming_bits = 4,
1193 .temperature_multiplier = 18 * 10000,
1194 .temperature_divider = 1024 * 10 * 85,
1197 static const struct meson_sar_adc_param meson_sar_adc_meson8b_param = {
1198 .has_bl30_integration = false,
1199 .clock_rate = 1150000,
1200 .bandgap_reg = MESON_SAR_ADC_DELTA_10,
1201 .regmap_config = &meson_sar_adc_regmap_config_meson8,
1202 .resolution = 10,
1203 .temperature_trimming_bits = 5,
1204 .temperature_multiplier = 10,
1205 .temperature_divider = 32,
1208 static const struct meson_sar_adc_param meson_sar_adc_gxbb_param = {
1209 .has_bl30_integration = true,
1210 .clock_rate = 1200000,
1211 .bandgap_reg = MESON_SAR_ADC_REG11,
1212 .regmap_config = &meson_sar_adc_regmap_config_gxbb,
1213 .resolution = 10,
1214 .has_reg11 = true,
1215 .vref_volatge = 1,
1216 .cmv_select = 1,
1219 static const struct meson_sar_adc_param meson_sar_adc_gxl_param = {
1220 .has_bl30_integration = true,
1221 .clock_rate = 1200000,
1222 .bandgap_reg = MESON_SAR_ADC_REG11,
1223 .regmap_config = &meson_sar_adc_regmap_config_gxbb,
1224 .resolution = 12,
1225 .disable_ring_counter = 1,
1226 .has_reg11 = true,
1227 .vref_volatge = 1,
1228 .cmv_select = 1,
1231 static const struct meson_sar_adc_param meson_sar_adc_axg_param = {
1232 .has_bl30_integration = true,
1233 .clock_rate = 1200000,
1234 .bandgap_reg = MESON_SAR_ADC_REG11,
1235 .regmap_config = &meson_sar_adc_regmap_config_gxbb,
1236 .resolution = 12,
1237 .disable_ring_counter = 1,
1238 .has_reg11 = true,
1239 .vref_volatge = 1,
1240 .has_vref_select = true,
1241 .vref_select = VREF_VDDA,
1242 .cmv_select = 1,
1245 static const struct meson_sar_adc_param meson_sar_adc_g12a_param = {
1246 .has_bl30_integration = false,
1247 .clock_rate = 1200000,
1248 .bandgap_reg = MESON_SAR_ADC_REG11,
1249 .regmap_config = &meson_sar_adc_regmap_config_gxbb,
1250 .resolution = 12,
1251 .disable_ring_counter = 1,
1252 .has_reg11 = true,
1253 .adc_eoc = 1,
1254 .has_vref_select = true,
1255 .vref_select = VREF_VDDA,
1258 static const struct meson_sar_adc_data meson_sar_adc_meson8_data = {
1259 .param = &meson_sar_adc_meson8_param,
1260 .name = "meson-meson8-saradc",
1263 static const struct meson_sar_adc_data meson_sar_adc_meson8b_data = {
1264 .param = &meson_sar_adc_meson8b_param,
1265 .name = "meson-meson8b-saradc",
1268 static const struct meson_sar_adc_data meson_sar_adc_meson8m2_data = {
1269 .param = &meson_sar_adc_meson8b_param,
1270 .name = "meson-meson8m2-saradc",
1273 static const struct meson_sar_adc_data meson_sar_adc_gxbb_data = {
1274 .param = &meson_sar_adc_gxbb_param,
1275 .name = "meson-gxbb-saradc",
1278 static const struct meson_sar_adc_data meson_sar_adc_gxl_data = {
1279 .param = &meson_sar_adc_gxl_param,
1280 .name = "meson-gxl-saradc",
1283 static const struct meson_sar_adc_data meson_sar_adc_gxm_data = {
1284 .param = &meson_sar_adc_gxl_param,
1285 .name = "meson-gxm-saradc",
1288 static const struct meson_sar_adc_data meson_sar_adc_axg_data = {
1289 .param = &meson_sar_adc_axg_param,
1290 .name = "meson-axg-saradc",
1293 static const struct meson_sar_adc_data meson_sar_adc_g12a_data = {
1294 .param = &meson_sar_adc_g12a_param,
1295 .name = "meson-g12a-saradc",
1298 static const struct of_device_id meson_sar_adc_of_match[] = {
1300 .compatible = "amlogic,meson8-saradc",
1301 .data = &meson_sar_adc_meson8_data,
1302 }, {
1303 .compatible = "amlogic,meson8b-saradc",
1304 .data = &meson_sar_adc_meson8b_data,
1305 }, {
1306 .compatible = "amlogic,meson8m2-saradc",
1307 .data = &meson_sar_adc_meson8m2_data,
1308 }, {
1309 .compatible = "amlogic,meson-gxbb-saradc",
1310 .data = &meson_sar_adc_gxbb_data,
1311 }, {
1312 .compatible = "amlogic,meson-gxl-saradc",
1313 .data = &meson_sar_adc_gxl_data,
1314 }, {
1315 .compatible = "amlogic,meson-gxm-saradc",
1316 .data = &meson_sar_adc_gxm_data,
1317 }, {
1318 .compatible = "amlogic,meson-axg-saradc",
1319 .data = &meson_sar_adc_axg_data,
1320 }, {
1321 .compatible = "amlogic,meson-g12a-saradc",
1322 .data = &meson_sar_adc_g12a_data,
1324 { /* sentinel */ }
1326 MODULE_DEVICE_TABLE(of, meson_sar_adc_of_match);
1328 static int meson_sar_adc_probe(struct platform_device *pdev)
1330 const struct meson_sar_adc_data *match_data;
1331 struct meson_sar_adc_priv *priv;
1332 struct device *dev = &pdev->dev;
1333 struct iio_dev *indio_dev;
1334 void __iomem *base;
1335 int irq, ret;
1337 indio_dev = devm_iio_device_alloc(dev, sizeof(*priv));
1338 if (!indio_dev)
1339 return dev_err_probe(dev, -ENOMEM, "failed allocating iio device\n");
1341 priv = iio_priv(indio_dev);
1342 init_completion(&priv->done);
1344 match_data = of_device_get_match_data(dev);
1345 if (!match_data)
1346 return dev_err_probe(dev, -ENODEV, "failed to get match data\n");
1348 priv->param = match_data->param;
1350 indio_dev->name = match_data->name;
1351 indio_dev->modes = INDIO_DIRECT_MODE;
1352 indio_dev->info = &meson_sar_adc_iio_info;
1354 base = devm_platform_ioremap_resource(pdev, 0);
1355 if (IS_ERR(base))
1356 return PTR_ERR(base);
1358 priv->regmap = devm_regmap_init_mmio(dev, base, priv->param->regmap_config);
1359 if (IS_ERR(priv->regmap))
1360 return dev_err_probe(dev, PTR_ERR(priv->regmap), "failed to init regmap\n");
1362 irq = irq_of_parse_and_map(dev->of_node, 0);
1363 if (!irq)
1364 return dev_err_probe(dev, -EINVAL, "failed to get irq\n");
1366 ret = devm_request_irq(dev, irq, meson_sar_adc_irq, IRQF_SHARED, dev_name(dev), indio_dev);
1367 if (ret)
1368 return dev_err_probe(dev, ret, "failed to request irq\n");
1370 priv->clkin = devm_clk_get(dev, "clkin");
1371 if (IS_ERR(priv->clkin))
1372 return dev_err_probe(dev, PTR_ERR(priv->clkin), "failed to get clkin\n");
1374 priv->core_clk = devm_clk_get_enabled(dev, "core");
1375 if (IS_ERR(priv->core_clk))
1376 return dev_err_probe(dev, PTR_ERR(priv->core_clk), "failed to get core clk\n");
1378 priv->adc_clk = devm_clk_get_optional(dev, "adc_clk");
1379 if (IS_ERR(priv->adc_clk))
1380 return dev_err_probe(dev, PTR_ERR(priv->adc_clk), "failed to get adc clk\n");
1382 priv->adc_sel_clk = devm_clk_get_optional(dev, "adc_sel");
1383 if (IS_ERR(priv->adc_sel_clk))
1384 return dev_err_probe(dev, PTR_ERR(priv->adc_sel_clk), "failed to get adc_sel clk\n");
1386 /* on pre-GXBB SoCs the SAR ADC itself provides the ADC clock: */
1387 if (!priv->adc_clk) {
1388 ret = meson_sar_adc_clk_init(indio_dev, base);
1389 if (ret)
1390 return dev_err_probe(dev, ret, "failed to init internal clk\n");
1393 priv->vref = devm_regulator_get(dev, "vref");
1394 if (IS_ERR(priv->vref))
1395 return dev_err_probe(dev, PTR_ERR(priv->vref), "failed to get vref regulator\n");
1397 priv->calibscale = MILLION;
1399 if (priv->param->temperature_trimming_bits) {
1400 ret = meson_sar_adc_temp_sensor_init(indio_dev);
1401 if (ret)
1402 return ret;
1405 if (priv->temperature_sensor_calibrated) {
1406 indio_dev->channels = meson_sar_adc_and_temp_iio_channels;
1407 indio_dev->num_channels =
1408 ARRAY_SIZE(meson_sar_adc_and_temp_iio_channels);
1409 } else {
1410 indio_dev->channels = meson_sar_adc_iio_channels;
1411 indio_dev->num_channels =
1412 ARRAY_SIZE(meson_sar_adc_iio_channels);
1415 ret = meson_sar_adc_init(indio_dev);
1416 if (ret)
1417 goto err;
1419 mutex_init(&priv->lock);
1421 ret = meson_sar_adc_hw_enable(indio_dev);
1422 if (ret)
1423 goto err;
1425 ret = meson_sar_adc_calib(indio_dev);
1426 if (ret)
1427 dev_warn(dev, "calibration failed\n");
1429 platform_set_drvdata(pdev, indio_dev);
1431 ret = iio_device_register(indio_dev);
1432 if (ret) {
1433 dev_err_probe(dev, ret, "failed to register iio device\n");
1434 goto err_hw;
1437 return 0;
1439 err_hw:
1440 meson_sar_adc_hw_disable(indio_dev);
1441 err:
1442 return ret;
1445 static void meson_sar_adc_remove(struct platform_device *pdev)
1447 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
1449 iio_device_unregister(indio_dev);
1451 meson_sar_adc_hw_disable(indio_dev);
1454 static int meson_sar_adc_suspend(struct device *dev)
1456 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1457 struct meson_sar_adc_priv *priv = iio_priv(indio_dev);
1459 meson_sar_adc_hw_disable(indio_dev);
1461 clk_disable_unprepare(priv->core_clk);
1463 return 0;
1466 static int meson_sar_adc_resume(struct device *dev)
1468 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1469 struct meson_sar_adc_priv *priv = iio_priv(indio_dev);
1470 int ret;
1472 ret = clk_prepare_enable(priv->core_clk);
1473 if (ret) {
1474 dev_err(dev, "failed to enable core clk\n");
1475 return ret;
1478 return meson_sar_adc_hw_enable(indio_dev);
1481 static DEFINE_SIMPLE_DEV_PM_OPS(meson_sar_adc_pm_ops,
1482 meson_sar_adc_suspend, meson_sar_adc_resume);
1484 static struct platform_driver meson_sar_adc_driver = {
1485 .probe = meson_sar_adc_probe,
1486 .remove_new = meson_sar_adc_remove,
1487 .driver = {
1488 .name = "meson-saradc",
1489 .of_match_table = meson_sar_adc_of_match,
1490 .pm = pm_sleep_ptr(&meson_sar_adc_pm_ops),
1494 module_platform_driver(meson_sar_adc_driver);
1496 MODULE_AUTHOR("Martin Blumenstingl <martin.blumenstingl@googlemail.com>");
1497 MODULE_DESCRIPTION("Amlogic Meson SAR ADC driver");
1498 MODULE_LICENSE("GPL v2");