blob: f9a95ab28883cadb5ef20709b95fc585b334ec89 [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
Timur Tabib715f9f2011-09-28 16:19:48 -050052struct diu_addr {
53 void *vaddr; /* Virtual address */
54 dma_addr_t paddr; /* Physical address */
55 __u32 offset;
56};
57
58struct diu_pool {
59 struct diu_addr ad;
60 struct diu_addr gamma;
61 struct diu_addr pallete;
62 struct diu_addr cursor;
63};
64
York Sun9b53a9e2008-04-28 02:15:34 -070065/*
Timur Tabi63cf8df2011-09-15 16:44:51 -050066 * List of supported video modes
67 *
Timur Tabi760af8f2011-09-28 16:19:50 -050068 * The first entry is the default video mode. The remain entries are in
69 * order if increasing resolution and frequency. The 320x240-60 mode is
70 * the initial AOI for the second and third planes.
York Sun9b53a9e2008-04-28 02:15:34 -070071 */
York Sun9b53a9e2008-04-28 02:15:34 -070072static struct fb_videomode __devinitdata fsl_diu_mode_db[] = {
73 {
York Sun9b53a9e2008-04-28 02:15:34 -070074 .refresh = 60,
75 .xres = 1024,
76 .yres = 768,
77 .pixclock = 15385,
78 .left_margin = 160,
79 .right_margin = 24,
80 .upper_margin = 29,
81 .lower_margin = 3,
82 .hsync_len = 136,
83 .vsync_len = 6,
84 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
85 .vmode = FB_VMODE_NONINTERLACED
86 },
87 {
Timur Tabi760af8f2011-09-28 16:19:50 -050088 .refresh = 60,
89 .xres = 320,
90 .yres = 240,
91 .pixclock = 79440,
92 .left_margin = 16,
93 .right_margin = 16,
94 .upper_margin = 16,
95 .lower_margin = 5,
96 .hsync_len = 48,
97 .vsync_len = 1,
98 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
99 .vmode = FB_VMODE_NONINTERLACED
100 },
101 {
102 .refresh = 60,
103 .xres = 640,
104 .yres = 480,
105 .pixclock = 39722,
106 .left_margin = 48,
107 .right_margin = 16,
108 .upper_margin = 33,
109 .lower_margin = 10,
110 .hsync_len = 96,
111 .vsync_len = 2,
112 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
113 .vmode = FB_VMODE_NONINTERLACED
114 },
115 {
116 .refresh = 72,
117 .xres = 640,
118 .yres = 480,
119 .pixclock = 32052,
120 .left_margin = 128,
121 .right_margin = 24,
122 .upper_margin = 28,
123 .lower_margin = 9,
124 .hsync_len = 40,
125 .vsync_len = 3,
126 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
127 .vmode = FB_VMODE_NONINTERLACED
128 },
129 {
130 .refresh = 75,
131 .xres = 640,
132 .yres = 480,
133 .pixclock = 31747,
134 .left_margin = 120,
135 .right_margin = 16,
136 .upper_margin = 16,
137 .lower_margin = 1,
138 .hsync_len = 64,
139 .vsync_len = 3,
140 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
141 .vmode = FB_VMODE_NONINTERLACED
142 },
143 {
144 .refresh = 90,
145 .xres = 640,
146 .yres = 480,
147 .pixclock = 25057,
148 .left_margin = 120,
149 .right_margin = 32,
150 .upper_margin = 14,
151 .lower_margin = 25,
152 .hsync_len = 40,
153 .vsync_len = 14,
154 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
155 .vmode = FB_VMODE_NONINTERLACED
156 },
157 {
158 .refresh = 100,
159 .xres = 640,
160 .yres = 480,
161 .pixclock = 22272,
162 .left_margin = 48,
163 .right_margin = 32,
164 .upper_margin = 17,
165 .lower_margin = 22,
166 .hsync_len = 128,
167 .vsync_len = 12,
168 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
169 .vmode = FB_VMODE_NONINTERLACED
170 },
171 {
172 .refresh = 60,
173 .xres = 800,
174 .yres = 480,
175 .pixclock = 33805,
176 .left_margin = 96,
177 .right_margin = 24,
178 .upper_margin = 10,
179 .lower_margin = 3,
180 .hsync_len = 72,
181 .vsync_len = 7,
182 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
183 .vmode = FB_VMODE_NONINTERLACED
184 },
185 {
186 .refresh = 60,
187 .xres = 800,
188 .yres = 600,
189 .pixclock = 25000,
190 .left_margin = 88,
191 .right_margin = 40,
192 .upper_margin = 23,
193 .lower_margin = 1,
194 .hsync_len = 128,
195 .vsync_len = 4,
196 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
197 .vmode = FB_VMODE_NONINTERLACED
198 },
199 {
200 .refresh = 60,
201 .xres = 854,
202 .yres = 480,
203 .pixclock = 31518,
204 .left_margin = 104,
205 .right_margin = 16,
206 .upper_margin = 13,
207 .lower_margin = 1,
208 .hsync_len = 88,
209 .vsync_len = 3,
210 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
211 .vmode = FB_VMODE_NONINTERLACED
212 },
213 {
York Sun9b53a9e2008-04-28 02:15:34 -0700214 .refresh = 70,
215 .xres = 1024,
216 .yres = 768,
217 .pixclock = 16886,
218 .left_margin = 3,
219 .right_margin = 3,
220 .upper_margin = 2,
221 .lower_margin = 2,
222 .hsync_len = 40,
223 .vsync_len = 18,
224 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
225 .vmode = FB_VMODE_NONINTERLACED
226 },
227 {
York Sun9b53a9e2008-04-28 02:15:34 -0700228 .refresh = 75,
229 .xres = 1024,
230 .yres = 768,
231 .pixclock = 15009,
232 .left_margin = 3,
233 .right_margin = 3,
234 .upper_margin = 2,
235 .lower_margin = 2,
236 .hsync_len = 80,
237 .vsync_len = 32,
238 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
239 .vmode = FB_VMODE_NONINTERLACED
240 },
241 {
Timur Tabi760af8f2011-09-28 16:19:50 -0500242 .refresh = 60,
243 .xres = 1280,
244 .yres = 480,
245 .pixclock = 18939,
246 .left_margin = 353,
247 .right_margin = 47,
248 .upper_margin = 39,
249 .lower_margin = 4,
250 .hsync_len = 8,
251 .vsync_len = 2,
252 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
253 .vmode = FB_VMODE_NONINTERLACED
254 },
255 {
256 .refresh = 60,
257 .xres = 1280,
258 .yres = 720,
259 .pixclock = 13426,
260 .left_margin = 192,
261 .right_margin = 64,
262 .upper_margin = 22,
263 .lower_margin = 1,
264 .hsync_len = 136,
265 .vsync_len = 3,
266 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
267 .vmode = FB_VMODE_NONINTERLACED
268 },
269 {
York Sun9b53a9e2008-04-28 02:15:34 -0700270 .refresh = 60,
271 .xres = 1280,
272 .yres = 1024,
273 .pixclock = 9375,
274 .left_margin = 38,
275 .right_margin = 128,
276 .upper_margin = 2,
277 .lower_margin = 7,
278 .hsync_len = 216,
279 .vsync_len = 37,
280 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
281 .vmode = FB_VMODE_NONINTERLACED
282 },
283 {
York Sun9b53a9e2008-04-28 02:15:34 -0700284 .refresh = 70,
285 .xres = 1280,
286 .yres = 1024,
287 .pixclock = 9380,
288 .left_margin = 6,
289 .right_margin = 6,
290 .upper_margin = 4,
291 .lower_margin = 4,
292 .hsync_len = 60,
293 .vsync_len = 94,
294 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
295 .vmode = FB_VMODE_NONINTERLACED
296 },
297 {
York Sun9b53a9e2008-04-28 02:15:34 -0700298 .refresh = 75,
299 .xres = 1280,
300 .yres = 1024,
301 .pixclock = 9380,
302 .left_margin = 6,
303 .right_margin = 6,
304 .upper_margin = 4,
305 .lower_margin = 4,
306 .hsync_len = 60,
307 .vsync_len = 15,
308 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
309 .vmode = FB_VMODE_NONINTERLACED
310 },
311 {
York Sun9b53a9e2008-04-28 02:15:34 -0700312 .refresh = 60,
Timur Tabi760af8f2011-09-28 16:19:50 -0500313 .xres = 1920,
314 .yres = 1080,
315 .pixclock = 5787,
316 .left_margin = 328,
317 .right_margin = 120,
318 .upper_margin = 34,
319 .lower_margin = 1,
320 .hsync_len = 208,
321 .vsync_len = 3,
York Sun9b53a9e2008-04-28 02:15:34 -0700322 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
323 .vmode = FB_VMODE_NONINTERLACED
324 },
325};
326
Timur Tabi760af8f2011-09-28 16:19:50 -0500327static char *fb_mode;
York Sun9b53a9e2008-04-28 02:15:34 -0700328static unsigned long default_bpp = 32;
Timur Tabi7653aaa2011-07-09 15:38:14 -0500329static enum fsl_diu_monitor_port monitor_port;
330static char *monitor_string;
York Sun9b53a9e2008-04-28 02:15:34 -0700331
332#if defined(CONFIG_NOT_COHERENT_CACHE)
333static u8 *coherence_data;
334static size_t coherence_data_size;
335static unsigned int d_cache_line_size;
336#endif
337
338static DEFINE_SPINLOCK(diu_lock);
339
340struct fsl_diu_data {
341 struct fb_info *fsl_diu_info[FSL_AOI_NUM - 1];
342 /*FSL_AOI_NUM has one dummy AOI */
343 struct device_attribute dev_attr;
344 struct diu_ad *dummy_ad;
345 void *dummy_aoi_virt;
346 unsigned int irq;
347 int fb_enabled;
Timur Tabi7653aaa2011-07-09 15:38:14 -0500348 enum fsl_diu_monitor_port monitor_port;
Timur Tabi3c755b72011-10-04 19:36:51 -0500349 struct diu __iomem *diu_reg;
350 spinlock_t reg_lock;
York Sun9b53a9e2008-04-28 02:15:34 -0700351};
352
Timur Tabi2572df92011-09-28 16:19:51 -0500353enum mfb_index {
354 PLANE0 = 0, /* Plane 0, only one AOI that fills the screen */
355 PLANE1_AOI0, /* Plane 1, first AOI */
356 PLANE1_AOI1, /* Plane 1, second AOI */
357 PLANE2_AOI0, /* Plane 2, first AOI */
358 PLANE2_AOI1, /* Plane 2, second AOI */
359};
360
York Sun9b53a9e2008-04-28 02:15:34 -0700361struct mfb_info {
Timur Tabi2572df92011-09-28 16:19:51 -0500362 enum mfb_index index;
York Sun9b53a9e2008-04-28 02:15:34 -0700363 char *id;
364 int registered;
York Sun9b53a9e2008-04-28 02:15:34 -0700365 unsigned long pseudo_palette[16];
366 struct diu_ad *ad;
367 int cursor_reset;
368 unsigned char g_alpha;
369 unsigned int count;
370 int x_aoi_d; /* aoi display x offset to physical screen */
371 int y_aoi_d; /* aoi display y offset to physical screen */
372 struct fsl_diu_data *parent;
Anatolij Gustschin8b856f02010-07-23 04:00:39 +0000373 u8 *edid_data;
York Sun9b53a9e2008-04-28 02:15:34 -0700374};
375
376
377static struct mfb_info mfb_template[] = {
Timur Tabi2572df92011-09-28 16:19:51 -0500378 {
379 .index = PLANE0,
Timur Tabi4a85dc8b2011-09-15 16:44:46 -0500380 .id = "Panel0",
381 .registered = 0,
382 .count = 0,
383 .x_aoi_d = 0,
384 .y_aoi_d = 0,
York Sun9b53a9e2008-04-28 02:15:34 -0700385 },
Timur Tabi2572df92011-09-28 16:19:51 -0500386 {
387 .index = PLANE1_AOI0,
Timur Tabi4a85dc8b2011-09-15 16:44:46 -0500388 .id = "Panel1 AOI0",
389 .registered = 0,
390 .g_alpha = 0xff,
391 .count = 0,
392 .x_aoi_d = 0,
393 .y_aoi_d = 0,
York Sun9b53a9e2008-04-28 02:15:34 -0700394 },
Timur Tabi2572df92011-09-28 16:19:51 -0500395 {
396 .index = PLANE1_AOI1,
Timur Tabi4a85dc8b2011-09-15 16:44:46 -0500397 .id = "Panel1 AOI1",
398 .registered = 0,
399 .g_alpha = 0xff,
400 .count = 0,
401 .x_aoi_d = 0,
402 .y_aoi_d = 480,
York Sun9b53a9e2008-04-28 02:15:34 -0700403 },
Timur Tabi2572df92011-09-28 16:19:51 -0500404 {
405 .index = PLANE2_AOI0,
Timur Tabi4a85dc8b2011-09-15 16:44:46 -0500406 .id = "Panel2 AOI0",
407 .registered = 0,
408 .g_alpha = 0xff,
409 .count = 0,
410 .x_aoi_d = 640,
411 .y_aoi_d = 0,
York Sun9b53a9e2008-04-28 02:15:34 -0700412 },
Timur Tabi2572df92011-09-28 16:19:51 -0500413 {
414 .index = PLANE2_AOI1,
Timur Tabi4a85dc8b2011-09-15 16:44:46 -0500415 .id = "Panel2 AOI1",
416 .registered = 0,
417 .g_alpha = 0xff,
418 .count = 0,
419 .x_aoi_d = 640,
420 .y_aoi_d = 480,
York Sun9b53a9e2008-04-28 02:15:34 -0700421 },
422};
423
York Sun9b53a9e2008-04-28 02:15:34 -0700424static struct diu_pool pool;
425
Timur Tabi6b51d512008-07-23 21:31:39 -0700426/**
Timur Tabi7653aaa2011-07-09 15:38:14 -0500427 * fsl_diu_name_to_port - convert a port name to a monitor port enum
428 *
429 * Takes the name of a monitor port ("dvi", "lvds", or "dlvds") and returns
430 * the enum fsl_diu_monitor_port that corresponds to that string.
431 *
432 * For compatibility with older versions, a number ("0", "1", or "2") is also
433 * supported.
434 *
435 * If the string is unknown, DVI is assumed.
436 *
437 * If the particular port is not supported by the platform, another port
438 * (platform-specific) is chosen instead.
439 */
440static enum fsl_diu_monitor_port fsl_diu_name_to_port(const char *s)
441{
442 enum fsl_diu_monitor_port port = FSL_DIU_PORT_DVI;
443 unsigned long val;
444
445 if (s) {
446 if (!strict_strtoul(s, 10, &val) && (val <= 2))
447 port = (enum fsl_diu_monitor_port) val;
448 else if (strncmp(s, "lvds", 4) == 0)
449 port = FSL_DIU_PORT_LVDS;
450 else if (strncmp(s, "dlvds", 5) == 0)
451 port = FSL_DIU_PORT_DLVDS;
452 }
453
454 return diu_ops.valid_monitor_port(port);
455}
456
457/**
Timur Tabi6b51d512008-07-23 21:31:39 -0700458 * fsl_diu_alloc - allocate memory for the DIU
459 * @size: number of bytes to allocate
460 * @param: returned physical address of memory
461 *
462 * This function allocates a physically-contiguous block of memory.
York Sun9b53a9e2008-04-28 02:15:34 -0700463 */
Timur Tabi6b51d512008-07-23 21:31:39 -0700464static void *fsl_diu_alloc(size_t size, phys_addr_t *phys)
York Sun9b53a9e2008-04-28 02:15:34 -0700465{
466 void *virt;
467
Timur Tabi6b51d512008-07-23 21:31:39 -0700468 virt = alloc_pages_exact(size, GFP_DMA | __GFP_ZERO);
Timur Tabi154152a2011-09-15 16:44:47 -0500469 if (virt)
York Sun9b53a9e2008-04-28 02:15:34 -0700470 *phys = virt_to_phys(virt);
York Sun9b53a9e2008-04-28 02:15:34 -0700471
472 return virt;
473}
474
Timur Tabi6b51d512008-07-23 21:31:39 -0700475/**
476 * fsl_diu_free - release DIU memory
477 * @virt: pointer returned by fsl_diu_alloc()
478 * @size: number of bytes allocated by fsl_diu_alloc()
479 *
480 * This function releases memory allocated by fsl_diu_alloc().
481 */
482static void fsl_diu_free(void *virt, size_t size)
York Sun9b53a9e2008-04-28 02:15:34 -0700483{
Timur Tabi6b51d512008-07-23 21:31:39 -0700484 if (virt && size)
485 free_pages_exact(virt, size);
York Sun9b53a9e2008-04-28 02:15:34 -0700486}
487
Anatolij Gustschin0d9dab32010-07-23 04:00:35 +0000488/*
489 * Workaround for failed writing desc register of planes.
490 * Needed with MPC5121 DIU rev 2.0 silicon.
491 */
492void wr_reg_wa(u32 *reg, u32 val)
493{
494 do {
495 out_be32(reg, val);
496 } while (in_be32(reg) != val);
497}
498
Timur Tabi7e47c212011-09-28 16:19:52 -0500499static void fsl_diu_enable_panel(struct fb_info *info)
York Sun9b53a9e2008-04-28 02:15:34 -0700500{
501 struct mfb_info *pmfbi, *cmfbi, *mfbi = info->par;
York Sun9b53a9e2008-04-28 02:15:34 -0700502 struct diu_ad *ad = mfbi->ad;
503 struct fsl_diu_data *machine_data = mfbi->parent;
Timur Tabi3c755b72011-10-04 19:36:51 -0500504 struct diu __iomem *hw = machine_data->diu_reg;
York Sun9b53a9e2008-04-28 02:15:34 -0700505
Timur Tabi7e47c212011-09-28 16:19:52 -0500506 switch (mfbi->index) {
507 case PLANE0:
508 if (hw->desc[0] != ad->paddr)
509 wr_reg_wa(&hw->desc[0], ad->paddr);
510 break;
511 case PLANE1_AOI0:
512 cmfbi = machine_data->fsl_diu_info[2]->par;
513 if (hw->desc[1] != ad->paddr) { /* AOI0 closed */
514 if (cmfbi->count > 0) /* AOI1 open */
515 ad->next_ad =
516 cpu_to_le32(cmfbi->ad->paddr);
517 else
518 ad->next_ad = 0;
519 wr_reg_wa(&hw->desc[1], ad->paddr);
York Sun9b53a9e2008-04-28 02:15:34 -0700520 }
Timur Tabi7e47c212011-09-28 16:19:52 -0500521 break;
522 case PLANE2_AOI0:
523 cmfbi = machine_data->fsl_diu_info[4]->par;
524 if (hw->desc[2] != ad->paddr) { /* AOI0 closed */
525 if (cmfbi->count > 0) /* AOI1 open */
526 ad->next_ad =
527 cpu_to_le32(cmfbi->ad->paddr);
528 else
529 ad->next_ad = 0;
530 wr_reg_wa(&hw->desc[2], ad->paddr);
531 }
532 break;
533 case PLANE1_AOI1:
534 pmfbi = machine_data->fsl_diu_info[1]->par;
535 ad->next_ad = 0;
536 if (hw->desc[1] == machine_data->dummy_ad->paddr)
537 wr_reg_wa(&hw->desc[1], ad->paddr);
538 else /* AOI0 open */
539 pmfbi->ad->next_ad = cpu_to_le32(ad->paddr);
540 break;
541 case PLANE2_AOI1:
542 pmfbi = machine_data->fsl_diu_info[3]->par;
543 ad->next_ad = 0;
544 if (hw->desc[2] == machine_data->dummy_ad->paddr)
545 wr_reg_wa(&hw->desc[2], ad->paddr);
546 else /* AOI0 was open */
547 pmfbi->ad->next_ad = cpu_to_le32(ad->paddr);
548 break;
549 }
York Sun9b53a9e2008-04-28 02:15:34 -0700550}
551
Timur Tabi2572df92011-09-28 16:19:51 -0500552static void fsl_diu_disable_panel(struct fb_info *info)
York Sun9b53a9e2008-04-28 02:15:34 -0700553{
554 struct mfb_info *pmfbi, *cmfbi, *mfbi = info->par;
York Sun9b53a9e2008-04-28 02:15:34 -0700555 struct diu_ad *ad = mfbi->ad;
556 struct fsl_diu_data *machine_data = mfbi->parent;
Timur Tabi3c755b72011-10-04 19:36:51 -0500557 struct diu __iomem *hw = machine_data->diu_reg;
York Sun9b53a9e2008-04-28 02:15:34 -0700558
559 switch (mfbi->index) {
Timur Tabi2572df92011-09-28 16:19:51 -0500560 case PLANE0:
York Sun9b53a9e2008-04-28 02:15:34 -0700561 if (hw->desc[0] != machine_data->dummy_ad->paddr)
Anatolij Gustschin0d9dab32010-07-23 04:00:35 +0000562 wr_reg_wa(&hw->desc[0], machine_data->dummy_ad->paddr);
York Sun9b53a9e2008-04-28 02:15:34 -0700563 break;
Timur Tabi2572df92011-09-28 16:19:51 -0500564 case PLANE1_AOI0:
York Sun9b53a9e2008-04-28 02:15:34 -0700565 cmfbi = machine_data->fsl_diu_info[2]->par;
566 if (cmfbi->count > 0) /* AOI1 is open */
Anatolij Gustschin0d9dab32010-07-23 04:00:35 +0000567 wr_reg_wa(&hw->desc[1], cmfbi->ad->paddr);
York Sun9b53a9e2008-04-28 02:15:34 -0700568 /* move AOI1 to the first */
569 else /* AOI1 was closed */
Anatolij Gustschin0d9dab32010-07-23 04:00:35 +0000570 wr_reg_wa(&hw->desc[1], machine_data->dummy_ad->paddr);
York Sun9b53a9e2008-04-28 02:15:34 -0700571 /* close AOI 0 */
572 break;
Timur Tabi2572df92011-09-28 16:19:51 -0500573 case PLANE2_AOI0:
York Sun9b53a9e2008-04-28 02:15:34 -0700574 cmfbi = machine_data->fsl_diu_info[4]->par;
575 if (cmfbi->count > 0) /* AOI1 is open */
Anatolij Gustschin0d9dab32010-07-23 04:00:35 +0000576 wr_reg_wa(&hw->desc[2], cmfbi->ad->paddr);
York Sun9b53a9e2008-04-28 02:15:34 -0700577 /* move AOI1 to the first */
578 else /* AOI1 was closed */
Anatolij Gustschin0d9dab32010-07-23 04:00:35 +0000579 wr_reg_wa(&hw->desc[2], machine_data->dummy_ad->paddr);
York Sun9b53a9e2008-04-28 02:15:34 -0700580 /* close AOI 0 */
581 break;
Timur Tabi2572df92011-09-28 16:19:51 -0500582 case PLANE1_AOI1:
York Sun9b53a9e2008-04-28 02:15:34 -0700583 pmfbi = machine_data->fsl_diu_info[1]->par;
584 if (hw->desc[1] != ad->paddr) {
585 /* AOI1 is not the first in the chain */
586 if (pmfbi->count > 0)
587 /* AOI0 is open, must be the first */
588 pmfbi->ad->next_ad = 0;
589 } else /* AOI1 is the first in the chain */
Anatolij Gustschin0d9dab32010-07-23 04:00:35 +0000590 wr_reg_wa(&hw->desc[1], machine_data->dummy_ad->paddr);
York Sun9b53a9e2008-04-28 02:15:34 -0700591 /* close AOI 1 */
592 break;
Timur Tabi2572df92011-09-28 16:19:51 -0500593 case PLANE2_AOI1:
York Sun9b53a9e2008-04-28 02:15:34 -0700594 pmfbi = machine_data->fsl_diu_info[3]->par;
595 if (hw->desc[2] != ad->paddr) {
596 /* AOI1 is not the first in the chain */
597 if (pmfbi->count > 0)
598 /* AOI0 is open, must be the first */
599 pmfbi->ad->next_ad = 0;
600 } else /* AOI1 is the first in the chain */
Anatolij Gustschin0d9dab32010-07-23 04:00:35 +0000601 wr_reg_wa(&hw->desc[2], machine_data->dummy_ad->paddr);
York Sun9b53a9e2008-04-28 02:15:34 -0700602 /* close AOI 1 */
603 break;
York Sun9b53a9e2008-04-28 02:15:34 -0700604 }
York Sun9b53a9e2008-04-28 02:15:34 -0700605}
606
607static void enable_lcdc(struct fb_info *info)
608{
York Sun9b53a9e2008-04-28 02:15:34 -0700609 struct mfb_info *mfbi = info->par;
610 struct fsl_diu_data *machine_data = mfbi->parent;
Timur Tabi3c755b72011-10-04 19:36:51 -0500611 struct diu __iomem *hw = machine_data->diu_reg;
York Sun9b53a9e2008-04-28 02:15:34 -0700612
613 if (!machine_data->fb_enabled) {
Timur Tabic4e5a022011-09-28 16:19:53 -0500614 out_be32(&hw->diu_mode, MFB_MODE1);
York Sun9b53a9e2008-04-28 02:15:34 -0700615 machine_data->fb_enabled++;
616 }
617}
618
619static void disable_lcdc(struct fb_info *info)
620{
York Sun9b53a9e2008-04-28 02:15:34 -0700621 struct mfb_info *mfbi = info->par;
622 struct fsl_diu_data *machine_data = mfbi->parent;
Timur Tabi3c755b72011-10-04 19:36:51 -0500623 struct diu __iomem *hw = machine_data->diu_reg;
York Sun9b53a9e2008-04-28 02:15:34 -0700624
625 if (machine_data->fb_enabled) {
626 out_be32(&hw->diu_mode, 0);
627 machine_data->fb_enabled = 0;
628 }
629}
630
631static void adjust_aoi_size_position(struct fb_var_screeninfo *var,
632 struct fb_info *info)
633{
634 struct mfb_info *lower_aoi_mfbi, *upper_aoi_mfbi, *mfbi = info->par;
635 struct fsl_diu_data *machine_data = mfbi->parent;
Timur Tabi2572df92011-09-28 16:19:51 -0500636 int available_height, upper_aoi_bottom;
637 enum mfb_index index = mfbi->index;
York Sun9b53a9e2008-04-28 02:15:34 -0700638 int lower_aoi_is_open, upper_aoi_is_open;
639 __u32 base_plane_width, base_plane_height, upper_aoi_height;
640
641 base_plane_width = machine_data->fsl_diu_info[0]->var.xres;
642 base_plane_height = machine_data->fsl_diu_info[0]->var.yres;
643
York Sunfdfaa48332008-08-15 00:40:29 -0700644 if (mfbi->x_aoi_d < 0)
645 mfbi->x_aoi_d = 0;
646 if (mfbi->y_aoi_d < 0)
647 mfbi->y_aoi_d = 0;
York Sun9b53a9e2008-04-28 02:15:34 -0700648 switch (index) {
Timur Tabi2572df92011-09-28 16:19:51 -0500649 case PLANE0:
York Sun9b53a9e2008-04-28 02:15:34 -0700650 if (mfbi->x_aoi_d != 0)
651 mfbi->x_aoi_d = 0;
652 if (mfbi->y_aoi_d != 0)
653 mfbi->y_aoi_d = 0;
654 break;
Timur Tabi2572df92011-09-28 16:19:51 -0500655 case PLANE1_AOI0:
656 case PLANE2_AOI0:
York Sun9b53a9e2008-04-28 02:15:34 -0700657 lower_aoi_mfbi = machine_data->fsl_diu_info[index+1]->par;
658 lower_aoi_is_open = lower_aoi_mfbi->count > 0 ? 1 : 0;
659 if (var->xres > base_plane_width)
660 var->xres = base_plane_width;
661 if ((mfbi->x_aoi_d + var->xres) > base_plane_width)
662 mfbi->x_aoi_d = base_plane_width - var->xres;
663
664 if (lower_aoi_is_open)
665 available_height = lower_aoi_mfbi->y_aoi_d;
666 else
667 available_height = base_plane_height;
668 if (var->yres > available_height)
669 var->yres = available_height;
670 if ((mfbi->y_aoi_d + var->yres) > available_height)
671 mfbi->y_aoi_d = available_height - var->yres;
672 break;
Timur Tabi2572df92011-09-28 16:19:51 -0500673 case PLANE1_AOI1:
674 case PLANE2_AOI1:
York Sun9b53a9e2008-04-28 02:15:34 -0700675 upper_aoi_mfbi = machine_data->fsl_diu_info[index-1]->par;
676 upper_aoi_height =
677 machine_data->fsl_diu_info[index-1]->var.yres;
678 upper_aoi_bottom = upper_aoi_mfbi->y_aoi_d + upper_aoi_height;
679 upper_aoi_is_open = upper_aoi_mfbi->count > 0 ? 1 : 0;
680 if (var->xres > base_plane_width)
681 var->xres = base_plane_width;
682 if ((mfbi->x_aoi_d + var->xres) > base_plane_width)
683 mfbi->x_aoi_d = base_plane_width - var->xres;
684 if (mfbi->y_aoi_d < 0)
685 mfbi->y_aoi_d = 0;
686 if (upper_aoi_is_open) {
687 if (mfbi->y_aoi_d < upper_aoi_bottom)
688 mfbi->y_aoi_d = upper_aoi_bottom;
689 available_height = base_plane_height
690 - upper_aoi_bottom;
691 } else
692 available_height = base_plane_height;
693 if (var->yres > available_height)
694 var->yres = available_height;
695 if ((mfbi->y_aoi_d + var->yres) > base_plane_height)
696 mfbi->y_aoi_d = base_plane_height - var->yres;
697 break;
698 }
699}
700/*
701 * Checks to see if the hardware supports the state requested by var passed
702 * in. This function does not alter the hardware state! If the var passed in
703 * is slightly off by what the hardware can support then we alter the var
704 * PASSED in to what we can do. If the hardware doesn't support mode change
705 * a -EINVAL will be returned by the upper layers.
706 */
707static int fsl_diu_check_var(struct fb_var_screeninfo *var,
708 struct fb_info *info)
709{
York Sun9b53a9e2008-04-28 02:15:34 -0700710 if (var->xres_virtual < var->xres)
711 var->xres_virtual = var->xres;
712 if (var->yres_virtual < var->yres)
713 var->yres_virtual = var->yres;
714
715 if (var->xoffset < 0)
716 var->xoffset = 0;
717
718 if (var->yoffset < 0)
719 var->yoffset = 0;
720
721 if (var->xoffset + info->var.xres > info->var.xres_virtual)
722 var->xoffset = info->var.xres_virtual - info->var.xres;
723
724 if (var->yoffset + info->var.yres > info->var.yres_virtual)
725 var->yoffset = info->var.yres_virtual - info->var.yres;
726
727 if ((var->bits_per_pixel != 32) && (var->bits_per_pixel != 24) &&
728 (var->bits_per_pixel != 16))
729 var->bits_per_pixel = default_bpp;
730
731 switch (var->bits_per_pixel) {
732 case 16:
733 var->red.length = 5;
734 var->red.offset = 11;
735 var->red.msb_right = 0;
736
737 var->green.length = 6;
738 var->green.offset = 5;
739 var->green.msb_right = 0;
740
741 var->blue.length = 5;
742 var->blue.offset = 0;
743 var->blue.msb_right = 0;
744
745 var->transp.length = 0;
746 var->transp.offset = 0;
747 var->transp.msb_right = 0;
748 break;
749 case 24:
750 var->red.length = 8;
751 var->red.offset = 0;
752 var->red.msb_right = 0;
753
754 var->green.length = 8;
755 var->green.offset = 8;
756 var->green.msb_right = 0;
757
758 var->blue.length = 8;
759 var->blue.offset = 16;
760 var->blue.msb_right = 0;
761
762 var->transp.length = 0;
763 var->transp.offset = 0;
764 var->transp.msb_right = 0;
765 break;
766 case 32:
767 var->red.length = 8;
768 var->red.offset = 16;
769 var->red.msb_right = 0;
770
771 var->green.length = 8;
772 var->green.offset = 8;
773 var->green.msb_right = 0;
774
775 var->blue.length = 8;
776 var->blue.offset = 0;
777 var->blue.msb_right = 0;
778
779 var->transp.length = 8;
780 var->transp.offset = 24;
781 var->transp.msb_right = 0;
782
783 break;
784 }
York Sun9b53a9e2008-04-28 02:15:34 -0700785
786 var->height = -1;
787 var->width = -1;
788 var->grayscale = 0;
789
790 /* Copy nonstd field to/from sync for fbset usage */
791 var->sync |= var->nonstd;
792 var->nonstd |= var->sync;
793
794 adjust_aoi_size_position(var, info);
795 return 0;
796}
797
798static void set_fix(struct fb_info *info)
799{
800 struct fb_fix_screeninfo *fix = &info->fix;
801 struct fb_var_screeninfo *var = &info->var;
802 struct mfb_info *mfbi = info->par;
803
Timur Tabiec02dd22011-09-15 16:44:54 -0500804 strncpy(fix->id, mfbi->id, sizeof(fix->id));
York Sun9b53a9e2008-04-28 02:15:34 -0700805 fix->line_length = var->xres_virtual * var->bits_per_pixel / 8;
806 fix->type = FB_TYPE_PACKED_PIXELS;
807 fix->accel = FB_ACCEL_NONE;
808 fix->visual = FB_VISUAL_TRUECOLOR;
809 fix->xpanstep = 1;
810 fix->ypanstep = 1;
811}
812
813static void update_lcdc(struct fb_info *info)
814{
815 struct fb_var_screeninfo *var = &info->var;
816 struct mfb_info *mfbi = info->par;
817 struct fsl_diu_data *machine_data = mfbi->parent;
Timur Tabi3c755b72011-10-04 19:36:51 -0500818 struct diu __iomem *hw;
York Sun9b53a9e2008-04-28 02:15:34 -0700819 int i, j;
820 char __iomem *cursor_base, *gamma_table_base;
821
822 u32 temp;
823
Timur Tabi3c755b72011-10-04 19:36:51 -0500824 hw = machine_data->diu_reg;
York Sun9b53a9e2008-04-28 02:15:34 -0700825
York Sun9b53a9e2008-04-28 02:15:34 -0700826 diu_ops.set_monitor_port(machine_data->monitor_port);
827 gamma_table_base = pool.gamma.vaddr;
828 cursor_base = pool.cursor.vaddr;
829 /* Prep for DIU init - gamma table, cursor table */
830
831 for (i = 0; i <= 2; i++)
Timur Tabi4a85dc8b2011-09-15 16:44:46 -0500832 for (j = 0; j <= 255; j++)
833 *gamma_table_base++ = j;
York Sun9b53a9e2008-04-28 02:15:34 -0700834
835 diu_ops.set_gamma_table(machine_data->monitor_port, pool.gamma.vaddr);
836
York Sun9b53a9e2008-04-28 02:15:34 -0700837 disable_lcdc(info);
838
839 /* Program DIU registers */
840
841 out_be32(&hw->gamma, pool.gamma.paddr);
842 out_be32(&hw->cursor, pool.cursor.paddr);
843
844 out_be32(&hw->bgnd, 0x007F7F7F); /* BGND */
845 out_be32(&hw->bgnd_wb, 0); /* BGND_WB */
846 out_be32(&hw->disp_size, (var->yres << 16 | var->xres));
847 /* DISP SIZE */
York Sun9b53a9e2008-04-28 02:15:34 -0700848 out_be32(&hw->wb_size, 0); /* WB SIZE */
849 out_be32(&hw->wb_mem_addr, 0); /* WB MEM ADDR */
850
851 /* Horizontal and vertical configuration register */
852 temp = var->left_margin << 22 | /* BP_H */
853 var->hsync_len << 11 | /* PW_H */
854 var->right_margin; /* FP_H */
855
856 out_be32(&hw->hsyn_para, temp);
857
858 temp = var->upper_margin << 22 | /* BP_V */
859 var->vsync_len << 11 | /* PW_V */
860 var->lower_margin; /* FP_V */
861
862 out_be32(&hw->vsyn_para, temp);
863
York Sun9b53a9e2008-04-28 02:15:34 -0700864 diu_ops.set_pixel_clock(var->pixclock);
865
866 out_be32(&hw->syn_pol, 0); /* SYNC SIGNALS POLARITY */
867 out_be32(&hw->thresholds, 0x00037800); /* The Thresholds */
868 out_be32(&hw->int_status, 0); /* INTERRUPT STATUS */
869 out_be32(&hw->plut, 0x01F5F666);
870
871 /* Enable the DIU */
872 enable_lcdc(info);
873}
874
875static int map_video_memory(struct fb_info *info)
876{
877 phys_addr_t phys;
Krzysztof Helt537a1bf2009-06-30 11:41:29 -0700878 u32 smem_len = info->fix.line_length * info->var.yres_virtual;
York Sun9b53a9e2008-04-28 02:15:34 -0700879
Krzysztof Helt537a1bf2009-06-30 11:41:29 -0700880 info->screen_base = fsl_diu_alloc(smem_len, &phys);
Anton Vorontsov05946bc2008-07-04 09:59:38 -0700881 if (info->screen_base == NULL) {
Timur Tabi154152a2011-09-15 16:44:47 -0500882 dev_err(info->dev, "unable to allocate fb memory\n");
York Sun9b53a9e2008-04-28 02:15:34 -0700883 return -ENOMEM;
884 }
Krzysztof Helt537a1bf2009-06-30 11:41:29 -0700885 mutex_lock(&info->mm_lock);
York Sun9b53a9e2008-04-28 02:15:34 -0700886 info->fix.smem_start = (unsigned long) phys;
Krzysztof Helt537a1bf2009-06-30 11:41:29 -0700887 info->fix.smem_len = smem_len;
888 mutex_unlock(&info->mm_lock);
York Sun9b53a9e2008-04-28 02:15:34 -0700889 info->screen_size = info->fix.smem_len;
890
York Sun9b53a9e2008-04-28 02:15:34 -0700891 return 0;
892}
893
894static void unmap_video_memory(struct fb_info *info)
895{
896 fsl_diu_free(info->screen_base, info->fix.smem_len);
Krzysztof Helt537a1bf2009-06-30 11:41:29 -0700897 mutex_lock(&info->mm_lock);
Anton Vorontsov05946bc2008-07-04 09:59:38 -0700898 info->screen_base = NULL;
York Sun9b53a9e2008-04-28 02:15:34 -0700899 info->fix.smem_start = 0;
900 info->fix.smem_len = 0;
Krzysztof Helt537a1bf2009-06-30 11:41:29 -0700901 mutex_unlock(&info->mm_lock);
York Sun9b53a9e2008-04-28 02:15:34 -0700902}
903
904/*
York Sunae5591e2008-08-15 00:40:28 -0700905 * Using the fb_var_screeninfo in fb_info we set the aoi of this
906 * particular framebuffer. It is a light version of fsl_diu_set_par.
907 */
908static int fsl_diu_set_aoi(struct fb_info *info)
909{
910 struct fb_var_screeninfo *var = &info->var;
911 struct mfb_info *mfbi = info->par;
912 struct diu_ad *ad = mfbi->ad;
913
914 /* AOI should not be greater than display size */
915 ad->offset_xyi = cpu_to_le32((var->yoffset << 16) | var->xoffset);
916 ad->offset_xyd = cpu_to_le32((mfbi->y_aoi_d << 16) | mfbi->x_aoi_d);
917 return 0;
918}
919
920/*
York Sun9b53a9e2008-04-28 02:15:34 -0700921 * Using the fb_var_screeninfo in fb_info we set the resolution of this
922 * particular framebuffer. This function alters the fb_fix_screeninfo stored
923 * in fb_info. It does not alter var in fb_info since we are using that
924 * data. This means we depend on the data in var inside fb_info to be
925 * supported by the hardware. fsl_diu_check_var is always called before
926 * fsl_diu_set_par to ensure this.
927 */
928static int fsl_diu_set_par(struct fb_info *info)
929{
930 unsigned long len;
931 struct fb_var_screeninfo *var = &info->var;
932 struct mfb_info *mfbi = info->par;
933 struct fsl_diu_data *machine_data = mfbi->parent;
934 struct diu_ad *ad = mfbi->ad;
Timur Tabi3c755b72011-10-04 19:36:51 -0500935 struct diu __iomem *hw;
York Sun9b53a9e2008-04-28 02:15:34 -0700936
Timur Tabi3c755b72011-10-04 19:36:51 -0500937 hw = machine_data->diu_reg;
York Sun9b53a9e2008-04-28 02:15:34 -0700938
939 set_fix(info);
940 mfbi->cursor_reset = 1;
941
942 len = info->var.yres_virtual * info->fix.line_length;
943 /* Alloc & dealloc each time resolution/bpp change */
944 if (len != info->fix.smem_len) {
945 if (info->fix.smem_start)
946 unmap_video_memory(info);
York Sun9b53a9e2008-04-28 02:15:34 -0700947
948 /* Memory allocation for framebuffer */
949 if (map_video_memory(info)) {
Timur Tabi154152a2011-09-15 16:44:47 -0500950 dev_err(info->dev, "unable to allocate fb memory 1\n");
York Sun9b53a9e2008-04-28 02:15:34 -0700951 return -ENOMEM;
952 }
953 }
954
Timur Tabi7653aaa2011-07-09 15:38:14 -0500955 ad->pix_fmt = diu_ops.get_pixel_format(machine_data->monitor_port,
956 var->bits_per_pixel);
York Sun9b53a9e2008-04-28 02:15:34 -0700957 ad->addr = cpu_to_le32(info->fix.smem_start);
York Sunae5591e2008-08-15 00:40:28 -0700958 ad->src_size_g_alpha = cpu_to_le32((var->yres_virtual << 12) |
959 var->xres_virtual) | mfbi->g_alpha;
960 /* AOI should not be greater than display size */
York Sun9b53a9e2008-04-28 02:15:34 -0700961 ad->aoi_size = cpu_to_le32((var->yres << 16) | var->xres);
York Sunae5591e2008-08-15 00:40:28 -0700962 ad->offset_xyi = cpu_to_le32((var->yoffset << 16) | var->xoffset);
York Sun9b53a9e2008-04-28 02:15:34 -0700963 ad->offset_xyd = cpu_to_le32((mfbi->y_aoi_d << 16) | mfbi->x_aoi_d);
964
965 /* Disable chroma keying function */
966 ad->ckmax_r = 0;
967 ad->ckmax_g = 0;
968 ad->ckmax_b = 0;
969
970 ad->ckmin_r = 255;
971 ad->ckmin_g = 255;
972 ad->ckmin_b = 255;
973
Timur Tabi2572df92011-09-28 16:19:51 -0500974 if (mfbi->index == PLANE0)
York Sun9b53a9e2008-04-28 02:15:34 -0700975 update_lcdc(info);
976 return 0;
977}
978
979static inline __u32 CNVT_TOHW(__u32 val, __u32 width)
980{
Timur Tabi4a85dc8b2011-09-15 16:44:46 -0500981 return ((val << width) + 0x7FFF - val) >> 16;
York Sun9b53a9e2008-04-28 02:15:34 -0700982}
983
984/*
985 * Set a single color register. The values supplied have a 16 bit magnitude
986 * which needs to be scaled in this function for the hardware. Things to take
987 * into consideration are how many color registers, if any, are supported with
988 * the current color visual. With truecolor mode no color palettes are
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300989 * supported. Here a pseudo palette is created which we store the value in
York Sun9b53a9e2008-04-28 02:15:34 -0700990 * pseudo_palette in struct fb_info. For pseudocolor mode we have a limited
991 * color palette.
992 */
Timur Tabi4a85dc8b2011-09-15 16:44:46 -0500993static int fsl_diu_setcolreg(unsigned int regno, unsigned int red,
994 unsigned int green, unsigned int blue,
995 unsigned int transp, struct fb_info *info)
York Sun9b53a9e2008-04-28 02:15:34 -0700996{
997 int ret = 1;
998
999 /*
1000 * If greyscale is true, then we convert the RGB value
1001 * to greyscale no matter what visual we are using.
1002 */
1003 if (info->var.grayscale)
1004 red = green = blue = (19595 * red + 38470 * green +
1005 7471 * blue) >> 16;
1006 switch (info->fix.visual) {
1007 case FB_VISUAL_TRUECOLOR:
1008 /*
1009 * 16-bit True Colour. We encode the RGB value
1010 * according to the RGB bitfield information.
1011 */
1012 if (regno < 16) {
1013 u32 *pal = info->pseudo_palette;
1014 u32 v;
1015
1016 red = CNVT_TOHW(red, info->var.red.length);
1017 green = CNVT_TOHW(green, info->var.green.length);
1018 blue = CNVT_TOHW(blue, info->var.blue.length);
1019 transp = CNVT_TOHW(transp, info->var.transp.length);
1020
1021 v = (red << info->var.red.offset) |
1022 (green << info->var.green.offset) |
1023 (blue << info->var.blue.offset) |
1024 (transp << info->var.transp.offset);
1025
1026 pal[regno] = v;
1027 ret = 0;
1028 }
1029 break;
York Sun9b53a9e2008-04-28 02:15:34 -07001030 }
1031
1032 return ret;
1033}
1034
1035/*
1036 * Pan (or wrap, depending on the `vmode' field) the display using the
1037 * 'xoffset' and 'yoffset' fields of the 'var' structure. If the values
1038 * don't fit, return -EINVAL.
1039 */
1040static int fsl_diu_pan_display(struct fb_var_screeninfo *var,
1041 struct fb_info *info)
1042{
1043 if ((info->var.xoffset == var->xoffset) &&
1044 (info->var.yoffset == var->yoffset))
1045 return 0; /* No change, do nothing */
1046
1047 if (var->xoffset < 0 || var->yoffset < 0
1048 || var->xoffset + info->var.xres > info->var.xres_virtual
1049 || var->yoffset + info->var.yres > info->var.yres_virtual)
1050 return -EINVAL;
1051
1052 info->var.xoffset = var->xoffset;
1053 info->var.yoffset = var->yoffset;
1054
1055 if (var->vmode & FB_VMODE_YWRAP)
1056 info->var.vmode |= FB_VMODE_YWRAP;
1057 else
1058 info->var.vmode &= ~FB_VMODE_YWRAP;
1059
York Sunae5591e2008-08-15 00:40:28 -07001060 fsl_diu_set_aoi(info);
1061
York Sun9b53a9e2008-04-28 02:15:34 -07001062 return 0;
1063}
1064
York Sun9b53a9e2008-04-28 02:15:34 -07001065static int fsl_diu_ioctl(struct fb_info *info, unsigned int cmd,
1066 unsigned long arg)
1067{
1068 struct mfb_info *mfbi = info->par;
1069 struct diu_ad *ad = mfbi->ad;
1070 struct mfb_chroma_key ck;
1071 unsigned char global_alpha;
1072 struct aoi_display_offset aoi_d;
1073 __u32 pix_fmt;
1074 void __user *buf = (void __user *)arg;
1075
1076 if (!arg)
1077 return -EINVAL;
1078 switch (cmd) {
Timur Tabi36b0b1d2011-10-04 19:36:44 -05001079 case MFB_SET_PIXFMT_OLD:
1080 dev_warn(info->dev,
1081 "MFB_SET_PIXFMT value of 0x%08x is deprecated.\n",
1082 MFB_SET_PIXFMT_OLD);
York Sun9b53a9e2008-04-28 02:15:34 -07001083 case MFB_SET_PIXFMT:
1084 if (copy_from_user(&pix_fmt, buf, sizeof(pix_fmt)))
1085 return -EFAULT;
1086 ad->pix_fmt = pix_fmt;
York Sun9b53a9e2008-04-28 02:15:34 -07001087 break;
Timur Tabi36b0b1d2011-10-04 19:36:44 -05001088 case MFB_GET_PIXFMT_OLD:
1089 dev_warn(info->dev,
1090 "MFB_GET_PIXFMT value of 0x%08x is deprecated.\n",
1091 MFB_GET_PIXFMT_OLD);
York Sun9b53a9e2008-04-28 02:15:34 -07001092 case MFB_GET_PIXFMT:
1093 pix_fmt = ad->pix_fmt;
1094 if (copy_to_user(buf, &pix_fmt, sizeof(pix_fmt)))
1095 return -EFAULT;
York Sun9b53a9e2008-04-28 02:15:34 -07001096 break;
1097 case MFB_SET_AOID:
1098 if (copy_from_user(&aoi_d, buf, sizeof(aoi_d)))
1099 return -EFAULT;
1100 mfbi->x_aoi_d = aoi_d.x_aoi_d;
1101 mfbi->y_aoi_d = aoi_d.y_aoi_d;
York Sun9b53a9e2008-04-28 02:15:34 -07001102 fsl_diu_check_var(&info->var, info);
York Sunae5591e2008-08-15 00:40:28 -07001103 fsl_diu_set_aoi(info);
York Sun9b53a9e2008-04-28 02:15:34 -07001104 break;
1105 case MFB_GET_AOID:
1106 aoi_d.x_aoi_d = mfbi->x_aoi_d;
1107 aoi_d.y_aoi_d = mfbi->y_aoi_d;
1108 if (copy_to_user(buf, &aoi_d, sizeof(aoi_d)))
1109 return -EFAULT;
York Sun9b53a9e2008-04-28 02:15:34 -07001110 break;
1111 case MFB_GET_ALPHA:
1112 global_alpha = mfbi->g_alpha;
1113 if (copy_to_user(buf, &global_alpha, sizeof(global_alpha)))
1114 return -EFAULT;
York Sun9b53a9e2008-04-28 02:15:34 -07001115 break;
1116 case MFB_SET_ALPHA:
1117 /* set panel information */
1118 if (copy_from_user(&global_alpha, buf, sizeof(global_alpha)))
1119 return -EFAULT;
1120 ad->src_size_g_alpha = (ad->src_size_g_alpha & (~0xff)) |
1121 (global_alpha & 0xff);
1122 mfbi->g_alpha = global_alpha;
York Sun9b53a9e2008-04-28 02:15:34 -07001123 break;
1124 case MFB_SET_CHROMA_KEY:
1125 /* set panel winformation */
1126 if (copy_from_user(&ck, buf, sizeof(ck)))
1127 return -EFAULT;
1128
1129 if (ck.enable &&
1130 (ck.red_max < ck.red_min ||
1131 ck.green_max < ck.green_min ||
1132 ck.blue_max < ck.blue_min))
1133 return -EINVAL;
1134
1135 if (!ck.enable) {
1136 ad->ckmax_r = 0;
1137 ad->ckmax_g = 0;
1138 ad->ckmax_b = 0;
1139 ad->ckmin_r = 255;
1140 ad->ckmin_g = 255;
1141 ad->ckmin_b = 255;
1142 } else {
1143 ad->ckmax_r = ck.red_max;
1144 ad->ckmax_g = ck.green_max;
1145 ad->ckmax_b = ck.blue_max;
1146 ad->ckmin_r = ck.red_min;
1147 ad->ckmin_g = ck.green_min;
1148 ad->ckmin_b = ck.blue_min;
1149 }
York Sun9b53a9e2008-04-28 02:15:34 -07001150 break;
York Sun9b53a9e2008-04-28 02:15:34 -07001151 default:
Timur Tabi154152a2011-09-15 16:44:47 -05001152 dev_err(info->dev, "unknown ioctl command (0x%08X)\n", cmd);
York Sun9b53a9e2008-04-28 02:15:34 -07001153 return -ENOIOCTLCMD;
1154 }
1155
1156 return 0;
1157}
1158
1159/* turn on fb if count == 1
1160 */
1161static int fsl_diu_open(struct fb_info *info, int user)
1162{
1163 struct mfb_info *mfbi = info->par;
1164 int res = 0;
1165
Anatolij Gustschin4b5006e2010-07-23 04:00:37 +00001166 /* free boot splash memory on first /dev/fb0 open */
Timur Tabi2572df92011-09-28 16:19:51 -05001167 if ((mfbi->index == PLANE0) && diu_ops.release_bootmem)
Anatolij Gustschin4b5006e2010-07-23 04:00:37 +00001168 diu_ops.release_bootmem();
1169
York Sun9b53a9e2008-04-28 02:15:34 -07001170 spin_lock(&diu_lock);
1171 mfbi->count++;
1172 if (mfbi->count == 1) {
York Sun9b53a9e2008-04-28 02:15:34 -07001173 fsl_diu_check_var(&info->var, info);
1174 res = fsl_diu_set_par(info);
1175 if (res < 0)
1176 mfbi->count--;
Timur Tabi7e47c212011-09-28 16:19:52 -05001177 else
1178 fsl_diu_enable_panel(info);
York Sun9b53a9e2008-04-28 02:15:34 -07001179 }
1180
1181 spin_unlock(&diu_lock);
1182 return res;
1183}
1184
1185/* turn off fb if count == 0
1186 */
1187static int fsl_diu_release(struct fb_info *info, int user)
1188{
1189 struct mfb_info *mfbi = info->par;
1190 int res = 0;
1191
1192 spin_lock(&diu_lock);
1193 mfbi->count--;
Timur Tabi2572df92011-09-28 16:19:51 -05001194 if (mfbi->count == 0)
1195 fsl_diu_disable_panel(info);
1196
York Sun9b53a9e2008-04-28 02:15:34 -07001197 spin_unlock(&diu_lock);
1198 return res;
1199}
1200
1201static struct fb_ops fsl_diu_ops = {
1202 .owner = THIS_MODULE,
1203 .fb_check_var = fsl_diu_check_var,
1204 .fb_set_par = fsl_diu_set_par,
1205 .fb_setcolreg = fsl_diu_setcolreg,
York Sun9b53a9e2008-04-28 02:15:34 -07001206 .fb_pan_display = fsl_diu_pan_display,
1207 .fb_fillrect = cfb_fillrect,
1208 .fb_copyarea = cfb_copyarea,
1209 .fb_imageblit = cfb_imageblit,
1210 .fb_ioctl = fsl_diu_ioctl,
1211 .fb_open = fsl_diu_open,
1212 .fb_release = fsl_diu_release,
1213};
1214
1215static int init_fbinfo(struct fb_info *info)
1216{
1217 struct mfb_info *mfbi = info->par;
1218
1219 info->device = NULL;
1220 info->var.activate = FB_ACTIVATE_NOW;
1221 info->fbops = &fsl_diu_ops;
1222 info->flags = FBINFO_FLAG_DEFAULT;
1223 info->pseudo_palette = &mfbi->pseudo_palette;
1224
1225 /* Allocate colormap */
1226 fb_alloc_cmap(&info->cmap, 16, 0);
1227 return 0;
1228}
1229
Anton Vorontsov05946bc2008-07-04 09:59:38 -07001230static int __devinit install_fb(struct fb_info *info)
York Sun9b53a9e2008-04-28 02:15:34 -07001231{
1232 int rc;
1233 struct mfb_info *mfbi = info->par;
1234 const char *aoi_mode, *init_aoi_mode = "320x240";
Anatolij Gustschin8b856f02010-07-23 04:00:39 +00001235 struct fb_videomode *db = fsl_diu_mode_db;
1236 unsigned int dbsize = ARRAY_SIZE(fsl_diu_mode_db);
1237 int has_default_mode = 1;
York Sun9b53a9e2008-04-28 02:15:34 -07001238
1239 if (init_fbinfo(info))
1240 return -EINVAL;
1241
Timur Tabi2572df92011-09-28 16:19:51 -05001242 if (mfbi->index == PLANE0) {
Anatolij Gustschin8b856f02010-07-23 04:00:39 +00001243 if (mfbi->edid_data) {
1244 /* Now build modedb from EDID */
1245 fb_edid_to_monspecs(mfbi->edid_data, &info->monspecs);
1246 fb_videomode_to_modelist(info->monspecs.modedb,
1247 info->monspecs.modedb_len,
1248 &info->modelist);
1249 db = info->monspecs.modedb;
1250 dbsize = info->monspecs.modedb_len;
1251 }
York Sun9b53a9e2008-04-28 02:15:34 -07001252 aoi_mode = fb_mode;
Anatolij Gustschin8b856f02010-07-23 04:00:39 +00001253 } else {
York Sun9b53a9e2008-04-28 02:15:34 -07001254 aoi_mode = init_aoi_mode;
Anatolij Gustschin8b856f02010-07-23 04:00:39 +00001255 }
Timur Tabi63cf8df2011-09-15 16:44:51 -05001256 rc = fb_find_mode(&info->var, info, aoi_mode, db, dbsize, NULL,
1257 default_bpp);
Timur Tabi154152a2011-09-15 16:44:47 -05001258 if (!rc) {
Anatolij Gustschin8b856f02010-07-23 04:00:39 +00001259 /*
1260 * For plane 0 we continue and look into
1261 * driver's internal modedb.
1262 */
Timur Tabi2572df92011-09-28 16:19:51 -05001263 if ((mfbi->index == PLANE0) && mfbi->edid_data)
Anatolij Gustschin8b856f02010-07-23 04:00:39 +00001264 has_default_mode = 0;
1265 else
1266 return -EINVAL;
York Sun9b53a9e2008-04-28 02:15:34 -07001267 }
1268
Anatolij Gustschin8b856f02010-07-23 04:00:39 +00001269 if (!has_default_mode) {
1270 rc = fb_find_mode(&info->var, info, aoi_mode, fsl_diu_mode_db,
Timur Tabi63cf8df2011-09-15 16:44:51 -05001271 ARRAY_SIZE(fsl_diu_mode_db), NULL, default_bpp);
1272 if (rc)
Anatolij Gustschin8b856f02010-07-23 04:00:39 +00001273 has_default_mode = 1;
1274 }
1275
1276 /* Still not found, use preferred mode from database if any */
1277 if (!has_default_mode && info->monspecs.modedb) {
1278 struct fb_monspecs *specs = &info->monspecs;
1279 struct fb_videomode *modedb = &specs->modedb[0];
1280
1281 /*
1282 * Get preferred timing. If not found,
1283 * first mode in database will be used.
1284 */
1285 if (specs->misc & FB_MISC_1ST_DETAIL) {
1286 int i;
1287
1288 for (i = 0; i < specs->modedb_len; i++) {
1289 if (specs->modedb[i].flag & FB_MODE_IS_FIRST) {
1290 modedb = &specs->modedb[i];
1291 break;
1292 }
1293 }
1294 }
1295
1296 info->var.bits_per_pixel = default_bpp;
1297 fb_videomode_to_var(&info->var, modedb);
1298 }
1299
York Sun9b53a9e2008-04-28 02:15:34 -07001300 if (fsl_diu_check_var(&info->var, info)) {
Timur Tabi154152a2011-09-15 16:44:47 -05001301 dev_err(info->dev, "fsl_diu_check_var failed\n");
Timur Tabi589c7972011-09-15 16:44:55 -05001302 unmap_video_memory(info);
York Sun9b53a9e2008-04-28 02:15:34 -07001303 fb_dealloc_cmap(&info->cmap);
1304 return -EINVAL;
1305 }
1306
York Sun9b53a9e2008-04-28 02:15:34 -07001307 if (register_framebuffer(info) < 0) {
Timur Tabi154152a2011-09-15 16:44:47 -05001308 dev_err(info->dev, "register_framebuffer failed\n");
York Sun9b53a9e2008-04-28 02:15:34 -07001309 unmap_video_memory(info);
1310 fb_dealloc_cmap(&info->cmap);
1311 return -EINVAL;
1312 }
1313
1314 mfbi->registered = 1;
Timur Tabi154152a2011-09-15 16:44:47 -05001315 dev_info(info->dev, "%s registered successfully\n", mfbi->id);
York Sun9b53a9e2008-04-28 02:15:34 -07001316
1317 return 0;
1318}
1319
Anton Vorontsov05946bc2008-07-04 09:59:38 -07001320static void uninstall_fb(struct fb_info *info)
York Sun9b53a9e2008-04-28 02:15:34 -07001321{
1322 struct mfb_info *mfbi = info->par;
1323
1324 if (!mfbi->registered)
1325 return;
1326
Timur Tabi2572df92011-09-28 16:19:51 -05001327 if (mfbi->index == PLANE0)
Anatolij Gustschin8b856f02010-07-23 04:00:39 +00001328 kfree(mfbi->edid_data);
1329
York Sun9b53a9e2008-04-28 02:15:34 -07001330 unregister_framebuffer(info);
1331 unmap_video_memory(info);
1332 if (&info->cmap)
1333 fb_dealloc_cmap(&info->cmap);
1334
1335 mfbi->registered = 0;
1336}
1337
1338static irqreturn_t fsl_diu_isr(int irq, void *dev_id)
1339{
Timur Tabi3c755b72011-10-04 19:36:51 -05001340 struct diu __iomem *hw = dev_id;
York Sun9b53a9e2008-04-28 02:15:34 -07001341 unsigned int status = in_be32(&hw->int_status);
1342
1343 if (status) {
1344 /* This is the workaround for underrun */
1345 if (status & INT_UNDRUN) {
1346 out_be32(&hw->diu_mode, 0);
York Sun9b53a9e2008-04-28 02:15:34 -07001347 udelay(1);
1348 out_be32(&hw->diu_mode, 1);
1349 }
1350#if defined(CONFIG_NOT_COHERENT_CACHE)
1351 else if (status & INT_VSYNC) {
1352 unsigned int i;
Timur Tabi4a85dc8b2011-09-15 16:44:46 -05001353
York Sun9b53a9e2008-04-28 02:15:34 -07001354 for (i = 0; i < coherence_data_size;
1355 i += d_cache_line_size)
1356 __asm__ __volatile__ (
1357 "dcbz 0, %[input]"
1358 ::[input]"r"(&coherence_data[i]));
1359 }
1360#endif
1361 return IRQ_HANDLED;
1362 }
1363 return IRQ_NONE;
1364}
1365
Timur Tabi3c755b72011-10-04 19:36:51 -05001366static int request_irq_local(struct fsl_diu_data *machine_data)
York Sun9b53a9e2008-04-28 02:15:34 -07001367{
Timur Tabi3c755b72011-10-04 19:36:51 -05001368 struct diu __iomem *hw = machine_data->diu_reg;
Timur Tabibada04f2011-09-15 16:44:52 -05001369 u32 ints;
York Sun9b53a9e2008-04-28 02:15:34 -07001370 int ret;
1371
York Sun9b53a9e2008-04-28 02:15:34 -07001372 /* Read to clear the status */
Timur Tabibada04f2011-09-15 16:44:52 -05001373 in_be32(&hw->int_status);
York Sun9b53a9e2008-04-28 02:15:34 -07001374
Timur Tabi3c755b72011-10-04 19:36:51 -05001375 ret = request_irq(machine_data->irq, fsl_diu_isr, 0, "fsl-diu-fb", hw);
Timur Tabi154152a2011-09-15 16:44:47 -05001376 if (!ret) {
York Sun9b53a9e2008-04-28 02:15:34 -07001377 ints = INT_PARERR | INT_LS_BF_VS;
1378#if !defined(CONFIG_NOT_COHERENT_CACHE)
1379 ints |= INT_VSYNC;
1380#endif
Timur Tabi4a85dc8b2011-09-15 16:44:46 -05001381
York Sun9b53a9e2008-04-28 02:15:34 -07001382 /* Read to clear the status */
Timur Tabibada04f2011-09-15 16:44:52 -05001383 in_be32(&hw->int_status);
York Sun9b53a9e2008-04-28 02:15:34 -07001384 out_be32(&hw->int_mask, ints);
1385 }
Timur Tabi4a85dc8b2011-09-15 16:44:46 -05001386
York Sun9b53a9e2008-04-28 02:15:34 -07001387 return ret;
1388}
1389
Timur Tabi3c755b72011-10-04 19:36:51 -05001390static void free_irq_local(struct fsl_diu_data *machine_data)
York Sun9b53a9e2008-04-28 02:15:34 -07001391{
Timur Tabi3c755b72011-10-04 19:36:51 -05001392 struct diu __iomem *hw = machine_data->diu_reg;
York Sun9b53a9e2008-04-28 02:15:34 -07001393
1394 /* Disable all LCDC interrupt */
1395 out_be32(&hw->int_mask, 0x1f);
1396
Timur Tabi3c755b72011-10-04 19:36:51 -05001397 free_irq(machine_data->irq, NULL);
York Sun9b53a9e2008-04-28 02:15:34 -07001398}
1399
1400#ifdef CONFIG_PM
1401/*
1402 * Power management hooks. Note that we won't be called from IRQ context,
1403 * unlike the blank functions above, so we may sleep.
1404 */
Grant Likely2dc11582010-08-06 09:25:50 -06001405static int fsl_diu_suspend(struct platform_device *ofdev, pm_message_t state)
York Sun9b53a9e2008-04-28 02:15:34 -07001406{
1407 struct fsl_diu_data *machine_data;
1408
Takashi Iwai48948a32008-07-08 18:41:17 +02001409 machine_data = dev_get_drvdata(&ofdev->dev);
York Sun9b53a9e2008-04-28 02:15:34 -07001410 disable_lcdc(machine_data->fsl_diu_info[0]);
1411
1412 return 0;
1413}
1414
Grant Likely2dc11582010-08-06 09:25:50 -06001415static int fsl_diu_resume(struct platform_device *ofdev)
York Sun9b53a9e2008-04-28 02:15:34 -07001416{
1417 struct fsl_diu_data *machine_data;
1418
Takashi Iwai48948a32008-07-08 18:41:17 +02001419 machine_data = dev_get_drvdata(&ofdev->dev);
York Sun9b53a9e2008-04-28 02:15:34 -07001420 enable_lcdc(machine_data->fsl_diu_info[0]);
1421
1422 return 0;
1423}
1424
1425#else
1426#define fsl_diu_suspend NULL
1427#define fsl_diu_resume NULL
1428#endif /* CONFIG_PM */
1429
1430/* Align to 64-bit(8-byte), 32-byte, etc. */
Anton Vorontsovf3791882009-04-04 22:31:20 +04001431static int allocate_buf(struct device *dev, struct diu_addr *buf, u32 size,
1432 u32 bytes_align)
York Sun9b53a9e2008-04-28 02:15:34 -07001433{
Timur Tabibada04f2011-09-15 16:44:52 -05001434 u32 offset;
1435 dma_addr_t mask;
York Sun9b53a9e2008-04-28 02:15:34 -07001436
Timur Tabibada04f2011-09-15 16:44:52 -05001437 buf->vaddr =
1438 dma_alloc_coherent(dev, size + bytes_align, &buf->paddr,
1439 GFP_DMA | __GFP_ZERO);
York Sun9b53a9e2008-04-28 02:15:34 -07001440 if (!buf->vaddr)
1441 return -ENOMEM;
1442
York Sun9b53a9e2008-04-28 02:15:34 -07001443 mask = bytes_align - 1;
Timur Tabibada04f2011-09-15 16:44:52 -05001444 offset = buf->paddr & mask;
York Sun9b53a9e2008-04-28 02:15:34 -07001445 if (offset) {
1446 buf->offset = bytes_align - offset;
Timur Tabibada04f2011-09-15 16:44:52 -05001447 buf->paddr = buf->paddr + offset;
York Sun9b53a9e2008-04-28 02:15:34 -07001448 } else
1449 buf->offset = 0;
Timur Tabi4a85dc8b2011-09-15 16:44:46 -05001450
York Sun9b53a9e2008-04-28 02:15:34 -07001451 return 0;
1452}
1453
Anton Vorontsovf3791882009-04-04 22:31:20 +04001454static void free_buf(struct device *dev, struct diu_addr *buf, u32 size,
1455 u32 bytes_align)
York Sun9b53a9e2008-04-28 02:15:34 -07001456{
Timur Tabi4a85dc8b2011-09-15 16:44:46 -05001457 dma_free_coherent(dev, size + bytes_align, buf->vaddr,
1458 buf->paddr - buf->offset);
York Sun9b53a9e2008-04-28 02:15:34 -07001459}
1460
1461static ssize_t store_monitor(struct device *device,
1462 struct device_attribute *attr, const char *buf, size_t count)
1463{
Timur Tabi7653aaa2011-07-09 15:38:14 -05001464 enum fsl_diu_monitor_port old_monitor_port;
York Sun9b53a9e2008-04-28 02:15:34 -07001465 struct fsl_diu_data *machine_data =
1466 container_of(attr, struct fsl_diu_data, dev_attr);
1467
York Sun9b53a9e2008-04-28 02:15:34 -07001468 old_monitor_port = machine_data->monitor_port;
Timur Tabi7653aaa2011-07-09 15:38:14 -05001469 machine_data->monitor_port = fsl_diu_name_to_port(buf);
York Sun9b53a9e2008-04-28 02:15:34 -07001470
1471 if (old_monitor_port != machine_data->monitor_port) {
1472 /* All AOIs need adjust pixel format
1473 * fsl_diu_set_par only change the pixsel format here
1474 * unlikely to fail. */
1475 fsl_diu_set_par(machine_data->fsl_diu_info[0]);
1476 fsl_diu_set_par(machine_data->fsl_diu_info[1]);
1477 fsl_diu_set_par(machine_data->fsl_diu_info[2]);
1478 fsl_diu_set_par(machine_data->fsl_diu_info[3]);
1479 fsl_diu_set_par(machine_data->fsl_diu_info[4]);
1480 }
1481 return count;
1482}
1483
1484static ssize_t show_monitor(struct device *device,
1485 struct device_attribute *attr, char *buf)
1486{
1487 struct fsl_diu_data *machine_data =
1488 container_of(attr, struct fsl_diu_data, dev_attr);
Timur Tabi7653aaa2011-07-09 15:38:14 -05001489
1490 switch (machine_data->monitor_port) {
1491 case FSL_DIU_PORT_DVI:
1492 return sprintf(buf, "DVI\n");
1493 case FSL_DIU_PORT_LVDS:
1494 return sprintf(buf, "Single-link LVDS\n");
1495 case FSL_DIU_PORT_DLVDS:
1496 return sprintf(buf, "Dual-link LVDS\n");
1497 }
1498
1499 return 0;
York Sun9b53a9e2008-04-28 02:15:34 -07001500}
1501
Timur Tabi9e52ba62011-09-15 16:44:50 -05001502static int __devinit fsl_diu_probe(struct platform_device *pdev)
York Sun9b53a9e2008-04-28 02:15:34 -07001503{
Timur Tabi9e52ba62011-09-15 16:44:50 -05001504 struct device_node *np = pdev->dev.of_node;
York Sun9b53a9e2008-04-28 02:15:34 -07001505 struct mfb_info *mfbi;
Timur Tabi89f08e32011-09-15 16:44:49 -05001506 phys_addr_t dummy_ad_addr = 0;
York Sun9b53a9e2008-04-28 02:15:34 -07001507 int ret, i, error = 0;
York Sun9b53a9e2008-04-28 02:15:34 -07001508 struct fsl_diu_data *machine_data;
Anatolij Gustschin4b5006e2010-07-23 04:00:37 +00001509 int diu_mode;
York Sun9b53a9e2008-04-28 02:15:34 -07001510
1511 machine_data = kzalloc(sizeof(struct fsl_diu_data), GFP_KERNEL);
1512 if (!machine_data)
1513 return -ENOMEM;
1514
Timur Tabi3c755b72011-10-04 19:36:51 -05001515 spin_lock_init(&machine_data->reg_lock);
1516
York Sun9b53a9e2008-04-28 02:15:34 -07001517 for (i = 0; i < ARRAY_SIZE(machine_data->fsl_diu_info); i++) {
1518 machine_data->fsl_diu_info[i] =
Timur Tabi9e52ba62011-09-15 16:44:50 -05001519 framebuffer_alloc(sizeof(struct mfb_info), &pdev->dev);
York Sun9b53a9e2008-04-28 02:15:34 -07001520 if (!machine_data->fsl_diu_info[i]) {
Timur Tabi9e52ba62011-09-15 16:44:50 -05001521 dev_err(&pdev->dev, "cannot allocate memory\n");
York Sun9b53a9e2008-04-28 02:15:34 -07001522 ret = -ENOMEM;
1523 goto error2;
1524 }
1525 mfbi = machine_data->fsl_diu_info[i]->par;
1526 memcpy(mfbi, &mfb_template[i], sizeof(struct mfb_info));
1527 mfbi->parent = machine_data;
Anatolij Gustschin8b856f02010-07-23 04:00:39 +00001528
Timur Tabi2572df92011-09-28 16:19:51 -05001529 if (mfbi->index == PLANE0) {
Anatolij Gustschin8b856f02010-07-23 04:00:39 +00001530 const u8 *prop;
1531 int len;
1532
1533 /* Get EDID */
1534 prop = of_get_property(np, "edid", &len);
1535 if (prop && len == EDID_LENGTH)
1536 mfbi->edid_data = kmemdup(prop, EDID_LENGTH,
1537 GFP_KERNEL);
1538 }
York Sun9b53a9e2008-04-28 02:15:34 -07001539 }
1540
Timur Tabi3c755b72011-10-04 19:36:51 -05001541 machine_data->diu_reg = of_iomap(np, 0);
1542 if (!machine_data->diu_reg) {
Timur Tabi9e52ba62011-09-15 16:44:50 -05001543 dev_err(&pdev->dev, "cannot map DIU registers\n");
York Sun9b53a9e2008-04-28 02:15:34 -07001544 ret = -EFAULT;
1545 goto error2;
1546 }
1547
Timur Tabi3c755b72011-10-04 19:36:51 -05001548 diu_mode = in_be32(&machine_data->diu_reg->diu_mode);
Timur Tabic4e5a022011-09-28 16:19:53 -05001549 if (diu_mode == MFB_MODE0)
Timur Tabi3c755b72011-10-04 19:36:51 -05001550 out_be32(&machine_data->diu_reg->diu_mode, 0); /* disable DIU */
York Sun9b53a9e2008-04-28 02:15:34 -07001551
1552 /* Get the IRQ of the DIU */
1553 machine_data->irq = irq_of_parse_and_map(np, 0);
1554
1555 if (!machine_data->irq) {
Timur Tabi9e52ba62011-09-15 16:44:50 -05001556 dev_err(&pdev->dev, "could not get DIU IRQ\n");
York Sun9b53a9e2008-04-28 02:15:34 -07001557 ret = -EINVAL;
1558 goto error;
1559 }
1560 machine_data->monitor_port = monitor_port;
1561
1562 /* Area descriptor memory pool aligns to 64-bit boundary */
Timur Tabi9e52ba62011-09-15 16:44:50 -05001563 if (allocate_buf(&pdev->dev, &pool.ad,
Anton Vorontsovf3791882009-04-04 22:31:20 +04001564 sizeof(struct diu_ad) * FSL_AOI_NUM, 8))
York Sun9b53a9e2008-04-28 02:15:34 -07001565 return -ENOMEM;
1566
1567 /* Get memory for Gamma Table - 32-byte aligned memory */
Timur Tabi9e52ba62011-09-15 16:44:50 -05001568 if (allocate_buf(&pdev->dev, &pool.gamma, 768, 32)) {
York Sun9b53a9e2008-04-28 02:15:34 -07001569 ret = -ENOMEM;
1570 goto error;
1571 }
1572
1573 /* For performance, cursor bitmap buffer aligns to 32-byte boundary */
Timur Tabi9e52ba62011-09-15 16:44:50 -05001574 if (allocate_buf(&pdev->dev, &pool.cursor, MAX_CURS * MAX_CURS * 2,
Anton Vorontsovf3791882009-04-04 22:31:20 +04001575 32)) {
York Sun9b53a9e2008-04-28 02:15:34 -07001576 ret = -ENOMEM;
1577 goto error;
1578 }
1579
1580 i = ARRAY_SIZE(machine_data->fsl_diu_info);
1581 machine_data->dummy_ad = (struct diu_ad *)
1582 ((u32)pool.ad.vaddr + pool.ad.offset) + i;
1583 machine_data->dummy_ad->paddr = pool.ad.paddr +
1584 i * sizeof(struct diu_ad);
1585 machine_data->dummy_aoi_virt = fsl_diu_alloc(64, &dummy_ad_addr);
1586 if (!machine_data->dummy_aoi_virt) {
1587 ret = -ENOMEM;
1588 goto error;
1589 }
1590 machine_data->dummy_ad->addr = cpu_to_le32(dummy_ad_addr);
1591 machine_data->dummy_ad->pix_fmt = 0x88882317;
1592 machine_data->dummy_ad->src_size_g_alpha = cpu_to_le32((4 << 12) | 4);
1593 machine_data->dummy_ad->aoi_size = cpu_to_le32((4 << 16) | 2);
1594 machine_data->dummy_ad->offset_xyi = 0;
1595 machine_data->dummy_ad->offset_xyd = 0;
1596 machine_data->dummy_ad->next_ad = 0;
1597
Anatolij Gustschin4b5006e2010-07-23 04:00:37 +00001598 /*
1599 * Let DIU display splash screen if it was pre-initialized
1600 * by the bootloader, set dummy area descriptor otherwise.
1601 */
Timur Tabic4e5a022011-09-28 16:19:53 -05001602 if (diu_mode == MFB_MODE0)
Timur Tabi3c755b72011-10-04 19:36:51 -05001603 out_be32(&machine_data->diu_reg->desc[0],
1604 machine_data->dummy_ad->paddr);
Anatolij Gustschin4b5006e2010-07-23 04:00:37 +00001605
Timur Tabi3c755b72011-10-04 19:36:51 -05001606 out_be32(&machine_data->diu_reg->desc[1], machine_data->dummy_ad->paddr);
1607 out_be32(&machine_data->diu_reg->desc[2], machine_data->dummy_ad->paddr);
York Sun9b53a9e2008-04-28 02:15:34 -07001608
1609 for (i = 0; i < ARRAY_SIZE(machine_data->fsl_diu_info); i++) {
1610 machine_data->fsl_diu_info[i]->fix.smem_start = 0;
1611 mfbi = machine_data->fsl_diu_info[i]->par;
1612 mfbi->ad = (struct diu_ad *)((u32)pool.ad.vaddr
1613 + pool.ad.offset) + i;
1614 mfbi->ad->paddr = pool.ad.paddr + i * sizeof(struct diu_ad);
1615 ret = install_fb(machine_data->fsl_diu_info[i]);
1616 if (ret) {
Timur Tabi9e52ba62011-09-15 16:44:50 -05001617 dev_err(&pdev->dev, "could not register fb %d\n", i);
York Sun9b53a9e2008-04-28 02:15:34 -07001618 goto error;
1619 }
1620 }
1621
Timur Tabi3c755b72011-10-04 19:36:51 -05001622 if (request_irq_local(machine_data)) {
Timur Tabi9e52ba62011-09-15 16:44:50 -05001623 dev_err(&pdev->dev, "could not claim irq\n");
York Sun9b53a9e2008-04-28 02:15:34 -07001624 goto error;
1625 }
1626
Wolfram Sang12765512010-04-06 14:34:52 -07001627 sysfs_attr_init(&machine_data->dev_attr.attr);
York Sun9b53a9e2008-04-28 02:15:34 -07001628 machine_data->dev_attr.attr.name = "monitor";
1629 machine_data->dev_attr.attr.mode = S_IRUGO|S_IWUSR;
1630 machine_data->dev_attr.show = show_monitor;
1631 machine_data->dev_attr.store = store_monitor;
1632 error = device_create_file(machine_data->fsl_diu_info[0]->dev,
1633 &machine_data->dev_attr);
1634 if (error) {
Timur Tabi9e52ba62011-09-15 16:44:50 -05001635 dev_err(&pdev->dev, "could not create sysfs file %s\n",
York Sun9b53a9e2008-04-28 02:15:34 -07001636 machine_data->dev_attr.attr.name);
1637 }
1638
Timur Tabi9e52ba62011-09-15 16:44:50 -05001639 dev_set_drvdata(&pdev->dev, machine_data);
York Sun9b53a9e2008-04-28 02:15:34 -07001640 return 0;
1641
1642error:
Timur Tabi3f78bbd2011-09-15 16:44:56 -05001643 for (i = 0; i < ARRAY_SIZE(machine_data->fsl_diu_info); i++)
1644 uninstall_fb(machine_data->fsl_diu_info[i]);
1645
York Sun9b53a9e2008-04-28 02:15:34 -07001646 if (pool.ad.vaddr)
Timur Tabi9e52ba62011-09-15 16:44:50 -05001647 free_buf(&pdev->dev, &pool.ad,
Anton Vorontsovf3791882009-04-04 22:31:20 +04001648 sizeof(struct diu_ad) * FSL_AOI_NUM, 8);
York Sun9b53a9e2008-04-28 02:15:34 -07001649 if (pool.gamma.vaddr)
Timur Tabi9e52ba62011-09-15 16:44:50 -05001650 free_buf(&pdev->dev, &pool.gamma, 768, 32);
York Sun9b53a9e2008-04-28 02:15:34 -07001651 if (pool.cursor.vaddr)
Timur Tabi9e52ba62011-09-15 16:44:50 -05001652 free_buf(&pdev->dev, &pool.cursor, MAX_CURS * MAX_CURS * 2,
Anton Vorontsovf3791882009-04-04 22:31:20 +04001653 32);
York Sun9b53a9e2008-04-28 02:15:34 -07001654 if (machine_data->dummy_aoi_virt)
1655 fsl_diu_free(machine_data->dummy_aoi_virt, 64);
Timur Tabi3c755b72011-10-04 19:36:51 -05001656 iounmap(machine_data->diu_reg);
York Sun9b53a9e2008-04-28 02:15:34 -07001657
1658error2:
1659 for (i = 0; i < ARRAY_SIZE(machine_data->fsl_diu_info); i++)
1660 if (machine_data->fsl_diu_info[i])
1661 framebuffer_release(machine_data->fsl_diu_info[i]);
1662 kfree(machine_data);
1663
1664 return ret;
1665}
1666
Timur Tabi9e52ba62011-09-15 16:44:50 -05001667static int fsl_diu_remove(struct platform_device *pdev)
York Sun9b53a9e2008-04-28 02:15:34 -07001668{
1669 struct fsl_diu_data *machine_data;
1670 int i;
1671
Timur Tabi9e52ba62011-09-15 16:44:50 -05001672 machine_data = dev_get_drvdata(&pdev->dev);
York Sun9b53a9e2008-04-28 02:15:34 -07001673 disable_lcdc(machine_data->fsl_diu_info[0]);
Timur Tabi3c755b72011-10-04 19:36:51 -05001674 free_irq_local(machine_data);
Timur Tabi3f78bbd2011-09-15 16:44:56 -05001675 for (i = 0; i < ARRAY_SIZE(machine_data->fsl_diu_info); i++)
1676 uninstall_fb(machine_data->fsl_diu_info[i]);
York Sun9b53a9e2008-04-28 02:15:34 -07001677 if (pool.ad.vaddr)
Timur Tabi9e52ba62011-09-15 16:44:50 -05001678 free_buf(&pdev->dev, &pool.ad,
Anton Vorontsovf3791882009-04-04 22:31:20 +04001679 sizeof(struct diu_ad) * FSL_AOI_NUM, 8);
York Sun9b53a9e2008-04-28 02:15:34 -07001680 if (pool.gamma.vaddr)
Timur Tabi9e52ba62011-09-15 16:44:50 -05001681 free_buf(&pdev->dev, &pool.gamma, 768, 32);
York Sun9b53a9e2008-04-28 02:15:34 -07001682 if (pool.cursor.vaddr)
Timur Tabi9e52ba62011-09-15 16:44:50 -05001683 free_buf(&pdev->dev, &pool.cursor, MAX_CURS * MAX_CURS * 2, 32);
York Sun9b53a9e2008-04-28 02:15:34 -07001684 if (machine_data->dummy_aoi_virt)
1685 fsl_diu_free(machine_data->dummy_aoi_virt, 64);
Timur Tabi3c755b72011-10-04 19:36:51 -05001686 iounmap(machine_data->diu_reg);
York Sun9b53a9e2008-04-28 02:15:34 -07001687 for (i = 0; i < ARRAY_SIZE(machine_data->fsl_diu_info); i++)
1688 if (machine_data->fsl_diu_info[i])
1689 framebuffer_release(machine_data->fsl_diu_info[i]);
1690 kfree(machine_data);
1691
1692 return 0;
1693}
1694
1695#ifndef MODULE
1696static int __init fsl_diu_setup(char *options)
1697{
1698 char *opt;
1699 unsigned long val;
1700
1701 if (!options || !*options)
1702 return 0;
1703
1704 while ((opt = strsep(&options, ",")) != NULL) {
1705 if (!*opt)
1706 continue;
1707 if (!strncmp(opt, "monitor=", 8)) {
Timur Tabi7653aaa2011-07-09 15:38:14 -05001708 monitor_port = fsl_diu_name_to_port(opt + 8);
York Sun9b53a9e2008-04-28 02:15:34 -07001709 } else if (!strncmp(opt, "bpp=", 4)) {
1710 if (!strict_strtoul(opt + 4, 10, &val))
1711 default_bpp = val;
1712 } else
1713 fb_mode = opt;
1714 }
1715
1716 return 0;
1717}
1718#endif
1719
1720static struct of_device_id fsl_diu_match[] = {
Anatolij Gustschind24720a2010-02-17 07:33:22 -07001721#ifdef CONFIG_PPC_MPC512x
1722 {
1723 .compatible = "fsl,mpc5121-diu",
1724 },
1725#endif
York Sun9b53a9e2008-04-28 02:15:34 -07001726 {
1727 .compatible = "fsl,diu",
1728 },
1729 {}
1730};
1731MODULE_DEVICE_TABLE(of, fsl_diu_match);
1732
Grant Likely28541d02011-02-22 21:07:43 -07001733static struct platform_driver fsl_diu_driver = {
Grant Likely40182942010-04-13 16:13:02 -07001734 .driver = {
Timur Tabif8c6bf62011-09-15 16:44:53 -05001735 .name = "fsl-diu-fb",
Grant Likely40182942010-04-13 16:13:02 -07001736 .owner = THIS_MODULE,
1737 .of_match_table = fsl_diu_match,
1738 },
York Sun9b53a9e2008-04-28 02:15:34 -07001739 .probe = fsl_diu_probe,
1740 .remove = fsl_diu_remove,
1741 .suspend = fsl_diu_suspend,
1742 .resume = fsl_diu_resume,
1743};
1744
1745static int __init fsl_diu_init(void)
1746{
1747#ifdef CONFIG_NOT_COHERENT_CACHE
1748 struct device_node *np;
1749 const u32 *prop;
1750#endif
1751 int ret;
1752#ifndef MODULE
1753 char *option;
1754
1755 /*
1756 * For kernel boot options (in 'video=xxxfb:<options>' format)
1757 */
1758 if (fb_get_options("fslfb", &option))
1759 return -ENODEV;
1760 fsl_diu_setup(option);
Timur Tabi7653aaa2011-07-09 15:38:14 -05001761#else
1762 monitor_port = fsl_diu_name_to_port(monitor_string);
York Sun9b53a9e2008-04-28 02:15:34 -07001763#endif
Timur Tabi154152a2011-09-15 16:44:47 -05001764 pr_info("Freescale Display Interface Unit (DIU) framebuffer driver\n");
York Sun9b53a9e2008-04-28 02:15:34 -07001765
1766#ifdef CONFIG_NOT_COHERENT_CACHE
1767 np = of_find_node_by_type(NULL, "cpu");
1768 if (!np) {
Timur Tabi154152a2011-09-15 16:44:47 -05001769 pr_err("fsl-diu-fb: can't find 'cpu' device node\n");
York Sun9b53a9e2008-04-28 02:15:34 -07001770 return -ENODEV;
1771 }
1772
1773 prop = of_get_property(np, "d-cache-size", NULL);
Julia Lawall5394ba02008-08-05 13:01:28 -07001774 if (prop == NULL) {
Timur Tabi154152a2011-09-15 16:44:47 -05001775 pr_err("fsl-diu-fb: missing 'd-cache-size' property' "
1776 "in 'cpu' node\n");
Julia Lawall5394ba02008-08-05 13:01:28 -07001777 of_node_put(np);
York Sun9b53a9e2008-04-28 02:15:34 -07001778 return -ENODEV;
Julia Lawall5394ba02008-08-05 13:01:28 -07001779 }
York Sun9b53a9e2008-04-28 02:15:34 -07001780
Timur Tabi4a85dc8b2011-09-15 16:44:46 -05001781 /*
1782 * Freescale PLRU requires 13/8 times the cache size to do a proper
1783 * displacement flush
York Sun9b53a9e2008-04-28 02:15:34 -07001784 */
Timur Tabi9e52ba62011-09-15 16:44:50 -05001785 coherence_data_size = be32_to_cpup(prop) * 13;
York Sun9b53a9e2008-04-28 02:15:34 -07001786 coherence_data_size /= 8;
1787
1788 prop = of_get_property(np, "d-cache-line-size", NULL);
Julia Lawall5394ba02008-08-05 13:01:28 -07001789 if (prop == NULL) {
Timur Tabi154152a2011-09-15 16:44:47 -05001790 pr_err("fsl-diu-fb: missing 'd-cache-line-size' property' "
1791 "in 'cpu' node\n");
Julia Lawall5394ba02008-08-05 13:01:28 -07001792 of_node_put(np);
York Sun9b53a9e2008-04-28 02:15:34 -07001793 return -ENODEV;
Julia Lawall5394ba02008-08-05 13:01:28 -07001794 }
Timur Tabi9e52ba62011-09-15 16:44:50 -05001795 d_cache_line_size = be32_to_cpup(prop);
York Sun9b53a9e2008-04-28 02:15:34 -07001796
1797 of_node_put(np);
1798 coherence_data = vmalloc(coherence_data_size);
1799 if (!coherence_data)
1800 return -ENOMEM;
1801#endif
Timur Tabi4a85dc8b2011-09-15 16:44:46 -05001802
Grant Likely28541d02011-02-22 21:07:43 -07001803 ret = platform_driver_register(&fsl_diu_driver);
York Sun9b53a9e2008-04-28 02:15:34 -07001804 if (ret) {
Timur Tabi154152a2011-09-15 16:44:47 -05001805 pr_err("fsl-diu-fb: failed to register platform driver\n");
York Sun9b53a9e2008-04-28 02:15:34 -07001806#if defined(CONFIG_NOT_COHERENT_CACHE)
1807 vfree(coherence_data);
1808#endif
York Sun9b53a9e2008-04-28 02:15:34 -07001809 }
1810 return ret;
1811}
1812
1813static void __exit fsl_diu_exit(void)
1814{
Grant Likely28541d02011-02-22 21:07:43 -07001815 platform_driver_unregister(&fsl_diu_driver);
York Sun9b53a9e2008-04-28 02:15:34 -07001816#if defined(CONFIG_NOT_COHERENT_CACHE)
1817 vfree(coherence_data);
1818#endif
1819}
1820
1821module_init(fsl_diu_init);
1822module_exit(fsl_diu_exit);
1823
1824MODULE_AUTHOR("York Sun <yorksun@freescale.com>");
1825MODULE_DESCRIPTION("Freescale DIU framebuffer driver");
1826MODULE_LICENSE("GPL");
1827
1828module_param_named(mode, fb_mode, charp, 0);
1829MODULE_PARM_DESC(mode,
1830 "Specify resolution as \"<xres>x<yres>[-<bpp>][@<refresh>]\" ");
1831module_param_named(bpp, default_bpp, ulong, 0);
Timur Tabi154152a2011-09-15 16:44:47 -05001832MODULE_PARM_DESC(bpp, "Specify bit-per-pixel if not specified in 'mode'");
Timur Tabi7653aaa2011-07-09 15:38:14 -05001833module_param_named(monitor, monitor_string, charp, 0);
1834MODULE_PARM_DESC(monitor, "Specify the monitor port "
1835 "(\"dvi\", \"lvds\", or \"dlvds\") if supported by the platform");
York Sun9b53a9e2008-04-28 02:15:34 -07001836