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: 13519091 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 03C64C4707B for ; Sat, 13 Jan 2024 22:47:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=H8/r3jp6sozVRECr5lWVU5uq5EAfO/yKdm2HhS38nPg=; b=TDEfsgonKoZSaa bBBIbFzYw0e9WPOax6VTIkhs8HRaYuH2o4NR4kn9A75Apdy+6jne3f3Ku9uUJSiSwx+eyjy0wqEpC +AJCQcpE/mZ6Pf38V4ZtG56D+v324LVcZjTb7GF9rpTXewC4+0VWop4IRHAzg3D5TTXQtJUE9p0yd FAZqjiPltHLqK6xbus6oYKAuUcErbD6He0qNZl1H3LdpoPWT5xJg5LDXwiPW4ayNMtL24cCzkRuRE wgZoznjmwXyB2TFsk24N7w7EUf2rWS5US9j5UmzDR7Te+cOZt2UIGe/YStGwxtVU1T5vrr9XfrR6F LYNMdxJe4b8c4+TSruyw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1rOmmM-005qsU-1r; Sat, 13 Jan 2024 22:47:02 +0000 Received: from mail-ed1-x535.google.com ([2a00:1450:4864:20::535]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1rOmmJ-005qrd-32 for linux-amlogic@lists.infradead.org; Sat, 13 Jan 2024 22:47:01 +0000 Received: by mail-ed1-x535.google.com with SMTP id 4fb4d7f45d1cf-556c3f0d6c5so9143355a12.2 for ; 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=lists.infradead.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=dAfJlV21xePusYJuB/53y15qq4e1kcb/dIWfKFrMUF/489amSwT7DQxasariniUb5J zomc4Xe4I1dhhiPE4ujYPASxrO8AoWpt8p6jBa4iEhHS1qUrKZjXVN3enN37Hb2DeDId njNRN1NXDbeIDFxMHx/jODKXqtFJI7RzrvHuq7iCfjqy0b7leXfvzF/SAgqpQCR11NLb 0Xa0w2/xc3V0pyl/ch5yOzuurhIek1yPkEJfiKOpQ9kNcUvaQywWcpHp5mSd0daqrxGM y4GZUYUI/0KoMrsM43lNRS922iiohR/HwOEUuM/x2j/zLas65KcHgqNBhr/IHqOUrY2y TCBA== 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=QLNZHdRAAJBlVAfSaTU8xUlkhE+8FIA2Gcqh6fPsyxYExljhYoccecerBlCjqUdcbU /5gSrxt53tsV6T6Cbz4yJXYCp78nKtq4kpv3oJ9w4++7w5+er+woUtxvfcTiZI75VZpU 6XoQsWiHUe84ik2Xr3xzwBLMXnIpxIDQnv8tutEaLexW05Rkd06vyIOGS571IEsduYMX fUYaoWG7C2T7uIPhOokKcLcrsxMnlal4aPiDmk7NdzWc2seon37s8ocNaPXR8b3VgSUR PKsB18wVLzJhiEGQKzmIRKBRPq/pOP7a8eKXFc5QuuPzt+InY/AWyphK2pExhqVGMJN5 SIrQ== X-Gm-Message-State: AOJu0Yyhu4dLw989iFhRfrL+O9f93DWuS++naV/DXbzYRYbKGUPlzxB1 vKNTxcMZQvqaYjMr1zGiXIk= 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> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240113_144659_986595_55487BAA X-CRM114-Status: GOOD ( 12.03 ) X-BeenThere: linux-amlogic@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-amlogic" Errors-To: linux-amlogic-bounces+linux-amlogic=archiver.kernel.org@lists.infradead.org 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: 13519089 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 6CEEBC4707B for ; Sat, 13 Jan 2024 22:47:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=y8L+Wv5lR/o1pL+z/lHA22VTebtFTkepJWGAiz0ulik=; b=G3IAJWBGOojrW1 RAtwCXsrvNcZO4Ig/1q/SqlonAMyBvkDHAXhgxPS57LIpjkw1CpKuQ1Xq76dX440C4u2w0EgQ/QYd pXoY0NTFP9YNDa/WMiKMawRsoWlzWKcGtEkoVILao88XfvpJAdYKxtw6guHr1IaEsqe22XbYVtWZr 28Vy7JmOTXL7GPd/FMoGLzRko7zoxEr6VTF3PNgmFmN3PqU/NhkxZdZMJ7erabNSPn+o8Lv0Nr22P 0rHaQdbWGb0gsKUhnz/VtQVV96URuSVRe5x3oWLZqjy7+8zgoTYCtsfEXbBT2qHsSKQoKt7daCscS UXtAAtnrXZDh+sGacQGA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1rOmmO-005qt3-1O; Sat, 13 Jan 2024 22:47:04 +0000 Received: from mail-ed1-x536.google.com ([2a00:1450:4864:20::536]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1rOmmM-005qrv-0J for linux-amlogic@lists.infradead.org; Sat, 13 Jan 2024 22:47:03 +0000 Received: by mail-ed1-x536.google.com with SMTP id 4fb4d7f45d1cf-5542a7f1f3cso9094472a12.2 for ; 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=1705186018; x=1705790818; darn=lists.infradead.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=LiEAEkJrXfjETKrcXKn2UbwjCnuK7afY6eeDnO0gw5RJ1KuWirGZB58MoCnuqoHaqr UdTpJootIVoSgEX0stoD5HdhsLBZy/kxLJ3xKVCNWsc294f2Ns9xpuM8OWPCeTCHS0Sd T6TYy54iBHIwjz5GF0mvqudyO4lXzWmc4JDsHT08bF2U2euQjByY8R+GUgk1uQn8i4LY PvTU2Q/Ioz7NpA/jAYPMFOX9ajo7WUmzX2ky2tGIcQS8OdsrN9nFMDNSLs60WhVsfjOc 7bBkTb5v6EO+DrNIXXfA5sYbiAcu6Z0Nw631376lXf+vJB9zG4vzZJNUrXsx85jBtzUq BuSg== 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=S48OxP5W5P8NkGzxkGQ0oJphIEo+TWdQbcGK5hJlW1XxlGGttIpAi2fIi6CyrCMoW3 PVEG8062wfvRCYZ/kk8u03rpgw+OY2wWCt7KJWNZdwcR3Xx8arxAwgyrFZ0ncd7Ltm7H UTPBjTIKcJPDXzp4RA6eN/TPN77L0Xi+UKCenvvN4HrfvO2/MUGtEzHf1y3bxNqZ8vqa bLvQ5clfdHG3x9t0yEXEyvhbXyqClFCChemcq4hCl/2lPlpWDjunJw80/lKhfFTlKUVD IDS9EBV5heMYidjNee2bAsPzIk8kD83eQ8B7hmIvUcft3ATdjDU53jW6gfyt5iofhQEF HD7Q== X-Gm-Message-State: AOJu0YxMFYuCWhiaqootXz7mkfiu5Wq/JC3DWvYR4AfOyvEpQPhWVhRl 3zXo/JIg+dzogVH/M0h969g= 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> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240113_144702_134332_487BAA39 X-CRM114-Status: GOOD ( 12.01 ) X-BeenThere: linux-amlogic@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-amlogic" Errors-To: linux-amlogic-bounces+linux-amlogic=archiver.kernel.org@lists.infradead.org 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: 13519090 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 88821C4706C for ; Sat, 13 Jan 2024 22:47:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=cdAUId+Y98du5hFB+cCcLY5jG/4hS77HLc61FbhETeM=; b=rHABC5e68BzxTd UB2hJuyN7rwb2kgfveUqXgi4k50VCsAuZbCMWQqNRWUULmhjUGpUuL4DsQ2+QFmKY3rj9HvHesozx dbYmybKaupsL8NQalRvZQ+MiD1sCh0iJL61psYVd9PtK4ozk+50zHAuSbANBUMXN+0QuKpeDrmVF+ mGH9QTq0wY5Ncxao8M4Wcf8q1biX8UStF7W+YaepnWBDoCwKvB+XyKBqcLvPgcyeclLsfnSjVW8tQ YUDjGKWPF9M8ubrwZH007B9RP4NPK7m7sAuPPmdTvytje2eNaBvVmV77RwAbPecrbwbXDQ/jllUkW P0CyCdFwOAhPl4WIaYag==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1rOmmQ-005qtJ-2j; Sat, 13 Jan 2024 22:47:06 +0000 Received: from mail-ed1-x52e.google.com ([2a00:1450:4864:20::52e]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1rOmmO-005qsE-0I for linux-amlogic@lists.infradead.org; Sat, 13 Jan 2024 22:47:05 +0000 Received: by mail-ed1-x52e.google.com with SMTP id 4fb4d7f45d1cf-55818b733f7so6142648a12.3 for ; Sat, 13 Jan 2024 14:47:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20230601; t=1705186019; x=1705790819; darn=lists.infradead.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=XhuUsE3gQDQvRSw52h929tkpqUxI55ljZv97WDQyURgprfiBxN54utcoY7lVnjP049 2zKfFgo4uOvHbuNlmt+4UCYrU2W7/OOmqall4hUY+zcOhx6xBNT0HdAtv57miIq7MN1+ YAnO74EKeBaNBGaus5Grsdg86QdwoLzdO+7we2AKLiuY1TCzB5SJD8w3zC+8hKAbUBkw pyxcErnpHc64dgDT6SH+Rm+zpe6UQ/eousdqmkNF9/lskk/e5an7symkMSseiJAecrpe Qf0sLvMmoGZsmdSGZPx4xhXP9+OMMkUzyfX1TSFmPxgBYuh/61705htjT2Ha0m2HI7p8 eDdg== 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=GwZZTJKldCa8PfSkgMzkqhGfD5eXvJ33hImD/9wOvtCm8YM4osl9o73u6ME8qH8ZaZ 5bI0HNvLsQmpU1y2pMQEeTguQjMbtw9bwZZuKU/EGRtCPyyLANK97P5aUNVen+dHEUUZ a3xC/KyA5+MUE6sHvk9kmqKxBxfYQIBT4/GY1QAGz3zRcgohBO/xKZUJHukmm59MZV9J w5gc4ETg+/KJpTzwgRn9wzeLzS/qsH/7OLILRfTsCBzxS9cX2VgXxiPJc/0AF8fSpiNG fpphDGsLsr7PosJSq6OeI6ZNiijMfY2s4iypOJrRAw9JaIRtJbpFMh1uhvXwP+uNBm05 Rn1g== X-Gm-Message-State: AOJu0Yz4tp4VeBQe4QLwhzP4Bqej0PYfx+RdKFO2M5WxtvCBv2XBvTWc TVZdiEWau+xewOSx8lBWVkE= 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> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240113_144704_127737_C4252CB4 X-CRM114-Status: GOOD ( 27.86 ) X-BeenThere: linux-amlogic@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-amlogic" Errors-To: linux-amlogic-bounces+linux-amlogic=archiver.kernel.org@lists.infradead.org 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)) {