xref: /freebsd/sys/dev/smbus/smbus.c (revision bc7512cc58af2e8bbe5bbf5ca0059b1daa1da897)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/bus.h>
36 #include <sys/lock.h>
37 #include <sys/malloc.h>
38 #include <sys/module.h>
39 #include <sys/mutex.h>
40 #include <sys/sbuf.h>
41 
42 #include <dev/smbus/smbconf.h>
43 #include <dev/smbus/smbus.h>
44 
45 #include "smbus_if.h"
46 #include "bus_if.h"
47 
48 struct smbus_ivar
49 {
50 	uint8_t	addr;
51 };
52 
53 /*
54  * Autoconfiguration and support routines for System Management bus
55  */
56 
57 static int
58 smbus_probe(device_t dev)
59 {
60 
61 	device_set_desc(dev, "System Management Bus");
62 
63 	return (0);
64 }
65 
66 static int
67 smbus_attach(device_t dev)
68 {
69 	struct smbus_softc *sc = device_get_softc(dev);
70 
71 	mtx_init(&sc->lock, device_get_nameunit(dev), "smbus", MTX_DEF);
72 	bus_generic_probe(dev);
73 	bus_enumerate_hinted_children(dev);
74 	bus_generic_attach(dev);
75 
76 	return (0);
77 }
78 
79 static int
80 smbus_detach(device_t dev)
81 {
82 	struct smbus_softc *sc = device_get_softc(dev);
83 	int error;
84 
85 	error = bus_generic_detach(dev);
86 	if (error)
87 		return (error);
88 	device_delete_children(dev);
89 	mtx_destroy(&sc->lock);
90 
91 	return (0);
92 }
93 
94 void
95 smbus_generic_intr(device_t dev, u_char devaddr, char low, char high, int err)
96 {
97 }
98 
99 static device_t
100 smbus_add_child(device_t dev, u_int order, const char *name, int unit)
101 {
102 	struct smbus_ivar *devi;
103 	device_t child;
104 
105 	child = device_add_child_ordered(dev, order, name, unit);
106 	if (child == NULL)
107 		return (child);
108 	devi = malloc(sizeof(struct smbus_ivar), M_DEVBUF, M_NOWAIT | M_ZERO);
109 	if (devi == NULL) {
110 		device_delete_child(dev, child);
111 		return (NULL);
112 	}
113 	device_set_ivars(child, devi);
114 	return (child);
115 }
116 
117 static void
118 smbus_hinted_child(device_t bus, const char *dname, int dunit)
119 {
120 	struct smbus_ivar *devi;
121 	device_t child;
122 	int addr;
123 
124 	addr = 0;
125 	resource_int_value(dname, dunit, "addr", &addr);
126 	if (addr > UINT8_MAX) {
127 		device_printf(bus, "ignored incorrect slave address hint 0x%x"
128 		    " for %s%d\n", addr, dname, dunit);
129 		return;
130 	}
131 	child = BUS_ADD_CHILD(bus, SMBUS_ORDER_HINTED, dname, dunit);
132 	if (child == NULL)
133 		return;
134 	devi = device_get_ivars(child);
135 	devi->addr = addr;
136 }
137 
138 static int
139 smbus_child_location(device_t parent, device_t child, struct sbuf *sb)
140 {
141 	struct smbus_ivar *devi;
142 
143 	devi = device_get_ivars(child);
144 	if (devi->addr != 0)
145 		sbuf_printf(sb, "addr=0x%x", devi->addr);
146 	return (0);
147 }
148 
149 static int
150 smbus_print_child(device_t parent, device_t child)
151 {
152 	struct smbus_ivar *devi;
153 	int retval;
154 
155 	devi = device_get_ivars(child);
156 	retval = bus_print_child_header(parent, child);
157 	if (devi->addr != 0)
158 		retval += printf(" at addr 0x%x", devi->addr);
159 	retval += bus_print_child_footer(parent, child);
160 
161 	return (retval);
162 }
163 
164 static int
165 smbus_read_ivar(device_t parent, device_t child, int which, uintptr_t *result)
166 {
167 	struct smbus_ivar *devi;
168 
169 	devi = device_get_ivars(child);
170 	switch (which) {
171 	case SMBUS_IVAR_ADDR:
172 		if (devi->addr != 0)
173 			*result = devi->addr;
174 		else
175 			*result = -1;
176 		break;
177 	default:
178 		return (ENOENT);
179 	}
180 	return (0);
181 }
182 
183 static int
184 smbus_write_ivar(device_t parent, device_t child, int which, uintptr_t value)
185 {
186 	struct smbus_ivar *devi;
187 
188 	devi = device_get_ivars(child);
189 	switch (which) {
190 	case SMBUS_IVAR_ADDR:
191 		/* Allow to set but no change the slave address. */
192 		if (devi->addr != 0)
193 			return (EINVAL);
194 		devi->addr = value;
195 		break;
196 	default:
197 		return (ENOENT);
198 	}
199 	return (0);
200 }
201 
202 static void
203 smbus_probe_nomatch(device_t bus, device_t child)
204 {
205 	struct smbus_ivar *devi = device_get_ivars(child);
206 
207 	/*
208 	 * Ignore (self-identified) devices without a slave address set.
209 	 * For example, smb(4).
210 	 */
211 	if (devi->addr != 0)
212 		device_printf(bus, "<unknown device> at addr %#x\n",
213 		    devi->addr);
214 }
215 
216 /*
217  * Device methods
218  */
219 static device_method_t smbus_methods[] = {
220         /* device interface */
221         DEVMETHOD(device_probe,         smbus_probe),
222         DEVMETHOD(device_attach,        smbus_attach),
223         DEVMETHOD(device_detach,        smbus_detach),
224 
225 	/* bus interface */
226 	DEVMETHOD(bus_add_child,	smbus_add_child),
227 	DEVMETHOD(bus_hinted_child,	smbus_hinted_child),
228 	DEVMETHOD(bus_probe_nomatch,	smbus_probe_nomatch),
229 	DEVMETHOD(bus_child_location,	smbus_child_location),
230 	DEVMETHOD(bus_print_child,	smbus_print_child),
231 	DEVMETHOD(bus_read_ivar,	smbus_read_ivar),
232 	DEVMETHOD(bus_write_ivar,	smbus_write_ivar),
233 
234 	DEVMETHOD_END
235 };
236 
237 driver_t smbus_driver = {
238         "smbus",
239         smbus_methods,
240         sizeof(struct smbus_softc),
241 };
242 
243 MODULE_VERSION(smbus, SMBUS_MODVER);
244