blob: 67137ffb3414fa8e2b328fee4daa17a5281d3617 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Przemyslaw Marczak5decbf52015-10-27 13:08:00 +01002/*
3 * Copyright (C) 2015 Samsung Electronics
4 * Przemyslaw Marczak <p.marczak@samsung.com>
Przemyslaw Marczak5decbf52015-10-27 13:08:00 +01005 */
6
Patrick Delaunayb953ec22021-04-27 11:02:19 +02007#define LOG_CATEGORY UCLASS_ADC
8
Przemyslaw Marczak5decbf52015-10-27 13:08:00 +01009#include <common.h>
10#include <errno.h>
Fabrice Gasnier63f004e2018-11-12 14:03:58 +010011#include <div64.h>
Przemyslaw Marczak5decbf52015-10-27 13:08:00 +010012#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 Glassc05ed002020-05-10 11:40:11 -060017#include <linux/delay.h>
Przemyslaw Marczak5decbf52015-10-27 13:08:00 +010018#include <power/regulator.h>
19
Simon Glasscaa4daa2020-12-03 16:55:18 -070020#define ADC_UCLASS_PLATDATA_SIZE sizeof(struct adc_uclass_plat)
Przemyslaw Marczak5decbf52015-10-27 13:08:00 +010021#define CHECK_NUMBER true
22#define CHECK_MASK (!CHECK_NUMBER)
23
24/* TODO: add support for timer uclass (for early calls) */
25#ifdef CONFIG_SANDBOX_ARCH
26#define sdelay(x) udelay(x)
27#else
28extern void sdelay(unsigned long loops);
29#endif
30
31static int check_channel(struct udevice *dev, int value, bool number_or_mask,
32 const char *caller_function)
33{
Simon Glasscaa4daa2020-12-03 16:55:18 -070034 struct adc_uclass_plat *uc_pdata = dev_get_uclass_plat(dev);
Przemyslaw Marczak5decbf52015-10-27 13:08:00 +010035 unsigned mask = number_or_mask ? (1 << value) : value;
36
37 /* For the real ADC hardware, some ADC channels can be inactive.
38 * For example if device has 4 analog channels, and only channels
39 * 1-st and 3-rd are valid, then channel mask is: 0b1010, so request
40 * with mask 0b1110 should return an error.
41 */
42 if ((uc_pdata->channel_mask >= mask) && (uc_pdata->channel_mask & mask))
43 return 0;
44
45 printf("Error in %s/%s().\nWrong channel selection for device: %s\n",
46 __FILE__, caller_function, dev->name);
47
48 return -EINVAL;
49}
50
51static int adc_supply_enable(struct udevice *dev)
52{
Simon Glasscaa4daa2020-12-03 16:55:18 -070053 struct adc_uclass_plat *uc_pdata = dev_get_uclass_plat(dev);
Przemyslaw Marczak5decbf52015-10-27 13:08:00 +010054 const char *supply_type;
55 int ret = 0;
56
57 if (uc_pdata->vdd_supply) {
58 supply_type = "vdd";
59 ret = regulator_set_enable(uc_pdata->vdd_supply, true);
60 }
61
62 if (!ret && uc_pdata->vss_supply) {
63 supply_type = "vss";
64 ret = regulator_set_enable(uc_pdata->vss_supply, true);
65 }
66
67 if (ret)
Masahiro Yamada9b643e32017-09-16 14:10:41 +090068 pr_err("%s: can't enable %s-supply!", dev->name, supply_type);
Przemyslaw Marczak5decbf52015-10-27 13:08:00 +010069
70 return ret;
71}
72
73int adc_data_mask(struct udevice *dev, unsigned int *data_mask)
74{
Simon Glasscaa4daa2020-12-03 16:55:18 -070075 struct adc_uclass_plat *uc_pdata = dev_get_uclass_plat(dev);
Przemyslaw Marczak5decbf52015-10-27 13:08:00 +010076
77 if (!uc_pdata)
78 return -ENOSYS;
79
80 *data_mask = uc_pdata->data_mask;
81 return 0;
82}
83
Fabrice Gasnier63f004e2018-11-12 14:03:58 +010084int adc_channel_mask(struct udevice *dev, unsigned int *channel_mask)
85{
Simon Glasscaa4daa2020-12-03 16:55:18 -070086 struct adc_uclass_plat *uc_pdata = dev_get_uclass_plat(dev);
Fabrice Gasnier63f004e2018-11-12 14:03:58 +010087
88 if (!uc_pdata)
89 return -ENOSYS;
90
91 *channel_mask = uc_pdata->channel_mask;
92
93 return 0;
94}
95
Przemyslaw Marczak5decbf52015-10-27 13:08:00 +010096int adc_stop(struct udevice *dev)
97{
98 const struct adc_ops *ops = dev_get_driver_ops(dev);
99
100 if (!ops->stop)
101 return -ENOSYS;
102
103 return ops->stop(dev);
104}
105
106int adc_start_channel(struct udevice *dev, int channel)
107{
108 const struct adc_ops *ops = dev_get_driver_ops(dev);
109 int ret;
110
111 if (!ops->start_channel)
112 return -ENOSYS;
113
114 ret = check_channel(dev, channel, CHECK_NUMBER, __func__);
115 if (ret)
116 return ret;
117
118 ret = adc_supply_enable(dev);
119 if (ret)
120 return ret;
121
122 return ops->start_channel(dev, channel);
123}
124
125int adc_start_channels(struct udevice *dev, unsigned int channel_mask)
126{
127 const struct adc_ops *ops = dev_get_driver_ops(dev);
128 int ret;
129
130 if (!ops->start_channels)
131 return -ENOSYS;
132
133 ret = check_channel(dev, channel_mask, CHECK_MASK, __func__);
134 if (ret)
135 return ret;
136
137 ret = adc_supply_enable(dev);
138 if (ret)
139 return ret;
140
141 return ops->start_channels(dev, channel_mask);
142}
143
144int adc_channel_data(struct udevice *dev, int channel, unsigned int *data)
145{
Simon Glasscaa4daa2020-12-03 16:55:18 -0700146 struct adc_uclass_plat *uc_pdata = dev_get_uclass_plat(dev);
Przemyslaw Marczak5decbf52015-10-27 13:08:00 +0100147 const struct adc_ops *ops = dev_get_driver_ops(dev);
148 unsigned int timeout_us = uc_pdata->data_timeout_us;
149 int ret;
150
151 if (!ops->channel_data)
152 return -ENOSYS;
153
154 ret = check_channel(dev, channel, CHECK_NUMBER, __func__);
155 if (ret)
156 return ret;
157
158 do {
159 ret = ops->channel_data(dev, channel, data);
160 if (!ret || ret != -EBUSY)
161 break;
162
163 /* TODO: use timer uclass (for early calls). */
164 sdelay(5);
165 } while (timeout_us--);
166
167 return ret;
168}
169
170int adc_channels_data(struct udevice *dev, unsigned int channel_mask,
171 struct adc_channel *channels)
172{
Simon Glasscaa4daa2020-12-03 16:55:18 -0700173 struct adc_uclass_plat *uc_pdata = dev_get_uclass_plat(dev);
Przemyslaw Marczak5decbf52015-10-27 13:08:00 +0100174 unsigned int timeout_us = uc_pdata->multidata_timeout_us;
175 const struct adc_ops *ops = dev_get_driver_ops(dev);
176 int ret;
177
178 if (!ops->channels_data)
179 return -ENOSYS;
180
181 ret = check_channel(dev, channel_mask, CHECK_MASK, __func__);
182 if (ret)
183 return ret;
184
185 do {
186 ret = ops->channels_data(dev, channel_mask, channels);
187 if (!ret || ret != -EBUSY)
188 break;
189
190 /* TODO: use timer uclass (for early calls). */
191 sdelay(5);
192 } while (timeout_us--);
193
194 return ret;
195}
196
197int adc_channel_single_shot(const char *name, int channel, unsigned int *data)
198{
199 struct udevice *dev;
200 int ret;
201
202 ret = uclass_get_device_by_name(UCLASS_ADC, name, &dev);
203 if (ret)
204 return ret;
205
206 ret = adc_start_channel(dev, channel);
207 if (ret)
208 return ret;
209
210 ret = adc_channel_data(dev, channel, data);
211 if (ret)
212 return ret;
213
214 return 0;
215}
216
217static int _adc_channels_single_shot(struct udevice *dev,
218 unsigned int channel_mask,
219 struct adc_channel *channels)
220{
221 unsigned int data;
222 int channel, ret;
223
224 for (channel = 0; channel <= ADC_MAX_CHANNEL; channel++) {
225 /* Check channel bit. */
226 if (!((channel_mask >> channel) & 0x1))
227 continue;
228
229 ret = adc_start_channel(dev, channel);
230 if (ret)
231 return ret;
232
233 ret = adc_channel_data(dev, channel, &data);
234 if (ret)
235 return ret;
236
237 channels->id = channel;
238 channels->data = data;
239 channels++;
240 }
241
242 return 0;
243}
244
245int adc_channels_single_shot(const char *name, unsigned int channel_mask,
246 struct adc_channel *channels)
247{
248 struct udevice *dev;
249 int ret;
250
251 ret = uclass_get_device_by_name(UCLASS_ADC, name, &dev);
252 if (ret)
253 return ret;
254
255 ret = adc_start_channels(dev, channel_mask);
256 if (ret)
257 goto try_manual;
258
259 ret = adc_channels_data(dev, channel_mask, channels);
260 if (ret)
261 return ret;
262
263 return 0;
264
265try_manual:
266 if (ret != -ENOSYS)
267 return ret;
268
269 return _adc_channels_single_shot(dev, channel_mask, channels);
270}
271
Simon Glass8a8d24b2020-12-03 16:55:23 -0700272static int adc_vdd_plat_update(struct udevice *dev)
Przemyslaw Marczak5decbf52015-10-27 13:08:00 +0100273{
Simon Glasscaa4daa2020-12-03 16:55:18 -0700274 struct adc_uclass_plat *uc_pdata = dev_get_uclass_plat(dev);
Przemyslaw Marczak5decbf52015-10-27 13:08:00 +0100275 int ret;
276
277 /* Warning!
278 * This function can't return supply device before its bind.
279 * Please pay attention to proper fdt scan sequence. If ADC device
280 * will bind before its supply regulator device, then the below 'get'
281 * will return an error.
282 */
Fabrice Gasniera4a87f72018-07-24 16:31:29 +0200283 if (!uc_pdata->vdd_supply)
284 return 0;
Przemyslaw Marczak5decbf52015-10-27 13:08:00 +0100285
286 ret = regulator_get_value(uc_pdata->vdd_supply);
287 if (ret < 0)
288 return ret;
289
290 uc_pdata->vdd_microvolts = ret;
291
292 return 0;
293}
294
Simon Glass8a8d24b2020-12-03 16:55:23 -0700295static int adc_vss_plat_update(struct udevice *dev)
Przemyslaw Marczak5decbf52015-10-27 13:08:00 +0100296{
Simon Glasscaa4daa2020-12-03 16:55:18 -0700297 struct adc_uclass_plat *uc_pdata = dev_get_uclass_plat(dev);
Przemyslaw Marczak5decbf52015-10-27 13:08:00 +0100298 int ret;
299
Fabrice Gasniera4a87f72018-07-24 16:31:29 +0200300 if (!uc_pdata->vss_supply)
301 return 0;
Przemyslaw Marczak5decbf52015-10-27 13:08:00 +0100302
303 ret = regulator_get_value(uc_pdata->vss_supply);
304 if (ret < 0)
305 return ret;
306
307 uc_pdata->vss_microvolts = ret;
308
309 return 0;
310}
311
312int adc_vdd_value(struct udevice *dev, int *uV)
313{
Simon Glasscaa4daa2020-12-03 16:55:18 -0700314 struct adc_uclass_plat *uc_pdata = dev_get_uclass_plat(dev);
Przemyslaw Marczak5decbf52015-10-27 13:08:00 +0100315 int ret, value_sign = uc_pdata->vdd_polarity_negative ? -1 : 1;
316
Przemyslaw Marczak5decbf52015-10-27 13:08:00 +0100317 /* Update the regulator Value. */
Simon Glass8a8d24b2020-12-03 16:55:23 -0700318 ret = adc_vdd_plat_update(dev);
Przemyslaw Marczak5decbf52015-10-27 13:08:00 +0100319 if (ret)
320 return ret;
Fabrice Gasniera4a87f72018-07-24 16:31:29 +0200321
Przemyslaw Marczak5decbf52015-10-27 13:08:00 +0100322 if (uc_pdata->vdd_microvolts == -ENODATA)
323 return -ENODATA;
324
325 *uV = uc_pdata->vdd_microvolts * value_sign;
326
327 return 0;
328}
329
330int adc_vss_value(struct udevice *dev, int *uV)
331{
Simon Glasscaa4daa2020-12-03 16:55:18 -0700332 struct adc_uclass_plat *uc_pdata = dev_get_uclass_plat(dev);
Przemyslaw Marczak5decbf52015-10-27 13:08:00 +0100333 int ret, value_sign = uc_pdata->vss_polarity_negative ? -1 : 1;
334
Przemyslaw Marczak5decbf52015-10-27 13:08:00 +0100335 /* Update the regulator Value. */
Simon Glass8a8d24b2020-12-03 16:55:23 -0700336 ret = adc_vss_plat_update(dev);
Przemyslaw Marczak5decbf52015-10-27 13:08:00 +0100337 if (ret)
338 return ret;
Fabrice Gasniera4a87f72018-07-24 16:31:29 +0200339
Przemyslaw Marczak5decbf52015-10-27 13:08:00 +0100340 if (uc_pdata->vss_microvolts == -ENODATA)
341 return -ENODATA;
342
343 *uV = uc_pdata->vss_microvolts * value_sign;
344
345 return 0;
346}
347
Fabrice Gasnier63f004e2018-11-12 14:03:58 +0100348int adc_raw_to_uV(struct udevice *dev, unsigned int raw, int *uV)
349{
350 unsigned int data_mask;
351 int ret, val, vref;
352 u64 raw64 = raw;
353
354 ret = adc_vdd_value(dev, &vref);
355 if (ret)
356 return ret;
357
358 if (!adc_vss_value(dev, &val))
359 vref -= val;
360
361 ret = adc_data_mask(dev, &data_mask);
362 if (ret)
363 return ret;
364
365 raw64 *= vref;
366 do_div(raw64, data_mask);
367 *uV = raw64;
368
369 return 0;
370}
371
Simon Glass8a8d24b2020-12-03 16:55:23 -0700372static int adc_vdd_plat_set(struct udevice *dev)
Przemyslaw Marczak5decbf52015-10-27 13:08:00 +0100373{
Simon Glasscaa4daa2020-12-03 16:55:18 -0700374 struct adc_uclass_plat *uc_pdata = dev_get_uclass_plat(dev);
Simon Glass53d788d2017-05-18 20:09:37 -0600375 int ret;
Przemyslaw Marczak5decbf52015-10-27 13:08:00 +0100376 char *prop;
377
378 prop = "vdd-polarity-negative";
Simon Glass53d788d2017-05-18 20:09:37 -0600379 uc_pdata->vdd_polarity_negative = dev_read_bool(dev, prop);
Przemyslaw Marczak5decbf52015-10-27 13:08:00 +0100380
Fabrice Gasniera4a87f72018-07-24 16:31:29 +0200381 /* Optionally get regulators */
382 ret = device_get_supply_regulator(dev, "vdd-supply",
383 &uc_pdata->vdd_supply);
384 if (!ret)
Simon Glass8a8d24b2020-12-03 16:55:23 -0700385 return adc_vdd_plat_update(dev);
Fabrice Gasniera4a87f72018-07-24 16:31:29 +0200386
Przemyslaw Marczak5decbf52015-10-27 13:08:00 +0100387 if (ret != -ENOENT)
388 return ret;
389
390 /* No vdd-supply phandle. */
391 prop = "vdd-microvolts";
Simon Glass53d788d2017-05-18 20:09:37 -0600392 uc_pdata->vdd_microvolts = dev_read_u32_default(dev, prop, -ENODATA);
Przemyslaw Marczak5decbf52015-10-27 13:08:00 +0100393
394 return 0;
395}
396
Simon Glass8a8d24b2020-12-03 16:55:23 -0700397static int adc_vss_plat_set(struct udevice *dev)
Przemyslaw Marczak5decbf52015-10-27 13:08:00 +0100398{
Simon Glasscaa4daa2020-12-03 16:55:18 -0700399 struct adc_uclass_plat *uc_pdata = dev_get_uclass_plat(dev);
Simon Glass53d788d2017-05-18 20:09:37 -0600400 int ret;
Przemyslaw Marczak5decbf52015-10-27 13:08:00 +0100401 char *prop;
402
403 prop = "vss-polarity-negative";
Simon Glass53d788d2017-05-18 20:09:37 -0600404 uc_pdata->vss_polarity_negative = dev_read_bool(dev, prop);
Przemyslaw Marczak5decbf52015-10-27 13:08:00 +0100405
Fabrice Gasniera4a87f72018-07-24 16:31:29 +0200406 ret = device_get_supply_regulator(dev, "vss-supply",
407 &uc_pdata->vss_supply);
408 if (!ret)
Simon Glass8a8d24b2020-12-03 16:55:23 -0700409 return adc_vss_plat_update(dev);
Fabrice Gasniera4a87f72018-07-24 16:31:29 +0200410
Przemyslaw Marczak5decbf52015-10-27 13:08:00 +0100411 if (ret != -ENOENT)
412 return ret;
413
414 /* No vss-supply phandle. */
415 prop = "vss-microvolts";
Simon Glass53d788d2017-05-18 20:09:37 -0600416 uc_pdata->vss_microvolts = dev_read_u32_default(dev, prop, -ENODATA);
Przemyslaw Marczak5decbf52015-10-27 13:08:00 +0100417
418 return 0;
419}
420
421static int adc_pre_probe(struct udevice *dev)
422{
423 int ret;
424
Simon Glasscaa4daa2020-12-03 16:55:18 -0700425 /* Set ADC VDD plat: polarity, uV, regulator (phandle). */
Simon Glass8a8d24b2020-12-03 16:55:23 -0700426 ret = adc_vdd_plat_set(dev);
Przemyslaw Marczak5decbf52015-10-27 13:08:00 +0100427 if (ret)
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900428 pr_err("%s: Can't update Vdd. Error: %d", dev->name, ret);
Przemyslaw Marczak5decbf52015-10-27 13:08:00 +0100429
Simon Glasscaa4daa2020-12-03 16:55:18 -0700430 /* Set ADC VSS plat: polarity, uV, regulator (phandle). */
Simon Glass8a8d24b2020-12-03 16:55:23 -0700431 ret = adc_vss_plat_set(dev);
Przemyslaw Marczak5decbf52015-10-27 13:08:00 +0100432 if (ret)
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900433 pr_err("%s: Can't update Vss. Error: %d", dev->name, ret);
Przemyslaw Marczak5decbf52015-10-27 13:08:00 +0100434
435 return 0;
436}
437
438UCLASS_DRIVER(adc) = {
439 .id = UCLASS_ADC,
440 .name = "adc",
441 .pre_probe = adc_pre_probe,
Simon Glasscaa4daa2020-12-03 16:55:18 -0700442 .per_device_plat_auto = ADC_UCLASS_PLATDATA_SIZE,
Przemyslaw Marczak5decbf52015-10-27 13:08:00 +0100443};