Neil Armstrong | 59beb23 | 2018-04-23 16:19:23 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (C) 2017 Martin Blumenstingl <martin.blumenstingl@googlemail.com> |
| 4 | * Copyright (C) 2018 BayLibre, SAS |
| 5 | * Author: Neil Armstrong <narmstrong@baylibre.com> |
| 6 | * |
| 7 | * Amlogic Meson Successive Approximation Register (SAR) A/D Converter |
| 8 | */ |
| 9 | |
| 10 | #include <common.h> |
| 11 | #include <adc.h> |
| 12 | #include <clk.h> |
| 13 | #include <dm.h> |
| 14 | #include <regmap.h> |
| 15 | #include <errno.h> |
| 16 | #include <asm/io.h> |
Simon Glass | c05ed00 | 2020-05-10 11:40:11 -0600 | [diff] [blame^] | 17 | #include <linux/delay.h> |
Neil Armstrong | 59beb23 | 2018-04-23 16:19:23 +0200 | [diff] [blame] | 18 | #include <linux/math64.h> |
| 19 | #include <linux/bitfield.h> |
| 20 | |
| 21 | #define MESON_SAR_ADC_REG0 0x00 |
| 22 | #define MESON_SAR_ADC_REG0_PANEL_DETECT BIT(31) |
| 23 | #define MESON_SAR_ADC_REG0_BUSY_MASK GENMASK(30, 28) |
| 24 | #define MESON_SAR_ADC_REG0_DELTA_BUSY BIT(30) |
| 25 | #define MESON_SAR_ADC_REG0_AVG_BUSY BIT(29) |
| 26 | #define MESON_SAR_ADC_REG0_SAMPLE_BUSY BIT(28) |
| 27 | #define MESON_SAR_ADC_REG0_FIFO_FULL BIT(27) |
| 28 | #define MESON_SAR_ADC_REG0_FIFO_EMPTY BIT(26) |
| 29 | #define MESON_SAR_ADC_REG0_FIFO_COUNT_MASK GENMASK(25, 21) |
| 30 | #define MESON_SAR_ADC_REG0_ADC_BIAS_CTRL_MASK GENMASK(20, 19) |
| 31 | #define MESON_SAR_ADC_REG0_CURR_CHAN_ID_MASK GENMASK(18, 16) |
| 32 | #define MESON_SAR_ADC_REG0_ADC_TEMP_SEN_SEL BIT(15) |
| 33 | #define MESON_SAR_ADC_REG0_SAMPLING_STOP BIT(14) |
| 34 | #define MESON_SAR_ADC_REG0_CHAN_DELTA_EN_MASK GENMASK(13, 12) |
| 35 | #define MESON_SAR_ADC_REG0_DETECT_IRQ_POL BIT(10) |
| 36 | #define MESON_SAR_ADC_REG0_DETECT_IRQ_EN BIT(9) |
| 37 | #define MESON_SAR_ADC_REG0_FIFO_CNT_IRQ_MASK GENMASK(8, 4) |
| 38 | #define MESON_SAR_ADC_REG0_FIFO_IRQ_EN BIT(3) |
| 39 | #define MESON_SAR_ADC_REG0_SAMPLING_START BIT(2) |
| 40 | #define MESON_SAR_ADC_REG0_CONTINUOUS_EN BIT(1) |
| 41 | #define MESON_SAR_ADC_REG0_SAMPLE_ENGINE_ENABLE BIT(0) |
| 42 | |
| 43 | #define MESON_SAR_ADC_CHAN_LIST 0x04 |
| 44 | #define MESON_SAR_ADC_CHAN_LIST_MAX_INDEX_MASK GENMASK(26, 24) |
| 45 | #define MESON_SAR_ADC_CHAN_LIST_ENTRY_MASK(_chan) \ |
| 46 | (GENMASK(2, 0) << ((_chan) * 3)) |
| 47 | |
| 48 | #define MESON_SAR_ADC_AVG_CNTL 0x08 |
| 49 | #define MESON_SAR_ADC_AVG_CNTL_AVG_MODE_SHIFT(_chan) \ |
| 50 | (16 + ((_chan) * 2)) |
| 51 | #define MESON_SAR_ADC_AVG_CNTL_AVG_MODE_MASK(_chan) \ |
| 52 | (GENMASK(17, 16) << ((_chan) * 2)) |
| 53 | #define MESON_SAR_ADC_AVG_CNTL_NUM_SAMPLES_SHIFT(_chan) \ |
| 54 | (0 + ((_chan) * 2)) |
| 55 | #define MESON_SAR_ADC_AVG_CNTL_NUM_SAMPLES_MASK(_chan) \ |
| 56 | (GENMASK(1, 0) << ((_chan) * 2)) |
| 57 | |
| 58 | #define MESON_SAR_ADC_REG3 0x0c |
| 59 | #define MESON_SAR_ADC_REG3_CNTL_USE_SC_DLY BIT(31) |
| 60 | #define MESON_SAR_ADC_REG3_CLK_EN BIT(30) |
| 61 | #define MESON_SAR_ADC_REG3_BL30_INITIALIZED BIT(28) |
| 62 | #define MESON_SAR_ADC_REG3_CTRL_CONT_RING_COUNTER_EN BIT(27) |
| 63 | #define MESON_SAR_ADC_REG3_CTRL_SAMPLING_CLOCK_PHASE BIT(26) |
| 64 | #define MESON_SAR_ADC_REG3_CTRL_CHAN7_MUX_SEL_MASK GENMASK(25, 23) |
| 65 | #define MESON_SAR_ADC_REG3_DETECT_EN BIT(22) |
| 66 | #define MESON_SAR_ADC_REG3_ADC_EN BIT(21) |
| 67 | #define MESON_SAR_ADC_REG3_PANEL_DETECT_COUNT_MASK GENMASK(20, 18) |
| 68 | #define MESON_SAR_ADC_REG3_PANEL_DETECT_FILTER_TB_MASK GENMASK(17, 16) |
| 69 | #define MESON_SAR_ADC_REG3_ADC_CLK_DIV_SHIFT 10 |
| 70 | #define MESON_SAR_ADC_REG3_ADC_CLK_DIV_WIDTH 5 |
| 71 | #define MESON_SAR_ADC_REG3_BLOCK_DLY_SEL_MASK GENMASK(9, 8) |
| 72 | #define MESON_SAR_ADC_REG3_BLOCK_DLY_MASK GENMASK(7, 0) |
| 73 | |
| 74 | #define MESON_SAR_ADC_DELAY 0x10 |
| 75 | #define MESON_SAR_ADC_DELAY_INPUT_DLY_SEL_MASK GENMASK(25, 24) |
| 76 | #define MESON_SAR_ADC_DELAY_BL30_BUSY BIT(15) |
| 77 | #define MESON_SAR_ADC_DELAY_KERNEL_BUSY BIT(14) |
| 78 | #define MESON_SAR_ADC_DELAY_INPUT_DLY_CNT_MASK GENMASK(23, 16) |
| 79 | #define MESON_SAR_ADC_DELAY_SAMPLE_DLY_SEL_MASK GENMASK(9, 8) |
| 80 | #define MESON_SAR_ADC_DELAY_SAMPLE_DLY_CNT_MASK GENMASK(7, 0) |
| 81 | |
| 82 | #define MESON_SAR_ADC_LAST_RD 0x14 |
| 83 | #define MESON_SAR_ADC_LAST_RD_LAST_CHANNEL1_MASK GENMASK(23, 16) |
| 84 | #define MESON_SAR_ADC_LAST_RD_LAST_CHANNEL0_MASK GENMASK(9, 0) |
| 85 | |
| 86 | #define MESON_SAR_ADC_FIFO_RD 0x18 |
| 87 | #define MESON_SAR_ADC_FIFO_RD_CHAN_ID_MASK GENMASK(14, 12) |
| 88 | #define MESON_SAR_ADC_FIFO_RD_SAMPLE_VALUE_MASK GENMASK(11, 0) |
| 89 | |
| 90 | #define MESON_SAR_ADC_AUX_SW 0x1c |
| 91 | #define MESON_SAR_ADC_AUX_SW_MUX_SEL_CHAN_SHIFT(_chan) \ |
| 92 | (8 + (((_chan) - 2) * 3)) |
| 93 | #define MESON_SAR_ADC_AUX_SW_VREF_P_MUX BIT(6) |
| 94 | #define MESON_SAR_ADC_AUX_SW_VREF_N_MUX BIT(5) |
| 95 | #define MESON_SAR_ADC_AUX_SW_MODE_SEL BIT(4) |
| 96 | #define MESON_SAR_ADC_AUX_SW_YP_DRIVE_SW BIT(3) |
| 97 | #define MESON_SAR_ADC_AUX_SW_XP_DRIVE_SW BIT(2) |
| 98 | #define MESON_SAR_ADC_AUX_SW_YM_DRIVE_SW BIT(1) |
| 99 | #define MESON_SAR_ADC_AUX_SW_XM_DRIVE_SW BIT(0) |
| 100 | |
| 101 | #define MESON_SAR_ADC_CHAN_10_SW 0x20 |
| 102 | #define MESON_SAR_ADC_CHAN_10_SW_CHAN1_MUX_SEL_MASK GENMASK(25, 23) |
| 103 | #define MESON_SAR_ADC_CHAN_10_SW_CHAN1_VREF_P_MUX BIT(22) |
| 104 | #define MESON_SAR_ADC_CHAN_10_SW_CHAN1_VREF_N_MUX BIT(21) |
| 105 | #define MESON_SAR_ADC_CHAN_10_SW_CHAN1_MODE_SEL BIT(20) |
| 106 | #define MESON_SAR_ADC_CHAN_10_SW_CHAN1_YP_DRIVE_SW BIT(19) |
| 107 | #define MESON_SAR_ADC_CHAN_10_SW_CHAN1_XP_DRIVE_SW BIT(18) |
| 108 | #define MESON_SAR_ADC_CHAN_10_SW_CHAN1_YM_DRIVE_SW BIT(17) |
| 109 | #define MESON_SAR_ADC_CHAN_10_SW_CHAN1_XM_DRIVE_SW BIT(16) |
| 110 | #define MESON_SAR_ADC_CHAN_10_SW_CHAN0_MUX_SEL_MASK GENMASK(9, 7) |
| 111 | #define MESON_SAR_ADC_CHAN_10_SW_CHAN0_VREF_P_MUX BIT(6) |
| 112 | #define MESON_SAR_ADC_CHAN_10_SW_CHAN0_VREF_N_MUX BIT(5) |
| 113 | #define MESON_SAR_ADC_CHAN_10_SW_CHAN0_MODE_SEL BIT(4) |
| 114 | #define MESON_SAR_ADC_CHAN_10_SW_CHAN0_YP_DRIVE_SW BIT(3) |
| 115 | #define MESON_SAR_ADC_CHAN_10_SW_CHAN0_XP_DRIVE_SW BIT(2) |
| 116 | #define MESON_SAR_ADC_CHAN_10_SW_CHAN0_YM_DRIVE_SW BIT(1) |
| 117 | #define MESON_SAR_ADC_CHAN_10_SW_CHAN0_XM_DRIVE_SW BIT(0) |
| 118 | |
| 119 | #define MESON_SAR_ADC_DETECT_IDLE_SW 0x24 |
| 120 | #define MESON_SAR_ADC_DETECT_IDLE_SW_DETECT_SW_EN BIT(26) |
| 121 | #define MESON_SAR_ADC_DETECT_IDLE_SW_DETECT_MUX_MASK GENMASK(25, 23) |
| 122 | #define MESON_SAR_ADC_DETECT_IDLE_SW_DETECT_VREF_P_MUX BIT(22) |
| 123 | #define MESON_SAR_ADC_DETECT_IDLE_SW_DETECT_VREF_N_MUX BIT(21) |
| 124 | #define MESON_SAR_ADC_DETECT_IDLE_SW_DETECT_MODE_SEL BIT(20) |
| 125 | #define MESON_SAR_ADC_DETECT_IDLE_SW_DETECT_YP_DRIVE_SW BIT(19) |
| 126 | #define MESON_SAR_ADC_DETECT_IDLE_SW_DETECT_XP_DRIVE_SW BIT(18) |
| 127 | #define MESON_SAR_ADC_DETECT_IDLE_SW_DETECT_YM_DRIVE_SW BIT(17) |
| 128 | #define MESON_SAR_ADC_DETECT_IDLE_SW_DETECT_XM_DRIVE_SW BIT(16) |
| 129 | #define MESON_SAR_ADC_DETECT_IDLE_SW_IDLE_MUX_SEL_MASK GENMASK(9, 7) |
| 130 | #define MESON_SAR_ADC_DETECT_IDLE_SW_IDLE_VREF_P_MUX BIT(6) |
| 131 | #define MESON_SAR_ADC_DETECT_IDLE_SW_IDLE_VREF_N_MUX BIT(5) |
| 132 | #define MESON_SAR_ADC_DETECT_IDLE_SW_IDLE_MODE_SEL BIT(4) |
| 133 | #define MESON_SAR_ADC_DETECT_IDLE_SW_IDLE_YP_DRIVE_SW BIT(3) |
| 134 | #define MESON_SAR_ADC_DETECT_IDLE_SW_IDLE_XP_DRIVE_SW BIT(2) |
| 135 | #define MESON_SAR_ADC_DETECT_IDLE_SW_IDLE_YM_DRIVE_SW BIT(1) |
| 136 | #define MESON_SAR_ADC_DETECT_IDLE_SW_IDLE_XM_DRIVE_SW BIT(0) |
| 137 | |
| 138 | #define MESON_SAR_ADC_DELTA_10 0x28 |
| 139 | #define MESON_SAR_ADC_DELTA_10_TEMP_SEL BIT(27) |
| 140 | #define MESON_SAR_ADC_DELTA_10_TS_REVE1 BIT(26) |
| 141 | #define MESON_SAR_ADC_DELTA_10_CHAN1_DELTA_VALUE_MASK GENMASK(25, 16) |
| 142 | #define MESON_SAR_ADC_DELTA_10_TS_REVE0 BIT(15) |
| 143 | #define MESON_SAR_ADC_DELTA_10_TS_C_SHIFT 11 |
| 144 | #define MESON_SAR_ADC_DELTA_10_TS_C_MASK GENMASK(14, 11) |
| 145 | #define MESON_SAR_ADC_DELTA_10_TS_VBG_EN BIT(10) |
| 146 | #define MESON_SAR_ADC_DELTA_10_CHAN0_DELTA_VALUE_MASK GENMASK(9, 0) |
| 147 | |
| 148 | /* |
| 149 | * NOTE: registers from here are undocumented (the vendor Linux kernel driver |
| 150 | * and u-boot source served as reference). These only seem to be relevant on |
| 151 | * GXBB and newer. |
| 152 | */ |
| 153 | #define MESON_SAR_ADC_REG11 0x2c |
| 154 | #define MESON_SAR_ADC_REG11_BANDGAP_EN BIT(13) |
| 155 | |
| 156 | #define MESON_SAR_ADC_REG13 0x34 |
| 157 | #define MESON_SAR_ADC_REG13_12BIT_CALIBRATION_MASK GENMASK(13, 8) |
| 158 | |
| 159 | #define MESON_SAR_ADC_MAX_FIFO_SIZE 32 |
| 160 | #define MESON_SAR_ADC_TIMEOUT 100 /* ms */ |
| 161 | |
| 162 | #define NUM_CHANNELS 8 |
| 163 | |
| 164 | #define MILLION 1000000 |
| 165 | |
| 166 | struct meson_saradc_data { |
| 167 | int num_bits; |
| 168 | }; |
| 169 | |
| 170 | struct meson_saradc_priv { |
| 171 | const struct meson_saradc_data *data; |
| 172 | struct regmap *regmap; |
| 173 | struct clk core_clk; |
| 174 | struct clk adc_clk; |
| 175 | bool initialized; |
| 176 | int active_channel; |
| 177 | int calibbias; |
| 178 | int calibscale; |
| 179 | }; |
| 180 | |
| 181 | static unsigned int |
| 182 | meson_saradc_get_fifo_count(struct meson_saradc_priv *priv) |
| 183 | { |
| 184 | u32 regval; |
| 185 | |
| 186 | regmap_read(priv->regmap, MESON_SAR_ADC_REG0, ®val); |
| 187 | |
| 188 | return FIELD_GET(MESON_SAR_ADC_REG0_FIFO_COUNT_MASK, regval); |
| 189 | } |
| 190 | |
| 191 | static int meson_saradc_lock(struct meson_saradc_priv *priv) |
| 192 | { |
| 193 | uint val, timeout = 10000; |
| 194 | |
| 195 | /* prevent BL30 from using the SAR ADC while we are using it */ |
| 196 | regmap_update_bits(priv->regmap, MESON_SAR_ADC_DELAY, |
| 197 | MESON_SAR_ADC_DELAY_KERNEL_BUSY, |
| 198 | MESON_SAR_ADC_DELAY_KERNEL_BUSY); |
| 199 | |
| 200 | /* |
| 201 | * wait until BL30 releases it's lock (so we can use the SAR ADC) |
| 202 | */ |
| 203 | do { |
| 204 | udelay(1); |
| 205 | regmap_read(priv->regmap, MESON_SAR_ADC_DELAY, &val); |
| 206 | } while (val & MESON_SAR_ADC_DELAY_BL30_BUSY && timeout--); |
| 207 | |
| 208 | if (timeout < 0) { |
| 209 | printf("Timeout while waiting for BL30 unlock\n"); |
| 210 | return -ETIMEDOUT; |
| 211 | } |
| 212 | |
| 213 | return 0; |
| 214 | } |
| 215 | |
| 216 | static void meson_saradc_unlock(struct meson_saradc_priv *priv) |
| 217 | { |
| 218 | /* allow BL30 to use the SAR ADC again */ |
| 219 | regmap_update_bits(priv->regmap, MESON_SAR_ADC_DELAY, |
| 220 | MESON_SAR_ADC_DELAY_KERNEL_BUSY, 0); |
| 221 | } |
| 222 | |
| 223 | static void meson_saradc_clear_fifo(struct meson_saradc_priv *priv) |
| 224 | { |
| 225 | unsigned int count, tmp; |
| 226 | |
| 227 | for (count = 0; count < MESON_SAR_ADC_MAX_FIFO_SIZE; count++) { |
| 228 | if (!meson_saradc_get_fifo_count(priv)) |
| 229 | break; |
| 230 | |
| 231 | regmap_read(priv->regmap, MESON_SAR_ADC_FIFO_RD, &tmp); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | static int meson_saradc_calib_val(struct meson_saradc_priv *priv, int val) |
| 236 | { |
| 237 | int tmp; |
| 238 | |
| 239 | /* use val_calib = scale * val_raw + offset calibration function */ |
| 240 | tmp = div_s64((s64)val * priv->calibscale, MILLION) + priv->calibbias; |
| 241 | |
| 242 | return clamp(tmp, 0, (1 << priv->data->num_bits) - 1); |
| 243 | } |
| 244 | |
| 245 | static int meson_saradc_wait_busy_clear(struct meson_saradc_priv *priv) |
| 246 | { |
| 247 | uint regval, timeout = 10000; |
| 248 | |
| 249 | /* |
| 250 | * NOTE: we need a small delay before reading the status, otherwise |
| 251 | * the sample engine may not have started internally (which would |
| 252 | * seem to us that sampling is already finished). |
| 253 | */ |
| 254 | do { |
| 255 | udelay(1); |
| 256 | regmap_read(priv->regmap, MESON_SAR_ADC_REG0, ®val); |
| 257 | } while (FIELD_GET(MESON_SAR_ADC_REG0_BUSY_MASK, regval) && timeout--); |
| 258 | |
| 259 | if (timeout < 0) |
| 260 | return -ETIMEDOUT; |
| 261 | |
| 262 | return 0; |
| 263 | } |
| 264 | |
| 265 | static int meson_saradc_read_raw_sample(struct meson_saradc_priv *priv, |
| 266 | unsigned int channel, uint *val) |
| 267 | { |
| 268 | uint regval, fifo_chan, fifo_val, count; |
| 269 | int ret; |
| 270 | |
| 271 | ret = meson_saradc_wait_busy_clear(priv); |
| 272 | if (ret) |
| 273 | return ret; |
| 274 | |
| 275 | count = meson_saradc_get_fifo_count(priv); |
| 276 | if (count != 1) { |
| 277 | printf("ADC FIFO has %d element(s) instead of one\n", count); |
| 278 | return -EINVAL; |
| 279 | } |
| 280 | |
| 281 | regmap_read(priv->regmap, MESON_SAR_ADC_FIFO_RD, ®val); |
| 282 | fifo_chan = FIELD_GET(MESON_SAR_ADC_FIFO_RD_CHAN_ID_MASK, regval); |
| 283 | if (fifo_chan != channel) { |
| 284 | printf("ADC FIFO entry belongs to channel %d instead of %d\n", |
| 285 | fifo_chan, channel); |
| 286 | return -EINVAL; |
| 287 | } |
| 288 | |
| 289 | fifo_val = FIELD_GET(MESON_SAR_ADC_FIFO_RD_SAMPLE_VALUE_MASK, regval); |
| 290 | fifo_val &= GENMASK(priv->data->num_bits - 1, 0); |
| 291 | *val = meson_saradc_calib_val(priv, fifo_val); |
| 292 | |
| 293 | return 0; |
| 294 | } |
| 295 | |
| 296 | static void meson_saradc_start_sample_engine(struct meson_saradc_priv *priv) |
| 297 | { |
| 298 | regmap_update_bits(priv->regmap, MESON_SAR_ADC_REG0, |
| 299 | MESON_SAR_ADC_REG0_FIFO_IRQ_EN, |
| 300 | MESON_SAR_ADC_REG0_FIFO_IRQ_EN); |
| 301 | |
| 302 | regmap_update_bits(priv->regmap, MESON_SAR_ADC_REG0, |
| 303 | MESON_SAR_ADC_REG0_SAMPLE_ENGINE_ENABLE, |
| 304 | MESON_SAR_ADC_REG0_SAMPLE_ENGINE_ENABLE); |
| 305 | |
| 306 | regmap_update_bits(priv->regmap, MESON_SAR_ADC_REG0, |
| 307 | MESON_SAR_ADC_REG0_SAMPLING_START, |
| 308 | MESON_SAR_ADC_REG0_SAMPLING_START); |
| 309 | } |
| 310 | |
| 311 | static void meson_saradc_stop_sample_engine(struct meson_saradc_priv *priv) |
| 312 | { |
| 313 | regmap_update_bits(priv->regmap, MESON_SAR_ADC_REG0, |
| 314 | MESON_SAR_ADC_REG0_FIFO_IRQ_EN, 0); |
| 315 | |
| 316 | regmap_update_bits(priv->regmap, MESON_SAR_ADC_REG0, |
| 317 | MESON_SAR_ADC_REG0_SAMPLING_STOP, |
| 318 | MESON_SAR_ADC_REG0_SAMPLING_STOP); |
| 319 | |
| 320 | /* wait until all modules are stopped */ |
| 321 | meson_saradc_wait_busy_clear(priv); |
| 322 | |
| 323 | regmap_update_bits(priv->regmap, MESON_SAR_ADC_REG0, |
| 324 | MESON_SAR_ADC_REG0_SAMPLE_ENGINE_ENABLE, 0); |
| 325 | } |
| 326 | |
| 327 | enum meson_saradc_avg_mode { |
| 328 | NO_AVERAGING = 0x0, |
| 329 | MEAN_AVERAGING = 0x1, |
| 330 | MEDIAN_AVERAGING = 0x2, |
| 331 | }; |
| 332 | |
| 333 | enum meson_saradc_num_samples { |
| 334 | ONE_SAMPLE = 0x0, |
| 335 | TWO_SAMPLES = 0x1, |
| 336 | FOUR_SAMPLES = 0x2, |
| 337 | EIGHT_SAMPLES = 0x3, |
| 338 | }; |
| 339 | |
| 340 | static void meson_saradc_set_averaging(struct meson_saradc_priv *priv, |
| 341 | unsigned int channel, |
| 342 | enum meson_saradc_avg_mode mode, |
| 343 | enum meson_saradc_num_samples samples) |
| 344 | { |
| 345 | int val; |
| 346 | |
| 347 | val = samples << MESON_SAR_ADC_AVG_CNTL_NUM_SAMPLES_SHIFT(channel); |
| 348 | regmap_update_bits(priv->regmap, MESON_SAR_ADC_AVG_CNTL, |
| 349 | MESON_SAR_ADC_AVG_CNTL_NUM_SAMPLES_MASK(channel), |
| 350 | val); |
| 351 | |
| 352 | val = mode << MESON_SAR_ADC_AVG_CNTL_AVG_MODE_SHIFT(channel); |
| 353 | regmap_update_bits(priv->regmap, MESON_SAR_ADC_AVG_CNTL, |
| 354 | MESON_SAR_ADC_AVG_CNTL_AVG_MODE_MASK(channel), val); |
| 355 | } |
| 356 | |
| 357 | static void meson_saradc_enable_channel(struct meson_saradc_priv *priv, |
| 358 | unsigned int channel) |
| 359 | { |
| 360 | uint regval; |
| 361 | |
| 362 | /* |
| 363 | * the SAR ADC engine allows sampling multiple channels at the same |
| 364 | * time. to keep it simple we're only working with one *internal* |
| 365 | * channel, which starts counting at index 0 (which means: count = 1). |
| 366 | */ |
| 367 | regval = FIELD_PREP(MESON_SAR_ADC_CHAN_LIST_MAX_INDEX_MASK, 0); |
| 368 | regmap_update_bits(priv->regmap, MESON_SAR_ADC_CHAN_LIST, |
| 369 | MESON_SAR_ADC_CHAN_LIST_MAX_INDEX_MASK, regval); |
| 370 | |
| 371 | /* map channel index 0 to the channel which we want to read */ |
| 372 | regval = FIELD_PREP(MESON_SAR_ADC_CHAN_LIST_ENTRY_MASK(0), channel); |
| 373 | regmap_update_bits(priv->regmap, MESON_SAR_ADC_CHAN_LIST, |
| 374 | MESON_SAR_ADC_CHAN_LIST_ENTRY_MASK(0), regval); |
| 375 | |
| 376 | regval = FIELD_PREP(MESON_SAR_ADC_DETECT_IDLE_SW_DETECT_MUX_MASK, |
| 377 | channel); |
| 378 | regmap_update_bits(priv->regmap, MESON_SAR_ADC_DETECT_IDLE_SW, |
| 379 | MESON_SAR_ADC_DETECT_IDLE_SW_DETECT_MUX_MASK, |
| 380 | regval); |
| 381 | |
| 382 | regval = FIELD_PREP(MESON_SAR_ADC_DETECT_IDLE_SW_IDLE_MUX_SEL_MASK, |
| 383 | channel); |
| 384 | regmap_update_bits(priv->regmap, MESON_SAR_ADC_DETECT_IDLE_SW, |
| 385 | MESON_SAR_ADC_DETECT_IDLE_SW_IDLE_MUX_SEL_MASK, |
| 386 | regval); |
| 387 | |
| 388 | if (channel == 6) |
| 389 | regmap_update_bits(priv->regmap, MESON_SAR_ADC_DELTA_10, |
| 390 | MESON_SAR_ADC_DELTA_10_TEMP_SEL, 0); |
| 391 | } |
| 392 | |
| 393 | static int meson_saradc_get_sample(struct meson_saradc_priv *priv, |
| 394 | int chan, uint *val) |
| 395 | { |
| 396 | int ret; |
| 397 | |
| 398 | ret = meson_saradc_lock(priv); |
| 399 | if (ret) |
| 400 | return ret; |
| 401 | |
| 402 | /* clear the FIFO to make sure we're not reading old values */ |
| 403 | meson_saradc_clear_fifo(priv); |
| 404 | |
| 405 | meson_saradc_set_averaging(priv, chan, MEAN_AVERAGING, EIGHT_SAMPLES); |
| 406 | |
| 407 | meson_saradc_enable_channel(priv, chan); |
| 408 | |
| 409 | meson_saradc_start_sample_engine(priv); |
| 410 | ret = meson_saradc_read_raw_sample(priv, chan, val); |
| 411 | meson_saradc_stop_sample_engine(priv); |
| 412 | |
| 413 | meson_saradc_unlock(priv); |
| 414 | |
| 415 | if (ret) { |
| 416 | printf("failed to read sample for channel %d: %d\n", |
| 417 | chan, ret); |
| 418 | return ret; |
| 419 | } |
| 420 | |
| 421 | return 0; |
| 422 | } |
| 423 | |
| 424 | static int meson_saradc_channel_data(struct udevice *dev, int channel, |
| 425 | unsigned int *data) |
| 426 | { |
| 427 | struct meson_saradc_priv *priv = dev_get_priv(dev); |
| 428 | |
| 429 | if (channel != priv->active_channel) { |
| 430 | pr_err("Requested channel is not active!"); |
| 431 | return -EINVAL; |
| 432 | } |
| 433 | |
| 434 | return meson_saradc_get_sample(priv, channel, data); |
| 435 | } |
| 436 | |
| 437 | enum meson_saradc_chan7_mux_sel { |
| 438 | CHAN7_MUX_VSS = 0x0, |
| 439 | CHAN7_MUX_VDD_DIV4 = 0x1, |
| 440 | CHAN7_MUX_VDD_DIV2 = 0x2, |
| 441 | CHAN7_MUX_VDD_MUL3_DIV4 = 0x3, |
| 442 | CHAN7_MUX_VDD = 0x4, |
| 443 | CHAN7_MUX_CH7_INPUT = 0x7, |
| 444 | }; |
| 445 | |
| 446 | static void meson_saradc_set_chan7_mux(struct meson_saradc_priv *priv, |
| 447 | enum meson_saradc_chan7_mux_sel sel) |
| 448 | { |
| 449 | u32 regval; |
| 450 | |
| 451 | regval = FIELD_PREP(MESON_SAR_ADC_REG3_CTRL_CHAN7_MUX_SEL_MASK, sel); |
| 452 | regmap_update_bits(priv->regmap, MESON_SAR_ADC_REG3, |
| 453 | MESON_SAR_ADC_REG3_CTRL_CHAN7_MUX_SEL_MASK, regval); |
| 454 | |
| 455 | udelay(20); |
| 456 | } |
| 457 | |
| 458 | static int meson_saradc_calib(struct meson_saradc_priv *priv) |
| 459 | { |
| 460 | uint nominal0, nominal1, value0, value1; |
| 461 | int ret; |
| 462 | |
| 463 | /* use points 25% and 75% for calibration */ |
| 464 | nominal0 = (1 << priv->data->num_bits) / 4; |
| 465 | nominal1 = (1 << priv->data->num_bits) * 3 / 4; |
| 466 | |
| 467 | meson_saradc_set_chan7_mux(priv, CHAN7_MUX_VDD_DIV4); |
| 468 | udelay(20); |
| 469 | ret = meson_saradc_get_sample(priv, 7, &value0); |
| 470 | if (ret < 0) |
| 471 | goto out; |
| 472 | |
| 473 | meson_saradc_set_chan7_mux(priv, CHAN7_MUX_VDD_MUL3_DIV4); |
| 474 | udelay(20); |
| 475 | ret = meson_saradc_get_sample(priv, 7, &value1); |
| 476 | if (ret < 0) |
| 477 | goto out; |
| 478 | |
| 479 | if (value1 <= value0) { |
| 480 | ret = -EINVAL; |
| 481 | goto out; |
| 482 | } |
| 483 | |
| 484 | priv->calibscale = div_s64((nominal1 - nominal0) * (s64)MILLION, |
| 485 | value1 - value0); |
| 486 | priv->calibbias = nominal0 - div_s64((s64)value0 * priv->calibscale, |
| 487 | MILLION); |
| 488 | ret = 0; |
| 489 | out: |
| 490 | meson_saradc_set_chan7_mux(priv, CHAN7_MUX_CH7_INPUT); |
| 491 | |
| 492 | return ret; |
| 493 | } |
| 494 | |
| 495 | static int meson_saradc_init(struct meson_saradc_priv *priv) |
| 496 | { |
| 497 | uint regval; |
| 498 | int ret, i; |
| 499 | |
| 500 | priv->calibscale = MILLION; |
| 501 | |
| 502 | /* |
| 503 | * make sure we start at CH7 input since the other muxes are only used |
| 504 | * for internal calibration. |
| 505 | */ |
| 506 | meson_saradc_set_chan7_mux(priv, CHAN7_MUX_CH7_INPUT); |
| 507 | |
| 508 | /* |
| 509 | * leave sampling delay and the input clocks as configured by |
| 510 | * BL30 to make sure BL30 gets the values it expects when |
| 511 | * reading the temperature sensor. |
| 512 | */ |
| 513 | regmap_read(priv->regmap, MESON_SAR_ADC_REG3, ®val); |
| 514 | if (regval & MESON_SAR_ADC_REG3_BL30_INITIALIZED) |
| 515 | return 0; |
| 516 | |
| 517 | meson_saradc_stop_sample_engine(priv); |
| 518 | |
| 519 | /* update the channel 6 MUX to select the temperature sensor */ |
| 520 | regmap_update_bits(priv->regmap, MESON_SAR_ADC_REG0, |
| 521 | MESON_SAR_ADC_REG0_ADC_TEMP_SEN_SEL, |
| 522 | MESON_SAR_ADC_REG0_ADC_TEMP_SEN_SEL); |
| 523 | |
| 524 | /* disable all channels by default */ |
| 525 | regmap_write(priv->regmap, MESON_SAR_ADC_CHAN_LIST, 0x0); |
| 526 | |
| 527 | regmap_update_bits(priv->regmap, MESON_SAR_ADC_REG3, |
| 528 | MESON_SAR_ADC_REG3_CTRL_SAMPLING_CLOCK_PHASE, 0); |
| 529 | regmap_update_bits(priv->regmap, MESON_SAR_ADC_REG3, |
| 530 | MESON_SAR_ADC_REG3_CNTL_USE_SC_DLY, |
| 531 | MESON_SAR_ADC_REG3_CNTL_USE_SC_DLY); |
| 532 | |
| 533 | /* delay between two samples = (10+1) * 1uS */ |
| 534 | regmap_update_bits(priv->regmap, MESON_SAR_ADC_DELAY, |
| 535 | MESON_SAR_ADC_DELAY_INPUT_DLY_CNT_MASK, |
| 536 | FIELD_PREP(MESON_SAR_ADC_DELAY_SAMPLE_DLY_CNT_MASK, |
| 537 | 10)); |
| 538 | regmap_update_bits(priv->regmap, MESON_SAR_ADC_DELAY, |
| 539 | MESON_SAR_ADC_DELAY_SAMPLE_DLY_SEL_MASK, |
| 540 | FIELD_PREP(MESON_SAR_ADC_DELAY_SAMPLE_DLY_SEL_MASK, |
| 541 | 0)); |
| 542 | |
| 543 | /* delay between two samples = (10+1) * 1uS */ |
| 544 | regmap_update_bits(priv->regmap, MESON_SAR_ADC_DELAY, |
| 545 | MESON_SAR_ADC_DELAY_INPUT_DLY_CNT_MASK, |
| 546 | FIELD_PREP(MESON_SAR_ADC_DELAY_INPUT_DLY_CNT_MASK, |
| 547 | 10)); |
| 548 | regmap_update_bits(priv->regmap, MESON_SAR_ADC_DELAY, |
| 549 | MESON_SAR_ADC_DELAY_INPUT_DLY_SEL_MASK, |
| 550 | FIELD_PREP(MESON_SAR_ADC_DELAY_INPUT_DLY_SEL_MASK, |
| 551 | 1)); |
| 552 | |
| 553 | /* |
| 554 | * set up the input channel muxes in MESON_SAR_ADC_CHAN_10_SW |
| 555 | * (0 = SAR_ADC_CH0, 1 = SAR_ADC_CH1) |
| 556 | */ |
| 557 | regval = FIELD_PREP(MESON_SAR_ADC_CHAN_10_SW_CHAN0_MUX_SEL_MASK, 0); |
| 558 | regmap_update_bits(priv->regmap, MESON_SAR_ADC_CHAN_10_SW, |
| 559 | MESON_SAR_ADC_CHAN_10_SW_CHAN0_MUX_SEL_MASK, |
| 560 | regval); |
| 561 | regval = FIELD_PREP(MESON_SAR_ADC_CHAN_10_SW_CHAN1_MUX_SEL_MASK, 1); |
| 562 | regmap_update_bits(priv->regmap, MESON_SAR_ADC_CHAN_10_SW, |
| 563 | MESON_SAR_ADC_CHAN_10_SW_CHAN1_MUX_SEL_MASK, |
| 564 | regval); |
| 565 | |
| 566 | /* |
| 567 | * set up the input channel muxes in MESON_SAR_ADC_AUX_SW |
| 568 | * (2 = SAR_ADC_CH2, 3 = SAR_ADC_CH3, ...) and enable |
| 569 | * MESON_SAR_ADC_AUX_SW_YP_DRIVE_SW and |
| 570 | * MESON_SAR_ADC_AUX_SW_XP_DRIVE_SW like the vendor driver. |
| 571 | */ |
| 572 | regval = 0; |
| 573 | for (i = 2; i <= 7; i++) |
| 574 | regval |= i << MESON_SAR_ADC_AUX_SW_MUX_SEL_CHAN_SHIFT(i); |
| 575 | regval |= MESON_SAR_ADC_AUX_SW_YP_DRIVE_SW; |
| 576 | regval |= MESON_SAR_ADC_AUX_SW_XP_DRIVE_SW; |
| 577 | regmap_write(priv->regmap, MESON_SAR_ADC_AUX_SW, regval); |
| 578 | |
| 579 | ret = meson_saradc_lock(priv); |
| 580 | if (ret) |
| 581 | return ret; |
| 582 | |
| 583 | #if CONFIG_IS_ENABLED(CLK) |
| 584 | ret = clk_enable(&priv->core_clk); |
| 585 | if (ret) |
| 586 | return ret; |
| 587 | #endif |
| 588 | |
| 589 | regval = FIELD_PREP(MESON_SAR_ADC_REG0_FIFO_CNT_IRQ_MASK, 1); |
| 590 | regmap_update_bits(priv->regmap, MESON_SAR_ADC_REG0, |
| 591 | MESON_SAR_ADC_REG0_FIFO_CNT_IRQ_MASK, regval); |
| 592 | |
| 593 | regmap_update_bits(priv->regmap, MESON_SAR_ADC_REG11, |
| 594 | MESON_SAR_ADC_REG11_BANDGAP_EN, |
| 595 | MESON_SAR_ADC_REG11_BANDGAP_EN); |
| 596 | |
| 597 | regmap_update_bits(priv->regmap, MESON_SAR_ADC_REG3, |
| 598 | MESON_SAR_ADC_REG3_ADC_EN, |
| 599 | MESON_SAR_ADC_REG3_ADC_EN); |
| 600 | |
| 601 | udelay(5); |
| 602 | |
| 603 | #if CONFIG_IS_ENABLED(CLK) |
| 604 | ret = clk_enable(&priv->adc_clk); |
| 605 | if (ret) |
| 606 | return ret; |
| 607 | #endif |
| 608 | |
| 609 | meson_saradc_unlock(priv); |
| 610 | |
| 611 | ret = meson_saradc_calib(priv); |
| 612 | if (ret) { |
| 613 | printf("calibration failed\n"); |
| 614 | return -EIO; |
| 615 | } |
| 616 | |
| 617 | return 0; |
| 618 | } |
| 619 | |
| 620 | static int meson_saradc_start_channel(struct udevice *dev, int channel) |
| 621 | { |
| 622 | struct meson_saradc_priv *priv = dev_get_priv(dev); |
| 623 | |
| 624 | if (channel < 0 || channel >= NUM_CHANNELS) { |
| 625 | printf("Requested channel is invalid!"); |
| 626 | return -EINVAL; |
| 627 | } |
| 628 | |
| 629 | if (!priv->initialized) { |
| 630 | int ret; |
| 631 | |
| 632 | ret = meson_saradc_init(priv); |
| 633 | if (ret) |
| 634 | return ret; |
| 635 | |
| 636 | priv->initialized = true; |
| 637 | } |
| 638 | |
| 639 | priv->active_channel = channel; |
| 640 | |
| 641 | return 0; |
| 642 | } |
| 643 | |
| 644 | static int meson_saradc_stop(struct udevice *dev) |
| 645 | { |
| 646 | struct meson_saradc_priv *priv = dev_get_priv(dev); |
| 647 | |
| 648 | priv->active_channel = -1; |
| 649 | |
| 650 | return 0; |
| 651 | } |
| 652 | |
| 653 | static int meson_saradc_probe(struct udevice *dev) |
| 654 | { |
| 655 | struct meson_saradc_priv *priv = dev_get_priv(dev); |
| 656 | int ret; |
| 657 | |
Neil Armstrong | 0421c98 | 2018-06-14 13:43:37 +0200 | [diff] [blame] | 658 | ret = regmap_init_mem(dev_ofnode(dev), &priv->regmap); |
Neil Armstrong | 59beb23 | 2018-04-23 16:19:23 +0200 | [diff] [blame] | 659 | if (ret) |
| 660 | return ret; |
| 661 | |
| 662 | #if CONFIG_IS_ENABLED(CLK) |
| 663 | ret = clk_get_by_name(dev, "core", &priv->core_clk); |
| 664 | if (ret) |
| 665 | return ret; |
| 666 | |
| 667 | ret = clk_get_by_name(dev, "adc_clk", &priv->adc_clk); |
| 668 | if (ret) |
| 669 | return ret; |
| 670 | #endif |
| 671 | |
| 672 | priv->active_channel = -1; |
| 673 | |
| 674 | return 0; |
| 675 | } |
| 676 | |
| 677 | int meson_saradc_ofdata_to_platdata(struct udevice *dev) |
| 678 | { |
| 679 | struct adc_uclass_platdata *uc_pdata = dev_get_uclass_platdata(dev); |
| 680 | struct meson_saradc_priv *priv = dev_get_priv(dev); |
| 681 | |
| 682 | priv->data = (struct meson_saradc_data *)dev_get_driver_data(dev); |
| 683 | |
| 684 | uc_pdata->data_mask = GENMASK(priv->data->num_bits - 1, 0); |
| 685 | uc_pdata->data_format = ADC_DATA_FORMAT_BIN; |
| 686 | uc_pdata->data_timeout_us = MESON_SAR_ADC_TIMEOUT * 1000; |
| 687 | uc_pdata->channel_mask = GENMASK(NUM_CHANNELS - 1, 0); |
| 688 | |
| 689 | return 0; |
| 690 | } |
| 691 | |
| 692 | static const struct adc_ops meson_saradc_ops = { |
| 693 | .start_channel = meson_saradc_start_channel, |
| 694 | .channel_data = meson_saradc_channel_data, |
| 695 | .stop = meson_saradc_stop, |
| 696 | }; |
| 697 | |
| 698 | static const struct meson_saradc_data gxbb_saradc_data = { |
| 699 | .num_bits = 10, |
| 700 | }; |
| 701 | |
| 702 | static const struct meson_saradc_data gxl_saradc_data = { |
| 703 | .num_bits = 12, |
| 704 | }; |
| 705 | |
| 706 | static const struct udevice_id meson_saradc_ids[] = { |
| 707 | { .compatible = "amlogic,meson-gxbb-saradc", |
| 708 | .data = (ulong)&gxbb_saradc_data }, |
| 709 | { .compatible = "amlogic,meson-gxl-saradc", |
| 710 | .data = (ulong)&gxl_saradc_data }, |
| 711 | { .compatible = "amlogic,meson-gxm-saradc", |
| 712 | .data = (ulong)&gxl_saradc_data }, |
| 713 | { } |
| 714 | }; |
| 715 | |
| 716 | U_BOOT_DRIVER(meson_saradc) = { |
| 717 | .name = "meson_saradc", |
| 718 | .id = UCLASS_ADC, |
| 719 | .of_match = meson_saradc_ids, |
| 720 | .ops = &meson_saradc_ops, |
| 721 | .probe = meson_saradc_probe, |
| 722 | .ofdata_to_platdata = meson_saradc_ofdata_to_platdata, |
| 723 | .priv_auto_alloc_size = sizeof(struct meson_saradc_priv), |
| 724 | }; |