2 * Copyright (C) 2015 Samsung Electronics
3 * Przemyslaw Marczak <p.marczak@samsung.com>
5 * SPDX-License-Identifier: GPL-2.0+
12 #include <dm/device-internal.h>
13 #include <dm/uclass-internal.h>
15 #include <power/regulator.h>
17 DECLARE_GLOBAL_DATA_PTR;
19 #define ADC_UCLASS_PLATDATA_SIZE sizeof(struct adc_uclass_platdata)
20 #define CHECK_NUMBER true
21 #define CHECK_MASK (!CHECK_NUMBER)
23 /* TODO: add support for timer uclass (for early calls) */
24 #ifdef CONFIG_SANDBOX_ARCH
25 #define sdelay(x) udelay(x)
27 extern void sdelay(unsigned long loops);
30 static int check_channel(struct udevice *dev, int value, bool number_or_mask,
31 const char *caller_function)
33 struct adc_uclass_platdata *uc_pdata = dev_get_uclass_platdata(dev);
34 unsigned mask = number_or_mask ? (1 << value) : value;
36 /* For the real ADC hardware, some ADC channels can be inactive.
37 * For example if device has 4 analog channels, and only channels
38 * 1-st and 3-rd are valid, then channel mask is: 0b1010, so request
39 * with mask 0b1110 should return an error.
41 if ((uc_pdata->channel_mask >= mask) && (uc_pdata->channel_mask & mask))
44 printf("Error in %s/%s().\nWrong channel selection for device: %s\n",
45 __FILE__, caller_function, dev->name);
50 static int adc_supply_enable(struct udevice *dev)
52 struct adc_uclass_platdata *uc_pdata = dev_get_uclass_platdata(dev);
53 const char *supply_type;
56 if (uc_pdata->vdd_supply) {
58 ret = regulator_set_enable(uc_pdata->vdd_supply, true);
61 if (!ret && uc_pdata->vss_supply) {
63 ret = regulator_set_enable(uc_pdata->vss_supply, true);
67 error("%s: can't enable %s-supply!", dev->name, supply_type);
72 int adc_data_mask(struct udevice *dev, unsigned int *data_mask)
74 struct adc_uclass_platdata *uc_pdata = dev_get_uclass_platdata(dev);
79 *data_mask = uc_pdata->data_mask;
83 int adc_stop(struct udevice *dev)
85 const struct adc_ops *ops = dev_get_driver_ops(dev);
90 return ops->stop(dev);
93 int adc_start_channel(struct udevice *dev, int channel)
95 const struct adc_ops *ops = dev_get_driver_ops(dev);
98 if (!ops->start_channel)
101 ret = check_channel(dev, channel, CHECK_NUMBER, __func__);
105 ret = adc_supply_enable(dev);
109 return ops->start_channel(dev, channel);
112 int adc_start_channels(struct udevice *dev, unsigned int channel_mask)
114 const struct adc_ops *ops = dev_get_driver_ops(dev);
117 if (!ops->start_channels)
120 ret = check_channel(dev, channel_mask, CHECK_MASK, __func__);
124 ret = adc_supply_enable(dev);
128 return ops->start_channels(dev, channel_mask);
131 int adc_channel_data(struct udevice *dev, int channel, unsigned int *data)
133 struct adc_uclass_platdata *uc_pdata = dev_get_uclass_platdata(dev);
134 const struct adc_ops *ops = dev_get_driver_ops(dev);
135 unsigned int timeout_us = uc_pdata->data_timeout_us;
138 if (!ops->channel_data)
141 ret = check_channel(dev, channel, CHECK_NUMBER, __func__);
146 ret = ops->channel_data(dev, channel, data);
147 if (!ret || ret != -EBUSY)
150 /* TODO: use timer uclass (for early calls). */
152 } while (timeout_us--);
157 int adc_channels_data(struct udevice *dev, unsigned int channel_mask,
158 struct adc_channel *channels)
160 struct adc_uclass_platdata *uc_pdata = dev_get_uclass_platdata(dev);
161 unsigned int timeout_us = uc_pdata->multidata_timeout_us;
162 const struct adc_ops *ops = dev_get_driver_ops(dev);
165 if (!ops->channels_data)
168 ret = check_channel(dev, channel_mask, CHECK_MASK, __func__);
173 ret = ops->channels_data(dev, channel_mask, channels);
174 if (!ret || ret != -EBUSY)
177 /* TODO: use timer uclass (for early calls). */
179 } while (timeout_us--);
184 int adc_channel_single_shot(const char *name, int channel, unsigned int *data)
189 ret = uclass_get_device_by_name(UCLASS_ADC, name, &dev);
193 ret = adc_start_channel(dev, channel);
197 ret = adc_channel_data(dev, channel, data);
204 static int _adc_channels_single_shot(struct udevice *dev,
205 unsigned int channel_mask,
206 struct adc_channel *channels)
211 for (channel = 0; channel <= ADC_MAX_CHANNEL; channel++) {
212 /* Check channel bit. */
213 if (!((channel_mask >> channel) & 0x1))
216 ret = adc_start_channel(dev, channel);
220 ret = adc_channel_data(dev, channel, &data);
224 channels->id = channel;
225 channels->data = data;
232 int adc_channels_single_shot(const char *name, unsigned int channel_mask,
233 struct adc_channel *channels)
238 ret = uclass_get_device_by_name(UCLASS_ADC, name, &dev);
242 ret = adc_start_channels(dev, channel_mask);
246 ret = adc_channels_data(dev, channel_mask, channels);
256 return _adc_channels_single_shot(dev, channel_mask, channels);
259 static int adc_vdd_platdata_update(struct udevice *dev)
261 struct adc_uclass_platdata *uc_pdata = dev_get_uclass_platdata(dev);
265 * This function can't return supply device before its bind.
266 * Please pay attention to proper fdt scan sequence. If ADC device
267 * will bind before its supply regulator device, then the below 'get'
268 * will return an error.
270 ret = device_get_supply_regulator(dev, "vdd-supply",
271 &uc_pdata->vdd_supply);
275 ret = regulator_get_value(uc_pdata->vdd_supply);
279 uc_pdata->vdd_microvolts = ret;
284 static int adc_vss_platdata_update(struct udevice *dev)
286 struct adc_uclass_platdata *uc_pdata = dev_get_uclass_platdata(dev);
289 ret = device_get_supply_regulator(dev, "vss-supply",
290 &uc_pdata->vss_supply);
294 ret = regulator_get_value(uc_pdata->vss_supply);
298 uc_pdata->vss_microvolts = ret;
303 int adc_vdd_value(struct udevice *dev, int *uV)
305 struct adc_uclass_platdata *uc_pdata = dev_get_uclass_platdata(dev);
306 int ret, value_sign = uc_pdata->vdd_polarity_negative ? -1 : 1;
308 if (!uc_pdata->vdd_supply)
311 /* Update the regulator Value. */
312 ret = adc_vdd_platdata_update(dev);
316 if (uc_pdata->vdd_microvolts == -ENODATA)
319 *uV = uc_pdata->vdd_microvolts * value_sign;
324 int adc_vss_value(struct udevice *dev, int *uV)
326 struct adc_uclass_platdata *uc_pdata = dev_get_uclass_platdata(dev);
327 int ret, value_sign = uc_pdata->vss_polarity_negative ? -1 : 1;
329 if (!uc_pdata->vss_supply)
332 /* Update the regulator Value. */
333 ret = adc_vss_platdata_update(dev);
337 if (uc_pdata->vss_microvolts == -ENODATA)
340 *uV = uc_pdata->vss_microvolts * value_sign;
345 static int adc_vdd_platdata_set(struct udevice *dev)
347 struct adc_uclass_platdata *uc_pdata = dev_get_uclass_platdata(dev);
348 int ret, offset = dev->of_offset;
349 const void *fdt = gd->fdt_blob;
352 prop = "vdd-polarity-negative";
353 uc_pdata->vdd_polarity_negative = fdtdec_get_bool(fdt, offset, prop);
355 ret = adc_vdd_platdata_update(dev);
359 /* No vdd-supply phandle. */
360 prop = "vdd-microvolts";
361 uc_pdata->vdd_microvolts = fdtdec_get_int(fdt, offset, prop, -ENODATA);
366 static int adc_vss_platdata_set(struct udevice *dev)
368 struct adc_uclass_platdata *uc_pdata = dev_get_uclass_platdata(dev);
369 int ret, offset = dev->of_offset;
370 const void *fdt = gd->fdt_blob;
373 prop = "vss-polarity-negative";
374 uc_pdata->vss_polarity_negative = fdtdec_get_bool(fdt, offset, prop);
376 ret = adc_vss_platdata_update(dev);
380 /* No vss-supply phandle. */
381 prop = "vss-microvolts";
382 uc_pdata->vss_microvolts = fdtdec_get_int(fdt, offset, prop, -ENODATA);
387 static int adc_pre_probe(struct udevice *dev)
391 /* Set ADC VDD platdata: polarity, uV, regulator (phandle). */
392 ret = adc_vdd_platdata_set(dev);
394 error("%s: Can't update Vdd. Error: %d", dev->name, ret);
396 /* Set ADC VSS platdata: polarity, uV, regulator (phandle). */
397 ret = adc_vss_platdata_set(dev);
399 error("%s: Can't update Vss. Error: %d", dev->name, ret);
404 UCLASS_DRIVER(adc) = {
407 .pre_probe = adc_pre_probe,
408 .per_device_platdata_auto_alloc_size = ADC_UCLASS_PLATDATA_SIZE,