From patchwork Thu Oct 22 20:49:26 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alper Nebi Yasak X-Patchwork-Id: 1386423 X-Patchwork-Delegate: agust@denx.de Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20161025 header.b=BmoQjkYZ; dkim-atps=neutral Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4CHKHR50Hzz9sTD for ; Fri, 23 Oct 2020 07:49:55 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 731F88242B; Thu, 22 Oct 2020 22:49:51 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="BmoQjkYZ"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id D96048243D; Thu, 22 Oct 2020 22:49:49 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,SPF_HELO_NONE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-wr1-x444.google.com (mail-wr1-x444.google.com [IPv6:2a00:1450:4864:20::444]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id C2EBB823F3 for ; Thu, 22 Oct 2020 22:49:46 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=alpernebiyasak@gmail.com Received: by mail-wr1-x444.google.com with SMTP id h7so4977830wre.4 for ; Thu, 22 Oct 2020 13:49:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=cylYjc0/5z8kN9bnn88iCiyJIkQoBMpvOuMdQ58aEAE=; b=BmoQjkYZMOWhJrQrzRxHn8QBcQoSnFQ5ossbj2L57US+rnjLSf8a7IqSMROCIIc92a wffrKimXZ0hOnbAn9iQtEZqMbqPUdBXmuuZu7TgpQVEw5TayrJ8JYAjc/Ru6BF0wU1RB Fai/FZ399v0bpFr980+i8oLOifMBXZqGL0F8Hm9AbzONdZ+qxOl7ZhMOLU0KMvbRDjKA TRIifX28UbwXhkIbNYbO7Z55hySdtPMR2ChFKJaXno71/QsKwl1DSArV3ZcdhwwCwW2P 7Zb7JoT2qMGmZO4189QvhEZsMjUWocElzqn6j/DpuPEAoqlY6wozeEnG7ZpV8UMJomN8 RnHg== 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:mime-version :content-transfer-encoding; bh=cylYjc0/5z8kN9bnn88iCiyJIkQoBMpvOuMdQ58aEAE=; b=KWUhGnYUroYbQj8Q22dbL93s70WREBUPutkwJBuVassKUCDBePnJSHqhbrnm1aPj0F Qzj/0cM1Rn8TlQSvMSSC+j3w1IVcURb+EG6SWvWYdXbn1EHLkTfbkFlfpjGW+QB+0JuP J9an0dnC0+aMUDdbAPzrgqqlDf6a8RkuI9cJWRf5vpL8bymbB7RuYx/VeDe4Nf2mqd0g tDOgFYcLWJXx9nRi4ahRJHdxJSnrzPlZXrcqRlkVkOh+l7sbSkPQ8S5+r1yH4TOqvMJr IKUAfFLAkhfk9+4LlzcG9fRji7mruD4FibUmo8AdBwfShQyH0PODZ5QTVL2M2eZenESY ntZg== X-Gm-Message-State: AOAM533+IPrJSGPkf6hvc2EPXv19j6285N4vV6p8VfoHG2cU+Dqh5GKd Fg8XG7zIfJf2dmErrH8CXm5npKCU1rsaYg== X-Google-Smtp-Source: ABdhPJy8RAjos6nBSvwgC1rpR1q/jRnGVPXlPDUrmZfDa5MSPDqBq4HSGU7niA1ZZo7Fk+KI53B9lA== X-Received: by 2002:adf:ed52:: with SMTP id u18mr4807744wro.357.1603399786033; Thu, 22 Oct 2020 13:49:46 -0700 (PDT) Received: from localhost.localdomain ([178.233.176.159]) by smtp.gmail.com with ESMTPSA id m8sm5535534wrw.17.2020.10.22.13.49.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Oct 2020 13:49:45 -0700 (PDT) From: Alper Nebi Yasak To: u-boot@lists.denx.de Cc: Anatolij Gustschin , Heiko Schocher , Sam Shih , Neil Armstrong , Yash Shah , Michael Auchter , Simon Glass , Alper Nebi Yasak Subject: [PATCH v3 1/2] video: backlight: Support PWMs without a known period_ns Date: Thu, 22 Oct 2020 23:49:26 +0300 Message-Id: <20201022204927.26290-1-alpernebiyasak@gmail.com> X-Mailer: git-send-email 2.28.0 MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.102.3 at phobos.denx.de X-Virus-Status: Clean The PWM device provided by Chrome OS EC doesn't really support anything other than setting a relative duty cycle. To support it as a backlight, this patch makes the PWM period optional in the device tree and pretends the valid brightness range is its period_ns. Also adds a sandbox test for a PWM channel that has a fixed period, checking that the resulting duty_cycle matches on a set_config() even if the requested period_ns can't be set. Signed-off-by: Alper Nebi Yasak Reviewed-by: Simon Glass --- Changes in v3: - Add tag: "Reviewed-by: Simon Glass " - Adjust to match recently merged duty cycle calculation fix. v2: https://patchwork.ozlabs.org/project/uboot/patch/20200926200851.4388-1-alpernebiyasak@gmail.com/ Changes in v2: - Add sandbox test to pwm.c (using sandbox_pwm's channel 2) - Add comments to pwm_set_config() and pwm_ops->set_config() v1: https://patchwork.ozlabs.org/project/uboot/patch/20200923165231.18188-1-alpernebiyasak@gmail.com/ drivers/pwm/sandbox_pwm.c | 11 +++++++++-- drivers/video/pwm_backlight.c | 20 ++++++++++++++------ include/pwm.h | 8 ++++++++ test/dm/pwm.c | 11 +++++++++++ 4 files changed, 42 insertions(+), 8 deletions(-) diff --git a/drivers/pwm/sandbox_pwm.c b/drivers/pwm/sandbox_pwm.c index 28988187e039..3fd2e78c909d 100644 --- a/drivers/pwm/sandbox_pwm.c +++ b/drivers/pwm/sandbox_pwm.c @@ -59,8 +59,15 @@ static int sandbox_pwm_set_config(struct udevice *dev, uint channel, if (channel >= NUM_CHANNELS) return -ENOSPC; chan = &priv->chan[channel]; - chan->period_ns = period_ns; - chan->duty_ns = duty_ns; + + if (channel == 2) { + /* Pretend to have some fixed period */ + chan->period_ns = 4096; + chan->duty_ns = duty_ns * 4096 / period_ns; + } else { + chan->period_ns = period_ns; + chan->duty_ns = duty_ns; + } return 0; } diff --git a/drivers/video/pwm_backlight.c b/drivers/video/pwm_backlight.c index 9519180ceb39..18b7a00b033c 100644 --- a/drivers/video/pwm_backlight.c +++ b/drivers/video/pwm_backlight.c @@ -62,10 +62,17 @@ static int set_pwm(struct pwm_backlight_priv *priv) uint duty_cycle; int ret; - duty_cycle = priv->period_ns * (priv->cur_level - priv->min_level) / - (priv->max_level - priv->min_level); - ret = pwm_set_config(priv->pwm, priv->channel, priv->period_ns, - duty_cycle); + if (priv->period_ns) { + duty_cycle = priv->period_ns * (priv->cur_level - priv->min_level) / + (priv->max_level - priv->min_level); + ret = pwm_set_config(priv->pwm, priv->channel, priv->period_ns, + duty_cycle); + } else { + /* PWM driver will internally scale these like the above. */ + ret = pwm_set_config(priv->pwm, priv->channel, + priv->max_level - priv->min_level, + priv->cur_level - priv->min_level); + } if (ret) return log_ret(ret); @@ -213,10 +220,11 @@ static int pwm_backlight_ofdata_to_platdata(struct udevice *dev) log_debug("Cannot get PWM: ret=%d\n", ret); return log_ret(ret); } - if (args.args_count < 2) + if (args.args_count < 1) return log_msg_ret("Not enough arguments to pwm\n", -EINVAL); priv->channel = args.args[0]; - priv->period_ns = args.args[1]; + if (args.args_count > 1) + priv->period_ns = args.args[1]; if (args.args_count > 2) priv->polarity = args.args[2]; diff --git a/include/pwm.h b/include/pwm.h index 54ae3e64c310..97bda0856e6c 100644 --- a/include/pwm.h +++ b/include/pwm.h @@ -15,6 +15,10 @@ struct pwm_ops { /** * set_config() - Set the PWM configuration * + * Change both the PWM device's period and it's duty period if + * possible. Otherwise, set an appropriate duty period that best + * matches the given period_ns / duty_ns ratio for the device. + * * @dev: PWM device to update * @channel: PWM channel to update * @period_ns: PWM period in nanoseconds @@ -49,6 +53,10 @@ struct pwm_ops { /** * pwm_set_config() - Set the PWM configuration * + * Change both the PWM device's period and it's duty period if + * possible. Otherwise, set an appropriate duty period that best + * matches the given period_ns / duty_ns ratio for the device. + * * @dev: PWM device to update * @channel: PWM channel to update * @period_ns: PWM period in nanoseconds diff --git a/test/dm/pwm.c b/test/dm/pwm.c index 0de6dba1ba64..b624cf3d6558 100644 --- a/test/dm/pwm.c +++ b/test/dm/pwm.c @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -14,6 +15,10 @@ static int dm_test_pwm_base(struct unit_test_state *uts) { struct udevice *dev; + uint period_ns; + uint duty_ns; + bool enable; + bool polarity; ut_assertok(uclass_get_device(UCLASS_PWM, 0, &dev)); ut_assertnonnull(dev); @@ -24,6 +29,12 @@ static int dm_test_pwm_base(struct unit_test_state *uts) ut_asserteq(-ENOSPC, pwm_set_enable(dev, 3, true)); ut_assertok(pwm_set_invert(dev, 0, true)); + ut_assertok(pwm_set_config(dev, 2, 100, 50)); + ut_assertok(sandbox_pwm_get_config(dev, 2, &period_ns, &duty_ns, + &enable, &polarity)); + ut_asserteq(period_ns, 4096); + ut_asserteq(duty_ns, 50 * 4096 / 100); + ut_assertok(uclass_get_device(UCLASS_PWM, 1, &dev)); ut_asserteq(-ENODEV, uclass_get_device(UCLASS_PWM, 2, &dev));