tps65010.c 28 KB

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