tps65010.c 28 KB

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