aboutsummaryrefslogtreecommitdiff
path: root/drivers/misc/mediatek/connectivity/common/common_detect/wmt_detect.c
blob: 487852df8f20fea8e299e4883690d2ba6ebab4a5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*
* Copyright (C) 2011-2014 MediaTek Inc.
*
* This program is free software: you can redistribute it and/or modify it under the terms of the
* GNU General Public License version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program.
* If not, see <http://www.gnu.org/licenses/>.
*/

#include <mtk_wcn_cmb_stub.h>
#include <linux/platform_device.h>

#ifdef DFT_TAG
#undef DFT_TAG
#endif
#define DFT_TAG         "[WMT-DETECT]"

#include "wmt_detect.h"
#include "wmt_gpio.h"

#if MTK_WCN_REMOVE_KO
#include "conn_drv_init.h"
#endif
#ifdef CONFIG_COMPAT
#include <linux/compat.h>
#endif

#define WMT_DETECT_MAJOR 154
#define WMT_DETECT_DEV_NUM 1
#define WMT_DETECT_DRVIER_NAME "mtk_wcn_detect"
#define WMT_DETECT_DEVICE_NAME "wmtdetect"

struct class *pDetectClass = NULL;
struct device *pDetectDev = NULL;
static int gWmtDetectMajor = WMT_DETECT_MAJOR;
static struct cdev gWmtDetectCdev;
unsigned int gWmtDetectDbgLvl = WMT_DETECT_LOG_INFO;


#ifdef MTK_WCN_COMBO_CHIP_SUPPORT
inline unsigned int wmt_plat_get_soc_chipid(void)
{
	WMT_DETECT_INFO_FUNC("no soc chip supported, due to MTK_WCN_SOC_CHIP_SUPPORT is not set.\n");
	return -1;
}
#endif

static int wmt_detect_open(struct inode *inode, struct file *file)
{
	WMT_DETECT_INFO_FUNC("open major %d minor %d (pid %d)\n", imajor(inode), iminor(inode), current->pid);

	return 0;
}

static int wmt_detect_close(struct inode *inode, struct file *file)
{
	WMT_DETECT_INFO_FUNC("close major %d minor %d (pid %d)\n", imajor(inode), iminor(inode), current->pid);

	return 0;
}

static ssize_t wmt_detect_read(struct file *filp, char __user *buf, size_t count, loff_t *f_pos)
{
	WMT_DETECT_INFO_FUNC(" ++\n");
	WMT_DETECT_INFO_FUNC(" --\n");

	return 0;
}

ssize_t wmt_detect_write(struct file *filp, const char __user *buf, size_t count, loff_t *f_pos)
{
	WMT_DETECT_INFO_FUNC(" ++\n");
	WMT_DETECT_INFO_FUNC(" --\n");

	return 0;
}

static long wmt_detect_unlocked_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
	int retval = 0;

	WMT_DETECT_INFO_FUNC("cmd (%d),arg(%ld)\n", cmd, arg);

	switch (cmd) {
	case COMBO_IOCTL_GET_CHIP_ID:
		/*just get chipid from sdio-detect module */
		/*check if external combo chip exists or not */
		/*if yes, just return combo chip id */
		/*if no, get soc chipid */
		retval = mtk_wcn_wmt_chipid_query();
		break;

	case COMBO_IOCTL_SET_CHIP_ID:
		mtk_wcn_wmt_set_chipid(arg);

		break;

	case COMBO_IOCTL_EXT_CHIP_PWR_ON:
		retval = wmt_detect_ext_chip_pwr_on();
		break;

	case COMBO_IOCTL_EXT_CHIP_DETECT:
		retval = wmt_detect_ext_chip_detect();
		break;

	case COMBO_IOCTL_EXT_CHIP_PWR_OFF:
		retval = wmt_detect_ext_chip_pwr_off();
		break;

	case COMBO_IOCTL_DO_SDIO_AUDOK:
		retval = sdio_detect_do_autok(arg);
		break;

	case COMBO_IOCTL_GET_SOC_CHIP_ID:
		retval = wmt_plat_get_soc_chipid();
		/*get soc chipid by HAL interface */
		break;

	case COMBO_IOCTL_MODULE_CLEANUP:
#if (MTK_WCN_REMOVE_KO)
		/*deinit SDIO-DETECT module */
		retval = sdio_detect_exit();
#else
		WMT_DETECT_INFO_FUNC("no MTK_WCN_REMOVE_KO defined\n");
#endif
		break;

	case COMBO_IOCTL_DO_MODULE_INIT:
#if (MTK_WCN_REMOVE_KO)
		/*deinit SDIO-DETECT module */
		retval = do_connectivity_driver_init(arg);
#else
		WMT_DETECT_INFO_FUNC("no MTK_WCN_REMOVE_KO defined\n");
#endif
		break;

	default:
		WMT_DETECT_WARN_FUNC("unknown cmd (%d)\n", cmd);
		retval = 0;
		break;
	}
	return retval;
}
#ifdef CONFIG_COMPAT
static long WMT_compat_detect_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
	long ret;

	WMT_DETECT_INFO_FUNC("cmd (%d)\n", cmd);
	ret = wmt_detect_unlocked_ioctl(filp, cmd, arg);
	return ret;
}
#endif
const struct file_operations gWmtDetectFops = {
	.open = wmt_detect_open,
	.release = wmt_detect_close,
	.read = wmt_detect_read,
	.write = wmt_detect_write,
	.unlocked_ioctl = wmt_detect_unlocked_ioctl,
#ifdef CONFIG_COMPAT
	.compat_ioctl = WMT_compat_detect_ioctl,
#endif
};

int wmt_detect_ext_chip_pwr_on(void)
{
	/*pre power on external chip */
	/* wmt_plat_pwr_ctrl(FUNC_ON); */
#ifdef MTK_WCN_COMBO_CHIP_SUPPORT
	WMT_DETECT_INFO_FUNC("++\n");
	if (0 != wmt_detect_chip_pwr_ctrl(1))
		return -1;
	if (0 != wmt_detect_sdio_pwr_ctrl(1))
		return -2;
	return 0;
#else
	WMT_DETECT_INFO_FUNC("combo chip is not supported\n");
	return -1;
#endif
}

int wmt_detect_ext_chip_pwr_off(void)
{
	/*pre power off external chip */
	/* wmt_plat_pwr_ctrl(FUNC_OFF); */
#ifdef MTK_WCN_COMBO_CHIP_SUPPORT
	WMT_DETECT_INFO_FUNC("--\n");
	wmt_detect_sdio_pwr_ctrl(0);
	return wmt_detect_chip_pwr_ctrl(0);
#else
	WMT_DETECT_INFO_FUNC("combo chip is not supported\n");
	return 0;
#endif
}

int wmt_detect_ext_chip_detect(void)
{
	int iRet = -1;
	unsigned int chipId = -1;
	/*if there is no external combo chip, return -1 */
	int bgfEintStatus = -1;

	WMT_DETECT_INFO_FUNC("++\n");
	/*wait for a stable time */
	msleep(20);

	/*read BGF_EINT_PIN status */
	bgfEintStatus = wmt_detect_read_ext_cmb_status();

	if (0 == bgfEintStatus) {
		/*external chip does not exist */
		WMT_DETECT_INFO_FUNC("external combo chip not detected\n");
	} else if (1 == bgfEintStatus) {
		/*combo chip exists */
		WMT_DETECT_INFO_FUNC("external combo chip detected\n");

		/*detect chipid by sdio_detect module */
		chipId = sdio_detect_query_chipid(1);
		if (0 <= hif_sdio_is_chipid_valid(chipId))
			WMT_DETECT_INFO_FUNC("valid external combo chip id (0x%x)\n", chipId);
		else
			WMT_DETECT_INFO_FUNC("invalid external combo chip id (0x%x)\n", chipId);
		iRet = 0;
	} else {
		/*Error exists */
		WMT_DETECT_ERR_FUNC("error happens when detecting combo chip\n");
	}
	WMT_DETECT_INFO_FUNC("--\n");
	/*return 0 */
	return iRet;
	/*todo: if there is external combo chip, power on chip return 0 */
}

#ifdef MTK_WCN_COMBO_CHIP_SUPPORT
static int wmt_detect_probe(struct platform_device *pdev)
{
	int ret = 0;

	WMT_DETECT_ERR_FUNC("platform name: %s\n", pdev->name);
	ret = wmt_gpio_init(pdev);
	if (-1 == ret)
		WMT_DETECT_ERR_FUNC("gpio init fail ret:%d\n", ret);
	return ret;
}

static int wmt_detect_remove(struct platform_device *pdev)
{
	wmt_gpio_deinit();
	return 0;
}
#endif

#ifdef MTK_WCN_COMBO_CHIP_SUPPORT
static struct of_device_id wmt_detect_match[] = {
	{ .compatible = "mediatek,connectivity-combo", },
	{}
};
MODULE_DEVICE_TABLE(of, wmt_detect_match);

static struct platform_driver wmt_detect_driver = {
	.probe = wmt_detect_probe,
	.remove = wmt_detect_remove,
	.driver = {
		.owner = THIS_MODULE,
		.name = "mediatek,connectivity-combo",
		.of_match_table = wmt_detect_match,
	},
};
#endif

/*module_platform_driver(wmt_detect_driver);*/
static int wmt_detect_driver_init(void)
{
	dev_t devID = MKDEV(gWmtDetectMajor, 0);
	int cdevErr = -1;
	int ret = -1;

	ret = register_chrdev_region(devID, WMT_DETECT_DEV_NUM, WMT_DETECT_DRVIER_NAME);
	if (ret) {
		WMT_DETECT_ERR_FUNC("fail to register chrdev\n");
		return ret;
	}

	cdev_init(&gWmtDetectCdev, &gWmtDetectFops);
	gWmtDetectCdev.owner = THIS_MODULE;

	cdevErr = cdev_add(&gWmtDetectCdev, devID, WMT_DETECT_DEV_NUM);
	if (cdevErr) {
		WMT_DETECT_ERR_FUNC("cdev_add() fails (%d)\n", cdevErr);
		goto err1;
	}

	pDetectClass = class_create(THIS_MODULE, WMT_DETECT_DEVICE_NAME);
	if (IS_ERR(pDetectClass)) {
		WMT_DETECT_ERR_FUNC("class create fail, error code(%ld)\n", PTR_ERR(pDetectClass));
		goto err1;
	}

	pDetectDev = device_create(pDetectClass, NULL, devID, NULL, WMT_DETECT_DEVICE_NAME);
	if (IS_ERR(pDetectDev)) {
		WMT_DETECT_ERR_FUNC("device create fail, error code(%ld)\n", PTR_ERR(pDetectDev));
		goto err2;
	}

	WMT_DETECT_INFO_FUNC("driver(major %d) installed success\n", gWmtDetectMajor);

	/*init SDIO-DETECT module */
	sdio_detect_init();

#ifdef MTK_WCN_COMBO_CHIP_SUPPORT
	ret = platform_driver_register(&wmt_detect_driver);
	if (ret)
		WMT_DETECT_ERR_FUNC("platform driver register fail ret:%d\n", ret);
#endif

	return 0;

err2:

	if (pDetectClass) {
		class_destroy(pDetectClass);
		pDetectClass = NULL;
	}

err1:

	if (cdevErr == 0)
		cdev_del(&gWmtDetectCdev);

	if (ret == 0) {
		unregister_chrdev_region(devID, WMT_DETECT_DEV_NUM);
		gWmtDetectMajor = -1;
	}

	WMT_DETECT_ERR_FUNC("fail\n");

	return -1;
}

static void wmt_detect_driver_exit(void)
{
	dev_t dev = MKDEV(gWmtDetectMajor, 0);

	if (pDetectDev) {
		device_destroy(pDetectClass, dev);
		pDetectDev = NULL;
	}

	if (pDetectClass) {
		class_destroy(pDetectClass);
		pDetectClass = NULL;
	}

	cdev_del(&gWmtDetectCdev);
	unregister_chrdev_region(dev, WMT_DETECT_DEV_NUM);

#if !(MTK_WCN_REMOVE_KO)
/*deinit SDIO-DETECT module*/
	sdio_detect_exit();
#endif

#ifdef MTK_WCN_COMBO_CHIP_SUPPORT
	platform_driver_unregister(&wmt_detect_driver);
#endif

	WMT_DETECT_INFO_FUNC("done\n");
}

module_init(wmt_detect_driver_init);
module_exit(wmt_detect_driver_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Zhiguo.Niu & Chaozhong.Liang @ MBJ/WCNSE/SS1");

module_param(gWmtDetectMajor, uint, 0);