xref: /freebsd/sys/dev/iicbus/iicbus.c (revision e4e9813eb92cd7c4d4b819a8fbed5cbd3d92f5d8)
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/module.h>
38 #include <sys/bus.h>
39 
40 
41 #include <dev/iicbus/iiconf.h>
42 #include <dev/iicbus/iicbus.h>
43 
44 #include "iicbus_if.h"
45 
46 #define DEVTOIICBUS(dev) ((struct iicbus_device*)device_get_ivars(dev))
47 
48 devclass_t iicbus_devclass;
49 
50 /* See comments below for why auto-scanning is a bad idea. */
51 #define SCAN_IICBUS 0
52 
53 /*
54  * Device methods
55  */
56 static int iicbus_probe(device_t);
57 static int iicbus_attach(device_t);
58 static int iicbus_detach(device_t);
59 static int iicbus_add_child(device_t dev, int order, const char *name, int unit);
60 
61 static device_method_t iicbus_methods[] = {
62         /* device interface */
63         DEVMETHOD(device_probe,         iicbus_probe),
64         DEVMETHOD(device_attach,        iicbus_attach),
65         DEVMETHOD(device_detach,        iicbus_detach),
66 
67         /* bus interface */
68         DEVMETHOD(bus_add_child,	iicbus_add_child),
69 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
70         DEVMETHOD(bus_print_child,      bus_generic_print_child),
71 
72         { 0, 0 }
73 };
74 
75 driver_t iicbus_driver = {
76         "iicbus",
77         iicbus_methods,
78         sizeof(struct iicbus_softc),
79 };
80 
81 static int
82 iicbus_probe(device_t dev)
83 {
84 
85 	device_set_desc(dev, "Philips I2C bus");
86 	return (0);
87 }
88 
89 #if SCAN_IICBUS
90 static int
91 iic_probe_device(device_t dev, u_char addr)
92 {
93 	int count;
94 	char byte;
95 
96 	if ((addr & 1) == 0) {
97 		/* is device writable? */
98 		if (!iicbus_start(dev, (u_char)addr, 0)) {
99 			iicbus_stop(dev);
100 			return (1);
101 		}
102 	} else {
103 		/* is device readable? */
104 		if (!iicbus_block_read(dev, (u_char)addr, &byte, 1, &count))
105 			return (1);
106 	}
107 
108 	return (0);
109 }
110 #endif
111 
112 /*
113  * We add all the devices which we know about.
114  * The generic attach routine will attach them if they are alive.
115  */
116 static int
117 iicbus_attach(device_t dev)
118 {
119 #if SCAN_IICBUS
120 	unsigned char addr;
121 #endif
122 
123 	iicbus_reset(dev, IIC_FASTEST, 0, NULL);
124 
125 	/* device probing is meaningless since the bus is supposed to be
126 	 * hot-plug. Moreover, some I2C chips do not appreciate random
127 	 * accesses like stop after start to fast, reads for less than
128 	 * x bytes...
129 	 */
130 #if SCAN_IICBUS
131 	printf("Probing for devices on iicbus%d:", device_get_unit(dev));
132 
133 	/* probe any devices */
134 	for (addr = 16; addr < 240; addr++) {
135 		if (iic_probe_device(dev, (u_char)addr)) {
136 			printf(" <%x>", addr);
137 		}
138 	}
139 	printf("\n");
140 #endif
141 
142 	device_add_child(dev, "ic", -1);
143 	device_add_child(dev, "iicsmb", -1);
144 #if 0
145 	/* attach any known device */
146 	device_add_child(dev, "iic", -1);
147 #endif
148 	bus_generic_attach(dev);
149         return (0);
150 }
151 
152 static int
153 iicbus_detach(device_t dev)
154 {
155 
156 	iicbus_reset(dev, IIC_FASTEST, 0, NULL);
157 	bus_generic_detach(dev);
158 	return (0);
159 }
160 
161 static int
162 iicbus_add_child(device_t dev, int order, const char *name, int unit)
163 {
164 
165 	device_add_child_ordered(dev, order, name, unit);
166 	bus_generic_attach(dev);
167 	return (0);
168 }
169 
170 int
171 iicbus_generic_intr(device_t dev, int event, char *buf)
172 {
173 
174 	return (0);
175 }
176 
177 int
178 iicbus_null_callback(device_t dev, int index, caddr_t data)
179 {
180 
181 	return (0);
182 }
183 
184 int
185 iicbus_null_repeated_start(device_t dev, u_char addr)
186 {
187 
188 	return (IIC_ENOTSUPP);
189 }
190 
191 DRIVER_MODULE(iicbus, envctrl, iicbus_driver, iicbus_devclass, 0, 0);
192 DRIVER_MODULE(iicbus, iicbb, iicbus_driver, iicbus_devclass, 0, 0);
193 MODULE_VERSION(iicbus, IICBUS_MODVER);
194