tps65010.c 28 KB

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