Received: by 2002:a5b:505:0:0:0:0:0 with SMTP id o5csp5878507ybp; Tue, 8 Oct 2019 09:34:32 -0700 (PDT) X-Google-Smtp-Source: APXvYqz06Xzq9TzbUAegRqI3LFAwaP0T2UxqWSp/kkYsEROhKI1d0ZUzU1Hh347nSjbOt6yYaZsv X-Received: by 2002:a17:906:4a17:: with SMTP id w23mr21033894eju.324.1570552472312; Tue, 08 Oct 2019 09:34:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1570552472; cv=none; d=google.com; s=arc-20160816; b=B0vQ+lNXPQOTa9fPH01yd4fLCnZjeX//okkQtesKs/cuZX3gXIiA7mqPeUYESzXEDg YtFPU2rtGV/5ZK8Sqb66yecytdOj99IU+j3L/JUsjkT6pQXkvbofWZsZStDqLQi+KgzB CgeDV586lRtpuBQhZtZ///FwYmpy4RL16qIvz2D6oKD4/UDuplu7Pytfy4gNYAX/coaT +CFK5SjbHhEI+4eTvCnue5nssWcQaKW6MSQSQI0TjqicGwm2LiM9fU8xtfRjqVvvl+DP yjVVqVU+PHu62427dF912hF3svfAXvnazct2Z+40RjP/DXmnqzzuokBxw4n6ItUIbzKN jNWw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject; bh=no87Ib9bGmVEnv0RsAK0xMGeVOx0GK2bVc2tvYRPVQs=; b=HLU9sS8zRwYJ4kSf576/gsP+AgwmRCp3+MzhPX1qts5/Eq7KIvu18/WjhO4szbgPXg u1o6C9ErSKkfjbsbMd6c7tA8PglUJ9Qz/aPkzudbNMjQbfdGaFCq99TuyRsKWei+c5bD UqDuex53CnngF+MRIASEUEJ6AsJPkFrUJoVBiyFLCmTmmqoB9Kw8JA3Z9xsE575kAUGD wTjkHSl+dz/9LFX7EuPTabY+bvmM7zBY9puTH5m1U+qQ5MbABKQpyprwLPCGdJ7jmMoF 58TUsHyVLHQnoUhyys4OVOUP2RG+3SWiDJ8qpC0ri1VOWXrePWO4+9IKxagU2e4em+Xi 6s/A== ARC-Authentication-Results: i=1; mx.google.com; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=collabora.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id h19si9502787ejd.142.2019.10.08.09.34.08; Tue, 08 Oct 2019 09:34:32 -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; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=collabora.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728929AbfJHQdW (ORCPT + 99 others); Tue, 8 Oct 2019 12:33:22 -0400 Received: from bhuna.collabora.co.uk ([46.235.227.227]:48474 "EHLO bhuna.collabora.co.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728561AbfJHQdV (ORCPT ); Tue, 8 Oct 2019 12:33:21 -0400 Received: from [127.0.0.1] (localhost [127.0.0.1]) (Authenticated sender: eballetbo) with ESMTPSA id C352228FC0D Subject: Re: [PATCH] pwm: cros-ec: Let cros_ec_pwm_get_state() return the last applied state To: =?UTF-8?Q?Uwe_Kleine-K=c3=b6nig?= Cc: linux-kernel@vger.kernel.org, thierry.reding@gmail.com, heiko@sntech.de, dianders@chromium.org, mka@chromium.org, groeck@chromium.org, kernel@collabora.com, bleung@chromium.org, linux-pwm@vger.kernel.org References: <20191008105417.16132-1-enric.balletbo@collabora.com> <20191008143432.pbhcqamd6f4qwbqn@pengutronix.de> From: Enric Balletbo i Serra Message-ID: <4f009344-242e-19a7-6872-2c55df086044@collabora.com> Date: Tue, 8 Oct 2019 18:33:15 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.9.0 MIME-Version: 1.0 In-Reply-To: <20191008143432.pbhcqamd6f4qwbqn@pengutronix.de> Content-Type: text/plain; charset=utf-8 Content-Language: en-GB Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Uwe, Thanks for the quick reply. On 8/10/19 16:34, Uwe Kleine-König wrote: > Hello Enric, > > On Tue, Oct 08, 2019 at 12:54:17PM +0200, Enric Balletbo i Serra wrote: >> @@ -117,17 +122,28 @@ static void cros_ec_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm, >> struct cros_ec_pwm_device *ec_pwm = pwm_to_cros_ec_pwm(chip); >> int ret; >> >> - ret = cros_ec_pwm_get_duty(ec_pwm->ec, pwm->hwpwm); >> - if (ret < 0) { >> - dev_err(chip->dev, "error getting initial duty: %d\n", ret); >> - return; >> + /* >> + * As there is no way for this hardware to separate the concept of >> + * duty cycle and enabled, but the PWM API does, let return the last >> + * applied state when the PWM is disabled and only return the real >> + * hardware value when the PWM is enabled. Otherwise, a user of this >> + * driver, can get confused because won't be able to program a duty >> + * cycle while the PWM is disabled. >> + */ >> + state->enabled = ec_pwm->state.enabled; > >> + if (state->enabled) { > > As part of registration of the pwm .get_state is called. In this case > .apply wasn't called before and so state->enabled is probably 0. So this > breaks reporting the initial state ... > >> + ret = cros_ec_pwm_get_duty(ec_pwm->ec, pwm->hwpwm); >> + if (ret < 0) { >> + dev_err(chip->dev, "error getting initial duty: %d\n", >> + ret); >> + return; >> + } >> + state->duty_cycle = ret; >> + } else { >> + state->duty_cycle = ec_pwm->state.duty_cycle; >> } >> >> - state->enabled = (ret > 0); >> state->period = EC_PWM_MAX_DUTY; >> - >> - /* Note that "disabled" and "duty cycle == 0" are treated the same */ >> - state->duty_cycle = ret; > > A few thoughts to your approach here ...: > > - Would it make sense to only store duty_cycle and enabled in the > driver struct? > Yes, in fact, my first approach (that I didn't send) was only storing enabled and duty cycle. For some reason I ended storing the full pwm_state struct, but I guess is not really needed. > - Which driver is the consumer of your pwm? If I understand correctly > the following sequence is the bad one: > The consumer is the pwm_bl driver. Actually I'n trying to identify other consumers. > state.period = P; > state.duty_cycle = D; > state.enabled = 0; > pwm_apply_state(pwm, &state); > > ... > > pwm_get_state(pwm, &state); > state.enabled = 1; > pwm_apply_state(pwm, &state); > Yes that's the sequence. > Before my patch there was an implicit promise in the PWM framework > that the last pwm_apply_state has .duty_cycle = D (and .period = P). > Is this worthwile, or should we instead declare this as > non-guaranteed and fix the caller? > pwm_bl is compliant with this, the problem in the pwm-cros-ec driver is when you set the duty_cycle but enable is 0. > - If this is a more or less common property that hardware doesn't know > the concept of "disabled" maybe it would make sense to drop this from > the PWM framework, too. (This is a question that I discussed some > time ago already with Thierry, but without an result. The key > question is: What is the difference between "disabled" and > "duty_cycle = 0" in general and does any consumer care about it.) > Good question, I don't really know all consumer requirements, but AFAIK, usually when you want to program duty_cycle to 0 you also want to disable the PWM. At least for the backlight case doesn't make sense program first the duty_cycle and then enable the PWM, is implicit, if duty_cycle is 0 the PWM is disabled, if duty_cycle > 0 the PWM is enabled. > - A softer variant of the above: Should pwm_get_state() anticipate that > with .enabled = 0 the duty_cycle (and maybe also period) is > unreliable and cache that for callers? > Sorry, when you say pwm_get_state(), you mean the core call or the lowlevel driver call? > Unrelated to the patch in question I noticed that the cros-ec-pwm driver > doesn't handle polarity. We need > > state->polarity = PWM_POLARITY_NORMAL; > > in cros_ec_pwm_get_state() and > > if (state->polarity != PWM_POLARITY_NORMAL) > return -ERANGE; > > in cros_ec_pwm_apply(). (Not sure -ERANGE is the right value, I think > there is no global rule in force that tells the right value though.) > Nice catch, thanks, I'll send a patch to fix this. Thanks, Enric > Best regards > Uwe >