tps65010.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969
  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. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/init.h>
  24. #include <linux/slab.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/i2c.h>
  27. #include <linux/delay.h>
  28. #include <linux/workqueue.h>
  29. #include <linux/debugfs.h>
  30. #include <linux/seq_file.h>
  31. #include <linux/mutex.h>
  32. #include <asm/arch/tps65010.h>
  33. /*-------------------------------------------------------------------------*/
  34. #define DRIVER_VERSION "2 May 2005"
  35. #define DRIVER_NAME (tps65010_driver.driver.name)
  36. MODULE_DESCRIPTION("TPS6501x Power Management Driver");
  37. MODULE_LICENSE("GPL");
  38. static struct i2c_driver tps65010_driver;
  39. /*-------------------------------------------------------------------------*/
  40. /* This driver handles a family of multipurpose chips, which incorporate
  41. * voltage regulators, lithium ion/polymer battery charging, GPIOs, LEDs,
  42. * and other features often needed in portable devices like cell phones
  43. * or digital cameras.
  44. *
  45. * The tps65011 and tps65013 have different voltage settings compared
  46. * to tps65010 and tps65012. The tps65013 has a NO_CHG status/irq.
  47. * All except tps65010 have "wait" mode, possibly defaulted so that
  48. * battery-insert != device-on.
  49. *
  50. * We could distinguish between some models by checking VDCDC1.UVLO or
  51. * other registers, unless they've been changed already after powerup
  52. * as part of board setup by a bootloader.
  53. */
  54. enum tps_model {
  55. TPS_UNKNOWN = 0,
  56. TPS65010,
  57. TPS65011,
  58. TPS65012,
  59. TPS65013,
  60. };
  61. struct tps65010 {
  62. struct i2c_client *client;
  63. struct mutex lock;
  64. struct delayed_work work;
  65. struct dentry *file;
  66. unsigned charging:1;
  67. unsigned por:1;
  68. unsigned model:8;
  69. u16 vbus;
  70. unsigned long flags;
  71. #define FLAG_VBUS_CHANGED 0
  72. #define FLAG_IRQ_ENABLE 1
  73. /* copies of last register state */
  74. u8 chgstatus, regstatus, chgconf;
  75. u8 nmask1, nmask2;
  76. /* not currently tracking GPIO state */
  77. };
  78. #define POWER_POLL_DELAY msecs_to_jiffies(5000)
  79. /*-------------------------------------------------------------------------*/
  80. #if defined(DEBUG) || defined(CONFIG_DEBUG_FS)
  81. static void dbg_chgstat(char *buf, size_t len, u8 chgstatus)
  82. {
  83. snprintf(buf, len, "%02x%s%s%s%s%s%s%s%s\n",
  84. chgstatus,
  85. (chgstatus & TPS_CHG_USB) ? " USB" : "",
  86. (chgstatus & TPS_CHG_AC) ? " AC" : "",
  87. (chgstatus & TPS_CHG_THERM) ? " therm" : "",
  88. (chgstatus & TPS_CHG_TERM) ? " done" :
  89. ((chgstatus & (TPS_CHG_USB|TPS_CHG_AC))
  90. ? " (charging)" : ""),
  91. (chgstatus & TPS_CHG_TAPER_TMO) ? " taper_tmo" : "",
  92. (chgstatus & TPS_CHG_CHG_TMO) ? " charge_tmo" : "",
  93. (chgstatus & TPS_CHG_PRECHG_TMO) ? " prechg_tmo" : "",
  94. (chgstatus & TPS_CHG_TEMP_ERR) ? " temp_err" : "");
  95. }
  96. static void dbg_regstat(char *buf, size_t len, u8 regstatus)
  97. {
  98. snprintf(buf, len, "%02x %s%s%s%s%s%s%s%s\n",
  99. regstatus,
  100. (regstatus & TPS_REG_ONOFF) ? "off" : "(on)",
  101. (regstatus & TPS_REG_COVER) ? " uncover" : "",
  102. (regstatus & TPS_REG_UVLO) ? " UVLO" : "",
  103. (regstatus & TPS_REG_NO_CHG) ? " NO_CHG" : "",
  104. (regstatus & TPS_REG_PG_LD02) ? " ld02_bad" : "",
  105. (regstatus & TPS_REG_PG_LD01) ? " ld01_bad" : "",
  106. (regstatus & TPS_REG_PG_MAIN) ? " main_bad" : "",
  107. (regstatus & TPS_REG_PG_CORE) ? " core_bad" : "");
  108. }
  109. static void dbg_chgconf(int por, char *buf, size_t len, u8 chgconfig)
  110. {
  111. const char *hibit;
  112. if (por)
  113. hibit = (chgconfig & TPS_CHARGE_POR)
  114. ? "POR=69ms" : "POR=1sec";
  115. else
  116. hibit = (chgconfig & TPS65013_AUA) ? "AUA" : "";
  117. snprintf(buf, len, "%02x %s%s%s AC=%d%% USB=%dmA %sCharge\n",
  118. chgconfig, hibit,
  119. (chgconfig & TPS_CHARGE_RESET) ? " reset" : "",
  120. (chgconfig & TPS_CHARGE_FAST) ? " fast" : "",
  121. ({int p; switch ((chgconfig >> 3) & 3) {
  122. case 3: p = 100; break;
  123. case 2: p = 75; break;
  124. case 1: p = 50; break;
  125. default: p = 25; break;
  126. }; p; }),
  127. (chgconfig & TPS_VBUS_CHARGING)
  128. ? ((chgconfig & TPS_VBUS_500MA) ? 500 : 100)
  129. : 0,
  130. (chgconfig & TPS_CHARGE_ENABLE) ? "" : "No");
  131. }
  132. #endif
  133. #ifdef DEBUG
  134. static void show_chgstatus(const char *label, u8 chgstatus)
  135. {
  136. char buf [100];
  137. dbg_chgstat(buf, sizeof buf, chgstatus);
  138. pr_debug("%s: %s %s", DRIVER_NAME, label, buf);
  139. }
  140. static void show_regstatus(const char *label, u8 regstatus)
  141. {
  142. char buf [100];
  143. dbg_regstat(buf, sizeof buf, regstatus);
  144. pr_debug("%s: %s %s", DRIVER_NAME, label, buf);
  145. }
  146. static void show_chgconfig(int por, const char *label, u8 chgconfig)
  147. {
  148. char buf [100];
  149. dbg_chgconf(por, buf, sizeof buf, chgconfig);
  150. pr_debug("%s: %s %s", DRIVER_NAME, label, buf);
  151. }
  152. #else
  153. static inline void show_chgstatus(const char *label, u8 chgstatus) { }
  154. static inline void show_regstatus(const char *label, u8 chgstatus) { }
  155. static inline void show_chgconfig(int por, const char *label, u8 chgconfig) { }
  156. #endif
  157. #ifdef CONFIG_DEBUG_FS
  158. static int dbg_show(struct seq_file *s, void *_)
  159. {
  160. struct tps65010 *tps = s->private;
  161. u8 value, v2;
  162. unsigned i;
  163. char buf[100];
  164. const char *chip;
  165. switch (tps->model) {
  166. case TPS65010: chip = "tps65010"; break;
  167. case TPS65011: chip = "tps65011"; break;
  168. case TPS65012: chip = "tps65012"; break;
  169. case TPS65013: chip = "tps65013"; break;
  170. default: chip = NULL; break;
  171. }
  172. seq_printf(s, "driver %s\nversion %s\nchip %s\n\n",
  173. DRIVER_NAME, DRIVER_VERSION, chip);
  174. mutex_lock(&tps->lock);
  175. /* FIXME how can we tell whether a battery is present?
  176. * likely involves a charge gauging chip (like BQ26501).
  177. */
  178. seq_printf(s, "%scharging\n\n", tps->charging ? "" : "(not) ");
  179. /* registers for monitoring battery charging and status; note
  180. * that reading chgstat and regstat may ack IRQs...
  181. */
  182. value = i2c_smbus_read_byte_data(tps->client, TPS_CHGCONFIG);
  183. dbg_chgconf(tps->por, buf, sizeof buf, value);
  184. seq_printf(s, "chgconfig %s", buf);
  185. value = i2c_smbus_read_byte_data(tps->client, TPS_CHGSTATUS);
  186. dbg_chgstat(buf, sizeof buf, value);
  187. seq_printf(s, "chgstat %s", buf);
  188. value = i2c_smbus_read_byte_data(tps->client, TPS_MASK1);
  189. dbg_chgstat(buf, sizeof buf, value);
  190. seq_printf(s, "mask1 %s", buf);
  191. /* ignore ackint1 */
  192. value = i2c_smbus_read_byte_data(tps->client, TPS_REGSTATUS);
  193. dbg_regstat(buf, sizeof buf, value);
  194. seq_printf(s, "regstat %s", buf);
  195. value = i2c_smbus_read_byte_data(tps->client, TPS_MASK2);
  196. dbg_regstat(buf, sizeof buf, value);
  197. seq_printf(s, "mask2 %s\n", buf);
  198. /* ignore ackint2 */
  199. (void) schedule_delayed_work(&tps->work, POWER_POLL_DELAY);
  200. /* VMAIN voltage, enable lowpower, etc */
  201. value = i2c_smbus_read_byte_data(tps->client, TPS_VDCDC1);
  202. seq_printf(s, "vdcdc1 %02x\n", value);
  203. /* VCORE voltage, vibrator on/off */
  204. value = i2c_smbus_read_byte_data(tps->client, TPS_VDCDC2);
  205. seq_printf(s, "vdcdc2 %02x\n", value);
  206. /* both LD0s, and their lowpower behavior */
  207. value = i2c_smbus_read_byte_data(tps->client, TPS_VREGS1);
  208. seq_printf(s, "vregs1 %02x\n\n", value);
  209. /* LEDs and GPIOs */
  210. value = i2c_smbus_read_byte_data(tps->client, TPS_LED1_ON);
  211. v2 = i2c_smbus_read_byte_data(tps->client, TPS_LED1_PER);
  212. seq_printf(s, "led1 %s, on=%02x, per=%02x, %d/%d msec\n",
  213. (value & 0x80)
  214. ? ((v2 & 0x80) ? "on" : "off")
  215. : ((v2 & 0x80) ? "blink" : "(nPG)"),
  216. value, v2,
  217. (value & 0x7f) * 10, (v2 & 0x7f) * 100);
  218. value = i2c_smbus_read_byte_data(tps->client, TPS_LED2_ON);
  219. v2 = i2c_smbus_read_byte_data(tps->client, TPS_LED2_PER);
  220. seq_printf(s, "led2 %s, on=%02x, per=%02x, %d/%d msec\n",
  221. (value & 0x80)
  222. ? ((v2 & 0x80) ? "on" : "off")
  223. : ((v2 & 0x80) ? "blink" : "off"),
  224. value, v2,
  225. (value & 0x7f) * 10, (v2 & 0x7f) * 100);
  226. value = i2c_smbus_read_byte_data(tps->client, TPS_DEFGPIO);
  227. v2 = i2c_smbus_read_byte_data(tps->client, TPS_MASK3);
  228. seq_printf(s, "defgpio %02x mask3 %02x\n", value, v2);
  229. for (i = 0; i < 4; i++) {
  230. if (value & (1 << (4 + i)))
  231. seq_printf(s, " gpio%d-out %s\n", i + 1,
  232. (value & (1 << i)) ? "low" : "hi ");
  233. else
  234. seq_printf(s, " gpio%d-in %s %s %s\n", i + 1,
  235. (value & (1 << i)) ? "hi " : "low",
  236. (v2 & (1 << i)) ? "no-irq" : "irq",
  237. (v2 & (1 << (4 + i))) ? "rising" : "falling");
  238. }
  239. mutex_unlock(&tps->lock);
  240. return 0;
  241. }
  242. static int dbg_tps_open(struct inode *inode, struct file *file)
  243. {
  244. return single_open(file, dbg_show, inode->i_private);
  245. }
  246. static const struct file_operations debug_fops = {
  247. .open = dbg_tps_open,
  248. .read = seq_read,
  249. .llseek = seq_lseek,
  250. .release = single_release,
  251. };
  252. #define DEBUG_FOPS &debug_fops
  253. #else
  254. #define DEBUG_FOPS NULL
  255. #endif
  256. /*-------------------------------------------------------------------------*/
  257. /* handle IRQS in a task context, so we can use I2C calls */
  258. static void tps65010_interrupt(struct tps65010 *tps)
  259. {
  260. u8 tmp = 0, mask, poll;
  261. /* IRQs won't trigger for certain events, but we can get
  262. * others by polling (normally, with external power applied).
  263. */
  264. poll = 0;
  265. /* regstatus irqs */
  266. if (tps->nmask2) {
  267. tmp = i2c_smbus_read_byte_data(tps->client, TPS_REGSTATUS);
  268. mask = tmp ^ tps->regstatus;
  269. tps->regstatus = tmp;
  270. mask &= tps->nmask2;
  271. } else
  272. mask = 0;
  273. if (mask) {
  274. tps->regstatus = tmp;
  275. /* may need to shut something down ... */
  276. /* "off" usually means deep sleep */
  277. if (tmp & TPS_REG_ONOFF) {
  278. pr_info("%s: power off button\n", DRIVER_NAME);
  279. #if 0
  280. /* REVISIT: this might need its own workqueue
  281. * plus tweaks including deadlock avoidance ...
  282. * also needs to get error handling and probably
  283. * an #ifdef CONFIG_HIBERNATION
  284. */
  285. hibernate();
  286. #endif
  287. poll = 1;
  288. }
  289. }
  290. /* chgstatus irqs */
  291. if (tps->nmask1) {
  292. tmp = i2c_smbus_read_byte_data(tps->client, TPS_CHGSTATUS);
  293. mask = tmp ^ tps->chgstatus;
  294. tps->chgstatus = tmp;
  295. mask &= tps->nmask1;
  296. } else
  297. mask = 0;
  298. if (mask) {
  299. unsigned charging = 0;
  300. show_chgstatus("chg/irq", tmp);
  301. if (tmp & (TPS_CHG_USB|TPS_CHG_AC))
  302. show_chgconfig(tps->por, "conf", tps->chgconf);
  303. /* Unless it was turned off or disabled, we charge any
  304. * battery whenever there's power available for it
  305. * and the charger hasn't been disabled.
  306. */
  307. if (!(tps->chgstatus & ~(TPS_CHG_USB|TPS_CHG_AC))
  308. && (tps->chgstatus & (TPS_CHG_USB|TPS_CHG_AC))
  309. && (tps->chgconf & TPS_CHARGE_ENABLE)
  310. ) {
  311. if (tps->chgstatus & TPS_CHG_USB) {
  312. /* VBUS options are readonly until reconnect */
  313. if (mask & TPS_CHG_USB)
  314. set_bit(FLAG_VBUS_CHANGED, &tps->flags);
  315. charging = 1;
  316. } else if (tps->chgstatus & TPS_CHG_AC)
  317. charging = 1;
  318. }
  319. if (charging != tps->charging) {
  320. tps->charging = charging;
  321. pr_info("%s: battery %scharging\n",
  322. DRIVER_NAME, charging ? "" :
  323. ((tps->chgstatus & (TPS_CHG_USB|TPS_CHG_AC))
  324. ? "NOT " : "dis"));
  325. }
  326. }
  327. /* always poll to detect (a) power removal, without tps65013
  328. * NO_CHG IRQ; or (b) restart of charging after stop.
  329. */
  330. if ((tps->model != TPS65013 || !tps->charging)
  331. && (tps->chgstatus & (TPS_CHG_USB|TPS_CHG_AC)))
  332. poll = 1;
  333. if (poll)
  334. (void) schedule_delayed_work(&tps->work, POWER_POLL_DELAY);
  335. /* also potentially gpio-in rise or fall */
  336. }
  337. /* handle IRQs and polling using keventd for now */
  338. static void tps65010_work(struct work_struct *work)
  339. {
  340. struct tps65010 *tps;
  341. tps = container_of(work, struct tps65010, work.work);
  342. mutex_lock(&tps->lock);
  343. tps65010_interrupt(tps);
  344. if (test_and_clear_bit(FLAG_VBUS_CHANGED, &tps->flags)) {
  345. int status;
  346. u8 chgconfig, tmp;
  347. chgconfig = i2c_smbus_read_byte_data(tps->client,
  348. TPS_CHGCONFIG);
  349. chgconfig &= ~(TPS_VBUS_500MA | TPS_VBUS_CHARGING);
  350. if (tps->vbus == 500)
  351. chgconfig |= TPS_VBUS_500MA | TPS_VBUS_CHARGING;
  352. else if (tps->vbus >= 100)
  353. chgconfig |= TPS_VBUS_CHARGING;
  354. status = i2c_smbus_write_byte_data(tps->client,
  355. TPS_CHGCONFIG, chgconfig);
  356. /* vbus update fails unless VBUS is connected! */
  357. tmp = i2c_smbus_read_byte_data(tps->client, TPS_CHGCONFIG);
  358. tps->chgconf = tmp;
  359. show_chgconfig(tps->por, "update vbus", tmp);
  360. }
  361. if (test_and_clear_bit(FLAG_IRQ_ENABLE, &tps->flags))
  362. enable_irq(tps->client->irq);
  363. mutex_unlock(&tps->lock);
  364. }
  365. static irqreturn_t tps65010_irq(int irq, void *_tps)
  366. {
  367. struct tps65010 *tps = _tps;
  368. disable_irq_nosync(irq);
  369. set_bit(FLAG_IRQ_ENABLE, &tps->flags);
  370. (void) schedule_work(&tps->work.work);
  371. return IRQ_HANDLED;
  372. }
  373. /*-------------------------------------------------------------------------*/
  374. static struct tps65010 *the_tps;
  375. static int __exit tps65010_remove(struct i2c_client *client)
  376. {
  377. struct tps65010 *tps = i2c_get_clientdata(client);
  378. if (client->irq > 0)
  379. free_irq(client->irq, tps);
  380. cancel_delayed_work(&tps->work);
  381. flush_scheduled_work();
  382. debugfs_remove(tps->file);
  383. kfree(tps);
  384. the_tps = NULL;
  385. return 0;
  386. }
  387. static int tps65010_probe(struct i2c_client *client)
  388. {
  389. struct tps65010 *tps;
  390. int status;
  391. if (the_tps) {
  392. dev_dbg(&client->dev, "only one tps6501x chip allowed\n");
  393. return -ENODEV;
  394. }
  395. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  396. return -EINVAL;
  397. tps = kzalloc(sizeof *tps, GFP_KERNEL);
  398. if (!tps)
  399. return -ENOMEM;
  400. mutex_init(&tps->lock);
  401. INIT_DELAYED_WORK(&tps->work, tps65010_work);
  402. tps->client = client;
  403. if (strcmp(client->name, "tps65010") == 0)
  404. tps->model = TPS65010;
  405. else if (strcmp(client->name, "tps65011") == 0)
  406. tps->model = TPS65011;
  407. else if (strcmp(client->name, "tps65012") == 0)
  408. tps->model = TPS65012;
  409. else if (strcmp(client->name, "tps65013") == 0)
  410. tps->model = TPS65013;
  411. else {
  412. dev_warn(&client->dev, "unknown chip '%s'\n", client->name);
  413. status = -ENODEV;
  414. goto fail1;
  415. }
  416. /* the IRQ is active low, but many gpio lines can't support that
  417. * so this driver uses falling-edge triggers instead.
  418. */
  419. if (client->irq > 0) {
  420. status = request_irq(client->irq, tps65010_irq,
  421. IRQF_SAMPLE_RANDOM | IRQF_TRIGGER_FALLING,
  422. DRIVER_NAME, tps);
  423. if (status < 0) {
  424. dev_dbg(&client->dev, "can't get IRQ %d, err %d\n",
  425. client->irq, status);
  426. goto fail1;
  427. }
  428. /* annoying race here, ideally we'd have an option
  429. * to claim the irq now and enable it later.
  430. * FIXME genirq IRQF_NOAUTOEN now solves that ...
  431. */
  432. disable_irq(client->irq);
  433. set_bit(FLAG_IRQ_ENABLE, &tps->flags);
  434. } else
  435. dev_warn(&client->dev, "IRQ not configured!\n");
  436. switch (tps->model) {
  437. case TPS65010:
  438. case TPS65012:
  439. tps->por = 1;
  440. break;
  441. case TPS_UNKNOWN:
  442. printk(KERN_WARNING "%s: unknown TPS chip\n", DRIVER_NAME);
  443. break;
  444. /* else CHGCONFIG.POR is replaced by AUA, enabling a WAIT mode */
  445. }
  446. tps->chgconf = i2c_smbus_read_byte_data(client, TPS_CHGCONFIG);
  447. show_chgconfig(tps->por, "conf/init", tps->chgconf);
  448. show_chgstatus("chg/init",
  449. i2c_smbus_read_byte_data(client, TPS_CHGSTATUS));
  450. show_regstatus("reg/init",
  451. i2c_smbus_read_byte_data(client, TPS_REGSTATUS));
  452. pr_debug("%s: vdcdc1 0x%02x, vdcdc2 %02x, vregs1 %02x\n", DRIVER_NAME,
  453. i2c_smbus_read_byte_data(client, TPS_VDCDC1),
  454. i2c_smbus_read_byte_data(client, TPS_VDCDC2),
  455. i2c_smbus_read_byte_data(client, TPS_VREGS1));
  456. pr_debug("%s: defgpio 0x%02x, mask3 0x%02x\n", DRIVER_NAME,
  457. i2c_smbus_read_byte_data(client, TPS_DEFGPIO),
  458. i2c_smbus_read_byte_data(client, TPS_MASK3));
  459. the_tps = tps;
  460. #if defined(CONFIG_USB_GADGET) && !defined(CONFIG_USB_OTG)
  461. /* USB hosts can't draw VBUS. OTG devices could, later
  462. * when OTG infrastructure enables it. USB peripherals
  463. * could be relying on VBUS while booting, though.
  464. */
  465. tps->vbus = 100;
  466. #endif
  467. /* unmask the "interesting" irqs, then poll once to
  468. * kickstart monitoring, initialize shadowed status
  469. * registers, and maybe disable VBUS draw.
  470. */
  471. tps->nmask1 = ~0;
  472. (void) i2c_smbus_write_byte_data(client, TPS_MASK1, ~tps->nmask1);
  473. tps->nmask2 = TPS_REG_ONOFF;
  474. if (tps->model == TPS65013)
  475. tps->nmask2 |= TPS_REG_NO_CHG;
  476. (void) i2c_smbus_write_byte_data(client, TPS_MASK2, ~tps->nmask2);
  477. (void) i2c_smbus_write_byte_data(client, TPS_MASK3, 0x0f
  478. | i2c_smbus_read_byte_data(client, TPS_MASK3));
  479. tps65010_work(&tps->work.work);
  480. tps->file = debugfs_create_file(DRIVER_NAME, S_IRUGO, NULL,
  481. tps, DEBUG_FOPS);
  482. return 0;
  483. fail1:
  484. kfree(tps);
  485. return status;
  486. }
  487. static struct i2c_driver tps65010_driver = {
  488. .driver = {
  489. .name = "tps65010",
  490. },
  491. .probe = tps65010_probe,
  492. .remove = __exit_p(tps65010_remove),
  493. };
  494. /*-------------------------------------------------------------------------*/
  495. /* Draw from VBUS:
  496. * 0 mA -- DON'T DRAW (might supply power instead)
  497. * 100 mA -- usb unit load (slowest charge rate)
  498. * 500 mA -- usb high power (fast battery charge)
  499. */
  500. int tps65010_set_vbus_draw(unsigned mA)
  501. {
  502. unsigned long flags;
  503. if (!the_tps)
  504. return -ENODEV;
  505. /* assumes non-SMP */
  506. local_irq_save(flags);
  507. if (mA >= 500)
  508. mA = 500;
  509. else if (mA >= 100)
  510. mA = 100;
  511. else
  512. mA = 0;
  513. the_tps->vbus = mA;
  514. if ((the_tps->chgstatus & TPS_CHG_USB)
  515. && test_and_set_bit(
  516. FLAG_VBUS_CHANGED, &the_tps->flags)) {
  517. /* gadget drivers call this in_irq() */
  518. (void) schedule_work(&the_tps->work.work);
  519. }
  520. local_irq_restore(flags);
  521. return 0;
  522. }
  523. EXPORT_SYMBOL(tps65010_set_vbus_draw);
  524. /*-------------------------------------------------------------------------*/
  525. /* tps65010_set_gpio_out_value parameter:
  526. * gpio: GPIO1, GPIO2, GPIO3 or GPIO4
  527. * value: LOW or HIGH
  528. */
  529. int tps65010_set_gpio_out_value(unsigned gpio, unsigned value)
  530. {
  531. int status;
  532. unsigned defgpio;
  533. if (!the_tps)
  534. return -ENODEV;
  535. if ((gpio < GPIO1) || (gpio > GPIO4))
  536. return -EINVAL;
  537. mutex_lock(&the_tps->lock);
  538. defgpio = i2c_smbus_read_byte_data(the_tps->client, TPS_DEFGPIO);
  539. /* Configure GPIO for output */
  540. defgpio |= 1 << (gpio + 3);
  541. /* Writing 1 forces a logic 0 on that GPIO and vice versa */
  542. switch (value) {
  543. case LOW:
  544. defgpio |= 1 << (gpio - 1); /* set GPIO low by writing 1 */
  545. break;
  546. /* case HIGH: */
  547. default:
  548. defgpio &= ~(1 << (gpio - 1)); /* set GPIO high by writing 0 */
  549. break;
  550. }
  551. status = i2c_smbus_write_byte_data(the_tps->client,
  552. TPS_DEFGPIO, defgpio);
  553. pr_debug("%s: gpio%dout = %s, defgpio 0x%02x\n", DRIVER_NAME,
  554. gpio, value ? "high" : "low",
  555. i2c_smbus_read_byte_data(the_tps->client, TPS_DEFGPIO));
  556. mutex_unlock(&the_tps->lock);
  557. return status;
  558. }
  559. EXPORT_SYMBOL(tps65010_set_gpio_out_value);
  560. /*-------------------------------------------------------------------------*/
  561. /* tps65010_set_led parameter:
  562. * led: LED1 or LED2
  563. * mode: ON, OFF or BLINK
  564. */
  565. int tps65010_set_led(unsigned led, unsigned mode)
  566. {
  567. int status;
  568. unsigned led_on, led_per, offs;
  569. if (!the_tps)
  570. return -ENODEV;
  571. if (led == LED1)
  572. offs = 0;
  573. else {
  574. offs = 2;
  575. led = LED2;
  576. }
  577. mutex_lock(&the_tps->lock);
  578. pr_debug("%s: led%i_on 0x%02x\n", DRIVER_NAME, led,
  579. i2c_smbus_read_byte_data(the_tps->client,
  580. TPS_LED1_ON + offs));
  581. pr_debug("%s: led%i_per 0x%02x\n", DRIVER_NAME, led,
  582. i2c_smbus_read_byte_data(the_tps->client,
  583. TPS_LED1_PER + offs));
  584. switch (mode) {
  585. case OFF:
  586. led_on = 1 << 7;
  587. led_per = 0 << 7;
  588. break;
  589. case ON:
  590. led_on = 1 << 7;
  591. led_per = 1 << 7;
  592. break;
  593. case BLINK:
  594. led_on = 0x30 | (0 << 7);
  595. led_per = 0x08 | (1 << 7);
  596. break;
  597. default:
  598. printk(KERN_ERR "%s: Wrong mode parameter for set_led()\n",
  599. DRIVER_NAME);
  600. mutex_unlock(&the_tps->lock);
  601. return -EINVAL;
  602. }
  603. status = i2c_smbus_write_byte_data(the_tps->client,
  604. TPS_LED1_ON + offs, led_on);
  605. if (status != 0) {
  606. printk(KERN_ERR "%s: Failed to write led%i_on register\n",
  607. DRIVER_NAME, led);
  608. mutex_unlock(&the_tps->lock);
  609. return status;
  610. }
  611. pr_debug("%s: led%i_on 0x%02x\n", DRIVER_NAME, led,
  612. i2c_smbus_read_byte_data(the_tps->client, TPS_LED1_ON + offs));
  613. status = i2c_smbus_write_byte_data(the_tps->client,
  614. TPS_LED1_PER + offs, led_per);
  615. if (status != 0) {
  616. printk(KERN_ERR "%s: Failed to write led%i_per register\n",
  617. DRIVER_NAME, led);
  618. mutex_unlock(&the_tps->lock);
  619. return status;
  620. }
  621. pr_debug("%s: led%i_per 0x%02x\n", DRIVER_NAME, led,
  622. i2c_smbus_read_byte_data(the_tps->client,
  623. TPS_LED1_PER + offs));
  624. mutex_unlock(&the_tps->lock);
  625. return status;
  626. }
  627. EXPORT_SYMBOL(tps65010_set_led);
  628. /*-------------------------------------------------------------------------*/
  629. /* tps65010_set_vib parameter:
  630. * value: ON or OFF
  631. */
  632. int tps65010_set_vib(unsigned value)
  633. {
  634. int status;
  635. unsigned vdcdc2;
  636. if (!the_tps)
  637. return -ENODEV;
  638. mutex_lock(&the_tps->lock);
  639. vdcdc2 = i2c_smbus_read_byte_data(the_tps->client, TPS_VDCDC2);
  640. vdcdc2 &= ~(1 << 1);
  641. if (value)
  642. vdcdc2 |= (1 << 1);
  643. status = i2c_smbus_write_byte_data(the_tps->client,
  644. TPS_VDCDC2, vdcdc2);
  645. pr_debug("%s: vibrator %s\n", DRIVER_NAME, value ? "on" : "off");
  646. mutex_unlock(&the_tps->lock);
  647. return status;
  648. }
  649. EXPORT_SYMBOL(tps65010_set_vib);
  650. /*-------------------------------------------------------------------------*/
  651. /* tps65010_set_low_pwr parameter:
  652. * mode: ON or OFF
  653. */
  654. int tps65010_set_low_pwr(unsigned mode)
  655. {
  656. int status;
  657. unsigned vdcdc1;
  658. if (!the_tps)
  659. return -ENODEV;
  660. mutex_lock(&the_tps->lock);
  661. pr_debug("%s: %s low_pwr, vdcdc1 0x%02x\n", DRIVER_NAME,
  662. mode ? "enable" : "disable",
  663. i2c_smbus_read_byte_data(the_tps->client, TPS_VDCDC1));
  664. vdcdc1 = i2c_smbus_read_byte_data(the_tps->client, TPS_VDCDC1);
  665. switch (mode) {
  666. case OFF:
  667. vdcdc1 &= ~TPS_ENABLE_LP; /* disable ENABLE_LP bit */
  668. break;
  669. /* case ON: */
  670. default:
  671. vdcdc1 |= TPS_ENABLE_LP; /* enable ENABLE_LP bit */
  672. break;
  673. }
  674. status = i2c_smbus_write_byte_data(the_tps->client,
  675. TPS_VDCDC1, vdcdc1);
  676. if (status != 0)
  677. printk(KERN_ERR "%s: Failed to write vdcdc1 register\n",
  678. DRIVER_NAME);
  679. else
  680. pr_debug("%s: vdcdc1 0x%02x\n", DRIVER_NAME,
  681. i2c_smbus_read_byte_data(the_tps->client, TPS_VDCDC1));
  682. mutex_unlock(&the_tps->lock);
  683. return status;
  684. }
  685. EXPORT_SYMBOL(tps65010_set_low_pwr);
  686. /*-------------------------------------------------------------------------*/
  687. /* tps65010_config_vregs1 parameter:
  688. * value to be written to VREGS1 register
  689. * Note: The complete register is written, set all bits you need
  690. */
  691. int tps65010_config_vregs1(unsigned value)
  692. {
  693. int status;
  694. if (!the_tps)
  695. return -ENODEV;
  696. mutex_lock(&the_tps->lock);
  697. pr_debug("%s: vregs1 0x%02x\n", DRIVER_NAME,
  698. i2c_smbus_read_byte_data(the_tps->client, TPS_VREGS1));
  699. status = i2c_smbus_write_byte_data(the_tps->client,
  700. TPS_VREGS1, value);
  701. if (status != 0)
  702. printk(KERN_ERR "%s: Failed to write vregs1 register\n",
  703. DRIVER_NAME);
  704. else
  705. pr_debug("%s: vregs1 0x%02x\n", DRIVER_NAME,
  706. i2c_smbus_read_byte_data(the_tps->client, TPS_VREGS1));
  707. mutex_unlock(&the_tps->lock);
  708. return status;
  709. }
  710. EXPORT_SYMBOL(tps65010_config_vregs1);
  711. /*-------------------------------------------------------------------------*/
  712. /* tps65013_set_low_pwr parameter:
  713. * mode: ON or OFF
  714. */
  715. /* FIXME: Assumes AC or USB power is present. Setting AUA bit is not
  716. required if power supply is through a battery */
  717. int tps65013_set_low_pwr(unsigned mode)
  718. {
  719. int status;
  720. unsigned vdcdc1, chgconfig;
  721. if (!the_tps || the_tps->por)
  722. return -ENODEV;
  723. mutex_lock(&the_tps->lock);
  724. pr_debug("%s: %s low_pwr, chgconfig 0x%02x vdcdc1 0x%02x\n",
  725. DRIVER_NAME,
  726. mode ? "enable" : "disable",
  727. i2c_smbus_read_byte_data(the_tps->client, TPS_CHGCONFIG),
  728. i2c_smbus_read_byte_data(the_tps->client, TPS_VDCDC1));
  729. chgconfig = i2c_smbus_read_byte_data(the_tps->client, TPS_CHGCONFIG);
  730. vdcdc1 = i2c_smbus_read_byte_data(the_tps->client, TPS_VDCDC1);
  731. switch (mode) {
  732. case OFF:
  733. chgconfig &= ~TPS65013_AUA; /* disable AUA bit */
  734. vdcdc1 &= ~TPS_ENABLE_LP; /* disable ENABLE_LP bit */
  735. break;
  736. /* case ON: */
  737. default:
  738. chgconfig |= TPS65013_AUA; /* enable AUA bit */
  739. vdcdc1 |= TPS_ENABLE_LP; /* enable ENABLE_LP bit */
  740. break;
  741. }
  742. status = i2c_smbus_write_byte_data(the_tps->client,
  743. TPS_CHGCONFIG, chgconfig);
  744. if (status != 0) {
  745. printk(KERN_ERR "%s: Failed to write chconfig register\n",
  746. DRIVER_NAME);
  747. mutex_unlock(&the_tps->lock);
  748. return status;
  749. }
  750. chgconfig = i2c_smbus_read_byte_data(the_tps->client, TPS_CHGCONFIG);
  751. the_tps->chgconf = chgconfig;
  752. show_chgconfig(0, "chgconf", chgconfig);
  753. status = i2c_smbus_write_byte_data(the_tps->client,
  754. TPS_VDCDC1, vdcdc1);
  755. if (status != 0)
  756. printk(KERN_ERR "%s: Failed to write vdcdc1 register\n",
  757. DRIVER_NAME);
  758. else
  759. pr_debug("%s: vdcdc1 0x%02x\n", DRIVER_NAME,
  760. i2c_smbus_read_byte_data(the_tps->client, TPS_VDCDC1));
  761. mutex_unlock(&the_tps->lock);
  762. return status;
  763. }
  764. EXPORT_SYMBOL(tps65013_set_low_pwr);
  765. /*-------------------------------------------------------------------------*/
  766. static int __init tps_init(void)
  767. {
  768. u32 tries = 3;
  769. int status = -ENODEV;
  770. printk(KERN_INFO "%s: version %s\n", DRIVER_NAME, DRIVER_VERSION);
  771. /* some boards have startup glitches */
  772. while (tries--) {
  773. status = i2c_add_driver(&tps65010_driver);
  774. if (the_tps)
  775. break;
  776. i2c_del_driver(&tps65010_driver);
  777. if (!tries) {
  778. printk(KERN_ERR "%s: no chip?\n", DRIVER_NAME);
  779. return -ENODEV;
  780. }
  781. pr_debug("%s: re-probe ...\n", DRIVER_NAME);
  782. msleep(10);
  783. }
  784. return status;
  785. }
  786. /* NOTE: this MUST be initialized before the other parts of the system
  787. * that rely on it ... but after the i2c bus on which this relies.
  788. * That is, much earlier than on PC-type systems, which don't often use
  789. * I2C as a core system bus.
  790. */
  791. subsys_initcall(tps_init);
  792. static void __exit tps_exit(void)
  793. {
  794. i2c_del_driver(&tps65010_driver);
  795. }
  796. module_exit(tps_exit);