2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1998, 2001 Nicolas Souchu
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
33 * Autoconfiguration and support routines for the Philips serial I2C bus
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
40 #include <sys/malloc.h>
41 #include <sys/module.h>
42 #include <sys/mutex.h>
44 #include <sys/sysctl.h>
47 #include <dev/iicbus/iiconf.h>
48 #include <dev/iicbus/iicbus.h>
50 #include "iicbus_if.h"
52 /* See comments below for why auto-scanning is a bad idea. */
56 iicbus_probe(device_t dev)
59 device_set_desc(dev, "Philips I2C bus");
61 /* Allow other subclasses to override this driver. */
62 return (BUS_PROBE_GENERIC);
67 iic_probe_device(device_t dev, u_char addr)
72 if ((addr & 1) == 0) {
73 /* is device writable? */
74 if (!iicbus_start(dev, (u_char)addr, 0)) {
79 /* is device readable? */
80 if (!iicbus_block_read(dev, (u_char)addr, &byte, 1, &count))
89 * We add all the devices which we know about.
90 * The generic attach routine will attach them if they are alive.
93 iicbus_attach(device_t dev)
98 struct iicbus_softc *sc = IICBUS_SOFTC(dev);
102 mtx_init(&sc->lock, "iicbus", NULL, MTX_DEF);
103 iicbus_init_frequency(dev, 0);
104 iicbus_reset(dev, IIC_FASTEST, 0, NULL);
105 if (resource_int_value(device_get_name(dev),
106 device_get_unit(dev), "strict", &strict) == 0)
111 /* device probing is meaningless since the bus is supposed to be
112 * hot-plug. Moreover, some I2C chips do not appreciate random
113 * accesses like stop after start to fast, reads for less than
117 printf("Probing for devices on iicbus%d:", device_get_unit(dev));
119 /* probe any devices */
120 for (addr = 16; addr < 240; addr++) {
121 if (iic_probe_device(dev, (u_char)addr)) {
122 printf(" <%x>", addr);
127 bus_generic_probe(dev);
128 bus_enumerate_hinted_children(dev);
129 bus_generic_attach(dev);
134 iicbus_detach(device_t dev)
136 struct iicbus_softc *sc = IICBUS_SOFTC(dev);
139 if ((err = device_delete_children(dev)) != 0)
141 iicbus_reset(dev, IIC_FASTEST, 0, NULL);
142 mtx_destroy(&sc->lock);
147 iicbus_print_child(device_t dev, device_t child)
149 struct iicbus_ivar *devi = IICBUS_IVAR(child);
152 retval += bus_print_child_header(dev, child);
154 retval += printf(" at addr %#x", devi->addr);
155 resource_list_print_type(&devi->rl, "irq", SYS_RES_IRQ, "%jd");
156 retval += bus_print_child_footer(dev, child);
162 iicbus_probe_nomatch(device_t bus, device_t child)
164 struct iicbus_ivar *devi = IICBUS_IVAR(child);
166 device_printf(bus, "<unknown card> at addr %#x\n", devi->addr);
170 iicbus_child_location_str(device_t bus, device_t child, char *buf,
173 struct iicbus_ivar *devi = IICBUS_IVAR(child);
175 snprintf(buf, buflen, "addr=%#x", devi->addr);
180 iicbus_child_pnpinfo_str(device_t bus, device_t child, char *buf,
188 iicbus_read_ivar(device_t bus, device_t child, int which, uintptr_t *result)
190 struct iicbus_ivar *devi = IICBUS_IVAR(child);
195 case IICBUS_IVAR_ADDR:
196 *result = devi->addr;
203 iicbus_write_ivar(device_t bus, device_t child, int which, uintptr_t value)
205 struct iicbus_ivar *devi = IICBUS_IVAR(child);
210 case IICBUS_IVAR_ADDR:
219 iicbus_add_child(device_t dev, u_int order, const char *name, int unit)
222 struct iicbus_ivar *devi;
224 child = device_add_child_ordered(dev, order, name, unit);
227 devi = malloc(sizeof(struct iicbus_ivar), M_DEVBUF, M_NOWAIT | M_ZERO);
229 device_delete_child(dev, child);
232 resource_list_init(&devi->rl);
233 device_set_ivars(child, devi);
238 iicbus_hinted_child(device_t bus, const char *dname, int dunit)
242 struct iicbus_ivar *devi;
244 child = BUS_ADD_CHILD(bus, 0, dname, dunit);
245 devi = IICBUS_IVAR(child);
246 resource_int_value(dname, dunit, "addr", &devi->addr);
247 if (resource_int_value(dname, dunit, "irq", &irq) == 0) {
248 if (bus_set_resource(child, SYS_RES_IRQ, 0, irq, 1) != 0)
250 "warning: bus_set_resource() failed\n");
254 static struct resource_list *
255 iicbus_get_resource_list(device_t bus __unused, device_t child)
257 struct iicbus_ivar *devi;
259 devi = IICBUS_IVAR(child);
264 iicbus_generic_intr(device_t dev, int event, char *buf)
271 iicbus_null_callback(device_t dev, int index, caddr_t data)
278 iicbus_null_repeated_start(device_t dev, u_char addr)
281 return (IIC_ENOTSUPP);
285 iicbus_init_frequency(device_t dev, u_int bus_freq)
287 struct iicbus_softc *sc = IICBUS_SOFTC(dev);
290 * If a bus frequency value was passed in, use it. Otherwise initialize
291 * it first to the standard i2c 100KHz frequency, then override that
292 * from a hint if one exists.
295 sc->bus_freq = bus_freq;
297 sc->bus_freq = 100000;
298 resource_int_value(device_get_name(dev), device_get_unit(dev),
299 "frequency", (int *)&sc->bus_freq);
302 * Set up the sysctl that allows the bus frequency to be changed.
303 * It is flagged as a tunable so that the user can set the value in
304 * loader(8), and that will override any other setting from any source.
305 * The sysctl tunable/value is the one most directly controlled by the
306 * user and thus the one that always takes precedence.
308 SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
309 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
310 OID_AUTO, "frequency", CTLFLAG_RW | CTLFLAG_TUN, &sc->bus_freq,
311 sc->bus_freq, "Bus frequency in Hz");
315 iicbus_get_frequency(device_t dev, u_char speed)
317 struct iicbus_softc *sc = IICBUS_SOFTC(dev);
320 * If the frequency has not been configured for the bus, or the request
321 * is specifically for SLOW speed, use the standard 100KHz rate, else
322 * use the configured bus speed.
324 if (sc->bus_freq == 0 || speed == IIC_SLOW)
326 return (sc->bus_freq);
329 static device_method_t iicbus_methods[] = {
330 /* device interface */
331 DEVMETHOD(device_probe, iicbus_probe),
332 DEVMETHOD(device_attach, iicbus_attach),
333 DEVMETHOD(device_detach, iicbus_detach),
334 DEVMETHOD(device_suspend, bus_generic_suspend),
335 DEVMETHOD(device_resume, bus_generic_resume),
338 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
339 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
340 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
341 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
342 DEVMETHOD(bus_adjust_resource, bus_generic_adjust_resource),
343 DEVMETHOD(bus_alloc_resource, bus_generic_rl_alloc_resource),
344 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource),
345 DEVMETHOD(bus_release_resource, bus_generic_rl_release_resource),
346 DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource),
347 DEVMETHOD(bus_get_resource_list, iicbus_get_resource_list),
348 DEVMETHOD(bus_add_child, iicbus_add_child),
349 DEVMETHOD(bus_print_child, iicbus_print_child),
350 DEVMETHOD(bus_probe_nomatch, iicbus_probe_nomatch),
351 DEVMETHOD(bus_read_ivar, iicbus_read_ivar),
352 DEVMETHOD(bus_write_ivar, iicbus_write_ivar),
353 DEVMETHOD(bus_child_pnpinfo_str, iicbus_child_pnpinfo_str),
354 DEVMETHOD(bus_child_location_str, iicbus_child_location_str),
355 DEVMETHOD(bus_hinted_child, iicbus_hinted_child),
357 /* iicbus interface */
358 DEVMETHOD(iicbus_transfer, iicbus_transfer),
359 DEVMETHOD(iicbus_get_frequency, iicbus_get_frequency),
364 driver_t iicbus_driver = {
367 sizeof(struct iicbus_softc),
370 devclass_t iicbus_devclass;
372 MODULE_VERSION(iicbus, IICBUS_MODVER);
373 DRIVER_MODULE(iicbus, iichb, iicbus_driver, iicbus_devclass, 0, 0);