Do not switch the screen off when changing mode
[kernel.git] / drivers / mfd / glamo / glamo-display.c
1 /*
2  * SMedia Glamo 336x/337x display
3  *
4  * Copyright (C) 2008-2009 Thomas White <taw@bitwiz.org.uk>
5  *
6  * Based on glamo-fb.c (C) 2007-2008 by Openmoko, Inc.
7  * Author: Harald Welte <laforge@openmoko.org>
8  * All rights reserved.
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License as
12  * published by the Free Software Foundation; either version 2 of
13  * the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
23  * MA 02111-1307 USA
24  *
25  *
26  * Based on intel_display.c and intel_crt.c from drivers/gpu/drm/i915
27  *  to which the following licence applies:
28  *
29  * Copyright © 2006-2007 Intel Corporation
30  *
31  * Permission is hereby granted, free of charge, to any person obtaining a
32  * copy of this software and associated documentation files (the "Software"),
33  * to deal in the Software without restriction, including without limitation
34  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
35  * and/or sell copies of the Software, and to permit persons to whom the
36  * Software is furnished to do so, subject to the following conditions:
37  *
38  * The above copyright notice and this permission notice (including the next
39  * paragraph) shall be included in all copies or substantial portions of the
40  * Software.
41  *
42  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
43  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
44  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
45  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
46  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
47  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
48  * DEALINGS IN THE SOFTWARE.
49  *
50  * Authors:
51  *      Eric Anholt <eric@anholt.net>
52  *
53  */
54
55 #define DEBUG 1
56
57 #include <drm/drmP.h>
58 #include <drm/glamo_drm.h>
59 #include <drm/drm_crtc_helper.h>
60 #include <drm/drm_crtc.h>
61
62 #include "glamo-core.h"
63 #include "glamo-drm-private.h"
64 #include "glamo-regs.h"
65 #include "glamo-kms-fb.h"
66 #include <linux/glamofb.h>
67
68
69 #define GLAMO_LCD_WIDTH_MASK 0x03FF
70 #define GLAMO_LCD_HEIGHT_MASK 0x03FF
71 #define GLAMO_LCD_PITCH_MASK 0x07FE
72 #define GLAMO_LCD_HV_TOTAL_MASK 0x03FF
73 #define GLAMO_LCD_HV_RETR_START_MASK 0x03FF
74 #define GLAMO_LCD_HV_RETR_END_MASK 0x03FF
75 #define GLAMO_LCD_HV_RETR_DISP_START_MASK 0x03FF
76 #define GLAMO_LCD_HV_RETR_DISP_END_MASK 0x03FF
77
78
79 struct glamofb_par {
80         struct drm_device *dev;
81         struct drm_display_mode *our_mode;
82         struct glamo_framebuffer *glamo_fb;
83         int crtc_count;
84         /* crtc currently bound to this */
85         uint32_t crtc_ids[2];
86 };
87
88
89 static int reg_read_lcd(struct glamodrm_handle *gdrm, u_int16_t reg)
90 {
91         int i = 0;
92
93         for (i = 0; i != 2; i++)
94                 nop();
95
96         return ioread16(gdrm->lcd_base + reg);
97 }
98
99
100 static void reg_write_lcd(struct glamodrm_handle *gdrm,
101                           u_int16_t reg, u_int16_t val)
102 {
103         int i = 0;
104
105         for (i = 0; i != 2; i++)
106                 nop();
107         
108         iowrite16(val, gdrm->lcd_base + reg);
109 }
110
111
112 static void reg_set_bit_mask_lcd(struct glamodrm_handle *gdrm,
113                                  u_int16_t reg, u_int16_t mask,
114                                  u_int16_t val)
115 {
116         u_int16_t tmp;
117
118         val &= mask;
119
120         tmp = reg_read_lcd(gdrm, reg);
121         tmp &= ~mask;
122         tmp |= val;
123         reg_write_lcd(gdrm, reg, tmp);
124 }
125
126
127 /* Note that this has nothing at all to do with the engine command queue
128  * in glamo-cmdq.c */
129 static inline int glamo_lcd_cmdq_empty(struct glamodrm_handle *gdrm)
130 {
131         /* DGCMdQempty -- 1 == command queue is empty */
132         return reg_read_lcd(gdrm, GLAMO_REG_LCD_STATUS1) & (1 << 15);
133 }
134
135
136 /* call holding gfb->lock_cmd  when locking, until you unlock */
137 int glamo_lcd_cmd_mode(struct glamodrm_handle *gdrm, int on)
138 {
139         int timeout = 2000000;
140
141         if (gdrm->glamo_core->suspending) {
142                 dev_err(&gdrm->glamo_core->pdev->dev,
143                                 "IGNORING glamofb_cmd_mode while"
144                                 " suspended\n");
145                 return -EBUSY;
146         }
147
148         dev_dbg(gdrm->dev, "glamofb_cmd_mode(on=%d)\n", on);
149         if (on) {
150
151                 while ((!glamo_lcd_cmdq_empty(gdrm)) && (timeout--))
152                         /* yield() */;
153                 if (timeout < 0) {
154                         printk(KERN_ERR "*************"
155                                         " LCD command queue never got empty "
156                                         "*************\n");
157                         return -EIO;
158                 }
159
160                 /* display the entire frame then switch to command */
161                 reg_write_lcd(gdrm, GLAMO_REG_LCD_COMMAND1,
162                           GLAMO_LCD_CMD_TYPE_DISP |
163                           GLAMO_LCD_CMD_DATA_FIRE_VSYNC);
164
165                 /* wait until lcd idle */
166                 timeout = 2000000;
167                 while ((!reg_read_lcd(gdrm, GLAMO_REG_LCD_STATUS2) & (1 << 12))
168                           && (timeout--))
169                         /* yield() */;
170                 if (timeout < 0) {
171                         printk(KERN_ERR"*************"
172                                        " LCD never idle "
173                                        "*************\n");
174                         return -EIO;
175                 }
176
177                 mdelay(100);
178
179         } else {
180                 /* RGB interface needs vsync/hsync */
181                 int mode;
182                 mode = reg_read_lcd(gdrm, GLAMO_REG_LCD_MODE3);
183                 if ( mode & GLAMO_LCD_MODE3_RGB)
184                         reg_write_lcd(gdrm, GLAMO_REG_LCD_COMMAND1,
185                                   GLAMO_LCD_CMD_TYPE_DISP |
186                                   GLAMO_LCD_CMD_DATA_DISP_SYNC);
187
188                 reg_write_lcd(gdrm, GLAMO_REG_LCD_COMMAND1,
189                           GLAMO_LCD_CMD_TYPE_DISP |
190                           GLAMO_LCD_CMD_DATA_DISP_FIRE);
191         }
192
193         return 0;
194 }
195
196
197 static struct glamo_script lcd_init_script[] = {
198         { GLAMO_REG_LCD_MODE1, 0x0020 },
199         /* no display rotation, no hardware cursor, no dither, no gamma,
200          * no retrace flip, vsync low-active, hsync low active,
201          * no TVCLK, no partial display, hw dest color from fb,
202          * no partial display mode, LCD1, software flip,  */
203         { GLAMO_REG_LCD_MODE2, 0x9020 },
204           /* video flip, no ptr, no ptr, dhclk off,
205            * normal mode,  no cpuif,
206            * res, serial msb first, single fb, no fr ctrl,
207            * cpu if bits all zero, no crc
208            * 0000 0000 0010  0000 */
209         { GLAMO_REG_LCD_MODE3, 0x0b40 },
210           /* src data rgb565, res, 18bit rgb666
211            * 000 01 011 0100 0000 */
212         { GLAMO_REG_LCD_POLARITY, 0x440c },
213           /* DE high active, no cpu/lcd if, cs0 force low, a0 low active,
214            * np cpu if, 9bit serial data, sclk rising edge latch data
215            * 01 00 0 100 0 000 01 0 0 */
216         /* The following values assume 640*480@16bpp */
217         { GLAMO_REG_LCD_A_BASE1, 0x0000 }, /* display A base address 15:0 */
218         { GLAMO_REG_LCD_A_BASE2, 0x0000 }, /* display A base address 22:16 */
219         { GLAMO_REG_LCD_B_BASE1, 0x6000 }, /* display B base address 15:0 */
220         { GLAMO_REG_LCD_B_BASE2, 0x0009 }, /* display B base address 22:16 */
221         { GLAMO_REG_LCD_CURSOR_BASE1, 0xC000 }, /* cursor base address 15:0 */
222         { GLAMO_REG_LCD_CURSOR_BASE2, 0x0012 }, /* cursor base address 22:16 */
223         { GLAMO_REG_LCD_COMMAND2, 0x0000 }, /* display page A */
224 };
225
226
227 static int glamo_run_lcd_script(struct glamodrm_handle *gdrm,
228                                 struct glamo_script *script, int len)
229 {
230         int i;
231
232         for (i = 0; i < len; i++) {
233                 struct glamo_script *line = &script[i];
234
235                 if (line->reg == 0xffff)
236                         return 0;
237                 else if (line->reg == 0xfffe)
238                         msleep(line->val);
239                 else
240                         reg_write_lcd(gdrm, script[i].reg, script[i].val);
241         }
242
243         return 0;
244 }
245
246
247 #if 0
248 static void notify_blank(struct drm_crtc *crtc, int mode)
249 {
250         struct fb_event event;
251
252         event.info = info;
253         event.data = &blank_mode;
254         fb_notifier_call_chain(FB_EVENT_CONBLANK, &event);
255 }
256 #endif
257
258
259 extern void jbt6k74_action(int val);
260
261
262 /* Power on/off */
263 static void glamo_crtc_dpms(struct drm_crtc *crtc, int mode)
264 {
265         struct glamodrm_handle *gdrm;
266         struct glamo_crtc *glamo_crtc = to_glamo_crtc(crtc);
267
268         printk(KERN_CRIT "glamo_crtc_dpms(%u)\n", mode);
269         gdrm = glamo_crtc->gdrm;
270
271         switch (mode) {
272         case DRM_MODE_DPMS_OFF:
273                 /* LCM need notification before pixel clock is stopped */
274                 //jbt6k74_action(0);
275
276                 /* disable the pixel clock */
277                 glamo_engine_clkreg_set(gdrm->glamo_core, GLAMO_ENGINE_LCD,
278                                         GLAMO_CLOCK_LCD_EN_DCLK, 0);
279                 glamo_crtc->blank_mode = mode;
280                 break;
281         case DRM_MODE_DPMS_ON:
282                 /* enable the pixel clock if off */
283                 if (glamo_crtc->blank_mode == DRM_MODE_DPMS_OFF)
284                         glamo_engine_clkreg_set(gdrm->glamo_core,
285                                                 GLAMO_ENGINE_LCD,
286                                                 GLAMO_CLOCK_LCD_EN_DCLK,
287                                                 GLAMO_CLOCK_LCD_EN_DCLK);
288
289                 //jbt6k74_action(1);
290                 glamo_crtc->blank_mode = mode;
291                 break;
292         }
293 }
294
295
296 static bool glamo_crtc_mode_fixup(struct drm_crtc *crtc,
297                                   struct drm_display_mode *mode,
298                                   struct drm_display_mode *adjusted_mode)
299 {
300         printk(KERN_CRIT "glamo_crtc_mode_fixup\n");
301         return true;
302 }
303
304
305 static void glamo_crtc_mode_set(struct drm_crtc *crtc,
306                                 struct drm_display_mode *mode,
307                                 struct drm_display_mode *adjusted_mode,
308                                 int x, int y,
309                                 struct drm_framebuffer *old_fb)
310 {
311         struct glamodrm_handle *gdrm;
312         struct glamo_crtc *gcrtc;
313         int retr_start, retr_end, disp_start, disp_end;
314
315         printk(KERN_CRIT "glamo_crtc_mode_set\n");
316
317         /* Dig out our handle */
318         gcrtc = to_glamo_crtc(crtc);
319         gdrm = gcrtc->gdrm;     /* Here it is! */
320
321         glamo_lcd_cmd_mode(gdrm, 1);
322         glamo_engine_reclock(gdrm->glamo_core, GLAMO_ENGINE_LCD, mode->clock);
323
324         reg_set_bit_mask_lcd(gdrm, GLAMO_REG_LCD_WIDTH,
325                              GLAMO_LCD_WIDTH_MASK, mode->hdisplay);
326         reg_set_bit_mask_lcd(gdrm, GLAMO_REG_LCD_HEIGHT,
327                              GLAMO_LCD_HEIGHT_MASK, mode->vdisplay);
328         reg_set_bit_mask_lcd(gdrm, GLAMO_REG_LCD_PITCH,
329                              GLAMO_LCD_PITCH_MASK, mode->hdisplay*2);
330
331         /* Convert "X modeline timings" into "Glamo timings" */
332         retr_start = 0;
333         retr_end = retr_start + mode->hsync_end - mode->hsync_start;
334         disp_start = mode->htotal - mode->hsync_start;
335         disp_end = disp_start + mode->hdisplay;
336         reg_set_bit_mask_lcd(gdrm, GLAMO_REG_LCD_HORIZ_TOTAL,
337                              GLAMO_LCD_HV_TOTAL_MASK, mode->htotal);
338         reg_set_bit_mask_lcd(gdrm, GLAMO_REG_LCD_HORIZ_RETR_START,
339                              GLAMO_LCD_HV_RETR_START_MASK, retr_start);
340         reg_set_bit_mask_lcd(gdrm, GLAMO_REG_LCD_HORIZ_RETR_END,
341                              GLAMO_LCD_HV_RETR_END_MASK, retr_end);
342         reg_set_bit_mask_lcd(gdrm, GLAMO_REG_LCD_HORIZ_DISP_START,
343                              GLAMO_LCD_HV_RETR_DISP_START_MASK, disp_start);
344         reg_set_bit_mask_lcd(gdrm, GLAMO_REG_LCD_HORIZ_DISP_END,
345                              GLAMO_LCD_HV_RETR_DISP_END_MASK, disp_end);
346
347         /* The same in the vertical direction */
348         retr_start = 0;
349         retr_end = retr_start + mode->vsync_end - mode->vsync_start;
350         disp_start = mode->vtotal - mode->vsync_start;
351         disp_end = disp_start + mode->vdisplay;
352         reg_set_bit_mask_lcd(gdrm, GLAMO_REG_LCD_VERT_TOTAL,
353                              GLAMO_LCD_HV_TOTAL_MASK, mode->vtotal);
354         reg_set_bit_mask_lcd(gdrm, GLAMO_REG_LCD_VERT_RETR_START,
355                              GLAMO_LCD_HV_RETR_START_MASK, retr_start);
356         reg_set_bit_mask_lcd(gdrm, GLAMO_REG_LCD_VERT_RETR_END,
357                              GLAMO_LCD_HV_RETR_END_MASK, retr_end);
358         reg_set_bit_mask_lcd(gdrm, GLAMO_REG_LCD_VERT_DISP_START,
359                              GLAMO_LCD_HV_RETR_DISP_START_MASK, disp_start);
360         reg_set_bit_mask_lcd(gdrm, GLAMO_REG_LCD_VERT_DISP_END,
361                              GLAMO_LCD_HV_RETR_DISP_END_MASK, disp_end);
362
363         glamo_lcd_cmd_mode(gdrm, 0);
364 }
365
366
367 static void glamo_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
368                                      struct drm_framebuffer *old_fb)
369 {
370         printk(KERN_CRIT "glamo_crtc_mode_set_base\n");
371 }
372
373
374 static void glamo_crtc_prepare(struct drm_crtc *crtc)
375 {
376         printk(KERN_CRIT "glamo_crtc_prepare\n");
377 }
378
379
380 static void glamo_crtc_commit(struct drm_crtc *crtc)
381 {
382         printk(KERN_CRIT "glamo_crtc_commit\n");
383 }
384
385
386 static int glamo_crtc_cursor_set(struct drm_crtc *crtc,
387                                  struct drm_file *file_priv,
388                                  uint32_t handle,
389                                  uint32_t width, uint32_t height)
390 {
391         printk(KERN_CRIT "glamo_crtc_cursor_set\n");
392         return 0;
393 }
394
395
396 static int glamo_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
397 {
398         printk(KERN_CRIT "glamo_crtc_cursor_move\n");
399         return 0;
400 }
401
402
403 static void glamo_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
404                                  u16 *blue, uint32_t size)
405 {
406         printk(KERN_CRIT "glamo_crtc_gamma_set\n");
407 }
408
409
410 static void glamo_crtc_destroy(struct drm_crtc *crtc)
411 {
412         struct glamo_crtc *glamo_crtc = to_glamo_crtc(crtc);
413         printk(KERN_CRIT "glamo_crtc_destroy\n");
414         drm_crtc_cleanup(crtc);
415         kfree(glamo_crtc);
416 }
417
418
419 static enum drm_connector_status
420 glamo_connector_detect(struct drm_connector *connector)
421 {
422         /* One hopes it hasn't been de-soldered... */
423         printk(KERN_CRIT "glamo_connector_detect\n");
424         return connector_status_connected;
425 }
426
427
428 static void glamo_connector_destroy(struct drm_connector *connector)
429 {
430         printk(KERN_CRIT "glamo_connector_destroy\n");
431         drm_sysfs_connector_remove(connector);
432         drm_connector_cleanup(connector);
433         kfree(connector);
434 }
435
436
437 static int glamo_connector_get_modes(struct drm_connector *connector)
438 {
439         struct drm_display_mode *mode;
440         struct glamofb_platform_data *mach_info;
441         struct glamo_output *goutput = to_glamo_output(connector);
442         struct glamodrm_handle *gdrm = goutput->gdrm;
443
444         /* Dig out the record which will tell us about the hardware */
445         mach_info = gdrm->glamo_core->pdev->dev.platform_data;
446
447         printk(KERN_CRIT "glamo_connector_get_modes\n");
448
449         mode = drm_mode_create(connector->dev);
450         if (!mode)
451                 return 0;
452         /* Fill in 'mode' here */
453         mode->type = DRM_MODE_TYPE_DEFAULT | DRM_MODE_TYPE_PREFERRED;
454
455         /* Convert framebuffer timings into KMS timings */
456         mode->clock = 1000000 / mach_info->pixclock;    /* ps -> Hz */
457         mode->hdisplay = mach_info->xres.defval;
458         mode->hsync_start = mach_info->right_margin + mode->hdisplay;
459         mode->hsync_end = mode->hsync_start + mach_info->hsync_len;
460         mode->htotal = mode->hsync_end + mach_info->left_margin;
461         mode->hskew = 0;
462         
463         mode->vdisplay = mach_info->yres.defval;
464         mode->vsync_start = mach_info->lower_margin + mode->vdisplay;
465         mode->vsync_end = mode->vsync_start + mach_info->vsync_len;
466         mode->vtotal = mode->vsync_end + mach_info->upper_margin;
467         mode->vscan = 0;
468
469         mode->width_mm = mach_info->width;
470         mode->height_mm = mach_info->height;
471
472         printk(KERN_CRIT "Modeline \"%ix%i\" %i   %i %i %i %i    %i %i %i %i\n",
473                mode->hdisplay, mode->vdisplay, mode->clock,
474                mode->hdisplay, mode->hsync_start, mode->hsync_end, mode->htotal,
475                mode->vdisplay, mode->vsync_start, mode->vsync_end, mode->vtotal);
476
477         drm_mode_set_name(mode);
478         drm_mode_probed_add(connector, mode);
479
480         return 1;       /* one mode, for now */
481 }
482
483
484 static int glamo_connector_set_property(struct drm_connector *connector,
485                                   struct drm_property *property,
486                                   uint64_t value)
487 {
488         printk(KERN_CRIT "glamo_connector_set_property\n");
489         return 0;
490 }
491
492
493 static int glamo_connector_mode_valid(struct drm_connector *connector,
494                                       struct drm_display_mode *mode)
495 {
496         printk(KERN_CRIT "glamo_connector_mode_valid\n");
497         if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
498                 return MODE_NO_DBLESCAN;
499
500         return MODE_OK;
501 }
502
503
504 struct drm_encoder *
505 glamo_connector_best_encoder(struct drm_connector *connector)
506 {
507         struct glamo_output *glamo_output = to_glamo_output(connector);
508         printk(KERN_CRIT "glamo_connector_best_encoder\n");
509         return &glamo_output->enc;
510 }
511
512
513 static void glamo_encoder_dpms(struct drm_encoder *encoder, int mode)
514 {
515         printk(KERN_CRIT "glamo_encoder_dpms\n");
516 }
517
518
519 static bool glamo_encoder_mode_fixup(struct drm_encoder *encoder,
520                                  struct drm_display_mode *mode,
521                                  struct drm_display_mode *adjusted_mode)
522 {
523         printk(KERN_CRIT "glamo_encoder_mode_fixup\n");
524         return true;
525 }
526
527
528 void glamo_encoder_prepare(struct drm_encoder *encoder)
529 {
530         printk(KERN_CRIT "glamo_encoder_prepare\n");
531 }
532
533
534 void glamo_encoder_commit(struct drm_encoder *encoder)
535 {
536         printk(KERN_CRIT "glamo_encoder_commit\n");
537 }
538
539
540 static void glamo_encoder_mode_set(struct drm_encoder *encoder,
541                                struct drm_display_mode *mode,
542                                struct drm_display_mode *adjusted_mode)
543 {
544         printk(KERN_CRIT "glamo_encoder_mode_set\n");
545 }
546
547
548 static void glamo_encoder_destroy(struct drm_encoder *encoder)
549 {
550         printk(KERN_CRIT "glamo_encoder_destroy\n");
551         drm_encoder_cleanup(encoder);
552 }
553
554
555 static void glamo_framebuffer_destroy(struct drm_framebuffer *fb)
556 {
557         struct glamo_framebuffer *glamo_fb = to_glamo_framebuffer(fb);
558         struct drm_device *dev = fb->dev;
559         printk(KERN_CRIT "glamo_user_framebuffer_destroy\n");
560
561         drm_framebuffer_cleanup(fb);
562         mutex_lock(&dev->struct_mutex);
563         drm_gem_object_unreference(glamo_fb->obj);
564         mutex_unlock(&dev->struct_mutex);
565
566         kfree(glamo_fb);
567 }
568
569 static int glamo_framebuffer_create_handle(struct drm_framebuffer *fb,
570                                                 struct drm_file *file_priv,
571                                                 unsigned int *handle)
572 {
573         struct glamo_framebuffer *glamo_fb = to_glamo_framebuffer(fb);
574         struct drm_gem_object *object = glamo_fb->obj;
575         printk(KERN_CRIT "glamo_user_framebuffer_create_handle\n");
576
577         return drm_gem_handle_create(file_priv, object, handle);
578 }
579
580
581 static const struct drm_framebuffer_funcs glamo_fb_funcs = {
582         .destroy = glamo_framebuffer_destroy,
583         .create_handle = glamo_framebuffer_create_handle,
584 };
585
586
587 int glamo_framebuffer_create(struct drm_device *dev,
588                              struct drm_mode_fb_cmd *mode_cmd,
589                              struct drm_framebuffer **fb,
590                              struct drm_gem_object *obj)
591 {
592         struct glamo_framebuffer *glamo_fb;
593         int ret;
594
595         printk(KERN_CRIT "glamo_framebuffer_create\n");
596         glamo_fb = kzalloc(sizeof(*glamo_fb), GFP_KERNEL);
597         if (!glamo_fb)
598                 return -ENOMEM;
599
600         ret = drm_framebuffer_init(dev, &glamo_fb->base, &glamo_fb_funcs);
601         if (ret) {
602                 DRM_ERROR("framebuffer init failed %d\n", ret);
603                 return ret;
604         }
605
606         drm_helper_mode_fill_fb_struct(&glamo_fb->base, mode_cmd);
607
608         glamo_fb->obj = obj;
609
610         *fb = &glamo_fb->base;
611
612         return 0;
613 }
614
615
616 static struct drm_framebuffer *
617 glamo_user_framebuffer_create(struct drm_device *dev,
618                               struct drm_file *filp,
619                               struct drm_mode_fb_cmd *mode_cmd)
620 {
621         struct drm_gem_object *obj;
622         struct drm_framebuffer *fb;
623         int ret;
624         printk(KERN_CRIT "glamo_user_framebuffer_create\n");
625
626         obj = drm_gem_object_lookup(dev, filp, mode_cmd->handle);
627         if (!obj)
628                 return NULL;
629
630         ret = glamo_framebuffer_create(dev, mode_cmd, &fb, obj);
631         if (ret) {
632                 drm_gem_object_unreference(obj);
633                 return NULL;
634         }
635
636         return fb;
637 }
638
639
640 int glamo_fbchanged(struct drm_device *dev)
641 {
642         printk(KERN_CRIT "glamo_fb_changed\n");
643         return 0;
644 }
645
646
647 /* CRTC functions */
648 static const struct drm_crtc_funcs glamo_crtc_funcs = {
649         .cursor_set = glamo_crtc_cursor_set,
650         .cursor_move = glamo_crtc_cursor_move,
651         .gamma_set = glamo_crtc_gamma_set,
652         .set_config = drm_crtc_helper_set_config,
653         .destroy = glamo_crtc_destroy,
654 };
655
656
657 /* CRTC helper functions */
658 static const struct drm_crtc_helper_funcs glamo_crtc_helper_funcs = {
659         .dpms = glamo_crtc_dpms,
660         .mode_fixup = glamo_crtc_mode_fixup,
661         .mode_set = glamo_crtc_mode_set,
662         .mode_set_base = glamo_crtc_mode_set_base,
663         .prepare = glamo_crtc_prepare,
664         .commit = glamo_crtc_commit,
665 };
666
667
668 /* Connector functions */
669 static const struct drm_connector_funcs glamo_connector_funcs = {
670         .detect = glamo_connector_detect,
671         .fill_modes = drm_helper_probe_single_connector_modes,
672         .destroy = glamo_connector_destroy,
673         .set_property = glamo_connector_set_property,
674 };
675
676
677 /* Connector helper functions */
678 static const struct drm_connector_helper_funcs glamo_connector_helper_funcs = {
679         .mode_valid = glamo_connector_mode_valid,
680         .get_modes = glamo_connector_get_modes,
681         .best_encoder = glamo_connector_best_encoder,
682 };
683
684
685 /* Encoder functions */
686 static const struct drm_encoder_funcs glamo_encoder_funcs = {
687         .destroy = glamo_encoder_destroy,
688 };
689
690
691 /* Encoder helper functions */
692 static const struct drm_encoder_helper_funcs glamo_encoder_helper_funcs = {
693         .dpms = glamo_encoder_dpms,
694         .mode_fixup = glamo_encoder_mode_fixup,
695         .prepare = glamo_encoder_prepare,
696         .commit = glamo_encoder_commit,
697         .mode_set = glamo_encoder_mode_set,
698 };
699
700
701 /* Mode functions */
702 static const struct drm_mode_config_funcs glamo_mode_funcs = {
703         .fb_create = glamo_user_framebuffer_create,
704         .fb_changed = glamo_fbchanged
705 };
706
707
708 static struct drm_mode_set kernelfb_mode;
709
710
711 /* Restore's the kernel's fbcon mode, used for panic path */
712 void glamo_display_restore(void)
713 {
714         drm_crtc_helper_set_config(&kernelfb_mode);
715 }
716
717
718 static int glamo_display_panic(struct notifier_block *n, unsigned long ununsed,
719                                void *panic_str)
720 {
721         DRM_ERROR("panic occurred, switching back to text console\n");
722
723         glamo_display_restore();
724         return 0;
725 }
726
727
728 static struct notifier_block paniced = {
729         .notifier_call = glamo_display_panic,
730 };
731
732
733 int glamo_display_init(struct drm_device *dev)
734 {
735         struct glamodrm_handle *gdrm;
736         struct glamo_crtc *glamo_crtc;
737         struct glamo_output *glamo_output;
738         struct drm_connector *connector;
739         struct glamo_framebuffer *glamo_fb;
740         struct fb_info *info;
741         struct glamofb_par *par;
742         struct drm_mode_set *modeset;
743
744         gdrm = dev->dev_private;
745
746         printk(KERN_CRIT "glamo_display_init\n");
747
748         glamo_engine_enable(gdrm->glamo_core, GLAMO_ENGINE_LCD);
749         glamo_engine_reset(gdrm->glamo_core, GLAMO_ENGINE_LCD);
750
751         drm_mode_config_init(dev);
752
753         dev->mode_config.min_width = 0;
754         dev->mode_config.min_height = 0;
755
756         dev->mode_config.funcs = (void *)&glamo_mode_funcs;
757
758         /* Initialise our CRTC object.
759          * Only one connector per CRTC.  We know this: it's kind of soldered. */
760         glamo_crtc = kzalloc(sizeof(struct glamo_crtc)
761                            + sizeof(struct drm_connector *), GFP_KERNEL);
762         if (glamo_crtc == NULL) return 1;
763         glamo_crtc->gdrm = gdrm;
764         glamo_crtc->blank_mode = DRM_MODE_DPMS_OFF;
765         drm_crtc_init(dev, &glamo_crtc->base, &glamo_crtc_funcs);
766         drm_crtc_helper_add(&glamo_crtc->base, &glamo_crtc_helper_funcs);
767
768         glamo_crtc->mode_set.crtc = &glamo_crtc->base;
769         glamo_crtc->mode_set.connectors =
770                                       (struct drm_connector **)(glamo_crtc + 1);
771         glamo_crtc->mode_set.num_connectors = 0;
772
773         /* Create our "output" object: consists of an output and an encoder */
774         glamo_output = kzalloc(sizeof(struct glamo_output), GFP_KERNEL);
775         if (glamo_output == NULL) return 1;
776         connector = &glamo_output->base;
777         glamo_output->gdrm = gdrm;
778
779         /* Initialise the connector */
780         drm_connector_init(dev, connector, &glamo_connector_funcs,
781                            DRM_MODE_CONNECTOR_Unknown);
782         drm_sysfs_connector_add(connector);
783         connector->interlace_allowed = 0;
784         connector->doublescan_allowed = 0;
785
786         /* Initialise the encoder */
787         drm_encoder_init(dev, &glamo_output->enc, &glamo_encoder_funcs,
788                          DRM_MODE_ENCODER_DAC);
789         glamo_output->enc.possible_crtcs = 1 << 0;
790         drm_mode_connector_attach_encoder(&glamo_output->base,
791                                           &glamo_output->enc);
792
793         drm_encoder_helper_add(&glamo_output->enc, &glamo_encoder_helper_funcs);
794         drm_connector_helper_add(connector, &glamo_connector_helper_funcs);
795
796         drm_helper_initial_config(dev, false);
797
798         /* Initial setup of the LCD controller */
799         glamo_run_lcd_script(gdrm, lcd_init_script,
800                                    ARRAY_SIZE(lcd_init_script));
801
802         if (list_empty(&dev->mode_config.fb_kernel_list)) {
803                 int ret;
804                 ret = glamofb_create(dev, 480, 640, 480, 640, &glamo_fb);
805                 if (ret) return -EINVAL;
806         }
807
808         info = glamo_fb->base.fbdev;
809         par = info->par;
810
811         modeset = &glamo_crtc->mode_set;
812         modeset->fb = &glamo_fb->base;
813         modeset->connectors[0] = connector;
814
815         par->crtc_ids[0] = glamo_crtc->base.base.id;
816
817         modeset->num_connectors = 1;
818         modeset->mode = modeset->crtc->desired_mode;
819
820         par->crtc_count = 1;
821
822         if (register_framebuffer(info) < 0)
823                 return -EINVAL;
824
825         printk(KERN_INFO "[glamo-drm] fb%d: %s frame buffer device\n",
826                info->node, info->fix.id);
827
828         /* Switch back to kernel console on panic */
829         kernelfb_mode = *modeset;
830         atomic_notifier_chain_register(&panic_notifier_list, &paniced);
831         printk(KERN_INFO "[glamo-drm] registered panic notifier\n");
832
833         return 0;
834 }