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/platform_data/cros_ec_commands.h>
10 #include <linux/platform_data/cros_ec_proto.h>
11 #include <linux/platform_data/cros_usbpd_notify.h>
12 #include <linux/platform_device.h>
13 #include <linux/power_supply.h>
14 #include <linux/slab.h>
16 #define CHARGER_USBPD_DIR_NAME "CROS_USBPD_CHARGER%d"
17 #define CHARGER_DEDICATED_DIR_NAME "CROS_DEDICATED_CHARGER"
18 #define CHARGER_DIR_NAME_LENGTH (sizeof(CHARGER_USBPD_DIR_NAME) >= \
19 sizeof(CHARGER_DEDICATED_DIR_NAME) ? \
20 sizeof(CHARGER_USBPD_DIR_NAME) : \
21 sizeof(CHARGER_DEDICATED_DIR_NAME))
22 #define CHARGER_CACHE_UPDATE_DELAY msecs_to_jiffies(500)
23 #define CHARGER_MANUFACTURER_MODEL_LENGTH 32
25 #define DRV_NAME "cros-usbpd-charger"
29 char name[CHARGER_DIR_NAME_LENGTH];
30 char manufacturer[CHARGER_MANUFACTURER_MODEL_LENGTH];
31 char model_name[CHARGER_MANUFACTURER_MODEL_LENGTH];
32 struct power_supply *psy;
33 struct power_supply_desc psy_desc;
38 int psy_voltage_max_design;
41 struct charger_data *charger;
42 unsigned long last_update;
47 struct cros_ec_dev *ec_dev;
48 struct cros_ec_device *ec_device;
49 int num_charger_ports;
51 int num_registered_psy;
52 struct port_data *ports[EC_USB_PD_MAX_PORTS];
53 struct notifier_block notifier;
56 static enum power_supply_property cros_usbpd_charger_props[] = {
57 POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT,
58 POWER_SUPPLY_PROP_INPUT_VOLTAGE_LIMIT,
59 POWER_SUPPLY_PROP_ONLINE,
60 POWER_SUPPLY_PROP_STATUS,
61 POWER_SUPPLY_PROP_CURRENT_MAX,
62 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
63 POWER_SUPPLY_PROP_VOLTAGE_NOW,
64 POWER_SUPPLY_PROP_MODEL_NAME,
65 POWER_SUPPLY_PROP_MANUFACTURER,
66 POWER_SUPPLY_PROP_USB_TYPE
69 static enum power_supply_property cros_usbpd_dedicated_charger_props[] = {
70 POWER_SUPPLY_PROP_ONLINE,
71 POWER_SUPPLY_PROP_STATUS,
72 POWER_SUPPLY_PROP_VOLTAGE_NOW,
75 static enum power_supply_usb_type cros_usbpd_charger_usb_types[] = {
76 POWER_SUPPLY_USB_TYPE_UNKNOWN,
77 POWER_SUPPLY_USB_TYPE_SDP,
78 POWER_SUPPLY_USB_TYPE_DCP,
79 POWER_SUPPLY_USB_TYPE_CDP,
80 POWER_SUPPLY_USB_TYPE_C,
81 POWER_SUPPLY_USB_TYPE_PD,
82 POWER_SUPPLY_USB_TYPE_PD_DRP,
83 POWER_SUPPLY_USB_TYPE_APPLE_BRICK_ID
86 /* Input voltage/current limit in mV/mA. Default to none. */
87 static u16 input_voltage_limit = EC_POWER_LIMIT_NONE;
88 static u16 input_current_limit = EC_POWER_LIMIT_NONE;
90 static bool cros_usbpd_charger_port_is_dedicated(struct port_data *port)
92 return port->port_number >= port->charger->num_usbpd_ports;
95 static int cros_usbpd_charger_ec_command(struct charger_data *charger,
103 struct cros_ec_dev *ec_dev = charger->ec_dev;
104 struct cros_ec_command *msg;
107 msg = kzalloc(struct_size(msg, data, max(outsize, insize)), GFP_KERNEL);
111 msg->version = version;
112 msg->command = ec_dev->cmd_offset + command;
113 msg->outsize = outsize;
114 msg->insize = insize;
117 memcpy(msg->data, outdata, outsize);
119 ret = cros_ec_cmd_xfer_status(charger->ec_device, msg);
120 if (ret >= 0 && insize)
121 memcpy(indata, msg->data, insize);
127 static int cros_usbpd_charger_get_num_ports(struct charger_data *charger)
129 struct ec_response_charge_port_count resp;
132 ret = cros_usbpd_charger_ec_command(charger, 0,
133 EC_CMD_CHARGE_PORT_COUNT,
134 NULL, 0, &resp, sizeof(resp));
138 return resp.port_count;
141 static int cros_usbpd_charger_get_usbpd_num_ports(struct charger_data *charger)
143 struct ec_response_usb_pd_ports resp;
146 ret = cros_usbpd_charger_ec_command(charger, 0, EC_CMD_USB_PD_PORTS,
147 NULL, 0, &resp, sizeof(resp));
151 return resp.num_ports;
154 static int cros_usbpd_charger_get_discovery_info(struct port_data *port)
156 struct charger_data *charger = port->charger;
157 struct ec_params_usb_pd_discovery_entry resp;
158 struct ec_params_usb_pd_info_request req;
161 req.port = port->port_number;
163 ret = cros_usbpd_charger_ec_command(charger, 0,
164 EC_CMD_USB_PD_DISCOVERY,
166 &resp, sizeof(resp));
168 dev_err(charger->dev,
169 "Unable to query discovery info (err:0x%x)\n", ret);
173 dev_dbg(charger->dev, "Port %d: VID = 0x%x, PID=0x%x, PTYPE=0x%x\n",
174 port->port_number, resp.vid, resp.pid, resp.ptype);
176 snprintf(port->manufacturer, sizeof(port->manufacturer), "%x",
178 snprintf(port->model_name, sizeof(port->model_name), "%x", resp.pid);
183 static int cros_usbpd_charger_get_power_info(struct port_data *port)
185 struct charger_data *charger = port->charger;
186 struct ec_response_usb_pd_power_info resp;
187 struct ec_params_usb_pd_power_info req;
188 int last_psy_status, last_psy_usb_type;
189 struct device *dev = charger->dev;
192 req.port = port->port_number;
193 ret = cros_usbpd_charger_ec_command(charger, 0,
194 EC_CMD_USB_PD_POWER_INFO,
196 &resp, sizeof(resp));
198 dev_err(dev, "Unable to query PD power info (err:0x%x)\n", ret);
202 last_psy_status = port->psy_status;
203 last_psy_usb_type = port->psy_usb_type;
206 case USB_PD_PORT_POWER_DISCONNECTED:
207 port->psy_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
208 port->psy_online = 0;
210 case USB_PD_PORT_POWER_SOURCE:
211 port->psy_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
212 port->psy_online = 0;
214 case USB_PD_PORT_POWER_SINK:
215 port->psy_status = POWER_SUPPLY_STATUS_CHARGING;
216 port->psy_online = 1;
218 case USB_PD_PORT_POWER_SINK_NOT_CHARGING:
219 port->psy_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
220 port->psy_online = 1;
223 dev_err(dev, "Unknown role %d\n", resp.role);
227 port->psy_voltage_max_design = resp.meas.voltage_max;
228 port->psy_voltage_now = resp.meas.voltage_now;
229 port->psy_current_max = resp.meas.current_max;
230 port->psy_power_max = resp.max_power;
233 case USB_CHG_TYPE_BC12_SDP:
234 case USB_CHG_TYPE_VBUS:
235 port->psy_usb_type = POWER_SUPPLY_USB_TYPE_SDP;
237 case USB_CHG_TYPE_NONE:
239 * For dual-role devices when we are a source, the firmware
240 * reports the type as NONE. Report such chargers as type
243 if (resp.role == USB_PD_PORT_POWER_SOURCE && resp.dualrole)
244 port->psy_usb_type = POWER_SUPPLY_USB_TYPE_PD_DRP;
246 port->psy_usb_type = POWER_SUPPLY_USB_TYPE_SDP;
248 case USB_CHG_TYPE_OTHER:
249 case USB_CHG_TYPE_PROPRIETARY:
250 port->psy_usb_type = POWER_SUPPLY_USB_TYPE_APPLE_BRICK_ID;
253 port->psy_usb_type = POWER_SUPPLY_USB_TYPE_C;
255 case USB_CHG_TYPE_BC12_DCP:
256 port->psy_usb_type = POWER_SUPPLY_USB_TYPE_DCP;
258 case USB_CHG_TYPE_BC12_CDP:
259 port->psy_usb_type = POWER_SUPPLY_USB_TYPE_CDP;
261 case USB_CHG_TYPE_PD:
263 port->psy_usb_type = POWER_SUPPLY_USB_TYPE_PD_DRP;
265 port->psy_usb_type = POWER_SUPPLY_USB_TYPE_PD;
267 case USB_CHG_TYPE_UNKNOWN:
269 * While the EC is trying to determine the type of charger that
270 * has been plugged in, it will report the charger type as
271 * unknown. Additionally since the power capabilities are
272 * unknown, report the max current and voltage as zero.
274 port->psy_usb_type = POWER_SUPPLY_USB_TYPE_UNKNOWN;
275 port->psy_voltage_max_design = 0;
276 port->psy_current_max = 0;
279 dev_err(dev, "Port %d: default case!\n", port->port_number);
280 port->psy_usb_type = POWER_SUPPLY_USB_TYPE_SDP;
283 if (cros_usbpd_charger_port_is_dedicated(port))
284 port->psy_desc.type = POWER_SUPPLY_TYPE_MAINS;
286 port->psy_desc.type = POWER_SUPPLY_TYPE_USB;
289 "Port %d: type=%d vmax=%d vnow=%d cmax=%d clim=%d pmax=%d\n",
290 port->port_number, resp.type, resp.meas.voltage_max,
291 resp.meas.voltage_now, resp.meas.current_max,
292 resp.meas.current_lim, resp.max_power);
295 * If power supply type or status changed, explicitly call
296 * power_supply_changed. This results in udev event getting generated
297 * and allows user mode apps to react quicker instead of waiting for
298 * their next poll of power supply status.
300 if (last_psy_usb_type != port->psy_usb_type ||
301 last_psy_status != port->psy_status)
302 power_supply_changed(port->psy);
307 static int cros_usbpd_charger_get_port_status(struct port_data *port,
313 time_is_after_jiffies(port->last_update +
314 CHARGER_CACHE_UPDATE_DELAY))
317 ret = cros_usbpd_charger_get_power_info(port);
321 if (!cros_usbpd_charger_port_is_dedicated(port))
322 ret = cros_usbpd_charger_get_discovery_info(port);
323 port->last_update = jiffies;
328 static int cros_usbpd_charger_set_ext_power_limit(struct charger_data *charger,
332 struct ec_params_external_power_limit_v1 req;
335 req.current_lim = current_lim;
336 req.voltage_lim = voltage_lim;
338 ret = cros_usbpd_charger_ec_command(charger, 0,
339 EC_CMD_EXTERNAL_POWER_LIMIT,
340 &req, sizeof(req), NULL, 0);
342 dev_err(charger->dev,
343 "Unable to set the 'External Power Limit': %d\n", ret);
348 static void cros_usbpd_charger_power_changed(struct power_supply *psy)
350 struct port_data *port = power_supply_get_drvdata(psy);
351 struct charger_data *charger = port->charger;
354 for (i = 0; i < charger->num_registered_psy; i++)
355 cros_usbpd_charger_get_port_status(charger->ports[i], false);
358 static int cros_usbpd_charger_get_prop(struct power_supply *psy,
359 enum power_supply_property psp,
360 union power_supply_propval *val)
362 struct port_data *port = power_supply_get_drvdata(psy);
363 struct charger_data *charger = port->charger;
364 struct cros_ec_device *ec_device = charger->ec_device;
365 struct device *dev = charger->dev;
368 /* Only refresh ec_port_status for dynamic properties */
370 case POWER_SUPPLY_PROP_ONLINE:
372 * If mkbp_event_supported, then we can be assured that
373 * the driver's state for the online property is consistent
374 * with the hardware. However, if we aren't event driven,
375 * the optimization before to skip an ec_port_status get
376 * and only returned cached values of the online property will
377 * cause a delay in detecting a cable attach until one of the
378 * other properties are read.
380 * Allow an ec_port_status refresh for online property check
381 * if we're not already online to check for plug events if
382 * not mkbp_event_supported.
384 if (ec_device->mkbp_event_supported || port->psy_online)
387 case POWER_SUPPLY_PROP_CURRENT_MAX:
388 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
389 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
390 ret = cros_usbpd_charger_get_port_status(port, true);
392 dev_err(dev, "Failed to get port status (err:0x%x)\n",
402 case POWER_SUPPLY_PROP_ONLINE:
403 val->intval = port->psy_online;
405 case POWER_SUPPLY_PROP_STATUS:
406 val->intval = port->psy_status;
408 case POWER_SUPPLY_PROP_CURRENT_MAX:
409 val->intval = port->psy_current_max * 1000;
411 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
412 val->intval = port->psy_voltage_max_design * 1000;
414 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
415 val->intval = port->psy_voltage_now * 1000;
417 case POWER_SUPPLY_PROP_USB_TYPE:
418 val->intval = port->psy_usb_type;
420 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
421 if (input_current_limit == EC_POWER_LIMIT_NONE)
424 val->intval = input_current_limit * 1000;
426 case POWER_SUPPLY_PROP_INPUT_VOLTAGE_LIMIT:
427 if (input_voltage_limit == EC_POWER_LIMIT_NONE)
430 val->intval = input_voltage_limit * 1000;
432 case POWER_SUPPLY_PROP_MODEL_NAME:
433 val->strval = port->model_name;
435 case POWER_SUPPLY_PROP_MANUFACTURER:
436 val->strval = port->manufacturer;
445 static int cros_usbpd_charger_set_prop(struct power_supply *psy,
446 enum power_supply_property psp,
447 const union power_supply_propval *val)
449 struct port_data *port = power_supply_get_drvdata(psy);
450 struct charger_data *charger = port->charger;
451 struct device *dev = charger->dev;
455 /* U16_MAX in mV/mA is the maximum supported value */
456 if (val->intval >= U16_MAX * 1000)
458 /* A negative number is used to clear the limit */
460 intval = EC_POWER_LIMIT_NONE;
461 else /* Convert from uA/uV to mA/mV */
462 intval = val->intval / 1000;
465 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
466 ret = cros_usbpd_charger_set_ext_power_limit(charger, intval,
467 input_voltage_limit);
471 input_current_limit = intval;
472 if (input_current_limit == EC_POWER_LIMIT_NONE)
474 "External Current Limit cleared for all ports\n");
477 "External Current Limit set to %dmA for all ports\n",
478 input_current_limit);
480 case POWER_SUPPLY_PROP_INPUT_VOLTAGE_LIMIT:
481 ret = cros_usbpd_charger_set_ext_power_limit(charger,
487 input_voltage_limit = intval;
488 if (input_voltage_limit == EC_POWER_LIMIT_NONE)
490 "External Voltage Limit cleared for all ports\n");
493 "External Voltage Limit set to %dmV for all ports\n",
494 input_voltage_limit);
503 static int cros_usbpd_charger_property_is_writeable(struct power_supply *psy,
504 enum power_supply_property psp)
509 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
510 case POWER_SUPPLY_PROP_INPUT_VOLTAGE_LIMIT:
520 static int cros_usbpd_charger_ec_event(struct notifier_block *nb,
521 unsigned long host_event,
524 struct charger_data *charger = container_of(nb, struct charger_data,
527 cros_usbpd_charger_power_changed(charger->ports[0]->psy);
531 static void cros_usbpd_charger_unregister_notifier(void *data)
533 struct charger_data *charger = data;
535 cros_usbpd_unregister_notify(&charger->notifier);
538 static int cros_usbpd_charger_probe(struct platform_device *pd)
540 struct cros_ec_dev *ec_dev = dev_get_drvdata(pd->dev.parent);
541 struct cros_ec_device *ec_device = ec_dev->ec_dev;
542 struct power_supply_desc *psy_desc;
543 struct device *dev = &pd->dev;
544 struct charger_data *charger;
545 struct power_supply *psy;
546 struct port_data *port;
550 charger = devm_kzalloc(dev, sizeof(struct charger_data),
556 charger->ec_dev = ec_dev;
557 charger->ec_device = ec_device;
559 platform_set_drvdata(pd, charger);
562 * We need to know the number of USB PD ports in order to know whether
563 * there is a dedicated port. The dedicated port will always be
564 * after the USB PD ports, and there should be only one.
566 charger->num_usbpd_ports =
567 cros_usbpd_charger_get_usbpd_num_ports(charger);
568 if (charger->num_usbpd_ports <= 0) {
570 * This can happen on a system that doesn't support USB PD.
571 * Log a message, but no need to warn.
573 dev_info(dev, "No USB PD charging ports found\n");
576 charger->num_charger_ports = cros_usbpd_charger_get_num_ports(charger);
577 if (charger->num_charger_ports < 0) {
579 * This can happen on a system that doesn't support USB PD.
580 * Log a message, but no need to warn.
581 * Older ECs do not support the above command, in that case
582 * let's set up the number of charger ports equal to the number
585 dev_info(dev, "Could not get charger port count\n");
586 charger->num_charger_ports = charger->num_usbpd_ports;
589 if (charger->num_charger_ports <= 0) {
591 * This can happen on a system that doesn't support USB PD and
592 * doesn't have a dedicated port.
593 * Log a message, but no need to warn.
595 dev_info(dev, "No charging ports found\n");
601 * Sanity checks on the number of ports:
602 * there should be at most 1 dedicated port
604 if (charger->num_charger_ports < charger->num_usbpd_ports ||
605 charger->num_charger_ports > (charger->num_usbpd_ports + 1)) {
606 dev_err(dev, "Unexpected number of charge port count\n");
611 for (i = 0; i < charger->num_charger_ports; i++) {
612 struct power_supply_config psy_cfg = {};
614 port = devm_kzalloc(dev, sizeof(struct port_data), GFP_KERNEL);
620 port->charger = charger;
621 port->port_number = i;
623 psy_desc = &port->psy_desc;
624 psy_desc->get_property = cros_usbpd_charger_get_prop;
625 psy_desc->set_property = cros_usbpd_charger_set_prop;
626 psy_desc->property_is_writeable =
627 cros_usbpd_charger_property_is_writeable;
628 psy_desc->external_power_changed =
629 cros_usbpd_charger_power_changed;
630 psy_cfg.drv_data = port;
632 if (cros_usbpd_charger_port_is_dedicated(port)) {
633 sprintf(port->name, CHARGER_DEDICATED_DIR_NAME);
634 psy_desc->type = POWER_SUPPLY_TYPE_MAINS;
635 psy_desc->properties =
636 cros_usbpd_dedicated_charger_props;
637 psy_desc->num_properties =
638 ARRAY_SIZE(cros_usbpd_dedicated_charger_props);
640 sprintf(port->name, CHARGER_USBPD_DIR_NAME, i);
641 psy_desc->type = POWER_SUPPLY_TYPE_USB;
642 psy_desc->properties = cros_usbpd_charger_props;
643 psy_desc->num_properties =
644 ARRAY_SIZE(cros_usbpd_charger_props);
645 psy_desc->usb_types = cros_usbpd_charger_usb_types;
646 psy_desc->num_usb_types =
647 ARRAY_SIZE(cros_usbpd_charger_usb_types);
650 psy_desc->name = port->name;
652 psy = devm_power_supply_register_no_ws(dev, psy_desc,
655 dev_err(dev, "Failed to register power supply\n");
660 charger->ports[charger->num_registered_psy++] = port;
663 if (!charger->num_registered_psy) {
665 dev_err(dev, "No power supplies registered\n");
669 /* Get PD events from the EC */
670 charger->notifier.notifier_call = cros_usbpd_charger_ec_event;
671 ret = cros_usbpd_register_notify(&charger->notifier);
673 dev_warn(dev, "failed to register notifier\n");
675 ret = devm_add_action_or_reset(dev,
676 cros_usbpd_charger_unregister_notifier,
685 WARN(1, "%s: Failing probe (err:0x%x)\n", dev_name(dev), ret);
688 dev_info(dev, "Failing probe (err:0x%x)\n", ret);
692 #ifdef CONFIG_PM_SLEEP
693 static int cros_usbpd_charger_resume(struct device *dev)
695 struct charger_data *charger = dev_get_drvdata(dev);
701 for (i = 0; i < charger->num_registered_psy; i++) {
702 power_supply_changed(charger->ports[i]->psy);
703 charger->ports[i]->last_update =
704 jiffies - CHARGER_CACHE_UPDATE_DELAY;
711 static SIMPLE_DEV_PM_OPS(cros_usbpd_charger_pm_ops, NULL,
712 cros_usbpd_charger_resume);
714 static struct platform_driver cros_usbpd_charger_driver = {
717 .pm = &cros_usbpd_charger_pm_ops,
719 .probe = cros_usbpd_charger_probe
722 module_platform_driver(cros_usbpd_charger_driver);
724 MODULE_LICENSE("GPL");
725 MODULE_DESCRIPTION("ChromeOS EC USBPD charger");
726 MODULE_ALIAS("platform:" DRV_NAME);