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