intel_bxtwc_tmu.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * intel_bxtwc_tmu.c - Intel BXT Whiskey Cove PMIC TMU driver
  3. *
  4. * Copyright (C) 2016 Intel Corporation. All rights reserved.
  5. *
  6. * This driver adds TMU (Time Management Unit) support for Intel BXT platform.
  7. * It enables the alarm wake-up functionality in the TMU unit of Whiskey Cove
  8. * PMIC.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License version
  12. * 2 as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. */
  20. #include <linux/module.h>
  21. #include <linux/mod_devicetable.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/mfd/intel_soc_pmic.h>
  25. #define BXTWC_TMUIRQ 0x4fb6
  26. #define BXTWC_MIRQLVL1 0x4e0e
  27. #define BXTWC_MTMUIRQ_REG 0x4fb7
  28. #define BXTWC_MIRQLVL1_MTMU BIT(1)
  29. #define BXTWC_TMU_WK_ALRM BIT(1)
  30. #define BXTWC_TMU_SYS_ALRM BIT(2)
  31. #define BXTWC_TMU_ALRM_MASK (BXTWC_TMU_WK_ALRM | BXTWC_TMU_SYS_ALRM)
  32. #define BXTWC_TMU_ALRM_IRQ (BXTWC_TMU_WK_ALRM | BXTWC_TMU_SYS_ALRM)
  33. struct wcove_tmu {
  34. int irq;
  35. struct device *dev;
  36. struct regmap *regmap;
  37. };
  38. static irqreturn_t bxt_wcove_tmu_irq_handler(int irq, void *data)
  39. {
  40. struct wcove_tmu *wctmu = data;
  41. unsigned int tmu_irq;
  42. /* Read TMU interrupt reg */
  43. regmap_read(wctmu->regmap, BXTWC_TMUIRQ, &tmu_irq);
  44. if (tmu_irq & BXTWC_TMU_ALRM_IRQ) {
  45. /* clear TMU irq */
  46. regmap_write(wctmu->regmap, BXTWC_TMUIRQ, tmu_irq);
  47. return IRQ_HANDLED;
  48. }
  49. return IRQ_NONE;
  50. }
  51. static int bxt_wcove_tmu_probe(struct platform_device *pdev)
  52. {
  53. struct intel_soc_pmic *pmic = dev_get_drvdata(pdev->dev.parent);
  54. struct regmap_irq_chip_data *regmap_irq_chip;
  55. struct wcove_tmu *wctmu;
  56. int ret, virq, irq;
  57. wctmu = devm_kzalloc(&pdev->dev, sizeof(*wctmu), GFP_KERNEL);
  58. if (!wctmu)
  59. return -ENOMEM;
  60. wctmu->dev = &pdev->dev;
  61. wctmu->regmap = pmic->regmap;
  62. irq = platform_get_irq(pdev, 0);
  63. if (irq < 0) {
  64. dev_err(&pdev->dev, "invalid irq %d\n", irq);
  65. return irq;
  66. }
  67. regmap_irq_chip = pmic->irq_chip_data_tmu;
  68. virq = regmap_irq_get_virq(regmap_irq_chip, irq);
  69. if (virq < 0) {
  70. dev_err(&pdev->dev,
  71. "failed to get virtual interrupt=%d\n", irq);
  72. return virq;
  73. }
  74. ret = devm_request_threaded_irq(&pdev->dev, virq,
  75. NULL, bxt_wcove_tmu_irq_handler,
  76. IRQF_ONESHOT, "bxt_wcove_tmu", wctmu);
  77. if (ret) {
  78. dev_err(&pdev->dev, "request irq failed: %d,virq: %d\n",
  79. ret, virq);
  80. return ret;
  81. }
  82. wctmu->irq = virq;
  83. /* Unmask TMU second level Wake & System alarm */
  84. regmap_update_bits(wctmu->regmap, BXTWC_MTMUIRQ_REG,
  85. BXTWC_TMU_ALRM_MASK, 0);
  86. platform_set_drvdata(pdev, wctmu);
  87. return 0;
  88. }
  89. static int bxt_wcove_tmu_remove(struct platform_device *pdev)
  90. {
  91. struct wcove_tmu *wctmu = platform_get_drvdata(pdev);
  92. unsigned int val;
  93. /* Mask TMU interrupts */
  94. regmap_read(wctmu->regmap, BXTWC_MIRQLVL1, &val);
  95. regmap_write(wctmu->regmap, BXTWC_MIRQLVL1,
  96. val | BXTWC_MIRQLVL1_MTMU);
  97. regmap_read(wctmu->regmap, BXTWC_MTMUIRQ_REG, &val);
  98. regmap_write(wctmu->regmap, BXTWC_MTMUIRQ_REG,
  99. val | BXTWC_TMU_ALRM_MASK);
  100. return 0;
  101. }
  102. #ifdef CONFIG_PM_SLEEP
  103. static int bxtwc_tmu_suspend(struct device *dev)
  104. {
  105. struct wcove_tmu *wctmu = dev_get_drvdata(dev);
  106. enable_irq_wake(wctmu->irq);
  107. return 0;
  108. }
  109. static int bxtwc_tmu_resume(struct device *dev)
  110. {
  111. struct wcove_tmu *wctmu = dev_get_drvdata(dev);
  112. disable_irq_wake(wctmu->irq);
  113. return 0;
  114. }
  115. #endif
  116. static SIMPLE_DEV_PM_OPS(bxtwc_tmu_pm_ops, bxtwc_tmu_suspend, bxtwc_tmu_resume);
  117. static const struct platform_device_id bxt_wcove_tmu_id_table[] = {
  118. { .name = "bxt_wcove_tmu" },
  119. {},
  120. };
  121. MODULE_DEVICE_TABLE(platform, bxt_wcove_tmu_id_table);
  122. static struct platform_driver bxt_wcove_tmu_driver = {
  123. .probe = bxt_wcove_tmu_probe,
  124. .remove = bxt_wcove_tmu_remove,
  125. .driver = {
  126. .name = "bxt_wcove_tmu",
  127. .pm = &bxtwc_tmu_pm_ops,
  128. },
  129. .id_table = bxt_wcove_tmu_id_table,
  130. };
  131. module_platform_driver(bxt_wcove_tmu_driver);
  132. MODULE_LICENSE("GPL v2");
  133. MODULE_AUTHOR("Nilesh Bacchewar <nilesh.bacchewar@intel.com>");
  134. MODULE_DESCRIPTION("BXT Whiskey Cove TMU Driver");