From patchwork Mon Apr 30 13:14:02 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Enric Balletbo i Serra X-Patchwork-Id: 10371765 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 8F0B86032A for ; Mon, 30 Apr 2018 13:15:00 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7F8552871B for ; Mon, 30 Apr 2018 13:15:00 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 73940288EE; Mon, 30 Apr 2018 13:15:00 +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=-7.9 required=2.0 tests=BAYES_00, MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI, UNPARSEABLE_RELAY autolearn=unavailable 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 498522871B for ; Mon, 30 Apr 2018 13:14:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753644AbeD3NOQ (ORCPT ); Mon, 30 Apr 2018 09:14:16 -0400 Received: from bhuna.collabora.co.uk ([46.235.227.227]:51902 "EHLO bhuna.collabora.co.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753378AbeD3NOO (ORCPT ); Mon, 30 Apr 2018 09:14:14 -0400 Received: from [127.0.0.1] (localhost [127.0.0.1]) (Authenticated sender: eballetbo) with ESMTPSA id 239B4279E13 From: Enric Balletbo i Serra To: Lee Jones , Sebastian Reichel Cc: Gwendal Grignou , Sameer Nanda , linux-pm@vger.kernel.org, Guenter Roeck , linux-kernel@vger.kernel.org, Benson Leung , miguel.ojeda.sandonis@gmail.com Subject: [PATCH v2 2/3] power: supply: add cros-ec USBPD charger driver. Date: Mon, 30 Apr 2018 15:14:02 +0200 Message-Id: <20180430131403.14296-3-enric.balletbo@collabora.com> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180430131403.14296-1-enric.balletbo@collabora.com> References: <20180430131403.14296-1-enric.balletbo@collabora.com> 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: Sameer Nanda This driver gets various bits of information about what is connected to USB PD ports from the EC and converts that into power_supply properties. Signed-off-by: Sameer Nanda Signed-off-by: Enric Balletbo i Serra Acked-by: Lee Jones --- Changes in v2: - [2/3] Add SPDX header, use devm_ variant and drop .owner - [2/3] Removed the PD log functionality (will be send on a follow up patch) - [2/3] Removed the extra custom sysfs attributes (will be send on a follow up patch) drivers/power/supply/Kconfig | 10 + drivers/power/supply/Makefile | 1 + drivers/power/supply/cros_usbpd-charger.c | 511 ++++++++++++++++++++++ include/linux/mfd/cros_ec.h | 2 + 4 files changed, 524 insertions(+) create mode 100644 drivers/power/supply/cros_usbpd-charger.c diff --git a/drivers/power/supply/Kconfig b/drivers/power/supply/Kconfig index 428b426842f4..046eb23ba6f0 100644 --- a/drivers/power/supply/Kconfig +++ b/drivers/power/supply/Kconfig @@ -624,4 +624,14 @@ config CHARGER_RT9455 help Say Y to enable support for Richtek RT9455 battery charger. +config CHARGER_CROS_USBPD + tristate "ChromeOS EC based USBPD charger" + depends on MFD_CROS_EC + default n + help + Say Y here to enable ChromeOS EC based USBPD charger + driver. This driver gets various bits of information about + what is connected to USB PD ports from the EC and converts + that into power_supply properties. + endif # POWER_SUPPLY diff --git a/drivers/power/supply/Makefile b/drivers/power/supply/Makefile index e83aa843bcc6..907e26f824b2 100644 --- a/drivers/power/supply/Makefile +++ b/drivers/power/supply/Makefile @@ -83,3 +83,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_CHARGER_CROS_USBPD) += cros_usbpd-charger.o diff --git a/drivers/power/supply/cros_usbpd-charger.c b/drivers/power/supply/cros_usbpd-charger.c new file mode 100644 index 000000000000..c57d1c41828c --- /dev/null +++ b/drivers/power/supply/cros_usbpd-charger.c @@ -0,0 +1,511 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Power supply driver for ChromeOS EC based USB PD Charger. + * + * Copyright (c) 2014 - 2018 Google, Inc + */ + +#include +#include +#include +#include +#include +#include + +#define CHARGER_DIR_NAME "CROS_USBPD_CHARGER%d" +#define CHARGER_DIR_NAME_LENGTH sizeof(CHARGER_DIR_NAME) +#define CHARGER_CACHE_UPDATE_DELAY msecs_to_jiffies(500) +#define CHARGER_MANUFACTURER_MODEL_LENGTH 32 + +#define DRV_NAME "cros-usbpd-charger" + +struct port_data { + int port_number; + char name[CHARGER_DIR_NAME_LENGTH]; + char manufacturer[CHARGER_MANUFACTURER_MODEL_LENGTH]; + char model_name[CHARGER_MANUFACTURER_MODEL_LENGTH]; + struct power_supply *psy; + struct power_supply_desc psy_desc; + int psy_usb_type; + int psy_online; + int psy_status; + int psy_current_max; + int psy_voltage_max_design; + int psy_voltage_now; + int psy_power_max; + struct charger_data *charger; + unsigned long last_update; +}; + +struct charger_data { + struct device *dev; + struct cros_ec_dev *ec_dev; + struct cros_ec_device *ec_device; + int num_charger_ports; + int num_registered_psy; + struct port_data *ports[EC_USB_PD_MAX_PORTS]; + struct notifier_block notifier; +}; + +static enum power_supply_property cros_usbpd_charger_props[] = { + POWER_SUPPLY_PROP_ONLINE, + POWER_SUPPLY_PROP_STATUS, + POWER_SUPPLY_PROP_CURRENT_MAX, + POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN, + POWER_SUPPLY_PROP_VOLTAGE_NOW, + POWER_SUPPLY_PROP_MODEL_NAME, + POWER_SUPPLY_PROP_MANUFACTURER, +}; + +static int cros_usbpd_charger_ec_command(struct charger_data *charger, + unsigned int version, + unsigned int command, + void *outdata, + unsigned int outsize, + void *indata, + unsigned int insize) +{ + struct cros_ec_dev *ec_dev = charger->ec_dev; + struct cros_ec_command *msg; + int ret; + + msg = kzalloc(sizeof(*msg) + max(outsize, insize), GFP_KERNEL); + if (!msg) + return -ENOMEM; + + msg->version = version; + msg->command = ec_dev->cmd_offset + command; + msg->outsize = outsize; + msg->insize = insize; + + if (outsize) + memcpy(msg->data, outdata, outsize); + + ret = cros_ec_cmd_xfer_status(charger->ec_device, msg); + if (ret >= 0 && insize) + memcpy(indata, msg->data, insize); + + kfree(msg); + return ret; +} + +static int cros_usbpd_charger_get_num_ports(struct charger_data *charger) +{ + struct ec_response_usb_pd_ports resp; + int ret; + + ret = cros_usbpd_charger_ec_command(charger, 0, EC_CMD_USB_PD_PORTS, + NULL, 0, &resp, sizeof(resp)); + if (ret < 0) { + dev_err(charger->dev, + "Unable to get the number or ports (err:0x%x)\n", ret); + return ret; + } + + return resp.num_ports; +} + +static int cros_usbpd_charger_get_discovery_info(struct port_data *port) +{ + struct charger_data *charger = port->charger; + struct ec_params_usb_pd_discovery_entry resp; + struct ec_params_usb_pd_info_request req; + int ret; + + req.port = port->port_number; + + ret = cros_usbpd_charger_ec_command(charger, 0, + EC_CMD_USB_PD_DISCOVERY, + &req, sizeof(req), + &resp, sizeof(resp)); + if (ret < 0) { + dev_err(charger->dev, + "Unable to query discovery info (err:0x%x)\n", ret); + return ret; + } + + dev_dbg(charger->dev, "Port %d: VID = 0x%x, PID=0x%x, PTYPE=0x%x\n", + port->port_number, resp.vid, resp.pid, resp.ptype); + + snprintf(port->manufacturer, sizeof(port->manufacturer), "%x", + resp.vid); + snprintf(port->model_name, sizeof(port->model_name), "%x", resp.pid); + + return 0; +} + +static int cros_usbpd_charger_get_power_info(struct port_data *port) +{ + struct charger_data *charger = port->charger; + struct ec_response_usb_pd_power_info resp; + struct ec_params_usb_pd_power_info req; + int last_psy_status, last_psy_usb_type; + struct device *dev = charger->dev; + int ret; + + req.port = port->port_number; + ret = cros_usbpd_charger_ec_command(charger, 0, + EC_CMD_USB_PD_POWER_INFO, + &req, sizeof(req), + &resp, sizeof(resp)); + if (ret < 0) { + dev_err(dev, "Unable to query PD power info (err:0x%x)\n", ret); + return ret; + } + + last_psy_status = port->psy_status; + last_psy_usb_type = port->psy_usb_type; + + switch (resp.role) { + case USB_PD_PORT_POWER_DISCONNECTED: + port->psy_status = POWER_SUPPLY_STATUS_NOT_CHARGING; + port->psy_online = 0; + break; + case USB_PD_PORT_POWER_SOURCE: + port->psy_status = POWER_SUPPLY_STATUS_NOT_CHARGING; + port->psy_online = 0; + break; + case USB_PD_PORT_POWER_SINK: + port->psy_status = POWER_SUPPLY_STATUS_CHARGING; + port->psy_online = 1; + break; + case USB_PD_PORT_POWER_SINK_NOT_CHARGING: + port->psy_status = POWER_SUPPLY_STATUS_NOT_CHARGING; + port->psy_online = 1; + break; + default: + dev_err(dev, "Unknown role %d\n", resp.role); + break; + } + + port->psy_voltage_max_design = resp.meas.voltage_max; + port->psy_voltage_now = resp.meas.voltage_now; + port->psy_current_max = resp.meas.current_max; + port->psy_power_max = resp.max_power; + + switch (resp.type) { + case USB_CHG_TYPE_BC12_SDP: + case USB_CHG_TYPE_VBUS: + port->psy_usb_type = POWER_SUPPLY_USB_TYPE_SDP; + break; + case USB_CHG_TYPE_NONE: + /* + * For dual-role devices when we are a source, the firmware + * reports the type as NONE. Report such chargers as type + * USB_PD_DRP. + */ + if (resp.role == USB_PD_PORT_POWER_SOURCE && resp.dualrole) + port->psy_usb_type = POWER_SUPPLY_USB_TYPE_PD_DRP; + else + port->psy_usb_type = POWER_SUPPLY_USB_TYPE_SDP; + break; + case USB_CHG_TYPE_OTHER: + case USB_CHG_TYPE_PROPRIETARY: + port->psy_usb_type = POWER_SUPPLY_USB_TYPE_APPLE_BRICK_ID; + break; + case USB_CHG_TYPE_C: + port->psy_usb_type = POWER_SUPPLY_USB_TYPE_C; + break; + case USB_CHG_TYPE_BC12_DCP: + port->psy_usb_type = POWER_SUPPLY_USB_TYPE_DCP; + break; + case USB_CHG_TYPE_BC12_CDP: + port->psy_usb_type = POWER_SUPPLY_USB_TYPE_CDP; + break; + case USB_CHG_TYPE_PD: + if (resp.dualrole) + port->psy_usb_type = POWER_SUPPLY_USB_TYPE_PD_DRP; + else + port->psy_usb_type = POWER_SUPPLY_USB_TYPE_PD; + break; + case USB_CHG_TYPE_UNKNOWN: + /* + * While the EC is trying to determine the type of charger that + * has been plugged in, it will report the charger type as + * unknown. Additionally since the power capabilities are + * unknown, report the max current and voltage as zero. + */ + port->psy_usb_type = POWER_SUPPLY_USB_TYPE_UNKNOWN; + port->psy_voltage_max_design = 0; + port->psy_current_max = 0; + break; + default: + dev_err(dev, "Port %d: default case!\n", port->port_number); + port->psy_usb_type = POWER_SUPPLY_USB_TYPE_SDP; + } + + port->psy_desc.type = POWER_SUPPLY_TYPE_USB; + + dev_dbg(dev, + "Port %d: type=%d vmax=%d vnow=%d cmax=%d clim=%d pmax=%d\n", + port->port_number, resp.type, resp.meas.voltage_max, + resp.meas.voltage_now, resp.meas.current_max, + resp.meas.current_lim, resp.max_power); + + /* + * If power supply type or status changed, explicitly call + * power_supply_changed. This results in udev event getting generated + * and allows user mode apps to react quicker instead of waiting for + * their next poll of power supply status. + */ + if (last_psy_usb_type != port->psy_usb_type || + last_psy_status != port->psy_status) + power_supply_changed(port->psy); + + return 0; +} + +static int cros_usbpd_charger_get_port_status(struct port_data *port, + bool ratelimit) +{ + int ret; + + if (ratelimit && + time_is_after_jiffies(port->last_update + + CHARGER_CACHE_UPDATE_DELAY)) + return 0; + + ret = cros_usbpd_charger_get_power_info(port); + if (ret < 0) + return ret; + + ret = cros_usbpd_charger_get_discovery_info(port); + port->last_update = jiffies; + + return ret; +} + +static void cros_usbpd_charger_power_changed(struct power_supply *psy) +{ + struct port_data *port = power_supply_get_drvdata(psy); + struct charger_data *charger = port->charger; + int i; + + for (i = 0; i < charger->num_registered_psy; i++) + cros_usbpd_charger_get_port_status(charger->ports[i], false); +} + +static int cros_usbpd_charger_get_prop(struct power_supply *psy, + enum power_supply_property psp, + union power_supply_propval *val) +{ + struct port_data *port = power_supply_get_drvdata(psy); + struct charger_data *charger = port->charger; + struct cros_ec_device *ec_device = charger->ec_device; + struct device *dev = charger->dev; + int ret; + + /* Only refresh ec_port_status for dynamic properties */ + switch (psp) { + case POWER_SUPPLY_PROP_ONLINE: + /* + * If mkbp_event_supported, then we can be assured that + * the driver's state for the online property is consistent + * with the hardware. However, if we aren't event driven, + * the optimization before to skip an ec_port_status get + * and only returned cached values of the online property will + * cause a delay in detecting a cable attach until one of the + * other properties are read. + * + * Allow an ec_port_status refresh for online property check + * if we're not already online to check for plug events if + * not mkbp_event_supported. + */ + if (ec_device->mkbp_event_supported || port->psy_online) + break; + case POWER_SUPPLY_PROP_CURRENT_MAX: + case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN: + case POWER_SUPPLY_PROP_VOLTAGE_NOW: + ret = cros_usbpd_charger_get_port_status(port, true); + if (ret < 0) { + dev_err(dev, "Failed to get port status (err:0x%x)\n", + ret); + return -EINVAL; + } + break; + default: + break; + } + + switch (psp) { + case POWER_SUPPLY_PROP_ONLINE: + val->intval = port->psy_online; + break; + case POWER_SUPPLY_PROP_STATUS: + val->intval = port->psy_status; + break; + case POWER_SUPPLY_PROP_CURRENT_MAX: + val->intval = port->psy_current_max * 1000; + break; + case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN: + val->intval = port->psy_voltage_max_design * 1000; + break; + case POWER_SUPPLY_PROP_VOLTAGE_NOW: + val->intval = port->psy_voltage_now * 1000; + break; + case POWER_SUPPLY_PROP_MODEL_NAME: + val->strval = port->model_name; + break; + case POWER_SUPPLY_PROP_MANUFACTURER: + val->strval = port->manufacturer; + break; + default: + return -EINVAL; + } + + return 0; +} + +static int cros_usbpd_charger_ec_event(struct notifier_block *nb, + unsigned long queued_during_suspend, + void *_notify) +{ + struct cros_ec_device *ec_device; + struct charger_data *charger; + struct device *dev; + u32 host_event; + + charger = container_of(nb, struct charger_data, notifier); + ec_device = charger->ec_device; + dev = charger->dev; + + host_event = cros_ec_get_host_event(ec_device); + if (host_event & EC_HOST_EVENT_MASK(EC_HOST_EVENT_PD_MCU)) { + cros_usbpd_charger_power_changed(charger->ports[0]->psy); + return NOTIFY_OK; + } else { + return NOTIFY_DONE; + } +} + +static int cros_usbpd_charger_probe(struct platform_device *pd) +{ + struct cros_ec_dev *ec_dev = dev_get_drvdata(pd->dev.parent); + struct cros_ec_device *ec_device = ec_dev->ec_dev; + struct power_supply_desc *psy_desc; + struct device *dev = &pd->dev; + struct charger_data *charger; + struct power_supply *psy; + struct port_data *port; + int ret = -EINVAL; + int i; + + charger = devm_kzalloc(dev, sizeof(struct charger_data), + GFP_KERNEL); + if (!charger) + return -ENOMEM; + + charger->dev = dev; + charger->ec_dev = ec_dev; + charger->ec_device = ec_device; + + platform_set_drvdata(pd, charger); + + charger->num_charger_ports = cros_usbpd_charger_get_num_ports(charger); + if (charger->num_charger_ports <= 0) { + /* + * This can happen on a system that doesn't support USB PD. + * Log a message, but no need to warn. + */ + dev_info(dev, "No charging ports found\n"); + ret = -ENODEV; + goto fail_nowarn; + } + + for (i = 0; i < charger->num_charger_ports; i++) { + struct power_supply_config psy_cfg = {}; + + port = devm_kzalloc(dev, sizeof(struct port_data), GFP_KERNEL); + if (!port) { + ret = -ENOMEM; + goto fail; + } + + port->charger = charger; + port->port_number = i; + sprintf(port->name, CHARGER_DIR_NAME, i); + + psy_desc = &port->psy_desc; + psy_desc->name = port->name; + psy_desc->type = POWER_SUPPLY_TYPE_USB; + psy_desc->get_property = cros_usbpd_charger_get_prop; + psy_desc->external_power_changed = + cros_usbpd_charger_power_changed; + psy_desc->properties = cros_usbpd_charger_props; + psy_desc->num_properties = + ARRAY_SIZE(cros_usbpd_charger_props); + psy_cfg.drv_data = port; + + psy = devm_power_supply_register_no_ws(dev, psy_desc, + &psy_cfg); + if (IS_ERR(psy)) { + dev_err(dev, "Failed to register power supply\n"); + continue; + } + port->psy = psy; + + charger->ports[charger->num_registered_psy++] = port; + ec_device->charger = psy; + } + + if (!charger->num_registered_psy) { + ret = -ENODEV; + dev_err(dev, "No power supplies registered\n"); + goto fail; + } + + if (ec_device->mkbp_event_supported) { + /* Get PD events from the EC */ + charger->notifier.notifier_call = cros_usbpd_charger_ec_event; + ret = blocking_notifier_chain_register( + &ec_device->event_notifier, + &charger->notifier); + if (ret < 0) + dev_warn(dev, "failed to register notifier\n"); + } + + return 0; + +fail: + WARN(1, "%s: Failing probe (err:0x%x)\n", dev_name(dev), ret); + +fail_nowarn: + dev_info(dev, "Failing probe (err:0x%x)\n", ret); + return ret; +} + +#ifdef CONFIG_PM_SLEEP +static int cros_usbpd_charger_resume(struct device *dev) +{ + struct charger_data *charger = dev_get_drvdata(dev); + int i; + + if (!charger) + return 0; + + for (i = 0; i < charger->num_registered_psy; i++) { + power_supply_changed(charger->ports[i]->psy); + charger->ports[i]->last_update = + jiffies - CHARGER_CACHE_UPDATE_DELAY; + } + + return 0; +} +#endif + +static SIMPLE_DEV_PM_OPS(cros_usbpd_charger_pm_ops, NULL, + cros_usbpd_charger_resume); + +static struct platform_driver cros_usbpd_charger_driver = { + .driver = { + .name = DRV_NAME, + .pm = &cros_usbpd_charger_pm_ops, + }, + .probe = cros_usbpd_charger_probe +}; + +module_platform_driver(cros_usbpd_charger_driver); + +MODULE_LICENSE("GPL"); +MODULE_DESCRIPTION("ChromeOS EC USBPD charger"); +MODULE_ALIAS("platform:" DRV_NAME); diff --git a/include/linux/mfd/cros_ec.h b/include/linux/mfd/cros_ec.h index f36125ee9245..a1eb39344231 100644 --- a/include/linux/mfd/cros_ec.h +++ b/include/linux/mfd/cros_ec.h @@ -19,6 +19,7 @@ #include #include #include +#include #include #include @@ -143,6 +144,7 @@ struct cros_ec_device { struct cros_ec_command *msg); int (*pkt_xfer)(struct cros_ec_device *ec, struct cros_ec_command *msg); + struct power_supply *charger; struct mutex lock; bool mkbp_event_supported; struct blocking_notifier_head event_notifier;