tps65010.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099
  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. the_tps = NULL;
  443. return 0;
  444. }
  445. static int tps65010_probe(struct i2c_client *client,
  446. const struct i2c_device_id *id)
  447. {
  448. struct tps65010 *tps;
  449. int status;
  450. struct tps65010_board *board = client->dev.platform_data;
  451. if (the_tps) {
  452. dev_dbg(&client->dev, "only one tps6501x chip allowed\n");
  453. return -ENODEV;
  454. }
  455. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  456. return -EINVAL;
  457. tps = kzalloc(sizeof *tps, GFP_KERNEL);
  458. if (!tps)
  459. return -ENOMEM;
  460. mutex_init(&tps->lock);
  461. INIT_DELAYED_WORK(&tps->work, tps65010_work);
  462. tps->client = client;
  463. tps->model = id->driver_data;
  464. /* the IRQ is active low, but many gpio lines can't support that
  465. * so this driver uses falling-edge triggers instead.
  466. */
  467. if (client->irq > 0) {
  468. status = request_irq(client->irq, tps65010_irq,
  469. IRQF_SAMPLE_RANDOM | IRQF_TRIGGER_FALLING,
  470. DRIVER_NAME, tps);
  471. if (status < 0) {
  472. dev_dbg(&client->dev, "can't get IRQ %d, err %d\n",
  473. client->irq, status);
  474. goto fail1;
  475. }
  476. /* annoying race here, ideally we'd have an option
  477. * to claim the irq now and enable it later.
  478. * FIXME genirq IRQF_NOAUTOEN now solves that ...
  479. */
  480. disable_irq(client->irq);
  481. set_bit(FLAG_IRQ_ENABLE, &tps->flags);
  482. } else
  483. dev_warn(&client->dev, "IRQ not configured!\n");
  484. switch (tps->model) {
  485. case TPS65010:
  486. case TPS65012:
  487. tps->por = 1;
  488. break;
  489. /* else CHGCONFIG.POR is replaced by AUA, enabling a WAIT mode */
  490. }
  491. tps->chgconf = i2c_smbus_read_byte_data(client, TPS_CHGCONFIG);
  492. show_chgconfig(tps->por, "conf/init", tps->chgconf);
  493. show_chgstatus("chg/init",
  494. i2c_smbus_read_byte_data(client, TPS_CHGSTATUS));
  495. show_regstatus("reg/init",
  496. i2c_smbus_read_byte_data(client, TPS_REGSTATUS));
  497. pr_debug("%s: vdcdc1 0x%02x, vdcdc2 %02x, vregs1 %02x\n", DRIVER_NAME,
  498. i2c_smbus_read_byte_data(client, TPS_VDCDC1),
  499. i2c_smbus_read_byte_data(client, TPS_VDCDC2),
  500. i2c_smbus_read_byte_data(client, TPS_VREGS1));
  501. pr_debug("%s: defgpio 0x%02x, mask3 0x%02x\n", DRIVER_NAME,
  502. i2c_smbus_read_byte_data(client, TPS_DEFGPIO),
  503. i2c_smbus_read_byte_data(client, TPS_MASK3));
  504. i2c_set_clientdata(client, tps);
  505. the_tps = tps;
  506. #if defined(CONFIG_USB_GADGET) && !defined(CONFIG_USB_OTG)
  507. /* USB hosts can't draw VBUS. OTG devices could, later
  508. * when OTG infrastructure enables it. USB peripherals
  509. * could be relying on VBUS while booting, though.
  510. */
  511. tps->vbus = 100;
  512. #endif
  513. /* unmask the "interesting" irqs, then poll once to
  514. * kickstart monitoring, initialize shadowed status
  515. * registers, and maybe disable VBUS draw.
  516. */
  517. tps->nmask1 = ~0;
  518. (void) i2c_smbus_write_byte_data(client, TPS_MASK1, ~tps->nmask1);
  519. tps->nmask2 = TPS_REG_ONOFF;
  520. if (tps->model == TPS65013)
  521. tps->nmask2 |= TPS_REG_NO_CHG;
  522. (void) i2c_smbus_write_byte_data(client, TPS_MASK2, ~tps->nmask2);
  523. (void) i2c_smbus_write_byte_data(client, TPS_MASK3, 0x0f
  524. | i2c_smbus_read_byte_data(client, TPS_MASK3));
  525. tps65010_work(&tps->work.work);
  526. tps->file = debugfs_create_file(DRIVER_NAME, S_IRUGO, NULL,
  527. tps, DEBUG_FOPS);
  528. /* optionally register GPIOs */
  529. if (board && board->base != 0) {
  530. tps->outmask = board->outmask;
  531. tps->chip.label = client->name;
  532. tps->chip.dev = &client->dev;
  533. tps->chip.owner = THIS_MODULE;
  534. tps->chip.set = tps65010_gpio_set;
  535. tps->chip.direction_output = tps65010_output;
  536. /* NOTE: only partial support for inputs; nyet IRQs */
  537. tps->chip.get = tps65010_gpio_get;
  538. tps->chip.base = board->base;
  539. tps->chip.ngpio = 7;
  540. tps->chip.can_sleep = 1;
  541. status = gpiochip_add(&tps->chip);
  542. if (status < 0)
  543. dev_err(&client->dev, "can't add gpiochip, err %d\n",
  544. status);
  545. else if (board->setup) {
  546. status = board->setup(client, board->context);
  547. if (status < 0) {
  548. dev_dbg(&client->dev,
  549. "board %s %s err %d\n",
  550. "setup", client->name, status);
  551. status = 0;
  552. }
  553. }
  554. }
  555. return 0;
  556. fail1:
  557. kfree(tps);
  558. return status;
  559. }
  560. static const struct i2c_device_id tps65010_id[] = {
  561. { "tps65010", TPS65010 },
  562. { "tps65011", TPS65011 },
  563. { "tps65012", TPS65012 },
  564. { "tps65013", TPS65013 },
  565. { "tps65014", TPS65011 }, /* tps65011 charging at 6.5V max */
  566. { }
  567. };
  568. MODULE_DEVICE_TABLE(i2c, tps65010_id);
  569. static struct i2c_driver tps65010_driver = {
  570. .driver = {
  571. .name = "tps65010",
  572. },
  573. .probe = tps65010_probe,
  574. .remove = __exit_p(tps65010_remove),
  575. .id_table = tps65010_id,
  576. };
  577. /*-------------------------------------------------------------------------*/
  578. /* Draw from VBUS:
  579. * 0 mA -- DON'T DRAW (might supply power instead)
  580. * 100 mA -- usb unit load (slowest charge rate)
  581. * 500 mA -- usb high power (fast battery charge)
  582. */
  583. int tps65010_set_vbus_draw(unsigned mA)
  584. {
  585. unsigned long flags;
  586. if (!the_tps)
  587. return -ENODEV;
  588. /* assumes non-SMP */
  589. local_irq_save(flags);
  590. if (mA >= 500)
  591. mA = 500;
  592. else if (mA >= 100)
  593. mA = 100;
  594. else
  595. mA = 0;
  596. the_tps->vbus = mA;
  597. if ((the_tps->chgstatus & TPS_CHG_USB)
  598. && test_and_set_bit(
  599. FLAG_VBUS_CHANGED, &the_tps->flags)) {
  600. /* gadget drivers call this in_irq() */
  601. (void) schedule_work(&the_tps->work.work);
  602. }
  603. local_irq_restore(flags);
  604. return 0;
  605. }
  606. EXPORT_SYMBOL(tps65010_set_vbus_draw);
  607. /*-------------------------------------------------------------------------*/
  608. /* tps65010_set_gpio_out_value parameter:
  609. * gpio: GPIO1, GPIO2, GPIO3 or GPIO4
  610. * value: LOW or HIGH
  611. */
  612. int tps65010_set_gpio_out_value(unsigned gpio, unsigned value)
  613. {
  614. int status;
  615. unsigned defgpio;
  616. if (!the_tps)
  617. return -ENODEV;
  618. if ((gpio < GPIO1) || (gpio > GPIO4))
  619. return -EINVAL;
  620. mutex_lock(&the_tps->lock);
  621. defgpio = i2c_smbus_read_byte_data(the_tps->client, TPS_DEFGPIO);
  622. /* Configure GPIO for output */
  623. defgpio |= 1 << (gpio + 3);
  624. /* Writing 1 forces a logic 0 on that GPIO and vice versa */
  625. switch (value) {
  626. case LOW:
  627. defgpio |= 1 << (gpio - 1); /* set GPIO low by writing 1 */
  628. break;
  629. /* case HIGH: */
  630. default:
  631. defgpio &= ~(1 << (gpio - 1)); /* set GPIO high by writing 0 */
  632. break;
  633. }
  634. status = i2c_smbus_write_byte_data(the_tps->client,
  635. TPS_DEFGPIO, defgpio);
  636. pr_debug("%s: gpio%dout = %s, defgpio 0x%02x\n", DRIVER_NAME,
  637. gpio, value ? "high" : "low",
  638. i2c_smbus_read_byte_data(the_tps->client, TPS_DEFGPIO));
  639. mutex_unlock(&the_tps->lock);
  640. return status;
  641. }
  642. EXPORT_SYMBOL(tps65010_set_gpio_out_value);
  643. /*-------------------------------------------------------------------------*/
  644. /* tps65010_set_led parameter:
  645. * led: LED1 or LED2
  646. * mode: ON, OFF or BLINK
  647. */
  648. int tps65010_set_led(unsigned led, unsigned mode)
  649. {
  650. int status;
  651. unsigned led_on, led_per, offs;
  652. if (!the_tps)
  653. return -ENODEV;
  654. if (led == LED1)
  655. offs = 0;
  656. else {
  657. offs = 2;
  658. led = LED2;
  659. }
  660. mutex_lock(&the_tps->lock);
  661. pr_debug("%s: led%i_on 0x%02x\n", DRIVER_NAME, led,
  662. i2c_smbus_read_byte_data(the_tps->client,
  663. TPS_LED1_ON + offs));
  664. pr_debug("%s: led%i_per 0x%02x\n", DRIVER_NAME, led,
  665. i2c_smbus_read_byte_data(the_tps->client,
  666. TPS_LED1_PER + offs));
  667. switch (mode) {
  668. case OFF:
  669. led_on = 1 << 7;
  670. led_per = 0 << 7;
  671. break;
  672. case ON:
  673. led_on = 1 << 7;
  674. led_per = 1 << 7;
  675. break;
  676. case BLINK:
  677. led_on = 0x30 | (0 << 7);
  678. led_per = 0x08 | (1 << 7);
  679. break;
  680. default:
  681. printk(KERN_ERR "%s: Wrong mode parameter for set_led()\n",
  682. DRIVER_NAME);
  683. mutex_unlock(&the_tps->lock);
  684. return -EINVAL;
  685. }
  686. status = i2c_smbus_write_byte_data(the_tps->client,
  687. TPS_LED1_ON + offs, led_on);
  688. if (status != 0) {
  689. printk(KERN_ERR "%s: Failed to write led%i_on register\n",
  690. DRIVER_NAME, led);
  691. mutex_unlock(&the_tps->lock);
  692. return status;
  693. }
  694. pr_debug("%s: led%i_on 0x%02x\n", DRIVER_NAME, led,
  695. i2c_smbus_read_byte_data(the_tps->client, TPS_LED1_ON + offs));
  696. status = i2c_smbus_write_byte_data(the_tps->client,
  697. TPS_LED1_PER + offs, led_per);
  698. if (status != 0) {
  699. printk(KERN_ERR "%s: Failed to write led%i_per register\n",
  700. DRIVER_NAME, led);
  701. mutex_unlock(&the_tps->lock);
  702. return status;
  703. }
  704. pr_debug("%s: led%i_per 0x%02x\n", DRIVER_NAME, led,
  705. i2c_smbus_read_byte_data(the_tps->client,
  706. TPS_LED1_PER + offs));
  707. mutex_unlock(&the_tps->lock);
  708. return status;
  709. }
  710. EXPORT_SYMBOL(tps65010_set_led);
  711. /*-------------------------------------------------------------------------*/
  712. /* tps65010_set_vib parameter:
  713. * value: ON or OFF
  714. */
  715. int tps65010_set_vib(unsigned value)
  716. {
  717. int status;
  718. unsigned vdcdc2;
  719. if (!the_tps)
  720. return -ENODEV;
  721. mutex_lock(&the_tps->lock);
  722. vdcdc2 = i2c_smbus_read_byte_data(the_tps->client, TPS_VDCDC2);
  723. vdcdc2 &= ~(1 << 1);
  724. if (value)
  725. vdcdc2 |= (1 << 1);
  726. status = i2c_smbus_write_byte_data(the_tps->client,
  727. TPS_VDCDC2, vdcdc2);
  728. pr_debug("%s: vibrator %s\n", DRIVER_NAME, value ? "on" : "off");
  729. mutex_unlock(&the_tps->lock);
  730. return status;
  731. }
  732. EXPORT_SYMBOL(tps65010_set_vib);
  733. /*-------------------------------------------------------------------------*/
  734. /* tps65010_set_low_pwr parameter:
  735. * mode: ON or OFF
  736. */
  737. int tps65010_set_low_pwr(unsigned mode)
  738. {
  739. int status;
  740. unsigned vdcdc1;
  741. if (!the_tps)
  742. return -ENODEV;
  743. mutex_lock(&the_tps->lock);
  744. pr_debug("%s: %s low_pwr, vdcdc1 0x%02x\n", DRIVER_NAME,
  745. mode ? "enable" : "disable",
  746. i2c_smbus_read_byte_data(the_tps->client, TPS_VDCDC1));
  747. vdcdc1 = i2c_smbus_read_byte_data(the_tps->client, TPS_VDCDC1);
  748. switch (mode) {
  749. case OFF:
  750. vdcdc1 &= ~TPS_ENABLE_LP; /* disable ENABLE_LP bit */
  751. break;
  752. /* case ON: */
  753. default:
  754. vdcdc1 |= TPS_ENABLE_LP; /* enable ENABLE_LP bit */
  755. break;
  756. }
  757. status = i2c_smbus_write_byte_data(the_tps->client,
  758. TPS_VDCDC1, vdcdc1);
  759. if (status != 0)
  760. printk(KERN_ERR "%s: Failed to write vdcdc1 register\n",
  761. DRIVER_NAME);
  762. else
  763. pr_debug("%s: vdcdc1 0x%02x\n", DRIVER_NAME,
  764. i2c_smbus_read_byte_data(the_tps->client, TPS_VDCDC1));
  765. mutex_unlock(&the_tps->lock);
  766. return status;
  767. }
  768. EXPORT_SYMBOL(tps65010_set_low_pwr);
  769. /*-------------------------------------------------------------------------*/
  770. /* tps65010_config_vregs1 parameter:
  771. * value to be written to VREGS1 register
  772. * Note: The complete register is written, set all bits you need
  773. */
  774. int tps65010_config_vregs1(unsigned value)
  775. {
  776. int status;
  777. if (!the_tps)
  778. return -ENODEV;
  779. mutex_lock(&the_tps->lock);
  780. pr_debug("%s: vregs1 0x%02x\n", DRIVER_NAME,
  781. i2c_smbus_read_byte_data(the_tps->client, TPS_VREGS1));
  782. status = i2c_smbus_write_byte_data(the_tps->client,
  783. TPS_VREGS1, value);
  784. if (status != 0)
  785. printk(KERN_ERR "%s: Failed to write vregs1 register\n",
  786. DRIVER_NAME);
  787. else
  788. pr_debug("%s: vregs1 0x%02x\n", DRIVER_NAME,
  789. i2c_smbus_read_byte_data(the_tps->client, TPS_VREGS1));
  790. mutex_unlock(&the_tps->lock);
  791. return status;
  792. }
  793. EXPORT_SYMBOL(tps65010_config_vregs1);
  794. int tps65010_config_vdcdc2(unsigned value)
  795. {
  796. struct i2c_client *c;
  797. int status;
  798. if (!the_tps)
  799. return -ENODEV;
  800. c = the_tps->client;
  801. mutex_lock(&the_tps->lock);
  802. pr_debug("%s: vdcdc2 0x%02x\n", DRIVER_NAME,
  803. i2c_smbus_read_byte_data(c, TPS_VDCDC2));
  804. status = i2c_smbus_write_byte_data(c, TPS_VDCDC2, value);
  805. if (status != 0)
  806. printk(KERN_ERR "%s: Failed to write vdcdc2 register\n",
  807. DRIVER_NAME);
  808. else
  809. pr_debug("%s: vregs1 0x%02x\n", DRIVER_NAME,
  810. i2c_smbus_read_byte_data(c, TPS_VDCDC2));
  811. mutex_unlock(&the_tps->lock);
  812. return status;
  813. }
  814. EXPORT_SYMBOL(tps65010_config_vdcdc2);
  815. /*-------------------------------------------------------------------------*/
  816. /* tps65013_set_low_pwr parameter:
  817. * mode: ON or OFF
  818. */
  819. /* FIXME: Assumes AC or USB power is present. Setting AUA bit is not
  820. required if power supply is through a battery */
  821. int tps65013_set_low_pwr(unsigned mode)
  822. {
  823. int status;
  824. unsigned vdcdc1, chgconfig;
  825. if (!the_tps || the_tps->por)
  826. return -ENODEV;
  827. mutex_lock(&the_tps->lock);
  828. pr_debug("%s: %s low_pwr, chgconfig 0x%02x vdcdc1 0x%02x\n",
  829. DRIVER_NAME,
  830. mode ? "enable" : "disable",
  831. i2c_smbus_read_byte_data(the_tps->client, TPS_CHGCONFIG),
  832. i2c_smbus_read_byte_data(the_tps->client, TPS_VDCDC1));
  833. chgconfig = i2c_smbus_read_byte_data(the_tps->client, TPS_CHGCONFIG);
  834. vdcdc1 = i2c_smbus_read_byte_data(the_tps->client, TPS_VDCDC1);
  835. switch (mode) {
  836. case OFF:
  837. chgconfig &= ~TPS65013_AUA; /* disable AUA bit */
  838. vdcdc1 &= ~TPS_ENABLE_LP; /* disable ENABLE_LP bit */
  839. break;
  840. /* case ON: */
  841. default:
  842. chgconfig |= TPS65013_AUA; /* enable AUA bit */
  843. vdcdc1 |= TPS_ENABLE_LP; /* enable ENABLE_LP bit */
  844. break;
  845. }
  846. status = i2c_smbus_write_byte_data(the_tps->client,
  847. TPS_CHGCONFIG, chgconfig);
  848. if (status != 0) {
  849. printk(KERN_ERR "%s: Failed to write chconfig register\n",
  850. DRIVER_NAME);
  851. mutex_unlock(&the_tps->lock);
  852. return status;
  853. }
  854. chgconfig = i2c_smbus_read_byte_data(the_tps->client, TPS_CHGCONFIG);
  855. the_tps->chgconf = chgconfig;
  856. show_chgconfig(0, "chgconf", chgconfig);
  857. status = i2c_smbus_write_byte_data(the_tps->client,
  858. TPS_VDCDC1, vdcdc1);
  859. if (status != 0)
  860. printk(KERN_ERR "%s: Failed to write vdcdc1 register\n",
  861. DRIVER_NAME);
  862. else
  863. pr_debug("%s: vdcdc1 0x%02x\n", DRIVER_NAME,
  864. i2c_smbus_read_byte_data(the_tps->client, TPS_VDCDC1));
  865. mutex_unlock(&the_tps->lock);
  866. return status;
  867. }
  868. EXPORT_SYMBOL(tps65013_set_low_pwr);
  869. /*-------------------------------------------------------------------------*/
  870. static int __init tps_init(void)
  871. {
  872. u32 tries = 3;
  873. int status = -ENODEV;
  874. printk(KERN_INFO "%s: version %s\n", DRIVER_NAME, DRIVER_VERSION);
  875. /* some boards have startup glitches */
  876. while (tries--) {
  877. status = i2c_add_driver(&tps65010_driver);
  878. if (the_tps)
  879. break;
  880. i2c_del_driver(&tps65010_driver);
  881. if (!tries) {
  882. printk(KERN_ERR "%s: no chip?\n", DRIVER_NAME);
  883. return -ENODEV;
  884. }
  885. pr_debug("%s: re-probe ...\n", DRIVER_NAME);
  886. msleep(10);
  887. }
  888. return status;
  889. }
  890. /* NOTE: this MUST be initialized before the other parts of the system
  891. * that rely on it ... but after the i2c bus on which this relies.
  892. * That is, much earlier than on PC-type systems, which don't often use
  893. * I2C as a core system bus.
  894. */
  895. subsys_initcall(tps_init);
  896. static void __exit tps_exit(void)
  897. {
  898. i2c_del_driver(&tps65010_driver);
  899. }
  900. module_exit(tps_exit);