From patchwork Wed Aug 23 14:36:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Konrad Dybcio X-Patchwork-Id: 716783 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 40CE7EE49B0 for ; Wed, 23 Aug 2023 14:36:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236550AbjHWOgk (ORCPT ); Wed, 23 Aug 2023 10:36:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44456 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236530AbjHWOgj (ORCPT ); Wed, 23 Aug 2023 10:36:39 -0400 Received: from mail-lj1-x233.google.com (mail-lj1-x233.google.com [IPv6:2a00:1450:4864:20::233]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9EB8A10D0 for ; Wed, 23 Aug 2023 07:36:35 -0700 (PDT) Received: by mail-lj1-x233.google.com with SMTP id 38308e7fff4ca-2b974031aeaso88568741fa.0 for ; Wed, 23 Aug 2023 07:36:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1692801394; x=1693406194; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=ivqS0xEwrVFeeN64R1VkPAP36lNsGjNwDFiLimBnEdE=; b=Z1RhUzuucUJ1YKoDqZ6FVaSWpTeX8kStbYl5jjawAr7DUILhVN3elfbANhvCZd3UBK sw/8zyeaqjRjyzkWL1T0AGRcR5SES/xHgboV4yU/d6q2e5AMtYoqnM0dhhBM/vnihQly Q6shkoUnL74aWeYfQf0G4sEaA1wnJe/0Z5W3vfk0DpAHzYzkaa63uprxVi5Z6fl6n05q cERPMcAbacYl4RQFwGuwzs72nyVDowjVy+nRd0CvcjVeXXNCMReu3dUG+dp6Ag5xodbK Z0EharSQv4D6KWPHhAv9sSNUM/OPVnKP9BWOFGlOucWtaRB5RF8O0hKm0t4+Ec1NbE8T cQ9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692801394; x=1693406194; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ivqS0xEwrVFeeN64R1VkPAP36lNsGjNwDFiLimBnEdE=; b=awzboycbuccXkRIi9iK79SnlmAHLrVk9cPLXKY0BfBiuQSvg/GjPbJxmN0PxLH+16P F73H2yBkvONSFMYpV7sRPr74gYULWjiO43U/2sJYVbbA9unHSMf/ukZlVTaLOvcyz4gE sfOKHQ7UFmfudDzZlQkzTbmQF5fU81RigFUR2sO3Lpt4C6Hw1rqbdK/5fuN/rwX261yt ZYXIwoGc9VX6fRFO2HZZ7Nn+hMB7QRNdb/Gr0kdDr0SUNbykEfAPgIoEq32vy9mHaPYN C3zkdPLYjQ8/zGC42D030Ra07BTnyJcp/kLtLxJHJLYD+aEXf4SpssdVSXpEAF9k5HgL 1Q5g== X-Gm-Message-State: AOJu0YyUCwp1bIkKlQ2Qq0SReLYz2TsAuzSTmkFfUWe72B8mGI5asEk+ 8DBJ0BPLFElsnKgTSfn4S4UKeg== X-Google-Smtp-Source: AGHT+IGhYtqDsu1Foae0Bp0hasc1syDFC0sos1npv/eWwKQThsKzgMlJJrspRuNJftUmAvGqbRJejw== X-Received: by 2002:a05:6512:a92:b0:4fe:c55:4861 with SMTP id m18-20020a0565120a9200b004fe0c554861mr11048535lfu.53.1692801394003; Wed, 23 Aug 2023 07:36:34 -0700 (PDT) Received: from [192.168.1.101] (abyj76.neoplus.adsl.tpnet.pl. [83.9.29.76]) by smtp.gmail.com with ESMTPSA id z6-20020ac24186000000b004fe36bae2d6sm2668882lfh.81.2023.08.23.07.36.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 23 Aug 2023 07:36:33 -0700 (PDT) From: Konrad Dybcio Date: Wed, 23 Aug 2023 16:36:13 +0200 Subject: [PATCH RESEND v2 1/3] dt-bindings: vendor-prefixes: Add Mitsumi Electric Co., Ltd. MIME-Version: 1.0 Message-Id: <20230621-topic-mm8013-v2-1-9f1b41f4bc06@linaro.org> References: <20230621-topic-mm8013-v2-0-9f1b41f4bc06@linaro.org> In-Reply-To: <20230621-topic-mm8013-v2-0-9f1b41f4bc06@linaro.org> To: Rob Herring , Krzysztof Kozlowski , Conor Dooley , Sebastian Reichel , Konrad Dybcio Cc: Marijn Suijten , Rob Herring , devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-pm@vger.kernel.org, Konrad Dybcio , Krzysztof Kozlowski X-Mailer: b4 0.12.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1692801391; l=1125; i=konrad.dybcio@linaro.org; s=20230215; h=from:subject:message-id; bh=cMf7ni6nLZNuOnZX2l2Ut6wq6sxntra5jVOeRd5nM1U=; b=cgPIWTlzumQQfj+VC3sFuor7PGW/b2+umgB6fw59qDGWXx8IgkpokMDQNSex7Fp0hIvIsIlc0 t+IQvDhKCvDB1WxqKmqEKMv1nUrZu2msGH3paMBLslvPOfoUjDhj9Ke X-Developer-Key: i=konrad.dybcio@linaro.org; a=ed25519; pk=iclgkYvtl2w05SSXO5EjjSYlhFKsJ+5OSZBjOkQuEms= Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org Mitsumi was [1] a Japanese company making various electronics devices and components. They've merged into a new entity, MinebeaMitsumi in 2017. Their current main page is available at [2] [1] https://en.wikipedia.org/wiki/Mitsumi_Electric [2] https://product.minebeamitsumi.com/en/ Acked-by: Krzysztof Kozlowski Signed-off-by: Konrad Dybcio --- Documentation/devicetree/bindings/vendor-prefixes.yaml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Documentation/devicetree/bindings/vendor-prefixes.yaml b/Documentation/devicetree/bindings/vendor-prefixes.yaml index af60bf1a6664..6142c40cf06e 100644 --- a/Documentation/devicetree/bindings/vendor-prefixes.yaml +++ b/Documentation/devicetree/bindings/vendor-prefixes.yaml @@ -865,6 +865,8 @@ patternProperties: description: MiraMEMS Sensing Technology Co., Ltd. "^mitsubishi,.*": description: Mitsubishi Electric Corporation + "^mitsumi,.*": + description: Mitsumi Electric Co., Ltd. "^mixel,.*": description: Mixel, Inc. "^miyoo,.*": From patchwork Wed Aug 23 14:36:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Konrad Dybcio X-Patchwork-Id: 716782 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 33367EE49B0 for ; Wed, 23 Aug 2023 14:36:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236262AbjHWOgv (ORCPT ); Wed, 23 Aug 2023 10:36:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38576 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236561AbjHWOgt (ORCPT ); Wed, 23 Aug 2023 10:36:49 -0400 Received: from mail-lf1-x135.google.com (mail-lf1-x135.google.com [IPv6:2a00:1450:4864:20::135]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3CDAF10C2 for ; Wed, 23 Aug 2023 07:36:38 -0700 (PDT) Received: by mail-lf1-x135.google.com with SMTP id 2adb3069b0e04-4ff9abf18f9so8711551e87.2 for ; Wed, 23 Aug 2023 07:36:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1692801396; x=1693406196; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=/WLSfK19yGazCSe/ro0dcEq/1cOt60o3gtnCvbViYtI=; b=p22tE7rzJbxrckhgdcHzLIiSR2HrNbQlFehRDZNFXUhFBMDzO0R/U/DcbIHP5DuHgC uf6qaZJDQPA1BiEhK27W50lgE/FTeaFxIYxBYxfVs/HbPN4VghgOYXgh4/FuHk9YL29u 4+8HSq+0zPZIZFmyi9tLwkPiOn6yf2mRnDZUg+e7sijf6MmXKE9jAX6c/hvypt3Bxp1X 3+o7jfMyuNEcsrodxxx5VjydD931GW/oP6qsDjUrJZyYoimdFvf7dT20Am4CxqhMT+4D PHLdv604MEvnOb4NMZpUz/tHjMzxOPuR0573OexP7tmGIMn6qz78wZRfH4KNQKfh8TMG JJWg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692801396; x=1693406196; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=/WLSfK19yGazCSe/ro0dcEq/1cOt60o3gtnCvbViYtI=; b=ZzMAt8he05UE9q2sxTFUrAEf2JMvf742c0/ZCKxyTRBcRtUAod6WZgURM6Wq+ydMds cehK3/Dpj7F96OviZKCVO1bhuZUexGVQEZGeHcjGcFRmQjdWegdrQf0sFubb005pPdVJ Q+HNOtRkF5vt8hYn3fY8vN/misUyN/gXjeXjYSbKIFEdW+pBeE83H0bVa2GLlE2bdBiw lmlOWaO7OY7tLZAFyQs1bpMwBhauwU3ouYmfr2Gefs8W27wcE0LqQ2PItJZOof/ykS0/ i+/6pjCZVjo9AJzJLutb4n+CnM/oOyK+bV9huGwRaO5+3AB+19ioPdMQgtWKAiW1y+od sI6A== X-Gm-Message-State: AOJu0YxxDAoKCAA8bcvhHl97IdMf5MimRczvsTPFMIMbl9alBP7Gl33H aOctNuIB9ftky3+Epw7JAaHs4fotrGgiWiZsC1DJaA== X-Google-Smtp-Source: AGHT+IHCgnesWE5gl0naKICAJk2CYWcG15+Maj1rInBUsG82XNC8pGXQZILHD5fB1GH2Z81u0Fb2DA== X-Received: by 2002:ac2:551d:0:b0:500:9a45:638 with SMTP id j29-20020ac2551d000000b005009a450638mr731052lfk.2.1692801396348; Wed, 23 Aug 2023 07:36:36 -0700 (PDT) Received: from [192.168.1.101] (abyj76.neoplus.adsl.tpnet.pl. [83.9.29.76]) by smtp.gmail.com with ESMTPSA id z6-20020ac24186000000b004fe36bae2d6sm2668882lfh.81.2023.08.23.07.36.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 23 Aug 2023 07:36:36 -0700 (PDT) From: Konrad Dybcio Date: Wed, 23 Aug 2023 16:36:15 +0200 Subject: [PATCH RESEND v2 3/3] power: supply: Introduce MM8013 fuel gauge driver MIME-Version: 1.0 Message-Id: <20230621-topic-mm8013-v2-3-9f1b41f4bc06@linaro.org> References: <20230621-topic-mm8013-v2-0-9f1b41f4bc06@linaro.org> In-Reply-To: <20230621-topic-mm8013-v2-0-9f1b41f4bc06@linaro.org> To: Rob Herring , Krzysztof Kozlowski , Conor Dooley , Sebastian Reichel , Konrad Dybcio Cc: Marijn Suijten , Rob Herring , devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-pm@vger.kernel.org, Konrad Dybcio X-Mailer: b4 0.12.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1692801391; l=10078; i=konrad.dybcio@linaro.org; s=20230215; h=from:subject:message-id; bh=eshR2h/oIe5G8JJ1PUcWTKUPAzP3s/bvABQOjvQ3Xmw=; b=F6KjSoCA3CwkobXpd7Tmhjynh60xXgBmjkhhItBslWKspmCYXFKr74yphpC3yUclTSWS4gPwf jzshNbt0NZ9DqXsbg/n9mZGiXgDPPktyPVAmMpbz4bMWMmo/XvygIOO X-Developer-Key: i=konrad.dybcio@linaro.org; a=ed25519; pk=iclgkYvtl2w05SSXO5EjjSYlhFKsJ+5OSZBjOkQuEms= Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org Add a driver for the Mitsumi MM8013 fuel gauge. The driver is a vastly cleaned up and improved version of the one that shipped in some obscure Lenovo downstream kernel [1], with some register definitions borrowed from ChromeOS EC platform code [2]. [1] https://github.com/adazem009/kernel_lenovo_bengal/commit/b6b346427a871715709bd22aae449b9383f3b66b [2] https://chromium.googlesource.com/chromiumos/platform/ec/+/master/driver/battery/mm8013.h Signed-off-by: Konrad Dybcio --- MAINTAINERS | 5 + drivers/power/supply/Kconfig | 9 ++ drivers/power/supply/Makefile | 1 + drivers/power/supply/mm8013.c | 283 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 298 insertions(+) diff --git a/MAINTAINERS b/MAINTAINERS index 5ce188b58eaa..ba5f075a2ca8 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -14296,6 +14296,11 @@ W: https://linuxtv.org T: git git://linuxtv.org/media_tree.git F: drivers/media/radio/radio-miropcm20* +MITSUMI MM8013 FG DRIVER +M: Konrad Dybcio +F: Documentation/devicetree/bindings/power/supply/mitsumi,mm8013.yaml +F: drivers/power/supply/mm8013.c + MMP SUPPORT R: Lubomir Rintel L: linux-arm-kernel@lists.infradead.org (moderated for non-subscribers) diff --git a/drivers/power/supply/Kconfig b/drivers/power/supply/Kconfig index 663a1c423806..c19e8287d80f 100644 --- a/drivers/power/supply/Kconfig +++ b/drivers/power/supply/Kconfig @@ -951,4 +951,13 @@ config CHARGER_QCOM_SMB2 adds support for the SMB2 switch mode battery charger found in PMI8998 and related PMICs. +config FUEL_GAUGE_MM8013 + tristate "Mitsumi MM8013 fuel gauge driver" + depends on I2C + help + Say Y here to enable the Mitsumi MM8013 fuel gauge driver. + It enables the monitoring of many battery parameters, including + the state of charge, temperature, cycle count, actual and design + capacity, etc. + endif # POWER_SUPPLY diff --git a/drivers/power/supply/Makefile b/drivers/power/supply/Makefile index a8a9fa6de1e9..ba2c41f060be 100644 --- a/drivers/power/supply/Makefile +++ b/drivers/power/supply/Makefile @@ -111,3 +111,4 @@ obj-$(CONFIG_BATTERY_SURFACE) += surface_battery.o obj-$(CONFIG_CHARGER_SURFACE) += surface_charger.o obj-$(CONFIG_BATTERY_UG3105) += ug3105_battery.o obj-$(CONFIG_CHARGER_QCOM_SMB2) += qcom_pmi8998_charger.o +obj-$(CONFIG_FUEL_GAUGE_MM8013) += mm8013.o diff --git a/drivers/power/supply/mm8013.c b/drivers/power/supply/mm8013.c new file mode 100644 index 000000000000..ce20c6078116 --- /dev/null +++ b/drivers/power/supply/mm8013.c @@ -0,0 +1,283 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (c) 2016-2019 The Linux Foundation. All rights reserved. + * Copyright (c) 2023, Linaro Limited + */ +#include +#include +#include + +#define REG_BATID 0x00 /* This one is very unclear */ + #define BATID_101 0x0101 /* 107kOhm */ + #define BATID_102 0x0102 /* 10kOhm */ +#define REG_TEMPERATURE 0x06 +#define REG_VOLTAGE 0x08 +#define REG_FLAGS 0x0a + #define MM8013_FLAG_OTC BIT(15) + #define MM8013_FLAG_OTD BIT(14) + #define MM8013_FLAG_BATHI BIT(13) + #define MM8013_FLAG_FC BIT(9) + #define MM8013_FLAG_CHG BIT(8) + #define MM8013_FLAG_DSG BIT(0) +#define REG_FULL_CHARGE_CAPACITY 0x0e +#define REG_AVERAGE_CURRENT 0x14 +#define REG_AVERAGE_TIME_TO_EMPTY 0x16 +#define REG_AVERAGE_TIME_TO_FULL 0x18 +#define REG_CYCLE_COUNT 0x2a +#define REG_STATE_OF_CHARGE 0x2c +#define REG_DESIGN_CAPACITY 0x3c +/* TODO: 0x62-0x68 seem to contain 'MM8013C' in a length-prefixed, non-terminated string */ + +#define DECIKELVIN_TO_DECIDEGC(t) (t - 2731) + +struct mm8013_chip { + struct i2c_client *client; +}; + +static int mm8013_write_reg(struct i2c_client *client, u8 reg, u16 value) +{ + int ret; + + ret = i2c_smbus_write_word_data(client, reg, value); + if (ret < 0) + dev_err(&client->dev, "%s: err %d\n", __func__, ret); + + usleep_range(4000, 5000); + return ret; +} + +static int mm8013_read_reg(struct i2c_client *client, u8 reg) +{ + int ret; + + ret = i2c_smbus_read_word_data(client, reg); + if (ret < 0) + dev_err(&client->dev, "%s: err %d\n", __func__, ret); + + usleep_range(4000, 5000); + return ret; +} + +static int mm8013_checkdevice(struct mm8013_chip *chip) +{ + int battery_id, ret; + + ret = mm8013_write_reg(chip->client, REG_BATID, 0x0008); + if (ret < 0) + return ret; + + ret = mm8013_read_reg(chip->client, REG_BATID); + if (ret < 0) + return ret; + + if (ret == BATID_102) + battery_id = 2; + else if (ret == BATID_101) + battery_id = 1; + else + return -EINVAL; + + dev_dbg(&chip->client->dev, "battery_id: %d\n", battery_id); + + return 0; +} + +static enum power_supply_property mm8013_battery_props[] = { + POWER_SUPPLY_PROP_CAPACITY, + POWER_SUPPLY_PROP_CHARGE_FULL, + POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, + POWER_SUPPLY_PROP_CURRENT_NOW, + POWER_SUPPLY_PROP_CYCLE_COUNT, + POWER_SUPPLY_PROP_HEALTH, + POWER_SUPPLY_PROP_PRESENT, + POWER_SUPPLY_PROP_STATUS, + POWER_SUPPLY_PROP_TEMP, + POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG, + POWER_SUPPLY_PROP_TIME_TO_FULL_AVG, + POWER_SUPPLY_PROP_VOLTAGE_NOW, +}; + +static int mm8013_get_property(struct power_supply *psy, + enum power_supply_property psp, + union power_supply_propval *val) +{ + struct mm8013_chip *chip = psy->drv_data; + struct i2c_client *client = chip->client; + int ret = 0; + + switch (psp) { + case POWER_SUPPLY_PROP_CAPACITY: + ret = mm8013_read_reg(client, REG_STATE_OF_CHARGE); + if (ret < 0) + return ret; + + val->intval = ret; + break; + case POWER_SUPPLY_PROP_CHARGE_FULL: + ret = mm8013_read_reg(client, REG_FULL_CHARGE_CAPACITY); + if (ret < 0) + return ret; + + val->intval = ret; + break; + case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: + ret = mm8013_read_reg(client, REG_DESIGN_CAPACITY); + if (ret < 0) + return ret; + + val->intval = ret; + break; + case POWER_SUPPLY_PROP_CURRENT_NOW: + ret = mm8013_read_reg(client, REG_AVERAGE_CURRENT); + if (ret < 0) + return ret; + + if (ret > S16_MAX) + val->intval -= (1 << 16); + else + val->intval = ret; + + val->intval *= -1000; + break; + case POWER_SUPPLY_PROP_CYCLE_COUNT: + ret = mm8013_read_reg(client, REG_CYCLE_COUNT); + if (ret < 0) + return ret; + + val->intval = ret; + break; + case POWER_SUPPLY_PROP_HEALTH: + ret = mm8013_read_reg(client, REG_FLAGS); + if (ret < 0) + return ret; + + if (ret & MM8013_FLAG_BATHI) + val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE; + else if (ret & (MM8013_FLAG_OTD | MM8013_FLAG_OTC)) + val->intval = POWER_SUPPLY_HEALTH_OVERHEAT; + else + val->intval = POWER_SUPPLY_HEALTH_GOOD; + break; + case POWER_SUPPLY_PROP_PRESENT: + val->intval = mm8013_read_reg(client, REG_TEMPERATURE) > 0; + break; + case POWER_SUPPLY_PROP_STATUS: + ret = mm8013_read_reg(client, REG_FLAGS); + if (ret < 0) + return ret; + + if (ret & MM8013_FLAG_DSG) + val->intval = POWER_SUPPLY_STATUS_DISCHARGING; + else if (ret & MM8013_FLAG_CHG) + val->intval = POWER_SUPPLY_STATUS_CHARGING; + else if (ret & MM8013_FLAG_FC) + val->intval = POWER_SUPPLY_STATUS_FULL; + else + val->intval = POWER_SUPPLY_STATUS_UNKNOWN; + break; + case POWER_SUPPLY_PROP_TEMP: + ret = mm8013_read_reg(client, REG_TEMPERATURE); + if (ret < 0) + return ret; + + val->intval = DECIKELVIN_TO_DECIDEGC(ret); + break; + case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG: + ret = mm8013_read_reg(client, REG_AVERAGE_TIME_TO_EMPTY); + if (ret < 0) + return ret; + + /* The estimation is not yet ready */ + if (ret == U16_MAX) + return -ENODATA; + + val->intval = ret; + break; + case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG: + ret = mm8013_read_reg(client, REG_AVERAGE_TIME_TO_FULL); + if (ret < 0) + return ret; + + /* The estimation is not yet ready */ + if (ret == U16_MAX) + return -ENODATA; + + val->intval = ret; + break; + case POWER_SUPPLY_PROP_VOLTAGE_NOW: + ret = mm8013_read_reg(client, REG_VOLTAGE); + if (ret < 0) + return ret; + + val->intval = ret; + break; + default: + return -EINVAL; + } + + return 0; +} + +static const struct power_supply_desc mm8013_desc = { + .name = "mm8013", + .type = POWER_SUPPLY_TYPE_BATTERY, + .properties = mm8013_battery_props, + .num_properties = ARRAY_SIZE(mm8013_battery_props), + .get_property = mm8013_get_property, +}; + +static int mm8013_probe(struct i2c_client *client) +{ + struct power_supply_config psy_cfg = {}; + struct device *dev = &client->dev; + struct power_supply *psy; + struct mm8013_chip *chip; + int ret = 0; + + if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WORD_DATA)) + return dev_err_probe(dev, -EIO, + "I2C_FUNC_SMBUS_WORD_DATA not supported\n"); + + chip = devm_kzalloc(dev, sizeof(struct mm8013_chip), GFP_KERNEL); + if (!chip) + return -ENOMEM; + + chip->client = client; + + ret = mm8013_checkdevice(chip); + if (ret) + return dev_err_probe(dev, -ENODEV, "MM8013 not found\n"); + + psy_cfg.drv_data = chip; + psy_cfg.of_node = dev->of_node; + + psy = devm_power_supply_register(dev, &mm8013_desc, &psy_cfg); + if (IS_ERR(psy)) + return PTR_ERR(psy); + + return 0; +} + +static const struct i2c_device_id mm8013_id_table[] = { + { "mm8013", 0 }, + {}, +}; +MODULE_DEVICE_TABLE(i2c, mm8013_id_table); + +static const struct of_device_id mm8013_match_table[] = { + { .compatible = "mitsumi,mm8013" }, + { }, +}; + +static struct i2c_driver mm8013_i2c_driver = { + .probe = mm8013_probe, + .id_table = mm8013_id_table, + .driver = { + .name = "mm8013", + .of_match_table = mm8013_match_table, + }, +}; +module_i2c_driver(mm8013_i2c_driver); + +MODULE_DESCRIPTION("MM8013 fuel gauge driver"); +MODULE_LICENSE("GPL");