drm/nouveau/pm: manual pwm fanspeed management for nv40+ boards
authorMartin Peres <martin.peres@ensi-bourges.fr>
Mon, 15 Aug 2011 01:10:30 +0000 (11:10 +1000)
committerBen Skeggs <bskeggs@redhat.com>
Wed, 21 Dec 2011 09:01:11 +0000 (19:01 +1000)
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: Ben Skeggs <bskeggs@redhat.com>
Signed-off-by: Martin Peres <martin.peres@ensi-bourges.fr>
drivers/gpu/drm/nouveau/nouveau_bios.h
drivers/gpu/drm/nouveau/nouveau_drv.h
drivers/gpu/drm/nouveau/nouveau_pm.c
drivers/gpu/drm/nouveau/nouveau_temp.c

index 8adb69e4a6b124f2034e0e80ed0ac97ca13a1b9f..3f36c7074d4f0b51a01ebb6d5960b475cfe1c0d1 100644 (file)
@@ -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 {
index 2c8ebf6248a0368dc99f2ba40ba514479dc959c7..432be658060dd3fdfd76e4742aa3592c69939414 100644 (file)
@@ -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;
index ee2872ed60ee76504b45debe416f4051d020b796..23b297fd6f6106552779fac2ece7e2fac34d7582 100644 (file)
@@ -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
index 5a46446dd5a8682e4773b8a0ff60724ef7de3514..97c172cae3bc08d26d91da5cebb52dcc39bec0b6 100644 (file)
@@ -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