From patchwork Sat Nov 17 10:55:46 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Courbot X-Patchwork-Id: 1759101 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork1.kernel.org Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) by patchwork1.kernel.org (Postfix) with ESMTP id C90093FC8A for ; Sat, 17 Nov 2012 10:58:30 +0000 (UTC) Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.76 #1 (Red Hat Linux)) id 1TZg4s-0000vU-7S; Sat, 17 Nov 2012 10:56:46 +0000 Received: from mail-pa0-f49.google.com ([209.85.220.49]) by merlin.infradead.org with esmtps (Exim 4.76 #1 (Red Hat Linux)) id 1TZg4G-0000pk-Pd for linux-arm-kernel@lists.infradead.org; Sat, 17 Nov 2012 10:56:12 +0000 Received: by mail-pa0-f49.google.com with SMTP id bi5so2260872pad.36 for ; Sat, 17 Nov 2012 02:56:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:from:to:cc:subject:date:message-id:x-mailer:in-reply-to :references; bh=I2XADu5QXIVBL0LD/C6kDspNCD5LIOxPNjRQHdkMfRY=; b=EE7D54+Hec8NTX12bo8p7B7Zb/T/pT4ZJR6hfGxOjVP8oi8VBL8IAX7RMtwwzR++3B nnNEKCid/y1Rq+JvkHKd4ruz3jTiOBPZV2ejwCee8Zv6IYesdK0ybzB1su0U4hZ9lFLs cAEbTpt8jupD7tz/ZRUQc+4Q9AWuwr6PW4jDzFCjNeTrWZ2LjDj2x6Xyw/ZbR+eXl96g zjtcaceN+Nmr7XvfyyvsEx0IeGCLWAov+OC8k/7OjqbT6ltcxQvHpuN4Vs2MvXLWpg61 9qCCLTFi/gq6wr+JcGQ3Ic7RkalDeXDI8UKiqDqn/tcQOZK5QnFA66Nl+CUPt+uZA/7D VvCg== Received: by 10.66.75.101 with SMTP id b5mr20464565paw.45.1353149766333; Sat, 17 Nov 2012 02:56:06 -0800 (PST) Received: from localhost.localdomain (softbank126010191003.bbtec.net. [126.10.191.3]) by mx.google.com with ESMTPS id ju7sm2766307pbb.60.2012.11.17.02.56.03 (version=TLSv1/SSLv3 cipher=OTHER); Sat, 17 Nov 2012 02:56:05 -0800 (PST) From: Alexandre Courbot To: Anton Vorontsov , Stephen Warren , Thierry Reding , Mark Zhang , Grant Likely , Rob Herring , Mark Brown , David Woodhouse , Arnd Bergmann Subject: [PATCHv9 2/3] pwm_backlight: use power sequences Date: Sat, 17 Nov 2012 19:55:46 +0900 Message-Id: <1353149747-31871-3-git-send-email-acourbot@nvidia.com> X-Mailer: git-send-email 1.8.0 In-Reply-To: <1353149747-31871-1-git-send-email-acourbot@nvidia.com> References: <1353149747-31871-1-git-send-email-acourbot@nvidia.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20121117_055609_097344_A3387987 X-CRM114-Status: GOOD ( 27.21 ) X-Spam-Score: -2.6 (--) X-Spam-Report: SpamAssassin version 3.3.2 on merlin.infradead.org summary: Content analysis details: (-2.6 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.7 RCVD_IN_DNSWL_LOW RBL: Sender listed at http://www.dnswl.org/, low trust [209.85.220.49 listed in list.dnswl.org] 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider (gnurou[at]gmail.com) -0.0 SPF_PASS SPF: sender matches SPF record -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature Cc: Alexandre Courbot , linux-fbdev@vger.kernel.org, linux-pm@vger.kernel.org, devicetree-discuss@lists.ozlabs.org, linux-kernel@vger.kernel.org, Alexandre Courbot , linux-tegra@vger.kernel.org, linux-arm-kernel@lists.infradead.org X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: linux-arm-kernel-bounces@lists.infradead.org Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org Make use of the power sequences specified in the device tree or platform data to control how the backlight is powered on and off. Signed-off-by: Alexandre Courbot Reviewed-by: Stephen Warren Tested-by: Stephen Warren Tested-by: Thierry Reding Acked-by: Thierry Reding --- .../bindings/video/backlight/pwm-backlight.txt | 63 +++++++- drivers/video/backlight/Kconfig | 1 + drivers/video/backlight/pwm_bl.c | 160 ++++++++++++++++----- include/linux/pwm_backlight.h | 18 ++- 4 files changed, 202 insertions(+), 40 deletions(-) diff --git a/Documentation/devicetree/bindings/video/backlight/pwm-backlight.txt b/Documentation/devicetree/bindings/video/backlight/pwm-backlight.txt index 1e4fc72..b20e98e 100644 --- a/Documentation/devicetree/bindings/video/backlight/pwm-backlight.txt +++ b/Documentation/devicetree/bindings/video/backlight/pwm-backlight.txt @@ -13,16 +13,73 @@ Required properties: Optional properties: - pwm-names: a list of names for the PWM devices specified in the - "pwms" property (see PWM binding[0]) + "pwms" property (see PWM binding[0]). + - power-sequences: Power sequences (see Power sequences[1]) used to bring the + backlight on and off. If this property is present, then two power + sequences named "power-on" and "power-off" must be defined to control how + the backlight is to be powered on and off. These sequences must reference + the PWM specified in the pwms property by its name, and can also reference + other resources supported by the power sequences mechanism [0]: Documentation/devicetree/bindings/pwm/pwm.txt +[1]: Documentation/devicetree/bindings/power/power_seq.txt Example: backlight { compatible = "pwm-backlight"; - pwms = <&pwm 0 5000000>; - brightness-levels = <0 4 8 16 32 64 128 255>; default-brightness-level = <6>; + low-threshold-brightness = <50>; + + /* resources used by the power sequences */ + pwms = <&pwm 0 5000000>; + pwm-names = "backlight"; + power-supply = <&backlight_reg>; + + power-sequences { + power-on { + step0 { + type = "regulator"; + id = "power"; + enable; + }; + step1 { + type = "delay"; + delay = <10000>; + }; + step2 { + type = "pwm"; + id = "backlight"; + enable; + }; + step3 { + type = "gpio"; + gpio = <&gpio 28 0>; + value = <1>; + }; + }; + + power-off { + step0 { + type = "gpio"; + gpio = <&gpio 28 0>; + value = <0>; + }; + step1 { + type = "pwm"; + id = "backlight"; + disable; + }; + step2 { + type = "delay"; + delay = <10000>; + }; + step3 { + type = "regulator"; + id = "power"; + disable; + }; + }; + }; }; diff --git a/drivers/video/backlight/Kconfig b/drivers/video/backlight/Kconfig index 765a945..a6b0640 100644 --- a/drivers/video/backlight/Kconfig +++ b/drivers/video/backlight/Kconfig @@ -240,6 +240,7 @@ config BACKLIGHT_CARILLO_RANCH config BACKLIGHT_PWM tristate "Generic PWM based Backlight Driver" depends on PWM + select POWER_SEQ help If you have a LCD backlight adjustable by PWM, say Y to enable this driver. diff --git a/drivers/video/backlight/pwm_bl.c b/drivers/video/backlight/pwm_bl.c index 069983c..cfc0780 100644 --- a/drivers/video/backlight/pwm_bl.c +++ b/drivers/video/backlight/pwm_bl.c @@ -27,6 +27,12 @@ struct pwm_bl_data { unsigned int period; unsigned int lth_brightness; unsigned int *levels; + bool enabled; + struct power_seq_set power_seqs; + struct power_seq *power_on_seq; + struct power_seq *power_off_seq; + + /* Legacy callbacks */ int (*notify)(struct device *, int brightness); void (*notify_after)(struct device *, @@ -35,6 +41,51 @@ struct pwm_bl_data { void (*exit)(struct device *); }; +static void pwm_backlight_on(struct backlight_device *bl) +{ + struct pwm_bl_data *pb = dev_get_drvdata(&bl->dev); + int ret; + + if (pb->enabled) + return; + + if (pb->power_on_seq) { + ret = power_seq_run(pb->power_on_seq); + if (ret < 0) { + dev_err(&bl->dev, "cannot run power on sequence\n"); + return; + } + } else { + /* legacy framework */ + pwm_enable(pb->pwm); + } + + pb->enabled = true; +} + +static void pwm_backlight_off(struct backlight_device *bl) +{ + struct pwm_bl_data *pb = dev_get_drvdata(&bl->dev); + int ret; + + if (!pb->enabled) + return; + + if (pb->power_off_seq) { + ret = power_seq_run(pb->power_off_seq); + if (ret < 0) { + dev_err(&bl->dev, "cannot run power off sequence\n"); + return; + } + } else { + /* legacy framework */ + pwm_config(pb->pwm, 0, pb->period); + pwm_disable(pb->pwm); + } + + pb->enabled = false; +} + static int pwm_backlight_update_status(struct backlight_device *bl) { struct pwm_bl_data *pb = dev_get_drvdata(&bl->dev); @@ -51,8 +102,7 @@ static int pwm_backlight_update_status(struct backlight_device *bl) brightness = pb->notify(pb->dev, brightness); if (brightness == 0) { - pwm_config(pb->pwm, 0, pb->period); - pwm_disable(pb->pwm); + pwm_backlight_off(bl); } else { int duty_cycle; @@ -66,7 +116,7 @@ static int pwm_backlight_update_status(struct backlight_device *bl) duty_cycle = pb->lth_brightness + (duty_cycle * (pb->period - pb->lth_brightness) / max); pwm_config(pb->pwm, duty_cycle, pb->period); - pwm_enable(pb->pwm); + pwm_backlight_on(bl); } if (pb->notify_after) @@ -145,11 +195,10 @@ static int pwm_backlight_parse_dt(struct device *dev, data->max_brightness--; } - /* - * TODO: Most users of this driver use a number of GPIOs to control - * backlight power. Support for specifying these needs to be - * added. - */ + /* read power sequences */ + data->power_seqs = devm_of_parse_power_seq_set(dev); + if (IS_ERR(data->power_seqs)) + return PTR_ERR(data->power_seqs); return 0; } @@ -172,6 +221,7 @@ static int pwm_backlight_probe(struct platform_device *pdev) { struct platform_pwm_backlight_data *data = pdev->dev.platform_data; struct platform_pwm_backlight_data defdata; + struct power_seq_resource *res; struct backlight_properties props; struct backlight_device *bl; struct pwm_bl_data *pb; @@ -180,7 +230,9 @@ static int pwm_backlight_probe(struct platform_device *pdev) if (!data) { ret = pwm_backlight_parse_dt(&pdev->dev, &defdata); - if (ret < 0) { + if (ret == -EPROBE_DEFER) { + return ret; + } else if (ret < 0) { dev_err(&pdev->dev, "failed to find platform data\n"); return ret; } @@ -201,6 +253,68 @@ static int pwm_backlight_probe(struct platform_device *pdev) goto err_alloc; } + if (data->power_seqs) { + /* use power sequences */ + struct power_seq_set *seqs = &pb->power_seqs; + + power_seq_set_init(seqs, &pdev->dev); + power_seq_set_add_sequences(seqs, data->power_seqs); + + /* Check that the required sequences are here */ + pb->power_on_seq = power_seq_lookup(seqs, "power-on"); + if (!pb->power_on_seq) { + dev_err(&pdev->dev, "missing power-on sequence\n"); + return -EINVAL; + } + pb->power_off_seq = power_seq_lookup(seqs, "power-off"); + if (!pb->power_off_seq) { + dev_err(&pdev->dev, "missing power-off sequence\n"); + return -EINVAL; + } + + /* we must have exactly one PWM resource for this driver */ + power_seq_for_each_resource(res, seqs) { + if (res->type != POWER_SEQ_PWM) + continue; + if (pb->pwm) { + dev_err(&pdev->dev, "more than one PWM used\n"); + return -EINVAL; + } + /* keep the pwm at hand */ + pb->pwm = res->pwm.pwm; + } + /* from here we should have a PWM */ + if (!pb->pwm) { + dev_err(&pdev->dev, "no PWM defined!\n"); + return -EINVAL; + } + } else { + /* use legacy interface */ + pb->pwm = devm_pwm_get(&pdev->dev, NULL); + if (IS_ERR(pb->pwm)) { + dev_err(&pdev->dev, + "unable to request PWM, trying legacy API\n"); + + pb->pwm = pwm_request(data->pwm_id, "pwm-backlight"); + if (IS_ERR(pb->pwm)) { + dev_err(&pdev->dev, + "unable to request legacy PWM\n"); + ret = PTR_ERR(pb->pwm); + goto err_alloc; + } + } + + dev_dbg(&pdev->dev, "got pwm for backlight\n"); + + /* + * The DT case will set the pwm_period_ns field to 0 and store + * the period, parsed from the DT, in the PWM device. For the + * non-DT case, set the period from platform data. + */ + if (data->pwm_period_ns > 0) + pwm_set_period(pb->pwm, data->pwm_period_ns); + } + if (data->levels) { max = data->levels[data->max_brightness]; pb->levels = data->levels; @@ -213,28 +327,6 @@ static int pwm_backlight_probe(struct platform_device *pdev) pb->exit = data->exit; pb->dev = &pdev->dev; - pb->pwm = devm_pwm_get(&pdev->dev, NULL); - if (IS_ERR(pb->pwm)) { - dev_err(&pdev->dev, "unable to request PWM, trying legacy API\n"); - - pb->pwm = pwm_request(data->pwm_id, "pwm-backlight"); - if (IS_ERR(pb->pwm)) { - dev_err(&pdev->dev, "unable to request legacy PWM\n"); - ret = PTR_ERR(pb->pwm); - goto err_alloc; - } - } - - dev_dbg(&pdev->dev, "got pwm for backlight\n"); - - /* - * The DT case will set the pwm_period_ns field to 0 and store the - * period, parsed from the DT, in the PWM device. For the non-DT case, - * set the period from platform data. - */ - if (data->pwm_period_ns > 0) - pwm_set_period(pb->pwm, data->pwm_period_ns); - pb->period = pwm_get_period(pb->pwm); pb->lth_brightness = data->lth_brightness * (pb->period / max); @@ -267,8 +359,7 @@ static int pwm_backlight_remove(struct platform_device *pdev) struct pwm_bl_data *pb = dev_get_drvdata(&bl->dev); backlight_device_unregister(bl); - pwm_config(pb->pwm, 0, pb->period); - pwm_disable(pb->pwm); + pwm_backlight_off(bl); if (pb->exit) pb->exit(&pdev->dev); return 0; @@ -282,8 +373,7 @@ static int pwm_backlight_suspend(struct device *dev) if (pb->notify) pb->notify(pb->dev, 0); - pwm_config(pb->pwm, 0, pb->period); - pwm_disable(pb->pwm); + pwm_backlight_off(bl); if (pb->notify_after) pb->notify_after(pb->dev, 0); return 0; diff --git a/include/linux/pwm_backlight.h b/include/linux/pwm_backlight.h index 56f4a86..0dcec1d 100644 --- a/include/linux/pwm_backlight.h +++ b/include/linux/pwm_backlight.h @@ -5,14 +5,28 @@ #define __LINUX_PWM_BACKLIGHT_H #include +#include struct platform_pwm_backlight_data { - int pwm_id; unsigned int max_brightness; unsigned int dft_brightness; unsigned int lth_brightness; - unsigned int pwm_period_ns; unsigned int *levels; + /* + * New interface using power sequences. Must include exactly + * two power sequences named 'power-on' and 'power-off'. If NULL, + * the legacy interface is used. + */ + struct platform_power_seq_set *power_seqs; + + /* + * Legacy interface - use power sequences instead! + * + * pwm_id and pwm_period_ns need only be specified + * if get_pwm(dev, NULL) would return NULL. + */ + int pwm_id; + unsigned int pwm_period_ns; int (*init)(struct device *dev); int (*notify)(struct device *dev, int brightness); void (*notify_after)(struct device *dev, int brightness);