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_DIR_NAME "CROS_USBPD_CHARGER%d"
16 #define CHARGER_DIR_NAME_LENGTH sizeof(CHARGER_DIR_NAME)
17 #define CHARGER_CACHE_UPDATE_DELAY msecs_to_jiffies(500)
18 #define CHARGER_MANUFACTURER_MODEL_LENGTH 32
20 #define DRV_NAME "cros-usbpd-charger"
24 char name
[CHARGER_DIR_NAME_LENGTH
];
25 char manufacturer
[CHARGER_MANUFACTURER_MODEL_LENGTH
];
26 char model_name
[CHARGER_MANUFACTURER_MODEL_LENGTH
];
27 struct power_supply
*psy
;
28 struct power_supply_desc psy_desc
;
33 int psy_voltage_max_design
;
36 struct charger_data
*charger
;
37 unsigned long last_update
;
42 struct cros_ec_dev
*ec_dev
;
43 struct cros_ec_device
*ec_device
;
44 int num_charger_ports
;
45 int num_registered_psy
;
46 struct port_data
*ports
[EC_USB_PD_MAX_PORTS
];
47 struct notifier_block notifier
;
50 static enum power_supply_property cros_usbpd_charger_props
[] = {
51 POWER_SUPPLY_PROP_ONLINE
,
52 POWER_SUPPLY_PROP_STATUS
,
53 POWER_SUPPLY_PROP_CURRENT_MAX
,
54 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN
,
55 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
56 POWER_SUPPLY_PROP_MODEL_NAME
,
57 POWER_SUPPLY_PROP_MANUFACTURER
,
58 POWER_SUPPLY_PROP_USB_TYPE
61 static enum power_supply_usb_type cros_usbpd_charger_usb_types
[] = {
62 POWER_SUPPLY_USB_TYPE_UNKNOWN
,
63 POWER_SUPPLY_USB_TYPE_SDP
,
64 POWER_SUPPLY_USB_TYPE_DCP
,
65 POWER_SUPPLY_USB_TYPE_CDP
,
66 POWER_SUPPLY_USB_TYPE_C
,
67 POWER_SUPPLY_USB_TYPE_PD
,
68 POWER_SUPPLY_USB_TYPE_PD_DRP
,
69 POWER_SUPPLY_USB_TYPE_APPLE_BRICK_ID
72 static int cros_usbpd_charger_ec_command(struct charger_data
*charger
,
80 struct cros_ec_dev
*ec_dev
= charger
->ec_dev
;
81 struct cros_ec_command
*msg
;
84 msg
= kzalloc(sizeof(*msg
) + max(outsize
, insize
), GFP_KERNEL
);
88 msg
->version
= version
;
89 msg
->command
= ec_dev
->cmd_offset
+ command
;
90 msg
->outsize
= outsize
;
94 memcpy(msg
->data
, outdata
, outsize
);
96 ret
= cros_ec_cmd_xfer_status(charger
->ec_device
, msg
);
97 if (ret
>= 0 && insize
)
98 memcpy(indata
, msg
->data
, insize
);
104 static int cros_usbpd_charger_get_num_ports(struct charger_data
*charger
)
106 struct ec_response_usb_pd_ports resp
;
109 ret
= cros_usbpd_charger_ec_command(charger
, 0, EC_CMD_USB_PD_PORTS
,
110 NULL
, 0, &resp
, sizeof(resp
));
112 dev_err(charger
->dev
,
113 "Unable to get the number or ports (err:0x%x)\n", ret
);
117 return resp
.num_ports
;
120 static int cros_usbpd_charger_get_discovery_info(struct port_data
*port
)
122 struct charger_data
*charger
= port
->charger
;
123 struct ec_params_usb_pd_discovery_entry resp
;
124 struct ec_params_usb_pd_info_request req
;
127 req
.port
= port
->port_number
;
129 ret
= cros_usbpd_charger_ec_command(charger
, 0,
130 EC_CMD_USB_PD_DISCOVERY
,
132 &resp
, sizeof(resp
));
134 dev_err(charger
->dev
,
135 "Unable to query discovery info (err:0x%x)\n", ret
);
139 dev_dbg(charger
->dev
, "Port %d: VID = 0x%x, PID=0x%x, PTYPE=0x%x\n",
140 port
->port_number
, resp
.vid
, resp
.pid
, resp
.ptype
);
142 snprintf(port
->manufacturer
, sizeof(port
->manufacturer
), "%x",
144 snprintf(port
->model_name
, sizeof(port
->model_name
), "%x", resp
.pid
);
149 static int cros_usbpd_charger_get_power_info(struct port_data
*port
)
151 struct charger_data
*charger
= port
->charger
;
152 struct ec_response_usb_pd_power_info resp
;
153 struct ec_params_usb_pd_power_info req
;
154 int last_psy_status
, last_psy_usb_type
;
155 struct device
*dev
= charger
->dev
;
158 req
.port
= port
->port_number
;
159 ret
= cros_usbpd_charger_ec_command(charger
, 0,
160 EC_CMD_USB_PD_POWER_INFO
,
162 &resp
, sizeof(resp
));
164 dev_err(dev
, "Unable to query PD power info (err:0x%x)\n", ret
);
168 last_psy_status
= port
->psy_status
;
169 last_psy_usb_type
= port
->psy_usb_type
;
172 case USB_PD_PORT_POWER_DISCONNECTED
:
173 port
->psy_status
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
174 port
->psy_online
= 0;
176 case USB_PD_PORT_POWER_SOURCE
:
177 port
->psy_status
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
178 port
->psy_online
= 0;
180 case USB_PD_PORT_POWER_SINK
:
181 port
->psy_status
= POWER_SUPPLY_STATUS_CHARGING
;
182 port
->psy_online
= 1;
184 case USB_PD_PORT_POWER_SINK_NOT_CHARGING
:
185 port
->psy_status
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
186 port
->psy_online
= 1;
189 dev_err(dev
, "Unknown role %d\n", resp
.role
);
193 port
->psy_voltage_max_design
= resp
.meas
.voltage_max
;
194 port
->psy_voltage_now
= resp
.meas
.voltage_now
;
195 port
->psy_current_max
= resp
.meas
.current_max
;
196 port
->psy_power_max
= resp
.max_power
;
199 case USB_CHG_TYPE_BC12_SDP
:
200 case USB_CHG_TYPE_VBUS
:
201 port
->psy_usb_type
= POWER_SUPPLY_USB_TYPE_SDP
;
203 case USB_CHG_TYPE_NONE
:
205 * For dual-role devices when we are a source, the firmware
206 * reports the type as NONE. Report such chargers as type
209 if (resp
.role
== USB_PD_PORT_POWER_SOURCE
&& resp
.dualrole
)
210 port
->psy_usb_type
= POWER_SUPPLY_USB_TYPE_PD_DRP
;
212 port
->psy_usb_type
= POWER_SUPPLY_USB_TYPE_SDP
;
214 case USB_CHG_TYPE_OTHER
:
215 case USB_CHG_TYPE_PROPRIETARY
:
216 port
->psy_usb_type
= POWER_SUPPLY_USB_TYPE_APPLE_BRICK_ID
;
219 port
->psy_usb_type
= POWER_SUPPLY_USB_TYPE_C
;
221 case USB_CHG_TYPE_BC12_DCP
:
222 port
->psy_usb_type
= POWER_SUPPLY_USB_TYPE_DCP
;
224 case USB_CHG_TYPE_BC12_CDP
:
225 port
->psy_usb_type
= POWER_SUPPLY_USB_TYPE_CDP
;
227 case USB_CHG_TYPE_PD
:
229 port
->psy_usb_type
= POWER_SUPPLY_USB_TYPE_PD_DRP
;
231 port
->psy_usb_type
= POWER_SUPPLY_USB_TYPE_PD
;
233 case USB_CHG_TYPE_UNKNOWN
:
235 * While the EC is trying to determine the type of charger that
236 * has been plugged in, it will report the charger type as
237 * unknown. Additionally since the power capabilities are
238 * unknown, report the max current and voltage as zero.
240 port
->psy_usb_type
= POWER_SUPPLY_USB_TYPE_UNKNOWN
;
241 port
->psy_voltage_max_design
= 0;
242 port
->psy_current_max
= 0;
245 dev_err(dev
, "Port %d: default case!\n", port
->port_number
);
246 port
->psy_usb_type
= POWER_SUPPLY_USB_TYPE_SDP
;
249 port
->psy_desc
.type
= POWER_SUPPLY_TYPE_USB
;
252 "Port %d: type=%d vmax=%d vnow=%d cmax=%d clim=%d pmax=%d\n",
253 port
->port_number
, resp
.type
, resp
.meas
.voltage_max
,
254 resp
.meas
.voltage_now
, resp
.meas
.current_max
,
255 resp
.meas
.current_lim
, resp
.max_power
);
258 * If power supply type or status changed, explicitly call
259 * power_supply_changed. This results in udev event getting generated
260 * and allows user mode apps to react quicker instead of waiting for
261 * their next poll of power supply status.
263 if (last_psy_usb_type
!= port
->psy_usb_type
||
264 last_psy_status
!= port
->psy_status
)
265 power_supply_changed(port
->psy
);
270 static int cros_usbpd_charger_get_port_status(struct port_data
*port
,
276 time_is_after_jiffies(port
->last_update
+
277 CHARGER_CACHE_UPDATE_DELAY
))
280 ret
= cros_usbpd_charger_get_power_info(port
);
284 ret
= cros_usbpd_charger_get_discovery_info(port
);
285 port
->last_update
= jiffies
;
290 static void cros_usbpd_charger_power_changed(struct power_supply
*psy
)
292 struct port_data
*port
= power_supply_get_drvdata(psy
);
293 struct charger_data
*charger
= port
->charger
;
296 for (i
= 0; i
< charger
->num_registered_psy
; i
++)
297 cros_usbpd_charger_get_port_status(charger
->ports
[i
], false);
300 static int cros_usbpd_charger_get_prop(struct power_supply
*psy
,
301 enum power_supply_property psp
,
302 union power_supply_propval
*val
)
304 struct port_data
*port
= power_supply_get_drvdata(psy
);
305 struct charger_data
*charger
= port
->charger
;
306 struct cros_ec_device
*ec_device
= charger
->ec_device
;
307 struct device
*dev
= charger
->dev
;
310 /* Only refresh ec_port_status for dynamic properties */
312 case POWER_SUPPLY_PROP_ONLINE
:
314 * If mkbp_event_supported, then we can be assured that
315 * the driver's state for the online property is consistent
316 * with the hardware. However, if we aren't event driven,
317 * the optimization before to skip an ec_port_status get
318 * and only returned cached values of the online property will
319 * cause a delay in detecting a cable attach until one of the
320 * other properties are read.
322 * Allow an ec_port_status refresh for online property check
323 * if we're not already online to check for plug events if
324 * not mkbp_event_supported.
326 if (ec_device
->mkbp_event_supported
|| port
->psy_online
)
329 case POWER_SUPPLY_PROP_CURRENT_MAX
:
330 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN
:
331 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
332 ret
= cros_usbpd_charger_get_port_status(port
, true);
334 dev_err(dev
, "Failed to get port status (err:0x%x)\n",
344 case POWER_SUPPLY_PROP_ONLINE
:
345 val
->intval
= port
->psy_online
;
347 case POWER_SUPPLY_PROP_STATUS
:
348 val
->intval
= port
->psy_status
;
350 case POWER_SUPPLY_PROP_CURRENT_MAX
:
351 val
->intval
= port
->psy_current_max
* 1000;
353 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN
:
354 val
->intval
= port
->psy_voltage_max_design
* 1000;
356 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
357 val
->intval
= port
->psy_voltage_now
* 1000;
359 case POWER_SUPPLY_PROP_USB_TYPE
:
360 val
->intval
= port
->psy_usb_type
;
362 case POWER_SUPPLY_PROP_MODEL_NAME
:
363 val
->strval
= port
->model_name
;
365 case POWER_SUPPLY_PROP_MANUFACTURER
:
366 val
->strval
= port
->manufacturer
;
375 static int cros_usbpd_charger_ec_event(struct notifier_block
*nb
,
376 unsigned long queued_during_suspend
,
379 struct cros_ec_device
*ec_device
;
380 struct charger_data
*charger
;
384 charger
= container_of(nb
, struct charger_data
, notifier
);
385 ec_device
= charger
->ec_device
;
388 host_event
= cros_ec_get_host_event(ec_device
);
389 if (host_event
& EC_HOST_EVENT_MASK(EC_HOST_EVENT_PD_MCU
)) {
390 cros_usbpd_charger_power_changed(charger
->ports
[0]->psy
);
397 static void cros_usbpd_charger_unregister_notifier(void *data
)
399 struct charger_data
*charger
= data
;
400 struct cros_ec_device
*ec_device
= charger
->ec_device
;
402 blocking_notifier_chain_unregister(&ec_device
->event_notifier
,
406 static int cros_usbpd_charger_probe(struct platform_device
*pd
)
408 struct cros_ec_dev
*ec_dev
= dev_get_drvdata(pd
->dev
.parent
);
409 struct cros_ec_device
*ec_device
= ec_dev
->ec_dev
;
410 struct power_supply_desc
*psy_desc
;
411 struct device
*dev
= &pd
->dev
;
412 struct charger_data
*charger
;
413 struct power_supply
*psy
;
414 struct port_data
*port
;
418 charger
= devm_kzalloc(dev
, sizeof(struct charger_data
),
424 charger
->ec_dev
= ec_dev
;
425 charger
->ec_device
= ec_device
;
427 platform_set_drvdata(pd
, charger
);
429 charger
->num_charger_ports
= cros_usbpd_charger_get_num_ports(charger
);
430 if (charger
->num_charger_ports
<= 0) {
432 * This can happen on a system that doesn't support USB PD.
433 * Log a message, but no need to warn.
435 dev_info(dev
, "No charging ports found\n");
440 for (i
= 0; i
< charger
->num_charger_ports
; i
++) {
441 struct power_supply_config psy_cfg
= {};
443 port
= devm_kzalloc(dev
, sizeof(struct port_data
), GFP_KERNEL
);
449 port
->charger
= charger
;
450 port
->port_number
= i
;
451 sprintf(port
->name
, CHARGER_DIR_NAME
, i
);
453 psy_desc
= &port
->psy_desc
;
454 psy_desc
->name
= port
->name
;
455 psy_desc
->type
= POWER_SUPPLY_TYPE_USB
;
456 psy_desc
->get_property
= cros_usbpd_charger_get_prop
;
457 psy_desc
->external_power_changed
=
458 cros_usbpd_charger_power_changed
;
459 psy_desc
->properties
= cros_usbpd_charger_props
;
460 psy_desc
->num_properties
=
461 ARRAY_SIZE(cros_usbpd_charger_props
);
462 psy_desc
->usb_types
= cros_usbpd_charger_usb_types
;
463 psy_desc
->num_usb_types
=
464 ARRAY_SIZE(cros_usbpd_charger_usb_types
);
465 psy_cfg
.drv_data
= port
;
467 psy
= devm_power_supply_register_no_ws(dev
, psy_desc
,
470 dev_err(dev
, "Failed to register power supply\n");
475 charger
->ports
[charger
->num_registered_psy
++] = port
;
478 if (!charger
->num_registered_psy
) {
480 dev_err(dev
, "No power supplies registered\n");
484 if (ec_device
->mkbp_event_supported
) {
485 /* Get PD events from the EC */
486 charger
->notifier
.notifier_call
= cros_usbpd_charger_ec_event
;
487 ret
= blocking_notifier_chain_register(
488 &ec_device
->event_notifier
,
491 dev_warn(dev
, "failed to register notifier\n");
493 ret
= devm_add_action_or_reset(dev
,
494 cros_usbpd_charger_unregister_notifier
,
504 WARN(1, "%s: Failing probe (err:0x%x)\n", dev_name(dev
), ret
);
507 dev_info(dev
, "Failing probe (err:0x%x)\n", ret
);
511 #ifdef CONFIG_PM_SLEEP
512 static int cros_usbpd_charger_resume(struct device
*dev
)
514 struct charger_data
*charger
= dev_get_drvdata(dev
);
520 for (i
= 0; i
< charger
->num_registered_psy
; i
++) {
521 power_supply_changed(charger
->ports
[i
]->psy
);
522 charger
->ports
[i
]->last_update
=
523 jiffies
- CHARGER_CACHE_UPDATE_DELAY
;
530 static SIMPLE_DEV_PM_OPS(cros_usbpd_charger_pm_ops
, NULL
,
531 cros_usbpd_charger_resume
);
533 static struct platform_driver cros_usbpd_charger_driver
= {
536 .pm
= &cros_usbpd_charger_pm_ops
,
538 .probe
= cros_usbpd_charger_probe
541 module_platform_driver(cros_usbpd_charger_driver
);
543 MODULE_LICENSE("GPL");
544 MODULE_DESCRIPTION("ChromeOS EC USBPD charger");
545 MODULE_ALIAS("platform:" DRV_NAME
);