tps65010.c 27 KB

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