rt5081_pmu_charger.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  1. /* Copyright Statement:
  2. *
  3. * This software/firmware and related documentation ("MediaTek Software") are
  4. * protected under relevant copyright laws. The information contained herein
  5. * is confidential and proprietary to MediaTek Inc. and/or its licensors.
  6. * Without the prior written permission of MediaTek inc. and/or its licensors,
  7. * any reproduction, modification, use or disclosure of MediaTek Software,
  8. * and information contained herein, in whole or in part, shall be strictly prohibited.
  9. */
  10. /* MediaTek Inc. (C) 2015. All rights reserved.
  11. *
  12. * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
  13. * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
  14. * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON
  15. * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
  18. * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
  19. * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
  20. * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
  21. * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY ACKNOWLEDGES
  22. * THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES
  23. * CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK
  24. * SOFTWARE RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
  25. * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND
  26. * CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
  27. * AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
  28. * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY RECEIVER TO
  29. * MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
  30. */
  31. #include <platform/mt_typedefs.h>
  32. #include <platform/mt_reg_base.h>
  33. #include <platform/errno.h>
  34. #include <platform/mt_i2c.h>
  35. #include <platform/mt_pmic.h>
  36. #include <platform/mt_gpt.h>
  37. #include <platform/rt5081_pmu_charger.h>
  38. #include <platform/mtk_charger_intf.h>
  39. #include <printf.h>
  40. #if !defined(CONFIG_POWER_EXT)
  41. #include <platform/upmu_common.h>
  42. #endif
  43. #define RT5081_PMU_CHARGER_LK_DRV_VERSION "1.0.2_MTK"
  44. /* ================= */
  45. /* Internal variable */
  46. /* ================= */
  47. struct rt5081_pmu_charger_info {
  48. struct mtk_charger_info mchr_info;
  49. struct mt_i2c_t i2c;
  50. int i2c_log_level;
  51. };
  52. enum rt5081_charging_status {
  53. RT5081_CHG_STATUS_READY = 0,
  54. RT5081_CHG_STATUS_PROGRESS,
  55. RT5081_CHG_STATUS_DONE,
  56. RT5081_CHG_STATUS_FAULT,
  57. RT5081_CHG_STATUS_MAX,
  58. };
  59. /* Charging status name */
  60. static const char *rt5081_chg_status_name[RT5081_CHG_STATUS_MAX] = {
  61. "ready", "progress", "done", "fault",
  62. };
  63. /* ======================= */
  64. /* Address & Default value */
  65. /* ======================= */
  66. static const unsigned char rt5081_chg_reg_addr[] = {
  67. RT5081_PMU_REG_CHGCTRL1,
  68. RT5081_PMU_REG_CHGCTRL2,
  69. RT5081_PMU_REG_CHGCTRL3,
  70. RT5081_PMU_REG_CHGCTRL4,
  71. RT5081_PMU_REG_CHGCTRL5,
  72. RT5081_PMU_REG_CHGCTRL6,
  73. RT5081_PMU_REG_CHGCTRL7,
  74. RT5081_PMU_REG_CHGCTRL8,
  75. RT5081_PMU_REG_CHGCTRL9,
  76. RT5081_PMU_REG_CHGCTRL10,
  77. RT5081_PMU_REG_CHGCTRL11,
  78. RT5081_PMU_REG_CHGCTRL12,
  79. RT5081_PMU_REG_CHGCTRL13,
  80. RT5081_PMU_REG_CHGCTRL14,
  81. RT5081_PMU_REG_CHGCTRL15,
  82. RT5081_PMU_REG_CHGCTRL16,
  83. RT5081_PMU_REG_CHGADC,
  84. RT5081_PMU_REG_DEVICETYPE,
  85. RT5081_PMU_REG_QCCTRL1,
  86. RT5081_PMU_REG_QCCTRL2,
  87. RT5081_PMU_REG_QC3P0CTRL1,
  88. RT5081_PMU_REG_QC3P0CTRL2,
  89. RT5081_PMU_REG_USBSTATUS1,
  90. RT5081_PMU_REG_QCSTATUS1,
  91. RT5081_PMU_REG_QCSTATUS2,
  92. RT5081_PMU_REG_CHGPUMP,
  93. RT5081_PMU_REG_CHGCTRL17,
  94. RT5081_PMU_REG_CHGCTRL18,
  95. RT5081_PMU_REG_CHGDIRCHG1,
  96. RT5081_PMU_REG_CHGDIRCHG2,
  97. RT5081_PMU_REG_CHGDIRCHG3,
  98. RT5081_PMU_REG_CHGSTAT,
  99. RT5081_PMU_REG_CHGNTC,
  100. RT5081_PMU_REG_ADCDATAH,
  101. RT5081_PMU_REG_ADCDATAL,
  102. RT5081_PMU_REG_CHGCTRL19,
  103. RT5081_PMU_REG_CHGSTAT1,
  104. RT5081_PMU_REG_CHGSTAT2,
  105. RT5081_PMU_REG_CHGSTAT3,
  106. RT5081_PMU_REG_CHGSTAT4,
  107. RT5081_PMU_REG_CHGSTAT5,
  108. RT5081_PMU_REG_CHGSTAT6,
  109. RT5081_PMU_REG_QCSTAT,
  110. RT5081_PMU_REG_DICHGSTAT,
  111. RT5081_PMU_REG_OVPCTRLSTAT,
  112. };
  113. enum rt5081_iin_limit_sel {
  114. RT5081_IIMLMTSEL_AICR_3250 = 0,
  115. RT5081_IIMLMTSEL_CHR_TYPE,
  116. RT5081_IINLMTSEL_AICR,
  117. RT5081_IINLMTSEL_LOWER_LEVEL, /* lower of above three */
  118. };
  119. /* ========================= */
  120. /* I2C operations */
  121. /* ========================= */
  122. static int rt5081_i2c_write_byte(struct rt5081_pmu_charger_info *info, u8 cmd,
  123. u8 data)
  124. {
  125. unsigned int ret = I2C_OK;
  126. unsigned char write_buf[2] = {cmd, data};
  127. struct mt_i2c_t *i2c = &info->i2c;
  128. ret = i2c_write(i2c, write_buf, 2);
  129. if (ret != I2C_OK)
  130. dprintf(CRITICAL,
  131. "%s: I2CW[0x%02X] = 0x%02X failed, code = %d\n",
  132. __func__, cmd, data, ret);
  133. else
  134. dprintf(info->i2c_log_level, "%s: I2CW[0x%02X] = 0x%02X\n",
  135. __func__, cmd, data);
  136. return ret;
  137. }
  138. static int rt5081_i2c_read_byte(struct rt5081_pmu_charger_info *info, u8 cmd,
  139. u8 *data)
  140. {
  141. int ret = I2C_OK;
  142. u8 ret_data = cmd;
  143. struct mt_i2c_t *i2c = &info->i2c;
  144. ret = i2c_write_read(i2c, &ret_data, 1, 1);
  145. if (ret != I2C_OK)
  146. dprintf(CRITICAL, "%s: I2CR[0x%02X] failed, code = %d\n",
  147. __func__, cmd, ret);
  148. else {
  149. dprintf(info->i2c_log_level, "%s: I2CR[0x%02X] = 0x%02X\n",
  150. __func__, cmd, ret_data);
  151. *data = ret_data;
  152. }
  153. return ret;
  154. }
  155. static int rt5081_i2c_test_bit(struct rt5081_pmu_charger_info *info, u8 cmd,
  156. u8 shift)
  157. {
  158. int ret = 0;
  159. u8 data = 0;
  160. ret = rt5081_i2c_read_byte(info, cmd, &data);
  161. if (ret != I2C_OK)
  162. return ret;
  163. ret = data & (1 << shift);
  164. return ret;
  165. }
  166. static int rt5081_i2c_update_bits(struct rt5081_pmu_charger_info *info, u8 cmd,
  167. u8 mask, u8 data)
  168. {
  169. int ret = 0;
  170. u8 reg_data = 0;
  171. ret = rt5081_i2c_read_byte(info, cmd, &reg_data);
  172. if (ret != I2C_OK)
  173. return ret;
  174. reg_data = reg_data & 0xFF;
  175. reg_data &= ~mask;
  176. reg_data |= (data & mask);
  177. return rt5081_i2c_write_byte(info, cmd, reg_data);
  178. }
  179. static inline int rt5081_set_bit(struct rt5081_pmu_charger_info *info, u8 reg,
  180. u8 mask)
  181. {
  182. return rt5081_i2c_update_bits(info, reg, mask, mask);
  183. }
  184. static inline int rt5081_clr_bit(struct rt5081_pmu_charger_info *info, u8 reg,
  185. u8 mask)
  186. {
  187. return rt5081_i2c_update_bits(info, reg, mask, 0x00);
  188. }
  189. /* ================== */
  190. /* internal functions */
  191. /* ================== */
  192. static int rt_charger_set_ichg(struct mtk_charger_info *mchr_info, u32 ichg);
  193. static int rt_charger_set_aicr(struct mtk_charger_info *mchr_info, u32 aicr);
  194. static int rt_charger_set_mivr(struct mtk_charger_info *mchr_info, u32 mivr);
  195. static int rt_charger_get_ichg(struct mtk_charger_info *mchr_info, u32 *ichg);
  196. static int rt_charger_get_aicr(struct mtk_charger_info *mchr_info, u32 *aicr);
  197. static u8 rt5081_find_closest_reg_value(const u32 min, const u32 max,
  198. const u32 step, const u32 num, const u32 target)
  199. {
  200. u32 i = 0, cur_val = 0, next_val = 0;
  201. /* Smaller than minimum supported value, use minimum one */
  202. if (target < min)
  203. return 0;
  204. for (i = 0; i < num - 1; i++) {
  205. cur_val = min + i * step;
  206. next_val = cur_val + step;
  207. if (cur_val > max)
  208. cur_val = max;
  209. if (next_val > max)
  210. next_val = max;
  211. if (target >= cur_val && target < next_val)
  212. return i;
  213. }
  214. /* Greater than maximum supported value, use maximum one */
  215. return num - 1;
  216. }
  217. static u8 rt5081_find_closest_reg_value_via_table(const u32 *value_table,
  218. const u32 table_size, const u32 target_value)
  219. {
  220. u32 i = 0;
  221. /* Smaller than minimum supported value, use minimum one */
  222. if (target_value < value_table[0])
  223. return 0;
  224. for (i = 0; i < table_size - 1; i++) {
  225. if (target_value >= value_table[i] &&
  226. target_value < value_table[i + 1])
  227. return i;
  228. }
  229. /* Greater than maximum supported value, use maximum one */
  230. return table_size - 1;
  231. }
  232. static u32 rt5081_find_closest_real_value(const u32 min, const u32 max,
  233. const u32 step, const u8 reg_val)
  234. {
  235. u32 ret_val = 0;
  236. ret_val = min + reg_val * step;
  237. if (ret_val > max)
  238. ret_val = max;
  239. return ret_val;
  240. }
  241. /* Hardware pin current limit */
  242. static int rt5081_enable_ilim(struct rt5081_pmu_charger_info *info, bool enable)
  243. {
  244. int ret = 0;
  245. dprintf(CRITICAL, "%s: enable ilim = %d\n", __func__, enable);
  246. ret = (enable ? rt5081_set_bit : rt5081_clr_bit)
  247. (info, RT5081_PMU_REG_CHGCTRL3, RT5081_MASK_ILIM_EN);
  248. return ret;
  249. }
  250. /* Select IINLMTSEL to use AICR */
  251. static int rt5081_select_input_current_limit(
  252. struct rt5081_pmu_charger_info *info, enum rt5081_iin_limit_sel sel)
  253. {
  254. int ret = 0;
  255. dprintf(CRITICAL, "%s: select input current limit = %d\n",
  256. __func__, sel);
  257. ret = rt5081_i2c_update_bits(
  258. info,
  259. RT5081_PMU_REG_CHGCTRL2,
  260. RT5081_MASK_IINLMTSEL,
  261. sel << RT5081_SHIFT_IINLMTSEL
  262. );
  263. return ret;
  264. }
  265. static bool rt5081_is_hw_exist(struct rt5081_pmu_charger_info *info)
  266. {
  267. int ret = 0;
  268. u8 vendor_id = 0, revision = 0;
  269. u8 data = 0;
  270. ret = rt5081_i2c_read_byte(info, RT5081_PMU_REG_DEVINFO, &data);
  271. if (ret != I2C_OK)
  272. return false;
  273. vendor_id = data & 0xF0;
  274. revision = data & 0x0F;
  275. if (vendor_id != RT5081_VENDOR_ID) {
  276. dprintf(CRITICAL, "%s: vendor id is incorrect\n", __func__);
  277. return false;
  278. }
  279. dprintf(CRITICAL, "%s: E%d(0x%02X)\n", __func__, revision, revision);
  280. info->mchr_info.device_id = revision;
  281. return true;
  282. }
  283. #if 0
  284. /* Set register's value to default */
  285. static int rt5081_chg_reset_chip(struct rt5081_pmu_charger_info *info)
  286. {
  287. int ret = 0;
  288. dprintf(CRITICAL, "%s: starts\n", __func__);
  289. ret = rt5081_set_bit(info, RT5081_PMU_REG_CORECTRL2,
  290. RT5081_MASK_CHG_RST);
  291. return ret;
  292. }
  293. #endif
  294. static int rt5081_set_battery_voreg(struct rt5081_pmu_charger_info *info,
  295. u32 voreg)
  296. {
  297. int ret = 0;
  298. u8 reg_voreg = 0;
  299. reg_voreg = rt5081_find_closest_reg_value(RT5081_BAT_VOREG_MIN,
  300. RT5081_BAT_VOREG_MAX, RT5081_BAT_VOREG_STEP,
  301. RT5081_BAT_VOREG_NUM, voreg);
  302. dprintf(CRITICAL, "%s: bat voreg = %d\n", __func__, voreg);
  303. ret = rt5081_i2c_update_bits(
  304. info,
  305. RT5081_PMU_REG_CHGCTRL4,
  306. RT5081_MASK_BAT_VOREG,
  307. reg_voreg << RT5081_SHIFT_BAT_VOREG
  308. );
  309. return ret;
  310. }
  311. static int rt5081_enable_wdt(struct rt5081_pmu_charger_info *info,
  312. bool en)
  313. {
  314. int ret = 0;
  315. dprintf(CRITICAL, "%s: en = %d\n", __func__, en);
  316. ret = (en ? rt5081_set_bit : rt5081_clr_bit)
  317. (info, RT5081_PMU_REG_CHGCTRL13, RT5081_MASK_WDT_EN);
  318. return ret;
  319. }
  320. static int rt5081_enable_jeita(struct rt5081_pmu_charger_info *info,
  321. bool en)
  322. {
  323. int ret = 0;
  324. dprintf(CRITICAL, "%s: en = %d\n", __func__, en);
  325. ret = (en ? rt5081_set_bit : rt5081_clr_bit)
  326. (info, RT5081_PMU_REG_CHGCTRL16, RT5081_MASK_JEITA_EN);
  327. return ret;
  328. }
  329. static int rt5081_chg_init_setting(struct rt5081_pmu_charger_info *info)
  330. {
  331. int ret = 0;
  332. dprintf(CRITICAL, "%s: starts\n", __func__);
  333. ret = rt5081_enable_wdt(info, true);
  334. if (ret < 0)
  335. dprintf(CRITICAL, "%s: enable wdt failed\n", __func__);
  336. /* Select input current limit to referenced from AICR */
  337. ret = rt5081_select_input_current_limit(info,
  338. RT5081_IINLMTSEL_AICR);
  339. if (ret < 0)
  340. dprintf(CRITICAL, "%s: select input current limit failed\n",
  341. __func__);
  342. mdelay(5);
  343. /* Disable HW iinlimit, use SW */
  344. ret = rt5081_enable_ilim(info, false);
  345. if (ret < 0)
  346. dprintf(CRITICAL, "%s: disable ilim failed\n", __func__);
  347. ret = rt5081_enable_jeita(info, false);
  348. if (ret < 0)
  349. dprintf(CRITICAL, "%s: disable jeita failed\n", __func__);
  350. ret = rt_charger_set_ichg(&info->mchr_info, 2000);
  351. if (ret < 0)
  352. dprintf(CRITICAL, "%s: set ichg failed\n", __func__);
  353. ret = rt_charger_set_aicr(&info->mchr_info, 500);
  354. if (ret < 0)
  355. dprintf(CRITICAL, "%s: set aicr failed\n", __func__);
  356. ret = rt_charger_set_mivr(&info->mchr_info, 4500);
  357. if (ret < 0)
  358. dprintf(CRITICAL, "%s: set mivr failed\n", __func__);
  359. ret = rt5081_set_battery_voreg(info, 4350);
  360. if (ret < 0)
  361. dprintf(CRITICAL, "%s: set cv failed\n", __func__);
  362. return ret;
  363. }
  364. static int rt5081_get_charging_status(struct rt5081_pmu_charger_info *info,
  365. enum rt5081_charging_status *chg_stat)
  366. {
  367. int ret = 0;
  368. u8 data = 0;
  369. ret = rt5081_i2c_read_byte(info, RT5081_PMU_REG_CHGSTAT, &data);
  370. if (ret != I2C_OK)
  371. return ret;
  372. *chg_stat = (data & RT5081_MASK_CHG_STAT) >> RT5081_SHIFT_CHG_STAT;
  373. return ret;
  374. }
  375. static int rt5081_get_mivr(struct rt5081_pmu_charger_info *info, u32 *mivr)
  376. {
  377. int ret = 0;
  378. u8 reg_mivr = 0;
  379. u8 data = 0;
  380. ret = rt5081_i2c_read_byte(info, RT5081_PMU_REG_CHGCTRL6, &data);
  381. if (ret != I2C_OK)
  382. return ret;
  383. reg_mivr = ((data & RT5081_MASK_MIVR) >> RT5081_SHIFT_MIVR) & 0xFF;
  384. *mivr = rt5081_find_closest_real_value(RT5081_MIVR_MIN, RT5081_MIVR_MAX,
  385. RT5081_MIVR_STEP, reg_mivr);
  386. return ret;
  387. }
  388. static int rt5081_is_charging_enable(struct rt5081_pmu_charger_info *info, bool *enable)
  389. {
  390. int ret = 0;
  391. u8 data = 0;
  392. ret = rt5081_i2c_read_byte(info, RT5081_PMU_REG_CHGCTRL2, &data);
  393. if (ret != I2C_OK)
  394. return ret;
  395. if (((data & RT5081_MASK_CHG_EN) >> RT5081_SHIFT_CHG_EN) & 0xFF)
  396. *enable = true;
  397. else
  398. *enable = false;
  399. return ret;
  400. }
  401. static int rt5081_get_ieoc(struct rt5081_pmu_charger_info *info, u32 *ieoc)
  402. {
  403. int ret = 0;
  404. u8 reg_ieoc = 0;
  405. u8 data = 0;
  406. ret = rt5081_i2c_read_byte(info, RT5081_PMU_REG_CHGCTRL9, &data);
  407. if (ret != I2C_OK)
  408. return ret;
  409. reg_ieoc = (data & RT5081_MASK_IEOC) >> RT5081_SHIFT_IEOC;
  410. *ieoc = rt5081_find_closest_real_value(RT5081_IEOC_MIN, RT5081_IEOC_MAX,
  411. RT5081_IEOC_STEP, reg_ieoc);
  412. return ret;
  413. }
  414. /* =========================================================== */
  415. /* The following is implementation for interface of rt_charger */
  416. /* =========================================================== */
  417. static int rt_charger_dump_register(struct mtk_charger_info *mchr_info)
  418. {
  419. int ret = 0;
  420. u32 i = 0;
  421. u32 ichg = 0, aicr = 0, mivr = 0, ieoc = 0;
  422. bool chg_enable = 0;
  423. enum rt5081_charging_status chg_status = RT5081_CHG_STATUS_READY;
  424. struct rt5081_pmu_charger_info *info =
  425. (struct rt5081_pmu_charger_info *)mchr_info;
  426. u8 data = 0;
  427. ret = rt5081_get_charging_status(info, &chg_status);
  428. if (chg_status == RT5081_CHG_STATUS_FAULT) {
  429. info->i2c_log_level = CRITICAL;
  430. for (i = 0; i < ARRAY_SIZE(rt5081_chg_reg_addr); i++) {
  431. ret = rt5081_i2c_read_byte(info, rt5081_chg_reg_addr[i],
  432. &data);
  433. if (ret != I2C_OK)
  434. return ret;
  435. }
  436. } else
  437. info->i2c_log_level = INFO;
  438. ret = rt_charger_get_ichg(mchr_info, &ichg);
  439. ret = rt5081_get_mivr(info, &mivr);
  440. ret = rt_charger_get_aicr(mchr_info, &aicr);
  441. ret = rt5081_get_ieoc(info, &ieoc);
  442. ret = rt5081_is_charging_enable(info, &chg_enable);
  443. dprintf(CRITICAL,
  444. "%s: ICHG = %dmA, AICR = %dmA, MIVR = %dmV, IEOC = %dmA\n",
  445. __func__, ichg, aicr, mivr, ieoc);
  446. dprintf(CRITICAL, "%s: CHG_EN = %d, CHG_STATUS = %s\n",
  447. __func__, chg_enable, rt5081_chg_status_name[chg_status]);
  448. return ret;
  449. }
  450. static int rt_charger_enable_charging(struct mtk_charger_info *mchr_info,
  451. bool enable)
  452. {
  453. int ret = 0;
  454. struct rt5081_pmu_charger_info *info =
  455. (struct rt5081_pmu_charger_info *)mchr_info;
  456. ret = (enable ? rt5081_set_bit : rt5081_clr_bit)
  457. (info, RT5081_PMU_REG_CHGCTRL2, RT5081_MASK_CHG_EN);
  458. return ret;
  459. }
  460. static int rt_charger_enable_power_path(struct mtk_charger_info *mchr_info,
  461. bool enable)
  462. {
  463. int ret = 0;
  464. struct rt5081_pmu_charger_info *info =
  465. (struct rt5081_pmu_charger_info *)mchr_info;
  466. dprintf(CRITICAL, "%s: enable = %d\n", __func__, enable);
  467. ret = (enable ? rt5081_clr_bit : rt5081_set_bit)
  468. (info, RT5081_PMU_REG_CHGCTRL1, RT5081_MASK_HZ_EN);
  469. return ret;
  470. }
  471. static int rt_charger_set_ichg(struct mtk_charger_info *mchr_info, u32 ichg)
  472. {
  473. int ret = 0;
  474. struct rt5081_pmu_charger_info *info =
  475. (struct rt5081_pmu_charger_info *)mchr_info;
  476. /* Find corresponding reg value */
  477. u8 reg_ichg = rt5081_find_closest_reg_value(RT5081_ICHG_MIN,
  478. RT5081_ICHG_MAX, RT5081_ICHG_STEP, RT5081_ICHG_NUM, ichg);
  479. dprintf(CRITICAL, "%s: ichg = %d\n", __func__, ichg);
  480. ret = rt5081_i2c_update_bits(
  481. info,
  482. RT5081_PMU_REG_CHGCTRL7,
  483. RT5081_MASK_ICHG,
  484. reg_ichg << RT5081_SHIFT_ICHG
  485. );
  486. return ret;
  487. }
  488. static int rt_charger_set_aicr(struct mtk_charger_info *mchr_info, u32 aicr)
  489. {
  490. int ret = 0;
  491. struct rt5081_pmu_charger_info *info =
  492. (struct rt5081_pmu_charger_info *)mchr_info;
  493. /* Find corresponding reg value */
  494. u8 reg_aicr = rt5081_find_closest_reg_value(RT5081_AICR_MIN,
  495. RT5081_AICR_MAX, RT5081_AICR_STEP, RT5081_AICR_NUM, aicr);
  496. dprintf(CRITICAL, "%s: aicr = %d\n", __func__, aicr);
  497. ret = rt5081_i2c_update_bits(
  498. info,
  499. RT5081_PMU_REG_CHGCTRL3,
  500. RT5081_MASK_AICR,
  501. reg_aicr << RT5081_SHIFT_AICR
  502. );
  503. return ret;
  504. }
  505. static int rt_charger_set_mivr(struct mtk_charger_info *mchr_info, u32 mivr)
  506. {
  507. int ret = 0;
  508. struct rt5081_pmu_charger_info *info =
  509. (struct rt5081_pmu_charger_info *)mchr_info;
  510. /* Find corresponding reg value */
  511. u8 reg_mivr = rt5081_find_closest_reg_value(RT5081_MIVR_MIN,
  512. RT5081_MIVR_MAX, RT5081_MIVR_STEP, RT5081_MIVR_NUM, mivr);
  513. dprintf(CRITICAL, "%s: mivr = %d\n", __func__, mivr);
  514. ret = rt5081_i2c_update_bits(
  515. info,
  516. RT5081_PMU_REG_CHGCTRL6,
  517. RT5081_MASK_MIVR,
  518. reg_mivr << RT5081_SHIFT_MIVR
  519. );
  520. return ret;
  521. }
  522. static int rt_charger_get_ichg(struct mtk_charger_info *mchr_info, u32 *ichg)
  523. {
  524. int ret = 0;
  525. u8 reg_ichg = 0;
  526. struct rt5081_pmu_charger_info *info =
  527. (struct rt5081_pmu_charger_info *)mchr_info;
  528. u8 data = 0;
  529. ret = rt5081_i2c_read_byte(info, RT5081_PMU_REG_CHGCTRL7, &data);
  530. if (ret != I2C_OK)
  531. return ret;
  532. reg_ichg = (data & RT5081_MASK_ICHG) >> RT5081_SHIFT_ICHG;
  533. *ichg = rt5081_find_closest_real_value(RT5081_ICHG_MIN, RT5081_ICHG_MAX,
  534. RT5081_ICHG_STEP, reg_ichg);
  535. return ret;
  536. }
  537. static int rt_charger_get_aicr(struct mtk_charger_info *mchr_info, u32 *aicr)
  538. {
  539. int ret = 0;
  540. u8 reg_aicr = 0;
  541. struct rt5081_pmu_charger_info *info =
  542. (struct rt5081_pmu_charger_info *)mchr_info;
  543. u8 data = 0;
  544. ret = rt5081_i2c_read_byte(info, RT5081_PMU_REG_CHGCTRL3, &data);
  545. if (ret != I2C_OK)
  546. return ret;
  547. reg_aicr = (data & RT5081_MASK_AICR) >> RT5081_SHIFT_AICR;
  548. *aicr = rt5081_find_closest_real_value(RT5081_AICR_MIN, RT5081_AICR_MAX,
  549. RT5081_AICR_STEP, reg_aicr);
  550. return ret;
  551. }
  552. static int rt_charger_reset_pumpx(struct mtk_charger_info *mchr_info,
  553. bool reset)
  554. {
  555. int ret = 0;
  556. u32 aicr = 0;
  557. aicr = (reset ? 100 : 3250);
  558. ret = rt_charger_set_aicr(mchr_info, aicr);
  559. return ret;
  560. }
  561. static struct mtk_charger_ops rt5081_mchr_ops = {
  562. .dump_register = rt_charger_dump_register,
  563. .enable_charging = rt_charger_enable_charging,
  564. .get_ichg = rt_charger_get_ichg,
  565. .set_ichg = rt_charger_set_ichg,
  566. .set_aicr = rt_charger_set_aicr,
  567. .set_mivr = rt_charger_set_mivr,
  568. .enable_power_path = rt_charger_enable_power_path,
  569. .get_aicr = rt_charger_get_aicr,
  570. .reset_pumpx = rt_charger_reset_pumpx,
  571. };
  572. /* Info of primary charger */
  573. static struct rt5081_pmu_charger_info g_rt5081_pmu_charger_info = {
  574. .mchr_info = {
  575. .name = "primary_charger",
  576. .device_id = -1,
  577. .mchr_ops = &rt5081_mchr_ops,
  578. },
  579. .i2c = {
  580. .id = I2C_APPM,
  581. .addr = RT5081_SLAVE_ADDR,
  582. .mode = HS_MODE,
  583. .speed = 3400,
  584. },
  585. .i2c_log_level = INFO,
  586. };
  587. int rt5081_chg_probe(void)
  588. {
  589. int ret = 0;
  590. /* Check primary charger */
  591. if (rt5081_is_hw_exist(&g_rt5081_pmu_charger_info)) {
  592. dprintf(CRITICAL, "%s: %s\n", __func__,
  593. RT5081_PMU_CHARGER_LK_DRV_VERSION);
  594. #if 0
  595. ret = rt5081_chg_reset_chip(&g_rt5081_pmu_charger_info);
  596. #endif
  597. ret = rt5081_chg_init_setting(&g_rt5081_pmu_charger_info);
  598. mtk_charger_set_info(&(g_rt5081_pmu_charger_info.mchr_info));
  599. }
  600. return ret;
  601. }
  602. /*
  603. * Revision Note
  604. * 1.0.2
  605. * (1) Modify sequence of enable_ilim & select iinlimit
  606. * and add 5ms delay between these two ops
  607. *
  608. * 1.0.1
  609. * (1) Disable JEITA
  610. * (2) Enable WDT
  611. *
  612. * 1.0.0
  613. * (1) Initial release
  614. */