0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #include <linux/module.h>
0013 #include <linux/init.h>
0014 #include <linux/delay.h>
0015 #include <linux/i2c.h>
0016 #include <linux/interrupt.h>
0017 #include <linux/slab.h>
0018 #include <linux/of_device.h>
0019 #include <linux/workqueue.h>
0020 #include <linux/power_supply.h>
0021 #include <linux/devm-helpers.h>
0022
0023 #define MAX14656_MANUFACTURER "Maxim Integrated"
0024 #define MAX14656_NAME "max14656"
0025
0026 #define MAX14656_DEVICE_ID 0x00
0027 #define MAX14656_INTERRUPT_1 0x01
0028 #define MAX14656_INTERRUPT_2 0x02
0029 #define MAX14656_STATUS_1 0x03
0030 #define MAX14656_STATUS_2 0x04
0031 #define MAX14656_INTMASK_1 0x05
0032 #define MAX14656_INTMASK_2 0x06
0033 #define MAX14656_CONTROL_1 0x07
0034 #define MAX14656_CONTROL_2 0x08
0035 #define MAX14656_CONTROL_3 0x09
0036
0037 #define DEVICE_VENDOR_MASK 0xf0
0038 #define DEVICE_REV_MASK 0x0f
0039 #define INT_EN_REG_MASK BIT(4)
0040 #define CHG_TYPE_INT_MASK BIT(0)
0041 #define STATUS1_VB_VALID_MASK BIT(4)
0042 #define STATUS1_CHG_TYPE_MASK 0xf
0043 #define INT1_DCD_TIMEOUT_MASK BIT(7)
0044 #define CONTROL1_DEFAULT 0x0d
0045 #define CONTROL1_INT_EN BIT(4)
0046 #define CONTROL1_INT_ACTIVE_HIGH BIT(5)
0047 #define CONTROL1_EDGE BIT(7)
0048 #define CONTROL2_DEFAULT 0x8e
0049 #define CONTROL2_ADC_EN BIT(0)
0050 #define CONTROL3_DEFAULT 0x8d
0051
0052 enum max14656_chg_type {
0053 MAX14656_NO_CHARGER = 0,
0054 MAX14656_SDP_CHARGER,
0055 MAX14656_CDP_CHARGER,
0056 MAX14656_DCP_CHARGER,
0057 MAX14656_APPLE_500MA_CHARGER,
0058 MAX14656_APPLE_1A_CHARGER,
0059 MAX14656_APPLE_2A_CHARGER,
0060 MAX14656_SPECIAL_500MA_CHARGER,
0061 MAX14656_APPLE_12W,
0062 MAX14656_CHARGER_LAST
0063 };
0064
0065 static const struct max14656_chg_type_props {
0066 enum power_supply_type type;
0067 } chg_type_props[] = {
0068 { POWER_SUPPLY_TYPE_UNKNOWN },
0069 { POWER_SUPPLY_TYPE_USB },
0070 { POWER_SUPPLY_TYPE_USB_CDP },
0071 { POWER_SUPPLY_TYPE_USB_DCP },
0072 { POWER_SUPPLY_TYPE_USB_DCP },
0073 { POWER_SUPPLY_TYPE_USB_DCP },
0074 { POWER_SUPPLY_TYPE_USB_DCP },
0075 { POWER_SUPPLY_TYPE_USB_DCP },
0076 { POWER_SUPPLY_TYPE_USB },
0077 };
0078
0079 struct max14656_chip {
0080 struct i2c_client *client;
0081 struct power_supply *detect_psy;
0082 struct power_supply_desc psy_desc;
0083 struct delayed_work irq_work;
0084
0085 int irq;
0086 int online;
0087 };
0088
0089 static int max14656_read_reg(struct i2c_client *client, int reg, u8 *val)
0090 {
0091 s32 ret;
0092
0093 ret = i2c_smbus_read_byte_data(client, reg);
0094 if (ret < 0) {
0095 dev_err(&client->dev,
0096 "i2c read fail: can't read from %02x: %d\n",
0097 reg, ret);
0098 return ret;
0099 }
0100 *val = ret;
0101 return 0;
0102 }
0103
0104 static int max14656_write_reg(struct i2c_client *client, int reg, u8 val)
0105 {
0106 s32 ret;
0107
0108 ret = i2c_smbus_write_byte_data(client, reg, val);
0109 if (ret < 0) {
0110 dev_err(&client->dev,
0111 "i2c write fail: can't write %02x to %02x: %d\n",
0112 val, reg, ret);
0113 return ret;
0114 }
0115 return 0;
0116 }
0117
0118 static int max14656_read_block_reg(struct i2c_client *client, u8 reg,
0119 u8 length, u8 *val)
0120 {
0121 int ret;
0122
0123 ret = i2c_smbus_read_i2c_block_data(client, reg, length, val);
0124 if (ret < 0) {
0125 dev_err(&client->dev, "failed to block read reg 0x%x: %d\n",
0126 reg, ret);
0127 return ret;
0128 }
0129
0130 return 0;
0131 }
0132
0133 #define REG_TOTAL_NUM 5
0134 static void max14656_irq_worker(struct work_struct *work)
0135 {
0136 struct max14656_chip *chip =
0137 container_of(work, struct max14656_chip, irq_work.work);
0138
0139 u8 buf[REG_TOTAL_NUM];
0140 u8 chg_type;
0141
0142 max14656_read_block_reg(chip->client, MAX14656_DEVICE_ID,
0143 REG_TOTAL_NUM, buf);
0144
0145 if ((buf[MAX14656_STATUS_1] & STATUS1_VB_VALID_MASK) &&
0146 (buf[MAX14656_STATUS_1] & STATUS1_CHG_TYPE_MASK)) {
0147 chg_type = buf[MAX14656_STATUS_1] & STATUS1_CHG_TYPE_MASK;
0148 if (chg_type < MAX14656_CHARGER_LAST)
0149 chip->psy_desc.type = chg_type_props[chg_type].type;
0150 else
0151 chip->psy_desc.type = POWER_SUPPLY_TYPE_UNKNOWN;
0152 chip->online = 1;
0153 } else {
0154 chip->online = 0;
0155 chip->psy_desc.type = POWER_SUPPLY_TYPE_UNKNOWN;
0156 }
0157
0158 power_supply_changed(chip->detect_psy);
0159 }
0160
0161 static irqreturn_t max14656_irq(int irq, void *dev_id)
0162 {
0163 struct max14656_chip *chip = dev_id;
0164
0165 schedule_delayed_work(&chip->irq_work, msecs_to_jiffies(100));
0166
0167 return IRQ_HANDLED;
0168 }
0169
0170 static int max14656_hw_init(struct max14656_chip *chip)
0171 {
0172 uint8_t val = 0;
0173 uint8_t rev;
0174 struct i2c_client *client = chip->client;
0175
0176 if (max14656_read_reg(client, MAX14656_DEVICE_ID, &val))
0177 return -ENODEV;
0178
0179 if ((val & DEVICE_VENDOR_MASK) != 0x20) {
0180 dev_err(&client->dev, "wrong vendor ID %d\n",
0181 ((val & DEVICE_VENDOR_MASK) >> 4));
0182 return -ENODEV;
0183 }
0184 rev = val & DEVICE_REV_MASK;
0185
0186
0187 if (max14656_write_reg(client, MAX14656_CONTROL_2, CONTROL2_ADC_EN))
0188 return -EINVAL;
0189
0190
0191 if (max14656_write_reg(client, MAX14656_CONTROL_1,
0192 CONTROL1_DEFAULT |
0193 CONTROL1_INT_EN |
0194 CONTROL1_INT_ACTIVE_HIGH |
0195 CONTROL1_EDGE))
0196 return -EINVAL;
0197
0198 if (max14656_write_reg(client, MAX14656_INTMASK_1, 0x3))
0199 return -EINVAL;
0200
0201 if (max14656_write_reg(client, MAX14656_INTMASK_2, 0x1))
0202 return -EINVAL;
0203
0204 dev_info(&client->dev, "detected revision %d\n", rev);
0205 return 0;
0206 }
0207
0208 static int max14656_get_property(struct power_supply *psy,
0209 enum power_supply_property psp,
0210 union power_supply_propval *val)
0211 {
0212 struct max14656_chip *chip = power_supply_get_drvdata(psy);
0213
0214 switch (psp) {
0215 case POWER_SUPPLY_PROP_ONLINE:
0216 val->intval = chip->online;
0217 break;
0218 case POWER_SUPPLY_PROP_MODEL_NAME:
0219 val->strval = MAX14656_NAME;
0220 break;
0221 case POWER_SUPPLY_PROP_MANUFACTURER:
0222 val->strval = MAX14656_MANUFACTURER;
0223 break;
0224 default:
0225 return -EINVAL;
0226 }
0227
0228 return 0;
0229 }
0230
0231 static enum power_supply_property max14656_battery_props[] = {
0232 POWER_SUPPLY_PROP_ONLINE,
0233 POWER_SUPPLY_PROP_MODEL_NAME,
0234 POWER_SUPPLY_PROP_MANUFACTURER,
0235 };
0236
0237 static int max14656_probe(struct i2c_client *client,
0238 const struct i2c_device_id *id)
0239 {
0240 struct i2c_adapter *adapter = client->adapter;
0241 struct device *dev = &client->dev;
0242 struct power_supply_config psy_cfg = {};
0243 struct max14656_chip *chip;
0244 int irq = client->irq;
0245 int ret = 0;
0246
0247 if (irq <= 0) {
0248 dev_err(dev, "invalid irq number: %d\n", irq);
0249 return -ENODEV;
0250 }
0251
0252 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
0253 dev_err(dev, "No support for SMBUS_BYTE_DATA\n");
0254 return -ENODEV;
0255 }
0256
0257 chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
0258 if (!chip)
0259 return -ENOMEM;
0260
0261 psy_cfg.drv_data = chip;
0262 chip->client = client;
0263 chip->online = 0;
0264 chip->psy_desc.name = MAX14656_NAME;
0265 chip->psy_desc.type = POWER_SUPPLY_TYPE_UNKNOWN;
0266 chip->psy_desc.properties = max14656_battery_props;
0267 chip->psy_desc.num_properties = ARRAY_SIZE(max14656_battery_props);
0268 chip->psy_desc.get_property = max14656_get_property;
0269 chip->irq = irq;
0270
0271 ret = max14656_hw_init(chip);
0272 if (ret)
0273 return -ENODEV;
0274
0275 chip->detect_psy = devm_power_supply_register(dev,
0276 &chip->psy_desc, &psy_cfg);
0277 if (IS_ERR(chip->detect_psy)) {
0278 dev_err(dev, "power_supply_register failed\n");
0279 return -EINVAL;
0280 }
0281
0282 ret = devm_delayed_work_autocancel(dev, &chip->irq_work,
0283 max14656_irq_worker);
0284 if (ret) {
0285 dev_err(dev, "devm_delayed_work_autocancel %d failed\n", ret);
0286 return ret;
0287 }
0288
0289 ret = devm_request_irq(dev, chip->irq, max14656_irq,
0290 IRQF_TRIGGER_FALLING,
0291 MAX14656_NAME, chip);
0292 if (ret) {
0293 dev_err(dev, "request_irq %d failed\n", chip->irq);
0294 return -EINVAL;
0295 }
0296 enable_irq_wake(chip->irq);
0297
0298 schedule_delayed_work(&chip->irq_work, msecs_to_jiffies(2000));
0299
0300 return 0;
0301 }
0302
0303 static const struct i2c_device_id max14656_id[] = {
0304 { "max14656", 0 },
0305 {}
0306 };
0307 MODULE_DEVICE_TABLE(i2c, max14656_id);
0308
0309 static const struct of_device_id max14656_match_table[] = {
0310 { .compatible = "maxim,max14656", },
0311 {}
0312 };
0313 MODULE_DEVICE_TABLE(of, max14656_match_table);
0314
0315 static struct i2c_driver max14656_i2c_driver = {
0316 .driver = {
0317 .name = "max14656",
0318 .of_match_table = max14656_match_table,
0319 },
0320 .probe = max14656_probe,
0321 .id_table = max14656_id,
0322 };
0323 module_i2c_driver(max14656_i2c_driver);
0324
0325 MODULE_DESCRIPTION("MAX14656 USB charger detector");
0326 MODULE_LICENSE("GPL v2");