as3722.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Copyright (C) 2014 NVIDIA Corporation
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #define pr_fmt(fmt) "as3722: " fmt
  7. #include <common.h>
  8. #include <dm.h>
  9. #include <errno.h>
  10. #include <fdtdec.h>
  11. #include <i2c.h>
  12. #include <dm/lists.h>
  13. #include <power/as3722.h>
  14. #include <power/pmic.h>
  15. #define AS3722_NUM_OF_REGS 0x92
  16. static int as3722_read(struct udevice *dev, uint reg, uint8_t *buff, int len)
  17. {
  18. int ret;
  19. ret = dm_i2c_read(dev, reg, buff, len);
  20. if (ret < 0)
  21. return ret;
  22. return 0;
  23. }
  24. static int as3722_write(struct udevice *dev, uint reg, const uint8_t *buff,
  25. int len)
  26. {
  27. int ret;
  28. ret = dm_i2c_write(dev, reg, buff, len);
  29. if (ret < 0)
  30. return ret;
  31. return 0;
  32. }
  33. static int as3722_read_id(struct udevice *dev, uint *idp, uint *revisionp)
  34. {
  35. int ret;
  36. ret = pmic_reg_read(dev, AS3722_ASIC_ID1);
  37. if (ret < 0) {
  38. pr_err("failed to read ID1 register: %d", ret);
  39. return ret;
  40. }
  41. *idp = ret;
  42. ret = pmic_reg_read(dev, AS3722_ASIC_ID2);
  43. if (ret < 0) {
  44. pr_err("failed to read ID2 register: %d", ret);
  45. return ret;
  46. }
  47. *revisionp = ret;
  48. return 0;
  49. }
  50. /* TODO(treding@nvidia.com): Add proper regulator support to avoid this */
  51. int as3722_sd_set_voltage(struct udevice *dev, unsigned int sd, u8 value)
  52. {
  53. int ret;
  54. if (sd > 6)
  55. return -EINVAL;
  56. ret = pmic_reg_write(dev, AS3722_SD_VOLTAGE(sd), value);
  57. if (ret < 0) {
  58. pr_err("failed to write SD%u voltage register: %d", sd, ret);
  59. return ret;
  60. }
  61. return 0;
  62. }
  63. int as3722_ldo_set_voltage(struct udevice *dev, unsigned int ldo, u8 value)
  64. {
  65. int ret;
  66. if (ldo > 11)
  67. return -EINVAL;
  68. ret = pmic_reg_write(dev, AS3722_LDO_VOLTAGE(ldo), value);
  69. if (ret < 0) {
  70. pr_err("failed to write LDO%u voltage register: %d", ldo,
  71. ret);
  72. return ret;
  73. }
  74. return 0;
  75. }
  76. static int as3722_probe(struct udevice *dev)
  77. {
  78. uint id, revision;
  79. int ret;
  80. ret = as3722_read_id(dev, &id, &revision);
  81. if (ret < 0) {
  82. pr_err("failed to read ID: %d", ret);
  83. return ret;
  84. }
  85. if (id != AS3722_DEVICE_ID) {
  86. pr_err("unknown device");
  87. return -ENOENT;
  88. }
  89. debug("AS3722 revision %#x found on I2C bus %s\n", revision, dev->name);
  90. return 0;
  91. }
  92. #if CONFIG_IS_ENABLED(PMIC_CHILDREN)
  93. static const struct pmic_child_info pmic_children_info[] = {
  94. { .prefix = "sd", .driver = "as3722_stepdown"},
  95. { .prefix = "ldo", .driver = "as3722_ldo"},
  96. { },
  97. };
  98. static int as3722_bind(struct udevice *dev)
  99. {
  100. struct udevice *gpio_dev;
  101. ofnode regulators_node;
  102. int children;
  103. int ret;
  104. regulators_node = dev_read_subnode(dev, "regulators");
  105. if (!ofnode_valid(regulators_node)) {
  106. debug("%s: %s regulators subnode not found\n", __func__,
  107. dev->name);
  108. return -ENXIO;
  109. }
  110. children = pmic_bind_children(dev, regulators_node, pmic_children_info);
  111. if (!children)
  112. debug("%s: %s - no child found\n", __func__, dev->name);
  113. ret = device_bind_driver(dev, "gpio_as3722", "gpio_as3722", &gpio_dev);
  114. if (ret) {
  115. debug("%s: Cannot bind GPIOs (ret=%d)\n", __func__, ret);
  116. return ret;
  117. }
  118. return 0;
  119. }
  120. #endif
  121. static int as3722_reg_count(struct udevice *dev)
  122. {
  123. return AS3722_NUM_OF_REGS;
  124. }
  125. static struct dm_pmic_ops as3722_ops = {
  126. .reg_count = as3722_reg_count,
  127. .read = as3722_read,
  128. .write = as3722_write,
  129. };
  130. static const struct udevice_id as3722_ids[] = {
  131. { .compatible = "ams,as3722" },
  132. { }
  133. };
  134. U_BOOT_DRIVER(pmic_as3722) = {
  135. .name = "as3722_pmic",
  136. .id = UCLASS_PMIC,
  137. .of_match = as3722_ids,
  138. #if CONFIG_IS_ENABLED(PMIC_CHILDREN)
  139. .bind = as3722_bind,
  140. #endif
  141. .probe = as3722_probe,
  142. .ops = &as3722_ops,
  143. };