From c2e92581daeaa811ceec4f301d85688567e699d2 Mon Sep 17 00:00:00 2001 From: Anand Moon Date: Thu, 13 Apr 2017 11:44:58 +0530 Subject: [PATCH] hwmon: pwm-fan: add sys attribute to control active cooling update fan_speed sys attribute to update the cooling level for pwm-fan. Change-Id: I67b0d047dbe0cb0cfa81896ec6226e8e6d79fd1e --- drivers/hwmon/pwm-fan.c | 44 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/drivers/hwmon/pwm-fan.c b/drivers/hwmon/pwm-fan.c index f9af3935b427..e3ef976a255c 100644 --- a/drivers/hwmon/pwm-fan.c +++ b/drivers/hwmon/pwm-fan.c @@ -107,11 +107,55 @@ static ssize_t show_pwm(struct device *dev, return sprintf(buf, "%u\n", ctx->pwm_value); } +static ssize_t set_cooll(struct device *dev, struct device_attribute *attr, + const char *buf, size_t count) +{ + struct pwm_fan_ctx *ctx = dev_get_drvdata(dev); + unsigned int speed_0, speed_1, speed_2, speed_3; + + if(sscanf(buf, "%u %u %u %u\n", &speed_0, &speed_1, &speed_2, &speed_3) != 4) { + dev_err(dev, "invalid speed input"); + return -EINVAL; + } + + if(!(speed_0 < speed_1 && speed_1 < speed_2 && speed_2 < speed_3)){ + dev_err(dev, "fan speeds must be increasing in value"); + return count; + } + + dev_info(dev, "fan_speeds : %s [%d %d %d %d] \n", + __func__, speed_0, speed_1, speed_2, speed_3); + + mutex_lock(&ctx->lock); + ctx->pwm_fan_cooling_levels[0] = speed_0; + ctx->pwm_fan_cooling_levels[1] = speed_1; + ctx->pwm_fan_cooling_levels[2] = speed_2; + ctx->pwm_fan_cooling_levels[3] = speed_3; + mutex_unlock(&ctx->lock); + + return count; +} + +static ssize_t show_cooll(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct pwm_fan_ctx *ctx = dev_get_drvdata(dev); + int lenght = 0, i; + + mutex_lock(&ctx->lock); + for (i = 0; i <= ctx->pwm_fan_max_state; i++) + lenght += sprintf(buf+lenght, "%u ", ctx->pwm_fan_cooling_levels[i]); + mutex_unlock(&ctx->lock); + + return lenght; +} static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 0); +static SENSOR_DEVICE_ATTR(fan_speed, S_IRUGO | S_IWUSR, show_cooll, set_cooll, 0); static struct attribute *pwm_fan_attrs[] = { &sensor_dev_attr_pwm1.dev_attr.attr, + &sensor_dev_attr_fan_speed.dev_attr.attr, NULL, };