tps65010.c 28 KB

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