Whitespace and debugging
[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 /* Power on/off */
260 static void glamo_crtc_dpms(struct drm_crtc *crtc, int mode)
261 {
262         struct glamodrm_handle *gdrm;
263         struct glamo_crtc *glamo_crtc = to_glamo_crtc(crtc);
264
265         printk(KERN_CRIT "glamo_crtc_dpms(%u)\n", mode);
266         gdrm = glamo_crtc->gdrm;
267
268         switch (mode) {
269         case DRM_MODE_DPMS_OFF:
270                 /* Simulating FB_BLANK_NORMAL allow turning off backlight */
271                 //if (gfb->blank_mode != FB_BLANK_NORMAL)
272                 //      notify_blank(info, FB_BLANK_NORMAL);
273
274                 /* LCM need notification before pixel clock is stopped */
275                 //notify_blank(crtc, blank_mode);
276
277                 /* disable the pixel clock */
278                 glamo_engine_clkreg_set(gdrm->glamo_core, GLAMO_ENGINE_LCD,
279                                         GLAMO_CLOCK_LCD_EN_DCLK, 0);
280                 glamo_crtc->blank_mode = mode;
281                 break;
282         case DRM_MODE_DPMS_ON:
283                 /* enable the pixel clock if off */
284                 if (glamo_crtc->blank_mode == DRM_MODE_DPMS_OFF)
285                         glamo_engine_clkreg_set(gdrm->glamo_core,
286                                         GLAMO_ENGINE_LCD,
287                                         GLAMO_CLOCK_LCD_EN_DCLK,
288                                         GLAMO_CLOCK_LCD_EN_DCLK);
289
290                 //notify_blank(info, blank_mode);
291                 glamo_crtc->blank_mode = mode;
292                 break;
293         }
294
295 }
296
297
298 static bool glamo_crtc_mode_fixup(struct drm_crtc *crtc,
299                                   struct drm_display_mode *mode,
300                                   struct drm_display_mode *adjusted_mode)
301 {
302         printk(KERN_CRIT "glamo_crtc_mode_fixup\n");
303         return true;
304 }
305
306
307 static void glamo_crtc_mode_set(struct drm_crtc *crtc,
308                                 struct drm_display_mode *mode,
309                                 struct drm_display_mode *adjusted_mode,
310                                 int x, int y,
311                                 struct drm_framebuffer *old_fb)
312 {
313         struct glamodrm_handle *gdrm;
314         struct glamo_crtc *gcrtc;
315         int retr_end, disp_start, disp_end;
316
317         printk(KERN_CRIT "glamo_crtc_mode_set\n");
318
319         /* Dig out our handle */
320         gcrtc = to_glamo_crtc(crtc);
321         gdrm = gcrtc->gdrm;     /* Here it is! */
322
323         glamo_lcd_cmd_mode(gdrm, 1);
324         glamo_engine_reclock(gdrm->glamo_core, GLAMO_ENGINE_LCD, mode->clock);
325
326         reg_set_bit_mask_lcd(gdrm, GLAMO_REG_LCD_WIDTH,
327                              GLAMO_LCD_WIDTH_MASK, mode->hdisplay);
328         reg_set_bit_mask_lcd(gdrm, GLAMO_REG_LCD_HEIGHT,
329                              GLAMO_LCD_HEIGHT_MASK, mode->vdisplay);
330         reg_set_bit_mask_lcd(gdrm, GLAMO_REG_LCD_PITCH,
331                              GLAMO_LCD_PITCH_MASK, mode->hdisplay*2);
332
333         /* Convert "X modeline timings" into "Glamo timings" */
334         retr_end = mode->hsync_end - mode->hsync_start;
335         disp_start = mode->htotal - mode->hsync_start;
336         disp_end = mode->hsync_start;
337         reg_set_bit_mask_lcd(gdrm, GLAMO_REG_LCD_HORIZ_TOTAL,
338                              GLAMO_LCD_HV_TOTAL_MASK, mode->htotal);
339         reg_set_bit_mask_lcd(gdrm, GLAMO_REG_LCD_HORIZ_RETR_START,
340                              GLAMO_LCD_HV_RETR_START_MASK, 0);
341         reg_set_bit_mask_lcd(gdrm, GLAMO_REG_LCD_HORIZ_RETR_END,
342                              GLAMO_LCD_HV_RETR_END_MASK, retr_end);
343         reg_set_bit_mask_lcd(gdrm, GLAMO_REG_LCD_HORIZ_DISP_START,
344                              GLAMO_LCD_HV_RETR_DISP_START_MASK, disp_start);
345         reg_set_bit_mask_lcd(gdrm, GLAMO_REG_LCD_HORIZ_DISP_END,
346                              GLAMO_LCD_HV_RETR_DISP_END_MASK, disp_end);
347
348         /* The same in the vertical direction */
349         retr_end = mode->vsync_end - mode->vsync_start;
350         disp_start = mode->vtotal - mode->vsync_start;
351         disp_end = mode->vsync_start;
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, 0);
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         struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
377         printk(KERN_CRIT "glamo_crtc_prepare\n");
378         crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
379 }
380
381
382 static void glamo_crtc_commit(struct drm_crtc *crtc)
383 {
384         struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
385         printk(KERN_CRIT "glamo_crtc_commit\n");
386         crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
387 }
388
389
390 static int glamo_crtc_cursor_set(struct drm_crtc *crtc,
391                                  struct drm_file *file_priv,
392                                  uint32_t handle,
393                                  uint32_t width, uint32_t height)
394 {
395         printk(KERN_CRIT "glamo_crtc_cursor_set\n");
396         return 0;
397 }
398
399
400 static int glamo_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
401 {
402         printk(KERN_CRIT "glamo_crtc_cursor_move\n");
403         return 0;
404 }
405
406
407 static void glamo_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
408                                  u16 *blue, uint32_t size)
409 {
410         printk(KERN_CRIT "glamo_crtc_gamma_set\n");
411 }
412
413
414 static void glamo_crtc_destroy(struct drm_crtc *crtc)
415 {
416         struct glamo_crtc *glamo_crtc = to_glamo_crtc(crtc);
417         printk(KERN_CRIT "glamo_crtc_destroy\n");
418         drm_crtc_cleanup(crtc);
419         kfree(glamo_crtc);
420 }
421
422
423 static enum drm_connector_status
424 glamo_connector_detect(struct drm_connector *connector)
425 {
426         /* One hopes it hasn't been de-soldered... */
427         printk(KERN_CRIT "glamo_connector_detect\n");
428         return connector_status_connected;
429 }
430
431
432 static void glamo_connector_destroy(struct drm_connector *connector)
433 {
434         printk(KERN_CRIT "glamo_connector_destroy\n");
435         drm_sysfs_connector_remove(connector);
436         drm_connector_cleanup(connector);
437         kfree(connector);
438 }
439
440
441 static int glamo_connector_get_modes(struct drm_connector *connector)
442 {
443         struct drm_display_mode *mode;
444         struct glamofb_platform_data *mach_info;
445         struct glamo_output *goutput = to_glamo_output(connector);
446         struct glamodrm_handle *gdrm = goutput->gdrm;
447
448         /* Dig out the record which will tell us about the hardware */
449         mach_info = gdrm->glamo_core->pdev->dev.platform_data;
450
451         printk(KERN_CRIT "glamo_connector_get_modes\n");
452
453         mode = drm_mode_create(connector->dev);
454         if (!mode)
455                 return 0;
456         /* Fill in 'mode' here */
457         mode->type = DRM_MODE_TYPE_DEFAULT | DRM_MODE_TYPE_PREFERRED;
458
459         /* Convert framebuffer timings into KMS timings */
460         mode->clock = 1000000 / mach_info->pixclock;    /* ps -> Hz */
461         mode->hdisplay = mach_info->xres.defval;
462         mode->hsync_start = mach_info->right_margin + mode->hdisplay;
463         mode->hsync_end = mode->hsync_start + mach_info->hsync_len;
464         mode->htotal = mode->hdisplay + mach_info->left_margin
465                         + mach_info->right_margin + mach_info->hsync_len;
466
467         mode->hskew = 0;
468         mode->vdisplay = mach_info->yres.defval;
469         mode->vsync_start = mach_info->lower_margin + mode->vdisplay;
470         mode->vsync_end = mode->vsync_start + mach_info->vsync_len;
471         mode->vtotal = mode->vdisplay + mach_info->upper_margin
472                         + mach_info->lower_margin + mach_info->vsync_len;
473         mode->vscan = 0;
474
475         mode->width_mm = mach_info->width;
476         mode->height_mm = mach_info->height;
477
478         printk(KERN_CRIT "Modeline \"%ix%i\" %i   %i %i %i %i    %i %i %i %i\n",
479                mode->hdisplay, mode->vdisplay, mode->clock,
480                mode->hdisplay, mode->hsync_start, mode->hsync_end, mode->htotal,
481                mode->vdisplay, mode->vsync_start, mode->vsync_end, mode->vtotal);
482
483         drm_mode_set_name(mode);
484         drm_mode_probed_add(connector, mode);
485
486         return 1;       /* one mode, for now */
487 }
488
489
490 static int glamo_connector_set_property(struct drm_connector *connector,
491                                   struct drm_property *property,
492                                   uint64_t value)
493 {
494         printk(KERN_CRIT "glamo_connector_set_property\n");
495         return 0;
496 }
497
498
499 static int glamo_connector_mode_valid(struct drm_connector *connector,
500                                       struct drm_display_mode *mode)
501 {
502         printk(KERN_CRIT "glamo_connector_mode_valid\n");
503         if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
504                 return MODE_NO_DBLESCAN;
505
506         return MODE_OK;
507 }
508
509
510 struct drm_encoder *
511 glamo_connector_best_encoder(struct drm_connector *connector)
512 {
513         struct glamo_output *glamo_output = to_glamo_output(connector);
514         printk(KERN_CRIT "glamo_connector_best_encoder\n");
515         return &glamo_output->enc;
516 }
517
518
519 static void glamo_encoder_dpms(struct drm_encoder *encoder, int mode)
520 {
521         printk(KERN_CRIT "glamo_encoder_dpms\n");
522 }
523
524
525 static bool glamo_encoder_mode_fixup(struct drm_encoder *encoder,
526                                  struct drm_display_mode *mode,
527                                  struct drm_display_mode *adjusted_mode)
528 {
529         printk(KERN_CRIT "glamo_encoder_mode_fixup\n");
530         return true;
531 }
532
533
534 void glamo_encoder_prepare(struct drm_encoder *encoder)
535 {
536         printk(KERN_CRIT "glamo_encoder_prepare\n");
537 }
538
539
540 void glamo_encoder_commit(struct drm_encoder *encoder)
541 {
542         printk(KERN_CRIT "glamo_encoder_commit\n");
543 }
544
545
546 static void glamo_encoder_mode_set(struct drm_encoder *encoder,
547                                struct drm_display_mode *mode,
548                                struct drm_display_mode *adjusted_mode)
549 {
550         printk(KERN_CRIT "glamo_encoder_mode_set\n");
551 }
552
553
554 static void glamo_encoder_destroy(struct drm_encoder *encoder)
555 {
556         printk(KERN_CRIT "glamo_encoder_destroy\n");
557         drm_encoder_cleanup(encoder);
558 }
559
560
561 static void glamo_framebuffer_destroy(struct drm_framebuffer *fb)
562 {
563         struct glamo_framebuffer *glamo_fb = to_glamo_framebuffer(fb);
564         struct drm_device *dev = fb->dev;
565         printk(KERN_CRIT "glamo_user_framebuffer_destroy\n");
566
567         drm_framebuffer_cleanup(fb);
568         mutex_lock(&dev->struct_mutex);
569         drm_gem_object_unreference(glamo_fb->obj);
570         mutex_unlock(&dev->struct_mutex);
571
572         kfree(glamo_fb);
573 }
574
575 static int glamo_framebuffer_create_handle(struct drm_framebuffer *fb,
576                                                 struct drm_file *file_priv,
577                                                 unsigned int *handle)
578 {
579         struct glamo_framebuffer *glamo_fb = to_glamo_framebuffer(fb);
580         struct drm_gem_object *object = glamo_fb->obj;
581         printk(KERN_CRIT "glamo_user_framebuffer_create_handle\n");
582
583         return drm_gem_handle_create(file_priv, object, handle);
584 }
585
586
587 static const struct drm_framebuffer_funcs glamo_fb_funcs = {
588         .destroy = glamo_framebuffer_destroy,
589         .create_handle = glamo_framebuffer_create_handle,
590 };
591
592
593 int glamo_framebuffer_create(struct drm_device *dev,
594                              struct drm_mode_fb_cmd *mode_cmd,
595                              struct drm_framebuffer **fb,
596                              struct drm_gem_object *obj)
597 {
598         struct glamo_framebuffer *glamo_fb;
599         int ret;
600
601         printk(KERN_CRIT "glamo_framebuffer_create\n");
602         glamo_fb = kzalloc(sizeof(*glamo_fb), GFP_KERNEL);
603         if (!glamo_fb)
604                 return -ENOMEM;
605
606         ret = drm_framebuffer_init(dev, &glamo_fb->base, &glamo_fb_funcs);
607         if (ret) {
608                 DRM_ERROR("framebuffer init failed %d\n", ret);
609                 return ret;
610         }
611
612         drm_helper_mode_fill_fb_struct(&glamo_fb->base, mode_cmd);
613
614         glamo_fb->obj = obj;
615
616         *fb = &glamo_fb->base;
617
618         return 0;
619 }
620
621
622 static struct drm_framebuffer *
623 glamo_user_framebuffer_create(struct drm_device *dev,
624                               struct drm_file *filp,
625                               struct drm_mode_fb_cmd *mode_cmd)
626 {
627         struct drm_gem_object *obj;
628         struct drm_framebuffer *fb;
629         int ret;
630         printk(KERN_CRIT "glamo_user_framebuffer_create\n");
631
632         obj = drm_gem_object_lookup(dev, filp, mode_cmd->handle);
633         if (!obj)
634                 return NULL;
635
636         ret = glamo_framebuffer_create(dev, mode_cmd, &fb, obj);
637         if (ret) {
638                 drm_gem_object_unreference(obj);
639                 return NULL;
640         }
641
642         return fb;
643 }
644
645
646 int glamo_fbchanged(struct drm_device *dev)
647 {
648         printk(KERN_CRIT "glamo_fb_changed\n");
649         return 0;
650 }
651
652
653 /* CRTC functions */
654 static const struct drm_crtc_funcs glamo_crtc_funcs = {
655         .cursor_set = glamo_crtc_cursor_set,
656         .cursor_move = glamo_crtc_cursor_move,
657         .gamma_set = glamo_crtc_gamma_set,
658         .set_config = drm_crtc_helper_set_config,
659         .destroy = glamo_crtc_destroy,
660 };
661
662
663 /* CRTC helper functions */
664 static const struct drm_crtc_helper_funcs glamo_crtc_helper_funcs = {
665         .dpms = glamo_crtc_dpms,
666         .mode_fixup = glamo_crtc_mode_fixup,
667         .mode_set = glamo_crtc_mode_set,
668         .mode_set_base = glamo_crtc_mode_set_base,
669         .prepare = glamo_crtc_prepare,
670         .commit = glamo_crtc_commit,
671 };
672
673
674 /* Connector functions */
675 static const struct drm_connector_funcs glamo_connector_funcs = {
676         .detect = glamo_connector_detect,
677         .fill_modes = drm_helper_probe_single_connector_modes,
678         .destroy = glamo_connector_destroy,
679         .set_property = glamo_connector_set_property,
680 };
681
682
683 /* Connector helper functions */
684 static const struct drm_connector_helper_funcs glamo_connector_helper_funcs = {
685         .mode_valid = glamo_connector_mode_valid,
686         .get_modes = glamo_connector_get_modes,
687         .best_encoder = glamo_connector_best_encoder,
688 };
689
690
691 /* Encoder functions */
692 static const struct drm_encoder_funcs glamo_encoder_funcs = {
693         .destroy = glamo_encoder_destroy,
694 };
695
696
697 /* Encoder helper functions */
698 static const struct drm_encoder_helper_funcs glamo_encoder_helper_funcs = {
699         .dpms = glamo_encoder_dpms,
700         .mode_fixup = glamo_encoder_mode_fixup,
701         .prepare = glamo_encoder_prepare,
702         .commit = glamo_encoder_commit,
703         .mode_set = glamo_encoder_mode_set,
704 };
705
706
707 /* Mode functions */
708 static const struct drm_mode_config_funcs glamo_mode_funcs = {
709         .fb_create = glamo_user_framebuffer_create,
710         .fb_changed = glamo_fbchanged
711 };
712
713
714 static struct drm_mode_set kernelfb_mode;
715
716
717 /* Restore's the kernel's fbcon mode, used for panic path */
718 void glamo_display_restore(void)
719 {
720         drm_crtc_helper_set_config(&kernelfb_mode);
721 }
722
723
724 static int glamo_display_panic(struct notifier_block *n, unsigned long ununsed,
725                                void *panic_str)
726 {
727         DRM_ERROR("panic occurred, switching back to text console\n");
728
729         glamo_display_restore();
730         return 0;
731 }
732
733
734 static struct notifier_block paniced = {
735         .notifier_call = glamo_display_panic,
736 };
737
738
739 int glamo_display_init(struct drm_device *dev)
740 {
741         struct glamodrm_handle *gdrm;
742         struct glamo_crtc *glamo_crtc;
743         struct glamo_output *glamo_output;
744         struct drm_connector *connector;
745         struct glamo_framebuffer *glamo_fb;
746         struct fb_info *info;
747         struct glamofb_par *par;
748         struct drm_mode_set *modeset;
749
750         gdrm = dev->dev_private;
751
752         printk(KERN_CRIT "glamo_display_init\n");
753
754         drm_mode_config_init(dev);
755
756         dev->mode_config.min_width = 0;
757         dev->mode_config.min_height = 0;
758
759         dev->mode_config.funcs = (void *)&glamo_mode_funcs;
760
761         /* Initialise our CRTC object.
762          * Only one connector per CRTC.  We know this: it's kind of soldered. */
763         glamo_crtc = kzalloc(sizeof(struct glamo_crtc)
764                            + sizeof(struct drm_connector *), GFP_KERNEL);
765         if (glamo_crtc == NULL) return 1;
766         glamo_crtc->gdrm = gdrm;
767         glamo_crtc->blank_mode = DRM_MODE_DPMS_OFF;
768         drm_crtc_init(dev, &glamo_crtc->base, &glamo_crtc_funcs);
769         drm_crtc_helper_add(&glamo_crtc->base, &glamo_crtc_helper_funcs);
770
771         glamo_crtc->mode_set.crtc = &glamo_crtc->base;
772         glamo_crtc->mode_set.connectors =
773                                       (struct drm_connector **)(glamo_crtc + 1);
774         glamo_crtc->mode_set.num_connectors = 0;
775
776         /* Create our "output" object: consists of an output and an encoder */
777         glamo_output = kzalloc(sizeof(struct glamo_output), GFP_KERNEL);
778         if (glamo_output == NULL) return 1;
779         connector = &glamo_output->base;
780         glamo_output->gdrm = gdrm;
781
782         /* Initialise the connector */
783         drm_connector_init(dev, connector, &glamo_connector_funcs,
784                            DRM_MODE_CONNECTOR_Unknown);
785         drm_sysfs_connector_add(connector);
786         connector->interlace_allowed = 0;
787         connector->doublescan_allowed = 0;
788
789         /* Initialise the encoder */
790         drm_encoder_init(dev, &glamo_output->enc, &glamo_encoder_funcs,
791                          DRM_MODE_ENCODER_DAC);
792         glamo_output->enc.possible_crtcs = 1 << 0;
793         drm_mode_connector_attach_encoder(&glamo_output->base,
794                                           &glamo_output->enc);
795
796         drm_encoder_helper_add(&glamo_output->enc, &glamo_encoder_helper_funcs);
797         drm_connector_helper_add(connector, &glamo_connector_helper_funcs);
798
799         drm_helper_initial_config(dev, false);
800
801         /* Initial setup of the LCD controller */
802         glamo_run_lcd_script(gdrm, lcd_init_script,
803                                    ARRAY_SIZE(lcd_init_script));
804
805         if (list_empty(&dev->mode_config.fb_kernel_list)) {
806                 int ret;
807                 ret = glamofb_create(dev, 480, 640, 480, 640, &glamo_fb);
808                 if (ret) return -EINVAL;
809         }
810
811         info = glamo_fb->base.fbdev;
812         par = info->par;
813
814         modeset = &glamo_crtc->mode_set;
815         modeset->fb = &glamo_fb->base;
816         modeset->connectors[0] = connector;
817
818         par->crtc_ids[0] = glamo_crtc->base.base.id;
819
820         modeset->num_connectors = 1;
821         modeset->mode = modeset->crtc->desired_mode;
822
823         par->crtc_count = 1;
824
825         if (register_framebuffer(info) < 0)
826                 return -EINVAL;
827
828         printk(KERN_INFO "[glamo-drm] fb%d: %s frame buffer device\n",
829                info->node, info->fix.id);
830
831         /* Switch back to kernel console on panic */
832         kernelfb_mode = *modeset;
833         atomic_notifier_chain_register(&panic_notifier_list, &paniced);
834         printk(KERN_INFO "[glamo-drm] registered panic notifier\n");
835
836         return 0;
837 }