xref: /linux/drivers/input/misc/da9052_onkey.c (revision b3fdfc1b4b641d372e35ced98814289bc60bc5d1)
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 
14 #include <linux/input.h>
15 #include <linux/module.h>
16 #include <linux/platform_device.h>
17 #include <linux/workqueue.h>
18 
19 #include <linux/mfd/da9052/da9052.h>
20 #include <linux/mfd/da9052/reg.h>
21 
22 struct da9052_onkey {
23 	struct da9052 *da9052;
24 	struct input_dev *input;
25 	struct delayed_work work;
26 };
27 
28 static void da9052_onkey_query(struct da9052_onkey *onkey)
29 {
30 	int key_stat;
31 
32 	key_stat = da9052_reg_read(onkey->da9052, DA9052_EVENT_B_REG);
33 	if (key_stat < 0) {
34 		dev_err(onkey->da9052->dev,
35 			"Failed to read onkey event %d\n", key_stat);
36 	} else {
37 		/*
38 		 * Since interrupt for deassertion of ONKEY pin is not
39 		 * generated, onkey event state determines the onkey
40 		 * button state.
41 		 */
42 		key_stat &= DA9052_EVENTB_ENONKEY;
43 		input_report_key(onkey->input, KEY_POWER, key_stat);
44 		input_sync(onkey->input);
45 	}
46 
47 	/*
48 	 * Interrupt is generated only when the ONKEY pin is asserted.
49 	 * Hence the deassertion of the pin is simulated through work queue.
50 	 */
51 	if (key_stat)
52 		schedule_delayed_work(&onkey->work, msecs_to_jiffies(50));
53 }
54 
55 static void da9052_onkey_work(struct work_struct *work)
56 {
57 	struct da9052_onkey *onkey = container_of(work, struct da9052_onkey,
58 						  work.work);
59 
60 	da9052_onkey_query(onkey);
61 }
62 
63 static irqreturn_t da9052_onkey_irq(int irq, void *data)
64 {
65 	struct da9052_onkey *onkey = data;
66 
67 	da9052_onkey_query(onkey);
68 
69 	return IRQ_HANDLED;
70 }
71 
72 static int da9052_onkey_probe(struct platform_device *pdev)
73 {
74 	struct da9052 *da9052 = dev_get_drvdata(pdev->dev.parent);
75 	struct da9052_onkey *onkey;
76 	struct input_dev *input_dev;
77 	int error;
78 
79 	if (!da9052) {
80 		dev_err(&pdev->dev, "Failed to get the driver's data\n");
81 		return -EINVAL;
82 	}
83 
84 	onkey = kzalloc(sizeof(*onkey), GFP_KERNEL);
85 	input_dev = input_allocate_device();
86 	if (!onkey || !input_dev) {
87 		dev_err(&pdev->dev, "Failed to allocate memory\n");
88 		error = -ENOMEM;
89 		goto err_free_mem;
90 	}
91 
92 	onkey->input = input_dev;
93 	onkey->da9052 = da9052;
94 	INIT_DELAYED_WORK(&onkey->work, da9052_onkey_work);
95 
96 	input_dev->name = "da9052-onkey";
97 	input_dev->phys = "da9052-onkey/input0";
98 	input_dev->dev.parent = &pdev->dev;
99 
100 	input_dev->evbit[0] = BIT_MASK(EV_KEY);
101 	__set_bit(KEY_POWER, input_dev->keybit);
102 
103 	error = da9052_request_irq(onkey->da9052, DA9052_IRQ_NONKEY, "ONKEY",
104 			    da9052_onkey_irq, onkey);
105 	if (error < 0) {
106 		dev_err(onkey->da9052->dev,
107 			"Failed to register ONKEY IRQ: %d\n", error);
108 		goto err_free_mem;
109 	}
110 
111 	error = input_register_device(onkey->input);
112 	if (error) {
113 		dev_err(&pdev->dev, "Unable to register input device, %d\n",
114 			error);
115 		goto err_free_irq;
116 	}
117 
118 	platform_set_drvdata(pdev, onkey);
119 	return 0;
120 
121 err_free_irq:
122 	da9052_free_irq(onkey->da9052, DA9052_IRQ_NONKEY, onkey);
123 	cancel_delayed_work_sync(&onkey->work);
124 err_free_mem:
125 	input_free_device(input_dev);
126 	kfree(onkey);
127 
128 	return error;
129 }
130 
131 static int da9052_onkey_remove(struct platform_device *pdev)
132 {
133 	struct da9052_onkey *onkey = platform_get_drvdata(pdev);
134 
135 	da9052_free_irq(onkey->da9052, DA9052_IRQ_NONKEY, onkey);
136 	cancel_delayed_work_sync(&onkey->work);
137 
138 	input_unregister_device(onkey->input);
139 	kfree(onkey);
140 
141 	return 0;
142 }
143 
144 static struct platform_driver da9052_onkey_driver = {
145 	.probe	= da9052_onkey_probe,
146 	.remove	= da9052_onkey_remove,
147 	.driver = {
148 		.name	= "da9052-onkey",
149 		.owner	= THIS_MODULE,
150 	},
151 };
152 module_platform_driver(da9052_onkey_driver);
153 
154 MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>");
155 MODULE_DESCRIPTION("Onkey driver for DA9052");
156 MODULE_LICENSE("GPL");
157 MODULE_ALIAS("platform:da9052-onkey");
158