Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 11b7d895 authored by Martin Peres's avatar Martin Peres Committed by Ben Skeggs
Browse files

drm/nouveau/pm: manual pwm fanspeed management for nv40+ boards



Exposes the following sysfs entries:
- fan0_input: read the rotational speed of the fan (poll a bit during 250ms)
- pwm0: set the pwm duty cycle
- pwm0_min/max: set the minimum/maximum pwm value

v2 (Ben Skeggs):
- nv50 pwm controller code removed in favour of other more complete code
- FAN_RPM -> FAN_SENSE
- merged FAN_SENSE readout into common code, not at all nv50-specific
- protected fanspeed changes with perflvl_wr
- formatting tidying
- added some comments where things are shaky

v3 (Martin Peres)
- ensure duty min/max from thermal table are sane

Signed-off-by: default avatarBen Skeggs <bskeggs@redhat.com>
Signed-off-by: default avatarMartin Peres <martin.peres@ensi-bourges.fr>
parent cb9fa626
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -58,6 +58,8 @@ struct dcb_i2c_entry {
enum dcb_gpio_tag {
	DCB_GPIO_TVDAC0 = 0xc,
	DCB_GPIO_TVDAC1 = 0x2d,
	DCB_GPIO_PWM_FAN = 0x9,
	DCB_GPIO_FAN_SENSE = 0x3d,
};

struct dcb_gpio_entry {
+6 −0
Original line number Diff line number Diff line
@@ -521,6 +521,11 @@ struct nouveau_pm_memtimings {
	int nr_timing;
};

struct nouveau_pm_fan {
	u32 min_duty;
	u32 max_duty;
};

struct nouveau_pm_engine {
	struct nouveau_pm_voltage voltage;
	struct nouveau_pm_level perflvl[NOUVEAU_PM_MAX_LEVEL];
@@ -528,6 +533,7 @@ struct nouveau_pm_engine {
	struct nouveau_pm_memtimings memtimings;
	struct nouveau_pm_temp_sensor_constants sensor_constants;
	struct nouveau_pm_threshold_temp threshold_temp;
	struct nouveau_pm_fan fan;
	u32 pwm_divisor;

	struct nouveau_pm_level boot;
+228 −6
Original line number Diff line number Diff line
@@ -64,6 +64,10 @@ nouveau_pm_perflvl_set(struct drm_device *dev, struct nouveau_pm_level *perflvl)
	if (perflvl == pm->cur)
		return 0;

	/*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 (pm->fanspeed_set && perflvl->fanspeed) {
		ret = pm->fanspeed_set(dev, perflvl->fanspeed);
		if (ret)
@@ -424,6 +428,176 @@ static SENSOR_DEVICE_ATTR(update_rate, S_IRUGO,
						nouveau_hwmon_show_update_rate,
						NULL, 0);

static ssize_t
nouveau_hwmon_show_fan0_input(struct device *d, struct device_attribute *attr,
			      char *buf)
{
	struct drm_device *dev = dev_get_drvdata(d);
	struct drm_nouveau_private *dev_priv = dev->dev_private;
	struct nouveau_timer_engine *ptimer = &dev_priv->engine.timer;
	struct nouveau_gpio_engine *pgpio = &dev_priv->engine.gpio;
	struct dcb_gpio_entry *gpio;
	u32 cycles, cur, prev;
	u64 start;

	gpio = nouveau_bios_gpio_entry(dev, DCB_GPIO_FAN_SENSE);
	if (!gpio)
		return -ENODEV;

	/* Monitor the GPIO input 0x3b for 250ms.
	 * When the fan spins, it changes the value of GPIO FAN_SENSE.
	 * We get 4 changes (0 -> 1 -> 0 -> 1 -> [...]) per complete rotation.
	 */
	start = ptimer->read(dev);
	prev = pgpio->get(dev, DCB_GPIO_FAN_SENSE);
	cycles = 0;
	do {
		cur = pgpio->get(dev, DCB_GPIO_FAN_SENSE);
		if (prev != cur) {
			cycles++;
			prev = cur;
		}

		usleep_range(500, 1000); /* supports 0 < rpm < 7500 */
	} while (ptimer->read(dev) - start < 250000000);

	/* interpolate to get rpm */
	return sprintf(buf, "%i\n", cycles / 4 * 4 * 60);
}
static SENSOR_DEVICE_ATTR(fan0_input, S_IRUGO, nouveau_hwmon_show_fan0_input,
			  NULL, 0);

static ssize_t
nouveau_hwmon_get_pwm0(struct device *d, struct device_attribute *a, char *buf)
{
	struct drm_device *dev = dev_get_drvdata(d);
	struct drm_nouveau_private *dev_priv = dev->dev_private;
	struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
	int ret = -ENODEV;

	if (pm->fanspeed_get)
		ret = pm->fanspeed_get(dev);
	if (ret < 0)
		return ret;

	return sprintf(buf, "%i\n", ret);
}

static ssize_t
nouveau_hwmon_set_pwm0(struct device *d, struct device_attribute *a,
		       const char *buf, size_t count)
{
	struct drm_device *dev = dev_get_drvdata(d);
	struct drm_nouveau_private *dev_priv = dev->dev_private;
	struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
	int ret = -ENODEV;
	long value;

	if (nouveau_perflvl_wr != 7777)
		return -EPERM;

	if (strict_strtol(buf, 10, &value) == -EINVAL)
		return -EINVAL;

	if (value < pm->fan.min_duty)
		value = pm->fan.min_duty;
	if (value > pm->fan.max_duty)
		value = pm->fan.max_duty;

	if (pm->fanspeed_set)
		ret = pm->fanspeed_set(dev, value);
	if (ret)
		return ret;

	return count;
}

static SENSOR_DEVICE_ATTR(pwm0, S_IRUGO | S_IWUSR,
			  nouveau_hwmon_get_pwm0,
			  nouveau_hwmon_set_pwm0, 0);

static ssize_t
nouveau_hwmon_get_pwm0_min(struct device *d,
			   struct device_attribute *a, char *buf)
{
	struct drm_device *dev = dev_get_drvdata(d);
	struct drm_nouveau_private *dev_priv = dev->dev_private;
	struct nouveau_pm_engine *pm = &dev_priv->engine.pm;

	return sprintf(buf, "%i\n", pm->fan.min_duty);
}

static ssize_t
nouveau_hwmon_set_pwm0_min(struct device *d, struct device_attribute *a,
			   const char *buf, size_t count)
{
	struct drm_device *dev = dev_get_drvdata(d);
	struct drm_nouveau_private *dev_priv = dev->dev_private;
	struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
	long value;

	if (strict_strtol(buf, 10, &value) == -EINVAL)
		return -EINVAL;

	if (value < 0)
		value = 0;

	if (pm->fan.max_duty - value < 10)
		value = pm->fan.max_duty - 10;

	if (value < 10)
		pm->fan.min_duty = 10;
	else
		pm->fan.min_duty = value;

	return count;
}

static SENSOR_DEVICE_ATTR(pwm0_min, S_IRUGO | S_IWUSR,
			  nouveau_hwmon_get_pwm0_min,
			  nouveau_hwmon_set_pwm0_min, 0);

static ssize_t
nouveau_hwmon_get_pwm0_max(struct device *d,
			   struct device_attribute *a, char *buf)
{
	struct drm_device *dev = dev_get_drvdata(d);
	struct drm_nouveau_private *dev_priv = dev->dev_private;
	struct nouveau_pm_engine *pm = &dev_priv->engine.pm;

	return sprintf(buf, "%i\n", pm->fan.max_duty);
}

static ssize_t
nouveau_hwmon_set_pwm0_max(struct device *d, struct device_attribute *a,
			   const char *buf, size_t count)
{
	struct drm_device *dev = dev_get_drvdata(d);
	struct drm_nouveau_private *dev_priv = dev->dev_private;
	struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
	long value;

	if (strict_strtol(buf, 10, &value) == -EINVAL)
		return -EINVAL;

	if (value < 0)
		value = 0;

	if (value - pm->fan.min_duty < 10)
		value = pm->fan.min_duty + 10;

	if (value > 100)
		pm->fan.max_duty = 100;
	else
		pm->fan.max_duty = value;

	return count;
}

static SENSOR_DEVICE_ATTR(pwm0_max, S_IRUGO | S_IWUSR,
			  nouveau_hwmon_get_pwm0_max,
			  nouveau_hwmon_set_pwm0_max, 0);

static struct attribute *hwmon_attributes[] = {
	&sensor_dev_attr_temp1_input.dev_attr.attr,
	&sensor_dev_attr_temp1_max.dev_attr.attr,
@@ -432,10 +606,26 @@ static struct attribute *hwmon_attributes[] = {
	&sensor_dev_attr_update_rate.dev_attr.attr,
	NULL
};
static struct attribute *hwmon_fan_rpm_attributes[] = {
	&sensor_dev_attr_fan0_input.dev_attr.attr,
	NULL
};
static struct attribute *hwmon_pwm_fan_attributes[] = {
	&sensor_dev_attr_pwm0.dev_attr.attr,
	&sensor_dev_attr_pwm0_min.dev_attr.attr,
	&sensor_dev_attr_pwm0_max.dev_attr.attr,
	NULL
};

static const struct attribute_group hwmon_attrgroup = {
	.attrs = hwmon_attributes,
};
static const struct attribute_group hwmon_fan_rpm_attrgroup = {
	.attrs = hwmon_fan_rpm_attributes,
};
static const struct attribute_group hwmon_pwm_fan_attrgroup = {
	.attrs = hwmon_pwm_fan_attributes,
};
#endif

static int
@@ -445,7 +635,7 @@ nouveau_hwmon_init(struct drm_device *dev)
	struct drm_nouveau_private *dev_priv = dev->dev_private;
	struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
	struct device *hwmon_dev;
	int ret;
	int ret = 0;

	if (!pm->temp_get)
		return -ENODEV;
@@ -458,17 +648,46 @@ nouveau_hwmon_init(struct drm_device *dev)
		return ret;
	}
	dev_set_drvdata(hwmon_dev, dev);

	/* default sysfs entries */
	ret = sysfs_create_group(&dev->pdev->dev.kobj, &hwmon_attrgroup);
	if (ret) {
		NV_ERROR(dev,
			"Unable to create hwmon sysfs file: %d\n", ret);
		hwmon_device_unregister(hwmon_dev);
		return ret;
		if (ret)
			goto error;
	}

	/* if the card has a pwm fan */
	/*XXX: incorrect, need better detection for this, some boards have
	 *     the gpio entries for pwm fan control even when there's no
	 *     actual fan connected to it... therm table? */
	if (pm->fanspeed_get && pm->fanspeed_get(dev) >= 0) {
		ret = sysfs_create_group(&dev->pdev->dev.kobj,
					 &hwmon_pwm_fan_attrgroup);
		if (ret)
			goto error;
	}

	/* if the card can read the fan rpm */
	if (nouveau_bios_gpio_entry(dev, DCB_GPIO_FAN_SENSE)) {
		ret = sysfs_create_group(&dev->pdev->dev.kobj,
					 &hwmon_fan_rpm_attrgroup);
		if (ret)
			goto error;
	}

	pm->hwmon = hwmon_dev;
#endif

	return 0;

error:
	NV_ERROR(dev, "Unable to create some hwmon sysfs files: %d\n", ret);
	hwmon_device_unregister(hwmon_dev);
	pm->hwmon = NULL;
	return ret;
#else
	pm->hwmon = NULL;
	return 0;
#endif
}

static void
@@ -480,6 +699,9 @@ nouveau_hwmon_fini(struct drm_device *dev)

	if (pm->hwmon) {
		sysfs_remove_group(&dev->pdev->dev.kobj, &hwmon_attrgroup);
		sysfs_remove_group(&dev->pdev->dev.kobj, &hwmon_pwm_fan_attrgroup);
		sysfs_remove_group(&dev->pdev->dev.kobj, &hwmon_fan_rpm_attrgroup);

		hwmon_device_unregister(pm->hwmon);
	}
#endif
+16 −0
Original line number Diff line number Diff line
@@ -55,6 +55,10 @@ nouveau_temp_vbios_parse(struct drm_device *dev, u8 *temp)
	temps->down_clock = 100;
	temps->fan_boost = 90;

	/* Set the default range for the pwm fan */
	pm->fan.min_duty = 30;
	pm->fan.max_duty = 100;

	/* Set the known default values to setup the temperature sensor */
	if (dev_priv->card_type >= NV_40) {
		switch (dev_priv->chipset) {
@@ -156,11 +160,23 @@ nouveau_temp_vbios_parse(struct drm_device *dev, u8 *temp)
		case 0x13:
			sensor->slope_div = value;
			break;
		case 0x22:
			pm->fan.min_duty = value & 0xff;
			pm->fan.max_duty = (value & 0xff00) >> 8;
			break;
		}
		temp += recordlen;
	}

	nouveau_temp_safety_checks(dev);

	/* check the fan min/max settings */
	if (pm->fan.min_duty < 10)
		pm->fan.min_duty = 10;
	if (pm->fan.max_duty > 100)
		pm->fan.max_duty = 100;
	if (pm->fan.max_duty < pm->fan.min_duty)
		pm->fan.max_duty = pm->fan.min_duty;
}

static int