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