| 1 |
// SPDX-License-Identifier: GPL-2.0 |
| 2 |
/* |
| 3 |
* Power Supply for UCSI |
| 4 |
* |
| 5 |
* Copyright (C) 2020, Intel Corporation |
| 6 |
* Author: K V, Abhilash <[email protected]> |
| 7 |
* Author: Heikki Krogerus <[email protected]> |
| 8 |
*/ |
| 9 |
|
| 10 |
#include <linux/property.h> |
| 11 |
#include <linux/usb/pd.h> |
| 12 |
|
| 13 |
#include "ucsi.h" |
| 14 |
|
| 15 |
/* Power Supply access to expose source power information */ |
| 16 |
enum ucsi_psy_online_states { |
| 17 |
UCSI_PSY_OFFLINE = 0, |
| 18 |
UCSI_PSY_FIXED_ONLINE, |
| 19 |
UCSI_PSY_PROG_ONLINE, |
| 20 |
}; |
| 21 |
|
| 22 |
static enum power_supply_property ucsi_psy_props[] = { |
| 23 |
POWER_SUPPLY_PROP_CHARGE_TYPE, |
| 24 |
POWER_SUPPLY_PROP_USB_TYPE, |
| 25 |
POWER_SUPPLY_PROP_ONLINE, |
| 26 |
POWER_SUPPLY_PROP_VOLTAGE_MIN, |
| 27 |
POWER_SUPPLY_PROP_VOLTAGE_MAX, |
| 28 |
POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| 29 |
POWER_SUPPLY_PROP_CURRENT_MAX, |
| 30 |
POWER_SUPPLY_PROP_CURRENT_NOW, |
| 31 |
POWER_SUPPLY_PROP_SCOPE, |
| 32 |
POWER_SUPPLY_PROP_STATUS, |
| 33 |
}; |
| 34 |
|
| 35 |
static int ucsi_psy_get_scope(struct ucsi_connector *con, |
| 36 |
union power_supply_propval *val) |
| 37 |
{ |
| 38 |
u8 scope = POWER_SUPPLY_SCOPE_UNKNOWN; |
| 39 |
struct device *dev = con->ucsi->dev; |
| 40 |
|
| 41 |
device_property_read_u8(dev, "scope", &scope); |
| 42 |
if (scope == POWER_SUPPLY_SCOPE_UNKNOWN) { |
| 43 |
u32 mask = UCSI_CAP_ATTR_POWER_AC_SUPPLY | |
| 44 |
UCSI_CAP_ATTR_BATTERY_CHARGING; |
| 45 |
|
| 46 |
if (con->ucsi->cap.attributes & mask) |
| 47 |
scope = POWER_SUPPLY_SCOPE_SYSTEM; |
| 48 |
else |
| 49 |
scope = POWER_SUPPLY_SCOPE_DEVICE; |
| 50 |
} |
| 51 |
val->intval = scope; |
| 52 |
return 0; |
| 53 |
} |
| 54 |
|
| 55 |
static int ucsi_psy_get_status(struct ucsi_connector *con, |
| 56 |
union power_supply_propval *val) |
| 57 |
{ |
| 58 |
bool is_sink = UCSI_CONSTAT(con, PWR_DIR) == TYPEC_SINK; |
| 59 |
bool sink_path_enabled = true; |
| 60 |
|
| 61 |
val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING; |
| 62 |
|
| 63 |
if (con->ucsi->version >= UCSI_VERSION_2_0) |
| 64 |
sink_path_enabled = |
| 65 |
UCSI_CONSTAT(con, SINK_PATH_STATUS_V2_0) == |
| 66 |
UCSI_CONSTAT_SINK_PATH_ENABLED; |
| 67 |
|
| 68 |
if (UCSI_CONSTAT(con, CONNECTED)) { |
| 69 |
if (is_sink && sink_path_enabled) |
| 70 |
val->intval = POWER_SUPPLY_STATUS_CHARGING; |
| 71 |
else if (!is_sink) |
| 72 |
val->intval = POWER_SUPPLY_STATUS_DISCHARGING; |
| 73 |
} |
| 74 |
|
| 75 |
return 0; |
| 76 |
} |
| 77 |
|
| 78 |
static int ucsi_psy_get_online(struct ucsi_connector *con, |
| 79 |
union power_supply_propval *val) |
| 80 |
{ |
| 81 |
val->intval = UCSI_PSY_OFFLINE; |
| 82 |
if (UCSI_CONSTAT(con, CONNECTED) && |
| 83 |
(UCSI_CONSTAT(con, PWR_DIR) == TYPEC_SINK)) |
| 84 |
val->intval = UCSI_PSY_FIXED_ONLINE; |
| 85 |
return 0; |
| 86 |
} |
| 87 |
|
| 88 |
static int ucsi_psy_get_voltage_min(struct ucsi_connector *con, |
| 89 |
union power_supply_propval *val) |
| 90 |
{ |
| 91 |
u32 pdo; |
| 92 |
|
| 93 |
switch (UCSI_CONSTAT(con, PWR_OPMODE)) { |
| 94 |
case UCSI_CONSTAT_PWR_OPMODE_PD: |
| 95 |
pdo = con->src_pdos[0]; |
| 96 |
val->intval = pdo_fixed_voltage(pdo) * 1000; |
| 97 |
break; |
| 98 |
case UCSI_CONSTAT_PWR_OPMODE_TYPEC3_0: |
| 99 |
case UCSI_CONSTAT_PWR_OPMODE_TYPEC1_5: |
| 100 |
case UCSI_CONSTAT_PWR_OPMODE_BC: |
| 101 |
case UCSI_CONSTAT_PWR_OPMODE_DEFAULT: |
| 102 |
val->intval = UCSI_TYPEC_VSAFE5V * 1000; |
| 103 |
break; |
| 104 |
default: |
| 105 |
val->intval = 0; |
| 106 |
break; |
| 107 |
} |
| 108 |
return 0; |
| 109 |
} |
| 110 |
|
| 111 |
static int ucsi_psy_get_voltage_max(struct ucsi_connector *con, |
| 112 |
union power_supply_propval *val) |
| 113 |
{ |
| 114 |
u32 pdo; |
| 115 |
int max_voltage = 0; |
| 116 |
|
| 117 |
switch (UCSI_CONSTAT(con, PWR_OPMODE)) { |
| 118 |
case UCSI_CONSTAT_PWR_OPMODE_PD: |
| 119 |
for (int i = 0; i < con->num_pdos; i++) { |
| 120 |
int pdo_voltage = 0; |
| 121 |
|
| 122 |
pdo = con->src_pdos[i]; |
| 123 |
if (pdo_type(pdo) == PDO_TYPE_FIXED) |
| 124 |
pdo_voltage = pdo_fixed_voltage(pdo) * 1000; |
| 125 |
max_voltage = (pdo_voltage > max_voltage) ? pdo_voltage |
| 126 |
: max_voltage; |
| 127 |
} |
| 128 |
val->intval = max_voltage; |
| 129 |
break; |
| 130 |
case UCSI_CONSTAT_PWR_OPMODE_TYPEC3_0: |
| 131 |
case UCSI_CONSTAT_PWR_OPMODE_TYPEC1_5: |
| 132 |
case UCSI_CONSTAT_PWR_OPMODE_BC: |
| 133 |
case UCSI_CONSTAT_PWR_OPMODE_DEFAULT: |
| 134 |
val->intval = UCSI_TYPEC_VSAFE5V * 1000; |
| 135 |
break; |
| 136 |
default: |
| 137 |
val->intval = 0; |
| 138 |
break; |
| 139 |
} |
| 140 |
return 0; |
| 141 |
} |
| 142 |
|
| 143 |
static int ucsi_psy_get_voltage_now(struct ucsi_connector *con, |
| 144 |
union power_supply_propval *val) |
| 145 |
{ |
| 146 |
int index; |
| 147 |
u32 pdo; |
| 148 |
|
| 149 |
switch (UCSI_CONSTAT(con, PWR_OPMODE)) { |
| 150 |
case UCSI_CONSTAT_PWR_OPMODE_PD: |
| 151 |
index = rdo_index(con->rdo); |
| 152 |
if (index > 0) { |
| 153 |
pdo = con->src_pdos[index - 1]; |
| 154 |
val->intval = pdo_fixed_voltage(pdo) * 1000; |
| 155 |
} else { |
| 156 |
val->intval = 0; |
| 157 |
} |
| 158 |
break; |
| 159 |
case UCSI_CONSTAT_PWR_OPMODE_TYPEC3_0: |
| 160 |
case UCSI_CONSTAT_PWR_OPMODE_TYPEC1_5: |
| 161 |
case UCSI_CONSTAT_PWR_OPMODE_BC: |
| 162 |
case UCSI_CONSTAT_PWR_OPMODE_DEFAULT: |
| 163 |
val->intval = UCSI_TYPEC_VSAFE5V * 1000; |
| 164 |
break; |
| 165 |
default: |
| 166 |
val->intval = 0; |
| 167 |
break; |
| 168 |
} |
| 169 |
return 0; |
| 170 |
} |
| 171 |
|
| 172 |
static int ucsi_psy_get_current_max(struct ucsi_connector *con, |
| 173 |
union power_supply_propval *val) |
| 174 |
{ |
| 175 |
u32 pdo; |
| 176 |
int max_current = 0; |
| 177 |
|
| 178 |
if (!UCSI_CONSTAT(con, CONNECTED)) { |
| 179 |
val->intval = 0; |
| 180 |
return 0; |
| 181 |
} |
| 182 |
|
| 183 |
switch (UCSI_CONSTAT(con, PWR_OPMODE)) { |
| 184 |
case UCSI_CONSTAT_PWR_OPMODE_PD: |
| 185 |
for (int i = 0; i < con->num_pdos; i++) { |
| 186 |
int pdo_current = 0; |
| 187 |
|
| 188 |
pdo = con->src_pdos[i]; |
| 189 |
if (pdo_type(pdo) == PDO_TYPE_FIXED) |
| 190 |
pdo_current = pdo_max_current(pdo) * 1000; |
| 191 |
max_current = (pdo_current > max_current) ? pdo_current |
| 192 |
: max_current; |
| 193 |
} |
| 194 |
val->intval = max_current; |
| 195 |
break; |
| 196 |
case UCSI_CONSTAT_PWR_OPMODE_TYPEC1_5: |
| 197 |
val->intval = UCSI_TYPEC_1_5_CURRENT * 1000; |
| 198 |
break; |
| 199 |
case UCSI_CONSTAT_PWR_OPMODE_TYPEC3_0: |
| 200 |
val->intval = UCSI_TYPEC_3_0_CURRENT * 1000; |
| 201 |
break; |
| 202 |
case UCSI_CONSTAT_PWR_OPMODE_BC: |
| 203 |
case UCSI_CONSTAT_PWR_OPMODE_DEFAULT: |
| 204 |
/* UCSI can't tell b/w DCP/CDP or USB2/3x1/3x2 SDP chargers */ |
| 205 |
default: |
| 206 |
val->intval = UCSI_TYPEC_DEFAULT_CURRENT * 1000; |
| 207 |
break; |
| 208 |
} |
| 209 |
return 0; |
| 210 |
} |
| 211 |
|
| 212 |
static int ucsi_psy_get_current_now(struct ucsi_connector *con, |
| 213 |
union power_supply_propval *val) |
| 214 |
{ |
| 215 |
if (!UCSI_CONSTAT(con, CONNECTED)) { |
| 216 |
val->intval = 0; |
| 217 |
return 0; |
| 218 |
} |
| 219 |
|
| 220 |
switch (UCSI_CONSTAT(con, PWR_OPMODE)) { |
| 221 |
case UCSI_CONSTAT_PWR_OPMODE_PD: |
| 222 |
val->intval = rdo_op_current(con->rdo) * 1000; |
| 223 |
break; |
| 224 |
case UCSI_CONSTAT_PWR_OPMODE_TYPEC1_5: |
| 225 |
val->intval = UCSI_TYPEC_1_5_CURRENT * 1000; |
| 226 |
break; |
| 227 |
case UCSI_CONSTAT_PWR_OPMODE_TYPEC3_0: |
| 228 |
val->intval = UCSI_TYPEC_3_0_CURRENT * 1000; |
| 229 |
break; |
| 230 |
case UCSI_CONSTAT_PWR_OPMODE_BC: |
| 231 |
case UCSI_CONSTAT_PWR_OPMODE_DEFAULT: |
| 232 |
/* UCSI can't tell b/w DCP/CDP or USB2/3x1/3x2 SDP chargers */ |
| 233 |
default: |
| 234 |
val->intval = UCSI_TYPEC_DEFAULT_CURRENT * 1000; |
| 235 |
break; |
| 236 |
} |
| 237 |
return 0; |
| 238 |
} |
| 239 |
|
| 240 |
static int ucsi_psy_get_usb_type(struct ucsi_connector *con, |
| 241 |
union power_supply_propval *val) |
| 242 |
{ |
| 243 |
val->intval = POWER_SUPPLY_USB_TYPE_C; |
| 244 |
if (UCSI_CONSTAT(con, CONNECTED) && |
| 245 |
UCSI_CONSTAT(con, PWR_OPMODE) == UCSI_CONSTAT_PWR_OPMODE_PD) |
| 246 |
val->intval = POWER_SUPPLY_USB_TYPE_PD; |
| 247 |
|
| 248 |
return 0; |
| 249 |
} |
| 250 |
|
| 251 |
static int ucsi_psy_get_charge_type(struct ucsi_connector *con, union power_supply_propval *val) |
| 252 |
{ |
| 253 |
if (!(UCSI_CONSTAT(con, CONNECTED))) { |
| 254 |
val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE; |
| 255 |
return 0; |
| 256 |
} |
| 257 |
|
| 258 |
/* The Battery Charging Cabability Status field is only valid in sink role. */ |
| 259 |
if (UCSI_CONSTAT(con, PWR_DIR) != TYPEC_SINK) { |
| 260 |
val->intval = POWER_SUPPLY_CHARGE_TYPE_UNKNOWN; |
| 261 |
return 0; |
| 262 |
} |
| 263 |
|
| 264 |
switch (UCSI_CONSTAT(con, BC_STATUS)) { |
| 265 |
case UCSI_CONSTAT_BC_NOMINAL_CHARGING: |
| 266 |
val->intval = POWER_SUPPLY_CHARGE_TYPE_STANDARD; |
| 267 |
break; |
| 268 |
case UCSI_CONSTAT_BC_SLOW_CHARGING: |
| 269 |
case UCSI_CONSTAT_BC_TRICKLE_CHARGING: |
| 270 |
val->intval = POWER_SUPPLY_CHARGE_TYPE_TRICKLE; |
| 271 |
break; |
| 272 |
default: |
| 273 |
val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE; |
| 274 |
break; |
| 275 |
} |
| 276 |
|
| 277 |
return 0; |
| 278 |
} |
| 279 |
|
| 280 |
static int ucsi_psy_get_prop(struct power_supply *psy, |
| 281 |
enum power_supply_property psp, |
| 282 |
union power_supply_propval *val) |
| 283 |
{ |
| 284 |
struct ucsi_connector *con = power_supply_get_drvdata(psy); |
| 285 |
|
| 286 |
switch (psp) { |
| 287 |
case POWER_SUPPLY_PROP_CHARGE_TYPE: |
| 288 |
return ucsi_psy_get_charge_type(con, val); |
| 289 |
case POWER_SUPPLY_PROP_USB_TYPE: |
| 290 |
return ucsi_psy_get_usb_type(con, val); |
| 291 |
case POWER_SUPPLY_PROP_ONLINE: |
| 292 |
return ucsi_psy_get_online(con, val); |
| 293 |
case POWER_SUPPLY_PROP_VOLTAGE_MIN: |
| 294 |
return ucsi_psy_get_voltage_min(con, val); |
| 295 |
case POWER_SUPPLY_PROP_VOLTAGE_MAX: |
| 296 |
return ucsi_psy_get_voltage_max(con, val); |
| 297 |
case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
| 298 |
return ucsi_psy_get_voltage_now(con, val); |
| 299 |
case POWER_SUPPLY_PROP_CURRENT_MAX: |
| 300 |
return ucsi_psy_get_current_max(con, val); |
| 301 |
case POWER_SUPPLY_PROP_CURRENT_NOW: |
| 302 |
return ucsi_psy_get_current_now(con, val); |
| 303 |
case POWER_SUPPLY_PROP_SCOPE: |
| 304 |
return ucsi_psy_get_scope(con, val); |
| 305 |
case POWER_SUPPLY_PROP_STATUS: |
| 306 |
return ucsi_psy_get_status(con, val); |
| 307 |
default: |
| 308 |
return -EINVAL; |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
int ucsi_register_port_psy(struct ucsi_connector *con) |
| 313 |
{ |
| 314 |
struct power_supply_config psy_cfg = {}; |
| 315 |
struct device *dev = con->ucsi->dev; |
| 316 |
char *psy_name; |
| 317 |
|
| 318 |
psy_cfg.drv_data = con; |
| 319 |
psy_cfg.fwnode = dev_fwnode(dev); |
| 320 |
|
| 321 |
psy_name = devm_kasprintf(dev, GFP_KERNEL, "ucsi-source-psy-%s%d", |
| 322 |
dev_name(dev), con->num); |
| 323 |
if (!psy_name) |
| 324 |
return -ENOMEM; |
| 325 |
|
| 326 |
con->psy_desc.name = psy_name; |
| 327 |
con->psy_desc.type = POWER_SUPPLY_TYPE_USB; |
| 328 |
con->psy_desc.usb_types = BIT(POWER_SUPPLY_USB_TYPE_C) | |
| 329 |
BIT(POWER_SUPPLY_USB_TYPE_PD) | |
| 330 |
BIT(POWER_SUPPLY_USB_TYPE_PD_PPS); |
| 331 |
con->psy_desc.properties = ucsi_psy_props; |
| 332 |
con->psy_desc.num_properties = ARRAY_SIZE(ucsi_psy_props); |
| 333 |
con->psy_desc.get_property = ucsi_psy_get_prop; |
| 334 |
|
| 335 |
con->psy = power_supply_register(dev, &con->psy_desc, &psy_cfg); |
| 336 |
|
| 337 |
return PTR_ERR_OR_ZERO(con->psy); |
| 338 |
} |
| 339 |
|
| 340 |
void ucsi_unregister_port_psy(struct ucsi_connector *con) |
| 341 |
{ |
| 342 |
if (IS_ERR_OR_NULL(con->psy)) |
| 343 |
return; |
| 344 |
|
| 345 |
power_supply_unregister(con->psy); |
| 346 |
con->psy = NULL; |
| 347 |
} |
| 348 |
|
| 349 |
void ucsi_port_psy_changed(struct ucsi_connector *con) |
| 350 |
{ |
| 351 |
if (IS_ERR_OR_NULL(con->psy)) |
| 352 |
return; |
| 353 |
|
| 354 |
power_supply_changed(con->psy); |
| 355 |
} |