Re: [PATCH v2 7/8] hwmon: thermal: Read PWM FAN configuration from device tree

Guenter Roeck
2014-12-29T12:51:45Z

On Mon, Dec 22, 2014 at 05:27:47PM +0100, Lukasz Majewski wrote:
> Code for reading PWM FAN configuration data via device tree.
>
> Signed-off-by: Lukasz Majewski <l.majewski@samsung.com>
> ---

The headline is quite misleading. Please provide the affected subsystem (hwmon)
and the affected driver (pwm-fan) in the hwmon-customary form
hwmon: (pwm-fan) Description

The Description should explain what the patch is about.

> Changes for v2:
> - Rename pwm_fan_max_states to pwm_fan_cooling_levels
> - Moving pwm_fan_of_get_cooling_data() call after setting end enabling PWM FAN
> - pwm_fan_of_get_cooling_data() now can fail - preserving old behaviour
> - Remove unnecessary dev_err() call
> ---
> drivers/hwmon/pwm-fan.c | 48 +++++++++++++++++++++++++++++++++++++++++++++++-
> 1 file changed, 47 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/hwmon/pwm-fan.c b/drivers/hwmon/pwm-fan.c
> index 870e100..8e68308 100644
> --- a/drivers/hwmon/pwm-fan.c
> +++ b/drivers/hwmon/pwm-fan.c
> @@ -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)
> @@ -100,6 +103,47 @@ 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;
> + struct property *pp;
> + int len, num, i;
> +
> + pp = of_find_property(np, "cooling-levels", &len);
> + if (!pp) {
> + dev_err(dev, "Property: 'cooling-levels' not found\n");
> + return -EINVAL;
> + }
> +
> + if (len == 0) {
> + dev_err(dev, "Length wrong value!\n");

Semantics. "Wrong length" would be better.

> + return -EINVAL;
> + }
> +

of_property_count_elems_of_size() might be more appropriate here.

> + ctx->pwm_fan_cooling_levels = devm_kzalloc(dev, len, GFP_KERNEL);
> + if (!ctx->pwm_fan_cooling_levels)
> + return -ENOMEM;
> +
> + num = len / sizeof(u32);

What if 'num' is 0 ? Is that guaranteed to never happen ?

> + if (of_property_read_u32_array(np, pp->name,
> + ctx->pwm_fan_cooling_levels, num)) {
> + dev_err(dev, "Property: %s cannot be read!\n", pp->name);

The ':' after 'Property' in those error messages doesn't seem to make much sense
to me.

> + return -EINVAL;

of_property_read_u32_array() returns an error code. Please use it.

> + }
> +
> + 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 +189,8 @@ static int pwm_fan_probe(struct platform_device *pdev)
> pwm_disable(ctx->pwm);
> return PTR_ERR(hwmon);
> }
> +
> + pwm_fan_of_get_cooling_data(&pdev->dev, ctx);

Now this is odd. Adding a function to return an error code just to ignore it
doesn't make much sense. While it makes sense to ignore errors if there is no
cooling data in the devicetree, errors due to bad devicetree data should not be
ignored.

I am also a bit concerned that you make this call _after_ instantiating the
hwmon device; this may potentially result in a race condition. Please ensure
that this is not the case.

Thanks,
Guenter