2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2019 Ian Lepore <ian@freebsd.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include "opt_platform.h"
33 #include <sys/param.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
37 #include <sys/systm.h>
39 #include <dev/iicbus/iicbus.h>
40 #include <dev/iicbus/iiconf.h>
41 #include "iicbus_if.h"
42 #include "iicmux_if.h"
44 static struct chip_info {
46 const char *description;
49 {"ltc4305", "LTC4305 I2C Mux", 2},
50 {"ltc4306", "LTC4306 I2C Mux", 4},
52 #define CHIP_NONE (-1)
53 #define CHIP_LTC4305 0
54 #define CHIP_LTC4306 1
57 #include <dev/ofw/ofw_bus.h>
58 #include <dev/ofw/ofw_bus_subr.h>
59 #include <dev/ofw/openfirm.h>
61 static struct ofw_compat_data compat_data[] = {
62 {"lltc,ltc4305", CHIP_LTC4305},
63 {"lltc,ltc4306", CHIP_LTC4306},
66 IICBUS_FDT_PNP_INFO(compat_data);
69 #include <dev/iicbus/mux/iicmux.h>
71 struct ltc430x_softc {
72 struct iicmux_softc mux;
77 * The datasheet doesn't name the registers, it calls them control register 0-3.
79 #define LTC430X_CTLREG_0 0
80 #define LTC430X_CTLREG_1 1
81 #define LTC430X_CTLREG_2 2
82 #define LTC430X_CR2_ENABLE_MW (1u << 3) /* Enable mass write address. */
83 #define LTC430X_CTLREG_3 3
86 ltc430x_bus_select(device_t dev, int busidx, struct iic_reqbus_data *rd)
88 struct ltc430x_softc *sc = device_get_softc(dev);
92 * The iicmux caller ensures busidx is between 0 and the number of buses
93 * we passed to iicmux_init_softc(), no need for validation here. If
94 * the fdt data has the idle_disconnect property we idle the bus by
95 * selecting no downstream buses, otherwise we just leave the current
96 * bus active. The upper bits of control register 3 activate the
97 * downstream buses; bit 7 is the first bus, bit 6 the second, etc.
99 if (busidx == IICMUX_SELECT_IDLE) {
100 if (sc->idle_disconnect)
105 busbits = 1u << (7 - busidx);
109 * We have to add the IIC_RECURSIVE flag because the iicmux core has
110 * already reserved the bus for us, and iicdev_writeto() is going to try
111 * to reserve it again, which is allowed with the recursive flag.
113 return (iicdev_writeto(dev, LTC430X_CTLREG_3, &busbits, sizeof(busbits),
114 rd->flags | IIC_RECURSIVE));
118 ltc430x_find_chiptype(device_t dev)
121 return (ofw_bus_search_compatible(dev, compat_data)->ocd_data);
126 if (resource_string_value(device_get_name(dev), device_get_unit(dev),
127 "chip_type", &type) == 0) {
128 for (i = 0; i < nitems(chip_infos); ++i) {
129 if (strcasecmp(type, chip_infos[i].partname) == 0) {
139 ltc430x_probe(device_t dev)
144 if (!ofw_bus_status_okay(dev))
148 type = ltc430x_find_chiptype(dev);
149 if (type == CHIP_NONE)
152 device_set_desc(dev, chip_infos[type].description);
154 return (BUS_PROBE_DEFAULT);
158 ltc430x_attach(device_t dev)
160 struct ltc430x_softc *sc __unused;
161 int chip, err, numchan, val;
162 uint8_t busbits, ctlreg2;
164 sc = device_get_softc(dev);
167 ctlreg2 = LTC430X_CR2_ENABLE_MW;
170 * Check for the idle-disconnect and ctlreg2 options, first in FDT data,
171 * then allow them to be overriden by hints data.
176 node = ofw_bus_get_node(dev);
177 sc->idle_disconnect = OF_hasprop(node, "i2c-mux-idle-disconnect");
179 if (OF_getprop(node, "freebsd,ctlreg2", &val, sizeof(val)) > 0) {
184 if (resource_int_value(device_get_name(dev), device_get_unit(dev),
185 "idle_disconnect", &val) == 0) {
186 sc->idle_disconnect = val;
189 if (resource_int_value(device_get_name(dev), device_get_unit(dev),
190 "ctlreg2", &val) == 0) {
194 /* We found the chip type when probing, so now it "can't fail". */
195 if ((chip = ltc430x_find_chiptype(dev)) == CHIP_NONE) {
196 device_printf(dev, "impossible: can't identify chip type\n");
199 numchan = chip_infos[chip].numchannels;
201 /* Set control reg 2 with configured (or default) values. */
202 iicdev_writeto(dev, LTC430X_CTLREG_2, &ctlreg2, sizeof(ctlreg2),
205 /* If configured for idle-disconnect, ensure we start disconnected. */
206 if (sc->idle_disconnect) {
207 iicdev_writeto(dev, LTC430X_CTLREG_3, &busbits, sizeof(busbits),
212 * Invoke the iicmux framework's attach code, and if it succeeds, invoke
213 * the probe and attach code of any child iicbus instances it added.
215 if ((err = iicmux_attach(dev, device_get_parent(dev), numchan)) == 0)
216 bus_generic_attach(dev);
222 ltc430x_detach(device_t dev)
226 if ((err = iicmux_detach(dev)) != 0)
232 static device_method_t ltc430x_methods[] = {
234 DEVMETHOD(device_probe, ltc430x_probe),
235 DEVMETHOD(device_attach, ltc430x_attach),
236 DEVMETHOD(device_detach, ltc430x_detach),
239 DEVMETHOD(iicmux_bus_select, ltc430x_bus_select),
244 static devclass_t ltc430x_devclass;
246 DEFINE_CLASS_1(ltc430x, ltc430x_driver, ltc430x_methods,
247 sizeof(struct ltc430x_softc), iicmux_driver);
248 DRIVER_MODULE(ltc430x, iicbus, ltc430x_driver, ltc430x_devclass, 0, 0);
251 DRIVER_MODULE(ofw_iicbus, ltc430x, ofw_iicbus_driver, ofw_iicbus_devclass, 0, 0);
253 DRIVER_MODULE(iicbus, ltc430x, iicbus_driver, iicbus_devclass, 0, 0);
256 MODULE_DEPEND(ltc430x, iicmux, 1, 1, 1);
257 MODULE_DEPEND(ltc430x, iicbus, 1, 1, 1);