1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2017, Fuzhou Rockchip Electronics Co., Ltd
5 * Rockchip SARADC driver for U-Boot
13 #include <asm/arch-rockchip/hardware.h>
14 #include <linux/bitfield.h>
15 #include <linux/bitops.h>
16 #include <linux/delay.h>
17 #include <linux/err.h>
18 #include <linux/printk.h>
19 #include <power/regulator.h>
21 #define usleep_range(a, b) udelay((b))
23 #define SARADC_CTRL_CHN_MASK GENMASK(2, 0)
24 #define SARADC_CTRL_POWER_CTRL BIT(3)
25 #define SARADC_CTRL_IRQ_ENABLE BIT(5)
26 #define SARADC_CTRL_IRQ_STATUS BIT(6)
28 #define SARADC_TIMEOUT (100 * 1000)
30 struct rockchip_saradc_regs_v1 {
34 unsigned int dly_pu_soc;
37 struct rockchip_saradc_regs_v2 {
38 unsigned int conv_con;
39 #define SARADC2_SINGLE_MODE BIT(5)
40 #define SARADC2_START BIT(4)
41 #define SARADC2_CONV_CHANNELS GENMASK(3, 0)
42 unsigned int t_pd_soc;
43 unsigned int t_as_soc;
44 unsigned int t_das_soc;
45 unsigned int t_sel_soc;
46 unsigned int high_comp[16];
47 unsigned int low_comp[16];
48 unsigned int debounce;
49 unsigned int ht_int_en;
50 unsigned int lt_int_en;
51 unsigned int reserved[24];
52 unsigned int mt_int_en;
53 unsigned int end_int_en;
54 #define SARADC2_EN_END_INT BIT(0)
57 unsigned int end_int_st;
58 unsigned int ht_int_st;
59 unsigned int lt_int_st;
60 unsigned int mt_int_st;
61 unsigned int data[16];
62 unsigned int auto_ch_en;
65 union rockchip_saradc_regs {
66 struct rockchip_saradc_regs_v1 *v1;
67 struct rockchip_saradc_regs_v2 *v2;
69 struct rockchip_saradc_data {
72 unsigned long clk_rate;
73 int (*channel_data)(struct udevice *dev, int channel, unsigned int *data);
74 int (*start_channel)(struct udevice *dev, int channel);
75 int (*stop)(struct udevice *dev);
78 struct rockchip_saradc_priv {
79 union rockchip_saradc_regs regs;
81 const struct rockchip_saradc_data *data;
82 struct reset_ctl *reset;
85 int rockchip_saradc_channel_data_v1(struct udevice *dev, int channel,
88 struct rockchip_saradc_priv *priv = dev_get_priv(dev);
90 if ((readl(&priv->regs.v1->ctrl) & SARADC_CTRL_IRQ_STATUS) !=
91 SARADC_CTRL_IRQ_STATUS)
95 *data = readl(&priv->regs.v1->data);
98 writel(0, &priv->regs.v1->ctrl);
103 int rockchip_saradc_channel_data_v2(struct udevice *dev, int channel,
106 struct rockchip_saradc_priv *priv = dev_get_priv(dev);
108 if (!(readl(&priv->regs.v2->end_int_st) & SARADC2_EN_END_INT))
112 *data = readl(&priv->regs.v2->data[channel]);
114 /* Acknowledge the interrupt */
115 writel(SARADC2_EN_END_INT, &priv->regs.v2->end_int_st);
119 int rockchip_saradc_channel_data(struct udevice *dev, int channel,
122 struct rockchip_saradc_priv *priv = dev_get_priv(dev);
123 struct adc_uclass_plat *uc_pdata = dev_get_uclass_plat(dev);
126 if (channel != priv->active_channel) {
127 pr_err("Requested channel is not active!");
131 ret = priv->data->channel_data(dev, channel, data);
134 pr_err("Error reading channel data, %d!", ret);
138 *data &= uc_pdata->data_mask;
143 int rockchip_saradc_start_channel_v1(struct udevice *dev, int channel)
145 struct rockchip_saradc_priv *priv = dev_get_priv(dev);
147 /* 8 clock periods as delay between power up and start cmd */
148 writel(8, &priv->regs.v1->dly_pu_soc);
150 /* Select the channel to be used and trigger conversion */
151 writel(SARADC_CTRL_POWER_CTRL | (channel & SARADC_CTRL_CHN_MASK) |
152 SARADC_CTRL_IRQ_ENABLE, &priv->regs.v1->ctrl);
157 static void rockchip_saradc_reset_controller(struct reset_ctl *reset)
160 usleep_range(10, 20);
161 reset_deassert(reset);
164 int rockchip_saradc_start_channel_v2(struct udevice *dev, int channel)
166 struct rockchip_saradc_priv *priv = dev_get_priv(dev);
170 * """If read other chn at anytime, then chn1 will error, assert
171 * controller as a workaround."""
174 rockchip_saradc_reset_controller(priv->reset);
176 writel(0xc, &priv->regs.v2->t_das_soc);
177 writel(0x20, &priv->regs.v2->t_pd_soc);
179 /* Acknowledge any previous interrupt */
180 writel(SARADC2_EN_END_INT, &priv->regs.v2->end_int_st);
182 rk_clrsetreg(&priv->regs.v2->conv_con,
183 SARADC2_CONV_CHANNELS | SARADC2_START | SARADC2_SINGLE_MODE,
184 FIELD_PREP(SARADC2_CONV_CHANNELS, channel) |
185 FIELD_PREP(SARADC2_START, 1) |
186 FIELD_PREP(SARADC2_SINGLE_MODE, 1));
191 int rockchip_saradc_start_channel(struct udevice *dev, int channel)
193 struct rockchip_saradc_priv *priv = dev_get_priv(dev);
196 if (channel < 0 || channel >= priv->data->num_channels) {
197 pr_err("Requested channel is invalid!");
201 ret = priv->data->start_channel(dev, channel);
203 pr_err("Error starting channel, %d!", ret);
207 priv->active_channel = channel;
212 int rockchip_saradc_stop_v1(struct udevice *dev)
214 struct rockchip_saradc_priv *priv = dev_get_priv(dev);
217 writel(0, &priv->regs.v1->ctrl);
222 int rockchip_saradc_stop(struct udevice *dev)
224 struct rockchip_saradc_priv *priv = dev_get_priv(dev);
226 if (priv->data->stop) {
227 int ret = priv->data->stop(dev);
230 pr_err("Error stopping channel, %d!", ret);
235 priv->active_channel = -1;
240 int rockchip_saradc_probe(struct udevice *dev)
242 struct adc_uclass_plat *uc_pdata = dev_get_uclass_plat(dev);
243 struct rockchip_saradc_priv *priv = dev_get_priv(dev);
244 struct udevice *vref;
249 priv->reset = devm_reset_control_get_optional(dev, "saradc-apb");
251 ret = clk_get_by_index(dev, 0, &clk);
255 ret = clk_set_rate(&clk, priv->data->clk_rate);
256 if (IS_ERR_VALUE(ret))
259 priv->active_channel = -1;
261 ret = device_get_supply_regulator(dev, "vref-supply", &vref);
263 printf("can't get vref-supply: %d\n", ret);
268 rockchip_saradc_reset_controller(priv->reset);
270 vref_uv = regulator_get_value(vref);
272 printf("can't get vref-supply value: %d\n", vref_uv);
276 /* VDD supplied by common vref pin */
277 uc_pdata->vdd_supply = vref;
278 uc_pdata->vdd_microvolts = vref_uv;
279 uc_pdata->vss_microvolts = 0;
284 int rockchip_saradc_of_to_plat(struct udevice *dev)
286 struct adc_uclass_plat *uc_pdata = dev_get_uclass_plat(dev);
287 struct rockchip_saradc_priv *priv = dev_get_priv(dev);
288 struct rockchip_saradc_data *data;
290 data = (struct rockchip_saradc_data *)dev_get_driver_data(dev);
291 priv->regs.v1 = dev_read_addr_ptr(dev);
292 if (!priv->regs.v1) {
293 pr_err("Dev: %s - can't get address!", dev->name);
298 uc_pdata->data_mask = (1 << priv->data->num_bits) - 1;
299 uc_pdata->data_format = ADC_DATA_FORMAT_BIN;
300 uc_pdata->data_timeout_us = SARADC_TIMEOUT / 5;
301 uc_pdata->channel_mask = (1 << priv->data->num_channels) - 1;
306 static const struct adc_ops rockchip_saradc_ops = {
307 .start_channel = rockchip_saradc_start_channel,
308 .channel_data = rockchip_saradc_channel_data,
309 .stop = rockchip_saradc_stop,
312 static const struct rockchip_saradc_data saradc_data = {
316 .channel_data = rockchip_saradc_channel_data_v1,
317 .start_channel = rockchip_saradc_start_channel_v1,
318 .stop = rockchip_saradc_stop_v1,
321 static const struct rockchip_saradc_data rk3066_tsadc_data = {
325 .channel_data = rockchip_saradc_channel_data_v1,
326 .start_channel = rockchip_saradc_start_channel_v1,
327 .stop = rockchip_saradc_stop_v1,
330 static const struct rockchip_saradc_data rk3399_saradc_data = {
334 .channel_data = rockchip_saradc_channel_data_v1,
335 .start_channel = rockchip_saradc_start_channel_v1,
336 .stop = rockchip_saradc_stop_v1,
339 static const struct rockchip_saradc_data rk3588_saradc_data = {
343 .channel_data = rockchip_saradc_channel_data_v2,
344 .start_channel = rockchip_saradc_start_channel_v2,
347 static const struct udevice_id rockchip_saradc_ids[] = {
348 { .compatible = "rockchip,saradc",
349 .data = (ulong)&saradc_data },
350 { .compatible = "rockchip,rk3066-tsadc",
351 .data = (ulong)&rk3066_tsadc_data },
352 { .compatible = "rockchip,rk3399-saradc",
353 .data = (ulong)&rk3399_saradc_data },
354 { .compatible = "rockchip,rk3588-saradc",
355 .data = (ulong)&rk3588_saradc_data },
359 U_BOOT_DRIVER(rockchip_saradc) = {
360 .name = "rockchip_saradc",
362 .of_match = rockchip_saradc_ids,
363 .ops = &rockchip_saradc_ops,
364 .probe = rockchip_saradc_probe,
365 .of_to_plat = rockchip_saradc_of_to_plat,
366 .priv_auto = sizeof(struct rockchip_saradc_priv),