From patchwork Sat Jan 13 22:46:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Martin Blumenstingl X-Patchwork-Id: 1886413 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=googlemail.com header.i=@googlemail.com header.a=rsa-sha256 header.s=20230601 header.b=H3bgI93m; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=2604:1380:45d1:ec00::1; helo=ny.mirrors.kernel.org; envelope-from=linux-pwm+bounces-780-incoming=patchwork.ozlabs.org@vger.kernel.org; receiver=patchwork.ozlabs.org) Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org [IPv6:2604:1380:45d1:ec00::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4TCD711Wtyz1yPV for ; Sun, 14 Jan 2024 09:47:09 +1100 (AEDT) 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 4AF2E1C20FE6 for ; Sat, 13 Jan 2024 22:47:06 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C155B17740; Sat, 13 Jan 2024 22:47:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=googlemail.com header.i=@googlemail.com header.b="H3bgI93m" X-Original-To: linux-pwm@vger.kernel.org Received: from mail-ej1-f49.google.com (mail-ej1-f49.google.com [209.85.218.49]) (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 2EBC3168A7; Sat, 13 Jan 2024 22:46:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=googlemail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=googlemail.com Received: by mail-ej1-f49.google.com with SMTP id a640c23a62f3a-a28d61ba65eso877374066b.3; Sat, 13 Jan 2024 14:46:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20230601; t=1705186017; x=1705790817; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=DY9iwm5mIi1yu3RP8RXkfD0AElUziXIbkBosoDfpThM=; b=H3bgI93mjqra2P+5N/BwWUiMBHWmxsV7ll8TbTsYsHXFlZ4BvvVT4HUKK7H8aliTac GlExmATvZaZAwV9zcAIXiqlDYYvkjZQ18P95mDfayPe63wy1+GlnQAOTrZYBS8/3Aue4 /U6ZjRy96SzzMBZgUD+SJQVPe/tLcmScUvBExoDklhldC5p9k4dGj9Gcggku8QLiG9H8 sK5asLsgHfnXiV9EVKFNAtn86jl01nmLoyCJVAXbWwZM1Q5Bc+yWumalbm3unGLLiNHm q0mAie1IvXc/V5nIzYF3jagS4KXYuOyWO5m8qbvkd4bmNeVZ8rtl54MrB930zZnvOpi3 QPRg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705186017; x=1705790817; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=DY9iwm5mIi1yu3RP8RXkfD0AElUziXIbkBosoDfpThM=; b=uvkya0e7jlaMn75Rx0r9GoC25U8Zfn1TE2tu7XE8k75dRo0/clDY62X6quylO1IY0Q zhxexOjUQSHy+0nBfsCCo1wrvnPEBF2VNemiFJMStZFJ1vaFVh1Ze7AVRlA/NBE/mhac riAYD823jQppvfD26ido9j6yfeKqzd2z4pn92Kqyb+J020sZJJ96s1FbHqD8tmwtD+9L La22FoUfmmy3HrdOrbIojbRFT9IZ/d+gSAecHR00InP+mFU23ytVT8CwVbDSwc6DglsC gf4Vp9pdwxDKr9Ot41nu5TLIpuhqJylIl7Lt2mje48LLCtxy1L6sKLlU0TVN0xZ1A22J dXSw== X-Gm-Message-State: AOJu0YyJpCxOWmGnfagOwVoNZgba+9xnynjsXJ0cE3ef/oIsFj/HF/lF J7fKx2t1K4jygSmeAg8j5kI= X-Google-Smtp-Source: AGHT+IH62OCCDlaYXs/phJT6VVbys7so+fsQxIC6B1SCuu1YODV45kYu9d5gsD4El1bDlgTa3vn+8w== X-Received: by 2002:a17:906:ae54:b0:a2d:559a:c72d with SMTP id lf20-20020a170906ae5400b00a2d559ac72dmr263432ejb.229.1705186017432; Sat, 13 Jan 2024 14:46:57 -0800 (PST) Received: from localhost.localdomain (dynamic-2a01-0c22-6fe7-c700-0000-0000-0000-0e63.c22.pool.telefonica.de. [2a01:c22:6fe7:c700::e63]) by smtp.googlemail.com with ESMTPSA id g18-20020a170906595200b00a2d4e658132sm755409ejr.42.2024.01.13.14.46.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 13 Jan 2024 14:46:57 -0800 (PST) From: Martin Blumenstingl To: lgirdwood@gmail.com, broonie@kernel.org, =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= , linux-pwm@vger.kernel.org, linux-amlogic@lists.infradead.org Cc: linux-kernel@vger.kernel.org, Heiner Kallweit , Dmitry Rokosov , Martin Blumenstingl Subject: [RFC PATCH v2 1/3] regulator: pwm-regulator: Add validity checks in continuous .get_voltage Date: Sat, 13 Jan 2024 23:46:26 +0100 Message-ID: <20240113224628.377993-2-martin.blumenstingl@googlemail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240113224628.377993-1-martin.blumenstingl@googlemail.com> References: <20240113224628.377993-1-martin.blumenstingl@googlemail.com> Precedence: bulk X-Mailing-List: linux-pwm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Continuous regulators can be configured to operate only in a certain duty cycle range (for example from 0..91%). Add a check to error out if the duty cycle translates to an unsupported (or out of range) voltage. Suggested-by: Uwe Kleine-König Signed-off-by: Martin Blumenstingl --- Changes from v1 -> v2: - this patch is new, using suggestions from Uwe drivers/regulator/pwm-regulator.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/drivers/regulator/pwm-regulator.c b/drivers/regulator/pwm-regulator.c index 698c420e0869..226ca4c62673 100644 --- a/drivers/regulator/pwm-regulator.c +++ b/drivers/regulator/pwm-regulator.c @@ -158,6 +158,9 @@ static int pwm_regulator_get_voltage(struct regulator_dev *rdev) pwm_get_state(drvdata->pwm, &pstate); voltage = pwm_get_relative_duty_cycle(&pstate, duty_unit); + if (voltage < min(max_uV_duty, min_uV_duty) || + voltage > max(max_uV_duty, min_uV_duty)) + return -ENOTRECOVERABLE; /* * The dutycycle for min_uV might be greater than the one for max_uV. From patchwork Sat Jan 13 22:46:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Blumenstingl X-Patchwork-Id: 1886414 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=googlemail.com header.i=@googlemail.com header.a=rsa-sha256 header.s=20230601 header.b=MDY9+/BQ; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=2604:1380:40f1:3f00::1; helo=sy.mirrors.kernel.org; envelope-from=linux-pwm+bounces-781-incoming=patchwork.ozlabs.org@vger.kernel.org; receiver=patchwork.ozlabs.org) Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org [IPv6:2604:1380:40f1:3f00::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4TCD7770DCz1yPV for ; Sun, 14 Jan 2024 09:47:15 +1100 (AEDT) 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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 74A86B21F67 for ; Sat, 13 Jan 2024 22:47:15 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E13ED182A0; Sat, 13 Jan 2024 22:47:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=googlemail.com header.i=@googlemail.com header.b="MDY9+/BQ" X-Original-To: linux-pwm@vger.kernel.org Received: from mail-ej1-f47.google.com (mail-ej1-f47.google.com [209.85.218.47]) (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 56D85171D8; Sat, 13 Jan 2024 22:47:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=googlemail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=googlemail.com Received: by mail-ej1-f47.google.com with SMTP id a640c23a62f3a-a26f73732c5so880465266b.3; Sat, 13 Jan 2024 14:47:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20230601; t=1705186018; x=1705790818; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=5JNMD09d2RI31XsyGdLgYlrGA0nsMzH49soHDGceK5A=; b=MDY9+/BQ1PzXeR2Sj1JDqdZGXFRJ/8tlGpVKdGEaQpCiAYUoGrgh/q9sfVZvhoSkpX /Mcnprk/fGIhQvUk7uJU7l/7aw5rVKAexC7OLtsTum3ipDcnleFM5aLhVYYckXadJHlA Fd/ak7kUjyYJemIeS7GXtv++kjp1t+V+4RgMlv6/aoJH+5a0Ot5LtjIj4oavWHSsBupt skOcYqvtdg7EiI5phshoyqKstWKTNsflW9GuvDI/dLXO2X20PurgBKGYxdbCaD6lFNB0 hv1Q5IYwlzuCowod+YWnRtPVkIxWH1nqumkxuxjh/BDbj4Gv7kmcb7W6bTWy8qQzRfLC nnjw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705186018; x=1705790818; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=5JNMD09d2RI31XsyGdLgYlrGA0nsMzH49soHDGceK5A=; b=IHKxnDzXGaHWoihXRCSML3qNNee3bsOV2E49U/7UbjXDONinjM3oUKcWZnZT93MzXY e5pjxSLIowtmxDDX8u/fBD7vGxX6sUKq3eQzH2GEJrJ1IW3KbYECUQeZr0BTxjNhU8kQ gEnLH6wBObWh0xh16PjaKTSQIRRy0EjAUpor0Rfv5wVKJa1RJS5vCVI38cRrjMUOLxr9 dW/68KM5VAQP3JuJXFwW8cdjF4geB8HI+mdQxDTyw/i4JwGWum5HAcDdvSs7Irn63odQ 5e6a4b332HzQZQqkN5MNmdxxGzHk8sAuNPpag8tcxjvddFeaph+rysVflCIuPzpZrijQ XVzQ== X-Gm-Message-State: AOJu0YwRDN7H1dmUqagfs88qRoj0x3vYWz+dxwyuDo2MeFXB0HMCRhup S81HD6gtuhn0o4ThXZnlLic582ULPMg= X-Google-Smtp-Source: AGHT+IH57+jQicl/nrMvgqRkc+mYqfkhCajogy2AYQezBOw34978xaHVIC68tHaYJzDWugdeqFxkPg== X-Received: by 2002:a17:907:908c:b0:a28:e2d7:b41d with SMTP id ge12-20020a170907908c00b00a28e2d7b41dmr1266919ejb.0.1705186018417; Sat, 13 Jan 2024 14:46:58 -0800 (PST) Received: from localhost.localdomain (dynamic-2a01-0c22-6fe7-c700-0000-0000-0000-0e63.c22.pool.telefonica.de. [2a01:c22:6fe7:c700::e63]) by smtp.googlemail.com with ESMTPSA id g18-20020a170906595200b00a2d4e658132sm755409ejr.42.2024.01.13.14.46.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 13 Jan 2024 14:46:57 -0800 (PST) From: Martin Blumenstingl To: lgirdwood@gmail.com, broonie@kernel.org, =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= , linux-pwm@vger.kernel.org, linux-amlogic@lists.infradead.org Cc: linux-kernel@vger.kernel.org, Heiner Kallweit , Dmitry Rokosov , Martin Blumenstingl Subject: [RFC PATCH v2 2/3] regulator: pwm-regulator: Calculate the output voltage for disabled PWMs Date: Sat, 13 Jan 2024 23:46:27 +0100 Message-ID: <20240113224628.377993-3-martin.blumenstingl@googlemail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240113224628.377993-1-martin.blumenstingl@googlemail.com> References: <20240113224628.377993-1-martin.blumenstingl@googlemail.com> Precedence: bulk X-Mailing-List: linux-pwm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 If a PWM output is disabled then it's voltage has to be calculated based on a zero duty cycle (for normal polarity) or duty cycle being equal to the PWM period (for inverted polarity). Add support for this to pwm_regulator_get_voltage(). Signed-off-by: Martin Blumenstingl --- Changes from v1 -> v2: - update pstate.duty_cycle to catch out-of-range values with the new check from the first patch in this series drivers/regulator/pwm-regulator.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/drivers/regulator/pwm-regulator.c b/drivers/regulator/pwm-regulator.c index 226ca4c62673..d27b9a7a30c9 100644 --- a/drivers/regulator/pwm-regulator.c +++ b/drivers/regulator/pwm-regulator.c @@ -157,6 +157,13 @@ static int pwm_regulator_get_voltage(struct regulator_dev *rdev) 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); if (voltage < min(max_uV_duty, min_uV_duty) || voltage > max(max_uV_duty, min_uV_duty)) From patchwork Sat Jan 13 22:46:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Blumenstingl X-Patchwork-Id: 1886415 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=googlemail.com header.i=@googlemail.com header.a=rsa-sha256 header.s=20230601 header.b=Piszy7Xm; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=147.75.199.223; helo=ny.mirrors.kernel.org; envelope-from=linux-pwm+bounces-782-incoming=patchwork.ozlabs.org@vger.kernel.org; receiver=patchwork.ozlabs.org) Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org [147.75.199.223]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4TCD7R25Xbz1yPV for ; Sun, 14 Jan 2024 09:47:31 +1100 (AEDT) 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 DADD41C20FA6 for ; Sat, 13 Jan 2024 22:47:28 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 314A118629; Sat, 13 Jan 2024 22:47:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=googlemail.com header.i=@googlemail.com header.b="Piszy7Xm" X-Original-To: linux-pwm@vger.kernel.org Received: from mail-ed1-f54.google.com (mail-ed1-f54.google.com [209.85.208.54]) (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 693851804A; Sat, 13 Jan 2024 22:47:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=googlemail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=googlemail.com Received: by mail-ed1-f54.google.com with SMTP id 4fb4d7f45d1cf-5592019711fso193203a12.0; Sat, 13 Jan 2024 14:47:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20230601; t=1705186019; x=1705790819; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=u77d1PdAZp3rFhBqhc5w9JQRTU1ABuAUI/VbGA4eG5E=; b=Piszy7Xm+GIOKAYhAve6L8kGuZslbrJO76Fkc73X/7cUdG5Mqc/jLCPWDGod8G9k37 OKFB1z4DEWByAGTt8++1BeGQJSd6XFmzV8MsTZq98N39N5mWiYv22hfLDULRl+UxMCeS 1r09UPn30Mx2mU881M+iFmViguVrXQLTe4JpFDrftNwKyJN3iCUN68dtlm65xEgoQDYg zAA2a1Yi7bgmBiC/tFVy3cUj3cTsyAHXd4xjKi8RCjj4QktuLKTkNJ/PqcsF1fb25XIq q5lUk02dUb0R8PUgNq4nvUFSs9rE0MBK2kXh6cn2A9/EJ72PeOTmBniYSKO68UIfEv8Q PmXQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705186019; x=1705790819; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=u77d1PdAZp3rFhBqhc5w9JQRTU1ABuAUI/VbGA4eG5E=; b=mZvYYvZaJWgb3ytQ6N8N2CQGoO90agEhKngV247VMsT8sFCwWCVYP9CAbBUN/I2Yzs LDbL59ucexP3e3DO6wPxww9FU7UHOe1+2pHDZAm1RWKmJfW+ySvDyGlTpwAJZp6Wgiu9 2Wl2+IdjkGnUjZyzB1s2frc7giBjV+ynj3YPz6hW09SyUHHqMuSwS//8fg4AnG7uSyRE yYbNFfIvZLdjQ1lqWyoHd/eEIubTpC+8v5TQJ4G8DcckV+aiCig2ZJskZ+oYvkFaO+/5 7O8KkkZ6h0c0c2kV1lxnXqBmiDUF6y9+MA3zsGm6/jEDyt+/kFNu71FyoMM6vVoZvYfF kPnQ== X-Gm-Message-State: AOJu0YzxXVTvPZB9dtLXMNBBG6t35nb+ajj49guU0wLXOj+3/E92ZNNg qxL4d7ViCDA+iPvbEmAYzWs= X-Google-Smtp-Source: AGHT+IGrTHqAsnkxLbC9/RDxevudhcPYQyFh07IzdE4LD1fX0nX39xtURTRDLkgg0t9KwbqmZEZvHA== X-Received: by 2002:a17:907:1a55:b0:a2b:1ba9:873b with SMTP id mf21-20020a1709071a5500b00a2b1ba9873bmr1400845ejc.125.1705186019470; Sat, 13 Jan 2024 14:46:59 -0800 (PST) Received: from localhost.localdomain (dynamic-2a01-0c22-6fe7-c700-0000-0000-0000-0e63.c22.pool.telefonica.de. [2a01:c22:6fe7:c700::e63]) by smtp.googlemail.com with ESMTPSA id g18-20020a170906595200b00a2d4e658132sm755409ejr.42.2024.01.13.14.46.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 13 Jan 2024 14:46:58 -0800 (PST) From: Martin Blumenstingl To: lgirdwood@gmail.com, broonie@kernel.org, =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= , linux-pwm@vger.kernel.org, linux-amlogic@lists.infradead.org Cc: linux-kernel@vger.kernel.org, Heiner Kallweit , Dmitry Rokosov , Martin Blumenstingl Subject: [RFC PATCH v2 3/3] regulator: pwm-regulator: Manage boot-on with disabled PWM channels Date: Sat, 13 Jan 2024 23:46:28 +0100 Message-ID: <20240113224628.377993-4-martin.blumenstingl@googlemail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240113224628.377993-1-martin.blumenstingl@googlemail.com> References: <20240113224628.377993-1-martin.blumenstingl@googlemail.com> Precedence: bulk X-Mailing-List: linux-pwm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Odroid-C1 uses a Monolithic Power Systems MP2161 controlled via PWM for the VDDEE voltage supply of the Meson8b SoC. Commit 6b9352f3f8a1 ("pwm: meson: modify and simplify calculation in meson_pwm_get_state") results in my Odroid-C1 crashing with memory corruption in many different places (seemingly at random). It turns out that this is due to a currently not supported corner case. The VDDEE regulator can generate between 860mV (duty cycle of ~91%) and 1140mV (duty cycle of 0%). We consider it to be enabled by the bootloader (which is why it has the regulator-boot-on flag in .dts) as well as being always-on (which is why it has the regulator-always-on flag in .dts) because the VDDEE voltage is generally required for the Meson8b SoC to work. The public S805 datasheet [0] states on page 17 (where "A5" refers to the Cortex-A5 CPU cores): [...] So if EE domains is shut off, A5 memory is also shut off. That does not matter. Before EE power domain is shut off, A5 should be shut off at first. It turns out that at least some bootloader versions are keeping the PWM output disabled. This is not a problem due to the specific design of the regulator: when the PWM output is disabled the output pin is pulled LOW, effectively achieving a 0% duty cycle (which in return means that VDDEE voltage is at 1140mV). The problem comes when the pwm-regulator driver tries to initialize the PWM output. To do so it reads the current state from the hardware, which is: period: 3666ns duty cycle: 3333ns (= ~91%) enabled: false Then those values are translated using the continuous voltage range to 860mV. Later, when the regulator is being enabled (either by the regulator core due to the always-on flag or first consumer - in this case the lima driver for the Mali-450 GPU) the pwm-regulator driver tries to keep the voltage (at 860mV) and just enable the PWM output. This is when things start to go wrong as the typical voltage used for VDDEE is 1100mV. Commit 6b9352f3f8a1 ("pwm: meson: modify and simplify calculation in meson_pwm_get_state") triggers above condition as before that change period and duty cycle were both at 0. Since the change to the pwm-meson driver is considered correct the solution is to be found in the pwm-regulator driver. Update the duty cycle during driver probe if the regulator is flagged as boot-on so that a call to pwm_regulator_enable() (by the regulator core during initialization of a regulator flagged with boot-on) without any preceding call to pwm_regulator_set_voltage() does not change the output voltage. [0] https://dn.odroid.com/S805/Datasheet/S805_Datasheet%20V0.8%2020150126.pdf Signed-off-by: Martin Blumenstingl --- Changes from v1 -> v2: - This patch is new and the idea is to keep the voltage regulator description in device-tree as-is (which means: support for 860mV to 1140mV). Instead we allow the pwm-regulator driver to preserve the output voltage at boot when enabling the regulator. - note: Mark Brown said in v1 "I'd expect a change in the init_state() function". That function is not updated as it's only relevant for regulators with a voltage table - on Odroid-C1 we have a continuous regulator though. drivers/regulator/pwm-regulator.c | 33 +++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/drivers/regulator/pwm-regulator.c b/drivers/regulator/pwm-regulator.c index d27b9a7a30c9..60cfcd741c2a 100644 --- a/drivers/regulator/pwm-regulator.c +++ b/drivers/regulator/pwm-regulator.c @@ -323,6 +323,32 @@ 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; @@ -382,6 +408,13 @@ 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) { + dev_err(&pdev->dev, "Failed to apply boot_on settings: %d\n", + ret); + return ret; + } + regulator = devm_regulator_register(&pdev->dev, &drvdata->desc, &config); if (IS_ERR(regulator)) {