Skip to content

Commit

Permalink
hwmon: pwm-fan: add sys attribute to control active cooling
Browse files Browse the repository at this point in the history
update fan_speed sys attribute to update the cooling level
for pwm-fan.

Change-Id: I67b0d047dbe0cb0cfa81896ec6226e8e6d79fd1e
Signed-off-by: Dongjin Kim <tobetter@gmail.com>
  • Loading branch information
moonlinux authored and tobetter committed Oct 16, 2017
1 parent 9e79254 commit 2bb3a4b
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions drivers/hwmon/pwm-fan.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,55 @@ static ssize_t show_pwm(struct device *dev,
return sprintf(buf, "%u\n", ctx->pwm_value);
}

static ssize_t set_cool(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_cool(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_cool, set_cool, 0);

static struct attribute *pwm_fan_attrs[] = {
&sensor_dev_attr_pwm1.dev_attr.attr,
&sensor_dev_attr_fan_speed.dev_attr.attr,
NULL,
};

Expand Down

0 comments on commit 2bb3a4b

Please sign in to comment.