From patchwork Wed Mar 4 12:03:44 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lukasz Majewski X-Patchwork-Id: 5935771 Return-Path: X-Original-To: patchwork-linux-pm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 2E8759F318 for ; Wed, 4 Mar 2015 12:04:58 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 509A32026F for ; Wed, 4 Mar 2015 12:04:56 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 36F5B20272 for ; Wed, 4 Mar 2015 12:04:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932821AbbCDMEh (ORCPT ); Wed, 4 Mar 2015 07:04:37 -0500 Received: from mailout1.samsung.com ([203.254.224.24]:61212 "EHLO mailout1.samsung.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932583AbbCDMEe (ORCPT ); Wed, 4 Mar 2015 07:04:34 -0500 Received: from epcpsbgm2.samsung.com (epcpsbgm2 [203.254.230.27]) by mailout1.samsung.com (Oracle Communications Messaging Server 7u4-24.01(7.0.4.24.0) 64bit (built Nov 17 2011)) with ESMTP id <0NKO00FXYS7JHN90@mailout1.samsung.com>; Wed, 04 Mar 2015 21:04:31 +0900 (KST) X-AuditID: cbfee61b-f79d76d0000024d6-1e-54f6f4cfec79 Received: from epmmp1.local.host ( [203.254.227.16]) by epcpsbgm2.samsung.com (EPCPMTA) with SMTP id 54.CF.09430.FC4F6F45; Wed, 04 Mar 2015 21:04:31 +0900 (KST) Received: from mcdsrvbld02.digital.local ([106.116.37.23]) by mmp1.samsung.com (Oracle Communications Messaging Server 7u4-24.01 (7.0.4.24.0) 64bit (built Nov 17 2011)) with ESMTPA id <0NKO00ILPS6ADG60@mmp1.samsung.com>; Wed, 04 Mar 2015 21:04:31 +0900 (KST) 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 RESEND v6 6/6] hwmon: pwm-fan: Code for using PWM FAN as a cooling device Date: Wed, 04 Mar 2015 13:03:44 +0100 Message-id: <1425470624-25793-7-git-send-email-l.majewski@samsung.com> X-Mailer: git-send-email 1.7.10.4 In-reply-to: <1425470624-25793-1-git-send-email-l.majewski@samsung.com> References: <1418897591-18332-1-git-send-email-l.majewski@samsung.com> <1425470624-25793-1-git-send-email-l.majewski@samsung.com> X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFrrMLMWRmVeSWpSXmKPExsVy+t9jAd3zX76FGKxfpmzxeM1iJov5R86x Wsy/co3VYundT2wWP15fYLNY81fJonfBVTaL/sevmS3ePOK2ePNwM6PF5V1z2Cw+9x5htJhx fh+TxZOFZ5gs7kzby2Yx/fhbVgcBj7/Pr7N47Jx1l91j06pONo8HE3ezeayb9pbZY+f3BnaP vi2rGD02n672+LxJLoAzissmJTUnsyy1SN8ugStj9b/z7AVN6hXzpk1ibmB8JN/FyMEhIWAi 0bPZt4uRE8gUk7hwbz1bFyMXh5DAIkaJw4c/MEI4XUwSSz5NZQSpYhPQk/h89ykTSEJEYDuj xIp538BamAWuMUvs23yUCaRKWCBa4tODj+wgK1gEVCUWrlcGCfMKuEmsedrGBrFOUaL72QQw m1PAXeL6oxYWiG3NjBKLTn5hn8DIu4CRYRWjaGpBckFxUnqukV5xYm5xaV66XnJ+7iZGcFA/ k97BuKrB4hCjAAejEg/vgdhvIUKsiWXFlbmHGCU4mJVEeGMvAoV4UxIrq1KL8uOLSnNSiw8x SnOwKInzKtm3hQgJpCeWpGanphakFsFkmTg4pRoYPQMCvuhxzZnatP+fUV9CwWqeM1N2NTEa HW4+3nP2eNoF4+xvVzJDfhpM21zwyXryyo0ir+8bmstu3fT5rs/0FwIr1Sf38mwNayl4I3/1 Rkz3No7Lj1Jzjk/Yd6a+w3fX6p1/JNWma5opbbZs2xMu8MpObEF5h3in46vSpyffTLvXxL0u Wm/TMiWW4oxEQy3mouJEAFJv31JmAgAA Sender: linux-pm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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;