]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/cbus/retu-headset.c
CBUS: Cleanup retu-headset driver
[linux-2.6-omap-h63xx.git] / drivers / cbus / retu-headset.c
1 /**
2  * Retu/Vilma headset detection
3  *
4  * Copyright (C) 2006 Nokia Corporation
5  *
6  * Written by Juha Yrjölä
7  *
8  * This file is subject to the terms and conditions of the GNU General
9  * Public License. See the file "COPYING" in the main directory of this
10  * archive for more details.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include <linux/module.h>
23 #include <linux/init.h>
24 #include <linux/kernel.h>
25 #include <linux/delay.h>
26 #include <linux/input.h>
27 #include <linux/platform_device.h>
28
29 #include "retu.h"
30
31 #define RETU_ADC_CHANNEL_HOOKDET        0x05
32
33 #define RETU_HEADSET_KEY                KEY_PHONE
34
35 struct retu_headset {
36         spinlock_t                      lock;
37         struct platform_device          *pdev;
38         struct input_dev                *idev;
39         unsigned                        bias_enabled:1;
40         unsigned                        detection_enabled:1;
41         unsigned                        pressed:1;
42         struct timer_list               enable_timer;
43         struct timer_list               detect_timer;
44 };
45
46 static void retu_headset_set_bias(int enable)
47 {
48         if (enable) {
49                 retu_set_clear_reg_bits(RETU_REG_AUDTXR,
50                                         (1 << 0) | (1 << 1), 0);
51                 msleep(2);
52                 retu_set_clear_reg_bits(RETU_REG_AUDTXR, 1 << 3, 0);
53         } else {
54                 retu_set_clear_reg_bits(RETU_REG_AUDTXR, 0,
55                                         (1 << 0) | (1 << 1) | (1 << 3));
56         }
57 }
58
59 static void retu_headset_enable(struct retu_headset *hs)
60 {
61         if (hs->bias_enabled)
62                 return;
63         hs->bias_enabled = 1;
64         retu_headset_set_bias(1);
65 }
66
67 static void retu_headset_disable(struct retu_headset *hs)
68 {
69         if (!hs->bias_enabled)
70                 return;
71         hs->bias_enabled = 0;
72         retu_headset_set_bias(0);
73 }
74
75 static void retu_headset_det_enable(struct retu_headset *hs)
76 {
77         if (hs->detection_enabled)
78                 return;
79         hs->detection_enabled = 1;
80         retu_set_clear_reg_bits(RETU_REG_CC1, (1 << 10) | (1 << 8), 0);
81         retu_enable_irq(RETU_INT_HOOK);
82 }
83
84 static void retu_headset_det_disable(struct retu_headset *hs)
85 {
86         if (!hs->detection_enabled)
87                 return;
88         hs->detection_enabled = 0;
89         retu_disable_irq(RETU_INT_HOOK);
90         del_timer_sync(&hs->enable_timer);
91         del_timer_sync(&hs->detect_timer);
92         if (hs->pressed)
93                 input_report_key(hs->idev, RETU_HEADSET_KEY, 0);
94         retu_set_clear_reg_bits(RETU_REG_CC1, 0, (1 << 10) | (1 << 8));
95 }
96
97 static ssize_t retu_headset_hookdet_show(struct device *dev,
98                                          struct device_attribute *attr,
99                                          char *buf)
100 {
101         int val;
102
103         val = retu_read_adc(RETU_ADC_CHANNEL_HOOKDET);
104         return sprintf(buf, "%d\n", val);
105 }
106
107 static DEVICE_ATTR(hookdet, S_IRUGO, retu_headset_hookdet_show, NULL);
108
109 static ssize_t retu_headset_enable_show(struct device *dev,
110                                         struct device_attribute *attr,
111                                         char *buf)
112 {
113         struct retu_headset *hs = dev_get_drvdata(dev);
114
115         return sprintf(buf, "%u\n", hs->bias_enabled);
116 }
117
118 static ssize_t retu_headset_enable_store(struct device *dev,
119                                          struct device_attribute *attr,
120                                          const char *buf, size_t count)
121 {
122         struct retu_headset *hs = dev_get_drvdata(dev);
123         int enable;
124
125         if (sscanf(buf, "%u", &enable) != 1)
126                 return -EINVAL;
127         if (enable)
128                 retu_headset_enable(hs);
129         else
130                 retu_headset_disable(hs);
131         return count;
132 }
133
134 static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR | S_IWGRP,
135                    retu_headset_enable_show, retu_headset_enable_store);
136
137 static ssize_t retu_headset_enable_det_show(struct device *dev,
138                                             struct device_attribute *attr,
139                                             char *buf)
140 {
141         struct retu_headset *hs = dev_get_drvdata(dev);
142
143         return sprintf(buf, "%u\n", hs->detection_enabled);
144 }
145
146 static ssize_t retu_headset_enable_det_store(struct device *dev,
147                                              struct device_attribute *attr,
148                                              const char *buf, size_t count)
149 {
150         struct retu_headset *hs = dev_get_drvdata(dev);
151         int enable;
152
153         if (sscanf(buf, "%u", &enable) != 1)
154                 return -EINVAL;
155         if (enable)
156                 retu_headset_det_enable(hs);
157         else
158                 retu_headset_det_disable(hs);
159         return count;
160 }
161
162 static DEVICE_ATTR(enable_det, S_IRUGO | S_IWUSR | S_IWGRP,
163                    retu_headset_enable_det_show,
164                    retu_headset_enable_det_store);
165
166 static void retu_headset_hook_interrupt(unsigned long arg)
167 {
168         struct retu_headset *hs = (struct retu_headset *) arg;
169         unsigned long flags;
170         int was_pressed = 0;
171
172         retu_ack_irq(RETU_INT_HOOK);
173         spin_lock_irqsave(&hs->lock, flags);
174         if (!hs->pressed) {
175                 /* Headset button was just pressed down. */
176                 hs->pressed = 1;
177                 input_report_key(hs->idev, RETU_HEADSET_KEY, 1);
178                 was_pressed = 1;
179         }
180         spin_unlock_irqrestore(&hs->lock, flags);
181         if (was_pressed)
182                 dev_info(&hs->pdev->dev, "button pressed\n");
183         retu_set_clear_reg_bits(RETU_REG_CC1, 0, (1 << 10) | (1 << 8));
184         mod_timer(&hs->enable_timer, jiffies + msecs_to_jiffies(50));
185 }
186
187 static void retu_headset_enable_timer(unsigned long arg)
188 {
189         struct retu_headset *hs = (struct retu_headset *) arg;
190
191         retu_set_clear_reg_bits(RETU_REG_CC1, (1 << 10) | (1 << 8), 0);
192         mod_timer(&hs->detect_timer, jiffies + msecs_to_jiffies(350));
193 }
194
195 static void retu_headset_detect_timer(unsigned long arg)
196 {
197         struct retu_headset *hs = (struct retu_headset *) arg;
198         unsigned long flags;
199
200         spin_lock_irqsave(&hs->lock, flags);
201         BUG_ON(!hs->pressed);
202         input_report_key(hs->idev, RETU_HEADSET_KEY, 0);
203         hs->pressed = 0;
204         spin_unlock_irqrestore(&hs->lock, flags);
205         dev_info(&hs->pdev->dev, "button released\n");
206 }
207
208 static int __init retu_headset_probe(struct platform_device *pdev)
209 {
210         struct retu_headset *hs;
211         int r;
212
213         hs = kzalloc(sizeof(*hs), GFP_KERNEL);
214         if (hs == NULL)
215                 return -ENOMEM;
216
217         hs->pdev = pdev;
218
219         hs->idev = input_allocate_device();
220         if (hs->idev == NULL) {
221                 r = -ENOMEM;
222                 goto err1;
223         }
224         hs->idev->name = "retu-headset";
225         hs->idev->cdev.dev = &pdev->dev;
226         hs->idev->private = hs;
227         set_bit(EV_KEY, hs->idev->evbit);
228         set_bit(RETU_HEADSET_KEY, hs->idev->keybit);
229         r = input_register_device(hs->idev);
230         if (r < 0)
231                 goto err2;
232
233         r = device_create_file(&pdev->dev, &dev_attr_hookdet);
234         if (r < 0)
235                 goto err3;
236         r = device_create_file(&pdev->dev, &dev_attr_enable);
237         if (r < 0)
238                 goto err4;
239         r = device_create_file(&pdev->dev, &dev_attr_enable_det);
240         if (r < 0)
241                 goto err5;
242         platform_set_drvdata(pdev, hs);
243
244         spin_lock_init(&hs->lock);
245         setup_timer(&hs->enable_timer, retu_headset_enable_timer,
246                     (unsigned long) hs);
247         setup_timer(&hs->detect_timer, retu_headset_detect_timer,
248                     (unsigned long) hs);
249
250         r = retu_request_irq(RETU_INT_HOOK, retu_headset_hook_interrupt,
251                              (unsigned long) hs, "hookdet");
252         if (r != 0) {
253                 dev_err(&pdev->dev, "hookdet IRQ not available\n");
254                 goto err6;
255         }
256         retu_disable_irq(RETU_INT_HOOK);
257         return 0;
258 err6:
259         device_remove_file(&pdev->dev, &dev_attr_enable_det);
260 err5:
261         device_remove_file(&pdev->dev, &dev_attr_enable);
262 err4:
263         device_remove_file(&pdev->dev, &dev_attr_hookdet);
264 err3:
265         input_unregister_device(hs->idev);
266 err2:
267         input_free_device(hs->idev);
268 err1:
269         kfree(hs);
270         return r;
271 }
272
273 static int retu_headset_remove(struct platform_device *pdev)
274 {
275         struct retu_headset *hs = platform_get_drvdata(pdev);
276
277         device_remove_file(&pdev->dev, &dev_attr_hookdet);
278         device_remove_file(&pdev->dev, &dev_attr_enable);
279         device_remove_file(&pdev->dev, &dev_attr_enable_det);
280         retu_headset_disable(hs);
281         retu_headset_det_disable(hs);
282         retu_free_irq(RETU_INT_HOOK);
283         input_unregister_device(hs->idev);
284         input_free_device(hs->idev);
285         return 0;
286 }
287
288 static int retu_headset_suspend(struct platform_device *pdev,
289                                 pm_message_t mesg)
290 {
291         return 0;
292 }
293
294 static int retu_headset_resume(struct platform_device *pdev)
295 {
296         return 0;
297 }
298
299 static struct platform_driver retu_headset_driver = {
300         .probe          = retu_headset_probe,
301         .remove         = retu_headset_remove,
302         .suspend        = retu_headset_suspend,
303         .resume         = retu_headset_resume,
304         .driver         = {
305                 .name   = "retu-headset",
306         },
307 };
308
309 static int __init retu_headset_init(void)
310 {
311         int r;
312
313         printk(KERN_INFO "Retu/Vilma headset driver initializing\n");
314
315         r = platform_driver_register(&retu_headset_driver);
316         if (r < 0)
317                 return r;
318
319         return 0;
320 }
321
322 static void __exit retu_headset_exit(void)
323 {
324         platform_driver_unregister(&retu_headset_driver);
325 }
326
327 module_init(retu_headset_init);
328 module_exit(retu_headset_exit);
329
330 MODULE_DESCRIPTION("Retu/Vilma headset detection");
331 MODULE_LICENSE("GPL");
332 MODULE_AUTHOR("Juha Yrjölä");