diff mbox series

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

Message ID 20200504212032.3759-2-rasmus.villemoes@prevas.dk
State New
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
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(+)

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:
>
> 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)

How about just rtc_read() ?

Reviewed-by: Simon Glass <sjg at chromium.org>
Rasmus Villemoes May 6, 2020, 8:13 a.m. UTC | #2
On 06/05/2020 05.42, Simon Glass wrote:
> On Mon, 4 May 2020 at 15:20, Rasmus Villemoes
> <rasmus.villemoes at prevas.dk> wrote:
>>
>> 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)
> 
> How about just rtc_read() ?

I certainly like a shorter name, and I suppose 8-bit registers are
ubiquitous enough among RTCs. If no-one else speaks up, I'll rename both
the functions and the methods in the next revision.

Thanks,
Rasmus
diff mbox series

Patch

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
  *