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