blob: c5f97318f19359447d2f964bd336648f29d96a8b [file] [log] [blame]
York Sun9b53a9e2008-04-28 02:15:34 -07001/*
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>
Timur Tabib715f9f2011-09-28 16:19:48 -050033#include <linux/spinlock.h>
York Sun9b53a9e2008-04-28 02:15:34 -070034
York Sun9b53a9e2008-04-28 02:15:34 -070035#include <sysdev/fsl_soc.h>
Anatolij Gustschin0814a972010-07-23 04:00:36 +000036#include <linux/fsl-diu-fb.h>
Anatolij Gustschin8b856f02010-07-23 04:00:39 +000037#include "edid.h"
York Sun9b53a9e2008-04-28 02:15:34 -070038
Timur Tabib715f9f2011-09-28 16:19:48 -050039#define FSL_AOI_NUM 6 /* 5 AOIs and one dummy AOI */
40 /* 1 for plane 0, 2 for plane 1&2 each */
41
42/* HW cursor parameters */
43#define MAX_CURS 32
44
45/* INT_STATUS/INT_MASK field descriptions */
46#define INT_VSYNC 0x01 /* Vsync interrupt */
47#define INT_VSYNC_WB 0x02 /* Vsync interrupt for write back operation */
48#define INT_UNDRUN 0x04 /* Under run exception interrupt */
49#define INT_PARERR 0x08 /* Display parameters error interrupt */
50#define INT_LS_BF_VS 0x10 /* Lines before vsync. interrupt */
51
52/* Panels'operation modes */
53#define MFB_TYPE_OUTPUT 0 /* Panel output to display */
54#define MFB_TYPE_OFF 1 /* Panel off */
55#define MFB_TYPE_WB 2 /* Panel written back to memory */
56#define MFB_TYPE_TEST 3 /* Panel generate color bar */
57
58struct diu_hw {
59 struct diu __iomem *diu_reg;
60 spinlock_t reg_lock;
61 unsigned int mode; /* DIU operation mode */
62};
63
64struct diu_addr {
65 void *vaddr; /* Virtual address */
66 dma_addr_t paddr; /* Physical address */
67 __u32 offset;
68};
69
70struct diu_pool {
71 struct diu_addr ad;
72 struct diu_addr gamma;
73 struct diu_addr pallete;
74 struct diu_addr cursor;
75};
76
York Sun9b53a9e2008-04-28 02:15:34 -070077/*
Timur Tabi63cf8df2011-09-15 16:44:51 -050078 * List of supported video modes
79 *
Timur Tabi760af8f2011-09-28 16:19:50 -050080 * The first entry is the default video mode. The remain entries are in
81 * order if increasing resolution and frequency. The 320x240-60 mode is
82 * the initial AOI for the second and third planes.
York Sun9b53a9e2008-04-28 02:15:34 -070083 */
York Sun9b53a9e2008-04-28 02:15:34 -070084static struct fb_videomode __devinitdata fsl_diu_mode_db[] = {
85 {
York Sun9b53a9e2008-04-28 02:15:34 -070086 .refresh = 60,
87 .xres = 1024,
88 .yres = 768,
89 .pixclock = 15385,
90 .left_margin = 160,
91 .right_margin = 24,
92 .upper_margin = 29,
93 .lower_margin = 3,
94 .hsync_len = 136,
95 .vsync_len = 6,
96 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
97 .vmode = FB_VMODE_NONINTERLACED
98 },
99 {
Timur Tabi760af8f2011-09-28 16:19:50 -0500100 .refresh = 60,
101 .xres = 320,
102 .yres = 240,
103 .pixclock = 79440,
104 .left_margin = 16,
105 .right_margin = 16,
106 .upper_margin = 16,
107 .lower_margin = 5,
108 .hsync_len = 48,
109 .vsync_len = 1,
110 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
111 .vmode = FB_VMODE_NONINTERLACED
112 },
113 {
114 .refresh = 60,
115 .xres = 640,
116 .yres = 480,
117 .pixclock = 39722,
118 .left_margin = 48,
119 .right_margin = 16,
120 .upper_margin = 33,
121 .lower_margin = 10,
122 .hsync_len = 96,
123 .vsync_len = 2,
124 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
125 .vmode = FB_VMODE_NONINTERLACED
126 },
127 {
128 .refresh = 72,
129 .xres = 640,
130 .yres = 480,
131 .pixclock = 32052,
132 .left_margin = 128,
133 .right_margin = 24,
134 .upper_margin = 28,
135 .lower_margin = 9,
136 .hsync_len = 40,
137 .vsync_len = 3,
138 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
139 .vmode = FB_VMODE_NONINTERLACED
140 },
141 {
142 .refresh = 75,
143 .xres = 640,
144 .yres = 480,
145 .pixclock = 31747,
146 .left_margin = 120,
147 .right_margin = 16,
148 .upper_margin = 16,
149 .lower_margin = 1,
150 .hsync_len = 64,
151 .vsync_len = 3,
152 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
153 .vmode = FB_VMODE_NONINTERLACED
154 },
155 {
156 .refresh = 90,
157 .xres = 640,
158 .yres = 480,
159 .pixclock = 25057,
160 .left_margin = 120,
161 .right_margin = 32,
162 .upper_margin = 14,
163 .lower_margin = 25,
164 .hsync_len = 40,
165 .vsync_len = 14,
166 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
167 .vmode = FB_VMODE_NONINTERLACED
168 },
169 {
170 .refresh = 100,
171 .xres = 640,
172 .yres = 480,
173 .pixclock = 22272,
174 .left_margin = 48,
175 .right_margin = 32,
176 .upper_margin = 17,
177 .lower_margin = 22,
178 .hsync_len = 128,
179 .vsync_len = 12,
180 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
181 .vmode = FB_VMODE_NONINTERLACED
182 },
183 {
184 .refresh = 60,
185 .xres = 800,
186 .yres = 480,
187 .pixclock = 33805,
188 .left_margin = 96,
189 .right_margin = 24,
190 .upper_margin = 10,
191 .lower_margin = 3,
192 .hsync_len = 72,
193 .vsync_len = 7,
194 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
195 .vmode = FB_VMODE_NONINTERLACED
196 },
197 {
198 .refresh = 60,
199 .xres = 800,
200 .yres = 600,
201 .pixclock = 25000,
202 .left_margin = 88,
203 .right_margin = 40,
204 .upper_margin = 23,
205 .lower_margin = 1,
206 .hsync_len = 128,
207 .vsync_len = 4,
208 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
209 .vmode = FB_VMODE_NONINTERLACED
210 },
211 {
212 .refresh = 60,
213 .xres = 854,
214 .yres = 480,
215 .pixclock = 31518,
216 .left_margin = 104,
217 .right_margin = 16,
218 .upper_margin = 13,
219 .lower_margin = 1,
220 .hsync_len = 88,
221 .vsync_len = 3,
222 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
223 .vmode = FB_VMODE_NONINTERLACED
224 },
225 {
York Sun9b53a9e2008-04-28 02:15:34 -0700226 .refresh = 70,
227 .xres = 1024,
228 .yres = 768,
229 .pixclock = 16886,
230 .left_margin = 3,
231 .right_margin = 3,
232 .upper_margin = 2,
233 .lower_margin = 2,
234 .hsync_len = 40,
235 .vsync_len = 18,
236 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
237 .vmode = FB_VMODE_NONINTERLACED
238 },
239 {
York Sun9b53a9e2008-04-28 02:15:34 -0700240 .refresh = 75,
241 .xres = 1024,
242 .yres = 768,
243 .pixclock = 15009,
244 .left_margin = 3,
245 .right_margin = 3,
246 .upper_margin = 2,
247 .lower_margin = 2,
248 .hsync_len = 80,
249 .vsync_len = 32,
250 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
251 .vmode = FB_VMODE_NONINTERLACED
252 },
253 {
Timur Tabi760af8f2011-09-28 16:19:50 -0500254 .refresh = 60,
255 .xres = 1280,
256 .yres = 480,
257 .pixclock = 18939,
258 .left_margin = 353,
259 .right_margin = 47,
260 .upper_margin = 39,
261 .lower_margin = 4,
262 .hsync_len = 8,
263 .vsync_len = 2,
264 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
265 .vmode = FB_VMODE_NONINTERLACED
266 },
267 {
268 .refresh = 60,
269 .xres = 1280,
270 .yres = 720,
271 .pixclock = 13426,
272 .left_margin = 192,
273 .right_margin = 64,
274 .upper_margin = 22,
275 .lower_margin = 1,
276 .hsync_len = 136,
277 .vsync_len = 3,
278 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
279 .vmode = FB_VMODE_NONINTERLACED
280 },
281 {
York Sun9b53a9e2008-04-28 02:15:34 -0700282 .refresh = 60,
283 .xres = 1280,
284 .yres = 1024,
285 .pixclock = 9375,
286 .left_margin = 38,
287 .right_margin = 128,
288 .upper_margin = 2,
289 .lower_margin = 7,
290 .hsync_len = 216,
291 .vsync_len = 37,
292 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
293 .vmode = FB_VMODE_NONINTERLACED
294 },
295 {
York Sun9b53a9e2008-04-28 02:15:34 -0700296 .refresh = 70,
297 .xres = 1280,
298 .yres = 1024,
299 .pixclock = 9380,
300 .left_margin = 6,
301 .right_margin = 6,
302 .upper_margin = 4,
303 .lower_margin = 4,
304 .hsync_len = 60,
305 .vsync_len = 94,
306 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
307 .vmode = FB_VMODE_NONINTERLACED
308 },
309 {
York Sun9b53a9e2008-04-28 02:15:34 -0700310 .refresh = 75,
311 .xres = 1280,
312 .yres = 1024,
313 .pixclock = 9380,
314 .left_margin = 6,
315 .right_margin = 6,
316 .upper_margin = 4,
317 .lower_margin = 4,
318 .hsync_len = 60,
319 .vsync_len = 15,
320 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
321 .vmode = FB_VMODE_NONINTERLACED
322 },
323 {
York Sun9b53a9e2008-04-28 02:15:34 -0700324 .refresh = 60,
Timur Tabi760af8f2011-09-28 16:19:50 -0500325 .xres = 1920,
326 .yres = 1080,
327 .pixclock = 5787,
328 .left_margin = 328,
329 .right_margin = 120,
330 .upper_margin = 34,
331 .lower_margin = 1,
332 .hsync_len = 208,
333 .vsync_len = 3,
York Sun9b53a9e2008-04-28 02:15:34 -0700334 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
335 .vmode = FB_VMODE_NONINTERLACED
336 },
337};
338
Timur Tabi760af8f2011-09-28 16:19:50 -0500339static char *fb_mode;
York Sun9b53a9e2008-04-28 02:15:34 -0700340static unsigned long default_bpp = 32;
Timur Tabi7653aaa2011-07-09 15:38:14 -0500341static enum fsl_diu_monitor_port monitor_port;
342static char *monitor_string;
York Sun9b53a9e2008-04-28 02:15:34 -0700343
344#if defined(CONFIG_NOT_COHERENT_CACHE)
345static u8 *coherence_data;
346static size_t coherence_data_size;
347static unsigned int d_cache_line_size;
348#endif
349
350static DEFINE_SPINLOCK(diu_lock);
351
352struct fsl_diu_data {
353 struct fb_info *fsl_diu_info[FSL_AOI_NUM - 1];
354 /*FSL_AOI_NUM has one dummy AOI */
355 struct device_attribute dev_attr;
356 struct diu_ad *dummy_ad;
357 void *dummy_aoi_virt;
358 unsigned int irq;
359 int fb_enabled;
Timur Tabi7653aaa2011-07-09 15:38:14 -0500360 enum fsl_diu_monitor_port monitor_port;
York Sun9b53a9e2008-04-28 02:15:34 -0700361};
362
Timur Tabi2572df92011-09-28 16:19:51 -0500363enum mfb_index {
364 PLANE0 = 0, /* Plane 0, only one AOI that fills the screen */
365 PLANE1_AOI0, /* Plane 1, first AOI */
366 PLANE1_AOI1, /* Plane 1, second AOI */
367 PLANE2_AOI0, /* Plane 2, first AOI */
368 PLANE2_AOI1, /* Plane 2, second AOI */
369};
370
York Sun9b53a9e2008-04-28 02:15:34 -0700371struct mfb_info {
Timur Tabi2572df92011-09-28 16:19:51 -0500372 enum mfb_index index;
York Sun9b53a9e2008-04-28 02:15:34 -0700373 int type;
374 char *id;
375 int registered;
York Sun9b53a9e2008-04-28 02:15:34 -0700376 unsigned long pseudo_palette[16];
377 struct diu_ad *ad;
378 int cursor_reset;
379 unsigned char g_alpha;
380 unsigned int count;
381 int x_aoi_d; /* aoi display x offset to physical screen */
382 int y_aoi_d; /* aoi display y offset to physical screen */
383 struct fsl_diu_data *parent;
Anatolij Gustschin8b856f02010-07-23 04:00:39 +0000384 u8 *edid_data;
York Sun9b53a9e2008-04-28 02:15:34 -0700385};
386
387
388static struct mfb_info mfb_template[] = {
Timur Tabi2572df92011-09-28 16:19:51 -0500389 {
390 .index = PLANE0,
Timur Tabi4a85dc8b2011-09-15 16:44:46 -0500391 .type = MFB_TYPE_OUTPUT,
392 .id = "Panel0",
393 .registered = 0,
394 .count = 0,
395 .x_aoi_d = 0,
396 .y_aoi_d = 0,
York Sun9b53a9e2008-04-28 02:15:34 -0700397 },
Timur Tabi2572df92011-09-28 16:19:51 -0500398 {
399 .index = PLANE1_AOI0,
Timur Tabi4a85dc8b2011-09-15 16:44:46 -0500400 .type = MFB_TYPE_OUTPUT,
401 .id = "Panel1 AOI0",
402 .registered = 0,
403 .g_alpha = 0xff,
404 .count = 0,
405 .x_aoi_d = 0,
406 .y_aoi_d = 0,
York Sun9b53a9e2008-04-28 02:15:34 -0700407 },
Timur Tabi2572df92011-09-28 16:19:51 -0500408 {
409 .index = PLANE1_AOI1,
Timur Tabi4a85dc8b2011-09-15 16:44:46 -0500410 .type = MFB_TYPE_OUTPUT,
411 .id = "Panel1 AOI1",
412 .registered = 0,
413 .g_alpha = 0xff,
414 .count = 0,
415 .x_aoi_d = 0,
416 .y_aoi_d = 480,
York Sun9b53a9e2008-04-28 02:15:34 -0700417 },
Timur Tabi2572df92011-09-28 16:19:51 -0500418 {
419 .index = PLANE2_AOI0,
Timur Tabi4a85dc8b2011-09-15 16:44:46 -0500420 .type = MFB_TYPE_OUTPUT,
421 .id = "Panel2 AOI0",
422 .registered = 0,
423 .g_alpha = 0xff,
424 .count = 0,
425 .x_aoi_d = 640,
426 .y_aoi_d = 0,
York Sun9b53a9e2008-04-28 02:15:34 -0700427 },
Timur Tabi2572df92011-09-28 16:19:51 -0500428 {
429 .index = PLANE2_AOI1,
Timur Tabi4a85dc8b2011-09-15 16:44:46 -0500430 .type = MFB_TYPE_OUTPUT,
431 .id = "Panel2 AOI1",
432 .registered = 0,
433 .g_alpha = 0xff,
434 .count = 0,
435 .x_aoi_d = 640,
436 .y_aoi_d = 480,
York Sun9b53a9e2008-04-28 02:15:34 -0700437 },
438};
439
440static struct diu_hw dr = {
441 .mode = MFB_MODE1,
442 .reg_lock = __SPIN_LOCK_UNLOCKED(diu_hw.reg_lock),
443};
444
445static struct diu_pool pool;
446
Timur Tabi6b51d512008-07-23 21:31:39 -0700447/**
Timur Tabi7653aaa2011-07-09 15:38:14 -0500448 * fsl_diu_name_to_port - convert a port name to a monitor port enum
449 *
450 * Takes the name of a monitor port ("dvi", "lvds", or "dlvds") and returns
451 * the enum fsl_diu_monitor_port that corresponds to that string.
452 *
453 * For compatibility with older versions, a number ("0", "1", or "2") is also
454 * supported.
455 *
456 * If the string is unknown, DVI is assumed.
457 *
458 * If the particular port is not supported by the platform, another port
459 * (platform-specific) is chosen instead.
460 */
461static enum fsl_diu_monitor_port fsl_diu_name_to_port(const char *s)
462{
463 enum fsl_diu_monitor_port port = FSL_DIU_PORT_DVI;
464 unsigned long val;
465
466 if (s) {
467 if (!strict_strtoul(s, 10, &val) && (val <= 2))
468 port = (enum fsl_diu_monitor_port) val;
469 else if (strncmp(s, "lvds", 4) == 0)
470 port = FSL_DIU_PORT_LVDS;
471 else if (strncmp(s, "dlvds", 5) == 0)
472 port = FSL_DIU_PORT_DLVDS;
473 }
474
475 return diu_ops.valid_monitor_port(port);
476}
477
478/**
Timur Tabi6b51d512008-07-23 21:31:39 -0700479 * fsl_diu_alloc - allocate memory for the DIU
480 * @size: number of bytes to allocate
481 * @param: returned physical address of memory
482 *
483 * This function allocates a physically-contiguous block of memory.
York Sun9b53a9e2008-04-28 02:15:34 -0700484 */
Timur Tabi6b51d512008-07-23 21:31:39 -0700485static void *fsl_diu_alloc(size_t size, phys_addr_t *phys)
York Sun9b53a9e2008-04-28 02:15:34 -0700486{
487 void *virt;
488
Timur Tabi6b51d512008-07-23 21:31:39 -0700489 virt = alloc_pages_exact(size, GFP_DMA | __GFP_ZERO);
Timur Tabi154152a2011-09-15 16:44:47 -0500490 if (virt)
York Sun9b53a9e2008-04-28 02:15:34 -0700491 *phys = virt_to_phys(virt);
York Sun9b53a9e2008-04-28 02:15:34 -0700492
493 return virt;
494}
495
Timur Tabi6b51d512008-07-23 21:31:39 -0700496/**
497 * fsl_diu_free - release DIU memory
498 * @virt: pointer returned by fsl_diu_alloc()
499 * @size: number of bytes allocated by fsl_diu_alloc()
500 *
501 * This function releases memory allocated by fsl_diu_alloc().
502 */
503static void fsl_diu_free(void *virt, size_t size)
York Sun9b53a9e2008-04-28 02:15:34 -0700504{
Timur Tabi6b51d512008-07-23 21:31:39 -0700505 if (virt && size)
506 free_pages_exact(virt, size);
York Sun9b53a9e2008-04-28 02:15:34 -0700507}
508
Anatolij Gustschin0d9dab32010-07-23 04:00:35 +0000509/*
510 * Workaround for failed writing desc register of planes.
511 * Needed with MPC5121 DIU rev 2.0 silicon.
512 */
513void wr_reg_wa(u32 *reg, u32 val)
514{
515 do {
516 out_be32(reg, val);
517 } while (in_be32(reg) != val);
518}
519
York Sun9b53a9e2008-04-28 02:15:34 -0700520static int fsl_diu_enable_panel(struct fb_info *info)
521{
522 struct mfb_info *pmfbi, *cmfbi, *mfbi = info->par;
523 struct diu *hw = dr.diu_reg;
524 struct diu_ad *ad = mfbi->ad;
525 struct fsl_diu_data *machine_data = mfbi->parent;
526 int res = 0;
527
York Sun9b53a9e2008-04-28 02:15:34 -0700528 if (mfbi->type != MFB_TYPE_OFF) {
529 switch (mfbi->index) {
Timur Tabi2572df92011-09-28 16:19:51 -0500530 case PLANE0:
York Sun9b53a9e2008-04-28 02:15:34 -0700531 if (hw->desc[0] != ad->paddr)
Anatolij Gustschin0d9dab32010-07-23 04:00:35 +0000532 wr_reg_wa(&hw->desc[0], ad->paddr);
York Sun9b53a9e2008-04-28 02:15:34 -0700533 break;
Timur Tabi2572df92011-09-28 16:19:51 -0500534 case PLANE1_AOI0:
York Sun9b53a9e2008-04-28 02:15:34 -0700535 cmfbi = machine_data->fsl_diu_info[2]->par;
536 if (hw->desc[1] != ad->paddr) { /* AOI0 closed */
537 if (cmfbi->count > 0) /* AOI1 open */
538 ad->next_ad =
539 cpu_to_le32(cmfbi->ad->paddr);
540 else
541 ad->next_ad = 0;
Anatolij Gustschin0d9dab32010-07-23 04:00:35 +0000542 wr_reg_wa(&hw->desc[1], ad->paddr);
York Sun9b53a9e2008-04-28 02:15:34 -0700543 }
544 break;
Timur Tabi2572df92011-09-28 16:19:51 -0500545 case PLANE2_AOI0:
York Sun9b53a9e2008-04-28 02:15:34 -0700546 cmfbi = machine_data->fsl_diu_info[4]->par;
547 if (hw->desc[2] != ad->paddr) { /* AOI0 closed */
548 if (cmfbi->count > 0) /* AOI1 open */
549 ad->next_ad =
550 cpu_to_le32(cmfbi->ad->paddr);
551 else
552 ad->next_ad = 0;
Anatolij Gustschin0d9dab32010-07-23 04:00:35 +0000553 wr_reg_wa(&hw->desc[2], ad->paddr);
York Sun9b53a9e2008-04-28 02:15:34 -0700554 }
555 break;
Timur Tabi2572df92011-09-28 16:19:51 -0500556 case PLANE1_AOI1:
York Sun9b53a9e2008-04-28 02:15:34 -0700557 pmfbi = machine_data->fsl_diu_info[1]->par;
558 ad->next_ad = 0;
559 if (hw->desc[1] == machine_data->dummy_ad->paddr)
Anatolij Gustschin0d9dab32010-07-23 04:00:35 +0000560 wr_reg_wa(&hw->desc[1], ad->paddr);
York Sun9b53a9e2008-04-28 02:15:34 -0700561 else /* AOI0 open */
562 pmfbi->ad->next_ad = cpu_to_le32(ad->paddr);
563 break;
Timur Tabi2572df92011-09-28 16:19:51 -0500564 case PLANE2_AOI1:
York Sun9b53a9e2008-04-28 02:15:34 -0700565 pmfbi = machine_data->fsl_diu_info[3]->par;
566 ad->next_ad = 0;
567 if (hw->desc[2] == machine_data->dummy_ad->paddr)
Anatolij Gustschin0d9dab32010-07-23 04:00:35 +0000568 wr_reg_wa(&hw->desc[2], ad->paddr);
York Sun9b53a9e2008-04-28 02:15:34 -0700569 else /* AOI0 was open */
570 pmfbi->ad->next_ad = cpu_to_le32(ad->paddr);
571 break;
York Sun9b53a9e2008-04-28 02:15:34 -0700572 }
573 } else
574 res = -EINVAL;
575 return res;
576}
577
Timur Tabi2572df92011-09-28 16:19:51 -0500578static void fsl_diu_disable_panel(struct fb_info *info)
York Sun9b53a9e2008-04-28 02:15:34 -0700579{
580 struct mfb_info *pmfbi, *cmfbi, *mfbi = info->par;
581 struct diu *hw = dr.diu_reg;
582 struct diu_ad *ad = mfbi->ad;
583 struct fsl_diu_data *machine_data = mfbi->parent;
York Sun9b53a9e2008-04-28 02:15:34 -0700584
585 switch (mfbi->index) {
Timur Tabi2572df92011-09-28 16:19:51 -0500586 case PLANE0:
York Sun9b53a9e2008-04-28 02:15:34 -0700587 if (hw->desc[0] != machine_data->dummy_ad->paddr)
Anatolij Gustschin0d9dab32010-07-23 04:00:35 +0000588 wr_reg_wa(&hw->desc[0], machine_data->dummy_ad->paddr);
York Sun9b53a9e2008-04-28 02:15:34 -0700589 break;
Timur Tabi2572df92011-09-28 16:19:51 -0500590 case PLANE1_AOI0:
York Sun9b53a9e2008-04-28 02:15:34 -0700591 cmfbi = machine_data->fsl_diu_info[2]->par;
592 if (cmfbi->count > 0) /* AOI1 is open */
Anatolij Gustschin0d9dab32010-07-23 04:00:35 +0000593 wr_reg_wa(&hw->desc[1], cmfbi->ad->paddr);
York Sun9b53a9e2008-04-28 02:15:34 -0700594 /* move AOI1 to the first */
595 else /* AOI1 was closed */
Anatolij Gustschin0d9dab32010-07-23 04:00:35 +0000596 wr_reg_wa(&hw->desc[1], machine_data->dummy_ad->paddr);
York Sun9b53a9e2008-04-28 02:15:34 -0700597 /* close AOI 0 */
598 break;
Timur Tabi2572df92011-09-28 16:19:51 -0500599 case PLANE2_AOI0:
York Sun9b53a9e2008-04-28 02:15:34 -0700600 cmfbi = machine_data->fsl_diu_info[4]->par;
601 if (cmfbi->count > 0) /* AOI1 is open */
Anatolij Gustschin0d9dab32010-07-23 04:00:35 +0000602 wr_reg_wa(&hw->desc[2], cmfbi->ad->paddr);
York Sun9b53a9e2008-04-28 02:15:34 -0700603 /* move AOI1 to the first */
604 else /* AOI1 was closed */
Anatolij Gustschin0d9dab32010-07-23 04:00:35 +0000605 wr_reg_wa(&hw->desc[2], machine_data->dummy_ad->paddr);
York Sun9b53a9e2008-04-28 02:15:34 -0700606 /* close AOI 0 */
607 break;
Timur Tabi2572df92011-09-28 16:19:51 -0500608 case PLANE1_AOI1:
York Sun9b53a9e2008-04-28 02:15:34 -0700609 pmfbi = machine_data->fsl_diu_info[1]->par;
610 if (hw->desc[1] != ad->paddr) {
611 /* AOI1 is not the first in the chain */
612 if (pmfbi->count > 0)
613 /* AOI0 is open, must be the first */
614 pmfbi->ad->next_ad = 0;
615 } else /* AOI1 is the first in the chain */
Anatolij Gustschin0d9dab32010-07-23 04:00:35 +0000616 wr_reg_wa(&hw->desc[1], machine_data->dummy_ad->paddr);
York Sun9b53a9e2008-04-28 02:15:34 -0700617 /* close AOI 1 */
618 break;
Timur Tabi2572df92011-09-28 16:19:51 -0500619 case PLANE2_AOI1:
York Sun9b53a9e2008-04-28 02:15:34 -0700620 pmfbi = machine_data->fsl_diu_info[3]->par;
621 if (hw->desc[2] != ad->paddr) {
622 /* AOI1 is not the first in the chain */
623 if (pmfbi->count > 0)
624 /* AOI0 is open, must be the first */
625 pmfbi->ad->next_ad = 0;
626 } else /* AOI1 is the first in the chain */
Anatolij Gustschin0d9dab32010-07-23 04:00:35 +0000627 wr_reg_wa(&hw->desc[2], machine_data->dummy_ad->paddr);
York Sun9b53a9e2008-04-28 02:15:34 -0700628 /* close AOI 1 */
629 break;
York Sun9b53a9e2008-04-28 02:15:34 -0700630 }
York Sun9b53a9e2008-04-28 02:15:34 -0700631}
632
633static void enable_lcdc(struct fb_info *info)
634{
635 struct diu *hw = dr.diu_reg;
636 struct mfb_info *mfbi = info->par;
637 struct fsl_diu_data *machine_data = mfbi->parent;
638
639 if (!machine_data->fb_enabled) {
640 out_be32(&hw->diu_mode, dr.mode);
641 machine_data->fb_enabled++;
642 }
643}
644
645static void disable_lcdc(struct fb_info *info)
646{
647 struct diu *hw = dr.diu_reg;
648 struct mfb_info *mfbi = info->par;
649 struct fsl_diu_data *machine_data = mfbi->parent;
650
651 if (machine_data->fb_enabled) {
652 out_be32(&hw->diu_mode, 0);
653 machine_data->fb_enabled = 0;
654 }
655}
656
657static void adjust_aoi_size_position(struct fb_var_screeninfo *var,
658 struct fb_info *info)
659{
660 struct mfb_info *lower_aoi_mfbi, *upper_aoi_mfbi, *mfbi = info->par;
661 struct fsl_diu_data *machine_data = mfbi->parent;
Timur Tabi2572df92011-09-28 16:19:51 -0500662 int available_height, upper_aoi_bottom;
663 enum mfb_index index = mfbi->index;
York Sun9b53a9e2008-04-28 02:15:34 -0700664 int lower_aoi_is_open, upper_aoi_is_open;
665 __u32 base_plane_width, base_plane_height, upper_aoi_height;
666
667 base_plane_width = machine_data->fsl_diu_info[0]->var.xres;
668 base_plane_height = machine_data->fsl_diu_info[0]->var.yres;
669
York Sunfdfaa48332008-08-15 00:40:29 -0700670 if (mfbi->x_aoi_d < 0)
671 mfbi->x_aoi_d = 0;
672 if (mfbi->y_aoi_d < 0)
673 mfbi->y_aoi_d = 0;
York Sun9b53a9e2008-04-28 02:15:34 -0700674 switch (index) {
Timur Tabi2572df92011-09-28 16:19:51 -0500675 case PLANE0:
York Sun9b53a9e2008-04-28 02:15:34 -0700676 if (mfbi->x_aoi_d != 0)
677 mfbi->x_aoi_d = 0;
678 if (mfbi->y_aoi_d != 0)
679 mfbi->y_aoi_d = 0;
680 break;
Timur Tabi2572df92011-09-28 16:19:51 -0500681 case PLANE1_AOI0:
682 case PLANE2_AOI0:
York Sun9b53a9e2008-04-28 02:15:34 -0700683 lower_aoi_mfbi = machine_data->fsl_diu_info[index+1]->par;
684 lower_aoi_is_open = lower_aoi_mfbi->count > 0 ? 1 : 0;
685 if (var->xres > base_plane_width)
686 var->xres = base_plane_width;
687 if ((mfbi->x_aoi_d + var->xres) > base_plane_width)
688 mfbi->x_aoi_d = base_plane_width - var->xres;
689
690 if (lower_aoi_is_open)
691 available_height = lower_aoi_mfbi->y_aoi_d;
692 else
693 available_height = base_plane_height;
694 if (var->yres > available_height)
695 var->yres = available_height;
696 if ((mfbi->y_aoi_d + var->yres) > available_height)
697 mfbi->y_aoi_d = available_height - var->yres;
698 break;
Timur Tabi2572df92011-09-28 16:19:51 -0500699 case PLANE1_AOI1:
700 case PLANE2_AOI1:
York Sun9b53a9e2008-04-28 02:15:34 -0700701 upper_aoi_mfbi = machine_data->fsl_diu_info[index-1]->par;
702 upper_aoi_height =
703 machine_data->fsl_diu_info[index-1]->var.yres;
704 upper_aoi_bottom = upper_aoi_mfbi->y_aoi_d + upper_aoi_height;
705 upper_aoi_is_open = upper_aoi_mfbi->count > 0 ? 1 : 0;
706 if (var->xres > base_plane_width)
707 var->xres = base_plane_width;
708 if ((mfbi->x_aoi_d + var->xres) > base_plane_width)
709 mfbi->x_aoi_d = base_plane_width - var->xres;
710 if (mfbi->y_aoi_d < 0)
711 mfbi->y_aoi_d = 0;
712 if (upper_aoi_is_open) {
713 if (mfbi->y_aoi_d < upper_aoi_bottom)
714 mfbi->y_aoi_d = upper_aoi_bottom;
715 available_height = base_plane_height
716 - upper_aoi_bottom;
717 } else
718 available_height = base_plane_height;
719 if (var->yres > available_height)
720 var->yres = available_height;
721 if ((mfbi->y_aoi_d + var->yres) > base_plane_height)
722 mfbi->y_aoi_d = base_plane_height - var->yres;
723 break;
724 }
725}
726/*
727 * Checks to see if the hardware supports the state requested by var passed
728 * in. This function does not alter the hardware state! If the var passed in
729 * is slightly off by what the hardware can support then we alter the var
730 * PASSED in to what we can do. If the hardware doesn't support mode change
731 * a -EINVAL will be returned by the upper layers.
732 */
733static int fsl_diu_check_var(struct fb_var_screeninfo *var,
734 struct fb_info *info)
735{
York Sun9b53a9e2008-04-28 02:15:34 -0700736 if (var->xres_virtual < var->xres)
737 var->xres_virtual = var->xres;
738 if (var->yres_virtual < var->yres)
739 var->yres_virtual = var->yres;
740
741 if (var->xoffset < 0)
742 var->xoffset = 0;
743
744 if (var->yoffset < 0)
745 var->yoffset = 0;
746
747 if (var->xoffset + info->var.xres > info->var.xres_virtual)
748 var->xoffset = info->var.xres_virtual - info->var.xres;
749
750 if (var->yoffset + info->var.yres > info->var.yres_virtual)
751 var->yoffset = info->var.yres_virtual - info->var.yres;
752
753 if ((var->bits_per_pixel != 32) && (var->bits_per_pixel != 24) &&
754 (var->bits_per_pixel != 16))
755 var->bits_per_pixel = default_bpp;
756
757 switch (var->bits_per_pixel) {
758 case 16:
759 var->red.length = 5;
760 var->red.offset = 11;
761 var->red.msb_right = 0;
762
763 var->green.length = 6;
764 var->green.offset = 5;
765 var->green.msb_right = 0;
766
767 var->blue.length = 5;
768 var->blue.offset = 0;
769 var->blue.msb_right = 0;
770
771 var->transp.length = 0;
772 var->transp.offset = 0;
773 var->transp.msb_right = 0;
774 break;
775 case 24:
776 var->red.length = 8;
777 var->red.offset = 0;
778 var->red.msb_right = 0;
779
780 var->green.length = 8;
781 var->green.offset = 8;
782 var->green.msb_right = 0;
783
784 var->blue.length = 8;
785 var->blue.offset = 16;
786 var->blue.msb_right = 0;
787
788 var->transp.length = 0;
789 var->transp.offset = 0;
790 var->transp.msb_right = 0;
791 break;
792 case 32:
793 var->red.length = 8;
794 var->red.offset = 16;
795 var->red.msb_right = 0;
796
797 var->green.length = 8;
798 var->green.offset = 8;
799 var->green.msb_right = 0;
800
801 var->blue.length = 8;
802 var->blue.offset = 0;
803 var->blue.msb_right = 0;
804
805 var->transp.length = 8;
806 var->transp.offset = 24;
807 var->transp.msb_right = 0;
808
809 break;
810 }
York Sun9b53a9e2008-04-28 02:15:34 -0700811
812 var->height = -1;
813 var->width = -1;
814 var->grayscale = 0;
815
816 /* Copy nonstd field to/from sync for fbset usage */
817 var->sync |= var->nonstd;
818 var->nonstd |= var->sync;
819
820 adjust_aoi_size_position(var, info);
821 return 0;
822}
823
824static void set_fix(struct fb_info *info)
825{
826 struct fb_fix_screeninfo *fix = &info->fix;
827 struct fb_var_screeninfo *var = &info->var;
828 struct mfb_info *mfbi = info->par;
829
Timur Tabiec02dd22011-09-15 16:44:54 -0500830 strncpy(fix->id, mfbi->id, sizeof(fix->id));
York Sun9b53a9e2008-04-28 02:15:34 -0700831 fix->line_length = var->xres_virtual * var->bits_per_pixel / 8;
832 fix->type = FB_TYPE_PACKED_PIXELS;
833 fix->accel = FB_ACCEL_NONE;
834 fix->visual = FB_VISUAL_TRUECOLOR;
835 fix->xpanstep = 1;
836 fix->ypanstep = 1;
837}
838
839static void update_lcdc(struct fb_info *info)
840{
841 struct fb_var_screeninfo *var = &info->var;
842 struct mfb_info *mfbi = info->par;
843 struct fsl_diu_data *machine_data = mfbi->parent;
844 struct diu *hw;
845 int i, j;
846 char __iomem *cursor_base, *gamma_table_base;
847
848 u32 temp;
849
850 hw = dr.diu_reg;
851
852 if (mfbi->type == MFB_TYPE_OFF) {
853 fsl_diu_disable_panel(info);
854 return;
855 }
856
857 diu_ops.set_monitor_port(machine_data->monitor_port);
858 gamma_table_base = pool.gamma.vaddr;
859 cursor_base = pool.cursor.vaddr;
860 /* Prep for DIU init - gamma table, cursor table */
861
862 for (i = 0; i <= 2; i++)
Timur Tabi4a85dc8b2011-09-15 16:44:46 -0500863 for (j = 0; j <= 255; j++)
864 *gamma_table_base++ = j;
York Sun9b53a9e2008-04-28 02:15:34 -0700865
866 diu_ops.set_gamma_table(machine_data->monitor_port, pool.gamma.vaddr);
867
York Sun9b53a9e2008-04-28 02:15:34 -0700868 disable_lcdc(info);
869
870 /* Program DIU registers */
871
872 out_be32(&hw->gamma, pool.gamma.paddr);
873 out_be32(&hw->cursor, pool.cursor.paddr);
874
875 out_be32(&hw->bgnd, 0x007F7F7F); /* BGND */
876 out_be32(&hw->bgnd_wb, 0); /* BGND_WB */
877 out_be32(&hw->disp_size, (var->yres << 16 | var->xres));
878 /* DISP SIZE */
York Sun9b53a9e2008-04-28 02:15:34 -0700879 out_be32(&hw->wb_size, 0); /* WB SIZE */
880 out_be32(&hw->wb_mem_addr, 0); /* WB MEM ADDR */
881
882 /* Horizontal and vertical configuration register */
883 temp = var->left_margin << 22 | /* BP_H */
884 var->hsync_len << 11 | /* PW_H */
885 var->right_margin; /* FP_H */
886
887 out_be32(&hw->hsyn_para, temp);
888
889 temp = var->upper_margin << 22 | /* BP_V */
890 var->vsync_len << 11 | /* PW_V */
891 var->lower_margin; /* FP_V */
892
893 out_be32(&hw->vsyn_para, temp);
894
York Sun9b53a9e2008-04-28 02:15:34 -0700895 diu_ops.set_pixel_clock(var->pixclock);
896
897 out_be32(&hw->syn_pol, 0); /* SYNC SIGNALS POLARITY */
898 out_be32(&hw->thresholds, 0x00037800); /* The Thresholds */
899 out_be32(&hw->int_status, 0); /* INTERRUPT STATUS */
900 out_be32(&hw->plut, 0x01F5F666);
901
902 /* Enable the DIU */
903 enable_lcdc(info);
904}
905
906static int map_video_memory(struct fb_info *info)
907{
908 phys_addr_t phys;
Krzysztof Helt537a1bf2009-06-30 11:41:29 -0700909 u32 smem_len = info->fix.line_length * info->var.yres_virtual;
York Sun9b53a9e2008-04-28 02:15:34 -0700910
Krzysztof Helt537a1bf2009-06-30 11:41:29 -0700911 info->screen_base = fsl_diu_alloc(smem_len, &phys);
Anton Vorontsov05946bc2008-07-04 09:59:38 -0700912 if (info->screen_base == NULL) {
Timur Tabi154152a2011-09-15 16:44:47 -0500913 dev_err(info->dev, "unable to allocate fb memory\n");
York Sun9b53a9e2008-04-28 02:15:34 -0700914 return -ENOMEM;
915 }
Krzysztof Helt537a1bf2009-06-30 11:41:29 -0700916 mutex_lock(&info->mm_lock);
York Sun9b53a9e2008-04-28 02:15:34 -0700917 info->fix.smem_start = (unsigned long) phys;
Krzysztof Helt537a1bf2009-06-30 11:41:29 -0700918 info->fix.smem_len = smem_len;
919 mutex_unlock(&info->mm_lock);
York Sun9b53a9e2008-04-28 02:15:34 -0700920 info->screen_size = info->fix.smem_len;
921
York Sun9b53a9e2008-04-28 02:15:34 -0700922 return 0;
923}
924
925static void unmap_video_memory(struct fb_info *info)
926{
927 fsl_diu_free(info->screen_base, info->fix.smem_len);
Krzysztof Helt537a1bf2009-06-30 11:41:29 -0700928 mutex_lock(&info->mm_lock);
Anton Vorontsov05946bc2008-07-04 09:59:38 -0700929 info->screen_base = NULL;
York Sun9b53a9e2008-04-28 02:15:34 -0700930 info->fix.smem_start = 0;
931 info->fix.smem_len = 0;
Krzysztof Helt537a1bf2009-06-30 11:41:29 -0700932 mutex_unlock(&info->mm_lock);
York Sun9b53a9e2008-04-28 02:15:34 -0700933}
934
935/*
York Sunae5591e2008-08-15 00:40:28 -0700936 * Using the fb_var_screeninfo in fb_info we set the aoi of this
937 * particular framebuffer. It is a light version of fsl_diu_set_par.
938 */
939static int fsl_diu_set_aoi(struct fb_info *info)
940{
941 struct fb_var_screeninfo *var = &info->var;
942 struct mfb_info *mfbi = info->par;
943 struct diu_ad *ad = mfbi->ad;
944
945 /* AOI should not be greater than display size */
946 ad->offset_xyi = cpu_to_le32((var->yoffset << 16) | var->xoffset);
947 ad->offset_xyd = cpu_to_le32((mfbi->y_aoi_d << 16) | mfbi->x_aoi_d);
948 return 0;
949}
950
951/*
York Sun9b53a9e2008-04-28 02:15:34 -0700952 * Using the fb_var_screeninfo in fb_info we set the resolution of this
953 * particular framebuffer. This function alters the fb_fix_screeninfo stored
954 * in fb_info. It does not alter var in fb_info since we are using that
955 * data. This means we depend on the data in var inside fb_info to be
956 * supported by the hardware. fsl_diu_check_var is always called before
957 * fsl_diu_set_par to ensure this.
958 */
959static int fsl_diu_set_par(struct fb_info *info)
960{
961 unsigned long len;
962 struct fb_var_screeninfo *var = &info->var;
963 struct mfb_info *mfbi = info->par;
964 struct fsl_diu_data *machine_data = mfbi->parent;
965 struct diu_ad *ad = mfbi->ad;
966 struct diu *hw;
967
968 hw = dr.diu_reg;
969
970 set_fix(info);
971 mfbi->cursor_reset = 1;
972
973 len = info->var.yres_virtual * info->fix.line_length;
974 /* Alloc & dealloc each time resolution/bpp change */
975 if (len != info->fix.smem_len) {
976 if (info->fix.smem_start)
977 unmap_video_memory(info);
York Sun9b53a9e2008-04-28 02:15:34 -0700978
979 /* Memory allocation for framebuffer */
980 if (map_video_memory(info)) {
Timur Tabi154152a2011-09-15 16:44:47 -0500981 dev_err(info->dev, "unable to allocate fb memory 1\n");
York Sun9b53a9e2008-04-28 02:15:34 -0700982 return -ENOMEM;
983 }
984 }
985
Timur Tabi7653aaa2011-07-09 15:38:14 -0500986 ad->pix_fmt = diu_ops.get_pixel_format(machine_data->monitor_port,
987 var->bits_per_pixel);
York Sun9b53a9e2008-04-28 02:15:34 -0700988 ad->addr = cpu_to_le32(info->fix.smem_start);
York Sunae5591e2008-08-15 00:40:28 -0700989 ad->src_size_g_alpha = cpu_to_le32((var->yres_virtual << 12) |
990 var->xres_virtual) | mfbi->g_alpha;
991 /* AOI should not be greater than display size */
York Sun9b53a9e2008-04-28 02:15:34 -0700992 ad->aoi_size = cpu_to_le32((var->yres << 16) | var->xres);
York Sunae5591e2008-08-15 00:40:28 -0700993 ad->offset_xyi = cpu_to_le32((var->yoffset << 16) | var->xoffset);
York Sun9b53a9e2008-04-28 02:15:34 -0700994 ad->offset_xyd = cpu_to_le32((mfbi->y_aoi_d << 16) | mfbi->x_aoi_d);
995
996 /* Disable chroma keying function */
997 ad->ckmax_r = 0;
998 ad->ckmax_g = 0;
999 ad->ckmax_b = 0;
1000
1001 ad->ckmin_r = 255;
1002 ad->ckmin_g = 255;
1003 ad->ckmin_b = 255;
1004
Timur Tabi2572df92011-09-28 16:19:51 -05001005 if (mfbi->index == PLANE0)
York Sun9b53a9e2008-04-28 02:15:34 -07001006 update_lcdc(info);
1007 return 0;
1008}
1009
1010static inline __u32 CNVT_TOHW(__u32 val, __u32 width)
1011{
Timur Tabi4a85dc8b2011-09-15 16:44:46 -05001012 return ((val << width) + 0x7FFF - val) >> 16;
York Sun9b53a9e2008-04-28 02:15:34 -07001013}
1014
1015/*
1016 * Set a single color register. The values supplied have a 16 bit magnitude
1017 * which needs to be scaled in this function for the hardware. Things to take
1018 * into consideration are how many color registers, if any, are supported with
1019 * the current color visual. With truecolor mode no color palettes are
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001020 * supported. Here a pseudo palette is created which we store the value in
York Sun9b53a9e2008-04-28 02:15:34 -07001021 * pseudo_palette in struct fb_info. For pseudocolor mode we have a limited
1022 * color palette.
1023 */
Timur Tabi4a85dc8b2011-09-15 16:44:46 -05001024static int fsl_diu_setcolreg(unsigned int regno, unsigned int red,
1025 unsigned int green, unsigned int blue,
1026 unsigned int transp, struct fb_info *info)
York Sun9b53a9e2008-04-28 02:15:34 -07001027{
1028 int ret = 1;
1029
1030 /*
1031 * If greyscale is true, then we convert the RGB value
1032 * to greyscale no matter what visual we are using.
1033 */
1034 if (info->var.grayscale)
1035 red = green = blue = (19595 * red + 38470 * green +
1036 7471 * blue) >> 16;
1037 switch (info->fix.visual) {
1038 case FB_VISUAL_TRUECOLOR:
1039 /*
1040 * 16-bit True Colour. We encode the RGB value
1041 * according to the RGB bitfield information.
1042 */
1043 if (regno < 16) {
1044 u32 *pal = info->pseudo_palette;
1045 u32 v;
1046
1047 red = CNVT_TOHW(red, info->var.red.length);
1048 green = CNVT_TOHW(green, info->var.green.length);
1049 blue = CNVT_TOHW(blue, info->var.blue.length);
1050 transp = CNVT_TOHW(transp, info->var.transp.length);
1051
1052 v = (red << info->var.red.offset) |
1053 (green << info->var.green.offset) |
1054 (blue << info->var.blue.offset) |
1055 (transp << info->var.transp.offset);
1056
1057 pal[regno] = v;
1058 ret = 0;
1059 }
1060 break;
York Sun9b53a9e2008-04-28 02:15:34 -07001061 }
1062
1063 return ret;
1064}
1065
1066/*
1067 * Pan (or wrap, depending on the `vmode' field) the display using the
1068 * 'xoffset' and 'yoffset' fields of the 'var' structure. If the values
1069 * don't fit, return -EINVAL.
1070 */
1071static int fsl_diu_pan_display(struct fb_var_screeninfo *var,
1072 struct fb_info *info)
1073{
1074 if ((info->var.xoffset == var->xoffset) &&
1075 (info->var.yoffset == var->yoffset))
1076 return 0; /* No change, do nothing */
1077
1078 if (var->xoffset < 0 || var->yoffset < 0
1079 || var->xoffset + info->var.xres > info->var.xres_virtual
1080 || var->yoffset + info->var.yres > info->var.yres_virtual)
1081 return -EINVAL;
1082
1083 info->var.xoffset = var->xoffset;
1084 info->var.yoffset = var->yoffset;
1085
1086 if (var->vmode & FB_VMODE_YWRAP)
1087 info->var.vmode |= FB_VMODE_YWRAP;
1088 else
1089 info->var.vmode &= ~FB_VMODE_YWRAP;
1090
York Sunae5591e2008-08-15 00:40:28 -07001091 fsl_diu_set_aoi(info);
1092
York Sun9b53a9e2008-04-28 02:15:34 -07001093 return 0;
1094}
1095
York Sun9b53a9e2008-04-28 02:15:34 -07001096static int fsl_diu_ioctl(struct fb_info *info, unsigned int cmd,
1097 unsigned long arg)
1098{
1099 struct mfb_info *mfbi = info->par;
1100 struct diu_ad *ad = mfbi->ad;
1101 struct mfb_chroma_key ck;
1102 unsigned char global_alpha;
1103 struct aoi_display_offset aoi_d;
1104 __u32 pix_fmt;
1105 void __user *buf = (void __user *)arg;
1106
1107 if (!arg)
1108 return -EINVAL;
1109 switch (cmd) {
Timur Tabi36b0b1d2011-10-04 19:36:44 -05001110 case MFB_SET_PIXFMT_OLD:
1111 dev_warn(info->dev,
1112 "MFB_SET_PIXFMT value of 0x%08x is deprecated.\n",
1113 MFB_SET_PIXFMT_OLD);
York Sun9b53a9e2008-04-28 02:15:34 -07001114 case MFB_SET_PIXFMT:
1115 if (copy_from_user(&pix_fmt, buf, sizeof(pix_fmt)))
1116 return -EFAULT;
1117 ad->pix_fmt = pix_fmt;
York Sun9b53a9e2008-04-28 02:15:34 -07001118 break;
Timur Tabi36b0b1d2011-10-04 19:36:44 -05001119 case MFB_GET_PIXFMT_OLD:
1120 dev_warn(info->dev,
1121 "MFB_GET_PIXFMT value of 0x%08x is deprecated.\n",
1122 MFB_GET_PIXFMT_OLD);
York Sun9b53a9e2008-04-28 02:15:34 -07001123 case MFB_GET_PIXFMT:
1124 pix_fmt = ad->pix_fmt;
1125 if (copy_to_user(buf, &pix_fmt, sizeof(pix_fmt)))
1126 return -EFAULT;
York Sun9b53a9e2008-04-28 02:15:34 -07001127 break;
1128 case MFB_SET_AOID:
1129 if (copy_from_user(&aoi_d, buf, sizeof(aoi_d)))
1130 return -EFAULT;
1131 mfbi->x_aoi_d = aoi_d.x_aoi_d;
1132 mfbi->y_aoi_d = aoi_d.y_aoi_d;
York Sun9b53a9e2008-04-28 02:15:34 -07001133 fsl_diu_check_var(&info->var, info);
York Sunae5591e2008-08-15 00:40:28 -07001134 fsl_diu_set_aoi(info);
York Sun9b53a9e2008-04-28 02:15:34 -07001135 break;
1136 case MFB_GET_AOID:
1137 aoi_d.x_aoi_d = mfbi->x_aoi_d;
1138 aoi_d.y_aoi_d = mfbi->y_aoi_d;
1139 if (copy_to_user(buf, &aoi_d, sizeof(aoi_d)))
1140 return -EFAULT;
York Sun9b53a9e2008-04-28 02:15:34 -07001141 break;
1142 case MFB_GET_ALPHA:
1143 global_alpha = mfbi->g_alpha;
1144 if (copy_to_user(buf, &global_alpha, sizeof(global_alpha)))
1145 return -EFAULT;
York Sun9b53a9e2008-04-28 02:15:34 -07001146 break;
1147 case MFB_SET_ALPHA:
1148 /* set panel information */
1149 if (copy_from_user(&global_alpha, buf, sizeof(global_alpha)))
1150 return -EFAULT;
1151 ad->src_size_g_alpha = (ad->src_size_g_alpha & (~0xff)) |
1152 (global_alpha & 0xff);
1153 mfbi->g_alpha = global_alpha;
York Sun9b53a9e2008-04-28 02:15:34 -07001154 break;
1155 case MFB_SET_CHROMA_KEY:
1156 /* set panel winformation */
1157 if (copy_from_user(&ck, buf, sizeof(ck)))
1158 return -EFAULT;
1159
1160 if (ck.enable &&
1161 (ck.red_max < ck.red_min ||
1162 ck.green_max < ck.green_min ||
1163 ck.blue_max < ck.blue_min))
1164 return -EINVAL;
1165
1166 if (!ck.enable) {
1167 ad->ckmax_r = 0;
1168 ad->ckmax_g = 0;
1169 ad->ckmax_b = 0;
1170 ad->ckmin_r = 255;
1171 ad->ckmin_g = 255;
1172 ad->ckmin_b = 255;
1173 } else {
1174 ad->ckmax_r = ck.red_max;
1175 ad->ckmax_g = ck.green_max;
1176 ad->ckmax_b = ck.blue_max;
1177 ad->ckmin_r = ck.red_min;
1178 ad->ckmin_g = ck.green_min;
1179 ad->ckmin_b = ck.blue_min;
1180 }
York Sun9b53a9e2008-04-28 02:15:34 -07001181 break;
York Sun9b53a9e2008-04-28 02:15:34 -07001182 default:
Timur Tabi154152a2011-09-15 16:44:47 -05001183 dev_err(info->dev, "unknown ioctl command (0x%08X)\n", cmd);
York Sun9b53a9e2008-04-28 02:15:34 -07001184 return -ENOIOCTLCMD;
1185 }
1186
1187 return 0;
1188}
1189
1190/* turn on fb if count == 1
1191 */
1192static int fsl_diu_open(struct fb_info *info, int user)
1193{
1194 struct mfb_info *mfbi = info->par;
1195 int res = 0;
1196
Anatolij Gustschin4b5006e2010-07-23 04:00:37 +00001197 /* free boot splash memory on first /dev/fb0 open */
Timur Tabi2572df92011-09-28 16:19:51 -05001198 if ((mfbi->index == PLANE0) && diu_ops.release_bootmem)
Anatolij Gustschin4b5006e2010-07-23 04:00:37 +00001199 diu_ops.release_bootmem();
1200
York Sun9b53a9e2008-04-28 02:15:34 -07001201 spin_lock(&diu_lock);
1202 mfbi->count++;
1203 if (mfbi->count == 1) {
York Sun9b53a9e2008-04-28 02:15:34 -07001204 fsl_diu_check_var(&info->var, info);
1205 res = fsl_diu_set_par(info);
1206 if (res < 0)
1207 mfbi->count--;
1208 else {
1209 res = fsl_diu_enable_panel(info);
1210 if (res < 0)
1211 mfbi->count--;
1212 }
1213 }
1214
1215 spin_unlock(&diu_lock);
1216 return res;
1217}
1218
1219/* turn off fb if count == 0
1220 */
1221static int fsl_diu_release(struct fb_info *info, int user)
1222{
1223 struct mfb_info *mfbi = info->par;
1224 int res = 0;
1225
1226 spin_lock(&diu_lock);
1227 mfbi->count--;
Timur Tabi2572df92011-09-28 16:19:51 -05001228 if (mfbi->count == 0)
1229 fsl_diu_disable_panel(info);
1230
York Sun9b53a9e2008-04-28 02:15:34 -07001231 spin_unlock(&diu_lock);
1232 return res;
1233}
1234
1235static struct fb_ops fsl_diu_ops = {
1236 .owner = THIS_MODULE,
1237 .fb_check_var = fsl_diu_check_var,
1238 .fb_set_par = fsl_diu_set_par,
1239 .fb_setcolreg = fsl_diu_setcolreg,
York Sun9b53a9e2008-04-28 02:15:34 -07001240 .fb_pan_display = fsl_diu_pan_display,
1241 .fb_fillrect = cfb_fillrect,
1242 .fb_copyarea = cfb_copyarea,
1243 .fb_imageblit = cfb_imageblit,
1244 .fb_ioctl = fsl_diu_ioctl,
1245 .fb_open = fsl_diu_open,
1246 .fb_release = fsl_diu_release,
1247};
1248
1249static int init_fbinfo(struct fb_info *info)
1250{
1251 struct mfb_info *mfbi = info->par;
1252
1253 info->device = NULL;
1254 info->var.activate = FB_ACTIVATE_NOW;
1255 info->fbops = &fsl_diu_ops;
1256 info->flags = FBINFO_FLAG_DEFAULT;
1257 info->pseudo_palette = &mfbi->pseudo_palette;
1258
1259 /* Allocate colormap */
1260 fb_alloc_cmap(&info->cmap, 16, 0);
1261 return 0;
1262}
1263
Anton Vorontsov05946bc2008-07-04 09:59:38 -07001264static int __devinit install_fb(struct fb_info *info)
York Sun9b53a9e2008-04-28 02:15:34 -07001265{
1266 int rc;
1267 struct mfb_info *mfbi = info->par;
1268 const char *aoi_mode, *init_aoi_mode = "320x240";
Anatolij Gustschin8b856f02010-07-23 04:00:39 +00001269 struct fb_videomode *db = fsl_diu_mode_db;
1270 unsigned int dbsize = ARRAY_SIZE(fsl_diu_mode_db);
1271 int has_default_mode = 1;
York Sun9b53a9e2008-04-28 02:15:34 -07001272
1273 if (init_fbinfo(info))
1274 return -EINVAL;
1275
Timur Tabi2572df92011-09-28 16:19:51 -05001276 if (mfbi->index == PLANE0) {
Anatolij Gustschin8b856f02010-07-23 04:00:39 +00001277 if (mfbi->edid_data) {
1278 /* Now build modedb from EDID */
1279 fb_edid_to_monspecs(mfbi->edid_data, &info->monspecs);
1280 fb_videomode_to_modelist(info->monspecs.modedb,
1281 info->monspecs.modedb_len,
1282 &info->modelist);
1283 db = info->monspecs.modedb;
1284 dbsize = info->monspecs.modedb_len;
1285 }
York Sun9b53a9e2008-04-28 02:15:34 -07001286 aoi_mode = fb_mode;
Anatolij Gustschin8b856f02010-07-23 04:00:39 +00001287 } else {
York Sun9b53a9e2008-04-28 02:15:34 -07001288 aoi_mode = init_aoi_mode;
Anatolij Gustschin8b856f02010-07-23 04:00:39 +00001289 }
Timur Tabi63cf8df2011-09-15 16:44:51 -05001290 rc = fb_find_mode(&info->var, info, aoi_mode, db, dbsize, NULL,
1291 default_bpp);
Timur Tabi154152a2011-09-15 16:44:47 -05001292 if (!rc) {
Anatolij Gustschin8b856f02010-07-23 04:00:39 +00001293 /*
1294 * For plane 0 we continue and look into
1295 * driver's internal modedb.
1296 */
Timur Tabi2572df92011-09-28 16:19:51 -05001297 if ((mfbi->index == PLANE0) && mfbi->edid_data)
Anatolij Gustschin8b856f02010-07-23 04:00:39 +00001298 has_default_mode = 0;
1299 else
1300 return -EINVAL;
York Sun9b53a9e2008-04-28 02:15:34 -07001301 }
1302
Anatolij Gustschin8b856f02010-07-23 04:00:39 +00001303 if (!has_default_mode) {
1304 rc = fb_find_mode(&info->var, info, aoi_mode, fsl_diu_mode_db,
Timur Tabi63cf8df2011-09-15 16:44:51 -05001305 ARRAY_SIZE(fsl_diu_mode_db), NULL, default_bpp);
1306 if (rc)
Anatolij Gustschin8b856f02010-07-23 04:00:39 +00001307 has_default_mode = 1;
1308 }
1309
1310 /* Still not found, use preferred mode from database if any */
1311 if (!has_default_mode && info->monspecs.modedb) {
1312 struct fb_monspecs *specs = &info->monspecs;
1313 struct fb_videomode *modedb = &specs->modedb[0];
1314
1315 /*
1316 * Get preferred timing. If not found,
1317 * first mode in database will be used.
1318 */
1319 if (specs->misc & FB_MISC_1ST_DETAIL) {
1320 int i;
1321
1322 for (i = 0; i < specs->modedb_len; i++) {
1323 if (specs->modedb[i].flag & FB_MODE_IS_FIRST) {
1324 modedb = &specs->modedb[i];
1325 break;
1326 }
1327 }
1328 }
1329
1330 info->var.bits_per_pixel = default_bpp;
1331 fb_videomode_to_var(&info->var, modedb);
1332 }
1333
York Sun9b53a9e2008-04-28 02:15:34 -07001334 if (fsl_diu_check_var(&info->var, info)) {
Timur Tabi154152a2011-09-15 16:44:47 -05001335 dev_err(info->dev, "fsl_diu_check_var failed\n");
Timur Tabi589c7972011-09-15 16:44:55 -05001336 unmap_video_memory(info);
York Sun9b53a9e2008-04-28 02:15:34 -07001337 fb_dealloc_cmap(&info->cmap);
1338 return -EINVAL;
1339 }
1340
York Sun9b53a9e2008-04-28 02:15:34 -07001341 if (register_framebuffer(info) < 0) {
Timur Tabi154152a2011-09-15 16:44:47 -05001342 dev_err(info->dev, "register_framebuffer failed\n");
York Sun9b53a9e2008-04-28 02:15:34 -07001343 unmap_video_memory(info);
1344 fb_dealloc_cmap(&info->cmap);
1345 return -EINVAL;
1346 }
1347
1348 mfbi->registered = 1;
Timur Tabi154152a2011-09-15 16:44:47 -05001349 dev_info(info->dev, "%s registered successfully\n", mfbi->id);
York Sun9b53a9e2008-04-28 02:15:34 -07001350
1351 return 0;
1352}
1353
Anton Vorontsov05946bc2008-07-04 09:59:38 -07001354static void uninstall_fb(struct fb_info *info)
York Sun9b53a9e2008-04-28 02:15:34 -07001355{
1356 struct mfb_info *mfbi = info->par;
1357
1358 if (!mfbi->registered)
1359 return;
1360
Timur Tabi2572df92011-09-28 16:19:51 -05001361 if (mfbi->index == PLANE0)
Anatolij Gustschin8b856f02010-07-23 04:00:39 +00001362 kfree(mfbi->edid_data);
1363
York Sun9b53a9e2008-04-28 02:15:34 -07001364 unregister_framebuffer(info);
1365 unmap_video_memory(info);
1366 if (&info->cmap)
1367 fb_dealloc_cmap(&info->cmap);
1368
1369 mfbi->registered = 0;
1370}
1371
1372static irqreturn_t fsl_diu_isr(int irq, void *dev_id)
1373{
1374 struct diu *hw = dr.diu_reg;
1375 unsigned int status = in_be32(&hw->int_status);
1376
1377 if (status) {
1378 /* This is the workaround for underrun */
1379 if (status & INT_UNDRUN) {
1380 out_be32(&hw->diu_mode, 0);
York Sun9b53a9e2008-04-28 02:15:34 -07001381 udelay(1);
1382 out_be32(&hw->diu_mode, 1);
1383 }
1384#if defined(CONFIG_NOT_COHERENT_CACHE)
1385 else if (status & INT_VSYNC) {
1386 unsigned int i;
Timur Tabi4a85dc8b2011-09-15 16:44:46 -05001387
York Sun9b53a9e2008-04-28 02:15:34 -07001388 for (i = 0; i < coherence_data_size;
1389 i += d_cache_line_size)
1390 __asm__ __volatile__ (
1391 "dcbz 0, %[input]"
1392 ::[input]"r"(&coherence_data[i]));
1393 }
1394#endif
1395 return IRQ_HANDLED;
1396 }
1397 return IRQ_NONE;
1398}
1399
1400static int request_irq_local(int irq)
1401{
Timur Tabibada04f2011-09-15 16:44:52 -05001402 u32 ints;
York Sun9b53a9e2008-04-28 02:15:34 -07001403 struct diu *hw;
1404 int ret;
1405
1406 hw = dr.diu_reg;
1407
1408 /* Read to clear the status */
Timur Tabibada04f2011-09-15 16:44:52 -05001409 in_be32(&hw->int_status);
York Sun9b53a9e2008-04-28 02:15:34 -07001410
Timur Tabif8c6bf62011-09-15 16:44:53 -05001411 ret = request_irq(irq, fsl_diu_isr, 0, "fsl-diu-fb", NULL);
Timur Tabi154152a2011-09-15 16:44:47 -05001412 if (!ret) {
York Sun9b53a9e2008-04-28 02:15:34 -07001413 ints = INT_PARERR | INT_LS_BF_VS;
1414#if !defined(CONFIG_NOT_COHERENT_CACHE)
1415 ints |= INT_VSYNC;
1416#endif
Timur Tabi4a85dc8b2011-09-15 16:44:46 -05001417
York Sun9b53a9e2008-04-28 02:15:34 -07001418 if (dr.mode == MFB_MODE2 || dr.mode == MFB_MODE3)
1419 ints |= INT_VSYNC_WB;
1420
1421 /* Read to clear the status */
Timur Tabibada04f2011-09-15 16:44:52 -05001422 in_be32(&hw->int_status);
York Sun9b53a9e2008-04-28 02:15:34 -07001423 out_be32(&hw->int_mask, ints);
1424 }
Timur Tabi4a85dc8b2011-09-15 16:44:46 -05001425
York Sun9b53a9e2008-04-28 02:15:34 -07001426 return ret;
1427}
1428
1429static void free_irq_local(int irq)
1430{
1431 struct diu *hw = dr.diu_reg;
1432
1433 /* Disable all LCDC interrupt */
1434 out_be32(&hw->int_mask, 0x1f);
1435
Anton Vorontsov05946bc2008-07-04 09:59:38 -07001436 free_irq(irq, NULL);
York Sun9b53a9e2008-04-28 02:15:34 -07001437}
1438
1439#ifdef CONFIG_PM
1440/*
1441 * Power management hooks. Note that we won't be called from IRQ context,
1442 * unlike the blank functions above, so we may sleep.
1443 */
Grant Likely2dc11582010-08-06 09:25:50 -06001444static int fsl_diu_suspend(struct platform_device *ofdev, pm_message_t state)
York Sun9b53a9e2008-04-28 02:15:34 -07001445{
1446 struct fsl_diu_data *machine_data;
1447
Takashi Iwai48948a32008-07-08 18:41:17 +02001448 machine_data = dev_get_drvdata(&ofdev->dev);
York Sun9b53a9e2008-04-28 02:15:34 -07001449 disable_lcdc(machine_data->fsl_diu_info[0]);
1450
1451 return 0;
1452}
1453
Grant Likely2dc11582010-08-06 09:25:50 -06001454static int fsl_diu_resume(struct platform_device *ofdev)
York Sun9b53a9e2008-04-28 02:15:34 -07001455{
1456 struct fsl_diu_data *machine_data;
1457
Takashi Iwai48948a32008-07-08 18:41:17 +02001458 machine_data = dev_get_drvdata(&ofdev->dev);
York Sun9b53a9e2008-04-28 02:15:34 -07001459 enable_lcdc(machine_data->fsl_diu_info[0]);
1460
1461 return 0;
1462}
1463
1464#else
1465#define fsl_diu_suspend NULL
1466#define fsl_diu_resume NULL
1467#endif /* CONFIG_PM */
1468
1469/* Align to 64-bit(8-byte), 32-byte, etc. */
Anton Vorontsovf3791882009-04-04 22:31:20 +04001470static int allocate_buf(struct device *dev, struct diu_addr *buf, u32 size,
1471 u32 bytes_align)
York Sun9b53a9e2008-04-28 02:15:34 -07001472{
Timur Tabibada04f2011-09-15 16:44:52 -05001473 u32 offset;
1474 dma_addr_t mask;
York Sun9b53a9e2008-04-28 02:15:34 -07001475
Timur Tabibada04f2011-09-15 16:44:52 -05001476 buf->vaddr =
1477 dma_alloc_coherent(dev, size + bytes_align, &buf->paddr,
1478 GFP_DMA | __GFP_ZERO);
York Sun9b53a9e2008-04-28 02:15:34 -07001479 if (!buf->vaddr)
1480 return -ENOMEM;
1481
York Sun9b53a9e2008-04-28 02:15:34 -07001482 mask = bytes_align - 1;
Timur Tabibada04f2011-09-15 16:44:52 -05001483 offset = buf->paddr & mask;
York Sun9b53a9e2008-04-28 02:15:34 -07001484 if (offset) {
1485 buf->offset = bytes_align - offset;
Timur Tabibada04f2011-09-15 16:44:52 -05001486 buf->paddr = buf->paddr + offset;
York Sun9b53a9e2008-04-28 02:15:34 -07001487 } else
1488 buf->offset = 0;
Timur Tabi4a85dc8b2011-09-15 16:44:46 -05001489
York Sun9b53a9e2008-04-28 02:15:34 -07001490 return 0;
1491}
1492
Anton Vorontsovf3791882009-04-04 22:31:20 +04001493static void free_buf(struct device *dev, struct diu_addr *buf, u32 size,
1494 u32 bytes_align)
York Sun9b53a9e2008-04-28 02:15:34 -07001495{
Timur Tabi4a85dc8b2011-09-15 16:44:46 -05001496 dma_free_coherent(dev, size + bytes_align, buf->vaddr,
1497 buf->paddr - buf->offset);
York Sun9b53a9e2008-04-28 02:15:34 -07001498}
1499
1500static ssize_t store_monitor(struct device *device,
1501 struct device_attribute *attr, const char *buf, size_t count)
1502{
Timur Tabi7653aaa2011-07-09 15:38:14 -05001503 enum fsl_diu_monitor_port old_monitor_port;
York Sun9b53a9e2008-04-28 02:15:34 -07001504 struct fsl_diu_data *machine_data =
1505 container_of(attr, struct fsl_diu_data, dev_attr);
1506
York Sun9b53a9e2008-04-28 02:15:34 -07001507 old_monitor_port = machine_data->monitor_port;
Timur Tabi7653aaa2011-07-09 15:38:14 -05001508 machine_data->monitor_port = fsl_diu_name_to_port(buf);
York Sun9b53a9e2008-04-28 02:15:34 -07001509
1510 if (old_monitor_port != machine_data->monitor_port) {
1511 /* All AOIs need adjust pixel format
1512 * fsl_diu_set_par only change the pixsel format here
1513 * unlikely to fail. */
1514 fsl_diu_set_par(machine_data->fsl_diu_info[0]);
1515 fsl_diu_set_par(machine_data->fsl_diu_info[1]);
1516 fsl_diu_set_par(machine_data->fsl_diu_info[2]);
1517 fsl_diu_set_par(machine_data->fsl_diu_info[3]);
1518 fsl_diu_set_par(machine_data->fsl_diu_info[4]);
1519 }
1520 return count;
1521}
1522
1523static ssize_t show_monitor(struct device *device,
1524 struct device_attribute *attr, char *buf)
1525{
1526 struct fsl_diu_data *machine_data =
1527 container_of(attr, struct fsl_diu_data, dev_attr);
Timur Tabi7653aaa2011-07-09 15:38:14 -05001528
1529 switch (machine_data->monitor_port) {
1530 case FSL_DIU_PORT_DVI:
1531 return sprintf(buf, "DVI\n");
1532 case FSL_DIU_PORT_LVDS:
1533 return sprintf(buf, "Single-link LVDS\n");
1534 case FSL_DIU_PORT_DLVDS:
1535 return sprintf(buf, "Dual-link LVDS\n");
1536 }
1537
1538 return 0;
York Sun9b53a9e2008-04-28 02:15:34 -07001539}
1540
Timur Tabi9e52ba62011-09-15 16:44:50 -05001541static int __devinit fsl_diu_probe(struct platform_device *pdev)
York Sun9b53a9e2008-04-28 02:15:34 -07001542{
Timur Tabi9e52ba62011-09-15 16:44:50 -05001543 struct device_node *np = pdev->dev.of_node;
York Sun9b53a9e2008-04-28 02:15:34 -07001544 struct mfb_info *mfbi;
Timur Tabi89f08e32011-09-15 16:44:49 -05001545 phys_addr_t dummy_ad_addr = 0;
York Sun9b53a9e2008-04-28 02:15:34 -07001546 int ret, i, error = 0;
York Sun9b53a9e2008-04-28 02:15:34 -07001547 struct fsl_diu_data *machine_data;
Anatolij Gustschin4b5006e2010-07-23 04:00:37 +00001548 int diu_mode;
York Sun9b53a9e2008-04-28 02:15:34 -07001549
1550 machine_data = kzalloc(sizeof(struct fsl_diu_data), GFP_KERNEL);
1551 if (!machine_data)
1552 return -ENOMEM;
1553
1554 for (i = 0; i < ARRAY_SIZE(machine_data->fsl_diu_info); i++) {
1555 machine_data->fsl_diu_info[i] =
Timur Tabi9e52ba62011-09-15 16:44:50 -05001556 framebuffer_alloc(sizeof(struct mfb_info), &pdev->dev);
York Sun9b53a9e2008-04-28 02:15:34 -07001557 if (!machine_data->fsl_diu_info[i]) {
Timur Tabi9e52ba62011-09-15 16:44:50 -05001558 dev_err(&pdev->dev, "cannot allocate memory\n");
York Sun9b53a9e2008-04-28 02:15:34 -07001559 ret = -ENOMEM;
1560 goto error2;
1561 }
1562 mfbi = machine_data->fsl_diu_info[i]->par;
1563 memcpy(mfbi, &mfb_template[i], sizeof(struct mfb_info));
1564 mfbi->parent = machine_data;
Anatolij Gustschin8b856f02010-07-23 04:00:39 +00001565
Timur Tabi2572df92011-09-28 16:19:51 -05001566 if (mfbi->index == PLANE0) {
Anatolij Gustschin8b856f02010-07-23 04:00:39 +00001567 const u8 *prop;
1568 int len;
1569
1570 /* Get EDID */
1571 prop = of_get_property(np, "edid", &len);
1572 if (prop && len == EDID_LENGTH)
1573 mfbi->edid_data = kmemdup(prop, EDID_LENGTH,
1574 GFP_KERNEL);
1575 }
York Sun9b53a9e2008-04-28 02:15:34 -07001576 }
1577
Timur Tabi9e52ba62011-09-15 16:44:50 -05001578 dr.diu_reg = of_iomap(np, 0);
York Sun9b53a9e2008-04-28 02:15:34 -07001579 if (!dr.diu_reg) {
Timur Tabi9e52ba62011-09-15 16:44:50 -05001580 dev_err(&pdev->dev, "cannot map DIU registers\n");
York Sun9b53a9e2008-04-28 02:15:34 -07001581 ret = -EFAULT;
1582 goto error2;
1583 }
1584
Anatolij Gustschin4b5006e2010-07-23 04:00:37 +00001585 diu_mode = in_be32(&dr.diu_reg->diu_mode);
1586 if (diu_mode != MFB_MODE1)
1587 out_be32(&dr.diu_reg->diu_mode, 0); /* disable DIU */
York Sun9b53a9e2008-04-28 02:15:34 -07001588
1589 /* Get the IRQ of the DIU */
1590 machine_data->irq = irq_of_parse_and_map(np, 0);
1591
1592 if (!machine_data->irq) {
Timur Tabi9e52ba62011-09-15 16:44:50 -05001593 dev_err(&pdev->dev, "could not get DIU IRQ\n");
York Sun9b53a9e2008-04-28 02:15:34 -07001594 ret = -EINVAL;
1595 goto error;
1596 }
1597 machine_data->monitor_port = monitor_port;
1598
1599 /* Area descriptor memory pool aligns to 64-bit boundary */
Timur Tabi9e52ba62011-09-15 16:44:50 -05001600 if (allocate_buf(&pdev->dev, &pool.ad,
Anton Vorontsovf3791882009-04-04 22:31:20 +04001601 sizeof(struct diu_ad) * FSL_AOI_NUM, 8))
York Sun9b53a9e2008-04-28 02:15:34 -07001602 return -ENOMEM;
1603
1604 /* Get memory for Gamma Table - 32-byte aligned memory */
Timur Tabi9e52ba62011-09-15 16:44:50 -05001605 if (allocate_buf(&pdev->dev, &pool.gamma, 768, 32)) {
York Sun9b53a9e2008-04-28 02:15:34 -07001606 ret = -ENOMEM;
1607 goto error;
1608 }
1609
1610 /* For performance, cursor bitmap buffer aligns to 32-byte boundary */
Timur Tabi9e52ba62011-09-15 16:44:50 -05001611 if (allocate_buf(&pdev->dev, &pool.cursor, MAX_CURS * MAX_CURS * 2,
Anton Vorontsovf3791882009-04-04 22:31:20 +04001612 32)) {
York Sun9b53a9e2008-04-28 02:15:34 -07001613 ret = -ENOMEM;
1614 goto error;
1615 }
1616
1617 i = ARRAY_SIZE(machine_data->fsl_diu_info);
1618 machine_data->dummy_ad = (struct diu_ad *)
1619 ((u32)pool.ad.vaddr + pool.ad.offset) + i;
1620 machine_data->dummy_ad->paddr = pool.ad.paddr +
1621 i * sizeof(struct diu_ad);
1622 machine_data->dummy_aoi_virt = fsl_diu_alloc(64, &dummy_ad_addr);
1623 if (!machine_data->dummy_aoi_virt) {
1624 ret = -ENOMEM;
1625 goto error;
1626 }
1627 machine_data->dummy_ad->addr = cpu_to_le32(dummy_ad_addr);
1628 machine_data->dummy_ad->pix_fmt = 0x88882317;
1629 machine_data->dummy_ad->src_size_g_alpha = cpu_to_le32((4 << 12) | 4);
1630 machine_data->dummy_ad->aoi_size = cpu_to_le32((4 << 16) | 2);
1631 machine_data->dummy_ad->offset_xyi = 0;
1632 machine_data->dummy_ad->offset_xyd = 0;
1633 machine_data->dummy_ad->next_ad = 0;
1634
Anatolij Gustschin4b5006e2010-07-23 04:00:37 +00001635 /*
1636 * Let DIU display splash screen if it was pre-initialized
1637 * by the bootloader, set dummy area descriptor otherwise.
1638 */
1639 if (diu_mode != MFB_MODE1)
1640 out_be32(&dr.diu_reg->desc[0], machine_data->dummy_ad->paddr);
1641
York Sun9b53a9e2008-04-28 02:15:34 -07001642 out_be32(&dr.diu_reg->desc[1], machine_data->dummy_ad->paddr);
1643 out_be32(&dr.diu_reg->desc[2], machine_data->dummy_ad->paddr);
1644
1645 for (i = 0; i < ARRAY_SIZE(machine_data->fsl_diu_info); i++) {
1646 machine_data->fsl_diu_info[i]->fix.smem_start = 0;
1647 mfbi = machine_data->fsl_diu_info[i]->par;
1648 mfbi->ad = (struct diu_ad *)((u32)pool.ad.vaddr
1649 + pool.ad.offset) + i;
1650 mfbi->ad->paddr = pool.ad.paddr + i * sizeof(struct diu_ad);
1651 ret = install_fb(machine_data->fsl_diu_info[i]);
1652 if (ret) {
Timur Tabi9e52ba62011-09-15 16:44:50 -05001653 dev_err(&pdev->dev, "could not register fb %d\n", i);
York Sun9b53a9e2008-04-28 02:15:34 -07001654 goto error;
1655 }
1656 }
1657
1658 if (request_irq_local(machine_data->irq)) {
Timur Tabi9e52ba62011-09-15 16:44:50 -05001659 dev_err(&pdev->dev, "could not claim irq\n");
York Sun9b53a9e2008-04-28 02:15:34 -07001660 goto error;
1661 }
1662
Wolfram Sang12765512010-04-06 14:34:52 -07001663 sysfs_attr_init(&machine_data->dev_attr.attr);
York Sun9b53a9e2008-04-28 02:15:34 -07001664 machine_data->dev_attr.attr.name = "monitor";
1665 machine_data->dev_attr.attr.mode = S_IRUGO|S_IWUSR;
1666 machine_data->dev_attr.show = show_monitor;
1667 machine_data->dev_attr.store = store_monitor;
1668 error = device_create_file(machine_data->fsl_diu_info[0]->dev,
1669 &machine_data->dev_attr);
1670 if (error) {
Timur Tabi9e52ba62011-09-15 16:44:50 -05001671 dev_err(&pdev->dev, "could not create sysfs file %s\n",
York Sun9b53a9e2008-04-28 02:15:34 -07001672 machine_data->dev_attr.attr.name);
1673 }
1674
Timur Tabi9e52ba62011-09-15 16:44:50 -05001675 dev_set_drvdata(&pdev->dev, machine_data);
York Sun9b53a9e2008-04-28 02:15:34 -07001676 return 0;
1677
1678error:
Timur Tabi3f78bbd2011-09-15 16:44:56 -05001679 for (i = 0; i < ARRAY_SIZE(machine_data->fsl_diu_info); i++)
1680 uninstall_fb(machine_data->fsl_diu_info[i]);
1681
York Sun9b53a9e2008-04-28 02:15:34 -07001682 if (pool.ad.vaddr)
Timur Tabi9e52ba62011-09-15 16:44:50 -05001683 free_buf(&pdev->dev, &pool.ad,
Anton Vorontsovf3791882009-04-04 22:31:20 +04001684 sizeof(struct diu_ad) * FSL_AOI_NUM, 8);
York Sun9b53a9e2008-04-28 02:15:34 -07001685 if (pool.gamma.vaddr)
Timur Tabi9e52ba62011-09-15 16:44:50 -05001686 free_buf(&pdev->dev, &pool.gamma, 768, 32);
York Sun9b53a9e2008-04-28 02:15:34 -07001687 if (pool.cursor.vaddr)
Timur Tabi9e52ba62011-09-15 16:44:50 -05001688 free_buf(&pdev->dev, &pool.cursor, MAX_CURS * MAX_CURS * 2,
Anton Vorontsovf3791882009-04-04 22:31:20 +04001689 32);
York Sun9b53a9e2008-04-28 02:15:34 -07001690 if (machine_data->dummy_aoi_virt)
1691 fsl_diu_free(machine_data->dummy_aoi_virt, 64);
1692 iounmap(dr.diu_reg);
1693
1694error2:
1695 for (i = 0; i < ARRAY_SIZE(machine_data->fsl_diu_info); i++)
1696 if (machine_data->fsl_diu_info[i])
1697 framebuffer_release(machine_data->fsl_diu_info[i]);
1698 kfree(machine_data);
1699
1700 return ret;
1701}
1702
Timur Tabi9e52ba62011-09-15 16:44:50 -05001703static int fsl_diu_remove(struct platform_device *pdev)
York Sun9b53a9e2008-04-28 02:15:34 -07001704{
1705 struct fsl_diu_data *machine_data;
1706 int i;
1707
Timur Tabi9e52ba62011-09-15 16:44:50 -05001708 machine_data = dev_get_drvdata(&pdev->dev);
York Sun9b53a9e2008-04-28 02:15:34 -07001709 disable_lcdc(machine_data->fsl_diu_info[0]);
1710 free_irq_local(machine_data->irq);
Timur Tabi3f78bbd2011-09-15 16:44:56 -05001711 for (i = 0; i < ARRAY_SIZE(machine_data->fsl_diu_info); i++)
1712 uninstall_fb(machine_data->fsl_diu_info[i]);
York Sun9b53a9e2008-04-28 02:15:34 -07001713 if (pool.ad.vaddr)
Timur Tabi9e52ba62011-09-15 16:44:50 -05001714 free_buf(&pdev->dev, &pool.ad,
Anton Vorontsovf3791882009-04-04 22:31:20 +04001715 sizeof(struct diu_ad) * FSL_AOI_NUM, 8);
York Sun9b53a9e2008-04-28 02:15:34 -07001716 if (pool.gamma.vaddr)
Timur Tabi9e52ba62011-09-15 16:44:50 -05001717 free_buf(&pdev->dev, &pool.gamma, 768, 32);
York Sun9b53a9e2008-04-28 02:15:34 -07001718 if (pool.cursor.vaddr)
Timur Tabi9e52ba62011-09-15 16:44:50 -05001719 free_buf(&pdev->dev, &pool.cursor, MAX_CURS * MAX_CURS * 2, 32);
York Sun9b53a9e2008-04-28 02:15:34 -07001720 if (machine_data->dummy_aoi_virt)
1721 fsl_diu_free(machine_data->dummy_aoi_virt, 64);
1722 iounmap(dr.diu_reg);
1723 for (i = 0; i < ARRAY_SIZE(machine_data->fsl_diu_info); i++)
1724 if (machine_data->fsl_diu_info[i])
1725 framebuffer_release(machine_data->fsl_diu_info[i]);
1726 kfree(machine_data);
1727
1728 return 0;
1729}
1730
1731#ifndef MODULE
1732static int __init fsl_diu_setup(char *options)
1733{
1734 char *opt;
1735 unsigned long val;
1736
1737 if (!options || !*options)
1738 return 0;
1739
1740 while ((opt = strsep(&options, ",")) != NULL) {
1741 if (!*opt)
1742 continue;
1743 if (!strncmp(opt, "monitor=", 8)) {
Timur Tabi7653aaa2011-07-09 15:38:14 -05001744 monitor_port = fsl_diu_name_to_port(opt + 8);
York Sun9b53a9e2008-04-28 02:15:34 -07001745 } else if (!strncmp(opt, "bpp=", 4)) {
1746 if (!strict_strtoul(opt + 4, 10, &val))
1747 default_bpp = val;
1748 } else
1749 fb_mode = opt;
1750 }
1751
1752 return 0;
1753}
1754#endif
1755
1756static struct of_device_id fsl_diu_match[] = {
Anatolij Gustschind24720a2010-02-17 07:33:22 -07001757#ifdef CONFIG_PPC_MPC512x
1758 {
1759 .compatible = "fsl,mpc5121-diu",
1760 },
1761#endif
York Sun9b53a9e2008-04-28 02:15:34 -07001762 {
1763 .compatible = "fsl,diu",
1764 },
1765 {}
1766};
1767MODULE_DEVICE_TABLE(of, fsl_diu_match);
1768
Grant Likely28541d02011-02-22 21:07:43 -07001769static struct platform_driver fsl_diu_driver = {
Grant Likely40182942010-04-13 16:13:02 -07001770 .driver = {
Timur Tabif8c6bf62011-09-15 16:44:53 -05001771 .name = "fsl-diu-fb",
Grant Likely40182942010-04-13 16:13:02 -07001772 .owner = THIS_MODULE,
1773 .of_match_table = fsl_diu_match,
1774 },
York Sun9b53a9e2008-04-28 02:15:34 -07001775 .probe = fsl_diu_probe,
1776 .remove = fsl_diu_remove,
1777 .suspend = fsl_diu_suspend,
1778 .resume = fsl_diu_resume,
1779};
1780
1781static int __init fsl_diu_init(void)
1782{
1783#ifdef CONFIG_NOT_COHERENT_CACHE
1784 struct device_node *np;
1785 const u32 *prop;
1786#endif
1787 int ret;
1788#ifndef MODULE
1789 char *option;
1790
1791 /*
1792 * For kernel boot options (in 'video=xxxfb:<options>' format)
1793 */
1794 if (fb_get_options("fslfb", &option))
1795 return -ENODEV;
1796 fsl_diu_setup(option);
Timur Tabi7653aaa2011-07-09 15:38:14 -05001797#else
1798 monitor_port = fsl_diu_name_to_port(monitor_string);
York Sun9b53a9e2008-04-28 02:15:34 -07001799#endif
Timur Tabi154152a2011-09-15 16:44:47 -05001800 pr_info("Freescale Display Interface Unit (DIU) framebuffer driver\n");
York Sun9b53a9e2008-04-28 02:15:34 -07001801
1802#ifdef CONFIG_NOT_COHERENT_CACHE
1803 np = of_find_node_by_type(NULL, "cpu");
1804 if (!np) {
Timur Tabi154152a2011-09-15 16:44:47 -05001805 pr_err("fsl-diu-fb: can't find 'cpu' device node\n");
York Sun9b53a9e2008-04-28 02:15:34 -07001806 return -ENODEV;
1807 }
1808
1809 prop = of_get_property(np, "d-cache-size", NULL);
Julia Lawall5394ba02008-08-05 13:01:28 -07001810 if (prop == NULL) {
Timur Tabi154152a2011-09-15 16:44:47 -05001811 pr_err("fsl-diu-fb: missing 'd-cache-size' property' "
1812 "in 'cpu' node\n");
Julia Lawall5394ba02008-08-05 13:01:28 -07001813 of_node_put(np);
York Sun9b53a9e2008-04-28 02:15:34 -07001814 return -ENODEV;
Julia Lawall5394ba02008-08-05 13:01:28 -07001815 }
York Sun9b53a9e2008-04-28 02:15:34 -07001816
Timur Tabi4a85dc8b2011-09-15 16:44:46 -05001817 /*
1818 * Freescale PLRU requires 13/8 times the cache size to do a proper
1819 * displacement flush
York Sun9b53a9e2008-04-28 02:15:34 -07001820 */
Timur Tabi9e52ba62011-09-15 16:44:50 -05001821 coherence_data_size = be32_to_cpup(prop) * 13;
York Sun9b53a9e2008-04-28 02:15:34 -07001822 coherence_data_size /= 8;
1823
1824 prop = of_get_property(np, "d-cache-line-size", NULL);
Julia Lawall5394ba02008-08-05 13:01:28 -07001825 if (prop == NULL) {
Timur Tabi154152a2011-09-15 16:44:47 -05001826 pr_err("fsl-diu-fb: missing 'd-cache-line-size' property' "
1827 "in 'cpu' node\n");
Julia Lawall5394ba02008-08-05 13:01:28 -07001828 of_node_put(np);
York Sun9b53a9e2008-04-28 02:15:34 -07001829 return -ENODEV;
Julia Lawall5394ba02008-08-05 13:01:28 -07001830 }
Timur Tabi9e52ba62011-09-15 16:44:50 -05001831 d_cache_line_size = be32_to_cpup(prop);
York Sun9b53a9e2008-04-28 02:15:34 -07001832
1833 of_node_put(np);
1834 coherence_data = vmalloc(coherence_data_size);
1835 if (!coherence_data)
1836 return -ENOMEM;
1837#endif
Timur Tabi4a85dc8b2011-09-15 16:44:46 -05001838
Grant Likely28541d02011-02-22 21:07:43 -07001839 ret = platform_driver_register(&fsl_diu_driver);
York Sun9b53a9e2008-04-28 02:15:34 -07001840 if (ret) {
Timur Tabi154152a2011-09-15 16:44:47 -05001841 pr_err("fsl-diu-fb: failed to register platform driver\n");
York Sun9b53a9e2008-04-28 02:15:34 -07001842#if defined(CONFIG_NOT_COHERENT_CACHE)
1843 vfree(coherence_data);
1844#endif
1845 iounmap(dr.diu_reg);
1846 }
1847 return ret;
1848}
1849
1850static void __exit fsl_diu_exit(void)
1851{
Grant Likely28541d02011-02-22 21:07:43 -07001852 platform_driver_unregister(&fsl_diu_driver);
York Sun9b53a9e2008-04-28 02:15:34 -07001853#if defined(CONFIG_NOT_COHERENT_CACHE)
1854 vfree(coherence_data);
1855#endif
1856}
1857
1858module_init(fsl_diu_init);
1859module_exit(fsl_diu_exit);
1860
1861MODULE_AUTHOR("York Sun <yorksun@freescale.com>");
1862MODULE_DESCRIPTION("Freescale DIU framebuffer driver");
1863MODULE_LICENSE("GPL");
1864
1865module_param_named(mode, fb_mode, charp, 0);
1866MODULE_PARM_DESC(mode,
1867 "Specify resolution as \"<xres>x<yres>[-<bpp>][@<refresh>]\" ");
1868module_param_named(bpp, default_bpp, ulong, 0);
Timur Tabi154152a2011-09-15 16:44:47 -05001869MODULE_PARM_DESC(bpp, "Specify bit-per-pixel if not specified in 'mode'");
Timur Tabi7653aaa2011-07-09 15:38:14 -05001870module_param_named(monitor, monitor_string, charp, 0);
1871MODULE_PARM_DESC(monitor, "Specify the monitor port "
1872 "(\"dvi\", \"lvds\", or \"dlvds\") if supported by the platform");
York Sun9b53a9e2008-04-28 02:15:34 -07001873