Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp3855012pxj; Mon, 7 Jun 2021 23:39:42 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyutKNrcCV6fU4UD1taKXzAK2of2e0Q4TR4SIbfKltqO1X4WePW1JKBmIxVKo2rP1B4BUfr X-Received: by 2002:a17:907:2709:: with SMTP id w9mr21810743ejk.118.1623134382776; Mon, 07 Jun 2021 23:39:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1623134382; cv=none; d=google.com; s=arc-20160816; b=hMIeRjHSghy65M8JdMxvTynPgEkAuXeRARnpr5avwpRKDfBg6mnBIAttHgJp9T3cCn aXorkLiE3aTs0Jx0fLq8fvba82nHAFuQdn4vN+VMlVlHfM3MU2OuhxVPl9h+f4j1YsIj 6AAyX69a3/IjS+QNr2w30w1rRHw9YBGxrxUMbLVemqqc2+5TybnE2jGj7XStngaAsw3D fqQyxTnpnEOgGtTujJ6hVGmm55sr/oWjqixdXTpz21+H1N1B4rSKlNXhojahOI27SkJm HTRn/BWV5jOQwYwziIJAo913yTm+D6+DCiBrxyaAO97WBFDj5YHuMeQSOw3CuQ3ai/Hj +X/Q== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=R4clZ8ZJRTuTc/QrguH4w64auSZ4LLlsG595KU1MPBM=; b=cP5uT0zYLBgF+7IwUdqrw9tZIplB5mCErsAcV7SbJAp+RuQHhnI84GGfqyYdYVFNrI yn4M7N+c2RQbJdYUQFZGI85Djo/OFTLACIPKyvnNQsdz2iyGzBmGVHuLzZOomt94NFWS /zb6wBQYkXRMIPX4uIrUyAlMHOll44CUi/VvR3pqdDEqU9Mzl9MqgAGf52PMDvoaQQHa hjEfuJEZgGnO/DjOxDsC/BKYlRHn9kSOdhKVldUlcbDaqM2IiHJUnO9kikGY4nf1wWEc GcW+WiFMuHmQPaZjLjS9sOiuKJ1xYCD+C/FmUJxL5fpSc23vOOP9+QcNYfXz3H7mrlD8 uKUQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=gdFEISbh; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id t16si17714710edi.147.2021.06.07.23.39.18; Mon, 07 Jun 2021 23:39:42 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=gdFEISbh; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230190AbhFHGjS (ORCPT + 99 others); Tue, 8 Jun 2021 02:39:18 -0400 Received: from mail-ej1-f48.google.com ([209.85.218.48]:42878 "EHLO mail-ej1-f48.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229507AbhFHGjQ (ORCPT ); Tue, 8 Jun 2021 02:39:16 -0400 Received: by mail-ej1-f48.google.com with SMTP id k25so25231325eja.9; Mon, 07 Jun 2021 23:37:12 -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=R4clZ8ZJRTuTc/QrguH4w64auSZ4LLlsG595KU1MPBM=; b=gdFEISbh5wEAU9s+anA5iwBXogIhTX4ZjmljXpjfOvRt0if2VxgUvGo1a8U9UIvVsd ahPBIJ7mK4CyJluB04DzVIabcOg7GKBJERR7Z0UrfE8ytwUN8uEJ4FVQXSXjKqR2KnmO LbIb4vrloLIoBAzDWAxeTe/bZIzTqVmLLwCmtj+wclXko3z/yg0RIRAC0FaNAFovRPXo cr2o0fQd5LojOm1Znk9VcP+YLHPFSxEVMCYH3Nbney8ljmNKHJ+8hn7tYErnLEZ3LcRy RuDVvzmsiwwXI8cwu3GnoA13xjy79eHWGNPMc1/6hDuX94kzACTYCo6rp2Hv0ZPYcciI iWfg== 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=R4clZ8ZJRTuTc/QrguH4w64auSZ4LLlsG595KU1MPBM=; b=EGnL16fb5A9yB2c6hi5MkRa9ggh9M+rHhmeHmO1MsclGimufqrTwH8/zn7Ov/bfG6g 1THKfEPX5h0hTERge+Lht7orWy2wIHb3t2RBl5kDbONNak6DyZ/8tiXU5yhse17aazh1 Y5Ad510bhggH52vzZGvUcHzzphQ/BLepSIPuXHjIIsYNV1BF9GAGmT+eyfjqxlpa+LkI yENhqtbJb8XjoZJ+99VGokPlzsgzg4XimcguvBdM4hNl197fMgasm+Zx2TPFOmlhf/Bf Cr8vGi6oFB3fVkHHbN+nB15ft1nP30G/O8GziWo7/hOcrRn2SyLUtAPL9tHfGxyckjhv eQOA== X-Gm-Message-State: AOAM531QAZ9RY0kaEkTp+TSzvLuLb1chXCE5U6Nb6TBR2mUHifkCp8mC oybf0bu3TQna7T6qNU3zRUc= X-Received: by 2002:a17:907:f9b:: with SMTP id kb27mr11592971ejc.44.1623134171806; Mon, 07 Jun 2021 23:36:11 -0700 (PDT) Received: from fujitsu.fritz.box (p200300da6709910000b233666d22ecd9.dip0.t-ipconnect.de. [2003:da:6709:9100:b2:3366:6d22:ecd9]) by smtp.gmail.com with ESMTPSA id ot30sm6698168ejb.61.2021.06.07.23.36.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Jun 2021 23:36:11 -0700 (PDT) From: Denis Osterland-Heim To: Pavel Machek Cc: denis.osterland@diehl.com, linux-leds@vger.kernel.org, linux-kernel@vger.kernel.org, Denis Osterland-Heim Subject: [PATCH v8 2/2] leds: pwm: add support for default-state device property Date: Tue, 8 Jun 2021 08:35:54 +0200 Message-Id: <20210608063554.6590-3-denis.osterland.heim@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210608063554.6590-1-denis.osterland.heim@gmail.com> References: <20210608063554.6590-1-denis.osterland.heim@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Denis Osterland-Heim This patch adds support for "default-state" devicetree property, which 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 | 49 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 43 insertions(+), 6 deletions(-) diff --git a/drivers/leds/leds-pwm.c b/drivers/leds/leds-pwm.c index d71e9fa5c8de..6832180c1c54 100644 --- a/drivers/leds/leds-pwm.c +++ b/drivers/leds/leds-pwm.c @@ -17,10 +17,12 @@ #include #include #include +#include "leds.h" struct led_pwm { const char *name; u8 active_low; + u8 default_state; unsigned int max_brightness; }; @@ -77,7 +79,38 @@ static int led_pwm_add(struct device *dev, struct led_pwm_priv *priv, led_data->cdev.brightness_set_blocking = led_pwm_set; - pwm_init_state(led_data->pwm, &led_data->pwmstate); + /* init PWM state */ + switch (led->default_state) { + case LEDS_DEFSTATE_KEEP: + pwm_get_state(led_data->pwm, &led_data->pwmstate); + if (led_data->pwmstate.period) + break; + led->default_state = LEDS_DEFSTATE_OFF; + dev_warn(dev, + "failed to read period for %s, default to off", + led->name); + fallthrough; + default: + pwm_init_state(led_data->pwm, &led_data->pwmstate); + break; + } + + /* set brightness */ + switch (led->default_state) { + case LEDS_DEFSTATE_ON: + led_data->cdev.brightness = led->max_brightness; + break; + case LEDS_DEFSTATE_KEEP: + { + uint64_t brightness; + + brightness = led->max_brightness; + brightness *= led_data->pwmstate.duty_cycle; + do_div(brightness, led_data->pwmstate.period); + led_data->cdev.brightness = brightness; + } + break; + } ret = devm_led_classdev_register_ext(dev, &led_data->cdev, &init_data); if (ret) { @@ -86,11 +119,13 @@ static int led_pwm_add(struct device *dev, struct led_pwm_priv *priv, return ret; } - ret = led_pwm_set(&led_data->cdev, led_data->cdev.brightness); - if (ret) { - dev_err(dev, "failed to set led PWM value for %s: %d", - led->name, ret); - return ret; + if (led->default_state != LEDS_DEFSTATE_KEEP) { + ret = led_pwm_set(&led_data->cdev, led_data->cdev.brightness); + if (ret) { + dev_err(dev, "failed to set led PWM value for %s: %d", + led->name, ret); + return ret; + } } priv->num_leds++; @@ -120,6 +155,8 @@ static int led_pwm_create_fwnode(struct device *dev, struct led_pwm_priv *priv) fwnode_property_read_u32(fwnode, "max-brightness", &led.max_brightness); + led.default_state = led_init_default_state_get(fwnode); + ret = led_pwm_add(dev, priv, &led, fwnode); if (ret) goto err_child_out; -- 2.25.1