From patchwork Wed Sep 23 16:52:30 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: 1369961 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=85.214.62.61; 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=Dpzm1SE+; dkim-atps=neutral Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4BxPPG41SPz9sTC for ; Thu, 24 Sep 2020 02:52:50 +1000 (AEST) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 3CBF382478; Wed, 23 Sep 2020 18:52: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=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="Dpzm1SE+"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 2CE3D82433; Wed, 23 Sep 2020 18:52:45 +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 autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-wr1-x442.google.com (mail-wr1-x442.google.com [IPv6:2a00:1450:4864:20::442]) (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 9AE8982359 for ; Wed, 23 Sep 2020 18:52:42 +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-x442.google.com with SMTP id m6so835022wrn.0 for ; Wed, 23 Sep 2020 09:52:42 -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=vacc917CHygBFE3xiid4k4VaLwjIAocZl9GQAueA++0=; b=Dpzm1SE+4LmsQf70LEJksaOFN07J0YjNQoqgI4XpZSDNv6pwDbv/R4okEAbzcVfHll oHjeWn7IqUb+lmEf/nm3y8nvph3yAEIWyMpe1GOSVHwMSLOng8lH3xoat1o87uRaqBcV ovTYYLdrP/CqzFNVO7S8ZlRbrnJ3yIDe8TzsZHdaN45yvAueLI9QUXrOKp9WPeAJ92un 0FKc28d24tUBfSNWdkJWoXV4Ri80WwMrTIz133ESX2q1RRxh25xHzn2dHs9UgIeGfoic j27WMgD5FF6ceqGZ8XQaAbAjcbsYmB8PTpusFtvDbYBTLdqH7rN/V3ON3SwG50oapfBr PwnQ== 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=vacc917CHygBFE3xiid4k4VaLwjIAocZl9GQAueA++0=; b=hJMBtaHf/V21aB7E1u97iTayWr5SHfLYuEKS3aYROMsv+flmVbHiitERNBh46r/6Hy UPxS8jTBuss+IzLux24CtSLEV2Z+QjiW+9zyQe+LF3Qb0FS/Os1/mpqBPIdT6/rzXxx8 7nGvWe2W16luGNL4fiGO2noSJ1lgvIu5CZRpj6myO1XP0i7f44FzdOMa+3JZWuVQCNmX p6kYEAYteprsN1ZKeTuJolVkADweByn9cYz5ZqPhWoJkoWytjkWa9srjoEftN5OOWRze fgtkUb2Z03L8AiqCWTsl+v8TiY3ojzp2wbEz5HnLsQp9t7fyoG9/D8LpSz/Zi/NDC7eU jfeA== X-Gm-Message-State: AOAM533zzUPfusmktKnHOb0Rr7IfOK1npbXCNaC1Dh3heVEQak0KKWYw dRCDlAarNkP9qxMGCHex0Lq6iCzJi8Y= X-Google-Smtp-Source: ABdhPJw/xlx5KTJeVeMkfk6zB46cn/H0TM9XPrjBlt4CQIAiX+/FqPiZnercuV4R5ecG7VuP/GsdbA== X-Received: by 2002:adf:e407:: with SMTP id g7mr553471wrm.349.1600879961926; Wed, 23 Sep 2020 09:52:41 -0700 (PDT) Received: from localhost.localdomain ([178.233.178.9]) by smtp.gmail.com with ESMTPSA id d18sm406893wrm.10.2020.09.23.09.52.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 23 Sep 2020 09:52:41 -0700 (PDT) From: Alper Nebi Yasak To: u-boot@lists.denx.de Cc: Sam Shih , Simon Glass , Heiko Schocher , Yash Shah , Michael Auchter , Anatolij Gustschin , Alper Nebi Yasak Subject: [PATCH 1/2] video: backlight: Support PWMs without a known period_ns Date: Wed, 23 Sep 2020 19:52:30 +0300 Message-Id: <20200923165231.18188-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. Signed-off-by: Alper Nebi Yasak --- drivers/video/pwm_backlight.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/drivers/video/pwm_backlight.c b/drivers/video/pwm_backlight.c index 468a5703bd..4c2a307462 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 + 1); - 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 + 1); + 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 + 1, + 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]; From patchwork Wed Sep 23 16:52:31 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: 1369962 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=dKxNKweU; 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 4BxPPf44lWz9sSt for ; Thu, 24 Sep 2020 02:53:10 +1000 (AEST) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 48D64824A8; Wed, 23 Sep 2020 18:52:52 +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="dKxNKweU"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id DBC108249D; Wed, 23 Sep 2020 18:52:48 +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 autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-wr1-x442.google.com (mail-wr1-x442.google.com [IPv6:2a00:1450:4864:20::442]) (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 3207082359 for ; Wed, 23 Sep 2020 18:52:45 +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-x442.google.com with SMTP id j2so774703wrx.7 for ; Wed, 23 Sep 2020 09:52:45 -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:in-reply-to:references :mime-version:content-transfer-encoding; bh=P6o80df4BhqBrKShEnK7eewmfAPWmKzvdQczegS23Hg=; b=dKxNKweUN/ofK4kU4Renvg6qHySy5PtOrsq0EAB3EJ5QbEv+9bb+wtZNOtZfIr6JXv d9zGkFaoXbHD9ZqkDPfT3RGc55iClKfPxpB+DJddONtdVP+53/HBgh/vfVAIJW0xwdWT sNmiIYWsWrumHl2K2e/V2E1RbD9w26cQRP1Kf8ZlUk+4stEqQjrqgpzRBPrFQ9szkpfR I3ifdZDx9h2qrmm+duRx6v2nQ9HZsa17msNLjWZdgQufC+HDR5tG4Tb45ak37AFDOBK9 W9UQwYABYo0HzsiijtmKthTM/djuBscHKAUP6zspeUiLF99AF7vFbYuHEnQ1qqUTp6UC X/vQ== 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=P6o80df4BhqBrKShEnK7eewmfAPWmKzvdQczegS23Hg=; b=BuLwCzaQISMQkoYGNKxQ/P0de8zzuY/MmeInx2ZtfiIcZmLw0/qXi5P5GQEUJcEcRL XkzRNRe75DXRq5xNGHsMWKK0eJ6ETYNwR4gNOs9NvVNew4KaamXxrN+nMoZ4Hvb0fQnm N019l990Ng21WAyt8vGKn5T/LPlDbbDO6u27GTcfEtqE1tPRx8DTr/WyvFtEIfTpjgB+ 8oL3mVekFqYYFKAzta4eIiI3j3bEQXtXKHyFnEjUAu7mz+tSh7BTbf6ltes5vMfYyb3y z2vS2Ga9OekVphknwnhWQG2YxHMTX9xCs8uubMyOkFzzLe4e1ncPQaq+jpq0j2J8ngBM vUKA== X-Gm-Message-State: AOAM531vfCnjDuBaTVWxvvkQvTqhWiygbN2kd95HXnClfeJsaOrBenua +ZExcZ4GoeiXWt3G2MiyjxjrgL1GYOiuWA== X-Google-Smtp-Source: ABdhPJxJzEwfv3INmNXOQ6s7VtbtZ3vpm+T5UBeTIaszSGmARe9E2MAbKjK1LIebtgF5rsm2fDc+vg== X-Received: by 2002:adf:83c3:: with SMTP id 61mr545813wre.287.1600879964339; Wed, 23 Sep 2020 09:52:44 -0700 (PDT) Received: from localhost.localdomain ([178.233.178.9]) by smtp.gmail.com with ESMTPSA id d18sm406893wrm.10.2020.09.23.09.52.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 23 Sep 2020 09:52:43 -0700 (PDT) From: Alper Nebi Yasak To: u-boot@lists.denx.de Cc: Sam Shih , Simon Glass , Heiko Schocher , Yash Shah , Michael Auchter , Anatolij Gustschin , Alper Nebi Yasak Subject: [PATCH 2/2] pwm: Add a driver for Chrome OS EC PWM Date: Wed, 23 Sep 2020 19:52:31 +0300 Message-Id: <20200923165231.18188-2-alpernebiyasak@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200923165231.18188-1-alpernebiyasak@gmail.com> References: <20200923165231.18188-1-alpernebiyasak@gmail.com> 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 This PWM is used in rk3399-gru-bob and rk3399-gru-kevin to control the display brightness. We can only change the duty cycle, so on set_config() we just try to match the duty cycle that dividing duty_ns by period_ns gives us. To disable, we set the duty cycle to zero while keeping the old value for when we want to re-enable it. The cros_ec_set_pwm_duty() function is taken from Depthcharge's cros_ec_set_bl_pwm_duty() but modified to use the generic pwm type. The driver itself is very loosely based on rk_pwm.c for the general pwm driver structure. Signed-off-by: Alper Nebi Yasak --- I'm testing on a rk3399-gru-kevin with a lot of other patches to get it (and it's screen) barely working, but using something like the following in a rk_board_late_init() added to gru.c gets the backlight to change: struct *udevice panel; uclass_first_device_err(UCLASS_PANEL, &panel); panel_enable_backlight(panel); panel_set_backlight(panel, 50); panel_set_backlight(panel, 100); The actual tree I'm testing and how I'm getting it to chainload from the vendor firmware (see commit message there) is available here: https://github.com/alpernebbi/u-boot/tree/rk3399-gru-kevin/wip (currently at commit 2cdb2cc4fb503dd5f6958e8f30406293e90b835) drivers/misc/cros_ec.c | 14 +++++++ drivers/pwm/Kconfig | 9 +++++ drivers/pwm/Makefile | 1 + drivers/pwm/cros_ec_pwm.c | 82 +++++++++++++++++++++++++++++++++++++++ include/cros_ec.h | 13 +++++++ 5 files changed, 119 insertions(+) create mode 100644 drivers/pwm/cros_ec_pwm.c diff --git a/drivers/misc/cros_ec.c b/drivers/misc/cros_ec.c index a5534b1667..a9a1eb8a25 100644 --- a/drivers/misc/cros_ec.c +++ b/drivers/misc/cros_ec.c @@ -1110,6 +1110,20 @@ int cros_ec_battery_cutoff(struct udevice *dev, uint8_t flags) return 0; } +int cros_ec_set_pwm_duty(struct udevice *dev, uint8_t index, uint16_t duty) +{ + struct ec_params_pwm_set_duty p; + + p.duty = duty; + p.pwm_type = EC_PWM_TYPE_GENERIC; + p.index = index; + + if (ec_command(dev, EC_CMD_PWM_SET_DUTY, 0, &p, sizeof(p), + NULL, 0) < 0) + return -1; + return 0; +} + int cros_ec_set_ldo(struct udevice *dev, uint8_t index, uint8_t state) { struct ec_params_ldo_set params; diff --git a/drivers/pwm/Kconfig b/drivers/pwm/Kconfig index 61eb468cde..73b0e5ed1e 100644 --- a/drivers/pwm/Kconfig +++ b/drivers/pwm/Kconfig @@ -9,6 +9,15 @@ config DM_PWM frequency/period can be controlled along with the proportion of that time that the signal is high. +config PWM_CROS_EC + bool "Enable support for the Chrome OS EC PWM" + depends on DM_PWM + help + This PWM is found on several Chrome OS devices and controlled by + the Chrome OS embedded controller. It may be used to control the + screen brightness and/or the keyboard backlight depending on the + device. + config PWM_EXYNOS bool "Enable support for the Exynos PWM" depends on DM_PWM diff --git a/drivers/pwm/Makefile b/drivers/pwm/Makefile index 0f4e84b04d..7af13538de 100644 --- a/drivers/pwm/Makefile +++ b/drivers/pwm/Makefile @@ -10,6 +10,7 @@ obj-$(CONFIG_DM_PWM) += pwm-uclass.o +obj-$(CONFIG_PWM_CROS_EC) += cros_ec_pwm.o obj-$(CONFIG_PWM_EXYNOS) += exynos_pwm.o obj-$(CONFIG_PWM_IMX) += pwm-imx.o pwm-imx-util.o obj-$(CONFIG_PWM_MTK) += pwm-mtk.o diff --git a/drivers/pwm/cros_ec_pwm.c b/drivers/pwm/cros_ec_pwm.c new file mode 100644 index 0000000000..781f1a1eae --- /dev/null +++ b/drivers/pwm/cros_ec_pwm.c @@ -0,0 +1,82 @@ +// SPDX-License-Identifier: GPL-2.0+ + +#include +#include +#include +#include +#include +#include + +struct cros_ec_pwm_priv { + bool enabled; + uint duty; +}; + +static int cros_ec_pwm_set_config(struct udevice *dev, uint channel, + uint period_ns, uint duty_ns) +{ + struct cros_ec_pwm_priv *priv = dev_get_priv(dev); + uint duty; + int ret; + + debug("%s: period_ns=%u, duty_ns=%u asked\n", __func__, + period_ns, duty_ns); + + /* No way to set the period, only a relative duty cycle */ + duty = EC_PWM_MAX_DUTY * duty_ns / period_ns; + if (duty > EC_PWM_MAX_DUTY) + duty = EC_PWM_MAX_DUTY; + + if (!priv->enabled) { + priv->duty = duty; + debug("%s: duty=%#x to-be-set\n", __func__, duty); + return 0; + } + + ret = cros_ec_set_pwm_duty(dev->parent, channel, duty); + if (ret) { + debug("%s: duty=%#x failed\n", __func__, duty); + return ret; + } + + priv->duty = duty; + debug("%s: duty=%#x set\n", __func__, duty); + return 0; +} + +static int cros_ec_pwm_set_enable(struct udevice *dev, uint channel, + bool enable) +{ + struct cros_ec_pwm_priv *priv = dev_get_priv(dev); + int ret; + + ret = cros_ec_set_pwm_duty(dev->parent, channel, + enable ? priv->duty : 0); + if (ret) { + debug("%s: enable=%d failed\n", __func__, enable); + return ret; + } + + priv->enabled = enable; + debug("%s: enable=%d (duty=%#x) set\n", __func__, + enable, priv->duty); + return 0; +} + +static const struct pwm_ops cros_ec_pwm_ops = { + .set_config = cros_ec_pwm_set_config, + .set_enable = cros_ec_pwm_set_enable, +}; + +static const struct udevice_id cros_ec_pwm_ids[] = { + { .compatible = "google,cros-ec-pwm" }, + { } +}; + +U_BOOT_DRIVER(cros_ec_pwm) = { + .name = "cros_ec_pwm", + .id = UCLASS_PWM, + .of_match = cros_ec_pwm_ids, + .ops = &cros_ec_pwm_ops, + .priv_auto_alloc_size = sizeof(struct cros_ec_pwm_priv), +}; diff --git a/include/cros_ec.h b/include/cros_ec.h index f4b9b7a5c2..202e5c2c74 100644 --- a/include/cros_ec.h +++ b/include/cros_ec.h @@ -443,6 +443,19 @@ int cros_ec_efs_verify(struct udevice *dev, enum ec_flash_region region); */ int cros_ec_battery_cutoff(struct udevice *dev, uint8_t flags); +/** + * cros_ec_set_pwm_duty() - Set duty cycle of a generic pwm + * + * Note that duty value needs to be passed to the EC as a 16 bit number + * for increased precision. + * + * @param dev CROS-EC device + * @param index Index of the pwm + * @param duty Desired duty cycle, in 0..EC_PWM_MAX_DUTY range. + * @return 0 if OK, -ve on error + */ +int cros_ec_set_pwm_duty(struct udevice *dev, uint8_t index, uint16_t duty); + /** * cros_ec_read_limit_power() - Check if power is limited by batter/charger *