xref: /freebsd/sys/dev/smbus/smbus.c (revision 0640d357f29fb1c0daaaffadd0416c5981413afd)
1 /*-
2  * Copyright (c) 1998 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  *	$Id: smbus.c,v 1.1.1.1 1998/09/03 20:52:54 nsouch Exp $
27  *
28  */
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/kernel.h>
32 #include <sys/module.h>
33 #include <sys/bus.h>
34 
35 #include <dev/smbus/smbconf.h>
36 #include <dev/smbus/smbus.h>
37 
38 /*
39  * Autoconfiguration and support routines for the Philips serial I2C bus
40  */
41 
42 #define DEVTOSMBUS(dev) ((struct smbus_device*)device_get_ivars(dev))
43 
44 /*
45  * structure used to attach devices to the I2C bus
46  */
47 struct smbus_device {
48 	const char *smbd_name;		/* device name */
49 	const u_char smbd_addr;		/* address of the device */
50 	const char *smbd_desc;		/* device descriptor */
51 };
52 
53 /*
54  * list of known devices
55  */
56 struct smbus_device smbus_children[] = {
57 #if 0
58 	{ "smb", 0, "General Call" },
59 #endif
60 	{ NULL, 0 }
61 };
62 
63 static devclass_t smbus_devclass;
64 
65 /*
66  * Device methods
67  */
68 static int smbus_probe(device_t);
69 static int smbus_attach(device_t);
70 static void smbus_print_child(device_t, device_t);
71 static int smbus_read_ivar(device_t , device_t, int, u_long *);
72 
73 static device_method_t smbus_methods[] = {
74         /* device interface */
75         DEVMETHOD(device_probe,         smbus_probe),
76         DEVMETHOD(device_attach,        smbus_attach),
77         DEVMETHOD(device_detach,        bus_generic_detach),
78         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
79 
80         /* bus interface */
81         DEVMETHOD(bus_print_child,      smbus_print_child),
82         DEVMETHOD(bus_read_ivar,        smbus_read_ivar),
83         DEVMETHOD(bus_write_ivar,       bus_generic_write_ivar),
84         DEVMETHOD(bus_create_intr,      bus_generic_create_intr),
85         DEVMETHOD(bus_connect_intr,     bus_generic_connect_intr),
86 
87         { 0, 0 }
88 };
89 
90 static driver_t smbus_driver = {
91         "smbus",
92         smbus_methods,
93         DRIVER_TYPE_MISC,
94         sizeof(struct smbus_softc),
95 };
96 
97 /*
98  * At 'probe' time, we add all the devices which we know about to the
99  * bus.  The generic attach routine will probe and attach them if they
100  * are alive.
101  */
102 static int
103 smbus_probe(device_t dev)
104 {
105 	device_set_desc(dev, "System Management Bus");
106 	return (0);
107 }
108 
109 static int
110 smbus_attach(device_t dev)
111 {
112 	struct smbus_device *smbdev;
113 	device_t child;
114 	char byte;
115 	u_short addr;
116 
117 	bus_generic_attach(dev);
118 
119 #if 0
120 	printf("Probing for devices on smbus%d:\n", device_get_unit(dev));
121 
122 	/* probe known devices */
123 	for (smbdev = smbus_children; smbdev->smbd_name; smbdev++) {
124 
125 		child = device_add_child(dev, smbdev->smbd_name, -1, smbdev);
126 		device_set_desc(child, smbdev->smbd_desc);
127 	}
128 #endif
129 
130         return (0);
131 }
132 
133 void
134 smbus_generic_intr(device_t dev, u_char devaddr, char low, char high)
135 {
136 	return;
137 }
138 
139 static void
140 smbus_print_child(device_t bus, device_t dev)
141 {
142 	struct smbus_device* smbdev = DEVTOSMBUS(dev);
143 
144 	printf(" on %s%d addr 0x%x", device_get_name(bus),
145 		device_get_unit(bus), smbdev->smbd_addr);
146 
147 	return;
148 }
149 
150 static int
151 smbus_read_ivar(device_t bus, device_t dev, int index, u_long* result)
152 {
153 	struct smbus_device* smbdev = DEVTOSMBUS(dev);
154 
155 	switch (index) {
156 	case SMBUS_IVAR_ADDR:
157 		*result = smbdev->smbd_addr;
158 		break;
159 	}
160 	return (ENOENT);
161 }
162 
163 DRIVER_MODULE(smbus, iicsmb, smbus_driver, smbus_devclass, 0, 0);
164 DRIVER_MODULE(smbus, bti2c, smbus_driver, smbus_devclass, 0, 0);
165