2 * Copyright (c) 1998 Nicolas Souchu
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
26 * $FreeBSD: src/sys/dev/iicbus/iicbus.c,v 1.13 1999/12/03 08:41:02 mdodd Exp $
27 * $DragonFly: src/sys/bus/iicbus/iicbus.c,v 1.5 2006/12/22 23:12:16 swildner Exp $
32 * Autoconfiguration and support routines for the Philips serial I2C bus
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/module.h>
41 #include <machine/clock.h>
46 #include "iicbus_if.h"
48 #define DEVTOIICBUS(dev) ((struct iicbus_device*)device_get_ivars(dev))
51 * structure used to attach devices to the I2C bus
53 struct iicbus_device
{
54 const char *iicd_name
; /* device name */
55 int iicd_class
; /* driver or slave device class */
56 const char *iicd_desc
; /* device descriptor */
57 u_char iicd_addr
; /* address of the device */
58 int iicd_waitack
; /* wait for ack timeout or delay */
59 int iicd_alive
; /* 1 if device found */
63 * Common I2C addresses
65 #define I2C_GENERAL_CALL 0x0
66 #define PCF_MASTER_ADDRESS 0xaa
67 #define FIRST_SLAVE_ADDR 0x2
69 #define LAST_SLAVE_ADDR 255
71 #define IICBUS_UNKNOWN_CLASS 0
72 #define IICBUS_DEVICE_CLASS 1
73 #define IICBUS_DRIVER_CLASS 2
76 * list of known devices
78 * XXX only one smb driver should exist for each I2C interface
80 static struct iicbus_device iicbus_children
[] = {
81 { "iicsmb", IICBUS_DRIVER_CLASS
, "I2C to SMB bridge" },
82 { "iic", IICBUS_DRIVER_CLASS
, "I2C general purpose I/O" },
84 { "ic", IICBUS_DEVICE_CLASS
, "network interface", PCF_MASTER_ADDRESS
},
89 static devclass_t iicbus_devclass
;
94 static int iicbus_probe(device_t
);
95 static int iicbus_attach(device_t
);
96 static int iicbus_print_child(device_t
, device_t
);
97 static int iicbus_read_ivar(device_t
, device_t
, int, u_long
*);
98 static int iicbus_write_ivar(device_t
, device_t
, int, u_long
);
100 static device_method_t iicbus_methods
[] = {
101 /* device interface */
102 DEVMETHOD(device_probe
, iicbus_probe
),
103 DEVMETHOD(device_attach
, iicbus_attach
),
104 DEVMETHOD(device_detach
, bus_generic_detach
),
105 DEVMETHOD(device_shutdown
, bus_generic_shutdown
),
108 DEVMETHOD(bus_print_child
, iicbus_print_child
),
109 DEVMETHOD(bus_read_ivar
, iicbus_read_ivar
),
110 DEVMETHOD(bus_write_ivar
, iicbus_write_ivar
),
115 static driver_t iicbus_driver
= {
118 sizeof(struct iicbus_softc
),
122 iicbus_probe(device_t dev
)
124 device_set_desc(dev
, "Philips I2C bus");
131 iic_probe_device(device_t dev
, u_char addr
)
136 if ((addr
& 1) == 0) {
137 /* is device writable? */
138 if (!iicbus_start(dev
, (u_char
)addr
, 0)) {
143 /* is device readable? */
144 if (!iicbus_block_read(dev
, (u_char
)addr
, &byte
, 1, &count
))
153 * We add all the devices which we know about.
154 * The generic attach routine will attach them if they are alive.
157 iicbus_attach(device_t dev
)
159 struct iicbus_device
*iicdev
;
162 iicbus_reset(dev
, IIC_FASTEST
, 0, NULL
);
164 /* device probing is meaningless since the bus is supposed to be
165 * hot-plug. Moreover, some I2C chips do not appreciate random
166 * accesses like stop after start to fast, reads for less than
170 kprintf("Probing for devices on iicbus%d:", device_get_unit(dev
));
172 /* probe any devices */
173 for (addr
= FIRST_SLAVE_ADDR
; addr
<= LAST_SLAVE_ADDR
; addr
++) {
174 if (iic_probe_device(dev
, (u_char
)addr
)) {
175 kprintf(" <%x>", addr
);
181 /* attach known devices */
182 for (iicdev
= iicbus_children
; iicdev
->iicd_name
; iicdev
++) {
183 switch (iicdev
->iicd_class
) {
184 case IICBUS_DEVICE_CLASS
:
185 /* check if the devclass exists */
186 if (devclass_find(iicdev
->iicd_name
))
187 iicdev
->iicd_alive
= 1;
188 else if (bootverbose
)
189 kprintf("iicbus: %s devclass not found\n",
193 case IICBUS_DRIVER_CLASS
:
194 /* check if the devclass exists */
195 if (devclass_find(iicdev
->iicd_name
))
196 iicdev
->iicd_alive
= 1;
197 else if (bootverbose
)
198 kprintf("iicbus: %s devclass not found\n",
203 panic("%s: unknown class!", __func__
);
206 if (iicdev
->iicd_alive
) {
207 child
= device_add_child(dev
, iicdev
->iicd_name
, -1);
208 device_set_ivars(child
, iicdev
);
209 device_set_desc(child
, iicdev
->iicd_desc
);
212 bus_generic_attach(dev
);
218 iicbus_generic_intr(device_t dev
, int event
, char *buf
)
224 iicbus_null_callback(device_t dev
, int index
, caddr_t data
)
230 iicbus_null_repeated_start(device_t dev
, u_char addr
)
232 return (IIC_ENOTSUPP
);
236 iicbus_print_child(device_t bus
, device_t dev
)
238 struct iicbus_device
* iicdev
= DEVTOIICBUS(dev
);
241 retval
+= bus_print_child_header(bus
, dev
);
243 switch (iicdev
->iicd_class
) {
244 case IICBUS_DEVICE_CLASS
:
245 retval
+= kprintf(" on %s addr 0x%x\n",
246 device_get_nameunit(bus
), iicdev
->iicd_addr
);
249 case IICBUS_DRIVER_CLASS
:
250 retval
+= bus_print_child_footer(bus
, dev
);
254 panic("%s: unknown class!", __func__
);
261 iicbus_read_ivar(device_t bus
, device_t dev
, int index
, u_long
* result
)
263 struct iicbus_device
* iicdev
= DEVTOIICBUS(dev
);
266 case IICBUS_IVAR_ADDR
:
267 *result
= (u_long
)iicdev
->iicd_addr
;
278 iicbus_write_ivar(device_t bus
, device_t dev
, int index
, u_long val
)
288 DRIVER_MODULE(iicbus
, pcf
, iicbus_driver
, iicbus_devclass
, 0, 0);
289 DRIVER_MODULE(iicbus
, iicbb
, iicbus_driver
, iicbus_devclass
, 0, 0);
290 DRIVER_MODULE(iicbus
, bti2c
, iicbus_driver
, iicbus_devclass
, 0, 0);