1/* 2 * tps65010 - driver for tps6501x power management chips 3 * 4 * Copyright (C) 2004 Texas Instruments 5 * Copyright (C) 2004-2005 David Brownell 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation; either version 2 of the License, or 10 * (at your option) any later version. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with this program; if not, write to the Free Software 19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 20 */ 21 22#include <linux/kernel.h> 23#include <linux/module.h> 24#include <linux/init.h> 25#include <linux/slab.h> 26#include <linux/interrupt.h> 27#include <linux/i2c.h> 28#include <linux/delay.h> 29#include <linux/workqueue.h> 30#include <linux/debugfs.h> 31#include <linux/seq_file.h> 32#include <linux/mutex.h> 33#include <linux/platform_device.h> 34 35#include <linux/i2c/tps65010.h> 36 37#include <asm/gpio.h> 38 39 40/*-------------------------------------------------------------------------*/ 41 42#define DRIVER_VERSION "2 May 2005" 43#define DRIVER_NAME (tps65010_driver.driver.name) 44 45MODULE_DESCRIPTION("TPS6501x Power Management Driver"); 46MODULE_LICENSE("GPL"); 47 48static struct i2c_driver tps65010_driver; 49 50/*-------------------------------------------------------------------------*/ 51 52/* This driver handles a family of multipurpose chips, which incorporate 53 * voltage regulators, lithium ion/polymer battery charging, GPIOs, LEDs, 54 * and other features often needed in portable devices like cell phones 55 * or digital cameras. 56 * 57 * The tps65011 and tps65013 have different voltage settings compared 58 * to tps65010 and tps65012. The tps65013 has a NO_CHG status/irq. 59 * All except tps65010 have "wait" mode, possibly defaulted so that 60 * battery-insert != device-on. 61 * 62 * We could distinguish between some models by checking VDCDC1.UVLO or 63 * other registers, unless they've been changed already after powerup 64 * as part of board setup by a bootloader. 65 */ 66enum tps_model { 67 TPS65010, 68 TPS65011, 69 TPS65012, 70 TPS65013, 71}; 72 73struct tps65010 { 74 struct i2c_client *client; 75 struct mutex lock; 76 struct delayed_work work; 77 struct dentry *file; 78 unsigned charging:1; 79 unsigned por:1; 80 unsigned model:8; 81 u16 vbus; 82 unsigned long flags; 83#define FLAG_VBUS_CHANGED 0 84#define FLAG_IRQ_ENABLE 1 85 86 /* copies of last register state */ 87 u8 chgstatus, regstatus, chgconf; 88 u8 nmask1, nmask2; 89 90 u8 outmask; 91 struct gpio_chip chip; 92 struct platform_device *leds; 93}; 94 95#define POWER_POLL_DELAY msecs_to_jiffies(5000) 96 97/*-------------------------------------------------------------------------*/ 98 99#if defined(DEBUG) || defined(CONFIG_DEBUG_FS) 100 101static void dbg_chgstat(char *buf, size_t len, u8 chgstatus) 102{ 103 snprintf(buf, len, "%02x%s%s%s%s%s%s%s%s\n", 104 chgstatus, 105 (chgstatus & TPS_CHG_USB) ? " USB" : "", 106 (chgstatus & TPS_CHG_AC) ? " AC" : "", 107 (chgstatus & TPS_CHG_THERM) ? " therm" : "", 108 (chgstatus & TPS_CHG_TERM) ? " done" : 109 ((chgstatus & (TPS_CHG_USB|TPS_CHG_AC)) 110 ? " (charging)" : ""), 111 (chgstatus & TPS_CHG_TAPER_TMO) ? " taper_tmo" : "", 112 (chgstatus & TPS_CHG_CHG_TMO) ? " charge_tmo" : "", 113 (chgstatus & TPS_CHG_PRECHG_TMO) ? " prechg_tmo" : "", 114 (chgstatus & TPS_CHG_TEMP_ERR) ? " temp_err" : ""); 115} 116 117static void dbg_regstat(char *buf, size_t len, u8 regstatus) 118{ 119 snprintf(buf, len, "%02x %s%s%s%s%s%s%s%s\n", 120 regstatus, 121 (regstatus & TPS_REG_ONOFF) ? "off" : "(on)", 122 (regstatus & TPS_REG_COVER) ? " uncover" : "", 123 (regstatus & TPS_REG_UVLO) ? " UVLO" : "", 124 (regstatus & TPS_REG_NO_CHG) ? " NO_CHG" : "", 125 (regstatus & TPS_REG_PG_LD02) ? " ld02_bad" : "", 126 (regstatus & TPS_REG_PG_LD01) ? " ld01_bad" : "", 127 (regstatus & TPS_REG_PG_MAIN) ? " main_bad" : "", 128 (regstatus & TPS_REG_PG_CORE) ? " core_bad" : ""); 129} 130 131static void dbg_chgconf(int por, char *buf, size_t len, u8 chgconfig) 132{ 133 const char *hibit; 134 135 if (por) 136 hibit = (chgconfig & TPS_CHARGE_POR) 137 ? "POR=69ms" : "POR=1sec"; 138 else 139 hibit = (chgconfig & TPS65013_AUA) ? "AUA" : ""; 140 141 snprintf(buf, len, "%02x %s%s%s AC=%d%% USB=%dmA %sCharge\n", 142 chgconfig, hibit, 143 (chgconfig & TPS_CHARGE_RESET) ? " reset" : "", 144 (chgconfig & TPS_CHARGE_FAST) ? " fast" : "", 145 ({int p; switch ((chgconfig >> 3) & 3) { 146 case 3: p = 100; break; 147 case 2: p = 75; break; 148 case 1: p = 50; break; 149 default: p = 25; break; 150 }; p; }), 151 (chgconfig & TPS_VBUS_CHARGING) 152 ? ((chgconfig & TPS_VBUS_500MA) ? 500 : 100) 153 : 0, 154 (chgconfig & TPS_CHARGE_ENABLE) ? "" : "No"); 155} 156 157#endif 158 159#ifdef DEBUG 160 161static void show_chgstatus(const char *label, u8 chgstatus) 162{ 163 char buf [100]; 164 165 dbg_chgstat(buf, sizeof buf, chgstatus); 166 pr_debug("%s: %s %s", DRIVER_NAME, label, buf); 167} 168 169static void show_regstatus(const char *label, u8 regstatus) 170{ 171 char buf [100]; 172 173 dbg_regstat(buf, sizeof buf, regstatus); 174 pr_debug("%s: %s %s", DRIVER_NAME, label, buf); 175} 176 177static void show_chgconfig(int por, const char *label, u8 chgconfig) 178{ 179 char buf [100]; 180 181 dbg_chgconf(por, buf, sizeof buf, chgconfig); 182 pr_debug("%s: %s %s", DRIVER_NAME, label, buf); 183} 184 185#else 186 187static inline void show_chgstatus(const char *label, u8 chgstatus) { } 188static inline void show_regstatus(const char *label, u8 chgstatus) { } 189static inline void show_chgconfig(int por, const char *label, u8 chgconfig) { } 190 191#endif 192 193#ifdef CONFIG_DEBUG_FS 194 195static int dbg_show(struct seq_file *s, void *_) 196{ 197 struct tps65010 *tps = s->private; 198 u8 value, v2; 199 unsigned i; 200 char buf[100]; 201 const char *chip; 202 203 switch (tps->model) { 204 case TPS65010: chip = "tps65010"; break; 205 case TPS65011: chip = "tps65011"; break; 206 case TPS65012: chip = "tps65012"; break; 207 case TPS65013: chip = "tps65013"; break; 208 default: chip = NULL; break; 209 } 210 seq_printf(s, "driver %s\nversion %s\nchip %s\n\n", 211 DRIVER_NAME, DRIVER_VERSION, chip); 212 213 mutex_lock(&tps->lock); 214 215 216 seq_printf(s, "%scharging\n\n", tps->charging ? "" : "(not) "); 217 218 219 /* registers for monitoring battery charging and status; note 220 * that reading chgstat and regstat may ack IRQs... 221 */ 222 value = i2c_smbus_read_byte_data(tps->client, TPS_CHGCONFIG); 223 dbg_chgconf(tps->por, buf, sizeof buf, value); 224 seq_printf(s, "chgconfig %s", buf); 225 226 value = i2c_smbus_read_byte_data(tps->client, TPS_CHGSTATUS); 227 dbg_chgstat(buf, sizeof buf, value); 228 seq_printf(s, "chgstat %s", buf); 229 value = i2c_smbus_read_byte_data(tps->client, TPS_MASK1); 230 dbg_chgstat(buf, sizeof buf, value); 231 seq_printf(s, "mask1 %s", buf); 232 /* ignore ackint1 */ 233 234 value = i2c_smbus_read_byte_data(tps->client, TPS_REGSTATUS); 235 dbg_regstat(buf, sizeof buf, value); 236 seq_printf(s, "regstat %s", buf); 237 value = i2c_smbus_read_byte_data(tps->client, TPS_MASK2); 238 dbg_regstat(buf, sizeof buf, value); 239 seq_printf(s, "mask2 %s\n", buf); 240 /* ignore ackint2 */ 241 242 (void) schedule_delayed_work(&tps->work, POWER_POLL_DELAY); 243 244 245 /* VMAIN voltage, enable lowpower, etc */ 246 value = i2c_smbus_read_byte_data(tps->client, TPS_VDCDC1); 247 seq_printf(s, "vdcdc1 %02x\n", value); 248 249 /* VCORE voltage, vibrator on/off */ 250 value = i2c_smbus_read_byte_data(tps->client, TPS_VDCDC2); 251 seq_printf(s, "vdcdc2 %02x\n", value); 252 253 /* both LD0s, and their lowpower behavior */ 254 value = i2c_smbus_read_byte_data(tps->client, TPS_VREGS1); 255 seq_printf(s, "vregs1 %02x\n\n", value); 256 257 258 /* LEDs and GPIOs */ 259 value = i2c_smbus_read_byte_data(tps->client, TPS_LED1_ON); 260 v2 = i2c_smbus_read_byte_data(tps->client, TPS_LED1_PER); 261 seq_printf(s, "led1 %s, on=%02x, per=%02x, %d/%d msec\n", 262 (value & 0x80) 263 ? ((v2 & 0x80) ? "on" : "off") 264 : ((v2 & 0x80) ? "blink" : "(nPG)"), 265 value, v2, 266 (value & 0x7f) * 10, (v2 & 0x7f) * 100); 267 268 value = i2c_smbus_read_byte_data(tps->client, TPS_LED2_ON); 269 v2 = i2c_smbus_read_byte_data(tps->client, TPS_LED2_PER); 270 seq_printf(s, "led2 %s, on=%02x, per=%02x, %d/%d msec\n", 271 (value & 0x80) 272 ? ((v2 & 0x80) ? "on" : "off") 273 : ((v2 & 0x80) ? "blink" : "off"), 274 value, v2, 275 (value & 0x7f) * 10, (v2 & 0x7f) * 100); 276 277 value = i2c_smbus_read_byte_data(tps->client, TPS_DEFGPIO); 278 v2 = i2c_smbus_read_byte_data(tps->client, TPS_MASK3); 279 seq_printf(s, "defgpio %02x mask3 %02x\n", value, v2); 280 281 for (i = 0; i < 4; i++) { 282 if (value & (1 << (4 + i))) 283 seq_printf(s, " gpio%d-out %s\n", i + 1, 284 (value & (1 << i)) ? "low" : "hi "); 285 else 286 seq_printf(s, " gpio%d-in %s %s %s\n", i + 1, 287 (value & (1 << i)) ? "hi " : "low", 288 (v2 & (1 << i)) ? "no-irq" : "irq", 289 (v2 & (1 << (4 + i))) ? "rising" : "falling"); 290 } 291 292 mutex_unlock(&tps->lock); 293 return 0; 294} 295 296static int dbg_tps_open(struct inode *inode, struct file *file) 297{ 298 return single_open(file, dbg_show, inode->i_private); 299} 300 301static const struct file_operations debug_fops = { 302 .open = dbg_tps_open, 303 .read = seq_read, 304 .llseek = seq_lseek, 305 .release = single_release, 306}; 307 308#define DEBUG_FOPS &debug_fops 309 310#else 311#define DEBUG_FOPS NULL 312#endif 313 314/*-------------------------------------------------------------------------*/ 315 316/* handle IRQS in a task context, so we can use I2C calls */ 317static void tps65010_interrupt(struct tps65010 *tps) 318{ 319 u8 tmp = 0, mask, poll; 320 321 /* IRQs won't trigger for certain events, but we can get 322 * others by polling (normally, with external power applied). 323 */ 324 poll = 0; 325 326 /* regstatus irqs */ 327 if (tps->nmask2) { 328 tmp = i2c_smbus_read_byte_data(tps->client, TPS_REGSTATUS); 329 mask = tmp ^ tps->regstatus; 330 tps->regstatus = tmp; 331 mask &= tps->nmask2; 332 } else 333 mask = 0; 334 if (mask) { 335 tps->regstatus = tmp; 336 /* may need to shut something down ... */ 337 338 /* "off" usually means deep sleep */ 339 if (tmp & TPS_REG_ONOFF) { 340 pr_info("%s: power off button\n", DRIVER_NAME); 341 poll = 1; 342 } 343 } 344 345 /* chgstatus irqs */ 346 if (tps->nmask1) { 347 tmp = i2c_smbus_read_byte_data(tps->client, TPS_CHGSTATUS); 348 mask = tmp ^ tps->chgstatus; 349 tps->chgstatus = tmp; 350 mask &= tps->nmask1; 351 } else 352 mask = 0; 353 if (mask) { 354 unsigned charging = 0; 355 356 show_chgstatus("chg/irq", tmp); 357 if (tmp & (TPS_CHG_USB|TPS_CHG_AC)) 358 show_chgconfig(tps->por, "conf", tps->chgconf); 359 360 /* Unless it was turned off or disabled, we charge any 361 * battery whenever there's power available for it 362 * and the charger hasn't been disabled. 363 */ 364 if (!(tps->chgstatus & ~(TPS_CHG_USB|TPS_CHG_AC)) 365 && (tps->chgstatus & (TPS_CHG_USB|TPS_CHG_AC)) 366 && (tps->chgconf & TPS_CHARGE_ENABLE) 367 ) { 368 if (tps->chgstatus & TPS_CHG_USB) { 369 /* VBUS options are readonly until reconnect */ 370 if (mask & TPS_CHG_USB) 371 set_bit(FLAG_VBUS_CHANGED, &tps->flags); 372 charging = 1; 373 } else if (tps->chgstatus & TPS_CHG_AC) 374 charging = 1; 375 } 376 if (charging != tps->charging) { 377 tps->charging = charging; 378 pr_info("%s: battery %scharging\n", 379 DRIVER_NAME, charging ? "" : 380 ((tps->chgstatus & (TPS_CHG_USB|TPS_CHG_AC)) 381 ? "NOT " : "dis")); 382 } 383 } 384 385 /* always poll to detect (a) power removal, without tps65013 386 * NO_CHG IRQ; or (b) restart of charging after stop. 387 */ 388 if ((tps->model != TPS65013 || !tps->charging) 389 && (tps->chgstatus & (TPS_CHG_USB|TPS_CHG_AC))) 390 poll = 1; 391 if (poll) 392 (void) schedule_delayed_work(&tps->work, POWER_POLL_DELAY); 393 394 /* also potentially gpio-in rise or fall */ 395} 396 397/* handle IRQs and polling using keventd for now */ 398static void tps65010_work(struct work_struct *work) 399{ 400 struct tps65010 *tps; 401 402 tps = container_of(work, struct tps65010, work.work); 403 mutex_lock(&tps->lock); 404 405 tps65010_interrupt(tps); 406 407 if (test_and_clear_bit(FLAG_VBUS_CHANGED, &tps->flags)) { 408 int status; 409 u8 chgconfig, tmp; 410 411 chgconfig = i2c_smbus_read_byte_data(tps->client, 412 TPS_CHGCONFIG); 413 chgconfig &= ~(TPS_VBUS_500MA | TPS_VBUS_CHARGING); 414 if (tps->vbus == 500) 415 chgconfig |= TPS_VBUS_500MA | TPS_VBUS_CHARGING; 416 else if (tps->vbus >= 100) 417 chgconfig |= TPS_VBUS_CHARGING; 418 419 status = i2c_smbus_write_byte_data(tps->client, 420 TPS_CHGCONFIG, chgconfig); 421 422 /* vbus update fails unless VBUS is connected! */ 423 tmp = i2c_smbus_read_byte_data(tps->client, TPS_CHGCONFIG); 424 tps->chgconf = tmp; 425 show_chgconfig(tps->por, "update vbus", tmp); 426 } 427 428 if (test_and_clear_bit(FLAG_IRQ_ENABLE, &tps->flags)) 429 enable_irq(tps->client->irq); 430 431 mutex_unlock(&tps->lock); 432} 433 434static irqreturn_t tps65010_irq(int irq, void *_tps) 435{ 436 struct tps65010 *tps = _tps; 437 438 disable_irq_nosync(irq); 439 set_bit(FLAG_IRQ_ENABLE, &tps->flags); 440 (void) schedule_work(&tps->work.work); 441 return IRQ_HANDLED; 442} 443 444/*-------------------------------------------------------------------------*/ 445 446/* offsets 0..3 == GPIO1..GPIO4 447 * offsets 4..5 == LED1/nPG, LED2 (we set one of the non-BLINK modes) 448 * offset 6 == vibrator motor driver 449 */ 450static void 451tps65010_gpio_set(struct gpio_chip *chip, unsigned offset, int value) 452{ 453 if (offset < 4) 454 tps65010_set_gpio_out_value(offset + 1, value); 455 else if (offset < 6) 456 tps65010_set_led(offset - 3, value ? ON : OFF); 457 else 458 tps65010_set_vib(value); 459} 460 461static int 462tps65010_output(struct gpio_chip *chip, unsigned offset, int value) 463{ 464 /* GPIOs may be input-only */ 465 if (offset < 4) { 466 struct tps65010 *tps; 467 468 tps = container_of(chip, struct tps65010, chip); 469 if (!(tps->outmask & (1 << offset))) 470 return -EINVAL; 471 tps65010_set_gpio_out_value(offset + 1, value); 472 } else if (offset < 6) 473 tps65010_set_led(offset - 3, value ? ON : OFF); 474 else 475 tps65010_set_vib(value); 476 477 return 0; 478} 479 480static int tps65010_gpio_get(struct gpio_chip *chip, unsigned offset) 481{ 482 int value; 483 struct tps65010 *tps; 484 485 tps = container_of(chip, struct tps65010, chip); 486 487 if (offset < 4) { 488 value = i2c_smbus_read_byte_data(tps->client, TPS_DEFGPIO); 489 if (value < 0) 490 return 0; 491 if (value & (1 << (offset + 4))) /* output */ 492 return !(value & (1 << offset)); 493 else /* input */ 494 return (value & (1 << offset)); 495 } 496 497 /* REVISIT we *could* report LED1/nPG and LED2 state ... */ 498 return 0; 499} 500 501 502/*-------------------------------------------------------------------------*/ 503 504static struct tps65010 *the_tps; 505 506static int __exit tps65010_remove(struct i2c_client *client) 507{ 508 struct tps65010 *tps = i2c_get_clientdata(client); 509 struct tps65010_board *board = client->dev.platform_data; 510 511 if (board && board->teardown) { 512 int status = board->teardown(client, board->context); 513 if (status < 0) 514 dev_dbg(&client->dev, "board %s %s err %d\n", 515 "teardown", client->name, status); 516 } 517 if (client->irq > 0) 518 free_irq(client->irq, tps); 519 cancel_delayed_work(&tps->work); 520 flush_scheduled_work(); 521 debugfs_remove(tps->file); 522 kfree(tps); 523 the_tps = NULL; 524 return 0; 525} 526 527static int tps65010_probe(struct i2c_client *client, 528 const struct i2c_device_id *id) 529{ 530 struct tps65010 *tps; 531 int status; 532 struct tps65010_board *board = client->dev.platform_data; 533 534 if (the_tps) { 535 dev_dbg(&client->dev, "only one tps6501x chip allowed\n"); 536 return -ENODEV; 537 } 538 539 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA)) 540 return -EINVAL; 541 542 tps = kzalloc(sizeof *tps, GFP_KERNEL); 543 if (!tps) 544 return -ENOMEM; 545 546 mutex_init(&tps->lock); 547 INIT_DELAYED_WORK(&tps->work, tps65010_work); 548 tps->client = client; 549 tps->model = id->driver_data; 550 551 /* the IRQ is active low, but many gpio lines can't support that 552 * so this driver uses falling-edge triggers instead. 553 */ 554 if (client->irq > 0) { 555 status = request_irq(client->irq, tps65010_irq, 556 IRQF_SAMPLE_RANDOM | IRQF_TRIGGER_FALLING, 557 DRIVER_NAME, tps); 558 if (status < 0) { 559 dev_dbg(&client->dev, "can't get IRQ %d, err %d\n", 560 client->irq, status); 561 goto fail1; 562 } 563 disable_irq(client->irq); 564 set_bit(FLAG_IRQ_ENABLE, &tps->flags); 565 } else 566 dev_warn(&client->dev, "IRQ not configured!\n"); 567 568 569 switch (tps->model) { 570 case TPS65010: 571 case TPS65012: 572 tps->por = 1; 573 break; 574 /* else CHGCONFIG.POR is replaced by AUA, enabling a WAIT mode */ 575 } 576 tps->chgconf = i2c_smbus_read_byte_data(client, TPS_CHGCONFIG); 577 show_chgconfig(tps->por, "conf/init", tps->chgconf); 578 579 show_chgstatus("chg/init", 580 i2c_smbus_read_byte_data(client, TPS_CHGSTATUS)); 581 show_regstatus("reg/init", 582 i2c_smbus_read_byte_data(client, TPS_REGSTATUS)); 583 584 pr_debug("%s: vdcdc1 0x%02x, vdcdc2 %02x, vregs1 %02x\n", DRIVER_NAME, 585 i2c_smbus_read_byte_data(client, TPS_VDCDC1), 586 i2c_smbus_read_byte_data(client, TPS_VDCDC2), 587 i2c_smbus_read_byte_data(client, TPS_VREGS1)); 588 pr_debug("%s: defgpio 0x%02x, mask3 0x%02x\n", DRIVER_NAME, 589 i2c_smbus_read_byte_data(client, TPS_DEFGPIO), 590 i2c_smbus_read_byte_data(client, TPS_MASK3)); 591 592 i2c_set_clientdata(client, tps); 593 the_tps = tps; 594 595#if defined(CONFIG_USB_GADGET) && !defined(CONFIG_USB_OTG) 596 /* USB hosts can't draw VBUS. OTG devices could, later 597 * when OTG infrastructure enables it. USB peripherals 598 * could be relying on VBUS while booting, though. 599 */ 600 tps->vbus = 100; 601#endif 602 603 /* unmask the "interesting" irqs, then poll once to 604 * kickstart monitoring, initialize shadowed status 605 * registers, and maybe disable VBUS draw. 606 */ 607 tps->nmask1 = ~0; 608 (void) i2c_smbus_write_byte_data(client, TPS_MASK1, ~tps->nmask1); 609 610 tps->nmask2 = TPS_REG_ONOFF; 611 if (tps->model == TPS65013) 612 tps->nmask2 |= TPS_REG_NO_CHG; 613 (void) i2c_smbus_write_byte_data(client, TPS_MASK2, ~tps->nmask2); 614 615 (void) i2c_smbus_write_byte_data(client, TPS_MASK3, 0x0f 616 | i2c_smbus_read_byte_data(client, TPS_MASK3)); 617 618 tps65010_work(&tps->work.work); 619 620 tps->file = debugfs_create_file(DRIVER_NAME, S_IRUGO, NULL, 621 tps, DEBUG_FOPS); 622 623 /* optionally register GPIOs */ 624 if (board && board->base != 0) { 625 tps->outmask = board->outmask; 626 627 tps->chip.label = client->name; 628 tps->chip.dev = &client->dev; 629 tps->chip.owner = THIS_MODULE; 630 631 tps->chip.set = tps65010_gpio_set; 632 tps->chip.direction_output = tps65010_output; 633 634 /* NOTE: only partial support for inputs; nyet IRQs */ 635 tps->chip.get = tps65010_gpio_get; 636 637 tps->chip.base = board->base; 638 tps->chip.ngpio = 7; 639 tps->chip.can_sleep = 1; 640 641 status = gpiochip_add(&tps->chip); 642 if (status < 0) 643 dev_err(&client->dev, "can't add gpiochip, err %d\n", 644 status); 645 else if (board->setup) { 646 status = board->setup(client, board->context); 647 if (status < 0) { 648 dev_dbg(&client->dev, 649 "board %s %s err %d\n", 650 "setup", client->name, status); 651 status = 0; 652 } 653 } 654 } 655 656 return 0; 657fail1: 658 kfree(tps); 659 return status; 660} 661 662static const struct i2c_device_id tps65010_id[] = { 663 { "tps65010", TPS65010 }, 664 { "tps65011", TPS65011 }, 665 { "tps65012", TPS65012 }, 666 { "tps65013", TPS65013 }, 667 { "tps65014", TPS65011 }, /* tps65011 charging at 6.5V max */ 668 { } 669}; 670MODULE_DEVICE_TABLE(i2c, tps65010_id); 671 672static struct i2c_driver tps65010_driver = { 673 .driver = { 674 .name = "tps65010", 675 }, 676 .probe = tps65010_probe, 677 .remove = __exit_p(tps65010_remove), 678 .id_table = tps65010_id, 679}; 680 681/*-------------------------------------------------------------------------*/ 682 683/* Draw from VBUS: 684 * 0 mA -- DON'T DRAW (might supply power instead) 685 * 100 mA -- usb unit load (slowest charge rate) 686 * 500 mA -- usb high power (fast battery charge) 687 */ 688int tps65010_set_vbus_draw(unsigned mA) 689{ 690 unsigned long flags; 691 692 if (!the_tps) 693 return -ENODEV; 694 695 /* assumes non-SMP */ 696 local_irq_save(flags); 697 if (mA >= 500) 698 mA = 500; 699 else if (mA >= 100) 700 mA = 100; 701 else 702 mA = 0; 703 the_tps->vbus = mA; 704 if ((the_tps->chgstatus & TPS_CHG_USB) 705 && test_and_set_bit( 706 FLAG_VBUS_CHANGED, &the_tps->flags)) { 707 /* gadget drivers call this in_irq() */ 708 (void) schedule_work(&the_tps->work.work); 709 } 710 local_irq_restore(flags); 711 712 return 0; 713} 714EXPORT_SYMBOL(tps65010_set_vbus_draw); 715 716/*-------------------------------------------------------------------------*/ 717/* tps65010_set_gpio_out_value parameter: 718 * gpio: GPIO1, GPIO2, GPIO3 or GPIO4 719 * value: LOW or HIGH 720 */ 721int tps65010_set_gpio_out_value(unsigned gpio, unsigned value) 722{ 723 int status; 724 unsigned defgpio; 725 726 if (!the_tps) 727 return -ENODEV; 728 if ((gpio < GPIO1) || (gpio > GPIO4)) 729 return -EINVAL; 730 731 mutex_lock(&the_tps->lock); 732 733 defgpio = i2c_smbus_read_byte_data(the_tps->client, TPS_DEFGPIO); 734 735 /* Configure GPIO for output */ 736 defgpio |= 1 << (gpio + 3); 737 738 /* Writing 1 forces a logic 0 on that GPIO and vice versa */ 739 switch (value) { 740 case LOW: 741 defgpio |= 1 << (gpio - 1); /* set GPIO low by writing 1 */ 742 break; 743 /* case HIGH: */ 744 default: 745 defgpio &= ~(1 << (gpio - 1)); /* set GPIO high by writing 0 */ 746 break; 747 } 748 749 status = i2c_smbus_write_byte_data(the_tps->client, 750 TPS_DEFGPIO, defgpio); 751 752 pr_debug("%s: gpio%dout = %s, defgpio 0x%02x\n", DRIVER_NAME, 753 gpio, value ? "high" : "low", 754 i2c_smbus_read_byte_data(the_tps->client, TPS_DEFGPIO)); 755 756 mutex_unlock(&the_tps->lock); 757 return status; 758} 759EXPORT_SYMBOL(tps65010_set_gpio_out_value); 760 761/*-------------------------------------------------------------------------*/ 762/* tps65010_set_led parameter: 763 * led: LED1 or LED2 764 * mode: ON, OFF or BLINK 765 */ 766int tps65010_set_led(unsigned led, unsigned mode) 767{ 768 int status; 769 unsigned led_on, led_per, offs; 770 771 if (!the_tps) 772 return -ENODEV; 773 774 if (led == LED1) 775 offs = 0; 776 else { 777 offs = 2; 778 led = LED2; 779 } 780 781 mutex_lock(&the_tps->lock); 782 783 pr_debug("%s: led%i_on 0x%02x\n", DRIVER_NAME, led, 784 i2c_smbus_read_byte_data(the_tps->client, 785 TPS_LED1_ON + offs)); 786 787 pr_debug("%s: led%i_per 0x%02x\n", DRIVER_NAME, led, 788 i2c_smbus_read_byte_data(the_tps->client, 789 TPS_LED1_PER + offs)); 790 791 switch (mode) { 792 case OFF: 793 led_on = 1 << 7; 794 led_per = 0 << 7; 795 break; 796 case ON: 797 led_on = 1 << 7; 798 led_per = 1 << 7; 799 break; 800 case BLINK: 801 led_on = 0x30 | (0 << 7); 802 led_per = 0x08 | (1 << 7); 803 break; 804 default: 805 printk(KERN_ERR "%s: Wrong mode parameter for set_led()\n", 806 DRIVER_NAME); 807 mutex_unlock(&the_tps->lock); 808 return -EINVAL; 809 } 810 811 status = i2c_smbus_write_byte_data(the_tps->client, 812 TPS_LED1_ON + offs, led_on); 813 814 if (status != 0) { 815 printk(KERN_ERR "%s: Failed to write led%i_on register\n", 816 DRIVER_NAME, led); 817 mutex_unlock(&the_tps->lock); 818 return status; 819 } 820 821 pr_debug("%s: led%i_on 0x%02x\n", DRIVER_NAME, led, 822 i2c_smbus_read_byte_data(the_tps->client, TPS_LED1_ON + offs)); 823 824 status = i2c_smbus_write_byte_data(the_tps->client, 825 TPS_LED1_PER + offs, led_per); 826 827 if (status != 0) { 828 printk(KERN_ERR "%s: Failed to write led%i_per register\n", 829 DRIVER_NAME, led); 830 mutex_unlock(&the_tps->lock); 831 return status; 832 } 833 834 pr_debug("%s: led%i_per 0x%02x\n", DRIVER_NAME, led, 835 i2c_smbus_read_byte_data(the_tps->client, 836 TPS_LED1_PER + offs)); 837 838 mutex_unlock(&the_tps->lock); 839 840 return status; 841} 842EXPORT_SYMBOL(tps65010_set_led); 843 844/*-------------------------------------------------------------------------*/ 845/* tps65010_set_vib parameter: 846 * value: ON or OFF 847 */ 848int tps65010_set_vib(unsigned value) 849{ 850 int status; 851 unsigned vdcdc2; 852 853 if (!the_tps) 854 return -ENODEV; 855 856 mutex_lock(&the_tps->lock); 857 858 vdcdc2 = i2c_smbus_read_byte_data(the_tps->client, TPS_VDCDC2); 859 vdcdc2 &= ~(1 << 1); 860 if (value) 861 vdcdc2 |= (1 << 1); 862 status = i2c_smbus_write_byte_data(the_tps->client, 863 TPS_VDCDC2, vdcdc2); 864 865 pr_debug("%s: vibrator %s\n", DRIVER_NAME, value ? "on" : "off"); 866 867 mutex_unlock(&the_tps->lock); 868 return status; 869} 870EXPORT_SYMBOL(tps65010_set_vib); 871 872/*-------------------------------------------------------------------------*/ 873/* tps65010_set_low_pwr parameter: 874 * mode: ON or OFF 875 */ 876int tps65010_set_low_pwr(unsigned mode) 877{ 878 int status; 879 unsigned vdcdc1; 880 881 if (!the_tps) 882 return -ENODEV; 883 884 mutex_lock(&the_tps->lock); 885 886 pr_debug("%s: %s low_pwr, vdcdc1 0x%02x\n", DRIVER_NAME, 887 mode ? "enable" : "disable", 888 i2c_smbus_read_byte_data(the_tps->client, TPS_VDCDC1)); 889 890 vdcdc1 = i2c_smbus_read_byte_data(the_tps->client, TPS_VDCDC1); 891 892 switch (mode) { 893 case OFF: 894 vdcdc1 &= ~TPS_ENABLE_LP; /* disable ENABLE_LP bit */ 895 break; 896 /* case ON: */ 897 default: 898 vdcdc1 |= TPS_ENABLE_LP; /* enable ENABLE_LP bit */ 899 break; 900 } 901 902 status = i2c_smbus_write_byte_data(the_tps->client, 903 TPS_VDCDC1, vdcdc1); 904 905 if (status != 0) 906 printk(KERN_ERR "%s: Failed to write vdcdc1 register\n", 907 DRIVER_NAME); 908 else 909 pr_debug("%s: vdcdc1 0x%02x\n", DRIVER_NAME, 910 i2c_smbus_read_byte_data(the_tps->client, TPS_VDCDC1)); 911 912 mutex_unlock(&the_tps->lock); 913 914 return status; 915} 916EXPORT_SYMBOL(tps65010_set_low_pwr); 917 918/*-------------------------------------------------------------------------*/ 919/* tps65010_config_vregs1 parameter: 920 * value to be written to VREGS1 register 921 * Note: The complete register is written, set all bits you need 922 */ 923int tps65010_config_vregs1(unsigned value) 924{ 925 int status; 926 927 if (!the_tps) 928 return -ENODEV; 929 930 mutex_lock(&the_tps->lock); 931 932 pr_debug("%s: vregs1 0x%02x\n", DRIVER_NAME, 933 i2c_smbus_read_byte_data(the_tps->client, TPS_VREGS1)); 934 935 status = i2c_smbus_write_byte_data(the_tps->client, 936 TPS_VREGS1, value); 937 938 if (status != 0) 939 printk(KERN_ERR "%s: Failed to write vregs1 register\n", 940 DRIVER_NAME); 941 else 942 pr_debug("%s: vregs1 0x%02x\n", DRIVER_NAME, 943 i2c_smbus_read_byte_data(the_tps->client, TPS_VREGS1)); 944 945 mutex_unlock(&the_tps->lock); 946 947 return status; 948} 949EXPORT_SYMBOL(tps65010_config_vregs1); 950 951int tps65010_config_vdcdc2(unsigned value) 952{ 953 struct i2c_client *c; 954 int status; 955 956 if (!the_tps) 957 return -ENODEV; 958 959 c = the_tps->client; 960 mutex_lock(&the_tps->lock); 961 962 pr_debug("%s: vdcdc2 0x%02x\n", DRIVER_NAME, 963 i2c_smbus_read_byte_data(c, TPS_VDCDC2)); 964 965 status = i2c_smbus_write_byte_data(c, TPS_VDCDC2, value); 966 967 if (status != 0) 968 printk(KERN_ERR "%s: Failed to write vdcdc2 register\n", 969 DRIVER_NAME); 970 else 971 pr_debug("%s: vregs1 0x%02x\n", DRIVER_NAME, 972 i2c_smbus_read_byte_data(c, TPS_VDCDC2)); 973 974 mutex_unlock(&the_tps->lock); 975 return status; 976} 977EXPORT_SYMBOL(tps65010_config_vdcdc2); 978 979/*-------------------------------------------------------------------------*/ 980/* tps65013_set_low_pwr parameter: 981 * mode: ON or OFF 982 */ 983 984 985int tps65013_set_low_pwr(unsigned mode) 986{ 987 int status; 988 unsigned vdcdc1, chgconfig; 989 990 if (!the_tps || the_tps->por) 991 return -ENODEV; 992 993 mutex_lock(&the_tps->lock); 994 995 pr_debug("%s: %s low_pwr, chgconfig 0x%02x vdcdc1 0x%02x\n", 996 DRIVER_NAME, 997 mode ? "enable" : "disable", 998 i2c_smbus_read_byte_data(the_tps->client, TPS_CHGCONFIG), 999 i2c_smbus_read_byte_data(the_tps->client, TPS_VDCDC1)); 1000 1001 chgconfig = i2c_smbus_read_byte_data(the_tps->client, TPS_CHGCONFIG); 1002 vdcdc1 = i2c_smbus_read_byte_data(the_tps->client, TPS_VDCDC1); 1003 1004 switch (mode) { 1005 case OFF: 1006 chgconfig &= ~TPS65013_AUA; /* disable AUA bit */ 1007 vdcdc1 &= ~TPS_ENABLE_LP; /* disable ENABLE_LP bit */ 1008 break; 1009 /* case ON: */ 1010 default: 1011 chgconfig |= TPS65013_AUA; /* enable AUA bit */ 1012 vdcdc1 |= TPS_ENABLE_LP; /* enable ENABLE_LP bit */ 1013 break; 1014 } 1015 1016 status = i2c_smbus_write_byte_data(the_tps->client, 1017 TPS_CHGCONFIG, chgconfig); 1018 if (status != 0) { 1019 printk(KERN_ERR "%s: Failed to write chconfig register\n", 1020 DRIVER_NAME); 1021 mutex_unlock(&the_tps->lock); 1022 return status; 1023 } 1024 1025 chgconfig = i2c_smbus_read_byte_data(the_tps->client, TPS_CHGCONFIG); 1026 the_tps->chgconf = chgconfig; 1027 show_chgconfig(0, "chgconf", chgconfig); 1028 1029 status = i2c_smbus_write_byte_data(the_tps->client, 1030 TPS_VDCDC1, vdcdc1); 1031 1032 if (status != 0) 1033 printk(KERN_ERR "%s: Failed to write vdcdc1 register\n", 1034 DRIVER_NAME); 1035 else 1036 pr_debug("%s: vdcdc1 0x%02x\n", DRIVER_NAME, 1037 i2c_smbus_read_byte_data(the_tps->client, TPS_VDCDC1)); 1038 1039 mutex_unlock(&the_tps->lock); 1040 1041 return status; 1042} 1043EXPORT_SYMBOL(tps65013_set_low_pwr); 1044 1045/*-------------------------------------------------------------------------*/ 1046 1047static int __init tps_init(void) 1048{ 1049 u32 tries = 3; 1050 int status = -ENODEV; 1051 1052 printk(KERN_INFO "%s: version %s\n", DRIVER_NAME, DRIVER_VERSION); 1053 1054 /* some boards have startup glitches */ 1055 while (tries--) { 1056 status = i2c_add_driver(&tps65010_driver); 1057 if (the_tps) 1058 break; 1059 i2c_del_driver(&tps65010_driver); 1060 if (!tries) { 1061 printk(KERN_ERR "%s: no chip?\n", DRIVER_NAME); 1062 return -ENODEV; 1063 } 1064 pr_debug("%s: re-probe ...\n", DRIVER_NAME); 1065 msleep(10); 1066 } 1067 1068 return status; 1069} 1070/* NOTE: this MUST be initialized before the other parts of the system 1071 * that rely on it ... but after the i2c bus on which this relies. 1072 * That is, much earlier than on PC-type systems, which don't often use 1073 * I2C as a core system bus. 1074 */ 1075subsys_initcall(tps_init); 1076 1077static void __exit tps_exit(void) 1078{ 1079 i2c_del_driver(&tps65010_driver); 1080} 1081module_exit(tps_exit); 1082