1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#define gk20a_volt(p) container_of((p), struct gk20a_volt, base)
23#include "priv.h"
24
25#include <core/tegra.h>
26
27#include "gk20a.h"
28
29static const struct cvb_coef gk20a_cvb_coef[] = {
30
31 { 1209886, -36468, 515, 417, -13123, 203},
32 { 1130804, -27659, 296, 298, -10834, 221},
33 { 1162871, -27110, 247, 238, -10681, 268},
34 { 1220458, -28654, 247, 179, -10376, 298},
35 { 1280953, -30204, 247, 119, -9766, 304},
36 { 1344547, -31777, 247, 119, -8545, 292},
37 { 1420168, -34227, 269, 60, -7172, 256},
38 { 1490757, -35955, 274, 60, -5188, 197},
39 { 1599112, -42583, 398, 0, -1831, 119},
40 { 1366986, -16459, -274, 0, -3204, 72},
41 { 1391884, -17078, -274, -60, -1526, 30},
42 { 1415522, -17497, -274, -60, -458, 0},
43 { 1464061, -18331, -274, -119, 1831, -72},
44 { 1524225, -20064, -254, -119, 4272, -155},
45 { 1608418, -21643, -269, 0, 763, -48},
46};
47
48
49
50
51static inline int
52gk20a_volt_get_cvb_voltage(int speedo, int s_scale, const struct cvb_coef *coef)
53{
54 int mv;
55
56 mv = DIV_ROUND_CLOSEST(coef->c2 * speedo, s_scale);
57 mv = DIV_ROUND_CLOSEST((mv + coef->c1) * speedo, s_scale) + coef->c0;
58 return mv;
59}
60
61
62
63
64
65
66static inline int
67gk20a_volt_get_cvb_t_voltage(int speedo, int temp, int s_scale, int t_scale,
68 const struct cvb_coef *coef)
69{
70 int cvb_mv, mv;
71
72 cvb_mv = gk20a_volt_get_cvb_voltage(speedo, s_scale, coef);
73
74 mv = DIV_ROUND_CLOSEST(coef->c3 * speedo, s_scale) + coef->c4 +
75 DIV_ROUND_CLOSEST(coef->c5 * temp, t_scale);
76 mv = DIV_ROUND_CLOSEST(mv * temp, t_scale) + cvb_mv;
77 return mv;
78}
79
80static int
81gk20a_volt_calc_voltage(const struct cvb_coef *coef, int speedo)
82{
83 static const int v_scale = 1000;
84 int mv;
85
86 mv = gk20a_volt_get_cvb_t_voltage(speedo, -10, 100, 10, coef);
87 mv = DIV_ROUND_UP(mv, v_scale);
88
89 return mv * 1000;
90}
91
92static int
93gk20a_volt_vid_get(struct nvkm_volt *base)
94{
95 struct gk20a_volt *volt = gk20a_volt(base);
96 int i, uv;
97
98 uv = regulator_get_voltage(volt->vdd);
99
100 for (i = 0; i < volt->base.vid_nr; i++)
101 if (volt->base.vid[i].uv >= uv)
102 return i;
103
104 return -EINVAL;
105}
106
107static int
108gk20a_volt_vid_set(struct nvkm_volt *base, u8 vid)
109{
110 struct gk20a_volt *volt = gk20a_volt(base);
111 struct nvkm_subdev *subdev = &volt->base.subdev;
112
113 nvkm_debug(subdev, "set voltage as %duv\n", volt->base.vid[vid].uv);
114 return regulator_set_voltage(volt->vdd, volt->base.vid[vid].uv, 1200000);
115}
116
117static int
118gk20a_volt_set_id(struct nvkm_volt *base, u8 id, int condition)
119{
120 struct gk20a_volt *volt = gk20a_volt(base);
121 struct nvkm_subdev *subdev = &volt->base.subdev;
122 int prev_uv = regulator_get_voltage(volt->vdd);
123 int target_uv = volt->base.vid[id].uv;
124 int ret;
125
126 nvkm_debug(subdev, "prev=%d, target=%d, condition=%d\n",
127 prev_uv, target_uv, condition);
128 if (!condition ||
129 (condition < 0 && target_uv < prev_uv) ||
130 (condition > 0 && target_uv > prev_uv)) {
131 ret = gk20a_volt_vid_set(&volt->base, volt->base.vid[id].vid);
132 } else {
133 ret = 0;
134 }
135
136 return ret;
137}
138
139static const struct nvkm_volt_func
140gk20a_volt = {
141 .vid_get = gk20a_volt_vid_get,
142 .vid_set = gk20a_volt_vid_set,
143 .set_id = gk20a_volt_set_id,
144};
145
146int
147gk20a_volt_ctor(struct nvkm_device *device, int index,
148 const struct cvb_coef *coefs, int nb_coefs,
149 int vmin, struct gk20a_volt *volt)
150{
151 struct nvkm_device_tegra *tdev = device->func->tegra(device);
152 int i, uv;
153
154 nvkm_volt_ctor(&gk20a_volt, device, index, &volt->base);
155
156 uv = regulator_get_voltage(tdev->vdd);
157 nvkm_debug(&volt->base.subdev, "the default voltage is %duV\n", uv);
158
159 volt->vdd = tdev->vdd;
160
161 volt->base.vid_nr = nb_coefs;
162 for (i = 0; i < volt->base.vid_nr; i++) {
163 volt->base.vid[i].vid = i;
164 volt->base.vid[i].uv = max(
165 gk20a_volt_calc_voltage(&coefs[i], tdev->gpu_speedo),
166 vmin);
167 nvkm_debug(&volt->base.subdev, "%2d: vid=%d, uv=%d\n", i,
168 volt->base.vid[i].vid, volt->base.vid[i].uv);
169 }
170
171 return 0;
172}
173
174int
175gk20a_volt_new(struct nvkm_device *device, int index, struct nvkm_volt **pvolt)
176{
177 struct gk20a_volt *volt;
178
179 volt = kzalloc(sizeof(*volt), GFP_KERNEL);
180 if (!volt)
181 return -ENOMEM;
182 *pvolt = &volt->base;
183
184 return gk20a_volt_ctor(device, index, gk20a_cvb_coef,
185 ARRAY_SIZE(gk20a_cvb_coef), 0, volt);
186}
187