]> git.dujemihanovic.xyz Git - u-boot.git/commitdiff
rockchip: adc: rockchip-saradc: factor out channel_data callback
authorQuentin Schulz <quentin.schulz@theobroma-systems.com>
Thu, 14 Mar 2024 09:36:21 +0000 (10:36 +0100)
committerKever Yang <kever.yang@rock-chips.com>
Thu, 14 Mar 2024 10:19:44 +0000 (18:19 +0800)
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>
drivers/adc/rockchip-saradc.c

index 4a842556a60cdf1fca9a9aa38a113565531077a0..1cc57beecc2e9481065fc1887007edc9617cb4c3 100644 (file)
@@ -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[] = {