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