xref: /freebsd/sys/dev/iicbus/mux/ltc430x.c (revision e17f5b1d)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2019 Ian Lepore <ian@freebsd.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
14  *
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
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include "opt_platform.h"
32 
33 #include <sys/param.h>
34 #include <sys/bus.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
37 #include <sys/systm.h>
38 
39 #include <dev/iicbus/iicbus.h>
40 #include <dev/iicbus/iiconf.h>
41 #include "iicbus_if.h"
42 #include "iicmux_if.h"
43 
44 static struct chip_info {
45 	const char 	*partname;
46 	const char	*description;
47 	int		numchannels;
48 } chip_infos[] = {
49 	{"ltc4305", "LTC4305 I2C Mux", 2},
50 	{"ltc4306", "LTC4306 I2C Mux", 4},
51 };
52 #define CHIP_NONE	(-1)
53 #define	CHIP_LTC4305	0
54 #define	CHIP_LTC4306	1
55 
56 #ifdef FDT
57 #include <dev/ofw/ofw_bus.h>
58 #include <dev/ofw/ofw_bus_subr.h>
59 #include <dev/ofw/openfirm.h>
60 
61 static struct ofw_compat_data compat_data[] = {
62 	{"lltc,ltc4305",  CHIP_LTC4305},
63 	{"lltc,ltc4306",  CHIP_LTC4306},
64 	{NULL,            CHIP_NONE}
65 };
66 IICBUS_FDT_PNP_INFO(compat_data);
67 #endif
68 
69 #include <dev/iicbus/mux/iicmux.h>
70 
71 struct ltc430x_softc {
72 	struct iicmux_softc mux;
73 	bool idle_disconnect;
74 };
75 
76 /*
77  * The datasheet doesn't name the registers, it calls them control register 0-3.
78  */
79 #define	LTC430X_CTLREG_0	0
80 #define	LTC430X_CTLREG_1	1
81 #define	LTC430X_CTLREG_2	2
82 #define	LTC430X_CTLREG_3	3
83 
84 static int
85 ltc430x_bus_select(device_t dev, int busidx, struct iic_reqbus_data *rd)
86 {
87 	struct ltc430x_softc *sc = device_get_softc(dev);
88 	uint8_t busbits;
89 
90 	/*
91 	 * The iicmux caller ensures busidx is between 0 and the number of buses
92 	 * we passed to iicmux_init_softc(), no need for validation here.  If
93 	 * the fdt data has the idle_disconnect property we idle the bus by
94 	 * selecting no downstream buses, otherwise we just leave the current
95 	 * bus active.  The upper bits of control register 3 activate the
96 	 * downstream buses; bit 7 is the first bus, bit 6 the second, etc.
97 	 */
98 	if (busidx == IICMUX_SELECT_IDLE) {
99 		if (sc->idle_disconnect)
100 			busbits = 0;
101 		else
102 			return (0);
103 	} else {
104 		busbits = 1u << (7 - busidx);
105 	}
106 
107 	/*
108 	 * We have to add the IIC_RECURSIVE flag because the iicmux core has
109 	 * already reserved the bus for us, and iicdev_writeto() is going to try
110 	 * to reserve it again, which is allowed with the recursive flag.
111 	 */
112 	return (iicdev_writeto(dev, LTC430X_CTLREG_3, &busbits, sizeof(busbits),
113 	    rd->flags | IIC_RECURSIVE));
114 }
115 
116 static int
117 ltc430x_find_chiptype(device_t dev)
118 {
119 #ifdef FDT
120 	return (ofw_bus_search_compatible(dev, compat_data)->ocd_data);
121 #else
122 	const char *type;
123 	int i;
124 
125 	if (resource_string_value(device_get_name(dev), device_get_unit(dev),
126 	    "chip_type", &type) == 0) {
127 		for (i = 0; i < nitems(chip_infos); ++i) {
128 			if (strcasecmp(type, chip_infos[i].partname) == 0) {
129 				return (i);
130 			}
131 		}
132 	}
133 	return (CHIP_NONE);
134 #endif
135 }
136 
137 static int
138 ltc430x_probe(device_t dev)
139 {
140 	int type;
141 
142 #ifdef FDT
143 	if (!ofw_bus_status_okay(dev))
144 		return (ENXIO);
145 #endif
146 
147 	type = ltc430x_find_chiptype(dev);
148 	if (type == CHIP_NONE)
149 		return (ENXIO);
150 
151 	device_set_desc(dev, chip_infos[type].description);
152 
153 	return (BUS_PROBE_DEFAULT);
154 }
155 
156 static int
157 ltc430x_attach(device_t dev)
158 {
159 	struct ltc430x_softc *sc __unused;
160 	int chip, err, numchan;
161 
162 	sc = device_get_softc(dev);
163 
164 #ifdef FDT
165 	phandle_t node;
166 
167 	node = ofw_bus_get_node(dev);
168 	sc->idle_disconnect = OF_hasprop(node, "i2c-mux-idle-disconnect");
169 #endif
170 
171 	/* We found the chip type when probing, so now it "can't fail". */
172 	if ((chip = ltc430x_find_chiptype(dev)) == CHIP_NONE) {
173 		device_printf(dev, "impossible: can't identify chip type\n");
174 		return (ENXIO);
175 	}
176 	numchan = chip_infos[chip].numchannels;
177 
178 	if ((err = iicmux_attach(dev, device_get_parent(dev), numchan)) == 0)
179                 bus_generic_attach(dev);
180 
181 	return (err);
182 }
183 
184 static int
185 ltc430x_detach(device_t dev)
186 {
187 	int err;
188 
189 	if ((err = iicmux_detach(dev)) != 0)
190 		return (err);
191 
192 	return (0);
193 }
194 
195 static device_method_t ltc430x_methods[] = {
196 	/* device methods */
197 	DEVMETHOD(device_probe,			ltc430x_probe),
198 	DEVMETHOD(device_attach,		ltc430x_attach),
199 	DEVMETHOD(device_detach,		ltc430x_detach),
200 
201 	/* iicmux methods */
202 	DEVMETHOD(iicmux_bus_select,		ltc430x_bus_select),
203 
204 	DEVMETHOD_END
205 };
206 
207 static devclass_t ltc430x_devclass;
208 
209 DEFINE_CLASS_1(ltc430x, ltc430x_driver, ltc430x_methods,
210     sizeof(struct ltc430x_softc), iicmux_driver);
211 DRIVER_MODULE(ltc430x, iicbus, ltc430x_driver, ltc430x_devclass, 0, 0);
212 
213 #ifdef FDT
214 DRIVER_MODULE(ofw_iicbus, ltc430x, ofw_iicbus_driver, ofw_iicbus_devclass, 0, 0);
215 #else
216 DRIVER_MODULE(iicbus, ltc430x, iicbus_driver, iicbus_devclass, 0, 0);
217 #endif
218 
219 MODULE_DEPEND(ltc430x, iicmux, 1, 1, 1);
220 MODULE_DEPEND(ltc430x, iicbus, 1, 1, 1);
221 
222