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. #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/device.h>
  27. #include <linux/i2c.h>
  28. #include <linux/delay.h>
  29. #include <linux/workqueue.h>
  30. #include <linux/suspend.h>
  31. #include <linux/debugfs.h>
  32. #include <linux/seq_file.h>
  33. #include <linux/mutex.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.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. I2C_CLIENT_INSMOD;
  46. static struct i2c_driver tps65010_driver;
  47. /*-------------------------------------------------------------------------*/
  48. /* This driver handles a family of multipurpose chips, which incorporate
  49. * voltage regulators, lithium ion/polymer battery charging, GPIOs, LEDs,
  50. * and other features often needed in portable devices like cell phones
  51. * or digital cameras.
  52. *
  53. * The tps65011 and tps65013 have different voltage settings compared
  54. * to tps65010 and tps65012. The tps65013 has a NO_CHG status/irq.
  55. * All except tps65010 have "wait" mode, possibly defaulted so that
  56. * battery-insert != device-on.
  57. *
  58. * We could distinguish between some models by checking VDCDC1.UVLO or
  59. * other registers, unless they've been changed already after powerup
  60. * as part of board setup by a bootloader.
  61. */
  62. enum tps_model {
  63. TPS_UNKNOWN = 0,
  64. TPS65010,
  65. TPS65011,
  66. TPS65012,
  67. TPS65013,
  68. };
  69. struct tps65010 {
  70. struct i2c_client client;
  71. struct mutex lock;
  72. int irq;
  73. struct delayed_work work;
  74. struct dentry *file;
  75. unsigned charging:1;
  76. unsigned por:1;
  77. unsigned model:8;
  78. u16 vbus;
  79. unsigned long flags;
  80. #define FLAG_VBUS_CHANGED 0
  81. #define FLAG_IRQ_ENABLE 1
  82. /* copies of last register state */
  83. u8 chgstatus, regstatus, chgconf;
  84. u8 nmask1, nmask2;
  85. /* not currently tracking GPIO state */
  86. };
  87. #define POWER_POLL_DELAY msecs_to_jiffies(5000)
  88. /*-------------------------------------------------------------------------*/
  89. #if defined(DEBUG) || defined(CONFIG_DEBUG_FS)
  90. static void dbg_chgstat(char *buf, size_t len, u8 chgstatus)
  91. {
  92. snprintf(buf, len, "%02x%s%s%s%s%s%s%s%s\n",
  93. chgstatus,
  94. (chgstatus & TPS_CHG_USB) ? " USB" : "",
  95. (chgstatus & TPS_CHG_AC) ? " AC" : "",
  96. (chgstatus & TPS_CHG_THERM) ? " therm" : "",
  97. (chgstatus & TPS_CHG_TERM) ? " done" :
  98. ((chgstatus & (TPS_CHG_USB|TPS_CHG_AC))
  99. ? " (charging)" : ""),
  100. (chgstatus & TPS_CHG_TAPER_TMO) ? " taper_tmo" : "",
  101. (chgstatus & TPS_CHG_CHG_TMO) ? " charge_tmo" : "",
  102. (chgstatus & TPS_CHG_PRECHG_TMO) ? " prechg_tmo" : "",
  103. (chgstatus & TPS_CHG_TEMP_ERR) ? " temp_err" : "");
  104. }
  105. static void dbg_regstat(char *buf, size_t len, u8 regstatus)
  106. {
  107. snprintf(buf, len, "%02x %s%s%s%s%s%s%s%s\n",
  108. regstatus,
  109. (regstatus & TPS_REG_ONOFF) ? "off" : "(on)",
  110. (regstatus & TPS_REG_COVER) ? " uncover" : "",
  111. (regstatus & TPS_REG_UVLO) ? " UVLO" : "",
  112. (regstatus & TPS_REG_NO_CHG) ? " NO_CHG" : "",
  113. (regstatus & TPS_REG_PG_LD02) ? " ld02_bad" : "",
  114. (regstatus & TPS_REG_PG_LD01) ? " ld01_bad" : "",
  115. (regstatus & TPS_REG_PG_MAIN) ? " main_bad" : "",
  116. (regstatus & TPS_REG_PG_CORE) ? " core_bad" : "");
  117. }
  118. static void dbg_chgconf(int por, char *buf, size_t len, u8 chgconfig)
  119. {
  120. const char *hibit;
  121. if (por)
  122. hibit = (chgconfig & TPS_CHARGE_POR)
  123. ? "POR=69ms" : "POR=1sec";
  124. else
  125. hibit = (chgconfig & TPS65013_AUA) ? "AUA" : "";
  126. snprintf(buf, len, "%02x %s%s%s AC=%d%% USB=%dmA %sCharge\n",
  127. chgconfig, hibit,
  128. (chgconfig & TPS_CHARGE_RESET) ? " reset" : "",
  129. (chgconfig & TPS_CHARGE_FAST) ? " fast" : "",
  130. ({int p; switch ((chgconfig >> 3) & 3) {
  131. case 3: p = 100; break;
  132. case 2: p = 75; break;
  133. case 1: p = 50; break;
  134. default: p = 25; break;
  135. }; p; }),
  136. (chgconfig & TPS_VBUS_CHARGING)
  137. ? ((chgconfig & TPS_VBUS_500MA) ? 500 : 100)
  138. : 0,
  139. (chgconfig & TPS_CHARGE_ENABLE) ? "" : "No");
  140. }
  141. #endif
  142. #ifdef DEBUG
  143. static void show_chgstatus(const char *label, u8 chgstatus)
  144. {
  145. char buf [100];
  146. dbg_chgstat(buf, sizeof buf, chgstatus);
  147. pr_debug("%s: %s %s", DRIVER_NAME, label, buf);
  148. }
  149. static void show_regstatus(const char *label, u8 regstatus)
  150. {
  151. char buf [100];
  152. dbg_regstat(buf, sizeof buf, regstatus);
  153. pr_debug("%s: %s %s", DRIVER_NAME, label, buf);
  154. }
  155. static void show_chgconfig(int por, const char *label, u8 chgconfig)
  156. {
  157. char buf [100];
  158. dbg_chgconf(por, buf, sizeof buf, chgconfig);
  159. pr_debug("%s: %s %s", DRIVER_NAME, label, buf);
  160. }
  161. #else
  162. static inline void show_chgstatus(const char *label, u8 chgstatus) { }
  163. static inline void show_regstatus(const char *label, u8 chgstatus) { }
  164. static inline void show_chgconfig(int por, const char *label, u8 chgconfig) { }
  165. #endif
  166. #ifdef CONFIG_DEBUG_FS
  167. static int dbg_show(struct seq_file *s, void *_)
  168. {
  169. struct tps65010 *tps = s->private;
  170. u8 value, v2;
  171. unsigned i;
  172. char buf[100];
  173. const char *chip;
  174. switch (tps->model) {
  175. case TPS65010: chip = "tps65010"; break;
  176. case TPS65011: chip = "tps65011"; break;
  177. case TPS65012: chip = "tps65012"; break;
  178. case TPS65013: chip = "tps65013"; break;
  179. default: chip = NULL; break;
  180. }
  181. seq_printf(s, "driver %s\nversion %s\nchip %s\n\n",
  182. DRIVER_NAME, DRIVER_VERSION, chip);
  183. mutex_lock(&tps->lock);
  184. /* FIXME how can we tell whether a battery is present?
  185. * likely involves a charge gauging chip (like BQ26501).
  186. */
  187. seq_printf(s, "%scharging\n\n", tps->charging ? "" : "(not) ");
  188. /* registers for monitoring battery charging and status; note
  189. * that reading chgstat and regstat may ack IRQs...
  190. */
  191. value = i2c_smbus_read_byte_data(&tps->client, TPS_CHGCONFIG);
  192. dbg_chgconf(tps->por, buf, sizeof buf, value);
  193. seq_printf(s, "chgconfig %s", buf);
  194. value = i2c_smbus_read_byte_data(&tps->client, TPS_CHGSTATUS);
  195. dbg_chgstat(buf, sizeof buf, value);
  196. seq_printf(s, "chgstat %s", buf);
  197. value = i2c_smbus_read_byte_data(&tps->client, TPS_MASK1);
  198. dbg_chgstat(buf, sizeof buf, value);
  199. seq_printf(s, "mask1 %s", buf);
  200. /* ignore ackint1 */
  201. value = i2c_smbus_read_byte_data(&tps->client, TPS_REGSTATUS);
  202. dbg_regstat(buf, sizeof buf, value);
  203. seq_printf(s, "regstat %s", buf);
  204. value = i2c_smbus_read_byte_data(&tps->client, TPS_MASK2);
  205. dbg_regstat(buf, sizeof buf, value);
  206. seq_printf(s, "mask2 %s\n", buf);
  207. /* ignore ackint2 */
  208. (void) schedule_delayed_work(&tps->work, POWER_POLL_DELAY);
  209. /* VMAIN voltage, enable lowpower, etc */
  210. value = i2c_smbus_read_byte_data(&tps->client, TPS_VDCDC1);
  211. seq_printf(s, "vdcdc1 %02x\n", value);
  212. /* VCORE voltage, vibrator on/off */
  213. value = i2c_smbus_read_byte_data(&tps->client, TPS_VDCDC2);
  214. seq_printf(s, "vdcdc2 %02x\n", value);
  215. /* both LD0s, and their lowpower behavior */
  216. value = i2c_smbus_read_byte_data(&tps->client, TPS_VREGS1);
  217. seq_printf(s, "vregs1 %02x\n\n", value);
  218. /* LEDs and GPIOs */
  219. value = i2c_smbus_read_byte_data(&tps->client, TPS_LED1_ON);
  220. v2 = i2c_smbus_read_byte_data(&tps->client, TPS_LED1_PER);
  221. seq_printf(s, "led1 %s, on=%02x, per=%02x, %d/%d msec\n",
  222. (value & 0x80)
  223. ? ((v2 & 0x80) ? "on" : "off")
  224. : ((v2 & 0x80) ? "blink" : "(nPG)"),
  225. value, v2,
  226. (value & 0x7f) * 10, (v2 & 0x7f) * 100);
  227. value = i2c_smbus_read_byte_data(&tps->client, TPS_LED2_ON);
  228. v2 = i2c_smbus_read_byte_data(&tps->client, TPS_LED2_PER);
  229. seq_printf(s, "led2 %s, on=%02x, per=%02x, %d/%d msec\n",
  230. (value & 0x80)
  231. ? ((v2 & 0x80) ? "on" : "off")
  232. : ((v2 & 0x80) ? "blink" : "off"),
  233. value, v2,
  234. (value & 0x7f) * 10, (v2 & 0x7f) * 100);
  235. value = i2c_smbus_read_byte_data(&tps->client, TPS_DEFGPIO);
  236. v2 = i2c_smbus_read_byte_data(&tps->client, TPS_MASK3);
  237. seq_printf(s, "defgpio %02x mask3 %02x\n", value, v2);
  238. for (i = 0; i < 4; i++) {
  239. if (value & (1 << (4 + i)))
  240. seq_printf(s, " gpio%d-out %s\n", i + 1,
  241. (value & (1 << i)) ? "low" : "hi ");
  242. else
  243. seq_printf(s, " gpio%d-in %s %s %s\n", i + 1,
  244. (value & (1 << i)) ? "hi " : "low",
  245. (v2 & (1 << i)) ? "no-irq" : "irq",
  246. (v2 & (1 << (4 + i))) ? "rising" : "falling");
  247. }
  248. mutex_unlock(&tps->lock);
  249. return 0;
  250. }
  251. static int dbg_tps_open(struct inode *inode, struct file *file)
  252. {
  253. return single_open(file, dbg_show, inode->i_private);
  254. }
  255. static struct file_operations debug_fops = {
  256. .open = dbg_tps_open,
  257. .read = seq_read,
  258. .llseek = seq_lseek,
  259. .release = single_release,
  260. };
  261. #define DEBUG_FOPS &debug_fops
  262. #else
  263. #define DEBUG_FOPS NULL
  264. #endif
  265. /*-------------------------------------------------------------------------*/
  266. /* handle IRQS in a task context, so we can use I2C calls */
  267. static void tps65010_interrupt(struct tps65010 *tps)
  268. {
  269. u8 tmp = 0, mask, poll;
  270. /* IRQs won't trigger for certain events, but we can get
  271. * others by polling (normally, with external power applied).
  272. */
  273. poll = 0;
  274. /* regstatus irqs */
  275. if (tps->nmask2) {
  276. tmp = i2c_smbus_read_byte_data(&tps->client, TPS_REGSTATUS);
  277. mask = tmp ^ tps->regstatus;
  278. tps->regstatus = tmp;
  279. mask &= tps->nmask2;
  280. } else
  281. mask = 0;
  282. if (mask) {
  283. tps->regstatus = tmp;
  284. /* may need to shut something down ... */
  285. /* "off" usually means deep sleep */
  286. if (tmp & TPS_REG_ONOFF) {
  287. pr_info("%s: power off button\n", DRIVER_NAME);
  288. #if 0
  289. /* REVISIT: this might need its own workqueue
  290. * plus tweaks including deadlock avoidance ...
  291. */
  292. software_suspend();
  293. #endif
  294. poll = 1;
  295. }
  296. }
  297. /* chgstatus irqs */
  298. if (tps->nmask1) {
  299. tmp = i2c_smbus_read_byte_data(&tps->client, TPS_CHGSTATUS);
  300. mask = tmp ^ tps->chgstatus;
  301. tps->chgstatus = tmp;
  302. mask &= tps->nmask1;
  303. } else
  304. mask = 0;
  305. if (mask) {
  306. unsigned charging = 0;
  307. show_chgstatus("chg/irq", tmp);
  308. if (tmp & (TPS_CHG_USB|TPS_CHG_AC))
  309. show_chgconfig(tps->por, "conf", tps->chgconf);
  310. /* Unless it was turned off or disabled, we charge any
  311. * battery whenever there's power available for it
  312. * and the charger hasn't been disabled.
  313. */
  314. if (!(tps->chgstatus & ~(TPS_CHG_USB|TPS_CHG_AC))
  315. && (tps->chgstatus & (TPS_CHG_USB|TPS_CHG_AC))
  316. && (tps->chgconf & TPS_CHARGE_ENABLE)
  317. ) {
  318. if (tps->chgstatus & TPS_CHG_USB) {
  319. /* VBUS options are readonly until reconnect */
  320. if (mask & TPS_CHG_USB)
  321. set_bit(FLAG_VBUS_CHANGED, &tps->flags);
  322. charging = 1;
  323. } else if (tps->chgstatus & TPS_CHG_AC)
  324. charging = 1;
  325. }
  326. if (charging != tps->charging) {
  327. tps->charging = charging;
  328. pr_info("%s: battery %scharging\n",
  329. DRIVER_NAME, charging ? "" :
  330. ((tps->chgstatus & (TPS_CHG_USB|TPS_CHG_AC))
  331. ? "NOT " : "dis"));
  332. }
  333. }
  334. /* always poll to detect (a) power removal, without tps65013
  335. * NO_CHG IRQ; or (b) restart of charging after stop.
  336. */
  337. if ((tps->model != TPS65013 || !tps->charging)
  338. && (tps->chgstatus & (TPS_CHG_USB|TPS_CHG_AC)))
  339. poll = 1;
  340. if (poll)
  341. (void) schedule_delayed_work(&tps->work, POWER_POLL_DELAY);
  342. /* also potentially gpio-in rise or fall */
  343. }
  344. /* handle IRQs and polling using keventd for now */
  345. static void tps65010_work(struct work_struct *work)
  346. {
  347. struct tps65010 *tps;
  348. tps = container_of(work, struct tps65010, work.work);
  349. mutex_lock(&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. mutex_unlock(&tps->lock);
  371. }
  372. static irqreturn_t tps65010_irq(int irq, void *_tps)
  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.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. free_irq(tps->irq, tps);
  387. #ifdef CONFIG_ARM
  388. if (machine_is_omap_h2())
  389. omap_free_gpio(58);
  390. if (machine_is_omap_osk())
  391. omap_free_gpio(OMAP_MPUIO(1));
  392. #endif
  393. cancel_delayed_work(&tps->work);
  394. flush_scheduled_work();
  395. debugfs_remove(tps->file);
  396. if (i2c_detach_client(client) == 0)
  397. kfree(tps);
  398. the_tps = NULL;
  399. return 0;
  400. }
  401. static int tps65010_noscan(struct i2c_adapter *bus)
  402. {
  403. /* pure paranoia, in case someone adds another i2c bus
  404. * after our init section's gone...
  405. */
  406. return -ENODEV;
  407. }
  408. /* no error returns, they'd just make bus scanning stop */
  409. static int __init
  410. tps65010_probe(struct i2c_adapter *bus, int address, int kind)
  411. {
  412. struct tps65010 *tps;
  413. int status;
  414. unsigned long irqflags;
  415. if (the_tps) {
  416. dev_dbg(&bus->dev, "only one %s for now\n", DRIVER_NAME);
  417. return 0;
  418. }
  419. tps = kzalloc(sizeof *tps, GFP_KERNEL);
  420. if (!tps)
  421. return 0;
  422. mutex_init(&tps->lock);
  423. INIT_DELAYED_WORK(&tps->work, tps65010_work);
  424. tps->irq = -1;
  425. tps->client.addr = address;
  426. tps->client.adapter = bus;
  427. tps->client.driver = &tps65010_driver;
  428. strlcpy(tps->client.name, DRIVER_NAME, I2C_NAME_SIZE);
  429. status = i2c_attach_client(&tps->client);
  430. if (status < 0) {
  431. dev_dbg(&bus->dev, "can't attach %s to device %d, err %d\n",
  432. DRIVER_NAME, address, status);
  433. goto fail1;
  434. }
  435. /* the IRQ is active low, but many gpio lines can't support that
  436. * so this driver can use falling-edge triggers instead.
  437. */
  438. irqflags = IRQF_SAMPLE_RANDOM;
  439. #ifdef CONFIG_ARM
  440. if (machine_is_omap_h2()) {
  441. tps->model = TPS65010;
  442. omap_cfg_reg(W4_GPIO58);
  443. tps->irq = OMAP_GPIO_IRQ(58);
  444. omap_request_gpio(58);
  445. omap_set_gpio_direction(58, 1);
  446. irqflags |= IRQF_TRIGGER_FALLING;
  447. }
  448. if (machine_is_omap_osk()) {
  449. tps->model = TPS65010;
  450. // omap_cfg_reg(U19_1610_MPUIO1);
  451. tps->irq = OMAP_GPIO_IRQ(OMAP_MPUIO(1));
  452. omap_request_gpio(OMAP_MPUIO(1));
  453. omap_set_gpio_direction(OMAP_MPUIO(1), 1);
  454. irqflags |= IRQF_TRIGGER_FALLING;
  455. }
  456. if (machine_is_omap_h3()) {
  457. tps->model = TPS65013;
  458. // FIXME set up this board's IRQ ...
  459. }
  460. #endif
  461. if (tps->irq > 0) {
  462. status = request_irq(tps->irq, tps65010_irq,
  463. irqflags, DRIVER_NAME, tps);
  464. if (status < 0) {
  465. dev_dbg(&tps->client.dev, "can't get IRQ %d, err %d\n",
  466. tps->irq, status);
  467. i2c_detach_client(&tps->client);
  468. goto fail1;
  469. }
  470. #ifdef CONFIG_ARM
  471. /* annoying race here, ideally we'd have an option
  472. * to claim the irq now and enable it later.
  473. */
  474. disable_irq(tps->irq);
  475. set_bit(FLAG_IRQ_ENABLE, &tps->flags);
  476. #endif
  477. } else
  478. printk(KERN_WARNING "%s: IRQ not configured!\n",
  479. DRIVER_NAME);
  480. switch (tps->model) {
  481. case TPS65010:
  482. case TPS65012:
  483. tps->por = 1;
  484. break;
  485. case TPS_UNKNOWN:
  486. printk(KERN_WARNING "%s: unknown TPS chip\n", DRIVER_NAME);
  487. break;
  488. /* else CHGCONFIG.POR is replaced by AUA, enabling a WAIT mode */
  489. }
  490. tps->chgconf = i2c_smbus_read_byte_data(&tps->client, TPS_CHGCONFIG);
  491. show_chgconfig(tps->por, "conf/init", tps->chgconf);
  492. show_chgstatus("chg/init",
  493. i2c_smbus_read_byte_data(&tps->client, TPS_CHGSTATUS));
  494. show_regstatus("reg/init",
  495. i2c_smbus_read_byte_data(&tps->client, TPS_REGSTATUS));
  496. pr_debug("%s: vdcdc1 0x%02x, vdcdc2 %02x, vregs1 %02x\n", DRIVER_NAME,
  497. i2c_smbus_read_byte_data(&tps->client, TPS_VDCDC1),
  498. i2c_smbus_read_byte_data(&tps->client, TPS_VDCDC2),
  499. i2c_smbus_read_byte_data(&tps->client, TPS_VREGS1));
  500. pr_debug("%s: defgpio 0x%02x, mask3 0x%02x\n", DRIVER_NAME,
  501. i2c_smbus_read_byte_data(&tps->client, TPS_DEFGPIO),
  502. i2c_smbus_read_byte_data(&tps->client, TPS_MASK3));
  503. tps65010_driver.attach_adapter = tps65010_noscan;
  504. the_tps = tps;
  505. #if defined(CONFIG_USB_GADGET) && !defined(CONFIG_USB_OTG)
  506. /* USB hosts can't draw VBUS. OTG devices could, later
  507. * when OTG infrastructure enables it. USB peripherals
  508. * could be relying on VBUS while booting, though.
  509. */
  510. tps->vbus = 100;
  511. #endif
  512. /* unmask the "interesting" irqs, then poll once to
  513. * kickstart monitoring, initialize shadowed status
  514. * registers, and maybe disable VBUS draw.
  515. */
  516. tps->nmask1 = ~0;
  517. (void) i2c_smbus_write_byte_data(&tps->client, TPS_MASK1, ~tps->nmask1);
  518. tps->nmask2 = TPS_REG_ONOFF;
  519. if (tps->model == TPS65013)
  520. tps->nmask2 |= TPS_REG_NO_CHG;
  521. (void) i2c_smbus_write_byte_data(&tps->client, TPS_MASK2, ~tps->nmask2);
  522. (void) i2c_smbus_write_byte_data(&tps->client, TPS_MASK3, 0x0f
  523. | i2c_smbus_read_byte_data(&tps->client, TPS_MASK3));
  524. tps65010_work(&tps->work.work);
  525. tps->file = debugfs_create_file(DRIVER_NAME, S_IRUGO, NULL,
  526. tps, DEBUG_FOPS);
  527. return 0;
  528. fail1:
  529. kfree(tps);
  530. return 0;
  531. }
  532. static int __init tps65010_scan_bus(struct i2c_adapter *bus)
  533. {
  534. if (!i2c_check_functionality(bus, I2C_FUNC_SMBUS_BYTE_DATA))
  535. return -EINVAL;
  536. return i2c_probe(bus, &addr_data, tps65010_probe);
  537. }
  538. static struct i2c_driver tps65010_driver = {
  539. .driver = {
  540. .name = "tps65010",
  541. },
  542. .attach_adapter = tps65010_scan_bus,
  543. .detach_client = __exit_p(tps65010_detach_client),
  544. };
  545. /*-------------------------------------------------------------------------*/
  546. /* Draw from VBUS:
  547. * 0 mA -- DON'T DRAW (might supply power instead)
  548. * 100 mA -- usb unit load (slowest charge rate)
  549. * 500 mA -- usb high power (fast battery charge)
  550. */
  551. int tps65010_set_vbus_draw(unsigned mA)
  552. {
  553. unsigned long flags;
  554. if (!the_tps)
  555. return -ENODEV;
  556. /* assumes non-SMP */
  557. local_irq_save(flags);
  558. if (mA >= 500)
  559. mA = 500;
  560. else if (mA >= 100)
  561. mA = 100;
  562. else
  563. mA = 0;
  564. the_tps->vbus = mA;
  565. if ((the_tps->chgstatus & TPS_CHG_USB)
  566. && test_and_set_bit(
  567. FLAG_VBUS_CHANGED, &the_tps->flags)) {
  568. /* gadget drivers call this in_irq() */
  569. (void) schedule_work(&the_tps->work.work);
  570. }
  571. local_irq_restore(flags);
  572. return 0;
  573. }
  574. EXPORT_SYMBOL(tps65010_set_vbus_draw);
  575. /*-------------------------------------------------------------------------*/
  576. /* tps65010_set_gpio_out_value parameter:
  577. * gpio: GPIO1, GPIO2, GPIO3 or GPIO4
  578. * value: LOW or HIGH
  579. */
  580. int tps65010_set_gpio_out_value(unsigned gpio, unsigned value)
  581. {
  582. int status;
  583. unsigned defgpio;
  584. if (!the_tps)
  585. return -ENODEV;
  586. if ((gpio < GPIO1) || (gpio > GPIO4))
  587. return -EINVAL;
  588. mutex_lock(&the_tps->lock);
  589. defgpio = i2c_smbus_read_byte_data(&the_tps->client, TPS_DEFGPIO);
  590. /* Configure GPIO for output */
  591. defgpio |= 1 << (gpio + 3);
  592. /* Writing 1 forces a logic 0 on that GPIO and vice versa */
  593. switch (value) {
  594. case LOW:
  595. defgpio |= 1 << (gpio - 1); /* set GPIO low by writing 1 */
  596. break;
  597. /* case HIGH: */
  598. default:
  599. defgpio &= ~(1 << (gpio - 1)); /* set GPIO high by writing 0 */
  600. break;
  601. }
  602. status = i2c_smbus_write_byte_data(&the_tps->client,
  603. TPS_DEFGPIO, defgpio);
  604. pr_debug("%s: gpio%dout = %s, defgpio 0x%02x\n", DRIVER_NAME,
  605. gpio, value ? "high" : "low",
  606. i2c_smbus_read_byte_data(&the_tps->client, TPS_DEFGPIO));
  607. mutex_unlock(&the_tps->lock);
  608. return status;
  609. }
  610. EXPORT_SYMBOL(tps65010_set_gpio_out_value);
  611. /*-------------------------------------------------------------------------*/
  612. /* tps65010_set_led parameter:
  613. * led: LED1 or LED2
  614. * mode: ON, OFF or BLINK
  615. */
  616. int tps65010_set_led(unsigned led, unsigned mode)
  617. {
  618. int status;
  619. unsigned led_on, led_per, offs;
  620. if (!the_tps)
  621. return -ENODEV;
  622. if (led == LED1)
  623. offs = 0;
  624. else {
  625. offs = 2;
  626. led = LED2;
  627. }
  628. mutex_lock(&the_tps->lock);
  629. pr_debug("%s: led%i_on 0x%02x\n", DRIVER_NAME, led,
  630. i2c_smbus_read_byte_data(&the_tps->client,
  631. TPS_LED1_ON + offs));
  632. pr_debug("%s: led%i_per 0x%02x\n", DRIVER_NAME, led,
  633. i2c_smbus_read_byte_data(&the_tps->client,
  634. TPS_LED1_PER + offs));
  635. switch (mode) {
  636. case OFF:
  637. led_on = 1 << 7;
  638. led_per = 0 << 7;
  639. break;
  640. case ON:
  641. led_on = 1 << 7;
  642. led_per = 1 << 7;
  643. break;
  644. case BLINK:
  645. led_on = 0x30 | (0 << 7);
  646. led_per = 0x08 | (1 << 7);
  647. break;
  648. default:
  649. printk(KERN_ERR "%s: Wrong mode parameter for set_led()\n",
  650. DRIVER_NAME);
  651. mutex_unlock(&the_tps->lock);
  652. return -EINVAL;
  653. }
  654. status = i2c_smbus_write_byte_data(&the_tps->client,
  655. TPS_LED1_ON + offs, led_on);
  656. if (status != 0) {
  657. printk(KERN_ERR "%s: Failed to write led%i_on register\n",
  658. DRIVER_NAME, led);
  659. mutex_unlock(&the_tps->lock);
  660. return status;
  661. }
  662. pr_debug("%s: led%i_on 0x%02x\n", DRIVER_NAME, led,
  663. i2c_smbus_read_byte_data(&the_tps->client, TPS_LED1_ON + offs));
  664. status = i2c_smbus_write_byte_data(&the_tps->client,
  665. TPS_LED1_PER + offs, led_per);
  666. if (status != 0) {
  667. printk(KERN_ERR "%s: Failed to write led%i_per register\n",
  668. DRIVER_NAME, led);
  669. mutex_unlock(&the_tps->lock);
  670. return status;
  671. }
  672. pr_debug("%s: led%i_per 0x%02x\n", DRIVER_NAME, led,
  673. i2c_smbus_read_byte_data(&the_tps->client,
  674. TPS_LED1_PER + offs));
  675. mutex_unlock(&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. mutex_lock(&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. mutex_unlock(&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. mutex_lock(&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. mutex_unlock(&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. mutex_lock(&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. mutex_unlock(&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. mutex_lock(&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. mutex_unlock(&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. mutex_unlock(&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. #ifdef 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);