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.
26 #include <linux/acpi.h>
28 #include <linux/power_supply.h>
29 #include <linux/hwmon.h>
30 #include <linux/hwmon-sysfs.h>
34 #include "nouveau_drm.h"
35 #include "nouveau_pm.h"
37 #include <subdev/bios/gpio.h>
38 #include <subdev/gpio.h>
39 #include <subdev/timer.h>
41 MODULE_PARM_DESC(perflvl, "Performance level (default: boot)");
42 static char *nouveau_perflvl;
43 module_param_named(perflvl, nouveau_perflvl, charp, 0400);
45 MODULE_PARM_DESC(perflvl_wr, "Allow perflvl changes (warning: dangerous!)");
46 static int nouveau_perflvl_wr;
47 module_param_named(perflvl_wr, nouveau_perflvl_wr, int, 0400);
50 nouveau_pwmfan_get(struct drm_device *dev)
52 struct nouveau_pm *pm = nouveau_pm(dev);
53 struct nouveau_drm *drm = nouveau_drm(dev);
54 struct nouveau_device *device = nv_device(drm->device);
55 struct nouveau_gpio *gpio = nouveau_gpio(device);
56 struct dcb_gpio_func func;
63 ret = gpio->find(gpio, 0, DCB_GPIO_PWM_FAN, 0xff, &func);
65 ret = pm->pwm_get(dev, func.line, &divs, &duty);
66 if (ret == 0 && divs) {
67 divs = max(divs, duty);
68 if (device->card_type <= NV_40 || (func.log[0] & 1))
70 return (duty * 100) / divs;
73 return gpio->get(gpio, 0, func.func, func.line) * 100;
80 nouveau_pwmfan_set(struct drm_device *dev, int percent)
82 struct nouveau_pm *pm = nouveau_pm(dev);
83 struct nouveau_drm *drm = nouveau_drm(dev);
84 struct nouveau_device *device = nv_device(drm->device);
85 struct nouveau_gpio *gpio = nouveau_gpio(device);
86 struct dcb_gpio_func func;
93 ret = gpio->find(gpio, 0, DCB_GPIO_PWM_FAN, 0xff, &func);
95 divs = pm->fan.pwm_divisor;
96 if (pm->fan.pwm_freq) {
97 /*XXX: PNVIO clock more than likely... */
98 divs = 135000 / pm->fan.pwm_freq;
99 if (nv_device(drm->device)->chipset < 0xa3)
103 duty = ((divs * percent) + 99) / 100;
104 if (device->card_type <= NV_40 || (func.log[0] & 1))
107 ret = pm->pwm_set(dev, func.line, divs, duty);
109 pm->fan.percent = percent;
117 nouveau_pm_perflvl_aux(struct drm_device *dev, struct nouveau_pm_level *perflvl,
118 struct nouveau_pm_level *a, struct nouveau_pm_level *b)
120 struct nouveau_drm *drm = nouveau_drm(dev);
121 struct nouveau_pm *pm = nouveau_pm(dev);
124 /*XXX: not on all boards, we should control based on temperature
125 * on recent boards.. or maybe on some other factor we don't
128 if (a->fanspeed && b->fanspeed && b->fanspeed > a->fanspeed) {
129 ret = nouveau_pwmfan_set(dev, perflvl->fanspeed);
130 if (ret && ret != -ENODEV) {
131 NV_ERROR(drm, "fanspeed set failed: %d\n", ret);
136 if (pm->voltage.supported && pm->voltage_set) {
137 if (perflvl->volt_min && b->volt_min > a->volt_min) {
138 ret = pm->voltage_set(dev, perflvl->volt_min);
140 NV_ERROR(drm, "voltage set failed: %d\n", ret);
150 nouveau_pm_perflvl_set(struct drm_device *dev, struct nouveau_pm_level *perflvl)
152 struct nouveau_pm *pm = nouveau_pm(dev);
156 if (perflvl == pm->cur)
159 ret = nouveau_pm_perflvl_aux(dev, perflvl, pm->cur, perflvl);
163 state = pm->clocks_pre(dev, perflvl);
165 ret = PTR_ERR(state);
168 ret = pm->clocks_set(dev, state);
172 ret = nouveau_pm_perflvl_aux(dev, perflvl, perflvl, pm->cur);
180 /* restore the fan speed and voltage before leaving */
181 nouveau_pm_perflvl_aux(dev, perflvl, perflvl, pm->cur);
186 nouveau_pm_trigger(struct drm_device *dev)
188 struct nouveau_drm *drm = nouveau_drm(dev);
189 struct nouveau_timer *ptimer = nouveau_timer(drm->device);
190 struct nouveau_pm *pm = nouveau_pm(dev);
191 struct nouveau_pm_profile *profile = NULL;
192 struct nouveau_pm_level *perflvl = NULL;
195 /* select power profile based on current power source */
196 if (power_supply_is_system_supplied())
197 profile = pm->profile_ac;
199 profile = pm->profile_dc;
201 if (profile != pm->profile) {
202 pm->profile->func->fini(pm->profile);
203 pm->profile = profile;
204 pm->profile->func->init(pm->profile);
207 /* select performance level based on profile */
208 perflvl = profile->func->select(profile);
210 /* change perflvl, if necessary */
211 if (perflvl != pm->cur) {
212 u64 time0 = ptimer->read(ptimer);
214 NV_INFO(drm, "setting performance level: %d", perflvl->id);
215 ret = nouveau_pm_perflvl_set(dev, perflvl);
217 NV_INFO(drm, "> reclocking failed: %d\n\n", ret);
219 NV_INFO(drm, "> reclocking took %lluns\n\n",
220 ptimer->read(ptimer) - time0);
224 static struct nouveau_pm_profile *
225 profile_find(struct drm_device *dev, const char *string)
227 struct nouveau_pm *pm = nouveau_pm(dev);
228 struct nouveau_pm_profile *profile;
230 list_for_each_entry(profile, &pm->profiles, head) {
231 if (!strncmp(profile->name, string, sizeof(profile->name)))
239 nouveau_pm_profile_set(struct drm_device *dev, const char *profile)
241 struct nouveau_pm *pm = nouveau_pm(dev);
242 struct nouveau_pm_profile *ac = NULL, *dc = NULL;
243 char string[16], *cur = string, *ptr;
245 /* safety precaution, for now */
246 if (nouveau_perflvl_wr != 7777)
249 strncpy(string, profile, sizeof(string));
250 string[sizeof(string) - 1] = 0;
251 if ((ptr = strchr(string, '\n')))
254 ptr = strsep(&cur, ",");
256 ac = profile_find(dev, ptr);
258 ptr = strsep(&cur, ",");
260 dc = profile_find(dev, ptr);
264 if (ac == NULL || dc == NULL)
269 nouveau_pm_trigger(dev);
274 nouveau_pm_static_dummy(struct nouveau_pm_profile *profile)
278 static struct nouveau_pm_level *
279 nouveau_pm_static_select(struct nouveau_pm_profile *profile)
281 return container_of(profile, struct nouveau_pm_level, profile);
284 const struct nouveau_pm_profile_func nouveau_pm_static_profile_func = {
285 .destroy = nouveau_pm_static_dummy,
286 .init = nouveau_pm_static_dummy,
287 .fini = nouveau_pm_static_dummy,
288 .select = nouveau_pm_static_select,
292 nouveau_pm_perflvl_get(struct drm_device *dev, struct nouveau_pm_level *perflvl)
294 struct nouveau_pm *pm = nouveau_pm(dev);
297 memset(perflvl, 0, sizeof(*perflvl));
299 if (pm->clocks_get) {
300 ret = pm->clocks_get(dev, perflvl);
305 if (pm->voltage.supported && pm->voltage_get) {
306 ret = pm->voltage_get(dev);
308 perflvl->volt_min = ret;
309 perflvl->volt_max = ret;
313 ret = nouveau_pwmfan_get(dev);
315 perflvl->fanspeed = ret;
317 nouveau_mem_timing_read(dev, &perflvl->timing);
322 nouveau_pm_perflvl_info(struct nouveau_pm_level *perflvl, char *ptr, int len)
324 char c[16], s[16], v[32], f[16], m[16];
328 snprintf(c, sizeof(c), " core %dMHz", perflvl->core / 1000);
332 snprintf(s, sizeof(s), " shader %dMHz", perflvl->shader / 1000);
336 snprintf(m, sizeof(m), " memory %dMHz", perflvl->memory / 1000);
339 if (perflvl->volt_min && perflvl->volt_min != perflvl->volt_max) {
340 snprintf(v, sizeof(v), " voltage %dmV-%dmV",
341 perflvl->volt_min / 1000, perflvl->volt_max / 1000);
343 if (perflvl->volt_min) {
344 snprintf(v, sizeof(v), " voltage %dmV",
345 perflvl->volt_min / 1000);
349 if (perflvl->fanspeed)
350 snprintf(f, sizeof(f), " fanspeed %d%%", perflvl->fanspeed);
352 snprintf(ptr, len, "%s%s%s%s%s\n", c, s, m, v, f);
356 nouveau_pm_get_perflvl_info(struct device *d,
357 struct device_attribute *a, char *buf)
359 struct nouveau_pm_level *perflvl =
360 container_of(a, struct nouveau_pm_level, dev_attr);
364 snprintf(ptr, len, "%d:", perflvl->id);
368 nouveau_pm_perflvl_info(perflvl, ptr, len);
373 nouveau_pm_get_perflvl(struct device *d, struct device_attribute *a, char *buf)
375 struct drm_device *dev = pci_get_drvdata(to_pci_dev(d));
376 struct nouveau_pm *pm = nouveau_pm(dev);
377 struct nouveau_pm_level cur;
378 int len = PAGE_SIZE, ret;
381 snprintf(ptr, len, "profile: %s, %s\nc:",
382 pm->profile_ac->name, pm->profile_dc->name);
386 ret = nouveau_pm_perflvl_get(dev, &cur);
388 nouveau_pm_perflvl_info(&cur, ptr, len);
393 nouveau_pm_set_perflvl(struct device *d, struct device_attribute *a,
394 const char *buf, size_t count)
396 struct drm_device *dev = pci_get_drvdata(to_pci_dev(d));
399 ret = nouveau_pm_profile_set(dev, buf);
405 static DEVICE_ATTR(performance_level, S_IRUGO | S_IWUSR,
406 nouveau_pm_get_perflvl, nouveau_pm_set_perflvl);
409 nouveau_sysfs_init(struct drm_device *dev)
411 struct nouveau_drm *drm = nouveau_drm(dev);
412 struct nouveau_pm *pm = nouveau_pm(dev);
413 struct device *d = &dev->pdev->dev;
416 ret = device_create_file(d, &dev_attr_performance_level);
420 for (i = 0; i < pm->nr_perflvl; i++) {
421 struct nouveau_pm_level *perflvl = &pm->perflvl[i];
423 perflvl->dev_attr.attr.name = perflvl->name;
424 perflvl->dev_attr.attr.mode = S_IRUGO;
425 perflvl->dev_attr.show = nouveau_pm_get_perflvl_info;
426 perflvl->dev_attr.store = NULL;
427 sysfs_attr_init(&perflvl->dev_attr.attr);
429 ret = device_create_file(d, &perflvl->dev_attr);
431 NV_ERROR(drm, "failed pervlvl %d sysfs: %d\n",
433 perflvl->dev_attr.attr.name = NULL;
434 nouveau_pm_fini(dev);
443 nouveau_sysfs_fini(struct drm_device *dev)
445 struct nouveau_pm *pm = nouveau_pm(dev);
446 struct device *d = &dev->pdev->dev;
449 device_remove_file(d, &dev_attr_performance_level);
450 for (i = 0; i < pm->nr_perflvl; i++) {
451 struct nouveau_pm_level *pl = &pm->perflvl[i];
453 if (!pl->dev_attr.attr.name)
456 device_remove_file(d, &pl->dev_attr);
460 #if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
462 nouveau_hwmon_show_temp(struct device *d, struct device_attribute *a, char *buf)
464 struct drm_device *dev = dev_get_drvdata(d);
465 struct nouveau_pm *pm = nouveau_pm(dev);
467 return snprintf(buf, PAGE_SIZE, "%d\n", pm->temp_get(dev)*1000);
469 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, nouveau_hwmon_show_temp,
473 nouveau_hwmon_max_temp(struct device *d, struct device_attribute *a, char *buf)
475 struct drm_device *dev = dev_get_drvdata(d);
476 struct nouveau_pm *pm = nouveau_pm(dev);
477 struct nouveau_pm_threshold_temp *temp = &pm->threshold_temp;
479 return snprintf(buf, PAGE_SIZE, "%d\n", temp->down_clock*1000);
482 nouveau_hwmon_set_max_temp(struct device *d, struct device_attribute *a,
483 const char *buf, size_t count)
485 struct drm_device *dev = dev_get_drvdata(d);
486 struct nouveau_pm *pm = nouveau_pm(dev);
487 struct nouveau_pm_threshold_temp *temp = &pm->threshold_temp;
490 if (kstrtol(buf, 10, &value) == -EINVAL)
493 temp->down_clock = value/1000;
495 nouveau_temp_safety_checks(dev);
499 static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR, nouveau_hwmon_max_temp,
500 nouveau_hwmon_set_max_temp,
504 nouveau_hwmon_critical_temp(struct device *d, struct device_attribute *a,
507 struct drm_device *dev = dev_get_drvdata(d);
508 struct nouveau_pm *pm = nouveau_pm(dev);
509 struct nouveau_pm_threshold_temp *temp = &pm->threshold_temp;
511 return snprintf(buf, PAGE_SIZE, "%d\n", temp->critical*1000);
514 nouveau_hwmon_set_critical_temp(struct device *d, struct device_attribute *a,
518 struct drm_device *dev = dev_get_drvdata(d);
519 struct nouveau_pm *pm = nouveau_pm(dev);
520 struct nouveau_pm_threshold_temp *temp = &pm->threshold_temp;
523 if (kstrtol(buf, 10, &value) == -EINVAL)
526 temp->critical = value/1000;
528 nouveau_temp_safety_checks(dev);
532 static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO | S_IWUSR,
533 nouveau_hwmon_critical_temp,
534 nouveau_hwmon_set_critical_temp,
537 static ssize_t nouveau_hwmon_show_name(struct device *dev,
538 struct device_attribute *attr,
541 return sprintf(buf, "nouveau\n");
543 static SENSOR_DEVICE_ATTR(name, S_IRUGO, nouveau_hwmon_show_name, NULL, 0);
545 static ssize_t nouveau_hwmon_show_update_rate(struct device *dev,
546 struct device_attribute *attr,
549 return sprintf(buf, "1000\n");
551 static SENSOR_DEVICE_ATTR(update_rate, S_IRUGO,
552 nouveau_hwmon_show_update_rate,
556 nouveau_hwmon_show_fan0_input(struct device *d, struct device_attribute *attr,
559 struct drm_device *dev = dev_get_drvdata(d);
560 struct nouveau_drm *drm = nouveau_drm(dev);
561 struct nouveau_timer *ptimer = nouveau_timer(drm->device);
562 struct nouveau_gpio *gpio = nouveau_gpio(drm->device);
563 struct dcb_gpio_func func;
564 u32 cycles, cur, prev;
567 if (gpio->find(gpio, 0, DCB_GPIO_FAN_SENSE, 0xff, &func))
570 /* Monitor the GPIO input 0x3b for 250ms.
571 * When the fan spins, it changes the value of GPIO FAN_SENSE.
572 * We get 4 changes (0 -> 1 -> 0 -> 1 -> [...]) per complete rotation.
574 start = ptimer->read(ptimer);
575 prev = gpio->get(gpio, 0, func.func, func.line);
578 cur = gpio->get(gpio, 0, func.func, func.line);
584 usleep_range(500, 1000); /* supports 0 < rpm < 7500 */
585 } while (ptimer->read(ptimer) - start < 250000000);
587 /* interpolate to get rpm */
588 return sprintf(buf, "%i\n", cycles / 4 * 4 * 60);
590 static SENSOR_DEVICE_ATTR(fan0_input, S_IRUGO, nouveau_hwmon_show_fan0_input,
594 nouveau_hwmon_get_pwm0(struct device *d, struct device_attribute *a, char *buf)
596 struct drm_device *dev = dev_get_drvdata(d);
599 ret = nouveau_pwmfan_get(dev);
603 return sprintf(buf, "%i\n", ret);
607 nouveau_hwmon_set_pwm0(struct device *d, struct device_attribute *a,
608 const char *buf, size_t count)
610 struct drm_device *dev = dev_get_drvdata(d);
611 struct nouveau_pm *pm = nouveau_pm(dev);
615 if (nouveau_perflvl_wr != 7777)
618 if (kstrtol(buf, 10, &value) == -EINVAL)
621 if (value < pm->fan.min_duty)
622 value = pm->fan.min_duty;
623 if (value > pm->fan.max_duty)
624 value = pm->fan.max_duty;
626 ret = nouveau_pwmfan_set(dev, value);
633 static SENSOR_DEVICE_ATTR(pwm0, S_IRUGO | S_IWUSR,
634 nouveau_hwmon_get_pwm0,
635 nouveau_hwmon_set_pwm0, 0);
638 nouveau_hwmon_get_pwm0_min(struct device *d,
639 struct device_attribute *a, char *buf)
641 struct drm_device *dev = dev_get_drvdata(d);
642 struct nouveau_pm *pm = nouveau_pm(dev);
644 return sprintf(buf, "%i\n", pm->fan.min_duty);
648 nouveau_hwmon_set_pwm0_min(struct device *d, struct device_attribute *a,
649 const char *buf, size_t count)
651 struct drm_device *dev = dev_get_drvdata(d);
652 struct nouveau_pm *pm = nouveau_pm(dev);
655 if (kstrtol(buf, 10, &value) == -EINVAL)
661 if (pm->fan.max_duty - value < 10)
662 value = pm->fan.max_duty - 10;
665 pm->fan.min_duty = 10;
667 pm->fan.min_duty = value;
672 static SENSOR_DEVICE_ATTR(pwm0_min, S_IRUGO | S_IWUSR,
673 nouveau_hwmon_get_pwm0_min,
674 nouveau_hwmon_set_pwm0_min, 0);
677 nouveau_hwmon_get_pwm0_max(struct device *d,
678 struct device_attribute *a, char *buf)
680 struct drm_device *dev = dev_get_drvdata(d);
681 struct nouveau_pm *pm = nouveau_pm(dev);
683 return sprintf(buf, "%i\n", pm->fan.max_duty);
687 nouveau_hwmon_set_pwm0_max(struct device *d, struct device_attribute *a,
688 const char *buf, size_t count)
690 struct drm_device *dev = dev_get_drvdata(d);
691 struct nouveau_pm *pm = nouveau_pm(dev);
694 if (kstrtol(buf, 10, &value) == -EINVAL)
700 if (value - pm->fan.min_duty < 10)
701 value = pm->fan.min_duty + 10;
704 pm->fan.max_duty = 100;
706 pm->fan.max_duty = value;
711 static SENSOR_DEVICE_ATTR(pwm0_max, S_IRUGO | S_IWUSR,
712 nouveau_hwmon_get_pwm0_max,
713 nouveau_hwmon_set_pwm0_max, 0);
715 static struct attribute *hwmon_attributes[] = {
716 &sensor_dev_attr_temp1_input.dev_attr.attr,
717 &sensor_dev_attr_temp1_max.dev_attr.attr,
718 &sensor_dev_attr_temp1_crit.dev_attr.attr,
719 &sensor_dev_attr_name.dev_attr.attr,
720 &sensor_dev_attr_update_rate.dev_attr.attr,
723 static struct attribute *hwmon_fan_rpm_attributes[] = {
724 &sensor_dev_attr_fan0_input.dev_attr.attr,
727 static struct attribute *hwmon_pwm_fan_attributes[] = {
728 &sensor_dev_attr_pwm0.dev_attr.attr,
729 &sensor_dev_attr_pwm0_min.dev_attr.attr,
730 &sensor_dev_attr_pwm0_max.dev_attr.attr,
734 static const struct attribute_group hwmon_attrgroup = {
735 .attrs = hwmon_attributes,
737 static const struct attribute_group hwmon_fan_rpm_attrgroup = {
738 .attrs = hwmon_fan_rpm_attributes,
740 static const struct attribute_group hwmon_pwm_fan_attrgroup = {
741 .attrs = hwmon_pwm_fan_attributes,
746 nouveau_hwmon_init(struct drm_device *dev)
748 struct nouveau_pm *pm = nouveau_pm(dev);
749 struct nouveau_drm *drm = nouveau_drm(dev);
750 struct nouveau_gpio *gpio = nouveau_gpio(drm->device);
751 struct dcb_gpio_func func;
753 #if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
754 struct device *hwmon_dev;
760 hwmon_dev = hwmon_device_register(&dev->pdev->dev);
761 if (IS_ERR(hwmon_dev)) {
762 ret = PTR_ERR(hwmon_dev);
763 NV_ERROR(drm, "Unable to register hwmon device: %d\n", ret);
766 dev_set_drvdata(hwmon_dev, dev);
768 /* default sysfs entries */
769 ret = sysfs_create_group(&dev->pdev->dev.kobj, &hwmon_attrgroup);
775 /* if the card has a pwm fan */
776 /*XXX: incorrect, need better detection for this, some boards have
777 * the gpio entries for pwm fan control even when there's no
778 * actual fan connected to it... therm table? */
779 if (nouveau_pwmfan_get(dev) >= 0) {
780 ret = sysfs_create_group(&dev->pdev->dev.kobj,
781 &hwmon_pwm_fan_attrgroup);
786 /* if the card can read the fan rpm */
787 if (!gpio->find(gpio, 0, DCB_GPIO_FAN_SENSE, 0xff, &func)) {
788 ret = sysfs_create_group(&dev->pdev->dev.kobj,
789 &hwmon_fan_rpm_attrgroup);
794 pm->hwmon = hwmon_dev;
799 NV_ERROR(drm, "Unable to create some hwmon sysfs files: %d\n", ret);
800 hwmon_device_unregister(hwmon_dev);
810 nouveau_hwmon_fini(struct drm_device *dev)
812 #if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
813 struct nouveau_pm *pm = nouveau_pm(dev);
816 sysfs_remove_group(&dev->pdev->dev.kobj, &hwmon_attrgroup);
817 sysfs_remove_group(&dev->pdev->dev.kobj,
818 &hwmon_pwm_fan_attrgroup);
819 sysfs_remove_group(&dev->pdev->dev.kobj,
820 &hwmon_fan_rpm_attrgroup);
822 hwmon_device_unregister(pm->hwmon);
827 #if defined(CONFIG_ACPI) && defined(CONFIG_POWER_SUPPLY)
829 nouveau_pm_acpi_event(struct notifier_block *nb, unsigned long val, void *data)
831 struct nouveau_pm *pm = container_of(nb, struct nouveau_pm, acpi_nb);
832 struct nouveau_drm *drm = nouveau_drm(pm->dev);
833 struct acpi_bus_event *entry = (struct acpi_bus_event *)data;
835 if (strcmp(entry->device_class, "ac_adapter") == 0) {
836 bool ac = power_supply_is_system_supplied();
838 NV_DEBUG(drm, "power supply changed: %s\n", ac ? "AC" : "DC");
839 nouveau_pm_trigger(pm->dev);
847 nouveau_pm_init(struct drm_device *dev)
849 struct nouveau_device *device = nouveau_dev(dev);
850 struct nouveau_drm *drm = nouveau_drm(dev);
851 struct nouveau_pm *pm;
855 pm = drm->pm = kzalloc(sizeof(*pm), GFP_KERNEL);
861 if (device->card_type < NV_40) {
862 pm->clocks_get = nv04_pm_clocks_get;
863 pm->clocks_pre = nv04_pm_clocks_pre;
864 pm->clocks_set = nv04_pm_clocks_set;
865 if (nouveau_gpio(drm->device)) {
866 pm->voltage_get = nouveau_voltage_gpio_get;
867 pm->voltage_set = nouveau_voltage_gpio_set;
870 if (device->card_type < NV_50) {
871 pm->clocks_get = nv40_pm_clocks_get;
872 pm->clocks_pre = nv40_pm_clocks_pre;
873 pm->clocks_set = nv40_pm_clocks_set;
874 pm->voltage_get = nouveau_voltage_gpio_get;
875 pm->voltage_set = nouveau_voltage_gpio_set;
876 pm->temp_get = nv40_temp_get;
877 pm->pwm_get = nv40_pm_pwm_get;
878 pm->pwm_set = nv40_pm_pwm_set;
880 if (device->card_type < NV_C0) {
881 if (device->chipset < 0xa3 ||
882 device->chipset == 0xaa ||
883 device->chipset == 0xac) {
884 pm->clocks_get = nv50_pm_clocks_get;
885 pm->clocks_pre = nv50_pm_clocks_pre;
886 pm->clocks_set = nv50_pm_clocks_set;
888 pm->clocks_get = nva3_pm_clocks_get;
889 pm->clocks_pre = nva3_pm_clocks_pre;
890 pm->clocks_set = nva3_pm_clocks_set;
892 pm->voltage_get = nouveau_voltage_gpio_get;
893 pm->voltage_set = nouveau_voltage_gpio_set;
894 if (device->chipset == 0x50)
895 pm->temp_get = nv40_temp_get;
897 pm->temp_get = nv84_temp_get;
898 pm->pwm_get = nv50_pm_pwm_get;
899 pm->pwm_set = nv50_pm_pwm_set;
901 if (device->card_type < NV_E0) {
902 pm->clocks_get = nvc0_pm_clocks_get;
903 pm->clocks_pre = nvc0_pm_clocks_pre;
904 pm->clocks_set = nvc0_pm_clocks_set;
905 pm->voltage_get = nouveau_voltage_gpio_get;
906 pm->voltage_set = nouveau_voltage_gpio_set;
907 pm->temp_get = nv84_temp_get;
908 if (device->card_type < NV_D0) {
909 pm->pwm_get = nv50_pm_pwm_get;
910 pm->pwm_set = nv50_pm_pwm_set;
915 /* parse aux tables from vbios */
916 nouveau_volt_init(dev);
917 nouveau_temp_init(dev);
919 /* determine current ("boot") performance level */
920 ret = nouveau_pm_perflvl_get(dev, &pm->boot);
922 NV_ERROR(drm, "failed to determine boot perflvl\n");
926 strncpy(pm->boot.name, "boot", 4);
927 strncpy(pm->boot.profile.name, "boot", 4);
928 pm->boot.profile.func = &nouveau_pm_static_profile_func;
930 INIT_LIST_HEAD(&pm->profiles);
931 list_add(&pm->boot.profile.head, &pm->profiles);
933 pm->profile_ac = &pm->boot.profile;
934 pm->profile_dc = &pm->boot.profile;
935 pm->profile = &pm->boot.profile;
938 /* add performance levels from vbios */
939 nouveau_perf_init(dev);
941 /* display available performance levels */
942 NV_INFO(drm, "%d available performance level(s)\n", pm->nr_perflvl);
943 for (i = 0; i < pm->nr_perflvl; i++) {
944 nouveau_pm_perflvl_info(&pm->perflvl[i], info, sizeof(info));
945 NV_INFO(drm, "%d:%s", pm->perflvl[i].id, info);
948 nouveau_pm_perflvl_info(&pm->boot, info, sizeof(info));
949 NV_INFO(drm, "c:%s", info);
951 /* switch performance levels now if requested */
952 if (nouveau_perflvl != NULL)
953 nouveau_pm_profile_set(dev, nouveau_perflvl);
955 /* determine the current fan speed */
956 pm->fan.percent = nouveau_pwmfan_get(dev);
958 nouveau_sysfs_init(dev);
959 nouveau_hwmon_init(dev);
960 #if defined(CONFIG_ACPI) && defined(CONFIG_POWER_SUPPLY)
961 pm->acpi_nb.notifier_call = nouveau_pm_acpi_event;
962 register_acpi_notifier(&pm->acpi_nb);
969 nouveau_pm_fini(struct drm_device *dev)
971 struct nouveau_pm *pm = nouveau_pm(dev);
972 struct nouveau_pm_profile *profile, *tmp;
974 list_for_each_entry_safe(profile, tmp, &pm->profiles, head) {
975 list_del(&profile->head);
976 profile->func->destroy(profile);
979 if (pm->cur != &pm->boot)
980 nouveau_pm_perflvl_set(dev, &pm->boot);
982 nouveau_temp_fini(dev);
983 nouveau_perf_fini(dev);
984 nouveau_volt_fini(dev);
986 #if defined(CONFIG_ACPI) && defined(CONFIG_POWER_SUPPLY)
987 unregister_acpi_notifier(&pm->acpi_nb);
989 nouveau_hwmon_fini(dev);
990 nouveau_sysfs_fini(dev);
992 nouveau_drm(dev)->pm = NULL;
997 nouveau_pm_resume(struct drm_device *dev)
999 struct nouveau_pm *pm = nouveau_pm(dev);
1000 struct nouveau_pm_level *perflvl;
1002 if (!pm->cur || pm->cur == &pm->boot)
1006 pm->cur = &pm->boot;
1007 nouveau_pm_perflvl_set(dev, perflvl);
1008 nouveau_pwmfan_set(dev, pm->fan.percent);