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