psb_intel_display.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  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. */
  20. #include <linux/i2c.h>
  21. #include <linux/pm_runtime.h>
  22. #include <drm/drmP.h>
  23. #include "framebuffer.h"
  24. #include "psb_drv.h"
  25. #include "psb_intel_drv.h"
  26. #include "psb_intel_reg.h"
  27. #include "gma_display.h"
  28. #include "power.h"
  29. #define INTEL_LIMIT_I9XX_SDVO_DAC 0
  30. #define INTEL_LIMIT_I9XX_LVDS 1
  31. static const struct gma_limit_t psb_intel_limits[] = {
  32. { /* INTEL_LIMIT_I9XX_SDVO_DAC */
  33. .dot = {.min = 20000, .max = 400000},
  34. .vco = {.min = 1400000, .max = 2800000},
  35. .n = {.min = 1, .max = 6},
  36. .m = {.min = 70, .max = 120},
  37. .m1 = {.min = 8, .max = 18},
  38. .m2 = {.min = 3, .max = 7},
  39. .p = {.min = 5, .max = 80},
  40. .p1 = {.min = 1, .max = 8},
  41. .p2 = {.dot_limit = 200000, .p2_slow = 10, .p2_fast = 5},
  42. .find_pll = gma_find_best_pll,
  43. },
  44. { /* INTEL_LIMIT_I9XX_LVDS */
  45. .dot = {.min = 20000, .max = 400000},
  46. .vco = {.min = 1400000, .max = 2800000},
  47. .n = {.min = 1, .max = 6},
  48. .m = {.min = 70, .max = 120},
  49. .m1 = {.min = 8, .max = 18},
  50. .m2 = {.min = 3, .max = 7},
  51. .p = {.min = 7, .max = 98},
  52. .p1 = {.min = 1, .max = 8},
  53. /* The single-channel range is 25-112Mhz, and dual-channel
  54. * is 80-224Mhz. Prefer single channel as much as possible.
  55. */
  56. .p2 = {.dot_limit = 112000, .p2_slow = 14, .p2_fast = 7},
  57. .find_pll = gma_find_best_pll,
  58. },
  59. };
  60. static const struct gma_limit_t *psb_intel_limit(struct drm_crtc *crtc,
  61. int refclk)
  62. {
  63. const struct gma_limit_t *limit;
  64. if (gma_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  65. limit = &psb_intel_limits[INTEL_LIMIT_I9XX_LVDS];
  66. else
  67. limit = &psb_intel_limits[INTEL_LIMIT_I9XX_SDVO_DAC];
  68. return limit;
  69. }
  70. static void psb_intel_clock(int refclk, struct gma_clock_t *clock)
  71. {
  72. clock->m = 5 * (clock->m1 + 2) + (clock->m2 + 2);
  73. clock->p = clock->p1 * clock->p2;
  74. clock->vco = refclk * clock->m / (clock->n + 2);
  75. clock->dot = clock->vco / clock->p;
  76. }
  77. void psb_intel_encoder_prepare(struct drm_encoder *encoder)
  78. {
  79. struct drm_encoder_helper_funcs *encoder_funcs =
  80. encoder->helper_private;
  81. /* lvds has its own version of prepare see psb_intel_lvds_prepare */
  82. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
  83. }
  84. void psb_intel_encoder_commit(struct drm_encoder *encoder)
  85. {
  86. struct drm_encoder_helper_funcs *encoder_funcs =
  87. encoder->helper_private;
  88. /* lvds has its own version of commit see psb_intel_lvds_commit */
  89. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
  90. }
  91. void psb_intel_encoder_destroy(struct drm_encoder *encoder)
  92. {
  93. struct psb_intel_encoder *intel_encoder = to_psb_intel_encoder(encoder);
  94. drm_encoder_cleanup(encoder);
  95. kfree(intel_encoder);
  96. }
  97. /**
  98. * Return the pipe currently connected to the panel fitter,
  99. * or -1 if the panel fitter is not present or not in use
  100. */
  101. static int psb_intel_panel_fitter_pipe(struct drm_device *dev)
  102. {
  103. u32 pfit_control;
  104. pfit_control = REG_READ(PFIT_CONTROL);
  105. /* See if the panel fitter is in use */
  106. if ((pfit_control & PFIT_ENABLE) == 0)
  107. return -1;
  108. /* Must be on PIPE 1 for PSB */
  109. return 1;
  110. }
  111. static int psb_intel_crtc_mode_set(struct drm_crtc *crtc,
  112. struct drm_display_mode *mode,
  113. struct drm_display_mode *adjusted_mode,
  114. int x, int y,
  115. struct drm_framebuffer *old_fb)
  116. {
  117. struct drm_device *dev = crtc->dev;
  118. struct drm_psb_private *dev_priv = dev->dev_private;
  119. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  120. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  121. int pipe = psb_intel_crtc->pipe;
  122. const struct psb_offset *map = &dev_priv->regmap[pipe];
  123. int refclk;
  124. struct gma_clock_t clock;
  125. u32 dpll = 0, fp = 0, dspcntr, pipeconf;
  126. bool ok, is_sdvo = false;
  127. bool is_lvds = false, is_tv = false;
  128. struct drm_mode_config *mode_config = &dev->mode_config;
  129. struct drm_connector *connector;
  130. const struct gma_limit_t *limit;
  131. /* No scan out no play */
  132. if (crtc->fb == NULL) {
  133. crtc_funcs->mode_set_base(crtc, x, y, old_fb);
  134. return 0;
  135. }
  136. list_for_each_entry(connector, &mode_config->connector_list, head) {
  137. struct psb_intel_encoder *psb_intel_encoder =
  138. psb_intel_attached_encoder(connector);
  139. if (!connector->encoder
  140. || connector->encoder->crtc != crtc)
  141. continue;
  142. switch (psb_intel_encoder->type) {
  143. case INTEL_OUTPUT_LVDS:
  144. is_lvds = true;
  145. break;
  146. case INTEL_OUTPUT_SDVO:
  147. is_sdvo = true;
  148. break;
  149. case INTEL_OUTPUT_TVOUT:
  150. is_tv = true;
  151. break;
  152. }
  153. }
  154. refclk = 96000;
  155. limit = psb_intel_crtc->clock_funcs->limit(crtc, refclk);
  156. ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk,
  157. &clock);
  158. if (!ok) {
  159. DRM_ERROR("Couldn't find PLL settings for mode! target: %d, actual: %d",
  160. adjusted_mode->clock, clock.dot);
  161. return 0;
  162. }
  163. fp = clock.n << 16 | clock.m1 << 8 | clock.m2;
  164. dpll = DPLL_VGA_MODE_DIS;
  165. if (is_lvds) {
  166. dpll |= DPLLB_MODE_LVDS;
  167. dpll |= DPLL_DVO_HIGH_SPEED;
  168. } else
  169. dpll |= DPLLB_MODE_DAC_SERIAL;
  170. if (is_sdvo) {
  171. int sdvo_pixel_multiply =
  172. adjusted_mode->clock / mode->clock;
  173. dpll |= DPLL_DVO_HIGH_SPEED;
  174. dpll |=
  175. (sdvo_pixel_multiply - 1) << SDVO_MULTIPLIER_SHIFT_HIRES;
  176. }
  177. /* compute bitmask from p1 value */
  178. dpll |= (1 << (clock.p1 - 1)) << 16;
  179. switch (clock.p2) {
  180. case 5:
  181. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  182. break;
  183. case 7:
  184. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  185. break;
  186. case 10:
  187. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  188. break;
  189. case 14:
  190. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  191. break;
  192. }
  193. if (is_tv) {
  194. /* XXX: just matching BIOS for now */
  195. /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
  196. dpll |= 3;
  197. }
  198. dpll |= PLL_REF_INPUT_DREFCLK;
  199. /* setup pipeconf */
  200. pipeconf = REG_READ(map->conf);
  201. /* Set up the display plane register */
  202. dspcntr = DISPPLANE_GAMMA_ENABLE;
  203. if (pipe == 0)
  204. dspcntr |= DISPPLANE_SEL_PIPE_A;
  205. else
  206. dspcntr |= DISPPLANE_SEL_PIPE_B;
  207. dspcntr |= DISPLAY_PLANE_ENABLE;
  208. pipeconf |= PIPEACONF_ENABLE;
  209. dpll |= DPLL_VCO_ENABLE;
  210. /* Disable the panel fitter if it was on our pipe */
  211. if (psb_intel_panel_fitter_pipe(dev) == pipe)
  212. REG_WRITE(PFIT_CONTROL, 0);
  213. drm_mode_debug_printmodeline(mode);
  214. if (dpll & DPLL_VCO_ENABLE) {
  215. REG_WRITE(map->fp0, fp);
  216. REG_WRITE(map->dpll, dpll & ~DPLL_VCO_ENABLE);
  217. REG_READ(map->dpll);
  218. udelay(150);
  219. }
  220. /* The LVDS pin pair needs to be on before the DPLLs are enabled.
  221. * This is an exception to the general rule that mode_set doesn't turn
  222. * things on.
  223. */
  224. if (is_lvds) {
  225. u32 lvds = REG_READ(LVDS);
  226. lvds &= ~LVDS_PIPEB_SELECT;
  227. if (pipe == 1)
  228. lvds |= LVDS_PIPEB_SELECT;
  229. lvds |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP;
  230. /* Set the B0-B3 data pairs corresponding to
  231. * whether we're going to
  232. * set the DPLLs for dual-channel mode or not.
  233. */
  234. lvds &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
  235. if (clock.p2 == 7)
  236. lvds |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
  237. /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
  238. * appropriately here, but we need to look more
  239. * thoroughly into how panels behave in the two modes.
  240. */
  241. REG_WRITE(LVDS, lvds);
  242. REG_READ(LVDS);
  243. }
  244. REG_WRITE(map->fp0, fp);
  245. REG_WRITE(map->dpll, dpll);
  246. REG_READ(map->dpll);
  247. /* Wait for the clocks to stabilize. */
  248. udelay(150);
  249. /* write it again -- the BIOS does, after all */
  250. REG_WRITE(map->dpll, dpll);
  251. REG_READ(map->dpll);
  252. /* Wait for the clocks to stabilize. */
  253. udelay(150);
  254. REG_WRITE(map->htotal, (adjusted_mode->crtc_hdisplay - 1) |
  255. ((adjusted_mode->crtc_htotal - 1) << 16));
  256. REG_WRITE(map->hblank, (adjusted_mode->crtc_hblank_start - 1) |
  257. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  258. REG_WRITE(map->hsync, (adjusted_mode->crtc_hsync_start - 1) |
  259. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  260. REG_WRITE(map->vtotal, (adjusted_mode->crtc_vdisplay - 1) |
  261. ((adjusted_mode->crtc_vtotal - 1) << 16));
  262. REG_WRITE(map->vblank, (adjusted_mode->crtc_vblank_start - 1) |
  263. ((adjusted_mode->crtc_vblank_end - 1) << 16));
  264. REG_WRITE(map->vsync, (adjusted_mode->crtc_vsync_start - 1) |
  265. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  266. /* pipesrc and dspsize control the size that is scaled from,
  267. * which should always be the user's requested size.
  268. */
  269. REG_WRITE(map->size,
  270. ((mode->vdisplay - 1) << 16) | (mode->hdisplay - 1));
  271. REG_WRITE(map->pos, 0);
  272. REG_WRITE(map->src,
  273. ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
  274. REG_WRITE(map->conf, pipeconf);
  275. REG_READ(map->conf);
  276. gma_wait_for_vblank(dev);
  277. REG_WRITE(map->cntr, dspcntr);
  278. /* Flush the plane changes */
  279. crtc_funcs->mode_set_base(crtc, x, y, old_fb);
  280. gma_wait_for_vblank(dev);
  281. return 0;
  282. }
  283. /**
  284. * Save HW states of giving crtc
  285. */
  286. static void psb_intel_crtc_save(struct drm_crtc *crtc)
  287. {
  288. struct drm_device *dev = crtc->dev;
  289. struct drm_psb_private *dev_priv = dev->dev_private;
  290. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  291. struct psb_intel_crtc_state *crtc_state = psb_intel_crtc->crtc_state;
  292. const struct psb_offset *map = &dev_priv->regmap[psb_intel_crtc->pipe];
  293. uint32_t paletteReg;
  294. int i;
  295. if (!crtc_state) {
  296. dev_err(dev->dev, "No CRTC state found\n");
  297. return;
  298. }
  299. crtc_state->saveDSPCNTR = REG_READ(map->cntr);
  300. crtc_state->savePIPECONF = REG_READ(map->conf);
  301. crtc_state->savePIPESRC = REG_READ(map->src);
  302. crtc_state->saveFP0 = REG_READ(map->fp0);
  303. crtc_state->saveFP1 = REG_READ(map->fp1);
  304. crtc_state->saveDPLL = REG_READ(map->dpll);
  305. crtc_state->saveHTOTAL = REG_READ(map->htotal);
  306. crtc_state->saveHBLANK = REG_READ(map->hblank);
  307. crtc_state->saveHSYNC = REG_READ(map->hsync);
  308. crtc_state->saveVTOTAL = REG_READ(map->vtotal);
  309. crtc_state->saveVBLANK = REG_READ(map->vblank);
  310. crtc_state->saveVSYNC = REG_READ(map->vsync);
  311. crtc_state->saveDSPSTRIDE = REG_READ(map->stride);
  312. /*NOTE: DSPSIZE DSPPOS only for psb*/
  313. crtc_state->saveDSPSIZE = REG_READ(map->size);
  314. crtc_state->saveDSPPOS = REG_READ(map->pos);
  315. crtc_state->saveDSPBASE = REG_READ(map->base);
  316. paletteReg = map->palette;
  317. for (i = 0; i < 256; ++i)
  318. crtc_state->savePalette[i] = REG_READ(paletteReg + (i << 2));
  319. }
  320. /**
  321. * Restore HW states of giving crtc
  322. */
  323. static void psb_intel_crtc_restore(struct drm_crtc *crtc)
  324. {
  325. struct drm_device *dev = crtc->dev;
  326. struct drm_psb_private *dev_priv = dev->dev_private;
  327. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  328. struct psb_intel_crtc_state *crtc_state = psb_intel_crtc->crtc_state;
  329. const struct psb_offset *map = &dev_priv->regmap[psb_intel_crtc->pipe];
  330. uint32_t paletteReg;
  331. int i;
  332. if (!crtc_state) {
  333. dev_err(dev->dev, "No crtc state\n");
  334. return;
  335. }
  336. if (crtc_state->saveDPLL & DPLL_VCO_ENABLE) {
  337. REG_WRITE(map->dpll,
  338. crtc_state->saveDPLL & ~DPLL_VCO_ENABLE);
  339. REG_READ(map->dpll);
  340. udelay(150);
  341. }
  342. REG_WRITE(map->fp0, crtc_state->saveFP0);
  343. REG_READ(map->fp0);
  344. REG_WRITE(map->fp1, crtc_state->saveFP1);
  345. REG_READ(map->fp1);
  346. REG_WRITE(map->dpll, crtc_state->saveDPLL);
  347. REG_READ(map->dpll);
  348. udelay(150);
  349. REG_WRITE(map->htotal, crtc_state->saveHTOTAL);
  350. REG_WRITE(map->hblank, crtc_state->saveHBLANK);
  351. REG_WRITE(map->hsync, crtc_state->saveHSYNC);
  352. REG_WRITE(map->vtotal, crtc_state->saveVTOTAL);
  353. REG_WRITE(map->vblank, crtc_state->saveVBLANK);
  354. REG_WRITE(map->vsync, crtc_state->saveVSYNC);
  355. REG_WRITE(map->stride, crtc_state->saveDSPSTRIDE);
  356. REG_WRITE(map->size, crtc_state->saveDSPSIZE);
  357. REG_WRITE(map->pos, crtc_state->saveDSPPOS);
  358. REG_WRITE(map->src, crtc_state->savePIPESRC);
  359. REG_WRITE(map->base, crtc_state->saveDSPBASE);
  360. REG_WRITE(map->conf, crtc_state->savePIPECONF);
  361. gma_wait_for_vblank(dev);
  362. REG_WRITE(map->cntr, crtc_state->saveDSPCNTR);
  363. REG_WRITE(map->base, crtc_state->saveDSPBASE);
  364. gma_wait_for_vblank(dev);
  365. paletteReg = map->palette;
  366. for (i = 0; i < 256; ++i)
  367. REG_WRITE(paletteReg + (i << 2), crtc_state->savePalette[i]);
  368. }
  369. static int psb_crtc_set_config(struct drm_mode_set *set)
  370. {
  371. int ret;
  372. struct drm_device *dev = set->crtc->dev;
  373. struct drm_psb_private *dev_priv = dev->dev_private;
  374. if (!dev_priv->rpm_enabled)
  375. return drm_crtc_helper_set_config(set);
  376. pm_runtime_forbid(&dev->pdev->dev);
  377. ret = drm_crtc_helper_set_config(set);
  378. pm_runtime_allow(&dev->pdev->dev);
  379. return ret;
  380. }
  381. /* Returns the clock of the currently programmed mode of the given pipe. */
  382. static int psb_intel_crtc_clock_get(struct drm_device *dev,
  383. struct drm_crtc *crtc)
  384. {
  385. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  386. struct drm_psb_private *dev_priv = dev->dev_private;
  387. int pipe = psb_intel_crtc->pipe;
  388. const struct psb_offset *map = &dev_priv->regmap[pipe];
  389. u32 dpll;
  390. u32 fp;
  391. struct gma_clock_t clock;
  392. bool is_lvds;
  393. struct psb_pipe *p = &dev_priv->regs.pipe[pipe];
  394. if (gma_power_begin(dev, false)) {
  395. dpll = REG_READ(map->dpll);
  396. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  397. fp = REG_READ(map->fp0);
  398. else
  399. fp = REG_READ(map->fp1);
  400. is_lvds = (pipe == 1) && (REG_READ(LVDS) & LVDS_PORT_EN);
  401. gma_power_end(dev);
  402. } else {
  403. dpll = p->dpll;
  404. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  405. fp = p->fp0;
  406. else
  407. fp = p->fp1;
  408. is_lvds = (pipe == 1) && (dev_priv->regs.psb.saveLVDS &
  409. LVDS_PORT_EN);
  410. }
  411. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  412. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  413. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  414. if (is_lvds) {
  415. clock.p1 =
  416. ffs((dpll &
  417. DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  418. DPLL_FPA01_P1_POST_DIV_SHIFT);
  419. clock.p2 = 14;
  420. if ((dpll & PLL_REF_INPUT_MASK) ==
  421. PLLB_REF_INPUT_SPREADSPECTRUMIN) {
  422. /* XXX: might not be 66MHz */
  423. psb_intel_clock(66000, &clock);
  424. } else
  425. psb_intel_clock(48000, &clock);
  426. } else {
  427. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  428. clock.p1 = 2;
  429. else {
  430. clock.p1 =
  431. ((dpll &
  432. DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  433. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  434. }
  435. if (dpll & PLL_P2_DIVIDE_BY_4)
  436. clock.p2 = 4;
  437. else
  438. clock.p2 = 2;
  439. psb_intel_clock(48000, &clock);
  440. }
  441. /* XXX: It would be nice to validate the clocks, but we can't reuse
  442. * i830PllIsValid() because it relies on the xf86_config connector
  443. * configuration being accurate, which it isn't necessarily.
  444. */
  445. return clock.dot;
  446. }
  447. /** Returns the currently programmed mode of the given pipe. */
  448. struct drm_display_mode *psb_intel_crtc_mode_get(struct drm_device *dev,
  449. struct drm_crtc *crtc)
  450. {
  451. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  452. int pipe = psb_intel_crtc->pipe;
  453. struct drm_display_mode *mode;
  454. int htot;
  455. int hsync;
  456. int vtot;
  457. int vsync;
  458. struct drm_psb_private *dev_priv = dev->dev_private;
  459. struct psb_pipe *p = &dev_priv->regs.pipe[pipe];
  460. const struct psb_offset *map = &dev_priv->regmap[pipe];
  461. if (gma_power_begin(dev, false)) {
  462. htot = REG_READ(map->htotal);
  463. hsync = REG_READ(map->hsync);
  464. vtot = REG_READ(map->vtotal);
  465. vsync = REG_READ(map->vsync);
  466. gma_power_end(dev);
  467. } else {
  468. htot = p->htotal;
  469. hsync = p->hsync;
  470. vtot = p->vtotal;
  471. vsync = p->vsync;
  472. }
  473. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  474. if (!mode)
  475. return NULL;
  476. mode->clock = psb_intel_crtc_clock_get(dev, crtc);
  477. mode->hdisplay = (htot & 0xffff) + 1;
  478. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  479. mode->hsync_start = (hsync & 0xffff) + 1;
  480. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  481. mode->vdisplay = (vtot & 0xffff) + 1;
  482. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  483. mode->vsync_start = (vsync & 0xffff) + 1;
  484. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  485. drm_mode_set_name(mode);
  486. drm_mode_set_crtcinfo(mode, 0);
  487. return mode;
  488. }
  489. const struct drm_crtc_helper_funcs psb_intel_helper_funcs = {
  490. .dpms = gma_crtc_dpms,
  491. .mode_fixup = gma_crtc_mode_fixup,
  492. .mode_set = psb_intel_crtc_mode_set,
  493. .mode_set_base = gma_pipe_set_base,
  494. .prepare = gma_crtc_prepare,
  495. .commit = gma_crtc_commit,
  496. .disable = gma_crtc_disable,
  497. };
  498. const struct drm_crtc_funcs psb_intel_crtc_funcs = {
  499. .save = psb_intel_crtc_save,
  500. .restore = psb_intel_crtc_restore,
  501. .cursor_set = gma_crtc_cursor_set,
  502. .cursor_move = gma_crtc_cursor_move,
  503. .gamma_set = gma_crtc_gamma_set,
  504. .set_config = psb_crtc_set_config,
  505. .destroy = gma_crtc_destroy,
  506. };
  507. const struct gma_clock_funcs psb_clock_funcs = {
  508. .clock = psb_intel_clock,
  509. .limit = psb_intel_limit,
  510. .pll_is_valid = gma_pll_is_valid,
  511. };
  512. /*
  513. * Set the default value of cursor control and base register
  514. * to zero. This is a workaround for h/w defect on Oaktrail
  515. */
  516. static void psb_intel_cursor_init(struct drm_device *dev,
  517. struct psb_intel_crtc *psb_intel_crtc)
  518. {
  519. struct drm_psb_private *dev_priv = dev->dev_private;
  520. u32 control[3] = { CURACNTR, CURBCNTR, CURCCNTR };
  521. u32 base[3] = { CURABASE, CURBBASE, CURCBASE };
  522. struct gtt_range *cursor_gt;
  523. if (dev_priv->ops->cursor_needs_phys) {
  524. /* Allocate 4 pages of stolen mem for a hardware cursor. That
  525. * is enough for the 64 x 64 ARGB cursors we support.
  526. */
  527. cursor_gt = psb_gtt_alloc_range(dev, 4 * PAGE_SIZE, "cursor", 1);
  528. if (!cursor_gt) {
  529. psb_intel_crtc->cursor_gt = NULL;
  530. goto out;
  531. }
  532. psb_intel_crtc->cursor_gt = cursor_gt;
  533. psb_intel_crtc->cursor_addr = dev_priv->stolen_base +
  534. cursor_gt->offset;
  535. } else {
  536. psb_intel_crtc->cursor_gt = NULL;
  537. }
  538. out:
  539. REG_WRITE(control[psb_intel_crtc->pipe], 0);
  540. REG_WRITE(base[psb_intel_crtc->pipe], 0);
  541. }
  542. void psb_intel_crtc_init(struct drm_device *dev, int pipe,
  543. struct psb_intel_mode_device *mode_dev)
  544. {
  545. struct drm_psb_private *dev_priv = dev->dev_private;
  546. struct psb_intel_crtc *psb_intel_crtc;
  547. int i;
  548. uint16_t *r_base, *g_base, *b_base;
  549. /* We allocate a extra array of drm_connector pointers
  550. * for fbdev after the crtc */
  551. psb_intel_crtc =
  552. kzalloc(sizeof(struct psb_intel_crtc) +
  553. (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)),
  554. GFP_KERNEL);
  555. if (psb_intel_crtc == NULL)
  556. return;
  557. psb_intel_crtc->crtc_state =
  558. kzalloc(sizeof(struct psb_intel_crtc_state), GFP_KERNEL);
  559. if (!psb_intel_crtc->crtc_state) {
  560. dev_err(dev->dev, "Crtc state error: No memory\n");
  561. kfree(psb_intel_crtc);
  562. return;
  563. }
  564. /* Set the CRTC operations from the chip specific data */
  565. drm_crtc_init(dev, &psb_intel_crtc->base, dev_priv->ops->crtc_funcs);
  566. /* Set the CRTC clock functions from chip specific data */
  567. psb_intel_crtc->clock_funcs = dev_priv->ops->clock_funcs;
  568. drm_mode_crtc_set_gamma_size(&psb_intel_crtc->base, 256);
  569. psb_intel_crtc->pipe = pipe;
  570. psb_intel_crtc->plane = pipe;
  571. r_base = psb_intel_crtc->base.gamma_store;
  572. g_base = r_base + 256;
  573. b_base = g_base + 256;
  574. for (i = 0; i < 256; i++) {
  575. psb_intel_crtc->lut_r[i] = i;
  576. psb_intel_crtc->lut_g[i] = i;
  577. psb_intel_crtc->lut_b[i] = i;
  578. r_base[i] = i << 8;
  579. g_base[i] = i << 8;
  580. b_base[i] = i << 8;
  581. psb_intel_crtc->lut_adj[i] = 0;
  582. }
  583. psb_intel_crtc->mode_dev = mode_dev;
  584. psb_intel_crtc->cursor_addr = 0;
  585. drm_crtc_helper_add(&psb_intel_crtc->base,
  586. dev_priv->ops->crtc_helper);
  587. /* Setup the array of drm_connector pointer array */
  588. psb_intel_crtc->mode_set.crtc = &psb_intel_crtc->base;
  589. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  590. dev_priv->plane_to_crtc_mapping[psb_intel_crtc->plane] != NULL);
  591. dev_priv->plane_to_crtc_mapping[psb_intel_crtc->plane] =
  592. &psb_intel_crtc->base;
  593. dev_priv->pipe_to_crtc_mapping[psb_intel_crtc->pipe] =
  594. &psb_intel_crtc->base;
  595. psb_intel_crtc->mode_set.connectors =
  596. (struct drm_connector **) (psb_intel_crtc + 1);
  597. psb_intel_crtc->mode_set.num_connectors = 0;
  598. psb_intel_cursor_init(dev, psb_intel_crtc);
  599. /* Set to true so that the pipe is forced off on initial config. */
  600. psb_intel_crtc->active = true;
  601. }
  602. int psb_intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  603. struct drm_file *file_priv)
  604. {
  605. struct drm_psb_private *dev_priv = dev->dev_private;
  606. struct drm_psb_get_pipe_from_crtc_id_arg *pipe_from_crtc_id = data;
  607. struct drm_mode_object *drmmode_obj;
  608. struct psb_intel_crtc *crtc;
  609. if (!dev_priv) {
  610. dev_err(dev->dev, "called with no initialization\n");
  611. return -EINVAL;
  612. }
  613. drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
  614. DRM_MODE_OBJECT_CRTC);
  615. if (!drmmode_obj) {
  616. dev_err(dev->dev, "no such CRTC id\n");
  617. return -EINVAL;
  618. }
  619. crtc = to_psb_intel_crtc(obj_to_crtc(drmmode_obj));
  620. pipe_from_crtc_id->pipe = crtc->pipe;
  621. return 0;
  622. }
  623. struct drm_crtc *psb_intel_get_crtc_from_pipe(struct drm_device *dev, int pipe)
  624. {
  625. struct drm_crtc *crtc = NULL;
  626. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  627. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  628. if (psb_intel_crtc->pipe == pipe)
  629. break;
  630. }
  631. return crtc;
  632. }
  633. int psb_intel_connector_clones(struct drm_device *dev, int type_mask)
  634. {
  635. int index_mask = 0;
  636. struct drm_connector *connector;
  637. int entry = 0;
  638. list_for_each_entry(connector, &dev->mode_config.connector_list,
  639. head) {
  640. struct psb_intel_encoder *psb_intel_encoder =
  641. psb_intel_attached_encoder(connector);
  642. if (type_mask & (1 << psb_intel_encoder->type))
  643. index_mask |= (1 << entry);
  644. entry++;
  645. }
  646. return index_mask;
  647. }
  648. /* current intel driver doesn't take advantage of encoders
  649. always give back the encoder for the connector
  650. */
  651. struct drm_encoder *psb_intel_best_encoder(struct drm_connector *connector)
  652. {
  653. struct psb_intel_encoder *psb_intel_encoder =
  654. psb_intel_attached_encoder(connector);
  655. return &psb_intel_encoder->base;
  656. }
  657. void psb_intel_connector_attach_encoder(struct psb_intel_connector *connector,
  658. struct psb_intel_encoder *encoder)
  659. {
  660. connector->encoder = encoder;
  661. drm_mode_connector_attach_encoder(&connector->base,
  662. &encoder->base);
  663. }