From patchwork Fri Nov 13 15:08:50 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Barker X-Patchwork-Id: 11903897 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 531F1139F for ; Fri, 13 Nov 2020 15:09:37 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2D2652225E for ; Fri, 13 Nov 2020 15:09:37 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=konsulko.com header.i=@konsulko.com header.b="FCSaLYo3" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726614AbgKMPJg (ORCPT ); Fri, 13 Nov 2020 10:09:36 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37042 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726278AbgKMPJg (ORCPT ); Fri, 13 Nov 2020 10:09:36 -0500 Received: from mail-wm1-x344.google.com (mail-wm1-x344.google.com [IPv6:2a00:1450:4864:20::344]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 729E6C0613D1 for ; Fri, 13 Nov 2020 07:09:36 -0800 (PST) Received: by mail-wm1-x344.google.com with SMTP id 10so8420074wml.2 for ; Fri, 13 Nov 2020 07:09:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=konsulko.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=SC6v/9U+HIlsnisZ5co4yMBSoBpm8f6NrRqUBASHBlM=; b=FCSaLYo3AnegtxIfnMkLC7divq/Lmt8U5Jpc+kWHyokEMXARdifg1u9BsBEvo2/2Qx w0QugCGQQdsBSC8igPLc6TjZDpWLodGL/4ogVqPvlkc7QGwcPr9PTX516xfKeQ6g5xur GA90uyWkiowZycaLG4+I8UJlzb+QVGIthVWyA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=SC6v/9U+HIlsnisZ5co4yMBSoBpm8f6NrRqUBASHBlM=; b=WvLOs6fYv17/9M0adHEUyUOgHgEmij2H/K8v5Nl5/VD9BM4cLNz/mOfg64B1DE0qwj ZKodF1MlduN+z0w39EMH3e0mk6dEm6Hd4mU3KMG9eEU3UnbS6cudaEP/h5//5clK/CJh WhybYZhNDWbdoHJRpdu4p3PSsFf5E9n7KPYlSVXRThxpfKdKMc5SPbiCei/R5SMJTiMl +P0gHia9wu1lCIBgVOF10x/LBZ7/2oFbvA9DobePTB8XXT7pL7VMoRgQeNG60+kHQDTF hwkxe2zwV1LR/xtBrh1Q1KMZsdFbfwX5BCJg1irjSxRLisher3+S+nWy1iOnRsflm+Ru tIrQ== X-Gm-Message-State: AOAM530b0C15ufQjju2u2HynRTzrN9/6DMmj1WW7+pYcMIMyAnkSivQM qukYd9eC1+dUytqGqOJEH9RrvA== X-Google-Smtp-Source: ABdhPJyeRhF0vcqqQVFDPNNIKMScBufW/HxBk9zFiQHUKMs0Woc/dmd4ExG3nIEtLx44kjfjlo8IrQ== X-Received: by 2002:a1c:8150:: with SMTP id c77mr3174978wmd.26.1605280174287; Fri, 13 Nov 2020 07:09:34 -0800 (PST) Received: from alpha.home.b5net.uk (cpc76132-clif11-2-0-cust249.12-4.cable.virginm.net. [80.7.160.250]) by smtp.gmail.com with ESMTPSA id v2sm11368221wrm.96.2020.11.13.07.09.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Nov 2020 07:09:33 -0800 (PST) From: Paul Barker To: Kamil Debski , Bartlomiej Zolnierkiewicz , Jean Delvare , Guenter Roeck Cc: Paul Barker , linux-hwmon@vger.kernel.org Subject: [PATCH v2 1/4] hwmon: pwm-fan: Refactor pwm_fan_probe Date: Fri, 13 Nov 2020 15:08:50 +0000 Message-Id: <20201113150853.155495-2-pbarker@konsulko.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201113150853.155495-1-pbarker@konsulko.com> References: <20201113150853.155495-1-pbarker@konsulko.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-hwmon@vger.kernel.org Use platform_irq_count to determine the number of fan tachometer inputs configured in the device tree. At this stage we support either 0 or 1 inputs. Once we have this information we only need to read the pulses-per-revolution value if a fan tachometer is actually configured via an IRQ value. Also add a debug print of the IRQ number and the pulses-per-revolution value to aid in investigating issues. Signed-off-by: Paul Barker Reviewed-by: Guenter Roeck --- drivers/hwmon/pwm-fan.c | 50 +++++++++++++++++++++++++++-------------- 1 file changed, 33 insertions(+), 17 deletions(-) diff --git a/drivers/hwmon/pwm-fan.c b/drivers/hwmon/pwm-fan.c index 1f63807c0399..efe2764f42d3 100644 --- a/drivers/hwmon/pwm-fan.c +++ b/drivers/hwmon/pwm-fan.c @@ -286,7 +286,7 @@ static int pwm_fan_probe(struct platform_device *pdev) struct device *hwmon; int ret; struct pwm_state state = { }; - u32 ppr = 2; + int tach_count; ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL); if (!ctx) @@ -300,10 +300,6 @@ static int pwm_fan_probe(struct platform_device *pdev) platform_set_drvdata(pdev, ctx); - ctx->irq = platform_get_irq_optional(pdev, 0); - if (ctx->irq == -EPROBE_DEFER) - return ctx->irq; - ctx->reg_en = devm_regulator_get_optional(dev, "fan"); if (IS_ERR(ctx->reg_en)) { if (PTR_ERR(ctx->reg_en) != -ENODEV) @@ -339,20 +335,40 @@ static int pwm_fan_probe(struct platform_device *pdev) if (ret) return ret; - of_property_read_u32(dev->of_node, "pulses-per-revolution", &ppr); - ctx->pulses_per_revolution = ppr; - if (!ctx->pulses_per_revolution) { - dev_err(dev, "pulses-per-revolution can't be zero.\n"); - return -EINVAL; - } + tach_count = platform_irq_count(pdev); + if (tach_count < 0) + return dev_err_probe(dev, tach_count, + "Could not get number of fan tachometer inputs\n"); + + if (tach_count > 0) { + u32 ppr = 2; + + ctx->irq = platform_get_irq(pdev, 0); + if (ctx->irq == -EPROBE_DEFER) + return ctx->irq; + if (ctx->irq > 0) { + ret = devm_request_irq(dev, ctx->irq, pulse_handler, 0, + pdev->name, ctx); + if (ret) { + dev_err(dev, + "Failed to request interrupt: %d\n", + ret); + return ret; + } + } - if (ctx->irq > 0) { - ret = devm_request_irq(dev, ctx->irq, pulse_handler, 0, - pdev->name, ctx); - if (ret) { - dev_err(dev, "Failed to request interrupt: %d\n", ret); - return ret; + of_property_read_u32(dev->of_node, + "pulses-per-revolution", + &ppr); + ctx->pulses_per_revolution = ppr; + if (!ctx->pulses_per_revolution) { + dev_err(dev, "pulses-per-revolution can't be zero.\n"); + return -EINVAL; } + + dev_dbg(dev, "tach: irq=%d, pulses_per_revolution=%d\n", + ctx->irq, ctx->pulses_per_revolution); + ctx->sample_start = ktime_get(); mod_timer(&ctx->rpm_timer, jiffies + HZ); } From patchwork Fri Nov 13 15:08:51 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Barker X-Patchwork-Id: 11903899 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id CCAA4139F for ; Fri, 13 Nov 2020 15:09:38 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A9EB22225E for ; Fri, 13 Nov 2020 15:09:38 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=konsulko.com header.i=@konsulko.com header.b="dXybmEmD" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726324AbgKMPJi (ORCPT ); Fri, 13 Nov 2020 10:09:38 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37048 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726278AbgKMPJi (ORCPT ); Fri, 13 Nov 2020 10:09:38 -0500 Received: from mail-wm1-x343.google.com (mail-wm1-x343.google.com [IPv6:2a00:1450:4864:20::343]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D77F7C0613D1 for ; Fri, 13 Nov 2020 07:09:37 -0800 (PST) Received: by mail-wm1-x343.google.com with SMTP id w24so8811339wmi.0 for ; Fri, 13 Nov 2020 07:09:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=konsulko.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=zlVLQ/eSSwM5CkwG1+n/Ft7FPgN9/8zKQeZjhQqRfeo=; b=dXybmEmDuBX0Q1fgLUW0rXxBcBqR3kyUaQwV/rqQox5FPWTpQufr/i8HBueAxjobKG OA5qR52jYsjNS5ISEomcDIPuMAqZkMMio0ir6XFMJXUGxsOIb0CL2OG34me89wpikBTy eOPCiyLAE0fs91tGo4FCVwfzcvpDozki429rU= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=zlVLQ/eSSwM5CkwG1+n/Ft7FPgN9/8zKQeZjhQqRfeo=; b=NTOCZD51v9N2XgMeBPqRp8ni+Mr3OFWy8p4uDEcMu93oYMmfHeDvwm7qilifcYlXWp 48jsUIV05dck7KcsTj1qyOyt6e00WZxH/7MCOgXdXHZUaLws4WomvEMgoSeGSTO4IxvQ a0j2xdnAapiUil9Aocw7iAacc1OPnvL/CSJB58uq8DxX1uNR3x20+o17+wT7ftvruWMv wLsCflneE1j4y7DHX36iT2qj3HW9yggb7APHBLvSI55K/aPvBa2gG5GAGZ83uoHy4x2x aAJ4MExi6rhoaxiYwwwR8F0M1hTMyopT5ElRTxlbf0zFkXktgZUABay6nbpvgpZJELW6 kWxw== X-Gm-Message-State: AOAM531wkvXx+ewPHHeu5yaI7V3bKnCrlss8QOyg1266TJae1XZO9MUq Bj+NkAxaJtm+lEYH9/huqY/pbw== X-Google-Smtp-Source: ABdhPJwZSl/Nrkh8PVuucGCJcZccnMJjUUPOD+SvWk9eTg+BfGjZe9a5IFYjEfSfmZbga09ZP4CJ7Q== X-Received: by 2002:a05:600c:230e:: with SMTP id 14mr2976635wmo.119.1605280175577; Fri, 13 Nov 2020 07:09:35 -0800 (PST) Received: from alpha.home.b5net.uk (cpc76132-clif11-2-0-cust249.12-4.cable.virginm.net. [80.7.160.250]) by smtp.gmail.com with ESMTPSA id v2sm11368221wrm.96.2020.11.13.07.09.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Nov 2020 07:09:35 -0800 (PST) From: Paul Barker To: Kamil Debski , Bartlomiej Zolnierkiewicz , Jean Delvare , Guenter Roeck Cc: Paul Barker , linux-hwmon@vger.kernel.org Subject: [PATCH v2 2/4] hwmon: pwm-fan: Dynamically setup attribute groups Date: Fri, 13 Nov 2020 15:08:51 +0000 Message-Id: <20201113150853.155495-3-pbarker@konsulko.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201113150853.155495-1-pbarker@konsulko.com> References: <20201113150853.155495-1-pbarker@konsulko.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-hwmon@vger.kernel.org Instead of implementing an is_visible function we can dynamically populate the attribute group for the device based on whether a fan tachometer input is configured or not. Signed-off-by: Paul Barker --- drivers/hwmon/pwm-fan.c | 55 +++++++++++++++++------------------------ 1 file changed, 23 insertions(+), 32 deletions(-) diff --git a/drivers/hwmon/pwm-fan.c b/drivers/hwmon/pwm-fan.c index efe2764f42d3..c4e0059ccaec 100644 --- a/drivers/hwmon/pwm-fan.c +++ b/drivers/hwmon/pwm-fan.c @@ -31,6 +31,8 @@ struct pwm_fan_ctx { atomic_t pulses; unsigned int rpm; u8 pulses_per_revolution; + struct sensor_device_attribute sensor_attr; + ktime_t sample_start; struct timer_list rpm_timer; @@ -39,6 +41,9 @@ struct pwm_fan_ctx { unsigned int pwm_fan_max_state; unsigned int *pwm_fan_cooling_levels; struct thermal_cooling_device *cdev; + + struct attribute_group fan_group; + struct attribute_group *fan_groups[2]; }; /* This handler assumes self resetting edge triggered interrupt. */ @@ -138,36 +143,6 @@ static ssize_t rpm_show(struct device *dev, } static SENSOR_DEVICE_ATTR_RW(pwm1, pwm, 0); -static SENSOR_DEVICE_ATTR_RO(fan1_input, rpm, 0); - -static struct attribute *pwm_fan_attrs[] = { - &sensor_dev_attr_pwm1.dev_attr.attr, - &sensor_dev_attr_fan1_input.dev_attr.attr, - NULL, -}; - -static umode_t pwm_fan_attrs_visible(struct kobject *kobj, struct attribute *a, - int n) -{ - struct device *dev = container_of(kobj, struct device, kobj); - struct pwm_fan_ctx *ctx = dev_get_drvdata(dev); - - /* Hide fan_input in case no interrupt is available */ - if (n == 1 && ctx->irq <= 0) - return 0; - - return a->mode; -} - -static const struct attribute_group pwm_fan_group = { - .attrs = pwm_fan_attrs, - .is_visible = pwm_fan_attrs_visible, -}; - -static const struct attribute_group *pwm_fan_groups[] = { - &pwm_fan_group, - NULL, -}; /* thermal cooling device callbacks */ static int pwm_fan_get_max_state(struct thermal_cooling_device *cdev, @@ -287,6 +262,7 @@ static int pwm_fan_probe(struct platform_device *pdev) int ret; struct pwm_state state = { }; int tach_count; + size_t sz; ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL); if (!ctx) @@ -340,6 +316,13 @@ static int pwm_fan_probe(struct platform_device *pdev) return dev_err_probe(dev, tach_count, "Could not get number of fan tachometer inputs\n"); + sz = (2 + tach_count) * sizeof(struct attribute *); + ctx->fan_group.attrs = devm_kzalloc(dev, sz, GFP_KERNEL); + if (!ctx->fan_group.attrs) + return -ENOMEM; + + ctx->fan_group.attrs[0] = &sensor_dev_attr_pwm1.dev_attr.attr; + if (tach_count > 0) { u32 ppr = 2; @@ -366,6 +349,13 @@ static int pwm_fan_probe(struct platform_device *pdev) return -EINVAL; } + sysfs_attr_init(&ctx->sensor_attr.dev_attr.attr); + + ctx->sensor_attr.dev_attr.attr.name = "fan1_input"; + ctx->sensor_attr.dev_attr.attr.mode = 0444; + ctx->sensor_attr.dev_attr.show = rpm_show; + ctx->fan_group.attrs[1] = &ctx->sensor_attr.dev_attr.attr; + dev_dbg(dev, "tach: irq=%d, pulses_per_revolution=%d\n", ctx->irq, ctx->pulses_per_revolution); @@ -373,8 +363,9 @@ static int pwm_fan_probe(struct platform_device *pdev) mod_timer(&ctx->rpm_timer, jiffies + HZ); } - hwmon = devm_hwmon_device_register_with_groups(dev, "pwmfan", - ctx, pwm_fan_groups); + ctx->fan_groups[0] = &ctx->fan_group; + hwmon = devm_hwmon_device_register_with_groups(dev, "pwmfan", ctx, + (const struct attribute_group **)ctx->fan_groups); if (IS_ERR(hwmon)) { dev_err(dev, "Failed to register hwmon device\n"); return PTR_ERR(hwmon); From patchwork Fri Nov 13 15:08:52 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Barker X-Patchwork-Id: 11903901 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id E46B51391 for ; Fri, 13 Nov 2020 15:09:39 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C05912225D for ; Fri, 13 Nov 2020 15:09:39 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=konsulko.com header.i=@konsulko.com header.b="rrccujCk" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726682AbgKMPJj (ORCPT ); Fri, 13 Nov 2020 10:09:39 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37052 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726278AbgKMPJj (ORCPT ); Fri, 13 Nov 2020 10:09:39 -0500 Received: from mail-wr1-x444.google.com (mail-wr1-x444.google.com [IPv6:2a00:1450:4864:20::444]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4FF39C0617A6 for ; Fri, 13 Nov 2020 07:09:38 -0800 (PST) Received: by mail-wr1-x444.google.com with SMTP id c17so10228174wrc.11 for ; Fri, 13 Nov 2020 07:09:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=konsulko.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=eWGvHYLa2aBEzPoqidlg2Pw58TdL7QfyxPylAlX90ls=; b=rrccujCkP+6G9oW0Ot+K8OEvOIuXhzd+Pp9SKqpRiBbVUBD8YI1m6RjRI+GtRAJ2zP oEH+SooMwE57IJ/FaGh6ZJ8PzNXgMg6NSSX5qPlY4FxG7phBHPSViAhHYX/FIWBcncqp 02ajRd0cjWveATyG7br9/E1mgOJotU9O+gSpA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=eWGvHYLa2aBEzPoqidlg2Pw58TdL7QfyxPylAlX90ls=; b=ujTWNRZxQK46t9IDKXlM4+i/aDlF5DFNrGRu7NQKc8lECvgu3pUAIbxvUlHIePew49 67TPcpdRjYDey59mdTOhYGtR2SqeYM1/VbKP3SSSAUV5j1nar6cOZ3Rx0O6u4SpW79AV wvwRzlCWQYaN0DkU31k286XlA68homZEKFUb1YPfG3mDDYVFvxDuuZ6ePBr5SQuRZ/yg /mYMeaCXFebHSVrZIywIt/n/kr7oHE/vZfZxhVqyPu/xF+HWWywq8hGZl7mXj7GFwoNt VCuQp3W54hR6l1H34v3TBlNfrv03e/KyiypG2ttQ44Oi3Xz8g1R1xIasT10l0NRLCezk 64iw== X-Gm-Message-State: AOAM533wpBOJZqQ6atTROgls8gMID6dpUHE3A4R7TzzVKtglXiCXYxq0 /VrV5PSLHk4cLYFkJOum6EJpOw== X-Google-Smtp-Source: ABdhPJzRJw321ybvRm+nUlSZ3Yq8688KjeNPkrwnGWvByo5vl+1lKpUnUyJ2xwmaf1ZacmyI3TXrAw== X-Received: by 2002:adf:ecc9:: with SMTP id s9mr4171208wro.246.1605280176728; Fri, 13 Nov 2020 07:09:36 -0800 (PST) Received: from alpha.home.b5net.uk (cpc76132-clif11-2-0-cust249.12-4.cable.virginm.net. [80.7.160.250]) by smtp.gmail.com with ESMTPSA id v2sm11368221wrm.96.2020.11.13.07.09.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Nov 2020 07:09:36 -0800 (PST) From: Paul Barker To: Kamil Debski , Bartlomiej Zolnierkiewicz , Jean Delvare , Guenter Roeck Cc: Paul Barker , linux-hwmon@vger.kernel.org Subject: [PATCH v2 3/4] hwmon: pwm-fan: Store tach data separately Date: Fri, 13 Nov 2020 15:08:52 +0000 Message-Id: <20201113150853.155495-4-pbarker@konsulko.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201113150853.155495-1-pbarker@konsulko.com> References: <20201113150853.155495-1-pbarker@konsulko.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-hwmon@vger.kernel.org The data for the (optional) fan tachometer input is moved to a separate structure which is only allocated if an input is actually configured. After this change the pulse IRQ handler takes a pointer to the tachometer data structure instead of the whole device context. Signed-off-by: Paul Barker --- drivers/hwmon/pwm-fan.c | 66 +++++++++++++++++++++++++---------------- 1 file changed, 40 insertions(+), 26 deletions(-) diff --git a/drivers/hwmon/pwm-fan.c b/drivers/hwmon/pwm-fan.c index c4e0059ccaec..7c75ce78b36a 100644 --- a/drivers/hwmon/pwm-fan.c +++ b/drivers/hwmon/pwm-fan.c @@ -22,17 +22,21 @@ #define MAX_PWM 255 -struct pwm_fan_ctx { - struct mutex lock; - struct pwm_device *pwm; - struct regulator *reg_en; - +struct pwm_fan_tach { int irq; atomic_t pulses; unsigned int rpm; u8 pulses_per_revolution; + struct sensor_device_attribute sensor_attr; +}; +struct pwm_fan_ctx { + struct mutex lock; + struct pwm_device *pwm; + struct regulator *reg_en; + + struct pwm_fan_tach *tach; ktime_t sample_start; struct timer_list rpm_timer; @@ -49,9 +53,9 @@ struct pwm_fan_ctx { /* This handler assumes self resetting edge triggered interrupt. */ static irqreturn_t pulse_handler(int irq, void *dev_id) { - struct pwm_fan_ctx *ctx = dev_id; + struct pwm_fan_tach *tach = dev_id; - atomic_inc(&ctx->pulses); + atomic_inc(&tach->pulses); return IRQ_HANDLED; } @@ -59,14 +63,15 @@ static irqreturn_t pulse_handler(int irq, void *dev_id) static void sample_timer(struct timer_list *t) { struct pwm_fan_ctx *ctx = from_timer(ctx, t, rpm_timer); + struct pwm_fan_tach *tach = ctx->tach; unsigned int delta = ktime_ms_delta(ktime_get(), ctx->sample_start); int pulses; if (delta) { - pulses = atomic_read(&ctx->pulses); - atomic_sub(pulses, &ctx->pulses); - ctx->rpm = (unsigned int)(pulses * 1000 * 60) / - (ctx->pulses_per_revolution * delta); + pulses = atomic_read(&tach->pulses); + atomic_sub(pulses, &tach->pulses); + tach->rpm = (unsigned int)(pulses * 1000 * 60) / + (tach->pulses_per_revolution * delta); ctx->sample_start = ktime_get(); } @@ -138,8 +143,9 @@ static ssize_t rpm_show(struct device *dev, struct device_attribute *attr, char *buf) { struct pwm_fan_ctx *ctx = dev_get_drvdata(dev); + struct pwm_fan_tach *tach = ctx->tach; - return sprintf(buf, "%u\n", ctx->rpm); + return sprintf(buf, "%u\n", tach->rpm); } static SENSOR_DEVICE_ATTR_RW(pwm1, pwm, 0); @@ -324,14 +330,22 @@ static int pwm_fan_probe(struct platform_device *pdev) ctx->fan_group.attrs[0] = &sensor_dev_attr_pwm1.dev_attr.attr; if (tach_count > 0) { + struct pwm_fan_tach *tach; u32 ppr = 2; - ctx->irq = platform_get_irq(pdev, 0); - if (ctx->irq == -EPROBE_DEFER) - return ctx->irq; - if (ctx->irq > 0) { - ret = devm_request_irq(dev, ctx->irq, pulse_handler, 0, - pdev->name, ctx); + ctx->tach = devm_kzalloc(dev, sizeof(struct pwm_fan_tach), + GFP_KERNEL); + if (!ctx->tach) + return -ENOMEM; + + tach = ctx->tach; + + tach->irq = platform_get_irq(pdev, 0); + if (tach->irq == -EPROBE_DEFER) + return tach->irq; + if (tach->irq > 0) { + ret = devm_request_irq(dev, tach->irq, pulse_handler, 0, + pdev->name, tach); if (ret) { dev_err(dev, "Failed to request interrupt: %d\n", @@ -343,21 +357,21 @@ static int pwm_fan_probe(struct platform_device *pdev) of_property_read_u32(dev->of_node, "pulses-per-revolution", &ppr); - ctx->pulses_per_revolution = ppr; - if (!ctx->pulses_per_revolution) { + tach->pulses_per_revolution = ppr; + if (!tach->pulses_per_revolution) { dev_err(dev, "pulses-per-revolution can't be zero.\n"); return -EINVAL; } - sysfs_attr_init(&ctx->sensor_attr.dev_attr.attr); + sysfs_attr_init(&tach->sensor_attr.dev_attr.attr); - ctx->sensor_attr.dev_attr.attr.name = "fan1_input"; - ctx->sensor_attr.dev_attr.attr.mode = 0444; - ctx->sensor_attr.dev_attr.show = rpm_show; - ctx->fan_group.attrs[1] = &ctx->sensor_attr.dev_attr.attr; + tach->sensor_attr.dev_attr.attr.name = "fan1_input"; + tach->sensor_attr.dev_attr.attr.mode = 0444; + tach->sensor_attr.dev_attr.show = rpm_show; + ctx->fan_group.attrs[1] = &tach->sensor_attr.dev_attr.attr; dev_dbg(dev, "tach: irq=%d, pulses_per_revolution=%d\n", - ctx->irq, ctx->pulses_per_revolution); + tach->irq, tach->pulses_per_revolution); ctx->sample_start = ktime_get(); mod_timer(&ctx->rpm_timer, jiffies + HZ); From patchwork Fri Nov 13 15:08:53 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Barker X-Patchwork-Id: 11903903 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 49F89139F for ; Fri, 13 Nov 2020 15:09:40 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1CDBD2225D for ; Fri, 13 Nov 2020 15:09:40 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=konsulko.com header.i=@konsulko.com header.b="QZn2VvKu" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726691AbgKMPJj (ORCPT ); Fri, 13 Nov 2020 10:09:39 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37054 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726278AbgKMPJj (ORCPT ); Fri, 13 Nov 2020 10:09:39 -0500 Received: from mail-wm1-x342.google.com (mail-wm1-x342.google.com [IPv6:2a00:1450:4864:20::342]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4BE14C0613D1 for ; Fri, 13 Nov 2020 07:09:39 -0800 (PST) Received: by mail-wm1-x342.google.com with SMTP id h2so8453153wmm.0 for ; Fri, 13 Nov 2020 07:09:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=konsulko.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=pAWPrO2/9s7VsqZCjQ34zbmG6M+xaNdfHE34WnneOZk=; b=QZn2VvKu+HHXrMfpO40xQzAYWLDj8upVIgTkbOWN2xKzAbXX787rKXAR5V8RcpFmGb Pkg5tdRLqrNy3XBcTX7LllCUB08g6Ug/hqER0Rs0FN6mJKV4C9fsYlkyM6fyEbqhv2w9 4C+2NE6m6gYfABnmpaLkO3mEk0YlFkrLOGs1o= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=pAWPrO2/9s7VsqZCjQ34zbmG6M+xaNdfHE34WnneOZk=; b=WgzeGUf28YzQkFQGmFHn6PK2jUHqZtyZzaQ/EpEPWOdg7WuICr8ELFlJGs298ooZQ6 DbYCicDOiHbfiiJk/+hvsJRaWup2A4SodRfXXjt6djz7LAMHhyI39yJmesQhdnqRa1pt yDg0bCQ7FmezzwvYtYO7abR0nNOndMxSQRG5kCys6V6CkDFkhWlIq6oIHEUGbtEQtinN Vp1DehnJ5kUkx06LgSVBbBOqWaDeNNCSnmInlagoIGBydUu8//+3y66foqfSsisOzakz vg6wY5C+J4HXHdpsg7uw1LLTu8CygE5FvdLjHBMWM4iEBzFTh76VP4S1x3qHeL9sdJim BlFw== X-Gm-Message-State: AOAM531uH5nW6WGbfzF2Tm7rN0+HHeLWGtctm9pGDuCjdxHe7VoTq7q2 3xETvYSkIVlJ0Sdc97DP2cteqw== X-Google-Smtp-Source: ABdhPJyBGUMw98cCiEZbk8caErfSsMX1xsLfnNDqpivtp+KsocrKc9ZYKtcTSIYGAw6bvHYBwfU7gg== X-Received: by 2002:a05:600c:2ccb:: with SMTP id l11mr7309wmc.65.1605280177885; Fri, 13 Nov 2020 07:09:37 -0800 (PST) Received: from alpha.home.b5net.uk (cpc76132-clif11-2-0-cust249.12-4.cable.virginm.net. [80.7.160.250]) by smtp.gmail.com with ESMTPSA id v2sm11368221wrm.96.2020.11.13.07.09.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Nov 2020 07:09:37 -0800 (PST) From: Paul Barker To: Kamil Debski , Bartlomiej Zolnierkiewicz , Jean Delvare , Guenter Roeck Cc: Paul Barker , linux-hwmon@vger.kernel.org Subject: [PATCH v2 4/4] hwmon: pwm-fan: Support multiple fan tachometers Date: Fri, 13 Nov 2020 15:08:53 +0000 Message-Id: <20201113150853.155495-5-pbarker@konsulko.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201113150853.155495-1-pbarker@konsulko.com> References: <20201113150853.155495-1-pbarker@konsulko.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-hwmon@vger.kernel.org The pwm-fan driver is extended to support multiple fan tachometer signals connected to GPIO inputs. This is intended to support the case where a single PWM output signal is routed to multiple fans, each of which have a tachometer output connected back to a GPIO pin. The number of fan tachometer inputs is determined by the number of interrupt sources configured for the pwm-fan device. The number of pulses-per-revolution entries should match the number of interrupt sources so that each input has a value assigned. The fan tachometer measurements are exposed as sysfs files fan1_input, fan2_input, etc up to the number of configured inputs. Signed-off-by: Paul Barker --- drivers/hwmon/pwm-fan.c | 72 ++++++++++++++++++++++++----------------- 1 file changed, 43 insertions(+), 29 deletions(-) diff --git a/drivers/hwmon/pwm-fan.c b/drivers/hwmon/pwm-fan.c index 7c75ce78b36a..03a3b57e2d99 100644 --- a/drivers/hwmon/pwm-fan.c +++ b/drivers/hwmon/pwm-fan.c @@ -36,7 +36,8 @@ struct pwm_fan_ctx { struct pwm_device *pwm; struct regulator *reg_en; - struct pwm_fan_tach *tach; + int tach_count; + struct pwm_fan_tach *tachs; ktime_t sample_start; struct timer_list rpm_timer; @@ -63,15 +64,19 @@ static irqreturn_t pulse_handler(int irq, void *dev_id) static void sample_timer(struct timer_list *t) { struct pwm_fan_ctx *ctx = from_timer(ctx, t, rpm_timer); - struct pwm_fan_tach *tach = ctx->tach; unsigned int delta = ktime_ms_delta(ktime_get(), ctx->sample_start); - int pulses; + int i; if (delta) { - pulses = atomic_read(&tach->pulses); - atomic_sub(pulses, &tach->pulses); - tach->rpm = (unsigned int)(pulses * 1000 * 60) / - (tach->pulses_per_revolution * delta); + for (i = 0; i < ctx->tach_count; i++) { + struct pwm_fan_tach *tach = &ctx->tachs[i]; + int pulses; + + pulses = atomic_read(&tach->pulses); + atomic_sub(pulses, &tach->pulses); + tach->rpm = (unsigned int)(pulses * 1000 * 60) / + (tach->pulses_per_revolution * delta); + } ctx->sample_start = ktime_get(); } @@ -143,7 +148,8 @@ static ssize_t rpm_show(struct device *dev, struct device_attribute *attr, char *buf) { struct pwm_fan_ctx *ctx = dev_get_drvdata(dev); - struct pwm_fan_tach *tach = ctx->tach; + struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); + struct pwm_fan_tach *tach = &ctx->tachs[sensor_attr->index]; return sprintf(buf, "%u\n", tach->rpm); } @@ -267,8 +273,8 @@ static int pwm_fan_probe(struct platform_device *pdev) struct device *hwmon; int ret; struct pwm_state state = { }; - int tach_count; size_t sz; + int i; ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL); if (!ctx) @@ -317,30 +323,32 @@ static int pwm_fan_probe(struct platform_device *pdev) if (ret) return ret; - tach_count = platform_irq_count(pdev); - if (tach_count < 0) - return dev_err_probe(dev, tach_count, + ctx->tach_count = platform_irq_count(pdev); + if (ctx->tach_count < 0) + return dev_err_probe(dev, ctx->tach_count, "Could not get number of fan tachometer inputs\n"); + dev_dbg(dev, "%d fan tachometer inputs\n", ctx->tach_count); - sz = (2 + tach_count) * sizeof(struct attribute *); + sz = (2 + ctx->tach_count) * sizeof(struct attribute *); ctx->fan_group.attrs = devm_kzalloc(dev, sz, GFP_KERNEL); if (!ctx->fan_group.attrs) return -ENOMEM; ctx->fan_group.attrs[0] = &sensor_dev_attr_pwm1.dev_attr.attr; - if (tach_count > 0) { - struct pwm_fan_tach *tach; - u32 ppr = 2; - - ctx->tach = devm_kzalloc(dev, sizeof(struct pwm_fan_tach), - GFP_KERNEL); - if (!ctx->tach) + if (ctx->tach_count > 0) { + sz = ctx->tach_count * sizeof(struct pwm_fan_tach); + ctx->tachs = devm_kzalloc(dev, sz, GFP_KERNEL); + if (!ctx->tachs) return -ENOMEM; + } - tach = ctx->tach; + for (i = 0; i < ctx->tach_count; i++) { + struct pwm_fan_tach *tach = &ctx->tachs[i]; + u32 ppr = 2; + char *name; - tach->irq = platform_get_irq(pdev, 0); + tach->irq = platform_get_irq(pdev, i); if (tach->irq == -EPROBE_DEFER) return tach->irq; if (tach->irq > 0) { @@ -354,9 +362,10 @@ static int pwm_fan_probe(struct platform_device *pdev) } } - of_property_read_u32(dev->of_node, - "pulses-per-revolution", - &ppr); + of_property_read_u32_index(dev->of_node, + "pulses-per-revolution", + i, + &ppr); tach->pulses_per_revolution = ppr; if (!tach->pulses_per_revolution) { dev_err(dev, "pulses-per-revolution can't be zero.\n"); @@ -365,14 +374,19 @@ static int pwm_fan_probe(struct platform_device *pdev) sysfs_attr_init(&tach->sensor_attr.dev_attr.attr); - tach->sensor_attr.dev_attr.attr.name = "fan1_input"; + name = devm_kzalloc(dev, 16, GFP_KERNEL); + snprintf(name, 16, "fan%d_input", i + 1); + tach->sensor_attr.dev_attr.attr.name = name; tach->sensor_attr.dev_attr.attr.mode = 0444; tach->sensor_attr.dev_attr.show = rpm_show; - ctx->fan_group.attrs[1] = &tach->sensor_attr.dev_attr.attr; + tach->sensor_attr.index = i; + ctx->fan_group.attrs[i + 1] = &tach->sensor_attr.dev_attr.attr; - dev_dbg(dev, "tach: irq=%d, pulses_per_revolution=%d\n", - tach->irq, tach->pulses_per_revolution); + dev_dbg(dev, "%s: irq=%d, pulses_per_revolution=%d\n", + name, tach->irq, tach->pulses_per_revolution); + } + if (ctx->tach_count > 0) { ctx->sample_start = ktime_get(); mod_timer(&ctx->rpm_timer, jiffies + HZ); }