1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
|
// SPDX-License-Identifier: GPL-2.0
/*
* Power Supply for UCSI
*
* Copyright (C) 2020, Intel Corporation
* Author: K V, Abhilash <abhilash.k.v@intel.com>
* Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
*/
#include <linux/property.h>
#include <linux/usb/pd.h>
#include "ucsi.h"
/* Power Supply access to expose source power information */
enum ucsi_psy_online_states {
UCSI_PSY_OFFLINE = 0,
UCSI_PSY_FIXED_ONLINE,
UCSI_PSY_PROG_ONLINE,
};
static enum power_supply_property ucsi_psy_props[] = {
POWER_SUPPLY_PROP_USB_TYPE,
POWER_SUPPLY_PROP_ONLINE,
POWER_SUPPLY_PROP_VOLTAGE_MIN,
POWER_SUPPLY_PROP_VOLTAGE_MAX,
POWER_SUPPLY_PROP_VOLTAGE_NOW,
POWER_SUPPLY_PROP_CURRENT_MAX,
POWER_SUPPLY_PROP_CURRENT_NOW,
POWER_SUPPLY_PROP_SCOPE,
};
static int ucsi_psy_get_scope(struct ucsi_connector *con,
union power_supply_propval *val)
{
u8 scope = POWER_SUPPLY_SCOPE_UNKNOWN;
struct device *dev = con->ucsi->dev;
device_property_read_u8(dev, "scope", &scope);
if (scope == POWER_SUPPLY_SCOPE_UNKNOWN) {
u32 mask = UCSI_CAP_ATTR_POWER_AC_SUPPLY |
UCSI_CAP_ATTR_BATTERY_CHARGING;
if (con->ucsi->cap.attributes & mask)
scope = POWER_SUPPLY_SCOPE_SYSTEM;
else
scope = POWER_SUPPLY_SCOPE_DEVICE;
}
val->intval = scope;
return 0;
}
static int ucsi_psy_get_online(struct ucsi_connector *con,
union power_supply_propval *val)
{
val->intval = UCSI_PSY_OFFLINE;
if (con->status.flags & UCSI_CONSTAT_CONNECTED &&
(con->status.flags & UCSI_CONSTAT_PWR_DIR) == TYPEC_SINK)
val->intval = UCSI_PSY_FIXED_ONLINE;
return 0;
}
static int ucsi_psy_get_voltage_min(struct ucsi_connector *con,
union power_supply_propval *val)
{
u32 pdo;
switch (UCSI_CONSTAT_PWR_OPMODE(con->status.flags)) {
case UCSI_CONSTAT_PWR_OPMODE_PD:
pdo = con->src_pdos[0];
val->intval = pdo_fixed_voltage(pdo) * 1000;
break;
case UCSI_CONSTAT_PWR_OPMODE_TYPEC3_0:
case UCSI_CONSTAT_PWR_OPMODE_TYPEC1_5:
case UCSI_CONSTAT_PWR_OPMODE_BC:
case UCSI_CONSTAT_PWR_OPMODE_DEFAULT:
val->intval = UCSI_TYPEC_VSAFE5V * 1000;
break;
default:
val->intval = 0;
break;
}
return 0;
}
static int ucsi_psy_get_voltage_max(struct ucsi_connector *con,
union power_supply_propval *val)
{
u32 pdo;
switch (UCSI_CONSTAT_PWR_OPMODE(con->status.flags)) {
case UCSI_CONSTAT_PWR_OPMODE_PD:
if (con->num_pdos > 0) {
pdo = con->src_pdos[con->num_pdos - 1];
val->intval = pdo_fixed_voltage(pdo) * 1000;
} else {
val->intval = 0;
}
break;
case UCSI_CONSTAT_PWR_OPMODE_TYPEC3_0:
case UCSI_CONSTAT_PWR_OPMODE_TYPEC1_5:
case UCSI_CONSTAT_PWR_OPMODE_BC:
case UCSI_CONSTAT_PWR_OPMODE_DEFAULT:
val->intval = UCSI_TYPEC_VSAFE5V * 1000;
break;
default:
val->intval = 0;
break;
}
return 0;
}
static int ucsi_psy_get_voltage_now(struct ucsi_connector *con,
union power_supply_propval *val)
{
int index;
u32 pdo;
switch (UCSI_CONSTAT_PWR_OPMODE(con->status.flags)) {
case UCSI_CONSTAT_PWR_OPMODE_PD:
index = rdo_index(con->rdo);
if (index > 0) {
pdo = con->src_pdos[index - 1];
val->intval = pdo_fixed_voltage(pdo) * 1000;
} else {
val->intval = 0;
}
break;
case UCSI_CONSTAT_PWR_OPMODE_TYPEC3_0:
case UCSI_CONSTAT_PWR_OPMODE_TYPEC1_5:
case UCSI_CONSTAT_PWR_OPMODE_BC:
case UCSI_CONSTAT_PWR_OPMODE_DEFAULT:
val->intval = UCSI_TYPEC_VSAFE5V * 1000;
break;
default:
val->intval = 0;
break;
}
return 0;
}
static int ucsi_psy_get_current_max(struct ucsi_connector *con,
union power_supply_propval *val)
{
u32 pdo;
switch (UCSI_CONSTAT_PWR_OPMODE(con->status.flags)) {
case UCSI_CONSTAT_PWR_OPMODE_PD:
if (con->num_pdos > 0) {
pdo = con->src_pdos[con->num_pdos - 1];
val->intval = pdo_max_current(pdo) * 1000;
} else {
val->intval = 0;
}
break;
case UCSI_CONSTAT_PWR_OPMODE_TYPEC1_5:
val->intval = UCSI_TYPEC_1_5_CURRENT * 1000;
break;
case UCSI_CONSTAT_PWR_OPMODE_TYPEC3_0:
val->intval = UCSI_TYPEC_3_0_CURRENT * 1000;
break;
case UCSI_CONSTAT_PWR_OPMODE_BC:
case UCSI_CONSTAT_PWR_OPMODE_DEFAULT:
/* UCSI can't tell b/w DCP/CDP or USB2/3x1/3x2 SDP chargers */
default:
val->intval = 0;
break;
}
return 0;
}
static int ucsi_psy_get_current_now(struct ucsi_connector *con,
union power_supply_propval *val)
{
u16 flags = con->status.flags;
if (UCSI_CONSTAT_PWR_OPMODE(flags) == UCSI_CONSTAT_PWR_OPMODE_PD)
val->intval = rdo_op_current(con->rdo) * 1000;
else
val->intval = 0;
return 0;
}
static int ucsi_psy_get_usb_type(struct ucsi_connector *con,
union power_supply_propval *val)
{
u16 flags = con->status.flags;
val->intval = POWER_SUPPLY_USB_TYPE_C;
if (flags & UCSI_CONSTAT_CONNECTED &&
UCSI_CONSTAT_PWR_OPMODE(flags) == UCSI_CONSTAT_PWR_OPMODE_PD)
val->intval = POWER_SUPPLY_USB_TYPE_PD;
return 0;
}
static int ucsi_psy_get_prop(struct power_supply *psy,
enum power_supply_property psp,
union power_supply_propval *val)
{
struct ucsi_connector *con = power_supply_get_drvdata(psy);
switch (psp) {
case POWER_SUPPLY_PROP_USB_TYPE:
return ucsi_psy_get_usb_type(con, val);
case POWER_SUPPLY_PROP_ONLINE:
return ucsi_psy_get_online(con, val);
case POWER_SUPPLY_PROP_VOLTAGE_MIN:
return ucsi_psy_get_voltage_min(con, val);
case POWER_SUPPLY_PROP_VOLTAGE_MAX:
return ucsi_psy_get_voltage_max(con, val);
case POWER_SUPPLY_PROP_VOLTAGE_NOW:
return ucsi_psy_get_voltage_now(con, val);
case POWER_SUPPLY_PROP_CURRENT_MAX:
return ucsi_psy_get_current_max(con, val);
case POWER_SUPPLY_PROP_CURRENT_NOW:
return ucsi_psy_get_current_now(con, val);
case POWER_SUPPLY_PROP_SCOPE:
return ucsi_psy_get_scope(con, val);
default:
return -EINVAL;
}
}
static enum power_supply_usb_type ucsi_psy_usb_types[] = {
POWER_SUPPLY_USB_TYPE_C,
POWER_SUPPLY_USB_TYPE_PD,
POWER_SUPPLY_USB_TYPE_PD_PPS,
};
int ucsi_register_port_psy(struct ucsi_connector *con)
{
struct power_supply_config psy_cfg = {};
struct device *dev = con->ucsi->dev;
char *psy_name;
psy_cfg.drv_data = con;
psy_cfg.fwnode = dev_fwnode(dev);
psy_name = devm_kasprintf(dev, GFP_KERNEL, "ucsi-source-psy-%s%d",
dev_name(dev), con->num);
if (!psy_name)
return -ENOMEM;
con->psy_desc.name = psy_name;
con->psy_desc.type = POWER_SUPPLY_TYPE_USB;
con->psy_desc.usb_types = ucsi_psy_usb_types;
con->psy_desc.num_usb_types = ARRAY_SIZE(ucsi_psy_usb_types);
con->psy_desc.properties = ucsi_psy_props;
con->psy_desc.num_properties = ARRAY_SIZE(ucsi_psy_props);
con->psy_desc.get_property = ucsi_psy_get_prop;
con->psy = power_supply_register(dev, &con->psy_desc, &psy_cfg);
return PTR_ERR_OR_ZERO(con->psy);
}
void ucsi_unregister_port_psy(struct ucsi_connector *con)
{
if (IS_ERR_OR_NULL(con->psy))
return;
power_supply_unregister(con->psy);
con->psy = NULL;
}
void ucsi_port_psy_changed(struct ucsi_connector *con)
{
if (IS_ERR_OR_NULL(con->psy))
return;
power_supply_changed(con->psy);
}
|