tps6586x.c 5.5 KB

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