[s3c24xx] fix glamo modesetting.
[openwrt/svn-archive/archive.git] / target / linux / s3c24xx / files-2.6.30 / drivers / mfd / glamo / glamo-fb.c
1 /* Smedia Glamo 336x/337x driver
2 *
3 * (C) 2007-2008 by Openmoko, Inc.
4 * Author: Harald Welte <laforge@openmoko.org>
5 * All rights reserved.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of
10 * the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
20 * MA 02111-1307 USA
21 */
22
23 #include <linux/module.h>
24 #include <linux/kernel.h>
25 #include <linux/errno.h>
26 #include <linux/string.h>
27 #include <linux/mm.h>
28 #include <linux/slab.h>
29 #include <linux/delay.h>
30 #include <linux/fb.h>
31 #include <linux/init.h>
32 #include <linux/vmalloc.h>
33 #include <linux/dma-mapping.h>
34 #include <linux/interrupt.h>
35 #include <linux/workqueue.h>
36 #include <linux/wait.h>
37 #include <linux/platform_device.h>
38 #include <linux/clk.h>
39 #include <linux/spinlock.h>
40 #include <linux/io.h>
41 #include <linux/uaccess.h>
42
43 #include <asm/div64.h>
44
45 #ifdef CONFIG_PM
46 #include <linux/pm.h>
47 #endif
48
49 #include <linux/glamofb.h>
50
51 #include "glamo-regs.h"
52 #include "glamo-core.h"
53
54 #ifndef DEBUG
55 #define GLAMO_LOG(...)
56 #else
57 #define GLAMO_LOG(...) \
58 do { \
59 printk(KERN_DEBUG "in %s:%s:%d", __FILE__, __func__, __LINE__); \
60 printk(KERN_DEBUG __VA_ARGS__); \
61 } while (0);
62 #endif
63
64 static void glamofb_program_mode(struct glamofb_handle* glamo);
65
66 struct glamofb_handle {
67 struct fb_info *fb;
68 struct device *dev;
69 struct resource *reg;
70 struct resource *fb_res;
71 char __iomem *base;
72 struct glamofb_platform_data *mach_info;
73 char __iomem *cursor_addr;
74 int cursor_on;
75 u_int32_t pseudo_pal[16];
76 spinlock_t lock_cmd;
77 int blank_mode;
78 int mode_set; /* 0 if the current display mode hasn't been set on the glamo */
79 int output_enabled; /* 0 if the video output is disabled */
80 };
81
82 /* 'sibling' spi device for lcm init */
83 static struct platform_device glamo_spi_dev = {
84 .name = "glamo-lcm-spi",
85 };
86
87 static void glamo_output_enable(struct glamofb_handle *gfb) {
88 struct glamo_core *gcore = gfb->mach_info->glamo;
89
90 if (gfb->output_enabled)
91 return;
92
93 /* enable the pixel clock if off */
94 glamo_engine_clkreg_set(gcore,
95 GLAMO_ENGINE_LCD,
96 GLAMO_CLOCK_LCD_EN_DCLK,
97 GLAMO_CLOCK_LCD_EN_DCLK);
98
99 gfb->output_enabled = 1;
100 if (!gfb->mode_set)
101 glamofb_program_mode(gfb);
102 }
103
104 static void glamo_output_disable(struct glamofb_handle *gfb) {
105 struct glamo_core *gcore = gfb->mach_info->glamo;
106
107 if (!gfb->output_enabled)
108 return;
109
110 /* enable the pixel clock if off */
111 glamo_engine_clkreg_set(gcore,
112 GLAMO_ENGINE_LCD,
113 GLAMO_CLOCK_LCD_EN_DCLK,
114 0);
115
116 gfb->output_enabled = 0;
117 }
118
119
120 static int reg_read(struct glamofb_handle *glamo,
121 u_int16_t reg)
122 {
123 int i = 0;
124
125 for (i = 0; i != 2; i++)
126 nop();
127
128 return readw(glamo->base + reg);
129 }
130
131 static void reg_write(struct glamofb_handle *glamo,
132 u_int16_t reg, u_int16_t val)
133 {
134 int i = 0;
135
136 for (i = 0; i != 2; i++)
137 nop();
138
139 writew(val, glamo->base + reg);
140 }
141
142 static struct glamo_script glamo_regs[] = {
143 { GLAMO_REG_LCD_MODE1, 0x0020 },
144 /* no display rotation, no hardware cursor, no dither, no gamma,
145 * no retrace flip, vsync low-active, hsync low active,
146 * no TVCLK, no partial display, hw dest color from fb,
147 * no partial display mode, LCD1, software flip, */
148 { GLAMO_REG_LCD_MODE2, 0x9020 },
149 /* video flip, no ptr, no ptr, dhclk off,
150 * normal mode, no cpuif,
151 * res, serial msb first, single fb, no fr ctrl,
152 * cpu if bits all zero, no crc
153 * 0000 0000 0010 0000 */
154 { GLAMO_REG_LCD_MODE3, 0x0b40 },
155 /* src data rgb565, res, 18bit rgb666
156 * 000 01 011 0100 0000 */
157 { GLAMO_REG_LCD_POLARITY, 0x440c },
158 /* DE high active, no cpu/lcd if, cs0 force low, a0 low active,
159 * np cpu if, 9bit serial data, sclk rising edge latch data
160 * 01 00 0 100 0 000 01 0 0 */
161 /* The following values assume 640*480@16bpp */
162 { GLAMO_REG_LCD_A_BASE1, 0x0000 }, /* display A base address 15:0 */
163 { GLAMO_REG_LCD_A_BASE2, 0x0000 }, /* display A base address 22:16 */
164 { GLAMO_REG_LCD_CURSOR_BASE1, 0xC000 }, /* cursor base address 15:0 */
165 { GLAMO_REG_LCD_CURSOR_BASE2, 0x0012 }, /* cursor base address 22:16 */
166 { GLAMO_REG_LCD_COMMAND2, 0x0000 }, /* display page A */
167 };
168
169 static int glamofb_run_script(struct glamofb_handle *glamo,
170 struct glamo_script *script, int len)
171 {
172 int i;
173
174 if (glamo->mach_info->glamo->suspending) {
175 dev_err(&glamo->mach_info->glamo->pdev->dev,
176 "IGNORING glamofb_run_script while "
177 "suspended\n");
178 return -EBUSY;
179 }
180
181 for (i = 0; i < len; i++) {
182 struct glamo_script *line = &script[i];
183
184 if (line->reg == 0xffff)
185 return 0;
186 else if (line->reg == 0xfffe)
187 msleep(line->val);
188 else
189 reg_write(glamo, script[i].reg, script[i].val);
190 }
191
192 return 0;
193 }
194
195 static int glamofb_check_var(struct fb_var_screeninfo *var,
196 struct fb_info *info)
197 {
198 struct glamofb_handle *glamo = info->par;
199
200 if (glamo->mach_info->glamo->suspending) {
201 dev_err(&glamo->mach_info->glamo->pdev->dev,
202 "IGNORING glamofb_check_var while "
203 "suspended\n");
204 return -EBUSY;
205 }
206
207 if (var->bits_per_pixel != 16)
208 var->bits_per_pixel = 16;
209
210 var->height = glamo->mach_info->height;
211 var->width = glamo->mach_info->width;
212
213 /* FIXME: set rgb positions */
214 switch (var->bits_per_pixel) {
215 case 16:
216 switch (reg_read(glamo, GLAMO_REG_LCD_MODE3) & 0xc000) {
217 case GLAMO_LCD_SRC_RGB565:
218 var->red.offset = 11;
219 var->green.offset = 5;
220 var->blue.offset = 0;
221 var->red.length = 5;
222 var->green.length = 6;
223 var->blue.length = 5;
224 var->transp.length = 0;
225 break;
226 case GLAMO_LCD_SRC_ARGB1555:
227 var->transp.offset = 15;
228 var->red.offset = 10;
229 var->green.offset = 5;
230 var->blue.offset = 0;
231 var->transp.length = 1;
232 var->red.length = 5;
233 var->green.length = 5;
234 var->blue.length = 5;
235 break;
236 case GLAMO_LCD_SRC_ARGB4444:
237 var->transp.offset = 12;
238 var->red.offset = 8;
239 var->green.offset = 4;
240 var->blue.offset = 0;
241 var->transp.length = 4;
242 var->red.length = 4;
243 var->green.length = 4;
244 var->blue.length = 4;
245 break;
246 }
247 break;
248 case 24:
249 case 32:
250 default:
251 /* The Smedia Glamo doesn't support anything but 16bit color */
252 printk(KERN_ERR
253 "Smedia driver does not [yet?] support 24/32bpp\n");
254 return -EINVAL;
255 }
256
257 return 0;
258 }
259
260 static void reg_set_bit_mask(struct glamofb_handle *glamo,
261 u_int16_t reg, u_int16_t mask,
262 u_int16_t val)
263 {
264 u_int16_t tmp;
265
266 val &= mask;
267
268 tmp = reg_read(glamo, reg);
269 tmp &= ~mask;
270 tmp |= val;
271 reg_write(glamo, reg, tmp);
272 }
273
274 #define GLAMO_LCD_WIDTH_MASK 0x03FF
275 #define GLAMO_LCD_HEIGHT_MASK 0x03FF
276 #define GLAMO_LCD_PITCH_MASK 0x07FE
277 #define GLAMO_LCD_HV_TOTAL_MASK 0x03FF
278 #define GLAMO_LCD_HV_RETR_START_MASK 0x03FF
279 #define GLAMO_LCD_HV_RETR_END_MASK 0x03FF
280 #define GLAMO_LCD_HV_RETR_DISP_START_MASK 0x03FF
281 #define GLAMO_LCD_HV_RETR_DISP_END_MASK 0x03FF
282
283 /* the caller has to enxure lock_cmd is held and we are in cmd mode */
284 static void __rotate_lcd(struct glamofb_handle *glamo, __u32 rotation)
285 {
286 int glamo_rot;
287
288 if (glamo->mach_info->glamo->suspending) {
289 dev_err(&glamo->mach_info->glamo->pdev->dev,
290 "IGNORING rotate_lcd while "
291 "suspended\n");
292 return;
293 }
294
295 switch (rotation) {
296 case FB_ROTATE_CW:
297 glamo_rot = GLAMO_LCD_ROT_MODE_90;
298 break;
299 case FB_ROTATE_UD:
300 glamo_rot = GLAMO_LCD_ROT_MODE_180;
301 break;
302 case FB_ROTATE_CCW:
303 glamo_rot = GLAMO_LCD_ROT_MODE_270;
304 break;
305 default:
306 glamo_rot = GLAMO_LCD_ROT_MODE_0;
307 break;
308 }
309
310 reg_set_bit_mask(glamo,
311 GLAMO_REG_LCD_WIDTH,
312 GLAMO_LCD_ROT_MODE_MASK,
313 glamo_rot);
314 reg_set_bit_mask(glamo,
315 GLAMO_REG_LCD_MODE1,
316 GLAMO_LCD_MODE1_ROTATE_EN,
317 (glamo_rot != GLAMO_LCD_ROT_MODE_0) ?
318 GLAMO_LCD_MODE1_ROTATE_EN : 0);
319 }
320
321 static void glamofb_program_mode(struct glamofb_handle* gfb) {
322 int sync, bp, disp, fp, total;
323 unsigned long flags;
324 struct glamo_core *gcore = gfb->mach_info->glamo;
325 struct fb_var_screeninfo *var = &gfb->fb->var;
326
327 dev_dbg(&gcore->pdev->dev,
328 "glamofb_program_mode spin_lock_irqsave\n");
329 spin_lock_irqsave(&gfb->lock_cmd, flags);
330
331 if (glamofb_cmd_mode(gfb, 1))
332 goto out_unlock;
333
334 if (var->pixclock)
335 glamo_engine_reclock(gcore,
336 GLAMO_ENGINE_LCD,
337 gfb->fb->var.pixclock);
338
339 reg_set_bit_mask(gfb,
340 GLAMO_REG_LCD_WIDTH,
341 GLAMO_LCD_WIDTH_MASK,
342 var->xres);
343 reg_set_bit_mask(gfb,
344 GLAMO_REG_LCD_HEIGHT,
345 GLAMO_LCD_HEIGHT_MASK,
346 var->yres);
347 reg_set_bit_mask(gfb,
348 GLAMO_REG_LCD_PITCH,
349 GLAMO_LCD_PITCH_MASK,
350 gfb->fb->fix.line_length);
351
352 /* honour the rotation request */
353 __rotate_lcd(gfb, var->rotate);
354
355 /* update scannout timings */
356 sync = 0;
357 bp = sync + var->hsync_len;
358 disp = bp + var->left_margin;
359 fp = disp + var->xres;
360 total = fp + var->right_margin;
361
362 reg_set_bit_mask(gfb, GLAMO_REG_LCD_HORIZ_TOTAL,
363 GLAMO_LCD_HV_TOTAL_MASK, total);
364 reg_set_bit_mask(gfb, GLAMO_REG_LCD_HORIZ_RETR_START,
365 GLAMO_LCD_HV_RETR_START_MASK, sync);
366 reg_set_bit_mask(gfb, GLAMO_REG_LCD_HORIZ_RETR_END,
367 GLAMO_LCD_HV_RETR_END_MASK, bp);
368 reg_set_bit_mask(gfb, GLAMO_REG_LCD_HORIZ_DISP_START,
369 GLAMO_LCD_HV_RETR_DISP_START_MASK, disp);
370 reg_set_bit_mask(gfb, GLAMO_REG_LCD_HORIZ_DISP_END,
371 GLAMO_LCD_HV_RETR_DISP_END_MASK, fp);
372
373 sync = 0;
374 bp = sync + var->vsync_len;
375 disp = bp + var->upper_margin;
376 fp = disp + var->yres;
377 total = fp + var->lower_margin;
378
379 reg_set_bit_mask(gfb, GLAMO_REG_LCD_VERT_TOTAL,
380 GLAMO_LCD_HV_TOTAL_MASK, total);
381 reg_set_bit_mask(gfb, GLAMO_REG_LCD_VERT_RETR_START,
382 GLAMO_LCD_HV_RETR_START_MASK, sync);
383 reg_set_bit_mask(gfb, GLAMO_REG_LCD_VERT_RETR_END,
384 GLAMO_LCD_HV_RETR_END_MASK, bp);
385 reg_set_bit_mask(gfb, GLAMO_REG_LCD_VERT_DISP_START,
386 GLAMO_LCD_HV_RETR_DISP_START_MASK, disp);
387 reg_set_bit_mask(gfb, GLAMO_REG_LCD_VERT_DISP_END,
388 GLAMO_LCD_HV_RETR_DISP_END_MASK, fp);
389
390 glamofb_cmd_mode(gfb, 0);
391
392 gfb->mode_set = 1;
393
394 out_unlock:
395 dev_dbg(&gcore->pdev->dev,
396 "glamofb_program_mode spin_unlock_irqrestore\n");
397 spin_unlock_irqrestore(&gfb->lock_cmd, flags);
398 }
399
400
401 static int glamofb_pan_display(struct fb_var_screeninfo *var,
402 struct fb_info *info)
403 {
404 return 0;
405 }
406
407 static struct fb_videomode *glamofb_find_mode(struct fb_info *info,
408 struct fb_var_screeninfo *var) {
409 struct glamofb_handle *glamo = info->par;
410 struct glamofb_platform_data *mach_info = glamo->mach_info;
411 struct fb_videomode *mode;
412 int i;
413
414 for(i = mach_info->num_modes, mode = mach_info->modes; i > 0; --i, ++mode) {
415 if (mode->xres == var->xres &&
416 mode->yres == var->yres)
417 return mode;
418 }
419
420 return NULL;
421 }
422
423 static int glamofb_set_par(struct fb_info *info)
424 {
425 struct glamofb_handle *glamo = info->par;
426 struct fb_var_screeninfo *var = &info->var;
427 struct fb_videomode *mode;
428
429 if (glamo->mach_info->glamo->suspending) {
430 dev_err(&glamo->mach_info->glamo->pdev->dev,
431 "IGNORING glamofb_set_par while "
432 "suspended\n");
433 return -EBUSY;
434 }
435
436 mode = glamofb_find_mode(info, var);
437 if (!mode)
438 return -EINVAL;
439
440 fb_videomode_to_var(var, mode);
441
442 info->mode = mode;
443
444 glamo->mode_set = 0;
445
446 switch(var->rotate) {
447 case FB_ROTATE_CW:
448 case FB_ROTATE_CCW:
449 info->fix.line_length = (var->yres * var->bits_per_pixel) / 8;
450 /* FIXME: Limit pixelclock */
451 var->pixclock *= 2;
452 break;
453 default:
454 info->fix.line_length = (var->xres * var->bits_per_pixel) / 8;
455 break;
456 }
457
458 if(glamo->output_enabled)
459 glamofb_program_mode(glamo);
460
461 return 0;
462 }
463
464 static int glamofb_blank(int blank_mode, struct fb_info *info)
465 {
466 struct glamofb_handle *gfb = info->par;
467
468 dev_dbg(gfb->dev, "glamofb_blank(%u)\n", blank_mode);
469
470 switch (blank_mode) {
471 case FB_BLANK_VSYNC_SUSPEND:
472 case FB_BLANK_HSYNC_SUSPEND:
473 /* FIXME: add pdata hook/flag to indicate whether
474 * we should already switch off pixel clock here */
475 break;
476 case FB_BLANK_POWERDOWN:
477 /* disable the pixel clock */
478 glamo_output_disable(gfb);
479 gfb->blank_mode = blank_mode;
480 break;
481 case FB_BLANK_UNBLANK:
482 case FB_BLANK_NORMAL:
483 glamo_output_enable(gfb);
484 gfb->blank_mode = blank_mode;
485 break;
486 }
487
488 /* FIXME: once we have proper clock management in glamo-core,
489 * we can determine if other units need MCLK1 or the PLL, and
490 * disable it if not used. */
491 return 0;
492 }
493
494 static inline unsigned int chan_to_field(unsigned int chan,
495 struct fb_bitfield *bf)
496 {
497 chan &= 0xffff;
498 chan >>= 16 - bf->length;
499 return chan << bf->offset;
500 }
501
502 static int glamofb_setcolreg(unsigned regno,
503 unsigned red, unsigned green, unsigned blue,
504 unsigned transp, struct fb_info *info)
505 {
506 struct glamofb_handle *glamo = info->par;
507 unsigned int val;
508
509 if (glamo->mach_info->glamo->suspending) {
510 dev_err(&glamo->mach_info->glamo->pdev->dev,
511 "IGNORING glamofb_set_par while "
512 "suspended\n");
513 return -EBUSY;
514 }
515
516 switch (glamo->fb->fix.visual) {
517 case FB_VISUAL_TRUECOLOR:
518 case FB_VISUAL_DIRECTCOLOR:
519 /* true-colour, use pseuo-palette */
520
521 if (regno < 16) {
522 u32 *pal = glamo->fb->pseudo_palette;
523
524 val = chan_to_field(red, &glamo->fb->var.red);
525 val |= chan_to_field(green, &glamo->fb->var.green);
526 val |= chan_to_field(blue, &glamo->fb->var.blue);
527
528 pal[regno] = val;
529 };
530 break;
531 default:
532 return 1; /* unknown type */
533 }
534
535 return 0;
536 }
537
538 #ifdef CONFIG_MFD_GLAMO_HWACCEL
539 static inline void glamofb_vsync_wait(struct glamofb_handle *glamo,
540 int line, int size, int range)
541 {
542 int count[2];
543
544 do {
545 count[0] = reg_read(glamo, GLAMO_REG_LCD_STATUS2) & 0x3ff;
546 count[1] = reg_read(glamo, GLAMO_REG_LCD_STATUS2) & 0x3ff;
547 } while (count[0] != count[1] ||
548 (line < count[0] + range &&
549 size > count[0] - range) ||
550 count[0] < range * 2);
551 }
552
553 /*
554 * Enable/disable the hardware cursor mode altogether
555 * (for blinking and such, use glamofb_cursor()).
556 */
557 static void glamofb_cursor_onoff(struct glamofb_handle *glamo, int on)
558 {
559 int y, size;
560
561 if (glamo->cursor_on) {
562 y = reg_read(glamo, GLAMO_REG_LCD_CURSOR_Y_POS);
563 size = reg_read(glamo, GLAMO_REG_LCD_CURSOR_Y_SIZE);
564
565 glamofb_vsync_wait(glamo, y, size, 30);
566 }
567
568 reg_set_bit_mask(glamo, GLAMO_REG_LCD_MODE1,
569 GLAMO_LCD_MODE1_CURSOR_EN,
570 on ? GLAMO_LCD_MODE1_CURSOR_EN : 0);
571 glamo->cursor_on = on;
572
573 /* Hide the cursor by default */
574 reg_write(glamo, GLAMO_REG_LCD_CURSOR_X_SIZE, 0);
575 }
576
577 static int glamofb_cursor(struct fb_info *info, struct fb_cursor *cursor)
578 {
579 struct glamofb_handle *glamo = info->par;
580 unsigned long flags;
581
582 spin_lock_irqsave(&glamo->lock_cmd, flags);
583
584 reg_write(glamo, GLAMO_REG_LCD_CURSOR_X_SIZE,
585 cursor->enable ? cursor->image.width : 0);
586
587 if (cursor->set & FB_CUR_SETPOS) {
588 reg_write(glamo, GLAMO_REG_LCD_CURSOR_X_POS,
589 cursor->image.dx);
590 reg_write(glamo, GLAMO_REG_LCD_CURSOR_Y_POS,
591 cursor->image.dy);
592 }
593
594 if (cursor->set & FB_CUR_SETCMAP) {
595 uint16_t fg = glamo->pseudo_pal[cursor->image.fg_color];
596 uint16_t bg = glamo->pseudo_pal[cursor->image.bg_color];
597
598 reg_write(glamo, GLAMO_REG_LCD_CURSOR_FG_COLOR, fg);
599 reg_write(glamo, GLAMO_REG_LCD_CURSOR_BG_COLOR, bg);
600 reg_write(glamo, GLAMO_REG_LCD_CURSOR_DST_COLOR, fg);
601 }
602
603 if (cursor->set & FB_CUR_SETHOT)
604 reg_write(glamo, GLAMO_REG_LCD_CURSOR_PRESET,
605 (cursor->hot.x << 8) | cursor->hot.y);
606
607 if ((cursor->set & FB_CUR_SETSIZE) ||
608 (cursor->set & (FB_CUR_SETIMAGE | FB_CUR_SETSHAPE))) {
609 int x, y, pitch, op;
610 const uint8_t *pcol = cursor->image.data;
611 const uint8_t *pmsk = cursor->mask;
612 uint8_t __iomem *dst = glamo->cursor_addr;
613 uint8_t dcol = 0;
614 uint8_t dmsk = 0;
615 uint8_t byte = 0;
616
617 if (cursor->image.depth > 1) {
618 spin_unlock_irqrestore(&glamo->lock_cmd, flags);
619 return -EINVAL;
620 }
621
622 pitch = ((cursor->image.width + 7) >> 2) & ~1;
623 reg_write(glamo, GLAMO_REG_LCD_CURSOR_PITCH,
624 pitch);
625 reg_write(glamo, GLAMO_REG_LCD_CURSOR_Y_SIZE,
626 cursor->image.height);
627
628 for (y = 0; y < cursor->image.height; y++) {
629 byte = 0;
630 for (x = 0; x < cursor->image.width; x++) {
631 if ((x % 8) == 0) {
632 dcol = *pcol++;
633 dmsk = *pmsk++;
634 } else {
635 dcol >>= 1;
636 dmsk >>= 1;
637 }
638
639 if (cursor->rop == ROP_COPY)
640 op = (dmsk & 1) ?
641 (dcol & 1) ? 1 : 3 : 0;
642 else
643 op = ((dmsk & 1) << 1) |
644 ((dcol & 1) << 0);
645 byte |= op << ((x & 3) << 1);
646
647 if (x % 4 == 3) {
648 writeb(byte, dst + x / 4);
649 byte = 0;
650 }
651 }
652 if (x % 4) {
653 writeb(byte, dst + x / 4);
654 byte = 0;
655 }
656
657 dst += pitch;
658 }
659 }
660
661 spin_unlock_irqrestore(&glamo->lock_cmd, flags);
662
663 return 0;
664 }
665 #endif
666
667 static inline int glamofb_cmdq_empty(struct glamofb_handle *gfb)
668 {
669 /* DGCMdQempty -- 1 == command queue is empty */
670 return reg_read(gfb, GLAMO_REG_LCD_STATUS1) & (1 << 15);
671 }
672
673 /* call holding gfb->lock_cmd when locking, until you unlock */
674 int glamofb_cmd_mode(struct glamofb_handle *gfb, int on)
675 {
676 int timeout = 2000000;
677
678 if (gfb->mach_info->glamo->suspending) {
679 dev_err(&gfb->mach_info->glamo->pdev->dev,
680 "IGNORING glamofb_cmd_mode while "
681 "suspended\n");
682 return -EBUSY;
683 }
684
685 dev_dbg(gfb->dev, "glamofb_cmd_mode(gfb=%p, on=%d)\n", gfb, on);
686 if (on) {
687 dev_dbg(gfb->dev, "%s: waiting for cmdq empty: ",
688 __func__);
689 while ((!glamofb_cmdq_empty(gfb)) && (timeout--))
690 yield();
691 if (timeout < 0) {
692 printk(KERN_ERR"*************"
693 "glamofb cmd_queue never got empty"
694 "*************\n");
695 return -EIO;
696 }
697 dev_dbg(gfb->dev, "empty!\n");
698
699 /* display the entire frame then switch to command */
700 reg_write(gfb, GLAMO_REG_LCD_COMMAND1,
701 GLAMO_LCD_CMD_TYPE_DISP |
702 GLAMO_LCD_CMD_DATA_FIRE_VSYNC);
703
704 /* wait until lcd idle */
705 dev_dbg(gfb->dev, "waiting for lcd idle: ");
706 timeout = 2000000;
707 while ((!reg_read(gfb, GLAMO_REG_LCD_STATUS2) & (1 << 12)) &&
708 (timeout--))
709 /* yield() */;
710 if (timeout < 0) {
711 printk(KERN_ERR"*************"
712 "glamofb lcd never idle"
713 "*************\n");
714 return -EIO;
715 }
716
717 mdelay(100);
718
719 dev_dbg(gfb->dev, "cmd mode entered\n");
720
721 } else {
722 /* RGB interface needs vsync/hsync */
723 if (reg_read(gfb, GLAMO_REG_LCD_MODE3) & GLAMO_LCD_MODE3_RGB)
724 reg_write(gfb, GLAMO_REG_LCD_COMMAND1,
725 GLAMO_LCD_CMD_TYPE_DISP |
726 GLAMO_LCD_CMD_DATA_DISP_SYNC);
727
728 reg_write(gfb, GLAMO_REG_LCD_COMMAND1,
729 GLAMO_LCD_CMD_TYPE_DISP |
730 GLAMO_LCD_CMD_DATA_DISP_FIRE);
731 }
732
733 return 0;
734 }
735 EXPORT_SYMBOL_GPL(glamofb_cmd_mode);
736
737
738 int glamofb_cmd_write(struct glamofb_handle *gfb, u_int16_t val)
739 {
740 int timeout = 200000;
741
742 if (gfb->mach_info->glamo->suspending) {
743 dev_err(&gfb->mach_info->glamo->pdev->dev,
744 "IGNORING glamofb_cmd_write while "
745 "suspended\n");
746 return -EBUSY;
747 }
748
749 dev_dbg(gfb->dev, "%s: waiting for cmdq empty\n", __func__);
750 while ((!glamofb_cmdq_empty(gfb)) && (timeout--))
751 yield();
752 if (timeout < 0) {
753 printk(KERN_ERR"*************"
754 "glamofb cmd_queue never got empty"
755 "*************\n");
756 return 1;
757 }
758 dev_dbg(gfb->dev, "idle, writing 0x%04x\n", val);
759
760 reg_write(gfb, GLAMO_REG_LCD_COMMAND1, val);
761
762 return 0;
763 }
764 EXPORT_SYMBOL_GPL(glamofb_cmd_write);
765
766 static struct fb_ops glamofb_ops = {
767 .owner = THIS_MODULE,
768 .fb_check_var = glamofb_check_var,
769 .fb_pan_display = glamofb_pan_display,
770 .fb_set_par = glamofb_set_par,
771 .fb_blank = glamofb_blank,
772 .fb_setcolreg = glamofb_setcolreg,
773 #ifdef CONFIG_MFD_GLAMO_HWACCEL
774 .fb_cursor = glamofb_cursor,
775 #endif
776 .fb_fillrect = cfb_fillrect,
777 .fb_copyarea = cfb_copyarea,
778 .fb_imageblit = cfb_imageblit,
779 };
780
781 static int glamofb_init_regs(struct glamofb_handle *glamo)
782 {
783 struct fb_info *info = glamo->fb;
784
785 glamofb_check_var(&info->var, info);
786 glamofb_run_script(glamo, glamo_regs, ARRAY_SIZE(glamo_regs));
787 glamofb_set_par(info);
788
789 return 0;
790 }
791
792 static int __init glamofb_probe(struct platform_device *pdev)
793 {
794 int rc = -EIO;
795 struct fb_info *fbinfo;
796 struct glamofb_handle *glamofb;
797 struct glamofb_platform_data *mach_info = pdev->dev.platform_data;
798
799 printk(KERN_INFO "SMEDIA Glamo frame buffer driver (C) 2007 "
800 "Openmoko, Inc.\n");
801
802 fbinfo = framebuffer_alloc(sizeof(struct glamofb_handle), &pdev->dev);
803 if (!fbinfo)
804 return -ENOMEM;
805
806 glamofb = fbinfo->par;
807 glamofb->fb = fbinfo;
808 glamofb->dev = &pdev->dev;
809
810 glamofb->blank_mode = FB_BLANK_POWERDOWN;
811
812 strcpy(fbinfo->fix.id, "SMedia Glamo");
813
814 glamofb->reg = platform_get_resource_byname(pdev, IORESOURCE_MEM,
815 "glamo-fb-regs");
816 if (!glamofb->reg) {
817 dev_err(&pdev->dev, "platform device with no registers?\n");
818 rc = -ENOENT;
819 goto out_free;
820 }
821
822 glamofb->fb_res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
823 "glamo-fb-mem");
824 if (!glamofb->fb_res) {
825 dev_err(&pdev->dev, "platform device with no memory ?\n");
826 rc = -ENOENT;
827 goto out_free;
828 }
829
830 glamofb->reg = request_mem_region(glamofb->reg->start,
831 resource_size(glamofb->reg), pdev->name);
832 if (!glamofb->reg) {
833 dev_err(&pdev->dev, "failed to request mmio region\n");
834 goto out_free;
835 }
836
837 glamofb->fb_res = request_mem_region(glamofb->fb_res->start,
838 mach_info->fb_mem_size,
839 pdev->name);
840 if (!glamofb->fb_res) {
841 dev_err(&pdev->dev, "failed to request vram region\n");
842 goto out_release_reg;
843 }
844
845 /* we want to remap only the registers required for this core
846 * driver. */
847 glamofb->base = ioremap_nocache(glamofb->reg->start, resource_size(glamofb->reg));
848 if (!glamofb->base) {
849 dev_err(&pdev->dev, "failed to ioremap() mmio memory\n");
850 goto out_release_fb;
851 }
852 fbinfo->fix.smem_start = (unsigned long) glamofb->fb_res->start;
853 fbinfo->fix.smem_len = (__u32) resource_size(glamofb->fb_res);
854
855 fbinfo->screen_base = ioremap(glamofb->fb_res->start,
856 resource_size(glamofb->fb_res));
857 if (!fbinfo->screen_base) {
858 dev_err(&pdev->dev, "failed to ioremap() vram memory\n");
859 goto out_release_fb;
860 }
861 glamofb->cursor_addr = fbinfo->screen_base + 0x12C000;
862
863 platform_set_drvdata(pdev, glamofb);
864
865 glamofb->mach_info = pdev->dev.platform_data;
866
867 fbinfo->fix.visual = FB_VISUAL_TRUECOLOR;
868 fbinfo->fix.type = FB_TYPE_PACKED_PIXELS;
869 fbinfo->fix.type_aux = 0;
870 fbinfo->fix.xpanstep = 0;
871 fbinfo->fix.ypanstep = 0;
872 fbinfo->fix.ywrapstep = 0;
873 fbinfo->fix.accel = FB_ACCEL_GLAMO;
874
875
876 fbinfo->fbops = &glamofb_ops;
877 fbinfo->flags = FBINFO_FLAG_DEFAULT;
878 fbinfo->pseudo_palette = &glamofb->pseudo_pal;
879
880 fbinfo->mode = mach_info->modes;
881 fb_videomode_to_var(&fbinfo->var, fbinfo->mode);
882 fbinfo->var.bits_per_pixel = 16;
883 fbinfo->var.nonstd = 0;
884 fbinfo->var.activate = FB_ACTIVATE_NOW;
885 fbinfo->var.height = mach_info->height;
886 fbinfo->var.width = mach_info->width;
887 fbinfo->var.accel_flags = 0;
888 fbinfo->var.vmode = FB_VMODE_NONINTERLACED;
889
890 glamo_engine_enable(mach_info->glamo, GLAMO_ENGINE_LCD);
891 glamo_engine_reset(mach_info->glamo, GLAMO_ENGINE_LCD);
892 glamofb->output_enabled = 1;
893 glamofb->mode_set = 1;
894
895 dev_info(&pdev->dev, "spin_lock_init\n");
896 spin_lock_init(&glamofb->lock_cmd);
897 glamofb_init_regs(glamofb);
898 #ifdef CONFIG_MFD_GLAMO_HWACCEL
899 glamofb_cursor_onoff(glamofb, 1);
900 #endif
901
902 fb_videomode_to_modelist(mach_info->modes, mach_info->num_modes,
903 &fbinfo->modelist);
904
905 rc = register_framebuffer(fbinfo);
906 if (rc < 0) {
907 dev_err(&pdev->dev, "failed to register framebuffer\n");
908 goto out_unmap_fb;
909 }
910
911 if (mach_info->spi_info) {
912 /* register the sibling spi device */
913 mach_info->spi_info->glamofb_handle = glamofb;
914 glamo_spi_dev.dev.parent = &pdev->dev;
915 glamo_spi_dev.dev.platform_data = mach_info->spi_info;
916 platform_device_register(&glamo_spi_dev);
917 }
918
919 printk(KERN_INFO "fb%d: %s frame buffer device\n",
920 fbinfo->node, fbinfo->fix.id);
921
922 return 0;
923
924 out_unmap_fb:
925 iounmap(fbinfo->screen_base);
926 iounmap(glamofb->base);
927 out_release_fb:
928 release_mem_region(glamofb->fb_res->start, resource_size(glamofb->fb_res));
929 out_release_reg:
930 release_mem_region(glamofb->reg->start, resource_size(glamofb->reg));
931 out_free:
932 framebuffer_release(fbinfo);
933 return rc;
934 }
935
936 static int glamofb_remove(struct platform_device *pdev)
937 {
938 struct glamofb_handle *glamofb = platform_get_drvdata(pdev);
939
940 platform_set_drvdata(pdev, NULL);
941 iounmap(glamofb->base);
942 release_mem_region(glamofb->reg->start, resource_size(glamofb->reg));
943 kfree(glamofb);
944
945 return 0;
946 }
947
948 #ifdef CONFIG_PM
949
950 static int glamofb_suspend(struct platform_device *pdev, pm_message_t state)
951 {
952 struct glamofb_handle *gfb = platform_get_drvdata(pdev);
953
954 /* we need to stop anything touching our framebuffer */
955 fb_set_suspend(gfb->fb, 1);
956
957 /* seriously -- nobody is allowed to touch glamo memory when we
958 * are suspended or we lock on nWAIT
959 */
960 /* iounmap(gfb->fb->screen_base); */
961
962 return 0;
963 }
964
965 static int glamofb_resume(struct platform_device *pdev)
966 {
967 struct glamofb_handle *gfb = platform_get_drvdata(pdev);
968 struct glamofb_platform_data *mach_info = pdev->dev.platform_data;
969
970 /* OK let's allow framebuffer ops again */
971 /* gfb->fb->screen_base = ioremap(gfb->fb_res->start,
972 resource_size(gfb->fb_res)); */
973 glamo_engine_enable(mach_info->glamo, GLAMO_ENGINE_LCD);
974 glamo_engine_reset(mach_info->glamo, GLAMO_ENGINE_LCD);
975
976 printk(KERN_ERR"spin_lock_init\n");
977 spin_lock_init(&gfb->lock_cmd);
978 glamofb_init_regs(gfb);
979 #ifdef CONFIG_MFD_GLAMO_HWACCEL
980 glamofb_cursor_onoff(gfb, 1);
981 #endif
982
983 fb_set_suspend(gfb->fb, 0);
984
985 return 0;
986 }
987 #else
988 #define glamofb_suspend NULL
989 #define glamofb_resume NULL
990 #endif
991
992 static struct platform_driver glamofb_driver = {
993 .probe = glamofb_probe,
994 .remove = glamofb_remove,
995 .suspend = glamofb_suspend,
996 .resume = glamofb_resume,
997 .driver = {
998 .name = "glamo-fb",
999 .owner = THIS_MODULE,
1000 },
1001 };
1002
1003 static int __devinit glamofb_init(void)
1004 {
1005 return platform_driver_register(&glamofb_driver);
1006 }
1007
1008 static void __exit glamofb_cleanup(void)
1009 {
1010 platform_driver_unregister(&glamofb_driver);
1011 }
1012
1013 module_init(glamofb_init);
1014 module_exit(glamofb_cleanup);
1015
1016 MODULE_AUTHOR("Harald Welte <laforge@openmoko.org>");
1017 MODULE_DESCRIPTION("Smedia Glamo 336x/337x framebuffer driver");
1018 MODULE_LICENSE("GPL");