pm2301_charger.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259
  1. /*
  2. * Copyright 2012 ST Ericsson.
  3. *
  4. * Power supply driver for ST Ericsson pm2xxx_charger charger
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/init.h>
  11. #include <linux/module.h>
  12. #include <linux/device.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/delay.h>
  15. #include <linux/slab.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/power_supply.h>
  18. #include <linux/regulator/consumer.h>
  19. #include <linux/err.h>
  20. #include <linux/i2c.h>
  21. #include <linux/workqueue.h>
  22. #include <linux/mfd/abx500/ab8500.h>
  23. #include <linux/mfd/abx500/ab8500-bm.h>
  24. #include <linux/mfd/abx500/ux500_chargalg.h>
  25. #include <linux/pm2301_charger.h>
  26. #include <linux/gpio.h>
  27. #include <linux/pm_runtime.h>
  28. #include <linux/pm.h>
  29. #include "pm2301_charger.h"
  30. #define to_pm2xxx_charger_ac_device_info(x) container_of((x), \
  31. struct pm2xxx_charger, ac_chg)
  32. #define SLEEP_MIN 50
  33. #define SLEEP_MAX 100
  34. #define PM2XXX_AUTOSUSPEND_DELAY 500
  35. static int pm2xxx_interrupt_registers[] = {
  36. PM2XXX_REG_INT1,
  37. PM2XXX_REG_INT2,
  38. PM2XXX_REG_INT3,
  39. PM2XXX_REG_INT4,
  40. PM2XXX_REG_INT5,
  41. PM2XXX_REG_INT6,
  42. };
  43. static enum power_supply_property pm2xxx_charger_ac_props[] = {
  44. POWER_SUPPLY_PROP_HEALTH,
  45. POWER_SUPPLY_PROP_PRESENT,
  46. POWER_SUPPLY_PROP_ONLINE,
  47. POWER_SUPPLY_PROP_VOLTAGE_AVG,
  48. };
  49. static int pm2xxx_charger_voltage_map[] = {
  50. 3500,
  51. 3525,
  52. 3550,
  53. 3575,
  54. 3600,
  55. 3625,
  56. 3650,
  57. 3675,
  58. 3700,
  59. 3725,
  60. 3750,
  61. 3775,
  62. 3800,
  63. 3825,
  64. 3850,
  65. 3875,
  66. 3900,
  67. 3925,
  68. 3950,
  69. 3975,
  70. 4000,
  71. 4025,
  72. 4050,
  73. 4075,
  74. 4100,
  75. 4125,
  76. 4150,
  77. 4175,
  78. 4200,
  79. 4225,
  80. 4250,
  81. 4275,
  82. 4300,
  83. };
  84. static int pm2xxx_charger_current_map[] = {
  85. 200,
  86. 200,
  87. 400,
  88. 600,
  89. 800,
  90. 1000,
  91. 1200,
  92. 1400,
  93. 1600,
  94. 1800,
  95. 2000,
  96. 2200,
  97. 2400,
  98. 2600,
  99. 2800,
  100. 3000,
  101. };
  102. static const struct i2c_device_id pm2xxx_ident[] = {
  103. { "pm2301", 0 },
  104. { }
  105. };
  106. static void set_lpn_pin(struct pm2xxx_charger *pm2)
  107. {
  108. if (!pm2->ac.charger_connected && gpio_is_valid(pm2->lpn_pin)) {
  109. gpio_set_value(pm2->lpn_pin, 1);
  110. usleep_range(SLEEP_MIN, SLEEP_MAX);
  111. }
  112. }
  113. static void clear_lpn_pin(struct pm2xxx_charger *pm2)
  114. {
  115. if (!pm2->ac.charger_connected && gpio_is_valid(pm2->lpn_pin))
  116. gpio_set_value(pm2->lpn_pin, 0);
  117. }
  118. static int pm2xxx_reg_read(struct pm2xxx_charger *pm2, int reg, u8 *val)
  119. {
  120. int ret;
  121. /* wake up the device */
  122. pm_runtime_get_sync(pm2->dev);
  123. ret = i2c_smbus_read_i2c_block_data(pm2->config.pm2xxx_i2c, reg,
  124. 1, val);
  125. if (ret < 0)
  126. dev_err(pm2->dev, "Error reading register at 0x%x\n", reg);
  127. else
  128. ret = 0;
  129. pm_runtime_put_sync(pm2->dev);
  130. return ret;
  131. }
  132. static int pm2xxx_reg_write(struct pm2xxx_charger *pm2, int reg, u8 val)
  133. {
  134. int ret;
  135. /* wake up the device */
  136. pm_runtime_get_sync(pm2->dev);
  137. ret = i2c_smbus_write_i2c_block_data(pm2->config.pm2xxx_i2c, reg,
  138. 1, &val);
  139. if (ret < 0)
  140. dev_err(pm2->dev, "Error writing register at 0x%x\n", reg);
  141. else
  142. ret = 0;
  143. pm_runtime_put_sync(pm2->dev);
  144. return ret;
  145. }
  146. static int pm2xxx_charging_enable_mngt(struct pm2xxx_charger *pm2)
  147. {
  148. int ret;
  149. /* Enable charging */
  150. ret = pm2xxx_reg_write(pm2, PM2XXX_BATT_CTRL_REG2,
  151. (PM2XXX_CH_AUTO_RESUME_EN | PM2XXX_CHARGER_ENA));
  152. return ret;
  153. }
  154. static int pm2xxx_charging_disable_mngt(struct pm2xxx_charger *pm2)
  155. {
  156. int ret;
  157. /* Disable SW EOC ctrl */
  158. ret = pm2xxx_reg_write(pm2, PM2XXX_SW_CTRL_REG, PM2XXX_SWCTRL_HW);
  159. if (ret < 0) {
  160. dev_err(pm2->dev, "%s pm2xxx write failed\n", __func__);
  161. return ret;
  162. }
  163. /* Disable charging */
  164. ret = pm2xxx_reg_write(pm2, PM2XXX_BATT_CTRL_REG2,
  165. (PM2XXX_CH_AUTO_RESUME_DIS | PM2XXX_CHARGER_DIS));
  166. if (ret < 0) {
  167. dev_err(pm2->dev, "%s pm2xxx write failed\n", __func__);
  168. return ret;
  169. }
  170. return 0;
  171. }
  172. static int pm2xxx_charger_batt_therm_mngt(struct pm2xxx_charger *pm2, int val)
  173. {
  174. queue_work(pm2->charger_wq, &pm2->check_main_thermal_prot_work);
  175. return 0;
  176. }
  177. static int pm2xxx_charger_die_therm_mngt(struct pm2xxx_charger *pm2, int val)
  178. {
  179. queue_work(pm2->charger_wq, &pm2->check_main_thermal_prot_work);
  180. return 0;
  181. }
  182. static int pm2xxx_charger_ovv_mngt(struct pm2xxx_charger *pm2, int val)
  183. {
  184. dev_err(pm2->dev, "Overvoltage detected\n");
  185. pm2->flags.ovv = true;
  186. power_supply_changed(pm2->ac_chg.psy);
  187. /* Schedule a new HW failure check */
  188. queue_delayed_work(pm2->charger_wq, &pm2->check_hw_failure_work, 0);
  189. return 0;
  190. }
  191. static int pm2xxx_charger_wd_exp_mngt(struct pm2xxx_charger *pm2, int val)
  192. {
  193. dev_dbg(pm2->dev , "20 minutes watchdog expired\n");
  194. pm2->ac.wd_expired = true;
  195. power_supply_changed(pm2->ac_chg.psy);
  196. return 0;
  197. }
  198. static int pm2xxx_charger_vbat_lsig_mngt(struct pm2xxx_charger *pm2, int val)
  199. {
  200. int ret;
  201. switch (val) {
  202. case PM2XXX_INT1_ITVBATLOWR:
  203. dev_dbg(pm2->dev, "VBAT grows above VBAT_LOW level\n");
  204. /* Enable SW EOC ctrl */
  205. ret = pm2xxx_reg_write(pm2, PM2XXX_SW_CTRL_REG,
  206. PM2XXX_SWCTRL_SW);
  207. if (ret < 0) {
  208. dev_err(pm2->dev, "%s pm2xxx write failed\n", __func__);
  209. return ret;
  210. }
  211. break;
  212. case PM2XXX_INT1_ITVBATLOWF:
  213. dev_dbg(pm2->dev, "VBAT drops below VBAT_LOW level\n");
  214. /* Disable SW EOC ctrl */
  215. ret = pm2xxx_reg_write(pm2, PM2XXX_SW_CTRL_REG,
  216. PM2XXX_SWCTRL_HW);
  217. if (ret < 0) {
  218. dev_err(pm2->dev, "%s pm2xxx write failed\n", __func__);
  219. return ret;
  220. }
  221. break;
  222. default:
  223. dev_err(pm2->dev, "Unknown VBAT level\n");
  224. }
  225. return 0;
  226. }
  227. static int pm2xxx_charger_bat_disc_mngt(struct pm2xxx_charger *pm2, int val)
  228. {
  229. dev_dbg(pm2->dev, "battery disconnected\n");
  230. return 0;
  231. }
  232. static int pm2xxx_charger_detection(struct pm2xxx_charger *pm2, u8 *val)
  233. {
  234. int ret;
  235. ret = pm2xxx_reg_read(pm2, PM2XXX_SRCE_REG_INT2, val);
  236. if (ret < 0) {
  237. dev_err(pm2->dev, "Charger detection failed\n");
  238. goto out;
  239. }
  240. *val &= (PM2XXX_INT2_S_ITVPWR1PLUG | PM2XXX_INT2_S_ITVPWR2PLUG);
  241. out:
  242. return ret;
  243. }
  244. static int pm2xxx_charger_itv_pwr_plug_mngt(struct pm2xxx_charger *pm2, int val)
  245. {
  246. int ret;
  247. u8 read_val;
  248. /*
  249. * Since we can't be sure that the events are received
  250. * synchronously, we have the check if the main charger is
  251. * connected by reading the interrupt source register.
  252. */
  253. ret = pm2xxx_charger_detection(pm2, &read_val);
  254. if ((ret == 0) && read_val) {
  255. pm2->ac.charger_connected = 1;
  256. pm2->ac_conn = true;
  257. queue_work(pm2->charger_wq, &pm2->ac_work);
  258. }
  259. return ret;
  260. }
  261. static int pm2xxx_charger_itv_pwr_unplug_mngt(struct pm2xxx_charger *pm2,
  262. int val)
  263. {
  264. pm2->ac.charger_connected = 0;
  265. queue_work(pm2->charger_wq, &pm2->ac_work);
  266. return 0;
  267. }
  268. static int pm2_int_reg0(void *pm2_data, int val)
  269. {
  270. struct pm2xxx_charger *pm2 = pm2_data;
  271. int ret = 0;
  272. if (val & PM2XXX_INT1_ITVBATLOWR) {
  273. ret = pm2xxx_charger_vbat_lsig_mngt(pm2,
  274. PM2XXX_INT1_ITVBATLOWR);
  275. if (ret < 0)
  276. goto out;
  277. }
  278. if (val & PM2XXX_INT1_ITVBATLOWF) {
  279. ret = pm2xxx_charger_vbat_lsig_mngt(pm2,
  280. PM2XXX_INT1_ITVBATLOWF);
  281. if (ret < 0)
  282. goto out;
  283. }
  284. if (val & PM2XXX_INT1_ITVBATDISCONNECT) {
  285. ret = pm2xxx_charger_bat_disc_mngt(pm2,
  286. PM2XXX_INT1_ITVBATDISCONNECT);
  287. if (ret < 0)
  288. goto out;
  289. }
  290. out:
  291. return ret;
  292. }
  293. static int pm2_int_reg1(void *pm2_data, int val)
  294. {
  295. struct pm2xxx_charger *pm2 = pm2_data;
  296. int ret = 0;
  297. if (val & (PM2XXX_INT2_ITVPWR1PLUG | PM2XXX_INT2_ITVPWR2PLUG)) {
  298. dev_dbg(pm2->dev , "Main charger plugged\n");
  299. ret = pm2xxx_charger_itv_pwr_plug_mngt(pm2, val &
  300. (PM2XXX_INT2_ITVPWR1PLUG | PM2XXX_INT2_ITVPWR2PLUG));
  301. }
  302. if (val &
  303. (PM2XXX_INT2_ITVPWR1UNPLUG | PM2XXX_INT2_ITVPWR2UNPLUG)) {
  304. dev_dbg(pm2->dev , "Main charger unplugged\n");
  305. ret = pm2xxx_charger_itv_pwr_unplug_mngt(pm2, val &
  306. (PM2XXX_INT2_ITVPWR1UNPLUG |
  307. PM2XXX_INT2_ITVPWR2UNPLUG));
  308. }
  309. return ret;
  310. }
  311. static int pm2_int_reg2(void *pm2_data, int val)
  312. {
  313. struct pm2xxx_charger *pm2 = pm2_data;
  314. int ret = 0;
  315. if (val & PM2XXX_INT3_ITAUTOTIMEOUTWD)
  316. ret = pm2xxx_charger_wd_exp_mngt(pm2, val);
  317. if (val & (PM2XXX_INT3_ITCHPRECHARGEWD |
  318. PM2XXX_INT3_ITCHCCWD | PM2XXX_INT3_ITCHCVWD)) {
  319. dev_dbg(pm2->dev,
  320. "Watchdog occurred for precharge, CC and CV charge\n");
  321. }
  322. return ret;
  323. }
  324. static int pm2_int_reg3(void *pm2_data, int val)
  325. {
  326. struct pm2xxx_charger *pm2 = pm2_data;
  327. int ret = 0;
  328. if (val & (PM2XXX_INT4_ITCHARGINGON)) {
  329. dev_dbg(pm2->dev ,
  330. "chargind operation has started\n");
  331. }
  332. if (val & (PM2XXX_INT4_ITVRESUME)) {
  333. dev_dbg(pm2->dev,
  334. "battery discharged down to VResume threshold\n");
  335. }
  336. if (val & (PM2XXX_INT4_ITBATTFULL)) {
  337. dev_dbg(pm2->dev , "battery fully detected\n");
  338. }
  339. if (val & (PM2XXX_INT4_ITCVPHASE)) {
  340. dev_dbg(pm2->dev, "CV phase enter with 0.5C charging\n");
  341. }
  342. if (val & (PM2XXX_INT4_ITVPWR2OVV | PM2XXX_INT4_ITVPWR1OVV)) {
  343. pm2->failure_case = VPWR_OVV;
  344. ret = pm2xxx_charger_ovv_mngt(pm2, val &
  345. (PM2XXX_INT4_ITVPWR2OVV | PM2XXX_INT4_ITVPWR1OVV));
  346. dev_dbg(pm2->dev, "VPWR/VSYSTEM overvoltage detected\n");
  347. }
  348. if (val & (PM2XXX_INT4_S_ITBATTEMPCOLD |
  349. PM2XXX_INT4_S_ITBATTEMPHOT)) {
  350. ret = pm2xxx_charger_batt_therm_mngt(pm2, val &
  351. (PM2XXX_INT4_S_ITBATTEMPCOLD |
  352. PM2XXX_INT4_S_ITBATTEMPHOT));
  353. dev_dbg(pm2->dev, "BTEMP is too Low/High\n");
  354. }
  355. return ret;
  356. }
  357. static int pm2_int_reg4(void *pm2_data, int val)
  358. {
  359. struct pm2xxx_charger *pm2 = pm2_data;
  360. int ret = 0;
  361. if (val & PM2XXX_INT5_ITVSYSTEMOVV) {
  362. pm2->failure_case = VSYSTEM_OVV;
  363. ret = pm2xxx_charger_ovv_mngt(pm2, val &
  364. PM2XXX_INT5_ITVSYSTEMOVV);
  365. dev_dbg(pm2->dev, "VSYSTEM overvoltage detected\n");
  366. }
  367. if (val & (PM2XXX_INT5_ITTHERMALWARNINGFALL |
  368. PM2XXX_INT5_ITTHERMALWARNINGRISE |
  369. PM2XXX_INT5_ITTHERMALSHUTDOWNFALL |
  370. PM2XXX_INT5_ITTHERMALSHUTDOWNRISE)) {
  371. dev_dbg(pm2->dev, "BTEMP die temperature is too Low/High\n");
  372. ret = pm2xxx_charger_die_therm_mngt(pm2, val &
  373. (PM2XXX_INT5_ITTHERMALWARNINGFALL |
  374. PM2XXX_INT5_ITTHERMALWARNINGRISE |
  375. PM2XXX_INT5_ITTHERMALSHUTDOWNFALL |
  376. PM2XXX_INT5_ITTHERMALSHUTDOWNRISE));
  377. }
  378. return ret;
  379. }
  380. static int pm2_int_reg5(void *pm2_data, int val)
  381. {
  382. struct pm2xxx_charger *pm2 = pm2_data;
  383. int ret = 0;
  384. if (val & (PM2XXX_INT6_ITVPWR2DROP | PM2XXX_INT6_ITVPWR1DROP)) {
  385. dev_dbg(pm2->dev, "VMPWR drop to VBAT level\n");
  386. }
  387. if (val & (PM2XXX_INT6_ITVPWR2VALIDRISE |
  388. PM2XXX_INT6_ITVPWR1VALIDRISE |
  389. PM2XXX_INT6_ITVPWR2VALIDFALL |
  390. PM2XXX_INT6_ITVPWR1VALIDFALL)) {
  391. dev_dbg(pm2->dev, "Falling/Rising edge on WPWR1/2\n");
  392. }
  393. return ret;
  394. }
  395. static irqreturn_t pm2xxx_irq_int(int irq, void *data)
  396. {
  397. struct pm2xxx_charger *pm2 = data;
  398. struct pm2xxx_interrupts *interrupt = pm2->pm2_int;
  399. int i;
  400. /* wake up the device */
  401. pm_runtime_get_sync(pm2->dev);
  402. do {
  403. for (i = 0; i < PM2XXX_NUM_INT_REG; i++) {
  404. pm2xxx_reg_read(pm2,
  405. pm2xxx_interrupt_registers[i],
  406. &(interrupt->reg[i]));
  407. if (interrupt->reg[i] > 0)
  408. interrupt->handler[i](pm2, interrupt->reg[i]);
  409. }
  410. } while (gpio_get_value(pm2->pdata->gpio_irq_number) == 0);
  411. pm_runtime_mark_last_busy(pm2->dev);
  412. pm_runtime_put_autosuspend(pm2->dev);
  413. return IRQ_HANDLED;
  414. }
  415. static int pm2xxx_charger_get_ac_cv(struct pm2xxx_charger *pm2)
  416. {
  417. int ret = 0;
  418. u8 val;
  419. if (pm2->ac.charger_connected && pm2->ac.charger_online) {
  420. ret = pm2xxx_reg_read(pm2, PM2XXX_SRCE_REG_INT4, &val);
  421. if (ret < 0) {
  422. dev_err(pm2->dev, "%s pm2xxx read failed\n", __func__);
  423. goto out;
  424. }
  425. if (val & PM2XXX_INT4_S_ITCVPHASE)
  426. ret = PM2XXX_CONST_VOLT;
  427. else
  428. ret = PM2XXX_CONST_CURR;
  429. }
  430. out:
  431. return ret;
  432. }
  433. static int pm2xxx_current_to_regval(int curr)
  434. {
  435. int i;
  436. if (curr < pm2xxx_charger_current_map[0])
  437. return 0;
  438. for (i = 1; i < ARRAY_SIZE(pm2xxx_charger_current_map); i++) {
  439. if (curr < pm2xxx_charger_current_map[i])
  440. return (i - 1);
  441. }
  442. i = ARRAY_SIZE(pm2xxx_charger_current_map) - 1;
  443. if (curr == pm2xxx_charger_current_map[i])
  444. return i;
  445. else
  446. return -EINVAL;
  447. }
  448. static int pm2xxx_voltage_to_regval(int curr)
  449. {
  450. int i;
  451. if (curr < pm2xxx_charger_voltage_map[0])
  452. return 0;
  453. for (i = 1; i < ARRAY_SIZE(pm2xxx_charger_voltage_map); i++) {
  454. if (curr < pm2xxx_charger_voltage_map[i])
  455. return i - 1;
  456. }
  457. i = ARRAY_SIZE(pm2xxx_charger_voltage_map) - 1;
  458. if (curr == pm2xxx_charger_voltage_map[i])
  459. return i;
  460. else
  461. return -EINVAL;
  462. }
  463. static int pm2xxx_charger_update_charger_current(struct ux500_charger *charger,
  464. int ich_out)
  465. {
  466. int ret;
  467. int curr_index;
  468. struct pm2xxx_charger *pm2;
  469. u8 val;
  470. if (charger->psy->desc->type == POWER_SUPPLY_TYPE_MAINS)
  471. pm2 = to_pm2xxx_charger_ac_device_info(charger);
  472. else
  473. return -ENXIO;
  474. curr_index = pm2xxx_current_to_regval(ich_out);
  475. if (curr_index < 0) {
  476. dev_err(pm2->dev,
  477. "Charger current too high, charging not started\n");
  478. return -ENXIO;
  479. }
  480. ret = pm2xxx_reg_read(pm2, PM2XXX_BATT_CTRL_REG6, &val);
  481. if (ret >= 0) {
  482. val &= ~PM2XXX_DIR_CH_CC_CURRENT_MASK;
  483. val |= curr_index;
  484. ret = pm2xxx_reg_write(pm2, PM2XXX_BATT_CTRL_REG6, val);
  485. if (ret < 0) {
  486. dev_err(pm2->dev,
  487. "%s write failed\n", __func__);
  488. }
  489. }
  490. else
  491. dev_err(pm2->dev, "%s read failed\n", __func__);
  492. return ret;
  493. }
  494. static int pm2xxx_charger_ac_get_property(struct power_supply *psy,
  495. enum power_supply_property psp,
  496. union power_supply_propval *val)
  497. {
  498. struct pm2xxx_charger *pm2;
  499. pm2 = to_pm2xxx_charger_ac_device_info(psy_to_ux500_charger(psy));
  500. switch (psp) {
  501. case POWER_SUPPLY_PROP_HEALTH:
  502. if (pm2->flags.mainextchnotok)
  503. val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
  504. else if (pm2->ac.wd_expired)
  505. val->intval = POWER_SUPPLY_HEALTH_DEAD;
  506. else if (pm2->flags.main_thermal_prot)
  507. val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
  508. else if (pm2->flags.ovv)
  509. val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
  510. else
  511. val->intval = POWER_SUPPLY_HEALTH_GOOD;
  512. break;
  513. case POWER_SUPPLY_PROP_ONLINE:
  514. val->intval = pm2->ac.charger_online;
  515. break;
  516. case POWER_SUPPLY_PROP_PRESENT:
  517. val->intval = pm2->ac.charger_connected;
  518. break;
  519. case POWER_SUPPLY_PROP_VOLTAGE_AVG:
  520. pm2->ac.cv_active = pm2xxx_charger_get_ac_cv(pm2);
  521. val->intval = pm2->ac.cv_active;
  522. break;
  523. default:
  524. return -EINVAL;
  525. }
  526. return 0;
  527. }
  528. static int pm2xxx_charging_init(struct pm2xxx_charger *pm2)
  529. {
  530. int ret = 0;
  531. /* enable CC and CV watchdog */
  532. ret = pm2xxx_reg_write(pm2, PM2XXX_BATT_CTRL_REG3,
  533. (PM2XXX_CH_WD_CV_PHASE_60MIN | PM2XXX_CH_WD_CC_PHASE_60MIN));
  534. if( ret < 0)
  535. return ret;
  536. /* enable precharge watchdog */
  537. ret = pm2xxx_reg_write(pm2, PM2XXX_BATT_CTRL_REG4,
  538. PM2XXX_CH_WD_PRECH_PHASE_60MIN);
  539. /* Disable auto timeout */
  540. ret = pm2xxx_reg_write(pm2, PM2XXX_BATT_CTRL_REG5,
  541. PM2XXX_CH_WD_AUTO_TIMEOUT_20MIN);
  542. /*
  543. * EOC current level = 100mA
  544. * Precharge current level = 100mA
  545. * CC current level = 1000mA
  546. */
  547. ret = pm2xxx_reg_write(pm2, PM2XXX_BATT_CTRL_REG6,
  548. (PM2XXX_DIR_CH_CC_CURRENT_1000MA |
  549. PM2XXX_CH_PRECH_CURRENT_100MA |
  550. PM2XXX_CH_EOC_CURRENT_100MA));
  551. /*
  552. * recharge threshold = 3.8V
  553. * Precharge to CC threshold = 2.9V
  554. */
  555. ret = pm2xxx_reg_write(pm2, PM2XXX_BATT_CTRL_REG7,
  556. (PM2XXX_CH_PRECH_VOL_2_9 | PM2XXX_CH_VRESUME_VOL_3_8));
  557. /* float voltage charger level = 4.2V */
  558. ret = pm2xxx_reg_write(pm2, PM2XXX_BATT_CTRL_REG8,
  559. PM2XXX_CH_VOLT_4_2);
  560. /* Voltage drop between VBAT and VSYS in HW charging = 300mV */
  561. ret = pm2xxx_reg_write(pm2, PM2XXX_BATT_CTRL_REG9,
  562. (PM2XXX_CH_150MV_DROP_300MV | PM2XXX_CHARCHING_INFO_DIS |
  563. PM2XXX_CH_CC_REDUCED_CURRENT_IDENT |
  564. PM2XXX_CH_CC_MODEDROP_DIS));
  565. /* Input charger level of over voltage = 10V */
  566. ret = pm2xxx_reg_write(pm2, PM2XXX_INP_VOLT_VPWR2,
  567. PM2XXX_VPWR2_OVV_10);
  568. ret = pm2xxx_reg_write(pm2, PM2XXX_INP_VOLT_VPWR1,
  569. PM2XXX_VPWR1_OVV_10);
  570. /* Input charger drop */
  571. ret = pm2xxx_reg_write(pm2, PM2XXX_INP_DROP_VPWR2,
  572. (PM2XXX_VPWR2_HW_OPT_DIS | PM2XXX_VPWR2_VALID_DIS |
  573. PM2XXX_VPWR2_DROP_DIS));
  574. ret = pm2xxx_reg_write(pm2, PM2XXX_INP_DROP_VPWR1,
  575. (PM2XXX_VPWR1_HW_OPT_DIS | PM2XXX_VPWR1_VALID_DIS |
  576. PM2XXX_VPWR1_DROP_DIS));
  577. /* Disable battery low monitoring */
  578. ret = pm2xxx_reg_write(pm2, PM2XXX_BATT_LOW_LEV_COMP_REG,
  579. PM2XXX_VBAT_LOW_MONITORING_ENA);
  580. return ret;
  581. }
  582. static int pm2xxx_charger_ac_en(struct ux500_charger *charger,
  583. int enable, int vset, int iset)
  584. {
  585. int ret;
  586. int volt_index;
  587. int curr_index;
  588. u8 val;
  589. struct pm2xxx_charger *pm2 = to_pm2xxx_charger_ac_device_info(charger);
  590. if (enable) {
  591. if (!pm2->ac.charger_connected) {
  592. dev_dbg(pm2->dev, "AC charger not connected\n");
  593. return -ENXIO;
  594. }
  595. dev_dbg(pm2->dev, "Enable AC: %dmV %dmA\n", vset, iset);
  596. if (!pm2->vddadc_en_ac) {
  597. ret = regulator_enable(pm2->regu);
  598. if (ret)
  599. dev_warn(pm2->dev,
  600. "Failed to enable vddadc regulator\n");
  601. else
  602. pm2->vddadc_en_ac = true;
  603. }
  604. ret = pm2xxx_charging_init(pm2);
  605. if (ret < 0) {
  606. dev_err(pm2->dev, "%s charging init failed\n",
  607. __func__);
  608. goto error_occured;
  609. }
  610. volt_index = pm2xxx_voltage_to_regval(vset);
  611. curr_index = pm2xxx_current_to_regval(iset);
  612. if (volt_index < 0 || curr_index < 0) {
  613. dev_err(pm2->dev,
  614. "Charger voltage or current too high, "
  615. "charging not started\n");
  616. return -ENXIO;
  617. }
  618. ret = pm2xxx_reg_read(pm2, PM2XXX_BATT_CTRL_REG8, &val);
  619. if (ret < 0) {
  620. dev_err(pm2->dev, "%s pm2xxx read failed\n", __func__);
  621. goto error_occured;
  622. }
  623. val &= ~PM2XXX_CH_VOLT_MASK;
  624. val |= volt_index;
  625. ret = pm2xxx_reg_write(pm2, PM2XXX_BATT_CTRL_REG8, val);
  626. if (ret < 0) {
  627. dev_err(pm2->dev, "%s pm2xxx write failed\n", __func__);
  628. goto error_occured;
  629. }
  630. ret = pm2xxx_reg_read(pm2, PM2XXX_BATT_CTRL_REG6, &val);
  631. if (ret < 0) {
  632. dev_err(pm2->dev, "%s pm2xxx read failed\n", __func__);
  633. goto error_occured;
  634. }
  635. val &= ~PM2XXX_DIR_CH_CC_CURRENT_MASK;
  636. val |= curr_index;
  637. ret = pm2xxx_reg_write(pm2, PM2XXX_BATT_CTRL_REG6, val);
  638. if (ret < 0) {
  639. dev_err(pm2->dev, "%s pm2xxx write failed\n", __func__);
  640. goto error_occured;
  641. }
  642. if (!pm2->bat->enable_overshoot) {
  643. ret = pm2xxx_reg_read(pm2, PM2XXX_LED_CTRL_REG, &val);
  644. if (ret < 0) {
  645. dev_err(pm2->dev, "%s pm2xxx read failed\n",
  646. __func__);
  647. goto error_occured;
  648. }
  649. val |= PM2XXX_ANTI_OVERSHOOT_EN;
  650. ret = pm2xxx_reg_write(pm2, PM2XXX_LED_CTRL_REG, val);
  651. if (ret < 0) {
  652. dev_err(pm2->dev, "%s pm2xxx write failed\n",
  653. __func__);
  654. goto error_occured;
  655. }
  656. }
  657. ret = pm2xxx_charging_enable_mngt(pm2);
  658. if (ret < 0) {
  659. dev_err(pm2->dev, "Failed to enable"
  660. "pm2xxx ac charger\n");
  661. goto error_occured;
  662. }
  663. pm2->ac.charger_online = 1;
  664. } else {
  665. pm2->ac.charger_online = 0;
  666. pm2->ac.wd_expired = false;
  667. /* Disable regulator if enabled */
  668. if (pm2->vddadc_en_ac) {
  669. regulator_disable(pm2->regu);
  670. pm2->vddadc_en_ac = false;
  671. }
  672. ret = pm2xxx_charging_disable_mngt(pm2);
  673. if (ret < 0) {
  674. dev_err(pm2->dev, "failed to disable"
  675. "pm2xxx ac charger\n");
  676. goto error_occured;
  677. }
  678. dev_dbg(pm2->dev, "PM2301: " "Disabled AC charging\n");
  679. }
  680. power_supply_changed(pm2->ac_chg.psy);
  681. error_occured:
  682. return ret;
  683. }
  684. static int pm2xxx_charger_watchdog_kick(struct ux500_charger *charger)
  685. {
  686. int ret;
  687. struct pm2xxx_charger *pm2;
  688. if (charger->psy->desc->type == POWER_SUPPLY_TYPE_MAINS)
  689. pm2 = to_pm2xxx_charger_ac_device_info(charger);
  690. else
  691. return -ENXIO;
  692. ret = pm2xxx_reg_write(pm2, PM2XXX_BATT_WD_KICK, WD_TIMER);
  693. if (ret)
  694. dev_err(pm2->dev, "Failed to kick WD!\n");
  695. return ret;
  696. }
  697. static void pm2xxx_charger_ac_work(struct work_struct *work)
  698. {
  699. struct pm2xxx_charger *pm2 = container_of(work,
  700. struct pm2xxx_charger, ac_work);
  701. power_supply_changed(pm2->ac_chg.psy);
  702. sysfs_notify(&pm2->ac_chg.psy->dev.kobj, NULL, "present");
  703. };
  704. static void pm2xxx_charger_check_hw_failure_work(struct work_struct *work)
  705. {
  706. u8 reg_value;
  707. struct pm2xxx_charger *pm2 = container_of(work,
  708. struct pm2xxx_charger, check_hw_failure_work.work);
  709. if (pm2->flags.ovv) {
  710. pm2xxx_reg_read(pm2, PM2XXX_SRCE_REG_INT4, &reg_value);
  711. if (!(reg_value & (PM2XXX_INT4_S_ITVPWR1OVV |
  712. PM2XXX_INT4_S_ITVPWR2OVV))) {
  713. pm2->flags.ovv = false;
  714. power_supply_changed(pm2->ac_chg.psy);
  715. }
  716. }
  717. /* If we still have a failure, schedule a new check */
  718. if (pm2->flags.ovv) {
  719. queue_delayed_work(pm2->charger_wq,
  720. &pm2->check_hw_failure_work, round_jiffies(HZ));
  721. }
  722. }
  723. static void pm2xxx_charger_check_main_thermal_prot_work(
  724. struct work_struct *work)
  725. {
  726. int ret;
  727. u8 val;
  728. struct pm2xxx_charger *pm2 = container_of(work, struct pm2xxx_charger,
  729. check_main_thermal_prot_work);
  730. /* Check if die temp warning is still active */
  731. ret = pm2xxx_reg_read(pm2, PM2XXX_SRCE_REG_INT5, &val);
  732. if (ret < 0) {
  733. dev_err(pm2->dev, "%s pm2xxx read failed\n", __func__);
  734. return;
  735. }
  736. if (val & (PM2XXX_INT5_S_ITTHERMALWARNINGRISE
  737. | PM2XXX_INT5_S_ITTHERMALSHUTDOWNRISE))
  738. pm2->flags.main_thermal_prot = true;
  739. else if (val & (PM2XXX_INT5_S_ITTHERMALWARNINGFALL
  740. | PM2XXX_INT5_S_ITTHERMALSHUTDOWNFALL))
  741. pm2->flags.main_thermal_prot = false;
  742. power_supply_changed(pm2->ac_chg.psy);
  743. }
  744. static struct pm2xxx_interrupts pm2xxx_int = {
  745. .handler[0] = pm2_int_reg0,
  746. .handler[1] = pm2_int_reg1,
  747. .handler[2] = pm2_int_reg2,
  748. .handler[3] = pm2_int_reg3,
  749. .handler[4] = pm2_int_reg4,
  750. .handler[5] = pm2_int_reg5,
  751. };
  752. static struct pm2xxx_irq pm2xxx_charger_irq[] = {
  753. {"PM2XXX_IRQ_INT", pm2xxx_irq_int},
  754. };
  755. static int __maybe_unused pm2xxx_wall_charger_resume(struct device *dev)
  756. {
  757. struct i2c_client *i2c_client = to_i2c_client(dev);
  758. struct pm2xxx_charger *pm2;
  759. pm2 = (struct pm2xxx_charger *)i2c_get_clientdata(i2c_client);
  760. set_lpn_pin(pm2);
  761. /* If we still have a HW failure, schedule a new check */
  762. if (pm2->flags.ovv)
  763. queue_delayed_work(pm2->charger_wq,
  764. &pm2->check_hw_failure_work, 0);
  765. return 0;
  766. }
  767. static int __maybe_unused pm2xxx_wall_charger_suspend(struct device *dev)
  768. {
  769. struct i2c_client *i2c_client = to_i2c_client(dev);
  770. struct pm2xxx_charger *pm2;
  771. pm2 = (struct pm2xxx_charger *)i2c_get_clientdata(i2c_client);
  772. clear_lpn_pin(pm2);
  773. /* Cancel any pending HW failure check */
  774. if (delayed_work_pending(&pm2->check_hw_failure_work))
  775. cancel_delayed_work(&pm2->check_hw_failure_work);
  776. flush_work(&pm2->ac_work);
  777. flush_work(&pm2->check_main_thermal_prot_work);
  778. return 0;
  779. }
  780. static int __maybe_unused pm2xxx_runtime_suspend(struct device *dev)
  781. {
  782. struct i2c_client *pm2xxx_i2c_client = to_i2c_client(dev);
  783. struct pm2xxx_charger *pm2;
  784. pm2 = (struct pm2xxx_charger *)i2c_get_clientdata(pm2xxx_i2c_client);
  785. clear_lpn_pin(pm2);
  786. return 0;
  787. }
  788. static int __maybe_unused pm2xxx_runtime_resume(struct device *dev)
  789. {
  790. struct i2c_client *pm2xxx_i2c_client = to_i2c_client(dev);
  791. struct pm2xxx_charger *pm2;
  792. pm2 = (struct pm2xxx_charger *)i2c_get_clientdata(pm2xxx_i2c_client);
  793. if (gpio_is_valid(pm2->lpn_pin) && gpio_get_value(pm2->lpn_pin) == 0)
  794. set_lpn_pin(pm2);
  795. return 0;
  796. }
  797. static const struct dev_pm_ops pm2xxx_pm_ops __maybe_unused = {
  798. SET_SYSTEM_SLEEP_PM_OPS(pm2xxx_wall_charger_suspend,
  799. pm2xxx_wall_charger_resume)
  800. SET_RUNTIME_PM_OPS(pm2xxx_runtime_suspend, pm2xxx_runtime_resume, NULL)
  801. };
  802. static int pm2xxx_wall_charger_probe(struct i2c_client *i2c_client,
  803. const struct i2c_device_id *id)
  804. {
  805. struct pm2xxx_platform_data *pl_data = i2c_client->dev.platform_data;
  806. struct power_supply_config psy_cfg = {};
  807. struct pm2xxx_charger *pm2;
  808. int ret = 0;
  809. u8 val;
  810. int i;
  811. if (!pl_data) {
  812. dev_err(&i2c_client->dev, "No platform data supplied\n");
  813. return -EINVAL;
  814. }
  815. pm2 = kzalloc(sizeof(struct pm2xxx_charger), GFP_KERNEL);
  816. if (!pm2) {
  817. dev_err(&i2c_client->dev, "pm2xxx_charger allocation failed\n");
  818. return -ENOMEM;
  819. }
  820. /* get parent data */
  821. pm2->dev = &i2c_client->dev;
  822. pm2->pm2_int = &pm2xxx_int;
  823. /* get charger spcific platform data */
  824. if (!pl_data->wall_charger) {
  825. dev_err(pm2->dev, "no charger platform data supplied\n");
  826. ret = -EINVAL;
  827. goto free_device_info;
  828. }
  829. pm2->pdata = pl_data->wall_charger;
  830. /* get battery specific platform data */
  831. if (!pl_data->battery) {
  832. dev_err(pm2->dev, "no battery platform data supplied\n");
  833. ret = -EINVAL;
  834. goto free_device_info;
  835. }
  836. pm2->bat = pl_data->battery;
  837. if (!i2c_check_functionality(i2c_client->adapter,
  838. I2C_FUNC_SMBUS_BYTE_DATA |
  839. I2C_FUNC_SMBUS_READ_WORD_DATA)) {
  840. ret = -ENODEV;
  841. dev_info(pm2->dev, "pm2301 i2c_check_functionality failed\n");
  842. goto free_device_info;
  843. }
  844. pm2->config.pm2xxx_i2c = i2c_client;
  845. pm2->config.pm2xxx_id = (struct i2c_device_id *) id;
  846. i2c_set_clientdata(i2c_client, pm2);
  847. /* AC supply */
  848. /* power_supply base class */
  849. pm2->ac_chg_desc.name = pm2->pdata->label;
  850. pm2->ac_chg_desc.type = POWER_SUPPLY_TYPE_MAINS;
  851. pm2->ac_chg_desc.properties = pm2xxx_charger_ac_props;
  852. pm2->ac_chg_desc.num_properties = ARRAY_SIZE(pm2xxx_charger_ac_props);
  853. pm2->ac_chg_desc.get_property = pm2xxx_charger_ac_get_property;
  854. psy_cfg.supplied_to = pm2->pdata->supplied_to;
  855. psy_cfg.num_supplicants = pm2->pdata->num_supplicants;
  856. /* pm2xxx_charger sub-class */
  857. pm2->ac_chg.ops.enable = &pm2xxx_charger_ac_en;
  858. pm2->ac_chg.ops.kick_wd = &pm2xxx_charger_watchdog_kick;
  859. pm2->ac_chg.ops.update_curr = &pm2xxx_charger_update_charger_current;
  860. pm2->ac_chg.max_out_volt = pm2xxx_charger_voltage_map[
  861. ARRAY_SIZE(pm2xxx_charger_voltage_map) - 1];
  862. pm2->ac_chg.max_out_curr = pm2xxx_charger_current_map[
  863. ARRAY_SIZE(pm2xxx_charger_current_map) - 1];
  864. pm2->ac_chg.wdt_refresh = WD_KICK_INTERVAL;
  865. pm2->ac_chg.enabled = true;
  866. pm2->ac_chg.external = true;
  867. /* Create a work queue for the charger */
  868. pm2->charger_wq = alloc_ordered_workqueue("pm2xxx_charger_wq",
  869. WQ_MEM_RECLAIM);
  870. if (pm2->charger_wq == NULL) {
  871. ret = -ENOMEM;
  872. dev_err(pm2->dev, "failed to create work queue\n");
  873. goto free_device_info;
  874. }
  875. /* Init work for charger detection */
  876. INIT_WORK(&pm2->ac_work, pm2xxx_charger_ac_work);
  877. /* Init work for checking HW status */
  878. INIT_WORK(&pm2->check_main_thermal_prot_work,
  879. pm2xxx_charger_check_main_thermal_prot_work);
  880. /* Init work for HW failure check */
  881. INIT_DEFERRABLE_WORK(&pm2->check_hw_failure_work,
  882. pm2xxx_charger_check_hw_failure_work);
  883. /*
  884. * VDD ADC supply needs to be enabled from this driver when there
  885. * is a charger connected to avoid erroneous BTEMP_HIGH/LOW
  886. * interrupts during charging
  887. */
  888. pm2->regu = regulator_get(pm2->dev, "vddadc");
  889. if (IS_ERR(pm2->regu)) {
  890. ret = PTR_ERR(pm2->regu);
  891. dev_err(pm2->dev, "failed to get vddadc regulator\n");
  892. goto free_charger_wq;
  893. }
  894. /* Register AC charger class */
  895. pm2->ac_chg.psy = power_supply_register(pm2->dev, &pm2->ac_chg_desc,
  896. &psy_cfg);
  897. if (IS_ERR(pm2->ac_chg.psy)) {
  898. dev_err(pm2->dev, "failed to register AC charger\n");
  899. ret = PTR_ERR(pm2->ac_chg.psy);
  900. goto free_regulator;
  901. }
  902. /* Register interrupts */
  903. ret = request_threaded_irq(gpio_to_irq(pm2->pdata->gpio_irq_number),
  904. NULL,
  905. pm2xxx_charger_irq[0].isr,
  906. pm2->pdata->irq_type,
  907. pm2xxx_charger_irq[0].name, pm2);
  908. if (ret != 0) {
  909. dev_err(pm2->dev, "failed to request %s IRQ %d: %d\n",
  910. pm2xxx_charger_irq[0].name,
  911. gpio_to_irq(pm2->pdata->gpio_irq_number), ret);
  912. goto unregister_pm2xxx_charger;
  913. }
  914. ret = pm_runtime_set_active(pm2->dev);
  915. if (ret)
  916. dev_err(pm2->dev, "set active Error\n");
  917. pm_runtime_enable(pm2->dev);
  918. pm_runtime_set_autosuspend_delay(pm2->dev, PM2XXX_AUTOSUSPEND_DELAY);
  919. pm_runtime_use_autosuspend(pm2->dev);
  920. pm_runtime_resume(pm2->dev);
  921. /* pm interrupt can wake up system */
  922. ret = enable_irq_wake(gpio_to_irq(pm2->pdata->gpio_irq_number));
  923. if (ret) {
  924. dev_err(pm2->dev, "failed to set irq wake\n");
  925. goto unregister_pm2xxx_interrupt;
  926. }
  927. mutex_init(&pm2->lock);
  928. if (gpio_is_valid(pm2->pdata->lpn_gpio)) {
  929. /* get lpn GPIO from platform data */
  930. pm2->lpn_pin = pm2->pdata->lpn_gpio;
  931. /*
  932. * Charger detection mechanism requires pulling up the LPN pin
  933. * while i2c communication if Charger is not connected
  934. * LPN pin of PM2301 is GPIO60 of AB9540
  935. */
  936. ret = gpio_request(pm2->lpn_pin, "pm2301_lpm_gpio");
  937. if (ret < 0) {
  938. dev_err(pm2->dev, "pm2301_lpm_gpio request failed\n");
  939. goto disable_pm2_irq_wake;
  940. }
  941. ret = gpio_direction_output(pm2->lpn_pin, 0);
  942. if (ret < 0) {
  943. dev_err(pm2->dev, "pm2301_lpm_gpio direction failed\n");
  944. goto free_gpio;
  945. }
  946. set_lpn_pin(pm2);
  947. }
  948. /* read interrupt registers */
  949. for (i = 0; i < PM2XXX_NUM_INT_REG; i++)
  950. pm2xxx_reg_read(pm2,
  951. pm2xxx_interrupt_registers[i],
  952. &val);
  953. ret = pm2xxx_charger_detection(pm2, &val);
  954. if ((ret == 0) && val) {
  955. pm2->ac.charger_connected = 1;
  956. ab8500_override_turn_on_stat(~AB8500_POW_KEY_1_ON,
  957. AB8500_MAIN_CH_DET);
  958. pm2->ac_conn = true;
  959. power_supply_changed(pm2->ac_chg.psy);
  960. sysfs_notify(&pm2->ac_chg.psy->dev.kobj, NULL, "present");
  961. }
  962. return 0;
  963. free_gpio:
  964. if (gpio_is_valid(pm2->lpn_pin))
  965. gpio_free(pm2->lpn_pin);
  966. disable_pm2_irq_wake:
  967. disable_irq_wake(gpio_to_irq(pm2->pdata->gpio_irq_number));
  968. unregister_pm2xxx_interrupt:
  969. /* disable interrupt */
  970. free_irq(gpio_to_irq(pm2->pdata->gpio_irq_number), pm2);
  971. unregister_pm2xxx_charger:
  972. /* unregister power supply */
  973. power_supply_unregister(pm2->ac_chg.psy);
  974. free_regulator:
  975. /* disable the regulator */
  976. regulator_put(pm2->regu);
  977. free_charger_wq:
  978. destroy_workqueue(pm2->charger_wq);
  979. free_device_info:
  980. kfree(pm2);
  981. return ret;
  982. }
  983. static int pm2xxx_wall_charger_remove(struct i2c_client *i2c_client)
  984. {
  985. struct pm2xxx_charger *pm2 = i2c_get_clientdata(i2c_client);
  986. /* Disable pm_runtime */
  987. pm_runtime_disable(pm2->dev);
  988. /* Disable AC charging */
  989. pm2xxx_charger_ac_en(&pm2->ac_chg, false, 0, 0);
  990. /* Disable wake by pm interrupt */
  991. disable_irq_wake(gpio_to_irq(pm2->pdata->gpio_irq_number));
  992. /* Disable interrupts */
  993. free_irq(gpio_to_irq(pm2->pdata->gpio_irq_number), pm2);
  994. /* Delete the work queue */
  995. destroy_workqueue(pm2->charger_wq);
  996. flush_scheduled_work();
  997. /* disable the regulator */
  998. regulator_put(pm2->regu);
  999. power_supply_unregister(pm2->ac_chg.psy);
  1000. if (gpio_is_valid(pm2->lpn_pin))
  1001. gpio_free(pm2->lpn_pin);
  1002. kfree(pm2);
  1003. return 0;
  1004. }
  1005. static const struct i2c_device_id pm2xxx_id[] = {
  1006. { "pm2301", 0 },
  1007. { }
  1008. };
  1009. MODULE_DEVICE_TABLE(i2c, pm2xxx_id);
  1010. static struct i2c_driver pm2xxx_charger_driver = {
  1011. .probe = pm2xxx_wall_charger_probe,
  1012. .remove = pm2xxx_wall_charger_remove,
  1013. .driver = {
  1014. .name = "pm2xxx-wall_charger",
  1015. .pm = IS_ENABLED(CONFIG_PM) ? &pm2xxx_pm_ops : NULL,
  1016. },
  1017. .id_table = pm2xxx_id,
  1018. };
  1019. static int __init pm2xxx_charger_init(void)
  1020. {
  1021. return i2c_add_driver(&pm2xxx_charger_driver);
  1022. }
  1023. static void __exit pm2xxx_charger_exit(void)
  1024. {
  1025. i2c_del_driver(&pm2xxx_charger_driver);
  1026. }
  1027. device_initcall_sync(pm2xxx_charger_init);
  1028. module_exit(pm2xxx_charger_exit);
  1029. MODULE_LICENSE("GPL v2");
  1030. MODULE_AUTHOR("Rajkumar kasirajan, Olivier Launay");
  1031. MODULE_DESCRIPTION("PM2xxx charger management driver");