tps65010.c 28 KB

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