mt6362_pmu_charger.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  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,
  9. * shall be strictly prohibited.
  10. */
  11. /* MediaTek Inc. (C) 2015. All rights reserved.
  12. *
  13. * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
  14. * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
  15. * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON
  16. * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
  17. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
  19. * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
  20. * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
  21. * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
  22. * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY
  23. * ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY
  24. * THIRD PARTY ALL PROPER LICENSES CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK SHALL
  25. * ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO RECEIVER'S
  26. * SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM.
  27. * RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND CUMULATIVE
  28. * LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
  29. * AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
  30. * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY RECEIVER TO
  31. * MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
  32. */
  33. #include <platform/mt_typedefs.h>
  34. #include <platform/errno.h>
  35. #include <platform/spmi.h>
  36. #include <platform/spmi_sw.h>
  37. #include <printf.h>
  38. #include <debug.h>
  39. #include <platform/timer.h>
  40. #include "mt6362_pmu_charger.h"
  41. #include "mtk_charger_intf.h"
  42. #define MT6362_PMU_CHARGER_LK_DRV_VERSION "1.0.1_MTK"
  43. #define MT6362_LOG_LVL (INFO)
  44. #define U32_MAX 0xFFFFFFFF
  45. /* Register Table */
  46. #define MT6362_REG_DEV_INFO (0x00)
  47. #define MT6362_REG_TM_PASCODE1 (0x07)
  48. #define MT6362_REG_CHG_TOP1 (0x20)
  49. #define MT6362_REG_CHG_TOP2 (0x21)
  50. #define MT6362_REG_CHG_AICR (0x22)
  51. #define MT6362_REG_CHG_MIVR (0x23)
  52. #define MT6362_REG_CHG_PREC (0x24)
  53. #define MT6362_REG_CHG_VCHG (0x25)
  54. #define MT6362_REG_CHG_ICHG (0x26)
  55. #define MT6362_REG_CHG_EOC (0x28)
  56. #define MT6362_REG_CHG_WDT (0x2A)
  57. #define MT6362_REG_CHG_STAT (0x34)
  58. #define MT6362_REG_CHG_DUMMY0 (0x35)
  59. #define MT6362_REG_CHG_HD_BUCK5 (0x40)
  60. #define MT6362_REG_ADCCFG1 (0xA4)
  61. #define MT6362_REG_ADCCFG3 (0xA6)
  62. #define MT6362_REG_ADCEN1 (0xA7)
  63. #define MT6362_CHRPT_BASEADDR (0xAA)
  64. #define MT6362_REG_ADC_IRQ (0xD5)
  65. /* Mask & Shift */
  66. /* 0x20 */
  67. #define MT6362_MASK_CHG_BUCK_EN BIT(1)
  68. #define MT6362_MASK_CHG_EN BIT(0)
  69. /* 0x21 */
  70. #define MT6362_MASK_SEL_CLK_FREQ (0xC0)
  71. #define MT6362_SHFT_SEL_CLK_FREQ (6)
  72. /* 0x22 */
  73. #define MT6362_MASK_ILIM_EN BIT(7)
  74. #define MT6362_MASK_AICR (0x7F)
  75. #define MT6362_SHFT_AICR (0)
  76. /* 0x23 */
  77. #define MT6362_MASK_MIVR (0x7F)
  78. #define MT6362_SHFT_MIVR (0)
  79. /* 0x24 */
  80. #define MT6362_MASK_IPREC (0x1F)
  81. #define MT6362_SHFT_IPREC (0)
  82. /* 0x25 */
  83. #define MT6362_MASK_CV (0x7F)
  84. #define MT6362_SHFT_CV (0)
  85. /* 0x26 */
  86. #define MT6362_MASK_CC (0x3F)
  87. #define MT6362_SHFT_CC (0)
  88. /* 0x28 */
  89. #define MT6362_MASK_IEOC (0xF0)
  90. #define MT6362_SHFT_IEOC (4)
  91. /* 0x2A */
  92. #define MT6362_MASK_WDT_EN BIT(3)
  93. /* 0x34 */
  94. #define MT6362_MASK_IC_STAT (0x0F)
  95. #define MT6362_SHFT_IC_STAT (0)
  96. /* 0x35 */
  97. #define MT6362_MASK_COMP_CLAMP (0x03)
  98. #define MT6362_SHFT_COMP_CLAMP (0)
  99. /* 0x40 */
  100. #define MT6362_MASK_BUCK_RAMPOFT (0xC0)
  101. #define MT6362_SHFT_BUCK_RAMPOFT (6)
  102. /* 0xA6 */
  103. #define MT6362_SHFT_ADC_ONESHOT_SEL (4)
  104. #define MT6362_SHFT_ADC_DONE_IRQ_SEL (0)
  105. /* 0xD5 */
  106. #define MT6362_MASK_ADC_DONEI BIT(4)
  107. /* Engineer Spec */
  108. /* uA */
  109. #define MT6362_AICR_MIN 50
  110. #define MT6362_AICR_MAX 3225
  111. #define MT6362_AICR_STEP 25
  112. /* uV */
  113. #define MT6362_MIVR_MIN 3900
  114. #define MT6362_MIVR_MAX 13400
  115. #define MT6362_MIVR_STEP 100
  116. /* uV */
  117. #define MT6362_VPREC_MIN 2600
  118. #define MT6362_VPREC_MAX 3300
  119. #define MT6362_VPREC_STEP 100
  120. /* uA */
  121. #define MT6362_IPREC_MIN 50
  122. #define MT6362_IPREC_MAX 1600
  123. #define MT6362_IPREC_STEP 50
  124. /* uV */
  125. #define MT6362_CV_MIN 3900
  126. #define MT6362_CV_MAX 4710
  127. #define MT6362_CV_STEP 10
  128. /* uA */
  129. #define MT6362_CC_MIN 0
  130. #define MT6362_CC_MAX 3150
  131. #define MT6362_CC_STEP 50
  132. /* uA */
  133. #define MT6362_IEOC_MIN 50
  134. #define MT6362_IEOC_MAX 800
  135. #define MT6362_IEOC_STEP 50
  136. /* for reset PD adapter */
  137. #define TCPC_V10_REG_ROLE_CTRL (0x41A)
  138. #define MT6362_REG_SYSCTRL1 (0x48F)
  139. #define MT6362_MASK_SHIPPING_OFF BIT(5)
  140. /* ================= */
  141. /* Internal variable */
  142. /* ================= */
  143. struct mt6362_chg_data {
  144. struct mtk_charger_info mchr_info;
  145. struct spmi_device *sdev;
  146. u32 last_access_addr;
  147. u32 last_access_tick;
  148. u8 vid;
  149. u32 iprec;
  150. u32 ichg;
  151. u32 ichg_dis_chg;
  152. u32 aicr;
  153. u32 mivr;
  154. u32 cv;
  155. };
  156. enum mt6362_clk_freq {
  157. MT6362_CLK_FREQ_1500K,
  158. MT6362_CLK_FREQ_1000K,
  159. MT6362_CLK_FREQ_750K,
  160. MT6362_CLK_FREQ_MAX,
  161. };
  162. enum mt6362_ic_stat {
  163. MT6362_STAT_HZ = 0x0,
  164. MT6362_STAT_READY = 0x1,
  165. MT6362_STAT_TRI_CHG = 0x2,
  166. MT6362_STAT_PRE_CHG = 0x3,
  167. MT6362_STAT_FAST_CHG = 0x4,
  168. MT6362_STAT_EOC_CHG = 0x5,
  169. MT6362_STAT_BACKGND_CHG = 0x6,
  170. MT6362_STAT_CHG_DONE = 0x7,
  171. MT6362_STAT_CHG_FAULT = 0x8,
  172. MT6362_STAT_OTG = 0xf,
  173. };
  174. static const char *mt6362_ic_stat_list[] = {
  175. "hz/sleep", "ready", "trickle_chg", "pre_chg", "fast_chg",
  176. "ieoc_chg", "backgnd_chg", "chg_done", "chg_fault", "otg",
  177. };
  178. /* ========================= */
  179. /* SPMI operations */
  180. /* ========================= */
  181. static int mt6362_write_byte(struct mt6362_chg_data *cdata, u32 addr, u8 data)
  182. {
  183. struct spmi_device *sdev = cdata->sdev;
  184. int ret;
  185. ret = spmi_ext_register_writel(sdev, addr, &data, 1);
  186. if (ret < 0)
  187. dprintf(CRITICAL, "%s: fail, addr:0x%02X, data:0x%02X\n",
  188. __func__, addr, data);
  189. else {
  190. dprintf(MT6362_LOG_LVL, "%s: addr:0x%02X, data:0x%02X\n",
  191. __func__, addr, data);
  192. cdata->last_access_addr = addr;
  193. cdata->last_access_tick = gpt4_get_current_tick();
  194. }
  195. return ret;
  196. }
  197. static int mt6362_read_byte(struct mt6362_chg_data *cdata, u32 addr, u8 *data)
  198. {
  199. struct spmi_device *sdev = cdata->sdev;
  200. u32 avail_access_tick, current_tick, rest_tick;
  201. int ret;
  202. if (addr == cdata->last_access_addr) {
  203. avail_access_tick =
  204. cdata->last_access_tick + gpt4_time2tick_us(3);
  205. current_tick = gpt4_get_current_tick();
  206. if (avail_access_tick > current_tick) {
  207. rest_tick = avail_access_tick - current_tick;
  208. udelay(gpt4_tick2time_us(rest_tick));
  209. }
  210. }
  211. ret = spmi_ext_register_readl(sdev, addr, data, 1);
  212. if (ret < 0)
  213. dprintf(CRITICAL, "%s: fail, addr:0x%02X\n", __func__, addr);
  214. else
  215. dprintf(MT6362_LOG_LVL, "%s: addr:0x%02X, data:0x%02X\n",
  216. __func__, addr, *data);
  217. return ret;
  218. }
  219. static inline int mt6362_block_read(struct mt6362_chg_data *cdata,
  220. u32 addr, u8 *data, int len)
  221. {
  222. int i, ret;
  223. for (i = 0; i < len; i++) {
  224. ret = mt6362_read_byte(cdata, addr + i, data + i);
  225. if (ret < 0)
  226. return ret;
  227. }
  228. return ret;
  229. }
  230. static inline int mt6362_block_write(struct mt6362_chg_data *cdata,
  231. u32 addr, u8 *data, int len)
  232. {
  233. int i, ret;
  234. for (i = 0; i < len; i++) {
  235. ret = mt6362_write_byte(cdata, addr, *(data + i));
  236. if (ret < 0)
  237. return ret;
  238. }
  239. return ret;
  240. }
  241. static int mt6362_update_bits(struct mt6362_chg_data *cdata,
  242. u32 addr, u8 mask, u8 data)
  243. {
  244. int ret;
  245. u8 org = 0;
  246. ret = mt6362_read_byte(cdata, addr, &org);
  247. if (ret < 0)
  248. return ret;
  249. org &= ~mask;
  250. org |= (data & mask);
  251. return mt6362_write_byte(cdata, addr, org);
  252. }
  253. static inline int mt6362_set_bits(struct mt6362_chg_data *cdata,
  254. u32 addr, u8 mask)
  255. {
  256. return mt6362_update_bits(cdata, addr, mask, mask);
  257. }
  258. static inline int mt6362_clr_bits(struct mt6362_chg_data *cdata, u32 addr, u8 mask)
  259. {
  260. return mt6362_update_bits(cdata, addr, mask, 0);
  261. }
  262. static inline u32 mt6362_map_reg_sel(u32 data, u32 min, u32 max, u32 step)
  263. {
  264. u32 target = 0, max_sel;
  265. if (data >= min) {
  266. target = (data - min) / step;
  267. max_sel = (max - min) / step;
  268. if (target > max_sel)
  269. target = max_sel;
  270. }
  271. return target;
  272. }
  273. static inline u32 mt6362_map_real_val(u32 sel, u32 min, u32 max, u32 step)
  274. {
  275. u32 target = 0;
  276. target = min + (sel * step);
  277. if (target > max)
  278. target = max;
  279. return target;
  280. }
  281. /* ================== */
  282. /* internal functions */
  283. /* ================== */
  284. #define MT6362_VENDOR_ID (0x70)
  285. static bool mt6362_is_hw_exist(struct mt6362_chg_data *cdata)
  286. {
  287. int ret;
  288. u8 regval = 0, vid, rev_id;
  289. ret = mt6362_read_byte(cdata, MT6362_REG_DEV_INFO, &regval);
  290. if (ret < 0)
  291. return false;
  292. vid = regval & 0xF0;
  293. rev_id = regval & 0x0F;
  294. if (vid != MT6362_VENDOR_ID) {
  295. dprintf(CRITICAL, "%s: vid is not match(%d)\n", __func__, vid);
  296. return false;
  297. }
  298. dprintf(CRITICAL, "%s: rev_id = %d\n", __func__, rev_id);
  299. cdata->mchr_info.device_id = rev_id;
  300. cdata->vid = vid;
  301. return true;
  302. }
  303. static int mt6362_enable_hidden_mode(struct mt6362_chg_data *cdata, bool en)
  304. {
  305. return mt6362_write_byte(cdata, MT6362_REG_TM_PASCODE1,
  306. en ? 0x69 : 0);
  307. }
  308. static int mt6362_enable_ilim(struct mt6362_chg_data *cdata, bool en)
  309. {
  310. dprintf(MT6362_LOG_LVL, "%s: en = %d\n", __func__, en);
  311. return (en ? mt6362_set_bits : mt6362_clr_bits)
  312. (cdata, MT6362_REG_CHG_AICR, MT6362_MASK_ILIM_EN);
  313. }
  314. static int mt6362_set_cv(struct mt6362_chg_data *cdata, u32 uV)
  315. {
  316. u8 sel;
  317. dprintf(MT6362_LOG_LVL, "%s: cv = %d\n", __func__, uV);
  318. sel = mt6362_map_reg_sel(uV, MT6362_CV_MIN, MT6362_CV_MAX,
  319. MT6362_CV_STEP);
  320. return mt6362_update_bits(cdata,
  321. MT6362_REG_CHG_VCHG,
  322. MT6362_MASK_CV,
  323. sel << MT6362_SHFT_CV);
  324. }
  325. static int mt6362_get_charging_status(struct mt6362_chg_data *cdata,
  326. enum mt6362_ic_stat *ic_stat)
  327. {
  328. int ret;
  329. u8 regval = 0;
  330. ret = mt6362_read_byte(cdata, MT6362_REG_CHG_STAT, &regval);
  331. if (ret < 0)
  332. return ret;
  333. *ic_stat = (regval & MT6362_MASK_IC_STAT) >> MT6362_SHFT_IC_STAT;
  334. return ret;
  335. }
  336. static int mt6362_get_mivr(struct mt6362_chg_data *cdata, u32 *mivr)
  337. {
  338. int ret;
  339. u8 regval = 0;
  340. ret = mt6362_read_byte(cdata, MT6362_REG_CHG_MIVR, &regval);
  341. if (ret < 0)
  342. return ret;
  343. regval = (regval & MT6362_MASK_MIVR) >> MT6362_SHFT_MIVR;
  344. *mivr = mt6362_map_real_val(regval, MT6362_MIVR_MIN, MT6362_MIVR_MAX,
  345. MT6362_MIVR_STEP);
  346. return 0;
  347. }
  348. static int mt6362_get_ieoc(struct mt6362_chg_data *cdata, u32 *ieoc)
  349. {
  350. int ret;
  351. u8 regval = 0;
  352. ret = mt6362_read_byte(cdata, MT6362_REG_CHG_EOC, &regval);
  353. if (ret < 0)
  354. return ret;
  355. regval = (regval & MT6362_MASK_IEOC) >> MT6362_SHFT_IEOC;
  356. *ieoc = mt6362_map_real_val(regval, MT6362_IEOC_MIN, MT6362_IEOC_MAX,
  357. MT6362_IEOC_STEP);
  358. return 0;
  359. }
  360. static int mt6362_is_charging_enable(struct mt6362_chg_data *cdata, bool *en)
  361. {
  362. int ret;
  363. u8 regval = 0;
  364. ret = mt6362_read_byte(cdata, MT6362_REG_CHG_TOP1, &regval);
  365. if (ret < 0)
  366. return ret;
  367. *en = (regval & MT6362_MASK_CHG_EN) ? true : false;
  368. return 0;
  369. }
  370. /* =========================================================== */
  371. /* The following is implementation for interface of mt_charger */
  372. /* =========================================================== */
  373. static int mt_charger_set_ichg(struct mtk_charger_info *mchr_info, u32 ichg)
  374. {
  375. struct mt6362_chg_data *cdata = (struct mt6362_chg_data *)mchr_info;
  376. int ret;
  377. u8 sel;
  378. dprintf(MT6362_LOG_LVL, "%s: ichg = %d\n", __func__, ichg);
  379. /* mapping datasheet define */
  380. if (ichg < 300)
  381. return -EINVAL;
  382. sel = mt6362_map_reg_sel(ichg, MT6362_CC_MIN, MT6362_CC_MAX,
  383. MT6362_CC_STEP);
  384. ret = mt6362_update_bits(cdata,
  385. MT6362_REG_CHG_ICHG,
  386. MT6362_MASK_CC,
  387. sel << MT6362_SHFT_CC);
  388. if (ret < 0)
  389. dprintf(CRITICAL, "%s: fail\n", __func__);
  390. else
  391. cdata->ichg = ichg;
  392. return ret;
  393. }
  394. static int mt_charger_enable_charging(struct mtk_charger_info *mchr_info,
  395. bool en)
  396. {
  397. struct mt6362_chg_data *cdata = (struct mt6362_chg_data *)mchr_info;
  398. u32 ichg_ramp_t = 0;
  399. int ret;
  400. /* Workaround for vsys overshoot */
  401. if (cdata->ichg < 500) {
  402. dprintf(CRITICAL,
  403. "%s: ichg < 500mA, bypass vsys wkard\n", __func__);
  404. goto out;
  405. }
  406. if (!en) {
  407. cdata->ichg_dis_chg = cdata->ichg;
  408. ichg_ramp_t = (cdata->ichg - MT6362_CC_MIN) / MT6362_CC_STEP * 2;
  409. /* Set ichg to 500mA */
  410. ret = mt6362_update_bits(cdata,
  411. MT6362_REG_CHG_ICHG,
  412. MT6362_MASK_CC,
  413. 0x0A << MT6362_SHFT_CC);
  414. if (ret < 0) {
  415. dprintf(CRITICAL,
  416. "%s: set ichg 500mA fail\n", __func__);
  417. goto vsys_wkard_fail;
  418. }
  419. mdelay(ichg_ramp_t);
  420. } else {
  421. if (cdata->ichg == cdata->ichg_dis_chg) {
  422. ret = mt_charger_set_ichg(&cdata->mchr_info, cdata->ichg);
  423. if (ret < 0)
  424. dprintf(CRITICAL,
  425. "%s: recover ichg fail\n", __func__);
  426. }
  427. }
  428. out:
  429. ret = (en ? mt6362_set_bits : mt6362_clr_bits)
  430. (cdata, MT6362_REG_CHG_TOP1, MT6362_MASK_CHG_EN);
  431. if (ret < 0)
  432. dprintf(CRITICAL, "%s: fail\n", __func__);
  433. vsys_wkard_fail:
  434. return ret;
  435. }
  436. static int mt_charger_get_ichg(struct mtk_charger_info *mchr_info, u32 *ichg)
  437. {
  438. struct mt6362_chg_data *cdata = (struct mt6362_chg_data *)mchr_info;
  439. int ret;
  440. u8 regval = 0;
  441. ret = mt6362_read_byte(cdata, MT6362_REG_CHG_ICHG, &regval);
  442. if (ret < 0)
  443. return ret;
  444. regval = (regval & MT6362_MASK_CC) >> MT6362_SHFT_CC;
  445. *ichg = mt6362_map_real_val(regval, MT6362_CC_MIN, MT6362_CC_MAX,
  446. MT6362_CC_STEP);
  447. return 0;
  448. }
  449. static int mt_charger_set_iprec(struct mtk_charger_info *mchr_info, u32 iprec)
  450. {
  451. struct mt6362_chg_data *cdata = (struct mt6362_chg_data *)mchr_info;
  452. u8 sel;
  453. dprintf(MT6362_LOG_LVL, "%s: iprec = %d\n", __func__, iprec);
  454. sel = mt6362_map_reg_sel(iprec,
  455. MT6362_IPREC_MIN,
  456. MT6362_IPREC_MAX,
  457. MT6362_IPREC_STEP);
  458. return mt6362_update_bits(cdata,
  459. MT6362_REG_CHG_PREC,
  460. MT6362_MASK_IPREC,
  461. sel << MT6362_SHFT_IPREC);
  462. }
  463. static int mt_charger_get_aicr(struct mtk_charger_info *mchr_info, u32 *aicr)
  464. {
  465. struct mt6362_chg_data *cdata = (struct mt6362_chg_data *)mchr_info;
  466. int ret;
  467. u8 regval = 0;
  468. ret = mt6362_read_byte(cdata, MT6362_REG_CHG_AICR, &regval);
  469. if (ret < 0)
  470. return ret;
  471. regval = (regval & MT6362_MASK_AICR) >> MT6362_SHFT_AICR;
  472. *aicr = mt6362_map_real_val(regval,
  473. MT6362_AICR_MIN,
  474. MT6362_AICR_MAX,
  475. MT6362_AICR_STEP);
  476. return 0;
  477. }
  478. static int mt_charger_set_aicr(struct mtk_charger_info *mchr_info, u32 aicr)
  479. {
  480. struct mt6362_chg_data *cdata = (struct mt6362_chg_data *)mchr_info;
  481. u8 sel;
  482. dprintf(MT6362_LOG_LVL, "%s: aicr = %d\n", __func__, aicr);
  483. sel = mt6362_map_reg_sel(aicr, MT6362_AICR_MIN, MT6362_AICR_MAX,
  484. MT6362_AICR_STEP);
  485. return mt6362_update_bits(cdata,
  486. MT6362_REG_CHG_AICR,
  487. MT6362_MASK_AICR,
  488. sel << MT6362_SHFT_AICR);
  489. }
  490. #define MT6362_IAICR_525mA (0x13)
  491. static int mt6362_adc_get_process_val(struct mt6362_chg_data *cdata,
  492. enum mt6362_adc_channel chan, u32 *val)
  493. {
  494. int ret;
  495. u8 regval = 0;
  496. switch (chan) {
  497. case MT6362_ADC_VBUSDIV5:
  498. *val *= 6250;
  499. break;
  500. case MT6362_ADC_VSYS:
  501. case MT6362_ADC_VBAT:
  502. *val *= 1250;
  503. break;
  504. case MT6362_ADC_IBUS:
  505. ret = mt6362_read_byte(cdata, MT6362_REG_CHG_AICR, &regval);
  506. if (ret < 0)
  507. return ret;
  508. regval = (regval & MT6362_MASK_AICR) >> MT6362_SHFT_AICR;
  509. if (regval < MT6362_IAICR_525mA)
  510. *val *= 1900;
  511. else
  512. *val *= 2500;
  513. break;
  514. case MT6362_ADC_IBAT:
  515. *val *= 2500;
  516. break;
  517. case MT6362_ADC_CHG_VDDP:
  518. *val *= 1250;
  519. break;
  520. case MT6362_ADC_TEMP_JC:
  521. *val -= 64;
  522. break;
  523. case MT6362_ADC_VREF_TS:
  524. case MT6362_ADC_TS:
  525. *val *= 1250;
  526. break;
  527. default:
  528. break;
  529. }
  530. return 0;
  531. }
  532. static int mt_charger_get_adc(struct mtk_charger_info *mchr_info,
  533. int chan, u32 *val)
  534. {
  535. struct mt6362_chg_data *cdata = (struct mt6362_chg_data *)mchr_info;
  536. int i, ret, max_retry_cnt = 2;
  537. u8 oneshot_ch, regval = 0, rpt[2] = {0};
  538. u16 rpt_addr;
  539. dprintf(MT6362_LOG_LVL, "%s\n", __func__);
  540. /* config onshot channel and irq reported channel */
  541. oneshot_ch = (chan << MT6362_SHFT_ADC_ONESHOT_SEL);
  542. oneshot_ch |= (chan << MT6362_SHFT_ADC_DONE_IRQ_SEL);
  543. ret = mt6362_write_byte(cdata, MT6362_REG_ADCCFG3, oneshot_ch);
  544. if (ret < 0)
  545. return ret;
  546. for (i = 0; i < max_retry_cnt; i++) {
  547. mdelay(1);
  548. /* read adc conversion done event */
  549. ret = mt6362_read_byte(cdata, MT6362_REG_ADC_IRQ, &regval);
  550. if (ret < 0)
  551. goto err_adc_conv;
  552. if (!(regval & MT6362_MASK_ADC_DONEI))
  553. continue;
  554. /* read adc report */
  555. rpt_addr = MT6362_CHRPT_BASEADDR + (chan - 1) * 2;
  556. ret = mt6362_write_byte(cdata, rpt_addr, 0);
  557. if (ret < 0)
  558. goto err_adc_conv;
  559. ret = mt6362_block_read(cdata, rpt_addr, rpt, 2);
  560. if (ret < 0)
  561. goto err_adc_conv;
  562. *val = ((rpt[0] << 8) | rpt[1]);
  563. mt6362_adc_get_process_val(cdata, chan, val);
  564. break;
  565. }
  566. if (i == max_retry_cnt) {
  567. dprintf(CRITICAL, "%s: reach adc retry cnt\n", __func__);
  568. ret = -EBUSY;
  569. }
  570. dprintf(MT6362_LOG_LVL, "%s: val = %d\n", __func__, *val);
  571. err_adc_conv:
  572. mt6362_set_bits(cdata, MT6362_REG_ADC_IRQ, MT6362_MASK_ADC_DONEI);
  573. return ret;
  574. }
  575. static int mt_charger_get_vbus(struct mtk_charger_info *mchr_info, u32 *vbus)
  576. {
  577. return mt_charger_get_adc(mchr_info, MT6362_ADC_VBUSDIV5, vbus);
  578. }
  579. static int mt_charger_set_mivr(struct mtk_charger_info *mchr_info, u32 mivr)
  580. {
  581. struct mt6362_chg_data *cdata = (struct mt6362_chg_data *)mchr_info;
  582. u8 sel;
  583. dprintf(MT6362_LOG_LVL, "%s: mivr = %d\n", __func__, mivr);
  584. sel = mt6362_map_reg_sel(mivr, MT6362_MIVR_MIN, MT6362_MIVR_MAX,
  585. MT6362_MIVR_STEP);
  586. return mt6362_update_bits(cdata,
  587. MT6362_REG_CHG_MIVR,
  588. MT6362_MASK_MIVR,
  589. sel << MT6362_SHFT_MIVR);
  590. }
  591. static int mt_charger_enable_power_path(struct mtk_charger_info *mchr_info,
  592. bool en)
  593. {
  594. struct mt6362_chg_data *cdata = (struct mt6362_chg_data *)mchr_info;
  595. dprintf(MT6362_LOG_LVL, "%s: en = %d\n", __func__, en);
  596. return (en ? mt6362_set_bits : mt6362_clr_bits)
  597. (cdata, MT6362_REG_CHG_TOP1, MT6362_MASK_CHG_BUCK_EN);
  598. }
  599. static int mt_charger_reset_pumpx(struct mtk_charger_info *mchr_info, bool en)
  600. {
  601. struct mt6362_chg_data *cdata = (struct mt6362_chg_data *)mchr_info;
  602. if (en)
  603. mt_charger_get_aicr(mchr_info, &cdata->aicr);
  604. return mt_charger_set_aicr(mchr_info, en ? 100 : cdata->aicr);
  605. }
  606. static int mt_charger_enable_wdt(struct mtk_charger_info *mchr_info, bool en)
  607. {
  608. struct mt6362_chg_data *cdata = (struct mt6362_chg_data *)mchr_info;
  609. dprintf(MT6362_LOG_LVL, "%s: en = %d\n", __func__, en);
  610. return (en ? mt6362_set_bits : mt6362_clr_bits)
  611. (cdata, MT6362_REG_CHG_WDT, MT6362_MASK_WDT_EN);
  612. }
  613. static int mt_charger_dump_register(struct mtk_charger_info *mchr_info)
  614. {
  615. struct mt6362_chg_data *cdata = (struct mt6362_chg_data *)mchr_info;
  616. enum mt6362_ic_stat ic_stat = MT6362_STAT_HZ;
  617. u32 ichg = 0, aicr = 0, mivr = 0, ieoc = 0;
  618. bool chg_en = 0;
  619. int ret;
  620. ret = mt_charger_get_ichg(mchr_info, &ichg);
  621. ret |= mt_charger_get_aicr(mchr_info, &aicr);
  622. ret |= mt6362_get_mivr(cdata, &mivr);
  623. ret |= mt6362_get_ieoc(cdata, &ieoc);
  624. ret |= mt6362_is_charging_enable(cdata, &chg_en);
  625. ret |= mt6362_get_charging_status(cdata, &ic_stat);
  626. if (ret < 0) {
  627. dprintf(CRITICAL, "%s: fail\n", __func__);
  628. return ret;
  629. }
  630. dprintf(CRITICAL,
  631. "%s: ICHG = %dmA, AICR = %dmA, MIVR = %dmV, IEOC = %dmA\n",
  632. __func__, ichg, aicr, mivr, ieoc);
  633. dprintf(CRITICAL, "%s: CHG_EN = %d, CHG_STATUS = %s\n",
  634. __func__, chg_en, mt6362_ic_stat_list[ic_stat]);
  635. return ret;
  636. }
  637. static struct mtk_charger_ops mt6362_mchr_ops = {
  638. .dump_register = mt_charger_dump_register,
  639. .enable_charging = mt_charger_enable_charging,
  640. .get_ichg = mt_charger_get_ichg,
  641. .set_ichg = mt_charger_set_ichg,
  642. .get_aicr = mt_charger_get_aicr,
  643. .set_aicr = mt_charger_set_aicr,
  644. .get_adc = mt_charger_get_adc,
  645. .get_vbus = mt_charger_get_vbus,
  646. .set_mivr = mt_charger_set_mivr,
  647. .enable_power_path = mt_charger_enable_power_path,
  648. .reset_pumpx = mt_charger_reset_pumpx,
  649. .enable_wdt = mt_charger_enable_wdt,
  650. };
  651. /* Info of primary charger */
  652. static struct mt6362_chg_data g_cdata = {
  653. .mchr_info = {
  654. .name = "primary_charger",
  655. .device_id = -1,
  656. .mchr_ops = &mt6362_mchr_ops,
  657. },
  658. .last_access_addr = U32_MAX,
  659. .last_access_tick = 0,
  660. .vid = 0x00,
  661. .iprec = 150,
  662. .ichg = 500,
  663. .ichg_dis_chg = 500,
  664. .aicr = 500,
  665. .mivr = 4500,
  666. .cv = 4350,
  667. };
  668. static int mt6362_chg_init_setting(struct mt6362_chg_data *cdata)
  669. {
  670. int ret = 0;
  671. dprintf(CRITICAL, "%s\n", __func__);
  672. ret = mt_charger_set_aicr(&cdata->mchr_info, cdata->aicr);
  673. if (ret < 0) {
  674. dprintf(CRITICAL, "%s: set aicr failed\n", __func__);
  675. return ret;
  676. }
  677. mdelay(5);
  678. /* Disable HW iinlimit, use SW */
  679. ret = mt6362_enable_ilim(cdata, false);
  680. if (ret < 0) {
  681. dprintf(CRITICAL, "%s: disable ilim failed\n", __func__);
  682. return ret;
  683. }
  684. ret = mt_charger_set_iprec(&cdata->mchr_info, cdata->iprec);
  685. if (ret < 0) {
  686. dprintf(CRITICAL, "%s: set iprec failed\n", __func__);
  687. return ret;
  688. }
  689. ret = mt_charger_set_ichg(&cdata->mchr_info, cdata->ichg);
  690. if (ret < 0) {
  691. dprintf(CRITICAL, "%s: set ichg failed\n", __func__);
  692. return ret;
  693. }
  694. ret = mt_charger_set_mivr(&cdata->mchr_info, cdata->mivr);
  695. if (ret < 0) {
  696. dprintf(CRITICAL, "%s: set mivr failed\n", __func__);
  697. return ret;
  698. }
  699. ret = mt6362_set_cv(cdata, cdata->cv);
  700. if (ret < 0) {
  701. dprintf(CRITICAL, "%s: set cv failed\n", __func__);
  702. return ret;
  703. }
  704. /* Set switch frequency to 1.5->1 MHz for buck efficiency */
  705. ret = mt6362_update_bits(cdata,
  706. MT6362_REG_CHG_TOP2,
  707. MT6362_MASK_SEL_CLK_FREQ,
  708. MT6362_CLK_FREQ_1000K <<
  709. MT6362_SHFT_SEL_CLK_FREQ);
  710. if (ret < 0)
  711. dprintf(CRITICAL, "%s: set sw freq 1M failed\n", __func__);
  712. ret = mt6362_enable_hidden_mode(cdata, true);
  713. if (ret < 0)
  714. return ret;
  715. /* Set comp diode to 1 for avoid inrush current */
  716. ret = mt6362_update_bits(cdata,
  717. MT6362_REG_CHG_DUMMY0,
  718. MT6362_MASK_COMP_CLAMP,
  719. 0 << MT6362_SHFT_COMP_CLAMP);
  720. if (ret < 0) {
  721. dprintf(CRITICAL, "%s: set comp diode 1 failed\n", __func__);
  722. goto out;
  723. }
  724. /* Set buck_ramp offset 300->330 mV */
  725. /* not to enter psk mode when charging in hv and low loading */
  726. ret = mt6362_update_bits(cdata,
  727. MT6362_REG_CHG_HD_BUCK5,
  728. MT6362_MASK_BUCK_RAMPOFT,
  729. 0x1 << MT6362_SHFT_BUCK_RAMPOFT);
  730. if (ret < 0) {
  731. dprintf(CRITICAL,
  732. "%s: fail to set buck ramp offset 330 mV\n", __func__);
  733. goto out;
  734. }
  735. out:
  736. mt6362_enable_hidden_mode(cdata, false);
  737. return ret;
  738. }
  739. int pd_reset_adapter(void)
  740. {
  741. int ret;
  742. dprintf(CRITICAL, "%s: ++\n", __func__);
  743. /* SHIPPING off */
  744. ret = mt6362_set_bits(&g_cdata, MT6362_REG_SYSCTRL1,
  745. MT6362_MASK_SHIPPING_OFF);
  746. if (ret < 0) {
  747. dprintf(CRITICAL, "%s: shipping mode off fail\n", __func__);
  748. return ret;
  749. }
  750. /* Set CC (open, open) */
  751. ret = mt6362_set_bits(&g_cdata, TCPC_V10_REG_ROLE_CTRL, 0x0F);
  752. if (ret < 0) {
  753. dprintf(CRITICAL, "%s: set cc open fail\n", __func__);
  754. return ret;
  755. }
  756. mdelay(300);
  757. /* SHIPPING on */
  758. ret = mt6362_clr_bits(&g_cdata, MT6362_REG_SYSCTRL1,
  759. MT6362_MASK_SHIPPING_OFF);
  760. if (ret < 0)
  761. dprintf(CRITICAL, "%s: shipping mode on fail\n", __func__);
  762. dprintf(CRITICAL, "%s: --\n", __func__);
  763. return ret;
  764. }
  765. int mt6362_chg_probe(void)
  766. {
  767. int ret = 0;
  768. dprintf(CRITICAL, "%s: %s\n",
  769. __func__, MT6362_PMU_CHARGER_LK_DRV_VERSION);
  770. /* get spmi device */
  771. g_cdata.sdev = get_spmi_device(SPMI_MASTER_1, SPMI_SLAVE_9);
  772. if (!g_cdata.sdev) {
  773. dprintf(CRITICAL, "%s: get spmi device fail\n", __func__);
  774. return ret;
  775. }
  776. /* check hw exist */
  777. if (!mt6362_is_hw_exist(&g_cdata))
  778. return -ENODEV;
  779. ret = mt6362_chg_init_setting(&g_cdata);
  780. if (ret < 0) {
  781. dprintf(CRITICAL, "%s: init setting fail\n", __func__);
  782. return ret;
  783. }
  784. ret = mtk_charger_set_info(&g_cdata.mchr_info);
  785. if (ret < 0)
  786. dprintf(CRITICAL, "%s: add mtk chg info list fail\n", __func__);
  787. return ret;
  788. }
  789. /*
  790. * Revision Note
  791. * 1.0.1
  792. * (1) fix spmi w/r need 3us delay with same register
  793. * (2) fix get spmi device in spmi master channel 1
  794. * (3) set initial setting ichg 500mA for decrease vsys overshoot delay
  795. *
  796. * 1.0.0
  797. * (1) Initial release
  798. */