* [PATCH v2 0/8] EC-based USB Power Delivery support for Chrome machines
@ 2016-02-12 12:56 Tomeu Vizoso
2016-02-12 12:56 ` [PATCH v2 2/8] power_supply: Add types for USB Type C and PD chargers Tomeu Vizoso
` (2 more replies)
0 siblings, 3 replies; 11+ messages in thread
From: Tomeu Vizoso @ 2016-02-12 12:56 UTC (permalink / raw)
To: linux-kernel
Cc: Sameer Nanda, Lee Jones, Benson Leung, Enric Balletbò,
Vic Yang, Vincent Palatin, Randall Spangler, Todd Broch,
Gwendal Grignou, Tomeu Vizoso, linux-pm, Sebastian Reichel,
Dmitry Eremin-Solenikov, Dmitry Torokhov, David Woodhouse,
linux-input, Olof Johansson
Hi,
this series contains a driver that exposes a power_supply to userspace
representing a port that support USB PD charging.
Allows userspace to display to the user if the machine is charging and
on which port, and if another device is being charged by a port. Also
allows to limit the current draw for thermal reasons.
Thanks,
Tomeu
Changes in v2:
- Allocate enough for the structs in cros_ec_get_host_command_version_mask,
not their pointers.
- Allocate msg in the stack in get_next_event and
get_keyboard_state_event, as suggested by Gwendal.
- Remove the mention to Type C from the comments where it didn't make
sense, as suggested by Benson Leung.
- Split out changes to cros_ec_commands.h and the helpers added to
mfd/cros_ec.h from the patch that adds the charger driver, as
suggested by Lee.
- Actually call get_ec_num_ports.
- Move cros_ec_usb_pd_charger_register into cros_ec_dev.c
Benson Leung (1):
power_supply: Add types for USB Type C and PD chargers
Sameer Nanda (1):
power: cros_usbpd-charger: Add EC-based USB PD charger driver
Tomeu Vizoso (4):
mfd: cros_ec: Add cros_ec_cmd_xfer_status helper
mfd: cros_ec: Add cros_ec_get_host_event
mfd: cros_ec: Add more definitions for PD commands
platform/chrome: Register USB PD charger device
Vic Yang (1):
mfd: cros_ec: Add MKBP event support
Vincent Palatin (1):
platform/chrome: Check the USB PD feature before creating a charger
drivers/input/keyboard/cros_ec_keyb.c | 135 ++---
drivers/mfd/cros_ec.c | 57 +-
drivers/platform/chrome/cros_ec_dev.c | 82 +++
drivers/platform/chrome/cros_ec_proto.c | 129 +++++
drivers/power/Kconfig | 10 +
drivers/power/Makefile | 1 +
drivers/power/cros_usbpd-charger.c | 908 ++++++++++++++++++++++++++++++++
drivers/power/power_supply_sysfs.c | 3 +-
include/linux/mfd/cros_ec.h | 73 +++
include/linux/mfd/cros_ec_commands.h | 442 +++++++++++++++-
include/linux/power_supply.h | 3 +
11 files changed, 1730 insertions(+), 113 deletions(-)
create mode 100644 drivers/power/cros_usbpd-charger.c
--
2.5.0
^ permalink raw reply [flat|nested] 11+ messages in thread
* [PATCH v2 2/8] power_supply: Add types for USB Type C and PD chargers
2016-02-12 12:56 [PATCH v2 0/8] EC-based USB Power Delivery support for Chrome machines Tomeu Vizoso
@ 2016-02-12 12:56 ` Tomeu Vizoso
2016-02-16 16:59 ` Sebastian Reichel
2016-02-12 12:57 ` [PATCH v2 6/8] power: cros_usbpd-charger: Add EC-based USB PD charger driver Tomeu Vizoso
2016-02-20 16:22 ` [PATCH v2 0/8] EC-based USB Power Delivery support for Chrome machines Pavel Machek
2 siblings, 1 reply; 11+ messages in thread
From: Tomeu Vizoso @ 2016-02-12 12:56 UTC (permalink / raw)
To: linux-kernel
Cc: Sameer Nanda, Lee Jones, Benson Leung, Enric Balletbò,
Vic Yang, Vincent Palatin, Randall Spangler, Todd Broch,
Gwendal Grignou, Tomeu Vizoso, Sebastian Reichel,
Dmitry Eremin-Solenikov, David Woodhouse, linux-pm
From: Benson Leung <bleung@chromium.org>
This adds power supply types for USB chargers defined in
the USB Type-C Specification 1.1 and in the
USB Power Delivery Specification Revision 2.0 V1.1.
The following are added :
POWER_SUPPLY_TYPE_USB_TYPE_C, /* Type C Port */
POWER_SUPPLY_TYPE_USB_PD, /* Power Delivery Port */
POWER_SUPPLY_TYPE_USB_PD_DRP, /* PD Dual Role Port */
Signed-off-by: Benson Leung <bleung@chromium.org>
[tomeu: remove the mention to Type C from the comments]
Signed-off-by: Tomeu Vizoso <tomeu.vizoso@collabora.com>
Reviewed-by: Alec Berg <alecaberg@chromium.org>
Reviewed-by: Vincent Palatin <vpalatin@chromium.org>
Reviewed-by: Todd Broch <tbroch@chromium.org>
---
Changes in v2:
- Remove the mention to Type C from the comments where it didn't make
sense, as suggested by Benson Leung.
drivers/power/power_supply_sysfs.c | 3 ++-
include/linux/power_supply.h | 3 +++
2 files changed, 5 insertions(+), 1 deletion(-)
diff --git a/drivers/power/power_supply_sysfs.c b/drivers/power/power_supply_sysfs.c
index ed2d7fd0c734..80fed98832f9 100644
--- a/drivers/power/power_supply_sysfs.c
+++ b/drivers/power/power_supply_sysfs.c
@@ -45,7 +45,8 @@ static ssize_t power_supply_show_property(struct device *dev,
char *buf) {
static char *type_text[] = {
"Unknown", "Battery", "UPS", "Mains", "USB",
- "USB_DCP", "USB_CDP", "USB_ACA"
+ "USB_DCP", "USB_CDP", "USB_ACA", "USB_C",
+ "USB_PD", "USB_PD_DRP"
};
static char *status_text[] = {
"Unknown", "Charging", "Discharging", "Not charging", "Full"
diff --git a/include/linux/power_supply.h b/include/linux/power_supply.h
index ef9f1592185d..751061790626 100644
--- a/include/linux/power_supply.h
+++ b/include/linux/power_supply.h
@@ -163,6 +163,9 @@ enum power_supply_type {
POWER_SUPPLY_TYPE_USB_DCP, /* Dedicated Charging Port */
POWER_SUPPLY_TYPE_USB_CDP, /* Charging Downstream Port */
POWER_SUPPLY_TYPE_USB_ACA, /* Accessory Charger Adapters */
+ POWER_SUPPLY_TYPE_USB_TYPE_C, /* Type C Port */
+ POWER_SUPPLY_TYPE_USB_PD, /* Power Delivery Port */
+ POWER_SUPPLY_TYPE_USB_PD_DRP, /* PD Dual Role Port */
};
enum power_supply_notifier_events {
--
2.5.0
^ permalink raw reply related [flat|nested] 11+ messages in thread
* [PATCH v2 6/8] power: cros_usbpd-charger: Add EC-based USB PD charger driver
2016-02-12 12:56 [PATCH v2 0/8] EC-based USB Power Delivery support for Chrome machines Tomeu Vizoso
2016-02-12 12:56 ` [PATCH v2 2/8] power_supply: Add types for USB Type C and PD chargers Tomeu Vizoso
@ 2016-02-12 12:57 ` Tomeu Vizoso
2016-02-12 14:14 ` kbuild test robot
` (3 more replies)
2016-02-20 16:22 ` [PATCH v2 0/8] EC-based USB Power Delivery support for Chrome machines Pavel Machek
2 siblings, 4 replies; 11+ messages in thread
From: Tomeu Vizoso @ 2016-02-12 12:57 UTC (permalink / raw)
To: linux-kernel
Cc: Sameer Nanda, Lee Jones, Benson Leung, Enric Balletbò,
Vic Yang, Vincent Palatin, Randall Spangler, Todd Broch,
Gwendal Grignou, Tomeu Vizoso, Shawn Nematbakhsh,
Sebastian Reichel, Dmitry Eremin-Solenikov, linux-pm,
David Woodhouse
From: Sameer Nanda <snanda@chromium.org>
This driver exposes the charger functionality in the PD EC to userspace.
Signed-off-by: Tomeu Vizoso <tomeu.vizoso@collabora.com>
Cc: Sameer Nanda <snanda@chromium.org>
Cc: Benson Leung <bleung@chromium.org>
Cc: Shawn Nematbakhsh <shawnn@chromium.org>
---
Changes in v2:
- Split out changes to cros_ec_commands.h and the helpers added to
mfd/cros_ec.h from the patch that adds the charger driver, as
suggested by Lee.
- Actually call get_ec_num_ports.
drivers/power/Kconfig | 10 +
drivers/power/Makefile | 1 +
drivers/power/cros_usbpd-charger.c | 908 +++++++++++++++++++++++++++++++++++++
3 files changed, 919 insertions(+)
create mode 100644 drivers/power/cros_usbpd-charger.c
diff --git a/drivers/power/Kconfig b/drivers/power/Kconfig
index 1ddd13cc0c07..26355850d91b 100644
--- a/drivers/power/Kconfig
+++ b/drivers/power/Kconfig
@@ -502,6 +502,16 @@ config AXP20X_POWER
This driver provides support for the power supply features of
AXP20x PMIC.
+config CHARGER_CROS_USB_PD
+ tristate "Chrome OS EC based USB PD charger"
+ depends on MFD_CROS_EC
+ default y
+ help
+ Say Y here to enable Chrome OS EC based USB PD 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
source "drivers/power/reset/Kconfig"
diff --git a/drivers/power/Makefile b/drivers/power/Makefile
index 0e4eab55f8d7..b83685bfae61 100644
--- a/drivers/power/Makefile
+++ b/drivers/power/Makefile
@@ -73,3 +73,4 @@ obj-$(CONFIG_CHARGER_TPS65217) += tps65217_charger.o
obj-$(CONFIG_POWER_RESET) += reset/
obj-$(CONFIG_AXP288_FUEL_GAUGE) += axp288_fuel_gauge.o
obj-$(CONFIG_AXP288_CHARGER) += axp288_charger.o
+obj-$(CONFIG_CHARGER_CROS_USB_PD) += cros_usbpd-charger.o
diff --git a/drivers/power/cros_usbpd-charger.c b/drivers/power/cros_usbpd-charger.c
new file mode 100644
index 000000000000..c1aa58b47f56
--- /dev/null
+++ b/drivers/power/cros_usbpd-charger.c
@@ -0,0 +1,908 @@
+/*
+ * Power supply driver for ChromeOS EC based USB PD Charger.
+ *
+ * Copyright (c) 2014 Google, Inc
+ *
+ * This software is licensed under the terms of the GNU General Public
+ * License version 2, as published by the Free Software Foundation, and
+ * may be copied, distributed, and modified under those terms.
+ *
+ * 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.
+ */
+
+#include <linux/fs.h>
+#include <linux/ktime.h>
+#include <linux/module.h>
+#include <linux/mfd/cros_ec.h>
+#include <linux/mfd/cros_ec_commands.h>
+#include <linux/platform_device.h>
+#include <linux/power_supply.h>
+#include <linux/rtc.h>
+#include <linux/slab.h>
+
+#define CROS_USB_PD_MAX_PORTS 8
+#define CROS_USB_PD_MAX_LOG_ENTRIES 30
+
+#define CROS_USB_PD_LOG_UPDATE_DELAY msecs_to_jiffies(60000)
+#define CROS_USB_PD_CACHE_UPDATE_DELAY msecs_to_jiffies(500)
+
+/* Buffer + macro for building PDLOG string */
+#define BUF_SIZE 80
+#define APPEND_STRING(buf, len, str, ...) ((len) += \
+ snprintf((buf) + (len), max(BUF_SIZE - (len), 0), (str), ##__VA_ARGS__))
+
+#define CHARGER_DIR_NAME "CROS_USB_PD_CHARGER%d"
+#define CHARGER_DIR_NAME_LENGTH sizeof(CHARGER_DIR_NAME)
+
+#define MANUFACTURER_MODEL_LENGTH 32
+
+struct port_data {
+ int port_number;
+ char name[CHARGER_DIR_NAME_LENGTH];
+ char manufacturer[MANUFACTURER_MODEL_LENGTH];
+ char model_name[MANUFACTURER_MODEL_LENGTH];
+ struct power_supply *psy;
+ struct power_supply_desc psy_desc;
+ int psy_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[CROS_USB_PD_MAX_PORTS];
+ struct delayed_work log_work;
+ struct workqueue_struct *log_workqueue;
+ struct notifier_block notifier;
+ bool suspended;
+};
+
+#define EC_MAX_IN_SIZE EC_PROTO2_MAX_REQUEST_SIZE
+#define EC_MAX_OUT_SIZE EC_PROTO2_MAX_RESPONSE_SIZE
+uint8_t ec_inbuf[EC_MAX_IN_SIZE];
+uint8_t ec_outbuf[EC_MAX_OUT_SIZE];
+
+static enum power_supply_property cros_usb_pd_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_CHARGE_CONTROL_LIMIT_MAX,
+ POWER_SUPPLY_PROP_MODEL_NAME,
+ POWER_SUPPLY_PROP_MANUFACTURER,
+};
+
+/* Current external voltage/current limit in mV/mA. Default to none. */
+static uint16_t ext_voltage_lim = EC_POWER_LIMIT_NONE;
+static uint16_t ext_current_lim = EC_POWER_LIMIT_NONE;
+
+static int ec_command(struct cros_ec_dev *ec_dev, int version, int command,
+ uint8_t *outdata, int outsize, uint8_t *indata,
+ int insize)
+{
+ struct cros_ec_device *ec_device = ec_dev->ec_dev;
+ struct cros_ec_command *msg;
+ int ret;
+
+ msg = kmalloc(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;
+
+ memcpy(msg->data, outdata, outsize);
+ ret = cros_ec_cmd_xfer_status(ec_device, msg);
+ memcpy(indata, msg->data, insize);
+
+ kfree(msg);
+
+ return ret;
+}
+
+static int set_ec_usb_pd_override_ports(struct charger_data *charger,
+ int port_num)
+{
+ struct device *dev = charger->dev;
+ struct ec_params_charge_port_override req;
+ int ret;
+
+ req.override_port = port_num;
+
+ ret = ec_command(charger->ec_dev, 0, EC_CMD_PD_CHARGE_PORT_OVERRIDE,
+ (uint8_t *)&req, sizeof(req),
+ NULL, 0);
+ if (ret < 0) {
+ dev_info(dev, "Port Override command returned 0x%x\n", ret);
+ return -EINVAL;
+ }
+
+ return 0;
+}
+
+static int get_ec_num_ports(struct charger_data *charger, int *num_ports)
+{
+ struct device *dev = charger->dev;
+ struct ec_response_usb_pd_ports *resp =
+ (struct ec_response_usb_pd_ports *)ec_inbuf;
+ int ret;
+
+ *num_ports = 0;
+ ret = ec_command(charger->ec_dev, 0, EC_CMD_USB_PD_PORTS,
+ NULL, 0, ec_inbuf, EC_MAX_IN_SIZE);
+ if (ret < 0) {
+ dev_err(dev, "Unable to query PD ports (err:0x%x)\n", ret);
+ return ret;
+ }
+ *num_ports = resp->num_ports;
+ dev_dbg(dev, "Num ports = %d\n", *num_ports);
+
+ return 0;
+}
+
+static int get_ec_usb_pd_discovery_info(struct port_data *port)
+{
+ struct charger_data *charger = port->charger;
+ struct device *dev = charger->dev;
+ struct ec_params_usb_pd_info_request req;
+ struct ec_params_usb_pd_discovery_entry resp;
+ int ret;
+
+ req.port = port->port_number;
+
+ ret = ec_command(charger->ec_dev, 0, EC_CMD_USB_PD_DISCOVERY,
+ (uint8_t *)&req, sizeof(req),
+ (uint8_t *)&resp, sizeof(resp));
+ if (ret < 0) {
+ dev_err(dev, "Unable to query Discovery info (err:0x%x)\n",
+ ret);
+ return -EINVAL;
+ }
+
+ dev_dbg(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, MANUFACTURER_MODEL_LENGTH, "%x", resp.vid);
+ snprintf(port->model_name, MANUFACTURER_MODEL_LENGTH, "%x", resp.pid);
+
+ return 0;
+}
+
+static int get_ec_usb_pd_power_info(struct port_data *port)
+{
+ struct charger_data *charger = port->charger;
+ struct device *dev = charger->dev;
+ struct ec_params_usb_pd_power_info req;
+ struct ec_response_usb_pd_power_info resp;
+ char role_str[80];
+ int ret, last_psy_status, last_psy_type;
+
+ req.port = port->port_number;
+ ret = ec_command(charger->ec_dev, 0, EC_CMD_USB_PD_POWER_INFO,
+ (uint8_t *)&req, sizeof(req),
+ (uint8_t *)&resp, sizeof(resp));
+ if (ret < 0) {
+ dev_err(dev, "Unable to query PD power info (err:0x%x)\n", ret);
+ return -EINVAL;
+ }
+
+ last_psy_status = port->psy_status;
+ last_psy_type = port->psy_type;
+
+ switch (resp.role) {
+ case USB_PD_PORT_POWER_DISCONNECTED:
+ snprintf(role_str, sizeof(role_str), "%s", "DISCONNECTED");
+ port->psy_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
+ port->psy_online = 0;
+ break;
+ case USB_PD_PORT_POWER_SOURCE:
+ snprintf(role_str, sizeof(role_str), "%s", "SOURCE");
+ port->psy_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
+ port->psy_online = 0;
+ break;
+ case USB_PD_PORT_POWER_SINK:
+ snprintf(role_str, sizeof(role_str), "%s", "SINK");
+ port->psy_status = POWER_SUPPLY_STATUS_CHARGING;
+ port->psy_online = 1;
+ break;
+ case USB_PD_PORT_POWER_SINK_NOT_CHARGING:
+ snprintf(role_str, sizeof(role_str), "%s", "NOT CHARGING");
+ port->psy_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
+ port->psy_online = 1;
+ break;
+ default:
+ snprintf(role_str, sizeof(role_str), "%s", "UNKNOWN");
+ 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_type = POWER_SUPPLY_TYPE_USB;
+ 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_type = POWER_SUPPLY_TYPE_USB_PD_DRP;
+ else
+ port->psy_type = POWER_SUPPLY_TYPE_USB;
+ break;
+ case USB_CHG_TYPE_PROPRIETARY:
+ port->psy_type = POWER_SUPPLY_TYPE_MAINS;
+ break;
+ case USB_CHG_TYPE_C:
+ port->psy_type = POWER_SUPPLY_TYPE_USB_TYPE_C;
+ break;
+ case USB_CHG_TYPE_BC12_DCP:
+ port->psy_type = POWER_SUPPLY_TYPE_USB_DCP;
+ break;
+ case USB_CHG_TYPE_BC12_CDP:
+ port->psy_type = POWER_SUPPLY_TYPE_USB_CDP;
+ break;
+ case USB_CHG_TYPE_PD:
+ if (resp.dualrole)
+ port->psy_type = POWER_SUPPLY_TYPE_USB_PD_DRP;
+ else
+ port->psy_type = POWER_SUPPLY_TYPE_USB_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. Treat this case as a dedicated charger since we
+ * don't know any better just yet. Additionally since the power
+ * capabilities are unknown, report the max current and voltage
+ * as zero.
+ */
+ port->psy_type = POWER_SUPPLY_TYPE_MAINS;
+ 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_type = POWER_SUPPLY_TYPE_USB;
+ }
+
+ port->psy_desc.type = port->psy_type;
+
+ dev_dbg(dev,
+ "Port %d: %s type=%d=vmax=%d vnow=%d cmax=%d clim=%d pmax=%d\n",
+ port->port_number, role_str, 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_type != port->psy_type ||
+ last_psy_status != port->psy_status) {
+ dev_dbg(dev, "Port %d: Calling power_supply_changed\n",
+ port->port_number);
+ power_supply_changed(port->psy);
+ }
+
+ return 0;
+}
+
+static int get_ec_port_status(struct port_data *port, bool ratelimit)
+{
+ int ret;
+
+ if (ratelimit &&
+ time_is_after_jiffies(port->last_update +
+ CROS_USB_PD_CACHE_UPDATE_DELAY))
+ return 0;
+
+ ret = get_ec_usb_pd_power_info(port);
+ if (ret < 0)
+ return ret;
+
+ ret = get_ec_usb_pd_discovery_info(port);
+ port->last_update = jiffies;
+
+ return ret;
+}
+
+static void cros_usb_pd_charger_power_changed(struct power_supply *psy)
+{
+ struct port_data *port = power_supply_get_drvdata(psy);
+ struct charger_data *charger = port->charger;
+ struct device *dev = charger->dev;
+ int i;
+
+ dev_dbg(dev, "cros_usb_pd_charger_power_changed\n");
+ for (i = 0; i < charger->num_registered_psy; i++)
+ get_ec_port_status(charger->ports[i], false);
+}
+
+static int cros_usb_pd_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 device *dev = charger->dev;
+ int ret;
+
+
+ /* Only refresh ec_port_status for dynamic properties */
+ switch (psp) {
+ case POWER_SUPPLY_PROP_CURRENT_MAX:
+ case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
+ case POWER_SUPPLY_PROP_VOLTAGE_NOW:
+ case POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX:
+ ret = get_ec_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_CHARGE_CONTROL_LIMIT_MAX:
+ /* TODO: send a TBD host command to the EC. */
+ val->intval = 0;
+ 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_usb_pd_charger_set_prop(struct power_supply *psy,
+ enum power_supply_property psp,
+ const union power_supply_propval *val)
+{
+ struct port_data *port = power_supply_get_drvdata(psy);
+ struct charger_data *charger = port->charger;
+ int port_number;
+
+ switch (psp) {
+ case POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX:
+ /*
+ * A value of -1 implies switching to battery as the power
+ * source. Any other value implies using this port as the
+ * power source.
+ */
+ port_number = val->intval;
+ if (port_number != -1)
+ port_number = port->port_number;
+ return set_ec_usb_pd_override_ports(charger, port_number);
+ default:
+ return -EINVAL;
+ }
+ return 0;
+}
+
+static int cros_usb_pd_charger_is_writeable(struct power_supply *psy,
+ enum power_supply_property psp)
+{
+ int ret;
+
+ switch (psp) {
+ case POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX:
+ ret = 1;
+ break;
+ default:
+ ret = 0;
+ }
+
+ return ret;
+}
+
+static void cros_usb_pd_print_log_entry(struct ec_response_pd_log *r,
+ ktime_t tstamp)
+{
+ static const char * const fault_names[] = {
+ "---", "OCP", "fast OCP", "OVP", "Discharge"
+ };
+ static const char * const role_names[] = {
+ "Disconnected", "SRC", "SNK", "SNK (not charging)"
+ };
+ static const char * const chg_type_names[] = {
+ "None", "PD", "Type-C", "Proprietary",
+ "DCP", "CDP", "SDP", "Other", "VBUS"
+ };
+ int i;
+ int role_idx, type_idx;
+ const char *fault, *role, *chg_type;
+ struct usb_chg_measures *meas;
+ struct mcdp_info *minfo;
+ struct rtc_time rt;
+ int len = 0;
+ char buf[BUF_SIZE + 1];
+
+ /* the timestamp is the number of 1024th of seconds in the past */
+ tstamp = ktime_sub_us(tstamp,
+ (uint64_t)r->timestamp << PD_LOG_TIMESTAMP_SHIFT);
+ rt = rtc_ktime_to_tm(tstamp);
+
+ switch (r->type) {
+ case PD_EVENT_MCU_CHARGE:
+ if (r->data & CHARGE_FLAGS_OVERRIDE)
+ APPEND_STRING(buf, len, "override ");
+ if (r->data & CHARGE_FLAGS_DELAYED_OVERRIDE)
+ APPEND_STRING(buf, len, "pending_override ");
+ role_idx = r->data & CHARGE_FLAGS_ROLE_MASK;
+ role = role_idx < ARRAY_SIZE(role_names) ?
+ role_names[role_idx] : "Unknown";
+ type_idx = (r->data & CHARGE_FLAGS_TYPE_MASK)
+ >> CHARGE_FLAGS_TYPE_SHIFT;
+ chg_type = type_idx < ARRAY_SIZE(chg_type_names) ?
+ chg_type_names[type_idx] : "???";
+
+ if ((role_idx == USB_PD_PORT_POWER_DISCONNECTED) ||
+ (role_idx == USB_PD_PORT_POWER_SOURCE)) {
+ APPEND_STRING(buf, len, "%s", role);
+ break;
+ }
+
+ meas = (struct usb_chg_measures *)r->payload;
+ APPEND_STRING(buf, len, "%s %s %s %dmV max %dmV / %dmA", role,
+ r->data & CHARGE_FLAGS_DUAL_ROLE ? "DRP" : "Charger",
+ chg_type,
+ meas->voltage_now,
+ meas->voltage_max,
+ meas->current_max);
+ break;
+ case PD_EVENT_ACC_RW_FAIL:
+ APPEND_STRING(buf, len, "RW signature check failed");
+ break;
+ case PD_EVENT_PS_FAULT:
+ fault = r->data < ARRAY_SIZE(fault_names) ? fault_names[r->data]
+ : "???";
+ APPEND_STRING(buf, len, "Power supply fault: %s", fault);
+ break;
+ case PD_EVENT_VIDEO_DP_MODE:
+ APPEND_STRING(buf, len, "DP mode %sabled",
+ (r->data == 1) ? "en" : "dis");
+ break;
+ case PD_EVENT_VIDEO_CODEC:
+ minfo = (struct mcdp_info *)r->payload;
+ APPEND_STRING(buf, len,
+ "HDMI info: family:%04x chipid:%04x irom:%d.%d.%d fw:%d.%d.%d",
+ MCDP_FAMILY(minfo->family),
+ MCDP_CHIPID(minfo->chipid),
+ minfo->irom.major, minfo->irom.minor,
+ minfo->irom.build, minfo->fw.major,
+ minfo->fw.minor, minfo->fw.build);
+ break;
+ default:
+ APPEND_STRING(buf, len,
+ "Event %02x (%04x) [", r->type, r->data);
+ for (i = 0; i < PD_LOG_SIZE(r->size_port); i++)
+ APPEND_STRING(buf, len, "%02x ", r->payload[i]);
+ APPEND_STRING(buf, len, "]");
+ break;
+ }
+
+ pr_info("PDLOG %d/%02d/%02d %02d:%02d:%02d.%03d P%d %s\n",
+ rt.tm_year + 1900, rt.tm_mon + 1, rt.tm_mday,
+ rt.tm_hour, rt.tm_min, rt.tm_sec,
+ (int)(ktime_to_ms(tstamp) % MSEC_PER_SEC),
+ PD_LOG_PORT(r->size_port), buf);
+}
+
+static void cros_usb_pd_log_check(struct work_struct *work)
+{
+ struct charger_data *charger = container_of(to_delayed_work(work),
+ struct charger_data, log_work);
+ struct device *dev = charger->dev;
+ union {
+ struct ec_response_pd_log r;
+ uint32_t words[8]; /* space for the payload */
+ } u;
+ int ret;
+ int entries = 0;
+ ktime_t now;
+
+ if (charger->suspended) {
+ dev_dbg(dev, "Suspended...bailing out\n");
+ return;
+ }
+
+ while (entries++ < CROS_USB_PD_MAX_LOG_ENTRIES) {
+ ret = ec_command(charger->ec_dev, 0, EC_CMD_PD_GET_LOG_ENTRY,
+ NULL, 0, (uint8_t *)&u, sizeof(u));
+ now = ktime_get_real();
+ if (ret < 0) {
+ dev_dbg(dev, "Cannot get PD log %d\n", ret);
+ break;
+ }
+ if (u.r.type == PD_EVENT_NO_ENTRY)
+ break;
+
+ cros_usb_pd_print_log_entry(&u.r, now);
+ }
+
+ queue_delayed_work(charger->log_workqueue, &charger->log_work,
+ CROS_USB_PD_LOG_UPDATE_DELAY);
+}
+
+static int cros_usb_pd_ec_event(struct notifier_block *nb,
+ unsigned long queued_during_suspend, void *_notify)
+{
+ struct charger_data *charger;
+ struct device *dev;
+ struct cros_ec_device *ec_device;
+ u32 host_event;
+
+ charger = container_of(nb, struct charger_data, notifier);
+ dev = charger->dev;
+ ec_device = charger->ec_device;
+
+ host_event = cros_ec_get_host_event(ec_device);
+ if (host_event & EC_HOST_EVENT_MASK(EC_HOST_EVENT_PD_MCU)) {
+ cros_usb_pd_charger_power_changed(charger->ports[0]->psy);
+ return NOTIFY_OK;
+ } else {
+ return NOTIFY_DONE;
+ }
+}
+
+static char *charger_supplied_to[] = {"cros-usb_pd-charger"};
+
+static int cros_usb_pd_charger_probe(struct platform_device *pd)
+{
+ struct device *dev = &pd->dev;
+ struct cros_ec_dev *ec_dev = dev_get_drvdata(pd->dev.parent);
+ struct cros_ec_device *ec_device;
+ struct charger_data *charger;
+ struct port_data *port;
+ struct power_supply_desc *psy_desc;
+ struct power_supply_config psy_cfg = {};
+ int i;
+ int ret = -EINVAL;
+
+ dev_dbg(dev, "cros_usb_pd_charger_probe\n");
+ if (!ec_dev) {
+ WARN(1, "%s: No EC dev found\n", dev_name(dev));
+ return -EINVAL;
+ }
+
+ ec_device = ec_dev->ec_dev;
+ if (!ec_device) {
+ WARN(1, "%s: No EC device found\n", dev_name(dev));
+ return -EINVAL;
+ }
+
+ 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);
+
+ if ((get_ec_num_ports(charger, &charger->num_charger_ports) < 0) ||
+ !charger->num_charger_ports) {
+ /*
+ * 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++) {
+ port = devm_kzalloc(dev, sizeof(struct port_data), GFP_KERNEL);
+ if (!port) {
+ dev_err(dev, "Failed to alloc port structure\n");
+ 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_usb_pd_charger_get_prop;
+ psy_desc->set_property = cros_usb_pd_charger_set_prop;
+ psy_desc->property_is_writeable =
+ cros_usb_pd_charger_is_writeable;
+ psy_desc->external_power_changed =
+ cros_usb_pd_charger_power_changed;
+ psy_desc->properties = cros_usb_pd_charger_props;
+ psy_desc->num_properties = ARRAY_SIZE(
+ cros_usb_pd_charger_props);
+
+ psy_cfg.drv_data = port;
+ psy_cfg.supplied_to = charger_supplied_to;
+ psy_cfg.num_supplicants = ARRAY_SIZE(charger_supplied_to);
+
+ port->psy = power_supply_register_no_ws(dev, psy_desc,
+ &psy_cfg);
+ if (IS_ERR(port->psy)) {
+ dev_err(dev, "Failed to register power supply: %ld\n",
+ PTR_ERR(port->psy));
+ continue;
+ }
+
+ charger->ports[charger->num_registered_psy++] = port;
+ }
+
+ 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_usb_pd_ec_event;
+ ret = blocking_notifier_chain_register(
+ &ec_device->event_notifier, &charger->notifier);
+ if (ret < 0)
+ dev_warn(dev, "failed to register notifier\n");
+ }
+
+ /* Retrieve PD event logs periodically */
+ INIT_DELAYED_WORK(&charger->log_work, cros_usb_pd_log_check);
+ charger->log_workqueue =
+ create_singlethread_workqueue("cros_usb_pd_log");
+ queue_delayed_work(charger->log_workqueue, &charger->log_work,
+ CROS_USB_PD_LOG_UPDATE_DELAY);
+
+ return 0;
+
+fail:
+ WARN(1, "%s: Failing probe (err:0x%x)\n", dev_name(dev), ret);
+
+fail_nowarn:
+ if (charger) {
+ for (i = 0; i < charger->num_registered_psy; i++) {
+ port = charger->ports[i];
+ power_supply_unregister(port->psy);
+ devm_kfree(dev, port);
+ }
+ platform_set_drvdata(pd, NULL);
+ devm_kfree(dev, charger);
+ }
+
+ dev_info(dev, "Failing probe (err:0x%x)\n", ret);
+ return ret;
+}
+
+static int cros_usb_pd_charger_remove(struct platform_device *pd)
+{
+ struct charger_data *charger = platform_get_drvdata(pd);
+ struct device *dev = charger->dev;
+ struct port_data *port;
+ int i;
+
+ if (charger) {
+ for (i = 0; i < charger->num_registered_psy; i++) {
+ port = charger->ports[i];
+ power_supply_unregister(port->psy);
+ devm_kfree(dev, port);
+ }
+ flush_delayed_work(&charger->log_work);
+ platform_set_drvdata(pd, NULL);
+ devm_kfree(dev, charger);
+ }
+ return 0;
+}
+
+#ifdef CONFIG_PM_SLEEP
+static int cros_usb_pd_charger_resume(struct device *dev)
+{
+ struct charger_data *charger = dev_get_drvdata(dev);
+ int i;
+
+ if (!charger)
+ return 0;
+
+ charger->suspended = false;
+
+ dev_dbg(dev, "cros_usb_pd_charger_resume: updating power supplies\n");
+ for (i = 0; i < charger->num_registered_psy; i++) {
+ power_supply_changed(charger->ports[i]->psy);
+ charger->ports[i]->last_update =
+ jiffies - CROS_USB_PD_CACHE_UPDATE_DELAY;
+ }
+ queue_delayed_work(charger->log_workqueue, &charger->log_work,
+ CROS_USB_PD_LOG_UPDATE_DELAY);
+
+ return 0;
+}
+
+static int cros_usb_pd_charger_suspend(struct device *dev)
+{
+ struct charger_data *charger = dev_get_drvdata(dev);
+
+ charger->suspended = true;
+
+ if (charger)
+ flush_delayed_work(&charger->log_work);
+ return 0;
+}
+#endif
+
+static int set_ec_ext_power_limit(struct cros_ec_dev *ec,
+ uint16_t current_lim, uint16_t voltage_lim)
+{
+ struct ec_params_external_power_limit_v1 req;
+ int ret;
+
+ req.current_lim = current_lim;
+ req.voltage_lim = voltage_lim;
+
+ ret = ec_command(ec, 0, EC_CMD_EXT_POWER_CURRENT_LIMIT,
+ (uint8_t *)&req, sizeof(req), NULL, 0);
+ if (ret < 0) {
+ dev_warn(ec->dev,
+ "External power limit command returned 0x%x\n",
+ ret);
+ return -EINVAL;
+ }
+
+ return 0;
+}
+
+static ssize_t get_ec_ext_current_lim(struct device *dev,
+ struct device_attribute *attr,
+ char *buf)
+{
+ return scnprintf(buf, PAGE_SIZE, "%d\n", ext_current_lim);
+}
+
+static ssize_t set_ec_ext_current_lim(struct device *dev,
+ struct device_attribute *attr,
+ const char *buf, size_t count)
+{
+ int ret;
+ uint16_t tmp_val;
+ struct cros_ec_dev *ec = container_of(
+ dev, struct cros_ec_dev, class_dev);
+
+ if (kstrtou16(buf, 0, &tmp_val) < 0)
+ return -EINVAL;
+
+ ret = set_ec_ext_power_limit(ec, tmp_val, ext_voltage_lim);
+ if (ret < 0)
+ return ret;
+
+ ext_current_lim = tmp_val;
+ if (ext_current_lim == EC_POWER_LIMIT_NONE)
+ dev_info(ec->dev, "External current limit removed\n");
+ else
+ dev_info(ec->dev, "External current limit set to %dmA\n",
+ ext_current_lim);
+
+ return count;
+}
+
+static ssize_t get_ec_ext_voltage_lim(struct device *dev,
+ struct device_attribute *attr,
+ char *buf)
+{
+ return scnprintf(buf, PAGE_SIZE, "%d\n", ext_voltage_lim);
+}
+
+static ssize_t set_ec_ext_voltage_lim(struct device *dev,
+ struct device_attribute *attr,
+ const char *buf, size_t count)
+{
+ int ret;
+ uint16_t tmp_val;
+
+ struct cros_ec_dev *ec = container_of(
+ dev, struct cros_ec_dev, class_dev);
+
+ if (kstrtou16(buf, 0, &tmp_val) < 0)
+ return -EINVAL;
+
+ ret = set_ec_ext_power_limit(ec, ext_current_lim, tmp_val);
+ if (ret < 0)
+ return ret;
+
+ ext_voltage_lim = tmp_val;
+ if (ext_voltage_lim == EC_POWER_LIMIT_NONE)
+ dev_info(ec->dev, "External voltage limit removed\n");
+ else
+ dev_info(ec->dev, "External voltage limit set to %dmV\n",
+ ext_voltage_lim);
+
+ return count;
+}
+
+static DEVICE_ATTR(ext_current_lim, S_IWUSR | S_IWGRP | S_IRUGO,
+ get_ec_ext_current_lim,
+ set_ec_ext_current_lim);
+static DEVICE_ATTR(ext_voltage_lim, S_IWUSR | S_IWGRP | S_IRUGO,
+ get_ec_ext_voltage_lim,
+ set_ec_ext_voltage_lim);
+
+static struct attribute *__ext_power_cmds_attrs[] = {
+ &dev_attr_ext_current_lim.attr,
+ &dev_attr_ext_voltage_lim.attr,
+ NULL,
+};
+
+struct attribute_group cros_usb_pd_charger_attr_group = {
+ .name = "usb-pd-charger",
+ .attrs = __ext_power_cmds_attrs,
+};
+
+static SIMPLE_DEV_PM_OPS(cros_usb_pd_charger_pm_ops,
+ cros_usb_pd_charger_suspend, cros_usb_pd_charger_resume);
+
+static struct platform_driver cros_usb_pd_charger_driver = {
+ .driver = {
+ .name = "cros-usb-pd-charger",
+ .owner = THIS_MODULE,
+ .pm = &cros_usb_pd_charger_pm_ops,
+ },
+ .probe = cros_usb_pd_charger_probe,
+ .remove = cros_usb_pd_charger_remove,
+};
+
+module_platform_driver(cros_usb_pd_charger_driver);
+
+MODULE_LICENSE("GPL");
+MODULE_DESCRIPTION("Chrome USB PD charger");
+MODULE_ALIAS("power_supply:cros-usb-pd-charger");
--
2.5.0
^ permalink raw reply related [flat|nested] 11+ messages in thread
* Re: [PATCH v2 6/8] power: cros_usbpd-charger: Add EC-based USB PD charger driver
2016-02-12 12:57 ` [PATCH v2 6/8] power: cros_usbpd-charger: Add EC-based USB PD charger driver Tomeu Vizoso
@ 2016-02-12 14:14 ` kbuild test robot
2016-02-12 15:01 ` kbuild test robot
` (2 subsequent siblings)
3 siblings, 0 replies; 11+ messages in thread
From: kbuild test robot @ 2016-02-12 14:14 UTC (permalink / raw)
Cc: kbuild-all, linux-kernel, Sameer Nanda, Lee Jones, Benson Leung,
Enric Balletbò, Vic Yang, Vincent Palatin, Randall Spangler,
Todd Broch, Gwendal Grignou, Tomeu Vizoso, Shawn Nematbakhsh,
Sebastian Reichel, Dmitry Eremin-Solenikov, linux-pm,
David Woodhouse
[-- Attachment #1: Type: text/plain, Size: 920 bytes --]
Hi Sameer,
[auto build test ERROR on ljones-mfd/for-mfd-next]
[also build test ERROR on v4.5-rc3 next-20160212]
[if your patch is applied to the wrong git tree, please drop us a note to help improving the system]
url: https://github.com/0day-ci/linux/commits/Tomeu-Vizoso/EC-based-USB-Power-Delivery-support-for-Chrome-machines/20160212-210229
base: https://git.kernel.org/pub/scm/linux/kernel/git/lee/mfd.git for-mfd-next
config: i386-randconfig-s0-201606 (attached as .config)
reproduce:
# save the attached .config to linux build tree
make ARCH=i386
All errors (new ones prefixed by >>):
drivers/built-in.o: In function `cros_usb_pd_print_log_entry':
>> cros_usbpd-charger.c:(.text+0xdab0a5): undefined reference to `__moddi3'
---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation
[-- Attachment #2: .config.gz --]
[-- Type: application/octet-stream, Size: 23622 bytes --]
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH v2 6/8] power: cros_usbpd-charger: Add EC-based USB PD charger driver
2016-02-12 12:57 ` [PATCH v2 6/8] power: cros_usbpd-charger: Add EC-based USB PD charger driver Tomeu Vizoso
2016-02-12 14:14 ` kbuild test robot
@ 2016-02-12 15:01 ` kbuild test robot
2016-02-12 15:38 ` kbuild test robot
2016-02-13 0:46 ` Stephen Boyd
3 siblings, 0 replies; 11+ messages in thread
From: kbuild test robot @ 2016-02-12 15:01 UTC (permalink / raw)
Cc: kbuild-all, linux-kernel, Sameer Nanda, Lee Jones, Benson Leung,
Enric Balletbò, Vic Yang, Vincent Palatin, Randall Spangler,
Todd Broch, Gwendal Grignou, Tomeu Vizoso, Shawn Nematbakhsh,
Sebastian Reichel, Dmitry Eremin-Solenikov, linux-pm,
David Woodhouse
[-- Attachment #1: Type: text/plain, Size: 848 bytes --]
Hi Sameer,
[auto build test ERROR on ljones-mfd/for-mfd-next]
[also build test ERROR on v4.5-rc3 next-20160212]
[if your patch is applied to the wrong git tree, please drop us a note to help improving the system]
url: https://github.com/0day-ci/linux/commits/Tomeu-Vizoso/EC-based-USB-Power-Delivery-support-for-Chrome-machines/20160212-210229
base: https://git.kernel.org/pub/scm/linux/kernel/git/lee/mfd.git for-mfd-next
config: i386-randconfig-a0-201606 (attached as .config)
reproduce:
# save the attached .config to linux build tree
make ARCH=i386
All errors (new ones prefixed by >>):
>> ERROR: "__moddi3" [drivers/power/cros_usbpd-charger.ko] undefined!
---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation
[-- Attachment #2: .config.gz --]
[-- Type: application/octet-stream, Size: 22106 bytes --]
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH v2 6/8] power: cros_usbpd-charger: Add EC-based USB PD charger driver
2016-02-12 12:57 ` [PATCH v2 6/8] power: cros_usbpd-charger: Add EC-based USB PD charger driver Tomeu Vizoso
2016-02-12 14:14 ` kbuild test robot
2016-02-12 15:01 ` kbuild test robot
@ 2016-02-12 15:38 ` kbuild test robot
2016-02-13 0:46 ` Stephen Boyd
3 siblings, 0 replies; 11+ messages in thread
From: kbuild test robot @ 2016-02-12 15:38 UTC (permalink / raw)
Cc: kbuild-all, linux-kernel, Sameer Nanda, Lee Jones, Benson Leung,
Enric Balletbò, Vic Yang, Vincent Palatin, Randall Spangler,
Todd Broch, Gwendal Grignou, Tomeu Vizoso, Shawn Nematbakhsh,
Sebastian Reichel, Dmitry Eremin-Solenikov, linux-pm,
David Woodhouse
[-- Attachment #1: Type: text/plain, Size: 1009 bytes --]
Hi Sameer,
[auto build test ERROR on ljones-mfd/for-mfd-next]
[also build test ERROR on v4.5-rc3 next-20160212]
[if your patch is applied to the wrong git tree, please drop us a note to help improving the system]
url: https://github.com/0day-ci/linux/commits/Tomeu-Vizoso/EC-based-USB-Power-Delivery-support-for-Chrome-machines/20160212-210229
base: https://git.kernel.org/pub/scm/linux/kernel/git/lee/mfd.git for-mfd-next
config: arm-multi_v7_defconfig (attached as .config)
reproduce:
wget https://git.kernel.org/cgit/linux/kernel/git/wfg/lkp-tests.git/plain/sbin/make.cross -O ~/bin/make.cross
chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
make.cross ARCH=arm
All errors (new ones prefixed by >>):
>> ERROR: "__aeabi_ldivmod" [drivers/power/cros_usbpd-charger.ko] undefined!
---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation
[-- Attachment #2: .config.gz --]
[-- Type: application/octet-stream, Size: 36593 bytes --]
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH v2 6/8] power: cros_usbpd-charger: Add EC-based USB PD charger driver
2016-02-12 12:57 ` [PATCH v2 6/8] power: cros_usbpd-charger: Add EC-based USB PD charger driver Tomeu Vizoso
` (2 preceding siblings ...)
2016-02-12 15:38 ` kbuild test robot
@ 2016-02-13 0:46 ` Stephen Boyd
2016-02-16 14:35 ` Tomeu Vizoso
3 siblings, 1 reply; 11+ messages in thread
From: Stephen Boyd @ 2016-02-13 0:46 UTC (permalink / raw)
To: Tomeu Vizoso, linux-kernel
Cc: Sameer Nanda, Lee Jones, Benson Leung, Enric Balletbò,
Vic Yang, Vincent Palatin, Randall Spangler, Todd Broch,
Gwendal Grignou, Shawn Nematbakhsh, Sebastian Reichel,
Dmitry Eremin-Solenikov, linux-pm, David Woodhouse
On 02/12/2016 04:57 AM, Tomeu Vizoso wrote:
>
>
> diff --git a/drivers/power/cros_usbpd-charger.c b/drivers/power/cros_usbpd-charger.c
> new file mode 100644
> index 000000000000..c1aa58b47f56
> --- /dev/null
> +++ b/drivers/power/cros_usbpd-charger.c
> @@ -0,0 +1,908 @@
> +/*
> + * Power supply driver for ChromeOS EC based USB PD Charger.
> + *
> + * Copyright (c) 2014 Google, Inc
> + *
> + * This software is licensed under the terms of the GNU General Public
> + * License version 2, as published by the Free Software Foundation, and
> + * may be copied, distributed, and modified under those terms.
> + *
> + * 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.
> + */
> +
> +#include <linux/fs.h>
> +#include <linux/ktime.h>
> +#include <linux/module.h>
> +#include <linux/mfd/cros_ec.h>
> +#include <linux/mfd/cros_ec_commands.h>
> +#include <linux/platform_device.h>
> +#include <linux/power_supply.h>
> +#include <linux/rtc.h>
> +#include <linux/slab.h>
> +
> +#define CROS_USB_PD_MAX_PORTS 8
> +#define CROS_USB_PD_MAX_LOG_ENTRIES 30
> +
> +#define CROS_USB_PD_LOG_UPDATE_DELAY msecs_to_jiffies(60000)
> +#define CROS_USB_PD_CACHE_UPDATE_DELAY msecs_to_jiffies(500)
> +
> +/* Buffer + macro for building PDLOG string */
> +#define BUF_SIZE 80
> +#define APPEND_STRING(buf, len, str, ...) ((len) += \
> + snprintf((buf) + (len), max(BUF_SIZE - (len), 0), (str), ##__VA_ARGS__))
> +
> +#define CHARGER_DIR_NAME "CROS_USB_PD_CHARGER%d"
> +#define CHARGER_DIR_NAME_LENGTH sizeof(CHARGER_DIR_NAME)
> +
> +#define MANUFACTURER_MODEL_LENGTH 32
> +
> +struct port_data {
> + int port_number;
> + char name[CHARGER_DIR_NAME_LENGTH];
> + char manufacturer[MANUFACTURER_MODEL_LENGTH];
> + char model_name[MANUFACTURER_MODEL_LENGTH];
> + struct power_supply *psy;
> + struct power_supply_desc psy_desc;
> + int psy_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[CROS_USB_PD_MAX_PORTS];
> + struct delayed_work log_work;
> + struct workqueue_struct *log_workqueue;
> + struct notifier_block notifier;
> + bool suspended;
> +};
> +
> +#define EC_MAX_IN_SIZE EC_PROTO2_MAX_REQUEST_SIZE
> +#define EC_MAX_OUT_SIZE EC_PROTO2_MAX_RESPONSE_SIZE
> +uint8_t ec_inbuf[EC_MAX_IN_SIZE];
> +uint8_t ec_outbuf[EC_MAX_OUT_SIZE];
static? Why can't these be part of charger_data?
>
> +
> +static int set_ec_usb_pd_override_ports(struct charger_data *charger,
> + int port_num)
> +{
> + struct device *dev = charger->dev;
> + struct ec_params_charge_port_override req;
> + int ret;
> +
> + req.override_port = port_num;
> +
> + ret = ec_command(charger->ec_dev, 0, EC_CMD_PD_CHARGE_PORT_OVERRIDE,
> + (uint8_t *)&req, sizeof(req),
> + NULL, 0);
> + if (ret < 0) {
> + dev_info(dev, "Port Override command returned 0x%x\n", ret);
dev_err?
> + return -EINVAL;
> + }
> +
> + return 0;
> +}
> +
> +
[...]
> +
> +static void cros_usb_pd_print_log_entry(struct ec_response_pd_log *r,
> + ktime_t tstamp)
> +{
> + static const char * const fault_names[] = {
> + "---", "OCP", "fast OCP", "OVP", "Discharge"
> + };
> + static const char * const role_names[] = {
> + "Disconnected", "SRC", "SNK", "SNK (not charging)"
> + };
> + static const char * const chg_type_names[] = {
> + "None", "PD", "Type-C", "Proprietary",
> + "DCP", "CDP", "SDP", "Other", "VBUS"
> + };
> + int i;
> + int role_idx, type_idx;
> + const char *fault, *role, *chg_type;
> + struct usb_chg_measures *meas;
> + struct mcdp_info *minfo;
> + struct rtc_time rt;
> + int len = 0;
> + char buf[BUF_SIZE + 1];
> +
> + /* the timestamp is the number of 1024th of seconds in the past */
> + tstamp = ktime_sub_us(tstamp,
> + (uint64_t)r->timestamp << PD_LOG_TIMESTAMP_SHIFT);
> + rt = rtc_ktime_to_tm(tstamp);
> +
> + switch (r->type) {
> + case PD_EVENT_MCU_CHARGE:
> + if (r->data & CHARGE_FLAGS_OVERRIDE)
> + APPEND_STRING(buf, len, "override ");
> + if (r->data & CHARGE_FLAGS_DELAYED_OVERRIDE)
> + APPEND_STRING(buf, len, "pending_override ");
> + role_idx = r->data & CHARGE_FLAGS_ROLE_MASK;
> + role = role_idx < ARRAY_SIZE(role_names) ?
> + role_names[role_idx] : "Unknown";
> + type_idx = (r->data & CHARGE_FLAGS_TYPE_MASK)
> + >> CHARGE_FLAGS_TYPE_SHIFT;
> + chg_type = type_idx < ARRAY_SIZE(chg_type_names) ?
> + chg_type_names[type_idx] : "???";
> +
> + if ((role_idx == USB_PD_PORT_POWER_DISCONNECTED) ||
> + (role_idx == USB_PD_PORT_POWER_SOURCE)) {
> + APPEND_STRING(buf, len, "%s", role);
> + break;
> + }
> +
> + meas = (struct usb_chg_measures *)r->payload;
> + APPEND_STRING(buf, len, "%s %s %s %dmV max %dmV / %dmA", role,
> + r->data & CHARGE_FLAGS_DUAL_ROLE ? "DRP" : "Charger",
> + chg_type,
> + meas->voltage_now,
> + meas->voltage_max,
> + meas->current_max);
> + break;
> + case PD_EVENT_ACC_RW_FAIL:
> + APPEND_STRING(buf, len, "RW signature check failed");
> + break;
> + case PD_EVENT_PS_FAULT:
> + fault = r->data < ARRAY_SIZE(fault_names) ? fault_names[r->data]
> + : "???";
> + APPEND_STRING(buf, len, "Power supply fault: %s", fault);
> + break;
> + case PD_EVENT_VIDEO_DP_MODE:
> + APPEND_STRING(buf, len, "DP mode %sabled",
> + (r->data == 1) ? "en" : "dis");
> + break;
> + case PD_EVENT_VIDEO_CODEC:
> + minfo = (struct mcdp_info *)r->payload;
> + APPEND_STRING(buf, len,
> + "HDMI info: family:%04x chipid:%04x irom:%d.%d.%d fw:%d.%d.%d",
> + MCDP_FAMILY(minfo->family),
> + MCDP_CHIPID(minfo->chipid),
> + minfo->irom.major, minfo->irom.minor,
> + minfo->irom.build, minfo->fw.major,
> + minfo->fw.minor, minfo->fw.build);
> + break;
> + default:
> + APPEND_STRING(buf, len,
> + "Event %02x (%04x) [", r->type, r->data);
> + for (i = 0; i < PD_LOG_SIZE(r->size_port); i++)
> + APPEND_STRING(buf, len, "%02x ", r->payload[i]);
> + APPEND_STRING(buf, len, "]");
> + break;
> + }
> +
> + pr_info("PDLOG %d/%02d/%02d %02d:%02d:%02d.%03d P%d %s\n",
> + rt.tm_year + 1900, rt.tm_mon + 1, rt.tm_mday,
> + rt.tm_hour, rt.tm_min, rt.tm_sec,
> + (int)(ktime_to_ms(tstamp) % MSEC_PER_SEC),
> + PD_LOG_PORT(r->size_port), buf);
Is the kernel log a good place to be putting this information? Maybe
this should go into debugfs?
> +}
> +
> +static void cros_usb_pd_log_check(struct work_struct *work)
> +{
> + struct charger_data *charger = container_of(to_delayed_work(work),
> + struct charger_data, log_work);
> + struct device *dev = charger->dev;
> + union {
> + struct ec_response_pd_log r;
> + uint32_t words[8]; /* space for the payload */
> + } u;
> + int ret;
> + int entries = 0;
> + ktime_t now;
> +
> + if (charger->suspended) {
> + dev_dbg(dev, "Suspended...bailing out\n");
> + return;
> + }
> +
> + while (entries++ < CROS_USB_PD_MAX_LOG_ENTRIES) {
> + ret = ec_command(charger->ec_dev, 0, EC_CMD_PD_GET_LOG_ENTRY,
> + NULL, 0, (uint8_t *)&u, sizeof(u));
> + now = ktime_get_real();
> + if (ret < 0) {
> + dev_dbg(dev, "Cannot get PD log %d\n", ret);
> + break;
> + }
> + if (u.r.type == PD_EVENT_NO_ENTRY)
> + break;
> +
> + cros_usb_pd_print_log_entry(&u.r, now);
> + }
> +
> + queue_delayed_work(charger->log_workqueue, &charger->log_work,
> + CROS_USB_PD_LOG_UPDATE_DELAY);
> +}
> +
> +static int cros_usb_pd_ec_event(struct notifier_block *nb,
> + unsigned long queued_during_suspend, void *_notify)
> +{
> + struct charger_data *charger;
> + struct device *dev;
> + struct cros_ec_device *ec_device;
> + u32 host_event;
> +
> + charger = container_of(nb, struct charger_data, notifier);
> + dev = charger->dev;
> + ec_device = charger->ec_device;
> +
> + host_event = cros_ec_get_host_event(ec_device);
> + if (host_event & EC_HOST_EVENT_MASK(EC_HOST_EVENT_PD_MCU)) {
> + cros_usb_pd_charger_power_changed(charger->ports[0]->psy);
> + return NOTIFY_OK;
> + } else {
> + return NOTIFY_DONE;
> + }
Simplify to return NOTIFY_DONE without the else.
> +}
> +
> +static char *charger_supplied_to[] = {"cros-usb_pd-charger"};
const?
> +
> +static int cros_usb_pd_charger_probe(struct platform_device *pd)
> +{
> + struct device *dev = &pd->dev;
> + struct cros_ec_dev *ec_dev = dev_get_drvdata(pd->dev.parent);
> + struct cros_ec_device *ec_device;
> + struct charger_data *charger;
> + struct port_data *port;
> + struct power_supply_desc *psy_desc;
> + struct power_supply_config psy_cfg = {};
> + int i;
> + int ret = -EINVAL;
> +
> + dev_dbg(dev, "cros_usb_pd_charger_probe\n");
> + if (!ec_dev) {
> + WARN(1, "%s: No EC dev found\n", dev_name(dev));
> + return -EINVAL;
> + }
> +
> + ec_device = ec_dev->ec_dev;
> + if (!ec_device) {
> + WARN(1, "%s: No EC device found\n", dev_name(dev));
> + return -EINVAL;
> + }
> +
> + 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);
> +
> + if ((get_ec_num_ports(charger, &charger->num_charger_ports) < 0) ||
> + !charger->num_charger_ports) {
> + /*
> + * 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");
dev_err?
> + ret = -ENODEV;
> + goto fail_nowarn;
> + }
> +
> + for (i = 0; i < charger->num_charger_ports; i++) {
> + port = devm_kzalloc(dev, sizeof(struct port_data), GFP_KERNEL);
> + if (!port) {
> + dev_err(dev, "Failed to alloc port structure\n");
We don't need error messages on allocation failures (checkpatch usually
catches this).
> + 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_usb_pd_charger_get_prop;
> + psy_desc->set_property = cros_usb_pd_charger_set_prop;
> + psy_desc->property_is_writeable =
> + cros_usb_pd_charger_is_writeable;
> + psy_desc->external_power_changed =
> + cros_usb_pd_charger_power_changed;
> + psy_desc->properties = cros_usb_pd_charger_props;
> + psy_desc->num_properties = ARRAY_SIZE(
> + cros_usb_pd_charger_props);
> +
> + psy_cfg.drv_data = port;
> + psy_cfg.supplied_to = charger_supplied_to;
> + psy_cfg.num_supplicants = ARRAY_SIZE(charger_supplied_to);
> +
> + port->psy = power_supply_register_no_ws(dev, psy_desc,
> + &psy_cfg);
> + if (IS_ERR(port->psy)) {
> + dev_err(dev, "Failed to register power supply: %ld\n",
> + PTR_ERR(port->psy));
> + continue;
> + }
> +
> + charger->ports[charger->num_registered_psy++] = port;
> + }
> +
> + 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_usb_pd_ec_event;
> + ret = blocking_notifier_chain_register(
> + &ec_device->event_notifier, &charger->notifier);
> + if (ret < 0)
> + dev_warn(dev, "failed to register notifier\n");
> + }
> +
> + /* Retrieve PD event logs periodically */
> + INIT_DELAYED_WORK(&charger->log_work, cros_usb_pd_log_check);
> + charger->log_workqueue =
> + create_singlethread_workqueue("cros_usb_pd_log");
> + queue_delayed_work(charger->log_workqueue, &charger->log_work,
> + CROS_USB_PD_LOG_UPDATE_DELAY);
> +
> + return 0;
> +
> +fail:
> + WARN(1, "%s: Failing probe (err:0x%x)\n", dev_name(dev), ret);
> +
> +fail_nowarn:
> + if (charger) {
This is always true?
> + for (i = 0; i < charger->num_registered_psy; i++) {
> + port = charger->ports[i];
> + power_supply_unregister(port->psy);
> + devm_kfree(dev, port);
> + }
> + platform_set_drvdata(pd, NULL);
> + devm_kfree(dev, charger);
Seems like we could let devres take care of this.
> + }
> +
> + dev_info(dev, "Failing probe (err:0x%x)\n", ret);
Is this necessary? Doesn't driver core already spew something out for
this case if some debugging flag is enabled?
> + return ret;
> +}
> +
> +static int cros_usb_pd_charger_remove(struct platform_device *pd)
> +{
> + struct charger_data *charger = platform_get_drvdata(pd);
> + struct device *dev = charger->dev;
> + struct port_data *port;
> + int i;
> +
> + if (charger) {
We just derefed charger a few lines up.
> + for (i = 0; i < charger->num_registered_psy; i++) {
> + port = charger->ports[i];
> + power_supply_unregister(port->psy);
> + devm_kfree(dev, port);
Can't we let driver removal take care of this?
> + }
> + flush_delayed_work(&charger->log_work);
> + platform_set_drvdata(pd, NULL);
This is not needed.
> + devm_kfree(dev, charger);
Again, driver remove already does this?
> + }
> + return 0;
> +}
> +
> +#ifdef CONFIG_PM_SLEEP
> +static int cros_usb_pd_charger_resume(struct device *dev)
> +{
> + struct charger_data *charger = dev_get_drvdata(dev);
> + int i;
> +
> + if (!charger)
> + return 0;
Seems sort of impossible...
> +
> + charger->suspended = false;
> +
> + dev_dbg(dev, "cros_usb_pd_charger_resume: updating power supplies\n");
> + for (i = 0; i < charger->num_registered_psy; i++) {
> + power_supply_changed(charger->ports[i]->psy);
> + charger->ports[i]->last_update =
> + jiffies - CROS_USB_PD_CACHE_UPDATE_DELAY;
> + }
> + queue_delayed_work(charger->log_workqueue, &charger->log_work,
> + CROS_USB_PD_LOG_UPDATE_DELAY);
> +
> + return 0;
> +}
> +
> +static int cros_usb_pd_charger_suspend(struct device *dev)
> +{
> + struct charger_data *charger = dev_get_drvdata(dev);
> +
> + charger->suspended = true;
> +
> + if (charger)
We just derefed charger so it better be non-NULL
> + flush_delayed_work(&charger->log_work);
> + return 0;
> +}
> +#endif
> +
> +static int set_ec_ext_power_limit(struct cros_ec_dev *ec,
> + uint16_t current_lim, uint16_t voltage_lim)
> +{
> + struct ec_params_external_power_limit_v1 req;
> + int ret;
> +
> + req.current_lim = current_lim;
> + req.voltage_lim = voltage_lim;
> +
> + ret = ec_command(ec, 0, EC_CMD_EXT_POWER_CURRENT_LIMIT,
> + (uint8_t *)&req, sizeof(req), NULL, 0);
> + if (ret < 0) {
> + dev_warn(ec->dev,
> + "External power limit command returned 0x%x\n",
> + ret);
dev_err?
> + return -EINVAL;
> + }
> +
> + return 0;
> +}
> +
> +static ssize_t get_ec_ext_current_lim(struct device *dev,
> + struct device_attribute *attr,
> + char *buf)
> +{
> + return scnprintf(buf, PAGE_SIZE, "%d\n", ext_current_lim);
> +}
> +
> +static ssize_t set_ec_ext_current_lim(struct device *dev,
> + struct device_attribute *attr,
> + const char *buf, size_t count)
> +{
> + int ret;
> + uint16_t tmp_val;
> + struct cros_ec_dev *ec = container_of(
> + dev, struct cros_ec_dev, class_dev);
> +
> + if (kstrtou16(buf, 0, &tmp_val) < 0)
> + return -EINVAL;
> +
> + ret = set_ec_ext_power_limit(ec, tmp_val, ext_voltage_lim);
> + if (ret < 0)
> + return ret;
> +
> + ext_current_lim = tmp_val;
> + if (ext_current_lim == EC_POWER_LIMIT_NONE)
> + dev_info(ec->dev, "External current limit removed\n");
> + else
> + dev_info(ec->dev, "External current limit set to %dmA\n",
> + ext_current_lim);
Why are we dev_info printing on sysfs writes?
> +
> + return count;
> +}
> +
> +static ssize_t get_ec_ext_voltage_lim(struct device *dev,
> + struct device_attribute *attr,
> + char *buf)
> +{
> + return scnprintf(buf, PAGE_SIZE, "%d\n", ext_voltage_lim);
> +}
> +
> +static ssize_t set_ec_ext_voltage_lim(struct device *dev,
> + struct device_attribute *attr,
> + const char *buf, size_t count)
> +{
> + int ret;
> + uint16_t tmp_val;
> +
> + struct cros_ec_dev *ec = container_of(
> + dev, struct cros_ec_dev, class_dev);
> +
> + if (kstrtou16(buf, 0, &tmp_val) < 0)
> + return -EINVAL;
> +
> + ret = set_ec_ext_power_limit(ec, ext_current_lim, tmp_val);
> + if (ret < 0)
> + return ret;
> +
> + ext_voltage_lim = tmp_val;
> + if (ext_voltage_lim == EC_POWER_LIMIT_NONE)
> + dev_info(ec->dev, "External voltage limit removed\n");
> + else
> + dev_info(ec->dev, "External voltage limit set to %dmV\n",
> + ext_voltage_lim);
Again...
> +
> + return count;
> +}
> +
> +static DEVICE_ATTR(ext_current_lim, S_IWUSR | S_IWGRP | S_IRUGO,
> + get_ec_ext_current_lim,
> + set_ec_ext_current_lim);
> +static DEVICE_ATTR(ext_voltage_lim, S_IWUSR | S_IWGRP | S_IRUGO,
> + get_ec_ext_voltage_lim,
> + set_ec_ext_voltage_lim);
Any documentation for these in Documentation/ABI?
> +
> +static struct attribute *__ext_power_cmds_attrs[] = {
> + &dev_attr_ext_current_lim.attr,
> + &dev_attr_ext_voltage_lim.attr,
> + NULL,
> +};
> +
> +struct attribute_group cros_usb_pd_charger_attr_group = {
static?
> + .name = "usb-pd-charger",
> + .attrs = __ext_power_cmds_attrs,
> +};
> +
> +static SIMPLE_DEV_PM_OPS(cros_usb_pd_charger_pm_ops,
> + cros_usb_pd_charger_suspend, cros_usb_pd_charger_resume);
const?
> +
> +static struct platform_driver cros_usb_pd_charger_driver = {
> + .driver = {
> + .name = "cros-usb-pd-charger",
> + .owner = THIS_MODULE,
This isn't needed anymore.
> + .pm = &cros_usb_pd_charger_pm_ops,
> + },
> + .probe = cros_usb_pd_charger_probe,
> + .remove = cros_usb_pd_charger_remove,
> +};
> +
> +module_platform_driver(cros_usb_pd_charger_driver);
>
--
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum,
a Linux Foundation Collaborative Project
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH v2 6/8] power: cros_usbpd-charger: Add EC-based USB PD charger driver
2016-02-13 0:46 ` Stephen Boyd
@ 2016-02-16 14:35 ` Tomeu Vizoso
0 siblings, 0 replies; 11+ messages in thread
From: Tomeu Vizoso @ 2016-02-16 14:35 UTC (permalink / raw)
To: Stephen Boyd
Cc: linux-kernel@vger.kernel.org, Sameer Nanda, Lee Jones,
Benson Leung, Enric Balletbò, Vic Yang, Vincent Palatin,
Randall Spangler, Todd Broch, Gwendal Grignou, Shawn Nematbakhsh,
Sebastian Reichel, Dmitry Eremin-Solenikov,
linux-pm@vger.kernel.org, David Woodhouse
On 13 February 2016 at 01:46, Stephen Boyd <sboyd@codeaurora.org> wrote:
> On 02/12/2016 04:57 AM, Tomeu Vizoso wrote:
>> +
>> +#define EC_MAX_IN_SIZE EC_PROTO2_MAX_REQUEST_SIZE
>> +#define EC_MAX_OUT_SIZE EC_PROTO2_MAX_RESPONSE_SIZE
>> +uint8_t ec_inbuf[EC_MAX_IN_SIZE];
>> +uint8_t ec_outbuf[EC_MAX_OUT_SIZE];
>
> static? Why can't these be part of charger_data?
Actually, I don't think we need any of that pre-allocation.
>> +static int set_ec_usb_pd_override_ports(struct charger_data *charger,
>> + int port_num)
>> +{
>> + struct device *dev = charger->dev;
>> + struct ec_params_charge_port_override req;
>> + int ret;
>> +
>> + req.override_port = port_num;
>> +
>> + ret = ec_command(charger->ec_dev, 0, EC_CMD_PD_CHARGE_PORT_OVERRIDE,
>> + (uint8_t *)&req, sizeof(req),
>> + NULL, 0);
>> + if (ret < 0) {
>> + dev_info(dev, "Port Override command returned 0x%x\n", ret);
>
> dev_err?
Sounds good.
>> + pr_info("PDLOG %d/%02d/%02d %02d:%02d:%02d.%03d P%d %s\n",
>> + rt.tm_year + 1900, rt.tm_mon + 1, rt.tm_mday,
>> + rt.tm_hour, rt.tm_min, rt.tm_sec,
>> + (int)(ktime_to_ms(tstamp) % MSEC_PER_SEC),
>> + PD_LOG_PORT(r->size_port), buf);
>
> Is the kernel log a good place to be putting this information? Maybe
> this should go into debugfs?
It should be really sparse and it has already proved useful to users
willing to know if their Type-C cables are broken.We could also make
it dev_dbg.
http://www.ibtimes.co.uk/googler-shows-how-test-compatibility-usb-c-cable-chromebook-pixel-2015-1527334
>> + return NOTIFY_OK;
>> + } else {
>> + return NOTIFY_DONE;
>> + }
>
> Simplify to return NOTIFY_DONE without the else.
Ok.
>> +}
>> +
>> +static char *charger_supplied_to[] = {"cros-usb_pd-charger"};
>
> const?
In principle yes, but power_supply_config.supplied_to isn't const.
>> + !charger->num_charger_ports) {
>> + /*
>> + * 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");
>
> dev_err?
I think so, but will also need to make sure that we only register a
device if we have one or more charging port.
>> + ret = -ENODEV;
>> + goto fail_nowarn;
>> + }
>> +
>> + for (i = 0; i < charger->num_charger_ports; i++) {
>> + port = devm_kzalloc(dev, sizeof(struct port_data), GFP_KERNEL);
>> + if (!port) {
>> + dev_err(dev, "Failed to alloc port structure\n");
>
> We don't need error messages on allocation failures (checkpatch usually
> catches this).
It did catch another one in this file, not sure why it failed here.
>> +
>> +fail_nowarn:
>> + if (charger) {
>
> This is always true?
Ah, yes.
>> + for (i = 0; i < charger->num_registered_psy; i++) {
>> + port = charger->ports[i];
>> + power_supply_unregister(port->psy);
>> + devm_kfree(dev, port);
>> + }
>> + platform_set_drvdata(pd, NULL);
>> + devm_kfree(dev, charger);
>
> Seems like we could let devres take care of this.
Indeed.
>> + }
>> +
>> + dev_info(dev, "Failing probe (err:0x%x)\n", ret);
>
> Is this necessary? Doesn't driver core already spew something out for
> this case if some debugging flag is enabled?
True, a warning is printed.
>> + return ret;
>> +}
>> +
>> +static int cros_usb_pd_charger_remove(struct platform_device *pd)
>> +{
>> + struct charger_data *charger = platform_get_drvdata(pd);
>> + struct device *dev = charger->dev;
>> + struct port_data *port;
>> + int i;
>> +
>> + if (charger) {
>
> We just derefed charger a few lines up.
Ok.
>> + for (i = 0; i < charger->num_registered_psy; i++) {
>> + port = charger->ports[i];
>> + power_supply_unregister(port->psy);
>> + devm_kfree(dev, port);
>
> Can't we let driver removal take care of this?
Ok.
>> + }
>> + flush_delayed_work(&charger->log_work);
>> + platform_set_drvdata(pd, NULL);
>
> This is not needed.
Ok.
>> + devm_kfree(dev, charger);
>
> Again, driver remove already does this?
Yup.
>> + }
>> + return 0;
>> +}
>> +
>> +#ifdef CONFIG_PM_SLEEP
>> +static int cros_usb_pd_charger_resume(struct device *dev)
>> +{
>> + struct charger_data *charger = dev_get_drvdata(dev);
>> + int i;
>> +
>> + if (!charger)
>> + return 0;
>
> Seems sort of impossible...
True.
>> +
>> + charger->suspended = false;
>> +
>> + dev_dbg(dev, "cros_usb_pd_charger_resume: updating power supplies\n");
>> + for (i = 0; i < charger->num_registered_psy; i++) {
>> + power_supply_changed(charger->ports[i]->psy);
>> + charger->ports[i]->last_update =
>> + jiffies - CROS_USB_PD_CACHE_UPDATE_DELAY;
>> + }
>> + queue_delayed_work(charger->log_workqueue, &charger->log_work,
>> + CROS_USB_PD_LOG_UPDATE_DELAY);
>> +
>> + return 0;
>> +}
>> +
>> +static int cros_usb_pd_charger_suspend(struct device *dev)
>> +{
>> + struct charger_data *charger = dev_get_drvdata(dev);
>> +
>> + charger->suspended = true;
>> +
>> + if (charger)
>
> We just derefed charger so it better be non-NULL
Yup.
>> + ret = ec_command(ec, 0, EC_CMD_EXT_POWER_CURRENT_LIMIT,
>> + (uint8_t *)&req, sizeof(req), NULL, 0);
>> + if (ret < 0) {
>> + dev_warn(ec->dev,
>> + "External power limit command returned 0x%x\n",
>> + ret);
>
> dev_err?
Sounds good to me.
>> + if (ext_current_lim == EC_POWER_LIMIT_NONE)
>> + dev_info(ec->dev, "External current limit removed\n");
>> + else
>> + dev_info(ec->dev, "External current limit set to %dmA\n",
>> + ext_current_lim);
>
> Why are we dev_info printing on sysfs writes?
Cannot think of a good reason, so I'm changing those to dev_dbg.
>> + ext_voltage_lim = tmp_val;
>> + if (ext_voltage_lim == EC_POWER_LIMIT_NONE)
>> + dev_info(ec->dev, "External voltage limit removed\n");
>> + else
>> + dev_info(ec->dev, "External voltage limit set to %dmV\n",
>> + ext_voltage_lim);
>
> Again...
Ok.
>> +
>> + return count;
>> +}
>> +
>> +static DEVICE_ATTR(ext_current_lim, S_IWUSR | S_IWGRP | S_IRUGO,
>> + get_ec_ext_current_lim,
>> + set_ec_ext_current_lim);
>> +static DEVICE_ATTR(ext_voltage_lim, S_IWUSR | S_IWGRP | S_IRUGO,
>> + get_ec_ext_voltage_lim,
>> + set_ec_ext_voltage_lim);
>
> Any documentation for these in Documentation/ABI?
Will add.
>> +static struct attribute *__ext_power_cmds_attrs[] = {
>> + &dev_attr_ext_current_lim.attr,
>> + &dev_attr_ext_voltage_lim.attr,
>> + NULL,
>> +};
>> +
>> +struct attribute_group cros_usb_pd_charger_attr_group = {
>
> static?
Not really, as it's to be used externally.
>> + .name = "usb-pd-charger",
>> + .attrs = __ext_power_cmds_attrs,
>> +};
>> +
>> +static SIMPLE_DEV_PM_OPS(cros_usb_pd_charger_pm_ops,
>> + cros_usb_pd_charger_suspend, cros_usb_pd_charger_resume);
>
> const?
The macro already has the const keyword.
>> +
>> +static struct platform_driver cros_usb_pd_charger_driver = {
>> + .driver = {
>> + .name = "cros-usb-pd-charger",
>> + .owner = THIS_MODULE,
>
> This isn't needed anymore.
Ok.
Thank you very much for the great review!
Tomeu
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH v2 2/8] power_supply: Add types for USB Type C and PD chargers
2016-02-12 12:56 ` [PATCH v2 2/8] power_supply: Add types for USB Type C and PD chargers Tomeu Vizoso
@ 2016-02-16 16:59 ` Sebastian Reichel
0 siblings, 0 replies; 11+ messages in thread
From: Sebastian Reichel @ 2016-02-16 16:59 UTC (permalink / raw)
To: Tomeu Vizoso
Cc: linux-kernel, Sameer Nanda, Lee Jones, Benson Leung,
Enric Balletbò, Vic Yang, Vincent Palatin, Randall Spangler,
Todd Broch, Gwendal Grignou, Dmitry Eremin-Solenikov,
David Woodhouse, linux-pm
[-- Attachment #1: Type: text/plain, Size: 862 bytes --]
Hi Tomeu,
On Fri, Feb 12, 2016 at 01:56:59PM +0100, Tomeu Vizoso wrote:
> From: Benson Leung <bleung@chromium.org>
>
> This adds power supply types for USB chargers defined in
> the USB Type-C Specification 1.1 and in the
> USB Power Delivery Specification Revision 2.0 V1.1.
>
> The following are added :
> POWER_SUPPLY_TYPE_USB_TYPE_C, /* Type C Port */
> POWER_SUPPLY_TYPE_USB_PD, /* Power Delivery Port */
> POWER_SUPPLY_TYPE_USB_PD_DRP, /* PD Dual Role Port */
>
> Signed-off-by: Benson Leung <bleung@chromium.org>
> [tomeu: remove the mention to Type C from the comments]
> Signed-off-by: Tomeu Vizoso <tomeu.vizoso@collabora.com>
> Reviewed-by: Alec Berg <alecaberg@chromium.org>
> Reviewed-by: Vincent Palatin <vpalatin@chromium.org>
> Reviewed-by: Todd Broch <tbroch@chromium.org>
I queued this patch for 4.6.
-- Sebastian
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 819 bytes --]
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH v2 0/8] EC-based USB Power Delivery support for Chrome machines
2016-02-12 12:56 [PATCH v2 0/8] EC-based USB Power Delivery support for Chrome machines Tomeu Vizoso
2016-02-12 12:56 ` [PATCH v2 2/8] power_supply: Add types for USB Type C and PD chargers Tomeu Vizoso
2016-02-12 12:57 ` [PATCH v2 6/8] power: cros_usbpd-charger: Add EC-based USB PD charger driver Tomeu Vizoso
@ 2016-02-20 16:22 ` Pavel Machek
2016-02-22 7:56 ` Tomeu Vizoso
2 siblings, 1 reply; 11+ messages in thread
From: Pavel Machek @ 2016-02-20 16:22 UTC (permalink / raw)
To: Tomeu Vizoso
Cc: linux-kernel, Sameer Nanda, Lee Jones, Benson Leung,
Enric Balletbò, Vic Yang, Vincent Palatin, Randall Spangler,
Todd Broch, Gwendal Grignou, linux-pm, Sebastian Reichel,
Dmitry Eremin-Solenikov, Dmitry Torokhov, David Woodhouse,
linux-input, Olof Johansson
Hi!
> this series contains a driver that exposes a power_supply to userspace
> representing a port that support USB PD charging.
Well, if this is meant for userspace, should it add some
documentation, too?
Thanks,
Pavel
--
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH v2 0/8] EC-based USB Power Delivery support for Chrome machines
2016-02-20 16:22 ` [PATCH v2 0/8] EC-based USB Power Delivery support for Chrome machines Pavel Machek
@ 2016-02-22 7:56 ` Tomeu Vizoso
0 siblings, 0 replies; 11+ messages in thread
From: Tomeu Vizoso @ 2016-02-22 7:56 UTC (permalink / raw)
To: Pavel Machek
Cc: linux-kernel@vger.kernel.org, Sameer Nanda, Lee Jones,
Benson Leung, Enric Balletbò, Vic Yang, Vincent Palatin,
Randall Spangler, Todd Broch, Gwendal Grignou,
linux-pm@vger.kernel.org, Sebastian Reichel,
Dmitry Eremin-Solenikov, Dmitry Torokhov, David Woodhouse,
linux-input, Olof Johansson
On 20 February 2016 at 17:22, Pavel Machek <pavel@ucw.cz> wrote:
> Hi!
>
>> this series contains a driver that exposes a power_supply to userspace
>> representing a port that support USB PD charging.
>
> Well, if this is meant for userspace, should it add some
> documentation, too?
Hi Pavel, in v3 I have dropped any new sysfs attributes, so everything
exposed to userspace is already documented.
Thanks,
Tomeu
> Thanks,
> Pavel
>
> --
> (english) http://www.livejournal.com/~pavelmachek
> (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
^ permalink raw reply [flat|nested] 11+ messages in thread
end of thread, other threads:[~2016-02-22 7:56 UTC | newest]
Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-02-12 12:56 [PATCH v2 0/8] EC-based USB Power Delivery support for Chrome machines Tomeu Vizoso
2016-02-12 12:56 ` [PATCH v2 2/8] power_supply: Add types for USB Type C and PD chargers Tomeu Vizoso
2016-02-16 16:59 ` Sebastian Reichel
2016-02-12 12:57 ` [PATCH v2 6/8] power: cros_usbpd-charger: Add EC-based USB PD charger driver Tomeu Vizoso
2016-02-12 14:14 ` kbuild test robot
2016-02-12 15:01 ` kbuild test robot
2016-02-12 15:38 ` kbuild test robot
2016-02-13 0:46 ` Stephen Boyd
2016-02-16 14:35 ` Tomeu Vizoso
2016-02-20 16:22 ` [PATCH v2 0/8] EC-based USB Power Delivery support for Chrome machines Pavel Machek
2016-02-22 7:56 ` Tomeu Vizoso
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).