tps65010.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100
  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/i2c/tps65010.h>
  34. #include <asm/gpio.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. (void) schedule_delayed_work(&tps->work, POWER_POLL_DELAY);
  203. /* VMAIN voltage, enable lowpower, etc */
  204. value = i2c_smbus_read_byte_data(tps->client, TPS_VDCDC1);
  205. seq_printf(s, "vdcdc1 %02x\n", value);
  206. /* VCORE voltage, vibrator on/off */
  207. value = i2c_smbus_read_byte_data(tps->client, TPS_VDCDC2);
  208. seq_printf(s, "vdcdc2 %02x\n", value);
  209. /* both LD0s, and their lowpower behavior */
  210. value = i2c_smbus_read_byte_data(tps->client, TPS_VREGS1);
  211. seq_printf(s, "vregs1 %02x\n\n", value);
  212. /* LEDs and GPIOs */
  213. value = i2c_smbus_read_byte_data(tps->client, TPS_LED1_ON);
  214. v2 = i2c_smbus_read_byte_data(tps->client, TPS_LED1_PER);
  215. seq_printf(s, "led1 %s, on=%02x, per=%02x, %d/%d msec\n",
  216. (value & 0x80)
  217. ? ((v2 & 0x80) ? "on" : "off")
  218. : ((v2 & 0x80) ? "blink" : "(nPG)"),
  219. value, v2,
  220. (value & 0x7f) * 10, (v2 & 0x7f) * 100);
  221. value = i2c_smbus_read_byte_data(tps->client, TPS_LED2_ON);
  222. v2 = i2c_smbus_read_byte_data(tps->client, TPS_LED2_PER);
  223. seq_printf(s, "led2 %s, on=%02x, per=%02x, %d/%d msec\n",
  224. (value & 0x80)
  225. ? ((v2 & 0x80) ? "on" : "off")
  226. : ((v2 & 0x80) ? "blink" : "off"),
  227. value, v2,
  228. (value & 0x7f) * 10, (v2 & 0x7f) * 100);
  229. value = i2c_smbus_read_byte_data(tps->client, TPS_DEFGPIO);
  230. v2 = i2c_smbus_read_byte_data(tps->client, TPS_MASK3);
  231. seq_printf(s, "defgpio %02x mask3 %02x\n", value, v2);
  232. for (i = 0; i < 4; i++) {
  233. if (value & (1 << (4 + i)))
  234. seq_printf(s, " gpio%d-out %s\n", i + 1,
  235. (value & (1 << i)) ? "low" : "hi ");
  236. else
  237. seq_printf(s, " gpio%d-in %s %s %s\n", i + 1,
  238. (value & (1 << i)) ? "hi " : "low",
  239. (v2 & (1 << i)) ? "no-irq" : "irq",
  240. (v2 & (1 << (4 + i))) ? "rising" : "falling");
  241. }
  242. mutex_unlock(&tps->lock);
  243. return 0;
  244. }
  245. static int dbg_tps_open(struct inode *inode, struct file *file)
  246. {
  247. return single_open(file, dbg_show, inode->i_private);
  248. }
  249. static const struct file_operations debug_fops = {
  250. .open = dbg_tps_open,
  251. .read = seq_read,
  252. .llseek = seq_lseek,
  253. .release = single_release,
  254. };
  255. #define DEBUG_FOPS &debug_fops
  256. #else
  257. #define DEBUG_FOPS NULL
  258. #endif
  259. /*-------------------------------------------------------------------------*/
  260. /* handle IRQS in a task context, so we can use I2C calls */
  261. static void tps65010_interrupt(struct tps65010 *tps)
  262. {
  263. u8 tmp = 0, mask, poll;
  264. /* IRQs won't trigger for certain events, but we can get
  265. * others by polling (normally, with external power applied).
  266. */
  267. poll = 0;
  268. /* regstatus irqs */
  269. if (tps->nmask2) {
  270. tmp = i2c_smbus_read_byte_data(tps->client, TPS_REGSTATUS);
  271. mask = tmp ^ tps->regstatus;
  272. tps->regstatus = tmp;
  273. mask &= tps->nmask2;
  274. } else
  275. mask = 0;
  276. if (mask) {
  277. tps->regstatus = tmp;
  278. /* may need to shut something down ... */
  279. /* "off" usually means deep sleep */
  280. if (tmp & TPS_REG_ONOFF) {
  281. pr_info("%s: power off button\n", DRIVER_NAME);
  282. #if 0
  283. /* REVISIT: this might need its own workqueue
  284. * plus tweaks including deadlock avoidance ...
  285. * also needs to get error handling and probably
  286. * an #ifdef CONFIG_HIBERNATION
  287. */
  288. hibernate();
  289. #endif
  290. poll = 1;
  291. }
  292. }
  293. /* chgstatus irqs */
  294. if (tps->nmask1) {
  295. tmp = i2c_smbus_read_byte_data(tps->client, TPS_CHGSTATUS);
  296. mask = tmp ^ tps->chgstatus;
  297. tps->chgstatus = tmp;
  298. mask &= tps->nmask1;
  299. } else
  300. mask = 0;
  301. if (mask) {
  302. unsigned charging = 0;
  303. show_chgstatus("chg/irq", tmp);
  304. if (tmp & (TPS_CHG_USB|TPS_CHG_AC))
  305. show_chgconfig(tps->por, "conf", tps->chgconf);
  306. /* Unless it was turned off or disabled, we charge any
  307. * battery whenever there's power available for it
  308. * and the charger hasn't been disabled.
  309. */
  310. if (!(tps->chgstatus & ~(TPS_CHG_USB|TPS_CHG_AC))
  311. && (tps->chgstatus & (TPS_CHG_USB|TPS_CHG_AC))
  312. && (tps->chgconf & TPS_CHARGE_ENABLE)
  313. ) {
  314. if (tps->chgstatus & TPS_CHG_USB) {
  315. /* VBUS options are readonly until reconnect */
  316. if (mask & TPS_CHG_USB)
  317. set_bit(FLAG_VBUS_CHANGED, &tps->flags);
  318. charging = 1;
  319. } else if (tps->chgstatus & TPS_CHG_AC)
  320. charging = 1;
  321. }
  322. if (charging != tps->charging) {
  323. tps->charging = charging;
  324. pr_info("%s: battery %scharging\n",
  325. DRIVER_NAME, charging ? "" :
  326. ((tps->chgstatus & (TPS_CHG_USB|TPS_CHG_AC))
  327. ? "NOT " : "dis"));
  328. }
  329. }
  330. /* always poll to detect (a) power removal, without tps65013
  331. * NO_CHG IRQ; or (b) restart of charging after stop.
  332. */
  333. if ((tps->model != TPS65013 || !tps->charging)
  334. && (tps->chgstatus & (TPS_CHG_USB|TPS_CHG_AC)))
  335. poll = 1;
  336. if (poll)
  337. (void) schedule_delayed_work(&tps->work, POWER_POLL_DELAY);
  338. /* also potentially gpio-in rise or fall */
  339. }
  340. /* handle IRQs and polling using keventd for now */
  341. static void tps65010_work(struct work_struct *work)
  342. {
  343. struct tps65010 *tps;
  344. tps = container_of(work, struct tps65010, work.work);
  345. mutex_lock(&tps->lock);
  346. tps65010_interrupt(tps);
  347. if (test_and_clear_bit(FLAG_VBUS_CHANGED, &tps->flags)) {
  348. int status;
  349. u8 chgconfig, tmp;
  350. chgconfig = i2c_smbus_read_byte_data(tps->client,
  351. TPS_CHGCONFIG);
  352. chgconfig &= ~(TPS_VBUS_500MA | TPS_VBUS_CHARGING);
  353. if (tps->vbus == 500)
  354. chgconfig |= TPS_VBUS_500MA | TPS_VBUS_CHARGING;
  355. else if (tps->vbus >= 100)
  356. chgconfig |= TPS_VBUS_CHARGING;
  357. status = i2c_smbus_write_byte_data(tps->client,
  358. TPS_CHGCONFIG, chgconfig);
  359. /* vbus update fails unless VBUS is connected! */
  360. tmp = i2c_smbus_read_byte_data(tps->client, TPS_CHGCONFIG);
  361. tps->chgconf = tmp;
  362. show_chgconfig(tps->por, "update vbus", tmp);
  363. }
  364. if (test_and_clear_bit(FLAG_IRQ_ENABLE, &tps->flags))
  365. enable_irq(tps->client->irq);
  366. mutex_unlock(&tps->lock);
  367. }
  368. static irqreturn_t tps65010_irq(int irq, void *_tps)
  369. {
  370. struct tps65010 *tps = _tps;
  371. disable_irq_nosync(irq);
  372. set_bit(FLAG_IRQ_ENABLE, &tps->flags);
  373. (void) schedule_work(&tps->work.work);
  374. return IRQ_HANDLED;
  375. }
  376. /*-------------------------------------------------------------------------*/
  377. /* offsets 0..3 == GPIO1..GPIO4
  378. * offsets 4..5 == LED1/nPG, LED2 (we set one of the non-BLINK modes)
  379. * offset 6 == vibrator motor driver
  380. */
  381. static void
  382. tps65010_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  383. {
  384. if (offset < 4)
  385. tps65010_set_gpio_out_value(offset + 1, value);
  386. else if (offset < 6)
  387. tps65010_set_led(offset - 3, value ? ON : OFF);
  388. else
  389. tps65010_set_vib(value);
  390. }
  391. static int
  392. tps65010_output(struct gpio_chip *chip, unsigned offset, int value)
  393. {
  394. /* GPIOs may be input-only */
  395. if (offset < 4) {
  396. struct tps65010 *tps;
  397. tps = container_of(chip, struct tps65010, chip);
  398. if (!(tps->outmask & (1 << offset)))
  399. return -EINVAL;
  400. tps65010_set_gpio_out_value(offset + 1, value);
  401. } else if (offset < 6)
  402. tps65010_set_led(offset - 3, value ? ON : OFF);
  403. else
  404. tps65010_set_vib(value);
  405. return 0;
  406. }
  407. static int tps65010_gpio_get(struct gpio_chip *chip, unsigned offset)
  408. {
  409. int value;
  410. struct tps65010 *tps;
  411. tps = container_of(chip, struct tps65010, chip);
  412. if (offset < 4) {
  413. value = i2c_smbus_read_byte_data(tps->client, TPS_DEFGPIO);
  414. if (value < 0)
  415. return 0;
  416. if (value & (1 << (offset + 4))) /* output */
  417. return !(value & (1 << offset));
  418. else /* input */
  419. return (value & (1 << offset));
  420. }
  421. /* REVISIT we *could* report LED1/nPG and LED2 state ... */
  422. return 0;
  423. }
  424. /*-------------------------------------------------------------------------*/
  425. static struct tps65010 *the_tps;
  426. static int __exit tps65010_remove(struct i2c_client *client)
  427. {
  428. struct tps65010 *tps = i2c_get_clientdata(client);
  429. struct tps65010_board *board = client->dev.platform_data;
  430. if (board && board->teardown) {
  431. int status = board->teardown(client, board->context);
  432. if (status < 0)
  433. dev_dbg(&client->dev, "board %s %s err %d\n",
  434. "teardown", client->name, status);
  435. }
  436. if (client->irq > 0)
  437. free_irq(client->irq, tps);
  438. cancel_delayed_work(&tps->work);
  439. flush_scheduled_work();
  440. debugfs_remove(tps->file);
  441. kfree(tps);
  442. i2c_set_clientdata(client, NULL);
  443. the_tps = NULL;
  444. return 0;
  445. }
  446. static int tps65010_probe(struct i2c_client *client,
  447. const struct i2c_device_id *id)
  448. {
  449. struct tps65010 *tps;
  450. int status;
  451. struct tps65010_board *board = client->dev.platform_data;
  452. if (the_tps) {
  453. dev_dbg(&client->dev, "only one tps6501x chip allowed\n");
  454. return -ENODEV;
  455. }
  456. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  457. return -EINVAL;
  458. tps = kzalloc(sizeof *tps, GFP_KERNEL);
  459. if (!tps)
  460. return -ENOMEM;
  461. mutex_init(&tps->lock);
  462. INIT_DELAYED_WORK(&tps->work, tps65010_work);
  463. tps->client = client;
  464. tps->model = id->driver_data;
  465. /* the IRQ is active low, but many gpio lines can't support that
  466. * so this driver uses falling-edge triggers instead.
  467. */
  468. if (client->irq > 0) {
  469. status = request_irq(client->irq, tps65010_irq,
  470. IRQF_SAMPLE_RANDOM | IRQF_TRIGGER_FALLING,
  471. DRIVER_NAME, tps);
  472. if (status < 0) {
  473. dev_dbg(&client->dev, "can't get IRQ %d, err %d\n",
  474. client->irq, status);
  475. goto fail1;
  476. }
  477. /* annoying race here, ideally we'd have an option
  478. * to claim the irq now and enable it later.
  479. * FIXME genirq IRQF_NOAUTOEN now solves that ...
  480. */
  481. disable_irq(client->irq);
  482. set_bit(FLAG_IRQ_ENABLE, &tps->flags);
  483. } else
  484. dev_warn(&client->dev, "IRQ not configured!\n");
  485. switch (tps->model) {
  486. case TPS65010:
  487. case TPS65012:
  488. tps->por = 1;
  489. break;
  490. /* else CHGCONFIG.POR is replaced by AUA, enabling a WAIT mode */
  491. }
  492. tps->chgconf = i2c_smbus_read_byte_data(client, TPS_CHGCONFIG);
  493. show_chgconfig(tps->por, "conf/init", tps->chgconf);
  494. show_chgstatus("chg/init",
  495. i2c_smbus_read_byte_data(client, TPS_CHGSTATUS));
  496. show_regstatus("reg/init",
  497. i2c_smbus_read_byte_data(client, TPS_REGSTATUS));
  498. pr_debug("%s: vdcdc1 0x%02x, vdcdc2 %02x, vregs1 %02x\n", DRIVER_NAME,
  499. i2c_smbus_read_byte_data(client, TPS_VDCDC1),
  500. i2c_smbus_read_byte_data(client, TPS_VDCDC2),
  501. i2c_smbus_read_byte_data(client, TPS_VREGS1));
  502. pr_debug("%s: defgpio 0x%02x, mask3 0x%02x\n", DRIVER_NAME,
  503. i2c_smbus_read_byte_data(client, TPS_DEFGPIO),
  504. i2c_smbus_read_byte_data(client, TPS_MASK3));
  505. i2c_set_clientdata(client, tps);
  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(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(client, TPS_MASK2, ~tps->nmask2);
  524. (void) i2c_smbus_write_byte_data(client, TPS_MASK3, 0x0f
  525. | i2c_smbus_read_byte_data(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. /* optionally register GPIOs */
  530. if (board && board->base != 0) {
  531. tps->outmask = board->outmask;
  532. tps->chip.label = client->name;
  533. tps->chip.dev = &client->dev;
  534. tps->chip.owner = THIS_MODULE;
  535. tps->chip.set = tps65010_gpio_set;
  536. tps->chip.direction_output = tps65010_output;
  537. /* NOTE: only partial support for inputs; nyet IRQs */
  538. tps->chip.get = tps65010_gpio_get;
  539. tps->chip.base = board->base;
  540. tps->chip.ngpio = 7;
  541. tps->chip.can_sleep = 1;
  542. status = gpiochip_add(&tps->chip);
  543. if (status < 0)
  544. dev_err(&client->dev, "can't add gpiochip, err %d\n",
  545. status);
  546. else if (board->setup) {
  547. status = board->setup(client, board->context);
  548. if (status < 0) {
  549. dev_dbg(&client->dev,
  550. "board %s %s err %d\n",
  551. "setup", client->name, status);
  552. status = 0;
  553. }
  554. }
  555. }
  556. return 0;
  557. fail1:
  558. kfree(tps);
  559. return status;
  560. }
  561. static const struct i2c_device_id tps65010_id[] = {
  562. { "tps65010", TPS65010 },
  563. { "tps65011", TPS65011 },
  564. { "tps65012", TPS65012 },
  565. { "tps65013", TPS65013 },
  566. { "tps65014", TPS65011 }, /* tps65011 charging at 6.5V max */
  567. { }
  568. };
  569. MODULE_DEVICE_TABLE(i2c, tps65010_id);
  570. static struct i2c_driver tps65010_driver = {
  571. .driver = {
  572. .name = "tps65010",
  573. },
  574. .probe = tps65010_probe,
  575. .remove = __exit_p(tps65010_remove),
  576. .id_table = tps65010_id,
  577. };
  578. /*-------------------------------------------------------------------------*/
  579. /* Draw from VBUS:
  580. * 0 mA -- DON'T DRAW (might supply power instead)
  581. * 100 mA -- usb unit load (slowest charge rate)
  582. * 500 mA -- usb high power (fast battery charge)
  583. */
  584. int tps65010_set_vbus_draw(unsigned mA)
  585. {
  586. unsigned long flags;
  587. if (!the_tps)
  588. return -ENODEV;
  589. /* assumes non-SMP */
  590. local_irq_save(flags);
  591. if (mA >= 500)
  592. mA = 500;
  593. else if (mA >= 100)
  594. mA = 100;
  595. else
  596. mA = 0;
  597. the_tps->vbus = mA;
  598. if ((the_tps->chgstatus & TPS_CHG_USB)
  599. && test_and_set_bit(
  600. FLAG_VBUS_CHANGED, &the_tps->flags)) {
  601. /* gadget drivers call this in_irq() */
  602. (void) schedule_work(&the_tps->work.work);
  603. }
  604. local_irq_restore(flags);
  605. return 0;
  606. }
  607. EXPORT_SYMBOL(tps65010_set_vbus_draw);
  608. /*-------------------------------------------------------------------------*/
  609. /* tps65010_set_gpio_out_value parameter:
  610. * gpio: GPIO1, GPIO2, GPIO3 or GPIO4
  611. * value: LOW or HIGH
  612. */
  613. int tps65010_set_gpio_out_value(unsigned gpio, unsigned value)
  614. {
  615. int status;
  616. unsigned defgpio;
  617. if (!the_tps)
  618. return -ENODEV;
  619. if ((gpio < GPIO1) || (gpio > GPIO4))
  620. return -EINVAL;
  621. mutex_lock(&the_tps->lock);
  622. defgpio = i2c_smbus_read_byte_data(the_tps->client, TPS_DEFGPIO);
  623. /* Configure GPIO for output */
  624. defgpio |= 1 << (gpio + 3);
  625. /* Writing 1 forces a logic 0 on that GPIO and vice versa */
  626. switch (value) {
  627. case LOW:
  628. defgpio |= 1 << (gpio - 1); /* set GPIO low by writing 1 */
  629. break;
  630. /* case HIGH: */
  631. default:
  632. defgpio &= ~(1 << (gpio - 1)); /* set GPIO high by writing 0 */
  633. break;
  634. }
  635. status = i2c_smbus_write_byte_data(the_tps->client,
  636. TPS_DEFGPIO, defgpio);
  637. pr_debug("%s: gpio%dout = %s, defgpio 0x%02x\n", DRIVER_NAME,
  638. gpio, value ? "high" : "low",
  639. i2c_smbus_read_byte_data(the_tps->client, TPS_DEFGPIO));
  640. mutex_unlock(&the_tps->lock);
  641. return status;
  642. }
  643. EXPORT_SYMBOL(tps65010_set_gpio_out_value);
  644. /*-------------------------------------------------------------------------*/
  645. /* tps65010_set_led parameter:
  646. * led: LED1 or LED2
  647. * mode: ON, OFF or BLINK
  648. */
  649. int tps65010_set_led(unsigned led, unsigned mode)
  650. {
  651. int status;
  652. unsigned led_on, led_per, offs;
  653. if (!the_tps)
  654. return -ENODEV;
  655. if (led == LED1)
  656. offs = 0;
  657. else {
  658. offs = 2;
  659. led = LED2;
  660. }
  661. mutex_lock(&the_tps->lock);
  662. pr_debug("%s: led%i_on 0x%02x\n", DRIVER_NAME, led,
  663. i2c_smbus_read_byte_data(the_tps->client,
  664. TPS_LED1_ON + offs));
  665. pr_debug("%s: led%i_per 0x%02x\n", DRIVER_NAME, led,
  666. i2c_smbus_read_byte_data(the_tps->client,
  667. TPS_LED1_PER + offs));
  668. switch (mode) {
  669. case OFF:
  670. led_on = 1 << 7;
  671. led_per = 0 << 7;
  672. break;
  673. case ON:
  674. led_on = 1 << 7;
  675. led_per = 1 << 7;
  676. break;
  677. case BLINK:
  678. led_on = 0x30 | (0 << 7);
  679. led_per = 0x08 | (1 << 7);
  680. break;
  681. default:
  682. printk(KERN_ERR "%s: Wrong mode parameter for set_led()\n",
  683. DRIVER_NAME);
  684. mutex_unlock(&the_tps->lock);
  685. return -EINVAL;
  686. }
  687. status = i2c_smbus_write_byte_data(the_tps->client,
  688. TPS_LED1_ON + offs, led_on);
  689. if (status != 0) {
  690. printk(KERN_ERR "%s: Failed to write led%i_on register\n",
  691. DRIVER_NAME, led);
  692. mutex_unlock(&the_tps->lock);
  693. return status;
  694. }
  695. pr_debug("%s: led%i_on 0x%02x\n", DRIVER_NAME, led,
  696. i2c_smbus_read_byte_data(the_tps->client, TPS_LED1_ON + offs));
  697. status = i2c_smbus_write_byte_data(the_tps->client,
  698. TPS_LED1_PER + offs, led_per);
  699. if (status != 0) {
  700. printk(KERN_ERR "%s: Failed to write led%i_per register\n",
  701. DRIVER_NAME, led);
  702. mutex_unlock(&the_tps->lock);
  703. return status;
  704. }
  705. pr_debug("%s: led%i_per 0x%02x\n", DRIVER_NAME, led,
  706. i2c_smbus_read_byte_data(the_tps->client,
  707. TPS_LED1_PER + offs));
  708. mutex_unlock(&the_tps->lock);
  709. return status;
  710. }
  711. EXPORT_SYMBOL(tps65010_set_led);
  712. /*-------------------------------------------------------------------------*/
  713. /* tps65010_set_vib parameter:
  714. * value: ON or OFF
  715. */
  716. int tps65010_set_vib(unsigned value)
  717. {
  718. int status;
  719. unsigned vdcdc2;
  720. if (!the_tps)
  721. return -ENODEV;
  722. mutex_lock(&the_tps->lock);
  723. vdcdc2 = i2c_smbus_read_byte_data(the_tps->client, TPS_VDCDC2);
  724. vdcdc2 &= ~(1 << 1);
  725. if (value)
  726. vdcdc2 |= (1 << 1);
  727. status = i2c_smbus_write_byte_data(the_tps->client,
  728. TPS_VDCDC2, vdcdc2);
  729. pr_debug("%s: vibrator %s\n", DRIVER_NAME, value ? "on" : "off");
  730. mutex_unlock(&the_tps->lock);
  731. return status;
  732. }
  733. EXPORT_SYMBOL(tps65010_set_vib);
  734. /*-------------------------------------------------------------------------*/
  735. /* tps65010_set_low_pwr parameter:
  736. * mode: ON or OFF
  737. */
  738. int tps65010_set_low_pwr(unsigned mode)
  739. {
  740. int status;
  741. unsigned vdcdc1;
  742. if (!the_tps)
  743. return -ENODEV;
  744. mutex_lock(&the_tps->lock);
  745. pr_debug("%s: %s low_pwr, vdcdc1 0x%02x\n", DRIVER_NAME,
  746. mode ? "enable" : "disable",
  747. i2c_smbus_read_byte_data(the_tps->client, TPS_VDCDC1));
  748. vdcdc1 = i2c_smbus_read_byte_data(the_tps->client, TPS_VDCDC1);
  749. switch (mode) {
  750. case OFF:
  751. vdcdc1 &= ~TPS_ENABLE_LP; /* disable ENABLE_LP bit */
  752. break;
  753. /* case ON: */
  754. default:
  755. vdcdc1 |= TPS_ENABLE_LP; /* enable ENABLE_LP bit */
  756. break;
  757. }
  758. status = i2c_smbus_write_byte_data(the_tps->client,
  759. TPS_VDCDC1, vdcdc1);
  760. if (status != 0)
  761. printk(KERN_ERR "%s: Failed to write vdcdc1 register\n",
  762. DRIVER_NAME);
  763. else
  764. pr_debug("%s: vdcdc1 0x%02x\n", DRIVER_NAME,
  765. i2c_smbus_read_byte_data(the_tps->client, TPS_VDCDC1));
  766. mutex_unlock(&the_tps->lock);
  767. return status;
  768. }
  769. EXPORT_SYMBOL(tps65010_set_low_pwr);
  770. /*-------------------------------------------------------------------------*/
  771. /* tps65010_config_vregs1 parameter:
  772. * value to be written to VREGS1 register
  773. * Note: The complete register is written, set all bits you need
  774. */
  775. int tps65010_config_vregs1(unsigned value)
  776. {
  777. int status;
  778. if (!the_tps)
  779. return -ENODEV;
  780. mutex_lock(&the_tps->lock);
  781. pr_debug("%s: vregs1 0x%02x\n", DRIVER_NAME,
  782. i2c_smbus_read_byte_data(the_tps->client, TPS_VREGS1));
  783. status = i2c_smbus_write_byte_data(the_tps->client,
  784. TPS_VREGS1, value);
  785. if (status != 0)
  786. printk(KERN_ERR "%s: Failed to write vregs1 register\n",
  787. DRIVER_NAME);
  788. else
  789. pr_debug("%s: vregs1 0x%02x\n", DRIVER_NAME,
  790. i2c_smbus_read_byte_data(the_tps->client, TPS_VREGS1));
  791. mutex_unlock(&the_tps->lock);
  792. return status;
  793. }
  794. EXPORT_SYMBOL(tps65010_config_vregs1);
  795. int tps65010_config_vdcdc2(unsigned value)
  796. {
  797. struct i2c_client *c;
  798. int status;
  799. if (!the_tps)
  800. return -ENODEV;
  801. c = the_tps->client;
  802. mutex_lock(&the_tps->lock);
  803. pr_debug("%s: vdcdc2 0x%02x\n", DRIVER_NAME,
  804. i2c_smbus_read_byte_data(c, TPS_VDCDC2));
  805. status = i2c_smbus_write_byte_data(c, TPS_VDCDC2, value);
  806. if (status != 0)
  807. printk(KERN_ERR "%s: Failed to write vdcdc2 register\n",
  808. DRIVER_NAME);
  809. else
  810. pr_debug("%s: vregs1 0x%02x\n", DRIVER_NAME,
  811. i2c_smbus_read_byte_data(c, TPS_VDCDC2));
  812. mutex_unlock(&the_tps->lock);
  813. return status;
  814. }
  815. EXPORT_SYMBOL(tps65010_config_vdcdc2);
  816. /*-------------------------------------------------------------------------*/
  817. /* tps65013_set_low_pwr parameter:
  818. * mode: ON or OFF
  819. */
  820. /* FIXME: Assumes AC or USB power is present. Setting AUA bit is not
  821. required if power supply is through a battery */
  822. int tps65013_set_low_pwr(unsigned mode)
  823. {
  824. int status;
  825. unsigned vdcdc1, chgconfig;
  826. if (!the_tps || the_tps->por)
  827. return -ENODEV;
  828. mutex_lock(&the_tps->lock);
  829. pr_debug("%s: %s low_pwr, chgconfig 0x%02x vdcdc1 0x%02x\n",
  830. DRIVER_NAME,
  831. mode ? "enable" : "disable",
  832. i2c_smbus_read_byte_data(the_tps->client, TPS_CHGCONFIG),
  833. i2c_smbus_read_byte_data(the_tps->client, TPS_VDCDC1));
  834. chgconfig = i2c_smbus_read_byte_data(the_tps->client, TPS_CHGCONFIG);
  835. vdcdc1 = i2c_smbus_read_byte_data(the_tps->client, TPS_VDCDC1);
  836. switch (mode) {
  837. case OFF:
  838. chgconfig &= ~TPS65013_AUA; /* disable AUA bit */
  839. vdcdc1 &= ~TPS_ENABLE_LP; /* disable ENABLE_LP bit */
  840. break;
  841. /* case ON: */
  842. default:
  843. chgconfig |= TPS65013_AUA; /* enable AUA bit */
  844. vdcdc1 |= TPS_ENABLE_LP; /* enable ENABLE_LP bit */
  845. break;
  846. }
  847. status = i2c_smbus_write_byte_data(the_tps->client,
  848. TPS_CHGCONFIG, chgconfig);
  849. if (status != 0) {
  850. printk(KERN_ERR "%s: Failed to write chconfig register\n",
  851. DRIVER_NAME);
  852. mutex_unlock(&the_tps->lock);
  853. return status;
  854. }
  855. chgconfig = i2c_smbus_read_byte_data(the_tps->client, TPS_CHGCONFIG);
  856. the_tps->chgconf = chgconfig;
  857. show_chgconfig(0, "chgconf", chgconfig);
  858. status = i2c_smbus_write_byte_data(the_tps->client,
  859. TPS_VDCDC1, vdcdc1);
  860. if (status != 0)
  861. printk(KERN_ERR "%s: Failed to write vdcdc1 register\n",
  862. DRIVER_NAME);
  863. else
  864. pr_debug("%s: vdcdc1 0x%02x\n", DRIVER_NAME,
  865. i2c_smbus_read_byte_data(the_tps->client, TPS_VDCDC1));
  866. mutex_unlock(&the_tps->lock);
  867. return status;
  868. }
  869. EXPORT_SYMBOL(tps65013_set_low_pwr);
  870. /*-------------------------------------------------------------------------*/
  871. static int __init tps_init(void)
  872. {
  873. u32 tries = 3;
  874. int status = -ENODEV;
  875. printk(KERN_INFO "%s: version %s\n", DRIVER_NAME, DRIVER_VERSION);
  876. /* some boards have startup glitches */
  877. while (tries--) {
  878. status = i2c_add_driver(&tps65010_driver);
  879. if (the_tps)
  880. break;
  881. i2c_del_driver(&tps65010_driver);
  882. if (!tries) {
  883. printk(KERN_ERR "%s: no chip?\n", DRIVER_NAME);
  884. return -ENODEV;
  885. }
  886. pr_debug("%s: re-probe ...\n", DRIVER_NAME);
  887. msleep(10);
  888. }
  889. return status;
  890. }
  891. /* NOTE: this MUST be initialized before the other parts of the system
  892. * that rely on it ... but after the i2c bus on which this relies.
  893. * That is, much earlier than on PC-type systems, which don't often use
  894. * I2C as a core system bus.
  895. */
  896. subsys_initcall(tps_init);
  897. static void __exit tps_exit(void)
  898. {
  899. i2c_del_driver(&tps65010_driver);
  900. }
  901. module_exit(tps_exit);