From nobody Thu Apr 2 20:14:48 2026 Received: from mail-wr1-f48.google.com (mail-wr1-f48.google.com [209.85.221.48]) (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 437DE2DB7AA for ; Tue, 17 Feb 2026 08:12:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.48 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771315933; cv=none; b=bY14kaFahS7l8Og9mb2wU5oIeGCe6EMKS2BBLV2JKKTQSM4yFHDROaJlOttXGdFZ/Y3NBfyu9JsnHuatVPnRUMzTmqS7nsUboaYMNBYgUQf9a8ebuk94R39VBjjooa2lF2VvIG7ism3LpSt6S+7Ozfoc9pbSc7IhcaWPl29rkc8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771315933; c=relaxed/simple; bh=MBxXvm9SN85wIbPQozevHNwhIzjPtliVSJlxB+6iP8A=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=VbH0w/SKswxyIDoNder8jHzqGU7/8DDbQERLWK0IJE2uEgYLCdLx78Mavli+A1RRckkUIOdX+hHt3cxXdowuJHSN6uT17cWBekRsSP00kzyy29YL/5zwFoLDTNFFjyQZiNgJIio6OqxKAMQdj8B4ltNHREf1nx9auG1AfC4QLLg= 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=CGEhoovy; arc=none smtp.client-ip=209.85.221.48 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 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="CGEhoovy" Received: by mail-wr1-f48.google.com with SMTP id ffacd0b85a97d-4359a316d89so3467924f8f.0 for ; Tue, 17 Feb 2026 00:12:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1771315931; x=1771920731; 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=nkpDAlBYfnDGxcVrWynzmo5K/OoXZi98TxFwnjNQDvI=; b=CGEhoovyrTXMH3AGZk2ioO/R21qCAlzaHC+2A16Mf4inI7GtLktrKRTdTA/ayTqeVz vmUzKjuG845s16Fest5iKf7P8To540+UB8NLWZ8sdSTMgnMxtV8kCd+5oTQQtiLJQVIp xGsC874JgyTizIek/XIQryT5M9rX6KNE96LvS72Jfj10UC7NxCsExUVvSCv+tCu7VDI5 govp1pXa8oyj1aL06273xXk2KBU2XnSYuEwfoYSlGjO/1E+LiZpwB/h3SBWPrd6PA4Eh BN+UN283Z49PBY4uIlasQHhCMXB4Qe4d4KKRwA8doiwPTMxcPAFMtXylRDo6ii7cIPUW hldQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1771315931; x=1771920731; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=nkpDAlBYfnDGxcVrWynzmo5K/OoXZi98TxFwnjNQDvI=; b=smVrXKFhmt/5GRZzZdMGpybbimtDGEN4+t/eOvTkxR7Nt3C2E+tdyx7fEwA6IvZ0ty 0M7MdFxEKQfrMxrUQOdT1i+H6tZmCk1JVBqU7t2+x/IR8CMBTrkYJFWHZUKs0NK5xmn4 c7/sQ98Y7I+Qf5BiKtip79oCuG2VC6VklUOON5GPRy9AixuxPiVk7By9VmxTKjBb/5Z0 C/ADWxmg99hHXfWdCeKFbDMjVQCLOq2YIpKz/FwE6W8qCl0Tb/hAoj6Q89dY8vVb9lgd 4KNq729jbsf1U+TXGByBBIqLaGAV109VUXRQq0Hkk9ZSph0s6hvQ8QTOFmhPfD+rkKYT HkBQ== X-Forwarded-Encrypted: i=1; AJvYcCXXCBWEvKGGk4plXw8MJRb6v8kPN7H6YITXqUXnvV1FMPGdLXvoQW7675UVilikn5Dh7vGmLKmDXSNF3cs=@vger.kernel.org X-Gm-Message-State: AOJu0Yw9pbF/xLTsB1bC8RykruKGJurYbpGIHJ8ElRTXdRYDqJY8olkX v/CzmfTkTpzEPrpCdtmF00OdMVHBO3RDWvbUAb5ImaTPYAhgqiGZfKVxDJf73xBjFhs= X-Gm-Gg: AZuq6aKfkpakJLRnDj5BMDUBjZAEsQfp6+XhVNsH86sQQUHl9MOlYZDTCJ6Hfr826Mp UY4V25kVdaKVM2yb1AyCoz8CUVrPAe0tQeQ18Dqllzdp0CgOid+JCXKf/dTkdtuZHEshqIHeQef kTx5neAqwp7xAEvmh/Lv5CFfO1sAI+YFOOA2hHwOv+V0I568Ju32jKBoFmtJL32tjtrAUoUKEkm CzwaEpn1ARId7nWZUvJNjHJKJjlLH22FzAU/wLm5CZ6+Qt9JBhGwuZ4F9byPxgeHct9eEKsh3x4 6RtJG220y98lrZ/m+eTZJ4n5myf//kzdLy0JMH+PCFOOUY9i6QcR1U3syczeNvxX1Gpc1E3uO2y qx/BdhzAA8XPFg0XBA5HU12VAD0fK5/o/QxByzmka9xYtUWdiPkCRGrDWhxzZP622HRvav820pP BSVrkS86/V4rOQm+Rh45Sz44Vlgw/QmScF7qZmfaMhP6pLiIvNi3SL//AjbMNzlqE5y5FpWz4nh m9gP2BSOPW0kSY/oDuE X-Received: by 2002:a05:6000:4203:b0:437:8fc0:8802 with SMTP id ffacd0b85a97d-4379793d751mr24549679f8f.48.1771315930539; Tue, 17 Feb 2026 00:12:10 -0800 (PST) Received: from silence.. ([46.10.240.40]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-43796a5d156sm35757690f8f.5.2026.02.17.00.12.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Feb 2026 00:12:10 -0800 (PST) From: Stoyan Bogdanov To: jbrunet@baylibre.com, linux@roeck-us.net, robh@kernel.org, krzk+dt@kernel.org, conor+dt@kernel.org, corbet@lwn.net, skhan@linuxfoundation.org Cc: linux-hwmon@vger.kernel.org, devicetree@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, Stoyan Bogdanov Subject: [PATCH v3 1/3] hwmon: (pmbus/tps25990): Rework TPS25990 non standatd direct conversion Date: Tue, 17 Feb 2026 10:12:01 +0200 Message-Id: <20260217081203.1792025-2-sbogdanov@baylibre.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260217081203.1792025-1-sbogdanov@baylibre.com> References: <20260217081203.1792025-1-sbogdanov@baylibre.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Rework existing implementation for calculation of direct format conversion for TPS25990. With this implamentation is leveraged code reusability for non standard parameters. - Add enum for parameter - Add m, b, R structure to hold value per device - Add data structure to hold for pmbus_driver_info and local_direct_values - Conversion functions are implemented according to formula from TPS25990 datasheet - Remove previously used defines replace with structure Signed-off-by: Stoyan Bogdanov --- drivers/hwmon/pmbus/tps25990.c | 115 +++++++++++++++++++++++++-------- 1 file changed, 88 insertions(+), 27 deletions(-) diff --git a/drivers/hwmon/pmbus/tps25990.c b/drivers/hwmon/pmbus/tps25990.c index c13edd7e1abf..33f6367f797c 100644 --- a/drivers/hwmon/pmbus/tps25990.c +++ b/drivers/hwmon/pmbus/tps25990.c @@ -36,17 +36,58 @@ #define TPS25990_UNLOCKED BIT(7) =20 #define TPS25990_8B_SHIFT 2 -#define TPS25990_VIN_OVF_NUM 525100 -#define TPS25990_VIN_OVF_DIV 10163 -#define TPS25990_VIN_OVF_OFF 155 -#define TPS25990_IIN_OCF_NUM 953800 -#define TPS25990_IIN_OCF_DIV 129278 -#define TPS25990_IIN_OCF_OFF 157 =20 #define PK_MIN_AVG_RST_MASK (PK_MIN_AVG_RST_PEAK | \ PK_MIN_AVG_RST_AVG | \ PK_MIN_AVG_RST_MIN) =20 +enum tps25990_parameters { + TPS25990_VIN_OVF =3D 0, /* VIN over volatage fault */ + TPS25990_IIN_OCF, /* IIN Over currect fault */ + TPS25590_DIRECT_VALUES_MAX, /* Max value ensure there enough space */ +}; + +struct local_direct_value { + int m[TPS25590_DIRECT_VALUES_MAX]; /* mantissa for direct data format */ + int b[TPS25590_DIRECT_VALUES_MAX]; /* offset */ + int R[TPS25590_DIRECT_VALUES_MAX]; /* exponent */ +}; + +struct tps25990_data { + struct pmbus_driver_info *info; + struct local_direct_value *info_local; +}; + +static int tps25990_raw_to_value(struct i2c_client *client, int param, int= raw) +{ + struct tps25990_data *data =3D (struct tps25990_data *)of_device_get_matc= h_data(&client->dev); + struct local_direct_value *info_local =3D data->info_local; + + /* Formula : X =3D (Y / 10^R - b) / m */ + if (info_local->R[param] >=3D 0) + raw /=3D int_pow(10, info_local->R[param]); + else + raw *=3D int_pow(10, -info_local->R[param]); + + return DIV_ROUND_CLOSEST(raw - info_local->b[param], info_local->m[param]= ); +} + +static unsigned int tps25990_value_to_raw(struct i2c_client *client, int p= aram, int val) +{ + struct tps25990_data *data =3D (struct tps25990_data *)of_device_get_matc= h_data(&client->dev); + struct local_direct_value *info_local =3D data->info_local; + + /* Formula : Y =3D ( m * X + b) * 10^R */ + val =3D (long)val * info_local->m[param] + info_local->b[param]; + + if (info_local->R[param] >=3D 0) + val *=3D int_pow(10, info_local->R[param]); + else + val =3D DIV_ROUND_CLOSEST(val, int_pow(10, -info_local->R[param])); + + return val; +} + /* * Arbitrary default Rimon value: 1kOhm * This correspond to an overcurrent limit of 55A, close to the specified = limit @@ -184,9 +225,7 @@ static int tps25990_read_word_data(struct i2c_client *c= lient, ret =3D pmbus_read_word_data(client, page, phase, reg); if (ret < 0) break; - ret =3D DIV_ROUND_CLOSEST(ret * TPS25990_VIN_OVF_NUM, - TPS25990_VIN_OVF_DIV); - ret +=3D TPS25990_VIN_OVF_OFF; + ret =3D tps25990_raw_to_value(client, TPS25990_VIN_OVF, ret); break; =20 case PMBUS_IIN_OC_FAULT_LIMIT: @@ -198,9 +237,7 @@ static int tps25990_read_word_data(struct i2c_client *c= lient, ret =3D pmbus_read_byte_data(client, page, TPS25990_VIREF); if (ret < 0) break; - ret =3D DIV_ROUND_CLOSEST(ret * TPS25990_IIN_OCF_NUM, - TPS25990_IIN_OCF_DIV); - ret +=3D TPS25990_IIN_OCF_OFF; + ret =3D tps25990_raw_to_value(client, TPS25990_IIN_OCF, ret); break; =20 case PMBUS_VIRT_SAMPLES: @@ -246,17 +283,13 @@ static int tps25990_write_word_data(struct i2c_client= *client, break; =20 case PMBUS_VIN_OV_FAULT_LIMIT: - value -=3D TPS25990_VIN_OVF_OFF; - value =3D DIV_ROUND_CLOSEST(((unsigned int)value) * TPS25990_VIN_OVF_DIV, - TPS25990_VIN_OVF_NUM); + value =3D tps25990_value_to_raw(client, TPS25990_VIN_OVF, value); value =3D clamp_val(value, 0, 0xf); ret =3D pmbus_write_word_data(client, page, reg, value); break; =20 case PMBUS_IIN_OC_FAULT_LIMIT: - value -=3D TPS25990_IIN_OCF_OFF; - value =3D DIV_ROUND_CLOSEST(((unsigned int)value) * TPS25990_IIN_OCF_DIV, - TPS25990_IIN_OCF_NUM); + value =3D tps25990_value_to_raw(client, TPS25990_IIN_OCF, value); value =3D clamp_val(value, 0, 0x3f); ret =3D pmbus_write_byte_data(client, page, TPS25990_VIREF, value); break; @@ -337,7 +370,16 @@ static const struct regulator_desc tps25990_reg_desc[]= =3D { }; #endif =20 -static const struct pmbus_driver_info tps25990_base_info =3D { +struct local_direct_value tps25590_local_info =3D { + .m[TPS25990_VIN_OVF] =3D 10163, + .b[TPS25990_VIN_OVF] =3D -30081, + .R[TPS25990_VIN_OVF] =3D -4, + .m[TPS25990_IIN_OCF] =3D 9538, + .b[TPS25990_IIN_OCF] =3D 0, + .R[TPS25990_IIN_OCF] =3D -6, +}; + +static struct pmbus_driver_info tps25990_base_info =3D { .pages =3D 1, .format[PSC_VOLTAGE_IN] =3D direct, .m[PSC_VOLTAGE_IN] =3D 5251, @@ -386,14 +428,19 @@ static const struct pmbus_driver_info tps25990_base_i= nfo =3D { #endif }; =20 +struct tps25990_data data_tps25990 =3D { + .info =3D &tps25990_base_info, + .info_local =3D &tps25590_local_info, +}; + static const struct i2c_device_id tps25990_i2c_id[] =3D { - { "tps25990" }, + { .name =3D "tps25990", .driver_data =3D (kernel_ulong_t)&data_tps25990 }, {} }; MODULE_DEVICE_TABLE(i2c, tps25990_i2c_id); =20 static const struct of_device_id tps25990_of_match[] =3D { - { .compatible =3D "ti,tps25990" }, + { .compatible =3D "ti,tps25990", .data =3D &data_tps25990 }, {} }; MODULE_DEVICE_TABLE(of, tps25990_of_match); @@ -401,23 +448,37 @@ MODULE_DEVICE_TABLE(of, tps25990_of_match); static int tps25990_probe(struct i2c_client *client) { struct device *dev =3D &client->dev; - struct pmbus_driver_info *info; + struct tps25990_data *data; u32 rimon =3D TPS25990_DEFAULT_RIMON; + struct pmbus_driver_info *info_get; + struct local_direct_value *info_local_get; int ret; =20 ret =3D device_property_read_u32(dev, "ti,rimon-micro-ohms", &rimon); if (ret < 0 && ret !=3D -EINVAL) return dev_err_probe(dev, ret, "failed to get rimon\n"); =20 - info =3D devm_kmemdup(dev, &tps25990_base_info, sizeof(*info), GFP_KERNEL= ); - if (!info) + data =3D (struct tps25990_data *)of_device_get_match_data(dev); + if (!data) + return -EOPNOTSUPP; + + info_get =3D data->info; + /* Make copy of pmbus_info and replace it to preserve original values */ + data->info =3D devm_kmemdup(dev, info_get, sizeof(*info_get), GFP_KERNEL); + if (!data->info) + return -ENOMEM; + + info_local_get =3D data->info_local; + /* Make copy of pmbus_info and replace it to preserve original values */ + data->info_local =3D devm_kmemdup(dev, info_local_get, sizeof(*info_local= _get), GFP_KERNEL); + if (!data->info_local) return -ENOMEM; =20 /* Adapt the current and power scale for each instance */ - tps25990_set_m(&info->m[PSC_CURRENT_IN], rimon); - tps25990_set_m(&info->m[PSC_POWER], rimon); + tps25990_set_m(&data->info->m[PSC_CURRENT_IN], rimon); + tps25990_set_m(&data->info->m[PSC_POWER], rimon); =20 - return pmbus_do_probe(client, info); + return pmbus_do_probe(client, data->info); } =20 static struct i2c_driver tps25990_driver =3D { --=20 2.34.1