Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * R-Car Generation 2 da9063(L)/da9210 regulator quirk
0004  *
0005  * Certain Gen2 development boards have an da9063 and one or more da9210
0006  * regulators. All of these regulators have their interrupt request lines
0007  * tied to the same interrupt pin (IRQ2) on the SoC.
0008  *
0009  * After cold boot or da9063-induced restart, both the da9063 and da9210 seem
0010  * to assert their interrupt request lines.  Hence as soon as one driver
0011  * requests this irq, it gets stuck in an interrupt storm, as it only manages
0012  * to deassert its own interrupt request line, and the other driver hasn't
0013  * installed an interrupt handler yet.
0014  *
0015  * To handle this, install a quirk that masks the interrupts in both the
0016  * da9063 and da9210.  This quirk has to run after the i2c master driver has
0017  * been initialized, but before the i2c slave drivers are initialized.
0018  *
0019  * Copyright (C) 2015 Glider bvba
0020  */
0021 
0022 #include <linux/device.h>
0023 #include <linux/i2c.h>
0024 #include <linux/init.h>
0025 #include <linux/io.h>
0026 #include <linux/list.h>
0027 #include <linux/notifier.h>
0028 #include <linux/of.h>
0029 #include <linux/of_irq.h>
0030 #include <linux/mfd/da9063/registers.h>
0031 
0032 #define IRQC_BASE       0xe61c0000
0033 #define IRQC_MONITOR        0x104   /* IRQn Signal Level Monitor Register */
0034 
0035 #define REGULATOR_IRQ_MASK  BIT(2)  /* IRQ2, active low */
0036 
0037 /* start of DA9210 System Control and Event Registers */
0038 #define DA9210_REG_MASK_A       0x54
0039 
0040 struct regulator_quirk {
0041     struct list_head        list;
0042     const struct of_device_id   *id;
0043     struct device_node      *np;
0044     struct of_phandle_args      irq_args;
0045     struct i2c_msg          i2c_msg;
0046     bool                shared; /* IRQ line is shared */
0047 };
0048 
0049 static LIST_HEAD(quirk_list);
0050 static void __iomem *irqc;
0051 
0052 /* first byte sets the memory pointer, following are consecutive reg values */
0053 static u8 da9063_irq_clr[] = { DA9063_REG_IRQ_MASK_A, 0xff, 0xff, 0xff, 0xff };
0054 static u8 da9210_irq_clr[] = { DA9210_REG_MASK_A, 0xff, 0xff };
0055 
0056 static struct i2c_msg da9063_msg = {
0057     .len = ARRAY_SIZE(da9063_irq_clr),
0058     .buf = da9063_irq_clr,
0059 };
0060 
0061 static struct i2c_msg da9210_msg = {
0062     .len = ARRAY_SIZE(da9210_irq_clr),
0063     .buf = da9210_irq_clr,
0064 };
0065 
0066 static const struct of_device_id rcar_gen2_quirk_match[] = {
0067     { .compatible = "dlg,da9063", .data = &da9063_msg },
0068     { .compatible = "dlg,da9063l", .data = &da9063_msg },
0069     { .compatible = "dlg,da9210", .data = &da9210_msg },
0070     { /* sentinel */ }
0071 };
0072 
0073 static int regulator_quirk_notify(struct notifier_block *nb,
0074                   unsigned long action, void *data)
0075 {
0076     struct regulator_quirk *pos, *tmp;
0077     struct device *dev = data;
0078     struct i2c_client *client;
0079     static bool done;
0080     int ret;
0081     u32 mon;
0082 
0083     if (done)
0084         return 0;
0085 
0086     mon = ioread32(irqc + IRQC_MONITOR);
0087     dev_dbg(dev, "%s: %ld, IRQC_MONITOR = 0x%x\n", __func__, action, mon);
0088     if (mon & REGULATOR_IRQ_MASK)
0089         goto remove;
0090 
0091     if (action != BUS_NOTIFY_ADD_DEVICE || dev->type == &i2c_adapter_type)
0092         return 0;
0093 
0094     client = to_i2c_client(dev);
0095     dev_dbg(dev, "Detected %s\n", client->name);
0096 
0097     /*
0098      * Send message to all PMICs that share an IRQ line to deassert it.
0099      *
0100      * WARNING: This works only if all the PMICs are on the same I2C bus.
0101      */
0102     list_for_each_entry(pos, &quirk_list, list) {
0103         if (!pos->shared)
0104             continue;
0105 
0106         if (pos->np->parent != client->dev.parent->of_node)
0107             continue;
0108 
0109         dev_info(&client->dev, "clearing %s@0x%02x interrupts\n",
0110              pos->id->compatible, pos->i2c_msg.addr);
0111 
0112         ret = i2c_transfer(client->adapter, &pos->i2c_msg, 1);
0113         if (ret != 1)
0114             dev_err(&client->dev, "i2c error %d\n", ret);
0115     }
0116 
0117     mon = ioread32(irqc + IRQC_MONITOR);
0118     if (mon & REGULATOR_IRQ_MASK)
0119         goto remove;
0120 
0121     return 0;
0122 
0123 remove:
0124     dev_info(dev, "IRQ2 is not asserted, removing quirk\n");
0125 
0126     list_for_each_entry_safe(pos, tmp, &quirk_list, list) {
0127         list_del(&pos->list);
0128         of_node_put(pos->np);
0129         kfree(pos);
0130     }
0131 
0132     done = true;
0133     iounmap(irqc);
0134     return 0;
0135 }
0136 
0137 static struct notifier_block regulator_quirk_nb = {
0138     .notifier_call = regulator_quirk_notify
0139 };
0140 
0141 static int __init rcar_gen2_regulator_quirk(void)
0142 {
0143     struct regulator_quirk *quirk, *pos, *tmp;
0144     struct of_phandle_args *argsa, *argsb;
0145     const struct of_device_id *id;
0146     struct device_node *np;
0147     u32 mon, addr;
0148     int ret;
0149 
0150     if (!of_machine_is_compatible("renesas,koelsch") &&
0151         !of_machine_is_compatible("renesas,lager") &&
0152         !of_machine_is_compatible("renesas,porter") &&
0153         !of_machine_is_compatible("renesas,stout") &&
0154         !of_machine_is_compatible("renesas,gose"))
0155         return -ENODEV;
0156 
0157     for_each_matching_node_and_match(np, rcar_gen2_quirk_match, &id) {
0158         if (!of_device_is_available(np)) {
0159             of_node_put(np);
0160             break;
0161         }
0162 
0163         ret = of_property_read_u32(np, "reg", &addr);
0164         if (ret)    /* Skip invalid entry and continue */
0165             continue;
0166 
0167         quirk = kzalloc(sizeof(*quirk), GFP_KERNEL);
0168         if (!quirk) {
0169             ret = -ENOMEM;
0170             of_node_put(np);
0171             goto err_mem;
0172         }
0173 
0174         argsa = &quirk->irq_args;
0175         memcpy(&quirk->i2c_msg, id->data, sizeof(quirk->i2c_msg));
0176 
0177         quirk->id = id;
0178         quirk->np = of_node_get(np);
0179         quirk->i2c_msg.addr = addr;
0180 
0181         ret = of_irq_parse_one(np, 0, argsa);
0182         if (ret) {  /* Skip invalid entry and continue */
0183             of_node_put(np);
0184             kfree(quirk);
0185             continue;
0186         }
0187 
0188         list_for_each_entry(pos, &quirk_list, list) {
0189             argsb = &pos->irq_args;
0190 
0191             if (argsa->args_count != argsb->args_count)
0192                 continue;
0193 
0194             ret = memcmp(argsa->args, argsb->args,
0195                      argsa->args_count *
0196                      sizeof(argsa->args[0]));
0197             if (!ret) {
0198                 pos->shared = true;
0199                 quirk->shared = true;
0200             }
0201         }
0202 
0203         list_add_tail(&quirk->list, &quirk_list);
0204     }
0205 
0206     irqc = ioremap(IRQC_BASE, PAGE_SIZE);
0207     if (!irqc) {
0208         ret = -ENOMEM;
0209         goto err_mem;
0210     }
0211 
0212     mon = ioread32(irqc + IRQC_MONITOR);
0213     if (mon & REGULATOR_IRQ_MASK) {
0214         pr_debug("%s: IRQ2 is not asserted, not installing quirk\n",
0215              __func__);
0216         ret = 0;
0217         goto err_free;
0218     }
0219 
0220     pr_info("IRQ2 is asserted, installing regulator quirk\n");
0221 
0222     bus_register_notifier(&i2c_bus_type, &regulator_quirk_nb);
0223     return 0;
0224 
0225 err_free:
0226     iounmap(irqc);
0227 err_mem:
0228     list_for_each_entry_safe(pos, tmp, &quirk_list, list) {
0229         list_del(&pos->list);
0230         of_node_put(pos->np);
0231         kfree(pos);
0232     }
0233 
0234     return ret;
0235 }
0236 
0237 arch_initcall(rcar_gen2_regulator_quirk);