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>
43 #include <dev/iicbus/iiconf.h>
44 #include <dev/iicbus/iicbus.h>
46 #include "iicbus_if.h"
48 /* See comments below for why auto-scanning is a bad idea. */
52 iicbus_probe(device_t dev)
55 device_set_desc(dev, "Philips I2C bus");
57 /* Allow other subclasses to override this driver. */
58 return (BUS_PROBE_GENERIC);
63 iic_probe_device(device_t dev, u_char addr)
68 if ((addr & 1) == 0) {
69 /* is device writable? */
70 if (!iicbus_start(dev, (u_char)addr, 0)) {
75 /* is device readable? */
76 if (!iicbus_block_read(dev, (u_char)addr, &byte, 1, &count))
85 * We add all the devices which we know about.
86 * The generic attach routine will attach them if they are alive.
89 iicbus_attach(device_t dev)
94 struct iicbus_softc *sc = IICBUS_SOFTC(dev);
97 mtx_init(&sc->lock, "iicbus", NULL, MTX_DEF);
98 iicbus_reset(dev, IIC_FASTEST, 0, NULL);
100 /* device probing is meaningless since the bus is supposed to be
101 * hot-plug. Moreover, some I2C chips do not appreciate random
102 * accesses like stop after start to fast, reads for less than
106 printf("Probing for devices on iicbus%d:", device_get_unit(dev));
108 /* probe any devices */
109 for (addr = 16; addr < 240; addr++) {
110 if (iic_probe_device(dev, (u_char)addr)) {
111 printf(" <%x>", addr);
116 bus_generic_probe(dev);
117 bus_enumerate_hinted_children(dev);
118 bus_generic_attach(dev);
123 iicbus_detach(device_t dev)
125 struct iicbus_softc *sc = IICBUS_SOFTC(dev);
127 iicbus_reset(dev, IIC_FASTEST, 0, NULL);
128 bus_generic_detach(dev);
129 mtx_destroy(&sc->lock);
134 iicbus_print_child(device_t dev, device_t child)
136 struct iicbus_ivar *devi = IICBUS_IVAR(child);
139 retval += bus_print_child_header(dev, child);
141 retval += printf(" at addr %#x", devi->addr);
142 retval += bus_print_child_footer(dev, child);
148 iicbus_probe_nomatch(device_t bus, device_t child)
150 struct iicbus_ivar *devi = IICBUS_IVAR(child);
152 device_printf(bus, "<unknown card>");
153 printf(" at addr %#x\n", devi->addr);
158 iicbus_child_location_str(device_t bus, device_t child, char *buf,
161 struct iicbus_ivar *devi = IICBUS_IVAR(child);
163 snprintf(buf, buflen, "addr=%#x", devi->addr);
168 iicbus_child_pnpinfo_str(device_t bus, device_t child, char *buf,
176 iicbus_read_ivar(device_t bus, device_t child, int which, uintptr_t *result)
178 struct iicbus_ivar *devi = IICBUS_IVAR(child);
183 case IICBUS_IVAR_ADDR:
184 *result = devi->addr;
191 iicbus_add_child(device_t dev, u_int order, const char *name, int unit)
194 struct iicbus_ivar *devi;
196 child = device_add_child_ordered(dev, order, name, unit);
199 devi = malloc(sizeof(struct iicbus_ivar), M_DEVBUF, M_NOWAIT | M_ZERO);
201 device_delete_child(dev, child);
204 device_set_ivars(child, devi);
209 iicbus_hinted_child(device_t bus, const char *dname, int dunit)
212 struct iicbus_ivar *devi;
214 child = BUS_ADD_CHILD(bus, 0, dname, dunit);
215 devi = IICBUS_IVAR(child);
216 resource_int_value(dname, dunit, "addr", &devi->addr);
220 iicbus_generic_intr(device_t dev, int event, char *buf)
227 iicbus_null_callback(device_t dev, int index, caddr_t data)
234 iicbus_null_repeated_start(device_t dev, u_char addr)
237 return (IIC_ENOTSUPP);
240 static device_method_t iicbus_methods[] = {
241 /* device interface */
242 DEVMETHOD(device_probe, iicbus_probe),
243 DEVMETHOD(device_attach, iicbus_attach),
244 DEVMETHOD(device_detach, iicbus_detach),
247 DEVMETHOD(bus_add_child, iicbus_add_child),
248 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
249 DEVMETHOD(bus_print_child, iicbus_print_child),
250 DEVMETHOD(bus_probe_nomatch, iicbus_probe_nomatch),
251 DEVMETHOD(bus_read_ivar, iicbus_read_ivar),
252 DEVMETHOD(bus_child_pnpinfo_str, iicbus_child_pnpinfo_str),
253 DEVMETHOD(bus_child_location_str, iicbus_child_location_str),
254 DEVMETHOD(bus_hinted_child, iicbus_hinted_child),
256 /* iicbus interface */
257 DEVMETHOD(iicbus_transfer, iicbus_transfer),
262 driver_t iicbus_driver = {
265 sizeof(struct iicbus_softc),
268 devclass_t iicbus_devclass;
270 MODULE_VERSION(iicbus, IICBUS_MODVER);
271 DRIVER_MODULE(iicbus, iichb, iicbus_driver, iicbus_devclass, 0, 0);