blob: 9b3891a6a1875311b1d9dfd51627c04b9cd63ede [file] [log] [blame]
York Sun9b53a9e2008-04-28 02:15:34 -07001/*
2 * Copyright 2008 Freescale Semiconductor, Inc. All Rights Reserved.
3 *
4 * Freescale DIU Frame Buffer device driver
5 *
6 * Authors: Hongjun Chen <hong-jun.chen@freescale.com>
7 * Paul Widmer <paul.widmer@freescale.com>
8 * Srikanth Srinivasan <srikanth.srinivasan@freescale.com>
9 * York Sun <yorksun@freescale.com>
10 *
11 * Based on imxfb.c Copyright (C) 2004 S.Hauer, Pengutronix
12 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version.
17 *
18 */
19
20#include <linux/module.h>
21#include <linux/kernel.h>
22#include <linux/errno.h>
23#include <linux/string.h>
24#include <linux/slab.h>
25#include <linux/fb.h>
26#include <linux/init.h>
27#include <linux/dma-mapping.h>
28#include <linux/platform_device.h>
29#include <linux/interrupt.h>
30#include <linux/clk.h>
31#include <linux/uaccess.h>
32#include <linux/vmalloc.h>
Timur Tabib715f9f2011-09-28 16:19:48 -050033#include <linux/spinlock.h>
York Sun9b53a9e2008-04-28 02:15:34 -070034
York Sun9b53a9e2008-04-28 02:15:34 -070035#include <sysdev/fsl_soc.h>
Anatolij Gustschin0814a972010-07-23 04:00:36 +000036#include <linux/fsl-diu-fb.h>
Anatolij Gustschin8b856f02010-07-23 04:00:39 +000037#include "edid.h"
York Sun9b53a9e2008-04-28 02:15:34 -070038
Timur Tabib715f9f2011-09-28 16:19:48 -050039#define FSL_AOI_NUM 6 /* 5 AOIs and one dummy AOI */
40 /* 1 for plane 0, 2 for plane 1&2 each */
41
42/* HW cursor parameters */
43#define MAX_CURS 32
44
45/* INT_STATUS/INT_MASK field descriptions */
46#define INT_VSYNC 0x01 /* Vsync interrupt */
47#define INT_VSYNC_WB 0x02 /* Vsync interrupt for write back operation */
48#define INT_UNDRUN 0x04 /* Under run exception interrupt */
49#define INT_PARERR 0x08 /* Display parameters error interrupt */
50#define INT_LS_BF_VS 0x10 /* Lines before vsync. interrupt */
51
52/* Panels'operation modes */
53#define MFB_TYPE_OUTPUT 0 /* Panel output to display */
54#define MFB_TYPE_OFF 1 /* Panel off */
55#define MFB_TYPE_WB 2 /* Panel written back to memory */
56#define MFB_TYPE_TEST 3 /* Panel generate color bar */
57
58struct diu_hw {
59 struct diu __iomem *diu_reg;
60 spinlock_t reg_lock;
61 unsigned int mode; /* DIU operation mode */
62};
63
64struct diu_addr {
65 void *vaddr; /* Virtual address */
66 dma_addr_t paddr; /* Physical address */
67 __u32 offset;
68};
69
70struct diu_pool {
71 struct diu_addr ad;
72 struct diu_addr gamma;
73 struct diu_addr pallete;
74 struct diu_addr cursor;
75};
76
York Sun9b53a9e2008-04-28 02:15:34 -070077/*
Timur Tabi63cf8df2011-09-15 16:44:51 -050078 * List of supported video modes
79 *
80 * The first entry is the default video mode
York Sun9b53a9e2008-04-28 02:15:34 -070081 */
York Sun9b53a9e2008-04-28 02:15:34 -070082static struct fb_videomode __devinitdata fsl_diu_mode_db[] = {
83 {
84 .name = "1024x768-60",
85 .refresh = 60,
86 .xres = 1024,
87 .yres = 768,
88 .pixclock = 15385,
89 .left_margin = 160,
90 .right_margin = 24,
91 .upper_margin = 29,
92 .lower_margin = 3,
93 .hsync_len = 136,
94 .vsync_len = 6,
95 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
96 .vmode = FB_VMODE_NONINTERLACED
97 },
98 {
99 .name = "1024x768-70",
100 .refresh = 70,
101 .xres = 1024,
102 .yres = 768,
103 .pixclock = 16886,
104 .left_margin = 3,
105 .right_margin = 3,
106 .upper_margin = 2,
107 .lower_margin = 2,
108 .hsync_len = 40,
109 .vsync_len = 18,
110 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
111 .vmode = FB_VMODE_NONINTERLACED
112 },
113 {
114 .name = "1024x768-75",
115 .refresh = 75,
116 .xres = 1024,
117 .yres = 768,
118 .pixclock = 15009,
119 .left_margin = 3,
120 .right_margin = 3,
121 .upper_margin = 2,
122 .lower_margin = 2,
123 .hsync_len = 80,
124 .vsync_len = 32,
125 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
126 .vmode = FB_VMODE_NONINTERLACED
127 },
128 {
129 .name = "1280x1024-60",
130 .refresh = 60,
131 .xres = 1280,
132 .yres = 1024,
133 .pixclock = 9375,
134 .left_margin = 38,
135 .right_margin = 128,
136 .upper_margin = 2,
137 .lower_margin = 7,
138 .hsync_len = 216,
139 .vsync_len = 37,
140 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
141 .vmode = FB_VMODE_NONINTERLACED
142 },
143 {
144 .name = "1280x1024-70",
145 .refresh = 70,
146 .xres = 1280,
147 .yres = 1024,
148 .pixclock = 9380,
149 .left_margin = 6,
150 .right_margin = 6,
151 .upper_margin = 4,
152 .lower_margin = 4,
153 .hsync_len = 60,
154 .vsync_len = 94,
155 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
156 .vmode = FB_VMODE_NONINTERLACED
157 },
158 {
159 .name = "1280x1024-75",
160 .refresh = 75,
161 .xres = 1280,
162 .yres = 1024,
163 .pixclock = 9380,
164 .left_margin = 6,
165 .right_margin = 6,
166 .upper_margin = 4,
167 .lower_margin = 4,
168 .hsync_len = 60,
169 .vsync_len = 15,
170 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
171 .vmode = FB_VMODE_NONINTERLACED
172 },
173 {
174 .name = "320x240", /* for AOI only */
175 .refresh = 60,
176 .xres = 320,
177 .yres = 240,
178 .pixclock = 15385,
179 .left_margin = 0,
180 .right_margin = 0,
181 .upper_margin = 0,
182 .lower_margin = 0,
183 .hsync_len = 0,
184 .vsync_len = 0,
185 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
186 .vmode = FB_VMODE_NONINTERLACED
187 },
188 {
189 .name = "1280x480-60",
190 .refresh = 60,
191 .xres = 1280,
192 .yres = 480,
193 .pixclock = 18939,
194 .left_margin = 353,
195 .right_margin = 47,
196 .upper_margin = 39,
197 .lower_margin = 4,
198 .hsync_len = 8,
199 .vsync_len = 2,
200 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
201 .vmode = FB_VMODE_NONINTERLACED
202 },
203};
204
205static char *fb_mode = "1024x768-32@60";
206static unsigned long default_bpp = 32;
Timur Tabi7653aaa2011-07-09 15:38:14 -0500207static enum fsl_diu_monitor_port monitor_port;
208static char *monitor_string;
York Sun9b53a9e2008-04-28 02:15:34 -0700209
210#if defined(CONFIG_NOT_COHERENT_CACHE)
211static u8 *coherence_data;
212static size_t coherence_data_size;
213static unsigned int d_cache_line_size;
214#endif
215
216static DEFINE_SPINLOCK(diu_lock);
217
218struct fsl_diu_data {
219 struct fb_info *fsl_diu_info[FSL_AOI_NUM - 1];
220 /*FSL_AOI_NUM has one dummy AOI */
221 struct device_attribute dev_attr;
222 struct diu_ad *dummy_ad;
223 void *dummy_aoi_virt;
224 unsigned int irq;
225 int fb_enabled;
Timur Tabi7653aaa2011-07-09 15:38:14 -0500226 enum fsl_diu_monitor_port monitor_port;
York Sun9b53a9e2008-04-28 02:15:34 -0700227};
228
229struct mfb_info {
230 int index;
231 int type;
232 char *id;
233 int registered;
234 int blank;
235 unsigned long pseudo_palette[16];
236 struct diu_ad *ad;
237 int cursor_reset;
238 unsigned char g_alpha;
239 unsigned int count;
240 int x_aoi_d; /* aoi display x offset to physical screen */
241 int y_aoi_d; /* aoi display y offset to physical screen */
242 struct fsl_diu_data *parent;
Anatolij Gustschin8b856f02010-07-23 04:00:39 +0000243 u8 *edid_data;
York Sun9b53a9e2008-04-28 02:15:34 -0700244};
245
246
247static struct mfb_info mfb_template[] = {
248 { /* AOI 0 for plane 0 */
Timur Tabi4a85dc8b2011-09-15 16:44:46 -0500249 .index = 0,
250 .type = MFB_TYPE_OUTPUT,
251 .id = "Panel0",
252 .registered = 0,
253 .count = 0,
254 .x_aoi_d = 0,
255 .y_aoi_d = 0,
York Sun9b53a9e2008-04-28 02:15:34 -0700256 },
257 { /* AOI 0 for plane 1 */
Timur Tabi4a85dc8b2011-09-15 16:44:46 -0500258 .index = 1,
259 .type = MFB_TYPE_OUTPUT,
260 .id = "Panel1 AOI0",
261 .registered = 0,
262 .g_alpha = 0xff,
263 .count = 0,
264 .x_aoi_d = 0,
265 .y_aoi_d = 0,
York Sun9b53a9e2008-04-28 02:15:34 -0700266 },
267 { /* AOI 1 for plane 1 */
Timur Tabi4a85dc8b2011-09-15 16:44:46 -0500268 .index = 2,
269 .type = MFB_TYPE_OUTPUT,
270 .id = "Panel1 AOI1",
271 .registered = 0,
272 .g_alpha = 0xff,
273 .count = 0,
274 .x_aoi_d = 0,
275 .y_aoi_d = 480,
York Sun9b53a9e2008-04-28 02:15:34 -0700276 },
277 { /* AOI 0 for plane 2 */
Timur Tabi4a85dc8b2011-09-15 16:44:46 -0500278 .index = 3,
279 .type = MFB_TYPE_OUTPUT,
280 .id = "Panel2 AOI0",
281 .registered = 0,
282 .g_alpha = 0xff,
283 .count = 0,
284 .x_aoi_d = 640,
285 .y_aoi_d = 0,
York Sun9b53a9e2008-04-28 02:15:34 -0700286 },
287 { /* AOI 1 for plane 2 */
Timur Tabi4a85dc8b2011-09-15 16:44:46 -0500288 .index = 4,
289 .type = MFB_TYPE_OUTPUT,
290 .id = "Panel2 AOI1",
291 .registered = 0,
292 .g_alpha = 0xff,
293 .count = 0,
294 .x_aoi_d = 640,
295 .y_aoi_d = 480,
York Sun9b53a9e2008-04-28 02:15:34 -0700296 },
297};
298
299static struct diu_hw dr = {
300 .mode = MFB_MODE1,
301 .reg_lock = __SPIN_LOCK_UNLOCKED(diu_hw.reg_lock),
302};
303
304static struct diu_pool pool;
305
Timur Tabi6b51d512008-07-23 21:31:39 -0700306/**
Timur Tabi7653aaa2011-07-09 15:38:14 -0500307 * fsl_diu_name_to_port - convert a port name to a monitor port enum
308 *
309 * Takes the name of a monitor port ("dvi", "lvds", or "dlvds") and returns
310 * the enum fsl_diu_monitor_port that corresponds to that string.
311 *
312 * For compatibility with older versions, a number ("0", "1", or "2") is also
313 * supported.
314 *
315 * If the string is unknown, DVI is assumed.
316 *
317 * If the particular port is not supported by the platform, another port
318 * (platform-specific) is chosen instead.
319 */
320static enum fsl_diu_monitor_port fsl_diu_name_to_port(const char *s)
321{
322 enum fsl_diu_monitor_port port = FSL_DIU_PORT_DVI;
323 unsigned long val;
324
325 if (s) {
326 if (!strict_strtoul(s, 10, &val) && (val <= 2))
327 port = (enum fsl_diu_monitor_port) val;
328 else if (strncmp(s, "lvds", 4) == 0)
329 port = FSL_DIU_PORT_LVDS;
330 else if (strncmp(s, "dlvds", 5) == 0)
331 port = FSL_DIU_PORT_DLVDS;
332 }
333
334 return diu_ops.valid_monitor_port(port);
335}
336
337/**
Timur Tabi6b51d512008-07-23 21:31:39 -0700338 * fsl_diu_alloc - allocate memory for the DIU
339 * @size: number of bytes to allocate
340 * @param: returned physical address of memory
341 *
342 * This function allocates a physically-contiguous block of memory.
York Sun9b53a9e2008-04-28 02:15:34 -0700343 */
Timur Tabi6b51d512008-07-23 21:31:39 -0700344static void *fsl_diu_alloc(size_t size, phys_addr_t *phys)
York Sun9b53a9e2008-04-28 02:15:34 -0700345{
346 void *virt;
347
Timur Tabi6b51d512008-07-23 21:31:39 -0700348 virt = alloc_pages_exact(size, GFP_DMA | __GFP_ZERO);
Timur Tabi154152a2011-09-15 16:44:47 -0500349 if (virt)
York Sun9b53a9e2008-04-28 02:15:34 -0700350 *phys = virt_to_phys(virt);
York Sun9b53a9e2008-04-28 02:15:34 -0700351
352 return virt;
353}
354
Timur Tabi6b51d512008-07-23 21:31:39 -0700355/**
356 * fsl_diu_free - release DIU memory
357 * @virt: pointer returned by fsl_diu_alloc()
358 * @size: number of bytes allocated by fsl_diu_alloc()
359 *
360 * This function releases memory allocated by fsl_diu_alloc().
361 */
362static void fsl_diu_free(void *virt, size_t size)
York Sun9b53a9e2008-04-28 02:15:34 -0700363{
Timur Tabi6b51d512008-07-23 21:31:39 -0700364 if (virt && size)
365 free_pages_exact(virt, size);
York Sun9b53a9e2008-04-28 02:15:34 -0700366}
367
Anatolij Gustschin0d9dab32010-07-23 04:00:35 +0000368/*
369 * Workaround for failed writing desc register of planes.
370 * Needed with MPC5121 DIU rev 2.0 silicon.
371 */
372void wr_reg_wa(u32 *reg, u32 val)
373{
374 do {
375 out_be32(reg, val);
376 } while (in_be32(reg) != val);
377}
378
York Sun9b53a9e2008-04-28 02:15:34 -0700379static int fsl_diu_enable_panel(struct fb_info *info)
380{
381 struct mfb_info *pmfbi, *cmfbi, *mfbi = info->par;
382 struct diu *hw = dr.diu_reg;
383 struct diu_ad *ad = mfbi->ad;
384 struct fsl_diu_data *machine_data = mfbi->parent;
385 int res = 0;
386
York Sun9b53a9e2008-04-28 02:15:34 -0700387 if (mfbi->type != MFB_TYPE_OFF) {
388 switch (mfbi->index) {
389 case 0: /* plane 0 */
390 if (hw->desc[0] != ad->paddr)
Anatolij Gustschin0d9dab32010-07-23 04:00:35 +0000391 wr_reg_wa(&hw->desc[0], ad->paddr);
York Sun9b53a9e2008-04-28 02:15:34 -0700392 break;
393 case 1: /* plane 1 AOI 0 */
394 cmfbi = machine_data->fsl_diu_info[2]->par;
395 if (hw->desc[1] != ad->paddr) { /* AOI0 closed */
396 if (cmfbi->count > 0) /* AOI1 open */
397 ad->next_ad =
398 cpu_to_le32(cmfbi->ad->paddr);
399 else
400 ad->next_ad = 0;
Anatolij Gustschin0d9dab32010-07-23 04:00:35 +0000401 wr_reg_wa(&hw->desc[1], ad->paddr);
York Sun9b53a9e2008-04-28 02:15:34 -0700402 }
403 break;
404 case 3: /* plane 2 AOI 0 */
405 cmfbi = machine_data->fsl_diu_info[4]->par;
406 if (hw->desc[2] != ad->paddr) { /* AOI0 closed */
407 if (cmfbi->count > 0) /* AOI1 open */
408 ad->next_ad =
409 cpu_to_le32(cmfbi->ad->paddr);
410 else
411 ad->next_ad = 0;
Anatolij Gustschin0d9dab32010-07-23 04:00:35 +0000412 wr_reg_wa(&hw->desc[2], ad->paddr);
York Sun9b53a9e2008-04-28 02:15:34 -0700413 }
414 break;
415 case 2: /* plane 1 AOI 1 */
416 pmfbi = machine_data->fsl_diu_info[1]->par;
417 ad->next_ad = 0;
418 if (hw->desc[1] == machine_data->dummy_ad->paddr)
Anatolij Gustschin0d9dab32010-07-23 04:00:35 +0000419 wr_reg_wa(&hw->desc[1], ad->paddr);
York Sun9b53a9e2008-04-28 02:15:34 -0700420 else /* AOI0 open */
421 pmfbi->ad->next_ad = cpu_to_le32(ad->paddr);
422 break;
423 case 4: /* plane 2 AOI 1 */
424 pmfbi = machine_data->fsl_diu_info[3]->par;
425 ad->next_ad = 0;
426 if (hw->desc[2] == machine_data->dummy_ad->paddr)
Anatolij Gustschin0d9dab32010-07-23 04:00:35 +0000427 wr_reg_wa(&hw->desc[2], ad->paddr);
York Sun9b53a9e2008-04-28 02:15:34 -0700428 else /* AOI0 was open */
429 pmfbi->ad->next_ad = cpu_to_le32(ad->paddr);
430 break;
431 default:
432 res = -EINVAL;
433 break;
434 }
435 } else
436 res = -EINVAL;
437 return res;
438}
439
440static int fsl_diu_disable_panel(struct fb_info *info)
441{
442 struct mfb_info *pmfbi, *cmfbi, *mfbi = info->par;
443 struct diu *hw = dr.diu_reg;
444 struct diu_ad *ad = mfbi->ad;
445 struct fsl_diu_data *machine_data = mfbi->parent;
446 int res = 0;
447
448 switch (mfbi->index) {
449 case 0: /* plane 0 */
450 if (hw->desc[0] != machine_data->dummy_ad->paddr)
Anatolij Gustschin0d9dab32010-07-23 04:00:35 +0000451 wr_reg_wa(&hw->desc[0], machine_data->dummy_ad->paddr);
York Sun9b53a9e2008-04-28 02:15:34 -0700452 break;
453 case 1: /* plane 1 AOI 0 */
454 cmfbi = machine_data->fsl_diu_info[2]->par;
455 if (cmfbi->count > 0) /* AOI1 is open */
Anatolij Gustschin0d9dab32010-07-23 04:00:35 +0000456 wr_reg_wa(&hw->desc[1], cmfbi->ad->paddr);
York Sun9b53a9e2008-04-28 02:15:34 -0700457 /* move AOI1 to the first */
458 else /* AOI1 was closed */
Anatolij Gustschin0d9dab32010-07-23 04:00:35 +0000459 wr_reg_wa(&hw->desc[1], machine_data->dummy_ad->paddr);
York Sun9b53a9e2008-04-28 02:15:34 -0700460 /* close AOI 0 */
461 break;
462 case 3: /* plane 2 AOI 0 */
463 cmfbi = machine_data->fsl_diu_info[4]->par;
464 if (cmfbi->count > 0) /* AOI1 is open */
Anatolij Gustschin0d9dab32010-07-23 04:00:35 +0000465 wr_reg_wa(&hw->desc[2], cmfbi->ad->paddr);
York Sun9b53a9e2008-04-28 02:15:34 -0700466 /* move AOI1 to the first */
467 else /* AOI1 was closed */
Anatolij Gustschin0d9dab32010-07-23 04:00:35 +0000468 wr_reg_wa(&hw->desc[2], machine_data->dummy_ad->paddr);
York Sun9b53a9e2008-04-28 02:15:34 -0700469 /* close AOI 0 */
470 break;
471 case 2: /* plane 1 AOI 1 */
472 pmfbi = machine_data->fsl_diu_info[1]->par;
473 if (hw->desc[1] != ad->paddr) {
474 /* AOI1 is not the first in the chain */
475 if (pmfbi->count > 0)
476 /* AOI0 is open, must be the first */
477 pmfbi->ad->next_ad = 0;
478 } else /* AOI1 is the first in the chain */
Anatolij Gustschin0d9dab32010-07-23 04:00:35 +0000479 wr_reg_wa(&hw->desc[1], machine_data->dummy_ad->paddr);
York Sun9b53a9e2008-04-28 02:15:34 -0700480 /* close AOI 1 */
481 break;
482 case 4: /* plane 2 AOI 1 */
483 pmfbi = machine_data->fsl_diu_info[3]->par;
484 if (hw->desc[2] != ad->paddr) {
485 /* AOI1 is not the first in the chain */
486 if (pmfbi->count > 0)
487 /* AOI0 is open, must be the first */
488 pmfbi->ad->next_ad = 0;
489 } else /* AOI1 is the first in the chain */
Anatolij Gustschin0d9dab32010-07-23 04:00:35 +0000490 wr_reg_wa(&hw->desc[2], machine_data->dummy_ad->paddr);
York Sun9b53a9e2008-04-28 02:15:34 -0700491 /* close AOI 1 */
492 break;
493 default:
494 res = -EINVAL;
495 break;
496 }
497
498 return res;
499}
500
501static void enable_lcdc(struct fb_info *info)
502{
503 struct diu *hw = dr.diu_reg;
504 struct mfb_info *mfbi = info->par;
505 struct fsl_diu_data *machine_data = mfbi->parent;
506
507 if (!machine_data->fb_enabled) {
508 out_be32(&hw->diu_mode, dr.mode);
509 machine_data->fb_enabled++;
510 }
511}
512
513static void disable_lcdc(struct fb_info *info)
514{
515 struct diu *hw = dr.diu_reg;
516 struct mfb_info *mfbi = info->par;
517 struct fsl_diu_data *machine_data = mfbi->parent;
518
519 if (machine_data->fb_enabled) {
520 out_be32(&hw->diu_mode, 0);
521 machine_data->fb_enabled = 0;
522 }
523}
524
525static void adjust_aoi_size_position(struct fb_var_screeninfo *var,
526 struct fb_info *info)
527{
528 struct mfb_info *lower_aoi_mfbi, *upper_aoi_mfbi, *mfbi = info->par;
529 struct fsl_diu_data *machine_data = mfbi->parent;
530 int available_height, upper_aoi_bottom, index = mfbi->index;
531 int lower_aoi_is_open, upper_aoi_is_open;
532 __u32 base_plane_width, base_plane_height, upper_aoi_height;
533
534 base_plane_width = machine_data->fsl_diu_info[0]->var.xres;
535 base_plane_height = machine_data->fsl_diu_info[0]->var.yres;
536
York Sunfdfaa48332008-08-15 00:40:29 -0700537 if (mfbi->x_aoi_d < 0)
538 mfbi->x_aoi_d = 0;
539 if (mfbi->y_aoi_d < 0)
540 mfbi->y_aoi_d = 0;
York Sun9b53a9e2008-04-28 02:15:34 -0700541 switch (index) {
542 case 0:
543 if (mfbi->x_aoi_d != 0)
544 mfbi->x_aoi_d = 0;
545 if (mfbi->y_aoi_d != 0)
546 mfbi->y_aoi_d = 0;
547 break;
548 case 1: /* AOI 0 */
549 case 3:
550 lower_aoi_mfbi = machine_data->fsl_diu_info[index+1]->par;
551 lower_aoi_is_open = lower_aoi_mfbi->count > 0 ? 1 : 0;
552 if (var->xres > base_plane_width)
553 var->xres = base_plane_width;
554 if ((mfbi->x_aoi_d + var->xres) > base_plane_width)
555 mfbi->x_aoi_d = base_plane_width - var->xres;
556
557 if (lower_aoi_is_open)
558 available_height = lower_aoi_mfbi->y_aoi_d;
559 else
560 available_height = base_plane_height;
561 if (var->yres > available_height)
562 var->yres = available_height;
563 if ((mfbi->y_aoi_d + var->yres) > available_height)
564 mfbi->y_aoi_d = available_height - var->yres;
565 break;
566 case 2: /* AOI 1 */
567 case 4:
568 upper_aoi_mfbi = machine_data->fsl_diu_info[index-1]->par;
569 upper_aoi_height =
570 machine_data->fsl_diu_info[index-1]->var.yres;
571 upper_aoi_bottom = upper_aoi_mfbi->y_aoi_d + upper_aoi_height;
572 upper_aoi_is_open = upper_aoi_mfbi->count > 0 ? 1 : 0;
573 if (var->xres > base_plane_width)
574 var->xres = base_plane_width;
575 if ((mfbi->x_aoi_d + var->xres) > base_plane_width)
576 mfbi->x_aoi_d = base_plane_width - var->xres;
577 if (mfbi->y_aoi_d < 0)
578 mfbi->y_aoi_d = 0;
579 if (upper_aoi_is_open) {
580 if (mfbi->y_aoi_d < upper_aoi_bottom)
581 mfbi->y_aoi_d = upper_aoi_bottom;
582 available_height = base_plane_height
583 - upper_aoi_bottom;
584 } else
585 available_height = base_plane_height;
586 if (var->yres > available_height)
587 var->yres = available_height;
588 if ((mfbi->y_aoi_d + var->yres) > base_plane_height)
589 mfbi->y_aoi_d = base_plane_height - var->yres;
590 break;
591 }
592}
593/*
594 * Checks to see if the hardware supports the state requested by var passed
595 * in. This function does not alter the hardware state! If the var passed in
596 * is slightly off by what the hardware can support then we alter the var
597 * PASSED in to what we can do. If the hardware doesn't support mode change
598 * a -EINVAL will be returned by the upper layers.
599 */
600static int fsl_diu_check_var(struct fb_var_screeninfo *var,
601 struct fb_info *info)
602{
York Sun9b53a9e2008-04-28 02:15:34 -0700603 if (var->xres_virtual < var->xres)
604 var->xres_virtual = var->xres;
605 if (var->yres_virtual < var->yres)
606 var->yres_virtual = var->yres;
607
608 if (var->xoffset < 0)
609 var->xoffset = 0;
610
611 if (var->yoffset < 0)
612 var->yoffset = 0;
613
614 if (var->xoffset + info->var.xres > info->var.xres_virtual)
615 var->xoffset = info->var.xres_virtual - info->var.xres;
616
617 if (var->yoffset + info->var.yres > info->var.yres_virtual)
618 var->yoffset = info->var.yres_virtual - info->var.yres;
619
620 if ((var->bits_per_pixel != 32) && (var->bits_per_pixel != 24) &&
621 (var->bits_per_pixel != 16))
622 var->bits_per_pixel = default_bpp;
623
624 switch (var->bits_per_pixel) {
625 case 16:
626 var->red.length = 5;
627 var->red.offset = 11;
628 var->red.msb_right = 0;
629
630 var->green.length = 6;
631 var->green.offset = 5;
632 var->green.msb_right = 0;
633
634 var->blue.length = 5;
635 var->blue.offset = 0;
636 var->blue.msb_right = 0;
637
638 var->transp.length = 0;
639 var->transp.offset = 0;
640 var->transp.msb_right = 0;
641 break;
642 case 24:
643 var->red.length = 8;
644 var->red.offset = 0;
645 var->red.msb_right = 0;
646
647 var->green.length = 8;
648 var->green.offset = 8;
649 var->green.msb_right = 0;
650
651 var->blue.length = 8;
652 var->blue.offset = 16;
653 var->blue.msb_right = 0;
654
655 var->transp.length = 0;
656 var->transp.offset = 0;
657 var->transp.msb_right = 0;
658 break;
659 case 32:
660 var->red.length = 8;
661 var->red.offset = 16;
662 var->red.msb_right = 0;
663
664 var->green.length = 8;
665 var->green.offset = 8;
666 var->green.msb_right = 0;
667
668 var->blue.length = 8;
669 var->blue.offset = 0;
670 var->blue.msb_right = 0;
671
672 var->transp.length = 8;
673 var->transp.offset = 24;
674 var->transp.msb_right = 0;
675
676 break;
677 }
York Sun9b53a9e2008-04-28 02:15:34 -0700678
679 var->height = -1;
680 var->width = -1;
681 var->grayscale = 0;
682
683 /* Copy nonstd field to/from sync for fbset usage */
684 var->sync |= var->nonstd;
685 var->nonstd |= var->sync;
686
687 adjust_aoi_size_position(var, info);
688 return 0;
689}
690
691static void set_fix(struct fb_info *info)
692{
693 struct fb_fix_screeninfo *fix = &info->fix;
694 struct fb_var_screeninfo *var = &info->var;
695 struct mfb_info *mfbi = info->par;
696
Timur Tabiec02dd22011-09-15 16:44:54 -0500697 strncpy(fix->id, mfbi->id, sizeof(fix->id));
York Sun9b53a9e2008-04-28 02:15:34 -0700698 fix->line_length = var->xres_virtual * var->bits_per_pixel / 8;
699 fix->type = FB_TYPE_PACKED_PIXELS;
700 fix->accel = FB_ACCEL_NONE;
701 fix->visual = FB_VISUAL_TRUECOLOR;
702 fix->xpanstep = 1;
703 fix->ypanstep = 1;
704}
705
706static void update_lcdc(struct fb_info *info)
707{
708 struct fb_var_screeninfo *var = &info->var;
709 struct mfb_info *mfbi = info->par;
710 struct fsl_diu_data *machine_data = mfbi->parent;
711 struct diu *hw;
712 int i, j;
713 char __iomem *cursor_base, *gamma_table_base;
714
715 u32 temp;
716
717 hw = dr.diu_reg;
718
719 if (mfbi->type == MFB_TYPE_OFF) {
720 fsl_diu_disable_panel(info);
721 return;
722 }
723
724 diu_ops.set_monitor_port(machine_data->monitor_port);
725 gamma_table_base = pool.gamma.vaddr;
726 cursor_base = pool.cursor.vaddr;
727 /* Prep for DIU init - gamma table, cursor table */
728
729 for (i = 0; i <= 2; i++)
Timur Tabi4a85dc8b2011-09-15 16:44:46 -0500730 for (j = 0; j <= 255; j++)
731 *gamma_table_base++ = j;
York Sun9b53a9e2008-04-28 02:15:34 -0700732
733 diu_ops.set_gamma_table(machine_data->monitor_port, pool.gamma.vaddr);
734
York Sun9b53a9e2008-04-28 02:15:34 -0700735 disable_lcdc(info);
736
737 /* Program DIU registers */
738
739 out_be32(&hw->gamma, pool.gamma.paddr);
740 out_be32(&hw->cursor, pool.cursor.paddr);
741
742 out_be32(&hw->bgnd, 0x007F7F7F); /* BGND */
743 out_be32(&hw->bgnd_wb, 0); /* BGND_WB */
744 out_be32(&hw->disp_size, (var->yres << 16 | var->xres));
745 /* DISP SIZE */
York Sun9b53a9e2008-04-28 02:15:34 -0700746 out_be32(&hw->wb_size, 0); /* WB SIZE */
747 out_be32(&hw->wb_mem_addr, 0); /* WB MEM ADDR */
748
749 /* Horizontal and vertical configuration register */
750 temp = var->left_margin << 22 | /* BP_H */
751 var->hsync_len << 11 | /* PW_H */
752 var->right_margin; /* FP_H */
753
754 out_be32(&hw->hsyn_para, temp);
755
756 temp = var->upper_margin << 22 | /* BP_V */
757 var->vsync_len << 11 | /* PW_V */
758 var->lower_margin; /* FP_V */
759
760 out_be32(&hw->vsyn_para, temp);
761
York Sun9b53a9e2008-04-28 02:15:34 -0700762 diu_ops.set_pixel_clock(var->pixclock);
763
764 out_be32(&hw->syn_pol, 0); /* SYNC SIGNALS POLARITY */
765 out_be32(&hw->thresholds, 0x00037800); /* The Thresholds */
766 out_be32(&hw->int_status, 0); /* INTERRUPT STATUS */
767 out_be32(&hw->plut, 0x01F5F666);
768
769 /* Enable the DIU */
770 enable_lcdc(info);
771}
772
773static int map_video_memory(struct fb_info *info)
774{
775 phys_addr_t phys;
Krzysztof Helt537a1bf2009-06-30 11:41:29 -0700776 u32 smem_len = info->fix.line_length * info->var.yres_virtual;
York Sun9b53a9e2008-04-28 02:15:34 -0700777
Krzysztof Helt537a1bf2009-06-30 11:41:29 -0700778 info->screen_base = fsl_diu_alloc(smem_len, &phys);
Anton Vorontsov05946bc2008-07-04 09:59:38 -0700779 if (info->screen_base == NULL) {
Timur Tabi154152a2011-09-15 16:44:47 -0500780 dev_err(info->dev, "unable to allocate fb memory\n");
York Sun9b53a9e2008-04-28 02:15:34 -0700781 return -ENOMEM;
782 }
Krzysztof Helt537a1bf2009-06-30 11:41:29 -0700783 mutex_lock(&info->mm_lock);
York Sun9b53a9e2008-04-28 02:15:34 -0700784 info->fix.smem_start = (unsigned long) phys;
Krzysztof Helt537a1bf2009-06-30 11:41:29 -0700785 info->fix.smem_len = smem_len;
786 mutex_unlock(&info->mm_lock);
York Sun9b53a9e2008-04-28 02:15:34 -0700787 info->screen_size = info->fix.smem_len;
788
York Sun9b53a9e2008-04-28 02:15:34 -0700789 return 0;
790}
791
792static void unmap_video_memory(struct fb_info *info)
793{
794 fsl_diu_free(info->screen_base, info->fix.smem_len);
Krzysztof Helt537a1bf2009-06-30 11:41:29 -0700795 mutex_lock(&info->mm_lock);
Anton Vorontsov05946bc2008-07-04 09:59:38 -0700796 info->screen_base = NULL;
York Sun9b53a9e2008-04-28 02:15:34 -0700797 info->fix.smem_start = 0;
798 info->fix.smem_len = 0;
Krzysztof Helt537a1bf2009-06-30 11:41:29 -0700799 mutex_unlock(&info->mm_lock);
York Sun9b53a9e2008-04-28 02:15:34 -0700800}
801
802/*
York Sunae5591e2008-08-15 00:40:28 -0700803 * Using the fb_var_screeninfo in fb_info we set the aoi of this
804 * particular framebuffer. It is a light version of fsl_diu_set_par.
805 */
806static int fsl_diu_set_aoi(struct fb_info *info)
807{
808 struct fb_var_screeninfo *var = &info->var;
809 struct mfb_info *mfbi = info->par;
810 struct diu_ad *ad = mfbi->ad;
811
812 /* AOI should not be greater than display size */
813 ad->offset_xyi = cpu_to_le32((var->yoffset << 16) | var->xoffset);
814 ad->offset_xyd = cpu_to_le32((mfbi->y_aoi_d << 16) | mfbi->x_aoi_d);
815 return 0;
816}
817
818/*
York Sun9b53a9e2008-04-28 02:15:34 -0700819 * Using the fb_var_screeninfo in fb_info we set the resolution of this
820 * particular framebuffer. This function alters the fb_fix_screeninfo stored
821 * in fb_info. It does not alter var in fb_info since we are using that
822 * data. This means we depend on the data in var inside fb_info to be
823 * supported by the hardware. fsl_diu_check_var is always called before
824 * fsl_diu_set_par to ensure this.
825 */
826static int fsl_diu_set_par(struct fb_info *info)
827{
828 unsigned long len;
829 struct fb_var_screeninfo *var = &info->var;
830 struct mfb_info *mfbi = info->par;
831 struct fsl_diu_data *machine_data = mfbi->parent;
832 struct diu_ad *ad = mfbi->ad;
833 struct diu *hw;
834
835 hw = dr.diu_reg;
836
837 set_fix(info);
838 mfbi->cursor_reset = 1;
839
840 len = info->var.yres_virtual * info->fix.line_length;
841 /* Alloc & dealloc each time resolution/bpp change */
842 if (len != info->fix.smem_len) {
843 if (info->fix.smem_start)
844 unmap_video_memory(info);
York Sun9b53a9e2008-04-28 02:15:34 -0700845
846 /* Memory allocation for framebuffer */
847 if (map_video_memory(info)) {
Timur Tabi154152a2011-09-15 16:44:47 -0500848 dev_err(info->dev, "unable to allocate fb memory 1\n");
York Sun9b53a9e2008-04-28 02:15:34 -0700849 return -ENOMEM;
850 }
851 }
852
Timur Tabi7653aaa2011-07-09 15:38:14 -0500853 ad->pix_fmt = diu_ops.get_pixel_format(machine_data->monitor_port,
854 var->bits_per_pixel);
York Sun9b53a9e2008-04-28 02:15:34 -0700855 ad->addr = cpu_to_le32(info->fix.smem_start);
York Sunae5591e2008-08-15 00:40:28 -0700856 ad->src_size_g_alpha = cpu_to_le32((var->yres_virtual << 12) |
857 var->xres_virtual) | mfbi->g_alpha;
858 /* AOI should not be greater than display size */
York Sun9b53a9e2008-04-28 02:15:34 -0700859 ad->aoi_size = cpu_to_le32((var->yres << 16) | var->xres);
York Sunae5591e2008-08-15 00:40:28 -0700860 ad->offset_xyi = cpu_to_le32((var->yoffset << 16) | var->xoffset);
York Sun9b53a9e2008-04-28 02:15:34 -0700861 ad->offset_xyd = cpu_to_le32((mfbi->y_aoi_d << 16) | mfbi->x_aoi_d);
862
863 /* Disable chroma keying function */
864 ad->ckmax_r = 0;
865 ad->ckmax_g = 0;
866 ad->ckmax_b = 0;
867
868 ad->ckmin_r = 255;
869 ad->ckmin_g = 255;
870 ad->ckmin_b = 255;
871
872 if (mfbi->index == 0)
873 update_lcdc(info);
874 return 0;
875}
876
877static inline __u32 CNVT_TOHW(__u32 val, __u32 width)
878{
Timur Tabi4a85dc8b2011-09-15 16:44:46 -0500879 return ((val << width) + 0x7FFF - val) >> 16;
York Sun9b53a9e2008-04-28 02:15:34 -0700880}
881
882/*
883 * Set a single color register. The values supplied have a 16 bit magnitude
884 * which needs to be scaled in this function for the hardware. Things to take
885 * into consideration are how many color registers, if any, are supported with
886 * the current color visual. With truecolor mode no color palettes are
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300887 * supported. Here a pseudo palette is created which we store the value in
York Sun9b53a9e2008-04-28 02:15:34 -0700888 * pseudo_palette in struct fb_info. For pseudocolor mode we have a limited
889 * color palette.
890 */
Timur Tabi4a85dc8b2011-09-15 16:44:46 -0500891static int fsl_diu_setcolreg(unsigned int regno, unsigned int red,
892 unsigned int green, unsigned int blue,
893 unsigned int transp, struct fb_info *info)
York Sun9b53a9e2008-04-28 02:15:34 -0700894{
895 int ret = 1;
896
897 /*
898 * If greyscale is true, then we convert the RGB value
899 * to greyscale no matter what visual we are using.
900 */
901 if (info->var.grayscale)
902 red = green = blue = (19595 * red + 38470 * green +
903 7471 * blue) >> 16;
904 switch (info->fix.visual) {
905 case FB_VISUAL_TRUECOLOR:
906 /*
907 * 16-bit True Colour. We encode the RGB value
908 * according to the RGB bitfield information.
909 */
910 if (regno < 16) {
911 u32 *pal = info->pseudo_palette;
912 u32 v;
913
914 red = CNVT_TOHW(red, info->var.red.length);
915 green = CNVT_TOHW(green, info->var.green.length);
916 blue = CNVT_TOHW(blue, info->var.blue.length);
917 transp = CNVT_TOHW(transp, info->var.transp.length);
918
919 v = (red << info->var.red.offset) |
920 (green << info->var.green.offset) |
921 (blue << info->var.blue.offset) |
922 (transp << info->var.transp.offset);
923
924 pal[regno] = v;
925 ret = 0;
926 }
927 break;
York Sun9b53a9e2008-04-28 02:15:34 -0700928 }
929
930 return ret;
931}
932
933/*
934 * Pan (or wrap, depending on the `vmode' field) the display using the
935 * 'xoffset' and 'yoffset' fields of the 'var' structure. If the values
936 * don't fit, return -EINVAL.
937 */
938static int fsl_diu_pan_display(struct fb_var_screeninfo *var,
939 struct fb_info *info)
940{
941 if ((info->var.xoffset == var->xoffset) &&
942 (info->var.yoffset == var->yoffset))
943 return 0; /* No change, do nothing */
944
945 if (var->xoffset < 0 || var->yoffset < 0
946 || var->xoffset + info->var.xres > info->var.xres_virtual
947 || var->yoffset + info->var.yres > info->var.yres_virtual)
948 return -EINVAL;
949
950 info->var.xoffset = var->xoffset;
951 info->var.yoffset = var->yoffset;
952
953 if (var->vmode & FB_VMODE_YWRAP)
954 info->var.vmode |= FB_VMODE_YWRAP;
955 else
956 info->var.vmode &= ~FB_VMODE_YWRAP;
957
York Sunae5591e2008-08-15 00:40:28 -0700958 fsl_diu_set_aoi(info);
959
York Sun9b53a9e2008-04-28 02:15:34 -0700960 return 0;
961}
962
963/*
964 * Blank the screen if blank_mode != 0, else unblank. Return 0 if blanking
965 * succeeded, != 0 if un-/blanking failed.
966 * blank_mode == 2: suspend vsync
967 * blank_mode == 3: suspend hsync
968 * blank_mode == 4: powerdown
969 */
970static int fsl_diu_blank(int blank_mode, struct fb_info *info)
971{
972 struct mfb_info *mfbi = info->par;
973
974 mfbi->blank = blank_mode;
975
976 switch (blank_mode) {
977 case FB_BLANK_VSYNC_SUSPEND:
978 case FB_BLANK_HSYNC_SUSPEND:
979 /* FIXME: fixes to enable_panel and enable lcdc needed */
980 case FB_BLANK_NORMAL:
981 /* fsl_diu_disable_panel(info);*/
982 break;
983 case FB_BLANK_POWERDOWN:
984 /* disable_lcdc(info); */
985 break;
986 case FB_BLANK_UNBLANK:
987 /* fsl_diu_enable_panel(info);*/
988 break;
989 }
990
991 return 0;
992}
993
994static int fsl_diu_ioctl(struct fb_info *info, unsigned int cmd,
995 unsigned long arg)
996{
997 struct mfb_info *mfbi = info->par;
998 struct diu_ad *ad = mfbi->ad;
999 struct mfb_chroma_key ck;
1000 unsigned char global_alpha;
1001 struct aoi_display_offset aoi_d;
1002 __u32 pix_fmt;
1003 void __user *buf = (void __user *)arg;
1004
1005 if (!arg)
1006 return -EINVAL;
1007 switch (cmd) {
Timur Tabi36b0b1d2011-10-04 19:36:44 -05001008 case MFB_SET_PIXFMT_OLD:
1009 dev_warn(info->dev,
1010 "MFB_SET_PIXFMT value of 0x%08x is deprecated.\n",
1011 MFB_SET_PIXFMT_OLD);
York Sun9b53a9e2008-04-28 02:15:34 -07001012 case MFB_SET_PIXFMT:
1013 if (copy_from_user(&pix_fmt, buf, sizeof(pix_fmt)))
1014 return -EFAULT;
1015 ad->pix_fmt = pix_fmt;
York Sun9b53a9e2008-04-28 02:15:34 -07001016 break;
Timur Tabi36b0b1d2011-10-04 19:36:44 -05001017 case MFB_GET_PIXFMT_OLD:
1018 dev_warn(info->dev,
1019 "MFB_GET_PIXFMT value of 0x%08x is deprecated.\n",
1020 MFB_GET_PIXFMT_OLD);
York Sun9b53a9e2008-04-28 02:15:34 -07001021 case MFB_GET_PIXFMT:
1022 pix_fmt = ad->pix_fmt;
1023 if (copy_to_user(buf, &pix_fmt, sizeof(pix_fmt)))
1024 return -EFAULT;
York Sun9b53a9e2008-04-28 02:15:34 -07001025 break;
1026 case MFB_SET_AOID:
1027 if (copy_from_user(&aoi_d, buf, sizeof(aoi_d)))
1028 return -EFAULT;
1029 mfbi->x_aoi_d = aoi_d.x_aoi_d;
1030 mfbi->y_aoi_d = aoi_d.y_aoi_d;
York Sun9b53a9e2008-04-28 02:15:34 -07001031 fsl_diu_check_var(&info->var, info);
York Sunae5591e2008-08-15 00:40:28 -07001032 fsl_diu_set_aoi(info);
York Sun9b53a9e2008-04-28 02:15:34 -07001033 break;
1034 case MFB_GET_AOID:
1035 aoi_d.x_aoi_d = mfbi->x_aoi_d;
1036 aoi_d.y_aoi_d = mfbi->y_aoi_d;
1037 if (copy_to_user(buf, &aoi_d, sizeof(aoi_d)))
1038 return -EFAULT;
York Sun9b53a9e2008-04-28 02:15:34 -07001039 break;
1040 case MFB_GET_ALPHA:
1041 global_alpha = mfbi->g_alpha;
1042 if (copy_to_user(buf, &global_alpha, sizeof(global_alpha)))
1043 return -EFAULT;
York Sun9b53a9e2008-04-28 02:15:34 -07001044 break;
1045 case MFB_SET_ALPHA:
1046 /* set panel information */
1047 if (copy_from_user(&global_alpha, buf, sizeof(global_alpha)))
1048 return -EFAULT;
1049 ad->src_size_g_alpha = (ad->src_size_g_alpha & (~0xff)) |
1050 (global_alpha & 0xff);
1051 mfbi->g_alpha = global_alpha;
York Sun9b53a9e2008-04-28 02:15:34 -07001052 break;
1053 case MFB_SET_CHROMA_KEY:
1054 /* set panel winformation */
1055 if (copy_from_user(&ck, buf, sizeof(ck)))
1056 return -EFAULT;
1057
1058 if (ck.enable &&
1059 (ck.red_max < ck.red_min ||
1060 ck.green_max < ck.green_min ||
1061 ck.blue_max < ck.blue_min))
1062 return -EINVAL;
1063
1064 if (!ck.enable) {
1065 ad->ckmax_r = 0;
1066 ad->ckmax_g = 0;
1067 ad->ckmax_b = 0;
1068 ad->ckmin_r = 255;
1069 ad->ckmin_g = 255;
1070 ad->ckmin_b = 255;
1071 } else {
1072 ad->ckmax_r = ck.red_max;
1073 ad->ckmax_g = ck.green_max;
1074 ad->ckmax_b = ck.blue_max;
1075 ad->ckmin_r = ck.red_min;
1076 ad->ckmin_g = ck.green_min;
1077 ad->ckmin_b = ck.blue_min;
1078 }
York Sun9b53a9e2008-04-28 02:15:34 -07001079 break;
York Sun9b53a9e2008-04-28 02:15:34 -07001080 default:
Timur Tabi154152a2011-09-15 16:44:47 -05001081 dev_err(info->dev, "unknown ioctl command (0x%08X)\n", cmd);
York Sun9b53a9e2008-04-28 02:15:34 -07001082 return -ENOIOCTLCMD;
1083 }
1084
1085 return 0;
1086}
1087
1088/* turn on fb if count == 1
1089 */
1090static int fsl_diu_open(struct fb_info *info, int user)
1091{
1092 struct mfb_info *mfbi = info->par;
1093 int res = 0;
1094
Anatolij Gustschin4b5006e2010-07-23 04:00:37 +00001095 /* free boot splash memory on first /dev/fb0 open */
1096 if (!mfbi->index && diu_ops.release_bootmem)
1097 diu_ops.release_bootmem();
1098
York Sun9b53a9e2008-04-28 02:15:34 -07001099 spin_lock(&diu_lock);
1100 mfbi->count++;
1101 if (mfbi->count == 1) {
York Sun9b53a9e2008-04-28 02:15:34 -07001102 fsl_diu_check_var(&info->var, info);
1103 res = fsl_diu_set_par(info);
1104 if (res < 0)
1105 mfbi->count--;
1106 else {
1107 res = fsl_diu_enable_panel(info);
1108 if (res < 0)
1109 mfbi->count--;
1110 }
1111 }
1112
1113 spin_unlock(&diu_lock);
1114 return res;
1115}
1116
1117/* turn off fb if count == 0
1118 */
1119static int fsl_diu_release(struct fb_info *info, int user)
1120{
1121 struct mfb_info *mfbi = info->par;
1122 int res = 0;
1123
1124 spin_lock(&diu_lock);
1125 mfbi->count--;
1126 if (mfbi->count == 0) {
York Sun9b53a9e2008-04-28 02:15:34 -07001127 res = fsl_diu_disable_panel(info);
1128 if (res < 0)
1129 mfbi->count++;
1130 }
1131 spin_unlock(&diu_lock);
1132 return res;
1133}
1134
1135static struct fb_ops fsl_diu_ops = {
1136 .owner = THIS_MODULE,
1137 .fb_check_var = fsl_diu_check_var,
1138 .fb_set_par = fsl_diu_set_par,
1139 .fb_setcolreg = fsl_diu_setcolreg,
1140 .fb_blank = fsl_diu_blank,
1141 .fb_pan_display = fsl_diu_pan_display,
1142 .fb_fillrect = cfb_fillrect,
1143 .fb_copyarea = cfb_copyarea,
1144 .fb_imageblit = cfb_imageblit,
1145 .fb_ioctl = fsl_diu_ioctl,
1146 .fb_open = fsl_diu_open,
1147 .fb_release = fsl_diu_release,
1148};
1149
1150static int init_fbinfo(struct fb_info *info)
1151{
1152 struct mfb_info *mfbi = info->par;
1153
1154 info->device = NULL;
1155 info->var.activate = FB_ACTIVATE_NOW;
1156 info->fbops = &fsl_diu_ops;
1157 info->flags = FBINFO_FLAG_DEFAULT;
1158 info->pseudo_palette = &mfbi->pseudo_palette;
1159
1160 /* Allocate colormap */
1161 fb_alloc_cmap(&info->cmap, 16, 0);
1162 return 0;
1163}
1164
Anton Vorontsov05946bc2008-07-04 09:59:38 -07001165static int __devinit install_fb(struct fb_info *info)
York Sun9b53a9e2008-04-28 02:15:34 -07001166{
1167 int rc;
1168 struct mfb_info *mfbi = info->par;
1169 const char *aoi_mode, *init_aoi_mode = "320x240";
Anatolij Gustschin8b856f02010-07-23 04:00:39 +00001170 struct fb_videomode *db = fsl_diu_mode_db;
1171 unsigned int dbsize = ARRAY_SIZE(fsl_diu_mode_db);
1172 int has_default_mode = 1;
York Sun9b53a9e2008-04-28 02:15:34 -07001173
1174 if (init_fbinfo(info))
1175 return -EINVAL;
1176
Anatolij Gustschin8b856f02010-07-23 04:00:39 +00001177 if (mfbi->index == 0) { /* plane 0 */
1178 if (mfbi->edid_data) {
1179 /* Now build modedb from EDID */
1180 fb_edid_to_monspecs(mfbi->edid_data, &info->monspecs);
1181 fb_videomode_to_modelist(info->monspecs.modedb,
1182 info->monspecs.modedb_len,
1183 &info->modelist);
1184 db = info->monspecs.modedb;
1185 dbsize = info->monspecs.modedb_len;
1186 }
York Sun9b53a9e2008-04-28 02:15:34 -07001187 aoi_mode = fb_mode;
Anatolij Gustschin8b856f02010-07-23 04:00:39 +00001188 } else {
York Sun9b53a9e2008-04-28 02:15:34 -07001189 aoi_mode = init_aoi_mode;
Anatolij Gustschin8b856f02010-07-23 04:00:39 +00001190 }
Timur Tabi63cf8df2011-09-15 16:44:51 -05001191 rc = fb_find_mode(&info->var, info, aoi_mode, db, dbsize, NULL,
1192 default_bpp);
Timur Tabi154152a2011-09-15 16:44:47 -05001193 if (!rc) {
Anatolij Gustschin8b856f02010-07-23 04:00:39 +00001194 /*
1195 * For plane 0 we continue and look into
1196 * driver's internal modedb.
1197 */
1198 if (mfbi->index == 0 && mfbi->edid_data)
1199 has_default_mode = 0;
1200 else
1201 return -EINVAL;
York Sun9b53a9e2008-04-28 02:15:34 -07001202 }
1203
Anatolij Gustschin8b856f02010-07-23 04:00:39 +00001204 if (!has_default_mode) {
1205 rc = fb_find_mode(&info->var, info, aoi_mode, fsl_diu_mode_db,
Timur Tabi63cf8df2011-09-15 16:44:51 -05001206 ARRAY_SIZE(fsl_diu_mode_db), NULL, default_bpp);
1207 if (rc)
Anatolij Gustschin8b856f02010-07-23 04:00:39 +00001208 has_default_mode = 1;
1209 }
1210
1211 /* Still not found, use preferred mode from database if any */
1212 if (!has_default_mode && info->monspecs.modedb) {
1213 struct fb_monspecs *specs = &info->monspecs;
1214 struct fb_videomode *modedb = &specs->modedb[0];
1215
1216 /*
1217 * Get preferred timing. If not found,
1218 * first mode in database will be used.
1219 */
1220 if (specs->misc & FB_MISC_1ST_DETAIL) {
1221 int i;
1222
1223 for (i = 0; i < specs->modedb_len; i++) {
1224 if (specs->modedb[i].flag & FB_MODE_IS_FIRST) {
1225 modedb = &specs->modedb[i];
1226 break;
1227 }
1228 }
1229 }
1230
1231 info->var.bits_per_pixel = default_bpp;
1232 fb_videomode_to_var(&info->var, modedb);
1233 }
1234
York Sun9b53a9e2008-04-28 02:15:34 -07001235 if (mfbi->type == MFB_TYPE_OFF)
1236 mfbi->blank = FB_BLANK_NORMAL;
1237 else
1238 mfbi->blank = FB_BLANK_UNBLANK;
1239
1240 if (fsl_diu_check_var(&info->var, info)) {
Timur Tabi154152a2011-09-15 16:44:47 -05001241 dev_err(info->dev, "fsl_diu_check_var failed\n");
Timur Tabi589c7972011-09-15 16:44:55 -05001242 unmap_video_memory(info);
York Sun9b53a9e2008-04-28 02:15:34 -07001243 fb_dealloc_cmap(&info->cmap);
1244 return -EINVAL;
1245 }
1246
York Sun9b53a9e2008-04-28 02:15:34 -07001247 if (register_framebuffer(info) < 0) {
Timur Tabi154152a2011-09-15 16:44:47 -05001248 dev_err(info->dev, "register_framebuffer failed\n");
York Sun9b53a9e2008-04-28 02:15:34 -07001249 unmap_video_memory(info);
1250 fb_dealloc_cmap(&info->cmap);
1251 return -EINVAL;
1252 }
1253
1254 mfbi->registered = 1;
Timur Tabi154152a2011-09-15 16:44:47 -05001255 dev_info(info->dev, "%s registered successfully\n", mfbi->id);
York Sun9b53a9e2008-04-28 02:15:34 -07001256
1257 return 0;
1258}
1259
Anton Vorontsov05946bc2008-07-04 09:59:38 -07001260static void uninstall_fb(struct fb_info *info)
York Sun9b53a9e2008-04-28 02:15:34 -07001261{
1262 struct mfb_info *mfbi = info->par;
1263
1264 if (!mfbi->registered)
1265 return;
1266
Anatolij Gustschin8b856f02010-07-23 04:00:39 +00001267 if (mfbi->index == 0)
1268 kfree(mfbi->edid_data);
1269
York Sun9b53a9e2008-04-28 02:15:34 -07001270 unregister_framebuffer(info);
1271 unmap_video_memory(info);
1272 if (&info->cmap)
1273 fb_dealloc_cmap(&info->cmap);
1274
1275 mfbi->registered = 0;
1276}
1277
1278static irqreturn_t fsl_diu_isr(int irq, void *dev_id)
1279{
1280 struct diu *hw = dr.diu_reg;
1281 unsigned int status = in_be32(&hw->int_status);
1282
1283 if (status) {
1284 /* This is the workaround for underrun */
1285 if (status & INT_UNDRUN) {
1286 out_be32(&hw->diu_mode, 0);
York Sun9b53a9e2008-04-28 02:15:34 -07001287 udelay(1);
1288 out_be32(&hw->diu_mode, 1);
1289 }
1290#if defined(CONFIG_NOT_COHERENT_CACHE)
1291 else if (status & INT_VSYNC) {
1292 unsigned int i;
Timur Tabi4a85dc8b2011-09-15 16:44:46 -05001293
York Sun9b53a9e2008-04-28 02:15:34 -07001294 for (i = 0; i < coherence_data_size;
1295 i += d_cache_line_size)
1296 __asm__ __volatile__ (
1297 "dcbz 0, %[input]"
1298 ::[input]"r"(&coherence_data[i]));
1299 }
1300#endif
1301 return IRQ_HANDLED;
1302 }
1303 return IRQ_NONE;
1304}
1305
1306static int request_irq_local(int irq)
1307{
Timur Tabibada04f2011-09-15 16:44:52 -05001308 u32 ints;
York Sun9b53a9e2008-04-28 02:15:34 -07001309 struct diu *hw;
1310 int ret;
1311
1312 hw = dr.diu_reg;
1313
1314 /* Read to clear the status */
Timur Tabibada04f2011-09-15 16:44:52 -05001315 in_be32(&hw->int_status);
York Sun9b53a9e2008-04-28 02:15:34 -07001316
Timur Tabif8c6bf62011-09-15 16:44:53 -05001317 ret = request_irq(irq, fsl_diu_isr, 0, "fsl-diu-fb", NULL);
Timur Tabi154152a2011-09-15 16:44:47 -05001318 if (!ret) {
York Sun9b53a9e2008-04-28 02:15:34 -07001319 ints = INT_PARERR | INT_LS_BF_VS;
1320#if !defined(CONFIG_NOT_COHERENT_CACHE)
1321 ints |= INT_VSYNC;
1322#endif
Timur Tabi4a85dc8b2011-09-15 16:44:46 -05001323
York Sun9b53a9e2008-04-28 02:15:34 -07001324 if (dr.mode == MFB_MODE2 || dr.mode == MFB_MODE3)
1325 ints |= INT_VSYNC_WB;
1326
1327 /* Read to clear the status */
Timur Tabibada04f2011-09-15 16:44:52 -05001328 in_be32(&hw->int_status);
York Sun9b53a9e2008-04-28 02:15:34 -07001329 out_be32(&hw->int_mask, ints);
1330 }
Timur Tabi4a85dc8b2011-09-15 16:44:46 -05001331
York Sun9b53a9e2008-04-28 02:15:34 -07001332 return ret;
1333}
1334
1335static void free_irq_local(int irq)
1336{
1337 struct diu *hw = dr.diu_reg;
1338
1339 /* Disable all LCDC interrupt */
1340 out_be32(&hw->int_mask, 0x1f);
1341
Anton Vorontsov05946bc2008-07-04 09:59:38 -07001342 free_irq(irq, NULL);
York Sun9b53a9e2008-04-28 02:15:34 -07001343}
1344
1345#ifdef CONFIG_PM
1346/*
1347 * Power management hooks. Note that we won't be called from IRQ context,
1348 * unlike the blank functions above, so we may sleep.
1349 */
Grant Likely2dc11582010-08-06 09:25:50 -06001350static int fsl_diu_suspend(struct platform_device *ofdev, pm_message_t state)
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 disable_lcdc(machine_data->fsl_diu_info[0]);
1356
1357 return 0;
1358}
1359
Grant Likely2dc11582010-08-06 09:25:50 -06001360static int fsl_diu_resume(struct platform_device *ofdev)
York Sun9b53a9e2008-04-28 02:15:34 -07001361{
1362 struct fsl_diu_data *machine_data;
1363
Takashi Iwai48948a32008-07-08 18:41:17 +02001364 machine_data = dev_get_drvdata(&ofdev->dev);
York Sun9b53a9e2008-04-28 02:15:34 -07001365 enable_lcdc(machine_data->fsl_diu_info[0]);
1366
1367 return 0;
1368}
1369
1370#else
1371#define fsl_diu_suspend NULL
1372#define fsl_diu_resume NULL
1373#endif /* CONFIG_PM */
1374
1375/* Align to 64-bit(8-byte), 32-byte, etc. */
Anton Vorontsovf3791882009-04-04 22:31:20 +04001376static int allocate_buf(struct device *dev, struct diu_addr *buf, u32 size,
1377 u32 bytes_align)
York Sun9b53a9e2008-04-28 02:15:34 -07001378{
Timur Tabibada04f2011-09-15 16:44:52 -05001379 u32 offset;
1380 dma_addr_t mask;
York Sun9b53a9e2008-04-28 02:15:34 -07001381
Timur Tabibada04f2011-09-15 16:44:52 -05001382 buf->vaddr =
1383 dma_alloc_coherent(dev, size + bytes_align, &buf->paddr,
1384 GFP_DMA | __GFP_ZERO);
York Sun9b53a9e2008-04-28 02:15:34 -07001385 if (!buf->vaddr)
1386 return -ENOMEM;
1387
York Sun9b53a9e2008-04-28 02:15:34 -07001388 mask = bytes_align - 1;
Timur Tabibada04f2011-09-15 16:44:52 -05001389 offset = buf->paddr & mask;
York Sun9b53a9e2008-04-28 02:15:34 -07001390 if (offset) {
1391 buf->offset = bytes_align - offset;
Timur Tabibada04f2011-09-15 16:44:52 -05001392 buf->paddr = buf->paddr + offset;
York Sun9b53a9e2008-04-28 02:15:34 -07001393 } else
1394 buf->offset = 0;
Timur Tabi4a85dc8b2011-09-15 16:44:46 -05001395
York Sun9b53a9e2008-04-28 02:15:34 -07001396 return 0;
1397}
1398
Anton Vorontsovf3791882009-04-04 22:31:20 +04001399static void free_buf(struct device *dev, struct diu_addr *buf, u32 size,
1400 u32 bytes_align)
York Sun9b53a9e2008-04-28 02:15:34 -07001401{
Timur Tabi4a85dc8b2011-09-15 16:44:46 -05001402 dma_free_coherent(dev, size + bytes_align, buf->vaddr,
1403 buf->paddr - buf->offset);
York Sun9b53a9e2008-04-28 02:15:34 -07001404}
1405
1406static ssize_t store_monitor(struct device *device,
1407 struct device_attribute *attr, const char *buf, size_t count)
1408{
Timur Tabi7653aaa2011-07-09 15:38:14 -05001409 enum fsl_diu_monitor_port old_monitor_port;
York Sun9b53a9e2008-04-28 02:15:34 -07001410 struct fsl_diu_data *machine_data =
1411 container_of(attr, struct fsl_diu_data, dev_attr);
1412
York Sun9b53a9e2008-04-28 02:15:34 -07001413 old_monitor_port = machine_data->monitor_port;
Timur Tabi7653aaa2011-07-09 15:38:14 -05001414 machine_data->monitor_port = fsl_diu_name_to_port(buf);
York Sun9b53a9e2008-04-28 02:15:34 -07001415
1416 if (old_monitor_port != machine_data->monitor_port) {
1417 /* All AOIs need adjust pixel format
1418 * fsl_diu_set_par only change the pixsel format here
1419 * unlikely to fail. */
1420 fsl_diu_set_par(machine_data->fsl_diu_info[0]);
1421 fsl_diu_set_par(machine_data->fsl_diu_info[1]);
1422 fsl_diu_set_par(machine_data->fsl_diu_info[2]);
1423 fsl_diu_set_par(machine_data->fsl_diu_info[3]);
1424 fsl_diu_set_par(machine_data->fsl_diu_info[4]);
1425 }
1426 return count;
1427}
1428
1429static ssize_t show_monitor(struct device *device,
1430 struct device_attribute *attr, char *buf)
1431{
1432 struct fsl_diu_data *machine_data =
1433 container_of(attr, struct fsl_diu_data, dev_attr);
Timur Tabi7653aaa2011-07-09 15:38:14 -05001434
1435 switch (machine_data->monitor_port) {
1436 case FSL_DIU_PORT_DVI:
1437 return sprintf(buf, "DVI\n");
1438 case FSL_DIU_PORT_LVDS:
1439 return sprintf(buf, "Single-link LVDS\n");
1440 case FSL_DIU_PORT_DLVDS:
1441 return sprintf(buf, "Dual-link LVDS\n");
1442 }
1443
1444 return 0;
York Sun9b53a9e2008-04-28 02:15:34 -07001445}
1446
Timur Tabi9e52ba62011-09-15 16:44:50 -05001447static int __devinit fsl_diu_probe(struct platform_device *pdev)
York Sun9b53a9e2008-04-28 02:15:34 -07001448{
Timur Tabi9e52ba62011-09-15 16:44:50 -05001449 struct device_node *np = pdev->dev.of_node;
York Sun9b53a9e2008-04-28 02:15:34 -07001450 struct mfb_info *mfbi;
Timur Tabi89f08e32011-09-15 16:44:49 -05001451 phys_addr_t dummy_ad_addr = 0;
York Sun9b53a9e2008-04-28 02:15:34 -07001452 int ret, i, error = 0;
York Sun9b53a9e2008-04-28 02:15:34 -07001453 struct fsl_diu_data *machine_data;
Anatolij Gustschin4b5006e2010-07-23 04:00:37 +00001454 int diu_mode;
York Sun9b53a9e2008-04-28 02:15:34 -07001455
1456 machine_data = kzalloc(sizeof(struct fsl_diu_data), GFP_KERNEL);
1457 if (!machine_data)
1458 return -ENOMEM;
1459
1460 for (i = 0; i < ARRAY_SIZE(machine_data->fsl_diu_info); i++) {
1461 machine_data->fsl_diu_info[i] =
Timur Tabi9e52ba62011-09-15 16:44:50 -05001462 framebuffer_alloc(sizeof(struct mfb_info), &pdev->dev);
York Sun9b53a9e2008-04-28 02:15:34 -07001463 if (!machine_data->fsl_diu_info[i]) {
Timur Tabi9e52ba62011-09-15 16:44:50 -05001464 dev_err(&pdev->dev, "cannot allocate memory\n");
York Sun9b53a9e2008-04-28 02:15:34 -07001465 ret = -ENOMEM;
1466 goto error2;
1467 }
1468 mfbi = machine_data->fsl_diu_info[i]->par;
1469 memcpy(mfbi, &mfb_template[i], sizeof(struct mfb_info));
1470 mfbi->parent = machine_data;
Anatolij Gustschin8b856f02010-07-23 04:00:39 +00001471
1472 if (mfbi->index == 0) {
1473 const u8 *prop;
1474 int len;
1475
1476 /* Get EDID */
1477 prop = of_get_property(np, "edid", &len);
1478 if (prop && len == EDID_LENGTH)
1479 mfbi->edid_data = kmemdup(prop, EDID_LENGTH,
1480 GFP_KERNEL);
1481 }
York Sun9b53a9e2008-04-28 02:15:34 -07001482 }
1483
Timur Tabi9e52ba62011-09-15 16:44:50 -05001484 dr.diu_reg = of_iomap(np, 0);
York Sun9b53a9e2008-04-28 02:15:34 -07001485 if (!dr.diu_reg) {
Timur Tabi9e52ba62011-09-15 16:44:50 -05001486 dev_err(&pdev->dev, "cannot map DIU registers\n");
York Sun9b53a9e2008-04-28 02:15:34 -07001487 ret = -EFAULT;
1488 goto error2;
1489 }
1490
Anatolij Gustschin4b5006e2010-07-23 04:00:37 +00001491 diu_mode = in_be32(&dr.diu_reg->diu_mode);
1492 if (diu_mode != MFB_MODE1)
1493 out_be32(&dr.diu_reg->diu_mode, 0); /* disable DIU */
York Sun9b53a9e2008-04-28 02:15:34 -07001494
1495 /* Get the IRQ of the DIU */
1496 machine_data->irq = irq_of_parse_and_map(np, 0);
1497
1498 if (!machine_data->irq) {
Timur Tabi9e52ba62011-09-15 16:44:50 -05001499 dev_err(&pdev->dev, "could not get DIU IRQ\n");
York Sun9b53a9e2008-04-28 02:15:34 -07001500 ret = -EINVAL;
1501 goto error;
1502 }
1503 machine_data->monitor_port = monitor_port;
1504
1505 /* Area descriptor memory pool aligns to 64-bit boundary */
Timur Tabi9e52ba62011-09-15 16:44:50 -05001506 if (allocate_buf(&pdev->dev, &pool.ad,
Anton Vorontsovf3791882009-04-04 22:31:20 +04001507 sizeof(struct diu_ad) * FSL_AOI_NUM, 8))
York Sun9b53a9e2008-04-28 02:15:34 -07001508 return -ENOMEM;
1509
1510 /* Get memory for Gamma Table - 32-byte aligned memory */
Timur Tabi9e52ba62011-09-15 16:44:50 -05001511 if (allocate_buf(&pdev->dev, &pool.gamma, 768, 32)) {
York Sun9b53a9e2008-04-28 02:15:34 -07001512 ret = -ENOMEM;
1513 goto error;
1514 }
1515
1516 /* For performance, cursor bitmap buffer aligns to 32-byte boundary */
Timur Tabi9e52ba62011-09-15 16:44:50 -05001517 if (allocate_buf(&pdev->dev, &pool.cursor, MAX_CURS * MAX_CURS * 2,
Anton Vorontsovf3791882009-04-04 22:31:20 +04001518 32)) {
York Sun9b53a9e2008-04-28 02:15:34 -07001519 ret = -ENOMEM;
1520 goto error;
1521 }
1522
1523 i = ARRAY_SIZE(machine_data->fsl_diu_info);
1524 machine_data->dummy_ad = (struct diu_ad *)
1525 ((u32)pool.ad.vaddr + pool.ad.offset) + i;
1526 machine_data->dummy_ad->paddr = pool.ad.paddr +
1527 i * sizeof(struct diu_ad);
1528 machine_data->dummy_aoi_virt = fsl_diu_alloc(64, &dummy_ad_addr);
1529 if (!machine_data->dummy_aoi_virt) {
1530 ret = -ENOMEM;
1531 goto error;
1532 }
1533 machine_data->dummy_ad->addr = cpu_to_le32(dummy_ad_addr);
1534 machine_data->dummy_ad->pix_fmt = 0x88882317;
1535 machine_data->dummy_ad->src_size_g_alpha = cpu_to_le32((4 << 12) | 4);
1536 machine_data->dummy_ad->aoi_size = cpu_to_le32((4 << 16) | 2);
1537 machine_data->dummy_ad->offset_xyi = 0;
1538 machine_data->dummy_ad->offset_xyd = 0;
1539 machine_data->dummy_ad->next_ad = 0;
1540
Anatolij Gustschin4b5006e2010-07-23 04:00:37 +00001541 /*
1542 * Let DIU display splash screen if it was pre-initialized
1543 * by the bootloader, set dummy area descriptor otherwise.
1544 */
1545 if (diu_mode != MFB_MODE1)
1546 out_be32(&dr.diu_reg->desc[0], machine_data->dummy_ad->paddr);
1547
York Sun9b53a9e2008-04-28 02:15:34 -07001548 out_be32(&dr.diu_reg->desc[1], machine_data->dummy_ad->paddr);
1549 out_be32(&dr.diu_reg->desc[2], machine_data->dummy_ad->paddr);
1550
1551 for (i = 0; i < ARRAY_SIZE(machine_data->fsl_diu_info); i++) {
1552 machine_data->fsl_diu_info[i]->fix.smem_start = 0;
1553 mfbi = machine_data->fsl_diu_info[i]->par;
1554 mfbi->ad = (struct diu_ad *)((u32)pool.ad.vaddr
1555 + pool.ad.offset) + i;
1556 mfbi->ad->paddr = pool.ad.paddr + i * sizeof(struct diu_ad);
1557 ret = install_fb(machine_data->fsl_diu_info[i]);
1558 if (ret) {
Timur Tabi9e52ba62011-09-15 16:44:50 -05001559 dev_err(&pdev->dev, "could not register fb %d\n", i);
York Sun9b53a9e2008-04-28 02:15:34 -07001560 goto error;
1561 }
1562 }
1563
1564 if (request_irq_local(machine_data->irq)) {
Timur Tabi9e52ba62011-09-15 16:44:50 -05001565 dev_err(&pdev->dev, "could not claim irq\n");
York Sun9b53a9e2008-04-28 02:15:34 -07001566 goto error;
1567 }
1568
Wolfram Sang12765512010-04-06 14:34:52 -07001569 sysfs_attr_init(&machine_data->dev_attr.attr);
York Sun9b53a9e2008-04-28 02:15:34 -07001570 machine_data->dev_attr.attr.name = "monitor";
1571 machine_data->dev_attr.attr.mode = S_IRUGO|S_IWUSR;
1572 machine_data->dev_attr.show = show_monitor;
1573 machine_data->dev_attr.store = store_monitor;
1574 error = device_create_file(machine_data->fsl_diu_info[0]->dev,
1575 &machine_data->dev_attr);
1576 if (error) {
Timur Tabi9e52ba62011-09-15 16:44:50 -05001577 dev_err(&pdev->dev, "could not create sysfs file %s\n",
York Sun9b53a9e2008-04-28 02:15:34 -07001578 machine_data->dev_attr.attr.name);
1579 }
1580
Timur Tabi9e52ba62011-09-15 16:44:50 -05001581 dev_set_drvdata(&pdev->dev, machine_data);
York Sun9b53a9e2008-04-28 02:15:34 -07001582 return 0;
1583
1584error:
Timur Tabi3f78bbd2011-09-15 16:44:56 -05001585 for (i = 0; i < ARRAY_SIZE(machine_data->fsl_diu_info); i++)
1586 uninstall_fb(machine_data->fsl_diu_info[i]);
1587
York Sun9b53a9e2008-04-28 02:15:34 -07001588 if (pool.ad.vaddr)
Timur Tabi9e52ba62011-09-15 16:44:50 -05001589 free_buf(&pdev->dev, &pool.ad,
Anton Vorontsovf3791882009-04-04 22:31:20 +04001590 sizeof(struct diu_ad) * FSL_AOI_NUM, 8);
York Sun9b53a9e2008-04-28 02:15:34 -07001591 if (pool.gamma.vaddr)
Timur Tabi9e52ba62011-09-15 16:44:50 -05001592 free_buf(&pdev->dev, &pool.gamma, 768, 32);
York Sun9b53a9e2008-04-28 02:15:34 -07001593 if (pool.cursor.vaddr)
Timur Tabi9e52ba62011-09-15 16:44:50 -05001594 free_buf(&pdev->dev, &pool.cursor, MAX_CURS * MAX_CURS * 2,
Anton Vorontsovf3791882009-04-04 22:31:20 +04001595 32);
York Sun9b53a9e2008-04-28 02:15:34 -07001596 if (machine_data->dummy_aoi_virt)
1597 fsl_diu_free(machine_data->dummy_aoi_virt, 64);
1598 iounmap(dr.diu_reg);
1599
1600error2:
1601 for (i = 0; i < ARRAY_SIZE(machine_data->fsl_diu_info); i++)
1602 if (machine_data->fsl_diu_info[i])
1603 framebuffer_release(machine_data->fsl_diu_info[i]);
1604 kfree(machine_data);
1605
1606 return ret;
1607}
1608
Timur Tabi9e52ba62011-09-15 16:44:50 -05001609static int fsl_diu_remove(struct platform_device *pdev)
York Sun9b53a9e2008-04-28 02:15:34 -07001610{
1611 struct fsl_diu_data *machine_data;
1612 int i;
1613
Timur Tabi9e52ba62011-09-15 16:44:50 -05001614 machine_data = dev_get_drvdata(&pdev->dev);
York Sun9b53a9e2008-04-28 02:15:34 -07001615 disable_lcdc(machine_data->fsl_diu_info[0]);
1616 free_irq_local(machine_data->irq);
Timur Tabi3f78bbd2011-09-15 16:44:56 -05001617 for (i = 0; i < ARRAY_SIZE(machine_data->fsl_diu_info); i++)
1618 uninstall_fb(machine_data->fsl_diu_info[i]);
York Sun9b53a9e2008-04-28 02:15:34 -07001619 if (pool.ad.vaddr)
Timur Tabi9e52ba62011-09-15 16:44:50 -05001620 free_buf(&pdev->dev, &pool.ad,
Anton Vorontsovf3791882009-04-04 22:31:20 +04001621 sizeof(struct diu_ad) * FSL_AOI_NUM, 8);
York Sun9b53a9e2008-04-28 02:15:34 -07001622 if (pool.gamma.vaddr)
Timur Tabi9e52ba62011-09-15 16:44:50 -05001623 free_buf(&pdev->dev, &pool.gamma, 768, 32);
York Sun9b53a9e2008-04-28 02:15:34 -07001624 if (pool.cursor.vaddr)
Timur Tabi9e52ba62011-09-15 16:44:50 -05001625 free_buf(&pdev->dev, &pool.cursor, MAX_CURS * MAX_CURS * 2, 32);
York Sun9b53a9e2008-04-28 02:15:34 -07001626 if (machine_data->dummy_aoi_virt)
1627 fsl_diu_free(machine_data->dummy_aoi_virt, 64);
1628 iounmap(dr.diu_reg);
1629 for (i = 0; i < ARRAY_SIZE(machine_data->fsl_diu_info); i++)
1630 if (machine_data->fsl_diu_info[i])
1631 framebuffer_release(machine_data->fsl_diu_info[i]);
1632 kfree(machine_data);
1633
1634 return 0;
1635}
1636
1637#ifndef MODULE
1638static int __init fsl_diu_setup(char *options)
1639{
1640 char *opt;
1641 unsigned long val;
1642
1643 if (!options || !*options)
1644 return 0;
1645
1646 while ((opt = strsep(&options, ",")) != NULL) {
1647 if (!*opt)
1648 continue;
1649 if (!strncmp(opt, "monitor=", 8)) {
Timur Tabi7653aaa2011-07-09 15:38:14 -05001650 monitor_port = fsl_diu_name_to_port(opt + 8);
York Sun9b53a9e2008-04-28 02:15:34 -07001651 } else if (!strncmp(opt, "bpp=", 4)) {
1652 if (!strict_strtoul(opt + 4, 10, &val))
1653 default_bpp = val;
1654 } else
1655 fb_mode = opt;
1656 }
1657
1658 return 0;
1659}
1660#endif
1661
1662static struct of_device_id fsl_diu_match[] = {
Anatolij Gustschind24720a2010-02-17 07:33:22 -07001663#ifdef CONFIG_PPC_MPC512x
1664 {
1665 .compatible = "fsl,mpc5121-diu",
1666 },
1667#endif
York Sun9b53a9e2008-04-28 02:15:34 -07001668 {
1669 .compatible = "fsl,diu",
1670 },
1671 {}
1672};
1673MODULE_DEVICE_TABLE(of, fsl_diu_match);
1674
Grant Likely28541d02011-02-22 21:07:43 -07001675static struct platform_driver fsl_diu_driver = {
Grant Likely40182942010-04-13 16:13:02 -07001676 .driver = {
Timur Tabif8c6bf62011-09-15 16:44:53 -05001677 .name = "fsl-diu-fb",
Grant Likely40182942010-04-13 16:13:02 -07001678 .owner = THIS_MODULE,
1679 .of_match_table = fsl_diu_match,
1680 },
York Sun9b53a9e2008-04-28 02:15:34 -07001681 .probe = fsl_diu_probe,
1682 .remove = fsl_diu_remove,
1683 .suspend = fsl_diu_suspend,
1684 .resume = fsl_diu_resume,
1685};
1686
1687static int __init fsl_diu_init(void)
1688{
1689#ifdef CONFIG_NOT_COHERENT_CACHE
1690 struct device_node *np;
1691 const u32 *prop;
1692#endif
1693 int ret;
1694#ifndef MODULE
1695 char *option;
1696
1697 /*
1698 * For kernel boot options (in 'video=xxxfb:<options>' format)
1699 */
1700 if (fb_get_options("fslfb", &option))
1701 return -ENODEV;
1702 fsl_diu_setup(option);
Timur Tabi7653aaa2011-07-09 15:38:14 -05001703#else
1704 monitor_port = fsl_diu_name_to_port(monitor_string);
York Sun9b53a9e2008-04-28 02:15:34 -07001705#endif
Timur Tabi154152a2011-09-15 16:44:47 -05001706 pr_info("Freescale Display Interface Unit (DIU) framebuffer driver\n");
York Sun9b53a9e2008-04-28 02:15:34 -07001707
1708#ifdef CONFIG_NOT_COHERENT_CACHE
1709 np = of_find_node_by_type(NULL, "cpu");
1710 if (!np) {
Timur Tabi154152a2011-09-15 16:44:47 -05001711 pr_err("fsl-diu-fb: can't find 'cpu' device node\n");
York Sun9b53a9e2008-04-28 02:15:34 -07001712 return -ENODEV;
1713 }
1714
1715 prop = of_get_property(np, "d-cache-size", NULL);
Julia Lawall5394ba02008-08-05 13:01:28 -07001716 if (prop == NULL) {
Timur Tabi154152a2011-09-15 16:44:47 -05001717 pr_err("fsl-diu-fb: missing 'd-cache-size' property' "
1718 "in 'cpu' node\n");
Julia Lawall5394ba02008-08-05 13:01:28 -07001719 of_node_put(np);
York Sun9b53a9e2008-04-28 02:15:34 -07001720 return -ENODEV;
Julia Lawall5394ba02008-08-05 13:01:28 -07001721 }
York Sun9b53a9e2008-04-28 02:15:34 -07001722
Timur Tabi4a85dc8b2011-09-15 16:44:46 -05001723 /*
1724 * Freescale PLRU requires 13/8 times the cache size to do a proper
1725 * displacement flush
York Sun9b53a9e2008-04-28 02:15:34 -07001726 */
Timur Tabi9e52ba62011-09-15 16:44:50 -05001727 coherence_data_size = be32_to_cpup(prop) * 13;
York Sun9b53a9e2008-04-28 02:15:34 -07001728 coherence_data_size /= 8;
1729
1730 prop = of_get_property(np, "d-cache-line-size", NULL);
Julia Lawall5394ba02008-08-05 13:01:28 -07001731 if (prop == NULL) {
Timur Tabi154152a2011-09-15 16:44:47 -05001732 pr_err("fsl-diu-fb: missing 'd-cache-line-size' property' "
1733 "in 'cpu' node\n");
Julia Lawall5394ba02008-08-05 13:01:28 -07001734 of_node_put(np);
York Sun9b53a9e2008-04-28 02:15:34 -07001735 return -ENODEV;
Julia Lawall5394ba02008-08-05 13:01:28 -07001736 }
Timur Tabi9e52ba62011-09-15 16:44:50 -05001737 d_cache_line_size = be32_to_cpup(prop);
York Sun9b53a9e2008-04-28 02:15:34 -07001738
1739 of_node_put(np);
1740 coherence_data = vmalloc(coherence_data_size);
1741 if (!coherence_data)
1742 return -ENOMEM;
1743#endif
Timur Tabi4a85dc8b2011-09-15 16:44:46 -05001744
Grant Likely28541d02011-02-22 21:07:43 -07001745 ret = platform_driver_register(&fsl_diu_driver);
York Sun9b53a9e2008-04-28 02:15:34 -07001746 if (ret) {
Timur Tabi154152a2011-09-15 16:44:47 -05001747 pr_err("fsl-diu-fb: failed to register platform driver\n");
York Sun9b53a9e2008-04-28 02:15:34 -07001748#if defined(CONFIG_NOT_COHERENT_CACHE)
1749 vfree(coherence_data);
1750#endif
1751 iounmap(dr.diu_reg);
1752 }
1753 return ret;
1754}
1755
1756static void __exit fsl_diu_exit(void)
1757{
Grant Likely28541d02011-02-22 21:07:43 -07001758 platform_driver_unregister(&fsl_diu_driver);
York Sun9b53a9e2008-04-28 02:15:34 -07001759#if defined(CONFIG_NOT_COHERENT_CACHE)
1760 vfree(coherence_data);
1761#endif
1762}
1763
1764module_init(fsl_diu_init);
1765module_exit(fsl_diu_exit);
1766
1767MODULE_AUTHOR("York Sun <yorksun@freescale.com>");
1768MODULE_DESCRIPTION("Freescale DIU framebuffer driver");
1769MODULE_LICENSE("GPL");
1770
1771module_param_named(mode, fb_mode, charp, 0);
1772MODULE_PARM_DESC(mode,
1773 "Specify resolution as \"<xres>x<yres>[-<bpp>][@<refresh>]\" ");
1774module_param_named(bpp, default_bpp, ulong, 0);
Timur Tabi154152a2011-09-15 16:44:47 -05001775MODULE_PARM_DESC(bpp, "Specify bit-per-pixel if not specified in 'mode'");
Timur Tabi7653aaa2011-07-09 15:38:14 -05001776module_param_named(monitor, monitor_string, charp, 0);
1777MODULE_PARM_DESC(monitor, "Specify the monitor port "
1778 "(\"dvi\", \"lvds\", or \"dlvds\") if supported by the platform");
York Sun9b53a9e2008-04-28 02:15:34 -07001779