From patchwork Wed Nov 16 13:53:27 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Tom Levens X-Patchwork-Id: 9432233 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 8B54B60469 for ; Wed, 16 Nov 2016 17:26:33 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7A94328FD3 for ; Wed, 16 Nov 2016 17:26:33 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 6E6A128FD6; Wed, 16 Nov 2016 17:26:33 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9D2F028FD3 for ; Wed, 16 Nov 2016 17:26:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752834AbcKPR0b (ORCPT ); Wed, 16 Nov 2016 12:26:31 -0500 Received: from mail-db5eur01on0055.outbound.protection.outlook.com ([104.47.2.55]:29568 "EHLO EUR01-DB5-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1752275AbcKPR03 (ORCPT ); Wed, 16 Nov 2016 12:26:29 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cern.onmicrosoft.com; s=selector1-cern-ch; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=hBv4/xRdCt2H4+4LDzCUscFlUTIJGiwRWEMgcF+YOOE=; b=KWlZ7VUsmo/m2AzkebYdlZQz8Ul5ECXK6e4T4nhUeFAierOlchydwlbbsIVox28X7ibG6igqvTdh1gZ+3Dn+7RoriPrByS/K26v4FRn7F2b0XvQQweQrojP2MJnrqQQmVsfwJoL4V9N3pRwodAC8czl5uoO5hhG9+fn39kzXzMo= Received: from HE1PR0601CA0007.eurprd06.prod.outlook.com (10.166.117.17) by HE1PR06MB1162.eurprd06.prod.outlook.com (10.162.252.156) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P384) id 15.1.734.8; Wed, 16 Nov 2016 13:53:45 +0000 Received: from DB3FFO11FD048.protection.gbl (2a01:111:f400:7e04::168) by HE1PR0601CA0007.outlook.office365.com (2a01:111:e400:c513::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P384) id 15.1.734.8 via Frontend Transport; Wed, 16 Nov 2016 13:53:45 +0000 Authentication-Results: spf=none (sender IP is 188.184.36.16) smtp.mailfrom=mail.cern.ch; vger.kernel.org; dkim=none (message not signed) header.d=none; vger.kernel.org; dmarc=none action=none header.from=cern.ch; Received-SPF: None (protection.outlook.com: mail.cern.ch does not designate permitted sender hosts) Received: from CERNMX14.cern.ch (188.184.36.16) by DB3FFO11FD048.mail.protection.outlook.com (10.47.217.79) with Microsoft SMTP Server (version=TLS1_0, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA_P384) id 15.1.721.5 via Frontend Transport; Wed, 16 Nov 2016 13:53:44 +0000 X-IncomingTopHeaderMarker: OriginalChecksum:; UpperCasedChecksum:; SizeAsReceived:968; Count:13 Received: from pcbe13573-vm.dyndns.cern.ch (128.141.154.157) by cernmxlb4.cern.ch (188.184.36.24) with Microsoft SMTP Server (TLS) id 14.3.319.2; Wed, 16 Nov 2016 14:53:43 +0100 Received: from pcbe13573-vm.dyndns.cern.ch (localhost [127.0.0.1]) by pcbe13573-vm.dyndns.cern.ch (8.14.4/8.14.4) with ESMTP id uAGDrh8V003953; Wed, 16 Nov 2016 14:53:43 +0100 Received: (from tlevens@localhost) by pcbe13573-vm.dyndns.cern.ch (8.14.4/8.14.4/Submit) id uAGDrgDR003952; Wed, 16 Nov 2016 14:53:42 +0100 From: Tom Levens To: , CC: , , Subject: [PATCH] hwmon: ltc2990: support all measurement modes Date: Wed, 16 Nov 2016 14:53:27 +0100 Message-ID: <1479304407-3922-1-git-send-email-tom.levens@cern.ch> X-Mailer: git-send-email 1.7.1 MIME-Version: 1.0 X-IncomingHeaderCount: 13 X-EOPAttributedMessage: 0 X-Forefront-Antispam-Report: CIP:188.184.36.16; IPV:NLI; CTRY:CH; EFV:NLI; SFV:NSPM; SFS:(10009020)(6009001)(7916002)(2980300002)(428002)(249900001)(199003)(189002)(106466001)(47776003)(1720100001)(2906002)(50226002)(7846002)(246002)(356003)(50986999)(5660300001)(42186005)(6666003)(4326007)(8936002)(189998001)(87936001)(86362001)(626004)(305945005)(5003940100001)(7636002)(48376002)(50466002)(8676002)(92566002)(5001770100001)(74482002)(36756003)(33646002); DIR:OUT; SFP:1101; SCL:1; SRVR:HE1PR06MB1162; H:CERNMX14.cern.ch; FPR:; SPF:None; PTR:cernmx14.cern.ch; A:1; MX:1; LANG:en; X-Microsoft-Exchange-Diagnostics: 1; DB3FFO11FD048; 1:81f7ooZOJhVl+iiiP08hzcevboWVnNzK1jd5JNL6HkdmNtTUoKHIhjpVxOV27rqOUZOa+1CWKTkRS7WBwv9kWpls3cTUURKbrwWjOdJmCDE6G8TdxPSz6Jonuqh0OeQvEy+mYiQQLK/vT6j+WYfCGoEkI6eyYXKuxbiaffrp5zAFIIgl1WAQZ1J41AI6uIMZ430KUkcnMKUn8Z5mYZkL2soqJdLImqc3riJEtigUq+Z3AgO2Rv/Uo/ocFbT8S/SSFKxW8W6yGZCZiE850L9CX6l9QBKhpXlpJ4uTc0t7cgBQdKPm8dITK6UpXqo10xMk3tCtqupqIP5Y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Zx6xOb5NQ2no6wtsq6qhGyw5NY5oURjwhN1clZuDW/S0XEdyJh4mnZJ3OetjVNYThi7Y2dnY6+N2stD1XqV6qi1nkJy4ILQv9sq93RpiRpaKx8u7Jazq/WqM9+5fOaftFJqEA== X-MS-Office365-Filtering-Correlation-Id: a82f03a7-57a3-4292-c1c8-08d40e27fb28 X-Microsoft-Antispam: UriScan:; BCL:0; PCL:0; RULEID:(22001); SRVR:HE1PR06MB1162; X-Microsoft-Exchange-Diagnostics: 1; HE1PR06MB1162; 31:HP0FzJnDPKk6pOm/aSoh5hVUM0F2MpgJ/+0G0uAdscFcIbX1sTMkfvy3zBmrN4rFFzanCixyHdglbZ6fIQY3VsMArW06vmTkMQ3p1jfZclqYNEibbW+Sb+3edz1+pC+HCQwPOEtoSWVLpMUuYPBNzisQhTVhNbcjByFMPchlEkFKvfHWREUSt89Hh73wBIIvELly8F9v4GFdksNgbNXiPjSB+lZrEtLVh49L2UR7pmwxyDcBKIr6CrS3XVaiTpSRZq9X4YFpI6Zgakwsid5gnMs/JQJxZhiNwOJrAdvsmWg=; 20:lstsSmdTBgSs7iRSOjLazjncYEmM0dw+vXLZ45eGLy2FS7UwlgWb6UXglChte3KhBhzTzeABvZ0G69+DIKfUeY28xepI5Xn2AA4ftfMuuqgyaCSDm+7k/8bvQK31J7FRshYytL/ImdsTo9b31ckAlG6m1mhlkg+lpnwH4tF9pKyZ9FSH3pX66ylHvI+zl+ljrhjjOyh2uFof/pN9A+hhZopMAEVXgkoGOWGBrluq7bxjurV/zYN0W8cV61a7Bxxt1BSVPUxJXbSQ2rzrQKwWlq4sUN2oHdNe5pGxjVLrkVYMZs9fuFI+CYETs44oYOGl7UJf3ObX9eni2dT49kiaD9rHeeF08/zPsBTKayFHKxguaaaZQQh3gyuB2XtpnSFqe4N5eIYlRQ3bJraAuylG6UBBvKZySJBYHlFKZ78xiX4jSfsE/tiUupJ+OzaIcg9jsNpEwP3sd/tCjT8MYo2kYIB24wQt/sCvR7RLWOmzINKDOm61QmtDkhIIR2Dw731E X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(20558992708506)(72170088055959)(112613623882745); X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(6060326)(6040281)(601004)(2401047)(13024025)(5005006)(13023025)(8121501046)(13016025)(13018025)(3002001)(10201501046)(6061324)(6041223)(6042181); SRVR:HE1PR06MB1162; BCL:0; PCL:0; RULEID:; SRVR:HE1PR06MB1162; X-Microsoft-Exchange-Diagnostics: 1; HE1PR06MB1162; 4:lJ62s8K1EStubCc2xDKSq7YZyhzTgYCuVsSptyQ0bZmk5Y7yhUsuVNcMsWEyqUZXYFcqeo9WYpbj+m7xl3vMn562fbqFVcSuofdmcH0uC7bp6QzJpgV69VokztVmsg9qgaUWBpkftGGz8l8S91avLDmcq51lOMw0cqrLdSKBP/JRBnO/WTFpiwF6jxYkD/OWuDlF40kotk3adrG2ffNCQ3KVX/ZfD3OGDdAofV+ZykMpANkZcwJV4skN5qh9B0Tte4mFYXRT40I1ilpA+NylbRCbASg+YIzEaiC4AvWYjGj73c380VP01rx6E0js3b63lUzVLpcRLVCrMkIjFtDgkapvVjMOgu/1VDtbZ07tgdzVBrEOZDYlbJSryhq2Rwa3gbWGZwLt11L9XriS6+lIOQXl8vk5f0XDsXc8TgcwlSppB1x3P0cOSE10vvHDRO7pBcHhUccto3wV3oYpS4UFbfh5Bu0Kv8lkDEyHFh5JPNoQZJJ681M2bVtdb+BvKsDGUzDWw/BA6WdKEBclW+gG12JbAARfiMPxgkOpWl/H9ihwjrpKXkQfSARvXSUfx3DgWHptFYudWeHM6vBNcPCstK9w7myeqLw9gzJ/6cyKj7gBRmgTn9kRPpo3omKTh87JR2X6thRU8jQVKYOovXi97+D2lgnd9mMf3WslXhVMDIE= X-Forefront-PRVS: 01283822F8 X-Microsoft-Exchange-Diagnostics: 1; HE1PR06MB1162; 23:EV1iep4rESyWi3FeVHcqVnkwK0eML3IDDLTB3QMgMpiogc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Hpnsw8gqMmziqK8mxcLXJpFMfSNKO9BX7+o8wTTiSEKtFP7+0ZjkfLbonrhxyKKQHOLGc3ODcC+B4OTiTvWN5aAH3clCwS5KHpAPwxUbinImui+NPoYRHAbm/Uaqk0URG3fyeKmA+drbeFC8lIMs8RPdY9vy1X+O2PjhUynKBJsEZnRLlG7jBLyGIFHu0B7SdNyiPpvL1cFPFMUQv6wZghGZnlXFOnZDi46oJ1BdabZo=; 5:O6FMyQCsB2dCxPmPUBYmbY7YaUmVp7v4MBZUXlLVMYxRPzGc4aMdPGsIR/ZS65QEzGOZtBg6X9Z60cE/FDYosZOzsSK3s2LHPLc7ic+vci6dgwcTVdEqOnqcu5EVIyi/8TqpdPeaNDaxazKwgyJg7dHAnV2TJQtfpVQRLlqv2LY=; 24:LNzxoL+hEQA2lvnQC1O5Gb85NuqQnqww2w23kK7FmDldxXcnNoaeUZbsNuChtTp7G1QijvgQ7I881o4j9VeIcvBdaetuhpGhlriOS6gSI3U= SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1; HE1PR06MB1162; 7:Z4xCkr73cYkGECqwxHvLeNLuP/oVhgPDq2fCciBYWEJL7hBZCC6bafQ8+iz0mCcLFsvPqQv+P1Xr/qFKr2oHtWxUfKSasWxRpzyjrwnn4RH3a5y3wUXVZUPKM74zzWDo8UgiYP+0/Prmcz7w09adS0sLjKJVI9q+YCNjqN0pzyuXyuDanidFMI+uCnAQT68qOe/QN9q4tj7JPPqHjruDvuiWjPbwSn96mEJXVvmbbHkclxuLEKW+TX8YKvTs3XcmViXbxh2N5qLq2Q52xdHorFCbVlYL8Y+mrXW3R000M+5+LQuMyOu89cDX3dW0HiGDvSbYrM6juPUBQOxNerGGiUn8oJ16EUZ1OuRqYibtG5g= X-OriginatorOrg: cern.ch X-MS-Exchange-CrossTenant-OriginalArrivalTime: 16 Nov 2016 13:53:44.3384 (UTC) X-MS-Exchange-CrossTenant-Id: c80d3499-4a40-4a8c-986e-abce017d6b19 X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=c80d3499-4a40-4a8c-986e-abce017d6b19; Ip=[188.184.36.16]; Helo=[CERNMX14.cern.ch] X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: HE1PR06MB1162 Sender: linux-hwmon-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-hwmon@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Updated ltc2990 driver to support all possible measurement modes that the chip provides. Mode can be set through a device tree attribute or by writing to the "mode" sysfs attribute. Signed-off-by: Tom Levens --- .../devicetree/bindings/hwmon/ltc2990.txt | 15 + Documentation/hwmon/ltc2990 | 25 ++- drivers/hwmon/Kconfig | 7 +- drivers/hwmon/ltc2990.c | 268 +++++++++++++++++--- 4 files changed, 266 insertions(+), 49 deletions(-) create mode 100644 Documentation/devicetree/bindings/hwmon/ltc2990.txt diff --git a/Documentation/devicetree/bindings/hwmon/ltc2990.txt b/Documentation/devicetree/bindings/hwmon/ltc2990.txt new file mode 100644 index 0000000..0a17e8d --- /dev/null +++ b/Documentation/devicetree/bindings/hwmon/ltc2990.txt @@ -0,0 +1,15 @@ +ltc2990 + +Required properties: +- compatible: must be "lltc,ltc2990" +- reg: I2C slave address + +Optional properties: +- mode: measurement mode (0-7), see Documentation/hwmon/ltc2990 for details + +Example: +ltc2990@4c { + compatible = "lltc,ltc2990"; + mode = <7>; + reg = <0x4c>; +}; diff --git a/Documentation/hwmon/ltc2990 b/Documentation/hwmon/ltc2990 index c25211e..a97df88 100644 --- a/Documentation/hwmon/ltc2990 +++ b/Documentation/hwmon/ltc2990 @@ -8,6 +8,7 @@ Supported chips: Datasheet: http://www.linear.com/product/ltc2990 Author: Mike Looijmans + Tom Levens Description @@ -16,10 +17,8 @@ Description LTC2990 is a Quad I2C Voltage, Current and Temperature Monitor. The chip's inputs can measure 4 voltages, or two inputs together (1+2 and 3+4) can be combined to measure a differential voltage, which is typically used to -measure current through a series resistor, or a temperature. - -This driver currently uses the 2x differential mode only. In order to support -other modes, the driver will need to be expanded. +measure current through a series resistor, or a temperature with an external +diode. Usage Notes @@ -32,12 +31,20 @@ devices explicitly. Sysfs attributes ---------------- +in0_input Voltage at Vcc pin in millivolt (range 2.5V to 5V) +temp1_input Internal chip temperature in millidegrees Celcius +mode Set the measurement mode (range 0 to 7) + +A subset of the following attributes are visible, depending on the programmed +measurement mode. + +in[1-4]_input Voltage at V[1-4] pin in millivolt +temp2_input External temperature sensor TR1 in millidegrees Celcius +temp3_input External temperature sensor TR2 in millidegrees Celcius +curr1_input Current in mA across V1-V2 assuming a 1mOhm sense resistor +curr2_input Current in mA across V3-V4 assuming a 1mOhm sense resistor + The "curr*_input" measurements actually report the voltage drop across the input pins in microvolts. This is equivalent to the current through a 1mOhm sense resistor. Divide the reported value by the actual sense resistor value in mOhm to get the actual value. - -in0_input Voltage at Vcc pin in millivolt (range 2.5V to 5V) -temp1_input Internal chip temperature in millidegrees Celcius -curr1_input Current in mA across v1-v2 assuming a 1mOhm sense resistor. -curr2_input Current in mA across v3-v4 assuming a 1mOhm sense resistor. diff --git a/drivers/hwmon/Kconfig b/drivers/hwmon/Kconfig index 45cef3d..f7096ca 100644 --- a/drivers/hwmon/Kconfig +++ b/drivers/hwmon/Kconfig @@ -699,15 +699,12 @@ config SENSORS_LTC2945 be called ltc2945. config SENSORS_LTC2990 - tristate "Linear Technology LTC2990 (current monitoring mode only)" + tristate "Linear Technology LTC2990" depends on I2C help If you say yes here you get support for Linear Technology LTC2990 I2C System Monitor. The LTC2990 supports a combination of voltage, - current and temperature monitoring, but in addition to the Vcc supply - voltage and chip temperature, this driver currently only supports - reading two currents by measuring two differential voltages across - series resistors. + current and temperature monitoring. This driver can also be built as a module. If so, the module will be called ltc2990. diff --git a/drivers/hwmon/ltc2990.c b/drivers/hwmon/ltc2990.c index 8f8fe05..b6f910d 100644 --- a/drivers/hwmon/ltc2990.c +++ b/drivers/hwmon/ltc2990.c @@ -4,11 +4,10 @@ * Copyright (C) 2014 Topic Embedded Products * Author: Mike Looijmans * - * License: GPLv2 + * Copyright (C) 2016 European Organization for Nuclear Research (CERN) + * Author: Tom Levens * - * This driver assumes the chip is wired as a dual current monitor, and - * reports the voltage drop across two series resistors. It also reports - * the chip's internal temperature and Vcc power supply voltage. + * License: GPLv2 */ #include @@ -17,6 +16,7 @@ #include #include #include +#include #define LTC2990_STATUS 0x00 #define LTC2990_CONTROL 0x01 @@ -31,8 +31,36 @@ #define LTC2990_CONTROL_KELVIN BIT(7) #define LTC2990_CONTROL_SINGLE BIT(6) #define LTC2990_CONTROL_MEASURE_ALL (0x3 << 3) -#define LTC2990_CONTROL_MODE_CURRENT 0x06 -#define LTC2990_CONTROL_MODE_VOLTAGE 0x07 +#define LTC2990_CONTROL_MODE_DEFAULT 0x06 +#define LTC2990_CONTROL_MODE_MAX 0x07 + +#define LTC2990_IN0 BIT(0) +#define LTC2990_IN1 BIT(1) +#define LTC2990_IN2 BIT(2) +#define LTC2990_IN3 BIT(3) +#define LTC2990_IN4 BIT(4) +#define LTC2990_CURR1 BIT(5) +#define LTC2990_CURR2 BIT(6) +#define LTC2990_TEMP1 BIT(7) +#define LTC2990_TEMP2 BIT(8) +#define LTC2990_TEMP3 BIT(9) + +static const int ltc2990_attrs_ena[] = { + LTC2990_IN1 | LTC2990_IN2 | LTC2990_TEMP3, + LTC2990_CURR1 | LTC2990_TEMP3, + LTC2990_CURR1 | LTC2990_IN3 | LTC2990_IN4, + LTC2990_TEMP2 | LTC2990_IN3 | LTC2990_IN4, + LTC2990_TEMP2 | LTC2990_CURR2, + LTC2990_TEMP2 | LTC2990_TEMP3, + LTC2990_CURR1 | LTC2990_CURR2, + LTC2990_IN1 | LTC2990_IN2 | LTC2990_IN3 | LTC2990_IN4 +}; + +struct ltc2990_data { + struct i2c_client *i2c; + struct mutex update_lock; + u32 mode; +}; /* convert raw register value to sign-extended integer in 16-bit range */ static int ltc2990_voltage_to_int(int raw) @@ -44,30 +72,68 @@ static int ltc2990_voltage_to_int(int raw) } /* Return the converted value from the given register in uV or mC */ -static int ltc2990_get_value(struct i2c_client *i2c, u8 reg, int *result) +static int ltc2990_get_value(struct i2c_client *i2c, int index, int *result) { int val; + u8 reg; + + switch (index) { + case LTC2990_IN0: + reg = LTC2990_VCC_MSB; + break; + case LTC2990_IN1: + case LTC2990_CURR1: + case LTC2990_TEMP2: + reg = LTC2990_V1_MSB; + break; + case LTC2990_IN2: + reg = LTC2990_V2_MSB; + break; + case LTC2990_IN3: + case LTC2990_CURR2: + case LTC2990_TEMP3: + reg = LTC2990_V3_MSB; + break; + case LTC2990_IN4: + reg = LTC2990_V4_MSB; + break; + case LTC2990_TEMP1: + reg = LTC2990_TINT_MSB; + break; + default: + return -EINVAL; + } val = i2c_smbus_read_word_swapped(i2c, reg); if (unlikely(val < 0)) return val; - switch (reg) { - case LTC2990_TINT_MSB: - /* internal temp, 0.0625 degrees/LSB, 13-bit */ + switch (index) { + case LTC2990_TEMP1: + case LTC2990_TEMP2: + case LTC2990_TEMP3: + /* temp, 0.0625 degrees/LSB, 13-bit */ val = (val & 0x1FFF) << 3; *result = (val * 1000) >> 7; break; - case LTC2990_V1_MSB: - case LTC2990_V3_MSB: - /* Vx-Vy, 19.42uV/LSB. Depends on mode. */ + case LTC2990_CURR1: + case LTC2990_CURR2: + /* Vx-Vy, 19.42uV/LSB */ *result = ltc2990_voltage_to_int(val) * 1942 / (4 * 100); break; - case LTC2990_VCC_MSB: - /* Vcc, 305.18μV/LSB, 2.5V offset */ + case LTC2990_IN0: + /* Vcc, 305.18uV/LSB, 2.5V offset */ *result = (ltc2990_voltage_to_int(val) * 30518 / (4 * 100 * 1000)) + 2500; break; + case LTC2990_IN1: + case LTC2990_IN2: + case LTC2990_IN3: + case LTC2990_IN4: + /* Vx: 305.18uV/LSB */ + *result = (ltc2990_voltage_to_int(val) * 30518 / + (4 * 100 * 1000)); + break; default: return -EINVAL; /* won't happen, keep compiler happy */ } @@ -75,66 +141,198 @@ static int ltc2990_get_value(struct i2c_client *i2c, u8 reg, int *result) return 0; } + +static int ltc2990_write_control_trigger(struct ltc2990_data *data) +{ + int ret; + + /* Setup continuous mode */ + ret = i2c_smbus_write_byte_data(data->i2c, LTC2990_CONTROL, + LTC2990_CONTROL_MEASURE_ALL | + data->mode); + if (unlikely(ret < 0)) { + dev_err(&data->i2c->dev, + "Error: Failed to set control mode.\n"); + return ret; + } + /* Trigger once to start continuous conversion */ + ret = i2c_smbus_write_byte_data(data->i2c, LTC2990_TRIGGER, 0x01); + if (unlikely(ret < 0)) { + dev_err(&data->i2c->dev, + "Error: Failed to start acquisition.\n"); + return ret; + } + + return 0; +} + static ssize_t ltc2990_show_value(struct device *dev, struct device_attribute *da, char *buf) { struct sensor_device_attribute *attr = to_sensor_dev_attr(da); + struct ltc2990_data *data = dev_get_drvdata(dev); int value; int ret; - ret = ltc2990_get_value(dev_get_drvdata(dev), attr->index, &value); + ret = ltc2990_get_value(data->i2c, attr->index, &value); if (unlikely(ret < 0)) return ret; return snprintf(buf, PAGE_SIZE, "%d\n", value); } +static ssize_t ltc2990_get_mode(struct device *dev, + struct device_attribute *da, char *buf) +{ + struct ltc2990_data *data = dev_get_drvdata(dev); + + return snprintf(buf, PAGE_SIZE, "%d\n", data->mode); +} + +static const struct attribute_group ltc2990_group; + +static ssize_t ltc2990_set_mode(struct device *dev, + struct device_attribute *da, + const char *buf, size_t count) +{ + struct ltc2990_data *data = dev_get_drvdata(dev); + int ret; + unsigned long mode; + + ret = kstrtoul(buf, 10, &mode); + if (unlikely(ret)) + return ret; + + if (mode > LTC2990_CONTROL_MODE_MAX) + return -EINVAL; + + mutex_lock(&data->update_lock); + + data->mode = mode; + + ret = ltc2990_write_control_trigger(data); + if (unlikely(ret < 0)) + goto abort; + + ret = sysfs_update_group(&dev->kobj, <c2990_group); + if (unlikely(ret < 0)) + goto abort; + + ret = count; + +abort: + mutex_unlock(&data->update_lock); + return ret; +} + +static umode_t ltc2990_attrs_visible(struct kobject *kobj, + struct attribute *a, int n) +{ + struct device *dev = container_of(kobj, struct device, kobj); + struct ltc2990_data *data = dev_get_drvdata(dev); + struct device_attribute *da = + container_of(a, struct device_attribute, attr); + struct sensor_device_attribute *attr = to_sensor_dev_attr(da); + + if (ltc2990_attrs_ena[data->mode] & attr->index) + return a->mode; + else + return 0; +} + static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, ltc2990_show_value, NULL, - LTC2990_TINT_MSB); + LTC2990_TEMP1); +static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, ltc2990_show_value, NULL, + LTC2990_TEMP2); +static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, ltc2990_show_value, NULL, + LTC2990_TEMP3); static SENSOR_DEVICE_ATTR(curr1_input, S_IRUGO, ltc2990_show_value, NULL, - LTC2990_V1_MSB); + LTC2990_CURR1); static SENSOR_DEVICE_ATTR(curr2_input, S_IRUGO, ltc2990_show_value, NULL, - LTC2990_V3_MSB); + LTC2990_CURR2); static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, ltc2990_show_value, NULL, - LTC2990_VCC_MSB); + LTC2990_IN0); +static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, ltc2990_show_value, NULL, + LTC2990_IN1); +static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, ltc2990_show_value, NULL, + LTC2990_IN2); +static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, ltc2990_show_value, NULL, + LTC2990_IN3); +static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, ltc2990_show_value, NULL, + LTC2990_IN4); +static SENSOR_DEVICE_ATTR(mode, S_IRUGO | S_IWUSR, ltc2990_get_mode, + ltc2990_set_mode, 0); static struct attribute *ltc2990_attrs[] = { - &sensor_dev_attr_temp1_input.dev_attr.attr, + &sensor_dev_attr_temp2_input.dev_attr.attr, + &sensor_dev_attr_temp3_input.dev_attr.attr, &sensor_dev_attr_curr1_input.dev_attr.attr, &sensor_dev_attr_curr2_input.dev_attr.attr, + &sensor_dev_attr_in1_input.dev_attr.attr, + &sensor_dev_attr_in2_input.dev_attr.attr, + &sensor_dev_attr_in3_input.dev_attr.attr, + &sensor_dev_attr_in4_input.dev_attr.attr, + NULL, +}; + +static const struct attribute_group ltc2990_group = { + .attrs = ltc2990_attrs, + .is_visible = ltc2990_attrs_visible, +}; + +static struct attribute *ltc2990_static_attrs[] = { + &sensor_dev_attr_temp1_input.dev_attr.attr, &sensor_dev_attr_in0_input.dev_attr.attr, + &sensor_dev_attr_mode.dev_attr.attr, + NULL, +}; + +static const struct attribute_group ltc2990_static_group = { + .attrs = ltc2990_static_attrs +}; + +static const struct attribute_group *ltc2990_groups[] = { + <c2990_group, + <c2990_static_group, NULL, }; -ATTRIBUTE_GROUPS(ltc2990); static int ltc2990_i2c_probe(struct i2c_client *i2c, const struct i2c_device_id *id) { int ret; struct device *hwmon_dev; + struct ltc2990_data *data; + struct device_node *of_node = i2c->dev.of_node; if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA)) return -ENODEV; - /* Setup continuous mode, current monitor */ - ret = i2c_smbus_write_byte_data(i2c, LTC2990_CONTROL, - LTC2990_CONTROL_MEASURE_ALL | - LTC2990_CONTROL_MODE_CURRENT); - if (ret < 0) { - dev_err(&i2c->dev, "Error: Failed to set control mode.\n"); - return ret; + data = devm_kzalloc(&i2c->dev, sizeof(struct ltc2990_data), GFP_KERNEL); + if (unlikely(!data)) + return -ENOMEM; + data->i2c = i2c; + + if (!of_node || of_property_read_u32(of_node, "mode", &data->mode)) + data->mode = LTC2990_CONTROL_MODE_DEFAULT; + + if (data->mode > LTC2990_CONTROL_MODE_MAX) { + dev_warn(&data->i2c->dev, + "Warning: Mode %d out of range, defaulting to %d.\n", + data->mode, LTC2990_CONTROL_MODE_DEFAULT); + data->mode = LTC2990_CONTROL_MODE_DEFAULT; } - /* Trigger once to start continuous conversion */ - ret = i2c_smbus_write_byte_data(i2c, LTC2990_TRIGGER, 1); - if (ret < 0) { - dev_err(&i2c->dev, "Error: Failed to start acquisition.\n"); + + ret = ltc2990_write_control_trigger(data); + if (unlikely(ret < 0)) return ret; - } + + mutex_init(&data->update_lock); hwmon_dev = devm_hwmon_device_register_with_groups(&i2c->dev, i2c->name, - i2c, + data, ltc2990_groups); return PTR_ERR_OR_ZERO(hwmon_dev);