]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - arch/arm/plat-omap/gpio-switch.c
Merge current mainline tree into linux-omap tree
[linux-2.6-omap-h63xx.git] / arch / arm / plat-omap / gpio-switch.c
1 /*
2  *  linux/arch/arm/plat-omap/gpio-switch.c
3  *
4  *  Copyright (C) 2004-2006 Nokia Corporation
5  *  Written by Juha Yrjölä <juha.yrjola@nokia.com>
6  *         and Paul Mundt <paul.mundt@nokia.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  */
12
13 #include <linux/sched.h>
14 #include <linux/init.h>
15 #include <linux/list.h>
16 #include <linux/irq.h>
17 #include <linux/interrupt.h>
18 #include <linux/module.h>
19 #include <linux/platform_device.h>
20 #include <linux/timer.h>
21 #include <linux/err.h>
22 #include <asm/arch/hardware.h>
23 #include <asm/arch/gpio.h>
24 #include <asm/arch/irqs.h>
25 #include <asm/arch/mux.h>
26 #include <asm/arch/board.h>
27 #include <asm/arch/gpio-switch.h>
28
29 struct gpio_switch {
30         char            name[14];
31         u16             gpio;
32         unsigned        flags:4;
33         unsigned        type:4;
34         unsigned        state:1;
35         unsigned        both_edges:1;
36
37         u16             debounce_rising;
38         u16             debounce_falling;
39
40         void (* notify)(void *data, int state);
41         void *notify_data;
42
43         struct work_struct      work;
44         struct timer_list       timer;
45         struct platform_device  pdev;
46
47         struct list_head        node;
48 };
49
50 static LIST_HEAD(gpio_switches);
51 static struct platform_device *gpio_sw_platform_dev;
52 static struct platform_driver gpio_sw_driver;
53
54 static const struct omap_gpio_switch *board_gpio_sw_table;
55 static int board_gpio_sw_count;
56
57 static const char *cover_str[2] = { "open", "closed" };
58 static const char *connection_str[2] = { "disconnected", "connected" };
59 static const char *activity_str[2] = { "inactive", "active" };
60
61 /*
62  * GPIO switch state default debounce delay in ms
63  */
64 #define OMAP_GPIO_SW_DEFAULT_DEBOUNCE           10
65
66 static const char **get_sw_str(struct gpio_switch *sw)
67 {
68         switch (sw->type) {
69         case OMAP_GPIO_SWITCH_TYPE_COVER:
70                 return cover_str;
71         case OMAP_GPIO_SWITCH_TYPE_CONNECTION:
72                 return connection_str;
73         case OMAP_GPIO_SWITCH_TYPE_ACTIVITY:
74                 return activity_str;
75         default:
76                 BUG();
77                 return NULL;
78         }
79 }
80
81 static const char *get_sw_type(struct gpio_switch *sw)
82 {
83         switch (sw->type) {
84         case OMAP_GPIO_SWITCH_TYPE_COVER:
85                 return "cover";
86         case OMAP_GPIO_SWITCH_TYPE_CONNECTION:
87                 return "connection";
88         case OMAP_GPIO_SWITCH_TYPE_ACTIVITY:
89                 return "activity";
90         default:
91                 BUG();
92                 return NULL;
93         }
94 }
95
96 static void print_sw_state(struct gpio_switch *sw, int state)
97 {
98         const char **str;
99
100         str = get_sw_str(sw);
101         if (str != NULL)
102                 printk(KERN_INFO "%s (GPIO %d) is now %s\n", sw->name, sw->gpio, str[state]);
103 }
104
105 static int gpio_sw_get_state(struct gpio_switch *sw)
106 {
107         int state;
108
109         state = omap_get_gpio_datain(sw->gpio);
110         if (sw->flags & OMAP_GPIO_SWITCH_FLAG_INVERTED)
111                 state = !state;
112
113         return state;
114 }
115
116 static ssize_t gpio_sw_state_store(struct device *dev,
117                                    struct device_attribute *attr,
118                                    const char *buf,
119                                    size_t count)
120 {
121         struct gpio_switch *sw = dev_get_drvdata(dev);
122         const char **str;
123         char state[16];
124         int enable;
125
126         if (!(sw->flags & OMAP_GPIO_SWITCH_FLAG_OUTPUT))
127                 return -EPERM;
128
129         if (sscanf(buf, "%15s", state) != 1)
130                 return -EINVAL;
131
132         str = get_sw_str(sw);
133         if (strcmp(state, str[0]) == 0)
134                 enable = 0;
135         else if (strcmp(state, str[1]) == 0)
136                 enable = 1;
137         else
138                 return -EINVAL;
139         if (sw->flags & OMAP_GPIO_SWITCH_FLAG_INVERTED)
140                 enable = !enable;
141         omap_set_gpio_dataout(sw->gpio, enable);
142
143         return count;
144 }
145
146 static ssize_t gpio_sw_state_show(struct device *dev,
147                                   struct device_attribute *attr,
148                                   char *buf)
149 {
150         struct gpio_switch *sw = dev_get_drvdata(dev);
151         const char **str;
152
153         str = get_sw_str(sw);
154         return sprintf(buf, "%s\n", str[sw->state]);
155 }
156
157 static DEVICE_ATTR(state, S_IRUGO | S_IWUSR, gpio_sw_state_show,
158                    gpio_sw_state_store);
159
160 static ssize_t gpio_sw_type_show(struct device *dev,
161                                  struct device_attribute *attr,
162                                  char *buf)
163 {
164         struct gpio_switch *sw = dev_get_drvdata(dev);
165
166         return sprintf(buf, "%s\n", get_sw_type(sw));
167 }
168
169 static DEVICE_ATTR(type, S_IRUGO, gpio_sw_type_show, NULL);
170
171 static ssize_t gpio_sw_direction_show(struct device *dev,
172                                       struct device_attribute *attr,
173                                       char *buf)
174 {
175         struct gpio_switch *sw = dev_get_drvdata(dev);
176         int is_output;
177
178         is_output = sw->flags & OMAP_GPIO_SWITCH_FLAG_OUTPUT;
179         return sprintf(buf, "%s\n", is_output ? "output" : "input");
180 }
181
182 static DEVICE_ATTR(direction, S_IRUGO, gpio_sw_direction_show, NULL);
183
184
185 static irqreturn_t gpio_sw_irq_handler(int irq, void *arg)
186 {
187         struct gpio_switch *sw = arg;
188         unsigned long timeout;
189         int state;
190
191         if (!sw->both_edges) {
192                 if (omap_get_gpio_datain(sw->gpio))
193                         set_irq_type(OMAP_GPIO_IRQ(sw->gpio), IRQT_FALLING);
194                 else
195                         set_irq_type(OMAP_GPIO_IRQ(sw->gpio), IRQT_RISING);
196         }
197
198         state = gpio_sw_get_state(sw);
199         if (sw->state == state)
200                 return IRQ_HANDLED;
201
202         if (state)
203                 timeout = sw->debounce_rising;
204         else
205                 timeout = sw->debounce_falling;
206         if (!timeout)
207                 schedule_work(&sw->work);
208         else
209                 mod_timer(&sw->timer, jiffies + msecs_to_jiffies(timeout));
210
211         return IRQ_HANDLED;
212 }
213
214 static void gpio_sw_timer(unsigned long arg)
215 {
216         struct gpio_switch *sw = (struct gpio_switch *) arg;
217
218         schedule_work(&sw->work);
219 }
220
221 static void gpio_sw_handler(struct work_struct *work)
222 {
223         struct gpio_switch *sw = container_of(work, struct gpio_switch, work);
224         int state;
225
226         state = gpio_sw_get_state(sw);
227         if (sw->state == state)
228                 return;
229
230         sw->state = state;
231         if (sw->notify != NULL)
232                 sw->notify(sw->notify_data, state);
233         sysfs_notify(&sw->pdev.dev.kobj, NULL, "state");
234         print_sw_state(sw, state);
235 }
236
237 static int __init can_do_both_edges(struct gpio_switch *sw)
238 {
239         if (!cpu_class_is_omap1())
240                 return 1;
241         if (OMAP_GPIO_IS_MPUIO(sw->gpio))
242                 return 0;
243         else
244                 return 1;
245 }
246
247 static void gpio_sw_release(struct device *dev)
248 {
249 }
250
251 static int __init new_switch(struct gpio_switch *sw)
252 {
253         int r, direction, trigger;
254
255         switch (sw->type) {
256         case OMAP_GPIO_SWITCH_TYPE_COVER:
257         case OMAP_GPIO_SWITCH_TYPE_CONNECTION:
258         case OMAP_GPIO_SWITCH_TYPE_ACTIVITY:
259                 break;
260         default:
261                 printk(KERN_ERR "invalid GPIO switch type: %d\n", sw->type);
262                 return -EINVAL;
263         }
264
265         sw->pdev.name   = sw->name;
266         sw->pdev.id     = -1;
267
268         sw->pdev.dev.parent = &gpio_sw_platform_dev->dev;
269         sw->pdev.dev.driver = &gpio_sw_driver.driver;
270         sw->pdev.dev.release = gpio_sw_release;
271
272         r = platform_device_register(&sw->pdev);
273         if (r) {
274                 printk(KERN_ERR "gpio-switch: platform device registration "
275                        "failed for %s", sw->name);
276                 return r;
277         }
278         dev_set_drvdata(&sw->pdev.dev, sw);
279
280         r = omap_request_gpio(sw->gpio);
281         if (r < 0) {
282                 platform_device_unregister(&sw->pdev);
283                 return r;
284         }
285
286         /* input: 1, output: 0 */
287         direction = !(sw->flags & OMAP_GPIO_SWITCH_FLAG_OUTPUT);
288         omap_set_gpio_direction(sw->gpio, direction);
289
290         sw->state = gpio_sw_get_state(sw);
291
292         r = 0;
293         r |= device_create_file(&sw->pdev.dev, &dev_attr_state);
294         r |= device_create_file(&sw->pdev.dev, &dev_attr_type);
295         r |= device_create_file(&sw->pdev.dev, &dev_attr_direction);
296         if (r)
297                 printk(KERN_ERR "gpio-switch: attribute file creation "
298                        "failed for %s\n", sw->name);
299
300         if (!direction)
301                 return 0;
302
303         if (can_do_both_edges(sw)) {
304                 trigger = IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING;
305                 sw->both_edges = 1;
306         } else {
307                 if (omap_get_gpio_datain(sw->gpio))
308                         trigger = IRQF_TRIGGER_FALLING;
309                 else
310                         trigger = IRQF_TRIGGER_RISING;
311         }
312         r = request_irq(OMAP_GPIO_IRQ(sw->gpio), gpio_sw_irq_handler,
313                         IRQF_SHARED | trigger, sw->name, sw);
314         if (r < 0) {
315                 printk(KERN_ERR "gpio-switch: request_irq() failed "
316                        "for GPIO %d\n", sw->gpio);
317                 platform_device_unregister(&sw->pdev);
318                 omap_free_gpio(sw->gpio);
319                 return r;
320         }
321
322         INIT_WORK(&sw->work, gpio_sw_handler);
323         init_timer(&sw->timer);
324
325         sw->timer.function = gpio_sw_timer;
326         sw->timer.data = (unsigned long)sw;
327
328         list_add(&sw->node, &gpio_switches);
329
330         return 0;
331 }
332
333 static int __init add_atag_switches(void)
334 {
335         const struct omap_gpio_switch_config *cfg;
336         struct gpio_switch *sw;
337         int i, r;
338
339         for (i = 0; ; i++) {
340                 cfg = omap_get_nr_config(OMAP_TAG_GPIO_SWITCH,
341                                          struct omap_gpio_switch_config, i);
342                 if (cfg == NULL)
343                         break;
344                 sw = kzalloc(sizeof(*sw), GFP_KERNEL);
345                 if (sw == NULL) {
346                         printk(KERN_ERR "gpio-switch: kmalloc failed\n");
347                         return -ENOMEM;
348                 }
349                 strncpy(sw->name, cfg->name, sizeof(cfg->name));
350                 sw->gpio = cfg->gpio;
351                 sw->flags = cfg->flags;
352                 sw->type = cfg->type;
353                 sw->debounce_rising = OMAP_GPIO_SW_DEFAULT_DEBOUNCE;
354                 sw->debounce_falling = OMAP_GPIO_SW_DEFAULT_DEBOUNCE;
355                 if ((r = new_switch(sw)) < 0) {
356                         kfree(sw);
357                         return r;
358                 }
359         }
360         return 0;
361 }
362
363 static struct gpio_switch * __init find_switch(int gpio, const char *name)
364 {
365         struct gpio_switch *sw;
366
367         list_for_each_entry(sw, &gpio_switches, node) {
368                 if ((gpio < 0 || sw->gpio != gpio) &&
369                     (name == NULL || strcmp(sw->name, name) != 0))
370                         continue;
371
372                 if (gpio < 0 || name == NULL)
373                         goto no_check;
374
375                 if (strcmp(sw->name, name) != 0)
376                         printk("gpio-switch: name mismatch for %d (%s, %s)\n",
377                                gpio, name, sw->name);
378                 else if (sw->gpio != gpio)
379                         printk("gpio-switch: GPIO mismatch for %s (%d, %d)\n",
380                                name, gpio, sw->gpio);
381 no_check:
382                 return sw;
383         }
384         return NULL;
385 }
386
387 static int __init add_board_switches(void)
388 {
389         int i;
390
391         for (i = 0; i < board_gpio_sw_count; i++) {
392                 const struct omap_gpio_switch *cfg;
393                 struct gpio_switch *sw;
394                 int r;
395
396                 cfg = board_gpio_sw_table + i;
397                 if (strlen(cfg->name) > sizeof(sw->name) - 1)
398                         return -EINVAL;
399                 /* Check whether we only update an existing switch
400                  * or add a new switch. */
401                 sw = find_switch(cfg->gpio, cfg->name);
402                 if (sw != NULL) {
403                         sw->debounce_rising = cfg->debounce_rising;
404                         sw->debounce_falling = cfg->debounce_falling;
405                         sw->notify = cfg->notify;
406                         sw->notify_data = cfg->notify_data;
407                         continue;
408                 } else {
409                         if (cfg->gpio < 0 || cfg->name == NULL) {
410                                 printk("gpio-switch: required switch not "
411                                        "found (%d, %s)\n", cfg->gpio,
412                                        cfg->name);
413                                 continue;
414                         }
415                 }
416                 sw = kzalloc(sizeof(*sw), GFP_KERNEL);
417                 if (sw == NULL) {
418                         printk(KERN_ERR "gpio-switch: kmalloc failed\n");
419                         return -ENOMEM;
420                 }
421                 strlcpy(sw->name, cfg->name, sizeof(sw->name));
422                 sw->gpio = cfg->gpio;
423                 sw->flags = cfg->flags;
424                 sw->type = cfg->type;
425                 sw->debounce_rising = cfg->debounce_rising;
426                 sw->debounce_falling = cfg->debounce_falling;
427                 sw->notify = cfg->notify;
428                 sw->notify_data = cfg->notify_data;
429                 if ((r = new_switch(sw)) < 0) {
430                         kfree(sw);
431                         return r;
432                 }
433         }
434         return 0;
435 }
436
437 static void gpio_sw_cleanup(void)
438 {
439         struct gpio_switch *sw = NULL, *old = NULL;
440
441         list_for_each_entry(sw, &gpio_switches, node) {
442                 if (old != NULL)
443                         kfree(old);
444                 flush_scheduled_work();
445                 del_timer_sync(&sw->timer);
446
447                 free_irq(OMAP_GPIO_IRQ(sw->gpio), sw);
448
449                 device_remove_file(&sw->pdev.dev, &dev_attr_state);
450                 device_remove_file(&sw->pdev.dev, &dev_attr_type);
451                 device_remove_file(&sw->pdev.dev, &dev_attr_direction);
452
453                 platform_device_unregister(&sw->pdev);
454                 omap_free_gpio(sw->gpio);
455                 old = sw;
456         }
457         kfree(old);
458 }
459
460 static void __init report_initial_state(void)
461 {
462         struct gpio_switch *sw;
463
464         list_for_each_entry(sw, &gpio_switches, node) {
465                 int state;
466
467                 state = omap_get_gpio_datain(sw->gpio);
468                 if (sw->flags & OMAP_GPIO_SWITCH_FLAG_INVERTED)
469                         state = !state;
470                 if (sw->notify != NULL)
471                         sw->notify(sw->notify_data, state);
472                 print_sw_state(sw, state);
473         }
474 }
475
476 static int gpio_sw_remove(struct platform_device *dev)
477 {
478         return 0;
479 }
480
481 static struct platform_driver gpio_sw_driver = {
482         .remove         = gpio_sw_remove,
483         .driver         = {
484                 .name   = "gpio-switch",
485         },
486 };
487
488 void __init omap_register_gpio_switches(const struct omap_gpio_switch *tbl,
489                                         int count)
490 {
491         BUG_ON(board_gpio_sw_table != NULL);
492
493         board_gpio_sw_table = tbl;
494         board_gpio_sw_count = count;
495 }
496
497 static int __init gpio_sw_init(void)
498 {
499         int r;
500
501         printk(KERN_INFO "OMAP GPIO switch handler initializing\n");
502
503         r = platform_driver_register(&gpio_sw_driver);
504         if (r)
505                 return r;
506
507         gpio_sw_platform_dev = platform_device_register_simple("gpio-switch",
508                                                                -1, NULL, 0);
509         if (IS_ERR(gpio_sw_platform_dev)) {
510                 r = PTR_ERR(gpio_sw_platform_dev);
511                 goto err1;
512         }
513
514         r = add_atag_switches();
515         if (r < 0)
516                 goto err2;
517
518         r = add_board_switches();
519         if (r < 0)
520                 goto err2;
521
522         report_initial_state();
523
524         return 0;
525 err2:
526         gpio_sw_cleanup();
527         platform_device_unregister(gpio_sw_platform_dev);
528 err1:
529         platform_driver_unregister(&gpio_sw_driver);
530         return r;
531 }
532
533 static void __exit gpio_sw_exit(void)
534 {
535         gpio_sw_cleanup();
536         platform_device_unregister(gpio_sw_platform_dev);
537         platform_driver_unregister(&gpio_sw_driver);
538 }
539
540 #ifndef MODULE
541 late_initcall(gpio_sw_init);
542 #else
543 module_init(gpio_sw_init);
544 #endif
545 module_exit(gpio_sw_exit);
546
547 MODULE_AUTHOR("Juha Yrjölä <juha.yrjola@nokia.com>, Paul Mundt <paul.mundt@nokia.com");
548 MODULE_DESCRIPTION("GPIO switch driver");
549 MODULE_LICENSE("GPL");