blob: eac7c1ace7a583509554987dbc57dc896a776786 [file] [log] [blame]
Sascha Hauerf0a523b2011-01-14 15:22:31 +01001/*
2 * Copyright (C) 2010 Juergen Beisert, Pengutronix
3 *
4 * This code is based on:
5 * Author: Vitaly Wool <vital@embeddedalley.com>
6 *
7 * Copyright 2008-2009 Freescale Semiconductor, Inc. All Rights Reserved.
8 * Copyright 2008 Embedded Alley Solutions, Inc All Rights Reserved.
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 */
19
20#define DRIVER_NAME "mxsfb"
21
22/**
23 * @file
24 * @brief LCDIF driver for i.MX23 and i.MX28
25 *
26 * The LCDIF support four modes of operation
27 * - MPU interface (to drive smart displays) -> not supported yet
28 * - VSYNC interface (like MPU interface plus Vsync) -> not supported yet
29 * - Dotclock interface (to drive LC displays with RGB data and sync signals)
30 * - DVI (to drive ITU-R BT656) -> not supported yet
31 *
32 * This driver depends on a correct setup of the pins used for this purpose
33 * (platform specific).
34 *
35 * For the developer: Don't forget to set the data bus width to the display
36 * in the imx_fb_videomode structure. You will else end up with ugly colours.
37 * If you fight against jitter you can vary the clock delay. This is a feature
38 * of the i.MX28 and you can vary it between 2 ns ... 8 ns in 2 ns steps. Give
39 * the required value in the imx_fb_videomode structure.
40 */
41
Axel Lin36893672011-09-01 08:11:59 +080042#include <linux/module.h>
Sascha Hauerf0a523b2011-01-14 15:22:31 +010043#include <linux/kernel.h>
Shawn Guo73fc6102012-06-25 19:54:35 +080044#include <linux/of_device.h>
45#include <linux/of_gpio.h>
Shawn Guo66940652013-03-14 10:57:34 +080046#include <video/of_display_timing.h>
Sascha Hauerf0a523b2011-01-14 15:22:31 +010047#include <linux/platform_device.h>
48#include <linux/clk.h>
49#include <linux/dma-mapping.h>
50#include <linux/io.h>
Shawn Guofe233b92012-05-06 23:01:41 +080051#include <linux/pinctrl/consumer.h>
Shawn Guoc8b5cfc2013-03-14 13:21:56 +080052#include <linux/fb.h>
Shawn Guo66940652013-03-14 10:57:34 +080053#include <video/videomode.h>
Sascha Hauerf0a523b2011-01-14 15:22:31 +010054
55#define REG_SET 4
56#define REG_CLR 8
57
58#define LCDC_CTRL 0x00
59#define LCDC_CTRL1 0x10
60#define LCDC_V4_CTRL2 0x20
61#define LCDC_V3_TRANSFER_COUNT 0x20
62#define LCDC_V4_TRANSFER_COUNT 0x30
63#define LCDC_V4_CUR_BUF 0x40
64#define LCDC_V4_NEXT_BUF 0x50
65#define LCDC_V3_CUR_BUF 0x30
66#define LCDC_V3_NEXT_BUF 0x40
67#define LCDC_TIMING 0x60
68#define LCDC_VDCTRL0 0x70
69#define LCDC_VDCTRL1 0x80
70#define LCDC_VDCTRL2 0x90
71#define LCDC_VDCTRL3 0xa0
72#define LCDC_VDCTRL4 0xb0
73#define LCDC_DVICTRL0 0xc0
74#define LCDC_DVICTRL1 0xd0
75#define LCDC_DVICTRL2 0xe0
76#define LCDC_DVICTRL3 0xf0
77#define LCDC_DVICTRL4 0x100
78#define LCDC_V4_DATA 0x180
79#define LCDC_V3_DATA 0x1b0
80#define LCDC_V4_DEBUG0 0x1d0
81#define LCDC_V3_DEBUG0 0x1f0
82
83#define CTRL_SFTRST (1 << 31)
84#define CTRL_CLKGATE (1 << 30)
85#define CTRL_BYPASS_COUNT (1 << 19)
86#define CTRL_VSYNC_MODE (1 << 18)
87#define CTRL_DOTCLK_MODE (1 << 17)
88#define CTRL_DATA_SELECT (1 << 16)
89#define CTRL_SET_BUS_WIDTH(x) (((x) & 0x3) << 10)
90#define CTRL_GET_BUS_WIDTH(x) (((x) >> 10) & 0x3)
91#define CTRL_SET_WORD_LENGTH(x) (((x) & 0x3) << 8)
92#define CTRL_GET_WORD_LENGTH(x) (((x) >> 8) & 0x3)
93#define CTRL_MASTER (1 << 5)
94#define CTRL_DF16 (1 << 3)
95#define CTRL_DF18 (1 << 2)
96#define CTRL_DF24 (1 << 1)
97#define CTRL_RUN (1 << 0)
98
99#define CTRL1_FIFO_CLEAR (1 << 21)
100#define CTRL1_SET_BYTE_PACKAGING(x) (((x) & 0xf) << 16)
101#define CTRL1_GET_BYTE_PACKAGING(x) (((x) >> 16) & 0xf)
102
103#define TRANSFER_COUNT_SET_VCOUNT(x) (((x) & 0xffff) << 16)
104#define TRANSFER_COUNT_GET_VCOUNT(x) (((x) >> 16) & 0xffff)
105#define TRANSFER_COUNT_SET_HCOUNT(x) ((x) & 0xffff)
106#define TRANSFER_COUNT_GET_HCOUNT(x) ((x) & 0xffff)
107
108
109#define VDCTRL0_ENABLE_PRESENT (1 << 28)
110#define VDCTRL0_VSYNC_ACT_HIGH (1 << 27)
111#define VDCTRL0_HSYNC_ACT_HIGH (1 << 26)
Shawn Guoc8b5cfc2013-03-14 13:21:56 +0800112#define VDCTRL0_DOTCLK_ACT_FALLING (1 << 25)
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100113#define VDCTRL0_ENABLE_ACT_HIGH (1 << 24)
114#define VDCTRL0_VSYNC_PERIOD_UNIT (1 << 21)
115#define VDCTRL0_VSYNC_PULSE_WIDTH_UNIT (1 << 20)
116#define VDCTRL0_HALF_LINE (1 << 19)
117#define VDCTRL0_HALF_LINE_MODE (1 << 18)
118#define VDCTRL0_SET_VSYNC_PULSE_WIDTH(x) ((x) & 0x3ffff)
119#define VDCTRL0_GET_VSYNC_PULSE_WIDTH(x) ((x) & 0x3ffff)
120
121#define VDCTRL2_SET_HSYNC_PERIOD(x) ((x) & 0x3ffff)
122#define VDCTRL2_GET_HSYNC_PERIOD(x) ((x) & 0x3ffff)
123
124#define VDCTRL3_MUX_SYNC_SIGNALS (1 << 29)
125#define VDCTRL3_VSYNC_ONLY (1 << 28)
126#define SET_HOR_WAIT_CNT(x) (((x) & 0xfff) << 16)
127#define GET_HOR_WAIT_CNT(x) (((x) >> 16) & 0xfff)
128#define SET_VERT_WAIT_CNT(x) ((x) & 0xffff)
129#define GET_VERT_WAIT_CNT(x) ((x) & 0xffff)
130
131#define VDCTRL4_SET_DOTCLK_DLY(x) (((x) & 0x7) << 29) /* v4 only */
132#define VDCTRL4_GET_DOTCLK_DLY(x) (((x) >> 29) & 0x7) /* v4 only */
133#define VDCTRL4_SYNC_SIGNALS_ON (1 << 18)
134#define SET_DOTCLK_H_VALID_DATA_CNT(x) ((x) & 0x3ffff)
135
136#define DEBUG0_HSYNC (1 < 26)
137#define DEBUG0_VSYNC (1 < 25)
138
139#define MIN_XRES 120
140#define MIN_YRES 120
141
142#define RED 0
143#define GREEN 1
144#define BLUE 2
145#define TRANSP 3
146
Shawn Guoc8b5cfc2013-03-14 13:21:56 +0800147#define STMLCDIF_8BIT 1 /** pixel data bus to the display is of 8 bit width */
148#define STMLCDIF_16BIT 0 /** pixel data bus to the display is of 16 bit width */
149#define STMLCDIF_18BIT 2 /** pixel data bus to the display is of 18 bit width */
150#define STMLCDIF_24BIT 3 /** pixel data bus to the display is of 24 bit width */
151
152#define MXSFB_SYNC_DATA_ENABLE_HIGH_ACT (1 << 6)
153#define MXSFB_SYNC_DOTCLK_FALLING_ACT (1 << 7) /* negtive edge sampling */
154
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100155enum mxsfb_devtype {
156 MXSFB_V3,
157 MXSFB_V4,
158};
159
160/* CPU dependent register offsets */
161struct mxsfb_devdata {
162 unsigned transfer_count;
163 unsigned cur_buf;
164 unsigned next_buf;
165 unsigned debug0;
166 unsigned hs_wdth_mask;
167 unsigned hs_wdth_shift;
168 unsigned ipversion;
169};
170
171struct mxsfb_info {
172 struct fb_info fb_info;
173 struct platform_device *pdev;
174 struct clk *clk;
175 void __iomem *base; /* registers */
176 unsigned allocated_size;
177 int enabled;
178 unsigned ld_intf_width;
179 unsigned dotclk_delay;
180 const struct mxsfb_devdata *devdata;
Marek Vasut6a150752013-03-18 19:24:02 +0100181 u32 sync;
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100182};
183
184#define mxsfb_is_v3(host) (host->devdata->ipversion == 3)
185#define mxsfb_is_v4(host) (host->devdata->ipversion == 4)
186
187static const struct mxsfb_devdata mxsfb_devdata[] = {
188 [MXSFB_V3] = {
189 .transfer_count = LCDC_V3_TRANSFER_COUNT,
190 .cur_buf = LCDC_V3_CUR_BUF,
191 .next_buf = LCDC_V3_NEXT_BUF,
192 .debug0 = LCDC_V3_DEBUG0,
193 .hs_wdth_mask = 0xff,
194 .hs_wdth_shift = 24,
195 .ipversion = 3,
196 },
197 [MXSFB_V4] = {
198 .transfer_count = LCDC_V4_TRANSFER_COUNT,
199 .cur_buf = LCDC_V4_CUR_BUF,
200 .next_buf = LCDC_V4_NEXT_BUF,
201 .debug0 = LCDC_V4_DEBUG0,
202 .hs_wdth_mask = 0x3fff,
203 .hs_wdth_shift = 18,
204 .ipversion = 4,
205 },
206};
207
208#define to_imxfb_host(x) (container_of(x, struct mxsfb_info, fb_info))
209
210/* mask and shift depends on architecture */
211static inline u32 set_hsync_pulse_width(struct mxsfb_info *host, unsigned val)
212{
213 return (val & host->devdata->hs_wdth_mask) <<
214 host->devdata->hs_wdth_shift;
215}
216
217static inline u32 get_hsync_pulse_width(struct mxsfb_info *host, unsigned val)
218{
219 return (val >> host->devdata->hs_wdth_shift) &
220 host->devdata->hs_wdth_mask;
221}
222
223static const struct fb_bitfield def_rgb565[] = {
224 [RED] = {
225 .offset = 11,
226 .length = 5,
227 },
228 [GREEN] = {
229 .offset = 5,
230 .length = 6,
231 },
232 [BLUE] = {
233 .offset = 0,
234 .length = 5,
235 },
236 [TRANSP] = { /* no support for transparency */
237 .length = 0,
238 }
239};
240
241static const struct fb_bitfield def_rgb666[] = {
242 [RED] = {
243 .offset = 16,
244 .length = 6,
245 },
246 [GREEN] = {
247 .offset = 8,
248 .length = 6,
249 },
250 [BLUE] = {
251 .offset = 0,
252 .length = 6,
253 },
254 [TRANSP] = { /* no support for transparency */
255 .length = 0,
256 }
257};
258
259static const struct fb_bitfield def_rgb888[] = {
260 [RED] = {
261 .offset = 16,
262 .length = 8,
263 },
264 [GREEN] = {
265 .offset = 8,
266 .length = 8,
267 },
268 [BLUE] = {
269 .offset = 0,
270 .length = 8,
271 },
272 [TRANSP] = { /* no support for transparency */
273 .length = 0,
274 }
275};
276
277static inline unsigned chan_to_field(unsigned chan, struct fb_bitfield *bf)
278{
279 chan &= 0xffff;
280 chan >>= 16 - bf->length;
281 return chan << bf->offset;
282}
283
284static int mxsfb_check_var(struct fb_var_screeninfo *var,
285 struct fb_info *fb_info)
286{
287 struct mxsfb_info *host = to_imxfb_host(fb_info);
288 const struct fb_bitfield *rgb = NULL;
289
290 if (var->xres < MIN_XRES)
291 var->xres = MIN_XRES;
292 if (var->yres < MIN_YRES)
293 var->yres = MIN_YRES;
294
295 var->xres_virtual = var->xres;
296
297 var->yres_virtual = var->yres;
298
299 switch (var->bits_per_pixel) {
300 case 16:
301 /* always expect RGB 565 */
302 rgb = def_rgb565;
303 break;
304 case 32:
305 switch (host->ld_intf_width) {
306 case STMLCDIF_8BIT:
307 pr_debug("Unsupported LCD bus width mapping\n");
308 break;
309 case STMLCDIF_16BIT:
310 case STMLCDIF_18BIT:
311 /* 24 bit to 18 bit mapping */
312 rgb = def_rgb666;
313 break;
314 case STMLCDIF_24BIT:
315 /* real 24 bit */
316 rgb = def_rgb888;
317 break;
318 }
319 break;
320 default:
321 pr_debug("Unsupported colour depth: %u\n", var->bits_per_pixel);
322 return -EINVAL;
323 }
324
325 /*
326 * Copy the RGB parameters for this display
327 * from the machine specific parameters.
328 */
329 var->red = rgb[RED];
330 var->green = rgb[GREEN];
331 var->blue = rgb[BLUE];
332 var->transp = rgb[TRANSP];
333
334 return 0;
335}
336
337static void mxsfb_enable_controller(struct fb_info *fb_info)
338{
339 struct mxsfb_info *host = to_imxfb_host(fb_info);
340 u32 reg;
341
342 dev_dbg(&host->pdev->dev, "%s\n", __func__);
343
Shawn Guoca4c22d32011-12-20 14:12:54 +0800344 clk_prepare_enable(host->clk);
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100345 clk_set_rate(host->clk, PICOS2KHZ(fb_info->var.pixclock) * 1000U);
346
347 /* if it was disabled, re-enable the mode again */
348 writel(CTRL_DOTCLK_MODE, host->base + LCDC_CTRL + REG_SET);
349
350 /* enable the SYNC signals first, then the DMA engine */
351 reg = readl(host->base + LCDC_VDCTRL4);
352 reg |= VDCTRL4_SYNC_SIGNALS_ON;
353 writel(reg, host->base + LCDC_VDCTRL4);
354
355 writel(CTRL_RUN, host->base + LCDC_CTRL + REG_SET);
356
357 host->enabled = 1;
358}
359
360static void mxsfb_disable_controller(struct fb_info *fb_info)
361{
362 struct mxsfb_info *host = to_imxfb_host(fb_info);
363 unsigned loop;
364 u32 reg;
365
366 dev_dbg(&host->pdev->dev, "%s\n", __func__);
367
368 /*
369 * Even if we disable the controller here, it will still continue
370 * until its FIFOs are running out of data
371 */
372 writel(CTRL_DOTCLK_MODE, host->base + LCDC_CTRL + REG_CLR);
373
374 loop = 1000;
375 while (loop) {
376 reg = readl(host->base + LCDC_CTRL);
377 if (!(reg & CTRL_RUN))
378 break;
379 loop--;
380 }
381
Lothar Waßmann6c1ecba2012-11-22 13:49:14 +0100382 reg = readl(host->base + LCDC_VDCTRL4);
383 writel(reg & ~VDCTRL4_SYNC_SIGNALS_ON, host->base + LCDC_VDCTRL4);
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100384
Shawn Guoca4c22d32011-12-20 14:12:54 +0800385 clk_disable_unprepare(host->clk);
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100386
387 host->enabled = 0;
388}
389
390static int mxsfb_set_par(struct fb_info *fb_info)
391{
392 struct mxsfb_info *host = to_imxfb_host(fb_info);
393 u32 ctrl, vdctrl0, vdctrl4;
394 int line_size, fb_size;
395 int reenable = 0;
396
397 line_size = fb_info->var.xres * (fb_info->var.bits_per_pixel >> 3);
398 fb_size = fb_info->var.yres_virtual * line_size;
399
400 if (fb_size > fb_info->fix.smem_len)
401 return -ENOMEM;
402
403 fb_info->fix.line_length = line_size;
404
405 /*
406 * It seems, you can't re-program the controller if it is still running.
407 * This may lead into shifted pictures (FIFO issue?).
408 * So, first stop the controller and drain its FIFOs
409 */
410 if (host->enabled) {
411 reenable = 1;
412 mxsfb_disable_controller(fb_info);
413 }
414
415 /* clear the FIFOs */
416 writel(CTRL1_FIFO_CLEAR, host->base + LCDC_CTRL1 + REG_SET);
417
418 ctrl = CTRL_BYPASS_COUNT | CTRL_MASTER |
Justin P. Mattock6eab04a2011-04-08 19:49:08 -0700419 CTRL_SET_BUS_WIDTH(host->ld_intf_width);
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100420
421 switch (fb_info->var.bits_per_pixel) {
422 case 16:
423 dev_dbg(&host->pdev->dev, "Setting up RGB565 mode\n");
424 ctrl |= CTRL_SET_WORD_LENGTH(0);
425 writel(CTRL1_SET_BYTE_PACKAGING(0xf), host->base + LCDC_CTRL1);
426 break;
427 case 32:
428 dev_dbg(&host->pdev->dev, "Setting up RGB888/666 mode\n");
429 ctrl |= CTRL_SET_WORD_LENGTH(3);
430 switch (host->ld_intf_width) {
431 case STMLCDIF_8BIT:
432 dev_dbg(&host->pdev->dev,
433 "Unsupported LCD bus width mapping\n");
434 return -EINVAL;
435 case STMLCDIF_16BIT:
436 case STMLCDIF_18BIT:
437 /* 24 bit to 18 bit mapping */
438 ctrl |= CTRL_DF24; /* ignore the upper 2 bits in
439 * each colour component
440 */
441 break;
442 case STMLCDIF_24BIT:
443 /* real 24 bit */
444 break;
445 }
446 /* do not use packed pixels = one pixel per word instead */
447 writel(CTRL1_SET_BYTE_PACKAGING(0x7), host->base + LCDC_CTRL1);
448 break;
449 default:
450 dev_dbg(&host->pdev->dev, "Unhandled color depth of %u\n",
451 fb_info->var.bits_per_pixel);
452 return -EINVAL;
453 }
454
455 writel(ctrl, host->base + LCDC_CTRL);
456
457 writel(TRANSFER_COUNT_SET_VCOUNT(fb_info->var.yres) |
458 TRANSFER_COUNT_SET_HCOUNT(fb_info->var.xres),
459 host->base + host->devdata->transfer_count);
460
461 vdctrl0 = VDCTRL0_ENABLE_PRESENT | /* always in DOTCLOCK mode */
462 VDCTRL0_VSYNC_PERIOD_UNIT |
463 VDCTRL0_VSYNC_PULSE_WIDTH_UNIT |
464 VDCTRL0_SET_VSYNC_PULSE_WIDTH(fb_info->var.vsync_len);
465 if (fb_info->var.sync & FB_SYNC_HOR_HIGH_ACT)
466 vdctrl0 |= VDCTRL0_HSYNC_ACT_HIGH;
467 if (fb_info->var.sync & FB_SYNC_VERT_HIGH_ACT)
468 vdctrl0 |= VDCTRL0_VSYNC_ACT_HIGH;
Marek Vasut6a150752013-03-18 19:24:02 +0100469 if (host->sync & MXSFB_SYNC_DATA_ENABLE_HIGH_ACT)
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100470 vdctrl0 |= VDCTRL0_ENABLE_ACT_HIGH;
Shawn Guoc8b5cfc2013-03-14 13:21:56 +0800471 if (host->sync & MXSFB_SYNC_DOTCLK_FALLING_ACT)
472 vdctrl0 |= VDCTRL0_DOTCLK_ACT_FALLING;
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100473
474 writel(vdctrl0, host->base + LCDC_VDCTRL0);
475
476 /* frame length in lines */
477 writel(fb_info->var.upper_margin + fb_info->var.vsync_len +
478 fb_info->var.lower_margin + fb_info->var.yres,
479 host->base + LCDC_VDCTRL1);
480
481 /* line length in units of clocks or pixels */
482 writel(set_hsync_pulse_width(host, fb_info->var.hsync_len) |
483 VDCTRL2_SET_HSYNC_PERIOD(fb_info->var.left_margin +
484 fb_info->var.hsync_len + fb_info->var.right_margin +
485 fb_info->var.xres),
486 host->base + LCDC_VDCTRL2);
487
488 writel(SET_HOR_WAIT_CNT(fb_info->var.left_margin +
489 fb_info->var.hsync_len) |
490 SET_VERT_WAIT_CNT(fb_info->var.upper_margin +
491 fb_info->var.vsync_len),
492 host->base + LCDC_VDCTRL3);
493
494 vdctrl4 = SET_DOTCLK_H_VALID_DATA_CNT(fb_info->var.xres);
495 if (mxsfb_is_v4(host))
496 vdctrl4 |= VDCTRL4_SET_DOTCLK_DLY(host->dotclk_delay);
497 writel(vdctrl4, host->base + LCDC_VDCTRL4);
498
499 writel(fb_info->fix.smem_start +
500 fb_info->fix.line_length * fb_info->var.yoffset,
501 host->base + host->devdata->next_buf);
502
503 if (reenable)
504 mxsfb_enable_controller(fb_info);
505
506 return 0;
507}
508
509static int mxsfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
510 u_int transp, struct fb_info *fb_info)
511{
512 unsigned int val;
513 int ret = -EINVAL;
514
515 /*
516 * If greyscale is true, then we convert the RGB value
517 * to greyscale no matter what visual we are using.
518 */
519 if (fb_info->var.grayscale)
520 red = green = blue = (19595 * red + 38470 * green +
521 7471 * blue) >> 16;
522
523 switch (fb_info->fix.visual) {
524 case FB_VISUAL_TRUECOLOR:
525 /*
526 * 12 or 16-bit True Colour. We encode the RGB value
527 * according to the RGB bitfield information.
528 */
529 if (regno < 16) {
530 u32 *pal = fb_info->pseudo_palette;
531
532 val = chan_to_field(red, &fb_info->var.red);
533 val |= chan_to_field(green, &fb_info->var.green);
534 val |= chan_to_field(blue, &fb_info->var.blue);
535
536 pal[regno] = val;
537 ret = 0;
538 }
539 break;
540
541 case FB_VISUAL_STATIC_PSEUDOCOLOR:
542 case FB_VISUAL_PSEUDOCOLOR:
543 break;
544 }
545
546 return ret;
547}
548
549static int mxsfb_blank(int blank, struct fb_info *fb_info)
550{
551 struct mxsfb_info *host = to_imxfb_host(fb_info);
552
553 switch (blank) {
554 case FB_BLANK_POWERDOWN:
555 case FB_BLANK_VSYNC_SUSPEND:
556 case FB_BLANK_HSYNC_SUSPEND:
557 case FB_BLANK_NORMAL:
558 if (host->enabled)
559 mxsfb_disable_controller(fb_info);
560 break;
561
562 case FB_BLANK_UNBLANK:
563 if (!host->enabled)
564 mxsfb_enable_controller(fb_info);
565 break;
566 }
567 return 0;
568}
569
570static int mxsfb_pan_display(struct fb_var_screeninfo *var,
571 struct fb_info *fb_info)
572{
573 struct mxsfb_info *host = to_imxfb_host(fb_info);
574 unsigned offset;
575
576 if (var->xoffset != 0)
577 return -EINVAL;
578
579 offset = fb_info->fix.line_length * var->yoffset;
580
581 /* update on next VSYNC */
582 writel(fb_info->fix.smem_start + offset,
583 host->base + host->devdata->next_buf);
584
585 return 0;
586}
587
588static struct fb_ops mxsfb_ops = {
589 .owner = THIS_MODULE,
590 .fb_check_var = mxsfb_check_var,
591 .fb_set_par = mxsfb_set_par,
592 .fb_setcolreg = mxsfb_setcolreg,
593 .fb_blank = mxsfb_blank,
594 .fb_pan_display = mxsfb_pan_display,
595 .fb_fillrect = cfb_fillrect,
596 .fb_copyarea = cfb_copyarea,
597 .fb_imageblit = cfb_imageblit,
598};
599
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -0800600static int mxsfb_restore_mode(struct mxsfb_info *host)
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100601{
602 struct fb_info *fb_info = &host->fb_info;
603 unsigned line_count;
604 unsigned period;
605 unsigned long pa, fbsize;
606 int bits_per_pixel, ofs;
607 u32 transfer_count, vdctrl0, vdctrl2, vdctrl3, vdctrl4, ctrl;
608 struct fb_videomode vmode;
609
610 /* Only restore the mode when the controller is running */
611 ctrl = readl(host->base + LCDC_CTRL);
612 if (!(ctrl & CTRL_RUN))
613 return -EINVAL;
614
615 vdctrl0 = readl(host->base + LCDC_VDCTRL0);
616 vdctrl2 = readl(host->base + LCDC_VDCTRL2);
617 vdctrl3 = readl(host->base + LCDC_VDCTRL3);
618 vdctrl4 = readl(host->base + LCDC_VDCTRL4);
619
620 transfer_count = readl(host->base + host->devdata->transfer_count);
621
622 vmode.xres = TRANSFER_COUNT_GET_HCOUNT(transfer_count);
623 vmode.yres = TRANSFER_COUNT_GET_VCOUNT(transfer_count);
624
625 switch (CTRL_GET_WORD_LENGTH(ctrl)) {
626 case 0:
627 bits_per_pixel = 16;
628 break;
629 case 3:
630 bits_per_pixel = 32;
631 case 1:
632 default:
633 return -EINVAL;
634 }
635
636 fb_info->var.bits_per_pixel = bits_per_pixel;
637
638 vmode.pixclock = KHZ2PICOS(clk_get_rate(host->clk) / 1000U);
639 vmode.hsync_len = get_hsync_pulse_width(host, vdctrl2);
640 vmode.left_margin = GET_HOR_WAIT_CNT(vdctrl3) - vmode.hsync_len;
641 vmode.right_margin = VDCTRL2_GET_HSYNC_PERIOD(vdctrl2) - vmode.hsync_len -
642 vmode.left_margin - vmode.xres;
643 vmode.vsync_len = VDCTRL0_GET_VSYNC_PULSE_WIDTH(vdctrl0);
644 period = readl(host->base + LCDC_VDCTRL1);
645 vmode.upper_margin = GET_VERT_WAIT_CNT(vdctrl3) - vmode.vsync_len;
646 vmode.lower_margin = period - vmode.vsync_len - vmode.upper_margin - vmode.yres;
647
648 vmode.vmode = FB_VMODE_NONINTERLACED;
649
650 vmode.sync = 0;
651 if (vdctrl0 & VDCTRL0_HSYNC_ACT_HIGH)
652 vmode.sync |= FB_SYNC_HOR_HIGH_ACT;
653 if (vdctrl0 & VDCTRL0_VSYNC_ACT_HIGH)
654 vmode.sync |= FB_SYNC_VERT_HIGH_ACT;
655
656 pr_debug("Reconstructed video mode:\n");
657 pr_debug("%dx%d, hsync: %u left: %u, right: %u, vsync: %u, upper: %u, lower: %u\n",
658 vmode.xres, vmode.yres,
659 vmode.hsync_len, vmode.left_margin, vmode.right_margin,
660 vmode.vsync_len, vmode.upper_margin, vmode.lower_margin);
661 pr_debug("pixclk: %ldkHz\n", PICOS2KHZ(vmode.pixclock));
662
663 fb_add_videomode(&vmode, &fb_info->modelist);
664
665 host->ld_intf_width = CTRL_GET_BUS_WIDTH(ctrl);
666 host->dotclk_delay = VDCTRL4_GET_DOTCLK_DLY(vdctrl4);
667
668 fb_info->fix.line_length = vmode.xres * (bits_per_pixel >> 3);
669
670 pa = readl(host->base + host->devdata->cur_buf);
671 fbsize = fb_info->fix.line_length * vmode.yres;
672 if (pa < fb_info->fix.smem_start)
673 return -EINVAL;
674 if (pa + fbsize > fb_info->fix.smem_start + fb_info->fix.smem_len)
675 return -EINVAL;
676 ofs = pa - fb_info->fix.smem_start;
677 if (ofs) {
678 memmove(fb_info->screen_base, fb_info->screen_base + ofs, fbsize);
679 writel(fb_info->fix.smem_start, host->base + host->devdata->next_buf);
680 }
681
682 line_count = fb_info->fix.smem_len / fb_info->fix.line_length;
683 fb_info->fix.ypanstep = 1;
684
Shawn Guoca4c22d32011-12-20 14:12:54 +0800685 clk_prepare_enable(host->clk);
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100686 host->enabled = 1;
687
688 return 0;
689}
690
Shawn Guo66940652013-03-14 10:57:34 +0800691static int mxsfb_init_fbinfo_dt(struct mxsfb_info *host)
692{
693 struct fb_info *fb_info = &host->fb_info;
694 struct fb_var_screeninfo *var = &fb_info->var;
695 struct device *dev = &host->pdev->dev;
696 struct device_node *np = host->pdev->dev.of_node;
697 struct device_node *display_np;
698 struct device_node *timings_np;
699 struct display_timings *timings;
700 u32 width;
701 int i;
702 int ret = 0;
703
704 display_np = of_parse_phandle(np, "display", 0);
705 if (!display_np) {
706 dev_err(dev, "failed to find display phandle\n");
707 return -ENOENT;
708 }
709
710 ret = of_property_read_u32(display_np, "bus-width", &width);
711 if (ret < 0) {
712 dev_err(dev, "failed to get property bus-width\n");
713 goto put_display_node;
714 }
715
716 switch (width) {
717 case 8:
718 host->ld_intf_width = STMLCDIF_8BIT;
719 break;
720 case 16:
721 host->ld_intf_width = STMLCDIF_16BIT;
722 break;
723 case 18:
724 host->ld_intf_width = STMLCDIF_18BIT;
725 break;
726 case 24:
727 host->ld_intf_width = STMLCDIF_24BIT;
728 break;
729 default:
730 dev_err(dev, "invalid bus-width value\n");
731 ret = -EINVAL;
732 goto put_display_node;
733 }
734
735 ret = of_property_read_u32(display_np, "bits-per-pixel",
736 &var->bits_per_pixel);
737 if (ret < 0) {
738 dev_err(dev, "failed to get property bits-per-pixel\n");
739 goto put_display_node;
740 }
741
742 timings = of_get_display_timings(display_np);
743 if (!timings) {
744 dev_err(dev, "failed to get display timings\n");
745 ret = -ENOENT;
746 goto put_display_node;
747 }
748
749 timings_np = of_find_node_by_name(display_np,
750 "display-timings");
751 if (!timings_np) {
752 dev_err(dev, "failed to find display-timings node\n");
753 ret = -ENOENT;
754 goto put_display_node;
755 }
756
757 for (i = 0; i < of_get_child_count(timings_np); i++) {
758 struct videomode vm;
759 struct fb_videomode fb_vm;
760
761 ret = videomode_from_timing(timings, &vm, i);
762 if (ret < 0)
763 goto put_timings_node;
764 ret = fb_videomode_from_videomode(&vm, &fb_vm);
765 if (ret < 0)
766 goto put_timings_node;
767
768 if (vm.data_flags & DISPLAY_FLAGS_DE_HIGH)
769 host->sync |= MXSFB_SYNC_DATA_ENABLE_HIGH_ACT;
770 if (vm.data_flags & DISPLAY_FLAGS_PIXDATA_NEGEDGE)
Shawn Guoc8b5cfc2013-03-14 13:21:56 +0800771 host->sync |= MXSFB_SYNC_DOTCLK_FALLING_ACT;
Shawn Guo66940652013-03-14 10:57:34 +0800772 fb_add_videomode(&fb_vm, &fb_info->modelist);
773 }
774
775put_timings_node:
776 of_node_put(timings_np);
777put_display_node:
778 of_node_put(display_np);
779 return ret;
780}
781
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -0800782static int mxsfb_init_fbinfo(struct mxsfb_info *host)
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100783{
784 struct fb_info *fb_info = &host->fb_info;
785 struct fb_var_screeninfo *var = &fb_info->var;
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100786 dma_addr_t fb_phys;
787 void *fb_virt;
Shawn Guo4aa02c72013-03-13 14:03:12 +0800788 unsigned fb_size;
Shawn Guo66940652013-03-14 10:57:34 +0800789 int ret;
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100790
791 fb_info->fbops = &mxsfb_ops;
792 fb_info->flags = FBINFO_FLAG_DEFAULT | FBINFO_READS_FAST;
793 strlcpy(fb_info->fix.id, "mxs", sizeof(fb_info->fix.id));
794 fb_info->fix.type = FB_TYPE_PACKED_PIXELS;
795 fb_info->fix.ypanstep = 1;
796 fb_info->fix.visual = FB_VISUAL_TRUECOLOR,
797 fb_info->fix.accel = FB_ACCEL_NONE;
798
Shawn Guoc8b5cfc2013-03-14 13:21:56 +0800799 ret = mxsfb_init_fbinfo_dt(host);
800 if (ret)
801 return ret;
Shawn Guo66940652013-03-14 10:57:34 +0800802
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100803 var->nonstd = 0;
804 var->activate = FB_ACTIVATE_NOW;
805 var->accel_flags = 0;
806 var->vmode = FB_VMODE_NONINTERLACED;
807
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100808 /* Memory allocation for framebuffer */
Shawn Guo4aa02c72013-03-13 14:03:12 +0800809 fb_size = SZ_2M;
810 fb_virt = alloc_pages_exact(fb_size, GFP_DMA);
811 if (!fb_virt)
812 return -ENOMEM;
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100813
Shawn Guo4aa02c72013-03-13 14:03:12 +0800814 fb_phys = virt_to_phys(fb_virt);
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100815
816 fb_info->fix.smem_start = fb_phys;
817 fb_info->screen_base = fb_virt;
818 fb_info->screen_size = fb_info->fix.smem_len = fb_size;
819
820 if (mxsfb_restore_mode(host))
821 memset(fb_virt, 0, fb_size);
822
823 return 0;
824}
825
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -0800826static void mxsfb_free_videomem(struct mxsfb_info *host)
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100827{
828 struct fb_info *fb_info = &host->fb_info;
829
Shawn Guo4aa02c72013-03-13 14:03:12 +0800830 free_pages_exact(fb_info->screen_base, fb_info->fix.smem_len);
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100831}
832
Shawn Guo73fc6102012-06-25 19:54:35 +0800833static struct platform_device_id mxsfb_devtype[] = {
834 {
835 .name = "imx23-fb",
836 .driver_data = MXSFB_V3,
837 }, {
838 .name = "imx28-fb",
839 .driver_data = MXSFB_V4,
840 }, {
841 /* sentinel */
842 }
843};
844MODULE_DEVICE_TABLE(platform, mxsfb_devtype);
845
846static const struct of_device_id mxsfb_dt_ids[] = {
847 { .compatible = "fsl,imx23-lcdif", .data = &mxsfb_devtype[0], },
848 { .compatible = "fsl,imx28-lcdif", .data = &mxsfb_devtype[1], },
849 { /* sentinel */ }
850};
851MODULE_DEVICE_TABLE(of, mxsfb_dt_ids);
852
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -0800853static int mxsfb_probe(struct platform_device *pdev)
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100854{
Shawn Guo73fc6102012-06-25 19:54:35 +0800855 const struct of_device_id *of_id =
856 of_match_device(mxsfb_dt_ids, &pdev->dev);
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100857 struct resource *res;
858 struct mxsfb_info *host;
859 struct fb_info *fb_info;
860 struct fb_modelist *modelist;
Shawn Guofe233b92012-05-06 23:01:41 +0800861 struct pinctrl *pinctrl;
Shawn Guo73fc6102012-06-25 19:54:35 +0800862 int panel_enable;
863 enum of_gpio_flags flags;
Shawn Guoc8b5cfc2013-03-14 13:21:56 +0800864 int ret;
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100865
Shawn Guo73fc6102012-06-25 19:54:35 +0800866 if (of_id)
867 pdev->id_entry = of_id->data;
868
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100869 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
870 if (!res) {
871 dev_err(&pdev->dev, "Cannot get memory IO resource\n");
872 return -ENODEV;
873 }
874
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100875 fb_info = framebuffer_alloc(sizeof(struct mxsfb_info), &pdev->dev);
876 if (!fb_info) {
877 dev_err(&pdev->dev, "Failed to allocate fbdev\n");
Shawn Guo9e548572013-03-13 13:37:11 +0800878 return -ENOMEM;
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100879 }
880
881 host = to_imxfb_host(fb_info);
882
Shawn Guo9e548572013-03-13 13:37:11 +0800883 host->base = devm_ioremap_resource(&pdev->dev, res);
884 if (IS_ERR(host->base)) {
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100885 dev_err(&pdev->dev, "ioremap failed\n");
Shawn Guo9e548572013-03-13 13:37:11 +0800886 ret = PTR_ERR(host->base);
887 goto fb_release;
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100888 }
889
890 host->pdev = pdev;
891 platform_set_drvdata(pdev, host);
892
893 host->devdata = &mxsfb_devdata[pdev->id_entry->driver_data];
894
Shawn Guofe233b92012-05-06 23:01:41 +0800895 pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
896 if (IS_ERR(pinctrl)) {
897 ret = PTR_ERR(pinctrl);
Shawn Guo9e548572013-03-13 13:37:11 +0800898 goto fb_release;
Shawn Guofe233b92012-05-06 23:01:41 +0800899 }
900
Shawn Guo9e548572013-03-13 13:37:11 +0800901 host->clk = devm_clk_get(&host->pdev->dev, NULL);
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100902 if (IS_ERR(host->clk)) {
903 ret = PTR_ERR(host->clk);
Shawn Guo9e548572013-03-13 13:37:11 +0800904 goto fb_release;
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100905 }
906
Shawn Guo73fc6102012-06-25 19:54:35 +0800907 panel_enable = of_get_named_gpio_flags(pdev->dev.of_node,
908 "panel-enable-gpios", 0, &flags);
909 if (gpio_is_valid(panel_enable)) {
910 unsigned long f = GPIOF_OUT_INIT_HIGH;
911 if (flags == OF_GPIO_ACTIVE_LOW)
912 f = GPIOF_OUT_INIT_LOW;
913 ret = devm_gpio_request_one(&pdev->dev, panel_enable,
914 f, "panel-enable");
915 if (ret) {
916 dev_err(&pdev->dev,
917 "failed to request gpio %d: %d\n",
918 panel_enable, ret);
Shawn Guo9e548572013-03-13 13:37:11 +0800919 goto fb_release;
Shawn Guo73fc6102012-06-25 19:54:35 +0800920 }
921 }
922
Shawn Guo9e548572013-03-13 13:37:11 +0800923 fb_info->pseudo_palette = devm_kzalloc(&pdev->dev, sizeof(u32) * 16,
924 GFP_KERNEL);
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100925 if (!fb_info->pseudo_palette) {
926 ret = -ENOMEM;
Shawn Guo9e548572013-03-13 13:37:11 +0800927 goto fb_release;
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100928 }
929
930 INIT_LIST_HEAD(&fb_info->modelist);
931
932 ret = mxsfb_init_fbinfo(host);
933 if (ret != 0)
Shawn Guo9e548572013-03-13 13:37:11 +0800934 goto fb_release;
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100935
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100936 modelist = list_first_entry(&fb_info->modelist,
937 struct fb_modelist, list);
938 fb_videomode_to_var(&fb_info->var, &modelist->mode);
939
940 /* init the color fields */
941 mxsfb_check_var(&fb_info->var, fb_info);
942
943 platform_set_drvdata(pdev, fb_info);
944
945 ret = register_framebuffer(fb_info);
946 if (ret != 0) {
947 dev_err(&pdev->dev,"Failed to register framebuffer\n");
Shawn Guo9e548572013-03-13 13:37:11 +0800948 goto fb_destroy;
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100949 }
950
951 if (!host->enabled) {
952 writel(0, host->base + LCDC_CTRL);
953 mxsfb_set_par(fb_info);
954 mxsfb_enable_controller(fb_info);
955 }
956
957 dev_info(&pdev->dev, "initialized\n");
958
959 return 0;
960
Shawn Guo9e548572013-03-13 13:37:11 +0800961fb_destroy:
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100962 if (host->enabled)
Shawn Guoca4c22d32011-12-20 14:12:54 +0800963 clk_disable_unprepare(host->clk);
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100964 fb_destroy_modelist(&fb_info->modelist);
Shawn Guo9e548572013-03-13 13:37:11 +0800965fb_release:
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100966 framebuffer_release(fb_info);
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100967
968 return ret;
969}
970
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -0800971static int mxsfb_remove(struct platform_device *pdev)
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100972{
973 struct fb_info *fb_info = platform_get_drvdata(pdev);
974 struct mxsfb_info *host = to_imxfb_host(fb_info);
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100975
976 if (host->enabled)
977 mxsfb_disable_controller(fb_info);
978
979 unregister_framebuffer(fb_info);
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100980 mxsfb_free_videomem(host);
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100981
982 framebuffer_release(fb_info);
Sascha Hauerf0a523b2011-01-14 15:22:31 +0100983
984 platform_set_drvdata(pdev, NULL);
985
986 return 0;
987}
988
Marek Vasutd313a862012-04-19 20:31:02 +0200989static void mxsfb_shutdown(struct platform_device *pdev)
990{
991 struct fb_info *fb_info = platform_get_drvdata(pdev);
992 struct mxsfb_info *host = to_imxfb_host(fb_info);
993
994 /*
995 * Force stop the LCD controller as keeping it running during reboot
996 * might interfere with the BootROM's boot mode pads sampling.
997 */
998 writel(CTRL_RUN, host->base + LCDC_CTRL + REG_CLR);
999}
1000
Sascha Hauerf0a523b2011-01-14 15:22:31 +01001001static struct platform_driver mxsfb_driver = {
1002 .probe = mxsfb_probe,
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -08001003 .remove = mxsfb_remove,
Marek Vasutd313a862012-04-19 20:31:02 +02001004 .shutdown = mxsfb_shutdown,
Sascha Hauerf0a523b2011-01-14 15:22:31 +01001005 .id_table = mxsfb_devtype,
1006 .driver = {
1007 .name = DRIVER_NAME,
Shawn Guo73fc6102012-06-25 19:54:35 +08001008 .of_match_table = mxsfb_dt_ids,
Sascha Hauerf0a523b2011-01-14 15:22:31 +01001009 },
1010};
1011
Marek Vasut396fa992011-12-19 16:37:59 +01001012module_platform_driver(mxsfb_driver);
Sascha Hauerf0a523b2011-01-14 15:22:31 +01001013
1014MODULE_DESCRIPTION("Freescale mxs framebuffer driver");
1015MODULE_AUTHOR("Sascha Hauer, Pengutronix");
1016MODULE_LICENSE("GPL");