rt9468.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  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/rt9468.h>
  37. #include <platform/mtk_charger_intf.h>
  38. #include <printf.h>
  39. #if !defined(CONFIG_POWER_EXT)
  40. #include <platform/upmu_common.h>
  41. #endif
  42. #define RT9468_LK_DRV_VERSION "1.0.0_MTK"
  43. /* ================= */
  44. /* Internal variable */
  45. /* ================= */
  46. struct rt9468_info {
  47. struct mtk_charger_info mchr_info;
  48. struct mt_i2c_t i2c;
  49. int i2c_log_level;
  50. };
  51. enum rt9468_charging_status {
  52. RT9468_CHG_STATUS_READY = 0,
  53. RT9468_CHG_STATUS_PROGRESS,
  54. RT9468_CHG_STATUS_DONE,
  55. RT9468_CHG_STATUS_FAULT,
  56. RT9468_CHG_STATUS_MAX,
  57. };
  58. enum rt9468_iin_limit_sel {
  59. RT9468_IIMLMTSEL_CHR_TYPE = 1,
  60. RT9468_IINLMTSEL_AICR = 2,
  61. RT9468_IINLMTSEL_LOWER_LEVEL, /* lower of above two */
  62. };
  63. /* Charging status name */
  64. static const char *rt9468_chg_status_name[RT9468_CHG_STATUS_MAX] = {
  65. "ready", "progress", "done", "fault",
  66. };
  67. /* ======================= */
  68. /* RT9468 Register Address */
  69. /* ======================= */
  70. static const unsigned char rt9468_reg_addr[] = {
  71. RT9468_REG_CORE_CTRL0,
  72. RT9468_REG_CHG_CTRL1,
  73. RT9468_REG_CHG_CTRL2,
  74. RT9468_REG_CHG_CTRL3,
  75. RT9468_REG_CHG_CTRL4,
  76. RT9468_REG_CHG_CTRL5,
  77. RT9468_REG_CHG_CTRL6,
  78. RT9468_REG_CHG_CTRL7,
  79. RT9468_REG_CHG_CTRL8,
  80. RT9468_REG_CHG_CTRL9,
  81. RT9468_REG_CHG_CTRL10,
  82. RT9468_REG_CHG_CTRL11,
  83. RT9468_REG_CHG_CTRL12,
  84. RT9468_REG_CHG_CTRL13,
  85. RT9468_REG_CHG_CTRL14,
  86. RT9468_REG_CHG_CTRL15,
  87. RT9468_REG_CHG_CTRL16,
  88. RT9468_REG_CHG_ADC,
  89. RT9468_REG_CHG_DPDM1,
  90. RT9468_REG_CHG_DPDM2,
  91. RT9468_REG_CHG_DPDM3,
  92. RT9468_REG_CHG_DPDM4,
  93. RT9468_REG_CHG_DPDM5,
  94. RT9468_REG_CHG_DPDM6,
  95. RT9468_REG_CHG_PUMP,
  96. RT9468_REG_CHG_CTRL17,
  97. RT9468_REG_CHG_CTRL18,
  98. RT9468_REG_CHG_DIRCHG1,
  99. RT9468_REG_CHG_DIRCHG2,
  100. RT9468_REG_CHG_DIRCHG3,
  101. RT9468_REG_CHG_DPDM7,
  102. RT9468_REG_CHG_DPDM8,
  103. RT9468_REG_DEVICE_ID,
  104. RT9468_REG_CHG_STAT,
  105. RT9468_REG_CHG_NTC,
  106. RT9468_REG_ADC_DATA_H,
  107. RT9468_REG_ADC_DATA_L,
  108. RT9468_REG_CHG_STATC,
  109. RT9468_REG_CHG_FAULT,
  110. RT9468_REG_TS_STATC,
  111. RT9468_REG_CHG_IRQ1,
  112. RT9468_REG_CHG_IRQ2,
  113. RT9468_REG_CHG_IRQ3,
  114. RT9468_REG_DPDM_IRQ,
  115. RT9468_REG_CHG_IRQ5,
  116. RT9468_REG_CHG_STATC_CTRL,
  117. RT9468_REG_CHG_FAULT_CTRL,
  118. RT9468_REG_TS_STATC_CTRL,
  119. RT9468_REG_CHG_IRQ1_CTRL,
  120. RT9468_REG_CHG_IRQ2_CTRL,
  121. RT9468_REG_CHG_IRQ3_CTRL,
  122. RT9468_REG_DPDM_IRQ_CTRL,
  123. RT9468_REG_CHG_IRQ5_CTRL,
  124. };
  125. /* ========================= */
  126. /* I2C operations */
  127. /* ========================= */
  128. static int rt9468_i2c_write_byte(struct rt9468_info *info, u8 cmd, u8 data)
  129. {
  130. unsigned int ret = I2C_OK;
  131. unsigned char write_buf[2] = {cmd, data};
  132. struct mt_i2c_t *i2c = &info->i2c;
  133. ret = i2c_write(i2c, write_buf, 2);
  134. if (ret != I2C_OK)
  135. dprintf(CRITICAL,
  136. "%s: I2CW[0x%02X] = 0x%02X failed, code = %d\n",
  137. __func__, cmd, data, ret);
  138. else
  139. dprintf(info->i2c_log_level, "%s: I2CW[0x%02X] = 0x%02X\n",
  140. __func__, cmd, data);
  141. return ret;
  142. }
  143. static int rt9468_i2c_read_byte(struct rt9468_info *info, u8 cmd, u8 *data)
  144. {
  145. int ret = I2C_OK;
  146. u8 ret_data = cmd;
  147. struct mt_i2c_t *i2c = &info->i2c;
  148. ret = i2c_write_read(i2c, &ret_data, 1, 1);
  149. if (ret != I2C_OK)
  150. dprintf(CRITICAL, "%s: I2CR[0x%02X] failed, code = %d\n",
  151. __func__, cmd, ret);
  152. else {
  153. dprintf(info->i2c_log_level, "%s: I2CR[0x%02X] = 0x%02X\n",
  154. __func__, cmd, ret_data);
  155. *data = ret_data;
  156. }
  157. return ret;
  158. }
  159. static int rt9468_i2c_test_bit(struct rt9468_info *info, u8 cmd, u8 shift)
  160. {
  161. int ret = 0;
  162. u8 data = 0;
  163. ret = rt9468_i2c_read_byte(info, cmd, &data);
  164. if (ret != I2C_OK)
  165. return ret;
  166. ret = data & (1 << shift);
  167. return ret;
  168. }
  169. static int rt9468_i2c_update_bits(struct rt9468_info *info, u8 cmd, u8 data,
  170. u8 mask)
  171. {
  172. int ret = 0;
  173. u8 reg_data = 0;
  174. ret = rt9468_i2c_read_byte(info, cmd, &reg_data);
  175. if (ret != I2C_OK)
  176. return ret;
  177. reg_data = reg_data & 0xFF;
  178. reg_data &= ~mask;
  179. reg_data |= (data & mask);
  180. return rt9468_i2c_write_byte(info, cmd, reg_data);
  181. }
  182. static inline int rt9468_set_bit(struct rt9468_info *info, u8 reg, u8 mask)
  183. {
  184. return rt9468_i2c_update_bits(info, reg, mask, mask);
  185. }
  186. static inline int rt9468_clr_bit(struct rt9468_info *info, u8 reg, u8 mask)
  187. {
  188. return rt9468_i2c_update_bits(info, reg, 0x00, mask);
  189. }
  190. /* ================== */
  191. /* internal functions */
  192. /* ================== */
  193. static int rt_charger_set_ichg(struct mtk_charger_info *mchr_info, u32 ichg);
  194. static int rt_charger_set_aicr(struct mtk_charger_info *mchr_info, u32 aicr);
  195. static int rt_charger_set_mivr(struct mtk_charger_info *mchr_info, u32 mivr);
  196. static int rt_charger_get_ichg(struct mtk_charger_info *mchr_info, u32 *ichg);
  197. static int rt_charger_get_aicr(struct mtk_charger_info *mchr_info, u32 *aicr);
  198. static u8 rt9468_find_closest_reg_value(const u32 min, const u32 max,
  199. const u32 step, const u32 num, const u32 target)
  200. {
  201. u32 i = 0, cur_val = 0, next_val = 0;
  202. /* Smaller than minimum supported value, use minimum one */
  203. if (target < min)
  204. return 0;
  205. for (i = 0; i < num - 1; i++) {
  206. cur_val = min + i * step;
  207. next_val = cur_val + step;
  208. if (cur_val > max)
  209. cur_val = max;
  210. if (next_val > max)
  211. next_val = max;
  212. if (target >= cur_val && target < next_val)
  213. return i;
  214. }
  215. /* Greater than maximum supported value, use maximum one */
  216. return num - 1;
  217. }
  218. static u8 rt9468_find_closest_reg_value_via_table(const u32 *value_table,
  219. const u32 table_size, const u32 target_value)
  220. {
  221. u32 i = 0;
  222. /* Smaller than minimum supported value, use minimum one */
  223. if (target_value < value_table[0])
  224. return 0;
  225. for (i = 0; i < table_size - 1; i++) {
  226. if (target_value >= value_table[i] && 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 rt9468_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 rt9468_enable_ilim(struct rt9468_info *info, bool enable)
  243. {
  244. int ret = 0;
  245. dprintf(CRITICAL, "%s: enable ilim = %d\n", __func__, enable);
  246. ret = (enable ? rt9468_set_bit : rt9468_clr_bit)
  247. (info, RT9468_REG_CHG_CTRL3, RT9468_MASK_ILIM_EN);
  248. return ret;
  249. }
  250. /* Select IINLMTSEL to use AICR */
  251. static int rt9468_select_input_current_limit(struct rt9468_info *info,
  252. enum rt9468_iin_limit_sel sel)
  253. {
  254. int ret = 0;
  255. dprintf(CRITICAL, "%s: select input current limit = %d\n",
  256. __func__, sel);
  257. ret = rt9468_i2c_update_bits(
  258. info,
  259. RT9468_REG_CHG_CTRL2,
  260. sel << RT9468_SHIFT_IINLMTSEL,
  261. RT9468_MASK_IINLMTSEL);
  262. return ret;
  263. }
  264. static int rt9468_set_battery_voreg(struct rt9468_info *info, u32 voreg)
  265. {
  266. int ret = 0;
  267. u8 reg_voreg = 0;
  268. reg_voreg = rt9468_find_closest_reg_value(RT9468_BAT_VOREG_MIN,
  269. RT9468_BAT_VOREG_MAX, RT9468_BAT_VOREG_STEP,
  270. RT9468_BAT_VOREG_NUM, voreg);
  271. dprintf(CRITICAL, "%s: bat voreg = %d\n", __func__, voreg);
  272. ret = rt9468_i2c_update_bits(
  273. info,
  274. RT9468_REG_CHG_CTRL4,
  275. reg_voreg << RT9468_SHIFT_BAT_VOREG,
  276. RT9468_MASK_BAT_VOREG
  277. );
  278. return ret;
  279. }
  280. static int rt9468_mask_all_irq(struct rt9468_info *info)
  281. {
  282. int i = 0, ret = 0;
  283. for (i = RT9468_REG_CHG_STATC_CTRL; i <= RT9468_REG_DPDM_IRQ_CTRL; i++) {
  284. ret = rt9468_i2c_write_byte(info, i, 0xFF);
  285. if (ret != I2C_OK)
  286. dprintf(CRITICAL,
  287. "%s: mask irq 0x%02X failed, ret = %d\n",
  288. __func__, i, ret);
  289. }
  290. /* This one is not continuous */
  291. ret = rt9468_i2c_write_byte(info, RT9468_REG_CHG_IRQ5_CTRL, 0xFF);
  292. dprintf(CRITICAL, "%s: mask irq 0x%02X failed, ret = %d\n",
  293. __func__, RT9468_REG_CHG_IRQ5_CTRL, ret);
  294. return ret;
  295. }
  296. static int rt9468_sw_init(struct rt9468_info *info)
  297. {
  298. int ret = 0;
  299. dprintf(CRITICAL, "%s: starts\n", __func__);
  300. /* Mask all irq */
  301. ret = rt9468_mask_all_irq(info);
  302. if (ret < 0)
  303. dprintf(CRITICAL, "%s: mask all irq failed\n", __func__);
  304. ret = rt9468_enable_ilim(info, false);
  305. if (ret < 0)
  306. dprintf(CRITICAL, "%s: disable ilim failed\n", __func__);
  307. /* Select input current limit to referenced from AICR */
  308. ret = rt9468_select_input_current_limit(info, RT9468_IINLMTSEL_AICR);
  309. if (ret < 0)
  310. dprintf(CRITICAL, "%s: select input current limit failed\n",
  311. __func__);
  312. ret = rt_charger_set_ichg(&info->mchr_info, 2000);
  313. if (ret < 0)
  314. dprintf(CRITICAL, "%s: set ichg failed\n", __func__);
  315. ret = rt_charger_set_aicr(&info->mchr_info, 500);
  316. if (ret < 0)
  317. dprintf(CRITICAL, "%s: set aicr failed\n", __func__);
  318. ret = rt_charger_set_mivr(&info->mchr_info, 4500);
  319. if (ret < 0)
  320. dprintf(CRITICAL, "%s: set mivr failed\n", __func__);
  321. ret = rt9468_set_battery_voreg(info, 4350);
  322. if (ret < 0)
  323. dprintf(CRITICAL, "%s: set voreg failed\n", __func__);
  324. return ret;
  325. }
  326. static int rt9468_get_charging_status(struct rt9468_info *info,
  327. enum rt9468_charging_status *chg_stat)
  328. {
  329. int ret = 0;
  330. u8 data = 0;
  331. ret = rt9468_i2c_read_byte(info, RT9468_REG_CHG_STAT, &data);
  332. if (ret != I2C_OK)
  333. return ret;
  334. *chg_stat = (data & RT9468_MASK_CHG_STAT) >> RT9468_SHIFT_CHG_STAT;
  335. return ret;
  336. }
  337. static int rt9468_get_ieoc(struct rt9468_info *info, u32 *ieoc)
  338. {
  339. int ret = 0;
  340. u8 reg_ieoc = 0;
  341. u8 data = 0;
  342. ret = rt9468_i2c_read_byte(info, RT9468_REG_CHG_CTRL9, &data);
  343. if (ret != I2C_OK)
  344. return ret;
  345. reg_ieoc = (data & RT9468_MASK_IEOC) >> RT9468_SHIFT_IEOC;
  346. *ieoc = rt9468_find_closest_real_value(RT9468_IEOC_MIN, RT9468_IEOC_MAX,
  347. RT9468_IEOC_STEP, reg_ieoc);
  348. return ret;
  349. }
  350. static bool rt9468_is_hw_exist(struct rt9468_info *info)
  351. {
  352. int ret = 0;
  353. u8 revision = 0;
  354. u8 data = 0;
  355. ret = rt9468_i2c_read_byte(info, RT9468_REG_DEVICE_ID, &data);
  356. if (ret != I2C_OK)
  357. return false;
  358. revision = data & 0xFF;
  359. if (revision == RT9468_DEVICE_ID_E2)
  360. dprintf(CRITICAL, "%s: rt9468 E2 revision\n", __func__);
  361. else if (revision == RT9468_DEVICE_ID_E3)
  362. dprintf(CRITICAL, "%s: rt9468 E3 revision\n", __func__);
  363. else
  364. return false;
  365. info->mchr_info.device_id = revision;
  366. return true;
  367. }
  368. static int rt9468_is_charging_enable(struct rt9468_info *info, bool *enable)
  369. {
  370. int ret = 0;
  371. u8 data = 0;
  372. ret = rt9468_i2c_read_byte(info, RT9468_REG_CHG_CTRL2, &data);
  373. if (ret != I2C_OK)
  374. return ret;
  375. if (((data & RT9468_MASK_CHG_EN) >> RT9468_SHIFT_CHG_EN) & 0xFF)
  376. *enable = true;
  377. else
  378. *enable = false;
  379. return ret;
  380. }
  381. static int rt9468_get_mivr(struct rt9468_info *info, u32 *mivr)
  382. {
  383. int ret = 0;
  384. u8 reg_mivr = 0;
  385. u8 data = 0;
  386. ret = rt9468_i2c_read_byte(info, RT9468_REG_CHG_CTRL6, &data);
  387. if (ret != I2C_OK)
  388. return ret;
  389. reg_mivr = ((data & RT9468_MASK_MIVR) >> RT9468_SHIFT_MIVR) & 0xFF;
  390. *mivr = rt9468_find_closest_real_value(RT9468_MIVR_MIN, RT9468_MIVR_MAX,
  391. RT9468_MIVR_STEP, reg_mivr);
  392. return ret;
  393. }
  394. /* Set register's value to default */
  395. static int rt9468_hw_init(struct rt9468_info *info)
  396. {
  397. int ret = 0;
  398. dprintf(CRITICAL, "%s: starts\n", __func__);
  399. ret = rt9468_set_bit(info, RT9468_REG_CORE_CTRL0, RT9468_MASK_RST);
  400. return ret;
  401. }
  402. /* =========================================================== */
  403. /* The following is implementation for interface of rt_charger */
  404. /* =========================================================== */
  405. static int rt_charger_dump_register(struct mtk_charger_info *mchr_info)
  406. {
  407. int i = 0, ret = 0;
  408. u32 ichg = 0, aicr = 0, mivr = 0, ieoc = 0;
  409. bool chg_enable = 0;
  410. enum rt9468_charging_status chg_status = RT9468_CHG_STATUS_READY;
  411. struct rt9468_info *info = (struct rt9468_info *)mchr_info;
  412. u8 data = 0;
  413. ret = rt9468_get_charging_status(info, &chg_status);
  414. if (chg_status == RT9468_CHG_STATUS_FAULT) {
  415. info->i2c_log_level = CRITICAL;
  416. for (i = 0; i < ARRAY_SIZE(rt9468_reg_addr); i++) {
  417. ret = rt9468_i2c_read_byte(info, rt9468_reg_addr[i],
  418. &data);
  419. if (ret != I2C_OK)
  420. return ret;
  421. }
  422. } else
  423. info->i2c_log_level = INFO;
  424. ret = rt_charger_get_ichg(&info->mchr_info, &ichg);
  425. ret = rt9468_get_mivr(info, &mivr);
  426. ret = rt_charger_get_aicr(&info->mchr_info, &aicr);
  427. ret = rt9468_get_ieoc(info, &ieoc);
  428. ret = rt9468_is_charging_enable(info, &chg_enable);
  429. dprintf(CRITICAL,
  430. "%s: ICHG = %dmA, AICR = %dmA, MIVR = %dmV, IEOC = %dmA\n",
  431. __func__, ichg, aicr, mivr, ieoc);
  432. dprintf(CRITICAL, "%s: CHG_EN = %d, CHG_STATUS = %s\n",
  433. __func__, chg_enable, rt9468_chg_status_name[chg_status]);
  434. return ret;
  435. }
  436. static int rt_charger_enable_charging(struct mtk_charger_info *mchr_info,
  437. bool enable)
  438. {
  439. int ret = 0;
  440. struct rt9468_info *info = (struct rt9468_info *)mchr_info;
  441. ret = (enable ? rt9468_set_bit : rt9468_clr_bit)
  442. (info, RT9468_REG_CHG_CTRL2, RT9468_MASK_CHG_EN);
  443. return ret;
  444. }
  445. static int rt_charger_enable_power_path(struct mtk_charger_info *mchr_info,
  446. bool enable)
  447. {
  448. int ret = 0;
  449. struct rt9468_info *info = (struct rt9468_info *)mchr_info;
  450. ret = (enable ? rt9468_clr_bit : rt9468_set_bit)
  451. (info, RT9468_REG_CHG_CTRL1, RT9468_MASK_HZ_EN);
  452. return ret;
  453. }
  454. static int rt_charger_set_ichg(struct mtk_charger_info *mchr_info, u32 ichg)
  455. {
  456. int ret = 0;
  457. struct rt9468_info *info = (struct rt9468_info *)mchr_info;
  458. /* Find corresponding reg value */
  459. u8 reg_ichg = rt9468_find_closest_reg_value(RT9468_ICHG_MIN,
  460. RT9468_ICHG_MAX, RT9468_ICHG_STEP, RT9468_ICHG_NUM, ichg);
  461. dprintf(CRITICAL, "%s: ichg = %d\n", __func__, ichg);
  462. ret = rt9468_i2c_update_bits(
  463. info,
  464. RT9468_REG_CHG_CTRL7,
  465. reg_ichg << RT9468_SHIFT_ICHG,
  466. RT9468_MASK_ICHG
  467. );
  468. return ret;
  469. }
  470. static int rt_charger_set_aicr(struct mtk_charger_info *mchr_info, u32 aicr)
  471. {
  472. int ret = 0;
  473. struct rt9468_info *info = (struct rt9468_info *)mchr_info;
  474. /* Find corresponding reg value */
  475. u8 reg_aicr = rt9468_find_closest_reg_value(RT9468_AICR_MIN,
  476. RT9468_AICR_MAX, RT9468_AICR_STEP, RT9468_AICR_NUM, aicr);
  477. dprintf(CRITICAL, "%s: aicr = %d\n", __func__, aicr);
  478. ret = rt9468_i2c_update_bits(
  479. info,
  480. RT9468_REG_CHG_CTRL3,
  481. reg_aicr << RT9468_SHIFT_AICR,
  482. RT9468_MASK_AICR
  483. );
  484. return ret;
  485. }
  486. static int rt_charger_set_mivr(struct mtk_charger_info *mchr_info, u32 mivr)
  487. {
  488. int ret = 0;
  489. struct rt9468_info *info = (struct rt9468_info *)mchr_info;
  490. /* Find corresponding reg value */
  491. u8 reg_mivr = rt9468_find_closest_reg_value(RT9468_MIVR_MIN,
  492. RT9468_MIVR_MAX, RT9468_MIVR_STEP, RT9468_MIVR_NUM, mivr);
  493. dprintf(CRITICAL, "%s: mivr = %d\n", __func__, mivr);
  494. ret = rt9468_i2c_update_bits(
  495. info,
  496. RT9468_REG_CHG_CTRL6,
  497. reg_mivr << RT9468_SHIFT_MIVR,
  498. RT9468_MASK_MIVR
  499. );
  500. return ret;
  501. }
  502. static int rt_charger_get_ichg(struct mtk_charger_info *mchr_info, u32 *ichg)
  503. {
  504. int ret = 0;
  505. u8 reg_ichg = 0;
  506. struct rt9468_info *info = (struct rt9468_info *)mchr_info;
  507. u8 data = 0;
  508. ret = rt9468_i2c_read_byte(info, RT9468_REG_CHG_CTRL7, &data);
  509. if (ret != I2C_OK)
  510. return ret;
  511. reg_ichg = (data & RT9468_MASK_ICHG) >> RT9468_SHIFT_ICHG;
  512. *ichg = rt9468_find_closest_real_value(RT9468_ICHG_MIN,
  513. RT9468_ICHG_MAX, RT9468_ICHG_STEP, reg_ichg);
  514. return ret;
  515. }
  516. static int rt_charger_get_aicr(struct mtk_charger_info *mchr_info, u32 *aicr)
  517. {
  518. int ret = 0;
  519. u8 reg_aicr = 0;
  520. struct rt9468_info *info = (struct rt9468_info *)mchr_info;
  521. u8 data = 0;
  522. ret = rt9468_i2c_read_byte(info, RT9468_REG_CHG_CTRL3, &data);
  523. if (ret != I2C_OK)
  524. return ret;
  525. reg_aicr = (data & RT9468_MASK_AICR) >> RT9468_SHIFT_AICR;
  526. *aicr = rt9468_find_closest_real_value(RT9468_AICR_MIN,
  527. RT9468_AICR_MAX, RT9468_AICR_STEP, reg_aicr);
  528. return ret;
  529. }
  530. static int rt_charger_reset_pumpx(struct mtk_charger_info *mchr_info,
  531. bool reset)
  532. {
  533. int ret = 0;
  534. u32 aicr = 0;
  535. aicr = (reset ? 100 : 3250);
  536. ret = rt_charger_set_aicr(mchr_info, aicr);
  537. return ret;
  538. }
  539. static struct mtk_charger_ops rt9468_mchr_ops = {
  540. .dump_register = rt_charger_dump_register,
  541. .enable_charging = rt_charger_enable_charging,
  542. .get_ichg = rt_charger_get_ichg,
  543. .set_ichg = rt_charger_set_ichg,
  544. .set_aicr = rt_charger_set_aicr,
  545. .set_mivr = rt_charger_set_mivr,
  546. .enable_power_path = rt_charger_enable_power_path,
  547. .get_aicr = rt_charger_get_aicr,
  548. .reset_pumpx = rt_charger_reset_pumpx,
  549. };
  550. /* Info of primary charger */
  551. static struct rt9468_info g_rt9468_info = {
  552. .mchr_info = {
  553. .name = "primary_charger",
  554. .alias_name = "rt9468",
  555. .device_id = -1,
  556. .mchr_ops = &rt9468_mchr_ops,
  557. },
  558. .i2c = {
  559. .id = I2C1,
  560. .addr = RT9468_SLAVE_ADDR,
  561. .mode = ST_MODE,
  562. .speed = 100,
  563. },
  564. .i2c_log_level = INFO,
  565. };
  566. int rt9468_probe(void)
  567. {
  568. int ret = 0;
  569. /* Check primary charger */
  570. if (rt9468_is_hw_exist(&g_rt9468_info)) {
  571. ret = rt9468_hw_init(&g_rt9468_info);
  572. ret = rt9468_sw_init(&g_rt9468_info);
  573. mtk_charger_set_info(&(g_rt9468_info.mchr_info));
  574. }
  575. return ret;
  576. }