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 #include <sys/cdefs.h>
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/bus.h>
38 #include <sys/lock.h>
39 #include <sys/malloc.h>
40 #include <sys/module.h>
41 #include <sys/mutex.h>
42 #include <sys/rman.h>
43 #include <sys/sbuf.h>
44 #include <sys/sysctl.h>
45
46 #include <dev/iicbus/iiconf.h>
47 #include <dev/iicbus/iicbus.h>
48
49 #include "iicbus_if.h"
50
51 /* See comments below for why auto-scanning is a bad idea. */
52 #define SCAN_IICBUS 0
53
54 SYSCTL_NODE(_hw, OID_AUTO, i2c, CTLFLAG_RW, 0, "i2c controls");
55
56 static int
iicbus_probe(device_t dev)57 iicbus_probe(device_t dev)
58 {
59
60 device_set_desc(dev, "Philips I2C bus");
61
62 /* Allow other subclasses to override this driver. */
63 return (BUS_PROBE_GENERIC);
64 }
65
66 #if SCAN_IICBUS
67 static int
iic_probe_device(device_t dev,u_char addr)68 iic_probe_device(device_t dev, u_char addr)
69 {
70 int count;
71 char byte;
72
73 if ((addr & 1) == 0) {
74 /* is device writable? */
75 if (!iicbus_start(dev, (u_char)addr, 0)) {
76 iicbus_stop(dev);
77 return (1);
78 }
79 } else {
80 /* is device readable? */
81 if (!iicbus_block_read(dev, (u_char)addr, &byte, 1, &count))
82 return (1);
83 }
84
85 return (0);
86 }
87 #endif
88
89 /*
90 * We add all the devices which we know about.
91 * The generic attach routine will attach them if they are alive.
92 */
93 int
iicbus_attach_common(device_t dev,u_int bus_freq)94 iicbus_attach_common(device_t dev, u_int bus_freq)
95 {
96 #if SCAN_IICBUS
97 unsigned char addr;
98 #endif
99 struct iicbus_softc *sc = IICBUS_SOFTC(dev);
100 int strict;
101
102 sc->dev = dev;
103 mtx_init(&sc->lock, "iicbus", NULL, MTX_DEF);
104 iicbus_init_frequency(dev, bus_freq);
105 iicbus_reset(dev, IIC_FASTEST, 0, NULL);
106 if (resource_int_value(device_get_name(dev),
107 device_get_unit(dev), "strict", &strict) == 0)
108 sc->strict = strict;
109 else
110 sc->strict = 1;
111
112 /* device probing is meaningless since the bus is supposed to be
113 * hot-plug. Moreover, some I2C chips do not appreciate random
114 * accesses like stop after start to fast, reads for less than
115 * x bytes...
116 */
117 #if SCAN_IICBUS
118 printf("Probing for devices on iicbus%d:", device_get_unit(dev));
119
120 /* probe any devices */
121 for (addr = 16; addr < 240; addr++) {
122 if (iic_probe_device(dev, (u_char)addr)) {
123 printf(" <%x>", addr);
124 }
125 }
126 printf("\n");
127 #endif
128 bus_generic_probe(dev);
129 bus_enumerate_hinted_children(dev);
130 bus_generic_attach(dev);
131 return (0);
132 }
133
134 static int
iicbus_attach(device_t dev)135 iicbus_attach(device_t dev)
136 {
137
138 return (iicbus_attach_common(dev, 0));
139 }
140
141 int
iicbus_detach(device_t dev)142 iicbus_detach(device_t dev)
143 {
144 struct iicbus_softc *sc = IICBUS_SOFTC(dev);
145 int err;
146
147 if ((err = device_delete_children(dev)) != 0)
148 return (err);
149 iicbus_reset(dev, IIC_FASTEST, 0, NULL);
150 mtx_destroy(&sc->lock);
151 return (0);
152 }
153
154 static int
iicbus_print_child(device_t dev,device_t child)155 iicbus_print_child(device_t dev, device_t child)
156 {
157 struct iicbus_ivar *devi = IICBUS_IVAR(child);
158 int retval = 0;
159
160 retval += bus_print_child_header(dev, child);
161 if (devi->addr != 0)
162 retval += printf(" at addr %#x", devi->addr);
163 resource_list_print_type(&devi->rl, "irq", SYS_RES_IRQ, "%jd");
164 retval += bus_print_child_footer(dev, child);
165
166 return (retval);
167 }
168
169 void
iicbus_probe_nomatch(device_t bus,device_t child)170 iicbus_probe_nomatch(device_t bus, device_t child)
171 {
172 struct iicbus_ivar *devi = IICBUS_IVAR(child);
173
174 device_printf(bus, "<unknown card> at addr %#x\n", devi->addr);
175 }
176
177 int
iicbus_child_location(device_t bus,device_t child,struct sbuf * sb)178 iicbus_child_location(device_t bus, device_t child, struct sbuf *sb)
179 {
180 struct iicbus_ivar *devi = IICBUS_IVAR(child);
181
182 sbuf_printf(sb, "addr=%#x", devi->addr);
183 return (0);
184 }
185
186 int
iicbus_child_pnpinfo(device_t bus,device_t child,struct sbuf * sb)187 iicbus_child_pnpinfo(device_t bus, device_t child, struct sbuf *sb)
188 {
189 return (0);
190 }
191
192 int
iicbus_read_ivar(device_t bus,device_t child,int which,uintptr_t * result)193 iicbus_read_ivar(device_t bus, device_t child, int which, uintptr_t *result)
194 {
195 struct iicbus_ivar *devi = IICBUS_IVAR(child);
196
197 switch (which) {
198 default:
199 return (EINVAL);
200 case IICBUS_IVAR_ADDR:
201 *result = devi->addr;
202 break;
203 }
204 return (0);
205 }
206
207 int
iicbus_write_ivar(device_t bus,device_t child,int which,uintptr_t value)208 iicbus_write_ivar(device_t bus, device_t child, int which, uintptr_t value)
209 {
210 struct iicbus_ivar *devi = IICBUS_IVAR(child);
211
212 switch (which) {
213 default:
214 return (EINVAL);
215 case IICBUS_IVAR_ADDR:
216 if (devi->addr != 0)
217 return (EINVAL);
218 devi->addr = value;
219 }
220 return (0);
221 }
222
223 device_t
iicbus_add_child_common(device_t dev,u_int order,const char * name,int unit,size_t ivars_size)224 iicbus_add_child_common(device_t dev, u_int order, const char *name, int unit,
225 size_t ivars_size)
226 {
227 device_t child;
228 struct iicbus_ivar *devi;
229
230 child = device_add_child_ordered(dev, order, name, unit);
231 if (child == NULL)
232 return (child);
233 devi = malloc(ivars_size, M_DEVBUF, M_NOWAIT | M_ZERO);
234 if (devi == NULL) {
235 device_delete_child(dev, child);
236 return (0);
237 }
238 resource_list_init(&devi->rl);
239 device_set_ivars(child, devi);
240 return (child);
241 }
242
243 static device_t
iicbus_add_child(device_t dev,u_int order,const char * name,int unit)244 iicbus_add_child(device_t dev, u_int order, const char *name, int unit)
245 {
246
247 return (iicbus_add_child_common(
248 dev, order, name, unit, sizeof(struct iicbus_ivar)));
249 }
250
251 static void
iicbus_child_deleted(device_t dev,device_t child)252 iicbus_child_deleted(device_t dev, device_t child)
253 {
254 struct iicbus_ivar *devi;
255
256 devi = device_get_ivars(child);
257 if (devi == NULL)
258 return;
259 resource_list_free(&devi->rl);
260 free(devi, M_DEVBUF);
261 }
262
263 static void
iicbus_hinted_child(device_t bus,const char * dname,int dunit)264 iicbus_hinted_child(device_t bus, const char *dname, int dunit)
265 {
266 device_t child;
267 int irq;
268 struct iicbus_ivar *devi;
269
270 child = BUS_ADD_CHILD(bus, 0, dname, dunit);
271 devi = IICBUS_IVAR(child);
272 resource_int_value(dname, dunit, "addr", &devi->addr);
273 if (resource_int_value(dname, dunit, "irq", &irq) == 0) {
274 if (bus_set_resource(child, SYS_RES_IRQ, 0, irq, 1) != 0)
275 device_printf(bus,
276 "warning: bus_set_resource() failed\n");
277 }
278 }
279
280 static struct resource_list *
iicbus_get_resource_list(device_t bus __unused,device_t child)281 iicbus_get_resource_list(device_t bus __unused, device_t child)
282 {
283 struct iicbus_ivar *devi;
284
285 devi = IICBUS_IVAR(child);
286 return (&devi->rl);
287 }
288
289 int
iicbus_generic_intr(device_t dev,int event,char * buf)290 iicbus_generic_intr(device_t dev, int event, char *buf)
291 {
292
293 return (0);
294 }
295
296 int
iicbus_null_callback(device_t dev,int index,caddr_t data)297 iicbus_null_callback(device_t dev, int index, caddr_t data)
298 {
299
300 return (0);
301 }
302
303 int
iicbus_null_repeated_start(device_t dev,u_char addr)304 iicbus_null_repeated_start(device_t dev, u_char addr)
305 {
306
307 return (IIC_ENOTSUPP);
308 }
309
310 void
iicbus_init_frequency(device_t dev,u_int bus_freq)311 iicbus_init_frequency(device_t dev, u_int bus_freq)
312 {
313 struct iicbus_softc *sc = IICBUS_SOFTC(dev);
314
315 /*
316 * If a bus frequency value was passed in, use it. Otherwise initialize
317 * it first to the standard i2c 100KHz frequency, then override that
318 * from a hint if one exists.
319 */
320 if (bus_freq > 0)
321 sc->bus_freq = bus_freq;
322 else {
323 sc->bus_freq = 100000;
324 resource_int_value(device_get_name(dev), device_get_unit(dev),
325 "frequency", (int *)&sc->bus_freq);
326 }
327 /*
328 * Set up the sysctl that allows the bus frequency to be changed.
329 * It is flagged as a tunable so that the user can set the value in
330 * loader(8), and that will override any other setting from any source.
331 * The sysctl tunable/value is the one most directly controlled by the
332 * user and thus the one that always takes precedence.
333 */
334 SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
335 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
336 OID_AUTO, "frequency", CTLFLAG_RWTUN, &sc->bus_freq,
337 sc->bus_freq, "Bus frequency in Hz");
338 }
339
340 static u_int
iicbus_get_frequency(device_t dev,u_char speed)341 iicbus_get_frequency(device_t dev, u_char speed)
342 {
343 struct iicbus_softc *sc = IICBUS_SOFTC(dev);
344
345 /*
346 * If the frequency has not been configured for the bus, or the request
347 * is specifically for SLOW speed, use the standard 100KHz rate, else
348 * use the configured bus speed.
349 */
350 if (sc->bus_freq == 0 || speed == IIC_SLOW)
351 return (100000);
352 return (sc->bus_freq);
353 }
354
355 static device_method_t iicbus_methods[] = {
356 /* device interface */
357 DEVMETHOD(device_probe, iicbus_probe),
358 DEVMETHOD(device_attach, iicbus_attach),
359 DEVMETHOD(device_detach, iicbus_detach),
360 DEVMETHOD(device_suspend, bus_generic_suspend),
361 DEVMETHOD(device_resume, bus_generic_resume),
362
363 /* bus interface */
364 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
365 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
366 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
367 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
368 DEVMETHOD(bus_adjust_resource, bus_generic_adjust_resource),
369 DEVMETHOD(bus_alloc_resource, bus_generic_rl_alloc_resource),
370 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource),
371 DEVMETHOD(bus_release_resource, bus_generic_rl_release_resource),
372 DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource),
373 DEVMETHOD(bus_get_resource_list, iicbus_get_resource_list),
374 DEVMETHOD(bus_add_child, iicbus_add_child),
375 DEVMETHOD(bus_child_deleted, iicbus_child_deleted),
376 DEVMETHOD(bus_print_child, iicbus_print_child),
377 DEVMETHOD(bus_probe_nomatch, iicbus_probe_nomatch),
378 DEVMETHOD(bus_read_ivar, iicbus_read_ivar),
379 DEVMETHOD(bus_write_ivar, iicbus_write_ivar),
380 DEVMETHOD(bus_child_pnpinfo, iicbus_child_pnpinfo),
381 DEVMETHOD(bus_child_location, iicbus_child_location),
382 DEVMETHOD(bus_hinted_child, iicbus_hinted_child),
383
384 /* iicbus interface */
385 DEVMETHOD(iicbus_transfer, iicbus_transfer),
386 DEVMETHOD(iicbus_get_frequency, iicbus_get_frequency),
387
388 DEVMETHOD_END
389 };
390
391 driver_t iicbus_driver = {
392 "iicbus",
393 iicbus_methods,
394 sizeof(struct iicbus_softc),
395 };
396
397 MODULE_VERSION(iicbus, IICBUS_MODVER);
398 DRIVER_MODULE(iicbus, iichb, iicbus_driver, 0, 0);
399