xref: /freebsd/sys/dev/iicbus/iicbus.c (revision dd41de95a84d979615a2ef11df6850622bf6184e)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 1998, 2001 Nicolas Souchu
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
15  *
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
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 /*
33  * Autoconfiguration and support routines for the Philips serial I2C bus
34  */
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/lock.h>
40 #include <sys/malloc.h>
41 #include <sys/module.h>
42 #include <sys/mutex.h>
43 #include <sys/rman.h>
44 #include <sys/sysctl.h>
45 #include <sys/bus.h>
46 
47 #include <dev/iicbus/iiconf.h>
48 #include <dev/iicbus/iicbus.h>
49 
50 #include "iicbus_if.h"
51 
52 /* See comments below for why auto-scanning is a bad idea. */
53 #define SCAN_IICBUS 0
54 
55 SYSCTL_NODE(_hw, OID_AUTO, i2c, CTLFLAG_RW, 0, "i2c controls");
56 
57 static int
58 iicbus_probe(device_t dev)
59 {
60 
61 	device_set_desc(dev, "Philips I2C bus");
62 
63 	/* Allow other subclasses to override this driver. */
64 	return (BUS_PROBE_GENERIC);
65 }
66 
67 #if SCAN_IICBUS
68 static int
69 iic_probe_device(device_t dev, u_char addr)
70 {
71 	int count;
72 	char byte;
73 
74 	if ((addr & 1) == 0) {
75 		/* is device writable? */
76 		if (!iicbus_start(dev, (u_char)addr, 0)) {
77 			iicbus_stop(dev);
78 			return (1);
79 		}
80 	} else {
81 		/* is device readable? */
82 		if (!iicbus_block_read(dev, (u_char)addr, &byte, 1, &count))
83 			return (1);
84 	}
85 
86 	return (0);
87 }
88 #endif
89 
90 /*
91  * We add all the devices which we know about.
92  * The generic attach routine will attach them if they are alive.
93  */
94 int
95 iicbus_attach_common(device_t dev, u_int bus_freq)
96 {
97 #if SCAN_IICBUS
98 	unsigned char addr;
99 #endif
100 	struct iicbus_softc *sc = IICBUS_SOFTC(dev);
101 	int strict;
102 
103 	sc->dev = dev;
104 	mtx_init(&sc->lock, "iicbus", NULL, MTX_DEF);
105 	iicbus_init_frequency(dev, bus_freq);
106 	iicbus_reset(dev, IIC_FASTEST, 0, NULL);
107 	if (resource_int_value(device_get_name(dev),
108 		device_get_unit(dev), "strict", &strict) == 0)
109 		sc->strict = strict;
110 	else
111 		sc->strict = 1;
112 
113 	/* device probing is meaningless since the bus is supposed to be
114 	 * hot-plug. Moreover, some I2C chips do not appreciate random
115 	 * accesses like stop after start to fast, reads for less than
116 	 * x bytes...
117 	 */
118 #if SCAN_IICBUS
119 	printf("Probing for devices on iicbus%d:", device_get_unit(dev));
120 
121 	/* probe any devices */
122 	for (addr = 16; addr < 240; addr++) {
123 		if (iic_probe_device(dev, (u_char)addr)) {
124 			printf(" <%x>", addr);
125 		}
126 	}
127 	printf("\n");
128 #endif
129 	bus_generic_probe(dev);
130 	bus_enumerate_hinted_children(dev);
131 	bus_generic_attach(dev);
132         return (0);
133 }
134 
135 static int
136 iicbus_attach(device_t dev)
137 {
138 
139 	return (iicbus_attach_common(dev, 0));
140 }
141 
142 int
143 iicbus_detach(device_t dev)
144 {
145 	struct iicbus_softc *sc = IICBUS_SOFTC(dev);
146 	int err;
147 
148 	if ((err = device_delete_children(dev)) != 0)
149 		return (err);
150 	iicbus_reset(dev, IIC_FASTEST, 0, NULL);
151 	mtx_destroy(&sc->lock);
152 	return (0);
153 }
154 
155 static int
156 iicbus_print_child(device_t dev, device_t child)
157 {
158 	struct iicbus_ivar *devi = IICBUS_IVAR(child);
159 	int retval = 0;
160 
161 	retval += bus_print_child_header(dev, child);
162 	if (devi->addr != 0)
163 		retval += printf(" at addr %#x", devi->addr);
164 	resource_list_print_type(&devi->rl, "irq", SYS_RES_IRQ, "%jd");
165 	retval += bus_print_child_footer(dev, child);
166 
167 	return (retval);
168 }
169 
170 void
171 iicbus_probe_nomatch(device_t bus, device_t child)
172 {
173 	struct iicbus_ivar *devi = IICBUS_IVAR(child);
174 
175 	device_printf(bus, "<unknown card> at addr %#x\n", devi->addr);
176 }
177 
178 int
179 iicbus_child_location_str(device_t bus, device_t child, char *buf,
180     size_t buflen)
181 {
182 	struct iicbus_ivar *devi = IICBUS_IVAR(child);
183 
184 	snprintf(buf, buflen, "addr=%#x", devi->addr);
185 	return (0);
186 }
187 
188 int
189 iicbus_child_pnpinfo_str(device_t bus, device_t child, char *buf,
190     size_t buflen)
191 {
192 	*buf = '\0';
193 	return (0);
194 }
195 
196 int
197 iicbus_read_ivar(device_t bus, device_t child, int which, uintptr_t *result)
198 {
199 	struct iicbus_ivar *devi = IICBUS_IVAR(child);
200 
201 	switch (which) {
202 	default:
203 		return (EINVAL);
204 	case IICBUS_IVAR_ADDR:
205 		*result = devi->addr;
206 		break;
207 	}
208 	return (0);
209 }
210 
211 int
212 iicbus_write_ivar(device_t bus, device_t child, int which, uintptr_t value)
213 {
214 	struct iicbus_ivar *devi = IICBUS_IVAR(child);
215 
216 	switch (which) {
217 	default:
218 		return (EINVAL);
219 	case IICBUS_IVAR_ADDR:
220 		if (devi->addr != 0)
221 			return (EINVAL);
222 		devi->addr = value;
223 	}
224 	return (0);
225 }
226 
227 device_t
228 iicbus_add_child_common(device_t dev, u_int order, const char *name, int unit,
229     size_t ivars_size)
230 {
231 	device_t child;
232 	struct iicbus_ivar *devi;
233 
234 	child = device_add_child_ordered(dev, order, name, unit);
235 	if (child == NULL)
236 		return (child);
237 	devi = malloc(ivars_size, M_DEVBUF, M_NOWAIT | M_ZERO);
238 	if (devi == NULL) {
239 		device_delete_child(dev, child);
240 		return (0);
241 	}
242 	resource_list_init(&devi->rl);
243 	device_set_ivars(child, devi);
244 	return (child);
245 }
246 
247 static device_t
248 iicbus_add_child(device_t dev, u_int order, const char *name, int unit)
249 {
250 
251 	return (iicbus_add_child_common(
252 	    dev, order, name, unit, sizeof(struct iicbus_ivar)));
253 }
254 
255 static void
256 iicbus_hinted_child(device_t bus, const char *dname, int dunit)
257 {
258 	device_t child;
259 	int irq;
260 	struct iicbus_ivar *devi;
261 
262 	child = BUS_ADD_CHILD(bus, 0, dname, dunit);
263 	devi = IICBUS_IVAR(child);
264 	resource_int_value(dname, dunit, "addr", &devi->addr);
265 	if (resource_int_value(dname, dunit, "irq", &irq) == 0) {
266 		if (bus_set_resource(child, SYS_RES_IRQ, 0, irq, 1) != 0)
267 			device_printf(bus,
268 			    "warning: bus_set_resource() failed\n");
269 	}
270 }
271 
272 static struct resource_list *
273 iicbus_get_resource_list(device_t bus __unused, device_t child)
274 {
275 	struct iicbus_ivar *devi;
276 
277 	devi = IICBUS_IVAR(child);
278 	return (&devi->rl);
279 }
280 
281 int
282 iicbus_generic_intr(device_t dev, int event, char *buf)
283 {
284 
285 	return (0);
286 }
287 
288 int
289 iicbus_null_callback(device_t dev, int index, caddr_t data)
290 {
291 
292 	return (0);
293 }
294 
295 int
296 iicbus_null_repeated_start(device_t dev, u_char addr)
297 {
298 
299 	return (IIC_ENOTSUPP);
300 }
301 
302 void
303 iicbus_init_frequency(device_t dev, u_int bus_freq)
304 {
305 	struct iicbus_softc *sc = IICBUS_SOFTC(dev);
306 
307 	/*
308 	 * If a bus frequency value was passed in, use it.  Otherwise initialize
309 	 * it first to the standard i2c 100KHz frequency, then override that
310 	 * from a hint if one exists.
311 	 */
312 	if (bus_freq > 0)
313 		sc->bus_freq = bus_freq;
314 	else {
315 		sc->bus_freq = 100000;
316 		resource_int_value(device_get_name(dev), device_get_unit(dev),
317 		    "frequency", (int *)&sc->bus_freq);
318 	}
319 	/*
320 	 * Set up the sysctl that allows the bus frequency to be changed.
321 	 * It is flagged as a tunable so that the user can set the value in
322 	 * loader(8), and that will override any other setting from any source.
323 	 * The sysctl tunable/value is the one most directly controlled by the
324 	 * user and thus the one that always takes precedence.
325 	 */
326 	SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
327 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
328 	    OID_AUTO, "frequency", CTLFLAG_RW | CTLFLAG_TUN, &sc->bus_freq,
329 	    sc->bus_freq, "Bus frequency in Hz");
330 }
331 
332 static u_int
333 iicbus_get_frequency(device_t dev, u_char speed)
334 {
335 	struct iicbus_softc *sc = IICBUS_SOFTC(dev);
336 
337 	/*
338 	 * If the frequency has not been configured for the bus, or the request
339 	 * is specifically for SLOW speed, use the standard 100KHz rate, else
340 	 * use the configured bus speed.
341 	 */
342 	if (sc->bus_freq == 0 || speed == IIC_SLOW)
343 		return (100000);
344 	return (sc->bus_freq);
345 }
346 
347 static device_method_t iicbus_methods[] = {
348 	/* device interface */
349 	DEVMETHOD(device_probe,		iicbus_probe),
350 	DEVMETHOD(device_attach,	iicbus_attach),
351 	DEVMETHOD(device_detach,	iicbus_detach),
352 	DEVMETHOD(device_suspend,	bus_generic_suspend),
353 	DEVMETHOD(device_resume,	bus_generic_resume),
354 
355 	/* bus interface */
356 	DEVMETHOD(bus_setup_intr,	bus_generic_setup_intr),
357 	DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
358 	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
359 	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
360 	DEVMETHOD(bus_adjust_resource,	bus_generic_adjust_resource),
361 	DEVMETHOD(bus_alloc_resource,	bus_generic_rl_alloc_resource),
362 	DEVMETHOD(bus_get_resource,	bus_generic_rl_get_resource),
363 	DEVMETHOD(bus_release_resource, bus_generic_rl_release_resource),
364 	DEVMETHOD(bus_set_resource,	bus_generic_rl_set_resource),
365 	DEVMETHOD(bus_get_resource_list, iicbus_get_resource_list),
366 	DEVMETHOD(bus_add_child,	iicbus_add_child),
367 	DEVMETHOD(bus_print_child,	iicbus_print_child),
368 	DEVMETHOD(bus_probe_nomatch,	iicbus_probe_nomatch),
369 	DEVMETHOD(bus_read_ivar,	iicbus_read_ivar),
370 	DEVMETHOD(bus_write_ivar,	iicbus_write_ivar),
371 	DEVMETHOD(bus_child_pnpinfo_str, iicbus_child_pnpinfo_str),
372 	DEVMETHOD(bus_child_location_str, iicbus_child_location_str),
373 	DEVMETHOD(bus_hinted_child,	iicbus_hinted_child),
374 
375 	/* iicbus interface */
376 	DEVMETHOD(iicbus_transfer,	iicbus_transfer),
377 	DEVMETHOD(iicbus_get_frequency,	iicbus_get_frequency),
378 
379 	DEVMETHOD_END
380 };
381 
382 driver_t iicbus_driver = {
383         "iicbus",
384         iicbus_methods,
385         sizeof(struct iicbus_softc),
386 };
387 
388 devclass_t iicbus_devclass;
389 
390 MODULE_VERSION(iicbus, IICBUS_MODVER);
391 DRIVER_MODULE(iicbus, iichb, iicbus_driver, iicbus_devclass, 0, 0);
392