Skip to contentSkip to conversation

max313xx RTC driver

PATCH Superseded Chris Packham 3 participants 6 messages Mar 16, 2023 – Mar 18, 2023 Real time clock (RTC)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover max313xx RTC driver

This series is based on the in-flight linux patch that is adding support for this family of RTCs to linux[1]. The u-boot driver is a bit different due to some of the differences between Linux and u-boot and I've dropped the support for hwmon and clock source functions. Where possible I've tried to keep things such that the U-Boot and Linux versions can be compared and kept in sync.

[1] - https://lore.kernel.org/all/[email protected]/

Chris Packham (2):
include: kernel.h: port find_closest() from Linux
drivers: rtc: add max313xx series rtc driver

 drivers/rtc/Kconfig    |   8 +
 drivers/rtc/Makefile   |   1 +
 drivers/rtc/max313xx.c | 442 +++++++++++++++++++++++++++++++++++++++++
 include/linux/kernel.h |  24 +++
 4 files changed, 475 insertions(+)
 create mode 100644 drivers/rtc/max313xx.c
Signature · 1 line
2.40.0
#1 lore raw

PATCH 1/2 include: kernel.h: port find_closest() from Linux

The find_closest() macro can be used to find an element in a sorted array that is closest to an input value.

---

 include/linux/kernel.h | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)
include/linux/kernel.h +24 -0
index 3e71d61074b6..5cd6c9dc8219 100644
@@ -284,4 +284,28 @@
 	offsetof(struct structure, member) == (offset), \
 	"`struct " #structure "` offset for `" #member "` is not " #offset)
 
+#define __find_closest(x, a, as, op)					\
+({									\
+	typeof(as) __fc_i, __fc_as = (as) - 1;				\
+	typeof(x) __fc_x = (x);						\
+	typeof(*a) const *__fc_a = (a);					\
+	for (__fc_i = 0; __fc_i < __fc_as; __fc_i++) {			\
+		if (__fc_x op DIV_ROUND_CLOSEST(__fc_a[__fc_i] +	\
+						__fc_a[__fc_i + 1], 2))	\
+			break;						\
+	}								\
+	(__fc_i);							\
+})
+
+/**
+ * find_closest - locate the closest element in a sorted array
+ * @x: The reference value.
+ * @a: The array in which to look for the closest element. Must be sorted
+ *  in ascending order.
+ * @as: Size of 'a'.
+ *
+ * Returns the index of the element closest to 'x'.
+ */
+#define find_closest(x, a, as) __find_closest(x, a, as, <=)
+
 #endif
Signature · 1 line
2.40.0
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] include: kernel.h: port find_closest() from Linux

Quoted 10 lines · Chris Packham

The find_closest() macro can be used to find an element in a sorted array that is closest to an input value.

---

 include/linux/kernel.h | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)

Good to mention the Linux version it was taken from

#3 lore raw

PATCH 2/2 drivers: rtc: add max313xx series rtc driver

In reply to Chris Packham

Adding support for Analog Devices MAX313XX series RTCs.

This is ported from the Linux driver and adapted for use in u-boot. Notable differences are
- handling of tm_year and tm_mon differ
- clock source support is omitted
- hwmon support for the MAX31328 and MAX31343 is omitted
- rtc_ops->reset is added

---

 drivers/rtc/Kconfig    |   8 +
 drivers/rtc/Makefile   |   1 +
 drivers/rtc/max313xx.c | 442 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 451 insertions(+)
 create mode 100644 drivers/rtc/max313xx.c
drivers/rtc/Kconfig +8 -0
index 35b6ed4d7c72..49c260b5b190 100644
@@ -134,6 +134,14 @@ config RTC_ISL1208
 	  This driver supports reading and writing the RTC/calendar and detects
 	  total power failures.
 
+config RTC_MAX313XX
+	bool "Analog Devices MAX313XX RTC driver"
+	depends on DM_RTC
+	depends on DM_I2C
+	help
+	  If you say yes here you will get support for the
+	  Analog Devices MAX313XX series RTC family.
+
 config RTC_PCF8563
 	tristate "Philips PCF8563"
 	help
drivers/rtc/Makefile +1 -0
index 447551e15aa2..adfa23f66702 100644
@@ -19,6 +19,7 @@ obj-$(CONFIG_RTC_HT1380) += ht1380.o
 obj-$(CONFIG_SANDBOX) += i2c_rtc_emul.o
 obj-$(CONFIG_RTC_ISL1208) += isl1208.o
 obj-$(CONFIG_RTC_M41T62) += m41t62.o
+obj-$(CONFIG_RTC_MAX313XX) += max313xx.o
 obj-$(CONFIG_RTC_MC13XXX) += mc13xxx-rtc.o
 obj-$(CONFIG_RTC_MC146818) += mc146818.o
 obj-$(CONFIG_MCFRTC) += mcfrtc.o
drivers/rtc/max313xx.c new file +442 -0

442 lines not on this page · raw message

Signature · 1 line
2.40.0
#4 lore raw

Re: [PATCH 2/2] drivers: rtc: add max313xx series rtc driver

Quoted 9 lines · Chris Packham

Adding support for Analog Devices MAX313XX series RTCs.

This is ported from the Linux driver and adapted for use in u-boot. Notable differences are
- handling of tm_year and tm_mon differ
- clock source support is omitted
- hwmon support for the MAX31328 and MAX31343 is omitted
- rtc_ops->reset is added

Quoted 8 lines

---

 drivers/rtc/Kconfig    |   8 +
 drivers/rtc/Makefile   |   1 +
 drivers/rtc/max313xx.c | 442 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 451 insertions(+)
 create mode 100644 drivers/rtc/max313xx.c

Can we please enable this in sandbox as well, so it's tested in CI and put through static analysis? Thanks.

Signature · 1 line
Tom
Reviewed-by on v1 2/2
#5 lore raw

Re: [PATCH 2/2] drivers: rtc: add max313xx series rtc driver

In reply to Chris Packham

Hi Chris,

Quoted 10 lines · Chris Packham

Adding support for Analog Devices MAX313XX series RTCs.

This is ported from the Linux driver and adapted for use in u-boot. Notable differences are
- handling of tm_year and tm_mon differ
- clock source support is omitted
- hwmon support for the MAX31328 and MAX31343 is omitted
- rtc_ops->reset is added

Quoted 8 lines

---

 drivers/rtc/Kconfig    |   8 +
 drivers/rtc/Makefile   |   1 +
 drivers/rtc/max313xx.c | 442 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 451 insertions(+)
 create mode 100644 drivers/rtc/max313xx.c

with requests below

Quoted 6 lines
drivers/rtc/Kconfig +0 -0
index 35b6ed4d7c72..49c260b5b190 100644

drivers/rtc/Kconfig config RTC_ISL1208 · line 143

Quoted 10 lines
drivers/rtc/Kconfig +7 -0
@@ -134,6 +134,14 @@ config RTC_ISL1208
          This driver supports reading and writing the RTC/calendar and detects
          total power failures.
 
+config RTC_MAX313XX
+       bool "Analog Devices MAX313XX RTC driver"
+       depends on DM_RTC
+       depends on DM_I2C
+       help
+         If you say yes here you will get support for the
+         Analog Devices MAX313XX series RTC family.

Can you mention the features the chip supports and anything notable about the driver support/lack of support? E.g. does it have CMOS RAM?

Quoted 26 lines
drivers/rtc/Kconfig +1 -0
+
 config RTC_PCF8563
        tristate "Philips PCF8563"
        help
drivers/rtc/Makefile +1 -0
index 447551e15aa2..adfa23f66702 100644
@@ -19,6 +19,7 @@ obj-$(CONFIG_RTC_HT1380) += ht1380.o
 obj-$(CONFIG_SANDBOX) += i2c_rtc_emul.o
 obj-$(CONFIG_RTC_ISL1208) += isl1208.o
 obj-$(CONFIG_RTC_M41T62) += m41t62.o
+obj-$(CONFIG_RTC_MAX313XX) += max313xx.o
 obj-$(CONFIG_RTC_MC13XXX) += mc13xxx-rtc.o
 obj-$(CONFIG_RTC_MC146818) += mc146818.o
 obj-$(CONFIG_MCFRTC) += mcfrtc.o
drivers/rtc/max313xx.c new file +4 -0
new file mode 100644
index 000000000000..1aa430d121ee
@@ -0,0 +1,442 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Analog Devices MAX313XX series I2C RTC driver
+ *

drivers/rtc/max313xx.c line 14

Quoted 10 lines
drivers/rtc/max313xx.c new file +10 -0
+ * Copyright 2022 Analog Devices Inc.
+ */
+#include <bcd.h>
+#include <dm.h>
+#include <dm/device_compat.h>
+#include <i2c.h>
+#include <linux/bitfield.h>
+#include <linux/delay.h>
+#include <linux/kernel.h>
+#include <rtc.h>

Please check header order...the dm/ and linux/ ones should go at the end

https://u-boot.readthedocs.io/en/latest/develop/codingstyle.html#include-files

[..]

Quoted 8 lines
drivers/rtc/max313xx.c new file +8 -0
+struct chip_desc {
+       struct clkout_cfg *clkout;
+       const char *clkout_name;
+       u8 sec_reg;
+       u8 alarm1_sec_reg;
+
+       u8 int_en_reg;
+       u8 int_status_reg;

drivers/rtc/max313xx.c line 90

Quoted 10 lines
drivers/rtc/max313xx.c new file +10 -0
+
+       u8 ram_reg;
+       u8 ram_size;
+
+       u8 temp_reg;
+
+       u8 trickle_reg;
+
+       u8 rst_reg;
+       u8 rst_bit;

Please comment this struct

drivers/rtc/max313xx.c line 93

Quoted 3 lines
drivers/rtc/max313xx.c new file +3 -0
+};
+
+struct max313xx {

The convention is to use a _priv suffix on this type. If this would increase the diff from Linux, then it is OK as is.

Quoted 4 lines
drivers/rtc/max313xx.c new file +4 -0
+       enum max313xx_ids id;
+       const struct chip_desc *chip;
+};
+

[..]

[..]

Quoted 14 lines
drivers/rtc/max313xx.c new file +14 -0
+
+static int max313xx_trickle_charger_setup(struct udevice *dev)
+{
+       struct max313xx *rtc = dev_get_priv(dev);
+       bool diode;
+       int index, reg;
+       u32 ohms;
+       u32 chargeable;
+       int ret;
+
+       if (dev_read_u32(dev, "trickle-resistor-ohms", &ohms) ||
+           dev_read_u32(dev, "aux-voltage-chargeable", &chargeable))
+               return 0;
+

drivers/rtc/max313xx.c line 360

Quoted 10 lines
drivers/rtc/max313xx.c new file +10 -0
+       switch (chargeable) {
+       case 0:
+               diode = false;
+               break;
+       case 1:
+               diode = true;
+               break;
+       default:
+               dev_err(dev, "unsupported aux-voltage-chargeable value\n");
+               return -EINVAL;

consider deb_dbg() as code size is smaller

drivers/rtc/max313xx.c line 365

Quoted 5 lines
drivers/rtc/max313xx.c new file +5 -0
+       }
+
+       if (!rtc->chip->trickle_reg) {
+               dev_warn(dev, "device does not have trickle charger\n");
+               return 0;

-ENOTSUPP ?

[..]

Regards,
Simon