1 /*- 2 * Copyright (c) 1998, 2001 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/iicbus.c,v 1.29 2009/02/10 22:50:23 imp Exp $ 27 */ 28 29 /* 30 * Autoconfiguration and support routines for the Philips serial I2C bus 31 */ 32 33 #include <sys/param.h> 34 #include <sys/systm.h> 35 #include <sys/kernel.h> 36 #include <sys/module.h> 37 #include <sys/bus.h> 38 39 #include <bus/iicbus/iiconf.h> 40 #include <bus/iicbus/iicbus.h> 41 42 #include "iicbus_if.h" 43 44 #define DEVTOIICBUS(dev) ((struct iicbus_device*)device_get_ivars(dev)) 45 46 devclass_t iicbus_devclass; 47 48 /* See comments below for why auto-scanning is a bad idea. */ 49 #define SCAN_IICBUS 0 50 51 /* 52 * Device methods 53 */ 54 static int iicbus_probe(device_t); 55 static int iicbus_attach(device_t); 56 static int iicbus_detach(device_t); 57 static int iicbus_add_child(device_t dev, int order, const char *name, int unit); 58 59 static device_method_t iicbus_methods[] = { 60 /* device interface */ 61 DEVMETHOD(device_probe, iicbus_probe), 62 DEVMETHOD(device_attach, iicbus_attach), 63 DEVMETHOD(device_detach, iicbus_detach), 64 65 /* bus interface */ 66 DEVMETHOD(bus_add_child, iicbus_add_child), 67 DEVMETHOD(bus_driver_added, bus_generic_driver_added), 68 DEVMETHOD(bus_print_child, bus_generic_print_child), 69 70 DEVMETHOD(iicbus_transfer, iicbus_transfer_gen), 71 72 { 0, 0 } 73 }; 74 75 driver_t iicbus_driver = { 76 "iicbus", 77 iicbus_methods, 78 sizeof(struct iicbus_softc), 79 }; 80 81 static int 82 iicbus_probe(device_t dev) 83 { 84 85 device_set_desc(dev, "Philips I2C bus"); 86 87 /* Allow other subclasses to override this driver. */ 88 return (BUS_PROBE_GENERIC); 89 } 90 91 #if SCAN_IICBUS 92 static int 93 iic_probe_device(device_t dev, u_char addr) 94 { 95 int count; 96 char byte; 97 98 if ((addr & 1) == 0) { 99 /* is device writable? */ 100 if (!iicbus_start(dev, (u_char)addr, 0)) { 101 iicbus_stop(dev); 102 return (1); 103 } 104 } else { 105 /* is device readable? */ 106 if (!iicbus_block_read(dev, (u_char)addr, &byte, 1, &count)) 107 return (1); 108 } 109 110 return (0); 111 } 112 #endif 113 114 /* 115 * We add all the devices which we know about. 116 * The generic attach routine will attach them if they are alive. 117 */ 118 static int 119 iicbus_attach(device_t dev) 120 { 121 #if SCAN_IICBUS 122 unsigned char addr; 123 #endif 124 125 iicbus_reset(dev, IIC_FASTEST, 0, NULL); 126 127 /* device probing is meaningless since the bus is supposed to be 128 * hot-plug. Moreover, some I2C chips do not appreciate random 129 * accesses like stop after start to fast, reads for less than 130 * x bytes... 131 */ 132 #if SCAN_IICBUS 133 kprintf("Probing for devices on iicbus%d:", device_get_unit(dev)); 134 135 /* probe any devices */ 136 for (addr = 16; addr < 240; addr++) { 137 if (iic_probe_device(dev, (u_char)addr)) { 138 kprintf(" <%x>", addr); 139 } 140 } 141 kprintf("\n"); 142 #endif 143 144 device_add_child(dev, "ic", -1); 145 device_add_child(dev, "iic", -1); 146 device_add_child(dev, "iicsmb", -1); 147 #if 0 148 /* attach any known device */ 149 device_add_child(dev, "iic", -1); 150 #endif 151 bus_generic_attach(dev); 152 153 return (0); 154 } 155 156 static int 157 iicbus_detach(device_t dev) 158 { 159 iicbus_reset(dev, IIC_FASTEST, 0, NULL); 160 bus_generic_detach(dev); 161 return (0); 162 } 163 164 static int 165 iicbus_add_child(device_t dev, int order, const char *name, int unit) 166 { 167 168 device_add_child_ordered(dev, order, name, unit); 169 bus_generic_attach(dev); 170 return (0); 171 } 172 173 int 174 iicbus_generic_intr(device_t dev, int event, char *buf) 175 { 176 177 return (0); 178 } 179 180 int 181 iicbus_null_callback(device_t dev, int index, caddr_t data) 182 { 183 184 return (0); 185 } 186 187 int 188 iicbus_null_repeated_start(device_t dev, u_char addr) 189 { 190 191 return (IIC_ENOTSUPP); 192 } 193 194 DRIVER_MODULE(iicbus, iicbb, iicbus_driver, iicbus_devclass, NULL, NULL); 195 MODULE_VERSION(iicbus, IICBUS_MODVER); 196