as3722.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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 <power/as3722.h>
  13. #define AS3722_SD_VOLTAGE(n) (0x00 + (n))
  14. #define AS3722_GPIO_CONTROL(n) (0x08 + (n))
  15. #define AS3722_GPIO_CONTROL_MODE_OUTPUT_VDDH (1 << 0)
  16. #define AS3722_GPIO_CONTROL_MODE_OUTPUT_VDDL (7 << 0)
  17. #define AS3722_GPIO_CONTROL_INVERT (1 << 7)
  18. #define AS3722_LDO_VOLTAGE(n) (0x10 + (n))
  19. #define AS3722_GPIO_SIGNAL_OUT 0x20
  20. #define AS3722_SD_CONTROL 0x4d
  21. #define AS3722_LDO_CONTROL 0x4e
  22. #define AS3722_ASIC_ID1 0x90
  23. #define AS3722_DEVICE_ID 0x0c
  24. #define AS3722_ASIC_ID2 0x91
  25. int as3722_read(struct udevice *pmic, u8 reg, u8 *value)
  26. {
  27. int err;
  28. err = dm_i2c_read(pmic, reg, value, 1);
  29. if (err < 0)
  30. return err;
  31. return 0;
  32. }
  33. int as3722_write(struct udevice *pmic, u8 reg, u8 value)
  34. {
  35. int err;
  36. err = dm_i2c_write(pmic, reg, &value, 1);
  37. if (err < 0)
  38. return err;
  39. return 0;
  40. }
  41. static int as3722_read_id(struct udevice *pmic, u8 *id, u8 *revision)
  42. {
  43. int err;
  44. err = as3722_read(pmic, AS3722_ASIC_ID1, id);
  45. if (err) {
  46. error("failed to read ID1 register: %d", err);
  47. return err;
  48. }
  49. err = as3722_read(pmic, AS3722_ASIC_ID2, revision);
  50. if (err) {
  51. error("failed to read ID2 register: %d", err);
  52. return err;
  53. }
  54. return 0;
  55. }
  56. int as3722_sd_enable(struct udevice *pmic, unsigned int sd)
  57. {
  58. u8 value;
  59. int err;
  60. if (sd > 6)
  61. return -EINVAL;
  62. err = as3722_read(pmic, AS3722_SD_CONTROL, &value);
  63. if (err) {
  64. error("failed to read SD control register: %d", err);
  65. return err;
  66. }
  67. value |= 1 << sd;
  68. err = as3722_write(pmic, AS3722_SD_CONTROL, value);
  69. if (err < 0) {
  70. error("failed to write SD control register: %d", err);
  71. return err;
  72. }
  73. return 0;
  74. }
  75. int as3722_sd_set_voltage(struct udevice *pmic, unsigned int sd, u8 value)
  76. {
  77. int err;
  78. if (sd > 6)
  79. return -EINVAL;
  80. err = as3722_write(pmic, AS3722_SD_VOLTAGE(sd), value);
  81. if (err < 0) {
  82. error("failed to write SD%u voltage register: %d", sd, err);
  83. return err;
  84. }
  85. return 0;
  86. }
  87. int as3722_ldo_enable(struct udevice *pmic, unsigned int ldo)
  88. {
  89. u8 value;
  90. int err;
  91. if (ldo > 11)
  92. return -EINVAL;
  93. err = as3722_read(pmic, AS3722_LDO_CONTROL, &value);
  94. if (err) {
  95. error("failed to read LDO control register: %d", err);
  96. return err;
  97. }
  98. value |= 1 << ldo;
  99. err = as3722_write(pmic, AS3722_LDO_CONTROL, value);
  100. if (err < 0) {
  101. error("failed to write LDO control register: %d", err);
  102. return err;
  103. }
  104. return 0;
  105. }
  106. int as3722_ldo_set_voltage(struct udevice *pmic, unsigned int ldo, u8 value)
  107. {
  108. int err;
  109. if (ldo > 11)
  110. return -EINVAL;
  111. err = as3722_write(pmic, AS3722_LDO_VOLTAGE(ldo), value);
  112. if (err < 0) {
  113. error("failed to write LDO%u voltage register: %d", ldo,
  114. err);
  115. return err;
  116. }
  117. return 0;
  118. }
  119. int as3722_gpio_configure(struct udevice *pmic, unsigned int gpio,
  120. unsigned long flags)
  121. {
  122. u8 value = 0;
  123. int err;
  124. if (flags & AS3722_GPIO_OUTPUT_VDDH)
  125. value |= AS3722_GPIO_CONTROL_MODE_OUTPUT_VDDH;
  126. if (flags & AS3722_GPIO_INVERT)
  127. value |= AS3722_GPIO_CONTROL_INVERT;
  128. err = as3722_write(pmic, AS3722_GPIO_CONTROL(gpio), value);
  129. if (err) {
  130. error("failed to configure GPIO#%u: %d", gpio, err);
  131. return err;
  132. }
  133. return 0;
  134. }
  135. static int as3722_gpio_set(struct udevice *pmic, unsigned int gpio,
  136. unsigned int level)
  137. {
  138. const char *l;
  139. u8 value;
  140. int err;
  141. if (gpio > 7)
  142. return -EINVAL;
  143. err = as3722_read(pmic, AS3722_GPIO_SIGNAL_OUT, &value);
  144. if (err < 0) {
  145. error("failed to read GPIO signal out register: %d", err);
  146. return err;
  147. }
  148. if (level == 0) {
  149. value &= ~(1 << gpio);
  150. l = "low";
  151. } else {
  152. value |= 1 << gpio;
  153. l = "high";
  154. }
  155. err = as3722_write(pmic, AS3722_GPIO_SIGNAL_OUT, value);
  156. if (err) {
  157. error("failed to set GPIO#%u %s: %d", gpio, l, err);
  158. return err;
  159. }
  160. return 0;
  161. }
  162. int as3722_gpio_direction_output(struct udevice *pmic, unsigned int gpio,
  163. unsigned int level)
  164. {
  165. u8 value;
  166. int err;
  167. if (gpio > 7)
  168. return -EINVAL;
  169. if (level == 0)
  170. value = AS3722_GPIO_CONTROL_MODE_OUTPUT_VDDL;
  171. else
  172. value = AS3722_GPIO_CONTROL_MODE_OUTPUT_VDDH;
  173. err = as3722_write(pmic, AS3722_GPIO_CONTROL(gpio), value);
  174. if (err) {
  175. error("failed to configure GPIO#%u as output: %d", gpio, err);
  176. return err;
  177. }
  178. err = as3722_gpio_set(pmic, gpio, level);
  179. if (err < 0) {
  180. error("failed to set GPIO#%u high: %d", gpio, err);
  181. return err;
  182. }
  183. return 0;
  184. }
  185. /* Temporary function until we get the pmic framework */
  186. int as3722_get(struct udevice **devp)
  187. {
  188. int bus = 0;
  189. int address = 0x40;
  190. return i2c_get_chip_for_busnum(bus, address, 1, devp);
  191. }
  192. int as3722_init(struct udevice **devp)
  193. {
  194. struct udevice *pmic;
  195. u8 id, revision;
  196. const unsigned int bus = 0;
  197. const unsigned int address = 0x40;
  198. int err;
  199. err = i2c_get_chip_for_busnum(bus, address, 1, &pmic);
  200. if (err)
  201. return err;
  202. err = as3722_read_id(pmic, &id, &revision);
  203. if (err < 0) {
  204. error("failed to read ID: %d", err);
  205. return err;
  206. }
  207. if (id != AS3722_DEVICE_ID) {
  208. error("unknown device");
  209. return -ENOENT;
  210. }
  211. debug("AS3722 revision %#x found on I2C bus %u, address %#x\n",
  212. revision, bus, address);
  213. if (devp)
  214. *devp = pmic;
  215. return 0;
  216. }