1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2011 5 * Ben Gray <ben.r.gray@gmail.com>. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #include <sys/cdefs.h> 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 maximum 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 * twl_is_4030 - returns true if the device is TWL4030 123 * twl_is_6025 - returns true if the device is TWL6025 124 * twl_is_6030 - returns true if the device is TWL6030 125 * @sc: device soft context 126 * 127 * Returns a non-zero value if the device matches. 128 * 129 * RETURNS: 130 * Returns a non-zero value if the device matches, otherwise zero. 131 */ 132 int 133 twl_is_4030(device_t dev) 134 { 135 struct twl_softc *sc = device_get_softc(dev); 136 return (sc->sc_type == TWL_DEVICE_4030); 137 } 138 139 int 140 twl_is_6025(device_t dev) 141 { 142 struct twl_softc *sc = device_get_softc(dev); 143 return (sc->sc_type == TWL_DEVICE_6025); 144 } 145 146 int 147 twl_is_6030(device_t dev) 148 { 149 struct twl_softc *sc = device_get_softc(dev); 150 return (sc->sc_type == TWL_DEVICE_6030); 151 } 152 153 /** 154 * twl_read - read one or more registers from the TWL device 155 * @sc: device soft context 156 * @nsub: the sub-module to read from 157 * @reg: the register offset within the module to read 158 * @buf: buffer to store the bytes in 159 * @cnt: the number of bytes to read 160 * 161 * Reads one or more registers and stores the result in the suppled buffer. 162 * 163 * RETURNS: 164 * Zero on success or an error code on failure. 165 */ 166 int 167 twl_read(device_t dev, uint8_t nsub, uint8_t reg, uint8_t *buf, uint16_t cnt) 168 { 169 struct twl_softc *sc; 170 struct iic_msg msg[2]; 171 uint8_t addr; 172 int rc; 173 174 sc = device_get_softc(dev); 175 176 TWL_LOCK(sc); 177 addr = sc->sc_subaddr_map[nsub]; 178 TWL_UNLOCK(sc); 179 180 if (addr == TWL_INVALID_CHIP_ID) 181 return (EIO); 182 183 /* Set the address to read from */ 184 msg[0].slave = addr; 185 msg[0].flags = IIC_M_WR | IIC_M_NOSTOP; 186 msg[0].len = 1; 187 msg[0].buf = ® 188 /* Read the data back */ 189 msg[1].slave = addr; 190 msg[1].flags = IIC_M_RD; 191 msg[1].len = cnt; 192 msg[1].buf = buf; 193 194 rc = iicbus_transfer(dev, msg, 2); 195 if (rc != 0) { 196 device_printf(dev, "iicbus read failed (adr:0x%02x, reg:0x%02x)\n", 197 addr, reg); 198 return (EIO); 199 } 200 201 return (0); 202 } 203 204 /** 205 * twl_write - writes one or more registers to the TWL device 206 * @sc: device soft context 207 * @nsub: the sub-module to read from 208 * @reg: the register offset within the module to read 209 * @buf: data to write 210 * @cnt: the number of bytes to write 211 * 212 * Writes one or more registers. 213 * 214 * RETURNS: 215 * Zero on success or a negative error code on failure. 216 */ 217 int 218 twl_write(device_t dev, uint8_t nsub, uint8_t reg, uint8_t *buf, uint16_t cnt) 219 { 220 struct twl_softc *sc; 221 struct iic_msg msg; 222 uint8_t addr; 223 uint8_t tmp_buf[TWL_MAX_IIC_DATA_SIZE + 1]; 224 int rc; 225 226 if (cnt > TWL_MAX_IIC_DATA_SIZE) 227 return (ENOMEM); 228 229 /* Set the register address as the first byte */ 230 tmp_buf[0] = reg; 231 memcpy(&tmp_buf[1], buf, cnt); 232 233 sc = device_get_softc(dev); 234 235 TWL_LOCK(sc); 236 addr = sc->sc_subaddr_map[nsub]; 237 TWL_UNLOCK(sc); 238 239 if (addr == TWL_INVALID_CHIP_ID) 240 return (EIO); 241 242 /* Setup the transfer and execute it */ 243 msg.slave = addr; 244 msg.flags = IIC_M_WR; 245 msg.len = cnt + 1; 246 msg.buf = tmp_buf; 247 248 rc = iicbus_transfer(dev, &msg, 1); 249 if (rc != 0) { 250 device_printf(sc->sc_dev, "iicbus write failed (adr:0x%02x, reg:0x%02x)\n", 251 addr, reg); 252 return (EIO); 253 } 254 255 return (0); 256 } 257 258 /** 259 * twl_test_present - checks if a device with given address is present 260 * @sc: device soft context 261 * @addr: the address of the device to scan for 262 * 263 * Sends just the address byte and checks for an ACK. If no ACK then device 264 * is assumed to not be present. 265 * 266 * RETURNS: 267 * EIO if device is not present, otherwise 0 is returned. 268 */ 269 static int 270 twl_test_present(struct twl_softc *sc, uint8_t addr) 271 { 272 struct iic_msg msg; 273 uint8_t tmp; 274 275 /* Set the address to read from */ 276 msg.slave = addr; 277 msg.flags = IIC_M_RD; 278 msg.len = 1; 279 msg.buf = &tmp; 280 281 if (iicbus_transfer(sc->sc_dev, &msg, 1) != 0) 282 return (EIO); 283 284 return (0); 285 } 286 287 /** 288 * twl_scan - scans the i2c bus for sub modules 289 * @dev: the twl device 290 * 291 * TWL devices don't just have one i2c slave address, rather they have up to 292 * 5 other addresses, each is for separate modules within the device. This 293 * function scans the bus for 4 possible sub-devices and stores the info 294 * internally. 295 * 296 */ 297 static void 298 twl_scan(void *dev) 299 { 300 struct twl_softc *sc; 301 unsigned i; 302 uint8_t devs[TWL_MAX_SUBADDRS]; 303 uint8_t base = TWL_CHIP_ID0; 304 305 sc = device_get_softc((device_t)dev); 306 307 memset(devs, TWL_INVALID_CHIP_ID, TWL_MAX_SUBADDRS); 308 309 /* Try each of the addresses (0x48, 0x49, 0x4a & 0x4b) to determine which 310 * sub modules we have. 311 */ 312 for (i = 0; i < TWL_MAX_SUBADDRS; i++) { 313 if (twl_test_present(sc, (base + i)) == 0) { 314 devs[i] = (base + i); 315 device_printf(sc->sc_dev, "Found (sub)device at 0x%02x\n", (base + i)); 316 } 317 } 318 319 TWL_LOCK(sc); 320 memcpy(sc->sc_subaddr_map, devs, TWL_MAX_SUBADDRS); 321 TWL_UNLOCK(sc); 322 323 /* Finished with the interrupt hook */ 324 config_intrhook_disestablish(&sc->sc_scan_hook); 325 } 326 327 /** 328 * twl_probe - 329 * @dev: the twl device 330 * 331 * Scans the FDT for a match for the device, possible compatible device 332 * strings are; "ti,twl6030", "ti,twl6025", "ti,twl4030". 333 * 334 * The FDT compat string also determines the type of device (it is currently 335 * not possible to dynamically determine the device type). 336 * 337 */ 338 static int 339 twl_probe(device_t dev) 340 { 341 phandle_t node; 342 const char *compat; 343 int len, l; 344 struct twl_softc *sc; 345 346 if ((compat = ofw_bus_get_compat(dev)) == NULL) 347 return (ENXIO); 348 349 if ((node = ofw_bus_get_node(dev)) == 0) 350 return (ENXIO); 351 352 /* Get total 'compatible' prop len */ 353 if ((len = OF_getproplen(node, "compatible")) <= 0) 354 return (ENXIO); 355 356 sc = device_get_softc(dev); 357 sc->sc_dev = dev; 358 sc->sc_type = TWL_DEVICE_UNKNOWN; 359 360 while (len > 0) { 361 if (strncasecmp(compat, "ti,twl6030", 10) == 0) 362 sc->sc_type = TWL_DEVICE_6030; 363 else if (strncasecmp(compat, "ti,twl6025", 10) == 0) 364 sc->sc_type = TWL_DEVICE_6025; 365 else if (strncasecmp(compat, "ti,twl4030", 10) == 0) 366 sc->sc_type = TWL_DEVICE_4030; 367 368 if (sc->sc_type != TWL_DEVICE_UNKNOWN) 369 break; 370 371 /* Slide to the next sub-string. */ 372 l = strlen(compat) + 1; 373 compat += l; 374 len -= l; 375 } 376 377 switch (sc->sc_type) { 378 case TWL_DEVICE_4030: 379 device_set_desc(dev, "TI TWL4030/TPS659x0 Companion IC"); 380 break; 381 case TWL_DEVICE_6025: 382 device_set_desc(dev, "TI TWL6025 Companion IC"); 383 break; 384 case TWL_DEVICE_6030: 385 device_set_desc(dev, "TI TWL6030 Companion IC"); 386 break; 387 case TWL_DEVICE_UNKNOWN: 388 default: 389 return (ENXIO); 390 } 391 392 return (0); 393 } 394 395 static int 396 twl_attach(device_t dev) 397 { 398 struct twl_softc *sc; 399 400 sc = device_get_softc(dev); 401 sc->sc_dev = dev; 402 403 TWL_LOCK_INIT(sc); 404 405 /* We have to wait until interrupts are enabled. I2C read and write 406 * only works if the interrupts are available. 407 */ 408 sc->sc_scan_hook.ich_func = twl_scan; 409 sc->sc_scan_hook.ich_arg = dev; 410 411 if (config_intrhook_establish(&sc->sc_scan_hook) != 0) 412 return (ENOMEM); 413 414 /* FIXME: should be in DTS file */ 415 if ((sc->sc_vreg = device_add_child(dev, "twl_vreg", -1)) == NULL) 416 device_printf(dev, "could not allocate twl_vreg instance\n"); 417 if ((sc->sc_clks = device_add_child(dev, "twl_clks", -1)) == NULL) 418 device_printf(dev, "could not allocate twl_clks instance\n"); 419 420 bus_attach_children(dev); 421 return (0); 422 } 423 424 static int 425 twl_detach(device_t dev) 426 { 427 struct twl_softc *sc; 428 429 sc = device_get_softc(dev); 430 431 if (sc->sc_vreg) 432 device_delete_child(dev, sc->sc_vreg); 433 if (sc->sc_clks) 434 device_delete_child(dev, sc->sc_clks); 435 436 TWL_LOCK_DESTROY(sc); 437 438 return (0); 439 } 440 441 static device_method_t twl_methods[] = { 442 DEVMETHOD(device_probe, twl_probe), 443 DEVMETHOD(device_attach, twl_attach), 444 DEVMETHOD(device_detach, twl_detach), 445 446 {0, 0}, 447 }; 448 449 static driver_t twl_driver = { 450 "twl", 451 twl_methods, 452 sizeof(struct twl_softc), 453 }; 454 455 DRIVER_MODULE(twl, iicbus, twl_driver, 0, 0); 456 MODULE_VERSION(twl, 1); 457