da9052_onkey.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * ON pin driver for Dialog DA9052 PMICs
  3. *
  4. * Copyright(c) 2012 Dialog Semiconductor Ltd.
  5. *
  6. * Author: David Dajun Chen <dchen@diasemi.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. */
  13. #include <linux/input.h>
  14. #include <linux/module.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/workqueue.h>
  17. #include <linux/mfd/da9052/da9052.h>
  18. #include <linux/mfd/da9052/reg.h>
  19. struct da9052_onkey {
  20. struct da9052 *da9052;
  21. struct input_dev *input;
  22. struct delayed_work work;
  23. };
  24. static void da9052_onkey_query(struct da9052_onkey *onkey)
  25. {
  26. int ret;
  27. ret = da9052_reg_read(onkey->da9052, DA9052_STATUS_A_REG);
  28. if (ret < 0) {
  29. dev_err(onkey->da9052->dev,
  30. "Failed to read onkey event err=%d\n", ret);
  31. } else {
  32. /*
  33. * Since interrupt for deassertion of ONKEY pin is not
  34. * generated, onkey event state determines the onkey
  35. * button state.
  36. */
  37. bool pressed = !(ret & DA9052_STATUSA_NONKEY);
  38. input_report_key(onkey->input, KEY_POWER, pressed);
  39. input_sync(onkey->input);
  40. /*
  41. * Interrupt is generated only when the ONKEY pin
  42. * is asserted. Hence the deassertion of the pin
  43. * is simulated through work queue.
  44. */
  45. if (pressed)
  46. schedule_delayed_work(&onkey->work,
  47. msecs_to_jiffies(50));
  48. }
  49. }
  50. static void da9052_onkey_work(struct work_struct *work)
  51. {
  52. struct da9052_onkey *onkey = container_of(work, struct da9052_onkey,
  53. work.work);
  54. da9052_onkey_query(onkey);
  55. }
  56. static irqreturn_t da9052_onkey_irq(int irq, void *data)
  57. {
  58. struct da9052_onkey *onkey = data;
  59. da9052_onkey_query(onkey);
  60. return IRQ_HANDLED;
  61. }
  62. static int da9052_onkey_probe(struct platform_device *pdev)
  63. {
  64. struct da9052 *da9052 = dev_get_drvdata(pdev->dev.parent);
  65. struct da9052_onkey *onkey;
  66. struct input_dev *input_dev;
  67. int error;
  68. if (!da9052) {
  69. dev_err(&pdev->dev, "Failed to get the driver's data\n");
  70. return -EINVAL;
  71. }
  72. onkey = kzalloc(sizeof(*onkey), GFP_KERNEL);
  73. input_dev = input_allocate_device();
  74. if (!onkey || !input_dev) {
  75. dev_err(&pdev->dev, "Failed to allocate memory\n");
  76. error = -ENOMEM;
  77. goto err_free_mem;
  78. }
  79. onkey->input = input_dev;
  80. onkey->da9052 = da9052;
  81. INIT_DELAYED_WORK(&onkey->work, da9052_onkey_work);
  82. input_dev->name = "da9052-onkey";
  83. input_dev->phys = "da9052-onkey/input0";
  84. input_dev->dev.parent = &pdev->dev;
  85. input_dev->evbit[0] = BIT_MASK(EV_KEY);
  86. __set_bit(KEY_POWER, input_dev->keybit);
  87. error = da9052_request_irq(onkey->da9052, DA9052_IRQ_NONKEY, "ONKEY",
  88. da9052_onkey_irq, onkey);
  89. if (error < 0) {
  90. dev_err(onkey->da9052->dev,
  91. "Failed to register ONKEY IRQ: %d\n", error);
  92. goto err_free_mem;
  93. }
  94. error = input_register_device(onkey->input);
  95. if (error) {
  96. dev_err(&pdev->dev, "Unable to register input device, %d\n",
  97. error);
  98. goto err_free_irq;
  99. }
  100. platform_set_drvdata(pdev, onkey);
  101. return 0;
  102. err_free_irq:
  103. da9052_free_irq(onkey->da9052, DA9052_IRQ_NONKEY, onkey);
  104. cancel_delayed_work_sync(&onkey->work);
  105. err_free_mem:
  106. input_free_device(input_dev);
  107. kfree(onkey);
  108. return error;
  109. }
  110. static int da9052_onkey_remove(struct platform_device *pdev)
  111. {
  112. struct da9052_onkey *onkey = platform_get_drvdata(pdev);
  113. da9052_free_irq(onkey->da9052, DA9052_IRQ_NONKEY, onkey);
  114. cancel_delayed_work_sync(&onkey->work);
  115. input_unregister_device(onkey->input);
  116. kfree(onkey);
  117. return 0;
  118. }
  119. static struct platform_driver da9052_onkey_driver = {
  120. .probe = da9052_onkey_probe,
  121. .remove = da9052_onkey_remove,
  122. .driver = {
  123. .name = "da9052-onkey",
  124. },
  125. };
  126. module_platform_driver(da9052_onkey_driver);
  127. MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>");
  128. MODULE_DESCRIPTION("Onkey driver for DA9052");
  129. MODULE_LICENSE("GPL");
  130. MODULE_ALIAS("platform:da9052-onkey");