axp221.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /*
  2. * AXP221 and AXP223 driver
  3. *
  4. * IMPORTANT when making changes to this file check that the registers
  5. * used are the same for the axp221 and axp223.
  6. *
  7. * (C) Copyright 2014 Hans de Goede <hdegoede@redhat.com>
  8. * (C) Copyright 2013 Oliver Schinagl <oliver@schinagl.nl>
  9. *
  10. * SPDX-License-Identifier: GPL-2.0+
  11. */
  12. #include <common.h>
  13. #include <errno.h>
  14. #include <asm/arch/p2wi.h>
  15. #include <asm/arch/rsb.h>
  16. #include <axp221.h>
  17. /*
  18. * The axp221 uses the p2wi bus, the axp223 is identical (for all registers
  19. * used sofar) but uses the rsb bus. These functions abstract this.
  20. */
  21. static int pmic_bus_init(void)
  22. {
  23. #ifdef CONFIG_MACH_SUN6I
  24. p2wi_init();
  25. return p2wi_change_to_p2wi_mode(AXP221_CHIP_ADDR, AXP221_CTRL_ADDR,
  26. AXP221_INIT_DATA);
  27. #else
  28. int ret;
  29. rsb_init();
  30. ret = rsb_set_device_mode(AXP223_DEVICE_MODE_DATA);
  31. if (ret)
  32. return ret;
  33. return rsb_set_device_address(AXP223_DEVICE_ADDR, AXP223_RUNTIME_ADDR);
  34. #endif
  35. }
  36. static int pmic_bus_read(const u8 addr, u8 *data)
  37. {
  38. #ifdef CONFIG_MACH_SUN6I
  39. return p2wi_read(addr, data);
  40. #else
  41. return rsb_read(AXP223_RUNTIME_ADDR, addr, data);
  42. #endif
  43. }
  44. static int pmic_bus_write(const u8 addr, u8 data)
  45. {
  46. #ifdef CONFIG_MACH_SUN6I
  47. return p2wi_write(addr, data);
  48. #else
  49. return rsb_write(AXP223_RUNTIME_ADDR, addr, data);
  50. #endif
  51. }
  52. static u8 axp221_mvolt_to_cfg(int mvolt, int min, int max, int div)
  53. {
  54. if (mvolt < min)
  55. mvolt = min;
  56. else if (mvolt > max)
  57. mvolt = max;
  58. return (mvolt - min) / div;
  59. }
  60. static int axp221_setbits(u8 reg, u8 bits)
  61. {
  62. int ret;
  63. u8 val;
  64. ret = pmic_bus_read(reg, &val);
  65. if (ret)
  66. return ret;
  67. val |= bits;
  68. return pmic_bus_write(reg, val);
  69. }
  70. static int axp221_clrbits(u8 reg, u8 bits)
  71. {
  72. int ret;
  73. u8 val;
  74. ret = pmic_bus_read(reg, &val);
  75. if (ret)
  76. return ret;
  77. val &= ~bits;
  78. return pmic_bus_write(reg, val);
  79. }
  80. int axp221_set_dcdc1(unsigned int mvolt)
  81. {
  82. int ret;
  83. u8 cfg = axp221_mvolt_to_cfg(mvolt, 1600, 3400, 100);
  84. if (mvolt == 0)
  85. return axp221_clrbits(AXP221_OUTPUT_CTRL1,
  86. AXP221_OUTPUT_CTRL1_DCDC1_EN);
  87. ret = pmic_bus_write(AXP221_DCDC1_CTRL, cfg);
  88. if (ret)
  89. return ret;
  90. ret = axp221_setbits(AXP221_OUTPUT_CTRL2,
  91. AXP221_OUTPUT_CTRL2_DCDC1SW_EN);
  92. if (ret)
  93. return ret;
  94. return axp221_setbits(AXP221_OUTPUT_CTRL1,
  95. AXP221_OUTPUT_CTRL1_DCDC1_EN);
  96. }
  97. int axp221_set_dcdc2(unsigned int mvolt)
  98. {
  99. int ret;
  100. u8 cfg = axp221_mvolt_to_cfg(mvolt, 600, 1540, 20);
  101. if (mvolt == 0)
  102. return axp221_clrbits(AXP221_OUTPUT_CTRL1,
  103. AXP221_OUTPUT_CTRL1_DCDC2_EN);
  104. ret = pmic_bus_write(AXP221_DCDC2_CTRL, cfg);
  105. if (ret)
  106. return ret;
  107. return axp221_setbits(AXP221_OUTPUT_CTRL1,
  108. AXP221_OUTPUT_CTRL1_DCDC2_EN);
  109. }
  110. int axp221_set_dcdc3(unsigned int mvolt)
  111. {
  112. int ret;
  113. u8 cfg = axp221_mvolt_to_cfg(mvolt, 600, 1860, 20);
  114. if (mvolt == 0)
  115. return axp221_clrbits(AXP221_OUTPUT_CTRL1,
  116. AXP221_OUTPUT_CTRL1_DCDC3_EN);
  117. ret = pmic_bus_write(AXP221_DCDC3_CTRL, cfg);
  118. if (ret)
  119. return ret;
  120. return axp221_setbits(AXP221_OUTPUT_CTRL1,
  121. AXP221_OUTPUT_CTRL1_DCDC3_EN);
  122. }
  123. int axp221_set_dcdc4(unsigned int mvolt)
  124. {
  125. int ret;
  126. u8 cfg = axp221_mvolt_to_cfg(mvolt, 600, 1540, 20);
  127. if (mvolt == 0)
  128. return axp221_clrbits(AXP221_OUTPUT_CTRL1,
  129. AXP221_OUTPUT_CTRL1_DCDC4_EN);
  130. ret = pmic_bus_write(AXP221_DCDC4_CTRL, cfg);
  131. if (ret)
  132. return ret;
  133. return axp221_setbits(AXP221_OUTPUT_CTRL1,
  134. AXP221_OUTPUT_CTRL1_DCDC4_EN);
  135. }
  136. int axp221_set_dcdc5(unsigned int mvolt)
  137. {
  138. int ret;
  139. u8 cfg = axp221_mvolt_to_cfg(mvolt, 1000, 2550, 50);
  140. if (mvolt == 0)
  141. return axp221_clrbits(AXP221_OUTPUT_CTRL1,
  142. AXP221_OUTPUT_CTRL1_DCDC5_EN);
  143. ret = pmic_bus_write(AXP221_DCDC5_CTRL, cfg);
  144. if (ret)
  145. return ret;
  146. return axp221_setbits(AXP221_OUTPUT_CTRL1,
  147. AXP221_OUTPUT_CTRL1_DCDC5_EN);
  148. }
  149. int axp221_set_dldo1(unsigned int mvolt)
  150. {
  151. int ret;
  152. u8 cfg = axp221_mvolt_to_cfg(mvolt, 700, 3300, 100);
  153. if (mvolt == 0)
  154. return axp221_clrbits(AXP221_OUTPUT_CTRL2,
  155. AXP221_OUTPUT_CTRL2_DLDO1_EN);
  156. ret = pmic_bus_write(AXP221_DLDO1_CTRL, cfg);
  157. if (ret)
  158. return ret;
  159. return axp221_setbits(AXP221_OUTPUT_CTRL2,
  160. AXP221_OUTPUT_CTRL2_DLDO1_EN);
  161. }
  162. int axp221_set_dldo2(unsigned int mvolt)
  163. {
  164. int ret;
  165. u8 cfg = axp221_mvolt_to_cfg(mvolt, 700, 3300, 100);
  166. if (mvolt == 0)
  167. return axp221_clrbits(AXP221_OUTPUT_CTRL2,
  168. AXP221_OUTPUT_CTRL2_DLDO2_EN);
  169. ret = pmic_bus_write(AXP221_DLDO2_CTRL, cfg);
  170. if (ret)
  171. return ret;
  172. return axp221_setbits(AXP221_OUTPUT_CTRL2,
  173. AXP221_OUTPUT_CTRL2_DLDO2_EN);
  174. }
  175. int axp221_set_dldo3(unsigned int mvolt)
  176. {
  177. int ret;
  178. u8 cfg = axp221_mvolt_to_cfg(mvolt, 700, 3300, 100);
  179. if (mvolt == 0)
  180. return axp221_clrbits(AXP221_OUTPUT_CTRL2,
  181. AXP221_OUTPUT_CTRL2_DLDO3_EN);
  182. ret = pmic_bus_write(AXP221_DLDO3_CTRL, cfg);
  183. if (ret)
  184. return ret;
  185. return axp221_setbits(AXP221_OUTPUT_CTRL2,
  186. AXP221_OUTPUT_CTRL2_DLDO3_EN);
  187. }
  188. int axp221_set_dldo4(unsigned int mvolt)
  189. {
  190. int ret;
  191. u8 cfg = axp221_mvolt_to_cfg(mvolt, 700, 3300, 100);
  192. if (mvolt == 0)
  193. return axp221_clrbits(AXP221_OUTPUT_CTRL2,
  194. AXP221_OUTPUT_CTRL2_DLDO4_EN);
  195. ret = pmic_bus_write(AXP221_DLDO4_CTRL, cfg);
  196. if (ret)
  197. return ret;
  198. return axp221_setbits(AXP221_OUTPUT_CTRL2,
  199. AXP221_OUTPUT_CTRL2_DLDO4_EN);
  200. }
  201. int axp221_set_aldo1(unsigned int mvolt)
  202. {
  203. int ret;
  204. u8 cfg = axp221_mvolt_to_cfg(mvolt, 700, 3300, 100);
  205. if (mvolt == 0)
  206. return axp221_clrbits(AXP221_OUTPUT_CTRL1,
  207. AXP221_OUTPUT_CTRL1_ALDO1_EN);
  208. ret = pmic_bus_write(AXP221_ALDO1_CTRL, cfg);
  209. if (ret)
  210. return ret;
  211. return axp221_setbits(AXP221_OUTPUT_CTRL1,
  212. AXP221_OUTPUT_CTRL1_ALDO1_EN);
  213. }
  214. int axp221_set_aldo2(unsigned int mvolt)
  215. {
  216. int ret;
  217. u8 cfg = axp221_mvolt_to_cfg(mvolt, 700, 3300, 100);
  218. if (mvolt == 0)
  219. return axp221_clrbits(AXP221_OUTPUT_CTRL1,
  220. AXP221_OUTPUT_CTRL1_ALDO2_EN);
  221. ret = pmic_bus_write(AXP221_ALDO2_CTRL, cfg);
  222. if (ret)
  223. return ret;
  224. return axp221_setbits(AXP221_OUTPUT_CTRL1,
  225. AXP221_OUTPUT_CTRL1_ALDO2_EN);
  226. }
  227. int axp221_set_aldo3(unsigned int mvolt)
  228. {
  229. int ret;
  230. u8 cfg = axp221_mvolt_to_cfg(mvolt, 700, 3300, 100);
  231. if (mvolt == 0)
  232. return axp221_clrbits(AXP221_OUTPUT_CTRL3,
  233. AXP221_OUTPUT_CTRL3_ALDO3_EN);
  234. ret = pmic_bus_write(AXP221_ALDO3_CTRL, cfg);
  235. if (ret)
  236. return ret;
  237. return axp221_setbits(AXP221_OUTPUT_CTRL3,
  238. AXP221_OUTPUT_CTRL3_ALDO3_EN);
  239. }
  240. int axp221_set_eldo(int eldo_num, unsigned int mvolt)
  241. {
  242. int ret;
  243. u8 cfg = axp221_mvolt_to_cfg(mvolt, 700, 3300, 100);
  244. u8 addr, bits;
  245. switch (eldo_num) {
  246. case 3:
  247. addr = AXP221_ELDO3_CTRL;
  248. bits = AXP221_OUTPUT_CTRL2_ELDO3_EN;
  249. break;
  250. case 2:
  251. addr = AXP221_ELDO2_CTRL;
  252. bits = AXP221_OUTPUT_CTRL2_ELDO2_EN;
  253. break;
  254. case 1:
  255. addr = AXP221_ELDO1_CTRL;
  256. bits = AXP221_OUTPUT_CTRL2_ELDO1_EN;
  257. break;
  258. default:
  259. return -EINVAL;
  260. }
  261. if (mvolt == 0)
  262. return axp221_clrbits(AXP221_OUTPUT_CTRL2, bits);
  263. ret = pmic_bus_write(addr, cfg);
  264. if (ret)
  265. return ret;
  266. return axp221_setbits(AXP221_OUTPUT_CTRL2, bits);
  267. }
  268. int axp221_init(void)
  269. {
  270. /* This cannot be 0 because it is used in SPL before BSS is ready */
  271. static int needs_init = 1;
  272. u8 axp_chip_id;
  273. int ret;
  274. if (!needs_init)
  275. return 0;
  276. ret = pmic_bus_init();
  277. if (ret)
  278. return ret;
  279. ret = pmic_bus_read(AXP221_CHIP_ID, &axp_chip_id);
  280. if (ret)
  281. return ret;
  282. if (!(axp_chip_id == 0x6 || axp_chip_id == 0x7 || axp_chip_id == 0x17))
  283. return -ENODEV;
  284. needs_init = 0;
  285. return 0;
  286. }
  287. int axp221_get_sid(unsigned int *sid)
  288. {
  289. u8 *dest = (u8 *)sid;
  290. int i, ret;
  291. ret = axp221_init();
  292. if (ret)
  293. return ret;
  294. ret = pmic_bus_write(AXP221_PAGE, 1);
  295. if (ret)
  296. return ret;
  297. for (i = 0; i < 16; i++) {
  298. ret = pmic_bus_read(AXP221_SID + i, &dest[i]);
  299. if (ret)
  300. return ret;
  301. }
  302. pmic_bus_write(AXP221_PAGE, 0);
  303. for (i = 0; i < 4; i++)
  304. sid[i] = be32_to_cpu(sid[i]);
  305. return 0;
  306. }
  307. static int axp_drivebus_setup(void)
  308. {
  309. int ret;
  310. ret = axp221_init();
  311. if (ret)
  312. return ret;
  313. /* Set N_VBUSEN pin to output / DRIVEBUS function */
  314. return axp221_clrbits(AXP221_MISC_CTRL, AXP221_MISC_CTRL_N_VBUSEN_FUNC);
  315. }
  316. int axp_drivebus_enable(void)
  317. {
  318. int ret;
  319. ret = axp_drivebus_setup();
  320. if (ret)
  321. return ret;
  322. /* Set DRIVEBUS high */
  323. return axp221_setbits(AXP221_VBUS_IPSOUT, AXP221_VBUS_IPSOUT_DRIVEBUS);
  324. }
  325. int axp_drivebus_disable(void)
  326. {
  327. int ret;
  328. ret = axp_drivebus_setup();
  329. if (ret)
  330. return ret;
  331. /* Set DRIVEBUS low */
  332. return axp221_clrbits(AXP221_VBUS_IPSOUT, AXP221_VBUS_IPSOUT_DRIVEBUS);
  333. }