blob: 6e32ba83aa5b8c569d194bdf9d4519f0fd5c900c [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 Tabiddd3d902011-12-02 17:03:27 -060039#define NUM_AOIS 5 /* 1 for plane 0, 2 for planes 1 & 2 each */
Timur Tabib715f9f2011-09-28 16:19:48 -050040
41/* HW cursor parameters */
42#define MAX_CURS 32
43
44/* INT_STATUS/INT_MASK field descriptions */
45#define INT_VSYNC 0x01 /* Vsync interrupt */
46#define INT_VSYNC_WB 0x02 /* Vsync interrupt for write back operation */
47#define INT_UNDRUN 0x04 /* Under run exception interrupt */
48#define INT_PARERR 0x08 /* Display parameters error interrupt */
49#define INT_LS_BF_VS 0x10 /* Lines before vsync. interrupt */
50
York Sun9b53a9e2008-04-28 02:15:34 -070051/*
Timur Tabi63cf8df2011-09-15 16:44:51 -050052 * List of supported video modes
53 *
Timur Tabi760af8f2011-09-28 16:19:50 -050054 * The first entry is the default video mode. The remain entries are in
55 * order if increasing resolution and frequency. The 320x240-60 mode is
56 * the initial AOI for the second and third planes.
York Sun9b53a9e2008-04-28 02:15:34 -070057 */
York Sun9b53a9e2008-04-28 02:15:34 -070058static struct fb_videomode __devinitdata fsl_diu_mode_db[] = {
59 {
York Sun9b53a9e2008-04-28 02:15:34 -070060 .refresh = 60,
61 .xres = 1024,
62 .yres = 768,
63 .pixclock = 15385,
64 .left_margin = 160,
65 .right_margin = 24,
66 .upper_margin = 29,
67 .lower_margin = 3,
68 .hsync_len = 136,
69 .vsync_len = 6,
70 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
71 .vmode = FB_VMODE_NONINTERLACED
72 },
73 {
Timur Tabi760af8f2011-09-28 16:19:50 -050074 .refresh = 60,
75 .xres = 320,
76 .yres = 240,
77 .pixclock = 79440,
78 .left_margin = 16,
79 .right_margin = 16,
80 .upper_margin = 16,
81 .lower_margin = 5,
82 .hsync_len = 48,
83 .vsync_len = 1,
84 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
85 .vmode = FB_VMODE_NONINTERLACED
86 },
87 {
88 .refresh = 60,
89 .xres = 640,
90 .yres = 480,
91 .pixclock = 39722,
92 .left_margin = 48,
93 .right_margin = 16,
94 .upper_margin = 33,
95 .lower_margin = 10,
96 .hsync_len = 96,
97 .vsync_len = 2,
98 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
99 .vmode = FB_VMODE_NONINTERLACED
100 },
101 {
102 .refresh = 72,
103 .xres = 640,
104 .yres = 480,
105 .pixclock = 32052,
106 .left_margin = 128,
107 .right_margin = 24,
108 .upper_margin = 28,
109 .lower_margin = 9,
110 .hsync_len = 40,
111 .vsync_len = 3,
112 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
113 .vmode = FB_VMODE_NONINTERLACED
114 },
115 {
116 .refresh = 75,
117 .xres = 640,
118 .yres = 480,
119 .pixclock = 31747,
120 .left_margin = 120,
121 .right_margin = 16,
122 .upper_margin = 16,
123 .lower_margin = 1,
124 .hsync_len = 64,
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 = 90,
131 .xres = 640,
132 .yres = 480,
133 .pixclock = 25057,
134 .left_margin = 120,
135 .right_margin = 32,
136 .upper_margin = 14,
137 .lower_margin = 25,
138 .hsync_len = 40,
139 .vsync_len = 14,
140 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
141 .vmode = FB_VMODE_NONINTERLACED
142 },
143 {
144 .refresh = 100,
145 .xres = 640,
146 .yres = 480,
147 .pixclock = 22272,
148 .left_margin = 48,
149 .right_margin = 32,
150 .upper_margin = 17,
151 .lower_margin = 22,
152 .hsync_len = 128,
153 .vsync_len = 12,
154 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
155 .vmode = FB_VMODE_NONINTERLACED
156 },
157 {
158 .refresh = 60,
159 .xres = 800,
160 .yres = 480,
161 .pixclock = 33805,
162 .left_margin = 96,
163 .right_margin = 24,
164 .upper_margin = 10,
165 .lower_margin = 3,
166 .hsync_len = 72,
167 .vsync_len = 7,
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 = 600,
175 .pixclock = 25000,
176 .left_margin = 88,
177 .right_margin = 40,
178 .upper_margin = 23,
179 .lower_margin = 1,
180 .hsync_len = 128,
181 .vsync_len = 4,
182 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
183 .vmode = FB_VMODE_NONINTERLACED
184 },
185 {
186 .refresh = 60,
187 .xres = 854,
188 .yres = 480,
189 .pixclock = 31518,
190 .left_margin = 104,
191 .right_margin = 16,
192 .upper_margin = 13,
193 .lower_margin = 1,
194 .hsync_len = 88,
195 .vsync_len = 3,
196 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
197 .vmode = FB_VMODE_NONINTERLACED
198 },
199 {
York Sun9b53a9e2008-04-28 02:15:34 -0700200 .refresh = 70,
201 .xres = 1024,
202 .yres = 768,
203 .pixclock = 16886,
204 .left_margin = 3,
205 .right_margin = 3,
206 .upper_margin = 2,
207 .lower_margin = 2,
208 .hsync_len = 40,
209 .vsync_len = 18,
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 = 75,
215 .xres = 1024,
216 .yres = 768,
217 .pixclock = 15009,
218 .left_margin = 3,
219 .right_margin = 3,
220 .upper_margin = 2,
221 .lower_margin = 2,
222 .hsync_len = 80,
223 .vsync_len = 32,
224 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
225 .vmode = FB_VMODE_NONINTERLACED
226 },
227 {
Timur Tabi760af8f2011-09-28 16:19:50 -0500228 .refresh = 60,
229 .xres = 1280,
230 .yres = 480,
231 .pixclock = 18939,
232 .left_margin = 353,
233 .right_margin = 47,
234 .upper_margin = 39,
235 .lower_margin = 4,
236 .hsync_len = 8,
237 .vsync_len = 2,
238 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
239 .vmode = FB_VMODE_NONINTERLACED
240 },
241 {
242 .refresh = 60,
243 .xres = 1280,
244 .yres = 720,
245 .pixclock = 13426,
246 .left_margin = 192,
247 .right_margin = 64,
248 .upper_margin = 22,
249 .lower_margin = 1,
250 .hsync_len = 136,
251 .vsync_len = 3,
252 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
253 .vmode = FB_VMODE_NONINTERLACED
254 },
255 {
York Sun9b53a9e2008-04-28 02:15:34 -0700256 .refresh = 60,
257 .xres = 1280,
258 .yres = 1024,
259 .pixclock = 9375,
260 .left_margin = 38,
261 .right_margin = 128,
262 .upper_margin = 2,
263 .lower_margin = 7,
264 .hsync_len = 216,
265 .vsync_len = 37,
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 = 70,
271 .xres = 1280,
272 .yres = 1024,
273 .pixclock = 9380,
274 .left_margin = 6,
275 .right_margin = 6,
276 .upper_margin = 4,
277 .lower_margin = 4,
278 .hsync_len = 60,
279 .vsync_len = 94,
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 = 75,
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 = 15,
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 = 60,
Timur Tabi760af8f2011-09-28 16:19:50 -0500299 .xres = 1920,
300 .yres = 1080,
301 .pixclock = 5787,
302 .left_margin = 328,
303 .right_margin = 120,
304 .upper_margin = 34,
305 .lower_margin = 1,
306 .hsync_len = 208,
307 .vsync_len = 3,
York Sun9b53a9e2008-04-28 02:15:34 -0700308 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
309 .vmode = FB_VMODE_NONINTERLACED
310 },
311};
312
Timur Tabi760af8f2011-09-28 16:19:50 -0500313static char *fb_mode;
York Sun9b53a9e2008-04-28 02:15:34 -0700314static unsigned long default_bpp = 32;
Timur Tabi7653aaa2011-07-09 15:38:14 -0500315static enum fsl_diu_monitor_port monitor_port;
316static char *monitor_string;
York Sun9b53a9e2008-04-28 02:15:34 -0700317
318#if defined(CONFIG_NOT_COHERENT_CACHE)
319static u8 *coherence_data;
320static size_t coherence_data_size;
321static unsigned int d_cache_line_size;
322#endif
323
324static DEFINE_SPINLOCK(diu_lock);
325
Timur Tabi2572df92011-09-28 16:19:51 -0500326enum mfb_index {
327 PLANE0 = 0, /* Plane 0, only one AOI that fills the screen */
328 PLANE1_AOI0, /* Plane 1, first AOI */
329 PLANE1_AOI1, /* Plane 1, second AOI */
330 PLANE2_AOI0, /* Plane 2, first AOI */
331 PLANE2_AOI1, /* Plane 2, second AOI */
332};
333
York Sun9b53a9e2008-04-28 02:15:34 -0700334struct mfb_info {
Timur Tabi2572df92011-09-28 16:19:51 -0500335 enum mfb_index index;
York Sun9b53a9e2008-04-28 02:15:34 -0700336 char *id;
337 int registered;
York Sun9b53a9e2008-04-28 02:15:34 -0700338 unsigned long pseudo_palette[16];
339 struct diu_ad *ad;
340 int cursor_reset;
341 unsigned char g_alpha;
342 unsigned int count;
343 int x_aoi_d; /* aoi display x offset to physical screen */
344 int y_aoi_d; /* aoi display y offset to physical screen */
345 struct fsl_diu_data *parent;
Anatolij Gustschin8b856f02010-07-23 04:00:39 +0000346 u8 *edid_data;
York Sun9b53a9e2008-04-28 02:15:34 -0700347};
348
Timur Tabiddd3d902011-12-02 17:03:27 -0600349/**
350 * struct fsl_diu_data - per-DIU data structure
351 * @dma_addr: DMA address of this structure
352 * @fsl_diu_info: fb_info objects, one per AOI
353 * @dev_attr: sysfs structure
354 * @irq: IRQ
Timur Tabiddd3d902011-12-02 17:03:27 -0600355 * @monitor_port: the monitor port this DIU is connected to
356 * @diu_reg: pointer to the DIU hardware registers
357 * @reg_lock: spinlock for register access
358 * @dummy_aoi: video buffer for the 4x4 32-bit dummy AOI
359 * dummy_ad: DIU Area Descriptor for the dummy AOI
360 * @ad[]: Area Descriptors for each real AOI
361 * @gamma: gamma color table
362 * @cursor: hardware cursor data
363 *
364 * This data structure must be allocated with 32-byte alignment, so that the
365 * internal fields can be aligned properly.
366 */
367struct fsl_diu_data {
368 dma_addr_t dma_addr;
369 struct fb_info fsl_diu_info[NUM_AOIS];
370 struct mfb_info mfb[NUM_AOIS];
371 struct device_attribute dev_attr;
372 unsigned int irq;
Timur Tabiddd3d902011-12-02 17:03:27 -0600373 enum fsl_diu_monitor_port monitor_port;
374 struct diu __iomem *diu_reg;
375 spinlock_t reg_lock;
376 u8 dummy_aoi[4 * 4 * 4];
377 struct diu_ad dummy_ad __aligned(8);
378 struct diu_ad ad[NUM_AOIS] __aligned(8);
379 u8 gamma[256 * 3] __aligned(32);
380 u8 cursor[MAX_CURS * MAX_CURS * 2] __aligned(32);
381} __aligned(32);
382
383/* Determine the DMA address of a member of the fsl_diu_data structure */
384#define DMA_ADDR(p, f) ((p)->dma_addr + offsetof(struct fsl_diu_data, f))
York Sun9b53a9e2008-04-28 02:15:34 -0700385
386static struct mfb_info mfb_template[] = {
Timur Tabi2572df92011-09-28 16:19:51 -0500387 {
388 .index = PLANE0,
Timur Tabi4a85dc8b2011-09-15 16:44:46 -0500389 .id = "Panel0",
390 .registered = 0,
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_AOI0,
Timur Tabi4a85dc8b2011-09-15 16:44:46 -0500397 .id = "Panel1 AOI0",
398 .registered = 0,
399 .g_alpha = 0xff,
400 .count = 0,
401 .x_aoi_d = 0,
402 .y_aoi_d = 0,
York Sun9b53a9e2008-04-28 02:15:34 -0700403 },
Timur Tabi2572df92011-09-28 16:19:51 -0500404 {
405 .index = PLANE1_AOI1,
Timur Tabi4a85dc8b2011-09-15 16:44:46 -0500406 .id = "Panel1 AOI1",
407 .registered = 0,
408 .g_alpha = 0xff,
409 .count = 0,
410 .x_aoi_d = 0,
411 .y_aoi_d = 480,
York Sun9b53a9e2008-04-28 02:15:34 -0700412 },
Timur Tabi2572df92011-09-28 16:19:51 -0500413 {
414 .index = PLANE2_AOI0,
Timur Tabi4a85dc8b2011-09-15 16:44:46 -0500415 .id = "Panel2 AOI0",
416 .registered = 0,
417 .g_alpha = 0xff,
418 .count = 0,
419 .x_aoi_d = 640,
420 .y_aoi_d = 0,
York Sun9b53a9e2008-04-28 02:15:34 -0700421 },
Timur Tabi2572df92011-09-28 16:19:51 -0500422 {
423 .index = PLANE2_AOI1,
Timur Tabi4a85dc8b2011-09-15 16:44:46 -0500424 .id = "Panel2 AOI1",
425 .registered = 0,
426 .g_alpha = 0xff,
427 .count = 0,
428 .x_aoi_d = 640,
429 .y_aoi_d = 480,
York Sun9b53a9e2008-04-28 02:15:34 -0700430 },
431};
432
Timur Tabi5cc2a362012-10-16 17:33:46 -0500433#ifdef DEBUG
434static void __attribute__ ((unused)) fsl_diu_dump(struct diu __iomem *hw)
435{
436 mb();
437 pr_debug("DIU: desc=%08x,%08x,%08x, gamma=%08x pallete=%08x "
438 "cursor=%08x curs_pos=%08x diu_mode=%08x bgnd=%08x "
439 "disp_size=%08x hsyn_para=%08x vsyn_para=%08x syn_pol=%08x "
440 "thresholds=%08x int_mask=%08x plut=%08x\n",
441 hw->desc[0], hw->desc[1], hw->desc[2], hw->gamma,
442 hw->pallete, hw->cursor, hw->curs_pos, hw->diu_mode,
443 hw->bgnd, hw->disp_size, hw->hsyn_para, hw->vsyn_para,
444 hw->syn_pol, hw->thresholds, hw->int_mask, hw->plut);
445 rmb();
446}
447#endif
448
Timur Tabi6b51d512008-07-23 21:31:39 -0700449/**
Timur Tabi7653aaa2011-07-09 15:38:14 -0500450 * fsl_diu_name_to_port - convert a port name to a monitor port enum
451 *
452 * Takes the name of a monitor port ("dvi", "lvds", or "dlvds") and returns
453 * the enum fsl_diu_monitor_port that corresponds to that string.
454 *
455 * For compatibility with older versions, a number ("0", "1", or "2") is also
456 * supported.
457 *
458 * If the string is unknown, DVI is assumed.
459 *
460 * If the particular port is not supported by the platform, another port
461 * (platform-specific) is chosen instead.
462 */
463static enum fsl_diu_monitor_port fsl_diu_name_to_port(const char *s)
464{
465 enum fsl_diu_monitor_port port = FSL_DIU_PORT_DVI;
466 unsigned long val;
467
468 if (s) {
469 if (!strict_strtoul(s, 10, &val) && (val <= 2))
470 port = (enum fsl_diu_monitor_port) val;
471 else if (strncmp(s, "lvds", 4) == 0)
472 port = FSL_DIU_PORT_LVDS;
473 else if (strncmp(s, "dlvds", 5) == 0)
474 port = FSL_DIU_PORT_DLVDS;
475 }
476
477 return diu_ops.valid_monitor_port(port);
478}
479
Anatolij Gustschin0d9dab32010-07-23 04:00:35 +0000480/*
481 * Workaround for failed writing desc register of planes.
482 * Needed with MPC5121 DIU rev 2.0 silicon.
483 */
484void wr_reg_wa(u32 *reg, u32 val)
485{
486 do {
487 out_be32(reg, val);
488 } while (in_be32(reg) != val);
489}
490
Timur Tabi7e47c212011-09-28 16:19:52 -0500491static void fsl_diu_enable_panel(struct fb_info *info)
York Sun9b53a9e2008-04-28 02:15:34 -0700492{
493 struct mfb_info *pmfbi, *cmfbi, *mfbi = info->par;
York Sun9b53a9e2008-04-28 02:15:34 -0700494 struct diu_ad *ad = mfbi->ad;
Timur Tabib7795052011-12-08 17:27:31 -0600495 struct fsl_diu_data *data = mfbi->parent;
496 struct diu __iomem *hw = data->diu_reg;
York Sun9b53a9e2008-04-28 02:15:34 -0700497
Timur Tabi7e47c212011-09-28 16:19:52 -0500498 switch (mfbi->index) {
499 case PLANE0:
500 if (hw->desc[0] != ad->paddr)
501 wr_reg_wa(&hw->desc[0], ad->paddr);
502 break;
503 case PLANE1_AOI0:
Timur Tabib7795052011-12-08 17:27:31 -0600504 cmfbi = &data->mfb[2];
Timur Tabi7e47c212011-09-28 16:19:52 -0500505 if (hw->desc[1] != ad->paddr) { /* AOI0 closed */
506 if (cmfbi->count > 0) /* AOI1 open */
507 ad->next_ad =
508 cpu_to_le32(cmfbi->ad->paddr);
509 else
510 ad->next_ad = 0;
511 wr_reg_wa(&hw->desc[1], ad->paddr);
York Sun9b53a9e2008-04-28 02:15:34 -0700512 }
Timur Tabi7e47c212011-09-28 16:19:52 -0500513 break;
514 case PLANE2_AOI0:
Timur Tabib7795052011-12-08 17:27:31 -0600515 cmfbi = &data->mfb[4];
Timur Tabi7e47c212011-09-28 16:19:52 -0500516 if (hw->desc[2] != ad->paddr) { /* AOI0 closed */
517 if (cmfbi->count > 0) /* AOI1 open */
518 ad->next_ad =
519 cpu_to_le32(cmfbi->ad->paddr);
520 else
521 ad->next_ad = 0;
522 wr_reg_wa(&hw->desc[2], ad->paddr);
523 }
524 break;
525 case PLANE1_AOI1:
Timur Tabib7795052011-12-08 17:27:31 -0600526 pmfbi = &data->mfb[1];
Timur Tabi7e47c212011-09-28 16:19:52 -0500527 ad->next_ad = 0;
Timur Tabib7795052011-12-08 17:27:31 -0600528 if (hw->desc[1] == data->dummy_ad.paddr)
Timur Tabi7e47c212011-09-28 16:19:52 -0500529 wr_reg_wa(&hw->desc[1], ad->paddr);
530 else /* AOI0 open */
531 pmfbi->ad->next_ad = cpu_to_le32(ad->paddr);
532 break;
533 case PLANE2_AOI1:
Timur Tabib7795052011-12-08 17:27:31 -0600534 pmfbi = &data->mfb[3];
Timur Tabi7e47c212011-09-28 16:19:52 -0500535 ad->next_ad = 0;
Timur Tabib7795052011-12-08 17:27:31 -0600536 if (hw->desc[2] == data->dummy_ad.paddr)
Timur Tabi7e47c212011-09-28 16:19:52 -0500537 wr_reg_wa(&hw->desc[2], ad->paddr);
538 else /* AOI0 was open */
539 pmfbi->ad->next_ad = cpu_to_le32(ad->paddr);
540 break;
541 }
York Sun9b53a9e2008-04-28 02:15:34 -0700542}
543
Timur Tabi2572df92011-09-28 16:19:51 -0500544static void fsl_diu_disable_panel(struct fb_info *info)
York Sun9b53a9e2008-04-28 02:15:34 -0700545{
546 struct mfb_info *pmfbi, *cmfbi, *mfbi = info->par;
York Sun9b53a9e2008-04-28 02:15:34 -0700547 struct diu_ad *ad = mfbi->ad;
Timur Tabib7795052011-12-08 17:27:31 -0600548 struct fsl_diu_data *data = mfbi->parent;
549 struct diu __iomem *hw = data->diu_reg;
York Sun9b53a9e2008-04-28 02:15:34 -0700550
551 switch (mfbi->index) {
Timur Tabi2572df92011-09-28 16:19:51 -0500552 case PLANE0:
Timur Tabib7795052011-12-08 17:27:31 -0600553 if (hw->desc[0] != data->dummy_ad.paddr)
554 wr_reg_wa(&hw->desc[0], data->dummy_ad.paddr);
York Sun9b53a9e2008-04-28 02:15:34 -0700555 break;
Timur Tabi2572df92011-09-28 16:19:51 -0500556 case PLANE1_AOI0:
Timur Tabib7795052011-12-08 17:27:31 -0600557 cmfbi = &data->mfb[2];
York Sun9b53a9e2008-04-28 02:15:34 -0700558 if (cmfbi->count > 0) /* AOI1 is open */
Anatolij Gustschin0d9dab32010-07-23 04:00:35 +0000559 wr_reg_wa(&hw->desc[1], cmfbi->ad->paddr);
York Sun9b53a9e2008-04-28 02:15:34 -0700560 /* move AOI1 to the first */
561 else /* AOI1 was closed */
Timur Tabib7795052011-12-08 17:27:31 -0600562 wr_reg_wa(&hw->desc[1], data->dummy_ad.paddr);
York Sun9b53a9e2008-04-28 02:15:34 -0700563 /* close AOI 0 */
564 break;
Timur Tabi2572df92011-09-28 16:19:51 -0500565 case PLANE2_AOI0:
Timur Tabib7795052011-12-08 17:27:31 -0600566 cmfbi = &data->mfb[4];
York Sun9b53a9e2008-04-28 02:15:34 -0700567 if (cmfbi->count > 0) /* AOI1 is open */
Anatolij Gustschin0d9dab32010-07-23 04:00:35 +0000568 wr_reg_wa(&hw->desc[2], cmfbi->ad->paddr);
York Sun9b53a9e2008-04-28 02:15:34 -0700569 /* move AOI1 to the first */
570 else /* AOI1 was closed */
Timur Tabib7795052011-12-08 17:27:31 -0600571 wr_reg_wa(&hw->desc[2], data->dummy_ad.paddr);
York Sun9b53a9e2008-04-28 02:15:34 -0700572 /* close AOI 0 */
573 break;
Timur Tabi2572df92011-09-28 16:19:51 -0500574 case PLANE1_AOI1:
Timur Tabib7795052011-12-08 17:27:31 -0600575 pmfbi = &data->mfb[1];
York Sun9b53a9e2008-04-28 02:15:34 -0700576 if (hw->desc[1] != ad->paddr) {
577 /* AOI1 is not the first in the chain */
578 if (pmfbi->count > 0)
579 /* AOI0 is open, must be the first */
580 pmfbi->ad->next_ad = 0;
581 } else /* AOI1 is the first in the chain */
Timur Tabib7795052011-12-08 17:27:31 -0600582 wr_reg_wa(&hw->desc[1], data->dummy_ad.paddr);
York Sun9b53a9e2008-04-28 02:15:34 -0700583 /* close AOI 1 */
584 break;
Timur Tabi2572df92011-09-28 16:19:51 -0500585 case PLANE2_AOI1:
Timur Tabib7795052011-12-08 17:27:31 -0600586 pmfbi = &data->mfb[3];
York Sun9b53a9e2008-04-28 02:15:34 -0700587 if (hw->desc[2] != ad->paddr) {
588 /* AOI1 is not the first in the chain */
589 if (pmfbi->count > 0)
590 /* AOI0 is open, must be the first */
591 pmfbi->ad->next_ad = 0;
592 } else /* AOI1 is the first in the chain */
Timur Tabib7795052011-12-08 17:27:31 -0600593 wr_reg_wa(&hw->desc[2], data->dummy_ad.paddr);
York Sun9b53a9e2008-04-28 02:15:34 -0700594 /* close AOI 1 */
595 break;
York Sun9b53a9e2008-04-28 02:15:34 -0700596 }
York Sun9b53a9e2008-04-28 02:15:34 -0700597}
598
599static void enable_lcdc(struct fb_info *info)
600{
York Sun9b53a9e2008-04-28 02:15:34 -0700601 struct mfb_info *mfbi = info->par;
Timur Tabib7795052011-12-08 17:27:31 -0600602 struct fsl_diu_data *data = mfbi->parent;
603 struct diu __iomem *hw = data->diu_reg;
York Sun9b53a9e2008-04-28 02:15:34 -0700604
Timur Tabid397e9162011-12-19 16:26:16 -0600605 out_be32(&hw->diu_mode, MFB_MODE1);
York Sun9b53a9e2008-04-28 02:15:34 -0700606}
607
608static void disable_lcdc(struct fb_info *info)
609{
York Sun9b53a9e2008-04-28 02:15:34 -0700610 struct mfb_info *mfbi = info->par;
Timur Tabib7795052011-12-08 17:27:31 -0600611 struct fsl_diu_data *data = mfbi->parent;
612 struct diu __iomem *hw = data->diu_reg;
York Sun9b53a9e2008-04-28 02:15:34 -0700613
Timur Tabid397e9162011-12-19 16:26:16 -0600614 out_be32(&hw->diu_mode, 0);
York Sun9b53a9e2008-04-28 02:15:34 -0700615}
616
617static void adjust_aoi_size_position(struct fb_var_screeninfo *var,
618 struct fb_info *info)
619{
620 struct mfb_info *lower_aoi_mfbi, *upper_aoi_mfbi, *mfbi = info->par;
Timur Tabib7795052011-12-08 17:27:31 -0600621 struct fsl_diu_data *data = mfbi->parent;
Timur Tabi2572df92011-09-28 16:19:51 -0500622 int available_height, upper_aoi_bottom;
623 enum mfb_index index = mfbi->index;
York Sun9b53a9e2008-04-28 02:15:34 -0700624 int lower_aoi_is_open, upper_aoi_is_open;
625 __u32 base_plane_width, base_plane_height, upper_aoi_height;
626
Timur Tabib7795052011-12-08 17:27:31 -0600627 base_plane_width = data->fsl_diu_info[0].var.xres;
628 base_plane_height = data->fsl_diu_info[0].var.yres;
York Sun9b53a9e2008-04-28 02:15:34 -0700629
York Sunfdfaa48332008-08-15 00:40:29 -0700630 if (mfbi->x_aoi_d < 0)
631 mfbi->x_aoi_d = 0;
632 if (mfbi->y_aoi_d < 0)
633 mfbi->y_aoi_d = 0;
York Sun9b53a9e2008-04-28 02:15:34 -0700634 switch (index) {
Timur Tabi2572df92011-09-28 16:19:51 -0500635 case PLANE0:
York Sun9b53a9e2008-04-28 02:15:34 -0700636 if (mfbi->x_aoi_d != 0)
637 mfbi->x_aoi_d = 0;
638 if (mfbi->y_aoi_d != 0)
639 mfbi->y_aoi_d = 0;
640 break;
Timur Tabi2572df92011-09-28 16:19:51 -0500641 case PLANE1_AOI0:
642 case PLANE2_AOI0:
Timur Tabib7795052011-12-08 17:27:31 -0600643 lower_aoi_mfbi = data->fsl_diu_info[index+1].par;
York Sun9b53a9e2008-04-28 02:15:34 -0700644 lower_aoi_is_open = lower_aoi_mfbi->count > 0 ? 1 : 0;
645 if (var->xres > base_plane_width)
646 var->xres = base_plane_width;
647 if ((mfbi->x_aoi_d + var->xres) > base_plane_width)
648 mfbi->x_aoi_d = base_plane_width - var->xres;
649
650 if (lower_aoi_is_open)
651 available_height = lower_aoi_mfbi->y_aoi_d;
652 else
653 available_height = base_plane_height;
654 if (var->yres > available_height)
655 var->yres = available_height;
656 if ((mfbi->y_aoi_d + var->yres) > available_height)
657 mfbi->y_aoi_d = available_height - var->yres;
658 break;
Timur Tabi2572df92011-09-28 16:19:51 -0500659 case PLANE1_AOI1:
660 case PLANE2_AOI1:
Timur Tabib7795052011-12-08 17:27:31 -0600661 upper_aoi_mfbi = data->fsl_diu_info[index-1].par;
662 upper_aoi_height = data->fsl_diu_info[index-1].var.yres;
York Sun9b53a9e2008-04-28 02:15:34 -0700663 upper_aoi_bottom = upper_aoi_mfbi->y_aoi_d + upper_aoi_height;
664 upper_aoi_is_open = upper_aoi_mfbi->count > 0 ? 1 : 0;
665 if (var->xres > base_plane_width)
666 var->xres = base_plane_width;
667 if ((mfbi->x_aoi_d + var->xres) > base_plane_width)
668 mfbi->x_aoi_d = base_plane_width - var->xres;
669 if (mfbi->y_aoi_d < 0)
670 mfbi->y_aoi_d = 0;
671 if (upper_aoi_is_open) {
672 if (mfbi->y_aoi_d < upper_aoi_bottom)
673 mfbi->y_aoi_d = upper_aoi_bottom;
674 available_height = base_plane_height
675 - upper_aoi_bottom;
676 } else
677 available_height = base_plane_height;
678 if (var->yres > available_height)
679 var->yres = available_height;
680 if ((mfbi->y_aoi_d + var->yres) > base_plane_height)
681 mfbi->y_aoi_d = base_plane_height - var->yres;
682 break;
683 }
684}
685/*
686 * Checks to see if the hardware supports the state requested by var passed
687 * in. This function does not alter the hardware state! If the var passed in
688 * is slightly off by what the hardware can support then we alter the var
689 * PASSED in to what we can do. If the hardware doesn't support mode change
690 * a -EINVAL will be returned by the upper layers.
691 */
692static int fsl_diu_check_var(struct fb_var_screeninfo *var,
693 struct fb_info *info)
694{
York Sun9b53a9e2008-04-28 02:15:34 -0700695 if (var->xres_virtual < var->xres)
696 var->xres_virtual = var->xres;
697 if (var->yres_virtual < var->yres)
698 var->yres_virtual = var->yres;
699
700 if (var->xoffset < 0)
701 var->xoffset = 0;
702
703 if (var->yoffset < 0)
704 var->yoffset = 0;
705
706 if (var->xoffset + info->var.xres > info->var.xres_virtual)
707 var->xoffset = info->var.xres_virtual - info->var.xres;
708
709 if (var->yoffset + info->var.yres > info->var.yres_virtual)
710 var->yoffset = info->var.yres_virtual - info->var.yres;
711
712 if ((var->bits_per_pixel != 32) && (var->bits_per_pixel != 24) &&
713 (var->bits_per_pixel != 16))
714 var->bits_per_pixel = default_bpp;
715
716 switch (var->bits_per_pixel) {
717 case 16:
718 var->red.length = 5;
719 var->red.offset = 11;
720 var->red.msb_right = 0;
721
722 var->green.length = 6;
723 var->green.offset = 5;
724 var->green.msb_right = 0;
725
726 var->blue.length = 5;
727 var->blue.offset = 0;
728 var->blue.msb_right = 0;
729
730 var->transp.length = 0;
731 var->transp.offset = 0;
732 var->transp.msb_right = 0;
733 break;
734 case 24:
735 var->red.length = 8;
736 var->red.offset = 0;
737 var->red.msb_right = 0;
738
739 var->green.length = 8;
740 var->green.offset = 8;
741 var->green.msb_right = 0;
742
743 var->blue.length = 8;
744 var->blue.offset = 16;
745 var->blue.msb_right = 0;
746
747 var->transp.length = 0;
748 var->transp.offset = 0;
749 var->transp.msb_right = 0;
750 break;
751 case 32:
752 var->red.length = 8;
753 var->red.offset = 16;
754 var->red.msb_right = 0;
755
756 var->green.length = 8;
757 var->green.offset = 8;
758 var->green.msb_right = 0;
759
760 var->blue.length = 8;
761 var->blue.offset = 0;
762 var->blue.msb_right = 0;
763
764 var->transp.length = 8;
765 var->transp.offset = 24;
766 var->transp.msb_right = 0;
767
768 break;
769 }
York Sun9b53a9e2008-04-28 02:15:34 -0700770
771 var->height = -1;
772 var->width = -1;
773 var->grayscale = 0;
774
775 /* Copy nonstd field to/from sync for fbset usage */
776 var->sync |= var->nonstd;
777 var->nonstd |= var->sync;
778
779 adjust_aoi_size_position(var, info);
780 return 0;
781}
782
783static void set_fix(struct fb_info *info)
784{
785 struct fb_fix_screeninfo *fix = &info->fix;
786 struct fb_var_screeninfo *var = &info->var;
787 struct mfb_info *mfbi = info->par;
788
Timur Tabiec02dd22011-09-15 16:44:54 -0500789 strncpy(fix->id, mfbi->id, sizeof(fix->id));
York Sun9b53a9e2008-04-28 02:15:34 -0700790 fix->line_length = var->xres_virtual * var->bits_per_pixel / 8;
791 fix->type = FB_TYPE_PACKED_PIXELS;
792 fix->accel = FB_ACCEL_NONE;
793 fix->visual = FB_VISUAL_TRUECOLOR;
794 fix->xpanstep = 1;
795 fix->ypanstep = 1;
796}
797
798static void update_lcdc(struct fb_info *info)
799{
800 struct fb_var_screeninfo *var = &info->var;
801 struct mfb_info *mfbi = info->par;
Timur Tabib7795052011-12-08 17:27:31 -0600802 struct fsl_diu_data *data = mfbi->parent;
Timur Tabi3c755b72011-10-04 19:36:51 -0500803 struct diu __iomem *hw;
York Sun9b53a9e2008-04-28 02:15:34 -0700804 int i, j;
Timur Tabiddd3d902011-12-02 17:03:27 -0600805 u8 *gamma_table_base;
York Sun9b53a9e2008-04-28 02:15:34 -0700806
807 u32 temp;
808
Timur Tabib7795052011-12-08 17:27:31 -0600809 hw = data->diu_reg;
York Sun9b53a9e2008-04-28 02:15:34 -0700810
Timur Tabi28671732012-10-16 17:33:43 -0500811 if (diu_ops.set_monitor_port)
812 diu_ops.set_monitor_port(data->monitor_port);
Timur Tabib7795052011-12-08 17:27:31 -0600813 gamma_table_base = data->gamma;
Timur Tabiddd3d902011-12-02 17:03:27 -0600814
York Sun9b53a9e2008-04-28 02:15:34 -0700815 /* Prep for DIU init - gamma table, cursor table */
816
817 for (i = 0; i <= 2; i++)
Timur Tabi4a85dc8b2011-09-15 16:44:46 -0500818 for (j = 0; j <= 255; j++)
819 *gamma_table_base++ = j;
York Sun9b53a9e2008-04-28 02:15:34 -0700820
Timur Tabie09a8c32011-12-19 16:26:17 -0600821 if (diu_ops.set_gamma_table)
822 diu_ops.set_gamma_table(data->monitor_port, data->gamma);
York Sun9b53a9e2008-04-28 02:15:34 -0700823
York Sun9b53a9e2008-04-28 02:15:34 -0700824 disable_lcdc(info);
825
826 /* Program DIU registers */
827
Timur Tabib7795052011-12-08 17:27:31 -0600828 out_be32(&hw->gamma, DMA_ADDR(data, gamma));
829 out_be32(&hw->cursor, DMA_ADDR(data, cursor));
York Sun9b53a9e2008-04-28 02:15:34 -0700830
831 out_be32(&hw->bgnd, 0x007F7F7F); /* BGND */
832 out_be32(&hw->bgnd_wb, 0); /* BGND_WB */
833 out_be32(&hw->disp_size, (var->yres << 16 | var->xres));
834 /* DISP SIZE */
York Sun9b53a9e2008-04-28 02:15:34 -0700835 out_be32(&hw->wb_size, 0); /* WB SIZE */
836 out_be32(&hw->wb_mem_addr, 0); /* WB MEM ADDR */
837
838 /* Horizontal and vertical configuration register */
839 temp = var->left_margin << 22 | /* BP_H */
840 var->hsync_len << 11 | /* PW_H */
841 var->right_margin; /* FP_H */
842
843 out_be32(&hw->hsyn_para, temp);
844
845 temp = var->upper_margin << 22 | /* BP_V */
846 var->vsync_len << 11 | /* PW_V */
847 var->lower_margin; /* FP_V */
848
849 out_be32(&hw->vsyn_para, temp);
850
York Sun9b53a9e2008-04-28 02:15:34 -0700851 diu_ops.set_pixel_clock(var->pixclock);
852
853 out_be32(&hw->syn_pol, 0); /* SYNC SIGNALS POLARITY */
York Sun9b53a9e2008-04-28 02:15:34 -0700854 out_be32(&hw->int_status, 0); /* INTERRUPT STATUS */
855 out_be32(&hw->plut, 0x01F5F666);
856
857 /* Enable the DIU */
858 enable_lcdc(info);
859}
860
861static int map_video_memory(struct fb_info *info)
862{
Krzysztof Helt537a1bf2009-06-30 11:41:29 -0700863 u32 smem_len = info->fix.line_length * info->var.yres_virtual;
Timur Tabi05342c02011-12-19 16:26:18 -0600864 void *p;
York Sun9b53a9e2008-04-28 02:15:34 -0700865
Timur Tabi05342c02011-12-19 16:26:18 -0600866 p = alloc_pages_exact(smem_len, GFP_DMA | __GFP_ZERO);
867 if (!p) {
Timur Tabi154152a2011-09-15 16:44:47 -0500868 dev_err(info->dev, "unable to allocate fb memory\n");
York Sun9b53a9e2008-04-28 02:15:34 -0700869 return -ENOMEM;
870 }
Krzysztof Helt537a1bf2009-06-30 11:41:29 -0700871 mutex_lock(&info->mm_lock);
Timur Tabi05342c02011-12-19 16:26:18 -0600872 info->screen_base = p;
873 info->fix.smem_start = virt_to_phys(info->screen_base);
Krzysztof Helt537a1bf2009-06-30 11:41:29 -0700874 info->fix.smem_len = smem_len;
875 mutex_unlock(&info->mm_lock);
York Sun9b53a9e2008-04-28 02:15:34 -0700876 info->screen_size = info->fix.smem_len;
877
York Sun9b53a9e2008-04-28 02:15:34 -0700878 return 0;
879}
880
881static void unmap_video_memory(struct fb_info *info)
882{
Timur Tabi05342c02011-12-19 16:26:18 -0600883 void *p = info->screen_base;
884 size_t l = info->fix.smem_len;
885
Krzysztof Helt537a1bf2009-06-30 11:41:29 -0700886 mutex_lock(&info->mm_lock);
Anton Vorontsov05946bc2008-07-04 09:59:38 -0700887 info->screen_base = NULL;
York Sun9b53a9e2008-04-28 02:15:34 -0700888 info->fix.smem_start = 0;
889 info->fix.smem_len = 0;
Krzysztof Helt537a1bf2009-06-30 11:41:29 -0700890 mutex_unlock(&info->mm_lock);
Timur Tabi05342c02011-12-19 16:26:18 -0600891
892 if (p)
893 free_pages_exact(p, l);
York Sun9b53a9e2008-04-28 02:15:34 -0700894}
895
896/*
York Sunae5591e2008-08-15 00:40:28 -0700897 * Using the fb_var_screeninfo in fb_info we set the aoi of this
898 * particular framebuffer. It is a light version of fsl_diu_set_par.
899 */
900static int fsl_diu_set_aoi(struct fb_info *info)
901{
902 struct fb_var_screeninfo *var = &info->var;
903 struct mfb_info *mfbi = info->par;
904 struct diu_ad *ad = mfbi->ad;
905
906 /* AOI should not be greater than display size */
907 ad->offset_xyi = cpu_to_le32((var->yoffset << 16) | var->xoffset);
908 ad->offset_xyd = cpu_to_le32((mfbi->y_aoi_d << 16) | mfbi->x_aoi_d);
909 return 0;
910}
911
Timur Tabie09a8c32011-12-19 16:26:17 -0600912/**
913 * fsl_diu_get_pixel_format: return the pixel format for a given color depth
914 *
915 * The pixel format is a 32-bit value that determine which bits in each
916 * pixel are to be used for each color. This is the default function used
917 * if the platform does not define its own version.
918 */
919static u32 fsl_diu_get_pixel_format(unsigned int bits_per_pixel)
920{
921#define PF_BYTE_F 0x10000000
922#define PF_ALPHA_C_MASK 0x0E000000
923#define PF_ALPHA_C_SHIFT 25
924#define PF_BLUE_C_MASK 0x01800000
925#define PF_BLUE_C_SHIFT 23
926#define PF_GREEN_C_MASK 0x00600000
927#define PF_GREEN_C_SHIFT 21
928#define PF_RED_C_MASK 0x00180000
929#define PF_RED_C_SHIFT 19
930#define PF_PALETTE 0x00040000
931#define PF_PIXEL_S_MASK 0x00030000
932#define PF_PIXEL_S_SHIFT 16
933#define PF_COMP_3_MASK 0x0000F000
934#define PF_COMP_3_SHIFT 12
935#define PF_COMP_2_MASK 0x00000F00
936#define PF_COMP_2_SHIFT 8
937#define PF_COMP_1_MASK 0x000000F0
938#define PF_COMP_1_SHIFT 4
939#define PF_COMP_0_MASK 0x0000000F
940#define PF_COMP_0_SHIFT 0
941
942#define MAKE_PF(alpha, red, blue, green, size, c0, c1, c2, c3) \
943 cpu_to_le32(PF_BYTE_F | (alpha << PF_ALPHA_C_SHIFT) | \
944 (blue << PF_BLUE_C_SHIFT) | (green << PF_GREEN_C_SHIFT) | \
945 (red << PF_RED_C_SHIFT) | (c3 << PF_COMP_3_SHIFT) | \
946 (c2 << PF_COMP_2_SHIFT) | (c1 << PF_COMP_1_SHIFT) | \
947 (c0 << PF_COMP_0_SHIFT) | (size << PF_PIXEL_S_SHIFT))
948
949 switch (bits_per_pixel) {
950 case 32:
951 /* 0x88883316 */
952 return MAKE_PF(3, 2, 0, 1, 3, 8, 8, 8, 8);
953 case 24:
954 /* 0x88082219 */
955 return MAKE_PF(4, 0, 1, 2, 2, 0, 8, 8, 8);
956 case 16:
957 /* 0x65053118 */
958 return MAKE_PF(4, 2, 1, 0, 1, 5, 6, 5, 0);
959 default:
960 pr_err("fsl-diu: unsupported color depth %u\n", bits_per_pixel);
961 return 0;
962 }
963}
964
York Sunae5591e2008-08-15 00:40:28 -0700965/*
York Sun9b53a9e2008-04-28 02:15:34 -0700966 * Using the fb_var_screeninfo in fb_info we set the resolution of this
967 * particular framebuffer. This function alters the fb_fix_screeninfo stored
968 * in fb_info. It does not alter var in fb_info since we are using that
969 * data. This means we depend on the data in var inside fb_info to be
970 * supported by the hardware. fsl_diu_check_var is always called before
971 * fsl_diu_set_par to ensure this.
972 */
973static int fsl_diu_set_par(struct fb_info *info)
974{
975 unsigned long len;
976 struct fb_var_screeninfo *var = &info->var;
977 struct mfb_info *mfbi = info->par;
Timur Tabib7795052011-12-08 17:27:31 -0600978 struct fsl_diu_data *data = mfbi->parent;
York Sun9b53a9e2008-04-28 02:15:34 -0700979 struct diu_ad *ad = mfbi->ad;
Timur Tabi3c755b72011-10-04 19:36:51 -0500980 struct diu __iomem *hw;
York Sun9b53a9e2008-04-28 02:15:34 -0700981
Timur Tabib7795052011-12-08 17:27:31 -0600982 hw = data->diu_reg;
York Sun9b53a9e2008-04-28 02:15:34 -0700983
984 set_fix(info);
985 mfbi->cursor_reset = 1;
986
987 len = info->var.yres_virtual * info->fix.line_length;
988 /* Alloc & dealloc each time resolution/bpp change */
989 if (len != info->fix.smem_len) {
990 if (info->fix.smem_start)
991 unmap_video_memory(info);
York Sun9b53a9e2008-04-28 02:15:34 -0700992
993 /* Memory allocation for framebuffer */
994 if (map_video_memory(info)) {
Timur Tabi154152a2011-09-15 16:44:47 -0500995 dev_err(info->dev, "unable to allocate fb memory 1\n");
York Sun9b53a9e2008-04-28 02:15:34 -0700996 return -ENOMEM;
997 }
998 }
999
Timur Tabie09a8c32011-12-19 16:26:17 -06001000 if (diu_ops.get_pixel_format)
1001 ad->pix_fmt = diu_ops.get_pixel_format(data->monitor_port,
1002 var->bits_per_pixel);
1003 else
1004 ad->pix_fmt = fsl_diu_get_pixel_format(var->bits_per_pixel);
1005
York Sun9b53a9e2008-04-28 02:15:34 -07001006 ad->addr = cpu_to_le32(info->fix.smem_start);
York Sunae5591e2008-08-15 00:40:28 -07001007 ad->src_size_g_alpha = cpu_to_le32((var->yres_virtual << 12) |
1008 var->xres_virtual) | mfbi->g_alpha;
1009 /* AOI should not be greater than display size */
York Sun9b53a9e2008-04-28 02:15:34 -07001010 ad->aoi_size = cpu_to_le32((var->yres << 16) | var->xres);
York Sunae5591e2008-08-15 00:40:28 -07001011 ad->offset_xyi = cpu_to_le32((var->yoffset << 16) | var->xoffset);
York Sun9b53a9e2008-04-28 02:15:34 -07001012 ad->offset_xyd = cpu_to_le32((mfbi->y_aoi_d << 16) | mfbi->x_aoi_d);
1013
1014 /* Disable chroma keying function */
1015 ad->ckmax_r = 0;
1016 ad->ckmax_g = 0;
1017 ad->ckmax_b = 0;
1018
1019 ad->ckmin_r = 255;
1020 ad->ckmin_g = 255;
1021 ad->ckmin_b = 255;
1022
Timur Tabi2572df92011-09-28 16:19:51 -05001023 if (mfbi->index == PLANE0)
York Sun9b53a9e2008-04-28 02:15:34 -07001024 update_lcdc(info);
1025 return 0;
1026}
1027
1028static inline __u32 CNVT_TOHW(__u32 val, __u32 width)
1029{
Timur Tabi4a85dc8b2011-09-15 16:44:46 -05001030 return ((val << width) + 0x7FFF - val) >> 16;
York Sun9b53a9e2008-04-28 02:15:34 -07001031}
1032
1033/*
1034 * Set a single color register. The values supplied have a 16 bit magnitude
1035 * which needs to be scaled in this function for the hardware. Things to take
1036 * into consideration are how many color registers, if any, are supported with
1037 * the current color visual. With truecolor mode no color palettes are
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001038 * supported. Here a pseudo palette is created which we store the value in
York Sun9b53a9e2008-04-28 02:15:34 -07001039 * pseudo_palette in struct fb_info. For pseudocolor mode we have a limited
1040 * color palette.
1041 */
Timur Tabi4a85dc8b2011-09-15 16:44:46 -05001042static int fsl_diu_setcolreg(unsigned int regno, unsigned int red,
1043 unsigned int green, unsigned int blue,
1044 unsigned int transp, struct fb_info *info)
York Sun9b53a9e2008-04-28 02:15:34 -07001045{
1046 int ret = 1;
1047
1048 /*
1049 * If greyscale is true, then we convert the RGB value
1050 * to greyscale no matter what visual we are using.
1051 */
1052 if (info->var.grayscale)
1053 red = green = blue = (19595 * red + 38470 * green +
1054 7471 * blue) >> 16;
1055 switch (info->fix.visual) {
1056 case FB_VISUAL_TRUECOLOR:
1057 /*
1058 * 16-bit True Colour. We encode the RGB value
1059 * according to the RGB bitfield information.
1060 */
1061 if (regno < 16) {
1062 u32 *pal = info->pseudo_palette;
1063 u32 v;
1064
1065 red = CNVT_TOHW(red, info->var.red.length);
1066 green = CNVT_TOHW(green, info->var.green.length);
1067 blue = CNVT_TOHW(blue, info->var.blue.length);
1068 transp = CNVT_TOHW(transp, info->var.transp.length);
1069
1070 v = (red << info->var.red.offset) |
1071 (green << info->var.green.offset) |
1072 (blue << info->var.blue.offset) |
1073 (transp << info->var.transp.offset);
1074
1075 pal[regno] = v;
1076 ret = 0;
1077 }
1078 break;
York Sun9b53a9e2008-04-28 02:15:34 -07001079 }
1080
1081 return ret;
1082}
1083
1084/*
1085 * Pan (or wrap, depending on the `vmode' field) the display using the
1086 * 'xoffset' and 'yoffset' fields of the 'var' structure. If the values
1087 * don't fit, return -EINVAL.
1088 */
1089static int fsl_diu_pan_display(struct fb_var_screeninfo *var,
1090 struct fb_info *info)
1091{
1092 if ((info->var.xoffset == var->xoffset) &&
1093 (info->var.yoffset == var->yoffset))
1094 return 0; /* No change, do nothing */
1095
1096 if (var->xoffset < 0 || var->yoffset < 0
1097 || var->xoffset + info->var.xres > info->var.xres_virtual
1098 || var->yoffset + info->var.yres > info->var.yres_virtual)
1099 return -EINVAL;
1100
1101 info->var.xoffset = var->xoffset;
1102 info->var.yoffset = var->yoffset;
1103
1104 if (var->vmode & FB_VMODE_YWRAP)
1105 info->var.vmode |= FB_VMODE_YWRAP;
1106 else
1107 info->var.vmode &= ~FB_VMODE_YWRAP;
1108
York Sunae5591e2008-08-15 00:40:28 -07001109 fsl_diu_set_aoi(info);
1110
York Sun9b53a9e2008-04-28 02:15:34 -07001111 return 0;
1112}
1113
York Sun9b53a9e2008-04-28 02:15:34 -07001114static int fsl_diu_ioctl(struct fb_info *info, unsigned int cmd,
1115 unsigned long arg)
1116{
1117 struct mfb_info *mfbi = info->par;
1118 struct diu_ad *ad = mfbi->ad;
1119 struct mfb_chroma_key ck;
1120 unsigned char global_alpha;
1121 struct aoi_display_offset aoi_d;
1122 __u32 pix_fmt;
1123 void __user *buf = (void __user *)arg;
1124
1125 if (!arg)
1126 return -EINVAL;
Timur Tabi5cc2a362012-10-16 17:33:46 -05001127
1128 dev_dbg(info->dev, "ioctl %08x (dir=%s%s type=%u nr=%u size=%u)\n", cmd,
1129 _IOC_DIR(cmd) & _IOC_READ ? "R" : "",
1130 _IOC_DIR(cmd) & _IOC_WRITE ? "W" : "",
1131 _IOC_TYPE(cmd), _IOC_NR(cmd), _IOC_SIZE(cmd));
1132
York Sun9b53a9e2008-04-28 02:15:34 -07001133 switch (cmd) {
Timur Tabi36b0b1d2011-10-04 19:36:44 -05001134 case MFB_SET_PIXFMT_OLD:
1135 dev_warn(info->dev,
1136 "MFB_SET_PIXFMT value of 0x%08x is deprecated.\n",
1137 MFB_SET_PIXFMT_OLD);
York Sun9b53a9e2008-04-28 02:15:34 -07001138 case MFB_SET_PIXFMT:
1139 if (copy_from_user(&pix_fmt, buf, sizeof(pix_fmt)))
1140 return -EFAULT;
1141 ad->pix_fmt = pix_fmt;
York Sun9b53a9e2008-04-28 02:15:34 -07001142 break;
Timur Tabi36b0b1d2011-10-04 19:36:44 -05001143 case MFB_GET_PIXFMT_OLD:
1144 dev_warn(info->dev,
1145 "MFB_GET_PIXFMT value of 0x%08x is deprecated.\n",
1146 MFB_GET_PIXFMT_OLD);
York Sun9b53a9e2008-04-28 02:15:34 -07001147 case MFB_GET_PIXFMT:
1148 pix_fmt = ad->pix_fmt;
1149 if (copy_to_user(buf, &pix_fmt, sizeof(pix_fmt)))
1150 return -EFAULT;
York Sun9b53a9e2008-04-28 02:15:34 -07001151 break;
1152 case MFB_SET_AOID:
1153 if (copy_from_user(&aoi_d, buf, sizeof(aoi_d)))
1154 return -EFAULT;
1155 mfbi->x_aoi_d = aoi_d.x_aoi_d;
1156 mfbi->y_aoi_d = aoi_d.y_aoi_d;
York Sun9b53a9e2008-04-28 02:15:34 -07001157 fsl_diu_check_var(&info->var, info);
York Sunae5591e2008-08-15 00:40:28 -07001158 fsl_diu_set_aoi(info);
York Sun9b53a9e2008-04-28 02:15:34 -07001159 break;
1160 case MFB_GET_AOID:
1161 aoi_d.x_aoi_d = mfbi->x_aoi_d;
1162 aoi_d.y_aoi_d = mfbi->y_aoi_d;
1163 if (copy_to_user(buf, &aoi_d, sizeof(aoi_d)))
1164 return -EFAULT;
York Sun9b53a9e2008-04-28 02:15:34 -07001165 break;
1166 case MFB_GET_ALPHA:
1167 global_alpha = mfbi->g_alpha;
1168 if (copy_to_user(buf, &global_alpha, sizeof(global_alpha)))
1169 return -EFAULT;
York Sun9b53a9e2008-04-28 02:15:34 -07001170 break;
1171 case MFB_SET_ALPHA:
1172 /* set panel information */
1173 if (copy_from_user(&global_alpha, buf, sizeof(global_alpha)))
1174 return -EFAULT;
1175 ad->src_size_g_alpha = (ad->src_size_g_alpha & (~0xff)) |
1176 (global_alpha & 0xff);
1177 mfbi->g_alpha = global_alpha;
York Sun9b53a9e2008-04-28 02:15:34 -07001178 break;
1179 case MFB_SET_CHROMA_KEY:
1180 /* set panel winformation */
1181 if (copy_from_user(&ck, buf, sizeof(ck)))
1182 return -EFAULT;
1183
1184 if (ck.enable &&
1185 (ck.red_max < ck.red_min ||
1186 ck.green_max < ck.green_min ||
1187 ck.blue_max < ck.blue_min))
1188 return -EINVAL;
1189
1190 if (!ck.enable) {
1191 ad->ckmax_r = 0;
1192 ad->ckmax_g = 0;
1193 ad->ckmax_b = 0;
1194 ad->ckmin_r = 255;
1195 ad->ckmin_g = 255;
1196 ad->ckmin_b = 255;
1197 } else {
1198 ad->ckmax_r = ck.red_max;
1199 ad->ckmax_g = ck.green_max;
1200 ad->ckmax_b = ck.blue_max;
1201 ad->ckmin_r = ck.red_min;
1202 ad->ckmin_g = ck.green_min;
1203 ad->ckmin_b = ck.blue_min;
1204 }
York Sun9b53a9e2008-04-28 02:15:34 -07001205 break;
Timur Tabie95c17e2012-10-16 17:33:44 -05001206#ifdef CONFIG_PPC_MPC512x
1207 case MFB_SET_GAMMA: {
1208 struct fsl_diu_data *data = mfbi->parent;
1209
1210 if (copy_from_user(data->gamma, buf, sizeof(data->gamma)))
1211 return -EFAULT;
1212 setbits32(&data->diu_reg->gamma, 0); /* Force table reload */
1213 break;
1214 }
1215 case MFB_GET_GAMMA: {
1216 struct fsl_diu_data *data = mfbi->parent;
1217
1218 if (copy_to_user(buf, data->gamma, sizeof(data->gamma)))
1219 return -EFAULT;
1220 break;
1221 }
1222#endif
York Sun9b53a9e2008-04-28 02:15:34 -07001223 default:
Timur Tabi154152a2011-09-15 16:44:47 -05001224 dev_err(info->dev, "unknown ioctl command (0x%08X)\n", cmd);
York Sun9b53a9e2008-04-28 02:15:34 -07001225 return -ENOIOCTLCMD;
1226 }
1227
1228 return 0;
1229}
1230
1231/* turn on fb if count == 1
1232 */
1233static int fsl_diu_open(struct fb_info *info, int user)
1234{
1235 struct mfb_info *mfbi = info->par;
1236 int res = 0;
1237
Anatolij Gustschin4b5006e2010-07-23 04:00:37 +00001238 /* free boot splash memory on first /dev/fb0 open */
Timur Tabi2572df92011-09-28 16:19:51 -05001239 if ((mfbi->index == PLANE0) && diu_ops.release_bootmem)
Anatolij Gustschin4b5006e2010-07-23 04:00:37 +00001240 diu_ops.release_bootmem();
1241
York Sun9b53a9e2008-04-28 02:15:34 -07001242 spin_lock(&diu_lock);
1243 mfbi->count++;
1244 if (mfbi->count == 1) {
York Sun9b53a9e2008-04-28 02:15:34 -07001245 fsl_diu_check_var(&info->var, info);
1246 res = fsl_diu_set_par(info);
1247 if (res < 0)
1248 mfbi->count--;
Timur Tabif74de502012-10-16 17:33:45 -05001249 else {
1250 struct fsl_diu_data *data = mfbi->parent;
1251
1252#ifdef CONFIG_NOT_COHERENT_CACHE
1253 /*
1254 * Enable underrun detection and vertical sync
1255 * interrupts.
1256 */
1257 clrbits32(&data->diu_reg->int_mask,
1258 INT_UNDRUN | INT_VSYNC);
1259#else
1260 /* Enable underrun detection */
1261 clrbits32(&data->diu_reg->int_mask, INT_UNDRUN);
1262#endif
Timur Tabi7e47c212011-09-28 16:19:52 -05001263 fsl_diu_enable_panel(info);
Timur Tabif74de502012-10-16 17:33:45 -05001264 }
York Sun9b53a9e2008-04-28 02:15:34 -07001265 }
1266
1267 spin_unlock(&diu_lock);
1268 return res;
1269}
1270
1271/* turn off fb if count == 0
1272 */
1273static int fsl_diu_release(struct fb_info *info, int user)
1274{
1275 struct mfb_info *mfbi = info->par;
1276 int res = 0;
1277
1278 spin_lock(&diu_lock);
1279 mfbi->count--;
Timur Tabif74de502012-10-16 17:33:45 -05001280 if (mfbi->count == 0) {
1281 struct fsl_diu_data *data = mfbi->parent;
1282
1283 /* Disable interrupts */
1284 out_be32(&data->diu_reg->int_mask, 0xffffffff);
Timur Tabi2572df92011-09-28 16:19:51 -05001285 fsl_diu_disable_panel(info);
Timur Tabif74de502012-10-16 17:33:45 -05001286 }
Timur Tabi2572df92011-09-28 16:19:51 -05001287
York Sun9b53a9e2008-04-28 02:15:34 -07001288 spin_unlock(&diu_lock);
1289 return res;
1290}
1291
1292static struct fb_ops fsl_diu_ops = {
1293 .owner = THIS_MODULE,
1294 .fb_check_var = fsl_diu_check_var,
1295 .fb_set_par = fsl_diu_set_par,
1296 .fb_setcolreg = fsl_diu_setcolreg,
York Sun9b53a9e2008-04-28 02:15:34 -07001297 .fb_pan_display = fsl_diu_pan_display,
1298 .fb_fillrect = cfb_fillrect,
1299 .fb_copyarea = cfb_copyarea,
1300 .fb_imageblit = cfb_imageblit,
1301 .fb_ioctl = fsl_diu_ioctl,
1302 .fb_open = fsl_diu_open,
1303 .fb_release = fsl_diu_release,
1304};
1305
Anton Vorontsov05946bc2008-07-04 09:59:38 -07001306static int __devinit install_fb(struct fb_info *info)
York Sun9b53a9e2008-04-28 02:15:34 -07001307{
1308 int rc;
1309 struct mfb_info *mfbi = info->par;
1310 const char *aoi_mode, *init_aoi_mode = "320x240";
Anatolij Gustschin8b856f02010-07-23 04:00:39 +00001311 struct fb_videomode *db = fsl_diu_mode_db;
1312 unsigned int dbsize = ARRAY_SIZE(fsl_diu_mode_db);
1313 int has_default_mode = 1;
York Sun9b53a9e2008-04-28 02:15:34 -07001314
Timur Tabif934fbd2011-12-19 16:26:14 -06001315 info->var.activate = FB_ACTIVATE_NOW;
1316 info->fbops = &fsl_diu_ops;
Timur Tabi934dbee2011-12-19 16:26:15 -06001317 info->flags = FBINFO_DEFAULT | FBINFO_VIRTFB | FBINFO_PARTIAL_PAN_OK |
1318 FBINFO_READS_FAST;
Timur Tabif934fbd2011-12-19 16:26:14 -06001319 info->pseudo_palette = mfbi->pseudo_palette;
1320
1321 rc = fb_alloc_cmap(&info->cmap, 16, 0);
1322 if (rc)
1323 return rc;
York Sun9b53a9e2008-04-28 02:15:34 -07001324
Timur Tabi2572df92011-09-28 16:19:51 -05001325 if (mfbi->index == PLANE0) {
Anatolij Gustschin8b856f02010-07-23 04:00:39 +00001326 if (mfbi->edid_data) {
1327 /* Now build modedb from EDID */
1328 fb_edid_to_monspecs(mfbi->edid_data, &info->monspecs);
1329 fb_videomode_to_modelist(info->monspecs.modedb,
1330 info->monspecs.modedb_len,
1331 &info->modelist);
1332 db = info->monspecs.modedb;
1333 dbsize = info->monspecs.modedb_len;
1334 }
York Sun9b53a9e2008-04-28 02:15:34 -07001335 aoi_mode = fb_mode;
Anatolij Gustschin8b856f02010-07-23 04:00:39 +00001336 } else {
York Sun9b53a9e2008-04-28 02:15:34 -07001337 aoi_mode = init_aoi_mode;
Anatolij Gustschin8b856f02010-07-23 04:00:39 +00001338 }
Timur Tabi63cf8df2011-09-15 16:44:51 -05001339 rc = fb_find_mode(&info->var, info, aoi_mode, db, dbsize, NULL,
1340 default_bpp);
Timur Tabi154152a2011-09-15 16:44:47 -05001341 if (!rc) {
Anatolij Gustschin8b856f02010-07-23 04:00:39 +00001342 /*
1343 * For plane 0 we continue and look into
1344 * driver's internal modedb.
1345 */
Timur Tabi2572df92011-09-28 16:19:51 -05001346 if ((mfbi->index == PLANE0) && mfbi->edid_data)
Anatolij Gustschin8b856f02010-07-23 04:00:39 +00001347 has_default_mode = 0;
1348 else
1349 return -EINVAL;
York Sun9b53a9e2008-04-28 02:15:34 -07001350 }
1351
Anatolij Gustschin8b856f02010-07-23 04:00:39 +00001352 if (!has_default_mode) {
1353 rc = fb_find_mode(&info->var, info, aoi_mode, fsl_diu_mode_db,
Timur Tabi63cf8df2011-09-15 16:44:51 -05001354 ARRAY_SIZE(fsl_diu_mode_db), NULL, default_bpp);
1355 if (rc)
Anatolij Gustschin8b856f02010-07-23 04:00:39 +00001356 has_default_mode = 1;
1357 }
1358
1359 /* Still not found, use preferred mode from database if any */
1360 if (!has_default_mode && info->monspecs.modedb) {
1361 struct fb_monspecs *specs = &info->monspecs;
1362 struct fb_videomode *modedb = &specs->modedb[0];
1363
1364 /*
1365 * Get preferred timing. If not found,
1366 * first mode in database will be used.
1367 */
1368 if (specs->misc & FB_MISC_1ST_DETAIL) {
1369 int i;
1370
1371 for (i = 0; i < specs->modedb_len; i++) {
1372 if (specs->modedb[i].flag & FB_MODE_IS_FIRST) {
1373 modedb = &specs->modedb[i];
1374 break;
1375 }
1376 }
1377 }
1378
1379 info->var.bits_per_pixel = default_bpp;
1380 fb_videomode_to_var(&info->var, modedb);
1381 }
1382
York Sun9b53a9e2008-04-28 02:15:34 -07001383 if (fsl_diu_check_var(&info->var, info)) {
Timur Tabi154152a2011-09-15 16:44:47 -05001384 dev_err(info->dev, "fsl_diu_check_var failed\n");
Timur Tabi589c7972011-09-15 16:44:55 -05001385 unmap_video_memory(info);
York Sun9b53a9e2008-04-28 02:15:34 -07001386 fb_dealloc_cmap(&info->cmap);
1387 return -EINVAL;
1388 }
1389
York Sun9b53a9e2008-04-28 02:15:34 -07001390 if (register_framebuffer(info) < 0) {
Timur Tabi154152a2011-09-15 16:44:47 -05001391 dev_err(info->dev, "register_framebuffer failed\n");
York Sun9b53a9e2008-04-28 02:15:34 -07001392 unmap_video_memory(info);
1393 fb_dealloc_cmap(&info->cmap);
1394 return -EINVAL;
1395 }
1396
1397 mfbi->registered = 1;
Timur Tabi154152a2011-09-15 16:44:47 -05001398 dev_info(info->dev, "%s registered successfully\n", mfbi->id);
York Sun9b53a9e2008-04-28 02:15:34 -07001399
1400 return 0;
1401}
1402
Anton Vorontsov05946bc2008-07-04 09:59:38 -07001403static void uninstall_fb(struct fb_info *info)
York Sun9b53a9e2008-04-28 02:15:34 -07001404{
1405 struct mfb_info *mfbi = info->par;
1406
1407 if (!mfbi->registered)
1408 return;
1409
Timur Tabi2572df92011-09-28 16:19:51 -05001410 if (mfbi->index == PLANE0)
Anatolij Gustschin8b856f02010-07-23 04:00:39 +00001411 kfree(mfbi->edid_data);
1412
York Sun9b53a9e2008-04-28 02:15:34 -07001413 unregister_framebuffer(info);
1414 unmap_video_memory(info);
1415 if (&info->cmap)
1416 fb_dealloc_cmap(&info->cmap);
1417
1418 mfbi->registered = 0;
1419}
1420
1421static irqreturn_t fsl_diu_isr(int irq, void *dev_id)
1422{
Timur Tabi3c755b72011-10-04 19:36:51 -05001423 struct diu __iomem *hw = dev_id;
Timur Tabif74de502012-10-16 17:33:45 -05001424 uint32_t status = in_be32(&hw->int_status);
York Sun9b53a9e2008-04-28 02:15:34 -07001425
1426 if (status) {
1427 /* This is the workaround for underrun */
1428 if (status & INT_UNDRUN) {
1429 out_be32(&hw->diu_mode, 0);
York Sun9b53a9e2008-04-28 02:15:34 -07001430 udelay(1);
1431 out_be32(&hw->diu_mode, 1);
1432 }
1433#if defined(CONFIG_NOT_COHERENT_CACHE)
1434 else if (status & INT_VSYNC) {
1435 unsigned int i;
Timur Tabi4a85dc8b2011-09-15 16:44:46 -05001436
York Sun9b53a9e2008-04-28 02:15:34 -07001437 for (i = 0; i < coherence_data_size;
1438 i += d_cache_line_size)
1439 __asm__ __volatile__ (
1440 "dcbz 0, %[input]"
1441 ::[input]"r"(&coherence_data[i]));
1442 }
1443#endif
1444 return IRQ_HANDLED;
1445 }
1446 return IRQ_NONE;
1447}
1448
York Sun9b53a9e2008-04-28 02:15:34 -07001449#ifdef CONFIG_PM
1450/*
1451 * Power management hooks. Note that we won't be called from IRQ context,
1452 * unlike the blank functions above, so we may sleep.
1453 */
Grant Likely2dc11582010-08-06 09:25:50 -06001454static int fsl_diu_suspend(struct platform_device *ofdev, pm_message_t state)
York Sun9b53a9e2008-04-28 02:15:34 -07001455{
Timur Tabib7795052011-12-08 17:27:31 -06001456 struct fsl_diu_data *data;
York Sun9b53a9e2008-04-28 02:15:34 -07001457
Timur Tabib7795052011-12-08 17:27:31 -06001458 data = dev_get_drvdata(&ofdev->dev);
Michael Neulingf7f5ef02012-01-28 19:35:24 +00001459 disable_lcdc(data->fsl_diu_info);
York Sun9b53a9e2008-04-28 02:15:34 -07001460
1461 return 0;
1462}
1463
Grant Likely2dc11582010-08-06 09:25:50 -06001464static int fsl_diu_resume(struct platform_device *ofdev)
York Sun9b53a9e2008-04-28 02:15:34 -07001465{
Timur Tabib7795052011-12-08 17:27:31 -06001466 struct fsl_diu_data *data;
York Sun9b53a9e2008-04-28 02:15:34 -07001467
Timur Tabib7795052011-12-08 17:27:31 -06001468 data = dev_get_drvdata(&ofdev->dev);
Michael Neulingf7f5ef02012-01-28 19:35:24 +00001469 enable_lcdc(data->fsl_diu_info);
York Sun9b53a9e2008-04-28 02:15:34 -07001470
1471 return 0;
1472}
1473
1474#else
1475#define fsl_diu_suspend NULL
1476#define fsl_diu_resume NULL
1477#endif /* CONFIG_PM */
1478
York Sun9b53a9e2008-04-28 02:15:34 -07001479static ssize_t store_monitor(struct device *device,
1480 struct device_attribute *attr, const char *buf, size_t count)
1481{
Timur Tabi7653aaa2011-07-09 15:38:14 -05001482 enum fsl_diu_monitor_port old_monitor_port;
Timur Tabib7795052011-12-08 17:27:31 -06001483 struct fsl_diu_data *data =
York Sun9b53a9e2008-04-28 02:15:34 -07001484 container_of(attr, struct fsl_diu_data, dev_attr);
1485
Timur Tabib7795052011-12-08 17:27:31 -06001486 old_monitor_port = data->monitor_port;
1487 data->monitor_port = fsl_diu_name_to_port(buf);
York Sun9b53a9e2008-04-28 02:15:34 -07001488
Timur Tabib7795052011-12-08 17:27:31 -06001489 if (old_monitor_port != data->monitor_port) {
York Sun9b53a9e2008-04-28 02:15:34 -07001490 /* All AOIs need adjust pixel format
1491 * fsl_diu_set_par only change the pixsel format here
1492 * unlikely to fail. */
Timur Tabiddd3d902011-12-02 17:03:27 -06001493 unsigned int i;
1494
1495 for (i=0; i < NUM_AOIS; i++)
Timur Tabib7795052011-12-08 17:27:31 -06001496 fsl_diu_set_par(&data->fsl_diu_info[i]);
York Sun9b53a9e2008-04-28 02:15:34 -07001497 }
1498 return count;
1499}
1500
1501static ssize_t show_monitor(struct device *device,
1502 struct device_attribute *attr, char *buf)
1503{
Timur Tabib7795052011-12-08 17:27:31 -06001504 struct fsl_diu_data *data =
York Sun9b53a9e2008-04-28 02:15:34 -07001505 container_of(attr, struct fsl_diu_data, dev_attr);
Timur Tabi7653aaa2011-07-09 15:38:14 -05001506
Timur Tabib7795052011-12-08 17:27:31 -06001507 switch (data->monitor_port) {
Timur Tabi7653aaa2011-07-09 15:38:14 -05001508 case FSL_DIU_PORT_DVI:
1509 return sprintf(buf, "DVI\n");
1510 case FSL_DIU_PORT_LVDS:
1511 return sprintf(buf, "Single-link LVDS\n");
1512 case FSL_DIU_PORT_DLVDS:
1513 return sprintf(buf, "Dual-link LVDS\n");
1514 }
1515
1516 return 0;
York Sun9b53a9e2008-04-28 02:15:34 -07001517}
1518
Timur Tabi9e52ba62011-09-15 16:44:50 -05001519static int __devinit fsl_diu_probe(struct platform_device *pdev)
York Sun9b53a9e2008-04-28 02:15:34 -07001520{
Timur Tabi9e52ba62011-09-15 16:44:50 -05001521 struct device_node *np = pdev->dev.of_node;
York Sun9b53a9e2008-04-28 02:15:34 -07001522 struct mfb_info *mfbi;
Timur Tabib7795052011-12-08 17:27:31 -06001523 struct fsl_diu_data *data;
Anatolij Gustschin4b5006e2010-07-23 04:00:37 +00001524 int diu_mode;
Timur Tabib7795052011-12-08 17:27:31 -06001525 dma_addr_t dma_addr; /* DMA addr of fsl_diu_data struct */
Timur Tabiddd3d902011-12-02 17:03:27 -06001526 unsigned int i;
1527 int ret;
York Sun9b53a9e2008-04-28 02:15:34 -07001528
Damien Cassouc1f383d2012-07-31 18:39:20 +02001529 data = dmam_alloc_coherent(&pdev->dev, sizeof(struct fsl_diu_data),
1530 &dma_addr, GFP_DMA | __GFP_ZERO);
Timur Tabib7795052011-12-08 17:27:31 -06001531 if (!data)
York Sun9b53a9e2008-04-28 02:15:34 -07001532 return -ENOMEM;
Timur Tabib7795052011-12-08 17:27:31 -06001533 data->dma_addr = dma_addr;
Timur Tabiddd3d902011-12-02 17:03:27 -06001534
1535 /*
1536 * dma_alloc_coherent() uses a page allocator, so the address is
1537 * always page-aligned. We need the memory to be 32-byte aligned,
1538 * so that's good. However, if one day the allocator changes, we
1539 * need to catch that. It's not worth the effort to handle unaligned
1540 * alloctions now because it's highly unlikely to ever be a problem.
1541 */
Timur Tabib7795052011-12-08 17:27:31 -06001542 if ((unsigned long)data & 31) {
Timur Tabiddd3d902011-12-02 17:03:27 -06001543 dev_err(&pdev->dev, "misaligned allocation");
1544 ret = -ENOMEM;
1545 goto error;
1546 }
York Sun9b53a9e2008-04-28 02:15:34 -07001547
Timur Tabib7795052011-12-08 17:27:31 -06001548 spin_lock_init(&data->reg_lock);
Timur Tabi3c755b72011-10-04 19:36:51 -05001549
Timur Tabiddd3d902011-12-02 17:03:27 -06001550 for (i = 0; i < NUM_AOIS; i++) {
Timur Tabib7795052011-12-08 17:27:31 -06001551 struct fb_info *info = &data->fsl_diu_info[i];
Timur Tabiddd3d902011-12-02 17:03:27 -06001552
1553 info->device = &pdev->dev;
Timur Tabib7795052011-12-08 17:27:31 -06001554 info->par = &data->mfb[i];
Timur Tabiddd3d902011-12-02 17:03:27 -06001555
1556 /*
1557 * We store the physical address of the AD in the reserved
1558 * 'paddr' field of the AD itself.
1559 */
Timur Tabib7795052011-12-08 17:27:31 -06001560 data->ad[i].paddr = DMA_ADDR(data, ad[i]);
Timur Tabiddd3d902011-12-02 17:03:27 -06001561
1562 info->fix.smem_start = 0;
1563
1564 /* Initialize the AOI data structure */
1565 mfbi = info->par;
York Sun9b53a9e2008-04-28 02:15:34 -07001566 memcpy(mfbi, &mfb_template[i], sizeof(struct mfb_info));
Timur Tabib7795052011-12-08 17:27:31 -06001567 mfbi->parent = data;
1568 mfbi->ad = &data->ad[i];
Anatolij Gustschin8b856f02010-07-23 04:00:39 +00001569
Timur Tabi2572df92011-09-28 16:19:51 -05001570 if (mfbi->index == PLANE0) {
Anatolij Gustschin8b856f02010-07-23 04:00:39 +00001571 const u8 *prop;
1572 int len;
1573
1574 /* Get EDID */
1575 prop = of_get_property(np, "edid", &len);
1576 if (prop && len == EDID_LENGTH)
1577 mfbi->edid_data = kmemdup(prop, EDID_LENGTH,
1578 GFP_KERNEL);
1579 }
York Sun9b53a9e2008-04-28 02:15:34 -07001580 }
1581
Timur Tabib7795052011-12-08 17:27:31 -06001582 data->diu_reg = of_iomap(np, 0);
1583 if (!data->diu_reg) {
Timur Tabi9e52ba62011-09-15 16:44:50 -05001584 dev_err(&pdev->dev, "cannot map DIU registers\n");
York Sun9b53a9e2008-04-28 02:15:34 -07001585 ret = -EFAULT;
Timur Tabiddd3d902011-12-02 17:03:27 -06001586 goto error;
York Sun9b53a9e2008-04-28 02:15:34 -07001587 }
1588
Timur Tabib7795052011-12-08 17:27:31 -06001589 diu_mode = in_be32(&data->diu_reg->diu_mode);
Timur Tabic4e5a022011-09-28 16:19:53 -05001590 if (diu_mode == MFB_MODE0)
Timur Tabib7795052011-12-08 17:27:31 -06001591 out_be32(&data->diu_reg->diu_mode, 0); /* disable DIU */
York Sun9b53a9e2008-04-28 02:15:34 -07001592
1593 /* Get the IRQ of the DIU */
Timur Tabib7795052011-12-08 17:27:31 -06001594 data->irq = irq_of_parse_and_map(np, 0);
York Sun9b53a9e2008-04-28 02:15:34 -07001595
Timur Tabib7795052011-12-08 17:27:31 -06001596 if (!data->irq) {
Timur Tabi9e52ba62011-09-15 16:44:50 -05001597 dev_err(&pdev->dev, "could not get DIU IRQ\n");
York Sun9b53a9e2008-04-28 02:15:34 -07001598 ret = -EINVAL;
1599 goto error;
1600 }
Timur Tabib7795052011-12-08 17:27:31 -06001601 data->monitor_port = monitor_port;
York Sun9b53a9e2008-04-28 02:15:34 -07001602
Timur Tabiddd3d902011-12-02 17:03:27 -06001603 /* Initialize the dummy Area Descriptor */
Timur Tabib7795052011-12-08 17:27:31 -06001604 data->dummy_ad.addr = cpu_to_le32(DMA_ADDR(data, dummy_aoi));
1605 data->dummy_ad.pix_fmt = 0x88882317;
1606 data->dummy_ad.src_size_g_alpha = cpu_to_le32((4 << 12) | 4);
1607 data->dummy_ad.aoi_size = cpu_to_le32((4 << 16) | 2);
1608 data->dummy_ad.offset_xyi = 0;
1609 data->dummy_ad.offset_xyd = 0;
1610 data->dummy_ad.next_ad = 0;
1611 data->dummy_ad.paddr = DMA_ADDR(data, dummy_ad);
York Sun9b53a9e2008-04-28 02:15:34 -07001612
Anatolij Gustschin4b5006e2010-07-23 04:00:37 +00001613 /*
1614 * Let DIU display splash screen if it was pre-initialized
1615 * by the bootloader, set dummy area descriptor otherwise.
1616 */
Timur Tabic4e5a022011-09-28 16:19:53 -05001617 if (diu_mode == MFB_MODE0)
Timur Tabib7795052011-12-08 17:27:31 -06001618 out_be32(&data->diu_reg->desc[0], data->dummy_ad.paddr);
Anatolij Gustschin4b5006e2010-07-23 04:00:37 +00001619
Timur Tabib7795052011-12-08 17:27:31 -06001620 out_be32(&data->diu_reg->desc[1], data->dummy_ad.paddr);
1621 out_be32(&data->diu_reg->desc[2], data->dummy_ad.paddr);
York Sun9b53a9e2008-04-28 02:15:34 -07001622
Timur Tabiddd3d902011-12-02 17:03:27 -06001623 for (i = 0; i < NUM_AOIS; i++) {
Timur Tabib7795052011-12-08 17:27:31 -06001624 ret = install_fb(&data->fsl_diu_info[i]);
York Sun9b53a9e2008-04-28 02:15:34 -07001625 if (ret) {
Timur Tabi9e52ba62011-09-15 16:44:50 -05001626 dev_err(&pdev->dev, "could not register fb %d\n", i);
York Sun9b53a9e2008-04-28 02:15:34 -07001627 goto error;
1628 }
1629 }
1630
Timur Tabif74de502012-10-16 17:33:45 -05001631 /*
1632 * Older versions of U-Boot leave interrupts enabled, so disable
1633 * all of them and clear the status register.
1634 */
1635 out_be32(&data->diu_reg->int_mask, 0xffffffff);
1636 in_be32(&data->diu_reg->int_status);
1637
1638 ret = request_irq(data->irq, fsl_diu_isr, 0, "fsl-diu-fb",
1639 &data->diu_reg);
1640 if (ret) {
Timur Tabi9e52ba62011-09-15 16:44:50 -05001641 dev_err(&pdev->dev, "could not claim irq\n");
York Sun9b53a9e2008-04-28 02:15:34 -07001642 goto error;
1643 }
1644
Timur Tabib7795052011-12-08 17:27:31 -06001645 sysfs_attr_init(&data->dev_attr.attr);
1646 data->dev_attr.attr.name = "monitor";
1647 data->dev_attr.attr.mode = S_IRUGO|S_IWUSR;
1648 data->dev_attr.show = show_monitor;
1649 data->dev_attr.store = store_monitor;
1650 ret = device_create_file(&pdev->dev, &data->dev_attr);
Timur Tabiddd3d902011-12-02 17:03:27 -06001651 if (ret) {
Timur Tabi9e52ba62011-09-15 16:44:50 -05001652 dev_err(&pdev->dev, "could not create sysfs file %s\n",
Timur Tabib7795052011-12-08 17:27:31 -06001653 data->dev_attr.attr.name);
York Sun9b53a9e2008-04-28 02:15:34 -07001654 }
1655
Timur Tabib7795052011-12-08 17:27:31 -06001656 dev_set_drvdata(&pdev->dev, data);
York Sun9b53a9e2008-04-28 02:15:34 -07001657 return 0;
1658
1659error:
Timur Tabiddd3d902011-12-02 17:03:27 -06001660 for (i = 0; i < NUM_AOIS; i++)
Timur Tabib7795052011-12-08 17:27:31 -06001661 uninstall_fb(&data->fsl_diu_info[i]);
Timur Tabi3f78bbd2011-09-15 16:44:56 -05001662
Timur Tabib7795052011-12-08 17:27:31 -06001663 iounmap(data->diu_reg);
York Sun9b53a9e2008-04-28 02:15:34 -07001664
York Sun9b53a9e2008-04-28 02:15:34 -07001665 return ret;
1666}
1667
Timur Tabi9e52ba62011-09-15 16:44:50 -05001668static int fsl_diu_remove(struct platform_device *pdev)
York Sun9b53a9e2008-04-28 02:15:34 -07001669{
Timur Tabib7795052011-12-08 17:27:31 -06001670 struct fsl_diu_data *data;
York Sun9b53a9e2008-04-28 02:15:34 -07001671 int i;
1672
Timur Tabib7795052011-12-08 17:27:31 -06001673 data = dev_get_drvdata(&pdev->dev);
1674 disable_lcdc(&data->fsl_diu_info[0]);
Timur Tabif74de502012-10-16 17:33:45 -05001675
1676 free_irq(data->irq, &data->diu_reg);
Timur Tabiddd3d902011-12-02 17:03:27 -06001677
1678 for (i = 0; i < NUM_AOIS; i++)
Timur Tabib7795052011-12-08 17:27:31 -06001679 uninstall_fb(&data->fsl_diu_info[i]);
Timur Tabiddd3d902011-12-02 17:03:27 -06001680
Timur Tabib7795052011-12-08 17:27:31 -06001681 iounmap(data->diu_reg);
Timur Tabiddd3d902011-12-02 17:03:27 -06001682
York Sun9b53a9e2008-04-28 02:15:34 -07001683 return 0;
1684}
1685
1686#ifndef MODULE
1687static int __init fsl_diu_setup(char *options)
1688{
1689 char *opt;
1690 unsigned long val;
1691
1692 if (!options || !*options)
1693 return 0;
1694
1695 while ((opt = strsep(&options, ",")) != NULL) {
1696 if (!*opt)
1697 continue;
1698 if (!strncmp(opt, "monitor=", 8)) {
Timur Tabi7653aaa2011-07-09 15:38:14 -05001699 monitor_port = fsl_diu_name_to_port(opt + 8);
York Sun9b53a9e2008-04-28 02:15:34 -07001700 } else if (!strncmp(opt, "bpp=", 4)) {
1701 if (!strict_strtoul(opt + 4, 10, &val))
1702 default_bpp = val;
1703 } else
1704 fb_mode = opt;
1705 }
1706
1707 return 0;
1708}
1709#endif
1710
1711static struct of_device_id fsl_diu_match[] = {
Anatolij Gustschind24720a2010-02-17 07:33:22 -07001712#ifdef CONFIG_PPC_MPC512x
1713 {
1714 .compatible = "fsl,mpc5121-diu",
1715 },
1716#endif
York Sun9b53a9e2008-04-28 02:15:34 -07001717 {
1718 .compatible = "fsl,diu",
1719 },
1720 {}
1721};
1722MODULE_DEVICE_TABLE(of, fsl_diu_match);
1723
Grant Likely28541d02011-02-22 21:07:43 -07001724static struct platform_driver fsl_diu_driver = {
Grant Likely40182942010-04-13 16:13:02 -07001725 .driver = {
Timur Tabif8c6bf62011-09-15 16:44:53 -05001726 .name = "fsl-diu-fb",
Grant Likely40182942010-04-13 16:13:02 -07001727 .owner = THIS_MODULE,
1728 .of_match_table = fsl_diu_match,
1729 },
York Sun9b53a9e2008-04-28 02:15:34 -07001730 .probe = fsl_diu_probe,
1731 .remove = fsl_diu_remove,
1732 .suspend = fsl_diu_suspend,
1733 .resume = fsl_diu_resume,
1734};
1735
1736static int __init fsl_diu_init(void)
1737{
1738#ifdef CONFIG_NOT_COHERENT_CACHE
1739 struct device_node *np;
1740 const u32 *prop;
1741#endif
1742 int ret;
1743#ifndef MODULE
1744 char *option;
1745
1746 /*
1747 * For kernel boot options (in 'video=xxxfb:<options>' format)
1748 */
1749 if (fb_get_options("fslfb", &option))
1750 return -ENODEV;
1751 fsl_diu_setup(option);
Timur Tabi7653aaa2011-07-09 15:38:14 -05001752#else
1753 monitor_port = fsl_diu_name_to_port(monitor_string);
York Sun9b53a9e2008-04-28 02:15:34 -07001754#endif
Timur Tabi154152a2011-09-15 16:44:47 -05001755 pr_info("Freescale Display Interface Unit (DIU) framebuffer driver\n");
York Sun9b53a9e2008-04-28 02:15:34 -07001756
1757#ifdef CONFIG_NOT_COHERENT_CACHE
1758 np = of_find_node_by_type(NULL, "cpu");
1759 if (!np) {
Timur Tabi154152a2011-09-15 16:44:47 -05001760 pr_err("fsl-diu-fb: can't find 'cpu' device node\n");
York Sun9b53a9e2008-04-28 02:15:34 -07001761 return -ENODEV;
1762 }
1763
1764 prop = of_get_property(np, "d-cache-size", NULL);
Julia Lawall5394ba02008-08-05 13:01:28 -07001765 if (prop == NULL) {
Timur Tabi154152a2011-09-15 16:44:47 -05001766 pr_err("fsl-diu-fb: missing 'd-cache-size' property' "
1767 "in 'cpu' node\n");
Julia Lawall5394ba02008-08-05 13:01:28 -07001768 of_node_put(np);
York Sun9b53a9e2008-04-28 02:15:34 -07001769 return -ENODEV;
Julia Lawall5394ba02008-08-05 13:01:28 -07001770 }
York Sun9b53a9e2008-04-28 02:15:34 -07001771
Timur Tabi4a85dc8b2011-09-15 16:44:46 -05001772 /*
1773 * Freescale PLRU requires 13/8 times the cache size to do a proper
1774 * displacement flush
York Sun9b53a9e2008-04-28 02:15:34 -07001775 */
Timur Tabi9e52ba62011-09-15 16:44:50 -05001776 coherence_data_size = be32_to_cpup(prop) * 13;
York Sun9b53a9e2008-04-28 02:15:34 -07001777 coherence_data_size /= 8;
1778
Timur Tabi5cc2a362012-10-16 17:33:46 -05001779 pr_debug("fsl-diu-fb: coherence data size is %zu bytes\n",
1780 coherence_data_size);
1781
York Sun9b53a9e2008-04-28 02:15:34 -07001782 prop = of_get_property(np, "d-cache-line-size", NULL);
Julia Lawall5394ba02008-08-05 13:01:28 -07001783 if (prop == NULL) {
Timur Tabi154152a2011-09-15 16:44:47 -05001784 pr_err("fsl-diu-fb: missing 'd-cache-line-size' property' "
1785 "in 'cpu' node\n");
Julia Lawall5394ba02008-08-05 13:01:28 -07001786 of_node_put(np);
York Sun9b53a9e2008-04-28 02:15:34 -07001787 return -ENODEV;
Julia Lawall5394ba02008-08-05 13:01:28 -07001788 }
Timur Tabi9e52ba62011-09-15 16:44:50 -05001789 d_cache_line_size = be32_to_cpup(prop);
York Sun9b53a9e2008-04-28 02:15:34 -07001790
Timur Tabi5cc2a362012-10-16 17:33:46 -05001791 pr_debug("fsl-diu-fb: cache lines size is %u bytes\n",
1792 d_cache_line_size);
1793
York Sun9b53a9e2008-04-28 02:15:34 -07001794 of_node_put(np);
1795 coherence_data = vmalloc(coherence_data_size);
Timur Tabi5cc2a362012-10-16 17:33:46 -05001796 if (!coherence_data) {
1797 pr_err("fsl-diu-fb: could not allocate coherence data "
1798 "(size=%zu)\n", coherence_data_size);
York Sun9b53a9e2008-04-28 02:15:34 -07001799 return -ENOMEM;
Timur Tabi5cc2a362012-10-16 17:33:46 -05001800 }
1801
York Sun9b53a9e2008-04-28 02:15:34 -07001802#endif
Timur Tabi4a85dc8b2011-09-15 16:44:46 -05001803
Grant Likely28541d02011-02-22 21:07:43 -07001804 ret = platform_driver_register(&fsl_diu_driver);
York Sun9b53a9e2008-04-28 02:15:34 -07001805 if (ret) {
Timur Tabi154152a2011-09-15 16:44:47 -05001806 pr_err("fsl-diu-fb: failed to register platform driver\n");
York Sun9b53a9e2008-04-28 02:15:34 -07001807#if defined(CONFIG_NOT_COHERENT_CACHE)
1808 vfree(coherence_data);
1809#endif
York Sun9b53a9e2008-04-28 02:15:34 -07001810 }
1811 return ret;
1812}
1813
1814static void __exit fsl_diu_exit(void)
1815{
Grant Likely28541d02011-02-22 21:07:43 -07001816 platform_driver_unregister(&fsl_diu_driver);
York Sun9b53a9e2008-04-28 02:15:34 -07001817#if defined(CONFIG_NOT_COHERENT_CACHE)
1818 vfree(coherence_data);
1819#endif
1820}
1821
1822module_init(fsl_diu_init);
1823module_exit(fsl_diu_exit);
1824
1825MODULE_AUTHOR("York Sun <yorksun@freescale.com>");
1826MODULE_DESCRIPTION("Freescale DIU framebuffer driver");
1827MODULE_LICENSE("GPL");
1828
1829module_param_named(mode, fb_mode, charp, 0);
1830MODULE_PARM_DESC(mode,
1831 "Specify resolution as \"<xres>x<yres>[-<bpp>][@<refresh>]\" ");
1832module_param_named(bpp, default_bpp, ulong, 0);
Timur Tabi154152a2011-09-15 16:44:47 -05001833MODULE_PARM_DESC(bpp, "Specify bit-per-pixel if not specified in 'mode'");
Timur Tabi7653aaa2011-07-09 15:38:14 -05001834module_param_named(monitor, monitor_string, charp, 0);
1835MODULE_PARM_DESC(monitor, "Specify the monitor port "
1836 "(\"dvi\", \"lvds\", or \"dlvds\") if supported by the platform");
York Sun9b53a9e2008-04-28 02:15:34 -07001837