1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * max8907.c - mfd driver for MAX8907
4  *
5  * Copyright (C) 2010 Gyungoh Yoo <jack.yoo@maxim-ic.com>
6  * Copyright (C) 2010-2012, NVIDIA CORPORATION. All rights reserved.
7  */
8 
9 #include <linux/err.h>
10 #include <linux/i2c.h>
11 #include <linux/init.h>
12 #include <linux/interrupt.h>
13 #include <linux/irq.h>
14 #include <linux/mfd/core.h>
15 #include <linux/mfd/max8907.h>
16 #include <linux/module.h>
17 #include <linux/of.h>
18 #include <linux/of_device.h>
19 #include <linux/regmap.h>
20 #include <linux/slab.h>
21 
22 static const struct mfd_cell max8907_cells[] = {
23 	{ .name = "max8907-regulator", },
24 	{ .name = "max8907-rtc", },
25 };
26 
max8907_gen_is_volatile_reg(struct device * dev,unsigned int reg)27 static bool max8907_gen_is_volatile_reg(struct device *dev, unsigned int reg)
28 {
29 	switch (reg) {
30 	case MAX8907_REG_ON_OFF_IRQ1:
31 	case MAX8907_REG_ON_OFF_STAT:
32 	case MAX8907_REG_ON_OFF_IRQ2:
33 	case MAX8907_REG_CHG_IRQ1:
34 	case MAX8907_REG_CHG_IRQ2:
35 	case MAX8907_REG_CHG_STAT:
36 		return true;
37 	default:
38 		return false;
39 	}
40 }
41 
max8907_gen_is_precious_reg(struct device * dev,unsigned int reg)42 static bool max8907_gen_is_precious_reg(struct device *dev, unsigned int reg)
43 {
44 	switch (reg) {
45 	case MAX8907_REG_ON_OFF_IRQ1:
46 	case MAX8907_REG_ON_OFF_IRQ2:
47 	case MAX8907_REG_CHG_IRQ1:
48 	case MAX8907_REG_CHG_IRQ2:
49 		return true;
50 	default:
51 		return false;
52 	}
53 }
54 
max8907_gen_is_writeable_reg(struct device * dev,unsigned int reg)55 static bool max8907_gen_is_writeable_reg(struct device *dev, unsigned int reg)
56 {
57 	return !max8907_gen_is_volatile_reg(dev, reg);
58 }
59 
60 static const struct regmap_config max8907_regmap_gen_config = {
61 	.reg_bits = 8,
62 	.val_bits = 8,
63 	.volatile_reg = max8907_gen_is_volatile_reg,
64 	.precious_reg = max8907_gen_is_precious_reg,
65 	.writeable_reg = max8907_gen_is_writeable_reg,
66 	.max_register = MAX8907_REG_LDO20VOUT,
67 	.cache_type = REGCACHE_RBTREE,
68 };
69 
max8907_rtc_is_volatile_reg(struct device * dev,unsigned int reg)70 static bool max8907_rtc_is_volatile_reg(struct device *dev, unsigned int reg)
71 {
72 	if (reg <= MAX8907_REG_RTC_YEAR2)
73 		return true;
74 
75 	switch (reg) {
76 	case MAX8907_REG_RTC_STATUS:
77 	case MAX8907_REG_RTC_IRQ:
78 		return true;
79 	default:
80 		return false;
81 	}
82 }
83 
max8907_rtc_is_precious_reg(struct device * dev,unsigned int reg)84 static bool max8907_rtc_is_precious_reg(struct device *dev, unsigned int reg)
85 {
86 	switch (reg) {
87 	case MAX8907_REG_RTC_IRQ:
88 		return true;
89 	default:
90 		return false;
91 	}
92 }
93 
max8907_rtc_is_writeable_reg(struct device * dev,unsigned int reg)94 static bool max8907_rtc_is_writeable_reg(struct device *dev, unsigned int reg)
95 {
96 	switch (reg) {
97 	case MAX8907_REG_RTC_STATUS:
98 	case MAX8907_REG_RTC_IRQ:
99 		return false;
100 	default:
101 		return true;
102 	}
103 }
104 
105 static const struct regmap_config max8907_regmap_rtc_config = {
106 	.reg_bits = 8,
107 	.val_bits = 8,
108 	.volatile_reg = max8907_rtc_is_volatile_reg,
109 	.precious_reg = max8907_rtc_is_precious_reg,
110 	.writeable_reg = max8907_rtc_is_writeable_reg,
111 	.max_register = MAX8907_REG_MPL_CNTL,
112 	.cache_type = REGCACHE_RBTREE,
113 };
114 
115 static const struct regmap_irq max8907_chg_irqs[] = {
116 	{ .reg_offset = 0, .mask = 1 << 0, },
117 	{ .reg_offset = 0, .mask = 1 << 1, },
118 	{ .reg_offset = 0, .mask = 1 << 2, },
119 	{ .reg_offset = 1, .mask = 1 << 0, },
120 	{ .reg_offset = 1, .mask = 1 << 1, },
121 	{ .reg_offset = 1, .mask = 1 << 2, },
122 	{ .reg_offset = 1, .mask = 1 << 3, },
123 	{ .reg_offset = 1, .mask = 1 << 4, },
124 	{ .reg_offset = 1, .mask = 1 << 5, },
125 	{ .reg_offset = 1, .mask = 1 << 6, },
126 	{ .reg_offset = 1, .mask = 1 << 7, },
127 };
128 
129 static const struct regmap_irq_chip max8907_chg_irq_chip = {
130 	.name = "max8907 chg",
131 	.status_base = MAX8907_REG_CHG_IRQ1,
132 	.mask_base = MAX8907_REG_CHG_IRQ1_MASK,
133 	.wake_base = MAX8907_REG_CHG_IRQ1_MASK,
134 	.irq_reg_stride = MAX8907_REG_CHG_IRQ2 - MAX8907_REG_CHG_IRQ1,
135 	.num_regs = 2,
136 	.irqs = max8907_chg_irqs,
137 	.num_irqs = ARRAY_SIZE(max8907_chg_irqs),
138 };
139 
140 static const struct regmap_irq max8907_on_off_irqs[] = {
141 	{ .reg_offset = 0, .mask = 1 << 0, },
142 	{ .reg_offset = 0, .mask = 1 << 1, },
143 	{ .reg_offset = 0, .mask = 1 << 2, },
144 	{ .reg_offset = 0, .mask = 1 << 3, },
145 	{ .reg_offset = 0, .mask = 1 << 4, },
146 	{ .reg_offset = 0, .mask = 1 << 5, },
147 	{ .reg_offset = 0, .mask = 1 << 6, },
148 	{ .reg_offset = 0, .mask = 1 << 7, },
149 	{ .reg_offset = 1, .mask = 1 << 0, },
150 	{ .reg_offset = 1, .mask = 1 << 1, },
151 };
152 
153 static const struct regmap_irq_chip max8907_on_off_irq_chip = {
154 	.name = "max8907 on_off",
155 	.status_base = MAX8907_REG_ON_OFF_IRQ1,
156 	.mask_base = MAX8907_REG_ON_OFF_IRQ1_MASK,
157 	.irq_reg_stride = MAX8907_REG_ON_OFF_IRQ2 - MAX8907_REG_ON_OFF_IRQ1,
158 	.num_regs = 2,
159 	.irqs = max8907_on_off_irqs,
160 	.num_irqs = ARRAY_SIZE(max8907_on_off_irqs),
161 };
162 
163 static const struct regmap_irq max8907_rtc_irqs[] = {
164 	{ .reg_offset = 0, .mask = 1 << 2, },
165 	{ .reg_offset = 0, .mask = 1 << 3, },
166 };
167 
168 static const struct regmap_irq_chip max8907_rtc_irq_chip = {
169 	.name = "max8907 rtc",
170 	.status_base = MAX8907_REG_RTC_IRQ,
171 	.mask_base = MAX8907_REG_RTC_IRQ_MASK,
172 	.num_regs = 1,
173 	.irqs = max8907_rtc_irqs,
174 	.num_irqs = ARRAY_SIZE(max8907_rtc_irqs),
175 };
176 
177 static struct max8907 *max8907_pm_off;
max8907_power_off(void)178 static void max8907_power_off(void)
179 {
180 	regmap_update_bits(max8907_pm_off->regmap_gen, MAX8907_REG_RESET_CNFG,
181 			MAX8907_MASK_POWER_OFF, MAX8907_MASK_POWER_OFF);
182 }
183 
max8907_i2c_probe(struct i2c_client * i2c)184 static int max8907_i2c_probe(struct i2c_client *i2c)
185 {
186 	struct max8907 *max8907;
187 	int ret;
188 	struct max8907_platform_data *pdata = dev_get_platdata(&i2c->dev);
189 	bool pm_off = false;
190 
191 	if (pdata)
192 		pm_off = pdata->pm_off;
193 	else if (i2c->dev.of_node)
194 		pm_off = of_property_read_bool(i2c->dev.of_node,
195 					"maxim,system-power-controller");
196 
197 	max8907 = devm_kzalloc(&i2c->dev, sizeof(struct max8907), GFP_KERNEL);
198 	if (!max8907) {
199 		ret = -ENOMEM;
200 		goto err_alloc_drvdata;
201 	}
202 
203 	max8907->dev = &i2c->dev;
204 	dev_set_drvdata(max8907->dev, max8907);
205 
206 	max8907->i2c_gen = i2c;
207 	i2c_set_clientdata(i2c, max8907);
208 	max8907->regmap_gen = devm_regmap_init_i2c(i2c,
209 						&max8907_regmap_gen_config);
210 	if (IS_ERR(max8907->regmap_gen)) {
211 		ret = PTR_ERR(max8907->regmap_gen);
212 		dev_err(&i2c->dev, "gen regmap init failed: %d\n", ret);
213 		goto err_regmap_gen;
214 	}
215 
216 	max8907->i2c_rtc = i2c_new_dummy_device(i2c->adapter, MAX8907_RTC_I2C_ADDR);
217 	if (IS_ERR(max8907->i2c_rtc)) {
218 		ret = PTR_ERR(max8907->i2c_rtc);
219 		goto err_dummy_rtc;
220 	}
221 	i2c_set_clientdata(max8907->i2c_rtc, max8907);
222 	max8907->regmap_rtc = devm_regmap_init_i2c(max8907->i2c_rtc,
223 						&max8907_regmap_rtc_config);
224 	if (IS_ERR(max8907->regmap_rtc)) {
225 		ret = PTR_ERR(max8907->regmap_rtc);
226 		dev_err(&i2c->dev, "rtc regmap init failed: %d\n", ret);
227 		goto err_regmap_rtc;
228 	}
229 
230 	ret = regmap_add_irq_chip(max8907->regmap_gen, max8907->i2c_gen->irq,
231 				  IRQF_ONESHOT | IRQF_SHARED,
232 				  -1, &max8907_chg_irq_chip,
233 				  &max8907->irqc_chg);
234 	if (ret != 0) {
235 		dev_err(&i2c->dev, "failed to add chg irq chip: %d\n", ret);
236 		goto err_irqc_chg;
237 	}
238 	ret = regmap_add_irq_chip(max8907->regmap_gen, max8907->i2c_gen->irq,
239 				  IRQF_ONESHOT | IRQF_SHARED, -1,
240 				  &max8907_on_off_irq_chip,
241 				  &max8907->irqc_on_off);
242 	if (ret != 0) {
243 		dev_err(&i2c->dev, "failed to add on off irq chip: %d\n", ret);
244 		goto err_irqc_on_off;
245 	}
246 	ret = regmap_add_irq_chip(max8907->regmap_rtc, max8907->i2c_gen->irq,
247 				  IRQF_ONESHOT | IRQF_SHARED, -1,
248 				  &max8907_rtc_irq_chip,
249 				  &max8907->irqc_rtc);
250 	if (ret != 0) {
251 		dev_err(&i2c->dev, "failed to add rtc irq chip: %d\n", ret);
252 		goto err_irqc_rtc;
253 	}
254 
255 	ret = mfd_add_devices(max8907->dev, -1, max8907_cells,
256 			      ARRAY_SIZE(max8907_cells), NULL, 0, NULL);
257 	if (ret != 0) {
258 		dev_err(&i2c->dev, "failed to add MFD devices %d\n", ret);
259 		goto err_add_devices;
260 	}
261 
262 	if (pm_off && !pm_power_off) {
263 		max8907_pm_off = max8907;
264 		pm_power_off = max8907_power_off;
265 	}
266 
267 	return 0;
268 
269 err_add_devices:
270 	regmap_del_irq_chip(max8907->i2c_gen->irq, max8907->irqc_rtc);
271 err_irqc_rtc:
272 	regmap_del_irq_chip(max8907->i2c_gen->irq, max8907->irqc_on_off);
273 err_irqc_on_off:
274 	regmap_del_irq_chip(max8907->i2c_gen->irq, max8907->irqc_chg);
275 err_irqc_chg:
276 err_regmap_rtc:
277 	i2c_unregister_device(max8907->i2c_rtc);
278 err_dummy_rtc:
279 err_regmap_gen:
280 err_alloc_drvdata:
281 	return ret;
282 }
283 
max8907_i2c_remove(struct i2c_client * i2c)284 static void max8907_i2c_remove(struct i2c_client *i2c)
285 {
286 	struct max8907 *max8907 = i2c_get_clientdata(i2c);
287 
288 	mfd_remove_devices(max8907->dev);
289 
290 	regmap_del_irq_chip(max8907->i2c_gen->irq, max8907->irqc_rtc);
291 	regmap_del_irq_chip(max8907->i2c_gen->irq, max8907->irqc_on_off);
292 	regmap_del_irq_chip(max8907->i2c_gen->irq, max8907->irqc_chg);
293 
294 	i2c_unregister_device(max8907->i2c_rtc);
295 }
296 
297 #ifdef CONFIG_OF
298 static const struct of_device_id max8907_of_match[] = {
299 	{ .compatible = "maxim,max8907" },
300 	{ },
301 };
302 MODULE_DEVICE_TABLE(of, max8907_of_match);
303 #endif
304 
305 static const struct i2c_device_id max8907_i2c_id[] = {
306 	{"max8907", 0},
307 	{}
308 };
309 MODULE_DEVICE_TABLE(i2c, max8907_i2c_id);
310 
311 static struct i2c_driver max8907_i2c_driver = {
312 	.driver = {
313 		.name = "max8907",
314 		.of_match_table = of_match_ptr(max8907_of_match),
315 	},
316 	.probe_new = max8907_i2c_probe,
317 	.remove = max8907_i2c_remove,
318 	.id_table = max8907_i2c_id,
319 };
320 
max8907_i2c_init(void)321 static int __init max8907_i2c_init(void)
322 {
323 	int ret = -ENODEV;
324 
325 	ret = i2c_add_driver(&max8907_i2c_driver);
326 	if (ret != 0)
327 		pr_err("Failed to register I2C driver: %d\n", ret);
328 
329 	return ret;
330 }
331 subsys_initcall(max8907_i2c_init);
332 
max8907_i2c_exit(void)333 static void __exit max8907_i2c_exit(void)
334 {
335 	i2c_del_driver(&max8907_i2c_driver);
336 }
337 module_exit(max8907_i2c_exit);
338 
339 MODULE_DESCRIPTION("MAX8907 multi-function core driver");
340 MODULE_AUTHOR("Gyungoh Yoo <jack.yoo@maxim-ic.com>");
341 MODULE_LICENSE("GPL v2");
342