Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * Driver for UCS1002 Programmable USB Port Power Controller
0004  *
0005  * Copyright (C) 2019 Zodiac Inflight Innovations
0006  */
0007 #include <linux/bits.h>
0008 #include <linux/freezer.h>
0009 #include <linux/gpio/consumer.h>
0010 #include <linux/i2c.h>
0011 #include <linux/interrupt.h>
0012 #include <linux/kernel.h>
0013 #include <linux/kthread.h>
0014 #include <linux/device.h>
0015 #include <linux/module.h>
0016 #include <linux/of.h>
0017 #include <linux/of_irq.h>
0018 #include <linux/power_supply.h>
0019 #include <linux/regmap.h>
0020 #include <linux/regulator/driver.h>
0021 #include <linux/regulator/of_regulator.h>
0022 
0023 /* UCS1002 Registers */
0024 #define UCS1002_REG_CURRENT_MEASUREMENT 0x00
0025 
0026 /*
0027  * The Total Accumulated Charge registers store the total accumulated
0028  * charge delivered from the VS source to a portable device. The total
0029  * value is calculated using four registers, from 01h to 04h. The bit
0030  * weighting of the registers is given in mA/hrs.
0031  */
0032 #define UCS1002_REG_TOTAL_ACC_CHARGE    0x01
0033 
0034 /* Other Status Register */
0035 #define UCS1002_REG_OTHER_STATUS    0x0f
0036 #  define F_ADET_PIN            BIT(4)
0037 #  define F_CHG_ACT         BIT(3)
0038 
0039 /* Interrupt Status */
0040 #define UCS1002_REG_INTERRUPT_STATUS    0x10
0041 #  define F_ERR             BIT(7)
0042 #  define F_DISCHARGE_ERR       BIT(6)
0043 #  define F_RESET           BIT(5)
0044 #  define F_MIN_KEEP_OUT        BIT(4)
0045 #  define F_TSD             BIT(3)
0046 #  define F_OVER_VOLT           BIT(2)
0047 #  define F_BACK_VOLT           BIT(1)
0048 #  define F_OVER_ILIM           BIT(0)
0049 
0050 /* Pin Status Register */
0051 #define UCS1002_REG_PIN_STATUS      0x14
0052 #  define UCS1002_PWR_STATE_MASK    0x03
0053 #  define F_PWR_EN_PIN          BIT(6)
0054 #  define F_M2_PIN          BIT(5)
0055 #  define F_M1_PIN          BIT(4)
0056 #  define F_EM_EN_PIN           BIT(3)
0057 #  define F_SEL_PIN         BIT(2)
0058 #  define F_ACTIVE_MODE_MASK        GENMASK(5, 3)
0059 #  define F_ACTIVE_MODE_PASSTHROUGH F_M2_PIN
0060 #  define F_ACTIVE_MODE_DEDICATED   F_EM_EN_PIN
0061 #  define F_ACTIVE_MODE_BC12_DCP    (F_M2_PIN | F_EM_EN_PIN)
0062 #  define F_ACTIVE_MODE_BC12_SDP    F_M1_PIN
0063 #  define F_ACTIVE_MODE_BC12_CDP    (F_M1_PIN | F_M2_PIN | F_EM_EN_PIN)
0064 
0065 /* General Configuration Register */
0066 #define UCS1002_REG_GENERAL_CFG     0x15
0067 #  define F_RATION_EN           BIT(3)
0068 
0069 /* Emulation Configuration Register */
0070 #define UCS1002_REG_EMU_CFG     0x16
0071 
0072 /* Switch Configuration Register */
0073 #define UCS1002_REG_SWITCH_CFG      0x17
0074 #  define F_PIN_IGNORE          BIT(7)
0075 #  define F_EM_EN_SET           BIT(5)
0076 #  define F_M2_SET          BIT(4)
0077 #  define F_M1_SET          BIT(3)
0078 #  define F_S0_SET          BIT(2)
0079 #  define F_PWR_EN_SET          BIT(1)
0080 #  define F_LATCH_SET           BIT(0)
0081 #  define V_SET_ACTIVE_MODE_MASK    GENMASK(5, 3)
0082 #  define V_SET_ACTIVE_MODE_PASSTHROUGH F_M2_SET
0083 #  define V_SET_ACTIVE_MODE_DEDICATED   F_EM_EN_SET
0084 #  define V_SET_ACTIVE_MODE_BC12_DCP    (F_M2_SET | F_EM_EN_SET)
0085 #  define V_SET_ACTIVE_MODE_BC12_SDP    F_M1_SET
0086 #  define V_SET_ACTIVE_MODE_BC12_CDP    (F_M1_SET | F_M2_SET | F_EM_EN_SET)
0087 
0088 /* Current Limit Register */
0089 #define UCS1002_REG_ILIMIT      0x19
0090 #  define UCS1002_ILIM_SW_MASK      GENMASK(3, 0)
0091 
0092 /* Product ID */
0093 #define UCS1002_REG_PRODUCT_ID      0xfd
0094 #  define UCS1002_PRODUCT_ID        0x4e
0095 
0096 /* Manufacture name */
0097 #define UCS1002_MANUFACTURER        "SMSC"
0098 
0099 struct ucs1002_info {
0100     struct power_supply *charger;
0101     struct i2c_client *client;
0102     struct regmap *regmap;
0103     struct regulator_desc *regulator_descriptor;
0104     struct regulator_dev *rdev;
0105     bool present;
0106     bool output_disable;
0107     struct delayed_work health_poll;
0108     int health;
0109 
0110 };
0111 
0112 static enum power_supply_property ucs1002_props[] = {
0113     POWER_SUPPLY_PROP_ONLINE,
0114     POWER_SUPPLY_PROP_CHARGE_NOW,
0115     POWER_SUPPLY_PROP_CURRENT_NOW,
0116     POWER_SUPPLY_PROP_CURRENT_MAX,
0117     POWER_SUPPLY_PROP_PRESENT, /* the presence of PED */
0118     POWER_SUPPLY_PROP_MANUFACTURER,
0119     POWER_SUPPLY_PROP_USB_TYPE,
0120     POWER_SUPPLY_PROP_HEALTH,
0121 };
0122 
0123 static int ucs1002_get_online(struct ucs1002_info *info,
0124                   union power_supply_propval *val)
0125 {
0126     unsigned int reg;
0127     int ret;
0128 
0129     ret = regmap_read(info->regmap, UCS1002_REG_OTHER_STATUS, &reg);
0130     if (ret)
0131         return ret;
0132 
0133     val->intval = !!(reg & F_CHG_ACT);
0134 
0135     return 0;
0136 }
0137 
0138 static int ucs1002_get_charge(struct ucs1002_info *info,
0139                   union power_supply_propval *val)
0140 {
0141     /*
0142      * To fit within 32 bits some values are rounded (uA/h)
0143      *
0144      * For Total Accumulated Charge Middle Low Byte register, addr
0145      * 03h, byte 2
0146      *
0147      *   B0: 0.01084 mA/h rounded to 11 uA/h
0148      *   B1: 0.02169 mA/h rounded to 22 uA/h
0149      *   B2: 0.04340 mA/h rounded to 43 uA/h
0150      *   B3: 0.08676 mA/h rounded to 87 uA/h
0151      *   B4: 0.17350 mA/h rounded to 173 uÁ/h
0152      *
0153      * For Total Accumulated Charge Low Byte register, addr 04h,
0154      * byte 3
0155      *
0156      *   B6: 0.00271 mA/h rounded to 3 uA/h
0157      *   B7: 0.005422 mA/h rounded to 5 uA/h
0158      */
0159     static const int bit_weights_uAh[BITS_PER_TYPE(u32)] = {
0160         /*
0161          * Bit corresponding to low byte (offset 0x04)
0162          * B0 B1 B2 B3 B4 B5 B6 B7
0163          */
0164         0, 0, 0, 0, 0, 0, 3, 5,
0165         /*
0166          * Bit corresponding to middle low byte (offset 0x03)
0167          * B0 B1 B2 B3 B4 B5 B6 B7
0168          */
0169         11, 22, 43, 87, 173, 347, 694, 1388,
0170         /*
0171          * Bit corresponding to middle high byte (offset 0x02)
0172          * B0 B1 B2 B3 B4 B5 B6 B7
0173          */
0174         2776, 5552, 11105, 22210, 44420, 88840, 177700, 355400,
0175         /*
0176          * Bit corresponding to high byte (offset 0x01)
0177          * B0 B1 B2 B3 B4 B5 B6 B7
0178          */
0179         710700, 1421000, 2843000, 5685000, 11371000, 22742000,
0180         45484000, 90968000,
0181     };
0182     unsigned long total_acc_charger;
0183     unsigned int reg;
0184     int i, ret;
0185 
0186     ret = regmap_bulk_read(info->regmap, UCS1002_REG_TOTAL_ACC_CHARGE,
0187                    &reg, sizeof(u32));
0188     if (ret)
0189         return ret;
0190 
0191     total_acc_charger = be32_to_cpu(reg); /* BE as per offsets above */
0192     val->intval = 0;
0193 
0194     for_each_set_bit(i, &total_acc_charger, ARRAY_SIZE(bit_weights_uAh))
0195         val->intval += bit_weights_uAh[i];
0196 
0197     return 0;
0198 }
0199 
0200 static int ucs1002_get_current(struct ucs1002_info *info,
0201                    union power_supply_propval *val)
0202 {
0203     /*
0204      * The Current Measurement register stores the measured
0205      * current value delivered to the portable device. The range
0206      * is from 9.76 mA to 2.5 A.
0207      */
0208     static const int bit_weights_uA[BITS_PER_TYPE(u8)] = {
0209         9760, 19500, 39000, 78100, 156200, 312300, 624600, 1249300,
0210     };
0211     unsigned long current_measurement;
0212     unsigned int reg;
0213     int i, ret;
0214 
0215     ret = regmap_read(info->regmap, UCS1002_REG_CURRENT_MEASUREMENT, &reg);
0216     if (ret)
0217         return ret;
0218 
0219     current_measurement = reg;
0220     val->intval = 0;
0221 
0222     for_each_set_bit(i, &current_measurement, ARRAY_SIZE(bit_weights_uA))
0223         val->intval += bit_weights_uA[i];
0224 
0225     return 0;
0226 }
0227 
0228 /*
0229  * The Current Limit register stores the maximum current used by the
0230  * port switch. The range is from 500mA to 2.5 A.
0231  */
0232 static const u32 ucs1002_current_limit_uA[] = {
0233     500000, 900000, 1000000, 1200000, 1500000, 1800000, 2000000, 2500000,
0234 };
0235 
0236 static int ucs1002_get_max_current(struct ucs1002_info *info,
0237                    union power_supply_propval *val)
0238 {
0239     unsigned int reg;
0240     int ret;
0241 
0242     if (info->output_disable) {
0243         val->intval = 0;
0244         return 0;
0245     }
0246 
0247     ret = regmap_read(info->regmap, UCS1002_REG_ILIMIT, &reg);
0248     if (ret)
0249         return ret;
0250 
0251     val->intval = ucs1002_current_limit_uA[reg & UCS1002_ILIM_SW_MASK];
0252 
0253     return 0;
0254 }
0255 
0256 static int ucs1002_set_max_current(struct ucs1002_info *info, u32 val)
0257 {
0258     unsigned int reg;
0259     int ret, idx;
0260 
0261     if (val == 0) {
0262         info->output_disable = true;
0263         regulator_disable_regmap(info->rdev);
0264         return 0;
0265     }
0266 
0267     for (idx = 0; idx < ARRAY_SIZE(ucs1002_current_limit_uA); idx++) {
0268         if (val == ucs1002_current_limit_uA[idx])
0269             break;
0270     }
0271 
0272     if (idx == ARRAY_SIZE(ucs1002_current_limit_uA))
0273         return -EINVAL;
0274 
0275     ret = regmap_write(info->regmap, UCS1002_REG_ILIMIT, idx);
0276     if (ret)
0277         return ret;
0278     /*
0279      * Any current limit setting exceeding the one set via ILIM
0280      * pin will be rejected, so we read out freshly changed limit
0281      * to make sure that it took effect.
0282      */
0283     ret = regmap_read(info->regmap, UCS1002_REG_ILIMIT, &reg);
0284     if (ret)
0285         return ret;
0286 
0287     if (reg != idx)
0288         return -EINVAL;
0289 
0290     info->output_disable = false;
0291 
0292     if (info->rdev && info->rdev->use_count &&
0293         !regulator_is_enabled_regmap(info->rdev))
0294         regulator_enable_regmap(info->rdev);
0295 
0296     return 0;
0297 }
0298 
0299 static enum power_supply_usb_type ucs1002_usb_types[] = {
0300     POWER_SUPPLY_USB_TYPE_PD,
0301     POWER_SUPPLY_USB_TYPE_SDP,
0302     POWER_SUPPLY_USB_TYPE_DCP,
0303     POWER_SUPPLY_USB_TYPE_CDP,
0304     POWER_SUPPLY_USB_TYPE_UNKNOWN,
0305 };
0306 
0307 static int ucs1002_set_usb_type(struct ucs1002_info *info, int val)
0308 {
0309     unsigned int mode;
0310 
0311     if (val < 0 || val >= ARRAY_SIZE(ucs1002_usb_types))
0312         return -EINVAL;
0313 
0314     switch (ucs1002_usb_types[val]) {
0315     case POWER_SUPPLY_USB_TYPE_PD:
0316         mode = V_SET_ACTIVE_MODE_DEDICATED;
0317         break;
0318     case POWER_SUPPLY_USB_TYPE_SDP:
0319         mode = V_SET_ACTIVE_MODE_BC12_SDP;
0320         break;
0321     case POWER_SUPPLY_USB_TYPE_DCP:
0322         mode = V_SET_ACTIVE_MODE_BC12_DCP;
0323         break;
0324     case POWER_SUPPLY_USB_TYPE_CDP:
0325         mode = V_SET_ACTIVE_MODE_BC12_CDP;
0326         break;
0327     default:
0328         return -EINVAL;
0329     }
0330 
0331     return regmap_update_bits(info->regmap, UCS1002_REG_SWITCH_CFG,
0332                   V_SET_ACTIVE_MODE_MASK, mode);
0333 }
0334 
0335 static int ucs1002_get_usb_type(struct ucs1002_info *info,
0336                 union power_supply_propval *val)
0337 {
0338     enum power_supply_usb_type type;
0339     unsigned int reg;
0340     int ret;
0341 
0342     ret = regmap_read(info->regmap, UCS1002_REG_PIN_STATUS, &reg);
0343     if (ret)
0344         return ret;
0345 
0346     switch (reg & F_ACTIVE_MODE_MASK) {
0347     default:
0348         type = POWER_SUPPLY_USB_TYPE_UNKNOWN;
0349         break;
0350     case F_ACTIVE_MODE_DEDICATED:
0351         type = POWER_SUPPLY_USB_TYPE_PD;
0352         break;
0353     case F_ACTIVE_MODE_BC12_SDP:
0354         type = POWER_SUPPLY_USB_TYPE_SDP;
0355         break;
0356     case F_ACTIVE_MODE_BC12_DCP:
0357         type = POWER_SUPPLY_USB_TYPE_DCP;
0358         break;
0359     case F_ACTIVE_MODE_BC12_CDP:
0360         type = POWER_SUPPLY_USB_TYPE_CDP;
0361         break;
0362     }
0363 
0364     val->intval = type;
0365 
0366     return 0;
0367 }
0368 
0369 static int ucs1002_get_property(struct power_supply *psy,
0370                 enum power_supply_property psp,
0371                 union power_supply_propval *val)
0372 {
0373     struct ucs1002_info *info = power_supply_get_drvdata(psy);
0374 
0375     switch (psp) {
0376     case POWER_SUPPLY_PROP_ONLINE:
0377         return ucs1002_get_online(info, val);
0378     case POWER_SUPPLY_PROP_CHARGE_NOW:
0379         return ucs1002_get_charge(info, val);
0380     case POWER_SUPPLY_PROP_CURRENT_NOW:
0381         return ucs1002_get_current(info, val);
0382     case POWER_SUPPLY_PROP_CURRENT_MAX:
0383         return ucs1002_get_max_current(info, val);
0384     case POWER_SUPPLY_PROP_USB_TYPE:
0385         return ucs1002_get_usb_type(info, val);
0386     case POWER_SUPPLY_PROP_HEALTH:
0387         return val->intval = info->health;
0388     case POWER_SUPPLY_PROP_PRESENT:
0389         val->intval = info->present;
0390         return 0;
0391     case POWER_SUPPLY_PROP_MANUFACTURER:
0392         val->strval = UCS1002_MANUFACTURER;
0393         return 0;
0394     default:
0395         return -EINVAL;
0396     }
0397 }
0398 
0399 static int ucs1002_set_property(struct power_supply *psy,
0400                 enum power_supply_property psp,
0401                 const union power_supply_propval *val)
0402 {
0403     struct ucs1002_info *info = power_supply_get_drvdata(psy);
0404 
0405     switch (psp) {
0406     case POWER_SUPPLY_PROP_CURRENT_MAX:
0407         return ucs1002_set_max_current(info, val->intval);
0408     case POWER_SUPPLY_PROP_USB_TYPE:
0409         return ucs1002_set_usb_type(info, val->intval);
0410     default:
0411         return -EINVAL;
0412     }
0413 }
0414 
0415 static int ucs1002_property_is_writeable(struct power_supply *psy,
0416                      enum power_supply_property psp)
0417 {
0418     switch (psp) {
0419     case POWER_SUPPLY_PROP_CURRENT_MAX:
0420     case POWER_SUPPLY_PROP_USB_TYPE:
0421         return true;
0422     default:
0423         return false;
0424     }
0425 }
0426 
0427 static const struct power_supply_desc ucs1002_charger_desc = {
0428     .name           = "ucs1002",
0429     .type           = POWER_SUPPLY_TYPE_USB,
0430     .usb_types      = ucs1002_usb_types,
0431     .num_usb_types      = ARRAY_SIZE(ucs1002_usb_types),
0432     .get_property       = ucs1002_get_property,
0433     .set_property       = ucs1002_set_property,
0434     .property_is_writeable  = ucs1002_property_is_writeable,
0435     .properties     = ucs1002_props,
0436     .num_properties     = ARRAY_SIZE(ucs1002_props),
0437 };
0438 
0439 static void ucs1002_health_poll(struct work_struct *work)
0440 {
0441     struct ucs1002_info *info = container_of(work, struct ucs1002_info,
0442                          health_poll.work);
0443     int ret;
0444     u32 reg;
0445 
0446     ret = regmap_read(info->regmap, UCS1002_REG_INTERRUPT_STATUS, &reg);
0447     if (ret)
0448         return;
0449 
0450     /* bad health and no status change, just schedule us again in a while */
0451     if ((reg & F_ERR) && info->health != POWER_SUPPLY_HEALTH_GOOD) {
0452         schedule_delayed_work(&info->health_poll,
0453                       msecs_to_jiffies(2000));
0454         return;
0455     }
0456 
0457     if (reg & F_TSD)
0458         info->health = POWER_SUPPLY_HEALTH_OVERHEAT;
0459     else if (reg & (F_OVER_VOLT | F_BACK_VOLT))
0460         info->health = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
0461     else if (reg & F_OVER_ILIM)
0462         info->health = POWER_SUPPLY_HEALTH_OVERCURRENT;
0463     else if (reg & (F_DISCHARGE_ERR | F_MIN_KEEP_OUT))
0464         info->health = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
0465     else
0466         info->health = POWER_SUPPLY_HEALTH_GOOD;
0467 
0468     sysfs_notify(&info->charger->dev.kobj, NULL, "health");
0469 }
0470 
0471 static irqreturn_t ucs1002_charger_irq(int irq, void *data)
0472 {
0473     int ret, regval;
0474     bool present;
0475     struct ucs1002_info *info = data;
0476 
0477     present = info->present;
0478 
0479     ret = regmap_read(info->regmap, UCS1002_REG_OTHER_STATUS, &regval);
0480     if (ret)
0481         return IRQ_HANDLED;
0482 
0483     /* update attached status */
0484     info->present = regval & F_ADET_PIN;
0485 
0486     /* notify the change */
0487     if (present != info->present)
0488         power_supply_changed(info->charger);
0489 
0490     return IRQ_HANDLED;
0491 }
0492 
0493 static irqreturn_t ucs1002_alert_irq(int irq, void *data)
0494 {
0495     struct ucs1002_info *info = data;
0496 
0497     mod_delayed_work(system_wq, &info->health_poll, 0);
0498 
0499     return IRQ_HANDLED;
0500 }
0501 
0502 static int ucs1002_regulator_enable(struct regulator_dev *rdev)
0503 {
0504     struct ucs1002_info *info = rdev_get_drvdata(rdev);
0505 
0506     /*
0507      * If the output is disabled due to 0 maximum current, just pretend the
0508      * enable did work. The regulator will be enabled as soon as we get a
0509      * a non-zero maximum current budget.
0510      */
0511     if (info->output_disable)
0512         return 0;
0513 
0514     return regulator_enable_regmap(rdev);
0515 }
0516 
0517 static const struct regulator_ops ucs1002_regulator_ops = {
0518     .is_enabled = regulator_is_enabled_regmap,
0519     .enable     = ucs1002_regulator_enable,
0520     .disable    = regulator_disable_regmap,
0521 };
0522 
0523 static const struct regulator_desc ucs1002_regulator_descriptor = {
0524     .name       = "ucs1002-vbus",
0525     .ops        = &ucs1002_regulator_ops,
0526     .type       = REGULATOR_VOLTAGE,
0527     .owner      = THIS_MODULE,
0528     .enable_reg = UCS1002_REG_SWITCH_CFG,
0529     .enable_mask    = F_PWR_EN_SET,
0530     .enable_val = F_PWR_EN_SET,
0531     .fixed_uV   = 5000000,
0532     .n_voltages = 1,
0533 };
0534 
0535 static int ucs1002_probe(struct i2c_client *client,
0536              const struct i2c_device_id *dev_id)
0537 {
0538     struct device *dev = &client->dev;
0539     struct power_supply_config charger_config = {};
0540     const struct regmap_config regmap_config = {
0541         .reg_bits = 8,
0542         .val_bits = 8,
0543     };
0544     struct regulator_config regulator_config = {};
0545     int irq_a_det, irq_alert, ret;
0546     struct ucs1002_info *info;
0547     unsigned int regval;
0548 
0549     info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
0550     if (!info)
0551         return -ENOMEM;
0552 
0553     info->regmap = devm_regmap_init_i2c(client, &regmap_config);
0554     ret = PTR_ERR_OR_ZERO(info->regmap);
0555     if (ret) {
0556         dev_err(dev, "Regmap initialization failed: %d\n", ret);
0557         return ret;
0558     }
0559 
0560     info->client = client;
0561 
0562     irq_a_det = of_irq_get_byname(dev->of_node, "a_det");
0563     irq_alert = of_irq_get_byname(dev->of_node, "alert");
0564 
0565     charger_config.of_node = dev->of_node;
0566     charger_config.drv_data = info;
0567 
0568     ret = regmap_read(info->regmap, UCS1002_REG_PRODUCT_ID, &regval);
0569     if (ret) {
0570         dev_err(dev, "Failed to read product ID: %d\n", ret);
0571         return ret;
0572     }
0573 
0574     if (regval != UCS1002_PRODUCT_ID) {
0575         dev_err(dev,
0576             "Product ID does not match (0x%02x != 0x%02x)\n",
0577             regval, UCS1002_PRODUCT_ID);
0578         return -ENODEV;
0579     }
0580 
0581     /* Enable charge rationing by default */
0582     ret = regmap_update_bits(info->regmap, UCS1002_REG_GENERAL_CFG,
0583                  F_RATION_EN, F_RATION_EN);
0584     if (ret) {
0585         dev_err(dev, "Failed to read general config: %d\n", ret);
0586         return ret;
0587     }
0588 
0589     /*
0590      * Ignore the M1, M2, PWR_EN, and EM_EN pin states. Set active
0591      * mode selection to BC1.2 CDP.
0592      */
0593     ret = regmap_update_bits(info->regmap, UCS1002_REG_SWITCH_CFG,
0594                  V_SET_ACTIVE_MODE_MASK | F_PIN_IGNORE,
0595                  V_SET_ACTIVE_MODE_BC12_CDP | F_PIN_IGNORE);
0596     if (ret) {
0597         dev_err(dev, "Failed to configure default mode: %d\n", ret);
0598         return ret;
0599     }
0600     /*
0601      * Be safe and set initial current limit to 500mA
0602      */
0603     ret = ucs1002_set_max_current(info, 500000);
0604     if (ret) {
0605         dev_err(dev, "Failed to set max current default: %d\n", ret);
0606         return ret;
0607     }
0608 
0609     info->charger = devm_power_supply_register(dev, &ucs1002_charger_desc,
0610                            &charger_config);
0611     ret = PTR_ERR_OR_ZERO(info->charger);
0612     if (ret) {
0613         dev_err(dev, "Failed to register power supply: %d\n", ret);
0614         return ret;
0615     }
0616 
0617     ret = regmap_read(info->regmap, UCS1002_REG_PIN_STATUS, &regval);
0618     if (ret) {
0619         dev_err(dev, "Failed to read pin status: %d\n", ret);
0620         return ret;
0621     }
0622 
0623     info->regulator_descriptor =
0624         devm_kmemdup(dev, &ucs1002_regulator_descriptor,
0625                  sizeof(ucs1002_regulator_descriptor),
0626                  GFP_KERNEL);
0627     if (!info->regulator_descriptor)
0628         return -ENOMEM;
0629 
0630     info->regulator_descriptor->enable_is_inverted = !(regval & F_SEL_PIN);
0631 
0632     regulator_config.dev = dev;
0633     regulator_config.of_node = dev->of_node;
0634     regulator_config.regmap = info->regmap;
0635     regulator_config.driver_data = info;
0636 
0637     info->rdev = devm_regulator_register(dev, info->regulator_descriptor,
0638                        &regulator_config);
0639     ret = PTR_ERR_OR_ZERO(info->rdev);
0640     if (ret) {
0641         dev_err(dev, "Failed to register VBUS regulator: %d\n", ret);
0642         return ret;
0643     }
0644 
0645     info->health = POWER_SUPPLY_HEALTH_GOOD;
0646     INIT_DELAYED_WORK(&info->health_poll, ucs1002_health_poll);
0647 
0648     if (irq_a_det > 0) {
0649         ret = devm_request_threaded_irq(dev, irq_a_det, NULL,
0650                         ucs1002_charger_irq,
0651                         IRQF_ONESHOT,
0652                         "ucs1002-a_det", info);
0653         if (ret) {
0654             dev_err(dev, "Failed to request A_DET threaded irq: %d\n",
0655                 ret);
0656             return ret;
0657         }
0658     }
0659 
0660     if (irq_alert > 0) {
0661         ret = devm_request_irq(dev, irq_alert, ucs1002_alert_irq,
0662                        0,"ucs1002-alert", info);
0663         if (ret) {
0664             dev_err(dev, "Failed to request ALERT threaded irq: %d\n",
0665                 ret);
0666             return ret;
0667         }
0668     }
0669 
0670     return 0;
0671 }
0672 
0673 static const struct of_device_id ucs1002_of_match[] = {
0674     { .compatible = "microchip,ucs1002", },
0675     { /* sentinel */ },
0676 };
0677 MODULE_DEVICE_TABLE(of, ucs1002_of_match);
0678 
0679 static struct i2c_driver ucs1002_driver = {
0680     .driver = {
0681            .name = "ucs1002",
0682            .of_match_table = ucs1002_of_match,
0683     },
0684     .probe = ucs1002_probe,
0685 };
0686 module_i2c_driver(ucs1002_driver);
0687 
0688 MODULE_DESCRIPTION("Microchip UCS1002 Programmable USB Port Power Controller");
0689 MODULE_AUTHOR("Enric Balletbo Serra <enric.balletbo@collabora.com>");
0690 MODULE_AUTHOR("Andrey Smirnov <andrew.smirnov@gmail.com>");
0691 MODULE_LICENSE("GPL");