.. | .. |
---|
1 | 1 | // SPDX-License-Identifier: GPL-2.0 |
---|
2 | 2 | /* |
---|
3 | | - * R-Car Generation 2 da9063/da9210 regulator quirk |
---|
| 3 | + * R-Car Generation 2 da9063(L)/da9210 regulator quirk |
---|
4 | 4 | * |
---|
5 | 5 | * Certain Gen2 development boards have an da9063 and one or more da9210 |
---|
6 | 6 | * regulators. All of these regulators have their interrupt request lines |
---|
.. | .. |
---|
23 | 23 | #include <linux/i2c.h> |
---|
24 | 24 | #include <linux/init.h> |
---|
25 | 25 | #include <linux/io.h> |
---|
| 26 | +#include <linux/list.h> |
---|
26 | 27 | #include <linux/notifier.h> |
---|
27 | 28 | #include <linux/of.h> |
---|
| 29 | +#include <linux/of_irq.h> |
---|
28 | 30 | #include <linux/mfd/da9063/registers.h> |
---|
29 | | - |
---|
30 | 31 | |
---|
31 | 32 | #define IRQC_BASE 0xe61c0000 |
---|
32 | 33 | #define IRQC_MONITOR 0x104 /* IRQn Signal Level Monitor Register */ |
---|
.. | .. |
---|
36 | 37 | /* start of DA9210 System Control and Event Registers */ |
---|
37 | 38 | #define DA9210_REG_MASK_A 0x54 |
---|
38 | 39 | |
---|
| 40 | +struct regulator_quirk { |
---|
| 41 | + struct list_head list; |
---|
| 42 | + const struct of_device_id *id; |
---|
| 43 | + struct device_node *np; |
---|
| 44 | + struct of_phandle_args irq_args; |
---|
| 45 | + struct i2c_msg i2c_msg; |
---|
| 46 | + bool shared; /* IRQ line is shared */ |
---|
| 47 | +}; |
---|
| 48 | + |
---|
| 49 | +static LIST_HEAD(quirk_list); |
---|
39 | 50 | static void __iomem *irqc; |
---|
40 | 51 | |
---|
41 | 52 | /* first byte sets the memory pointer, following are consecutive reg values */ |
---|
42 | 53 | static u8 da9063_irq_clr[] = { DA9063_REG_IRQ_MASK_A, 0xff, 0xff, 0xff, 0xff }; |
---|
43 | 54 | static u8 da9210_irq_clr[] = { DA9210_REG_MASK_A, 0xff, 0xff }; |
---|
44 | 55 | |
---|
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 | | - }, |
---|
| 56 | +static struct i2c_msg da9063_msg = { |
---|
| 57 | + .len = ARRAY_SIZE(da9063_irq_clr), |
---|
| 58 | + .buf = da9063_irq_clr, |
---|
| 59 | +}; |
---|
| 60 | + |
---|
| 61 | +static struct i2c_msg da9210_msg = { |
---|
| 62 | + .len = ARRAY_SIZE(da9210_irq_clr), |
---|
| 63 | + .buf = da9210_irq_clr, |
---|
| 64 | +}; |
---|
| 65 | + |
---|
| 66 | +static const struct of_device_id rcar_gen2_quirk_match[] = { |
---|
| 67 | + { .compatible = "dlg,da9063", .data = &da9063_msg }, |
---|
| 68 | + { .compatible = "dlg,da9063l", .data = &da9063_msg }, |
---|
| 69 | + { .compatible = "dlg,da9210", .data = &da9210_msg }, |
---|
| 70 | + {}, |
---|
59 | 71 | }; |
---|
60 | 72 | |
---|
61 | 73 | static int regulator_quirk_notify(struct notifier_block *nb, |
---|
62 | 74 | unsigned long action, void *data) |
---|
63 | 75 | { |
---|
| 76 | + struct regulator_quirk *pos, *tmp; |
---|
64 | 77 | struct device *dev = data; |
---|
65 | 78 | struct i2c_client *client; |
---|
66 | 79 | static bool done; |
---|
| 80 | + int ret; |
---|
67 | 81 | u32 mon; |
---|
68 | 82 | |
---|
69 | 83 | if (done) |
---|
.. | .. |
---|
80 | 94 | client = to_i2c_client(dev); |
---|
81 | 95 | dev_dbg(dev, "Detected %s\n", client->name); |
---|
82 | 96 | |
---|
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; |
---|
| 97 | + /* |
---|
| 98 | + * Send message to all PMICs that share an IRQ line to deassert it. |
---|
| 99 | + * |
---|
| 100 | + * WARNING: This works only if all the PMICs are on the same I2C bus. |
---|
| 101 | + */ |
---|
| 102 | + list_for_each_entry(pos, &quirk_list, list) { |
---|
| 103 | + if (!pos->shared) |
---|
| 104 | + continue; |
---|
87 | 105 | |
---|
88 | | - /* There are two DA9210 on Stout, one on the other boards. */ |
---|
89 | | - len = of_machine_is_compatible("renesas,stout") ? 3 : 2; |
---|
| 106 | + if (pos->np->parent != client->dev.parent->of_node) |
---|
| 107 | + continue; |
---|
90 | 108 | |
---|
91 | | - dev_info(&client->dev, "clearing da9063/da9210 interrupts\n"); |
---|
92 | | - ret = i2c_transfer(client->adapter, da9xxx_msgs, len); |
---|
93 | | - if (ret != len) |
---|
| 109 | + dev_info(&client->dev, "clearing %s@0x%02x interrupts\n", |
---|
| 110 | + pos->id->compatible, pos->i2c_msg.addr); |
---|
| 111 | + |
---|
| 112 | + ret = i2c_transfer(client->adapter, &pos->i2c_msg, 1); |
---|
| 113 | + if (ret != 1) |
---|
94 | 114 | dev_err(&client->dev, "i2c error %d\n", ret); |
---|
95 | 115 | } |
---|
96 | 116 | |
---|
.. | .. |
---|
103 | 123 | remove: |
---|
104 | 124 | dev_info(dev, "IRQ2 is not asserted, removing quirk\n"); |
---|
105 | 125 | |
---|
| 126 | + list_for_each_entry_safe(pos, tmp, &quirk_list, list) { |
---|
| 127 | + list_del(&pos->list); |
---|
| 128 | + of_node_put(pos->np); |
---|
| 129 | + kfree(pos); |
---|
| 130 | + } |
---|
| 131 | + |
---|
106 | 132 | done = true; |
---|
107 | 133 | iounmap(irqc); |
---|
108 | 134 | return 0; |
---|
.. | .. |
---|
114 | 140 | |
---|
115 | 141 | static int __init rcar_gen2_regulator_quirk(void) |
---|
116 | 142 | { |
---|
117 | | - u32 mon; |
---|
| 143 | + struct regulator_quirk *quirk, *pos, *tmp; |
---|
| 144 | + struct of_phandle_args *argsa, *argsb; |
---|
| 145 | + const struct of_device_id *id; |
---|
| 146 | + struct device_node *np; |
---|
| 147 | + u32 mon, addr; |
---|
| 148 | + int ret; |
---|
118 | 149 | |
---|
119 | 150 | if (!of_machine_is_compatible("renesas,koelsch") && |
---|
120 | 151 | !of_machine_is_compatible("renesas,lager") && |
---|
| 152 | + !of_machine_is_compatible("renesas,porter") && |
---|
121 | 153 | !of_machine_is_compatible("renesas,stout") && |
---|
122 | 154 | !of_machine_is_compatible("renesas,gose")) |
---|
123 | 155 | return -ENODEV; |
---|
124 | 156 | |
---|
| 157 | + for_each_matching_node_and_match(np, rcar_gen2_quirk_match, &id) { |
---|
| 158 | + if (!of_device_is_available(np)) { |
---|
| 159 | + of_node_put(np); |
---|
| 160 | + break; |
---|
| 161 | + } |
---|
| 162 | + |
---|
| 163 | + ret = of_property_read_u32(np, "reg", &addr); |
---|
| 164 | + if (ret) /* Skip invalid entry and continue */ |
---|
| 165 | + continue; |
---|
| 166 | + |
---|
| 167 | + quirk = kzalloc(sizeof(*quirk), GFP_KERNEL); |
---|
| 168 | + if (!quirk) { |
---|
| 169 | + ret = -ENOMEM; |
---|
| 170 | + of_node_put(np); |
---|
| 171 | + goto err_mem; |
---|
| 172 | + } |
---|
| 173 | + |
---|
| 174 | + argsa = &quirk->irq_args; |
---|
| 175 | + memcpy(&quirk->i2c_msg, id->data, sizeof(quirk->i2c_msg)); |
---|
| 176 | + |
---|
| 177 | + quirk->id = id; |
---|
| 178 | + quirk->np = of_node_get(np); |
---|
| 179 | + quirk->i2c_msg.addr = addr; |
---|
| 180 | + |
---|
| 181 | + ret = of_irq_parse_one(np, 0, argsa); |
---|
| 182 | + if (ret) { /* Skip invalid entry and continue */ |
---|
| 183 | + of_node_put(np); |
---|
| 184 | + kfree(quirk); |
---|
| 185 | + continue; |
---|
| 186 | + } |
---|
| 187 | + |
---|
| 188 | + list_for_each_entry(pos, &quirk_list, list) { |
---|
| 189 | + argsb = &pos->irq_args; |
---|
| 190 | + |
---|
| 191 | + if (argsa->args_count != argsb->args_count) |
---|
| 192 | + continue; |
---|
| 193 | + |
---|
| 194 | + ret = memcmp(argsa->args, argsb->args, |
---|
| 195 | + argsa->args_count * |
---|
| 196 | + sizeof(argsa->args[0])); |
---|
| 197 | + if (!ret) { |
---|
| 198 | + pos->shared = true; |
---|
| 199 | + quirk->shared = true; |
---|
| 200 | + } |
---|
| 201 | + } |
---|
| 202 | + |
---|
| 203 | + list_add_tail(&quirk->list, &quirk_list); |
---|
| 204 | + } |
---|
| 205 | + |
---|
125 | 206 | irqc = ioremap(IRQC_BASE, PAGE_SIZE); |
---|
126 | | - if (!irqc) |
---|
127 | | - return -ENOMEM; |
---|
| 207 | + if (!irqc) { |
---|
| 208 | + ret = -ENOMEM; |
---|
| 209 | + goto err_mem; |
---|
| 210 | + } |
---|
128 | 211 | |
---|
129 | 212 | mon = ioread32(irqc + IRQC_MONITOR); |
---|
130 | 213 | if (mon & REGULATOR_IRQ_MASK) { |
---|
131 | 214 | pr_debug("%s: IRQ2 is not asserted, not installing quirk\n", |
---|
132 | 215 | __func__); |
---|
133 | | - iounmap(irqc); |
---|
134 | | - return 0; |
---|
| 216 | + ret = 0; |
---|
| 217 | + goto err_free; |
---|
135 | 218 | } |
---|
136 | 219 | |
---|
137 | | - pr_info("IRQ2 is asserted, installing da9063/da9210 regulator quirk\n"); |
---|
| 220 | + pr_info("IRQ2 is asserted, installing regulator quirk\n"); |
---|
138 | 221 | |
---|
139 | 222 | bus_register_notifier(&i2c_bus_type, ®ulator_quirk_nb); |
---|
140 | 223 | return 0; |
---|
| 224 | + |
---|
| 225 | +err_free: |
---|
| 226 | + iounmap(irqc); |
---|
| 227 | +err_mem: |
---|
| 228 | + list_for_each_entry_safe(pos, tmp, &quirk_list, list) { |
---|
| 229 | + list_del(&pos->list); |
---|
| 230 | + of_node_put(pos->np); |
---|
| 231 | + kfree(pos); |
---|
| 232 | + } |
---|
| 233 | + |
---|
| 234 | + return ret; |
---|
141 | 235 | } |
---|
142 | 236 | |
---|
143 | 237 | arch_initcall(rcar_gen2_regulator_quirk); |
---|