cdv_intel_lvds.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  1. /*
  2. * Copyright © 2006-2011 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 <linux/dmi.h>
  24. #include <drm/drmP.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. #include "cdv_device.h"
  32. /**
  33. * LVDS I2C backlight control macros
  34. */
  35. #define BRIGHTNESS_MAX_LEVEL 100
  36. #define BRIGHTNESS_MASK 0xFF
  37. #define BLC_I2C_TYPE 0x01
  38. #define BLC_PWM_TYPT 0x02
  39. #define BLC_POLARITY_NORMAL 0
  40. #define BLC_POLARITY_INVERSE 1
  41. #define PSB_BLC_MAX_PWM_REG_FREQ (0xFFFE)
  42. #define PSB_BLC_MIN_PWM_REG_FREQ (0x2)
  43. #define PSB_BLC_PWM_PRECISION_FACTOR (10)
  44. #define PSB_BACKLIGHT_PWM_CTL_SHIFT (16)
  45. #define PSB_BACKLIGHT_PWM_POLARITY_BIT_CLEAR (0xFFFE)
  46. struct cdv_intel_lvds_priv {
  47. /**
  48. * Saved LVDO output states
  49. */
  50. uint32_t savePP_ON;
  51. uint32_t savePP_OFF;
  52. uint32_t saveLVDS;
  53. uint32_t savePP_CONTROL;
  54. uint32_t savePP_CYCLE;
  55. uint32_t savePFIT_CONTROL;
  56. uint32_t savePFIT_PGM_RATIOS;
  57. uint32_t saveBLC_PWM_CTL;
  58. };
  59. /*
  60. * Returns the maximum level of the backlight duty cycle field.
  61. */
  62. static u32 cdv_intel_lvds_get_max_backlight(struct drm_device *dev)
  63. {
  64. struct drm_psb_private *dev_priv = dev->dev_private;
  65. u32 retval;
  66. if (gma_power_begin(dev, false)) {
  67. retval = ((REG_READ(BLC_PWM_CTL) &
  68. BACKLIGHT_MODULATION_FREQ_MASK) >>
  69. BACKLIGHT_MODULATION_FREQ_SHIFT) * 2;
  70. gma_power_end(dev);
  71. } else
  72. retval = ((dev_priv->regs.saveBLC_PWM_CTL &
  73. BACKLIGHT_MODULATION_FREQ_MASK) >>
  74. BACKLIGHT_MODULATION_FREQ_SHIFT) * 2;
  75. return retval;
  76. }
  77. #if 0
  78. /*
  79. * Set LVDS backlight level by I2C command
  80. */
  81. static int cdv_lvds_i2c_set_brightness(struct drm_device *dev,
  82. unsigned int level)
  83. {
  84. struct drm_psb_private *dev_priv = dev->dev_private;
  85. struct psb_intel_i2c_chan *lvds_i2c_bus = dev_priv->lvds_i2c_bus;
  86. u8 out_buf[2];
  87. unsigned int blc_i2c_brightness;
  88. struct i2c_msg msgs[] = {
  89. {
  90. .addr = lvds_i2c_bus->slave_addr,
  91. .flags = 0,
  92. .len = 2,
  93. .buf = out_buf,
  94. }
  95. };
  96. blc_i2c_brightness = BRIGHTNESS_MASK & ((unsigned int)level *
  97. BRIGHTNESS_MASK /
  98. BRIGHTNESS_MAX_LEVEL);
  99. if (dev_priv->lvds_bl->pol == BLC_POLARITY_INVERSE)
  100. blc_i2c_brightness = BRIGHTNESS_MASK - blc_i2c_brightness;
  101. out_buf[0] = dev_priv->lvds_bl->brightnesscmd;
  102. out_buf[1] = (u8)blc_i2c_brightness;
  103. if (i2c_transfer(&lvds_i2c_bus->adapter, msgs, 1) == 1)
  104. return 0;
  105. DRM_ERROR("I2C transfer error\n");
  106. return -1;
  107. }
  108. static int cdv_lvds_pwm_set_brightness(struct drm_device *dev, int level)
  109. {
  110. struct drm_psb_private *dev_priv = dev->dev_private;
  111. u32 max_pwm_blc;
  112. u32 blc_pwm_duty_cycle;
  113. max_pwm_blc = cdv_intel_lvds_get_max_backlight(dev);
  114. /*BLC_PWM_CTL Should be initiated while backlight device init*/
  115. BUG_ON((max_pwm_blc & PSB_BLC_MAX_PWM_REG_FREQ) == 0);
  116. blc_pwm_duty_cycle = level * max_pwm_blc / BRIGHTNESS_MAX_LEVEL;
  117. if (dev_priv->lvds_bl->pol == BLC_POLARITY_INVERSE)
  118. blc_pwm_duty_cycle = max_pwm_blc - blc_pwm_duty_cycle;
  119. blc_pwm_duty_cycle &= PSB_BACKLIGHT_PWM_POLARITY_BIT_CLEAR;
  120. REG_WRITE(BLC_PWM_CTL,
  121. (max_pwm_blc << PSB_BACKLIGHT_PWM_CTL_SHIFT) |
  122. (blc_pwm_duty_cycle));
  123. return 0;
  124. }
  125. /*
  126. * Set LVDS backlight level either by I2C or PWM
  127. */
  128. void cdv_intel_lvds_set_brightness(struct drm_device *dev, int level)
  129. {
  130. struct drm_psb_private *dev_priv = dev->dev_private;
  131. if (!dev_priv->lvds_bl) {
  132. DRM_ERROR("NO LVDS Backlight Info\n");
  133. return;
  134. }
  135. if (dev_priv->lvds_bl->type == BLC_I2C_TYPE)
  136. cdv_lvds_i2c_set_brightness(dev, level);
  137. else
  138. cdv_lvds_pwm_set_brightness(dev, level);
  139. }
  140. #endif
  141. /**
  142. * Sets the backlight level.
  143. *
  144. * level backlight level, from 0 to cdv_intel_lvds_get_max_backlight().
  145. */
  146. static void cdv_intel_lvds_set_backlight(struct drm_device *dev, int level)
  147. {
  148. struct drm_psb_private *dev_priv = dev->dev_private;
  149. u32 blc_pwm_ctl;
  150. if (gma_power_begin(dev, false)) {
  151. blc_pwm_ctl =
  152. REG_READ(BLC_PWM_CTL) & ~BACKLIGHT_DUTY_CYCLE_MASK;
  153. REG_WRITE(BLC_PWM_CTL,
  154. (blc_pwm_ctl |
  155. (level << BACKLIGHT_DUTY_CYCLE_SHIFT)));
  156. gma_power_end(dev);
  157. } else {
  158. blc_pwm_ctl = dev_priv->regs.saveBLC_PWM_CTL &
  159. ~BACKLIGHT_DUTY_CYCLE_MASK;
  160. dev_priv->regs.saveBLC_PWM_CTL = (blc_pwm_ctl |
  161. (level << BACKLIGHT_DUTY_CYCLE_SHIFT));
  162. }
  163. }
  164. /**
  165. * Sets the power state for the panel.
  166. */
  167. static void cdv_intel_lvds_set_power(struct drm_device *dev,
  168. struct drm_encoder *encoder, bool on)
  169. {
  170. struct drm_psb_private *dev_priv = dev->dev_private;
  171. u32 pp_status;
  172. if (!gma_power_begin(dev, true))
  173. return;
  174. if (on) {
  175. REG_WRITE(PP_CONTROL, REG_READ(PP_CONTROL) |
  176. POWER_TARGET_ON);
  177. do {
  178. pp_status = REG_READ(PP_STATUS);
  179. } while ((pp_status & PP_ON) == 0);
  180. cdv_intel_lvds_set_backlight(dev,
  181. dev_priv->mode_dev.backlight_duty_cycle);
  182. } else {
  183. cdv_intel_lvds_set_backlight(dev, 0);
  184. REG_WRITE(PP_CONTROL, REG_READ(PP_CONTROL) &
  185. ~POWER_TARGET_ON);
  186. do {
  187. pp_status = REG_READ(PP_STATUS);
  188. } while (pp_status & PP_ON);
  189. }
  190. gma_power_end(dev);
  191. }
  192. static void cdv_intel_lvds_encoder_dpms(struct drm_encoder *encoder, int mode)
  193. {
  194. struct drm_device *dev = encoder->dev;
  195. if (mode == DRM_MODE_DPMS_ON)
  196. cdv_intel_lvds_set_power(dev, encoder, true);
  197. else
  198. cdv_intel_lvds_set_power(dev, encoder, false);
  199. /* XXX: We never power down the LVDS pairs. */
  200. }
  201. static void cdv_intel_lvds_save(struct drm_connector *connector)
  202. {
  203. }
  204. static void cdv_intel_lvds_restore(struct drm_connector *connector)
  205. {
  206. }
  207. static int cdv_intel_lvds_mode_valid(struct drm_connector *connector,
  208. struct drm_display_mode *mode)
  209. {
  210. struct drm_device *dev = connector->dev;
  211. struct drm_psb_private *dev_priv = dev->dev_private;
  212. struct drm_display_mode *fixed_mode =
  213. dev_priv->mode_dev.panel_fixed_mode;
  214. /* just in case */
  215. if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
  216. return MODE_NO_DBLESCAN;
  217. /* just in case */
  218. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  219. return MODE_NO_INTERLACE;
  220. if (fixed_mode) {
  221. if (mode->hdisplay > fixed_mode->hdisplay)
  222. return MODE_PANEL;
  223. if (mode->vdisplay > fixed_mode->vdisplay)
  224. return MODE_PANEL;
  225. }
  226. return MODE_OK;
  227. }
  228. static bool cdv_intel_lvds_mode_fixup(struct drm_encoder *encoder,
  229. const struct drm_display_mode *mode,
  230. struct drm_display_mode *adjusted_mode)
  231. {
  232. struct drm_device *dev = encoder->dev;
  233. struct drm_psb_private *dev_priv = dev->dev_private;
  234. struct psb_intel_mode_device *mode_dev = &dev_priv->mode_dev;
  235. struct drm_encoder *tmp_encoder;
  236. struct drm_display_mode *panel_fixed_mode = mode_dev->panel_fixed_mode;
  237. /* Should never happen!! */
  238. list_for_each_entry(tmp_encoder, &dev->mode_config.encoder_list,
  239. head) {
  240. if (tmp_encoder != encoder
  241. && tmp_encoder->crtc == encoder->crtc) {
  242. printk(KERN_ERR "Can't enable LVDS and another "
  243. "encoder on the same pipe\n");
  244. return false;
  245. }
  246. }
  247. /*
  248. * If we have timings from the BIOS for the panel, put them in
  249. * to the adjusted mode. The CRTC will be set up for this mode,
  250. * with the panel scaling set up to source from the H/VDisplay
  251. * of the original mode.
  252. */
  253. if (panel_fixed_mode != NULL) {
  254. adjusted_mode->hdisplay = panel_fixed_mode->hdisplay;
  255. adjusted_mode->hsync_start = panel_fixed_mode->hsync_start;
  256. adjusted_mode->hsync_end = panel_fixed_mode->hsync_end;
  257. adjusted_mode->htotal = panel_fixed_mode->htotal;
  258. adjusted_mode->vdisplay = panel_fixed_mode->vdisplay;
  259. adjusted_mode->vsync_start = panel_fixed_mode->vsync_start;
  260. adjusted_mode->vsync_end = panel_fixed_mode->vsync_end;
  261. adjusted_mode->vtotal = panel_fixed_mode->vtotal;
  262. adjusted_mode->clock = panel_fixed_mode->clock;
  263. drm_mode_set_crtcinfo(adjusted_mode,
  264. CRTC_INTERLACE_HALVE_V);
  265. }
  266. /*
  267. * XXX: It would be nice to support lower refresh rates on the
  268. * panels to reduce power consumption, and perhaps match the
  269. * user's requested refresh rate.
  270. */
  271. return true;
  272. }
  273. static void cdv_intel_lvds_prepare(struct drm_encoder *encoder)
  274. {
  275. struct drm_device *dev = encoder->dev;
  276. struct drm_psb_private *dev_priv = dev->dev_private;
  277. struct psb_intel_mode_device *mode_dev = &dev_priv->mode_dev;
  278. if (!gma_power_begin(dev, true))
  279. return;
  280. mode_dev->saveBLC_PWM_CTL = REG_READ(BLC_PWM_CTL);
  281. mode_dev->backlight_duty_cycle = (mode_dev->saveBLC_PWM_CTL &
  282. BACKLIGHT_DUTY_CYCLE_MASK);
  283. cdv_intel_lvds_set_power(dev, encoder, false);
  284. gma_power_end(dev);
  285. }
  286. static void cdv_intel_lvds_commit(struct drm_encoder *encoder)
  287. {
  288. struct drm_device *dev = encoder->dev;
  289. struct drm_psb_private *dev_priv = dev->dev_private;
  290. struct psb_intel_mode_device *mode_dev = &dev_priv->mode_dev;
  291. if (mode_dev->backlight_duty_cycle == 0)
  292. mode_dev->backlight_duty_cycle =
  293. cdv_intel_lvds_get_max_backlight(dev);
  294. cdv_intel_lvds_set_power(dev, encoder, true);
  295. }
  296. static void cdv_intel_lvds_mode_set(struct drm_encoder *encoder,
  297. struct drm_display_mode *mode,
  298. struct drm_display_mode *adjusted_mode)
  299. {
  300. struct drm_device *dev = encoder->dev;
  301. struct drm_psb_private *dev_priv = dev->dev_private;
  302. struct gma_crtc *gma_crtc = to_gma_crtc(encoder->crtc);
  303. u32 pfit_control;
  304. /*
  305. * The LVDS pin pair will already have been turned on in the
  306. * cdv_intel_crtc_mode_set since it has a large impact on the DPLL
  307. * settings.
  308. */
  309. /*
  310. * Enable automatic panel scaling so that non-native modes fill the
  311. * screen. Should be enabled before the pipe is enabled, according to
  312. * register description and PRM.
  313. */
  314. if (mode->hdisplay != adjusted_mode->hdisplay ||
  315. mode->vdisplay != adjusted_mode->vdisplay)
  316. pfit_control = (PFIT_ENABLE | VERT_AUTO_SCALE |
  317. HORIZ_AUTO_SCALE | VERT_INTERP_BILINEAR |
  318. HORIZ_INTERP_BILINEAR);
  319. else
  320. pfit_control = 0;
  321. pfit_control |= gma_crtc->pipe << PFIT_PIPE_SHIFT;
  322. if (dev_priv->lvds_dither)
  323. pfit_control |= PANEL_8TO6_DITHER_ENABLE;
  324. REG_WRITE(PFIT_CONTROL, pfit_control);
  325. }
  326. /**
  327. * Detect the LVDS connection.
  328. *
  329. * This always returns CONNECTOR_STATUS_CONNECTED.
  330. * This connector should only have
  331. * been set up if the LVDS was actually connected anyway.
  332. */
  333. static enum drm_connector_status cdv_intel_lvds_detect(
  334. struct drm_connector *connector, bool force)
  335. {
  336. return connector_status_connected;
  337. }
  338. /**
  339. * Return the list of DDC modes if available, or the BIOS fixed mode otherwise.
  340. */
  341. static int cdv_intel_lvds_get_modes(struct drm_connector *connector)
  342. {
  343. struct drm_device *dev = connector->dev;
  344. struct drm_psb_private *dev_priv = dev->dev_private;
  345. struct gma_encoder *gma_encoder = gma_attached_encoder(connector);
  346. struct psb_intel_mode_device *mode_dev = &dev_priv->mode_dev;
  347. int ret;
  348. ret = psb_intel_ddc_get_modes(connector, &gma_encoder->i2c_bus->adapter);
  349. if (ret)
  350. return ret;
  351. /* Didn't get an EDID, so
  352. * Set wide sync ranges so we get all modes
  353. * handed to valid_mode for checking
  354. */
  355. connector->display_info.min_vfreq = 0;
  356. connector->display_info.max_vfreq = 200;
  357. connector->display_info.min_hfreq = 0;
  358. connector->display_info.max_hfreq = 200;
  359. if (mode_dev->panel_fixed_mode != NULL) {
  360. struct drm_display_mode *mode =
  361. drm_mode_duplicate(dev, mode_dev->panel_fixed_mode);
  362. drm_mode_probed_add(connector, mode);
  363. return 1;
  364. }
  365. return 0;
  366. }
  367. /**
  368. * cdv_intel_lvds_destroy - unregister and free LVDS structures
  369. * @connector: connector to free
  370. *
  371. * Unregister the DDC bus for this connector then free the driver private
  372. * structure.
  373. */
  374. static void cdv_intel_lvds_destroy(struct drm_connector *connector)
  375. {
  376. struct gma_encoder *gma_encoder = gma_attached_encoder(connector);
  377. if (gma_encoder->i2c_bus)
  378. psb_intel_i2c_destroy(gma_encoder->i2c_bus);
  379. drm_connector_unregister(connector);
  380. drm_connector_cleanup(connector);
  381. kfree(connector);
  382. }
  383. static int cdv_intel_lvds_set_property(struct drm_connector *connector,
  384. struct drm_property *property,
  385. uint64_t value)
  386. {
  387. struct drm_encoder *encoder = connector->encoder;
  388. if (!strcmp(property->name, "scaling mode") && encoder) {
  389. struct gma_crtc *crtc = to_gma_crtc(encoder->crtc);
  390. uint64_t curValue;
  391. if (!crtc)
  392. return -1;
  393. switch (value) {
  394. case DRM_MODE_SCALE_FULLSCREEN:
  395. break;
  396. case DRM_MODE_SCALE_NO_SCALE:
  397. break;
  398. case DRM_MODE_SCALE_ASPECT:
  399. break;
  400. default:
  401. return -1;
  402. }
  403. if (drm_object_property_get_value(&connector->base,
  404. property,
  405. &curValue))
  406. return -1;
  407. if (curValue == value)
  408. return 0;
  409. if (drm_object_property_set_value(&connector->base,
  410. property,
  411. value))
  412. return -1;
  413. if (crtc->saved_mode.hdisplay != 0 &&
  414. crtc->saved_mode.vdisplay != 0) {
  415. if (!drm_crtc_helper_set_mode(encoder->crtc,
  416. &crtc->saved_mode,
  417. encoder->crtc->x,
  418. encoder->crtc->y,
  419. encoder->crtc->primary->fb))
  420. return -1;
  421. }
  422. } else if (!strcmp(property->name, "backlight") && encoder) {
  423. if (drm_object_property_set_value(&connector->base,
  424. property,
  425. value))
  426. return -1;
  427. else
  428. gma_backlight_set(encoder->dev, value);
  429. } else if (!strcmp(property->name, "DPMS") && encoder) {
  430. const struct drm_encoder_helper_funcs *helpers =
  431. encoder->helper_private;
  432. helpers->dpms(encoder, value);
  433. }
  434. return 0;
  435. }
  436. static const struct drm_encoder_helper_funcs
  437. cdv_intel_lvds_helper_funcs = {
  438. .dpms = cdv_intel_lvds_encoder_dpms,
  439. .mode_fixup = cdv_intel_lvds_mode_fixup,
  440. .prepare = cdv_intel_lvds_prepare,
  441. .mode_set = cdv_intel_lvds_mode_set,
  442. .commit = cdv_intel_lvds_commit,
  443. };
  444. static const struct drm_connector_helper_funcs
  445. cdv_intel_lvds_connector_helper_funcs = {
  446. .get_modes = cdv_intel_lvds_get_modes,
  447. .mode_valid = cdv_intel_lvds_mode_valid,
  448. .best_encoder = gma_best_encoder,
  449. };
  450. static const struct drm_connector_funcs cdv_intel_lvds_connector_funcs = {
  451. .dpms = drm_helper_connector_dpms,
  452. .save = cdv_intel_lvds_save,
  453. .restore = cdv_intel_lvds_restore,
  454. .detect = cdv_intel_lvds_detect,
  455. .fill_modes = drm_helper_probe_single_connector_modes,
  456. .set_property = cdv_intel_lvds_set_property,
  457. .destroy = cdv_intel_lvds_destroy,
  458. };
  459. static void cdv_intel_lvds_enc_destroy(struct drm_encoder *encoder)
  460. {
  461. drm_encoder_cleanup(encoder);
  462. }
  463. static const struct drm_encoder_funcs cdv_intel_lvds_enc_funcs = {
  464. .destroy = cdv_intel_lvds_enc_destroy,
  465. };
  466. /*
  467. * Enumerate the child dev array parsed from VBT to check whether
  468. * the LVDS is present.
  469. * If it is present, return 1.
  470. * If it is not present, return false.
  471. * If no child dev is parsed from VBT, it assumes that the LVDS is present.
  472. */
  473. static bool lvds_is_present_in_vbt(struct drm_device *dev,
  474. u8 *i2c_pin)
  475. {
  476. struct drm_psb_private *dev_priv = dev->dev_private;
  477. int i;
  478. if (!dev_priv->child_dev_num)
  479. return true;
  480. for (i = 0; i < dev_priv->child_dev_num; i++) {
  481. struct child_device_config *child = dev_priv->child_dev + i;
  482. /* If the device type is not LFP, continue.
  483. * We have to check both the new identifiers as well as the
  484. * old for compatibility with some BIOSes.
  485. */
  486. if (child->device_type != DEVICE_TYPE_INT_LFP &&
  487. child->device_type != DEVICE_TYPE_LFP)
  488. continue;
  489. if (child->i2c_pin)
  490. *i2c_pin = child->i2c_pin;
  491. /* However, we cannot trust the BIOS writers to populate
  492. * the VBT correctly. Since LVDS requires additional
  493. * information from AIM blocks, a non-zero addin offset is
  494. * a good indicator that the LVDS is actually present.
  495. */
  496. if (child->addin_offset)
  497. return true;
  498. /* But even then some BIOS writers perform some black magic
  499. * and instantiate the device without reference to any
  500. * additional data. Trust that if the VBT was written into
  501. * the OpRegion then they have validated the LVDS's existence.
  502. */
  503. if (dev_priv->opregion.vbt)
  504. return true;
  505. }
  506. return false;
  507. }
  508. /**
  509. * cdv_intel_lvds_init - setup LVDS connectors on this device
  510. * @dev: drm device
  511. *
  512. * Create the connector, register the LVDS DDC bus, and try to figure out what
  513. * modes we can display on the LVDS panel (if present).
  514. */
  515. void cdv_intel_lvds_init(struct drm_device *dev,
  516. struct psb_intel_mode_device *mode_dev)
  517. {
  518. struct gma_encoder *gma_encoder;
  519. struct gma_connector *gma_connector;
  520. struct cdv_intel_lvds_priv *lvds_priv;
  521. struct drm_connector *connector;
  522. struct drm_encoder *encoder;
  523. struct drm_display_mode *scan;
  524. struct drm_crtc *crtc;
  525. struct drm_psb_private *dev_priv = dev->dev_private;
  526. u32 lvds;
  527. int pipe;
  528. u8 pin;
  529. pin = GMBUS_PORT_PANEL;
  530. if (!lvds_is_present_in_vbt(dev, &pin)) {
  531. DRM_DEBUG_KMS("LVDS is not present in VBT\n");
  532. return;
  533. }
  534. gma_encoder = kzalloc(sizeof(struct gma_encoder),
  535. GFP_KERNEL);
  536. if (!gma_encoder)
  537. return;
  538. gma_connector = kzalloc(sizeof(struct gma_connector),
  539. GFP_KERNEL);
  540. if (!gma_connector)
  541. goto failed_connector;
  542. lvds_priv = kzalloc(sizeof(struct cdv_intel_lvds_priv), GFP_KERNEL);
  543. if (!lvds_priv)
  544. goto failed_lvds_priv;
  545. gma_encoder->dev_priv = lvds_priv;
  546. connector = &gma_connector->base;
  547. encoder = &gma_encoder->base;
  548. drm_connector_init(dev, connector,
  549. &cdv_intel_lvds_connector_funcs,
  550. DRM_MODE_CONNECTOR_LVDS);
  551. drm_encoder_init(dev, encoder,
  552. &cdv_intel_lvds_enc_funcs,
  553. DRM_MODE_ENCODER_LVDS);
  554. gma_connector_attach_encoder(gma_connector, gma_encoder);
  555. gma_encoder->type = INTEL_OUTPUT_LVDS;
  556. drm_encoder_helper_add(encoder, &cdv_intel_lvds_helper_funcs);
  557. drm_connector_helper_add(connector,
  558. &cdv_intel_lvds_connector_helper_funcs);
  559. connector->display_info.subpixel_order = SubPixelHorizontalRGB;
  560. connector->interlace_allowed = false;
  561. connector->doublescan_allowed = false;
  562. /*Attach connector properties*/
  563. drm_object_attach_property(&connector->base,
  564. dev->mode_config.scaling_mode_property,
  565. DRM_MODE_SCALE_FULLSCREEN);
  566. drm_object_attach_property(&connector->base,
  567. dev_priv->backlight_property,
  568. BRIGHTNESS_MAX_LEVEL);
  569. /**
  570. * Set up I2C bus
  571. * FIXME: distroy i2c_bus when exit
  572. */
  573. gma_encoder->i2c_bus = psb_intel_i2c_create(dev,
  574. GPIOB,
  575. "LVDSBLC_B");
  576. if (!gma_encoder->i2c_bus) {
  577. dev_printk(KERN_ERR,
  578. &dev->pdev->dev, "I2C bus registration failed.\n");
  579. goto failed_blc_i2c;
  580. }
  581. gma_encoder->i2c_bus->slave_addr = 0x2C;
  582. dev_priv->lvds_i2c_bus = gma_encoder->i2c_bus;
  583. /*
  584. * LVDS discovery:
  585. * 1) check for EDID on DDC
  586. * 2) check for VBT data
  587. * 3) check to see if LVDS is already on
  588. * if none of the above, no panel
  589. * 4) make sure lid is open
  590. * if closed, act like it's not there for now
  591. */
  592. /* Set up the DDC bus. */
  593. gma_encoder->ddc_bus = psb_intel_i2c_create(dev,
  594. GPIOC,
  595. "LVDSDDC_C");
  596. if (!gma_encoder->ddc_bus) {
  597. dev_printk(KERN_ERR, &dev->pdev->dev,
  598. "DDC bus registration " "failed.\n");
  599. goto failed_ddc;
  600. }
  601. /*
  602. * Attempt to get the fixed panel mode from DDC. Assume that the
  603. * preferred mode is the right one.
  604. */
  605. mutex_lock(&dev->mode_config.mutex);
  606. psb_intel_ddc_get_modes(connector,
  607. &gma_encoder->ddc_bus->adapter);
  608. list_for_each_entry(scan, &connector->probed_modes, head) {
  609. if (scan->type & DRM_MODE_TYPE_PREFERRED) {
  610. mode_dev->panel_fixed_mode =
  611. drm_mode_duplicate(dev, scan);
  612. goto out; /* FIXME: check for quirks */
  613. }
  614. }
  615. /* Failed to get EDID, what about VBT? do we need this?*/
  616. if (dev_priv->lfp_lvds_vbt_mode) {
  617. mode_dev->panel_fixed_mode =
  618. drm_mode_duplicate(dev, dev_priv->lfp_lvds_vbt_mode);
  619. if (mode_dev->panel_fixed_mode) {
  620. mode_dev->panel_fixed_mode->type |=
  621. DRM_MODE_TYPE_PREFERRED;
  622. goto out; /* FIXME: check for quirks */
  623. }
  624. }
  625. /*
  626. * If we didn't get EDID, try checking if the panel is already turned
  627. * on. If so, assume that whatever is currently programmed is the
  628. * correct mode.
  629. */
  630. lvds = REG_READ(LVDS);
  631. pipe = (lvds & LVDS_PIPEB_SELECT) ? 1 : 0;
  632. crtc = psb_intel_get_crtc_from_pipe(dev, pipe);
  633. if (crtc && (lvds & LVDS_PORT_EN)) {
  634. mode_dev->panel_fixed_mode =
  635. cdv_intel_crtc_mode_get(dev, crtc);
  636. if (mode_dev->panel_fixed_mode) {
  637. mode_dev->panel_fixed_mode->type |=
  638. DRM_MODE_TYPE_PREFERRED;
  639. goto out; /* FIXME: check for quirks */
  640. }
  641. }
  642. /* If we still don't have a mode after all that, give up. */
  643. if (!mode_dev->panel_fixed_mode) {
  644. DRM_DEBUG
  645. ("Found no modes on the lvds, ignoring the LVDS\n");
  646. goto failed_find;
  647. }
  648. /* setup PWM */
  649. {
  650. u32 pwm;
  651. pwm = REG_READ(BLC_PWM_CTL2);
  652. if (pipe == 1)
  653. pwm |= PWM_PIPE_B;
  654. else
  655. pwm &= ~PWM_PIPE_B;
  656. pwm |= PWM_ENABLE;
  657. REG_WRITE(BLC_PWM_CTL2, pwm);
  658. }
  659. out:
  660. mutex_unlock(&dev->mode_config.mutex);
  661. drm_connector_register(connector);
  662. return;
  663. failed_find:
  664. mutex_unlock(&dev->mode_config.mutex);
  665. printk(KERN_ERR "Failed find\n");
  666. if (gma_encoder->ddc_bus)
  667. psb_intel_i2c_destroy(gma_encoder->ddc_bus);
  668. failed_ddc:
  669. printk(KERN_ERR "Failed DDC\n");
  670. if (gma_encoder->i2c_bus)
  671. psb_intel_i2c_destroy(gma_encoder->i2c_bus);
  672. failed_blc_i2c:
  673. printk(KERN_ERR "Failed BLC\n");
  674. drm_encoder_cleanup(encoder);
  675. drm_connector_cleanup(connector);
  676. kfree(lvds_priv);
  677. failed_lvds_priv:
  678. kfree(gma_connector);
  679. failed_connector:
  680. kfree(gma_encoder);
  681. }