1 /*- 2 * Copyright (c) 1998 Nicolas Souchu 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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. 13 * 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 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD: src/sys/dev/iicbus/iic.c,v 1.18 1999/11/18 05:43:32 peter Exp $ 27 * $DragonFly: src/sys/bus/iicbus/iic.c,v 1.4 2003/07/21 07:57:41 dillon Exp $ 28 * 29 */ 30 #include <sys/param.h> 31 #include <sys/kernel.h> 32 #include <sys/systm.h> 33 #include <sys/module.h> 34 #include <sys/bus.h> 35 #include <sys/conf.h> 36 #include <sys/buf.h> 37 #include <sys/uio.h> 38 #include <sys/malloc.h> 39 #include <sys/fcntl.h> 40 41 #include <machine/clock.h> 42 43 #include <dev/iicbus/iiconf.h> 44 #include <dev/iicbus/iicbus.h> 45 46 #include <machine/iic.h> 47 48 #include "iicbus_if.h" 49 50 #define BUFSIZE 1024 51 52 struct iic_softc { 53 54 u_char sc_addr; /* address on iicbus */ 55 int sc_count; /* >0 if device opened */ 56 57 char sc_buffer[BUFSIZE]; /* output buffer */ 58 char sc_inbuf[BUFSIZE]; /* input buffer */ 59 }; 60 61 #define IIC_SOFTC(unit) \ 62 ((struct iic_softc *)devclass_get_softc(iic_devclass, (unit))) 63 64 #define IIC_DEVICE(unit) \ 65 (devclass_get_device(iic_devclass, (unit))) 66 67 static int iic_probe(device_t); 68 static int iic_attach(device_t); 69 70 static devclass_t iic_devclass; 71 72 static device_method_t iic_methods[] = { 73 /* device interface */ 74 DEVMETHOD(device_probe, iic_probe), 75 DEVMETHOD(device_attach, iic_attach), 76 77 /* iicbus interface */ 78 DEVMETHOD(iicbus_intr, iicbus_generic_intr), 79 80 { 0, 0 } 81 }; 82 83 static driver_t iic_driver = { 84 "iic", 85 iic_methods, 86 sizeof(struct iic_softc), 87 }; 88 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; 94 95 #define CDEV_MAJOR 105 96 static struct cdevsw iic_cdevsw = { 97 /* name */ "iic", 98 /* maj */ CDEV_MAJOR, 99 /* flags */ 0, 100 /* port */ NULL, 101 /* autoq */ 0, 102 103 /* open */ iicopen, 104 /* close */ iicclose, 105 /* read */ iicread, 106 /* write */ iicwrite, 107 /* ioctl */ iicioctl, 108 /* poll */ nopoll, 109 /* mmap */ nommap, 110 /* strategy */ nostrategy, 111 /* dump */ nodump, 112 /* psize */ nopsize 113 }; 114 115 /* 116 * iicprobe() 117 */ 118 static int 119 iic_probe(device_t dev) 120 { 121 struct iic_softc *sc = (struct iic_softc *)device_get_softc(dev); 122 123 sc->sc_addr = iicbus_get_addr(dev); 124 125 /* XXX detect chip with start/stop conditions */ 126 127 return (0); 128 } 129 130 /* 131 * iicattach() 132 */ 133 static int 134 iic_attach(device_t dev) 135 { 136 make_dev(&iic_cdevsw, device_get_unit(dev), /* XXX cleanup */ 137 UID_ROOT, GID_WHEEL, 138 0600, "iic%d", device_get_unit(dev)); 139 return (0); 140 } 141 142 static int 143 iicopen (dev_t dev, int flags, int fmt, struct thread *td) 144 { 145 struct iic_softc *sc = IIC_SOFTC(minor(dev)); 146 147 if (!sc) 148 return (EINVAL); 149 150 if (sc->sc_count > 0) 151 return (EBUSY); 152 153 sc->sc_count++; 154 155 return (0); 156 } 157 158 static int 159 iicclose(dev_t dev, int flags, int fmt, struct thread *td) 160 { 161 struct iic_softc *sc = IIC_SOFTC(minor(dev)); 162 163 if (!sc) 164 return (EINVAL); 165 166 if (!sc->sc_count) 167 return (EINVAL); 168 169 sc->sc_count--; 170 171 if (sc->sc_count < 0) 172 panic("%s: iic_count < 0!", __FUNCTION__); 173 174 return (0); 175 } 176 177 static int 178 iicwrite(dev_t dev, struct uio * uio, int ioflag) 179 { 180 device_t iicdev = IIC_DEVICE(minor(dev)); 181 struct iic_softc *sc = IIC_SOFTC(minor(dev)); 182 int sent, error, count; 183 184 if (!sc || !iicdev) 185 return (EINVAL); 186 187 if (sc->sc_count == 0) 188 return (EINVAL); 189 190 if ((error = iicbus_request_bus(device_get_parent(iicdev), iicdev, IIC_DONTWAIT))) 191 return (error); 192 193 count = min(uio->uio_resid, BUFSIZE); 194 uiomove(sc->sc_buffer, count, uio); 195 196 error = iicbus_block_write(device_get_parent(iicdev), sc->sc_addr, 197 sc->sc_buffer, count, &sent); 198 199 iicbus_release_bus(device_get_parent(iicdev), iicdev); 200 201 return(error); 202 } 203 204 static int 205 iicread(dev_t dev, struct uio * uio, int ioflag) 206 { 207 device_t iicdev = IIC_DEVICE(minor(dev)); 208 struct iic_softc *sc = IIC_SOFTC(minor(dev)); 209 int len, error = 0; 210 int bufsize; 211 212 if (!sc || !iicdev) 213 return (EINVAL); 214 215 if (sc->sc_count == 0) 216 return (EINVAL); 217 218 if ((error = iicbus_request_bus(device_get_parent(iicdev), iicdev, IIC_DONTWAIT))) 219 return (error); 220 221 /* max amount of data to read */ 222 len = min(uio->uio_resid, BUFSIZE); 223 224 if ((error = iicbus_block_read(device_get_parent(iicdev), sc->sc_addr, 225 sc->sc_inbuf, len, &bufsize))) 226 return (error); 227 228 if (bufsize > uio->uio_resid) 229 panic("%s: too much data read!", __FUNCTION__); 230 231 iicbus_release_bus(device_get_parent(iicdev), iicdev); 232 233 return (uiomove(sc->sc_inbuf, bufsize, uio)); 234 } 235 236 static int 237 iicioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct thread *td) 238 { 239 device_t iicdev = IIC_DEVICE(minor(dev)); 240 struct iic_softc *sc = IIC_SOFTC(minor(dev)); 241 device_t parent = device_get_parent(iicdev); 242 struct iiccmd *s = (struct iiccmd *)data; 243 int error, count; 244 245 if (!sc) 246 return (EINVAL); 247 248 if ((error = iicbus_request_bus(device_get_parent(iicdev), iicdev, 249 (flags & O_NONBLOCK) ? IIC_DONTWAIT : 250 (IIC_WAIT | IIC_INTR)))) 251 return (error); 252 253 switch (cmd) { 254 case I2CSTART: 255 error = iicbus_start(parent, s->slave, 0); 256 break; 257 258 case I2CSTOP: 259 error = iicbus_stop(parent); 260 break; 261 262 case I2CRSTCARD: 263 error = iicbus_reset(parent, 0, 0, NULL); 264 break; 265 266 case I2CWRITE: 267 error = iicbus_write(parent, s->buf, s->count, &count, 0); 268 break; 269 270 case I2CREAD: 271 error = iicbus_read(parent, s->buf, s->count, &count, s->last, 0); 272 break; 273 274 default: 275 error = ENODEV; 276 } 277 278 iicbus_release_bus(device_get_parent(iicdev), iicdev); 279 280 return (error); 281 } 282 283 DRIVER_MODULE(iic, iicbus, iic_driver, iic_devclass, 0, 0); 284