axp152.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * (C) Copyright 2012
  3. * Henrik Nordstrom <henrik@henriknordstrom.net>
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <i2c.h>
  9. #include <axp152.h>
  10. static int axp152_write(enum axp152_reg reg, u8 val)
  11. {
  12. return i2c_write(0x30, reg, 1, &val, 1);
  13. }
  14. static int axp152_read(enum axp152_reg reg, u8 *val)
  15. {
  16. return i2c_read(0x30, reg, 1, val, 1);
  17. }
  18. static u8 axp152_mvolt_to_target(int mvolt, int min, int max, int div)
  19. {
  20. if (mvolt < min)
  21. mvolt = min;
  22. else if (mvolt > max)
  23. mvolt = max;
  24. return (mvolt - min) / div;
  25. }
  26. int axp152_set_dcdc2(int mvolt)
  27. {
  28. int rc;
  29. u8 current, target;
  30. target = axp152_mvolt_to_target(mvolt, 700, 2275, 25);
  31. /* Do we really need to be this gentle? It has built-in voltage slope */
  32. while ((rc = axp152_read(AXP152_DCDC2_VOLTAGE, &current)) == 0 &&
  33. current != target) {
  34. if (current < target)
  35. current++;
  36. else
  37. current--;
  38. rc = axp152_write(AXP152_DCDC2_VOLTAGE, current);
  39. if (rc)
  40. break;
  41. }
  42. return rc;
  43. }
  44. int axp152_set_dcdc3(int mvolt)
  45. {
  46. u8 target = axp152_mvolt_to_target(mvolt, 700, 3500, 50);
  47. return axp152_write(AXP152_DCDC3_VOLTAGE, target);
  48. }
  49. int axp152_set_dcdc4(int mvolt)
  50. {
  51. u8 target = axp152_mvolt_to_target(mvolt, 700, 3500, 25);
  52. return axp152_write(AXP152_DCDC4_VOLTAGE, target);
  53. }
  54. int axp152_set_ldo2(int mvolt)
  55. {
  56. u8 target = axp152_mvolt_to_target(mvolt, 700, 3500, 100);
  57. return axp152_write(AXP152_LDO2_VOLTAGE, target);
  58. }
  59. int axp152_init(void)
  60. {
  61. u8 ver;
  62. int rc;
  63. rc = axp152_read(AXP152_CHIP_VERSION, &ver);
  64. if (rc)
  65. return rc;
  66. if (ver != 0x05)
  67. return -1;
  68. return 0;
  69. }