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.10 2006/09/10 01:26:32 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 "iiconf.h" 44 #include "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 dev_ops iic_ops = { 97 { "iic", CDEV_MAJOR, 0 }, 98 .d_open = iicopen, 99 .d_close = iicclose, 100 .d_read = iicread, 101 .d_write = iicwrite, 102 .d_ioctl = iicioctl, 103 }; 104 105 /* 106 * iicprobe() 107 */ 108 static int 109 iic_probe(device_t dev) 110 { 111 struct iic_softc *sc = (struct iic_softc *)device_get_softc(dev); 112 113 sc->sc_addr = iicbus_get_addr(dev); 114 115 /* XXX detect chip with start/stop conditions */ 116 117 return (0); 118 } 119 120 /* 121 * iicattach() 122 */ 123 static int 124 iic_attach(device_t dev) 125 { 126 dev_ops_add(&iic_ops, -1, device_get_unit(dev)); 127 make_dev(&iic_ops, device_get_unit(dev), /* XXX cleanup */ 128 UID_ROOT, GID_WHEEL, 129 0600, "iic%d", device_get_unit(dev)); 130 return (0); 131 } 132 133 static int 134 iicopen (struct dev_open_args *ap) 135 { 136 cdev_t dev = ap->a_head.a_dev; 137 struct iic_softc *sc = IIC_SOFTC(minor(dev)); 138 139 if (!sc) 140 return (EINVAL); 141 142 if (sc->sc_count > 0) 143 return (EBUSY); 144 145 sc->sc_count++; 146 147 return (0); 148 } 149 150 static int 151 iicclose(struct dev_close_args *ap) 152 { 153 cdev_t dev = ap->a_head.a_dev; 154 struct iic_softc *sc = IIC_SOFTC(minor(dev)); 155 156 if (!sc) 157 return (EINVAL); 158 159 if (!sc->sc_count) 160 return (EINVAL); 161 162 sc->sc_count--; 163 164 if (sc->sc_count < 0) 165 panic("%s: iic_count < 0!", __func__); 166 167 return (0); 168 } 169 170 static int 171 iicwrite(struct dev_write_args *ap) 172 { 173 cdev_t dev = ap->a_head.a_dev; 174 struct uio *uio = ap->a_uio; 175 device_t iicdev = IIC_DEVICE(minor(dev)); 176 struct iic_softc *sc = IIC_SOFTC(minor(dev)); 177 int sent, error, count; 178 179 if (!sc || !iicdev) 180 return (EINVAL); 181 182 if (sc->sc_count == 0) 183 return (EINVAL); 184 185 if ((error = iicbus_request_bus(device_get_parent(iicdev), iicdev, IIC_DONTWAIT))) 186 return (error); 187 188 count = min(uio->uio_resid, BUFSIZE); 189 uiomove(sc->sc_buffer, count, uio); 190 191 error = iicbus_block_write(device_get_parent(iicdev), sc->sc_addr, 192 sc->sc_buffer, count, &sent); 193 194 iicbus_release_bus(device_get_parent(iicdev), iicdev); 195 196 return(error); 197 } 198 199 static int 200 iicread(struct dev_read_args *ap) 201 { 202 cdev_t dev = ap->a_head.a_dev; 203 struct uio *uio = ap->a_uio; 204 device_t iicdev = IIC_DEVICE(minor(dev)); 205 struct iic_softc *sc = IIC_SOFTC(minor(dev)); 206 int len, error = 0; 207 int bufsize; 208 209 if (!sc || !iicdev) 210 return (EINVAL); 211 212 if (sc->sc_count == 0) 213 return (EINVAL); 214 215 if ((error = iicbus_request_bus(device_get_parent(iicdev), iicdev, IIC_DONTWAIT))) 216 return (error); 217 218 /* max amount of data to read */ 219 len = min(uio->uio_resid, BUFSIZE); 220 221 if ((error = iicbus_block_read(device_get_parent(iicdev), sc->sc_addr, 222 sc->sc_inbuf, len, &bufsize))) 223 return (error); 224 225 if (bufsize > uio->uio_resid) 226 panic("%s: too much data read!", __func__); 227 228 iicbus_release_bus(device_get_parent(iicdev), iicdev); 229 230 return (uiomove(sc->sc_inbuf, bufsize, uio)); 231 } 232 233 static int 234 iicioctl(struct dev_ioctl_args *ap) 235 { 236 cdev_t dev = ap->a_head.a_dev; 237 device_t iicdev = IIC_DEVICE(minor(dev)); 238 struct iic_softc *sc = IIC_SOFTC(minor(dev)); 239 device_t parent = device_get_parent(iicdev); 240 struct iiccmd *s = (struct iiccmd *)ap->a_data; 241 int error, count; 242 243 if (!sc) 244 return (EINVAL); 245 246 if ((error = iicbus_request_bus(device_get_parent(iicdev), iicdev, 247 (ap->a_fflag & O_NONBLOCK) ? IIC_DONTWAIT : 248 (IIC_WAIT | IIC_INTR)))) 249 return (error); 250 251 switch (ap->a_cmd) { 252 case I2CSTART: 253 error = iicbus_start(parent, s->slave, 0); 254 break; 255 256 case I2CSTOP: 257 error = iicbus_stop(parent); 258 break; 259 260 case I2CRSTCARD: 261 error = iicbus_reset(parent, 0, 0, NULL); 262 break; 263 264 case I2CWRITE: 265 error = iicbus_write(parent, s->buf, s->count, &count, 0); 266 break; 267 268 case I2CREAD: 269 error = iicbus_read(parent, s->buf, s->count, &count, s->last, 0); 270 break; 271 272 default: 273 error = ENODEV; 274 } 275 276 iicbus_release_bus(device_get_parent(iicdev), iicdev); 277 278 return (error); 279 } 280 281 DRIVER_MODULE(iic, iicbus, iic_driver, iic_devclass, 0, 0); 282