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/mod_devicetable.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_data/cros_usbpd_notify.h>
13 #include <linux/platform_device.h>
14 #include <linux/power_supply.h>
15 #include <linux/slab.h>
17 #define CHARGER_USBPD_DIR_NAME "CROS_USBPD_CHARGER%d"
18 #define CHARGER_DEDICATED_DIR_NAME "CROS_DEDICATED_CHARGER"
19 #define CHARGER_DIR_NAME_LENGTH (sizeof(CHARGER_USBPD_DIR_NAME) >= \
20 sizeof(CHARGER_DEDICATED_DIR_NAME) ? \
21 sizeof(CHARGER_USBPD_DIR_NAME) : \
22 sizeof(CHARGER_DEDICATED_DIR_NAME))
23 #define CHARGER_CACHE_UPDATE_DELAY msecs_to_jiffies(500)
24 #define CHARGER_MANUFACTURER_MODEL_LENGTH 32
26 #define DRV_NAME "cros-usbpd-charger"
30 char name
[CHARGER_DIR_NAME_LENGTH
];
31 char manufacturer
[CHARGER_MANUFACTURER_MODEL_LENGTH
];
32 char model_name
[CHARGER_MANUFACTURER_MODEL_LENGTH
];
33 struct power_supply
*psy
;
34 struct power_supply_desc psy_desc
;
39 int psy_voltage_max_design
;
42 struct charger_data
*charger
;
43 unsigned long last_update
;
48 struct cros_ec_dev
*ec_dev
;
49 struct cros_ec_device
*ec_device
;
50 int num_charger_ports
;
52 int num_registered_psy
;
53 struct port_data
*ports
[EC_USB_PD_MAX_PORTS
];
54 struct notifier_block notifier
;
57 static enum power_supply_property cros_usbpd_charger_props
[] = {
58 POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT
,
59 POWER_SUPPLY_PROP_INPUT_VOLTAGE_LIMIT
,
60 POWER_SUPPLY_PROP_ONLINE
,
61 POWER_SUPPLY_PROP_STATUS
,
62 POWER_SUPPLY_PROP_CURRENT_MAX
,
63 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN
,
64 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
65 POWER_SUPPLY_PROP_MODEL_NAME
,
66 POWER_SUPPLY_PROP_MANUFACTURER
,
67 POWER_SUPPLY_PROP_USB_TYPE
70 static enum power_supply_property cros_usbpd_dedicated_charger_props
[] = {
71 POWER_SUPPLY_PROP_ONLINE
,
72 POWER_SUPPLY_PROP_STATUS
,
73 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
76 /* Input voltage/current limit in mV/mA. Default to none. */
77 static u16 input_voltage_limit
= EC_POWER_LIMIT_NONE
;
78 static u16 input_current_limit
= EC_POWER_LIMIT_NONE
;
80 static bool cros_usbpd_charger_port_is_dedicated(struct port_data
*port
)
82 return port
->port_number
>= port
->charger
->num_usbpd_ports
;
85 static int cros_usbpd_charger_ec_command(struct charger_data
*charger
,
93 struct cros_ec_dev
*ec_dev
= charger
->ec_dev
;
94 struct cros_ec_command
*msg
;
97 msg
= kzalloc(struct_size(msg
, data
, max(outsize
, insize
)), GFP_KERNEL
);
101 msg
->version
= version
;
102 msg
->command
= ec_dev
->cmd_offset
+ command
;
103 msg
->outsize
= outsize
;
104 msg
->insize
= insize
;
107 memcpy(msg
->data
, outdata
, outsize
);
109 ret
= cros_ec_cmd_xfer_status(charger
->ec_device
, msg
);
110 if (ret
>= 0 && insize
)
111 memcpy(indata
, msg
->data
, insize
);
117 static int cros_usbpd_charger_get_num_ports(struct charger_data
*charger
)
119 struct ec_response_charge_port_count resp
;
122 ret
= cros_usbpd_charger_ec_command(charger
, 0,
123 EC_CMD_CHARGE_PORT_COUNT
,
124 NULL
, 0, &resp
, sizeof(resp
));
128 return resp
.port_count
;
131 static int cros_usbpd_charger_get_usbpd_num_ports(struct charger_data
*charger
)
133 struct ec_response_usb_pd_ports resp
;
136 ret
= cros_usbpd_charger_ec_command(charger
, 0, EC_CMD_USB_PD_PORTS
,
137 NULL
, 0, &resp
, sizeof(resp
));
141 return resp
.num_ports
;
144 static int cros_usbpd_charger_get_discovery_info(struct port_data
*port
)
146 struct charger_data
*charger
= port
->charger
;
147 struct ec_params_usb_pd_discovery_entry resp
;
148 struct ec_params_usb_pd_info_request req
;
151 req
.port
= port
->port_number
;
153 ret
= cros_usbpd_charger_ec_command(charger
, 0,
154 EC_CMD_USB_PD_DISCOVERY
,
156 &resp
, sizeof(resp
));
158 dev_err(charger
->dev
,
159 "Unable to query discovery info (err:0x%x)\n", ret
);
163 dev_dbg(charger
->dev
, "Port %d: VID = 0x%x, PID=0x%x, PTYPE=0x%x\n",
164 port
->port_number
, resp
.vid
, resp
.pid
, resp
.ptype
);
166 snprintf(port
->manufacturer
, sizeof(port
->manufacturer
), "%x",
168 snprintf(port
->model_name
, sizeof(port
->model_name
), "%x", resp
.pid
);
173 static int cros_usbpd_charger_get_power_info(struct port_data
*port
)
175 struct charger_data
*charger
= port
->charger
;
176 struct ec_response_usb_pd_power_info resp
;
177 struct ec_params_usb_pd_power_info req
;
178 int last_psy_status
, last_psy_usb_type
;
179 struct device
*dev
= charger
->dev
;
182 req
.port
= port
->port_number
;
183 ret
= cros_usbpd_charger_ec_command(charger
, 0,
184 EC_CMD_USB_PD_POWER_INFO
,
186 &resp
, sizeof(resp
));
188 dev_err(dev
, "Unable to query PD power info (err:0x%x)\n", ret
);
192 last_psy_status
= port
->psy_status
;
193 last_psy_usb_type
= port
->psy_usb_type
;
196 case USB_PD_PORT_POWER_DISCONNECTED
:
197 port
->psy_status
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
198 port
->psy_online
= 0;
200 case USB_PD_PORT_POWER_SOURCE
:
201 port
->psy_status
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
202 port
->psy_online
= 0;
204 case USB_PD_PORT_POWER_SINK
:
205 port
->psy_status
= POWER_SUPPLY_STATUS_CHARGING
;
206 port
->psy_online
= 1;
208 case USB_PD_PORT_POWER_SINK_NOT_CHARGING
:
209 port
->psy_status
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
210 port
->psy_online
= 1;
213 dev_err(dev
, "Unknown role %d\n", resp
.role
);
217 port
->psy_voltage_max_design
= resp
.meas
.voltage_max
;
218 port
->psy_voltage_now
= resp
.meas
.voltage_now
;
219 port
->psy_current_max
= resp
.meas
.current_max
;
220 port
->psy_power_max
= resp
.max_power
;
223 case USB_CHG_TYPE_BC12_SDP
:
224 case USB_CHG_TYPE_VBUS
:
225 port
->psy_usb_type
= POWER_SUPPLY_USB_TYPE_SDP
;
227 case USB_CHG_TYPE_NONE
:
229 * For dual-role devices when we are a source, the firmware
230 * reports the type as NONE. Report such chargers as type
233 if (resp
.role
== USB_PD_PORT_POWER_SOURCE
&& resp
.dualrole
)
234 port
->psy_usb_type
= POWER_SUPPLY_USB_TYPE_PD_DRP
;
236 port
->psy_usb_type
= POWER_SUPPLY_USB_TYPE_SDP
;
238 case USB_CHG_TYPE_OTHER
:
239 case USB_CHG_TYPE_PROPRIETARY
:
240 port
->psy_usb_type
= POWER_SUPPLY_USB_TYPE_APPLE_BRICK_ID
;
243 port
->psy_usb_type
= POWER_SUPPLY_USB_TYPE_C
;
245 case USB_CHG_TYPE_BC12_DCP
:
246 port
->psy_usb_type
= POWER_SUPPLY_USB_TYPE_DCP
;
248 case USB_CHG_TYPE_BC12_CDP
:
249 port
->psy_usb_type
= POWER_SUPPLY_USB_TYPE_CDP
;
251 case USB_CHG_TYPE_PD
:
253 port
->psy_usb_type
= POWER_SUPPLY_USB_TYPE_PD_DRP
;
255 port
->psy_usb_type
= POWER_SUPPLY_USB_TYPE_PD
;
257 case USB_CHG_TYPE_UNKNOWN
:
259 * While the EC is trying to determine the type of charger that
260 * has been plugged in, it will report the charger type as
261 * unknown. Additionally since the power capabilities are
262 * unknown, report the max current and voltage as zero.
264 port
->psy_usb_type
= POWER_SUPPLY_USB_TYPE_UNKNOWN
;
265 port
->psy_voltage_max_design
= 0;
266 port
->psy_current_max
= 0;
269 dev_dbg(dev
, "Port %d: default case!\n", port
->port_number
);
270 port
->psy_usb_type
= POWER_SUPPLY_USB_TYPE_SDP
;
273 if (cros_usbpd_charger_port_is_dedicated(port
))
274 port
->psy_desc
.type
= POWER_SUPPLY_TYPE_MAINS
;
276 port
->psy_desc
.type
= POWER_SUPPLY_TYPE_USB
;
279 "Port %d: type=%d vmax=%d vnow=%d cmax=%d clim=%d pmax=%d\n",
280 port
->port_number
, resp
.type
, resp
.meas
.voltage_max
,
281 resp
.meas
.voltage_now
, resp
.meas
.current_max
,
282 resp
.meas
.current_lim
, resp
.max_power
);
285 * If power supply type or status changed, explicitly call
286 * power_supply_changed. This results in udev event getting generated
287 * and allows user mode apps to react quicker instead of waiting for
288 * their next poll of power supply status.
290 if (last_psy_usb_type
!= port
->psy_usb_type
||
291 last_psy_status
!= port
->psy_status
)
292 power_supply_changed(port
->psy
);
297 static int cros_usbpd_charger_get_port_status(struct port_data
*port
,
303 time_is_after_jiffies(port
->last_update
+
304 CHARGER_CACHE_UPDATE_DELAY
))
307 ret
= cros_usbpd_charger_get_power_info(port
);
311 if (!cros_usbpd_charger_port_is_dedicated(port
))
312 ret
= cros_usbpd_charger_get_discovery_info(port
);
313 port
->last_update
= jiffies
;
318 static int cros_usbpd_charger_set_ext_power_limit(struct charger_data
*charger
,
322 struct ec_params_external_power_limit_v1 req
;
325 req
.current_lim
= current_lim
;
326 req
.voltage_lim
= voltage_lim
;
328 ret
= cros_usbpd_charger_ec_command(charger
, 0,
329 EC_CMD_EXTERNAL_POWER_LIMIT
,
330 &req
, sizeof(req
), NULL
, 0);
332 dev_err(charger
->dev
,
333 "Unable to set the 'External Power Limit': %d\n", ret
);
338 static void cros_usbpd_charger_power_changed(struct power_supply
*psy
)
340 struct port_data
*port
= power_supply_get_drvdata(psy
);
341 struct charger_data
*charger
= port
->charger
;
344 for (i
= 0; i
< charger
->num_registered_psy
; i
++)
345 cros_usbpd_charger_get_port_status(charger
->ports
[i
], false);
348 static int cros_usbpd_charger_get_prop(struct power_supply
*psy
,
349 enum power_supply_property psp
,
350 union power_supply_propval
*val
)
352 struct port_data
*port
= power_supply_get_drvdata(psy
);
353 struct charger_data
*charger
= port
->charger
;
354 struct cros_ec_device
*ec_device
= charger
->ec_device
;
355 struct device
*dev
= charger
->dev
;
358 /* Only refresh ec_port_status for dynamic properties */
360 case POWER_SUPPLY_PROP_ONLINE
:
362 * If mkbp_event_supported, then we can be assured that
363 * the driver's state for the online property is consistent
364 * with the hardware. However, if we aren't event driven,
365 * the optimization before to skip an ec_port_status get
366 * and only returned cached values of the online property will
367 * cause a delay in detecting a cable attach until one of the
368 * other properties are read.
370 * Allow an ec_port_status refresh for online property check
371 * if we're not already online to check for plug events if
372 * not mkbp_event_supported.
374 if (ec_device
->mkbp_event_supported
|| port
->psy_online
)
377 case POWER_SUPPLY_PROP_CURRENT_MAX
:
378 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN
:
379 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
380 ret
= cros_usbpd_charger_get_port_status(port
, true);
382 dev_err(dev
, "Failed to get port status (err:0x%x)\n",
392 case POWER_SUPPLY_PROP_ONLINE
:
393 val
->intval
= port
->psy_online
;
395 case POWER_SUPPLY_PROP_STATUS
:
396 val
->intval
= port
->psy_status
;
398 case POWER_SUPPLY_PROP_CURRENT_MAX
:
399 val
->intval
= port
->psy_current_max
* 1000;
401 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN
:
402 val
->intval
= port
->psy_voltage_max_design
* 1000;
404 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
405 val
->intval
= port
->psy_voltage_now
* 1000;
407 case POWER_SUPPLY_PROP_USB_TYPE
:
408 val
->intval
= port
->psy_usb_type
;
410 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT
:
411 if (input_current_limit
== EC_POWER_LIMIT_NONE
)
414 val
->intval
= input_current_limit
* 1000;
416 case POWER_SUPPLY_PROP_INPUT_VOLTAGE_LIMIT
:
417 if (input_voltage_limit
== EC_POWER_LIMIT_NONE
)
420 val
->intval
= input_voltage_limit
* 1000;
422 case POWER_SUPPLY_PROP_MODEL_NAME
:
423 val
->strval
= port
->model_name
;
425 case POWER_SUPPLY_PROP_MANUFACTURER
:
426 val
->strval
= port
->manufacturer
;
435 static int cros_usbpd_charger_set_prop(struct power_supply
*psy
,
436 enum power_supply_property psp
,
437 const union power_supply_propval
*val
)
439 struct port_data
*port
= power_supply_get_drvdata(psy
);
440 struct charger_data
*charger
= port
->charger
;
441 struct device
*dev
= charger
->dev
;
445 /* U16_MAX in mV/mA is the maximum supported value */
446 if (val
->intval
>= U16_MAX
* 1000)
448 /* A negative number is used to clear the limit */
450 intval
= EC_POWER_LIMIT_NONE
;
451 else /* Convert from uA/uV to mA/mV */
452 intval
= val
->intval
/ 1000;
455 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT
:
456 ret
= cros_usbpd_charger_set_ext_power_limit(charger
, intval
,
457 input_voltage_limit
);
461 input_current_limit
= intval
;
462 if (input_current_limit
== EC_POWER_LIMIT_NONE
)
464 "External Current Limit cleared for all ports\n");
467 "External Current Limit set to %dmA for all ports\n",
468 input_current_limit
);
470 case POWER_SUPPLY_PROP_INPUT_VOLTAGE_LIMIT
:
471 ret
= cros_usbpd_charger_set_ext_power_limit(charger
,
477 input_voltage_limit
= intval
;
478 if (input_voltage_limit
== EC_POWER_LIMIT_NONE
)
480 "External Voltage Limit cleared for all ports\n");
483 "External Voltage Limit set to %dmV for all ports\n",
484 input_voltage_limit
);
493 static int cros_usbpd_charger_property_is_writeable(struct power_supply
*psy
,
494 enum power_supply_property psp
)
499 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT
:
500 case POWER_SUPPLY_PROP_INPUT_VOLTAGE_LIMIT
:
510 static int cros_usbpd_charger_ec_event(struct notifier_block
*nb
,
511 unsigned long host_event
,
514 struct charger_data
*charger
= container_of(nb
, struct charger_data
,
517 cros_usbpd_charger_power_changed(charger
->ports
[0]->psy
);
521 static void cros_usbpd_charger_unregister_notifier(void *data
)
523 struct charger_data
*charger
= data
;
525 cros_usbpd_unregister_notify(&charger
->notifier
);
528 static int cros_usbpd_charger_probe(struct platform_device
*pd
)
530 struct cros_ec_dev
*ec_dev
= dev_get_drvdata(pd
->dev
.parent
);
531 struct cros_ec_device
*ec_device
= ec_dev
->ec_dev
;
532 struct power_supply_desc
*psy_desc
;
533 struct device
*dev
= &pd
->dev
;
534 struct charger_data
*charger
;
535 struct power_supply
*psy
;
536 struct port_data
*port
;
540 charger
= devm_kzalloc(dev
, sizeof(struct charger_data
),
546 charger
->ec_dev
= ec_dev
;
547 charger
->ec_device
= ec_device
;
549 platform_set_drvdata(pd
, charger
);
552 * We need to know the number of USB PD ports in order to know whether
553 * there is a dedicated port. The dedicated port will always be
554 * after the USB PD ports, and there should be only one.
556 charger
->num_usbpd_ports
=
557 cros_usbpd_charger_get_usbpd_num_ports(charger
);
558 if (charger
->num_usbpd_ports
<= 0) {
560 * This can happen on a system that doesn't support USB PD.
561 * Log a message, but no need to warn.
563 dev_info(dev
, "No USB PD charging ports found\n");
566 charger
->num_charger_ports
= cros_usbpd_charger_get_num_ports(charger
);
567 if (charger
->num_charger_ports
< 0) {
569 * This can happen on a system that doesn't support USB PD.
570 * Log a message, but no need to warn.
571 * Older ECs do not support the above command, in that case
572 * let's set up the number of charger ports equal to the number
575 dev_info(dev
, "Could not get charger port count\n");
576 charger
->num_charger_ports
= charger
->num_usbpd_ports
;
579 if (charger
->num_charger_ports
<= 0) {
581 * This can happen on a system that doesn't support USB PD and
582 * doesn't have a dedicated port.
583 * Log a message, but no need to warn.
585 dev_info(dev
, "No charging ports found\n");
591 * Sanity checks on the number of ports:
592 * there should be at most 1 dedicated port
594 if (charger
->num_charger_ports
< charger
->num_usbpd_ports
||
595 charger
->num_charger_ports
> (charger
->num_usbpd_ports
+ 1)) {
596 dev_err(dev
, "Unexpected number of charge port count\n");
601 for (i
= 0; i
< charger
->num_charger_ports
; i
++) {
602 struct power_supply_config psy_cfg
= {};
604 port
= devm_kzalloc(dev
, sizeof(struct port_data
), GFP_KERNEL
);
610 port
->charger
= charger
;
611 port
->port_number
= i
;
613 psy_desc
= &port
->psy_desc
;
614 psy_desc
->get_property
= cros_usbpd_charger_get_prop
;
615 psy_desc
->set_property
= cros_usbpd_charger_set_prop
;
616 psy_desc
->property_is_writeable
=
617 cros_usbpd_charger_property_is_writeable
;
618 psy_desc
->external_power_changed
=
619 cros_usbpd_charger_power_changed
;
620 psy_cfg
.drv_data
= port
;
621 psy_cfg
.no_wakeup_source
= true;
623 if (cros_usbpd_charger_port_is_dedicated(port
)) {
624 sprintf(port
->name
, CHARGER_DEDICATED_DIR_NAME
);
625 psy_desc
->type
= POWER_SUPPLY_TYPE_MAINS
;
626 psy_desc
->properties
=
627 cros_usbpd_dedicated_charger_props
;
628 psy_desc
->num_properties
=
629 ARRAY_SIZE(cros_usbpd_dedicated_charger_props
);
631 sprintf(port
->name
, CHARGER_USBPD_DIR_NAME
, i
);
632 psy_desc
->type
= POWER_SUPPLY_TYPE_USB
;
633 psy_desc
->properties
= cros_usbpd_charger_props
;
634 psy_desc
->num_properties
=
635 ARRAY_SIZE(cros_usbpd_charger_props
);
636 psy_desc
->usb_types
= BIT(POWER_SUPPLY_USB_TYPE_UNKNOWN
) |
637 BIT(POWER_SUPPLY_USB_TYPE_SDP
) |
638 BIT(POWER_SUPPLY_USB_TYPE_DCP
) |
639 BIT(POWER_SUPPLY_USB_TYPE_CDP
) |
640 BIT(POWER_SUPPLY_USB_TYPE_C
) |
641 BIT(POWER_SUPPLY_USB_TYPE_PD
) |
642 BIT(POWER_SUPPLY_USB_TYPE_PD_DRP
) |
643 BIT(POWER_SUPPLY_USB_TYPE_APPLE_BRICK_ID
);
646 psy_desc
->name
= port
->name
;
648 psy
= devm_power_supply_register(dev
, psy_desc
, &psy_cfg
);
650 dev_err(dev
, "Failed to register power supply\n");
655 charger
->ports
[charger
->num_registered_psy
++] = port
;
658 if (!charger
->num_registered_psy
) {
660 dev_err(dev
, "No power supplies registered\n");
664 /* Get PD events from the EC */
665 charger
->notifier
.notifier_call
= cros_usbpd_charger_ec_event
;
666 ret
= cros_usbpd_register_notify(&charger
->notifier
);
668 dev_warn(dev
, "failed to register notifier\n");
670 ret
= devm_add_action_or_reset(dev
,
671 cros_usbpd_charger_unregister_notifier
,
680 WARN(1, "%s: Failing probe (err:0x%x)\n", dev_name(dev
), ret
);
683 dev_info(dev
, "Failing probe (err:0x%x)\n", ret
);
687 #ifdef CONFIG_PM_SLEEP
688 static int cros_usbpd_charger_resume(struct device
*dev
)
690 struct charger_data
*charger
= dev_get_drvdata(dev
);
696 for (i
= 0; i
< charger
->num_registered_psy
; i
++) {
697 power_supply_changed(charger
->ports
[i
]->psy
);
698 charger
->ports
[i
]->last_update
=
699 jiffies
- CHARGER_CACHE_UPDATE_DELAY
;
706 static SIMPLE_DEV_PM_OPS(cros_usbpd_charger_pm_ops
, NULL
,
707 cros_usbpd_charger_resume
);
709 static const struct platform_device_id cros_usbpd_charger_id
[] = {
713 MODULE_DEVICE_TABLE(platform
, cros_usbpd_charger_id
);
715 static struct platform_driver cros_usbpd_charger_driver
= {
718 .pm
= &cros_usbpd_charger_pm_ops
,
720 .probe
= cros_usbpd_charger_probe
,
721 .id_table
= cros_usbpd_charger_id
,
724 module_platform_driver(cros_usbpd_charger_driver
);
726 MODULE_LICENSE("GPL");
727 MODULE_DESCRIPTION("ChromeOS EC USBPD charger");