Lines Matching +full:usb +full:- +full:charger

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
26 #define DRV_NAME "cros-usbpd-charger"
42 struct charger_data *charger; member
82 return port->port_number >= port->charger->num_usbpd_ports; in cros_usbpd_charger_port_is_dedicated()
85 static int cros_usbpd_charger_ec_command(struct charger_data *charger, in cros_usbpd_charger_ec_command() argument
93 struct cros_ec_dev *ec_dev = charger->ec_dev; in cros_usbpd_charger_ec_command()
99 return -ENOMEM; in cros_usbpd_charger_ec_command()
101 msg->version = version; in cros_usbpd_charger_ec_command()
102 msg->command = ec_dev->cmd_offset + command; in cros_usbpd_charger_ec_command()
103 msg->outsize = outsize; in cros_usbpd_charger_ec_command()
104 msg->insize = insize; in cros_usbpd_charger_ec_command()
107 memcpy(msg->data, outdata, outsize); in cros_usbpd_charger_ec_command()
109 ret = cros_ec_cmd_xfer_status(charger->ec_device, msg); in cros_usbpd_charger_ec_command()
111 memcpy(indata, msg->data, insize); in cros_usbpd_charger_ec_command()
117 static int cros_usbpd_charger_get_num_ports(struct charger_data *charger) in cros_usbpd_charger_get_num_ports() argument
122 ret = cros_usbpd_charger_ec_command(charger, 0, in cros_usbpd_charger_get_num_ports()
131 static int cros_usbpd_charger_get_usbpd_num_ports(struct charger_data *charger) in cros_usbpd_charger_get_usbpd_num_ports() argument
136 ret = cros_usbpd_charger_ec_command(charger, 0, EC_CMD_USB_PD_PORTS, in cros_usbpd_charger_get_usbpd_num_ports()
146 struct charger_data *charger = port->charger; in cros_usbpd_charger_get_discovery_info() local
151 req.port = port->port_number; in cros_usbpd_charger_get_discovery_info()
153 ret = cros_usbpd_charger_ec_command(charger, 0, in cros_usbpd_charger_get_discovery_info()
158 dev_err(charger->dev, in cros_usbpd_charger_get_discovery_info()
163 dev_dbg(charger->dev, "Port %d: VID = 0x%x, PID=0x%x, PTYPE=0x%x\n", in cros_usbpd_charger_get_discovery_info()
164 port->port_number, resp.vid, resp.pid, resp.ptype); in cros_usbpd_charger_get_discovery_info()
166 snprintf(port->manufacturer, sizeof(port->manufacturer), "%x", in cros_usbpd_charger_get_discovery_info()
168 snprintf(port->model_name, sizeof(port->model_name), "%x", resp.pid); in cros_usbpd_charger_get_discovery_info()
175 struct charger_data *charger = port->charger; in cros_usbpd_charger_get_power_info() local
179 struct device *dev = charger->dev; in cros_usbpd_charger_get_power_info()
182 req.port = port->port_number; in cros_usbpd_charger_get_power_info()
183 ret = cros_usbpd_charger_ec_command(charger, 0, in cros_usbpd_charger_get_power_info()
192 last_psy_status = port->psy_status; in cros_usbpd_charger_get_power_info()
193 last_psy_usb_type = port->psy_usb_type; in cros_usbpd_charger_get_power_info()
197 port->psy_status = POWER_SUPPLY_STATUS_NOT_CHARGING; in cros_usbpd_charger_get_power_info()
198 port->psy_online = 0; in cros_usbpd_charger_get_power_info()
201 port->psy_status = POWER_SUPPLY_STATUS_NOT_CHARGING; in cros_usbpd_charger_get_power_info()
202 port->psy_online = 0; in cros_usbpd_charger_get_power_info()
205 port->psy_status = POWER_SUPPLY_STATUS_CHARGING; in cros_usbpd_charger_get_power_info()
206 port->psy_online = 1; in cros_usbpd_charger_get_power_info()
209 port->psy_status = POWER_SUPPLY_STATUS_NOT_CHARGING; in cros_usbpd_charger_get_power_info()
210 port->psy_online = 1; in cros_usbpd_charger_get_power_info()
217 port->psy_voltage_max_design = resp.meas.voltage_max; in cros_usbpd_charger_get_power_info()
218 port->psy_voltage_now = resp.meas.voltage_now; in cros_usbpd_charger_get_power_info()
219 port->psy_current_max = resp.meas.current_max; in cros_usbpd_charger_get_power_info()
220 port->psy_power_max = resp.max_power; in cros_usbpd_charger_get_power_info()
225 port->psy_usb_type = POWER_SUPPLY_USB_TYPE_SDP; in cros_usbpd_charger_get_power_info()
229 * For dual-role devices when we are a source, the firmware in cros_usbpd_charger_get_power_info()
234 port->psy_usb_type = POWER_SUPPLY_USB_TYPE_PD_DRP; in cros_usbpd_charger_get_power_info()
236 port->psy_usb_type = POWER_SUPPLY_USB_TYPE_SDP; in cros_usbpd_charger_get_power_info()
240 port->psy_usb_type = POWER_SUPPLY_USB_TYPE_APPLE_BRICK_ID; in cros_usbpd_charger_get_power_info()
243 port->psy_usb_type = POWER_SUPPLY_USB_TYPE_C; in cros_usbpd_charger_get_power_info()
246 port->psy_usb_type = POWER_SUPPLY_USB_TYPE_DCP; in cros_usbpd_charger_get_power_info()
249 port->psy_usb_type = POWER_SUPPLY_USB_TYPE_CDP; in cros_usbpd_charger_get_power_info()
253 port->psy_usb_type = POWER_SUPPLY_USB_TYPE_PD_DRP; in cros_usbpd_charger_get_power_info()
255 port->psy_usb_type = POWER_SUPPLY_USB_TYPE_PD; in cros_usbpd_charger_get_power_info()
259 * While the EC is trying to determine the type of charger that in cros_usbpd_charger_get_power_info()
260 * has been plugged in, it will report the charger type as in cros_usbpd_charger_get_power_info()
264 port->psy_usb_type = POWER_SUPPLY_USB_TYPE_UNKNOWN; in cros_usbpd_charger_get_power_info()
265 port->psy_voltage_max_design = 0; in cros_usbpd_charger_get_power_info()
266 port->psy_current_max = 0; in cros_usbpd_charger_get_power_info()
269 dev_dbg(dev, "Port %d: default case!\n", port->port_number); in cros_usbpd_charger_get_power_info()
270 port->psy_usb_type = POWER_SUPPLY_USB_TYPE_SDP; in cros_usbpd_charger_get_power_info()
274 port->psy_desc.type = POWER_SUPPLY_TYPE_MAINS; in cros_usbpd_charger_get_power_info()
276 port->psy_desc.type = POWER_SUPPLY_TYPE_USB; in cros_usbpd_charger_get_power_info()
280 port->port_number, resp.type, resp.meas.voltage_max, in cros_usbpd_charger_get_power_info()
290 if (last_psy_usb_type != port->psy_usb_type || in cros_usbpd_charger_get_power_info()
291 last_psy_status != port->psy_status) in cros_usbpd_charger_get_power_info()
292 power_supply_changed(port->psy); in cros_usbpd_charger_get_power_info()
303 time_is_after_jiffies(port->last_update + in cros_usbpd_charger_get_port_status()
313 port->last_update = jiffies; in cros_usbpd_charger_get_port_status()
318 static int cros_usbpd_charger_set_ext_power_limit(struct charger_data *charger, in cros_usbpd_charger_set_ext_power_limit() argument
328 ret = cros_usbpd_charger_ec_command(charger, 0, in cros_usbpd_charger_set_ext_power_limit()
332 dev_err(charger->dev, in cros_usbpd_charger_set_ext_power_limit()
341 struct charger_data *charger = port->charger; in cros_usbpd_charger_power_changed() local
344 for (i = 0; i < charger->num_registered_psy; i++) in cros_usbpd_charger_power_changed()
345 cros_usbpd_charger_get_port_status(charger->ports[i], false); in cros_usbpd_charger_power_changed()
353 struct charger_data *charger = port->charger; in cros_usbpd_charger_get_prop() local
354 struct cros_ec_device *ec_device = charger->ec_device; in cros_usbpd_charger_get_prop()
355 struct device *dev = charger->dev; in cros_usbpd_charger_get_prop()
374 if (ec_device->mkbp_event_supported || port->psy_online) in cros_usbpd_charger_get_prop()
384 return -EINVAL; in cros_usbpd_charger_get_prop()
393 val->intval = port->psy_online; in cros_usbpd_charger_get_prop()
396 val->intval = port->psy_status; in cros_usbpd_charger_get_prop()
399 val->intval = port->psy_current_max * 1000; in cros_usbpd_charger_get_prop()
402 val->intval = port->psy_voltage_max_design * 1000; in cros_usbpd_charger_get_prop()
405 val->intval = port->psy_voltage_now * 1000; in cros_usbpd_charger_get_prop()
408 val->intval = port->psy_usb_type; in cros_usbpd_charger_get_prop()
412 val->intval = -1; in cros_usbpd_charger_get_prop()
414 val->intval = input_current_limit * 1000; in cros_usbpd_charger_get_prop()
418 val->intval = -1; in cros_usbpd_charger_get_prop()
420 val->intval = input_voltage_limit * 1000; in cros_usbpd_charger_get_prop()
423 val->strval = port->model_name; in cros_usbpd_charger_get_prop()
426 val->strval = port->manufacturer; in cros_usbpd_charger_get_prop()
429 return -EINVAL; in cros_usbpd_charger_get_prop()
440 struct charger_data *charger = port->charger; in cros_usbpd_charger_set_prop() local
441 struct device *dev = charger->dev; in cros_usbpd_charger_set_prop()
446 if (val->intval >= U16_MAX * 1000) in cros_usbpd_charger_set_prop()
447 return -EINVAL; in cros_usbpd_charger_set_prop()
449 if (val->intval < 0) in cros_usbpd_charger_set_prop()
452 intval = val->intval / 1000; in cros_usbpd_charger_set_prop()
456 ret = cros_usbpd_charger_set_ext_power_limit(charger, intval, in cros_usbpd_charger_set_prop()
471 ret = cros_usbpd_charger_set_ext_power_limit(charger, in cros_usbpd_charger_set_prop()
487 ret = -EINVAL; in cros_usbpd_charger_set_prop()
514 struct charger_data *charger = container_of(nb, struct charger_data, in cros_usbpd_charger_ec_event() local
517 cros_usbpd_charger_power_changed(charger->ports[0]->psy); in cros_usbpd_charger_ec_event()
523 struct charger_data *charger = data; in cros_usbpd_charger_unregister_notifier() local
525 cros_usbpd_unregister_notify(&charger->notifier); in cros_usbpd_charger_unregister_notifier()
530 struct cros_ec_dev *ec_dev = dev_get_drvdata(pd->dev.parent); in cros_usbpd_charger_probe()
531 struct cros_ec_device *ec_device = ec_dev->ec_dev; in cros_usbpd_charger_probe()
533 struct device *dev = &pd->dev; in cros_usbpd_charger_probe()
534 struct charger_data *charger; in cros_usbpd_charger_probe() local
537 int ret = -EINVAL; in cros_usbpd_charger_probe()
540 charger = devm_kzalloc(dev, sizeof(struct charger_data), in cros_usbpd_charger_probe()
542 if (!charger) in cros_usbpd_charger_probe()
543 return -ENOMEM; in cros_usbpd_charger_probe()
545 charger->dev = dev; in cros_usbpd_charger_probe()
546 charger->ec_dev = ec_dev; in cros_usbpd_charger_probe()
547 charger->ec_device = ec_device; in cros_usbpd_charger_probe()
549 platform_set_drvdata(pd, charger); in cros_usbpd_charger_probe()
552 * We need to know the number of USB PD ports in order to know whether in cros_usbpd_charger_probe()
554 * after the USB PD ports, and there should be only one. in cros_usbpd_charger_probe()
556 charger->num_usbpd_ports = in cros_usbpd_charger_probe()
557 cros_usbpd_charger_get_usbpd_num_ports(charger); in cros_usbpd_charger_probe()
558 if (charger->num_usbpd_ports <= 0) { in cros_usbpd_charger_probe()
560 * This can happen on a system that doesn't support USB PD. in cros_usbpd_charger_probe()
563 dev_info(dev, "No USB PD charging ports found\n"); in cros_usbpd_charger_probe()
566 charger->num_charger_ports = cros_usbpd_charger_get_num_ports(charger); in cros_usbpd_charger_probe()
567 if (charger->num_charger_ports < 0) { in cros_usbpd_charger_probe()
569 * This can happen on a system that doesn't support USB PD. in cros_usbpd_charger_probe()
572 * let's set up the number of charger ports equal to the number in cros_usbpd_charger_probe()
573 * of USB PD ports in cros_usbpd_charger_probe()
575 dev_info(dev, "Could not get charger port count\n"); in cros_usbpd_charger_probe()
576 charger->num_charger_ports = charger->num_usbpd_ports; in cros_usbpd_charger_probe()
579 if (charger->num_charger_ports <= 0) { in cros_usbpd_charger_probe()
581 * This can happen on a system that doesn't support USB PD and in cros_usbpd_charger_probe()
586 ret = -ENODEV; in cros_usbpd_charger_probe()
594 if (charger->num_charger_ports < charger->num_usbpd_ports || in cros_usbpd_charger_probe()
595 charger->num_charger_ports > (charger->num_usbpd_ports + 1)) { in cros_usbpd_charger_probe()
597 ret = -EPROTO; in cros_usbpd_charger_probe()
601 for (i = 0; i < charger->num_charger_ports; i++) { in cros_usbpd_charger_probe()
606 ret = -ENOMEM; in cros_usbpd_charger_probe()
610 port->charger = charger; in cros_usbpd_charger_probe()
611 port->port_number = i; in cros_usbpd_charger_probe()
613 psy_desc = &port->psy_desc; in cros_usbpd_charger_probe()
614 psy_desc->get_property = cros_usbpd_charger_get_prop; in cros_usbpd_charger_probe()
615 psy_desc->set_property = cros_usbpd_charger_set_prop; in cros_usbpd_charger_probe()
616 psy_desc->property_is_writeable = in cros_usbpd_charger_probe()
618 psy_desc->external_power_changed = in cros_usbpd_charger_probe()
624 sprintf(port->name, CHARGER_DEDICATED_DIR_NAME); in cros_usbpd_charger_probe()
625 psy_desc->type = POWER_SUPPLY_TYPE_MAINS; in cros_usbpd_charger_probe()
626 psy_desc->properties = in cros_usbpd_charger_probe()
628 psy_desc->num_properties = in cros_usbpd_charger_probe()
631 sprintf(port->name, CHARGER_USBPD_DIR_NAME, i); in cros_usbpd_charger_probe()
632 psy_desc->type = POWER_SUPPLY_TYPE_USB; in cros_usbpd_charger_probe()
633 psy_desc->properties = cros_usbpd_charger_props; in cros_usbpd_charger_probe()
634 psy_desc->num_properties = in cros_usbpd_charger_probe()
636 psy_desc->usb_types = BIT(POWER_SUPPLY_USB_TYPE_UNKNOWN) | in cros_usbpd_charger_probe()
646 psy_desc->name = port->name; in cros_usbpd_charger_probe()
653 port->psy = psy; in cros_usbpd_charger_probe()
655 charger->ports[charger->num_registered_psy++] = port; in cros_usbpd_charger_probe()
658 if (!charger->num_registered_psy) { in cros_usbpd_charger_probe()
659 ret = -ENODEV; in cros_usbpd_charger_probe()
665 charger->notifier.notifier_call = cros_usbpd_charger_ec_event; in cros_usbpd_charger_probe()
666 ret = cros_usbpd_register_notify(&charger->notifier); in cros_usbpd_charger_probe()
672 charger); in cros_usbpd_charger_probe()
690 struct charger_data *charger = dev_get_drvdata(dev); in cros_usbpd_charger_resume() local
693 if (!charger) in cros_usbpd_charger_resume()
696 for (i = 0; i < charger->num_registered_psy; i++) { in cros_usbpd_charger_resume()
697 power_supply_changed(charger->ports[i]->psy); in cros_usbpd_charger_resume()
698 charger->ports[i]->last_update = in cros_usbpd_charger_resume()
699 jiffies - CHARGER_CACHE_UPDATE_DELAY; in cros_usbpd_charger_resume()
727 MODULE_DESCRIPTION("ChromeOS EC USBPD charger");