oaktrail_lvds.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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 psb_intel_output *output, bool on)
  43. {
  44. u32 pp_status;
  45. struct drm_psb_private *dev_priv = dev->dev_private;
  46. if (!gma_power_begin(dev, true))
  47. return;
  48. if (on) {
  49. REG_WRITE(PP_CONTROL, REG_READ(PP_CONTROL) |
  50. POWER_TARGET_ON);
  51. do {
  52. pp_status = REG_READ(PP_STATUS);
  53. } while ((pp_status & (PP_ON | PP_READY)) == PP_READY);
  54. dev_priv->is_lvds_on = true;
  55. if (dev_priv->ops->lvds_bl_power)
  56. dev_priv->ops->lvds_bl_power(dev, true);
  57. } else {
  58. if (dev_priv->ops->lvds_bl_power)
  59. dev_priv->ops->lvds_bl_power(dev, false);
  60. REG_WRITE(PP_CONTROL, REG_READ(PP_CONTROL) &
  61. ~POWER_TARGET_ON);
  62. do {
  63. pp_status = REG_READ(PP_STATUS);
  64. } while (pp_status & PP_ON);
  65. dev_priv->is_lvds_on = false;
  66. pm_request_idle(&dev->pdev->dev);
  67. }
  68. gma_power_end(dev);
  69. }
  70. static void oaktrail_lvds_dpms(struct drm_encoder *encoder, int mode)
  71. {
  72. struct drm_device *dev = encoder->dev;
  73. struct psb_intel_output *output = enc_to_psb_intel_output(encoder);
  74. if (mode == DRM_MODE_DPMS_ON)
  75. oaktrail_lvds_set_power(dev, output, true);
  76. else
  77. oaktrail_lvds_set_power(dev, output, false);
  78. /* XXX: We never power down the LVDS pairs. */
  79. }
  80. static void oaktrail_lvds_mode_set(struct drm_encoder *encoder,
  81. struct drm_display_mode *mode,
  82. struct drm_display_mode *adjusted_mode)
  83. {
  84. struct psb_intel_mode_device *mode_dev =
  85. enc_to_psb_intel_output(encoder)->mode_dev;
  86. struct drm_device *dev = encoder->dev;
  87. struct drm_psb_private *dev_priv = dev->dev_private;
  88. u32 lvds_port;
  89. uint64_t v = DRM_MODE_SCALE_FULLSCREEN;
  90. if (!gma_power_begin(dev, true))
  91. return;
  92. /*
  93. * The LVDS pin pair will already have been turned on in the
  94. * psb_intel_crtc_mode_set since it has a large impact on the DPLL
  95. * settings.
  96. */
  97. lvds_port = (REG_READ(LVDS) &
  98. (~LVDS_PIPEB_SELECT)) |
  99. LVDS_PORT_EN |
  100. LVDS_BORDER_EN;
  101. /* If the firmware says dither on Moorestown, or the BIOS does
  102. on Oaktrail then enable dithering */
  103. if (mode_dev->panel_wants_dither || dev_priv->lvds_dither)
  104. lvds_port |= MRST_PANEL_8TO6_DITHER_ENABLE;
  105. REG_WRITE(LVDS, lvds_port);
  106. drm_connector_property_get_value(
  107. &enc_to_psb_intel_output(encoder)->base,
  108. dev->mode_config.scaling_mode_property,
  109. &v);
  110. if (v == DRM_MODE_SCALE_NO_SCALE)
  111. REG_WRITE(PFIT_CONTROL, 0);
  112. else if (v == DRM_MODE_SCALE_ASPECT) {
  113. if ((mode->vdisplay != adjusted_mode->crtc_vdisplay) ||
  114. (mode->hdisplay != adjusted_mode->crtc_hdisplay)) {
  115. if ((adjusted_mode->crtc_hdisplay * mode->vdisplay) ==
  116. (mode->hdisplay * adjusted_mode->crtc_vdisplay))
  117. REG_WRITE(PFIT_CONTROL, PFIT_ENABLE);
  118. else if ((adjusted_mode->crtc_hdisplay *
  119. mode->vdisplay) > (mode->hdisplay *
  120. adjusted_mode->crtc_vdisplay))
  121. REG_WRITE(PFIT_CONTROL, PFIT_ENABLE |
  122. PFIT_SCALING_MODE_PILLARBOX);
  123. else
  124. REG_WRITE(PFIT_CONTROL, PFIT_ENABLE |
  125. PFIT_SCALING_MODE_LETTERBOX);
  126. } else
  127. REG_WRITE(PFIT_CONTROL, PFIT_ENABLE);
  128. } else /*(v == DRM_MODE_SCALE_FULLSCREEN)*/
  129. REG_WRITE(PFIT_CONTROL, PFIT_ENABLE);
  130. gma_power_end(dev);
  131. }
  132. static void oaktrail_lvds_prepare(struct drm_encoder *encoder)
  133. {
  134. struct drm_device *dev = encoder->dev;
  135. struct psb_intel_output *output = enc_to_psb_intel_output(encoder);
  136. struct psb_intel_mode_device *mode_dev = output->mode_dev;
  137. if (!gma_power_begin(dev, true))
  138. return;
  139. mode_dev->saveBLC_PWM_CTL = REG_READ(BLC_PWM_CTL);
  140. mode_dev->backlight_duty_cycle = (mode_dev->saveBLC_PWM_CTL &
  141. BACKLIGHT_DUTY_CYCLE_MASK);
  142. oaktrail_lvds_set_power(dev, output, false);
  143. gma_power_end(dev);
  144. }
  145. static u32 oaktrail_lvds_get_max_backlight(struct drm_device *dev)
  146. {
  147. struct drm_psb_private *dev_priv = dev->dev_private;
  148. u32 ret;
  149. if (gma_power_begin(dev, false)) {
  150. ret = ((REG_READ(BLC_PWM_CTL) &
  151. BACKLIGHT_MODULATION_FREQ_MASK) >>
  152. BACKLIGHT_MODULATION_FREQ_SHIFT) * 2;
  153. gma_power_end(dev);
  154. } else
  155. ret = ((dev_priv->saveBLC_PWM_CTL &
  156. BACKLIGHT_MODULATION_FREQ_MASK) >>
  157. BACKLIGHT_MODULATION_FREQ_SHIFT) * 2;
  158. return ret;
  159. }
  160. static void oaktrail_lvds_commit(struct drm_encoder *encoder)
  161. {
  162. struct drm_device *dev = encoder->dev;
  163. struct psb_intel_output *output = enc_to_psb_intel_output(encoder);
  164. struct psb_intel_mode_device *mode_dev = output->mode_dev;
  165. if (mode_dev->backlight_duty_cycle == 0)
  166. mode_dev->backlight_duty_cycle =
  167. oaktrail_lvds_get_max_backlight(dev);
  168. oaktrail_lvds_set_power(dev, output, true);
  169. }
  170. static const struct drm_encoder_helper_funcs oaktrail_lvds_helper_funcs = {
  171. .dpms = oaktrail_lvds_dpms,
  172. .mode_fixup = psb_intel_lvds_mode_fixup,
  173. .prepare = oaktrail_lvds_prepare,
  174. .mode_set = oaktrail_lvds_mode_set,
  175. .commit = oaktrail_lvds_commit,
  176. };
  177. static struct drm_display_mode lvds_configuration_modes[] = {
  178. /* hard coded fixed mode for TPO LTPS LPJ040K001A */
  179. { DRM_MODE("800x480", DRM_MODE_TYPE_DRIVER, 33264, 800, 836,
  180. 846, 1056, 0, 480, 489, 491, 525, 0, 0) },
  181. /* hard coded fixed mode for LVDS 800x480 */
  182. { DRM_MODE("800x480", DRM_MODE_TYPE_DRIVER, 30994, 800, 801,
  183. 802, 1024, 0, 480, 481, 482, 525, 0, 0) },
  184. /* hard coded fixed mode for Samsung 480wsvga LVDS 1024x600@75 */
  185. { DRM_MODE("1024x600", DRM_MODE_TYPE_DRIVER, 53990, 1024, 1072,
  186. 1104, 1184, 0, 600, 603, 604, 608, 0, 0) },
  187. /* hard coded fixed mode for Samsung 480wsvga LVDS 1024x600@75 */
  188. { DRM_MODE("1024x600", DRM_MODE_TYPE_DRIVER, 53990, 1024, 1104,
  189. 1136, 1184, 0, 600, 603, 604, 608, 0, 0) },
  190. /* hard coded fixed mode for Sharp wsvga LVDS 1024x600 */
  191. { DRM_MODE("1024x600", DRM_MODE_TYPE_DRIVER, 48885, 1024, 1124,
  192. 1204, 1312, 0, 600, 607, 610, 621, 0, 0) },
  193. /* hard coded fixed mode for LVDS 1024x768 */
  194. { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 65000, 1024, 1048,
  195. 1184, 1344, 0, 768, 771, 777, 806, 0, 0) },
  196. /* hard coded fixed mode for LVDS 1366x768 */
  197. { DRM_MODE("1366x768", DRM_MODE_TYPE_DRIVER, 77500, 1366, 1430,
  198. 1558, 1664, 0, 768, 769, 770, 776, 0, 0) },
  199. };
  200. /* Returns the panel fixed mode from configuration. */
  201. static void oaktrail_lvds_get_configuration_mode(struct drm_device *dev,
  202. struct psb_intel_mode_device *mode_dev)
  203. {
  204. struct drm_display_mode *mode = NULL;
  205. struct drm_psb_private *dev_priv = dev->dev_private;
  206. struct oaktrail_timing_info *ti = &dev_priv->gct_data.DTD;
  207. mode_dev->panel_fixed_mode = NULL;
  208. /* Use the firmware provided data on Moorestown */
  209. if (dev_priv->vbt_data.size != 0x00) { /*if non-zero, then use vbt*/
  210. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  211. if (!mode)
  212. return;
  213. mode->hdisplay = (ti->hactive_hi << 8) | ti->hactive_lo;
  214. mode->vdisplay = (ti->vactive_hi << 8) | ti->vactive_lo;
  215. mode->hsync_start = mode->hdisplay + \
  216. ((ti->hsync_offset_hi << 8) | \
  217. ti->hsync_offset_lo);
  218. mode->hsync_end = mode->hsync_start + \
  219. ((ti->hsync_pulse_width_hi << 8) | \
  220. ti->hsync_pulse_width_lo);
  221. mode->htotal = mode->hdisplay + ((ti->hblank_hi << 8) | \
  222. ti->hblank_lo);
  223. mode->vsync_start = \
  224. mode->vdisplay + ((ti->vsync_offset_hi << 4) | \
  225. ti->vsync_offset_lo);
  226. mode->vsync_end = \
  227. mode->vsync_start + ((ti->vsync_pulse_width_hi << 4) | \
  228. ti->vsync_pulse_width_lo);
  229. mode->vtotal = mode->vdisplay + \
  230. ((ti->vblank_hi << 8) | ti->vblank_lo);
  231. mode->clock = ti->pixel_clock * 10;
  232. #if 0
  233. printk(KERN_INFO "hdisplay is %d\n", mode->hdisplay);
  234. printk(KERN_INFO "vdisplay is %d\n", mode->vdisplay);
  235. printk(KERN_INFO "HSS is %d\n", mode->hsync_start);
  236. printk(KERN_INFO "HSE is %d\n", mode->hsync_end);
  237. printk(KERN_INFO "htotal is %d\n", mode->htotal);
  238. printk(KERN_INFO "VSS is %d\n", mode->vsync_start);
  239. printk(KERN_INFO "VSE is %d\n", mode->vsync_end);
  240. printk(KERN_INFO "vtotal is %d\n", mode->vtotal);
  241. printk(KERN_INFO "clock is %d\n", mode->clock);
  242. #endif
  243. mode_dev->panel_fixed_mode = mode;
  244. }
  245. /* Use the BIOS VBT mode if available */
  246. if (mode_dev->panel_fixed_mode == NULL && mode_dev->vbt_mode)
  247. mode_dev->panel_fixed_mode = drm_mode_duplicate(dev,
  248. mode_dev->vbt_mode);
  249. /* Then try the LVDS VBT mode */
  250. if (mode_dev->panel_fixed_mode == NULL)
  251. if (dev_priv->lfp_lvds_vbt_mode)
  252. mode_dev->panel_fixed_mode =
  253. drm_mode_duplicate(dev,
  254. dev_priv->lfp_lvds_vbt_mode);
  255. /* Then guess */
  256. if (mode_dev->panel_fixed_mode == NULL)
  257. mode_dev->panel_fixed_mode
  258. = drm_mode_duplicate(dev, &lvds_configuration_modes[2]);
  259. drm_mode_set_name(mode_dev->panel_fixed_mode);
  260. drm_mode_set_crtcinfo(mode_dev->panel_fixed_mode, 0);
  261. }
  262. /**
  263. * oaktrail_lvds_init - setup LVDS connectors on this device
  264. * @dev: drm device
  265. *
  266. * Create the connector, register the LVDS DDC bus, and try to figure out what
  267. * modes we can display on the LVDS panel (if present).
  268. */
  269. void oaktrail_lvds_init(struct drm_device *dev,
  270. struct psb_intel_mode_device *mode_dev)
  271. {
  272. struct psb_intel_output *psb_intel_output;
  273. struct drm_connector *connector;
  274. struct drm_encoder *encoder;
  275. struct drm_psb_private *dev_priv =
  276. (struct drm_psb_private *) dev->dev_private;
  277. struct edid *edid;
  278. int ret = 0;
  279. struct i2c_adapter *i2c_adap;
  280. struct drm_display_mode *scan; /* *modes, *bios_mode; */
  281. psb_intel_output = kzalloc(sizeof(struct psb_intel_output), GFP_KERNEL);
  282. if (!psb_intel_output)
  283. return;
  284. psb_intel_output->mode_dev = mode_dev;
  285. connector = &psb_intel_output->base;
  286. encoder = &psb_intel_output->enc;
  287. dev_priv->is_lvds_on = true;
  288. drm_connector_init(dev, &psb_intel_output->base,
  289. &psb_intel_lvds_connector_funcs,
  290. DRM_MODE_CONNECTOR_LVDS);
  291. drm_encoder_init(dev, &psb_intel_output->enc, &psb_intel_lvds_enc_funcs,
  292. DRM_MODE_ENCODER_LVDS);
  293. drm_mode_connector_attach_encoder(&psb_intel_output->base,
  294. &psb_intel_output->enc);
  295. psb_intel_output->type = INTEL_OUTPUT_LVDS;
  296. drm_encoder_helper_add(encoder, &oaktrail_lvds_helper_funcs);
  297. drm_connector_helper_add(connector,
  298. &psb_intel_lvds_connector_helper_funcs);
  299. connector->display_info.subpixel_order = SubPixelHorizontalRGB;
  300. connector->interlace_allowed = false;
  301. connector->doublescan_allowed = false;
  302. drm_connector_attach_property(connector,
  303. dev->mode_config.scaling_mode_property,
  304. DRM_MODE_SCALE_FULLSCREEN);
  305. drm_connector_attach_property(connector,
  306. dev_priv->backlight_property,
  307. BRIGHTNESS_MAX_LEVEL);
  308. mode_dev->panel_wants_dither = false;
  309. if (dev_priv->vbt_data.size != 0x00)
  310. mode_dev->panel_wants_dither = (dev_priv->gct_data.
  311. Panel_Port_Control & MRST_PANEL_8TO6_DITHER_ENABLE);
  312. /*
  313. * LVDS discovery:
  314. * 1) check for EDID on DDC
  315. * 2) check for VBT data
  316. * 3) check to see if LVDS is already on
  317. * if none of the above, no panel
  318. * 4) make sure lid is open
  319. * if closed, act like it's not there for now
  320. */
  321. i2c_adap = i2c_get_adapter(dev_priv->ops->i2c_bus);
  322. if (i2c_adap == NULL)
  323. dev_err(dev->dev, "No ddc adapter available!\n");
  324. /*
  325. * Attempt to get the fixed panel mode from DDC. Assume that the
  326. * preferred mode is the right one.
  327. */
  328. if (i2c_adap) {
  329. edid = drm_get_edid(connector, i2c_adap);
  330. if (edid) {
  331. drm_mode_connector_update_edid_property(connector,
  332. edid);
  333. ret = drm_add_edid_modes(connector, edid);
  334. kfree(edid);
  335. }
  336. list_for_each_entry(scan, &connector->probed_modes, head) {
  337. if (scan->type & DRM_MODE_TYPE_PREFERRED) {
  338. mode_dev->panel_fixed_mode =
  339. drm_mode_duplicate(dev, scan);
  340. goto out; /* FIXME: check for quirks */
  341. }
  342. }
  343. }
  344. /*
  345. * If we didn't get EDID, try geting panel timing
  346. * from configuration data
  347. */
  348. oaktrail_lvds_get_configuration_mode(dev, mode_dev);
  349. if (mode_dev->panel_fixed_mode) {
  350. mode_dev->panel_fixed_mode->type |= DRM_MODE_TYPE_PREFERRED;
  351. goto out; /* FIXME: check for quirks */
  352. }
  353. /* If we still don't have a mode after all that, give up. */
  354. if (!mode_dev->panel_fixed_mode) {
  355. dev_err(dev->dev, "Found no modes on the lvds, ignoring the LVDS\n");
  356. goto failed_find;
  357. }
  358. out:
  359. drm_sysfs_connector_add(connector);
  360. return;
  361. failed_find:
  362. dev_dbg(dev->dev, "No LVDS modes found, disabling.\n");
  363. if (psb_intel_output->ddc_bus)
  364. psb_intel_i2c_destroy(psb_intel_output->ddc_bus);
  365. /* failed_ddc: */
  366. drm_encoder_cleanup(encoder);
  367. drm_connector_cleanup(connector);
  368. kfree(connector);
  369. }