diff mbox series

[v3,01/10] rtc: add dm_rtc_read helper and ->read method

Message ID 20200602191403.7542-2-rasmus.villemoes@prevas.dk
State Superseded
Headers show
Series new rtc methods, rtc command, and tests | expand

Commit Message

Rasmus Villemoes June 2, 2020, 7:13 p.m. UTC
Some users may want to read multiple consecutive 8-bit
registers. Instead of each caller having to implement the loop,
provide a dm_rtc_read() helper. Also, allow a driver to provide a
->read method, which can be more efficient than reading one register
at a time.

Reviewed-by: Simon Glass <sjg at chromium.org>
Signed-off-by: Rasmus Villemoes <rasmus.villemoes at prevas.dk>
---
 drivers/rtc/rtc-uclass.c | 18 ++++++++++++++++++
 include/rtc.h            | 23 +++++++++++++++++++++++
 2 files changed, 41 insertions(+)

Comments

Heiko Schocher June 3, 2020, 4:18 a.m. UTC | #1
Hello Rasmus,

Am 02.06.2020 um 21:13 schrieb Rasmus Villemoes:
> Some users may want to read multiple consecutive 8-bit
> registers. Instead of each caller having to implement the loop,
> provide a dm_rtc_read() helper. Also, allow a driver to provide a
> ->read method, which can be more efficient than reading one register
> at a time.
> 
> Reviewed-by: Simon Glass <sjg at chromium.org>
> Signed-off-by: Rasmus Villemoes <rasmus.villemoes at prevas.dk>
> ---
>   drivers/rtc/rtc-uclass.c | 18 ++++++++++++++++++
>   include/rtc.h            | 23 +++++++++++++++++++++++
>   2 files changed, 41 insertions(+)

Reviewed-by: Heiko Schocher <hs at denx.de>

bye,
Heiko
diff mbox series

Patch

diff --git a/drivers/rtc/rtc-uclass.c b/drivers/rtc/rtc-uclass.c
index 926cca234e..92cc8c5664 100644
--- a/drivers/rtc/rtc-uclass.c
+++ b/drivers/rtc/rtc-uclass.c
@@ -40,6 +40,24 @@  int dm_rtc_reset(struct udevice *dev)
 	return ops->reset(dev);
 }
 
+int dm_rtc_read(struct udevice *dev, unsigned int reg, u8 *buf, unsigned int len)
+{
+	struct rtc_ops *ops = rtc_get_ops(dev);
+
+	assert(ops);
+	if (ops->read)
+		return ops->read(dev, reg, buf, len);
+	if (!ops->read8)
+		return -ENOSYS;
+	while (len--) {
+		int ret = ops->read8(dev, reg++);
+		if (ret < 0)
+			return ret;
+		*buf++ = ret;
+	}
+	return 0;
+}
+
 int rtc_read8(struct udevice *dev, unsigned int reg)
 {
 	struct rtc_ops *ops = rtc_get_ops(dev);
diff --git a/include/rtc.h b/include/rtc.h
index 8aabfc1162..6887fe4d42 100644
--- a/include/rtc.h
+++ b/include/rtc.h
@@ -55,6 +55,18 @@  struct rtc_ops {
 	 */
 	int (*reset)(struct udevice *dev);
 
+	/**
+	 * read() - Read multiple 8-bit registers
+	 *
+	 * @dev:	Device to read from
+	 * @reg:	First register to read
+	 * @buf:	Output buffer
+	 * @len:	Number of registers to read
+	 * @return 0 if OK, -ve on error
+	 */
+	int (*read)(struct udevice *dev, unsigned int reg,
+			   u8 *buf, unsigned int len);
+
 	/**
 	 * read8() - Read an 8-bit register
 	 *
@@ -109,6 +121,17 @@  int dm_rtc_set(struct udevice *dev, struct rtc_time *time);
  */
 int dm_rtc_reset(struct udevice *dev);
 
+/**
+ * dm_rtc_read() - Read multiple 8-bit registers
+ *
+ * @dev:	Device to read from
+ * @reg:	First register to read
+ * @buf:	Output buffer
+ * @len:	Number of registers to read
+ * @return 0 if OK, -ve on error
+ */
+int dm_rtc_read(struct udevice *dev, unsigned int reg, u8 *buf, unsigned int len);
+
 /**
  * rtc_read8() - Read an 8-bit register
  *