mc34vr500.c 2.1 KB

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