]>
Commit | Line | Data |
---|---|---|
330c5988 BS |
1 | /* |
2 | * Copyright 2010 Red Hat Inc. | |
3 | * | |
4 | * Permission is hereby granted, free of charge, to any person obtaining a | |
5 | * copy of this software and associated documentation files (the "Software"), | |
6 | * to deal in the Software without restriction, including without limitation | |
7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, | |
8 | * and/or sell copies of the Software, and to permit persons to whom the | |
9 | * Software is furnished to do so, subject to the following conditions: | |
10 | * | |
11 | * The above copyright notice and this permission notice shall be included in | |
12 | * all copies or substantial portions of the Software. | |
13 | * | |
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
17 | * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR | |
18 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, | |
19 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | |
20 | * OTHER DEALINGS IN THE SOFTWARE. | |
21 | * | |
22 | * Authors: Ben Skeggs | |
23 | */ | |
24 | ||
25 | #include "drmP.h" | |
26 | ||
27 | #include "nouveau_drv.h" | |
28 | #include "nouveau_pm.h" | |
a0b25635 | 29 | #include "nouveau_gpio.h" |
330c5988 | 30 | |
6032649d BS |
31 | #ifdef CONFIG_ACPI |
32 | #include <linux/acpi.h> | |
33 | #endif | |
34 | #include <linux/power_supply.h> | |
34e9d85a MP |
35 | #include <linux/hwmon.h> |
36 | #include <linux/hwmon-sysfs.h> | |
37 | ||
a175094c BS |
38 | static int |
39 | nouveau_pwmfan_get(struct drm_device *dev) | |
40 | { | |
41 | struct drm_nouveau_private *dev_priv = dev->dev_private; | |
a175094c | 42 | struct nouveau_pm_engine *pm = &dev_priv->engine.pm; |
a0b25635 | 43 | struct gpio_func gpio; |
a175094c BS |
44 | u32 divs, duty; |
45 | int ret; | |
46 | ||
1e054157 | 47 | if (!pm->pwm_get) |
a175094c | 48 | return -ENODEV; |
a175094c | 49 | |
a0b25635 BS |
50 | ret = nouveau_gpio_find(dev, 0, DCB_GPIO_PWM_FAN, 0xff, &gpio); |
51 | if (ret == 0) { | |
52 | ret = pm->pwm_get(dev, gpio.line, &divs, &duty); | |
3d8a408c | 53 | if (ret == 0 && divs) { |
a175094c | 54 | divs = max(divs, duty); |
a0b25635 | 55 | if (dev_priv->card_type <= NV_40 || (gpio.log[0] & 1)) |
a175094c BS |
56 | duty = divs - duty; |
57 | return (duty * 100) / divs; | |
58 | } | |
59 | ||
a0b25635 | 60 | return nouveau_gpio_func_get(dev, gpio.func) * 100; |
a175094c BS |
61 | } |
62 | ||
63 | return -ENODEV; | |
64 | } | |
65 | ||
66 | static int | |
67 | nouveau_pwmfan_set(struct drm_device *dev, int percent) | |
68 | { | |
69 | struct drm_nouveau_private *dev_priv = dev->dev_private; | |
70 | struct nouveau_pm_engine *pm = &dev_priv->engine.pm; | |
a0b25635 | 71 | struct gpio_func gpio; |
a175094c | 72 | u32 divs, duty; |
a0b25635 | 73 | int ret; |
a175094c | 74 | |
1e054157 | 75 | if (!pm->pwm_set) |
a175094c | 76 | return -ENODEV; |
a175094c | 77 | |
a0b25635 BS |
78 | ret = nouveau_gpio_find(dev, 0, DCB_GPIO_PWM_FAN, 0xff, &gpio); |
79 | if (ret == 0) { | |
b1aa5531 | 80 | divs = pm->fan.pwm_divisor; |
a175094c BS |
81 | if (pm->fan.pwm_freq) { |
82 | /*XXX: PNVIO clock more than likely... */ | |
83 | divs = 135000 / pm->fan.pwm_freq; | |
84 | if (dev_priv->chipset < 0xa3) | |
85 | divs /= 4; | |
86 | } | |
87 | ||
88 | duty = ((divs * percent) + 99) / 100; | |
a0b25635 | 89 | if (dev_priv->card_type <= NV_40 || (gpio.log[0] & 1)) |
a175094c BS |
90 | duty = divs - duty; |
91 | ||
bc6389e4 MP |
92 | ret = pm->pwm_set(dev, gpio.line, divs, duty); |
93 | if (!ret) | |
94 | pm->fan.percent = percent; | |
95 | return ret; | |
a175094c BS |
96 | } |
97 | ||
98 | return -ENODEV; | |
99 | } | |
100 | ||
64f1c11a | 101 | static int |
0b627a0b BS |
102 | nouveau_pm_perflvl_aux(struct drm_device *dev, struct nouveau_pm_level *perflvl, |
103 | struct nouveau_pm_level *a, struct nouveau_pm_level *b) | |
64f1c11a BS |
104 | { |
105 | struct drm_nouveau_private *dev_priv = dev->dev_private; | |
106 | struct nouveau_pm_engine *pm = &dev_priv->engine.pm; | |
107 | int ret; | |
108 | ||
11b7d895 MP |
109 | /*XXX: not on all boards, we should control based on temperature |
110 | * on recent boards.. or maybe on some other factor we don't | |
111 | * know about? | |
112 | */ | |
0b627a0b | 113 | if (a->fanspeed && b->fanspeed && b->fanspeed > a->fanspeed) { |
a175094c | 114 | ret = nouveau_pwmfan_set(dev, perflvl->fanspeed); |
0b627a0b BS |
115 | if (ret && ret != -ENODEV) { |
116 | NV_ERROR(dev, "fanspeed set failed: %d\n", ret); | |
117 | return ret; | |
118 | } | |
771e1035 BS |
119 | } |
120 | ||
0b627a0b | 121 | if (pm->voltage.supported && pm->voltage_set) { |
d2edab4a | 122 | if (perflvl->volt_min && b->volt_min > a->volt_min) { |
0b627a0b BS |
123 | ret = pm->voltage_set(dev, perflvl->volt_min); |
124 | if (ret) { | |
125 | NV_ERROR(dev, "voltage set failed: %d\n", ret); | |
126 | return ret; | |
127 | } | |
64f1c11a BS |
128 | } |
129 | } | |
130 | ||
0b627a0b BS |
131 | return 0; |
132 | } | |
133 | ||
134 | static int | |
135 | nouveau_pm_perflvl_set(struct drm_device *dev, struct nouveau_pm_level *perflvl) | |
136 | { | |
137 | struct drm_nouveau_private *dev_priv = dev->dev_private; | |
138 | struct nouveau_pm_engine *pm = &dev_priv->engine.pm; | |
139 | void *state; | |
140 | int ret; | |
141 | ||
142 | if (perflvl == pm->cur) | |
143 | return 0; | |
144 | ||
145 | ret = nouveau_pm_perflvl_aux(dev, perflvl, pm->cur, perflvl); | |
146 | if (ret) | |
147 | return ret; | |
148 | ||
ff2b6c6e | 149 | state = pm->clocks_pre(dev, perflvl); |
b0103747 MP |
150 | if (IS_ERR(state)) { |
151 | ret = PTR_ERR(state); | |
152 | goto error; | |
153 | } | |
154 | ret = pm->clocks_set(dev, state); | |
155 | if (ret) | |
156 | goto error; | |
64f1c11a | 157 | |
0b627a0b BS |
158 | ret = nouveau_pm_perflvl_aux(dev, perflvl, perflvl, pm->cur); |
159 | if (ret) | |
160 | return ret; | |
161 | ||
64f1c11a BS |
162 | pm->cur = perflvl; |
163 | return 0; | |
b0103747 MP |
164 | |
165 | error: | |
166 | /* restore the fan speed and voltage before leaving */ | |
167 | nouveau_pm_perflvl_aux(dev, perflvl, perflvl, pm->cur); | |
168 | return ret; | |
64f1c11a BS |
169 | } |
170 | ||
6f876986 BS |
171 | static int |
172 | nouveau_pm_profile_set(struct drm_device *dev, const char *profile) | |
173 | { | |
174 | struct drm_nouveau_private *dev_priv = dev->dev_private; | |
175 | struct nouveau_pm_engine *pm = &dev_priv->engine.pm; | |
176 | struct nouveau_pm_level *perflvl = NULL; | |
b0103747 MP |
177 | u64 start_time; |
178 | int ret = 0; | |
ddb20055 | 179 | long pl; |
6f876986 BS |
180 | |
181 | /* safety precaution, for now */ | |
182 | if (nouveau_perflvl_wr != 7777) | |
183 | return -EPERM; | |
184 | ||
6f876986 BS |
185 | if (!strncmp(profile, "boot", 4)) |
186 | perflvl = &pm->boot; | |
187 | else { | |
6f876986 | 188 | int i; |
ddb20055 MP |
189 | if (kstrtol(profile, 10, &pl) == -EINVAL) |
190 | return -EINVAL; | |
6f876986 BS |
191 | |
192 | for (i = 0; i < pm->nr_perflvl; i++) { | |
193 | if (pm->perflvl[i].id == pl) { | |
194 | perflvl = &pm->perflvl[i]; | |
195 | break; | |
196 | } | |
197 | } | |
198 | ||
199 | if (!perflvl) | |
200 | return -EINVAL; | |
201 | } | |
202 | ||
b0103747 MP |
203 | NV_INFO(dev, "setting performance level: %s", profile); |
204 | start_time = nv04_timer_read(dev); | |
205 | ret = nouveau_pm_perflvl_set(dev, perflvl); | |
206 | if (!ret) { | |
207 | NV_INFO(dev, "> reclocking took %lluns\n\n", | |
208 | (nv04_timer_read(dev) - start_time)); | |
209 | } else { | |
210 | NV_INFO(dev, "> reclocking failed\n\n"); | |
211 | } | |
212 | ||
213 | return ret; | |
6f876986 BS |
214 | } |
215 | ||
330c5988 BS |
216 | static int |
217 | nouveau_pm_perflvl_get(struct drm_device *dev, struct nouveau_pm_level *perflvl) | |
218 | { | |
219 | struct drm_nouveau_private *dev_priv = dev->dev_private; | |
220 | struct nouveau_pm_engine *pm = &dev_priv->engine.pm; | |
221 | int ret; | |
222 | ||
330c5988 BS |
223 | memset(perflvl, 0, sizeof(*perflvl)); |
224 | ||
ff2b6c6e BS |
225 | ret = pm->clocks_get(dev, perflvl); |
226 | if (ret) | |
227 | return ret; | |
330c5988 BS |
228 | |
229 | if (pm->voltage.supported && pm->voltage_get) { | |
230 | ret = pm->voltage_get(dev); | |
3b5565dd BS |
231 | if (ret > 0) { |
232 | perflvl->volt_min = ret; | |
233 | perflvl->volt_max = ret; | |
234 | } | |
330c5988 BS |
235 | } |
236 | ||
a175094c BS |
237 | ret = nouveau_pwmfan_get(dev); |
238 | if (ret > 0) | |
239 | perflvl->fanspeed = ret; | |
771e1035 | 240 | |
330c5988 BS |
241 | return 0; |
242 | } | |
243 | ||
244 | static void | |
245 | nouveau_pm_perflvl_info(struct nouveau_pm_level *perflvl, char *ptr, int len) | |
246 | { | |
93dccbed | 247 | char c[16], s[16], v[32], f[16], t[16], m[16]; |
0fbb114a FJ |
248 | |
249 | c[0] = '\0'; | |
250 | if (perflvl->core) | |
251 | snprintf(c, sizeof(c), " core %dMHz", perflvl->core / 1000); | |
330c5988 BS |
252 | |
253 | s[0] = '\0'; | |
254 | if (perflvl->shader) | |
255 | snprintf(s, sizeof(s), " shader %dMHz", perflvl->shader / 1000); | |
256 | ||
93dccbed BS |
257 | m[0] = '\0'; |
258 | if (perflvl->memory) | |
259 | snprintf(m, sizeof(m), " memory %dMHz", perflvl->memory / 1000); | |
260 | ||
330c5988 | 261 | v[0] = '\0'; |
3b5565dd BS |
262 | if (perflvl->volt_min && perflvl->volt_min != perflvl->volt_max) { |
263 | snprintf(v, sizeof(v), " voltage %dmV-%dmV", | |
264 | perflvl->volt_min / 1000, perflvl->volt_max / 1000); | |
265 | } else | |
266 | if (perflvl->volt_min) { | |
267 | snprintf(v, sizeof(v), " voltage %dmV", | |
268 | perflvl->volt_min / 1000); | |
269 | } | |
330c5988 BS |
270 | |
271 | f[0] = '\0'; | |
272 | if (perflvl->fanspeed) | |
273 | snprintf(f, sizeof(f), " fanspeed %d%%", perflvl->fanspeed); | |
274 | ||
e614b2e7 MP |
275 | t[0] = '\0'; |
276 | if (perflvl->timing) | |
277 | snprintf(t, sizeof(t), " timing %d", perflvl->timing->id); | |
278 | ||
93dccbed | 279 | snprintf(ptr, len, "%s%s%s%s%s%s\n", c, s, m, t, v, f); |
330c5988 BS |
280 | } |
281 | ||
282 | static ssize_t | |
283 | nouveau_pm_get_perflvl_info(struct device *d, | |
284 | struct device_attribute *a, char *buf) | |
285 | { | |
286 | struct nouveau_pm_level *perflvl = (struct nouveau_pm_level *)a; | |
287 | char *ptr = buf; | |
288 | int len = PAGE_SIZE; | |
289 | ||
93dccbed | 290 | snprintf(ptr, len, "%d:", perflvl->id); |
330c5988 BS |
291 | ptr += strlen(buf); |
292 | len -= strlen(buf); | |
293 | ||
294 | nouveau_pm_perflvl_info(perflvl, ptr, len); | |
295 | return strlen(buf); | |
296 | } | |
297 | ||
298 | static ssize_t | |
299 | nouveau_pm_get_perflvl(struct device *d, struct device_attribute *a, char *buf) | |
300 | { | |
34e9d85a | 301 | struct drm_device *dev = pci_get_drvdata(to_pci_dev(d)); |
330c5988 BS |
302 | struct drm_nouveau_private *dev_priv = dev->dev_private; |
303 | struct nouveau_pm_engine *pm = &dev_priv->engine.pm; | |
304 | struct nouveau_pm_level cur; | |
305 | int len = PAGE_SIZE, ret; | |
306 | char *ptr = buf; | |
307 | ||
308 | if (!pm->cur) | |
309 | snprintf(ptr, len, "setting: boot\n"); | |
310 | else if (pm->cur == &pm->boot) | |
93dccbed | 311 | snprintf(ptr, len, "setting: boot\nc:"); |
330c5988 | 312 | else |
93dccbed | 313 | snprintf(ptr, len, "setting: static %d\nc:", pm->cur->id); |
330c5988 BS |
314 | ptr += strlen(buf); |
315 | len -= strlen(buf); | |
316 | ||
317 | ret = nouveau_pm_perflvl_get(dev, &cur); | |
318 | if (ret == 0) | |
319 | nouveau_pm_perflvl_info(&cur, ptr, len); | |
320 | return strlen(buf); | |
321 | } | |
322 | ||
323 | static ssize_t | |
324 | nouveau_pm_set_perflvl(struct device *d, struct device_attribute *a, | |
325 | const char *buf, size_t count) | |
326 | { | |
34e9d85a | 327 | struct drm_device *dev = pci_get_drvdata(to_pci_dev(d)); |
6f876986 BS |
328 | int ret; |
329 | ||
330 | ret = nouveau_pm_profile_set(dev, buf); | |
331 | if (ret) | |
332 | return ret; | |
333 | return strlen(buf); | |
330c5988 BS |
334 | } |
335 | ||
5c4abd09 FJ |
336 | static DEVICE_ATTR(performance_level, S_IRUGO | S_IWUSR, |
337 | nouveau_pm_get_perflvl, nouveau_pm_set_perflvl); | |
330c5988 | 338 | |
34e9d85a MP |
339 | static int |
340 | nouveau_sysfs_init(struct drm_device *dev) | |
330c5988 BS |
341 | { |
342 | struct drm_nouveau_private *dev_priv = dev->dev_private; | |
343 | struct nouveau_pm_engine *pm = &dev_priv->engine.pm; | |
344 | struct device *d = &dev->pdev->dev; | |
330c5988 BS |
345 | int ret, i; |
346 | ||
330c5988 BS |
347 | ret = device_create_file(d, &dev_attr_performance_level); |
348 | if (ret) | |
349 | return ret; | |
350 | ||
351 | for (i = 0; i < pm->nr_perflvl; i++) { | |
352 | struct nouveau_pm_level *perflvl = &pm->perflvl[i]; | |
353 | ||
354 | perflvl->dev_attr.attr.name = perflvl->name; | |
355 | perflvl->dev_attr.attr.mode = S_IRUGO; | |
356 | perflvl->dev_attr.show = nouveau_pm_get_perflvl_info; | |
357 | perflvl->dev_attr.store = NULL; | |
358 | sysfs_attr_init(&perflvl->dev_attr.attr); | |
359 | ||
360 | ret = device_create_file(d, &perflvl->dev_attr); | |
361 | if (ret) { | |
362 | NV_ERROR(dev, "failed pervlvl %d sysfs: %d\n", | |
363 | perflvl->id, i); | |
364 | perflvl->dev_attr.attr.name = NULL; | |
365 | nouveau_pm_fini(dev); | |
366 | return ret; | |
367 | } | |
368 | } | |
369 | ||
370 | return 0; | |
371 | } | |
372 | ||
34e9d85a MP |
373 | static void |
374 | nouveau_sysfs_fini(struct drm_device *dev) | |
330c5988 BS |
375 | { |
376 | struct drm_nouveau_private *dev_priv = dev->dev_private; | |
377 | struct nouveau_pm_engine *pm = &dev_priv->engine.pm; | |
378 | struct device *d = &dev->pdev->dev; | |
379 | int i; | |
380 | ||
381 | device_remove_file(d, &dev_attr_performance_level); | |
382 | for (i = 0; i < pm->nr_perflvl; i++) { | |
383 | struct nouveau_pm_level *pl = &pm->perflvl[i]; | |
384 | ||
385 | if (!pl->dev_attr.attr.name) | |
386 | break; | |
387 | ||
388 | device_remove_file(d, &pl->dev_attr); | |
389 | } | |
34e9d85a MP |
390 | } |
391 | ||
658e86ee | 392 | #if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE)) |
34e9d85a MP |
393 | static ssize_t |
394 | nouveau_hwmon_show_temp(struct device *d, struct device_attribute *a, char *buf) | |
395 | { | |
396 | struct drm_device *dev = dev_get_drvdata(d); | |
8155cac4 FJ |
397 | struct drm_nouveau_private *dev_priv = dev->dev_private; |
398 | struct nouveau_pm_engine *pm = &dev_priv->engine.pm; | |
34e9d85a | 399 | |
8155cac4 | 400 | return snprintf(buf, PAGE_SIZE, "%d\n", pm->temp_get(dev)*1000); |
34e9d85a MP |
401 | } |
402 | static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, nouveau_hwmon_show_temp, | |
403 | NULL, 0); | |
404 | ||
405 | static ssize_t | |
406 | nouveau_hwmon_max_temp(struct device *d, struct device_attribute *a, char *buf) | |
407 | { | |
408 | struct drm_device *dev = dev_get_drvdata(d); | |
409 | struct drm_nouveau_private *dev_priv = dev->dev_private; | |
410 | struct nouveau_pm_engine *pm = &dev_priv->engine.pm; | |
411 | struct nouveau_pm_threshold_temp *temp = &pm->threshold_temp; | |
412 | ||
413 | return snprintf(buf, PAGE_SIZE, "%d\n", temp->down_clock*1000); | |
414 | } | |
415 | static ssize_t | |
416 | nouveau_hwmon_set_max_temp(struct device *d, struct device_attribute *a, | |
417 | const char *buf, size_t count) | |
418 | { | |
419 | struct drm_device *dev = dev_get_drvdata(d); | |
420 | struct drm_nouveau_private *dev_priv = dev->dev_private; | |
421 | struct nouveau_pm_engine *pm = &dev_priv->engine.pm; | |
422 | struct nouveau_pm_threshold_temp *temp = &pm->threshold_temp; | |
423 | long value; | |
424 | ||
ddb20055 | 425 | if (kstrtol(buf, 10, &value) == -EINVAL) |
34e9d85a MP |
426 | return count; |
427 | ||
428 | temp->down_clock = value/1000; | |
429 | ||
430 | nouveau_temp_safety_checks(dev); | |
431 | ||
432 | return count; | |
433 | } | |
434 | static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR, nouveau_hwmon_max_temp, | |
435 | nouveau_hwmon_set_max_temp, | |
436 | 0); | |
437 | ||
438 | static ssize_t | |
439 | nouveau_hwmon_critical_temp(struct device *d, struct device_attribute *a, | |
440 | char *buf) | |
441 | { | |
442 | struct drm_device *dev = dev_get_drvdata(d); | |
443 | struct drm_nouveau_private *dev_priv = dev->dev_private; | |
444 | struct nouveau_pm_engine *pm = &dev_priv->engine.pm; | |
445 | struct nouveau_pm_threshold_temp *temp = &pm->threshold_temp; | |
446 | ||
447 | return snprintf(buf, PAGE_SIZE, "%d\n", temp->critical*1000); | |
448 | } | |
449 | static ssize_t | |
450 | nouveau_hwmon_set_critical_temp(struct device *d, struct device_attribute *a, | |
451 | const char *buf, | |
452 | size_t count) | |
453 | { | |
454 | struct drm_device *dev = dev_get_drvdata(d); | |
455 | struct drm_nouveau_private *dev_priv = dev->dev_private; | |
456 | struct nouveau_pm_engine *pm = &dev_priv->engine.pm; | |
457 | struct nouveau_pm_threshold_temp *temp = &pm->threshold_temp; | |
458 | long value; | |
459 | ||
ddb20055 | 460 | if (kstrtol(buf, 10, &value) == -EINVAL) |
34e9d85a MP |
461 | return count; |
462 | ||
463 | temp->critical = value/1000; | |
464 | ||
465 | nouveau_temp_safety_checks(dev); | |
466 | ||
467 | return count; | |
468 | } | |
469 | static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO | S_IWUSR, | |
470 | nouveau_hwmon_critical_temp, | |
471 | nouveau_hwmon_set_critical_temp, | |
472 | 0); | |
473 | ||
474 | static ssize_t nouveau_hwmon_show_name(struct device *dev, | |
475 | struct device_attribute *attr, | |
476 | char *buf) | |
477 | { | |
478 | return sprintf(buf, "nouveau\n"); | |
479 | } | |
480 | static SENSOR_DEVICE_ATTR(name, S_IRUGO, nouveau_hwmon_show_name, NULL, 0); | |
481 | ||
482 | static ssize_t nouveau_hwmon_show_update_rate(struct device *dev, | |
483 | struct device_attribute *attr, | |
484 | char *buf) | |
485 | { | |
486 | return sprintf(buf, "1000\n"); | |
487 | } | |
488 | static SENSOR_DEVICE_ATTR(update_rate, S_IRUGO, | |
489 | nouveau_hwmon_show_update_rate, | |
490 | NULL, 0); | |
491 | ||
11b7d895 MP |
492 | static ssize_t |
493 | nouveau_hwmon_show_fan0_input(struct device *d, struct device_attribute *attr, | |
494 | char *buf) | |
495 | { | |
496 | struct drm_device *dev = dev_get_drvdata(d); | |
497 | struct drm_nouveau_private *dev_priv = dev->dev_private; | |
498 | struct nouveau_timer_engine *ptimer = &dev_priv->engine.timer; | |
a0b25635 | 499 | struct gpio_func gpio; |
11b7d895 MP |
500 | u32 cycles, cur, prev; |
501 | u64 start; | |
a0b25635 | 502 | int ret; |
11b7d895 | 503 | |
a0b25635 BS |
504 | ret = nouveau_gpio_find(dev, 0, DCB_GPIO_FAN_SENSE, 0xff, &gpio); |
505 | if (ret) | |
506 | return ret; | |
11b7d895 MP |
507 | |
508 | /* Monitor the GPIO input 0x3b for 250ms. | |
509 | * When the fan spins, it changes the value of GPIO FAN_SENSE. | |
510 | * We get 4 changes (0 -> 1 -> 0 -> 1 -> [...]) per complete rotation. | |
511 | */ | |
512 | start = ptimer->read(dev); | |
a0b25635 | 513 | prev = nouveau_gpio_sense(dev, 0, gpio.line); |
11b7d895 MP |
514 | cycles = 0; |
515 | do { | |
a0b25635 | 516 | cur = nouveau_gpio_sense(dev, 0, gpio.line); |
11b7d895 MP |
517 | if (prev != cur) { |
518 | cycles++; | |
519 | prev = cur; | |
520 | } | |
521 | ||
522 | usleep_range(500, 1000); /* supports 0 < rpm < 7500 */ | |
523 | } while (ptimer->read(dev) - start < 250000000); | |
524 | ||
525 | /* interpolate to get rpm */ | |
526 | return sprintf(buf, "%i\n", cycles / 4 * 4 * 60); | |
527 | } | |
528 | static SENSOR_DEVICE_ATTR(fan0_input, S_IRUGO, nouveau_hwmon_show_fan0_input, | |
529 | NULL, 0); | |
530 | ||
531 | static ssize_t | |
532 | nouveau_hwmon_get_pwm0(struct device *d, struct device_attribute *a, char *buf) | |
533 | { | |
534 | struct drm_device *dev = dev_get_drvdata(d); | |
a175094c | 535 | int ret; |
11b7d895 | 536 | |
a175094c | 537 | ret = nouveau_pwmfan_get(dev); |
11b7d895 MP |
538 | if (ret < 0) |
539 | return ret; | |
540 | ||
541 | return sprintf(buf, "%i\n", ret); | |
542 | } | |
543 | ||
544 | static ssize_t | |
545 | nouveau_hwmon_set_pwm0(struct device *d, struct device_attribute *a, | |
546 | const char *buf, size_t count) | |
547 | { | |
548 | struct drm_device *dev = dev_get_drvdata(d); | |
549 | struct drm_nouveau_private *dev_priv = dev->dev_private; | |
550 | struct nouveau_pm_engine *pm = &dev_priv->engine.pm; | |
551 | int ret = -ENODEV; | |
552 | long value; | |
553 | ||
554 | if (nouveau_perflvl_wr != 7777) | |
555 | return -EPERM; | |
556 | ||
ddb20055 | 557 | if (kstrtol(buf, 10, &value) == -EINVAL) |
11b7d895 MP |
558 | return -EINVAL; |
559 | ||
560 | if (value < pm->fan.min_duty) | |
561 | value = pm->fan.min_duty; | |
562 | if (value > pm->fan.max_duty) | |
563 | value = pm->fan.max_duty; | |
564 | ||
a175094c | 565 | ret = nouveau_pwmfan_set(dev, value); |
11b7d895 MP |
566 | if (ret) |
567 | return ret; | |
568 | ||
569 | return count; | |
570 | } | |
571 | ||
572 | static SENSOR_DEVICE_ATTR(pwm0, S_IRUGO | S_IWUSR, | |
573 | nouveau_hwmon_get_pwm0, | |
574 | nouveau_hwmon_set_pwm0, 0); | |
575 | ||
576 | static ssize_t | |
577 | nouveau_hwmon_get_pwm0_min(struct device *d, | |
578 | struct device_attribute *a, char *buf) | |
579 | { | |
580 | struct drm_device *dev = dev_get_drvdata(d); | |
581 | struct drm_nouveau_private *dev_priv = dev->dev_private; | |
582 | struct nouveau_pm_engine *pm = &dev_priv->engine.pm; | |
583 | ||
584 | return sprintf(buf, "%i\n", pm->fan.min_duty); | |
585 | } | |
586 | ||
587 | static ssize_t | |
588 | nouveau_hwmon_set_pwm0_min(struct device *d, struct device_attribute *a, | |
589 | const char *buf, size_t count) | |
590 | { | |
591 | struct drm_device *dev = dev_get_drvdata(d); | |
592 | struct drm_nouveau_private *dev_priv = dev->dev_private; | |
593 | struct nouveau_pm_engine *pm = &dev_priv->engine.pm; | |
594 | long value; | |
595 | ||
ddb20055 | 596 | if (kstrtol(buf, 10, &value) == -EINVAL) |
11b7d895 MP |
597 | return -EINVAL; |
598 | ||
599 | if (value < 0) | |
600 | value = 0; | |
601 | ||
602 | if (pm->fan.max_duty - value < 10) | |
603 | value = pm->fan.max_duty - 10; | |
604 | ||
605 | if (value < 10) | |
606 | pm->fan.min_duty = 10; | |
607 | else | |
608 | pm->fan.min_duty = value; | |
609 | ||
610 | return count; | |
611 | } | |
612 | ||
613 | static SENSOR_DEVICE_ATTR(pwm0_min, S_IRUGO | S_IWUSR, | |
614 | nouveau_hwmon_get_pwm0_min, | |
615 | nouveau_hwmon_set_pwm0_min, 0); | |
616 | ||
617 | static ssize_t | |
618 | nouveau_hwmon_get_pwm0_max(struct device *d, | |
619 | struct device_attribute *a, char *buf) | |
620 | { | |
621 | struct drm_device *dev = dev_get_drvdata(d); | |
622 | struct drm_nouveau_private *dev_priv = dev->dev_private; | |
623 | struct nouveau_pm_engine *pm = &dev_priv->engine.pm; | |
624 | ||
625 | return sprintf(buf, "%i\n", pm->fan.max_duty); | |
626 | } | |
627 | ||
628 | static ssize_t | |
629 | nouveau_hwmon_set_pwm0_max(struct device *d, struct device_attribute *a, | |
630 | const char *buf, size_t count) | |
631 | { | |
632 | struct drm_device *dev = dev_get_drvdata(d); | |
633 | struct drm_nouveau_private *dev_priv = dev->dev_private; | |
634 | struct nouveau_pm_engine *pm = &dev_priv->engine.pm; | |
635 | long value; | |
636 | ||
ddb20055 | 637 | if (kstrtol(buf, 10, &value) == -EINVAL) |
11b7d895 MP |
638 | return -EINVAL; |
639 | ||
640 | if (value < 0) | |
641 | value = 0; | |
642 | ||
643 | if (value - pm->fan.min_duty < 10) | |
644 | value = pm->fan.min_duty + 10; | |
645 | ||
646 | if (value > 100) | |
647 | pm->fan.max_duty = 100; | |
648 | else | |
649 | pm->fan.max_duty = value; | |
650 | ||
651 | return count; | |
652 | } | |
653 | ||
654 | static SENSOR_DEVICE_ATTR(pwm0_max, S_IRUGO | S_IWUSR, | |
655 | nouveau_hwmon_get_pwm0_max, | |
656 | nouveau_hwmon_set_pwm0_max, 0); | |
657 | ||
34e9d85a MP |
658 | static struct attribute *hwmon_attributes[] = { |
659 | &sensor_dev_attr_temp1_input.dev_attr.attr, | |
660 | &sensor_dev_attr_temp1_max.dev_attr.attr, | |
661 | &sensor_dev_attr_temp1_crit.dev_attr.attr, | |
662 | &sensor_dev_attr_name.dev_attr.attr, | |
663 | &sensor_dev_attr_update_rate.dev_attr.attr, | |
664 | NULL | |
665 | }; | |
11b7d895 MP |
666 | static struct attribute *hwmon_fan_rpm_attributes[] = { |
667 | &sensor_dev_attr_fan0_input.dev_attr.attr, | |
668 | NULL | |
669 | }; | |
670 | static struct attribute *hwmon_pwm_fan_attributes[] = { | |
671 | &sensor_dev_attr_pwm0.dev_attr.attr, | |
672 | &sensor_dev_attr_pwm0_min.dev_attr.attr, | |
673 | &sensor_dev_attr_pwm0_max.dev_attr.attr, | |
674 | NULL | |
675 | }; | |
34e9d85a MP |
676 | |
677 | static const struct attribute_group hwmon_attrgroup = { | |
678 | .attrs = hwmon_attributes, | |
679 | }; | |
11b7d895 MP |
680 | static const struct attribute_group hwmon_fan_rpm_attrgroup = { |
681 | .attrs = hwmon_fan_rpm_attributes, | |
682 | }; | |
683 | static const struct attribute_group hwmon_pwm_fan_attrgroup = { | |
684 | .attrs = hwmon_pwm_fan_attributes, | |
685 | }; | |
b54262f3 | 686 | #endif |
34e9d85a MP |
687 | |
688 | static int | |
689 | nouveau_hwmon_init(struct drm_device *dev) | |
690 | { | |
691 | struct drm_nouveau_private *dev_priv = dev->dev_private; | |
8155cac4 | 692 | struct nouveau_pm_engine *pm = &dev_priv->engine.pm; |
095f979a | 693 | #if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE)) |
34e9d85a | 694 | struct device *hwmon_dev; |
11b7d895 | 695 | int ret = 0; |
34e9d85a | 696 | |
8155cac4 FJ |
697 | if (!pm->temp_get) |
698 | return -ENODEV; | |
34e9d85a MP |
699 | |
700 | hwmon_dev = hwmon_device_register(&dev->pdev->dev); | |
701 | if (IS_ERR(hwmon_dev)) { | |
702 | ret = PTR_ERR(hwmon_dev); | |
703 | NV_ERROR(dev, | |
704 | "Unable to register hwmon device: %d\n", ret); | |
705 | return ret; | |
706 | } | |
707 | dev_set_drvdata(hwmon_dev, dev); | |
11b7d895 MP |
708 | |
709 | /* default sysfs entries */ | |
07cfe0e7 | 710 | ret = sysfs_create_group(&dev->pdev->dev.kobj, &hwmon_attrgroup); |
34e9d85a | 711 | if (ret) { |
11b7d895 MP |
712 | if (ret) |
713 | goto error; | |
714 | } | |
715 | ||
716 | /* if the card has a pwm fan */ | |
717 | /*XXX: incorrect, need better detection for this, some boards have | |
718 | * the gpio entries for pwm fan control even when there's no | |
719 | * actual fan connected to it... therm table? */ | |
a175094c | 720 | if (nouveau_pwmfan_get(dev) >= 0) { |
11b7d895 MP |
721 | ret = sysfs_create_group(&dev->pdev->dev.kobj, |
722 | &hwmon_pwm_fan_attrgroup); | |
723 | if (ret) | |
724 | goto error; | |
725 | } | |
726 | ||
727 | /* if the card can read the fan rpm */ | |
a0b25635 | 728 | if (nouveau_gpio_func_valid(dev, DCB_GPIO_FAN_SENSE)) { |
11b7d895 MP |
729 | ret = sysfs_create_group(&dev->pdev->dev.kobj, |
730 | &hwmon_fan_rpm_attrgroup); | |
731 | if (ret) | |
732 | goto error; | |
34e9d85a MP |
733 | } |
734 | ||
8155cac4 | 735 | pm->hwmon = hwmon_dev; |
11b7d895 | 736 | |
34e9d85a | 737 | return 0; |
11b7d895 MP |
738 | |
739 | error: | |
740 | NV_ERROR(dev, "Unable to create some hwmon sysfs files: %d\n", ret); | |
741 | hwmon_device_unregister(hwmon_dev); | |
742 | pm->hwmon = NULL; | |
743 | return ret; | |
744 | #else | |
745 | pm->hwmon = NULL; | |
746 | return 0; | |
747 | #endif | |
34e9d85a MP |
748 | } |
749 | ||
750 | static void | |
751 | nouveau_hwmon_fini(struct drm_device *dev) | |
752 | { | |
658e86ee | 753 | #if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE)) |
34e9d85a | 754 | struct drm_nouveau_private *dev_priv = dev->dev_private; |
8155cac4 | 755 | struct nouveau_pm_engine *pm = &dev_priv->engine.pm; |
34e9d85a | 756 | |
8155cac4 | 757 | if (pm->hwmon) { |
8c06a3e0 | 758 | sysfs_remove_group(&dev->pdev->dev.kobj, &hwmon_attrgroup); |
ddb20055 MP |
759 | sysfs_remove_group(&dev->pdev->dev.kobj, |
760 | &hwmon_pwm_fan_attrgroup); | |
761 | sysfs_remove_group(&dev->pdev->dev.kobj, | |
762 | &hwmon_fan_rpm_attrgroup); | |
11b7d895 | 763 | |
8155cac4 | 764 | hwmon_device_unregister(pm->hwmon); |
34e9d85a | 765 | } |
b54262f3 | 766 | #endif |
34e9d85a MP |
767 | } |
768 | ||
1f962797 | 769 | #if defined(CONFIG_ACPI) && defined(CONFIG_POWER_SUPPLY) |
6032649d BS |
770 | static int |
771 | nouveau_pm_acpi_event(struct notifier_block *nb, unsigned long val, void *data) | |
772 | { | |
773 | struct drm_nouveau_private *dev_priv = | |
774 | container_of(nb, struct drm_nouveau_private, engine.pm.acpi_nb); | |
775 | struct drm_device *dev = dev_priv->dev; | |
776 | struct acpi_bus_event *entry = (struct acpi_bus_event *)data; | |
777 | ||
778 | if (strcmp(entry->device_class, "ac_adapter") == 0) { | |
779 | bool ac = power_supply_is_system_supplied(); | |
780 | ||
781 | NV_DEBUG(dev, "power supply changed: %s\n", ac ? "AC" : "DC"); | |
782 | } | |
783 | ||
784 | return NOTIFY_OK; | |
785 | } | |
786 | #endif | |
787 | ||
34e9d85a MP |
788 | int |
789 | nouveau_pm_init(struct drm_device *dev) | |
790 | { | |
791 | struct drm_nouveau_private *dev_priv = dev->dev_private; | |
792 | struct nouveau_pm_engine *pm = &dev_priv->engine.pm; | |
793 | char info[256]; | |
794 | int ret, i; | |
795 | ||
e614b2e7 | 796 | nouveau_mem_timing_init(dev); |
34e9d85a MP |
797 | nouveau_volt_init(dev); |
798 | nouveau_perf_init(dev); | |
799 | nouveau_temp_init(dev); | |
800 | ||
801 | NV_INFO(dev, "%d available performance level(s)\n", pm->nr_perflvl); | |
802 | for (i = 0; i < pm->nr_perflvl; i++) { | |
803 | nouveau_pm_perflvl_info(&pm->perflvl[i], info, sizeof(info)); | |
93dccbed | 804 | NV_INFO(dev, "%d:%s", pm->perflvl[i].id, info); |
34e9d85a MP |
805 | } |
806 | ||
807 | /* determine current ("boot") performance level */ | |
808 | ret = nouveau_pm_perflvl_get(dev, &pm->boot); | |
809 | if (ret == 0) { | |
01e542c6 | 810 | strncpy(pm->boot.name, "boot", 4); |
c7c039fd | 811 | pm->boot.timing = &pm->memtimings.boot; |
34e9d85a MP |
812 | pm->cur = &pm->boot; |
813 | ||
814 | nouveau_pm_perflvl_info(&pm->boot, info, sizeof(info)); | |
93dccbed | 815 | NV_INFO(dev, "c:%s", info); |
34e9d85a MP |
816 | } |
817 | ||
818 | /* switch performance levels now if requested */ | |
819 | if (nouveau_perflvl != NULL) { | |
820 | ret = nouveau_pm_profile_set(dev, nouveau_perflvl); | |
821 | if (ret) { | |
822 | NV_ERROR(dev, "error setting perflvl \"%s\": %d\n", | |
823 | nouveau_perflvl, ret); | |
824 | } | |
825 | } | |
826 | ||
bc6389e4 MP |
827 | /* determine the current fan speed */ |
828 | pm->fan.percent = nouveau_pwmfan_get(dev); | |
829 | ||
34e9d85a MP |
830 | nouveau_sysfs_init(dev); |
831 | nouveau_hwmon_init(dev); | |
1f962797 | 832 | #if defined(CONFIG_ACPI) && defined(CONFIG_POWER_SUPPLY) |
6032649d BS |
833 | pm->acpi_nb.notifier_call = nouveau_pm_acpi_event; |
834 | register_acpi_notifier(&pm->acpi_nb); | |
835 | #endif | |
34e9d85a MP |
836 | |
837 | return 0; | |
838 | } | |
839 | ||
840 | void | |
841 | nouveau_pm_fini(struct drm_device *dev) | |
842 | { | |
843 | struct drm_nouveau_private *dev_priv = dev->dev_private; | |
844 | struct nouveau_pm_engine *pm = &dev_priv->engine.pm; | |
845 | ||
846 | if (pm->cur != &pm->boot) | |
847 | nouveau_pm_perflvl_set(dev, &pm->boot); | |
330c5988 | 848 | |
7760fcb0 | 849 | nouveau_temp_fini(dev); |
330c5988 BS |
850 | nouveau_perf_fini(dev); |
851 | nouveau_volt_fini(dev); | |
e614b2e7 | 852 | nouveau_mem_timing_fini(dev); |
34e9d85a | 853 | |
1f962797 | 854 | #if defined(CONFIG_ACPI) && defined(CONFIG_POWER_SUPPLY) |
6032649d BS |
855 | unregister_acpi_notifier(&pm->acpi_nb); |
856 | #endif | |
34e9d85a MP |
857 | nouveau_hwmon_fini(dev); |
858 | nouveau_sysfs_fini(dev); | |
330c5988 BS |
859 | } |
860 | ||
64f1c11a BS |
861 | void |
862 | nouveau_pm_resume(struct drm_device *dev) | |
863 | { | |
864 | struct drm_nouveau_private *dev_priv = dev->dev_private; | |
865 | struct nouveau_pm_engine *pm = &dev_priv->engine.pm; | |
866 | struct nouveau_pm_level *perflvl; | |
867 | ||
317495b2 | 868 | if (!pm->cur || pm->cur == &pm->boot) |
64f1c11a BS |
869 | return; |
870 | ||
871 | perflvl = pm->cur; | |
872 | pm->cur = &pm->boot; | |
873 | nouveau_pm_perflvl_set(dev, perflvl); | |
bc6389e4 | 874 | nouveau_pwmfan_set(dev, pm->fan.percent); |
64f1c11a | 875 | } |