rt9466.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  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/rt9466.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 RT9466_LK_DRV_VERSION "1.0.3_MTK"
  43. /* ================= */
  44. /* Internal variable */
  45. /* ================= */
  46. struct rt9466_info {
  47. struct mtk_charger_info mchr_info;
  48. struct mt_i2c_t i2c;
  49. int i2c_log_level;
  50. };
  51. /*
  52. * Not to unmask the following IRQs
  53. * PWR_RDYM/CHG_MIVRM/CHG_AICRM
  54. * VBUSOVM
  55. * TS_BAT_HOTM/TS_BAT_WARMM/TS_BAT_COOLM/TS_BAT_COLDM
  56. * CHG_OTPM/CHG_RVPM/CHG_ADPBADM/CHG_STATCM/CHG_FAULTM/TS_STATCM
  57. * IEOCM/TERMM/SSFINISHM/SSFINISHM/AICLMeasM
  58. * BST_BATUVM/PUMPX_DONEM/ADC_DONEM
  59. */
  60. static const u8 rt9466_irq_init_data[] = {
  61. RT9466_MASK_PWR_RDYM | RT9466_MASK_CHG_MIVRM | RT9466_MASK_CHG_AICRM,
  62. RT9466_MASK_VBUSOVM,
  63. RT9466_MASK_TS_BAT_HOTM | RT9466_MASK_TS_BAT_WARMM |
  64. RT9466_MASK_TS_BAT_COOLM | RT9466_MASK_TS_BAT_COLDM |
  65. RT9466_MASK_TS_STATC_RESERVED,
  66. RT9466_MASK_CHG_OTPM | RT9466_MASK_CHG_RVPM | RT9466_MASK_CHG_ADPBADM |
  67. RT9466_MASK_CHG_STATCM | RT9466_MASK_CHG_FAULTM | RT9466_MASK_TS_STATCM,
  68. RT9466_MASK_IEOCM | RT9466_MASK_TERMM | RT9466_MASK_SSFINISHM |
  69. RT9466_MASK_CHG_AICLMEASM | RT9466_MASK_IRQ2_RESERVED,
  70. RT9466_MASK_BST_BATUVM | RT9466_MASK_PUMPX_DONEM |
  71. RT9466_MASK_ADC_DONEM | RT9466_MASK_IRQ3_RESERVED,
  72. };
  73. static const u8 rt9466_irq_maskall_data[] = {
  74. 0xF0, 0xF0, 0xFF, 0xFF, 0xFF, 0xFF
  75. };
  76. enum rt9466_charging_status {
  77. RT9466_CHG_STATUS_READY = 0,
  78. RT9466_CHG_STATUS_PROGRESS,
  79. RT9466_CHG_STATUS_DONE,
  80. RT9466_CHG_STATUS_FAULT,
  81. RT9466_CHG_STATUS_MAX,
  82. };
  83. /* Charging status name */
  84. static const char *rt9466_chg_status_name[RT9466_CHG_STATUS_MAX] = {
  85. "ready", "progress", "done", "fault",
  86. };
  87. /* ======================= */
  88. /* Address & Default value */
  89. /* ======================= */
  90. static const unsigned char rt9466_reg_addr[] = {
  91. RT9466_REG_CORE_CTRL0,
  92. RT9466_REG_CHG_CTRL1,
  93. RT9466_REG_CHG_CTRL2,
  94. RT9466_REG_CHG_CTRL3,
  95. RT9466_REG_CHG_CTRL4,
  96. RT9466_REG_CHG_CTRL5,
  97. RT9466_REG_CHG_CTRL6,
  98. RT9466_REG_CHG_CTRL7,
  99. RT9466_REG_CHG_CTRL8,
  100. RT9466_REG_CHG_CTRL9,
  101. RT9466_REG_CHG_CTRL10,
  102. RT9466_REG_CHG_CTRL11,
  103. RT9466_REG_CHG_CTRL12,
  104. RT9466_REG_CHG_CTRL13,
  105. RT9466_REG_CHG_CTRL14,
  106. RT9466_REG_CHG_CTRL15,
  107. RT9466_REG_CHG_CTRL16,
  108. RT9466_REG_CHG_ADC,
  109. RT9466_REG_CHG_CTRL17,
  110. RT9466_REG_CHG_CTRL18,
  111. RT9466_REG_DEVICE_ID,
  112. RT9466_REG_CHG_STAT,
  113. RT9466_REG_CHG_NTC,
  114. RT9466_REG_ADC_DATA_H,
  115. RT9466_REG_ADC_DATA_L,
  116. RT9466_REG_CHG_STATC,
  117. RT9466_REG_CHG_FAULT,
  118. RT9466_REG_TS_STATC,
  119. RT9466_REG_CHG_IRQ1,
  120. RT9466_REG_CHG_IRQ2,
  121. RT9466_REG_CHG_IRQ3,
  122. RT9466_REG_CHG_STATC_CTRL,
  123. RT9466_REG_CHG_FAULT_CTRL,
  124. RT9466_REG_TS_STATC_CTRL,
  125. RT9466_REG_CHG_IRQ1_CTRL,
  126. RT9466_REG_CHG_IRQ2_CTRL,
  127. RT9466_REG_CHG_IRQ3_CTRL,
  128. };
  129. enum rt9466_iin_limit_sel {
  130. RT9466_IIMLMTSEL_PSEL_OTG,
  131. RT9466_IINLMTSEL_AICR = 2,
  132. RT9466_IINLMTSEL_LOWER_LEVEL, /* lower of above two */
  133. };
  134. /* ========================= */
  135. /* I2C operations */
  136. /* ========================= */
  137. static int rt9466_i2c_write_byte(struct rt9466_info *info, u8 cmd, u8 data)
  138. {
  139. unsigned int ret = I2C_OK;
  140. unsigned char write_buf[2] = {cmd, data};
  141. struct mt_i2c_t *i2c = &info->i2c;
  142. ret = i2c_write(i2c, write_buf, 2);
  143. if (ret != I2C_OK)
  144. dprintf(CRITICAL,
  145. "%s: I2CW[0x%02X] = 0x%02X failed, code = %d\n",
  146. __func__, cmd, data, ret);
  147. else
  148. dprintf(info->i2c_log_level, "%s: I2CW[0x%02X] = 0x%02X\n",
  149. __func__, cmd, data);
  150. return ret;
  151. }
  152. static int rt9466_i2c_read_byte(struct rt9466_info *info, u8 cmd, u8 *data)
  153. {
  154. int ret = I2C_OK;
  155. u8 ret_data = cmd;
  156. struct mt_i2c_t *i2c = &info->i2c;
  157. ret = i2c_write_read(i2c, &ret_data, 1, 1);
  158. if (ret != I2C_OK)
  159. dprintf(CRITICAL, "%s: I2CR[0x%02X] failed, code = %d\n",
  160. __func__, cmd, ret);
  161. else {
  162. dprintf(info->i2c_log_level, "%s: I2CR[0x%02X] = 0x%02X\n",
  163. __func__, cmd, ret_data);
  164. *data = ret_data;
  165. }
  166. return ret;
  167. }
  168. static int rt9466_i2c_test_bit(struct rt9466_info *info, u8 cmd, u8 shift)
  169. {
  170. int ret = 0;
  171. u8 data = 0;
  172. ret = rt9466_i2c_read_byte(info, cmd, &data);
  173. if (ret != I2C_OK)
  174. return ret;
  175. ret = data & (1 << shift);
  176. return ret;
  177. }
  178. static int rt9466_i2c_update_bits(struct rt9466_info *info, u8 cmd, u8 data,
  179. u8 mask)
  180. {
  181. int ret = 0;
  182. u8 reg_data = 0;
  183. ret = rt9466_i2c_read_byte(info, cmd, &reg_data);
  184. if (ret != I2C_OK)
  185. return ret;
  186. reg_data = reg_data & 0xFF;
  187. reg_data &= ~mask;
  188. reg_data |= (data & mask);
  189. return rt9466_i2c_write_byte(info, cmd, reg_data);
  190. }
  191. static inline int rt9466_set_bit(struct rt9466_info *info, u8 reg, u8 mask)
  192. {
  193. return rt9466_i2c_update_bits(info, reg, mask, mask);
  194. }
  195. static inline int rt9466_clr_bit(struct rt9466_info *info, u8 reg, u8 mask)
  196. {
  197. return rt9466_i2c_update_bits(info, reg, 0x00, mask);
  198. }
  199. /* ================== */
  200. /* internal functions */
  201. /* ================== */
  202. static int rt_charger_set_ichg(struct mtk_charger_info *mchr_info, u32 ichg);
  203. static int rt_charger_set_aicr(struct mtk_charger_info *mchr_info, u32 aicr);
  204. static int rt_charger_set_mivr(struct mtk_charger_info *mchr_info, u32 mivr);
  205. static int rt_charger_get_ichg(struct mtk_charger_info *mchr_info, u32 *ichg);
  206. static int rt_charger_get_aicr(struct mtk_charger_info *mchr_info, u32 *aicr);
  207. static u8 rt9466_find_closest_reg_value(const u32 min, const u32 max,
  208. const u32 step, const u32 num, const u32 target)
  209. {
  210. u32 i = 0, cur_val = 0, next_val = 0;
  211. /* Smaller than minimum supported value, use minimum one */
  212. if (target < min)
  213. return 0;
  214. for (i = 0; i < num - 1; i++) {
  215. cur_val = min + i * step;
  216. next_val = cur_val + step;
  217. if (cur_val > max)
  218. cur_val = max;
  219. if (next_val > max)
  220. next_val = max;
  221. if (target >= cur_val && target < next_val)
  222. return i;
  223. }
  224. /* Greater than maximum supported value, use maximum one */
  225. return num - 1;
  226. }
  227. static u8 rt9466_find_closest_reg_value_via_table(const u32 *value_table,
  228. const u32 table_size, const u32 target_value)
  229. {
  230. u32 i = 0;
  231. /* Smaller than minimum supported value, use minimum one */
  232. if (target_value < value_table[0])
  233. return 0;
  234. for (i = 0; i < table_size - 1; i++) {
  235. if (target_value >= value_table[i] && target_value < value_table[i + 1])
  236. return i;
  237. }
  238. /* Greater than maximum supported value, use maximum one */
  239. return table_size - 1;
  240. }
  241. static u32 rt9466_find_closest_real_value(const u32 min, const u32 max,
  242. const u32 step, const u8 reg_val)
  243. {
  244. u32 ret_val = 0;
  245. ret_val = min + reg_val * step;
  246. if (ret_val > max)
  247. ret_val = max;
  248. return ret_val;
  249. }
  250. /* Hardware pin current limit */
  251. static int rt9466_enable_ilim(struct rt9466_info *info, bool enable)
  252. {
  253. int ret = 0;
  254. dprintf(CRITICAL, "%s: enable ilim = %d\n", __func__, enable);
  255. ret = (enable ? rt9466_set_bit : rt9466_clr_bit)
  256. (info, RT9466_REG_CHG_CTRL3, RT9466_MASK_ILIM_EN);
  257. return ret;
  258. }
  259. /* Select IINLMTSEL to use AICR */
  260. static int rt9466_select_input_current_limit(struct rt9466_info *info,
  261. enum rt9466_iin_limit_sel sel)
  262. {
  263. int ret = 0;
  264. dprintf(CRITICAL, "%s: select input current limit = %d\n",
  265. __func__, sel);
  266. ret = rt9466_i2c_update_bits(
  267. info,
  268. RT9466_REG_CHG_CTRL2,
  269. sel << RT9466_SHIFT_IINLMTSEL,
  270. RT9466_MASK_IINLMTSEL);
  271. return ret;
  272. }
  273. static bool rt9466_is_hw_exist(struct rt9466_info *info)
  274. {
  275. int ret = 0;
  276. u8 device_id = 0, vendor_id = 0, chip_rev = 0;
  277. ret = rt9466_i2c_read_byte(info, RT9466_REG_DEVICE_ID, &device_id);
  278. if (ret != I2C_OK)
  279. return false;
  280. vendor_id = device_id & 0xF0;
  281. chip_rev = device_id & 0x0F;
  282. if (vendor_id != RT9466_VENDOR_ID) {
  283. dprintf(CRITICAL, "%s: vendor id is incorrect (0x%02X)\n",
  284. __func__, vendor_id);
  285. return false;
  286. }
  287. dprintf(CRITICAL, "%s: chip rev(E%d,0x%02X)\n", __func__, chip_rev,
  288. chip_rev);
  289. info->mchr_info.device_id = chip_rev;
  290. return true;
  291. }
  292. /* Set register's value to default */
  293. static int rt9466_reset_chip(struct rt9466_info *info)
  294. {
  295. int ret = 0;
  296. dprintf(CRITICAL, "%s: starts\n", __func__);
  297. ret = rt9466_set_bit(info, RT9466_REG_CORE_CTRL0, RT9466_MASK_RST);
  298. return ret;
  299. }
  300. static int rt9466_set_battery_voreg(struct rt9466_info *info, u32 voreg)
  301. {
  302. int ret = 0;
  303. u8 reg_voreg = 0;
  304. reg_voreg = rt9466_find_closest_reg_value(RT9466_BAT_VOREG_MIN,
  305. RT9466_BAT_VOREG_MAX, RT9466_BAT_VOREG_STEP,
  306. RT9466_BAT_VOREG_NUM, voreg);
  307. dprintf(CRITICAL, "%s: bat voreg = %d\n", __func__, voreg);
  308. ret = rt9466_i2c_update_bits(
  309. info,
  310. RT9466_REG_CHG_CTRL4,
  311. reg_voreg << RT9466_SHIFT_BAT_VOREG,
  312. RT9466_MASK_BAT_VOREG
  313. );
  314. return ret;
  315. }
  316. static int rt9466_mask_all_irq(struct rt9466_info *info)
  317. {
  318. int i = 0, ret = 0;
  319. for (i = 0; i < ARRAY_SIZE(rt9466_irq_maskall_data); i++) {
  320. ret = rt9466_i2c_write_byte(info, RT9466_REG_CHG_STATC_CTRL + i,
  321. rt9466_irq_maskall_data[i]);
  322. if (ret != I2C_OK)
  323. dprintf(CRITICAL,
  324. "%s: mask irq 0x%02X failed, ret = %d\n",
  325. __func__, i, ret);
  326. }
  327. return ret;
  328. }
  329. static int rt9466_enable_watchdog_timer(struct rt9466_info *info, bool enable)
  330. {
  331. int ret = 0;
  332. ret = (enable ? rt9466_set_bit : rt9466_clr_bit)
  333. (info, RT9466_REG_CHG_CTRL13, RT9466_MASK_WDT_EN);
  334. return ret;
  335. }
  336. static int rt9466_init_setting(struct rt9466_info *info)
  337. {
  338. int ret = 0;
  339. dprintf(CRITICAL, "%s: starts\n", __func__);
  340. /* Mask all IRQs */
  341. ret = rt9466_mask_all_irq(info);
  342. if (ret < 0)
  343. dprintf(CRITICAL, "%s: mask all irq failed\n", __func__);
  344. /* Disable HW iinlimit, use SW */
  345. ret = rt9466_enable_ilim(info, false);
  346. if (ret < 0)
  347. dprintf(CRITICAL, "%s: disable ilim failed\n", __func__);
  348. /* Select input current limit to referenced from AICR */
  349. ret = rt9466_select_input_current_limit(info, RT9466_IINLMTSEL_AICR);
  350. if (ret < 0)
  351. dprintf(CRITICAL, "%s: select input current limit failed\n",
  352. __func__);
  353. ret = rt_charger_set_ichg(&info->mchr_info, 2000);
  354. if (ret < 0)
  355. dprintf(CRITICAL, "%s: set ichg failed\n", __func__);
  356. ret = rt_charger_set_aicr(&info->mchr_info, 500);
  357. if (ret < 0)
  358. dprintf(CRITICAL, "%s: set aicr failed\n", __func__);
  359. ret = rt_charger_set_mivr(&info->mchr_info, 4500);
  360. if (ret < 0)
  361. dprintf(CRITICAL, "%s: set mivr failed\n", __func__);
  362. ret = rt9466_set_battery_voreg(info, 4350);
  363. if (ret < 0)
  364. dprintf(CRITICAL, "%s: set cv failed\n", __func__);
  365. ret = rt9466_enable_watchdog_timer(info, true);
  366. if (ret < 0)
  367. dprintf(CRITICAL, "%s: enable wdt failed\n",__func__);
  368. return ret;
  369. }
  370. static int rt9466_get_charging_status(struct rt9466_info *info,
  371. enum rt9466_charging_status *chg_stat)
  372. {
  373. int ret = 0;
  374. u8 data = 0;
  375. ret = rt9466_i2c_read_byte(info, RT9466_REG_CHG_STAT, &data);
  376. if (ret != I2C_OK)
  377. return ret;
  378. *chg_stat = (data & RT9466_MASK_CHG_STAT) >> RT9466_SHIFT_CHG_STAT;
  379. return ret;
  380. }
  381. static int rt9466_get_mivr(struct rt9466_info *info, u32 *mivr)
  382. {
  383. int ret = 0;
  384. u8 reg_mivr = 0;
  385. u8 data = 0;
  386. ret = rt9466_i2c_read_byte(info, RT9466_REG_CHG_CTRL6, &data);
  387. if (ret != I2C_OK)
  388. return ret;
  389. reg_mivr = ((data & RT9466_MASK_MIVR) >> RT9466_SHIFT_MIVR) & 0xFF;
  390. *mivr = rt9466_find_closest_real_value(RT9466_MIVR_MIN, RT9466_MIVR_MAX,
  391. RT9466_MIVR_STEP, reg_mivr);
  392. return ret;
  393. }
  394. static int rt9466_is_charging_enable(struct rt9466_info *info, bool *enable)
  395. {
  396. int ret = 0;
  397. u8 data = 0;
  398. ret = rt9466_i2c_read_byte(info, RT9466_REG_CHG_CTRL2, &data);
  399. if (ret != I2C_OK)
  400. return ret;
  401. if (((data & RT9466_MASK_CHG_EN) >> RT9466_SHIFT_CHG_EN) & 0xFF)
  402. *enable = true;
  403. else
  404. *enable = false;
  405. return ret;
  406. }
  407. static int rt9466_get_ieoc(struct rt9466_info *info, u32 *ieoc)
  408. {
  409. int ret = 0;
  410. u8 reg_ieoc = 0;
  411. u8 data = 0;
  412. ret = rt9466_i2c_read_byte(info, RT9466_REG_CHG_CTRL9, &data);
  413. if (ret != I2C_OK)
  414. return ret;
  415. reg_ieoc = (data & RT9466_MASK_IEOC) >> RT9466_SHIFT_IEOC;
  416. *ieoc = rt9466_find_closest_real_value(RT9466_IEOC_MIN, RT9466_IEOC_MAX,
  417. RT9466_IEOC_STEP, reg_ieoc);
  418. return ret;
  419. }
  420. /* =========================================================== */
  421. /* The following is implementation for interface of rt_charger */
  422. /* =========================================================== */
  423. static int rt_charger_dump_register(struct mtk_charger_info *mchr_info)
  424. {
  425. int i = 0, ret = 0;
  426. u32 ichg = 0, aicr = 0, mivr = 0, ieoc = 0;
  427. bool chg_enable = 0;
  428. enum rt9466_charging_status chg_status = RT9466_CHG_STATUS_READY;
  429. struct rt9466_info *info = (struct rt9466_info *)mchr_info;
  430. u8 data = 0;
  431. ret = rt9466_get_charging_status(info, &chg_status);
  432. if (chg_status == RT9466_CHG_STATUS_FAULT) {
  433. info->i2c_log_level = CRITICAL;
  434. for (i = 0; i < ARRAY_SIZE(rt9466_reg_addr); i++) {
  435. ret = rt9466_i2c_read_byte(info, rt9466_reg_addr[i],
  436. &data);
  437. if (ret != I2C_OK)
  438. return ret;
  439. }
  440. } else
  441. info->i2c_log_level = INFO;
  442. ret = rt_charger_get_ichg(mchr_info, &ichg);
  443. ret = rt9466_get_mivr(info, &mivr);
  444. ret = rt_charger_get_aicr(mchr_info, &aicr);
  445. ret = rt9466_get_ieoc(info, &ieoc);
  446. ret = rt9466_is_charging_enable(info, &chg_enable);
  447. dprintf(CRITICAL,
  448. "%s: ICHG = %dmA, AICR = %dmA, MIVR = %dmV, IEOC = %dmA\n",
  449. __func__, ichg, aicr, mivr, ieoc);
  450. dprintf(CRITICAL, "%s: CHG_EN = %d, CHG_STATUS = %s\n",
  451. __func__, chg_enable, rt9466_chg_status_name[chg_status]);
  452. return ret;
  453. }
  454. static int rt_charger_enable_charging(struct mtk_charger_info *mchr_info,
  455. bool enable)
  456. {
  457. int ret = 0;
  458. struct rt9466_info *info = (struct rt9466_info *)mchr_info;
  459. dprintf(CRITICAL, "%s: enable = %d\n", __func__, enable);
  460. ret = (enable ? rt9466_set_bit : rt9466_clr_bit)
  461. (info, RT9466_REG_CHG_CTRL2, RT9466_MASK_CHG_EN);
  462. return ret;
  463. }
  464. static int rt_charger_enable_power_path(struct mtk_charger_info *mchr_info,
  465. bool enable)
  466. {
  467. int ret = 0;
  468. struct rt9466_info *info = (struct rt9466_info *)mchr_info;
  469. u32 mivr = enable ? 4500 : RT9466_MIVR_MAX;
  470. dprintf(CRITICAL, "%s: enable = %d\n", __func__, enable);
  471. ret = rt_charger_set_mivr(&info->mchr_info, mivr);
  472. return ret;
  473. }
  474. static int rt_charger_set_ichg(struct mtk_charger_info *mchr_info, u32 ichg)
  475. {
  476. int ret = 0;
  477. struct rt9466_info *info = (struct rt9466_info *)mchr_info;
  478. /* Find corresponding reg value */
  479. u8 reg_ichg = rt9466_find_closest_reg_value(RT9466_ICHG_MIN,
  480. RT9466_ICHG_MAX, RT9466_ICHG_STEP, RT9466_ICHG_NUM, ichg);
  481. dprintf(CRITICAL, "%s: ichg = %d\n", __func__, ichg);
  482. ret = rt9466_i2c_update_bits(
  483. info,
  484. RT9466_REG_CHG_CTRL7,
  485. reg_ichg << RT9466_SHIFT_ICHG,
  486. RT9466_MASK_ICHG
  487. );
  488. return ret;
  489. }
  490. static int rt_charger_set_aicr(struct mtk_charger_info *mchr_info, u32 aicr)
  491. {
  492. int ret = 0;
  493. struct rt9466_info *info = (struct rt9466_info *)mchr_info;
  494. /* Find corresponding reg value */
  495. u8 reg_aicr = rt9466_find_closest_reg_value(RT9466_AICR_MIN,
  496. RT9466_AICR_MAX, RT9466_AICR_STEP, RT9466_AICR_NUM, aicr);
  497. dprintf(CRITICAL, "%s: aicr = %d\n", __func__, aicr);
  498. ret = rt9466_i2c_update_bits(
  499. info,
  500. RT9466_REG_CHG_CTRL3,
  501. reg_aicr << RT9466_SHIFT_AICR,
  502. RT9466_MASK_AICR
  503. );
  504. return ret;
  505. }
  506. static int rt_charger_set_mivr(struct mtk_charger_info *mchr_info, u32 mivr)
  507. {
  508. int ret = 0;
  509. struct rt9466_info *info = (struct rt9466_info *)mchr_info;
  510. /* Find corresponding reg value */
  511. u8 reg_mivr = rt9466_find_closest_reg_value(RT9466_MIVR_MIN,
  512. RT9466_MIVR_MAX, RT9466_MIVR_STEP, RT9466_MIVR_NUM, mivr);
  513. dprintf(CRITICAL, "%s: mivr = %d\n", __func__, mivr);
  514. ret = rt9466_i2c_update_bits(
  515. info,
  516. RT9466_REG_CHG_CTRL6,
  517. reg_mivr << RT9466_SHIFT_MIVR,
  518. RT9466_MASK_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 rt9466_info *info = (struct rt9466_info *)mchr_info;
  527. u8 data = 0;
  528. ret = rt9466_i2c_read_byte(info, RT9466_REG_CHG_CTRL7, &data);
  529. if (ret != I2C_OK)
  530. return ret;
  531. reg_ichg = (data & RT9466_MASK_ICHG) >> RT9466_SHIFT_ICHG;
  532. *ichg = rt9466_find_closest_real_value(RT9466_ICHG_MIN, RT9466_ICHG_MAX,
  533. RT9466_ICHG_STEP, reg_ichg);
  534. return ret;
  535. }
  536. static int rt_charger_get_aicr(struct mtk_charger_info *mchr_info, u32 *aicr)
  537. {
  538. int ret = 0;
  539. u8 reg_aicr = 0;
  540. struct rt9466_info *info = (struct rt9466_info *)mchr_info;
  541. u8 data = 0;
  542. ret = rt9466_i2c_read_byte(info, RT9466_REG_CHG_CTRL3, &data);
  543. if (ret != I2C_OK)
  544. return ret;
  545. reg_aicr = (data & RT9466_MASK_AICR) >> RT9466_SHIFT_AICR;
  546. *aicr = rt9466_find_closest_real_value(RT9466_AICR_MIN, RT9466_AICR_MAX,
  547. RT9466_AICR_STEP, reg_aicr);
  548. return ret;
  549. }
  550. static int rt_charger_reset_pumpx(struct mtk_charger_info *mchr_info,
  551. bool reset)
  552. {
  553. int ret = 0;
  554. u32 aicr = 0;
  555. aicr = (reset ? 100 : 3250);
  556. ret = rt_charger_set_aicr(mchr_info, aicr);
  557. return ret;
  558. }
  559. /*
  560. * Workaround to disable IRQ and WDT
  561. * In case that calling this function before charger is init,
  562. * use global info
  563. */
  564. static int rt_charger_sw_reset(struct mtk_charger_info *mchr_info)
  565. {
  566. int ret = 0, i = 0;
  567. u8 data = 0;
  568. struct rt9466_info *info = (struct rt9466_info *)mchr_info;
  569. dprintf(CRITICAL, "%s: starts\n", __func__);
  570. /* Mask all IRQs */
  571. for (i = RT9466_REG_CHG_STATC_CTRL; i <= RT9466_REG_CHG_IRQ3_CTRL; i++) {
  572. ret = rt9466_i2c_write_byte(info, i, 0xFF);
  573. if (ret != I2C_OK) {
  574. dprintf(CRITICAL, "%s: mask irq%d failed\n", __func__, i);
  575. }
  576. }
  577. /* Read all IRQs */
  578. for (i = RT9466_REG_CHG_STATC; i <= RT9466_REG_CHG_IRQ3; i++) {
  579. ret = rt9466_i2c_read_byte(info, i, &data);
  580. if (ret != I2C_OK) {
  581. dprintf(CRITICAL, "%s: read irq%d failed\n", __func__, i);
  582. }
  583. }
  584. /* Disable WDT */
  585. ret = rt9466_enable_watchdog_timer(info, false);
  586. if (ret != I2C_OK)
  587. dprintf(CRITICAL, "%s: disable wdt failed\n", __func__);
  588. return ret;
  589. }
  590. static struct mtk_charger_ops rt9466_mchr_ops = {
  591. .dump_register = rt_charger_dump_register,
  592. .enable_charging = rt_charger_enable_charging,
  593. .get_ichg = rt_charger_get_ichg,
  594. .set_ichg = rt_charger_set_ichg,
  595. .set_aicr = rt_charger_set_aicr,
  596. .set_mivr = rt_charger_set_mivr,
  597. .enable_power_path = rt_charger_enable_power_path,
  598. .get_aicr = rt_charger_get_aicr,
  599. .reset_pumpx = rt_charger_reset_pumpx,
  600. .sw_reset = rt_charger_sw_reset,
  601. };
  602. /* Info of primary charger */
  603. static struct rt9466_info g_rt9466_info = {
  604. .mchr_info = {
  605. .name = "primary_charger",
  606. .alias_name = "rt9466",
  607. .device_id = -1,
  608. .mchr_ops = &rt9466_mchr_ops,
  609. },
  610. .i2c = {
  611. .id = I2C1,
  612. .addr = RT9466_SLAVE_ADDR,
  613. .mode = ST_MODE,
  614. .speed = 100,
  615. },
  616. .i2c_log_level = INFO,
  617. };
  618. int rt9466_probe(void)
  619. {
  620. int ret = 0;
  621. /* Check primary charger */
  622. if (rt9466_is_hw_exist(&g_rt9466_info)) {
  623. ret = rt9466_reset_chip(&g_rt9466_info);
  624. ret = rt9466_init_setting(&g_rt9466_info);
  625. mtk_charger_set_info(&(g_rt9466_info.mchr_info));
  626. dprintf(CRITICAL, "%s: %s\n", __func__, RT9466_LK_DRV_VERSION);
  627. }
  628. return ret;
  629. }
  630. /*
  631. * Revision Note
  632. * 1.0.3
  633. * (1) Modify rt9466_is_hw_exist, check vendor id and revision id separately
  634. * (2) Set MIVR to highest value as disabling power path
  635. * (3) Update irq mask data
  636. *
  637. * 1.0.2
  638. * (1) Support E4 chip
  639. * (2) Release sw reset interface
  640. */