Subversion Repositories Kolibri OS

Rev

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