rt9466.c 20 KB

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