max77803_charger.c 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959
  1. /*
  2. * max77803_charger.c
  3. * Samsung max77803 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. #include <linux/mfd/max77803.h>
  13. #include <linux/mfd/max77803-private.h>
  14. #include <linux/of_gpio.h>
  15. #ifdef CONFIG_USB_HOST_NOTIFY
  16. #include <linux/host_notify.h>
  17. #endif
  18. #ifdef CONFIG_FORCE_FAST_CHARGE
  19. #include <linux/fastchg.h>
  20. #endif
  21. #define ENABLE 1
  22. #define DISABLE 0
  23. #define RECOVERY_DELAY 3000
  24. #define RECOVERY_CNT 5
  25. #define REDUCE_CURRENT_STEP 100
  26. #if defined(CONFIG_MACH_HLTEDCM) || defined(CONFIG_MACH_JS01LTEDCM)
  27. #define MINIMUM_INPUT_CURRENT 100
  28. #else
  29. #define MINIMUM_INPUT_CURRENT 300
  30. #endif
  31. #if defined(CONFIG_MACH_FLTESKT)
  32. #define SIOP_INPUT_LIMIT_CURRENT 1001
  33. #else
  34. #define SIOP_INPUT_LIMIT_CURRENT 1200
  35. #endif
  36. #define SIOP_CHARGING_LIMIT_CURRENT 1000
  37. #define SLOW_CHARGING_CURRENT_STANDARD 400
  38. struct max77803_charger_data {
  39. struct max77803_dev *max77803;
  40. struct power_supply psy_chg;
  41. struct workqueue_struct *wqueue;
  42. struct work_struct chgin_work;
  43. struct delayed_work isr_work;
  44. struct delayed_work recovery_work; /* softreg recovery work */
  45. struct delayed_work wpc_work; /* wpc detect work */
  46. struct delayed_work chgin_init_work; /* chgin init work */
  47. /* mutex */
  48. struct mutex irq_lock;
  49. struct mutex ops_lock;
  50. /* wakelock */
  51. struct wakeup_source recovery_ws;
  52. struct wakeup_source wpc_ws;
  53. struct wakeup_source chgin_ws;
  54. unsigned int is_charging;
  55. unsigned int charging_type;
  56. unsigned int battery_state;
  57. unsigned int battery_present;
  58. unsigned int cable_type;
  59. unsigned int charging_current_max;
  60. unsigned int charging_current;
  61. unsigned int vbus_state;
  62. int aicl_on;
  63. int status;
  64. int siop_level;
  65. int irq_bypass;
  66. #if defined(CONFIG_CHARGER_MAX77803)
  67. int irq_batp;
  68. #else
  69. int irq_therm;
  70. #endif
  71. int irq_battery;
  72. int irq_chg;
  73. #if defined(CONFIG_CHARGER_MAX77803)
  74. int irq_wcin;
  75. #endif
  76. int irq_chgin;
  77. /* software regulation */
  78. bool soft_reg_state;
  79. int soft_reg_current;
  80. /* unsufficient power */
  81. bool reg_loop_deted;
  82. #if defined(CONFIG_CHARGER_MAX77803)
  83. /* wireless charge, w(wpc), v(vbus) */
  84. int wc_w_gpio;
  85. int wc_w_irq;
  86. int wc_w_state;
  87. int wc_v_gpio;
  88. int wc_v_irq;
  89. int wc_v_state;
  90. bool wc_pwr_det;
  91. #endif
  92. int soft_reg_recovery_cnt;
  93. int pmic_ver;
  94. int input_curr_limit_step;
  95. int charging_curr_step;
  96. sec_battery_platform_data_t *pdata;
  97. };
  98. static enum power_supply_property sec_charger_props[] = {
  99. POWER_SUPPLY_PROP_STATUS,
  100. POWER_SUPPLY_PROP_CHARGE_TYPE,
  101. POWER_SUPPLY_PROP_HEALTH,
  102. POWER_SUPPLY_PROP_ONLINE,
  103. POWER_SUPPLY_PROP_CURRENT_MAX,
  104. POWER_SUPPLY_PROP_CURRENT_AVG,
  105. POWER_SUPPLY_PROP_CURRENT_NOW,
  106. POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
  107. #ifdef WPC_CHECK_CVPRM_FEATURE
  108. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  109. #endif
  110. };
  111. static void max77803_charger_initialize(struct max77803_charger_data *charger);
  112. static int max77803_get_vbus_state(struct max77803_charger_data *charger);
  113. static int max77803_get_charger_state(struct max77803_charger_data *charger);
  114. /*
  115. static void max77803_dump_reg(struct max77803_charger_data *charger)
  116. {
  117. u8 reg_data;
  118. u32 reg_addr;
  119. pr_info("%s\n", __func__);
  120. for (reg_addr = 0xB0; reg_addr <= 0xC5; reg_addr++) {
  121. max77803_read_reg(charger->max77803->i2c, reg_addr, &reg_data);
  122. pr_info("max77803: c: 0x%02x(0x%02x)\n", reg_addr, reg_data);
  123. }
  124. }
  125. */
  126. static bool max77803_charger_unlock(struct max77803_charger_data *chg_data)
  127. {
  128. struct i2c_client *i2c = chg_data->max77803->i2c;
  129. u8 reg_data;
  130. u8 chgprot;
  131. int retry_cnt = 0;
  132. bool need_init = false;
  133. do {
  134. max77803_read_reg(i2c, MAX77803_CHG_REG_CHG_CNFG_06, &reg_data);
  135. chgprot = ((reg_data & 0x0C) >> 2);
  136. if (chgprot != 0x03) {
  137. pr_err("%s: unlock err, chgprot(0x%x), retry(%d)\n",
  138. __func__, chgprot, retry_cnt);
  139. max77803_write_reg(i2c, MAX77803_CHG_REG_CHG_CNFG_06,
  140. (0x03 << 2));
  141. need_init = true;
  142. msleep(20);
  143. } else {
  144. pr_debug("%s: unlock success, chgprot(0x%x)\n",
  145. __func__, chgprot);
  146. break;
  147. }
  148. } while ((chgprot != 0x03) && (++retry_cnt < 10));
  149. return need_init;
  150. }
  151. static void check_charger_unlock_state(struct max77803_charger_data *chg_data)
  152. {
  153. bool need_reg_init;
  154. pr_debug("%s\n", __func__);
  155. need_reg_init = max77803_charger_unlock(chg_data);
  156. if (need_reg_init) {
  157. pr_err("%s: charger locked state, reg init\n", __func__);
  158. max77803_charger_initialize(chg_data);
  159. }
  160. }
  161. static int max77803_get_battery_present(struct max77803_charger_data *charger)
  162. {
  163. u8 reg_data;
  164. if (max77803_read_reg(charger->max77803->i2c,
  165. MAX77803_CHG_REG_CHG_INT_OK, &reg_data) < 0) {
  166. /* Eventhough there is an error,
  167. don't do power-off */
  168. return 1;
  169. }
  170. pr_debug("%s: CHG_INT_OK(0x%02x)\n", __func__, reg_data);
  171. #if defined(CONFIG_CHARGER_MAX77803)
  172. reg_data = ((reg_data & MAX77803_BATP_OK) >> MAX77803_BATP_OK_SHIFT);
  173. #else
  174. reg_data = ((reg_data & MAX77803_DETBAT) >> MAX77803_DETBAT_SHIFT);
  175. #endif
  176. return reg_data;
  177. }
  178. static void max77803_set_charger_state(struct max77803_charger_data *charger,
  179. int enable)
  180. {
  181. u8 reg_data;
  182. max77803_read_reg(charger->max77803->i2c,
  183. MAX77803_CHG_REG_CHG_CNFG_00, &reg_data);
  184. if (enable)
  185. reg_data |= MAX77803_MODE_CHGR;
  186. else
  187. reg_data &= ~MAX77803_MODE_CHGR;
  188. pr_debug("%s: CHG_CNFG_00(0x%02x)\n", __func__, reg_data);
  189. max77803_write_reg(charger->max77803->i2c,
  190. MAX77803_CHG_REG_CHG_CNFG_00, reg_data);
  191. }
  192. static void max77803_set_buck(struct max77803_charger_data *charger,
  193. int enable)
  194. {
  195. u8 reg_data;
  196. max77803_read_reg(charger->max77803->i2c,
  197. MAX77803_CHG_REG_CHG_CNFG_00, &reg_data);
  198. if (enable)
  199. reg_data |= MAX77803_MODE_BUCK;
  200. else
  201. reg_data &= ~MAX77803_MODE_BUCK;
  202. pr_debug("%s: CHG_CNFG_00(0x%02x)\n", __func__, reg_data);
  203. max77803_write_reg(charger->max77803->i2c,
  204. MAX77803_CHG_REG_CHG_CNFG_00, reg_data);
  205. }
  206. #ifdef WPC_CHECK_CVPRM_FEATURE
  207. static void max77803_check_cvprm(struct max77803_charger_data *charger, u8 data)
  208. {
  209. u8 reg_data;
  210. max77803_read_reg(charger->max77803->i2c,
  211. MAX77803_CHG_REG_CHG_CNFG_04, &reg_data);
  212. if ((reg_data & 0x1f) != data) {
  213. reg_data &= ~(0x1f << 0);
  214. reg_data |= (data << 0);
  215. max77803_write_reg(charger->max77803->i2c,
  216. MAX77803_CHG_REG_CHG_CNFG_04, reg_data);
  217. }
  218. }
  219. static int max77803_get_charge_voltage(struct max77803_charger_data *charger)
  220. {
  221. u8 reg_data;
  222. int charge_voltage;
  223. max77803_read_reg(charger->max77803->i2c,
  224. MAX77803_CHG_REG_CHG_CNFG_04, &reg_data);
  225. reg_data &= 0x1f;
  226. if (reg_data == 0x1c) {
  227. charge_voltage = 4340;
  228. } else {
  229. if (reg_data > 0x1c) {
  230. reg_data -= 1;
  231. }
  232. charge_voltage = (reg_data * 25) + 3650;
  233. }
  234. return charge_voltage;
  235. }
  236. #endif
  237. static void max77803_check_slow_charging(struct max77803_charger_data *charger, int set_current_reg)
  238. {
  239. /* under 500mA, slow rate */
  240. if (set_current_reg <= (SLOW_CHARGING_CURRENT_STANDARD / charger->input_curr_limit_step) &&
  241. (charger->cable_type != POWER_SUPPLY_TYPE_BATTERY)) {
  242. charger->aicl_on = true;
  243. pr_info("%s: slow charging on : set_current_reg(0x%02x), cable type(%d)\n", __func__, set_current_reg, charger->cable_type);
  244. }
  245. else
  246. charger->aicl_on = false;
  247. }
  248. extern unsigned int system_rev;
  249. static void max77803_change_charge_path(struct max77803_charger_data *charger,
  250. int path)
  251. {
  252. u8 cnfg12, ctrl3;
  253. if (path == POWER_SUPPLY_TYPE_WIRELESS) {
  254. cnfg12 = (0 << CHG_CNFG_12_CHGINSEL_SHIFT);
  255. ctrl3 = (1 << CTRL3_JIGSET_SHIFT);
  256. } else {
  257. cnfg12 = (1 << CHG_CNFG_12_CHGINSEL_SHIFT);
  258. ctrl3 = (0 << CTRL3_JIGSET_SHIFT);
  259. }
  260. //#if defined(CONFIG_MACH_HLTEVZW) || defined(CONFIG_MACH_HLTESPR) || defined(CONFIG_MACH_HLTEUSC)
  261. if (system_rev == 0)
  262. ctrl3 = (1 << CTRL3_JIGSET_SHIFT);
  263. //#endif
  264. if (charger->pmic_ver == 0x04)
  265. max77803_update_reg(charger->max77803->muic, MAX77803_MUIC_REG_CTRL3,
  266. ctrl3, CTRL3_JIGSET_MASK);
  267. max77803_update_reg(charger->max77803->i2c, MAX77803_CHG_REG_CHG_CNFG_12,
  268. cnfg12, CHG_CNFG_12_CHGINSEL_MASK);
  269. }
  270. static void max77803_set_input_current(struct max77803_charger_data *charger,
  271. int cur)
  272. {
  273. int set_current_reg, now_current_reg;
  274. int vbus_state, curr_step, delay;
  275. u8 set_reg, reg_data;
  276. int chg_state;
  277. mutex_lock(&charger->ops_lock);
  278. reg_data = (1 << CHGIN_SHIFT);
  279. max77803_update_reg(charger->max77803->i2c, MAX77803_CHG_REG_CHG_INT_MASK, reg_data,
  280. CHGIN_MASK);
  281. if (charger->cable_type == POWER_SUPPLY_TYPE_WIRELESS) {
  282. set_reg = MAX77803_CHG_REG_CHG_CNFG_10;
  283. charger->input_curr_limit_step = 20;
  284. } else {
  285. set_reg = MAX77803_CHG_REG_CHG_CNFG_09;
  286. if (charger->pmic_ver == 0x04)
  287. charger->input_curr_limit_step = 25;
  288. else
  289. charger->input_curr_limit_step = 20;
  290. }
  291. if (cur <= 0) {
  292. /* disable only buck because power onoff test issue */
  293. max77803_write_reg(charger->max77803->i2c,
  294. set_reg, 0x19);
  295. max77803_set_buck(charger, DISABLE);
  296. goto exit;
  297. } else {
  298. max77803_change_charge_path(charger, charger->cable_type);
  299. max77803_set_buck(charger, ENABLE);
  300. }
  301. set_current_reg = cur / charger->input_curr_limit_step;
  302. if (charger->cable_type == POWER_SUPPLY_TYPE_BATTERY)
  303. goto set_input_current;
  304. max77803_read_reg(charger->max77803->i2c,
  305. set_reg, &reg_data);
  306. if (reg_data == set_current_reg) {
  307. /* check uvlo */
  308. while((set_current_reg > (MINIMUM_INPUT_CURRENT / charger->input_curr_limit_step)) && (set_current_reg < 255)) {
  309. vbus_state = max77803_get_vbus_state(charger);
  310. if (((vbus_state == 0x00) || (vbus_state == 0x01)) &&
  311. (charger->cable_type != POWER_SUPPLY_TYPE_WIRELESS)) {
  312. /* UVLO */
  313. set_current_reg -= 5;
  314. if (set_current_reg < (MINIMUM_INPUT_CURRENT / charger->input_curr_limit_step))
  315. set_current_reg = (MINIMUM_INPUT_CURRENT / charger->input_curr_limit_step);
  316. max77803_write_reg(charger->max77803->i2c,
  317. set_reg, set_current_reg);
  318. pr_info("%s: set_current_reg(0x%02x)\n", __func__, set_current_reg);
  319. chg_state = max77803_get_charger_state(charger);
  320. if ((chg_state != POWER_SUPPLY_STATUS_CHARGING) &&
  321. (chg_state != POWER_SUPPLY_STATUS_FULL))
  322. break;
  323. msleep(50);
  324. } else
  325. break;
  326. }
  327. goto exit;
  328. }
  329. if (reg_data == 0) {
  330. now_current_reg = SOFT_CHG_START_CURR / charger->input_curr_limit_step;
  331. max77803_write_reg(charger->max77803->i2c,
  332. set_reg, now_current_reg);
  333. msleep(SOFT_CHG_START_DUR);
  334. } else
  335. now_current_reg = reg_data;
  336. if (cur <= 1000) {
  337. curr_step = 1;
  338. delay = 50;
  339. } else {
  340. curr_step = SOFT_CHG_CURR_STEP / charger->input_curr_limit_step;
  341. delay = SOFT_CHG_STEP_DUR;
  342. }
  343. now_current_reg += (curr_step);
  344. while (now_current_reg < set_current_reg &&
  345. charger->cable_type != POWER_SUPPLY_TYPE_BATTERY)
  346. {
  347. now_current_reg = min(now_current_reg, set_current_reg);
  348. max77803_write_reg(charger->max77803->i2c,
  349. set_reg, now_current_reg);
  350. msleep(delay);
  351. vbus_state = max77803_get_vbus_state(charger);
  352. if (((vbus_state == 0x00) || (vbus_state == 0x01)) &&
  353. (charger->cable_type != POWER_SUPPLY_TYPE_WIRELESS)) {
  354. /* UVLO */
  355. if (now_current_reg > (curr_step * 3))
  356. now_current_reg -= (curr_step * 3);
  357. /* current limit 300mA */
  358. if (now_current_reg < (MINIMUM_INPUT_CURRENT / charger->input_curr_limit_step))
  359. now_current_reg = (MINIMUM_INPUT_CURRENT / charger->input_curr_limit_step);
  360. curr_step /= 2;
  361. max77803_write_reg(charger->max77803->i2c,
  362. set_reg, now_current_reg);
  363. pr_info("%s: now_current_reg(0x%02x)\n", __func__, now_current_reg);
  364. chg_state = max77803_get_charger_state(charger);
  365. if ((chg_state != POWER_SUPPLY_STATUS_CHARGING) &&
  366. (chg_state != POWER_SUPPLY_STATUS_FULL))
  367. goto exit;
  368. if (curr_step < 2) {
  369. goto exit;
  370. }
  371. msleep(50);
  372. } else
  373. now_current_reg += (curr_step);
  374. }
  375. set_input_current:
  376. pr_info("%s: reg_data(0x%02x), input(%d)\n",
  377. __func__, set_current_reg, cur);
  378. max77803_write_reg(charger->max77803->i2c,
  379. set_reg, set_current_reg);
  380. exit:
  381. /* slow charging check */
  382. max77803_read_reg(charger->max77803->i2c,
  383. set_reg, &reg_data);
  384. max77803_check_slow_charging(charger, reg_data);
  385. reg_data = (0 << CHGIN_SHIFT);
  386. max77803_update_reg(charger->max77803->i2c, MAX77803_CHG_REG_CHG_INT_MASK, reg_data,
  387. CHGIN_MASK);
  388. mutex_unlock(&charger->ops_lock);
  389. }
  390. static int max77803_get_input_current(struct max77803_charger_data *charger)
  391. {
  392. u8 reg_data;
  393. int get_current = 0;
  394. if (charger->cable_type == POWER_SUPPLY_TYPE_WIRELESS) {
  395. max77803_read_reg(charger->max77803->i2c,
  396. MAX77803_CHG_REG_CHG_CNFG_10, &reg_data);
  397. pr_info("%s: CHG_CNFG_10(0x%02x)\n", __func__, reg_data);
  398. /* AND operation for removing the formal 2bit */
  399. reg_data = reg_data & 0x3F;
  400. charger->input_curr_limit_step = 20;
  401. } else {
  402. max77803_read_reg(charger->max77803->i2c,
  403. MAX77803_CHG_REG_CHG_CNFG_09, &reg_data);
  404. pr_info("%s: CHG_CNFG_09(0x%02x)\n", __func__, reg_data);
  405. /* AND operation for removing the formal 1bit */
  406. reg_data = reg_data & 0x7F;
  407. if (charger->pmic_ver == 0x04)
  408. charger->input_curr_limit_step = 25;
  409. else
  410. charger->input_curr_limit_step = 20;
  411. }
  412. get_current = reg_data * charger->input_curr_limit_step;
  413. pr_debug("%s: get input current: %dmA\n", __func__, get_current);
  414. return get_current;
  415. }
  416. static void max77803_set_topoff_current(struct max77803_charger_data *charger,
  417. int cur, int timeout)
  418. {
  419. u8 reg_data;
  420. if (cur >= 350)
  421. reg_data = 0x07;
  422. else if (cur >= 300)
  423. reg_data = 0x06;
  424. else if (cur >= 250)
  425. reg_data = 0x05;
  426. else if (cur >= 200)
  427. reg_data = 0x04;
  428. else if (cur >= 175)
  429. reg_data = 0x03;
  430. else if (cur >= 150)
  431. reg_data = 0x02;
  432. else if (cur >= 125)
  433. reg_data = 0x01;
  434. else
  435. reg_data = 0x00;
  436. /* the unit of timeout is second*/
  437. timeout = timeout / 60;
  438. reg_data |= ((timeout / 10) << 3);
  439. pr_info("%s: reg_data(0x%02x), topoff(%d)\n", __func__, reg_data, cur);
  440. max77803_write_reg(charger->max77803->i2c,
  441. MAX77803_CHG_REG_CHG_CNFG_03, reg_data);
  442. }
  443. static void max77803_set_charge_current(struct max77803_charger_data *charger,
  444. int cur)
  445. {
  446. u8 reg_data = 0;
  447. max77803_read_reg(charger->max77803->i2c,
  448. MAX77803_CHG_REG_CHG_CNFG_02, &reg_data);
  449. reg_data &= ~MAX77803_CHG_CC;
  450. if (!cur) {
  451. /* No charger */
  452. max77803_write_reg(charger->max77803->i2c,
  453. MAX77803_CHG_REG_CHG_CNFG_02, reg_data);
  454. } else {
  455. reg_data |= ((cur * 10 / charger->charging_curr_step) << 0);
  456. max77803_write_reg(charger->max77803->i2c,
  457. MAX77803_CHG_REG_CHG_CNFG_02, reg_data);
  458. }
  459. pr_info("%s: reg_data(0x%02x), charge(%d)\n",
  460. __func__, reg_data, cur);
  461. }
  462. /*
  463. static int max77803_get_charge_current(struct max77803_charger_data *charger)
  464. {
  465. u8 reg_data;
  466. int get_current;
  467. max77803_read_reg(charger->max77803->i2c,
  468. MAX77803_CHG_REG_CHG_CNFG_02, &reg_data);
  469. pr_debug("%s: CHG_CNFG_02(0x%02x)\n", __func__, reg_data);
  470. reg_data &= MAX77803_CHG_CC;
  471. get_current = reg_data * charger->charging_curr_step / 10;
  472. pr_debug("%s: get charge current: %dmA\n", __func__, get_current);
  473. return get_current;
  474. }
  475. */
  476. /* in soft regulation, current recovery operation */
  477. static void max77803_recovery_work(struct work_struct *work)
  478. {
  479. struct max77803_charger_data *chg_data = container_of(work,
  480. struct max77803_charger_data,
  481. recovery_work.work);
  482. u8 dtls_00, chgin_dtls;
  483. u8 dtls_01, chg_dtls;
  484. u8 dtls_02, byp_dtls;
  485. pr_debug("%s\n", __func__);
  486. __pm_relax(&chg_data->recovery_ws);
  487. if ((!chg_data->is_charging) || mutex_is_locked(&chg_data->ops_lock) ||
  488. (chg_data->cable_type != POWER_SUPPLY_TYPE_MAINS))
  489. return;
  490. max77803_read_reg(chg_data->max77803->i2c,
  491. MAX77803_CHG_REG_CHG_DTLS_00, &dtls_00);
  492. max77803_read_reg(chg_data->max77803->i2c,
  493. MAX77803_CHG_REG_CHG_DTLS_01, &dtls_01);
  494. max77803_read_reg(chg_data->max77803->i2c,
  495. MAX77803_CHG_REG_CHG_DTLS_02, &dtls_02);
  496. chgin_dtls = ((dtls_00 & MAX77803_CHGIN_DTLS) >>
  497. MAX77803_CHGIN_DTLS_SHIFT);
  498. chg_dtls = ((dtls_01 & MAX77803_CHG_DTLS) >>
  499. MAX77803_CHG_DTLS_SHIFT);
  500. byp_dtls = ((dtls_02 & MAX77803_BYP_DTLS) >>
  501. MAX77803_BYP_DTLS_SHIFT);
  502. if ((chg_data->soft_reg_recovery_cnt < RECOVERY_CNT) && (
  503. (chgin_dtls == 0x3) && (chg_dtls != 0x8) && (byp_dtls == 0x0))) {
  504. pr_info("%s: try to recovery, cnt(%d)\n", __func__,
  505. (chg_data->soft_reg_recovery_cnt + 1));
  506. if (chg_data->siop_level < 100 &&
  507. chg_data->cable_type == POWER_SUPPLY_TYPE_MAINS) {
  508. pr_info("%s : LCD on status and recover current\n", __func__);
  509. max77803_set_input_current(chg_data,
  510. SIOP_INPUT_LIMIT_CURRENT);
  511. } else {
  512. max77803_set_input_current(chg_data,
  513. chg_data->charging_current_max);
  514. }
  515. } else {
  516. pr_info("%s: fail to recovery, cnt(%d)\n", __func__,
  517. (chg_data->soft_reg_recovery_cnt + 1));
  518. pr_info("%s: CHGIN(0x%x), CHG(0x%x), BYP(0x%x)\n",
  519. __func__, chgin_dtls, chg_dtls, byp_dtls);
  520. /* schedule softreg recovery wq */
  521. if (chg_data->soft_reg_recovery_cnt < RECOVERY_CNT) {
  522. __pm_stay_awake(&chg_data->recovery_ws);
  523. queue_delayed_work(chg_data->wqueue, &chg_data->recovery_work,
  524. msecs_to_jiffies(RECOVERY_DELAY));
  525. } else {
  526. pr_info("%s: recovery cnt(%d) is over\n",
  527. __func__, RECOVERY_CNT);
  528. }
  529. }
  530. /* add recovery try count */
  531. chg_data->soft_reg_recovery_cnt++;
  532. }
  533. static void reduce_input_current(struct max77803_charger_data *charger, int cur)
  534. {
  535. u8 set_reg;
  536. u8 set_value;
  537. unsigned int min_input_current = 0;
  538. if ((!charger->is_charging) || mutex_is_locked(&charger->ops_lock) ||
  539. (charger->cable_type == POWER_SUPPLY_TYPE_WIRELESS))
  540. return;
  541. set_reg = MAX77803_CHG_REG_CHG_CNFG_09;
  542. min_input_current = MINIMUM_INPUT_CURRENT;
  543. if (charger->pmic_ver == 0x04)
  544. charger->input_curr_limit_step = 25;
  545. else
  546. charger->input_curr_limit_step = 20;
  547. if (!max77803_read_reg(charger->max77803->i2c,
  548. set_reg, &set_value)) {
  549. if ((set_value <= (min_input_current / charger->input_curr_limit_step)) ||
  550. (set_value <= (cur / charger->input_curr_limit_step)))
  551. return;
  552. set_value -= (cur / charger->input_curr_limit_step);
  553. set_value = (set_value < (min_input_current / charger->input_curr_limit_step)) ?
  554. (min_input_current / charger->input_curr_limit_step) : set_value;
  555. max77803_write_reg(charger->max77803->i2c,
  556. set_reg, set_value);
  557. pr_info("%s: set current: reg:(0x%x), val:(0x%x)\n",
  558. __func__, set_reg, set_value);
  559. }
  560. if(charger->cable_type == POWER_SUPPLY_TYPE_MAINS) {
  561. /* schedule softreg recovery wq */
  562. cancel_delayed_work_sync(&charger->recovery_work);
  563. __pm_stay_awake(&charger->recovery_ws);
  564. queue_delayed_work(charger->wqueue, &charger->recovery_work,
  565. msecs_to_jiffies(RECOVERY_DELAY));
  566. }
  567. }
  568. static int max77803_get_vbus_state(struct max77803_charger_data *charger)
  569. {
  570. u8 reg_data;
  571. union power_supply_propval value;
  572. max77803_read_reg(charger->max77803->i2c,
  573. MAX77803_CHG_REG_CHG_DTLS_00, &reg_data);
  574. psy_do_property("battery", get, POWER_SUPPLY_PROP_ONLINE,
  575. value);
  576. if (value.intval == POWER_SUPPLY_TYPE_WIRELESS)
  577. reg_data = ((reg_data & MAX77803_WCIN_DTLS) >>
  578. MAX77803_WCIN_DTLS_SHIFT);
  579. else
  580. reg_data = ((reg_data & MAX77803_CHGIN_DTLS) >>
  581. MAX77803_CHGIN_DTLS_SHIFT);
  582. switch (reg_data) {
  583. case 0x00:
  584. pr_info("%s: VBUS is invalid. CHGIN < CHGIN_UVLO\n",
  585. __func__);
  586. break;
  587. case 0x01:
  588. pr_info("%s: VBUS is invalid. CHGIN < MBAT+CHGIN2SYS" \
  589. "and CHGIN > CHGIN_UVLO\n", __func__);
  590. break;
  591. case 0x02:
  592. pr_info("%s: VBUS is invalid. CHGIN > CHGIN_OVLO",
  593. __func__);
  594. break;
  595. case 0x03:
  596. break;
  597. default:
  598. break;
  599. }
  600. return reg_data;
  601. }
  602. static int max77803_get_charger_state(struct max77803_charger_data *charger)
  603. {
  604. int state;
  605. u8 reg_data;
  606. max77803_read_reg(charger->max77803->i2c,
  607. MAX77803_CHG_REG_CHG_DTLS_01, &reg_data);
  608. reg_data = ((reg_data & MAX77803_CHG_DTLS) >> MAX77803_CHG_DTLS_SHIFT);
  609. pr_info("%s: CHG_DTLS : 0x%2x\n", __func__, reg_data);
  610. switch (reg_data) {
  611. case 0x0:
  612. case 0x1:
  613. case 0x2:
  614. state = POWER_SUPPLY_STATUS_CHARGING;
  615. break;
  616. case 0x3:
  617. case 0x4:
  618. state = POWER_SUPPLY_STATUS_FULL;
  619. break;
  620. case 0x5:
  621. case 0x6:
  622. case 0x7:
  623. state = POWER_SUPPLY_STATUS_NOT_CHARGING;
  624. break;
  625. case 0x8:
  626. case 0xA:
  627. case 0xB:
  628. state = POWER_SUPPLY_STATUS_DISCHARGING;
  629. break;
  630. default:
  631. state = POWER_SUPPLY_STATUS_UNKNOWN;
  632. break;
  633. }
  634. return state;
  635. }
  636. static int max77803_get_health_state(struct max77803_charger_data *charger)
  637. {
  638. int state;
  639. int vbus_state;
  640. int retry_cnt;
  641. u8 chg_dtls_00, chg_dtls, reg_data;
  642. u8 chg_cnfg_00, chg_cnfg_01 ,chg_cnfg_02, chg_cnfg_04, chg_cnfg_09, chg_cnfg_12;
  643. max77803_read_reg(charger->max77803->i2c,
  644. MAX77803_CHG_REG_CHG_DTLS_01, &reg_data);
  645. reg_data = ((reg_data & MAX77803_BAT_DTLS) >> MAX77803_BAT_DTLS_SHIFT);
  646. pr_info("%s: reg_data(0x%x)\n", __func__, reg_data);
  647. switch (reg_data) {
  648. case 0x00:
  649. pr_info("%s: No battery and the charger is suspended\n",
  650. __func__);
  651. state = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
  652. break;
  653. case 0x01:
  654. pr_info("%s: battery is okay "
  655. "but its voltage is low(~VPQLB)\n", __func__);
  656. state = POWER_SUPPLY_HEALTH_GOOD;
  657. break;
  658. case 0x02:
  659. pr_info("%s: battery dead\n", __func__);
  660. state = POWER_SUPPLY_HEALTH_DEAD;
  661. break;
  662. case 0x03:
  663. state = POWER_SUPPLY_HEALTH_GOOD;
  664. break;
  665. case 0x04:
  666. pr_info("%s: battery is okay" \
  667. "but its voltage is low\n", __func__);
  668. state = POWER_SUPPLY_HEALTH_GOOD;
  669. break;
  670. case 0x05:
  671. pr_info("%s: battery ovp\n", __func__);
  672. state = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
  673. break;
  674. default:
  675. pr_info("%s: battery unknown : 0x%d\n", __func__, reg_data);
  676. state = POWER_SUPPLY_HEALTH_UNKNOWN;
  677. break;
  678. }
  679. if (state == POWER_SUPPLY_HEALTH_GOOD) {
  680. union power_supply_propval value;
  681. psy_do_property("battery", get,
  682. POWER_SUPPLY_PROP_HEALTH, value);
  683. /* VBUS OVP state return battery OVP state */
  684. vbus_state = max77803_get_vbus_state(charger);
  685. /* read CHG_DTLS and detecting battery terminal error */
  686. max77803_read_reg(charger->max77803->i2c,
  687. MAX77803_CHG_REG_CHG_DTLS_01, &chg_dtls);
  688. chg_dtls = ((chg_dtls & MAX77803_CHG_DTLS) >>
  689. MAX77803_CHG_DTLS_SHIFT);
  690. max77803_read_reg(charger->max77803->i2c,
  691. MAX77803_CHG_REG_CHG_CNFG_00, &chg_cnfg_00);
  692. /* print the log at the abnormal case */
  693. if((charger->is_charging == 1) && (chg_dtls & 0x08)) {
  694. max77803_read_reg(charger->max77803->i2c,
  695. MAX77803_CHG_REG_CHG_DTLS_00, &chg_dtls_00);
  696. max77803_read_reg(charger->max77803->i2c,
  697. MAX77803_CHG_REG_CHG_CNFG_01, &chg_cnfg_01);
  698. max77803_read_reg(charger->max77803->i2c,
  699. MAX77803_CHG_REG_CHG_CNFG_02, &chg_cnfg_02);
  700. max77803_read_reg(charger->max77803->i2c,
  701. MAX77803_CHG_REG_CHG_CNFG_04, &chg_cnfg_04);
  702. max77803_read_reg(charger->max77803->i2c,
  703. MAX77803_CHG_REG_CHG_CNFG_09, &chg_cnfg_09);
  704. max77803_read_reg(charger->max77803->i2c,
  705. MAX77803_CHG_REG_CHG_CNFG_12, &chg_cnfg_12);
  706. pr_info("%s: CHG_DTLS_00(0x%x), CHG_DTLS_01(0x%x), CHG_CNFG_00(0x%x)\n",
  707. __func__, chg_dtls_00, chg_dtls, chg_cnfg_00);
  708. pr_info("%s: CHG_CNFG_01(0x%x), CHG_CNFG_02(0x%x), CHG_CNFG_04(0x%x)\n",
  709. __func__, chg_cnfg_01, chg_cnfg_02, chg_cnfg_04);
  710. pr_info("%s: CHG_CNFG_09(0x%x), CHG_CNFG_12(0x%x)\n",
  711. __func__, chg_cnfg_09, chg_cnfg_12);
  712. }
  713. pr_info("%s: vbus_state : 0x%d, chg_dtls : 0x%d\n", __func__, vbus_state, chg_dtls);
  714. /* OVP is higher priority */
  715. if (vbus_state == 0x02) { /* CHGIN_OVLO */
  716. pr_info("%s: vbus ovp\n", __func__);
  717. state = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
  718. if (charger->cable_type == POWER_SUPPLY_TYPE_WIRELESS) {
  719. retry_cnt = 0;
  720. do {
  721. msleep(50);
  722. vbus_state = max77803_get_vbus_state(charger);
  723. } while((retry_cnt++ < 2) && (vbus_state == 0x02));
  724. if (vbus_state == 0x02) {
  725. state = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
  726. pr_info("%s: wpc and over-voltage\n", __func__);
  727. } else
  728. state = POWER_SUPPLY_HEALTH_GOOD;
  729. }
  730. } else if (((vbus_state == 0x0) || (vbus_state == 0x01)) &&(chg_dtls & 0x08) && \
  731. (chg_cnfg_00 & MAX77803_MODE_BUCK) && \
  732. (chg_cnfg_00 & MAX77803_MODE_CHGR) && \
  733. (charger->cable_type != POWER_SUPPLY_TYPE_WIRELESS)) {
  734. pr_info("%s: vbus is under\n", __func__);
  735. state = POWER_SUPPLY_HEALTH_UNDERVOLTAGE;
  736. } else if((value.intval == POWER_SUPPLY_HEALTH_UNDERVOLTAGE) && \
  737. !((vbus_state == 0x0) || (vbus_state == 0x01))){
  738. max77803_set_input_current(charger,
  739. charger->charging_current_max);
  740. }
  741. }
  742. return state;
  743. }
  744. static int sec_chg_get_property(struct power_supply *psy,
  745. enum power_supply_property psp,
  746. union power_supply_propval *val)
  747. {
  748. struct max77803_charger_data *charger =
  749. container_of(psy, struct max77803_charger_data, psy_chg);
  750. u8 reg_data;
  751. switch (psp) {
  752. case POWER_SUPPLY_PROP_ONLINE:
  753. val->intval = POWER_SUPPLY_TYPE_BATTERY;
  754. if (max77803_read_reg(charger->max77803->i2c,
  755. MAX77803_CHG_REG_CHG_INT_OK, &reg_data) == 0) {
  756. pr_info("%s: reg_data(%d)\n", __func__, reg_data);
  757. if (reg_data & MAX77803_WCIN_OK) {
  758. val->intval = POWER_SUPPLY_TYPE_WIRELESS;
  759. charger->wc_w_state = 1;
  760. } else if (reg_data & MAX77803_CHGIN_OK) {
  761. val->intval = POWER_SUPPLY_TYPE_MAINS;
  762. }
  763. }
  764. break;
  765. case POWER_SUPPLY_PROP_STATUS:
  766. val->intval = max77803_get_charger_state(charger);
  767. break;
  768. case POWER_SUPPLY_PROP_HEALTH:
  769. val->intval = max77803_get_health_state(charger);
  770. break;
  771. case POWER_SUPPLY_PROP_CURRENT_MAX:
  772. // AOSP expects the charging current to be in microamperes
  773. // frameworks/base/core/java/android/os/BatteryManager.java L256
  774. val->intval = charger->charging_current_max * 1000;
  775. break;
  776. case POWER_SUPPLY_PROP_CURRENT_AVG:
  777. // AOSP expects the charging current to be in microamperes
  778. // frameworks/base/core/java/android/os/BatteryManager.java L263
  779. val->intval = charger->charging_current * 1000;
  780. break;
  781. case POWER_SUPPLY_PROP_CURRENT_NOW:
  782. val->intval = max77803_get_input_current(charger);
  783. break;
  784. case POWER_SUPPLY_PROP_CHARGE_TYPE:
  785. if (!charger->is_charging)
  786. val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE;
  787. else if (charger->aicl_on)
  788. {
  789. val->intval = POWER_SUPPLY_CHARGE_TYPE_SLOW;
  790. pr_info("%s: slow-charging mode\n", __func__);
  791. }
  792. else
  793. val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST;
  794. break;
  795. case POWER_SUPPLY_PROP_PRESENT:
  796. val->intval = max77803_get_battery_present(charger);
  797. break;
  798. case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
  799. break;
  800. #ifdef WPC_CHECK_CVPRM_FEATURE
  801. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  802. val->intval = max77803_get_charge_voltage(charger);
  803. break;
  804. #endif
  805. default:
  806. return -EINVAL;
  807. }
  808. return 0;
  809. }
  810. static int sec_chg_set_property(struct power_supply *psy,
  811. enum power_supply_property psp,
  812. const union power_supply_propval *val)
  813. {
  814. struct max77803_charger_data *charger =
  815. container_of(psy, struct max77803_charger_data, psy_chg);
  816. union power_supply_propval value;
  817. int set_charging_current, set_charging_current_max;
  818. const int usb_charging_current = charger->pdata->charging_current[
  819. POWER_SUPPLY_TYPE_USB].fast_charging_current;
  820. u8 chg_cnfg_00;
  821. switch (psp) {
  822. case POWER_SUPPLY_PROP_STATUS:
  823. charger->status = val->intval;
  824. break;
  825. /* val->intval : type */
  826. case POWER_SUPPLY_PROP_ONLINE:
  827. /* check and unlock */
  828. check_charger_unlock_state(charger);
  829. if (val->intval == POWER_SUPPLY_TYPE_POWER_SHARING) {
  830. psy_do_property("ps", get,
  831. POWER_SUPPLY_PROP_STATUS, value);
  832. chg_cnfg_00 = CHG_CNFG_00_OTG_MASK
  833. | CHG_CNFG_00_BOOST_MASK
  834. | CHG_CNFG_00_DIS_MUIC_CTRL_MASK;
  835. if (value.intval) {
  836. max77803_update_reg(charger->max77803->i2c, MAX77803_CHG_REG_CHG_CNFG_00,
  837. chg_cnfg_00, chg_cnfg_00);
  838. pr_info("%s: ps enable\n", __func__);
  839. } else {
  840. max77803_update_reg(charger->max77803->i2c, MAX77803_CHG_REG_CHG_CNFG_00,
  841. 0, chg_cnfg_00);
  842. pr_info("%s: ps disable\n", __func__);
  843. }
  844. break;
  845. }
  846. charger->cable_type = val->intval;
  847. psy_do_property("battery", get,
  848. POWER_SUPPLY_PROP_HEALTH, value);
  849. if (val->intval == POWER_SUPPLY_TYPE_BATTERY) {
  850. charger->is_charging = false;
  851. charger->aicl_on = false;
  852. charger->soft_reg_recovery_cnt = 0;
  853. set_charging_current = 0;
  854. set_charging_current_max =
  855. charger->pdata->charging_current[
  856. POWER_SUPPLY_TYPE_USB].input_current_limit;
  857. } else {
  858. charger->is_charging = true;
  859. charger->charging_current_max =
  860. charger->pdata->charging_current
  861. [charger->cable_type].input_current_limit;
  862. charger->charging_current =
  863. charger->pdata->charging_current
  864. [charger->cable_type].fast_charging_current;
  865. #ifdef CONFIG_FORCE_FAST_CHARGE
  866. /* Yank555 : Use Fast charge currents accroding to user settings */
  867. if (force_fast_charge == FAST_CHARGE_FORCE_AC) {/* We are in basic Fast Charge mode, so we substitute AC to USB levels */
  868. switch(charger->cable_type) {
  869. case POWER_SUPPLY_TYPE_USB: /* These are low current USB connections, apply usual 1A/h AC levels to USB */
  870. case POWER_SUPPLY_TYPE_USB_ACA:
  871. case POWER_SUPPLY_TYPE_CARDOCK:
  872. case POWER_SUPPLY_TYPE_OTG: charger->charging_current_max = USB_CHARGE_1000;
  873. charger->charging_current = USB_CHARGE_1000;
  874. break;
  875. default: /* Don't do anything for any other kind of connections and don't touch when type is unknown */
  876. break;
  877. }
  878. } else if (force_fast_charge == FAST_CHARGE_FORCE_CUSTOM_MA) { /* We are in custom current Fast Charge mode for both AC and USB */
  879. switch(charger->cable_type) {
  880. case POWER_SUPPLY_TYPE_USB:
  881. case POWER_SUPPLY_TYPE_USB_DCP:
  882. case POWER_SUPPLY_TYPE_USB_CDP:
  883. case POWER_SUPPLY_TYPE_USB_ACA:
  884. case POWER_SUPPLY_TYPE_CARDOCK:
  885. case POWER_SUPPLY_TYPE_OTG: /* These are USB connections, apply custom USB current for all of them */
  886. charger->charging_current_max = usb_charge_level;
  887. charger->charging_current = usb_charge_level;
  888. break;
  889. case POWER_SUPPLY_TYPE_MAINS: /* These are AC connections, apply custom AC current for all of them */
  890. charger->charging_current_max = ac_charge_level;
  891. charger->charging_current = min(ac_charge_level+300, MAX_CHARGE_LEVEL); /* Keep the 300mA/h delta, but never go above 2.1A/h */
  892. break;
  893. default: /* Don't do anything for any other kind of connections and don't touch when type is unknown */
  894. break;
  895. }
  896. }
  897. #endif // CONFIG_FORCE_FAST_CHARGE
  898. /* decrease the charging current according to siop level */
  899. set_charging_current =
  900. charger->charging_current * charger->siop_level / 100;
  901. if (set_charging_current > 0 &&
  902. set_charging_current < usb_charging_current)
  903. set_charging_current = usb_charging_current;
  904. set_charging_current_max =
  905. charger->charging_current_max;
  906. #ifdef WPC_CHECK_CVPRM_FEATURE
  907. if (val->intval == POWER_SUPPLY_TYPE_WIRELESS)
  908. max77803_check_cvprm(charger, 0x1C);
  909. else
  910. max77803_check_cvprm(charger, 0x1D);
  911. #endif
  912. if (charger->siop_level < 100 &&
  913. val->intval == POWER_SUPPLY_TYPE_MAINS) {
  914. set_charging_current_max = SIOP_INPUT_LIMIT_CURRENT;
  915. if (set_charging_current > SIOP_CHARGING_LIMIT_CURRENT)
  916. set_charging_current = SIOP_CHARGING_LIMIT_CURRENT;
  917. }
  918. }
  919. max77803_set_charger_state(charger, charger->is_charging);
  920. /* if battery full, only disable charging */
  921. if ((charger->status == POWER_SUPPLY_STATUS_CHARGING) ||
  922. (charger->status == POWER_SUPPLY_STATUS_DISCHARGING) ||
  923. (value.intval == POWER_SUPPLY_HEALTH_UNSPEC_FAILURE)) {
  924. /* current setting */
  925. max77803_set_charge_current(charger,
  926. set_charging_current);
  927. /* if battery is removed, disable input current and reenable input current
  928. * to enable buck always */
  929. if (value.intval == POWER_SUPPLY_HEALTH_UNSPEC_FAILURE)
  930. max77803_set_input_current(charger, 0);
  931. else
  932. max77803_set_input_current(charger,
  933. set_charging_current_max);
  934. max77803_set_topoff_current(charger,
  935. charger->pdata->charging_current[
  936. val->intval].full_check_current_1st,
  937. charger->pdata->charging_current[
  938. val->intval].full_check_current_2nd);
  939. }
  940. break;
  941. /* val->intval : input charging current */
  942. case POWER_SUPPLY_PROP_CURRENT_MAX:
  943. charger->charging_current_max = val->intval;
  944. break;
  945. /* val->intval : charging current */
  946. case POWER_SUPPLY_PROP_CURRENT_AVG:
  947. charger->charging_current = val->intval;
  948. break;
  949. case POWER_SUPPLY_PROP_CURRENT_NOW:
  950. max77803_set_charge_current(charger,
  951. val->intval);
  952. max77803_set_input_current(charger,
  953. val->intval);
  954. break;
  955. case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
  956. charger->siop_level = val->intval;
  957. if (charger->is_charging) {
  958. /* decrease the charging current according to siop level */
  959. int current_now =
  960. charger->charging_current * val->intval / 100;
  961. /* do forced set charging current */
  962. if (current_now > 0 &&
  963. current_now < usb_charging_current)
  964. current_now = usb_charging_current;
  965. if (charger->cable_type == POWER_SUPPLY_TYPE_MAINS) {
  966. if (charger->siop_level < 100 ) {
  967. set_charging_current_max = SIOP_INPUT_LIMIT_CURRENT;
  968. } else {
  969. set_charging_current_max =
  970. charger->charging_current_max;
  971. }
  972. if (charger->siop_level < 100 &&
  973. current_now > SIOP_CHARGING_LIMIT_CURRENT)
  974. current_now = SIOP_CHARGING_LIMIT_CURRENT;
  975. max77803_set_input_current(charger,
  976. set_charging_current_max);
  977. }
  978. max77803_set_charge_current(charger, current_now);
  979. }
  980. break;
  981. #if defined(CONFIG_SAMSUNG_BATTERY_ENG_TEST)
  982. case POWER_SUPPLY_PROP_CHARGE_TYPE:
  983. {
  984. u8 ctrl3, cnfg12;
  985. if(val->intval == POWER_SUPPLY_TYPE_WIRELESS) {
  986. cnfg12 = (0 << CHG_CNFG_12_CHGINSEL_SHIFT);
  987. ctrl3 = (1 << CTRL3_JIGSET_SHIFT);
  988. if (charger->cable_type == POWER_SUPPLY_TYPE_WIRELESS) {
  989. #ifdef CONFIG_FORCE_FAST_CHARGE
  990. /* Yank555 : Use Fast charge currents accroding to user settings */
  991. if (force_fast_charge == FAST_CHARGE_FORCE_AC) {
  992. /* We are in basic Fast Charge mode, so we substitute AC to WIRELESS levels */
  993. charger->charging_current_max = WIRELESS_CHARGE_1000;
  994. charger->charging_current = WIRELESS_CHARGE_1000 + 100;
  995. } else if (force_fast_charge == FAST_CHARGE_FORCE_CUSTOM_MA) {
  996. /* We are in custom current Fast Charge mode for WIRELESS */
  997. charger->charging_current_max = wireless_charge_level;
  998. charger->charging_current = min(wireless_charge_level+100, MAX_CHARGE_LEVEL);
  999. }
  1000. #else
  1001. charger->charging_current_max = 650;
  1002. charger->charging_current = 750;
  1003. #endif // CONFIG_FORCE_FAST_CHARGE
  1004. max77803_set_input_current(charger,
  1005. charger->charging_current_max);
  1006. max77803_set_charge_current(charger, charger->charging_current);
  1007. }
  1008. }
  1009. else {
  1010. cnfg12 = (1 << CHG_CNFG_12_CHGINSEL_SHIFT);
  1011. ctrl3 = (0 << CTRL3_JIGSET_SHIFT);
  1012. }
  1013. max77803_update_reg(charger->max77803->muic, MAX77803_MUIC_REG_CTRL3, ctrl3,
  1014. CTRL3_JIGSET_MASK);
  1015. max77803_update_reg(charger->max77803->i2c, MAX77803_CHG_REG_CHG_CNFG_12, cnfg12,
  1016. CHG_CNFG_12_CHGINSEL_MASK);
  1017. pr_info("%s: ctrl3 : (0x%02x)\n", __func__, ctrl3);
  1018. pr_info("%s: set CNFG_12: 0x%x\n", __func__, cnfg12);
  1019. break;
  1020. }
  1021. #endif
  1022. default:
  1023. return -EINVAL;
  1024. }
  1025. return 0;
  1026. }
  1027. static u8 max77803_get_float_voltage_data(
  1028. int float_voltage)
  1029. {
  1030. u8 data = 0x16;
  1031. if (float_voltage >= 4400)
  1032. data = 0x1f;
  1033. else if (float_voltage >= 4375)
  1034. data = 0x1e;
  1035. else if (float_voltage >= 4350)
  1036. data = 0x1d;
  1037. else if (float_voltage >= 4340)
  1038. data = 0x1c;
  1039. else
  1040. data = (float_voltage - 3650) / 25;
  1041. return data;
  1042. }
  1043. static void max77803_charger_initialize(struct max77803_charger_data *charger)
  1044. {
  1045. u8 reg_data;
  1046. pr_debug("%s\n", __func__);
  1047. /* unmasked: CHGIN_I, WCIN_I, BATP_I, BYP_I */
  1048. max77803_write_reg(charger->max77803->i2c,
  1049. MAX77803_CHG_REG_CHG_INT_MASK, 0x9a);
  1050. /* unlock charger setting protect */
  1051. reg_data = (0x03 << 2);
  1052. max77803_write_reg(charger->max77803->i2c,
  1053. MAX77803_CHG_REG_CHG_CNFG_06, reg_data);
  1054. /*
  1055. * fast charge timer disable
  1056. * restart threshold disable
  1057. * pre-qual charge enable(default)
  1058. */
  1059. reg_data = (0x0 << 0) | (0x03 << 4);
  1060. max77803_write_reg(charger->max77803->i2c,
  1061. MAX77803_CHG_REG_CHG_CNFG_01, reg_data);
  1062. /*
  1063. * charge current 466mA(default)
  1064. * otg current limit 900mA
  1065. */
  1066. max77803_read_reg(charger->max77803->i2c,
  1067. MAX77803_CHG_REG_CHG_CNFG_02, &reg_data);
  1068. reg_data |= (1 << 7);
  1069. max77803_write_reg(charger->max77803->i2c,
  1070. MAX77803_CHG_REG_CHG_CNFG_02, reg_data);
  1071. /*
  1072. * top off current 100mA
  1073. * top off timer 40min
  1074. */
  1075. reg_data = (0x04 << 3);
  1076. max77803_write_reg(charger->max77803->i2c,
  1077. MAX77803_CHG_REG_CHG_CNFG_03, reg_data);
  1078. /*
  1079. * cv voltage 4.2V or 4.35V
  1080. * MINVSYS 3.6V(default)
  1081. */
  1082. reg_data = max77803_get_float_voltage_data(charger->pdata->chg_float_voltage);
  1083. max77803_update_reg(charger->max77803->i2c, MAX77803_CHG_REG_CHG_CNFG_04,
  1084. (reg_data << CHG_CNFG_04_CHG_CV_PRM_SHIFT),
  1085. CHG_CNFG_04_CHG_CV_PRM_MASK);
  1086. max77803_read_reg(charger->max77803->i2c,
  1087. MAX77803_CHG_REG_CHG_CNFG_04, &reg_data);
  1088. pr_info("%s: battery cv voltage 0x%x\n", __func__, reg_data);
  1089. }
  1090. static void sec_chg_isr_work(struct work_struct *work)
  1091. {
  1092. struct max77803_charger_data *charger =
  1093. container_of(work, struct max77803_charger_data, isr_work.work);
  1094. union power_supply_propval val;
  1095. if (charger->pdata->full_check_type ==
  1096. SEC_BATTERY_FULLCHARGED_CHGINT) {
  1097. val.intval = max77803_get_charger_state(charger);
  1098. switch (val.intval) {
  1099. case POWER_SUPPLY_STATUS_DISCHARGING:
  1100. pr_err("%s: Interrupted but Discharging\n", __func__);
  1101. break;
  1102. case POWER_SUPPLY_STATUS_NOT_CHARGING:
  1103. pr_err("%s: Interrupted but NOT Charging\n", __func__);
  1104. break;
  1105. case POWER_SUPPLY_STATUS_FULL:
  1106. pr_info("%s: Interrupted by Full\n", __func__);
  1107. psy_do_property("battery", set,
  1108. POWER_SUPPLY_PROP_STATUS, val);
  1109. break;
  1110. case POWER_SUPPLY_STATUS_CHARGING:
  1111. pr_err("%s: Interrupted but Charging\n", __func__);
  1112. break;
  1113. case POWER_SUPPLY_STATUS_UNKNOWN:
  1114. default:
  1115. pr_err("%s: Invalid Charger Status\n", __func__);
  1116. break;
  1117. }
  1118. }
  1119. if (charger->pdata->ovp_uvlo_check_type ==
  1120. SEC_BATTERY_OVP_UVLO_CHGINT) {
  1121. val.intval = max77803_get_health_state(charger);
  1122. switch (val.intval) {
  1123. case POWER_SUPPLY_HEALTH_OVERHEAT:
  1124. case POWER_SUPPLY_HEALTH_COLD:
  1125. pr_err("%s: Interrupted but Hot/Cold\n", __func__);
  1126. break;
  1127. case POWER_SUPPLY_HEALTH_DEAD:
  1128. pr_err("%s: Interrupted but Dead\n", __func__);
  1129. break;
  1130. case POWER_SUPPLY_HEALTH_OVERVOLTAGE:
  1131. case POWER_SUPPLY_HEALTH_UNDERVOLTAGE:
  1132. pr_info("%s: Interrupted by OVP/UVLO\n", __func__);
  1133. psy_do_property("battery", set,
  1134. POWER_SUPPLY_PROP_HEALTH, val);
  1135. break;
  1136. case POWER_SUPPLY_HEALTH_UNSPEC_FAILURE:
  1137. pr_err("%s: Interrupted but Unspec\n", __func__);
  1138. break;
  1139. case POWER_SUPPLY_HEALTH_GOOD:
  1140. pr_err("%s: Interrupted but Good\n", __func__);
  1141. break;
  1142. case POWER_SUPPLY_HEALTH_UNKNOWN:
  1143. default:
  1144. pr_err("%s: Invalid Charger Health\n", __func__);
  1145. break;
  1146. }
  1147. }
  1148. }
  1149. static irqreturn_t sec_chg_irq_thread(int irq, void *irq_data)
  1150. {
  1151. struct max77803_charger_data *charger = irq_data;
  1152. pr_info("%s: Charger interrupt occured\n", __func__);
  1153. if ((charger->pdata->full_check_type ==
  1154. SEC_BATTERY_FULLCHARGED_CHGINT) ||
  1155. (charger->pdata->ovp_uvlo_check_type ==
  1156. SEC_BATTERY_OVP_UVLO_CHGINT))
  1157. schedule_delayed_work(&charger->isr_work, 0);
  1158. return IRQ_HANDLED;
  1159. }
  1160. #if defined(CONFIG_CHARGER_MAX77803)
  1161. static void wpc_detect_work(struct work_struct *work)
  1162. {
  1163. struct max77803_charger_data *chg_data = container_of(work,
  1164. struct max77803_charger_data,
  1165. wpc_work.work);
  1166. int wc_w_state;
  1167. int retry_cnt;
  1168. union power_supply_propval value;
  1169. u8 reg_data = 0;
  1170. pr_debug("%s\n", __func__);
  1171. reg_data = (0 << WCIN_SHIFT);
  1172. max77803_update_reg(chg_data->max77803->i2c, MAX77803_CHG_REG_CHG_INT_MASK, reg_data,
  1173. WCIN_MASK);
  1174. /* check and unlock */
  1175. check_charger_unlock_state(chg_data);
  1176. retry_cnt = 0;
  1177. do {
  1178. max77803_read_reg(chg_data->max77803->i2c,
  1179. MAX77803_CHG_REG_CHG_INT_OK, &reg_data);
  1180. wc_w_state = (reg_data & MAX77803_WCIN_OK)
  1181. >> MAX77803_WCIN_OK_SHIFT;
  1182. msleep(50);
  1183. } while((retry_cnt++ < 2) && (wc_w_state == 0));
  1184. if ((chg_data->wc_w_state == 0) && (wc_w_state == 1)) {
  1185. #if 0
  1186. value.intval = POWER_SUPPLY_TYPE_WIRELESS;
  1187. psy_do_property("battery", set,
  1188. POWER_SUPPLY_PROP_ONLINE, value);
  1189. #else
  1190. value.intval = 1;
  1191. psy_do_property("wireless", set,
  1192. POWER_SUPPLY_PROP_ONLINE, value);
  1193. #endif
  1194. pr_info("%s: wpc activated, set V_INT as PN\n",
  1195. __func__);
  1196. } else if ((chg_data->wc_w_state == 1) && (wc_w_state == 0)) {
  1197. if (!chg_data->is_charging)
  1198. max77803_set_charger_state(chg_data, true);
  1199. retry_cnt = 0;
  1200. do {
  1201. max77803_read_reg(chg_data->max77803->i2c,
  1202. MAX77803_CHG_REG_CHG_DTLS_01, &reg_data);
  1203. reg_data = ((reg_data & MAX77803_CHG_DTLS)
  1204. >> MAX77803_CHG_DTLS_SHIFT);
  1205. msleep(50);
  1206. } while((retry_cnt++ < 2) && (reg_data == 0x8));
  1207. pr_info("%s: reg_data: 0x%x, charging: %d\n", __func__,
  1208. reg_data, chg_data->is_charging);
  1209. if (!chg_data->is_charging)
  1210. max77803_set_charger_state(chg_data, false);
  1211. if ((reg_data != 0x08)
  1212. && (chg_data->cable_type == POWER_SUPPLY_TYPE_WIRELESS)) {
  1213. pr_info("%s: wpc uvlo, but charging\n", __func__);
  1214. queue_delayed_work(chg_data->wqueue, &chg_data->wpc_work,
  1215. msecs_to_jiffies(500));
  1216. return;
  1217. } else {
  1218. #if 0
  1219. value.intval = POWER_SUPPLY_TYPE_WIRELESS_REMOVE;
  1220. psy_do_property("battery", set,
  1221. POWER_SUPPLY_PROP_ONLINE, value);
  1222. #else
  1223. value.intval = 0;
  1224. psy_do_property("wireless", set,
  1225. POWER_SUPPLY_PROP_ONLINE, value);
  1226. #endif
  1227. pr_info("%s: wpc deactivated, set V_INT as PD\n",
  1228. __func__);
  1229. }
  1230. }
  1231. pr_info("%s: w(%d to %d)\n", __func__,
  1232. chg_data->wc_w_state, wc_w_state);
  1233. chg_data->wc_w_state = wc_w_state;
  1234. __pm_relax(&chg_data->wpc_ws);
  1235. }
  1236. static irqreturn_t wpc_charger_irq(int irq, void *data)
  1237. {
  1238. struct max77803_charger_data *chg_data = data;
  1239. unsigned long delay;
  1240. __pm_stay_awake(&chg_data->wpc_ws);
  1241. #ifdef CONFIG_SAMSUNG_BATTERY_FACTORY
  1242. delay = msecs_to_jiffies(0);
  1243. #else
  1244. if (chg_data->wc_w_state)
  1245. delay = msecs_to_jiffies(500);
  1246. else
  1247. delay = msecs_to_jiffies(0);
  1248. #endif
  1249. queue_delayed_work(chg_data->wqueue, &chg_data->wpc_work,
  1250. delay);
  1251. return IRQ_HANDLED;
  1252. }
  1253. #elif defined(CONFIG_WIRELESS_CHARGING)
  1254. static irqreturn_t wpc_charger_irq(int irq, void *data)
  1255. {
  1256. struct max77803_charger_data *chg_data = data;
  1257. int wc_w_state;
  1258. union power_supply_propval value;
  1259. pr_info("%s: irq(%d)\n", __func__, irq);
  1260. /* check and unlock */
  1261. check_charger_unlock_state(chg_data);
  1262. wc_w_state = !gpio_get_value(chg_data->wc_w_gpio);
  1263. if ((chg_data->wc_w_state == 0) && (wc_w_state == 1)) {
  1264. value.intval = POWER_SUPPLY_TYPE_WIRELESS
  1265. <<ONLINE_TYPE_MAIN_SHIFT;
  1266. psy_do_property("battery", set,
  1267. POWER_SUPPLY_PROP_ONLINE, value);
  1268. pr_info("%s: wpc activated, set V_INT as PN\n",
  1269. __func__);
  1270. } else if ((chg_data->wc_w_state == 1) && (wc_w_state == 0)) {
  1271. value.intval =
  1272. POWER_SUPPLY_TYPE_BATTERY<<ONLINE_TYPE_MAIN_SHIFT;
  1273. psy_do_property("battery", set,
  1274. POWER_SUPPLY_PROP_ONLINE, value);
  1275. pr_info("%s: wpc deactivated, set V_INT as PD\n",
  1276. __func__);
  1277. }
  1278. pr_info("%s: w(%d to %d)\n", __func__,
  1279. chg_data->wc_w_state, wc_w_state);
  1280. chg_data->wc_w_state = wc_w_state;
  1281. return IRQ_HANDLED;
  1282. }
  1283. #endif
  1284. static irqreturn_t max77803_bypass_irq(int irq, void *data)
  1285. {
  1286. struct max77803_charger_data *chg_data = data;
  1287. u8 dtls_02;
  1288. u8 byp_dtls;
  1289. u8 chg_cnfg_00;
  1290. u8 vbus_state;
  1291. pr_info("%s: irq(%d)\n", __func__, irq);
  1292. /* check and unlock */
  1293. check_charger_unlock_state(chg_data);
  1294. max77803_read_reg(chg_data->max77803->i2c,
  1295. MAX77803_CHG_REG_CHG_DTLS_02,
  1296. &dtls_02);
  1297. byp_dtls = ((dtls_02 & MAX77803_BYP_DTLS) >>
  1298. MAX77803_BYP_DTLS_SHIFT);
  1299. pr_info("%s: BYP_DTLS(0x%02x)\n", __func__, byp_dtls);
  1300. vbus_state = max77803_get_vbus_state(chg_data);
  1301. if (byp_dtls & 0x1) {
  1302. pr_info("%s: bypass overcurrent limit\n", __func__);
  1303. #ifdef CONFIG_USB_HOST_NOTIFY
  1304. sec_otg_notify(HNOTIFY_OVERCURRENT);
  1305. #endif
  1306. /* disable the register values just related to OTG and
  1307. keep the values about the charging */
  1308. max77803_read_reg(chg_data->max77803->i2c,
  1309. MAX77803_CHG_REG_CHG_CNFG_00, &chg_cnfg_00);
  1310. chg_cnfg_00 &= ~(CHG_CNFG_00_OTG_MASK
  1311. | CHG_CNFG_00_BOOST_MASK
  1312. | CHG_CNFG_00_DIS_MUIC_CTRL_MASK);
  1313. max77803_write_reg(chg_data->max77803->i2c,
  1314. MAX77803_CHG_REG_CHG_CNFG_00,
  1315. chg_cnfg_00);
  1316. }
  1317. if ((byp_dtls & 0x8) && (vbus_state < 0x03)) {
  1318. reduce_input_current(chg_data, REDUCE_CURRENT_STEP);
  1319. }
  1320. return IRQ_HANDLED;
  1321. }
  1322. static void max77803_chgin_isr_work(struct work_struct *work)
  1323. {
  1324. struct max77803_charger_data *charger = container_of(work,
  1325. struct max77803_charger_data, chgin_work);
  1326. u8 chgin_dtls, chg_dtls, chg_cnfg_00, reg_data;
  1327. u8 prev_chgin_dtls = 0xff;
  1328. int battery_health;
  1329. union power_supply_propval value;
  1330. int stable_count = 0;
  1331. __pm_stay_awake(&charger->chgin_ws);
  1332. while (1) {
  1333. psy_do_property("battery", get,
  1334. POWER_SUPPLY_PROP_HEALTH, value);
  1335. battery_health = value.intval;
  1336. max77803_read_reg(charger->max77803->i2c,
  1337. MAX77803_CHG_REG_CHG_DTLS_00,
  1338. &chgin_dtls);
  1339. chgin_dtls = ((chgin_dtls & MAX77803_CHGIN_DTLS) >>
  1340. MAX77803_CHGIN_DTLS_SHIFT);
  1341. max77803_read_reg(charger->max77803->i2c,
  1342. MAX77803_CHG_REG_CHG_DTLS_01, &chg_dtls);
  1343. chg_dtls = ((chg_dtls & MAX77803_CHG_DTLS) >>
  1344. MAX77803_CHG_DTLS_SHIFT);
  1345. max77803_read_reg(charger->max77803->i2c,
  1346. MAX77803_CHG_REG_CHG_CNFG_00, &chg_cnfg_00);
  1347. if (prev_chgin_dtls == chgin_dtls)
  1348. stable_count++;
  1349. else
  1350. stable_count = 0;
  1351. if (stable_count > 10) {
  1352. pr_info("%s: irq(%d), chgin(0x%x), chg_dtls(0x%x) prev 0x%x\n",
  1353. __func__, charger->irq_chgin,
  1354. chgin_dtls, chg_dtls, prev_chgin_dtls);
  1355. if (charger->is_charging) {
  1356. if ((chgin_dtls == 0x02) && \
  1357. (battery_health != POWER_SUPPLY_HEALTH_OVERVOLTAGE)) {
  1358. pr_info("%s: charger is over voltage\n",
  1359. __func__);
  1360. value.intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
  1361. psy_do_property("battery", set,
  1362. POWER_SUPPLY_PROP_HEALTH, value);
  1363. } else if (((chgin_dtls == 0x0) || (chgin_dtls == 0x01)) &&(chg_dtls & 0x08) && \
  1364. (chg_cnfg_00 & MAX77803_MODE_BUCK) && \
  1365. (chg_cnfg_00 & MAX77803_MODE_CHGR) && \
  1366. (battery_health != POWER_SUPPLY_HEALTH_UNDERVOLTAGE) && \
  1367. (charger->cable_type != POWER_SUPPLY_TYPE_WIRELESS)) {
  1368. pr_info("%s, vbus_state : 0x%d, chg_state : 0x%d\n", __func__, chgin_dtls, chg_dtls);
  1369. pr_info("%s: vBus is undervoltage\n", __func__);
  1370. value.intval = POWER_SUPPLY_HEALTH_UNDERVOLTAGE;
  1371. psy_do_property("battery", set,
  1372. POWER_SUPPLY_PROP_HEALTH, value);
  1373. }
  1374. } else {
  1375. if ((battery_health == \
  1376. POWER_SUPPLY_HEALTH_OVERVOLTAGE) &&
  1377. (chgin_dtls != 0x02)) {
  1378. pr_info("%s: vbus_state : 0x%d, chg_state : 0x%d\n", __func__, chgin_dtls, chg_dtls);
  1379. pr_info("%s: overvoltage->normal\n", __func__);
  1380. value.intval = POWER_SUPPLY_HEALTH_GOOD;
  1381. psy_do_property("battery", set,
  1382. POWER_SUPPLY_PROP_HEALTH, value);
  1383. } else if ((battery_health == \
  1384. POWER_SUPPLY_HEALTH_UNDERVOLTAGE) &&
  1385. !((chgin_dtls == 0x0) || (chgin_dtls == 0x01))){
  1386. pr_info("%s: vbus_state : 0x%d, chg_state : 0x%d\n", __func__, chgin_dtls, chg_dtls);
  1387. pr_info("%s: undervoltage->normal\n", __func__);
  1388. value.intval = POWER_SUPPLY_HEALTH_GOOD;
  1389. psy_do_property("battery", set,
  1390. POWER_SUPPLY_PROP_HEALTH, value);
  1391. max77803_set_input_current(charger,
  1392. charger->charging_current_max);
  1393. }
  1394. }
  1395. break;
  1396. }
  1397. if (charger->is_charging) {
  1398. /* reduce only at CC MODE */
  1399. if (((chgin_dtls == 0x0) || (chgin_dtls == 0x01)) &&
  1400. (chg_dtls == 0x01) && (stable_count > 2))
  1401. reduce_input_current(charger, REDUCE_CURRENT_STEP);
  1402. }
  1403. prev_chgin_dtls = chgin_dtls;
  1404. msleep(100);
  1405. }
  1406. reg_data = (0 << CHGIN_SHIFT);
  1407. max77803_update_reg(charger->max77803->i2c, MAX77803_CHG_REG_CHG_INT_MASK, reg_data,
  1408. CHGIN_MASK);
  1409. __pm_relax(&charger->chgin_ws);
  1410. }
  1411. static irqreturn_t max77803_chgin_irq(int irq, void *data)
  1412. {
  1413. struct max77803_charger_data *charger = data;
  1414. queue_work(charger->wqueue, &charger->chgin_work);
  1415. return IRQ_HANDLED;
  1416. }
  1417. static irqreturn_t max77803_battery_irq(int irq, void *data)
  1418. {
  1419. struct max77803_charger_data *charger = data;
  1420. union power_supply_propval value;
  1421. value.intval = max77803_get_battery_present(charger);
  1422. pr_info("%s: battery present(%d)\n", __func__, value.intval);
  1423. psy_do_property("battery", set,
  1424. POWER_SUPPLY_PROP_PRESENT, value);
  1425. return IRQ_HANDLED;
  1426. }
  1427. /* register chgin/battery isr after sec_battery_probe */
  1428. static void max77803_chgin_init_work(struct work_struct *work)
  1429. {
  1430. struct max77803_charger_data *charger = container_of(work,
  1431. struct max77803_charger_data,
  1432. chgin_init_work.work);
  1433. int ret;
  1434. pr_info("%s \n", __func__);
  1435. ret = request_threaded_irq(charger->irq_chgin, NULL,
  1436. max77803_chgin_irq, 0, "chgin-irq", charger);
  1437. if (ret < 0) {
  1438. pr_err("%s: fail to request chgin IRQ: %d: %d\n",
  1439. __func__, charger->irq_chgin, ret);
  1440. }
  1441. if (charger->irq_battery > 0) {
  1442. ret = request_threaded_irq(charger->irq_battery, NULL,
  1443. max77803_battery_irq, IRQF_TRIGGER_FALLING,
  1444. "battery-irq", charger);
  1445. if (ret < 0)
  1446. pr_err("%s: fail to request battery IRQ: %d: %d\n",
  1447. __func__, charger->irq_battery, ret);
  1448. }
  1449. }
  1450. #ifdef CONFIG_OF
  1451. static int sec_charger_read_u32_index_dt(const struct device_node *np,
  1452. const char *propname,
  1453. u32 index, u32 *out_value)
  1454. {
  1455. struct property *prop = of_find_property(np, propname, NULL);
  1456. u32 len = (index + 1) * sizeof(*out_value);
  1457. if (!prop)
  1458. return (-EINVAL);
  1459. if (!prop->value)
  1460. return (-ENODATA);
  1461. if (len > prop->length)
  1462. return (-EOVERFLOW);
  1463. *out_value = be32_to_cpup(((__be32 *)prop->value) + index);
  1464. return 0;
  1465. }
  1466. static int sec_charger_parse_dt(struct max77803_charger_data *charger)
  1467. {
  1468. struct device_node *np = of_find_node_by_name(NULL, "charger");
  1469. sec_battery_platform_data_t *pdata = charger->pdata;
  1470. int ret = 0;
  1471. int i, len;
  1472. const u32 *p;
  1473. if (np == NULL) {
  1474. pr_err("%s np NULL\n", __func__);
  1475. } else {
  1476. ret = of_property_read_u32(np, "battery,chg_float_voltage",
  1477. &pdata->chg_float_voltage);
  1478. ret = of_property_read_u32(np, "battery,ovp_uvlo_check_type",
  1479. &pdata->ovp_uvlo_check_type);
  1480. ret = of_property_read_u32(np, "battery,full_check_type",
  1481. &pdata->full_check_type);
  1482. p = of_get_property(np, "battery,input_current_limit", &len);
  1483. len = len / sizeof(u32);
  1484. pdata->charging_current = kzalloc(sizeof(sec_charging_current_t) * len,
  1485. GFP_KERNEL);
  1486. for(i = 0; i < len; i++) {
  1487. ret = sec_charger_read_u32_index_dt(np,
  1488. "battery,input_current_limit", i,
  1489. &pdata->charging_current[i].input_current_limit);
  1490. ret = sec_charger_read_u32_index_dt(np,
  1491. "battery,fast_charging_current", i,
  1492. &pdata->charging_current[i].fast_charging_current);
  1493. ret = sec_charger_read_u32_index_dt(np,
  1494. "battery,full_check_current_1st", i,
  1495. &pdata->charging_current[i].full_check_current_1st);
  1496. ret = sec_charger_read_u32_index_dt(np,
  1497. "battery,full_check_current_2nd", i,
  1498. &pdata->charging_current[i].full_check_current_2nd);
  1499. }
  1500. np = of_find_node_by_name(NULL, "battery");
  1501. if (np == NULL) {
  1502. pr_err("%s : np NULL\n", __func__);
  1503. pdata->bat_irq_gpio = -1;
  1504. } else {
  1505. ret = of_get_named_gpio(np, "battery,ta_int", 0);
  1506. if (ret < 0) {
  1507. pr_info("%s use bat irq %d\n", __func__, ret);
  1508. /* temporally assign for check*/
  1509. pdata->bat_irq_gpio = ret;
  1510. }
  1511. }
  1512. }
  1513. return ret;
  1514. }
  1515. #endif
  1516. static __devinit int max77803_charger_probe(struct platform_device *pdev)
  1517. {
  1518. struct max77803_dev *iodev = dev_get_drvdata(pdev->dev.parent);
  1519. struct max77803_platform_data *pdata = dev_get_platdata(iodev->dev);
  1520. struct max77803_charger_data *charger;
  1521. int ret = 0;
  1522. u8 reg_data;
  1523. pr_info("%s: MAX77803 Charger driver probe\n", __func__);
  1524. charger = kzalloc(sizeof(*charger), GFP_KERNEL);
  1525. if (!charger)
  1526. return -ENOMEM;
  1527. pdata->charger_data = kzalloc(sizeof(sec_battery_platform_data_t), GFP_KERNEL);
  1528. if (!pdata->charger_data) {
  1529. ret = -ENOMEM;
  1530. goto err_free;
  1531. }
  1532. charger->max77803 = iodev;
  1533. charger->pdata = pdata->charger_data;
  1534. charger->aicl_on = false;
  1535. charger->siop_level = 100;
  1536. #ifdef CONFIG_OF
  1537. if (sec_charger_parse_dt(charger))
  1538. dev_err(&pdev->dev,
  1539. "%s : Failed to get charger int\n", __func__);
  1540. #endif
  1541. platform_set_drvdata(pdev, charger);
  1542. charger->psy_chg.name = "sec-charger";
  1543. charger->psy_chg.type = POWER_SUPPLY_TYPE_UNKNOWN;
  1544. charger->psy_chg.get_property = sec_chg_get_property;
  1545. charger->psy_chg.set_property = sec_chg_set_property;
  1546. charger->psy_chg.properties = sec_charger_props;
  1547. charger->psy_chg.num_properties = ARRAY_SIZE(sec_charger_props);
  1548. mutex_init(&charger->ops_lock);
  1549. if (charger->pdata->chg_gpio_init) {
  1550. if (!charger->pdata->chg_gpio_init()) {
  1551. pr_err("%s: Failed to Initialize GPIO\n", __func__);
  1552. goto err_free;
  1553. }
  1554. }
  1555. max77803_charger_initialize(charger);
  1556. if (max77803_read_reg(charger->max77803->i2c, MAX77803_PMIC_REG_PMIC_ID1, &reg_data) < 0) {
  1557. pr_err("device not found on this channel (this is not an error)\n");
  1558. ret = -ENODEV;
  1559. goto err_free;
  1560. } else {
  1561. charger->pmic_ver = (reg_data & 0xf);
  1562. pr_info("%s: device found: ver.0x%x\n", __func__,
  1563. charger->pmic_ver);
  1564. }
  1565. if (charger->pmic_ver == 0x04) {
  1566. charger->input_curr_limit_step = 25;
  1567. charger->charging_curr_step= 400; // 0.1mA unit
  1568. } else {
  1569. charger->input_curr_limit_step = 20;
  1570. charger->charging_curr_step= 333; // 0.1mA unit
  1571. }
  1572. charger->wqueue =
  1573. create_singlethread_workqueue(dev_name(&pdev->dev));
  1574. if (!charger->wqueue) {
  1575. pr_err("%s: Fail to Create Workqueue\n", __func__);
  1576. goto err_free;
  1577. }
  1578. wakeup_source_init(&charger->chgin_ws, "charger-chgin");
  1579. INIT_WORK(&charger->chgin_work, max77803_chgin_isr_work);
  1580. INIT_DELAYED_WORK(&charger->chgin_init_work, max77803_chgin_init_work);
  1581. wakeup_source_init(&charger->recovery_ws, "charger-recovery");
  1582. INIT_DELAYED_WORK(&charger->recovery_work, max77803_recovery_work);
  1583. wakeup_source_init(&charger->wpc_ws, "charger-wpc");
  1584. INIT_DELAYED_WORK(&charger->wpc_work, wpc_detect_work);
  1585. ret = power_supply_register(&pdev->dev, &charger->psy_chg);
  1586. if (ret) {
  1587. pr_err("%s: Failed to Register psy_chg\n", __func__);
  1588. goto err_power_supply_register;
  1589. }
  1590. if (charger->pdata->chg_irq) {
  1591. INIT_DELAYED_WORK_DEFERRABLE(
  1592. &charger->isr_work, sec_chg_isr_work);
  1593. ret = request_threaded_irq(charger->pdata->chg_irq,
  1594. NULL, sec_chg_irq_thread,
  1595. charger->pdata->chg_irq_attr,
  1596. "charger-irq", charger);
  1597. if (ret) {
  1598. pr_err("%s: Failed to Reqeust IRQ\n", __func__);
  1599. goto err_irq;
  1600. }
  1601. }
  1602. #if defined(CONFIG_CHARGER_MAX77803)
  1603. charger->wc_w_irq = pdata->irq_base + MAX77803_CHG_IRQ_WCIN_I;
  1604. ret = request_threaded_irq(charger->wc_w_irq,
  1605. NULL, wpc_charger_irq,
  1606. 0, "wpc-int", charger);
  1607. if (ret) {
  1608. pr_err("%s: Failed to Reqeust IRQ\n", __func__);
  1609. goto err_wc_irq;
  1610. }
  1611. max77803_read_reg(charger->max77803->i2c,
  1612. MAX77803_CHG_REG_CHG_INT_OK, &reg_data);
  1613. charger->wc_w_state = (reg_data & MAX77803_WCIN_OK)
  1614. >> MAX77803_WCIN_OK_SHIFT;
  1615. #elif defined(CONFIG_WIRELESS_CHARGING)
  1616. charger->wc_w_gpio = pdata->wc_irq_gpio;
  1617. if (charger->wc_w_gpio) {
  1618. charger->wc_w_irq = gpio_to_irq(charger->wc_w_gpio);
  1619. ret = gpio_request(charger->wc_w_gpio, "wpc_charger-irq");
  1620. if (ret < 0) {
  1621. pr_err("%s: failed requesting gpio %d\n", __func__,
  1622. charger->wc_w_gpio);
  1623. goto err_wc_irq;
  1624. }
  1625. ret = request_threaded_irq(charger->wc_w_irq,
  1626. NULL, wpc_charger_irq,
  1627. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING |
  1628. IRQF_ONESHOT,
  1629. "wpc-int", charger);
  1630. if (ret) {
  1631. pr_err("%s: Failed to Reqeust IRQ\n", __func__);
  1632. goto err_wc_irq;
  1633. }
  1634. enable_irq_wake(charger->wc_w_irq);
  1635. charger->wc_w_state = !gpio_get_value(charger->wc_w_gpio);
  1636. }
  1637. #endif
  1638. charger->irq_chgin = pdata->irq_base + MAX77803_CHG_IRQ_CHGIN_I;
  1639. if (charger->pdata->bat_irq_gpio < 0)
  1640. charger->irq_battery = pdata->irq_base + MAX77803_CHG_IRQ_BATP_I;
  1641. /* enable chgin/battery irq after sec_battery_probe */
  1642. queue_delayed_work(charger->wqueue, &charger->chgin_init_work,
  1643. msecs_to_jiffies(3000));
  1644. charger->irq_bypass = pdata->irq_base + MAX77803_CHG_IRQ_BYP_I;
  1645. ret = request_threaded_irq(charger->irq_bypass, NULL,
  1646. max77803_bypass_irq, 0, "bypass-irq", charger);
  1647. if (ret < 0)
  1648. pr_err("%s: fail to request bypass IRQ: %d: %d\n",
  1649. __func__, charger->irq_bypass, ret);
  1650. return 0;
  1651. err_wc_irq:
  1652. free_irq(charger->pdata->chg_irq, NULL);
  1653. err_irq:
  1654. power_supply_unregister(&charger->psy_chg);
  1655. err_power_supply_register:
  1656. destroy_workqueue(charger->wqueue);
  1657. err_free:
  1658. kfree(charger);
  1659. return ret;
  1660. }
  1661. static int __devexit max77803_charger_remove(struct platform_device *pdev)
  1662. {
  1663. struct max77803_charger_data *charger =
  1664. platform_get_drvdata(pdev);
  1665. destroy_workqueue(charger->wqueue);
  1666. free_irq(charger->wc_w_irq, NULL);
  1667. free_irq(charger->pdata->chg_irq, NULL);
  1668. power_supply_unregister(&charger->psy_chg);
  1669. kfree(charger);
  1670. return 0;
  1671. }
  1672. #if defined CONFIG_PM
  1673. static int max77803_charger_suspend(struct device *dev)
  1674. {
  1675. return 0;
  1676. }
  1677. static int max77803_charger_resume(struct device *dev)
  1678. {
  1679. return 0;
  1680. }
  1681. #else
  1682. #define max77803_charger_suspend NULL
  1683. #define max77803_charger_resume NULL
  1684. #endif
  1685. static void max77803_charger_shutdown(struct device *dev)
  1686. {
  1687. struct max77803_charger_data *charger = dev_get_drvdata(dev);
  1688. u8 reg_data;
  1689. pr_info("%s: MAX77803 Charger driver shutdown\n", __func__);
  1690. if (!charger->max77803->i2c) {
  1691. pr_err("%s: no max77803 i2c client\n", __func__);
  1692. return;
  1693. }
  1694. reg_data = 0x04;
  1695. max77803_write_reg(charger->max77803->i2c,
  1696. MAX77803_CHG_REG_CHG_CNFG_00, reg_data);
  1697. reg_data = 0x19;
  1698. max77803_write_reg(charger->max77803->i2c,
  1699. MAX77803_CHG_REG_CHG_CNFG_09, reg_data);
  1700. reg_data = 0x19;
  1701. max77803_write_reg(charger->max77803->i2c,
  1702. MAX77803_CHG_REG_CHG_CNFG_10, reg_data);
  1703. reg_data = 0x64;
  1704. max77803_write_reg(charger->max77803->i2c,
  1705. MAX77803_CHG_REG_CHG_CNFG_12, reg_data);
  1706. pr_info("func:%s \n", __func__);
  1707. }
  1708. static SIMPLE_DEV_PM_OPS(max77803_charger_pm_ops, max77803_charger_suspend,
  1709. max77803_charger_resume);
  1710. static struct platform_driver max77803_charger_driver = {
  1711. .driver = {
  1712. .name = "max77803-charger",
  1713. .owner = THIS_MODULE,
  1714. .pm = &max77803_charger_pm_ops,
  1715. .shutdown = max77803_charger_shutdown,
  1716. },
  1717. .probe = max77803_charger_probe,
  1718. .remove = __devexit_p(max77803_charger_remove),
  1719. };
  1720. static int __init max77803_charger_init(void)
  1721. {
  1722. pr_info("func:%s\n", __func__);
  1723. return platform_driver_register(&max77803_charger_driver);
  1724. }
  1725. module_init(max77803_charger_init);
  1726. static void __exit max77803_charger_exit(void)
  1727. {
  1728. platform_driver_register(&max77803_charger_driver);
  1729. }
  1730. module_exit(max77803_charger_exit);
  1731. MODULE_DESCRIPTION("max77803 charger driver");
  1732. MODULE_AUTHOR("Samsung Electronics");
  1733. MODULE_LICENSE("GPL");