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