hwmon: (pwm-fan) Read PWM FAN configuration from device tree
authorLukasz Majewski <l.majewski@samsung.com>
Thu, 26 Feb 2015 13:59:36 +0000 (14:59 +0100)
committerGuenter Roeck <linux@roeck-us.net>
Mon, 9 Mar 2015 16:59:36 +0000 (09:59 -0700)
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>
drivers/hwmon/pwm-fan.c

index bd42d3996a863a2c302feb7d40c33ceff69dc5b2..e6ed3532deac74754e4c172562b85e493c54d047 100644 (file)
 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)
@@ -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;
@@ -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;
 }