xref: /freebsd/sys/dev/iicbus/iicbus.c (revision e39e854e27f53a784c3982cbeb68f4ad1cfd9162)
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$");
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/bus.h>
42 
43 #include <dev/iicbus/iiconf.h>
44 #include <dev/iicbus/iicbus.h>
45 
46 #include "iicbus_if.h"
47 
48 /* See comments below for why auto-scanning is a bad idea. */
49 #define SCAN_IICBUS 0
50 
51 static int
52 iicbus_probe(device_t dev)
53 {
54 
55 	device_set_desc(dev, "Philips I2C bus");
56 
57 	/* Allow other subclasses to override this driver. */
58 	return (BUS_PROBE_GENERIC);
59 }
60 
61 #if SCAN_IICBUS
62 static int
63 iic_probe_device(device_t dev, u_char addr)
64 {
65 	int count;
66 	char byte;
67 
68 	if ((addr & 1) == 0) {
69 		/* is device writable? */
70 		if (!iicbus_start(dev, (u_char)addr, 0)) {
71 			iicbus_stop(dev);
72 			return (1);
73 		}
74 	} else {
75 		/* is device readable? */
76 		if (!iicbus_block_read(dev, (u_char)addr, &byte, 1, &count))
77 			return (1);
78 	}
79 
80 	return (0);
81 }
82 #endif
83 
84 /*
85  * We add all the devices which we know about.
86  * The generic attach routine will attach them if they are alive.
87  */
88 static int
89 iicbus_attach(device_t dev)
90 {
91 #if SCAN_IICBUS
92 	unsigned char addr;
93 #endif
94 	struct iicbus_softc *sc = IICBUS_SOFTC(dev);
95 	int strict;
96 
97 	sc->dev = dev;
98 	mtx_init(&sc->lock, "iicbus", NULL, MTX_DEF);
99 	iicbus_reset(dev, IIC_FASTEST, 0, NULL);
100 	if (resource_int_value(device_get_name(dev),
101 		device_get_unit(dev), "strict", &strict) == 0)
102 		sc->strict = strict;
103 	else
104 		sc->strict = 1;
105 
106 	/* device probing is meaningless since the bus is supposed to be
107 	 * hot-plug. Moreover, some I2C chips do not appreciate random
108 	 * accesses like stop after start to fast, reads for less than
109 	 * x bytes...
110 	 */
111 #if SCAN_IICBUS
112 	printf("Probing for devices on iicbus%d:", device_get_unit(dev));
113 
114 	/* probe any devices */
115 	for (addr = 16; addr < 240; addr++) {
116 		if (iic_probe_device(dev, (u_char)addr)) {
117 			printf(" <%x>", addr);
118 		}
119 	}
120 	printf("\n");
121 #endif
122 	bus_generic_probe(dev);
123 	bus_enumerate_hinted_children(dev);
124 	bus_generic_attach(dev);
125         return (0);
126 }
127 
128 static int
129 iicbus_detach(device_t dev)
130 {
131 	struct iicbus_softc *sc = IICBUS_SOFTC(dev);
132 
133 	iicbus_reset(dev, IIC_FASTEST, 0, NULL);
134 	bus_generic_detach(dev);
135 	mtx_destroy(&sc->lock);
136 	return (0);
137 }
138 
139 static int
140 iicbus_print_child(device_t dev, device_t child)
141 {
142 	struct iicbus_ivar *devi = IICBUS_IVAR(child);
143 	int retval = 0;
144 
145 	retval += bus_print_child_header(dev, child);
146 	if (devi->addr != 0)
147 		retval += printf(" at addr %#x", devi->addr);
148 	retval += bus_print_child_footer(dev, child);
149 
150 	return (retval);
151 }
152 
153 static void
154 iicbus_probe_nomatch(device_t bus, device_t child)
155 {
156 	struct iicbus_ivar *devi = IICBUS_IVAR(child);
157 
158 	device_printf(bus, "<unknown card>");
159 	printf(" at addr %#x\n", devi->addr);
160 	return;
161 }
162 
163 static int
164 iicbus_child_location_str(device_t bus, device_t child, char *buf,
165     size_t buflen)
166 {
167 	struct iicbus_ivar *devi = IICBUS_IVAR(child);
168 
169 	snprintf(buf, buflen, "addr=%#x", devi->addr);
170 	return (0);
171 }
172 
173 static int
174 iicbus_child_pnpinfo_str(device_t bus, device_t child, char *buf,
175     size_t buflen)
176 {
177 	*buf = '\0';
178 	return (0);
179 }
180 
181 static int
182 iicbus_read_ivar(device_t bus, device_t child, int which, uintptr_t *result)
183 {
184 	struct iicbus_ivar *devi = IICBUS_IVAR(child);
185 
186 	switch (which) {
187 	default:
188 		return (EINVAL);
189 	case IICBUS_IVAR_ADDR:
190 		*result = devi->addr;
191 		break;
192 	}
193 	return (0);
194 }
195 
196 static device_t
197 iicbus_add_child(device_t dev, u_int order, const char *name, int unit)
198 {
199 	device_t child;
200 	struct iicbus_ivar *devi;
201 
202 	child = device_add_child_ordered(dev, order, name, unit);
203 	if (child == NULL)
204 		return (child);
205 	devi = malloc(sizeof(struct iicbus_ivar), M_DEVBUF, M_NOWAIT | M_ZERO);
206 	if (devi == NULL) {
207 		device_delete_child(dev, child);
208 		return (0);
209 	}
210 	device_set_ivars(child, devi);
211 	return (child);
212 }
213 
214 static void
215 iicbus_hinted_child(device_t bus, const char *dname, int dunit)
216 {
217 	device_t child;
218 	struct iicbus_ivar *devi;
219 
220 	child = BUS_ADD_CHILD(bus, 0, dname, dunit);
221 	devi = IICBUS_IVAR(child);
222 	resource_int_value(dname, dunit, "addr", &devi->addr);
223 }
224 
225 int
226 iicbus_generic_intr(device_t dev, int event, char *buf)
227 {
228 
229 	return (0);
230 }
231 
232 int
233 iicbus_null_callback(device_t dev, int index, caddr_t data)
234 {
235 
236 	return (0);
237 }
238 
239 int
240 iicbus_null_repeated_start(device_t dev, u_char addr)
241 {
242 
243 	return (IIC_ENOTSUPP);
244 }
245 
246 static device_method_t iicbus_methods[] = {
247         /* device interface */
248         DEVMETHOD(device_probe,         iicbus_probe),
249         DEVMETHOD(device_attach,        iicbus_attach),
250         DEVMETHOD(device_detach,        iicbus_detach),
251 
252         /* bus interface */
253         DEVMETHOD(bus_add_child,	iicbus_add_child),
254         DEVMETHOD(bus_print_child,      iicbus_print_child),
255 	DEVMETHOD(bus_probe_nomatch,	iicbus_probe_nomatch),
256 	DEVMETHOD(bus_read_ivar,	iicbus_read_ivar),
257 	DEVMETHOD(bus_child_pnpinfo_str, iicbus_child_pnpinfo_str),
258 	DEVMETHOD(bus_child_location_str, iicbus_child_location_str),
259 	DEVMETHOD(bus_hinted_child,	iicbus_hinted_child),
260 
261 	/* iicbus interface */
262 	DEVMETHOD(iicbus_transfer,	iicbus_transfer),
263 
264 	DEVMETHOD_END
265 };
266 
267 driver_t iicbus_driver = {
268         "iicbus",
269         iicbus_methods,
270         sizeof(struct iicbus_softc),
271 };
272 
273 devclass_t iicbus_devclass;
274 
275 MODULE_VERSION(iicbus, IICBUS_MODVER);
276 DRIVER_MODULE(iicbus, iichb, iicbus_driver, iicbus_devclass, 0, 0);
277 
278