tps6586x.c 5.6 KB

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