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