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