pcap_keys.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Input driver for PCAP events:
  3. * * Power key
  4. * * Headphone button
  5. *
  6. * Copyright (c) 2008,2009 Ilya Petrov <ilya.muromec@gmail.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. */
  13. #include <linux/module.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/input.h>
  17. #include <linux/mfd/ezx-pcap.h>
  18. #include <linux/slab.h>
  19. struct pcap_keys {
  20. struct pcap_chip *pcap;
  21. struct input_dev *input;
  22. };
  23. /* PCAP2 interrupts us on keypress */
  24. static irqreturn_t pcap_keys_handler(int irq, void *_pcap_keys)
  25. {
  26. struct pcap_keys *pcap_keys = _pcap_keys;
  27. int pirq = irq_to_pcap(pcap_keys->pcap, irq);
  28. u32 pstat;
  29. ezx_pcap_read(pcap_keys->pcap, PCAP_REG_PSTAT, &pstat);
  30. pstat &= 1 << pirq;
  31. switch (pirq) {
  32. case PCAP_IRQ_ONOFF:
  33. input_report_key(pcap_keys->input, KEY_POWER, !pstat);
  34. break;
  35. case PCAP_IRQ_MIC:
  36. input_report_key(pcap_keys->input, KEY_HP, !pstat);
  37. break;
  38. }
  39. input_sync(pcap_keys->input);
  40. return IRQ_HANDLED;
  41. }
  42. static int pcap_keys_probe(struct platform_device *pdev)
  43. {
  44. int err = -ENOMEM;
  45. struct pcap_keys *pcap_keys;
  46. struct input_dev *input_dev;
  47. pcap_keys = kmalloc(sizeof(struct pcap_keys), GFP_KERNEL);
  48. if (!pcap_keys)
  49. return err;
  50. pcap_keys->pcap = dev_get_drvdata(pdev->dev.parent);
  51. input_dev = input_allocate_device();
  52. if (!input_dev)
  53. goto fail;
  54. pcap_keys->input = input_dev;
  55. platform_set_drvdata(pdev, pcap_keys);
  56. input_dev->name = pdev->name;
  57. input_dev->phys = "pcap-keys/input0";
  58. input_dev->id.bustype = BUS_HOST;
  59. input_dev->dev.parent = &pdev->dev;
  60. __set_bit(EV_KEY, input_dev->evbit);
  61. __set_bit(KEY_POWER, input_dev->keybit);
  62. __set_bit(KEY_HP, input_dev->keybit);
  63. err = input_register_device(input_dev);
  64. if (err)
  65. goto fail_allocate;
  66. err = request_irq(pcap_to_irq(pcap_keys->pcap, PCAP_IRQ_ONOFF),
  67. pcap_keys_handler, 0, "Power key", pcap_keys);
  68. if (err)
  69. goto fail_register;
  70. err = request_irq(pcap_to_irq(pcap_keys->pcap, PCAP_IRQ_MIC),
  71. pcap_keys_handler, 0, "Headphone button", pcap_keys);
  72. if (err)
  73. goto fail_pwrkey;
  74. return 0;
  75. fail_pwrkey:
  76. free_irq(pcap_to_irq(pcap_keys->pcap, PCAP_IRQ_ONOFF), pcap_keys);
  77. fail_register:
  78. input_unregister_device(input_dev);
  79. goto fail;
  80. fail_allocate:
  81. input_free_device(input_dev);
  82. fail:
  83. kfree(pcap_keys);
  84. return err;
  85. }
  86. static int pcap_keys_remove(struct platform_device *pdev)
  87. {
  88. struct pcap_keys *pcap_keys = platform_get_drvdata(pdev);
  89. free_irq(pcap_to_irq(pcap_keys->pcap, PCAP_IRQ_ONOFF), pcap_keys);
  90. free_irq(pcap_to_irq(pcap_keys->pcap, PCAP_IRQ_MIC), pcap_keys);
  91. input_unregister_device(pcap_keys->input);
  92. kfree(pcap_keys);
  93. return 0;
  94. }
  95. static struct platform_driver pcap_keys_device_driver = {
  96. .probe = pcap_keys_probe,
  97. .remove = pcap_keys_remove,
  98. .driver = {
  99. .name = "pcap-keys",
  100. }
  101. };
  102. module_platform_driver(pcap_keys_device_driver);
  103. MODULE_DESCRIPTION("Motorola PCAP2 input events driver");
  104. MODULE_AUTHOR("Ilya Petrov <ilya.muromec@gmail.com>");
  105. MODULE_LICENSE("GPL");
  106. MODULE_ALIAS("platform:pcap_keys");