1 /*
2  * R-Car Generation 2 da9063/da9210 regulator quirk
3  *
4  * Certain Gen2 development boards have an da9063 and one or more da9210
5  * regulators. All of these regulators have their interrupt request lines
6  * tied to the same interrupt pin (IRQ2) on the SoC.
7  *
8  * After cold boot or da9063-induced restart, both the da9063 and da9210 seem
9  * to assert their interrupt request lines.  Hence as soon as one driver
10  * requests this irq, it gets stuck in an interrupt storm, as it only manages
11  * to deassert its own interrupt request line, and the other driver hasn't
12  * installed an interrupt handler yet.
13  *
14  * To handle this, install a quirk that masks the interrupts in both the
15  * da9063 and da9210.  This quirk has to run after the i2c master driver has
16  * been initialized, but before the i2c slave drivers are initialized.
17  *
18  * Copyright (C) 2015 Glider bvba
19  *
20  * This program is free software; you can redistribute it and/or modify
21  * it under the terms of the GNU General Public License as published by
22  * the Free Software Foundation; version 2 of the License.
23  *
24  * This program is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27  * GNU General Public License for more details.
28  */
29 
30 #include <linux/device.h>
31 #include <linux/i2c.h>
32 #include <linux/init.h>
33 #include <linux/io.h>
34 #include <linux/notifier.h>
35 #include <linux/of.h>
36 #include <linux/mfd/da9063/registers.h>
37 
38 
39 #define IRQC_BASE		0xe61c0000
40 #define IRQC_MONITOR		0x104	/* IRQn Signal Level Monitor Register */
41 
42 #define REGULATOR_IRQ_MASK	BIT(2)	/* IRQ2, active low */
43 
44 /* start of DA9210 System Control and Event Registers */
45 #define DA9210_REG_MASK_A		0x54
46 
47 static void __iomem *irqc;
48 
49 /* first byte sets the memory pointer, following are consecutive reg values */
50 static u8 da9063_irq_clr[] = { DA9063_REG_IRQ_MASK_A, 0xff, 0xff, 0xff, 0xff };
51 static u8 da9210_irq_clr[] = { DA9210_REG_MASK_A, 0xff, 0xff };
52 
53 static struct i2c_msg da9xxx_msgs[3] = {
54 	{
55 		.addr = 0x58,
56 		.len = ARRAY_SIZE(da9063_irq_clr),
57 		.buf = da9063_irq_clr,
58 	}, {
59 		.addr = 0x68,
60 		.len = ARRAY_SIZE(da9210_irq_clr),
61 		.buf = da9210_irq_clr,
62 	}, {
63 		.addr = 0x70,
64 		.len = ARRAY_SIZE(da9210_irq_clr),
65 		.buf = da9210_irq_clr,
66 	},
67 };
68 
69 static int regulator_quirk_notify(struct notifier_block *nb,
70 				  unsigned long action, void *data)
71 {
72 	struct device *dev = data;
73 	struct i2c_client *client;
74 	static bool done;
75 	u32 mon;
76 
77 	if (done)
78 		return 0;
79 
80 	mon = ioread32(irqc + IRQC_MONITOR);
81 	dev_dbg(dev, "%s: %ld, IRQC_MONITOR = 0x%x\n", __func__, action, mon);
82 	if (mon & REGULATOR_IRQ_MASK)
83 		goto remove;
84 
85 	if (action != BUS_NOTIFY_ADD_DEVICE || dev->type == &i2c_adapter_type)
86 		return 0;
87 
88 	client = to_i2c_client(dev);
89 	dev_dbg(dev, "Detected %s\n", client->name);
90 
91 	if ((client->addr == 0x58 && !strcmp(client->name, "da9063")) ||
92 	    (client->addr == 0x68 && !strcmp(client->name, "da9210")) ||
93 	    (client->addr == 0x70 && !strcmp(client->name, "da9210"))) {
94 		int ret, len;
95 
96 		/* There are two DA9210 on Stout, one on the other boards. */
97 		len = of_machine_is_compatible("renesas,stout") ? 3 : 2;
98 
99 		dev_info(&client->dev, "clearing da9063/da9210 interrupts\n");
100 		ret = i2c_transfer(client->adapter, da9xxx_msgs, len);
101 		if (ret != len)
102 			dev_err(&client->dev, "i2c error %d\n", ret);
103 	}
104 
105 	mon = ioread32(irqc + IRQC_MONITOR);
106 	if (mon & REGULATOR_IRQ_MASK)
107 		goto remove;
108 
109 	return 0;
110 
111 remove:
112 	dev_info(dev, "IRQ2 is not asserted, removing quirk\n");
113 
114 	done = true;
115 	iounmap(irqc);
116 	return 0;
117 }
118 
119 static struct notifier_block regulator_quirk_nb = {
120 	.notifier_call = regulator_quirk_notify
121 };
122 
123 static int __init rcar_gen2_regulator_quirk(void)
124 {
125 	u32 mon;
126 
127 	if (!of_machine_is_compatible("renesas,koelsch") &&
128 	    !of_machine_is_compatible("renesas,lager") &&
129 	    !of_machine_is_compatible("renesas,stout") &&
130 	    !of_machine_is_compatible("renesas,gose"))
131 		return -ENODEV;
132 
133 	irqc = ioremap(IRQC_BASE, PAGE_SIZE);
134 	if (!irqc)
135 		return -ENOMEM;
136 
137 	mon = ioread32(irqc + IRQC_MONITOR);
138 	if (mon & REGULATOR_IRQ_MASK) {
139 		pr_debug("%s: IRQ2 is not asserted, not installing quirk\n",
140 			 __func__);
141 		iounmap(irqc);
142 		return 0;
143 	}
144 
145 	pr_info("IRQ2 is asserted, installing da9063/da9210 regulator quirk\n");
146 
147 	bus_register_notifier(&i2c_bus_type, &regulator_quirk_nb);
148 	return 0;
149 }
150 
151 arch_initcall(rcar_gen2_regulator_quirk);
152