intel_bxtwc_tmu.c 3.7 KB

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