]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/gpu/drm/nouveau/nouveau_pm.c
drm/nouveau/pm: fix dll off -> dll on transitions
[mirror_ubuntu-bionic-kernel.git] / drivers / gpu / drm / nouveau / nouveau_pm.c
CommitLineData
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
38static int
39nouveau_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
66static int
67nouveau_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 101static int
0b627a0b
BS
102nouveau_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
134static int
135nouveau_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
165error:
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
171static int
172nouveau_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
216static int
217nouveau_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
fd99fd61 241 nouveau_mem_timing_read(dev, &perflvl->timing);
330c5988
BS
242 return 0;
243}
244
245static void
246nouveau_pm_perflvl_info(struct nouveau_pm_level *perflvl, char *ptr, int len)
247{
085028ce 248 char c[16], s[16], v[32], f[16], m[16];
0fbb114a
FJ
249
250 c[0] = '\0';
251 if (perflvl->core)
252 snprintf(c, sizeof(c), " core %dMHz", perflvl->core / 1000);
330c5988
BS
253
254 s[0] = '\0';
255 if (perflvl->shader)
256 snprintf(s, sizeof(s), " shader %dMHz", perflvl->shader / 1000);
257
93dccbed
BS
258 m[0] = '\0';
259 if (perflvl->memory)
260 snprintf(m, sizeof(m), " memory %dMHz", perflvl->memory / 1000);
261
330c5988 262 v[0] = '\0';
3b5565dd
BS
263 if (perflvl->volt_min && perflvl->volt_min != perflvl->volt_max) {
264 snprintf(v, sizeof(v), " voltage %dmV-%dmV",
265 perflvl->volt_min / 1000, perflvl->volt_max / 1000);
266 } else
267 if (perflvl->volt_min) {
268 snprintf(v, sizeof(v), " voltage %dmV",
269 perflvl->volt_min / 1000);
270 }
330c5988
BS
271
272 f[0] = '\0';
273 if (perflvl->fanspeed)
274 snprintf(f, sizeof(f), " fanspeed %d%%", perflvl->fanspeed);
275
085028ce 276 snprintf(ptr, len, "%s%s%s%s%s\n", c, s, m, v, f);
330c5988
BS
277}
278
279static ssize_t
280nouveau_pm_get_perflvl_info(struct device *d,
281 struct device_attribute *a, char *buf)
282{
283 struct nouveau_pm_level *perflvl = (struct nouveau_pm_level *)a;
284 char *ptr = buf;
285 int len = PAGE_SIZE;
286
93dccbed 287 snprintf(ptr, len, "%d:", perflvl->id);
330c5988
BS
288 ptr += strlen(buf);
289 len -= strlen(buf);
290
291 nouveau_pm_perflvl_info(perflvl, ptr, len);
292 return strlen(buf);
293}
294
295static ssize_t
296nouveau_pm_get_perflvl(struct device *d, struct device_attribute *a, char *buf)
297{
34e9d85a 298 struct drm_device *dev = pci_get_drvdata(to_pci_dev(d));
330c5988
BS
299 struct drm_nouveau_private *dev_priv = dev->dev_private;
300 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
301 struct nouveau_pm_level cur;
302 int len = PAGE_SIZE, ret;
303 char *ptr = buf;
304
305 if (!pm->cur)
306 snprintf(ptr, len, "setting: boot\n");
307 else if (pm->cur == &pm->boot)
93dccbed 308 snprintf(ptr, len, "setting: boot\nc:");
330c5988 309 else
93dccbed 310 snprintf(ptr, len, "setting: static %d\nc:", pm->cur->id);
330c5988
BS
311 ptr += strlen(buf);
312 len -= strlen(buf);
313
314 ret = nouveau_pm_perflvl_get(dev, &cur);
315 if (ret == 0)
316 nouveau_pm_perflvl_info(&cur, ptr, len);
317 return strlen(buf);
318}
319
320static ssize_t
321nouveau_pm_set_perflvl(struct device *d, struct device_attribute *a,
322 const char *buf, size_t count)
323{
34e9d85a 324 struct drm_device *dev = pci_get_drvdata(to_pci_dev(d));
6f876986
BS
325 int ret;
326
327 ret = nouveau_pm_profile_set(dev, buf);
328 if (ret)
329 return ret;
330 return strlen(buf);
330c5988
BS
331}
332
5c4abd09
FJ
333static DEVICE_ATTR(performance_level, S_IRUGO | S_IWUSR,
334 nouveau_pm_get_perflvl, nouveau_pm_set_perflvl);
330c5988 335
34e9d85a
MP
336static int
337nouveau_sysfs_init(struct drm_device *dev)
330c5988
BS
338{
339 struct drm_nouveau_private *dev_priv = dev->dev_private;
340 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
341 struct device *d = &dev->pdev->dev;
330c5988
BS
342 int ret, i;
343
330c5988
BS
344 ret = device_create_file(d, &dev_attr_performance_level);
345 if (ret)
346 return ret;
347
348 for (i = 0; i < pm->nr_perflvl; i++) {
349 struct nouveau_pm_level *perflvl = &pm->perflvl[i];
350
351 perflvl->dev_attr.attr.name = perflvl->name;
352 perflvl->dev_attr.attr.mode = S_IRUGO;
353 perflvl->dev_attr.show = nouveau_pm_get_perflvl_info;
354 perflvl->dev_attr.store = NULL;
355 sysfs_attr_init(&perflvl->dev_attr.attr);
356
357 ret = device_create_file(d, &perflvl->dev_attr);
358 if (ret) {
359 NV_ERROR(dev, "failed pervlvl %d sysfs: %d\n",
360 perflvl->id, i);
361 perflvl->dev_attr.attr.name = NULL;
362 nouveau_pm_fini(dev);
363 return ret;
364 }
365 }
366
367 return 0;
368}
369
34e9d85a
MP
370static void
371nouveau_sysfs_fini(struct drm_device *dev)
330c5988
BS
372{
373 struct drm_nouveau_private *dev_priv = dev->dev_private;
374 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
375 struct device *d = &dev->pdev->dev;
376 int i;
377
378 device_remove_file(d, &dev_attr_performance_level);
379 for (i = 0; i < pm->nr_perflvl; i++) {
380 struct nouveau_pm_level *pl = &pm->perflvl[i];
381
382 if (!pl->dev_attr.attr.name)
383 break;
384
385 device_remove_file(d, &pl->dev_attr);
386 }
34e9d85a
MP
387}
388
658e86ee 389#if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
34e9d85a
MP
390static ssize_t
391nouveau_hwmon_show_temp(struct device *d, struct device_attribute *a, char *buf)
392{
393 struct drm_device *dev = dev_get_drvdata(d);
8155cac4
FJ
394 struct drm_nouveau_private *dev_priv = dev->dev_private;
395 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
34e9d85a 396
8155cac4 397 return snprintf(buf, PAGE_SIZE, "%d\n", pm->temp_get(dev)*1000);
34e9d85a
MP
398}
399static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, nouveau_hwmon_show_temp,
400 NULL, 0);
401
402static ssize_t
403nouveau_hwmon_max_temp(struct device *d, struct device_attribute *a, char *buf)
404{
405 struct drm_device *dev = dev_get_drvdata(d);
406 struct drm_nouveau_private *dev_priv = dev->dev_private;
407 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
408 struct nouveau_pm_threshold_temp *temp = &pm->threshold_temp;
409
410 return snprintf(buf, PAGE_SIZE, "%d\n", temp->down_clock*1000);
411}
412static ssize_t
413nouveau_hwmon_set_max_temp(struct device *d, struct device_attribute *a,
414 const char *buf, size_t count)
415{
416 struct drm_device *dev = dev_get_drvdata(d);
417 struct drm_nouveau_private *dev_priv = dev->dev_private;
418 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
419 struct nouveau_pm_threshold_temp *temp = &pm->threshold_temp;
420 long value;
421
ddb20055 422 if (kstrtol(buf, 10, &value) == -EINVAL)
34e9d85a
MP
423 return count;
424
425 temp->down_clock = value/1000;
426
427 nouveau_temp_safety_checks(dev);
428
429 return count;
430}
431static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR, nouveau_hwmon_max_temp,
432 nouveau_hwmon_set_max_temp,
433 0);
434
435static ssize_t
436nouveau_hwmon_critical_temp(struct device *d, struct device_attribute *a,
437 char *buf)
438{
439 struct drm_device *dev = dev_get_drvdata(d);
440 struct drm_nouveau_private *dev_priv = dev->dev_private;
441 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
442 struct nouveau_pm_threshold_temp *temp = &pm->threshold_temp;
443
444 return snprintf(buf, PAGE_SIZE, "%d\n", temp->critical*1000);
445}
446static ssize_t
447nouveau_hwmon_set_critical_temp(struct device *d, struct device_attribute *a,
448 const char *buf,
449 size_t count)
450{
451 struct drm_device *dev = dev_get_drvdata(d);
452 struct drm_nouveau_private *dev_priv = dev->dev_private;
453 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
454 struct nouveau_pm_threshold_temp *temp = &pm->threshold_temp;
455 long value;
456
ddb20055 457 if (kstrtol(buf, 10, &value) == -EINVAL)
34e9d85a
MP
458 return count;
459
460 temp->critical = value/1000;
461
462 nouveau_temp_safety_checks(dev);
463
464 return count;
465}
466static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO | S_IWUSR,
467 nouveau_hwmon_critical_temp,
468 nouveau_hwmon_set_critical_temp,
469 0);
470
471static ssize_t nouveau_hwmon_show_name(struct device *dev,
472 struct device_attribute *attr,
473 char *buf)
474{
475 return sprintf(buf, "nouveau\n");
476}
477static SENSOR_DEVICE_ATTR(name, S_IRUGO, nouveau_hwmon_show_name, NULL, 0);
478
479static ssize_t nouveau_hwmon_show_update_rate(struct device *dev,
480 struct device_attribute *attr,
481 char *buf)
482{
483 return sprintf(buf, "1000\n");
484}
485static SENSOR_DEVICE_ATTR(update_rate, S_IRUGO,
486 nouveau_hwmon_show_update_rate,
487 NULL, 0);
488
11b7d895
MP
489static ssize_t
490nouveau_hwmon_show_fan0_input(struct device *d, struct device_attribute *attr,
491 char *buf)
492{
493 struct drm_device *dev = dev_get_drvdata(d);
494 struct drm_nouveau_private *dev_priv = dev->dev_private;
495 struct nouveau_timer_engine *ptimer = &dev_priv->engine.timer;
a0b25635 496 struct gpio_func gpio;
11b7d895
MP
497 u32 cycles, cur, prev;
498 u64 start;
a0b25635 499 int ret;
11b7d895 500
a0b25635
BS
501 ret = nouveau_gpio_find(dev, 0, DCB_GPIO_FAN_SENSE, 0xff, &gpio);
502 if (ret)
503 return ret;
11b7d895
MP
504
505 /* Monitor the GPIO input 0x3b for 250ms.
506 * When the fan spins, it changes the value of GPIO FAN_SENSE.
507 * We get 4 changes (0 -> 1 -> 0 -> 1 -> [...]) per complete rotation.
508 */
509 start = ptimer->read(dev);
a0b25635 510 prev = nouveau_gpio_sense(dev, 0, gpio.line);
11b7d895
MP
511 cycles = 0;
512 do {
a0b25635 513 cur = nouveau_gpio_sense(dev, 0, gpio.line);
11b7d895
MP
514 if (prev != cur) {
515 cycles++;
516 prev = cur;
517 }
518
519 usleep_range(500, 1000); /* supports 0 < rpm < 7500 */
520 } while (ptimer->read(dev) - start < 250000000);
521
522 /* interpolate to get rpm */
523 return sprintf(buf, "%i\n", cycles / 4 * 4 * 60);
524}
525static SENSOR_DEVICE_ATTR(fan0_input, S_IRUGO, nouveau_hwmon_show_fan0_input,
526 NULL, 0);
527
528static ssize_t
529nouveau_hwmon_get_pwm0(struct device *d, struct device_attribute *a, char *buf)
530{
531 struct drm_device *dev = dev_get_drvdata(d);
a175094c 532 int ret;
11b7d895 533
a175094c 534 ret = nouveau_pwmfan_get(dev);
11b7d895
MP
535 if (ret < 0)
536 return ret;
537
538 return sprintf(buf, "%i\n", ret);
539}
540
541static ssize_t
542nouveau_hwmon_set_pwm0(struct device *d, struct device_attribute *a,
543 const char *buf, size_t count)
544{
545 struct drm_device *dev = dev_get_drvdata(d);
546 struct drm_nouveau_private *dev_priv = dev->dev_private;
547 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
548 int ret = -ENODEV;
549 long value;
550
551 if (nouveau_perflvl_wr != 7777)
552 return -EPERM;
553
ddb20055 554 if (kstrtol(buf, 10, &value) == -EINVAL)
11b7d895
MP
555 return -EINVAL;
556
557 if (value < pm->fan.min_duty)
558 value = pm->fan.min_duty;
559 if (value > pm->fan.max_duty)
560 value = pm->fan.max_duty;
561
a175094c 562 ret = nouveau_pwmfan_set(dev, value);
11b7d895
MP
563 if (ret)
564 return ret;
565
566 return count;
567}
568
569static SENSOR_DEVICE_ATTR(pwm0, S_IRUGO | S_IWUSR,
570 nouveau_hwmon_get_pwm0,
571 nouveau_hwmon_set_pwm0, 0);
572
573static ssize_t
574nouveau_hwmon_get_pwm0_min(struct device *d,
575 struct device_attribute *a, char *buf)
576{
577 struct drm_device *dev = dev_get_drvdata(d);
578 struct drm_nouveau_private *dev_priv = dev->dev_private;
579 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
580
581 return sprintf(buf, "%i\n", pm->fan.min_duty);
582}
583
584static ssize_t
585nouveau_hwmon_set_pwm0_min(struct device *d, struct device_attribute *a,
586 const char *buf, size_t count)
587{
588 struct drm_device *dev = dev_get_drvdata(d);
589 struct drm_nouveau_private *dev_priv = dev->dev_private;
590 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
591 long value;
592
ddb20055 593 if (kstrtol(buf, 10, &value) == -EINVAL)
11b7d895
MP
594 return -EINVAL;
595
596 if (value < 0)
597 value = 0;
598
599 if (pm->fan.max_duty - value < 10)
600 value = pm->fan.max_duty - 10;
601
602 if (value < 10)
603 pm->fan.min_duty = 10;
604 else
605 pm->fan.min_duty = value;
606
607 return count;
608}
609
610static SENSOR_DEVICE_ATTR(pwm0_min, S_IRUGO | S_IWUSR,
611 nouveau_hwmon_get_pwm0_min,
612 nouveau_hwmon_set_pwm0_min, 0);
613
614static ssize_t
615nouveau_hwmon_get_pwm0_max(struct device *d,
616 struct device_attribute *a, char *buf)
617{
618 struct drm_device *dev = dev_get_drvdata(d);
619 struct drm_nouveau_private *dev_priv = dev->dev_private;
620 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
621
622 return sprintf(buf, "%i\n", pm->fan.max_duty);
623}
624
625static ssize_t
626nouveau_hwmon_set_pwm0_max(struct device *d, struct device_attribute *a,
627 const char *buf, size_t count)
628{
629 struct drm_device *dev = dev_get_drvdata(d);
630 struct drm_nouveau_private *dev_priv = dev->dev_private;
631 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
632 long value;
633
ddb20055 634 if (kstrtol(buf, 10, &value) == -EINVAL)
11b7d895
MP
635 return -EINVAL;
636
637 if (value < 0)
638 value = 0;
639
640 if (value - pm->fan.min_duty < 10)
641 value = pm->fan.min_duty + 10;
642
643 if (value > 100)
644 pm->fan.max_duty = 100;
645 else
646 pm->fan.max_duty = value;
647
648 return count;
649}
650
651static SENSOR_DEVICE_ATTR(pwm0_max, S_IRUGO | S_IWUSR,
652 nouveau_hwmon_get_pwm0_max,
653 nouveau_hwmon_set_pwm0_max, 0);
654
34e9d85a
MP
655static struct attribute *hwmon_attributes[] = {
656 &sensor_dev_attr_temp1_input.dev_attr.attr,
657 &sensor_dev_attr_temp1_max.dev_attr.attr,
658 &sensor_dev_attr_temp1_crit.dev_attr.attr,
659 &sensor_dev_attr_name.dev_attr.attr,
660 &sensor_dev_attr_update_rate.dev_attr.attr,
661 NULL
662};
11b7d895
MP
663static struct attribute *hwmon_fan_rpm_attributes[] = {
664 &sensor_dev_attr_fan0_input.dev_attr.attr,
665 NULL
666};
667static struct attribute *hwmon_pwm_fan_attributes[] = {
668 &sensor_dev_attr_pwm0.dev_attr.attr,
669 &sensor_dev_attr_pwm0_min.dev_attr.attr,
670 &sensor_dev_attr_pwm0_max.dev_attr.attr,
671 NULL
672};
34e9d85a
MP
673
674static const struct attribute_group hwmon_attrgroup = {
675 .attrs = hwmon_attributes,
676};
11b7d895
MP
677static const struct attribute_group hwmon_fan_rpm_attrgroup = {
678 .attrs = hwmon_fan_rpm_attributes,
679};
680static const struct attribute_group hwmon_pwm_fan_attrgroup = {
681 .attrs = hwmon_pwm_fan_attributes,
682};
b54262f3 683#endif
34e9d85a
MP
684
685static int
686nouveau_hwmon_init(struct drm_device *dev)
687{
688 struct drm_nouveau_private *dev_priv = dev->dev_private;
8155cac4 689 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
095f979a 690#if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
34e9d85a 691 struct device *hwmon_dev;
11b7d895 692 int ret = 0;
34e9d85a 693
8155cac4
FJ
694 if (!pm->temp_get)
695 return -ENODEV;
34e9d85a
MP
696
697 hwmon_dev = hwmon_device_register(&dev->pdev->dev);
698 if (IS_ERR(hwmon_dev)) {
699 ret = PTR_ERR(hwmon_dev);
700 NV_ERROR(dev,
701 "Unable to register hwmon device: %d\n", ret);
702 return ret;
703 }
704 dev_set_drvdata(hwmon_dev, dev);
11b7d895
MP
705
706 /* default sysfs entries */
07cfe0e7 707 ret = sysfs_create_group(&dev->pdev->dev.kobj, &hwmon_attrgroup);
34e9d85a 708 if (ret) {
11b7d895
MP
709 if (ret)
710 goto error;
711 }
712
713 /* if the card has a pwm fan */
714 /*XXX: incorrect, need better detection for this, some boards have
715 * the gpio entries for pwm fan control even when there's no
716 * actual fan connected to it... therm table? */
a175094c 717 if (nouveau_pwmfan_get(dev) >= 0) {
11b7d895
MP
718 ret = sysfs_create_group(&dev->pdev->dev.kobj,
719 &hwmon_pwm_fan_attrgroup);
720 if (ret)
721 goto error;
722 }
723
724 /* if the card can read the fan rpm */
a0b25635 725 if (nouveau_gpio_func_valid(dev, DCB_GPIO_FAN_SENSE)) {
11b7d895
MP
726 ret = sysfs_create_group(&dev->pdev->dev.kobj,
727 &hwmon_fan_rpm_attrgroup);
728 if (ret)
729 goto error;
34e9d85a
MP
730 }
731
8155cac4 732 pm->hwmon = hwmon_dev;
11b7d895 733
34e9d85a 734 return 0;
11b7d895
MP
735
736error:
737 NV_ERROR(dev, "Unable to create some hwmon sysfs files: %d\n", ret);
738 hwmon_device_unregister(hwmon_dev);
739 pm->hwmon = NULL;
740 return ret;
741#else
742 pm->hwmon = NULL;
743 return 0;
744#endif
34e9d85a
MP
745}
746
747static void
748nouveau_hwmon_fini(struct drm_device *dev)
749{
658e86ee 750#if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
34e9d85a 751 struct drm_nouveau_private *dev_priv = dev->dev_private;
8155cac4 752 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
34e9d85a 753
8155cac4 754 if (pm->hwmon) {
8c06a3e0 755 sysfs_remove_group(&dev->pdev->dev.kobj, &hwmon_attrgroup);
ddb20055
MP
756 sysfs_remove_group(&dev->pdev->dev.kobj,
757 &hwmon_pwm_fan_attrgroup);
758 sysfs_remove_group(&dev->pdev->dev.kobj,
759 &hwmon_fan_rpm_attrgroup);
11b7d895 760
8155cac4 761 hwmon_device_unregister(pm->hwmon);
34e9d85a 762 }
b54262f3 763#endif
34e9d85a
MP
764}
765
1f962797 766#if defined(CONFIG_ACPI) && defined(CONFIG_POWER_SUPPLY)
6032649d
BS
767static int
768nouveau_pm_acpi_event(struct notifier_block *nb, unsigned long val, void *data)
769{
770 struct drm_nouveau_private *dev_priv =
771 container_of(nb, struct drm_nouveau_private, engine.pm.acpi_nb);
772 struct drm_device *dev = dev_priv->dev;
773 struct acpi_bus_event *entry = (struct acpi_bus_event *)data;
774
775 if (strcmp(entry->device_class, "ac_adapter") == 0) {
776 bool ac = power_supply_is_system_supplied();
777
778 NV_DEBUG(dev, "power supply changed: %s\n", ac ? "AC" : "DC");
779 }
780
781 return NOTIFY_OK;
782}
783#endif
784
34e9d85a
MP
785int
786nouveau_pm_init(struct drm_device *dev)
787{
788 struct drm_nouveau_private *dev_priv = dev->dev_private;
789 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
790 char info[256];
791 int ret, i;
792
68a64cad 793 /* parse aux tables from vbios */
34e9d85a 794 nouveau_volt_init(dev);
34e9d85a
MP
795 nouveau_temp_init(dev);
796
68a64cad
BS
797 /* determine current ("boot") performance level */
798 ret = nouveau_pm_perflvl_get(dev, &pm->boot);
799 if (ret) {
800 NV_ERROR(dev, "failed to determine boot perflvl\n");
801 return ret;
802 }
803
804 strncpy(pm->boot.name, "boot", 4);
68a64cad
BS
805 pm->cur = &pm->boot;
806
807 /* add performance levels from vbios */
808 nouveau_perf_init(dev);
809
810 /* display available performance levels */
34e9d85a
MP
811 NV_INFO(dev, "%d available performance level(s)\n", pm->nr_perflvl);
812 for (i = 0; i < pm->nr_perflvl; i++) {
813 nouveau_pm_perflvl_info(&pm->perflvl[i], info, sizeof(info));
93dccbed 814 NV_INFO(dev, "%d:%s", pm->perflvl[i].id, info);
34e9d85a
MP
815 }
816
68a64cad
BS
817 nouveau_pm_perflvl_info(&pm->boot, info, sizeof(info));
818 NV_INFO(dev, "c:%s", info);
34e9d85a
MP
819
820 /* switch performance levels now if requested */
821 if (nouveau_perflvl != NULL) {
822 ret = nouveau_pm_profile_set(dev, nouveau_perflvl);
823 if (ret) {
824 NV_ERROR(dev, "error setting perflvl \"%s\": %d\n",
825 nouveau_perflvl, ret);
826 }
827 }
828
bc6389e4
MP
829 /* determine the current fan speed */
830 pm->fan.percent = nouveau_pwmfan_get(dev);
831
34e9d85a
MP
832 nouveau_sysfs_init(dev);
833 nouveau_hwmon_init(dev);
1f962797 834#if defined(CONFIG_ACPI) && defined(CONFIG_POWER_SUPPLY)
6032649d
BS
835 pm->acpi_nb.notifier_call = nouveau_pm_acpi_event;
836 register_acpi_notifier(&pm->acpi_nb);
837#endif
34e9d85a
MP
838
839 return 0;
840}
841
842void
843nouveau_pm_fini(struct drm_device *dev)
844{
845 struct drm_nouveau_private *dev_priv = dev->dev_private;
846 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
847
848 if (pm->cur != &pm->boot)
849 nouveau_pm_perflvl_set(dev, &pm->boot);
330c5988 850
7760fcb0 851 nouveau_temp_fini(dev);
330c5988
BS
852 nouveau_perf_fini(dev);
853 nouveau_volt_fini(dev);
34e9d85a 854
1f962797 855#if defined(CONFIG_ACPI) && defined(CONFIG_POWER_SUPPLY)
6032649d
BS
856 unregister_acpi_notifier(&pm->acpi_nb);
857#endif
34e9d85a
MP
858 nouveau_hwmon_fini(dev);
859 nouveau_sysfs_fini(dev);
330c5988
BS
860}
861
64f1c11a
BS
862void
863nouveau_pm_resume(struct drm_device *dev)
864{
865 struct drm_nouveau_private *dev_priv = dev->dev_private;
866 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
867 struct nouveau_pm_level *perflvl;
868
317495b2 869 if (!pm->cur || pm->cur == &pm->boot)
64f1c11a
BS
870 return;
871
872 perflvl = pm->cur;
873 pm->cur = &pm->boot;
874 nouveau_pm_perflvl_set(dev, perflvl);
bc6389e4 875 nouveau_pwmfan_set(dev, pm->fan.percent);
64f1c11a 876}