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