sys/dev/video/cxm/cxm_i2c.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 | /* * Copyright (c) 2003, 2004, 2005 * John Wehle <john@feith.com>. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by John Wehle. * 4. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ /* * I2c routines for the Conexant MPEG-2 Codec driver. */ #include <sys/param.h> #include <sys/systm.h> #include <sys/malloc.h> #include <sys/conf.h> #include <sys/uio.h> #include <sys/kernel.h> #include <sys/module.h> #include <sys/poll.h> #include <sys/select.h> #include <sys/resource.h> #include <sys/bus.h> #include <sys/rman.h> #include <machine/clock.h> #include <bus/pci/pcireg.h> #include <bus/pci/pcivar.h> #include <dev/video/cxm/cxm.h> #include <bus/iicbus/iiconf.h> #include "iicbb_if.h" static int cxm_iic_probe(device_t dev); static int cxm_iic_attach(device_t dev); static int cxm_iic_detach(device_t dev); static void cxm_iic_child_detached(device_t dev, device_t child); static int cxm_iic_callback(device_t, int, caddr_t *); static int cxm_iic_reset(device_t, u_char, u_char, u_char *); static int cxm_iic_getscl(device_t); static int cxm_iic_getsda(device_t); static void cxm_iic_setscl(device_t, int); static void cxm_iic_setsda(device_t, int); static device_method_t cxm_iic_methods[] = { /* Device interface */ DEVMETHOD(device_probe, cxm_iic_probe), DEVMETHOD(device_attach, cxm_iic_attach), DEVMETHOD(device_detach, cxm_iic_detach), /* bus interface */ DEVMETHOD(bus_child_detached, cxm_iic_child_detached), DEVMETHOD(bus_print_child, bus_generic_print_child), DEVMETHOD(bus_driver_added, bus_generic_driver_added), /* iicbb interface */ DEVMETHOD(iicbb_callback, cxm_iic_callback), DEVMETHOD(iicbb_reset, cxm_iic_reset), DEVMETHOD(iicbb_getscl, cxm_iic_getscl), DEVMETHOD(iicbb_getsda, cxm_iic_getsda), DEVMETHOD(iicbb_setscl, cxm_iic_setscl), DEVMETHOD(iicbb_setsda, cxm_iic_setsda), DEVMETHOD_END }; static driver_t cxm_iic_driver = { "cxm_iic", cxm_iic_methods, sizeof(struct cxm_iic_softc), }; static devclass_t cxm_iic_devclass; DRIVER_MODULE(cxm_iic, cxm, cxm_iic_driver, cxm_iic_devclass, NULL, NULL); MODULE_VERSION(cxm_iic, 1); MODULE_DEPEND(cxm_iic, iicbb, IICBB_MINVER, IICBB_PREFVER, IICBB_MAXVER); /* * the boot time probe routine. * * The cxm_iic device is only probed after it has * been established that the cxm device is present * which means that the cxm_iic device * must * * be present since it's built into the cxm hardware. */ static int cxm_iic_probe(device_t dev) { device_set_desc(dev, "Conexant iTVC15 / iTVC16 I2C controller"); return 0; } /* * the attach routine. */ static int cxm_iic_attach(device_t dev) { device_t *kids; device_t iicbus; int error; int numkids; int i; bus_space_handle_t *bhandlep; bus_space_tag_t *btagp; struct cxm_iic_softc *sc; device_t child; /* Get the device data */ sc = device_get_softc(dev); /* retrieve the cxm btag and bhandle */ if (BUS_READ_IVAR(device_get_parent(dev), dev, CXM_IVAR_BTAG, (uintptr_t *)&btagp) || BUS_READ_IVAR(device_get_parent(dev), dev, CXM_IVAR_BHANDLE, (uintptr_t *)&bhandlep)) { device_printf(dev, "could not retrieve bus space information\n"); return ENXIO; } sc->btag = *btagp; sc->bhandle = *bhandlep; /* add bit-banging generic code onto cxm_iic interface */ sc->iicbb = device_add_child(dev, "iicbb", -1); if (!sc->iicbb) { device_printf(dev, "could not add iicbb\n"); return ENXIO; } /* probed and attached the bit-banging code */ error = device_probe_and_attach(sc->iicbb); if (error) { device_printf(dev, "could not attach iicbb\n"); goto fail; } /* locate iicbus which was attached by the bit-banging code */ iicbus = NULL; device_get_children(sc->iicbb, &kids, &numkids); for (i = 0; i < numkids; i++) if (strcmp(device_get_name(kids[i]), "iicbus") == 0) { iicbus = kids[i]; break; } kfree(kids, M_TEMP); if (!iicbus) { device_printf(dev, "could not find iicbus\n"); error = ENXIO; goto fail; } if (BUS_WRITE_IVAR(device_get_parent(dev), dev, CXM_IVAR_IICBUS, (uintptr_t)&iicbus)) { device_printf(dev, "could not store iicbus information\n"); error = ENXIO; goto fail; } return 0; fail: /* * Detach the children before recursively deleting * in case a child has a pointer to a grandchild * which is used by the child's detach routine. * * Remember the child before detaching so we can * delete it (bus_generic_detach indirectly zeroes * sc->child_dev). */ child = sc->iicbb; bus_generic_detach(dev); if (child) device_delete_child(dev, child); return error; } /* * the detach routine. */ static int cxm_iic_detach(device_t dev) { struct cxm_iic_softc *sc; device_t child; /* Get the device data */ sc = device_get_softc(dev); BUS_WRITE_IVAR(device_get_parent(dev), dev, CXM_IVAR_IICBUS, 0); /* * Detach the children before recursively deleting * in case a child has a pointer to a grandchild * which is used by the child's detach routine. * * Remember the child before detaching so we can * delete it (bus_generic_detach indirectly zeroes * sc->child_dev). */ child = sc->iicbb; bus_generic_detach(dev); if (child) device_delete_child(dev, child); return 0; } /* * the child detached routine. */ static void cxm_iic_child_detached(device_t dev, device_t child) { struct cxm_iic_softc *sc; /* Get the device data */ sc = device_get_softc(dev); if (child == sc->iicbb) sc->iicbb = NULL; } static int cxm_iic_callback(device_t dev, int index, caddr_t *data) { return 0; } static int cxm_iic_reset(device_t dev, u_char speed, u_char addr, u_char * oldaddr) { struct cxm_iic_softc *sc; /* Get the device data */ sc = (struct cxm_iic_softc *)device_get_softc(dev); /* Set scl to 1 */ CSR_WRITE_4(sc, CXM_REG_I2C_SETSCL, ~(int)1); /* Set sda to 1 */ CSR_WRITE_4(sc, CXM_REG_I2C_SETSDA, ~(int)1); /* * PCI writes may be buffered so force the * write to complete by reading the last * location written. */ CSR_READ_4(sc, CXM_REG_I2C_SETSDA); /* Wait for 10 usec */ DELAY(10); return IIC_ENOADDR; } static int cxm_iic_getscl(device_t dev) { struct cxm_iic_softc *sc; /* Get the device data */ sc = (struct cxm_iic_softc *)device_get_softc(dev); /* Get sda */ return CSR_READ_1(sc, CXM_REG_I2C_GETSCL); } static int cxm_iic_getsda(device_t dev) { struct cxm_iic_softc *sc; /* Get the device data */ sc = (struct cxm_iic_softc *)device_get_softc(dev); /* Get sda */ return CSR_READ_1(sc, CXM_REG_I2C_GETSDA); } static void cxm_iic_setscl(device_t dev, int val) { struct cxm_iic_softc *sc; /* Get the device data */ sc = (struct cxm_iic_softc *)device_get_softc(dev); /* Set scl to the requested value */ CSR_WRITE_4(sc, CXM_REG_I2C_SETSCL, ~(int)(val ? 1 : 0)); /* * PCI writes may be buffered so force the * write to complete by reading the last * location written. */ CSR_READ_4(sc, CXM_REG_I2C_SETSCL); } static void cxm_iic_setsda(device_t dev, int val) { struct cxm_iic_softc *sc; /* Get the device data */ sc = (struct cxm_iic_softc *)device_get_softc(dev); /* Set sda to the requested value */ CSR_WRITE_4(sc, CXM_REG_I2C_SETSDA, ~(int)(val ? 1 : 0)); /* * PCI writes may be buffered so force the * write to complete by reading the last * location written. */ CSR_READ_4(sc, CXM_REG_I2C_SETSDA); } |