regulator-quirk-rcar-gen2.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * R-Car Generation 2 da9063/da9210 regulator quirk
  3. *
  4. * The r8a7790/lager and r8a7791/koelsch development boards have da9063 and
  5. * da9210 regulators. Both regulators have their interrupt request lines tied
  6. * 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. #include <linux/device.h>
  30. #include <linux/i2c.h>
  31. #include <linux/init.h>
  32. #include <linux/io.h>
  33. #include <linux/notifier.h>
  34. #include <linux/of.h>
  35. #include <linux/mfd/da9063/registers.h>
  36. #define IRQC_BASE 0xe61c0000
  37. #define IRQC_MONITOR 0x104 /* IRQn Signal Level Monitor Register */
  38. #define REGULATOR_IRQ_MASK BIT(2) /* IRQ2, active low */
  39. static void __iomem *irqc;
  40. static const u8 da9063_mask_regs[] = {
  41. DA9063_REG_IRQ_MASK_A,
  42. DA9063_REG_IRQ_MASK_B,
  43. DA9063_REG_IRQ_MASK_C,
  44. DA9063_REG_IRQ_MASK_D,
  45. };
  46. /* DA9210 System Control and Event Registers */
  47. #define DA9210_REG_MASK_A 0x54
  48. #define DA9210_REG_MASK_B 0x55
  49. static const u8 da9210_mask_regs[] = {
  50. DA9210_REG_MASK_A,
  51. DA9210_REG_MASK_B,
  52. };
  53. static void da9xxx_mask_irqs(struct i2c_client *client, const u8 regs[],
  54. unsigned int nregs)
  55. {
  56. unsigned int i;
  57. dev_info(&client->dev, "Masking %s interrupt sources\n", client->name);
  58. for (i = 0; i < nregs; i++) {
  59. int error = i2c_smbus_write_byte_data(client, regs[i], ~0);
  60. if (error) {
  61. dev_err(&client->dev, "i2c error %d\n", error);
  62. return;
  63. }
  64. }
  65. }
  66. static int regulator_quirk_notify(struct notifier_block *nb,
  67. unsigned long action, void *data)
  68. {
  69. struct device *dev = data;
  70. struct i2c_client *client;
  71. u32 mon;
  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. if (action != BUS_NOTIFY_ADD_DEVICE || dev->type == &i2c_adapter_type)
  77. return 0;
  78. client = to_i2c_client(dev);
  79. dev_dbg(dev, "Detected %s\n", client->name);
  80. if ((client->addr == 0x58 && !strcmp(client->name, "da9063")))
  81. da9xxx_mask_irqs(client, da9063_mask_regs,
  82. ARRAY_SIZE(da9063_mask_regs));
  83. else if (client->addr == 0x68 && !strcmp(client->name, "da9210"))
  84. da9xxx_mask_irqs(client, da9210_mask_regs,
  85. ARRAY_SIZE(da9210_mask_regs));
  86. mon = ioread32(irqc + IRQC_MONITOR);
  87. if (mon & REGULATOR_IRQ_MASK)
  88. goto remove;
  89. return 0;
  90. remove:
  91. dev_info(dev, "IRQ2 is not asserted, removing quirk\n");
  92. bus_unregister_notifier(&i2c_bus_type, nb);
  93. iounmap(irqc);
  94. return 0;
  95. }
  96. static struct notifier_block regulator_quirk_nb = {
  97. .notifier_call = regulator_quirk_notify
  98. };
  99. static int __init rcar_gen2_regulator_quirk(void)
  100. {
  101. u32 mon;
  102. if (!of_machine_is_compatible("renesas,koelsch") &&
  103. !of_machine_is_compatible("renesas,lager") &&
  104. !of_machine_is_compatible("renesas,gose"))
  105. return -ENODEV;
  106. irqc = ioremap(IRQC_BASE, PAGE_SIZE);
  107. if (!irqc)
  108. return -ENOMEM;
  109. mon = ioread32(irqc + IRQC_MONITOR);
  110. if (mon & REGULATOR_IRQ_MASK) {
  111. pr_debug("%s: IRQ2 is not asserted, not installing quirk\n",
  112. __func__);
  113. iounmap(irqc);
  114. return 0;
  115. }
  116. pr_info("IRQ2 is asserted, installing da9063/da9210 regulator quirk\n");
  117. bus_register_notifier(&i2c_bus_type, &regulator_quirk_nb);
  118. return 0;
  119. }
  120. arch_initcall(rcar_gen2_regulator_quirk);