Received: by 2002:ab2:784b:0:b0:1fd:adc2:8405 with SMTP id m11csp272632lqp; Mon, 10 Jun 2024 03:54:26 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXDcgsjOQRVbkieTFftUAWYPkRw60XYRPcfBbATsCBScjaKdX3n27LXumwGik98VDnMFcw0x7s+jB6g0CssPRjePS5bsoaJf3ICCvG+8w== X-Google-Smtp-Source: AGHT+IFPvZ+SHecpHHNuIO/tw3zznjWsksBEK3WGyMMS+ghStizgWUqTLPFXpStL0qB2UZRKqgHE X-Received: by 2002:ac8:5ac5:0:b0:440:9078:f438 with SMTP id d75a77b69052e-4409078f513mr32986751cf.49.1718016866166; Mon, 10 Jun 2024 03:54:26 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1718016866; cv=pass; d=google.com; s=arc-20160816; b=TAN0f7sSVO6jZMWm5tzUQt/mr9TTbMDpNX9lk5kq5Q/zyjSL6BAk/a10K24HdyUfNc +pDuWCoO/U5VLFig2tnJiyysQ5dprUALpiDUG13d8BHVyM6/q+1642oLd2/Ew/JK7JJd dnA3Y9AtJx4pRCluoDbhmJ4Ux2ajU5gC4wthzqfWqejrM9HQqc5FqB3SkjmzTOe5uQYe I7DcpoYd544Ghn8pt1RYGvPQ9hN+ZHzpBmZSmdoyFXiUKtlhKopyPW7N3y28TL0GgkOO axcjtQbG+DIcLFLNq3DLaHTfOFz9qt0PskpxNyiJBmTO8NLy2Z1emWEv+mOgTil8hJBp Lq+A== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from:dkim-signature; bh=/XlZMzh78GMHOD1gDwKx1xR6KaRV34mJ3eqV2qIma2k=; fh=zDdmKu7ZhIyquo2Wsd8r0Ee7ihxnXHIqkCXwI/9Ulx8=; b=qbqaJEks8n4CcdphU//0jBOjYqni7J7bPpyqqXf5wisyI9lWixMSe0CkWTo3EodF6J Hz21O6ZHIz1RfT9HKYDwLSYTXBMFFswBwSOZFhYIYCVoCuiSK4npIKsDZPlWgPeNNEC7 WODBv8EACO2tQVXLrpFk0lBzeOc4EYtG735wMQzXWcMh1o5AK2mEZgO41C1DQSVPWdXA Iej5JDCJAYvXawA3os78VKdH51nlSuaXaRc7FX3/ccNJCEBKR/vWh/Pzl+55SBZYdYP7 zz0Y2UKEnWiIFu4kdi0BGV4GtITL2wTAwgP93nDftE7BL805CS/Gb4S0IT+pGOD0V41R 533g==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@baylibre-com.20230601.gappssmtp.com header.s=20230601 header.b=rjhO9hqx; arc=pass (i=1 spf=pass spfdomain=baylibre.com dkim=pass dkdomain=baylibre-com.20230601.gappssmtp.com); spf=pass (google.com: domain of linux-kernel+bounces-208050-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-208050-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id d75a77b69052e-440f83f0549si17914491cf.655.2024.06.10.03.54.25 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Jun 2024 03:54:26 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-208050-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@baylibre-com.20230601.gappssmtp.com header.s=20230601 header.b=rjhO9hqx; arc=pass (i=1 spf=pass spfdomain=baylibre.com dkim=pass dkdomain=baylibre-com.20230601.gappssmtp.com); spf=pass (google.com: domain of linux-kernel+bounces-208050-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-208050-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id CC06C1C217F6 for ; Mon, 10 Jun 2024 10:54:25 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8140414F9E5; Mon, 10 Jun 2024 10:46:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b="rjhO9hqx" Received: from mail-ej1-f53.google.com (mail-ej1-f53.google.com [209.85.218.53]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 99BF214F9D2 for ; Mon, 10 Jun 2024 10:46:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.53 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718016383; cv=none; b=HaW1MJ6qgb9nRpp5ardyVVJnl9iCIKUA1C+e6dvDT4SNK+Qdkjjid0x6GmCCuKEQ+HDFkFgpx5WWmEzHyM5+ZOyLVuvwmkiYpEtB3Osoh28myDGnoeEex84FEkffm5sd4pqQ+X/F2RCmHxc1YtBJhNvEcWrNJ1S69M0OyDKWofY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718016383; c=relaxed/simple; bh=74n1Zibr4fPkE6scCimdQ1VXXNPZn1aItHzIJ17piqM=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version:Content-Type; b=nJ9sUrfhsic5y2XXMHraf0Dm0Lxher6vCVHlBYWci48Sc0cuodTjD7CmU5uj09FRfp2pFc6jXNh/hvy0O2xfyY4g8fl/1BHPrRNnDECXagMjk/IPOLOVMaDP35/saKnTcDcXO9Ot09OEDVoAVxC6oA3fakp6RwtXOwzI9oJENac= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com; spf=pass smtp.mailfrom=baylibre.com; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b=rjhO9hqx; arc=none smtp.client-ip=209.85.218.53 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=baylibre.com Received: by mail-ej1-f53.google.com with SMTP id a640c23a62f3a-a68b41ef3f6so465541866b.1 for ; Mon, 10 Jun 2024 03:46:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1718016379; x=1718621179; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=/XlZMzh78GMHOD1gDwKx1xR6KaRV34mJ3eqV2qIma2k=; b=rjhO9hqx4OZdYQcKcV1KqUJwCtM2krBGypULAQ/QepJLk2fiUNRQN1JMpBwxMhZDGW 5btqBWhEFNshhUUEwXQqxxaRXq49cT4Me0U++bKIdj85CaQ7V9jw+u0PyGmnDkR73bqr DqdM60dClWqf8c+R3xg2muKfbIUqj6se3+wlhqAoRSx1iquN22Xbj243Q1/+tDH+IFCC uMfNVBSaldV4HbLwPTpH55PtnBbXWGy6duYXfwZiFSxCPmo5LEyLZmf+fbiat0bU37l7 JtEY2M0Usw1L4ZiT/571eOatcf02E+vZD9PCpnVyagsIdmZZBgIWfPsTfdvGQEZboBcP dGxQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718016379; x=1718621179; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=/XlZMzh78GMHOD1gDwKx1xR6KaRV34mJ3eqV2qIma2k=; b=XvX44/oSlh7BU5Exkk6BkbPn8q7e1gB7Ed4L0SlJC0RYglcSIN4pjZLzLMcoufS4kw EZOefivwLi8ExR1DtwGNbMz8sC3Q8n0kDFaAHSb/0GVaaBPaSXPBA4iUTNbwtPKIXwho Mr69bYeldPZEHWQOCpHcOoLJwdzv3/iayUftCMSVYGXHOavuF5lLIWjo5TEdss/6v5Xg TXgEaOhaDnlWUNIUtA980c8yC531KqSXUrFi9DNSzul9lCSx8Id+mEU44R8ibh3Gsa+c gxb8tXwXpSGQ7GsV4X1+6ra4UFIxs7SZ9p0R7VZVCbiyocASwj1aJKRA8RHJ5R8wIZhC Zdwg== X-Gm-Message-State: AOJu0YxOKEyyO3k59QcaJUlMFRImXkG48MMVNEhxrezXBoCEHSRYIHNU Erpx3v3oMGIYA3asnBICATzEahGlp1icOM2KcWykueT6BurGPMztWKJ1NTzIAgI= X-Received: by 2002:a17:906:eea:b0:a62:fc9d:6fec with SMTP id a640c23a62f3a-a6cd74bf905mr556338366b.34.1718016378782; Mon, 10 Jun 2024 03:46:18 -0700 (PDT) Received: from localhost (p509153eb.dip0.t-ipconnect.de. [80.145.83.235]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a6f08204ba1sm309996566b.105.2024.06.10.03.46.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Jun 2024 03:46:18 -0700 (PDT) From: =?UTF-8?q?Uwe=20Kleine-K=C3=B6nig?= To: Liam Girdwood , Mark Brown , Martin Blumenstingl Cc: linux-kernel@vger.kernel.org, linux-pwm@vger.kernel.org Subject: [RFC PATCH] regulator: pwm-regulator: Make assumptions about disabled PWM consistent Date: Mon, 10 Jun 2024 12:46:00 +0200 Message-ID: <20240610104600.458308-2-u.kleine-koenig@baylibre.com> X-Mailer: git-send-email 2.43.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Developer-Signature: v=1; a=openpgp-sha256; l=5030; i=u.kleine-koenig@baylibre.com; h=from:subject; bh=74n1Zibr4fPkE6scCimdQ1VXXNPZn1aItHzIJ17piqM=; b=owEBbQGS/pANAwAKAY+A+1h9Ev5OAcsmYgBmZtloVvG9ndchYWd7fvEXizTIhME1/yc0Ep8Ys E5Ns8dEVw+JATMEAAEKAB0WIQQ/gaxpOnoeWYmt/tOPgPtYfRL+TgUCZmbZaAAKCRCPgPtYfRL+ TnT0B/9KAwtSY/bi7poMsPG7y3w5zMqCMPkH+Upgu/g8HQRJZkTlNiwNrnGAko16TU9vkorDMzj Qt5J/psei9zapGrbxi/IUCwQssVNuT9sZEIy5x16+ADhJCW7mvZmgFk4WyqvfAd8lhc6BSfUa30 kJKjUJHpyWL6KS1zgWTKv2+LwJ3pElylE14w5V2SP03lRmpBAcJglCD11mXWL5MiUWVJzuh+KIZ EEg6XTnb3j9jdrUejaMk588dAgd/XUDu80ZqGuVu371UCz8JbgLSyBCs04IJbjZ74v8Ic0b+m01 5xcwndLp6kKs8hDYNhQ/cbYBo/1+pR07ciO0TkIB5x6fyC8S X-Developer-Key: i=u.kleine-koenig@baylibre.com; a=openpgp; fpr=0D2511F322BFAB1C1580266BE2DCDD9132669BD6 Content-Transfer-Encoding: 8bit Generally it's not known how a disabled PWM behaves. However if the bootloader hands over a disabled PWM that drives a regulator and it's claimed the regulator is enabled, then the most likely assumption is that the PWM emits the inactive level. This is represented by duty_cycle = 0 even for .polarity == PWM_POLARITY_INVERSED. Put that assumption in a dedicated function, document that it relies on an assumption and use that in both functions pwm_regulator_init_state() and pwm_regulator_get_voltage(). With that pwm_regulator_init_boot_on() can be dropped, as it's only purpose is to make pwm_get_state() return a configuration that results in emitting constantly the inactive level if the PWM is off. Fixes: 6a7d11efd691 ("regulator: pwm-regulator: Calculate the output voltage for disabled PWMs") Fixes: b3cbdcc19181 ("regulator: pwm-regulator: Manage boot-on with disabled PWM channels") Signed-off-by: Uwe Kleine-König --- Hello, this is my suggestion to fix the concerns I expressed in https://lore.kernel.org/all/hf24mrplr76xtziztkqiscowkh2f3vmceuarecqcwnr6udggs6@uiof2rvvqq5v/ . It's only compile tested as I don't have a machine with a pwm-regulator. So getting test feedback before applying it would be great. Best regards Uwe drivers/regulator/pwm-regulator.c | 68 +++++++++++-------------------- 1 file changed, 23 insertions(+), 45 deletions(-) diff --git a/drivers/regulator/pwm-regulator.c b/drivers/regulator/pwm-regulator.c index 7434b6b22d32..648a53b67a2f 100644 --- a/drivers/regulator/pwm-regulator.c +++ b/drivers/regulator/pwm-regulator.c @@ -48,18 +48,37 @@ struct pwm_voltages { unsigned int dutycycle; }; +static unsigned int pwm_regulator_get_relative_dutycyle(struct regulator_dev *rdev, + unsigned int scale) +{ + struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev); + struct pwm_state pwm_state; + + pwm_get_state(drvdata->pwm, &pwm_state); + + if (!pwm_state.enabled) { + /* + * In general it's unknown what the output of a disabled PWM is. + * The only sane assumption here is it emits the inactive level + * which corresponds to duty_cycle = 0 (independent of the + * polarity). + */ + return 0; + } + + return pwm_get_relative_duty_cycle(&pwm_state, scale); +} + /* * Voltage table call-backs */ static void pwm_regulator_init_state(struct regulator_dev *rdev) { struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev); - struct pwm_state pwm_state; unsigned int dutycycle; int i; - pwm_get_state(drvdata->pwm, &pwm_state); - dutycycle = pwm_get_relative_duty_cycle(&pwm_state, 100); + dutycycle = pwm_regulator_get_relative_dutycyle(rdev, 100); for (i = 0; i < rdev->desc->n_voltages; i++) { if (dutycycle == drvdata->duty_cycle_table[i].dutycycle) { @@ -151,20 +170,10 @@ static int pwm_regulator_get_voltage(struct regulator_dev *rdev) int min_uV = rdev->constraints->min_uV; int max_uV = rdev->constraints->max_uV; int diff_uV = max_uV - min_uV; - struct pwm_state pstate; unsigned int diff_duty; unsigned int voltage; - pwm_get_state(drvdata->pwm, &pstate); - - if (!pstate.enabled) { - if (pstate.polarity == PWM_POLARITY_INVERSED) - pstate.duty_cycle = pstate.period; - else - pstate.duty_cycle = 0; - } - - voltage = pwm_get_relative_duty_cycle(&pstate, duty_unit); + voltage = pwm_regulator_get_relative_dutycyle(rdev, duty_unit); if (voltage < min(max_uV_duty, min_uV_duty) || voltage > max(max_uV_duty, min_uV_duty)) return -ENOTRECOVERABLE; @@ -321,32 +330,6 @@ static int pwm_regulator_init_continuous(struct platform_device *pdev, return 0; } -static int pwm_regulator_init_boot_on(struct platform_device *pdev, - struct pwm_regulator_data *drvdata, - const struct regulator_init_data *init_data) -{ - struct pwm_state pstate; - - if (!init_data->constraints.boot_on || drvdata->enb_gpio) - return 0; - - pwm_get_state(drvdata->pwm, &pstate); - if (pstate.enabled) - return 0; - - /* - * Update the duty cycle so the output does not change - * when the regulator core enables the regulator (and - * thus the PWM channel). - */ - if (pstate.polarity == PWM_POLARITY_INVERSED) - pstate.duty_cycle = pstate.period; - else - pstate.duty_cycle = 0; - - return pwm_apply_might_sleep(drvdata->pwm, &pstate); -} - static int pwm_regulator_probe(struct platform_device *pdev) { const struct regulator_init_data *init_data; @@ -404,11 +387,6 @@ static int pwm_regulator_probe(struct platform_device *pdev) if (ret) return ret; - ret = pwm_regulator_init_boot_on(pdev, drvdata, init_data); - if (ret) - return dev_err_probe(&pdev->dev, ret, - "Failed to apply boot_on settings\n"); - regulator = devm_regulator_register(&pdev->dev, &drvdata->desc, &config); if (IS_ERR(regulator)) { base-commit: 1613e604df0cd359cf2a7fbd9be7a0bcfacfabd0 -- 2.43.0