blob: 760f8fe628047d4eac6ff330510f51c6ec981eb1 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
David Wuae3ed042017-09-20 14:28:16 +08002/*
3 * (C) Copyright 2017, Fuzhou Rockchip Electronics Co., Ltd
4 *
David Wuae3ed042017-09-20 14:28:16 +08005 * Rockchip SARADC driver for U-Boot
6 */
7
8#include <common.h>
9#include <adc.h>
10#include <clk.h>
11#include <dm.h>
12#include <errno.h>
13#include <asm/io.h>
Simon Glasscd93d622020-05-10 11:40:13 -060014#include <linux/bitops.h>
Simon Glass61b29b82020-02-03 07:36:15 -070015#include <linux/err.h>
Peter Caie9632282022-02-04 15:16:06 -050016#include <power/regulator.h>
David Wuae3ed042017-09-20 14:28:16 +080017
18#define SARADC_CTRL_CHN_MASK GENMASK(2, 0)
19#define SARADC_CTRL_POWER_CTRL BIT(3)
20#define SARADC_CTRL_IRQ_ENABLE BIT(5)
21#define SARADC_CTRL_IRQ_STATUS BIT(6)
22
23#define SARADC_TIMEOUT (100 * 1000)
24
25struct rockchip_saradc_regs {
26 unsigned int data;
27 unsigned int stas;
28 unsigned int ctrl;
29 unsigned int dly_pu_soc;
30};
31
32struct rockchip_saradc_data {
33 int num_bits;
34 int num_channels;
35 unsigned long clk_rate;
36};
37
38struct rockchip_saradc_priv {
39 struct rockchip_saradc_regs *regs;
40 int active_channel;
41 const struct rockchip_saradc_data *data;
42};
43
44int rockchip_saradc_channel_data(struct udevice *dev, int channel,
45 unsigned int *data)
46{
47 struct rockchip_saradc_priv *priv = dev_get_priv(dev);
Simon Glasscaa4daa2020-12-03 16:55:18 -070048 struct adc_uclass_plat *uc_pdata = dev_get_uclass_plat(dev);
David Wuae3ed042017-09-20 14:28:16 +080049
50 if (channel != priv->active_channel) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +090051 pr_err("Requested channel is not active!");
David Wuae3ed042017-09-20 14:28:16 +080052 return -EINVAL;
53 }
54
55 if ((readl(&priv->regs->ctrl) & SARADC_CTRL_IRQ_STATUS) !=
56 SARADC_CTRL_IRQ_STATUS)
57 return -EBUSY;
58
59 /* Read value */
60 *data = readl(&priv->regs->data);
61 *data &= uc_pdata->data_mask;
62
63 /* Power down adc */
64 writel(0, &priv->regs->ctrl);
65
66 return 0;
67}
68
69int rockchip_saradc_start_channel(struct udevice *dev, int channel)
70{
71 struct rockchip_saradc_priv *priv = dev_get_priv(dev);
72
73 if (channel < 0 || channel >= priv->data->num_channels) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +090074 pr_err("Requested channel is invalid!");
David Wuae3ed042017-09-20 14:28:16 +080075 return -EINVAL;
76 }
77
78 /* 8 clock periods as delay between power up and start cmd */
79 writel(8, &priv->regs->dly_pu_soc);
80
81 /* Select the channel to be used and trigger conversion */
82 writel(SARADC_CTRL_POWER_CTRL | (channel & SARADC_CTRL_CHN_MASK) |
83 SARADC_CTRL_IRQ_ENABLE, &priv->regs->ctrl);
84
85 priv->active_channel = channel;
86
87 return 0;
88}
89
90int rockchip_saradc_stop(struct udevice *dev)
91{
92 struct rockchip_saradc_priv *priv = dev_get_priv(dev);
93
94 /* Power down adc */
95 writel(0, &priv->regs->ctrl);
96
97 priv->active_channel = -1;
98
99 return 0;
100}
101
102int rockchip_saradc_probe(struct udevice *dev)
103{
Peter Caie9632282022-02-04 15:16:06 -0500104 struct adc_uclass_plat *uc_pdata = dev_get_uclass_plat(dev);
David Wuae3ed042017-09-20 14:28:16 +0800105 struct rockchip_saradc_priv *priv = dev_get_priv(dev);
Peter Caie9632282022-02-04 15:16:06 -0500106 struct udevice *vref;
David Wuae3ed042017-09-20 14:28:16 +0800107 struct clk clk;
Peter Caie9632282022-02-04 15:16:06 -0500108 int vref_uv;
David Wuae3ed042017-09-20 14:28:16 +0800109 int ret;
110
111 ret = clk_get_by_index(dev, 0, &clk);
112 if (ret)
113 return ret;
114
115 ret = clk_set_rate(&clk, priv->data->clk_rate);
116 if (IS_ERR_VALUE(ret))
117 return ret;
118
119 priv->active_channel = -1;
120
Peter Caie9632282022-02-04 15:16:06 -0500121 ret = device_get_supply_regulator(dev, "vref-supply", &vref);
122 if (ret) {
123 printf("can't get vref-supply: %d\n", ret);
124 return ret;
125 }
126
127 vref_uv = regulator_get_value(vref);
128 if (vref_uv < 0) {
129 printf("can't get vref-supply value: %d\n", vref_uv);
130 return vref_uv;
131 }
132
133 /* VDD supplied by common vref pin */
134 uc_pdata->vdd_supply = vref;
135 uc_pdata->vdd_microvolts = vref_uv;
136 uc_pdata->vss_microvolts = 0;
137
David Wuae3ed042017-09-20 14:28:16 +0800138 return 0;
139}
140
Simon Glassd1998a92020-12-03 16:55:21 -0700141int rockchip_saradc_of_to_plat(struct udevice *dev)
David Wuae3ed042017-09-20 14:28:16 +0800142{
Simon Glasscaa4daa2020-12-03 16:55:18 -0700143 struct adc_uclass_plat *uc_pdata = dev_get_uclass_plat(dev);
David Wuae3ed042017-09-20 14:28:16 +0800144 struct rockchip_saradc_priv *priv = dev_get_priv(dev);
145 struct rockchip_saradc_data *data;
146
147 data = (struct rockchip_saradc_data *)dev_get_driver_data(dev);
148 priv->regs = (struct rockchip_saradc_regs *)dev_read_addr(dev);
149 if (priv->regs == (struct rockchip_saradc_regs *)FDT_ADDR_T_NONE) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900150 pr_err("Dev: %s - can't get address!", dev->name);
David Wuae3ed042017-09-20 14:28:16 +0800151 return -ENODATA;
152 }
153
154 priv->data = data;
Giulio Benetti9acae542022-03-14 10:09:43 +0100155 uc_pdata->data_mask = (1 << priv->data->num_bits) - 1;
David Wuae3ed042017-09-20 14:28:16 +0800156 uc_pdata->data_format = ADC_DATA_FORMAT_BIN;
157 uc_pdata->data_timeout_us = SARADC_TIMEOUT / 5;
158 uc_pdata->channel_mask = (1 << priv->data->num_channels) - 1;
159
160 return 0;
161}
162
163static const struct adc_ops rockchip_saradc_ops = {
164 .start_channel = rockchip_saradc_start_channel,
165 .channel_data = rockchip_saradc_channel_data,
166 .stop = rockchip_saradc_stop,
167};
168
169static const struct rockchip_saradc_data saradc_data = {
170 .num_bits = 10,
171 .num_channels = 3,
172 .clk_rate = 1000000,
173};
174
175static const struct rockchip_saradc_data rk3066_tsadc_data = {
176 .num_bits = 12,
177 .num_channels = 2,
178 .clk_rate = 50000,
179};
180
181static const struct rockchip_saradc_data rk3399_saradc_data = {
182 .num_bits = 10,
183 .num_channels = 6,
184 .clk_rate = 1000000,
185};
186
187static const struct udevice_id rockchip_saradc_ids[] = {
188 { .compatible = "rockchip,saradc",
189 .data = (ulong)&saradc_data },
190 { .compatible = "rockchip,rk3066-tsadc",
191 .data = (ulong)&rk3066_tsadc_data },
192 { .compatible = "rockchip,rk3399-saradc",
193 .data = (ulong)&rk3399_saradc_data },
194 { }
195};
196
197U_BOOT_DRIVER(rockchip_saradc) = {
198 .name = "rockchip_saradc",
199 .id = UCLASS_ADC,
200 .of_match = rockchip_saradc_ids,
201 .ops = &rockchip_saradc_ops,
202 .probe = rockchip_saradc_probe,
Simon Glassd1998a92020-12-03 16:55:21 -0700203 .of_to_plat = rockchip_saradc_of_to_plat,
Simon Glass41575d82020-12-03 16:55:17 -0700204 .priv_auto = sizeof(struct rockchip_saradc_priv),
David Wuae3ed042017-09-20 14:28:16 +0800205};