1 // SPDX-License-Identifier: GPL-2.0+
3 * Power supply driver for ChromeOS EC based USB PD Charger.
5 * Copyright (c) 2014 - 2018 Google, Inc
8 #include <linux/module.h>
9 #include <linux/mfd/cros_ec.h>
10 #include <linux/mfd/cros_ec_commands.h>
11 #include <linux/platform_device.h>
12 #include <linux/power_supply.h>
13 #include <linux/slab.h>
15 #define CHARGER_USBPD_DIR_NAME "CROS_USBPD_CHARGER%d"
16 #define CHARGER_DEDICATED_DIR_NAME "CROS_DEDICATED_CHARGER"
17 #define CHARGER_DIR_NAME_LENGTH (sizeof(CHARGER_USBPD_DIR_NAME) >= \
18 sizeof(CHARGER_DEDICATED_DIR_NAME) ? \
19 sizeof(CHARGER_USBPD_DIR_NAME) : \
20 sizeof(CHARGER_DEDICATED_DIR_NAME))
21 #define CHARGER_CACHE_UPDATE_DELAY msecs_to_jiffies(500)
22 #define CHARGER_MANUFACTURER_MODEL_LENGTH 32
24 #define DRV_NAME "cros-usbpd-charger"
28 char name[CHARGER_DIR_NAME_LENGTH];
29 char manufacturer[CHARGER_MANUFACTURER_MODEL_LENGTH];
30 char model_name[CHARGER_MANUFACTURER_MODEL_LENGTH];
31 struct power_supply *psy;
32 struct power_supply_desc psy_desc;
37 int psy_voltage_max_design;
40 struct charger_data *charger;
41 unsigned long last_update;
46 struct cros_ec_dev *ec_dev;
47 struct cros_ec_device *ec_device;
48 int num_charger_ports;
50 int num_registered_psy;
51 struct port_data *ports[EC_USB_PD_MAX_PORTS];
52 struct notifier_block notifier;
55 static enum power_supply_property cros_usbpd_charger_props[] = {
56 POWER_SUPPLY_PROP_ONLINE,
57 POWER_SUPPLY_PROP_STATUS,
58 POWER_SUPPLY_PROP_CURRENT_MAX,
59 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
60 POWER_SUPPLY_PROP_VOLTAGE_NOW,
61 POWER_SUPPLY_PROP_MODEL_NAME,
62 POWER_SUPPLY_PROP_MANUFACTURER,
63 POWER_SUPPLY_PROP_USB_TYPE
66 static enum power_supply_property cros_usbpd_dedicated_charger_props[] = {
67 POWER_SUPPLY_PROP_ONLINE,
68 POWER_SUPPLY_PROP_STATUS,
69 POWER_SUPPLY_PROP_VOLTAGE_NOW,
72 static enum power_supply_usb_type cros_usbpd_charger_usb_types[] = {
73 POWER_SUPPLY_USB_TYPE_UNKNOWN,
74 POWER_SUPPLY_USB_TYPE_SDP,
75 POWER_SUPPLY_USB_TYPE_DCP,
76 POWER_SUPPLY_USB_TYPE_CDP,
77 POWER_SUPPLY_USB_TYPE_C,
78 POWER_SUPPLY_USB_TYPE_PD,
79 POWER_SUPPLY_USB_TYPE_PD_DRP,
80 POWER_SUPPLY_USB_TYPE_APPLE_BRICK_ID
83 static bool cros_usbpd_charger_port_is_dedicated(struct port_data *port)
85 return port->port_number >= port->charger->num_usbpd_ports;
88 static int cros_usbpd_charger_ec_command(struct charger_data *charger,
96 struct cros_ec_dev *ec_dev = charger->ec_dev;
97 struct cros_ec_command *msg;
100 msg = kzalloc(sizeof(*msg) + max(outsize, insize), GFP_KERNEL);
104 msg->version = version;
105 msg->command = ec_dev->cmd_offset + command;
106 msg->outsize = outsize;
107 msg->insize = insize;
110 memcpy(msg->data, outdata, outsize);
112 ret = cros_ec_cmd_xfer_status(charger->ec_device, msg);
113 if (ret >= 0 && insize)
114 memcpy(indata, msg->data, insize);
120 static int cros_usbpd_charger_get_num_ports(struct charger_data *charger)
122 struct ec_response_charge_port_count resp;
125 ret = cros_usbpd_charger_ec_command(charger, 0,
126 EC_CMD_CHARGE_PORT_COUNT,
127 NULL, 0, &resp, sizeof(resp));
129 dev_err(charger->dev,
130 "Unable to get the number of ports (err:0x%x)\n", ret);
134 return resp.port_count;
137 static int cros_usbpd_charger_get_usbpd_num_ports(struct charger_data *charger)
139 struct ec_response_usb_pd_ports resp;
142 ret = cros_usbpd_charger_ec_command(charger, 0, EC_CMD_USB_PD_PORTS,
143 NULL, 0, &resp, sizeof(resp));
145 dev_err(charger->dev,
146 "Unable to get the number or ports (err:0x%x)\n", ret);
150 return resp.num_ports;
153 static int cros_usbpd_charger_get_discovery_info(struct port_data *port)
155 struct charger_data *charger = port->charger;
156 struct ec_params_usb_pd_discovery_entry resp;
157 struct ec_params_usb_pd_info_request req;
160 req.port = port->port_number;
162 ret = cros_usbpd_charger_ec_command(charger, 0,
163 EC_CMD_USB_PD_DISCOVERY,
165 &resp, sizeof(resp));
167 dev_err(charger->dev,
168 "Unable to query discovery info (err:0x%x)\n", ret);
172 dev_dbg(charger->dev, "Port %d: VID = 0x%x, PID=0x%x, PTYPE=0x%x\n",
173 port->port_number, resp.vid, resp.pid, resp.ptype);
175 snprintf(port->manufacturer, sizeof(port->manufacturer), "%x",
177 snprintf(port->model_name, sizeof(port->model_name), "%x", resp.pid);
182 static int cros_usbpd_charger_get_power_info(struct port_data *port)
184 struct charger_data *charger = port->charger;
185 struct ec_response_usb_pd_power_info resp;
186 struct ec_params_usb_pd_power_info req;
187 int last_psy_status, last_psy_usb_type;
188 struct device *dev = charger->dev;
191 req.port = port->port_number;
192 ret = cros_usbpd_charger_ec_command(charger, 0,
193 EC_CMD_USB_PD_POWER_INFO,
195 &resp, sizeof(resp));
197 dev_err(dev, "Unable to query PD power info (err:0x%x)\n", ret);
201 last_psy_status = port->psy_status;
202 last_psy_usb_type = port->psy_usb_type;
205 case USB_PD_PORT_POWER_DISCONNECTED:
206 port->psy_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
207 port->psy_online = 0;
209 case USB_PD_PORT_POWER_SOURCE:
210 port->psy_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
211 port->psy_online = 0;
213 case USB_PD_PORT_POWER_SINK:
214 port->psy_status = POWER_SUPPLY_STATUS_CHARGING;
215 port->psy_online = 1;
217 case USB_PD_PORT_POWER_SINK_NOT_CHARGING:
218 port->psy_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
219 port->psy_online = 1;
222 dev_err(dev, "Unknown role %d\n", resp.role);
226 port->psy_voltage_max_design = resp.meas.voltage_max;
227 port->psy_voltage_now = resp.meas.voltage_now;
228 port->psy_current_max = resp.meas.current_max;
229 port->psy_power_max = resp.max_power;
232 case USB_CHG_TYPE_BC12_SDP:
233 case USB_CHG_TYPE_VBUS:
234 port->psy_usb_type = POWER_SUPPLY_USB_TYPE_SDP;
236 case USB_CHG_TYPE_NONE:
238 * For dual-role devices when we are a source, the firmware
239 * reports the type as NONE. Report such chargers as type
242 if (resp.role == USB_PD_PORT_POWER_SOURCE && resp.dualrole)
243 port->psy_usb_type = POWER_SUPPLY_USB_TYPE_PD_DRP;
245 port->psy_usb_type = POWER_SUPPLY_USB_TYPE_SDP;
247 case USB_CHG_TYPE_OTHER:
248 case USB_CHG_TYPE_PROPRIETARY:
249 port->psy_usb_type = POWER_SUPPLY_USB_TYPE_APPLE_BRICK_ID;
252 port->psy_usb_type = POWER_SUPPLY_USB_TYPE_C;
254 case USB_CHG_TYPE_BC12_DCP:
255 port->psy_usb_type = POWER_SUPPLY_USB_TYPE_DCP;
257 case USB_CHG_TYPE_BC12_CDP:
258 port->psy_usb_type = POWER_SUPPLY_USB_TYPE_CDP;
260 case USB_CHG_TYPE_PD:
262 port->psy_usb_type = POWER_SUPPLY_USB_TYPE_PD_DRP;
264 port->psy_usb_type = POWER_SUPPLY_USB_TYPE_PD;
266 case USB_CHG_TYPE_UNKNOWN:
268 * While the EC is trying to determine the type of charger that
269 * has been plugged in, it will report the charger type as
270 * unknown. Additionally since the power capabilities are
271 * unknown, report the max current and voltage as zero.
273 port->psy_usb_type = POWER_SUPPLY_USB_TYPE_UNKNOWN;
274 port->psy_voltage_max_design = 0;
275 port->psy_current_max = 0;
278 dev_err(dev, "Port %d: default case!\n", port->port_number);
279 port->psy_usb_type = POWER_SUPPLY_USB_TYPE_SDP;
282 if (cros_usbpd_charger_port_is_dedicated(port))
283 port->psy_desc.type = POWER_SUPPLY_TYPE_MAINS;
285 port->psy_desc.type = POWER_SUPPLY_TYPE_USB;
288 "Port %d: type=%d vmax=%d vnow=%d cmax=%d clim=%d pmax=%d\n",
289 port->port_number, resp.type, resp.meas.voltage_max,
290 resp.meas.voltage_now, resp.meas.current_max,
291 resp.meas.current_lim, resp.max_power);
294 * If power supply type or status changed, explicitly call
295 * power_supply_changed. This results in udev event getting generated
296 * and allows user mode apps to react quicker instead of waiting for
297 * their next poll of power supply status.
299 if (last_psy_usb_type != port->psy_usb_type ||
300 last_psy_status != port->psy_status)
301 power_supply_changed(port->psy);
306 static int cros_usbpd_charger_get_port_status(struct port_data *port,
312 time_is_after_jiffies(port->last_update +
313 CHARGER_CACHE_UPDATE_DELAY))
316 ret = cros_usbpd_charger_get_power_info(port);
320 if (!cros_usbpd_charger_port_is_dedicated(port))
321 ret = cros_usbpd_charger_get_discovery_info(port);
322 port->last_update = jiffies;
327 static void cros_usbpd_charger_power_changed(struct power_supply *psy)
329 struct port_data *port = power_supply_get_drvdata(psy);
330 struct charger_data *charger = port->charger;
333 for (i = 0; i < charger->num_registered_psy; i++)
334 cros_usbpd_charger_get_port_status(charger->ports[i], false);
337 static int cros_usbpd_charger_get_prop(struct power_supply *psy,
338 enum power_supply_property psp,
339 union power_supply_propval *val)
341 struct port_data *port = power_supply_get_drvdata(psy);
342 struct charger_data *charger = port->charger;
343 struct cros_ec_device *ec_device = charger->ec_device;
344 struct device *dev = charger->dev;
347 /* Only refresh ec_port_status for dynamic properties */
349 case POWER_SUPPLY_PROP_ONLINE:
351 * If mkbp_event_supported, then we can be assured that
352 * the driver's state for the online property is consistent
353 * with the hardware. However, if we aren't event driven,
354 * the optimization before to skip an ec_port_status get
355 * and only returned cached values of the online property will
356 * cause a delay in detecting a cable attach until one of the
357 * other properties are read.
359 * Allow an ec_port_status refresh for online property check
360 * if we're not already online to check for plug events if
361 * not mkbp_event_supported.
363 if (ec_device->mkbp_event_supported || port->psy_online)
366 case POWER_SUPPLY_PROP_CURRENT_MAX:
367 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
368 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
369 ret = cros_usbpd_charger_get_port_status(port, true);
371 dev_err(dev, "Failed to get port status (err:0x%x)\n",
381 case POWER_SUPPLY_PROP_ONLINE:
382 val->intval = port->psy_online;
384 case POWER_SUPPLY_PROP_STATUS:
385 val->intval = port->psy_status;
387 case POWER_SUPPLY_PROP_CURRENT_MAX:
388 val->intval = port->psy_current_max * 1000;
390 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
391 val->intval = port->psy_voltage_max_design * 1000;
393 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
394 val->intval = port->psy_voltage_now * 1000;
396 case POWER_SUPPLY_PROP_USB_TYPE:
397 val->intval = port->psy_usb_type;
399 case POWER_SUPPLY_PROP_MODEL_NAME:
400 val->strval = port->model_name;
402 case POWER_SUPPLY_PROP_MANUFACTURER:
403 val->strval = port->manufacturer;
412 static int cros_usbpd_charger_ec_event(struct notifier_block *nb,
413 unsigned long queued_during_suspend,
416 struct cros_ec_device *ec_device;
417 struct charger_data *charger;
420 charger = container_of(nb, struct charger_data, notifier);
421 ec_device = charger->ec_device;
423 host_event = cros_ec_get_host_event(ec_device);
424 if (host_event & EC_HOST_EVENT_MASK(EC_HOST_EVENT_PD_MCU)) {
425 cros_usbpd_charger_power_changed(charger->ports[0]->psy);
432 static void cros_usbpd_charger_unregister_notifier(void *data)
434 struct charger_data *charger = data;
435 struct cros_ec_device *ec_device = charger->ec_device;
437 blocking_notifier_chain_unregister(&ec_device->event_notifier,
441 static int cros_usbpd_charger_probe(struct platform_device *pd)
443 struct cros_ec_dev *ec_dev = dev_get_drvdata(pd->dev.parent);
444 struct cros_ec_device *ec_device = ec_dev->ec_dev;
445 struct power_supply_desc *psy_desc;
446 struct device *dev = &pd->dev;
447 struct charger_data *charger;
448 struct power_supply *psy;
449 struct port_data *port;
453 charger = devm_kzalloc(dev, sizeof(struct charger_data),
459 charger->ec_dev = ec_dev;
460 charger->ec_device = ec_device;
462 platform_set_drvdata(pd, charger);
465 * We need to know the number of USB PD ports in order to know whether
466 * there is a dedicated port. The dedicated port will always be
467 * after the USB PD ports, and there should be only one.
469 charger->num_usbpd_ports =
470 cros_usbpd_charger_get_usbpd_num_ports(charger);
471 if (charger->num_usbpd_ports <= 0) {
473 * This can happen on a system that doesn't support USB PD.
474 * Log a message, but no need to warn.
476 dev_info(dev, "No USB PD charging ports found\n");
479 charger->num_charger_ports = cros_usbpd_charger_get_num_ports(charger);
480 if (charger->num_charger_ports < 0) {
482 * This can happen on a system that doesn't support USB PD.
483 * Log a message, but no need to warn.
484 * Older ECs do not support the above command, in that case
485 * let's set up the number of charger ports equal to the number
488 dev_info(dev, "Could not get charger port count\n");
489 charger->num_charger_ports = charger->num_usbpd_ports;
492 if (charger->num_charger_ports <= 0) {
494 * This can happen on a system that doesn't support USB PD and
495 * doesn't have a dedicated port.
496 * Log a message, but no need to warn.
498 dev_info(dev, "No charging ports found\n");
504 * Sanity checks on the number of ports:
505 * there should be at most 1 dedicated port
507 if (charger->num_charger_ports < charger->num_usbpd_ports ||
508 charger->num_charger_ports > (charger->num_usbpd_ports + 1)) {
509 dev_err(dev, "Unexpected number of charge port count\n");
514 for (i = 0; i < charger->num_charger_ports; i++) {
515 struct power_supply_config psy_cfg = {};
517 port = devm_kzalloc(dev, sizeof(struct port_data), GFP_KERNEL);
523 port->charger = charger;
524 port->port_number = i;
526 psy_desc = &port->psy_desc;
527 psy_desc->get_property = cros_usbpd_charger_get_prop;
528 psy_desc->external_power_changed =
529 cros_usbpd_charger_power_changed;
530 psy_cfg.drv_data = port;
532 if (cros_usbpd_charger_port_is_dedicated(port)) {
533 sprintf(port->name, CHARGER_DEDICATED_DIR_NAME);
534 psy_desc->type = POWER_SUPPLY_TYPE_MAINS;
535 psy_desc->properties =
536 cros_usbpd_dedicated_charger_props;
537 psy_desc->num_properties =
538 ARRAY_SIZE(cros_usbpd_dedicated_charger_props);
540 sprintf(port->name, CHARGER_USBPD_DIR_NAME, i);
541 psy_desc->type = POWER_SUPPLY_TYPE_USB;
542 psy_desc->properties = cros_usbpd_charger_props;
543 psy_desc->num_properties =
544 ARRAY_SIZE(cros_usbpd_charger_props);
545 psy_desc->usb_types = cros_usbpd_charger_usb_types;
546 psy_desc->num_usb_types =
547 ARRAY_SIZE(cros_usbpd_charger_usb_types);
550 psy_desc->name = port->name;
552 psy = devm_power_supply_register_no_ws(dev, psy_desc,
555 dev_err(dev, "Failed to register power supply\n");
560 charger->ports[charger->num_registered_psy++] = port;
563 if (!charger->num_registered_psy) {
565 dev_err(dev, "No power supplies registered\n");
569 if (ec_device->mkbp_event_supported) {
570 /* Get PD events from the EC */
571 charger->notifier.notifier_call = cros_usbpd_charger_ec_event;
572 ret = blocking_notifier_chain_register(
573 &ec_device->event_notifier,
576 dev_warn(dev, "failed to register notifier\n");
578 ret = devm_add_action_or_reset(dev,
579 cros_usbpd_charger_unregister_notifier,
589 WARN(1, "%s: Failing probe (err:0x%x)\n", dev_name(dev), ret);
592 dev_info(dev, "Failing probe (err:0x%x)\n", ret);
596 #ifdef CONFIG_PM_SLEEP
597 static int cros_usbpd_charger_resume(struct device *dev)
599 struct charger_data *charger = dev_get_drvdata(dev);
605 for (i = 0; i < charger->num_registered_psy; i++) {
606 power_supply_changed(charger->ports[i]->psy);
607 charger->ports[i]->last_update =
608 jiffies - CHARGER_CACHE_UPDATE_DELAY;
615 static SIMPLE_DEV_PM_OPS(cros_usbpd_charger_pm_ops, NULL,
616 cros_usbpd_charger_resume);
618 static struct platform_driver cros_usbpd_charger_driver = {
621 .pm = &cros_usbpd_charger_pm_ops,
623 .probe = cros_usbpd_charger_probe
626 module_platform_driver(cros_usbpd_charger_driver);
628 MODULE_LICENSE("GPL");
629 MODULE_DESCRIPTION("ChromeOS EC USBPD charger");
630 MODULE_ALIAS("platform:" DRV_NAME);