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