| 1 |
// SPDX-License-Identifier: GPL-2.0+ |
| 2 |
/* |
| 3 |
* Driver for UCS1002 Programmable USB Port Power Controller |
| 4 |
* |
| 5 |
* Copyright (C) 2019 Zodiac Inflight Innovations |
| 6 |
*/ |
| 7 |
#include <linux/bits.h> |
| 8 |
#include <linux/freezer.h> |
| 9 |
#include <linux/i2c.h> |
| 10 |
#include <linux/interrupt.h> |
| 11 |
#include <linux/kernel.h> |
| 12 |
#include <linux/kthread.h> |
| 13 |
#include <linux/device.h> |
| 14 |
#include <linux/module.h> |
| 15 |
#include <linux/of.h> |
| 16 |
#include <linux/of_irq.h> |
| 17 |
#include <linux/power_supply.h> |
| 18 |
#include <linux/regmap.h> |
| 19 |
#include <linux/regulator/driver.h> |
| 20 |
#include <linux/regulator/of_regulator.h> |
| 21 |
|
| 22 |
/* UCS1002 Registers */ |
| 23 |
#define UCS1002_REG_CURRENT_MEASUREMENT 0x00 |
| 24 |
|
| 25 |
/* |
| 26 |
* The Total Accumulated Charge registers store the total accumulated |
| 27 |
* charge delivered from the VS source to a portable device. The total |
| 28 |
* value is calculated using four registers, from 01h to 04h. The bit |
| 29 |
* weighting of the registers is given in mA/hrs. |
| 30 |
*/ |
| 31 |
#define UCS1002_REG_TOTAL_ACC_CHARGE 0x01 |
| 32 |
|
| 33 |
/* Other Status Register */ |
| 34 |
#define UCS1002_REG_OTHER_STATUS 0x0f |
| 35 |
# define F_ADET_PIN BIT(4) |
| 36 |
# define F_CHG_ACT BIT(3) |
| 37 |
|
| 38 |
/* Interrupt Status */ |
| 39 |
#define UCS1002_REG_INTERRUPT_STATUS 0x10 |
| 40 |
# define F_ERR BIT(7) |
| 41 |
# define F_DISCHARGE_ERR BIT(6) |
| 42 |
# define F_RESET BIT(5) |
| 43 |
# define F_MIN_KEEP_OUT BIT(4) |
| 44 |
# define F_TSD BIT(3) |
| 45 |
# define F_OVER_VOLT BIT(2) |
| 46 |
# define F_BACK_VOLT BIT(1) |
| 47 |
# define F_OVER_ILIM BIT(0) |
| 48 |
|
| 49 |
/* Pin Status Register */ |
| 50 |
#define UCS1002_REG_PIN_STATUS 0x14 |
| 51 |
# define UCS1002_PWR_STATE_MASK 0x03 |
| 52 |
# define F_PWR_EN_PIN BIT(6) |
| 53 |
# define F_M2_PIN BIT(5) |
| 54 |
# define F_M1_PIN BIT(4) |
| 55 |
# define F_EM_EN_PIN BIT(3) |
| 56 |
# define F_SEL_PIN BIT(2) |
| 57 |
# define F_ACTIVE_MODE_MASK GENMASK(5, 3) |
| 58 |
# define F_ACTIVE_MODE_PASSTHROUGH F_M2_PIN |
| 59 |
# define F_ACTIVE_MODE_DEDICATED F_EM_EN_PIN |
| 60 |
# define F_ACTIVE_MODE_BC12_DCP (F_M2_PIN | F_EM_EN_PIN) |
| 61 |
# define F_ACTIVE_MODE_BC12_SDP F_M1_PIN |
| 62 |
# define F_ACTIVE_MODE_BC12_CDP (F_M1_PIN | F_M2_PIN | F_EM_EN_PIN) |
| 63 |
|
| 64 |
/* General Configuration Register */ |
| 65 |
#define UCS1002_REG_GENERAL_CFG 0x15 |
| 66 |
# define F_RATION_EN BIT(3) |
| 67 |
|
| 68 |
/* Emulation Configuration Register */ |
| 69 |
#define UCS1002_REG_EMU_CFG 0x16 |
| 70 |
|
| 71 |
/* Switch Configuration Register */ |
| 72 |
#define UCS1002_REG_SWITCH_CFG 0x17 |
| 73 |
# define F_PIN_IGNORE BIT(7) |
| 74 |
# define F_EM_EN_SET BIT(5) |
| 75 |
# define F_M2_SET BIT(4) |
| 76 |
# define F_M1_SET BIT(3) |
| 77 |
# define F_S0_SET BIT(2) |
| 78 |
# define F_PWR_EN_SET BIT(1) |
| 79 |
# define F_LATCH_SET BIT(0) |
| 80 |
# define V_SET_ACTIVE_MODE_MASK GENMASK(5, 3) |
| 81 |
# define V_SET_ACTIVE_MODE_PASSTHROUGH F_M2_SET |
| 82 |
# define V_SET_ACTIVE_MODE_DEDICATED F_EM_EN_SET |
| 83 |
# define V_SET_ACTIVE_MODE_BC12_DCP (F_M2_SET | F_EM_EN_SET) |
| 84 |
# define V_SET_ACTIVE_MODE_BC12_SDP F_M1_SET |
| 85 |
# define V_SET_ACTIVE_MODE_BC12_CDP (F_M1_SET | F_M2_SET | F_EM_EN_SET) |
| 86 |
|
| 87 |
/* Current Limit Register */ |
| 88 |
#define UCS1002_REG_ILIMIT 0x19 |
| 89 |
# define UCS1002_ILIM_SW_MASK GENMASK(3, 0) |
| 90 |
|
| 91 |
/* Product ID */ |
| 92 |
#define UCS1002_REG_PRODUCT_ID 0xfd |
| 93 |
# define UCS1002_PRODUCT_ID 0x4e |
| 94 |
|
| 95 |
/* Manufacture name */ |
| 96 |
#define UCS1002_MANUFACTURER "SMSC" |
| 97 |
|
| 98 |
struct ucs1002_info { |
| 99 |
struct power_supply *charger; |
| 100 |
struct i2c_client *client; |
| 101 |
struct regmap *regmap; |
| 102 |
struct regulator_desc *regulator_descriptor; |
| 103 |
struct regulator_dev *rdev; |
| 104 |
bool present; |
| 105 |
bool output_disable; |
| 106 |
struct delayed_work health_poll; |
| 107 |
int health; |
| 108 |
|
| 109 |
}; |
| 110 |
|
| 111 |
static enum power_supply_property ucs1002_props[] = { |
| 112 |
POWER_SUPPLY_PROP_ONLINE, |
| 113 |
POWER_SUPPLY_PROP_CHARGE_NOW, |
| 114 |
POWER_SUPPLY_PROP_CURRENT_NOW, |
| 115 |
POWER_SUPPLY_PROP_CURRENT_MAX, |
| 116 |
POWER_SUPPLY_PROP_PRESENT, /* the presence of PED */ |
| 117 |
POWER_SUPPLY_PROP_MANUFACTURER, |
| 118 |
POWER_SUPPLY_PROP_USB_TYPE, |
| 119 |
POWER_SUPPLY_PROP_HEALTH, |
| 120 |
}; |
| 121 |
|
| 122 |
static int ucs1002_get_online(struct ucs1002_info *info, |
| 123 |
union power_supply_propval *val) |
| 124 |
{ |
| 125 |
unsigned int reg; |
| 126 |
int ret; |
| 127 |
|
| 128 |
ret = regmap_read(info->regmap, UCS1002_REG_OTHER_STATUS, ®); |
| 129 |
if (ret) |
| 130 |
return ret; |
| 131 |
|
| 132 |
val->intval = !!(reg & F_CHG_ACT); |
| 133 |
|
| 134 |
return 0; |
| 135 |
} |
| 136 |
|
| 137 |
static int ucs1002_get_charge(struct ucs1002_info *info, |
| 138 |
union power_supply_propval *val) |
| 139 |
{ |
| 140 |
/* |
| 141 |
* To fit within 32 bits some values are rounded (uA/h) |
| 142 |
* |
| 143 |
* For Total Accumulated Charge Middle Low Byte register, addr |
| 144 |
* 03h, byte 2 |
| 145 |
* |
| 146 |
* B0: 0.01084 mA/h rounded to 11 uA/h |
| 147 |
* B1: 0.02169 mA/h rounded to 22 uA/h |
| 148 |
* B2: 0.04340 mA/h rounded to 43 uA/h |
| 149 |
* B3: 0.08676 mA/h rounded to 87 uA/h |
| 150 |
* B4: 0.17350 mA/h rounded to 173 uÁ/h |
| 151 |
* |
| 152 |
* For Total Accumulated Charge Low Byte register, addr 04h, |
| 153 |
* byte 3 |
| 154 |
* |
| 155 |
* B6: 0.00271 mA/h rounded to 3 uA/h |
| 156 |
* B7: 0.005422 mA/h rounded to 5 uA/h |
| 157 |
*/ |
| 158 |
static const int bit_weights_uAh[BITS_PER_TYPE(u32)] = { |
| 159 |
/* |
| 160 |
* Bit corresponding to low byte (offset 0x04) |
| 161 |
* B0 B1 B2 B3 B4 B5 B6 B7 |
| 162 |
*/ |
| 163 |
0, 0, 0, 0, 0, 0, 3, 5, |
| 164 |
/* |
| 165 |
* Bit corresponding to middle low byte (offset 0x03) |
| 166 |
* B0 B1 B2 B3 B4 B5 B6 B7 |
| 167 |
*/ |
| 168 |
11, 22, 43, 87, 173, 347, 694, 1388, |
| 169 |
/* |
| 170 |
* Bit corresponding to middle high byte (offset 0x02) |
| 171 |
* B0 B1 B2 B3 B4 B5 B6 B7 |
| 172 |
*/ |
| 173 |
2776, 5552, 11105, 22210, 44420, 88840, 177700, 355400, |
| 174 |
/* |
| 175 |
* Bit corresponding to high byte (offset 0x01) |
| 176 |
* B0 B1 B2 B3 B4 B5 B6 B7 |
| 177 |
*/ |
| 178 |
710700, 1421000, 2843000, 5685000, 11371000, 22742000, |
| 179 |
45484000, 90968000, |
| 180 |
}; |
| 181 |
unsigned long total_acc_charger; |
| 182 |
unsigned int reg; |
| 183 |
int i, ret; |
| 184 |
|
| 185 |
ret = regmap_bulk_read(info->regmap, UCS1002_REG_TOTAL_ACC_CHARGE, |
| 186 |
®, sizeof(u32)); |
| 187 |
if (ret) |
| 188 |
return ret; |
| 189 |
|
| 190 |
total_acc_charger = be32_to_cpu(reg); /* BE as per offsets above */ |
| 191 |
val->intval = 0; |
| 192 |
|
| 193 |
for_each_set_bit(i, &total_acc_charger, ARRAY_SIZE(bit_weights_uAh)) |
| 194 |
val->intval += bit_weights_uAh[i]; |
| 195 |
|
| 196 |
return 0; |
| 197 |
} |
| 198 |
|
| 199 |
static int ucs1002_get_current(struct ucs1002_info *info, |
| 200 |
union power_supply_propval *val) |
| 201 |
{ |
| 202 |
/* |
| 203 |
* The Current Measurement register stores the measured |
| 204 |
* current value delivered to the portable device. The range |
| 205 |
* is from 9.76 mA to 2.5 A. |
| 206 |
*/ |
| 207 |
static const int bit_weights_uA[BITS_PER_TYPE(u8)] = { |
| 208 |
9760, 19500, 39000, 78100, 156200, 312300, 624600, 1249300, |
| 209 |
}; |
| 210 |
unsigned long current_measurement; |
| 211 |
unsigned int reg; |
| 212 |
int i, ret; |
| 213 |
|
| 214 |
ret = regmap_read(info->regmap, UCS1002_REG_CURRENT_MEASUREMENT, ®); |
| 215 |
if (ret) |
| 216 |
return ret; |
| 217 |
|
| 218 |
current_measurement = reg; |
| 219 |
val->intval = 0; |
| 220 |
|
| 221 |
for_each_set_bit(i, ¤t_measurement, ARRAY_SIZE(bit_weights_uA)) |
| 222 |
val->intval += bit_weights_uA[i]; |
| 223 |
|
| 224 |
return 0; |
| 225 |
} |
| 226 |
|
| 227 |
/* |
| 228 |
* The Current Limit register stores the maximum current used by the |
| 229 |
* port switch. The range is from 500mA to 2.5 A. |
| 230 |
*/ |
| 231 |
static const u32 ucs1002_current_limit_uA[] = { |
| 232 |
500000, 900000, 1000000, 1200000, 1500000, 1800000, 2000000, 2500000, |
| 233 |
}; |
| 234 |
|
| 235 |
static int ucs1002_get_max_current(struct ucs1002_info *info, |
| 236 |
union power_supply_propval *val) |
| 237 |
{ |
| 238 |
unsigned int reg; |
| 239 |
int ret; |
| 240 |
|
| 241 |
if (info->output_disable) { |
| 242 |
val->intval = 0; |
| 243 |
return 0; |
| 244 |
} |
| 245 |
|
| 246 |
ret = regmap_read(info->regmap, UCS1002_REG_ILIMIT, ®); |
| 247 |
if (ret) |
| 248 |
return ret; |
| 249 |
|
| 250 |
val->intval = ucs1002_current_limit_uA[reg & UCS1002_ILIM_SW_MASK]; |
| 251 |
|
| 252 |
return 0; |
| 253 |
} |
| 254 |
|
| 255 |
static int ucs1002_set_max_current(struct ucs1002_info *info, u32 val) |
| 256 |
{ |
| 257 |
unsigned int reg; |
| 258 |
int ret, idx; |
| 259 |
|
| 260 |
if (val == 0) { |
| 261 |
info->output_disable = true; |
| 262 |
regulator_disable_regmap(info->rdev); |
| 263 |
return 0; |
| 264 |
} |
| 265 |
|
| 266 |
for (idx = 0; idx < ARRAY_SIZE(ucs1002_current_limit_uA); idx++) { |
| 267 |
if (val == ucs1002_current_limit_uA[idx]) |
| 268 |
break; |
| 269 |
} |
| 270 |
|
| 271 |
if (idx == ARRAY_SIZE(ucs1002_current_limit_uA)) |
| 272 |
return -EINVAL; |
| 273 |
|
| 274 |
ret = regmap_write(info->regmap, UCS1002_REG_ILIMIT, idx); |
| 275 |
if (ret) |
| 276 |
return ret; |
| 277 |
/* |
| 278 |
* Any current limit setting exceeding the one set via ILIM |
| 279 |
* pin will be rejected, so we read out freshly changed limit |
| 280 |
* to make sure that it took effect. |
| 281 |
*/ |
| 282 |
ret = regmap_read(info->regmap, UCS1002_REG_ILIMIT, ®); |
| 283 |
if (ret) |
| 284 |
return ret; |
| 285 |
|
| 286 |
if (reg != idx) |
| 287 |
return -EINVAL; |
| 288 |
|
| 289 |
info->output_disable = false; |
| 290 |
|
| 291 |
if (info->rdev && info->rdev->use_count && |
| 292 |
!regulator_is_enabled_regmap(info->rdev)) |
| 293 |
regulator_enable_regmap(info->rdev); |
| 294 |
|
| 295 |
return 0; |
| 296 |
} |
| 297 |
|
| 298 |
static int ucs1002_set_usb_type(struct ucs1002_info *info, int val) |
| 299 |
{ |
| 300 |
unsigned int mode; |
| 301 |
|
| 302 |
switch (val) { |
| 303 |
/* |
| 304 |
* POWER_SUPPLY_USB_TYPE_UNKNOWN == 0, map this to dedicated for |
| 305 |
* userspace API compatibility with older versions of this driver |
| 306 |
* which mapped 0 to dedicated. |
| 307 |
*/ |
| 308 |
case POWER_SUPPLY_USB_TYPE_UNKNOWN: |
| 309 |
case POWER_SUPPLY_USB_TYPE_PD: |
| 310 |
mode = V_SET_ACTIVE_MODE_DEDICATED; |
| 311 |
break; |
| 312 |
case POWER_SUPPLY_USB_TYPE_SDP: |
| 313 |
mode = V_SET_ACTIVE_MODE_BC12_SDP; |
| 314 |
break; |
| 315 |
case POWER_SUPPLY_USB_TYPE_DCP: |
| 316 |
mode = V_SET_ACTIVE_MODE_BC12_DCP; |
| 317 |
break; |
| 318 |
case POWER_SUPPLY_USB_TYPE_CDP: |
| 319 |
mode = V_SET_ACTIVE_MODE_BC12_CDP; |
| 320 |
break; |
| 321 |
default: |
| 322 |
return -EINVAL; |
| 323 |
} |
| 324 |
|
| 325 |
return regmap_update_bits(info->regmap, UCS1002_REG_SWITCH_CFG, |
| 326 |
V_SET_ACTIVE_MODE_MASK, mode); |
| 327 |
} |
| 328 |
|
| 329 |
static int ucs1002_get_usb_type(struct ucs1002_info *info, |
| 330 |
union power_supply_propval *val) |
| 331 |
{ |
| 332 |
enum power_supply_usb_type type; |
| 333 |
unsigned int reg; |
| 334 |
int ret; |
| 335 |
|
| 336 |
ret = regmap_read(info->regmap, UCS1002_REG_PIN_STATUS, ®); |
| 337 |
if (ret) |
| 338 |
return ret; |
| 339 |
|
| 340 |
switch (reg & F_ACTIVE_MODE_MASK) { |
| 341 |
default: |
| 342 |
type = POWER_SUPPLY_USB_TYPE_UNKNOWN; |
| 343 |
break; |
| 344 |
case F_ACTIVE_MODE_DEDICATED: |
| 345 |
type = POWER_SUPPLY_USB_TYPE_PD; |
| 346 |
break; |
| 347 |
case F_ACTIVE_MODE_BC12_SDP: |
| 348 |
type = POWER_SUPPLY_USB_TYPE_SDP; |
| 349 |
break; |
| 350 |
case F_ACTIVE_MODE_BC12_DCP: |
| 351 |
type = POWER_SUPPLY_USB_TYPE_DCP; |
| 352 |
break; |
| 353 |
case F_ACTIVE_MODE_BC12_CDP: |
| 354 |
type = POWER_SUPPLY_USB_TYPE_CDP; |
| 355 |
break; |
| 356 |
} |
| 357 |
|
| 358 |
val->intval = type; |
| 359 |
|
| 360 |
return 0; |
| 361 |
} |
| 362 |
|
| 363 |
static int ucs1002_get_property(struct power_supply *psy, |
| 364 |
enum power_supply_property psp, |
| 365 |
union power_supply_propval *val) |
| 366 |
{ |
| 367 |
struct ucs1002_info *info = power_supply_get_drvdata(psy); |
| 368 |
|
| 369 |
switch (psp) { |
| 370 |
case POWER_SUPPLY_PROP_ONLINE: |
| 371 |
return ucs1002_get_online(info, val); |
| 372 |
case POWER_SUPPLY_PROP_CHARGE_NOW: |
| 373 |
return ucs1002_get_charge(info, val); |
| 374 |
case POWER_SUPPLY_PROP_CURRENT_NOW: |
| 375 |
return ucs1002_get_current(info, val); |
| 376 |
case POWER_SUPPLY_PROP_CURRENT_MAX: |
| 377 |
return ucs1002_get_max_current(info, val); |
| 378 |
case POWER_SUPPLY_PROP_USB_TYPE: |
| 379 |
return ucs1002_get_usb_type(info, val); |
| 380 |
case POWER_SUPPLY_PROP_HEALTH: |
| 381 |
val->intval = info->health; |
| 382 |
return 0; |
| 383 |
case POWER_SUPPLY_PROP_PRESENT: |
| 384 |
val->intval = info->present; |
| 385 |
return 0; |
| 386 |
case POWER_SUPPLY_PROP_MANUFACTURER: |
| 387 |
val->strval = UCS1002_MANUFACTURER; |
| 388 |
return 0; |
| 389 |
default: |
| 390 |
return -EINVAL; |
| 391 |
} |
| 392 |
} |
| 393 |
|
| 394 |
static int ucs1002_set_property(struct power_supply *psy, |
| 395 |
enum power_supply_property psp, |
| 396 |
const union power_supply_propval *val) |
| 397 |
{ |
| 398 |
struct ucs1002_info *info = power_supply_get_drvdata(psy); |
| 399 |
|
| 400 |
switch (psp) { |
| 401 |
case POWER_SUPPLY_PROP_CURRENT_MAX: |
| 402 |
return ucs1002_set_max_current(info, val->intval); |
| 403 |
case POWER_SUPPLY_PROP_USB_TYPE: |
| 404 |
return ucs1002_set_usb_type(info, val->intval); |
| 405 |
default: |
| 406 |
return -EINVAL; |
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
static int ucs1002_property_is_writeable(struct power_supply *psy, |
| 411 |
enum power_supply_property psp) |
| 412 |
{ |
| 413 |
switch (psp) { |
| 414 |
case POWER_SUPPLY_PROP_CURRENT_MAX: |
| 415 |
case POWER_SUPPLY_PROP_USB_TYPE: |
| 416 |
return true; |
| 417 |
default: |
| 418 |
return false; |
| 419 |
} |
| 420 |
} |
| 421 |
|
| 422 |
static const struct power_supply_desc ucs1002_charger_desc = { |
| 423 |
.name = "ucs1002", |
| 424 |
.type = POWER_SUPPLY_TYPE_USB, |
| 425 |
.usb_types = BIT(POWER_SUPPLY_USB_TYPE_SDP) | |
| 426 |
BIT(POWER_SUPPLY_USB_TYPE_CDP) | |
| 427 |
BIT(POWER_SUPPLY_USB_TYPE_DCP) | |
| 428 |
BIT(POWER_SUPPLY_USB_TYPE_PD) | |
| 429 |
BIT(POWER_SUPPLY_USB_TYPE_UNKNOWN), |
| 430 |
.get_property = ucs1002_get_property, |
| 431 |
.set_property = ucs1002_set_property, |
| 432 |
.property_is_writeable = ucs1002_property_is_writeable, |
| 433 |
.properties = ucs1002_props, |
| 434 |
.num_properties = ARRAY_SIZE(ucs1002_props), |
| 435 |
}; |
| 436 |
|
| 437 |
static void ucs1002_health_poll(struct work_struct *work) |
| 438 |
{ |
| 439 |
struct ucs1002_info *info = container_of(work, struct ucs1002_info, |
| 440 |
health_poll.work); |
| 441 |
int ret; |
| 442 |
u32 reg; |
| 443 |
|
| 444 |
ret = regmap_read(info->regmap, UCS1002_REG_INTERRUPT_STATUS, ®); |
| 445 |
if (ret) |
| 446 |
return; |
| 447 |
|
| 448 |
/* bad health and no status change, just schedule us again in a while */ |
| 449 |
if ((reg & F_ERR) && info->health != POWER_SUPPLY_HEALTH_GOOD) { |
| 450 |
schedule_delayed_work(&info->health_poll, |
| 451 |
msecs_to_jiffies(2000)); |
| 452 |
return; |
| 453 |
} |
| 454 |
|
| 455 |
if (reg & F_TSD) |
| 456 |
info->health = POWER_SUPPLY_HEALTH_OVERHEAT; |
| 457 |
else if (reg & (F_OVER_VOLT | F_BACK_VOLT)) |
| 458 |
info->health = POWER_SUPPLY_HEALTH_OVERVOLTAGE; |
| 459 |
else if (reg & F_OVER_ILIM) |
| 460 |
info->health = POWER_SUPPLY_HEALTH_OVERCURRENT; |
| 461 |
else if (reg & (F_DISCHARGE_ERR | F_MIN_KEEP_OUT)) |
| 462 |
info->health = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE; |
| 463 |
else |
| 464 |
info->health = POWER_SUPPLY_HEALTH_GOOD; |
| 465 |
|
| 466 |
sysfs_notify(&info->charger->dev.kobj, NULL, "health"); |
| 467 |
} |
| 468 |
|
| 469 |
static irqreturn_t ucs1002_charger_irq(int irq, void *data) |
| 470 |
{ |
| 471 |
int ret, regval; |
| 472 |
bool present; |
| 473 |
struct ucs1002_info *info = data; |
| 474 |
|
| 475 |
present = info->present; |
| 476 |
|
| 477 |
ret = regmap_read(info->regmap, UCS1002_REG_OTHER_STATUS, ®val); |
| 478 |
if (ret) |
| 479 |
return IRQ_HANDLED; |
| 480 |
|
| 481 |
/* update attached status */ |
| 482 |
info->present = regval & F_ADET_PIN; |
| 483 |
|
| 484 |
/* notify the change */ |
| 485 |
if (present != info->present) |
| 486 |
power_supply_changed(info->charger); |
| 487 |
|
| 488 |
return IRQ_HANDLED; |
| 489 |
} |
| 490 |
|
| 491 |
static irqreturn_t ucs1002_alert_irq(int irq, void *data) |
| 492 |
{ |
| 493 |
struct ucs1002_info *info = data; |
| 494 |
|
| 495 |
mod_delayed_work(system_percpu_wq, &info->health_poll, 0); |
| 496 |
|
| 497 |
return IRQ_HANDLED; |
| 498 |
} |
| 499 |
|
| 500 |
static int ucs1002_regulator_enable(struct regulator_dev *rdev) |
| 501 |
{ |
| 502 |
struct ucs1002_info *info = rdev_get_drvdata(rdev); |
| 503 |
|
| 504 |
/* |
| 505 |
* If the output is disabled due to 0 maximum current, just pretend the |
| 506 |
* enable did work. The regulator will be enabled as soon as we get a |
| 507 |
* a non-zero maximum current budget. |
| 508 |
*/ |
| 509 |
if (info->output_disable) |
| 510 |
return 0; |
| 511 |
|
| 512 |
return regulator_enable_regmap(rdev); |
| 513 |
} |
| 514 |
|
| 515 |
static const struct regulator_ops ucs1002_regulator_ops = { |
| 516 |
.is_enabled = regulator_is_enabled_regmap, |
| 517 |
.enable = ucs1002_regulator_enable, |
| 518 |
.disable = regulator_disable_regmap, |
| 519 |
}; |
| 520 |
|
| 521 |
static const struct regulator_desc ucs1002_regulator_descriptor = { |
| 522 |
.name = "ucs1002-vbus", |
| 523 |
.ops = &ucs1002_regulator_ops, |
| 524 |
.type = REGULATOR_VOLTAGE, |
| 525 |
.owner = THIS_MODULE, |
| 526 |
.enable_reg = UCS1002_REG_SWITCH_CFG, |
| 527 |
.enable_mask = F_PWR_EN_SET, |
| 528 |
.enable_val = F_PWR_EN_SET, |
| 529 |
.fixed_uV = 5000000, |
| 530 |
.n_voltages = 1, |
| 531 |
}; |
| 532 |
|
| 533 |
static int ucs1002_probe(struct i2c_client *client) |
| 534 |
{ |
| 535 |
struct device *dev = &client->dev; |
| 536 |
struct power_supply_config charger_config = {}; |
| 537 |
const struct regmap_config regmap_config = { |
| 538 |
.reg_bits = 8, |
| 539 |
.val_bits = 8, |
| 540 |
}; |
| 541 |
struct regulator_config regulator_config = {}; |
| 542 |
int irq_a_det, irq_alert, ret; |
| 543 |
struct ucs1002_info *info; |
| 544 |
unsigned int regval; |
| 545 |
|
| 546 |
info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL); |
| 547 |
if (!info) |
| 548 |
return -ENOMEM; |
| 549 |
|
| 550 |
info->regmap = devm_regmap_init_i2c(client, ®map_config); |
| 551 |
ret = PTR_ERR_OR_ZERO(info->regmap); |
| 552 |
if (ret) { |
| 553 |
dev_err(dev, "Regmap initialization failed: %d\n", ret); |
| 554 |
return ret; |
| 555 |
} |
| 556 |
|
| 557 |
info->client = client; |
| 558 |
|
| 559 |
irq_a_det = of_irq_get_byname(dev->of_node, "a_det"); |
| 560 |
irq_alert = of_irq_get_byname(dev->of_node, "alert"); |
| 561 |
|
| 562 |
charger_config.fwnode = dev_fwnode(dev); |
| 563 |
charger_config.drv_data = info; |
| 564 |
|
| 565 |
ret = regmap_read(info->regmap, UCS1002_REG_PRODUCT_ID, ®val); |
| 566 |
if (ret) { |
| 567 |
dev_err(dev, "Failed to read product ID: %d\n", ret); |
| 568 |
return ret; |
| 569 |
} |
| 570 |
|
| 571 |
if (regval != UCS1002_PRODUCT_ID) { |
| 572 |
dev_err(dev, |
| 573 |
"Product ID does not match (0x%02x != 0x%02x)\n", |
| 574 |
regval, UCS1002_PRODUCT_ID); |
| 575 |
return -ENODEV; |
| 576 |
} |
| 577 |
|
| 578 |
/* Enable charge rationing by default */ |
| 579 |
ret = regmap_update_bits(info->regmap, UCS1002_REG_GENERAL_CFG, |
| 580 |
F_RATION_EN, F_RATION_EN); |
| 581 |
if (ret) { |
| 582 |
dev_err(dev, "Failed to read general config: %d\n", ret); |
| 583 |
return ret; |
| 584 |
} |
| 585 |
|
| 586 |
/* |
| 587 |
* Ignore the M1, M2, PWR_EN, and EM_EN pin states. Set active |
| 588 |
* mode selection to BC1.2 CDP. |
| 589 |
*/ |
| 590 |
ret = regmap_update_bits(info->regmap, UCS1002_REG_SWITCH_CFG, |
| 591 |
V_SET_ACTIVE_MODE_MASK | F_PIN_IGNORE, |
| 592 |
V_SET_ACTIVE_MODE_BC12_CDP | F_PIN_IGNORE); |
| 593 |
if (ret) { |
| 594 |
dev_err(dev, "Failed to configure default mode: %d\n", ret); |
| 595 |
return ret; |
| 596 |
} |
| 597 |
/* |
| 598 |
* Be safe and set initial current limit to 500mA |
| 599 |
*/ |
| 600 |
ret = ucs1002_set_max_current(info, 500000); |
| 601 |
if (ret) { |
| 602 |
dev_err(dev, "Failed to set max current default: %d\n", ret); |
| 603 |
return ret; |
| 604 |
} |
| 605 |
|
| 606 |
info->charger = devm_power_supply_register(dev, &ucs1002_charger_desc, |
| 607 |
&charger_config); |
| 608 |
ret = PTR_ERR_OR_ZERO(info->charger); |
| 609 |
if (ret) { |
| 610 |
dev_err(dev, "Failed to register power supply: %d\n", ret); |
| 611 |
return ret; |
| 612 |
} |
| 613 |
|
| 614 |
ret = regmap_read(info->regmap, UCS1002_REG_PIN_STATUS, ®val); |
| 615 |
if (ret) { |
| 616 |
dev_err(dev, "Failed to read pin status: %d\n", ret); |
| 617 |
return ret; |
| 618 |
} |
| 619 |
|
| 620 |
info->regulator_descriptor = |
| 621 |
devm_kmemdup(dev, &ucs1002_regulator_descriptor, |
| 622 |
sizeof(ucs1002_regulator_descriptor), |
| 623 |
GFP_KERNEL); |
| 624 |
if (!info->regulator_descriptor) |
| 625 |
return -ENOMEM; |
| 626 |
|
| 627 |
info->regulator_descriptor->enable_is_inverted = !(regval & F_SEL_PIN); |
| 628 |
|
| 629 |
regulator_config.dev = dev; |
| 630 |
regulator_config.of_node = dev->of_node; |
| 631 |
regulator_config.regmap = info->regmap; |
| 632 |
regulator_config.driver_data = info; |
| 633 |
|
| 634 |
info->rdev = devm_regulator_register(dev, info->regulator_descriptor, |
| 635 |
®ulator_config); |
| 636 |
ret = PTR_ERR_OR_ZERO(info->rdev); |
| 637 |
if (ret) { |
| 638 |
dev_err(dev, "Failed to register VBUS regulator: %d\n", ret); |
| 639 |
return ret; |
| 640 |
} |
| 641 |
|
| 642 |
info->health = POWER_SUPPLY_HEALTH_GOOD; |
| 643 |
INIT_DELAYED_WORK(&info->health_poll, ucs1002_health_poll); |
| 644 |
|
| 645 |
if (irq_a_det > 0) { |
| 646 |
ret = devm_request_threaded_irq(dev, irq_a_det, NULL, |
| 647 |
ucs1002_charger_irq, |
| 648 |
IRQF_ONESHOT, |
| 649 |
"ucs1002-a_det", info); |
| 650 |
if (ret) { |
| 651 |
dev_err(dev, "Failed to request A_DET threaded irq: %d\n", |
| 652 |
ret); |
| 653 |
return ret; |
| 654 |
} |
| 655 |
} |
| 656 |
|
| 657 |
if (irq_alert > 0) { |
| 658 |
ret = devm_request_irq(dev, irq_alert, ucs1002_alert_irq, |
| 659 |
0,"ucs1002-alert", info); |
| 660 |
if (ret) { |
| 661 |
dev_err(dev, "Failed to request ALERT threaded irq: %d\n", |
| 662 |
ret); |
| 663 |
return ret; |
| 664 |
} |
| 665 |
} |
| 666 |
|
| 667 |
return 0; |
| 668 |
} |
| 669 |
|
| 670 |
static const struct of_device_id ucs1002_of_match[] = { |
| 671 |
{ .compatible = "microchip,ucs1002", }, |
| 672 |
{ /* sentinel */ }, |
| 673 |
}; |
| 674 |
MODULE_DEVICE_TABLE(of, ucs1002_of_match); |
| 675 |
|
| 676 |
static struct i2c_driver ucs1002_driver = { |
| 677 |
.driver = { |
| 678 |
.name = "ucs1002", |
| 679 |
.of_match_table = ucs1002_of_match, |
| 680 |
}, |
| 681 |
.probe = ucs1002_probe, |
| 682 |
}; |
| 683 |
module_i2c_driver(ucs1002_driver); |
| 684 |
|
| 685 |
MODULE_DESCRIPTION("Microchip UCS1002 Programmable USB Port Power Controller"); |
| 686 |
MODULE_AUTHOR("Enric Balletbo Serra <[email protected]>"); |
| 687 |
MODULE_AUTHOR("Andrey Smirnov <[email protected]>"); |
| 688 |
MODULE_LICENSE("GPL"); |