xref: /freebsd/sys/dev/iicbus/iicbus.c (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 1998, 2001 Nicolas Souchu
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 /*
31  * Autoconfiguration and support routines for the Philips serial I2C bus
32  */
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/bus.h>
38 #include <sys/lock.h>
39 #include <sys/malloc.h>
40 #include <sys/module.h>
41 #include <sys/mutex.h>
42 #include <sys/rman.h>
43 #include <sys/sbuf.h>
44 #include <sys/sysctl.h>
45 
46 #include <dev/iicbus/iiconf.h>
47 #include <dev/iicbus/iicbus.h>
48 
49 #include "iicbus_if.h"
50 
51 /* See comments below for why auto-scanning is a bad idea. */
52 #define SCAN_IICBUS 0
53 
54 SYSCTL_NODE(_hw, OID_AUTO, i2c, CTLFLAG_RW, 0, "i2c controls");
55 
56 static int
57 iicbus_probe(device_t dev)
58 {
59 
60 	device_set_desc(dev, "Philips I2C bus");
61 
62 	/* Allow other subclasses to override this driver. */
63 	return (BUS_PROBE_GENERIC);
64 }
65 
66 #if SCAN_IICBUS
67 static int
68 iic_probe_device(device_t dev, u_char addr)
69 {
70 	int count;
71 	char byte;
72 
73 	if ((addr & 1) == 0) {
74 		/* is device writable? */
75 		if (!iicbus_start(dev, (u_char)addr, 0)) {
76 			iicbus_stop(dev);
77 			return (1);
78 		}
79 	} else {
80 		/* is device readable? */
81 		if (!iicbus_block_read(dev, (u_char)addr, &byte, 1, &count))
82 			return (1);
83 	}
84 
85 	return (0);
86 }
87 #endif
88 
89 /*
90  * We add all the devices which we know about.
91  * The generic attach routine will attach them if they are alive.
92  */
93 int
94 iicbus_attach_common(device_t dev, u_int bus_freq)
95 {
96 #if SCAN_IICBUS
97 	unsigned char addr;
98 #endif
99 	struct iicbus_softc *sc = IICBUS_SOFTC(dev);
100 	int strict;
101 
102 	sc->dev = dev;
103 	mtx_init(&sc->lock, "iicbus", NULL, MTX_DEF);
104 	iicbus_init_frequency(dev, bus_freq);
105 	iicbus_reset(dev, IIC_FASTEST, 0, NULL);
106 	if (resource_int_value(device_get_name(dev),
107 		device_get_unit(dev), "strict", &strict) == 0)
108 		sc->strict = strict;
109 	else
110 		sc->strict = 1;
111 
112 	/* device probing is meaningless since the bus is supposed to be
113 	 * hot-plug. Moreover, some I2C chips do not appreciate random
114 	 * accesses like stop after start to fast, reads for less than
115 	 * x bytes...
116 	 */
117 #if SCAN_IICBUS
118 	printf("Probing for devices on iicbus%d:", device_get_unit(dev));
119 
120 	/* probe any devices */
121 	for (addr = 16; addr < 240; addr++) {
122 		if (iic_probe_device(dev, (u_char)addr)) {
123 			printf(" <%x>", addr);
124 		}
125 	}
126 	printf("\n");
127 #endif
128 	bus_generic_probe(dev);
129 	bus_enumerate_hinted_children(dev);
130 	bus_generic_attach(dev);
131         return (0);
132 }
133 
134 static int
135 iicbus_attach(device_t dev)
136 {
137 
138 	return (iicbus_attach_common(dev, 0));
139 }
140 
141 int
142 iicbus_detach(device_t dev)
143 {
144 	struct iicbus_softc *sc = IICBUS_SOFTC(dev);
145 	int err;
146 
147 	if ((err = device_delete_children(dev)) != 0)
148 		return (err);
149 	iicbus_reset(dev, IIC_FASTEST, 0, NULL);
150 	mtx_destroy(&sc->lock);
151 	return (0);
152 }
153 
154 static int
155 iicbus_print_child(device_t dev, device_t child)
156 {
157 	struct iicbus_ivar *devi = IICBUS_IVAR(child);
158 	int retval = 0;
159 
160 	retval += bus_print_child_header(dev, child);
161 	if (devi->addr != 0)
162 		retval += printf(" at addr %#x", devi->addr);
163 	resource_list_print_type(&devi->rl, "irq", SYS_RES_IRQ, "%jd");
164 	retval += bus_print_child_footer(dev, child);
165 
166 	return (retval);
167 }
168 
169 void
170 iicbus_probe_nomatch(device_t bus, device_t child)
171 {
172 	struct iicbus_ivar *devi = IICBUS_IVAR(child);
173 
174 	device_printf(bus, "<unknown card> at addr %#x\n", devi->addr);
175 }
176 
177 int
178 iicbus_child_location(device_t bus, device_t child, struct sbuf *sb)
179 {
180 	struct iicbus_ivar *devi = IICBUS_IVAR(child);
181 
182 	sbuf_printf(sb, "addr=%#x", devi->addr);
183 	return (0);
184 }
185 
186 int
187 iicbus_child_pnpinfo(device_t bus, device_t child, struct sbuf *sb)
188 {
189 	return (0);
190 }
191 
192 int
193 iicbus_read_ivar(device_t bus, device_t child, int which, uintptr_t *result)
194 {
195 	struct iicbus_ivar *devi = IICBUS_IVAR(child);
196 
197 	switch (which) {
198 	default:
199 		return (EINVAL);
200 	case IICBUS_IVAR_ADDR:
201 		*result = devi->addr;
202 		break;
203 	}
204 	return (0);
205 }
206 
207 int
208 iicbus_write_ivar(device_t bus, device_t child, int which, uintptr_t value)
209 {
210 	struct iicbus_ivar *devi = IICBUS_IVAR(child);
211 
212 	switch (which) {
213 	default:
214 		return (EINVAL);
215 	case IICBUS_IVAR_ADDR:
216 		if (devi->addr != 0)
217 			return (EINVAL);
218 		devi->addr = value;
219 	}
220 	return (0);
221 }
222 
223 device_t
224 iicbus_add_child_common(device_t dev, u_int order, const char *name, int unit,
225     size_t ivars_size)
226 {
227 	device_t child;
228 	struct iicbus_ivar *devi;
229 
230 	child = device_add_child_ordered(dev, order, name, unit);
231 	if (child == NULL)
232 		return (child);
233 	devi = malloc(ivars_size, M_DEVBUF, M_NOWAIT | M_ZERO);
234 	if (devi == NULL) {
235 		device_delete_child(dev, child);
236 		return (0);
237 	}
238 	resource_list_init(&devi->rl);
239 	device_set_ivars(child, devi);
240 	return (child);
241 }
242 
243 static device_t
244 iicbus_add_child(device_t dev, u_int order, const char *name, int unit)
245 {
246 
247 	return (iicbus_add_child_common(
248 	    dev, order, name, unit, sizeof(struct iicbus_ivar)));
249 }
250 
251 static void
252 iicbus_hinted_child(device_t bus, const char *dname, int dunit)
253 {
254 	device_t child;
255 	int irq;
256 	struct iicbus_ivar *devi;
257 
258 	child = BUS_ADD_CHILD(bus, 0, dname, dunit);
259 	devi = IICBUS_IVAR(child);
260 	resource_int_value(dname, dunit, "addr", &devi->addr);
261 	if (resource_int_value(dname, dunit, "irq", &irq) == 0) {
262 		if (bus_set_resource(child, SYS_RES_IRQ, 0, irq, 1) != 0)
263 			device_printf(bus,
264 			    "warning: bus_set_resource() failed\n");
265 	}
266 }
267 
268 static struct resource_list *
269 iicbus_get_resource_list(device_t bus __unused, device_t child)
270 {
271 	struct iicbus_ivar *devi;
272 
273 	devi = IICBUS_IVAR(child);
274 	return (&devi->rl);
275 }
276 
277 int
278 iicbus_generic_intr(device_t dev, int event, char *buf)
279 {
280 
281 	return (0);
282 }
283 
284 int
285 iicbus_null_callback(device_t dev, int index, caddr_t data)
286 {
287 
288 	return (0);
289 }
290 
291 int
292 iicbus_null_repeated_start(device_t dev, u_char addr)
293 {
294 
295 	return (IIC_ENOTSUPP);
296 }
297 
298 void
299 iicbus_init_frequency(device_t dev, u_int bus_freq)
300 {
301 	struct iicbus_softc *sc = IICBUS_SOFTC(dev);
302 
303 	/*
304 	 * If a bus frequency value was passed in, use it.  Otherwise initialize
305 	 * it first to the standard i2c 100KHz frequency, then override that
306 	 * from a hint if one exists.
307 	 */
308 	if (bus_freq > 0)
309 		sc->bus_freq = bus_freq;
310 	else {
311 		sc->bus_freq = 100000;
312 		resource_int_value(device_get_name(dev), device_get_unit(dev),
313 		    "frequency", (int *)&sc->bus_freq);
314 	}
315 	/*
316 	 * Set up the sysctl that allows the bus frequency to be changed.
317 	 * It is flagged as a tunable so that the user can set the value in
318 	 * loader(8), and that will override any other setting from any source.
319 	 * The sysctl tunable/value is the one most directly controlled by the
320 	 * user and thus the one that always takes precedence.
321 	 */
322 	SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
323 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
324 	    OID_AUTO, "frequency", CTLFLAG_RWTUN, &sc->bus_freq,
325 	    sc->bus_freq, "Bus frequency in Hz");
326 }
327 
328 static u_int
329 iicbus_get_frequency(device_t dev, u_char speed)
330 {
331 	struct iicbus_softc *sc = IICBUS_SOFTC(dev);
332 
333 	/*
334 	 * If the frequency has not been configured for the bus, or the request
335 	 * is specifically for SLOW speed, use the standard 100KHz rate, else
336 	 * use the configured bus speed.
337 	 */
338 	if (sc->bus_freq == 0 || speed == IIC_SLOW)
339 		return (100000);
340 	return (sc->bus_freq);
341 }
342 
343 static device_method_t iicbus_methods[] = {
344 	/* device interface */
345 	DEVMETHOD(device_probe,		iicbus_probe),
346 	DEVMETHOD(device_attach,	iicbus_attach),
347 	DEVMETHOD(device_detach,	iicbus_detach),
348 	DEVMETHOD(device_suspend,	bus_generic_suspend),
349 	DEVMETHOD(device_resume,	bus_generic_resume),
350 
351 	/* bus interface */
352 	DEVMETHOD(bus_setup_intr,	bus_generic_setup_intr),
353 	DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
354 	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
355 	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
356 	DEVMETHOD(bus_adjust_resource,	bus_generic_adjust_resource),
357 	DEVMETHOD(bus_alloc_resource,	bus_generic_rl_alloc_resource),
358 	DEVMETHOD(bus_get_resource,	bus_generic_rl_get_resource),
359 	DEVMETHOD(bus_release_resource, bus_generic_rl_release_resource),
360 	DEVMETHOD(bus_set_resource,	bus_generic_rl_set_resource),
361 	DEVMETHOD(bus_get_resource_list, iicbus_get_resource_list),
362 	DEVMETHOD(bus_add_child,	iicbus_add_child),
363 	DEVMETHOD(bus_print_child,	iicbus_print_child),
364 	DEVMETHOD(bus_probe_nomatch,	iicbus_probe_nomatch),
365 	DEVMETHOD(bus_read_ivar,	iicbus_read_ivar),
366 	DEVMETHOD(bus_write_ivar,	iicbus_write_ivar),
367 	DEVMETHOD(bus_child_pnpinfo,	iicbus_child_pnpinfo),
368 	DEVMETHOD(bus_child_location,	iicbus_child_location),
369 	DEVMETHOD(bus_hinted_child,	iicbus_hinted_child),
370 
371 	/* iicbus interface */
372 	DEVMETHOD(iicbus_transfer,	iicbus_transfer),
373 	DEVMETHOD(iicbus_get_frequency,	iicbus_get_frequency),
374 
375 	DEVMETHOD_END
376 };
377 
378 driver_t iicbus_driver = {
379         "iicbus",
380         iicbus_methods,
381         sizeof(struct iicbus_softc),
382 };
383 
384 MODULE_VERSION(iicbus, IICBUS_MODVER);
385 DRIVER_MODULE(iicbus, iichb, iicbus_driver, 0, 0);
386