aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHans de Goede <hdegoede@redhat.com>2017-08-30 05:48:10 -0400
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>2017-08-31 12:26:10 -0400
commit4046c06fb11cbb5d90324da2df7e2acafcb9826c (patch)
treee0a4c755095cd76905bc99fdc6f3e64b33713d7b
parent382099d6bcffa13ea729ad5c8f0ae67fafbe563f (diff)
staging: typec: fusb302: Export current-limit through a power_supply class dev
The fusb302 Type-C port-controller cannot control the current-limit directly, so we need to exported the limit so that another driver (e.g. the charger driver) can pick the limit up and configure the system accordingly. The power-supply subsys already provides infrastructure for this, power-supply devices have the notion of being supplied by another power-supply and have properties through which we can export the current-limit. Register a power_supply and export the current-limit through the power_supply's current-max property. Cc: "Yueyao (Nathan) Zhu" <yueyao@google.com> Signed-off-by: Hans de Goede <hdegoede@redhat.com> Reviewed-by: Guenter Roeck <linux@roeck-us.net> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
-rw-r--r--drivers/staging/typec/fusb302/Kconfig2
-rw-r--r--drivers/staging/typec/fusb302/fusb302.c63
2 files changed, 62 insertions, 3 deletions
diff --git a/drivers/staging/typec/fusb302/Kconfig b/drivers/staging/typec/fusb302/Kconfig
index fce099ff39fe..48a4f2fcee03 100644
--- a/drivers/staging/typec/fusb302/Kconfig
+++ b/drivers/staging/typec/fusb302/Kconfig
@@ -1,6 +1,6 @@
1config TYPEC_FUSB302 1config TYPEC_FUSB302
2 tristate "Fairchild FUSB302 Type-C chip driver" 2 tristate "Fairchild FUSB302 Type-C chip driver"
3 depends on I2C 3 depends on I2C && POWER_SUPPLY
4 help 4 help
5 The Fairchild FUSB302 Type-C chip driver that works with 5 The Fairchild FUSB302 Type-C chip driver that works with
6 Type-C Port Controller Manager to provide USB PD and USB 6 Type-C Port Controller Manager to provide USB PD and USB
diff --git a/drivers/staging/typec/fusb302/fusb302.c b/drivers/staging/typec/fusb302/fusb302.c
index 6f007f66d597..cf6355f59cd9 100644
--- a/drivers/staging/typec/fusb302/fusb302.c
+++ b/drivers/staging/typec/fusb302/fusb302.c
@@ -28,6 +28,7 @@
28#include <linux/of_device.h> 28#include <linux/of_device.h>
29#include <linux/of_gpio.h> 29#include <linux/of_gpio.h>
30#include <linux/pinctrl/consumer.h> 30#include <linux/pinctrl/consumer.h>
31#include <linux/power_supply.h>
31#include <linux/proc_fs.h> 32#include <linux/proc_fs.h>
32#include <linux/regulator/consumer.h> 33#include <linux/regulator/consumer.h>
33#include <linux/sched/clock.h> 34#include <linux/sched/clock.h>
@@ -108,6 +109,11 @@ struct fusb302_chip {
108 /* lock for sharing chip states */ 109 /* lock for sharing chip states */
109 struct mutex lock; 110 struct mutex lock;
110 111
112 /* psy + psy status */
113 struct power_supply *psy;
114 u32 current_limit;
115 u32 supply_voltage;
116
111 /* chip status */ 117 /* chip status */
112 enum toggling_mode toggling_mode; 118 enum toggling_mode toggling_mode;
113 enum src_current_status src_current_status; 119 enum src_current_status src_current_status;
@@ -876,11 +882,13 @@ static int tcpm_set_vbus(struct tcpc_dev *dev, bool on, bool charge)
876 chip->vbus_on = on; 882 chip->vbus_on = on;
877 fusb302_log(chip, "vbus := %s", on ? "On" : "Off"); 883 fusb302_log(chip, "vbus := %s", on ? "On" : "Off");
878 } 884 }
879 if (chip->charge_on == charge) 885 if (chip->charge_on == charge) {
880 fusb302_log(chip, "charge is already %s", 886 fusb302_log(chip, "charge is already %s",
881 charge ? "On" : "Off"); 887 charge ? "On" : "Off");
882 else 888 } else {
883 chip->charge_on = charge; 889 chip->charge_on = charge;
890 power_supply_changed(chip->psy);
891 }
884 892
885done: 893done:
886 mutex_unlock(&chip->lock); 894 mutex_unlock(&chip->lock);
@@ -896,6 +904,11 @@ static int tcpm_set_current_limit(struct tcpc_dev *dev, u32 max_ma, u32 mv)
896 fusb302_log(chip, "current limit: %d ma, %d mv (not implemented)", 904 fusb302_log(chip, "current limit: %d ma, %d mv (not implemented)",
897 max_ma, mv); 905 max_ma, mv);
898 906
907 chip->supply_voltage = mv;
908 chip->current_limit = max_ma;
909
910 power_supply_changed(chip->psy);
911
899 return 0; 912 return 0;
900} 913}
901 914
@@ -1681,6 +1694,43 @@ done:
1681 return IRQ_HANDLED; 1694 return IRQ_HANDLED;
1682} 1695}
1683 1696
1697static int fusb302_psy_get_property(struct power_supply *psy,
1698 enum power_supply_property psp,
1699 union power_supply_propval *val)
1700{
1701 struct fusb302_chip *chip = power_supply_get_drvdata(psy);
1702
1703 switch (psp) {
1704 case POWER_SUPPLY_PROP_ONLINE:
1705 val->intval = chip->charge_on;
1706 break;
1707 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1708 val->intval = chip->supply_voltage * 1000; /* mV -> µV */
1709 break;
1710 case POWER_SUPPLY_PROP_CURRENT_MAX:
1711 val->intval = chip->current_limit * 1000; /* mA -> µA */
1712 break;
1713 default:
1714 return -ENODATA;
1715 }
1716
1717 return 0;
1718}
1719
1720static enum power_supply_property fusb302_psy_properties[] = {
1721 POWER_SUPPLY_PROP_ONLINE,
1722 POWER_SUPPLY_PROP_VOLTAGE_NOW,
1723 POWER_SUPPLY_PROP_CURRENT_MAX,
1724};
1725
1726const struct power_supply_desc fusb302_psy_desc = {
1727 .name = "fusb302-typec-source",
1728 .type = POWER_SUPPLY_TYPE_USB_TYPE_C,
1729 .properties = fusb302_psy_properties,
1730 .num_properties = ARRAY_SIZE(fusb302_psy_properties),
1731 .get_property = fusb302_psy_get_property,
1732};
1733
1684static int init_gpio(struct fusb302_chip *chip) 1734static int init_gpio(struct fusb302_chip *chip)
1685{ 1735{
1686 struct device_node *node; 1736 struct device_node *node;
@@ -1720,6 +1770,7 @@ static int fusb302_probe(struct i2c_client *client,
1720 struct fusb302_chip *chip; 1770 struct fusb302_chip *chip;
1721 struct i2c_adapter *adapter; 1771 struct i2c_adapter *adapter;
1722 struct device *dev = &client->dev; 1772 struct device *dev = &client->dev;
1773 struct power_supply_config cfg = {};
1723 const char *name; 1774 const char *name;
1724 int ret = 0; 1775 int ret = 0;
1725 u32 v; 1776 u32 v;
@@ -1766,6 +1817,14 @@ static int fusb302_probe(struct i2c_client *client,
1766 return -EPROBE_DEFER; 1817 return -EPROBE_DEFER;
1767 } 1818 }
1768 1819
1820 cfg.drv_data = chip;
1821 chip->psy = devm_power_supply_register(dev, &fusb302_psy_desc, &cfg);
1822 if (IS_ERR(chip->psy)) {
1823 ret = PTR_ERR(chip->psy);
1824 dev_err(chip->dev, "Error registering power-supply: %d\n", ret);
1825 return ret;
1826 }
1827
1769 ret = fusb302_debugfs_init(chip); 1828 ret = fusb302_debugfs_init(chip);
1770 if (ret < 0) 1829 if (ret < 0)
1771 return ret; 1830 return ret;