1 /*
2 * Copyright (c) 2015 South Silicon Valley Microelectronics Inc.
3 * Copyright (c) 2015 iComm Corporation
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * See the GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17 #include <linux/irq.h>
18 #include <linux/version.h>
19 #include <linux/module.h>
20 #include <linux/vmalloc.h>
21 #include <linux/platform_device.h>
22 #include <linux/gpio.h>
23 #include <linux/pm_runtime.h>
24 #include <linux/mmc/host.h>
25 #include <linux/delay.h>
26 #include <linux/semaphore.h>
27 #include <linux/regulator/consumer.h>
28 #include <asm/io.h>
29 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 0, 0))
30 #include <linux/printk.h>
31 #include <linux/err.h>
32 #else
33 #include <config/printk.h>
34 #endif
35 #if LINUX_VERSION_CODE > KERNEL_VERSION(3,2,0)
36 #include <linux/wlan_plat.h>
37 #else
38 struct wifi_platform_data {
39 int (*set_power)(int val);
40 int (*set_reset)(int val);
41 int (*set_carddetect)(int val);
42 void *(*mem_prealloc)(int section, unsigned long size);
43 int (*get_mac_addr)(unsigned char *buf);
44 void *(*get_country_code)(char *ccode);
45 };
46 #endif
47 #define GPIO_REG_WRITEL(val,reg) do{__raw_writel(val, CTL_PIN_BASE + (reg));}while(0)
48 static int g_wifidev_registered = 0;
49 static struct semaphore wifi_control_sem;
50 static struct wifi_platform_data *wifi_control_data = NULL;
51 static struct resource *wifi_irqres = NULL;
52 static int g_wifi_irq_rc=0;
53 #define SDIO_ID 2
54 #define IRQ_RES_NAME "ssv_wlan_irq"
55 #define WIFI_HOST_WAKE 0xFFFF
56 extern int ssv_6xxx_wlan_init(void);
57 extern int ssv_wlan_power_on(int flag);
58 extern int ssv_wlan_power_off(int flag);
wifi_pm_gpio_ctrl(char * name,int level)59 static int wifi_pm_gpio_ctrl(char* name, int level)
60 {
61 (void)name;
62 (void)level;
63 if (level)
64 {
65 ssv_wlan_power_on(1);
66 }
67 else
68 {
69 ssv_wlan_power_off(1);
70 }
71 return 0;
72 }
ssv_wifi_power(int on)73 static int ssv_wifi_power(int on)
74 {
75 printk("ssv pwr on=%d\n",on);
76 if(on)
77 {
78 wifi_pm_gpio_ctrl("wifi_ssv6200_power", 0);
79 mdelay(50);
80 wifi_pm_gpio_ctrl("wifi_ssv6200_power", 1);
81 }
82 else
83 {
84 wifi_pm_gpio_ctrl("wifi_ssv6200_power", 0);
85 }
86 return 0;
87 }
ssv_wifi_reset(int on)88 static int ssv_wifi_reset(int on)
89 {
90 return 0;
91 }
ssv_wifi_set_carddetect(int val)92 int ssv_wifi_set_carddetect(int val)
93 {
94 if (val)
95 {
96 ssv_wlan_power_on(1);
97 }
98 else
99 {
100 ssv_wlan_power_off(1);
101 }
102 return 0;
103 }
104 static struct wifi_platform_data ssv_wifi_control = {
105 .set_power = ssv_wifi_power,
106 .set_reset = ssv_wifi_reset,
107 .set_carddetect = ssv_wifi_set_carddetect,
108 };
109 static struct resource resources[] = {
110 {
111 .start = WIFI_HOST_WAKE,
112 .flags = IORESOURCE_IRQ,
113 .name = IRQ_RES_NAME,
114 },
115 };
ssv_wifi_device_release(struct device * dev)116 void ssv_wifi_device_release(struct device *dev)
117 {
118 printk(KERN_INFO "ssv_wifi_device_release\n");
119 }
120 static struct platform_device ssv_wifi_device = {
121 .name = "ssv_wlan",
122 .id = 1,
123 .num_resources = ARRAY_SIZE(resources),
124 .resource = resources,
125 .dev = {
126 .platform_data = &ssv_wifi_control,
127 .release = ssv_wifi_device_release,
128 },
129 };
wifi_set_power(int on,unsigned long msec)130 int wifi_set_power(int on, unsigned long msec)
131 {
132 if (wifi_control_data && wifi_control_data->set_power) {
133 wifi_control_data->set_power(on);
134 }
135 if (msec)
136 msleep(msec);
137 return 0;
138 }
wifi_set_reset(int on,unsigned long msec)139 int wifi_set_reset(int on, unsigned long msec)
140 {
141 if (wifi_control_data && wifi_control_data->set_reset) {
142 wifi_control_data->set_reset(on);
143 }
144 if (msec)
145 msleep(msec);
146 return 0;
147 }
wifi_set_carddetect(int on)148 static int wifi_set_carddetect(int on)
149 {
150 if (wifi_control_data && wifi_control_data->set_carddetect) {
151 wifi_control_data->set_carddetect(on);
152 }
153 return 0;
154 }
wifi_wakeup_irq_handler(int irq,void * dev)155 static irqreturn_t wifi_wakeup_irq_handler(int irq, void *dev){
156 printk("sdhci_wakeup_irq_handler\n");
157 disable_irq_nosync(irq);
158 return IRQ_HANDLED;
159 }
setup_wifi_wakeup_BB(struct platform_device * pdev,bool bEnable)160 void setup_wifi_wakeup_BB(struct platform_device *pdev, bool bEnable)
161 {
162 int rc=0,ret=0;
163 if (bEnable){
164 wifi_irqres = platform_get_resource_byname(pdev, IORESOURCE_IRQ, IRQ_RES_NAME);
165 rc = (int)wifi_irqres->start;
166 g_wifi_irq_rc = rc;
167 ret = request_threaded_irq(rc,
168 NULL,
169 (void *)wifi_wakeup_irq_handler,
170 #if LINUX_VERSION_CODE > KERNEL_VERSION(3,0,0)
171 IRQ_TYPE_LEVEL_HIGH | IRQF_ONESHOT |IRQF_FORCE_RESUME,
172 #else
173 IRQ_TYPE_LEVEL_HIGH | IRQF_ONESHOT,
174 #endif
175 "wlan_wakeup_irq", NULL);
176 enable_irq_wake(g_wifi_irq_rc);
177 }else{
178 if(g_wifi_irq_rc){
179 free_irq(g_wifi_irq_rc,NULL);
180 g_wifi_irq_rc = 0;
181 }
182 }
183 }
wifi_probe(struct platform_device * pdev)184 static int wifi_probe(struct platform_device *pdev)
185 {
186 struct wifi_platform_data *wifi_ctrl =
187 (struct wifi_platform_data *)(pdev->dev.platform_data);
188 printk(KERN_ALERT "wifi_probe\n");
189 wifi_control_data = wifi_ctrl;
190 #ifndef CONFIG_SSV6XXX
191 ssv_6xxx_wlan_init();
192 #endif
193 wifi_set_carddetect(1);
194 up(&wifi_control_sem);
195 return 0;
196 }
wifi_remove(struct platform_device * pdev)197 static int wifi_remove(struct platform_device *pdev)
198 {
199 struct wifi_platform_data *wifi_ctrl =
200 (struct wifi_platform_data *)(pdev->dev.platform_data);
201 wifi_control_data = wifi_ctrl;
202 wifi_set_power(0, 0);
203 wifi_set_carddetect(0);
204 setup_wifi_wakeup_BB(pdev,false);
205 return 0;
206 }
wifi_suspend(struct platform_device * pdev,pm_message_t state)207 static int wifi_suspend(struct platform_device *pdev, pm_message_t state)
208 {
209 setup_wifi_wakeup_BB(pdev,true);
210 return 0;
211 }
wifi_resume(struct platform_device * pdev)212 static int wifi_resume(struct platform_device *pdev)
213 {
214 setup_wifi_wakeup_BB(pdev,false);
215 return 0;
216 }
217 static struct platform_driver wifi_driver = {
218 .probe = wifi_probe,
219 .remove = wifi_remove,
220 .suspend = wifi_suspend,
221 .resume = wifi_resume,
222 .driver = {
223 .name = "ssv_wlan",
224 }
225 };
226 extern int ssvdevice_init(void);
227 extern void ssvdevice_exit(void);
228 #ifdef CONFIG_SSV_SUPPORT_AES_ASM
229 extern int aes_init(void);
230 extern void aes_fini(void);
231 extern int sha1_mod_init(void);
232 extern void sha1_mod_fini(void);
233 #endif
initWlan(void)234 int initWlan(void)
235 {
236 int ret=0;
237 sema_init(&wifi_control_sem, 0);
238 #ifdef CONFIG_SSV_SUPPORT_AES_ASM
239 sha1_mod_init();
240 aes_init();
241 #endif
242 platform_device_register(&ssv_wifi_device);
243 platform_driver_register(&wifi_driver);
244 g_wifidev_registered = 1;
245 if (down_timeout(&wifi_control_sem, msecs_to_jiffies(1000)) != 0) {
246 ret = -EINVAL;
247 printk(KERN_ALERT "%s: platform_driver_register timeout\n", __FUNCTION__);
248 }
249 ret = ssvdevice_init();
250 return ret;
251 }
exitWlan(void)252 void exitWlan(void)
253 {
254 if (g_wifidev_registered)
255 {
256 ssvdevice_exit();
257 #ifdef CONFIG_SSV_SUPPORT_AES_ASM
258 aes_fini();
259 sha1_mod_fini();
260 #endif
261 platform_driver_unregister(&wifi_driver);
262 platform_device_unregister(&ssv_wifi_device);
263 g_wifidev_registered = 0;
264 }
265 return;
266 }
generic_wifi_init_module(void)267 static int generic_wifi_init_module(void)
268 {
269 return initWlan();
270 }
generic_wifi_exit_module(void)271 static void generic_wifi_exit_module(void)
272 {
273 exitWlan();
274 }
275 EXPORT_SYMBOL(generic_wifi_init_module);
276 EXPORT_SYMBOL(generic_wifi_exit_module);
277 #ifdef CONFIG_SSV6XXX
278 late_initcall(generic_wifi_init_module);
279 #else
280 module_init(generic_wifi_init_module);
281 #endif
282 module_exit(generic_wifi_exit_module);
283 MODULE_LICENSE("Dual BSD/GPL");
284