summaryrefslogtreecommitdiffstats
path: root/drivers/mfd/max14577.c
blob: 1f4f5002595c0ae43a306217d3483e72b355353a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
// SPDX-License-Identifier: GPL-2.0+
//
// max14577.c - mfd core driver for the Maxim 14577/77836
//
// Copyright (C) 2014 Samsung Electronics
// Chanwoo Choi <cw00.choi@samsung.com>
// Krzysztof Kozlowski <krzk@kernel.org>
//
// This driver is based on max8997.c

#include <linux/err.h>
#include <linux/module.h>
#include <linux/interrupt.h>
#include <linux/of_device.h>
#include <linux/mfd/core.h>
#include <linux/mfd/max14577.h>
#include <linux/mfd/max14577-private.h>

/*
 * Table of valid charger currents for different Maxim chipsets.
 * It is placed here because it is used by both charger and regulator driver.
 */
const struct maxim_charger_current maxim_charger_currents[] = {
	[MAXIM_DEVICE_TYPE_UNKNOWN] = { 0, 0, 0, 0 },
	[MAXIM_DEVICE_TYPE_MAX14577] = {
		.min		= MAX14577_CHARGER_CURRENT_LIMIT_MIN,
		.high_start	= MAX14577_CHARGER_CURRENT_LIMIT_HIGH_START,
		.high_step	= MAX14577_CHARGER_CURRENT_LIMIT_HIGH_STEP,
		.max		= MAX14577_CHARGER_CURRENT_LIMIT_MAX,
	},
	[MAXIM_DEVICE_TYPE_MAX77836] = {
		.min		= MAX77836_CHARGER_CURRENT_LIMIT_MIN,
		.high_start	= MAX77836_CHARGER_CURRENT_LIMIT_HIGH_START,
		.high_step	= MAX77836_CHARGER_CURRENT_LIMIT_HIGH_STEP,
		.max		= MAX77836_CHARGER_CURRENT_LIMIT_MAX,
	},
};
EXPORT_SYMBOL_GPL(maxim_charger_currents);

/*
 * maxim_charger_calc_reg_current - Calculate register value for current
 * @limits:	constraints for charger, matching the MBCICHWRC register
 * @min_ua:	minimal requested current, micro Amps
 * @max_ua:	maximum requested current, micro Amps
 * @dst:	destination to store calculated register value
 *
 * Calculates the value of MBCICHWRC (Fast Battery Charge Current) register
 * for given current and stores it under pointed 'dst'. The stored value
 * combines low bit (MBCICHWRCL) and high bits (MBCICHWRCH). It is also
 * properly shifted.
 *
 * The calculated register value matches the current which:
 *  - is always between <limits.min, limits.max>;
 *  - is always less or equal to max_ua;
 *  - is the highest possible value;
 *  - may be lower than min_ua.
 *
 * On success returns 0. On error returns -EINVAL (requested min/max current
 * is outside of given charger limits) and 'dst' is not set.
 */
int maxim_charger_calc_reg_current(const struct maxim_charger_current *limits,
		unsigned int min_ua, unsigned int max_ua, u8 *dst)
{
	unsigned int current_bits;

	if (min_ua > max_ua)
		return -EINVAL;

	if (min_ua > limits->max || max_ua < limits->min)
		return -EINVAL;

	if (max_ua < limits->high_start) {
		/*
		 * Less than high_start, so set the minimal current
		 * (turn Low Bit off, 0 as high bits).
		 */
		*dst = 0x0;
		return 0;
	}

	/* max_ua is in range: <high_start, infinite>, cut it to limits.max */
	max_ua = min(limits->max, max_ua);
	max_ua -= limits->high_start;
	/*
	 * There is no risk of overflow 'max_ua' here because:
	 *  - max_ua >= limits.high_start
	 *  - BUILD_BUG checks that 'limits' are: max >= high_start + high_step
	 */
	current_bits = max_ua / limits->high_step;

	/* Turn Low Bit on (use range <limits.high_start, limits.max>) ... */
	*dst = 0x1 << CHGCTRL4_MBCICHWRCL_SHIFT;
	/* and set proper High Bits */
	*dst |= current_bits << CHGCTRL4_MBCICHWRCH_SHIFT;

	return 0;
}
EXPORT_SYMBOL_GPL(maxim_charger_calc_reg_current);

static const struct mfd_cell max14577_devs[] = {
	{
		.name = "max14577-muic",
		.of_compatible = "maxim,max14577-muic",
	},
	{
		.name = "max14577-regulator",
		.of_compatible = "maxim,max14577-regulator",
	},
	{
		.name = "max14577-charger",
		.of_compatible = "maxim,max14577-charger",
	},
};

static const struct mfd_cell max77836_devs[] = {
	{
		.name = "max77836-muic",
		.of_compatible = "maxim,max77836-muic",
	},
	{
		.name = "max77836-regulator",
		.of_compatible = "maxim,max77836-regulator",
	},
	{
		.name = "max77836-charger",
		.of_compatible = "maxim,max77836-charger",
	},
	{
		.name = "max77836-battery",
		.of_compatible = "maxim,max77836-battery",
	},
};

static const struct of_device_id max14577_dt_match[] = {
	{
		.compatible = "maxim,max14577",
		.data = (void *)MAXIM_DEVICE_TYPE_MAX14577,
	},
	{
		.compatible = "maxim,max77836",
		.data = (void *)MAXIM_DEVICE_TYPE_MAX77836,
	},
	{},
};

static bool max14577_muic_volatile_reg(struct device *dev, unsigned int reg)
{
	switch (reg) {
	case MAX14577_REG_INT1 ... MAX14577_REG_STATUS3:
		return true;
	default:
		break;
	}
	return false;
}

static bool max77836_muic_volatile_reg(struct device *dev, unsigned int reg)
{
	/* Any max14577 volatile registers are also max77836 volatile. */
	if (max14577_muic_volatile_reg(dev, reg))
		return true;

	switch (reg) {
	case MAX77836_FG_REG_VCELL_MSB ... MAX77836_FG_REG_SOC_LSB:
	case MAX77836_FG_REG_CRATE_MSB ... MAX77836_FG_REG_CRATE_LSB:
	case MAX77836_FG_REG_STATUS_H ... MAX77836_FG_REG_STATUS_L:
	case MAX77836_PMIC_REG_INTSRC:
	case MAX77836_PMIC_REG_TOPSYS_INT:
	case MAX77836_PMIC_REG_TOPSYS_STAT:
		return true;
	default:
		break;
	}
	return false;
}

static const struct regmap_config max14577_muic_regmap_config = {
	.reg_bits	= 8,
	.val_bits	= 8,
	.volatile_reg	= max14577_muic_volatile_reg,
	.max_register	= MAX14577_REG_END,
};

static const struct regmap_config max77836_pmic_regmap_config = {
	.reg_bits	= 8,
	.val_bits	= 8,
	.volatile_reg	= max77836_muic_volatile_reg,
	.max_register	= MAX77836_PMIC_REG_END,
};

static const struct regmap_irq max14577_irqs[] = {
	/* INT1 interrupts */
	{ .reg_offset = 0, .mask = MAX14577_INT1_ADC_MASK, },
	{ .reg_offset = 0, .mask = MAX14577_INT1_ADCLOW_MASK, },
	{ .reg_offset = 0, .mask = MAX14577_INT1_ADCERR_MASK, },
	/* INT2 interrupts */
	{ .reg_offset = 1, .mask = MAX14577_INT2_CHGTYP_MASK, },
	{ .reg_offset = 1, .mask = MAX14577_INT2_CHGDETRUN_MASK, },
	{ .reg_offset = 1, .mask = MAX14577_INT2_DCDTMR_MASK, },
	{ .reg_offset = 1, .mask = MAX14577_INT2_DBCHG_MASK, },
	{ .reg_offset = 1, .mask = MAX14577_INT2_VBVOLT_MASK, },
	/* INT3 interrupts */
	{ .reg_offset = 2, .mask = MAX14577_INT3_EOC_MASK, },
	{ .reg_offset = 2, .mask = MAX14577_INT3_CGMBC_MASK, },
	{ .reg_offset = 2, .mask = MAX14577_INT3_OVP_MASK, },
	{ .reg_offset = 2, .mask = MAX14577_INT3_MBCCHGERR_MASK, },
};

static const struct regmap_irq_chip max14577_irq_chip = {
	.name			= "max14577",
	.status_base		= MAX14577_REG_INT1,
	.unmask_base		= MAX14577_REG_INTMASK1,
	.num_regs		= 3,
	.irqs			= max14577_irqs,
	.num_irqs		= ARRAY_SIZE(max14577_irqs),
};

static const struct regmap_irq max77836_muic_irqs[] = {
	/* INT1 interrupts */
	{ .reg_offset = 0, .mask = MAX14577_INT1_ADC_MASK, },
	{ .reg_offset = 0, .mask = MAX14577_INT1_ADCLOW_MASK, },
	{ .reg_offset = 0, .mask = MAX14577_INT1_ADCERR_MASK, },
	{ .reg_offset = 0, .mask = MAX77836_INT1_ADC1K_MASK, },
	/* INT2 interrupts */
	{ .reg_offset = 1, .mask = MAX14577_INT2_CHGTYP_MASK, },
	{ .reg_offset = 1, .mask = MAX14577_INT2_CHGDETRUN_MASK, },
	{ .reg_offset = 1, .mask = MAX14577_INT2_DCDTMR_MASK, },
	{ .reg_offset = 1, .mask = MAX14577_INT2_DBCHG_MASK, },
	{ .reg_offset = 1, .mask = MAX14577_INT2_VBVOLT_MASK, },
	{ .reg_offset = 1, .mask = MAX77836_INT2_VIDRM_MASK, },
	/* INT3 interrupts */
	{ .reg_offset = 2, .mask = MAX14577_INT3_EOC_MASK, },
	{ .reg_offset = 2, .mask = MAX14577_INT3_CGMBC_MASK, },
	{ .reg_offset = 2, .mask = MAX14577_INT3_OVP_MASK, },
	{ .reg_offset = 2, .mask = MAX14577_INT3_MBCCHGERR_MASK, },
};

static const struct regmap_irq_chip max77836_muic_irq_chip = {
	.name			= "max77836-muic",
	.status_base		= MAX14577_REG_INT1,
	.unmask_base		= MAX14577_REG_INTMASK1,
	.num_regs		= 3,
	.irqs			= max77836_muic_irqs,
	.num_irqs		= ARRAY_SIZE(max77836_muic_irqs),
};

static const struct regmap_irq max77836_pmic_irqs[] = {
	{ .reg_offset = 0, .mask = MAX77836_TOPSYS_INT_T120C_MASK, },
	{ .reg_offset = 0, .mask = MAX77836_TOPSYS_INT_T140C_MASK, },
};

static const struct regmap_irq_chip max77836_pmic_irq_chip = {
	.name			= "max77836-pmic",
	.status_base		= MAX77836_PMIC_REG_TOPSYS_INT,
	.mask_base		= MAX77836_PMIC_REG_TOPSYS_INT_MASK,
	.num_regs		= 1,
	.irqs			= max77836_pmic_irqs,
	.num_irqs		= ARRAY_SIZE(max77836_pmic_irqs),
};

static void max14577_print_dev_type(struct max14577 *max14577)
{
	u8 reg_data, vendor_id, device_id;
	int ret;

	ret = max14577_read_reg(max14577->regmap, MAX14577_REG_DEVICEID,
			&reg_data);
	if (ret) {
		dev_err(max14577->dev,
			"Failed to read DEVICEID register: %d\n", ret);
		return;
	}

	vendor_id = ((reg_data & DEVID_VENDORID_MASK) >>
				DEVID_VENDORID_SHIFT);
	device_id = ((reg_data & DEVID_DEVICEID_MASK) >>
				DEVID_DEVICEID_SHIFT);

	dev_info(max14577->dev, "Device type: %u (ID: 0x%x, vendor: 0x%x)\n",
			max14577->dev_type, device_id, vendor_id);
}

/*
 * Max77836 specific initialization code for driver probe.
 * Adds new I2C dummy device, regmap and regmap IRQ chip.
 * Unmasks Interrupt Source register.
 *
 * On success returns 0.
 * On failure returns errno and reverts any changes done so far (e.g. remove
 * I2C dummy device), except masking the INT SRC register.
 */
static int max77836_init(struct max14577 *max14577)
{
	int ret;
	u8 intsrc_mask;

	max14577->i2c_pmic = i2c_new_dummy_device(max14577->i2c->adapter,
			I2C_ADDR_PMIC);
	if (IS_ERR(max14577->i2c_pmic)) {
		dev_err(max14577->dev, "Failed to register PMIC I2C device\n");
		return PTR_ERR(max14577->i2c_pmic);
	}
	i2c_set_clientdata(max14577->i2c_pmic, max14577);

	max14577->regmap_pmic = devm_regmap_init_i2c(max14577->i2c_pmic,
			&max77836_pmic_regmap_config);
	if (IS_ERR(max14577->regmap_pmic)) {
		ret = PTR_ERR(max14577->regmap_pmic);
		dev_err(max14577->dev, "Failed to allocate PMIC register map: %d\n",
				ret);
		goto err;
	}

	/* Un-mask MAX77836 Interrupt Source register */
	ret = max14577_read_reg(max14577->regmap_pmic,
			MAX77836_PMIC_REG_INTSRC_MASK, &intsrc_mask);
	if (ret < 0) {
		dev_err(max14577->dev, "Failed to read PMIC register\n");
		goto err;
	}

	intsrc_mask &= ~(MAX77836_INTSRC_MASK_TOP_INT_MASK);
	intsrc_mask &= ~(MAX77836_INTSRC_MASK_MUIC_CHG_INT_MASK);
	ret = max14577_write_reg(max14577->regmap_pmic,
			MAX77836_PMIC_REG_INTSRC_MASK, intsrc_mask);
	if (ret < 0) {
		dev_err(max14577->dev, "Failed to write PMIC register\n");
		goto err;
	}

	ret = regmap_add_irq_chip(max14577->regmap_pmic, max14577->irq,
			IRQF_ONESHOT | IRQF_SHARED,
			0, &max77836_pmic_irq_chip,
			&max14577->irq_data_pmic);
	if (ret != 0) {
		dev_err(max14577->dev, "Failed to request PMIC IRQ %d: %d\n",
				max14577->irq, ret);
		goto err;
	}

	return 0;

err:
	i2c_unregister_device(max14577->i2c_pmic);

	return ret;
}

/*
 * Max77836 specific de-initialization code for driver remove.
 */
static void max77836_remove(struct max14577 *max14577)
{
	regmap_del_irq_chip(max14577->irq, max14577->irq_data_pmic);
	i2c_unregister_device(max14577->i2c_pmic);
}

static int max14577_i2c_probe(struct i2c_client *i2c)
{
	const struct i2c_device_id *id = i2c_client_get_device_id(i2c);
	struct max14577 *max14577;
	struct max14577_platform_data *pdata = dev_get_platdata(&i2c->dev);
	struct device_node *np = i2c->dev.of_node;
	int ret = 0;
	const struct regmap_irq_chip *irq_chip;
	const struct mfd_cell *mfd_devs;
	unsigned int mfd_devs_size;
	int irq_flags;

	if (np) {
		pdata = devm_kzalloc(&i2c->dev, sizeof(*pdata), GFP_KERNEL);
		if (!pdata)
			return -ENOMEM;
		i2c->dev.platform_data = pdata;
	}

	if (!pdata) {
		dev_err(&i2c->dev, "No platform data found.\n");
		return -EINVAL;
	}

	max14577 = devm_kzalloc(&i2c->dev, sizeof(*max14577), GFP_KERNEL);
	if (!max14577)
		return -ENOMEM;

	i2c_set_clientdata(i2c, max14577);
	max14577->dev = &i2c->dev;
	max14577->i2c = i2c;
	max14577->irq = i2c->irq;

	max14577->regmap = devm_regmap_init_i2c(i2c,
			&max14577_muic_regmap_config);
	if (IS_ERR(max14577->regmap)) {
		ret = PTR_ERR(max14577->regmap);
		dev_err(max14577->dev, "Failed to allocate register map: %d\n",
				ret);
		return ret;
	}

	if (np) {
		const struct of_device_id *of_id;

		of_id = of_match_device(max14577_dt_match, &i2c->dev);
		if (of_id)
			max14577->dev_type = (uintptr_t)of_id->data;
	} else {
		max14577->dev_type = id->driver_data;
	}

	max14577_print_dev_type(max14577);

	switch (max14577->dev_type) {
	case MAXIM_DEVICE_TYPE_MAX77836:
		irq_chip = &max77836_muic_irq_chip;
		mfd_devs = max77836_devs;
		mfd_devs_size = ARRAY_SIZE(max77836_devs);
		irq_flags = IRQF_ONESHOT | IRQF_SHARED;
		break;
	case MAXIM_DEVICE_TYPE_MAX14577:
	default:
		irq_chip = &max14577_irq_chip;
		mfd_devs = max14577_devs;
		mfd_devs_size = ARRAY_SIZE(max14577_devs);
		irq_flags = IRQF_ONESHOT;
		break;
	}

	ret = regmap_add_irq_chip(max14577->regmap, max14577->irq,
				  irq_flags, 0, irq_chip,
				  &max14577->irq_data);
	if (ret != 0) {
		dev_err(&i2c->dev, "Failed to request IRQ %d: %d\n",
				max14577->irq, ret);
		return ret;
	}

	/* Max77836 specific initialization code (additional regmap) */
	if (max14577->dev_type == MAXIM_DEVICE_TYPE_MAX77836) {
		ret = max77836_init(max14577);
		if (ret < 0)
			goto err_max77836;
	}

	ret = mfd_add_devices(max14577->dev, -1, mfd_devs,
			mfd_devs_size, NULL, 0, NULL);
	if (ret < 0)
		goto err_mfd;

	device_init_wakeup(max14577->dev, 1);

	return 0;

err_mfd:
	if (max14577->dev_type == MAXIM_DEVICE_TYPE_MAX77836)
		max77836_remove(max14577);
err_max77836:
	regmap_del_irq_chip(max14577->irq, max14577->irq_data);

	return ret;
}

static void max14577_i2c_remove(struct i2c_client *i2c)
{
	struct max14577 *max14577 = i2c_get_clientdata(i2c);

	mfd_remove_devices(max14577->dev);
	regmap_del_irq_chip(max14577->irq, max14577->irq_data);
	if (max14577->dev_type == MAXIM_DEVICE_TYPE_MAX77836)
		max77836_remove(max14577);
}

static const struct i2c_device_id max14577_i2c_id[] = {
	{ "max14577", MAXIM_DEVICE_TYPE_MAX14577, },
	{ "max77836", MAXIM_DEVICE_TYPE_MAX77836, },
	{ }
};
MODULE_DEVICE_TABLE(i2c, max14577_i2c_id);

static int max14577_suspend(struct device *dev)
{
	struct i2c_client *i2c = to_i2c_client(dev);
	struct max14577 *max14577 = i2c_get_clientdata(i2c);

	if (device_may_wakeup(dev))
		enable_irq_wake(max14577->irq);
	/*
	 * MUIC IRQ must be disabled during suspend because if it happens
	 * while suspended it will be handled before resuming I2C.
	 *
	 * When device is woken up from suspend (e.g. by ADC change),
	 * an interrupt occurs before resuming I2C bus controller.
	 * Interrupt handler tries to read registers but this read
	 * will fail because I2C is still suspended.
	 */
	disable_irq(max14577->irq);

	return 0;
}

static int max14577_resume(struct device *dev)
{
	struct i2c_client *i2c = to_i2c_client(dev);
	struct max14577 *max14577 = i2c_get_clientdata(i2c);

	if (device_may_wakeup(dev))
		disable_irq_wake(max14577->irq);
	enable_irq(max14577->irq);

	return 0;
}

static DEFINE_SIMPLE_DEV_PM_OPS(max14577_pm, max14577_suspend, max14577_resume);

static struct i2c_driver max14577_i2c_driver = {
	.driver = {
		.name = "max14577",
		.pm = pm_sleep_ptr(&max14577_pm),
		.of_match_table = max14577_dt_match,
	},
	.probe = max14577_i2c_probe,
	.remove = max14577_i2c_remove,
	.id_table = max14577_i2c_id,
};

static int __init max14577_i2c_init(void)
{
	BUILD_BUG_ON(ARRAY_SIZE(max14577_i2c_id) != MAXIM_DEVICE_TYPE_NUM);
	BUILD_BUG_ON(ARRAY_SIZE(max14577_dt_match) != MAXIM_DEVICE_TYPE_NUM);

	/* Valid charger current values must be provided for each chipset */
	BUILD_BUG_ON(ARRAY_SIZE(maxim_charger_currents) != MAXIM_DEVICE_TYPE_NUM);

	/* Check for valid values for charger */
	BUILD_BUG_ON(MAX14577_CHARGER_CURRENT_LIMIT_HIGH_START +
			MAX14577_CHARGER_CURRENT_LIMIT_HIGH_STEP * 0xf !=
			MAX14577_CHARGER_CURRENT_LIMIT_MAX);
	BUILD_BUG_ON(MAX14577_CHARGER_CURRENT_LIMIT_HIGH_STEP == 0);

	BUILD_BUG_ON(MAX77836_CHARGER_CURRENT_LIMIT_HIGH_START +
			MAX77836_CHARGER_CURRENT_LIMIT_HIGH_STEP * 0xf !=
			MAX77836_CHARGER_CURRENT_LIMIT_MAX);
	BUILD_BUG_ON(MAX77836_CHARGER_CURRENT_LIMIT_HIGH_STEP == 0);

	return i2c_add_driver(&max14577_i2c_driver);
}
module_init(max14577_i2c_init);

static void __exit max14577_i2c_exit(void)
{
	i2c_del_driver(&max14577_i2c_driver);
}
module_exit(max14577_i2c_exit);

MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>, Krzysztof Kozlowski <krzk@kernel.org>");
MODULE_DESCRIPTION("Maxim 14577/77836 multi-function core driver");
MODULE_LICENSE("GPL");