From nobody Thu Apr 9 07:52:54 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A16EBC4332F for ; Thu, 3 Nov 2022 11:02:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230510AbiKCLB6 (ORCPT ); Thu, 3 Nov 2022 07:01:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35404 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231316AbiKCLBl (ORCPT ); Thu, 3 Nov 2022 07:01:41 -0400 Received: from mail-pf1-x433.google.com (mail-pf1-x433.google.com [IPv6:2607:f8b0:4864:20::433]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0E307B7FD for ; Thu, 3 Nov 2022 04:01:40 -0700 (PDT) Received: by mail-pf1-x433.google.com with SMTP id d10so1248395pfh.6 for ; Thu, 03 Nov 2022 04:01:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; 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=jKnEZG0ef1w3+ZtyBo/x5Fjh/UyIelJHnysoJI3A1/8=; b=M/k9L/GATSz3t4hC6JTpBUGIjCJJ+L5qRie3ARsFP0YAVMwwS8hJcprzYnjHgBiZFC JQsycKDKpqWj8grx1dC5NEnjUIk2Sc4Nh3jWiqN8/ECXYdWtTAaKHZo0qV2rKIek12Xw 2TVsLH4Cz4GmkwcJSrR59QhTA1KmNdS1Hw2fx2ZHK9H+yr3QfkFuQO9DL47np6DRic4g Un5TVyF+TMYrtZWrYQ8QvE16ARmZMQV7rFI0AUa/2h+CxzLhQIiulGBW1W3ced9RNGCD dG++DjXtkjNM4EfSy61SpEIHefsrFsX67Swfdbwko9yf3HirK83PtIZ3o7uSCPnQ2hEn tm8w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=jKnEZG0ef1w3+ZtyBo/x5Fjh/UyIelJHnysoJI3A1/8=; b=P9V/x/CWeyEL2gg+ZD2R6S/1BtnIcMZaB+vuOz494y3hitDD4phEFt4r006ylrmmap m8D3zX8i9JkPXzNtTBMxc1YpQB/QiPnKhddLxjgFnUk7ke6q0U8SoNvsRwzD+IFPSpHj IFoVnwYc5kirE6YiNUYNluFOMjgsZSZATFxudxddMSN/7gzHC+18ZjYqcnFCrAtSlTal 26h0MSg5T9A/H3vC3rnrCiN4/1m+E+mvKn5N8jKceWlxyLzEBhq5rCa+6hxQ63t+LUiv AQRdfa510HJmcNhv6y/UvIEjmRv/lO4801fSv9P/6mlDw/r1LUhQ/y/9pei0A7JZ3tuE 85pQ== X-Gm-Message-State: ACrzQf0pKIB4beG7NJHNahXMnihFxFKBgtbGuGZuwjATtIH37Hfk7B35 sj4GH8XQmBpEdaZByotB80xg0g== X-Google-Smtp-Source: AMsMyM4dVhJX1yGhZhr6WzIziWnaGlZDw/A/iObTpehlPp5zHnq37z035BHCsoaJg+MsELfh+4zvqg== X-Received: by 2002:a63:106:0:b0:460:64ce:51c4 with SMTP id 6-20020a630106000000b0046064ce51c4mr25372692pgb.17.1667473299580; Thu, 03 Nov 2022 04:01:39 -0700 (PDT) Received: from localhost ([122.172.84.80]) by smtp.gmail.com with ESMTPSA id o9-20020a17090ab88900b0020d39ffe987sm699759pjr.50.2022.11.03.04.01.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Nov 2022 04:01:39 -0700 (PDT) From: Viresh Kumar To: James Calligeros , Viresh Kumar , Nishanth Menon , Stephen Boyd Cc: Viresh Kumar , linux-pm@vger.kernel.org, Vincent Guittot , "Rafael J. Wysocki" , linux-kernel@vger.kernel.org Subject: [PATCH 4/5] OPP: Simplify opp_parse_supplies() by restructuring it Date: Thu, 3 Nov 2022 16:31:07 +0530 Message-Id: X-Mailer: git-send-email 2.31.1.272.g89b43f80a514 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" opp_parse_supplies() has grown into too big of a routine (~190 lines) and it is not straight-forward to understand it anymore. Break it into smaller routines and reduce code redundancy a bit by using the same code to parse properties. This shouldn't result in any logical changes. Signed-off-by: Viresh Kumar --- drivers/opp/of.c | 216 ++++++++++++++++++----------------------------- 1 file changed, 81 insertions(+), 135 deletions(-) diff --git a/drivers/opp/of.c b/drivers/opp/of.c index e010e119c42b..e51c43495e21 100644 --- a/drivers/opp/of.c +++ b/drivers/opp/of.c @@ -578,179 +578,126 @@ static bool _opp_is_supported(struct device *dev, s= truct opp_table *opp_table, return false; } =20 -static int opp_parse_supplies(struct dev_pm_opp *opp, struct device *dev, - struct opp_table *opp_table) +static u32 *_parse_named_prop(struct dev_pm_opp *opp, struct device *dev, + struct opp_table *opp_table, + const char *prop_type, bool *triplet) { - u32 *microvolt, *microamp =3D NULL, *microwatt =3D NULL; - int supplies =3D opp_table->regulator_count; - int vcount, icount, pcount, ret, i, j; struct property *prop =3D NULL; char name[NAME_MAX]; + int count, ret; + u32 *out; =20 - /* Search for "opp-microvolt-" */ + /* Search for "opp--" */ if (opp_table->prop_name) { - snprintf(name, sizeof(name), "opp-microvolt-%s", + snprintf(name, sizeof(name), "opp-%s-%s", prop_type, opp_table->prop_name); prop =3D of_find_property(opp->np, name, NULL); } =20 if (!prop) { - /* Search for "opp-microvolt" */ - sprintf(name, "opp-microvolt"); + /* Search for "opp-" */ + snprintf(name, sizeof(name), "opp-%s", prop_type); prop =3D of_find_property(opp->np, name, NULL); - - /* Missing property isn't a problem, but an invalid entry is */ - if (!prop) { - if (unlikely(supplies =3D=3D -1)) { - /* Initialize regulator_count */ - opp_table->regulator_count =3D 0; - return 0; - } - - if (!supplies) - return 0; - - dev_err(dev, "%s: opp-microvolt missing although OPP managing regulator= s\n", - __func__); - return -EINVAL; - } - } - - if (unlikely(supplies =3D=3D -1)) { - /* Initialize regulator_count */ - supplies =3D opp_table->regulator_count =3D 1; - } else if (unlikely(!supplies)) { - dev_err(dev, "%s: opp-microvolt wasn't expected\n", __func__); - return -EINVAL; + if (!prop) + return NULL; } =20 - vcount =3D of_property_count_u32_elems(opp->np, name); - if (vcount < 0) { - dev_err(dev, "%s: Invalid %s property (%d)\n", - __func__, name, vcount); - return vcount; + count =3D of_property_count_u32_elems(opp->np, name); + if (count < 0) { + dev_err(dev, "%s: Invalid %s property (%d)\n", __func__, name, + count); + return ERR_PTR(count); } =20 - /* There can be one or three elements per supply */ - if (vcount !=3D supplies && vcount !=3D supplies * 3) { - dev_err(dev, "%s: Invalid number of elements in %s property (%d) with su= pplies (%d)\n", - __func__, name, vcount, supplies); - return -EINVAL; + /* + * Initialize regulator_count, if regulator information isn't provided + * by the platform. Now that one of the properties is available, fix the + * regulator_count to 1. + */ + if (unlikely(opp_table->regulator_count =3D=3D -1)) + opp_table->regulator_count =3D 1; + + if (count !=3D opp_table->regulator_count && + (!triplet || count !=3D opp_table->regulator_count * 3)) { + dev_err(dev, "%s: Invalid number of elements in %s property (%u) with su= pplies (%d)\n", + __func__, prop_type, count, opp_table->regulator_count); + return ERR_PTR(-EINVAL); } =20 - microvolt =3D kmalloc_array(vcount, sizeof(*microvolt), GFP_KERNEL); - if (!microvolt) - return -ENOMEM; + out =3D kmalloc_array(count, sizeof(*out), GFP_KERNEL); + if (!out) + return ERR_PTR(-EINVAL); =20 - ret =3D of_property_read_u32_array(opp->np, name, microvolt, vcount); + ret =3D of_property_read_u32_array(opp->np, name, out, count); if (ret) { dev_err(dev, "%s: error parsing %s: %d\n", __func__, name, ret); - ret =3D -EINVAL; - goto free_microvolt; + kfree(out); + return ERR_PTR(-EINVAL); } =20 - /* Search for "opp-microamp-" */ - prop =3D NULL; - if (opp_table->prop_name) { - snprintf(name, sizeof(name), "opp-microamp-%s", - opp_table->prop_name); - prop =3D of_find_property(opp->np, name, NULL); - } + if (triplet) + *triplet =3D count !=3D opp_table->regulator_count; =20 - if (!prop) { - /* Search for "opp-microamp" */ - sprintf(name, "opp-microamp"); - prop =3D of_find_property(opp->np, name, NULL); - } - - if (prop) { - icount =3D of_property_count_u32_elems(opp->np, name); - if (icount < 0) { - dev_err(dev, "%s: Invalid %s property (%d)\n", __func__, - name, icount); - ret =3D icount; - goto free_microvolt; - } + return out; +} =20 - if (icount !=3D supplies) { - dev_err(dev, "%s: Invalid number of elements in %s property (%d) with s= upplies (%d)\n", - __func__, name, icount, supplies); - ret =3D -EINVAL; - goto free_microvolt; - } +static u32 *opp_parse_microvolt(struct dev_pm_opp *opp, struct device *dev, + struct opp_table *opp_table, bool *triplet) +{ + u32 *microvolt; =20 - microamp =3D kmalloc_array(icount, sizeof(*microamp), GFP_KERNEL); - if (!microamp) { - ret =3D -EINVAL; - goto free_microvolt; - } + microvolt =3D _parse_named_prop(opp, dev, opp_table, "microvolt", triplet= ); + if (IS_ERR(microvolt)) + return microvolt; =20 - ret =3D of_property_read_u32_array(opp->np, name, microamp, - icount); - if (ret) { - dev_err(dev, "%s: error parsing %s: %d\n", __func__, - name, ret); - ret =3D -EINVAL; - goto free_microamp; + if (!microvolt) { + /* + * Missing property isn't a problem, but an invalid + * entry is. This property isn't optional if regulator + * information is provided. + */ + if (opp_table->regulator_count > 0) { + dev_err(dev, "%s: opp-microvolt missing although OPP managing regulator= s\n", + __func__); + return ERR_PTR(-EINVAL); } } =20 - /* Search for "opp-microwatt-" */ - prop =3D NULL; - if (opp_table->prop_name) { - snprintf(name, sizeof(name), "opp-microwatt-%s", - opp_table->prop_name); - prop =3D of_find_property(opp->np, name, NULL); - } - - if (!prop) { - /* Search for "opp-microwatt" */ - sprintf(name, "opp-microwatt"); - prop =3D of_find_property(opp->np, name, NULL); - } + return microvolt; +} =20 - if (prop) { - pcount =3D of_property_count_u32_elems(opp->np, name); - if (pcount < 0) { - dev_err(dev, "%s: Invalid %s property (%d)\n", __func__, - name, pcount); - ret =3D pcount; - goto free_microamp; - } +static int opp_parse_supplies(struct dev_pm_opp *opp, struct device *dev, + struct opp_table *opp_table) +{ + u32 *microvolt, *microamp, *microwatt; + int ret, i, j; + bool triplet; =20 - if (pcount !=3D supplies) { - dev_err(dev, "%s: Invalid number of elements in %s property (%d) with s= upplies (%d)\n", - __func__, name, pcount, supplies); - ret =3D -EINVAL; - goto free_microamp; - } + microvolt =3D opp_parse_microvolt(opp, dev, opp_table, &triplet); + if (IS_ERR_OR_NULL(microvolt)) + return PTR_ERR(microvolt); =20 - microwatt =3D kmalloc_array(pcount, sizeof(*microwatt), - GFP_KERNEL); - if (!microwatt) { - ret =3D -EINVAL; - goto free_microamp; - } + microamp =3D _parse_named_prop(opp, dev, opp_table, "microamp", NULL); + if (IS_ERR(microamp)) { + ret =3D PTR_ERR(microamp); + goto free_microvolt; + } =20 - ret =3D of_property_read_u32_array(opp->np, name, microwatt, - pcount); - if (ret) { - dev_err(dev, "%s: error parsing %s: %d\n", __func__, - name, ret); - ret =3D -EINVAL; - goto free_microwatt; - } + microwatt =3D _parse_named_prop(opp, dev, opp_table, "microwatt", NULL); + if (IS_ERR(microwatt)) { + ret =3D PTR_ERR(microwatt); + goto free_microamp; } =20 - for (i =3D 0, j =3D 0; i < supplies; i++) { + for (i =3D 0, j =3D 0; i < opp_table->regulator_count; i++) { opp->supplies[i].u_volt =3D microvolt[j++]; =20 - if (vcount =3D=3D supplies) { - opp->supplies[i].u_volt_min =3D opp->supplies[i].u_volt; - opp->supplies[i].u_volt_max =3D opp->supplies[i].u_volt; - } else { + if (triplet) { opp->supplies[i].u_volt_min =3D microvolt[j++]; opp->supplies[i].u_volt_max =3D microvolt[j++]; + } else { + opp->supplies[i].u_volt_min =3D opp->supplies[i].u_volt; + opp->supplies[i].u_volt_max =3D opp->supplies[i].u_volt; } =20 if (microamp) @@ -760,7 +707,6 @@ static int opp_parse_supplies(struct dev_pm_opp *opp, s= truct device *dev, opp->supplies[i].u_watt =3D microwatt[i]; } =20 -free_microwatt: kfree(microwatt); free_microamp: kfree(microamp); --=20 2.31.1.272.g89b43f80a514