Subversion Repositories Kolibri OS

Rev

Rev 2160 | Rev 3192 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright (c) 2006-2009 Red Hat Inc.
  3.  * Copyright (c) 2006-2008 Intel Corporation
  4.  * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
  5.  *
  6.  * DRM framebuffer helper functions
  7.  *
  8.  * Permission to use, copy, modify, distribute, and sell this software and its
  9.  * documentation for any purpose is hereby granted without fee, provided that
  10.  * the above copyright notice appear in all copies and that both that copyright
  11.  * notice and this permission notice appear in supporting documentation, and
  12.  * that the name of the copyright holders not be used in advertising or
  13.  * publicity pertaining to distribution of the software without specific,
  14.  * written prior permission.  The copyright holders make no representations
  15.  * about the suitability of this software for any purpose.  It is provided "as
  16.  * is" without express or implied warranty.
  17.  *
  18.  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  19.  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  20.  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  21.  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  22.  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  23.  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  24.  * OF THIS SOFTWARE.
  25.  *
  26.  * Authors:
  27.  *      Dave Airlie <airlied@linux.ie>
  28.  *      Jesse Barnes <jesse.barnes@intel.com>
  29.  */
  30. #include <linux/kernel.h>
  31. #include <linux/sysrq.h>
  32. #include <linux/slab.h>
  33. #include <linux/fb.h>
  34. #include <linux/module.h>
  35. #include <drm/drmP.h>
  36. #include <drm/drm_crtc.h>
  37. #include <drm/drm_fb_helper.h>
  38. #include <drm/drm_crtc_helper.h>
  39.  
  40. MODULE_AUTHOR("David Airlie, Jesse Barnes");
  41. MODULE_DESCRIPTION("DRM KMS helper");
  42. MODULE_LICENSE("GPL and additional rights");
  43.  
  44. static LIST_HEAD(kernel_fb_helper_list);
  45.  
  46. /* simple single crtc case helper function */
  47. int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper)
  48. {
  49.         struct drm_device *dev = fb_helper->dev;
  50.         struct drm_connector *connector;
  51.         int i;
  52.  
  53.         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  54.                 struct drm_fb_helper_connector *fb_helper_connector;
  55.  
  56.                 fb_helper_connector = kzalloc(sizeof(struct drm_fb_helper_connector), GFP_KERNEL);
  57.                 if (!fb_helper_connector)
  58.                         goto fail;
  59.  
  60.                 fb_helper_connector->connector = connector;
  61.                 fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector;
  62.         }
  63.         return 0;
  64. fail:
  65.         for (i = 0; i < fb_helper->connector_count; i++) {
  66.                 kfree(fb_helper->connector_info[i]);
  67.                 fb_helper->connector_info[i] = NULL;
  68.         }
  69.         fb_helper->connector_count = 0;
  70.     return -ENOMEM;
  71. }
  72. EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors);
  73.  
  74. static void drm_fb_helper_save_lut_atomic(struct drm_crtc *crtc, struct drm_fb_helper *helper)
  75. {
  76.         uint16_t *r_base, *g_base, *b_base;
  77.         int i;
  78.  
  79.         r_base = crtc->gamma_store;
  80.         g_base = r_base + crtc->gamma_size;
  81.         b_base = g_base + crtc->gamma_size;
  82.  
  83.         for (i = 0; i < crtc->gamma_size; i++)
  84.                 helper->funcs->gamma_get(crtc, &r_base[i], &g_base[i], &b_base[i], i);
  85. }
  86.  
  87. static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc)
  88. {
  89.         uint16_t *r_base, *g_base, *b_base;
  90.  
  91.         if (crtc->funcs->gamma_set == NULL)
  92.                 return;
  93.  
  94.         r_base = crtc->gamma_store;
  95.         g_base = r_base + crtc->gamma_size;
  96.         b_base = g_base + crtc->gamma_size;
  97.  
  98.         crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
  99. }
  100.  
  101.  
  102.  
  103. static void drm_fb_helper_dpms(struct fb_info *info, int dpms_mode)
  104. {
  105.         struct drm_fb_helper *fb_helper = info->par;
  106.         struct drm_device *dev = fb_helper->dev;
  107.         struct drm_crtc *crtc;
  108.         struct drm_connector *connector;
  109.         int i, j;
  110.  
  111.         /*
  112.          * For each CRTC in this fb, turn the connectors on/off.
  113.          */
  114.         mutex_lock(&dev->mode_config.mutex);
  115.         for (i = 0; i < fb_helper->crtc_count; i++) {
  116.                 crtc = fb_helper->crtc_info[i].mode_set.crtc;
  117.  
  118.                 if (!crtc->enabled)
  119.                                 continue;
  120.  
  121.                 /* Walk the connectors & encoders on this fb turning them on/off */
  122.                 for (j = 0; j < fb_helper->connector_count; j++) {
  123.                         connector = fb_helper->connector_info[j]->connector;
  124.                         connector->funcs->dpms(connector, dpms_mode);
  125.                         drm_connector_property_set_value(connector,
  126.                                 dev->mode_config.dpms_property, dpms_mode);
  127.                 }
  128.         }
  129.                                 mutex_unlock(&dev->mode_config.mutex);
  130. }
  131.  
  132. int drm_fb_helper_blank(int blank, struct fb_info *info)
  133. {
  134.         switch (blank) {
  135.         /* Display: On; HSync: On, VSync: On */
  136.         case FB_BLANK_UNBLANK:
  137.                 drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON);
  138.                 break;
  139.         /* Display: Off; HSync: On, VSync: On */
  140.         case FB_BLANK_NORMAL:
  141.                 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
  142.                 break;
  143.         /* Display: Off; HSync: Off, VSync: On */
  144.         case FB_BLANK_HSYNC_SUSPEND:
  145.                 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
  146.                 break;
  147.         /* Display: Off; HSync: On, VSync: Off */
  148.         case FB_BLANK_VSYNC_SUSPEND:
  149.                 drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND);
  150.                 break;
  151.         /* Display: Off; HSync: Off, VSync: Off */
  152.         case FB_BLANK_POWERDOWN:
  153.                 drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF);
  154.                 break;
  155.         }
  156.         return 0;
  157. }
  158. EXPORT_SYMBOL(drm_fb_helper_blank);
  159.  
  160. static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
  161. {
  162.         int i;
  163.  
  164.         for (i = 0; i < helper->connector_count; i++)
  165.                 kfree(helper->connector_info[i]);
  166.         kfree(helper->connector_info);
  167.         for (i = 0; i < helper->crtc_count; i++) {
  168.                 kfree(helper->crtc_info[i].mode_set.connectors);
  169.                 if (helper->crtc_info[i].mode_set.mode)
  170.                         drm_mode_destroy(helper->dev, helper->crtc_info[i].mode_set.mode);
  171.         }
  172.         kfree(helper->crtc_info);
  173. }
  174.  
  175. int drm_fb_helper_init(struct drm_device *dev,
  176.                        struct drm_fb_helper *fb_helper,
  177.                        int crtc_count, int max_conn_count)
  178. {
  179.         struct drm_crtc *crtc;
  180.         int i;
  181.  
  182.         fb_helper->dev = dev;
  183.  
  184.         INIT_LIST_HEAD(&fb_helper->kernel_fb_list);
  185.  
  186.         fb_helper->crtc_info = kcalloc(crtc_count, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
  187.         if (!fb_helper->crtc_info)
  188.                 return -ENOMEM;
  189.  
  190.         fb_helper->crtc_count = crtc_count;
  191.         fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL);
  192.         if (!fb_helper->connector_info) {
  193.                 kfree(fb_helper->crtc_info);
  194.                 return -ENOMEM;
  195.         }
  196.         fb_helper->connector_count = 0;
  197.  
  198.         for (i = 0; i < crtc_count; i++) {
  199.                 fb_helper->crtc_info[i].mode_set.connectors =
  200.                         kcalloc(max_conn_count,
  201.                                 sizeof(struct drm_connector *),
  202.                                 GFP_KERNEL);
  203.  
  204.                 if (!fb_helper->crtc_info[i].mode_set.connectors)
  205.                         goto out_free;
  206.                 fb_helper->crtc_info[i].mode_set.num_connectors = 0;
  207.         }
  208.  
  209.         i = 0;
  210.         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  211.                 fb_helper->crtc_info[i].mode_set.crtc = crtc;
  212.                 i++;
  213.         }
  214.  
  215.         return 0;
  216. out_free:
  217.         drm_fb_helper_crtc_free(fb_helper);
  218.         return -ENOMEM;
  219. }
  220. EXPORT_SYMBOL(drm_fb_helper_init);
  221.  
  222. static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
  223.                      u16 blue, u16 regno, struct fb_info *info)
  224. {
  225.         struct drm_fb_helper *fb_helper = info->par;
  226.         struct drm_framebuffer *fb = fb_helper->fb;
  227.         int pindex;
  228.  
  229.         if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
  230.                 u32 *palette;
  231.                 u32 value;
  232.                 /* place color in psuedopalette */
  233.                 if (regno > 16)
  234.                         return -EINVAL;
  235.                 palette = (u32 *)info->pseudo_palette;
  236.                 red >>= (16 - info->var.red.length);
  237.                 green >>= (16 - info->var.green.length);
  238.                 blue >>= (16 - info->var.blue.length);
  239.                 value = (red << info->var.red.offset) |
  240.                         (green << info->var.green.offset) |
  241.                         (blue << info->var.blue.offset);
  242.                 if (info->var.transp.length > 0) {
  243.                         u32 mask = (1 << info->var.transp.length) - 1;
  244.                         mask <<= info->var.transp.offset;
  245.                         value |= mask;
  246.                 }
  247.                 palette[regno] = value;
  248.                 return 0;
  249.         }
  250.  
  251.         pindex = regno;
  252.  
  253.         if (fb->bits_per_pixel == 16) {
  254.                 pindex = regno << 3;
  255.  
  256.                 if (fb->depth == 16 && regno > 63)
  257.                         return -EINVAL;
  258.                 if (fb->depth == 15 && regno > 31)
  259.                         return -EINVAL;
  260.  
  261.                 if (fb->depth == 16) {
  262.                         u16 r, g, b;
  263.                         int i;
  264.                         if (regno < 32) {
  265.                                 for (i = 0; i < 8; i++)
  266.                                         fb_helper->funcs->gamma_set(crtc, red,
  267.                                                 green, blue, pindex + i);
  268.                         }
  269.  
  270.                         fb_helper->funcs->gamma_get(crtc, &r,
  271.                                                     &g, &b,
  272.                                                     pindex >> 1);
  273.  
  274.                         for (i = 0; i < 4; i++)
  275.                                 fb_helper->funcs->gamma_set(crtc, r,
  276.                                                             green, b,
  277.                                                             (pindex >> 1) + i);
  278.                 }
  279.         }
  280.  
  281.         if (fb->depth != 16)
  282.                 fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex);
  283.         return 0;
  284. }
  285.  
  286. int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
  287. {
  288.         struct drm_fb_helper *fb_helper = info->par;
  289.         struct drm_crtc_helper_funcs *crtc_funcs;
  290.         u16 *red, *green, *blue, *transp;
  291.         struct drm_crtc *crtc;
  292.         int i, j, rc = 0;
  293.         int start;
  294.  
  295.                 for (i = 0; i < fb_helper->crtc_count; i++) {
  296.                 crtc = fb_helper->crtc_info[i].mode_set.crtc;
  297.                 crtc_funcs = crtc->helper_private;
  298.  
  299.                 red = cmap->red;
  300.                 green = cmap->green;
  301.                 blue = cmap->blue;
  302.                 transp = cmap->transp;
  303.                 start = cmap->start;
  304.  
  305.                 for (j = 0; j < cmap->len; j++) {
  306.                         u16 hred, hgreen, hblue, htransp = 0xffff;
  307.  
  308.                         hred = *red++;
  309.                         hgreen = *green++;
  310.                         hblue = *blue++;
  311.  
  312.                         if (transp)
  313.                                 htransp = *transp++;
  314.  
  315.                         rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
  316.                         if (rc)
  317.                                 return rc;
  318.                 }
  319.                 crtc_funcs->load_lut(crtc);
  320.         }
  321.         return rc;
  322. }
  323. EXPORT_SYMBOL(drm_fb_helper_setcmap);
  324.  
  325. int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
  326.                             struct fb_info *info)
  327. {
  328.         struct drm_fb_helper *fb_helper = info->par;
  329.         struct drm_framebuffer *fb = fb_helper->fb;
  330.         int depth;
  331.  
  332.         if (var->pixclock != 0 || in_dbg_master())
  333.                 return -EINVAL;
  334.  
  335.         /* Need to resize the fb object !!! */
  336.         if (var->bits_per_pixel > fb->bits_per_pixel ||
  337.             var->xres > fb->width || var->yres > fb->height ||
  338.             var->xres_virtual > fb->width || var->yres_virtual > fb->height) {
  339.                 DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb "
  340.                           "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
  341.                           var->xres, var->yres, var->bits_per_pixel,
  342.                           var->xres_virtual, var->yres_virtual,
  343.                           fb->width, fb->height, fb->bits_per_pixel);
  344.                 return -EINVAL;
  345.         }
  346.  
  347.         switch (var->bits_per_pixel) {
  348.         case 16:
  349.                 depth = (var->green.length == 6) ? 16 : 15;
  350.                 break;
  351.         case 32:
  352.                 depth = (var->transp.length > 0) ? 32 : 24;
  353.                 break;
  354.         default:
  355.                 depth = var->bits_per_pixel;
  356.                 break;
  357.         }
  358.  
  359.         switch (depth) {
  360.         case 8:
  361.                 var->red.offset = 0;
  362.                 var->green.offset = 0;
  363.                 var->blue.offset = 0;
  364.                 var->red.length = 8;
  365.                 var->green.length = 8;
  366.                 var->blue.length = 8;
  367.                 var->transp.length = 0;
  368.                 var->transp.offset = 0;
  369.                 break;
  370.         case 15:
  371.                 var->red.offset = 10;
  372.                 var->green.offset = 5;
  373.                 var->blue.offset = 0;
  374.                 var->red.length = 5;
  375.                 var->green.length = 5;
  376.                 var->blue.length = 5;
  377.                 var->transp.length = 1;
  378.                 var->transp.offset = 15;
  379.                 break;
  380.         case 16:
  381.                 var->red.offset = 11;
  382.                 var->green.offset = 5;
  383.                 var->blue.offset = 0;
  384.                 var->red.length = 5;
  385.                 var->green.length = 6;
  386.                 var->blue.length = 5;
  387.                 var->transp.length = 0;
  388.                 var->transp.offset = 0;
  389.                 break;
  390.         case 24:
  391.                 var->red.offset = 16;
  392.                 var->green.offset = 8;
  393.                 var->blue.offset = 0;
  394.                 var->red.length = 8;
  395.                 var->green.length = 8;
  396.                 var->blue.length = 8;
  397.                 var->transp.length = 0;
  398.                 var->transp.offset = 0;
  399.                 break;
  400.         case 32:
  401.                 var->red.offset = 16;
  402.                 var->green.offset = 8;
  403.                 var->blue.offset = 0;
  404.                 var->red.length = 8;
  405.                 var->green.length = 8;
  406.                 var->blue.length = 8;
  407.                 var->transp.length = 8;
  408.                 var->transp.offset = 24;
  409.                 break;
  410.         default:
  411.                 return -EINVAL;
  412.         }
  413.         return 0;
  414. }
  415. EXPORT_SYMBOL(drm_fb_helper_check_var);
  416.  
  417. /* this will let fbcon do the mode init */
  418. int drm_fb_helper_set_par(struct fb_info *info)
  419. {
  420.         struct drm_fb_helper *fb_helper = info->par;
  421.         struct drm_device *dev = fb_helper->dev;
  422.         struct fb_var_screeninfo *var = &info->var;
  423.         struct drm_crtc *crtc;
  424.         int ret;
  425.         int i;
  426.  
  427.         if (var->pixclock != 0) {
  428.                 DRM_ERROR("PIXEL CLOCK SET\n");
  429.                 return -EINVAL;
  430.         }
  431.  
  432.         mutex_lock(&dev->mode_config.mutex);
  433.                 for (i = 0; i < fb_helper->crtc_count; i++) {
  434.                 crtc = fb_helper->crtc_info[i].mode_set.crtc;
  435.                         ret = crtc->funcs->set_config(&fb_helper->crtc_info[i].mode_set);
  436.                 if (ret) {
  437.                         mutex_unlock(&dev->mode_config.mutex);
  438.                                 return ret;
  439.                 }
  440.         }
  441.         mutex_unlock(&dev->mode_config.mutex);
  442.  
  443.         if (fb_helper->delayed_hotplug) {
  444.                 fb_helper->delayed_hotplug = false;
  445. //       drm_fb_helper_hotplug_event(fb_helper);
  446.         }
  447.         return 0;
  448. }
  449. EXPORT_SYMBOL(drm_fb_helper_set_par);
  450.  
  451. int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
  452.                               struct fb_info *info)
  453. {
  454.         struct drm_fb_helper *fb_helper = info->par;
  455.         struct drm_device *dev = fb_helper->dev;
  456.         struct drm_mode_set *modeset;
  457.         struct drm_crtc *crtc;
  458.         int ret = 0;
  459.         int i;
  460.  
  461.         mutex_lock(&dev->mode_config.mutex);
  462.                 for (i = 0; i < fb_helper->crtc_count; i++) {
  463.                 crtc = fb_helper->crtc_info[i].mode_set.crtc;
  464.  
  465.                 modeset = &fb_helper->crtc_info[i].mode_set;
  466.  
  467.                 modeset->x = var->xoffset;
  468.                 modeset->y = var->yoffset;
  469.  
  470.                 if (modeset->num_connectors) {
  471.                         ret = crtc->funcs->set_config(modeset);
  472.                         if (!ret) {
  473.                                 info->var.xoffset = var->xoffset;
  474.                                 info->var.yoffset = var->yoffset;
  475.                         }
  476.                 }
  477.         }
  478.         mutex_unlock(&dev->mode_config.mutex);
  479.         return ret;
  480. }
  481. EXPORT_SYMBOL(drm_fb_helper_pan_display);
  482.  
  483. int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
  484.                                   int preferred_bpp)
  485. {
  486.         int new_fb = 0;
  487.         int crtc_count = 0;
  488.         int i;
  489.         struct fb_info *info;
  490.         struct drm_fb_helper_surface_size sizes;
  491.         int gamma_size = 0;
  492.  
  493.         memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
  494.         sizes.surface_depth = 24;
  495.         sizes.surface_bpp = 32;
  496.         sizes.fb_width = (unsigned)-1;
  497.         sizes.fb_height = (unsigned)-1;
  498.  
  499.         /* if driver picks 8 or 16 by default use that
  500.            for both depth/bpp */
  501.         if (preferred_bpp != sizes.surface_bpp) {
  502.                 sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
  503.         }
  504.         /* first up get a count of crtcs now in use and new min/maxes width/heights */
  505.         for (i = 0; i < fb_helper->connector_count; i++) {
  506.                 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
  507.                 struct drm_cmdline_mode *cmdline_mode;
  508.  
  509.                 cmdline_mode = &fb_helper_conn->cmdline_mode;
  510.  
  511.                 if (cmdline_mode->bpp_specified) {
  512.                         switch (cmdline_mode->bpp) {
  513.                         case 8:
  514.                                 sizes.surface_depth = sizes.surface_bpp = 8;
  515.                                 break;
  516.                         case 15:
  517.                                 sizes.surface_depth = 15;
  518.                                 sizes.surface_bpp = 16;
  519.                                 break;
  520.                         case 16:
  521.                                 sizes.surface_depth = sizes.surface_bpp = 16;
  522.                                 break;
  523.                         case 24:
  524.                                 sizes.surface_depth = sizes.surface_bpp = 24;
  525.                                 break;
  526.                         case 32:
  527.     sizes.surface_depth = 24;
  528.     sizes.surface_bpp = 32;
  529.                                 break;
  530.                         }
  531.                         break;
  532.                 }
  533.         }
  534.  
  535.         crtc_count = 0;
  536.         for (i = 0; i < fb_helper->crtc_count; i++) {
  537.                 struct drm_display_mode *desired_mode;
  538.                 desired_mode = fb_helper->crtc_info[i].desired_mode;
  539.  
  540.                 if (desired_mode) {
  541.                         if (gamma_size == 0)
  542.                                 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
  543.                         if (desired_mode->hdisplay < sizes.fb_width)
  544.                                 sizes.fb_width = desired_mode->hdisplay;
  545.                         if (desired_mode->vdisplay < sizes.fb_height)
  546.                                 sizes.fb_height = desired_mode->vdisplay;
  547.                         if (desired_mode->hdisplay > sizes.surface_width)
  548.                                 sizes.surface_width = desired_mode->hdisplay;
  549.                         if (desired_mode->vdisplay > sizes.surface_height)
  550.                                 sizes.surface_height = desired_mode->vdisplay;
  551.                         crtc_count++;
  552.                 }
  553.         }
  554.  
  555.         if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
  556.                 /* hmm everyone went away - assume VGA cable just fell out
  557.                    and will come back later. */
  558.                 DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
  559.                 sizes.fb_width = sizes.surface_width = 1024;
  560.                 sizes.fb_height = sizes.surface_height = 768;
  561.         }
  562.  
  563.         /* push down into drivers */
  564.     new_fb = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
  565.         if (new_fb < 0)
  566.                 return new_fb;
  567.  
  568.         info = fb_helper->fbdev;
  569.  
  570.         /* set the fb pointer */
  571.         for (i = 0; i < fb_helper->crtc_count; i++) {
  572.                 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
  573.                 }
  574.  
  575.         if (new_fb) {
  576.                 info->var.pixclock = 0;
  577.  
  578.                 printk(KERN_INFO "fb%d: %s frame buffer device\n", info->node,
  579.                        info->fix.id);
  580.  
  581.         } else {
  582.                 drm_fb_helper_set_par(info);
  583.         }
  584.  
  585.  
  586.         if (new_fb)
  587.         list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
  588.  
  589.         return 0;
  590. }
  591. EXPORT_SYMBOL(drm_fb_helper_single_fb_probe);
  592.  
  593. void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
  594.                             uint32_t depth)
  595. {
  596.         info->fix.type = FB_TYPE_PACKED_PIXELS;
  597.         info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
  598.                 FB_VISUAL_TRUECOLOR;
  599.         info->fix.mmio_start = 0;
  600.         info->fix.mmio_len = 0;
  601.         info->fix.type_aux = 0;
  602.         info->fix.xpanstep = 1; /* doing it in hw */
  603.         info->fix.ypanstep = 1; /* doing it in hw */
  604.         info->fix.ywrapstep = 0;
  605.         info->fix.accel = FB_ACCEL_NONE;
  606.         info->fix.type_aux = 0;
  607.  
  608.         info->fix.line_length = pitch;
  609.         return;
  610. }
  611. EXPORT_SYMBOL(drm_fb_helper_fill_fix);
  612.  
  613. void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
  614.                             uint32_t fb_width, uint32_t fb_height)
  615. {
  616.         struct drm_framebuffer *fb = fb_helper->fb;
  617.         info->pseudo_palette = fb_helper->pseudo_palette;
  618.         info->var.xres_virtual = fb->width;
  619.         info->var.yres_virtual = fb->height;
  620.         info->var.bits_per_pixel = fb->bits_per_pixel;
  621.         info->var.accel_flags = FB_ACCELF_TEXT;
  622.         info->var.xoffset = 0;
  623.         info->var.yoffset = 0;
  624.         info->var.activate = FB_ACTIVATE_NOW;
  625.         info->var.height = -1;
  626.         info->var.width = -1;
  627.  
  628.         switch (fb->depth) {
  629.         case 8:
  630.                 info->var.red.offset = 0;
  631.                 info->var.green.offset = 0;
  632.                 info->var.blue.offset = 0;
  633.                 info->var.red.length = 8; /* 8bit DAC */
  634.                 info->var.green.length = 8;
  635.                 info->var.blue.length = 8;
  636.                 info->var.transp.offset = 0;
  637.                 info->var.transp.length = 0;
  638.                 break;
  639.         case 15:
  640.                 info->var.red.offset = 10;
  641.                 info->var.green.offset = 5;
  642.                 info->var.blue.offset = 0;
  643.                 info->var.red.length = 5;
  644.                 info->var.green.length = 5;
  645.                 info->var.blue.length = 5;
  646.                 info->var.transp.offset = 15;
  647.                 info->var.transp.length = 1;
  648.                 break;
  649.         case 16:
  650.                 info->var.red.offset = 11;
  651.                 info->var.green.offset = 5;
  652.                 info->var.blue.offset = 0;
  653.                 info->var.red.length = 5;
  654.                 info->var.green.length = 6;
  655.                 info->var.blue.length = 5;
  656.                 info->var.transp.offset = 0;
  657.                 break;
  658.         case 24:
  659.                 info->var.red.offset = 16;
  660.                 info->var.green.offset = 8;
  661.                 info->var.blue.offset = 0;
  662.                 info->var.red.length = 8;
  663.                 info->var.green.length = 8;
  664.                 info->var.blue.length = 8;
  665.                 info->var.transp.offset = 0;
  666.                 info->var.transp.length = 0;
  667.                 break;
  668.         case 32:
  669.                 info->var.red.offset = 16;
  670.                 info->var.green.offset = 8;
  671.                 info->var.blue.offset = 0;
  672.                 info->var.red.length = 8;
  673.                 info->var.green.length = 8;
  674.                 info->var.blue.length = 8;
  675.                 info->var.transp.offset = 24;
  676.                 info->var.transp.length = 8;
  677.                 break;
  678.         default:
  679.                 break;
  680.         }
  681.  
  682.         info->var.xres = fb_width;
  683.         info->var.yres = fb_height;
  684. }
  685. EXPORT_SYMBOL(drm_fb_helper_fill_var);
  686.  
  687. static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
  688.                                                uint32_t maxX,
  689.                                                uint32_t maxY)
  690. {
  691.         struct drm_connector *connector;
  692.         int count = 0;
  693.         int i;
  694.  
  695.         for (i = 0; i < fb_helper->connector_count; i++) {
  696.                 connector = fb_helper->connector_info[i]->connector;
  697.                 count += connector->funcs->fill_modes(connector, maxX, maxY);
  698.         }
  699.  
  700.         return count;
  701. }
  702.  
  703. static struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height)
  704. {
  705.         struct drm_display_mode *mode;
  706.  
  707.         list_for_each_entry(mode, &fb_connector->connector->modes, head) {
  708.                 if (drm_mode_width(mode) > width ||
  709.                     drm_mode_height(mode) > height)
  710.                         continue;
  711.                 if (mode->type & DRM_MODE_TYPE_PREFERRED)
  712.                         return mode;
  713.         }
  714.         return NULL;
  715. }
  716.  
  717. static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
  718. {
  719.         struct drm_cmdline_mode *cmdline_mode;
  720.         cmdline_mode = &fb_connector->cmdline_mode;
  721.         return cmdline_mode->specified;
  722. }
  723.  
  724.  
  725. static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
  726. {
  727.         bool enable;
  728.  
  729.         if (strict) {
  730.                 enable = connector->status == connector_status_connected;
  731.         } else {
  732.                 enable = connector->status != connector_status_disconnected;
  733.         }
  734.         return enable;
  735. }
  736.  
  737. static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
  738.                                   bool *enabled)
  739. {
  740.         bool any_enabled = false;
  741.         struct drm_connector *connector;
  742.         int i = 0;
  743.  
  744.         for (i = 0; i < fb_helper->connector_count; i++) {
  745.                 connector = fb_helper->connector_info[i]->connector;
  746.                 enabled[i] = drm_connector_enabled(connector, true);
  747.                 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
  748.                           enabled[i] ? "yes" : "no");
  749.                 any_enabled |= enabled[i];
  750.         }
  751.  
  752.         if (any_enabled)
  753.                 return;
  754.  
  755.         for (i = 0; i < fb_helper->connector_count; i++) {
  756.                 connector = fb_helper->connector_info[i]->connector;
  757.                 enabled[i] = drm_connector_enabled(connector, false);
  758.         }
  759. }
  760.  
  761.  
  762. static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
  763.                                  struct drm_display_mode **modes,
  764.                                  bool *enabled, int width, int height)
  765. {
  766.         struct drm_fb_helper_connector *fb_helper_conn;
  767.         int i;
  768.  
  769.         for (i = 0; i < fb_helper->connector_count; i++) {
  770.                 fb_helper_conn = fb_helper->connector_info[i];
  771.  
  772.                 if (enabled[i] == false)
  773.                         continue;
  774.  
  775.                 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
  776.                               fb_helper_conn->connector->base.id);
  777.  
  778.                 /* got for command line mode first */
  779. //       modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
  780.  
  781.         modes[i] = NULL;
  782.  
  783.                 if (!modes[i]) {
  784.                         DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
  785.                                       fb_helper_conn->connector->base.id);
  786.                         modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
  787.                 }
  788.                 /* No preferred modes, pick one off the list */
  789.                 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
  790.                         list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
  791.                                 break;
  792.                 }
  793.                 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
  794.                           "none");
  795.         }
  796.         return true;
  797. }
  798.  
  799. static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
  800.                           struct drm_fb_helper_crtc **best_crtcs,
  801.                           struct drm_display_mode **modes,
  802.                           int n, int width, int height)
  803. {
  804.         int c, o;
  805.         struct drm_device *dev = fb_helper->dev;
  806.         struct drm_connector *connector;
  807.         struct drm_connector_helper_funcs *connector_funcs;
  808.         struct drm_encoder *encoder;
  809.         struct drm_fb_helper_crtc *best_crtc;
  810.         int my_score, best_score, score;
  811.         struct drm_fb_helper_crtc **crtcs, *crtc;
  812.         struct drm_fb_helper_connector *fb_helper_conn;
  813.  
  814.         if (n == fb_helper->connector_count)
  815.                 return 0;
  816.  
  817.         fb_helper_conn = fb_helper->connector_info[n];
  818.         connector = fb_helper_conn->connector;
  819.  
  820.         best_crtcs[n] = NULL;
  821.         best_crtc = NULL;
  822.         best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
  823.         if (modes[n] == NULL)
  824.                 return best_score;
  825.  
  826.         crtcs = kzalloc(dev->mode_config.num_connector *
  827.                         sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
  828.         if (!crtcs)
  829.                 return best_score;
  830.  
  831.         my_score = 1;
  832.         if (connector->status == connector_status_connected)
  833.                 my_score++;
  834.         if (drm_has_cmdline_mode(fb_helper_conn))
  835.                 my_score++;
  836.         if (drm_has_preferred_mode(fb_helper_conn, width, height))
  837.                 my_score++;
  838.  
  839.         connector_funcs = connector->helper_private;
  840.         encoder = connector_funcs->best_encoder(connector);
  841.         if (!encoder)
  842.                 goto out;
  843.  
  844.         /* select a crtc for this connector and then attempt to configure
  845.            remaining connectors */
  846.         for (c = 0; c < fb_helper->crtc_count; c++) {
  847.                 crtc = &fb_helper->crtc_info[c];
  848.  
  849.                 if ((encoder->possible_crtcs & (1 << c)) == 0) {
  850.                         continue;
  851.                 }
  852.  
  853.                 for (o = 0; o < n; o++)
  854.                         if (best_crtcs[o] == crtc)
  855.                                 break;
  856.  
  857.                 if (o < n) {
  858.                         /* ignore cloning unless only a single crtc */
  859.                         if (fb_helper->crtc_count > 1)
  860.                                 continue;
  861.  
  862.                         if (!drm_mode_equal(modes[o], modes[n]))
  863.                                 continue;
  864.                 }
  865.  
  866.                 crtcs[n] = crtc;
  867.                 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
  868.                 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
  869.                                                   width, height);
  870.                 if (score > best_score) {
  871.                         best_crtc = crtc;
  872.                         best_score = score;
  873.                         memcpy(best_crtcs, crtcs,
  874.                                dev->mode_config.num_connector *
  875.                                sizeof(struct drm_fb_helper_crtc *));
  876.                 }
  877.         }
  878. out:
  879.         kfree(crtcs);
  880.         return best_score;
  881. }
  882.  
  883. static void drm_setup_crtcs(struct drm_fb_helper *fb_helper)
  884. {
  885.         struct drm_device *dev = fb_helper->dev;
  886.         struct drm_fb_helper_crtc **crtcs;
  887.         struct drm_display_mode **modes;
  888.         struct drm_mode_set *modeset;
  889.         bool *enabled;
  890.         int width, height;
  891.         int i, ret;
  892.  
  893.         DRM_DEBUG_KMS("\n");
  894.  
  895.         width = dev->mode_config.max_width;
  896.         height = dev->mode_config.max_height;
  897.  
  898.         crtcs = kcalloc(dev->mode_config.num_connector,
  899.                         sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
  900.         modes = kcalloc(dev->mode_config.num_connector,
  901.                         sizeof(struct drm_display_mode *), GFP_KERNEL);
  902.         enabled = kcalloc(dev->mode_config.num_connector,
  903.                           sizeof(bool), GFP_KERNEL);
  904.  
  905.         drm_enable_connectors(fb_helper, enabled);
  906.  
  907.     //ret = drm_target_cloned(fb_helper, modes, enabled, width, height);
  908.  
  909.     ret = 0;
  910.  
  911.         if (!ret) {
  912.                 ret = drm_target_preferred(fb_helper, modes, enabled, width, height);
  913.                 if (!ret)
  914.                         DRM_ERROR("Unable to find initial modes\n");
  915.         }
  916.  
  917.         DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", width, height);
  918.  
  919.         drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
  920.  
  921.         /* need to set the modesets up here for use later */
  922.         /* fill out the connector<->crtc mappings into the modesets */
  923.         for (i = 0; i < fb_helper->crtc_count; i++) {
  924.                 modeset = &fb_helper->crtc_info[i].mode_set;
  925.                 modeset->num_connectors = 0;
  926.         }
  927.  
  928.         for (i = 0; i < fb_helper->connector_count; i++) {
  929.                 struct drm_display_mode *mode = modes[i];
  930.                 struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
  931.                 modeset = &fb_crtc->mode_set;
  932.  
  933.                 if (mode && fb_crtc) {
  934.                         DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
  935.                                       mode->name, fb_crtc->mode_set.crtc->base.id);
  936.                         fb_crtc->desired_mode = mode;
  937.                         if (modeset->mode)
  938.                                 drm_mode_destroy(dev, modeset->mode);
  939.                         modeset->mode = drm_mode_duplicate(dev,
  940.                                                            fb_crtc->desired_mode);
  941.                         modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector;
  942.                 }
  943.         }
  944.  
  945.         kfree(crtcs);
  946.         kfree(modes);
  947.         kfree(enabled);
  948. }
  949.  
  950. /**
  951.  * drm_helper_initial_config - setup a sane initial connector configuration
  952.  * @dev: DRM device
  953.  *
  954.  * LOCKING:
  955.  * Called at init time, must take mode config lock.
  956.  *
  957.  * Scan the CRTCs and connectors and try to put together an initial setup.
  958.  * At the moment, this is a cloned configuration across all heads with
  959.  * a new framebuffer object as the backing store.
  960.  *
  961.  * RETURNS:
  962.  * Zero if everything went ok, nonzero otherwise.
  963.  */
  964. bool drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
  965. {
  966.         struct drm_device *dev = fb_helper->dev;
  967.         int count = 0;
  968.  
  969.         /* disable all the possible outputs/crtcs before entering KMS mode */
  970.         drm_helper_disable_unused_functions(fb_helper->dev);
  971.  
  972. //   drm_fb_helper_parse_command_line(fb_helper);
  973.  
  974.         count = drm_fb_helper_probe_connector_modes(fb_helper,
  975.                                                     dev->mode_config.max_width,
  976.                                                     dev->mode_config.max_height);
  977.         /*
  978.          * we shouldn't end up with no modes here.
  979.          */
  980.         if (count == 0) {
  981.                 printk(KERN_INFO "No connectors reported connected with modes\n");
  982.         }
  983.         drm_setup_crtcs(fb_helper);
  984.  
  985.         return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
  986. }
  987. EXPORT_SYMBOL(drm_fb_helper_initial_config);
  988.