[PATCH 2/6] rtc: add rtc_write8_array() helper

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


Similar to the rtc_read8_array(), introduce a helper that allows the
caller to write multiple consecutive 8-bit registers with one call. If
the driver provides the ->write8_array method, use that, otherwise
loop using ->write8.

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

diff --git a/drivers/rtc/rtc-uclass.c b/drivers/rtc/rtc-uclass.c
index 5070fb416d..56490a876f 100644
--- a/drivers/rtc/rtc-uclass.c
+++ b/drivers/rtc/rtc-uclass.c
@@ -78,6 +78,24 @@ int rtc_write8(struct udevice *dev, unsigned int reg, int val)
 	return ops->write8(dev, reg, val);
 }
 
+int rtc_write8_array(struct udevice *dev, unsigned int reg,
+		     const u8 *buf, unsigned int len)
+{
+	struct rtc_ops *ops = rtc_get_ops(dev);
+
+	assert(ops);
+	if (ops->write8_array)
+		return ops->write8_array(dev, reg, buf, len);
+	if (!ops->write8)
+		return -ENOSYS;
+	while (len--) {
+		int ret = ops->write8(dev, reg++, *buf++);
+		if (ret < 0)
+			return ret;
+	}
+	return 0;
+}
+
 int rtc_read16(struct udevice *dev, unsigned int reg, u16 *valuep)
 {
 	u16 value = 0;
diff --git a/include/rtc.h b/include/rtc.h
index f7f622c1db..08b2a00567 100644
--- a/include/rtc.h
+++ b/include/rtc.h
@@ -85,6 +85,18 @@ struct rtc_ops {
 	* @return 0 if OK, -ve on error
 	*/
 	int (*write8)(struct udevice *dev, unsigned int reg, int val);
+
+	/**
+	 * write8_array() - Write multiple 8-bit registers
+	 *
+	 * @dev:	Device to write to
+	 * @reg:	First register to write
+	 * @buf:	Input buffer
+	 * @len:	Number of registers to write
+	 * @return 0 if OK, -ve on error
+	 */
+	int (*write8_array)(struct udevice *dev, unsigned int reg,
+			    const u8 *buf, unsigned int len);
 };
 
 /* Access the operations for an RTC device */
@@ -152,6 +164,18 @@ int rtc_read8_array(struct udevice *dev, unsigned int reg,
  */
 int rtc_write8(struct udevice *dev, unsigned int reg, int val);
 
+/**
+ * rtc_write8_array() - Write multiple 8-bit registers
+ *
+ * @dev:	Device to write to
+ * @reg:	First register to write
+ * @buf:	Input buffer
+ * @len:	Number of registers to write
+ * @return 0 if OK, -ve on error
+ */
+int rtc_write8_array(struct udevice *dev, unsigned int reg,
+		     const u8 *buf, unsigned int len);
+
 /**
  * rtc_read16() - Read a 16-bit value from the RTC
  *
-- 
2.23.0



More information about the U-Boot mailing list