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