oaktrail_lvds.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /*
  2. * Copyright © 2006-2009 Intel Corporation
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along with
  14. * this program; if not, write to the Free Software Foundation, Inc.,
  15. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  16. *
  17. * Authors:
  18. * Eric Anholt <eric@anholt.net>
  19. * Dave Airlie <airlied@linux.ie>
  20. * Jesse Barnes <jesse.barnes@intel.com>
  21. */
  22. #include <linux/i2c.h>
  23. #include <drm/drmP.h>
  24. #include <asm/mrst.h>
  25. #include "intel_bios.h"
  26. #include "psb_drv.h"
  27. #include "psb_intel_drv.h"
  28. #include "psb_intel_reg.h"
  29. #include "power.h"
  30. #include <linux/pm_runtime.h>
  31. /* The max/min PWM frequency in BPCR[31:17] - */
  32. /* The smallest number is 1 (not 0) that can fit in the
  33. * 15-bit field of the and then*/
  34. /* shifts to the left by one bit to get the actual 16-bit
  35. * value that the 15-bits correspond to.*/
  36. #define MRST_BLC_MAX_PWM_REG_FREQ 0xFFFF
  37. #define BRIGHTNESS_MAX_LEVEL 100
  38. /**
  39. * Sets the power state for the panel.
  40. */
  41. static void oaktrail_lvds_set_power(struct drm_device *dev,
  42. struct gma_encoder *gma_encoder,
  43. bool on)
  44. {
  45. u32 pp_status;
  46. struct drm_psb_private *dev_priv = dev->dev_private;
  47. if (!gma_power_begin(dev, true))
  48. return;
  49. if (on) {
  50. REG_WRITE(PP_CONTROL, REG_READ(PP_CONTROL) |
  51. POWER_TARGET_ON);
  52. do {
  53. pp_status = REG_READ(PP_STATUS);
  54. } while ((pp_status & (PP_ON | PP_READY)) == PP_READY);
  55. dev_priv->is_lvds_on = true;
  56. if (dev_priv->ops->lvds_bl_power)
  57. dev_priv->ops->lvds_bl_power(dev, true);
  58. } else {
  59. if (dev_priv->ops->lvds_bl_power)
  60. dev_priv->ops->lvds_bl_power(dev, false);
  61. REG_WRITE(PP_CONTROL, REG_READ(PP_CONTROL) &
  62. ~POWER_TARGET_ON);
  63. do {
  64. pp_status = REG_READ(PP_STATUS);
  65. } while (pp_status & PP_ON);
  66. dev_priv->is_lvds_on = false;
  67. pm_request_idle(&dev->pdev->dev);
  68. }
  69. gma_power_end(dev);
  70. }
  71. static void oaktrail_lvds_dpms(struct drm_encoder *encoder, int mode)
  72. {
  73. struct drm_device *dev = encoder->dev;
  74. struct gma_encoder *gma_encoder = to_gma_encoder(encoder);
  75. if (mode == DRM_MODE_DPMS_ON)
  76. oaktrail_lvds_set_power(dev, gma_encoder, true);
  77. else
  78. oaktrail_lvds_set_power(dev, gma_encoder, false);
  79. /* XXX: We never power down the LVDS pairs. */
  80. }
  81. static void oaktrail_lvds_mode_set(struct drm_encoder *encoder,
  82. struct drm_display_mode *mode,
  83. struct drm_display_mode *adjusted_mode)
  84. {
  85. struct drm_device *dev = encoder->dev;
  86. struct drm_psb_private *dev_priv = dev->dev_private;
  87. struct psb_intel_mode_device *mode_dev = &dev_priv->mode_dev;
  88. struct drm_mode_config *mode_config = &dev->mode_config;
  89. struct drm_connector *connector = NULL;
  90. struct drm_crtc *crtc = encoder->crtc;
  91. u32 lvds_port;
  92. uint64_t v = DRM_MODE_SCALE_FULLSCREEN;
  93. if (!gma_power_begin(dev, true))
  94. return;
  95. /*
  96. * The LVDS pin pair will already have been turned on in the
  97. * psb_intel_crtc_mode_set since it has a large impact on the DPLL
  98. * settings.
  99. */
  100. lvds_port = (REG_READ(LVDS) &
  101. (~LVDS_PIPEB_SELECT)) |
  102. LVDS_PORT_EN |
  103. LVDS_BORDER_EN;
  104. /* If the firmware says dither on Moorestown, or the BIOS does
  105. on Oaktrail then enable dithering */
  106. if (mode_dev->panel_wants_dither || dev_priv->lvds_dither)
  107. lvds_port |= MRST_PANEL_8TO6_DITHER_ENABLE;
  108. REG_WRITE(LVDS, lvds_port);
  109. /* Find the connector we're trying to set up */
  110. list_for_each_entry(connector, &mode_config->connector_list, head) {
  111. if (!connector->encoder || connector->encoder->crtc != crtc)
  112. continue;
  113. }
  114. if (!connector) {
  115. DRM_ERROR("Couldn't find connector when setting mode");
  116. return;
  117. }
  118. drm_object_property_get_value(
  119. &connector->base,
  120. dev->mode_config.scaling_mode_property,
  121. &v);
  122. if (v == DRM_MODE_SCALE_NO_SCALE)
  123. REG_WRITE(PFIT_CONTROL, 0);
  124. else if (v == DRM_MODE_SCALE_ASPECT) {
  125. if ((mode->vdisplay != adjusted_mode->crtc_vdisplay) ||
  126. (mode->hdisplay != adjusted_mode->crtc_hdisplay)) {
  127. if ((adjusted_mode->crtc_hdisplay * mode->vdisplay) ==
  128. (mode->hdisplay * adjusted_mode->crtc_vdisplay))
  129. REG_WRITE(PFIT_CONTROL, PFIT_ENABLE);
  130. else if ((adjusted_mode->crtc_hdisplay *
  131. mode->vdisplay) > (mode->hdisplay *
  132. adjusted_mode->crtc_vdisplay))
  133. REG_WRITE(PFIT_CONTROL, PFIT_ENABLE |
  134. PFIT_SCALING_MODE_PILLARBOX);
  135. else
  136. REG_WRITE(PFIT_CONTROL, PFIT_ENABLE |
  137. PFIT_SCALING_MODE_LETTERBOX);
  138. } else
  139. REG_WRITE(PFIT_CONTROL, PFIT_ENABLE);
  140. } else /*(v == DRM_MODE_SCALE_FULLSCREEN)*/
  141. REG_WRITE(PFIT_CONTROL, PFIT_ENABLE);
  142. gma_power_end(dev);
  143. }
  144. static void oaktrail_lvds_prepare(struct drm_encoder *encoder)
  145. {
  146. struct drm_device *dev = encoder->dev;
  147. struct drm_psb_private *dev_priv = dev->dev_private;
  148. struct gma_encoder *gma_encoder = to_gma_encoder(encoder);
  149. struct psb_intel_mode_device *mode_dev = &dev_priv->mode_dev;
  150. if (!gma_power_begin(dev, true))
  151. return;
  152. mode_dev->saveBLC_PWM_CTL = REG_READ(BLC_PWM_CTL);
  153. mode_dev->backlight_duty_cycle = (mode_dev->saveBLC_PWM_CTL &
  154. BACKLIGHT_DUTY_CYCLE_MASK);
  155. oaktrail_lvds_set_power(dev, gma_encoder, false);
  156. gma_power_end(dev);
  157. }
  158. static u32 oaktrail_lvds_get_max_backlight(struct drm_device *dev)
  159. {
  160. struct drm_psb_private *dev_priv = dev->dev_private;
  161. u32 ret;
  162. if (gma_power_begin(dev, false)) {
  163. ret = ((REG_READ(BLC_PWM_CTL) &
  164. BACKLIGHT_MODULATION_FREQ_MASK) >>
  165. BACKLIGHT_MODULATION_FREQ_SHIFT) * 2;
  166. gma_power_end(dev);
  167. } else
  168. ret = ((dev_priv->regs.saveBLC_PWM_CTL &
  169. BACKLIGHT_MODULATION_FREQ_MASK) >>
  170. BACKLIGHT_MODULATION_FREQ_SHIFT) * 2;
  171. return ret;
  172. }
  173. static void oaktrail_lvds_commit(struct drm_encoder *encoder)
  174. {
  175. struct drm_device *dev = encoder->dev;
  176. struct drm_psb_private *dev_priv = dev->dev_private;
  177. struct gma_encoder *gma_encoder = to_gma_encoder(encoder);
  178. struct psb_intel_mode_device *mode_dev = &dev_priv->mode_dev;
  179. if (mode_dev->backlight_duty_cycle == 0)
  180. mode_dev->backlight_duty_cycle =
  181. oaktrail_lvds_get_max_backlight(dev);
  182. oaktrail_lvds_set_power(dev, gma_encoder, true);
  183. }
  184. static const struct drm_encoder_helper_funcs oaktrail_lvds_helper_funcs = {
  185. .dpms = oaktrail_lvds_dpms,
  186. .mode_fixup = psb_intel_lvds_mode_fixup,
  187. .prepare = oaktrail_lvds_prepare,
  188. .mode_set = oaktrail_lvds_mode_set,
  189. .commit = oaktrail_lvds_commit,
  190. };
  191. static struct drm_display_mode lvds_configuration_modes[] = {
  192. /* hard coded fixed mode for TPO LTPS LPJ040K001A */
  193. { DRM_MODE("800x480", DRM_MODE_TYPE_DRIVER, 33264, 800, 836,
  194. 846, 1056, 0, 480, 489, 491, 525, 0, 0) },
  195. /* hard coded fixed mode for LVDS 800x480 */
  196. { DRM_MODE("800x480", DRM_MODE_TYPE_DRIVER, 30994, 800, 801,
  197. 802, 1024, 0, 480, 481, 482, 525, 0, 0) },
  198. /* hard coded fixed mode for Samsung 480wsvga LVDS 1024x600@75 */
  199. { DRM_MODE("1024x600", DRM_MODE_TYPE_DRIVER, 53990, 1024, 1072,
  200. 1104, 1184, 0, 600, 603, 604, 608, 0, 0) },
  201. /* hard coded fixed mode for Samsung 480wsvga LVDS 1024x600@75 */
  202. { DRM_MODE("1024x600", DRM_MODE_TYPE_DRIVER, 53990, 1024, 1104,
  203. 1136, 1184, 0, 600, 603, 604, 608, 0, 0) },
  204. /* hard coded fixed mode for Sharp wsvga LVDS 1024x600 */
  205. { DRM_MODE("1024x600", DRM_MODE_TYPE_DRIVER, 48885, 1024, 1124,
  206. 1204, 1312, 0, 600, 607, 610, 621, 0, 0) },
  207. /* hard coded fixed mode for LVDS 1024x768 */
  208. { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 65000, 1024, 1048,
  209. 1184, 1344, 0, 768, 771, 777, 806, 0, 0) },
  210. /* hard coded fixed mode for LVDS 1366x768 */
  211. { DRM_MODE("1366x768", DRM_MODE_TYPE_DRIVER, 77500, 1366, 1430,
  212. 1558, 1664, 0, 768, 769, 770, 776, 0, 0) },
  213. };
  214. /* Returns the panel fixed mode from configuration. */
  215. static void oaktrail_lvds_get_configuration_mode(struct drm_device *dev,
  216. struct psb_intel_mode_device *mode_dev)
  217. {
  218. struct drm_display_mode *mode = NULL;
  219. struct drm_psb_private *dev_priv = dev->dev_private;
  220. struct oaktrail_timing_info *ti = &dev_priv->gct_data.DTD;
  221. mode_dev->panel_fixed_mode = NULL;
  222. /* Use the firmware provided data on Moorestown */
  223. if (dev_priv->has_gct) {
  224. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  225. if (!mode)
  226. return;
  227. mode->hdisplay = (ti->hactive_hi << 8) | ti->hactive_lo;
  228. mode->vdisplay = (ti->vactive_hi << 8) | ti->vactive_lo;
  229. mode->hsync_start = mode->hdisplay + \
  230. ((ti->hsync_offset_hi << 8) | \
  231. ti->hsync_offset_lo);
  232. mode->hsync_end = mode->hsync_start + \
  233. ((ti->hsync_pulse_width_hi << 8) | \
  234. ti->hsync_pulse_width_lo);
  235. mode->htotal = mode->hdisplay + ((ti->hblank_hi << 8) | \
  236. ti->hblank_lo);
  237. mode->vsync_start = \
  238. mode->vdisplay + ((ti->vsync_offset_hi << 4) | \
  239. ti->vsync_offset_lo);
  240. mode->vsync_end = \
  241. mode->vsync_start + ((ti->vsync_pulse_width_hi << 4) | \
  242. ti->vsync_pulse_width_lo);
  243. mode->vtotal = mode->vdisplay + \
  244. ((ti->vblank_hi << 8) | ti->vblank_lo);
  245. mode->clock = ti->pixel_clock * 10;
  246. #if 0
  247. printk(KERN_INFO "hdisplay is %d\n", mode->hdisplay);
  248. printk(KERN_INFO "vdisplay is %d\n", mode->vdisplay);
  249. printk(KERN_INFO "HSS is %d\n", mode->hsync_start);
  250. printk(KERN_INFO "HSE is %d\n", mode->hsync_end);
  251. printk(KERN_INFO "htotal is %d\n", mode->htotal);
  252. printk(KERN_INFO "VSS is %d\n", mode->vsync_start);
  253. printk(KERN_INFO "VSE is %d\n", mode->vsync_end);
  254. printk(KERN_INFO "vtotal is %d\n", mode->vtotal);
  255. printk(KERN_INFO "clock is %d\n", mode->clock);
  256. #endif
  257. mode_dev->panel_fixed_mode = mode;
  258. }
  259. /* Use the BIOS VBT mode if available */
  260. if (mode_dev->panel_fixed_mode == NULL && mode_dev->vbt_mode)
  261. mode_dev->panel_fixed_mode = drm_mode_duplicate(dev,
  262. mode_dev->vbt_mode);
  263. /* Then try the LVDS VBT mode */
  264. if (mode_dev->panel_fixed_mode == NULL)
  265. if (dev_priv->lfp_lvds_vbt_mode)
  266. mode_dev->panel_fixed_mode =
  267. drm_mode_duplicate(dev,
  268. dev_priv->lfp_lvds_vbt_mode);
  269. /* Then guess */
  270. if (mode_dev->panel_fixed_mode == NULL)
  271. mode_dev->panel_fixed_mode
  272. = drm_mode_duplicate(dev, &lvds_configuration_modes[2]);
  273. drm_mode_set_name(mode_dev->panel_fixed_mode);
  274. drm_mode_set_crtcinfo(mode_dev->panel_fixed_mode, 0);
  275. }
  276. /**
  277. * oaktrail_lvds_init - setup LVDS connectors on this device
  278. * @dev: drm device
  279. *
  280. * Create the connector, register the LVDS DDC bus, and try to figure out what
  281. * modes we can display on the LVDS panel (if present).
  282. */
  283. void oaktrail_lvds_init(struct drm_device *dev,
  284. struct psb_intel_mode_device *mode_dev)
  285. {
  286. struct gma_encoder *gma_encoder;
  287. struct gma_connector *gma_connector;
  288. struct drm_connector *connector;
  289. struct drm_encoder *encoder;
  290. struct drm_psb_private *dev_priv = dev->dev_private;
  291. struct edid *edid;
  292. struct i2c_adapter *i2c_adap;
  293. struct drm_display_mode *scan; /* *modes, *bios_mode; */
  294. gma_encoder = kzalloc(sizeof(struct gma_encoder), GFP_KERNEL);
  295. if (!gma_encoder)
  296. return;
  297. gma_connector = kzalloc(sizeof(struct gma_connector), GFP_KERNEL);
  298. if (!gma_connector)
  299. goto failed_connector;
  300. connector = &gma_connector->base;
  301. encoder = &gma_encoder->base;
  302. dev_priv->is_lvds_on = true;
  303. drm_connector_init(dev, connector,
  304. &psb_intel_lvds_connector_funcs,
  305. DRM_MODE_CONNECTOR_LVDS);
  306. drm_encoder_init(dev, encoder, &psb_intel_lvds_enc_funcs,
  307. DRM_MODE_ENCODER_LVDS);
  308. gma_connector_attach_encoder(gma_connector, gma_encoder);
  309. gma_encoder->type = INTEL_OUTPUT_LVDS;
  310. drm_encoder_helper_add(encoder, &oaktrail_lvds_helper_funcs);
  311. drm_connector_helper_add(connector,
  312. &psb_intel_lvds_connector_helper_funcs);
  313. connector->display_info.subpixel_order = SubPixelHorizontalRGB;
  314. connector->interlace_allowed = false;
  315. connector->doublescan_allowed = false;
  316. drm_object_attach_property(&connector->base,
  317. dev->mode_config.scaling_mode_property,
  318. DRM_MODE_SCALE_FULLSCREEN);
  319. drm_object_attach_property(&connector->base,
  320. dev_priv->backlight_property,
  321. BRIGHTNESS_MAX_LEVEL);
  322. mode_dev->panel_wants_dither = false;
  323. if (dev_priv->has_gct)
  324. mode_dev->panel_wants_dither = (dev_priv->gct_data.
  325. Panel_Port_Control & MRST_PANEL_8TO6_DITHER_ENABLE);
  326. if (dev_priv->lvds_dither)
  327. mode_dev->panel_wants_dither = 1;
  328. /*
  329. * LVDS discovery:
  330. * 1) check for EDID on DDC
  331. * 2) check for VBT data
  332. * 3) check to see if LVDS is already on
  333. * if none of the above, no panel
  334. * 4) make sure lid is open
  335. * if closed, act like it's not there for now
  336. */
  337. i2c_adap = i2c_get_adapter(dev_priv->ops->i2c_bus);
  338. if (i2c_adap == NULL)
  339. dev_err(dev->dev, "No ddc adapter available!\n");
  340. /*
  341. * Attempt to get the fixed panel mode from DDC. Assume that the
  342. * preferred mode is the right one.
  343. */
  344. if (i2c_adap) {
  345. edid = drm_get_edid(connector, i2c_adap);
  346. if (edid) {
  347. drm_mode_connector_update_edid_property(connector,
  348. edid);
  349. drm_add_edid_modes(connector, edid);
  350. kfree(edid);
  351. }
  352. list_for_each_entry(scan, &connector->probed_modes, head) {
  353. if (scan->type & DRM_MODE_TYPE_PREFERRED) {
  354. mode_dev->panel_fixed_mode =
  355. drm_mode_duplicate(dev, scan);
  356. goto out; /* FIXME: check for quirks */
  357. }
  358. }
  359. }
  360. /*
  361. * If we didn't get EDID, try geting panel timing
  362. * from configuration data
  363. */
  364. oaktrail_lvds_get_configuration_mode(dev, mode_dev);
  365. if (mode_dev->panel_fixed_mode) {
  366. mode_dev->panel_fixed_mode->type |= DRM_MODE_TYPE_PREFERRED;
  367. goto out; /* FIXME: check for quirks */
  368. }
  369. /* If we still don't have a mode after all that, give up. */
  370. if (!mode_dev->panel_fixed_mode) {
  371. dev_err(dev->dev, "Found no modes on the lvds, ignoring the LVDS\n");
  372. goto failed_find;
  373. }
  374. out:
  375. drm_sysfs_connector_add(connector);
  376. return;
  377. failed_find:
  378. dev_dbg(dev->dev, "No LVDS modes found, disabling.\n");
  379. if (gma_encoder->ddc_bus)
  380. psb_intel_i2c_destroy(gma_encoder->ddc_bus);
  381. /* failed_ddc: */
  382. drm_encoder_cleanup(encoder);
  383. drm_connector_cleanup(connector);
  384. kfree(gma_connector);
  385. failed_connector:
  386. kfree(gma_encoder);
  387. }