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