summaryrefslogtreecommitdiff
path: root/drivers/mfd/da9062-core.c
blob: dbbc4779170a0f224469e6dbe7b4d6f53eb4188d (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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Core, IRQ and I2C device driver for DA9061 and DA9062 PMICs
 * Copyright (C) 2015-2017  Dialog Semiconductor
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/interrupt.h>
#include <linux/of.h>
#include <linux/regmap.h>
#include <linux/irq.h>
#include <linux/mfd/core.h>
#include <linux/i2c.h>
#include <linux/mfd/da9062/core.h>
#include <linux/mfd/da9062/registers.h>
#include <linux/regulator/of_regulator.h>

#define	DA9062_REG_EVENT_A_OFFSET	0
#define	DA9062_REG_EVENT_B_OFFSET	1
#define	DA9062_REG_EVENT_C_OFFSET	2

#define	DA9062_IRQ_LOW	0
#define	DA9062_IRQ_HIGH	1

static struct regmap_irq da9061_irqs[] = {
	/* EVENT A */
	[DA9061_IRQ_ONKEY] = {
		.reg_offset = DA9062_REG_EVENT_A_OFFSET,
		.mask = DA9062AA_M_NONKEY_MASK,
	},
	[DA9061_IRQ_WDG_WARN] = {
		.reg_offset = DA9062_REG_EVENT_A_OFFSET,
		.mask = DA9062AA_M_WDG_WARN_MASK,
	},
	[DA9061_IRQ_SEQ_RDY] = {
		.reg_offset = DA9062_REG_EVENT_A_OFFSET,
		.mask = DA9062AA_M_SEQ_RDY_MASK,
	},
	/* EVENT B */
	[DA9061_IRQ_TEMP] = {
		.reg_offset = DA9062_REG_EVENT_B_OFFSET,
		.mask = DA9062AA_M_TEMP_MASK,
	},
	[DA9061_IRQ_LDO_LIM] = {
		.reg_offset = DA9062_REG_EVENT_B_OFFSET,
		.mask = DA9062AA_M_LDO_LIM_MASK,
	},
	[DA9061_IRQ_DVC_RDY] = {
		.reg_offset = DA9062_REG_EVENT_B_OFFSET,
		.mask = DA9062AA_M_DVC_RDY_MASK,
	},
	[DA9061_IRQ_VDD_WARN] = {
		.reg_offset = DA9062_REG_EVENT_B_OFFSET,
		.mask = DA9062AA_M_VDD_WARN_MASK,
	},
	/* EVENT C */
	[DA9061_IRQ_GPI0] = {
		.reg_offset = DA9062_REG_EVENT_C_OFFSET,
		.mask = DA9062AA_M_GPI0_MASK,
	},
	[DA9061_IRQ_GPI1] = {
		.reg_offset = DA9062_REG_EVENT_C_OFFSET,
		.mask = DA9062AA_M_GPI1_MASK,
	},
	[DA9061_IRQ_GPI2] = {
		.reg_offset = DA9062_REG_EVENT_C_OFFSET,
		.mask = DA9062AA_M_GPI2_MASK,
	},
	[DA9061_IRQ_GPI3] = {
		.reg_offset = DA9062_REG_EVENT_C_OFFSET,
		.mask = DA9062AA_M_GPI3_MASK,
	},
	[DA9061_IRQ_GPI4] = {
		.reg_offset = DA9062_REG_EVENT_C_OFFSET,
		.mask = DA9062AA_M_GPI4_MASK,
	},
};

static struct regmap_irq_chip da9061_irq_chip = {
	.name = "da9061-irq",
	.irqs = da9061_irqs,
	.num_irqs = DA9061_NUM_IRQ,
	.num_regs = 3,
	.status_base = DA9062AA_EVENT_A,
	.mask_base = DA9062AA_IRQ_MASK_A,
	.ack_base = DA9062AA_EVENT_A,
};

static struct regmap_irq da9062_irqs[] = {
	/* EVENT A */
	[DA9062_IRQ_ONKEY] = {
		.reg_offset = DA9062_REG_EVENT_A_OFFSET,
		.mask = DA9062AA_M_NONKEY_MASK,
	},
	[DA9062_IRQ_ALARM] = {
		.reg_offset = DA9062_REG_EVENT_A_OFFSET,
		.mask = DA9062AA_M_ALARM_MASK,
	},
	[DA9062_IRQ_TICK] = {
		.reg_offset = DA9062_REG_EVENT_A_OFFSET,
		.mask = DA9062AA_M_TICK_MASK,
	},
	[DA9062_IRQ_WDG_WARN] = {
		.reg_offset = DA9062_REG_EVENT_A_OFFSET,
		.mask = DA9062AA_M_WDG_WARN_MASK,
	},
	[DA9062_IRQ_SEQ_RDY] = {
		.reg_offset = DA9062_REG_EVENT_A_OFFSET,
		.mask = DA9062AA_M_SEQ_RDY_MASK,
	},
	/* EVENT B */
	[DA9062_IRQ_TEMP] = {
		.reg_offset = DA9062_REG_EVENT_B_OFFSET,
		.mask = DA9062AA_M_TEMP_MASK,
	},
	[DA9062_IRQ_LDO_LIM] = {
		.reg_offset = DA9062_REG_EVENT_B_OFFSET,
		.mask = DA9062AA_M_LDO_LIM_MASK,
	},
	[DA9062_IRQ_DVC_RDY] = {
		.reg_offset = DA9062_REG_EVENT_B_OFFSET,
		.mask = DA9062AA_M_DVC_RDY_MASK,
	},
	[DA9062_IRQ_VDD_WARN] = {
		.reg_offset = DA9062_REG_EVENT_B_OFFSET,
		.mask = DA9062AA_M_VDD_WARN_MASK,
	},
	/* EVENT C */
	[DA9062_IRQ_GPI0] = {
		.reg_offset = DA9062_REG_EVENT_C_OFFSET,
		.mask = DA9062AA_M_GPI0_MASK,
	},
	[DA9062_IRQ_GPI1] = {
		.reg_offset = DA9062_REG_EVENT_C_OFFSET,
		.mask = DA9062AA_M_GPI1_MASK,
	},
	[DA9062_IRQ_GPI2] = {
		.reg_offset = DA9062_REG_EVENT_C_OFFSET,
		.mask = DA9062AA_M_GPI2_MASK,
	},
	[DA9062_IRQ_GPI3] = {
		.reg_offset = DA9062_REG_EVENT_C_OFFSET,
		.mask = DA9062AA_M_GPI3_MASK,
	},
	[DA9062_IRQ_GPI4] = {
		.reg_offset = DA9062_REG_EVENT_C_OFFSET,
		.mask = DA9062AA_M_GPI4_MASK,
	},
};

static struct regmap_irq_chip da9062_irq_chip = {
	.name = "da9062-irq",
	.irqs = da9062_irqs,
	.num_irqs = DA9062_NUM_IRQ,
	.num_regs = 3,
	.status_base = DA9062AA_EVENT_A,
	.mask_base = DA9062AA_IRQ_MASK_A,
	.ack_base = DA9062AA_EVENT_A,
};

static const struct resource da9061_core_resources[] = {
	DEFINE_RES_IRQ_NAMED(DA9061_IRQ_VDD_WARN, "VDD_WARN"),
};

static const struct resource da9061_regulators_resources[] = {
	DEFINE_RES_IRQ_NAMED(DA9061_IRQ_LDO_LIM, "LDO_LIM"),
};

static const struct resource da9061_thermal_resources[] = {
	DEFINE_RES_IRQ_NAMED(DA9061_IRQ_TEMP, "THERMAL"),
};

static const struct resource da9061_wdt_resources[] = {
	DEFINE_RES_IRQ_NAMED(DA9061_IRQ_WDG_WARN, "WD_WARN"),
};

static const struct resource da9061_onkey_resources[] = {
	DEFINE_RES_IRQ_NAMED(DA9061_IRQ_ONKEY, "ONKEY"),
};

static const struct mfd_cell da9061_devs_irq[] = {
	MFD_CELL_OF("da9061-core", da9061_core_resources, NULL, 0, 0,
		    NULL),
	MFD_CELL_OF("da9062-regulators", da9061_regulators_resources, NULL, 0, 0,
		    NULL),
	MFD_CELL_OF("da9061-watchdog", da9061_wdt_resources, NULL, 0, 0,
		    "dlg,da9061-watchdog"),
	MFD_CELL_OF("da9061-thermal", da9061_thermal_resources, NULL, 0, 0,
		    "dlg,da9061-thermal"),
	MFD_CELL_OF("da9061-onkey", da9061_onkey_resources, NULL, 0, 0,
		    "dlg,da9061-onkey"),
};

static const struct mfd_cell da9061_devs_noirq[] = {
	MFD_CELL_OF("da9061-core", NULL, NULL, 0, 0, NULL),
	MFD_CELL_OF("da9062-regulators", NULL, NULL, 0, 0, NULL),
	MFD_CELL_OF("da9061-watchdog", NULL, NULL, 0, 0, "dlg,da9061-watchdog"),
	MFD_CELL_OF("da9061-thermal", NULL, NULL, 0, 0, "dlg,da9061-thermal"),
	MFD_CELL_OF("da9061-onkey", NULL, NULL, 0, 0, "dlg,da9061-onkey"),
};

static const struct resource da9062_core_resources[] = {
	DEFINE_RES_NAMED(DA9062_IRQ_VDD_WARN, 1, "VDD_WARN", IORESOURCE_IRQ),
};

static const struct resource da9062_regulators_resources[] = {
	DEFINE_RES_NAMED(DA9062_IRQ_LDO_LIM, 1, "LDO_LIM", IORESOURCE_IRQ),
};

static const struct resource da9062_thermal_resources[] = {
	DEFINE_RES_NAMED(DA9062_IRQ_TEMP, 1, "THERMAL", IORESOURCE_IRQ),
};

static const struct resource da9062_wdt_resources[] = {
	DEFINE_RES_NAMED(DA9062_IRQ_WDG_WARN, 1, "WD_WARN", IORESOURCE_IRQ),
};

static const struct resource da9062_rtc_resources[] = {
	DEFINE_RES_NAMED(DA9062_IRQ_ALARM, 1, "ALARM", IORESOURCE_IRQ),
	DEFINE_RES_NAMED(DA9062_IRQ_TICK, 1, "TICK", IORESOURCE_IRQ),
};

static const struct resource da9062_onkey_resources[] = {
	DEFINE_RES_NAMED(DA9062_IRQ_ONKEY, 1, "ONKEY", IORESOURCE_IRQ),
};

static const struct resource da9062_gpio_resources[] = {
	DEFINE_RES_NAMED(DA9062_IRQ_GPI0, 1, "GPI0", IORESOURCE_IRQ),
	DEFINE_RES_NAMED(DA9062_IRQ_GPI1, 1, "GPI1", IORESOURCE_IRQ),
	DEFINE_RES_NAMED(DA9062_IRQ_GPI2, 1, "GPI2", IORESOURCE_IRQ),
	DEFINE_RES_NAMED(DA9062_IRQ_GPI3, 1, "GPI3", IORESOURCE_IRQ),
	DEFINE_RES_NAMED(DA9062_IRQ_GPI4, 1, "GPI4", IORESOURCE_IRQ),
};

static const struct mfd_cell da9062_devs_irq[] = {
	MFD_CELL_OF("da9062-core", da9062_core_resources, NULL, 0, 0,
		    NULL),
	MFD_CELL_OF("da9062-regulators", da9062_regulators_resources, NULL, 0, 0,
		    NULL),
	MFD_CELL_OF("da9062-watchdog", da9062_wdt_resources, NULL, 0, 0,
		    "dlg,da9062-watchdog"),
	MFD_CELL_OF("da9062-thermal", da9062_thermal_resources, NULL, 0, 0,
		    "dlg,da9062-thermal"),
	MFD_CELL_OF("da9062-rtc", da9062_rtc_resources, NULL, 0, 0,
		    "dlg,da9062-rtc"),
	MFD_CELL_OF("da9062-onkey", da9062_onkey_resources, NULL, 0, 0,
		    "dlg,da9062-onkey"),
	MFD_CELL_OF("da9062-gpio", da9062_gpio_resources, NULL, 0, 0,
		    "dlg,da9062-gpio"),
};

static const struct mfd_cell da9062_devs_noirq[] = {
	MFD_CELL_OF("da9062-core", NULL, NULL, 0, 0, NULL),
	MFD_CELL_OF("da9062-regulators", NULL, NULL, 0, 0, NULL),
	MFD_CELL_OF("da9062-watchdog", NULL, NULL, 0, 0, "dlg,da9062-watchdog"),
	MFD_CELL_OF("da9062-thermal", NULL, NULL, 0, 0, "dlg,da9062-thermal"),
	MFD_CELL_OF("da9062-rtc", NULL, NULL, 0, 0, "dlg,da9062-rtc"),
	MFD_CELL_OF("da9062-onkey", NULL, NULL, 0, 0, "dlg,da9062-onkey"),
	MFD_CELL_OF("da9062-gpio", NULL, NULL, 0, 0, "dlg,da9062-gpio"),
};

static int da9062_clear_fault_log(struct da9062 *chip)
{
	int ret;
	int fault_log;

	ret = regmap_read(chip->regmap, DA9062AA_FAULT_LOG, &fault_log);
	if (ret < 0)
		return ret;

	if (fault_log) {
		if (fault_log & DA9062AA_TWD_ERROR_MASK)
			dev_dbg(chip->dev, "Fault log entry detected: TWD_ERROR\n");
		if (fault_log & DA9062AA_POR_MASK)
			dev_dbg(chip->dev, "Fault log entry detected: POR\n");
		if (fault_log & DA9062AA_VDD_FAULT_MASK)
			dev_dbg(chip->dev, "Fault log entry detected: VDD_FAULT\n");
		if (fault_log & DA9062AA_VDD_START_MASK)
			dev_dbg(chip->dev, "Fault log entry detected: VDD_START\n");
		if (fault_log & DA9062AA_TEMP_CRIT_MASK)
			dev_dbg(chip->dev, "Fault log entry detected: TEMP_CRIT\n");
		if (fault_log & DA9062AA_KEY_RESET_MASK)
			dev_dbg(chip->dev, "Fault log entry detected: KEY_RESET\n");
		if (fault_log & DA9062AA_NSHUTDOWN_MASK)
			dev_dbg(chip->dev, "Fault log entry detected: NSHUTDOWN\n");
		if (fault_log & DA9062AA_WAIT_SHUT_MASK)
			dev_dbg(chip->dev, "Fault log entry detected: WAIT_SHUT\n");

		ret = regmap_write(chip->regmap, DA9062AA_FAULT_LOG,
				   fault_log);
	}

	return ret;
}

static int da9062_get_device_type(struct da9062 *chip)
{
	int device_id, variant_id, variant_mrc, variant_vrc;
	char *type;
	int ret;

	ret = regmap_read(chip->regmap, DA9062AA_DEVICE_ID, &device_id);
	if (ret < 0) {
		dev_err(chip->dev, "Cannot read chip ID.\n");
		return -EIO;
	}
	if (device_id != DA9062_PMIC_DEVICE_ID) {
		dev_err(chip->dev, "Invalid device ID: 0x%02x\n", device_id);
		return -ENODEV;
	}

	ret = regmap_read(chip->regmap, DA9062AA_VARIANT_ID, &variant_id);
	if (ret < 0) {
		dev_err(chip->dev, "Cannot read chip variant id.\n");
		return -EIO;
	}

	variant_vrc = (variant_id & DA9062AA_VRC_MASK) >> DA9062AA_VRC_SHIFT;

	switch (variant_vrc) {
	case DA9062_PMIC_VARIANT_VRC_DA9061:
		type = "DA9061";
		break;
	case DA9062_PMIC_VARIANT_VRC_DA9062:
		type = "DA9062";
		break;
	default:
		type = "Unknown";
		break;
	}

	dev_info(chip->dev,
		 "Device detected (device-ID: 0x%02X, var-ID: 0x%02X, %s)\n",
		 device_id, variant_id, type);

	variant_mrc = (variant_id & DA9062AA_MRC_MASK) >> DA9062AA_MRC_SHIFT;

	if (variant_mrc < DA9062_PMIC_VARIANT_MRC_AA) {
		dev_err(chip->dev,
			"Cannot support variant MRC: 0x%02X\n", variant_mrc);
		return -ENODEV;
	}

	return ret;
}

static u32 da9062_configure_irq_type(struct da9062 *chip, int irq, u32 *trigger)
{
	u32 irq_type = 0;
	struct irq_data *irq_data = irq_get_irq_data(irq);

	if (!irq_data) {
		dev_err(chip->dev, "Invalid IRQ: %d\n", irq);
		return -EINVAL;
	}
	*trigger = irqd_get_trigger_type(irq_data);

	switch (*trigger) {
	case IRQ_TYPE_LEVEL_HIGH:
		irq_type = DA9062_IRQ_HIGH;
		break;
	case IRQ_TYPE_LEVEL_LOW:
		irq_type = DA9062_IRQ_LOW;
		break;
	default:
		dev_warn(chip->dev, "Unsupported IRQ type: %d\n", *trigger);
		return -EINVAL;
	}
	return regmap_update_bits(chip->regmap, DA9062AA_CONFIG_A,
			DA9062AA_IRQ_TYPE_MASK,
			irq_type << DA9062AA_IRQ_TYPE_SHIFT);
}

static const struct regmap_range da9061_aa_readable_ranges[] = {
	regmap_reg_range(DA9062AA_PAGE_CON, DA9062AA_STATUS_B),
	regmap_reg_range(DA9062AA_STATUS_D, DA9062AA_EVENT_C),
	regmap_reg_range(DA9062AA_IRQ_MASK_A, DA9062AA_IRQ_MASK_C),
	regmap_reg_range(DA9062AA_CONTROL_A, DA9062AA_GPIO_4),
	regmap_reg_range(DA9062AA_GPIO_WKUP_MODE, DA9062AA_GPIO_OUT3_4),
	regmap_reg_range(DA9062AA_BUCK1_CONT, DA9062AA_BUCK4_CONT),
	regmap_reg_range(DA9062AA_BUCK3_CONT, DA9062AA_BUCK3_CONT),
	regmap_reg_range(DA9062AA_LDO1_CONT, DA9062AA_LDO4_CONT),
	regmap_reg_range(DA9062AA_DVC_1, DA9062AA_DVC_1),
	regmap_reg_range(DA9062AA_SEQ, DA9062AA_ID_4_3),
	regmap_reg_range(DA9062AA_ID_12_11, DA9062AA_ID_16_15),
	regmap_reg_range(DA9062AA_ID_22_21, DA9062AA_ID_32_31),
	regmap_reg_range(DA9062AA_SEQ_A, DA9062AA_WAIT),
	regmap_reg_range(DA9062AA_RESET, DA9062AA_BUCK_ILIM_C),
	regmap_reg_range(DA9062AA_BUCK1_CFG, DA9062AA_BUCK3_CFG),
	regmap_reg_range(DA9062AA_VBUCK1_A, DA9062AA_VBUCK4_A),
	regmap_reg_range(DA9062AA_VBUCK3_A, DA9062AA_VBUCK3_A),
	regmap_reg_range(DA9062AA_VLDO1_A, DA9062AA_VLDO4_A),
	regmap_reg_range(DA9062AA_CONFIG_A, DA9062AA_CONFIG_A),
	regmap_reg_range(DA9062AA_VBUCK1_B, DA9062AA_VBUCK4_B),
	regmap_reg_range(DA9062AA_VBUCK3_B, DA9062AA_VBUCK3_B),
	regmap_reg_range(DA9062AA_VLDO1_B, DA9062AA_VLDO4_B),
	regmap_reg_range(DA9062AA_INTERFACE, DA9062AA_CONFIG_E),
	regmap_reg_range(DA9062AA_CONFIG_G, DA9062AA_CONFIG_K),
	regmap_reg_range(DA9062AA_CONFIG_M, DA9062AA_CONFIG_M),
	regmap_reg_range(DA9062AA_GP_ID_0, DA9062AA_GP_ID_19),
	regmap_reg_range(DA9062AA_DEVICE_ID, DA9062AA_CONFIG_ID),
};

static const struct regmap_range da9061_aa_writeable_ranges[] = {
	regmap_reg_range(DA9062AA_PAGE_CON, DA9062AA_PAGE_CON),
	regmap_reg_range(DA9062AA_FAULT_LOG, DA9062AA_EVENT_C),
	regmap_reg_range(DA9062AA_IRQ_MASK_A, DA9062AA_IRQ_MASK_C),
	regmap_reg_range(DA9062AA_CONTROL_A, DA9062AA_GPIO_4),
	regmap_reg_range(DA9062AA_GPIO_WKUP_MODE, DA9062AA_GPIO_OUT3_4),
	regmap_reg_range(DA9062AA_BUCK1_CONT, DA9062AA_BUCK4_CONT),
	regmap_reg_range(DA9062AA_BUCK3_CONT, DA9062AA_BUCK3_CONT),
	regmap_reg_range(DA9062AA_LDO1_CONT, DA9062AA_LDO4_CONT),
	regmap_reg_range(DA9062AA_DVC_1, DA9062AA_DVC_1),
	regmap_reg_range(DA9062AA_SEQ, DA9062AA_ID_4_3),
	regmap_reg_range(DA9062AA_ID_12_11, DA9062AA_ID_16_15),
	regmap_reg_range(DA9062AA_ID_22_21, DA9062AA_ID_32_31),
	regmap_reg_range(DA9062AA_SEQ_A, DA9062AA_WAIT),
	regmap_reg_range(DA9062AA_RESET, DA9062AA_BUCK_ILIM_C),
	regmap_reg_range(DA9062AA_BUCK1_CFG, DA9062AA_BUCK3_CFG),
	regmap_reg_range(DA9062AA_VBUCK1_A, DA9062AA_VBUCK4_A),
	regmap_reg_range(DA9062AA_VBUCK3_A, DA9062AA_VBUCK3_A),
	regmap_reg_range(DA9062AA_VLDO1_A, DA9062AA_VLDO4_A),
	regmap_reg_range(DA9062AA_CONFIG_A, DA9062AA_CONFIG_A),
	regmap_reg_range(DA9062AA_VBUCK1_B, DA9062AA_VBUCK4_B),
	regmap_reg_range(DA9062AA_VBUCK3_B, DA9062AA_VBUCK3_B),
	regmap_reg_range(DA9062AA_VLDO1_B, DA9062AA_VLDO4_B),
	regmap_reg_range(DA9062AA_CONFIG_J, DA9062AA_CONFIG_J),
	regmap_reg_range(DA9062AA_GP_ID_0, DA9062AA_GP_ID_19),
};

static const struct regmap_range da9061_aa_volatile_ranges[] = {
	regmap_reg_range(DA9062AA_PAGE_CON, DA9062AA_STATUS_B),
	regmap_reg_range(DA9062AA_STATUS_D, DA9062AA_EVENT_C),
	regmap_reg_range(DA9062AA_CONTROL_A, DA9062AA_CONTROL_B),
	regmap_reg_range(DA9062AA_CONTROL_E, DA9062AA_CONTROL_F),
	regmap_reg_range(DA9062AA_BUCK1_CONT, DA9062AA_BUCK4_CONT),
	regmap_reg_range(DA9062AA_BUCK3_CONT, DA9062AA_BUCK3_CONT),
	regmap_reg_range(DA9062AA_LDO1_CONT, DA9062AA_LDO4_CONT),
	regmap_reg_range(DA9062AA_DVC_1, DA9062AA_DVC_1),
	regmap_reg_range(DA9062AA_SEQ, DA9062AA_SEQ),
};

static const struct regmap_access_table da9061_aa_readable_table = {
	.yes_ranges = da9061_aa_readable_ranges,
	.n_yes_ranges = ARRAY_SIZE(da9061_aa_readable_ranges),
};

static const struct regmap_access_table da9061_aa_writeable_table = {
	.yes_ranges = da9061_aa_writeable_ranges,
	.n_yes_ranges = ARRAY_SIZE(da9061_aa_writeable_ranges),
};

static const struct regmap_access_table da9061_aa_volatile_table = {
	.yes_ranges = da9061_aa_volatile_ranges,
	.n_yes_ranges = ARRAY_SIZE(da9061_aa_volatile_ranges),
};

static const struct regmap_range_cfg da9061_range_cfg[] = {
	{
		.range_min = DA9062AA_PAGE_CON,
		.range_max = DA9062AA_CONFIG_ID,
		.selector_reg = DA9062AA_PAGE_CON,
		.selector_mask = 1 << DA9062_I2C_PAGE_SEL_SHIFT,
		.selector_shift = DA9062_I2C_PAGE_SEL_SHIFT,
		.window_start = 0,
		.window_len = 256,
	}
};

static struct regmap_config da9061_regmap_config = {
	.reg_bits = 8,
	.val_bits = 8,
	.ranges = da9061_range_cfg,
	.num_ranges = ARRAY_SIZE(da9061_range_cfg),
	.max_register = DA9062AA_CONFIG_ID,
	.cache_type = REGCACHE_MAPLE,
	.rd_table = &da9061_aa_readable_table,
	.wr_table = &da9061_aa_writeable_table,
	.volatile_table = &da9061_aa_volatile_table,
};

static const struct regmap_range da9062_aa_readable_ranges[] = {
	regmap_reg_range(DA9062AA_PAGE_CON, DA9062AA_STATUS_B),
	regmap_reg_range(DA9062AA_STATUS_D, DA9062AA_EVENT_C),
	regmap_reg_range(DA9062AA_IRQ_MASK_A, DA9062AA_IRQ_MASK_C),
	regmap_reg_range(DA9062AA_CONTROL_A, DA9062AA_GPIO_4),
	regmap_reg_range(DA9062AA_GPIO_WKUP_MODE, DA9062AA_BUCK4_CONT),
	regmap_reg_range(DA9062AA_BUCK3_CONT, DA9062AA_BUCK3_CONT),
	regmap_reg_range(DA9062AA_LDO1_CONT, DA9062AA_LDO4_CONT),
	regmap_reg_range(DA9062AA_DVC_1, DA9062AA_DVC_1),
	regmap_reg_range(DA9062AA_COUNT_S, DA9062AA_SECOND_D),
	regmap_reg_range(DA9062AA_SEQ, DA9062AA_ID_4_3),
	regmap_reg_range(DA9062AA_ID_12_11, DA9062AA_ID_16_15),
	regmap_reg_range(DA9062AA_ID_22_21, DA9062AA_ID_32_31),
	regmap_reg_range(DA9062AA_SEQ_A, DA9062AA_BUCK3_CFG),
	regmap_reg_range(DA9062AA_VBUCK2_A, DA9062AA_VBUCK4_A),
	regmap_reg_range(DA9062AA_VBUCK3_A, DA9062AA_VBUCK3_A),
	regmap_reg_range(DA9062AA_VLDO1_A, DA9062AA_VLDO4_A),
	regmap_reg_range(DA9062AA_VBUCK2_B, DA9062AA_VBUCK4_B),
	regmap_reg_range(DA9062AA_VBUCK3_B, DA9062AA_VBUCK3_B),
	regmap_reg_range(DA9062AA_VLDO1_B, DA9062AA_VLDO4_B),
	regmap_reg_range(DA9062AA_BBAT_CONT, DA9062AA_BBAT_CONT),
	regmap_reg_range(DA9062AA_INTERFACE, DA9062AA_CONFIG_E),
	regmap_reg_range(DA9062AA_CONFIG_G, DA9062AA_CONFIG_K),
	regmap_reg_range(DA9062AA_CONFIG_M, DA9062AA_CONFIG_M),
	regmap_reg_range(DA9062AA_TRIM_CLDR, DA9062AA_GP_ID_19),
	regmap_reg_range(DA9062AA_DEVICE_ID, DA9062AA_CONFIG_ID),
};

static const struct regmap_range da9062_aa_writeable_ranges[] = {
	regmap_reg_range(DA9062AA_PAGE_CON, DA9062AA_PAGE_CON),
	regmap_reg_range(DA9062AA_FAULT_LOG, DA9062AA_EVENT_C),
	regmap_reg_range(DA9062AA_IRQ_MASK_A, DA9062AA_IRQ_MASK_C),
	regmap_reg_range(DA9062AA_CONTROL_A, DA9062AA_GPIO_4),
	regmap_reg_range(DA9062AA_GPIO_WKUP_MODE, DA9062AA_BUCK4_CONT),
	regmap_reg_range(DA9062AA_BUCK3_CONT, DA9062AA_BUCK3_CONT),
	regmap_reg_range(DA9062AA_LDO1_CONT, DA9062AA_LDO4_CONT),
	regmap_reg_range(DA9062AA_DVC_1, DA9062AA_DVC_1),
	regmap_reg_range(DA9062AA_COUNT_S, DA9062AA_ALARM_Y),
	regmap_reg_range(DA9062AA_SEQ, DA9062AA_ID_4_3),
	regmap_reg_range(DA9062AA_ID_12_11, DA9062AA_ID_16_15),
	regmap_reg_range(DA9062AA_ID_22_21, DA9062AA_ID_32_31),
	regmap_reg_range(DA9062AA_SEQ_A, DA9062AA_BUCK3_CFG),
	regmap_reg_range(DA9062AA_VBUCK2_A, DA9062AA_VBUCK4_A),
	regmap_reg_range(DA9062AA_VBUCK3_A, DA9062AA_VBUCK3_A),
	regmap_reg_range(DA9062AA_VLDO1_A, DA9062AA_VLDO4_A),
	regmap_reg_range(DA9062AA_VBUCK2_B, DA9062AA_VBUCK4_B),
	regmap_reg_range(DA9062AA_VBUCK3_B, DA9062AA_VBUCK3_B),
	regmap_reg_range(DA9062AA_VLDO1_B, DA9062AA_VLDO4_B),
	regmap_reg_range(DA9062AA_BBAT_CONT, DA9062AA_BBAT_CONT),
	regmap_reg_range(DA9062AA_CONFIG_J, DA9062AA_CONFIG_J),
	regmap_reg_range(DA9062AA_GP_ID_0, DA9062AA_GP_ID_19),
};

static const struct regmap_range da9062_aa_volatile_ranges[] = {
	regmap_reg_range(DA9062AA_PAGE_CON, DA9062AA_STATUS_B),
	regmap_reg_range(DA9062AA_STATUS_D, DA9062AA_EVENT_C),
	regmap_reg_range(DA9062AA_CONTROL_A, DA9062AA_CONTROL_B),
	regmap_reg_range(DA9062AA_CONTROL_E, DA9062AA_CONTROL_F),
	regmap_reg_range(DA9062AA_BUCK2_CONT, DA9062AA_BUCK4_CONT),
	regmap_reg_range(DA9062AA_BUCK3_CONT, DA9062AA_BUCK3_CONT),
	regmap_reg_range(DA9062AA_LDO1_CONT, DA9062AA_LDO4_CONT),
	regmap_reg_range(DA9062AA_DVC_1, DA9062AA_DVC_1),
	regmap_reg_range(DA9062AA_COUNT_S, DA9062AA_SECOND_D),
	regmap_reg_range(DA9062AA_SEQ, DA9062AA_SEQ),
	regmap_reg_range(DA9062AA_EN_32K, DA9062AA_EN_32K),
};

static const struct regmap_access_table da9062_aa_readable_table = {
	.yes_ranges = da9062_aa_readable_ranges,
	.n_yes_ranges = ARRAY_SIZE(da9062_aa_readable_ranges),
};

static const struct regmap_access_table da9062_aa_writeable_table = {
	.yes_ranges = da9062_aa_writeable_ranges,
	.n_yes_ranges = ARRAY_SIZE(da9062_aa_writeable_ranges),
};

static const struct regmap_access_table da9062_aa_volatile_table = {
	.yes_ranges = da9062_aa_volatile_ranges,
	.n_yes_ranges = ARRAY_SIZE(da9062_aa_volatile_ranges),
};

static const struct regmap_range_cfg da9062_range_cfg[] = {
	{
		.range_min = DA9062AA_PAGE_CON,
		.range_max = DA9062AA_CONFIG_ID,
		.selector_reg = DA9062AA_PAGE_CON,
		.selector_mask = 1 << DA9062_I2C_PAGE_SEL_SHIFT,
		.selector_shift = DA9062_I2C_PAGE_SEL_SHIFT,
		.window_start = 0,
		.window_len = 256,
	}
};

static struct regmap_config da9062_regmap_config = {
	.reg_bits = 8,
	.val_bits = 8,
	.ranges = da9062_range_cfg,
	.num_ranges = ARRAY_SIZE(da9062_range_cfg),
	.max_register = DA9062AA_CONFIG_ID,
	.cache_type = REGCACHE_MAPLE,
	.rd_table = &da9062_aa_readable_table,
	.wr_table = &da9062_aa_writeable_table,
	.volatile_table = &da9062_aa_volatile_table,
};

static int da9062_i2c_probe(struct i2c_client *i2c)
{
	struct da9062 *chip;
	unsigned int irq_base = 0;
	const struct mfd_cell *cell;
	const struct regmap_irq_chip *irq_chip;
	const struct regmap_config *config;
	int cell_num;
	u32 trigger_type = 0;
	int ret;

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

	chip->chip_type = (uintptr_t)i2c_get_match_data(i2c);

	i2c_set_clientdata(i2c, chip);
	chip->dev = &i2c->dev;

	/* Start with a base configuration without IRQ */
	switch (chip->chip_type) {
	case COMPAT_TYPE_DA9061:
		cell = da9061_devs_noirq;
		cell_num = ARRAY_SIZE(da9061_devs_noirq);
		config = &da9061_regmap_config;
		break;
	case COMPAT_TYPE_DA9062:
		cell = da9062_devs_noirq;
		cell_num = ARRAY_SIZE(da9062_devs_noirq);
		config = &da9062_regmap_config;
		break;
	default:
		dev_err(chip->dev, "Unrecognised chip type\n");
		return -ENODEV;
	}

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

	/* If SMBus is not available and only I2C is possible, enter I2C mode */
	if (i2c_check_functionality(i2c->adapter, I2C_FUNC_I2C)) {
		dev_info(chip->dev, "Entering I2C mode!\n");
		ret = regmap_clear_bits(chip->regmap, DA9062AA_CONFIG_J,
					DA9062AA_TWOWIRE_TO_MASK);
		if (ret < 0) {
			dev_err(chip->dev, "Failed to set Two-Wire Bus Mode.\n");
			return ret;
		}
	}

	ret = da9062_clear_fault_log(chip);
	if (ret < 0)
		dev_warn(chip->dev, "Cannot clear fault log\n");

	ret = da9062_get_device_type(chip);
	if (ret)
		return ret;

	/* If IRQ is available, reconfigure it accordingly */
	if (i2c->irq) {
		if (chip->chip_type == COMPAT_TYPE_DA9061) {
			cell = da9061_devs_irq;
			cell_num = ARRAY_SIZE(da9061_devs_irq);
			irq_chip = &da9061_irq_chip;
		} else {
			cell = da9062_devs_irq;
			cell_num = ARRAY_SIZE(da9062_devs_irq);
			irq_chip = &da9062_irq_chip;
		}

		ret = da9062_configure_irq_type(chip, i2c->irq, &trigger_type);
		if (ret < 0) {
			dev_err(chip->dev, "Failed to configure IRQ type\n");
			return ret;
		}

		ret = regmap_add_irq_chip(chip->regmap, i2c->irq,
					  trigger_type | IRQF_SHARED | IRQF_ONESHOT,
					  -1, irq_chip, &chip->regmap_irq);
		if (ret) {
			dev_err(chip->dev, "Failed to request IRQ %d: %d\n",
				i2c->irq, ret);
			return ret;
		}

		irq_base = regmap_irq_chip_get_base(chip->regmap_irq);
	}

	ret = mfd_add_devices(chip->dev, PLATFORM_DEVID_NONE, cell,
			      cell_num, NULL, irq_base,
			      NULL);
	if (ret) {
		dev_err(chip->dev, "Cannot register child devices\n");
		if (i2c->irq)
			regmap_del_irq_chip(i2c->irq, chip->regmap_irq);
		return ret;
	}

	return ret;
}

static void da9062_i2c_remove(struct i2c_client *i2c)
{
	struct da9062 *chip = i2c_get_clientdata(i2c);

	mfd_remove_devices(chip->dev);
	regmap_del_irq_chip(i2c->irq, chip->regmap_irq);
}

static const struct of_device_id da9062_dt_ids[] = {
	{ .compatible = "dlg,da9061", .data = (void *)COMPAT_TYPE_DA9061 },
	{ .compatible = "dlg,da9062", .data = (void *)COMPAT_TYPE_DA9062 },
	{ }
};
MODULE_DEVICE_TABLE(of, da9062_dt_ids);

static const struct i2c_device_id da9062_i2c_id[] = {
	{ "da9061", COMPAT_TYPE_DA9061 },
	{ "da9062", COMPAT_TYPE_DA9062 },
	{ }
};
MODULE_DEVICE_TABLE(i2c, da9062_i2c_id);

static struct i2c_driver da9062_i2c_driver = {
	.driver = {
		.name = "da9062",
		.of_match_table = da9062_dt_ids,
	},
	.probe = da9062_i2c_probe,
	.remove   = da9062_i2c_remove,
	.id_table = da9062_i2c_id,
};

module_i2c_driver(da9062_i2c_driver);

MODULE_DESCRIPTION("Core device driver for Dialog DA9061 and DA9062");
MODULE_AUTHOR("Steve Twiss <stwiss.opensource@diasemi.com>");
MODULE_LICENSE("GPL");