From patchwork Thu Dec 19 09:49:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Bhavin Sharma X-Patchwork-Id: 13914910 Received: from PNYPR01CU001.outbound.protection.outlook.com (mail-centralindiaazon11020102.outbound.protection.outlook.com [52.101.225.102]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 00293221DBC; Thu, 19 Dec 2024 09:49:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.101.225.102 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734601792; cv=fail; b=AaBe+b8C895JoXryKalBRse+Z055SPF1lpSQGDD1i0VMLyB6FwD6QF3TvS1eLwOwvSSCDZy9+GP7H4pMgRFw8gFGwwXGhxtGl0os67tiIaCmaf3836zk2oL2+z93U2bVRpwo9nO4z/YJZi43VQ1fm+3ACAUYtsn8EfmA9tE5RcI= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734601792; c=relaxed/simple; bh=HIzgnlJBNBz/6OejXKswZKVi39G8wZhTlwmgipxTt74=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: Content-Type:MIME-Version; b=dTJSEBsDS2IAc2MGOQd28o6MZCkoib388qR4wed5oG1X5FaeIsyDLwd6ilaz4RJpvA6TLzsL4YlV2CqfqUifTRNhdDQ56ZfPWSRQwakmSD8E55YsCeymdTHvZQiN0gRPcFGz6DVT2EKtihV6GRgaMz7UEDwqflMDV0lXC1ZLLGs= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=siliconsignals.io; spf=pass smtp.mailfrom=siliconsignals.io; arc=fail smtp.client-ip=52.101.225.102 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=siliconsignals.io Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=siliconsignals.io ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=daOQ62BtWzGCL48AyRjfjJEH3J9x9+yWpSAdymX3qOhzbCSllZc0iqQJQ9+xw/Sn+OfsEi+ZEDYihSuZL4Jalwg6N085MxM6RIRoq2bLIZUrcrTf+WO741cHFgD0uGAMwaL1voN+GWbAJ3zxjGJnB3IqC23K3qSpwF5JSOHJtZPsEd+3pr14WPMx1H3gqKncPHIF1ugXDp52UeBHnwHJgaMAWk0u5ljaINlXT1J8ryZnsmiuLpi0LfyKsvwpMOF+YN07fSyR6XqpV88ivp1v7QEl+dqUPqfclfzNQKIyuIfgy5LlPZrwj34H2X4tGQVfm9L7jZm2PpGLm1e3bXCoqg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=L/3eU/AbwhPX/80HUCWGzEyK9px/WMqCcf/UZv8Pht8=; b=H0Chf5EGexIoZ2r0c286a5Qg6go3cFLPFKu1gE8h9APegv2oXcZRZrXhO/w97Z43gE4qcIeIa6s2krlkS9lQFaf/yQaotWZ5fWadIPxljEeFIjC92MQIYK8vNpiJzebT8Ms6hvuprjTlojF4QTi4EoCOfKpp9RUe4cuOEsiz2ccEQit7kz5DSPq5lQ4DW4seErSj11A8vZCcmY4D3w4PsiI3FMnbnZYwBVN0ctFT690G+sKGKt//TGcsj8BmLYHc5XQNtGcFJ7oUeDHTdu3QPvkG11JkwDru2DlKTQFJYuXPCeWeGjdlmk5MXmt5dRCaQhrve5M3R2G7Gwor91Q36g== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=siliconsignals.io; dmarc=pass action=none header.from=siliconsignals.io; dkim=pass header.d=siliconsignals.io; arc=none Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=siliconsignals.io; Received: from PN2PPFF679F9759.INDP287.PROD.OUTLOOK.COM (2603:1096:c04:1::15d) by PN0P287MB1187.INDP287.PROD.OUTLOOK.COM (2603:1096:c01:18d::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8230.11; Thu, 19 Dec 2024 09:49:47 +0000 Received: from PN2PPFF679F9759.INDP287.PROD.OUTLOOK.COM ([fe80::740f:ab98:2be1:538]) by PN2PPFF679F9759.INDP287.PROD.OUTLOOK.COM ([fe80::740f:ab98:2be1:538%4]) with mapi id 15.20.8251.015; Thu, 19 Dec 2024 09:49:47 +0000 From: Bhavin Sharma To: sre@kernel.org Cc: Bhavin Sharma , Hardevsinh Palaniya , Rob Herring , Krzysztof Kozlowski , Conor Dooley , linux-pm@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v12 2/2] power: supply: Add STC3117 fuel gauge unit driver Date: Thu, 19 Dec 2024 15:19:12 +0530 Message-Id: <20241219094915.19059-3-bhavin.sharma@siliconsignals.io> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241219094915.19059-1-bhavin.sharma@siliconsignals.io> References: <20241219094915.19059-1-bhavin.sharma@siliconsignals.io> X-ClientProxiedBy: BMXP287CA0004.INDP287.PROD.OUTLOOK.COM (2603:1096:b00:2c::23) To PN2PPFF679F9759.INDP287.PROD.OUTLOOK.COM (2603:1096:c04:1::15d) Precedence: bulk X-Mailing-List: linux-pm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: PN2PPFF679F9759:EE_|PN0P287MB1187:EE_ X-MS-Office365-Filtering-Correlation-Id: 26f2528b-d757-41a0-9e19-08dd20127942 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|376014|52116014|366016|38350700014; X-Microsoft-Antispam-Message-Info: =?utf-8?q?osp1apdMQRmM35YRpra6AJcIX2/AA9x?= =?utf-8?q?b/Ut+CjERopEgOv7a2YKv6C5QikxlCwnpyBKYhRsPqZmMX4MX8Ij2eRAwJtkYfdgz?= =?utf-8?q?VIKKgCmqJnylCtwV8NPXHB19WQUHz+8C5S0weqtTwd7MCGDn72/CheJ0ovY7rjsZK?= =?utf-8?q?TCAbY8jYEtM5urLIHPeMa6pnIHWaDU2/Mvk2ocS0TqEQA8hWxTt4MMtOHhreuySdp?= =?utf-8?q?bFWpfsRHWiyAlUnpyvpt6zAuKjO8So/flhPK7PdisHscWaYQFo0dztwSooGpg1DlY?= =?utf-8?q?C5IBft8mPSXNN50bYS6I9NscazMumytToAGKIslUS5lCRxzA4NwjunV+cpCGSL2t0?= =?utf-8?q?dngK0YJS0InyJue0L5IKb98l46yoNYcmVTIFugFEZ6JAxMA7yUD6AqmzDyvjK1qeh?= =?utf-8?q?tR+YJgpSx2vGWQHKozURqIRs1wgVtvMgDCl299iURF1GBWo53qQYRX8g1N9pV1svA?= =?utf-8?q?G5Hpy/YU2W9qV1lwsG0X0gWz7m77GExajdMGrvIip4PNFmukVG/Tv626XveGlcM4y?= =?utf-8?q?RLd7DoQRGKpm+NROW7n2p5MUpMNq0d4kO3HIktsN8JHoFlA96x948R9xtHFV23ZVx?= =?utf-8?q?8sI2vjGzEGALU7NoJv5tu7FkE4ZwuoGt/Q7glWtvMCTJe2TyRebmj6GvtBM94l1T2?= =?utf-8?q?vVNTKxRJtFVNaAEDGh7hagsktB+NJRt7/xFPoH1cIRDttqah7Nn2QQv+9a8WYSg9j?= =?utf-8?q?Crn6nyV9k0hBUoSOenJ7vTTomSNFWLZhGv4/UFyJSG+eLVrDnQslNMgsRROBLYFrb?= =?utf-8?q?sVnDOI4UgufAur+qcUAScVUc5YIpyoMYeWZ0+LWskhDEF9LJvceNGm2OROeCESJcX?= =?utf-8?q?krBySv8dNA1JWqEU8Kwn4lDkH+MwpJ9FDWgvEYQXQ0CWaOUSVyBANjsR1s66r2kZW?= =?utf-8?q?MMGd6V1MIgZiRaVpUw45eJ9xY4GryUZRUZKGKMkk4EONOurC4GtZSCAtduDwElmIo?= =?utf-8?q?VgBArNaAs8vEuBQUJ/RkQ8Y+kBlcIgHueQUKL/ol1WETAzMQkapB0qeWljfIsDcW8?= =?utf-8?q?uB/5h2XtL+59yaRzD2wx3k9UViZZhqbU2D3mKMM7rYtA+sIv+IUnuz8m0YJMAnT0Q?= =?utf-8?q?pUfR6vpSYAycSsn5m8BjbtxYNfOSwoSi2vUxao+zXqOJfdNyu0NrCL67Yzp9Q5OUz?= =?utf-8?q?oamS0kYIJ9x5OhFLdnJrRLKIa02oQZyyw3vN2PW5/Q3w79ztmDsiHsVU2z+0kLkWO?= =?utf-8?q?8fLcGpaqtvxdxEpSkYr6cYG9N5BbAQ5+Za/KaUtwyvIsZQJBI2DK6Mx9cYeIeSXxD?= =?utf-8?q?VyCEf3jKqBm7msWIVjUqYKBBOSKkNyJt8lyhgD3Cj8jBrqKxL7JkPw6kOkHcuERIp?= =?utf-8?q?BbeVTi+riT3Drm2d55Hv3T5LJkJnP7Oz55zw6QfpQSuAOe1ux95UoRtd9eqsEkQij?= =?utf-8?q?J/tzUebyUAM?= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:PN2PPFF679F9759.INDP287.PROD.OUTLOOK.COM;PTR:;CAT:NONE;SFS:(13230040)(1800799024)(376014)(52116014)(366016)(38350700014);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?r2XdkVVyAZuFbqPicEgyYab7Ogxe?= =?utf-8?q?0nPnYGimw2UKyan8bGelk9TQP2TWs+BhpbveRhCxtOXFM7T5FadoPaQNyRdZJC07R?= =?utf-8?q?vBPrb8gcxEIgh034Q/Bv9LiMc+5WcUPUvWInNqsheGJfuxbx9VErL+lM4okcVLOP+?= =?utf-8?q?7nODBik8Ef2onX2qWprA/xdh6lhWm2w0CYkGw0wuhuLcjvojqXYxlUj8hZyOZXU48?= =?utf-8?q?57OP8LB2u6B9caPuR3jWaxPiul+NrZ+ce7OAn1dfz8ibVL4Q6q2DjbcKE/V3KhPOX?= =?utf-8?q?6qMu4ZjPWS2PuLF+IL0g+RbR8TP7ZNHFFvQ4gu/v0Qtj8Ghei+gWnOxdmiOTZy6gY?= =?utf-8?q?sDrPgHbtnnU4FOpw6+FaEdktqm5kVh8Dt0kKt7wO4geV0a+3i18QROUoyiHYnAlsN?= =?utf-8?q?qkB+/ka4lSjeTJTbeouX7LAGS6FS/nukeZJGFQIocMUT78bUAtUyRpryEem5CTrWI?= =?utf-8?q?SnCqX03kiPGRouDF/wyCnmZ8eo5FYKMyBWKHaB3D2L5OjIk6dq+HnXhkROqfxk0uj?= =?utf-8?q?Jg39Rh6PPg4PSB7ebzLMgCAXeDxvtDlXs5570wzUZNM7SrRMxwkhO81wp0mpGdeZ0?= =?utf-8?q?ZoClBb8Dr8IAvqM5BH7pCLKq6FYvsSIyXrCUUyxlJZvReHXpWx6qShObaqKfQ0+TW?= =?utf-8?q?5BFOAtohBuhlJ3caRBZ0+8mECxksoZCWOX6g74oFmoS6UpR+49ssC8Dkh/7/sBOqZ?= =?utf-8?q?MIhbeVb4qUANly2+D3VZgOkKom8fbYho470tOx0C0+QzbR4/IrcSwm7DCkFFr+H5J?= =?utf-8?q?SP14HPDi+e99/Y3n1M9rIxEG0tP/dskhKXih5B4ZVsG1rCk81djh2EVSiwgBf9d+Y?= =?utf-8?q?OiwAtWJmaw5OsMjWCWPnOVyYarQsojQc+dO6PC/SbQv5R7H0HS+Mwd0Sv0j255eM2?= =?utf-8?q?cfAg3K9x97W8Vsd0zfM+IzA1y+cwGx1JIIg3d/w8IEWBOT0iDTPVs+Q49X7SshTQG?= =?utf-8?q?jpwzt/esGy1vYX+au8USzsjT9soCK2akvh4rHxy7Kk4WC3dCnQR+niVzdt0hT1/5J?= =?utf-8?q?8oKg3rkNe7iv4LSOP/z4JpuG3hJDi9TOrf/WAuhI2f+Gt/gKYY34ZWdch2aYHv+t8?= =?utf-8?q?/Jq/oaR8NUNCu/gKD3Thjb8yeUBR+9ztjNA+cVQzzzrtqcqIZARC4F4JhjsCRy9zP?= =?utf-8?q?fu/F6J0QrJtf4WkJuHFngbb6kHaJ+C2vCujHkgEb6Jh2FuVTC4WUA7po8Nv5HW2QC?= =?utf-8?q?/17GNdA3+0j9bA45fHGNp1VMW9BI7uKn/ewEkk2IQRgjTHUs4OmAYds7sqC+JB6bc?= =?utf-8?q?rUwaiEv0mtmppGQ/5nznMb+MlvF5i9tLCGS6IKGCetCl7mobs7ndOa+dnZ/U2B28v?= =?utf-8?q?ZK5ivY3C0DMzE52ZL2X+ajgBdbPmSh8F/VCUXkdrj68CK2SEpMcuauIvIEh2oiyRQ?= =?utf-8?q?e4J9uf82vvRuUd4c95sN/g2f4APCRziAcFxWbT/86/98qwDmzq/z1lbQBGfpNp039?= =?utf-8?q?zGdreUsi0f/LUQfgib9u6HxIEDm9Y/d14iYtOa0PjgxKaS3f/ZhSIWGJ96LffhTgI?= =?utf-8?q?bjYkhxsMcVgwGpKZZghCMXOKoXfz38059g1xCqO2HAjy9CJhy5SHgOY=3D?= X-OriginatorOrg: siliconsignals.io X-MS-Exchange-CrossTenant-Network-Message-Id: 26f2528b-d757-41a0-9e19-08dd20127942 X-MS-Exchange-CrossTenant-AuthSource: PN2PPFF679F9759.INDP287.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 Dec 2024 09:49:47.3868 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 7ec5089e-a433-4bd1-a638-82ee62e21d37 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: hURY6XI6f/uL2VLsolvsetDskhToN+XWv7FAH2rvfYXMH4cr0IT8uvrwmDzQ6IPYCd2J1bHtnhNE6ggMmUzAi1sJSwWbV6RCBYX30Ulgq4c= X-MS-Exchange-Transport-CrossTenantHeadersStamped: PN0P287MB1187 Adds initial support for the STC3117 fuel gauge. The driver provides functionality to monitor key parameters including: - Voltage - Current - State of Charge (SOC) - Temperature - Status Co-developed-by: Hardevsinh Palaniya Signed-off-by: Hardevsinh Palaniya Signed-off-by: Bhavin Sharma --- MAINTAINERS | 8 + drivers/power/supply/Kconfig | 7 + drivers/power/supply/Makefile | 1 + drivers/power/supply/stc3117_fuel_gauge.c | 607 ++++++++++++++++++++++ 4 files changed, 623 insertions(+) create mode 100644 drivers/power/supply/stc3117_fuel_gauge.c diff --git a/MAINTAINERS b/MAINTAINERS index 7e29c4e86db1..847690a8d9a7 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -22170,6 +22170,14 @@ T: git git://linuxtv.org/media.git F: Documentation/devicetree/bindings/media/i2c/st,st-mipid02.yaml F: drivers/media/i2c/st-mipid02.c +ST STC3117 FUEL GAUGE DRIVER +M: Hardevsinh Palaniya +M: Bhavin Sharma +L: linux-pm@vger.kernel.org +S: Maintained +F: Documentation/devicetree/bindings/power/supply/st,stc3117.yaml +F: drivers/power/supply/stc3117_fuel_gauge.c + ST STM32 FIREWALL M: Gatien Chevallier S: Maintained diff --git a/drivers/power/supply/Kconfig b/drivers/power/supply/Kconfig index 9f2eef6787f7..5c13fbc9751e 100644 --- a/drivers/power/supply/Kconfig +++ b/drivers/power/supply/Kconfig @@ -918,6 +918,13 @@ config FUEL_GAUGE_SC27XX Say Y here to enable support for fuel gauge with SC27XX PMIC chips. +config FUEL_GAUGE_STC3117 + tristate "STMicroelectronics STC3117 fuel gauge driver" + depends on I2C + help + Say Y here to enable support for fuel gauge with STC3117 + chip. + config CHARGER_UCS1002 tristate "Microchip UCS1002 USB Port Power Controller" depends on I2C diff --git a/drivers/power/supply/Makefile b/drivers/power/supply/Makefile index 59c4a9f40d28..b55cc48a4c86 100644 --- a/drivers/power/supply/Makefile +++ b/drivers/power/supply/Makefile @@ -108,6 +108,7 @@ obj-$(CONFIG_CHARGER_CROS_USBPD) += cros_usbpd-charger.o obj-$(CONFIG_CHARGER_CROS_PCHG) += cros_peripheral_charger.o obj-$(CONFIG_CHARGER_SC2731) += sc2731_charger.o obj-$(CONFIG_FUEL_GAUGE_SC27XX) += sc27xx_fuel_gauge.o +obj-$(CONFIG_FUEL_GAUGE_STC3117) += stc3117_fuel_gauge.o obj-$(CONFIG_CHARGER_UCS1002) += ucs1002_power.o obj-$(CONFIG_CHARGER_BD99954) += bd99954-charger.o obj-$(CONFIG_CHARGER_WILCO) += wilco-charger.o diff --git a/drivers/power/supply/stc3117_fuel_gauge.c b/drivers/power/supply/stc3117_fuel_gauge.c new file mode 100644 index 000000000000..203996ebd215 --- /dev/null +++ b/drivers/power/supply/stc3117_fuel_gauge.c @@ -0,0 +1,607 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * stc3117_fuel_gauge.c - STMicroelectronics STC3117 Fuel Gauge Driver + * + * Copyright (c) 2024 Silicon Signals Pvt Ltd. + * Author: Hardevsinh Palaniya + * Bhavin Sharma + */ + +#include +#include +#include +#include +#include +#include + +#define STC3117_ADDR_MODE 0x00 +#define STC3117_ADDR_CTRL 0x01 +#define STC3117_ADDR_SOC_L 0x02 +#define STC3117_ADDR_SOC_H 0x03 +#define STC3117_ADDR_COUNTER_L 0x04 +#define STC3117_ADDR_COUNTER_H 0x05 +#define STC3117_ADDR_CURRENT_L 0x06 +#define STC3117_ADDR_CURRENT_H 0x07 +#define STC3117_ADDR_VOLTAGE_L 0x08 +#define STC3117_ADDR_VOLTAGE_H 0x09 +#define STC3117_ADDR_TEMPERATURE 0x0A +#define STC3117_ADDR_AVG_CURRENT_L 0x0B +#define STC3117_ADDR_AVG_CURRENT_H 0x0C +#define STC3117_ADDR_OCV_L 0x0D +#define STC3117_ADDR_OCV_H 0x0E +#define STC3117_ADDR_CC_CNF_L 0x0F +#define STC3117_ADDR_CC_CNF_H 0x10 +#define STC3117_ADDR_VM_CNF_L 0x11 +#define STC3117_ADDR_VM_CNF_H 0x12 +#define STC3117_ADDR_ALARM_soc 0x13 +#define STC3117_ADDR_ALARM_VOLTAGE 0x14 +#define STC3117_ADDR_ID 0x18 +#define STC3117_ADDR_CC_ADJ_L 0x1B +#define STC3117_ADDR_CC_ADJ_H 0x1C +#define STC3117_ADDR_VM_ADJ_L 0x1D +#define STC3117_ADDR_VM_ADJ_H 0x1E +#define STC3117_ADDR_RAM 0x20 +#define STC3117_ADDR_OCV_TABLE 0x30 +#define STC3117_ADDR_SOC_TABLE 0x30 + +/* Bit mask definition */ +#define STC3117_ID 0x16 +#define STC3117_MIXED_MODE 0x00 +#define STC3117_VMODE BIT(0) +#define STC3117_GG_RUN BIT(4) +#define STC3117_CC_MODE BIT(5) +#define STC3117_BATFAIL BIT(3) +#define STC3117_PORDET BIT(4) +#define STC3117_RAM_SIZE 16 +#define STC3117_OCV_TABLE_SIZE 16 +#define STC3117_RAM_TESTWORD 0x53A9 +#define STC3117_SOFT_RESET 0x11 +#define STC3117_NOMINAL_CAPACITY 2600 + +#define VOLTAGE_LSB_VALUE 9011 +#define CURRENT_LSB_VALUE 24084 +#define APP_CUTOFF_VOLTAGE 2500 +#define MAX_HRSOC 51200 +#define MAX_SOC 1000 +#define CHG_MIN_CURRENT 200 +#define CHG_END_CURRENT 20 +#define APP_MIN_CURRENT (-5) +#define BATTERY_FULL 95 +#define CRC8_POLYNOMIAL 0x07 +#define CRC8_INIT 0x00 + +DECLARE_CRC8_TABLE(stc3117_crc_table); + +enum stc3117_state { + STC3117_INIT, + STC3117_RUNNING, + STC3117_POWERDN, +}; + +/* Default ocv curve Li-ion battery */ +static const int ocv_value[16] = { + 3400, 3582, 3669, 3676, 3699, 3737, 3757, 3774, + 3804, 3844, 3936, 3984, 4028, 4131, 4246, 4320 +}; + +static union stc3117_internal_ram { + u8 ram_bytes[STC3117_RAM_SIZE]; + struct { + u16 testword; /* 0-1 Bytes */ + u16 hrsoc; /* 2-3 Bytes */ + u16 cc_cnf; /* 4-5 Bytes */ + u16 vm_cnf; /* 6-7 Bytes */ + u8 soc; /* 8 Byte */ + u8 state; /* 9 Byte */ + u8 unused[5]; /* 10-14 Bytes */ + u8 crc; /* 15 Byte */ + } reg; +} ram_data; + +struct stc3117_data { + struct i2c_client *client; + struct regmap *regmap; + struct delayed_work update_work; + struct power_supply *battery; + + u8 soc_tab[16]; + int cc_cnf; + int vm_cnf; + int cc_adj; + int vm_adj; + int avg_current; + int avg_voltage; + int batt_current; + int voltage; + int temp; + int soc; + int ocv; + int hrsoc; + int presence; +}; + +struct stc3117_battery_info { + int voltage_min_mv; + int voltage_max_mv; + int battery_capacity_mah; + int sense_resistor; +} battery_info; + +static int stc3117_convert(int value, int factor) +{ + value = (value * factor) / 4096; + return value * 1000; +} + +static int stc3117_get_battery_data(struct stc3117_data *data) +{ + u8 reg_list[16]; + u8 data_adjust[4]; + int value, mode; + + regmap_bulk_read(data->regmap, STC3117_ADDR_MODE, + reg_list, sizeof(reg_list)); + + /* soc */ + value = (reg_list[3] << 8) + reg_list[2]; + data->hrsoc = value; + data->soc = (value * 10 + 256) / 512; + + /* current in uA*/ + value = (reg_list[7] << 8) + reg_list[6]; + data->batt_current = stc3117_convert(value, + CURRENT_LSB_VALUE / battery_info.sense_resistor); + + /* voltage in uV */ + value = (reg_list[9] << 8) + reg_list[8]; + data->voltage = stc3117_convert(value, VOLTAGE_LSB_VALUE); + + /* temp in 1/10 of °C */ + data->temp = reg_list[10] * 10; + + /* Avg current in uA */ + value = (reg_list[12] << 8) + reg_list[11]; + regmap_read(data->regmap, STC3117_ADDR_MODE, &mode); + if (!(mode & STC3117_VMODE)) { + value = stc3117_convert(value, + CURRENT_LSB_VALUE / battery_info.sense_resistor); + value = value / 4; + } else { + value = stc3117_convert(value, 36 * STC3117_NOMINAL_CAPACITY); + } + data->avg_current = value; + + /* ocv in uV */ + value = (reg_list[14] << 8) + reg_list[13]; + value = stc3117_convert(value, VOLTAGE_LSB_VALUE); + value = (value + 2) / 4; + data->ocv = value; + + /* CC & VM adjustment counters */ + regmap_bulk_read(data->regmap, STC3117_ADDR_CC_ADJ_L, + data_adjust, sizeof(data_adjust)); + value = (data_adjust[1] << 8) + data_adjust[0]; + data->cc_adj = value; + + value = (data_adjust[3] << 8) + data_adjust[2]; + data->vm_adj = value; + + return 0; +} + +static int ram_write(struct stc3117_data *data) +{ + int ret; + + ret = regmap_bulk_write(data->regmap, STC3117_ADDR_RAM, + ram_data.ram_bytes, STC3117_RAM_SIZE); + if (ret) + return ret; + + return 0; +}; + +static int ram_read(struct stc3117_data *data) +{ + int ret; + + ret = regmap_bulk_read(data->regmap, STC3117_ADDR_RAM, + ram_data.ram_bytes, STC3117_RAM_SIZE); + if (ret) + return ret; + + return 0; +}; + +static int stc3117_set_para(struct stc3117_data *data) +{ + int ret; + + ret = regmap_write(data->regmap, STC3117_ADDR_MODE, STC3117_VMODE); + + for (int i = 0; i < STC3117_OCV_TABLE_SIZE; i++) + ret |= regmap_write(data->regmap, STC3117_ADDR_OCV_TABLE + i, + ocv_value[i] * 100 / 55); + if (data->soc_tab[1] != 0) + ret |= regmap_bulk_write(data->regmap, STC3117_ADDR_SOC_TABLE, + data->soc_tab, STC3117_OCV_TABLE_SIZE); + + ret |= regmap_write(data->regmap, STC3117_ADDR_CC_CNF_H, + (ram_data.reg.cc_cnf >> 8) & 0xFF); + + ret |= regmap_write(data->regmap, STC3117_ADDR_CC_CNF_L, + ram_data.reg.cc_cnf & 0xFF); + + ret |= regmap_write(data->regmap, STC3117_ADDR_VM_CNF_H, + (ram_data.reg.vm_cnf >> 8) & 0xFF); + + ret |= regmap_write(data->regmap, STC3117_ADDR_VM_CNF_L, + ram_data.reg.vm_cnf & 0xFF); + + ret |= regmap_write(data->regmap, STC3117_ADDR_CTRL, 0x03); + + ret |= regmap_write(data->regmap, STC3117_ADDR_MODE, + STC3117_MIXED_MODE | STC3117_GG_RUN); + + return ret; +}; + +static int stc3117_init(struct stc3117_data *data) +{ + int id, ret; + int ctrl; + int ocv_m, ocv_l; + + regmap_read(data->regmap, STC3117_ADDR_ID, &id); + if (id != STC3117_ID) + return -EINVAL; + + data->cc_cnf = (battery_info.battery_capacity_mah * + battery_info.sense_resistor * 250 + 6194) / 12389; + data->vm_cnf = (battery_info.battery_capacity_mah + * 200 * 50 + 24444) / 48889; + + /* Battery has not been removed */ + data->presence = 1; + + /* Read RAM data */ + ret = ram_read(data); + if (ret) + return ret; + + if (ram_data.reg.testword != STC3117_RAM_TESTWORD || + (crc8(stc3117_crc_table, ram_data.ram_bytes, + STC3117_RAM_SIZE, CRC8_INIT)) != 0) { + ram_data.reg.testword = STC3117_RAM_TESTWORD; + ram_data.reg.cc_cnf = data->cc_cnf; + ram_data.reg.vm_cnf = data->vm_cnf; + ram_data.reg.crc = crc8(stc3117_crc_table, ram_data.ram_bytes, + STC3117_RAM_SIZE - 1, CRC8_INIT); + + ret = regmap_read(data->regmap, STC3117_ADDR_OCV_H, &ocv_m); + + ret |= regmap_read(data->regmap, STC3117_ADDR_OCV_L, &ocv_l); + + ret |= stc3117_set_para(data); + + ret |= regmap_write(data->regmap, STC3117_ADDR_OCV_H, ocv_m); + + ret |= regmap_write(data->regmap, STC3117_ADDR_OCV_L, ocv_l); + if (ret) + return ret; + } else { + ret = regmap_read(data->regmap, STC3117_ADDR_CTRL, &ctrl); + if (ret) + return ret; + + if ((ctrl & STC3117_BATFAIL) != 0 || + (ctrl & STC3117_PORDET) != 0) { + ret = regmap_read(data->regmap, + STC3117_ADDR_OCV_H, &ocv_m); + + ret |= regmap_read(data->regmap, + STC3117_ADDR_OCV_L, &ocv_l); + + ret |= stc3117_set_para(data); + + ret |= regmap_write(data->regmap, + STC3117_ADDR_OCV_H, ocv_m); + + ret |= regmap_write(data->regmap, + STC3117_ADDR_OCV_L, ocv_l); + if (ret) + return ret; + } else { + ret = stc3117_set_para(data); + ret |= regmap_write(data->regmap, STC3117_ADDR_SOC_H, + (ram_data.reg.hrsoc >> 8 & 0xFF)); + ret |= regmap_write(data->regmap, STC3117_ADDR_SOC_L, + (ram_data.reg.hrsoc & 0xFF)); + if (ret) + return ret; + } + } + + ram_data.reg.state = STC3117_INIT; + ram_data.reg.crc = crc8(stc3117_crc_table, ram_data.ram_bytes, + STC3117_RAM_SIZE - 1, CRC8_INIT); + ret = ram_write(data); + if (ret) + return ret; + + return 0; +}; + +static int stc3117_task(struct stc3117_data *data) +{ + int id, mode, ret; + int count_l, count_m; + int ocv_l, ocv_m; + + regmap_read(data->regmap, STC3117_ADDR_ID, &id); + if (id != STC3117_ID) { + data->presence = 0; + return -EINVAL; + } + + stc3117_get_battery_data(data); + + /* Read RAM data */ + ret = ram_read(data); + if (ret) + return ret; + + if (ram_data.reg.testword != STC3117_RAM_TESTWORD || + (crc8(stc3117_crc_table, ram_data.ram_bytes, + STC3117_RAM_SIZE, CRC8_INIT) != 0)) { + ram_data.reg.testword = STC3117_RAM_TESTWORD; + ram_data.reg.cc_cnf = data->cc_cnf; + ram_data.reg.vm_cnf = data->vm_cnf; + ram_data.reg.crc = crc8(stc3117_crc_table, ram_data.ram_bytes, + STC3117_RAM_SIZE - 1, CRC8_INIT); + ram_data.reg.state = STC3117_INIT; + } + + /* check battery presence status */ + ret = regmap_read(data->regmap, STC3117_ADDR_CTRL, &mode); + if ((mode & STC3117_BATFAIL) != 0) { + data->presence = 0; + ram_data.reg.testword = 0; + ram_data.reg.state = STC3117_INIT; + ret = ram_write(data); + ret |= regmap_write(data->regmap, STC3117_ADDR_CTRL, STC3117_PORDET); + if (ret) + return ret; + } + + data->presence = 1; + + ret = regmap_read(data->regmap, STC3117_ADDR_MODE, &mode); + if (ret) + return ret; + if ((mode & STC3117_GG_RUN) == 0) { + if (ram_data.reg.state > STC3117_INIT) { + ret = stc3117_set_para(data); + + ret |= regmap_write(data->regmap, STC3117_ADDR_SOC_H, + (ram_data.reg.hrsoc >> 8 & 0xFF)); + ret |= regmap_write(data->regmap, STC3117_ADDR_SOC_L, + (ram_data.reg.hrsoc & 0xFF)); + if (ret) + return ret; + } else { + ret = regmap_read(data->regmap, STC3117_ADDR_OCV_H, &ocv_m); + + ret |= regmap_read(data->regmap, STC3117_ADDR_OCV_L, &ocv_l); + + ret |= stc3117_set_para(data); + + ret |= regmap_write(data->regmap, STC3117_ADDR_OCV_H, ocv_m); + + ret |= regmap_write(data->regmap, STC3117_ADDR_OCV_L, ocv_l); + if (ret) + return ret; + } + ram_data.reg.state = STC3117_INIT; + } + + regmap_read(data->regmap, STC3117_ADDR_COUNTER_L, &count_l); + regmap_read(data->regmap, STC3117_ADDR_COUNTER_H, &count_m); + + count_m = (count_m << 8) + count_l; + + /* INIT state, wait for batt_current & temperature value available: */ + if (ram_data.reg.state == STC3117_INIT && count_m > 4) { + data->avg_voltage = data->voltage; + data->avg_current = data->batt_current; + ram_data.reg.state = STC3117_RUNNING; + } + + if (ram_data.reg.state != STC3117_RUNNING) { + data->batt_current = -ENODATA; + data->temp = -ENODATA; + } else { + if (data->voltage < APP_CUTOFF_VOLTAGE) + data->soc = -ENODATA; + + if (mode & STC3117_VMODE) { + data->avg_current = -ENODATA; + data->batt_current = -ENODATA; + } + } + + ram_data.reg.hrsoc = data->hrsoc; + ram_data.reg.soc = (data->soc + 5) / 10; + ram_data.reg.crc = crc8(stc3117_crc_table, ram_data.ram_bytes, + STC3117_RAM_SIZE - 1, CRC8_INIT); + + ret = ram_write(data); + if (ret) + return ret; + return 0; +}; + +static void fuel_gauge_update_work(struct work_struct *work) +{ + struct stc3117_data *data = + container_of(work, struct stc3117_data, update_work.work); + + stc3117_task(data); + + /* Schedule the work to run again in 2 seconds */ + schedule_delayed_work(&data->update_work, msecs_to_jiffies(2000)); +} + +static int stc3117_get_property(struct power_supply *psy, + enum power_supply_property psp, union power_supply_propval *val) +{ + struct stc3117_data *data = power_supply_get_drvdata(psy); + + switch (psp) { + case POWER_SUPPLY_PROP_STATUS: + if (data->soc > BATTERY_FULL) + val->intval = POWER_SUPPLY_STATUS_FULL; + else if (data->batt_current < 0) + val->intval = POWER_SUPPLY_STATUS_CHARGING; + else if (data->batt_current > 0) + val->intval = POWER_SUPPLY_STATUS_DISCHARGING; + else + val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING; + break; + case POWER_SUPPLY_PROP_VOLTAGE_NOW: + val->intval = data->voltage; + break; + case POWER_SUPPLY_PROP_CURRENT_NOW: + val->intval = data->batt_current; + break; + case POWER_SUPPLY_PROP_VOLTAGE_OCV: + val->intval = data->ocv; + break; + case POWER_SUPPLY_PROP_CURRENT_AVG: + val->intval = data->avg_current; + break; + case POWER_SUPPLY_PROP_CAPACITY: + val->intval = data->soc; + break; + case POWER_SUPPLY_PROP_TEMP: + val->intval = data->temp; + break; + case POWER_SUPPLY_PROP_PRESENT: + val->intval = data->presence; + break; + default: + return -EINVAL; + } + return 0; +} + +static enum power_supply_property stc3117_battery_props[] = { + POWER_SUPPLY_PROP_STATUS, + POWER_SUPPLY_PROP_VOLTAGE_NOW, + POWER_SUPPLY_PROP_CURRENT_NOW, + POWER_SUPPLY_PROP_VOLTAGE_OCV, + POWER_SUPPLY_PROP_CURRENT_AVG, + POWER_SUPPLY_PROP_CAPACITY, + POWER_SUPPLY_PROP_TEMP, + POWER_SUPPLY_PROP_PRESENT, +}; + +static const struct power_supply_desc stc3117_battery_desc = { + .name = "stc3117-battery", + .type = POWER_SUPPLY_TYPE_BATTERY, + .get_property = stc3117_get_property, + .properties = stc3117_battery_props, + .num_properties = ARRAY_SIZE(stc3117_battery_props), +}; + +static const struct regmap_config stc3117_regmap_config = { + .reg_bits = 8, + .val_bits = 8, +}; + +static int stc3117_probe(struct i2c_client *client) +{ + struct stc3117_data *data; + struct power_supply_config psy_cfg = {}; + struct power_supply_battery_info *info; + int ret; + + data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL); + if (!data) + return -ENOMEM; + + data->client = client; + data->regmap = devm_regmap_init_i2c(client, &stc3117_regmap_config); + if (IS_ERR(data->regmap)) + return PTR_ERR(data->regmap); + + psy_cfg.drv_data = data; + + psy_cfg.fwnode = dev_fwnode(&client->dev); + + crc8_populate_msb(stc3117_crc_table, CRC8_POLYNOMIAL); + + data->battery = devm_power_supply_register(&client->dev, + &stc3117_battery_desc, &psy_cfg); + if (IS_ERR(data->battery)) + return dev_err_probe(&client->dev, PTR_ERR(data->battery), + "failed to register battery\n"); + + ret = device_property_read_u32(&client->dev, "shunt-resistor-micro-ohms", + &battery_info.sense_resistor); + if (ret) + return dev_err_probe(&client->dev, ret, + "failed to get shunt-resistor-micro-ohms\n"); + battery_info.sense_resistor = battery_info.sense_resistor / 1000; + + ret = power_supply_get_battery_info(data->battery, &info); + if (ret) + return dev_err_probe(&client->dev, ret, + "failed to get battery information\n"); + + battery_info.battery_capacity_mah = info->charge_full_design_uah / 1000; + battery_info.voltage_min_mv = info->voltage_min_design_uv / 1000; + battery_info.voltage_max_mv = info->voltage_max_design_uv / 1000; + + ret = stc3117_init(data); + if (ret) + return dev_err_probe(&client->dev, ret, + "failed to initialize of stc3117\n"); + + ret = devm_delayed_work_autocancel(&client->dev, &data->update_work, + fuel_gauge_update_work); + if (ret) + return ret; + + schedule_delayed_work(&data->update_work, 0); + + return 0; +} + +static const struct i2c_device_id stc3117_id[] = { + { "stc3117", 0 }, + { } +}; +MODULE_DEVICE_TABLE(i2c, stc3117_id); + +static const struct of_device_id stc3117_of_match[] = { + { .compatible = "st,stc3117" }, + { } +}; +MODULE_DEVICE_TABLE(of, stc3117_of_match); + +static struct i2c_driver stc3117_i2c_driver = { + .driver = { + .name = "stc3117_i2c_driver", + .of_match_table = stc3117_of_match, + }, + .probe = stc3117_probe, + .id_table = stc3117_id, +}; + +module_i2c_driver(stc3117_i2c_driver); + +MODULE_LICENSE("GPL"); +MODULE_AUTHOR("Hardevsinh Palaniya "); +MODULE_AUTHOR("Bhavin Sharma "); +MODULE_DESCRIPTION("STC3117 Fuel Gauge Driver");