2 * Copyright (c) 1998, 2001 Nicolas Souchu
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
31 * Autoconfiguration and support routines for the Philips serial I2C bus
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <sys/module.h>
40 #include <sys/mutex.h>
41 #include <sys/sysctl.h>
44 #include <dev/iicbus/iiconf.h>
45 #include <dev/iicbus/iicbus.h>
47 #include "iicbus_if.h"
49 /* See comments below for why auto-scanning is a bad idea. */
53 iicbus_probe(device_t dev)
56 device_set_desc(dev, "Philips I2C bus");
58 /* Allow other subclasses to override this driver. */
59 return (BUS_PROBE_GENERIC);
64 iic_probe_device(device_t dev, u_char addr)
69 if ((addr & 1) == 0) {
70 /* is device writable? */
71 if (!iicbus_start(dev, (u_char)addr, 0)) {
76 /* is device readable? */
77 if (!iicbus_block_read(dev, (u_char)addr, &byte, 1, &count))
86 * We add all the devices which we know about.
87 * The generic attach routine will attach them if they are alive.
90 iicbus_attach(device_t dev)
95 struct iicbus_softc *sc = IICBUS_SOFTC(dev);
99 mtx_init(&sc->lock, "iicbus", NULL, MTX_DEF);
100 iicbus_init_frequency(dev, 0);
101 iicbus_reset(dev, IIC_FASTEST, 0, NULL);
102 if (resource_int_value(device_get_name(dev),
103 device_get_unit(dev), "strict", &strict) == 0)
108 /* device probing is meaningless since the bus is supposed to be
109 * hot-plug. Moreover, some I2C chips do not appreciate random
110 * accesses like stop after start to fast, reads for less than
114 printf("Probing for devices on iicbus%d:", device_get_unit(dev));
116 /* probe any devices */
117 for (addr = 16; addr < 240; addr++) {
118 if (iic_probe_device(dev, (u_char)addr)) {
119 printf(" <%x>", addr);
124 bus_generic_probe(dev);
125 bus_enumerate_hinted_children(dev);
126 bus_generic_attach(dev);
131 iicbus_detach(device_t dev)
133 struct iicbus_softc *sc = IICBUS_SOFTC(dev);
135 iicbus_reset(dev, IIC_FASTEST, 0, NULL);
136 bus_generic_detach(dev);
137 device_delete_children(dev);
138 mtx_destroy(&sc->lock);
143 iicbus_print_child(device_t dev, device_t child)
145 struct iicbus_ivar *devi = IICBUS_IVAR(child);
148 retval += bus_print_child_header(dev, child);
150 retval += printf(" at addr %#x", devi->addr);
151 retval += bus_print_child_footer(dev, child);
157 iicbus_probe_nomatch(device_t bus, device_t child)
159 struct iicbus_ivar *devi = IICBUS_IVAR(child);
161 device_printf(bus, "<unknown card>");
162 printf(" at addr %#x\n", devi->addr);
167 iicbus_child_location_str(device_t bus, device_t child, char *buf,
170 struct iicbus_ivar *devi = IICBUS_IVAR(child);
172 snprintf(buf, buflen, "addr=%#x", devi->addr);
177 iicbus_child_pnpinfo_str(device_t bus, device_t child, char *buf,
185 iicbus_read_ivar(device_t bus, device_t child, int which, uintptr_t *result)
187 struct iicbus_ivar *devi = IICBUS_IVAR(child);
192 case IICBUS_IVAR_ADDR:
193 *result = devi->addr;
200 iicbus_add_child(device_t dev, u_int order, const char *name, int unit)
203 struct iicbus_ivar *devi;
205 child = device_add_child_ordered(dev, order, name, unit);
208 devi = malloc(sizeof(struct iicbus_ivar), M_DEVBUF, M_NOWAIT | M_ZERO);
210 device_delete_child(dev, child);
213 device_set_ivars(child, devi);
218 iicbus_hinted_child(device_t bus, const char *dname, int dunit)
221 struct iicbus_ivar *devi;
223 child = BUS_ADD_CHILD(bus, 0, dname, dunit);
224 devi = IICBUS_IVAR(child);
225 resource_int_value(dname, dunit, "addr", &devi->addr);
229 iicbus_generic_intr(device_t dev, int event, char *buf)
236 iicbus_null_callback(device_t dev, int index, caddr_t data)
243 iicbus_null_repeated_start(device_t dev, u_char addr)
246 return (IIC_ENOTSUPP);
250 iicbus_init_frequency(device_t dev, u_int bus_freq)
252 struct iicbus_softc *sc = IICBUS_SOFTC(dev);
255 * If a bus frequency value was passed in, use it. Otherwise initialize
256 * it first to the standard i2c 100KHz frequency, then override that
257 * from a hint if one exists.
260 sc->bus_freq = bus_freq;
262 sc->bus_freq = 100000;
263 resource_int_value(device_get_name(dev), device_get_unit(dev),
264 "frequency", (int *)&sc->bus_freq);
267 * Set up the sysctl that allows the bus frequency to be changed.
268 * It is flagged as a tunable so that the user can set the value in
269 * loader(8), and that will override any other setting from any source.
270 * The sysctl tunable/value is the one most directly controlled by the
271 * user and thus the one that always takes precedence.
273 SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
274 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
275 OID_AUTO, "frequency", CTLFLAG_RW | CTLFLAG_TUN, &sc->bus_freq,
276 sc->bus_freq, "Bus frequency in Hz");
280 iicbus_get_frequency(device_t dev, u_char speed)
282 struct iicbus_softc *sc = IICBUS_SOFTC(dev);
285 * If the frequency has not been configured for the bus, or the request
286 * is specifically for SLOW speed, use the standard 100KHz rate, else
287 * use the configured bus speed.
289 if (sc->bus_freq == 0 || speed == IIC_SLOW)
291 return (sc->bus_freq);
294 static device_method_t iicbus_methods[] = {
295 /* device interface */
296 DEVMETHOD(device_probe, iicbus_probe),
297 DEVMETHOD(device_attach, iicbus_attach),
298 DEVMETHOD(device_detach, iicbus_detach),
301 DEVMETHOD(bus_add_child, iicbus_add_child),
302 DEVMETHOD(bus_print_child, iicbus_print_child),
303 DEVMETHOD(bus_probe_nomatch, iicbus_probe_nomatch),
304 DEVMETHOD(bus_read_ivar, iicbus_read_ivar),
305 DEVMETHOD(bus_child_pnpinfo_str, iicbus_child_pnpinfo_str),
306 DEVMETHOD(bus_child_location_str, iicbus_child_location_str),
307 DEVMETHOD(bus_hinted_child, iicbus_hinted_child),
309 /* iicbus interface */
310 DEVMETHOD(iicbus_transfer, iicbus_transfer),
311 DEVMETHOD(iicbus_get_frequency, iicbus_get_frequency),
316 driver_t iicbus_driver = {
319 sizeof(struct iicbus_softc),
322 devclass_t iicbus_devclass;
324 MODULE_VERSION(iicbus, IICBUS_MODVER);
325 DRIVER_MODULE(iicbus, iichb, iicbus_driver, iicbus_devclass, 0, 0);