Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Przemyslaw Marczak | 5decbf5 | 2015-10-27 13:08:00 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2015 Samsung Electronics |
| 4 | * Przemyslaw Marczak <p.marczak@samsung.com> |
Przemyslaw Marczak | 5decbf5 | 2015-10-27 13:08:00 +0100 | [diff] [blame] | 5 | */ |
| 6 | |
Patrick Delaunay | b953ec2 | 2021-04-27 11:02:19 +0200 | [diff] [blame] | 7 | #define LOG_CATEGORY UCLASS_ADC |
| 8 | |
Przemyslaw Marczak | 5decbf5 | 2015-10-27 13:08:00 +0100 | [diff] [blame] | 9 | #include <common.h> |
| 10 | #include <errno.h> |
Fabrice Gasnier | 63f004e | 2018-11-12 14:03:58 +0100 | [diff] [blame] | 11 | #include <div64.h> |
Przemyslaw Marczak | 5decbf5 | 2015-10-27 13:08:00 +0100 | [diff] [blame] | 12 | #include <dm.h> |
| 13 | #include <dm/lists.h> |
| 14 | #include <dm/device-internal.h> |
| 15 | #include <dm/uclass-internal.h> |
| 16 | #include <adc.h> |
Simon Glass | c05ed00 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 17 | #include <linux/delay.h> |
Simon Glass | 1e94b46 | 2023-09-14 18:21:46 -0600 | [diff] [blame] | 18 | #include <linux/printk.h> |
Przemyslaw Marczak | 5decbf5 | 2015-10-27 13:08:00 +0100 | [diff] [blame] | 19 | #include <power/regulator.h> |
| 20 | |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 21 | #define ADC_UCLASS_PLATDATA_SIZE sizeof(struct adc_uclass_plat) |
Przemyslaw Marczak | 5decbf5 | 2015-10-27 13:08:00 +0100 | [diff] [blame] | 22 | #define CHECK_NUMBER true |
| 23 | #define CHECK_MASK (!CHECK_NUMBER) |
| 24 | |
| 25 | /* TODO: add support for timer uclass (for early calls) */ |
Tom Rini | c465971 | 2022-12-02 16:42:43 -0500 | [diff] [blame] | 26 | #ifdef CONFIG_SANDBOX |
Przemyslaw Marczak | 5decbf5 | 2015-10-27 13:08:00 +0100 | [diff] [blame] | 27 | #define sdelay(x) udelay(x) |
| 28 | #else |
| 29 | extern void sdelay(unsigned long loops); |
| 30 | #endif |
| 31 | |
| 32 | static int check_channel(struct udevice *dev, int value, bool number_or_mask, |
| 33 | const char *caller_function) |
| 34 | { |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 35 | struct adc_uclass_plat *uc_pdata = dev_get_uclass_plat(dev); |
Przemyslaw Marczak | 5decbf5 | 2015-10-27 13:08:00 +0100 | [diff] [blame] | 36 | unsigned mask = number_or_mask ? (1 << value) : value; |
| 37 | |
| 38 | /* For the real ADC hardware, some ADC channels can be inactive. |
| 39 | * For example if device has 4 analog channels, and only channels |
| 40 | * 1-st and 3-rd are valid, then channel mask is: 0b1010, so request |
| 41 | * with mask 0b1110 should return an error. |
| 42 | */ |
| 43 | if ((uc_pdata->channel_mask >= mask) && (uc_pdata->channel_mask & mask)) |
| 44 | return 0; |
| 45 | |
| 46 | printf("Error in %s/%s().\nWrong channel selection for device: %s\n", |
| 47 | __FILE__, caller_function, dev->name); |
| 48 | |
| 49 | return -EINVAL; |
| 50 | } |
| 51 | |
| 52 | static int adc_supply_enable(struct udevice *dev) |
| 53 | { |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 54 | struct adc_uclass_plat *uc_pdata = dev_get_uclass_plat(dev); |
Jonas Karlman | d82cbc5 | 2023-07-19 21:20:55 +0000 | [diff] [blame] | 55 | int ret; |
Przemyslaw Marczak | 5decbf5 | 2015-10-27 13:08:00 +0100 | [diff] [blame] | 56 | |
Jonas Karlman | d82cbc5 | 2023-07-19 21:20:55 +0000 | [diff] [blame] | 57 | ret = regulator_set_enable_if_allowed(uc_pdata->vdd_supply, true); |
| 58 | if (ret && ret != -ENOSYS) { |
| 59 | pr_err("%s: can't enable vdd-supply!", dev->name); |
| 60 | return ret; |
Przemyslaw Marczak | 5decbf5 | 2015-10-27 13:08:00 +0100 | [diff] [blame] | 61 | } |
| 62 | |
Jonas Karlman | d82cbc5 | 2023-07-19 21:20:55 +0000 | [diff] [blame] | 63 | ret = regulator_set_enable_if_allowed(uc_pdata->vss_supply, true); |
| 64 | if (ret && ret != -ENOSYS) { |
| 65 | pr_err("%s: can't enable vss-supply!", dev->name); |
| 66 | return ret; |
Przemyslaw Marczak | 5decbf5 | 2015-10-27 13:08:00 +0100 | [diff] [blame] | 67 | } |
| 68 | |
Jonas Karlman | d82cbc5 | 2023-07-19 21:20:55 +0000 | [diff] [blame] | 69 | return 0; |
Przemyslaw Marczak | 5decbf5 | 2015-10-27 13:08:00 +0100 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | int adc_data_mask(struct udevice *dev, unsigned int *data_mask) |
| 73 | { |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 74 | struct adc_uclass_plat *uc_pdata = dev_get_uclass_plat(dev); |
Przemyslaw Marczak | 5decbf5 | 2015-10-27 13:08:00 +0100 | [diff] [blame] | 75 | |
| 76 | if (!uc_pdata) |
| 77 | return -ENOSYS; |
| 78 | |
| 79 | *data_mask = uc_pdata->data_mask; |
| 80 | return 0; |
| 81 | } |
| 82 | |
Fabrice Gasnier | 63f004e | 2018-11-12 14:03:58 +0100 | [diff] [blame] | 83 | int adc_channel_mask(struct udevice *dev, unsigned int *channel_mask) |
| 84 | { |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 85 | struct adc_uclass_plat *uc_pdata = dev_get_uclass_plat(dev); |
Fabrice Gasnier | 63f004e | 2018-11-12 14:03:58 +0100 | [diff] [blame] | 86 | |
| 87 | if (!uc_pdata) |
| 88 | return -ENOSYS; |
| 89 | |
| 90 | *channel_mask = uc_pdata->channel_mask; |
| 91 | |
| 92 | return 0; |
| 93 | } |
| 94 | |
Przemyslaw Marczak | 5decbf5 | 2015-10-27 13:08:00 +0100 | [diff] [blame] | 95 | int adc_stop(struct udevice *dev) |
| 96 | { |
| 97 | const struct adc_ops *ops = dev_get_driver_ops(dev); |
| 98 | |
| 99 | if (!ops->stop) |
| 100 | return -ENOSYS; |
| 101 | |
| 102 | return ops->stop(dev); |
| 103 | } |
| 104 | |
| 105 | int adc_start_channel(struct udevice *dev, int channel) |
| 106 | { |
| 107 | const struct adc_ops *ops = dev_get_driver_ops(dev); |
| 108 | int ret; |
| 109 | |
| 110 | if (!ops->start_channel) |
| 111 | return -ENOSYS; |
| 112 | |
| 113 | ret = check_channel(dev, channel, CHECK_NUMBER, __func__); |
| 114 | if (ret) |
| 115 | return ret; |
| 116 | |
| 117 | ret = adc_supply_enable(dev); |
| 118 | if (ret) |
| 119 | return ret; |
| 120 | |
| 121 | return ops->start_channel(dev, channel); |
| 122 | } |
| 123 | |
| 124 | int adc_start_channels(struct udevice *dev, unsigned int channel_mask) |
| 125 | { |
| 126 | const struct adc_ops *ops = dev_get_driver_ops(dev); |
| 127 | int ret; |
| 128 | |
| 129 | if (!ops->start_channels) |
| 130 | return -ENOSYS; |
| 131 | |
| 132 | ret = check_channel(dev, channel_mask, CHECK_MASK, __func__); |
| 133 | if (ret) |
| 134 | return ret; |
| 135 | |
| 136 | ret = adc_supply_enable(dev); |
| 137 | if (ret) |
| 138 | return ret; |
| 139 | |
| 140 | return ops->start_channels(dev, channel_mask); |
| 141 | } |
| 142 | |
| 143 | int adc_channel_data(struct udevice *dev, int channel, unsigned int *data) |
| 144 | { |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 145 | struct adc_uclass_plat *uc_pdata = dev_get_uclass_plat(dev); |
Przemyslaw Marczak | 5decbf5 | 2015-10-27 13:08:00 +0100 | [diff] [blame] | 146 | const struct adc_ops *ops = dev_get_driver_ops(dev); |
| 147 | unsigned int timeout_us = uc_pdata->data_timeout_us; |
| 148 | int ret; |
| 149 | |
| 150 | if (!ops->channel_data) |
| 151 | return -ENOSYS; |
| 152 | |
| 153 | ret = check_channel(dev, channel, CHECK_NUMBER, __func__); |
| 154 | if (ret) |
| 155 | return ret; |
| 156 | |
| 157 | do { |
| 158 | ret = ops->channel_data(dev, channel, data); |
| 159 | if (!ret || ret != -EBUSY) |
| 160 | break; |
| 161 | |
| 162 | /* TODO: use timer uclass (for early calls). */ |
| 163 | sdelay(5); |
| 164 | } while (timeout_us--); |
| 165 | |
| 166 | return ret; |
| 167 | } |
| 168 | |
| 169 | int adc_channels_data(struct udevice *dev, unsigned int channel_mask, |
| 170 | struct adc_channel *channels) |
| 171 | { |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 172 | struct adc_uclass_plat *uc_pdata = dev_get_uclass_plat(dev); |
Przemyslaw Marczak | 5decbf5 | 2015-10-27 13:08:00 +0100 | [diff] [blame] | 173 | unsigned int timeout_us = uc_pdata->multidata_timeout_us; |
| 174 | const struct adc_ops *ops = dev_get_driver_ops(dev); |
| 175 | int ret; |
| 176 | |
| 177 | if (!ops->channels_data) |
| 178 | return -ENOSYS; |
| 179 | |
| 180 | ret = check_channel(dev, channel_mask, CHECK_MASK, __func__); |
| 181 | if (ret) |
| 182 | return ret; |
| 183 | |
| 184 | do { |
| 185 | ret = ops->channels_data(dev, channel_mask, channels); |
| 186 | if (!ret || ret != -EBUSY) |
| 187 | break; |
| 188 | |
| 189 | /* TODO: use timer uclass (for early calls). */ |
| 190 | sdelay(5); |
| 191 | } while (timeout_us--); |
| 192 | |
| 193 | return ret; |
| 194 | } |
| 195 | |
| 196 | int adc_channel_single_shot(const char *name, int channel, unsigned int *data) |
| 197 | { |
| 198 | struct udevice *dev; |
| 199 | int ret; |
| 200 | |
| 201 | ret = uclass_get_device_by_name(UCLASS_ADC, name, &dev); |
| 202 | if (ret) |
| 203 | return ret; |
| 204 | |
| 205 | ret = adc_start_channel(dev, channel); |
| 206 | if (ret) |
| 207 | return ret; |
| 208 | |
| 209 | ret = adc_channel_data(dev, channel, data); |
| 210 | if (ret) |
| 211 | return ret; |
| 212 | |
| 213 | return 0; |
| 214 | } |
| 215 | |
| 216 | static int _adc_channels_single_shot(struct udevice *dev, |
| 217 | unsigned int channel_mask, |
| 218 | struct adc_channel *channels) |
| 219 | { |
| 220 | unsigned int data; |
| 221 | int channel, ret; |
| 222 | |
| 223 | for (channel = 0; channel <= ADC_MAX_CHANNEL; channel++) { |
| 224 | /* Check channel bit. */ |
| 225 | if (!((channel_mask >> channel) & 0x1)) |
| 226 | continue; |
| 227 | |
| 228 | ret = adc_start_channel(dev, channel); |
| 229 | if (ret) |
| 230 | return ret; |
| 231 | |
| 232 | ret = adc_channel_data(dev, channel, &data); |
| 233 | if (ret) |
| 234 | return ret; |
| 235 | |
| 236 | channels->id = channel; |
| 237 | channels->data = data; |
| 238 | channels++; |
| 239 | } |
| 240 | |
| 241 | return 0; |
| 242 | } |
| 243 | |
| 244 | int adc_channels_single_shot(const char *name, unsigned int channel_mask, |
| 245 | struct adc_channel *channels) |
| 246 | { |
| 247 | struct udevice *dev; |
| 248 | int ret; |
| 249 | |
| 250 | ret = uclass_get_device_by_name(UCLASS_ADC, name, &dev); |
| 251 | if (ret) |
| 252 | return ret; |
| 253 | |
| 254 | ret = adc_start_channels(dev, channel_mask); |
| 255 | if (ret) |
| 256 | goto try_manual; |
| 257 | |
| 258 | ret = adc_channels_data(dev, channel_mask, channels); |
| 259 | if (ret) |
| 260 | return ret; |
| 261 | |
| 262 | return 0; |
| 263 | |
| 264 | try_manual: |
| 265 | if (ret != -ENOSYS) |
| 266 | return ret; |
| 267 | |
| 268 | return _adc_channels_single_shot(dev, channel_mask, channels); |
| 269 | } |
| 270 | |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 271 | static int adc_vdd_plat_update(struct udevice *dev) |
Przemyslaw Marczak | 5decbf5 | 2015-10-27 13:08:00 +0100 | [diff] [blame] | 272 | { |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 273 | struct adc_uclass_plat *uc_pdata = dev_get_uclass_plat(dev); |
Przemyslaw Marczak | 5decbf5 | 2015-10-27 13:08:00 +0100 | [diff] [blame] | 274 | int ret; |
| 275 | |
| 276 | /* Warning! |
| 277 | * This function can't return supply device before its bind. |
| 278 | * Please pay attention to proper fdt scan sequence. If ADC device |
| 279 | * will bind before its supply regulator device, then the below 'get' |
| 280 | * will return an error. |
| 281 | */ |
Fabrice Gasnier | a4a87f7 | 2018-07-24 16:31:29 +0200 | [diff] [blame] | 282 | if (!uc_pdata->vdd_supply) |
| 283 | return 0; |
Przemyslaw Marczak | 5decbf5 | 2015-10-27 13:08:00 +0100 | [diff] [blame] | 284 | |
| 285 | ret = regulator_get_value(uc_pdata->vdd_supply); |
| 286 | if (ret < 0) |
| 287 | return ret; |
| 288 | |
| 289 | uc_pdata->vdd_microvolts = ret; |
| 290 | |
| 291 | return 0; |
| 292 | } |
| 293 | |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 294 | static int adc_vss_plat_update(struct udevice *dev) |
Przemyslaw Marczak | 5decbf5 | 2015-10-27 13:08:00 +0100 | [diff] [blame] | 295 | { |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 296 | struct adc_uclass_plat *uc_pdata = dev_get_uclass_plat(dev); |
Przemyslaw Marczak | 5decbf5 | 2015-10-27 13:08:00 +0100 | [diff] [blame] | 297 | int ret; |
| 298 | |
Fabrice Gasnier | a4a87f7 | 2018-07-24 16:31:29 +0200 | [diff] [blame] | 299 | if (!uc_pdata->vss_supply) |
| 300 | return 0; |
Przemyslaw Marczak | 5decbf5 | 2015-10-27 13:08:00 +0100 | [diff] [blame] | 301 | |
| 302 | ret = regulator_get_value(uc_pdata->vss_supply); |
| 303 | if (ret < 0) |
| 304 | return ret; |
| 305 | |
| 306 | uc_pdata->vss_microvolts = ret; |
| 307 | |
| 308 | return 0; |
| 309 | } |
| 310 | |
| 311 | int adc_vdd_value(struct udevice *dev, int *uV) |
| 312 | { |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 313 | struct adc_uclass_plat *uc_pdata = dev_get_uclass_plat(dev); |
Przemyslaw Marczak | 5decbf5 | 2015-10-27 13:08:00 +0100 | [diff] [blame] | 314 | int ret, value_sign = uc_pdata->vdd_polarity_negative ? -1 : 1; |
| 315 | |
Przemyslaw Marczak | 5decbf5 | 2015-10-27 13:08:00 +0100 | [diff] [blame] | 316 | /* Update the regulator Value. */ |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 317 | ret = adc_vdd_plat_update(dev); |
Przemyslaw Marczak | 5decbf5 | 2015-10-27 13:08:00 +0100 | [diff] [blame] | 318 | if (ret) |
| 319 | return ret; |
Fabrice Gasnier | a4a87f7 | 2018-07-24 16:31:29 +0200 | [diff] [blame] | 320 | |
Przemyslaw Marczak | 5decbf5 | 2015-10-27 13:08:00 +0100 | [diff] [blame] | 321 | if (uc_pdata->vdd_microvolts == -ENODATA) |
| 322 | return -ENODATA; |
| 323 | |
| 324 | *uV = uc_pdata->vdd_microvolts * value_sign; |
| 325 | |
| 326 | return 0; |
| 327 | } |
| 328 | |
| 329 | int adc_vss_value(struct udevice *dev, int *uV) |
| 330 | { |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 331 | struct adc_uclass_plat *uc_pdata = dev_get_uclass_plat(dev); |
Przemyslaw Marczak | 5decbf5 | 2015-10-27 13:08:00 +0100 | [diff] [blame] | 332 | int ret, value_sign = uc_pdata->vss_polarity_negative ? -1 : 1; |
| 333 | |
Przemyslaw Marczak | 5decbf5 | 2015-10-27 13:08:00 +0100 | [diff] [blame] | 334 | /* Update the regulator Value. */ |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 335 | ret = adc_vss_plat_update(dev); |
Przemyslaw Marczak | 5decbf5 | 2015-10-27 13:08:00 +0100 | [diff] [blame] | 336 | if (ret) |
| 337 | return ret; |
Fabrice Gasnier | a4a87f7 | 2018-07-24 16:31:29 +0200 | [diff] [blame] | 338 | |
Przemyslaw Marczak | 5decbf5 | 2015-10-27 13:08:00 +0100 | [diff] [blame] | 339 | if (uc_pdata->vss_microvolts == -ENODATA) |
| 340 | return -ENODATA; |
| 341 | |
| 342 | *uV = uc_pdata->vss_microvolts * value_sign; |
| 343 | |
| 344 | return 0; |
| 345 | } |
| 346 | |
Fabrice Gasnier | 63f004e | 2018-11-12 14:03:58 +0100 | [diff] [blame] | 347 | int adc_raw_to_uV(struct udevice *dev, unsigned int raw, int *uV) |
| 348 | { |
| 349 | unsigned int data_mask; |
| 350 | int ret, val, vref; |
| 351 | u64 raw64 = raw; |
| 352 | |
| 353 | ret = adc_vdd_value(dev, &vref); |
| 354 | if (ret) |
| 355 | return ret; |
| 356 | |
| 357 | if (!adc_vss_value(dev, &val)) |
| 358 | vref -= val; |
| 359 | |
| 360 | ret = adc_data_mask(dev, &data_mask); |
| 361 | if (ret) |
| 362 | return ret; |
| 363 | |
| 364 | raw64 *= vref; |
| 365 | do_div(raw64, data_mask); |
| 366 | *uV = raw64; |
| 367 | |
| 368 | return 0; |
| 369 | } |
| 370 | |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 371 | static int adc_vdd_plat_set(struct udevice *dev) |
Przemyslaw Marczak | 5decbf5 | 2015-10-27 13:08:00 +0100 | [diff] [blame] | 372 | { |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 373 | struct adc_uclass_plat *uc_pdata = dev_get_uclass_plat(dev); |
Simon Glass | 53d788d | 2017-05-18 20:09:37 -0600 | [diff] [blame] | 374 | int ret; |
Przemyslaw Marczak | 5decbf5 | 2015-10-27 13:08:00 +0100 | [diff] [blame] | 375 | char *prop; |
| 376 | |
| 377 | prop = "vdd-polarity-negative"; |
Simon Glass | 53d788d | 2017-05-18 20:09:37 -0600 | [diff] [blame] | 378 | uc_pdata->vdd_polarity_negative = dev_read_bool(dev, prop); |
Przemyslaw Marczak | 5decbf5 | 2015-10-27 13:08:00 +0100 | [diff] [blame] | 379 | |
Fabrice Gasnier | a4a87f7 | 2018-07-24 16:31:29 +0200 | [diff] [blame] | 380 | /* Optionally get regulators */ |
| 381 | ret = device_get_supply_regulator(dev, "vdd-supply", |
| 382 | &uc_pdata->vdd_supply); |
| 383 | if (!ret) |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 384 | return adc_vdd_plat_update(dev); |
Fabrice Gasnier | a4a87f7 | 2018-07-24 16:31:29 +0200 | [diff] [blame] | 385 | |
Przemyslaw Marczak | 5decbf5 | 2015-10-27 13:08:00 +0100 | [diff] [blame] | 386 | if (ret != -ENOENT) |
| 387 | return ret; |
| 388 | |
| 389 | /* No vdd-supply phandle. */ |
| 390 | prop = "vdd-microvolts"; |
Simon Glass | 53d788d | 2017-05-18 20:09:37 -0600 | [diff] [blame] | 391 | uc_pdata->vdd_microvolts = dev_read_u32_default(dev, prop, -ENODATA); |
Przemyslaw Marczak | 5decbf5 | 2015-10-27 13:08:00 +0100 | [diff] [blame] | 392 | |
| 393 | return 0; |
| 394 | } |
| 395 | |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 396 | static int adc_vss_plat_set(struct udevice *dev) |
Przemyslaw Marczak | 5decbf5 | 2015-10-27 13:08:00 +0100 | [diff] [blame] | 397 | { |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 398 | struct adc_uclass_plat *uc_pdata = dev_get_uclass_plat(dev); |
Simon Glass | 53d788d | 2017-05-18 20:09:37 -0600 | [diff] [blame] | 399 | int ret; |
Przemyslaw Marczak | 5decbf5 | 2015-10-27 13:08:00 +0100 | [diff] [blame] | 400 | char *prop; |
| 401 | |
| 402 | prop = "vss-polarity-negative"; |
Simon Glass | 53d788d | 2017-05-18 20:09:37 -0600 | [diff] [blame] | 403 | uc_pdata->vss_polarity_negative = dev_read_bool(dev, prop); |
Przemyslaw Marczak | 5decbf5 | 2015-10-27 13:08:00 +0100 | [diff] [blame] | 404 | |
Fabrice Gasnier | a4a87f7 | 2018-07-24 16:31:29 +0200 | [diff] [blame] | 405 | ret = device_get_supply_regulator(dev, "vss-supply", |
| 406 | &uc_pdata->vss_supply); |
| 407 | if (!ret) |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 408 | return adc_vss_plat_update(dev); |
Fabrice Gasnier | a4a87f7 | 2018-07-24 16:31:29 +0200 | [diff] [blame] | 409 | |
Przemyslaw Marczak | 5decbf5 | 2015-10-27 13:08:00 +0100 | [diff] [blame] | 410 | if (ret != -ENOENT) |
| 411 | return ret; |
| 412 | |
| 413 | /* No vss-supply phandle. */ |
| 414 | prop = "vss-microvolts"; |
Simon Glass | 53d788d | 2017-05-18 20:09:37 -0600 | [diff] [blame] | 415 | uc_pdata->vss_microvolts = dev_read_u32_default(dev, prop, -ENODATA); |
Przemyslaw Marczak | 5decbf5 | 2015-10-27 13:08:00 +0100 | [diff] [blame] | 416 | |
| 417 | return 0; |
| 418 | } |
| 419 | |
| 420 | static int adc_pre_probe(struct udevice *dev) |
| 421 | { |
| 422 | int ret; |
| 423 | |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 424 | /* Set ADC VDD plat: polarity, uV, regulator (phandle). */ |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 425 | ret = adc_vdd_plat_set(dev); |
Przemyslaw Marczak | 5decbf5 | 2015-10-27 13:08:00 +0100 | [diff] [blame] | 426 | if (ret) |
Masahiro Yamada | 9b643e3 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 427 | pr_err("%s: Can't update Vdd. Error: %d", dev->name, ret); |
Przemyslaw Marczak | 5decbf5 | 2015-10-27 13:08:00 +0100 | [diff] [blame] | 428 | |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 429 | /* Set ADC VSS plat: polarity, uV, regulator (phandle). */ |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 430 | ret = adc_vss_plat_set(dev); |
Przemyslaw Marczak | 5decbf5 | 2015-10-27 13:08:00 +0100 | [diff] [blame] | 431 | if (ret) |
Masahiro Yamada | 9b643e3 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 432 | pr_err("%s: Can't update Vss. Error: %d", dev->name, ret); |
Przemyslaw Marczak | 5decbf5 | 2015-10-27 13:08:00 +0100 | [diff] [blame] | 433 | |
| 434 | return 0; |
| 435 | } |
| 436 | |
| 437 | UCLASS_DRIVER(adc) = { |
| 438 | .id = UCLASS_ADC, |
| 439 | .name = "adc", |
| 440 | .pre_probe = adc_pre_probe, |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 441 | .per_device_plat_auto = ADC_UCLASS_PLATDATA_SIZE, |
Przemyslaw Marczak | 5decbf5 | 2015-10-27 13:08:00 +0100 | [diff] [blame] | 442 | }; |