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