Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp1579268ybb; Sat, 21 Mar 2020 01:16:46 -0700 (PDT) X-Google-Smtp-Source: ADFU+vtX70Epot8OmY0H4xAbXXZs/bjgAnhPuQPbYYbCFk02sipUsfhPMUDH5OvksA7Q2keySuL6 X-Received: by 2002:a05:6830:2014:: with SMTP id e20mr10310531otp.317.1584778606782; Sat, 21 Mar 2020 01:16:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1584778606; cv=none; d=google.com; s=arc-20160816; b=Pfxi1CcHbIk+3u73sVruNEonxUFQcw5U7jP1j+qoXdDJMudHucebQt9VS6//YSLAXn XVV6Rg4AmV//rFJ1kbYP9owpR0z7f1c+/sr775f7PpqMZ84wXeHOILxFXvWObl8iBdUc Zx1CeD+eAErFQkQeje6yiUq/dIo+OQeEqQztesFbITrmcFBpbW1FgYxalg4f8bvKN1gh N5xmKEZy7Up3kFkJ7Y00RN926XISRCAPjA2YEhvXZlUUwLAPpCibufpkZ3Hz/LhEMQRH OsF4BDb59c2XjprWrfvvDeUvFP4VJdSS9OrpA88hq5Z0Aqz2/5/yMKwrAWZpPiRekJ6C 1Yfg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:content-transfer-encoding :content-language:accept-language:in-reply-to:references:message-id :date:thread-index:thread-topic:subject:cc:to:from:ironport-sdr :dkim-signature; bh=in16KW7rDiqHwndJ9M6BMYYtXULLbfCRwFtjTImNHwA=; b=hqNrZL4a3RvncOrL1QR7aet1cPWSyjoACRLJfrF6XzyKhFQSyRIl6ij3HS5f+4f6eo N+Wj83amrTlokxQPTBAqfZlMxqxG8etcFkhmzkZSERKTkOJmUUr+HRepbG2j9Ng4sEmE pP30wjnVa5CMcOOVuAfAeKzf78r65kpkfx0hN07LQRmBQAqSm4dzJcEmMYfd5/+j3rO1 h/8gawHZvkvkUhuC+vv58dB17bQAr1b9L0DymuuK/Cmk4rqzBuagkbPJ39mfxKszKsbQ noHYMmsTYOzcRjgsx7GMONpXpfaLJDavXprympJjG3gA0wbWdPHLP8jteZo23i3qAlg6 g08g== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail (test mode) header.i=@diehl.com header.s=default header.b="OsLdVg/A"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id k11si4510404otb.252.2020.03.21.01.16.27; Sat, 21 Mar 2020 01:16:46 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=fail (test mode) header.i=@diehl.com header.s=default header.b="OsLdVg/A"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728239AbgCUIQF (ORCPT + 99 others); Sat, 21 Mar 2020 04:16:05 -0400 Received: from enterprise01.smtp.diehl.com ([193.201.238.219]:12449 "EHLO enterprise01.smtp.diehl.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727961AbgCUIQB (ORCPT ); Sat, 21 Mar 2020 04:16:01 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=diehl.com; i=@diehl.com; q=dns/txt; s=default; t=1584778560; x=1616314560; h=from:to:cc:subject:date:message-id:references: in-reply-to:content-transfer-encoding:mime-version; bh=Z2uB7z28QOCO6ZHYTC0YmbiAxXblaUq/Ne32486EdG0=; b=OsLdVg/AKLVzAZTKxy9FChMNhTCd6BW4ie0+RQM2n2uZcvuoZcGyp7sX 6rGA1xwpse4kH+FaJVNph0VX/UdzxpuBhij+4H33lu3Isb6yfGrbNepj7 iDT8PfUBtrWzYRlT1N9Zx+9lrTSyfGf0NfVg7MP1u9Yf78gX8RVJ0juxw RENDQenZr06Z/W2f7AZF9HEhPjr+aGdGM6VKwrHUdonetvoV/IBbE58FX IprxzC/hDXJRQACj30lzlkE0ktjBuzfoexIF8QksaJojI9xiai0uILCTv /9srQyBSOE3v3jYOUq2SFeBkUwq0C4uGCpyq52jOVvTcGHcjz54t1HfQA g==; IronPort-SDR: JGlCynbsOKWT0iqKYADU4jDJnWhu18yo24qSseS7HqPzLPQTZZLjAibyoBXUf5MD+vAe4wIU5X AwX/fhac/Wkw== From: Denis Osterland-Heim To: "dmurphy@ti.com" , "pavel@ucw.cz" , "mark.rutland@arm.com" , "jacek.anaszewski@gmail.com" , "robh+dt@kernel.org" CC: "linux-kernel@vger.kernel.org" , "Denis Osterland-Heim" , "linux-leds@vger.kernel.org" , "devicetree@vger.kernel.org" Subject: [PATCH v4 4/5] leds: pwm: add support for default-state device property Thread-Topic: [PATCH v4 4/5] leds: pwm: add support for default-state device property Thread-Index: AQHV/1jxcfu5++mTukas+V/Yy7sQpw== Date: Sat, 21 Mar 2020 08:15:55 +0000 Message-ID: <20200321081321.15614-5-Denis.Osterland@diehl.com> References: <20200321081321.15614-1-Denis.Osterland@diehl.com> In-Reply-To: <20200321081321.15614-1-Denis.Osterland@diehl.com> Accept-Language: de-DE, en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-mailer: Evolution 3.28.5-0ubuntu0.18.04.1 x-ms-exchange-messagesentrepresentingtype: 1 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-TrailerSkip: 1 X-GBS-PROC: 415AWy8o668fDtCxszluz3Qkyqh7rcy46kDTEvbxZbU7iORrjmTYQnM0XIpmHs+x Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This patch adds support for =22default-state=22 devicetree property, whic= h allows to defer pwm init to first use of led. This allows to configure the PWM early in bootloader to let the LED blink until an application in Linux userspace sets something different. Signed-off-by: Denis Osterland-Heim --- drivers/leds/leds-pwm.c | 54 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 48 insertions(+), 6 deletions(-) diff --git a/drivers/leds/leds-pwm.c b/drivers/leds/leds-pwm.c index 07eab2d8b7c7..73d36d5fb427 100644 --- a/drivers/leds/leds-pwm.c +++ b/drivers/leds/leds-pwm.c @@ -18,10 +18,15 @@ #include #include =20 +#define LEDS_PWM_DEFSTATE_OFF 0 +#define LEDS_PWM_DEFSTATE_ON 1 +#define LEDS_PWM_DEFSTATE_KEEP 2 + struct led_pwm { const char *name; const char *default_trigger; u8 active_low; + u8 default_state; unsigned int max_brightness; }; =20 @@ -88,7 +93,30 @@ static int led_pwm_add(struct device *dev, struct led_= pwm_priv *priv, =20 led_data->cdev.brightness_set_blocking =3D led_pwm_set; =20 - pwm_init_state(led_data->pwm, &led_data->pwmstate); + /* init PWM state */ + if (led->default_state =3D=3D LEDS_PWM_DEFSTATE_KEEP) { + pwm_get_state(led_data->pwm, &led_data->pwmstate); + if (!led_data->pwmstate.period) { + led->default_state =3D LEDS_PWM_DEFSTATE_OFF; + dev_warn(dev, + =22failed to read period for %s, default to off=22, + led->name); + } + } + if (led->default_state !=3D LEDS_PWM_DEFSTATE_KEEP) + pwm_init_state(led_data->pwm, &led_data->pwmstate); + + /* set brightness */ + if (led->default_state =3D=3D LEDS_PWM_DEFSTATE_ON) + led_data->cdev.brightness =3D led->max_brightness; + else if (led->default_state =3D=3D LEDS_PWM_DEFSTATE_KEEP) { + uint64_t brightness; + + brightness =3D led->max_brightness; + brightness *=3D led_data->pwmstate.duty_cycle; + do_div(brightness, led_data->pwmstate.period); + led_data->cdev.brightness =3D brightness; + } =20 ret =3D devm_led_classdev_register(dev, &led_data->cdev); if (ret) { @@ -97,11 +125,13 @@ static int led_pwm_add(struct device *dev, struct le= d_pwm_priv *priv, return ret; } =20 - ret =3D led_pwm_set(&led_data->cdev, led_data->cdev.brightness); - if (ret) { - dev_err(dev, =22failed to set led PWM value for %s: %d=22, - led->name, ret); - return ret; + if (led->default_state !=3D LEDS_PWM_DEFSTATE_KEEP) { + ret =3D led_pwm_set(&led_data->cdev, led_data->cdev.brightness); + if (ret) { + dev_err(dev, =22failed to set led PWM value for %s: %d=22, + led->name, ret); + return ret; + } } =20 priv->num_leds++; @@ -117,6 +147,8 @@ static int led_pwm_create_fwnode(struct device *dev, = struct led_pwm_priv *priv) memset(&led, 0, sizeof(led)); =20 device_for_each_child_node(dev, fwnode) { + const char *state =3D NULL; + ret =3D fwnode_property_read_string(fwnode, =22label=22, &led.name); if (ret && is_of_node(fwnode)) led.name =3D to_of_node(fwnode)->name; @@ -134,6 +166,16 @@ static int led_pwm_create_fwnode(struct device *dev,= struct led_pwm_priv *priv) fwnode_property_read_u32(fwnode, =22max-brightness=22, &led.max_brightness); =20 + if (!fwnode_property_read_string(fwnode, =22default-state=22, + &state)) { + if (!strcmp(state, =22keep=22)) + led.default_state =3D LEDS_PWM_DEFSTATE_KEEP; + else if (!strcmp(state, =22on=22)) + led.default_state =3D LEDS_PWM_DEFSTATE_ON; + else + led.default_state =3D LEDS_PWM_DEFSTATE_OFF; + } + ret =3D led_pwm_add(dev, priv, &led, fwnode); if (ret) { fwnode_handle_put(fwnode); --=20 2.25.1 Diehl Connectivity Solutions GmbH Gesch=E4ftsf=FChrung: Horst Leonberger Sitz der Gesellschaft: N=FCrnberg - Registergericht: Amtsgericht N=FCrnberg: HRB 32315 _________________________________________________________________________= __________________________ Der Inhalt der vorstehenden E-Mail ist nicht rechtlich bindend. Diese E-M= ail enthaelt vertrauliche und/oder rechtlich geschuetzte Informationen. Informieren Sie uns bitte, wenn Sie diese E-Mail faelschlicherweise erhal= ten haben. Bitte loeschen Sie in diesem Fall die Nachricht. Jede unerlaubte Form der Reproduktion, Bekanntgabe, Aenderung, Verteilung= und/oder Publikation dieser E-Mail ist strengstens untersagt. - Informationen zum Datenschutz, insbesondere zu Ihren Rechten, erhalten = Sie unter https://www.diehl.com/group/de/transparenz-und-informationspfli= chten/ The contents of the above mentioned e-mail is not legally binding. This e= -mail contains confidential and/or legally protected information. Please = inform us if you have received this e-mail by mistake and delete it in such a case. Each unauthorized reproduction, dis= closure, alteration, distribution and/or publication of this e-mail is st= rictly prohibited.=20 - For general information on data protection and your respective rights p= lease visit https://www.diehl.com/group/en/transparency-and-information-o= bligations/