diff mbox series

[3/6] rtc: fall back to ->{read, write}8_array if ->{read, write}8 are not provided

Message ID 20200504212032.3759-4-rasmus.villemoes@prevas.dk
State Superseded
Headers show
Series rtc: add rtc_{read,write}8_array and rtc command | expand

Commit Message

Rasmus Villemoes May 4, 2020, 9:20 p.m. UTC
Similar to how the rtc_{read,write}8_array functions fall back to
using the {read,write}8 methods, do the opposite in the
rtc_{read,write}8 functions.

This way, each driver only needs to provide either ->read8 or
->read8_array to make both rtc_read8() and rtc_read8_array() work -
without this, a driver that provides rtc_read8_array() would most
likely just duplicate the logic here for implementing a ->read8()
method in term of its ->read8_array() method. The same remarks of
course apply to the write case.

Signed-off-by: Rasmus Villemoes <rasmus.villemoes at prevas.dk>
---
 drivers/rtc/rtc-uclass.c | 24 ++++++++++++++++++------
 1 file changed, 18 insertions(+), 6 deletions(-)

Comments

Simon Glass May 6, 2020, 3:42 a.m. UTC | #1
On Mon, 4 May 2020 at 15:20, Rasmus Villemoes
<rasmus.villemoes at prevas.dk> wrote:
>
> Similar to how the rtc_{read,write}8_array functions fall back to
> using the {read,write}8 methods, do the opposite in the
> rtc_{read,write}8 functions.
>
> This way, each driver only needs to provide either ->read8 or
> ->read8_array to make both rtc_read8() and rtc_read8_array() work -
> without this, a driver that provides rtc_read8_array() would most
> likely just duplicate the logic here for implementing a ->read8()
> method in term of its ->read8_array() method. The same remarks of
> course apply to the write case.
>
> Signed-off-by: Rasmus Villemoes <rasmus.villemoes at prevas.dk>
> ---
>  drivers/rtc/rtc-uclass.c | 24 ++++++++++++++++++------
>  1 file changed, 18 insertions(+), 6 deletions(-)

Reviewed-by: Simon Glass <sjg at chromium.org>

Please make sure you add tests for these methods.
diff mbox series

Patch

diff --git a/drivers/rtc/rtc-uclass.c b/drivers/rtc/rtc-uclass.c
index 56490a876f..6bf964f937 100644
--- a/drivers/rtc/rtc-uclass.c
+++ b/drivers/rtc/rtc-uclass.c
@@ -44,9 +44,17 @@  int rtc_read8(struct udevice *dev, unsigned int reg)
 	struct rtc_ops *ops = rtc_get_ops(dev);
 
 	assert(ops);
-	if (!ops->read8)
-		return -ENOSYS;
-	return ops->read8(dev, reg);
+	if (ops->read8)
+		return ops->read8(dev, reg);
+	if (ops->read8_array) {
+		u8 buf[1];
+		int ret = ops->read8_array(dev, reg, buf, 1);
+
+		if (ret < 0)
+			return ret;
+		return buf[0];
+	}
+	return -ENOSYS;
 }
 
 int rtc_read8_array(struct udevice *dev, unsigned int reg,
@@ -73,9 +81,13 @@  int rtc_write8(struct udevice *dev, unsigned int reg, int val)
 	struct rtc_ops *ops = rtc_get_ops(dev);
 
 	assert(ops);
-	if (!ops->write8)
-		return -ENOSYS;
-	return ops->write8(dev, reg, val);
+	if (ops->write8)
+		return ops->write8(dev, reg, val);
+	if (ops->write8_array) {
+		u8 buf[1] = { val };
+		return ops->write8_array(dev, reg, buf, 1);
+	}
+	return -ENOSYS;
 }
 
 int rtc_write8_array(struct udevice *dev, unsigned int reg,