drivers/video: fsl-diu-fb: fix compilation warning
[deliverable/linux.git] / drivers / video / fsl-diu-fb.c
1 /*
2 * Copyright 2008 Freescale Semiconductor, Inc. All Rights Reserved.
3 *
4 * Freescale DIU Frame Buffer device driver
5 *
6 * Authors: Hongjun Chen <hong-jun.chen@freescale.com>
7 * Paul Widmer <paul.widmer@freescale.com>
8 * Srikanth Srinivasan <srikanth.srinivasan@freescale.com>
9 * York Sun <yorksun@freescale.com>
10 *
11 * Based on imxfb.c Copyright (C) 2004 S.Hauer, Pengutronix
12 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version.
17 *
18 */
19
20 #include <linux/module.h>
21 #include <linux/kernel.h>
22 #include <linux/errno.h>
23 #include <linux/string.h>
24 #include <linux/slab.h>
25 #include <linux/fb.h>
26 #include <linux/init.h>
27 #include <linux/dma-mapping.h>
28 #include <linux/platform_device.h>
29 #include <linux/interrupt.h>
30 #include <linux/clk.h>
31 #include <linux/uaccess.h>
32 #include <linux/vmalloc.h>
33
34 #include <sysdev/fsl_soc.h>
35 #include <linux/fsl-diu-fb.h>
36 #include "edid.h"
37
38 /*
39 * These parameters give default parameters
40 * for video output 1024x768,
41 * FIXME - change timing to proper amounts
42 * hsync 31.5kHz, vsync 60Hz
43 */
44 static struct fb_videomode __devinitdata fsl_diu_default_mode = {
45 .refresh = 60,
46 .xres = 1024,
47 .yres = 768,
48 .pixclock = 15385,
49 .left_margin = 160,
50 .right_margin = 24,
51 .upper_margin = 29,
52 .lower_margin = 3,
53 .hsync_len = 136,
54 .vsync_len = 6,
55 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
56 .vmode = FB_VMODE_NONINTERLACED
57 };
58
59 static struct fb_videomode __devinitdata fsl_diu_mode_db[] = {
60 {
61 .name = "1024x768-60",
62 .refresh = 60,
63 .xres = 1024,
64 .yres = 768,
65 .pixclock = 15385,
66 .left_margin = 160,
67 .right_margin = 24,
68 .upper_margin = 29,
69 .lower_margin = 3,
70 .hsync_len = 136,
71 .vsync_len = 6,
72 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
73 .vmode = FB_VMODE_NONINTERLACED
74 },
75 {
76 .name = "1024x768-70",
77 .refresh = 70,
78 .xres = 1024,
79 .yres = 768,
80 .pixclock = 16886,
81 .left_margin = 3,
82 .right_margin = 3,
83 .upper_margin = 2,
84 .lower_margin = 2,
85 .hsync_len = 40,
86 .vsync_len = 18,
87 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
88 .vmode = FB_VMODE_NONINTERLACED
89 },
90 {
91 .name = "1024x768-75",
92 .refresh = 75,
93 .xres = 1024,
94 .yres = 768,
95 .pixclock = 15009,
96 .left_margin = 3,
97 .right_margin = 3,
98 .upper_margin = 2,
99 .lower_margin = 2,
100 .hsync_len = 80,
101 .vsync_len = 32,
102 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
103 .vmode = FB_VMODE_NONINTERLACED
104 },
105 {
106 .name = "1280x1024-60",
107 .refresh = 60,
108 .xres = 1280,
109 .yres = 1024,
110 .pixclock = 9375,
111 .left_margin = 38,
112 .right_margin = 128,
113 .upper_margin = 2,
114 .lower_margin = 7,
115 .hsync_len = 216,
116 .vsync_len = 37,
117 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
118 .vmode = FB_VMODE_NONINTERLACED
119 },
120 {
121 .name = "1280x1024-70",
122 .refresh = 70,
123 .xres = 1280,
124 .yres = 1024,
125 .pixclock = 9380,
126 .left_margin = 6,
127 .right_margin = 6,
128 .upper_margin = 4,
129 .lower_margin = 4,
130 .hsync_len = 60,
131 .vsync_len = 94,
132 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
133 .vmode = FB_VMODE_NONINTERLACED
134 },
135 {
136 .name = "1280x1024-75",
137 .refresh = 75,
138 .xres = 1280,
139 .yres = 1024,
140 .pixclock = 9380,
141 .left_margin = 6,
142 .right_margin = 6,
143 .upper_margin = 4,
144 .lower_margin = 4,
145 .hsync_len = 60,
146 .vsync_len = 15,
147 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
148 .vmode = FB_VMODE_NONINTERLACED
149 },
150 {
151 .name = "320x240", /* for AOI only */
152 .refresh = 60,
153 .xres = 320,
154 .yres = 240,
155 .pixclock = 15385,
156 .left_margin = 0,
157 .right_margin = 0,
158 .upper_margin = 0,
159 .lower_margin = 0,
160 .hsync_len = 0,
161 .vsync_len = 0,
162 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
163 .vmode = FB_VMODE_NONINTERLACED
164 },
165 {
166 .name = "1280x480-60",
167 .refresh = 60,
168 .xres = 1280,
169 .yres = 480,
170 .pixclock = 18939,
171 .left_margin = 353,
172 .right_margin = 47,
173 .upper_margin = 39,
174 .lower_margin = 4,
175 .hsync_len = 8,
176 .vsync_len = 2,
177 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
178 .vmode = FB_VMODE_NONINTERLACED
179 },
180 };
181
182 static char *fb_mode = "1024x768-32@60";
183 static unsigned long default_bpp = 32;
184 static enum fsl_diu_monitor_port monitor_port;
185 static char *monitor_string;
186
187 #if defined(CONFIG_NOT_COHERENT_CACHE)
188 static u8 *coherence_data;
189 static size_t coherence_data_size;
190 static unsigned int d_cache_line_size;
191 #endif
192
193 static DEFINE_SPINLOCK(diu_lock);
194
195 struct fsl_diu_data {
196 struct fb_info *fsl_diu_info[FSL_AOI_NUM - 1];
197 /*FSL_AOI_NUM has one dummy AOI */
198 struct device_attribute dev_attr;
199 struct diu_ad *dummy_ad;
200 void *dummy_aoi_virt;
201 unsigned int irq;
202 int fb_enabled;
203 enum fsl_diu_monitor_port monitor_port;
204 };
205
206 struct mfb_info {
207 int index;
208 int type;
209 char *id;
210 int registered;
211 int blank;
212 unsigned long pseudo_palette[16];
213 struct diu_ad *ad;
214 int cursor_reset;
215 unsigned char g_alpha;
216 unsigned int count;
217 int x_aoi_d; /* aoi display x offset to physical screen */
218 int y_aoi_d; /* aoi display y offset to physical screen */
219 struct fsl_diu_data *parent;
220 u8 *edid_data;
221 };
222
223
224 static struct mfb_info mfb_template[] = {
225 { /* AOI 0 for plane 0 */
226 .index = 0,
227 .type = MFB_TYPE_OUTPUT,
228 .id = "Panel0",
229 .registered = 0,
230 .count = 0,
231 .x_aoi_d = 0,
232 .y_aoi_d = 0,
233 },
234 { /* AOI 0 for plane 1 */
235 .index = 1,
236 .type = MFB_TYPE_OUTPUT,
237 .id = "Panel1 AOI0",
238 .registered = 0,
239 .g_alpha = 0xff,
240 .count = 0,
241 .x_aoi_d = 0,
242 .y_aoi_d = 0,
243 },
244 { /* AOI 1 for plane 1 */
245 .index = 2,
246 .type = MFB_TYPE_OUTPUT,
247 .id = "Panel1 AOI1",
248 .registered = 0,
249 .g_alpha = 0xff,
250 .count = 0,
251 .x_aoi_d = 0,
252 .y_aoi_d = 480,
253 },
254 { /* AOI 0 for plane 2 */
255 .index = 3,
256 .type = MFB_TYPE_OUTPUT,
257 .id = "Panel2 AOI0",
258 .registered = 0,
259 .g_alpha = 0xff,
260 .count = 0,
261 .x_aoi_d = 640,
262 .y_aoi_d = 0,
263 },
264 { /* AOI 1 for plane 2 */
265 .index = 4,
266 .type = MFB_TYPE_OUTPUT,
267 .id = "Panel2 AOI1",
268 .registered = 0,
269 .g_alpha = 0xff,
270 .count = 0,
271 .x_aoi_d = 640,
272 .y_aoi_d = 480,
273 },
274 };
275
276 static struct diu_hw dr = {
277 .mode = MFB_MODE1,
278 .reg_lock = __SPIN_LOCK_UNLOCKED(diu_hw.reg_lock),
279 };
280
281 static struct diu_pool pool;
282
283 /**
284 * fsl_diu_name_to_port - convert a port name to a monitor port enum
285 *
286 * Takes the name of a monitor port ("dvi", "lvds", or "dlvds") and returns
287 * the enum fsl_diu_monitor_port that corresponds to that string.
288 *
289 * For compatibility with older versions, a number ("0", "1", or "2") is also
290 * supported.
291 *
292 * If the string is unknown, DVI is assumed.
293 *
294 * If the particular port is not supported by the platform, another port
295 * (platform-specific) is chosen instead.
296 */
297 static enum fsl_diu_monitor_port fsl_diu_name_to_port(const char *s)
298 {
299 enum fsl_diu_monitor_port port = FSL_DIU_PORT_DVI;
300 unsigned long val;
301
302 if (s) {
303 if (!strict_strtoul(s, 10, &val) && (val <= 2))
304 port = (enum fsl_diu_monitor_port) val;
305 else if (strncmp(s, "lvds", 4) == 0)
306 port = FSL_DIU_PORT_LVDS;
307 else if (strncmp(s, "dlvds", 5) == 0)
308 port = FSL_DIU_PORT_DLVDS;
309 }
310
311 return diu_ops.valid_monitor_port(port);
312 }
313
314 /**
315 * fsl_diu_alloc - allocate memory for the DIU
316 * @size: number of bytes to allocate
317 * @param: returned physical address of memory
318 *
319 * This function allocates a physically-contiguous block of memory.
320 */
321 static void *fsl_diu_alloc(size_t size, phys_addr_t *phys)
322 {
323 void *virt;
324
325 virt = alloc_pages_exact(size, GFP_DMA | __GFP_ZERO);
326 if (virt)
327 *phys = virt_to_phys(virt);
328
329 return virt;
330 }
331
332 /**
333 * fsl_diu_free - release DIU memory
334 * @virt: pointer returned by fsl_diu_alloc()
335 * @size: number of bytes allocated by fsl_diu_alloc()
336 *
337 * This function releases memory allocated by fsl_diu_alloc().
338 */
339 static void fsl_diu_free(void *virt, size_t size)
340 {
341 if (virt && size)
342 free_pages_exact(virt, size);
343 }
344
345 /*
346 * Workaround for failed writing desc register of planes.
347 * Needed with MPC5121 DIU rev 2.0 silicon.
348 */
349 void wr_reg_wa(u32 *reg, u32 val)
350 {
351 do {
352 out_be32(reg, val);
353 } while (in_be32(reg) != val);
354 }
355
356 static int fsl_diu_enable_panel(struct fb_info *info)
357 {
358 struct mfb_info *pmfbi, *cmfbi, *mfbi = info->par;
359 struct diu *hw = dr.diu_reg;
360 struct diu_ad *ad = mfbi->ad;
361 struct fsl_diu_data *machine_data = mfbi->parent;
362 int res = 0;
363
364 if (mfbi->type != MFB_TYPE_OFF) {
365 switch (mfbi->index) {
366 case 0: /* plane 0 */
367 if (hw->desc[0] != ad->paddr)
368 wr_reg_wa(&hw->desc[0], ad->paddr);
369 break;
370 case 1: /* plane 1 AOI 0 */
371 cmfbi = machine_data->fsl_diu_info[2]->par;
372 if (hw->desc[1] != ad->paddr) { /* AOI0 closed */
373 if (cmfbi->count > 0) /* AOI1 open */
374 ad->next_ad =
375 cpu_to_le32(cmfbi->ad->paddr);
376 else
377 ad->next_ad = 0;
378 wr_reg_wa(&hw->desc[1], ad->paddr);
379 }
380 break;
381 case 3: /* plane 2 AOI 0 */
382 cmfbi = machine_data->fsl_diu_info[4]->par;
383 if (hw->desc[2] != ad->paddr) { /* AOI0 closed */
384 if (cmfbi->count > 0) /* AOI1 open */
385 ad->next_ad =
386 cpu_to_le32(cmfbi->ad->paddr);
387 else
388 ad->next_ad = 0;
389 wr_reg_wa(&hw->desc[2], ad->paddr);
390 }
391 break;
392 case 2: /* plane 1 AOI 1 */
393 pmfbi = machine_data->fsl_diu_info[1]->par;
394 ad->next_ad = 0;
395 if (hw->desc[1] == machine_data->dummy_ad->paddr)
396 wr_reg_wa(&hw->desc[1], ad->paddr);
397 else /* AOI0 open */
398 pmfbi->ad->next_ad = cpu_to_le32(ad->paddr);
399 break;
400 case 4: /* plane 2 AOI 1 */
401 pmfbi = machine_data->fsl_diu_info[3]->par;
402 ad->next_ad = 0;
403 if (hw->desc[2] == machine_data->dummy_ad->paddr)
404 wr_reg_wa(&hw->desc[2], ad->paddr);
405 else /* AOI0 was open */
406 pmfbi->ad->next_ad = cpu_to_le32(ad->paddr);
407 break;
408 default:
409 res = -EINVAL;
410 break;
411 }
412 } else
413 res = -EINVAL;
414 return res;
415 }
416
417 static int fsl_diu_disable_panel(struct fb_info *info)
418 {
419 struct mfb_info *pmfbi, *cmfbi, *mfbi = info->par;
420 struct diu *hw = dr.diu_reg;
421 struct diu_ad *ad = mfbi->ad;
422 struct fsl_diu_data *machine_data = mfbi->parent;
423 int res = 0;
424
425 switch (mfbi->index) {
426 case 0: /* plane 0 */
427 if (hw->desc[0] != machine_data->dummy_ad->paddr)
428 wr_reg_wa(&hw->desc[0], machine_data->dummy_ad->paddr);
429 break;
430 case 1: /* plane 1 AOI 0 */
431 cmfbi = machine_data->fsl_diu_info[2]->par;
432 if (cmfbi->count > 0) /* AOI1 is open */
433 wr_reg_wa(&hw->desc[1], cmfbi->ad->paddr);
434 /* move AOI1 to the first */
435 else /* AOI1 was closed */
436 wr_reg_wa(&hw->desc[1], machine_data->dummy_ad->paddr);
437 /* close AOI 0 */
438 break;
439 case 3: /* plane 2 AOI 0 */
440 cmfbi = machine_data->fsl_diu_info[4]->par;
441 if (cmfbi->count > 0) /* AOI1 is open */
442 wr_reg_wa(&hw->desc[2], cmfbi->ad->paddr);
443 /* move AOI1 to the first */
444 else /* AOI1 was closed */
445 wr_reg_wa(&hw->desc[2], machine_data->dummy_ad->paddr);
446 /* close AOI 0 */
447 break;
448 case 2: /* plane 1 AOI 1 */
449 pmfbi = machine_data->fsl_diu_info[1]->par;
450 if (hw->desc[1] != ad->paddr) {
451 /* AOI1 is not the first in the chain */
452 if (pmfbi->count > 0)
453 /* AOI0 is open, must be the first */
454 pmfbi->ad->next_ad = 0;
455 } else /* AOI1 is the first in the chain */
456 wr_reg_wa(&hw->desc[1], machine_data->dummy_ad->paddr);
457 /* close AOI 1 */
458 break;
459 case 4: /* plane 2 AOI 1 */
460 pmfbi = machine_data->fsl_diu_info[3]->par;
461 if (hw->desc[2] != ad->paddr) {
462 /* AOI1 is not the first in the chain */
463 if (pmfbi->count > 0)
464 /* AOI0 is open, must be the first */
465 pmfbi->ad->next_ad = 0;
466 } else /* AOI1 is the first in the chain */
467 wr_reg_wa(&hw->desc[2], machine_data->dummy_ad->paddr);
468 /* close AOI 1 */
469 break;
470 default:
471 res = -EINVAL;
472 break;
473 }
474
475 return res;
476 }
477
478 static void enable_lcdc(struct fb_info *info)
479 {
480 struct diu *hw = dr.diu_reg;
481 struct mfb_info *mfbi = info->par;
482 struct fsl_diu_data *machine_data = mfbi->parent;
483
484 if (!machine_data->fb_enabled) {
485 out_be32(&hw->diu_mode, dr.mode);
486 machine_data->fb_enabled++;
487 }
488 }
489
490 static void disable_lcdc(struct fb_info *info)
491 {
492 struct diu *hw = dr.diu_reg;
493 struct mfb_info *mfbi = info->par;
494 struct fsl_diu_data *machine_data = mfbi->parent;
495
496 if (machine_data->fb_enabled) {
497 out_be32(&hw->diu_mode, 0);
498 machine_data->fb_enabled = 0;
499 }
500 }
501
502 static void adjust_aoi_size_position(struct fb_var_screeninfo *var,
503 struct fb_info *info)
504 {
505 struct mfb_info *lower_aoi_mfbi, *upper_aoi_mfbi, *mfbi = info->par;
506 struct fsl_diu_data *machine_data = mfbi->parent;
507 int available_height, upper_aoi_bottom, index = mfbi->index;
508 int lower_aoi_is_open, upper_aoi_is_open;
509 __u32 base_plane_width, base_plane_height, upper_aoi_height;
510
511 base_plane_width = machine_data->fsl_diu_info[0]->var.xres;
512 base_plane_height = machine_data->fsl_diu_info[0]->var.yres;
513
514 if (mfbi->x_aoi_d < 0)
515 mfbi->x_aoi_d = 0;
516 if (mfbi->y_aoi_d < 0)
517 mfbi->y_aoi_d = 0;
518 switch (index) {
519 case 0:
520 if (mfbi->x_aoi_d != 0)
521 mfbi->x_aoi_d = 0;
522 if (mfbi->y_aoi_d != 0)
523 mfbi->y_aoi_d = 0;
524 break;
525 case 1: /* AOI 0 */
526 case 3:
527 lower_aoi_mfbi = machine_data->fsl_diu_info[index+1]->par;
528 lower_aoi_is_open = lower_aoi_mfbi->count > 0 ? 1 : 0;
529 if (var->xres > base_plane_width)
530 var->xres = base_plane_width;
531 if ((mfbi->x_aoi_d + var->xres) > base_plane_width)
532 mfbi->x_aoi_d = base_plane_width - var->xres;
533
534 if (lower_aoi_is_open)
535 available_height = lower_aoi_mfbi->y_aoi_d;
536 else
537 available_height = base_plane_height;
538 if (var->yres > available_height)
539 var->yres = available_height;
540 if ((mfbi->y_aoi_d + var->yres) > available_height)
541 mfbi->y_aoi_d = available_height - var->yres;
542 break;
543 case 2: /* AOI 1 */
544 case 4:
545 upper_aoi_mfbi = machine_data->fsl_diu_info[index-1]->par;
546 upper_aoi_height =
547 machine_data->fsl_diu_info[index-1]->var.yres;
548 upper_aoi_bottom = upper_aoi_mfbi->y_aoi_d + upper_aoi_height;
549 upper_aoi_is_open = upper_aoi_mfbi->count > 0 ? 1 : 0;
550 if (var->xres > base_plane_width)
551 var->xres = base_plane_width;
552 if ((mfbi->x_aoi_d + var->xres) > base_plane_width)
553 mfbi->x_aoi_d = base_plane_width - var->xres;
554 if (mfbi->y_aoi_d < 0)
555 mfbi->y_aoi_d = 0;
556 if (upper_aoi_is_open) {
557 if (mfbi->y_aoi_d < upper_aoi_bottom)
558 mfbi->y_aoi_d = upper_aoi_bottom;
559 available_height = base_plane_height
560 - upper_aoi_bottom;
561 } else
562 available_height = base_plane_height;
563 if (var->yres > available_height)
564 var->yres = available_height;
565 if ((mfbi->y_aoi_d + var->yres) > base_plane_height)
566 mfbi->y_aoi_d = base_plane_height - var->yres;
567 break;
568 }
569 }
570 /*
571 * Checks to see if the hardware supports the state requested by var passed
572 * in. This function does not alter the hardware state! If the var passed in
573 * is slightly off by what the hardware can support then we alter the var
574 * PASSED in to what we can do. If the hardware doesn't support mode change
575 * a -EINVAL will be returned by the upper layers.
576 */
577 static int fsl_diu_check_var(struct fb_var_screeninfo *var,
578 struct fb_info *info)
579 {
580 if (var->xres_virtual < var->xres)
581 var->xres_virtual = var->xres;
582 if (var->yres_virtual < var->yres)
583 var->yres_virtual = var->yres;
584
585 if (var->xoffset < 0)
586 var->xoffset = 0;
587
588 if (var->yoffset < 0)
589 var->yoffset = 0;
590
591 if (var->xoffset + info->var.xres > info->var.xres_virtual)
592 var->xoffset = info->var.xres_virtual - info->var.xres;
593
594 if (var->yoffset + info->var.yres > info->var.yres_virtual)
595 var->yoffset = info->var.yres_virtual - info->var.yres;
596
597 if ((var->bits_per_pixel != 32) && (var->bits_per_pixel != 24) &&
598 (var->bits_per_pixel != 16))
599 var->bits_per_pixel = default_bpp;
600
601 switch (var->bits_per_pixel) {
602 case 16:
603 var->red.length = 5;
604 var->red.offset = 11;
605 var->red.msb_right = 0;
606
607 var->green.length = 6;
608 var->green.offset = 5;
609 var->green.msb_right = 0;
610
611 var->blue.length = 5;
612 var->blue.offset = 0;
613 var->blue.msb_right = 0;
614
615 var->transp.length = 0;
616 var->transp.offset = 0;
617 var->transp.msb_right = 0;
618 break;
619 case 24:
620 var->red.length = 8;
621 var->red.offset = 0;
622 var->red.msb_right = 0;
623
624 var->green.length = 8;
625 var->green.offset = 8;
626 var->green.msb_right = 0;
627
628 var->blue.length = 8;
629 var->blue.offset = 16;
630 var->blue.msb_right = 0;
631
632 var->transp.length = 0;
633 var->transp.offset = 0;
634 var->transp.msb_right = 0;
635 break;
636 case 32:
637 var->red.length = 8;
638 var->red.offset = 16;
639 var->red.msb_right = 0;
640
641 var->green.length = 8;
642 var->green.offset = 8;
643 var->green.msb_right = 0;
644
645 var->blue.length = 8;
646 var->blue.offset = 0;
647 var->blue.msb_right = 0;
648
649 var->transp.length = 8;
650 var->transp.offset = 24;
651 var->transp.msb_right = 0;
652
653 break;
654 }
655
656 var->height = -1;
657 var->width = -1;
658 var->grayscale = 0;
659
660 /* Copy nonstd field to/from sync for fbset usage */
661 var->sync |= var->nonstd;
662 var->nonstd |= var->sync;
663
664 adjust_aoi_size_position(var, info);
665 return 0;
666 }
667
668 static void set_fix(struct fb_info *info)
669 {
670 struct fb_fix_screeninfo *fix = &info->fix;
671 struct fb_var_screeninfo *var = &info->var;
672 struct mfb_info *mfbi = info->par;
673
674 strncpy(fix->id, mfbi->id, strlen(mfbi->id));
675 fix->line_length = var->xres_virtual * var->bits_per_pixel / 8;
676 fix->type = FB_TYPE_PACKED_PIXELS;
677 fix->accel = FB_ACCEL_NONE;
678 fix->visual = FB_VISUAL_TRUECOLOR;
679 fix->xpanstep = 1;
680 fix->ypanstep = 1;
681 }
682
683 static void update_lcdc(struct fb_info *info)
684 {
685 struct fb_var_screeninfo *var = &info->var;
686 struct mfb_info *mfbi = info->par;
687 struct fsl_diu_data *machine_data = mfbi->parent;
688 struct diu *hw;
689 int i, j;
690 char __iomem *cursor_base, *gamma_table_base;
691
692 u32 temp;
693
694 hw = dr.diu_reg;
695
696 if (mfbi->type == MFB_TYPE_OFF) {
697 fsl_diu_disable_panel(info);
698 return;
699 }
700
701 diu_ops.set_monitor_port(machine_data->monitor_port);
702 gamma_table_base = pool.gamma.vaddr;
703 cursor_base = pool.cursor.vaddr;
704 /* Prep for DIU init - gamma table, cursor table */
705
706 for (i = 0; i <= 2; i++)
707 for (j = 0; j <= 255; j++)
708 *gamma_table_base++ = j;
709
710 diu_ops.set_gamma_table(machine_data->monitor_port, pool.gamma.vaddr);
711
712 disable_lcdc(info);
713
714 /* Program DIU registers */
715
716 out_be32(&hw->gamma, pool.gamma.paddr);
717 out_be32(&hw->cursor, pool.cursor.paddr);
718
719 out_be32(&hw->bgnd, 0x007F7F7F); /* BGND */
720 out_be32(&hw->bgnd_wb, 0); /* BGND_WB */
721 out_be32(&hw->disp_size, (var->yres << 16 | var->xres));
722 /* DISP SIZE */
723 out_be32(&hw->wb_size, 0); /* WB SIZE */
724 out_be32(&hw->wb_mem_addr, 0); /* WB MEM ADDR */
725
726 /* Horizontal and vertical configuration register */
727 temp = var->left_margin << 22 | /* BP_H */
728 var->hsync_len << 11 | /* PW_H */
729 var->right_margin; /* FP_H */
730
731 out_be32(&hw->hsyn_para, temp);
732
733 temp = var->upper_margin << 22 | /* BP_V */
734 var->vsync_len << 11 | /* PW_V */
735 var->lower_margin; /* FP_V */
736
737 out_be32(&hw->vsyn_para, temp);
738
739 diu_ops.set_pixel_clock(var->pixclock);
740
741 out_be32(&hw->syn_pol, 0); /* SYNC SIGNALS POLARITY */
742 out_be32(&hw->thresholds, 0x00037800); /* The Thresholds */
743 out_be32(&hw->int_status, 0); /* INTERRUPT STATUS */
744 out_be32(&hw->plut, 0x01F5F666);
745
746 /* Enable the DIU */
747 enable_lcdc(info);
748 }
749
750 static int map_video_memory(struct fb_info *info)
751 {
752 phys_addr_t phys;
753 u32 smem_len = info->fix.line_length * info->var.yres_virtual;
754
755 info->screen_base = fsl_diu_alloc(smem_len, &phys);
756 if (info->screen_base == NULL) {
757 dev_err(info->dev, "unable to allocate fb memory\n");
758 return -ENOMEM;
759 }
760 mutex_lock(&info->mm_lock);
761 info->fix.smem_start = (unsigned long) phys;
762 info->fix.smem_len = smem_len;
763 mutex_unlock(&info->mm_lock);
764 info->screen_size = info->fix.smem_len;
765
766 return 0;
767 }
768
769 static void unmap_video_memory(struct fb_info *info)
770 {
771 fsl_diu_free(info->screen_base, info->fix.smem_len);
772 mutex_lock(&info->mm_lock);
773 info->screen_base = NULL;
774 info->fix.smem_start = 0;
775 info->fix.smem_len = 0;
776 mutex_unlock(&info->mm_lock);
777 }
778
779 /*
780 * Using the fb_var_screeninfo in fb_info we set the aoi of this
781 * particular framebuffer. It is a light version of fsl_diu_set_par.
782 */
783 static int fsl_diu_set_aoi(struct fb_info *info)
784 {
785 struct fb_var_screeninfo *var = &info->var;
786 struct mfb_info *mfbi = info->par;
787 struct diu_ad *ad = mfbi->ad;
788
789 /* AOI should not be greater than display size */
790 ad->offset_xyi = cpu_to_le32((var->yoffset << 16) | var->xoffset);
791 ad->offset_xyd = cpu_to_le32((mfbi->y_aoi_d << 16) | mfbi->x_aoi_d);
792 return 0;
793 }
794
795 /*
796 * Using the fb_var_screeninfo in fb_info we set the resolution of this
797 * particular framebuffer. This function alters the fb_fix_screeninfo stored
798 * in fb_info. It does not alter var in fb_info since we are using that
799 * data. This means we depend on the data in var inside fb_info to be
800 * supported by the hardware. fsl_diu_check_var is always called before
801 * fsl_diu_set_par to ensure this.
802 */
803 static int fsl_diu_set_par(struct fb_info *info)
804 {
805 unsigned long len;
806 struct fb_var_screeninfo *var = &info->var;
807 struct mfb_info *mfbi = info->par;
808 struct fsl_diu_data *machine_data = mfbi->parent;
809 struct diu_ad *ad = mfbi->ad;
810 struct diu *hw;
811
812 hw = dr.diu_reg;
813
814 set_fix(info);
815 mfbi->cursor_reset = 1;
816
817 len = info->var.yres_virtual * info->fix.line_length;
818 /* Alloc & dealloc each time resolution/bpp change */
819 if (len != info->fix.smem_len) {
820 if (info->fix.smem_start)
821 unmap_video_memory(info);
822
823 /* Memory allocation for framebuffer */
824 if (map_video_memory(info)) {
825 dev_err(info->dev, "unable to allocate fb memory 1\n");
826 return -ENOMEM;
827 }
828 }
829
830 ad->pix_fmt = diu_ops.get_pixel_format(machine_data->monitor_port,
831 var->bits_per_pixel);
832 ad->addr = cpu_to_le32(info->fix.smem_start);
833 ad->src_size_g_alpha = cpu_to_le32((var->yres_virtual << 12) |
834 var->xres_virtual) | mfbi->g_alpha;
835 /* AOI should not be greater than display size */
836 ad->aoi_size = cpu_to_le32((var->yres << 16) | var->xres);
837 ad->offset_xyi = cpu_to_le32((var->yoffset << 16) | var->xoffset);
838 ad->offset_xyd = cpu_to_le32((mfbi->y_aoi_d << 16) | mfbi->x_aoi_d);
839
840 /* Disable chroma keying function */
841 ad->ckmax_r = 0;
842 ad->ckmax_g = 0;
843 ad->ckmax_b = 0;
844
845 ad->ckmin_r = 255;
846 ad->ckmin_g = 255;
847 ad->ckmin_b = 255;
848
849 if (mfbi->index == 0)
850 update_lcdc(info);
851 return 0;
852 }
853
854 static inline __u32 CNVT_TOHW(__u32 val, __u32 width)
855 {
856 return ((val << width) + 0x7FFF - val) >> 16;
857 }
858
859 /*
860 * Set a single color register. The values supplied have a 16 bit magnitude
861 * which needs to be scaled in this function for the hardware. Things to take
862 * into consideration are how many color registers, if any, are supported with
863 * the current color visual. With truecolor mode no color palettes are
864 * supported. Here a pseudo palette is created which we store the value in
865 * pseudo_palette in struct fb_info. For pseudocolor mode we have a limited
866 * color palette.
867 */
868 static int fsl_diu_setcolreg(unsigned int regno, unsigned int red,
869 unsigned int green, unsigned int blue,
870 unsigned int transp, struct fb_info *info)
871 {
872 int ret = 1;
873
874 /*
875 * If greyscale is true, then we convert the RGB value
876 * to greyscale no matter what visual we are using.
877 */
878 if (info->var.grayscale)
879 red = green = blue = (19595 * red + 38470 * green +
880 7471 * blue) >> 16;
881 switch (info->fix.visual) {
882 case FB_VISUAL_TRUECOLOR:
883 /*
884 * 16-bit True Colour. We encode the RGB value
885 * according to the RGB bitfield information.
886 */
887 if (regno < 16) {
888 u32 *pal = info->pseudo_palette;
889 u32 v;
890
891 red = CNVT_TOHW(red, info->var.red.length);
892 green = CNVT_TOHW(green, info->var.green.length);
893 blue = CNVT_TOHW(blue, info->var.blue.length);
894 transp = CNVT_TOHW(transp, info->var.transp.length);
895
896 v = (red << info->var.red.offset) |
897 (green << info->var.green.offset) |
898 (blue << info->var.blue.offset) |
899 (transp << info->var.transp.offset);
900
901 pal[regno] = v;
902 ret = 0;
903 }
904 break;
905 }
906
907 return ret;
908 }
909
910 /*
911 * Pan (or wrap, depending on the `vmode' field) the display using the
912 * 'xoffset' and 'yoffset' fields of the 'var' structure. If the values
913 * don't fit, return -EINVAL.
914 */
915 static int fsl_diu_pan_display(struct fb_var_screeninfo *var,
916 struct fb_info *info)
917 {
918 if ((info->var.xoffset == var->xoffset) &&
919 (info->var.yoffset == var->yoffset))
920 return 0; /* No change, do nothing */
921
922 if (var->xoffset < 0 || var->yoffset < 0
923 || var->xoffset + info->var.xres > info->var.xres_virtual
924 || var->yoffset + info->var.yres > info->var.yres_virtual)
925 return -EINVAL;
926
927 info->var.xoffset = var->xoffset;
928 info->var.yoffset = var->yoffset;
929
930 if (var->vmode & FB_VMODE_YWRAP)
931 info->var.vmode |= FB_VMODE_YWRAP;
932 else
933 info->var.vmode &= ~FB_VMODE_YWRAP;
934
935 fsl_diu_set_aoi(info);
936
937 return 0;
938 }
939
940 /*
941 * Blank the screen if blank_mode != 0, else unblank. Return 0 if blanking
942 * succeeded, != 0 if un-/blanking failed.
943 * blank_mode == 2: suspend vsync
944 * blank_mode == 3: suspend hsync
945 * blank_mode == 4: powerdown
946 */
947 static int fsl_diu_blank(int blank_mode, struct fb_info *info)
948 {
949 struct mfb_info *mfbi = info->par;
950
951 mfbi->blank = blank_mode;
952
953 switch (blank_mode) {
954 case FB_BLANK_VSYNC_SUSPEND:
955 case FB_BLANK_HSYNC_SUSPEND:
956 /* FIXME: fixes to enable_panel and enable lcdc needed */
957 case FB_BLANK_NORMAL:
958 /* fsl_diu_disable_panel(info);*/
959 break;
960 case FB_BLANK_POWERDOWN:
961 /* disable_lcdc(info); */
962 break;
963 case FB_BLANK_UNBLANK:
964 /* fsl_diu_enable_panel(info);*/
965 break;
966 }
967
968 return 0;
969 }
970
971 static int fsl_diu_ioctl(struct fb_info *info, unsigned int cmd,
972 unsigned long arg)
973 {
974 struct mfb_info *mfbi = info->par;
975 struct diu_ad *ad = mfbi->ad;
976 struct mfb_chroma_key ck;
977 unsigned char global_alpha;
978 struct aoi_display_offset aoi_d;
979 __u32 pix_fmt;
980 void __user *buf = (void __user *)arg;
981
982 if (!arg)
983 return -EINVAL;
984 switch (cmd) {
985 case MFB_SET_PIXFMT:
986 if (copy_from_user(&pix_fmt, buf, sizeof(pix_fmt)))
987 return -EFAULT;
988 ad->pix_fmt = pix_fmt;
989 break;
990 case MFB_GET_PIXFMT:
991 pix_fmt = ad->pix_fmt;
992 if (copy_to_user(buf, &pix_fmt, sizeof(pix_fmt)))
993 return -EFAULT;
994 break;
995 case MFB_SET_AOID:
996 if (copy_from_user(&aoi_d, buf, sizeof(aoi_d)))
997 return -EFAULT;
998 mfbi->x_aoi_d = aoi_d.x_aoi_d;
999 mfbi->y_aoi_d = aoi_d.y_aoi_d;
1000 fsl_diu_check_var(&info->var, info);
1001 fsl_diu_set_aoi(info);
1002 break;
1003 case MFB_GET_AOID:
1004 aoi_d.x_aoi_d = mfbi->x_aoi_d;
1005 aoi_d.y_aoi_d = mfbi->y_aoi_d;
1006 if (copy_to_user(buf, &aoi_d, sizeof(aoi_d)))
1007 return -EFAULT;
1008 break;
1009 case MFB_GET_ALPHA:
1010 global_alpha = mfbi->g_alpha;
1011 if (copy_to_user(buf, &global_alpha, sizeof(global_alpha)))
1012 return -EFAULT;
1013 break;
1014 case MFB_SET_ALPHA:
1015 /* set panel information */
1016 if (copy_from_user(&global_alpha, buf, sizeof(global_alpha)))
1017 return -EFAULT;
1018 ad->src_size_g_alpha = (ad->src_size_g_alpha & (~0xff)) |
1019 (global_alpha & 0xff);
1020 mfbi->g_alpha = global_alpha;
1021 break;
1022 case MFB_SET_CHROMA_KEY:
1023 /* set panel winformation */
1024 if (copy_from_user(&ck, buf, sizeof(ck)))
1025 return -EFAULT;
1026
1027 if (ck.enable &&
1028 (ck.red_max < ck.red_min ||
1029 ck.green_max < ck.green_min ||
1030 ck.blue_max < ck.blue_min))
1031 return -EINVAL;
1032
1033 if (!ck.enable) {
1034 ad->ckmax_r = 0;
1035 ad->ckmax_g = 0;
1036 ad->ckmax_b = 0;
1037 ad->ckmin_r = 255;
1038 ad->ckmin_g = 255;
1039 ad->ckmin_b = 255;
1040 } else {
1041 ad->ckmax_r = ck.red_max;
1042 ad->ckmax_g = ck.green_max;
1043 ad->ckmax_b = ck.blue_max;
1044 ad->ckmin_r = ck.red_min;
1045 ad->ckmin_g = ck.green_min;
1046 ad->ckmin_b = ck.blue_min;
1047 }
1048 break;
1049 case FBIOGET_GWINFO:
1050 if (mfbi->type == MFB_TYPE_OFF)
1051 return -ENODEV;
1052 /* get graphic window information */
1053 if (copy_to_user(buf, ad, sizeof(*ad)))
1054 return -EFAULT;
1055 break;
1056
1057 default:
1058 dev_err(info->dev, "unknown ioctl command (0x%08X)\n", cmd);
1059 return -ENOIOCTLCMD;
1060 }
1061
1062 return 0;
1063 }
1064
1065 /* turn on fb if count == 1
1066 */
1067 static int fsl_diu_open(struct fb_info *info, int user)
1068 {
1069 struct mfb_info *mfbi = info->par;
1070 int res = 0;
1071
1072 /* free boot splash memory on first /dev/fb0 open */
1073 if (!mfbi->index && diu_ops.release_bootmem)
1074 diu_ops.release_bootmem();
1075
1076 spin_lock(&diu_lock);
1077 mfbi->count++;
1078 if (mfbi->count == 1) {
1079 fsl_diu_check_var(&info->var, info);
1080 res = fsl_diu_set_par(info);
1081 if (res < 0)
1082 mfbi->count--;
1083 else {
1084 res = fsl_diu_enable_panel(info);
1085 if (res < 0)
1086 mfbi->count--;
1087 }
1088 }
1089
1090 spin_unlock(&diu_lock);
1091 return res;
1092 }
1093
1094 /* turn off fb if count == 0
1095 */
1096 static int fsl_diu_release(struct fb_info *info, int user)
1097 {
1098 struct mfb_info *mfbi = info->par;
1099 int res = 0;
1100
1101 spin_lock(&diu_lock);
1102 mfbi->count--;
1103 if (mfbi->count == 0) {
1104 res = fsl_diu_disable_panel(info);
1105 if (res < 0)
1106 mfbi->count++;
1107 }
1108 spin_unlock(&diu_lock);
1109 return res;
1110 }
1111
1112 static struct fb_ops fsl_diu_ops = {
1113 .owner = THIS_MODULE,
1114 .fb_check_var = fsl_diu_check_var,
1115 .fb_set_par = fsl_diu_set_par,
1116 .fb_setcolreg = fsl_diu_setcolreg,
1117 .fb_blank = fsl_diu_blank,
1118 .fb_pan_display = fsl_diu_pan_display,
1119 .fb_fillrect = cfb_fillrect,
1120 .fb_copyarea = cfb_copyarea,
1121 .fb_imageblit = cfb_imageblit,
1122 .fb_ioctl = fsl_diu_ioctl,
1123 .fb_open = fsl_diu_open,
1124 .fb_release = fsl_diu_release,
1125 };
1126
1127 static int init_fbinfo(struct fb_info *info)
1128 {
1129 struct mfb_info *mfbi = info->par;
1130
1131 info->device = NULL;
1132 info->var.activate = FB_ACTIVATE_NOW;
1133 info->fbops = &fsl_diu_ops;
1134 info->flags = FBINFO_FLAG_DEFAULT;
1135 info->pseudo_palette = &mfbi->pseudo_palette;
1136
1137 /* Allocate colormap */
1138 fb_alloc_cmap(&info->cmap, 16, 0);
1139 return 0;
1140 }
1141
1142 static int __devinit install_fb(struct fb_info *info)
1143 {
1144 int rc;
1145 struct mfb_info *mfbi = info->par;
1146 const char *aoi_mode, *init_aoi_mode = "320x240";
1147 struct fb_videomode *db = fsl_diu_mode_db;
1148 unsigned int dbsize = ARRAY_SIZE(fsl_diu_mode_db);
1149 int has_default_mode = 1;
1150
1151 if (init_fbinfo(info))
1152 return -EINVAL;
1153
1154 if (mfbi->index == 0) { /* plane 0 */
1155 if (mfbi->edid_data) {
1156 /* Now build modedb from EDID */
1157 fb_edid_to_monspecs(mfbi->edid_data, &info->monspecs);
1158 fb_videomode_to_modelist(info->monspecs.modedb,
1159 info->monspecs.modedb_len,
1160 &info->modelist);
1161 db = info->monspecs.modedb;
1162 dbsize = info->monspecs.modedb_len;
1163 }
1164 aoi_mode = fb_mode;
1165 } else {
1166 aoi_mode = init_aoi_mode;
1167 }
1168 rc = fb_find_mode(&info->var, info, aoi_mode, db, dbsize,
1169 &fsl_diu_default_mode, default_bpp);
1170 if (!rc) {
1171 /*
1172 * For plane 0 we continue and look into
1173 * driver's internal modedb.
1174 */
1175 if (mfbi->index == 0 && mfbi->edid_data)
1176 has_default_mode = 0;
1177 else
1178 return -EINVAL;
1179 }
1180
1181 if (!has_default_mode) {
1182 rc = fb_find_mode(&info->var, info, aoi_mode, fsl_diu_mode_db,
1183 ARRAY_SIZE(fsl_diu_mode_db),
1184 &fsl_diu_default_mode,
1185 default_bpp);
1186 if (rc > 0 && rc < 5)
1187 has_default_mode = 1;
1188 }
1189
1190 /* Still not found, use preferred mode from database if any */
1191 if (!has_default_mode && info->monspecs.modedb) {
1192 struct fb_monspecs *specs = &info->monspecs;
1193 struct fb_videomode *modedb = &specs->modedb[0];
1194
1195 /*
1196 * Get preferred timing. If not found,
1197 * first mode in database will be used.
1198 */
1199 if (specs->misc & FB_MISC_1ST_DETAIL) {
1200 int i;
1201
1202 for (i = 0; i < specs->modedb_len; i++) {
1203 if (specs->modedb[i].flag & FB_MODE_IS_FIRST) {
1204 modedb = &specs->modedb[i];
1205 break;
1206 }
1207 }
1208 }
1209
1210 info->var.bits_per_pixel = default_bpp;
1211 fb_videomode_to_var(&info->var, modedb);
1212 }
1213
1214 if (mfbi->type == MFB_TYPE_OFF)
1215 mfbi->blank = FB_BLANK_NORMAL;
1216 else
1217 mfbi->blank = FB_BLANK_UNBLANK;
1218
1219 if (fsl_diu_check_var(&info->var, info)) {
1220 dev_err(info->dev, "fsl_diu_check_var failed\n");
1221 fb_dealloc_cmap(&info->cmap);
1222 return -EINVAL;
1223 }
1224
1225 if (register_framebuffer(info) < 0) {
1226 dev_err(info->dev, "register_framebuffer failed\n");
1227 unmap_video_memory(info);
1228 fb_dealloc_cmap(&info->cmap);
1229 return -EINVAL;
1230 }
1231
1232 mfbi->registered = 1;
1233 dev_info(info->dev, "%s registered successfully\n", mfbi->id);
1234
1235 return 0;
1236 }
1237
1238 static void uninstall_fb(struct fb_info *info)
1239 {
1240 struct mfb_info *mfbi = info->par;
1241
1242 if (!mfbi->registered)
1243 return;
1244
1245 if (mfbi->index == 0)
1246 kfree(mfbi->edid_data);
1247
1248 unregister_framebuffer(info);
1249 unmap_video_memory(info);
1250 if (&info->cmap)
1251 fb_dealloc_cmap(&info->cmap);
1252
1253 mfbi->registered = 0;
1254 }
1255
1256 static irqreturn_t fsl_diu_isr(int irq, void *dev_id)
1257 {
1258 struct diu *hw = dr.diu_reg;
1259 unsigned int status = in_be32(&hw->int_status);
1260
1261 if (status) {
1262 /* This is the workaround for underrun */
1263 if (status & INT_UNDRUN) {
1264 out_be32(&hw->diu_mode, 0);
1265 udelay(1);
1266 out_be32(&hw->diu_mode, 1);
1267 }
1268 #if defined(CONFIG_NOT_COHERENT_CACHE)
1269 else if (status & INT_VSYNC) {
1270 unsigned int i;
1271
1272 for (i = 0; i < coherence_data_size;
1273 i += d_cache_line_size)
1274 __asm__ __volatile__ (
1275 "dcbz 0, %[input]"
1276 ::[input]"r"(&coherence_data[i]));
1277 }
1278 #endif
1279 return IRQ_HANDLED;
1280 }
1281 return IRQ_NONE;
1282 }
1283
1284 static int request_irq_local(int irq)
1285 {
1286 unsigned long status, ints;
1287 struct diu *hw;
1288 int ret;
1289
1290 hw = dr.diu_reg;
1291
1292 /* Read to clear the status */
1293 status = in_be32(&hw->int_status);
1294
1295 ret = request_irq(irq, fsl_diu_isr, 0, "diu", NULL);
1296 if (!ret) {
1297 ints = INT_PARERR | INT_LS_BF_VS;
1298 #if !defined(CONFIG_NOT_COHERENT_CACHE)
1299 ints |= INT_VSYNC;
1300 #endif
1301
1302 if (dr.mode == MFB_MODE2 || dr.mode == MFB_MODE3)
1303 ints |= INT_VSYNC_WB;
1304
1305 /* Read to clear the status */
1306 status = in_be32(&hw->int_status);
1307 out_be32(&hw->int_mask, ints);
1308 }
1309
1310 return ret;
1311 }
1312
1313 static void free_irq_local(int irq)
1314 {
1315 struct diu *hw = dr.diu_reg;
1316
1317 /* Disable all LCDC interrupt */
1318 out_be32(&hw->int_mask, 0x1f);
1319
1320 free_irq(irq, NULL);
1321 }
1322
1323 #ifdef CONFIG_PM
1324 /*
1325 * Power management hooks. Note that we won't be called from IRQ context,
1326 * unlike the blank functions above, so we may sleep.
1327 */
1328 static int fsl_diu_suspend(struct platform_device *ofdev, pm_message_t state)
1329 {
1330 struct fsl_diu_data *machine_data;
1331
1332 machine_data = dev_get_drvdata(&ofdev->dev);
1333 disable_lcdc(machine_data->fsl_diu_info[0]);
1334
1335 return 0;
1336 }
1337
1338 static int fsl_diu_resume(struct platform_device *ofdev)
1339 {
1340 struct fsl_diu_data *machine_data;
1341
1342 machine_data = dev_get_drvdata(&ofdev->dev);
1343 enable_lcdc(machine_data->fsl_diu_info[0]);
1344
1345 return 0;
1346 }
1347
1348 #else
1349 #define fsl_diu_suspend NULL
1350 #define fsl_diu_resume NULL
1351 #endif /* CONFIG_PM */
1352
1353 /* Align to 64-bit(8-byte), 32-byte, etc. */
1354 static int allocate_buf(struct device *dev, struct diu_addr *buf, u32 size,
1355 u32 bytes_align)
1356 {
1357 u32 offset, ssize;
1358 u32 mask;
1359 dma_addr_t paddr = 0;
1360
1361 ssize = size + bytes_align;
1362 buf->vaddr = dma_alloc_coherent(dev, ssize, &paddr, GFP_DMA |
1363 __GFP_ZERO);
1364 if (!buf->vaddr)
1365 return -ENOMEM;
1366
1367 buf->paddr = (__u32) paddr;
1368
1369 mask = bytes_align - 1;
1370 offset = (u32)buf->paddr & mask;
1371 if (offset) {
1372 buf->offset = bytes_align - offset;
1373 buf->paddr = (u32)buf->paddr + offset;
1374 } else
1375 buf->offset = 0;
1376
1377 return 0;
1378 }
1379
1380 static void free_buf(struct device *dev, struct diu_addr *buf, u32 size,
1381 u32 bytes_align)
1382 {
1383 dma_free_coherent(dev, size + bytes_align, buf->vaddr,
1384 buf->paddr - buf->offset);
1385 }
1386
1387 static ssize_t store_monitor(struct device *device,
1388 struct device_attribute *attr, const char *buf, size_t count)
1389 {
1390 enum fsl_diu_monitor_port old_monitor_port;
1391 struct fsl_diu_data *machine_data =
1392 container_of(attr, struct fsl_diu_data, dev_attr);
1393
1394 old_monitor_port = machine_data->monitor_port;
1395 machine_data->monitor_port = fsl_diu_name_to_port(buf);
1396
1397 if (old_monitor_port != machine_data->monitor_port) {
1398 /* All AOIs need adjust pixel format
1399 * fsl_diu_set_par only change the pixsel format here
1400 * unlikely to fail. */
1401 fsl_diu_set_par(machine_data->fsl_diu_info[0]);
1402 fsl_diu_set_par(machine_data->fsl_diu_info[1]);
1403 fsl_diu_set_par(machine_data->fsl_diu_info[2]);
1404 fsl_diu_set_par(machine_data->fsl_diu_info[3]);
1405 fsl_diu_set_par(machine_data->fsl_diu_info[4]);
1406 }
1407 return count;
1408 }
1409
1410 static ssize_t show_monitor(struct device *device,
1411 struct device_attribute *attr, char *buf)
1412 {
1413 struct fsl_diu_data *machine_data =
1414 container_of(attr, struct fsl_diu_data, dev_attr);
1415
1416 switch (machine_data->monitor_port) {
1417 case FSL_DIU_PORT_DVI:
1418 return sprintf(buf, "DVI\n");
1419 case FSL_DIU_PORT_LVDS:
1420 return sprintf(buf, "Single-link LVDS\n");
1421 case FSL_DIU_PORT_DLVDS:
1422 return sprintf(buf, "Dual-link LVDS\n");
1423 }
1424
1425 return 0;
1426 }
1427
1428 static int __devinit fsl_diu_probe(struct platform_device *ofdev)
1429 {
1430 struct device_node *np = ofdev->dev.of_node;
1431 struct mfb_info *mfbi;
1432 phys_addr_t dummy_ad_addr = 0;
1433 int ret, i, error = 0;
1434 struct resource res;
1435 struct fsl_diu_data *machine_data;
1436 int diu_mode;
1437
1438 machine_data = kzalloc(sizeof(struct fsl_diu_data), GFP_KERNEL);
1439 if (!machine_data)
1440 return -ENOMEM;
1441
1442 for (i = 0; i < ARRAY_SIZE(machine_data->fsl_diu_info); i++) {
1443 machine_data->fsl_diu_info[i] =
1444 framebuffer_alloc(sizeof(struct mfb_info), &ofdev->dev);
1445 if (!machine_data->fsl_diu_info[i]) {
1446 dev_err(&ofdev->dev, "cannot allocate memory\n");
1447 ret = -ENOMEM;
1448 goto error2;
1449 }
1450 mfbi = machine_data->fsl_diu_info[i]->par;
1451 memcpy(mfbi, &mfb_template[i], sizeof(struct mfb_info));
1452 mfbi->parent = machine_data;
1453
1454 if (mfbi->index == 0) {
1455 const u8 *prop;
1456 int len;
1457
1458 /* Get EDID */
1459 prop = of_get_property(np, "edid", &len);
1460 if (prop && len == EDID_LENGTH)
1461 mfbi->edid_data = kmemdup(prop, EDID_LENGTH,
1462 GFP_KERNEL);
1463 }
1464 }
1465
1466 ret = of_address_to_resource(np, 0, &res);
1467 if (ret) {
1468 dev_err(&ofdev->dev, "could not obtain DIU address\n");
1469 goto error;
1470 }
1471 if (!res.start) {
1472 dev_err(&ofdev->dev, "invalid DIU address\n");
1473 goto error;
1474 }
1475
1476 dr.diu_reg = ioremap(res.start, sizeof(struct diu));
1477 if (!dr.diu_reg) {
1478 dev_err(&ofdev->dev, "Err: can't map DIU registers!\n");
1479 ret = -EFAULT;
1480 goto error2;
1481 }
1482
1483 diu_mode = in_be32(&dr.diu_reg->diu_mode);
1484 if (diu_mode != MFB_MODE1)
1485 out_be32(&dr.diu_reg->diu_mode, 0); /* disable DIU */
1486
1487 /* Get the IRQ of the DIU */
1488 machine_data->irq = irq_of_parse_and_map(np, 0);
1489
1490 if (!machine_data->irq) {
1491 dev_err(&ofdev->dev, "could not get DIU IRQ\n");
1492 ret = -EINVAL;
1493 goto error;
1494 }
1495 machine_data->monitor_port = monitor_port;
1496
1497 /* Area descriptor memory pool aligns to 64-bit boundary */
1498 if (allocate_buf(&ofdev->dev, &pool.ad,
1499 sizeof(struct diu_ad) * FSL_AOI_NUM, 8))
1500 return -ENOMEM;
1501
1502 /* Get memory for Gamma Table - 32-byte aligned memory */
1503 if (allocate_buf(&ofdev->dev, &pool.gamma, 768, 32)) {
1504 ret = -ENOMEM;
1505 goto error;
1506 }
1507
1508 /* For performance, cursor bitmap buffer aligns to 32-byte boundary */
1509 if (allocate_buf(&ofdev->dev, &pool.cursor, MAX_CURS * MAX_CURS * 2,
1510 32)) {
1511 ret = -ENOMEM;
1512 goto error;
1513 }
1514
1515 i = ARRAY_SIZE(machine_data->fsl_diu_info);
1516 machine_data->dummy_ad = (struct diu_ad *)
1517 ((u32)pool.ad.vaddr + pool.ad.offset) + i;
1518 machine_data->dummy_ad->paddr = pool.ad.paddr +
1519 i * sizeof(struct diu_ad);
1520 machine_data->dummy_aoi_virt = fsl_diu_alloc(64, &dummy_ad_addr);
1521 if (!machine_data->dummy_aoi_virt) {
1522 ret = -ENOMEM;
1523 goto error;
1524 }
1525 machine_data->dummy_ad->addr = cpu_to_le32(dummy_ad_addr);
1526 machine_data->dummy_ad->pix_fmt = 0x88882317;
1527 machine_data->dummy_ad->src_size_g_alpha = cpu_to_le32((4 << 12) | 4);
1528 machine_data->dummy_ad->aoi_size = cpu_to_le32((4 << 16) | 2);
1529 machine_data->dummy_ad->offset_xyi = 0;
1530 machine_data->dummy_ad->offset_xyd = 0;
1531 machine_data->dummy_ad->next_ad = 0;
1532
1533 /*
1534 * Let DIU display splash screen if it was pre-initialized
1535 * by the bootloader, set dummy area descriptor otherwise.
1536 */
1537 if (diu_mode != MFB_MODE1)
1538 out_be32(&dr.diu_reg->desc[0], machine_data->dummy_ad->paddr);
1539
1540 out_be32(&dr.diu_reg->desc[1], machine_data->dummy_ad->paddr);
1541 out_be32(&dr.diu_reg->desc[2], machine_data->dummy_ad->paddr);
1542
1543 for (i = 0; i < ARRAY_SIZE(machine_data->fsl_diu_info); i++) {
1544 machine_data->fsl_diu_info[i]->fix.smem_start = 0;
1545 mfbi = machine_data->fsl_diu_info[i]->par;
1546 mfbi->ad = (struct diu_ad *)((u32)pool.ad.vaddr
1547 + pool.ad.offset) + i;
1548 mfbi->ad->paddr = pool.ad.paddr + i * sizeof(struct diu_ad);
1549 ret = install_fb(machine_data->fsl_diu_info[i]);
1550 if (ret) {
1551 dev_err(&ofdev->dev,
1552 "Failed to register framebuffer %d\n",
1553 i);
1554 goto error;
1555 }
1556 }
1557
1558 if (request_irq_local(machine_data->irq)) {
1559 dev_err(machine_data->fsl_diu_info[0]->dev,
1560 "could not request irq for diu.");
1561 goto error;
1562 }
1563
1564 sysfs_attr_init(&machine_data->dev_attr.attr);
1565 machine_data->dev_attr.attr.name = "monitor";
1566 machine_data->dev_attr.attr.mode = S_IRUGO|S_IWUSR;
1567 machine_data->dev_attr.show = show_monitor;
1568 machine_data->dev_attr.store = store_monitor;
1569 error = device_create_file(machine_data->fsl_diu_info[0]->dev,
1570 &machine_data->dev_attr);
1571 if (error) {
1572 dev_err(machine_data->fsl_diu_info[0]->dev,
1573 "could not create sysfs %s file\n",
1574 machine_data->dev_attr.attr.name);
1575 }
1576
1577 dev_set_drvdata(&ofdev->dev, machine_data);
1578 return 0;
1579
1580 error:
1581 for (i = ARRAY_SIZE(machine_data->fsl_diu_info);
1582 i > 0; i--)
1583 uninstall_fb(machine_data->fsl_diu_info[i - 1]);
1584 if (pool.ad.vaddr)
1585 free_buf(&ofdev->dev, &pool.ad,
1586 sizeof(struct diu_ad) * FSL_AOI_NUM, 8);
1587 if (pool.gamma.vaddr)
1588 free_buf(&ofdev->dev, &pool.gamma, 768, 32);
1589 if (pool.cursor.vaddr)
1590 free_buf(&ofdev->dev, &pool.cursor, MAX_CURS * MAX_CURS * 2,
1591 32);
1592 if (machine_data->dummy_aoi_virt)
1593 fsl_diu_free(machine_data->dummy_aoi_virt, 64);
1594 iounmap(dr.diu_reg);
1595
1596 error2:
1597 for (i = 0; i < ARRAY_SIZE(machine_data->fsl_diu_info); i++)
1598 if (machine_data->fsl_diu_info[i])
1599 framebuffer_release(machine_data->fsl_diu_info[i]);
1600 kfree(machine_data);
1601
1602 return ret;
1603 }
1604
1605
1606 static int fsl_diu_remove(struct platform_device *ofdev)
1607 {
1608 struct fsl_diu_data *machine_data;
1609 int i;
1610
1611 machine_data = dev_get_drvdata(&ofdev->dev);
1612 disable_lcdc(machine_data->fsl_diu_info[0]);
1613 free_irq_local(machine_data->irq);
1614 for (i = ARRAY_SIZE(machine_data->fsl_diu_info); i > 0; i--)
1615 uninstall_fb(machine_data->fsl_diu_info[i - 1]);
1616 if (pool.ad.vaddr)
1617 free_buf(&ofdev->dev, &pool.ad,
1618 sizeof(struct diu_ad) * FSL_AOI_NUM, 8);
1619 if (pool.gamma.vaddr)
1620 free_buf(&ofdev->dev, &pool.gamma, 768, 32);
1621 if (pool.cursor.vaddr)
1622 free_buf(&ofdev->dev, &pool.cursor, MAX_CURS * MAX_CURS * 2,
1623 32);
1624 if (machine_data->dummy_aoi_virt)
1625 fsl_diu_free(machine_data->dummy_aoi_virt, 64);
1626 iounmap(dr.diu_reg);
1627 for (i = 0; i < ARRAY_SIZE(machine_data->fsl_diu_info); i++)
1628 if (machine_data->fsl_diu_info[i])
1629 framebuffer_release(machine_data->fsl_diu_info[i]);
1630 kfree(machine_data);
1631
1632 return 0;
1633 }
1634
1635 #ifndef MODULE
1636 static int __init fsl_diu_setup(char *options)
1637 {
1638 char *opt;
1639 unsigned long val;
1640
1641 if (!options || !*options)
1642 return 0;
1643
1644 while ((opt = strsep(&options, ",")) != NULL) {
1645 if (!*opt)
1646 continue;
1647 if (!strncmp(opt, "monitor=", 8)) {
1648 monitor_port = fsl_diu_name_to_port(opt + 8);
1649 } else if (!strncmp(opt, "bpp=", 4)) {
1650 if (!strict_strtoul(opt + 4, 10, &val))
1651 default_bpp = val;
1652 } else
1653 fb_mode = opt;
1654 }
1655
1656 return 0;
1657 }
1658 #endif
1659
1660 static struct of_device_id fsl_diu_match[] = {
1661 #ifdef CONFIG_PPC_MPC512x
1662 {
1663 .compatible = "fsl,mpc5121-diu",
1664 },
1665 #endif
1666 {
1667 .compatible = "fsl,diu",
1668 },
1669 {}
1670 };
1671 MODULE_DEVICE_TABLE(of, fsl_diu_match);
1672
1673 static struct platform_driver fsl_diu_driver = {
1674 .driver = {
1675 .name = "fsl_diu",
1676 .owner = THIS_MODULE,
1677 .of_match_table = fsl_diu_match,
1678 },
1679 .probe = fsl_diu_probe,
1680 .remove = fsl_diu_remove,
1681 .suspend = fsl_diu_suspend,
1682 .resume = fsl_diu_resume,
1683 };
1684
1685 static int __init fsl_diu_init(void)
1686 {
1687 #ifdef CONFIG_NOT_COHERENT_CACHE
1688 struct device_node *np;
1689 const u32 *prop;
1690 #endif
1691 int ret;
1692 #ifndef MODULE
1693 char *option;
1694
1695 /*
1696 * For kernel boot options (in 'video=xxxfb:<options>' format)
1697 */
1698 if (fb_get_options("fslfb", &option))
1699 return -ENODEV;
1700 fsl_diu_setup(option);
1701 #else
1702 monitor_port = fsl_diu_name_to_port(monitor_string);
1703 #endif
1704 pr_info("Freescale Display Interface Unit (DIU) framebuffer driver\n");
1705
1706 #ifdef CONFIG_NOT_COHERENT_CACHE
1707 np = of_find_node_by_type(NULL, "cpu");
1708 if (!np) {
1709 pr_err("fsl-diu-fb: can't find 'cpu' device node\n");
1710 return -ENODEV;
1711 }
1712
1713 prop = of_get_property(np, "d-cache-size", NULL);
1714 if (prop == NULL) {
1715 pr_err("fsl-diu-fb: missing 'd-cache-size' property' "
1716 "in 'cpu' node\n");
1717 of_node_put(np);
1718 return -ENODEV;
1719 }
1720
1721 /*
1722 * Freescale PLRU requires 13/8 times the cache size to do a proper
1723 * displacement flush
1724 */
1725 coherence_data_size = *prop * 13;
1726 coherence_data_size /= 8;
1727
1728 prop = of_get_property(np, "d-cache-line-size", NULL);
1729 if (prop == NULL) {
1730 pr_err("fsl-diu-fb: missing 'd-cache-line-size' property' "
1731 "in 'cpu' node\n");
1732 of_node_put(np);
1733 return -ENODEV;
1734 }
1735 d_cache_line_size = *prop;
1736
1737 of_node_put(np);
1738 coherence_data = vmalloc(coherence_data_size);
1739 if (!coherence_data)
1740 return -ENOMEM;
1741 #endif
1742
1743 ret = platform_driver_register(&fsl_diu_driver);
1744 if (ret) {
1745 pr_err("fsl-diu-fb: failed to register platform driver\n");
1746 #if defined(CONFIG_NOT_COHERENT_CACHE)
1747 vfree(coherence_data);
1748 #endif
1749 iounmap(dr.diu_reg);
1750 }
1751 return ret;
1752 }
1753
1754 static void __exit fsl_diu_exit(void)
1755 {
1756 platform_driver_unregister(&fsl_diu_driver);
1757 #if defined(CONFIG_NOT_COHERENT_CACHE)
1758 vfree(coherence_data);
1759 #endif
1760 }
1761
1762 module_init(fsl_diu_init);
1763 module_exit(fsl_diu_exit);
1764
1765 MODULE_AUTHOR("York Sun <yorksun@freescale.com>");
1766 MODULE_DESCRIPTION("Freescale DIU framebuffer driver");
1767 MODULE_LICENSE("GPL");
1768
1769 module_param_named(mode, fb_mode, charp, 0);
1770 MODULE_PARM_DESC(mode,
1771 "Specify resolution as \"<xres>x<yres>[-<bpp>][@<refresh>]\" ");
1772 module_param_named(bpp, default_bpp, ulong, 0);
1773 MODULE_PARM_DESC(bpp, "Specify bit-per-pixel if not specified in 'mode'");
1774 module_param_named(monitor, monitor_string, charp, 0);
1775 MODULE_PARM_DESC(monitor, "Specify the monitor port "
1776 "(\"dvi\", \"lvds\", or \"dlvds\") if supported by the platform");
1777
This page took 0.080453 seconds and 6 git commands to generate.