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