intel_bxtwc_tmu.c 4.1 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/interrupt.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/mfd/intel_soc_pmic.h>
  24. #define BXTWC_TMUIRQ 0x4fb6
  25. #define BXTWC_MIRQLVL1 0x4e0e
  26. #define BXTWC_MTMUIRQ_REG 0x4fb7
  27. #define BXTWC_MIRQLVL1_MTMU BIT(1)
  28. #define BXTWC_TMU_WK_ALRM BIT(1)
  29. #define BXTWC_TMU_SYS_ALRM BIT(2)
  30. #define BXTWC_TMU_ALRM_MASK (BXTWC_TMU_WK_ALRM | BXTWC_TMU_SYS_ALRM)
  31. #define BXTWC_TMU_ALRM_IRQ (BXTWC_TMU_WK_ALRM | BXTWC_TMU_SYS_ALRM)
  32. struct wcove_tmu {
  33. int irq;
  34. struct device *dev;
  35. struct regmap *regmap;
  36. };
  37. static irqreturn_t bxt_wcove_tmu_irq_handler(int irq, void *data)
  38. {
  39. struct wcove_tmu *wctmu = data;
  40. unsigned int tmu_irq;
  41. /* Read TMU interrupt reg */
  42. regmap_read(wctmu->regmap, BXTWC_TMUIRQ, &tmu_irq);
  43. if (tmu_irq & BXTWC_TMU_ALRM_IRQ) {
  44. /* clear TMU irq */
  45. regmap_write(wctmu->regmap, BXTWC_TMUIRQ, tmu_irq);
  46. return IRQ_HANDLED;
  47. }
  48. return IRQ_NONE;
  49. }
  50. static int bxt_wcove_tmu_probe(struct platform_device *pdev)
  51. {
  52. struct intel_soc_pmic *pmic = dev_get_drvdata(pdev->dev.parent);
  53. struct regmap_irq_chip_data *regmap_irq_chip;
  54. struct wcove_tmu *wctmu;
  55. int ret, virq, irq;
  56. wctmu = devm_kzalloc(&pdev->dev, sizeof(*wctmu), GFP_KERNEL);
  57. if (!wctmu)
  58. return -ENOMEM;
  59. wctmu->dev = &pdev->dev;
  60. wctmu->regmap = pmic->regmap;
  61. irq = platform_get_irq(pdev, 0);
  62. if (irq < 0) {
  63. dev_err(&pdev->dev, "invalid irq %d\n", irq);
  64. return irq;
  65. }
  66. regmap_irq_chip = pmic->irq_chip_data_tmu;
  67. virq = regmap_irq_get_virq(regmap_irq_chip, irq);
  68. if (virq < 0) {
  69. dev_err(&pdev->dev,
  70. "failed to get virtual interrupt=%d\n", irq);
  71. return virq;
  72. }
  73. ret = devm_request_threaded_irq(&pdev->dev, virq,
  74. NULL, bxt_wcove_tmu_irq_handler,
  75. IRQF_ONESHOT, "bxt_wcove_tmu", wctmu);
  76. if (ret) {
  77. dev_err(&pdev->dev, "request irq failed: %d,virq: %d\n",
  78. ret, virq);
  79. return ret;
  80. }
  81. wctmu->irq = virq;
  82. /* Unmask TMU second level Wake & System alarm */
  83. regmap_update_bits(wctmu->regmap, BXTWC_MTMUIRQ_REG,
  84. BXTWC_TMU_ALRM_MASK, 0);
  85. platform_set_drvdata(pdev, wctmu);
  86. return 0;
  87. }
  88. static int bxt_wcove_tmu_remove(struct platform_device *pdev)
  89. {
  90. struct wcove_tmu *wctmu = platform_get_drvdata(pdev);
  91. unsigned int val;
  92. /* Mask TMU interrupts */
  93. regmap_read(wctmu->regmap, BXTWC_MIRQLVL1, &val);
  94. regmap_write(wctmu->regmap, BXTWC_MIRQLVL1,
  95. val | BXTWC_MIRQLVL1_MTMU);
  96. regmap_read(wctmu->regmap, BXTWC_MTMUIRQ_REG, &val);
  97. regmap_write(wctmu->regmap, BXTWC_MTMUIRQ_REG,
  98. val | BXTWC_TMU_ALRM_MASK);
  99. return 0;
  100. }
  101. #ifdef CONFIG_PM_SLEEP
  102. static int bxtwc_tmu_suspend(struct device *dev)
  103. {
  104. struct wcove_tmu *wctmu = dev_get_drvdata(dev);
  105. enable_irq_wake(wctmu->irq);
  106. return 0;
  107. }
  108. static int bxtwc_tmu_resume(struct device *dev)
  109. {
  110. struct wcove_tmu *wctmu = dev_get_drvdata(dev);
  111. disable_irq_wake(wctmu->irq);
  112. return 0;
  113. }
  114. #endif
  115. static SIMPLE_DEV_PM_OPS(bxtwc_tmu_pm_ops, bxtwc_tmu_suspend, bxtwc_tmu_resume);
  116. static const struct platform_device_id bxt_wcove_tmu_id_table[] = {
  117. { .name = "bxt_wcove_tmu" },
  118. {},
  119. };
  120. MODULE_DEVICE_TABLE(platform, bxt_wcove_tmu_id_table);
  121. static struct platform_driver bxt_wcove_tmu_driver = {
  122. .probe = bxt_wcove_tmu_probe,
  123. .remove = bxt_wcove_tmu_remove,
  124. .driver = {
  125. .name = "bxt_wcove_tmu",
  126. .pm = &bxtwc_tmu_pm_ops,
  127. },
  128. .id_table = bxt_wcove_tmu_id_table,
  129. };
  130. module_platform_driver(bxt_wcove_tmu_driver);
  131. MODULE_LICENSE("GPL v2");
  132. MODULE_AUTHOR("Nilesh Bacchewar <nilesh.bacchewar@intel.com>");
  133. MODULE_DESCRIPTION("BXT Whiskey Cove TMU Driver");