Wolfram Sang | c44e182 | 2018-06-14 10:56:06 +0900 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Geert Uytterhoeven | 663fbb5 | 2015-03-09 20:50:40 +0100 | [diff] [blame] | 2 | /* |
Marek Vasut | d5aa840 | 2019-03-03 20:41:40 +0100 | [diff] [blame] | 3 | * R-Car Generation 2 da9063(L)/da9210 regulator quirk |
Geert Uytterhoeven | 663fbb5 | 2015-03-09 20:50:40 +0100 | [diff] [blame] | 4 | * |
Marek Vasut | ff938cd | 2018-02-15 12:33:50 +0100 | [diff] [blame] | 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. |
Geert Uytterhoeven | 663fbb5 | 2015-03-09 20:50:40 +0100 | [diff] [blame] | 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 |
Geert Uytterhoeven | 663fbb5 | 2015-03-09 20:50:40 +0100 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #include <linux/device.h> |
| 23 | #include <linux/i2c.h> |
| 24 | #include <linux/init.h> |
| 25 | #include <linux/io.h> |
Marek Vasut | 6d14d4d | 2018-09-18 14:23:40 +0200 | [diff] [blame] | 26 | #include <linux/list.h> |
Geert Uytterhoeven | 663fbb5 | 2015-03-09 20:50:40 +0100 | [diff] [blame] | 27 | #include <linux/notifier.h> |
| 28 | #include <linux/of.h> |
Marek Vasut | 6d14d4d | 2018-09-18 14:23:40 +0200 | [diff] [blame] | 29 | #include <linux/of_irq.h> |
Geert Uytterhoeven | 663fbb5 | 2015-03-09 20:50:40 +0100 | [diff] [blame] | 30 | #include <linux/mfd/da9063/registers.h> |
| 31 | |
Geert Uytterhoeven | 663fbb5 | 2015-03-09 20:50:40 +0100 | [diff] [blame] | 32 | #define IRQC_BASE 0xe61c0000 |
| 33 | #define IRQC_MONITOR 0x104 /* IRQn Signal Level Monitor Register */ |
| 34 | |
| 35 | #define REGULATOR_IRQ_MASK BIT(2) /* IRQ2, active low */ |
| 36 | |
Wolfram Sang | c2f3211 | 2016-08-30 21:50:22 +0200 | [diff] [blame] | 37 | /* start of DA9210 System Control and Event Registers */ |
| 38 | #define DA9210_REG_MASK_A 0x54 |
| 39 | |
Marek Vasut | 6d14d4d | 2018-09-18 14:23:40 +0200 | [diff] [blame] | 40 | struct regulator_quirk { |
| 41 | struct list_head list; |
| 42 | const struct of_device_id *id; |
Marek Vasut | 5347a02 | 2018-12-07 21:28:58 +0100 | [diff] [blame] | 43 | struct device_node *np; |
Marek Vasut | 6d14d4d | 2018-09-18 14:23:40 +0200 | [diff] [blame] | 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); |
Geert Uytterhoeven | 663fbb5 | 2015-03-09 20:50:40 +0100 | [diff] [blame] | 50 | static void __iomem *irqc; |
| 51 | |
Wolfram Sang | c2f3211 | 2016-08-30 21:50:22 +0200 | [diff] [blame] | 52 | /* first byte sets the memory pointer, following are consecutive reg values */ |
| 53 | static u8 da9063_irq_clr[] = { DA9063_REG_IRQ_MASK_A, 0xff, 0xff, 0xff, 0xff }; |
| 54 | static u8 da9210_irq_clr[] = { DA9210_REG_MASK_A, 0xff, 0xff }; |
| 55 | |
Marek Vasut | 6d14d4d | 2018-09-18 14:23:40 +0200 | [diff] [blame] | 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 }, |
Marek Vasut | d5aa840 | 2019-03-03 20:41:40 +0100 | [diff] [blame] | 68 | { .compatible = "dlg,da9063l", .data = &da9063_msg }, |
Marek Vasut | 6d14d4d | 2018-09-18 14:23:40 +0200 | [diff] [blame] | 69 | { .compatible = "dlg,da9210", .data = &da9210_msg }, |
| 70 | {}, |
Geert Uytterhoeven | 663fbb5 | 2015-03-09 20:50:40 +0100 | [diff] [blame] | 71 | }; |
| 72 | |
Geert Uytterhoeven | 663fbb5 | 2015-03-09 20:50:40 +0100 | [diff] [blame] | 73 | static int regulator_quirk_notify(struct notifier_block *nb, |
| 74 | unsigned long action, void *data) |
| 75 | { |
Marek Vasut | 6d14d4d | 2018-09-18 14:23:40 +0200 | [diff] [blame] | 76 | struct regulator_quirk *pos, *tmp; |
Geert Uytterhoeven | 663fbb5 | 2015-03-09 20:50:40 +0100 | [diff] [blame] | 77 | struct device *dev = data; |
| 78 | struct i2c_client *client; |
Geert Uytterhoeven | fce8dc5 | 2017-07-12 16:45:20 +0200 | [diff] [blame] | 79 | static bool done; |
Marek Vasut | 6d14d4d | 2018-09-18 14:23:40 +0200 | [diff] [blame] | 80 | int ret; |
Geert Uytterhoeven | 663fbb5 | 2015-03-09 20:50:40 +0100 | [diff] [blame] | 81 | u32 mon; |
| 82 | |
Geert Uytterhoeven | fce8dc5 | 2017-07-12 16:45:20 +0200 | [diff] [blame] | 83 | if (done) |
| 84 | return 0; |
| 85 | |
Geert Uytterhoeven | 663fbb5 | 2015-03-09 20:50:40 +0100 | [diff] [blame] | 86 | mon = ioread32(irqc + IRQC_MONITOR); |
| 87 | dev_dbg(dev, "%s: %ld, IRQC_MONITOR = 0x%x\n", __func__, action, mon); |
| 88 | if (mon & REGULATOR_IRQ_MASK) |
| 89 | goto remove; |
| 90 | |
| 91 | if (action != BUS_NOTIFY_ADD_DEVICE || dev->type == &i2c_adapter_type) |
| 92 | return 0; |
| 93 | |
| 94 | client = to_i2c_client(dev); |
| 95 | dev_dbg(dev, "Detected %s\n", client->name); |
| 96 | |
Marek Vasut | 6d14d4d | 2018-09-18 14:23:40 +0200 | [diff] [blame] | 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; |
Marek Vasut | ff938cd | 2018-02-15 12:33:50 +0100 | [diff] [blame] | 105 | |
Marek Vasut | 5347a02 | 2018-12-07 21:28:58 +0100 | [diff] [blame] | 106 | if (pos->np->parent != client->dev.parent->of_node) |
| 107 | continue; |
| 108 | |
Marek Vasut | 6d14d4d | 2018-09-18 14:23:40 +0200 | [diff] [blame] | 109 | dev_info(&client->dev, "clearing %s@0x%02x interrupts\n", |
| 110 | pos->id->compatible, pos->i2c_msg.addr); |
Wolfram Sang | c2f3211 | 2016-08-30 21:50:22 +0200 | [diff] [blame] | 111 | |
Marek Vasut | 6d14d4d | 2018-09-18 14:23:40 +0200 | [diff] [blame] | 112 | ret = i2c_transfer(client->adapter, &pos->i2c_msg, 1); |
| 113 | if (ret != 1) |
Wolfram Sang | c2f3211 | 2016-08-30 21:50:22 +0200 | [diff] [blame] | 114 | dev_err(&client->dev, "i2c error %d\n", ret); |
| 115 | } |
Geert Uytterhoeven | 663fbb5 | 2015-03-09 20:50:40 +0100 | [diff] [blame] | 116 | |
| 117 | mon = ioread32(irqc + IRQC_MONITOR); |
| 118 | if (mon & REGULATOR_IRQ_MASK) |
| 119 | goto remove; |
| 120 | |
| 121 | return 0; |
| 122 | |
| 123 | remove: |
| 124 | dev_info(dev, "IRQ2 is not asserted, removing quirk\n"); |
| 125 | |
Marek Vasut | 6d14d4d | 2018-09-18 14:23:40 +0200 | [diff] [blame] | 126 | list_for_each_entry_safe(pos, tmp, &quirk_list, list) { |
| 127 | list_del(&pos->list); |
| 128 | kfree(pos); |
| 129 | } |
| 130 | |
Geert Uytterhoeven | fce8dc5 | 2017-07-12 16:45:20 +0200 | [diff] [blame] | 131 | done = true; |
Geert Uytterhoeven | 663fbb5 | 2015-03-09 20:50:40 +0100 | [diff] [blame] | 132 | iounmap(irqc); |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | static struct notifier_block regulator_quirk_nb = { |
| 137 | .notifier_call = regulator_quirk_notify |
| 138 | }; |
| 139 | |
| 140 | static int __init rcar_gen2_regulator_quirk(void) |
| 141 | { |
Marek Vasut | 6d14d4d | 2018-09-18 14:23:40 +0200 | [diff] [blame] | 142 | struct regulator_quirk *quirk, *pos, *tmp; |
| 143 | struct of_phandle_args *argsa, *argsb; |
| 144 | const struct of_device_id *id; |
| 145 | struct device_node *np; |
| 146 | u32 mon, addr; |
| 147 | int ret; |
Geert Uytterhoeven | 663fbb5 | 2015-03-09 20:50:40 +0100 | [diff] [blame] | 148 | |
| 149 | if (!of_machine_is_compatible("renesas,koelsch") && |
Ulrich Hecht | 765b500 | 2015-06-01 16:22:57 +0200 | [diff] [blame] | 150 | !of_machine_is_compatible("renesas,lager") && |
Marek Vasut | d5aa840 | 2019-03-03 20:41:40 +0100 | [diff] [blame] | 151 | !of_machine_is_compatible("renesas,porter") && |
Marek Vasut | ff938cd | 2018-02-15 12:33:50 +0100 | [diff] [blame] | 152 | !of_machine_is_compatible("renesas,stout") && |
Ulrich Hecht | 765b500 | 2015-06-01 16:22:57 +0200 | [diff] [blame] | 153 | !of_machine_is_compatible("renesas,gose")) |
Geert Uytterhoeven | 663fbb5 | 2015-03-09 20:50:40 +0100 | [diff] [blame] | 154 | return -ENODEV; |
| 155 | |
Marek Vasut | 6d14d4d | 2018-09-18 14:23:40 +0200 | [diff] [blame] | 156 | for_each_matching_node_and_match(np, rcar_gen2_quirk_match, &id) { |
| 157 | if (!of_device_is_available(np)) |
| 158 | break; |
| 159 | |
| 160 | ret = of_property_read_u32(np, "reg", &addr); |
| 161 | if (ret) /* Skip invalid entry and continue */ |
| 162 | continue; |
| 163 | |
| 164 | quirk = kzalloc(sizeof(*quirk), GFP_KERNEL); |
| 165 | if (!quirk) { |
| 166 | ret = -ENOMEM; |
| 167 | goto err_mem; |
| 168 | } |
| 169 | |
| 170 | argsa = &quirk->irq_args; |
| 171 | memcpy(&quirk->i2c_msg, id->data, sizeof(quirk->i2c_msg)); |
| 172 | |
| 173 | quirk->id = id; |
Marek Vasut | 5347a02 | 2018-12-07 21:28:58 +0100 | [diff] [blame] | 174 | quirk->np = np; |
Marek Vasut | 6d14d4d | 2018-09-18 14:23:40 +0200 | [diff] [blame] | 175 | quirk->i2c_msg.addr = addr; |
| 176 | |
| 177 | ret = of_irq_parse_one(np, 0, argsa); |
| 178 | if (ret) { /* Skip invalid entry and continue */ |
| 179 | kfree(quirk); |
| 180 | continue; |
| 181 | } |
| 182 | |
| 183 | list_for_each_entry(pos, &quirk_list, list) { |
| 184 | argsb = &pos->irq_args; |
| 185 | |
| 186 | if (argsa->args_count != argsb->args_count) |
| 187 | continue; |
| 188 | |
| 189 | ret = memcmp(argsa->args, argsb->args, |
| 190 | argsa->args_count * |
| 191 | sizeof(argsa->args[0])); |
| 192 | if (!ret) { |
| 193 | pos->shared = true; |
| 194 | quirk->shared = true; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | list_add_tail(&quirk->list, &quirk_list); |
| 199 | } |
| 200 | |
Geert Uytterhoeven | 663fbb5 | 2015-03-09 20:50:40 +0100 | [diff] [blame] | 201 | irqc = ioremap(IRQC_BASE, PAGE_SIZE); |
Marek Vasut | 6d14d4d | 2018-09-18 14:23:40 +0200 | [diff] [blame] | 202 | if (!irqc) { |
| 203 | ret = -ENOMEM; |
| 204 | goto err_mem; |
| 205 | } |
Geert Uytterhoeven | 663fbb5 | 2015-03-09 20:50:40 +0100 | [diff] [blame] | 206 | |
| 207 | mon = ioread32(irqc + IRQC_MONITOR); |
| 208 | if (mon & REGULATOR_IRQ_MASK) { |
| 209 | pr_debug("%s: IRQ2 is not asserted, not installing quirk\n", |
| 210 | __func__); |
Marek Vasut | 6d14d4d | 2018-09-18 14:23:40 +0200 | [diff] [blame] | 211 | ret = 0; |
| 212 | goto err_free; |
Geert Uytterhoeven | 663fbb5 | 2015-03-09 20:50:40 +0100 | [diff] [blame] | 213 | } |
| 214 | |
Marek Vasut | d5aa840 | 2019-03-03 20:41:40 +0100 | [diff] [blame] | 215 | pr_info("IRQ2 is asserted, installing regulator quirk\n"); |
Geert Uytterhoeven | 663fbb5 | 2015-03-09 20:50:40 +0100 | [diff] [blame] | 216 | |
| 217 | bus_register_notifier(&i2c_bus_type, ®ulator_quirk_nb); |
| 218 | return 0; |
Marek Vasut | 6d14d4d | 2018-09-18 14:23:40 +0200 | [diff] [blame] | 219 | |
| 220 | err_free: |
| 221 | iounmap(irqc); |
| 222 | err_mem: |
| 223 | list_for_each_entry_safe(pos, tmp, &quirk_list, list) { |
| 224 | list_del(&pos->list); |
| 225 | kfree(pos); |
| 226 | } |
| 227 | |
| 228 | return ret; |
Geert Uytterhoeven | 663fbb5 | 2015-03-09 20:50:40 +0100 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | arch_initcall(rcar_gen2_regulator_quirk); |