max77823_charger.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404
  1. /*
  2. * max77823_charger.c
  3. * Samsung MAX77823 Charger Driver
  4. *
  5. * Copyright (C) 2012 Samsung Electronics
  6. *
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #define DEBUG
  13. #include <linux/mfd/max77823.h>
  14. #include <linux/mfd/max77823-private.h>
  15. #ifdef CONFIG_USB_HOST_NOTIFY
  16. #include <linux/host_notify.h>
  17. #endif
  18. #include <linux/debugfs.h>
  19. #include <linux/seq_file.h>
  20. static enum power_supply_property max77823_charger_props[] = {
  21. POWER_SUPPLY_PROP_STATUS,
  22. POWER_SUPPLY_PROP_CHARGE_TYPE,
  23. POWER_SUPPLY_PROP_HEALTH,
  24. POWER_SUPPLY_PROP_ONLINE,
  25. POWER_SUPPLY_PROP_CURRENT_MAX,
  26. POWER_SUPPLY_PROP_CURRENT_AVG,
  27. POWER_SUPPLY_PROP_CURRENT_NOW,
  28. POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
  29. };
  30. static void max77823_charger_initialize(struct max77823_charger_data *charger);
  31. static int max77823_get_vbus_state(struct max77823_charger_data *charger);
  32. static int max77823_get_charger_state(struct max77823_charger_data *charger);
  33. static bool max77823_charger_unlock(struct max77823_charger_data *charger)
  34. {
  35. struct i2c_client *i2c = charger->i2c;
  36. u8 reg_data;
  37. u8 chgprot;
  38. int retry_cnt = 0;
  39. bool need_init = false;
  40. do {
  41. max77823_read_reg(i2c, MAX77823_CHG_CNFG_06, &reg_data);
  42. chgprot = ((reg_data & 0x0C) >> 2);
  43. if (chgprot != 0x03) {
  44. pr_err("%s: unlock err, chgprot(0x%x), retry(%d)\n",
  45. __func__, chgprot, retry_cnt);
  46. max77823_write_reg(i2c, MAX77823_CHG_CNFG_06,
  47. (0x03 << 2));
  48. need_init = true;
  49. msleep(20);
  50. } else {
  51. pr_debug("%s: unlock success, chgprot(0x%x)\n",
  52. __func__, chgprot);
  53. break;
  54. }
  55. } while ((chgprot != 0x03) && (++retry_cnt < 10));
  56. return need_init;
  57. }
  58. static void check_charger_unlock_state(struct max77823_charger_data *charger)
  59. {
  60. bool need_reg_init;
  61. pr_debug("%s\n", __func__);
  62. need_reg_init = max77823_charger_unlock(charger);
  63. if (need_reg_init) {
  64. pr_err("%s: charger locked state, reg init\n", __func__);
  65. max77823_charger_initialize(charger);
  66. }
  67. }
  68. static void max77823_test_read(struct max77823_charger_data *charger)
  69. {
  70. u8 data = 0;
  71. u32 addr = 0;
  72. for (addr = 0xB0; addr <= 0xC3; addr++) {
  73. max77823_read_reg(charger->i2c, addr, &data);
  74. pr_debug("MAX7823 addr : 0x%02x data : 0x%02x\n", addr, data);
  75. }
  76. }
  77. static int max77823_get_vbus_state(struct max77823_charger_data *charger)
  78. {
  79. u8 reg_data;
  80. max77823_read_reg(charger->i2c,
  81. MAX77823_CHG_DETAILS_00, &reg_data);
  82. if (charger->cable_type == POWER_SUPPLY_TYPE_WIRELESS)
  83. reg_data = ((reg_data & MAX77823_WCIN_DTLS) >>
  84. MAX77823_WCIN_DTLS_SHIFT);
  85. else
  86. reg_data = ((reg_data & MAX77823_CHGIN_DTLS) >>
  87. MAX77823_CHGIN_DTLS_SHIFT);
  88. switch (reg_data) {
  89. case 0x00:
  90. pr_info("%s: VBUS is invalid. CHGIN < CHGIN_UVLO\n",
  91. __func__);
  92. break;
  93. case 0x01:
  94. pr_info("%s: VBUS is invalid. CHGIN < MBAT+CHGIN2SYS" \
  95. "and CHGIN > CHGIN_UVLO\n", __func__);
  96. break;
  97. case 0x02:
  98. pr_info("%s: VBUS is invalid. CHGIN > CHGIN_OVLO",
  99. __func__);
  100. break;
  101. case 0x03:
  102. break;
  103. default:
  104. break;
  105. }
  106. return reg_data;
  107. }
  108. static int max77823_get_charger_state(struct max77823_charger_data *charger)
  109. {
  110. int status = POWER_SUPPLY_STATUS_UNKNOWN;
  111. u8 reg_data;
  112. max77823_read_reg(charger->i2c,
  113. MAX77823_CHG_DETAILS_01, &reg_data);
  114. pr_info("%s : charger status (0x%02x)\n", __func__, reg_data);
  115. reg_data &= 0x0f;
  116. switch (reg_data)
  117. {
  118. case 0x00:
  119. case 0x01:
  120. case 0x02:
  121. status = POWER_SUPPLY_STATUS_CHARGING;
  122. break;
  123. case 0x03:
  124. case 0x04:
  125. status = POWER_SUPPLY_STATUS_FULL;
  126. break;
  127. case 0x05:
  128. case 0x06:
  129. case 0x07:
  130. status = POWER_SUPPLY_STATUS_NOT_CHARGING;
  131. break;
  132. case 0x08:
  133. case 0xA:
  134. case 0xB:
  135. status = POWER_SUPPLY_STATUS_DISCHARGING;
  136. default:
  137. status = POWER_SUPPLY_STATUS_UNKNOWN;
  138. break;
  139. }
  140. return (int)status;
  141. }
  142. static void max77823_dump_reg(struct max77823_charger_data *charger)
  143. {
  144. u8 reg_data;
  145. u32 reg_addr;
  146. pr_info("%s\n", __func__);
  147. for (reg_addr = 0xB0; reg_addr <= 0xC3; reg_addr++) {
  148. max77823_read_reg(charger->i2c, reg_addr, &reg_data);
  149. pr_info("max77823: c: 0x%02x(0x%02x)\n", reg_addr, reg_data);
  150. }
  151. }
  152. static int max77823_get_charging_health(struct max77823_charger_data *charger)
  153. {
  154. int state;
  155. int vbus_state;
  156. int retry_cnt;
  157. u8 chg_dtls_00, chg_dtls, reg_data;
  158. u8 chg_cnfg_00, chg_cnfg_01 ,chg_cnfg_02, chg_cnfg_04, chg_cnfg_09, chg_cnfg_12;
  159. max77823_read_reg(charger->i2c,
  160. MAX77823_CHG_DETAILS_01, &reg_data);
  161. reg_data = ((reg_data & MAX77823_BAT_DTLS) >> MAX77823_BAT_DTLS_SHIFT);
  162. pr_info("%s: reg_data(0x%x)\n", __func__, reg_data);
  163. switch (reg_data) {
  164. case 0x00:
  165. pr_info("%s: No battery and the charger is suspended\n",
  166. __func__);
  167. state = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
  168. break;
  169. case 0x01:
  170. pr_info("%s: battery is okay "
  171. "but its voltage is low(~VPQLB)\n", __func__);
  172. state = POWER_SUPPLY_HEALTH_GOOD;
  173. break;
  174. case 0x02:
  175. pr_info("%s: battery dead\n", __func__);
  176. state = POWER_SUPPLY_HEALTH_DEAD;
  177. break;
  178. case 0x03:
  179. state = POWER_SUPPLY_HEALTH_GOOD;
  180. break;
  181. case 0x04:
  182. pr_info("%s: battery is okay" \
  183. "but its voltage is low\n", __func__);
  184. state = POWER_SUPPLY_HEALTH_GOOD;
  185. break;
  186. case 0x05:
  187. pr_info("%s: battery ovp\n", __func__);
  188. state = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
  189. break;
  190. default:
  191. pr_info("%s: battery unknown : 0x%d\n", __func__, reg_data);
  192. state = POWER_SUPPLY_HEALTH_UNKNOWN;
  193. break;
  194. }
  195. if (state == POWER_SUPPLY_HEALTH_GOOD) {
  196. union power_supply_propval value;
  197. psy_do_property("battery", get,
  198. POWER_SUPPLY_PROP_HEALTH, value);
  199. /* VBUS OVP state return battery OVP state */
  200. vbus_state = max77823_get_vbus_state(charger);
  201. /* read CHG_DTLS and detecting battery terminal error */
  202. max77823_read_reg(charger->i2c,
  203. MAX77823_CHG_DETAILS_01, &chg_dtls);
  204. chg_dtls = ((chg_dtls & MAX77823_CHG_DTLS) >>
  205. MAX77823_CHG_DTLS_SHIFT);
  206. max77823_read_reg(charger->i2c,
  207. MAX77823_CHG_CNFG_00, &chg_cnfg_00);
  208. /* print the log at the abnormal case */
  209. if((charger->is_charging == 1) && (chg_dtls & 0x08)) {
  210. max77823_read_reg(charger->i2c,
  211. MAX77823_CHG_DETAILS_00, &chg_dtls_00);
  212. max77823_read_reg(charger->i2c,
  213. MAX77823_CHG_CNFG_01, &chg_cnfg_01);
  214. max77823_read_reg(charger->i2c,
  215. MAX77823_CHG_CNFG_02, &chg_cnfg_02);
  216. max77823_read_reg(charger->i2c,
  217. MAX77823_CHG_CNFG_04, &chg_cnfg_04);
  218. max77823_read_reg(charger->i2c,
  219. MAX77823_CHG_CNFG_09, &chg_cnfg_09);
  220. max77823_read_reg(charger->i2c,
  221. MAX77823_CHG_CNFG_12, &chg_cnfg_12);
  222. pr_info("%s: CHG_DTLS_00(0x%x), CHG_DTLS_01(0x%x), CHG_CNFG_00(0x%x)\n",
  223. __func__, chg_dtls_00, chg_dtls, chg_cnfg_00);
  224. pr_info("%s: CHG_CNFG_01(0x%x), CHG_CNFG_02(0x%x), CHG_CNFG_04(0x%x)\n",
  225. __func__, chg_cnfg_01, chg_cnfg_02, chg_cnfg_04);
  226. pr_info("%s: CHG_CNFG_09(0x%x), CHG_CNFG_12(0x%x)\n",
  227. __func__, chg_cnfg_09, chg_cnfg_12);
  228. }
  229. pr_info("%s: vbus_state : 0x%d, chg_dtls : 0x%d\n", __func__, vbus_state, chg_dtls);
  230. /* OVP is higher priority */
  231. if (vbus_state == 0x02) { /* CHGIN_OVLO */
  232. pr_info("%s: vbus ovp\n", __func__);
  233. state = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
  234. if (charger->cable_type == POWER_SUPPLY_TYPE_WIRELESS) {
  235. retry_cnt = 0;
  236. do {
  237. msleep(50);
  238. vbus_state = max77823_get_vbus_state(charger);
  239. } while((retry_cnt++ < 2) && (vbus_state == 0x02));
  240. if (vbus_state == 0x02) {
  241. state = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
  242. panic("wpc and ovp");
  243. } else
  244. state = POWER_SUPPLY_HEALTH_GOOD;
  245. }
  246. } else if (((vbus_state == 0x0) || (vbus_state == 0x01)) &&(chg_dtls & 0x08) && \
  247. (chg_cnfg_00 & MAX77823_MODE_BUCK) && \
  248. (chg_cnfg_00 & MAX77823_MODE_CHGR) && \
  249. (charger->cable_type != POWER_SUPPLY_TYPE_WIRELESS)) {
  250. pr_info("%s: vbus is under\n", __func__);
  251. state = POWER_SUPPLY_HEALTH_UNDERVOLTAGE;
  252. }
  253. }
  254. max77823_dump_reg(charger);
  255. return (int)state;
  256. }
  257. static u8 max77823_get_float_voltage_data(int float_voltage)
  258. {
  259. int voltage = 3650;
  260. int i;
  261. for (i = 0; voltage <= 4700; i++) {
  262. if (float_voltage <= voltage)
  263. break;
  264. voltage += 25;
  265. }
  266. if (float_voltage <= 4340)
  267. return i;
  268. else
  269. return (i+1);
  270. }
  271. static int max77823_get_input_current(struct max77823_charger_data *charger)
  272. {
  273. u8 reg_data;
  274. int get_current, quotient, remainder;
  275. if (charger->cable_type == POWER_SUPPLY_TYPE_WIRELESS) {
  276. max77823_read_reg(charger->i2c,
  277. MAX77823_CHG_CNFG_10, &reg_data);
  278. if (reg_data <= 3)
  279. get_current = 60;
  280. else
  281. get_current = 60 + (reg_data - 3) * 20;
  282. } else {
  283. max77823_read_reg(charger->i2c,
  284. MAX77823_CHG_CNFG_09, &reg_data);
  285. quotient = reg_data / 3;
  286. remainder = reg_data % 3;
  287. if (remainder <= 1)
  288. get_current = 100;
  289. else if (remainder == 0)
  290. get_current = quotient * 100;
  291. else if (remainder == 1)
  292. get_current = (quotient * 100) + 33;
  293. else if (remainder == 2)
  294. get_current = (quotient * 100) + 67;
  295. }
  296. return get_current;
  297. }
  298. static void max77823_set_input_current(struct max77823_charger_data *charger,
  299. int input_current)
  300. {
  301. int quotient, remainder;
  302. u8 reg_data;
  303. max77823_read_reg(charger->i2c,
  304. MAX77823_CHG_CNFG_09, &reg_data);
  305. reg_data &= ~MAX77823_CHG_CHGIN_LIM;
  306. if (!input_current) {
  307. max77823_write_reg(charger->i2c,
  308. MAX77823_CHG_CNFG_09, reg_data);
  309. } else {
  310. quotient = input_current / 100;
  311. remainder = input_current % 100;
  312. if (remainder >= 67)
  313. reg_data |= (quotient * 3) + 2;
  314. else if (remainder >= 33)
  315. reg_data |= (quotient * 3) + 1;
  316. else if (remainder < 33)
  317. reg_data |= quotient * 3;
  318. max77823_write_reg(charger->i2c,
  319. MAX77823_CHG_CNFG_09, reg_data);
  320. }
  321. pr_info("%s: reg_data(0x%02x), charging current(%d)\n",
  322. __func__, reg_data, input_current);
  323. }
  324. static void max77823_set_charge_current(struct max77823_charger_data *charger,
  325. int fast_charging_current)
  326. {
  327. int curr_step = 50;
  328. u8 reg_data;
  329. max77823_read_reg(charger->i2c,
  330. MAX77823_CHG_CNFG_02, &reg_data);
  331. reg_data &= ~MAX77823_CHG_CC;
  332. if (!fast_charging_current) {
  333. max77823_write_reg(charger->i2c,
  334. MAX77823_CHG_CNFG_02, reg_data);
  335. } else {
  336. reg_data |= (fast_charging_current / curr_step);
  337. max77823_write_reg(charger->i2c,MAX77823_CHG_CNFG_02, reg_data);
  338. }
  339. pr_info("%s: reg_data(0x%02x), charging current(%d)\n",
  340. __func__, reg_data, fast_charging_current);
  341. }
  342. static void max77823_set_topoff_current(struct max77823_charger_data *charger,
  343. int termination_current,
  344. int termination_time)
  345. {
  346. u8 reg_data;
  347. if (termination_current >= 350)
  348. reg_data = 0x07;
  349. else if (termination_current >= 300)
  350. reg_data = 0x06;
  351. else if (termination_current >= 250)
  352. reg_data = 0x05;
  353. else if (termination_current >= 200)
  354. reg_data = 0x04;
  355. else if (termination_current >= 175)
  356. reg_data = 0x03;
  357. else if (termination_current >= 150)
  358. reg_data = 0x02;
  359. else if (termination_current >= 125)
  360. reg_data = 0x01;
  361. else
  362. reg_data = 0x00;
  363. /* the unit of timeout is second*/
  364. termination_time = termination_time / 60;
  365. reg_data |= ((termination_time / 10) << 3);
  366. pr_info("%s: reg_data(0x%02x), topoff(%d), time(%d)\n",
  367. __func__, reg_data, termination_current,
  368. termination_time/60);
  369. max77823_update_reg(charger->i2c, MAX77823_CHG_CNFG_03,
  370. reg_data,
  371. MAX77823_CHG_TO_ITH | MAX77823_CHG_TO_TIME);
  372. }
  373. static void max77823_set_charger_state(struct max77823_charger_data *charger,
  374. int enable)
  375. {
  376. u8 reg_data;
  377. max77823_read_reg(charger->i2c, MAX77823_CHG_CNFG_00, &reg_data);
  378. if (enable)
  379. reg_data |= (0x05);
  380. else
  381. reg_data &= ~(0x05);
  382. pr_debug("%s : CHG_CNFG_00(0x%02x)\n", __func__, reg_data);
  383. max77823_write_reg(charger->i2c,MAX77823_CHG_CNFG_00, reg_data);
  384. }
  385. static void max77823_charger_function_control(
  386. struct max77823_charger_data *charger)
  387. {
  388. const int usb_charging_current = charger->pdata->charging_current[
  389. POWER_SUPPLY_TYPE_USB].fast_charging_current;
  390. int set_charging_current, set_charging_current_max;
  391. u8 chg_cnfg_00;
  392. pr_info("####%s####\n", __func__);
  393. if (charger->cable_type == POWER_SUPPLY_TYPE_BATTERY ||
  394. charger->cable_type == POWER_SUPPLY_TYPE_OTG) {
  395. charger->is_charging = false;
  396. charger->aicl_on = false;
  397. set_charging_current = 0;
  398. set_charging_current_max =
  399. charger->pdata->charging_current[
  400. POWER_SUPPLY_TYPE_USB].input_current_limit;
  401. max77823_read_reg(charger->i2c,
  402. MAX77823_CHG_CNFG_00, &chg_cnfg_00);
  403. chg_cnfg_00 &= ~(CHG_CNFG_00_CHG_MASK
  404. | CHG_CNFG_00_OTG_MASK
  405. | CHG_CNFG_00_BUCK_MASK
  406. | CHG_CNFG_00_BOOST_MASK
  407. | CHG_CNFG_00_DIS_MUIC_CTRL_MASK);
  408. if (charger->cable_type == POWER_SUPPLY_TYPE_OTG) {
  409. chg_cnfg_00 |= (CHG_CNFG_00_OTG_MASK
  410. | CHG_CNFG_00_BOOST_MASK
  411. | CHG_CNFG_00_DIS_MUIC_CTRL_MASK);
  412. } else {
  413. set_charging_current_max =
  414. charger->pdata->charging_current[
  415. POWER_SUPPLY_TYPE_USB].input_current_limit;
  416. }
  417. max77823_write_reg(charger->i2c,
  418. MAX77823_CHG_CNFG_00, chg_cnfg_00);
  419. } else {
  420. charger->is_charging = true;
  421. charger->charging_current_max =
  422. charger->pdata->charging_current
  423. [charger->cable_type].input_current_limit;
  424. charger->charging_current =
  425. charger->pdata->charging_current
  426. [charger->cable_type].fast_charging_current;
  427. /* decrease the charging current according to siop level */
  428. set_charging_current =
  429. charger->charging_current * charger->siop_level / 100;
  430. if (set_charging_current > 0 &&
  431. set_charging_current < usb_charging_current)
  432. set_charging_current = usb_charging_current;
  433. set_charging_current_max =
  434. charger->charging_current_max;
  435. if (charger->siop_level < 100 &&
  436. set_charging_current_max > SIOP_INPUT_LIMIT_CURRENT) {
  437. set_charging_current_max = SIOP_INPUT_LIMIT_CURRENT;
  438. if (set_charging_current > SIOP_CHARGING_LIMIT_CURRENT)
  439. set_charging_current = SIOP_CHARGING_LIMIT_CURRENT;
  440. }
  441. }
  442. max77823_set_charger_state(charger, charger->is_charging);
  443. /* if battery full, only disable charging */
  444. if ((charger->status == POWER_SUPPLY_STATUS_CHARGING) ||
  445. (charger->status == POWER_SUPPLY_STATUS_DISCHARGING)) {
  446. /* current setting */
  447. max77823_set_charge_current(charger,
  448. set_charging_current);
  449. /* if battery is removed, disable input current and reenable input current
  450. * to enable buck always */
  451. max77823_set_input_current(charger,
  452. set_charging_current_max);
  453. max77823_set_topoff_current(charger,
  454. charger->pdata->charging_current[
  455. charger->cable_type].full_check_current_1st,
  456. charger->pdata->charging_current[
  457. charger->cable_type].full_check_current_2nd);
  458. }
  459. pr_info("charging = %d, fc = %d, il = %d, t1 = %d, t2 = %d, cable = %d\n",
  460. charger->is_charging,
  461. charger->charging_current,
  462. charger->charging_current_max,
  463. charger->pdata->charging_current[charger->cable_type].full_check_current_1st,
  464. charger->pdata->charging_current[charger->cable_type].full_check_current_2nd,
  465. charger->cable_type);
  466. max77823_test_read(charger);
  467. }
  468. static void max77823_charger_initialize(struct max77823_charger_data *charger)
  469. {
  470. u8 reg_data;
  471. pr_info("%s\n", __func__);
  472. /* unmasked: CHGIN_I, WCIN_I, BATP_I, BYP_I */
  473. max77823_write_reg(charger->i2c, MAX77823_CHG_INT_MASK, 0x9a);
  474. /* unlock charger setting protect */
  475. reg_data = (0x03 << 2);
  476. max77823_write_reg(charger->i2c, MAX77823_CHG_CNFG_06, reg_data);
  477. /*
  478. * fast charge timer disable
  479. * restart threshold disable
  480. * pre-qual charge enable(default)
  481. */
  482. reg_data = (0x0 << 0) | (0x03 << 4);
  483. max77823_write_reg(charger->i2c, MAX77823_CHG_CNFG_01, reg_data);
  484. /*
  485. * charge current 466mA(default)
  486. * otg current limit 900mA
  487. */
  488. max77823_read_reg(charger->i2c, MAX77823_CHG_CNFG_02, &reg_data);
  489. reg_data |= (1 << 7);
  490. max77823_write_reg(charger->i2c, MAX77823_CHG_CNFG_02, reg_data);
  491. /*
  492. * top off current 100mA
  493. * top off timer 40min
  494. */
  495. reg_data = (0x04 << 3);
  496. max77823_update_reg(charger->i2c, MAX77823_CHG_CNFG_03,
  497. reg_data,
  498. MAX77823_CHG_TO_ITH | MAX77823_CHG_TO_TIME);
  499. /*
  500. * cv voltage 4.2V or 4.35V
  501. * MINVSYS 3.6V(default)
  502. */
  503. reg_data = max77823_get_float_voltage_data(charger->pdata->chg_float_voltage);
  504. max77823_update_reg(charger->i2c, MAX77823_CHG_CNFG_04,
  505. (reg_data << CHG_CNFG_04_CHG_CV_PRM_SHIFT),
  506. CHG_CNFG_04_CHG_CV_PRM_MASK);
  507. max77823_read_reg(charger->i2c, MAX77823_CHG_CNFG_04, &reg_data);
  508. pr_info("%s: battery cv voltage 0x%x\n", __func__, reg_data);
  509. }
  510. static int max77823_chg_get_property(struct power_supply *psy,
  511. enum power_supply_property psp,
  512. union power_supply_propval *val)
  513. {
  514. struct max77823_charger_data *charger =
  515. container_of(psy, struct max77823_charger_data, psy_chg);
  516. u8 reg_data;
  517. switch (psp) {
  518. case POWER_SUPPLY_PROP_ONLINE:
  519. val->intval = POWER_SUPPLY_TYPE_BATTERY;
  520. if (max77823_read_reg(charger->i2c,
  521. MAX77823_CHG_INT_OK, &reg_data) == 0) {
  522. pr_info("%s: reg_data(%d)\n", __func__, reg_data);
  523. if (reg_data & MAX77823_WCIN_OK) {
  524. val->intval = POWER_SUPPLY_TYPE_WIRELESS;
  525. charger->wc_w_state = 1;
  526. } else if (reg_data & MAX77823_CHGIN_OK) {
  527. val->intval = POWER_SUPPLY_TYPE_MAINS;
  528. }
  529. }
  530. break;
  531. case POWER_SUPPLY_PROP_STATUS:
  532. val->intval = max77823_get_charger_state(charger);
  533. break;
  534. case POWER_SUPPLY_PROP_CHARGE_TYPE:
  535. if (!charger->is_charging)
  536. val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE;
  537. else if (charger->aicl_on)
  538. {
  539. val->intval = POWER_SUPPLY_CHARGE_TYPE_SLOW;
  540. pr_info("%s: slow-charging mode\n", __func__);
  541. }
  542. else
  543. val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST;
  544. break;
  545. case POWER_SUPPLY_PROP_HEALTH:
  546. val->intval = max77823_get_charging_health(charger);
  547. break;
  548. case POWER_SUPPLY_PROP_CURRENT_MAX:
  549. val->intval = charger->charging_current_max;
  550. break;
  551. case POWER_SUPPLY_PROP_CURRENT_AVG:
  552. val->intval = charger->charging_current;
  553. break;
  554. case POWER_SUPPLY_PROP_CURRENT_NOW:
  555. val->intval = max77823_get_input_current(charger);
  556. pr_debug("%s : set-current(%dmA), current now(%dmA)\n",
  557. __func__, charger->charging_current, val->intval);
  558. break;
  559. case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
  560. break;
  561. default:
  562. return -EINVAL;
  563. }
  564. return 0;
  565. }
  566. static int max77823_chg_set_property(struct power_supply *psy,
  567. enum power_supply_property psp,
  568. const union power_supply_propval *val)
  569. {
  570. struct max77823_charger_data *charger =
  571. container_of(psy, struct max77823_charger_data, psy_chg);
  572. union power_supply_propval value;
  573. int set_charging_current_max;
  574. const int usb_charging_current = charger->pdata->charging_current[
  575. POWER_SUPPLY_TYPE_USB].fast_charging_current;
  576. switch (psp) {
  577. /* val->intval : type */
  578. case POWER_SUPPLY_PROP_STATUS:
  579. charger->status = val->intval;
  580. break;
  581. case POWER_SUPPLY_PROP_ONLINE:
  582. if (val->intval == POWER_SUPPLY_TYPE_POWER_SHARING) {
  583. psy_do_property("ps", get,
  584. POWER_SUPPLY_PROP_STATUS, value);
  585. if (value.intval) {
  586. max77823_update_reg(charger->i2c, MAX77823_CHG_CNFG_00,
  587. (1 << CHG_CNFG_00_OTG_SHIFT) | (1 << CHG_CNFG_00_BOOST_SHIFT),
  588. CHG_CNFG_00_OTG_MASK | CHG_CNFG_00_BOOST_MASK);
  589. } else {
  590. max77823_update_reg(charger->i2c, MAX77823_CHG_CNFG_00,
  591. 0,
  592. CHG_CNFG_00_OTG_MASK | CHG_CNFG_00_BOOST_MASK);
  593. }
  594. break;
  595. }
  596. charger->cable_type = val->intval;
  597. max77823_charger_function_control(charger);
  598. break;
  599. /* val->intval : input charging current */
  600. case POWER_SUPPLY_PROP_CURRENT_MAX:
  601. charger->charging_current_max = val->intval;
  602. break;
  603. /* val->intval : charging current */
  604. case POWER_SUPPLY_PROP_CURRENT_AVG:
  605. charger->charging_current = val->intval;
  606. break;
  607. /* val->intval : charging current */
  608. case POWER_SUPPLY_PROP_CURRENT_NOW:
  609. charger->charging_current = val->intval;
  610. max77823_set_charge_current(charger,
  611. charger->charging_current);
  612. break;
  613. case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
  614. charger->siop_level = val->intval;
  615. if (charger->is_charging) {
  616. /* decrease the charging current according to siop level */
  617. int current_now =
  618. charger->charging_current * val->intval / 100;
  619. /* do forced set charging current */
  620. if (current_now > 0 &&
  621. current_now < usb_charging_current)
  622. current_now = usb_charging_current;
  623. if (charger->cable_type == POWER_SUPPLY_TYPE_MAINS) {
  624. if (charger->siop_level < 100 ) {
  625. set_charging_current_max = SIOP_INPUT_LIMIT_CURRENT;
  626. } else {
  627. set_charging_current_max =
  628. charger->charging_current_max;
  629. }
  630. if (charger->siop_level < 100 &&
  631. current_now > SIOP_CHARGING_LIMIT_CURRENT)
  632. current_now = SIOP_CHARGING_LIMIT_CURRENT;
  633. max77823_set_input_current(charger,
  634. set_charging_current_max);
  635. }
  636. max77823_set_charge_current(charger, current_now);
  637. }
  638. break;
  639. default:
  640. return -EINVAL;
  641. }
  642. return 0;
  643. }
  644. static int max77823_debugfs_show(struct seq_file *s, void *data)
  645. {
  646. struct max77823_charger_data *charger = s->private;
  647. u8 reg;
  648. u8 reg_data;
  649. seq_printf(s, "MAX77823 CHARGER IC :\n");
  650. seq_printf(s, "===================\n");
  651. for (reg = 0xB0; reg <= 0xC3; reg++) {
  652. max77823_read_reg(charger->i2c, reg, &reg_data);
  653. seq_printf(s, "0x%02x:\t0x%02x\n", reg, reg_data);
  654. }
  655. seq_printf(s, "\n");
  656. return 0;
  657. }
  658. static int max77823_debugfs_open(struct inode *inode, struct file *file)
  659. {
  660. return single_open(file, max77823_debugfs_show, inode->i_private);
  661. }
  662. static const struct file_operations max77823_debugfs_fops = {
  663. .open = max77823_debugfs_open,
  664. .read = seq_read,
  665. .llseek = seq_lseek,
  666. .release = single_release,
  667. };
  668. static void max77823_chg_isr_work(struct work_struct *work)
  669. {
  670. struct max77823_charger_data *charger =
  671. container_of(work, struct max77823_charger_data, isr_work.work);
  672. union power_supply_propval val;
  673. if (charger->pdata->full_check_type ==
  674. SEC_BATTERY_FULLCHARGED_CHGINT) {
  675. val.intval = max77823_get_charger_state(charger);
  676. switch (val.intval) {
  677. case POWER_SUPPLY_STATUS_DISCHARGING:
  678. pr_err("%s: Interrupted but Discharging\n", __func__);
  679. break;
  680. case POWER_SUPPLY_STATUS_NOT_CHARGING:
  681. pr_err("%s: Interrupted but NOT Charging\n", __func__);
  682. break;
  683. case POWER_SUPPLY_STATUS_FULL:
  684. pr_info("%s: Interrupted by Full\n", __func__);
  685. psy_do_property("battery", set,
  686. POWER_SUPPLY_PROP_STATUS, val);
  687. break;
  688. case POWER_SUPPLY_STATUS_CHARGING:
  689. pr_err("%s: Interrupted but Charging\n", __func__);
  690. break;
  691. case POWER_SUPPLY_STATUS_UNKNOWN:
  692. default:
  693. pr_err("%s: Invalid Charger Status\n", __func__);
  694. break;
  695. }
  696. }
  697. if (charger->pdata->ovp_uvlo_check_type ==
  698. SEC_BATTERY_OVP_UVLO_CHGINT) {
  699. val.intval = max77823_get_charging_health(charger);
  700. switch (val.intval) {
  701. case POWER_SUPPLY_HEALTH_OVERHEAT:
  702. case POWER_SUPPLY_HEALTH_COLD:
  703. pr_err("%s: Interrupted but Hot/Cold\n", __func__);
  704. break;
  705. case POWER_SUPPLY_HEALTH_DEAD:
  706. pr_err("%s: Interrupted but Dead\n", __func__);
  707. break;
  708. case POWER_SUPPLY_HEALTH_OVERVOLTAGE:
  709. case POWER_SUPPLY_HEALTH_UNDERVOLTAGE:
  710. pr_info("%s: Interrupted by OVP/UVLO\n", __func__);
  711. psy_do_property("battery", set,
  712. POWER_SUPPLY_PROP_HEALTH, val);
  713. break;
  714. case POWER_SUPPLY_HEALTH_UNSPEC_FAILURE:
  715. pr_err("%s: Interrupted but Unspec\n", __func__);
  716. break;
  717. case POWER_SUPPLY_HEALTH_GOOD:
  718. pr_err("%s: Interrupted but Good\n", __func__);
  719. break;
  720. case POWER_SUPPLY_HEALTH_UNKNOWN:
  721. default:
  722. pr_err("%s: Invalid Charger Health\n", __func__);
  723. break;
  724. }
  725. }
  726. }
  727. static irqreturn_t max77823_chg_irq_thread(int irq, void *irq_data)
  728. {
  729. struct max77823_charger_data *charger = irq_data;
  730. pr_info("%s : Charger interrup occured\n",
  731. __func__);
  732. if ((charger->pdata->full_check_type ==
  733. SEC_BATTERY_FULLCHARGED_CHGINT) ||
  734. (charger->pdata->ovp_uvlo_check_type ==
  735. SEC_BATTERY_OVP_UVLO_CHGINT))
  736. schedule_delayed_work(&charger->isr_work, 0);
  737. return IRQ_HANDLED;
  738. }
  739. static void wpc_detect_work(struct work_struct *work)
  740. {
  741. struct max77823_charger_data *charger = container_of(work,
  742. struct max77823_charger_data,
  743. wpc_work.work);
  744. int wc_w_state;
  745. int retry_cnt;
  746. union power_supply_propval value;
  747. u8 reg_data;
  748. pr_debug("%s\n", __func__);
  749. max77823_read_reg(charger->i2c,
  750. MAX77823_CHG_INT_MASK, &reg_data);
  751. reg_data &= ~(1 << 5);
  752. max77823_write_reg(charger->i2c,
  753. MAX77823_CHG_INT_MASK, reg_data);
  754. /* check and unlock */
  755. check_charger_unlock_state(charger);
  756. retry_cnt = 0;
  757. do {
  758. max77823_read_reg(charger->i2c,
  759. MAX77823_CHG_INT_OK, &reg_data);
  760. wc_w_state = (reg_data & MAX77823_WCIN_OK)
  761. >> MAX77823_WCIN_OK_SHIFT;
  762. msleep(50);
  763. } while((retry_cnt++ < 2) && (wc_w_state == 0));
  764. if ((charger->wc_w_state == 0) && (wc_w_state == 1)) {
  765. value.intval = 1;
  766. psy_do_property("wireless", set,
  767. POWER_SUPPLY_PROP_ONLINE, value);
  768. value.intval = POWER_SUPPLY_TYPE_WIRELESS;
  769. pr_info("%s: wpc activated, set V_INT as PN\n",
  770. __func__);
  771. } else if ((charger->wc_w_state == 1) && (wc_w_state == 0)) {
  772. if (!charger->is_charging)
  773. max77823_set_charger_state(charger, true);
  774. retry_cnt = 0;
  775. do {
  776. max77823_read_reg(charger->i2c,
  777. MAX77823_CHG_DETAILS_01, &reg_data);
  778. reg_data = ((reg_data & MAX77823_CHG_DTLS)
  779. >> MAX77823_CHG_DTLS_SHIFT);
  780. msleep(50);
  781. } while((retry_cnt++ < 2) && (reg_data == 0x8));
  782. pr_info("%s: reg_data: 0x%x, charging: %d\n", __func__,
  783. reg_data, charger->is_charging);
  784. if (!charger->is_charging)
  785. max77823_set_charger_state(charger, false);
  786. if ((reg_data != 0x08)
  787. && (charger->cable_type == POWER_SUPPLY_TYPE_WIRELESS)) {
  788. pr_info("%s: wpc uvlo, but charging\n", __func__);
  789. queue_delayed_work(charger->wqueue, &charger->wpc_work,
  790. msecs_to_jiffies(500));
  791. return;
  792. } else {
  793. value.intval = 0;
  794. psy_do_property("wireless", set,
  795. POWER_SUPPLY_PROP_ONLINE, value);
  796. pr_info("%s: wpc deactivated, set V_INT as PD\n",
  797. __func__);
  798. }
  799. }
  800. pr_info("%s: w(%d to %d)\n", __func__,
  801. charger->wc_w_state, wc_w_state);
  802. charger->wc_w_state = wc_w_state;
  803. wake_unlock(&charger->wpc_wake_lock);
  804. }
  805. static irqreturn_t wpc_charger_irq(int irq, void *data)
  806. {
  807. struct max77823_charger_data *charger = data;
  808. unsigned long delay;
  809. u8 reg_data;
  810. max77823_read_reg(charger->i2c,
  811. MAX77823_CHG_INT_MASK, &reg_data);
  812. reg_data |= (1 << 5);
  813. max77823_write_reg(charger->i2c,
  814. MAX77823_CHG_INT_MASK, reg_data);
  815. wake_lock(&charger->wpc_wake_lock);
  816. #ifdef CONFIG_SAMSUNG_BATTERY_FACTORY
  817. delay = msecs_to_jiffies(0);
  818. #else
  819. if (charger->wc_w_state)
  820. delay = msecs_to_jiffies(500);
  821. else
  822. delay = msecs_to_jiffies(0);
  823. #endif
  824. queue_delayed_work(charger->wqueue, &charger->wpc_work,
  825. delay);
  826. return IRQ_HANDLED;
  827. }
  828. static irqreturn_t max77823_bypass_irq(int irq, void *data)
  829. {
  830. struct max77823_charger_data *charger = data;
  831. u8 dtls_02;
  832. u8 byp_dtls;
  833. u8 chg_cnfg_00;
  834. u8 vbus_state;
  835. pr_info("%s: irq(%d)\n", __func__, irq);
  836. /* check and unlock */
  837. check_charger_unlock_state(charger);
  838. max77823_read_reg(charger->i2c,
  839. MAX77823_CHG_DETAILS_02,
  840. &dtls_02);
  841. byp_dtls = ((dtls_02 & MAX77823_BYP_DTLS) >>
  842. MAX77823_BYP_DTLS_SHIFT);
  843. pr_info("%s: BYP_DTLS(0x%02x)\n", __func__, byp_dtls);
  844. vbus_state = max77823_get_vbus_state(charger);
  845. if (byp_dtls & 0x1) {
  846. pr_info("%s: bypass overcurrent limit\n", __func__);
  847. #ifdef CONFIG_USB_HOST_NOTIFY
  848. sec_otg_notify(HNOTIFY_OVERCURRENT);
  849. #endif
  850. /* disable the register values just related to OTG and
  851. keep the values about the charging */
  852. max77823_read_reg(charger->i2c,
  853. MAX77823_CHG_CNFG_00, &chg_cnfg_00);
  854. chg_cnfg_00 &= ~(CHG_CNFG_00_OTG_MASK
  855. | CHG_CNFG_00_BOOST_MASK
  856. | CHG_CNFG_00_DIS_MUIC_CTRL_MASK);
  857. max77823_write_reg(charger->i2c,
  858. MAX77823_CHG_CNFG_00,
  859. chg_cnfg_00);
  860. }
  861. return IRQ_HANDLED;
  862. }
  863. static void max77823_chgin_isr_work(struct work_struct *work)
  864. {
  865. struct max77823_charger_data *charger = container_of(work,
  866. struct max77823_charger_data, chgin_work);
  867. u8 chgin_dtls, chg_dtls, chg_cnfg_00, reg_data;
  868. u8 prev_chgin_dtls = 0xff;
  869. int battery_health;
  870. union power_supply_propval value;
  871. int stable_count = 0;
  872. wake_lock(&charger->chgin_wake_lock);
  873. max77823_read_reg(charger->i2c,
  874. MAX77823_CHG_INT_MASK, &reg_data);
  875. reg_data |= (1 << 6);
  876. max77823_write_reg(charger->i2c,
  877. MAX77823_CHG_INT_MASK, reg_data);
  878. while (1) {
  879. psy_do_property("battery", get,
  880. POWER_SUPPLY_PROP_HEALTH, value);
  881. battery_health = value.intval;
  882. max77823_read_reg(charger->i2c,
  883. MAX77823_CHG_DETAILS_00,
  884. &chgin_dtls);
  885. chgin_dtls = ((chgin_dtls & MAX77823_CHGIN_DTLS) >>
  886. MAX77823_CHGIN_DTLS_SHIFT);
  887. max77823_read_reg(charger->i2c,
  888. MAX77823_CHG_DETAILS_01, &chg_dtls);
  889. chg_dtls = ((chg_dtls & MAX77823_CHG_DTLS) >>
  890. MAX77823_CHG_DTLS_SHIFT);
  891. max77823_read_reg(charger->i2c,
  892. MAX77823_CHG_CNFG_00, &chg_cnfg_00);
  893. if (prev_chgin_dtls == chgin_dtls)
  894. stable_count++;
  895. else
  896. stable_count = 0;
  897. if (stable_count > 10) {
  898. pr_info("%s: irq(%d), chgin(0x%x), chg_dtls(0x%x) prev 0x%x\n",
  899. __func__, charger->irq_chgin,
  900. chgin_dtls, chg_dtls, prev_chgin_dtls);
  901. if (charger->is_charging) {
  902. if ((chgin_dtls == 0x02) && \
  903. (battery_health != POWER_SUPPLY_HEALTH_OVERVOLTAGE)) {
  904. pr_info("%s: charger is over voltage\n",
  905. __func__);
  906. value.intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
  907. psy_do_property("battery", set,
  908. POWER_SUPPLY_PROP_HEALTH, value);
  909. } else if (((chgin_dtls == 0x0) || (chgin_dtls == 0x01)) &&(chg_dtls & 0x08) && \
  910. (chg_cnfg_00 & MAX77823_MODE_BUCK) && \
  911. (chg_cnfg_00 & MAX77823_MODE_CHGR) && \
  912. (battery_health != POWER_SUPPLY_HEALTH_UNDERVOLTAGE) && \
  913. (charger->cable_type != POWER_SUPPLY_TYPE_WIRELESS)) {
  914. pr_info("%s, vbus_state : 0x%d, chg_state : 0x%d\n", __func__, chgin_dtls, chg_dtls);
  915. pr_info("%s: vBus is undervoltage\n", __func__);
  916. value.intval = POWER_SUPPLY_HEALTH_UNDERVOLTAGE;
  917. psy_do_property("battery", set,
  918. POWER_SUPPLY_PROP_HEALTH, value);
  919. } else if ((battery_health == \
  920. POWER_SUPPLY_HEALTH_OVERVOLTAGE) &&
  921. (chgin_dtls != 0x02)) {
  922. pr_info("%s: vbus_state : 0x%d, chg_state : 0x%d\n", __func__, chgin_dtls, chg_dtls);
  923. pr_info("%s: overvoltage->normal\n", __func__);
  924. value.intval = POWER_SUPPLY_HEALTH_GOOD;
  925. psy_do_property("battery", set,
  926. POWER_SUPPLY_PROP_HEALTH, value);
  927. } else if ((battery_health == \
  928. POWER_SUPPLY_HEALTH_UNDERVOLTAGE) &&
  929. !((chgin_dtls == 0x0) || (chgin_dtls == 0x01))){
  930. pr_info("%s: vbus_state : 0x%d, chg_state : 0x%d\n", __func__, chgin_dtls, chg_dtls);
  931. pr_info("%s: undervoltage->normal\n", __func__);
  932. value.intval = POWER_SUPPLY_HEALTH_GOOD;
  933. psy_do_property("battery", set,
  934. POWER_SUPPLY_PROP_HEALTH, value);
  935. max77823_set_input_current(charger,
  936. charger->charging_current_max);
  937. }
  938. }
  939. break;
  940. }
  941. prev_chgin_dtls = chgin_dtls;
  942. msleep(100);
  943. }
  944. max77823_read_reg(charger->i2c,
  945. MAX77823_CHG_INT_MASK, &reg_data);
  946. reg_data &= ~(1 << 6);
  947. max77823_write_reg(charger->i2c,
  948. MAX77823_CHG_INT_MASK, reg_data);
  949. wake_unlock(&charger->chgin_wake_lock);
  950. }
  951. static irqreturn_t max77823_chgin_irq(int irq, void *data)
  952. {
  953. struct max77823_charger_data *charger = data;
  954. queue_work(charger->wqueue, &charger->chgin_work);
  955. return IRQ_HANDLED;
  956. }
  957. /* register chgin isr after sec_battery_probe */
  958. static void max77823_chgin_init_work(struct work_struct *work)
  959. {
  960. struct max77823_charger_data *charger = container_of(work,
  961. struct max77823_charger_data,
  962. chgin_init_work.work);
  963. int ret;
  964. pr_info("%s \n", __func__);
  965. ret = request_threaded_irq(charger->irq_chgin, NULL,
  966. max77823_chgin_irq, 0, "chgin-irq", charger);
  967. if (ret < 0) {
  968. pr_err("%s: fail to request chgin IRQ: %d: %d\n",
  969. __func__, charger->irq_chgin, ret);
  970. }
  971. }
  972. #ifdef CONFIG_OF
  973. static int sec_charger_read_u32_index_dt(const struct device_node *np,
  974. const char *propname,
  975. u32 index, u32 *out_value)
  976. {
  977. struct property *prop = of_find_property(np, propname, NULL);
  978. u32 len = (index + 1) * sizeof(*out_value);
  979. if (!prop)
  980. return (-EINVAL);
  981. if (!prop->value)
  982. return (-ENODATA);
  983. if (len > prop->length)
  984. return (-EOVERFLOW);
  985. *out_value = be32_to_cpup(((__be32 *)prop->value) + index);
  986. return 0;
  987. }
  988. static int max77823_charger_parse_dt(struct max77823_charger_data *charger)
  989. {
  990. struct device_node *np = of_find_node_by_name(NULL, "charger");
  991. sec_battery_platform_data_t *pdata = charger->pdata;
  992. int ret = 0;
  993. int i, len;
  994. const u32 *p;
  995. if (np == NULL) {
  996. pr_err("%s np NULL\n", __func__);
  997. } else {
  998. ret = of_property_read_u32(np, "battery,chg_float_voltage",
  999. &pdata->chg_float_voltage);
  1000. ret = of_property_read_u32(np, "battery,ovp_uvlo_check_type",
  1001. &pdata->ovp_uvlo_check_type);
  1002. ret = of_property_read_u32(np, "battery,full_check_type",
  1003. &pdata->full_check_type);
  1004. p = of_get_property(np, "battery,input_current_limit", &len);
  1005. len = len / sizeof(u32);
  1006. pdata->charging_current = kzalloc(sizeof(sec_charging_current_t) * len,
  1007. GFP_KERNEL);
  1008. for(i = 0; i < len; i++) {
  1009. ret = sec_charger_read_u32_index_dt(np,
  1010. "battery,input_current_limit", i,
  1011. &pdata->charging_current[i].input_current_limit);
  1012. ret = sec_charger_read_u32_index_dt(np,
  1013. "battery,fast_charging_current", i,
  1014. &pdata->charging_current[i].fast_charging_current);
  1015. ret = sec_charger_read_u32_index_dt(np,
  1016. "battery,full_check_current_1st", i,
  1017. &pdata->charging_current[i].full_check_current_1st);
  1018. ret = sec_charger_read_u32_index_dt(np,
  1019. "battery,full_check_current_2nd", i,
  1020. &pdata->charging_current[i].full_check_current_2nd);
  1021. }
  1022. }
  1023. return ret;
  1024. }
  1025. #endif
  1026. static int __devinit max77823_charger_probe(struct platform_device *pdev)
  1027. {
  1028. struct max77823_dev *max77823 = dev_get_drvdata(pdev->dev.parent);
  1029. struct max77823_platform_data *pdata = dev_get_platdata(max77823->dev);
  1030. struct max77823_charger_data *charger;
  1031. int ret = 0;
  1032. u8 reg_data;
  1033. pr_info("%s: Max77823 Charger Driver Loading\n", __func__);
  1034. charger = kzalloc(sizeof(*charger), GFP_KERNEL);
  1035. if (!charger)
  1036. return -ENOMEM;
  1037. pdata->charger_data = kzalloc(sizeof(sec_battery_platform_data_t), GFP_KERNEL);
  1038. if (!pdata->charger_data)
  1039. return -ENOMEM;
  1040. mutex_init(&charger->charger_mutex);
  1041. charger->dev = &pdev->dev;
  1042. charger->i2c = max77823->charger;
  1043. charger->pmic_i2c = max77823->i2c;
  1044. charger->pdata = pdata->charger_data;
  1045. charger->aicl_on = false;
  1046. charger->siop_level = 100;
  1047. charger->max77823_pdata = pdata;
  1048. #if defined(CONFIG_OF)
  1049. ret = max77823_charger_parse_dt(charger);
  1050. if (ret < 0) {
  1051. pr_err("%s not found charger dt! ret[%d]\n",
  1052. __func__, ret);
  1053. }
  1054. #endif
  1055. platform_set_drvdata(pdev, charger);
  1056. charger->psy_chg.name = "max77823-charger";
  1057. charger->psy_chg.type = POWER_SUPPLY_TYPE_UNKNOWN;
  1058. charger->psy_chg.get_property = max77823_chg_get_property;
  1059. charger->psy_chg.set_property = max77823_chg_set_property;
  1060. charger->psy_chg.properties = max77823_charger_props;
  1061. charger->psy_chg.num_properties = ARRAY_SIZE(max77823_charger_props);
  1062. max77823_charger_initialize(charger);
  1063. (void) debugfs_create_file("max77823-regs",
  1064. S_IRUGO, NULL, (void *)charger, &max77823_debugfs_fops);
  1065. charger->wqueue =
  1066. create_singlethread_workqueue(dev_name(&pdev->dev));
  1067. if (!charger->wqueue) {
  1068. pr_err("%s: Fail to Create Workqueue\n", __func__);
  1069. goto err_free;
  1070. }
  1071. wake_lock_init(&charger->chgin_wake_lock, WAKE_LOCK_SUSPEND,
  1072. "charger-chgin");
  1073. INIT_WORK(&charger->chgin_work, max77823_chgin_isr_work);
  1074. INIT_DELAYED_WORK(&charger->chgin_init_work, max77823_chgin_init_work);
  1075. wake_lock_init(&charger->wpc_wake_lock, WAKE_LOCK_SUSPEND,
  1076. "charger-wpc");
  1077. INIT_DELAYED_WORK(&charger->wpc_work, wpc_detect_work);
  1078. ret = power_supply_register(&pdev->dev, &charger->psy_chg);
  1079. if (ret) {
  1080. pr_err("%s: Failed to Register psy_chg\n", __func__);
  1081. goto err_power_supply_register;
  1082. }
  1083. if (charger->pdata->chg_irq) {
  1084. INIT_DELAYED_WORK(&charger->isr_work, max77823_chg_isr_work);
  1085. ret = request_threaded_irq(charger->pdata->chg_irq,
  1086. NULL, max77823_chg_irq_thread,
  1087. charger->pdata->chg_irq_attr,
  1088. "charger-irq", charger);
  1089. if (ret) {
  1090. pr_err("%s: Failed to Reqeust IRQ\n", __func__);
  1091. goto err_irq;
  1092. }
  1093. ret = enable_irq_wake(charger->pdata->chg_irq);
  1094. if (ret < 0)
  1095. pr_err("%s: Failed to Enable Wakeup Source(%d)\n",
  1096. __func__, ret);
  1097. }
  1098. charger->wc_w_irq = pdata->irq_base + MAX77823_CHG_IRQ_WCIN_I;
  1099. ret = request_threaded_irq(charger->wc_w_irq,
  1100. NULL, wpc_charger_irq,
  1101. IRQF_TRIGGER_FALLING,
  1102. "wpc-int", charger);
  1103. if (ret) {
  1104. pr_err("%s: Failed to Reqeust IRQ\n", __func__);
  1105. goto err_wc_irq;
  1106. }
  1107. max77823_read_reg(charger->i2c,
  1108. MAX77823_CHG_INT_OK, &reg_data);
  1109. charger->wc_w_state = (reg_data & MAX77823_WCIN_OK)
  1110. >> MAX77823_WCIN_OK_SHIFT;
  1111. charger->irq_chgin = pdata->irq_base + MAX77823_CHG_IRQ_CHGIN_I;
  1112. /* enable chgin irq after sec_battery_probe */
  1113. queue_delayed_work(charger->wqueue, &charger->chgin_init_work,
  1114. msecs_to_jiffies(3000));
  1115. charger->irq_bypass = pdata->irq_base + MAX77823_CHG_IRQ_BYP_I;
  1116. ret = request_threaded_irq(charger->irq_bypass, NULL,
  1117. max77823_bypass_irq, 0, "bypass-irq", charger);
  1118. if (ret < 0)
  1119. pr_err("%s: fail to request bypass IRQ: %d: %d\n",
  1120. __func__, charger->irq_bypass, ret);
  1121. pr_info("%s: Max77823 Charger Driver Loaded\n", __func__);
  1122. return 0;
  1123. err_wc_irq:
  1124. free_irq(charger->pdata->chg_irq, NULL);
  1125. err_irq:
  1126. power_supply_unregister(&charger->psy_chg);
  1127. err_power_supply_register:
  1128. destroy_workqueue(charger->wqueue);
  1129. err_free:
  1130. kfree(charger);
  1131. return ret;
  1132. }
  1133. static int __devexit max77823_charger_remove(struct platform_device *pdev)
  1134. {
  1135. struct max77823_charger_data *charger =
  1136. platform_get_drvdata(pdev);
  1137. destroy_workqueue(charger->wqueue);
  1138. free_irq(charger->wc_w_irq, NULL);
  1139. free_irq(charger->pdata->chg_irq, NULL);
  1140. power_supply_unregister(&charger->psy_chg);
  1141. kfree(charger);
  1142. return 0;
  1143. }
  1144. #if defined CONFIG_PM
  1145. static int max77823_charger_suspend(struct device *dev)
  1146. {
  1147. return 0;
  1148. }
  1149. static int max77823_charger_resume(struct device *dev)
  1150. {
  1151. return 0;
  1152. }
  1153. #else
  1154. #define max77823_charger_suspend NULL
  1155. #define max77823_charger_resume NULL
  1156. #endif
  1157. static void max77823_charger_shutdown(struct device *dev)
  1158. {
  1159. struct max77823_charger_data *charger =
  1160. dev_get_drvdata(dev);
  1161. u8 reg_data;
  1162. pr_info("%s: MAX77823 Charger driver shutdown\n", __func__);
  1163. if (!charger->i2c) {
  1164. pr_err("%s: no max77823 i2c client\n", __func__);
  1165. return;
  1166. }
  1167. reg_data = 0x04;
  1168. max77823_write_reg(charger->i2c,
  1169. MAX77823_CHG_CNFG_00, reg_data);
  1170. reg_data = 0x19;
  1171. max77823_write_reg(charger->i2c,
  1172. MAX77823_CHG_CNFG_09, reg_data);
  1173. reg_data = 0x19;
  1174. max77823_write_reg(charger->i2c,
  1175. MAX77823_CHG_CNFG_10, reg_data);
  1176. pr_info("func:%s \n", __func__);
  1177. }
  1178. #ifdef CONFIG_OF
  1179. static struct of_device_id max77823_charger_dt_ids[] = {
  1180. { .compatible = "samsung,max77823-charger" },
  1181. { }
  1182. };
  1183. MODULE_DEVICE_TABLE(of, max77823_charger_dt_ids);
  1184. #endif
  1185. static SIMPLE_DEV_PM_OPS(max77823_charger_pm_ops, max77823_charger_suspend,
  1186. max77823_charger_resume);
  1187. static struct platform_driver max77823_charger_driver = {
  1188. .driver = {
  1189. .name = "max77823-charger",
  1190. .owner = THIS_MODULE,
  1191. #ifdef CONFIG_PM
  1192. .pm = &max77823_charger_pm_ops,
  1193. #endif
  1194. .shutdown = max77823_charger_shutdown,
  1195. #ifdef CONFIG_OF
  1196. .of_match_table = max77823_charger_dt_ids,
  1197. #endif
  1198. },
  1199. .probe = max77823_charger_probe,
  1200. .remove = __devexit_p(max77823_charger_remove),
  1201. };
  1202. static int __init max77823_charger_init(void)
  1203. {
  1204. pr_info("%s : \n", __func__);
  1205. return platform_driver_register(&max77823_charger_driver);
  1206. }
  1207. static void __exit max77823_charger_exit(void)
  1208. {
  1209. platform_driver_unregister(&max77823_charger_driver);
  1210. }
  1211. module_init(max77823_charger_init);
  1212. module_exit(max77823_charger_exit);
  1213. MODULE_DESCRIPTION("Samsung MAX77823 Charger Driver");
  1214. MODULE_AUTHOR("Samsung Electronics");
  1215. MODULE_LICENSE("GPL");