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