tps65010.c 28 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/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 work_struct 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 irqs 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(void *_tps)
  346. {
  347. struct tps65010 *tps = _tps;
  348. mutex_lock(&tps->lock);
  349. tps65010_interrupt(tps);
  350. if (test_and_clear_bit(FLAG_VBUS_CHANGED, &tps->flags)) {
  351. int status;
  352. u8 chgconfig, tmp;
  353. chgconfig = i2c_smbus_read_byte_data(&tps->client,
  354. TPS_CHGCONFIG);
  355. chgconfig &= ~(TPS_VBUS_500MA | TPS_VBUS_CHARGING);
  356. if (tps->vbus == 500)
  357. chgconfig |= TPS_VBUS_500MA | TPS_VBUS_CHARGING;
  358. else if (tps->vbus >= 100)
  359. chgconfig |= TPS_VBUS_CHARGING;
  360. status = i2c_smbus_write_byte_data(&tps->client,
  361. TPS_CHGCONFIG, chgconfig);
  362. /* vbus update fails unless VBUS is connected! */
  363. tmp = i2c_smbus_read_byte_data(&tps->client, TPS_CHGCONFIG);
  364. tps->chgconf = tmp;
  365. show_chgconfig(tps->por, "update vbus", tmp);
  366. }
  367. if (test_and_clear_bit(FLAG_IRQ_ENABLE, &tps->flags))
  368. enable_irq(tps->irq);
  369. mutex_unlock(&tps->lock);
  370. }
  371. static irqreturn_t tps65010_irq(int irq, void *_tps)
  372. {
  373. struct tps65010 *tps = _tps;
  374. disable_irq_nosync(irq);
  375. set_bit(FLAG_IRQ_ENABLE, &tps->flags);
  376. (void) schedule_work(&tps->work);
  377. return IRQ_HANDLED;
  378. }
  379. /*-------------------------------------------------------------------------*/
  380. static struct tps65010 *the_tps;
  381. static int __exit tps65010_detach_client(struct i2c_client *client)
  382. {
  383. struct tps65010 *tps;
  384. tps = container_of(client, struct tps65010, client);
  385. #ifdef CONFIG_ARM
  386. if (machine_is_omap_h2())
  387. omap_free_gpio(58);
  388. if (machine_is_omap_osk())
  389. omap_free_gpio(OMAP_MPUIO(1));
  390. #endif
  391. free_irq(tps->irq, tps);
  392. debugfs_remove(tps->file);
  393. if (i2c_detach_client(client) == 0)
  394. kfree(tps);
  395. the_tps = NULL;
  396. return 0;
  397. }
  398. static int tps65010_noscan(struct i2c_adapter *bus)
  399. {
  400. /* pure paranoia, in case someone adds another i2c bus
  401. * after our init section's gone...
  402. */
  403. return -ENODEV;
  404. }
  405. /* no error returns, they'd just make bus scanning stop */
  406. static int __init
  407. tps65010_probe(struct i2c_adapter *bus, int address, int kind)
  408. {
  409. struct tps65010 *tps;
  410. int status;
  411. unsigned long irqflags;
  412. if (the_tps) {
  413. dev_dbg(&bus->dev, "only one %s for now\n", DRIVER_NAME);
  414. return 0;
  415. }
  416. tps = kzalloc(sizeof *tps, GFP_KERNEL);
  417. if (!tps)
  418. return 0;
  419. mutex_init(&tps->lock);
  420. INIT_WORK(&tps->work, tps65010_work, tps);
  421. tps->irq = -1;
  422. tps->client.addr = address;
  423. tps->client.adapter = bus;
  424. tps->client.driver = &tps65010_driver;
  425. strlcpy(tps->client.name, DRIVER_NAME, I2C_NAME_SIZE);
  426. status = i2c_attach_client(&tps->client);
  427. if (status < 0) {
  428. dev_dbg(&bus->dev, "can't attach %s to device %d, err %d\n",
  429. DRIVER_NAME, address, status);
  430. goto fail1;
  431. }
  432. /* the IRQ is active low, but many gpio lines can't support that
  433. * so this driver can use falling-edge triggers instead.
  434. */
  435. irqflags = IRQF_SAMPLE_RANDOM;
  436. #ifdef CONFIG_ARM
  437. if (machine_is_omap_h2()) {
  438. tps->model = TPS65010;
  439. omap_cfg_reg(W4_GPIO58);
  440. tps->irq = OMAP_GPIO_IRQ(58);
  441. omap_request_gpio(58);
  442. omap_set_gpio_direction(58, 1);
  443. irqflags |= IRQF_TRIGGER_FALLING;
  444. }
  445. if (machine_is_omap_osk()) {
  446. tps->model = TPS65010;
  447. // omap_cfg_reg(U19_1610_MPUIO1);
  448. tps->irq = OMAP_GPIO_IRQ(OMAP_MPUIO(1));
  449. omap_request_gpio(OMAP_MPUIO(1));
  450. omap_set_gpio_direction(OMAP_MPUIO(1), 1);
  451. irqflags |= IRQF_TRIGGER_FALLING;
  452. }
  453. if (machine_is_omap_h3()) {
  454. tps->model = TPS65013;
  455. // FIXME set up this board's IRQ ...
  456. }
  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. mutex_lock(&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. mutex_unlock(&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. mutex_lock(&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. mutex_unlock(&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. mutex_unlock(&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. mutex_unlock(&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. mutex_unlock(&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. mutex_lock(&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. mutex_unlock(&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. mutex_lock(&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. mutex_unlock(&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. mutex_lock(&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. mutex_unlock(&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. mutex_lock(&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. mutex_unlock(&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. mutex_unlock(&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);