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