mc34vr500.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2016 Freescale Semiconductor, Inc.
  4. * Hou Zhiqiang <Zhiqiang.Hou@freescale.com>
  5. */
  6. #include <common.h>
  7. #include <errno.h>
  8. #include <i2c.h>
  9. #include <power/pmic.h>
  10. #include <power/mc34vr500_pmic.h>
  11. static uint8_t swxvolt_addr[4] = { MC34VR500_SW1VOLT,
  12. MC34VR500_SW2VOLT,
  13. MC34VR500_SW3VOLT,
  14. MC34VR500_SW4VOLT };
  15. static uint8_t swx_set_point_base[4] = { 13, 9, 9, 9 };
  16. int mc34vr500_get_sw_volt(uint8_t sw)
  17. {
  18. struct pmic *p;
  19. u32 swxvolt;
  20. uint8_t spb;
  21. int sw_volt;
  22. int ret;
  23. debug("%s: Get SW%u volt from swxvolt_addr = 0x%x\n",
  24. __func__, sw + 1, swxvolt_addr[sw]);
  25. if (sw > SW4) {
  26. printf("%s: Unsupported SW(sw%d)\n", __func__, sw + 1);
  27. return -EINVAL;
  28. }
  29. p = pmic_get("MC34VR500");
  30. if (!p) {
  31. printf("%s: Did NOT find PMIC MC34VR500\n", __func__);
  32. return -ENODEV;
  33. }
  34. ret = pmic_probe(p);
  35. if (ret)
  36. return ret;
  37. ret = pmic_reg_read(p, swxvolt_addr[sw], &swxvolt);
  38. if (ret) {
  39. printf("%s: Failed to get SW%u volt\n", __func__, sw + 1);
  40. return ret;
  41. }
  42. debug("%s: SW%d step point swxvolt = %u\n", __func__, sw + 1, swxvolt);
  43. spb = swx_set_point_base[sw];
  44. /* The base of SW volt is 625mV and increase by step 25mV */
  45. sw_volt = 625 + (swxvolt - spb) * 25;
  46. debug("%s: SW%u volt = %dmV\n", __func__, sw + 1, sw_volt);
  47. return sw_volt;
  48. }
  49. int mc34vr500_set_sw_volt(uint8_t sw, int sw_volt)
  50. {
  51. struct pmic *p;
  52. u32 swxvolt;
  53. uint8_t spb;
  54. int ret;
  55. debug("%s: Set SW%u volt to %dmV\n", __func__, sw + 1, sw_volt);
  56. /* The least SW volt is 625mV, and only 4 SW outputs */
  57. if (sw > SW4 || sw_volt < 625)
  58. return -EINVAL;
  59. p = pmic_get("MC34VR500");
  60. if (!p) {
  61. printf("%s: Did NOT find PMIC MC34VR500\n", __func__);
  62. return -ENODEV;
  63. }
  64. ret = pmic_probe(p);
  65. if (ret)
  66. return ret;
  67. spb = swx_set_point_base[sw];
  68. /* The base of SW volt is 625mV and increase by step 25mV */
  69. swxvolt = (sw_volt - 625) / 25 + spb;
  70. debug("%s: SW%d step point swxvolt = %u\n", __func__, sw + 1, swxvolt);
  71. if (swxvolt > 63)
  72. return -EINVAL;
  73. ret = pmic_reg_write(p, swxvolt_addr[sw], swxvolt);
  74. if (ret)
  75. return ret;
  76. return 0;
  77. }