regulator-quirk-rcar-gen2.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. #include <linux/device.h>
  22. #include <linux/i2c.h>
  23. #include <linux/init.h>
  24. #include <linux/io.h>
  25. #include <linux/notifier.h>
  26. #include <linux/of.h>
  27. #include <linux/mfd/da9063/registers.h>
  28. #define IRQC_BASE 0xe61c0000
  29. #define IRQC_MONITOR 0x104 /* IRQn Signal Level Monitor Register */
  30. #define REGULATOR_IRQ_MASK BIT(2) /* IRQ2, active low */
  31. /* start of DA9210 System Control and Event Registers */
  32. #define DA9210_REG_MASK_A 0x54
  33. static void __iomem *irqc;
  34. /* first byte sets the memory pointer, following are consecutive reg values */
  35. static u8 da9063_irq_clr[] = { DA9063_REG_IRQ_MASK_A, 0xff, 0xff, 0xff, 0xff };
  36. static u8 da9210_irq_clr[] = { DA9210_REG_MASK_A, 0xff, 0xff };
  37. static struct i2c_msg da9xxx_msgs[3] = {
  38. {
  39. .addr = 0x58,
  40. .len = ARRAY_SIZE(da9063_irq_clr),
  41. .buf = da9063_irq_clr,
  42. }, {
  43. .addr = 0x68,
  44. .len = ARRAY_SIZE(da9210_irq_clr),
  45. .buf = da9210_irq_clr,
  46. }, {
  47. .addr = 0x70,
  48. .len = ARRAY_SIZE(da9210_irq_clr),
  49. .buf = da9210_irq_clr,
  50. },
  51. };
  52. static int regulator_quirk_notify(struct notifier_block *nb,
  53. unsigned long action, void *data)
  54. {
  55. struct device *dev = data;
  56. struct i2c_client *client;
  57. static bool done;
  58. u32 mon;
  59. if (done)
  60. return 0;
  61. mon = ioread32(irqc + IRQC_MONITOR);
  62. dev_dbg(dev, "%s: %ld, IRQC_MONITOR = 0x%x\n", __func__, action, mon);
  63. if (mon & REGULATOR_IRQ_MASK)
  64. goto remove;
  65. if (action != BUS_NOTIFY_ADD_DEVICE || dev->type == &i2c_adapter_type)
  66. return 0;
  67. client = to_i2c_client(dev);
  68. dev_dbg(dev, "Detected %s\n", client->name);
  69. if ((client->addr == 0x58 && !strcmp(client->name, "da9063")) ||
  70. (client->addr == 0x68 && !strcmp(client->name, "da9210")) ||
  71. (client->addr == 0x70 && !strcmp(client->name, "da9210"))) {
  72. int ret, len;
  73. /* There are two DA9210 on Stout, one on the other boards. */
  74. len = of_machine_is_compatible("renesas,stout") ? 3 : 2;
  75. dev_info(&client->dev, "clearing da9063/da9210 interrupts\n");
  76. ret = i2c_transfer(client->adapter, da9xxx_msgs, len);
  77. if (ret != len)
  78. dev_err(&client->dev, "i2c error %d\n", ret);
  79. }
  80. mon = ioread32(irqc + IRQC_MONITOR);
  81. if (mon & REGULATOR_IRQ_MASK)
  82. goto remove;
  83. return 0;
  84. remove:
  85. dev_info(dev, "IRQ2 is not asserted, removing quirk\n");
  86. done = true;
  87. iounmap(irqc);
  88. return 0;
  89. }
  90. static struct notifier_block regulator_quirk_nb = {
  91. .notifier_call = regulator_quirk_notify
  92. };
  93. static int __init rcar_gen2_regulator_quirk(void)
  94. {
  95. u32 mon;
  96. if (!of_machine_is_compatible("renesas,koelsch") &&
  97. !of_machine_is_compatible("renesas,lager") &&
  98. !of_machine_is_compatible("renesas,stout") &&
  99. !of_machine_is_compatible("renesas,gose"))
  100. return -ENODEV;
  101. irqc = ioremap(IRQC_BASE, PAGE_SIZE);
  102. if (!irqc)
  103. return -ENOMEM;
  104. mon = ioread32(irqc + IRQC_MONITOR);
  105. if (mon & REGULATOR_IRQ_MASK) {
  106. pr_debug("%s: IRQ2 is not asserted, not installing quirk\n",
  107. __func__);
  108. iounmap(irqc);
  109. return 0;
  110. }
  111. pr_info("IRQ2 is asserted, installing da9063/da9210 regulator quirk\n");
  112. bus_register_notifier(&i2c_bus_type, &regulator_quirk_nb);
  113. return 0;
  114. }
  115. arch_initcall(rcar_gen2_regulator_quirk);