Linux 4.19.133
[linux/fpc-iii.git] / drivers / mfd / max77843.c
blobda9612dbb2227d5aa4a7dfb81b11877202de2f3a
1 /*
2 * MFD core driver for the Maxim MAX77843
4 * Copyright (C) 2015 Samsung Electronics
5 * Author: Jaewon Kim <jaewon02.kim@samsung.com>
6 * Author: Beomho Seo <beomho.seo@samsung.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
14 #include <linux/err.h>
15 #include <linux/i2c.h>
16 #include <linux/init.h>
17 #include <linux/interrupt.h>
18 #include <linux/mfd/core.h>
19 #include <linux/mfd/max77693-common.h>
20 #include <linux/mfd/max77843-private.h>
21 #include <linux/of_device.h>
22 #include <linux/platform_device.h>
24 static const struct mfd_cell max77843_devs[] = {
26 .name = "max77843-muic",
27 .of_compatible = "maxim,max77843-muic",
28 }, {
29 .name = "max77843-regulator",
30 .of_compatible = "maxim,max77843-regulator",
31 }, {
32 .name = "max77843-charger",
33 .of_compatible = "maxim,max77843-charger"
34 }, {
35 .name = "max77843-fuelgauge",
36 .of_compatible = "maxim,max77843-fuelgauge",
37 }, {
38 .name = "max77843-haptic",
39 .of_compatible = "maxim,max77843-haptic",
43 static const struct regmap_config max77843_charger_regmap_config = {
44 .reg_bits = 8,
45 .val_bits = 8,
46 .max_register = MAX77843_CHG_REG_END,
49 static const struct regmap_config max77843_regmap_config = {
50 .reg_bits = 8,
51 .val_bits = 8,
52 .max_register = MAX77843_SYS_REG_END,
55 static const struct regmap_irq max77843_irqs[] = {
56 /* TOPSYS interrupts */
57 { .reg_offset = 0, .mask = MAX77843_SYS_IRQ_SYSUVLO_INT, },
58 { .reg_offset = 0, .mask = MAX77843_SYS_IRQ_SYSOVLO_INT, },
59 { .reg_offset = 0, .mask = MAX77843_SYS_IRQ_TSHDN_INT, },
60 { .reg_offset = 0, .mask = MAX77843_SYS_IRQ_TM_INT, },
63 static const struct regmap_irq_chip max77843_irq_chip = {
64 .name = "max77843",
65 .status_base = MAX77843_SYS_REG_SYSINTSRC,
66 .mask_base = MAX77843_SYS_REG_SYSINTMASK,
67 .mask_invert = false,
68 .num_regs = 1,
69 .irqs = max77843_irqs,
70 .num_irqs = ARRAY_SIZE(max77843_irqs),
73 /* Charger and Charger regulator use same regmap. */
74 static int max77843_chg_init(struct max77693_dev *max77843)
76 int ret;
78 max77843->i2c_chg = i2c_new_dummy(max77843->i2c->adapter, I2C_ADDR_CHG);
79 if (!max77843->i2c_chg) {
80 dev_err(&max77843->i2c->dev,
81 "Cannot allocate I2C device for Charger\n");
82 return -ENODEV;
84 i2c_set_clientdata(max77843->i2c_chg, max77843);
86 max77843->regmap_chg = devm_regmap_init_i2c(max77843->i2c_chg,
87 &max77843_charger_regmap_config);
88 if (IS_ERR(max77843->regmap_chg)) {
89 ret = PTR_ERR(max77843->regmap_chg);
90 goto err_chg_i2c;
93 return 0;
95 err_chg_i2c:
96 i2c_unregister_device(max77843->i2c_chg);
98 return ret;
101 static int max77843_probe(struct i2c_client *i2c,
102 const struct i2c_device_id *id)
104 struct max77693_dev *max77843;
105 unsigned int reg_data;
106 int ret;
108 max77843 = devm_kzalloc(&i2c->dev, sizeof(*max77843), GFP_KERNEL);
109 if (!max77843)
110 return -ENOMEM;
112 i2c_set_clientdata(i2c, max77843);
113 max77843->dev = &i2c->dev;
114 max77843->i2c = i2c;
115 max77843->irq = i2c->irq;
116 max77843->type = id->driver_data;
118 max77843->regmap = devm_regmap_init_i2c(i2c,
119 &max77843_regmap_config);
120 if (IS_ERR(max77843->regmap)) {
121 dev_err(&i2c->dev, "Failed to allocate topsys register map\n");
122 return PTR_ERR(max77843->regmap);
125 ret = regmap_add_irq_chip(max77843->regmap, max77843->irq,
126 IRQF_TRIGGER_LOW | IRQF_ONESHOT | IRQF_SHARED,
127 0, &max77843_irq_chip, &max77843->irq_data_topsys);
128 if (ret) {
129 dev_err(&i2c->dev, "Failed to add TOPSYS IRQ chip\n");
130 return ret;
133 ret = regmap_read(max77843->regmap,
134 MAX77843_SYS_REG_PMICID, &reg_data);
135 if (ret < 0) {
136 dev_err(&i2c->dev, "Failed to read PMIC ID\n");
137 goto err_pmic_id;
139 dev_info(&i2c->dev, "device ID: 0x%x\n", reg_data);
141 ret = max77843_chg_init(max77843);
142 if (ret) {
143 dev_err(&i2c->dev, "Failed to init Charger\n");
144 goto err_pmic_id;
147 ret = regmap_update_bits(max77843->regmap,
148 MAX77843_SYS_REG_INTSRCMASK,
149 MAX77843_INTSRC_MASK_MASK,
150 (unsigned int)~MAX77843_INTSRC_MASK_MASK);
151 if (ret < 0) {
152 dev_err(&i2c->dev, "Failed to unmask interrupt source\n");
153 goto err_pmic_id;
156 ret = mfd_add_devices(max77843->dev, -1, max77843_devs,
157 ARRAY_SIZE(max77843_devs), NULL, 0, NULL);
158 if (ret < 0) {
159 dev_err(&i2c->dev, "Failed to add mfd device\n");
160 goto err_pmic_id;
163 device_init_wakeup(max77843->dev, true);
165 return 0;
167 err_pmic_id:
168 regmap_del_irq_chip(max77843->irq, max77843->irq_data_topsys);
170 return ret;
173 static const struct of_device_id max77843_dt_match[] = {
174 { .compatible = "maxim,max77843", },
175 { },
178 static const struct i2c_device_id max77843_id[] = {
179 { "max77843", TYPE_MAX77843, },
180 { },
183 static int __maybe_unused max77843_suspend(struct device *dev)
185 struct i2c_client *i2c = to_i2c_client(dev);
186 struct max77693_dev *max77843 = i2c_get_clientdata(i2c);
188 disable_irq(max77843->irq);
189 if (device_may_wakeup(dev))
190 enable_irq_wake(max77843->irq);
192 return 0;
195 static int __maybe_unused max77843_resume(struct device *dev)
197 struct i2c_client *i2c = to_i2c_client(dev);
198 struct max77693_dev *max77843 = i2c_get_clientdata(i2c);
200 if (device_may_wakeup(dev))
201 disable_irq_wake(max77843->irq);
202 enable_irq(max77843->irq);
204 return 0;
207 static SIMPLE_DEV_PM_OPS(max77843_pm, max77843_suspend, max77843_resume);
209 static struct i2c_driver max77843_i2c_driver = {
210 .driver = {
211 .name = "max77843",
212 .pm = &max77843_pm,
213 .of_match_table = max77843_dt_match,
214 .suppress_bind_attrs = true,
216 .probe = max77843_probe,
217 .id_table = max77843_id,
220 static int __init max77843_i2c_init(void)
222 return i2c_add_driver(&max77843_i2c_driver);
224 subsys_initcall(max77843_i2c_init);