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