git: a534b50e245d - main - tegra: Improve the detection of the secondary function (RTC) of the MAX77620.

From: Michal Meloun <mmel_at_FreeBSD.org>
Date: Sun, 20 Feb 2022 11:39:02 UTC
The branch main has been updated by mmel:

URL: https://cgit.FreeBSD.org/src/commit/?id=a534b50e245d801af887d91b5d48ebcf120aa039

commit a534b50e245d801af887d91b5d48ebcf120aa039
Author:     Michal Meloun <mmel@FreeBSD.org>
AuthorDate: 2022-02-20 10:18:42 +0000
Commit:     Michal Meloun <mmel@FreeBSD.org>
CommitDate: 2022-02-20 11:26:04 +0000

    tegra: Improve the detection of the secondary function (RTC) of the MAX77620.
    
    Use the new ofw_iicbus_set_devinfo() method to implant an OFW compatibility
    string for a manually created RTC sub-device.
    
    MFC after:      4 weeks
    Reported by:    archimedes.gaviola_at_gmail.com
                    bscott_at_bunyatech.com.au
---
 sys/arm64/nvidia/tegra210/max77620_rtc.c | 45 ++++++++++++++++++++------------
 1 file changed, 28 insertions(+), 17 deletions(-)

diff --git a/sys/arm64/nvidia/tegra210/max77620_rtc.c b/sys/arm64/nvidia/tegra210/max77620_rtc.c
index 09da17ad7d8f..4dace946a891 100644
--- a/sys/arm64/nvidia/tegra210/max77620_rtc.c
+++ b/sys/arm64/nvidia/tegra210/max77620_rtc.c
@@ -40,9 +40,12 @@ __FBSDID("$FreeBSD$");
 #include <dev/iicbus/iiconf.h>
 #include <dev/iicbus/iicbus.h>
 #include <dev/ofw/ofw_bus.h>
+#include <dev/ofw/ofw_bus_subr.h>
 
 #include "clock_if.h"
+#include "ofw_iicbus_if.h"
 #include "max77620.h"
+
 #define	MAX77620_RTC_INT	0x00
 #define	MAX77620_RTC_INTM	0x01
 #define	MAX77620_RTC_CONTROLM	0x02
@@ -91,6 +94,8 @@ struct max77620_rtc_softc {
 	int				bus_addr;
 };
 
+char max77620_rtc_compat[] = "maxim,max77620_rtc";
+
 /*
  * Raw register access function.
  */
@@ -304,12 +309,16 @@ max77620_rtc_settime(device_t dev, struct timespec *ts)
 static int
 max77620_rtc_probe(device_t dev)
 {
-	struct iicbus_ivar *dinfo;
-
-	dinfo = device_get_ivars(dev);
-	if (dinfo == NULL)
+	const char *compat;
+
+	/*
+	 * TODO:
+	 * ofw_bus_is_compatible() should use compat string from devinfo cache
+	 * maximum size of OFW property should be defined in public header
+	 */
+	if ((compat = ofw_bus_get_compat(dev)) == NULL)
 		return (ENXIO);
-	if (dinfo->addr != MAX77620_RTC_I2C_ADDR << 1)
+	if (strncasecmp(compat, max77620_rtc_compat, 255) != 0)
 		return (ENXIO);
 
 	device_set_desc(dev, "MAX77620 RTC");
@@ -356,10 +365,6 @@ fail:
 	return (rv);
 }
 
-/*
- * The secondary address of MAX77620 (RTC function) is not in DTB,
- * add it manualy
- */
 static int
 max77620_rtc_detach(device_t dev)
 {
@@ -371,25 +376,31 @@ max77620_rtc_detach(device_t dev)
 	return (bus_generic_detach(dev));
 }
 
+/*
+ * The secondary address of MAX77620 (RTC function) is not in DT,
+ * add it manualy as subdevice
+ */
 int
 max77620_rtc_create(struct max77620_softc *sc, phandle_t node)
 {
 	device_t parent, child;
-	struct iicbus_ivar *dinfo;
+	int rv;
 
 	parent = device_get_parent(sc->dev);
+
 	child = BUS_ADD_CHILD(parent, 0, NULL, -1);
-	if (child == 0)	{
-		device_printf(sc->dev, "Cannot add MAX77620 RTC device.\n");
+	if (child == NULL)	{
+		device_printf(sc->dev, "Cannot create MAX77620 RTC device.\n");
 		return (ENXIO);
 	}
-	dinfo = device_get_ivars(child);
-	if (dinfo == NULL)	{
-		device_printf(sc->dev,
-		    "Cannot set I2Caddress for MAX77620 RTC.\n");
+
+	rv = OFW_IICBUS_SET_DEVINFO(parent, child, -1, "rtc@68",
+	     max77620_rtc_compat, MAX77620_RTC_I2C_ADDR << 1);
+	if (rv != 0)	{
+		device_printf(sc->dev, "Cannot setup MAX77620 RTC device.\n");
 		return (ENXIO);
 	}
-	dinfo->addr = MAX77620_RTC_I2C_ADDR << 1;
+
 	return (0);
 }