2 * Copyright (c) 1998, 2001 Nicolas Souchu
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/param.h>
30 #include <sys/kernel.h>
31 #include <sys/systm.h>
32 #include <sys/module.h>
36 #include <sys/fcntl.h>
38 #include <dev/iicbus/iiconf.h>
39 #include <dev/iicbus/iicbus.h>
40 #include <dev/iicbus/iic.h>
42 #include "iicbus_if.h"
48 u_char sc_addr; /* 7 bit address on iicbus */
49 int sc_count; /* >0 if device opened */
51 char sc_buffer[BUFSIZE]; /* output buffer */
52 char sc_inbuf[BUFSIZE]; /* input buffer */
57 #define IIC_SOFTC(unit) \
58 ((struct iic_softc *)devclass_get_softc(iic_devclass, (unit)))
60 #define IIC_DEVICE(unit) \
61 (devclass_get_device(iic_devclass, (unit)))
63 static int iic_probe(device_t);
64 static int iic_attach(device_t);
65 static int iic_detach(device_t);
66 static void iic_identify(driver_t *driver, device_t parent);
68 static devclass_t iic_devclass;
70 static device_method_t iic_methods[] = {
71 /* device interface */
72 DEVMETHOD(device_identify, iic_identify),
73 DEVMETHOD(device_probe, iic_probe),
74 DEVMETHOD(device_attach, iic_attach),
75 DEVMETHOD(device_detach, iic_detach),
77 /* iicbus interface */
78 DEVMETHOD(iicbus_intr, iicbus_generic_intr),
83 static driver_t iic_driver = {
86 sizeof(struct iic_softc),
89 static d_open_t iicopen;
90 static d_close_t iicclose;
91 static d_write_t iicwrite;
92 static d_read_t iicread;
93 static d_ioctl_t iicioctl;
95 static struct cdevsw iic_cdevsw = {
96 .d_version = D_VERSION,
97 .d_flags = D_NEEDGIANT,
107 iic_identify(driver_t *driver, device_t parent)
109 BUS_ADD_CHILD(parent, 0, "iic", -1);
113 iic_probe(device_t dev)
115 device_set_desc(dev, "I2C generic I/O");
120 iic_attach(device_t dev)
122 struct iic_softc *sc = (struct iic_softc *)device_get_softc(dev);
127 bzero(sc, sizeof(struct iic_softc));
129 sc->sc_devnode = make_dev(&iic_cdevsw, device_get_unit(dev),
131 0600, "iic%d", device_get_unit(dev));
137 iic_detach(device_t dev)
139 struct iic_softc *sc = (struct iic_softc *)device_get_softc(dev);
142 destroy_dev(sc->sc_devnode);
148 iicopen (dev_t dev, int flags, int fmt, struct thread *td)
150 struct iic_softc *sc = IIC_SOFTC(minor(dev));
155 if (sc->sc_count > 0)
164 iicclose(dev_t dev, int flags, int fmt, struct thread *td)
166 struct iic_softc *sc = IIC_SOFTC(minor(dev));
176 if (sc->sc_count < 0)
177 panic("%s: iic_count < 0!", __func__);
183 iicwrite(dev_t dev, struct uio * uio, int ioflag)
185 device_t iicdev = IIC_DEVICE(minor(dev));
186 struct iic_softc *sc = IIC_SOFTC(minor(dev));
187 int sent, error, count;
189 if (!sc || !iicdev || !sc->sc_addr)
192 if (sc->sc_count == 0)
195 if ((error = iicbus_request_bus(device_get_parent(iicdev), iicdev, IIC_DONTWAIT)))
198 count = min(uio->uio_resid, BUFSIZE);
199 uiomove(sc->sc_buffer, count, uio);
201 error = iicbus_block_write(device_get_parent(iicdev), sc->sc_addr,
202 sc->sc_buffer, count, &sent);
204 iicbus_release_bus(device_get_parent(iicdev), iicdev);
210 iicread(dev_t dev, struct uio * uio, int ioflag)
212 device_t iicdev = IIC_DEVICE(minor(dev));
213 struct iic_softc *sc = IIC_SOFTC(minor(dev));
217 if (!sc || !iicdev || !sc->sc_addr)
220 if (sc->sc_count == 0)
223 if ((error = iicbus_request_bus(device_get_parent(iicdev), iicdev, IIC_DONTWAIT)))
226 /* max amount of data to read */
227 len = min(uio->uio_resid, BUFSIZE);
229 if ((error = iicbus_block_read(device_get_parent(iicdev), sc->sc_addr,
230 sc->sc_inbuf, len, &bufsize)))
233 if (bufsize > uio->uio_resid)
234 panic("%s: too much data read!", __func__);
236 iicbus_release_bus(device_get_parent(iicdev), iicdev);
238 return (uiomove(sc->sc_inbuf, bufsize, uio));
242 iicioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct thread *td)
244 device_t iicdev = IIC_DEVICE(minor(dev));
245 struct iic_softc *sc = IIC_SOFTC(minor(dev));
246 device_t parent = device_get_parent(iicdev);
247 struct iiccmd *s = (struct iiccmd *)data;
253 if ((error = iicbus_request_bus(device_get_parent(iicdev), iicdev,
254 (flags & O_NONBLOCK) ? IIC_DONTWAIT :
255 (IIC_WAIT | IIC_INTR))))
260 error = iicbus_start(parent, s->slave, 0);
263 * Implicitly set the chip addr to the slave addr passed as
264 * parameter. Consequently, start/stop shall be called before
265 * the read or the write of a block.
268 sc->sc_addr = s->slave;
273 error = iicbus_stop(parent);
277 error = iicbus_reset(parent, 0, 0, NULL);
281 error = iicbus_write(parent, s->buf, s->count, &count, 10);
285 error = iicbus_read(parent, s->buf, s->count, &count, s->last, 10);
292 iicbus_release_bus(device_get_parent(iicdev), iicdev);
297 DRIVER_MODULE(iic, iicbus, iic_driver, iic_devclass, 0, 0);
298 MODULE_DEPEND(iic, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER);
299 MODULE_VERSION(iic, 1);