/*
* Copyright 2010 Red Hat Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* Authors: Ben Skeggs
*/
#ifdef CONFIG_ACPI
#include <linux/acpi.h>
#endif
#include <linux/power_supply.h>
#include <linux/hwmon.h>
#include <linux/hwmon-sysfs.h>
#include <drm/drmP.h>
#include "nouveau_drm.h"
#include "nouveau_pm.h"
#include <subdev/gpio.h>
#include <subdev/timer.h>
#include <subdev/therm.h>
MODULE_PARM_DESC(perflvl, "Performance level (default: boot)");
static char *nouveau_perflvl;
module_param_named(perflvl, nouveau_perflvl, charp, 0400);
MODULE_PARM_DESC(perflvl_wr, "Allow perflvl changes (warning: dangerous!)");
static int nouveau_perflvl_wr;
module_param_named(perflvl_wr, nouveau_perflvl_wr, int, 0400);
static int
nouveau_pm_perflvl_aux(struct drm_device *dev, struct nouveau_pm_level *perflvl,
struct nouveau_pm_level *a, struct nouveau_pm_level *b)
{
struct nouveau_drm *drm = nouveau_drm(dev);
struct nouveau_pm *pm = nouveau_pm(dev);
struct nouveau_therm *therm = nouveau_therm(drm);
int ret;
/*XXX: not on all boards, we should control based on temperature
* on recent boards.. or maybe on some other factor we don't
* know about?
*/
if (therm && therm->fan_set &&
a->fanspeed && b->fanspeed && b->fanspeed > a->fanspeed) {
ret = therm->fan_set(therm, perflvl->fanspeed);
if (ret && ret != -ENODEV) {
NV_ERROR(drm, "fanspeed set failed: %d\n", ret);
return ret;
}
}
if (pm->voltage.supported && pm->voltage_set) {
if (perflvl->volt_min && b->volt_min > a->volt_min) {
ret = pm->voltage_set(dev, perflvl->volt_min);
if (ret) {
NV_ERROR(drm, "voltage set failed: %d\n", ret);
return ret;
}
}
}
return 0;
}
static int
nouveau_pm_perflvl_set(struct drm_device *dev, struct nouveau_pm_level *perflvl)
{
struct nouveau_pm *pm = nouveau_pm(dev);
void *state;
int ret;
if (perflvl == pm->cur)
return 0;
ret = nouveau_pm_perflvl_aux(dev, perflvl, pm->cur, perflvl);
if (ret)
return ret;
state = pm->clocks_pre(dev, perflvl);
if (IS_ERR(state)) {
ret = PTR_ERR(state);
goto error;
}
ret = pm->clocks_set(dev, state);
if (ret)
goto error;
ret = nouveau_pm_perflvl_aux(dev, perflvl, perflvl, pm->cur);
if (ret)
return ret;
pm->cur = perflvl;
return 0;
error:
/* restore the fan speed and voltage before leaving */
nouveau_pm_perflvl_aux(dev, perflvl, perflvl, pm->cur);
return ret;
}
void
nouveau_pm_trigger(struct drm_device *dev)
{
struct nouveau_drm *drm = nouveau_drm(dev);
struct nouveau_timer *ptimer = nouveau_timer(drm->device);
struct nouveau_pm *pm = nouveau_pm(dev);
struct nouveau_pm_profile *profile = NULL;
struct nouveau_pm_level *perflvl = NULL;
int ret;
/* select power profile based on current power source */
if (power_supply_is_system_supplied())
profile = pm->profile_ac;
else
profile = pm->profile_dc;
if (profile != pm->profile) {
pm->profile->func->fini(pm->profile);
pm->profile = profile;
pm->profile->func->init(pm->profile);
}
/* select performance level based on profile */
perflvl = profile->func->select(profile);
/* change perflvl, if necessary */
if (perflvl != pm->cur) {
u64 time0 = ptimer->read(ptimer);
NV_INFO(drm, "setting performance level: %d", perflvl->id);
ret = nouveau_pm_perflvl_set(dev, perflvl);
if (ret)
NV_INFO(drm, "> reclocking failed: %d\n\n", ret);
NV_INFO