blob: 37175ac6584abbaef0ea0b84a92ffcf50aad7b1a [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;
York Sun9b53a9e2008-04-28 02:15:34 -0700340 unsigned char g_alpha;
341 unsigned int count;
342 int x_aoi_d; /* aoi display x offset to physical screen */
343 int y_aoi_d; /* aoi display y offset to physical screen */
344 struct fsl_diu_data *parent;
Anatolij Gustschin8b856f02010-07-23 04:00:39 +0000345 u8 *edid_data;
York Sun9b53a9e2008-04-28 02:15:34 -0700346};
347
Timur Tabiddd3d902011-12-02 17:03:27 -0600348/**
349 * struct fsl_diu_data - per-DIU data structure
350 * @dma_addr: DMA address of this structure
351 * @fsl_diu_info: fb_info objects, one per AOI
352 * @dev_attr: sysfs structure
353 * @irq: IRQ
Timur Tabiddd3d902011-12-02 17:03:27 -0600354 * @monitor_port: the monitor port this DIU is connected to
355 * @diu_reg: pointer to the DIU hardware registers
356 * @reg_lock: spinlock for register access
357 * @dummy_aoi: video buffer for the 4x4 32-bit dummy AOI
358 * dummy_ad: DIU Area Descriptor for the dummy AOI
359 * @ad[]: Area Descriptors for each real AOI
360 * @gamma: gamma color table
361 * @cursor: hardware cursor data
362 *
363 * This data structure must be allocated with 32-byte alignment, so that the
364 * internal fields can be aligned properly.
365 */
366struct fsl_diu_data {
367 dma_addr_t dma_addr;
368 struct fb_info fsl_diu_info[NUM_AOIS];
369 struct mfb_info mfb[NUM_AOIS];
370 struct device_attribute dev_attr;
371 unsigned int irq;
Timur Tabiddd3d902011-12-02 17:03:27 -0600372 enum fsl_diu_monitor_port monitor_port;
373 struct diu __iomem *diu_reg;
374 spinlock_t reg_lock;
375 u8 dummy_aoi[4 * 4 * 4];
376 struct diu_ad dummy_ad __aligned(8);
377 struct diu_ad ad[NUM_AOIS] __aligned(8);
378 u8 gamma[256 * 3] __aligned(32);
379 u8 cursor[MAX_CURS * MAX_CURS * 2] __aligned(32);
380} __aligned(32);
381
382/* Determine the DMA address of a member of the fsl_diu_data structure */
383#define DMA_ADDR(p, f) ((p)->dma_addr + offsetof(struct fsl_diu_data, f))
York Sun9b53a9e2008-04-28 02:15:34 -0700384
385static struct mfb_info mfb_template[] = {
Timur Tabi2572df92011-09-28 16:19:51 -0500386 {
387 .index = PLANE0,
Timur Tabi4a85dc8b2011-09-15 16:44:46 -0500388 .id = "Panel0",
389 .registered = 0,
390 .count = 0,
391 .x_aoi_d = 0,
392 .y_aoi_d = 0,
York Sun9b53a9e2008-04-28 02:15:34 -0700393 },
Timur Tabi2572df92011-09-28 16:19:51 -0500394 {
395 .index = PLANE1_AOI0,
Timur Tabi4a85dc8b2011-09-15 16:44:46 -0500396 .id = "Panel1 AOI0",
397 .registered = 0,
398 .g_alpha = 0xff,
399 .count = 0,
400 .x_aoi_d = 0,
401 .y_aoi_d = 0,
York Sun9b53a9e2008-04-28 02:15:34 -0700402 },
Timur Tabi2572df92011-09-28 16:19:51 -0500403 {
404 .index = PLANE1_AOI1,
Timur Tabi4a85dc8b2011-09-15 16:44:46 -0500405 .id = "Panel1 AOI1",
406 .registered = 0,
407 .g_alpha = 0xff,
408 .count = 0,
409 .x_aoi_d = 0,
410 .y_aoi_d = 480,
York Sun9b53a9e2008-04-28 02:15:34 -0700411 },
Timur Tabi2572df92011-09-28 16:19:51 -0500412 {
413 .index = PLANE2_AOI0,
Timur Tabi4a85dc8b2011-09-15 16:44:46 -0500414 .id = "Panel2 AOI0",
415 .registered = 0,
416 .g_alpha = 0xff,
417 .count = 0,
418 .x_aoi_d = 640,
419 .y_aoi_d = 0,
York Sun9b53a9e2008-04-28 02:15:34 -0700420 },
Timur Tabi2572df92011-09-28 16:19:51 -0500421 {
422 .index = PLANE2_AOI1,
Timur Tabi4a85dc8b2011-09-15 16:44:46 -0500423 .id = "Panel2 AOI1",
424 .registered = 0,
425 .g_alpha = 0xff,
426 .count = 0,
427 .x_aoi_d = 640,
428 .y_aoi_d = 480,
York Sun9b53a9e2008-04-28 02:15:34 -0700429 },
430};
431
Timur Tabi5cc2a362012-10-16 17:33:46 -0500432#ifdef DEBUG
433static void __attribute__ ((unused)) fsl_diu_dump(struct diu __iomem *hw)
434{
435 mb();
436 pr_debug("DIU: desc=%08x,%08x,%08x, gamma=%08x pallete=%08x "
437 "cursor=%08x curs_pos=%08x diu_mode=%08x bgnd=%08x "
438 "disp_size=%08x hsyn_para=%08x vsyn_para=%08x syn_pol=%08x "
439 "thresholds=%08x int_mask=%08x plut=%08x\n",
440 hw->desc[0], hw->desc[1], hw->desc[2], hw->gamma,
441 hw->pallete, hw->cursor, hw->curs_pos, hw->diu_mode,
442 hw->bgnd, hw->disp_size, hw->hsyn_para, hw->vsyn_para,
443 hw->syn_pol, hw->thresholds, hw->int_mask, hw->plut);
444 rmb();
445}
446#endif
447
Timur Tabi6b51d512008-07-23 21:31:39 -0700448/**
Timur Tabi7653aaa2011-07-09 15:38:14 -0500449 * fsl_diu_name_to_port - convert a port name to a monitor port enum
450 *
451 * Takes the name of a monitor port ("dvi", "lvds", or "dlvds") and returns
452 * the enum fsl_diu_monitor_port that corresponds to that string.
453 *
454 * For compatibility with older versions, a number ("0", "1", or "2") is also
455 * supported.
456 *
457 * If the string is unknown, DVI is assumed.
458 *
459 * If the particular port is not supported by the platform, another port
460 * (platform-specific) is chosen instead.
461 */
462static enum fsl_diu_monitor_port fsl_diu_name_to_port(const char *s)
463{
464 enum fsl_diu_monitor_port port = FSL_DIU_PORT_DVI;
465 unsigned long val;
466
467 if (s) {
468 if (!strict_strtoul(s, 10, &val) && (val <= 2))
469 port = (enum fsl_diu_monitor_port) val;
470 else if (strncmp(s, "lvds", 4) == 0)
471 port = FSL_DIU_PORT_LVDS;
472 else if (strncmp(s, "dlvds", 5) == 0)
473 port = FSL_DIU_PORT_DLVDS;
474 }
475
476 return diu_ops.valid_monitor_port(port);
477}
478
Anatolij Gustschin0d9dab32010-07-23 04:00:35 +0000479/*
480 * Workaround for failed writing desc register of planes.
481 * Needed with MPC5121 DIU rev 2.0 silicon.
482 */
483void wr_reg_wa(u32 *reg, u32 val)
484{
485 do {
486 out_be32(reg, val);
487 } while (in_be32(reg) != val);
488}
489
Timur Tabi7e47c212011-09-28 16:19:52 -0500490static void fsl_diu_enable_panel(struct fb_info *info)
York Sun9b53a9e2008-04-28 02:15:34 -0700491{
492 struct mfb_info *pmfbi, *cmfbi, *mfbi = info->par;
York Sun9b53a9e2008-04-28 02:15:34 -0700493 struct diu_ad *ad = mfbi->ad;
Timur Tabib7795052011-12-08 17:27:31 -0600494 struct fsl_diu_data *data = mfbi->parent;
495 struct diu __iomem *hw = data->diu_reg;
York Sun9b53a9e2008-04-28 02:15:34 -0700496
Timur Tabi7e47c212011-09-28 16:19:52 -0500497 switch (mfbi->index) {
498 case PLANE0:
Timur Tabi7af3b132012-10-16 17:33:48 -0500499 wr_reg_wa(&hw->desc[0], ad->paddr);
Timur Tabi7e47c212011-09-28 16:19:52 -0500500 break;
501 case PLANE1_AOI0:
Timur Tabib7795052011-12-08 17:27:31 -0600502 cmfbi = &data->mfb[2];
Timur Tabi7e47c212011-09-28 16:19:52 -0500503 if (hw->desc[1] != ad->paddr) { /* AOI0 closed */
504 if (cmfbi->count > 0) /* AOI1 open */
505 ad->next_ad =
506 cpu_to_le32(cmfbi->ad->paddr);
507 else
508 ad->next_ad = 0;
509 wr_reg_wa(&hw->desc[1], ad->paddr);
York Sun9b53a9e2008-04-28 02:15:34 -0700510 }
Timur Tabi7e47c212011-09-28 16:19:52 -0500511 break;
512 case PLANE2_AOI0:
Timur Tabib7795052011-12-08 17:27:31 -0600513 cmfbi = &data->mfb[4];
Timur Tabi7e47c212011-09-28 16:19:52 -0500514 if (hw->desc[2] != ad->paddr) { /* AOI0 closed */
515 if (cmfbi->count > 0) /* AOI1 open */
516 ad->next_ad =
517 cpu_to_le32(cmfbi->ad->paddr);
518 else
519 ad->next_ad = 0;
520 wr_reg_wa(&hw->desc[2], ad->paddr);
521 }
522 break;
523 case PLANE1_AOI1:
Timur Tabib7795052011-12-08 17:27:31 -0600524 pmfbi = &data->mfb[1];
Timur Tabi7e47c212011-09-28 16:19:52 -0500525 ad->next_ad = 0;
Timur Tabib7795052011-12-08 17:27:31 -0600526 if (hw->desc[1] == data->dummy_ad.paddr)
Timur Tabi7e47c212011-09-28 16:19:52 -0500527 wr_reg_wa(&hw->desc[1], ad->paddr);
528 else /* AOI0 open */
529 pmfbi->ad->next_ad = cpu_to_le32(ad->paddr);
530 break;
531 case PLANE2_AOI1:
Timur Tabib7795052011-12-08 17:27:31 -0600532 pmfbi = &data->mfb[3];
Timur Tabi7e47c212011-09-28 16:19:52 -0500533 ad->next_ad = 0;
Timur Tabib7795052011-12-08 17:27:31 -0600534 if (hw->desc[2] == data->dummy_ad.paddr)
Timur Tabi7e47c212011-09-28 16:19:52 -0500535 wr_reg_wa(&hw->desc[2], ad->paddr);
536 else /* AOI0 was open */
537 pmfbi->ad->next_ad = cpu_to_le32(ad->paddr);
538 break;
539 }
York Sun9b53a9e2008-04-28 02:15:34 -0700540}
541
Timur Tabi2572df92011-09-28 16:19:51 -0500542static void fsl_diu_disable_panel(struct fb_info *info)
York Sun9b53a9e2008-04-28 02:15:34 -0700543{
544 struct mfb_info *pmfbi, *cmfbi, *mfbi = info->par;
York Sun9b53a9e2008-04-28 02:15:34 -0700545 struct diu_ad *ad = mfbi->ad;
Timur Tabib7795052011-12-08 17:27:31 -0600546 struct fsl_diu_data *data = mfbi->parent;
547 struct diu __iomem *hw = data->diu_reg;
York Sun9b53a9e2008-04-28 02:15:34 -0700548
549 switch (mfbi->index) {
Timur Tabi2572df92011-09-28 16:19:51 -0500550 case PLANE0:
Timur Tabi7af3b132012-10-16 17:33:48 -0500551 wr_reg_wa(&hw->desc[0], 0);
York Sun9b53a9e2008-04-28 02:15:34 -0700552 break;
Timur Tabi2572df92011-09-28 16:19:51 -0500553 case PLANE1_AOI0:
Timur Tabib7795052011-12-08 17:27:31 -0600554 cmfbi = &data->mfb[2];
York Sun9b53a9e2008-04-28 02:15:34 -0700555 if (cmfbi->count > 0) /* AOI1 is open */
Anatolij Gustschin0d9dab32010-07-23 04:00:35 +0000556 wr_reg_wa(&hw->desc[1], cmfbi->ad->paddr);
York Sun9b53a9e2008-04-28 02:15:34 -0700557 /* move AOI1 to the first */
558 else /* AOI1 was closed */
Timur Tabib7795052011-12-08 17:27:31 -0600559 wr_reg_wa(&hw->desc[1], data->dummy_ad.paddr);
York Sun9b53a9e2008-04-28 02:15:34 -0700560 /* close AOI 0 */
561 break;
Timur Tabi2572df92011-09-28 16:19:51 -0500562 case PLANE2_AOI0:
Timur Tabib7795052011-12-08 17:27:31 -0600563 cmfbi = &data->mfb[4];
York Sun9b53a9e2008-04-28 02:15:34 -0700564 if (cmfbi->count > 0) /* AOI1 is open */
Anatolij Gustschin0d9dab32010-07-23 04:00:35 +0000565 wr_reg_wa(&hw->desc[2], cmfbi->ad->paddr);
York Sun9b53a9e2008-04-28 02:15:34 -0700566 /* move AOI1 to the first */
567 else /* AOI1 was closed */
Timur Tabib7795052011-12-08 17:27:31 -0600568 wr_reg_wa(&hw->desc[2], data->dummy_ad.paddr);
York Sun9b53a9e2008-04-28 02:15:34 -0700569 /* close AOI 0 */
570 break;
Timur Tabi2572df92011-09-28 16:19:51 -0500571 case PLANE1_AOI1:
Timur Tabib7795052011-12-08 17:27:31 -0600572 pmfbi = &data->mfb[1];
York Sun9b53a9e2008-04-28 02:15:34 -0700573 if (hw->desc[1] != ad->paddr) {
574 /* AOI1 is not the first in the chain */
575 if (pmfbi->count > 0)
576 /* AOI0 is open, must be the first */
577 pmfbi->ad->next_ad = 0;
578 } else /* AOI1 is the first in the chain */
Timur Tabib7795052011-12-08 17:27:31 -0600579 wr_reg_wa(&hw->desc[1], data->dummy_ad.paddr);
York Sun9b53a9e2008-04-28 02:15:34 -0700580 /* close AOI 1 */
581 break;
Timur Tabi2572df92011-09-28 16:19:51 -0500582 case PLANE2_AOI1:
Timur Tabib7795052011-12-08 17:27:31 -0600583 pmfbi = &data->mfb[3];
York Sun9b53a9e2008-04-28 02:15:34 -0700584 if (hw->desc[2] != 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 */
Timur Tabib7795052011-12-08 17:27:31 -0600590 wr_reg_wa(&hw->desc[2], data->dummy_ad.paddr);
York Sun9b53a9e2008-04-28 02:15:34 -0700591 /* close AOI 1 */
592 break;
York Sun9b53a9e2008-04-28 02:15:34 -0700593 }
York Sun9b53a9e2008-04-28 02:15:34 -0700594}
595
596static void enable_lcdc(struct fb_info *info)
597{
York Sun9b53a9e2008-04-28 02:15:34 -0700598 struct mfb_info *mfbi = info->par;
Timur Tabib7795052011-12-08 17:27:31 -0600599 struct fsl_diu_data *data = mfbi->parent;
600 struct diu __iomem *hw = data->diu_reg;
York Sun9b53a9e2008-04-28 02:15:34 -0700601
Timur Tabid397e9162011-12-19 16:26:16 -0600602 out_be32(&hw->diu_mode, MFB_MODE1);
York Sun9b53a9e2008-04-28 02:15:34 -0700603}
604
605static void disable_lcdc(struct fb_info *info)
606{
York Sun9b53a9e2008-04-28 02:15:34 -0700607 struct mfb_info *mfbi = info->par;
Timur Tabib7795052011-12-08 17:27:31 -0600608 struct fsl_diu_data *data = mfbi->parent;
609 struct diu __iomem *hw = data->diu_reg;
York Sun9b53a9e2008-04-28 02:15:34 -0700610
Timur Tabid397e9162011-12-19 16:26:16 -0600611 out_be32(&hw->diu_mode, 0);
York Sun9b53a9e2008-04-28 02:15:34 -0700612}
613
614static void adjust_aoi_size_position(struct fb_var_screeninfo *var,
615 struct fb_info *info)
616{
617 struct mfb_info *lower_aoi_mfbi, *upper_aoi_mfbi, *mfbi = info->par;
Timur Tabib7795052011-12-08 17:27:31 -0600618 struct fsl_diu_data *data = mfbi->parent;
Timur Tabi2572df92011-09-28 16:19:51 -0500619 int available_height, upper_aoi_bottom;
620 enum mfb_index index = mfbi->index;
York Sun9b53a9e2008-04-28 02:15:34 -0700621 int lower_aoi_is_open, upper_aoi_is_open;
622 __u32 base_plane_width, base_plane_height, upper_aoi_height;
623
Timur Tabib7795052011-12-08 17:27:31 -0600624 base_plane_width = data->fsl_diu_info[0].var.xres;
625 base_plane_height = data->fsl_diu_info[0].var.yres;
York Sun9b53a9e2008-04-28 02:15:34 -0700626
York Sunfdfaa48332008-08-15 00:40:29 -0700627 if (mfbi->x_aoi_d < 0)
628 mfbi->x_aoi_d = 0;
629 if (mfbi->y_aoi_d < 0)
630 mfbi->y_aoi_d = 0;
York Sun9b53a9e2008-04-28 02:15:34 -0700631 switch (index) {
Timur Tabi2572df92011-09-28 16:19:51 -0500632 case PLANE0:
York Sun9b53a9e2008-04-28 02:15:34 -0700633 if (mfbi->x_aoi_d != 0)
634 mfbi->x_aoi_d = 0;
635 if (mfbi->y_aoi_d != 0)
636 mfbi->y_aoi_d = 0;
637 break;
Timur Tabi2572df92011-09-28 16:19:51 -0500638 case PLANE1_AOI0:
639 case PLANE2_AOI0:
Timur Tabib7795052011-12-08 17:27:31 -0600640 lower_aoi_mfbi = data->fsl_diu_info[index+1].par;
York Sun9b53a9e2008-04-28 02:15:34 -0700641 lower_aoi_is_open = lower_aoi_mfbi->count > 0 ? 1 : 0;
642 if (var->xres > base_plane_width)
643 var->xres = base_plane_width;
644 if ((mfbi->x_aoi_d + var->xres) > base_plane_width)
645 mfbi->x_aoi_d = base_plane_width - var->xres;
646
647 if (lower_aoi_is_open)
648 available_height = lower_aoi_mfbi->y_aoi_d;
649 else
650 available_height = base_plane_height;
651 if (var->yres > available_height)
652 var->yres = available_height;
653 if ((mfbi->y_aoi_d + var->yres) > available_height)
654 mfbi->y_aoi_d = available_height - var->yres;
655 break;
Timur Tabi2572df92011-09-28 16:19:51 -0500656 case PLANE1_AOI1:
657 case PLANE2_AOI1:
Timur Tabib7795052011-12-08 17:27:31 -0600658 upper_aoi_mfbi = data->fsl_diu_info[index-1].par;
659 upper_aoi_height = data->fsl_diu_info[index-1].var.yres;
York Sun9b53a9e2008-04-28 02:15:34 -0700660 upper_aoi_bottom = upper_aoi_mfbi->y_aoi_d + upper_aoi_height;
661 upper_aoi_is_open = upper_aoi_mfbi->count > 0 ? 1 : 0;
662 if (var->xres > base_plane_width)
663 var->xres = base_plane_width;
664 if ((mfbi->x_aoi_d + var->xres) > base_plane_width)
665 mfbi->x_aoi_d = base_plane_width - var->xres;
666 if (mfbi->y_aoi_d < 0)
667 mfbi->y_aoi_d = 0;
668 if (upper_aoi_is_open) {
669 if (mfbi->y_aoi_d < upper_aoi_bottom)
670 mfbi->y_aoi_d = upper_aoi_bottom;
671 available_height = base_plane_height
672 - upper_aoi_bottom;
673 } else
674 available_height = base_plane_height;
675 if (var->yres > available_height)
676 var->yres = available_height;
677 if ((mfbi->y_aoi_d + var->yres) > base_plane_height)
678 mfbi->y_aoi_d = base_plane_height - var->yres;
679 break;
680 }
681}
682/*
683 * Checks to see if the hardware supports the state requested by var passed
684 * in. This function does not alter the hardware state! If the var passed in
685 * is slightly off by what the hardware can support then we alter the var
686 * PASSED in to what we can do. If the hardware doesn't support mode change
687 * a -EINVAL will be returned by the upper layers.
688 */
689static int fsl_diu_check_var(struct fb_var_screeninfo *var,
690 struct fb_info *info)
691{
York Sun9b53a9e2008-04-28 02:15:34 -0700692 if (var->xres_virtual < var->xres)
693 var->xres_virtual = var->xres;
694 if (var->yres_virtual < var->yres)
695 var->yres_virtual = var->yres;
696
697 if (var->xoffset < 0)
698 var->xoffset = 0;
699
700 if (var->yoffset < 0)
701 var->yoffset = 0;
702
703 if (var->xoffset + info->var.xres > info->var.xres_virtual)
704 var->xoffset = info->var.xres_virtual - info->var.xres;
705
706 if (var->yoffset + info->var.yres > info->var.yres_virtual)
707 var->yoffset = info->var.yres_virtual - info->var.yres;
708
709 if ((var->bits_per_pixel != 32) && (var->bits_per_pixel != 24) &&
710 (var->bits_per_pixel != 16))
711 var->bits_per_pixel = default_bpp;
712
713 switch (var->bits_per_pixel) {
714 case 16:
715 var->red.length = 5;
716 var->red.offset = 11;
717 var->red.msb_right = 0;
718
719 var->green.length = 6;
720 var->green.offset = 5;
721 var->green.msb_right = 0;
722
723 var->blue.length = 5;
724 var->blue.offset = 0;
725 var->blue.msb_right = 0;
726
727 var->transp.length = 0;
728 var->transp.offset = 0;
729 var->transp.msb_right = 0;
730 break;
731 case 24:
732 var->red.length = 8;
733 var->red.offset = 0;
734 var->red.msb_right = 0;
735
736 var->green.length = 8;
737 var->green.offset = 8;
738 var->green.msb_right = 0;
739
740 var->blue.length = 8;
741 var->blue.offset = 16;
742 var->blue.msb_right = 0;
743
744 var->transp.length = 0;
745 var->transp.offset = 0;
746 var->transp.msb_right = 0;
747 break;
748 case 32:
749 var->red.length = 8;
750 var->red.offset = 16;
751 var->red.msb_right = 0;
752
753 var->green.length = 8;
754 var->green.offset = 8;
755 var->green.msb_right = 0;
756
757 var->blue.length = 8;
758 var->blue.offset = 0;
759 var->blue.msb_right = 0;
760
761 var->transp.length = 8;
762 var->transp.offset = 24;
763 var->transp.msb_right = 0;
764
765 break;
766 }
York Sun9b53a9e2008-04-28 02:15:34 -0700767
768 var->height = -1;
769 var->width = -1;
770 var->grayscale = 0;
771
772 /* Copy nonstd field to/from sync for fbset usage */
773 var->sync |= var->nonstd;
774 var->nonstd |= var->sync;
775
776 adjust_aoi_size_position(var, info);
777 return 0;
778}
779
780static void set_fix(struct fb_info *info)
781{
782 struct fb_fix_screeninfo *fix = &info->fix;
783 struct fb_var_screeninfo *var = &info->var;
784 struct mfb_info *mfbi = info->par;
785
Timur Tabiec02dd22011-09-15 16:44:54 -0500786 strncpy(fix->id, mfbi->id, sizeof(fix->id));
York Sun9b53a9e2008-04-28 02:15:34 -0700787 fix->line_length = var->xres_virtual * var->bits_per_pixel / 8;
788 fix->type = FB_TYPE_PACKED_PIXELS;
789 fix->accel = FB_ACCEL_NONE;
790 fix->visual = FB_VISUAL_TRUECOLOR;
791 fix->xpanstep = 1;
792 fix->ypanstep = 1;
793}
794
795static void update_lcdc(struct fb_info *info)
796{
797 struct fb_var_screeninfo *var = &info->var;
798 struct mfb_info *mfbi = info->par;
Timur Tabib7795052011-12-08 17:27:31 -0600799 struct fsl_diu_data *data = mfbi->parent;
Timur Tabi3c755b72011-10-04 19:36:51 -0500800 struct diu __iomem *hw;
York Sun9b53a9e2008-04-28 02:15:34 -0700801 int i, j;
Timur Tabiddd3d902011-12-02 17:03:27 -0600802 u8 *gamma_table_base;
York Sun9b53a9e2008-04-28 02:15:34 -0700803
804 u32 temp;
805
Timur Tabib7795052011-12-08 17:27:31 -0600806 hw = data->diu_reg;
York Sun9b53a9e2008-04-28 02:15:34 -0700807
Timur Tabi28671732012-10-16 17:33:43 -0500808 if (diu_ops.set_monitor_port)
809 diu_ops.set_monitor_port(data->monitor_port);
Timur Tabib7795052011-12-08 17:27:31 -0600810 gamma_table_base = data->gamma;
Timur Tabiddd3d902011-12-02 17:03:27 -0600811
York Sun9b53a9e2008-04-28 02:15:34 -0700812 /* Prep for DIU init - gamma table, cursor table */
813
814 for (i = 0; i <= 2; i++)
Timur Tabi4a85dc8b2011-09-15 16:44:46 -0500815 for (j = 0; j <= 255; j++)
816 *gamma_table_base++ = j;
York Sun9b53a9e2008-04-28 02:15:34 -0700817
Timur Tabie09a8c32011-12-19 16:26:17 -0600818 if (diu_ops.set_gamma_table)
819 diu_ops.set_gamma_table(data->monitor_port, data->gamma);
York Sun9b53a9e2008-04-28 02:15:34 -0700820
York Sun9b53a9e2008-04-28 02:15:34 -0700821 disable_lcdc(info);
822
823 /* Program DIU registers */
824
Timur Tabib7795052011-12-08 17:27:31 -0600825 out_be32(&hw->gamma, DMA_ADDR(data, gamma));
826 out_be32(&hw->cursor, DMA_ADDR(data, cursor));
York Sun9b53a9e2008-04-28 02:15:34 -0700827
828 out_be32(&hw->bgnd, 0x007F7F7F); /* BGND */
829 out_be32(&hw->bgnd_wb, 0); /* BGND_WB */
830 out_be32(&hw->disp_size, (var->yres << 16 | var->xres));
831 /* DISP SIZE */
York Sun9b53a9e2008-04-28 02:15:34 -0700832 out_be32(&hw->wb_size, 0); /* WB SIZE */
833 out_be32(&hw->wb_mem_addr, 0); /* WB MEM ADDR */
834
835 /* Horizontal and vertical configuration register */
836 temp = var->left_margin << 22 | /* BP_H */
837 var->hsync_len << 11 | /* PW_H */
838 var->right_margin; /* FP_H */
839
840 out_be32(&hw->hsyn_para, temp);
841
842 temp = var->upper_margin << 22 | /* BP_V */
843 var->vsync_len << 11 | /* PW_V */
844 var->lower_margin; /* FP_V */
845
846 out_be32(&hw->vsyn_para, temp);
847
York Sun9b53a9e2008-04-28 02:15:34 -0700848 diu_ops.set_pixel_clock(var->pixclock);
849
850 out_be32(&hw->syn_pol, 0); /* SYNC SIGNALS POLARITY */
York Sun9b53a9e2008-04-28 02:15:34 -0700851 out_be32(&hw->int_status, 0); /* INTERRUPT STATUS */
852 out_be32(&hw->plut, 0x01F5F666);
853
854 /* Enable the DIU */
855 enable_lcdc(info);
856}
857
858static int map_video_memory(struct fb_info *info)
859{
Krzysztof Helt537a1bf2009-06-30 11:41:29 -0700860 u32 smem_len = info->fix.line_length * info->var.yres_virtual;
Timur Tabi05342c02011-12-19 16:26:18 -0600861 void *p;
York Sun9b53a9e2008-04-28 02:15:34 -0700862
Timur Tabi05342c02011-12-19 16:26:18 -0600863 p = alloc_pages_exact(smem_len, GFP_DMA | __GFP_ZERO);
864 if (!p) {
Timur Tabi154152a2011-09-15 16:44:47 -0500865 dev_err(info->dev, "unable to allocate fb memory\n");
York Sun9b53a9e2008-04-28 02:15:34 -0700866 return -ENOMEM;
867 }
Krzysztof Helt537a1bf2009-06-30 11:41:29 -0700868 mutex_lock(&info->mm_lock);
Timur Tabi05342c02011-12-19 16:26:18 -0600869 info->screen_base = p;
870 info->fix.smem_start = virt_to_phys(info->screen_base);
Krzysztof Helt537a1bf2009-06-30 11:41:29 -0700871 info->fix.smem_len = smem_len;
872 mutex_unlock(&info->mm_lock);
York Sun9b53a9e2008-04-28 02:15:34 -0700873 info->screen_size = info->fix.smem_len;
874
York Sun9b53a9e2008-04-28 02:15:34 -0700875 return 0;
876}
877
878static void unmap_video_memory(struct fb_info *info)
879{
Timur Tabi05342c02011-12-19 16:26:18 -0600880 void *p = info->screen_base;
881 size_t l = info->fix.smem_len;
882
Krzysztof Helt537a1bf2009-06-30 11:41:29 -0700883 mutex_lock(&info->mm_lock);
Anton Vorontsov05946bc2008-07-04 09:59:38 -0700884 info->screen_base = NULL;
York Sun9b53a9e2008-04-28 02:15:34 -0700885 info->fix.smem_start = 0;
886 info->fix.smem_len = 0;
Krzysztof Helt537a1bf2009-06-30 11:41:29 -0700887 mutex_unlock(&info->mm_lock);
Timur Tabi05342c02011-12-19 16:26:18 -0600888
889 if (p)
890 free_pages_exact(p, l);
York Sun9b53a9e2008-04-28 02:15:34 -0700891}
892
893/*
York Sunae5591e2008-08-15 00:40:28 -0700894 * Using the fb_var_screeninfo in fb_info we set the aoi of this
895 * particular framebuffer. It is a light version of fsl_diu_set_par.
896 */
897static int fsl_diu_set_aoi(struct fb_info *info)
898{
899 struct fb_var_screeninfo *var = &info->var;
900 struct mfb_info *mfbi = info->par;
901 struct diu_ad *ad = mfbi->ad;
902
903 /* AOI should not be greater than display size */
904 ad->offset_xyi = cpu_to_le32((var->yoffset << 16) | var->xoffset);
905 ad->offset_xyd = cpu_to_le32((mfbi->y_aoi_d << 16) | mfbi->x_aoi_d);
906 return 0;
907}
908
Timur Tabie09a8c32011-12-19 16:26:17 -0600909/**
910 * fsl_diu_get_pixel_format: return the pixel format for a given color depth
911 *
912 * The pixel format is a 32-bit value that determine which bits in each
913 * pixel are to be used for each color. This is the default function used
914 * if the platform does not define its own version.
915 */
916static u32 fsl_diu_get_pixel_format(unsigned int bits_per_pixel)
917{
918#define PF_BYTE_F 0x10000000
919#define PF_ALPHA_C_MASK 0x0E000000
920#define PF_ALPHA_C_SHIFT 25
921#define PF_BLUE_C_MASK 0x01800000
922#define PF_BLUE_C_SHIFT 23
923#define PF_GREEN_C_MASK 0x00600000
924#define PF_GREEN_C_SHIFT 21
925#define PF_RED_C_MASK 0x00180000
926#define PF_RED_C_SHIFT 19
927#define PF_PALETTE 0x00040000
928#define PF_PIXEL_S_MASK 0x00030000
929#define PF_PIXEL_S_SHIFT 16
930#define PF_COMP_3_MASK 0x0000F000
931#define PF_COMP_3_SHIFT 12
932#define PF_COMP_2_MASK 0x00000F00
933#define PF_COMP_2_SHIFT 8
934#define PF_COMP_1_MASK 0x000000F0
935#define PF_COMP_1_SHIFT 4
936#define PF_COMP_0_MASK 0x0000000F
937#define PF_COMP_0_SHIFT 0
938
939#define MAKE_PF(alpha, red, blue, green, size, c0, c1, c2, c3) \
940 cpu_to_le32(PF_BYTE_F | (alpha << PF_ALPHA_C_SHIFT) | \
941 (blue << PF_BLUE_C_SHIFT) | (green << PF_GREEN_C_SHIFT) | \
942 (red << PF_RED_C_SHIFT) | (c3 << PF_COMP_3_SHIFT) | \
943 (c2 << PF_COMP_2_SHIFT) | (c1 << PF_COMP_1_SHIFT) | \
944 (c0 << PF_COMP_0_SHIFT) | (size << PF_PIXEL_S_SHIFT))
945
946 switch (bits_per_pixel) {
947 case 32:
948 /* 0x88883316 */
949 return MAKE_PF(3, 2, 0, 1, 3, 8, 8, 8, 8);
950 case 24:
951 /* 0x88082219 */
952 return MAKE_PF(4, 0, 1, 2, 2, 0, 8, 8, 8);
953 case 16:
954 /* 0x65053118 */
955 return MAKE_PF(4, 2, 1, 0, 1, 5, 6, 5, 0);
956 default:
957 pr_err("fsl-diu: unsupported color depth %u\n", bits_per_pixel);
958 return 0;
959 }
960}
961
York Sunae5591e2008-08-15 00:40:28 -0700962/*
York Sun9b53a9e2008-04-28 02:15:34 -0700963 * Using the fb_var_screeninfo in fb_info we set the resolution of this
964 * particular framebuffer. This function alters the fb_fix_screeninfo stored
965 * in fb_info. It does not alter var in fb_info since we are using that
966 * data. This means we depend on the data in var inside fb_info to be
967 * supported by the hardware. fsl_diu_check_var is always called before
968 * fsl_diu_set_par to ensure this.
969 */
970static int fsl_diu_set_par(struct fb_info *info)
971{
972 unsigned long len;
973 struct fb_var_screeninfo *var = &info->var;
974 struct mfb_info *mfbi = info->par;
Timur Tabib7795052011-12-08 17:27:31 -0600975 struct fsl_diu_data *data = mfbi->parent;
York Sun9b53a9e2008-04-28 02:15:34 -0700976 struct diu_ad *ad = mfbi->ad;
Timur Tabi3c755b72011-10-04 19:36:51 -0500977 struct diu __iomem *hw;
York Sun9b53a9e2008-04-28 02:15:34 -0700978
Timur Tabib7795052011-12-08 17:27:31 -0600979 hw = data->diu_reg;
York Sun9b53a9e2008-04-28 02:15:34 -0700980
981 set_fix(info);
York Sun9b53a9e2008-04-28 02:15:34 -0700982
983 len = info->var.yres_virtual * info->fix.line_length;
984 /* Alloc & dealloc each time resolution/bpp change */
985 if (len != info->fix.smem_len) {
986 if (info->fix.smem_start)
987 unmap_video_memory(info);
York Sun9b53a9e2008-04-28 02:15:34 -0700988
989 /* Memory allocation for framebuffer */
990 if (map_video_memory(info)) {
Timur Tabi154152a2011-09-15 16:44:47 -0500991 dev_err(info->dev, "unable to allocate fb memory 1\n");
York Sun9b53a9e2008-04-28 02:15:34 -0700992 return -ENOMEM;
993 }
994 }
995
Timur Tabie09a8c32011-12-19 16:26:17 -0600996 if (diu_ops.get_pixel_format)
997 ad->pix_fmt = diu_ops.get_pixel_format(data->monitor_port,
998 var->bits_per_pixel);
999 else
1000 ad->pix_fmt = fsl_diu_get_pixel_format(var->bits_per_pixel);
1001
York Sun9b53a9e2008-04-28 02:15:34 -07001002 ad->addr = cpu_to_le32(info->fix.smem_start);
York Sunae5591e2008-08-15 00:40:28 -07001003 ad->src_size_g_alpha = cpu_to_le32((var->yres_virtual << 12) |
1004 var->xres_virtual) | mfbi->g_alpha;
1005 /* AOI should not be greater than display size */
York Sun9b53a9e2008-04-28 02:15:34 -07001006 ad->aoi_size = cpu_to_le32((var->yres << 16) | var->xres);
York Sunae5591e2008-08-15 00:40:28 -07001007 ad->offset_xyi = cpu_to_le32((var->yoffset << 16) | var->xoffset);
York Sun9b53a9e2008-04-28 02:15:34 -07001008 ad->offset_xyd = cpu_to_le32((mfbi->y_aoi_d << 16) | mfbi->x_aoi_d);
1009
1010 /* Disable chroma keying function */
1011 ad->ckmax_r = 0;
1012 ad->ckmax_g = 0;
1013 ad->ckmax_b = 0;
1014
1015 ad->ckmin_r = 255;
1016 ad->ckmin_g = 255;
1017 ad->ckmin_b = 255;
1018
Timur Tabi2572df92011-09-28 16:19:51 -05001019 if (mfbi->index == PLANE0)
York Sun9b53a9e2008-04-28 02:15:34 -07001020 update_lcdc(info);
1021 return 0;
1022}
1023
1024static inline __u32 CNVT_TOHW(__u32 val, __u32 width)
1025{
Timur Tabi4a85dc8b2011-09-15 16:44:46 -05001026 return ((val << width) + 0x7FFF - val) >> 16;
York Sun9b53a9e2008-04-28 02:15:34 -07001027}
1028
1029/*
1030 * Set a single color register. The values supplied have a 16 bit magnitude
1031 * which needs to be scaled in this function for the hardware. Things to take
1032 * into consideration are how many color registers, if any, are supported with
1033 * the current color visual. With truecolor mode no color palettes are
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001034 * supported. Here a pseudo palette is created which we store the value in
York Sun9b53a9e2008-04-28 02:15:34 -07001035 * pseudo_palette in struct fb_info. For pseudocolor mode we have a limited
1036 * color palette.
1037 */
Timur Tabi4a85dc8b2011-09-15 16:44:46 -05001038static int fsl_diu_setcolreg(unsigned int regno, unsigned int red,
1039 unsigned int green, unsigned int blue,
1040 unsigned int transp, struct fb_info *info)
York Sun9b53a9e2008-04-28 02:15:34 -07001041{
1042 int ret = 1;
1043
1044 /*
1045 * If greyscale is true, then we convert the RGB value
1046 * to greyscale no matter what visual we are using.
1047 */
1048 if (info->var.grayscale)
1049 red = green = blue = (19595 * red + 38470 * green +
1050 7471 * blue) >> 16;
1051 switch (info->fix.visual) {
1052 case FB_VISUAL_TRUECOLOR:
1053 /*
1054 * 16-bit True Colour. We encode the RGB value
1055 * according to the RGB bitfield information.
1056 */
1057 if (regno < 16) {
1058 u32 *pal = info->pseudo_palette;
1059 u32 v;
1060
1061 red = CNVT_TOHW(red, info->var.red.length);
1062 green = CNVT_TOHW(green, info->var.green.length);
1063 blue = CNVT_TOHW(blue, info->var.blue.length);
1064 transp = CNVT_TOHW(transp, info->var.transp.length);
1065
1066 v = (red << info->var.red.offset) |
1067 (green << info->var.green.offset) |
1068 (blue << info->var.blue.offset) |
1069 (transp << info->var.transp.offset);
1070
1071 pal[regno] = v;
1072 ret = 0;
1073 }
1074 break;
York Sun9b53a9e2008-04-28 02:15:34 -07001075 }
1076
1077 return ret;
1078}
1079
1080/*
1081 * Pan (or wrap, depending on the `vmode' field) the display using the
1082 * 'xoffset' and 'yoffset' fields of the 'var' structure. If the values
1083 * don't fit, return -EINVAL.
1084 */
1085static int fsl_diu_pan_display(struct fb_var_screeninfo *var,
1086 struct fb_info *info)
1087{
1088 if ((info->var.xoffset == var->xoffset) &&
1089 (info->var.yoffset == var->yoffset))
1090 return 0; /* No change, do nothing */
1091
1092 if (var->xoffset < 0 || var->yoffset < 0
1093 || var->xoffset + info->var.xres > info->var.xres_virtual
1094 || var->yoffset + info->var.yres > info->var.yres_virtual)
1095 return -EINVAL;
1096
1097 info->var.xoffset = var->xoffset;
1098 info->var.yoffset = var->yoffset;
1099
1100 if (var->vmode & FB_VMODE_YWRAP)
1101 info->var.vmode |= FB_VMODE_YWRAP;
1102 else
1103 info->var.vmode &= ~FB_VMODE_YWRAP;
1104
York Sunae5591e2008-08-15 00:40:28 -07001105 fsl_diu_set_aoi(info);
1106
York Sun9b53a9e2008-04-28 02:15:34 -07001107 return 0;
1108}
1109
York Sun9b53a9e2008-04-28 02:15:34 -07001110static int fsl_diu_ioctl(struct fb_info *info, unsigned int cmd,
1111 unsigned long arg)
1112{
1113 struct mfb_info *mfbi = info->par;
1114 struct diu_ad *ad = mfbi->ad;
1115 struct mfb_chroma_key ck;
1116 unsigned char global_alpha;
1117 struct aoi_display_offset aoi_d;
1118 __u32 pix_fmt;
1119 void __user *buf = (void __user *)arg;
1120
1121 if (!arg)
1122 return -EINVAL;
Timur Tabi5cc2a362012-10-16 17:33:46 -05001123
1124 dev_dbg(info->dev, "ioctl %08x (dir=%s%s type=%u nr=%u size=%u)\n", cmd,
1125 _IOC_DIR(cmd) & _IOC_READ ? "R" : "",
1126 _IOC_DIR(cmd) & _IOC_WRITE ? "W" : "",
1127 _IOC_TYPE(cmd), _IOC_NR(cmd), _IOC_SIZE(cmd));
1128
York Sun9b53a9e2008-04-28 02:15:34 -07001129 switch (cmd) {
Timur Tabi36b0b1d2011-10-04 19:36:44 -05001130 case MFB_SET_PIXFMT_OLD:
1131 dev_warn(info->dev,
1132 "MFB_SET_PIXFMT value of 0x%08x is deprecated.\n",
1133 MFB_SET_PIXFMT_OLD);
York Sun9b53a9e2008-04-28 02:15:34 -07001134 case MFB_SET_PIXFMT:
1135 if (copy_from_user(&pix_fmt, buf, sizeof(pix_fmt)))
1136 return -EFAULT;
1137 ad->pix_fmt = pix_fmt;
York Sun9b53a9e2008-04-28 02:15:34 -07001138 break;
Timur Tabi36b0b1d2011-10-04 19:36:44 -05001139 case MFB_GET_PIXFMT_OLD:
1140 dev_warn(info->dev,
1141 "MFB_GET_PIXFMT value of 0x%08x is deprecated.\n",
1142 MFB_GET_PIXFMT_OLD);
York Sun9b53a9e2008-04-28 02:15:34 -07001143 case MFB_GET_PIXFMT:
1144 pix_fmt = ad->pix_fmt;
1145 if (copy_to_user(buf, &pix_fmt, sizeof(pix_fmt)))
1146 return -EFAULT;
York Sun9b53a9e2008-04-28 02:15:34 -07001147 break;
1148 case MFB_SET_AOID:
1149 if (copy_from_user(&aoi_d, buf, sizeof(aoi_d)))
1150 return -EFAULT;
1151 mfbi->x_aoi_d = aoi_d.x_aoi_d;
1152 mfbi->y_aoi_d = aoi_d.y_aoi_d;
York Sun9b53a9e2008-04-28 02:15:34 -07001153 fsl_diu_check_var(&info->var, info);
York Sunae5591e2008-08-15 00:40:28 -07001154 fsl_diu_set_aoi(info);
York Sun9b53a9e2008-04-28 02:15:34 -07001155 break;
1156 case MFB_GET_AOID:
1157 aoi_d.x_aoi_d = mfbi->x_aoi_d;
1158 aoi_d.y_aoi_d = mfbi->y_aoi_d;
1159 if (copy_to_user(buf, &aoi_d, sizeof(aoi_d)))
1160 return -EFAULT;
York Sun9b53a9e2008-04-28 02:15:34 -07001161 break;
1162 case MFB_GET_ALPHA:
1163 global_alpha = mfbi->g_alpha;
1164 if (copy_to_user(buf, &global_alpha, sizeof(global_alpha)))
1165 return -EFAULT;
York Sun9b53a9e2008-04-28 02:15:34 -07001166 break;
1167 case MFB_SET_ALPHA:
1168 /* set panel information */
1169 if (copy_from_user(&global_alpha, buf, sizeof(global_alpha)))
1170 return -EFAULT;
1171 ad->src_size_g_alpha = (ad->src_size_g_alpha & (~0xff)) |
1172 (global_alpha & 0xff);
1173 mfbi->g_alpha = global_alpha;
York Sun9b53a9e2008-04-28 02:15:34 -07001174 break;
1175 case MFB_SET_CHROMA_KEY:
1176 /* set panel winformation */
1177 if (copy_from_user(&ck, buf, sizeof(ck)))
1178 return -EFAULT;
1179
1180 if (ck.enable &&
1181 (ck.red_max < ck.red_min ||
1182 ck.green_max < ck.green_min ||
1183 ck.blue_max < ck.blue_min))
1184 return -EINVAL;
1185
1186 if (!ck.enable) {
1187 ad->ckmax_r = 0;
1188 ad->ckmax_g = 0;
1189 ad->ckmax_b = 0;
1190 ad->ckmin_r = 255;
1191 ad->ckmin_g = 255;
1192 ad->ckmin_b = 255;
1193 } else {
1194 ad->ckmax_r = ck.red_max;
1195 ad->ckmax_g = ck.green_max;
1196 ad->ckmax_b = ck.blue_max;
1197 ad->ckmin_r = ck.red_min;
1198 ad->ckmin_g = ck.green_min;
1199 ad->ckmin_b = ck.blue_min;
1200 }
York Sun9b53a9e2008-04-28 02:15:34 -07001201 break;
Timur Tabie95c17e2012-10-16 17:33:44 -05001202#ifdef CONFIG_PPC_MPC512x
1203 case MFB_SET_GAMMA: {
1204 struct fsl_diu_data *data = mfbi->parent;
1205
1206 if (copy_from_user(data->gamma, buf, sizeof(data->gamma)))
1207 return -EFAULT;
1208 setbits32(&data->diu_reg->gamma, 0); /* Force table reload */
1209 break;
1210 }
1211 case MFB_GET_GAMMA: {
1212 struct fsl_diu_data *data = mfbi->parent;
1213
1214 if (copy_to_user(buf, data->gamma, sizeof(data->gamma)))
1215 return -EFAULT;
1216 break;
1217 }
1218#endif
York Sun9b53a9e2008-04-28 02:15:34 -07001219 default:
Timur Tabi154152a2011-09-15 16:44:47 -05001220 dev_err(info->dev, "unknown ioctl command (0x%08X)\n", cmd);
York Sun9b53a9e2008-04-28 02:15:34 -07001221 return -ENOIOCTLCMD;
1222 }
1223
1224 return 0;
1225}
1226
1227/* turn on fb if count == 1
1228 */
1229static int fsl_diu_open(struct fb_info *info, int user)
1230{
1231 struct mfb_info *mfbi = info->par;
1232 int res = 0;
1233
Anatolij Gustschin4b5006e2010-07-23 04:00:37 +00001234 /* free boot splash memory on first /dev/fb0 open */
Timur Tabi2572df92011-09-28 16:19:51 -05001235 if ((mfbi->index == PLANE0) && diu_ops.release_bootmem)
Anatolij Gustschin4b5006e2010-07-23 04:00:37 +00001236 diu_ops.release_bootmem();
1237
York Sun9b53a9e2008-04-28 02:15:34 -07001238 spin_lock(&diu_lock);
1239 mfbi->count++;
1240 if (mfbi->count == 1) {
York Sun9b53a9e2008-04-28 02:15:34 -07001241 fsl_diu_check_var(&info->var, info);
1242 res = fsl_diu_set_par(info);
1243 if (res < 0)
1244 mfbi->count--;
Timur Tabif74de502012-10-16 17:33:45 -05001245 else {
1246 struct fsl_diu_data *data = mfbi->parent;
1247
1248#ifdef CONFIG_NOT_COHERENT_CACHE
1249 /*
1250 * Enable underrun detection and vertical sync
1251 * interrupts.
1252 */
1253 clrbits32(&data->diu_reg->int_mask,
1254 INT_UNDRUN | INT_VSYNC);
1255#else
1256 /* Enable underrun detection */
1257 clrbits32(&data->diu_reg->int_mask, INT_UNDRUN);
1258#endif
Timur Tabi7e47c212011-09-28 16:19:52 -05001259 fsl_diu_enable_panel(info);
Timur Tabif74de502012-10-16 17:33:45 -05001260 }
York Sun9b53a9e2008-04-28 02:15:34 -07001261 }
1262
1263 spin_unlock(&diu_lock);
1264 return res;
1265}
1266
1267/* turn off fb if count == 0
1268 */
1269static int fsl_diu_release(struct fb_info *info, int user)
1270{
1271 struct mfb_info *mfbi = info->par;
1272 int res = 0;
1273
1274 spin_lock(&diu_lock);
1275 mfbi->count--;
Timur Tabif74de502012-10-16 17:33:45 -05001276 if (mfbi->count == 0) {
1277 struct fsl_diu_data *data = mfbi->parent;
1278
1279 /* Disable interrupts */
1280 out_be32(&data->diu_reg->int_mask, 0xffffffff);
Timur Tabi2572df92011-09-28 16:19:51 -05001281 fsl_diu_disable_panel(info);
Timur Tabif74de502012-10-16 17:33:45 -05001282 }
Timur Tabi2572df92011-09-28 16:19:51 -05001283
York Sun9b53a9e2008-04-28 02:15:34 -07001284 spin_unlock(&diu_lock);
1285 return res;
1286}
1287
1288static struct fb_ops fsl_diu_ops = {
1289 .owner = THIS_MODULE,
1290 .fb_check_var = fsl_diu_check_var,
1291 .fb_set_par = fsl_diu_set_par,
1292 .fb_setcolreg = fsl_diu_setcolreg,
York Sun9b53a9e2008-04-28 02:15:34 -07001293 .fb_pan_display = fsl_diu_pan_display,
1294 .fb_fillrect = cfb_fillrect,
1295 .fb_copyarea = cfb_copyarea,
1296 .fb_imageblit = cfb_imageblit,
1297 .fb_ioctl = fsl_diu_ioctl,
1298 .fb_open = fsl_diu_open,
1299 .fb_release = fsl_diu_release,
1300};
1301
Anton Vorontsov05946bc2008-07-04 09:59:38 -07001302static int __devinit install_fb(struct fb_info *info)
York Sun9b53a9e2008-04-28 02:15:34 -07001303{
1304 int rc;
1305 struct mfb_info *mfbi = info->par;
1306 const char *aoi_mode, *init_aoi_mode = "320x240";
Anatolij Gustschin8b856f02010-07-23 04:00:39 +00001307 struct fb_videomode *db = fsl_diu_mode_db;
1308 unsigned int dbsize = ARRAY_SIZE(fsl_diu_mode_db);
1309 int has_default_mode = 1;
York Sun9b53a9e2008-04-28 02:15:34 -07001310
Timur Tabif934fbd2011-12-19 16:26:14 -06001311 info->var.activate = FB_ACTIVATE_NOW;
1312 info->fbops = &fsl_diu_ops;
Timur Tabi934dbee2011-12-19 16:26:15 -06001313 info->flags = FBINFO_DEFAULT | FBINFO_VIRTFB | FBINFO_PARTIAL_PAN_OK |
1314 FBINFO_READS_FAST;
Timur Tabif934fbd2011-12-19 16:26:14 -06001315 info->pseudo_palette = mfbi->pseudo_palette;
1316
1317 rc = fb_alloc_cmap(&info->cmap, 16, 0);
1318 if (rc)
1319 return rc;
York Sun9b53a9e2008-04-28 02:15:34 -07001320
Timur Tabi2572df92011-09-28 16:19:51 -05001321 if (mfbi->index == PLANE0) {
Anatolij Gustschin8b856f02010-07-23 04:00:39 +00001322 if (mfbi->edid_data) {
1323 /* Now build modedb from EDID */
1324 fb_edid_to_monspecs(mfbi->edid_data, &info->monspecs);
1325 fb_videomode_to_modelist(info->monspecs.modedb,
1326 info->monspecs.modedb_len,
1327 &info->modelist);
1328 db = info->monspecs.modedb;
1329 dbsize = info->monspecs.modedb_len;
1330 }
York Sun9b53a9e2008-04-28 02:15:34 -07001331 aoi_mode = fb_mode;
Anatolij Gustschin8b856f02010-07-23 04:00:39 +00001332 } else {
York Sun9b53a9e2008-04-28 02:15:34 -07001333 aoi_mode = init_aoi_mode;
Anatolij Gustschin8b856f02010-07-23 04:00:39 +00001334 }
Timur Tabi63cf8df2011-09-15 16:44:51 -05001335 rc = fb_find_mode(&info->var, info, aoi_mode, db, dbsize, NULL,
1336 default_bpp);
Timur Tabi154152a2011-09-15 16:44:47 -05001337 if (!rc) {
Anatolij Gustschin8b856f02010-07-23 04:00:39 +00001338 /*
1339 * For plane 0 we continue and look into
1340 * driver's internal modedb.
1341 */
Timur Tabi2572df92011-09-28 16:19:51 -05001342 if ((mfbi->index == PLANE0) && mfbi->edid_data)
Anatolij Gustschin8b856f02010-07-23 04:00:39 +00001343 has_default_mode = 0;
1344 else
1345 return -EINVAL;
York Sun9b53a9e2008-04-28 02:15:34 -07001346 }
1347
Anatolij Gustschin8b856f02010-07-23 04:00:39 +00001348 if (!has_default_mode) {
1349 rc = fb_find_mode(&info->var, info, aoi_mode, fsl_diu_mode_db,
Timur Tabi63cf8df2011-09-15 16:44:51 -05001350 ARRAY_SIZE(fsl_diu_mode_db), NULL, default_bpp);
1351 if (rc)
Anatolij Gustschin8b856f02010-07-23 04:00:39 +00001352 has_default_mode = 1;
1353 }
1354
1355 /* Still not found, use preferred mode from database if any */
1356 if (!has_default_mode && info->monspecs.modedb) {
1357 struct fb_monspecs *specs = &info->monspecs;
1358 struct fb_videomode *modedb = &specs->modedb[0];
1359
1360 /*
1361 * Get preferred timing. If not found,
1362 * first mode in database will be used.
1363 */
1364 if (specs->misc & FB_MISC_1ST_DETAIL) {
1365 int i;
1366
1367 for (i = 0; i < specs->modedb_len; i++) {
1368 if (specs->modedb[i].flag & FB_MODE_IS_FIRST) {
1369 modedb = &specs->modedb[i];
1370 break;
1371 }
1372 }
1373 }
1374
1375 info->var.bits_per_pixel = default_bpp;
1376 fb_videomode_to_var(&info->var, modedb);
1377 }
1378
York Sun9b53a9e2008-04-28 02:15:34 -07001379 if (fsl_diu_check_var(&info->var, info)) {
Timur Tabi154152a2011-09-15 16:44:47 -05001380 dev_err(info->dev, "fsl_diu_check_var failed\n");
Timur Tabi589c7972011-09-15 16:44:55 -05001381 unmap_video_memory(info);
York Sun9b53a9e2008-04-28 02:15:34 -07001382 fb_dealloc_cmap(&info->cmap);
1383 return -EINVAL;
1384 }
1385
York Sun9b53a9e2008-04-28 02:15:34 -07001386 if (register_framebuffer(info) < 0) {
Timur Tabi154152a2011-09-15 16:44:47 -05001387 dev_err(info->dev, "register_framebuffer failed\n");
York Sun9b53a9e2008-04-28 02:15:34 -07001388 unmap_video_memory(info);
1389 fb_dealloc_cmap(&info->cmap);
1390 return -EINVAL;
1391 }
1392
1393 mfbi->registered = 1;
Timur Tabi154152a2011-09-15 16:44:47 -05001394 dev_info(info->dev, "%s registered successfully\n", mfbi->id);
York Sun9b53a9e2008-04-28 02:15:34 -07001395
1396 return 0;
1397}
1398
Anton Vorontsov05946bc2008-07-04 09:59:38 -07001399static void uninstall_fb(struct fb_info *info)
York Sun9b53a9e2008-04-28 02:15:34 -07001400{
1401 struct mfb_info *mfbi = info->par;
1402
1403 if (!mfbi->registered)
1404 return;
1405
Timur Tabi2572df92011-09-28 16:19:51 -05001406 if (mfbi->index == PLANE0)
Anatolij Gustschin8b856f02010-07-23 04:00:39 +00001407 kfree(mfbi->edid_data);
1408
York Sun9b53a9e2008-04-28 02:15:34 -07001409 unregister_framebuffer(info);
1410 unmap_video_memory(info);
1411 if (&info->cmap)
1412 fb_dealloc_cmap(&info->cmap);
1413
1414 mfbi->registered = 0;
1415}
1416
1417static irqreturn_t fsl_diu_isr(int irq, void *dev_id)
1418{
Timur Tabi3c755b72011-10-04 19:36:51 -05001419 struct diu __iomem *hw = dev_id;
Timur Tabif74de502012-10-16 17:33:45 -05001420 uint32_t status = in_be32(&hw->int_status);
York Sun9b53a9e2008-04-28 02:15:34 -07001421
1422 if (status) {
1423 /* This is the workaround for underrun */
1424 if (status & INT_UNDRUN) {
1425 out_be32(&hw->diu_mode, 0);
York Sun9b53a9e2008-04-28 02:15:34 -07001426 udelay(1);
1427 out_be32(&hw->diu_mode, 1);
1428 }
1429#if defined(CONFIG_NOT_COHERENT_CACHE)
1430 else if (status & INT_VSYNC) {
1431 unsigned int i;
Timur Tabi4a85dc8b2011-09-15 16:44:46 -05001432
York Sun9b53a9e2008-04-28 02:15:34 -07001433 for (i = 0; i < coherence_data_size;
1434 i += d_cache_line_size)
1435 __asm__ __volatile__ (
1436 "dcbz 0, %[input]"
1437 ::[input]"r"(&coherence_data[i]));
1438 }
1439#endif
1440 return IRQ_HANDLED;
1441 }
1442 return IRQ_NONE;
1443}
1444
York Sun9b53a9e2008-04-28 02:15:34 -07001445#ifdef CONFIG_PM
1446/*
1447 * Power management hooks. Note that we won't be called from IRQ context,
1448 * unlike the blank functions above, so we may sleep.
1449 */
Grant Likely2dc11582010-08-06 09:25:50 -06001450static int fsl_diu_suspend(struct platform_device *ofdev, pm_message_t state)
York Sun9b53a9e2008-04-28 02:15:34 -07001451{
Timur Tabib7795052011-12-08 17:27:31 -06001452 struct fsl_diu_data *data;
York Sun9b53a9e2008-04-28 02:15:34 -07001453
Timur Tabib7795052011-12-08 17:27:31 -06001454 data = dev_get_drvdata(&ofdev->dev);
Michael Neulingf7f5ef02012-01-28 19:35:24 +00001455 disable_lcdc(data->fsl_diu_info);
York Sun9b53a9e2008-04-28 02:15:34 -07001456
1457 return 0;
1458}
1459
Grant Likely2dc11582010-08-06 09:25:50 -06001460static int fsl_diu_resume(struct platform_device *ofdev)
York Sun9b53a9e2008-04-28 02:15:34 -07001461{
Timur Tabib7795052011-12-08 17:27:31 -06001462 struct fsl_diu_data *data;
York Sun9b53a9e2008-04-28 02:15:34 -07001463
Timur Tabib7795052011-12-08 17:27:31 -06001464 data = dev_get_drvdata(&ofdev->dev);
Michael Neulingf7f5ef02012-01-28 19:35:24 +00001465 enable_lcdc(data->fsl_diu_info);
York Sun9b53a9e2008-04-28 02:15:34 -07001466
1467 return 0;
1468}
1469
1470#else
1471#define fsl_diu_suspend NULL
1472#define fsl_diu_resume NULL
1473#endif /* CONFIG_PM */
1474
York Sun9b53a9e2008-04-28 02:15:34 -07001475static ssize_t store_monitor(struct device *device,
1476 struct device_attribute *attr, const char *buf, size_t count)
1477{
Timur Tabi7653aaa2011-07-09 15:38:14 -05001478 enum fsl_diu_monitor_port old_monitor_port;
Timur Tabib7795052011-12-08 17:27:31 -06001479 struct fsl_diu_data *data =
York Sun9b53a9e2008-04-28 02:15:34 -07001480 container_of(attr, struct fsl_diu_data, dev_attr);
1481
Timur Tabib7795052011-12-08 17:27:31 -06001482 old_monitor_port = data->monitor_port;
1483 data->monitor_port = fsl_diu_name_to_port(buf);
York Sun9b53a9e2008-04-28 02:15:34 -07001484
Timur Tabib7795052011-12-08 17:27:31 -06001485 if (old_monitor_port != data->monitor_port) {
York Sun9b53a9e2008-04-28 02:15:34 -07001486 /* All AOIs need adjust pixel format
1487 * fsl_diu_set_par only change the pixsel format here
1488 * unlikely to fail. */
Timur Tabiddd3d902011-12-02 17:03:27 -06001489 unsigned int i;
1490
1491 for (i=0; i < NUM_AOIS; i++)
Timur Tabib7795052011-12-08 17:27:31 -06001492 fsl_diu_set_par(&data->fsl_diu_info[i]);
York Sun9b53a9e2008-04-28 02:15:34 -07001493 }
1494 return count;
1495}
1496
1497static ssize_t show_monitor(struct device *device,
1498 struct device_attribute *attr, char *buf)
1499{
Timur Tabib7795052011-12-08 17:27:31 -06001500 struct fsl_diu_data *data =
York Sun9b53a9e2008-04-28 02:15:34 -07001501 container_of(attr, struct fsl_diu_data, dev_attr);
Timur Tabi7653aaa2011-07-09 15:38:14 -05001502
Timur Tabib7795052011-12-08 17:27:31 -06001503 switch (data->monitor_port) {
Timur Tabi7653aaa2011-07-09 15:38:14 -05001504 case FSL_DIU_PORT_DVI:
1505 return sprintf(buf, "DVI\n");
1506 case FSL_DIU_PORT_LVDS:
1507 return sprintf(buf, "Single-link LVDS\n");
1508 case FSL_DIU_PORT_DLVDS:
1509 return sprintf(buf, "Dual-link LVDS\n");
1510 }
1511
1512 return 0;
York Sun9b53a9e2008-04-28 02:15:34 -07001513}
1514
Timur Tabi9e52ba62011-09-15 16:44:50 -05001515static int __devinit fsl_diu_probe(struct platform_device *pdev)
York Sun9b53a9e2008-04-28 02:15:34 -07001516{
Timur Tabi9e52ba62011-09-15 16:44:50 -05001517 struct device_node *np = pdev->dev.of_node;
York Sun9b53a9e2008-04-28 02:15:34 -07001518 struct mfb_info *mfbi;
Timur Tabib7795052011-12-08 17:27:31 -06001519 struct fsl_diu_data *data;
Timur Tabib7795052011-12-08 17:27:31 -06001520 dma_addr_t dma_addr; /* DMA addr of fsl_diu_data struct */
Timur Tabiddd3d902011-12-02 17:03:27 -06001521 unsigned int i;
1522 int ret;
York Sun9b53a9e2008-04-28 02:15:34 -07001523
Damien Cassouc1f383d2012-07-31 18:39:20 +02001524 data = dmam_alloc_coherent(&pdev->dev, sizeof(struct fsl_diu_data),
1525 &dma_addr, GFP_DMA | __GFP_ZERO);
Timur Tabib7795052011-12-08 17:27:31 -06001526 if (!data)
York Sun9b53a9e2008-04-28 02:15:34 -07001527 return -ENOMEM;
Timur Tabib7795052011-12-08 17:27:31 -06001528 data->dma_addr = dma_addr;
Timur Tabiddd3d902011-12-02 17:03:27 -06001529
1530 /*
1531 * dma_alloc_coherent() uses a page allocator, so the address is
1532 * always page-aligned. We need the memory to be 32-byte aligned,
1533 * so that's good. However, if one day the allocator changes, we
1534 * need to catch that. It's not worth the effort to handle unaligned
1535 * alloctions now because it's highly unlikely to ever be a problem.
1536 */
Timur Tabib7795052011-12-08 17:27:31 -06001537 if ((unsigned long)data & 31) {
Timur Tabiddd3d902011-12-02 17:03:27 -06001538 dev_err(&pdev->dev, "misaligned allocation");
1539 ret = -ENOMEM;
1540 goto error;
1541 }
York Sun9b53a9e2008-04-28 02:15:34 -07001542
Timur Tabib7795052011-12-08 17:27:31 -06001543 spin_lock_init(&data->reg_lock);
Timur Tabi3c755b72011-10-04 19:36:51 -05001544
Timur Tabiddd3d902011-12-02 17:03:27 -06001545 for (i = 0; i < NUM_AOIS; i++) {
Timur Tabib7795052011-12-08 17:27:31 -06001546 struct fb_info *info = &data->fsl_diu_info[i];
Timur Tabiddd3d902011-12-02 17:03:27 -06001547
1548 info->device = &pdev->dev;
Timur Tabib7795052011-12-08 17:27:31 -06001549 info->par = &data->mfb[i];
Timur Tabiddd3d902011-12-02 17:03:27 -06001550
1551 /*
1552 * We store the physical address of the AD in the reserved
1553 * 'paddr' field of the AD itself.
1554 */
Timur Tabib7795052011-12-08 17:27:31 -06001555 data->ad[i].paddr = DMA_ADDR(data, ad[i]);
Timur Tabiddd3d902011-12-02 17:03:27 -06001556
1557 info->fix.smem_start = 0;
1558
1559 /* Initialize the AOI data structure */
1560 mfbi = info->par;
York Sun9b53a9e2008-04-28 02:15:34 -07001561 memcpy(mfbi, &mfb_template[i], sizeof(struct mfb_info));
Timur Tabib7795052011-12-08 17:27:31 -06001562 mfbi->parent = data;
1563 mfbi->ad = &data->ad[i];
Anatolij Gustschin8b856f02010-07-23 04:00:39 +00001564
Timur Tabi2572df92011-09-28 16:19:51 -05001565 if (mfbi->index == PLANE0) {
Anatolij Gustschin8b856f02010-07-23 04:00:39 +00001566 const u8 *prop;
1567 int len;
1568
1569 /* Get EDID */
1570 prop = of_get_property(np, "edid", &len);
1571 if (prop && len == EDID_LENGTH)
1572 mfbi->edid_data = kmemdup(prop, EDID_LENGTH,
1573 GFP_KERNEL);
1574 }
York Sun9b53a9e2008-04-28 02:15:34 -07001575 }
1576
Timur Tabib7795052011-12-08 17:27:31 -06001577 data->diu_reg = of_iomap(np, 0);
1578 if (!data->diu_reg) {
Timur Tabi9e52ba62011-09-15 16:44:50 -05001579 dev_err(&pdev->dev, "cannot map DIU registers\n");
York Sun9b53a9e2008-04-28 02:15:34 -07001580 ret = -EFAULT;
Timur Tabiddd3d902011-12-02 17:03:27 -06001581 goto error;
York Sun9b53a9e2008-04-28 02:15:34 -07001582 }
1583
York Sun9b53a9e2008-04-28 02:15:34 -07001584 /* Get the IRQ of the DIU */
Timur Tabib7795052011-12-08 17:27:31 -06001585 data->irq = irq_of_parse_and_map(np, 0);
York Sun9b53a9e2008-04-28 02:15:34 -07001586
Timur Tabib7795052011-12-08 17:27:31 -06001587 if (!data->irq) {
Timur Tabi9e52ba62011-09-15 16:44:50 -05001588 dev_err(&pdev->dev, "could not get DIU IRQ\n");
York Sun9b53a9e2008-04-28 02:15:34 -07001589 ret = -EINVAL;
1590 goto error;
1591 }
Timur Tabib7795052011-12-08 17:27:31 -06001592 data->monitor_port = monitor_port;
York Sun9b53a9e2008-04-28 02:15:34 -07001593
Timur Tabiddd3d902011-12-02 17:03:27 -06001594 /* Initialize the dummy Area Descriptor */
Timur Tabib7795052011-12-08 17:27:31 -06001595 data->dummy_ad.addr = cpu_to_le32(DMA_ADDR(data, dummy_aoi));
1596 data->dummy_ad.pix_fmt = 0x88882317;
1597 data->dummy_ad.src_size_g_alpha = cpu_to_le32((4 << 12) | 4);
1598 data->dummy_ad.aoi_size = cpu_to_le32((4 << 16) | 2);
1599 data->dummy_ad.offset_xyi = 0;
1600 data->dummy_ad.offset_xyd = 0;
1601 data->dummy_ad.next_ad = 0;
1602 data->dummy_ad.paddr = DMA_ADDR(data, dummy_ad);
York Sun9b53a9e2008-04-28 02:15:34 -07001603
Anatolij Gustschin4b5006e2010-07-23 04:00:37 +00001604 /*
Timur Tabi7af3b132012-10-16 17:33:48 -05001605 * Let DIU continue to display splash screen if it was pre-initialized
1606 * by the bootloader; otherwise, clear the display.
Anatolij Gustschin4b5006e2010-07-23 04:00:37 +00001607 */
Timur Tabi7af3b132012-10-16 17:33:48 -05001608 if (in_be32(&data->diu_reg->diu_mode) == MFB_MODE0)
1609 out_be32(&data->diu_reg->desc[0], 0);
Anatolij Gustschin4b5006e2010-07-23 04:00:37 +00001610
Timur Tabib7795052011-12-08 17:27:31 -06001611 out_be32(&data->diu_reg->desc[1], data->dummy_ad.paddr);
1612 out_be32(&data->diu_reg->desc[2], data->dummy_ad.paddr);
York Sun9b53a9e2008-04-28 02:15:34 -07001613
Timur Tabiddd3d902011-12-02 17:03:27 -06001614 for (i = 0; i < NUM_AOIS; i++) {
Timur Tabib7795052011-12-08 17:27:31 -06001615 ret = install_fb(&data->fsl_diu_info[i]);
York Sun9b53a9e2008-04-28 02:15:34 -07001616 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 Tabif74de502012-10-16 17:33:45 -05001622 /*
1623 * Older versions of U-Boot leave interrupts enabled, so disable
1624 * all of them and clear the status register.
1625 */
1626 out_be32(&data->diu_reg->int_mask, 0xffffffff);
1627 in_be32(&data->diu_reg->int_status);
1628
1629 ret = request_irq(data->irq, fsl_diu_isr, 0, "fsl-diu-fb",
1630 &data->diu_reg);
1631 if (ret) {
Timur Tabi9e52ba62011-09-15 16:44:50 -05001632 dev_err(&pdev->dev, "could not claim irq\n");
York Sun9b53a9e2008-04-28 02:15:34 -07001633 goto error;
1634 }
1635
Timur Tabib7795052011-12-08 17:27:31 -06001636 sysfs_attr_init(&data->dev_attr.attr);
1637 data->dev_attr.attr.name = "monitor";
1638 data->dev_attr.attr.mode = S_IRUGO|S_IWUSR;
1639 data->dev_attr.show = show_monitor;
1640 data->dev_attr.store = store_monitor;
1641 ret = device_create_file(&pdev->dev, &data->dev_attr);
Timur Tabiddd3d902011-12-02 17:03:27 -06001642 if (ret) {
Timur Tabi9e52ba62011-09-15 16:44:50 -05001643 dev_err(&pdev->dev, "could not create sysfs file %s\n",
Timur Tabib7795052011-12-08 17:27:31 -06001644 data->dev_attr.attr.name);
York Sun9b53a9e2008-04-28 02:15:34 -07001645 }
1646
Timur Tabib7795052011-12-08 17:27:31 -06001647 dev_set_drvdata(&pdev->dev, data);
York Sun9b53a9e2008-04-28 02:15:34 -07001648 return 0;
1649
1650error:
Timur Tabiddd3d902011-12-02 17:03:27 -06001651 for (i = 0; i < NUM_AOIS; i++)
Timur Tabib7795052011-12-08 17:27:31 -06001652 uninstall_fb(&data->fsl_diu_info[i]);
Timur Tabi3f78bbd2011-09-15 16:44:56 -05001653
Timur Tabib7795052011-12-08 17:27:31 -06001654 iounmap(data->diu_reg);
York Sun9b53a9e2008-04-28 02:15:34 -07001655
York Sun9b53a9e2008-04-28 02:15:34 -07001656 return ret;
1657}
1658
Timur Tabi9e52ba62011-09-15 16:44:50 -05001659static int fsl_diu_remove(struct platform_device *pdev)
York Sun9b53a9e2008-04-28 02:15:34 -07001660{
Timur Tabib7795052011-12-08 17:27:31 -06001661 struct fsl_diu_data *data;
York Sun9b53a9e2008-04-28 02:15:34 -07001662 int i;
1663
Timur Tabib7795052011-12-08 17:27:31 -06001664 data = dev_get_drvdata(&pdev->dev);
1665 disable_lcdc(&data->fsl_diu_info[0]);
Timur Tabif74de502012-10-16 17:33:45 -05001666
1667 free_irq(data->irq, &data->diu_reg);
Timur Tabiddd3d902011-12-02 17:03:27 -06001668
1669 for (i = 0; i < NUM_AOIS; i++)
Timur Tabib7795052011-12-08 17:27:31 -06001670 uninstall_fb(&data->fsl_diu_info[i]);
Timur Tabiddd3d902011-12-02 17:03:27 -06001671
Timur Tabib7795052011-12-08 17:27:31 -06001672 iounmap(data->diu_reg);
Timur Tabiddd3d902011-12-02 17:03:27 -06001673
York Sun9b53a9e2008-04-28 02:15:34 -07001674 return 0;
1675}
1676
1677#ifndef MODULE
1678static int __init fsl_diu_setup(char *options)
1679{
1680 char *opt;
1681 unsigned long val;
1682
1683 if (!options || !*options)
1684 return 0;
1685
1686 while ((opt = strsep(&options, ",")) != NULL) {
1687 if (!*opt)
1688 continue;
1689 if (!strncmp(opt, "monitor=", 8)) {
Timur Tabi7653aaa2011-07-09 15:38:14 -05001690 monitor_port = fsl_diu_name_to_port(opt + 8);
York Sun9b53a9e2008-04-28 02:15:34 -07001691 } else if (!strncmp(opt, "bpp=", 4)) {
1692 if (!strict_strtoul(opt + 4, 10, &val))
1693 default_bpp = val;
1694 } else
1695 fb_mode = opt;
1696 }
1697
1698 return 0;
1699}
1700#endif
1701
1702static struct of_device_id fsl_diu_match[] = {
Anatolij Gustschind24720a2010-02-17 07:33:22 -07001703#ifdef CONFIG_PPC_MPC512x
1704 {
1705 .compatible = "fsl,mpc5121-diu",
1706 },
1707#endif
York Sun9b53a9e2008-04-28 02:15:34 -07001708 {
1709 .compatible = "fsl,diu",
1710 },
1711 {}
1712};
1713MODULE_DEVICE_TABLE(of, fsl_diu_match);
1714
Grant Likely28541d02011-02-22 21:07:43 -07001715static struct platform_driver fsl_diu_driver = {
Grant Likely40182942010-04-13 16:13:02 -07001716 .driver = {
Timur Tabif8c6bf62011-09-15 16:44:53 -05001717 .name = "fsl-diu-fb",
Grant Likely40182942010-04-13 16:13:02 -07001718 .owner = THIS_MODULE,
1719 .of_match_table = fsl_diu_match,
1720 },
York Sun9b53a9e2008-04-28 02:15:34 -07001721 .probe = fsl_diu_probe,
1722 .remove = fsl_diu_remove,
1723 .suspend = fsl_diu_suspend,
1724 .resume = fsl_diu_resume,
1725};
1726
1727static int __init fsl_diu_init(void)
1728{
1729#ifdef CONFIG_NOT_COHERENT_CACHE
1730 struct device_node *np;
1731 const u32 *prop;
1732#endif
1733 int ret;
1734#ifndef MODULE
1735 char *option;
1736
1737 /*
1738 * For kernel boot options (in 'video=xxxfb:<options>' format)
1739 */
1740 if (fb_get_options("fslfb", &option))
1741 return -ENODEV;
1742 fsl_diu_setup(option);
Timur Tabi7653aaa2011-07-09 15:38:14 -05001743#else
1744 monitor_port = fsl_diu_name_to_port(monitor_string);
York Sun9b53a9e2008-04-28 02:15:34 -07001745#endif
Timur Tabi154152a2011-09-15 16:44:47 -05001746 pr_info("Freescale Display Interface Unit (DIU) framebuffer driver\n");
York Sun9b53a9e2008-04-28 02:15:34 -07001747
1748#ifdef CONFIG_NOT_COHERENT_CACHE
1749 np = of_find_node_by_type(NULL, "cpu");
1750 if (!np) {
Timur Tabi154152a2011-09-15 16:44:47 -05001751 pr_err("fsl-diu-fb: can't find 'cpu' device node\n");
York Sun9b53a9e2008-04-28 02:15:34 -07001752 return -ENODEV;
1753 }
1754
1755 prop = of_get_property(np, "d-cache-size", NULL);
Julia Lawall5394ba02008-08-05 13:01:28 -07001756 if (prop == NULL) {
Timur Tabi154152a2011-09-15 16:44:47 -05001757 pr_err("fsl-diu-fb: missing 'd-cache-size' property' "
1758 "in 'cpu' node\n");
Julia Lawall5394ba02008-08-05 13:01:28 -07001759 of_node_put(np);
York Sun9b53a9e2008-04-28 02:15:34 -07001760 return -ENODEV;
Julia Lawall5394ba02008-08-05 13:01:28 -07001761 }
York Sun9b53a9e2008-04-28 02:15:34 -07001762
Timur Tabi4a85dc8b2011-09-15 16:44:46 -05001763 /*
1764 * Freescale PLRU requires 13/8 times the cache size to do a proper
1765 * displacement flush
York Sun9b53a9e2008-04-28 02:15:34 -07001766 */
Timur Tabi9e52ba62011-09-15 16:44:50 -05001767 coherence_data_size = be32_to_cpup(prop) * 13;
York Sun9b53a9e2008-04-28 02:15:34 -07001768 coherence_data_size /= 8;
1769
Timur Tabi5cc2a362012-10-16 17:33:46 -05001770 pr_debug("fsl-diu-fb: coherence data size is %zu bytes\n",
1771 coherence_data_size);
1772
York Sun9b53a9e2008-04-28 02:15:34 -07001773 prop = of_get_property(np, "d-cache-line-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-line-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 }
Timur Tabi9e52ba62011-09-15 16:44:50 -05001780 d_cache_line_size = be32_to_cpup(prop);
York Sun9b53a9e2008-04-28 02:15:34 -07001781
Timur Tabi5cc2a362012-10-16 17:33:46 -05001782 pr_debug("fsl-diu-fb: cache lines size is %u bytes\n",
1783 d_cache_line_size);
1784
York Sun9b53a9e2008-04-28 02:15:34 -07001785 of_node_put(np);
1786 coherence_data = vmalloc(coherence_data_size);
Timur Tabi5cc2a362012-10-16 17:33:46 -05001787 if (!coherence_data) {
1788 pr_err("fsl-diu-fb: could not allocate coherence data "
1789 "(size=%zu)\n", coherence_data_size);
York Sun9b53a9e2008-04-28 02:15:34 -07001790 return -ENOMEM;
Timur Tabi5cc2a362012-10-16 17:33:46 -05001791 }
1792
York Sun9b53a9e2008-04-28 02:15:34 -07001793#endif
Timur Tabi4a85dc8b2011-09-15 16:44:46 -05001794
Grant Likely28541d02011-02-22 21:07:43 -07001795 ret = platform_driver_register(&fsl_diu_driver);
York Sun9b53a9e2008-04-28 02:15:34 -07001796 if (ret) {
Timur Tabi154152a2011-09-15 16:44:47 -05001797 pr_err("fsl-diu-fb: failed to register platform driver\n");
York Sun9b53a9e2008-04-28 02:15:34 -07001798#if defined(CONFIG_NOT_COHERENT_CACHE)
1799 vfree(coherence_data);
1800#endif
York Sun9b53a9e2008-04-28 02:15:34 -07001801 }
1802 return ret;
1803}
1804
1805static void __exit fsl_diu_exit(void)
1806{
Grant Likely28541d02011-02-22 21:07:43 -07001807 platform_driver_unregister(&fsl_diu_driver);
York Sun9b53a9e2008-04-28 02:15:34 -07001808#if defined(CONFIG_NOT_COHERENT_CACHE)
1809 vfree(coherence_data);
1810#endif
1811}
1812
1813module_init(fsl_diu_init);
1814module_exit(fsl_diu_exit);
1815
1816MODULE_AUTHOR("York Sun <yorksun@freescale.com>");
1817MODULE_DESCRIPTION("Freescale DIU framebuffer driver");
1818MODULE_LICENSE("GPL");
1819
1820module_param_named(mode, fb_mode, charp, 0);
1821MODULE_PARM_DESC(mode,
1822 "Specify resolution as \"<xres>x<yres>[-<bpp>][@<refresh>]\" ");
1823module_param_named(bpp, default_bpp, ulong, 0);
Timur Tabi154152a2011-09-15 16:44:47 -05001824MODULE_PARM_DESC(bpp, "Specify bit-per-pixel if not specified in 'mode'");
Timur Tabi7653aaa2011-07-09 15:38:14 -05001825module_param_named(monitor, monitor_string, charp, 0);
1826MODULE_PARM_DESC(monitor, "Specify the monitor port "
1827 "(\"dvi\", \"lvds\", or \"dlvds\") if supported by the platform");
York Sun9b53a9e2008-04-28 02:15:34 -07001828