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: iiconf.c,v 1.6 1999/02/06 10:56:09 roger Exp $ 27 * 28 */ 29 #include <sys/param.h> 30 #include <sys/systm.h> 31 #include <sys/kernel.h> 32 #include <sys/malloc.h> 33 #include <sys/module.h> 34 #include <sys/bus.h> 35 36 #include <dev/iicbus/iiconf.h> 37 #include <dev/iicbus/iicbus.h> 38 #include "iicbus_if.h" 39 40 /* 41 * iicbus_intr() 42 */ 43 void 44 iicbus_intr(device_t bus, int event, char *buf) 45 { 46 struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus); 47 48 /* call owner's intr routine */ 49 if (sc->owner) 50 IICBUS_INTR(sc->owner, event, buf); 51 52 return; 53 } 54 55 /* 56 * iicbus_alloc_bus() 57 * 58 * Allocate a new bus connected to the given parent device 59 */ 60 device_t 61 iicbus_alloc_bus(device_t parent) 62 { 63 device_t child; 64 65 /* add the bus to the parent */ 66 child = device_add_child(parent, "iicbus", -1, NULL); 67 68 return (child); 69 } 70 71 static int 72 iicbus_poll(struct iicbus_softc *sc, int how) 73 { 74 int error; 75 76 switch (how) { 77 case (IIC_WAIT | IIC_INTR): 78 error = tsleep(sc, IICPRI|PCATCH, "iicreq", 0); 79 break; 80 81 case (IIC_WAIT | IIC_NOINTR): 82 error = tsleep(sc, IICPRI, "iicreq", 0); 83 break; 84 85 default: 86 return (EWOULDBLOCK); 87 break; 88 } 89 90 return (error); 91 } 92 93 /* 94 * iicbus_request_bus() 95 * 96 * Allocate the device to perform transfers. 97 * 98 * how : IIC_WAIT or IIC_DONTWAIT 99 */ 100 int 101 iicbus_request_bus(device_t bus, device_t dev, int how) 102 { 103 struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus); 104 int s, error = 0; 105 106 /* first, ask the underlying layers if the request is ok */ 107 error = IICBUS_CALLBACK(device_get_parent(bus), IIC_REQUEST_BUS, 108 (caddr_t)&how); 109 110 while (!error) { 111 s = splhigh(); 112 if (sc->owner && sc->owner != dev) { 113 splx(s); 114 115 error = iicbus_poll(sc, how); 116 } else { 117 sc->owner = dev; 118 119 splx(s); 120 return (0); 121 } 122 123 /* free any allocated resource */ 124 if (error) 125 IICBUS_CALLBACK(device_get_parent(bus), IIC_RELEASE_BUS, 126 (caddr_t)&how); 127 } 128 129 return (error); 130 } 131 132 /* 133 * iicbus_release_bus() 134 * 135 * Release the device allocated with iicbus_request_dev() 136 */ 137 int 138 iicbus_release_bus(device_t bus, device_t dev) 139 { 140 struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus); 141 int s, error; 142 143 /* first, ask the underlying layers if the release is ok */ 144 error = IICBUS_CALLBACK(device_get_parent(bus), IIC_RELEASE_BUS, NULL); 145 146 if (error) 147 return (error); 148 149 s = splhigh(); 150 if (sc->owner != dev) { 151 splx(s); 152 return (EACCES); 153 } 154 155 sc->owner = 0; 156 splx(s); 157 158 /* wakeup waiting processes */ 159 wakeup(sc); 160 161 return (0); 162 } 163 164 /* 165 * iicbus_started() 166 * 167 * Test if the iicbus is started by the controller 168 */ 169 int 170 iicbus_started(device_t bus) 171 { 172 struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus); 173 174 return (sc->started); 175 } 176 177 /* 178 * iicbus_start() 179 * 180 * Send start condition to the slave addressed by 'slave' 181 */ 182 int 183 iicbus_start(device_t bus, u_char slave, int timeout) 184 { 185 struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus); 186 int error = 0; 187 188 if (sc->started) 189 return (EINVAL); /* bus already started */ 190 191 if (!(error = IICBUS_START(device_get_parent(bus), slave, timeout))) 192 sc->started = slave; 193 else 194 sc->started = 0; 195 196 return (error); 197 } 198 199 /* 200 * iicbus_repeated_start() 201 * 202 * Send start condition to the slave addressed by 'slave' 203 */ 204 int 205 iicbus_repeated_start(device_t bus, u_char slave, int timeout) 206 { 207 struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus); 208 int error = 0; 209 210 if (!sc->started) 211 return (EINVAL); /* bus should have been already started */ 212 213 if (!(error = IICBUS_REPEATED_START(device_get_parent(bus), slave, timeout))) 214 sc->started = slave; 215 else 216 sc->started = 0; 217 218 return (error); 219 } 220 221 /* 222 * iicbus_stop() 223 * 224 * Send stop condition to the bus 225 */ 226 int 227 iicbus_stop(device_t bus) 228 { 229 struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus); 230 int error = 0; 231 232 if (!sc->started) 233 return (EINVAL); /* bus not started */ 234 235 error = IICBUS_STOP(device_get_parent(bus)); 236 237 /* refuse any further access */ 238 sc->started = 0; 239 240 return (error); 241 } 242 243 /* 244 * iicbus_write() 245 * 246 * Write a block of data to the slave previously started by 247 * iicbus_start() call 248 */ 249 int 250 iicbus_write(device_t bus, char *buf, int len, int *sent, int timeout) 251 { 252 struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus); 253 254 /* a slave must have been started with the appropriate address */ 255 if (!sc->started || (sc->started & LSB)) 256 return (EINVAL); 257 258 return (IICBUS_WRITE(device_get_parent(bus), buf, len, sent, timeout)); 259 } 260 261 /* 262 * iicbus_read() 263 * 264 * Read a block of data from the slave previously started by 265 * iicbus_read() call 266 */ 267 int 268 iicbus_read(device_t bus, char *buf, int len, int *read, int last, int delay) 269 { 270 struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus); 271 272 /* a slave must have been started with the appropriate address */ 273 if (!sc->started || !(sc->started & LSB)) 274 return (EINVAL); 275 276 return (IICBUS_READ(device_get_parent(bus), buf, len, read, last, delay)); 277 } 278 279 /* 280 * iicbus_write_byte() 281 * 282 * Write a byte to the slave previously started by iicbus_start() call 283 */ 284 int 285 iicbus_write_byte(device_t bus, char byte, int timeout) 286 { 287 char data = byte; 288 int sent; 289 290 return (iicbus_write(bus, &data, 1, &sent, timeout)); 291 } 292 293 /* 294 * iicbus_read_byte() 295 * 296 * Read a byte from the slave previously started by iicbus_start() call 297 */ 298 int 299 iicbus_read_byte(device_t bus, char *byte, int timeout) 300 { 301 int read; 302 303 return (iicbus_read(bus, byte, 1, &read, IIC_LAST_READ, timeout)); 304 } 305 306 /* 307 * iicbus_block_write() 308 * 309 * Write a block of data to slave ; start/stop protocol managed 310 */ 311 int 312 iicbus_block_write(device_t bus, u_char slave, char *buf, int len, int *sent) 313 { 314 u_char addr = slave & ~LSB; 315 int error; 316 317 if ((error = iicbus_start(bus, addr, 0))) 318 return (error); 319 320 error = iicbus_write(bus, buf, len, sent, 0); 321 322 iicbus_stop(bus); 323 324 return (error); 325 } 326 327 /* 328 * iicbus_block_read() 329 * 330 * Read a block of data from slave ; start/stop protocol managed 331 */ 332 int 333 iicbus_block_read(device_t bus, u_char slave, char *buf, int len, int *read) 334 { 335 u_char addr = slave | LSB; 336 int error; 337 338 if ((error = iicbus_start(bus, addr, 0))) 339 return (error); 340 341 error = iicbus_read(bus, buf, len, read, IIC_LAST_READ, 0); 342 343 iicbus_stop(bus); 344 345 return (error); 346 } 347 348 /* 349 * iicbus_get_addr() 350 * 351 * Get the I2C 7 bits address of the device 352 */ 353 u_char 354 iicbus_get_addr(device_t dev) 355 { 356 uintptr_t addr; 357 device_t parent = device_get_parent(dev); 358 359 BUS_READ_IVAR(parent, dev, IICBUS_IVAR_ADDR, &addr); 360 361 return ((u_char)addr); 362 } 363 364