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 <power/regulator.h>
17 #define ADC_UCLASS_PLATDATA_SIZE sizeof(struct adc_uclass_platdata)
18 #define CHECK_NUMBER true
19 #define CHECK_MASK (!CHECK_NUMBER)
21 /* TODO: add support for timer uclass (for early calls) */
22 #ifdef CONFIG_SANDBOX_ARCH
23 #define sdelay(x) udelay(x)
25 extern void sdelay(unsigned long loops);
28 static int check_channel(struct udevice *dev, int value, bool number_or_mask,
29 const char *caller_function)
31 struct adc_uclass_platdata *uc_pdata = dev_get_uclass_platdata(dev);
32 unsigned mask = number_or_mask ? (1 << value) : value;
34 /* For the real ADC hardware, some ADC channels can be inactive.
35 * For example if device has 4 analog channels, and only channels
36 * 1-st and 3-rd are valid, then channel mask is: 0b1010, so request
37 * with mask 0b1110 should return an error.
39 if ((uc_pdata->channel_mask >= mask) && (uc_pdata->channel_mask & mask))
42 printf("Error in %s/%s().\nWrong channel selection for device: %s\n",
43 __FILE__, caller_function, dev->name);
48 static int adc_supply_enable(struct udevice *dev)
50 struct adc_uclass_platdata *uc_pdata = dev_get_uclass_platdata(dev);
51 const char *supply_type;
54 if (uc_pdata->vdd_supply) {
56 ret = regulator_set_enable(uc_pdata->vdd_supply, true);
59 if (!ret && uc_pdata->vss_supply) {
61 ret = regulator_set_enable(uc_pdata->vss_supply, true);
65 pr_err("%s: can't enable %s-supply!", dev->name, supply_type);
70 int adc_data_mask(struct udevice *dev, unsigned int *data_mask)
72 struct adc_uclass_platdata *uc_pdata = dev_get_uclass_platdata(dev);
77 *data_mask = uc_pdata->data_mask;
81 int adc_channel_mask(struct udevice *dev, unsigned int *channel_mask)
83 struct adc_uclass_platdata *uc_pdata = dev_get_uclass_platdata(dev);
88 *channel_mask = uc_pdata->channel_mask;
93 int adc_stop(struct udevice *dev)
95 const struct adc_ops *ops = dev_get_driver_ops(dev);
100 return ops->stop(dev);
103 int adc_start_channel(struct udevice *dev, int channel)
105 const struct adc_ops *ops = dev_get_driver_ops(dev);
108 if (!ops->start_channel)
111 ret = check_channel(dev, channel, CHECK_NUMBER, __func__);
115 ret = adc_supply_enable(dev);
119 return ops->start_channel(dev, channel);
122 int adc_start_channels(struct udevice *dev, unsigned int channel_mask)
124 const struct adc_ops *ops = dev_get_driver_ops(dev);
127 if (!ops->start_channels)
130 ret = check_channel(dev, channel_mask, CHECK_MASK, __func__);
134 ret = adc_supply_enable(dev);
138 return ops->start_channels(dev, channel_mask);
141 int adc_channel_data(struct udevice *dev, int channel, unsigned int *data)
143 struct adc_uclass_platdata *uc_pdata = dev_get_uclass_platdata(dev);
144 const struct adc_ops *ops = dev_get_driver_ops(dev);
145 unsigned int timeout_us = uc_pdata->data_timeout_us;
148 if (!ops->channel_data)
151 ret = check_channel(dev, channel, CHECK_NUMBER, __func__);
156 ret = ops->channel_data(dev, channel, data);
157 if (!ret || ret != -EBUSY)
160 /* TODO: use timer uclass (for early calls). */
162 } while (timeout_us--);
167 int adc_channels_data(struct udevice *dev, unsigned int channel_mask,
168 struct adc_channel *channels)
170 struct adc_uclass_platdata *uc_pdata = dev_get_uclass_platdata(dev);
171 unsigned int timeout_us = uc_pdata->multidata_timeout_us;
172 const struct adc_ops *ops = dev_get_driver_ops(dev);
175 if (!ops->channels_data)
178 ret = check_channel(dev, channel_mask, CHECK_MASK, __func__);
183 ret = ops->channels_data(dev, channel_mask, channels);
184 if (!ret || ret != -EBUSY)
187 /* TODO: use timer uclass (for early calls). */
189 } while (timeout_us--);
194 int adc_channel_single_shot(const char *name, int channel, unsigned int *data)
199 ret = uclass_get_device_by_name(UCLASS_ADC, name, &dev);
203 ret = adc_start_channel(dev, channel);
207 ret = adc_channel_data(dev, channel, data);
214 static int _adc_channels_single_shot(struct udevice *dev,
215 unsigned int channel_mask,
216 struct adc_channel *channels)
221 for (channel = 0; channel <= ADC_MAX_CHANNEL; channel++) {
222 /* Check channel bit. */
223 if (!((channel_mask >> channel) & 0x1))
226 ret = adc_start_channel(dev, channel);
230 ret = adc_channel_data(dev, channel, &data);
234 channels->id = channel;
235 channels->data = data;
242 int adc_channels_single_shot(const char *name, unsigned int channel_mask,
243 struct adc_channel *channels)
248 ret = uclass_get_device_by_name(UCLASS_ADC, name, &dev);
252 ret = adc_start_channels(dev, channel_mask);
256 ret = adc_channels_data(dev, channel_mask, channels);
266 return _adc_channels_single_shot(dev, channel_mask, channels);
269 static int adc_vdd_platdata_update(struct udevice *dev)
271 struct adc_uclass_platdata *uc_pdata = dev_get_uclass_platdata(dev);
275 * This function can't return supply device before its bind.
276 * Please pay attention to proper fdt scan sequence. If ADC device
277 * will bind before its supply regulator device, then the below 'get'
278 * will return an error.
280 if (!uc_pdata->vdd_supply)
283 ret = regulator_get_value(uc_pdata->vdd_supply);
287 uc_pdata->vdd_microvolts = ret;
292 static int adc_vss_platdata_update(struct udevice *dev)
294 struct adc_uclass_platdata *uc_pdata = dev_get_uclass_platdata(dev);
297 if (!uc_pdata->vss_supply)
300 ret = regulator_get_value(uc_pdata->vss_supply);
304 uc_pdata->vss_microvolts = ret;
309 int adc_vdd_value(struct udevice *dev, int *uV)
311 struct adc_uclass_platdata *uc_pdata = dev_get_uclass_platdata(dev);
312 int ret, value_sign = uc_pdata->vdd_polarity_negative ? -1 : 1;
314 /* Update the regulator Value. */
315 ret = adc_vdd_platdata_update(dev);
319 if (uc_pdata->vdd_microvolts == -ENODATA)
322 *uV = uc_pdata->vdd_microvolts * value_sign;
327 int adc_vss_value(struct udevice *dev, int *uV)
329 struct adc_uclass_platdata *uc_pdata = dev_get_uclass_platdata(dev);
330 int ret, value_sign = uc_pdata->vss_polarity_negative ? -1 : 1;
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 int adc_raw_to_uV(struct udevice *dev, unsigned int raw, int *uV)
347 unsigned int data_mask;
351 ret = adc_vdd_value(dev, &vref);
355 if (!adc_vss_value(dev, &val))
358 ret = adc_data_mask(dev, &data_mask);
363 do_div(raw64, data_mask);
369 static int adc_vdd_platdata_set(struct udevice *dev)
371 struct adc_uclass_platdata *uc_pdata = dev_get_uclass_platdata(dev);
375 prop = "vdd-polarity-negative";
376 uc_pdata->vdd_polarity_negative = dev_read_bool(dev, prop);
378 /* Optionally get regulators */
379 ret = device_get_supply_regulator(dev, "vdd-supply",
380 &uc_pdata->vdd_supply);
382 return adc_vdd_platdata_update(dev);
387 /* No vdd-supply phandle. */
388 prop = "vdd-microvolts";
389 uc_pdata->vdd_microvolts = dev_read_u32_default(dev, prop, -ENODATA);
394 static int adc_vss_platdata_set(struct udevice *dev)
396 struct adc_uclass_platdata *uc_pdata = dev_get_uclass_platdata(dev);
400 prop = "vss-polarity-negative";
401 uc_pdata->vss_polarity_negative = dev_read_bool(dev, prop);
403 ret = device_get_supply_regulator(dev, "vss-supply",
404 &uc_pdata->vss_supply);
406 return adc_vss_platdata_update(dev);
411 /* No vss-supply phandle. */
412 prop = "vss-microvolts";
413 uc_pdata->vss_microvolts = dev_read_u32_default(dev, prop, -ENODATA);
418 static int adc_pre_probe(struct udevice *dev)
422 /* Set ADC VDD platdata: polarity, uV, regulator (phandle). */
423 ret = adc_vdd_platdata_set(dev);
425 pr_err("%s: Can't update Vdd. Error: %d", dev->name, ret);
427 /* Set ADC VSS platdata: polarity, uV, regulator (phandle). */
428 ret = adc_vss_platdata_set(dev);
430 pr_err("%s: Can't update Vss. Error: %d", dev->name, ret);
435 UCLASS_DRIVER(adc) = {
438 .pre_probe = adc_pre_probe,
439 .per_device_platdata_auto_alloc_size = ADC_UCLASS_PLATDATA_SIZE,