xref: /freebsd/sys/dev/iicbus/mux/ltc430x.c (revision d0b2dbfa0ecf2bbc9709efc5e20baf8e4b44bbbf)
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 #include "opt_platform.h"
30 
31 #include <sys/param.h>
32 #include <sys/bus.h>
33 #include <sys/kernel.h>
34 #include <sys/module.h>
35 #include <sys/systm.h>
36 
37 #include <dev/iicbus/iicbus.h>
38 #include <dev/iicbus/iiconf.h>
39 #include "iicbus_if.h"
40 #include "iicmux_if.h"
41 
42 static struct chip_info {
43 	const char 	*partname;
44 	const char	*description;
45 	int		numchannels;
46 } chip_infos[] = {
47 	{"ltc4305", "LTC4305 I2C Mux", 2},
48 	{"ltc4306", "LTC4306 I2C Mux", 4},
49 };
50 #define CHIP_NONE	(-1)
51 #define	CHIP_LTC4305	0
52 #define	CHIP_LTC4306	1
53 
54 #ifdef FDT
55 #include <dev/ofw/ofw_bus.h>
56 #include <dev/ofw/ofw_bus_subr.h>
57 #include <dev/ofw/openfirm.h>
58 
59 static struct ofw_compat_data compat_data[] = {
60 	{"lltc,ltc4305",  CHIP_LTC4305},
61 	{"lltc,ltc4306",  CHIP_LTC4306},
62 	{NULL,            CHIP_NONE}
63 };
64 IICBUS_FDT_PNP_INFO(compat_data);
65 #endif
66 
67 #include <dev/iicbus/mux/iicmux.h>
68 
69 struct ltc430x_softc {
70 	struct iicmux_softc mux;
71 	bool idle_disconnect;
72 };
73 
74 /*
75  * The datasheet doesn't name the registers, it calls them control register 0-3.
76  */
77 #define	LTC430X_CTLREG_0	0
78 #define	LTC430X_CTLREG_1	1
79 #define	LTC430X_CTLREG_2	2
80 #define	  LTC430X_CR2_ENABLE_MW	  (1u << 3) /* Enable mass write address. */
81 #define	LTC430X_CTLREG_3	3
82 
83 static int
84 ltc430x_bus_select(device_t dev, int busidx, struct iic_reqbus_data *rd)
85 {
86 	struct ltc430x_softc *sc = device_get_softc(dev);
87 	uint8_t busbits;
88 
89 	/*
90 	 * The iicmux caller ensures busidx is between 0 and the number of buses
91 	 * we passed to iicmux_init_softc(), no need for validation here.  If
92 	 * the fdt data has the idle_disconnect property we idle the bus by
93 	 * selecting no downstream buses, otherwise we just leave the current
94 	 * bus active.  The upper bits of control register 3 activate the
95 	 * downstream buses; bit 7 is the first bus, bit 6 the second, etc.
96 	 */
97 	if (busidx == IICMUX_SELECT_IDLE) {
98 		if (sc->idle_disconnect)
99 			busbits = 0;
100 		else
101 			return (0);
102 	} else {
103 		busbits = 1u << (7 - busidx);
104 	}
105 
106 	/*
107 	 * We have to add the IIC_RECURSIVE flag because the iicmux core has
108 	 * already reserved the bus for us, and iicdev_writeto() is going to try
109 	 * to reserve it again, which is allowed with the recursive flag.
110 	 */
111 	return (iicdev_writeto(dev, LTC430X_CTLREG_3, &busbits, sizeof(busbits),
112 	    rd->flags | IIC_RECURSIVE));
113 }
114 
115 static int
116 ltc430x_find_chiptype(device_t dev)
117 {
118 #ifdef FDT
119 	return (ofw_bus_search_compatible(dev, compat_data)->ocd_data);
120 #else
121 	const char *type;
122 	int i;
123 
124 	if (resource_string_value(device_get_name(dev), device_get_unit(dev),
125 	    "chip_type", &type) == 0) {
126 		for (i = 0; i < nitems(chip_infos); ++i) {
127 			if (strcasecmp(type, chip_infos[i].partname) == 0) {
128 				return (i);
129 			}
130 		}
131 	}
132 	return (CHIP_NONE);
133 #endif
134 }
135 
136 static int
137 ltc430x_probe(device_t dev)
138 {
139 	int type;
140 
141 #ifdef FDT
142 	if (!ofw_bus_status_okay(dev))
143 		return (ENXIO);
144 #endif
145 
146 	type = ltc430x_find_chiptype(dev);
147 	if (type == CHIP_NONE)
148 		return (ENXIO);
149 
150 	device_set_desc(dev, chip_infos[type].description);
151 
152 	return (BUS_PROBE_DEFAULT);
153 }
154 
155 static int
156 ltc430x_attach(device_t dev)
157 {
158 	struct ltc430x_softc *sc __unused;
159 	int chip, err, numchan, val;
160 	uint8_t busbits, ctlreg2;
161 
162 	sc = device_get_softc(dev);
163 
164 	busbits = 0;
165 	ctlreg2 = LTC430X_CR2_ENABLE_MW;
166 
167 	/*
168 	 * Check for the idle-disconnect and ctlreg2 options, first in FDT data,
169 	 * then allow them to be overridden by hints data.
170 	 */
171 #ifdef FDT
172 	phandle_t node;
173 
174 	node = ofw_bus_get_node(dev);
175 	sc->idle_disconnect = OF_hasprop(node, "i2c-mux-idle-disconnect");
176 
177 	if (OF_getprop(node, "freebsd,ctlreg2", &val, sizeof(val)) > 0) {
178 		ctlreg2 = val;
179 	}
180 #endif
181 
182 	if (resource_int_value(device_get_name(dev), device_get_unit(dev),
183 	    "idle_disconnect", &val) == 0) {
184 		sc->idle_disconnect = val;
185 	}
186 
187 	if (resource_int_value(device_get_name(dev), device_get_unit(dev),
188 	    "ctlreg2", &val) == 0) {
189 		ctlreg2 = val;
190 	}
191 
192 	/* We found the chip type when probing, so now it "can't fail". */
193 	if ((chip = ltc430x_find_chiptype(dev)) == CHIP_NONE) {
194 		device_printf(dev, "impossible: can't identify chip type\n");
195 		return (ENXIO);
196 	}
197 	numchan = chip_infos[chip].numchannels;
198 
199 	/* Set control reg 2 with configured (or default) values. */
200 	iicdev_writeto(dev, LTC430X_CTLREG_2, &ctlreg2, sizeof(ctlreg2),
201 	    IIC_WAIT);
202 
203 	/* If configured for idle-disconnect, ensure we start disconnected. */
204 	if (sc->idle_disconnect) {
205 		iicdev_writeto(dev, LTC430X_CTLREG_3, &busbits, sizeof(busbits),
206 		    IIC_WAIT);
207 	}
208 
209 	/*
210 	 * Invoke the iicmux framework's attach code, and if it succeeds, invoke
211 	 * the probe and attach code of any child iicbus instances it added.
212 	 */
213 	if ((err = iicmux_attach(dev, device_get_parent(dev), numchan)) == 0)
214 		bus_generic_attach(dev);
215 
216 	return (err);
217 }
218 
219 static int
220 ltc430x_detach(device_t dev)
221 {
222 	int err;
223 
224 	if ((err = iicmux_detach(dev)) != 0)
225 		return (err);
226 
227 	return (0);
228 }
229 
230 static device_method_t ltc430x_methods[] = {
231 	/* device methods */
232 	DEVMETHOD(device_probe,			ltc430x_probe),
233 	DEVMETHOD(device_attach,		ltc430x_attach),
234 	DEVMETHOD(device_detach,		ltc430x_detach),
235 
236 	/* iicmux methods */
237 	DEVMETHOD(iicmux_bus_select,		ltc430x_bus_select),
238 
239 	DEVMETHOD_END
240 };
241 
242 DEFINE_CLASS_1(ltc430x, ltc430x_driver, ltc430x_methods,
243     sizeof(struct ltc430x_softc), iicmux_driver);
244 DRIVER_MODULE(ltc430x, iicbus, ltc430x_driver, 0, 0);
245 
246 #ifdef FDT
247 DRIVER_MODULE(ofw_iicbus, ltc430x, ofw_iicbus_driver, 0, 0);
248 #else
249 DRIVER_MODULE(iicbus, ltc430x, iicbus_driver, 0, 0);
250 #endif
251 
252 MODULE_DEPEND(ltc430x, iicmux, 1, 1, 1);
253 MODULE_DEPEND(ltc430x, iicbus, 1, 1, 1);
254 
255