iiconf.c revision 38775
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.1.1.11 1998/08/29 17:02:05 son 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 */ 43void 44iicbus_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 */ 60device_t 61iicbus_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 if (child) 69 device_set_desc(child, "Philips I2C bus"); 70 71 return (child); 72} 73 74/* 75 * iicbus_request_bus() 76 * 77 * Allocate the device to perform transfers. 78 * 79 * how : IIC_WAIT or IIC_DONTWAIT 80 */ 81int 82iicbus_request_bus(device_t bus, device_t dev, int how) 83{ 84 struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus); 85 int s, error = 0; 86 87 while (!error) { 88 s = splhigh(); 89 if (sc->owner) { 90 splx(s); 91 92 switch (how) { 93 case (IIC_WAIT | IIC_INTR): 94 error = tsleep(sc, IICPRI|PCATCH, "iicreq", 0); 95 break; 96 97 case (IIC_WAIT | IIC_NOINTR): 98 error = tsleep(sc, IICPRI, "iicreq", 0); 99 break; 100 101 default: 102 return (EWOULDBLOCK); 103 break; 104 } 105 106 } else { 107 sc->owner = dev; 108 109 splx(s); 110 return (0); 111 } 112 } 113 114 return (error); 115} 116 117/* 118 * iicbus_release_bus() 119 * 120 * Release the device allocated with iicbus_request_dev() 121 */ 122int 123iicbus_release_bus(device_t bus, device_t dev) 124{ 125 struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus); 126 int s; 127 128 s = splhigh(); 129 if (sc->owner != dev) { 130 splx(s); 131 return (EACCES); 132 } 133 134 sc->owner = 0; 135 splx(s); 136 137 /* wakeup waiting processes */ 138 wakeup(sc); 139 140 return (0); 141} 142 143/* 144 * iicbus_block_write() 145 * 146 * Write a block of data to slave ; start/stop protocol managed 147 */ 148int 149iicbus_block_write(device_t bus, u_char slave, char *buf, int len, int *sent) 150{ 151 u_char addr = slave & ~LSB; 152 int error; 153 154 if ((error = iicbus_start(bus, addr))) 155 return (error); 156 157 error = iicbus_write(bus, buf, len, sent); 158 159 iicbus_stop(bus); 160 161 return (error); 162} 163 164/* 165 * iicbus_block_read() 166 * 167 * Read a block of data from slave ; start/stop protocol managed 168 */ 169int 170iicbus_block_read(device_t bus, u_char slave, char *buf, int len, int *read) 171{ 172 u_char addr = slave | LSB; 173 int error; 174 175 if ((error = iicbus_start(bus, addr))) 176 return (error); 177 178 error = iicbus_read(bus, buf, len, read); 179 180 /* STOP condition sent at adapter level */ 181 182 return (error); 183} 184 185/* 186 * iicbus_get_addr() 187 * 188 * Get the I2C 7 bits address of the device 189 */ 190u_char 191iicbus_get_addr(device_t dev) 192{ 193 u_long addr; 194 device_t parent = device_get_parent(dev); 195 196 BUS_READ_IVAR(parent, dev, IICBUS_IVAR_ADDR, &addr); 197 198 return ((u_char)addr); 199} 200 201u_char 202iicbus_get_own_address(device_t bus) 203{ 204 struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus); 205 206 return (sc->ownaddr); 207} 208