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