From patchwork Thu Apr 18 19:58:20 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Guenter Roeck X-Patchwork-Id: 10907937 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id A057814DB for ; Thu, 18 Apr 2019 19:59:40 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 81A8228C0B for ; Thu, 18 Apr 2019 19:59:40 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 72C5728C0E; Thu, 18 Apr 2019 19:59:40 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.2 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED autolearn=unavailable version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id EF7AD28C0B for ; Thu, 18 Apr 2019 19:59:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:References: In-Reply-To:Message-Id:Date:Subject:To:From:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=tWVIEuJtYhNUugAxiEH7XPU1WiaMfJ7Qi2QLV91UkPM=; b=N0UG10Ur9gkeTa9XywJI2fN9IX rySfGId58poABcD0fe8lhKsqU3D7PbHU7hOo4vkmy4FVNhl9oyoEGGBMM3opSs5kClkRkEidF8qVO 6Kc9zrhZlG2Rp85qXhxiNoD0u3aitnf3HA1Aon799MmyEl+NEZlc9dkM82KTvzq5ytnvSEIvKG1U5 fKQF5dypVWm0wvp/3Q2s0uXe2Yj2YIAkuXu5KG/9BUwwCwRfXqBtgcLrAUU6Cu/rcp4Z8XVWhFqLw aT9elAszRzue8OjStoyK5sKwuMHx0kf2PsxSdkRwbgMyidhsOT6jEMk5JzYa98mOPxaLJpklXvp7j BRM3jJVQ==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1hHDC0-0003yR-O5; Thu, 18 Apr 2019 19:59:32 +0000 Received: from mail-pg1-x542.google.com ([2607:f8b0:4864:20::542]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1hHDB3-0002iX-9U for linux-arm-kernel@lists.infradead.org; Thu, 18 Apr 2019 19:58:36 +0000 Received: by mail-pg1-x542.google.com with SMTP id z9so1654343pgu.10 for ; Thu, 18 Apr 2019 12:58:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=B+LNAKL2mwreepfAxVSDlLeu1nTRg9NDiYI0VC4TC00=; b=rqjoxiyNXurY80FdMqb1XIcVY1Nud70uNtYmfruRMk2u2YXL4fTnC7SeQsHVDfnZIv +2wwohnJUklymDYYzMcsp0nJDENdX0qkePVfhDHjPngp88FT+qHbXcQNgUVft7MUszgD BhlRol4n1F82v74M5vehGKqa01BQy1u+S0ospxjCt+ZHr4NIBNpq0TCN28WsZ0j8lmVZ tl+khSzxsQIk4LG5374Sd2h6C3YAhNHv2WooJRvYf/LZUUvPKDOLCg2TqmL2tNKrfKa0 pnRm/jqjXJ58kgtDS3oQacPwWYljpZN6GmieljHz1cnbDVJCd3DHWGIJFCYn+c66KCP0 nZOw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=B+LNAKL2mwreepfAxVSDlLeu1nTRg9NDiYI0VC4TC00=; b=uP4K8g9ReyOGf5DEtcXCXwNZDqWHHH//h/t9Rd2nb4YzDNigNNKrvR6BDoAWhoTL/z 4LCI3o/njnlwexzyX5qOn3gLDgT4aJZpH3VQ0lmUmOU3x4EMefoGVdQAk31nTgtmzLac g0OGsyuFVX62Z7UHFhIfYAJjtwKmh7O9lD8hUN3NHjz9d2NgqvKJuX9/XVY4EQjvWyet 2w4ZM7eDHDS9cAfeklnWM+T6YBGRVf5vE59S6PTTBXLfOkNVlojaXniCNzWwHbo68Yug g3z4+mifaqRO/47Q6gRoAZ1yDx/hl4PHVK3s8Q5z5n59eE1+2Ltl31zeN1zpHGmYcB5T b4wQ== X-Gm-Message-State: APjAAAWVhSH1LKl3DSyAfIinPwVHNGP6rgVr7ZBlZTIUGPKwh0KqvYiA rtGwVis1/+JnGr60P4KRZT0= X-Google-Smtp-Source: APXvYqyrcCmw+O4OFChgOEXhNjU0JwgPzu/CWUETVfyzN5U3Q8MjuDahbKsOjMdag3oLd5f74B99MA== X-Received: by 2002:a65:6144:: with SMTP id o4mr90124351pgv.247.1555617512356; Thu, 18 Apr 2019 12:58:32 -0700 (PDT) Received: from localhost ([2600:1700:e321:62f0:329c:23ff:fee3:9d7c]) by smtp.gmail.com with ESMTPSA id s9sm4219784pfe.183.2019.04.18.12.58.31 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 18 Apr 2019 12:58:31 -0700 (PDT) From: Guenter Roeck To: linux-hwmon@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-aspeed@lists.ozlabs.org, linux-kernel@vger.kernel.org, openbmc@lists.ozlabs.org, linux-pm@vger.kernel.org Subject: [PATCH 6/6] hwmon: (pwm-fan) Use devm_thermal_of_cooling_device_register Date: Thu, 18 Apr 2019 12:58:20 -0700 Message-Id: <1555617500-10862-7-git-send-email-linux@roeck-us.net> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1555617500-10862-1-git-send-email-linux@roeck-us.net> References: <1555617500-10862-1-git-send-email-linux@roeck-us.net> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20190418_125833_583005_4C8FB90B X-CRM114-Status: GOOD ( 18.12 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Lukasz Majewski , Jean Delvare , Tomer Maimon , Nancy Yuen , Andrew Jeffery , Patrick Venture , Bartlomiej Zolnierkiewicz , Kamil Debski , Tali Perry , Eduardo Valentin , Avi Fishman , Joel Stanley , Daniel Lezcano , Zhang Rui , Guenter Roeck , Benjamin Fair MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP Use devm_thermal_of_cooling_device_register() to register the cooling device. Also use devm_add_action_or_reset() to stop the fan on device removal, and to disable the pwm. Introduce a local 'dev' variable in the probe function to make the code easier to read. As a side effect, this fixes a bug seen if pwm_fan_of_get_cooling_data() returned an error. In that situation, the pwm was not disabled, and the fan was not stopped. Using devm functions also ensures that the pwm is disabled and that the fan is stopped only after the hwmon device has been unregistered. Cc: Lukasz Majewski Signed-off-by: Guenter Roeck --- drivers/hwmon/pwm-fan.c | 73 ++++++++++++++++++++----------------------------- 1 file changed, 29 insertions(+), 44 deletions(-) diff --git a/drivers/hwmon/pwm-fan.c b/drivers/hwmon/pwm-fan.c index 167221c7628a..0243ba70107e 100644 --- a/drivers/hwmon/pwm-fan.c +++ b/drivers/hwmon/pwm-fan.c @@ -207,33 +207,44 @@ static int pwm_fan_of_get_cooling_data(struct device *dev, return 0; } +static void pwm_fan_regulator_disable(void *data) +{ + regulator_disable(data); +} + +static void pwm_fan_pwm_disable(void *data) +{ + pwm_disable(data); +} + static int pwm_fan_probe(struct platform_device *pdev) { struct thermal_cooling_device *cdev; + struct device *dev = &pdev->dev; struct pwm_fan_ctx *ctx; struct device *hwmon; int ret; struct pwm_state state = { }; - ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL); + ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL); if (!ctx) return -ENOMEM; mutex_init(&ctx->lock); - ctx->pwm = devm_of_pwm_get(&pdev->dev, pdev->dev.of_node, NULL); + ctx->pwm = devm_of_pwm_get(dev, dev->of_node, NULL); if (IS_ERR(ctx->pwm)) { ret = PTR_ERR(ctx->pwm); if (ret != -EPROBE_DEFER) - dev_err(&pdev->dev, "Could not get PWM: %d\n", ret); + dev_err(dev, "Could not get PWM: %d\n", ret); return ret; } platform_set_drvdata(pdev, ctx); - ctx->reg_en = devm_regulator_get_optional(&pdev->dev, "fan"); + ctx->reg_en = devm_regulator_get_optional(dev, "fan"); if (IS_ERR(ctx->reg_en)) { if (PTR_ERR(ctx->reg_en) != -ENODEV) return PTR_ERR(ctx->reg_en); @@ -242,10 +253,11 @@ static int pwm_fan_probe(struct platform_device *pdev) } else { ret = regulator_enable(ctx->reg_en); if (ret) { - dev_err(&pdev->dev, - "Failed to enable fan supply: %d\n", ret); + dev_err(dev, "Failed to enable fan supply: %d\n", ret); return ret; } + devm_add_action_or_reset(dev, pwm_fan_regulator_disable, + ctx->reg_en); } ctx->pwm_value = MAX_PWM; @@ -257,62 +269,36 @@ static int pwm_fan_probe(struct platform_device *pdev) ret = pwm_apply_state(ctx->pwm, &state); if (ret) { - dev_err(&pdev->dev, "Failed to configure PWM\n"); - goto err_reg_disable; + dev_err(dev, "Failed to configure PWM\n"); + return ret; } + devm_add_action_or_reset(dev, pwm_fan_pwm_disable, ctx->pwm); - hwmon = devm_hwmon_device_register_with_groups(&pdev->dev, "pwmfan", + hwmon = devm_hwmon_device_register_with_groups(dev, "pwmfan", ctx, pwm_fan_groups); if (IS_ERR(hwmon)) { - dev_err(&pdev->dev, "Failed to register hwmon device\n"); - ret = PTR_ERR(hwmon); - goto err_pwm_disable; + dev_err(dev, "Failed to register hwmon device\n"); + return PTR_ERR(hwmon); } - ret = pwm_fan_of_get_cooling_data(&pdev->dev, ctx); + ret = pwm_fan_of_get_cooling_data(dev, ctx); 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); + cdev = devm_thermal_of_cooling_device_register(dev, + dev->of_node, "pwm-fan", ctx, &pwm_fan_cooling_ops); if (IS_ERR(cdev)) { - dev_err(&pdev->dev, + dev_err(dev, "Failed to register pwm-fan as cooling device"); - ret = PTR_ERR(cdev); - goto err_pwm_disable; + return PTR_ERR(cdev); } ctx->cdev = cdev; thermal_cdev_update(cdev); } return 0; - -err_pwm_disable: - state.enabled = false; - pwm_apply_state(ctx->pwm, &state); - -err_reg_disable: - if (ctx->reg_en) - regulator_disable(ctx->reg_en); - - return ret; -} - -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); - - if (ctx->reg_en) - regulator_disable(ctx->reg_en); - - return 0; } #ifdef CONFIG_PM_SLEEP @@ -380,7 +366,6 @@ MODULE_DEVICE_TABLE(of, of_pwm_fan_match); static struct platform_driver pwm_fan_driver = { .probe = pwm_fan_probe, - .remove = pwm_fan_remove, .driver = { .name = "pwm-fan", .pm = &pwm_fan_pm,