blob: 10ee411df3cff1040c4846a985f0fe431ffa2117 [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>
33
York Sun9b53a9e2008-04-28 02:15:34 -070034#include <sysdev/fsl_soc.h>
Anatolij Gustschin0814a972010-07-23 04:00:36 +000035#include <linux/fsl-diu-fb.h>
Anatolij Gustschin8b856f02010-07-23 04:00:39 +000036#include "edid.h"
York Sun9b53a9e2008-04-28 02:15:34 -070037
38/*
39 * These parameters give default parameters
40 * for video output 1024x768,
41 * FIXME - change timing to proper amounts
42 * hsync 31.5kHz, vsync 60Hz
43 */
44static struct fb_videomode __devinitdata fsl_diu_default_mode = {
45 .refresh = 60,
46 .xres = 1024,
47 .yres = 768,
48 .pixclock = 15385,
49 .left_margin = 160,
50 .right_margin = 24,
51 .upper_margin = 29,
52 .lower_margin = 3,
53 .hsync_len = 136,
54 .vsync_len = 6,
55 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
56 .vmode = FB_VMODE_NONINTERLACED
57};
58
59static struct fb_videomode __devinitdata fsl_diu_mode_db[] = {
60 {
61 .name = "1024x768-60",
62 .refresh = 60,
63 .xres = 1024,
64 .yres = 768,
65 .pixclock = 15385,
66 .left_margin = 160,
67 .right_margin = 24,
68 .upper_margin = 29,
69 .lower_margin = 3,
70 .hsync_len = 136,
71 .vsync_len = 6,
72 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
73 .vmode = FB_VMODE_NONINTERLACED
74 },
75 {
76 .name = "1024x768-70",
77 .refresh = 70,
78 .xres = 1024,
79 .yres = 768,
80 .pixclock = 16886,
81 .left_margin = 3,
82 .right_margin = 3,
83 .upper_margin = 2,
84 .lower_margin = 2,
85 .hsync_len = 40,
86 .vsync_len = 18,
87 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
88 .vmode = FB_VMODE_NONINTERLACED
89 },
90 {
91 .name = "1024x768-75",
92 .refresh = 75,
93 .xres = 1024,
94 .yres = 768,
95 .pixclock = 15009,
96 .left_margin = 3,
97 .right_margin = 3,
98 .upper_margin = 2,
99 .lower_margin = 2,
100 .hsync_len = 80,
101 .vsync_len = 32,
102 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
103 .vmode = FB_VMODE_NONINTERLACED
104 },
105 {
106 .name = "1280x1024-60",
107 .refresh = 60,
108 .xres = 1280,
109 .yres = 1024,
110 .pixclock = 9375,
111 .left_margin = 38,
112 .right_margin = 128,
113 .upper_margin = 2,
114 .lower_margin = 7,
115 .hsync_len = 216,
116 .vsync_len = 37,
117 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
118 .vmode = FB_VMODE_NONINTERLACED
119 },
120 {
121 .name = "1280x1024-70",
122 .refresh = 70,
123 .xres = 1280,
124 .yres = 1024,
125 .pixclock = 9380,
126 .left_margin = 6,
127 .right_margin = 6,
128 .upper_margin = 4,
129 .lower_margin = 4,
130 .hsync_len = 60,
131 .vsync_len = 94,
132 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
133 .vmode = FB_VMODE_NONINTERLACED
134 },
135 {
136 .name = "1280x1024-75",
137 .refresh = 75,
138 .xres = 1280,
139 .yres = 1024,
140 .pixclock = 9380,
141 .left_margin = 6,
142 .right_margin = 6,
143 .upper_margin = 4,
144 .lower_margin = 4,
145 .hsync_len = 60,
146 .vsync_len = 15,
147 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
148 .vmode = FB_VMODE_NONINTERLACED
149 },
150 {
151 .name = "320x240", /* for AOI only */
152 .refresh = 60,
153 .xres = 320,
154 .yres = 240,
155 .pixclock = 15385,
156 .left_margin = 0,
157 .right_margin = 0,
158 .upper_margin = 0,
159 .lower_margin = 0,
160 .hsync_len = 0,
161 .vsync_len = 0,
162 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
163 .vmode = FB_VMODE_NONINTERLACED
164 },
165 {
166 .name = "1280x480-60",
167 .refresh = 60,
168 .xres = 1280,
169 .yres = 480,
170 .pixclock = 18939,
171 .left_margin = 353,
172 .right_margin = 47,
173 .upper_margin = 39,
174 .lower_margin = 4,
175 .hsync_len = 8,
176 .vsync_len = 2,
177 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
178 .vmode = FB_VMODE_NONINTERLACED
179 },
180};
181
182static char *fb_mode = "1024x768-32@60";
183static unsigned long default_bpp = 32;
Timur Tabi7653aaa2011-07-09 15:38:14 -0500184static enum fsl_diu_monitor_port monitor_port;
185static char *monitor_string;
York Sun9b53a9e2008-04-28 02:15:34 -0700186
187#if defined(CONFIG_NOT_COHERENT_CACHE)
188static u8 *coherence_data;
189static size_t coherence_data_size;
190static unsigned int d_cache_line_size;
191#endif
192
193static DEFINE_SPINLOCK(diu_lock);
194
195struct fsl_diu_data {
196 struct fb_info *fsl_diu_info[FSL_AOI_NUM - 1];
197 /*FSL_AOI_NUM has one dummy AOI */
198 struct device_attribute dev_attr;
199 struct diu_ad *dummy_ad;
200 void *dummy_aoi_virt;
201 unsigned int irq;
202 int fb_enabled;
Timur Tabi7653aaa2011-07-09 15:38:14 -0500203 enum fsl_diu_monitor_port monitor_port;
York Sun9b53a9e2008-04-28 02:15:34 -0700204};
205
206struct mfb_info {
207 int index;
208 int type;
209 char *id;
210 int registered;
211 int blank;
212 unsigned long pseudo_palette[16];
213 struct diu_ad *ad;
214 int cursor_reset;
215 unsigned char g_alpha;
216 unsigned int count;
217 int x_aoi_d; /* aoi display x offset to physical screen */
218 int y_aoi_d; /* aoi display y offset to physical screen */
219 struct fsl_diu_data *parent;
Anatolij Gustschin8b856f02010-07-23 04:00:39 +0000220 u8 *edid_data;
York Sun9b53a9e2008-04-28 02:15:34 -0700221};
222
223
224static struct mfb_info mfb_template[] = {
225 { /* AOI 0 for plane 0 */
Timur Tabi4a85dc8b2011-09-15 16:44:46 -0500226 .index = 0,
227 .type = MFB_TYPE_OUTPUT,
228 .id = "Panel0",
229 .registered = 0,
230 .count = 0,
231 .x_aoi_d = 0,
232 .y_aoi_d = 0,
York Sun9b53a9e2008-04-28 02:15:34 -0700233 },
234 { /* AOI 0 for plane 1 */
Timur Tabi4a85dc8b2011-09-15 16:44:46 -0500235 .index = 1,
236 .type = MFB_TYPE_OUTPUT,
237 .id = "Panel1 AOI0",
238 .registered = 0,
239 .g_alpha = 0xff,
240 .count = 0,
241 .x_aoi_d = 0,
242 .y_aoi_d = 0,
York Sun9b53a9e2008-04-28 02:15:34 -0700243 },
244 { /* AOI 1 for plane 1 */
Timur Tabi4a85dc8b2011-09-15 16:44:46 -0500245 .index = 2,
246 .type = MFB_TYPE_OUTPUT,
247 .id = "Panel1 AOI1",
248 .registered = 0,
249 .g_alpha = 0xff,
250 .count = 0,
251 .x_aoi_d = 0,
252 .y_aoi_d = 480,
York Sun9b53a9e2008-04-28 02:15:34 -0700253 },
254 { /* AOI 0 for plane 2 */
Timur Tabi4a85dc8b2011-09-15 16:44:46 -0500255 .index = 3,
256 .type = MFB_TYPE_OUTPUT,
257 .id = "Panel2 AOI0",
258 .registered = 0,
259 .g_alpha = 0xff,
260 .count = 0,
261 .x_aoi_d = 640,
262 .y_aoi_d = 0,
York Sun9b53a9e2008-04-28 02:15:34 -0700263 },
264 { /* AOI 1 for plane 2 */
Timur Tabi4a85dc8b2011-09-15 16:44:46 -0500265 .index = 4,
266 .type = MFB_TYPE_OUTPUT,
267 .id = "Panel2 AOI1",
268 .registered = 0,
269 .g_alpha = 0xff,
270 .count = 0,
271 .x_aoi_d = 640,
272 .y_aoi_d = 480,
York Sun9b53a9e2008-04-28 02:15:34 -0700273 },
274};
275
276static struct diu_hw dr = {
277 .mode = MFB_MODE1,
278 .reg_lock = __SPIN_LOCK_UNLOCKED(diu_hw.reg_lock),
279};
280
281static struct diu_pool pool;
282
Timur Tabi6b51d512008-07-23 21:31:39 -0700283/**
Timur Tabi7653aaa2011-07-09 15:38:14 -0500284 * fsl_diu_name_to_port - convert a port name to a monitor port enum
285 *
286 * Takes the name of a monitor port ("dvi", "lvds", or "dlvds") and returns
287 * the enum fsl_diu_monitor_port that corresponds to that string.
288 *
289 * For compatibility with older versions, a number ("0", "1", or "2") is also
290 * supported.
291 *
292 * If the string is unknown, DVI is assumed.
293 *
294 * If the particular port is not supported by the platform, another port
295 * (platform-specific) is chosen instead.
296 */
297static enum fsl_diu_monitor_port fsl_diu_name_to_port(const char *s)
298{
299 enum fsl_diu_monitor_port port = FSL_DIU_PORT_DVI;
300 unsigned long val;
301
302 if (s) {
303 if (!strict_strtoul(s, 10, &val) && (val <= 2))
304 port = (enum fsl_diu_monitor_port) val;
305 else if (strncmp(s, "lvds", 4) == 0)
306 port = FSL_DIU_PORT_LVDS;
307 else if (strncmp(s, "dlvds", 5) == 0)
308 port = FSL_DIU_PORT_DLVDS;
309 }
310
311 return diu_ops.valid_monitor_port(port);
312}
313
314/**
Timur Tabi6b51d512008-07-23 21:31:39 -0700315 * fsl_diu_alloc - allocate memory for the DIU
316 * @size: number of bytes to allocate
317 * @param: returned physical address of memory
318 *
319 * This function allocates a physically-contiguous block of memory.
York Sun9b53a9e2008-04-28 02:15:34 -0700320 */
Timur Tabi6b51d512008-07-23 21:31:39 -0700321static void *fsl_diu_alloc(size_t size, phys_addr_t *phys)
York Sun9b53a9e2008-04-28 02:15:34 -0700322{
323 void *virt;
324
Timur Tabi6b51d512008-07-23 21:31:39 -0700325 virt = alloc_pages_exact(size, GFP_DMA | __GFP_ZERO);
Timur Tabi154152a2011-09-15 16:44:47 -0500326 if (virt)
York Sun9b53a9e2008-04-28 02:15:34 -0700327 *phys = virt_to_phys(virt);
York Sun9b53a9e2008-04-28 02:15:34 -0700328
329 return virt;
330}
331
Timur Tabi6b51d512008-07-23 21:31:39 -0700332/**
333 * fsl_diu_free - release DIU memory
334 * @virt: pointer returned by fsl_diu_alloc()
335 * @size: number of bytes allocated by fsl_diu_alloc()
336 *
337 * This function releases memory allocated by fsl_diu_alloc().
338 */
339static void fsl_diu_free(void *virt, size_t size)
York Sun9b53a9e2008-04-28 02:15:34 -0700340{
Timur Tabi6b51d512008-07-23 21:31:39 -0700341 if (virt && size)
342 free_pages_exact(virt, size);
York Sun9b53a9e2008-04-28 02:15:34 -0700343}
344
Anatolij Gustschin0d9dab32010-07-23 04:00:35 +0000345/*
346 * Workaround for failed writing desc register of planes.
347 * Needed with MPC5121 DIU rev 2.0 silicon.
348 */
349void wr_reg_wa(u32 *reg, u32 val)
350{
351 do {
352 out_be32(reg, val);
353 } while (in_be32(reg) != val);
354}
355
York Sun9b53a9e2008-04-28 02:15:34 -0700356static int fsl_diu_enable_panel(struct fb_info *info)
357{
358 struct mfb_info *pmfbi, *cmfbi, *mfbi = info->par;
359 struct diu *hw = dr.diu_reg;
360 struct diu_ad *ad = mfbi->ad;
361 struct fsl_diu_data *machine_data = mfbi->parent;
362 int res = 0;
363
York Sun9b53a9e2008-04-28 02:15:34 -0700364 if (mfbi->type != MFB_TYPE_OFF) {
365 switch (mfbi->index) {
366 case 0: /* plane 0 */
367 if (hw->desc[0] != ad->paddr)
Anatolij Gustschin0d9dab32010-07-23 04:00:35 +0000368 wr_reg_wa(&hw->desc[0], ad->paddr);
York Sun9b53a9e2008-04-28 02:15:34 -0700369 break;
370 case 1: /* plane 1 AOI 0 */
371 cmfbi = machine_data->fsl_diu_info[2]->par;
372 if (hw->desc[1] != ad->paddr) { /* AOI0 closed */
373 if (cmfbi->count > 0) /* AOI1 open */
374 ad->next_ad =
375 cpu_to_le32(cmfbi->ad->paddr);
376 else
377 ad->next_ad = 0;
Anatolij Gustschin0d9dab32010-07-23 04:00:35 +0000378 wr_reg_wa(&hw->desc[1], ad->paddr);
York Sun9b53a9e2008-04-28 02:15:34 -0700379 }
380 break;
381 case 3: /* plane 2 AOI 0 */
382 cmfbi = machine_data->fsl_diu_info[4]->par;
383 if (hw->desc[2] != ad->paddr) { /* AOI0 closed */
384 if (cmfbi->count > 0) /* AOI1 open */
385 ad->next_ad =
386 cpu_to_le32(cmfbi->ad->paddr);
387 else
388 ad->next_ad = 0;
Anatolij Gustschin0d9dab32010-07-23 04:00:35 +0000389 wr_reg_wa(&hw->desc[2], ad->paddr);
York Sun9b53a9e2008-04-28 02:15:34 -0700390 }
391 break;
392 case 2: /* plane 1 AOI 1 */
393 pmfbi = machine_data->fsl_diu_info[1]->par;
394 ad->next_ad = 0;
395 if (hw->desc[1] == machine_data->dummy_ad->paddr)
Anatolij Gustschin0d9dab32010-07-23 04:00:35 +0000396 wr_reg_wa(&hw->desc[1], ad->paddr);
York Sun9b53a9e2008-04-28 02:15:34 -0700397 else /* AOI0 open */
398 pmfbi->ad->next_ad = cpu_to_le32(ad->paddr);
399 break;
400 case 4: /* plane 2 AOI 1 */
401 pmfbi = machine_data->fsl_diu_info[3]->par;
402 ad->next_ad = 0;
403 if (hw->desc[2] == machine_data->dummy_ad->paddr)
Anatolij Gustschin0d9dab32010-07-23 04:00:35 +0000404 wr_reg_wa(&hw->desc[2], ad->paddr);
York Sun9b53a9e2008-04-28 02:15:34 -0700405 else /* AOI0 was open */
406 pmfbi->ad->next_ad = cpu_to_le32(ad->paddr);
407 break;
408 default:
409 res = -EINVAL;
410 break;
411 }
412 } else
413 res = -EINVAL;
414 return res;
415}
416
417static int fsl_diu_disable_panel(struct fb_info *info)
418{
419 struct mfb_info *pmfbi, *cmfbi, *mfbi = info->par;
420 struct diu *hw = dr.diu_reg;
421 struct diu_ad *ad = mfbi->ad;
422 struct fsl_diu_data *machine_data = mfbi->parent;
423 int res = 0;
424
425 switch (mfbi->index) {
426 case 0: /* plane 0 */
427 if (hw->desc[0] != machine_data->dummy_ad->paddr)
Anatolij Gustschin0d9dab32010-07-23 04:00:35 +0000428 wr_reg_wa(&hw->desc[0], machine_data->dummy_ad->paddr);
York Sun9b53a9e2008-04-28 02:15:34 -0700429 break;
430 case 1: /* plane 1 AOI 0 */
431 cmfbi = machine_data->fsl_diu_info[2]->par;
432 if (cmfbi->count > 0) /* AOI1 is open */
Anatolij Gustschin0d9dab32010-07-23 04:00:35 +0000433 wr_reg_wa(&hw->desc[1], cmfbi->ad->paddr);
York Sun9b53a9e2008-04-28 02:15:34 -0700434 /* move AOI1 to the first */
435 else /* AOI1 was closed */
Anatolij Gustschin0d9dab32010-07-23 04:00:35 +0000436 wr_reg_wa(&hw->desc[1], machine_data->dummy_ad->paddr);
York Sun9b53a9e2008-04-28 02:15:34 -0700437 /* close AOI 0 */
438 break;
439 case 3: /* plane 2 AOI 0 */
440 cmfbi = machine_data->fsl_diu_info[4]->par;
441 if (cmfbi->count > 0) /* AOI1 is open */
Anatolij Gustschin0d9dab32010-07-23 04:00:35 +0000442 wr_reg_wa(&hw->desc[2], cmfbi->ad->paddr);
York Sun9b53a9e2008-04-28 02:15:34 -0700443 /* move AOI1 to the first */
444 else /* AOI1 was closed */
Anatolij Gustschin0d9dab32010-07-23 04:00:35 +0000445 wr_reg_wa(&hw->desc[2], machine_data->dummy_ad->paddr);
York Sun9b53a9e2008-04-28 02:15:34 -0700446 /* close AOI 0 */
447 break;
448 case 2: /* plane 1 AOI 1 */
449 pmfbi = machine_data->fsl_diu_info[1]->par;
450 if (hw->desc[1] != ad->paddr) {
451 /* AOI1 is not the first in the chain */
452 if (pmfbi->count > 0)
453 /* AOI0 is open, must be the first */
454 pmfbi->ad->next_ad = 0;
455 } else /* AOI1 is the first in the chain */
Anatolij Gustschin0d9dab32010-07-23 04:00:35 +0000456 wr_reg_wa(&hw->desc[1], machine_data->dummy_ad->paddr);
York Sun9b53a9e2008-04-28 02:15:34 -0700457 /* close AOI 1 */
458 break;
459 case 4: /* plane 2 AOI 1 */
460 pmfbi = machine_data->fsl_diu_info[3]->par;
461 if (hw->desc[2] != ad->paddr) {
462 /* AOI1 is not the first in the chain */
463 if (pmfbi->count > 0)
464 /* AOI0 is open, must be the first */
465 pmfbi->ad->next_ad = 0;
466 } else /* AOI1 is the first in the chain */
Anatolij Gustschin0d9dab32010-07-23 04:00:35 +0000467 wr_reg_wa(&hw->desc[2], machine_data->dummy_ad->paddr);
York Sun9b53a9e2008-04-28 02:15:34 -0700468 /* close AOI 1 */
469 break;
470 default:
471 res = -EINVAL;
472 break;
473 }
474
475 return res;
476}
477
478static void enable_lcdc(struct fb_info *info)
479{
480 struct diu *hw = dr.diu_reg;
481 struct mfb_info *mfbi = info->par;
482 struct fsl_diu_data *machine_data = mfbi->parent;
483
484 if (!machine_data->fb_enabled) {
485 out_be32(&hw->diu_mode, dr.mode);
486 machine_data->fb_enabled++;
487 }
488}
489
490static void disable_lcdc(struct fb_info *info)
491{
492 struct diu *hw = dr.diu_reg;
493 struct mfb_info *mfbi = info->par;
494 struct fsl_diu_data *machine_data = mfbi->parent;
495
496 if (machine_data->fb_enabled) {
497 out_be32(&hw->diu_mode, 0);
498 machine_data->fb_enabled = 0;
499 }
500}
501
502static void adjust_aoi_size_position(struct fb_var_screeninfo *var,
503 struct fb_info *info)
504{
505 struct mfb_info *lower_aoi_mfbi, *upper_aoi_mfbi, *mfbi = info->par;
506 struct fsl_diu_data *machine_data = mfbi->parent;
507 int available_height, upper_aoi_bottom, index = mfbi->index;
508 int lower_aoi_is_open, upper_aoi_is_open;
509 __u32 base_plane_width, base_plane_height, upper_aoi_height;
510
511 base_plane_width = machine_data->fsl_diu_info[0]->var.xres;
512 base_plane_height = machine_data->fsl_diu_info[0]->var.yres;
513
York Sunfdfaa48332008-08-15 00:40:29 -0700514 if (mfbi->x_aoi_d < 0)
515 mfbi->x_aoi_d = 0;
516 if (mfbi->y_aoi_d < 0)
517 mfbi->y_aoi_d = 0;
York Sun9b53a9e2008-04-28 02:15:34 -0700518 switch (index) {
519 case 0:
520 if (mfbi->x_aoi_d != 0)
521 mfbi->x_aoi_d = 0;
522 if (mfbi->y_aoi_d != 0)
523 mfbi->y_aoi_d = 0;
524 break;
525 case 1: /* AOI 0 */
526 case 3:
527 lower_aoi_mfbi = machine_data->fsl_diu_info[index+1]->par;
528 lower_aoi_is_open = lower_aoi_mfbi->count > 0 ? 1 : 0;
529 if (var->xres > base_plane_width)
530 var->xres = base_plane_width;
531 if ((mfbi->x_aoi_d + var->xres) > base_plane_width)
532 mfbi->x_aoi_d = base_plane_width - var->xres;
533
534 if (lower_aoi_is_open)
535 available_height = lower_aoi_mfbi->y_aoi_d;
536 else
537 available_height = base_plane_height;
538 if (var->yres > available_height)
539 var->yres = available_height;
540 if ((mfbi->y_aoi_d + var->yres) > available_height)
541 mfbi->y_aoi_d = available_height - var->yres;
542 break;
543 case 2: /* AOI 1 */
544 case 4:
545 upper_aoi_mfbi = machine_data->fsl_diu_info[index-1]->par;
546 upper_aoi_height =
547 machine_data->fsl_diu_info[index-1]->var.yres;
548 upper_aoi_bottom = upper_aoi_mfbi->y_aoi_d + upper_aoi_height;
549 upper_aoi_is_open = upper_aoi_mfbi->count > 0 ? 1 : 0;
550 if (var->xres > base_plane_width)
551 var->xres = base_plane_width;
552 if ((mfbi->x_aoi_d + var->xres) > base_plane_width)
553 mfbi->x_aoi_d = base_plane_width - var->xres;
554 if (mfbi->y_aoi_d < 0)
555 mfbi->y_aoi_d = 0;
556 if (upper_aoi_is_open) {
557 if (mfbi->y_aoi_d < upper_aoi_bottom)
558 mfbi->y_aoi_d = upper_aoi_bottom;
559 available_height = base_plane_height
560 - upper_aoi_bottom;
561 } else
562 available_height = base_plane_height;
563 if (var->yres > available_height)
564 var->yres = available_height;
565 if ((mfbi->y_aoi_d + var->yres) > base_plane_height)
566 mfbi->y_aoi_d = base_plane_height - var->yres;
567 break;
568 }
569}
570/*
571 * Checks to see if the hardware supports the state requested by var passed
572 * in. This function does not alter the hardware state! If the var passed in
573 * is slightly off by what the hardware can support then we alter the var
574 * PASSED in to what we can do. If the hardware doesn't support mode change
575 * a -EINVAL will be returned by the upper layers.
576 */
577static int fsl_diu_check_var(struct fb_var_screeninfo *var,
578 struct fb_info *info)
579{
York Sun9b53a9e2008-04-28 02:15:34 -0700580 if (var->xres_virtual < var->xres)
581 var->xres_virtual = var->xres;
582 if (var->yres_virtual < var->yres)
583 var->yres_virtual = var->yres;
584
585 if (var->xoffset < 0)
586 var->xoffset = 0;
587
588 if (var->yoffset < 0)
589 var->yoffset = 0;
590
591 if (var->xoffset + info->var.xres > info->var.xres_virtual)
592 var->xoffset = info->var.xres_virtual - info->var.xres;
593
594 if (var->yoffset + info->var.yres > info->var.yres_virtual)
595 var->yoffset = info->var.yres_virtual - info->var.yres;
596
597 if ((var->bits_per_pixel != 32) && (var->bits_per_pixel != 24) &&
598 (var->bits_per_pixel != 16))
599 var->bits_per_pixel = default_bpp;
600
601 switch (var->bits_per_pixel) {
602 case 16:
603 var->red.length = 5;
604 var->red.offset = 11;
605 var->red.msb_right = 0;
606
607 var->green.length = 6;
608 var->green.offset = 5;
609 var->green.msb_right = 0;
610
611 var->blue.length = 5;
612 var->blue.offset = 0;
613 var->blue.msb_right = 0;
614
615 var->transp.length = 0;
616 var->transp.offset = 0;
617 var->transp.msb_right = 0;
618 break;
619 case 24:
620 var->red.length = 8;
621 var->red.offset = 0;
622 var->red.msb_right = 0;
623
624 var->green.length = 8;
625 var->green.offset = 8;
626 var->green.msb_right = 0;
627
628 var->blue.length = 8;
629 var->blue.offset = 16;
630 var->blue.msb_right = 0;
631
632 var->transp.length = 0;
633 var->transp.offset = 0;
634 var->transp.msb_right = 0;
635 break;
636 case 32:
637 var->red.length = 8;
638 var->red.offset = 16;
639 var->red.msb_right = 0;
640
641 var->green.length = 8;
642 var->green.offset = 8;
643 var->green.msb_right = 0;
644
645 var->blue.length = 8;
646 var->blue.offset = 0;
647 var->blue.msb_right = 0;
648
649 var->transp.length = 8;
650 var->transp.offset = 24;
651 var->transp.msb_right = 0;
652
653 break;
654 }
York Sun9b53a9e2008-04-28 02:15:34 -0700655
656 var->height = -1;
657 var->width = -1;
658 var->grayscale = 0;
659
660 /* Copy nonstd field to/from sync for fbset usage */
661 var->sync |= var->nonstd;
662 var->nonstd |= var->sync;
663
664 adjust_aoi_size_position(var, info);
665 return 0;
666}
667
668static void set_fix(struct fb_info *info)
669{
670 struct fb_fix_screeninfo *fix = &info->fix;
671 struct fb_var_screeninfo *var = &info->var;
672 struct mfb_info *mfbi = info->par;
673
674 strncpy(fix->id, mfbi->id, strlen(mfbi->id));
675 fix->line_length = var->xres_virtual * var->bits_per_pixel / 8;
676 fix->type = FB_TYPE_PACKED_PIXELS;
677 fix->accel = FB_ACCEL_NONE;
678 fix->visual = FB_VISUAL_TRUECOLOR;
679 fix->xpanstep = 1;
680 fix->ypanstep = 1;
681}
682
683static void update_lcdc(struct fb_info *info)
684{
685 struct fb_var_screeninfo *var = &info->var;
686 struct mfb_info *mfbi = info->par;
687 struct fsl_diu_data *machine_data = mfbi->parent;
688 struct diu *hw;
689 int i, j;
690 char __iomem *cursor_base, *gamma_table_base;
691
692 u32 temp;
693
694 hw = dr.diu_reg;
695
696 if (mfbi->type == MFB_TYPE_OFF) {
697 fsl_diu_disable_panel(info);
698 return;
699 }
700
701 diu_ops.set_monitor_port(machine_data->monitor_port);
702 gamma_table_base = pool.gamma.vaddr;
703 cursor_base = pool.cursor.vaddr;
704 /* Prep for DIU init - gamma table, cursor table */
705
706 for (i = 0; i <= 2; i++)
Timur Tabi4a85dc8b2011-09-15 16:44:46 -0500707 for (j = 0; j <= 255; j++)
708 *gamma_table_base++ = j;
York Sun9b53a9e2008-04-28 02:15:34 -0700709
710 diu_ops.set_gamma_table(machine_data->monitor_port, pool.gamma.vaddr);
711
York Sun9b53a9e2008-04-28 02:15:34 -0700712 disable_lcdc(info);
713
714 /* Program DIU registers */
715
716 out_be32(&hw->gamma, pool.gamma.paddr);
717 out_be32(&hw->cursor, pool.cursor.paddr);
718
719 out_be32(&hw->bgnd, 0x007F7F7F); /* BGND */
720 out_be32(&hw->bgnd_wb, 0); /* BGND_WB */
721 out_be32(&hw->disp_size, (var->yres << 16 | var->xres));
722 /* DISP SIZE */
York Sun9b53a9e2008-04-28 02:15:34 -0700723 out_be32(&hw->wb_size, 0); /* WB SIZE */
724 out_be32(&hw->wb_mem_addr, 0); /* WB MEM ADDR */
725
726 /* Horizontal and vertical configuration register */
727 temp = var->left_margin << 22 | /* BP_H */
728 var->hsync_len << 11 | /* PW_H */
729 var->right_margin; /* FP_H */
730
731 out_be32(&hw->hsyn_para, temp);
732
733 temp = var->upper_margin << 22 | /* BP_V */
734 var->vsync_len << 11 | /* PW_V */
735 var->lower_margin; /* FP_V */
736
737 out_be32(&hw->vsyn_para, temp);
738
York Sun9b53a9e2008-04-28 02:15:34 -0700739 diu_ops.set_pixel_clock(var->pixclock);
740
741 out_be32(&hw->syn_pol, 0); /* SYNC SIGNALS POLARITY */
742 out_be32(&hw->thresholds, 0x00037800); /* The Thresholds */
743 out_be32(&hw->int_status, 0); /* INTERRUPT STATUS */
744 out_be32(&hw->plut, 0x01F5F666);
745
746 /* Enable the DIU */
747 enable_lcdc(info);
748}
749
750static int map_video_memory(struct fb_info *info)
751{
752 phys_addr_t phys;
Krzysztof Helt537a1bf2009-06-30 11:41:29 -0700753 u32 smem_len = info->fix.line_length * info->var.yres_virtual;
York Sun9b53a9e2008-04-28 02:15:34 -0700754
Krzysztof Helt537a1bf2009-06-30 11:41:29 -0700755 info->screen_base = fsl_diu_alloc(smem_len, &phys);
Anton Vorontsov05946bc2008-07-04 09:59:38 -0700756 if (info->screen_base == NULL) {
Timur Tabi154152a2011-09-15 16:44:47 -0500757 dev_err(info->dev, "unable to allocate fb memory\n");
York Sun9b53a9e2008-04-28 02:15:34 -0700758 return -ENOMEM;
759 }
Krzysztof Helt537a1bf2009-06-30 11:41:29 -0700760 mutex_lock(&info->mm_lock);
York Sun9b53a9e2008-04-28 02:15:34 -0700761 info->fix.smem_start = (unsigned long) phys;
Krzysztof Helt537a1bf2009-06-30 11:41:29 -0700762 info->fix.smem_len = smem_len;
763 mutex_unlock(&info->mm_lock);
York Sun9b53a9e2008-04-28 02:15:34 -0700764 info->screen_size = info->fix.smem_len;
765
York Sun9b53a9e2008-04-28 02:15:34 -0700766 return 0;
767}
768
769static void unmap_video_memory(struct fb_info *info)
770{
771 fsl_diu_free(info->screen_base, info->fix.smem_len);
Krzysztof Helt537a1bf2009-06-30 11:41:29 -0700772 mutex_lock(&info->mm_lock);
Anton Vorontsov05946bc2008-07-04 09:59:38 -0700773 info->screen_base = NULL;
York Sun9b53a9e2008-04-28 02:15:34 -0700774 info->fix.smem_start = 0;
775 info->fix.smem_len = 0;
Krzysztof Helt537a1bf2009-06-30 11:41:29 -0700776 mutex_unlock(&info->mm_lock);
York Sun9b53a9e2008-04-28 02:15:34 -0700777}
778
779/*
York Sunae5591e2008-08-15 00:40:28 -0700780 * Using the fb_var_screeninfo in fb_info we set the aoi of this
781 * particular framebuffer. It is a light version of fsl_diu_set_par.
782 */
783static int fsl_diu_set_aoi(struct fb_info *info)
784{
785 struct fb_var_screeninfo *var = &info->var;
786 struct mfb_info *mfbi = info->par;
787 struct diu_ad *ad = mfbi->ad;
788
789 /* AOI should not be greater than display size */
790 ad->offset_xyi = cpu_to_le32((var->yoffset << 16) | var->xoffset);
791 ad->offset_xyd = cpu_to_le32((mfbi->y_aoi_d << 16) | mfbi->x_aoi_d);
792 return 0;
793}
794
795/*
York Sun9b53a9e2008-04-28 02:15:34 -0700796 * Using the fb_var_screeninfo in fb_info we set the resolution of this
797 * particular framebuffer. This function alters the fb_fix_screeninfo stored
798 * in fb_info. It does not alter var in fb_info since we are using that
799 * data. This means we depend on the data in var inside fb_info to be
800 * supported by the hardware. fsl_diu_check_var is always called before
801 * fsl_diu_set_par to ensure this.
802 */
803static int fsl_diu_set_par(struct fb_info *info)
804{
805 unsigned long len;
806 struct fb_var_screeninfo *var = &info->var;
807 struct mfb_info *mfbi = info->par;
808 struct fsl_diu_data *machine_data = mfbi->parent;
809 struct diu_ad *ad = mfbi->ad;
810 struct diu *hw;
811
812 hw = dr.diu_reg;
813
814 set_fix(info);
815 mfbi->cursor_reset = 1;
816
817 len = info->var.yres_virtual * info->fix.line_length;
818 /* Alloc & dealloc each time resolution/bpp change */
819 if (len != info->fix.smem_len) {
820 if (info->fix.smem_start)
821 unmap_video_memory(info);
York Sun9b53a9e2008-04-28 02:15:34 -0700822
823 /* Memory allocation for framebuffer */
824 if (map_video_memory(info)) {
Timur Tabi154152a2011-09-15 16:44:47 -0500825 dev_err(info->dev, "unable to allocate fb memory 1\n");
York Sun9b53a9e2008-04-28 02:15:34 -0700826 return -ENOMEM;
827 }
828 }
829
Timur Tabi7653aaa2011-07-09 15:38:14 -0500830 ad->pix_fmt = diu_ops.get_pixel_format(machine_data->monitor_port,
831 var->bits_per_pixel);
York Sun9b53a9e2008-04-28 02:15:34 -0700832 ad->addr = cpu_to_le32(info->fix.smem_start);
York Sunae5591e2008-08-15 00:40:28 -0700833 ad->src_size_g_alpha = cpu_to_le32((var->yres_virtual << 12) |
834 var->xres_virtual) | mfbi->g_alpha;
835 /* AOI should not be greater than display size */
York Sun9b53a9e2008-04-28 02:15:34 -0700836 ad->aoi_size = cpu_to_le32((var->yres << 16) | var->xres);
York Sunae5591e2008-08-15 00:40:28 -0700837 ad->offset_xyi = cpu_to_le32((var->yoffset << 16) | var->xoffset);
York Sun9b53a9e2008-04-28 02:15:34 -0700838 ad->offset_xyd = cpu_to_le32((mfbi->y_aoi_d << 16) | mfbi->x_aoi_d);
839
840 /* Disable chroma keying function */
841 ad->ckmax_r = 0;
842 ad->ckmax_g = 0;
843 ad->ckmax_b = 0;
844
845 ad->ckmin_r = 255;
846 ad->ckmin_g = 255;
847 ad->ckmin_b = 255;
848
849 if (mfbi->index == 0)
850 update_lcdc(info);
851 return 0;
852}
853
854static inline __u32 CNVT_TOHW(__u32 val, __u32 width)
855{
Timur Tabi4a85dc8b2011-09-15 16:44:46 -0500856 return ((val << width) + 0x7FFF - val) >> 16;
York Sun9b53a9e2008-04-28 02:15:34 -0700857}
858
859/*
860 * Set a single color register. The values supplied have a 16 bit magnitude
861 * which needs to be scaled in this function for the hardware. Things to take
862 * into consideration are how many color registers, if any, are supported with
863 * the current color visual. With truecolor mode no color palettes are
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300864 * supported. Here a pseudo palette is created which we store the value in
York Sun9b53a9e2008-04-28 02:15:34 -0700865 * pseudo_palette in struct fb_info. For pseudocolor mode we have a limited
866 * color palette.
867 */
Timur Tabi4a85dc8b2011-09-15 16:44:46 -0500868static int fsl_diu_setcolreg(unsigned int regno, unsigned int red,
869 unsigned int green, unsigned int blue,
870 unsigned int transp, struct fb_info *info)
York Sun9b53a9e2008-04-28 02:15:34 -0700871{
872 int ret = 1;
873
874 /*
875 * If greyscale is true, then we convert the RGB value
876 * to greyscale no matter what visual we are using.
877 */
878 if (info->var.grayscale)
879 red = green = blue = (19595 * red + 38470 * green +
880 7471 * blue) >> 16;
881 switch (info->fix.visual) {
882 case FB_VISUAL_TRUECOLOR:
883 /*
884 * 16-bit True Colour. We encode the RGB value
885 * according to the RGB bitfield information.
886 */
887 if (regno < 16) {
888 u32 *pal = info->pseudo_palette;
889 u32 v;
890
891 red = CNVT_TOHW(red, info->var.red.length);
892 green = CNVT_TOHW(green, info->var.green.length);
893 blue = CNVT_TOHW(blue, info->var.blue.length);
894 transp = CNVT_TOHW(transp, info->var.transp.length);
895
896 v = (red << info->var.red.offset) |
897 (green << info->var.green.offset) |
898 (blue << info->var.blue.offset) |
899 (transp << info->var.transp.offset);
900
901 pal[regno] = v;
902 ret = 0;
903 }
904 break;
905 case FB_VISUAL_STATIC_PSEUDOCOLOR:
906 case FB_VISUAL_PSEUDOCOLOR:
907 break;
908 }
909
910 return ret;
911}
912
913/*
914 * Pan (or wrap, depending on the `vmode' field) the display using the
915 * 'xoffset' and 'yoffset' fields of the 'var' structure. If the values
916 * don't fit, return -EINVAL.
917 */
918static int fsl_diu_pan_display(struct fb_var_screeninfo *var,
919 struct fb_info *info)
920{
921 if ((info->var.xoffset == var->xoffset) &&
922 (info->var.yoffset == var->yoffset))
923 return 0; /* No change, do nothing */
924
925 if (var->xoffset < 0 || var->yoffset < 0
926 || var->xoffset + info->var.xres > info->var.xres_virtual
927 || var->yoffset + info->var.yres > info->var.yres_virtual)
928 return -EINVAL;
929
930 info->var.xoffset = var->xoffset;
931 info->var.yoffset = var->yoffset;
932
933 if (var->vmode & FB_VMODE_YWRAP)
934 info->var.vmode |= FB_VMODE_YWRAP;
935 else
936 info->var.vmode &= ~FB_VMODE_YWRAP;
937
York Sunae5591e2008-08-15 00:40:28 -0700938 fsl_diu_set_aoi(info);
939
York Sun9b53a9e2008-04-28 02:15:34 -0700940 return 0;
941}
942
943/*
944 * Blank the screen if blank_mode != 0, else unblank. Return 0 if blanking
945 * succeeded, != 0 if un-/blanking failed.
946 * blank_mode == 2: suspend vsync
947 * blank_mode == 3: suspend hsync
948 * blank_mode == 4: powerdown
949 */
950static int fsl_diu_blank(int blank_mode, struct fb_info *info)
951{
952 struct mfb_info *mfbi = info->par;
953
954 mfbi->blank = blank_mode;
955
956 switch (blank_mode) {
957 case FB_BLANK_VSYNC_SUSPEND:
958 case FB_BLANK_HSYNC_SUSPEND:
959 /* FIXME: fixes to enable_panel and enable lcdc needed */
960 case FB_BLANK_NORMAL:
961 /* fsl_diu_disable_panel(info);*/
962 break;
963 case FB_BLANK_POWERDOWN:
964 /* disable_lcdc(info); */
965 break;
966 case FB_BLANK_UNBLANK:
967 /* fsl_diu_enable_panel(info);*/
968 break;
969 }
970
971 return 0;
972}
973
974static int fsl_diu_ioctl(struct fb_info *info, unsigned int cmd,
975 unsigned long arg)
976{
977 struct mfb_info *mfbi = info->par;
978 struct diu_ad *ad = mfbi->ad;
979 struct mfb_chroma_key ck;
980 unsigned char global_alpha;
981 struct aoi_display_offset aoi_d;
982 __u32 pix_fmt;
983 void __user *buf = (void __user *)arg;
984
985 if (!arg)
986 return -EINVAL;
987 switch (cmd) {
988 case MFB_SET_PIXFMT:
989 if (copy_from_user(&pix_fmt, buf, sizeof(pix_fmt)))
990 return -EFAULT;
991 ad->pix_fmt = pix_fmt;
York Sun9b53a9e2008-04-28 02:15:34 -0700992 break;
993 case MFB_GET_PIXFMT:
994 pix_fmt = ad->pix_fmt;
995 if (copy_to_user(buf, &pix_fmt, sizeof(pix_fmt)))
996 return -EFAULT;
York Sun9b53a9e2008-04-28 02:15:34 -0700997 break;
998 case MFB_SET_AOID:
999 if (copy_from_user(&aoi_d, buf, sizeof(aoi_d)))
1000 return -EFAULT;
1001 mfbi->x_aoi_d = aoi_d.x_aoi_d;
1002 mfbi->y_aoi_d = aoi_d.y_aoi_d;
York Sun9b53a9e2008-04-28 02:15:34 -07001003 fsl_diu_check_var(&info->var, info);
York Sunae5591e2008-08-15 00:40:28 -07001004 fsl_diu_set_aoi(info);
York Sun9b53a9e2008-04-28 02:15:34 -07001005 break;
1006 case MFB_GET_AOID:
1007 aoi_d.x_aoi_d = mfbi->x_aoi_d;
1008 aoi_d.y_aoi_d = mfbi->y_aoi_d;
1009 if (copy_to_user(buf, &aoi_d, sizeof(aoi_d)))
1010 return -EFAULT;
York Sun9b53a9e2008-04-28 02:15:34 -07001011 break;
1012 case MFB_GET_ALPHA:
1013 global_alpha = mfbi->g_alpha;
1014 if (copy_to_user(buf, &global_alpha, sizeof(global_alpha)))
1015 return -EFAULT;
York Sun9b53a9e2008-04-28 02:15:34 -07001016 break;
1017 case MFB_SET_ALPHA:
1018 /* set panel information */
1019 if (copy_from_user(&global_alpha, buf, sizeof(global_alpha)))
1020 return -EFAULT;
1021 ad->src_size_g_alpha = (ad->src_size_g_alpha & (~0xff)) |
1022 (global_alpha & 0xff);
1023 mfbi->g_alpha = global_alpha;
York Sun9b53a9e2008-04-28 02:15:34 -07001024 break;
1025 case MFB_SET_CHROMA_KEY:
1026 /* set panel winformation */
1027 if (copy_from_user(&ck, buf, sizeof(ck)))
1028 return -EFAULT;
1029
1030 if (ck.enable &&
1031 (ck.red_max < ck.red_min ||
1032 ck.green_max < ck.green_min ||
1033 ck.blue_max < ck.blue_min))
1034 return -EINVAL;
1035
1036 if (!ck.enable) {
1037 ad->ckmax_r = 0;
1038 ad->ckmax_g = 0;
1039 ad->ckmax_b = 0;
1040 ad->ckmin_r = 255;
1041 ad->ckmin_g = 255;
1042 ad->ckmin_b = 255;
1043 } else {
1044 ad->ckmax_r = ck.red_max;
1045 ad->ckmax_g = ck.green_max;
1046 ad->ckmax_b = ck.blue_max;
1047 ad->ckmin_r = ck.red_min;
1048 ad->ckmin_g = ck.green_min;
1049 ad->ckmin_b = ck.blue_min;
1050 }
York Sun9b53a9e2008-04-28 02:15:34 -07001051 break;
1052 case FBIOGET_GWINFO:
1053 if (mfbi->type == MFB_TYPE_OFF)
1054 return -ENODEV;
1055 /* get graphic window information */
1056 if (copy_to_user(buf, ad, sizeof(*ad)))
1057 return -EFAULT;
1058 break;
1059 case FBIOGET_HWCINFO:
1060 pr_debug("FBIOGET_HWCINFO:0x%08x\n", FBIOGET_HWCINFO);
1061 break;
1062 case FBIOPUT_MODEINFO:
1063 pr_debug("FBIOPUT_MODEINFO:0x%08x\n", FBIOPUT_MODEINFO);
1064 break;
1065 case FBIOGET_DISPINFO:
1066 pr_debug("FBIOGET_DISPINFO:0x%08x\n", FBIOGET_DISPINFO);
1067 break;
1068
1069 default:
Timur Tabi154152a2011-09-15 16:44:47 -05001070 dev_err(info->dev, "unknown ioctl command (0x%08X)\n", cmd);
York Sun9b53a9e2008-04-28 02:15:34 -07001071 return -ENOIOCTLCMD;
1072 }
1073
1074 return 0;
1075}
1076
1077/* turn on fb if count == 1
1078 */
1079static int fsl_diu_open(struct fb_info *info, int user)
1080{
1081 struct mfb_info *mfbi = info->par;
1082 int res = 0;
1083
Anatolij Gustschin4b5006e2010-07-23 04:00:37 +00001084 /* free boot splash memory on first /dev/fb0 open */
1085 if (!mfbi->index && diu_ops.release_bootmem)
1086 diu_ops.release_bootmem();
1087
York Sun9b53a9e2008-04-28 02:15:34 -07001088 spin_lock(&diu_lock);
1089 mfbi->count++;
1090 if (mfbi->count == 1) {
York Sun9b53a9e2008-04-28 02:15:34 -07001091 fsl_diu_check_var(&info->var, info);
1092 res = fsl_diu_set_par(info);
1093 if (res < 0)
1094 mfbi->count--;
1095 else {
1096 res = fsl_diu_enable_panel(info);
1097 if (res < 0)
1098 mfbi->count--;
1099 }
1100 }
1101
1102 spin_unlock(&diu_lock);
1103 return res;
1104}
1105
1106/* turn off fb if count == 0
1107 */
1108static int fsl_diu_release(struct fb_info *info, int user)
1109{
1110 struct mfb_info *mfbi = info->par;
1111 int res = 0;
1112
1113 spin_lock(&diu_lock);
1114 mfbi->count--;
1115 if (mfbi->count == 0) {
York Sun9b53a9e2008-04-28 02:15:34 -07001116 res = fsl_diu_disable_panel(info);
1117 if (res < 0)
1118 mfbi->count++;
1119 }
1120 spin_unlock(&diu_lock);
1121 return res;
1122}
1123
1124static struct fb_ops fsl_diu_ops = {
1125 .owner = THIS_MODULE,
1126 .fb_check_var = fsl_diu_check_var,
1127 .fb_set_par = fsl_diu_set_par,
1128 .fb_setcolreg = fsl_diu_setcolreg,
1129 .fb_blank = fsl_diu_blank,
1130 .fb_pan_display = fsl_diu_pan_display,
1131 .fb_fillrect = cfb_fillrect,
1132 .fb_copyarea = cfb_copyarea,
1133 .fb_imageblit = cfb_imageblit,
1134 .fb_ioctl = fsl_diu_ioctl,
1135 .fb_open = fsl_diu_open,
1136 .fb_release = fsl_diu_release,
1137};
1138
1139static int init_fbinfo(struct fb_info *info)
1140{
1141 struct mfb_info *mfbi = info->par;
1142
1143 info->device = NULL;
1144 info->var.activate = FB_ACTIVATE_NOW;
1145 info->fbops = &fsl_diu_ops;
1146 info->flags = FBINFO_FLAG_DEFAULT;
1147 info->pseudo_palette = &mfbi->pseudo_palette;
1148
1149 /* Allocate colormap */
1150 fb_alloc_cmap(&info->cmap, 16, 0);
1151 return 0;
1152}
1153
Anton Vorontsov05946bc2008-07-04 09:59:38 -07001154static int __devinit install_fb(struct fb_info *info)
York Sun9b53a9e2008-04-28 02:15:34 -07001155{
1156 int rc;
1157 struct mfb_info *mfbi = info->par;
1158 const char *aoi_mode, *init_aoi_mode = "320x240";
Anatolij Gustschin8b856f02010-07-23 04:00:39 +00001159 struct fb_videomode *db = fsl_diu_mode_db;
1160 unsigned int dbsize = ARRAY_SIZE(fsl_diu_mode_db);
1161 int has_default_mode = 1;
York Sun9b53a9e2008-04-28 02:15:34 -07001162
1163 if (init_fbinfo(info))
1164 return -EINVAL;
1165
Anatolij Gustschin8b856f02010-07-23 04:00:39 +00001166 if (mfbi->index == 0) { /* plane 0 */
1167 if (mfbi->edid_data) {
1168 /* Now build modedb from EDID */
1169 fb_edid_to_monspecs(mfbi->edid_data, &info->monspecs);
1170 fb_videomode_to_modelist(info->monspecs.modedb,
1171 info->monspecs.modedb_len,
1172 &info->modelist);
1173 db = info->monspecs.modedb;
1174 dbsize = info->monspecs.modedb_len;
1175 }
York Sun9b53a9e2008-04-28 02:15:34 -07001176 aoi_mode = fb_mode;
Anatolij Gustschin8b856f02010-07-23 04:00:39 +00001177 } else {
York Sun9b53a9e2008-04-28 02:15:34 -07001178 aoi_mode = init_aoi_mode;
Anatolij Gustschin8b856f02010-07-23 04:00:39 +00001179 }
Anatolij Gustschin8b856f02010-07-23 04:00:39 +00001180 rc = fb_find_mode(&info->var, info, aoi_mode, db, dbsize,
1181 &fsl_diu_default_mode, default_bpp);
Timur Tabi154152a2011-09-15 16:44:47 -05001182 if (!rc) {
Anatolij Gustschin8b856f02010-07-23 04:00:39 +00001183 /*
1184 * For plane 0 we continue and look into
1185 * driver's internal modedb.
1186 */
1187 if (mfbi->index == 0 && mfbi->edid_data)
1188 has_default_mode = 0;
1189 else
1190 return -EINVAL;
York Sun9b53a9e2008-04-28 02:15:34 -07001191 }
1192
Anatolij Gustschin8b856f02010-07-23 04:00:39 +00001193 if (!has_default_mode) {
1194 rc = fb_find_mode(&info->var, info, aoi_mode, fsl_diu_mode_db,
1195 ARRAY_SIZE(fsl_diu_mode_db),
1196 &fsl_diu_default_mode,
1197 default_bpp);
1198 if (rc > 0 && rc < 5)
1199 has_default_mode = 1;
1200 }
1201
1202 /* Still not found, use preferred mode from database if any */
1203 if (!has_default_mode && info->monspecs.modedb) {
1204 struct fb_monspecs *specs = &info->monspecs;
1205 struct fb_videomode *modedb = &specs->modedb[0];
1206
1207 /*
1208 * Get preferred timing. If not found,
1209 * first mode in database will be used.
1210 */
1211 if (specs->misc & FB_MISC_1ST_DETAIL) {
1212 int i;
1213
1214 for (i = 0; i < specs->modedb_len; i++) {
1215 if (specs->modedb[i].flag & FB_MODE_IS_FIRST) {
1216 modedb = &specs->modedb[i];
1217 break;
1218 }
1219 }
1220 }
1221
1222 info->var.bits_per_pixel = default_bpp;
1223 fb_videomode_to_var(&info->var, modedb);
1224 }
1225
York Sun9b53a9e2008-04-28 02:15:34 -07001226 if (mfbi->type == MFB_TYPE_OFF)
1227 mfbi->blank = FB_BLANK_NORMAL;
1228 else
1229 mfbi->blank = FB_BLANK_UNBLANK;
1230
1231 if (fsl_diu_check_var(&info->var, info)) {
Timur Tabi154152a2011-09-15 16:44:47 -05001232 dev_err(info->dev, "fsl_diu_check_var failed\n");
York Sun9b53a9e2008-04-28 02:15:34 -07001233 fb_dealloc_cmap(&info->cmap);
1234 return -EINVAL;
1235 }
1236
York Sun9b53a9e2008-04-28 02:15:34 -07001237 if (register_framebuffer(info) < 0) {
Timur Tabi154152a2011-09-15 16:44:47 -05001238 dev_err(info->dev, "register_framebuffer failed\n");
York Sun9b53a9e2008-04-28 02:15:34 -07001239 unmap_video_memory(info);
1240 fb_dealloc_cmap(&info->cmap);
1241 return -EINVAL;
1242 }
1243
1244 mfbi->registered = 1;
Timur Tabi154152a2011-09-15 16:44:47 -05001245 dev_info(info->dev, "%s registered successfully\n", mfbi->id);
York Sun9b53a9e2008-04-28 02:15:34 -07001246
1247 return 0;
1248}
1249
Anton Vorontsov05946bc2008-07-04 09:59:38 -07001250static void uninstall_fb(struct fb_info *info)
York Sun9b53a9e2008-04-28 02:15:34 -07001251{
1252 struct mfb_info *mfbi = info->par;
1253
1254 if (!mfbi->registered)
1255 return;
1256
Anatolij Gustschin8b856f02010-07-23 04:00:39 +00001257 if (mfbi->index == 0)
1258 kfree(mfbi->edid_data);
1259
York Sun9b53a9e2008-04-28 02:15:34 -07001260 unregister_framebuffer(info);
1261 unmap_video_memory(info);
1262 if (&info->cmap)
1263 fb_dealloc_cmap(&info->cmap);
1264
1265 mfbi->registered = 0;
1266}
1267
1268static irqreturn_t fsl_diu_isr(int irq, void *dev_id)
1269{
1270 struct diu *hw = dr.diu_reg;
1271 unsigned int status = in_be32(&hw->int_status);
1272
1273 if (status) {
1274 /* This is the workaround for underrun */
1275 if (status & INT_UNDRUN) {
1276 out_be32(&hw->diu_mode, 0);
York Sun9b53a9e2008-04-28 02:15:34 -07001277 udelay(1);
1278 out_be32(&hw->diu_mode, 1);
1279 }
1280#if defined(CONFIG_NOT_COHERENT_CACHE)
1281 else if (status & INT_VSYNC) {
1282 unsigned int i;
Timur Tabi4a85dc8b2011-09-15 16:44:46 -05001283
York Sun9b53a9e2008-04-28 02:15:34 -07001284 for (i = 0; i < coherence_data_size;
1285 i += d_cache_line_size)
1286 __asm__ __volatile__ (
1287 "dcbz 0, %[input]"
1288 ::[input]"r"(&coherence_data[i]));
1289 }
1290#endif
1291 return IRQ_HANDLED;
1292 }
1293 return IRQ_NONE;
1294}
1295
1296static int request_irq_local(int irq)
1297{
1298 unsigned long status, ints;
1299 struct diu *hw;
1300 int ret;
1301
1302 hw = dr.diu_reg;
1303
1304 /* Read to clear the status */
1305 status = in_be32(&hw->int_status);
1306
Anton Vorontsov05946bc2008-07-04 09:59:38 -07001307 ret = request_irq(irq, fsl_diu_isr, 0, "diu", NULL);
Timur Tabi154152a2011-09-15 16:44:47 -05001308 if (!ret) {
York Sun9b53a9e2008-04-28 02:15:34 -07001309 ints = INT_PARERR | INT_LS_BF_VS;
1310#if !defined(CONFIG_NOT_COHERENT_CACHE)
1311 ints |= INT_VSYNC;
1312#endif
Timur Tabi4a85dc8b2011-09-15 16:44:46 -05001313
York Sun9b53a9e2008-04-28 02:15:34 -07001314 if (dr.mode == MFB_MODE2 || dr.mode == MFB_MODE3)
1315 ints |= INT_VSYNC_WB;
1316
1317 /* Read to clear the status */
1318 status = in_be32(&hw->int_status);
1319 out_be32(&hw->int_mask, ints);
1320 }
Timur Tabi4a85dc8b2011-09-15 16:44:46 -05001321
York Sun9b53a9e2008-04-28 02:15:34 -07001322 return ret;
1323}
1324
1325static void free_irq_local(int irq)
1326{
1327 struct diu *hw = dr.diu_reg;
1328
1329 /* Disable all LCDC interrupt */
1330 out_be32(&hw->int_mask, 0x1f);
1331
Anton Vorontsov05946bc2008-07-04 09:59:38 -07001332 free_irq(irq, NULL);
York Sun9b53a9e2008-04-28 02:15:34 -07001333}
1334
1335#ifdef CONFIG_PM
1336/*
1337 * Power management hooks. Note that we won't be called from IRQ context,
1338 * unlike the blank functions above, so we may sleep.
1339 */
Grant Likely2dc11582010-08-06 09:25:50 -06001340static int fsl_diu_suspend(struct platform_device *ofdev, pm_message_t state)
York Sun9b53a9e2008-04-28 02:15:34 -07001341{
1342 struct fsl_diu_data *machine_data;
1343
Takashi Iwai48948a32008-07-08 18:41:17 +02001344 machine_data = dev_get_drvdata(&ofdev->dev);
York Sun9b53a9e2008-04-28 02:15:34 -07001345 disable_lcdc(machine_data->fsl_diu_info[0]);
1346
1347 return 0;
1348}
1349
Grant Likely2dc11582010-08-06 09:25:50 -06001350static int fsl_diu_resume(struct platform_device *ofdev)
York Sun9b53a9e2008-04-28 02:15:34 -07001351{
1352 struct fsl_diu_data *machine_data;
1353
Takashi Iwai48948a32008-07-08 18:41:17 +02001354 machine_data = dev_get_drvdata(&ofdev->dev);
York Sun9b53a9e2008-04-28 02:15:34 -07001355 enable_lcdc(machine_data->fsl_diu_info[0]);
1356
1357 return 0;
1358}
1359
1360#else
1361#define fsl_diu_suspend NULL
1362#define fsl_diu_resume NULL
1363#endif /* CONFIG_PM */
1364
1365/* Align to 64-bit(8-byte), 32-byte, etc. */
Anton Vorontsovf3791882009-04-04 22:31:20 +04001366static int allocate_buf(struct device *dev, struct diu_addr *buf, u32 size,
1367 u32 bytes_align)
York Sun9b53a9e2008-04-28 02:15:34 -07001368{
1369 u32 offset, ssize;
1370 u32 mask;
1371 dma_addr_t paddr = 0;
1372
1373 ssize = size + bytes_align;
Anton Vorontsovf3791882009-04-04 22:31:20 +04001374 buf->vaddr = dma_alloc_coherent(dev, ssize, &paddr, GFP_DMA |
Anton Vorontsov05946bc2008-07-04 09:59:38 -07001375 __GFP_ZERO);
York Sun9b53a9e2008-04-28 02:15:34 -07001376 if (!buf->vaddr)
1377 return -ENOMEM;
1378
1379 buf->paddr = (__u32) paddr;
1380
1381 mask = bytes_align - 1;
1382 offset = (u32)buf->paddr & mask;
1383 if (offset) {
1384 buf->offset = bytes_align - offset;
1385 buf->paddr = (u32)buf->paddr + offset;
1386 } else
1387 buf->offset = 0;
Timur Tabi4a85dc8b2011-09-15 16:44:46 -05001388
York Sun9b53a9e2008-04-28 02:15:34 -07001389 return 0;
1390}
1391
Anton Vorontsovf3791882009-04-04 22:31:20 +04001392static void free_buf(struct device *dev, struct diu_addr *buf, u32 size,
1393 u32 bytes_align)
York Sun9b53a9e2008-04-28 02:15:34 -07001394{
Timur Tabi4a85dc8b2011-09-15 16:44:46 -05001395 dma_free_coherent(dev, size + bytes_align, buf->vaddr,
1396 buf->paddr - buf->offset);
York Sun9b53a9e2008-04-28 02:15:34 -07001397}
1398
1399static ssize_t store_monitor(struct device *device,
1400 struct device_attribute *attr, const char *buf, size_t count)
1401{
Timur Tabi7653aaa2011-07-09 15:38:14 -05001402 enum fsl_diu_monitor_port old_monitor_port;
York Sun9b53a9e2008-04-28 02:15:34 -07001403 struct fsl_diu_data *machine_data =
1404 container_of(attr, struct fsl_diu_data, dev_attr);
1405
York Sun9b53a9e2008-04-28 02:15:34 -07001406 old_monitor_port = machine_data->monitor_port;
Timur Tabi7653aaa2011-07-09 15:38:14 -05001407 machine_data->monitor_port = fsl_diu_name_to_port(buf);
York Sun9b53a9e2008-04-28 02:15:34 -07001408
1409 if (old_monitor_port != machine_data->monitor_port) {
1410 /* All AOIs need adjust pixel format
1411 * fsl_diu_set_par only change the pixsel format here
1412 * unlikely to fail. */
1413 fsl_diu_set_par(machine_data->fsl_diu_info[0]);
1414 fsl_diu_set_par(machine_data->fsl_diu_info[1]);
1415 fsl_diu_set_par(machine_data->fsl_diu_info[2]);
1416 fsl_diu_set_par(machine_data->fsl_diu_info[3]);
1417 fsl_diu_set_par(machine_data->fsl_diu_info[4]);
1418 }
1419 return count;
1420}
1421
1422static ssize_t show_monitor(struct device *device,
1423 struct device_attribute *attr, char *buf)
1424{
1425 struct fsl_diu_data *machine_data =
1426 container_of(attr, struct fsl_diu_data, dev_attr);
Timur Tabi7653aaa2011-07-09 15:38:14 -05001427
1428 switch (machine_data->monitor_port) {
1429 case FSL_DIU_PORT_DVI:
1430 return sprintf(buf, "DVI\n");
1431 case FSL_DIU_PORT_LVDS:
1432 return sprintf(buf, "Single-link LVDS\n");
1433 case FSL_DIU_PORT_DLVDS:
1434 return sprintf(buf, "Dual-link LVDS\n");
1435 }
1436
1437 return 0;
York Sun9b53a9e2008-04-28 02:15:34 -07001438}
1439
Grant Likely28541d02011-02-22 21:07:43 -07001440static int __devinit fsl_diu_probe(struct platform_device *ofdev)
York Sun9b53a9e2008-04-28 02:15:34 -07001441{
Grant Likely61c7a082010-04-13 16:12:29 -07001442 struct device_node *np = ofdev->dev.of_node;
York Sun9b53a9e2008-04-28 02:15:34 -07001443 struct mfb_info *mfbi;
1444 phys_addr_t dummy_ad_addr;
1445 int ret, i, error = 0;
1446 struct resource res;
1447 struct fsl_diu_data *machine_data;
Anatolij Gustschin4b5006e2010-07-23 04:00:37 +00001448 int diu_mode;
York Sun9b53a9e2008-04-28 02:15:34 -07001449
1450 machine_data = kzalloc(sizeof(struct fsl_diu_data), GFP_KERNEL);
1451 if (!machine_data)
1452 return -ENOMEM;
1453
1454 for (i = 0; i < ARRAY_SIZE(machine_data->fsl_diu_info); i++) {
1455 machine_data->fsl_diu_info[i] =
1456 framebuffer_alloc(sizeof(struct mfb_info), &ofdev->dev);
1457 if (!machine_data->fsl_diu_info[i]) {
1458 dev_err(&ofdev->dev, "cannot allocate memory\n");
1459 ret = -ENOMEM;
1460 goto error2;
1461 }
1462 mfbi = machine_data->fsl_diu_info[i]->par;
1463 memcpy(mfbi, &mfb_template[i], sizeof(struct mfb_info));
1464 mfbi->parent = machine_data;
Anatolij Gustschin8b856f02010-07-23 04:00:39 +00001465
1466 if (mfbi->index == 0) {
1467 const u8 *prop;
1468 int len;
1469
1470 /* Get EDID */
1471 prop = of_get_property(np, "edid", &len);
1472 if (prop && len == EDID_LENGTH)
1473 mfbi->edid_data = kmemdup(prop, EDID_LENGTH,
1474 GFP_KERNEL);
1475 }
York Sun9b53a9e2008-04-28 02:15:34 -07001476 }
1477
1478 ret = of_address_to_resource(np, 0, &res);
1479 if (ret) {
1480 dev_err(&ofdev->dev, "could not obtain DIU address\n");
1481 goto error;
1482 }
1483 if (!res.start) {
1484 dev_err(&ofdev->dev, "invalid DIU address\n");
1485 goto error;
1486 }
York Sun9b53a9e2008-04-28 02:15:34 -07001487
1488 dr.diu_reg = ioremap(res.start, sizeof(struct diu));
1489 if (!dr.diu_reg) {
1490 dev_err(&ofdev->dev, "Err: can't map DIU registers!\n");
1491 ret = -EFAULT;
1492 goto error2;
1493 }
1494
Anatolij Gustschin4b5006e2010-07-23 04:00:37 +00001495 diu_mode = in_be32(&dr.diu_reg->diu_mode);
1496 if (diu_mode != MFB_MODE1)
1497 out_be32(&dr.diu_reg->diu_mode, 0); /* disable DIU */
York Sun9b53a9e2008-04-28 02:15:34 -07001498
1499 /* Get the IRQ of the DIU */
1500 machine_data->irq = irq_of_parse_and_map(np, 0);
1501
1502 if (!machine_data->irq) {
1503 dev_err(&ofdev->dev, "could not get DIU IRQ\n");
1504 ret = -EINVAL;
1505 goto error;
1506 }
1507 machine_data->monitor_port = monitor_port;
1508
1509 /* Area descriptor memory pool aligns to 64-bit boundary */
Anton Vorontsovf3791882009-04-04 22:31:20 +04001510 if (allocate_buf(&ofdev->dev, &pool.ad,
1511 sizeof(struct diu_ad) * FSL_AOI_NUM, 8))
York Sun9b53a9e2008-04-28 02:15:34 -07001512 return -ENOMEM;
1513
1514 /* Get memory for Gamma Table - 32-byte aligned memory */
Anton Vorontsovf3791882009-04-04 22:31:20 +04001515 if (allocate_buf(&ofdev->dev, &pool.gamma, 768, 32)) {
York Sun9b53a9e2008-04-28 02:15:34 -07001516 ret = -ENOMEM;
1517 goto error;
1518 }
1519
1520 /* For performance, cursor bitmap buffer aligns to 32-byte boundary */
Anton Vorontsovf3791882009-04-04 22:31:20 +04001521 if (allocate_buf(&ofdev->dev, &pool.cursor, MAX_CURS * MAX_CURS * 2,
1522 32)) {
York Sun9b53a9e2008-04-28 02:15:34 -07001523 ret = -ENOMEM;
1524 goto error;
1525 }
1526
1527 i = ARRAY_SIZE(machine_data->fsl_diu_info);
1528 machine_data->dummy_ad = (struct diu_ad *)
1529 ((u32)pool.ad.vaddr + pool.ad.offset) + i;
1530 machine_data->dummy_ad->paddr = pool.ad.paddr +
1531 i * sizeof(struct diu_ad);
1532 machine_data->dummy_aoi_virt = fsl_diu_alloc(64, &dummy_ad_addr);
1533 if (!machine_data->dummy_aoi_virt) {
1534 ret = -ENOMEM;
1535 goto error;
1536 }
1537 machine_data->dummy_ad->addr = cpu_to_le32(dummy_ad_addr);
1538 machine_data->dummy_ad->pix_fmt = 0x88882317;
1539 machine_data->dummy_ad->src_size_g_alpha = cpu_to_le32((4 << 12) | 4);
1540 machine_data->dummy_ad->aoi_size = cpu_to_le32((4 << 16) | 2);
1541 machine_data->dummy_ad->offset_xyi = 0;
1542 machine_data->dummy_ad->offset_xyd = 0;
1543 machine_data->dummy_ad->next_ad = 0;
1544
Anatolij Gustschin4b5006e2010-07-23 04:00:37 +00001545 /*
1546 * Let DIU display splash screen if it was pre-initialized
1547 * by the bootloader, set dummy area descriptor otherwise.
1548 */
1549 if (diu_mode != MFB_MODE1)
1550 out_be32(&dr.diu_reg->desc[0], machine_data->dummy_ad->paddr);
1551
York Sun9b53a9e2008-04-28 02:15:34 -07001552 out_be32(&dr.diu_reg->desc[1], machine_data->dummy_ad->paddr);
1553 out_be32(&dr.diu_reg->desc[2], machine_data->dummy_ad->paddr);
1554
1555 for (i = 0; i < ARRAY_SIZE(machine_data->fsl_diu_info); i++) {
1556 machine_data->fsl_diu_info[i]->fix.smem_start = 0;
1557 mfbi = machine_data->fsl_diu_info[i]->par;
1558 mfbi->ad = (struct diu_ad *)((u32)pool.ad.vaddr
1559 + pool.ad.offset) + i;
1560 mfbi->ad->paddr = pool.ad.paddr + i * sizeof(struct diu_ad);
1561 ret = install_fb(machine_data->fsl_diu_info[i]);
1562 if (ret) {
1563 dev_err(&ofdev->dev,
1564 "Failed to register framebuffer %d\n",
1565 i);
1566 goto error;
1567 }
1568 }
1569
1570 if (request_irq_local(machine_data->irq)) {
1571 dev_err(machine_data->fsl_diu_info[0]->dev,
1572 "could not request irq for diu.");
1573 goto error;
1574 }
1575
Wolfram Sang12765512010-04-06 14:34:52 -07001576 sysfs_attr_init(&machine_data->dev_attr.attr);
York Sun9b53a9e2008-04-28 02:15:34 -07001577 machine_data->dev_attr.attr.name = "monitor";
1578 machine_data->dev_attr.attr.mode = S_IRUGO|S_IWUSR;
1579 machine_data->dev_attr.show = show_monitor;
1580 machine_data->dev_attr.store = store_monitor;
1581 error = device_create_file(machine_data->fsl_diu_info[0]->dev,
1582 &machine_data->dev_attr);
1583 if (error) {
1584 dev_err(machine_data->fsl_diu_info[0]->dev,
1585 "could not create sysfs %s file\n",
1586 machine_data->dev_attr.attr.name);
1587 }
1588
1589 dev_set_drvdata(&ofdev->dev, machine_data);
1590 return 0;
1591
1592error:
1593 for (i = ARRAY_SIZE(machine_data->fsl_diu_info);
1594 i > 0; i--)
1595 uninstall_fb(machine_data->fsl_diu_info[i - 1]);
1596 if (pool.ad.vaddr)
Anton Vorontsovf3791882009-04-04 22:31:20 +04001597 free_buf(&ofdev->dev, &pool.ad,
1598 sizeof(struct diu_ad) * FSL_AOI_NUM, 8);
York Sun9b53a9e2008-04-28 02:15:34 -07001599 if (pool.gamma.vaddr)
Anton Vorontsovf3791882009-04-04 22:31:20 +04001600 free_buf(&ofdev->dev, &pool.gamma, 768, 32);
York Sun9b53a9e2008-04-28 02:15:34 -07001601 if (pool.cursor.vaddr)
Anton Vorontsovf3791882009-04-04 22:31:20 +04001602 free_buf(&ofdev->dev, &pool.cursor, MAX_CURS * MAX_CURS * 2,
1603 32);
York Sun9b53a9e2008-04-28 02:15:34 -07001604 if (machine_data->dummy_aoi_virt)
1605 fsl_diu_free(machine_data->dummy_aoi_virt, 64);
1606 iounmap(dr.diu_reg);
1607
1608error2:
1609 for (i = 0; i < ARRAY_SIZE(machine_data->fsl_diu_info); i++)
1610 if (machine_data->fsl_diu_info[i])
1611 framebuffer_release(machine_data->fsl_diu_info[i]);
1612 kfree(machine_data);
1613
1614 return ret;
1615}
1616
1617
Grant Likely2dc11582010-08-06 09:25:50 -06001618static int fsl_diu_remove(struct platform_device *ofdev)
York Sun9b53a9e2008-04-28 02:15:34 -07001619{
1620 struct fsl_diu_data *machine_data;
1621 int i;
1622
1623 machine_data = dev_get_drvdata(&ofdev->dev);
1624 disable_lcdc(machine_data->fsl_diu_info[0]);
1625 free_irq_local(machine_data->irq);
1626 for (i = ARRAY_SIZE(machine_data->fsl_diu_info); i > 0; i--)
1627 uninstall_fb(machine_data->fsl_diu_info[i - 1]);
1628 if (pool.ad.vaddr)
Anton Vorontsovf3791882009-04-04 22:31:20 +04001629 free_buf(&ofdev->dev, &pool.ad,
1630 sizeof(struct diu_ad) * FSL_AOI_NUM, 8);
York Sun9b53a9e2008-04-28 02:15:34 -07001631 if (pool.gamma.vaddr)
Anton Vorontsovf3791882009-04-04 22:31:20 +04001632 free_buf(&ofdev->dev, &pool.gamma, 768, 32);
York Sun9b53a9e2008-04-28 02:15:34 -07001633 if (pool.cursor.vaddr)
Anton Vorontsovf3791882009-04-04 22:31:20 +04001634 free_buf(&ofdev->dev, &pool.cursor, MAX_CURS * MAX_CURS * 2,
1635 32);
York Sun9b53a9e2008-04-28 02:15:34 -07001636 if (machine_data->dummy_aoi_virt)
1637 fsl_diu_free(machine_data->dummy_aoi_virt, 64);
1638 iounmap(dr.diu_reg);
1639 for (i = 0; i < ARRAY_SIZE(machine_data->fsl_diu_info); i++)
1640 if (machine_data->fsl_diu_info[i])
1641 framebuffer_release(machine_data->fsl_diu_info[i]);
1642 kfree(machine_data);
1643
1644 return 0;
1645}
1646
1647#ifndef MODULE
1648static int __init fsl_diu_setup(char *options)
1649{
1650 char *opt;
1651 unsigned long val;
1652
1653 if (!options || !*options)
1654 return 0;
1655
1656 while ((opt = strsep(&options, ",")) != NULL) {
1657 if (!*opt)
1658 continue;
1659 if (!strncmp(opt, "monitor=", 8)) {
Timur Tabi7653aaa2011-07-09 15:38:14 -05001660 monitor_port = fsl_diu_name_to_port(opt + 8);
York Sun9b53a9e2008-04-28 02:15:34 -07001661 } else if (!strncmp(opt, "bpp=", 4)) {
1662 if (!strict_strtoul(opt + 4, 10, &val))
1663 default_bpp = val;
1664 } else
1665 fb_mode = opt;
1666 }
1667
1668 return 0;
1669}
1670#endif
1671
1672static struct of_device_id fsl_diu_match[] = {
Anatolij Gustschind24720a2010-02-17 07:33:22 -07001673#ifdef CONFIG_PPC_MPC512x
1674 {
1675 .compatible = "fsl,mpc5121-diu",
1676 },
1677#endif
York Sun9b53a9e2008-04-28 02:15:34 -07001678 {
1679 .compatible = "fsl,diu",
1680 },
1681 {}
1682};
1683MODULE_DEVICE_TABLE(of, fsl_diu_match);
1684
Grant Likely28541d02011-02-22 21:07:43 -07001685static struct platform_driver fsl_diu_driver = {
Grant Likely40182942010-04-13 16:13:02 -07001686 .driver = {
1687 .name = "fsl_diu",
1688 .owner = THIS_MODULE,
1689 .of_match_table = fsl_diu_match,
1690 },
York Sun9b53a9e2008-04-28 02:15:34 -07001691 .probe = fsl_diu_probe,
1692 .remove = fsl_diu_remove,
1693 .suspend = fsl_diu_suspend,
1694 .resume = fsl_diu_resume,
1695};
1696
1697static int __init fsl_diu_init(void)
1698{
1699#ifdef CONFIG_NOT_COHERENT_CACHE
1700 struct device_node *np;
1701 const u32 *prop;
1702#endif
1703 int ret;
1704#ifndef MODULE
1705 char *option;
1706
1707 /*
1708 * For kernel boot options (in 'video=xxxfb:<options>' format)
1709 */
1710 if (fb_get_options("fslfb", &option))
1711 return -ENODEV;
1712 fsl_diu_setup(option);
Timur Tabi7653aaa2011-07-09 15:38:14 -05001713#else
1714 monitor_port = fsl_diu_name_to_port(monitor_string);
York Sun9b53a9e2008-04-28 02:15:34 -07001715#endif
Timur Tabi154152a2011-09-15 16:44:47 -05001716 pr_info("Freescale Display Interface Unit (DIU) framebuffer driver\n");
York Sun9b53a9e2008-04-28 02:15:34 -07001717
1718#ifdef CONFIG_NOT_COHERENT_CACHE
1719 np = of_find_node_by_type(NULL, "cpu");
1720 if (!np) {
Timur Tabi154152a2011-09-15 16:44:47 -05001721 pr_err("fsl-diu-fb: can't find 'cpu' device node\n");
York Sun9b53a9e2008-04-28 02:15:34 -07001722 return -ENODEV;
1723 }
1724
1725 prop = of_get_property(np, "d-cache-size", NULL);
Julia Lawall5394ba02008-08-05 13:01:28 -07001726 if (prop == NULL) {
Timur Tabi154152a2011-09-15 16:44:47 -05001727 pr_err("fsl-diu-fb: missing 'd-cache-size' property' "
1728 "in 'cpu' node\n");
Julia Lawall5394ba02008-08-05 13:01:28 -07001729 of_node_put(np);
York Sun9b53a9e2008-04-28 02:15:34 -07001730 return -ENODEV;
Julia Lawall5394ba02008-08-05 13:01:28 -07001731 }
York Sun9b53a9e2008-04-28 02:15:34 -07001732
Timur Tabi4a85dc8b2011-09-15 16:44:46 -05001733 /*
1734 * Freescale PLRU requires 13/8 times the cache size to do a proper
1735 * displacement flush
York Sun9b53a9e2008-04-28 02:15:34 -07001736 */
1737 coherence_data_size = *prop * 13;
1738 coherence_data_size /= 8;
1739
1740 prop = of_get_property(np, "d-cache-line-size", NULL);
Julia Lawall5394ba02008-08-05 13:01:28 -07001741 if (prop == NULL) {
Timur Tabi154152a2011-09-15 16:44:47 -05001742 pr_err("fsl-diu-fb: missing 'd-cache-line-size' property' "
1743 "in 'cpu' node\n");
Julia Lawall5394ba02008-08-05 13:01:28 -07001744 of_node_put(np);
York Sun9b53a9e2008-04-28 02:15:34 -07001745 return -ENODEV;
Julia Lawall5394ba02008-08-05 13:01:28 -07001746 }
York Sun9b53a9e2008-04-28 02:15:34 -07001747 d_cache_line_size = *prop;
1748
1749 of_node_put(np);
1750 coherence_data = vmalloc(coherence_data_size);
1751 if (!coherence_data)
1752 return -ENOMEM;
1753#endif
Timur Tabi4a85dc8b2011-09-15 16:44:46 -05001754
Grant Likely28541d02011-02-22 21:07:43 -07001755 ret = platform_driver_register(&fsl_diu_driver);
York Sun9b53a9e2008-04-28 02:15:34 -07001756 if (ret) {
Timur Tabi154152a2011-09-15 16:44:47 -05001757 pr_err("fsl-diu-fb: failed to register platform driver\n");
York Sun9b53a9e2008-04-28 02:15:34 -07001758#if defined(CONFIG_NOT_COHERENT_CACHE)
1759 vfree(coherence_data);
1760#endif
1761 iounmap(dr.diu_reg);
1762 }
1763 return ret;
1764}
1765
1766static void __exit fsl_diu_exit(void)
1767{
Grant Likely28541d02011-02-22 21:07:43 -07001768 platform_driver_unregister(&fsl_diu_driver);
York Sun9b53a9e2008-04-28 02:15:34 -07001769#if defined(CONFIG_NOT_COHERENT_CACHE)
1770 vfree(coherence_data);
1771#endif
1772}
1773
1774module_init(fsl_diu_init);
1775module_exit(fsl_diu_exit);
1776
1777MODULE_AUTHOR("York Sun <yorksun@freescale.com>");
1778MODULE_DESCRIPTION("Freescale DIU framebuffer driver");
1779MODULE_LICENSE("GPL");
1780
1781module_param_named(mode, fb_mode, charp, 0);
1782MODULE_PARM_DESC(mode,
1783 "Specify resolution as \"<xres>x<yres>[-<bpp>][@<refresh>]\" ");
1784module_param_named(bpp, default_bpp, ulong, 0);
Timur Tabi154152a2011-09-15 16:44:47 -05001785MODULE_PARM_DESC(bpp, "Specify bit-per-pixel if not specified in 'mode'");
Timur Tabi7653aaa2011-07-09 15:38:14 -05001786module_param_named(monitor, monitor_string, charp, 0);
1787MODULE_PARM_DESC(monitor, "Specify the monitor port "
1788 "(\"dvi\", \"lvds\", or \"dlvds\") if supported by the platform");
York Sun9b53a9e2008-04-28 02:15:34 -07001789