iic.c revision 46635
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 * $Id: iic.c,v 1.7 1999/02/13 18:01:55 nsouch Exp $ 27 * 28 */ 29#include <sys/param.h> 30#include <sys/kernel.h> 31#include <sys/systm.h> 32#include <sys/module.h> 33#include <sys/bus.h> 34#include <sys/conf.h> 35#include <sys/buf.h> 36#include <sys/uio.h> 37#include <sys/malloc.h> 38#include <sys/fcntl.h> 39 40#include <machine/clock.h> 41 42#include <dev/iicbus/iiconf.h> 43#include <dev/iicbus/iicbus.h> 44 45#include <machine/iic.h> 46 47#include "iicbus_if.h" 48 49#define BUFSIZE 1024 50 51struct iic_softc { 52 53 u_char sc_addr; /* address on iicbus */ 54 int sc_count; /* >0 if device opened */ 55 56 char sc_buffer[BUFSIZE]; /* output buffer */ 57 char sc_inbuf[BUFSIZE]; /* input buffer */ 58}; 59 60#define IIC_SOFTC(unit) \ 61 ((struct iic_softc *)devclass_get_softc(iic_devclass, (unit))) 62 63#define IIC_DEVICE(unit) \ 64 (devclass_get_device(iic_devclass, (unit))) 65 66static int iic_probe(device_t); 67static int iic_attach(device_t); 68 69static devclass_t iic_devclass; 70 71static device_method_t iic_methods[] = { 72 /* device interface */ 73 DEVMETHOD(device_probe, iic_probe), 74 DEVMETHOD(device_attach, iic_attach), 75 76 /* iicbus interface */ 77 DEVMETHOD(iicbus_intr, iicbus_generic_intr), 78 79 { 0, 0 } 80}; 81 82static driver_t iic_driver = { 83 "iic", 84 iic_methods, 85 DRIVER_TYPE_MISC, 86 sizeof(struct iic_softc), 87}; 88 89static d_open_t iicopen; 90static d_close_t iicclose; 91static d_write_t iicwrite; 92static d_read_t iicread; 93static d_ioctl_t iicioctl; 94 95#define CDEV_MAJOR 105 96static struct cdevsw iic_cdevsw = 97 { iicopen, iicclose, iicread, iicwrite, /*105*/ 98 iicioctl, nullstop, nullreset, nodevtotty, /*iic*/ 99 seltrue, nommap, nostrat, "iic", NULL, -1 }; 100 101/* 102 * iicprobe() 103 */ 104static int 105iic_probe(device_t dev) 106{ 107 struct iic_softc *sc = (struct iic_softc *)device_get_softc(dev); 108 109 sc->sc_addr = iicbus_get_addr(dev); 110 111 /* XXX detect chip with start/stop conditions */ 112 113 return (0); 114} 115 116/* 117 * iicattach() 118 */ 119static int 120iic_attach(device_t dev) 121{ 122 return (0); 123} 124 125static int 126iicopen (dev_t dev, int flags, int fmt, struct proc *p) 127{ 128 struct iic_softc *sc = IIC_SOFTC(minor(dev)); 129 130 if (!sc) 131 return (EINVAL); 132 133 if (sc->sc_count > 0) 134 return (EBUSY); 135 136 sc->sc_count++; 137 138 return (0); 139} 140 141static int 142iicclose(dev_t dev, int flags, int fmt, struct proc *p) 143{ 144 struct iic_softc *sc = IIC_SOFTC(minor(dev)); 145 146 if (!sc) 147 return (EINVAL); 148 149 if (!sc->sc_count) 150 return (EINVAL); 151 152 sc->sc_count--; 153 154 if (sc->sc_count < 0) 155 panic("%s: iic_count < 0!", __FUNCTION__); 156 157 return (0); 158} 159 160static int 161iicwrite(dev_t dev, struct uio * uio, int ioflag) 162{ 163 device_t iicdev = IIC_DEVICE(minor(dev)); 164 struct iic_softc *sc = IIC_SOFTC(minor(dev)); 165 int sent, error, count; 166 167 if (!sc || !iicdev) 168 return (EINVAL); 169 170 if (sc->sc_count == 0) 171 return (EINVAL); 172 173 if ((error = iicbus_request_bus(device_get_parent(iicdev), iicdev, IIC_DONTWAIT))) 174 return (error); 175 176 count = min(uio->uio_resid, BUFSIZE); 177 uiomove(sc->sc_buffer, count, uio); 178 179 error = iicbus_block_write(device_get_parent(iicdev), sc->sc_addr, 180 sc->sc_buffer, count, &sent); 181 182 iicbus_release_bus(device_get_parent(iicdev), iicdev); 183 184 return(error); 185} 186 187static int 188iicread(dev_t dev, struct uio * uio, int ioflag) 189{ 190 device_t iicdev = IIC_DEVICE(minor(dev)); 191 struct iic_softc *sc = IIC_SOFTC(minor(dev)); 192 int len, error = 0; 193 int bufsize; 194 195 if (!sc || !iicdev) 196 return (EINVAL); 197 198 if (sc->sc_count == 0) 199 return (EINVAL); 200 201 if ((error = iicbus_request_bus(device_get_parent(iicdev), iicdev, IIC_DONTWAIT))) 202 return (error); 203 204 /* max amount of data to read */ 205 len = min(uio->uio_resid, BUFSIZE); 206 207 if ((error = iicbus_block_read(device_get_parent(iicdev), sc->sc_addr, 208 sc->sc_inbuf, len, &bufsize))) 209 return (error); 210 211 if (bufsize > uio->uio_resid) 212 panic("%s: too much data read!", __FUNCTION__); 213 214 iicbus_release_bus(device_get_parent(iicdev), iicdev); 215 216 return (uiomove(sc->sc_inbuf, bufsize, uio)); 217} 218 219static int 220iicioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct proc *p) 221{ 222 device_t iicdev = IIC_DEVICE(minor(dev)); 223 struct iic_softc *sc = IIC_SOFTC(minor(dev)); 224 device_t parent = device_get_parent(iicdev); 225 struct iiccmd *s = (struct iiccmd *)data; 226 int error, count; 227 228 if (!sc) 229 return (EINVAL); 230 231 if ((error = iicbus_request_bus(device_get_parent(iicdev), iicdev, 232 (flags & O_NONBLOCK) ? IIC_DONTWAIT : 233 (IIC_WAIT | IIC_INTR)))) 234 return (error); 235 236 switch (cmd) { 237 case I2CSTART: 238 error = iicbus_start(parent, s->slave, 0); 239 break; 240 241 case I2CSTOP: 242 error = iicbus_stop(parent); 243 break; 244 245 case I2CRSTCARD: 246 error = iicbus_reset(parent, 0, 0, NULL); 247 break; 248 249 case I2CWRITE: 250 error = iicbus_write(parent, s->buf, s->count, &count, 0); 251 break; 252 253 case I2CREAD: 254 error = iicbus_read(parent, s->buf, s->count, &count, s->last, 0); 255 break; 256 257 default: 258 error = ENODEV; 259 } 260 261 iicbus_release_bus(device_get_parent(iicdev), iicdev); 262 263 return (error); 264} 265 266static int iic_devsw_installed = 0; 267 268static void 269iic_drvinit(void *unused) 270{ 271 dev_t dev; 272 273 if( ! iic_devsw_installed ) { 274 dev = makedev(CDEV_MAJOR,0); 275 cdevsw_add(&dev,&iic_cdevsw,NULL); 276 iic_devsw_installed = 1; 277 } 278} 279 280DEV_DRIVER_MODULE(iic, iicbus, iic_driver, iic_devclass, CDEV_MAJOR, 281 NODEV, iic_cdevsw, 0, 0); 282 283SYSINIT(iicdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,iic_drvinit,NULL) 284