xref: /freebsd/sys/dev/smbus/smbus.c (revision 8312a902f98c8a1d991bcee86acbff2abc218d58)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 1998, 2001 Nicolas Souchu
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  *
29  */
30 
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/bus.h>
34 #include <sys/lock.h>
35 #include <sys/malloc.h>
36 #include <sys/module.h>
37 #include <sys/mutex.h>
38 #include <sys/sbuf.h>
39 
40 #include <dev/smbus/smbconf.h>
41 #include <dev/smbus/smbus.h>
42 
43 #include "smbus_if.h"
44 #include "bus_if.h"
45 
46 struct smbus_ivar
47 {
48 	uint8_t	addr;
49 };
50 
51 /*
52  * Autoconfiguration and support routines for System Management bus
53  */
54 
55 static int
56 smbus_probe(device_t dev)
57 {
58 
59 	device_set_desc(dev, "System Management Bus");
60 
61 	return (0);
62 }
63 
64 static int
65 smbus_attach(device_t dev)
66 {
67 	struct smbus_softc *sc = device_get_softc(dev);
68 
69 	mtx_init(&sc->lock, device_get_nameunit(dev), "smbus", MTX_DEF);
70 	bus_generic_probe(dev);
71 	bus_enumerate_hinted_children(dev);
72 	bus_generic_attach(dev);
73 
74 	return (0);
75 }
76 
77 static int
78 smbus_detach(device_t dev)
79 {
80 	struct smbus_softc *sc = device_get_softc(dev);
81 	int error;
82 
83 	error = bus_generic_detach(dev);
84 	if (error)
85 		return (error);
86 	device_delete_children(dev);
87 	mtx_destroy(&sc->lock);
88 
89 	return (0);
90 }
91 
92 void
93 smbus_generic_intr(device_t dev, u_char devaddr, char low, char high, int err)
94 {
95 }
96 
97 static device_t
98 smbus_add_child(device_t dev, u_int order, const char *name, int unit)
99 {
100 	struct smbus_ivar *devi;
101 	device_t child;
102 
103 	child = device_add_child_ordered(dev, order, name, unit);
104 	if (child == NULL)
105 		return (child);
106 	devi = malloc(sizeof(struct smbus_ivar), M_DEVBUF, M_NOWAIT | M_ZERO);
107 	if (devi == NULL) {
108 		device_delete_child(dev, child);
109 		return (NULL);
110 	}
111 	device_set_ivars(child, devi);
112 	return (child);
113 }
114 
115 static void
116 smbus_hinted_child(device_t bus, const char *dname, int dunit)
117 {
118 	struct smbus_ivar *devi;
119 	device_t child;
120 	int addr;
121 
122 	addr = 0;
123 	resource_int_value(dname, dunit, "addr", &addr);
124 	if (addr > UINT8_MAX) {
125 		device_printf(bus, "ignored incorrect slave address hint 0x%x"
126 		    " for %s%d\n", addr, dname, dunit);
127 		return;
128 	}
129 	child = BUS_ADD_CHILD(bus, SMBUS_ORDER_HINTED, dname, dunit);
130 	if (child == NULL)
131 		return;
132 	devi = device_get_ivars(child);
133 	devi->addr = addr;
134 }
135 
136 static int
137 smbus_child_location(device_t parent, device_t child, struct sbuf *sb)
138 {
139 	struct smbus_ivar *devi;
140 
141 	devi = device_get_ivars(child);
142 	if (devi->addr != 0)
143 		sbuf_printf(sb, "addr=0x%x", devi->addr);
144 	return (0);
145 }
146 
147 static int
148 smbus_print_child(device_t parent, device_t child)
149 {
150 	struct smbus_ivar *devi;
151 	int retval;
152 
153 	devi = device_get_ivars(child);
154 	retval = bus_print_child_header(parent, child);
155 	if (devi->addr != 0)
156 		retval += printf(" at addr 0x%x", devi->addr);
157 	retval += bus_print_child_footer(parent, child);
158 
159 	return (retval);
160 }
161 
162 static int
163 smbus_read_ivar(device_t parent, device_t child, int which, uintptr_t *result)
164 {
165 	struct smbus_ivar *devi;
166 
167 	devi = device_get_ivars(child);
168 	switch (which) {
169 	case SMBUS_IVAR_ADDR:
170 		if (devi->addr != 0)
171 			*result = devi->addr;
172 		else
173 			*result = -1;
174 		break;
175 	default:
176 		return (ENOENT);
177 	}
178 	return (0);
179 }
180 
181 static int
182 smbus_write_ivar(device_t parent, device_t child, int which, uintptr_t value)
183 {
184 	struct smbus_ivar *devi;
185 
186 	devi = device_get_ivars(child);
187 	switch (which) {
188 	case SMBUS_IVAR_ADDR:
189 		/* Allow to set but no change the slave address. */
190 		if (devi->addr != 0)
191 			return (EINVAL);
192 		devi->addr = value;
193 		break;
194 	default:
195 		return (ENOENT);
196 	}
197 	return (0);
198 }
199 
200 static void
201 smbus_probe_nomatch(device_t bus, device_t child)
202 {
203 	struct smbus_ivar *devi = device_get_ivars(child);
204 
205 	/*
206 	 * Ignore (self-identified) devices without a slave address set.
207 	 * For example, smb(4).
208 	 */
209 	if (devi->addr != 0)
210 		device_printf(bus, "<unknown device> at addr %#x\n",
211 		    devi->addr);
212 }
213 
214 /*
215  * Device methods
216  */
217 static device_method_t smbus_methods[] = {
218         /* device interface */
219         DEVMETHOD(device_probe,         smbus_probe),
220         DEVMETHOD(device_attach,        smbus_attach),
221         DEVMETHOD(device_detach,        smbus_detach),
222 
223 	/* bus interface */
224 	DEVMETHOD(bus_add_child,	smbus_add_child),
225 	DEVMETHOD(bus_hinted_child,	smbus_hinted_child),
226 	DEVMETHOD(bus_probe_nomatch,	smbus_probe_nomatch),
227 	DEVMETHOD(bus_child_location,	smbus_child_location),
228 	DEVMETHOD(bus_print_child,	smbus_print_child),
229 	DEVMETHOD(bus_read_ivar,	smbus_read_ivar),
230 	DEVMETHOD(bus_write_ivar,	smbus_write_ivar),
231 
232 	DEVMETHOD_END
233 };
234 
235 driver_t smbus_driver = {
236         "smbus",
237         smbus_methods,
238         sizeof(struct smbus_softc),
239 };
240 
241 MODULE_VERSION(smbus, SMBUS_MODVER);
242