gta02: Add wlan power management device
[kernel.git] / arch / arm / mach-s3c2440 / gta02-pm-wlan.c
1 /*
2  * GTA02 WLAN power management
3  *
4  * (C) 2008, 2009 by Openmoko Inc.
5  * Author: Andy Green <andy@openmoko.com>
6  * All rights reserved.
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
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/kernel.h>
17 #include <linux/mutex.h>
18 #include <linux/platform_device.h>
19
20 #include <mach/hardware.h>
21 #include <asm/mach-types.h>
22
23 #include <mach/gta02.h>
24 #include <mach/gta02-pm-wlan.h>
25 #include <mach/regs-gpio.h>
26 #include <mach/regs-gpioj.h>
27 #include <mach/gpio-fns.h>
28
29 #include <linux/delay.h>
30 #include <linux/rfkill.h>
31
32
33 /* ----- Module hardware reset ("power") ----------------------------------- */
34
35
36 void gta02_wlan_reset(int assert_reset)
37 {
38         if (assert_reset) {
39                 s3c2410_gpio_setpin(GTA02_GPIO_nWLAN_RESET, 0);
40                 msleep(200); /* probably excessive but we don't have specs */
41         } else {
42                 s3c2410_gpio_setpin(GTA02_GPIO_nWLAN_RESET, 1);
43         }
44 }
45
46 /* ----- rfkill ------------------------------------------------------------ */
47
48 /*
49  * S3C MCI handles suspend/resume through device removal/insertion. In order to
50  * preserve rfkill state, as required in clause 7 of section 3.1 in rfkill.txt,
51  * we therefore need to maintain rfkill state outside the driver.
52  *
53  * This platform driver is as good a place as any other.
54  */
55
56 static int (*gta02_wlan_rfkill_cb)(void *user, int on);
57 static void *gta02_wlan_rfkill_user;
58 static DEFINE_MUTEX(gta02_wlan_rfkill_lock);
59 static int gta02_wlan_rfkill_on;
60
61 /*
62  * gta02_wlan_query_rfkill_lock is used to obtain the rfkill state before the
63  * driver is ready to process rfkill callbacks. To prevent the state from
64  * changing until the driver has completed its initialization, we grab and hold
65  * the rfkill lock.
66  *
67  * A call to gta02_wlan_query_rfkill_lock must be followed by either
68  * - a call to gta02_wlan_set_rfkill_cb, to complete the setup, or
69  * - a call to gta02_wlan_query_rfkill_unlock to abort the setup process.
70  */
71
72 int gta02_wlan_query_rfkill_lock(void)
73 {
74         mutex_lock(&gta02_wlan_rfkill_lock);
75         return gta02_wlan_rfkill_on;
76 }
77 EXPORT_SYMBOL_GPL(gta02_wlan_query_rfkill_lock);
78
79 void gta02_wlan_query_rfkill_unlock(void)
80 {
81         mutex_unlock(&gta02_wlan_rfkill_lock);
82 }
83 EXPORT_SYMBOL_GPL(gta02_wlan_query_rfkill_unlock);
84
85 void gta02_wlan_set_rfkill_cb(int (*cb)(void *user, int on), void *user)
86 {
87         BUG_ON(!mutex_is_locked(&gta02_wlan_rfkill_lock));
88         BUG_ON(gta02_wlan_rfkill_cb);
89         gta02_wlan_rfkill_cb = cb;
90         gta02_wlan_rfkill_user = user;
91         mutex_unlock(&gta02_wlan_rfkill_lock);
92 }
93 EXPORT_SYMBOL_GPL(gta02_wlan_set_rfkill_cb);
94
95 void gta02_wlan_clear_rfkill_cb(void)
96 {
97         mutex_lock(&gta02_wlan_rfkill_lock);
98         BUG_ON(!gta02_wlan_rfkill_cb);
99         gta02_wlan_rfkill_cb = NULL;
100         mutex_unlock(&gta02_wlan_rfkill_lock);
101 }
102 EXPORT_SYMBOL_GPL(gta02_wlan_clear_rfkill_cb);
103
104 static int gta02_wlan_set_radio_block(void *data, bool blocked)
105 {
106         struct device *dev = data;
107         int res = 0;
108
109         dev_dbg(dev, "gta02_wlan_toggle_radio: blocked %d (%p)\n",
110             blocked, gta02_wlan_rfkill_cb);
111         mutex_lock(&gta02_wlan_rfkill_lock);
112         if (gta02_wlan_rfkill_cb)
113                 res = gta02_wlan_rfkill_cb(gta02_wlan_rfkill_user, !blocked);
114         if (!res)
115                 gta02_wlan_rfkill_on = !blocked;
116         mutex_unlock(&gta02_wlan_rfkill_lock);
117         return res;
118 }
119
120 static const struct rfkill_ops gta02_wlan_rfkill_ops = {
121        .set_block = gta02_wlan_set_radio_block,
122 };
123
124 /* ----- Initialization/removal -------------------------------------------- */
125
126
127 static int __init gta02_wlan_probe(struct platform_device *pdev)
128 {
129         /* default-on for now */
130         const int default_state = 1;
131         struct rfkill *rfkill;
132         int ret;
133
134         dev_info(&pdev->dev, "starting\n");
135
136         s3c2410_gpio_cfgpin(GTA02_GPIO_nWLAN_RESET, S3C2410_GPIO_OUTPUT);
137         gta02_wlan_reset(1);
138         gta02_wlan_reset(0);
139
140         rfkill = rfkill_alloc("ar6000", &pdev->dev, RFKILL_TYPE_WLAN,
141                                                         &gta02_wlan_rfkill_ops, &pdev->dev);
142
143
144         if (!rfkill) {
145                 dev_err(&pdev->dev, "Failed to allocate rfkill\n");
146                 return -ENOMEM;
147         }
148
149         rfkill_init_sw_state(rfkill, default_state);
150         /*
151          * If the WLAN driver somehow managed to get activated before we're
152          * ready, the driver is now in an unknown state, which isn't something
153          * we're prepared to handle. This can't happen, so just fail hard.
154          */
155         BUG_ON(gta02_wlan_rfkill_cb);
156         gta02_wlan_rfkill_on = default_state;
157
158         ret = rfkill_register(rfkill);
159         if (ret) {
160                 rfkill_destroy(rfkill);
161                 dev_err(&pdev->dev, "Failed to register rfkill\n");
162                 return ret;
163         }
164
165         dev_set_drvdata(&pdev->dev, rfkill);
166
167         return 0;
168 }
169
170 static int gta02_wlan_remove(struct platform_device *pdev)
171 {
172         struct rfkill *rfkill = dev_get_drvdata(&pdev->dev);
173
174         rfkill_destroy(rfkill);
175
176         return 0;
177 }
178
179 static struct platform_driver gta02_wlan_driver = {
180         .probe          = gta02_wlan_probe,
181         .remove         = gta02_wlan_remove,
182         .driver         = {
183                 .name           = "gta02-pm-wlan",
184         },
185 };
186
187 static int __devinit gta02_wlan_init(void)
188 {
189         return platform_driver_register(&gta02_wlan_driver);
190 }
191 module_init(gta02_wlan_init);
192
193 static void gta02_wlan_exit(void)
194 {
195         platform_driver_unregister(&gta02_wlan_driver);
196 }
197 module_exit(gta02_wlan_exit);
198
199 MODULE_LICENSE("GPL");
200 MODULE_AUTHOR("Andy Green <andy@openmoko.com>");
201 MODULE_DESCRIPTION("Openmoko GTA02 WLAN power management");