From patchwork Thu May 4 06:18:03 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Breck X-Patchwork-Id: 9710805 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 5546060387 for ; Thu, 4 May 2017 06:19:26 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 467C028557 for ; Thu, 4 May 2017 06:19:26 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 3B9572860E; Thu, 4 May 2017 06:19:26 +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.3 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI, RCVD_IN_SORBS_SPAM, 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 5526928557 for ; Thu, 4 May 2017 06:19:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754550AbdEDGTT (ORCPT ); Thu, 4 May 2017 02:19:19 -0400 Received: from mail-pg0-f67.google.com ([74.125.83.67]:32903 "EHLO mail-pg0-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753077AbdEDGTK (ORCPT ); Thu, 4 May 2017 02:19:10 -0400 Received: by mail-pg0-f67.google.com with SMTP id s62so889266pgc.0 for ; Wed, 03 May 2017 23:19:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=4H0GIb+TdN8faE400P35RaF7EX0XEibUkTpzEPVqmt4=; b=OLqKw+MI3nZvDiL2+Kac9NwmP3pvRuUYV61bpHq4VdnQuxgjXcQoK9FlZZgj3fsZms mgGnY+T583G6mOsm14sKA15wFXknnVZ/Yo5uCZDlQ4BAACBMQLnCSg7X93r4CYDKVB1c XJTCXrmjNYbErh0g9w6qgTCaOjvUGkAcYIQDlopYqx71PMo1eS/bazYDB93d/mkRJSn9 cO5aenjDRhuUO2A2KgnuzdWDszd3GspZW++4Nb+cAN66vfBCOskBTvU2GiWJ8SoIKNgD DKjlxX+uhngRZ+1dCRSresPioRUJOLRA2Nw1CmBu1eTZUY54KKkVk1eFalzdmztnwagl bOpA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=4H0GIb+TdN8faE400P35RaF7EX0XEibUkTpzEPVqmt4=; b=WqTJilknUAAXyP5e9R7i9zPD2wwt2AmSHgAbS6wuCK41yMhkS85n7VviMPAeXRr+64 Jp2vZnFgITceKNnuBX3XXaobx5g/uA0No1xu5MYNlP6FdPZ7VDOPUydkXMxO7y0whj5c UMVGezI2vEWltVD9ng3ew75VZr6uQPSJDw0nM/1dJaQaqJflzNGbWdP9LU1GWgH2IYaW 617BNN+iYssENe+XRn5C2S4ws6Un1ySrjoYyJv+gkF/Ew+1Uk1ydVCuLDcbVVyTQR2sY 4oLph5goCb9g+v064Y5ptFYkYBm2O5/nKjdxqkGHGg0J0KqpLXGuhIoL0rOc5M3wzx2f jnTw== X-Gm-Message-State: AN3rC/5xvPa7t/Ec1jddkuSRB6urkBuzvOp427HJYHGrzzr+4OyUNMA6 OkTJLMD3EotGHA== X-Received: by 10.98.10.71 with SMTP id s68mr8775102pfi.78.1493878749323; Wed, 03 May 2017 23:19:09 -0700 (PDT) Received: from localhost.localdomain (c-73-223-93-234.hsd1.ca.comcast.net. [73.223.93.234]) by smtp.gmail.com with ESMTPSA id f24sm1870455pfk.88.2017.05.03.23.19.08 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 03 May 2017 23:19:08 -0700 (PDT) From: Liam Breck To: Sebastian Reichel , "Andrew F. Davis" , linux-pm@vger.kernel.org Cc: Matt Ranostay , Liam Breck Subject: [PATCH v13 03/11] power: supply: core: Add power_supply_battery_info and API Date: Wed, 3 May 2017 23:18:03 -0700 Message-Id: <20170504061811.18107-4-liam@networkimprov.net> X-Mailer: git-send-email 2.9.3 In-Reply-To: <20170504061811.18107-1-liam@networkimprov.net> References: <20170504061811.18107-1-liam@networkimprov.net> Sender: linux-pm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Liam Breck power_supply_get_battery_info() reads battery data from devicetree. struct power_supply_battery_info provides battery data to drivers. Its fields correspond to elements in enum power_supply_property. Drivers may surface battery data in sysfs via corresponding POWER_SUPPLY_PROP_* fields. Signed-off-by: Matt Ranostay Signed-off-by: Liam Breck --- Documentation/power/power_supply_class.txt | 12 +++++++ drivers/power/supply/power_supply_core.c | 51 ++++++++++++++++++++++++++++++ include/linux/power_supply.h | 20 ++++++++++++ 3 files changed, 83 insertions(+) diff --git a/Documentation/power/power_supply_class.txt b/Documentation/power/power_supply_class.txt index 0c72588..01f0075 100644 --- a/Documentation/power/power_supply_class.txt +++ b/Documentation/power/power_supply_class.txt @@ -174,6 +174,18 @@ issued by external power supply will notify supplicants via external_power_changed callback. +Devicetree battery characteristics +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Drivers should call power_supply_get_battery_info() to obtain battery +characteristics from a devicetree battery node, defined in +Documentation/devicetree/bindings/power/supply/battery.txt. This is +implemented in drivers/power/supply/bq27xxx_battery.c. + +Properties in struct power_supply_battery_info and their counterparts in the +battery node have names corresponding to elements in enum power_supply_property, +for naming consistency between sysfs attributes and battery node properties. + + QA ~~ Q: Where is POWER_SUPPLY_PROP_XYZ attribute? diff --git a/drivers/power/supply/power_supply_core.c b/drivers/power/supply/power_supply_core.c index a74d8ca..8376a76 100644 --- a/drivers/power/supply/power_supply_core.c +++ b/drivers/power/supply/power_supply_core.c @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include "power_supply.h" @@ -487,6 +488,56 @@ struct power_supply *devm_power_supply_get_by_phandle(struct device *dev, EXPORT_SYMBOL_GPL(devm_power_supply_get_by_phandle); #endif /* CONFIG_OF */ +int power_supply_get_battery_info(struct power_supply *psy, + struct power_supply_battery_info *info) +{ + struct device_node *battery_np; + const char *value; + int err; + + info->energy_full_design_uwh = -EINVAL; + info->charge_full_design_uah = -EINVAL; + info->voltage_min_design_uv = -EINVAL; + info->precharge_current_ua = -EINVAL; + info->charge_term_current_ua = -EINVAL; + + if (!psy->of_node) { + dev_warn(&psy->dev, "%s currently only supports devicetree\n", + __func__); + return -ENXIO; + } + + battery_np = of_parse_phandle(psy->of_node, "monitored-battery", 0); + if (!battery_np) + return -ENODEV; + + err = of_property_read_string(battery_np, "compatible", &value); + if (err) + return err; + + if (strcmp("simple-battery", value)) + return -ENODEV; + + /* The property and field names below must correspond to elements + * in enum power_supply_property. For reasoning, see + * Documentation/power/power_supply_class.txt. + */ + + of_property_read_u32(battery_np, "energy-full-design-microwatt-hours", + &info->energy_full_design_uwh); + of_property_read_u32(battery_np, "charge-full-design-microamp-hours", + &info->charge_full_design_uah); + of_property_read_u32(battery_np, "voltage-min-design-microvolt", + &info->voltage_min_design_uv); + of_property_read_u32(battery_np, "precharge-current-microamp", + &info->precharge_current_ua); + of_property_read_u32(battery_np, "charge-term-current-microamp", + &info->charge_term_current_ua); + + return 0; +} +EXPORT_SYMBOL_GPL(power_supply_get_battery_info); + int power_supply_get_property(struct power_supply *psy, enum power_supply_property psp, union power_supply_propval *val) diff --git a/include/linux/power_supply.h b/include/linux/power_supply.h index 3965503..67f363b 100644 --- a/include/linux/power_supply.h +++ b/include/linux/power_supply.h @@ -288,6 +288,23 @@ struct power_supply_info { int use_for_apm; }; +/* + * This is the recommended struct to manage static battery parameters, + * populated by power_supply_get_battery_info(). Most platform drivers should + * use these for consistency. + * Its field names must correspond to elements in enum power_supply_property. + * The default field value is -EINVAL. + * Power supply class itself doesn't use this. + */ + +struct power_supply_battery_info { + int energy_full_design_uwh; /* microWatt-hours */ + int charge_full_design_uah; /* microAmp-hours */ + int voltage_min_design_uv; /* microVolts */ + int precharge_current_ua; /* microAmps */ + int charge_term_current_ua; /* microAmps */ +}; + extern struct atomic_notifier_head power_supply_notifier; extern int power_supply_reg_notifier(struct notifier_block *nb); extern void power_supply_unreg_notifier(struct notifier_block *nb); @@ -306,6 +323,9 @@ static inline struct power_supply * devm_power_supply_get_by_phandle(struct device *dev, const char *property) { return NULL; } #endif /* CONFIG_OF */ + +extern int power_supply_get_battery_info(struct power_supply *psy, + struct power_supply_battery_info *info); extern void power_supply_changed(struct power_supply *psy); extern int power_supply_am_i_supplied(struct power_supply *psy); extern int power_supply_set_battery_charged(struct power_supply *psy);