1 /*- 2 * Copyright (c) 2011 3 * Ben Gray <ben.r.gray@gmail.com>. 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #include <sys/cdefs.h> 29 __FBSDID("$FreeBSD$"); 30 31 /* 32 * Texas Instruments TWL4030/TWL5030/TWL60x0/TPS659x0 Power Management and 33 * Audio CODEC devices. 34 * 35 * This code is based on the Linux TWL multifunctional device driver, which is 36 * copyright (C) 2005-2006 Texas Instruments, Inc. 37 * 38 * These chips are typically used as support ICs for the OMAP range of embedded 39 * ARM processes/SOC from Texas Instruments. They are typically used to control 40 * on board voltages, however some variants have other features like audio 41 * codecs, USB OTG transceivers, RTC, PWM, etc. 42 * 43 * This driver acts as a bus for more specific companion devices. 44 * 45 */ 46 47 #include <sys/param.h> 48 #include <sys/systm.h> 49 #include <sys/kernel.h> 50 #include <sys/lock.h> 51 #include <sys/module.h> 52 #include <sys/bus.h> 53 #include <sys/resource.h> 54 #include <sys/rman.h> 55 #include <sys/sysctl.h> 56 #include <sys/mutex.h> 57 #include <sys/malloc.h> 58 59 #include <machine/bus.h> 60 #include <machine/cpu.h> 61 #include <machine/cpufunc.h> 62 #include <machine/frame.h> 63 #include <machine/resource.h> 64 #include <machine/intr.h> 65 66 #include <dev/iicbus/iicbus.h> 67 #include <dev/iicbus/iiconf.h> 68 69 #include <dev/ofw/openfirm.h> 70 #include <dev/ofw/ofw_bus.h> 71 #include <dev/ofw/ofw_bus_subr.h> 72 73 #include "arm/ti/twl/twl.h" 74 75 /* TWL device IDs */ 76 #define TWL_DEVICE_UNKNOWN 0xffff 77 #define TWL_DEVICE_4030 0x4030 78 #define TWL_DEVICE_6025 0x6025 79 #define TWL_DEVICE_6030 0x6030 80 81 /* Each TWL device typically has more than one I2C address */ 82 #define TWL_MAX_SUBADDRS 4 83 84 /* The maxium number of bytes that can be written in one call */ 85 #define TWL_MAX_IIC_DATA_SIZE 63 86 87 /* The TWL devices typically use 4 I2C address for the different internal 88 * register sets, plus one SmartReflex I2C address. 89 */ 90 #define TWL_CHIP_ID0 0x48 91 #define TWL_CHIP_ID1 0x49 92 #define TWL_CHIP_ID2 0x4A 93 #define TWL_CHIP_ID3 0x4B 94 95 #define TWL_SMARTREFLEX_CHIP_ID 0x12 96 97 #define TWL_INVALID_CHIP_ID 0xff 98 99 struct twl_softc { 100 device_t sc_dev; 101 struct mtx sc_mtx; 102 unsigned int sc_type; 103 104 uint8_t sc_subaddr_map[TWL_MAX_SUBADDRS]; 105 106 struct intr_config_hook sc_scan_hook; 107 108 device_t sc_vreg; 109 device_t sc_clks; 110 }; 111 112 /** 113 * Macros for driver mutex locking 114 */ 115 #define TWL_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx) 116 #define TWL_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx) 117 #define TWL_LOCK_INIT(_sc) \ 118 mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->sc_dev), \ 119 "twl", MTX_DEF) 120 #define TWL_LOCK_DESTROY(_sc) mtx_destroy(&_sc->sc_mtx); 121 #define TWL_ASSERT_LOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_OWNED); 122 #define TWL_ASSERT_UNLOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_NOTOWNED); 123 124 125 /** 126 * twl_is_4030 - returns true if the device is TWL4030 127 * twl_is_6025 - returns true if the device is TWL6025 128 * twl_is_6030 - returns true if the device is TWL6030 129 * @sc: device soft context 130 * 131 * Returns a non-zero value if the device matches. 132 * 133 * RETURNS: 134 * Returns a non-zero value if the device matches, otherwise zero. 135 */ 136 int 137 twl_is_4030(device_t dev) 138 { 139 struct twl_softc *sc = device_get_softc(dev); 140 return (sc->sc_type == TWL_DEVICE_4030); 141 } 142 143 int 144 twl_is_6025(device_t dev) 145 { 146 struct twl_softc *sc = device_get_softc(dev); 147 return (sc->sc_type == TWL_DEVICE_6025); 148 } 149 150 int 151 twl_is_6030(device_t dev) 152 { 153 struct twl_softc *sc = device_get_softc(dev); 154 return (sc->sc_type == TWL_DEVICE_6030); 155 } 156 157 158 /** 159 * twl_read - read one or more registers from the TWL device 160 * @sc: device soft context 161 * @nsub: the sub-module to read from 162 * @reg: the register offset within the module to read 163 * @buf: buffer to store the bytes in 164 * @cnt: the number of bytes to read 165 * 166 * Reads one or more registers and stores the result in the suppled buffer. 167 * 168 * RETURNS: 169 * Zero on success or an error code on failure. 170 */ 171 int 172 twl_read(device_t dev, uint8_t nsub, uint8_t reg, uint8_t *buf, uint16_t cnt) 173 { 174 struct twl_softc *sc; 175 struct iic_msg msg[2]; 176 uint8_t addr; 177 int rc; 178 179 sc = device_get_softc(dev); 180 181 TWL_LOCK(sc); 182 addr = sc->sc_subaddr_map[nsub]; 183 TWL_UNLOCK(sc); 184 185 if (addr == TWL_INVALID_CHIP_ID) 186 return (EIO); 187 188 189 /* Set the address to read from */ 190 msg[0].slave = addr; 191 msg[0].flags = IIC_M_WR | IIC_M_NOSTOP; 192 msg[0].len = 1; 193 msg[0].buf = ® 194 /* Read the data back */ 195 msg[1].slave = addr; 196 msg[1].flags = IIC_M_RD; 197 msg[1].len = cnt; 198 msg[1].buf = buf; 199 200 rc = iicbus_transfer(dev, msg, 2); 201 if (rc != 0) { 202 device_printf(dev, "iicbus read failed (adr:0x%02x, reg:0x%02x)\n", 203 addr, reg); 204 return (EIO); 205 } 206 207 return (0); 208 } 209 210 /** 211 * twl_write - writes one or more registers to the TWL device 212 * @sc: device soft context 213 * @nsub: the sub-module to read from 214 * @reg: the register offset within the module to read 215 * @buf: data to write 216 * @cnt: the number of bytes to write 217 * 218 * Writes one or more registers. 219 * 220 * RETURNS: 221 * Zero on success or a negative error code on failure. 222 */ 223 int 224 twl_write(device_t dev, uint8_t nsub, uint8_t reg, uint8_t *buf, uint16_t cnt) 225 { 226 struct twl_softc *sc; 227 struct iic_msg msg; 228 uint8_t addr; 229 uint8_t tmp_buf[TWL_MAX_IIC_DATA_SIZE + 1]; 230 int rc; 231 232 if (cnt > TWL_MAX_IIC_DATA_SIZE) 233 return (ENOMEM); 234 235 /* Set the register address as the first byte */ 236 tmp_buf[0] = reg; 237 memcpy(&tmp_buf[1], buf, cnt); 238 239 sc = device_get_softc(dev); 240 241 TWL_LOCK(sc); 242 addr = sc->sc_subaddr_map[nsub]; 243 TWL_UNLOCK(sc); 244 245 if (addr == TWL_INVALID_CHIP_ID) 246 return (EIO); 247 248 249 /* Setup the transfer and execute it */ 250 msg.slave = addr; 251 msg.flags = IIC_M_WR; 252 msg.len = cnt + 1; 253 msg.buf = tmp_buf; 254 255 rc = iicbus_transfer(dev, &msg, 1); 256 if (rc != 0) { 257 device_printf(sc->sc_dev, "iicbus write failed (adr:0x%02x, reg:0x%02x)\n", 258 addr, reg); 259 return (EIO); 260 } 261 262 return (0); 263 } 264 265 /** 266 * twl_test_present - checks if a device with given address is present 267 * @sc: device soft context 268 * @addr: the address of the device to scan for 269 * 270 * Sends just the address byte and checks for an ACK. If no ACK then device 271 * is assumed to not be present. 272 * 273 * RETURNS: 274 * EIO if device is not present, otherwise 0 is returned. 275 */ 276 static int 277 twl_test_present(struct twl_softc *sc, uint8_t addr) 278 { 279 struct iic_msg msg; 280 uint8_t tmp; 281 282 /* Set the address to read from */ 283 msg.slave = addr; 284 msg.flags = IIC_M_RD; 285 msg.len = 1; 286 msg.buf = &tmp; 287 288 if (iicbus_transfer(sc->sc_dev, &msg, 1) != 0) 289 return (EIO); 290 291 return (0); 292 } 293 294 /** 295 * twl_scan - scans the i2c bus for sub modules 296 * @dev: the twl device 297 * 298 * TWL devices don't just have one i2c slave address, rather they have up to 299 * 5 other addresses, each is for separate modules within the device. This 300 * function scans the bus for 4 possible sub-devices and stores the info 301 * internally. 302 * 303 */ 304 static void 305 twl_scan(void *dev) 306 { 307 struct twl_softc *sc; 308 unsigned i; 309 uint8_t devs[TWL_MAX_SUBADDRS]; 310 uint8_t base = TWL_CHIP_ID0; 311 312 sc = device_get_softc((device_t)dev); 313 314 memset(devs, TWL_INVALID_CHIP_ID, TWL_MAX_SUBADDRS); 315 316 /* Try each of the addresses (0x48, 0x49, 0x4a & 0x4b) to determine which 317 * sub modules we have. 318 */ 319 for (i = 0; i < TWL_MAX_SUBADDRS; i++) { 320 if (twl_test_present(sc, (base + i)) == 0) { 321 devs[i] = (base + i); 322 device_printf(sc->sc_dev, "Found (sub)device at 0x%02x\n", (base + i)); 323 } 324 } 325 326 TWL_LOCK(sc); 327 memcpy(sc->sc_subaddr_map, devs, TWL_MAX_SUBADDRS); 328 TWL_UNLOCK(sc); 329 330 /* Finished with the interrupt hook */ 331 config_intrhook_disestablish(&sc->sc_scan_hook); 332 } 333 334 /** 335 * twl_probe - 336 * @dev: the twl device 337 * 338 * Scans the FDT for a match for the device, possible compatible device 339 * strings are; "ti,twl6030", "ti,twl6025", "ti,twl4030". 340 * 341 * The FDT compat string also determines the type of device (it is currently 342 * not possible to dynamically determine the device type). 343 * 344 */ 345 static int 346 twl_probe(device_t dev) 347 { 348 phandle_t node; 349 const char *compat; 350 int len, l; 351 struct twl_softc *sc; 352 353 if ((compat = ofw_bus_get_compat(dev)) == NULL) 354 return (ENXIO); 355 356 if ((node = ofw_bus_get_node(dev)) == 0) 357 return (ENXIO); 358 359 /* Get total 'compatible' prop len */ 360 if ((len = OF_getproplen(node, "compatible")) <= 0) 361 return (ENXIO); 362 363 sc = device_get_softc(dev); 364 sc->sc_dev = dev; 365 sc->sc_type = TWL_DEVICE_UNKNOWN; 366 367 while (len > 0) { 368 if (strncasecmp(compat, "ti,twl6030", 10) == 0) 369 sc->sc_type = TWL_DEVICE_6030; 370 else if (strncasecmp(compat, "ti,twl6025", 10) == 0) 371 sc->sc_type = TWL_DEVICE_6025; 372 else if (strncasecmp(compat, "ti,twl4030", 10) == 0) 373 sc->sc_type = TWL_DEVICE_4030; 374 375 if (sc->sc_type != TWL_DEVICE_UNKNOWN) 376 break; 377 378 /* Slide to the next sub-string. */ 379 l = strlen(compat) + 1; 380 compat += l; 381 len -= l; 382 } 383 384 switch (sc->sc_type) { 385 case TWL_DEVICE_4030: 386 device_set_desc(dev, "TI TWL4030/TPS659x0 Companion IC"); 387 break; 388 case TWL_DEVICE_6025: 389 device_set_desc(dev, "TI TWL6025 Companion IC"); 390 break; 391 case TWL_DEVICE_6030: 392 device_set_desc(dev, "TI TWL6030 Companion IC"); 393 break; 394 case TWL_DEVICE_UNKNOWN: 395 default: 396 return (ENXIO); 397 } 398 399 return (0); 400 } 401 402 static int 403 twl_attach(device_t dev) 404 { 405 struct twl_softc *sc; 406 407 sc = device_get_softc(dev); 408 sc->sc_dev = dev; 409 410 TWL_LOCK_INIT(sc); 411 412 /* We have to wait until interrupts are enabled. I2C read and write 413 * only works if the interrupts are available. 414 */ 415 sc->sc_scan_hook.ich_func = twl_scan; 416 sc->sc_scan_hook.ich_arg = dev; 417 418 if (config_intrhook_establish(&sc->sc_scan_hook) != 0) 419 return (ENOMEM); 420 421 /* FIXME: should be in DTS file */ 422 if ((sc->sc_vreg = device_add_child(dev, "twl_vreg", -1)) == NULL) 423 device_printf(dev, "could not allocate twl_vreg instance\n"); 424 if ((sc->sc_clks = device_add_child(dev, "twl_clks", -1)) == NULL) 425 device_printf(dev, "could not allocate twl_clks instance\n"); 426 427 return (bus_generic_attach(dev)); 428 } 429 430 static int 431 twl_detach(device_t dev) 432 { 433 struct twl_softc *sc; 434 435 sc = device_get_softc(dev); 436 437 if (sc->sc_vreg) 438 device_delete_child(dev, sc->sc_vreg); 439 if (sc->sc_clks) 440 device_delete_child(dev, sc->sc_clks); 441 442 443 TWL_LOCK_DESTROY(sc); 444 445 return (0); 446 } 447 448 static device_method_t twl_methods[] = { 449 DEVMETHOD(device_probe, twl_probe), 450 DEVMETHOD(device_attach, twl_attach), 451 DEVMETHOD(device_detach, twl_detach), 452 453 {0, 0}, 454 }; 455 456 static driver_t twl_driver = { 457 "twl", 458 twl_methods, 459 sizeof(struct twl_softc), 460 }; 461 static devclass_t twl_devclass; 462 463 DRIVER_MODULE(twl, iicbus, twl_driver, twl_devclass, 0, 0); 464 MODULE_VERSION(twl, 1); 465