drm/nouveau/pm: extend profile interface for destroy/init/fini
[pandora-kernel.git] / drivers / gpu / drm / nouveau / nouveau_pm.c
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"
29 #include "nouveau_gpio.h"
30
31 #ifdef CONFIG_ACPI
32 #include <linux/acpi.h>
33 #endif
34 #include <linux/power_supply.h>
35 #include <linux/hwmon.h>
36 #include <linux/hwmon-sysfs.h>
37
38 static int
39 nouveau_pwmfan_get(struct drm_device *dev)
40 {
41         struct drm_nouveau_private *dev_priv = dev->dev_private;
42         struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
43         struct gpio_func gpio;
44         u32 divs, duty;
45         int ret;
46
47         if (!pm->pwm_get)
48                 return -ENODEV;
49
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);
53                 if (ret == 0 && divs) {
54                         divs = max(divs, duty);
55                         if (dev_priv->card_type <= NV_40 || (gpio.log[0] & 1))
56                                 duty = divs - duty;
57                         return (duty * 100) / divs;
58                 }
59
60                 return nouveau_gpio_func_get(dev, gpio.func) * 100;
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;
71         struct gpio_func gpio;
72         u32 divs, duty;
73         int ret;
74
75         if (!pm->pwm_set)
76                 return -ENODEV;
77
78         ret = nouveau_gpio_find(dev, 0, DCB_GPIO_PWM_FAN, 0xff, &gpio);
79         if (ret == 0) {
80                 divs = pm->fan.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;
89                 if (dev_priv->card_type <= NV_40 || (gpio.log[0] & 1))
90                         duty = divs - duty;
91
92                 ret = pm->pwm_set(dev, gpio.line, divs, duty);
93                 if (!ret)
94                         pm->fan.percent = percent;
95                 return ret;
96         }
97
98         return -ENODEV;
99 }
100
101 static int
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)
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
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          */
113         if (a->fanspeed && b->fanspeed && b->fanspeed > a->fanspeed) {
114                 ret = nouveau_pwmfan_set(dev, perflvl->fanspeed);
115                 if (ret && ret != -ENODEV) {
116                         NV_ERROR(dev, "fanspeed set failed: %d\n", ret);
117                         return ret;
118                 }
119         }
120
121         if (pm->voltage.supported && pm->voltage_set) {
122                 if (perflvl->volt_min && b->volt_min > a->volt_min) {
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                         }
128                 }
129         }
130
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
149         state = pm->clocks_pre(dev, perflvl);
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;
157
158         ret = nouveau_pm_perflvl_aux(dev, perflvl, perflvl, pm->cur);
159         if (ret)
160                 return ret;
161
162         pm->cur = perflvl;
163         return 0;
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;
169 }
170
171 void
172 nouveau_pm_trigger(struct drm_device *dev)
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_profile *profile = NULL;
177         struct nouveau_pm_level *perflvl = NULL;
178         int ret;
179
180         /* select power profile based on current power source */
181         if (power_supply_is_system_supplied())
182                 profile = pm->profile_ac;
183         else
184                 profile = pm->profile_dc;
185
186         if (profile != pm->profile) {
187                 pm->profile->func->fini(pm->profile);
188                 pm->profile = profile;
189                 pm->profile->func->init(pm->profile);
190         }
191
192         /* select performance level based on profile */
193         perflvl = profile->func->select(profile);
194
195         /* change perflvl, if necessary */
196         if (perflvl != pm->cur) {
197                 struct nouveau_timer_engine *ptimer = &dev_priv->engine.timer;
198                 u64 time0 = ptimer->read(dev);
199
200                 NV_INFO(dev, "setting performance level: %d", perflvl->id);
201                 ret = nouveau_pm_perflvl_set(dev, perflvl);
202                 if (ret)
203                         NV_INFO(dev, "> reclocking failed: %d\n\n", ret);
204
205                 NV_INFO(dev, "> reclocking took %lluns\n\n",
206                              ptimer->read(dev) - time0);
207         }
208 }
209
210 static struct nouveau_pm_profile *
211 profile_find(struct drm_device *dev, const char *string)
212 {
213         struct drm_nouveau_private *dev_priv = dev->dev_private;
214         struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
215         struct nouveau_pm_profile *profile;
216
217         list_for_each_entry(profile, &pm->profiles, head) {
218                 if (!strncmp(profile->name, string, sizeof(profile->name)))
219                         return profile;
220         }
221
222         return NULL;
223 }
224
225 static int
226 nouveau_pm_profile_set(struct drm_device *dev, const char *profile)
227 {
228         struct drm_nouveau_private *dev_priv = dev->dev_private;
229         struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
230         struct nouveau_pm_profile *ac = NULL, *dc = NULL;
231         char string[16], *cur = string, *ptr;
232
233         /* safety precaution, for now */
234         if (nouveau_perflvl_wr != 7777)
235                 return -EPERM;
236
237         strncpy(string, profile, sizeof(string));
238         if ((ptr = strchr(string, '\n')))
239                 *ptr = '\0';
240
241         ptr = strsep(&cur, ",");
242         if (ptr)
243                 ac = profile_find(dev, ptr);
244
245         ptr = strsep(&cur, ",");
246         if (ptr)
247                 dc = profile_find(dev, ptr);
248         else
249                 dc = ac;
250
251         if (ac == NULL || dc == NULL)
252                 return -EINVAL;
253
254         pm->profile_ac = ac;
255         pm->profile_dc = dc;
256         nouveau_pm_trigger(dev);
257         return 0;
258 }
259
260 static void
261 nouveau_pm_static_dummy(struct nouveau_pm_profile *profile)
262 {
263 }
264
265 static struct nouveau_pm_level *
266 nouveau_pm_static_select(struct nouveau_pm_profile *profile)
267 {
268         return container_of(profile, struct nouveau_pm_level, profile);
269 }
270
271 const struct nouveau_pm_profile_func nouveau_pm_static_profile_func = {
272         .destroy = nouveau_pm_static_dummy,
273         .init = nouveau_pm_static_dummy,
274         .fini = nouveau_pm_static_dummy,
275         .select = nouveau_pm_static_select,
276 };
277
278 static int
279 nouveau_pm_perflvl_get(struct drm_device *dev, struct nouveau_pm_level *perflvl)
280 {
281         struct drm_nouveau_private *dev_priv = dev->dev_private;
282         struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
283         int ret;
284
285         memset(perflvl, 0, sizeof(*perflvl));
286
287         ret = pm->clocks_get(dev, perflvl);
288         if (ret)
289                 return ret;
290
291         if (pm->voltage.supported && pm->voltage_get) {
292                 ret = pm->voltage_get(dev);
293                 if (ret > 0) {
294                         perflvl->volt_min = ret;
295                         perflvl->volt_max = ret;
296                 }
297         }
298
299         ret = nouveau_pwmfan_get(dev);
300         if (ret > 0)
301                 perflvl->fanspeed = ret;
302
303         nouveau_mem_timing_read(dev, &perflvl->timing);
304         return 0;
305 }
306
307 static void
308 nouveau_pm_perflvl_info(struct nouveau_pm_level *perflvl, char *ptr, int len)
309 {
310         char c[16], s[16], v[32], f[16], m[16];
311
312         c[0] = '\0';
313         if (perflvl->core)
314                 snprintf(c, sizeof(c), " core %dMHz", perflvl->core / 1000);
315
316         s[0] = '\0';
317         if (perflvl->shader)
318                 snprintf(s, sizeof(s), " shader %dMHz", perflvl->shader / 1000);
319
320         m[0] = '\0';
321         if (perflvl->memory)
322                 snprintf(m, sizeof(m), " memory %dMHz", perflvl->memory / 1000);
323
324         v[0] = '\0';
325         if (perflvl->volt_min && perflvl->volt_min != perflvl->volt_max) {
326                 snprintf(v, sizeof(v), " voltage %dmV-%dmV",
327                          perflvl->volt_min / 1000, perflvl->volt_max / 1000);
328         } else
329         if (perflvl->volt_min) {
330                 snprintf(v, sizeof(v), " voltage %dmV",
331                          perflvl->volt_min / 1000);
332         }
333
334         f[0] = '\0';
335         if (perflvl->fanspeed)
336                 snprintf(f, sizeof(f), " fanspeed %d%%", perflvl->fanspeed);
337
338         snprintf(ptr, len, "%s%s%s%s%s\n", c, s, m, v, f);
339 }
340
341 static ssize_t
342 nouveau_pm_get_perflvl_info(struct device *d,
343                             struct device_attribute *a, char *buf)
344 {
345         struct nouveau_pm_level *perflvl =
346                 container_of(a, struct nouveau_pm_level, dev_attr);
347         char *ptr = buf;
348         int len = PAGE_SIZE;
349
350         snprintf(ptr, len, "%d:", perflvl->id);
351         ptr += strlen(buf);
352         len -= strlen(buf);
353
354         nouveau_pm_perflvl_info(perflvl, ptr, len);
355         return strlen(buf);
356 }
357
358 static ssize_t
359 nouveau_pm_get_perflvl(struct device *d, struct device_attribute *a, char *buf)
360 {
361         struct drm_device *dev = pci_get_drvdata(to_pci_dev(d));
362         struct drm_nouveau_private *dev_priv = dev->dev_private;
363         struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
364         struct nouveau_pm_level cur;
365         int len = PAGE_SIZE, ret;
366         char *ptr = buf;
367
368         snprintf(ptr, len, "profile: %s, %s\nc:",
369                  pm->profile_ac->name, pm->profile_dc->name);
370         ptr += strlen(buf);
371         len -= strlen(buf);
372
373         ret = nouveau_pm_perflvl_get(dev, &cur);
374         if (ret == 0)
375                 nouveau_pm_perflvl_info(&cur, ptr, len);
376         return strlen(buf);
377 }
378
379 static ssize_t
380 nouveau_pm_set_perflvl(struct device *d, struct device_attribute *a,
381                        const char *buf, size_t count)
382 {
383         struct drm_device *dev = pci_get_drvdata(to_pci_dev(d));
384         int ret;
385
386         ret = nouveau_pm_profile_set(dev, buf);
387         if (ret)
388                 return ret;
389         return strlen(buf);
390 }
391
392 static DEVICE_ATTR(performance_level, S_IRUGO | S_IWUSR,
393                    nouveau_pm_get_perflvl, nouveau_pm_set_perflvl);
394
395 static int
396 nouveau_sysfs_init(struct drm_device *dev)
397 {
398         struct drm_nouveau_private *dev_priv = dev->dev_private;
399         struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
400         struct device *d = &dev->pdev->dev;
401         int ret, i;
402
403         ret = device_create_file(d, &dev_attr_performance_level);
404         if (ret)
405                 return ret;
406
407         for (i = 0; i < pm->nr_perflvl; i++) {
408                 struct nouveau_pm_level *perflvl = &pm->perflvl[i];
409
410                 perflvl->dev_attr.attr.name = perflvl->name;
411                 perflvl->dev_attr.attr.mode = S_IRUGO;
412                 perflvl->dev_attr.show = nouveau_pm_get_perflvl_info;
413                 perflvl->dev_attr.store = NULL;
414                 sysfs_attr_init(&perflvl->dev_attr.attr);
415
416                 ret = device_create_file(d, &perflvl->dev_attr);
417                 if (ret) {
418                         NV_ERROR(dev, "failed pervlvl %d sysfs: %d\n",
419                                  perflvl->id, i);
420                         perflvl->dev_attr.attr.name = NULL;
421                         nouveau_pm_fini(dev);
422                         return ret;
423                 }
424         }
425
426         return 0;
427 }
428
429 static void
430 nouveau_sysfs_fini(struct drm_device *dev)
431 {
432         struct drm_nouveau_private *dev_priv = dev->dev_private;
433         struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
434         struct device *d = &dev->pdev->dev;
435         int i;
436
437         device_remove_file(d, &dev_attr_performance_level);
438         for (i = 0; i < pm->nr_perflvl; i++) {
439                 struct nouveau_pm_level *pl = &pm->perflvl[i];
440
441                 if (!pl->dev_attr.attr.name)
442                         break;
443
444                 device_remove_file(d, &pl->dev_attr);
445         }
446 }
447
448 #if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
449 static ssize_t
450 nouveau_hwmon_show_temp(struct device *d, struct device_attribute *a, char *buf)
451 {
452         struct drm_device *dev = dev_get_drvdata(d);
453         struct drm_nouveau_private *dev_priv = dev->dev_private;
454         struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
455
456         return snprintf(buf, PAGE_SIZE, "%d\n", pm->temp_get(dev)*1000);
457 }
458 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, nouveau_hwmon_show_temp,
459                                                   NULL, 0);
460
461 static ssize_t
462 nouveau_hwmon_max_temp(struct device *d, struct device_attribute *a, char *buf)
463 {
464         struct drm_device *dev = dev_get_drvdata(d);
465         struct drm_nouveau_private *dev_priv = dev->dev_private;
466         struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
467         struct nouveau_pm_threshold_temp *temp = &pm->threshold_temp;
468
469         return snprintf(buf, PAGE_SIZE, "%d\n", temp->down_clock*1000);
470 }
471 static ssize_t
472 nouveau_hwmon_set_max_temp(struct device *d, struct device_attribute *a,
473                                                 const char *buf, size_t count)
474 {
475         struct drm_device *dev = dev_get_drvdata(d);
476         struct drm_nouveau_private *dev_priv = dev->dev_private;
477         struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
478         struct nouveau_pm_threshold_temp *temp = &pm->threshold_temp;
479         long value;
480
481         if (kstrtol(buf, 10, &value) == -EINVAL)
482                 return count;
483
484         temp->down_clock = value/1000;
485
486         nouveau_temp_safety_checks(dev);
487
488         return count;
489 }
490 static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR, nouveau_hwmon_max_temp,
491                                                   nouveau_hwmon_set_max_temp,
492                                                   0);
493
494 static ssize_t
495 nouveau_hwmon_critical_temp(struct device *d, struct device_attribute *a,
496                                                         char *buf)
497 {
498         struct drm_device *dev = dev_get_drvdata(d);
499         struct drm_nouveau_private *dev_priv = dev->dev_private;
500         struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
501         struct nouveau_pm_threshold_temp *temp = &pm->threshold_temp;
502
503         return snprintf(buf, PAGE_SIZE, "%d\n", temp->critical*1000);
504 }
505 static ssize_t
506 nouveau_hwmon_set_critical_temp(struct device *d, struct device_attribute *a,
507                                                             const char *buf,
508                                                                 size_t count)
509 {
510         struct drm_device *dev = dev_get_drvdata(d);
511         struct drm_nouveau_private *dev_priv = dev->dev_private;
512         struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
513         struct nouveau_pm_threshold_temp *temp = &pm->threshold_temp;
514         long value;
515
516         if (kstrtol(buf, 10, &value) == -EINVAL)
517                 return count;
518
519         temp->critical = value/1000;
520
521         nouveau_temp_safety_checks(dev);
522
523         return count;
524 }
525 static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO | S_IWUSR,
526                                                 nouveau_hwmon_critical_temp,
527                                                 nouveau_hwmon_set_critical_temp,
528                                                 0);
529
530 static ssize_t nouveau_hwmon_show_name(struct device *dev,
531                                       struct device_attribute *attr,
532                                       char *buf)
533 {
534         return sprintf(buf, "nouveau\n");
535 }
536 static SENSOR_DEVICE_ATTR(name, S_IRUGO, nouveau_hwmon_show_name, NULL, 0);
537
538 static ssize_t nouveau_hwmon_show_update_rate(struct device *dev,
539                                       struct device_attribute *attr,
540                                       char *buf)
541 {
542         return sprintf(buf, "1000\n");
543 }
544 static SENSOR_DEVICE_ATTR(update_rate, S_IRUGO,
545                                                 nouveau_hwmon_show_update_rate,
546                                                 NULL, 0);
547
548 static ssize_t
549 nouveau_hwmon_show_fan0_input(struct device *d, struct device_attribute *attr,
550                               char *buf)
551 {
552         struct drm_device *dev = dev_get_drvdata(d);
553         struct drm_nouveau_private *dev_priv = dev->dev_private;
554         struct nouveau_timer_engine *ptimer = &dev_priv->engine.timer;
555         struct gpio_func gpio;
556         u32 cycles, cur, prev;
557         u64 start;
558         int ret;
559
560         ret = nouveau_gpio_find(dev, 0, DCB_GPIO_FAN_SENSE, 0xff, &gpio);
561         if (ret)
562                 return ret;
563
564         /* Monitor the GPIO input 0x3b for 250ms.
565          * When the fan spins, it changes the value of GPIO FAN_SENSE.
566          * We get 4 changes (0 -> 1 -> 0 -> 1 -> [...]) per complete rotation.
567          */
568         start = ptimer->read(dev);
569         prev = nouveau_gpio_sense(dev, 0, gpio.line);
570         cycles = 0;
571         do {
572                 cur = nouveau_gpio_sense(dev, 0, gpio.line);
573                 if (prev != cur) {
574                         cycles++;
575                         prev = cur;
576                 }
577
578                 usleep_range(500, 1000); /* supports 0 < rpm < 7500 */
579         } while (ptimer->read(dev) - start < 250000000);
580
581         /* interpolate to get rpm */
582         return sprintf(buf, "%i\n", cycles / 4 * 4 * 60);
583 }
584 static SENSOR_DEVICE_ATTR(fan0_input, S_IRUGO, nouveau_hwmon_show_fan0_input,
585                           NULL, 0);
586
587 static ssize_t
588 nouveau_hwmon_get_pwm0(struct device *d, struct device_attribute *a, char *buf)
589 {
590         struct drm_device *dev = dev_get_drvdata(d);
591         int ret;
592
593         ret = nouveau_pwmfan_get(dev);
594         if (ret < 0)
595                 return ret;
596
597         return sprintf(buf, "%i\n", ret);
598 }
599
600 static ssize_t
601 nouveau_hwmon_set_pwm0(struct device *d, struct device_attribute *a,
602                        const char *buf, size_t count)
603 {
604         struct drm_device *dev = dev_get_drvdata(d);
605         struct drm_nouveau_private *dev_priv = dev->dev_private;
606         struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
607         int ret = -ENODEV;
608         long value;
609
610         if (nouveau_perflvl_wr != 7777)
611                 return -EPERM;
612
613         if (kstrtol(buf, 10, &value) == -EINVAL)
614                 return -EINVAL;
615
616         if (value < pm->fan.min_duty)
617                 value = pm->fan.min_duty;
618         if (value > pm->fan.max_duty)
619                 value = pm->fan.max_duty;
620
621         ret = nouveau_pwmfan_set(dev, value);
622         if (ret)
623                 return ret;
624
625         return count;
626 }
627
628 static SENSOR_DEVICE_ATTR(pwm0, S_IRUGO | S_IWUSR,
629                           nouveau_hwmon_get_pwm0,
630                           nouveau_hwmon_set_pwm0, 0);
631
632 static ssize_t
633 nouveau_hwmon_get_pwm0_min(struct device *d,
634                            struct device_attribute *a, char *buf)
635 {
636         struct drm_device *dev = dev_get_drvdata(d);
637         struct drm_nouveau_private *dev_priv = dev->dev_private;
638         struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
639
640         return sprintf(buf, "%i\n", pm->fan.min_duty);
641 }
642
643 static ssize_t
644 nouveau_hwmon_set_pwm0_min(struct device *d, struct device_attribute *a,
645                            const char *buf, size_t count)
646 {
647         struct drm_device *dev = dev_get_drvdata(d);
648         struct drm_nouveau_private *dev_priv = dev->dev_private;
649         struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
650         long value;
651
652         if (kstrtol(buf, 10, &value) == -EINVAL)
653                 return -EINVAL;
654
655         if (value < 0)
656                 value = 0;
657
658         if (pm->fan.max_duty - value < 10)
659                 value = pm->fan.max_duty - 10;
660
661         if (value < 10)
662                 pm->fan.min_duty = 10;
663         else
664                 pm->fan.min_duty = value;
665
666         return count;
667 }
668
669 static SENSOR_DEVICE_ATTR(pwm0_min, S_IRUGO | S_IWUSR,
670                           nouveau_hwmon_get_pwm0_min,
671                           nouveau_hwmon_set_pwm0_min, 0);
672
673 static ssize_t
674 nouveau_hwmon_get_pwm0_max(struct device *d,
675                            struct device_attribute *a, char *buf)
676 {
677         struct drm_device *dev = dev_get_drvdata(d);
678         struct drm_nouveau_private *dev_priv = dev->dev_private;
679         struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
680
681         return sprintf(buf, "%i\n", pm->fan.max_duty);
682 }
683
684 static ssize_t
685 nouveau_hwmon_set_pwm0_max(struct device *d, struct device_attribute *a,
686                            const char *buf, size_t count)
687 {
688         struct drm_device *dev = dev_get_drvdata(d);
689         struct drm_nouveau_private *dev_priv = dev->dev_private;
690         struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
691         long value;
692
693         if (kstrtol(buf, 10, &value) == -EINVAL)
694                 return -EINVAL;
695
696         if (value < 0)
697                 value = 0;
698
699         if (value - pm->fan.min_duty < 10)
700                 value = pm->fan.min_duty + 10;
701
702         if (value > 100)
703                 pm->fan.max_duty = 100;
704         else
705                 pm->fan.max_duty = value;
706
707         return count;
708 }
709
710 static SENSOR_DEVICE_ATTR(pwm0_max, S_IRUGO | S_IWUSR,
711                           nouveau_hwmon_get_pwm0_max,
712                           nouveau_hwmon_set_pwm0_max, 0);
713
714 static struct attribute *hwmon_attributes[] = {
715         &sensor_dev_attr_temp1_input.dev_attr.attr,
716         &sensor_dev_attr_temp1_max.dev_attr.attr,
717         &sensor_dev_attr_temp1_crit.dev_attr.attr,
718         &sensor_dev_attr_name.dev_attr.attr,
719         &sensor_dev_attr_update_rate.dev_attr.attr,
720         NULL
721 };
722 static struct attribute *hwmon_fan_rpm_attributes[] = {
723         &sensor_dev_attr_fan0_input.dev_attr.attr,
724         NULL
725 };
726 static struct attribute *hwmon_pwm_fan_attributes[] = {
727         &sensor_dev_attr_pwm0.dev_attr.attr,
728         &sensor_dev_attr_pwm0_min.dev_attr.attr,
729         &sensor_dev_attr_pwm0_max.dev_attr.attr,
730         NULL
731 };
732
733 static const struct attribute_group hwmon_attrgroup = {
734         .attrs = hwmon_attributes,
735 };
736 static const struct attribute_group hwmon_fan_rpm_attrgroup = {
737         .attrs = hwmon_fan_rpm_attributes,
738 };
739 static const struct attribute_group hwmon_pwm_fan_attrgroup = {
740         .attrs = hwmon_pwm_fan_attributes,
741 };
742 #endif
743
744 static int
745 nouveau_hwmon_init(struct drm_device *dev)
746 {
747         struct drm_nouveau_private *dev_priv = dev->dev_private;
748         struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
749 #if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
750         struct device *hwmon_dev;
751         int ret = 0;
752
753         if (!pm->temp_get)
754                 return -ENODEV;
755
756         hwmon_dev = hwmon_device_register(&dev->pdev->dev);
757         if (IS_ERR(hwmon_dev)) {
758                 ret = PTR_ERR(hwmon_dev);
759                 NV_ERROR(dev,
760                         "Unable to register hwmon device: %d\n", ret);
761                 return ret;
762         }
763         dev_set_drvdata(hwmon_dev, dev);
764
765         /* default sysfs entries */
766         ret = sysfs_create_group(&dev->pdev->dev.kobj, &hwmon_attrgroup);
767         if (ret) {
768                 if (ret)
769                         goto error;
770         }
771
772         /* if the card has a pwm fan */
773         /*XXX: incorrect, need better detection for this, some boards have
774          *     the gpio entries for pwm fan control even when there's no
775          *     actual fan connected to it... therm table? */
776         if (nouveau_pwmfan_get(dev) >= 0) {
777                 ret = sysfs_create_group(&dev->pdev->dev.kobj,
778                                          &hwmon_pwm_fan_attrgroup);
779                 if (ret)
780                         goto error;
781         }
782
783         /* if the card can read the fan rpm */
784         if (nouveau_gpio_func_valid(dev, DCB_GPIO_FAN_SENSE)) {
785                 ret = sysfs_create_group(&dev->pdev->dev.kobj,
786                                          &hwmon_fan_rpm_attrgroup);
787                 if (ret)
788                         goto error;
789         }
790
791         pm->hwmon = hwmon_dev;
792
793         return 0;
794
795 error:
796         NV_ERROR(dev, "Unable to create some hwmon sysfs files: %d\n", ret);
797         hwmon_device_unregister(hwmon_dev);
798         pm->hwmon = NULL;
799         return ret;
800 #else
801         pm->hwmon = NULL;
802         return 0;
803 #endif
804 }
805
806 static void
807 nouveau_hwmon_fini(struct drm_device *dev)
808 {
809 #if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
810         struct drm_nouveau_private *dev_priv = dev->dev_private;
811         struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
812
813         if (pm->hwmon) {
814                 sysfs_remove_group(&dev->pdev->dev.kobj, &hwmon_attrgroup);
815                 sysfs_remove_group(&dev->pdev->dev.kobj,
816                                    &hwmon_pwm_fan_attrgroup);
817                 sysfs_remove_group(&dev->pdev->dev.kobj,
818                                    &hwmon_fan_rpm_attrgroup);
819
820                 hwmon_device_unregister(pm->hwmon);
821         }
822 #endif
823 }
824
825 #if defined(CONFIG_ACPI) && defined(CONFIG_POWER_SUPPLY)
826 static int
827 nouveau_pm_acpi_event(struct notifier_block *nb, unsigned long val, void *data)
828 {
829         struct drm_nouveau_private *dev_priv =
830                 container_of(nb, struct drm_nouveau_private, engine.pm.acpi_nb);
831         struct drm_device *dev = dev_priv->dev;
832         struct acpi_bus_event *entry = (struct acpi_bus_event *)data;
833
834         if (strcmp(entry->device_class, "ac_adapter") == 0) {
835                 bool ac = power_supply_is_system_supplied();
836
837                 NV_DEBUG(dev, "power supply changed: %s\n", ac ? "AC" : "DC");
838                 nouveau_pm_trigger(dev);
839         }
840
841         return NOTIFY_OK;
842 }
843 #endif
844
845 int
846 nouveau_pm_init(struct drm_device *dev)
847 {
848         struct drm_nouveau_private *dev_priv = dev->dev_private;
849         struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
850         char info[256];
851         int ret, i;
852
853         /* parse aux tables from vbios */
854         nouveau_volt_init(dev);
855         nouveau_temp_init(dev);
856
857         /* determine current ("boot") performance level */
858         ret = nouveau_pm_perflvl_get(dev, &pm->boot);
859         if (ret) {
860                 NV_ERROR(dev, "failed to determine boot perflvl\n");
861                 return ret;
862         }
863
864         strncpy(pm->boot.name, "boot", 4);
865         strncpy(pm->boot.profile.name, "boot", 4);
866         pm->boot.profile.func = &nouveau_pm_static_profile_func;
867
868         INIT_LIST_HEAD(&pm->profiles);
869         list_add(&pm->boot.profile.head, &pm->profiles);
870
871         pm->profile_ac = &pm->boot.profile;
872         pm->profile_dc = &pm->boot.profile;
873         pm->profile = &pm->boot.profile;
874         pm->cur = &pm->boot;
875
876         /* add performance levels from vbios */
877         nouveau_perf_init(dev);
878
879         /* display available performance levels */
880         NV_INFO(dev, "%d available performance level(s)\n", pm->nr_perflvl);
881         for (i = 0; i < pm->nr_perflvl; i++) {
882                 nouveau_pm_perflvl_info(&pm->perflvl[i], info, sizeof(info));
883                 NV_INFO(dev, "%d:%s", pm->perflvl[i].id, info);
884         }
885
886         nouveau_pm_perflvl_info(&pm->boot, info, sizeof(info));
887         NV_INFO(dev, "c:%s", info);
888
889         /* switch performance levels now if requested */
890         if (nouveau_perflvl != NULL)
891                 nouveau_pm_profile_set(dev, nouveau_perflvl);
892
893         /* determine the current fan speed */
894         pm->fan.percent = nouveau_pwmfan_get(dev);
895
896         nouveau_sysfs_init(dev);
897         nouveau_hwmon_init(dev);
898 #if defined(CONFIG_ACPI) && defined(CONFIG_POWER_SUPPLY)
899         pm->acpi_nb.notifier_call = nouveau_pm_acpi_event;
900         register_acpi_notifier(&pm->acpi_nb);
901 #endif
902
903         return 0;
904 }
905
906 void
907 nouveau_pm_fini(struct drm_device *dev)
908 {
909         struct drm_nouveau_private *dev_priv = dev->dev_private;
910         struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
911         struct nouveau_pm_profile *profile, *tmp;
912
913         list_for_each_entry_safe(profile, tmp, &pm->profiles, head) {
914                 list_del(&profile->head);
915                 profile->func->destroy(profile);
916         }
917
918         if (pm->cur != &pm->boot)
919                 nouveau_pm_perflvl_set(dev, &pm->boot);
920
921         nouveau_temp_fini(dev);
922         nouveau_perf_fini(dev);
923         nouveau_volt_fini(dev);
924
925 #if defined(CONFIG_ACPI) && defined(CONFIG_POWER_SUPPLY)
926         unregister_acpi_notifier(&pm->acpi_nb);
927 #endif
928         nouveau_hwmon_fini(dev);
929         nouveau_sysfs_fini(dev);
930 }
931
932 void
933 nouveau_pm_resume(struct drm_device *dev)
934 {
935         struct drm_nouveau_private *dev_priv = dev->dev_private;
936         struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
937         struct nouveau_pm_level *perflvl;
938
939         if (!pm->cur || pm->cur == &pm->boot)
940                 return;
941
942         perflvl = pm->cur;
943         pm->cur = &pm->boot;
944         nouveau_pm_perflvl_set(dev, perflvl);
945         nouveau_pwmfan_set(dev, pm->fan.percent);
946 }