1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2015 Samsung Electronics
4 * Przemyslaw Marczak <p.marczak@samsung.com>
12 #include <dm/device-internal.h>
13 #include <dm/uclass-internal.h>
15 #include <linux/delay.h>
16 #include <power/regulator.h>
18 #define ADC_UCLASS_PLATDATA_SIZE sizeof(struct adc_uclass_platdata)
19 #define CHECK_NUMBER true
20 #define CHECK_MASK (!CHECK_NUMBER)
22 /* TODO: add support for timer uclass (for early calls) */
23 #ifdef CONFIG_SANDBOX_ARCH
24 #define sdelay(x) udelay(x)
26 extern void sdelay(unsigned long loops);
29 static int check_channel(struct udevice *dev, int value, bool number_or_mask,
30 const char *caller_function)
32 struct adc_uclass_platdata *uc_pdata = dev_get_uclass_platdata(dev);
33 unsigned mask = number_or_mask ? (1 << value) : value;
35 /* For the real ADC hardware, some ADC channels can be inactive.
36 * For example if device has 4 analog channels, and only channels
37 * 1-st and 3-rd are valid, then channel mask is: 0b1010, so request
38 * with mask 0b1110 should return an error.
40 if ((uc_pdata->channel_mask >= mask) && (uc_pdata->channel_mask & mask))
43 printf("Error in %s/%s().\nWrong channel selection for device: %s\n",
44 __FILE__, caller_function, dev->name);
49 static int adc_supply_enable(struct udevice *dev)
51 struct adc_uclass_platdata *uc_pdata = dev_get_uclass_platdata(dev);
52 const char *supply_type;
55 if (uc_pdata->vdd_supply) {
57 ret = regulator_set_enable(uc_pdata->vdd_supply, true);
60 if (!ret && uc_pdata->vss_supply) {
62 ret = regulator_set_enable(uc_pdata->vss_supply, true);
66 pr_err("%s: can't enable %s-supply!", dev->name, supply_type);
71 int adc_data_mask(struct udevice *dev, unsigned int *data_mask)
73 struct adc_uclass_platdata *uc_pdata = dev_get_uclass_platdata(dev);
78 *data_mask = uc_pdata->data_mask;
82 int adc_channel_mask(struct udevice *dev, unsigned int *channel_mask)
84 struct adc_uclass_platdata *uc_pdata = dev_get_uclass_platdata(dev);
89 *channel_mask = uc_pdata->channel_mask;
94 int adc_stop(struct udevice *dev)
96 const struct adc_ops *ops = dev_get_driver_ops(dev);
101 return ops->stop(dev);
104 int adc_start_channel(struct udevice *dev, int channel)
106 const struct adc_ops *ops = dev_get_driver_ops(dev);
109 if (!ops->start_channel)
112 ret = check_channel(dev, channel, CHECK_NUMBER, __func__);
116 ret = adc_supply_enable(dev);
120 return ops->start_channel(dev, channel);
123 int adc_start_channels(struct udevice *dev, unsigned int channel_mask)
125 const struct adc_ops *ops = dev_get_driver_ops(dev);
128 if (!ops->start_channels)
131 ret = check_channel(dev, channel_mask, CHECK_MASK, __func__);
135 ret = adc_supply_enable(dev);
139 return ops->start_channels(dev, channel_mask);
142 int adc_channel_data(struct udevice *dev, int channel, unsigned int *data)
144 struct adc_uclass_platdata *uc_pdata = dev_get_uclass_platdata(dev);
145 const struct adc_ops *ops = dev_get_driver_ops(dev);
146 unsigned int timeout_us = uc_pdata->data_timeout_us;
149 if (!ops->channel_data)
152 ret = check_channel(dev, channel, CHECK_NUMBER, __func__);
157 ret = ops->channel_data(dev, channel, data);
158 if (!ret || ret != -EBUSY)
161 /* TODO: use timer uclass (for early calls). */
163 } while (timeout_us--);
168 int adc_channels_data(struct udevice *dev, unsigned int channel_mask,
169 struct adc_channel *channels)
171 struct adc_uclass_platdata *uc_pdata = dev_get_uclass_platdata(dev);
172 unsigned int timeout_us = uc_pdata->multidata_timeout_us;
173 const struct adc_ops *ops = dev_get_driver_ops(dev);
176 if (!ops->channels_data)
179 ret = check_channel(dev, channel_mask, CHECK_MASK, __func__);
184 ret = ops->channels_data(dev, channel_mask, channels);
185 if (!ret || ret != -EBUSY)
188 /* TODO: use timer uclass (for early calls). */
190 } while (timeout_us--);
195 int adc_channel_single_shot(const char *name, int channel, unsigned int *data)
200 ret = uclass_get_device_by_name(UCLASS_ADC, name, &dev);
204 ret = adc_start_channel(dev, channel);
208 ret = adc_channel_data(dev, channel, data);
215 static int _adc_channels_single_shot(struct udevice *dev,
216 unsigned int channel_mask,
217 struct adc_channel *channels)
222 for (channel = 0; channel <= ADC_MAX_CHANNEL; channel++) {
223 /* Check channel bit. */
224 if (!((channel_mask >> channel) & 0x1))
227 ret = adc_start_channel(dev, channel);
231 ret = adc_channel_data(dev, channel, &data);
235 channels->id = channel;
236 channels->data = data;
243 int adc_channels_single_shot(const char *name, unsigned int channel_mask,
244 struct adc_channel *channels)
249 ret = uclass_get_device_by_name(UCLASS_ADC, name, &dev);
253 ret = adc_start_channels(dev, channel_mask);
257 ret = adc_channels_data(dev, channel_mask, channels);
267 return _adc_channels_single_shot(dev, channel_mask, channels);
270 static int adc_vdd_platdata_update(struct udevice *dev)
272 struct adc_uclass_platdata *uc_pdata = dev_get_uclass_platdata(dev);
276 * This function can't return supply device before its bind.
277 * Please pay attention to proper fdt scan sequence. If ADC device
278 * will bind before its supply regulator device, then the below 'get'
279 * will return an error.
281 if (!uc_pdata->vdd_supply)
284 ret = regulator_get_value(uc_pdata->vdd_supply);
288 uc_pdata->vdd_microvolts = ret;
293 static int adc_vss_platdata_update(struct udevice *dev)
295 struct adc_uclass_platdata *uc_pdata = dev_get_uclass_platdata(dev);
298 if (!uc_pdata->vss_supply)
301 ret = regulator_get_value(uc_pdata->vss_supply);
305 uc_pdata->vss_microvolts = ret;
310 int adc_vdd_value(struct udevice *dev, int *uV)
312 struct adc_uclass_platdata *uc_pdata = dev_get_uclass_platdata(dev);
313 int ret, value_sign = uc_pdata->vdd_polarity_negative ? -1 : 1;
315 /* Update the regulator Value. */
316 ret = adc_vdd_platdata_update(dev);
320 if (uc_pdata->vdd_microvolts == -ENODATA)
323 *uV = uc_pdata->vdd_microvolts * value_sign;
328 int adc_vss_value(struct udevice *dev, int *uV)
330 struct adc_uclass_platdata *uc_pdata = dev_get_uclass_platdata(dev);
331 int ret, value_sign = uc_pdata->vss_polarity_negative ? -1 : 1;
333 /* Update the regulator Value. */
334 ret = adc_vss_platdata_update(dev);
338 if (uc_pdata->vss_microvolts == -ENODATA)
341 *uV = uc_pdata->vss_microvolts * value_sign;
346 int adc_raw_to_uV(struct udevice *dev, unsigned int raw, int *uV)
348 unsigned int data_mask;
352 ret = adc_vdd_value(dev, &vref);
356 if (!adc_vss_value(dev, &val))
359 ret = adc_data_mask(dev, &data_mask);
364 do_div(raw64, data_mask);
370 static int adc_vdd_platdata_set(struct udevice *dev)
372 struct adc_uclass_platdata *uc_pdata = dev_get_uclass_platdata(dev);
376 prop = "vdd-polarity-negative";
377 uc_pdata->vdd_polarity_negative = dev_read_bool(dev, prop);
379 /* Optionally get regulators */
380 ret = device_get_supply_regulator(dev, "vdd-supply",
381 &uc_pdata->vdd_supply);
383 return adc_vdd_platdata_update(dev);
388 /* No vdd-supply phandle. */
389 prop = "vdd-microvolts";
390 uc_pdata->vdd_microvolts = dev_read_u32_default(dev, prop, -ENODATA);
395 static int adc_vss_platdata_set(struct udevice *dev)
397 struct adc_uclass_platdata *uc_pdata = dev_get_uclass_platdata(dev);
401 prop = "vss-polarity-negative";
402 uc_pdata->vss_polarity_negative = dev_read_bool(dev, prop);
404 ret = device_get_supply_regulator(dev, "vss-supply",
405 &uc_pdata->vss_supply);
407 return adc_vss_platdata_update(dev);
412 /* No vss-supply phandle. */
413 prop = "vss-microvolts";
414 uc_pdata->vss_microvolts = dev_read_u32_default(dev, prop, -ENODATA);
419 static int adc_pre_probe(struct udevice *dev)
423 /* Set ADC VDD platdata: polarity, uV, regulator (phandle). */
424 ret = adc_vdd_platdata_set(dev);
426 pr_err("%s: Can't update Vdd. Error: %d", dev->name, ret);
428 /* Set ADC VSS platdata: polarity, uV, regulator (phandle). */
429 ret = adc_vss_platdata_set(dev);
431 pr_err("%s: Can't update Vss. Error: %d", dev->name, ret);
436 UCLASS_DRIVER(adc) = {
439 .pre_probe = adc_pre_probe,
440 .per_device_platdata_auto_alloc_size = ADC_UCLASS_PLATDATA_SIZE,