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