xref: /openbmc/linux/drivers/mfd/max14577.c (revision 703e7713)
1 // SPDX-License-Identifier: GPL-2.0+
2 //
3 // max14577.c - mfd core driver for the Maxim 14577/77836
4 //
5 // Copyright (C) 2014 Samsung Electronics
6 // Chanwoo Choi <cw00.choi@samsung.com>
7 // Krzysztof Kozlowski <krzk@kernel.org>
8 //
9 // This driver is based on max8997.c
10 
11 #include <linux/err.h>
12 #include <linux/module.h>
13 #include <linux/interrupt.h>
14 #include <linux/of_device.h>
15 #include <linux/mfd/core.h>
16 #include <linux/mfd/max14577.h>
17 #include <linux/mfd/max14577-private.h>
18 
19 /*
20  * Table of valid charger currents for different Maxim chipsets.
21  * It is placed here because it is used by both charger and regulator driver.
22  */
23 const struct maxim_charger_current maxim_charger_currents[] = {
24 	[MAXIM_DEVICE_TYPE_UNKNOWN] = { 0, 0, 0, 0 },
25 	[MAXIM_DEVICE_TYPE_MAX14577] = {
26 		.min		= MAX14577_CHARGER_CURRENT_LIMIT_MIN,
27 		.high_start	= MAX14577_CHARGER_CURRENT_LIMIT_HIGH_START,
28 		.high_step	= MAX14577_CHARGER_CURRENT_LIMIT_HIGH_STEP,
29 		.max		= MAX14577_CHARGER_CURRENT_LIMIT_MAX,
30 	},
31 	[MAXIM_DEVICE_TYPE_MAX77836] = {
32 		.min		= MAX77836_CHARGER_CURRENT_LIMIT_MIN,
33 		.high_start	= MAX77836_CHARGER_CURRENT_LIMIT_HIGH_START,
34 		.high_step	= MAX77836_CHARGER_CURRENT_LIMIT_HIGH_STEP,
35 		.max		= MAX77836_CHARGER_CURRENT_LIMIT_MAX,
36 	},
37 };
38 EXPORT_SYMBOL_GPL(maxim_charger_currents);
39 
40 /*
41  * maxim_charger_calc_reg_current - Calculate register value for current
42  * @limits:	constraints for charger, matching the MBCICHWRC register
43  * @min_ua:	minimal requested current, micro Amps
44  * @max_ua:	maximum requested current, micro Amps
45  * @dst:	destination to store calculated register value
46  *
47  * Calculates the value of MBCICHWRC (Fast Battery Charge Current) register
48  * for given current and stores it under pointed 'dst'. The stored value
49  * combines low bit (MBCICHWRCL) and high bits (MBCICHWRCH). It is also
50  * properly shifted.
51  *
52  * The calculated register value matches the current which:
53  *  - is always between <limits.min, limits.max>;
54  *  - is always less or equal to max_ua;
55  *  - is the highest possible value;
56  *  - may be lower than min_ua.
57  *
58  * On success returns 0. On error returns -EINVAL (requested min/max current
59  * is outside of given charger limits) and 'dst' is not set.
60  */
61 int maxim_charger_calc_reg_current(const struct maxim_charger_current *limits,
62 		unsigned int min_ua, unsigned int max_ua, u8 *dst)
63 {
64 	unsigned int current_bits;
65 
66 	if (min_ua > max_ua)
67 		return -EINVAL;
68 
69 	if (min_ua > limits->max || max_ua < limits->min)
70 		return -EINVAL;
71 
72 	if (max_ua < limits->high_start) {
73 		/*
74 		 * Less than high_start, so set the minimal current
75 		 * (turn Low Bit off, 0 as high bits).
76 		 */
77 		*dst = 0x0;
78 		return 0;
79 	}
80 
81 	/* max_ua is in range: <high_start, infinite>, cut it to limits.max */
82 	max_ua = min(limits->max, max_ua);
83 	max_ua -= limits->high_start;
84 	/*
85 	 * There is no risk of overflow 'max_ua' here because:
86 	 *  - max_ua >= limits.high_start
87 	 *  - BUILD_BUG checks that 'limits' are: max >= high_start + high_step
88 	 */
89 	current_bits = max_ua / limits->high_step;
90 
91 	/* Turn Low Bit on (use range <limits.high_start, limits.max>) ... */
92 	*dst = 0x1 << CHGCTRL4_MBCICHWRCL_SHIFT;
93 	/* and set proper High Bits */
94 	*dst |= current_bits << CHGCTRL4_MBCICHWRCH_SHIFT;
95 
96 	return 0;
97 }
98 EXPORT_SYMBOL_GPL(maxim_charger_calc_reg_current);
99 
100 static const struct mfd_cell max14577_devs[] = {
101 	{
102 		.name = "max14577-muic",
103 		.of_compatible = "maxim,max14577-muic",
104 	},
105 	{
106 		.name = "max14577-regulator",
107 		.of_compatible = "maxim,max14577-regulator",
108 	},
109 	{
110 		.name = "max14577-charger",
111 		.of_compatible = "maxim,max14577-charger",
112 	},
113 };
114 
115 static const struct mfd_cell max77836_devs[] = {
116 	{
117 		.name = "max77836-muic",
118 		.of_compatible = "maxim,max77836-muic",
119 	},
120 	{
121 		.name = "max77836-regulator",
122 		.of_compatible = "maxim,max77836-regulator",
123 	},
124 	{
125 		.name = "max77836-charger",
126 		.of_compatible = "maxim,max77836-charger",
127 	},
128 	{
129 		.name = "max77836-battery",
130 		.of_compatible = "maxim,max77836-battery",
131 	},
132 };
133 
134 static const struct of_device_id max14577_dt_match[] = {
135 	{
136 		.compatible = "maxim,max14577",
137 		.data = (void *)MAXIM_DEVICE_TYPE_MAX14577,
138 	},
139 	{
140 		.compatible = "maxim,max77836",
141 		.data = (void *)MAXIM_DEVICE_TYPE_MAX77836,
142 	},
143 	{},
144 };
145 
146 static bool max14577_muic_volatile_reg(struct device *dev, unsigned int reg)
147 {
148 	switch (reg) {
149 	case MAX14577_REG_INT1 ... MAX14577_REG_STATUS3:
150 		return true;
151 	default:
152 		break;
153 	}
154 	return false;
155 }
156 
157 static bool max77836_muic_volatile_reg(struct device *dev, unsigned int reg)
158 {
159 	/* Any max14577 volatile registers are also max77836 volatile. */
160 	if (max14577_muic_volatile_reg(dev, reg))
161 		return true;
162 
163 	switch (reg) {
164 	case MAX77836_FG_REG_VCELL_MSB ... MAX77836_FG_REG_SOC_LSB:
165 	case MAX77836_FG_REG_CRATE_MSB ... MAX77836_FG_REG_CRATE_LSB:
166 	case MAX77836_FG_REG_STATUS_H ... MAX77836_FG_REG_STATUS_L:
167 	case MAX77836_PMIC_REG_INTSRC:
168 	case MAX77836_PMIC_REG_TOPSYS_INT:
169 	case MAX77836_PMIC_REG_TOPSYS_STAT:
170 		return true;
171 	default:
172 		break;
173 	}
174 	return false;
175 }
176 
177 static const struct regmap_config max14577_muic_regmap_config = {
178 	.reg_bits	= 8,
179 	.val_bits	= 8,
180 	.volatile_reg	= max14577_muic_volatile_reg,
181 	.max_register	= MAX14577_REG_END,
182 };
183 
184 static const struct regmap_config max77836_pmic_regmap_config = {
185 	.reg_bits	= 8,
186 	.val_bits	= 8,
187 	.volatile_reg	= max77836_muic_volatile_reg,
188 	.max_register	= MAX77836_PMIC_REG_END,
189 };
190 
191 static const struct regmap_irq max14577_irqs[] = {
192 	/* INT1 interrupts */
193 	{ .reg_offset = 0, .mask = MAX14577_INT1_ADC_MASK, },
194 	{ .reg_offset = 0, .mask = MAX14577_INT1_ADCLOW_MASK, },
195 	{ .reg_offset = 0, .mask = MAX14577_INT1_ADCERR_MASK, },
196 	/* INT2 interrupts */
197 	{ .reg_offset = 1, .mask = MAX14577_INT2_CHGTYP_MASK, },
198 	{ .reg_offset = 1, .mask = MAX14577_INT2_CHGDETRUN_MASK, },
199 	{ .reg_offset = 1, .mask = MAX14577_INT2_DCDTMR_MASK, },
200 	{ .reg_offset = 1, .mask = MAX14577_INT2_DBCHG_MASK, },
201 	{ .reg_offset = 1, .mask = MAX14577_INT2_VBVOLT_MASK, },
202 	/* INT3 interrupts */
203 	{ .reg_offset = 2, .mask = MAX14577_INT3_EOC_MASK, },
204 	{ .reg_offset = 2, .mask = MAX14577_INT3_CGMBC_MASK, },
205 	{ .reg_offset = 2, .mask = MAX14577_INT3_OVP_MASK, },
206 	{ .reg_offset = 2, .mask = MAX14577_INT3_MBCCHGERR_MASK, },
207 };
208 
209 static const struct regmap_irq_chip max14577_irq_chip = {
210 	.name			= "max14577",
211 	.status_base		= MAX14577_REG_INT1,
212 	.unmask_base		= MAX14577_REG_INTMASK1,
213 	.num_regs		= 3,
214 	.irqs			= max14577_irqs,
215 	.num_irqs		= ARRAY_SIZE(max14577_irqs),
216 };
217 
218 static const struct regmap_irq max77836_muic_irqs[] = {
219 	/* INT1 interrupts */
220 	{ .reg_offset = 0, .mask = MAX14577_INT1_ADC_MASK, },
221 	{ .reg_offset = 0, .mask = MAX14577_INT1_ADCLOW_MASK, },
222 	{ .reg_offset = 0, .mask = MAX14577_INT1_ADCERR_MASK, },
223 	{ .reg_offset = 0, .mask = MAX77836_INT1_ADC1K_MASK, },
224 	/* INT2 interrupts */
225 	{ .reg_offset = 1, .mask = MAX14577_INT2_CHGTYP_MASK, },
226 	{ .reg_offset = 1, .mask = MAX14577_INT2_CHGDETRUN_MASK, },
227 	{ .reg_offset = 1, .mask = MAX14577_INT2_DCDTMR_MASK, },
228 	{ .reg_offset = 1, .mask = MAX14577_INT2_DBCHG_MASK, },
229 	{ .reg_offset = 1, .mask = MAX14577_INT2_VBVOLT_MASK, },
230 	{ .reg_offset = 1, .mask = MAX77836_INT2_VIDRM_MASK, },
231 	/* INT3 interrupts */
232 	{ .reg_offset = 2, .mask = MAX14577_INT3_EOC_MASK, },
233 	{ .reg_offset = 2, .mask = MAX14577_INT3_CGMBC_MASK, },
234 	{ .reg_offset = 2, .mask = MAX14577_INT3_OVP_MASK, },
235 	{ .reg_offset = 2, .mask = MAX14577_INT3_MBCCHGERR_MASK, },
236 };
237 
238 static const struct regmap_irq_chip max77836_muic_irq_chip = {
239 	.name			= "max77836-muic",
240 	.status_base		= MAX14577_REG_INT1,
241 	.unmask_base		= MAX14577_REG_INTMASK1,
242 	.num_regs		= 3,
243 	.irqs			= max77836_muic_irqs,
244 	.num_irqs		= ARRAY_SIZE(max77836_muic_irqs),
245 };
246 
247 static const struct regmap_irq max77836_pmic_irqs[] = {
248 	{ .reg_offset = 0, .mask = MAX77836_TOPSYS_INT_T120C_MASK, },
249 	{ .reg_offset = 0, .mask = MAX77836_TOPSYS_INT_T140C_MASK, },
250 };
251 
252 static const struct regmap_irq_chip max77836_pmic_irq_chip = {
253 	.name			= "max77836-pmic",
254 	.status_base		= MAX77836_PMIC_REG_TOPSYS_INT,
255 	.mask_base		= MAX77836_PMIC_REG_TOPSYS_INT_MASK,
256 	.num_regs		= 1,
257 	.irqs			= max77836_pmic_irqs,
258 	.num_irqs		= ARRAY_SIZE(max77836_pmic_irqs),
259 };
260 
261 static void max14577_print_dev_type(struct max14577 *max14577)
262 {
263 	u8 reg_data, vendor_id, device_id;
264 	int ret;
265 
266 	ret = max14577_read_reg(max14577->regmap, MAX14577_REG_DEVICEID,
267 			&reg_data);
268 	if (ret) {
269 		dev_err(max14577->dev,
270 			"Failed to read DEVICEID register: %d\n", ret);
271 		return;
272 	}
273 
274 	vendor_id = ((reg_data & DEVID_VENDORID_MASK) >>
275 				DEVID_VENDORID_SHIFT);
276 	device_id = ((reg_data & DEVID_DEVICEID_MASK) >>
277 				DEVID_DEVICEID_SHIFT);
278 
279 	dev_info(max14577->dev, "Device type: %u (ID: 0x%x, vendor: 0x%x)\n",
280 			max14577->dev_type, device_id, vendor_id);
281 }
282 
283 /*
284  * Max77836 specific initialization code for driver probe.
285  * Adds new I2C dummy device, regmap and regmap IRQ chip.
286  * Unmasks Interrupt Source register.
287  *
288  * On success returns 0.
289  * On failure returns errno and reverts any changes done so far (e.g. remove
290  * I2C dummy device), except masking the INT SRC register.
291  */
292 static int max77836_init(struct max14577 *max14577)
293 {
294 	int ret;
295 	u8 intsrc_mask;
296 
297 	max14577->i2c_pmic = i2c_new_dummy_device(max14577->i2c->adapter,
298 			I2C_ADDR_PMIC);
299 	if (IS_ERR(max14577->i2c_pmic)) {
300 		dev_err(max14577->dev, "Failed to register PMIC I2C device\n");
301 		return PTR_ERR(max14577->i2c_pmic);
302 	}
303 	i2c_set_clientdata(max14577->i2c_pmic, max14577);
304 
305 	max14577->regmap_pmic = devm_regmap_init_i2c(max14577->i2c_pmic,
306 			&max77836_pmic_regmap_config);
307 	if (IS_ERR(max14577->regmap_pmic)) {
308 		ret = PTR_ERR(max14577->regmap_pmic);
309 		dev_err(max14577->dev, "Failed to allocate PMIC register map: %d\n",
310 				ret);
311 		goto err;
312 	}
313 
314 	/* Un-mask MAX77836 Interrupt Source register */
315 	ret = max14577_read_reg(max14577->regmap_pmic,
316 			MAX77836_PMIC_REG_INTSRC_MASK, &intsrc_mask);
317 	if (ret < 0) {
318 		dev_err(max14577->dev, "Failed to read PMIC register\n");
319 		goto err;
320 	}
321 
322 	intsrc_mask &= ~(MAX77836_INTSRC_MASK_TOP_INT_MASK);
323 	intsrc_mask &= ~(MAX77836_INTSRC_MASK_MUIC_CHG_INT_MASK);
324 	ret = max14577_write_reg(max14577->regmap_pmic,
325 			MAX77836_PMIC_REG_INTSRC_MASK, intsrc_mask);
326 	if (ret < 0) {
327 		dev_err(max14577->dev, "Failed to write PMIC register\n");
328 		goto err;
329 	}
330 
331 	ret = regmap_add_irq_chip(max14577->regmap_pmic, max14577->irq,
332 			IRQF_ONESHOT | IRQF_SHARED,
333 			0, &max77836_pmic_irq_chip,
334 			&max14577->irq_data_pmic);
335 	if (ret != 0) {
336 		dev_err(max14577->dev, "Failed to request PMIC IRQ %d: %d\n",
337 				max14577->irq, ret);
338 		goto err;
339 	}
340 
341 	return 0;
342 
343 err:
344 	i2c_unregister_device(max14577->i2c_pmic);
345 
346 	return ret;
347 }
348 
349 /*
350  * Max77836 specific de-initialization code for driver remove.
351  */
352 static void max77836_remove(struct max14577 *max14577)
353 {
354 	regmap_del_irq_chip(max14577->irq, max14577->irq_data_pmic);
355 	i2c_unregister_device(max14577->i2c_pmic);
356 }
357 
358 static int max14577_i2c_probe(struct i2c_client *i2c)
359 {
360 	const struct i2c_device_id *id = i2c_client_get_device_id(i2c);
361 	struct max14577 *max14577;
362 	struct max14577_platform_data *pdata = dev_get_platdata(&i2c->dev);
363 	struct device_node *np = i2c->dev.of_node;
364 	int ret = 0;
365 	const struct regmap_irq_chip *irq_chip;
366 	const struct mfd_cell *mfd_devs;
367 	unsigned int mfd_devs_size;
368 	int irq_flags;
369 
370 	if (np) {
371 		pdata = devm_kzalloc(&i2c->dev, sizeof(*pdata), GFP_KERNEL);
372 		if (!pdata)
373 			return -ENOMEM;
374 		i2c->dev.platform_data = pdata;
375 	}
376 
377 	if (!pdata) {
378 		dev_err(&i2c->dev, "No platform data found.\n");
379 		return -EINVAL;
380 	}
381 
382 	max14577 = devm_kzalloc(&i2c->dev, sizeof(*max14577), GFP_KERNEL);
383 	if (!max14577)
384 		return -ENOMEM;
385 
386 	i2c_set_clientdata(i2c, max14577);
387 	max14577->dev = &i2c->dev;
388 	max14577->i2c = i2c;
389 	max14577->irq = i2c->irq;
390 
391 	max14577->regmap = devm_regmap_init_i2c(i2c,
392 			&max14577_muic_regmap_config);
393 	if (IS_ERR(max14577->regmap)) {
394 		ret = PTR_ERR(max14577->regmap);
395 		dev_err(max14577->dev, "Failed to allocate register map: %d\n",
396 				ret);
397 		return ret;
398 	}
399 
400 	if (np) {
401 		const struct of_device_id *of_id;
402 
403 		of_id = of_match_device(max14577_dt_match, &i2c->dev);
404 		if (of_id)
405 			max14577->dev_type =
406 				(enum maxim_device_type)of_id->data;
407 	} else {
408 		max14577->dev_type = id->driver_data;
409 	}
410 
411 	max14577_print_dev_type(max14577);
412 
413 	switch (max14577->dev_type) {
414 	case MAXIM_DEVICE_TYPE_MAX77836:
415 		irq_chip = &max77836_muic_irq_chip;
416 		mfd_devs = max77836_devs;
417 		mfd_devs_size = ARRAY_SIZE(max77836_devs);
418 		irq_flags = IRQF_ONESHOT | IRQF_SHARED;
419 		break;
420 	case MAXIM_DEVICE_TYPE_MAX14577:
421 	default:
422 		irq_chip = &max14577_irq_chip;
423 		mfd_devs = max14577_devs;
424 		mfd_devs_size = ARRAY_SIZE(max14577_devs);
425 		irq_flags = IRQF_ONESHOT;
426 		break;
427 	}
428 
429 	ret = regmap_add_irq_chip(max14577->regmap, max14577->irq,
430 				  irq_flags, 0, irq_chip,
431 				  &max14577->irq_data);
432 	if (ret != 0) {
433 		dev_err(&i2c->dev, "Failed to request IRQ %d: %d\n",
434 				max14577->irq, ret);
435 		return ret;
436 	}
437 
438 	/* Max77836 specific initialization code (additional regmap) */
439 	if (max14577->dev_type == MAXIM_DEVICE_TYPE_MAX77836) {
440 		ret = max77836_init(max14577);
441 		if (ret < 0)
442 			goto err_max77836;
443 	}
444 
445 	ret = mfd_add_devices(max14577->dev, -1, mfd_devs,
446 			mfd_devs_size, NULL, 0, NULL);
447 	if (ret < 0)
448 		goto err_mfd;
449 
450 	device_init_wakeup(max14577->dev, 1);
451 
452 	return 0;
453 
454 err_mfd:
455 	if (max14577->dev_type == MAXIM_DEVICE_TYPE_MAX77836)
456 		max77836_remove(max14577);
457 err_max77836:
458 	regmap_del_irq_chip(max14577->irq, max14577->irq_data);
459 
460 	return ret;
461 }
462 
463 static void max14577_i2c_remove(struct i2c_client *i2c)
464 {
465 	struct max14577 *max14577 = i2c_get_clientdata(i2c);
466 
467 	mfd_remove_devices(max14577->dev);
468 	regmap_del_irq_chip(max14577->irq, max14577->irq_data);
469 	if (max14577->dev_type == MAXIM_DEVICE_TYPE_MAX77836)
470 		max77836_remove(max14577);
471 }
472 
473 static const struct i2c_device_id max14577_i2c_id[] = {
474 	{ "max14577", MAXIM_DEVICE_TYPE_MAX14577, },
475 	{ "max77836", MAXIM_DEVICE_TYPE_MAX77836, },
476 	{ }
477 };
478 MODULE_DEVICE_TABLE(i2c, max14577_i2c_id);
479 
480 static int max14577_suspend(struct device *dev)
481 {
482 	struct i2c_client *i2c = to_i2c_client(dev);
483 	struct max14577 *max14577 = i2c_get_clientdata(i2c);
484 
485 	if (device_may_wakeup(dev))
486 		enable_irq_wake(max14577->irq);
487 	/*
488 	 * MUIC IRQ must be disabled during suspend because if it happens
489 	 * while suspended it will be handled before resuming I2C.
490 	 *
491 	 * When device is woken up from suspend (e.g. by ADC change),
492 	 * an interrupt occurs before resuming I2C bus controller.
493 	 * Interrupt handler tries to read registers but this read
494 	 * will fail because I2C is still suspended.
495 	 */
496 	disable_irq(max14577->irq);
497 
498 	return 0;
499 }
500 
501 static int max14577_resume(struct device *dev)
502 {
503 	struct i2c_client *i2c = to_i2c_client(dev);
504 	struct max14577 *max14577 = i2c_get_clientdata(i2c);
505 
506 	if (device_may_wakeup(dev))
507 		disable_irq_wake(max14577->irq);
508 	enable_irq(max14577->irq);
509 
510 	return 0;
511 }
512 
513 static DEFINE_SIMPLE_DEV_PM_OPS(max14577_pm, max14577_suspend, max14577_resume);
514 
515 static struct i2c_driver max14577_i2c_driver = {
516 	.driver = {
517 		.name = "max14577",
518 		.pm = pm_sleep_ptr(&max14577_pm),
519 		.of_match_table = max14577_dt_match,
520 	},
521 	.probe = max14577_i2c_probe,
522 	.remove = max14577_i2c_remove,
523 	.id_table = max14577_i2c_id,
524 };
525 
526 static int __init max14577_i2c_init(void)
527 {
528 	BUILD_BUG_ON(ARRAY_SIZE(max14577_i2c_id) != MAXIM_DEVICE_TYPE_NUM);
529 	BUILD_BUG_ON(ARRAY_SIZE(max14577_dt_match) != MAXIM_DEVICE_TYPE_NUM);
530 
531 	/* Valid charger current values must be provided for each chipset */
532 	BUILD_BUG_ON(ARRAY_SIZE(maxim_charger_currents) != MAXIM_DEVICE_TYPE_NUM);
533 
534 	/* Check for valid values for charger */
535 	BUILD_BUG_ON(MAX14577_CHARGER_CURRENT_LIMIT_HIGH_START +
536 			MAX14577_CHARGER_CURRENT_LIMIT_HIGH_STEP * 0xf !=
537 			MAX14577_CHARGER_CURRENT_LIMIT_MAX);
538 	BUILD_BUG_ON(MAX14577_CHARGER_CURRENT_LIMIT_HIGH_STEP == 0);
539 
540 	BUILD_BUG_ON(MAX77836_CHARGER_CURRENT_LIMIT_HIGH_START +
541 			MAX77836_CHARGER_CURRENT_LIMIT_HIGH_STEP * 0xf !=
542 			MAX77836_CHARGER_CURRENT_LIMIT_MAX);
543 	BUILD_BUG_ON(MAX77836_CHARGER_CURRENT_LIMIT_HIGH_STEP == 0);
544 
545 	return i2c_add_driver(&max14577_i2c_driver);
546 }
547 module_init(max14577_i2c_init);
548 
549 static void __exit max14577_i2c_exit(void)
550 {
551 	i2c_del_driver(&max14577_i2c_driver);
552 }
553 module_exit(max14577_i2c_exit);
554 
555 MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>, Krzysztof Kozlowski <krzk@kernel.org>");
556 MODULE_DESCRIPTION("Maxim 14577/77836 multi-function core driver");
557 MODULE_LICENSE("GPL");
558