summaryrefslogtreecommitdiff
path: root/drivers/misc/sprd_2351/sprd_2351.c
blob: 806c7f7aa8182950ede4f0bd794d41c1ba807bd6 (plain)
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/clk.h>
#include <linux/err.h>
#include <soc/sprd/hardware.h>
#include <linux/sprd_2351.h>
#include <asm/io.h>
#include <soc/sprd/sci.h>
#include <soc/sprd/sci_glb_regs.h>
#ifdef CONFIG_OF
#include <linux/device.h>
#include <linux/platform_device.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_irq.h>
#endif

#include <linux/gpio.h>
#include <linux/mutex.h>
#include <linux/regulator/consumer.h>
#include <soc/sprd/regulator.h>
#include <linux/sipc.h>
static struct sprd_2351_data rfspi;

static int s_gpio_ctrl_num = 0;
static bool s_is_gpio_register = false;
static bool s_is_vddwpa_register = false;
#define GPIO_RF2351_POWER_CTRL s_gpio_ctrl_num

void sprd_sr2351_gpio_ctrl_power_register(int gpio_num)
{
    s_gpio_ctrl_num  = gpio_num;
    s_is_gpio_register = true;
    return;
}

static int sprd_rfspi_wait_write_idle(void)
{
	u32 time_out;
	u32 reg_data;

	for (time_out = 0, reg_data = 0x10; \
		reg_data & BIT_4; time_out++) {/*wait fifo empty*/
		if (time_out > 1000) {
			RF2351_PRINT("mspi time out!\r\n");
			return -1;
		}
		reg_data = sci_glb_read(RFSPI_CFG0,-1UL);
		//RF2351_PRINT("reg_data is: %x\n", reg_data);
	}

	return 0;
}

static unsigned int sprd_rfspi_write(u16 Addr, u16 Data)
{
	u32 reg_data = 0;
	if (sprd_rfspi_wait_write_idle() == -1) {
		RF2351_PRINT("Error: mspi is busy\n");
		return -1;
	}
	reg_data = (Addr << 16) | Data;
	sci_glb_write(RFSPI_MCU_WCMD, reg_data, -1UL);

	sprd_rfspi_wait_write_idle();
	return 0;
}

static int sprd_rfspi_wait_read_idle(void)
{
	u32 time_out;
	u32 reg_data;

	for (time_out = 0, reg_data = 0; \
		!(reg_data & BIT_7); time_out++) {/*wait fifo empty*/
		if (time_out > 1000) {
			RF2351_PRINT("mspi time out!\r\n");
			return -1;
		}
		reg_data = sci_glb_read(RFSPI_CFG0,-1UL);
		//RF2351_PRINT("reg_data is: %x\n", reg_data);
	}
	
	return 0;
}

static unsigned int sprd_rfspi_read(u16 Addr, u32 *Read_data)
{
	u32 reg_data = 0;
	if (sprd_rfspi_wait_read_idle() == -1) {
		RF2351_PRINT("Error: mspi is busy\n");
		return -1;
	}
	
	reg_data = (Addr << 16) | BIT_31;
	sci_glb_write(RFSPI_MCU_RCMD, reg_data, -1UL);

	sprd_rfspi_wait_read_idle();
	*Read_data = sci_glb_read(RFSPI_MCU_RDATA,-1UL);
	return 0;
}

static void sprd_rfspi_clk_enable(void)
{
	if(rfspi.clk)
		#ifdef CONFIG_OF
		clk_prepare_enable(rfspi.clk);
		#else
		clk_enable(rfspi.clk);
		#endif
	else 
		RF2351_PRINT("rfspi.clk is NULL\n");
}

static void sprd_rfspi_clk_disable(void)
{
	if(rfspi.clk)
		#ifdef CONFIG_OF
		clk_disable_unprepare(rfspi.clk);
		#else
		clk_disable(rfspi.clk);
		#endif
	else
		RF2351_PRINT("rfspi.clk is NULL\n");
}

static void sprd_mspi_enable(void)
{
	sci_glb_set(APB_EB0, RFSPI_ENABLE_CTL);
}

static void sprd_mspi_disable(void)
{
	/*Because of BT can't work when connet BT Earphones if disable the mspi*/
	//sci_glb_clr(APB_EB0, RFSPI_ENABLE_CTL);
}

static unsigned int sprd_rfspi_enable(void)
{
	rfspi.count++;
	if(rfspi.count == 1)
	{
		#ifndef CONFIG_OF
		rfspi.clk = clk_get(NULL, "clk_cpll");
		if(rfspi.clk == NULL)
{
			RF2351_PRINT("rfspi get clk_cpll failed\n");
			return -1;
		}
		#endif
		sprd_rfspi_clk_enable();
		sprd_mspi_enable();
	}

	return 0;
}

static unsigned int sprd_rfspi_disable(void)
{
	if(rfspi.count == 1)
	{
		sprd_mspi_disable();
		sprd_rfspi_clk_disable();
		#ifndef CONFIG_OF
		clk_put(rfspi.clk);
		#endif
	}
	rfspi.count--;
	if(rfspi.count < 0)
		rfspi.count = 0;

	return 0;
}

static DEFINE_MUTEX(rf2351_lock);
static int rf2351_power_count=0;



void rf2351_gpio_ctrl_power_enable(int flag)
{
    if(!s_is_gpio_register)
    return;

    if(!flag && (0 == rf2351_power_count))//avoid calling the func  first time with flag =0
    return;
    
    RF2351_PRINT("rf2351_power_count =%d\n", rf2351_power_count);
    mutex_lock(&rf2351_lock);
    if(0 == rf2351_power_count)
    {
        if (gpio_request(GPIO_RF2351_POWER_CTRL, "rf2351_power_pin"))
        {
            RF2351_PRINT("request gpio %d failed\n",GPIO_RF2351_POWER_CTRL);
        }
        gpio_direction_output(GPIO_RF2351_POWER_CTRL, 1);	 
    }

    if(flag)
    {
        if(0 == rf2351_power_count)
        {
            gpio_set_value(GPIO_RF2351_POWER_CTRL, 1);
           RF2351_PRINT("rf2351_gpio_ctrl_power_enable  on\n"); 
        }
        rf2351_power_count++;
    }
    else
    {
         rf2351_power_count--;
        if(0 == rf2351_power_count)
        {
            gpio_set_value(GPIO_RF2351_POWER_CTRL, 0);
            gpio_free(GPIO_RF2351_POWER_CTRL);
            RF2351_PRINT("rf2351_gpio_ctrl_power_enable  off \n"); 
        }
    }
     mutex_unlock(&rf2351_lock);
}


EXPORT_SYMBOL(rf2351_gpio_ctrl_power_enable);

void sprd_sr2351_vddwpa_ctrl_power_register(void)
{
    s_is_vddwpa_register = true;
}
static int rf2351_cnt = 0;
int rf2351_vddwpa_ctrl_power_enable(int flag)
{
    static struct regulator *wpa_rf2351 = NULL;
    static int f_enabled = 0;

    if (!s_is_vddwpa_register)
    return 0;

    printk("[wpa_rf2351] LDO control : %s\n", flag ? "ON" : "OFF");

    if (flag ) {
		rf2351_cnt++;
		if(0 != f_enabled)
			return 0;
		
        wpa_rf2351 = regulator_get(NULL, "vddsim2");
        if (IS_ERR(wpa_rf2351)) {
            printk("rf2351 could not find the vddwpa regulator\n");
            wpa_rf2351 = NULL;
            return EIO;
        } else {
            regulator_set_voltage(wpa_rf2351, 2800000, 2800000);
            regulator_enable(wpa_rf2351);
        }
        f_enabled = 1;
    }
    if (!flag) {
		rf2351_cnt--;
		if(0 != rf2351_cnt)
			return 0;
        if (wpa_rf2351) {
            regulator_disable(wpa_rf2351);
            regulator_put(wpa_rf2351);
            wpa_rf2351 = NULL;
        }
        f_enabled = 0;
    }

    return 0;
}

EXPORT_SYMBOL(rf2351_vddwpa_ctrl_power_enable);

static struct sprd_2351_interface sprd_rf2351_ops = {
	.name = "rf2351",
	.mspi_enable = sprd_rfspi_enable,
	.mspi_disable = sprd_rfspi_disable,
	.read_reg = sprd_rfspi_read,
	.write_reg = sprd_rfspi_write,
};

int sprd_get_rf2351_ops(struct sprd_2351_interface **rf2351_ops)
{
	*rf2351_ops = &sprd_rf2351_ops;

	return 0;
}
EXPORT_SYMBOL(sprd_get_rf2351_ops);

int sprd_put_rf2351_ops(struct sprd_2351_interface **rf2351_ops)
{
	*rf2351_ops = NULL;

	return 0;
}
EXPORT_SYMBOL(sprd_put_rf2351_ops);

#if 0

#ifdef CONFIG_ARCH_SCX30G
#define SCI_IOMAP_BASE	0xF5000000

#define SCI_IOMAP(x)	(SCI_IOMAP_BASE + (x))
#define SPRD_PMU_BASE			SCI_IOMAP(0x230000)
#define WCN_REG_CLK_ADDR                               (SPRD_PMU_BASE + 0x68)
#else
#define SCI_IOMAP_BASE	0xF5000000

#define SCI_IOMAP(x)	(SCI_IOMAP_BASE + (x))
#define SPRD_PMU_BASE			SCI_IOMAP(0x230000)
#define WCN_REG_CLK_ADDR                               (SPRD_PMU_BASE + 0x60)
#endif
#endif
#ifdef CONFIG_ARCH_SCX30G
/*
return      1:cp2 work
	        0:cp2 not work
*/
int get_cp2_state()
{
	int reg_value = 0;
	/* cp2 force shutdown */
	//reg_value = sci_glb_read(WCN_REG_CLK_ADDR, 0x02000000);
	printk("[sprd_2351]:reg_value is 0x%x\n",reg_value);
	if(reg_value)
	{
		printk("[sprd_2351]:cp2 shutdown\n");
		return 0;
	}else {
		return 1;
	}
}
EXPORT_SYMBOL(get_cp2_state);

/*
return      1:ok
	        little than 0:fail
cam status: 1:on
  			0:off
*/
int sprd_switch_cp2_clk(int cam_status)
{
	char ret_buf[20]={0};
	char cmd_buf[20]={0};
	int reg_value = 0;
	int send_len=0,ret_len=0;
	if(!get_cp2_state())
		return 0;
	
	if(cam_status) //on
		sprintf((char *)cmd_buf, "at+switchclk=1?\r");
	else //off
		sprintf((char *)cmd_buf, "at+switchclk=0?\r");

	send_len = sbuf_write(SIPC_ID_WCN, SMSG_CH_PIPE, 12,(char *)cmd_buf, strlen(cmd_buf), msecs_to_jiffies(5000));
	if(send_len< 0){
		printk("[sprd_2351]cmd [%s] sbuf_write error!\n",cmd_buf);
		return send_len;
	} else if (send_len != (strlen(cmd_buf))) {
		printk("[sprd_2351]cmd [%s] sbuf_write not completely with send len %d\n",cmd_buf, send_len);
		return -EIO;
	}

	ret_len = sbuf_read(SIPC_ID_WCN, SMSG_CH_PIPE, 12,ret_buf,20, msecs_to_jiffies(5000));
	if(ret_len < 14){
		printk("[sprd_2351]cmd [%s] sbuf_read error, ret_len is %d\n",cmd_buf,ret_len);
		return -EIO;
	}


	if((strncmp(ret_buf,"+ok:switch clk",14) == 0) || (strncmp(ret_buf,"+ok:restore clk",14) == 0)){	
		printk("[sprd_2351] sprd_switch_cp2_clk end,ret_buf is %s\n",ret_buf);
		return 1;
	}
	else {
		printk("[sprd_2351] sprd_switch_cp2_clk error,ret_buf is %s\n",ret_buf);
		return -1;
	}
	
}
EXPORT_SYMBOL(sprd_switch_cp2_clk);

#endif

#ifdef CONFIG_OF
static struct rf2351_addr
{
	u32 rfspi_base;
	u32 apb_base;
}rf2351_base;

u32 rf2351_get_rfspi_base(void)
{
	return rf2351_base.rfspi_base;
}

u32 rf2351_get_apb_base(void)
{
	return rf2351_base.apb_base;
}

static int __init sprd_rfspi_init(void)
{
	int ret;

	struct device_node *np;
	struct resource res;

	np = of_find_node_by_name(NULL, "sprd_rf2351");
	if (!np) {
		RF2351_PRINT("Can't get the sprd_rfspi node!\n");
		return -ENODEV;
	}
	RF2351_PRINT(" find the sprd_rfspi node!\n");

	ret = of_address_to_resource(np, 0, &res);
	if (ret < 0) {
		RF2351_PRINT("Can't get the rfspi reg base!\n");
		return -EIO;
	}
	rf2351_base.rfspi_base =  (unsigned long)ioremap_nocache(res.start, resource_size(&res));
	RF2351_PRINT("rfspi reg base is 0x%x\n", rf2351_base.rfspi_base);

	ret = of_address_to_resource(np, 1, &res);
	if (ret < 0) {
		RF2351_PRINT("Can't get the rfspi reg base!\n");
		return -EIO;
	}
	rf2351_base.apb_base =  (unsigned long)ioremap_nocache(res.start, resource_size(&res));
	RF2351_PRINT("rfspi reg base is 0x%x\n", rf2351_base.apb_base);

	rfspi.clk = of_clk_get_by_name(np,"clk_cpll");
	if (IS_ERR(rfspi.clk)) {
		RF2351_PRINT("get clk_cpll fail!\n");
		return -1;
	} else {
		RF2351_PRINT("get clk_cpll ok!\n");
	}

	return ret;
}

module_init(sprd_rfspi_init);

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("sprd 2351 rfspi driver");
#endif