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