[PATCH 1/6] rtc: add rtc_read8_array helper and ->read8_array method

Rasmus Villemoes rasmus.villemoes at prevas.dk
Mon May 4 23:20:27 CEST 2020


Some users may want to read multiple consecutive 8-bit
registers. Instead of each caller having to implement the loop,
provide a rtc_read8_array() helper. Also, allow a driver to provide a
read8_array method, which can be more efficient than reading one
register at a time.

Signed-off-by: Rasmus Villemoes <rasmus.villemoes at prevas.dk>
---
 drivers/rtc/rtc-uclass.c | 19 +++++++++++++++++++
 include/rtc.h            | 24 ++++++++++++++++++++++++
 2 files changed, 43 insertions(+)

diff --git a/drivers/rtc/rtc-uclass.c b/drivers/rtc/rtc-uclass.c
index a0a238aedd..5070fb416d 100644
--- a/drivers/rtc/rtc-uclass.c
+++ b/drivers/rtc/rtc-uclass.c
@@ -49,6 +49,25 @@ int rtc_read8(struct udevice *dev, unsigned int reg)
 	return ops->read8(dev, reg);
 }
 
+int rtc_read8_array(struct udevice *dev, unsigned int reg,
+		    u8 *buf, unsigned int len)
+{
+	struct rtc_ops *ops = rtc_get_ops(dev);
+
+	assert(ops);
+	if (ops->read8_array)
+		return ops->read8_array(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_write8(struct udevice *dev, unsigned int reg, int val)
 {
 	struct rtc_ops *ops = rtc_get_ops(dev);
diff --git a/include/rtc.h b/include/rtc.h
index 8aabfc1162..f7f622c1db 100644
--- a/include/rtc.h
+++ b/include/rtc.h
@@ -64,6 +64,18 @@ struct rtc_ops {
 	 */
 	int (*read8)(struct udevice *dev, unsigned int reg);
 
+	/**
+	 * read8_array() - 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 (*read8_array)(struct udevice *dev, unsigned int reg,
+			   u8 *buf, unsigned int len);
+
 	/**
 	* write8() - Write an 8-bit register
 	*
@@ -118,6 +130,18 @@ int dm_rtc_reset(struct udevice *dev);
  */
 int rtc_read8(struct udevice *dev, unsigned int reg);
 
+/**
+ * rtc_read8_array() - 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 rtc_read8_array(struct udevice *dev, unsigned int reg,
+		    u8 *buf, unsigned int len);
+
 /**
  * rtc_write8() - Write an 8-bit register
  *
-- 
2.23.0



More information about the U-Boot mailing list