2 * Copyright 2010 Red Hat Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
27 #include "nouveau_drv.h"
28 #include "nouveau_pm.h"
29 #include <subdev/bios/gpio.h>
32 #include <linux/acpi.h>
34 #include <linux/power_supply.h>
35 #include <linux/hwmon.h>
36 #include <linux/hwmon-sysfs.h>
39 nouveau_pwmfan_get(struct drm_device *dev)
41 struct drm_nouveau_private *dev_priv = dev->dev_private;
42 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
43 struct dcb_gpio_func gpio;
50 ret = nouveau_gpio_find(dev, 0, DCB_GPIO_PWM_FAN, 0xff, &gpio);
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))
57 return (duty * 100) / divs;
60 return nouveau_gpio_func_get(dev, gpio.func) * 100;
67 nouveau_pwmfan_set(struct drm_device *dev, int percent)
69 struct drm_nouveau_private *dev_priv = dev->dev_private;
70 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
71 struct dcb_gpio_func gpio;
78 ret = nouveau_gpio_find(dev, 0, DCB_GPIO_PWM_FAN, 0xff, &gpio);
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)
88 duty = ((divs * percent) + 99) / 100;
89 if (dev_priv->card_type <= NV_40 || (gpio.log[0] & 1))
92 ret = pm->pwm_set(dev, gpio.line, divs, duty);
94 pm->fan.percent = percent;
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)
105 struct drm_nouveau_private *dev_priv = dev->dev_private;
106 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
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
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);
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);
125 NV_ERROR(dev, "voltage set failed: %d\n", ret);
135 nouveau_pm_perflvl_set(struct drm_device *dev, struct nouveau_pm_level *perflvl)
137 struct drm_nouveau_private *dev_priv = dev->dev_private;
138 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
142 if (perflvl == pm->cur)
145 ret = nouveau_pm_perflvl_aux(dev, perflvl, pm->cur, perflvl);
149 state = pm->clocks_pre(dev, perflvl);
151 ret = PTR_ERR(state);
154 ret = pm->clocks_set(dev, state);
158 ret = nouveau_pm_perflvl_aux(dev, perflvl, perflvl, pm->cur);
166 /* restore the fan speed and voltage before leaving */
167 nouveau_pm_perflvl_aux(dev, perflvl, perflvl, pm->cur);
172 nouveau_pm_trigger(struct drm_device *dev)
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;
180 /* select power profile based on current power source */
181 if (power_supply_is_system_supplied())
182 profile = pm->profile_ac;
184 profile = pm->profile_dc;
186 if (profile != pm->profile) {
187 pm->profile->func->fini(pm->profile);
188 pm->profile = profile;
189 pm->profile->func->init(pm->profile);
192 /* select performance level based on profile */
193 perflvl = profile->func->select(profile);
195 /* change perflvl, if necessary */
196 if (perflvl != pm->cur) {
197 u64 time0 = nv_timer_read(dev);
199 NV_INFO(dev, "setting performance level: %d", perflvl->id);
200 ret = nouveau_pm_perflvl_set(dev, perflvl);
202 NV_INFO(dev, "> reclocking failed: %d\n\n", ret);
204 NV_INFO(dev, "> reclocking took %lluns\n\n",
205 nv_timer_read(dev) - time0);
209 static struct nouveau_pm_profile *
210 profile_find(struct drm_device *dev, const char *string)
212 struct drm_nouveau_private *dev_priv = dev->dev_private;
213 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
214 struct nouveau_pm_profile *profile;
216 list_for_each_entry(profile, &pm->profiles, head) {
217 if (!strncmp(profile->name, string, sizeof(profile->name)))
225 nouveau_pm_profile_set(struct drm_device *dev, const char *profile)
227 struct drm_nouveau_private *dev_priv = dev->dev_private;
228 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
229 struct nouveau_pm_profile *ac = NULL, *dc = NULL;
230 char string[16], *cur = string, *ptr;
232 /* safety precaution, for now */
233 if (nouveau_perflvl_wr != 7777)
236 strncpy(string, profile, sizeof(string));
237 string[sizeof(string) - 1] = 0;
238 if ((ptr = strchr(string, '\n')))
241 ptr = strsep(&cur, ",");
243 ac = profile_find(dev, ptr);
245 ptr = strsep(&cur, ",");
247 dc = profile_find(dev, ptr);
251 if (ac == NULL || dc == NULL)
256 nouveau_pm_trigger(dev);
261 nouveau_pm_static_dummy(struct nouveau_pm_profile *profile)
265 static struct nouveau_pm_level *
266 nouveau_pm_static_select(struct nouveau_pm_profile *profile)
268 return container_of(profile, struct nouveau_pm_level, profile);
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,
279 nouveau_pm_perflvl_get(struct drm_device *dev, struct nouveau_pm_level *perflvl)
281 struct drm_nouveau_private *dev_priv = dev->dev_private;
282 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
285 memset(perflvl, 0, sizeof(*perflvl));
287 if (pm->clocks_get) {
288 ret = pm->clocks_get(dev, perflvl);
293 if (pm->voltage.supported && pm->voltage_get) {
294 ret = pm->voltage_get(dev);
296 perflvl->volt_min = ret;
297 perflvl->volt_max = ret;
301 ret = nouveau_pwmfan_get(dev);
303 perflvl->fanspeed = ret;
305 nouveau_mem_timing_read(dev, &perflvl->timing);
310 nouveau_pm_perflvl_info(struct nouveau_pm_level *perflvl, char *ptr, int len)
312 char c[16], s[16], v[32], f[16], m[16];
316 snprintf(c, sizeof(c), " core %dMHz", perflvl->core / 1000);
320 snprintf(s, sizeof(s), " shader %dMHz", perflvl->shader / 1000);
324 snprintf(m, sizeof(m), " memory %dMHz", perflvl->memory / 1000);
327 if (perflvl->volt_min && perflvl->volt_min != perflvl->volt_max) {
328 snprintf(v, sizeof(v), " voltage %dmV-%dmV",
329 perflvl->volt_min / 1000, perflvl->volt_max / 1000);
331 if (perflvl->volt_min) {
332 snprintf(v, sizeof(v), " voltage %dmV",
333 perflvl->volt_min / 1000);
337 if (perflvl->fanspeed)
338 snprintf(f, sizeof(f), " fanspeed %d%%", perflvl->fanspeed);
340 snprintf(ptr, len, "%s%s%s%s%s\n", c, s, m, v, f);
344 nouveau_pm_get_perflvl_info(struct device *d,
345 struct device_attribute *a, char *buf)
347 struct nouveau_pm_level *perflvl =
348 container_of(a, struct nouveau_pm_level, dev_attr);
352 snprintf(ptr, len, "%d:", perflvl->id);
356 nouveau_pm_perflvl_info(perflvl, ptr, len);
361 nouveau_pm_get_perflvl(struct device *d, struct device_attribute *a, char *buf)
363 struct drm_device *dev = pci_get_drvdata(to_pci_dev(d));
364 struct drm_nouveau_private *dev_priv = dev->dev_private;
365 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
366 struct nouveau_pm_level cur;
367 int len = PAGE_SIZE, ret;
370 snprintf(ptr, len, "profile: %s, %s\nc:",
371 pm->profile_ac->name, pm->profile_dc->name);
375 ret = nouveau_pm_perflvl_get(dev, &cur);
377 nouveau_pm_perflvl_info(&cur, ptr, len);
382 nouveau_pm_set_perflvl(struct device *d, struct device_attribute *a,
383 const char *buf, size_t count)
385 struct drm_device *dev = pci_get_drvdata(to_pci_dev(d));
388 ret = nouveau_pm_profile_set(dev, buf);
394 static DEVICE_ATTR(performance_level, S_IRUGO | S_IWUSR,
395 nouveau_pm_get_perflvl, nouveau_pm_set_perflvl);
398 nouveau_sysfs_init(struct drm_device *dev)
400 struct drm_nouveau_private *dev_priv = dev->dev_private;
401 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
402 struct device *d = &dev->pdev->dev;
405 ret = device_create_file(d, &dev_attr_performance_level);
409 for (i = 0; i < pm->nr_perflvl; i++) {
410 struct nouveau_pm_level *perflvl = &pm->perflvl[i];
412 perflvl->dev_attr.attr.name = perflvl->name;
413 perflvl->dev_attr.attr.mode = S_IRUGO;
414 perflvl->dev_attr.show = nouveau_pm_get_perflvl_info;
415 perflvl->dev_attr.store = NULL;
416 sysfs_attr_init(&perflvl->dev_attr.attr);
418 ret = device_create_file(d, &perflvl->dev_attr);
420 NV_ERROR(dev, "failed pervlvl %d sysfs: %d\n",
422 perflvl->dev_attr.attr.name = NULL;
423 nouveau_pm_fini(dev);
432 nouveau_sysfs_fini(struct drm_device *dev)
434 struct drm_nouveau_private *dev_priv = dev->dev_private;
435 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
436 struct device *d = &dev->pdev->dev;
439 device_remove_file(d, &dev_attr_performance_level);
440 for (i = 0; i < pm->nr_perflvl; i++) {
441 struct nouveau_pm_level *pl = &pm->perflvl[i];
443 if (!pl->dev_attr.attr.name)
446 device_remove_file(d, &pl->dev_attr);
450 #if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
452 nouveau_hwmon_show_temp(struct device *d, struct device_attribute *a, char *buf)
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;
458 return snprintf(buf, PAGE_SIZE, "%d\n", pm->temp_get(dev)*1000);
460 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, nouveau_hwmon_show_temp,
464 nouveau_hwmon_max_temp(struct device *d, struct device_attribute *a, char *buf)
466 struct drm_device *dev = dev_get_drvdata(d);
467 struct drm_nouveau_private *dev_priv = dev->dev_private;
468 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
469 struct nouveau_pm_threshold_temp *temp = &pm->threshold_temp;
471 return snprintf(buf, PAGE_SIZE, "%d\n", temp->down_clock*1000);
474 nouveau_hwmon_set_max_temp(struct device *d, struct device_attribute *a,
475 const char *buf, size_t count)
477 struct drm_device *dev = dev_get_drvdata(d);
478 struct drm_nouveau_private *dev_priv = dev->dev_private;
479 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
480 struct nouveau_pm_threshold_temp *temp = &pm->threshold_temp;
483 if (kstrtol(buf, 10, &value) == -EINVAL)
486 temp->down_clock = value/1000;
488 nouveau_temp_safety_checks(dev);
492 static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR, nouveau_hwmon_max_temp,
493 nouveau_hwmon_set_max_temp,
497 nouveau_hwmon_critical_temp(struct device *d, struct device_attribute *a,
500 struct drm_device *dev = dev_get_drvdata(d);
501 struct drm_nouveau_private *dev_priv = dev->dev_private;
502 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
503 struct nouveau_pm_threshold_temp *temp = &pm->threshold_temp;
505 return snprintf(buf, PAGE_SIZE, "%d\n", temp->critical*1000);
508 nouveau_hwmon_set_critical_temp(struct device *d, struct device_attribute *a,
512 struct drm_device *dev = dev_get_drvdata(d);
513 struct drm_nouveau_private *dev_priv = dev->dev_private;
514 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
515 struct nouveau_pm_threshold_temp *temp = &pm->threshold_temp;
518 if (kstrtol(buf, 10, &value) == -EINVAL)
521 temp->critical = value/1000;
523 nouveau_temp_safety_checks(dev);
527 static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO | S_IWUSR,
528 nouveau_hwmon_critical_temp,
529 nouveau_hwmon_set_critical_temp,
532 static ssize_t nouveau_hwmon_show_name(struct device *dev,
533 struct device_attribute *attr,
536 return sprintf(buf, "nouveau\n");
538 static SENSOR_DEVICE_ATTR(name, S_IRUGO, nouveau_hwmon_show_name, NULL, 0);
540 static ssize_t nouveau_hwmon_show_update_rate(struct device *dev,
541 struct device_attribute *attr,
544 return sprintf(buf, "1000\n");
546 static SENSOR_DEVICE_ATTR(update_rate, S_IRUGO,
547 nouveau_hwmon_show_update_rate,
551 nouveau_hwmon_show_fan0_input(struct device *d, struct device_attribute *attr,
554 struct drm_device *dev = dev_get_drvdata(d);
555 u32 cycles, cur, prev;
558 if (!nouveau_gpio_func_valid(dev, DCB_GPIO_FAN_SENSE))
561 /* Monitor the GPIO input 0x3b for 250ms.
562 * When the fan spins, it changes the value of GPIO FAN_SENSE.
563 * We get 4 changes (0 -> 1 -> 0 -> 1 -> [...]) per complete rotation.
565 start = nv_timer_read(dev);
566 prev = nouveau_gpio_func_get(dev, DCB_GPIO_FAN_SENSE);
569 cur = nouveau_gpio_func_get(dev, DCB_GPIO_FAN_SENSE);
575 usleep_range(500, 1000); /* supports 0 < rpm < 7500 */
576 } while (nv_timer_read(dev) - start < 250000000);
578 /* interpolate to get rpm */
579 return sprintf(buf, "%i\n", cycles / 4 * 4 * 60);
581 static SENSOR_DEVICE_ATTR(fan0_input, S_IRUGO, nouveau_hwmon_show_fan0_input,
585 nouveau_hwmon_get_pwm0(struct device *d, struct device_attribute *a, char *buf)
587 struct drm_device *dev = dev_get_drvdata(d);
590 ret = nouveau_pwmfan_get(dev);
594 return sprintf(buf, "%i\n", ret);
598 nouveau_hwmon_set_pwm0(struct device *d, struct device_attribute *a,
599 const char *buf, size_t count)
601 struct drm_device *dev = dev_get_drvdata(d);
602 struct drm_nouveau_private *dev_priv = dev->dev_private;
603 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
607 if (nouveau_perflvl_wr != 7777)
610 if (kstrtol(buf, 10, &value) == -EINVAL)
613 if (value < pm->fan.min_duty)
614 value = pm->fan.min_duty;
615 if (value > pm->fan.max_duty)
616 value = pm->fan.max_duty;
618 ret = nouveau_pwmfan_set(dev, value);
625 static SENSOR_DEVICE_ATTR(pwm0, S_IRUGO | S_IWUSR,
626 nouveau_hwmon_get_pwm0,
627 nouveau_hwmon_set_pwm0, 0);
630 nouveau_hwmon_get_pwm0_min(struct device *d,
631 struct device_attribute *a, char *buf)
633 struct drm_device *dev = dev_get_drvdata(d);
634 struct drm_nouveau_private *dev_priv = dev->dev_private;
635 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
637 return sprintf(buf, "%i\n", pm->fan.min_duty);
641 nouveau_hwmon_set_pwm0_min(struct device *d, struct device_attribute *a,
642 const char *buf, size_t count)
644 struct drm_device *dev = dev_get_drvdata(d);
645 struct drm_nouveau_private *dev_priv = dev->dev_private;
646 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
649 if (kstrtol(buf, 10, &value) == -EINVAL)
655 if (pm->fan.max_duty - value < 10)
656 value = pm->fan.max_duty - 10;
659 pm->fan.min_duty = 10;
661 pm->fan.min_duty = value;
666 static SENSOR_DEVICE_ATTR(pwm0_min, S_IRUGO | S_IWUSR,
667 nouveau_hwmon_get_pwm0_min,
668 nouveau_hwmon_set_pwm0_min, 0);
671 nouveau_hwmon_get_pwm0_max(struct device *d,
672 struct device_attribute *a, char *buf)
674 struct drm_device *dev = dev_get_drvdata(d);
675 struct drm_nouveau_private *dev_priv = dev->dev_private;
676 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
678 return sprintf(buf, "%i\n", pm->fan.max_duty);
682 nouveau_hwmon_set_pwm0_max(struct device *d, struct device_attribute *a,
683 const char *buf, size_t count)
685 struct drm_device *dev = dev_get_drvdata(d);
686 struct drm_nouveau_private *dev_priv = dev->dev_private;
687 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
690 if (kstrtol(buf, 10, &value) == -EINVAL)
696 if (value - pm->fan.min_duty < 10)
697 value = pm->fan.min_duty + 10;
700 pm->fan.max_duty = 100;
702 pm->fan.max_duty = value;
707 static SENSOR_DEVICE_ATTR(pwm0_max, S_IRUGO | S_IWUSR,
708 nouveau_hwmon_get_pwm0_max,
709 nouveau_hwmon_set_pwm0_max, 0);
711 static struct attribute *hwmon_attributes[] = {
712 &sensor_dev_attr_temp1_input.dev_attr.attr,
713 &sensor_dev_attr_temp1_max.dev_attr.attr,
714 &sensor_dev_attr_temp1_crit.dev_attr.attr,
715 &sensor_dev_attr_name.dev_attr.attr,
716 &sensor_dev_attr_update_rate.dev_attr.attr,
719 static struct attribute *hwmon_fan_rpm_attributes[] = {
720 &sensor_dev_attr_fan0_input.dev_attr.attr,
723 static struct attribute *hwmon_pwm_fan_attributes[] = {
724 &sensor_dev_attr_pwm0.dev_attr.attr,
725 &sensor_dev_attr_pwm0_min.dev_attr.attr,
726 &sensor_dev_attr_pwm0_max.dev_attr.attr,
730 static const struct attribute_group hwmon_attrgroup = {
731 .attrs = hwmon_attributes,
733 static const struct attribute_group hwmon_fan_rpm_attrgroup = {
734 .attrs = hwmon_fan_rpm_attributes,
736 static const struct attribute_group hwmon_pwm_fan_attrgroup = {
737 .attrs = hwmon_pwm_fan_attributes,
742 nouveau_hwmon_init(struct drm_device *dev)
744 struct drm_nouveau_private *dev_priv = dev->dev_private;
745 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
746 #if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
747 struct device *hwmon_dev;
753 hwmon_dev = hwmon_device_register(&dev->pdev->dev);
754 if (IS_ERR(hwmon_dev)) {
755 ret = PTR_ERR(hwmon_dev);
757 "Unable to register hwmon device: %d\n", ret);
760 dev_set_drvdata(hwmon_dev, dev);
762 /* default sysfs entries */
763 ret = sysfs_create_group(&dev->pdev->dev.kobj, &hwmon_attrgroup);
769 /* if the card has a pwm fan */
770 /*XXX: incorrect, need better detection for this, some boards have
771 * the gpio entries for pwm fan control even when there's no
772 * actual fan connected to it... therm table? */
773 if (nouveau_pwmfan_get(dev) >= 0) {
774 ret = sysfs_create_group(&dev->pdev->dev.kobj,
775 &hwmon_pwm_fan_attrgroup);
780 /* if the card can read the fan rpm */
781 if (nouveau_gpio_func_valid(dev, DCB_GPIO_FAN_SENSE)) {
782 ret = sysfs_create_group(&dev->pdev->dev.kobj,
783 &hwmon_fan_rpm_attrgroup);
788 pm->hwmon = hwmon_dev;
793 NV_ERROR(dev, "Unable to create some hwmon sysfs files: %d\n", ret);
794 hwmon_device_unregister(hwmon_dev);
804 nouveau_hwmon_fini(struct drm_device *dev)
806 #if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
807 struct drm_nouveau_private *dev_priv = dev->dev_private;
808 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
811 sysfs_remove_group(&dev->pdev->dev.kobj, &hwmon_attrgroup);
812 sysfs_remove_group(&dev->pdev->dev.kobj,
813 &hwmon_pwm_fan_attrgroup);
814 sysfs_remove_group(&dev->pdev->dev.kobj,
815 &hwmon_fan_rpm_attrgroup);
817 hwmon_device_unregister(pm->hwmon);
822 #if defined(CONFIG_ACPI) && defined(CONFIG_POWER_SUPPLY)
824 nouveau_pm_acpi_event(struct notifier_block *nb, unsigned long val, void *data)
826 struct drm_nouveau_private *dev_priv =
827 container_of(nb, struct drm_nouveau_private, engine.pm.acpi_nb);
828 struct drm_device *dev = dev_priv->dev;
829 struct acpi_bus_event *entry = (struct acpi_bus_event *)data;
831 if (strcmp(entry->device_class, "ac_adapter") == 0) {
832 bool ac = power_supply_is_system_supplied();
834 NV_DEBUG(dev, "power supply changed: %s\n", ac ? "AC" : "DC");
835 nouveau_pm_trigger(dev);
843 nouveau_pm_init(struct drm_device *dev)
845 struct drm_nouveau_private *dev_priv = dev->dev_private;
846 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
850 /* parse aux tables from vbios */
851 nouveau_volt_init(dev);
852 nouveau_temp_init(dev);
854 /* determine current ("boot") performance level */
855 ret = nouveau_pm_perflvl_get(dev, &pm->boot);
857 NV_ERROR(dev, "failed to determine boot perflvl\n");
861 strncpy(pm->boot.name, "boot", 4);
862 strncpy(pm->boot.profile.name, "boot", 4);
863 pm->boot.profile.func = &nouveau_pm_static_profile_func;
865 INIT_LIST_HEAD(&pm->profiles);
866 list_add(&pm->boot.profile.head, &pm->profiles);
868 pm->profile_ac = &pm->boot.profile;
869 pm->profile_dc = &pm->boot.profile;
870 pm->profile = &pm->boot.profile;
873 /* add performance levels from vbios */
874 nouveau_perf_init(dev);
876 /* display available performance levels */
877 NV_INFO(dev, "%d available performance level(s)\n", pm->nr_perflvl);
878 for (i = 0; i < pm->nr_perflvl; i++) {
879 nouveau_pm_perflvl_info(&pm->perflvl[i], info, sizeof(info));
880 NV_INFO(dev, "%d:%s", pm->perflvl[i].id, info);
883 nouveau_pm_perflvl_info(&pm->boot, info, sizeof(info));
884 NV_INFO(dev, "c:%s", info);
886 /* switch performance levels now if requested */
887 if (nouveau_perflvl != NULL)
888 nouveau_pm_profile_set(dev, nouveau_perflvl);
890 /* determine the current fan speed */
891 pm->fan.percent = nouveau_pwmfan_get(dev);
893 nouveau_sysfs_init(dev);
894 nouveau_hwmon_init(dev);
895 #if defined(CONFIG_ACPI) && defined(CONFIG_POWER_SUPPLY)
896 pm->acpi_nb.notifier_call = nouveau_pm_acpi_event;
897 register_acpi_notifier(&pm->acpi_nb);
904 nouveau_pm_fini(struct drm_device *dev)
906 struct drm_nouveau_private *dev_priv = dev->dev_private;
907 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
908 struct nouveau_pm_profile *profile, *tmp;
910 list_for_each_entry_safe(profile, tmp, &pm->profiles, head) {
911 list_del(&profile->head);
912 profile->func->destroy(profile);
915 if (pm->cur != &pm->boot)
916 nouveau_pm_perflvl_set(dev, &pm->boot);
918 nouveau_temp_fini(dev);
919 nouveau_perf_fini(dev);
920 nouveau_volt_fini(dev);
922 #if defined(CONFIG_ACPI) && defined(CONFIG_POWER_SUPPLY)
923 unregister_acpi_notifier(&pm->acpi_nb);
925 nouveau_hwmon_fini(dev);
926 nouveau_sysfs_fini(dev);
930 nouveau_pm_resume(struct drm_device *dev)
932 struct drm_nouveau_private *dev_priv = dev->dev_private;
933 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
934 struct nouveau_pm_level *perflvl;
936 if (!pm->cur || pm->cur == &pm->boot)
941 nouveau_pm_perflvl_set(dev, perflvl);
942 nouveau_pwmfan_set(dev, pm->fan.percent);