tps6586x.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2011 The Chromium OS Authors.
  4. * (C) Copyright 2010,2011 NVIDIA Corporation <www.nvidia.com>
  5. */
  6. #include <common.h>
  7. #include <tps6586x.h>
  8. #include <asm/io.h>
  9. #include <i2c.h>
  10. static struct udevice *tps6586x_dev;
  11. enum {
  12. /* Registers that we access */
  13. SUPPLY_CONTROL1 = 0x20,
  14. SUPPLY_CONTROL2,
  15. SM1_VOLTAGE_V1 = 0x23,
  16. SM1_VOLTAGE_V2,
  17. SM0_VOLTAGE_V1 = 0x26,
  18. SM0_VOLTAGE_V2,
  19. PFM_MODE = 0x47,
  20. /* Bits in the supply control registers */
  21. CTRL_SM1_RAMP = 0x01,
  22. CTRL_SM1_SUPPLY2 = 0x02,
  23. CTRL_SM0_RAMP = 0x04,
  24. CTRL_SM0_SUPPLY2 = 0x08,
  25. };
  26. #define MAX_I2C_RETRY 3
  27. static int tps6586x_read(int reg)
  28. {
  29. int i;
  30. uchar data;
  31. int retval = -1;
  32. for (i = 0; i < MAX_I2C_RETRY; ++i) {
  33. if (!dm_i2c_read(tps6586x_dev, reg, &data, 1)) {
  34. retval = (int)data;
  35. goto exit;
  36. }
  37. /* i2c access failed, retry */
  38. udelay(100);
  39. }
  40. exit:
  41. debug("pmu_read %x=%x\n", reg, retval);
  42. if (retval < 0)
  43. debug("%s: failed to read register %#x: %d\n", __func__, reg,
  44. retval);
  45. return retval;
  46. }
  47. static int tps6586x_write(int reg, uchar *data, uint len)
  48. {
  49. int i;
  50. int retval = -1;
  51. for (i = 0; i < MAX_I2C_RETRY; ++i) {
  52. if (!dm_i2c_write(tps6586x_dev, reg, data, len)) {
  53. retval = 0;
  54. goto exit;
  55. }
  56. /* i2c access failed, retry */
  57. udelay(100);
  58. }
  59. exit:
  60. debug("pmu_write %x=%x: ", reg, retval);
  61. for (i = 0; i < len; i++)
  62. debug("%x ", data[i]);
  63. if (retval)
  64. debug("%s: failed to write register %#x\n", __func__, reg);
  65. return retval;
  66. }
  67. /*
  68. * Get current voltage of SM0 and SM1
  69. *
  70. * @param sm0 Place to put SM0 voltage
  71. * @param sm1 Place to put SM1 voltage
  72. * @return 0 if ok, -1 on error
  73. */
  74. static int read_voltages(int *sm0, int *sm1)
  75. {
  76. int ctrl1, ctrl2;
  77. int is_v2;
  78. /*
  79. * Each vdd has two supply sources, ie, v1 and v2.
  80. * The supply control reg1 and reg2 determine the current selection.
  81. */
  82. ctrl1 = tps6586x_read(SUPPLY_CONTROL1);
  83. ctrl2 = tps6586x_read(SUPPLY_CONTROL2);
  84. if (ctrl1 == -1 || ctrl2 == -1)
  85. return -ENOTSUPP;
  86. /* Figure out whether V1 or V2 is selected */
  87. is_v2 = (ctrl1 | ctrl2) & CTRL_SM0_SUPPLY2;
  88. *sm0 = tps6586x_read(is_v2 ? SM0_VOLTAGE_V2 : SM0_VOLTAGE_V1);
  89. *sm1 = tps6586x_read(is_v2 ? SM1_VOLTAGE_V2 : SM1_VOLTAGE_V1);
  90. if (*sm0 == -1 || *sm1 == -1)
  91. return -ENOTSUPP;
  92. return 0;
  93. }
  94. static int set_voltage(int reg, int data, int rate)
  95. {
  96. uchar control_bit;
  97. uchar buff[3];
  98. control_bit = (reg == SM0_VOLTAGE_V1 ? CTRL_SM0_RAMP : CTRL_SM1_RAMP);
  99. /*
  100. * Only one supply is needed in u-boot. set both v1 and v2 to
  101. * same value.
  102. *
  103. * When both v1 and v2 are set to same value, we just need to set
  104. * control1 reg to trigger the supply selection.
  105. */
  106. buff[0] = buff[1] = (uchar)data;
  107. buff[2] = rate;
  108. /* write v1, v2 and rate, then trigger */
  109. if (tps6586x_write(reg, buff, 3) ||
  110. tps6586x_write(SUPPLY_CONTROL1, &control_bit, 1))
  111. return -ENOTSUPP;
  112. return 0;
  113. }
  114. static int calculate_next_voltage(int voltage, int target, int step)
  115. {
  116. int diff = voltage < target ? step : -step;
  117. if (abs(target - voltage) > step)
  118. voltage += diff;
  119. else
  120. voltage = target;
  121. return voltage;
  122. }
  123. int tps6586x_set_pwm_mode(int mask)
  124. {
  125. uchar val;
  126. int ret;
  127. assert(tps6586x_dev);
  128. ret = tps6586x_read(PFM_MODE);
  129. if (ret != -1) {
  130. val = (uchar)ret;
  131. val |= mask;
  132. ret = tps6586x_write(PFM_MODE, &val, 1);
  133. }
  134. if (ret == -1)
  135. debug("%s: Failed to read/write PWM mode reg\n", __func__);
  136. return ret;
  137. }
  138. int tps6586x_adjust_sm0_sm1(int sm0_target, int sm1_target, int step, int rate,
  139. int min_sm0_over_sm1)
  140. {
  141. int sm0, sm1;
  142. int bad;
  143. assert(tps6586x_dev);
  144. /* get current voltage settings */
  145. if (read_voltages(&sm0, &sm1)) {
  146. debug("%s: Cannot read voltage settings\n", __func__);
  147. return -EINVAL;
  148. }
  149. /*
  150. * if vdd_core < vdd_cpu + rel
  151. * skip
  152. *
  153. * This condition may happen when system reboots due to kernel crash.
  154. */
  155. if (min_sm0_over_sm1 != -1 && sm0 < sm1 + min_sm0_over_sm1) {
  156. debug("%s: SM0 is %d, SM1 is %d, but min_sm0_over_sm1 is %d\n",
  157. __func__, sm0, sm1, min_sm0_over_sm1);
  158. return -EINVAL;
  159. }
  160. /*
  161. * Since vdd_core and vdd_cpu may both stand at either greater or less
  162. * than their nominal voltage, the adjustment may go either directions.
  163. *
  164. * Make sure vdd_core is always higher than vdd_cpu with certain margin.
  165. * So, find out which vdd to adjust first in each step.
  166. *
  167. * case 1: both sm0 and sm1 need to move up
  168. * adjust sm0 before sm1
  169. *
  170. * case 2: both sm0 and sm1 need to move down
  171. * adjust sm1 before sm0
  172. *
  173. * case 3: sm0 moves down and sm1 moves up
  174. * adjusting either one first is fine.
  175. *
  176. * Adjust vdd_core and vdd_cpu one step at a time until they reach
  177. * their nominal values.
  178. */
  179. bad = 0;
  180. while (!bad && (sm0 != sm0_target || sm1 != sm1_target)) {
  181. int adjust_sm0_late = 0; /* flag to adjust vdd_core later */
  182. debug("%d-%d %d-%d ", sm0, sm0_target, sm1, sm1_target);
  183. if (sm0 != sm0_target) {
  184. /*
  185. * if case 1 and case 3, set new sm0 first.
  186. * otherwise, hold down until new sm1 is set.
  187. */
  188. sm0 = calculate_next_voltage(sm0, sm0_target, step);
  189. if (sm1 < sm1_target)
  190. bad |= set_voltage(SM0_VOLTAGE_V1, sm0, rate);
  191. else
  192. adjust_sm0_late = 1;
  193. }
  194. if (sm1 != sm1_target) {
  195. sm1 = calculate_next_voltage(sm1, sm1_target, step);
  196. bad |= set_voltage(SM1_VOLTAGE_V1, sm1, rate);
  197. }
  198. if (adjust_sm0_late)
  199. bad |= set_voltage(SM0_VOLTAGE_V1, sm0, rate);
  200. debug("%d\n", adjust_sm0_late);
  201. }
  202. debug("%d-%d %d-%d done\n", sm0, sm0_target, sm1, sm1_target);
  203. return bad ? -EINVAL : 0;
  204. }
  205. int tps6586x_init(struct udevice *dev)
  206. {
  207. tps6586x_dev = dev;
  208. return 0;
  209. }