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