{"id":807548,"url":"http://patchwork.ozlabs.org/api/1.0/patches/807548/?format=json","project":{"id":35,"url":"http://patchwork.ozlabs.org/api/1.0/projects/35/?format=json","name":"Linux I2C development","link_name":"linux-i2c","list_id":"linux-i2c.vger.kernel.org","list_email":"linux-i2c@vger.kernel.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20170830094814.17758-8-hdegoede@redhat.com>","date":"2017-08-30T09:48:10","name":"[v3,07/11] staging: typec: fusb302: Export current-limit through a power_supply class dev","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"3c811cf92f514445be939bc9766790d79b0c2bd7","submitter":{"id":1893,"url":"http://patchwork.ozlabs.org/api/1.0/people/1893/?format=json","name":"Hans de Goede","email":"hdegoede@redhat.com"},"delegate":null,"mbox":"http://patchwork.ozlabs.org/project/linux-i2c/patch/20170830094814.17758-8-hdegoede@redhat.com/mbox/","series":[{"id":564,"url":"http://patchwork.ozlabs.org/api/1.0/series/564/?format=json","date":"2017-08-30T09:48:14","name":"Hookup typec power-negotation to the PMIC and charger","version":3,"mbox":"http://patchwork.ozlabs.org/series/564/mbox/"}],"check":"pending","checks":"http://patchwork.ozlabs.org/api/patches/807548/checks/","tags":{},"headers":{"Return-Path":"<linux-i2c-owner@vger.kernel.org>","X-Original-To":"incoming@patchwork.ozlabs.org","Delivered-To":"patchwork-incoming@bilbo.ozlabs.org","Authentication-Results":["ozlabs.org;\n\tspf=none (mailfrom) smtp.mailfrom=vger.kernel.org\n\t(client-ip=209.132.180.67; helo=vger.kernel.org;\n\tenvelope-from=linux-i2c-owner@vger.kernel.org;\n\treceiver=<UNKNOWN>)","ext-mx04.extmail.prod.ext.phx2.redhat.com;\n\tdmarc=none (p=none dis=none) header.from=redhat.com","ext-mx04.extmail.prod.ext.phx2.redhat.com;\n\tspf=fail smtp.mailfrom=hdegoede@redhat.com"],"Received":["from vger.kernel.org (vger.kernel.org [209.132.180.67])\n\tby ozlabs.org (Postfix) with ESMTP id 3xj13X0NQ6z9t0F\n\tfor <incoming@patchwork.ozlabs.org>;\n\tWed, 30 Aug 2017 19:51:04 +1000 (AEST)","(majordomo@vger.kernel.org) by vger.kernel.org via listexpand\n\tid S1751880AbdH3Jus (ORCPT <rfc822;incoming@patchwork.ozlabs.org>);\n\tWed, 30 Aug 2017 05:50:48 -0400","from mx1.redhat.com ([209.132.183.28]:54012 \"EHLO mx1.redhat.com\"\n\trhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP\n\tid S1751787AbdH3Jsm (ORCPT <rfc822;linux-i2c@vger.kernel.org>);\n\tWed, 30 Aug 2017 05:48:42 -0400","from smtp.corp.redhat.com\n\t(int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15])\n\t(using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits))\n\t(No client certificate requested)\n\tby mx1.redhat.com (Postfix) with ESMTPS id 3DAAD7F7AB;\n\tWed, 30 Aug 2017 09:48:42 +0000 (UTC)","from shalem.localdomain.com (ovpn-116-238.ams2.redhat.com\n\t[10.36.116.238])\n\tby smtp.corp.redhat.com (Postfix) with ESMTP id 1AE8987C9A;\n\tWed, 30 Aug 2017 09:48:38 +0000 (UTC)"],"DMARC-Filter":"OpenDMARC Filter v1.3.2 mx1.redhat.com 3DAAD7F7AB","From":"Hans de Goede <hdegoede@redhat.com>","To":"Darren Hart <dvhart@infradead.org>, Andy Shevchenko <andy@infradead.org>,\n\tWolfram Sang <wsa@the-dreams.de>, Sebastian Reichel <sre@kernel.org>,\n\tGreg Kroah-Hartman <gregkh@linuxfoundation.org>,\n\tGuenter Roeck <linux@roeck-us.net>,\n\tHeikki Krogerus <heikki.krogerus@linux.intel.com>","Cc":"Hans de Goede <hdegoede@redhat.com>,\n\tplatform-driver-x86@vger.kernel.org, linux-kernel@vger.kernel.org,\n\tlinux-i2c@vger.kernel.org, Liam Breck <liam@networkimprov.net>,\n\tTony Lindgren <tony@atomide.com>, linux-pm@vger.kernel.org,\n\tdevel@driverdev.osuosl.org, \"Yueyao (Nathan) Zhu\" <yueyao@google.com>","Subject":"[PATCH v3 07/11] staging: typec: fusb302: Export current-limit\n\tthrough a power_supply class dev","Date":"Wed, 30 Aug 2017 11:48:10 +0200","Message-Id":"<20170830094814.17758-8-hdegoede@redhat.com>","In-Reply-To":"<20170830094814.17758-1-hdegoede@redhat.com>","References":"<20170830094814.17758-1-hdegoede@redhat.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=UTF-8","Content-Transfer-Encoding":"8bit","X-Scanned-By":"MIMEDefang 2.79 on 10.5.11.15","X-Greylist":"Sender IP whitelisted, not delayed by milter-greylist-4.5.16\n\t(mx1.redhat.com [10.5.110.28]);\n\tWed, 30 Aug 2017 09:48:42 +0000 (UTC)","Sender":"linux-i2c-owner@vger.kernel.org","Precedence":"bulk","List-ID":"<linux-i2c.vger.kernel.org>","X-Mailing-List":"linux-i2c@vger.kernel.org"},"content":"The fusb302 Type-C port-controller cannot control the current-limit\ndirectly, so we need to exported the limit so that another driver\n(e.g. the charger driver) can pick the limit up and configure the\nsystem accordingly.\n\nThe power-supply subsys already provides infrastructure for this,\npower-supply devices have the notion of being supplied by another\npower-supply and have properties through which we can export the\ncurrent-limit.\n\nRegister a power_supply and export the current-limit through the\npower_supply's current-max property.\n\nCc: \"Yueyao (Nathan) Zhu\" <yueyao@google.com>\nSigned-off-by: Hans de Goede <hdegoede@redhat.com>\n---\nChanges in v2:\n-Put the psy class device code directly in fusb302.c rather then introducing\n helpers which are only used by fusb302.c\n-Add an online property to the psy so that upower does not mistake it for a\n second battery in the system\n---\n drivers/staging/typec/fusb302/Kconfig   |  2 +-\n drivers/staging/typec/fusb302/fusb302.c | 63 +++++++++++++++++++++++++++++++--\n 2 files changed, 62 insertions(+), 3 deletions(-)","diff":"diff --git a/drivers/staging/typec/fusb302/Kconfig b/drivers/staging/typec/fusb302/Kconfig\nindex fce099ff39fe..48a4f2fcee03 100644\n--- a/drivers/staging/typec/fusb302/Kconfig\n+++ b/drivers/staging/typec/fusb302/Kconfig\n@@ -1,6 +1,6 @@\n config TYPEC_FUSB302\n \ttristate \"Fairchild FUSB302 Type-C chip driver\"\n-\tdepends on I2C\n+\tdepends on I2C && POWER_SUPPLY\n \thelp\n \t  The Fairchild FUSB302 Type-C chip driver that works with\n \t  Type-C Port Controller Manager to provide USB PD and USB\ndiff --git a/drivers/staging/typec/fusb302/fusb302.c b/drivers/staging/typec/fusb302/fusb302.c\nindex 6f007f66d597..cf6355f59cd9 100644\n--- a/drivers/staging/typec/fusb302/fusb302.c\n+++ b/drivers/staging/typec/fusb302/fusb302.c\n@@ -28,6 +28,7 @@\n #include <linux/of_device.h>\n #include <linux/of_gpio.h>\n #include <linux/pinctrl/consumer.h>\n+#include <linux/power_supply.h>\n #include <linux/proc_fs.h>\n #include <linux/regulator/consumer.h>\n #include <linux/sched/clock.h>\n@@ -108,6 +109,11 @@ struct fusb302_chip {\n \t/* lock for sharing chip states */\n \tstruct mutex lock;\n \n+\t/* psy + psy status */\n+\tstruct power_supply *psy;\n+\tu32 current_limit;\n+\tu32 supply_voltage;\n+\n \t/* chip status */\n \tenum toggling_mode toggling_mode;\n \tenum src_current_status src_current_status;\n@@ -876,11 +882,13 @@ static int tcpm_set_vbus(struct tcpc_dev *dev, bool on, bool charge)\n \t\tchip->vbus_on = on;\n \t\tfusb302_log(chip, \"vbus := %s\", on ? \"On\" : \"Off\");\n \t}\n-\tif (chip->charge_on == charge)\n+\tif (chip->charge_on == charge) {\n \t\tfusb302_log(chip, \"charge is already %s\",\n \t\t\t    charge ? \"On\" : \"Off\");\n-\telse\n+\t} else {\n \t\tchip->charge_on = charge;\n+\t\tpower_supply_changed(chip->psy);\n+\t}\n \n done:\n \tmutex_unlock(&chip->lock);\n@@ -896,6 +904,11 @@ static int tcpm_set_current_limit(struct tcpc_dev *dev, u32 max_ma, u32 mv)\n \tfusb302_log(chip, \"current limit: %d ma, %d mv (not implemented)\",\n \t\t    max_ma, mv);\n \n+\tchip->supply_voltage = mv;\n+\tchip->current_limit = max_ma;\n+\n+\tpower_supply_changed(chip->psy);\n+\n \treturn 0;\n }\n \n@@ -1681,6 +1694,43 @@ static irqreturn_t fusb302_irq_intn(int irq, void *dev_id)\n \treturn IRQ_HANDLED;\n }\n \n+static int fusb302_psy_get_property(struct power_supply *psy,\n+\t\t\t\t    enum power_supply_property psp,\n+\t\t\t\t    union power_supply_propval *val)\n+{\n+\tstruct fusb302_chip *chip = power_supply_get_drvdata(psy);\n+\n+\tswitch (psp) {\n+\tcase POWER_SUPPLY_PROP_ONLINE:\n+\t\tval->intval = chip->charge_on;\n+\t\tbreak;\n+\tcase POWER_SUPPLY_PROP_VOLTAGE_NOW:\n+\t\tval->intval = chip->supply_voltage * 1000; /* mV -> µV */\n+\t\tbreak;\n+\tcase POWER_SUPPLY_PROP_CURRENT_MAX:\n+\t\tval->intval = chip->current_limit * 1000; /* mA -> µA */\n+\t\tbreak;\n+\tdefault:\n+\t\treturn -ENODATA;\n+\t}\n+\n+\treturn 0;\n+}\n+\n+static enum power_supply_property fusb302_psy_properties[] = {\n+\tPOWER_SUPPLY_PROP_ONLINE,\n+\tPOWER_SUPPLY_PROP_VOLTAGE_NOW,\n+\tPOWER_SUPPLY_PROP_CURRENT_MAX,\n+};\n+\n+const struct power_supply_desc fusb302_psy_desc = {\n+\t.name\t\t= \"fusb302-typec-source\",\n+\t.type\t\t= POWER_SUPPLY_TYPE_USB_TYPE_C,\n+\t.properties\t= fusb302_psy_properties,\n+\t.num_properties\t= ARRAY_SIZE(fusb302_psy_properties),\n+\t.get_property\t= fusb302_psy_get_property,\n+};\n+\n static int init_gpio(struct fusb302_chip *chip)\n {\n \tstruct device_node *node;\n@@ -1720,6 +1770,7 @@ static int fusb302_probe(struct i2c_client *client,\n \tstruct fusb302_chip *chip;\n \tstruct i2c_adapter *adapter;\n \tstruct device *dev = &client->dev;\n+\tstruct power_supply_config cfg = {};\n \tconst char *name;\n \tint ret = 0;\n \tu32 v;\n@@ -1766,6 +1817,14 @@ static int fusb302_probe(struct i2c_client *client,\n \t\t\treturn -EPROBE_DEFER;\n \t}\n \n+\tcfg.drv_data = chip;\n+\tchip->psy = devm_power_supply_register(dev, &fusb302_psy_desc, &cfg);\n+\tif (IS_ERR(chip->psy)) {\n+\t\tret = PTR_ERR(chip->psy);\n+\t\tdev_err(chip->dev, \"Error registering power-supply: %d\\n\", ret);\n+\t\treturn ret;\n+\t}\n+\n \tret = fusb302_debugfs_init(chip);\n \tif (ret < 0)\n \t\treturn ret;\n","prefixes":["v3","07/11"]}