rockchip: adc: rockchip-saradc: factor out channel_data callback
SARADC v1 and v2 have a different way of reading data, therefore let's
abstract this function so that it can be provided from the udevice.data
pointer.
Cc: Quentin Schulz <foss+uboot@0leil.net>
Reviewed-by: Kever Yang <kever.yang@rock-chips.com>
Signed-off-by: Quentin Schulz <quentin.schulz@theobroma-systems.com>
diff --git a/drivers/adc/rockchip-saradc.c b/drivers/adc/rockchip-saradc.c
index 4a84255..1cc57be 100644
--- a/drivers/adc/rockchip-saradc.c
+++ b/drivers/adc/rockchip-saradc.c
@@ -37,6 +37,7 @@
int num_bits;
int num_channels;
unsigned long clk_rate;
+ int (*channel_data)(struct udevice *dev, int channel, unsigned int *data);
};
struct rockchip_saradc_priv {
@@ -45,16 +46,10 @@
const struct rockchip_saradc_data *data;
};
-int rockchip_saradc_channel_data(struct udevice *dev, int channel,
- unsigned int *data)
+int rockchip_saradc_channel_data_v1(struct udevice *dev, int channel,
+ unsigned int *data)
{
struct rockchip_saradc_priv *priv = dev_get_priv(dev);
- struct adc_uclass_plat *uc_pdata = dev_get_uclass_plat(dev);
-
- if (channel != priv->active_channel) {
- pr_err("Requested channel is not active!");
- return -EINVAL;
- }
if ((readl(&priv->regs.v1->ctrl) & SARADC_CTRL_IRQ_STATUS) !=
SARADC_CTRL_IRQ_STATUS)
@@ -62,7 +57,6 @@
/* Read value */
*data = readl(&priv->regs.v1->data);
- *data &= uc_pdata->data_mask;
/* Power down adc */
writel(0, &priv->regs.v1->ctrl);
@@ -70,6 +64,30 @@
return 0;
}
+int rockchip_saradc_channel_data(struct udevice *dev, int channel,
+ unsigned int *data)
+{
+ struct rockchip_saradc_priv *priv = dev_get_priv(dev);
+ struct adc_uclass_plat *uc_pdata = dev_get_uclass_plat(dev);
+ int ret;
+
+ if (channel != priv->active_channel) {
+ pr_err("Requested channel is not active!");
+ return -EINVAL;
+ }
+
+ ret = priv->data->channel_data(dev, channel, data);
+ if (ret) {
+ if (ret != -EBUSY)
+ pr_err("Error reading channel data, %d!", ret);
+ return ret;
+ }
+
+ *data &= uc_pdata->data_mask;
+
+ return 0;
+}
+
int rockchip_saradc_start_channel(struct udevice *dev, int channel)
{
struct rockchip_saradc_priv *priv = dev_get_priv(dev);
@@ -174,18 +192,21 @@
.num_bits = 10,
.num_channels = 3,
.clk_rate = 1000000,
+ .channel_data = rockchip_saradc_channel_data_v1,
};
static const struct rockchip_saradc_data rk3066_tsadc_data = {
.num_bits = 12,
.num_channels = 2,
.clk_rate = 50000,
+ .channel_data = rockchip_saradc_channel_data_v1,
};
static const struct rockchip_saradc_data rk3399_saradc_data = {
.num_bits = 10,
.num_channels = 6,
.clk_rate = 1000000,
+ .channel_data = rockchip_saradc_channel_data_v1,
};
static const struct udevice_id rockchip_saradc_ids[] = {