tps65010.c 27 KB

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