On 2024/3/4 19:30, Quentin Schulz wrote:
From: Quentin Schulz <quentin.sch...@theobroma-systems.com>

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+ub...@0leil.net>
Signed-off-by: Quentin Schulz <quentin.sch...@theobroma-systems.com>
Reviewed-by: Kever Yang <kever.y...@rock-chips.com>

Thanks,
- Kever
---
  drivers/adc/rockchip-saradc.c | 37 +++++++++++++++++++++++++++++--------
  1 file changed, 29 insertions(+), 8 deletions(-)

diff --git a/drivers/adc/rockchip-saradc.c b/drivers/adc/rockchip-saradc.c
index 4a842556a60..1cc57beecc2 100644
--- a/drivers/adc/rockchip-saradc.c
+++ b/drivers/adc/rockchip-saradc.c
@@ -37,6 +37,7 @@ struct rockchip_saradc_data {
        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,28 +46,45 @@ struct rockchip_saradc_priv {
        const struct rockchip_saradc_data       *data;
  };
+int rockchip_saradc_channel_data_v1(struct udevice *dev, int channel,
+                                   unsigned int *data)
+{
+       struct rockchip_saradc_priv *priv = dev_get_priv(dev);
+
+       if ((readl(&priv->regs.v1->ctrl) & SARADC_CTRL_IRQ_STATUS) !=
+           SARADC_CTRL_IRQ_STATUS)
+               return -EBUSY;
+
+       /* Read value */
+       *data = readl(&priv->regs.v1->data);
+
+       /* Power down adc */
+       writel(0, &priv->regs.v1->ctrl);
+
+       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;
        }
- if ((readl(&priv->regs.v1->ctrl) & SARADC_CTRL_IRQ_STATUS) !=
-           SARADC_CTRL_IRQ_STATUS)
-               return -EBUSY;
+       ret = priv->data->channel_data(dev, channel, data);
+       if (ret) {
+               if (ret != -EBUSY)
+                       pr_err("Error reading channel data, %d!", ret);
+               return ret;
+       }
- /* Read value */
-       *data = readl(&priv->regs.v1->data);
        *data &= uc_pdata->data_mask;
- /* Power down adc */
-       writel(0, &priv->regs.v1->ctrl);
-
        return 0;
  }
@@ -174,18 +192,21 @@ static const struct rockchip_saradc_data saradc_data = {
        .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[] = {

Reply via email to