From patchwork Fri Mar 17 09:55:26 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hans de Goede X-Patchwork-Id: 740207 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 3vl14f1gYSz9s2Q for ; Fri, 17 Mar 2017 20:58:26 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751354AbdCQJ6I (ORCPT ); Fri, 17 Mar 2017 05:58:08 -0400 Received: from mx1.redhat.com ([209.132.183.28]:56014 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751087AbdCQJ4t (ORCPT ); Fri, 17 Mar 2017 05:56:49 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 401EEC00B907; Fri, 17 Mar 2017 09:56:12 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 401EEC00B907 Authentication-Results: ext-mx07.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx07.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=hdegoede@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 401EEC00B907 Received: from shalem.localdomain.com (ovpn-116-172.ams2.redhat.com [10.36.116.172]) by smtp.corp.redhat.com (Postfix) with ESMTP id C9DDD5C3FA; Fri, 17 Mar 2017 09:56:09 +0000 (UTC) From: Hans de Goede To: "Rafael J . Wysocki" , Len Brown , Wolfram Sang , Andy Shevchenko , Lee Jones , Sebastian Reichel , MyungJoo Ham , Chanwoo Choi Cc: Hans de Goede , linux-acpi@vger.kernel.org, Takashi Iwai , linux-i2c@vger.kernel.org, linux-kernel@vger.kernel.org, linux-pm@vger.kernel.org Subject: [PATCH 14/15] power: supply: Add driver for Cherry Trail Whiskey Cove PMIC Fuel Gauge Date: Fri, 17 Mar 2017 10:55:26 +0100 Message-Id: <20170317095527.10487-15-hdegoede@redhat.com> In-Reply-To: <20170317095527.10487-1-hdegoede@redhat.com> References: <20170317095527.10487-1-hdegoede@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Fri, 17 Mar 2017 09:56:12 +0000 (UTC) Sender: linux-i2c-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-i2c@vger.kernel.org Add a driver for the Cherry Trail Whiskey Cove PMIC Fuel Gauge, note the Cherry Trail Whiskey Cove PMIC Fuel Gauge block is purely a fuel gauge and not a full battery controller. As such it offers a platform_data callback for extra power_supply properties for the actual external-charger ic driver and does not register a power_supply itself. Signed-off-by: Hans de Goede --- drivers/power/supply/Kconfig | 9 ++ drivers/power/supply/Makefile | 1 + drivers/power/supply/cht_wc_fuel_gauge.c | 209 +++++++++++++++++++++++++++++++ include/linux/power/cht_wc_fuel_gauge.h | 21 ++++ 4 files changed, 240 insertions(+) create mode 100644 drivers/power/supply/cht_wc_fuel_gauge.c create mode 100644 include/linux/power/cht_wc_fuel_gauge.h diff --git a/drivers/power/supply/Kconfig b/drivers/power/supply/Kconfig index fd93110..34ebfca 100644 --- a/drivers/power/supply/Kconfig +++ b/drivers/power/supply/Kconfig @@ -538,4 +538,13 @@ config AXP20X_POWER This driver provides support for the power supply features of AXP20x PMIC. +config CHT_WC_FUEL_GAUGE + tristate "Intel Cherry Trail Whiskey Cove PMIC Fuel Gauge" + depends on INTEL_SOC_PMIC_CHTWC + help + This adds support for the battery fuel gauge found in the Intel + Cherry Trail Whiskey Cove PMIC. This driver allows monitoring + of the charge level of the battery on Intel Cherry Trail systems + with a Whiskey Cove PMIC. + endif # POWER_SUPPLY diff --git a/drivers/power/supply/Makefile b/drivers/power/supply/Makefile index 3789a2c..702e28a 100644 --- a/drivers/power/supply/Makefile +++ b/drivers/power/supply/Makefile @@ -74,3 +74,4 @@ obj-$(CONFIG_CHARGER_TPS65090) += tps65090-charger.o obj-$(CONFIG_CHARGER_TPS65217) += tps65217_charger.o obj-$(CONFIG_AXP288_FUEL_GAUGE) += axp288_fuel_gauge.o obj-$(CONFIG_AXP288_CHARGER) += axp288_charger.o +obj-$(CONFIG_CHT_WC_FUEL_GAUGE) += cht_wc_fuel_gauge.o diff --git a/drivers/power/supply/cht_wc_fuel_gauge.c b/drivers/power/supply/cht_wc_fuel_gauge.c new file mode 100644 index 0000000..56f6e5a --- /dev/null +++ b/drivers/power/supply/cht_wc_fuel_gauge.c @@ -0,0 +1,209 @@ +/* + * Intel CHT Whiskey Cove Fuel Gauge driver + * Copyright (C) 2017 Hans de Goede + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License version + * 2 as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * Cherrytrail Whiskey Cove devices have 2 functional blocks which interact + * with the battery. + * + * 1) The fuel-gauge which is build into the Whiskey Cove PMIC, but has its + * own i2c bus and i2c client addresses separately from the rest of the PMIC. + * That block is what this driver is for. + * + * 2) An external charger IC, which is connected to the SMBUS controller + * which is part of the rest of the Whiskey Cove PMIC, mfd/intel_cht_wc.c + * registers a platform device for the SMBUS controller and + * i2c/busses/i2c-cht-wc.c contains the i2c-adapter driver for this. + * + * However we want to present this as a single power_supply device to + * userspace. So this driver offers a callback to get the fuel-gauge + * power_supply properties, which gets passed to the external charger + * driver via i2c_board_info when i2c-cht-wc.c calls i2c_new_device(). + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define REG_CHARGE_NOW 0x05 +#define REG_VOLTAGE_NOW 0x09 +#define REG_CURRENT_NOW 0x0a +#define REG_CURRENT_AVG 0x0b +#define REG_CHARGE_FULL 0x10 +#define REG_CHARGE_DESIGN 0x18 +#define REG_VOLTAGE_AVG 0x19 +#define REG_VOLTAGE_OCV 0x1b /* Only updated during charging */ + +#define CHT_WC_FG_PTYPE 4 + +struct cht_wc_fg_data { + struct device *dev; + struct i2c_client *client; +}; + +static DEFINE_MUTEX(cht_wc_fg_mutex); +static struct cht_wc_fg_data *cht_wc_fg; + +static int cht_wc_fg_read(struct cht_wc_fg_data *fg, u8 reg, + union power_supply_propval *val, int scale, + int sign_extend) +{ + int ret; + + ret = i2c_smbus_read_word_data(fg->client, reg); + if (ret < 0) + return ret; + + if (sign_extend) + ret = sign_extend32(ret, 15); + + val->intval = ret * scale; + + return 0; +} + +int cht_wc_fg_get_property(enum power_supply_property prop, + union power_supply_propval *val) +{ + int ret = 0; + + mutex_lock(&cht_wc_fg_mutex); + + if (!cht_wc_fg) { + ret = -ENXIO; + goto out_unlock; + } + + switch (prop) { + case POWER_SUPPLY_PROP_VOLTAGE_NOW: + ret = cht_wc_fg_read(cht_wc_fg, REG_VOLTAGE_NOW, val, 75, 0); + break; + case POWER_SUPPLY_PROP_VOLTAGE_AVG: + ret = cht_wc_fg_read(cht_wc_fg, REG_VOLTAGE_AVG, val, 75, 0); + break; + case POWER_SUPPLY_PROP_VOLTAGE_OCV: + ret = cht_wc_fg_read(cht_wc_fg, REG_VOLTAGE_OCV, val, 75, 0); + break; + case POWER_SUPPLY_PROP_CURRENT_NOW: + ret = cht_wc_fg_read(cht_wc_fg, REG_CURRENT_NOW, val, 150, 1); + break; + case POWER_SUPPLY_PROP_CURRENT_AVG: + ret = cht_wc_fg_read(cht_wc_fg, REG_CURRENT_AVG, val, 150, 1); + break; + case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: + ret = cht_wc_fg_read(cht_wc_fg, REG_CHARGE_DESIGN, val, 500, 0); + break; + case POWER_SUPPLY_PROP_CHARGE_FULL: + ret = cht_wc_fg_read(cht_wc_fg, REG_CHARGE_FULL, val, 500, 0); + break; + case POWER_SUPPLY_PROP_CHARGE_NOW: + ret = cht_wc_fg_read(cht_wc_fg, REG_CHARGE_NOW, val, 500, 0); + break; + default: + ret = -ENODATA; + } +out_unlock: + mutex_unlock(&cht_wc_fg_mutex); + return ret; +} +EXPORT_SYMBOL_GPL(cht_wc_fg_get_property); + +static int cht_wc_fg_probe(struct i2c_client *client, + const struct i2c_device_id *i2c_id) +{ + struct device *dev = &client->dev; + struct cht_wc_fg_data *fg; + acpi_status status; + unsigned long long ptyp; + + fg = devm_kzalloc(dev, sizeof(*fg), GFP_KERNEL); + if (!fg) + return -ENOMEM; + + status = acpi_evaluate_integer(ACPI_HANDLE(dev), "PTYP", NULL, &ptyp); + if (ACPI_FAILURE(status)) { + dev_err(dev, "Failed to get PTYPE\n"); + return -ENODEV; + } + + /* + * The same ACPI HID is used with different PMICs check PTYP to + * ensure that we are dealing with a Whiskey Cove PMIC. + */ + if (ptyp != CHT_WC_FG_PTYPE) + return -ENODEV; + + fg->dev = dev; + /* + * The current resource settings table for the fuel gauge contains + * multiple i2c devices on 2 different i2c-busses. The one we actually + * want is the second resource (index 1). + */ + fg->client = i2c_acpi_new_device(dev, 1); + if (!fg->client) + return -EPROBE_DEFER; + + i2c_set_clientdata(client, fg); + + mutex_lock(&cht_wc_fg_mutex); + cht_wc_fg = fg; + mutex_unlock(&cht_wc_fg_mutex); + + return 0; +} + +static int cht_wc_fg_remove(struct i2c_client *i2c) +{ + struct cht_wc_fg_data *fg = i2c_get_clientdata(i2c); + + mutex_lock(&cht_wc_fg_mutex); + cht_wc_fg = NULL; + mutex_unlock(&cht_wc_fg_mutex); + + i2c_unregister_device(fg->client); + + return 0; +} + +static const struct i2c_device_id cht_wc_fg_i2c_id[] = { + { } +}; +MODULE_DEVICE_TABLE(i2c, cht_wc_fg_i2c_id); + +static const struct acpi_device_id cht_wc_fg_acpi_ids[] = { + { "INT33FE", }, + { } +}; +MODULE_DEVICE_TABLE(acpi, cht_wc_fg_acpi_ids); + +static struct i2c_driver cht_wc_fg_driver = { + .driver = { + .name = "CHT Whiskey Cove PMIC Fuel Gauge", + .acpi_match_table = ACPI_PTR(cht_wc_fg_acpi_ids), + }, + .probe = cht_wc_fg_probe, + .remove = cht_wc_fg_remove, + .id_table = cht_wc_fg_i2c_id, + .irq_index = 1, +}; + +module_i2c_driver(cht_wc_fg_driver); + +MODULE_DESCRIPTION("Intel CHT Whiskey Cove PMIC Fuel Gauge driver"); +MODULE_AUTHOR("Hans de Goede "); +MODULE_LICENSE("GPL"); diff --git a/include/linux/power/cht_wc_fuel_gauge.h b/include/linux/power/cht_wc_fuel_gauge.h new file mode 100644 index 0000000..4b4e051 --- /dev/null +++ b/include/linux/power/cht_wc_fuel_gauge.h @@ -0,0 +1,21 @@ +/* + * Intel CHT Whiskey Cove Fuel Gauge driver + * Copyright (C) 2017 Hans de Goede + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License version + * 2 as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +#ifndef __CHT_WC_FUEL_GAUGE_H +#define __CHT_WC_FUEL_GAUGE_H + +int cht_wc_fg_get_property(enum power_supply_property prop, + union power_supply_propval *val); + +#endif