1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Power supply driver for ChromeOS EC based USB PD Charger. 4 * 5 * Copyright (c) 2014 - 2018 Google, Inc 6 */ 7 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> 14 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 23 24 #define DRV_NAME "cros-usbpd-charger" 25 26 struct port_data { 27 int port_number; 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; 33 int psy_usb_type; 34 int psy_online; 35 int psy_status; 36 int psy_current_max; 37 int psy_voltage_max_design; 38 int psy_voltage_now; 39 int psy_power_max; 40 struct charger_data *charger; 41 unsigned long last_update; 42 }; 43 44 struct charger_data { 45 struct device *dev; 46 struct cros_ec_dev *ec_dev; 47 struct cros_ec_device *ec_device; 48 int num_charger_ports; 49 int num_usbpd_ports; 50 int num_registered_psy; 51 struct port_data *ports[EC_USB_PD_MAX_PORTS]; 52 struct notifier_block notifier; 53 }; 54 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 64 }; 65 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, 70 }; 71 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 81 }; 82 83 static bool cros_usbpd_charger_port_is_dedicated(struct port_data *port) 84 { 85 return port->port_number >= port->charger->num_usbpd_ports; 86 } 87 88 static int cros_usbpd_charger_ec_command(struct charger_data *charger, 89 unsigned int version, 90 unsigned int command, 91 void *outdata, 92 unsigned int outsize, 93 void *indata, 94 unsigned int insize) 95 { 96 struct cros_ec_dev *ec_dev = charger->ec_dev; 97 struct cros_ec_command *msg; 98 int ret; 99 100 msg = kzalloc(sizeof(*msg) + max(outsize, insize), GFP_KERNEL); 101 if (!msg) 102 return -ENOMEM; 103 104 msg->version = version; 105 msg->command = ec_dev->cmd_offset + command; 106 msg->outsize = outsize; 107 msg->insize = insize; 108 109 if (outsize) 110 memcpy(msg->data, outdata, outsize); 111 112 ret = cros_ec_cmd_xfer_status(charger->ec_device, msg); 113 if (ret >= 0 && insize) 114 memcpy(indata, msg->data, insize); 115 116 kfree(msg); 117 return ret; 118 } 119 120 static int cros_usbpd_charger_get_num_ports(struct charger_data *charger) 121 { 122 struct ec_response_charge_port_count resp; 123 int ret; 124 125 ret = cros_usbpd_charger_ec_command(charger, 0, 126 EC_CMD_CHARGE_PORT_COUNT, 127 NULL, 0, &resp, sizeof(resp)); 128 if (ret < 0) { 129 dev_err(charger->dev, 130 "Unable to get the number of ports (err:0x%x)\n", ret); 131 return ret; 132 } 133 134 return resp.port_count; 135 } 136 137 static int cros_usbpd_charger_get_usbpd_num_ports(struct charger_data *charger) 138 { 139 struct ec_response_usb_pd_ports resp; 140 int ret; 141 142 ret = cros_usbpd_charger_ec_command(charger, 0, EC_CMD_USB_PD_PORTS, 143 NULL, 0, &resp, sizeof(resp)); 144 if (ret < 0) { 145 dev_err(charger->dev, 146 "Unable to get the number or ports (err:0x%x)\n", ret); 147 return ret; 148 } 149 150 return resp.num_ports; 151 } 152 153 static int cros_usbpd_charger_get_discovery_info(struct port_data *port) 154 { 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; 158 int ret; 159 160 req.port = port->port_number; 161 162 ret = cros_usbpd_charger_ec_command(charger, 0, 163 EC_CMD_USB_PD_DISCOVERY, 164 &req, sizeof(req), 165 &resp, sizeof(resp)); 166 if (ret < 0) { 167 dev_err(charger->dev, 168 "Unable to query discovery info (err:0x%x)\n", ret); 169 return ret; 170 } 171 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); 174 175 snprintf(port->manufacturer, sizeof(port->manufacturer), "%x", 176 resp.vid); 177 snprintf(port->model_name, sizeof(port->model_name), "%x", resp.pid); 178 179 return 0; 180 } 181 182 static int cros_usbpd_charger_get_power_info(struct port_data *port) 183 { 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; 189 int ret; 190 191 req.port = port->port_number; 192 ret = cros_usbpd_charger_ec_command(charger, 0, 193 EC_CMD_USB_PD_POWER_INFO, 194 &req, sizeof(req), 195 &resp, sizeof(resp)); 196 if (ret < 0) { 197 dev_err(dev, "Unable to query PD power info (err:0x%x)\n", ret); 198 return ret; 199 } 200 201 last_psy_status = port->psy_status; 202 last_psy_usb_type = port->psy_usb_type; 203 204 switch (resp.role) { 205 case USB_PD_PORT_POWER_DISCONNECTED: 206 port->psy_status = POWER_SUPPLY_STATUS_NOT_CHARGING; 207 port->psy_online = 0; 208 break; 209 case USB_PD_PORT_POWER_SOURCE: 210 port->psy_status = POWER_SUPPLY_STATUS_NOT_CHARGING; 211 port->psy_online = 0; 212 break; 213 case USB_PD_PORT_POWER_SINK: 214 port->psy_status = POWER_SUPPLY_STATUS_CHARGING; 215 port->psy_online = 1; 216 break; 217 case USB_PD_PORT_POWER_SINK_NOT_CHARGING: 218 port->psy_status = POWER_SUPPLY_STATUS_NOT_CHARGING; 219 port->psy_online = 1; 220 break; 221 default: 222 dev_err(dev, "Unknown role %d\n", resp.role); 223 break; 224 } 225 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; 230 231 switch (resp.type) { 232 case USB_CHG_TYPE_BC12_SDP: 233 case USB_CHG_TYPE_VBUS: 234 port->psy_usb_type = POWER_SUPPLY_USB_TYPE_SDP; 235 break; 236 case USB_CHG_TYPE_NONE: 237 /* 238 * For dual-role devices when we are a source, the firmware 239 * reports the type as NONE. Report such chargers as type 240 * USB_PD_DRP. 241 */ 242 if (resp.role == USB_PD_PORT_POWER_SOURCE && resp.dualrole) 243 port->psy_usb_type = POWER_SUPPLY_USB_TYPE_PD_DRP; 244 else 245 port->psy_usb_type = POWER_SUPPLY_USB_TYPE_SDP; 246 break; 247 case USB_CHG_TYPE_OTHER: 248 case USB_CHG_TYPE_PROPRIETARY: 249 port->psy_usb_type = POWER_SUPPLY_USB_TYPE_APPLE_BRICK_ID; 250 break; 251 case USB_CHG_TYPE_C: 252 port->psy_usb_type = POWER_SUPPLY_USB_TYPE_C; 253 break; 254 case USB_CHG_TYPE_BC12_DCP: 255 port->psy_usb_type = POWER_SUPPLY_USB_TYPE_DCP; 256 break; 257 case USB_CHG_TYPE_BC12_CDP: 258 port->psy_usb_type = POWER_SUPPLY_USB_TYPE_CDP; 259 break; 260 case USB_CHG_TYPE_PD: 261 if (resp.dualrole) 262 port->psy_usb_type = POWER_SUPPLY_USB_TYPE_PD_DRP; 263 else 264 port->psy_usb_type = POWER_SUPPLY_USB_TYPE_PD; 265 break; 266 case USB_CHG_TYPE_UNKNOWN: 267 /* 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. 272 */ 273 port->psy_usb_type = POWER_SUPPLY_USB_TYPE_UNKNOWN; 274 port->psy_voltage_max_design = 0; 275 port->psy_current_max = 0; 276 break; 277 default: 278 dev_err(dev, "Port %d: default case!\n", port->port_number); 279 port->psy_usb_type = POWER_SUPPLY_USB_TYPE_SDP; 280 } 281 282 if (cros_usbpd_charger_port_is_dedicated(port)) 283 port->psy_desc.type = POWER_SUPPLY_TYPE_MAINS; 284 else 285 port->psy_desc.type = POWER_SUPPLY_TYPE_USB; 286 287 dev_dbg(dev, 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); 292 293 /* 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. 298 */ 299 if (last_psy_usb_type != port->psy_usb_type || 300 last_psy_status != port->psy_status) 301 power_supply_changed(port->psy); 302 303 return 0; 304 } 305 306 static int cros_usbpd_charger_get_port_status(struct port_data *port, 307 bool ratelimit) 308 { 309 int ret; 310 311 if (ratelimit && 312 time_is_after_jiffies(port->last_update + 313 CHARGER_CACHE_UPDATE_DELAY)) 314 return 0; 315 316 ret = cros_usbpd_charger_get_power_info(port); 317 if (ret < 0) 318 return ret; 319 320 if (!cros_usbpd_charger_port_is_dedicated(port)) 321 ret = cros_usbpd_charger_get_discovery_info(port); 322 port->last_update = jiffies; 323 324 return ret; 325 } 326 327 static void cros_usbpd_charger_power_changed(struct power_supply *psy) 328 { 329 struct port_data *port = power_supply_get_drvdata(psy); 330 struct charger_data *charger = port->charger; 331 int i; 332 333 for (i = 0; i < charger->num_registered_psy; i++) 334 cros_usbpd_charger_get_port_status(charger->ports[i], false); 335 } 336 337 static int cros_usbpd_charger_get_prop(struct power_supply *psy, 338 enum power_supply_property psp, 339 union power_supply_propval *val) 340 { 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; 345 int ret; 346 347 /* Only refresh ec_port_status for dynamic properties */ 348 switch (psp) { 349 case POWER_SUPPLY_PROP_ONLINE: 350 /* 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. 358 * 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. 362 */ 363 if (ec_device->mkbp_event_supported || port->psy_online) 364 break; 365 /* fall through */ 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); 370 if (ret < 0) { 371 dev_err(dev, "Failed to get port status (err:0x%x)\n", 372 ret); 373 return -EINVAL; 374 } 375 break; 376 default: 377 break; 378 } 379 380 switch (psp) { 381 case POWER_SUPPLY_PROP_ONLINE: 382 val->intval = port->psy_online; 383 break; 384 case POWER_SUPPLY_PROP_STATUS: 385 val->intval = port->psy_status; 386 break; 387 case POWER_SUPPLY_PROP_CURRENT_MAX: 388 val->intval = port->psy_current_max * 1000; 389 break; 390 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN: 391 val->intval = port->psy_voltage_max_design * 1000; 392 break; 393 case POWER_SUPPLY_PROP_VOLTAGE_NOW: 394 val->intval = port->psy_voltage_now * 1000; 395 break; 396 case POWER_SUPPLY_PROP_USB_TYPE: 397 val->intval = port->psy_usb_type; 398 break; 399 case POWER_SUPPLY_PROP_MODEL_NAME: 400 val->strval = port->model_name; 401 break; 402 case POWER_SUPPLY_PROP_MANUFACTURER: 403 val->strval = port->manufacturer; 404 break; 405 default: 406 return -EINVAL; 407 } 408 409 return 0; 410 } 411 412 static int cros_usbpd_charger_ec_event(struct notifier_block *nb, 413 unsigned long queued_during_suspend, 414 void *_notify) 415 { 416 struct cros_ec_device *ec_device; 417 struct charger_data *charger; 418 u32 host_event; 419 420 charger = container_of(nb, struct charger_data, notifier); 421 ec_device = charger->ec_device; 422 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); 426 return NOTIFY_OK; 427 } else { 428 return NOTIFY_DONE; 429 } 430 } 431 432 static void cros_usbpd_charger_unregister_notifier(void *data) 433 { 434 struct charger_data *charger = data; 435 struct cros_ec_device *ec_device = charger->ec_device; 436 437 blocking_notifier_chain_unregister(&ec_device->event_notifier, 438 &charger->notifier); 439 } 440 441 static int cros_usbpd_charger_probe(struct platform_device *pd) 442 { 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; 450 int ret = -EINVAL; 451 int i; 452 453 charger = devm_kzalloc(dev, sizeof(struct charger_data), 454 GFP_KERNEL); 455 if (!charger) 456 return -ENOMEM; 457 458 charger->dev = dev; 459 charger->ec_dev = ec_dev; 460 charger->ec_device = ec_device; 461 462 platform_set_drvdata(pd, charger); 463 464 /* 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. 468 */ 469 charger->num_usbpd_ports = 470 cros_usbpd_charger_get_usbpd_num_ports(charger); 471 if (charger->num_usbpd_ports <= 0) { 472 /* 473 * This can happen on a system that doesn't support USB PD. 474 * Log a message, but no need to warn. 475 */ 476 dev_info(dev, "No USB PD charging ports found\n"); 477 } 478 479 charger->num_charger_ports = cros_usbpd_charger_get_num_ports(charger); 480 if (charger->num_charger_ports < 0) { 481 /* 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 486 * of USB PD ports 487 */ 488 dev_info(dev, "Could not get charger port count\n"); 489 charger->num_charger_ports = charger->num_usbpd_ports; 490 } 491 492 if (charger->num_charger_ports <= 0) { 493 /* 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. 497 */ 498 dev_info(dev, "No charging ports found\n"); 499 ret = -ENODEV; 500 goto fail_nowarn; 501 } 502 503 /* 504 * Sanity checks on the number of ports: 505 * there should be at most 1 dedicated port 506 */ 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"); 510 ret = -EPROTO; 511 goto fail_nowarn; 512 } 513 514 for (i = 0; i < charger->num_charger_ports; i++) { 515 struct power_supply_config psy_cfg = {}; 516 517 port = devm_kzalloc(dev, sizeof(struct port_data), GFP_KERNEL); 518 if (!port) { 519 ret = -ENOMEM; 520 goto fail; 521 } 522 523 port->charger = charger; 524 port->port_number = i; 525 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; 531 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); 539 } else { 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); 548 } 549 550 psy_desc->name = port->name; 551 552 psy = devm_power_supply_register_no_ws(dev, psy_desc, 553 &psy_cfg); 554 if (IS_ERR(psy)) { 555 dev_err(dev, "Failed to register power supply\n"); 556 continue; 557 } 558 port->psy = psy; 559 560 charger->ports[charger->num_registered_psy++] = port; 561 } 562 563 if (!charger->num_registered_psy) { 564 ret = -ENODEV; 565 dev_err(dev, "No power supplies registered\n"); 566 goto fail; 567 } 568 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, 574 &charger->notifier); 575 if (ret < 0) { 576 dev_warn(dev, "failed to register notifier\n"); 577 } else { 578 ret = devm_add_action_or_reset(dev, 579 cros_usbpd_charger_unregister_notifier, 580 charger); 581 if (ret < 0) 582 goto fail; 583 } 584 } 585 586 return 0; 587 588 fail: 589 WARN(1, "%s: Failing probe (err:0x%x)\n", dev_name(dev), ret); 590 591 fail_nowarn: 592 dev_info(dev, "Failing probe (err:0x%x)\n", ret); 593 return ret; 594 } 595 596 #ifdef CONFIG_PM_SLEEP 597 static int cros_usbpd_charger_resume(struct device *dev) 598 { 599 struct charger_data *charger = dev_get_drvdata(dev); 600 int i; 601 602 if (!charger) 603 return 0; 604 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; 609 } 610 611 return 0; 612 } 613 #endif 614 615 static SIMPLE_DEV_PM_OPS(cros_usbpd_charger_pm_ops, NULL, 616 cros_usbpd_charger_resume); 617 618 static struct platform_driver cros_usbpd_charger_driver = { 619 .driver = { 620 .name = DRV_NAME, 621 .pm = &cros_usbpd_charger_pm_ops, 622 }, 623 .probe = cros_usbpd_charger_probe 624 }; 625 626 module_platform_driver(cros_usbpd_charger_driver); 627 628 MODULE_LICENSE("GPL"); 629 MODULE_DESCRIPTION("ChromeOS EC USBPD charger"); 630 MODULE_ALIAS("platform:" DRV_NAME); 631