oaktrail_lvds.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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/intel-mid.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. /* Returns the panel fixed mode from configuration. */
  192. static void oaktrail_lvds_get_configuration_mode(struct drm_device *dev,
  193. struct psb_intel_mode_device *mode_dev)
  194. {
  195. struct drm_display_mode *mode = NULL;
  196. struct drm_psb_private *dev_priv = dev->dev_private;
  197. struct oaktrail_timing_info *ti = &dev_priv->gct_data.DTD;
  198. mode_dev->panel_fixed_mode = NULL;
  199. /* Use the firmware provided data on Moorestown */
  200. if (dev_priv->has_gct) {
  201. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  202. if (!mode)
  203. return;
  204. mode->hdisplay = (ti->hactive_hi << 8) | ti->hactive_lo;
  205. mode->vdisplay = (ti->vactive_hi << 8) | ti->vactive_lo;
  206. mode->hsync_start = mode->hdisplay + \
  207. ((ti->hsync_offset_hi << 8) | \
  208. ti->hsync_offset_lo);
  209. mode->hsync_end = mode->hsync_start + \
  210. ((ti->hsync_pulse_width_hi << 8) | \
  211. ti->hsync_pulse_width_lo);
  212. mode->htotal = mode->hdisplay + ((ti->hblank_hi << 8) | \
  213. ti->hblank_lo);
  214. mode->vsync_start = \
  215. mode->vdisplay + ((ti->vsync_offset_hi << 4) | \
  216. ti->vsync_offset_lo);
  217. mode->vsync_end = \
  218. mode->vsync_start + ((ti->vsync_pulse_width_hi << 4) | \
  219. ti->vsync_pulse_width_lo);
  220. mode->vtotal = mode->vdisplay + \
  221. ((ti->vblank_hi << 8) | ti->vblank_lo);
  222. mode->clock = ti->pixel_clock * 10;
  223. #if 0
  224. printk(KERN_INFO "hdisplay is %d\n", mode->hdisplay);
  225. printk(KERN_INFO "vdisplay is %d\n", mode->vdisplay);
  226. printk(KERN_INFO "HSS is %d\n", mode->hsync_start);
  227. printk(KERN_INFO "HSE is %d\n", mode->hsync_end);
  228. printk(KERN_INFO "htotal is %d\n", mode->htotal);
  229. printk(KERN_INFO "VSS is %d\n", mode->vsync_start);
  230. printk(KERN_INFO "VSE is %d\n", mode->vsync_end);
  231. printk(KERN_INFO "vtotal is %d\n", mode->vtotal);
  232. printk(KERN_INFO "clock is %d\n", mode->clock);
  233. #endif
  234. mode_dev->panel_fixed_mode = mode;
  235. }
  236. /* Use the BIOS VBT mode if available */
  237. if (mode_dev->panel_fixed_mode == NULL && mode_dev->vbt_mode)
  238. mode_dev->panel_fixed_mode = drm_mode_duplicate(dev,
  239. mode_dev->vbt_mode);
  240. /* Then try the LVDS VBT mode */
  241. if (mode_dev->panel_fixed_mode == NULL)
  242. if (dev_priv->lfp_lvds_vbt_mode)
  243. mode_dev->panel_fixed_mode =
  244. drm_mode_duplicate(dev,
  245. dev_priv->lfp_lvds_vbt_mode);
  246. /* If we still got no mode then bail */
  247. if (mode_dev->panel_fixed_mode == NULL)
  248. return;
  249. drm_mode_set_name(mode_dev->panel_fixed_mode);
  250. drm_mode_set_crtcinfo(mode_dev->panel_fixed_mode, 0);
  251. }
  252. /**
  253. * oaktrail_lvds_init - setup LVDS connectors on this device
  254. * @dev: drm device
  255. *
  256. * Create the connector, register the LVDS DDC bus, and try to figure out what
  257. * modes we can display on the LVDS panel (if present).
  258. */
  259. void oaktrail_lvds_init(struct drm_device *dev,
  260. struct psb_intel_mode_device *mode_dev)
  261. {
  262. struct gma_encoder *gma_encoder;
  263. struct gma_connector *gma_connector;
  264. struct drm_connector *connector;
  265. struct drm_encoder *encoder;
  266. struct drm_psb_private *dev_priv = dev->dev_private;
  267. struct edid *edid;
  268. struct i2c_adapter *i2c_adap;
  269. struct drm_display_mode *scan; /* *modes, *bios_mode; */
  270. gma_encoder = kzalloc(sizeof(struct gma_encoder), GFP_KERNEL);
  271. if (!gma_encoder)
  272. return;
  273. gma_connector = kzalloc(sizeof(struct gma_connector), GFP_KERNEL);
  274. if (!gma_connector)
  275. goto failed_connector;
  276. connector = &gma_connector->base;
  277. encoder = &gma_encoder->base;
  278. dev_priv->is_lvds_on = true;
  279. drm_connector_init(dev, connector,
  280. &psb_intel_lvds_connector_funcs,
  281. DRM_MODE_CONNECTOR_LVDS);
  282. drm_encoder_init(dev, encoder, &psb_intel_lvds_enc_funcs,
  283. DRM_MODE_ENCODER_LVDS);
  284. gma_connector_attach_encoder(gma_connector, gma_encoder);
  285. gma_encoder->type = INTEL_OUTPUT_LVDS;
  286. drm_encoder_helper_add(encoder, &oaktrail_lvds_helper_funcs);
  287. drm_connector_helper_add(connector,
  288. &psb_intel_lvds_connector_helper_funcs);
  289. connector->display_info.subpixel_order = SubPixelHorizontalRGB;
  290. connector->interlace_allowed = false;
  291. connector->doublescan_allowed = false;
  292. drm_object_attach_property(&connector->base,
  293. dev->mode_config.scaling_mode_property,
  294. DRM_MODE_SCALE_FULLSCREEN);
  295. drm_object_attach_property(&connector->base,
  296. dev_priv->backlight_property,
  297. BRIGHTNESS_MAX_LEVEL);
  298. mode_dev->panel_wants_dither = false;
  299. if (dev_priv->has_gct)
  300. mode_dev->panel_wants_dither = (dev_priv->gct_data.
  301. Panel_Port_Control & MRST_PANEL_8TO6_DITHER_ENABLE);
  302. if (dev_priv->lvds_dither)
  303. mode_dev->panel_wants_dither = 1;
  304. /*
  305. * LVDS discovery:
  306. * 1) check for EDID on DDC
  307. * 2) check for VBT data
  308. * 3) check to see if LVDS is already on
  309. * if none of the above, no panel
  310. * 4) make sure lid is open
  311. * if closed, act like it's not there for now
  312. */
  313. edid = NULL;
  314. mutex_lock(&dev->mode_config.mutex);
  315. i2c_adap = i2c_get_adapter(dev_priv->ops->i2c_bus);
  316. if (i2c_adap)
  317. edid = drm_get_edid(connector, i2c_adap);
  318. if (edid == NULL && dev_priv->lpc_gpio_base) {
  319. oaktrail_lvds_i2c_init(encoder);
  320. if (gma_encoder->ddc_bus != NULL) {
  321. i2c_adap = &gma_encoder->ddc_bus->adapter;
  322. edid = drm_get_edid(connector, i2c_adap);
  323. }
  324. }
  325. /*
  326. * Attempt to get the fixed panel mode from DDC. Assume that the
  327. * preferred mode is the right one.
  328. */
  329. if (edid) {
  330. drm_mode_connector_update_edid_property(connector, edid);
  331. drm_add_edid_modes(connector, edid);
  332. kfree(edid);
  333. list_for_each_entry(scan, &connector->probed_modes, head) {
  334. if (scan->type & DRM_MODE_TYPE_PREFERRED) {
  335. mode_dev->panel_fixed_mode =
  336. drm_mode_duplicate(dev, scan);
  337. goto out; /* FIXME: check for quirks */
  338. }
  339. }
  340. } else
  341. dev_err(dev->dev, "No ddc adapter available!\n");
  342. /*
  343. * If we didn't get EDID, try geting panel timing
  344. * from configuration data
  345. */
  346. oaktrail_lvds_get_configuration_mode(dev, mode_dev);
  347. if (mode_dev->panel_fixed_mode) {
  348. mode_dev->panel_fixed_mode->type |= DRM_MODE_TYPE_PREFERRED;
  349. goto out; /* FIXME: check for quirks */
  350. }
  351. /* If we still don't have a mode after all that, give up. */
  352. if (!mode_dev->panel_fixed_mode) {
  353. dev_err(dev->dev, "Found no modes on the lvds, ignoring the LVDS\n");
  354. goto failed_find;
  355. }
  356. out:
  357. mutex_unlock(&dev->mode_config.mutex);
  358. drm_connector_register(connector);
  359. return;
  360. failed_find:
  361. mutex_unlock(&dev->mode_config.mutex);
  362. dev_dbg(dev->dev, "No LVDS modes found, disabling.\n");
  363. if (gma_encoder->ddc_bus) {
  364. psb_intel_i2c_destroy(gma_encoder->ddc_bus);
  365. gma_encoder->ddc_bus = NULL;
  366. }
  367. /* failed_ddc: */
  368. drm_encoder_cleanup(encoder);
  369. drm_connector_cleanup(connector);
  370. kfree(gma_connector);
  371. failed_connector:
  372. kfree(gma_encoder);
  373. }