^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) //
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) // MFD core driver for the Maxim MAX77843
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) //
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) // Copyright (C) 2015 Samsung Electronics
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) // Author: Jaewon Kim <jaewon02.kim@samsung.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) // Author: Beomho Seo <beomho.seo@samsung.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/mfd/core.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/mfd/max77693-common.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/mfd/max77843-private.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/of_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) static const struct mfd_cell max77843_devs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) .name = "max77843-muic",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) .of_compatible = "maxim,max77843-muic",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) }, {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) .name = "max77843-regulator",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) .of_compatible = "maxim,max77843-regulator",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) }, {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) .name = "max77843-charger",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) .of_compatible = "maxim,max77843-charger"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) }, {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) .name = "max77843-fuelgauge",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) .of_compatible = "maxim,max77843-fuelgauge",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) }, {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) .name = "max77843-haptic",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) .of_compatible = "maxim,max77843-haptic",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) static const struct regmap_config max77843_charger_regmap_config = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) .reg_bits = 8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) .val_bits = 8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) .max_register = MAX77843_CHG_REG_END,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) static const struct regmap_config max77843_regmap_config = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) .reg_bits = 8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) .val_bits = 8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) .max_register = MAX77843_SYS_REG_END,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) static const struct regmap_irq max77843_irqs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) /* TOPSYS interrupts */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) { .reg_offset = 0, .mask = MAX77843_SYS_IRQ_SYSUVLO_INT, },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) { .reg_offset = 0, .mask = MAX77843_SYS_IRQ_SYSOVLO_INT, },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) { .reg_offset = 0, .mask = MAX77843_SYS_IRQ_TSHDN_INT, },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) { .reg_offset = 0, .mask = MAX77843_SYS_IRQ_TM_INT, },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) static const struct regmap_irq_chip max77843_irq_chip = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) .name = "max77843",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) .status_base = MAX77843_SYS_REG_SYSINTSRC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) .mask_base = MAX77843_SYS_REG_SYSINTMASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) .mask_invert = false,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) .num_regs = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) .irqs = max77843_irqs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) .num_irqs = ARRAY_SIZE(max77843_irqs),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) /* Charger and Charger regulator use same regmap. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) static int max77843_chg_init(struct max77693_dev *max77843)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) max77843->i2c_chg = i2c_new_dummy_device(max77843->i2c->adapter, I2C_ADDR_CHG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) if (IS_ERR(max77843->i2c_chg)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) dev_err(&max77843->i2c->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) "Cannot allocate I2C device for Charger\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) return PTR_ERR(max77843->i2c_chg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) i2c_set_clientdata(max77843->i2c_chg, max77843);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) max77843->regmap_chg = devm_regmap_init_i2c(max77843->i2c_chg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) &max77843_charger_regmap_config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) if (IS_ERR(max77843->regmap_chg)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) ret = PTR_ERR(max77843->regmap_chg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) goto err_chg_i2c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) err_chg_i2c:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) i2c_unregister_device(max77843->i2c_chg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) static int max77843_probe(struct i2c_client *i2c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) const struct i2c_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) struct max77693_dev *max77843;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) unsigned int reg_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) max77843 = devm_kzalloc(&i2c->dev, sizeof(*max77843), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) if (!max77843)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) i2c_set_clientdata(i2c, max77843);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) max77843->dev = &i2c->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) max77843->i2c = i2c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) max77843->irq = i2c->irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) max77843->type = id->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) max77843->regmap = devm_regmap_init_i2c(i2c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) &max77843_regmap_config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) if (IS_ERR(max77843->regmap)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) dev_err(&i2c->dev, "Failed to allocate topsys register map\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) return PTR_ERR(max77843->regmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) ret = regmap_add_irq_chip(max77843->regmap, max77843->irq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) IRQF_TRIGGER_LOW | IRQF_ONESHOT | IRQF_SHARED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 0, &max77843_irq_chip, &max77843->irq_data_topsys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) dev_err(&i2c->dev, "Failed to add TOPSYS IRQ chip\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) ret = regmap_read(max77843->regmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) MAX77843_SYS_REG_PMICID, ®_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) dev_err(&i2c->dev, "Failed to read PMIC ID\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) goto err_pmic_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) dev_info(&i2c->dev, "device ID: 0x%x\n", reg_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) ret = max77843_chg_init(max77843);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) dev_err(&i2c->dev, "Failed to init Charger\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) goto err_pmic_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) ret = regmap_update_bits(max77843->regmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) MAX77843_SYS_REG_INTSRCMASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) MAX77843_INTSRC_MASK_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) (unsigned int)~MAX77843_INTSRC_MASK_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) dev_err(&i2c->dev, "Failed to unmask interrupt source\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) goto err_pmic_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) ret = mfd_add_devices(max77843->dev, -1, max77843_devs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) ARRAY_SIZE(max77843_devs), NULL, 0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) dev_err(&i2c->dev, "Failed to add mfd device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) goto err_pmic_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) device_init_wakeup(max77843->dev, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) err_pmic_id:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) regmap_del_irq_chip(max77843->irq, max77843->irq_data_topsys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) static const struct of_device_id max77843_dt_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) { .compatible = "maxim,max77843", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) { },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) static const struct i2c_device_id max77843_id[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) { "max77843", TYPE_MAX77843, },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) { },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) static int __maybe_unused max77843_suspend(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) struct i2c_client *i2c = to_i2c_client(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) struct max77693_dev *max77843 = i2c_get_clientdata(i2c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) disable_irq(max77843->irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) if (device_may_wakeup(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) enable_irq_wake(max77843->irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) static int __maybe_unused max77843_resume(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) struct i2c_client *i2c = to_i2c_client(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) struct max77693_dev *max77843 = i2c_get_clientdata(i2c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) if (device_may_wakeup(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) disable_irq_wake(max77843->irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) enable_irq(max77843->irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) static SIMPLE_DEV_PM_OPS(max77843_pm, max77843_suspend, max77843_resume);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) static struct i2c_driver max77843_i2c_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) .name = "max77843",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) .pm = &max77843_pm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) .of_match_table = max77843_dt_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) .suppress_bind_attrs = true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) .probe = max77843_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) .id_table = max77843_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) static int __init max77843_i2c_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) return i2c_add_driver(&max77843_i2c_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) subsys_initcall(max77843_i2c_init);