Lines Matching +full:vid +full:- +full:supply
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"
82 return port->port_number >= port->charger->num_usbpd_ports; in cros_usbpd_charger_port_is_dedicated()
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()
146 struct charger_data *charger = port->charger; in cros_usbpd_charger_get_discovery_info()
151 req.port = port->port_number; 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()
167 resp.vid); 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()
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()
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()
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()
285 * If power supply type or status changed, explicitly call in cros_usbpd_charger_get_power_info()
288 * their next poll of power supply status. 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()
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()
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()
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()
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()
487 ret = -EINVAL; in cros_usbpd_charger_set_prop()
517 cros_usbpd_charger_power_changed(charger->ports[0]->psy); in cros_usbpd_charger_ec_event()
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()
537 int ret = -EINVAL; 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()
556 charger->num_usbpd_ports = in cros_usbpd_charger_probe()
558 if (charger->num_usbpd_ports <= 0) { 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()
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()
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()
623 sprintf(port->name, CHARGER_DEDICATED_DIR_NAME); in cros_usbpd_charger_probe()
624 psy_desc->type = POWER_SUPPLY_TYPE_MAINS; in cros_usbpd_charger_probe()
625 psy_desc->properties = in cros_usbpd_charger_probe()
627 psy_desc->num_properties = in cros_usbpd_charger_probe()
630 sprintf(port->name, CHARGER_USBPD_DIR_NAME, i); in cros_usbpd_charger_probe()
631 psy_desc->type = POWER_SUPPLY_TYPE_USB; in cros_usbpd_charger_probe()
632 psy_desc->properties = cros_usbpd_charger_props; in cros_usbpd_charger_probe()
633 psy_desc->num_properties = in cros_usbpd_charger_probe()
635 psy_desc->usb_types = BIT(POWER_SUPPLY_USB_TYPE_UNKNOWN) | in cros_usbpd_charger_probe()
645 psy_desc->name = port->name; in cros_usbpd_charger_probe()
650 dev_err(dev, "Failed to register power supply\n"); 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()
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()