Subversion Repositories Kolibri OS

Rev

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

  1. /*
  2.  * Copyright 2007-8 Advanced Micro Devices, Inc.
  3.  * Copyright 2008 Red Hat Inc.
  4.  *
  5.  * Permission is hereby granted, free of charge, to any person obtaining a
  6.  * copy of this software and associated documentation files (the "Software"),
  7.  * to deal in the Software without restriction, including without limitation
  8.  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9.  * and/or sell copies of the Software, and to permit persons to whom the
  10.  * Software is furnished to do so, subject to the following conditions:
  11.  *
  12.  * The above copyright notice and this permission notice shall be included in
  13.  * all copies or substantial portions of the Software.
  14.  *
  15.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  18.  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  19.  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  20.  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21.  * OTHER DEALINGS IN THE SOFTWARE.
  22.  *
  23.  * Authors: Dave Airlie
  24.  *          Alex Deucher
  25.  */
  26. #include "drmP.h"
  27. #include "radeon_drm.h"
  28. #include "radeon.h"
  29.  
  30. #include "atom.h"
  31. //#include <asm/div64.h>
  32.  
  33. #include "drm_crtc_helper.h"
  34. #include "drm_edid.h"
  35.  
  36. static int radeon_ddc_dump(struct drm_connector *connector);
  37.  
  38. static void avivo_crtc_load_lut(struct drm_crtc *crtc)
  39. {
  40.         struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  41.         struct drm_device *dev = crtc->dev;
  42.         struct radeon_device *rdev = dev->dev_private;
  43.         int i;
  44.  
  45.         DRM_DEBUG("%d\n", radeon_crtc->crtc_id);
  46.         WREG32(AVIVO_DC_LUTA_CONTROL + radeon_crtc->crtc_offset, 0);
  47.  
  48.         WREG32(AVIVO_DC_LUTA_BLACK_OFFSET_BLUE + radeon_crtc->crtc_offset, 0);
  49.         WREG32(AVIVO_DC_LUTA_BLACK_OFFSET_GREEN + radeon_crtc->crtc_offset, 0);
  50.         WREG32(AVIVO_DC_LUTA_BLACK_OFFSET_RED + radeon_crtc->crtc_offset, 0);
  51.  
  52.         WREG32(AVIVO_DC_LUTA_WHITE_OFFSET_BLUE + radeon_crtc->crtc_offset, 0xffff);
  53.         WREG32(AVIVO_DC_LUTA_WHITE_OFFSET_GREEN + radeon_crtc->crtc_offset, 0xffff);
  54.         WREG32(AVIVO_DC_LUTA_WHITE_OFFSET_RED + radeon_crtc->crtc_offset, 0xffff);
  55.  
  56.         WREG32(AVIVO_DC_LUT_RW_SELECT, radeon_crtc->crtc_id);
  57.         WREG32(AVIVO_DC_LUT_RW_MODE, 0);
  58.         WREG32(AVIVO_DC_LUT_WRITE_EN_MASK, 0x0000003f);
  59.  
  60.         WREG8(AVIVO_DC_LUT_RW_INDEX, 0);
  61.         for (i = 0; i < 256; i++) {
  62.                 WREG32(AVIVO_DC_LUT_30_COLOR,
  63.                              (radeon_crtc->lut_r[i] << 20) |
  64.                              (radeon_crtc->lut_g[i] << 10) |
  65.                              (radeon_crtc->lut_b[i] << 0));
  66.         }
  67.  
  68.         WREG32(AVIVO_D1GRPH_LUT_SEL + radeon_crtc->crtc_offset, radeon_crtc->crtc_id);
  69. }
  70.  
  71. static void legacy_crtc_load_lut(struct drm_crtc *crtc)
  72. {
  73.         struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  74.         struct drm_device *dev = crtc->dev;
  75.         struct radeon_device *rdev = dev->dev_private;
  76.         int i;
  77.         uint32_t dac2_cntl;
  78.  
  79.         dac2_cntl = RREG32(RADEON_DAC_CNTL2);
  80.         if (radeon_crtc->crtc_id == 0)
  81.                 dac2_cntl &= (uint32_t)~RADEON_DAC2_PALETTE_ACC_CTL;
  82.         else
  83.                 dac2_cntl |= RADEON_DAC2_PALETTE_ACC_CTL;
  84.         WREG32(RADEON_DAC_CNTL2, dac2_cntl);
  85.  
  86.         WREG8(RADEON_PALETTE_INDEX, 0);
  87.         for (i = 0; i < 256; i++) {
  88.                 WREG32(RADEON_PALETTE_30_DATA,
  89.                              (radeon_crtc->lut_r[i] << 20) |
  90.                              (radeon_crtc->lut_g[i] << 10) |
  91.                              (radeon_crtc->lut_b[i] << 0));
  92.         }
  93. }
  94.  
  95. void radeon_crtc_load_lut(struct drm_crtc *crtc)
  96. {
  97.         struct drm_device *dev = crtc->dev;
  98.         struct radeon_device *rdev = dev->dev_private;
  99.  
  100.         if (!crtc->enabled)
  101.                 return;
  102.  
  103.         if (ASIC_IS_AVIVO(rdev))
  104.                 avivo_crtc_load_lut(crtc);
  105.         else
  106.                 legacy_crtc_load_lut(crtc);
  107. }
  108.  
  109. /** Sets the color ramps on behalf of RandR */
  110. void radeon_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
  111.                               u16 blue, int regno)
  112. {
  113.         struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  114.  
  115.         if (regno == 0)
  116.                 DRM_DEBUG("gamma set %d\n", radeon_crtc->crtc_id);
  117.         radeon_crtc->lut_r[regno] = red >> 6;
  118.         radeon_crtc->lut_g[regno] = green >> 6;
  119.         radeon_crtc->lut_b[regno] = blue >> 6;
  120. }
  121.  
  122. static void radeon_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  123.                                   u16 *blue, uint32_t size)
  124. {
  125.         struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  126.         int i, j;
  127.  
  128.         if (size != 256) {
  129.                 return;
  130.         }
  131.         if (crtc->fb == NULL) {
  132.                 return;
  133.         }
  134.  
  135.         if (crtc->fb->depth == 16) {
  136.                 for (i = 0; i < 64; i++) {
  137.                         if (i <= 31) {
  138.                                 for (j = 0; j < 8; j++) {
  139.                                         radeon_crtc->lut_r[i * 8 + j] = red[i] >> 6;
  140.                                         radeon_crtc->lut_b[i * 8 + j] = blue[i] >> 6;
  141.                                 }
  142.                         }
  143.                         for (j = 0; j < 4; j++)
  144.                                 radeon_crtc->lut_g[i * 4 + j] = green[i] >> 6;
  145.                 }
  146.         } else {
  147.                 for (i = 0; i < 256; i++) {
  148.                         radeon_crtc->lut_r[i] = red[i] >> 6;
  149.                         radeon_crtc->lut_g[i] = green[i] >> 6;
  150.                         radeon_crtc->lut_b[i] = blue[i] >> 6;
  151.                 }
  152.         }
  153.  
  154.         radeon_crtc_load_lut(crtc);
  155. }
  156.  
  157. static void radeon_crtc_destroy(struct drm_crtc *crtc)
  158. {
  159.         struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  160.  
  161.         if (radeon_crtc->mode_set.mode) {
  162.                 drm_mode_destroy(crtc->dev, radeon_crtc->mode_set.mode);
  163.         }
  164.         drm_crtc_cleanup(crtc);
  165.         kfree(radeon_crtc);
  166. }
  167.  
  168. static const struct drm_crtc_funcs radeon_crtc_funcs = {
  169. //   .cursor_set = radeon_crtc_cursor_set,
  170. //   .cursor_move = radeon_crtc_cursor_move,
  171.         .gamma_set = radeon_crtc_gamma_set,
  172.         .set_config = drm_crtc_helper_set_config,
  173.         .destroy = radeon_crtc_destroy,
  174. };
  175.  
  176. static void radeon_crtc_init(struct drm_device *dev, int index)
  177. {
  178.         struct radeon_device *rdev = dev->dev_private;
  179.         struct radeon_crtc *radeon_crtc;
  180.         int i;
  181.  
  182.     ENTRY();
  183.  
  184.         radeon_crtc = kzalloc(sizeof(struct radeon_crtc) + (RADEONFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
  185.         if (radeon_crtc == NULL)
  186.                 return;
  187.  
  188.         drm_crtc_init(dev, &radeon_crtc->base, &radeon_crtc_funcs);
  189.  
  190.         drm_mode_crtc_set_gamma_size(&radeon_crtc->base, 256);
  191.         radeon_crtc->crtc_id = index;
  192.  
  193.         radeon_crtc->mode_set.crtc = &radeon_crtc->base;
  194.         radeon_crtc->mode_set.connectors = (struct drm_connector **)(radeon_crtc + 1);
  195.         radeon_crtc->mode_set.num_connectors = 0;
  196.  
  197.         for (i = 0; i < 256; i++) {
  198.                 radeon_crtc->lut_r[i] = i << 2;
  199.                 radeon_crtc->lut_g[i] = i << 2;
  200.                 radeon_crtc->lut_b[i] = i << 2;
  201.         }
  202.  
  203.         if (rdev->is_atom_bios && (ASIC_IS_AVIVO(rdev) || radeon_r4xx_atom))
  204.                 radeon_atombios_init_crtc(dev, radeon_crtc);
  205.         else
  206.                 radeon_legacy_init_crtc(dev, radeon_crtc);
  207.  
  208.     LEAVE();
  209. }
  210.  
  211. static const char *encoder_names[34] = {
  212.         "NONE",
  213.         "INTERNAL_LVDS",
  214.         "INTERNAL_TMDS1",
  215.         "INTERNAL_TMDS2",
  216.         "INTERNAL_DAC1",
  217.         "INTERNAL_DAC2",
  218.         "INTERNAL_SDVOA",
  219.         "INTERNAL_SDVOB",
  220.         "SI170B",
  221.         "CH7303",
  222.         "CH7301",
  223.         "INTERNAL_DVO1",
  224.         "EXTERNAL_SDVOA",
  225.         "EXTERNAL_SDVOB",
  226.         "TITFP513",
  227.         "INTERNAL_LVTM1",
  228.         "VT1623",
  229.         "HDMI_SI1930",
  230.         "HDMI_INTERNAL",
  231.         "INTERNAL_KLDSCP_TMDS1",
  232.         "INTERNAL_KLDSCP_DVO1",
  233.         "INTERNAL_KLDSCP_DAC1",
  234.         "INTERNAL_KLDSCP_DAC2",
  235.         "SI178",
  236.         "MVPU_FPGA",
  237.         "INTERNAL_DDI",
  238.         "VT1625",
  239.         "HDMI_SI1932",
  240.         "DP_AN9801",
  241.         "DP_DP501",
  242.         "INTERNAL_UNIPHY",
  243.         "INTERNAL_KLDSCP_LVTMA",
  244.         "INTERNAL_UNIPHY1",
  245.         "INTERNAL_UNIPHY2",
  246. };
  247.  
  248. static const char *connector_names[13] = {
  249.         "Unknown",
  250.         "VGA",
  251.         "DVI-I",
  252.         "DVI-D",
  253.         "DVI-A",
  254.         "Composite",
  255.         "S-video",
  256.         "LVDS",
  257.         "Component",
  258.         "DIN",
  259.         "DisplayPort",
  260.         "HDMI-A",
  261.         "HDMI-B",
  262. };
  263.  
  264. static void radeon_print_display_setup(struct drm_device *dev)
  265. {
  266.         struct drm_connector *connector;
  267.         struct radeon_connector *radeon_connector;
  268.         struct drm_encoder *encoder;
  269.         struct radeon_encoder *radeon_encoder;
  270.         uint32_t devices;
  271.         int i = 0;
  272.  
  273.         DRM_INFO("Radeon Display Connectors\n");
  274.         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  275.                 radeon_connector = to_radeon_connector(connector);
  276.                 DRM_INFO("Connector %d:\n", i);
  277.                 DRM_INFO("  %s\n", connector_names[connector->connector_type]);
  278.                 if (radeon_connector->ddc_bus)
  279.                         DRM_INFO("  DDC: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",
  280.                                  radeon_connector->ddc_bus->rec.mask_clk_reg,
  281.                                  radeon_connector->ddc_bus->rec.mask_data_reg,
  282.                                  radeon_connector->ddc_bus->rec.a_clk_reg,
  283.                                  radeon_connector->ddc_bus->rec.a_data_reg,
  284.                                  radeon_connector->ddc_bus->rec.put_clk_reg,
  285.                                  radeon_connector->ddc_bus->rec.put_data_reg,
  286.                                  radeon_connector->ddc_bus->rec.get_clk_reg,
  287.                                  radeon_connector->ddc_bus->rec.get_data_reg);
  288.                 DRM_INFO("  Encoders:\n");
  289.                 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  290.                         radeon_encoder = to_radeon_encoder(encoder);
  291.                         devices = radeon_encoder->devices & radeon_connector->devices;
  292.                         if (devices) {
  293.                                 if (devices & ATOM_DEVICE_CRT1_SUPPORT)
  294.                                         DRM_INFO("    CRT1: %s\n", encoder_names[radeon_encoder->encoder_id]);
  295.                                 if (devices & ATOM_DEVICE_CRT2_SUPPORT)
  296.                                         DRM_INFO("    CRT2: %s\n", encoder_names[radeon_encoder->encoder_id]);
  297.                                 if (devices & ATOM_DEVICE_LCD1_SUPPORT)
  298.                                         DRM_INFO("    LCD1: %s\n", encoder_names[radeon_encoder->encoder_id]);
  299.                                 if (devices & ATOM_DEVICE_DFP1_SUPPORT)
  300.                                         DRM_INFO("    DFP1: %s\n", encoder_names[radeon_encoder->encoder_id]);
  301.                                 if (devices & ATOM_DEVICE_DFP2_SUPPORT)
  302.                                         DRM_INFO("    DFP2: %s\n", encoder_names[radeon_encoder->encoder_id]);
  303.                                 if (devices & ATOM_DEVICE_DFP3_SUPPORT)
  304.                                         DRM_INFO("    DFP3: %s\n", encoder_names[radeon_encoder->encoder_id]);
  305.                                 if (devices & ATOM_DEVICE_DFP4_SUPPORT)
  306.                                         DRM_INFO("    DFP4: %s\n", encoder_names[radeon_encoder->encoder_id]);
  307.                                 if (devices & ATOM_DEVICE_DFP5_SUPPORT)
  308.                                         DRM_INFO("    DFP5: %s\n", encoder_names[radeon_encoder->encoder_id]);
  309.                                 if (devices & ATOM_DEVICE_TV1_SUPPORT)
  310.                                         DRM_INFO("    TV1: %s\n", encoder_names[radeon_encoder->encoder_id]);
  311.                                 if (devices & ATOM_DEVICE_CV_SUPPORT)
  312.                                         DRM_INFO("    CV: %s\n", encoder_names[radeon_encoder->encoder_id]);
  313.                         }
  314.                 }
  315.                 i++;
  316.         }
  317. }
  318.  
  319. bool radeon_setup_enc_conn(struct drm_device *dev)
  320. {
  321.         struct radeon_device *rdev = dev->dev_private;
  322.         struct drm_connector *drm_connector;
  323.         bool ret = false;
  324.  
  325.     ENTRY();
  326.  
  327.         if (rdev->bios) {
  328.                 if (rdev->is_atom_bios) {
  329.                         if (rdev->family >= CHIP_R600)
  330.                                 ret = radeon_get_atom_connector_info_from_object_table(dev);
  331.                         else
  332.                                 ret = radeon_get_atom_connector_info_from_supported_devices_table(dev);
  333.                 } else
  334.                         ret = radeon_get_legacy_connector_info_from_bios(dev);
  335.         } else {
  336.                 if (!ASIC_IS_AVIVO(rdev))
  337.                         ret = radeon_get_legacy_connector_info_from_table(dev);
  338.         }
  339.         if (ret) {
  340.                 radeon_print_display_setup(dev);
  341.                 list_for_each_entry(drm_connector, &dev->mode_config.connector_list, head)
  342.                         radeon_ddc_dump(drm_connector);
  343.         }
  344.     LEAVE();
  345.  
  346.         return ret;
  347. }
  348.  
  349. int radeon_ddc_get_modes(struct radeon_connector *radeon_connector)
  350. {
  351.         struct edid *edid;
  352.         int ret = 0;
  353.  
  354.         if (!radeon_connector->ddc_bus)
  355.                 return -1;
  356.         radeon_i2c_do_lock(radeon_connector, 1);
  357.         edid = drm_get_edid(&radeon_connector->base, &radeon_connector->ddc_bus->adapter);
  358.         radeon_i2c_do_lock(radeon_connector, 0);
  359.         if (edid) {
  360.                 /* update digital bits here */
  361.                 if (edid->input & DRM_EDID_INPUT_DIGITAL)
  362.                         radeon_connector->use_digital = 1;
  363.                 else
  364.                         radeon_connector->use_digital = 0;
  365.                 drm_mode_connector_update_edid_property(&radeon_connector->base, edid);
  366.                 ret = drm_add_edid_modes(&radeon_connector->base, edid);
  367.                 kfree(edid);
  368.                 return ret;
  369.         }
  370.         drm_mode_connector_update_edid_property(&radeon_connector->base, NULL);
  371.         return -1;
  372. }
  373.  
  374. static int radeon_ddc_dump(struct drm_connector *connector)
  375. {
  376.         struct edid *edid;
  377.         struct radeon_connector *radeon_connector = to_radeon_connector(connector);
  378.         int ret = 0;
  379.  
  380.         if (!radeon_connector->ddc_bus)
  381.                 return -1;
  382.         radeon_i2c_do_lock(radeon_connector, 1);
  383.         edid = drm_get_edid(connector, &radeon_connector->ddc_bus->adapter);
  384.         radeon_i2c_do_lock(radeon_connector, 0);
  385.         if (edid) {
  386.                 kfree(edid);
  387.         }
  388.         return ret;
  389. }
  390.  
  391. static inline uint32_t radeon_div(uint64_t n, uint32_t d)
  392. {
  393.         uint64_t mod;
  394.  
  395.         n += d / 2;
  396.  
  397.         mod = do_div(n, d);
  398.         return n;
  399. }
  400.  
  401. void radeon_compute_pll(struct radeon_pll *pll,
  402.                         uint64_t freq,
  403.                         uint32_t *dot_clock_p,
  404.                         uint32_t *fb_div_p,
  405.                         uint32_t *frac_fb_div_p,
  406.                         uint32_t *ref_div_p,
  407.                         uint32_t *post_div_p,
  408.                         int flags)
  409. {
  410.         uint32_t min_ref_div = pll->min_ref_div;
  411.         uint32_t max_ref_div = pll->max_ref_div;
  412.         uint32_t min_fractional_feed_div = 0;
  413.         uint32_t max_fractional_feed_div = 0;
  414.         uint32_t best_vco = pll->best_vco;
  415.         uint32_t best_post_div = 1;
  416.         uint32_t best_ref_div = 1;
  417.         uint32_t best_feedback_div = 1;
  418.         uint32_t best_frac_feedback_div = 0;
  419.         uint32_t best_freq = -1;
  420.         uint32_t best_error = 0xffffffff;
  421.         uint32_t best_vco_diff = 1;
  422.         uint32_t post_div;
  423.  
  424.         DRM_DEBUG("PLL freq %llu %u %u\n", freq, pll->min_ref_div, pll->max_ref_div);
  425.         freq = freq * 1000;
  426.  
  427.         if (flags & RADEON_PLL_USE_REF_DIV)
  428.                 min_ref_div = max_ref_div = pll->reference_div;
  429.         else {
  430.                 while (min_ref_div < max_ref_div-1) {
  431.                         uint32_t mid = (min_ref_div + max_ref_div) / 2;
  432.                         uint32_t pll_in = pll->reference_freq / mid;
  433.                         if (pll_in < pll->pll_in_min)
  434.                                 max_ref_div = mid;
  435.                         else if (pll_in > pll->pll_in_max)
  436.                                 min_ref_div = mid;
  437.                         else
  438.                                 break;
  439.                 }
  440.         }
  441.  
  442.         if (flags & RADEON_PLL_USE_FRAC_FB_DIV) {
  443.                 min_fractional_feed_div = pll->min_frac_feedback_div;
  444.                 max_fractional_feed_div = pll->max_frac_feedback_div;
  445.         }
  446.  
  447.         for (post_div = pll->min_post_div; post_div <= pll->max_post_div; ++post_div) {
  448.                 uint32_t ref_div;
  449.  
  450.                 if ((flags & RADEON_PLL_NO_ODD_POST_DIV) && (post_div & 1))
  451.                         continue;
  452.  
  453.                 /* legacy radeons only have a few post_divs */
  454.                 if (flags & RADEON_PLL_LEGACY) {
  455.                         if ((post_div == 5) ||
  456.                             (post_div == 7) ||
  457.                             (post_div == 9) ||
  458.                             (post_div == 10) ||
  459.                             (post_div == 11) ||
  460.                             (post_div == 13) ||
  461.                             (post_div == 14) ||
  462.                             (post_div == 15))
  463.                                 continue;
  464.                 }
  465.  
  466.                 for (ref_div = min_ref_div; ref_div <= max_ref_div; ++ref_div) {
  467.                         uint32_t feedback_div, current_freq = 0, error, vco_diff;
  468.                         uint32_t pll_in = pll->reference_freq / ref_div;
  469.                         uint32_t min_feed_div = pll->min_feedback_div;
  470.                         uint32_t max_feed_div = pll->max_feedback_div + 1;
  471.  
  472.                         if (pll_in < pll->pll_in_min || pll_in > pll->pll_in_max)
  473.                                 continue;
  474.  
  475.                         while (min_feed_div < max_feed_div) {
  476.                                 uint32_t vco;
  477.                                 uint32_t min_frac_feed_div = min_fractional_feed_div;
  478.                                 uint32_t max_frac_feed_div = max_fractional_feed_div + 1;
  479.                                 uint32_t frac_feedback_div;
  480.                                 uint64_t tmp;
  481.  
  482.                                 feedback_div = (min_feed_div + max_feed_div) / 2;
  483.  
  484.                                 tmp = (uint64_t)pll->reference_freq * feedback_div;
  485.                                 vco = radeon_div(tmp, ref_div);
  486.  
  487.                                 if (vco < pll->pll_out_min) {
  488.                                         min_feed_div = feedback_div + 1;
  489.                                         continue;
  490.                                 } else if (vco > pll->pll_out_max) {
  491.                                         max_feed_div = feedback_div;
  492.                                         continue;
  493.                                 }
  494.  
  495.                                 while (min_frac_feed_div < max_frac_feed_div) {
  496.                                         frac_feedback_div = (min_frac_feed_div + max_frac_feed_div) / 2;
  497.                                         tmp = (uint64_t)pll->reference_freq * 10000 * feedback_div;
  498.                                         tmp += (uint64_t)pll->reference_freq * 1000 * frac_feedback_div;
  499.                                         current_freq = radeon_div(tmp, ref_div * post_div);
  500.  
  501.                                         error = abs(current_freq - freq);
  502.                                         vco_diff = abs(vco - best_vco);
  503.  
  504.                                         if ((best_vco == 0 && error < best_error) ||
  505.                                             (best_vco != 0 &&
  506.                                              (error < best_error - 100 ||
  507.                                               (abs(error - best_error) < 100 && vco_diff < best_vco_diff)))) {
  508.                                                 best_post_div = post_div;
  509.                                                 best_ref_div = ref_div;
  510.                                                 best_feedback_div = feedback_div;
  511.                                                 best_frac_feedback_div = frac_feedback_div;
  512.                                                 best_freq = current_freq;
  513.                                                 best_error = error;
  514.                                                 best_vco_diff = vco_diff;
  515.                                         } else if (current_freq == freq) {
  516.                                                 if (best_freq == -1) {
  517.                                                         best_post_div = post_div;
  518.                                                         best_ref_div = ref_div;
  519.                                                         best_feedback_div = feedback_div;
  520.                                                         best_frac_feedback_div = frac_feedback_div;
  521.                                                         best_freq = current_freq;
  522.                                                         best_error = error;
  523.                                                         best_vco_diff = vco_diff;
  524.                                                 } else if (((flags & RADEON_PLL_PREFER_LOW_REF_DIV) && (ref_div < best_ref_div)) ||
  525.                                                            ((flags & RADEON_PLL_PREFER_HIGH_REF_DIV) && (ref_div > best_ref_div)) ||
  526.                                                            ((flags & RADEON_PLL_PREFER_LOW_FB_DIV) && (feedback_div < best_feedback_div)) ||
  527.                                                            ((flags & RADEON_PLL_PREFER_HIGH_FB_DIV) && (feedback_div > best_feedback_div)) ||
  528.                                                            ((flags & RADEON_PLL_PREFER_LOW_POST_DIV) && (post_div < best_post_div)) ||
  529.                                                            ((flags & RADEON_PLL_PREFER_HIGH_POST_DIV) && (post_div > best_post_div))) {
  530.                                                         best_post_div = post_div;
  531.                                                         best_ref_div = ref_div;
  532.                                                         best_feedback_div = feedback_div;
  533.                                                         best_frac_feedback_div = frac_feedback_div;
  534.                                                         best_freq = current_freq;
  535.                                                         best_error = error;
  536.                                                         best_vco_diff = vco_diff;
  537.                                                 }
  538.                                         }
  539.                                         if (current_freq < freq)
  540.                                                 min_frac_feed_div = frac_feedback_div + 1;
  541.                                         else
  542.                                                 max_frac_feed_div = frac_feedback_div;
  543.                                 }
  544.                                 if (current_freq < freq)
  545.                                         min_feed_div = feedback_div + 1;
  546.                                 else
  547.                                         max_feed_div = feedback_div;
  548.                         }
  549.                 }
  550.         }
  551.  
  552.         *dot_clock_p = best_freq / 10000;
  553.         *fb_div_p = best_feedback_div;
  554.         *frac_fb_div_p = best_frac_feedback_div;
  555.         *ref_div_p = best_ref_div;
  556.         *post_div_p = best_post_div;
  557. }
  558.  
  559.  
  560. static void radeon_user_framebuffer_destroy(struct drm_framebuffer *fb)
  561. {
  562.         struct radeon_framebuffer *radeon_fb = to_radeon_framebuffer(fb);
  563.         struct drm_device *dev = fb->dev;
  564.  
  565.         if (fb->fbdev)
  566.                 radeonfb_remove(dev, fb);
  567.  
  568. //   if (radeon_fb->obj) {
  569. //       radeon_gem_object_unpin(radeon_fb->obj);
  570. //       mutex_lock(&dev->struct_mutex);
  571. //       drm_gem_object_unreference(radeon_fb->obj);
  572. //       mutex_unlock(&dev->struct_mutex);
  573. //   }
  574.         drm_framebuffer_cleanup(fb);
  575.         kfree(radeon_fb);
  576. }
  577.  
  578. static int radeon_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  579.                          struct drm_file *file_priv,
  580.                          unsigned int *handle)
  581. {
  582.    struct radeon_framebuffer *radeon_fb = to_radeon_framebuffer(fb);
  583.  
  584.    return NULL;
  585. //   return drm_gem_handle_create(file_priv, radeon_fb->obj, handle);
  586. }
  587.  
  588. static const struct drm_framebuffer_funcs radeon_fb_funcs = {
  589.         .destroy = radeon_user_framebuffer_destroy,
  590.     .create_handle = radeon_user_framebuffer_create_handle,
  591. };
  592.  
  593.  
  594. struct drm_framebuffer *
  595. radeon_framebuffer_create(struct drm_device *dev,
  596.                           struct drm_mode_fb_cmd *mode_cmd,
  597.                           struct drm_gem_object *obj)
  598. {
  599.         struct radeon_framebuffer *radeon_fb;
  600.  
  601.         radeon_fb = kzalloc(sizeof(*radeon_fb), GFP_KERNEL);
  602.         if (radeon_fb == NULL) {
  603.                 return NULL;
  604.         }
  605.     drm_framebuffer_init(dev, &radeon_fb->base, &radeon_fb_funcs);
  606.     drm_helper_mode_fill_fb_struct(&radeon_fb->base, mode_cmd);
  607.         radeon_fb->obj = obj;
  608.         return &radeon_fb->base;
  609. }
  610.  
  611. static struct drm_framebuffer *
  612. radeon_user_framebuffer_create(struct drm_device *dev,
  613.                                struct drm_file *file_priv,
  614.                                struct drm_mode_fb_cmd *mode_cmd)
  615. {
  616.         struct drm_gem_object *obj;
  617.  
  618.     return NULL;
  619.  
  620. //   obj = drm_gem_object_lookup(dev, file_priv, mode_cmd->handle);
  621. //
  622. //   return radeon_framebuffer_create(dev, mode_cmd, obj);
  623. }
  624.  
  625.  
  626. static const struct drm_mode_config_funcs radeon_mode_funcs = {
  627.  //  .fb_create = radeon_user_framebuffer_create,
  628.      .fb_changed = radeonfb_probe,
  629. };
  630.  
  631. int radeon_modeset_init(struct radeon_device *rdev)
  632. {
  633.  
  634.     dbgprintf("%s\n",__FUNCTION__);
  635.  
  636.         int num_crtc = 2, i;
  637.         int ret;
  638.  
  639.         drm_mode_config_init(rdev->ddev);
  640.         rdev->mode_info.mode_config_initialized = true;
  641.  
  642.     rdev->ddev->mode_config.funcs = (void *)&radeon_mode_funcs;
  643.  
  644.         if (ASIC_IS_AVIVO(rdev)) {
  645.                 rdev->ddev->mode_config.max_width = 8192;
  646.                 rdev->ddev->mode_config.max_height = 8192;
  647.         } else {
  648.                 rdev->ddev->mode_config.max_width = 4096;
  649.                 rdev->ddev->mode_config.max_height = 4096;
  650.         }
  651.  
  652.         rdev->ddev->mode_config.fb_base = rdev->mc.aper_base;
  653.  
  654.         /* allocate crtcs - TODO single crtc */
  655.         for (i = 0; i < num_crtc; i++) {
  656.                 radeon_crtc_init(rdev->ddev, i);
  657.         }
  658.  
  659.         /* okay we should have all the bios connectors */
  660.         ret = radeon_setup_enc_conn(rdev->ddev);
  661.         if (!ret) {
  662.                 return ret;
  663.         }
  664.         drm_helper_initial_config(rdev->ddev);
  665.  
  666.     dbgprintf("done %s\n",__FUNCTION__);
  667.  
  668.         return 0;
  669. }
  670.  
  671. void radeon_modeset_fini(struct radeon_device *rdev)
  672. {
  673.         if (rdev->mode_info.mode_config_initialized) {
  674.                 drm_mode_config_cleanup(rdev->ddev);
  675.                 rdev->mode_info.mode_config_initialized = false;
  676.         }
  677. }
  678.  
  679. void radeon_init_disp_bandwidth(struct drm_device *dev)
  680. {
  681.         struct radeon_device *rdev = dev->dev_private;
  682.         struct drm_display_mode *modes[2];
  683.         int pixel_bytes[2];
  684.         struct drm_crtc *crtc;
  685.  
  686.         pixel_bytes[0] = pixel_bytes[1] = 0;
  687.         modes[0] = modes[1] = NULL;
  688.  
  689.         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  690.                 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  691.  
  692.                 if (crtc->enabled && crtc->fb) {
  693.                         modes[radeon_crtc->crtc_id] = &crtc->mode;
  694.                         pixel_bytes[radeon_crtc->crtc_id] = crtc->fb->bits_per_pixel / 8;
  695.                 }
  696.         }
  697.  
  698.         if (ASIC_IS_AVIVO(rdev)) {
  699.                 radeon_init_disp_bw_avivo(dev,
  700.                                           modes[0],
  701.                                           pixel_bytes[0],
  702.                                           modes[1],
  703.                                           pixel_bytes[1]);
  704.         } else {
  705.                 radeon_init_disp_bw_legacy(dev,
  706.                                            modes[0],
  707.                                            pixel_bytes[0],
  708.                                            modes[1],
  709.                                            pixel_bytes[1]);
  710.         }
  711. }
  712.