1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2017, Fuzhou Rockchip Electronics Co., Ltd
5 * Rockchip SARADC driver for U-Boot
14 #include <linux/bitops.h>
15 #include <linux/err.h>
17 #define SARADC_CTRL_CHN_MASK GENMASK(2, 0)
18 #define SARADC_CTRL_POWER_CTRL BIT(3)
19 #define SARADC_CTRL_IRQ_ENABLE BIT(5)
20 #define SARADC_CTRL_IRQ_STATUS BIT(6)
22 #define SARADC_TIMEOUT (100 * 1000)
24 struct rockchip_saradc_regs {
28 unsigned int dly_pu_soc;
31 struct rockchip_saradc_data {
34 unsigned long clk_rate;
37 struct rockchip_saradc_priv {
38 struct rockchip_saradc_regs *regs;
40 const struct rockchip_saradc_data *data;
43 int rockchip_saradc_channel_data(struct udevice *dev, int channel,
46 struct rockchip_saradc_priv *priv = dev_get_priv(dev);
47 struct adc_uclass_plat *uc_pdata = dev_get_uclass_plat(dev);
49 if (channel != priv->active_channel) {
50 pr_err("Requested channel is not active!");
54 if ((readl(&priv->regs->ctrl) & SARADC_CTRL_IRQ_STATUS) !=
55 SARADC_CTRL_IRQ_STATUS)
59 *data = readl(&priv->regs->data);
60 *data &= uc_pdata->data_mask;
63 writel(0, &priv->regs->ctrl);
68 int rockchip_saradc_start_channel(struct udevice *dev, int channel)
70 struct rockchip_saradc_priv *priv = dev_get_priv(dev);
72 if (channel < 0 || channel >= priv->data->num_channels) {
73 pr_err("Requested channel is invalid!");
77 /* 8 clock periods as delay between power up and start cmd */
78 writel(8, &priv->regs->dly_pu_soc);
80 /* Select the channel to be used and trigger conversion */
81 writel(SARADC_CTRL_POWER_CTRL | (channel & SARADC_CTRL_CHN_MASK) |
82 SARADC_CTRL_IRQ_ENABLE, &priv->regs->ctrl);
84 priv->active_channel = channel;
89 int rockchip_saradc_stop(struct udevice *dev)
91 struct rockchip_saradc_priv *priv = dev_get_priv(dev);
94 writel(0, &priv->regs->ctrl);
96 priv->active_channel = -1;
101 int rockchip_saradc_probe(struct udevice *dev)
103 struct rockchip_saradc_priv *priv = dev_get_priv(dev);
107 ret = clk_get_by_index(dev, 0, &clk);
111 ret = clk_set_rate(&clk, priv->data->clk_rate);
112 if (IS_ERR_VALUE(ret))
115 priv->active_channel = -1;
120 int rockchip_saradc_of_to_plat(struct udevice *dev)
122 struct adc_uclass_plat *uc_pdata = dev_get_uclass_plat(dev);
123 struct rockchip_saradc_priv *priv = dev_get_priv(dev);
124 struct rockchip_saradc_data *data;
126 data = (struct rockchip_saradc_data *)dev_get_driver_data(dev);
127 priv->regs = (struct rockchip_saradc_regs *)dev_read_addr(dev);
128 if (priv->regs == (struct rockchip_saradc_regs *)FDT_ADDR_T_NONE) {
129 pr_err("Dev: %s - can't get address!", dev->name);
134 uc_pdata->data_mask = (1 << priv->data->num_bits) - 1;;
135 uc_pdata->data_format = ADC_DATA_FORMAT_BIN;
136 uc_pdata->data_timeout_us = SARADC_TIMEOUT / 5;
137 uc_pdata->channel_mask = (1 << priv->data->num_channels) - 1;
142 static const struct adc_ops rockchip_saradc_ops = {
143 .start_channel = rockchip_saradc_start_channel,
144 .channel_data = rockchip_saradc_channel_data,
145 .stop = rockchip_saradc_stop,
148 static const struct rockchip_saradc_data saradc_data = {
154 static const struct rockchip_saradc_data rk3066_tsadc_data = {
160 static const struct rockchip_saradc_data rk3399_saradc_data = {
166 static const struct udevice_id rockchip_saradc_ids[] = {
167 { .compatible = "rockchip,saradc",
168 .data = (ulong)&saradc_data },
169 { .compatible = "rockchip,rk3066-tsadc",
170 .data = (ulong)&rk3066_tsadc_data },
171 { .compatible = "rockchip,rk3399-saradc",
172 .data = (ulong)&rk3399_saradc_data },
176 U_BOOT_DRIVER(rockchip_saradc) = {
177 .name = "rockchip_saradc",
179 .of_match = rockchip_saradc_ids,
180 .ops = &rockchip_saradc_ops,
181 .probe = rockchip_saradc_probe,
182 .of_to_plat = rockchip_saradc_of_to_plat,
183 .priv_auto = sizeof(struct rockchip_saradc_priv),