xref: /linux/drivers/input/misc/da9055_onkey.c (revision ec63e2a4897075e427c121d863bd89c44578094f)
1 /*
2  * ON pin driver for Dialog DA9055 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 
18 #include <linux/mfd/da9055/core.h>
19 #include <linux/mfd/da9055/reg.h>
20 
21 struct da9055_onkey {
22 	struct da9055 *da9055;
23 	struct input_dev *input;
24 	struct delayed_work work;
25 };
26 
27 static void da9055_onkey_query(struct da9055_onkey *onkey)
28 {
29 	int key_stat;
30 
31 	key_stat = da9055_reg_read(onkey->da9055, DA9055_REG_STATUS_A);
32 	if (key_stat < 0) {
33 		dev_err(onkey->da9055->dev,
34 			"Failed to read onkey event %d\n", key_stat);
35 	} else {
36 		key_stat &= DA9055_NOKEY_STS;
37 		/*
38 		 * Onkey status bit is cleared when onkey button is released.
39 		 */
40 		if (!key_stat) {
41 			input_report_key(onkey->input, KEY_POWER, 0);
42 			input_sync(onkey->input);
43 		}
44 	}
45 
46 	/*
47 	 * Interrupt is generated only when the ONKEY pin is asserted.
48 	 * Hence the deassertion of the pin is simulated through work queue.
49 	 */
50 	if (key_stat)
51 		schedule_delayed_work(&onkey->work, msecs_to_jiffies(10));
52 
53 }
54 
55 static void da9055_onkey_work(struct work_struct *work)
56 {
57 	struct da9055_onkey *onkey = container_of(work, struct da9055_onkey,
58 						  work.work);
59 
60 	da9055_onkey_query(onkey);
61 }
62 
63 static irqreturn_t da9055_onkey_irq(int irq, void *data)
64 {
65 	struct da9055_onkey *onkey = data;
66 
67 	input_report_key(onkey->input, KEY_POWER, 1);
68 	input_sync(onkey->input);
69 
70 	da9055_onkey_query(onkey);
71 
72 	return IRQ_HANDLED;
73 }
74 
75 static int da9055_onkey_probe(struct platform_device *pdev)
76 {
77 	struct da9055 *da9055 = dev_get_drvdata(pdev->dev.parent);
78 	struct da9055_onkey *onkey;
79 	struct input_dev *input_dev;
80 	int irq, err;
81 
82 	irq = platform_get_irq_byname(pdev, "ONKEY");
83 	if (irq < 0) {
84 		dev_err(&pdev->dev,
85 			"Failed to get an IRQ for input device, %d\n", irq);
86 		return -EINVAL;
87 	}
88 
89 	onkey = devm_kzalloc(&pdev->dev, sizeof(*onkey), GFP_KERNEL);
90 	if (!onkey) {
91 		dev_err(&pdev->dev, "Failed to allocate memory\n");
92 		return -ENOMEM;
93 	}
94 
95 	input_dev = input_allocate_device();
96 	if (!input_dev) {
97 		dev_err(&pdev->dev, "Failed to allocate memory\n");
98 		return -ENOMEM;
99 	}
100 
101 	onkey->input = input_dev;
102 	onkey->da9055 = da9055;
103 	input_dev->name = "da9055-onkey";
104 	input_dev->phys = "da9055-onkey/input0";
105 	input_dev->dev.parent = &pdev->dev;
106 
107 	input_dev->evbit[0] = BIT_MASK(EV_KEY);
108 	__set_bit(KEY_POWER, input_dev->keybit);
109 
110 	INIT_DELAYED_WORK(&onkey->work, da9055_onkey_work);
111 
112 	err = request_threaded_irq(irq, NULL, da9055_onkey_irq,
113 				   IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
114 				   "ONKEY", onkey);
115 	if (err < 0) {
116 		dev_err(&pdev->dev,
117 			"Failed to register ONKEY IRQ %d, error = %d\n",
118 			irq, err);
119 		goto err_free_input;
120 	}
121 
122 	err = input_register_device(input_dev);
123 	if (err) {
124 		dev_err(&pdev->dev, "Unable to register input device, %d\n",
125 			err);
126 		goto err_free_irq;
127 	}
128 
129 	platform_set_drvdata(pdev, onkey);
130 
131 	return 0;
132 
133 err_free_irq:
134 	free_irq(irq, onkey);
135 	cancel_delayed_work_sync(&onkey->work);
136 err_free_input:
137 	input_free_device(input_dev);
138 
139 	return err;
140 }
141 
142 static int da9055_onkey_remove(struct platform_device *pdev)
143 {
144 	struct da9055_onkey *onkey = platform_get_drvdata(pdev);
145 	int irq = platform_get_irq_byname(pdev, "ONKEY");
146 
147 	irq = regmap_irq_get_virq(onkey->da9055->irq_data, irq);
148 	free_irq(irq, onkey);
149 	cancel_delayed_work_sync(&onkey->work);
150 	input_unregister_device(onkey->input);
151 
152 	return 0;
153 }
154 
155 static struct platform_driver da9055_onkey_driver = {
156 	.probe	= da9055_onkey_probe,
157 	.remove	= da9055_onkey_remove,
158 	.driver = {
159 		.name	= "da9055-onkey",
160 	},
161 };
162 
163 module_platform_driver(da9055_onkey_driver);
164 
165 MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>");
166 MODULE_DESCRIPTION("Onkey driver for DA9055");
167 MODULE_LICENSE("GPL");
168 MODULE_ALIAS("platform:da9055-onkey");
169