struct port_data { int port_number; char name[CHARGER_DIR_NAME_LENGTH]; char manufacturer[CHARGER_MANUFACTURER_MODEL_LENGTH]; char model_name[CHARGER_MANUFACTURER_MODEL_LENGTH]; struct power_supply *psy; struct power_supply_desc psy_desc; int psy_usb_type; int psy_online; int psy_status; int psy_current_max; int psy_voltage_max_design; int psy_voltage_now; int psy_power_max; struct charger_data *charger; unsignedlong last_update;
};
struct charger_data { struct device *dev; struct cros_ec_dev *ec_dev; struct cros_ec_device *ec_device; int num_charger_ports; int num_usbpd_ports; int num_registered_psy; struct port_data *ports[EC_USB_PD_MAX_PORTS]; struct notifier_block notifier;
};
switch (resp.type) { case USB_CHG_TYPE_BC12_SDP: case USB_CHG_TYPE_VBUS:
port->psy_usb_type = POWER_SUPPLY_USB_TYPE_SDP; break; case USB_CHG_TYPE_NONE: /* * For dual-role devices when we are a source, the firmware * reports the type as NONE. Report such chargers as type * USB_PD_DRP.
*/ if (resp.role == USB_PD_PORT_POWER_SOURCE && resp.dualrole)
port->psy_usb_type = POWER_SUPPLY_USB_TYPE_PD_DRP; else
port->psy_usb_type = POWER_SUPPLY_USB_TYPE_SDP; break; case USB_CHG_TYPE_OTHER: case USB_CHG_TYPE_PROPRIETARY:
port->psy_usb_type = POWER_SUPPLY_USB_TYPE_APPLE_BRICK_ID; break; case USB_CHG_TYPE_C:
port->psy_usb_type = POWER_SUPPLY_USB_TYPE_C; break; case USB_CHG_TYPE_BC12_DCP:
port->psy_usb_type = POWER_SUPPLY_USB_TYPE_DCP; break; case USB_CHG_TYPE_BC12_CDP:
port->psy_usb_type = POWER_SUPPLY_USB_TYPE_CDP; break; case USB_CHG_TYPE_PD: if (resp.dualrole)
port->psy_usb_type = POWER_SUPPLY_USB_TYPE_PD_DRP; else
port->psy_usb_type = POWER_SUPPLY_USB_TYPE_PD; break; case USB_CHG_TYPE_UNKNOWN: /* * While the EC is trying to determine the type of charger that * has been plugged in, it will report the charger type as * unknown. Additionally since the power capabilities are * unknown, report the max current and voltage as zero.
*/
port->psy_usb_type = POWER_SUPPLY_USB_TYPE_UNKNOWN;
port->psy_voltage_max_design = 0;
port->psy_current_max = 0; break; default:
dev_dbg(dev, "Port %d: default case!\n", port->port_number);
port->psy_usb_type = POWER_SUPPLY_USB_TYPE_SDP;
}
if (cros_usbpd_charger_port_is_dedicated(port))
port->psy_desc.type = POWER_SUPPLY_TYPE_MAINS; else
port->psy_desc.type = POWER_SUPPLY_TYPE_USB;
/* * If power supply type or status changed, explicitly call * power_supply_changed. This results in udev event getting generated * and allows user mode apps to react quicker instead of waiting for * their next poll of power supply status.
*/ if (last_psy_usb_type != port->psy_usb_type ||
last_psy_status != port->psy_status)
power_supply_changed(port->psy);
return 0;
}
staticint cros_usbpd_charger_get_port_status(struct port_data *port, bool ratelimit)
{ int ret;
if (ratelimit &&
time_is_after_jiffies(port->last_update +
CHARGER_CACHE_UPDATE_DELAY)) return 0;
ret = cros_usbpd_charger_get_power_info(port); if (ret < 0) return ret;
if (!cros_usbpd_charger_port_is_dedicated(port))
ret = cros_usbpd_charger_get_discovery_info(port);
port->last_update = jiffies;
ret = cros_usbpd_charger_ec_command(charger, 0,
EC_CMD_EXTERNAL_POWER_LIMIT,
&req, sizeof(req), NULL, 0); if (ret < 0)
dev_err(charger->dev, "Unable to set the 'External Power Limit': %d\n", ret);
/* Only refresh ec_port_status for dynamic properties */ switch (psp) { case POWER_SUPPLY_PROP_ONLINE: /* * If mkbp_event_supported, then we can be assured that * the driver's state for the online property is consistent * with the hardware. However, if we aren't event driven, * the optimization before to skip an ec_port_status get * and only returned cached values of the online property will * cause a delay in detecting a cable attach until one of the * other properties are read. * * Allow an ec_port_status refresh for online property check * if we're not already online to check for plug events if * not mkbp_event_supported.
*/ if (ec_device->mkbp_event_supported || port->psy_online) break;
fallthrough; case POWER_SUPPLY_PROP_CURRENT_MAX: case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN: case POWER_SUPPLY_PROP_VOLTAGE_NOW:
ret = cros_usbpd_charger_get_port_status(port, true); if (ret < 0) {
dev_err(dev, "Failed to get port status (err:0x%x)\n",
ret); return -EINVAL;
} break; default: break;
}
switch (psp) { case POWER_SUPPLY_PROP_ONLINE:
val->intval = port->psy_online; break; case POWER_SUPPLY_PROP_STATUS:
val->intval = port->psy_status; break; case POWER_SUPPLY_PROP_CURRENT_MAX:
val->intval = port->psy_current_max * 1000; break; case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
val->intval = port->psy_voltage_max_design * 1000; break; case POWER_SUPPLY_PROP_VOLTAGE_NOW:
val->intval = port->psy_voltage_now * 1000; break; case POWER_SUPPLY_PROP_USB_TYPE:
val->intval = port->psy_usb_type; break; case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT: if (input_current_limit == EC_POWER_LIMIT_NONE)
val->intval = -1; else
val->intval = input_current_limit * 1000; break; case POWER_SUPPLY_PROP_INPUT_VOLTAGE_LIMIT: if (input_voltage_limit == EC_POWER_LIMIT_NONE)
val->intval = -1; else
val->intval = input_voltage_limit * 1000; break; case POWER_SUPPLY_PROP_MODEL_NAME:
val->strval = port->model_name; break; case POWER_SUPPLY_PROP_MANUFACTURER:
val->strval = port->manufacturer; break; default: return -EINVAL;
}
/* U16_MAX in mV/mA is the maximum supported value */ if (val->intval >= U16_MAX * 1000) return -EINVAL; /* A negative number is used to clear the limit */ if (val->intval < 0)
intval = EC_POWER_LIMIT_NONE; else/* Convert from uA/uV to mA/mV */
intval = val->intval / 1000;
switch (psp) { case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
ret = cros_usbpd_charger_set_ext_power_limit(charger, intval,
input_voltage_limit); if (ret < 0) break;
input_current_limit = intval; if (input_current_limit == EC_POWER_LIMIT_NONE)
dev_info(dev, "External Current Limit cleared for all ports\n"); else
dev_info(dev, "External Current Limit set to %dmA for all ports\n",
input_current_limit); break; case POWER_SUPPLY_PROP_INPUT_VOLTAGE_LIMIT:
ret = cros_usbpd_charger_set_ext_power_limit(charger,
input_current_limit,
intval); if (ret < 0) break;
input_voltage_limit = intval; if (input_voltage_limit == EC_POWER_LIMIT_NONE)
dev_info(dev, "External Voltage Limit cleared for all ports\n"); else
dev_info(dev, "External Voltage Limit set to %dmV for all ports\n",
input_voltage_limit); break; default:
ret = -EINVAL;
}
return ret;
}
staticint cros_usbpd_charger_property_is_writeable(struct power_supply *psy, enum power_supply_property psp)
{ int ret;
switch (psp) { case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT: case POWER_SUPPLY_PROP_INPUT_VOLTAGE_LIMIT:
ret = 1; break; default:
ret = 0;
}
/* * We need to know the number of USB PD ports in order to know whether * there is a dedicated port. The dedicated port will always be * after the USB PD ports, and there should be only one.
*/
charger->num_usbpd_ports =
cros_usbpd_charger_get_usbpd_num_ports(charger); if (charger->num_usbpd_ports <= 0) { /* * This can happen on a system that doesn't support USB PD. * Log a message, but no need to warn.
*/
dev_info(dev, "No USB PD charging ports found\n");
}
charger->num_charger_ports = cros_usbpd_charger_get_num_ports(charger); if (charger->num_charger_ports < 0) { /* * This can happen on a system that doesn't support USB PD. * Log a message, but no need to warn. * Older ECs do not support the above command, in that case * let's set up the number of charger ports equal to the number * of USB PD ports
*/
dev_info(dev, "Could not get charger port count\n");
charger->num_charger_ports = charger->num_usbpd_ports;
}
if (charger->num_charger_ports <= 0) { /* * This can happen on a system that doesn't support USB PD and * doesn't have a dedicated port. * Log a message, but no need to warn.
*/
dev_info(dev, "No charging ports found\n");
ret = -ENODEV; goto fail_nowarn;
}
/* * Sanity checks on the number of ports: * there should be at most 1 dedicated port
*/ if (charger->num_charger_ports < charger->num_usbpd_ports ||
charger->num_charger_ports > (charger->num_usbpd_ports + 1)) {
dev_err(dev, "Unexpected number of charge port count\n");
ret = -EPROTO; goto fail_nowarn;
}
for (i = 0; i < charger->num_charger_ports; i++) { struct power_supply_config psy_cfg = {};
port = devm_kzalloc(dev, sizeof(struct port_data), GFP_KERNEL); if (!port) {
ret = -ENOMEM; goto fail;
}
if (!charger->num_registered_psy) {
ret = -ENODEV;
dev_err(dev, "No power supplies registered\n"); goto fail;
}
/* Get PD events from the EC */
charger->notifier.notifier_call = cros_usbpd_charger_ec_event;
ret = cros_usbpd_register_notify(&charger->notifier); if (ret < 0) {
dev_warn(dev, "failed to register notifier\n");
} else {
ret = devm_add_action_or_reset(dev,
cros_usbpd_charger_unregister_notifier,
charger); if (ret < 0) goto fail;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.