[ARM] msm_fb: Fix framebuffer console
[htc-msm-2-6-32:leviathan-incoming.git] / drivers / video / msm / msm_fb.c
1 /* drivers/video/msm/msm_fb.c
2  *
3  * Core MSM framebuffer driver.
4  *
5  * Copyright (C) 2007 Google Incorporated
6  *
7  * This software is licensed under the terms of the GNU General Public
8  * License version 2, as published by the Free Software Foundation, and
9  * may be copied, distributed, and modified under those terms.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  */
16
17 #include <linux/platform_device.h>
18 #include <linux/module.h>
19 #include <linux/fb.h>
20 #include <linux/delay.h>
21
22 #include <linux/freezer.h>
23 #include <linux/wait.h>
24 #include <linux/wakelock.h>
25 #include <linux/earlysuspend.h>
26 #include <linux/msm_mdp.h>
27 #include <linux/io.h>
28 #include <linux/uaccess.h>
29 #include <mach/msm_fb.h>
30 #include <mach/board.h>
31 #include <linux/workqueue.h>
32 #include <linux/clk.h>
33 #include <linux/debugfs.h>
34 #include <linux/dma-mapping.h>
35
36 #define MSMFB_DEBUG 1
37 #ifdef CONFIG_FB_MSM_LOGO
38 #define INIT_IMAGE_FILE "/logo.rle"
39 extern int load_565rle_image(char *filename);
40 #endif
41
42 #define PRINT_FPS 0
43 #define PRINT_BLIT_TIME 0
44
45 #define SLEEPING 0x4
46 #define UPDATING 0x3
47 #define FULL_UPDATE_DONE 0x2
48 #define WAKING 0x1
49 #define AWAKE 0x0
50
51 #define NONE 0
52 #define SUSPEND_RESUME 0x1
53 #define FPS 0x2
54 #define BLIT_TIME 0x4
55 #define SHOW_UPDATES 0x8
56
57 #define DLOG(mask, fmt, args...) \
58 do { \
59         if (msmfb_debug_mask & mask) \
60                 printk(KERN_INFO "msmfb: "fmt, ##args); \
61 } while (0)
62
63 static int msmfb_debug_mask;
64 module_param_named(msmfb_debug_mask, msmfb_debug_mask, int,
65                    S_IRUGO | S_IWUSR | S_IWGRP);
66
67 struct mdp_device *mdp;
68
69 struct msmfb_info {
70         struct fb_info *fb;
71         struct msm_panel_data *panel;
72         int xres;
73         int yres;
74         unsigned output_format;
75         unsigned yoffset;
76         unsigned frame_requested;
77         unsigned frame_done;
78         int sleeping;
79         unsigned update_frame;
80         struct {
81                 int left;
82                 int top;
83                 int eright; /* exclusive */
84                 int ebottom; /* exclusive */
85         } update_info;
86         char *black;
87
88         struct early_suspend earlier_suspend;
89         struct early_suspend early_suspend;
90         struct wake_lock idle_lock;
91         spinlock_t update_lock;
92         struct mutex panel_init_lock;
93         wait_queue_head_t frame_wq;
94         struct workqueue_struct *resume_workqueue;
95         struct work_struct resume_work;
96         struct msmfb_callback dma_callback;
97         struct msmfb_callback vsync_callback;
98         struct hrtimer fake_vsync;
99         ktime_t vsync_request_time;
100 };
101
102 static int msmfb_open(struct fb_info *info, int user)
103 {
104         return 0;
105 }
106
107 static int msmfb_release(struct fb_info *info, int user)
108 {
109         return 0;
110 }
111
112 /* Called from dma interrupt handler, must not sleep */
113 static void msmfb_handle_dma_interrupt(struct msmfb_callback *callback)
114 {
115         unsigned long irq_flags;
116         struct msmfb_info *msmfb  = container_of(callback, struct msmfb_info,
117                                                dma_callback);
118 #if PRINT_FPS
119         int64_t dt;
120         ktime_t now;
121         static int64_t frame_count;
122         static ktime_t last_sec;
123 #endif
124
125         spin_lock_irqsave(&msmfb->update_lock, irq_flags);
126         msmfb->frame_done = msmfb->frame_requested;
127         if (msmfb->sleeping == UPDATING &&
128             msmfb->frame_done == msmfb->update_frame) {
129                 DLOG(SUSPEND_RESUME, "full update completed\n");
130                 queue_work(msmfb->resume_workqueue, &msmfb->resume_work);
131         }
132 #if PRINT_FPS
133         now = ktime_get();
134         dt = ktime_to_ns(ktime_sub(now, last_sec));
135         frame_count++;
136         if (dt > NSEC_PER_SEC) {
137                 int64_t fps = frame_count * NSEC_PER_SEC * 100;
138                 frame_count = 0;
139                 last_sec = ktime_get();
140                 do_div(fps, dt);
141                 DLOG(FPS, "fps * 100: %llu\n", fps);
142         }
143 #endif
144         spin_unlock_irqrestore(&msmfb->update_lock, irq_flags);
145         wake_up(&msmfb->frame_wq);
146 }
147
148 static int msmfb_start_dma(struct msmfb_info *msmfb)
149 {
150         uint32_t x, y, w, h;
151         unsigned addr;
152         unsigned long irq_flags;
153         uint32_t yoffset;
154         s64 time_since_request;
155         struct msm_panel_data *panel = msmfb->panel;
156
157         spin_lock_irqsave(&msmfb->update_lock, irq_flags);
158         time_since_request = ktime_to_ns(ktime_sub(ktime_get(),
159                              msmfb->vsync_request_time));
160         if (time_since_request > 20 * NSEC_PER_MSEC) {
161                 uint32_t us;
162                 us = do_div(time_since_request, NSEC_PER_MSEC) / NSEC_PER_USEC;
163                 printk(KERN_WARNING "msmfb_start_dma %lld.%03u ms after vsync "
164                         "request\n", time_since_request, us);
165         }
166         if (msmfb->frame_done == msmfb->frame_requested) {
167                 spin_unlock_irqrestore(&msmfb->update_lock, irq_flags);
168                 return -1;
169         }
170         if (msmfb->sleeping == SLEEPING) {
171                 DLOG(SUSPEND_RESUME, "tried to start dma while asleep\n");
172                 spin_unlock_irqrestore(&msmfb->update_lock, irq_flags);
173                 return -1;
174         }
175         x = msmfb->update_info.left;
176         y = msmfb->update_info.top;
177         w = msmfb->update_info.eright - x;
178         h = msmfb->update_info.ebottom - y;
179         yoffset = msmfb->yoffset;
180         msmfb->update_info.left = msmfb->xres + 1;
181         msmfb->update_info.top = msmfb->yres + 1;
182         msmfb->update_info.eright = 0;
183         msmfb->update_info.ebottom = 0;
184         if (unlikely(w > msmfb->xres || h > msmfb->yres ||
185                      w == 0 || h == 0)) {
186                 printk(KERN_INFO "invalid update: %d %d %d "
187                                 "%d\n", x, y, w, h);
188                 msmfb->frame_done = msmfb->frame_requested;
189                 goto error;
190         }
191         spin_unlock_irqrestore(&msmfb->update_lock, irq_flags);
192
193         addr = ((msmfb->xres * (yoffset + y) + x) * 2);
194         mdp->dma(mdp, addr + msmfb->fb->fix.smem_start,
195                  msmfb->xres * 2, w, h, x, y, &msmfb->dma_callback,
196                  panel->interface_type);
197         return 0;
198 error:
199         spin_unlock_irqrestore(&msmfb->update_lock, irq_flags);
200         /* some clients need to clear their vsync interrupt */
201         if (panel->clear_vsync)
202                 panel->clear_vsync(panel);
203         wake_up(&msmfb->frame_wq);
204         return 0;
205 }
206
207 /* Called from esync interrupt handler, must not sleep */
208 static void msmfb_handle_vsync_interrupt(struct msmfb_callback *callback)
209 {
210         struct msmfb_info *msmfb = container_of(callback, struct msmfb_info,
211                                                vsync_callback);
212         wake_unlock(&msmfb->idle_lock);
213         msmfb_start_dma(msmfb);
214 }
215
216 static enum hrtimer_restart msmfb_fake_vsync(struct hrtimer *timer)
217 {
218         struct msmfb_info *msmfb  = container_of(timer, struct msmfb_info,
219                                                fake_vsync);
220         msmfb_start_dma(msmfb);
221         return HRTIMER_NORESTART;
222 }
223
224 static void msmfb_pan_update(struct fb_info *info, uint32_t left, uint32_t top,
225                              uint32_t eright, uint32_t ebottom,
226                              uint32_t yoffset, int pan_display)
227 {
228         struct msmfb_info *msmfb = info->par;
229         struct msm_panel_data *panel = msmfb->panel;
230         unsigned long irq_flags;
231         int sleeping;
232         int retry = 1;
233 #if PRINT_FPS
234         ktime_t t1, t2;
235         static uint64_t pans;
236         static uint64_t dt;
237         t1 = ktime_get();
238 #endif
239
240         DLOG(SHOW_UPDATES, "update %d %d %d %d %d %d\n",
241                 left, top, eright, ebottom, yoffset, pan_display);
242 restart:
243         spin_lock_irqsave(&msmfb->update_lock, irq_flags);
244
245         /* if we are sleeping, on a pan_display wait 10ms (to throttle back
246          * drawing otherwise return */
247         if (msmfb->sleeping == SLEEPING) {
248                 DLOG(SUSPEND_RESUME, "drawing while asleep\n");
249                 spin_unlock_irqrestore(&msmfb->update_lock, irq_flags);
250                 if (pan_display)
251                         wait_event_interruptible_timeout(msmfb->frame_wq,
252                                 msmfb->sleeping != SLEEPING, HZ/10);
253                 return;
254         }
255
256         sleeping = msmfb->sleeping;
257         /* on a full update, if the last frame has not completed, wait for it */
258         if ((pan_display && msmfb->frame_requested != msmfb->frame_done) ||
259                             sleeping == UPDATING) {
260                 int ret;
261                 spin_unlock_irqrestore(&msmfb->update_lock, irq_flags);
262                 ret = wait_event_interruptible_timeout(msmfb->frame_wq,
263                         msmfb->frame_done == msmfb->frame_requested &&
264                         msmfb->sleeping != UPDATING, 5 * HZ);
265                 if (ret <= 0 && (msmfb->frame_requested != msmfb->frame_done ||
266                                  msmfb->sleeping == UPDATING)) {
267                         if (retry && panel->request_vsync &&
268                             (sleeping == AWAKE)) {
269                                 wake_lock_timeout(&msmfb->idle_lock, HZ/4);
270                                 panel->request_vsync(panel,
271                                         &msmfb->vsync_callback);
272                                 retry = 0;
273                                 printk(KERN_WARNING "msmfb_pan_display timeout "
274                                         "rerequest vsync\n");
275                         } else {
276                                 printk(KERN_WARNING "msmfb_pan_display timeout "
277                                         "waiting for frame start, %d %d\n",
278                                         msmfb->frame_requested,
279                                         msmfb->frame_done);
280                                 return;
281                         }
282                 }
283                 goto restart;
284         }
285
286 #if PRINT_FPS
287         t2 = ktime_get();
288         if (pan_display) {
289                 uint64_t temp = ktime_to_ns(ktime_sub(t2, t1));
290                 do_div(temp, 1000);
291                 dt += temp;
292                 pans++;
293                 if (pans > 1000) {
294                         do_div(dt, pans);
295                         DLOG(FPS, "ave_wait_time: %lld\n", dt);
296                         dt = 0;
297                         pans = 0;
298                 }
299         }
300 #endif
301
302         msmfb->frame_requested++;
303         /* if necessary, update the y offset, if this is the
304          * first full update on resume, set the sleeping state */
305         if (pan_display) {
306                 msmfb->yoffset = yoffset;
307                 if (left == 0 && top == 0 && eright == info->var.xres &&
308                     ebottom == info->var.yres) {
309                         if (sleeping == WAKING) {
310                                 msmfb->update_frame = msmfb->frame_requested;
311                                 DLOG(SUSPEND_RESUME, "full update starting\n");
312                                 msmfb->sleeping = UPDATING;
313                         }
314                 }
315         }
316
317         /* set the update request */
318         if (left < msmfb->update_info.left)
319                 msmfb->update_info.left = left;
320         if (top < msmfb->update_info.top)
321                 msmfb->update_info.top = top;
322         if (eright > msmfb->update_info.eright)
323                 msmfb->update_info.eright = eright;
324         if (ebottom > msmfb->update_info.ebottom)
325                 msmfb->update_info.ebottom = ebottom;
326         DLOG(SHOW_UPDATES, "update queued %d %d %d %d %d\n",
327                 msmfb->update_info.left, msmfb->update_info.top,
328                 msmfb->update_info.eright, msmfb->update_info.ebottom,
329                 msmfb->yoffset);
330         spin_unlock_irqrestore(&msmfb->update_lock, irq_flags);
331
332         /* if the panel is all the way on wait for vsync, otherwise sleep
333          * for 16 ms (long enough for the dma to panel) and then begin dma */
334         msmfb->vsync_request_time = ktime_get();
335         if (panel->request_vsync && (sleeping == AWAKE)) {
336                 wake_lock_timeout(&msmfb->idle_lock, HZ/4);
337                 panel->request_vsync(panel, &msmfb->vsync_callback);
338         } else {
339                 if (!hrtimer_active(&msmfb->fake_vsync)) {
340                         hrtimer_start(&msmfb->fake_vsync,
341                                       ktime_set(0, NSEC_PER_SEC/60),
342                                       HRTIMER_MODE_REL);
343                 }
344         }
345 }
346
347 static void msmfb_update(struct fb_info *info, uint32_t left, uint32_t top,
348                          uint32_t eright, uint32_t ebottom)
349 {
350         msmfb_pan_update(info, left, top, eright, ebottom, 0, 0);
351 }
352
353 static void power_on_panel(struct work_struct *work)
354 {
355         struct msmfb_info *msmfb =
356                 container_of(work, struct msmfb_info, resume_work);
357         struct msm_panel_data *panel = msmfb->panel;
358         unsigned long irq_flags;
359
360         mutex_lock(&msmfb->panel_init_lock);
361         DLOG(SUSPEND_RESUME, "turning on panel\n");
362         if (msmfb->sleeping == UPDATING) {
363                 wake_lock_timeout(&msmfb->idle_lock, HZ);
364                 if (panel->unblank(panel)) {
365                         printk(KERN_INFO "msmfb: panel unblank failed,"
366                                "not starting drawing\n");
367                         goto error;
368                 }
369                 wake_unlock(&msmfb->idle_lock);
370                 spin_lock_irqsave(&msmfb->update_lock, irq_flags);
371                 msmfb->sleeping = AWAKE;
372                 wake_up(&msmfb->frame_wq);
373                 spin_unlock_irqrestore(&msmfb->update_lock, irq_flags);
374         }
375 error:
376         mutex_unlock(&msmfb->panel_init_lock);
377 }
378
379 #ifdef CONFIG_HAS_EARLYSUSPEND
380 /* turn off the panel */
381 static void msmfb_earlier_suspend(struct early_suspend *h)
382 {
383         struct msmfb_info *msmfb = container_of(h, struct msmfb_info,
384                                                 earlier_suspend);
385         struct msm_panel_data *panel = msmfb->panel;
386         unsigned long irq_flags;
387
388         mutex_lock(&msmfb->panel_init_lock);
389         msmfb->sleeping = SLEEPING;
390         wake_up(&msmfb->frame_wq);
391         spin_lock_irqsave(&msmfb->update_lock, irq_flags);
392         spin_unlock_irqrestore(&msmfb->update_lock, irq_flags);
393         wait_event_timeout(msmfb->frame_wq,
394                            msmfb->frame_requested == msmfb->frame_done, HZ/10);
395
396         mdp->dma(mdp, virt_to_phys(msmfb->black), 0,
397                  msmfb->fb->var.xres, msmfb->fb->var.yres, 0, 0,
398                  NULL, panel->interface_type);
399         mdp->dma_wait(mdp);
400
401         /* turn off the panel */
402         panel->blank(panel);
403 }
404
405 static void msmfb_suspend(struct early_suspend *h)
406 {
407         struct msmfb_info *msmfb = container_of(h, struct msmfb_info,
408                                                 early_suspend);
409         struct msm_panel_data *panel = msmfb->panel;
410         /* suspend the panel */
411         panel->suspend(panel);
412         mutex_unlock(&msmfb->panel_init_lock);
413 }
414
415 static void msmfb_resume(struct early_suspend *h)
416 {
417         struct msmfb_info *msmfb = container_of(h, struct msmfb_info,
418                                                 early_suspend);
419         struct msm_panel_data *panel = msmfb->panel;
420         unsigned long irq_flags;
421
422         if (panel->resume(panel)) {
423                 printk(KERN_INFO "msmfb: panel resume failed, not resuming "
424                        "fb\n");
425                 return;
426         }
427         spin_lock_irqsave(&msmfb->update_lock, irq_flags);
428         msmfb->frame_requested = msmfb->frame_done = msmfb->update_frame = 0;
429         msmfb->sleeping = WAKING;
430         DLOG(SUSPEND_RESUME, "ready, waiting for full update\n");
431         spin_unlock_irqrestore(&msmfb->update_lock, irq_flags);
432 }
433 #endif
434
435 static int msmfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
436 {
437         if ((var->xres != info->var.xres) ||
438             (var->yres != info->var.yres) ||
439             (var->xres_virtual != info->var.xres_virtual) ||
440             (var->yres_virtual != info->var.yres_virtual) ||
441             (var->xoffset != info->var.xoffset) ||
442             (var->bits_per_pixel != info->var.bits_per_pixel) ||
443             (var->grayscale != info->var.grayscale))
444                  return -EINVAL;
445         return 0;
446 }
447
448 int msmfb_pan_display(struct fb_var_screeninfo *var, struct fb_info *info)
449 {
450         struct msmfb_info *msmfb = info->par;
451         struct msm_panel_data *panel = msmfb->panel;
452
453         /* "UPDT" */
454         if ((panel->caps & MSMFB_CAP_PARTIAL_UPDATES) &&
455             (var->reserved[0] == 0x54445055)) {
456 #if 0
457                 printk(KERN_INFO "pan frame %d-%d, rect %d %d %d %d\n",
458                        msmfb->frame_requested, msmfb->frame_done,
459                        var->reserved[1] & 0xffff,
460                        var->reserved[1] >> 16, var->reserved[2] & 0xffff,
461                        var->reserved[2] >> 16);
462 #endif
463                 msmfb_pan_update(info, var->reserved[1] & 0xffff,
464                                  var->reserved[1] >> 16,
465                                  var->reserved[2] & 0xffff,
466                                  var->reserved[2] >> 16, var->yoffset, 1);
467         } else {
468                 msmfb_pan_update(info, 0, 0, info->var.xres, info->var.yres,
469                                  var->yoffset, 1);
470         }
471         return 0;
472 }
473
474 static void msmfb_fillrect(struct fb_info *p, const struct fb_fillrect *rect)
475 {
476         cfb_fillrect(p, rect);
477         msmfb_update(p, rect->dx, rect->dy, rect->dx + rect->width,
478                      rect->dy + rect->height);
479 }
480
481 static void msmfb_copyarea(struct fb_info *p, const struct fb_copyarea *area)
482 {
483         cfb_copyarea(p, area);
484         msmfb_update(p, area->dx, area->dy, area->dx + area->width,
485                      area->dy + area->height);
486 }
487
488 static void msmfb_imageblit(struct fb_info *p, const struct fb_image *image)
489 {
490         cfb_imageblit(p, image);
491         msmfb_update(p, image->dx, image->dy, image->dx + image->width,
492                      image->dy + image->height);
493 }
494
495
496 static int msmfb_blit(struct fb_info *info,
497                       void __user *p)
498 {
499         struct mdp_blit_req req;
500         struct mdp_blit_req_list req_list;
501         int i;
502         int ret;
503
504         if (copy_from_user(&req_list, p, sizeof(req_list)))
505                 return -EFAULT;
506
507         for (i = 0; i < req_list.count; i++) {
508                 struct mdp_blit_req_list *list =
509                         (struct mdp_blit_req_list *)p;
510                 if (copy_from_user(&req, &list->req[i], sizeof(req)))
511                         return -EFAULT;
512                 ret = mdp->blit(mdp, info, &req);
513                 if (ret)
514                         return ret;
515         }
516         return 0;
517 }
518
519
520 DEFINE_MUTEX(mdp_ppp_lock);
521
522 static int msmfb_ioctl(struct fb_info *p, unsigned int cmd, unsigned long arg)
523 {
524         void __user *argp = (void __user *)arg;
525         int ret;
526 #if PRINT_BLIT_TIME
527         ktime_t t1, t2;
528 #endif
529
530         switch (cmd) {
531         case MSMFB_GRP_DISP:
532                 mdp->set_grp_disp(mdp, arg);
533                 break;
534         case MSMFB_BLIT:
535 #if PRINT_BLIT_TIME
536                 t1 = ktime_get();
537 #endif
538                 ret = msmfb_blit(p, argp);
539                 if (ret)
540                         return ret;
541 #if PRINT_BLIT_TIME
542                 t2 = ktime_get();
543                 DLOG(BLIT_TIME, "total %lld\n",
544                        ktime_to_ns(t2) - ktime_to_ns(t1));
545 #endif
546                 break;
547         default:
548                         printk(KERN_INFO "msmfb unknown ioctl: %d\n", cmd);
549                         return -EINVAL;
550         }
551         return 0;
552 }
553
554 static struct fb_ops msmfb_ops = {
555         .owner = THIS_MODULE,
556         .fb_open = msmfb_open,
557         .fb_release = msmfb_release,
558         .fb_check_var = msmfb_check_var,
559         .fb_pan_display = msmfb_pan_display,
560         .fb_fillrect = msmfb_fillrect,
561         .fb_copyarea = msmfb_copyarea,
562         .fb_imageblit = msmfb_imageblit,
563         .fb_ioctl = msmfb_ioctl,
564 };
565
566 static unsigned PP[16];
567
568
569 #if MSMFB_DEBUG
570 static ssize_t debug_open(struct inode *inode, struct file *file)
571 {
572         file->private_data = inode->i_private;
573         return 0;
574 }
575
576
577 static ssize_t debug_read(struct file *file, char __user *buf, size_t count,
578                           loff_t *ppos)
579 {
580         const int debug_bufmax = 4096;
581         static char buffer[4096];
582         int n = 0;
583         struct msmfb_info *msmfb = (struct msmfb_info *)file->private_data;
584         unsigned long irq_flags;
585
586         spin_lock_irqsave(&msmfb->update_lock, irq_flags);
587         n = scnprintf(buffer, debug_bufmax, "yoffset %d\n", msmfb->yoffset);
588         n += scnprintf(buffer + n, debug_bufmax, "frame_requested %d\n",
589                        msmfb->frame_requested);
590         n += scnprintf(buffer + n, debug_bufmax, "frame_done %d\n",
591                        msmfb->frame_done);
592         n += scnprintf(buffer + n, debug_bufmax, "sleeping %d\n",
593                        msmfb->sleeping);
594         n += scnprintf(buffer + n, debug_bufmax, "update_frame %d\n",
595                        msmfb->update_frame);
596         spin_unlock_irqrestore(&msmfb->update_lock, irq_flags);
597         n++;
598         buffer[n] = 0;
599         return simple_read_from_buffer(buf, count, ppos, buffer, n);
600 }
601
602 static struct file_operations debug_fops = {
603         .read = debug_read,
604         .open = debug_open,
605 };
606 #endif
607
608 #define BITS_PER_PIXEL 16
609
610 static void setup_fb_info(struct msmfb_info *msmfb)
611 {
612         struct fb_info *fb_info = msmfb->fb;
613         int r;
614
615         /* finish setting up the fb_info struct */
616         strncpy(fb_info->fix.id, "msmfb", 16);
617         fb_info->fix.ypanstep = 1;
618
619         fb_info->fbops = &msmfb_ops;
620         fb_info->flags = FBINFO_DEFAULT;
621
622         fb_info->fix.type = FB_TYPE_PACKED_PIXELS;
623         fb_info->fix.visual = FB_VISUAL_TRUECOLOR;
624         fb_info->fix.line_length = msmfb->xres * 2;
625
626         fb_info->var.xres = msmfb->xres;
627         fb_info->var.yres = msmfb->yres;
628         fb_info->var.width = msmfb->panel->fb_data->width;
629         fb_info->var.height = msmfb->panel->fb_data->height;
630         fb_info->var.xres_virtual = msmfb->xres;
631         fb_info->var.yres_virtual = msmfb->yres * 2;
632         fb_info->var.bits_per_pixel = BITS_PER_PIXEL;
633         fb_info->var.accel_flags = 0;
634
635         fb_info->var.yoffset = 0;
636
637         if (msmfb->panel->caps & MSMFB_CAP_PARTIAL_UPDATES) {
638                 /* set the param in the fixed screen, so userspace can't
639                  * change it. This will be used to check for the
640                  * capability. */
641                 fb_info->fix.reserved[0] = 0x5444;
642                 fb_info->fix.reserved[1] = 0x5055;
643
644                 /* This preloads the value so that if userspace doesn't
645                  * change it, it will be a full update */
646                 fb_info->var.reserved[0] = 0x54445055;
647                 fb_info->var.reserved[1] = 0;
648                 fb_info->var.reserved[2] = (uint16_t)msmfb->xres |
649                                            ((uint32_t)msmfb->yres << 16);
650         }
651
652         fb_info->var.red.offset = 11;
653         fb_info->var.red.length = 5;
654         fb_info->var.red.msb_right = 0;
655         fb_info->var.green.offset = 5;
656         fb_info->var.green.length = 6;
657         fb_info->var.green.msb_right = 0;
658         fb_info->var.blue.offset = 0;
659         fb_info->var.blue.length = 5;
660         fb_info->var.blue.msb_right = 0;
661
662         r = fb_alloc_cmap(&fb_info->cmap, 16, 0);
663         fb_info->pseudo_palette = PP;
664
665         PP[0] = 0;
666         for (r = 1; r < 16; r++)
667                 PP[r] = 0xffffffff;
668 }
669
670 static int setup_fbmem(struct msmfb_info *msmfb, struct platform_device *pdev)
671 {
672         struct fb_info *fb = msmfb->fb;
673         struct resource *resource;
674         unsigned long size = msmfb->xres * msmfb->yres *
675                              (BITS_PER_PIXEL >> 3) * 2;
676         unsigned char *fbram;
677
678         /* board file might have attached a resource describing an fb */
679         resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
680         if (!resource)
681                 return -EINVAL;
682
683         /* check the resource is large enough to fit the fb */
684         if (resource->end - resource->start < size) {
685                 printk(KERN_ERR "allocated resource is too small for "
686                                 "fb\n");
687                 return -ENOMEM;
688         }
689         fb->fix.smem_start = resource->start;
690         fb->fix.smem_len = resource->end - resource->start;
691         fbram = ioremap(resource->start,
692                         resource->end - resource->start);
693         if (fbram == 0) {
694                 printk(KERN_ERR "msmfb: cannot allocate fbram!\n");
695                 return -ENOMEM;
696         }
697         fb->screen_base = fbram;
698         return 0;
699 }
700
701 static int msmfb_probe(struct platform_device *pdev)
702 {
703         struct fb_info *fb;
704         struct msmfb_info *msmfb;
705         struct msm_panel_data *panel = pdev->dev.platform_data;
706         int ret;
707
708         if (!panel) {
709                 pr_err("msmfb_probe: no platform data\n");
710                 return -EINVAL;
711         }
712         if (!panel->fb_data) {
713                 pr_err("msmfb_probe: no fb_data\n");
714                 return -EINVAL;
715         }
716
717         fb = framebuffer_alloc(sizeof(struct msmfb_info), &pdev->dev);
718         if (!fb)
719                 return -ENOMEM;
720         msmfb = fb->par;
721         msmfb->fb = fb;
722         msmfb->panel = panel;
723         msmfb->xres = panel->fb_data->xres;
724         msmfb->yres = panel->fb_data->yres;
725
726         ret = setup_fbmem(msmfb, pdev);
727         if (ret)
728                 goto error_setup_fbmem;
729
730         setup_fb_info(msmfb);
731
732         spin_lock_init(&msmfb->update_lock);
733         mutex_init(&msmfb->panel_init_lock);
734         init_waitqueue_head(&msmfb->frame_wq);
735         msmfb->resume_workqueue = create_workqueue("panel_on");
736         if (msmfb->resume_workqueue == NULL) {
737                 printk(KERN_ERR "failed to create panel_on workqueue\n");
738                 ret = -ENOMEM;
739                 goto error_create_workqueue;
740         }
741         INIT_WORK(&msmfb->resume_work, power_on_panel);
742         msmfb->black = kzalloc(msmfb->fb->var.bits_per_pixel*msmfb->xres,
743                                GFP_KERNEL);
744
745         wake_lock_init(&msmfb->idle_lock, WAKE_LOCK_IDLE, "msmfb_idle_lock");
746
747 #ifdef CONFIG_HAS_EARLYSUSPEND
748         msmfb->early_suspend.suspend = msmfb_suspend;
749         msmfb->early_suspend.resume = msmfb_resume;
750         msmfb->early_suspend.level = EARLY_SUSPEND_LEVEL_DISABLE_FB;
751         register_early_suspend(&msmfb->early_suspend);
752
753         msmfb->earlier_suspend.suspend = msmfb_earlier_suspend;
754         msmfb->earlier_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN;
755         register_early_suspend(&msmfb->earlier_suspend);
756 #endif
757
758 #if MSMFB_DEBUG
759         debugfs_create_file("msm_fb", S_IFREG | S_IRUGO, NULL,
760                             (void *)fb->par, &debug_fops);
761 #endif
762
763         printk(KERN_INFO "msmfb_probe() installing %d x %d panel\n",
764                msmfb->xres, msmfb->yres);
765
766         msmfb->dma_callback.func = msmfb_handle_dma_interrupt;
767         msmfb->vsync_callback.func = msmfb_handle_vsync_interrupt;
768         hrtimer_init(&msmfb->fake_vsync, CLOCK_MONOTONIC,
769                      HRTIMER_MODE_REL);
770
771
772         msmfb->fake_vsync.function = msmfb_fake_vsync;
773
774         ret = register_framebuffer(fb);
775         if (ret)
776                 goto error_register_framebuffer;
777
778         msmfb->sleeping = WAKING;
779
780 #ifdef CONFIG_FB_MSM_LOGO
781         if (!load_565rle_image(INIT_IMAGE_FILE)) {
782                 /* Flip buffer */
783                 msmfb->update_info.left = 0;
784                 msmfb->update_info.top = 0;
785                 msmfb->update_info.eright = info->var.xres;
786                 msmfb->update_info.ebottom = info->var.yres;
787                 msmfb_pan_update(info, 0, 0, fb->var.xres,
788                                  fb->var.yres, 0, 1);
789         }
790 #endif
791         return 0;
792
793 error_register_framebuffer:
794         wake_lock_destroy(&msmfb->idle_lock);
795         destroy_workqueue(msmfb->resume_workqueue);
796 error_create_workqueue:
797         iounmap(fb->screen_base);
798 error_setup_fbmem:
799         framebuffer_release(msmfb->fb);
800         return ret;
801 }
802
803 static struct platform_driver msm_panel_driver = {
804         /* need to write remove */
805         .probe = msmfb_probe,
806         .driver = {.name = "msm_panel"},
807 };
808
809
810 static int msmfb_add_mdp_device(struct device *dev,
811                                 struct class_interface *class_intf)
812 {
813         /* might need locking if mulitple mdp devices */
814         if (mdp)
815                 return 0;
816         mdp = container_of(dev, struct mdp_device, dev);
817         return platform_driver_register(&msm_panel_driver);
818 }
819
820 static void msmfb_remove_mdp_device(struct device *dev,
821                                 struct class_interface *class_intf)
822 {
823         /* might need locking if mulitple mdp devices */
824         if (dev != &mdp->dev)
825                 return;
826         platform_driver_unregister(&msm_panel_driver);
827         mdp = NULL;
828 }
829
830 static struct class_interface msm_fb_interface = {
831         .add_dev = &msmfb_add_mdp_device,
832         .remove_dev = &msmfb_remove_mdp_device,
833 };
834
835 static int __init msmfb_init(void)
836 {
837         return register_mdp_client(&msm_fb_interface);
838 }
839
840 module_init(msmfb_init);