0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #include <linux/module.h>
0013 #include <linux/slab.h>
0014 #include <linux/i2c.h>
0015 #include <linux/err.h>
0016 #include <linux/interrupt.h>
0017 #include <linux/of.h>
0018 #include <linux/pm_runtime.h>
0019 #include <linux/mutex.h>
0020 #include <linux/mfd/core.h>
0021 #include <linux/mfd/max77693.h>
0022 #include <linux/mfd/max77693-common.h>
0023 #include <linux/mfd/max77693-private.h>
0024 #include <linux/regulator/machine.h>
0025 #include <linux/regmap.h>
0026
0027 #define I2C_ADDR_PMIC (0xCC >> 1)
0028 #define I2C_ADDR_MUIC (0x4A >> 1)
0029 #define I2C_ADDR_HAPTIC (0x90 >> 1)
0030
0031 static const struct mfd_cell max77693_devs[] = {
0032 { .name = "max77693-pmic", },
0033 {
0034 .name = "max77693-charger",
0035 .of_compatible = "maxim,max77693-charger",
0036 },
0037 {
0038 .name = "max77693-muic",
0039 .of_compatible = "maxim,max77693-muic",
0040 },
0041 {
0042 .name = "max77693-haptic",
0043 .of_compatible = "maxim,max77693-haptic",
0044 },
0045 {
0046 .name = "max77693-led",
0047 .of_compatible = "maxim,max77693-led",
0048 },
0049 };
0050
0051 static const struct regmap_config max77693_regmap_config = {
0052 .reg_bits = 8,
0053 .val_bits = 8,
0054 .max_register = MAX77693_PMIC_REG_END,
0055 };
0056
0057 static const struct regmap_irq max77693_led_irqs[] = {
0058 { .mask = LED_IRQ_FLED2_OPEN, },
0059 { .mask = LED_IRQ_FLED2_SHORT, },
0060 { .mask = LED_IRQ_FLED1_OPEN, },
0061 { .mask = LED_IRQ_FLED1_SHORT, },
0062 { .mask = LED_IRQ_MAX_FLASH, },
0063 };
0064
0065 static const struct regmap_irq_chip max77693_led_irq_chip = {
0066 .name = "max77693-led",
0067 .status_base = MAX77693_LED_REG_FLASH_INT,
0068 .mask_base = MAX77693_LED_REG_FLASH_INT_MASK,
0069 .mask_invert = false,
0070 .num_regs = 1,
0071 .irqs = max77693_led_irqs,
0072 .num_irqs = ARRAY_SIZE(max77693_led_irqs),
0073 };
0074
0075 static const struct regmap_irq max77693_topsys_irqs[] = {
0076 { .mask = TOPSYS_IRQ_T120C_INT, },
0077 { .mask = TOPSYS_IRQ_T140C_INT, },
0078 { .mask = TOPSYS_IRQ_LOWSYS_INT, },
0079 };
0080
0081 static const struct regmap_irq_chip max77693_topsys_irq_chip = {
0082 .name = "max77693-topsys",
0083 .status_base = MAX77693_PMIC_REG_TOPSYS_INT,
0084 .mask_base = MAX77693_PMIC_REG_TOPSYS_INT_MASK,
0085 .mask_invert = false,
0086 .num_regs = 1,
0087 .irqs = max77693_topsys_irqs,
0088 .num_irqs = ARRAY_SIZE(max77693_topsys_irqs),
0089 };
0090
0091 static const struct regmap_irq max77693_charger_irqs[] = {
0092 { .mask = CHG_IRQ_BYP_I, },
0093 { .mask = CHG_IRQ_THM_I, },
0094 { .mask = CHG_IRQ_BAT_I, },
0095 { .mask = CHG_IRQ_CHG_I, },
0096 { .mask = CHG_IRQ_CHGIN_I, },
0097 };
0098
0099 static const struct regmap_irq_chip max77693_charger_irq_chip = {
0100 .name = "max77693-charger",
0101 .status_base = MAX77693_CHG_REG_CHG_INT,
0102 .mask_base = MAX77693_CHG_REG_CHG_INT_MASK,
0103 .mask_invert = false,
0104 .num_regs = 1,
0105 .irqs = max77693_charger_irqs,
0106 .num_irqs = ARRAY_SIZE(max77693_charger_irqs),
0107 };
0108
0109 static const struct regmap_config max77693_regmap_muic_config = {
0110 .reg_bits = 8,
0111 .val_bits = 8,
0112 .max_register = MAX77693_MUIC_REG_END,
0113 };
0114
0115 static const struct regmap_irq max77693_muic_irqs[] = {
0116 { .reg_offset = 0, .mask = MUIC_IRQ_INT1_ADC, },
0117 { .reg_offset = 0, .mask = MUIC_IRQ_INT1_ADC_LOW, },
0118 { .reg_offset = 0, .mask = MUIC_IRQ_INT1_ADC_ERR, },
0119 { .reg_offset = 0, .mask = MUIC_IRQ_INT1_ADC1K, },
0120
0121 { .reg_offset = 1, .mask = MUIC_IRQ_INT2_CHGTYP, },
0122 { .reg_offset = 1, .mask = MUIC_IRQ_INT2_CHGDETREUN, },
0123 { .reg_offset = 1, .mask = MUIC_IRQ_INT2_DCDTMR, },
0124 { .reg_offset = 1, .mask = MUIC_IRQ_INT2_DXOVP, },
0125 { .reg_offset = 1, .mask = MUIC_IRQ_INT2_VBVOLT, },
0126 { .reg_offset = 1, .mask = MUIC_IRQ_INT2_VIDRM, },
0127
0128 { .reg_offset = 2, .mask = MUIC_IRQ_INT3_EOC, },
0129 { .reg_offset = 2, .mask = MUIC_IRQ_INT3_CGMBC, },
0130 { .reg_offset = 2, .mask = MUIC_IRQ_INT3_OVP, },
0131 { .reg_offset = 2, .mask = MUIC_IRQ_INT3_MBCCHG_ERR, },
0132 { .reg_offset = 2, .mask = MUIC_IRQ_INT3_CHG_ENABLED, },
0133 { .reg_offset = 2, .mask = MUIC_IRQ_INT3_BAT_DET, },
0134 };
0135
0136 static const struct regmap_irq_chip max77693_muic_irq_chip = {
0137 .name = "max77693-muic",
0138 .status_base = MAX77693_MUIC_REG_INT1,
0139 .mask_base = MAX77693_MUIC_REG_INTMASK1,
0140 .mask_invert = true,
0141 .num_regs = 3,
0142 .irqs = max77693_muic_irqs,
0143 .num_irqs = ARRAY_SIZE(max77693_muic_irqs),
0144 };
0145
0146 static const struct regmap_config max77693_regmap_haptic_config = {
0147 .reg_bits = 8,
0148 .val_bits = 8,
0149 .max_register = MAX77693_HAPTIC_REG_END,
0150 };
0151
0152 static int max77693_i2c_probe(struct i2c_client *i2c,
0153 const struct i2c_device_id *id)
0154 {
0155 struct max77693_dev *max77693;
0156 unsigned int reg_data;
0157 int ret = 0;
0158
0159 max77693 = devm_kzalloc(&i2c->dev,
0160 sizeof(struct max77693_dev), GFP_KERNEL);
0161 if (max77693 == NULL)
0162 return -ENOMEM;
0163
0164 i2c_set_clientdata(i2c, max77693);
0165 max77693->dev = &i2c->dev;
0166 max77693->i2c = i2c;
0167 max77693->irq = i2c->irq;
0168 max77693->type = id->driver_data;
0169
0170 max77693->regmap = devm_regmap_init_i2c(i2c, &max77693_regmap_config);
0171 if (IS_ERR(max77693->regmap)) {
0172 ret = PTR_ERR(max77693->regmap);
0173 dev_err(max77693->dev, "failed to allocate register map: %d\n",
0174 ret);
0175 return ret;
0176 }
0177
0178 ret = regmap_read(max77693->regmap, MAX77693_PMIC_REG_PMIC_ID2,
0179 ®_data);
0180 if (ret < 0) {
0181 dev_err(max77693->dev, "device not found on this channel\n");
0182 return ret;
0183 } else
0184 dev_info(max77693->dev, "device ID: 0x%x\n", reg_data);
0185
0186 max77693->i2c_muic = i2c_new_dummy_device(i2c->adapter, I2C_ADDR_MUIC);
0187 if (IS_ERR(max77693->i2c_muic)) {
0188 dev_err(max77693->dev, "Failed to allocate I2C device for MUIC\n");
0189 return PTR_ERR(max77693->i2c_muic);
0190 }
0191 i2c_set_clientdata(max77693->i2c_muic, max77693);
0192
0193 max77693->i2c_haptic = i2c_new_dummy_device(i2c->adapter, I2C_ADDR_HAPTIC);
0194 if (IS_ERR(max77693->i2c_haptic)) {
0195 dev_err(max77693->dev, "Failed to allocate I2C device for Haptic\n");
0196 ret = PTR_ERR(max77693->i2c_haptic);
0197 goto err_i2c_haptic;
0198 }
0199 i2c_set_clientdata(max77693->i2c_haptic, max77693);
0200
0201 max77693->regmap_haptic = devm_regmap_init_i2c(max77693->i2c_haptic,
0202 &max77693_regmap_haptic_config);
0203 if (IS_ERR(max77693->regmap_haptic)) {
0204 ret = PTR_ERR(max77693->regmap_haptic);
0205 dev_err(max77693->dev,
0206 "failed to initialize haptic register map: %d\n", ret);
0207 goto err_regmap;
0208 }
0209
0210
0211
0212
0213
0214
0215 max77693->regmap_muic = devm_regmap_init_i2c(max77693->i2c_muic,
0216 &max77693_regmap_muic_config);
0217 if (IS_ERR(max77693->regmap_muic)) {
0218 ret = PTR_ERR(max77693->regmap_muic);
0219 dev_err(max77693->dev,
0220 "failed to allocate register map: %d\n", ret);
0221 goto err_regmap;
0222 }
0223
0224 ret = regmap_add_irq_chip(max77693->regmap, max77693->irq,
0225 IRQF_ONESHOT | IRQF_SHARED, 0,
0226 &max77693_led_irq_chip,
0227 &max77693->irq_data_led);
0228 if (ret) {
0229 dev_err(max77693->dev, "failed to add irq chip: %d\n", ret);
0230 goto err_regmap;
0231 }
0232
0233 ret = regmap_add_irq_chip(max77693->regmap, max77693->irq,
0234 IRQF_ONESHOT | IRQF_SHARED, 0,
0235 &max77693_topsys_irq_chip,
0236 &max77693->irq_data_topsys);
0237 if (ret) {
0238 dev_err(max77693->dev, "failed to add irq chip: %d\n", ret);
0239 goto err_irq_topsys;
0240 }
0241
0242 ret = regmap_add_irq_chip(max77693->regmap, max77693->irq,
0243 IRQF_ONESHOT | IRQF_SHARED, 0,
0244 &max77693_charger_irq_chip,
0245 &max77693->irq_data_chg);
0246 if (ret) {
0247 dev_err(max77693->dev, "failed to add irq chip: %d\n", ret);
0248 goto err_irq_charger;
0249 }
0250
0251 ret = regmap_add_irq_chip(max77693->regmap_muic, max77693->irq,
0252 IRQF_ONESHOT | IRQF_SHARED, 0,
0253 &max77693_muic_irq_chip,
0254 &max77693->irq_data_muic);
0255 if (ret) {
0256 dev_err(max77693->dev, "failed to add irq chip: %d\n", ret);
0257 goto err_irq_muic;
0258 }
0259
0260
0261 ret = regmap_update_bits(max77693->regmap,
0262 MAX77693_PMIC_REG_INTSRC_MASK,
0263 SRC_IRQ_ALL, (unsigned int)~SRC_IRQ_ALL);
0264 if (ret < 0) {
0265 dev_err(max77693->dev,
0266 "Could not unmask interrupts in INTSRC: %d\n",
0267 ret);
0268 goto err_intsrc;
0269 }
0270
0271 pm_runtime_set_active(max77693->dev);
0272
0273 ret = mfd_add_devices(max77693->dev, -1, max77693_devs,
0274 ARRAY_SIZE(max77693_devs), NULL, 0, NULL);
0275 if (ret < 0)
0276 goto err_mfd;
0277
0278 return ret;
0279
0280 err_mfd:
0281 mfd_remove_devices(max77693->dev);
0282 err_intsrc:
0283 regmap_del_irq_chip(max77693->irq, max77693->irq_data_muic);
0284 err_irq_muic:
0285 regmap_del_irq_chip(max77693->irq, max77693->irq_data_chg);
0286 err_irq_charger:
0287 regmap_del_irq_chip(max77693->irq, max77693->irq_data_topsys);
0288 err_irq_topsys:
0289 regmap_del_irq_chip(max77693->irq, max77693->irq_data_led);
0290 err_regmap:
0291 i2c_unregister_device(max77693->i2c_haptic);
0292 err_i2c_haptic:
0293 i2c_unregister_device(max77693->i2c_muic);
0294 return ret;
0295 }
0296
0297 static int max77693_i2c_remove(struct i2c_client *i2c)
0298 {
0299 struct max77693_dev *max77693 = i2c_get_clientdata(i2c);
0300
0301 mfd_remove_devices(max77693->dev);
0302
0303 regmap_del_irq_chip(max77693->irq, max77693->irq_data_muic);
0304 regmap_del_irq_chip(max77693->irq, max77693->irq_data_chg);
0305 regmap_del_irq_chip(max77693->irq, max77693->irq_data_topsys);
0306 regmap_del_irq_chip(max77693->irq, max77693->irq_data_led);
0307
0308 i2c_unregister_device(max77693->i2c_muic);
0309 i2c_unregister_device(max77693->i2c_haptic);
0310
0311 return 0;
0312 }
0313
0314 static const struct i2c_device_id max77693_i2c_id[] = {
0315 { "max77693", TYPE_MAX77693 },
0316 { }
0317 };
0318 MODULE_DEVICE_TABLE(i2c, max77693_i2c_id);
0319
0320 static int max77693_suspend(struct device *dev)
0321 {
0322 struct i2c_client *i2c = to_i2c_client(dev);
0323 struct max77693_dev *max77693 = i2c_get_clientdata(i2c);
0324
0325 if (device_may_wakeup(dev)) {
0326 enable_irq_wake(max77693->irq);
0327 disable_irq(max77693->irq);
0328 }
0329
0330 return 0;
0331 }
0332
0333 static int max77693_resume(struct device *dev)
0334 {
0335 struct i2c_client *i2c = to_i2c_client(dev);
0336 struct max77693_dev *max77693 = i2c_get_clientdata(i2c);
0337
0338 if (device_may_wakeup(dev)) {
0339 disable_irq_wake(max77693->irq);
0340 enable_irq(max77693->irq);
0341 }
0342
0343 return 0;
0344 }
0345
0346 static const struct dev_pm_ops max77693_pm = {
0347 .suspend = max77693_suspend,
0348 .resume = max77693_resume,
0349 };
0350
0351 #ifdef CONFIG_OF
0352 static const struct of_device_id max77693_dt_match[] = {
0353 { .compatible = "maxim,max77693" },
0354 {},
0355 };
0356 MODULE_DEVICE_TABLE(of, max77693_dt_match);
0357 #endif
0358
0359 static struct i2c_driver max77693_i2c_driver = {
0360 .driver = {
0361 .name = "max77693",
0362 .pm = &max77693_pm,
0363 .of_match_table = of_match_ptr(max77693_dt_match),
0364 },
0365 .probe = max77693_i2c_probe,
0366 .remove = max77693_i2c_remove,
0367 .id_table = max77693_i2c_id,
0368 };
0369
0370 module_i2c_driver(max77693_i2c_driver);
0371
0372 MODULE_DESCRIPTION("MAXIM 77693 multi-function core driver");
0373 MODULE_AUTHOR("SangYoung, Son <hello.son@samsung.com>");
0374 MODULE_LICENSE("GPL");