summaryrefslogtreecommitdiff
path: root/drivers/video/sprdfb/sprdfb_mcu.c
blob: ff4f91fc648c8699c9e74631d007d6e27cc3e34d (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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
/*
 * Copyright (C) 2012 Spreadtrum Communications Inc.
 *
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 *
 * 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.
 */

#include <linux/kernel.h>
#include <linux/clk.h>
#include <linux/delay.h>
#include <linux/err.h>
#ifdef CONFIG_OF
#include <linux/fb.h>
#endif

#include "sprdfb.h"
#include "sprdfb_panel.h"
#include "sprdfb_dispc_reg.h"

//#define  CONFIG_FB_NO_FMARK    /*Jessica for FPGA test*/

static int32_t dispc_mcu_send_cmd(uint32_t cmd)
{
	int wait_count = 0;
	/* busy wait for ahb fifo full sign's disappearance */
	while((dispc_read(DISPC_DBI_QUEUE) & BIT(5)) && (wait_count < 20000)){
		udelay(5);
		wait_count++;
	}
	if(wait_count >= 20000){
		printk("sprdfb: [%s] send cmd not finish!!!\n", __FUNCTION__);
		return -1;
	}

	dispc_write(cmd, DISPC_DBI_CMD);

	return 0;
}

static int32_t dispc_mcu_send_cmd_data(uint32_t cmd, uint32_t data)
{
	int wait_count = 0;
	/* busy wait for ahb fifo full sign's disappearance */
	while((dispc_read(DISPC_DBI_QUEUE) & BIT(5)) && (wait_count < 20000)){
		udelay(5);
		wait_count++;
	}
	if(wait_count >= 20000){
		printk("sprdfb: [%s] send cmd data not finish 1!!!\n", __FUNCTION__);
		return -1;
	}

	dispc_write(cmd, DISPC_DBI_CMD);

	wait_count = 0;
	/* busy wait for ahb fifo full sign's disappearance */
	while((dispc_read(DISPC_DBI_QUEUE) & BIT(5)) && (wait_count < 20000)){
		udelay(5);
		wait_count++;
	}
	if(wait_count >= 20000){
		printk("sprdfb: [%s] send cmd data not finish 2!!!\n", __FUNCTION__);
		return -1;
	}

	dispc_write(data, DISPC_DBI_DATA);

	return 0;
}

static int32_t dispc_mcu_send_data(uint32_t data)
{
	int wait_count = 0;
	/* busy wait for ahb fifo full sign's disappearance */
	while((dispc_read(DISPC_DBI_QUEUE) & BIT(5)) && (wait_count < 20000)){
		udelay(5);
		wait_count++;
	}
	if(wait_count >= 20000){
		printk("sprdfb: [%s] send data not finish!!!\n", __FUNCTION__);
		return -1;
	}

	dispc_write(data, DISPC_DBI_DATA);

	return 0;
}

static uint32_t dispc_mcu_read_data(void)
{
	int wait_count = 0;
	/* busy wait for ahb fifo full sign's disappearance */
	while((dispc_read(DISPC_DBI_QUEUE) & BIT(5)) && (wait_count < 20000)){
		udelay(5);
		wait_count++;
	}
	if(wait_count >= 20000){
		printk("sprdfb: [%s] read data not finish!!!\n", __FUNCTION__);
		return -1;
	}
	dispc_write(1 << 24, DISPC_DBI_DATA);
	udelay(50);
	return dispc_read(DISPC_DBI_RDATA);
}

static struct ops_mcu dispc_mcu_ops = {
	.send_cmd = dispc_mcu_send_cmd,
	.send_cmd_data = dispc_mcu_send_cmd_data,
	.send_data = dispc_mcu_send_data,
	.read_data = dispc_mcu_read_data,
};

#ifdef CONFIG_OF
static uint32_t mcu_calc_timing(struct timing_mcu *timing, struct sprdfb_device *dev)
#else
static uint32_t mcu_calc_timing(struct timing_mcu *timing, uint16_t dev_id)
#endif
{
	uint32_t  clk_rate;
	uint32_t  rcss, rlpw, rhpw, wcss, wlpw, whpw;
	struct clk * clk = NULL;

	if(NULL == timing){
		printk(KERN_ERR "sprdfb: [%s]: Invalid Param\n", __FUNCTION__);
		return 0;
	}

#ifdef CONFIG_OF
	if(SPRDFB_MAINLCD_ID == dev->dev_id){
		clk = of_clk_get_by_name(dev->of_dev->of_node,"dispc_dbi_clk");
#else
	if(SPRDFB_MAINLCD_ID == dev_id){
		clk = clk_get(NULL,"clk_dispc_dbi");
#endif
		if (IS_ERR(clk)) {
			printk(KERN_WARNING "sprdfb: get clk_dispc_dbi fail!\n");
		} else {
			pr_debug(KERN_INFO "sprdfb: get clk_dispc_dbi ok!\n");
		}
	}
//	clk_rate = clk_get_rate(clk) / 1000000;
	clk_rate = 250;	// dummy 250M Hz

#ifdef CONFIG_OF
	pr_debug(KERN_INFO "sprdfb: [%s] clk_rate: 0x%x, dev_id = %d\n", __FUNCTION__, clk_rate, dev->dev_id);
#else
	pr_debug(KERN_INFO "sprdfb: [%s] clk_rate: 0x%x, dev_id = %d\n", __FUNCTION__, clk_rate, dev_id);
#endif
	/********************************************************
	* we assume : t = ? ns, dispc_dbi = ? MHz   so
	*      1ns need cycle  :  dispc_dbi /1000
	*      tns need cycles :  t * dispc_dbi / 1000
	*
	********************************************************/
#define MAX_DBI_RWCSS_TIMING_VALUE	15
#define MAX_DBI_RWLPW_TIMING_VALUE	63
#define MAX_DBI_RWHPW_TIMING_VALUE	63
#define DBI_CYCLES(ns) (( (ns) * clk_rate + 1000 - 1)/ 1000)

	/* ceiling*/
	rcss = DBI_CYCLES(timing->rcss);
	if (rcss > MAX_DBI_RWCSS_TIMING_VALUE) {
		rcss = MAX_DBI_RWCSS_TIMING_VALUE ;
	}

	rlpw = DBI_CYCLES(timing->rlpw);
	if (rlpw > MAX_DBI_RWLPW_TIMING_VALUE) {
		rlpw = MAX_DBI_RWLPW_TIMING_VALUE ;
	}

	rhpw = DBI_CYCLES (timing->rhpw);
	if (rhpw > MAX_DBI_RWHPW_TIMING_VALUE) {
		rhpw = MAX_DBI_RWHPW_TIMING_VALUE ;
	}

	wcss = DBI_CYCLES(timing->wcss);
	if (wcss > MAX_DBI_RWCSS_TIMING_VALUE) {
		wcss = MAX_DBI_RWCSS_TIMING_VALUE ;
	}

	wlpw = DBI_CYCLES(timing->wlpw);
	if (wlpw > MAX_DBI_RWLPW_TIMING_VALUE) {
		wlpw = MAX_DBI_RWLPW_TIMING_VALUE ;
	}

#ifndef CONFIG_LCD_CS_ALWAYS_LOW
	 /* dispc/lcdc will waste one cycle because CS pulse will use one cycle*/
	whpw = DBI_CYCLES (timing->whpw) - 1;
#else
	whpw = DBI_CYCLES (timing->whpw) ;
#endif
	if (whpw > MAX_DBI_RWHPW_TIMING_VALUE) {
		whpw = MAX_DBI_RWHPW_TIMING_VALUE ;
	}

	return (whpw | (wlpw << 6) | (wcss << 12)
			| (rhpw << 16) |(rlpw << 22) | (rcss << 28));
}

static uint32_t mcu_readid(struct panel_spec *self)
{
	uint32_t id = 0;

	/* default id reg is 0 */
	self->info.mcu->ops->send_cmd(0x0);

	if(self->info.mcu->bus_width == 8) {
		id = (self->info.mcu->ops->read_data()) & 0xff;
		id <<= 8;
		id |= (self->info.mcu->ops->read_data()) & 0xff;
	} else {
		id = self->info.mcu->ops->read_data();
	}

	return id;
}

#ifdef CONFIG_FB_LCD_CS1
/*cs1*/
void mcu_dispc_init_config(struct panel_spec *panel)
{
	uint32_t reg_val = 0;

	pr_debug("sprdfb: [%s] for cs1\n", __FUNCTION__);

	if(NULL == panel){
		printk(KERN_ERR "sprdfb: [%s] fail.(Invalid Param)\n", __FUNCTION__);
		return;
	}

	if(SPRDFB_PANEL_TYPE_MCU != panel->type){
		printk(KERN_ERR "sprdfb: [%s] fail.(not  mcu panel)\n", __FUNCTION__);
		return;
	}

	/*use dbi as interface*/
	dispc_set_bits((2<<1), DISPC_CTRL);

	/* CS1 bus mode [BIT8]: 8080/6800 */
	switch (panel->info.mcu->bus_mode) {
	case LCD_BUS_8080:
		break;
	case LCD_BUS_6800:
		reg_val |= (1<<8);
		break;
	default:
		break;
	}
	/* CS1 bus width [BIT11:9] */
	switch (panel->info.mcu->bus_width) {
	case 8:
		break;
	case 9:
		reg_val |= (1 << 9);
		break;
	case 16:
		reg_val |= (2 << 9);
		break;
	case 18:
		reg_val |= (3 << 9) ;
		break;
	case 24:
		reg_val |= (4 << 9);
		break;
	default:
		break;
	}

	/*CS1 pixel bits [BIT13:12]*/
	switch (panel->info.mcu->bpp) {
	case 16:
		break;
	case 18:
		reg_val |= (1 << 12) ;
		break;
	case 24:
		reg_val |= (2 << 12);
		break;
	default:
		break;
	}

#ifndef CONFIG_FB_NO_FMARK
	/*TE enable*/
	reg_val |= (1 << 16);
	if(SPRDFB_POLARITY_NEG == panel->info.mcu->te_pol){
		reg_val |= (1<< 17);
	}
	dispc_write(panel->info.mcu->te_sync_delay, DISPC_TE_SYNC_DELAY);
#endif

#ifdef CONFIG_LCD_CS_ALWAYS_LOW
	/*CS alway low mode*/
	reg_val |= (1<<21);
#else
	/*CS not alway low mode*/
#endif

	/*CS1 selected*/
	reg_val |= (1 << 20);

	dispc_write(reg_val, DISPC_DBI_CTRL);

	pr_debug("sprdfb: [%s] DISPC_DBI_CTRL = %d\n", __FUNCTION__, dispc_read(DISPC_DBI_CTRL));
}

void mcu_dispc_set_timing(struct sprdfb_device *dev, uint32_t type)
{
	pr_debug("sprdfb: [%s] for cs1, type = %d\n", __FUNCTION__, type);

	switch (type)
	{
	case MCU_LCD_REGISTER_TIMING:
		dispc_write(dev->panel_timing.mcu_timing[MCU_LCD_REGISTER_TIMING],DISPC_DBI_TIMING1);
		break;

	case MCU_LCD_GRAM_TIMING:
		dispc_write(dev->panel_timing.mcu_timing[MCU_LCD_GRAM_TIMING],DISPC_DBI_TIMING1);
		break;
	default:
		break;
	}
}

#else
/*cs0*/
void mcu_dispc_init_config(struct panel_spec *panel)
{
	uint32_t reg_val = 0;

	pr_debug("sprdfb: [%s] for cs0\n", __FUNCTION__);

	if(NULL == panel){
		printk(KERN_ERR "sprdfb: [%s] fail.(Invalid Param)\n", __FUNCTION__);
		return;
	}

	if(SPRDFB_PANEL_TYPE_MCU != panel->type){
		printk(KERN_ERR "sprdfb: [%s] fail.(not  mcu panel)\n", __FUNCTION__);
		return;
	}

	/*use dbi as interface*/
	dispc_set_bits((2<<1), DISPC_CTRL);

	/* CS0 bus mode [BIT0]: 8080/6800 */
	switch (panel->info.mcu->bus_mode) {
	case LCD_BUS_8080:
		break;
	case LCD_BUS_6800:
		reg_val |= 1;
		break;
	default:
		break;
	}
	/* CS0 bus width [BIT3:1] */
	switch (panel->info.mcu->bus_width) {
	case 8:
		break;
	case 9:
		reg_val |= (1 << 1);
		break;
	case 16:
		reg_val |= (2 << 1);
		break;
	case 18:
		reg_val |= (3 << 1) ;
		break;
	case 24:
		reg_val |= (4 << 1);
		break;
	default:
		break;
	}

	/*CS0 pixel bits [BIT5:4]*/
	switch (panel->info.mcu->bpp) {
	case 16:
		break;
	case 18:
		reg_val |= (1 << 4) ;
		break;
	case 24:
		reg_val |= (2 << 4);
		break;
	default:
		break;
	}

#ifndef CONFIG_FB_NO_FMARK
	/*TE enable*/
	reg_val |= (1 << 16);
	if(SPRDFB_POLARITY_NEG == panel->info.mcu->te_pol){
		reg_val |= (1<< 17);
	}
	dispc_write(panel->info.mcu->te_sync_delay, DISPC_TE_SYNC_DELAY);
#endif

#ifdef CONFIG_LCD_CS_ALWAYS_LOW
	/*CS alway low mode*/
	reg_val |= (1<<21);
#else
	/*CS not alway low mode*/
#endif

	/*CS0 selected*/

	dispc_write(reg_val, DISPC_DBI_CTRL);

	pr_debug("sprdfb: [%s] DISPC_DBI_CTRL = %d\n", __FUNCTION__, dispc_read(DISPC_DBI_CTRL));
}

void mcu_dispc_set_timing(struct sprdfb_device *dev, uint32_t type)
{
	pr_debug("sprdfb: [%s] for cs0, type = %d\n", __FUNCTION__, type);

	switch (type)
	{
	case MCU_LCD_REGISTER_TIMING:
		dispc_write(dev->panel_timing.mcu_timing[MCU_LCD_REGISTER_TIMING],DISPC_DBI_TIMING0);
		break;

	case MCU_LCD_GRAM_TIMING:
		dispc_write(dev->panel_timing.mcu_timing[MCU_LCD_GRAM_TIMING],DISPC_DBI_TIMING0);
		break;
	default:
		break;
	}
}

#endif

static int32_t sprdfb_mcu_panel_check(struct panel_spec *panel)
{
	struct info_mcu* mcu_info = NULL;
	bool rval = true;

	if(NULL == panel){
		printk("sprdfb: [%s] fail. (Invalid param)\n", __FUNCTION__);
		return false;
	}

	if(SPRDFB_PANEL_TYPE_MCU != panel->type){
		printk("sprdfb: [%s] fail. (not mcu param)\n", __FUNCTION__);
		return false;
	}

	mcu_info = panel->info.mcu;

	pr_debug("sprdfb: [%s]: bus width= %d, bpp = %d\n",__FUNCTION__, mcu_info->bus_width, mcu_info->bpp);

	switch(mcu_info->bus_width){
	case 8:
		if((16 != mcu_info->bpp) && (24 != mcu_info->bpp)){
			rval = false;
		}
		break;
	case 9:
		if(18 != mcu_info->bpp) {
			rval = false;
		}
		break;
	case 16:
		if((16 != mcu_info->bpp) && (18 != mcu_info->bpp) &&
			(24 != mcu_info->bpp)){
			rval = false;
		}
		break;
	case 18:
		if(18 != mcu_info->bpp){
			rval = false;
		}
		break;
	case 24:
		if(24 != mcu_info->bpp){
			rval = false;
		}
		break;
	default:
		rval = false;
		break;
	}

	if(!rval){
		printk(KERN_ERR "sprdfb: mcu_panel_check return false!\n");
	}

	return rval;
}

static void sprdfb_mcu_panel_mount(struct sprdfb_device *dev)
{
	struct timing_mcu* timing = NULL;

	if((NULL == dev) || (NULL == dev->panel)){
		printk(KERN_ERR "sprdfb: [%s]: Invalid Param\n", __FUNCTION__);
		return;
	}

	pr_debug(KERN_INFO "sprdfb: [%s], dev_id = %d\n",__FUNCTION__, dev->dev_id);

	dev->panel_if_type = SPRDFB_PANEL_IF_DBI;

	if(SPRDFB_MAINLCD_ID == dev->dev_id){
		dev->panel->info.mcu->ops =  &dispc_mcu_ops;
	}

	if(NULL == dev->panel->ops->panel_readid){
		dev->panel->ops->panel_readid = mcu_readid;
	}

	timing = dev->panel->info.mcu->timing;
#ifdef CONFIG_OF
	dev->panel_timing.mcu_timing[MCU_LCD_REGISTER_TIMING] = mcu_calc_timing(timing, dev);
#else
	dev->panel_timing.mcu_timing[MCU_LCD_REGISTER_TIMING] = mcu_calc_timing(timing, dev->dev_id);
#endif
	timing++;
#ifdef CONFIG_OF
	dev->panel_timing.mcu_timing[MCU_LCD_GRAM_TIMING] = mcu_calc_timing(timing, dev);
#else
	dev->panel_timing.mcu_timing[MCU_LCD_GRAM_TIMING] = mcu_calc_timing(timing, dev->dev_id);
#endif
}

static bool sprdfb_mcu_panel_init(struct sprdfb_device *dev)
{
	if(SPRDFB_MAINLCD_ID == dev->dev_id){
		mcu_dispc_init_config(dev->panel);
		mcu_dispc_set_timing(dev, MCU_LCD_REGISTER_TIMING);
	}
	return true;
}

static void sprdfb_mcu_panel_before_refresh(struct sprdfb_device *dev)
{
	if(SPRDFB_MAINLCD_ID == dev->dev_id){
		mcu_dispc_set_timing(dev, MCU_LCD_GRAM_TIMING);
	}
}

static void sprdfb_mcu_panel_after_refresh(struct sprdfb_device *dev)
{
	if(SPRDFB_MAINLCD_ID == dev->dev_id){
		mcu_dispc_set_timing(dev, MCU_LCD_REGISTER_TIMING);
	}
}

struct panel_if_ctrl sprdfb_mcu_ctrl = {
	.if_name		= "mcu",
	.panel_if_check		= sprdfb_mcu_panel_check,
	.panel_if_mount		 	= sprdfb_mcu_panel_mount,
	.panel_if_init		= sprdfb_mcu_panel_init,
	.panel_if_before_refresh	= sprdfb_mcu_panel_before_refresh,
	.panel_if_after_refresh	= sprdfb_mcu_panel_after_refresh,
};