sirfsoc-onkey.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Power key driver for SiRF PrimaII
  3. *
  4. * Copyright (c) 2013 Cambridge Silicon Radio Limited, a CSR plc group company.
  5. *
  6. * Licensed under GPLv2 or later.
  7. */
  8. #include <linux/module.h>
  9. #include <linux/init.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/delay.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/input.h>
  14. #include <linux/rtc/sirfsoc_rtciobrg.h>
  15. #include <linux/of.h>
  16. struct sirfsoc_pwrc_drvdata {
  17. u32 pwrc_base;
  18. struct input_dev *input;
  19. };
  20. #define PWRC_ON_KEY_BIT (1 << 0)
  21. #define PWRC_INT_STATUS 0xc
  22. #define PWRC_INT_MASK 0x10
  23. static irqreturn_t sirfsoc_pwrc_isr(int irq, void *dev_id)
  24. {
  25. struct sirfsoc_pwrc_drvdata *pwrcdrv = dev_id;
  26. u32 int_status;
  27. int_status = sirfsoc_rtc_iobrg_readl(pwrcdrv->pwrc_base +
  28. PWRC_INT_STATUS);
  29. sirfsoc_rtc_iobrg_writel(int_status & ~PWRC_ON_KEY_BIT,
  30. pwrcdrv->pwrc_base + PWRC_INT_STATUS);
  31. /*
  32. * For a typical Linux system, we report KEY_SUSPEND to trigger apm-power.c
  33. * to queue a SUSPEND APM event
  34. */
  35. input_event(pwrcdrv->input, EV_PWR, KEY_SUSPEND, 1);
  36. input_sync(pwrcdrv->input);
  37. /*
  38. * Todo: report KEY_POWER event for Android platforms, Android PowerManager
  39. * will handle the suspend and powerdown/hibernation
  40. */
  41. return IRQ_HANDLED;
  42. }
  43. static const struct of_device_id sirfsoc_pwrc_of_match[] = {
  44. { .compatible = "sirf,prima2-pwrc" },
  45. {},
  46. }
  47. MODULE_DEVICE_TABLE(of, sirfsoc_pwrc_of_match);
  48. static int sirfsoc_pwrc_probe(struct platform_device *pdev)
  49. {
  50. struct device_node *np = pdev->dev.of_node;
  51. struct sirfsoc_pwrc_drvdata *pwrcdrv;
  52. int irq;
  53. int error;
  54. pwrcdrv = devm_kzalloc(&pdev->dev, sizeof(struct sirfsoc_pwrc_drvdata),
  55. GFP_KERNEL);
  56. if (!pwrcdrv) {
  57. dev_info(&pdev->dev, "Not enough memory for the device data\n");
  58. return -ENOMEM;
  59. }
  60. /*
  61. * we can't use of_iomap because pwrc is not mapped in memory,
  62. * the so-called base address is only offset in rtciobrg
  63. */
  64. error = of_property_read_u32(np, "reg", &pwrcdrv->pwrc_base);
  65. if (error) {
  66. dev_err(&pdev->dev,
  67. "unable to find base address of pwrc node in dtb\n");
  68. return error;
  69. }
  70. pwrcdrv->input = devm_input_allocate_device(&pdev->dev);
  71. if (!pwrcdrv->input)
  72. return -ENOMEM;
  73. pwrcdrv->input->name = "sirfsoc pwrckey";
  74. pwrcdrv->input->phys = "pwrc/input0";
  75. pwrcdrv->input->evbit[0] = BIT_MASK(EV_PWR);
  76. irq = platform_get_irq(pdev, 0);
  77. error = devm_request_irq(&pdev->dev, irq,
  78. sirfsoc_pwrc_isr, IRQF_SHARED,
  79. "sirfsoc_pwrc_int", pwrcdrv);
  80. if (error) {
  81. dev_err(&pdev->dev, "unable to claim irq %d, error: %d\n",
  82. irq, error);
  83. return error;
  84. }
  85. sirfsoc_rtc_iobrg_writel(
  86. sirfsoc_rtc_iobrg_readl(pwrcdrv->pwrc_base + PWRC_INT_MASK) |
  87. PWRC_ON_KEY_BIT,
  88. pwrcdrv->pwrc_base + PWRC_INT_MASK);
  89. error = input_register_device(pwrcdrv->input);
  90. if (error) {
  91. dev_err(&pdev->dev,
  92. "unable to register input device, error: %d\n",
  93. error);
  94. return error;
  95. }
  96. platform_set_drvdata(pdev, pwrcdrv);
  97. device_init_wakeup(&pdev->dev, 1);
  98. return 0;
  99. }
  100. static int sirfsoc_pwrc_remove(struct platform_device *pdev)
  101. {
  102. device_init_wakeup(&pdev->dev, 0);
  103. return 0;
  104. }
  105. #ifdef CONFIG_PM_SLEEP
  106. static int pwrc_resume(struct device *dev)
  107. {
  108. struct platform_device *pdev = to_platform_device(dev);
  109. struct sirfsoc_pwrc_drvdata *pwrcdrv = platform_get_drvdata(pdev);
  110. /*
  111. * Do not mask pwrc interrupt as we want pwrc work as a wakeup source
  112. * if users touch X_ONKEY_B, see arch/arm/mach-prima2/pm.c
  113. */
  114. sirfsoc_rtc_iobrg_writel(
  115. sirfsoc_rtc_iobrg_readl(
  116. pwrcdrv->pwrc_base + PWRC_INT_MASK) | PWRC_ON_KEY_BIT,
  117. pwrcdrv->pwrc_base + PWRC_INT_MASK);
  118. return 0;
  119. }
  120. #endif
  121. static SIMPLE_DEV_PM_OPS(sirfsoc_pwrc_pm_ops, NULL, pwrc_resume);
  122. static struct platform_driver sirfsoc_pwrc_driver = {
  123. .probe = sirfsoc_pwrc_probe,
  124. .remove = sirfsoc_pwrc_remove,
  125. .driver = {
  126. .name = "sirfsoc-pwrc",
  127. .owner = THIS_MODULE,
  128. .pm = &sirfsoc_pwrc_pm_ops,
  129. .of_match_table = of_match_ptr(sirfsoc_pwrc_of_match),
  130. }
  131. };
  132. module_platform_driver(sirfsoc_pwrc_driver);
  133. MODULE_LICENSE("GPLv2");
  134. MODULE_AUTHOR("Binghua Duan <Binghua.Duan@csr.com>, Xianglong Du <Xianglong.Du@csr.com>");
  135. MODULE_DESCRIPTION("CSR Prima2 PWRC Driver");
  136. MODULE_ALIAS("platform:sirfsoc-pwrc");