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/mfd/cros_ec.h>
9 #include <linux/module.h>
10 #include <linux/platform_data/cros_ec_commands.h>
11 #include <linux/platform_data/cros_ec_proto.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(sizeof(*msg
) + 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 queued_during_suspend
,
524 struct cros_ec_device
*ec_device
;
525 struct charger_data
*charger
;
528 charger
= container_of(nb
, struct charger_data
, notifier
);
529 ec_device
= charger
->ec_device
;
531 host_event
= cros_ec_get_host_event(ec_device
);
532 if (host_event
& EC_HOST_EVENT_MASK(EC_HOST_EVENT_PD_MCU
)) {
533 cros_usbpd_charger_power_changed(charger
->ports
[0]->psy
);
540 static void cros_usbpd_charger_unregister_notifier(void *data
)
542 struct charger_data
*charger
= data
;
543 struct cros_ec_device
*ec_device
= charger
->ec_device
;
545 blocking_notifier_chain_unregister(&ec_device
->event_notifier
,
549 static int cros_usbpd_charger_probe(struct platform_device
*pd
)
551 struct cros_ec_dev
*ec_dev
= dev_get_drvdata(pd
->dev
.parent
);
552 struct cros_ec_device
*ec_device
= ec_dev
->ec_dev
;
553 struct power_supply_desc
*psy_desc
;
554 struct device
*dev
= &pd
->dev
;
555 struct charger_data
*charger
;
556 struct power_supply
*psy
;
557 struct port_data
*port
;
561 charger
= devm_kzalloc(dev
, sizeof(struct charger_data
),
567 charger
->ec_dev
= ec_dev
;
568 charger
->ec_device
= ec_device
;
570 platform_set_drvdata(pd
, charger
);
573 * We need to know the number of USB PD ports in order to know whether
574 * there is a dedicated port. The dedicated port will always be
575 * after the USB PD ports, and there should be only one.
577 charger
->num_usbpd_ports
=
578 cros_usbpd_charger_get_usbpd_num_ports(charger
);
579 if (charger
->num_usbpd_ports
<= 0) {
581 * This can happen on a system that doesn't support USB PD.
582 * Log a message, but no need to warn.
584 dev_info(dev
, "No USB PD charging ports found\n");
587 charger
->num_charger_ports
= cros_usbpd_charger_get_num_ports(charger
);
588 if (charger
->num_charger_ports
< 0) {
590 * This can happen on a system that doesn't support USB PD.
591 * Log a message, but no need to warn.
592 * Older ECs do not support the above command, in that case
593 * let's set up the number of charger ports equal to the number
596 dev_info(dev
, "Could not get charger port count\n");
597 charger
->num_charger_ports
= charger
->num_usbpd_ports
;
600 if (charger
->num_charger_ports
<= 0) {
602 * This can happen on a system that doesn't support USB PD and
603 * doesn't have a dedicated port.
604 * Log a message, but no need to warn.
606 dev_info(dev
, "No charging ports found\n");
612 * Sanity checks on the number of ports:
613 * there should be at most 1 dedicated port
615 if (charger
->num_charger_ports
< charger
->num_usbpd_ports
||
616 charger
->num_charger_ports
> (charger
->num_usbpd_ports
+ 1)) {
617 dev_err(dev
, "Unexpected number of charge port count\n");
622 for (i
= 0; i
< charger
->num_charger_ports
; i
++) {
623 struct power_supply_config psy_cfg
= {};
625 port
= devm_kzalloc(dev
, sizeof(struct port_data
), GFP_KERNEL
);
631 port
->charger
= charger
;
632 port
->port_number
= i
;
634 psy_desc
= &port
->psy_desc
;
635 psy_desc
->get_property
= cros_usbpd_charger_get_prop
;
636 psy_desc
->set_property
= cros_usbpd_charger_set_prop
;
637 psy_desc
->property_is_writeable
=
638 cros_usbpd_charger_property_is_writeable
;
639 psy_desc
->external_power_changed
=
640 cros_usbpd_charger_power_changed
;
641 psy_cfg
.drv_data
= port
;
643 if (cros_usbpd_charger_port_is_dedicated(port
)) {
644 sprintf(port
->name
, CHARGER_DEDICATED_DIR_NAME
);
645 psy_desc
->type
= POWER_SUPPLY_TYPE_MAINS
;
646 psy_desc
->properties
=
647 cros_usbpd_dedicated_charger_props
;
648 psy_desc
->num_properties
=
649 ARRAY_SIZE(cros_usbpd_dedicated_charger_props
);
651 sprintf(port
->name
, CHARGER_USBPD_DIR_NAME
, i
);
652 psy_desc
->type
= POWER_SUPPLY_TYPE_USB
;
653 psy_desc
->properties
= cros_usbpd_charger_props
;
654 psy_desc
->num_properties
=
655 ARRAY_SIZE(cros_usbpd_charger_props
);
656 psy_desc
->usb_types
= cros_usbpd_charger_usb_types
;
657 psy_desc
->num_usb_types
=
658 ARRAY_SIZE(cros_usbpd_charger_usb_types
);
661 psy_desc
->name
= port
->name
;
663 psy
= devm_power_supply_register_no_ws(dev
, psy_desc
,
666 dev_err(dev
, "Failed to register power supply\n");
671 charger
->ports
[charger
->num_registered_psy
++] = port
;
674 if (!charger
->num_registered_psy
) {
676 dev_err(dev
, "No power supplies registered\n");
680 if (ec_device
->mkbp_event_supported
) {
681 /* Get PD events from the EC */
682 charger
->notifier
.notifier_call
= cros_usbpd_charger_ec_event
;
683 ret
= blocking_notifier_chain_register(
684 &ec_device
->event_notifier
,
687 dev_warn(dev
, "failed to register notifier\n");
689 ret
= devm_add_action_or_reset(dev
,
690 cros_usbpd_charger_unregister_notifier
,
700 WARN(1, "%s: Failing probe (err:0x%x)\n", dev_name(dev
), ret
);
703 dev_info(dev
, "Failing probe (err:0x%x)\n", ret
);
707 #ifdef CONFIG_PM_SLEEP
708 static int cros_usbpd_charger_resume(struct device
*dev
)
710 struct charger_data
*charger
= dev_get_drvdata(dev
);
716 for (i
= 0; i
< charger
->num_registered_psy
; i
++) {
717 power_supply_changed(charger
->ports
[i
]->psy
);
718 charger
->ports
[i
]->last_update
=
719 jiffies
- CHARGER_CACHE_UPDATE_DELAY
;
726 static SIMPLE_DEV_PM_OPS(cros_usbpd_charger_pm_ops
, NULL
,
727 cros_usbpd_charger_resume
);
729 static struct platform_driver cros_usbpd_charger_driver
= {
732 .pm
= &cros_usbpd_charger_pm_ops
,
734 .probe
= cros_usbpd_charger_probe
737 module_platform_driver(cros_usbpd_charger_driver
);
739 MODULE_LICENSE("GPL");
740 MODULE_DESCRIPTION("ChromeOS EC USBPD charger");
741 MODULE_ALIAS("platform:" DRV_NAME
);