Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932589AbbBZOA4 (ORCPT ); Thu, 26 Feb 2015 09:00:56 -0500 Received: from mailout4.samsung.com ([203.254.224.34]:27603 "EHLO mailout4.samsung.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932294AbbBZOAc (ORCPT ); Thu, 26 Feb 2015 09:00:32 -0500 X-AuditID: cbfee61b-f79d76d0000024d6-7c-54ef26fd8888 From: Lukasz Majewski To: Eduardo Valentin , Kamil Debski , Jean Delvare , Guenter Roeck , Kukjin Kim Cc: lm-sensors@lm-sensors.org, Linux PM list , "linux-samsung-soc@vger.kernel.org" , devicetree@vger.kernel.org, Lukasz Majewski , Kukjin Kim , linux-kernel@vger.kernel.org, Sjoerd Simons , Abhilash Kesavan , Abhilash Kesavan , Lukasz Majewski Subject: [PATCH v6 6/6] hwmon: pwm-fan: Code for using PWM FAN as a cooling device Date: Thu, 26 Feb 2015 14:59:37 +0100 Message-id: <1424959177-28739-7-git-send-email-l.majewski@samsung.com> X-Mailer: git-send-email 1.7.10.4 In-reply-to: <1424959177-28739-1-git-send-email-l.majewski@samsung.com> References: <1418897591-18332-1-git-send-email-l.majewski@samsung.com> <1424959177-28739-1-git-send-email-l.majewski@samsung.com> X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFrrCLMWRmVeSWpSXmKPExsVy+t9jQd1/au9DDKb8YrV4vGYxk8X8I+dY LeZfucZqsfTuJzaLH68vsFms+atk0bvgKptF/+PXzBZvHnFbvHm4mdHi8q45bBafe48wWsw4 v4/J4snCM0wWd6btZbOYfvwtq4OAx9/n11k8ds66y+6xaVUnm8eDibvZPNZNe8vssfN7A7tH 35ZVjB6bT1d7fN4kF8AZxWWTkpqTWZZapG+XwJWx+t959oIm9Yp50yYxNzA+ku9i5OSQEDCR uHh5FxOELSZx4d56ti5GLg4hgemMEme3b2SGcLqYJG4d3scGUsUmoCfx+e5TJpCEiMB2RokV 876BtTALXGOW2Lf5KFCGg0NYIETi7yFekAYWAVWJuz+bwVbwCrhJbDmxlQ1inaJE97MJYDan gLvE04Z7UKubGSUmXV7MOIGRdwEjwypG0dSC5ILipPRcI73ixNzi0rx0veT83E2M4LB+Jr2D cVWDxSFGAQ5GJR7ehOx3IUKsiWXFlbmHGCU4mJVEeNmU3ocI8aYkVlalFuXHF5XmpBYfYpTm YFES51WybwsREkhPLEnNTk0tSC2CyTJxcEo1MErN+Pnzx+ZUF6W3Ow37Fafvk/v5Q2rL+ULW iWs2H5xY5DJp8tpn0bpvPWIcs6c8Ldh/YfsrnwVp4Rk3N15LeXJgzbPlnRf3xN0KE7Veunbm 2Rdftny9Z5z5bs7EVr7wTctYGnIfs00vmzA7UtH21Xc24Re6Gr/PXqkK4L3ifHOarVvfNrlv Rf/zlFiKMxINtZiLihMBUGOZwmcCAAA= Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 5058 Lines: 186 The PWM FAN device can now be used as a thermal cooling device. Necessary infrastructure has been added in this commit. Signed-off-by: Lukasz Majewski Acked-by: Eduardo Valentin --- Changes for v2: - Replace pwm_fan_cooling_states with pwm_fan_cooling_levels - Update ctx->pwm_fan_state when correct data from device tree is available - Using therma_cdev_update() when thermal is ready for controlling the fan Changes for v3: - Rename patch heading - pwm_fan_of_get_cooling_data() now returns -EINVAL when no "cooling-levels" property defined - register of cooling device only when proper cooling data is present Changes for v4: - None Changes for v5: - Check for IS_ENABLED(CONFIG_THERMAL) has been added to prevent from executing thermal_* specific functions Changes for v6: - Adding missing ctx == NULL check in pwm_fan_get_max_state() - Call to thermal_cooling_device_unregister(ctx->cdev); at pwm_fan_remove() - struct thermal_cooling_device *cdev; added to struct pwm_fan_ctx --- drivers/hwmon/pwm-fan.c | 89 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 88 insertions(+), 1 deletion(-) diff --git a/drivers/hwmon/pwm-fan.c b/drivers/hwmon/pwm-fan.c index e6ed353..7c83dc4 100644 --- a/drivers/hwmon/pwm-fan.c +++ b/drivers/hwmon/pwm-fan.c @@ -24,6 +24,7 @@ #include #include #include +#include #define MAX_PWM 255 @@ -34,6 +35,7 @@ struct pwm_fan_ctx { unsigned int pwm_fan_state; unsigned int pwm_fan_max_state; unsigned int *pwm_fan_cooling_levels; + struct thermal_cooling_device *cdev; }; static int __set_pwm(struct pwm_fan_ctx *ctx, unsigned long pwm) @@ -68,6 +70,17 @@ exit_set_pwm_err: return ret; } +static void pwm_fan_update_state(struct pwm_fan_ctx *ctx, unsigned long pwm) +{ + int i; + + for (i = 0; i < ctx->pwm_fan_max_state; ++i) + if (pwm < ctx->pwm_fan_cooling_levels[i + 1]) + break; + + ctx->pwm_fan_state = i; +} + static ssize_t set_pwm(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) { @@ -82,6 +95,7 @@ static ssize_t set_pwm(struct device *dev, struct device_attribute *attr, if (ret) return ret; + pwm_fan_update_state(ctx, pwm); return count; } @@ -103,6 +117,62 @@ static struct attribute *pwm_fan_attrs[] = { ATTRIBUTE_GROUPS(pwm_fan); +/* thermal cooling device callbacks */ +static int pwm_fan_get_max_state(struct thermal_cooling_device *cdev, + unsigned long *state) +{ + struct pwm_fan_ctx *ctx = cdev->devdata; + + if (!ctx) + return -EINVAL; + + *state = ctx->pwm_fan_max_state; + + return 0; +} + +static int pwm_fan_get_cur_state(struct thermal_cooling_device *cdev, + unsigned long *state) +{ + struct pwm_fan_ctx *ctx = cdev->devdata; + + if (!ctx) + return -EINVAL; + + *state = ctx->pwm_fan_state; + + return 0; +} + +static int +pwm_fan_set_cur_state(struct thermal_cooling_device *cdev, unsigned long state) +{ + struct pwm_fan_ctx *ctx = cdev->devdata; + int ret; + + if (!ctx || (state > ctx->pwm_fan_max_state)) + return -EINVAL; + + if (state == ctx->pwm_fan_state) + return 0; + + ret = __set_pwm(ctx, ctx->pwm_fan_cooling_levels[state]); + if (ret) { + dev_err(&cdev->device, "Cannot set pwm!\n"); + return ret; + } + + ctx->pwm_fan_state = state; + + return ret; +} + +static const struct thermal_cooling_device_ops pwm_fan_cooling_ops = { + .get_max_state = pwm_fan_get_max_state, + .get_cur_state = pwm_fan_get_cur_state, + .set_cur_state = pwm_fan_set_cur_state, +}; + int pwm_fan_of_get_cooling_data(struct device *dev, struct pwm_fan_ctx *ctx) { struct device_node *np = dev->of_node; @@ -145,8 +215,9 @@ int pwm_fan_of_get_cooling_data(struct device *dev, struct pwm_fan_ctx *ctx) static int pwm_fan_probe(struct platform_device *pdev) { - struct device *hwmon; + struct thermal_cooling_device *cdev; struct pwm_fan_ctx *ctx; + struct device *hwmon; int duty_cycle; int ret; @@ -193,6 +264,21 @@ static int pwm_fan_probe(struct platform_device *pdev) if (ret) return ret; + ctx->pwm_fan_state = ctx->pwm_fan_max_state; + if (IS_ENABLED(CONFIG_THERMAL)) { + cdev = thermal_of_cooling_device_register(pdev->dev.of_node, + "pwm-fan", ctx, + &pwm_fan_cooling_ops); + if (IS_ERR(cdev)) { + dev_err(&pdev->dev, + "Failed to register pwm-fan as cooling device"); + pwm_disable(ctx->pwm); + return PTR_ERR(cdev); + } + ctx->cdev = cdev; + thermal_cdev_update(cdev); + } + return 0; } @@ -200,6 +286,7 @@ static int pwm_fan_remove(struct platform_device *pdev) { struct pwm_fan_ctx *ctx = platform_get_drvdata(pdev); + thermal_cooling_device_unregister(ctx->cdev); if (ctx->pwm_value) pwm_disable(ctx->pwm); return 0; -- 2.0.0.rc2 -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/