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