blob: 76722a59f55e34c83ec948d77c2c0df5c936fa16 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/drivers/video/pxafb.c
3 *
4 * Copyright (C) 1999 Eric A. Thomas.
5 * Copyright (C) 2004 Jean-Frederic Clere.
6 * Copyright (C) 2004 Ian Campbell.
7 * Copyright (C) 2004 Jeff Lackey.
8 * Based on sa1100fb.c Copyright (C) 1999 Eric A. Thomas
9 * which in turn is
10 * Based on acornfb.c Copyright (C) Russell King.
11 *
12 * This file is subject to the terms and conditions of the GNU General Public
13 * License. See the file COPYING in the main directory of this archive for
14 * more details.
15 *
16 * Intel PXA250/210 LCD Controller Frame Buffer Driver
17 *
18 * Please direct your questions and comments on this driver to the following
19 * email address:
20 *
21 * linux-arm-kernel@lists.arm.linux.org.uk
22 *
Eric Miao198fc102008-12-23 17:49:43 +080023 * Add support for overlay1 and overlay2 based on pxafb_overlay.c:
24 *
25 * Copyright (C) 2004, Intel Corporation
26 *
27 * 2003/08/27: <yu.tang@intel.com>
28 * 2004/03/10: <stanley.cai@intel.com>
29 * 2004/10/28: <yan.yin@intel.com>
30 *
31 * Copyright (C) 2006-2008 Marvell International Ltd.
32 * All Rights Reserved
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 */
34
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/module.h>
36#include <linux/moduleparam.h>
37#include <linux/kernel.h>
38#include <linux/sched.h>
39#include <linux/errno.h>
40#include <linux/string.h>
41#include <linux/interrupt.h>
42#include <linux/slab.h>
Andrea Righi27ac7922008-07-23 21:28:13 -070043#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/fb.h>
45#include <linux/delay.h>
46#include <linux/init.h>
47#include <linux/ioport.h>
48#include <linux/cpufreq.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010049#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <linux/dma-mapping.h>
Russell King72e35242007-08-20 10:18:42 +010051#include <linux/clk.h>
52#include <linux/err.h>
Eric Miao2ba162b2008-04-30 00:52:24 -070053#include <linux/completion.h>
Matthias Kaehlckeb91dbce2008-07-23 21:31:14 -070054#include <linux/mutex.h>
Eric Miao3c42a442008-04-30 00:52:26 -070055#include <linux/kthread.h>
56#include <linux/freezer.h>
Vasily Khoruzhick23f136562012-02-26 16:52:02 +030057#include <linux/console.h>
Robert Jarzmik420a4882015-12-12 22:27:11 +010058#include <linux/of_graph.h>
59#include <video/of_display_timing.h>
60#include <video/videomode.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Russell Kinga09e64f2008-08-05 16:14:15 +010062#include <mach/hardware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include <asm/io.h>
64#include <asm/irq.h>
Nicolas Pitrebf1b8ab2005-06-23 21:56:45 +010065#include <asm/div64.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010066#include <mach/bitfield.h>
Arnd Bergmann293b2da2012-08-24 15:16:48 +020067#include <linux/platform_data/video-pxafb.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69/*
70 * Complain if VAR is out of range.
71 */
72#define DEBUG_VAR 1
73
74#include "pxafb.h"
75
76/* Bits which should not be set in machine configuration structures */
eric miaob0086ef2008-04-30 00:52:19 -070077#define LCCR0_INVALID_CONFIG_MASK (LCCR0_OUM | LCCR0_BM | LCCR0_QDM |\
78 LCCR0_DIS | LCCR0_EFM | LCCR0_IUM |\
79 LCCR0_SFM | LCCR0_LDM | LCCR0_ENB)
80
81#define LCCR3_INVALID_CONFIG_MASK (LCCR3_HSP | LCCR3_VSP |\
Eric Miao878f5782008-12-18 22:36:26 +080082 LCCR3_PCD | LCCR3_BPP(0xf))
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
eric miaob0086ef2008-04-30 00:52:19 -070084static int pxafb_activate_var(struct fb_var_screeninfo *var,
85 struct pxafb_info *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086static void set_ctrlr_state(struct pxafb_info *fbi, u_int state);
Sven Neumann448ac472009-10-22 08:34:34 +020087static void setup_base_frame(struct pxafb_info *fbi,
88 struct fb_var_screeninfo *var, int branch);
Eric Miao198fc102008-12-23 17:49:43 +080089static int setup_frame_dma(struct pxafb_info *fbi, int dma, int pal,
90 unsigned long offset, size_t size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Eric Miao77e19672008-12-16 11:54:34 +080092static unsigned long video_mem_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Eric Miaoa7535ba2008-04-30 00:52:24 -070094static inline unsigned long
95lcd_readl(struct pxafb_info *fbi, unsigned int off)
96{
97 return __raw_readl(fbi->mmio_base + off);
98}
99
100static inline void
101lcd_writel(struct pxafb_info *fbi, unsigned int off, unsigned long val)
102{
103 __raw_writel(val, fbi->mmio_base + off);
104}
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106static inline void pxafb_schedule_work(struct pxafb_info *fbi, u_int state)
107{
108 unsigned long flags;
109
110 local_irq_save(flags);
111 /*
112 * We need to handle two requests being made at the same time.
113 * There are two important cases:
eric miaob0086ef2008-04-30 00:52:19 -0700114 * 1. When we are changing VT (C_REENABLE) while unblanking
115 * (C_ENABLE) We must perform the unblanking, which will
116 * do our REENABLE for us.
117 * 2. When we are blanking, but immediately unblank before
118 * we have blanked. We do the "REENABLE" thing here as
119 * well, just to be sure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 */
121 if (fbi->task_state == C_ENABLE && state == C_REENABLE)
122 state = (u_int) -1;
123 if (fbi->task_state == C_DISABLE && state == C_ENABLE)
124 state = C_REENABLE;
125
126 if (state != (u_int)-1) {
127 fbi->task_state = state;
128 schedule_work(&fbi->task);
129 }
130 local_irq_restore(flags);
131}
132
133static inline u_int chan_to_field(u_int chan, struct fb_bitfield *bf)
134{
135 chan &= 0xffff;
136 chan >>= 16 - bf->length;
137 return chan << bf->offset;
138}
139
140static int
141pxafb_setpalettereg(u_int regno, u_int red, u_int green, u_int blue,
142 u_int trans, struct fb_info *info)
143{
Fabian Frederick29ebebb2014-09-17 21:00:18 +0200144 struct pxafb_info *fbi = container_of(info, struct pxafb_info, fb);
Hans J. Koch9ffa7392007-10-16 01:28:41 -0700145 u_int val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Hans J. Koch9ffa7392007-10-16 01:28:41 -0700147 if (regno >= fbi->palette_size)
148 return 1;
149
150 if (fbi->fb.var.grayscale) {
151 fbi->palette_cpu[regno] = ((blue >> 8) & 0x00ff);
152 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 }
Hans J. Koch9ffa7392007-10-16 01:28:41 -0700154
155 switch (fbi->lccr4 & LCCR4_PAL_FOR_MASK) {
156 case LCCR4_PAL_FOR_0:
157 val = ((red >> 0) & 0xf800);
158 val |= ((green >> 5) & 0x07e0);
159 val |= ((blue >> 11) & 0x001f);
160 fbi->palette_cpu[regno] = val;
161 break;
162 case LCCR4_PAL_FOR_1:
163 val = ((red << 8) & 0x00f80000);
164 val |= ((green >> 0) & 0x0000fc00);
165 val |= ((blue >> 8) & 0x000000f8);
eric miaob0086ef2008-04-30 00:52:19 -0700166 ((u32 *)(fbi->palette_cpu))[regno] = val;
Hans J. Koch9ffa7392007-10-16 01:28:41 -0700167 break;
168 case LCCR4_PAL_FOR_2:
169 val = ((red << 8) & 0x00fc0000);
170 val |= ((green >> 0) & 0x0000fc00);
171 val |= ((blue >> 8) & 0x000000fc);
eric miaob0086ef2008-04-30 00:52:19 -0700172 ((u32 *)(fbi->palette_cpu))[regno] = val;
Hans J. Koch9ffa7392007-10-16 01:28:41 -0700173 break;
Eric Miaoa0427502008-12-18 22:10:00 +0800174 case LCCR4_PAL_FOR_3:
175 val = ((red << 8) & 0x00ff0000);
176 val |= ((green >> 0) & 0x0000ff00);
177 val |= ((blue >> 8) & 0x000000ff);
178 ((u32 *)(fbi->palette_cpu))[regno] = val;
179 break;
Hans J. Koch9ffa7392007-10-16 01:28:41 -0700180 }
181
182 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183}
184
185static int
186pxafb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
187 u_int trans, struct fb_info *info)
188{
Fabian Frederick29ebebb2014-09-17 21:00:18 +0200189 struct pxafb_info *fbi = container_of(info, struct pxafb_info, fb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 unsigned int val;
191 int ret = 1;
192
193 /*
194 * If inverse mode was selected, invert all the colours
195 * rather than the register number. The register number
196 * is what you poke into the framebuffer to produce the
197 * colour you requested.
198 */
199 if (fbi->cmap_inverse) {
200 red = 0xffff - red;
201 green = 0xffff - green;
202 blue = 0xffff - blue;
203 }
204
205 /*
206 * If greyscale is true, then we convert the RGB value
207 * to greyscale no matter what visual we are using.
208 */
209 if (fbi->fb.var.grayscale)
210 red = green = blue = (19595 * red + 38470 * green +
211 7471 * blue) >> 16;
212
213 switch (fbi->fb.fix.visual) {
214 case FB_VISUAL_TRUECOLOR:
215 /*
216 * 16-bit True Colour. We encode the RGB value
217 * according to the RGB bitfield information.
218 */
219 if (regno < 16) {
220 u32 *pal = fbi->fb.pseudo_palette;
221
222 val = chan_to_field(red, &fbi->fb.var.red);
223 val |= chan_to_field(green, &fbi->fb.var.green);
224 val |= chan_to_field(blue, &fbi->fb.var.blue);
225
226 pal[regno] = val;
227 ret = 0;
228 }
229 break;
230
231 case FB_VISUAL_STATIC_PSEUDOCOLOR:
232 case FB_VISUAL_PSEUDOCOLOR:
233 ret = pxafb_setpalettereg(regno, red, green, blue, trans, info);
234 break;
235 }
236
237 return ret;
238}
239
Eric Miao878f5782008-12-18 22:36:26 +0800240/* calculate pixel depth, transparency bit included, >=16bpp formats _only_ */
241static inline int var_to_depth(struct fb_var_screeninfo *var)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
Eric Miao878f5782008-12-18 22:36:26 +0800243 return var->red.length + var->green.length +
244 var->blue.length + var->transp.length;
245}
246
247/* calculate 4-bit BPP value for LCCR3 and OVLxC1 */
248static int pxafb_var_to_bpp(struct fb_var_screeninfo *var)
249{
250 int bpp = -EINVAL;
251
eric miaob0086ef2008-04-30 00:52:19 -0700252 switch (var->bits_per_pixel) {
Eric Miao878f5782008-12-18 22:36:26 +0800253 case 1: bpp = 0; break;
254 case 2: bpp = 1; break;
255 case 4: bpp = 2; break;
256 case 8: bpp = 3; break;
257 case 16: bpp = 4; break;
Stefan Schmidtc1450f12008-07-09 08:06:32 +0100258 case 24:
Eric Miao878f5782008-12-18 22:36:26 +0800259 switch (var_to_depth(var)) {
260 case 18: bpp = 6; break; /* 18-bits/pixel packed */
261 case 19: bpp = 8; break; /* 19-bits/pixel packed */
262 case 24: bpp = 9; break;
Stefan Schmidtc1450f12008-07-09 08:06:32 +0100263 }
264 break;
265 case 32:
Eric Miao878f5782008-12-18 22:36:26 +0800266 switch (var_to_depth(var)) {
267 case 18: bpp = 5; break; /* 18-bits/pixel unpacked */
268 case 19: bpp = 7; break; /* 19-bits/pixel unpacked */
269 case 25: bpp = 10; break;
Stefan Schmidtc1450f12008-07-09 08:06:32 +0100270 }
271 break;
eric miaob0086ef2008-04-30 00:52:19 -0700272 }
Eric Miao878f5782008-12-18 22:36:26 +0800273 return bpp;
274}
275
276/*
277 * pxafb_var_to_lccr3():
278 * Convert a bits per pixel value to the correct bit pattern for LCCR3
279 *
280 * NOTE: for PXA27x with overlays support, the LCCR3_PDFOR_x bits have an
281 * implication of the acutal use of transparency bit, which we handle it
282 * here separatedly. See PXA27x Developer's Manual, Section <<7.4.6 Pixel
283 * Formats>> for the valid combination of PDFOR, PAL_FOR for various BPP.
284 *
285 * Transparency for palette pixel formats is not supported at the moment.
286 */
287static uint32_t pxafb_var_to_lccr3(struct fb_var_screeninfo *var)
288{
289 int bpp = pxafb_var_to_bpp(var);
290 uint32_t lccr3;
291
292 if (bpp < 0)
293 return 0;
294
295 lccr3 = LCCR3_BPP(bpp);
296
297 switch (var_to_depth(var)) {
298 case 16: lccr3 |= var->transp.length ? LCCR3_PDFOR_3 : 0; break;
299 case 18: lccr3 |= LCCR3_PDFOR_3; break;
300 case 24: lccr3 |= var->transp.length ? LCCR3_PDFOR_2 : LCCR3_PDFOR_3;
301 break;
302 case 19:
303 case 25: lccr3 |= LCCR3_PDFOR_0; break;
304 }
305 return lccr3;
306}
307
308#define SET_PIXFMT(v, r, g, b, t) \
309({ \
310 (v)->transp.offset = (t) ? (r) + (g) + (b) : 0; \
311 (v)->transp.length = (t) ? (t) : 0; \
312 (v)->blue.length = (b); (v)->blue.offset = 0; \
313 (v)->green.length = (g); (v)->green.offset = (b); \
314 (v)->red.length = (r); (v)->red.offset = (b) + (g); \
315})
316
317/* set the RGBT bitfields of fb_var_screeninf according to
318 * var->bits_per_pixel and given depth
319 */
320static void pxafb_set_pixfmt(struct fb_var_screeninfo *var, int depth)
321{
322 if (depth == 0)
323 depth = var->bits_per_pixel;
324
325 if (var->bits_per_pixel < 16) {
326 /* indexed pixel formats */
327 var->red.offset = 0; var->red.length = 8;
328 var->green.offset = 0; var->green.length = 8;
329 var->blue.offset = 0; var->blue.length = 8;
330 var->transp.offset = 0; var->transp.length = 8;
331 }
332
333 switch (depth) {
334 case 16: var->transp.length ?
335 SET_PIXFMT(var, 5, 5, 5, 1) : /* RGBT555 */
336 SET_PIXFMT(var, 5, 6, 5, 0); break; /* RGB565 */
337 case 18: SET_PIXFMT(var, 6, 6, 6, 0); break; /* RGB666 */
338 case 19: SET_PIXFMT(var, 6, 6, 6, 1); break; /* RGBT666 */
339 case 24: var->transp.length ?
340 SET_PIXFMT(var, 8, 8, 7, 1) : /* RGBT887 */
341 SET_PIXFMT(var, 8, 8, 8, 0); break; /* RGB888 */
342 case 25: SET_PIXFMT(var, 8, 8, 8, 1); break; /* RGBT888 */
343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344}
345
346#ifdef CONFIG_CPU_FREQ
347/*
348 * pxafb_display_dma_period()
349 * Calculate the minimum period (in picoseconds) between two DMA
350 * requests for the LCD controller. If we hit this, it means we're
351 * doing nothing but LCD DMA.
352 */
353static unsigned int pxafb_display_dma_period(struct fb_var_screeninfo *var)
354{
eric miaob0086ef2008-04-30 00:52:19 -0700355 /*
356 * Period = pixclock * bits_per_byte * bytes_per_transfer
357 * / memory_bits_per_pixel;
358 */
359 return var->pixclock * 8 * 16 / var->bits_per_pixel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361#endif
362
363/*
Richard Purdied14b2722006-09-20 22:54:21 +0100364 * Select the smallest mode that allows the desired resolution to be
365 * displayed. If desired parameters can be rounded up.
366 */
eric miaob0086ef2008-04-30 00:52:19 -0700367static struct pxafb_mode_info *pxafb_getmode(struct pxafb_mach_info *mach,
368 struct fb_var_screeninfo *var)
Richard Purdied14b2722006-09-20 22:54:21 +0100369{
370 struct pxafb_mode_info *mode = NULL;
371 struct pxafb_mode_info *modelist = mach->modes;
372 unsigned int best_x = 0xffffffff, best_y = 0xffffffff;
373 unsigned int i;
374
eric miaob0086ef2008-04-30 00:52:19 -0700375 for (i = 0; i < mach->num_modes; i++) {
376 if (modelist[i].xres >= var->xres &&
377 modelist[i].yres >= var->yres &&
378 modelist[i].xres < best_x &&
379 modelist[i].yres < best_y &&
380 modelist[i].bpp >= var->bits_per_pixel) {
Richard Purdied14b2722006-09-20 22:54:21 +0100381 best_x = modelist[i].xres;
382 best_y = modelist[i].yres;
383 mode = &modelist[i];
384 }
385 }
386
387 return mode;
388}
389
eric miaob0086ef2008-04-30 00:52:19 -0700390static void pxafb_setmode(struct fb_var_screeninfo *var,
391 struct pxafb_mode_info *mode)
Richard Purdied14b2722006-09-20 22:54:21 +0100392{
393 var->xres = mode->xres;
394 var->yres = mode->yres;
395 var->bits_per_pixel = mode->bpp;
396 var->pixclock = mode->pixclock;
397 var->hsync_len = mode->hsync_len;
398 var->left_margin = mode->left_margin;
399 var->right_margin = mode->right_margin;
400 var->vsync_len = mode->vsync_len;
401 var->upper_margin = mode->upper_margin;
402 var->lower_margin = mode->lower_margin;
403 var->sync = mode->sync;
404 var->grayscale = mode->cmap_greyscale;
Pieter Grimmerink049ad832009-11-13 10:28:54 +0100405 var->transp.length = mode->transparency;
Eric Miao878f5782008-12-18 22:36:26 +0800406
407 /* set the initial RGBA bitfields */
408 pxafb_set_pixfmt(var, mode->depth);
Richard Purdied14b2722006-09-20 22:54:21 +0100409}
410
Eric Miao3f16ff62008-12-18 22:51:54 +0800411static int pxafb_adjust_timing(struct pxafb_info *fbi,
412 struct fb_var_screeninfo *var)
413{
414 int line_length;
415
416 var->xres = max_t(int, var->xres, MIN_XRES);
417 var->yres = max_t(int, var->yres, MIN_YRES);
418
419 if (!(fbi->lccr0 & LCCR0_LCDT)) {
420 clamp_val(var->hsync_len, 1, 64);
421 clamp_val(var->vsync_len, 1, 64);
422 clamp_val(var->left_margin, 1, 255);
423 clamp_val(var->right_margin, 1, 255);
424 clamp_val(var->upper_margin, 1, 255);
425 clamp_val(var->lower_margin, 1, 255);
426 }
427
428 /* make sure each line is aligned on word boundary */
429 line_length = var->xres * var->bits_per_pixel / 8;
430 line_length = ALIGN(line_length, 4);
431 var->xres = line_length * 8 / var->bits_per_pixel;
432
433 /* we don't support xpan, force xres_virtual to be equal to xres */
434 var->xres_virtual = var->xres;
435
436 if (var->accel_flags & FB_ACCELF_TEXT)
437 var->yres_virtual = fbi->fb.fix.smem_len / line_length;
438 else
439 var->yres_virtual = max(var->yres_virtual, var->yres);
440
441 /* check for limits */
442 if (var->xres > MAX_XRES || var->yres > MAX_YRES)
443 return -EINVAL;
444
445 if (var->yres > var->yres_virtual)
446 return -EINVAL;
447
448 return 0;
Richard Purdied14b2722006-09-20 22:54:21 +0100449}
450
451/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 * pxafb_check_var():
453 * Get the video params out of 'var'. If a value doesn't fit, round it up,
454 * if it's too big, return -EINVAL.
455 *
456 * Round up in the following order: bits_per_pixel, xres,
457 * yres, xres_virtual, yres_virtual, xoffset, yoffset, grayscale,
458 * bitfields, horizontal timing, vertical timing.
459 */
460static int pxafb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
461{
Fabian Frederick29ebebb2014-09-17 21:00:18 +0200462 struct pxafb_info *fbi = container_of(info, struct pxafb_info, fb);
Robert Jarzmikf3621a62015-12-12 22:27:10 +0100463 struct pxafb_mach_info *inf = fbi->inf;
Eric Miao878f5782008-12-18 22:36:26 +0800464 int err;
Richard Purdied14b2722006-09-20 22:54:21 +0100465
466 if (inf->fixed_modes) {
467 struct pxafb_mode_info *mode;
468
469 mode = pxafb_getmode(inf, var);
470 if (!mode)
471 return -EINVAL;
472 pxafb_setmode(var, mode);
Richard Purdied14b2722006-09-20 22:54:21 +0100473 }
474
Eric Miao878f5782008-12-18 22:36:26 +0800475 /* do a test conversion to BPP fields to check the color formats */
476 err = pxafb_var_to_bpp(var);
477 if (err < 0)
478 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
Eric Miao878f5782008-12-18 22:36:26 +0800480 pxafb_set_pixfmt(var, var_to_depth(var));
Stefan Schmidtc1450f12008-07-09 08:06:32 +0100481
Eric Miao3f16ff62008-12-18 22:51:54 +0800482 err = pxafb_adjust_timing(fbi, var);
483 if (err)
484 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486#ifdef CONFIG_CPU_FREQ
Russell King78d3cfd2008-05-17 22:51:14 +0100487 pr_debug("pxafb: dma period = %d ps\n",
488 pxafb_display_dma_period(var));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489#endif
490
491 return 0;
492}
493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494/*
495 * pxafb_set_par():
496 * Set the user defined part of the display for the specified console
497 */
498static int pxafb_set_par(struct fb_info *info)
499{
Fabian Frederick29ebebb2014-09-17 21:00:18 +0200500 struct pxafb_info *fbi = container_of(info, struct pxafb_info, fb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 struct fb_var_screeninfo *var = &info->var;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Stefan Schmidtc1450f12008-07-09 08:06:32 +0100503 if (var->bits_per_pixel >= 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 fbi->fb.fix.visual = FB_VISUAL_TRUECOLOR;
505 else if (!fbi->cmap_static)
506 fbi->fb.fix.visual = FB_VISUAL_PSEUDOCOLOR;
507 else {
508 /*
509 * Some people have weird ideas about wanting static
510 * pseudocolor maps. I suspect their user space
511 * applications are broken.
512 */
513 fbi->fb.fix.visual = FB_VISUAL_STATIC_PSEUDOCOLOR;
514 }
515
516 fbi->fb.fix.line_length = var->xres_virtual *
517 var->bits_per_pixel / 8;
Stefan Schmidtc1450f12008-07-09 08:06:32 +0100518 if (var->bits_per_pixel >= 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 fbi->palette_size = 0;
520 else
eric miaob0086ef2008-04-30 00:52:19 -0700521 fbi->palette_size = var->bits_per_pixel == 1 ?
522 4 : 1 << var->bits_per_pixel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
eric miao2c42dd82008-04-30 00:52:21 -0700524 fbi->palette_cpu = (u16 *)&fbi->dma_buff->palette[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Stefan Schmidtc1450f12008-07-09 08:06:32 +0100526 if (fbi->fb.var.bits_per_pixel >= 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 fb_dealloc_cmap(&fbi->fb.cmap);
528 else
529 fb_alloc_cmap(&fbi->fb.cmap, 1<<fbi->fb.var.bits_per_pixel, 0);
530
531 pxafb_activate_var(var, fbi);
532
533 return 0;
534}
535
Eric Miao6e354842008-12-17 16:50:43 +0800536static int pxafb_pan_display(struct fb_var_screeninfo *var,
537 struct fb_info *info)
538{
Fabian Frederick29ebebb2014-09-17 21:00:18 +0200539 struct pxafb_info *fbi = container_of(info, struct pxafb_info, fb);
Sven Neumann448ac472009-10-22 08:34:34 +0200540 struct fb_var_screeninfo newvar;
Eric Miao6e354842008-12-17 16:50:43 +0800541 int dma = DMA_MAX + DMA_BASE;
542
543 if (fbi->state != C_ENABLE)
544 return 0;
545
Sven Neumann448ac472009-10-22 08:34:34 +0200546 /* Only take .xoffset, .yoffset and .vmode & FB_VMODE_YWRAP from what
547 * was passed in and copy the rest from the old screeninfo.
548 */
549 memcpy(&newvar, &fbi->fb.var, sizeof(newvar));
550 newvar.xoffset = var->xoffset;
551 newvar.yoffset = var->yoffset;
552 newvar.vmode &= ~FB_VMODE_YWRAP;
553 newvar.vmode |= var->vmode & FB_VMODE_YWRAP;
554
555 setup_base_frame(fbi, &newvar, 1);
Eric Miao6e354842008-12-17 16:50:43 +0800556
557 if (fbi->lccr0 & LCCR0_SDS)
558 lcd_writel(fbi, FBR1, fbi->fdadr[dma + 1] | 0x1);
559
560 lcd_writel(fbi, FBR0, fbi->fdadr[dma] | 0x1);
561 return 0;
562}
563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 * pxafb_blank():
566 * Blank the display by setting all palette values to zero. Note, the
567 * 16 bpp mode does not really use the palette, so this will not
568 * blank the display in all modes.
569 */
570static int pxafb_blank(int blank, struct fb_info *info)
571{
Fabian Frederick29ebebb2014-09-17 21:00:18 +0200572 struct pxafb_info *fbi = container_of(info, struct pxafb_info, fb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 int i;
574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 switch (blank) {
576 case FB_BLANK_POWERDOWN:
577 case FB_BLANK_VSYNC_SUSPEND:
578 case FB_BLANK_HSYNC_SUSPEND:
579 case FB_BLANK_NORMAL:
580 if (fbi->fb.fix.visual == FB_VISUAL_PSEUDOCOLOR ||
581 fbi->fb.fix.visual == FB_VISUAL_STATIC_PSEUDOCOLOR)
582 for (i = 0; i < fbi->palette_size; i++)
583 pxafb_setpalettereg(i, 0, 0, 0, 0, info);
584
585 pxafb_schedule_work(fbi, C_DISABLE);
eric miaob0086ef2008-04-30 00:52:19 -0700586 /* TODO if (pxafb_blank_helper) pxafb_blank_helper(blank); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 break;
588
589 case FB_BLANK_UNBLANK:
eric miaob0086ef2008-04-30 00:52:19 -0700590 /* TODO if (pxafb_blank_helper) pxafb_blank_helper(blank); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 if (fbi->fb.fix.visual == FB_VISUAL_PSEUDOCOLOR ||
592 fbi->fb.fix.visual == FB_VISUAL_STATIC_PSEUDOCOLOR)
593 fb_set_cmap(&fbi->fb.cmap, info);
594 pxafb_schedule_work(fbi, C_ENABLE);
595 }
596 return 0;
597}
598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599static struct fb_ops pxafb_ops = {
600 .owner = THIS_MODULE,
601 .fb_check_var = pxafb_check_var,
602 .fb_set_par = pxafb_set_par,
Eric Miao6e354842008-12-17 16:50:43 +0800603 .fb_pan_display = pxafb_pan_display,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 .fb_setcolreg = pxafb_setcolreg,
605 .fb_fillrect = cfb_fillrect,
606 .fb_copyarea = cfb_copyarea,
607 .fb_imageblit = cfb_imageblit,
608 .fb_blank = pxafb_blank,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609};
610
Eric Miao198fc102008-12-23 17:49:43 +0800611#ifdef CONFIG_FB_PXA_OVERLAY
612static void overlay1fb_setup(struct pxafb_layer *ofb)
613{
614 int size = ofb->fb.fix.line_length * ofb->fb.var.yres_virtual;
615 unsigned long start = ofb->video_mem_phys;
616 setup_frame_dma(ofb->fbi, DMA_OV1, PAL_NONE, start, size);
617}
618
619/* Depending on the enable status of overlay1/2, the DMA should be
620 * updated from FDADRx (when disabled) or FBRx (when enabled).
621 */
622static void overlay1fb_enable(struct pxafb_layer *ofb)
623{
624 int enabled = lcd_readl(ofb->fbi, OVL1C1) & OVLxC1_OEN;
625 uint32_t fdadr1 = ofb->fbi->fdadr[DMA_OV1] | (enabled ? 0x1 : 0);
626
627 lcd_writel(ofb->fbi, enabled ? FBR1 : FDADR1, fdadr1);
628 lcd_writel(ofb->fbi, OVL1C2, ofb->control[1]);
629 lcd_writel(ofb->fbi, OVL1C1, ofb->control[0] | OVLxC1_OEN);
630}
631
632static void overlay1fb_disable(struct pxafb_layer *ofb)
633{
Vasily Khoruzhick1b98d7c2011-03-11 11:20:47 +0200634 uint32_t lccr5;
635
636 if (!(lcd_readl(ofb->fbi, OVL1C1) & OVLxC1_OEN))
637 return;
638
639 lccr5 = lcd_readl(ofb->fbi, LCCR5);
Eric Miao198fc102008-12-23 17:49:43 +0800640
641 lcd_writel(ofb->fbi, OVL1C1, ofb->control[0] & ~OVLxC1_OEN);
642
643 lcd_writel(ofb->fbi, LCSR1, LCSR1_BS(1));
644 lcd_writel(ofb->fbi, LCCR5, lccr5 & ~LCSR1_BS(1));
645 lcd_writel(ofb->fbi, FBR1, ofb->fbi->fdadr[DMA_OV1] | 0x3);
646
647 if (wait_for_completion_timeout(&ofb->branch_done, 1 * HZ) == 0)
Joe Perches6f9655b2017-03-21 16:13:10 +0100648 pr_warn("%s: timeout disabling overlay1\n", __func__);
Eric Miao198fc102008-12-23 17:49:43 +0800649
650 lcd_writel(ofb->fbi, LCCR5, lccr5);
651}
652
653static void overlay2fb_setup(struct pxafb_layer *ofb)
654{
655 int size, div = 1, pfor = NONSTD_TO_PFOR(ofb->fb.var.nonstd);
656 unsigned long start[3] = { ofb->video_mem_phys, 0, 0 };
657
658 if (pfor == OVERLAY_FORMAT_RGB || pfor == OVERLAY_FORMAT_YUV444_PACKED) {
659 size = ofb->fb.fix.line_length * ofb->fb.var.yres_virtual;
660 setup_frame_dma(ofb->fbi, DMA_OV2_Y, -1, start[0], size);
661 } else {
662 size = ofb->fb.var.xres_virtual * ofb->fb.var.yres_virtual;
663 switch (pfor) {
664 case OVERLAY_FORMAT_YUV444_PLANAR: div = 1; break;
665 case OVERLAY_FORMAT_YUV422_PLANAR: div = 2; break;
666 case OVERLAY_FORMAT_YUV420_PLANAR: div = 4; break;
667 }
668 start[1] = start[0] + size;
669 start[2] = start[1] + size / div;
670 setup_frame_dma(ofb->fbi, DMA_OV2_Y, -1, start[0], size);
671 setup_frame_dma(ofb->fbi, DMA_OV2_Cb, -1, start[1], size / div);
672 setup_frame_dma(ofb->fbi, DMA_OV2_Cr, -1, start[2], size / div);
673 }
674}
675
676static void overlay2fb_enable(struct pxafb_layer *ofb)
677{
678 int pfor = NONSTD_TO_PFOR(ofb->fb.var.nonstd);
679 int enabled = lcd_readl(ofb->fbi, OVL2C1) & OVLxC1_OEN;
680 uint32_t fdadr2 = ofb->fbi->fdadr[DMA_OV2_Y] | (enabled ? 0x1 : 0);
681 uint32_t fdadr3 = ofb->fbi->fdadr[DMA_OV2_Cb] | (enabled ? 0x1 : 0);
682 uint32_t fdadr4 = ofb->fbi->fdadr[DMA_OV2_Cr] | (enabled ? 0x1 : 0);
683
684 if (pfor == OVERLAY_FORMAT_RGB || pfor == OVERLAY_FORMAT_YUV444_PACKED)
685 lcd_writel(ofb->fbi, enabled ? FBR2 : FDADR2, fdadr2);
686 else {
687 lcd_writel(ofb->fbi, enabled ? FBR2 : FDADR2, fdadr2);
688 lcd_writel(ofb->fbi, enabled ? FBR3 : FDADR3, fdadr3);
689 lcd_writel(ofb->fbi, enabled ? FBR4 : FDADR4, fdadr4);
690 }
691 lcd_writel(ofb->fbi, OVL2C2, ofb->control[1]);
692 lcd_writel(ofb->fbi, OVL2C1, ofb->control[0] | OVLxC1_OEN);
693}
694
695static void overlay2fb_disable(struct pxafb_layer *ofb)
696{
Vasily Khoruzhick1b98d7c2011-03-11 11:20:47 +0200697 uint32_t lccr5;
698
699 if (!(lcd_readl(ofb->fbi, OVL2C1) & OVLxC1_OEN))
700 return;
701
702 lccr5 = lcd_readl(ofb->fbi, LCCR5);
Eric Miao198fc102008-12-23 17:49:43 +0800703
704 lcd_writel(ofb->fbi, OVL2C1, ofb->control[0] & ~OVLxC1_OEN);
705
706 lcd_writel(ofb->fbi, LCSR1, LCSR1_BS(2));
707 lcd_writel(ofb->fbi, LCCR5, lccr5 & ~LCSR1_BS(2));
708 lcd_writel(ofb->fbi, FBR2, ofb->fbi->fdadr[DMA_OV2_Y] | 0x3);
709 lcd_writel(ofb->fbi, FBR3, ofb->fbi->fdadr[DMA_OV2_Cb] | 0x3);
710 lcd_writel(ofb->fbi, FBR4, ofb->fbi->fdadr[DMA_OV2_Cr] | 0x3);
711
712 if (wait_for_completion_timeout(&ofb->branch_done, 1 * HZ) == 0)
Joe Perches6f9655b2017-03-21 16:13:10 +0100713 pr_warn("%s: timeout disabling overlay2\n", __func__);
Eric Miao198fc102008-12-23 17:49:43 +0800714}
715
716static struct pxafb_layer_ops ofb_ops[] = {
717 [0] = {
718 .enable = overlay1fb_enable,
719 .disable = overlay1fb_disable,
720 .setup = overlay1fb_setup,
721 },
722 [1] = {
723 .enable = overlay2fb_enable,
724 .disable = overlay2fb_disable,
725 .setup = overlay2fb_setup,
726 },
727};
728
729static int overlayfb_open(struct fb_info *info, int user)
730{
Fabian Frederick29ebebb2014-09-17 21:00:18 +0200731 struct pxafb_layer *ofb = container_of(info, struct pxafb_layer, fb);
Eric Miao198fc102008-12-23 17:49:43 +0800732
733 /* no support for framebuffer console on overlay */
734 if (user == 0)
735 return -ENODEV;
736
Vasily Khoruzhick23f136562012-02-26 16:52:02 +0300737 if (ofb->usage++ == 0) {
Vasily Khoruzhick1b98d7c2011-03-11 11:20:47 +0200738 /* unblank the base framebuffer */
Vasily Khoruzhick23f136562012-02-26 16:52:02 +0300739 console_lock();
Vasily Khoruzhick1b98d7c2011-03-11 11:20:47 +0200740 fb_blank(&ofb->fbi->fb, FB_BLANK_UNBLANK);
Vasily Khoruzhick23f136562012-02-26 16:52:02 +0300741 console_unlock();
742 }
Eric Miao198fc102008-12-23 17:49:43 +0800743
Eric Miao198fc102008-12-23 17:49:43 +0800744 return 0;
745}
746
747static int overlayfb_release(struct fb_info *info, int user)
748{
Fabian Frederick29ebebb2014-09-17 21:00:18 +0200749 struct pxafb_layer *ofb = container_of(info, struct pxafb_layer, fb);
Eric Miao198fc102008-12-23 17:49:43 +0800750
Vasily Khoruzhick1b98d7c2011-03-11 11:20:47 +0200751 if (ofb->usage == 1) {
752 ofb->ops->disable(ofb);
753 ofb->fb.var.height = -1;
754 ofb->fb.var.width = -1;
755 ofb->fb.var.xres = ofb->fb.var.xres_virtual = 0;
756 ofb->fb.var.yres = ofb->fb.var.yres_virtual = 0;
Eric Miao198fc102008-12-23 17:49:43 +0800757
Vasily Khoruzhick1b98d7c2011-03-11 11:20:47 +0200758 ofb->usage--;
759 }
Eric Miao198fc102008-12-23 17:49:43 +0800760 return 0;
761}
762
763static int overlayfb_check_var(struct fb_var_screeninfo *var,
764 struct fb_info *info)
765{
Fabian Frederick29ebebb2014-09-17 21:00:18 +0200766 struct pxafb_layer *ofb = container_of(info, struct pxafb_layer, fb);
Eric Miao198fc102008-12-23 17:49:43 +0800767 struct fb_var_screeninfo *base_var = &ofb->fbi->fb.var;
768 int xpos, ypos, pfor, bpp;
769
770 xpos = NONSTD_TO_XPOS(var->nonstd);
Vasily Khoruzhickdcf8eee2011-03-11 11:20:49 +0200771 ypos = NONSTD_TO_YPOS(var->nonstd);
Eric Miao198fc102008-12-23 17:49:43 +0800772 pfor = NONSTD_TO_PFOR(var->nonstd);
773
774 bpp = pxafb_var_to_bpp(var);
775 if (bpp < 0)
776 return -EINVAL;
777
778 /* no support for YUV format on overlay1 */
779 if (ofb->id == OVERLAY1 && pfor != 0)
780 return -EINVAL;
781
782 /* for YUV packed formats, bpp = 'minimum bpp of YUV components' */
783 switch (pfor) {
784 case OVERLAY_FORMAT_RGB:
785 bpp = pxafb_var_to_bpp(var);
786 if (bpp < 0)
787 return -EINVAL;
788
789 pxafb_set_pixfmt(var, var_to_depth(var));
790 break;
791 case OVERLAY_FORMAT_YUV444_PACKED: bpp = 24; break;
792 case OVERLAY_FORMAT_YUV444_PLANAR: bpp = 8; break;
793 case OVERLAY_FORMAT_YUV422_PLANAR: bpp = 4; break;
794 case OVERLAY_FORMAT_YUV420_PLANAR: bpp = 2; break;
795 default:
796 return -EINVAL;
797 }
798
799 /* each line must start at a 32-bit word boundary */
800 if ((xpos * bpp) % 32)
801 return -EINVAL;
802
803 /* xres must align on 32-bit word boundary */
804 var->xres = roundup(var->xres * bpp, 32) / bpp;
805
806 if ((xpos + var->xres > base_var->xres) ||
807 (ypos + var->yres > base_var->yres))
808 return -EINVAL;
809
810 var->xres_virtual = var->xres;
811 var->yres_virtual = max(var->yres, var->yres_virtual);
812 return 0;
813}
814
Vasily Khoruzhick1b98d7c2011-03-11 11:20:47 +0200815static int overlayfb_check_video_memory(struct pxafb_layer *ofb)
Eric Miao198fc102008-12-23 17:49:43 +0800816{
817 struct fb_var_screeninfo *var = &ofb->fb.var;
818 int pfor = NONSTD_TO_PFOR(var->nonstd);
819 int size, bpp = 0;
820
821 switch (pfor) {
822 case OVERLAY_FORMAT_RGB: bpp = var->bits_per_pixel; break;
823 case OVERLAY_FORMAT_YUV444_PACKED: bpp = 24; break;
824 case OVERLAY_FORMAT_YUV444_PLANAR: bpp = 24; break;
825 case OVERLAY_FORMAT_YUV422_PLANAR: bpp = 16; break;
826 case OVERLAY_FORMAT_YUV420_PLANAR: bpp = 12; break;
827 }
828
829 ofb->fb.fix.line_length = var->xres_virtual * bpp / 8;
830
831 size = PAGE_ALIGN(ofb->fb.fix.line_length * var->yres_virtual);
832
Eric Miao198fc102008-12-23 17:49:43 +0800833 if (ofb->video_mem) {
834 if (ofb->video_mem_size >= size)
835 return 0;
Eric Miao198fc102008-12-23 17:49:43 +0800836 }
Vasily Khoruzhick1b98d7c2011-03-11 11:20:47 +0200837 return -EINVAL;
Eric Miao198fc102008-12-23 17:49:43 +0800838}
839
840static int overlayfb_set_par(struct fb_info *info)
841{
Fabian Frederick29ebebb2014-09-17 21:00:18 +0200842 struct pxafb_layer *ofb = container_of(info, struct pxafb_layer, fb);
Eric Miao198fc102008-12-23 17:49:43 +0800843 struct fb_var_screeninfo *var = &info->var;
844 int xpos, ypos, pfor, bpp, ret;
845
Vasily Khoruzhick1b98d7c2011-03-11 11:20:47 +0200846 ret = overlayfb_check_video_memory(ofb);
Eric Miao198fc102008-12-23 17:49:43 +0800847 if (ret)
848 return ret;
849
850 bpp = pxafb_var_to_bpp(var);
851 xpos = NONSTD_TO_XPOS(var->nonstd);
Vasily Khoruzhickdcf8eee2011-03-11 11:20:49 +0200852 ypos = NONSTD_TO_YPOS(var->nonstd);
Eric Miao198fc102008-12-23 17:49:43 +0800853 pfor = NONSTD_TO_PFOR(var->nonstd);
854
855 ofb->control[0] = OVLxC1_PPL(var->xres) | OVLxC1_LPO(var->yres) |
856 OVLxC1_BPP(bpp);
857 ofb->control[1] = OVLxC2_XPOS(xpos) | OVLxC2_YPOS(ypos);
858
859 if (ofb->id == OVERLAY2)
860 ofb->control[1] |= OVL2C2_PFOR(pfor);
861
862 ofb->ops->setup(ofb);
863 ofb->ops->enable(ofb);
864 return 0;
865}
866
867static struct fb_ops overlay_fb_ops = {
868 .owner = THIS_MODULE,
869 .fb_open = overlayfb_open,
870 .fb_release = overlayfb_release,
871 .fb_check_var = overlayfb_check_var,
872 .fb_set_par = overlayfb_set_par,
873};
874
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -0800875static void init_pxafb_overlay(struct pxafb_info *fbi, struct pxafb_layer *ofb,
876 int id)
Eric Miao198fc102008-12-23 17:49:43 +0800877{
878 sprintf(ofb->fb.fix.id, "overlay%d", id + 1);
879
880 ofb->fb.fix.type = FB_TYPE_PACKED_PIXELS;
881 ofb->fb.fix.xpanstep = 0;
882 ofb->fb.fix.ypanstep = 1;
883
884 ofb->fb.var.activate = FB_ACTIVATE_NOW;
885 ofb->fb.var.height = -1;
886 ofb->fb.var.width = -1;
887 ofb->fb.var.vmode = FB_VMODE_NONINTERLACED;
888
889 ofb->fb.fbops = &overlay_fb_ops;
890 ofb->fb.flags = FBINFO_FLAG_DEFAULT;
891 ofb->fb.node = -1;
892 ofb->fb.pseudo_palette = NULL;
893
894 ofb->id = id;
895 ofb->ops = &ofb_ops[id];
Vasily Khoruzhick1b98d7c2011-03-11 11:20:47 +0200896 ofb->usage = 0;
Eric Miao198fc102008-12-23 17:49:43 +0800897 ofb->fbi = fbi;
898 init_completion(&ofb->branch_done);
899}
900
Eric Miao782385a2009-03-19 15:24:30 +0800901static inline int pxafb_overlay_supported(void)
902{
903 if (cpu_is_pxa27x() || cpu_is_pxa3xx())
904 return 1;
905
906 return 0;
907}
908
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -0800909static int pxafb_overlay_map_video_memory(struct pxafb_info *pxafb,
910 struct pxafb_layer *ofb)
Vasily Khoruzhick1b98d7c2011-03-11 11:20:47 +0200911{
912 /* We assume that user will use at most video_mem_size for overlay fb,
913 * anyway, it's useless to use 16bpp main plane and 24bpp overlay
914 */
915 ofb->video_mem = alloc_pages_exact(PAGE_ALIGN(pxafb->video_mem_size),
916 GFP_KERNEL | __GFP_ZERO);
917 if (ofb->video_mem == NULL)
918 return -ENOMEM;
919
920 ofb->video_mem_phys = virt_to_phys(ofb->video_mem);
921 ofb->video_mem_size = PAGE_ALIGN(pxafb->video_mem_size);
922
923 mutex_lock(&ofb->fb.mm_lock);
924 ofb->fb.fix.smem_start = ofb->video_mem_phys;
925 ofb->fb.fix.smem_len = pxafb->video_mem_size;
926 mutex_unlock(&ofb->fb.mm_lock);
927
928 ofb->fb.screen_base = ofb->video_mem;
929
930 return 0;
931}
932
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -0800933static void pxafb_overlay_init(struct pxafb_info *fbi)
Eric Miao198fc102008-12-23 17:49:43 +0800934{
935 int i, ret;
936
Eric Miao782385a2009-03-19 15:24:30 +0800937 if (!pxafb_overlay_supported())
Vasily Khoruzhick1b98d7c2011-03-11 11:20:47 +0200938 return;
Eric Miao782385a2009-03-19 15:24:30 +0800939
Eric Miao198fc102008-12-23 17:49:43 +0800940 for (i = 0; i < 2; i++) {
Vasily Khoruzhick1b98d7c2011-03-11 11:20:47 +0200941 struct pxafb_layer *ofb = &fbi->overlay[i];
942 init_pxafb_overlay(fbi, ofb, i);
943 ret = register_framebuffer(&ofb->fb);
Eric Miao198fc102008-12-23 17:49:43 +0800944 if (ret) {
945 dev_err(fbi->dev, "failed to register overlay %d\n", i);
Vasily Khoruzhick1b98d7c2011-03-11 11:20:47 +0200946 continue;
Eric Miao198fc102008-12-23 17:49:43 +0800947 }
Vasily Khoruzhick1b98d7c2011-03-11 11:20:47 +0200948 ret = pxafb_overlay_map_video_memory(fbi, ofb);
949 if (ret) {
950 dev_err(fbi->dev,
951 "failed to map video memory for overlay %d\n",
952 i);
953 unregister_framebuffer(&ofb->fb);
954 continue;
955 }
956 ofb->registered = 1;
Eric Miao198fc102008-12-23 17:49:43 +0800957 }
958
959 /* mask all IU/BS/EOF/SOF interrupts */
960 lcd_writel(fbi, LCCR5, ~0);
961
Eric Miao198fc102008-12-23 17:49:43 +0800962 pr_info("PXA Overlay driver loaded successfully!\n");
Eric Miao198fc102008-12-23 17:49:43 +0800963}
964
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -0800965static void pxafb_overlay_exit(struct pxafb_info *fbi)
Eric Miao198fc102008-12-23 17:49:43 +0800966{
967 int i;
968
Eric Miao782385a2009-03-19 15:24:30 +0800969 if (!pxafb_overlay_supported())
970 return;
971
Vasily Khoruzhick1b98d7c2011-03-11 11:20:47 +0200972 for (i = 0; i < 2; i++) {
973 struct pxafb_layer *ofb = &fbi->overlay[i];
974 if (ofb->registered) {
975 if (ofb->video_mem)
976 free_pages_exact(ofb->video_mem,
977 ofb->video_mem_size);
978 unregister_framebuffer(&ofb->fb);
979 }
980 }
Eric Miao198fc102008-12-23 17:49:43 +0800981}
982#else
983static inline void pxafb_overlay_init(struct pxafb_info *fbi) {}
984static inline void pxafb_overlay_exit(struct pxafb_info *fbi) {}
985#endif /* CONFIG_FB_PXA_OVERLAY */
986
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987/*
988 * Calculate the PCD value from the clock rate (in picoseconds).
989 * We take account of the PPCR clock setting.
990 * From PXA Developer's Manual:
991 *
992 * PixelClock = LCLK
993 * -------------
994 * 2 ( PCD + 1 )
995 *
996 * PCD = LCLK
997 * ------------- - 1
998 * 2(PixelClock)
999 *
1000 * Where:
1001 * LCLK = LCD/Memory Clock
1002 * PCD = LCCR3[7:0]
1003 *
1004 * PixelClock here is in Hz while the pixclock argument given is the
1005 * period in picoseconds. Hence PixelClock = 1 / ( pixclock * 10^-12 )
1006 *
1007 * The function get_lclk_frequency_10khz returns LCLK in units of
1008 * 10khz. Calling the result of this function lclk gives us the
1009 * following
1010 *
1011 * PCD = (lclk * 10^4 ) * ( pixclock * 10^-12 )
1012 * -------------------------------------- - 1
1013 * 2
1014 *
1015 * Factoring the 10^4 and 10^-12 out gives 10^-8 == 1 / 100000000 as used below.
1016 */
eric miaob0086ef2008-04-30 00:52:19 -07001017static inline unsigned int get_pcd(struct pxafb_info *fbi,
1018 unsigned int pixclock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019{
1020 unsigned long long pcd;
1021
1022 /* FIXME: Need to take into account Double Pixel Clock mode
Russell King72e35242007-08-20 10:18:42 +01001023 * (DPC) bit? or perhaps set it based on the various clock
1024 * speeds */
1025 pcd = (unsigned long long)(clk_get_rate(fbi->clk) / 10000);
1026 pcd *= pixclock;
Nicolas Pitrebf1b8ab2005-06-23 21:56:45 +01001027 do_div(pcd, 100000000 * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 /* no need for this, since we should subtract 1 anyway. they cancel */
1029 /* pcd += 1; */ /* make up for integer math truncations */
1030 return (unsigned int)pcd;
1031}
1032
1033/*
Richard Purdieba44cd22005-09-09 13:10:03 -07001034 * Some touchscreens need hsync information from the video driver to
Russell King72e35242007-08-20 10:18:42 +01001035 * function correctly. We export it here. Note that 'hsync_time' and
1036 * the value returned from pxafb_get_hsync_time() is the *reciprocal*
1037 * of the hsync period in seconds.
Richard Purdieba44cd22005-09-09 13:10:03 -07001038 */
1039static inline void set_hsync_time(struct pxafb_info *fbi, unsigned int pcd)
1040{
Russell King72e35242007-08-20 10:18:42 +01001041 unsigned long htime;
Richard Purdieba44cd22005-09-09 13:10:03 -07001042
1043 if ((pcd == 0) || (fbi->fb.var.hsync_len == 0)) {
eric miaob0086ef2008-04-30 00:52:19 -07001044 fbi->hsync_time = 0;
Richard Purdieba44cd22005-09-09 13:10:03 -07001045 return;
1046 }
1047
Russell King72e35242007-08-20 10:18:42 +01001048 htime = clk_get_rate(fbi->clk) / (pcd * fbi->fb.var.hsync_len);
1049
Richard Purdieba44cd22005-09-09 13:10:03 -07001050 fbi->hsync_time = htime;
1051}
1052
1053unsigned long pxafb_get_hsync_time(struct device *dev)
1054{
1055 struct pxafb_info *fbi = dev_get_drvdata(dev);
1056
1057 /* If display is blanked/suspended, hsync isn't active */
1058 if (!fbi || (fbi->state != C_ENABLE))
1059 return 0;
1060
1061 return fbi->hsync_time;
1062}
1063EXPORT_SYMBOL(pxafb_get_hsync_time);
1064
eric miao2c42dd82008-04-30 00:52:21 -07001065static int setup_frame_dma(struct pxafb_info *fbi, int dma, int pal,
Eric Miao198fc102008-12-23 17:49:43 +08001066 unsigned long start, size_t size)
eric miao2c42dd82008-04-30 00:52:21 -07001067{
1068 struct pxafb_dma_descriptor *dma_desc, *pal_desc;
1069 unsigned int dma_desc_off, pal_desc_off;
1070
Eric Miao6e354842008-12-17 16:50:43 +08001071 if (dma < 0 || dma >= DMA_MAX * 2)
eric miao2c42dd82008-04-30 00:52:21 -07001072 return -EINVAL;
1073
1074 dma_desc = &fbi->dma_buff->dma_desc[dma];
1075 dma_desc_off = offsetof(struct pxafb_dma_buff, dma_desc[dma]);
1076
Eric Miao198fc102008-12-23 17:49:43 +08001077 dma_desc->fsadr = start;
eric miao2c42dd82008-04-30 00:52:21 -07001078 dma_desc->fidr = 0;
1079 dma_desc->ldcmd = size;
1080
Eric Miao6e354842008-12-17 16:50:43 +08001081 if (pal < 0 || pal >= PAL_MAX * 2) {
eric miao2c42dd82008-04-30 00:52:21 -07001082 dma_desc->fdadr = fbi->dma_buff_phys + dma_desc_off;
1083 fbi->fdadr[dma] = fbi->dma_buff_phys + dma_desc_off;
1084 } else {
Jürgen Schindele62cfcf42008-06-11 19:56:06 +01001085 pal_desc = &fbi->dma_buff->pal_desc[pal];
1086 pal_desc_off = offsetof(struct pxafb_dma_buff, pal_desc[pal]);
eric miao2c42dd82008-04-30 00:52:21 -07001087
1088 pal_desc->fsadr = fbi->dma_buff_phys + pal * PALETTE_SIZE;
1089 pal_desc->fidr = 0;
1090
1091 if ((fbi->lccr4 & LCCR4_PAL_FOR_MASK) == LCCR4_PAL_FOR_0)
1092 pal_desc->ldcmd = fbi->palette_size * sizeof(u16);
1093 else
1094 pal_desc->ldcmd = fbi->palette_size * sizeof(u32);
1095
1096 pal_desc->ldcmd |= LDCMD_PAL;
1097
1098 /* flip back and forth between palette and frame buffer */
1099 pal_desc->fdadr = fbi->dma_buff_phys + dma_desc_off;
1100 dma_desc->fdadr = fbi->dma_buff_phys + pal_desc_off;
1101 fbi->fdadr[dma] = fbi->dma_buff_phys + dma_desc_off;
1102 }
1103
1104 return 0;
1105}
1106
Sven Neumann448ac472009-10-22 08:34:34 +02001107static void setup_base_frame(struct pxafb_info *fbi,
1108 struct fb_var_screeninfo *var,
1109 int branch)
Eric Miao6e354842008-12-17 16:50:43 +08001110{
Eric Miao6e354842008-12-17 16:50:43 +08001111 struct fb_fix_screeninfo *fix = &fbi->fb.fix;
Eric Miao198fc102008-12-23 17:49:43 +08001112 int nbytes, dma, pal, bpp = var->bits_per_pixel;
1113 unsigned long offset;
Eric Miao6e354842008-12-17 16:50:43 +08001114
1115 dma = DMA_BASE + (branch ? DMA_MAX : 0);
1116 pal = (bpp >= 16) ? PAL_NONE : PAL_BASE + (branch ? PAL_MAX : 0);
1117
1118 nbytes = fix->line_length * var->yres;
Eric Miao198fc102008-12-23 17:49:43 +08001119 offset = fix->line_length * var->yoffset + fbi->video_mem_phys;
Eric Miao6e354842008-12-17 16:50:43 +08001120
1121 if (fbi->lccr0 & LCCR0_SDS) {
1122 nbytes = nbytes / 2;
1123 setup_frame_dma(fbi, dma + 1, PAL_NONE, offset + nbytes, nbytes);
1124 }
1125
1126 setup_frame_dma(fbi, dma, pal, offset, nbytes);
1127}
1128
Eric Miao3c42a442008-04-30 00:52:26 -07001129#ifdef CONFIG_FB_PXA_SMARTPANEL
1130static int setup_smart_dma(struct pxafb_info *fbi)
1131{
1132 struct pxafb_dma_descriptor *dma_desc;
1133 unsigned long dma_desc_off, cmd_buff_off;
1134
1135 dma_desc = &fbi->dma_buff->dma_desc[DMA_CMD];
1136 dma_desc_off = offsetof(struct pxafb_dma_buff, dma_desc[DMA_CMD]);
1137 cmd_buff_off = offsetof(struct pxafb_dma_buff, cmd_buff);
1138
1139 dma_desc->fdadr = fbi->dma_buff_phys + dma_desc_off;
1140 dma_desc->fsadr = fbi->dma_buff_phys + cmd_buff_off;
1141 dma_desc->fidr = 0;
1142 dma_desc->ldcmd = fbi->n_smart_cmds * sizeof(uint16_t);
1143
1144 fbi->fdadr[DMA_CMD] = dma_desc->fdadr;
1145 return 0;
1146}
1147
1148int pxafb_smart_flush(struct fb_info *info)
1149{
1150 struct pxafb_info *fbi = container_of(info, struct pxafb_info, fb);
1151 uint32_t prsr;
1152 int ret = 0;
1153
1154 /* disable controller until all registers are set up */
1155 lcd_writel(fbi, LCCR0, fbi->reg_lccr0 & ~LCCR0_ENB);
1156
1157 /* 1. make it an even number of commands to align on 32-bit boundary
1158 * 2. add the interrupt command to the end of the chain so we can
1159 * keep track of the end of the transfer
1160 */
1161
1162 while (fbi->n_smart_cmds & 1)
1163 fbi->smart_cmds[fbi->n_smart_cmds++] = SMART_CMD_NOOP;
1164
1165 fbi->smart_cmds[fbi->n_smart_cmds++] = SMART_CMD_INTERRUPT;
1166 fbi->smart_cmds[fbi->n_smart_cmds++] = SMART_CMD_WAIT_FOR_VSYNC;
1167 setup_smart_dma(fbi);
1168
1169 /* continue to execute next command */
1170 prsr = lcd_readl(fbi, PRSR) | PRSR_ST_OK | PRSR_CON_NT;
1171 lcd_writel(fbi, PRSR, prsr);
1172
1173 /* stop the processor in case it executed "wait for sync" cmd */
1174 lcd_writel(fbi, CMDCR, 0x0001);
1175
1176 /* don't send interrupts for fifo underruns on channel 6 */
1177 lcd_writel(fbi, LCCR5, LCCR5_IUM(6));
1178
1179 lcd_writel(fbi, LCCR1, fbi->reg_lccr1);
1180 lcd_writel(fbi, LCCR2, fbi->reg_lccr2);
1181 lcd_writel(fbi, LCCR3, fbi->reg_lccr3);
Eric Miaoa0427502008-12-18 22:10:00 +08001182 lcd_writel(fbi, LCCR4, fbi->reg_lccr4);
Eric Miao3c42a442008-04-30 00:52:26 -07001183 lcd_writel(fbi, FDADR0, fbi->fdadr[0]);
1184 lcd_writel(fbi, FDADR6, fbi->fdadr[6]);
1185
1186 /* begin sending */
1187 lcd_writel(fbi, LCCR0, fbi->reg_lccr0 | LCCR0_ENB);
1188
1189 if (wait_for_completion_timeout(&fbi->command_done, HZ/2) == 0) {
Joe Perches6f9655b2017-03-21 16:13:10 +01001190 pr_warn("%s: timeout waiting for command done\n", __func__);
Eric Miao3c42a442008-04-30 00:52:26 -07001191 ret = -ETIMEDOUT;
1192 }
1193
1194 /* quick disable */
1195 prsr = lcd_readl(fbi, PRSR) & ~(PRSR_ST_OK | PRSR_CON_NT);
1196 lcd_writel(fbi, PRSR, prsr);
1197 lcd_writel(fbi, LCCR0, fbi->reg_lccr0 & ~LCCR0_ENB);
1198 lcd_writel(fbi, FDADR6, 0);
1199 fbi->n_smart_cmds = 0;
1200 return ret;
1201}
1202
1203int pxafb_smart_queue(struct fb_info *info, uint16_t *cmds, int n_cmds)
1204{
1205 int i;
1206 struct pxafb_info *fbi = container_of(info, struct pxafb_info, fb);
1207
Eric Miao69bdea72008-12-08 18:46:00 +08001208 for (i = 0; i < n_cmds; i++, cmds++) {
1209 /* if it is a software delay, flush and delay */
1210 if ((*cmds & 0xff00) == SMART_CMD_DELAY) {
1211 pxafb_smart_flush(info);
1212 mdelay(*cmds & 0xff);
1213 continue;
1214 }
1215
1216 /* leave 2 commands for INTERRUPT and WAIT_FOR_SYNC */
Eric Miao3c42a442008-04-30 00:52:26 -07001217 if (fbi->n_smart_cmds == CMD_BUFF_SIZE - 8)
1218 pxafb_smart_flush(info);
1219
Eric Miao69bdea72008-12-08 18:46:00 +08001220 fbi->smart_cmds[fbi->n_smart_cmds++] = *cmds;
Eric Miao3c42a442008-04-30 00:52:26 -07001221 }
1222
1223 return 0;
1224}
1225
1226static unsigned int __smart_timing(unsigned time_ns, unsigned long lcd_clk)
1227{
1228 unsigned int t = (time_ns * (lcd_clk / 1000000) / 1000);
1229 return (t == 0) ? 1 : t;
1230}
1231
1232static void setup_smart_timing(struct pxafb_info *fbi,
1233 struct fb_var_screeninfo *var)
1234{
Robert Jarzmikf3621a62015-12-12 22:27:10 +01001235 struct pxafb_mach_info *inf = fbi->inf;
Eric Miao3c42a442008-04-30 00:52:26 -07001236 struct pxafb_mode_info *mode = &inf->modes[0];
1237 unsigned long lclk = clk_get_rate(fbi->clk);
1238 unsigned t1, t2, t3, t4;
1239
1240 t1 = max(mode->a0csrd_set_hld, mode->a0cswr_set_hld);
1241 t2 = max(mode->rd_pulse_width, mode->wr_pulse_width);
1242 t3 = mode->op_hold_time;
1243 t4 = mode->cmd_inh_time;
1244
1245 fbi->reg_lccr1 =
1246 LCCR1_DisWdth(var->xres) |
1247 LCCR1_BegLnDel(__smart_timing(t1, lclk)) |
1248 LCCR1_EndLnDel(__smart_timing(t2, lclk)) |
1249 LCCR1_HorSnchWdth(__smart_timing(t3, lclk));
1250
1251 fbi->reg_lccr2 = LCCR2_DisHght(var->yres);
Eric Miaoc1f99c22008-12-08 18:35:03 +08001252 fbi->reg_lccr3 = fbi->lccr3 | LCCR3_PixClkDiv(__smart_timing(t4, lclk));
1253 fbi->reg_lccr3 |= (var->sync & FB_SYNC_HOR_HIGH_ACT) ? LCCR3_HSP : 0;
1254 fbi->reg_lccr3 |= (var->sync & FB_SYNC_VERT_HIGH_ACT) ? LCCR3_VSP : 0;
Eric Miao3c42a442008-04-30 00:52:26 -07001255
1256 /* FIXME: make this configurable */
1257 fbi->reg_cmdcr = 1;
1258}
1259
1260static int pxafb_smart_thread(void *arg)
1261{
Eric Miao7f1133c2008-04-30 00:52:27 -07001262 struct pxafb_info *fbi = arg;
Robert Jarzmikf3621a62015-12-12 22:27:10 +01001263 struct pxafb_mach_info *inf = fbi->inf;
Eric Miao3c42a442008-04-30 00:52:26 -07001264
Eric Miaoda2c3f02009-12-26 16:25:18 +08001265 if (!inf->smart_update) {
Eric Miao3c42a442008-04-30 00:52:26 -07001266 pr_err("%s: not properly initialized, thread terminated\n",
1267 __func__);
1268 return -EINVAL;
1269 }
1270
1271 pr_debug("%s(): task starting\n", __func__);
1272
1273 set_freezable();
1274 while (!kthread_should_stop()) {
1275
1276 if (try_to_freeze())
1277 continue;
1278
Eric Miao07f651c2008-12-08 18:51:01 +08001279 mutex_lock(&fbi->ctrlr_lock);
1280
Eric Miao3c42a442008-04-30 00:52:26 -07001281 if (fbi->state == C_ENABLE) {
1282 inf->smart_update(&fbi->fb);
1283 complete(&fbi->refresh_done);
1284 }
1285
Eric Miao07f651c2008-12-08 18:51:01 +08001286 mutex_unlock(&fbi->ctrlr_lock);
1287
Eric Miao3c42a442008-04-30 00:52:26 -07001288 set_current_state(TASK_INTERRUPTIBLE);
Nicholas Mc Guire17713ce2015-02-20 09:34:01 -05001289 schedule_timeout(msecs_to_jiffies(30));
Eric Miao3c42a442008-04-30 00:52:26 -07001290 }
1291
1292 pr_debug("%s(): task ending\n", __func__);
1293 return 0;
1294}
1295
1296static int pxafb_smart_init(struct pxafb_info *fbi)
1297{
Eric Miao07df1c42008-12-04 11:14:11 +08001298 if (!(fbi->lccr0 & LCCR0_LCDT))
Eric Miao6cc4abe2008-11-11 21:47:07 +08001299 return 0;
1300
Eric Miao07df1c42008-12-04 11:14:11 +08001301 fbi->smart_cmds = (uint16_t *) fbi->dma_buff->cmd_buff;
1302 fbi->n_smart_cmds = 0;
1303
1304 init_completion(&fbi->command_done);
1305 init_completion(&fbi->refresh_done);
1306
Eric Miao3c42a442008-04-30 00:52:26 -07001307 fbi->smart_thread = kthread_run(pxafb_smart_thread, fbi,
1308 "lcd_refresh");
1309 if (IS_ERR(fbi->smart_thread)) {
Eric Miao07df1c42008-12-04 11:14:11 +08001310 pr_err("%s: unable to create kernel thread\n", __func__);
Eric Miao3c42a442008-04-30 00:52:26 -07001311 return PTR_ERR(fbi->smart_thread);
1312 }
Eric Miaoa5718a12008-11-11 21:50:39 +08001313
Eric Miao3c42a442008-04-30 00:52:26 -07001314 return 0;
1315}
1316#else
Eric Miao07df1c42008-12-04 11:14:11 +08001317static inline int pxafb_smart_init(struct pxafb_info *fbi) { return 0; }
1318#endif /* CONFIG_FB_PXA_SMARTPANEL */
Eric Miao3c42a442008-04-30 00:52:26 -07001319
Eric Miao90eabbf2008-04-30 00:52:25 -07001320static void setup_parallel_timing(struct pxafb_info *fbi,
1321 struct fb_var_screeninfo *var)
1322{
1323 unsigned int lines_per_panel, pcd = get_pcd(fbi, var->pixclock);
1324
1325 fbi->reg_lccr1 =
1326 LCCR1_DisWdth(var->xres) +
1327 LCCR1_HorSnchWdth(var->hsync_len) +
1328 LCCR1_BegLnDel(var->left_margin) +
1329 LCCR1_EndLnDel(var->right_margin);
1330
1331 /*
1332 * If we have a dual scan LCD, we need to halve
1333 * the YRES parameter.
1334 */
1335 lines_per_panel = var->yres;
1336 if ((fbi->lccr0 & LCCR0_SDS) == LCCR0_Dual)
1337 lines_per_panel /= 2;
1338
1339 fbi->reg_lccr2 =
1340 LCCR2_DisHght(lines_per_panel) +
1341 LCCR2_VrtSnchWdth(var->vsync_len) +
1342 LCCR2_BegFrmDel(var->upper_margin) +
1343 LCCR2_EndFrmDel(var->lower_margin);
1344
1345 fbi->reg_lccr3 = fbi->lccr3 |
1346 (var->sync & FB_SYNC_HOR_HIGH_ACT ?
1347 LCCR3_HorSnchH : LCCR3_HorSnchL) |
1348 (var->sync & FB_SYNC_VERT_HIGH_ACT ?
1349 LCCR3_VrtSnchH : LCCR3_VrtSnchL);
1350
1351 if (pcd) {
1352 fbi->reg_lccr3 |= LCCR3_PixClkDiv(pcd);
1353 set_hsync_time(fbi, pcd);
1354 }
1355}
1356
Richard Purdieba44cd22005-09-09 13:10:03 -07001357/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 * pxafb_activate_var():
eric miaob0086ef2008-04-30 00:52:19 -07001359 * Configures LCD Controller based on entries in var parameter.
1360 * Settings are only written to the controller if changes were made.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 */
eric miaob0086ef2008-04-30 00:52:19 -07001362static int pxafb_activate_var(struct fb_var_screeninfo *var,
1363 struct pxafb_info *fbi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 u_long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 /* Update shadow copy atomically */
1368 local_irq_save(flags);
1369
Eric Miao3c42a442008-04-30 00:52:26 -07001370#ifdef CONFIG_FB_PXA_SMARTPANEL
1371 if (fbi->lccr0 & LCCR0_LCDT)
1372 setup_smart_timing(fbi, var);
1373 else
1374#endif
1375 setup_parallel_timing(fbi, var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
Sven Neumann448ac472009-10-22 08:34:34 +02001377 setup_base_frame(fbi, var, 0);
Eric Miao6e354842008-12-17 16:50:43 +08001378
Eric Miao90eabbf2008-04-30 00:52:25 -07001379 fbi->reg_lccr0 = fbi->lccr0 |
1380 (LCCR0_LDM | LCCR0_SFM | LCCR0_IUM | LCCR0_EFM |
1381 LCCR0_QDM | LCCR0_BM | LCCR0_OUM);
1382
Eric Miao878f5782008-12-18 22:36:26 +08001383 fbi->reg_lccr3 |= pxafb_var_to_lccr3(var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
Eric Miaoa7535ba2008-04-30 00:52:24 -07001385 fbi->reg_lccr4 = lcd_readl(fbi, LCCR4) & ~LCCR4_PAL_FOR_MASK;
Hans J. Koch9ffa7392007-10-16 01:28:41 -07001386 fbi->reg_lccr4 |= (fbi->lccr4 & LCCR4_PAL_FOR_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 local_irq_restore(flags);
1388
1389 /*
1390 * Only update the registers if the controller is enabled
1391 * and something has changed.
1392 */
Eric Miaoa7535ba2008-04-30 00:52:24 -07001393 if ((lcd_readl(fbi, LCCR0) != fbi->reg_lccr0) ||
1394 (lcd_readl(fbi, LCCR1) != fbi->reg_lccr1) ||
1395 (lcd_readl(fbi, LCCR2) != fbi->reg_lccr2) ||
1396 (lcd_readl(fbi, LCCR3) != fbi->reg_lccr3) ||
Eric Miaoa0427502008-12-18 22:10:00 +08001397 (lcd_readl(fbi, LCCR4) != fbi->reg_lccr4) ||
Eric Miaoa7535ba2008-04-30 00:52:24 -07001398 (lcd_readl(fbi, FDADR0) != fbi->fdadr[0]) ||
Vasily Khoruzhick1b98d7c2011-03-11 11:20:47 +02001399 ((fbi->lccr0 & LCCR0_SDS) &&
1400 (lcd_readl(fbi, FDADR1) != fbi->fdadr[1])))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 pxafb_schedule_work(fbi, C_REENABLE);
1402
1403 return 0;
1404}
1405
1406/*
1407 * NOTE! The following functions are purely helpers for set_ctrlr_state.
1408 * Do not call them directly; set_ctrlr_state does the correct serialisation
1409 * to ensure that things happen in the right way 100% of time time.
1410 * -- rmk
1411 */
1412static inline void __pxafb_backlight_power(struct pxafb_info *fbi, int on)
1413{
Russell Kingca5da7102005-09-29 09:44:54 +01001414 pr_debug("pxafb: backlight o%s\n", on ? "n" : "ff");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
Eric Miaoa5718a12008-11-11 21:50:39 +08001416 if (fbi->backlight_power)
1417 fbi->backlight_power(on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418}
1419
1420static inline void __pxafb_lcd_power(struct pxafb_info *fbi, int on)
1421{
Russell Kingca5da7102005-09-29 09:44:54 +01001422 pr_debug("pxafb: LCD power o%s\n", on ? "n" : "ff");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
Eric Miaoa5718a12008-11-11 21:50:39 +08001424 if (fbi->lcd_power)
1425 fbi->lcd_power(on, &fbi->fb.var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426}
1427
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428static void pxafb_enable_controller(struct pxafb_info *fbi)
1429{
Russell Kingca5da7102005-09-29 09:44:54 +01001430 pr_debug("pxafb: Enabling LCD controller\n");
eric miao2c42dd82008-04-30 00:52:21 -07001431 pr_debug("fdadr0 0x%08x\n", (unsigned int) fbi->fdadr[0]);
1432 pr_debug("fdadr1 0x%08x\n", (unsigned int) fbi->fdadr[1]);
Russell Kingca5da7102005-09-29 09:44:54 +01001433 pr_debug("reg_lccr0 0x%08x\n", (unsigned int) fbi->reg_lccr0);
1434 pr_debug("reg_lccr1 0x%08x\n", (unsigned int) fbi->reg_lccr1);
1435 pr_debug("reg_lccr2 0x%08x\n", (unsigned int) fbi->reg_lccr2);
1436 pr_debug("reg_lccr3 0x%08x\n", (unsigned int) fbi->reg_lccr3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
Nicolas Pitre8d372262005-08-10 16:45:13 +01001438 /* enable LCD controller clock */
Arvind Yadav86388d82017-06-14 17:40:56 +02001439 if (clk_prepare_enable(fbi->clk)) {
1440 pr_err("%s: Failed to prepare clock\n", __func__);
1441 return;
1442 }
Nicolas Pitre8d372262005-08-10 16:45:13 +01001443
Eric Miao3c42a442008-04-30 00:52:26 -07001444 if (fbi->lccr0 & LCCR0_LCDT)
1445 return;
1446
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 /* Sequence from 11.7.10 */
Eric Miaoa0427502008-12-18 22:10:00 +08001448 lcd_writel(fbi, LCCR4, fbi->reg_lccr4);
Eric Miaoa7535ba2008-04-30 00:52:24 -07001449 lcd_writel(fbi, LCCR3, fbi->reg_lccr3);
1450 lcd_writel(fbi, LCCR2, fbi->reg_lccr2);
1451 lcd_writel(fbi, LCCR1, fbi->reg_lccr1);
1452 lcd_writel(fbi, LCCR0, fbi->reg_lccr0 & ~LCCR0_ENB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Eric Miaoa7535ba2008-04-30 00:52:24 -07001454 lcd_writel(fbi, FDADR0, fbi->fdadr[0]);
Vasily Khoruzhick1b98d7c2011-03-11 11:20:47 +02001455 if (fbi->lccr0 & LCCR0_SDS)
1456 lcd_writel(fbi, FDADR1, fbi->fdadr[1]);
Eric Miaoa7535ba2008-04-30 00:52:24 -07001457 lcd_writel(fbi, LCCR0, fbi->reg_lccr0 | LCCR0_ENB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458}
1459
1460static void pxafb_disable_controller(struct pxafb_info *fbi)
1461{
eric miaoce4fb7b2008-04-30 00:52:21 -07001462 uint32_t lccr0;
1463
Eric Miao3c42a442008-04-30 00:52:26 -07001464#ifdef CONFIG_FB_PXA_SMARTPANEL
1465 if (fbi->lccr0 & LCCR0_LCDT) {
1466 wait_for_completion_timeout(&fbi->refresh_done,
Nicholas Mc Guire17713ce2015-02-20 09:34:01 -05001467 msecs_to_jiffies(200));
Eric Miao3c42a442008-04-30 00:52:26 -07001468 return;
1469 }
1470#endif
1471
eric miaoce4fb7b2008-04-30 00:52:21 -07001472 /* Clear LCD Status Register */
Eric Miaoa7535ba2008-04-30 00:52:24 -07001473 lcd_writel(fbi, LCSR, 0xffffffff);
eric miaoce4fb7b2008-04-30 00:52:21 -07001474
Eric Miaoa7535ba2008-04-30 00:52:24 -07001475 lccr0 = lcd_readl(fbi, LCCR0) & ~LCCR0_LDM;
1476 lcd_writel(fbi, LCCR0, lccr0);
1477 lcd_writel(fbi, LCCR0, lccr0 | LCCR0_DIS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
Nicholas Mc Guire17713ce2015-02-20 09:34:01 -05001479 wait_for_completion_timeout(&fbi->disable_done, msecs_to_jiffies(200));
Nicolas Pitre8d372262005-08-10 16:45:13 +01001480
1481 /* disable LCD controller clock */
Philipp Zabelb622f1b2012-03-15 19:12:00 +01001482 clk_disable_unprepare(fbi->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483}
1484
1485/*
1486 * pxafb_handle_irq: Handle 'LCD DONE' interrupts.
1487 */
David Howells7d12e782006-10-05 14:55:46 +01001488static irqreturn_t pxafb_handle_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489{
1490 struct pxafb_info *fbi = dev_id;
Denis V. Lunevff14ed52009-04-21 12:23:59 -07001491 unsigned int lccr0, lcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
Eric Miao198fc102008-12-23 17:49:43 +08001493 lcsr = lcd_readl(fbi, LCSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 if (lcsr & LCSR_LDD) {
Eric Miaoa7535ba2008-04-30 00:52:24 -07001495 lccr0 = lcd_readl(fbi, LCCR0);
1496 lcd_writel(fbi, LCCR0, lccr0 | LCCR0_LDM);
Eric Miao2ba162b2008-04-30 00:52:24 -07001497 complete(&fbi->disable_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 }
1499
Eric Miao3c42a442008-04-30 00:52:26 -07001500#ifdef CONFIG_FB_PXA_SMARTPANEL
1501 if (lcsr & LCSR_CMD_INT)
1502 complete(&fbi->command_done);
1503#endif
Eric Miaoa7535ba2008-04-30 00:52:24 -07001504 lcd_writel(fbi, LCSR, lcsr);
Eric Miao198fc102008-12-23 17:49:43 +08001505
1506#ifdef CONFIG_FB_PXA_OVERLAY
Denis V. Lunevff14ed52009-04-21 12:23:59 -07001507 {
1508 unsigned int lcsr1 = lcd_readl(fbi, LCSR1);
1509 if (lcsr1 & LCSR1_BS(1))
1510 complete(&fbi->overlay[0].branch_done);
Eric Miao198fc102008-12-23 17:49:43 +08001511
Denis V. Lunevff14ed52009-04-21 12:23:59 -07001512 if (lcsr1 & LCSR1_BS(2))
1513 complete(&fbi->overlay[1].branch_done);
Eric Miao198fc102008-12-23 17:49:43 +08001514
Denis V. Lunevff14ed52009-04-21 12:23:59 -07001515 lcd_writel(fbi, LCSR1, lcsr1);
1516 }
Eric Miao198fc102008-12-23 17:49:43 +08001517#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 return IRQ_HANDLED;
1519}
1520
1521/*
1522 * This function must be called from task context only, since it will
1523 * sleep when disabling the LCD controller, or if we get two contending
1524 * processes trying to alter state.
1525 */
1526static void set_ctrlr_state(struct pxafb_info *fbi, u_int state)
1527{
1528 u_int old_state;
1529
Matthias Kaehlckeb91dbce2008-07-23 21:31:14 -07001530 mutex_lock(&fbi->ctrlr_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
1532 old_state = fbi->state;
1533
1534 /*
1535 * Hack around fbcon initialisation.
1536 */
1537 if (old_state == C_STARTUP && state == C_REENABLE)
1538 state = C_ENABLE;
1539
1540 switch (state) {
1541 case C_DISABLE_CLKCHANGE:
1542 /*
1543 * Disable controller for clock change. If the
1544 * controller is already disabled, then do nothing.
1545 */
1546 if (old_state != C_DISABLE && old_state != C_DISABLE_PM) {
1547 fbi->state = state;
eric miaob0086ef2008-04-30 00:52:19 -07001548 /* TODO __pxafb_lcd_power(fbi, 0); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 pxafb_disable_controller(fbi);
1550 }
1551 break;
1552
1553 case C_DISABLE_PM:
1554 case C_DISABLE:
1555 /*
1556 * Disable controller
1557 */
1558 if (old_state != C_DISABLE) {
1559 fbi->state = state;
1560 __pxafb_backlight_power(fbi, 0);
1561 __pxafb_lcd_power(fbi, 0);
1562 if (old_state != C_DISABLE_CLKCHANGE)
1563 pxafb_disable_controller(fbi);
1564 }
1565 break;
1566
1567 case C_ENABLE_CLKCHANGE:
1568 /*
1569 * Enable the controller after clock change. Only
1570 * do this if we were disabled for the clock change.
1571 */
1572 if (old_state == C_DISABLE_CLKCHANGE) {
1573 fbi->state = C_ENABLE;
1574 pxafb_enable_controller(fbi);
eric miaob0086ef2008-04-30 00:52:19 -07001575 /* TODO __pxafb_lcd_power(fbi, 1); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 }
1577 break;
1578
1579 case C_REENABLE:
1580 /*
1581 * Re-enable the controller only if it was already
1582 * enabled. This is so we reprogram the control
1583 * registers.
1584 */
1585 if (old_state == C_ENABLE) {
Richard Purdied14b2722006-09-20 22:54:21 +01001586 __pxafb_lcd_power(fbi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 pxafb_disable_controller(fbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 pxafb_enable_controller(fbi);
Richard Purdied14b2722006-09-20 22:54:21 +01001589 __pxafb_lcd_power(fbi, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 }
1591 break;
1592
1593 case C_ENABLE_PM:
1594 /*
1595 * Re-enable the controller after PM. This is not
1596 * perfect - think about the case where we were doing
1597 * a clock change, and we suspended half-way through.
1598 */
1599 if (old_state != C_DISABLE_PM)
1600 break;
1601 /* fall through */
1602
1603 case C_ENABLE:
1604 /*
1605 * Power up the LCD screen, enable controller, and
1606 * turn on the backlight.
1607 */
1608 if (old_state != C_ENABLE) {
1609 fbi->state = C_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 pxafb_enable_controller(fbi);
1611 __pxafb_lcd_power(fbi, 1);
1612 __pxafb_backlight_power(fbi, 1);
1613 }
1614 break;
1615 }
Matthias Kaehlckeb91dbce2008-07-23 21:31:14 -07001616 mutex_unlock(&fbi->ctrlr_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617}
1618
1619/*
1620 * Our LCD controller task (which is called when we blank or unblank)
1621 * via keventd.
1622 */
David Howells6d5aefb2006-12-05 19:36:26 +00001623static void pxafb_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624{
David Howells6d5aefb2006-12-05 19:36:26 +00001625 struct pxafb_info *fbi =
1626 container_of(work, struct pxafb_info, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 u_int state = xchg(&fbi->task_state, -1);
1628
1629 set_ctrlr_state(fbi, state);
1630}
1631
1632#ifdef CONFIG_CPU_FREQ
1633/*
1634 * CPU clock speed change handler. We need to adjust the LCD timing
1635 * parameters when the CPU clock is adjusted by the power management
1636 * subsystem.
1637 *
1638 * TODO: Determine why f->new != 10*get_lclk_frequency_10khz()
1639 */
1640static int
1641pxafb_freq_transition(struct notifier_block *nb, unsigned long val, void *data)
1642{
1643 struct pxafb_info *fbi = TO_INF(nb, freq_transition);
eric miaob0086ef2008-04-30 00:52:19 -07001644 /* TODO struct cpufreq_freqs *f = data; */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 u_int pcd;
1646
1647 switch (val) {
1648 case CPUFREQ_PRECHANGE:
Marek Vasuta6d710f2011-03-22 13:09:50 +01001649#ifdef CONFIG_FB_PXA_OVERLAY
1650 if (!(fbi->overlay[0].usage || fbi->overlay[1].usage))
1651#endif
Vasily Khoruzhick27be9a92011-03-11 11:20:50 +02001652 set_ctrlr_state(fbi, C_DISABLE_CLKCHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 break;
1654
1655 case CPUFREQ_POSTCHANGE:
Russell King72e35242007-08-20 10:18:42 +01001656 pcd = get_pcd(fbi, fbi->fb.var.pixclock);
Richard Purdieba44cd22005-09-09 13:10:03 -07001657 set_hsync_time(fbi, pcd);
eric miaob0086ef2008-04-30 00:52:19 -07001658 fbi->reg_lccr3 = (fbi->reg_lccr3 & ~0xff) |
1659 LCCR3_PixClkDiv(pcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 set_ctrlr_state(fbi, C_ENABLE_CLKCHANGE);
1661 break;
1662 }
1663 return 0;
1664}
1665
1666static int
1667pxafb_freq_policy(struct notifier_block *nb, unsigned long val, void *data)
1668{
1669 struct pxafb_info *fbi = TO_INF(nb, freq_policy);
1670 struct fb_var_screeninfo *var = &fbi->fb.var;
1671 struct cpufreq_policy *policy = data;
1672
1673 switch (val) {
1674 case CPUFREQ_ADJUST:
Holger Schurigac2bf5b2008-02-11 16:52:30 +01001675 pr_debug("min dma period: %d ps, "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 "new clock %d kHz\n", pxafb_display_dma_period(var),
1677 policy->max);
eric miaob0086ef2008-04-30 00:52:19 -07001678 /* TODO: fill in min/max values */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 }
1681 return 0;
1682}
1683#endif
1684
1685#ifdef CONFIG_PM
1686/*
1687 * Power management hooks. Note that we won't be called from IRQ context,
1688 * unlike the blank functions above, so we may sleep.
1689 */
Mike Rapoport4f3edfe2009-07-21 17:51:50 +03001690static int pxafb_suspend(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691{
Mike Rapoport4f3edfe2009-07-21 17:51:50 +03001692 struct pxafb_info *fbi = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
Russell King9480e302005-10-28 09:52:56 -07001694 set_ctrlr_state(fbi, C_DISABLE_PM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 return 0;
1696}
1697
Mike Rapoport4f3edfe2009-07-21 17:51:50 +03001698static int pxafb_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699{
Mike Rapoport4f3edfe2009-07-21 17:51:50 +03001700 struct pxafb_info *fbi = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
Russell King9480e302005-10-28 09:52:56 -07001702 set_ctrlr_state(fbi, C_ENABLE_PM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 return 0;
1704}
Mike Rapoport4f3edfe2009-07-21 17:51:50 +03001705
Alexey Dobriyan47145212009-12-14 18:00:08 -08001706static const struct dev_pm_ops pxafb_pm_ops = {
Mike Rapoport4f3edfe2009-07-21 17:51:50 +03001707 .suspend = pxafb_suspend,
1708 .resume = pxafb_resume,
1709};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710#endif
1711
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -08001712static int pxafb_init_video_memory(struct pxafb_info *fbi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713{
Eric Miao77e19672008-12-16 11:54:34 +08001714 int size = PAGE_ALIGN(fbi->video_mem_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715
Eric Miao77e19672008-12-16 11:54:34 +08001716 fbi->video_mem = alloc_pages_exact(size, GFP_KERNEL | __GFP_ZERO);
1717 if (fbi->video_mem == NULL)
1718 return -ENOMEM;
Eric Miao3c42a442008-04-30 00:52:26 -07001719
Eric Miao77e19672008-12-16 11:54:34 +08001720 fbi->video_mem_phys = virt_to_phys(fbi->video_mem);
1721 fbi->video_mem_size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
Eric Miao77e19672008-12-16 11:54:34 +08001723 fbi->fb.fix.smem_start = fbi->video_mem_phys;
1724 fbi->fb.fix.smem_len = fbi->video_mem_size;
1725 fbi->fb.screen_base = fbi->video_mem;
Eric Miao3c42a442008-04-30 00:52:26 -07001726
Eric Miao77e19672008-12-16 11:54:34 +08001727 return fbi->video_mem ? 0 : -ENOMEM;
eric miao84f43c32008-04-30 00:52:22 -07001728}
1729
Guennadi Liakhovetskiebdf9822008-05-05 15:31:44 +01001730static void pxafb_decode_mach_info(struct pxafb_info *fbi,
1731 struct pxafb_mach_info *inf)
eric miao84f43c32008-04-30 00:52:22 -07001732{
1733 unsigned int lcd_conn = inf->lcd_conn;
Eric Miao77e19672008-12-16 11:54:34 +08001734 struct pxafb_mode_info *m;
1735 int i;
eric miao84f43c32008-04-30 00:52:22 -07001736
1737 fbi->cmap_inverse = inf->cmap_inverse;
1738 fbi->cmap_static = inf->cmap_static;
Eric Miaoa0427502008-12-18 22:10:00 +08001739 fbi->lccr4 = inf->lccr4;
eric miao84f43c32008-04-30 00:52:22 -07001740
Eric Miao1ec26db2008-11-11 21:45:57 +08001741 switch (lcd_conn & LCD_TYPE_MASK) {
eric miao84f43c32008-04-30 00:52:22 -07001742 case LCD_TYPE_MONO_STN:
1743 fbi->lccr0 = LCCR0_CMS;
1744 break;
1745 case LCD_TYPE_MONO_DSTN:
1746 fbi->lccr0 = LCCR0_CMS | LCCR0_SDS;
1747 break;
1748 case LCD_TYPE_COLOR_STN:
1749 fbi->lccr0 = 0;
1750 break;
1751 case LCD_TYPE_COLOR_DSTN:
1752 fbi->lccr0 = LCCR0_SDS;
1753 break;
1754 case LCD_TYPE_COLOR_TFT:
1755 fbi->lccr0 = LCCR0_PAS;
1756 break;
1757 case LCD_TYPE_SMART_PANEL:
1758 fbi->lccr0 = LCCR0_LCDT | LCCR0_PAS;
1759 break;
1760 default:
1761 /* fall back to backward compatibility way */
1762 fbi->lccr0 = inf->lccr0;
1763 fbi->lccr3 = inf->lccr3;
Guennadi Liakhovetskiebdf9822008-05-05 15:31:44 +01001764 goto decode_mode;
eric miao84f43c32008-04-30 00:52:22 -07001765 }
1766
1767 if (lcd_conn == LCD_MONO_STN_8BPP)
1768 fbi->lccr0 |= LCCR0_DPD;
1769
Eric Miao9a1ac7e2008-08-15 02:50:44 -04001770 fbi->lccr0 |= (lcd_conn & LCD_ALTERNATE_MAPPING) ? LCCR0_LDDALT : 0;
1771
eric miao84f43c32008-04-30 00:52:22 -07001772 fbi->lccr3 = LCCR3_Acb((inf->lcd_conn >> 10) & 0xff);
1773 fbi->lccr3 |= (lcd_conn & LCD_BIAS_ACTIVE_LOW) ? LCCR3_OEP : 0;
1774 fbi->lccr3 |= (lcd_conn & LCD_PCLK_EDGE_FALL) ? LCCR3_PCP : 0;
1775
Guennadi Liakhovetskiebdf9822008-05-05 15:31:44 +01001776decode_mode:
Eric Miao77e19672008-12-16 11:54:34 +08001777 pxafb_setmode(&fbi->fb.var, &inf->modes[0]);
1778
1779 /* decide video memory size as follows:
1780 * 1. default to mode of maximum resolution
1781 * 2. allow platform to override
1782 * 3. allow module parameter to override
1783 */
1784 for (i = 0, m = &inf->modes[0]; i < inf->num_modes; i++, m++)
1785 fbi->video_mem_size = max_t(size_t, fbi->video_mem_size,
1786 m->xres * m->yres * m->bpp / 8);
1787
1788 if (inf->video_mem_size > fbi->video_mem_size)
1789 fbi->video_mem_size = inf->video_mem_size;
1790
1791 if (video_mem_size > fbi->video_mem_size)
1792 fbi->video_mem_size = video_mem_size;
eric miao84f43c32008-04-30 00:52:22 -07001793}
1794
Robert Jarzmikf3621a62015-12-12 22:27:10 +01001795static struct pxafb_info *pxafb_init_fbinfo(struct device *dev,
1796 struct pxafb_mach_info *inf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797{
1798 struct pxafb_info *fbi;
1799 void *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800
1801 /* Alloc the pxafb_info and pseudo_palette in one step */
1802 fbi = kmalloc(sizeof(struct pxafb_info) + sizeof(u32) * 16, GFP_KERNEL);
1803 if (!fbi)
1804 return NULL;
1805
1806 memset(fbi, 0, sizeof(struct pxafb_info));
1807 fbi->dev = dev;
Robert Jarzmikf3621a62015-12-12 22:27:10 +01001808 fbi->inf = inf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
Russell Kinge0d8b132008-11-11 17:52:32 +00001810 fbi->clk = clk_get(dev, NULL);
Russell King72e35242007-08-20 10:18:42 +01001811 if (IS_ERR(fbi->clk)) {
1812 kfree(fbi);
1813 return NULL;
1814 }
1815
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 strcpy(fbi->fb.fix.id, PXA_NAME);
1817
1818 fbi->fb.fix.type = FB_TYPE_PACKED_PIXELS;
1819 fbi->fb.fix.type_aux = 0;
1820 fbi->fb.fix.xpanstep = 0;
Eric Miao7e4b19c2008-12-17 14:56:54 +08001821 fbi->fb.fix.ypanstep = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 fbi->fb.fix.ywrapstep = 0;
1823 fbi->fb.fix.accel = FB_ACCEL_NONE;
1824
1825 fbi->fb.var.nonstd = 0;
1826 fbi->fb.var.activate = FB_ACTIVATE_NOW;
1827 fbi->fb.var.height = -1;
1828 fbi->fb.var.width = -1;
Eric Miao7e4b19c2008-12-17 14:56:54 +08001829 fbi->fb.var.accel_flags = FB_ACCELF_TEXT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 fbi->fb.var.vmode = FB_VMODE_NONINTERLACED;
1831
1832 fbi->fb.fbops = &pxafb_ops;
1833 fbi->fb.flags = FBINFO_DEFAULT;
1834 fbi->fb.node = -1;
1835
1836 addr = fbi;
1837 addr = addr + sizeof(struct pxafb_info);
1838 fbi->fb.pseudo_palette = addr;
1839
eric miaob0086ef2008-04-30 00:52:19 -07001840 fbi->state = C_STARTUP;
1841 fbi->task_state = (u_char)-1;
Richard Purdied14b2722006-09-20 22:54:21 +01001842
eric miao84f43c32008-04-30 00:52:22 -07001843 pxafb_decode_mach_info(fbi, inf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
Vasily Khoruzhick7779cee2011-03-11 11:20:48 +02001845#ifdef CONFIG_FB_PXA_OVERLAY
1846 /* place overlay(s) on top of base */
1847 if (pxafb_overlay_supported())
1848 fbi->lccr0 |= LCCR0_OUC;
1849#endif
1850
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 init_waitqueue_head(&fbi->ctrlr_wait);
David Howells6d5aefb2006-12-05 19:36:26 +00001852 INIT_WORK(&fbi->task, pxafb_task);
Matthias Kaehlckeb91dbce2008-07-23 21:31:14 -07001853 mutex_init(&fbi->ctrlr_lock);
Eric Miao2ba162b2008-04-30 00:52:24 -07001854 init_completion(&fbi->disable_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
1856 return fbi;
1857}
1858
1859#ifdef CONFIG_FB_PXA_PARAMETERS
Robert Jarzmikf3621a62015-12-12 22:27:10 +01001860static int parse_opt_mode(struct device *dev, const char *this_opt,
1861 struct pxafb_mach_info *inf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862{
eric miao817daf12008-04-30 00:52:18 -07001863 const char *name = this_opt+5;
1864 unsigned int namelen = strlen(name);
1865 int res_specified = 0, bpp_specified = 0;
1866 unsigned int xres = 0, yres = 0, bpp = 0;
1867 int yres_specified = 0;
1868 int i;
1869 for (i = namelen-1; i >= 0; i--) {
1870 switch (name[i]) {
1871 case '-':
1872 namelen = i;
1873 if (!bpp_specified && !yres_specified) {
1874 bpp = simple_strtoul(&name[i+1], NULL, 0);
1875 bpp_specified = 1;
1876 } else
1877 goto done;
1878 break;
1879 case 'x':
1880 if (!yres_specified) {
1881 yres = simple_strtoul(&name[i+1], NULL, 0);
1882 yres_specified = 1;
1883 } else
1884 goto done;
1885 break;
1886 case '0' ... '9':
1887 break;
1888 default:
1889 goto done;
1890 }
1891 }
1892 if (i < 0 && yres_specified) {
1893 xres = simple_strtoul(name, NULL, 0);
1894 res_specified = 1;
1895 }
1896done:
1897 if (res_specified) {
1898 dev_info(dev, "overriding resolution: %dx%d\n", xres, yres);
1899 inf->modes[0].xres = xres; inf->modes[0].yres = yres;
1900 }
1901 if (bpp_specified)
1902 switch (bpp) {
1903 case 1:
1904 case 2:
1905 case 4:
1906 case 8:
1907 case 16:
1908 inf->modes[0].bpp = bpp;
1909 dev_info(dev, "overriding bit depth: %d\n", bpp);
1910 break;
1911 default:
1912 dev_err(dev, "Depth %d is not valid\n", bpp);
1913 return -EINVAL;
1914 }
1915 return 0;
1916}
1917
Robert Jarzmikf3621a62015-12-12 22:27:10 +01001918static int parse_opt(struct device *dev, char *this_opt,
1919 struct pxafb_mach_info *inf)
eric miao817daf12008-04-30 00:52:18 -07001920{
eric miao817daf12008-04-30 00:52:18 -07001921 struct pxafb_mode_info *mode = &inf->modes[0];
1922 char s[64];
1923
1924 s[0] = '\0';
1925
Eric Miao77e19672008-12-16 11:54:34 +08001926 if (!strncmp(this_opt, "vmem:", 5)) {
1927 video_mem_size = memparse(this_opt + 5, NULL);
1928 } else if (!strncmp(this_opt, "mode:", 5)) {
Robert Jarzmikf3621a62015-12-12 22:27:10 +01001929 return parse_opt_mode(dev, this_opt, inf);
eric miao817daf12008-04-30 00:52:18 -07001930 } else if (!strncmp(this_opt, "pixclock:", 9)) {
1931 mode->pixclock = simple_strtoul(this_opt+9, NULL, 0);
1932 sprintf(s, "pixclock: %ld\n", mode->pixclock);
1933 } else if (!strncmp(this_opt, "left:", 5)) {
1934 mode->left_margin = simple_strtoul(this_opt+5, NULL, 0);
1935 sprintf(s, "left: %u\n", mode->left_margin);
1936 } else if (!strncmp(this_opt, "right:", 6)) {
1937 mode->right_margin = simple_strtoul(this_opt+6, NULL, 0);
1938 sprintf(s, "right: %u\n", mode->right_margin);
1939 } else if (!strncmp(this_opt, "upper:", 6)) {
1940 mode->upper_margin = simple_strtoul(this_opt+6, NULL, 0);
1941 sprintf(s, "upper: %u\n", mode->upper_margin);
1942 } else if (!strncmp(this_opt, "lower:", 6)) {
1943 mode->lower_margin = simple_strtoul(this_opt+6, NULL, 0);
1944 sprintf(s, "lower: %u\n", mode->lower_margin);
1945 } else if (!strncmp(this_opt, "hsynclen:", 9)) {
1946 mode->hsync_len = simple_strtoul(this_opt+9, NULL, 0);
1947 sprintf(s, "hsynclen: %u\n", mode->hsync_len);
1948 } else if (!strncmp(this_opt, "vsynclen:", 9)) {
1949 mode->vsync_len = simple_strtoul(this_opt+9, NULL, 0);
1950 sprintf(s, "vsynclen: %u\n", mode->vsync_len);
1951 } else if (!strncmp(this_opt, "hsync:", 6)) {
1952 if (simple_strtoul(this_opt+6, NULL, 0) == 0) {
1953 sprintf(s, "hsync: Active Low\n");
1954 mode->sync &= ~FB_SYNC_HOR_HIGH_ACT;
1955 } else {
1956 sprintf(s, "hsync: Active High\n");
1957 mode->sync |= FB_SYNC_HOR_HIGH_ACT;
1958 }
1959 } else if (!strncmp(this_opt, "vsync:", 6)) {
1960 if (simple_strtoul(this_opt+6, NULL, 0) == 0) {
1961 sprintf(s, "vsync: Active Low\n");
1962 mode->sync &= ~FB_SYNC_VERT_HIGH_ACT;
1963 } else {
1964 sprintf(s, "vsync: Active High\n");
1965 mode->sync |= FB_SYNC_VERT_HIGH_ACT;
1966 }
1967 } else if (!strncmp(this_opt, "dpc:", 4)) {
1968 if (simple_strtoul(this_opt+4, NULL, 0) == 0) {
1969 sprintf(s, "double pixel clock: false\n");
1970 inf->lccr3 &= ~LCCR3_DPC;
1971 } else {
1972 sprintf(s, "double pixel clock: true\n");
1973 inf->lccr3 |= LCCR3_DPC;
1974 }
1975 } else if (!strncmp(this_opt, "outputen:", 9)) {
1976 if (simple_strtoul(this_opt+9, NULL, 0) == 0) {
1977 sprintf(s, "output enable: active low\n");
1978 inf->lccr3 = (inf->lccr3 & ~LCCR3_OEP) | LCCR3_OutEnL;
1979 } else {
1980 sprintf(s, "output enable: active high\n");
1981 inf->lccr3 = (inf->lccr3 & ~LCCR3_OEP) | LCCR3_OutEnH;
1982 }
1983 } else if (!strncmp(this_opt, "pixclockpol:", 12)) {
1984 if (simple_strtoul(this_opt+12, NULL, 0) == 0) {
1985 sprintf(s, "pixel clock polarity: falling edge\n");
1986 inf->lccr3 = (inf->lccr3 & ~LCCR3_PCP) | LCCR3_PixFlEdg;
1987 } else {
1988 sprintf(s, "pixel clock polarity: rising edge\n");
1989 inf->lccr3 = (inf->lccr3 & ~LCCR3_PCP) | LCCR3_PixRsEdg;
1990 }
1991 } else if (!strncmp(this_opt, "color", 5)) {
1992 inf->lccr0 = (inf->lccr0 & ~LCCR0_CMS) | LCCR0_Color;
1993 } else if (!strncmp(this_opt, "mono", 4)) {
1994 inf->lccr0 = (inf->lccr0 & ~LCCR0_CMS) | LCCR0_Mono;
1995 } else if (!strncmp(this_opt, "active", 6)) {
1996 inf->lccr0 = (inf->lccr0 & ~LCCR0_PAS) | LCCR0_Act;
1997 } else if (!strncmp(this_opt, "passive", 7)) {
1998 inf->lccr0 = (inf->lccr0 & ~LCCR0_PAS) | LCCR0_Pas;
1999 } else if (!strncmp(this_opt, "single", 6)) {
2000 inf->lccr0 = (inf->lccr0 & ~LCCR0_SDS) | LCCR0_Sngl;
2001 } else if (!strncmp(this_opt, "dual", 4)) {
2002 inf->lccr0 = (inf->lccr0 & ~LCCR0_SDS) | LCCR0_Dual;
2003 } else if (!strncmp(this_opt, "4pix", 4)) {
2004 inf->lccr0 = (inf->lccr0 & ~LCCR0_DPD) | LCCR0_4PixMono;
2005 } else if (!strncmp(this_opt, "8pix", 4)) {
2006 inf->lccr0 = (inf->lccr0 & ~LCCR0_DPD) | LCCR0_8PixMono;
2007 } else {
2008 dev_err(dev, "unknown option: %s\n", this_opt);
2009 return -EINVAL;
2010 }
2011
2012 if (s[0] != '\0')
2013 dev_info(dev, "override %s", s);
2014
2015 return 0;
2016}
2017
Robert Jarzmikf3621a62015-12-12 22:27:10 +01002018static int pxafb_parse_options(struct device *dev, char *options,
2019 struct pxafb_mach_info *inf)
eric miao817daf12008-04-30 00:52:18 -07002020{
2021 char *this_opt;
2022 int ret;
2023
2024 if (!options || !*options)
2025 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026
2027 dev_dbg(dev, "options are \"%s\"\n", options ? options : "null");
2028
2029 /* could be made table driven or similar?... */
eric miao817daf12008-04-30 00:52:18 -07002030 while ((this_opt = strsep(&options, ",")) != NULL) {
Robert Jarzmikf3621a62015-12-12 22:27:10 +01002031 ret = parse_opt(dev, this_opt, inf);
eric miao817daf12008-04-30 00:52:18 -07002032 if (ret)
2033 return ret;
2034 }
2035 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036}
eric miao92ac73c2008-04-30 00:52:20 -07002037
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -08002038static char g_options[256] = "";
eric miao92ac73c2008-04-30 00:52:20 -07002039
Jaya Kumarf1edfc42008-06-22 04:27:25 +01002040#ifndef MODULE
Jaya Kumar9e6c2972008-06-22 04:27:27 +01002041static int __init pxafb_setup_options(void)
eric miao92ac73c2008-04-30 00:52:20 -07002042{
2043 char *options = NULL;
2044
2045 if (fb_get_options("pxafb", &options))
2046 return -ENODEV;
2047
2048 if (options)
2049 strlcpy(g_options, options, sizeof(g_options));
2050
2051 return 0;
2052}
2053#else
2054#define pxafb_setup_options() (0)
2055
2056module_param_string(options, g_options, sizeof(g_options), 0);
2057MODULE_PARM_DESC(options, "LCD parameters (see Documentation/fb/pxafb.txt)");
2058#endif
2059
2060#else
2061#define pxafb_parse_options(...) (0)
2062#define pxafb_setup_options() (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063#endif
2064
Eric Miao4f3e2662008-08-16 03:50:51 -04002065#ifdef DEBUG_VAR
2066/* Check for various illegal bit-combinations. Currently only
2067 * a warning is given. */
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -08002068static void pxafb_check_options(struct device *dev, struct pxafb_mach_info *inf)
Eric Miao4f3e2662008-08-16 03:50:51 -04002069{
2070 if (inf->lcd_conn)
2071 return;
2072
2073 if (inf->lccr0 & LCCR0_INVALID_CONFIG_MASK)
2074 dev_warn(dev, "machine LCCR0 setting contains "
2075 "illegal bits: %08x\n",
2076 inf->lccr0 & LCCR0_INVALID_CONFIG_MASK);
2077 if (inf->lccr3 & LCCR3_INVALID_CONFIG_MASK)
2078 dev_warn(dev, "machine LCCR3 setting contains "
2079 "illegal bits: %08x\n",
2080 inf->lccr3 & LCCR3_INVALID_CONFIG_MASK);
2081 if (inf->lccr0 & LCCR0_DPD &&
2082 ((inf->lccr0 & LCCR0_PAS) != LCCR0_Pas ||
2083 (inf->lccr0 & LCCR0_SDS) != LCCR0_Sngl ||
2084 (inf->lccr0 & LCCR0_CMS) != LCCR0_Mono))
2085 dev_warn(dev, "Double Pixel Data (DPD) mode is "
2086 "only valid in passive mono"
2087 " single panel mode\n");
2088 if ((inf->lccr0 & LCCR0_PAS) == LCCR0_Act &&
2089 (inf->lccr0 & LCCR0_SDS) == LCCR0_Dual)
2090 dev_warn(dev, "Dual panel only valid in passive mode\n");
2091 if ((inf->lccr0 & LCCR0_PAS) == LCCR0_Pas &&
2092 (inf->modes->upper_margin || inf->modes->lower_margin))
2093 dev_warn(dev, "Upper and lower margins must be 0 in "
2094 "passive mode\n");
2095}
2096#else
2097#define pxafb_check_options(...) do {} while (0)
2098#endif
2099
Robert Jarzmik420a4882015-12-12 22:27:11 +01002100#if defined(CONFIG_OF)
2101static const char * const lcd_types[] = {
Bartlomiej Zolnierkiewiczdbeef4f2018-05-15 18:03:23 +02002102 "unknown", "mono-stn", "mono-dstn", "color-stn", "color-dstn",
Robert Jarzmik420a4882015-12-12 22:27:11 +01002103 "color-tft", "smart-panel", NULL
2104};
2105
2106static int of_get_pxafb_display(struct device *dev, struct device_node *disp,
2107 struct pxafb_mach_info *info, u32 bus_width)
2108{
2109 struct display_timings *timings;
2110 struct videomode vm;
2111 int i, ret = -EINVAL;
2112 const char *s;
2113
2114 ret = of_property_read_string(disp, "lcd-type", &s);
2115 if (ret)
2116 s = "color-tft";
2117
Andy Shevchenko6d09dfe2018-05-15 12:41:10 +02002118 i = match_string(lcd_types, -1, s);
2119 if (i < 0) {
Robert Jarzmik420a4882015-12-12 22:27:11 +01002120 dev_err(dev, "lcd-type %s is unknown\n", s);
Andy Shevchenko6d09dfe2018-05-15 12:41:10 +02002121 return i;
Robert Jarzmik420a4882015-12-12 22:27:11 +01002122 }
2123 info->lcd_conn |= LCD_CONN_TYPE(i);
2124 info->lcd_conn |= LCD_CONN_WIDTH(bus_width);
2125
2126 timings = of_get_display_timings(disp);
2127 if (!timings)
Dan Carpentere0299902016-07-15 14:07:32 +03002128 return -EINVAL;
Robert Jarzmik420a4882015-12-12 22:27:11 +01002129
2130 ret = -ENOMEM;
2131 info->modes = kmalloc_array(timings->num_timings,
2132 sizeof(info->modes[0]), GFP_KERNEL);
2133 if (!info->modes)
2134 goto out;
2135 info->num_modes = timings->num_timings;
2136
2137 for (i = 0; i < timings->num_timings; i++) {
2138 ret = videomode_from_timings(timings, &vm, i);
2139 if (ret) {
2140 dev_err(dev, "videomode_from_timings %d failed: %d\n",
2141 i, ret);
2142 goto out;
2143 }
2144 if (vm.flags & DISPLAY_FLAGS_PIXDATA_POSEDGE)
2145 info->lcd_conn |= LCD_PCLK_EDGE_RISE;
2146 if (vm.flags & DISPLAY_FLAGS_PIXDATA_NEGEDGE)
2147 info->lcd_conn |= LCD_PCLK_EDGE_FALL;
2148 if (vm.flags & DISPLAY_FLAGS_DE_HIGH)
2149 info->lcd_conn |= LCD_BIAS_ACTIVE_HIGH;
2150 if (vm.flags & DISPLAY_FLAGS_DE_LOW)
2151 info->lcd_conn |= LCD_BIAS_ACTIVE_LOW;
2152 if (vm.flags & DISPLAY_FLAGS_HSYNC_HIGH)
2153 info->modes[i].sync |= FB_SYNC_HOR_HIGH_ACT;
2154 if (vm.flags & DISPLAY_FLAGS_VSYNC_HIGH)
2155 info->modes[i].sync |= FB_SYNC_VERT_HIGH_ACT;
2156
2157 info->modes[i].pixclock = 1000000000UL / (vm.pixelclock / 1000);
2158 info->modes[i].xres = vm.hactive;
2159 info->modes[i].yres = vm.vactive;
2160 info->modes[i].hsync_len = vm.hsync_len;
2161 info->modes[i].left_margin = vm.hback_porch;
2162 info->modes[i].right_margin = vm.hfront_porch;
2163 info->modes[i].vsync_len = vm.vsync_len;
2164 info->modes[i].upper_margin = vm.vback_porch;
2165 info->modes[i].lower_margin = vm.vfront_porch;
2166 }
2167 ret = 0;
2168
2169out:
2170 display_timings_release(timings);
2171 return ret;
2172}
2173
2174static int of_get_pxafb_mode_info(struct device *dev,
2175 struct pxafb_mach_info *info)
2176{
2177 struct device_node *display, *np;
2178 u32 bus_width;
2179 int ret, i;
2180
2181 np = of_graph_get_next_endpoint(dev->of_node, NULL);
2182 if (!np) {
2183 dev_err(dev, "could not find endpoint\n");
2184 return -EINVAL;
2185 }
2186 ret = of_property_read_u32(np, "bus-width", &bus_width);
2187 if (ret) {
2188 dev_err(dev, "no bus-width specified: %d\n", ret);
Wei Yongjund4b9efa2016-08-13 08:57:23 +00002189 of_node_put(np);
Robert Jarzmik420a4882015-12-12 22:27:11 +01002190 return ret;
2191 }
2192
2193 display = of_graph_get_remote_port_parent(np);
2194 of_node_put(np);
2195 if (!display) {
2196 dev_err(dev, "no display defined\n");
2197 return -EINVAL;
2198 }
2199
2200 ret = of_get_pxafb_display(dev, display, info, bus_width);
2201 of_node_put(display);
2202 if (ret)
2203 return ret;
2204
2205 for (i = 0; i < info->num_modes; i++)
2206 info->modes[i].bpp = bus_width;
2207
2208 return 0;
2209}
2210
2211static struct pxafb_mach_info *of_pxafb_of_mach_info(struct device *dev)
2212{
2213 int ret;
2214 struct pxafb_mach_info *info;
2215
2216 if (!dev->of_node)
2217 return NULL;
2218 info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
2219 if (!info)
2220 return ERR_PTR(-ENOMEM);
2221 ret = of_get_pxafb_mode_info(dev, info);
2222 if (ret) {
2223 kfree(info->modes);
2224 return ERR_PTR(ret);
2225 }
2226
2227 /*
2228 * On purpose, neither lccrX registers nor video memory size can be
2229 * specified through device-tree, they are considered more a debug hack
2230 * available through command line.
2231 */
2232 return info;
2233}
2234#else
2235static struct pxafb_mach_info *of_pxafb_of_mach_info(struct device *dev)
2236{
2237 return NULL;
2238}
2239#endif
2240
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -08002241static int pxafb_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242{
2243 struct pxafb_info *fbi;
Robert Jarzmikf3621a62015-12-12 22:27:10 +01002244 struct pxafb_mach_info *inf, *pdata;
eric miaoce4fb7b2008-04-30 00:52:21 -07002245 struct resource *r;
Robert Jarzmikf3621a62015-12-12 22:27:10 +01002246 int i, irq, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247
Richard Purdie2cbbb3b2006-03-31 02:31:53 -08002248 dev_dbg(&dev->dev, "pxafb_probe\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 ret = -ENOMEM;
Robert Jarzmikf3621a62015-12-12 22:27:10 +01002251 pdata = dev_get_platdata(&dev->dev);
2252 inf = devm_kmalloc(&dev->dev, sizeof(*inf), GFP_KERNEL);
Robert Jarzmik6f6abd32015-12-19 13:14:31 +01002253 if (!inf)
2254 goto failed;
Robert Jarzmik420a4882015-12-12 22:27:11 +01002255
Robert Jarzmikf3621a62015-12-12 22:27:10 +01002256 if (pdata) {
2257 *inf = *pdata;
2258 inf->modes =
2259 devm_kmalloc_array(&dev->dev, pdata->num_modes,
2260 sizeof(inf->modes[0]), GFP_KERNEL);
2261 if (!inf->modes)
2262 goto failed;
2263 for (i = 0; i < inf->num_modes; i++)
2264 inf->modes[i] = pdata->modes[i];
2265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266
Robert Jarzmikf3621a62015-12-12 22:27:10 +01002267 if (!pdata)
Robert Jarzmik420a4882015-12-12 22:27:11 +01002268 inf = of_pxafb_of_mach_info(&dev->dev);
2269 if (IS_ERR_OR_NULL(inf))
Robert Jarzmikf3621a62015-12-12 22:27:10 +01002270 goto failed;
2271
2272 ret = pxafb_parse_options(&dev->dev, g_options, inf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 if (ret < 0)
2274 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275
Eric Miao4f3e2662008-08-16 03:50:51 -04002276 pxafb_check_options(&dev->dev, inf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
eric miaob0086ef2008-04-30 00:52:19 -07002278 dev_dbg(&dev->dev, "got a %dx%dx%d LCD\n",
2279 inf->modes->xres,
2280 inf->modes->yres,
2281 inf->modes->bpp);
2282 if (inf->modes->xres == 0 ||
2283 inf->modes->yres == 0 ||
2284 inf->modes->bpp == 0) {
Russell King3ae5eae2005-11-09 22:32:44 +00002285 dev_err(&dev->dev, "Invalid resolution or bit depth\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 ret = -EINVAL;
2287 goto failed;
2288 }
Eric Miaoa5718a12008-11-11 21:50:39 +08002289
Robert Jarzmikf3621a62015-12-12 22:27:10 +01002290 fbi = pxafb_init_fbinfo(&dev->dev, inf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 if (!fbi) {
eric miaob0086ef2008-04-30 00:52:19 -07002292 /* only reason for pxafb_init_fbinfo to fail is kmalloc */
Russell King3ae5eae2005-11-09 22:32:44 +00002293 dev_err(&dev->dev, "Failed to initialize framebuffer device\n");
eric miaob0086ef2008-04-30 00:52:19 -07002294 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 goto failed;
2296 }
2297
Daniel Mack52a7a1c2009-09-10 15:26:30 +02002298 if (cpu_is_pxa3xx() && inf->acceleration_enabled)
2299 fbi->fb.fix.accel = FB_ACCEL_PXA3XX;
2300
Eric Miaoa5718a12008-11-11 21:50:39 +08002301 fbi->backlight_power = inf->pxafb_backlight_power;
2302 fbi->lcd_power = inf->pxafb_lcd_power;
2303
eric miaoce4fb7b2008-04-30 00:52:21 -07002304 r = platform_get_resource(dev, IORESOURCE_MEM, 0);
2305 if (r == NULL) {
2306 dev_err(&dev->dev, "no I/O memory resource defined\n");
2307 ret = -ENODEV;
Jaya Kumaree984762008-06-22 04:27:26 +01002308 goto failed_fbi;
eric miaoce4fb7b2008-04-30 00:52:21 -07002309 }
2310
Daniel Mack53eff412009-06-22 21:08:04 +02002311 r = request_mem_region(r->start, resource_size(r), dev->name);
eric miaoce4fb7b2008-04-30 00:52:21 -07002312 if (r == NULL) {
2313 dev_err(&dev->dev, "failed to request I/O memory\n");
2314 ret = -EBUSY;
Jaya Kumaree984762008-06-22 04:27:26 +01002315 goto failed_fbi;
eric miaoce4fb7b2008-04-30 00:52:21 -07002316 }
2317
Daniel Mack53eff412009-06-22 21:08:04 +02002318 fbi->mmio_base = ioremap(r->start, resource_size(r));
eric miaoce4fb7b2008-04-30 00:52:21 -07002319 if (fbi->mmio_base == NULL) {
2320 dev_err(&dev->dev, "failed to map I/O memory\n");
2321 ret = -EBUSY;
2322 goto failed_free_res;
2323 }
2324
Eric Miao77e19672008-12-16 11:54:34 +08002325 fbi->dma_buff_size = PAGE_ALIGN(sizeof(struct pxafb_dma_buff));
2326 fbi->dma_buff = dma_alloc_coherent(fbi->dev, fbi->dma_buff_size,
2327 &fbi->dma_buff_phys, GFP_KERNEL);
2328 if (fbi->dma_buff == NULL) {
2329 dev_err(&dev->dev, "failed to allocate memory for DMA\n");
2330 ret = -ENOMEM;
2331 goto failed_free_io;
2332 }
2333
2334 ret = pxafb_init_video_memory(fbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 if (ret) {
Russell King3ae5eae2005-11-09 22:32:44 +00002336 dev_err(&dev->dev, "Failed to allocate video RAM: %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 ret = -ENOMEM;
Eric Miao77e19672008-12-16 11:54:34 +08002338 goto failed_free_dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340
eric miaoce4fb7b2008-04-30 00:52:21 -07002341 irq = platform_get_irq(dev, 0);
2342 if (irq < 0) {
2343 dev_err(&dev->dev, "no IRQ defined\n");
2344 ret = -ENODEV;
2345 goto failed_free_mem;
2346 }
2347
Yong Zhangf8798cc2011-09-22 16:59:16 +08002348 ret = request_irq(irq, pxafb_handle_irq, 0, "LCD", fbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 if (ret) {
Russell King3ae5eae2005-11-09 22:32:44 +00002350 dev_err(&dev->dev, "request_irq failed: %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 ret = -EBUSY;
eric miaoce4fb7b2008-04-30 00:52:21 -07002352 goto failed_free_mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 }
2354
Eric Miao3c42a442008-04-30 00:52:26 -07002355 ret = pxafb_smart_init(fbi);
2356 if (ret) {
2357 dev_err(&dev->dev, "failed to initialize smartpanel\n");
2358 goto failed_free_irq;
2359 }
Eric Miao07df1c42008-12-04 11:14:11 +08002360
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 /*
2362 * This makes sure that our colour bitfield
2363 * descriptors are correctly initialised.
2364 */
Jaya Kumaree984762008-06-22 04:27:26 +01002365 ret = pxafb_check_var(&fbi->fb.var, &fbi->fb);
2366 if (ret) {
2367 dev_err(&dev->dev, "failed to get suitable mode\n");
2368 goto failed_free_irq;
2369 }
2370
2371 ret = pxafb_set_par(&fbi->fb);
2372 if (ret) {
2373 dev_err(&dev->dev, "Failed to set parameters\n");
2374 goto failed_free_irq;
2375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376
Russell King3ae5eae2005-11-09 22:32:44 +00002377 platform_set_drvdata(dev, fbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378
2379 ret = register_framebuffer(&fbi->fb);
2380 if (ret < 0) {
eric miaob0086ef2008-04-30 00:52:19 -07002381 dev_err(&dev->dev,
2382 "Failed to register framebuffer device: %d\n", ret);
Jaya Kumaree984762008-06-22 04:27:26 +01002383 goto failed_free_cmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 }
2385
Eric Miao198fc102008-12-23 17:49:43 +08002386 pxafb_overlay_init(fbi);
2387
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388#ifdef CONFIG_CPU_FREQ
2389 fbi->freq_transition.notifier_call = pxafb_freq_transition;
2390 fbi->freq_policy.notifier_call = pxafb_freq_policy;
eric miaob0086ef2008-04-30 00:52:19 -07002391 cpufreq_register_notifier(&fbi->freq_transition,
2392 CPUFREQ_TRANSITION_NOTIFIER);
2393 cpufreq_register_notifier(&fbi->freq_policy,
2394 CPUFREQ_POLICY_NOTIFIER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395#endif
2396
2397 /*
2398 * Ok, now enable the LCD controller
2399 */
2400 set_ctrlr_state(fbi, C_ENABLE);
2401
2402 return 0;
2403
Jaya Kumaree984762008-06-22 04:27:26 +01002404failed_free_cmap:
2405 if (fbi->fb.cmap.len)
2406 fb_dealloc_cmap(&fbi->fb.cmap);
eric miaoce4fb7b2008-04-30 00:52:21 -07002407failed_free_irq:
2408 free_irq(irq, fbi);
eric miaoce4fb7b2008-04-30 00:52:21 -07002409failed_free_mem:
Eric Miao77e19672008-12-16 11:54:34 +08002410 free_pages_exact(fbi->video_mem, fbi->video_mem_size);
2411failed_free_dma:
2412 dma_free_coherent(&dev->dev, fbi->dma_buff_size,
2413 fbi->dma_buff, fbi->dma_buff_phys);
Jaya Kumaree984762008-06-22 04:27:26 +01002414failed_free_io:
2415 iounmap(fbi->mmio_base);
2416failed_free_res:
Daniel Mack53eff412009-06-22 21:08:04 +02002417 release_mem_region(r->start, resource_size(r));
Jaya Kumaree984762008-06-22 04:27:26 +01002418failed_fbi:
2419 clk_put(fbi->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 kfree(fbi);
Jaya Kumaree984762008-06-22 04:27:26 +01002421failed:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 return ret;
2423}
2424
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -08002425static int pxafb_remove(struct platform_device *dev)
Jaya Kumar9f17f282008-06-22 04:27:28 +01002426{
2427 struct pxafb_info *fbi = platform_get_drvdata(dev);
2428 struct resource *r;
2429 int irq;
2430 struct fb_info *info;
2431
2432 if (!fbi)
2433 return 0;
2434
2435 info = &fbi->fb;
2436
Eric Miao198fc102008-12-23 17:49:43 +08002437 pxafb_overlay_exit(fbi);
Jaya Kumar9f17f282008-06-22 04:27:28 +01002438 unregister_framebuffer(info);
2439
2440 pxafb_disable_controller(fbi);
2441
2442 if (fbi->fb.cmap.len)
2443 fb_dealloc_cmap(&fbi->fb.cmap);
2444
2445 irq = platform_get_irq(dev, 0);
2446 free_irq(irq, fbi);
2447
Eric Miao77e19672008-12-16 11:54:34 +08002448 free_pages_exact(fbi->video_mem, fbi->video_mem_size);
2449
Luis R. Rodriguezf6e45662016-01-22 18:34:22 -08002450 dma_free_wc(&dev->dev, fbi->dma_buff_size, fbi->dma_buff,
2451 fbi->dma_buff_phys);
Jaya Kumar9f17f282008-06-22 04:27:28 +01002452
2453 iounmap(fbi->mmio_base);
2454
2455 r = platform_get_resource(dev, IORESOURCE_MEM, 0);
Daniel Mack53eff412009-06-22 21:08:04 +02002456 release_mem_region(r->start, resource_size(r));
Jaya Kumar9f17f282008-06-22 04:27:28 +01002457
2458 clk_put(fbi->clk);
2459 kfree(fbi);
2460
2461 return 0;
2462}
2463
Robert Jarzmik420a4882015-12-12 22:27:11 +01002464static const struct of_device_id pxafb_of_dev_id[] = {
2465 { .compatible = "marvell,pxa270-lcdc", },
2466 { .compatible = "marvell,pxa300-lcdc", },
2467 { .compatible = "marvell,pxa2xx-lcdc", },
2468 { /* sentinel */ }
2469};
2470MODULE_DEVICE_TABLE(of, pxafb_of_dev_id);
2471
Russell King3ae5eae2005-11-09 22:32:44 +00002472static struct platform_driver pxafb_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 .probe = pxafb_probe,
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -08002474 .remove = pxafb_remove,
Russell King3ae5eae2005-11-09 22:32:44 +00002475 .driver = {
2476 .name = "pxa2xx-fb",
Robert Jarzmik420a4882015-12-12 22:27:11 +01002477 .of_match_table = pxafb_of_dev_id,
Mike Rapoport4f3edfe2009-07-21 17:51:50 +03002478#ifdef CONFIG_PM
2479 .pm = &pxafb_pm_ops,
2480#endif
Russell King3ae5eae2005-11-09 22:32:44 +00002481 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482};
2483
Jaya Kumar9e6c2972008-06-22 04:27:27 +01002484static int __init pxafb_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485{
eric miao92ac73c2008-04-30 00:52:20 -07002486 if (pxafb_setup_options())
2487 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488
Russell King3ae5eae2005-11-09 22:32:44 +00002489 return platform_driver_register(&pxafb_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490}
2491
Jaya Kumar9f17f282008-06-22 04:27:28 +01002492static void __exit pxafb_exit(void)
2493{
2494 platform_driver_unregister(&pxafb_driver);
2495}
2496
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497module_init(pxafb_init);
Jaya Kumar9f17f282008-06-22 04:27:28 +01002498module_exit(pxafb_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499
2500MODULE_DESCRIPTION("loadable framebuffer driver for PXA");
2501MODULE_LICENSE("GPL");