Skip to contentSkip to conversation

aiodev: add driver for Rockchip SARADC

PATCH Superseded Michael Riesch 2 participants 3 messages Jun 17, 2021 – Jun 21, 2021 Arm Rockchip

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 aiodev: add driver for Rockchip SARADC

This commit adds support for the Successive Approximation Register (SAR) ADCs that can be found in Rockchip SoCs, such as the RK3568.

---
 drivers/aiodev/Kconfig           |   7 ++
 drivers/aiodev/Makefile          |   1 +
 drivers/aiodev/rockchip_saradc.c | 153 +++++++++++++++++++++++++++++++
 3 files changed, 161 insertions(+)
 create mode 100644 drivers/aiodev/rockchip_saradc.c
drivers/aiodev/Kconfig +7 -0
index 88d013aad..98223a8f9 100644
@@ -50,4 +50,11 @@ config STM32_ADC
 	  Support for ADC on STM32.  Supports simple one-shot readings
 	  rather than continuous sampling with DMA, etc.  ADC channels should be
 	  configured via device tree, using the kernel bindings.
+
+config ROCKCHIP_SARADC
+	tristate "Rockchip SARADC driver"
+	depends on ARCH_RK3568
+	help
+	  Support for Successive Approximation Register (SAR) ADC in Rockchip
+	  SoCs.
 endif
drivers/aiodev/Makefile +1 -0
index 52652f67b..1b480f6fa 100644
@@ -6,3 +6,4 @@ obj-$(CONFIG_MC13XXX_ADC) += mc13xxx_adc.o
 obj-$(CONFIG_QORIQ_THERMAL) += qoriq_thermal.o
 obj-$(CONFIG_AM335X_ADC) += am335x_adc.o
 obj-$(CONFIG_STM32_ADC) += stm32-adc.o stm32-adc-core.o
+obj-$(CONFIG_ROCKCHIP_SARADC) += rockchip_saradc.o
drivers/aiodev/rockchip_saradc.c new file +153 -0

153 lines not on this page · raw message

Signature · 7 lines
2.20.1


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#1 lore raw

Hi Michael,

Quoted 19 lines · Michael Riesch
drivers/aiodev/rockchip_saradc.c new file +19 -0
+static int rockchip_saradc_read(struct aiochannel *chan, int *val)
+{
+	struct rockchip_saradc_data *data;
+	int timeout = SARADC_TIMEOUT;
+	u32 value = 0;
+	u32 control = 0;
+	u32 mask;
+
+	if (!chan || !val)
+		return -EINVAL;
+
+	data = container_of(chan->aiodev, struct rockchip_saradc_data, aiodev);
+	if (!data)
+		return -EINVAL;
+
+	rockchip_saradc_reg_wr(data, 8, SARADC_DLY_PU_SOC);
+	rockchip_saradc_reg_wr(data,
+			       (chan->index & SARADC_CTRL_CHN_MASK) |
+				       SARADC_CTRL_IRQ_ENABLE |

drivers/aiodev/rockchip_saradc.c line 77

Quoted 10 lines
drivers/aiodev/rockchip_saradc.c new file +10 -0
+				       SARADC_CTRL_POWER_CTRL,
+			       SARADC_CTRL);
+
+	do {
+		control = rockchip_saradc_reg_rd(data, SARADC_CTRL);
+
+		if (--timeout == 0)
+			return -ETIMEDOUT;
+		mdelay(1);
+	} while (!(control & SARADC_CTRL_IRQ_STATUS));

You should do a timeout loop with

	u64 start = get_time_ns();

	do {
		if (is_timeout(start, 100 * MSECOND)
			return -ETIMEDOUT;
	} while(bar);

We don't have any way nor need to put the CPU into idle, so we can poll as fast as we can.

drivers/aiodev/rockchip_saradc.c line 83

Quoted 6 lines
drivers/aiodev/rockchip_saradc.c new file +6 -0
+
+	mask = (1 << data->config->num_bits) - 1;
+	value = rockchip_saradc_reg_rd(data, SARADC_DATA) & mask;
+	rockchip_saradc_reg_wr(data, 0, SARADC_CTRL);
+
+	printf("Raw value: %d\n", value);

Debugging leftover.

Quoted 12 lines
drivers/aiodev/rockchip_saradc.c new file +11 -0
+fail_channels:
+	kfree(data->channels);
+	kfree(data->aiodev.channels);
+
+fail_data:
+	kfree(data);
+	return ret;
+}
+
+static const struct rockchip_saradc_cfg rk3568_saradc_cfg = {
+	.ref_voltage_mv = 1800,
Quoted 1 line

From looking at the downstream dts files this doesn't seem to be SoC

specific:

	&saradc {
        	status = "okay";
		vref-supply = <&vcca_1v8>;
	};

I suggest doing the same here.

drivers/aiodev/rockchip_saradc.c line 144

Quoted 6 lines
drivers/aiodev/rockchip_saradc.c new file +6 -0
+	.num_bits = 10,
+	.num_channels = 8,
+};
+
+static const struct of_device_id of_rockchip_saradc_match[] = {
+	{ .compatible = "rockchip,rk3568-saradc", .data = &rk3568_saradc_cfg },

The device nodes in the downstream Kernel also contain some clocks. These should be handled in the driver.

Sascha

Signature · 9 lines
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#2 lore raw

Hello Sascha,

thanks for the review, I'll prepare a v2.

Quoted 42 lines · Sascha Hauer

Hi Michael,

Quoted 19 lines · Michael Riesch
drivers/aiodev/rockchip_saradc.c new file +19 -0
+static int rockchip_saradc_read(struct aiochannel *chan, int *val)
+{
+	struct rockchip_saradc_data *data;
+	int timeout = SARADC_TIMEOUT;
+	u32 value = 0;
+	u32 control = 0;
+	u32 mask;
+
+	if (!chan || !val)
+		return -EINVAL;
+
+	data = container_of(chan->aiodev, struct rockchip_saradc_data, aiodev);
+	if (!data)
+		return -EINVAL;
+
+	rockchip_saradc_reg_wr(data, 8, SARADC_DLY_PU_SOC);
+	rockchip_saradc_reg_wr(data,
+			       (chan->index & SARADC_CTRL_CHN_MASK) |
+				       SARADC_CTRL_IRQ_ENABLE |

drivers/aiodev/rockchip_saradc.c line 77

Quoted 10 lines
drivers/aiodev/rockchip_saradc.c new file +10 -0
+				       SARADC_CTRL_POWER_CTRL,
+			       SARADC_CTRL);
+
+	do {
+		control = rockchip_saradc_reg_rd(data, SARADC_CTRL);
+
+		if (--timeout == 0)
+			return -ETIMEDOUT;
+		mdelay(1);
+	} while (!(control & SARADC_CTRL_IRQ_STATUS));

You should do a timeout loop with

	u64 start = get_time_ns();

	do {
		if (is_timeout(start, 100 * MSECOND)
			return -ETIMEDOUT;
	} while(bar);
Quoted 2 lines

We don't have any way nor need to put the CPU into idle, so we can poll as fast as we can.

OK, will replace that.

Quoted 8 lines

drivers/aiodev/rockchip_saradc.c line 83

Quoted 6 lines
drivers/aiodev/rockchip_saradc.c new file +6 -0
+
+	mask = (1 << data->config->num_bits) - 1;
+	value = rockchip_saradc_reg_rd(data, SARADC_DATA) & mask;
+	rockchip_saradc_reg_wr(data, 0, SARADC_CTRL);
+
+	printf("Raw value: %d\n", value);

Debugging leftover.

D'oh!

Quoted 22 lines

drivers/aiodev/rockchip_saradc.c line 138

Quoted 11 lines
drivers/aiodev/rockchip_saradc.c new file +11 -0
+fail_channels:
+	kfree(data->channels);
+	kfree(data->aiodev.channels);
+
+fail_data:
+	kfree(data);
+	return ret;
+}
+
+static const struct rockchip_saradc_cfg rk3568_saradc_cfg = {
+	.ref_voltage_mv = 1800,

From looking at the downstream dts files this doesn't seem to be SoC specific:

	&saradc {
        	status = "okay";
		vref-supply = <&vcca_1v8>;
	};

I suggest doing the same here.

Quoted 9 lines

drivers/aiodev/rockchip_saradc.c line 144

Quoted 6 lines
drivers/aiodev/rockchip_saradc.c new file +6 -0
+	.num_bits = 10,
+	.num_channels = 8,
+};
+
+static const struct of_device_id of_rockchip_saradc_match[] = {
+	{ .compatible = "rockchip,rk3568-saradc", .data = &rk3568_saradc_cfg },

The device nodes in the downstream Kernel also contain some clocks. These should be handled in the driver.

I'll see what I can do :-)

Just had a look over the mainstream Kernel device tree. Resets, interrupts and number of io-channels are also provided there. Now I think that the interrupts are quite irrelevant for barebox, but should the resets be addressed as well?

As to the number of iochannels, I think the maximum number should be provided in the SoC specific struct. Not sure what the value of specifying the actual number of channels in the device tree is, though.

Regards, Michael

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox