import PULS_20160108
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / misc / mediatek / connectivity / common_detect / wmt_detect.c
1 /*
2 * Copyright (C) 2011-2014 MediaTek Inc.
3 *
4 * This program is free software: you can redistribute it and/or modify it under the terms of the
5 * GNU General Public License version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
8 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
9 * See the GNU General Public License for more details.
10 *
11 * You should have received a copy of the GNU General Public License along with this program.
12 * If not, see <http://www.gnu.org/licenses/>.
13 */
14
15 #include <mach/mtk_wcn_cmb_stub.h>
16
17
18
19 #ifdef DFT_TAG
20 #undef DFT_TAG
21 #endif
22 #define DFT_TAG "[WMT-DETECT]"
23
24 #include "wmt_detect.h"
25
26 #if MTK_WCN_REMOVE_KO
27 #include "conn_drv_init.h"
28 #endif
29
30
31
32 #define WMT_DETECT_MAJOR 154
33 #define WMT_DETECT_DEV_NUM 1
34 #define WMT_DETECT_DRVIER_NAME "mtk_wcn_detect"
35 #define WMT_DETECT_DEVICE_NAME "wmtdetect"
36
37 struct class *pDetectClass = NULL;
38 struct device *pDetectDev = NULL;
39 static int gWmtDetectMajor = WMT_DETECT_MAJOR;
40 static struct cdev gWmtDetectCdev;
41 unsigned int gWmtDetectDbgLvl = WMT_DETECT_LOG_INFO;
42
43 #ifdef MTK_WCN_SOC_CHIP_SUPPORT
44 extern unsigned int wmt_plat_get_soc_chipid(void);
45 #else
46 extern unsigned int wmt_plat_get_soc_chipid(void)
47 {
48 WMT_DETECT_INFO_FUNC("no soc chip supported, due to MTK_WCN_SOC_CHIP_SUPPORT is not set.\n");
49 return -1;
50 }
51 #endif
52
53 static int wmt_detect_open(struct inode *inode, struct file *file)
54 {
55 WMT_DETECT_INFO_FUNC("open major %d minor %d (pid %d)\n",
56 imajor(inode),
57 iminor(inode),
58 current->pid
59 );
60
61 return 0;
62 }
63
64 static int wmt_detect_close(struct inode *inode, struct file *file)
65 {
66 WMT_DETECT_INFO_FUNC("close major %d minor %d (pid %d)\n",
67 imajor(inode),
68 iminor(inode),
69 current->pid
70 );
71
72
73 return 0;
74 }
75
76 static ssize_t wmt_detect_read(struct file *filp, char __user *buf, size_t count, loff_t *f_pos)
77
78 {
79 WMT_DETECT_INFO_FUNC(" ++\n");
80 WMT_DETECT_INFO_FUNC(" --\n");
81
82 return 0;
83 }
84
85 ssize_t
86 wmt_detect_write (
87 struct file *filp,
88 const char __user *buf,
89 size_t count,
90 loff_t *f_pos
91 )
92 {
93 WMT_DETECT_INFO_FUNC(" ++\n");
94 WMT_DETECT_INFO_FUNC(" --\n");
95
96 return 0;
97 }
98
99 static long wmt_detect_unlocked_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
100 {
101 int retval = 0;
102
103 WMT_DETECT_INFO_FUNC("cmd (%d),arg(%ld)\n", cmd,arg);
104
105 switch(cmd){
106 case COMBO_IOCTL_GET_CHIP_ID:
107 /*just get chipid from sdio-detect module*/
108 /*check if external combo chip exists or not*/
109 /*if yes, just return combo chip id*/
110 /*if no, get soc chipid*/
111 retval = mtk_wcn_wmt_chipid_query();
112 break;
113
114 case COMBO_IOCTL_SET_CHIP_ID:
115 mtk_wcn_wmt_set_chipid(arg);
116
117 break;
118
119 case COMBO_IOCTL_EXT_CHIP_PWR_ON:
120 retval = wmt_detect_ext_chip_pwr_on();
121 break;
122
123 case COMBO_IOCTL_EXT_CHIP_DETECT:
124 retval = wmt_detect_ext_chip_detect();
125 break;
126
127 case COMBO_IOCTL_EXT_CHIP_PWR_OFF:
128 retval = wmt_detect_ext_chip_pwr_off();
129 break;
130
131 case COMBO_IOCTL_DO_SDIO_AUDOK:
132 retval = sdio_detect_do_autok(arg);
133 break;
134
135 case COMBO_IOCTL_GET_SOC_CHIP_ID:
136 retval = wmt_plat_get_soc_chipid();
137 /*get soc chipid by HAL interface*/
138 break;
139
140 case COMBO_IOCTL_MODULE_CLEANUP:
141 #if (MTK_WCN_REMOVE_KO)
142 /*deinit SDIO-DETECT module*/
143 retval = sdio_detect_exit();
144 #else
145 WMT_DETECT_INFO_FUNC("no MTK_WCN_REMOVE_KO defined\n");
146 #endif
147 break;
148
149 case COMBO_IOCTL_DO_MODULE_INIT:
150 #if (MTK_WCN_REMOVE_KO)
151 /*deinit SDIO-DETECT module*/
152 retval = do_connectivity_driver_init(arg);
153 #else
154 WMT_DETECT_INFO_FUNC("no MTK_WCN_REMOVE_KO defined\n");
155 #endif
156 break;
157
158 default:
159 WMT_DETECT_WARN_FUNC("unknown cmd (%d)\n", cmd);
160 retval = 0;
161 break;
162 }
163 return retval;
164 }
165
166 struct file_operations gWmtDetectFops = {
167 .open = wmt_detect_open,
168 .release = wmt_detect_close,
169 .read = wmt_detect_read,
170 .write = wmt_detect_write,
171 .unlocked_ioctl = wmt_detect_unlocked_ioctl,
172 };
173
174 int wmt_detect_ext_chip_pwr_on(void)
175 {
176 /*pre power on external chip*/
177 //wmt_plat_pwr_ctrl(FUNC_ON);
178 WMT_DETECT_INFO_FUNC("++\n");
179 if (0 != wmt_detect_chip_pwr_ctrl(1))
180 {
181 return -1;
182 }
183 if (0 != wmt_detect_sdio_pwr_ctrl(1))
184 {
185 return -2;
186 }
187 return 0;
188 }
189
190 int wmt_detect_ext_chip_pwr_off(void)
191 {
192 /*pre power off external chip*/
193 //wmt_plat_pwr_ctrl(FUNC_OFF);
194 WMT_DETECT_INFO_FUNC("--\n");
195 wmt_detect_sdio_pwr_ctrl(0);
196 return wmt_detect_chip_pwr_ctrl(0);
197 }
198
199 int wmt_detect_ext_chip_detect(void)
200 {
201 int iRet = -1;
202 unsigned int chipId = -1;
203 /*if there is no external combo chip, return -1*/
204 int bgfEintStatus = -1;
205 WMT_DETECT_INFO_FUNC("++\n");
206 /*wait for a stable time*/
207 msleep (10);
208
209 /*read BGF_EINT_PIN status*/
210 bgfEintStatus = wmt_detect_read_ext_cmb_status();;
211
212 if (0 == bgfEintStatus)
213 {
214 /*external chip does not exist*/
215 WMT_DETECT_INFO_FUNC("external combo chip not detected\n");
216 }
217 else if (1 == bgfEintStatus)
218 {
219 /*combo chip exists*/
220 WMT_DETECT_INFO_FUNC("external combo chip detected\n");
221
222 /*detect chipid by sdio_detect module*/
223 chipId = sdio_detect_query_chipid(1);
224 if (0 <= hif_sdio_is_chipid_valid(chipId))
225 {
226 WMT_DETECT_INFO_FUNC("valid external combo chip id (0x%x)\n", chipId);
227 }
228 else
229 {
230 WMT_DETECT_INFO_FUNC("invalid external combo chip id (0x%x)\n", chipId);
231 }
232 iRet = 0;
233 }
234 else
235 {
236 /*Error exists*/
237 WMT_DETECT_ERR_FUNC("error happens when detecting combo chip\n");
238 }
239 WMT_DETECT_INFO_FUNC("--\n");
240 /*return 0*/
241 return iRet;
242
243 /*todo: if there is external combo chip, power on chip return 0*/
244 }
245 static int wmt_detect_init(void)
246 {
247 dev_t devID = MKDEV(gWmtDetectMajor, 0);
248 int cdevErr = -1;
249 int ret = -1;
250
251 ret = register_chrdev_region(devID, WMT_DETECT_DEV_NUM, WMT_DETECT_DRVIER_NAME);
252 if (ret) {
253 WMT_DETECT_ERR_FUNC("fail to register chrdev\n");
254 return ret;
255 }
256
257 cdev_init(&gWmtDetectCdev, &gWmtDetectFops);
258 gWmtDetectCdev.owner = THIS_MODULE;
259
260 cdevErr = cdev_add(&gWmtDetectCdev, devID, WMT_DETECT_DEV_NUM);
261 if (cdevErr) {
262 WMT_DETECT_ERR_FUNC("cdev_add() fails (%d) \n", cdevErr);
263 goto err1;
264 }
265
266 pDetectClass = class_create(THIS_MODULE, WMT_DETECT_DEVICE_NAME);
267 if(IS_ERR(pDetectClass))
268 {
269 WMT_DETECT_ERR_FUNC("class create fail, error code(%ld)\n",PTR_ERR(pDetectClass));
270 goto err1;
271 }
272
273 pDetectDev = device_create(pDetectClass,NULL,devID,NULL,WMT_DETECT_DEVICE_NAME);
274 if(IS_ERR(pDetectDev))
275 {
276 WMT_DETECT_ERR_FUNC("device create fail, error code(%ld)\n",PTR_ERR(pDetectDev));
277 goto err2;
278 }
279
280 WMT_DETECT_INFO_FUNC("driver(major %d) installed success\n", gWmtDetectMajor);
281
282 /*init SDIO-DETECT module*/
283 sdio_detect_init();
284
285 return 0;
286
287 err2:
288
289 if(pDetectClass)
290 {
291 class_destroy(pDetectClass);
292 pDetectClass = NULL;
293 }
294
295 err1:
296
297 if (cdevErr == 0) {
298 cdev_del(&gWmtDetectCdev);
299 }
300
301 if (ret == 0) {
302 unregister_chrdev_region(devID, WMT_DETECT_DEV_NUM);
303 gWmtDetectMajor = -1;
304 }
305
306 WMT_DETECT_ERR_FUNC("fail \n");
307
308 return -1;
309 }
310
311 static void wmt_detect_exit (void)
312 {
313 dev_t dev = MKDEV(gWmtDetectMajor, 0);
314
315 if(pDetectDev)
316 {
317 device_destroy(pDetectClass, dev);
318 pDetectDev = NULL;
319 }
320
321 if(pDetectClass)
322 {
323 class_destroy(pDetectClass);
324 pDetectClass = NULL;
325 }
326
327 cdev_del(&gWmtDetectCdev);
328 unregister_chrdev_region(dev, WMT_DETECT_DEV_NUM);
329
330 #if !(MTK_WCN_REMOVE_KO)
331 /*deinit SDIO-DETECT module*/
332 sdio_detect_exit();
333 #endif
334
335 WMT_DETECT_INFO_FUNC("done\n");
336 }
337
338 module_init(wmt_detect_init);
339 module_exit(wmt_detect_exit);
340
341 MODULE_LICENSE("GPL");
342 MODULE_AUTHOR("Zhiguo.Niu & Chaozhong.Liang @ MBJ/WCNSE/SS1");
343
344 module_param(gWmtDetectMajor, uint, 0);