Skip to content

Commit

Permalink
hwmon: (pwm-fan) Read PWM FAN configuration from device tree
Browse files Browse the repository at this point in the history
This patch provides code for reading PWM FAN configuration data via
device tree. The pwm-fan can work with full speed when configuration
is not provided. However, errors are propagated when wrong DT bindings
are found.
Additionally the struct pwm_fan_ctx has been extended.

Signed-off-by: Lukasz Majewski <l.majewski@samsung.com>
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
  • Loading branch information
Lukasz Majewski authored and Guenter Roeck committed Mar 9, 2015
1 parent cb85ca3 commit 2e5219c
Showing 1 changed file with 49 additions and 1 deletion.
50 changes: 49 additions & 1 deletion drivers/hwmon/pwm-fan.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@
struct pwm_fan_ctx {
struct mutex lock;
struct pwm_device *pwm;
unsigned char pwm_value;
unsigned int pwm_value;
unsigned int pwm_fan_state;
unsigned int pwm_fan_max_state;
unsigned int *pwm_fan_cooling_levels;
};

static int __set_pwm(struct pwm_fan_ctx *ctx, unsigned long pwm)
Expand Down Expand Up @@ -100,6 +103,46 @@ static struct attribute *pwm_fan_attrs[] = {

ATTRIBUTE_GROUPS(pwm_fan);

int pwm_fan_of_get_cooling_data(struct device *dev, struct pwm_fan_ctx *ctx)
{
struct device_node *np = dev->of_node;
int num, i, ret;

if (!of_find_property(np, "cooling-levels", NULL))
return 0;

ret = of_property_count_u32_elems(np, "cooling-levels");
if (ret <= 0) {
dev_err(dev, "Wrong data!\n");
return ret ? : -EINVAL;
}

num = ret;
ctx->pwm_fan_cooling_levels = devm_kzalloc(dev, num * sizeof(u32),
GFP_KERNEL);
if (!ctx->pwm_fan_cooling_levels)
return -ENOMEM;

ret = of_property_read_u32_array(np, "cooling-levels",
ctx->pwm_fan_cooling_levels, num);
if (ret) {
dev_err(dev, "Property 'cooling-levels' cannot be read!\n");
return ret;
}

for (i = 0; i < num; i++) {
if (ctx->pwm_fan_cooling_levels[i] > MAX_PWM) {
dev_err(dev, "PWM fan state[%d]:%d > %d\n", i,
ctx->pwm_fan_cooling_levels[i], MAX_PWM);
return -EINVAL;
}
}

ctx->pwm_fan_max_state = num - 1;

return 0;
}

static int pwm_fan_probe(struct platform_device *pdev)
{
struct device *hwmon;
Expand Down Expand Up @@ -145,6 +188,11 @@ static int pwm_fan_probe(struct platform_device *pdev)
pwm_disable(ctx->pwm);
return PTR_ERR(hwmon);
}

ret = pwm_fan_of_get_cooling_data(&pdev->dev, ctx);
if (ret)
return ret;

return 0;
}

Expand Down

0 comments on commit 2e5219c

Please sign in to comment.