blob: fe0025213a82f925f04d4dc9e99ce046e5d40a2b [file] [log] [blame]
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001/*
2 * Copyright (C) 2008-2009 MontaVista Software Inc.
3 * Copyright (C) 2008-2009 Texas Instruments Inc
4 *
5 * Based on the LCD driver for TI Avalanche processors written by
6 * Ajay Singh and Shalom Hai.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option)any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22#include <linux/module.h>
23#include <linux/kernel.h>
24#include <linux/fb.h>
25#include <linux/dma-mapping.h>
26#include <linux/device.h>
27#include <linux/platform_device.h>
28#include <linux/uaccess.h>
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -070029#include <linux/interrupt.h>
30#include <linux/clk.h>
31#include <video/da8xx-fb.h>
32
33#define DRIVER_NAME "da8xx_lcdc"
34
35/* LCD Status Register */
36#define LCD_END_OF_FRAME0 BIT(8)
37#define LCD_FIFO_UNDERFLOW BIT(5)
38#define LCD_SYNC_LOST BIT(2)
39
40/* LCD DMA Control Register */
41#define LCD_DMA_BURST_SIZE(x) ((x) << 4)
42#define LCD_DMA_BURST_1 0x0
43#define LCD_DMA_BURST_2 0x1
44#define LCD_DMA_BURST_4 0x2
45#define LCD_DMA_BURST_8 0x3
46#define LCD_DMA_BURST_16 0x4
47#define LCD_END_OF_FRAME_INT_ENA BIT(2)
48#define LCD_DUAL_FRAME_BUFFER_ENABLE BIT(0)
49
50/* LCD Control Register */
51#define LCD_CLK_DIVISOR(x) ((x) << 8)
52#define LCD_RASTER_MODE 0x01
53
54/* LCD Raster Control Register */
55#define LCD_PALETTE_LOAD_MODE(x) ((x) << 20)
56#define PALETTE_AND_DATA 0x00
57#define PALETTE_ONLY 0x01
58
59#define LCD_MONO_8BIT_MODE BIT(9)
60#define LCD_RASTER_ORDER BIT(8)
61#define LCD_TFT_MODE BIT(7)
62#define LCD_UNDERFLOW_INT_ENA BIT(6)
63#define LCD_MONOCHROME_MODE BIT(1)
64#define LCD_RASTER_ENABLE BIT(0)
65#define LCD_TFT_ALT_ENABLE BIT(23)
66#define LCD_STN_565_ENABLE BIT(24)
67
68/* LCD Raster Timing 2 Register */
69#define LCD_AC_BIAS_TRANSITIONS_PER_INT(x) ((x) << 16)
70#define LCD_AC_BIAS_FREQUENCY(x) ((x) << 8)
71#define LCD_SYNC_CTRL BIT(25)
72#define LCD_SYNC_EDGE BIT(24)
73#define LCD_INVERT_PIXEL_CLOCK BIT(22)
74#define LCD_INVERT_LINE_CLOCK BIT(21)
75#define LCD_INVERT_FRAME_CLOCK BIT(20)
76
77/* LCD Block */
78#define LCD_CTRL_REG 0x4
79#define LCD_STAT_REG 0x8
80#define LCD_RASTER_CTRL_REG 0x28
81#define LCD_RASTER_TIMING_0_REG 0x2C
82#define LCD_RASTER_TIMING_1_REG 0x30
83#define LCD_RASTER_TIMING_2_REG 0x34
84#define LCD_DMA_CTRL_REG 0x40
85#define LCD_DMA_FRM_BUF_BASE_ADDR_0_REG 0x44
86#define LCD_DMA_FRM_BUF_CEILING_ADDR_0_REG 0x48
87
88#define WSI_TIMEOUT 50
89#define PALETTE_SIZE 256
90#define LEFT_MARGIN 64
91#define RIGHT_MARGIN 64
92#define UPPER_MARGIN 32
93#define LOWER_MARGIN 32
94
95static resource_size_t da8xx_fb_reg_base;
96static struct resource *lcdc_regs;
97
98static inline unsigned int lcdc_read(unsigned int addr)
99{
100 return (unsigned int)__raw_readl(da8xx_fb_reg_base + (addr));
101}
102
103static inline void lcdc_write(unsigned int val, unsigned int addr)
104{
105 __raw_writel(val, da8xx_fb_reg_base + (addr));
106}
107
108struct da8xx_fb_par {
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700109 resource_size_t p_palette_base;
110 unsigned char *v_palette_base;
111 struct clk *lcdc_clk;
112 int irq;
113 unsigned short pseudo_palette[16];
114 unsigned int databuf_sz;
115 unsigned int palette_sz;
Chaithrika U S8097b172009-12-15 16:46:29 -0800116 unsigned int pxl_clk;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700117};
118
119/* Variable Screen Information */
120static struct fb_var_screeninfo da8xx_fb_var __devinitdata = {
121 .xoffset = 0,
122 .yoffset = 0,
123 .transp = {0, 0, 0},
124 .nonstd = 0,
125 .activate = 0,
126 .height = -1,
127 .width = -1,
128 .pixclock = 46666, /* 46us - AUO display */
129 .accel_flags = 0,
130 .left_margin = LEFT_MARGIN,
131 .right_margin = RIGHT_MARGIN,
132 .upper_margin = UPPER_MARGIN,
133 .lower_margin = LOWER_MARGIN,
134 .sync = 0,
135 .vmode = FB_VMODE_NONINTERLACED
136};
137
138static struct fb_fix_screeninfo da8xx_fb_fix __devinitdata = {
139 .id = "DA8xx FB Drv",
140 .type = FB_TYPE_PACKED_PIXELS,
141 .type_aux = 0,
142 .visual = FB_VISUAL_PSEUDOCOLOR,
143 .xpanstep = 1,
144 .ypanstep = 1,
145 .ywrapstep = 1,
146 .accel = FB_ACCEL_NONE
147};
148
149struct da8xx_panel {
150 const char name[25]; /* Full name <vendor>_<model> */
151 unsigned short width;
152 unsigned short height;
153 int hfp; /* Horizontal front porch */
154 int hbp; /* Horizontal back porch */
155 int hsw; /* Horizontal Sync Pulse Width */
156 int vfp; /* Vertical front porch */
157 int vbp; /* Vertical back porch */
158 int vsw; /* Vertical Sync Pulse Width */
Chaithrika U S8097b172009-12-15 16:46:29 -0800159 unsigned int pxl_clk; /* Pixel clock */
Sudhakar Rajashekhara2f93e8f2009-09-22 16:47:06 -0700160 unsigned char invert_pxl_clk; /* Invert Pixel clock */
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700161};
162
163static struct da8xx_panel known_lcd_panels[] = {
164 /* Sharp LCD035Q3DG01 */
165 [0] = {
166 .name = "Sharp_LCD035Q3DG01",
167 .width = 320,
168 .height = 240,
169 .hfp = 8,
170 .hbp = 6,
171 .hsw = 0,
172 .vfp = 2,
173 .vbp = 2,
174 .vsw = 0,
Chaithrika U S8097b172009-12-15 16:46:29 -0800175 .pxl_clk = 4608000,
Sudhakar Rajashekhara2f93e8f2009-09-22 16:47:06 -0700176 .invert_pxl_clk = 1,
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700177 },
178 /* Sharp LK043T1DG01 */
179 [1] = {
180 .name = "Sharp_LK043T1DG01",
181 .width = 480,
182 .height = 272,
183 .hfp = 2,
184 .hbp = 2,
185 .hsw = 41,
186 .vfp = 2,
187 .vbp = 2,
188 .vsw = 10,
Chaithrika U S8097b172009-12-15 16:46:29 -0800189 .pxl_clk = 7833600,
Sudhakar Rajashekhara2f93e8f2009-09-22 16:47:06 -0700190 .invert_pxl_clk = 0,
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700191 },
192};
193
194/* Disable the Raster Engine of the LCD Controller */
Sudhakar Rajashekhara2f93e8f2009-09-22 16:47:06 -0700195static void lcd_disable_raster(struct da8xx_fb_par *par)
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700196{
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700197 u32 reg;
198
199 reg = lcdc_read(LCD_RASTER_CTRL_REG);
Sudhakar Rajashekhara2f93e8f2009-09-22 16:47:06 -0700200 if (reg & LCD_RASTER_ENABLE)
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700201 lcdc_write(reg & ~LCD_RASTER_ENABLE, LCD_RASTER_CTRL_REG);
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700202}
203
204static void lcd_blit(int load_mode, struct da8xx_fb_par *par)
205{
206 u32 tmp = par->p_palette_base + par->databuf_sz - 4;
207 u32 reg;
208
209 /* Update the databuf in the hw. */
210 lcdc_write(par->p_palette_base, LCD_DMA_FRM_BUF_BASE_ADDR_0_REG);
211 lcdc_write(tmp, LCD_DMA_FRM_BUF_CEILING_ADDR_0_REG);
212
213 /* Start the DMA. */
214 reg = lcdc_read(LCD_RASTER_CTRL_REG);
215 reg &= ~(3 << 20);
216 if (load_mode == LOAD_DATA)
217 reg |= LCD_PALETTE_LOAD_MODE(PALETTE_AND_DATA);
218 else if (load_mode == LOAD_PALETTE)
219 reg |= LCD_PALETTE_LOAD_MODE(PALETTE_ONLY);
220
221 lcdc_write(reg, LCD_RASTER_CTRL_REG);
222}
223
224/* Configure the Burst Size of DMA */
225static int lcd_cfg_dma(int burst_size)
226{
227 u32 reg;
228
229 reg = lcdc_read(LCD_DMA_CTRL_REG) & 0x00000001;
230 switch (burst_size) {
231 case 1:
232 reg |= LCD_DMA_BURST_SIZE(LCD_DMA_BURST_1);
233 break;
234 case 2:
235 reg |= LCD_DMA_BURST_SIZE(LCD_DMA_BURST_2);
236 break;
237 case 4:
238 reg |= LCD_DMA_BURST_SIZE(LCD_DMA_BURST_4);
239 break;
240 case 8:
241 reg |= LCD_DMA_BURST_SIZE(LCD_DMA_BURST_8);
242 break;
243 case 16:
244 reg |= LCD_DMA_BURST_SIZE(LCD_DMA_BURST_16);
245 break;
246 default:
247 return -EINVAL;
248 }
Sudhakar Rajashekhara2f93e8f2009-09-22 16:47:06 -0700249 lcdc_write(reg, LCD_DMA_CTRL_REG);
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700250
251 return 0;
252}
253
254static void lcd_cfg_ac_bias(int period, int transitions_per_int)
255{
256 u32 reg;
257
258 /* Set the AC Bias Period and Number of Transisitons per Interrupt */
259 reg = lcdc_read(LCD_RASTER_TIMING_2_REG) & 0xFFF00000;
260 reg |= LCD_AC_BIAS_FREQUENCY(period) |
261 LCD_AC_BIAS_TRANSITIONS_PER_INT(transitions_per_int);
262 lcdc_write(reg, LCD_RASTER_TIMING_2_REG);
263}
264
265static void lcd_cfg_horizontal_sync(int back_porch, int pulse_width,
266 int front_porch)
267{
268 u32 reg;
269
270 reg = lcdc_read(LCD_RASTER_TIMING_0_REG) & 0xf;
271 reg |= ((back_porch & 0xff) << 24)
272 | ((front_porch & 0xff) << 16)
273 | ((pulse_width & 0x3f) << 10);
274 lcdc_write(reg, LCD_RASTER_TIMING_0_REG);
275}
276
277static void lcd_cfg_vertical_sync(int back_porch, int pulse_width,
278 int front_porch)
279{
280 u32 reg;
281
282 reg = lcdc_read(LCD_RASTER_TIMING_1_REG) & 0x3ff;
283 reg |= ((back_porch & 0xff) << 24)
284 | ((front_porch & 0xff) << 16)
285 | ((pulse_width & 0x3f) << 10);
286 lcdc_write(reg, LCD_RASTER_TIMING_1_REG);
287}
288
289static int lcd_cfg_display(const struct lcd_ctrl_config *cfg)
290{
291 u32 reg;
292
293 reg = lcdc_read(LCD_RASTER_CTRL_REG) & ~(LCD_TFT_MODE |
294 LCD_MONO_8BIT_MODE |
295 LCD_MONOCHROME_MODE);
296
297 switch (cfg->p_disp_panel->panel_shade) {
298 case MONOCHROME:
299 reg |= LCD_MONOCHROME_MODE;
300 if (cfg->mono_8bit_mode)
301 reg |= LCD_MONO_8BIT_MODE;
302 break;
303 case COLOR_ACTIVE:
304 reg |= LCD_TFT_MODE;
305 if (cfg->tft_alt_mode)
306 reg |= LCD_TFT_ALT_ENABLE;
307 break;
308
309 case COLOR_PASSIVE:
310 if (cfg->stn_565_mode)
311 reg |= LCD_STN_565_ENABLE;
312 break;
313
314 default:
315 return -EINVAL;
316 }
317
318 /* enable additional interrupts here */
319 reg |= LCD_UNDERFLOW_INT_ENA;
320
321 lcdc_write(reg, LCD_RASTER_CTRL_REG);
322
323 reg = lcdc_read(LCD_RASTER_TIMING_2_REG);
324
325 if (cfg->sync_ctrl)
326 reg |= LCD_SYNC_CTRL;
327 else
328 reg &= ~LCD_SYNC_CTRL;
329
330 if (cfg->sync_edge)
331 reg |= LCD_SYNC_EDGE;
332 else
333 reg &= ~LCD_SYNC_EDGE;
334
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700335 if (cfg->invert_line_clock)
336 reg |= LCD_INVERT_LINE_CLOCK;
337 else
338 reg &= ~LCD_INVERT_LINE_CLOCK;
339
340 if (cfg->invert_frm_clock)
341 reg |= LCD_INVERT_FRAME_CLOCK;
342 else
343 reg &= ~LCD_INVERT_FRAME_CLOCK;
344
345 lcdc_write(reg, LCD_RASTER_TIMING_2_REG);
346
347 return 0;
348}
349
350static int lcd_cfg_frame_buffer(struct da8xx_fb_par *par, u32 width, u32 height,
351 u32 bpp, u32 raster_order)
352{
353 u32 bpl, reg;
354
355 /* Disable Dual Frame Buffer. */
356 reg = lcdc_read(LCD_DMA_CTRL_REG);
357 lcdc_write(reg & ~LCD_DUAL_FRAME_BUFFER_ENABLE,
358 LCD_DMA_CTRL_REG);
359 /* Set the Panel Width */
360 /* Pixels per line = (PPL + 1)*16 */
361 /*0x3F in bits 4..9 gives max horisontal resolution = 1024 pixels*/
362 width &= 0x3f0;
363 reg = lcdc_read(LCD_RASTER_TIMING_0_REG);
364 reg &= 0xfffffc00;
365 reg |= ((width >> 4) - 1) << 4;
366 lcdc_write(reg, LCD_RASTER_TIMING_0_REG);
367
368 /* Set the Panel Height */
369 reg = lcdc_read(LCD_RASTER_TIMING_1_REG);
370 reg = ((height - 1) & 0x3ff) | (reg & 0xfffffc00);
371 lcdc_write(reg, LCD_RASTER_TIMING_1_REG);
372
373 /* Set the Raster Order of the Frame Buffer */
374 reg = lcdc_read(LCD_RASTER_CTRL_REG) & ~(1 << 8);
375 if (raster_order)
376 reg |= LCD_RASTER_ORDER;
377 lcdc_write(reg, LCD_RASTER_CTRL_REG);
378
379 switch (bpp) {
380 case 1:
381 case 2:
382 case 4:
383 case 16:
384 par->palette_sz = 16 * 2;
385 break;
386
387 case 8:
388 par->palette_sz = 256 * 2;
389 break;
390
391 default:
392 return -EINVAL;
393 }
394
395 bpl = width * bpp / 8;
396 par->databuf_sz = height * bpl + par->palette_sz;
397
398 return 0;
399}
400
401static int fb_setcolreg(unsigned regno, unsigned red, unsigned green,
402 unsigned blue, unsigned transp,
403 struct fb_info *info)
404{
405 struct da8xx_fb_par *par = info->par;
406 unsigned short *palette = (unsigned short *)par->v_palette_base;
407 u_short pal;
408
409 if (regno > 255)
410 return 1;
411
412 if (info->fix.visual == FB_VISUAL_DIRECTCOLOR)
413 return 1;
414
415 if (info->var.bits_per_pixel == 8) {
416 red >>= 4;
417 green >>= 8;
418 blue >>= 12;
419
420 pal = (red & 0x0f00);
421 pal |= (green & 0x00f0);
422 pal |= (blue & 0x000f);
423
424 palette[regno] = pal;
425
426 } else if ((info->var.bits_per_pixel == 16) && regno < 16) {
427 red >>= (16 - info->var.red.length);
428 red <<= info->var.red.offset;
429
430 green >>= (16 - info->var.green.length);
431 green <<= info->var.green.offset;
432
433 blue >>= (16 - info->var.blue.length);
434 blue <<= info->var.blue.offset;
435
436 par->pseudo_palette[regno] = red | green | blue;
437
438 palette[0] = 0x4000;
439 }
440
441 return 0;
442}
443
Sudhakar Rajashekhara2f93e8f2009-09-22 16:47:06 -0700444static void lcd_reset(struct da8xx_fb_par *par)
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700445{
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700446 /* Disable the Raster if previously Enabled */
447 if (lcdc_read(LCD_RASTER_CTRL_REG) & LCD_RASTER_ENABLE)
Sudhakar Rajashekhara2f93e8f2009-09-22 16:47:06 -0700448 lcd_disable_raster(par);
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700449
450 /* DMA has to be disabled */
451 lcdc_write(0, LCD_DMA_CTRL_REG);
452 lcdc_write(0, LCD_RASTER_CTRL_REG);
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700453}
454
Chaithrika U S8097b172009-12-15 16:46:29 -0800455static void lcd_calc_clk_divider(struct da8xx_fb_par *par)
456{
457 unsigned int lcd_clk, div;
458
459 lcd_clk = clk_get_rate(par->lcdc_clk);
460 div = lcd_clk / par->pxl_clk;
461
462 /* Configure the LCD clock divisor. */
463 lcdc_write(LCD_CLK_DIVISOR(div) |
464 (LCD_RASTER_MODE & 0x1), LCD_CTRL_REG);
465}
466
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700467static int lcd_init(struct da8xx_fb_par *par, const struct lcd_ctrl_config *cfg,
468 struct da8xx_panel *panel)
469{
470 u32 bpp;
471 int ret = 0;
472
Sudhakar Rajashekhara2f93e8f2009-09-22 16:47:06 -0700473 lcd_reset(par);
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700474
Chaithrika U S8097b172009-12-15 16:46:29 -0800475 /* Calculate the divider */
476 lcd_calc_clk_divider(par);
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700477
Sudhakar Rajashekhara2f93e8f2009-09-22 16:47:06 -0700478 if (panel->invert_pxl_clk)
479 lcdc_write((lcdc_read(LCD_RASTER_TIMING_2_REG) |
480 LCD_INVERT_PIXEL_CLOCK), LCD_RASTER_TIMING_2_REG);
481 else
482 lcdc_write((lcdc_read(LCD_RASTER_TIMING_2_REG) &
483 ~LCD_INVERT_PIXEL_CLOCK), LCD_RASTER_TIMING_2_REG);
484
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700485 /* Configure the DMA burst size. */
486 ret = lcd_cfg_dma(cfg->dma_burst_sz);
487 if (ret < 0)
488 return ret;
489
490 /* Configure the AC bias properties. */
491 lcd_cfg_ac_bias(cfg->ac_bias, cfg->ac_bias_intrpt);
492
493 /* Configure the vertical and horizontal sync properties. */
494 lcd_cfg_vertical_sync(panel->vbp, panel->vsw, panel->vfp);
495 lcd_cfg_horizontal_sync(panel->hbp, panel->hsw, panel->hfp);
496
497 /* Configure for disply */
498 ret = lcd_cfg_display(cfg);
499 if (ret < 0)
500 return ret;
501
502 if (QVGA != cfg->p_disp_panel->panel_type)
503 return -EINVAL;
504
505 if (cfg->bpp <= cfg->p_disp_panel->max_bpp &&
506 cfg->bpp >= cfg->p_disp_panel->min_bpp)
507 bpp = cfg->bpp;
508 else
509 bpp = cfg->p_disp_panel->max_bpp;
510 if (bpp == 12)
511 bpp = 16;
512 ret = lcd_cfg_frame_buffer(par, (unsigned int)panel->width,
513 (unsigned int)panel->height, bpp,
514 cfg->raster_order);
515 if (ret < 0)
516 return ret;
517
518 /* Configure FDD */
519 lcdc_write((lcdc_read(LCD_RASTER_CTRL_REG) & 0xfff00fff) |
520 (cfg->fdd << 12), LCD_RASTER_CTRL_REG);
521
522 return 0;
523}
524
525static irqreturn_t lcdc_irq_handler(int irq, void *arg)
526{
527 u32 stat = lcdc_read(LCD_STAT_REG);
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700528 u32 reg;
529
530 if ((stat & LCD_SYNC_LOST) && (stat & LCD_FIFO_UNDERFLOW)) {
531 reg = lcdc_read(LCD_RASTER_CTRL_REG);
532 lcdc_write(reg & ~LCD_RASTER_ENABLE, LCD_RASTER_CTRL_REG);
533 lcdc_write(stat, LCD_STAT_REG);
534 lcdc_write(reg | LCD_RASTER_ENABLE, LCD_RASTER_CTRL_REG);
535 } else
536 lcdc_write(stat, LCD_STAT_REG);
537
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700538 return IRQ_HANDLED;
539}
540
541static int fb_check_var(struct fb_var_screeninfo *var,
542 struct fb_info *info)
543{
544 int err = 0;
545
546 switch (var->bits_per_pixel) {
547 case 1:
548 case 8:
549 var->red.offset = 0;
550 var->red.length = 8;
551 var->green.offset = 0;
552 var->green.length = 8;
553 var->blue.offset = 0;
554 var->blue.length = 8;
555 var->transp.offset = 0;
556 var->transp.length = 0;
557 break;
558 case 4:
559 var->red.offset = 0;
560 var->red.length = 4;
561 var->green.offset = 0;
562 var->green.length = 4;
563 var->blue.offset = 0;
564 var->blue.length = 4;
565 var->transp.offset = 0;
566 var->transp.length = 0;
567 break;
568 case 16: /* RGB 565 */
Sudhakar Rajashekhara3510b8f2009-12-01 13:17:43 -0800569 var->red.offset = 11;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700570 var->red.length = 5;
571 var->green.offset = 5;
572 var->green.length = 6;
Sudhakar Rajashekhara3510b8f2009-12-01 13:17:43 -0800573 var->blue.offset = 0;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700574 var->blue.length = 5;
575 var->transp.offset = 0;
576 var->transp.length = 0;
577 break;
578 default:
579 err = -EINVAL;
580 }
581
582 var->red.msb_right = 0;
583 var->green.msb_right = 0;
584 var->blue.msb_right = 0;
585 var->transp.msb_right = 0;
586 return err;
587}
588
589static int __devexit fb_remove(struct platform_device *dev)
590{
591 struct fb_info *info = dev_get_drvdata(&dev->dev);
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700592
593 if (info) {
594 struct da8xx_fb_par *par = info->par;
595
596 if (lcdc_read(LCD_RASTER_CTRL_REG) & LCD_RASTER_ENABLE)
Sudhakar Rajashekhara2f93e8f2009-09-22 16:47:06 -0700597 lcd_disable_raster(par);
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700598 lcdc_write(0, LCD_RASTER_CTRL_REG);
599
600 /* disable DMA */
601 lcdc_write(0, LCD_DMA_CTRL_REG);
602
603 unregister_framebuffer(info);
604 fb_dealloc_cmap(&info->cmap);
605 dma_free_coherent(NULL, par->databuf_sz + PAGE_SIZE,
Sudhakar Rajashekhara3510b8f2009-12-01 13:17:43 -0800606 info->screen_base - PAGE_SIZE,
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700607 info->fix.smem_start);
608 free_irq(par->irq, par);
609 clk_disable(par->lcdc_clk);
610 clk_put(par->lcdc_clk);
611 framebuffer_release(info);
612 iounmap((void __iomem *)da8xx_fb_reg_base);
613 release_mem_region(lcdc_regs->start, resource_size(lcdc_regs));
614
615 }
Sudhakar Rajashekhara2f93e8f2009-09-22 16:47:06 -0700616 return 0;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700617}
618
619static int fb_ioctl(struct fb_info *info, unsigned int cmd,
620 unsigned long arg)
621{
622 struct lcd_sync_arg sync_arg;
623
624 switch (cmd) {
625 case FBIOGET_CONTRAST:
626 case FBIOPUT_CONTRAST:
627 case FBIGET_BRIGHTNESS:
628 case FBIPUT_BRIGHTNESS:
629 case FBIGET_COLOR:
630 case FBIPUT_COLOR:
Sudhakar Rajashekhara2f93e8f2009-09-22 16:47:06 -0700631 return -ENOTTY;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700632 case FBIPUT_HSYNC:
633 if (copy_from_user(&sync_arg, (char *)arg,
634 sizeof(struct lcd_sync_arg)))
Sudhakar Rajashekhara2f93e8f2009-09-22 16:47:06 -0700635 return -EFAULT;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700636 lcd_cfg_horizontal_sync(sync_arg.back_porch,
637 sync_arg.pulse_width,
638 sync_arg.front_porch);
639 break;
640 case FBIPUT_VSYNC:
641 if (copy_from_user(&sync_arg, (char *)arg,
642 sizeof(struct lcd_sync_arg)))
Sudhakar Rajashekhara2f93e8f2009-09-22 16:47:06 -0700643 return -EFAULT;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700644 lcd_cfg_vertical_sync(sync_arg.back_porch,
645 sync_arg.pulse_width,
646 sync_arg.front_porch);
647 break;
648 default:
649 return -EINVAL;
650 }
651 return 0;
652}
653
654static struct fb_ops da8xx_fb_ops = {
655 .owner = THIS_MODULE,
656 .fb_check_var = fb_check_var,
657 .fb_setcolreg = fb_setcolreg,
658 .fb_ioctl = fb_ioctl,
659 .fb_fillrect = cfb_fillrect,
660 .fb_copyarea = cfb_copyarea,
661 .fb_imageblit = cfb_imageblit,
662};
663
664static int __init fb_probe(struct platform_device *device)
665{
666 struct da8xx_lcdc_platform_data *fb_pdata =
667 device->dev.platform_data;
668 struct lcd_ctrl_config *lcd_cfg;
669 struct da8xx_panel *lcdc_info;
670 struct fb_info *da8xx_fb_info;
671 struct clk *fb_clk = NULL;
672 struct da8xx_fb_par *par;
673 resource_size_t len;
674 int ret, i;
675
676 if (fb_pdata == NULL) {
677 dev_err(&device->dev, "Can not get platform data\n");
678 return -ENOENT;
679 }
680
681 lcdc_regs = platform_get_resource(device, IORESOURCE_MEM, 0);
682 if (!lcdc_regs) {
683 dev_err(&device->dev,
684 "Can not get memory resource for LCD controller\n");
685 return -ENOENT;
686 }
687
688 len = resource_size(lcdc_regs);
689
690 lcdc_regs = request_mem_region(lcdc_regs->start, len, lcdc_regs->name);
691 if (!lcdc_regs)
692 return -EBUSY;
693
694 da8xx_fb_reg_base = (resource_size_t)ioremap(lcdc_regs->start, len);
695 if (!da8xx_fb_reg_base) {
696 ret = -EBUSY;
697 goto err_request_mem;
698 }
699
700 fb_clk = clk_get(&device->dev, NULL);
701 if (IS_ERR(fb_clk)) {
702 dev_err(&device->dev, "Can not get device clock\n");
703 ret = -ENODEV;
704 goto err_ioremap;
705 }
706 ret = clk_enable(fb_clk);
707 if (ret)
708 goto err_clk_put;
709
710 for (i = 0, lcdc_info = known_lcd_panels;
711 i < ARRAY_SIZE(known_lcd_panels);
712 i++, lcdc_info++) {
713 if (strcmp(fb_pdata->type, lcdc_info->name) == 0)
714 break;
715 }
716
717 if (i == ARRAY_SIZE(known_lcd_panels)) {
718 dev_err(&device->dev, "GLCD: No valid panel found\n");
Roel Kluindd04a6b2009-11-17 14:06:15 -0800719 ret = -ENODEV;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700720 goto err_clk_disable;
721 } else
722 dev_info(&device->dev, "GLCD: Found %s panel\n",
723 fb_pdata->type);
724
725 lcd_cfg = (struct lcd_ctrl_config *)fb_pdata->controller_data;
726
727 da8xx_fb_info = framebuffer_alloc(sizeof(struct da8xx_fb_par),
728 &device->dev);
729 if (!da8xx_fb_info) {
730 dev_dbg(&device->dev, "Memory allocation failed for fb_info\n");
731 ret = -ENOMEM;
732 goto err_clk_disable;
733 }
734
735 par = da8xx_fb_info->par;
Chaithrika U S8097b172009-12-15 16:46:29 -0800736 par->lcdc_clk = fb_clk;
737 par->pxl_clk = lcdc_info->pxl_clk;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700738
739 if (lcd_init(par, lcd_cfg, lcdc_info) < 0) {
740 dev_err(&device->dev, "lcd_init failed\n");
741 ret = -EFAULT;
742 goto err_release_fb;
743 }
744
745 /* allocate frame buffer */
746 da8xx_fb_info->screen_base = dma_alloc_coherent(NULL,
747 par->databuf_sz + PAGE_SIZE,
748 (resource_size_t *)
749 &da8xx_fb_info->fix.smem_start,
750 GFP_KERNEL | GFP_DMA);
751
752 if (!da8xx_fb_info->screen_base) {
753 dev_err(&device->dev,
754 "GLCD: kmalloc for frame buffer failed\n");
755 ret = -EINVAL;
756 goto err_release_fb;
757 }
758
759 /* move palette base pointer by (PAGE_SIZE - palette_sz) bytes */
760 par->v_palette_base = da8xx_fb_info->screen_base +
761 (PAGE_SIZE - par->palette_sz);
762 par->p_palette_base = da8xx_fb_info->fix.smem_start +
763 (PAGE_SIZE - par->palette_sz);
764
765 /* the rest of the frame buffer is pixel data */
Sudhakar Rajashekhara3510b8f2009-12-01 13:17:43 -0800766 da8xx_fb_info->screen_base = par->v_palette_base + par->palette_sz;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700767 da8xx_fb_fix.smem_start = par->p_palette_base + par->palette_sz;
768 da8xx_fb_fix.smem_len = par->databuf_sz - par->palette_sz;
769 da8xx_fb_fix.line_length = (lcdc_info->width * lcd_cfg->bpp) / 8;
770
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700771 par->irq = platform_get_irq(device, 0);
772 if (par->irq < 0) {
773 ret = -ENOENT;
774 goto err_release_fb_mem;
775 }
776
777 ret = request_irq(par->irq, lcdc_irq_handler, 0, DRIVER_NAME, par);
778 if (ret)
779 goto err_release_fb_mem;
780
781 /* Initialize par */
782 da8xx_fb_info->var.bits_per_pixel = lcd_cfg->bpp;
783
784 da8xx_fb_var.xres = lcdc_info->width;
785 da8xx_fb_var.xres_virtual = lcdc_info->width;
786
787 da8xx_fb_var.yres = lcdc_info->height;
788 da8xx_fb_var.yres_virtual = lcdc_info->height;
789
790 da8xx_fb_var.grayscale =
791 lcd_cfg->p_disp_panel->panel_shade == MONOCHROME ? 1 : 0;
792 da8xx_fb_var.bits_per_pixel = lcd_cfg->bpp;
793
794 da8xx_fb_var.hsync_len = lcdc_info->hsw;
795 da8xx_fb_var.vsync_len = lcdc_info->vsw;
796
797 /* Initialize fbinfo */
798 da8xx_fb_info->flags = FBINFO_FLAG_DEFAULT;
799 da8xx_fb_info->fix = da8xx_fb_fix;
800 da8xx_fb_info->var = da8xx_fb_var;
801 da8xx_fb_info->fbops = &da8xx_fb_ops;
802 da8xx_fb_info->pseudo_palette = par->pseudo_palette;
Sudhakar Rajashekhara3510b8f2009-12-01 13:17:43 -0800803 da8xx_fb_info->fix.visual = (da8xx_fb_info->var.bits_per_pixel <= 8) ?
804 FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_TRUECOLOR;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700805
806 ret = fb_alloc_cmap(&da8xx_fb_info->cmap, PALETTE_SIZE, 0);
807 if (ret)
808 goto err_free_irq;
809
810 /* First palette_sz byte of the frame buffer is the palette */
811 da8xx_fb_info->cmap.len = par->palette_sz;
812
813 /* Flush the buffer to the screen. */
814 lcd_blit(LOAD_DATA, par);
815
816 /* initialize var_screeninfo */
817 da8xx_fb_var.activate = FB_ACTIVATE_FORCE;
818 fb_set_var(da8xx_fb_info, &da8xx_fb_var);
819
820 dev_set_drvdata(&device->dev, da8xx_fb_info);
821 /* Register the Frame Buffer */
822 if (register_framebuffer(da8xx_fb_info) < 0) {
823 dev_err(&device->dev,
824 "GLCD: Frame Buffer Registration Failed!\n");
825 ret = -EINVAL;
826 goto err_dealloc_cmap;
827 }
828
829 /* enable raster engine */
830 lcdc_write(lcdc_read(LCD_RASTER_CTRL_REG) |
831 LCD_RASTER_ENABLE, LCD_RASTER_CTRL_REG);
832
833 return 0;
834
835err_dealloc_cmap:
836 fb_dealloc_cmap(&da8xx_fb_info->cmap);
837
838err_free_irq:
839 free_irq(par->irq, par);
840
841err_release_fb_mem:
842 dma_free_coherent(NULL, par->databuf_sz + PAGE_SIZE,
Sudhakar Rajashekhara3510b8f2009-12-01 13:17:43 -0800843 da8xx_fb_info->screen_base - PAGE_SIZE,
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700844 da8xx_fb_info->fix.smem_start);
845
846err_release_fb:
847 framebuffer_release(da8xx_fb_info);
848
849err_clk_disable:
850 clk_disable(fb_clk);
851
852err_clk_put:
853 clk_put(fb_clk);
854
855err_ioremap:
856 iounmap((void __iomem *)da8xx_fb_reg_base);
857
858err_request_mem:
859 release_mem_region(lcdc_regs->start, len);
860
861 return ret;
862}
863
864#ifdef CONFIG_PM
865static int fb_suspend(struct platform_device *dev, pm_message_t state)
866{
867 return -EBUSY;
868}
869static int fb_resume(struct platform_device *dev)
870{
871 return -EBUSY;
872}
873#else
874#define fb_suspend NULL
875#define fb_resume NULL
876#endif
877
878static struct platform_driver da8xx_fb_driver = {
879 .probe = fb_probe,
880 .remove = fb_remove,
881 .suspend = fb_suspend,
882 .resume = fb_resume,
883 .driver = {
884 .name = DRIVER_NAME,
885 .owner = THIS_MODULE,
886 },
887};
888
889static int __init da8xx_fb_init(void)
890{
891 return platform_driver_register(&da8xx_fb_driver);
892}
893
894static void __exit da8xx_fb_cleanup(void)
895{
896 platform_driver_unregister(&da8xx_fb_driver);
897}
898
899module_init(da8xx_fb_init);
900module_exit(da8xx_fb_cleanup);
901
902MODULE_DESCRIPTION("Framebuffer driver for TI da8xx/omap-l1xx");
903MODULE_AUTHOR("Texas Instruments");
904MODULE_LICENSE("GPL");