Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
David Wu | ae3ed04 | 2017-09-20 14:28:16 +0800 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2017, Fuzhou Rockchip Electronics Co., Ltd |
| 4 | * |
David Wu | ae3ed04 | 2017-09-20 14:28:16 +0800 | [diff] [blame] | 5 | * 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 Glass | cd93d62 | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 14 | #include <linux/bitops.h> |
Simon Glass | 61b29b8 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 15 | #include <linux/err.h> |
Simon Glass | 1e94b46 | 2023-09-14 18:21:46 -0600 | [diff] [blame] | 16 | #include <linux/printk.h> |
Peter Cai | e963228 | 2022-02-04 15:16:06 -0500 | [diff] [blame] | 17 | #include <power/regulator.h> |
David Wu | ae3ed04 | 2017-09-20 14:28:16 +0800 | [diff] [blame] | 18 | |
| 19 | #define SARADC_CTRL_CHN_MASK GENMASK(2, 0) |
| 20 | #define SARADC_CTRL_POWER_CTRL BIT(3) |
| 21 | #define SARADC_CTRL_IRQ_ENABLE BIT(5) |
| 22 | #define SARADC_CTRL_IRQ_STATUS BIT(6) |
| 23 | |
| 24 | #define SARADC_TIMEOUT (100 * 1000) |
| 25 | |
Quentin Schulz | 0707bfd | 2024-03-14 10:36:20 +0100 | [diff] [blame] | 26 | struct rockchip_saradc_regs_v1 { |
David Wu | ae3ed04 | 2017-09-20 14:28:16 +0800 | [diff] [blame] | 27 | unsigned int data; |
| 28 | unsigned int stas; |
| 29 | unsigned int ctrl; |
| 30 | unsigned int dly_pu_soc; |
| 31 | }; |
| 32 | |
Quentin Schulz | 0707bfd | 2024-03-14 10:36:20 +0100 | [diff] [blame] | 33 | union rockchip_saradc_regs { |
| 34 | struct rockchip_saradc_regs_v1 *v1; |
| 35 | }; |
David Wu | ae3ed04 | 2017-09-20 14:28:16 +0800 | [diff] [blame] | 36 | struct rockchip_saradc_data { |
| 37 | int num_bits; |
| 38 | int num_channels; |
| 39 | unsigned long clk_rate; |
Quentin Schulz | 7da3065 | 2024-03-14 10:36:21 +0100 | [diff] [blame] | 40 | int (*channel_data)(struct udevice *dev, int channel, unsigned int *data); |
Quentin Schulz | 2577525 | 2024-03-14 10:36:22 +0100 | [diff] [blame] | 41 | int (*start_channel)(struct udevice *dev, int channel); |
Quentin Schulz | d63c57e | 2024-03-14 10:36:23 +0100 | [diff] [blame^] | 42 | int (*stop)(struct udevice *dev); |
David Wu | ae3ed04 | 2017-09-20 14:28:16 +0800 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | struct rockchip_saradc_priv { |
Quentin Schulz | 0707bfd | 2024-03-14 10:36:20 +0100 | [diff] [blame] | 46 | union rockchip_saradc_regs regs; |
David Wu | ae3ed04 | 2017-09-20 14:28:16 +0800 | [diff] [blame] | 47 | int active_channel; |
| 48 | const struct rockchip_saradc_data *data; |
| 49 | }; |
| 50 | |
Quentin Schulz | 7da3065 | 2024-03-14 10:36:21 +0100 | [diff] [blame] | 51 | int rockchip_saradc_channel_data_v1(struct udevice *dev, int channel, |
| 52 | unsigned int *data) |
David Wu | ae3ed04 | 2017-09-20 14:28:16 +0800 | [diff] [blame] | 53 | { |
| 54 | struct rockchip_saradc_priv *priv = dev_get_priv(dev); |
David Wu | ae3ed04 | 2017-09-20 14:28:16 +0800 | [diff] [blame] | 55 | |
Quentin Schulz | 0707bfd | 2024-03-14 10:36:20 +0100 | [diff] [blame] | 56 | if ((readl(&priv->regs.v1->ctrl) & SARADC_CTRL_IRQ_STATUS) != |
David Wu | ae3ed04 | 2017-09-20 14:28:16 +0800 | [diff] [blame] | 57 | SARADC_CTRL_IRQ_STATUS) |
| 58 | return -EBUSY; |
| 59 | |
| 60 | /* Read value */ |
Quentin Schulz | 0707bfd | 2024-03-14 10:36:20 +0100 | [diff] [blame] | 61 | *data = readl(&priv->regs.v1->data); |
David Wu | ae3ed04 | 2017-09-20 14:28:16 +0800 | [diff] [blame] | 62 | |
| 63 | /* Power down adc */ |
Quentin Schulz | 0707bfd | 2024-03-14 10:36:20 +0100 | [diff] [blame] | 64 | writel(0, &priv->regs.v1->ctrl); |
David Wu | ae3ed04 | 2017-09-20 14:28:16 +0800 | [diff] [blame] | 65 | |
| 66 | return 0; |
| 67 | } |
| 68 | |
Quentin Schulz | 7da3065 | 2024-03-14 10:36:21 +0100 | [diff] [blame] | 69 | int rockchip_saradc_channel_data(struct udevice *dev, int channel, |
| 70 | unsigned int *data) |
| 71 | { |
| 72 | struct rockchip_saradc_priv *priv = dev_get_priv(dev); |
| 73 | struct adc_uclass_plat *uc_pdata = dev_get_uclass_plat(dev); |
| 74 | int ret; |
| 75 | |
| 76 | if (channel != priv->active_channel) { |
| 77 | pr_err("Requested channel is not active!"); |
| 78 | return -EINVAL; |
| 79 | } |
| 80 | |
| 81 | ret = priv->data->channel_data(dev, channel, data); |
| 82 | if (ret) { |
| 83 | if (ret != -EBUSY) |
| 84 | pr_err("Error reading channel data, %d!", ret); |
| 85 | return ret; |
| 86 | } |
| 87 | |
| 88 | *data &= uc_pdata->data_mask; |
| 89 | |
| 90 | return 0; |
| 91 | } |
| 92 | |
Quentin Schulz | 2577525 | 2024-03-14 10:36:22 +0100 | [diff] [blame] | 93 | int rockchip_saradc_start_channel_v1(struct udevice *dev, int channel) |
David Wu | ae3ed04 | 2017-09-20 14:28:16 +0800 | [diff] [blame] | 94 | { |
| 95 | struct rockchip_saradc_priv *priv = dev_get_priv(dev); |
| 96 | |
David Wu | ae3ed04 | 2017-09-20 14:28:16 +0800 | [diff] [blame] | 97 | /* 8 clock periods as delay between power up and start cmd */ |
Quentin Schulz | 0707bfd | 2024-03-14 10:36:20 +0100 | [diff] [blame] | 98 | writel(8, &priv->regs.v1->dly_pu_soc); |
David Wu | ae3ed04 | 2017-09-20 14:28:16 +0800 | [diff] [blame] | 99 | |
| 100 | /* Select the channel to be used and trigger conversion */ |
| 101 | writel(SARADC_CTRL_POWER_CTRL | (channel & SARADC_CTRL_CHN_MASK) | |
Quentin Schulz | 0707bfd | 2024-03-14 10:36:20 +0100 | [diff] [blame] | 102 | SARADC_CTRL_IRQ_ENABLE, &priv->regs.v1->ctrl); |
David Wu | ae3ed04 | 2017-09-20 14:28:16 +0800 | [diff] [blame] | 103 | |
Quentin Schulz | 2577525 | 2024-03-14 10:36:22 +0100 | [diff] [blame] | 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | int rockchip_saradc_start_channel(struct udevice *dev, int channel) |
| 108 | { |
| 109 | struct rockchip_saradc_priv *priv = dev_get_priv(dev); |
| 110 | int ret; |
| 111 | |
| 112 | if (channel < 0 || channel >= priv->data->num_channels) { |
| 113 | pr_err("Requested channel is invalid!"); |
| 114 | return -EINVAL; |
| 115 | } |
| 116 | |
| 117 | ret = priv->data->start_channel(dev, channel); |
| 118 | if (ret) { |
| 119 | pr_err("Error starting channel, %d!", ret); |
| 120 | return ret; |
| 121 | } |
| 122 | |
David Wu | ae3ed04 | 2017-09-20 14:28:16 +0800 | [diff] [blame] | 123 | priv->active_channel = channel; |
| 124 | |
| 125 | return 0; |
| 126 | } |
| 127 | |
Quentin Schulz | d63c57e | 2024-03-14 10:36:23 +0100 | [diff] [blame^] | 128 | int rockchip_saradc_stop_v1(struct udevice *dev) |
David Wu | ae3ed04 | 2017-09-20 14:28:16 +0800 | [diff] [blame] | 129 | { |
| 130 | struct rockchip_saradc_priv *priv = dev_get_priv(dev); |
| 131 | |
| 132 | /* Power down adc */ |
Quentin Schulz | 0707bfd | 2024-03-14 10:36:20 +0100 | [diff] [blame] | 133 | writel(0, &priv->regs.v1->ctrl); |
David Wu | ae3ed04 | 2017-09-20 14:28:16 +0800 | [diff] [blame] | 134 | |
Quentin Schulz | d63c57e | 2024-03-14 10:36:23 +0100 | [diff] [blame^] | 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | int rockchip_saradc_stop(struct udevice *dev) |
| 139 | { |
| 140 | struct rockchip_saradc_priv *priv = dev_get_priv(dev); |
| 141 | |
| 142 | if (priv->data->stop) { |
| 143 | int ret = priv->data->stop(dev); |
| 144 | |
| 145 | if (ret) { |
| 146 | pr_err("Error stopping channel, %d!", ret); |
| 147 | return ret; |
| 148 | } |
| 149 | } |
| 150 | |
David Wu | ae3ed04 | 2017-09-20 14:28:16 +0800 | [diff] [blame] | 151 | priv->active_channel = -1; |
| 152 | |
| 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | int rockchip_saradc_probe(struct udevice *dev) |
| 157 | { |
Peter Cai | e963228 | 2022-02-04 15:16:06 -0500 | [diff] [blame] | 158 | struct adc_uclass_plat *uc_pdata = dev_get_uclass_plat(dev); |
David Wu | ae3ed04 | 2017-09-20 14:28:16 +0800 | [diff] [blame] | 159 | struct rockchip_saradc_priv *priv = dev_get_priv(dev); |
Peter Cai | e963228 | 2022-02-04 15:16:06 -0500 | [diff] [blame] | 160 | struct udevice *vref; |
David Wu | ae3ed04 | 2017-09-20 14:28:16 +0800 | [diff] [blame] | 161 | struct clk clk; |
Peter Cai | e963228 | 2022-02-04 15:16:06 -0500 | [diff] [blame] | 162 | int vref_uv; |
David Wu | ae3ed04 | 2017-09-20 14:28:16 +0800 | [diff] [blame] | 163 | int ret; |
| 164 | |
| 165 | ret = clk_get_by_index(dev, 0, &clk); |
| 166 | if (ret) |
| 167 | return ret; |
| 168 | |
| 169 | ret = clk_set_rate(&clk, priv->data->clk_rate); |
| 170 | if (IS_ERR_VALUE(ret)) |
| 171 | return ret; |
| 172 | |
| 173 | priv->active_channel = -1; |
| 174 | |
Peter Cai | e963228 | 2022-02-04 15:16:06 -0500 | [diff] [blame] | 175 | ret = device_get_supply_regulator(dev, "vref-supply", &vref); |
| 176 | if (ret) { |
| 177 | printf("can't get vref-supply: %d\n", ret); |
| 178 | return ret; |
| 179 | } |
| 180 | |
| 181 | vref_uv = regulator_get_value(vref); |
| 182 | if (vref_uv < 0) { |
| 183 | printf("can't get vref-supply value: %d\n", vref_uv); |
| 184 | return vref_uv; |
| 185 | } |
| 186 | |
| 187 | /* VDD supplied by common vref pin */ |
| 188 | uc_pdata->vdd_supply = vref; |
| 189 | uc_pdata->vdd_microvolts = vref_uv; |
| 190 | uc_pdata->vss_microvolts = 0; |
| 191 | |
David Wu | ae3ed04 | 2017-09-20 14:28:16 +0800 | [diff] [blame] | 192 | return 0; |
| 193 | } |
| 194 | |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 195 | int rockchip_saradc_of_to_plat(struct udevice *dev) |
David Wu | ae3ed04 | 2017-09-20 14:28:16 +0800 | [diff] [blame] | 196 | { |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 197 | struct adc_uclass_plat *uc_pdata = dev_get_uclass_plat(dev); |
David Wu | ae3ed04 | 2017-09-20 14:28:16 +0800 | [diff] [blame] | 198 | struct rockchip_saradc_priv *priv = dev_get_priv(dev); |
| 199 | struct rockchip_saradc_data *data; |
| 200 | |
| 201 | data = (struct rockchip_saradc_data *)dev_get_driver_data(dev); |
Quentin Schulz | 0707bfd | 2024-03-14 10:36:20 +0100 | [diff] [blame] | 202 | priv->regs.v1 = dev_read_addr_ptr(dev); |
| 203 | if (!priv->regs.v1) { |
Masahiro Yamada | 9b643e3 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 204 | pr_err("Dev: %s - can't get address!", dev->name); |
Johan Jonker | ac9198d | 2023-03-13 01:29:35 +0100 | [diff] [blame] | 205 | return -EINVAL; |
David Wu | ae3ed04 | 2017-09-20 14:28:16 +0800 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | priv->data = data; |
Giulio Benetti | 9acae54 | 2022-03-14 10:09:43 +0100 | [diff] [blame] | 209 | uc_pdata->data_mask = (1 << priv->data->num_bits) - 1; |
David Wu | ae3ed04 | 2017-09-20 14:28:16 +0800 | [diff] [blame] | 210 | uc_pdata->data_format = ADC_DATA_FORMAT_BIN; |
| 211 | uc_pdata->data_timeout_us = SARADC_TIMEOUT / 5; |
| 212 | uc_pdata->channel_mask = (1 << priv->data->num_channels) - 1; |
| 213 | |
| 214 | return 0; |
| 215 | } |
| 216 | |
| 217 | static const struct adc_ops rockchip_saradc_ops = { |
| 218 | .start_channel = rockchip_saradc_start_channel, |
| 219 | .channel_data = rockchip_saradc_channel_data, |
| 220 | .stop = rockchip_saradc_stop, |
| 221 | }; |
| 222 | |
| 223 | static const struct rockchip_saradc_data saradc_data = { |
| 224 | .num_bits = 10, |
| 225 | .num_channels = 3, |
| 226 | .clk_rate = 1000000, |
Quentin Schulz | 7da3065 | 2024-03-14 10:36:21 +0100 | [diff] [blame] | 227 | .channel_data = rockchip_saradc_channel_data_v1, |
Quentin Schulz | 2577525 | 2024-03-14 10:36:22 +0100 | [diff] [blame] | 228 | .start_channel = rockchip_saradc_start_channel_v1, |
Quentin Schulz | d63c57e | 2024-03-14 10:36:23 +0100 | [diff] [blame^] | 229 | .stop = rockchip_saradc_stop_v1, |
David Wu | ae3ed04 | 2017-09-20 14:28:16 +0800 | [diff] [blame] | 230 | }; |
| 231 | |
| 232 | static const struct rockchip_saradc_data rk3066_tsadc_data = { |
| 233 | .num_bits = 12, |
| 234 | .num_channels = 2, |
| 235 | .clk_rate = 50000, |
Quentin Schulz | 7da3065 | 2024-03-14 10:36:21 +0100 | [diff] [blame] | 236 | .channel_data = rockchip_saradc_channel_data_v1, |
Quentin Schulz | 2577525 | 2024-03-14 10:36:22 +0100 | [diff] [blame] | 237 | .start_channel = rockchip_saradc_start_channel_v1, |
Quentin Schulz | d63c57e | 2024-03-14 10:36:23 +0100 | [diff] [blame^] | 238 | .stop = rockchip_saradc_stop_v1, |
David Wu | ae3ed04 | 2017-09-20 14:28:16 +0800 | [diff] [blame] | 239 | }; |
| 240 | |
| 241 | static const struct rockchip_saradc_data rk3399_saradc_data = { |
| 242 | .num_bits = 10, |
| 243 | .num_channels = 6, |
| 244 | .clk_rate = 1000000, |
Quentin Schulz | 7da3065 | 2024-03-14 10:36:21 +0100 | [diff] [blame] | 245 | .channel_data = rockchip_saradc_channel_data_v1, |
Quentin Schulz | 2577525 | 2024-03-14 10:36:22 +0100 | [diff] [blame] | 246 | .start_channel = rockchip_saradc_start_channel_v1, |
Quentin Schulz | d63c57e | 2024-03-14 10:36:23 +0100 | [diff] [blame^] | 247 | .stop = rockchip_saradc_stop_v1, |
David Wu | ae3ed04 | 2017-09-20 14:28:16 +0800 | [diff] [blame] | 248 | }; |
| 249 | |
| 250 | static const struct udevice_id rockchip_saradc_ids[] = { |
| 251 | { .compatible = "rockchip,saradc", |
| 252 | .data = (ulong)&saradc_data }, |
| 253 | { .compatible = "rockchip,rk3066-tsadc", |
| 254 | .data = (ulong)&rk3066_tsadc_data }, |
| 255 | { .compatible = "rockchip,rk3399-saradc", |
| 256 | .data = (ulong)&rk3399_saradc_data }, |
| 257 | { } |
| 258 | }; |
| 259 | |
| 260 | U_BOOT_DRIVER(rockchip_saradc) = { |
| 261 | .name = "rockchip_saradc", |
| 262 | .id = UCLASS_ADC, |
| 263 | .of_match = rockchip_saradc_ids, |
| 264 | .ops = &rockchip_saradc_ops, |
| 265 | .probe = rockchip_saradc_probe, |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 266 | .of_to_plat = rockchip_saradc_of_to_plat, |
Simon Glass | 41575d8 | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 267 | .priv_auto = sizeof(struct rockchip_saradc_priv), |
David Wu | ae3ed04 | 2017-09-20 14:28:16 +0800 | [diff] [blame] | 268 | }; |