as3722.c 3.4 KB

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