aboutsummaryrefslogtreecommitdiff
path: root/drivers/misc/mediatek/videox/mt6735/mtkfb_console.c
blob: 469a5a629d43a4294ced16582772669622d0bb39 (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
381
382
383
384
385
#include <linux/font.h>
#include <linux/string.h>
#include <linux/semaphore.h>
#include <linux/slab.h>

#include <mach/mt_typedefs.h>
#include "mtkfb_console.h"
#include "ddp_hal.h"

// ---------------------------------------------------------------------------

#define MFC_WIDTH           (ctxt->fb_width)
#define MFC_HEIGHT          (ctxt->fb_height)
#define MFC_BPP             (ctxt->fb_bpp)
#define MFC_PITCH           (MFC_WIDTH * MFC_BPP)

#define MFC_FG_COLOR        (ctxt->fg_color)
#define MFC_BG_COLOR        (ctxt->bg_color)

#define MFC_FONT            font_vga_8x16
#define MFC_FONT_WIDTH      (MFC_FONT.width)
#define MFC_FONT_HEIGHT     (MFC_FONT.height)
#define MFC_FONT_DATA       (MFC_FONT.data)

#define MFC_ROW_SIZE        (MFC_FONT_HEIGHT * MFC_PITCH)
#define MFC_ROW_FIRST       ((BYTE*)(ctxt->fb_addr))
#define MFC_ROW_SECOND      (MFC_ROW_FIRST + MFC_ROW_SIZE)
#define MFC_ROW_LAST        (MFC_ROW_FIRST + MFC_SIZE - MFC_ROW_SIZE)
#define MFC_SIZE            (MFC_ROW_SIZE * ctxt->rows)
#define MFC_SCROLL_SIZE     (MFC_SIZE - MFC_ROW_SIZE)

#define MAKE_TWO_RGB565_COLOR(high, low)  (((low) << 16) | (high))

#define MFC_LOCK()                                                          \
    do {                                                                    \
        if (down_interruptible(&ctxt->sem)) {                               \
            printk("[MFC] ERROR: Can't get semaphore in %s()\n",            \
                   __func__);                                           \
            ASSERT(0);                                                      \
            return MFC_STATUS_LOCK_FAIL;                                    \
        }                                                                   \
    } while (0)
    
#define MFC_UNLOCK()                                                        \
    do {                                                                    \
        up(&ctxt->sem);                                                     \
    } while (0)


// ---------------------------------------------------------------------------
UINT32 MFC_Get_Cursor_Offset(MFC_HANDLE handle)
{
	MFC_CONTEXT *ctxt = (MFC_CONTEXT *)handle;
	
	UINT32 offset =  ctxt->cursor_col * MFC_FONT_WIDTH*MFC_BPP+ ctxt->cursor_row * MFC_FONT_HEIGHT*MFC_PITCH;
	
	return offset;
}

static void _MFC_DrawChar(MFC_CONTEXT *ctxt, UINT32 x, UINT32 y, char c)
{
    BYTE ch = *((BYTE*)&c);
	const BYTE *cdat;
    BYTE *dest;
	INT32 rows, offset;

    int font_draw_table16[4];

    ASSERT(x <= (MFC_WIDTH - MFC_FONT_WIDTH));
    ASSERT(y <= (MFC_HEIGHT - MFC_FONT_HEIGHT));

    offset = y * MFC_PITCH + x * MFC_BPP;
    dest = (MFC_ROW_FIRST + offset);

    switch (MFC_BPP) {
    case 2:
        font_draw_table16[0] = MAKE_TWO_RGB565_COLOR(MFC_BG_COLOR, MFC_BG_COLOR);
        font_draw_table16[1] = MAKE_TWO_RGB565_COLOR(MFC_BG_COLOR, MFC_FG_COLOR);
        font_draw_table16[2] = MAKE_TWO_RGB565_COLOR(MFC_FG_COLOR, MFC_BG_COLOR);
        font_draw_table16[3] = MAKE_TWO_RGB565_COLOR(MFC_FG_COLOR, MFC_FG_COLOR);

        cdat = (const BYTE*)MFC_FONT_DATA + ch * MFC_FONT_HEIGHT;
        
        for (rows = MFC_FONT_HEIGHT; rows--; dest += MFC_PITCH)
        {
            BYTE bits = *cdat++;
        
            ((UINT32*)dest)[0] = font_draw_table16[bits >> 6];
            ((UINT32*)dest)[1] = font_draw_table16[bits >> 4 & 3];
            ((UINT32*)dest)[2] = font_draw_table16[bits >> 2 & 3];
            ((UINT32*)dest)[3] = font_draw_table16[bits & 3];
        }
        break;

    default:
        ASSERT(0);
    }
}


static void _MFC_ScrollUp(MFC_CONTEXT *ctxt)
{
    const UINT32 BG_COLOR = MAKE_TWO_RGB565_COLOR(MFC_BG_COLOR, MFC_BG_COLOR);
    
    UINT32 *ptr = (UINT32 *)MFC_ROW_LAST;
    int i = MFC_ROW_SIZE / sizeof(UINT32);

    memcpy(MFC_ROW_FIRST, MFC_ROW_SECOND, MFC_SCROLL_SIZE);
    
    while(--i >= 0) {
        *ptr ++ = BG_COLOR;
    }
}


static void _MFC_Newline(MFC_CONTEXT *ctxt)
{
    ///Bin:add for filling the color for the blank of this column
    while(ctxt->cursor_col < ctxt->cols)
    {
        _MFC_DrawChar(ctxt,
                      ctxt->cursor_col * MFC_FONT_WIDTH,
                      ctxt->cursor_row * MFC_FONT_HEIGHT,
                      ' ');

        ++ ctxt->cursor_col;    
    }
    ++ ctxt->cursor_row;
    ctxt->cursor_col = 0;

    /* Check if we need to scroll the terminal */
    if (ctxt->cursor_row >= ctxt->rows) {
        /* Scroll everything up */
        _MFC_ScrollUp(ctxt);

        /* Decrement row number */
        -- ctxt->cursor_row;
    }
}


#define CHECK_NEWLINE()                     \
    do {                                    \
        if (ctxt->cursor_col >= ctxt->cols) \
            _MFC_Newline(ctxt);             \
    } while(0)

static void _MFC_Putc(MFC_CONTEXT *ctxt, const char c)
{
    CHECK_NEWLINE();

    switch (c) {
    case '\n':		/* next line */
        _MFC_Newline(ctxt);
        break;

    case '\r':		/* carriage return */
        ctxt->cursor_col = 0;
        break;

    case '\t':		/* tab 8 */
        ctxt->cursor_col += 8;
        ctxt->cursor_col &= ~0x0007;
        CHECK_NEWLINE();
        break;

    default:		/* draw the char */
        _MFC_DrawChar(ctxt,
                      ctxt->cursor_col * MFC_FONT_WIDTH,
                      ctxt->cursor_row * MFC_FONT_HEIGHT,
                      c);
        ++ ctxt->cursor_col;
        CHECK_NEWLINE();
    }
}

// ---------------------------------------------------------------------------

MFC_STATUS MFC_Open(MFC_HANDLE *handle,
                    void *fb_addr,
                    unsigned int fb_width,
                    unsigned int fb_height,
                    unsigned int fb_bpp,
                    unsigned int fg_color,
                    unsigned int bg_color)
{
    MFC_CONTEXT *ctxt = NULL;

    if (NULL == handle || NULL == fb_addr) 
        return MFC_STATUS_INVALID_ARGUMENT;

    if (fb_bpp != 2)
        return MFC_STATUS_NOT_IMPLEMENTED;  // only support RGB565

    ctxt = kzalloc(sizeof(MFC_CONTEXT), GFP_KERNEL);
    if (!ctxt) return MFC_STATUS_OUT_OF_MEMORY;

//    init_MUTEX(&ctxt->sem);
	sema_init(&ctxt->sem, 1);	
    ctxt->fb_addr   = fb_addr;
    ctxt->fb_width  = fb_width;
    ctxt->fb_height = fb_height;
    ctxt->fb_bpp    = fb_bpp;
    ctxt->fg_color  = fg_color;
    ctxt->bg_color  = bg_color;
    ctxt->rows      = fb_height / MFC_FONT_HEIGHT;
    ctxt->cols      = fb_width / MFC_FONT_WIDTH;
	ctxt->font_width = MFC_FONT_WIDTH;
	ctxt->font_height = MFC_FONT_HEIGHT;

    *handle = ctxt;

    return MFC_STATUS_OK;
}

MFC_STATUS MFC_Open_Ex(MFC_HANDLE *handle,
                            void *fb_addr,
                            unsigned int fb_width,
                            unsigned int fb_height,
                            unsigned int fb_pitch,
                            unsigned int fb_bpp,
                            unsigned int fg_color,
                            unsigned int bg_color)
{

     MFC_CONTEXT *ctxt = NULL;

    if (NULL == handle || NULL == fb_addr) 
        return MFC_STATUS_INVALID_ARGUMENT;

    if (fb_bpp != 2)
        return MFC_STATUS_NOT_IMPLEMENTED;  // only support RGB565

    ctxt = kzalloc(sizeof(MFC_CONTEXT), GFP_KERNEL);
    if (!ctxt) return MFC_STATUS_OUT_OF_MEMORY;

//    init_MUTEX(&ctxt->sem);
	sema_init(&ctxt->sem, 1);	
    ctxt->fb_addr   = fb_addr;
    ctxt->fb_width  = fb_pitch;
    ctxt->fb_height = fb_height;
    ctxt->fb_bpp    = fb_bpp;
    ctxt->fg_color  = fg_color;
    ctxt->bg_color  = bg_color;
    ctxt->rows      = fb_height / MFC_FONT_HEIGHT;
    ctxt->cols      = fb_width / MFC_FONT_WIDTH;
	ctxt->font_width = MFC_FONT_WIDTH;
	ctxt->font_height = MFC_FONT_HEIGHT;

    *handle = ctxt;

    return MFC_STATUS_OK;

}


MFC_STATUS MFC_Close(MFC_HANDLE handle)
{
    if (!handle)
        return MFC_STATUS_INVALID_ARGUMENT;

    kfree(handle);

    return MFC_STATUS_OK;
}


MFC_STATUS MFC_SetColor(MFC_HANDLE handle,
                        unsigned int fg_color, 
                        unsigned int bg_color)
{
    MFC_CONTEXT *ctxt = (MFC_CONTEXT *)handle;

    if (!ctxt) 
        return MFC_STATUS_INVALID_ARGUMENT;

    MFC_LOCK();
    ctxt->fg_color = fg_color;
    ctxt->bg_color = bg_color;
    MFC_UNLOCK();

    return MFC_STATUS_OK;
}


MFC_STATUS MFC_ResetCursor(MFC_HANDLE handle)
{
    MFC_CONTEXT *ctxt = (MFC_CONTEXT *)handle;

    if (!ctxt) 
        return MFC_STATUS_INVALID_ARGUMENT;

    MFC_LOCK();
    ctxt->cursor_row = ctxt->cursor_col = 0;
    MFC_UNLOCK();

    return MFC_STATUS_OK;
}


MFC_STATUS MFC_Print(MFC_HANDLE handle, const char *str)
{
    MFC_CONTEXT *ctxt = (MFC_CONTEXT *)handle;
	int count = 0;

    if (!ctxt || !str) 
        return MFC_STATUS_INVALID_ARGUMENT;

    MFC_LOCK();

    count = strlen(str);

	while (count--)
		_MFC_Putc(ctxt, *str++);

    MFC_UNLOCK();

    return MFC_STATUS_OK;
}

MFC_STATUS MFC_SetMem(MFC_HANDLE handle, const char *str, UINT32 color)
{
    MFC_CONTEXT *ctxt = (MFC_CONTEXT *)handle;
	int count = 0;
	int i,j;
	UINT32 *ptr;
    
	if (!ctxt || !str) 
        return MFC_STATUS_INVALID_ARGUMENT;

    MFC_LOCK();

    count = strlen(str);
	count = count * MFC_FONT_WIDTH;

	for(j=0;j<MFC_FONT_HEIGHT;j++){
		ptr = (UINT32 *)(ctxt->fb_addr + (j + 1) * MFC_PITCH - count * ctxt->fb_bpp);
		for(i=0;i<count * ctxt->fb_bpp/sizeof(UINT32);i++){
			*ptr ++ = color;
		}
	}

    MFC_UNLOCK();

    return MFC_STATUS_OK;
}

MFC_STATUS MFC_LowMemory_Printf(MFC_HANDLE handle, const char *str, UINT32 fg_color, UINT32 bg_color)
{
    MFC_CONTEXT *ctxt = (MFC_CONTEXT *)handle;
	int count = 0;
	unsigned int col, row, fg_color_mfc, bg_color_mfc;
    
	if (!ctxt || !str) 
        return MFC_STATUS_INVALID_ARGUMENT;

    MFC_LOCK();

    count = strlen(str);
////store cursor_col and row for printf low memory char temply
	row = ctxt->cursor_row;
	col = ctxt->cursor_col;
	ctxt->cursor_row = 0;
	ctxt->cursor_col = ctxt->cols - count;
    fg_color_mfc = ctxt->fg_color;
    bg_color_mfc = ctxt->bg_color;
	
///////////
    ctxt->fg_color = fg_color;
    ctxt->bg_color = bg_color;
	while (count--)
		_MFC_Putc(ctxt, *str++);

////restore cursor_col and row for printf low memory char temply
	ctxt->cursor_row = row;
	ctxt->cursor_col = col;
    ctxt->fg_color = fg_color_mfc;
    ctxt->bg_color = bg_color_mfc;
///////////

	
    MFC_UNLOCK();

    return MFC_STATUS_OK;
}