iicbus.c revision 274641
1/*-
2 * Copyright (c) 1998, 2001 Nicolas Souchu
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 *
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
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/sys/dev/iicbus/iicbus.c 274641 2014-11-18 01:54:31Z ian $");
29
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/lock.h>
38#include <sys/malloc.h>
39#include <sys/module.h>
40#include <sys/mutex.h>
41#include <sys/sysctl.h>
42#include <sys/bus.h>
43
44#include <dev/iicbus/iiconf.h>
45#include <dev/iicbus/iicbus.h>
46
47#include "iicbus_if.h"
48
49/* See comments below for why auto-scanning is a bad idea. */
50#define SCAN_IICBUS 0
51
52static int
53iicbus_probe(device_t dev)
54{
55
56	device_set_desc(dev, "Philips I2C bus");
57
58	/* Allow other subclasses to override this driver. */
59	return (BUS_PROBE_GENERIC);
60}
61
62#if SCAN_IICBUS
63static int
64iic_probe_device(device_t dev, u_char addr)
65{
66	int count;
67	char byte;
68
69	if ((addr & 1) == 0) {
70		/* is device writable? */
71		if (!iicbus_start(dev, (u_char)addr, 0)) {
72			iicbus_stop(dev);
73			return (1);
74		}
75	} else {
76		/* is device readable? */
77		if (!iicbus_block_read(dev, (u_char)addr, &byte, 1, &count))
78			return (1);
79	}
80
81	return (0);
82}
83#endif
84
85/*
86 * We add all the devices which we know about.
87 * The generic attach routine will attach them if they are alive.
88 */
89static int
90iicbus_attach(device_t dev)
91{
92#if SCAN_IICBUS
93	unsigned char addr;
94#endif
95	struct iicbus_softc *sc = IICBUS_SOFTC(dev);
96	int strict;
97
98	sc->dev = 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)
104		sc->strict = strict;
105	else
106		sc->strict = 1;
107
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
111	 * x bytes...
112	 */
113#if SCAN_IICBUS
114	printf("Probing for devices on iicbus%d:", device_get_unit(dev));
115
116	/* probe any devices */
117	for (addr = 16; addr < 240; addr++) {
118		if (iic_probe_device(dev, (u_char)addr)) {
119			printf(" <%x>", addr);
120		}
121	}
122	printf("\n");
123#endif
124	bus_generic_probe(dev);
125	bus_enumerate_hinted_children(dev);
126	bus_generic_attach(dev);
127        return (0);
128}
129
130static int
131iicbus_detach(device_t dev)
132{
133	struct iicbus_softc *sc = IICBUS_SOFTC(dev);
134
135	iicbus_reset(dev, IIC_FASTEST, 0, NULL);
136	bus_generic_detach(dev);
137	mtx_destroy(&sc->lock);
138	return (0);
139}
140
141static int
142iicbus_print_child(device_t dev, device_t child)
143{
144	struct iicbus_ivar *devi = IICBUS_IVAR(child);
145	int retval = 0;
146
147	retval += bus_print_child_header(dev, child);
148	if (devi->addr != 0)
149		retval += printf(" at addr %#x", devi->addr);
150	retval += bus_print_child_footer(dev, child);
151
152	return (retval);
153}
154
155static void
156iicbus_probe_nomatch(device_t bus, device_t child)
157{
158	struct iicbus_ivar *devi = IICBUS_IVAR(child);
159
160	device_printf(bus, "<unknown card>");
161	printf(" at addr %#x\n", devi->addr);
162	return;
163}
164
165static int
166iicbus_child_location_str(device_t bus, device_t child, char *buf,
167    size_t buflen)
168{
169	struct iicbus_ivar *devi = IICBUS_IVAR(child);
170
171	snprintf(buf, buflen, "addr=%#x", devi->addr);
172	return (0);
173}
174
175static int
176iicbus_child_pnpinfo_str(device_t bus, device_t child, char *buf,
177    size_t buflen)
178{
179	*buf = '\0';
180	return (0);
181}
182
183static int
184iicbus_read_ivar(device_t bus, device_t child, int which, uintptr_t *result)
185{
186	struct iicbus_ivar *devi = IICBUS_IVAR(child);
187
188	switch (which) {
189	default:
190		return (EINVAL);
191	case IICBUS_IVAR_ADDR:
192		*result = devi->addr;
193		break;
194	}
195	return (0);
196}
197
198static device_t
199iicbus_add_child(device_t dev, u_int order, const char *name, int unit)
200{
201	device_t child;
202	struct iicbus_ivar *devi;
203
204	child = device_add_child_ordered(dev, order, name, unit);
205	if (child == NULL)
206		return (child);
207	devi = malloc(sizeof(struct iicbus_ivar), M_DEVBUF, M_NOWAIT | M_ZERO);
208	if (devi == NULL) {
209		device_delete_child(dev, child);
210		return (0);
211	}
212	device_set_ivars(child, devi);
213	return (child);
214}
215
216static void
217iicbus_hinted_child(device_t bus, const char *dname, int dunit)
218{
219	device_t child;
220	struct iicbus_ivar *devi;
221
222	child = BUS_ADD_CHILD(bus, 0, dname, dunit);
223	devi = IICBUS_IVAR(child);
224	resource_int_value(dname, dunit, "addr", &devi->addr);
225}
226
227int
228iicbus_generic_intr(device_t dev, int event, char *buf)
229{
230
231	return (0);
232}
233
234int
235iicbus_null_callback(device_t dev, int index, caddr_t data)
236{
237
238	return (0);
239}
240
241int
242iicbus_null_repeated_start(device_t dev, u_char addr)
243{
244
245	return (IIC_ENOTSUPP);
246}
247
248void
249iicbus_init_frequency(device_t dev, u_int bus_freq)
250{
251	struct iicbus_softc *sc = IICBUS_SOFTC(dev);
252
253	/*
254	 * If a bus frequency value was passed in, use it.  Otherwise initialize
255	 * it first to the standard i2c 100KHz frequency, then override that
256	 * from a hint if one exists.
257	 */
258	if (bus_freq > 0)
259		sc->bus_freq = bus_freq;
260	else {
261		sc->bus_freq = 100000;
262		resource_int_value(device_get_name(dev), device_get_unit(dev),
263		    "frequency", (int *)&sc->bus_freq);
264	}
265	/*
266	 * Set up the sysctl that allows the bus frequency to be changed.
267	 * It is flagged as a tunable so that the user can set the value in
268	 * loader(8), and that will override any other setting from any source.
269	 * The sysctl tunable/value is the one most directly controlled by the
270	 * user and thus the one that always takes precedence.
271	 */
272	SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
273	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
274	    OID_AUTO, "frequency", CTLFLAG_RW | CTLFLAG_TUN, &sc->bus_freq,
275	    sc->bus_freq, "Bus frequency in Hz");
276}
277
278static u_int
279iicbus_get_frequency(device_t dev, u_char speed)
280{
281	struct iicbus_softc *sc = IICBUS_SOFTC(dev);
282
283	/*
284	 * If the frequency has not been configured for the bus, or the request
285	 * is specifically for SLOW speed, use the standard 100KHz rate, else
286	 * use the configured bus speed.
287	 */
288	if (sc->bus_freq == 0 || speed == IIC_SLOW)
289		return (100000);
290	return (sc->bus_freq);
291}
292
293static device_method_t iicbus_methods[] = {
294        /* device interface */
295        DEVMETHOD(device_probe,         iicbus_probe),
296        DEVMETHOD(device_attach,        iicbus_attach),
297        DEVMETHOD(device_detach,        iicbus_detach),
298
299        /* bus interface */
300        DEVMETHOD(bus_add_child,	iicbus_add_child),
301        DEVMETHOD(bus_print_child,      iicbus_print_child),
302	DEVMETHOD(bus_probe_nomatch,	iicbus_probe_nomatch),
303	DEVMETHOD(bus_read_ivar,	iicbus_read_ivar),
304	DEVMETHOD(bus_child_pnpinfo_str, iicbus_child_pnpinfo_str),
305	DEVMETHOD(bus_child_location_str, iicbus_child_location_str),
306	DEVMETHOD(bus_hinted_child,	iicbus_hinted_child),
307
308	/* iicbus interface */
309	DEVMETHOD(iicbus_transfer,	iicbus_transfer),
310	DEVMETHOD(iicbus_get_frequency,	iicbus_get_frequency),
311
312	DEVMETHOD_END
313};
314
315driver_t iicbus_driver = {
316        "iicbus",
317        iicbus_methods,
318        sizeof(struct iicbus_softc),
319};
320
321devclass_t iicbus_devclass;
322
323MODULE_VERSION(iicbus, IICBUS_MODVER);
324DRIVER_MODULE(iicbus, iichb, iicbus_driver, iicbus_devclass, 0, 0);
325
326