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