From patchwork Wed Jul 12 11:47:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Naresh Solanki X-Patchwork-Id: 702003 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 2AB45EB64D9 for ; Wed, 12 Jul 2023 11:50:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233497AbjGLLt6 (ORCPT ); Wed, 12 Jul 2023 07:49:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38048 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233089AbjGLLtR (ORCPT ); Wed, 12 Jul 2023 07:49:17 -0400 Received: from mail-wr1-x435.google.com (mail-wr1-x435.google.com [IPv6:2a00:1450:4864:20::435]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2272E213C for ; Wed, 12 Jul 2023 04:48:33 -0700 (PDT) Received: by mail-wr1-x435.google.com with SMTP id ffacd0b85a97d-3159d75606dso3164519f8f.1 for ; Wed, 12 Jul 2023 04:48:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=9elements.com; s=google; t=1689162503; x=1691754503; 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=i5BOr/cNAxLqespyX9yFLeLzm/eFPOS6HvzeibnDsPE=; b=eFKUE+hsXTXdVoZU/iamy44AFLV+pMDVAtXHkJgt9l4OlmmlA2YH61u8RCY83gil44 MCKUX0J08FctFthaeX6s6fQ+HYux1GlyfqMFsOR0LFMUUwwFIoA0qpVp0a/LRFz4LpSJ AgV5JN4O1P8GwuvpWeCFmoksvBGgCezw5E5FiL4r9FthpPV0ksyOv+frifxdwixNXfcI NXL259LldlSJpUs0XElVi8nJPj5vlKP9iywhwqmHSawEFtSWjnO+jjGqGWq44Tqz0vYb 7ScntcmRdSO9tzZwLmPJJWwnqIfTv9QNz7KcB15Ne443TV2uVcJuBWJ6i4ua4fQ2zF8C 9mow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689162503; x=1691754503; 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=i5BOr/cNAxLqespyX9yFLeLzm/eFPOS6HvzeibnDsPE=; b=Xi1SZ6JdZKMdKo5XUfV6GGMbM2sGnwtUEoZzFkZZlIRyWq380JP+d4vy6mnIWX4Twb OXiDF5Fq/2zVSlIbDdGBmeyHBaMS2Rrs7wkh03CRBZnzz/XlTdQD1yC1T4ScVEnthnnP oPhBl+aYZdcCJfY9tB/sD7GtKn575rYSIiGz+/6Hp76fBBL0QMpiIt/OAlRG+pFXGplA Ykp+XbQ/BEmUuYxqibi4CTGy1kXgjeN+bxOjHmxLNQvBFEL81bWv1GZyZF5wPurKUF2B vZ43VhDmv+gK+ckvjPTGvDHsLLfQvsTP11AnKo2RB/KSjCYDS3hQ3EacM6WPs8sojgYT IkSA== X-Gm-Message-State: ABy/qLbEhDWupSoPv1SOToW4liDHyBgsx49Q7bR9TekOYhIKcY8cjB9C 8hHEq8M2vWFwpCJSyXZOo6SsQ4SC3ZXcGbrboSwlyw== X-Google-Smtp-Source: APBJJlEEhHBvM+eUHyM06WHoH5MvzwC3d7TIGscJLcajhsTTUrYwOe65chTKH5iIbLWYddmuAffZkQ== X-Received: by 2002:a5d:4c88:0:b0:314:35df:4a3e with SMTP id z8-20020a5d4c88000000b0031435df4a3emr16296631wrs.7.1689162502828; Wed, 12 Jul 2023 04:48:22 -0700 (PDT) Received: from stroh80.sec.9e.network (ip-078-094-000-051.um19.pools.vodafone-ip.de. [78.94.0.51]) by smtp.gmail.com with ESMTPSA id j2-20020a5d4482000000b003143867d2ebsm4867463wrq.63.2023.07.12.04.48.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Jul 2023 04:48:22 -0700 (PDT) From: Naresh Solanki X-Google-Original-From: Naresh Solanki To: devicetree@vger.kernel.org, Guenter Roeck , Jean Delvare Cc: linux-kernel@vger.kernel.org, linux-hwmon@vger.kernel.org, Patrick Rudolph , Naresh Solanki Subject: [PATCH 8/8] hwmon: (pmbus/mp2975) Add OCP limit Date: Wed, 12 Jul 2023 13:47:49 +0200 Message-ID: <20230712114754.500477-8-Naresh.Solanki@9elements.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230712114754.500477-1-Naresh.Solanki@9elements.com> References: <20230712114754.500477-1-Naresh.Solanki@9elements.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: devicetree@vger.kernel.org From: Patrick Rudolph Add support for PMBUS_IOUT_OC_FAULT_LIMIT. Add a helper function to convert the limit to LINEAR11 format and read data->info.phases[0] on MP2971 and MP2973 as well. Signed-off-by: Patrick Rudolph Signed-off-by: Naresh Solanki --- drivers/hwmon/pmbus/mp2975.c | 76 ++++++++++++++++++++++++++++++------ 1 file changed, 65 insertions(+), 11 deletions(-) diff --git a/drivers/hwmon/pmbus/mp2975.c b/drivers/hwmon/pmbus/mp2975.c index 27a9724aed68..0a6669f865f6 100644 --- a/drivers/hwmon/pmbus/mp2975.c +++ b/drivers/hwmon/pmbus/mp2975.c @@ -65,6 +65,10 @@ #define MP2973_VID_STEP_SEL_R2 BIT(3) #define MP2973_IMVP9_EN_R2 BIT(13) +#define MP2973_MFR_OCP_TOTAL_SET 0x5f +#define MP2973_OCP_TOTAL_CUR_MASK GENMASK(6, 0) +#define MP2973_MFR_OCP_LEVEL_RES BIT(15) + #define MP2973_MFR_READ_IOUT_PK 0x90 #define MP2973_MFR_READ_POUT_PK 0x91 @@ -153,6 +157,41 @@ mp2975_vid2direct(int vrf, int val) return 0; } +#define MAX_LIN_MANTISSA (1023 * 1000) +#define MIN_LIN_MANTISSA (511 * 1000) + +/* Converts a milli-unit DIRECT value to LINEAR11 format */ +static u16 mp2975_data2reg_linear11(s64 val) +{ + s16 exponent = 0, mantissa; + bool negative = false; + + /* simple case */ + if (val == 0) + return 0; + + /* Reduce large mantissa until it fits into 10 bit */ + while (val >= MAX_LIN_MANTISSA && exponent < 15) { + exponent++; + val >>= 1; + } + /* Increase small mantissa to improve precision */ + while (val < MIN_LIN_MANTISSA && exponent > -15) { + exponent--; + val <<= 1; + } + + /* Convert mantissa from milli-units to units */ + mantissa = clamp_val(DIV_ROUND_CLOSEST_ULL(val, 1000), 0, 0x3ff); + + /* restore sign */ + if (negative) + mantissa = -mantissa; + + /* Convert to 5 bit exponent, 11 bit mantissa */ + return (mantissa & 0x7ff) | ((exponent << 11) & 0xf800); +} + static int mp2975_read_phase(struct i2c_client *client, struct mp2975_data *data, int page, int phase, u8 reg) @@ -297,6 +336,20 @@ static int mp2973_read_word_data(struct i2c_client *client, int page, ret = pmbus_read_word_data(client, page, phase, MP2973_MFR_READ_IOUT_PK); break; + case PMBUS_IOUT_OC_FAULT_LIMIT: + ret = mp2975_read_word_helper(client, page, phase, + MP2973_MFR_OCP_TOTAL_SET, + GENMASK(15, 0)); + if (ret < 0) + return ret; + + if (ret & MP2973_MFR_OCP_LEVEL_RES) + ret = 2 * (ret & MP2973_OCP_TOTAL_CUR_MASK); + else + ret = ret & MP2973_OCP_TOTAL_CUR_MASK; + + ret = mp2975_data2reg_linear11(ret * info->phases[page] * 1000); + break; case PMBUS_UT_WARN_LIMIT: case PMBUS_UT_FAULT_LIMIT: case PMBUS_VIN_UV_WARN_LIMIT: @@ -307,7 +360,6 @@ static int mp2973_read_word_data(struct i2c_client *client, int page, case PMBUS_IIN_OC_FAULT_LIMIT: case PMBUS_IOUT_OC_LV_FAULT_LIMIT: case PMBUS_IOUT_OC_WARN_LIMIT: - case PMBUS_IOUT_OC_FAULT_LIMIT: case PMBUS_IOUT_UC_FAULT_LIMIT: case PMBUS_POUT_OP_FAULT_LIMIT: case PMBUS_POUT_OP_WARN_LIMIT: @@ -481,11 +533,13 @@ mp2975_identify_multiphase(struct i2c_client *client, struct mp2975_data *data, if (info->phases[0] > data->max_phases[0]) return -EINVAL; - mp2975_set_phase_rail1(info); - num_phases2 = min(data->max_phases[0] - info->phases[0], - data->max_phases[1]); - if (info->phases[1] && info->phases[1] <= num_phases2) - mp2975_set_phase_rail2(info, num_phases2); + if (data->chip_id == mp2975) { + mp2975_set_phase_rail1(info); + num_phases2 = min(data->max_phases[0] - info->phases[0], + data->max_phases[1]); + if (info->phases[1] && info->phases[1] <= num_phases2) + mp2975_set_phase_rail2(info, num_phases2); + } return 0; } @@ -879,12 +933,12 @@ static int mp2975_probe(struct i2c_client *client) data->info.num_regulators = MP2975_PAGE_NUM; } - if (data->chip_id == mp2975) { - /* Identify multiphase configuration. */ - ret = mp2975_identify_multiphase(client, data, info); - if (ret) - return ret; + /* Identify multiphase configuration. */ + ret = mp2975_identify_multiphase(client, data, info); + if (ret) + return ret; + if (data->chip_id == mp2975) { /* Identify VID setting per rail. */ ret = mp2975_identify_rails_vid(client, data, info); if (ret < 0)