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/resource.h> 61 #include <machine/intr.h> 62 63 #include <dev/iicbus/iicbus.h> 64 #include <dev/iicbus/iiconf.h> 65 66 #include <dev/ofw/openfirm.h> 67 #include <dev/ofw/ofw_bus.h> 68 #include <dev/ofw/ofw_bus_subr.h> 69 70 #include "arm/ti/twl/twl.h" 71 72 /* TWL device IDs */ 73 #define TWL_DEVICE_UNKNOWN 0xffff 74 #define TWL_DEVICE_4030 0x4030 75 #define TWL_DEVICE_6025 0x6025 76 #define TWL_DEVICE_6030 0x6030 77 78 /* Each TWL device typically has more than one I2C address */ 79 #define TWL_MAX_SUBADDRS 4 80 81 /* The maxium number of bytes that can be written in one call */ 82 #define TWL_MAX_IIC_DATA_SIZE 63 83 84 /* The TWL devices typically use 4 I2C address for the different internal 85 * register sets, plus one SmartReflex I2C address. 86 */ 87 #define TWL_CHIP_ID0 0x48 88 #define TWL_CHIP_ID1 0x49 89 #define TWL_CHIP_ID2 0x4A 90 #define TWL_CHIP_ID3 0x4B 91 92 #define TWL_SMARTREFLEX_CHIP_ID 0x12 93 94 #define TWL_INVALID_CHIP_ID 0xff 95 96 struct twl_softc { 97 device_t sc_dev; 98 struct mtx sc_mtx; 99 unsigned int sc_type; 100 101 uint8_t sc_subaddr_map[TWL_MAX_SUBADDRS]; 102 103 struct intr_config_hook sc_scan_hook; 104 105 device_t sc_vreg; 106 device_t sc_clks; 107 }; 108 109 /** 110 * Macros for driver mutex locking 111 */ 112 #define TWL_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx) 113 #define TWL_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx) 114 #define TWL_LOCK_INIT(_sc) \ 115 mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->sc_dev), \ 116 "twl", MTX_DEF) 117 #define TWL_LOCK_DESTROY(_sc) mtx_destroy(&_sc->sc_mtx); 118 #define TWL_ASSERT_LOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_OWNED); 119 #define TWL_ASSERT_UNLOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_NOTOWNED); 120 121 122 /** 123 * twl_is_4030 - returns true if the device is TWL4030 124 * twl_is_6025 - returns true if the device is TWL6025 125 * twl_is_6030 - returns true if the device is TWL6030 126 * @sc: device soft context 127 * 128 * Returns a non-zero value if the device matches. 129 * 130 * RETURNS: 131 * Returns a non-zero value if the device matches, otherwise zero. 132 */ 133 int 134 twl_is_4030(device_t dev) 135 { 136 struct twl_softc *sc = device_get_softc(dev); 137 return (sc->sc_type == TWL_DEVICE_4030); 138 } 139 140 int 141 twl_is_6025(device_t dev) 142 { 143 struct twl_softc *sc = device_get_softc(dev); 144 return (sc->sc_type == TWL_DEVICE_6025); 145 } 146 147 int 148 twl_is_6030(device_t dev) 149 { 150 struct twl_softc *sc = device_get_softc(dev); 151 return (sc->sc_type == TWL_DEVICE_6030); 152 } 153 154 155 /** 156 * twl_read - read one or more registers from the TWL device 157 * @sc: device soft context 158 * @nsub: the sub-module to read from 159 * @reg: the register offset within the module to read 160 * @buf: buffer to store the bytes in 161 * @cnt: the number of bytes to read 162 * 163 * Reads one or more registers and stores the result in the suppled buffer. 164 * 165 * RETURNS: 166 * Zero on success or an error code on failure. 167 */ 168 int 169 twl_read(device_t dev, uint8_t nsub, uint8_t reg, uint8_t *buf, uint16_t cnt) 170 { 171 struct twl_softc *sc; 172 struct iic_msg msg[2]; 173 uint8_t addr; 174 int rc; 175 176 sc = device_get_softc(dev); 177 178 TWL_LOCK(sc); 179 addr = sc->sc_subaddr_map[nsub]; 180 TWL_UNLOCK(sc); 181 182 if (addr == TWL_INVALID_CHIP_ID) 183 return (EIO); 184 185 186 /* Set the address to read from */ 187 msg[0].slave = addr; 188 msg[0].flags = IIC_M_WR | IIC_M_NOSTOP; 189 msg[0].len = 1; 190 msg[0].buf = ® 191 /* Read the data back */ 192 msg[1].slave = addr; 193 msg[1].flags = IIC_M_RD; 194 msg[1].len = cnt; 195 msg[1].buf = buf; 196 197 rc = iicbus_transfer(dev, msg, 2); 198 if (rc != 0) { 199 device_printf(dev, "iicbus read failed (adr:0x%02x, reg:0x%02x)\n", 200 addr, reg); 201 return (EIO); 202 } 203 204 return (0); 205 } 206 207 /** 208 * twl_write - writes one or more registers to the TWL device 209 * @sc: device soft context 210 * @nsub: the sub-module to read from 211 * @reg: the register offset within the module to read 212 * @buf: data to write 213 * @cnt: the number of bytes to write 214 * 215 * Writes one or more registers. 216 * 217 * RETURNS: 218 * Zero on success or a negative error code on failure. 219 */ 220 int 221 twl_write(device_t dev, uint8_t nsub, uint8_t reg, uint8_t *buf, uint16_t cnt) 222 { 223 struct twl_softc *sc; 224 struct iic_msg msg; 225 uint8_t addr; 226 uint8_t tmp_buf[TWL_MAX_IIC_DATA_SIZE + 1]; 227 int rc; 228 229 if (cnt > TWL_MAX_IIC_DATA_SIZE) 230 return (ENOMEM); 231 232 /* Set the register address as the first byte */ 233 tmp_buf[0] = reg; 234 memcpy(&tmp_buf[1], buf, cnt); 235 236 sc = device_get_softc(dev); 237 238 TWL_LOCK(sc); 239 addr = sc->sc_subaddr_map[nsub]; 240 TWL_UNLOCK(sc); 241 242 if (addr == TWL_INVALID_CHIP_ID) 243 return (EIO); 244 245 246 /* Setup the transfer and execute it */ 247 msg.slave = addr; 248 msg.flags = IIC_M_WR; 249 msg.len = cnt + 1; 250 msg.buf = tmp_buf; 251 252 rc = iicbus_transfer(dev, &msg, 1); 253 if (rc != 0) { 254 device_printf(sc->sc_dev, "iicbus write failed (adr:0x%02x, reg:0x%02x)\n", 255 addr, reg); 256 return (EIO); 257 } 258 259 return (0); 260 } 261 262 /** 263 * twl_test_present - checks if a device with given address is present 264 * @sc: device soft context 265 * @addr: the address of the device to scan for 266 * 267 * Sends just the address byte and checks for an ACK. If no ACK then device 268 * is assumed to not be present. 269 * 270 * RETURNS: 271 * EIO if device is not present, otherwise 0 is returned. 272 */ 273 static int 274 twl_test_present(struct twl_softc *sc, uint8_t addr) 275 { 276 struct iic_msg msg; 277 uint8_t tmp; 278 279 /* Set the address to read from */ 280 msg.slave = addr; 281 msg.flags = IIC_M_RD; 282 msg.len = 1; 283 msg.buf = &tmp; 284 285 if (iicbus_transfer(sc->sc_dev, &msg, 1) != 0) 286 return (EIO); 287 288 return (0); 289 } 290 291 /** 292 * twl_scan - scans the i2c bus for sub modules 293 * @dev: the twl device 294 * 295 * TWL devices don't just have one i2c slave address, rather they have up to 296 * 5 other addresses, each is for separate modules within the device. This 297 * function scans the bus for 4 possible sub-devices and stores the info 298 * internally. 299 * 300 */ 301 static void 302 twl_scan(void *dev) 303 { 304 struct twl_softc *sc; 305 unsigned i; 306 uint8_t devs[TWL_MAX_SUBADDRS]; 307 uint8_t base = TWL_CHIP_ID0; 308 309 sc = device_get_softc((device_t)dev); 310 311 memset(devs, TWL_INVALID_CHIP_ID, TWL_MAX_SUBADDRS); 312 313 /* Try each of the addresses (0x48, 0x49, 0x4a & 0x4b) to determine which 314 * sub modules we have. 315 */ 316 for (i = 0; i < TWL_MAX_SUBADDRS; i++) { 317 if (twl_test_present(sc, (base + i)) == 0) { 318 devs[i] = (base + i); 319 device_printf(sc->sc_dev, "Found (sub)device at 0x%02x\n", (base + i)); 320 } 321 } 322 323 TWL_LOCK(sc); 324 memcpy(sc->sc_subaddr_map, devs, TWL_MAX_SUBADDRS); 325 TWL_UNLOCK(sc); 326 327 /* Finished with the interrupt hook */ 328 config_intrhook_disestablish(&sc->sc_scan_hook); 329 } 330 331 /** 332 * twl_probe - 333 * @dev: the twl device 334 * 335 * Scans the FDT for a match for the device, possible compatible device 336 * strings are; "ti,twl6030", "ti,twl6025", "ti,twl4030". 337 * 338 * The FDT compat string also determines the type of device (it is currently 339 * not possible to dynamically determine the device type). 340 * 341 */ 342 static int 343 twl_probe(device_t dev) 344 { 345 phandle_t node; 346 const char *compat; 347 int len, l; 348 struct twl_softc *sc; 349 350 if ((compat = ofw_bus_get_compat(dev)) == NULL) 351 return (ENXIO); 352 353 if ((node = ofw_bus_get_node(dev)) == 0) 354 return (ENXIO); 355 356 /* Get total 'compatible' prop len */ 357 if ((len = OF_getproplen(node, "compatible")) <= 0) 358 return (ENXIO); 359 360 sc = device_get_softc(dev); 361 sc->sc_dev = dev; 362 sc->sc_type = TWL_DEVICE_UNKNOWN; 363 364 while (len > 0) { 365 if (strncasecmp(compat, "ti,twl6030", 10) == 0) 366 sc->sc_type = TWL_DEVICE_6030; 367 else if (strncasecmp(compat, "ti,twl6025", 10) == 0) 368 sc->sc_type = TWL_DEVICE_6025; 369 else if (strncasecmp(compat, "ti,twl4030", 10) == 0) 370 sc->sc_type = TWL_DEVICE_4030; 371 372 if (sc->sc_type != TWL_DEVICE_UNKNOWN) 373 break; 374 375 /* Slide to the next sub-string. */ 376 l = strlen(compat) + 1; 377 compat += l; 378 len -= l; 379 } 380 381 switch (sc->sc_type) { 382 case TWL_DEVICE_4030: 383 device_set_desc(dev, "TI TWL4030/TPS659x0 Companion IC"); 384 break; 385 case TWL_DEVICE_6025: 386 device_set_desc(dev, "TI TWL6025 Companion IC"); 387 break; 388 case TWL_DEVICE_6030: 389 device_set_desc(dev, "TI TWL6030 Companion IC"); 390 break; 391 case TWL_DEVICE_UNKNOWN: 392 default: 393 return (ENXIO); 394 } 395 396 return (0); 397 } 398 399 static int 400 twl_attach(device_t dev) 401 { 402 struct twl_softc *sc; 403 404 sc = device_get_softc(dev); 405 sc->sc_dev = dev; 406 407 TWL_LOCK_INIT(sc); 408 409 /* We have to wait until interrupts are enabled. I2C read and write 410 * only works if the interrupts are available. 411 */ 412 sc->sc_scan_hook.ich_func = twl_scan; 413 sc->sc_scan_hook.ich_arg = dev; 414 415 if (config_intrhook_establish(&sc->sc_scan_hook) != 0) 416 return (ENOMEM); 417 418 /* FIXME: should be in DTS file */ 419 if ((sc->sc_vreg = device_add_child(dev, "twl_vreg", -1)) == NULL) 420 device_printf(dev, "could not allocate twl_vreg instance\n"); 421 if ((sc->sc_clks = device_add_child(dev, "twl_clks", -1)) == NULL) 422 device_printf(dev, "could not allocate twl_clks instance\n"); 423 424 return (bus_generic_attach(dev)); 425 } 426 427 static int 428 twl_detach(device_t dev) 429 { 430 struct twl_softc *sc; 431 432 sc = device_get_softc(dev); 433 434 if (sc->sc_vreg) 435 device_delete_child(dev, sc->sc_vreg); 436 if (sc->sc_clks) 437 device_delete_child(dev, sc->sc_clks); 438 439 440 TWL_LOCK_DESTROY(sc); 441 442 return (0); 443 } 444 445 static device_method_t twl_methods[] = { 446 DEVMETHOD(device_probe, twl_probe), 447 DEVMETHOD(device_attach, twl_attach), 448 DEVMETHOD(device_detach, twl_detach), 449 450 {0, 0}, 451 }; 452 453 static driver_t twl_driver = { 454 "twl", 455 twl_methods, 456 sizeof(struct twl_softc), 457 }; 458 static devclass_t twl_devclass; 459 460 DRIVER_MODULE(twl, iicbus, twl_driver, twl_devclass, 0, 0); 461 MODULE_VERSION(twl, 1); 462