From nobody Wed Jan 22 10:03:13 2025 Received: from m16.mail.163.com (m16.mail.163.com [117.135.210.2]) by smtp.subspace.kernel.org (Postfix) with ESMTP id EBE0923DE; Wed, 22 Jan 2025 01:30:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=117.135.210.2 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737509425; cv=none; b=eJVjku1j+SzAxdcXPkoKLdrDa6yCcB3MRckHV6ttTkHolhCFzvrfhohNu74gpxzqiv/A2It3nDV/s8277TfxHnySA+G9J9r7tZjqg8DedXp+d5vDD0rLffKHDGXqgV5YpzW1TQYxYAxWXujs8ufqWsg8fSJZp5d+gXAdN4DlF6M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737509425; c=relaxed/simple; bh=vrM9FCcSabp0iENUkhYWnk5ZCUCWJihsBrxkmgjFOz4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=axG2DyNLXyrKR1+muKZS3Va/5NtpEcCiSE8Lj2D+sD+vIPkTfBh0Tjucg+CyFb0D8M+6w8Jaj4NdNGXrU/YO0BGWnVqsq8C/NvnQjLIUduiX/08uOGXRYAFZ1IO0pHSDGW6tDyEZRPtDWajjTxPPoc3NDHtAjDnVGGgsqhrM55k= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com; spf=pass smtp.mailfrom=163.com; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.b=E73XVTtj; arc=none smtp.client-ip=117.135.210.2 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=163.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.b="E73XVTtj" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=From:Subject:Date:Message-ID:MIME-Version; bh=PPF2T NJs26XvPTra079pkEfMaeRetMCkRx0gBDwYafE=; b=E73XVTtj/2Gxb4NSFGrro E87HuK+6XLWNoYMDVSIE1Jyp2nNEQMbjgVnhwvSSFDfpO7HY3gWS3xxz3ltJHP5+ gp2S8fDBhPDaikPcL+uaQJ6p8uFjMXay7p0wTZrHtJmpjI1kvF7li+Zq6INIZOXc XIpnrQjQfJMwADYZi2iBlc= Received: from silergy-System-Product-Name.silergy.inc (unknown []) by gzga-smtp-mtada-g0-3 (Coremail) with SMTP id _____wDnl7MGSpBnDxF+HQ--.57201S5; Wed, 22 Jan 2025 09:29:46 +0800 (CST) From: Wenliang Yan To: linux@roeck-us.net, Jean Delvare , Rob Herring , Krzysztof Kozlowski , Conor Dooley Cc: Wenliang Yan , Jonathan Corbet , linux-hwmon@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v3 1/2] dt-bindings:Add SQ52206 to ina2xx devicetree bindings Date: Wed, 22 Jan 2025 09:29:39 +0800 Message-ID: <20250122012940.1005571-2-wenliang202407@163.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250122012940.1005571-1-wenliang202407@163.com> References: <20250122012940.1005571-1-wenliang202407@163.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 X-CM-TRANSID: _____wDnl7MGSpBnDxF+HQ--.57201S5 X-Coremail-Antispam: 1Uf129KBjvdXoW7XF1DJryxJF43ur4DJr4rAFb_yoWkuFbEgF WxAF4DXrZ8JFyFgr1qyw48Jr1ayws3Cr4kAw1UJFZYy3yavr90ga4kJw4kAF1xJFW3uFyr Zan5Wr45KrsrKjkaLaAFLSUrUUUUjb8apTn2vfkv8UJUUUU8Yxn0WfASr-VFAUDa7-sFnT 9fnUUvcSsGvfC2KfnxnUUI43ZEXa7sRCJPiUUUUUU== X-CM-SenderInfo: xzhqzxhdqjjiisuqlqqrwthudrp/1tbiNQTc02eQPfTHwQAAsG Content-Type: text/plain; charset="utf-8" Add the sq52206 compatible to the ina2xx.yaml Signed-off-by: Wenliang Yan --- Add the meaning of 'shunt-gain' in SQ52206. Documentation/devicetree/bindings/hwmon/ti,ina2xx.yaml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Documentation/devicetree/bindings/hwmon/ti,ina2xx.yaml b/Docum= entation/devicetree/bindings/hwmon/ti,ina2xx.yaml index 05a9cb36cd82..25e0b2f0b28d 100644 --- a/Documentation/devicetree/bindings/hwmon/ti,ina2xx.yaml +++ b/Documentation/devicetree/bindings/hwmon/ti,ina2xx.yaml @@ -20,6 +20,7 @@ description: | properties: compatible: enum: + - silergy,sq52206 - silergy,sy24655 - ti,ina209 - ti,ina219 @@ -58,6 +59,9 @@ properties: shunt voltage, and a value of 4 maps to ADCRANGE=3D0 such that a wid= er voltage range is used. =20 + For SQ52206,the shunt gain value 1 mapps to ADCRANGE=3D10/11, the va= lue 2 + mapps to ADCRANGE=3D01, and the value 4 mapps to ADCRANGE=3D00. + The default value is device dependent, and is defined by the reset v= alue of PGA/ADCRANGE in the respective configuration registers. $ref: /schemas/types.yaml#/definitions/uint32 --=20 2.43.0 From nobody Wed Jan 22 10:03:13 2025 Received: from m16.mail.163.com (m16.mail.163.com [220.197.31.3]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5188523DE; Wed, 22 Jan 2025 01:30:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=220.197.31.3 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737509432; cv=none; b=gW3VCJkysmWXTCoGiBtzV9w8xvR6TJeT7VljhkDmo0Ri8BzH7wg8PQbzXlm3Z0QdiAe4Jd5la1nzfBhf1n6z5c2fdMRfdsp/x43EXyYoFG9yMddhmGHwrtdHPsutTv8kgqm0EmQdXY1NAshbRQwwpu4Pryer/bcriJHA2OECCYs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737509432; c=relaxed/simple; bh=NP9M2Jxxc7KeLfIfMo1rn5aDpzRUi+W1ynMIWZ2sDyE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=NPk6YAIMdkiQ1PgXb5BYAnVysFNUvbz7dnXfP5JiSuPEo6L6RVVxIb76pgo64z9Yamu7FWICNqZ/+wChMsnf8zWU+EPyr0UBDTcs6JQ3IS0md39qRql6APFXeo9ciOLmsJXbiBVCT3vCjZG0/+KUgpxjqLvHG/WtTgfIyCvWnwc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com; spf=pass smtp.mailfrom=163.com; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.b=YWOyZhc1; arc=none smtp.client-ip=220.197.31.3 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=163.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.b="YWOyZhc1" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=From:Subject:Date:Message-ID:MIME-Version; bh=C2Fay HVx5TpbBfBvQxzTghRbtHOOt329IJ+Rhd4UxNI=; b=YWOyZhc1wCIehhlquTxHM DnED94pUVAvAjZ7DCyAHwNzQX3c7OdycYolfdNwvFmGWwzWDNEmro91L663S07Qt cV5upgf8tpmc74KBcqYscbpjXs/sPzZwhBQh4jVYpZl/8WkoY2CBD0HW+atdcoS1 TOjlGL/j4/5peWyBnLnpfY= Received: from silergy-System-Product-Name.silergy.inc (unknown []) by gzga-smtp-mtada-g0-3 (Coremail) with SMTP id _____wDnl7MGSpBnDxF+HQ--.57201S6; Wed, 22 Jan 2025 09:29:47 +0800 (CST) From: Wenliang Yan To: linux@roeck-us.net, Jean Delvare Cc: Wenliang Yan , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Jonathan Corbet , linux-hwmon@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v3 2/2] hwmon:(ina238)Add support for SQ52206 Date: Wed, 22 Jan 2025 09:29:40 +0800 Message-ID: <20250122012940.1005571-3-wenliang202407@163.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250122012940.1005571-1-wenliang202407@163.com> References: <20250122012940.1005571-1-wenliang202407@163.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 X-CM-TRANSID: _____wDnl7MGSpBnDxF+HQ--.57201S6 X-Coremail-Antispam: 1Uf129KBjvAXoW3uFyfWr4fCFW7uFW3Kr1rZwb_yoW8WFy3Go Wavrs3uw48Gw1fArWFkw1rCFW7Xr4jkr48ZF1UKrWDua4Iqrn8Ka4fZ34xWFyrZws0gFyx Wr1UGw1rXa1qy3W8n29KB7ZKAUJUUUU8529EdanIXcx71UUUUU7v73VFW2AGmfu7bjvjm3 AaLaJ3UbIYCTnIWIevJa73UjIFyTuYvjTRt8noUUUUU X-CM-SenderInfo: xzhqzxhdqjjiisuqlqqrwthudrp/1tbiJRLc02eQPVTQqAAAse Content-Type: text/plain; charset="utf-8" Add support for SQ52206 to the Ina238 driver. Add registers, add calculation formulas, increase compatibility, add compatibility programs for multiple chips. Signed-off-by: Wenliang Yan --- Incorporate four additional registers to the original register set of SQ52206 beyond INA238. The ADC measurement range of SQ52206 is divided into 1/2/4, so change the configuration of INA238_ADC_CONFIG. SQ52206's calculation of power read values is different from INA238.Add new value of BUS_VOLTAGE_LSB and DIE-TEMP_LSB for SQ52206. As a result of these changes, modify both the power and temperature read and write operations. Add new parameters in struct ina238_data to save the chip type and different configurations for each chip type, promoting program reusability. Due to the temperature reading of SQ52206 being a signed 16 bit value, while INA238 is a 12 bit value. So we changed the temperature reading function. Extract the chip initialization process into a separate function named ina238_init to facilitate adjustments for various chips. Add a corresponding compatible to the driver. Add a 40 bit data reading function to prepare for energy reading. Energy attributes are 5bytes wide, so modified the function for energy1_input to use u64. Add HWMON_P_INPUT_HIGHEST for power. Documentation/hwmon/ina238.rst | 15 +++ drivers/hwmon/ina238.c | 208 ++++++++++++++++++++++++++++----- 2 files changed, 191 insertions(+), 32 deletions(-) diff --git a/Documentation/hwmon/ina238.rst b/Documentation/hwmon/ina238.rst index d9f479984420..d1b93cf8627f 100644 --- a/Documentation/hwmon/ina238.rst +++ b/Documentation/hwmon/ina238.rst @@ -14,6 +14,12 @@ Supported chips: Datasheet: https://www.ti.com/lit/gpn/ina238 =20 + * Silergy SQ52206 + + Prefix: 'SQ52206' + + Addresses: I2C 0x40 - 0x4f + Author: Nathan Rossi =20 Description @@ -54,3 +60,12 @@ temp1_input Die temperature measurement (mC) temp1_max Maximum die temperature threshold (mC) temp1_max_alarm Maximum die temperature alarm =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D + +Additional sysfs entries for sq52206 +------------------------------------ + +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D +energy1_input Energy measurement (mJ) + +power1_input_highest Peak Power (uW) +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D diff --git a/drivers/hwmon/ina238.c b/drivers/hwmon/ina238.c index 2d9f12f68d50..74fdad2f92b9 100644 --- a/drivers/hwmon/ina238.c +++ b/drivers/hwmon/ina238.c @@ -21,11 +21,14 @@ #define INA238_CONFIG 0x0 #define INA238_ADC_CONFIG 0x1 #define INA238_SHUNT_CALIBRATION 0x2 +#define SQ52206_SHUNT_TEMPCO 0x3 #define INA238_SHUNT_VOLTAGE 0x4 #define INA238_BUS_VOLTAGE 0x5 #define INA238_DIE_TEMP 0x6 #define INA238_CURRENT 0x7 #define INA238_POWER 0x8 +#define SQ52206_ENERGY 0x9 +#define SQ52206_CHARGE 0xa #define INA238_DIAG_ALERT 0xb #define INA238_SHUNT_OVER_VOLTAGE 0xc #define INA238_SHUNT_UNDER_VOLTAGE 0xd @@ -33,9 +36,12 @@ #define INA238_BUS_UNDER_VOLTAGE 0xf #define INA238_TEMP_LIMIT 0x10 #define INA238_POWER_LIMIT 0x11 +#define SQ52206_POWER_PEAK 0x20 #define INA238_DEVICE_ID 0x3f /* not available on INA237 */ =20 #define INA238_CONFIG_ADCRANGE BIT(4) +#define SQ52206_CONFIG_ADCRANGE_HIGH BIT(4) +#define SQ52206_CONFIG_ADCRANGE_LOW BIT(3) =20 #define INA238_DIAG_ALERT_TMPOL BIT(7) #define INA238_DIAG_ALERT_SHNTOL BIT(6) @@ -44,12 +50,13 @@ #define INA238_DIAG_ALERT_BUSUL BIT(3) #define INA238_DIAG_ALERT_POL BIT(2) =20 -#define INA238_REGISTERS 0x11 +#define INA238_REGISTERS 0x20 =20 #define INA238_RSHUNT_DEFAULT 10000 /* uOhm */ =20 /* Default configuration of device on reset. */ #define INA238_CONFIG_DEFAULT 0 +#define SQ52206_CONFIG_DEFAULT 0x0005 /* 16 sample averaging, 1052us conversion time, continuous mode */ #define INA238_ADC_CONFIG_DEFAULT 0xfb6a /* Configure alerts to be based on averaged value (SLOWALERT) */ @@ -87,14 +94,19 @@ * shunt =3D 0x4000 / (819.2 * 10^6) / 0.001 =3D 20000 uOhms (with 1mA/ls= b) * * Current (mA) =3D register value * 20000 / rshunt / 4 * gain - * Power (W) =3D 0.2 * register value * 20000 / rshunt / 4 * gain + * Power (mW) =3D 0.2 * register value * 20000 / rshunt / 4 * gain + * (Specific for SQ52206) + * Power (mW) =3D 0.24 * register value * 20000 / rshunt / 4 * gain + * Energy (mJ) =3D 16 * 0.24 * register value * 20000 / rshunt / 4 * gain */ #define INA238_CALIBRATION_VALUE 16384 #define INA238_FIXED_SHUNT 20000 =20 #define INA238_SHUNT_VOLTAGE_LSB 5 /* 5 uV/lsb */ #define INA238_BUS_VOLTAGE_LSB 3125 /* 3.125 mV/lsb */ -#define INA238_DIE_TEMP_LSB 125 /* 125 mC/lsb */ +#define INA238_DIE_TEMP_LSB 1250000 /* 125 mC/lsb */ +#define SQ52206_BUS_VOLTAGE_LSB 3750 /* 3.75 mV/lsb */ +#define SQ52206_DIE_TEMP_LSB 78125 /* 7.8125 mC/lsb */ =20 static const struct regmap_config ina238_regmap_config =3D { .max_register =3D INA238_REGISTERS, @@ -102,7 +114,20 @@ static const struct regmap_config ina238_regmap_config= =3D { .val_bits =3D 16, }; =20 +enum ina238_ids { ina238, ina237, sq52206 }; + +struct ina238_config { + bool has_power_highest; /* chip detection power peak */ + bool has_energy; /* chip detection energy */ + u8 temp_shift; + u32 power_calculate_factor; /*fixed parameters for power calculate*/ + u16 config_default; + int bus_voltage_lsb; /* uV */ + int temp_lsb; /* mC */ +}; + struct ina238_data { + const struct ina238_config *config; struct i2c_client *client; struct mutex config_lock; struct regmap *regmap; @@ -110,6 +135,36 @@ struct ina238_data { int gain; }; =20 +static const struct ina238_config ina238_config[] =3D { + [ina238] =3D { + .has_energy =3D false, + .has_power_highest =3D false, + .temp_shift =3D 4, + .power_calculate_factor =3D 20, + .config_default =3D INA238_CONFIG_DEFAULT, + .bus_voltage_lsb =3D INA238_BUS_VOLTAGE_LSB, + .temp_lsb =3D INA238_DIE_TEMP_LSB, + }, + [ina237] =3D { + .has_energy =3D false, + .has_power_highest =3D false, + .temp_shift =3D 4, + .power_calculate_factor =3D 20, + .config_default =3D INA238_CONFIG_DEFAULT, + .bus_voltage_lsb =3D INA238_BUS_VOLTAGE_LSB, + .temp_lsb =3D INA238_DIE_TEMP_LSB, + }, + [sq52206] =3D { + .has_energy =3D true, + .has_power_highest =3D true, + .temp_shift =3D 0, + .power_calculate_factor =3D 24, + .config_default =3D SQ52206_CONFIG_DEFAULT, + .bus_voltage_lsb =3D SQ52206_BUS_VOLTAGE_LSB, + .temp_lsb =3D SQ52206_DIE_TEMP_LSB, + }, +}; + static int ina238_read_reg24(const struct i2c_client *client, u8 reg, u32 = *val) { u8 data[3]; @@ -126,6 +181,24 @@ static int ina238_read_reg24(const struct i2c_client *= client, u8 reg, u32 *val) return 0; } =20 +static int ina238_read_reg40(const struct i2c_client *client, u8 reg, u64 = *val) +{ + u8 data[5]; + u32 low; + int err; + + /* 40-bit register read */ + err =3D i2c_smbus_read_i2c_block_data(client, reg, 5, data); + if (err < 0) + return err; + if (err !=3D 5) + return -EIO; + low =3D (data[1] << 24) | (data[2] << 16) | (data[3] << 8) | data[4]; + *val =3D ((long long)data[0] << 32) | low; + + return 0; +} + static int ina238_read_in(struct device *dev, u32 attr, int channel, long *val) { @@ -197,10 +270,10 @@ static int ina238_read_in(struct device *dev, u32 att= r, int channel, regval =3D (s16)regval; if (channel =3D=3D 0) /* gain of 1 -> LSB / 4 */ - *val =3D (regval * INA238_SHUNT_VOLTAGE_LSB) / - (1000 * (4 - data->gain + 1)); + *val =3D (regval * INA238_SHUNT_VOLTAGE_LSB) * + data->gain / (1000 * 4); else - *val =3D (regval * INA238_BUS_VOLTAGE_LSB) / 1000; + *val =3D (regval * data->config->bus_voltage_lsb) / 1000; break; case hwmon_in_max_alarm: case hwmon_in_min_alarm: @@ -225,8 +298,8 @@ static int ina238_write_in(struct device *dev, u32 attr= , int channel, case 0: /* signed value, clamp to max range +/-163 mV */ regval =3D clamp_val(val, -163, 163); - regval =3D (regval * 1000 * (4 - data->gain + 1)) / - INA238_SHUNT_VOLTAGE_LSB; + regval =3D (regval * 1000 * 4) / + INA238_SHUNT_VOLTAGE_LSB * data->gain; regval =3D clamp_val(regval, S16_MIN, S16_MAX); =20 switch (attr) { @@ -242,7 +315,7 @@ static int ina238_write_in(struct device *dev, u32 attr= , int channel, case 1: /* signed value, positive values only. Clamp to max 102.396 V */ regval =3D clamp_val(val, 0, 102396); - regval =3D (regval * 1000) / INA238_BUS_VOLTAGE_LSB; + regval =3D (regval * 1000) / data->config->bus_voltage_lsb; regval =3D clamp_val(regval, 0, S16_MAX); =20 switch (attr) { @@ -297,8 +370,19 @@ static int ina238_read_power(struct device *dev, u32 a= ttr, long *val) return err; =20 /* Fixed 1mA lsb, scaled by 1000000 to have result in uW */ - power =3D div_u64(regval * 1000ULL * INA238_FIXED_SHUNT * - data->gain, 20 * data->rshunt); + power =3D div_u64(regval * data->config->power_calculate_factor * 50ULL * + INA238_FIXED_SHUNT * data->gain, 20 * data->rshunt); + /* Clamp value to maximum value of long */ + *val =3D clamp_val(power, 0, LONG_MAX); + break; + case hwmon_power_input_highest: + err =3D ina238_read_reg24(data->client, SQ52206_POWER_PEAK, ®val); + if (err) + return err; + + /* Fixed 1mA lsb, scaled by 1000000 to have result in uW */ + power =3D div_u64(regval * data->config->power_calculate_factor * 50ULL * + INA238_FIXED_SHUNT * data->gain, 20 * data->rshunt); /* Clamp value to maximum value of long */ *val =3D clamp_val(power, 0, LONG_MAX); break; @@ -311,8 +395,8 @@ static int ina238_read_power(struct device *dev, u32 at= tr, long *val) * Truncated 24-bit compare register, lower 8-bits are * truncated. Same conversion to/from uW as POWER register. */ - power =3D div_u64((regval << 8) * 1000ULL * INA238_FIXED_SHUNT * - data->gain, 20 * data->rshunt); + power =3D div_u64((regval << 8) * data->config->power_calculate_factor * + 50ULL * INA238_FIXED_SHUNT * data->gain, 20 * data->rshunt); /* Clamp value to maximum value of long */ *val =3D clamp_val(power, 0, LONG_MAX); break; @@ -344,8 +428,8 @@ static int ina238_write_power(struct device *dev, u32 a= ttr, long val) * register. */ regval =3D clamp_val(val, 0, LONG_MAX); - regval =3D div_u64(val * 20ULL * data->rshunt, - 1000ULL * INA238_FIXED_SHUNT * data->gain); + regval =3D div_u64(val * data->config->power_calculate_factor * data->rsh= unt, + 1000ULL * INA238_FIXED_SHUNT * data->gain); regval =3D clamp_val(regval >> 8, 0, U16_MAX); =20 return regmap_write(data->regmap, INA238_POWER_LIMIT, regval); @@ -362,17 +446,17 @@ static int ina238_read_temp(struct device *dev, u32 a= ttr, long *val) err =3D regmap_read(data->regmap, INA238_DIE_TEMP, ®val); if (err) return err; - - /* Signed, bits 15-4 of register, result in mC */ - *val =3D ((s16)regval >> 4) * INA238_DIE_TEMP_LSB; + /* Signed, result in mC */ + *val =3D div_s64(((s16)regval >> data->config->temp_shift) * + data->config->temp_lsb, 10000); break; case hwmon_temp_max: err =3D regmap_read(data->regmap, INA238_TEMP_LIMIT, ®val); if (err) return err; - - /* Signed, bits 15-4 of register, result in mC */ - *val =3D ((s16)regval >> 4) * INA238_DIE_TEMP_LSB; + /* Signed, result in mC */ + *val =3D div_s64(((s16)regval >> data->config->temp_shift) * + data->config->temp_lsb, 10000); break; case hwmon_temp_max_alarm: err =3D regmap_read(data->regmap, INA238_DIAG_ALERT, ®val); @@ -396,13 +480,31 @@ static int ina238_write_temp(struct device *dev, u32 = attr, long val) if (attr !=3D hwmon_temp_max) return -EOPNOTSUPP; =20 - /* Signed, bits 15-4 of register */ - regval =3D (val / INA238_DIE_TEMP_LSB) << 4; - regval =3D clamp_val(regval, S16_MIN, S16_MAX) & 0xfff0; + /* Signed */ + regval =3D div_u64(val*10000, data->config->temp_lsb) << data->config->te= mp_shift; + regval =3D clamp_val(regval, S16_MIN, S16_MAX) & (0xffff << data->config-= >temp_shift); =20 return regmap_write(data->regmap, INA238_TEMP_LIMIT, regval); } =20 +static ssize_t energy1_input_show(struct device *dev, + struct device_attribute *da, char *buf) +{ + struct ina238_data *data =3D dev_get_drvdata(dev); + int ret; + u64 val; + + ret =3D ina238_read_reg40(data->client, SQ52206_ENERGY, &val); + if (ret) + return ret; + + /* result in microJoule */ + val =3D div_u64(val * 96 * INA238_FIXED_SHUNT * data->gain, + data->rshunt * 100); + + return sprintf(buf, "%llu\n", val); +} + static int ina238_read(struct device *dev, enum hwmon_sensor_types type, u32 attr, int channel, long *val) { @@ -452,6 +554,9 @@ static umode_t ina238_is_visible(const void *drvdata, enum hwmon_sensor_types type, u32 attr, int channel) { + const struct ina238_data *data =3D drvdata; + bool has_power_highest =3D data->config->has_power_highest; + switch (type) { case hwmon_in: switch (attr) { @@ -479,6 +584,10 @@ static umode_t ina238_is_visible(const void *drvdata, return 0444; case hwmon_power_max: return 0644; + case hwmon_power_input_highest: + if (has_power_highest) + return 0444; + return 0; default: return 0; } @@ -512,7 +621,8 @@ static const struct hwmon_channel_info * const ina238_i= nfo[] =3D { HWMON_C_INPUT), HWMON_CHANNEL_INFO(power, /* 0: power */ - HWMON_P_INPUT | HWMON_P_MAX | HWMON_P_MAX_ALARM), + HWMON_P_INPUT | HWMON_P_MAX | + HWMON_P_MAX_ALARM | HWMON_P_INPUT_HIGHEST), HWMON_CHANNEL_INFO(temp, /* 0: die temperature */ HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MAX_ALARM), @@ -530,6 +640,15 @@ static const struct hwmon_chip_info ina238_chip_info = =3D { .info =3D ina238_info, }; =20 +/* energy attributes are 5bytes wide so we need u64 */ +static DEVICE_ATTR_RO(energy1_input); + +static struct attribute *ina238_attrs[] =3D { + &dev_attr_energy1_input.attr, + NULL, +}; +ATTRIBUTE_GROUPS(ina238); + static int ina238_probe(struct i2c_client *client) { struct ina2xx_platform_data *pdata =3D dev_get_platdata(&client->dev); @@ -537,13 +656,19 @@ static int ina238_probe(struct i2c_client *client) struct device *hwmon_dev; struct ina238_data *data; int config; + enum ina238_ids chip; int ret; =20 + chip =3D (uintptr_t)i2c_get_match_data(client); + data =3D devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); if (!data) return -ENOMEM; =20 data->client =3D client; + /* set the device type */ + data->config =3D &ina238_config[chip]; + mutex_init(&data->config_lock); =20 data->regmap =3D devm_regmap_init_i2c(client, &ina238_regmap_config); @@ -564,14 +689,19 @@ static int ina238_probe(struct i2c_client *client) /* load shunt gain value */ if (device_property_read_u32(dev, "ti,shunt-gain", &data->gain) < 0) data->gain =3D 4; /* Default of ADCRANGE =3D 0 */ - if (data->gain !=3D 1 && data->gain !=3D 4) { + if (data->gain !=3D 1 && data->gain !=3D 2 && data->gain !=3D 4) { dev_err(dev, "invalid shunt gain value %u\n", data->gain); return -EINVAL; } =20 /* Setup CONFIG register */ - config =3D INA238_CONFIG_DEFAULT; - if (data->gain =3D=3D 1) + config =3D data->config->config_default; + if (chip =3D=3D sq52206) { + if (data->gain =3D=3D 1) + config |=3D SQ52206_CONFIG_ADCRANGE_HIGH; /* ADCRANGE =3D 10/11 is /1 */ + else if (data->gain =3D=3D 2) + config |=3D SQ52206_CONFIG_ADCRANGE_LOW; /* ADCRANGE =3D 01 is /2 */ + } else if (data->gain =3D=3D 1) config |=3D INA238_CONFIG_ADCRANGE; /* ADCRANGE =3D 1 is /1 */ ret =3D regmap_write(data->regmap, INA238_CONFIG, config); if (ret < 0) { @@ -605,7 +735,8 @@ static int ina238_probe(struct i2c_client *client) =20 hwmon_dev =3D devm_hwmon_device_register_with_info(dev, client->name, dat= a, &ina238_chip_info, - NULL); + data->config->has_energy ? + ina238_groups : NULL); if (IS_ERR(hwmon_dev)) return PTR_ERR(hwmon_dev); =20 @@ -616,14 +747,26 @@ static int ina238_probe(struct i2c_client *client) } =20 static const struct i2c_device_id ina238_id[] =3D { - { "ina238" }, + { "ina237", ina237 }, + { "ina238", ina238 }, + { "sq52206", sq52206 }, { } }; MODULE_DEVICE_TABLE(i2c, ina238_id); =20 static const struct of_device_id __maybe_unused ina238_of_match[] =3D { - { .compatible =3D "ti,ina237" }, - { .compatible =3D "ti,ina238" }, + { + .compatible =3D "silergy,sq52206", + .data =3D (void *)sq52206 + }, + { + .compatible =3D "ti,ina237", + .data =3D (void *)ina237 + }, + { + .compatible =3D "ti,ina238", + .data =3D (void *)ina238 + }, { }, }; MODULE_DEVICE_TABLE(of, ina238_of_match); @@ -642,3 +785,4 @@ module_i2c_driver(ina238_driver); MODULE_AUTHOR("Nathan Rossi "); MODULE_DESCRIPTION("ina238 driver"); MODULE_LICENSE("GPL"); + --=20 2.43.0