From patchwork Wed Nov 9 11:37:24 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Emil Renner Berthing X-Patchwork-Id: 17497 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp284806wru; Wed, 9 Nov 2022 03:42:19 -0800 (PST) X-Google-Smtp-Source: AMsMyM410uXymts9quTeJAA+sUxfEscEl0m2KnVHLHdt9UPynN4cjHAeuWaXVDvb0Djpi4aca0Sp X-Received: by 2002:a63:5b23:0:b0:46f:6d7d:cd10 with SMTP id p35-20020a635b23000000b0046f6d7dcd10mr52456627pgb.194.1667994139219; Wed, 09 Nov 2022 03:42:19 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1667994139; cv=none; d=google.com; s=arc-20160816; b=C+gUmZoPQw/rIl3i9ui3JyyXhxnbl3wD0gL8i4h0lZC767isOtCc3Nr2Xx0oFEzY+K L/uZszu+tNy0F50cIU93NVULNM9rSg0aHAO1F14d8zFgg3CwyBJAb3PeV/l3cXX0jbs1 nOJyMW2HnZWjtLgWfyldItym110KiHE0tOI/dDV0m+3fUN2DsEC/Pni2nspoFZfgYdlS 0v0LaWCAQ3XMl9vBC3Abz6+lji1PcvX2Lie//JRoxr1FKqsFMf+glUihCB8hi2+YB1lD mj6HuPLkvEoINGFH1/hnU8fngmLn72/3lhAZLcIWcGitTAOS3h+eZ35CA8B3B5y/EZ3p 8yQg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=PUPJbzepJx5AFbllHqJhTSHtk/HwYip0Muk/9EDtYSQ=; b=crty8U4hQCZtUDnIn1jLBHWN1Ku+XG1tFiB7L8V1+2pxqqdZw/cIgQYnFs7O3lyV8q QcfIcmC7uSc80nR0oJrJ1CphRsmCz8x4urwVbza3gef7o0kc3nLrkdm1N5vRXLHThjNj 6j8iFyMTeayVMnM0OJNszctnIsvraB2LRbEp0t9/IjWwoekUk6nkapp2zqbGzzP06eOg tt263iytpeVM35SyJsPpgH6AJEEAgJVKjfMzg8zo588KZaj24BuDp/SfIb+P+oMxtL5c WDjZy2JdcQOoPVnbAj4Ryl8OL2lH0dRgAJZ3k7wRyjvjWgFGIw/2+0zjEqARpvkZTqRx IJmA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@canonical.com header.s=20210705 header.b=MmrQ2LVI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=canonical.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id w17-20020a170903311100b0017a7506e0dcsi14002671plc.231.2022.11.09.03.42.04; Wed, 09 Nov 2022 03:42:19 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@canonical.com header.s=20210705 header.b=MmrQ2LVI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=canonical.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230095AbiKILhn (ORCPT + 99 others); Wed, 9 Nov 2022 06:37:43 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53504 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230314AbiKILhc (ORCPT ); Wed, 9 Nov 2022 06:37:32 -0500 Received: from smtp-relay-internal-1.canonical.com (smtp-relay-internal-1.canonical.com [185.125.188.123]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F17C52EF57 for ; Wed, 9 Nov 2022 03:37:28 -0800 (PST) Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp-relay-internal-1.canonical.com (Postfix) with ESMTPS id EB082412BF for ; Wed, 9 Nov 2022 11:37:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=canonical.com; s=20210705; t=1667993846; bh=PUPJbzepJx5AFbllHqJhTSHtk/HwYip0Muk/9EDtYSQ=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=MmrQ2LVIc+NXpRvTTrrTO9+fRdLs4V/YtQyh4kLdi0EGsZ5P7iXmNPi+AZrx33T3I bH0huCjcUczj80ltgE4kTX5Uippw3jbT9tloKL/v46favap5NHJ4484BZDcxXPvLVo vPqQSnq8E6N3li8AK00dbCkvb0/2ZYfSsikF3OmXhGZbUNnnr3XnXv/hCgx91ySqXr ettLO4wtSO2Y3TnjwnFbd/dzJ6sLN9YKi9gVgY5o1EeuMejb6z+sYTlPyyzi0YGwON Jxm/8LYmfPUFrHiOUjC+8t9HjRqUkc3tXuq3ytxqrHM2faddufJXbSKChPlAUJ9SmL ybHqD8qFi1gzw== Received: by mail-wm1-f72.google.com with SMTP id r187-20020a1c44c4000000b003c41e9ae97dso890127wma.6 for ; Wed, 09 Nov 2022 03:37:26 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=PUPJbzepJx5AFbllHqJhTSHtk/HwYip0Muk/9EDtYSQ=; b=oyt93wBo7I3u5KuUdqs7ElFM3oFPIU6IzqguKgBDwXthOEgEcDUV7xTl4tUckyRG1N pZwXTYDRyEfnrIXpAVVaDc5EzmGuq4gzk5jP2vp6aELY38E+oJWQaecP2j1Nv2TnLcxw 22ZBvFc8BqnQDe6hgHcknxEJ4llf4TU6qrikkyHh3hZoZo1exVVPbJtTulmvZ2xz70pN x7xTCllodkpAMUpQpxq7aT4DcTgr+3P5juJw/dqGyVXvKDaAGnysnucx0EjZyYrP0RjP oE6PO7mJEAVh/3MXRjtzuKOQ4MwQ7Zt6d/lfth1o7QQAsex8jcGOwZ92u0cFIGLjCyQI B18A== X-Gm-Message-State: ACrzQf0E/30BtWuxGpkUTE2wdYmTUCCvBFQgFv8GFmBIRAyaii7xyGdV T1wiIqOMlJhZ3/nagKr/ZP/+HACNx8+SIv5z39Z2MlqFWCtsO+Pqvh6UEhEqiIaeHPlia6P9AT2 ZJKxbJ0PNkh6wfvL9TXPOC4vmMIR85X4GRnJ5IqMajg== X-Received: by 2002:adf:dd88:0:b0:236:57e3:fc86 with SMTP id x8-20020adfdd88000000b0023657e3fc86mr37599038wrl.493.1667993846676; Wed, 09 Nov 2022 03:37:26 -0800 (PST) X-Received: by 2002:adf:dd88:0:b0:236:57e3:fc86 with SMTP id x8-20020adfdd88000000b0023657e3fc86mr37599028wrl.493.1667993846479; Wed, 09 Nov 2022 03:37:26 -0800 (PST) Received: from stitch.. (80.71.140.73.ipv4.parknet.dk. [80.71.140.73]) by smtp.gmail.com with ESMTPSA id m27-20020a05600c3b1b00b003cf6c2f9513sm1487261wms.2.2022.11.09.03.37.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Nov 2022 03:37:26 -0800 (PST) From: Emil Renner Berthing To: Thierry Reding , =?utf-8?q?Uwe_Kleine-K=C3=B6n?= =?utf-8?q?ig?= , Palmer Dabbelt , Paul Walmsley , Atish Patra , "Wesley W. Terpstra" Cc: linux-pwm@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Subject: [PATCH v2] pwm: sifive: Always let the first pwm_apply_state succeed Date: Wed, 9 Nov 2022 12:37:24 +0100 Message-Id: <20221109113724.519021-1-emil.renner.berthing@canonical.com> X-Mailer: git-send-email 2.37.2 MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749018622776304900?= X-GMAIL-MSGID: =?utf-8?q?1749018622776304900?= Commit 2cfe9bbec56ea579135cdd92409fff371841904f added support for the RGB and green PWM controlled LEDs on the HiFive Unmatched board managed by the leds-pwm-multicolor and leds-pwm drivers respectively. All three colours of the RGB LED and the green LED run from different lines of the same PWM, but with the same period so this works fine when the LED drivers are loaded one after the other. Unfortunately it does expose a race in the PWM driver when both LED drivers are loaded at roughly the same time. Here is an example: | Thread A | Thread B | | led_pwm_mc_probe | led_pwm_probe | | devm_fwnode_pwm_get | | | pwm_sifive_request | | | ddata->user_count++ | | | | devm_fwnode_pwm_get | | | pwm_sifive_request | | | ddata->user_count++ | | ... | ... | | pwm_state_apply | pwm_state_apply | | pwm_sifive_apply | pwm_sifive_apply | Now both calls to pwm_sifive_apply will see that ddata->approx_period, initially 0, is different from the requested period and the clock needs to be updated. But since ddata->user_count >= 2 both calls will fail with -EBUSY, which will then cause both LED drivers to fail to probe. Fix it by letting the first call to pwm_sifive_apply update the clock even when ddata->user_count != 1. Fixes: 9e37a53eb051 ("pwm: sifive: Add a driver for SiFive SoC PWM") Signed-off-by: Emil Renner Berthing --- drivers/pwm/pwm-sifive.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/drivers/pwm/pwm-sifive.c b/drivers/pwm/pwm-sifive.c index 2d4fa5e5fdd4..b3c60ec72a6e 100644 --- a/drivers/pwm/pwm-sifive.c +++ b/drivers/pwm/pwm-sifive.c @@ -159,7 +159,13 @@ static int pwm_sifive_apply(struct pwm_chip *chip, struct pwm_device *pwm, mutex_lock(&ddata->lock); if (state->period != ddata->approx_period) { - if (ddata->user_count != 1) { + /* + * Don't let a 2nd user change the period underneath the 1st user. + * However if ddate->approx_period == 0 this is the first time we set + * any period, so let whoever gets here first set the period so other + * users who agree on the period won't fail. + */ + if (ddata->user_count != 1 && ddata->approx_period) { mutex_unlock(&ddata->lock); return -EBUSY; }