tps65010.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074
  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 const 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. * also needs to get error handling and probably
  292. * an #ifdef CONFIG_SOFTWARE_SUSPEND
  293. */
  294. hibernate();
  295. #endif
  296. poll = 1;
  297. }
  298. }
  299. /* chgstatus irqs */
  300. if (tps->nmask1) {
  301. tmp = i2c_smbus_read_byte_data(&tps->client, TPS_CHGSTATUS);
  302. mask = tmp ^ tps->chgstatus;
  303. tps->chgstatus = tmp;
  304. mask &= tps->nmask1;
  305. } else
  306. mask = 0;
  307. if (mask) {
  308. unsigned charging = 0;
  309. show_chgstatus("chg/irq", tmp);
  310. if (tmp & (TPS_CHG_USB|TPS_CHG_AC))
  311. show_chgconfig(tps->por, "conf", tps->chgconf);
  312. /* Unless it was turned off or disabled, we charge any
  313. * battery whenever there's power available for it
  314. * and the charger hasn't been disabled.
  315. */
  316. if (!(tps->chgstatus & ~(TPS_CHG_USB|TPS_CHG_AC))
  317. && (tps->chgstatus & (TPS_CHG_USB|TPS_CHG_AC))
  318. && (tps->chgconf & TPS_CHARGE_ENABLE)
  319. ) {
  320. if (tps->chgstatus & TPS_CHG_USB) {
  321. /* VBUS options are readonly until reconnect */
  322. if (mask & TPS_CHG_USB)
  323. set_bit(FLAG_VBUS_CHANGED, &tps->flags);
  324. charging = 1;
  325. } else if (tps->chgstatus & TPS_CHG_AC)
  326. charging = 1;
  327. }
  328. if (charging != tps->charging) {
  329. tps->charging = charging;
  330. pr_info("%s: battery %scharging\n",
  331. DRIVER_NAME, charging ? "" :
  332. ((tps->chgstatus & (TPS_CHG_USB|TPS_CHG_AC))
  333. ? "NOT " : "dis"));
  334. }
  335. }
  336. /* always poll to detect (a) power removal, without tps65013
  337. * NO_CHG IRQ; or (b) restart of charging after stop.
  338. */
  339. if ((tps->model != TPS65013 || !tps->charging)
  340. && (tps->chgstatus & (TPS_CHG_USB|TPS_CHG_AC)))
  341. poll = 1;
  342. if (poll)
  343. (void) schedule_delayed_work(&tps->work, POWER_POLL_DELAY);
  344. /* also potentially gpio-in rise or fall */
  345. }
  346. /* handle IRQs and polling using keventd for now */
  347. static void tps65010_work(struct work_struct *work)
  348. {
  349. struct tps65010 *tps;
  350. tps = container_of(work, struct tps65010, work.work);
  351. mutex_lock(&tps->lock);
  352. tps65010_interrupt(tps);
  353. if (test_and_clear_bit(FLAG_VBUS_CHANGED, &tps->flags)) {
  354. int status;
  355. u8 chgconfig, tmp;
  356. chgconfig = i2c_smbus_read_byte_data(&tps->client,
  357. TPS_CHGCONFIG);
  358. chgconfig &= ~(TPS_VBUS_500MA | TPS_VBUS_CHARGING);
  359. if (tps->vbus == 500)
  360. chgconfig |= TPS_VBUS_500MA | TPS_VBUS_CHARGING;
  361. else if (tps->vbus >= 100)
  362. chgconfig |= TPS_VBUS_CHARGING;
  363. status = i2c_smbus_write_byte_data(&tps->client,
  364. TPS_CHGCONFIG, chgconfig);
  365. /* vbus update fails unless VBUS is connected! */
  366. tmp = i2c_smbus_read_byte_data(&tps->client, TPS_CHGCONFIG);
  367. tps->chgconf = tmp;
  368. show_chgconfig(tps->por, "update vbus", tmp);
  369. }
  370. if (test_and_clear_bit(FLAG_IRQ_ENABLE, &tps->flags))
  371. enable_irq(tps->irq);
  372. mutex_unlock(&tps->lock);
  373. }
  374. static irqreturn_t tps65010_irq(int irq, void *_tps)
  375. {
  376. struct tps65010 *tps = _tps;
  377. disable_irq_nosync(irq);
  378. set_bit(FLAG_IRQ_ENABLE, &tps->flags);
  379. (void) schedule_work(&tps->work.work);
  380. return IRQ_HANDLED;
  381. }
  382. /*-------------------------------------------------------------------------*/
  383. static struct tps65010 *the_tps;
  384. static int __exit tps65010_detach_client(struct i2c_client *client)
  385. {
  386. struct tps65010 *tps;
  387. tps = container_of(client, struct tps65010, client);
  388. free_irq(tps->irq, tps);
  389. #ifdef CONFIG_ARM
  390. if (machine_is_omap_h2())
  391. omap_free_gpio(58);
  392. if (machine_is_omap_osk())
  393. omap_free_gpio(OMAP_MPUIO(1));
  394. #endif
  395. cancel_delayed_work(&tps->work);
  396. flush_scheduled_work();
  397. debugfs_remove(tps->file);
  398. if (i2c_detach_client(client) == 0)
  399. kfree(tps);
  400. the_tps = NULL;
  401. return 0;
  402. }
  403. static int tps65010_noscan(struct i2c_adapter *bus)
  404. {
  405. /* pure paranoia, in case someone adds another i2c bus
  406. * after our init section's gone...
  407. */
  408. return -ENODEV;
  409. }
  410. /* no error returns, they'd just make bus scanning stop */
  411. static int __init
  412. tps65010_probe(struct i2c_adapter *bus, int address, int kind)
  413. {
  414. struct tps65010 *tps;
  415. int status;
  416. unsigned long irqflags;
  417. if (the_tps) {
  418. dev_dbg(&bus->dev, "only one %s for now\n", DRIVER_NAME);
  419. return 0;
  420. }
  421. tps = kzalloc(sizeof *tps, GFP_KERNEL);
  422. if (!tps)
  423. return 0;
  424. mutex_init(&tps->lock);
  425. INIT_DELAYED_WORK(&tps->work, tps65010_work);
  426. tps->irq = -1;
  427. tps->client.addr = address;
  428. tps->client.adapter = bus;
  429. tps->client.driver = &tps65010_driver;
  430. strlcpy(tps->client.name, DRIVER_NAME, I2C_NAME_SIZE);
  431. status = i2c_attach_client(&tps->client);
  432. if (status < 0) {
  433. dev_dbg(&bus->dev, "can't attach %s to device %d, err %d\n",
  434. DRIVER_NAME, address, status);
  435. goto fail1;
  436. }
  437. /* the IRQ is active low, but many gpio lines can't support that
  438. * so this driver can use falling-edge triggers instead.
  439. */
  440. irqflags = IRQF_SAMPLE_RANDOM;
  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. irqflags |= IRQF_TRIGGER_FALLING;
  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. irqflags |= IRQF_TRIGGER_FALLING;
  457. }
  458. if (machine_is_omap_h3()) {
  459. tps->model = TPS65013;
  460. // FIXME set up this board's IRQ ...
  461. }
  462. #endif
  463. if (tps->irq > 0) {
  464. status = request_irq(tps->irq, tps65010_irq,
  465. irqflags, DRIVER_NAME, tps);
  466. if (status < 0) {
  467. dev_dbg(&tps->client.dev, "can't get IRQ %d, err %d\n",
  468. tps->irq, status);
  469. i2c_detach_client(&tps->client);
  470. goto fail1;
  471. }
  472. #ifdef CONFIG_ARM
  473. /* annoying race here, ideally we'd have an option
  474. * to claim the irq now and enable it later.
  475. */
  476. disable_irq(tps->irq);
  477. set_bit(FLAG_IRQ_ENABLE, &tps->flags);
  478. #endif
  479. } else
  480. printk(KERN_WARNING "%s: IRQ not configured!\n",
  481. DRIVER_NAME);
  482. switch (tps->model) {
  483. case TPS65010:
  484. case TPS65012:
  485. tps->por = 1;
  486. break;
  487. case TPS_UNKNOWN:
  488. printk(KERN_WARNING "%s: unknown TPS chip\n", DRIVER_NAME);
  489. break;
  490. /* else CHGCONFIG.POR is replaced by AUA, enabling a WAIT mode */
  491. }
  492. tps->chgconf = i2c_smbus_read_byte_data(&tps->client, TPS_CHGCONFIG);
  493. show_chgconfig(tps->por, "conf/init", tps->chgconf);
  494. show_chgstatus("chg/init",
  495. i2c_smbus_read_byte_data(&tps->client, TPS_CHGSTATUS));
  496. show_regstatus("reg/init",
  497. i2c_smbus_read_byte_data(&tps->client, TPS_REGSTATUS));
  498. pr_debug("%s: vdcdc1 0x%02x, vdcdc2 %02x, vregs1 %02x\n", DRIVER_NAME,
  499. i2c_smbus_read_byte_data(&tps->client, TPS_VDCDC1),
  500. i2c_smbus_read_byte_data(&tps->client, TPS_VDCDC2),
  501. i2c_smbus_read_byte_data(&tps->client, TPS_VREGS1));
  502. pr_debug("%s: defgpio 0x%02x, mask3 0x%02x\n", DRIVER_NAME,
  503. i2c_smbus_read_byte_data(&tps->client, TPS_DEFGPIO),
  504. i2c_smbus_read_byte_data(&tps->client, TPS_MASK3));
  505. tps65010_driver.attach_adapter = tps65010_noscan;
  506. the_tps = tps;
  507. #if defined(CONFIG_USB_GADGET) && !defined(CONFIG_USB_OTG)
  508. /* USB hosts can't draw VBUS. OTG devices could, later
  509. * when OTG infrastructure enables it. USB peripherals
  510. * could be relying on VBUS while booting, though.
  511. */
  512. tps->vbus = 100;
  513. #endif
  514. /* unmask the "interesting" irqs, then poll once to
  515. * kickstart monitoring, initialize shadowed status
  516. * registers, and maybe disable VBUS draw.
  517. */
  518. tps->nmask1 = ~0;
  519. (void) i2c_smbus_write_byte_data(&tps->client, TPS_MASK1, ~tps->nmask1);
  520. tps->nmask2 = TPS_REG_ONOFF;
  521. if (tps->model == TPS65013)
  522. tps->nmask2 |= TPS_REG_NO_CHG;
  523. (void) i2c_smbus_write_byte_data(&tps->client, TPS_MASK2, ~tps->nmask2);
  524. (void) i2c_smbus_write_byte_data(&tps->client, TPS_MASK3, 0x0f
  525. | i2c_smbus_read_byte_data(&tps->client, TPS_MASK3));
  526. tps65010_work(&tps->work.work);
  527. tps->file = debugfs_create_file(DRIVER_NAME, S_IRUGO, NULL,
  528. tps, DEBUG_FOPS);
  529. return 0;
  530. fail1:
  531. kfree(tps);
  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. .driver = {
  542. .name = "tps65010",
  543. },
  544. .attach_adapter = tps65010_scan_bus,
  545. .detach_client = __exit_p(tps65010_detach_client),
  546. };
  547. /*-------------------------------------------------------------------------*/
  548. /* Draw from VBUS:
  549. * 0 mA -- DON'T DRAW (might supply power instead)
  550. * 100 mA -- usb unit load (slowest charge rate)
  551. * 500 mA -- usb high power (fast battery charge)
  552. */
  553. int tps65010_set_vbus_draw(unsigned mA)
  554. {
  555. unsigned long flags;
  556. if (!the_tps)
  557. return -ENODEV;
  558. /* assumes non-SMP */
  559. local_irq_save(flags);
  560. if (mA >= 500)
  561. mA = 500;
  562. else if (mA >= 100)
  563. mA = 100;
  564. else
  565. mA = 0;
  566. the_tps->vbus = mA;
  567. if ((the_tps->chgstatus & TPS_CHG_USB)
  568. && test_and_set_bit(
  569. FLAG_VBUS_CHANGED, &the_tps->flags)) {
  570. /* gadget drivers call this in_irq() */
  571. (void) schedule_work(&the_tps->work.work);
  572. }
  573. local_irq_restore(flags);
  574. return 0;
  575. }
  576. EXPORT_SYMBOL(tps65010_set_vbus_draw);
  577. /*-------------------------------------------------------------------------*/
  578. /* tps65010_set_gpio_out_value parameter:
  579. * gpio: GPIO1, GPIO2, GPIO3 or GPIO4
  580. * value: LOW or HIGH
  581. */
  582. int tps65010_set_gpio_out_value(unsigned gpio, unsigned value)
  583. {
  584. int status;
  585. unsigned defgpio;
  586. if (!the_tps)
  587. return -ENODEV;
  588. if ((gpio < GPIO1) || (gpio > GPIO4))
  589. return -EINVAL;
  590. mutex_lock(&the_tps->lock);
  591. defgpio = i2c_smbus_read_byte_data(&the_tps->client, TPS_DEFGPIO);
  592. /* Configure GPIO for output */
  593. defgpio |= 1 << (gpio + 3);
  594. /* Writing 1 forces a logic 0 on that GPIO and vice versa */
  595. switch (value) {
  596. case LOW:
  597. defgpio |= 1 << (gpio - 1); /* set GPIO low by writing 1 */
  598. break;
  599. /* case HIGH: */
  600. default:
  601. defgpio &= ~(1 << (gpio - 1)); /* set GPIO high by writing 0 */
  602. break;
  603. }
  604. status = i2c_smbus_write_byte_data(&the_tps->client,
  605. TPS_DEFGPIO, defgpio);
  606. pr_debug("%s: gpio%dout = %s, defgpio 0x%02x\n", DRIVER_NAME,
  607. gpio, value ? "high" : "low",
  608. i2c_smbus_read_byte_data(&the_tps->client, TPS_DEFGPIO));
  609. mutex_unlock(&the_tps->lock);
  610. return status;
  611. }
  612. EXPORT_SYMBOL(tps65010_set_gpio_out_value);
  613. /*-------------------------------------------------------------------------*/
  614. /* tps65010_set_led parameter:
  615. * led: LED1 or LED2
  616. * mode: ON, OFF or BLINK
  617. */
  618. int tps65010_set_led(unsigned led, unsigned mode)
  619. {
  620. int status;
  621. unsigned led_on, led_per, offs;
  622. if (!the_tps)
  623. return -ENODEV;
  624. if (led == LED1)
  625. offs = 0;
  626. else {
  627. offs = 2;
  628. led = LED2;
  629. }
  630. mutex_lock(&the_tps->lock);
  631. pr_debug("%s: led%i_on 0x%02x\n", DRIVER_NAME, led,
  632. i2c_smbus_read_byte_data(&the_tps->client,
  633. TPS_LED1_ON + offs));
  634. pr_debug("%s: led%i_per 0x%02x\n", DRIVER_NAME, led,
  635. i2c_smbus_read_byte_data(&the_tps->client,
  636. TPS_LED1_PER + offs));
  637. switch (mode) {
  638. case OFF:
  639. led_on = 1 << 7;
  640. led_per = 0 << 7;
  641. break;
  642. case ON:
  643. led_on = 1 << 7;
  644. led_per = 1 << 7;
  645. break;
  646. case BLINK:
  647. led_on = 0x30 | (0 << 7);
  648. led_per = 0x08 | (1 << 7);
  649. break;
  650. default:
  651. printk(KERN_ERR "%s: Wrong mode parameter for set_led()\n",
  652. DRIVER_NAME);
  653. mutex_unlock(&the_tps->lock);
  654. return -EINVAL;
  655. }
  656. status = i2c_smbus_write_byte_data(&the_tps->client,
  657. TPS_LED1_ON + offs, led_on);
  658. if (status != 0) {
  659. printk(KERN_ERR "%s: Failed to write led%i_on register\n",
  660. DRIVER_NAME, led);
  661. mutex_unlock(&the_tps->lock);
  662. return status;
  663. }
  664. pr_debug("%s: led%i_on 0x%02x\n", DRIVER_NAME, led,
  665. i2c_smbus_read_byte_data(&the_tps->client, TPS_LED1_ON + offs));
  666. status = i2c_smbus_write_byte_data(&the_tps->client,
  667. TPS_LED1_PER + offs, led_per);
  668. if (status != 0) {
  669. printk(KERN_ERR "%s: Failed to write led%i_per register\n",
  670. DRIVER_NAME, led);
  671. mutex_unlock(&the_tps->lock);
  672. return status;
  673. }
  674. pr_debug("%s: led%i_per 0x%02x\n", DRIVER_NAME, led,
  675. i2c_smbus_read_byte_data(&the_tps->client,
  676. TPS_LED1_PER + offs));
  677. mutex_unlock(&the_tps->lock);
  678. return status;
  679. }
  680. EXPORT_SYMBOL(tps65010_set_led);
  681. /*-------------------------------------------------------------------------*/
  682. /* tps65010_set_vib parameter:
  683. * value: ON or OFF
  684. */
  685. int tps65010_set_vib(unsigned value)
  686. {
  687. int status;
  688. unsigned vdcdc2;
  689. if (!the_tps)
  690. return -ENODEV;
  691. mutex_lock(&the_tps->lock);
  692. vdcdc2 = i2c_smbus_read_byte_data(&the_tps->client, TPS_VDCDC2);
  693. vdcdc2 &= ~(1 << 1);
  694. if (value)
  695. vdcdc2 |= (1 << 1);
  696. status = i2c_smbus_write_byte_data(&the_tps->client,
  697. TPS_VDCDC2, vdcdc2);
  698. pr_debug("%s: vibrator %s\n", DRIVER_NAME, value ? "on" : "off");
  699. mutex_unlock(&the_tps->lock);
  700. return status;
  701. }
  702. EXPORT_SYMBOL(tps65010_set_vib);
  703. /*-------------------------------------------------------------------------*/
  704. /* tps65010_set_low_pwr parameter:
  705. * mode: ON or OFF
  706. */
  707. int tps65010_set_low_pwr(unsigned mode)
  708. {
  709. int status;
  710. unsigned vdcdc1;
  711. if (!the_tps)
  712. return -ENODEV;
  713. mutex_lock(&the_tps->lock);
  714. pr_debug("%s: %s low_pwr, vdcdc1 0x%02x\n", DRIVER_NAME,
  715. mode ? "enable" : "disable",
  716. i2c_smbus_read_byte_data(&the_tps->client, TPS_VDCDC1));
  717. vdcdc1 = i2c_smbus_read_byte_data(&the_tps->client, TPS_VDCDC1);
  718. switch (mode) {
  719. case OFF:
  720. vdcdc1 &= ~TPS_ENABLE_LP; /* disable ENABLE_LP bit */
  721. break;
  722. /* case ON: */
  723. default:
  724. vdcdc1 |= TPS_ENABLE_LP; /* enable ENABLE_LP bit */
  725. break;
  726. }
  727. status = i2c_smbus_write_byte_data(&the_tps->client,
  728. TPS_VDCDC1, vdcdc1);
  729. if (status != 0)
  730. printk(KERN_ERR "%s: Failed to write vdcdc1 register\n",
  731. DRIVER_NAME);
  732. else
  733. pr_debug("%s: vdcdc1 0x%02x\n", DRIVER_NAME,
  734. i2c_smbus_read_byte_data(&the_tps->client, TPS_VDCDC1));
  735. mutex_unlock(&the_tps->lock);
  736. return status;
  737. }
  738. EXPORT_SYMBOL(tps65010_set_low_pwr);
  739. /*-------------------------------------------------------------------------*/
  740. /* tps65010_config_vregs1 parameter:
  741. * value to be written to VREGS1 register
  742. * Note: The complete register is written, set all bits you need
  743. */
  744. int tps65010_config_vregs1(unsigned value)
  745. {
  746. int status;
  747. if (!the_tps)
  748. return -ENODEV;
  749. mutex_lock(&the_tps->lock);
  750. pr_debug("%s: vregs1 0x%02x\n", DRIVER_NAME,
  751. i2c_smbus_read_byte_data(&the_tps->client, TPS_VREGS1));
  752. status = i2c_smbus_write_byte_data(&the_tps->client,
  753. TPS_VREGS1, value);
  754. if (status != 0)
  755. printk(KERN_ERR "%s: Failed to write vregs1 register\n",
  756. DRIVER_NAME);
  757. else
  758. pr_debug("%s: vregs1 0x%02x\n", DRIVER_NAME,
  759. i2c_smbus_read_byte_data(&the_tps->client, TPS_VREGS1));
  760. mutex_unlock(&the_tps->lock);
  761. return status;
  762. }
  763. EXPORT_SYMBOL(tps65010_config_vregs1);
  764. /*-------------------------------------------------------------------------*/
  765. /* tps65013_set_low_pwr parameter:
  766. * mode: ON or OFF
  767. */
  768. /* FIXME: Assumes AC or USB power is present. Setting AUA bit is not
  769. required if power supply is through a battery */
  770. int tps65013_set_low_pwr(unsigned mode)
  771. {
  772. int status;
  773. unsigned vdcdc1, chgconfig;
  774. if (!the_tps || the_tps->por)
  775. return -ENODEV;
  776. mutex_lock(&the_tps->lock);
  777. pr_debug("%s: %s low_pwr, chgconfig 0x%02x vdcdc1 0x%02x\n",
  778. DRIVER_NAME,
  779. mode ? "enable" : "disable",
  780. i2c_smbus_read_byte_data(&the_tps->client, TPS_CHGCONFIG),
  781. i2c_smbus_read_byte_data(&the_tps->client, TPS_VDCDC1));
  782. chgconfig = i2c_smbus_read_byte_data(&the_tps->client, TPS_CHGCONFIG);
  783. vdcdc1 = i2c_smbus_read_byte_data(&the_tps->client, TPS_VDCDC1);
  784. switch (mode) {
  785. case OFF:
  786. chgconfig &= ~TPS65013_AUA; /* disable AUA bit */
  787. vdcdc1 &= ~TPS_ENABLE_LP; /* disable ENABLE_LP bit */
  788. break;
  789. /* case ON: */
  790. default:
  791. chgconfig |= TPS65013_AUA; /* enable AUA bit */
  792. vdcdc1 |= TPS_ENABLE_LP; /* enable ENABLE_LP bit */
  793. break;
  794. }
  795. status = i2c_smbus_write_byte_data(&the_tps->client,
  796. TPS_CHGCONFIG, chgconfig);
  797. if (status != 0) {
  798. printk(KERN_ERR "%s: Failed to write chconfig register\n",
  799. DRIVER_NAME);
  800. mutex_unlock(&the_tps->lock);
  801. return status;
  802. }
  803. chgconfig = i2c_smbus_read_byte_data(&the_tps->client, TPS_CHGCONFIG);
  804. the_tps->chgconf = chgconfig;
  805. show_chgconfig(0, "chgconf", chgconfig);
  806. status = i2c_smbus_write_byte_data(&the_tps->client,
  807. TPS_VDCDC1, vdcdc1);
  808. if (status != 0)
  809. printk(KERN_ERR "%s: Failed to write vdcdc1 register\n",
  810. DRIVER_NAME);
  811. else
  812. pr_debug("%s: vdcdc1 0x%02x\n", DRIVER_NAME,
  813. i2c_smbus_read_byte_data(&the_tps->client, TPS_VDCDC1));
  814. mutex_unlock(&the_tps->lock);
  815. return status;
  816. }
  817. EXPORT_SYMBOL(tps65013_set_low_pwr);
  818. /*-------------------------------------------------------------------------*/
  819. static int __init tps_init(void)
  820. {
  821. u32 tries = 3;
  822. int status = -ENODEV;
  823. printk(KERN_INFO "%s: version %s\n", DRIVER_NAME, DRIVER_VERSION);
  824. /* some boards have startup glitches */
  825. while (tries--) {
  826. status = i2c_add_driver(&tps65010_driver);
  827. if (the_tps)
  828. break;
  829. i2c_del_driver(&tps65010_driver);
  830. if (!tries) {
  831. printk(KERN_ERR "%s: no chip?\n", DRIVER_NAME);
  832. return -ENODEV;
  833. }
  834. pr_debug("%s: re-probe ...\n", DRIVER_NAME);
  835. msleep(10);
  836. }
  837. #ifdef CONFIG_ARM
  838. if (machine_is_omap_osk()) {
  839. // FIXME: More should be placed in the initialization code
  840. // of the submodules (DSP, ethernet, power management,
  841. // board-osk.c). Careful: I2C is initialized "late".
  842. /* Let LED1 (D9) blink */
  843. tps65010_set_led(LED1, BLINK);
  844. /* Disable LED 2 (D2) */
  845. tps65010_set_led(LED2, OFF);
  846. /* Set GPIO 1 HIGH to disable VBUS power supply;
  847. * OHCI driver powers it up/down as needed.
  848. */
  849. tps65010_set_gpio_out_value(GPIO1, HIGH);
  850. /* Set GPIO 2 low to turn on LED D3 */
  851. tps65010_set_gpio_out_value(GPIO2, HIGH);
  852. /* Set GPIO 3 low to take ethernet out of reset */
  853. tps65010_set_gpio_out_value(GPIO3, LOW);
  854. /* gpio4 for VDD_DSP */
  855. /* Enable LOW_PWR */
  856. tps65010_set_low_pwr(ON);
  857. /* Switch VLDO2 to 3.0V for AIC23 */
  858. tps65010_config_vregs1(TPS_LDO2_ENABLE | TPS_VLDO2_3_0V | TPS_LDO1_ENABLE);
  859. } else if (machine_is_omap_h2()) {
  860. /* gpio3 for SD, gpio4 for VDD_DSP */
  861. /* Enable LOW_PWR */
  862. tps65010_set_low_pwr(ON);
  863. } else if (machine_is_omap_h3()) {
  864. /* gpio4 for SD, gpio3 for VDD_DSP */
  865. #ifdef CONFIG_PM
  866. /* Enable LOW_PWR */
  867. tps65013_set_low_pwr(ON);
  868. #endif
  869. }
  870. #endif
  871. return status;
  872. }
  873. /* NOTE: this MUST be initialized before the other parts of the system
  874. * that rely on it ... but after the i2c bus on which this relies.
  875. * That is, much earlier than on PC-type systems, which don't often use
  876. * I2C as a core system bus.
  877. */
  878. subsys_initcall(tps_init);
  879. static void __exit tps_exit(void)
  880. {
  881. i2c_del_driver(&tps65010_driver);
  882. }
  883. module_exit(tps_exit);