git: f537b3056421 - stable/13 - icee: allow configuration via hints on FDT-based systems

From: Andriy Gapon <avg_at_FreeBSD.org>
Date: Sat, 27 Nov 2021 08:49:53 UTC
The branch stable/13 has been updated by avg:

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

commit f537b30564210708cea309de6e2ba54cf18e52b1
Author:     Andriy Gapon <avg@FreeBSD.org>
AuthorDate: 2021-11-04 11:56:22 +0000
Commit:     Andriy Gapon <avg@FreeBSD.org>
CommitDate: 2021-11-27 08:47:25 +0000

    icee: allow configuration via hints on FDT-based systems
    
    On-board devices should be configured via the FDT and overlays.
    Hints are primarily useful for external and temporarily attached devices.
    Adding hints is much easier and faster than writing and compiling
    an overlay.
    
    (cherry picked from commit 01e3492337cb48484e03be38340cc67ac5e30a5b)
---
 sys/dev/iicbus/icee.c | 64 +++++++++++++++++++++------------------------------
 1 file changed, 26 insertions(+), 38 deletions(-)

diff --git a/sys/dev/iicbus/icee.c b/sys/dev/iicbus/icee.c
index 19aff78bf45a..a658d73cb875 100644
--- a/sys/dev/iicbus/icee.c
+++ b/sys/dev/iicbus/icee.c
@@ -123,10 +123,10 @@ static struct cdevsw icee_cdevsw =
 	.d_write = icee_write
 };
 
-#ifdef FDT
 static int
 icee_probe(device_t dev)
 {
+#ifdef FDT
 	struct eeprom_desc *d;
 
 	if (!ofw_bus_status_okay(dev))
@@ -134,49 +134,42 @@ icee_probe(device_t dev)
 
 	d = (struct eeprom_desc *)
 	    ofw_bus_search_compatible(dev, compat_data)->ocd_data;
-	if (d == NULL)
-		return (ENXIO);
-
-	device_set_desc(dev, d->name);
-	return (BUS_PROBE_DEFAULT);
-}
-
-static void
-icee_init(struct icee_softc *sc)
-{
-	struct eeprom_desc *d;
-
-	d = (struct eeprom_desc *)
-	    ofw_bus_search_compatible(sc->dev, compat_data)->ocd_data;
-	if (d == NULL)
-		return; /* attach will see sc->size == 0 and return error */
-
-	sc->size  = d->size;
-	sc->type  = d->type;
-	sc->wr_sz = d->wr_sz;
-}
-#else /* !FDT */
-static int
-icee_probe(device_t dev)
-{
-
+	if (d != NULL) {
+		device_set_desc(dev, d->name);
+		return (BUS_PROBE_DEFAULT);
+	}
+#endif
 	device_set_desc(dev, "I2C EEPROM");
 	return (BUS_PROBE_NOWILDCARD);
 }
 
-static void
+static int
 icee_init(struct icee_softc *sc)
 {
 	const char *dname;
 	int dunit;
+#ifdef FDT
+	struct eeprom_desc *d;
 
+	d = (struct eeprom_desc *)
+	    ofw_bus_search_compatible(sc->dev, compat_data)->ocd_data;
+	if (d != NULL) {
+		sc->size  = d->size;
+		sc->type  = d->type;
+		sc->wr_sz = d->wr_sz;
+		return (0);
+	}
+#endif
 	dname = device_get_name(sc->dev);
 	dunit = device_get_unit(sc->dev);
-	resource_int_value(dname, dunit, "size", &sc->size);
-	resource_int_value(dname, dunit, "type", &sc->type);
-	resource_int_value(dname, dunit, "wr_sz", &sc->wr_sz);
+	if (resource_int_value(dname, dunit, "type", &sc->type) != 0)
+		return (ENOENT);
+	if (resource_int_value(dname, dunit, "size", &sc->size) != 0)
+		return (ENOENT);
+	if (resource_int_value(dname, dunit, "wr_sz", &sc->wr_sz) != 0)
+		return (ENOENT);
+	return (0);
 }
-#endif /* FDT */
 
 static int
 icee_attach(device_t dev)
@@ -187,13 +180,8 @@ icee_attach(device_t dev)
 
 	sc->dev = dev;
 	sc->addr = iicbus_get_addr(dev);
-	icee_init(sc);
-	if (sc->size == 0 || sc->type == 0 || sc->wr_sz == 0) {
-		device_printf(sc->dev, "Missing config data, "
-		    "these cannot be zero: size %d type %d wr_sz %d\n",
-		    sc->size, sc->type, sc->wr_sz);
+	if (icee_init(sc) != 0)
 		return (EINVAL);
-	}
 	if (bootverbose)
 		device_printf(dev, "size: %d bytes, addressing: %d-bits\n",
 		    sc->size, sc->type);