Received: by 10.223.164.202 with SMTP id h10csp310936wrb; Wed, 22 Nov 2017 07:34:05 -0800 (PST) X-Google-Smtp-Source: AGs4zMYmy+lZnNV6/HF0dHvcmwOaV7tJt68h+NWHhCPedM4V4M2U5dP2tCdvw4naOMNDd8N6JIYG X-Received: by 10.84.246.201 with SMTP id j9mr9952970plt.333.1511364845704; Wed, 22 Nov 2017 07:34:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1511364845; cv=none; d=google.com; s=arc-20160816; b=mRwYvI5pgE7a0eSCCXEmBRlMVIgkSrAweN0W9SfuSKYEpavoqbFhoUkAU32aN2NyCU HHJcCRFHelXMPJANB4Qklcsc4ok+aelRNlgffAI/IyzMy+mbYAQdvHQ9nrPb40lyLP2h G1TcwkSlGW8C1K5M8bhXhe18+tG8stJM9Aw95h6y/QrePx1Ik9iVVW8eL7gDQvVmOzpP Czy85Snx2tQL6EG8eW/283XZ3tsfdqgVnGg41UzOMa7v6gn+AChuL/hFTeUBJ7UBjBQN zmK5DgTS+opRAsKQ1qPY/KsCwHdkH++ytdHt17jPIn9VM2CwRpB8QWWYUncI5LnC4Vxa uR3w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:cms-type:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature:dkim-filter :arc-authentication-results; bh=YNBNbZJvjdDlB3mM1+hP4VpXq+8sHov6oEha1wqFWRI=; b=CkhYeUCOhrbQm2aqlitWXAZ9Ktlo94ioemWOP7V6R/OF6MBcLXaluffteedE4BpH5d xfryArdwCsA6bQGFkb/wckO11wxsGPvaKvYOey3l4KBbUfMC3+9Td5Uffk7efmWyQIzg 5LuAoDSka1s+cp1+7sWxmWjwulycEes6erq3lopKP9KgDwL2A3Sz+LkR1uWBpohl3ESu st7hz61Pdtqw1Q8k1K/+Xn9nPr9n27IWBnOvyu0/HZ7jhfkubj99pH6JFUjPMsNdV/Vi BeG2P7C/LgAKeoUmWXiAXfI/1avFs4KjsEHGk1ilIIog6GUbW31f3LSESi4D4qYvEgVU wH4g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@samsung.com header.s=mail20170921 header.b=sIcLJlHK; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=samsung.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 76si7470341pfi.64.2017.11.22.07.33.54; Wed, 22 Nov 2017 07:34:05 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@samsung.com header.s=mail20170921 header.b=sIcLJlHK; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=samsung.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751822AbdKVPcx (ORCPT + 77 others); Wed, 22 Nov 2017 10:32:53 -0500 Received: from mailout1.w1.samsung.com ([210.118.77.11]:51256 "EHLO mailout1.w1.samsung.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751681AbdKVPct (ORCPT ); Wed, 22 Nov 2017 10:32:49 -0500 Received: from eucas1p2.samsung.com (unknown [182.198.249.207]) by mailout1.w1.samsung.com (KnoxPortal) with ESMTP id 20171122153247euoutp01bdba4d446a24862e19604ae2eb6bbbd3~5cj_zPbP81295312953euoutp01M; Wed, 22 Nov 2017 15:32:47 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 mailout1.w1.samsung.com 20171122153247euoutp01bdba4d446a24862e19604ae2eb6bbbd3~5cj_zPbP81295312953euoutp01M DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=samsung.com; s=mail20170921; t=1511364767; bh=YNBNbZJvjdDlB3mM1+hP4VpXq+8sHov6oEha1wqFWRI=; h=From:To:Cc:Subject:Date:In-reply-to:References:From; b=sIcLJlHKiWWJ99mkyHniaHEqjNX0ZxPw163F/waO1zLx5GCNS/BH9fGf6dmSZVHWB DARZ7KMm1Tc/KIvXl06cJduQYiNmuiTULb6Ye/al+pGudtx0XmauyWJDiwqz3Vkila m3y4wt06LXe6OXp4xd8i7+bHh1xvyfs7YGvcQcOE= Received: from eusmges4.samsung.com (unknown [203.254.199.244]) by eucas1p2.samsung.com (KnoxPortal) with ESMTP id 20171122153247eucas1p20406402a68f09928d45e5bef93347646~5cj_E3jcQ2791927919eucas1p2u; Wed, 22 Nov 2017 15:32:47 +0000 (GMT) Received: from eucas1p1.samsung.com ( [182.198.249.206]) by eusmges4.samsung.com (EUCPMTA) with SMTP id A4.0D.12944.E98951A5; Wed, 22 Nov 2017 15:32:46 +0000 (GMT) Received: from eusmgms2.samsung.com (unknown [182.198.249.180]) by eucas1p2.samsung.com (KnoxPortal) with ESMTP id 20171122153246eucas1p20832152c270805d20343e40287402e0d~5cj9aOduj2891728917eucas1p2z; Wed, 22 Nov 2017 15:32:46 +0000 (GMT) X-AuditID: cbfec7f4-f79ab6d000003290-4f-5a15989e64c8 Received: from eusync1.samsung.com ( [203.254.199.211]) by eusmgms2.samsung.com (EUCPMTA) with SMTP id 7F.44.20118.E98951A5; Wed, 22 Nov 2017 15:32:46 +0000 (GMT) Received: from AMDC2075.DIGITAL.local ([106.120.51.25]) by eusync1.samsung.com (Oracle Communications Messaging Server 7.0.5.31.0 64bit (built May 5 2014)) with ESMTPA id <0OZT00FB7SICQ510@eusync1.samsung.com>; Wed, 22 Nov 2017 15:32:46 +0000 (GMT) From: Maciej Purski To: Jonathan Cameron , Stefan Bruens Cc: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hwmon@vger.kernel.org, Javier Martinez Canillas , Peter Meerwald-Stadler , Lars-Peter Clausen , Hartmut Knaack , Jean Delvare , Guenter Roeck , Marek Szyprowski , Bartlomiej Zolnierkiewicz , Maciej Purski Subject: [PATCH 1/2] iio: adc: ina2xx: Make calibration register value fixed Date: Wed, 22 Nov 2017 16:32:14 +0100 Message-id: <1511364735-16818-2-git-send-email-m.purski@samsung.com> X-Mailer: git-send-email 2.7.4 In-reply-to: <1511364735-16818-1-git-send-email-m.purski@samsung.com> X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFvrMIsWRmVeSWpSXmKPExsWy7djPc7rzZohGGbxqMLLYOGM9q8Wbt2uY LM5OCLR40LSKyWLX/zfMFksmz2e1aH+9ldFi3pF3LBaXd81hs3iy8AyTxYKXt1gs1h65y27x e9cxdovGU3NYHfg8PnyM89i0qpPNY8mbQ6weW/rvsnucbz7C6LHzewOQde8tm0ffllWMHuu3 XGXx+LxJLoArissmJTUnsyy1SN8ugSvj9/ITLAVb1Cvm7PjF2MC4Q76LkYNDQsBE4tJLoS5G TiBTTOLCvfVsXYxcHEICSxklDjeuZANJCAl8ZpSY1hEKUWQisevQE2aIomWMEp93foLq+M8o MWHWdnaQqWwCWhJr2uNBGkQEwiU+vm9lBKlhFrjFLPFr+2JmkISwgI/EoY0NLCA2i4CqxP4X nWC9vAIuEo9eJEAsk5O4ea4TrJxTwFWiqWMSC8gcCYHnbBL971ezQRS5SLzs3c0IYQtLvDq+ hR3ClpG4PLmbBcKulrj4dRdUfY1E4+0NUDXWEp8nbQFbwCzAJzFp23RmSKjwSnS0QUPFQ+LU /idQYxwlHqz6ygRSIiQwg1FiC9sERukFjAyrGEVSS4tz01OLTfSKE3OLS/PS9ZLzczcxApPC 6X/Hv+xgXHzM6hCjAAejEg/vjCSRKCHWxLLiytxDjBIczEoivJO6RaOEeFMSK6tSi/Lji0pz UosPMUpzsCiJ89pGtUUKCaQnlqRmp6YWpBbBZJk4OKUaGAW8eRVfRCt9MOqxXS32QDX0G+cj lXiBbfuv9y97Gmt0sThsf+mLp0UbGrzSdvj+2B7bp7NF9nn1D9eHn0Qm9RzmjP7b51Hw/9V9 UZf9fk63dPNcpi5dcdXt/kp20SXyJRc+/yuOm1nLc7r/gJFlhEdi5m2eisKykr4AzdXa72o5 rpvELNpjocRSnJFoqMVcVJwIABssFdAGAwAA X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFjrDLMWRmVeSWpSXmKPExsVy+t/xy7rzZohGGRzdIWqxccZ6Vos3b9cw WZydEGjxoGkVk8Wu/2+YLZZMns9q0f56K6PFvCPvWCwu75rDZvFk4RkmiwUvb7FYrD1yl93i 965j7BaNp+awOvB5fPgY57FpVSebx5I3h1g9tvTfZfc433yE0WPn9wYg695bNo++LasYPdZv ucri8XmTXABXFJdNSmpOZllqkb5dAlfG7+UnWAq2qFfM2fGLsYFxh3wXIyeHhICJxK5DT5gh bDGJC/fWs3UxcnEICSxhlFj/6CorhNPIJLH18232LkYODjYBLYk17fEgDSIC4RJL33xgBqlh FrjHLPHt8GZWkISwgI/EoY0NLCA2i4CqxP4XnWC9vAIuEo9eJEAsk5O4ea4TbDGngKtEU8ck sHIhoJKjl38yT2DkXcDIsIpRJLW0ODc9t9hIrzgxt7g0L10vOT93EyMwjLcd+7llB2PXu+BD jAIcjEo8vDOSRKKEWBPLiitzDzFKcDArifBO6haNEuJNSaysSi3Kjy8qzUktPsQozcGiJM7b u2d1pJBAemJJanZqakFqEUyWiYNTqoFx9q8lkxStd6ycO/kIb/TMU6kr2HyKpsXu2hfpOWPL CtY2Y1bBnDdqN1NTzj11qSo9d+O16iHdtdcDMpbe3fC+ytDX9ellM8uDk/bw3ti34CBXe/yH zMPtenq1DLNP25v8WO3F7mmbteTAfZYzh625dJc39knMVJXNVVbucdxutiPybYfb60lTlViK MxINtZiLihMBndkKZl8CAAA= X-CMS-MailID: 20171122153246eucas1p20832152c270805d20343e40287402e0d X-Msg-Generator: CA CMS-TYPE: 201P X-CMS-RootMailID: 20171122153246eucas1p20832152c270805d20343e40287402e0d X-RootMTR: 20171122153246eucas1p20832152c270805d20343e40287402e0d References: <1511364735-16818-1-git-send-email-m.purski@samsung.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Calibration register is used for calculating current register in hardware according to datasheet: current = shunt_volt * calib_register / 2048 (ina 226) current = shunt_volt * calib_register / 4096 (ina 219) Fix calib_register value to 2048 for ina226 and 4096 for ina 219 in order to avoid truncation error and provide best precision allowed by shunt_voltage measurement. Make current scale value follow changes of shunt_resistor from sysfs as calib_register value is now fixed. Power_lsb value should also follow shunt_resistor changes as stated in datasheet: power_lsb = 25 * current_lsb (ina 226) power_lsb = 20 * current_lsb (ina 219) Signed-off-by: Maciej Purski --- drivers/iio/adc/ina2xx-adc.c | 59 ++++++++++++++++++++++++-------------------- 1 file changed, 32 insertions(+), 27 deletions(-) diff --git a/drivers/iio/adc/ina2xx-adc.c b/drivers/iio/adc/ina2xx-adc.c index 84a4387..0f25087 100644 --- a/drivers/iio/adc/ina2xx-adc.c +++ b/drivers/iio/adc/ina2xx-adc.c @@ -110,11 +110,11 @@ enum ina2xx_ids { ina219, ina226 }; struct ina2xx_config { u16 config_default; - int calibration_factor; + int calibration_value; int shunt_div; int bus_voltage_shift; int bus_voltage_lsb; /* uV */ - int power_lsb; /* uW */ + int power_lsb_factor; enum ina2xx_ids chip_id; }; @@ -124,6 +124,8 @@ struct ina2xx_chip_info { const struct ina2xx_config *config; struct mutex state_lock; unsigned int shunt_resistor_uohm; + unsigned int current_lsb_uA; + unsigned int power_lsb_uW; int avg; int int_time_vbus; /* Bus voltage integration time uS */ int int_time_vshunt; /* Shunt voltage integration time uS */ @@ -133,20 +135,20 @@ struct ina2xx_chip_info { static const struct ina2xx_config ina2xx_config[] = { [ina219] = { .config_default = INA219_CONFIG_DEFAULT, - .calibration_factor = 40960000, + .calibration_value = 4096, .shunt_div = 100, .bus_voltage_shift = 3, .bus_voltage_lsb = 4000, - .power_lsb = 20000, + .power_lsb_factor = 20, .chip_id = ina219, }, [ina226] = { .config_default = INA226_CONFIG_DEFAULT, - .calibration_factor = 5120000, + .calibration_value = 2048, .shunt_div = 400, .bus_voltage_shift = 0, .bus_voltage_lsb = 1250, - .power_lsb = 25000, + .power_lsb_factor = 25, .chip_id = ina226, }, }; @@ -210,14 +212,15 @@ static int ina2xx_read_raw(struct iio_dev *indio_dev, case INA2XX_POWER: /* processed (mW) = raw*lsb (uW) / 1000 */ - *val = chip->config->power_lsb; + *val = chip->power_lsb_uW; *val2 = 1000; return IIO_VAL_FRACTIONAL; case INA2XX_CURRENT: - /* processed (mA) = raw (mA) */ - *val = 1; - return IIO_VAL_INT; + /* processed (mA) = raw*lsb (uA) / 1000 */ + *val = chip->current_lsb_uA; + *val2 = 1000; + return IIO_VAL_FRACTIONAL; } } @@ -434,28 +437,35 @@ static ssize_t ina2xx_allow_async_readout_store(struct device *dev, } /* - * Set current LSB to 1mA, shunt is in uOhms - * (equation 13 in datasheet). We hardcode a Current_LSB - * of 1.0 x10-3. The only remaining parameter is RShunt. - * There is no need to expose the CALIBRATION register - * to the user for now. But we need to reset this register - * if the user updates RShunt after driver init, e.g upon - * reading an EEPROM/Probe-type value. + * Calibration register is set to the best value, which eliminates + * truncation errors on calculating current register in hardware. + * According to datasheet (eq. 3) the best values are 2048 for + * ina226 and 4096 for ina219. They are hardcoded as calibration_value. */ static int ina2xx_set_calibration(struct ina2xx_chip_info *chip) { - u16 regval = DIV_ROUND_CLOSEST(chip->config->calibration_factor, - chip->shunt_resistor_uohm); - - return regmap_write(chip->regmap, INA2XX_CALIBRATION, regval); + return regmap_write(chip->regmap, INA2XX_CALIBRATION, + chip->config->calibration_value); } +/* + * In order to keep calibration register value fixed, the product + * of current_lsb and shunt_resistor should also be fixed and equal + * to shunt_voltage_lsb = 1 / shunt_div multiplied by 10^9 in order + * to keep the scale. + */ static int set_shunt_resistor(struct ina2xx_chip_info *chip, unsigned int val) { - if (val <= 0 || val > chip->config->calibration_factor) + unsigned int dividend = DIV_ROUND_CLOSEST(1000000000, + chip->config->shunt_div); + + if (val <= 0 || val > dividend) return -EINVAL; chip->shunt_resistor_uohm = val; + chip->current_lsb_uA = DIV_ROUND_CLOSEST(dividend, val); + chip->power_lsb_uW = chip->config->power_lsb_factor * + chip->current_lsb_uA; return 0; } @@ -485,11 +495,6 @@ static ssize_t ina2xx_shunt_resistor_store(struct device *dev, if (ret) return ret; - /* Update the Calibration register */ - ret = ina2xx_set_calibration(chip); - if (ret) - return ret; - return len; } -- 2.7.4 From 1584810271485542217@xxx Wed Nov 22 23:20:52 +0000 2017 X-GM-THRID: 1584810271485542217 X-Gmail-Labels: Inbox,Category Forums,HistoricalUnread