psb_intel_display.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright © 2006-2011 Intel Corporation
  4. *
  5. * Authors:
  6. * Eric Anholt <eric@anholt.net>
  7. */
  8. #include <linux/delay.h>
  9. #include <linux/i2c.h>
  10. #include <drm/drm_modeset_helper.h>
  11. #include <drm/drm_modeset_helper_vtables.h>
  12. #include "framebuffer.h"
  13. #include "gem.h"
  14. #include "gma_display.h"
  15. #include "power.h"
  16. #include "psb_drv.h"
  17. #include "psb_intel_drv.h"
  18. #include "psb_intel_reg.h"
  19. #define INTEL_LIMIT_I9XX_SDVO_DAC 0
  20. #define INTEL_LIMIT_I9XX_LVDS 1
  21. static const struct gma_limit_t psb_intel_limits[] = {
  22. { /* INTEL_LIMIT_I9XX_SDVO_DAC */
  23. .dot = {.min = 20000, .max = 400000},
  24. .vco = {.min = 1400000, .max = 2800000},
  25. .n = {.min = 1, .max = 6},
  26. .m = {.min = 70, .max = 120},
  27. .m1 = {.min = 8, .max = 18},
  28. .m2 = {.min = 3, .max = 7},
  29. .p = {.min = 5, .max = 80},
  30. .p1 = {.min = 1, .max = 8},
  31. .p2 = {.dot_limit = 200000, .p2_slow = 10, .p2_fast = 5},
  32. .find_pll = gma_find_best_pll,
  33. },
  34. { /* INTEL_LIMIT_I9XX_LVDS */
  35. .dot = {.min = 20000, .max = 400000},
  36. .vco = {.min = 1400000, .max = 2800000},
  37. .n = {.min = 1, .max = 6},
  38. .m = {.min = 70, .max = 120},
  39. .m1 = {.min = 8, .max = 18},
  40. .m2 = {.min = 3, .max = 7},
  41. .p = {.min = 7, .max = 98},
  42. .p1 = {.min = 1, .max = 8},
  43. /* The single-channel range is 25-112Mhz, and dual-channel
  44. * is 80-224Mhz. Prefer single channel as much as possible.
  45. */
  46. .p2 = {.dot_limit = 112000, .p2_slow = 14, .p2_fast = 7},
  47. .find_pll = gma_find_best_pll,
  48. },
  49. };
  50. static const struct gma_limit_t *psb_intel_limit(struct drm_crtc *crtc,
  51. int refclk)
  52. {
  53. const struct gma_limit_t *limit;
  54. if (gma_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  55. limit = &psb_intel_limits[INTEL_LIMIT_I9XX_LVDS];
  56. else
  57. limit = &psb_intel_limits[INTEL_LIMIT_I9XX_SDVO_DAC];
  58. return limit;
  59. }
  60. static void psb_intel_clock(int refclk, struct gma_clock_t *clock)
  61. {
  62. clock->m = 5 * (clock->m1 + 2) + (clock->m2 + 2);
  63. clock->p = clock->p1 * clock->p2;
  64. clock->vco = refclk * clock->m / (clock->n + 2);
  65. clock->dot = clock->vco / clock->p;
  66. }
  67. /*
  68. * Return the pipe currently connected to the panel fitter,
  69. * or -1 if the panel fitter is not present or not in use
  70. */
  71. static int psb_intel_panel_fitter_pipe(struct drm_device *dev)
  72. {
  73. u32 pfit_control;
  74. pfit_control = REG_READ(PFIT_CONTROL);
  75. /* See if the panel fitter is in use */
  76. if ((pfit_control & PFIT_ENABLE) == 0)
  77. return -1;
  78. /* Must be on PIPE 1 for PSB */
  79. return 1;
  80. }
  81. static int psb_intel_crtc_mode_set(struct drm_crtc *crtc,
  82. struct drm_display_mode *mode,
  83. struct drm_display_mode *adjusted_mode,
  84. int x, int y,
  85. struct drm_framebuffer *old_fb)
  86. {
  87. struct drm_device *dev = crtc->dev;
  88. struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
  89. struct gma_crtc *gma_crtc = to_gma_crtc(crtc);
  90. const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  91. int pipe = gma_crtc->pipe;
  92. const struct psb_offset *map = &dev_priv->regmap[pipe];
  93. int refclk;
  94. struct gma_clock_t clock;
  95. u32 dpll = 0, fp = 0, dspcntr, pipeconf;
  96. bool ok, is_sdvo = false;
  97. bool is_lvds = false, is_tv = false;
  98. struct drm_connector_list_iter conn_iter;
  99. struct drm_connector *connector;
  100. const struct gma_limit_t *limit;
  101. /* No scan out no play */
  102. if (crtc->primary->fb == NULL) {
  103. crtc_funcs->mode_set_base(crtc, x, y, old_fb);
  104. return 0;
  105. }
  106. drm_connector_list_iter_begin(dev, &conn_iter);
  107. drm_for_each_connector_iter(connector, &conn_iter) {
  108. struct gma_encoder *gma_encoder = gma_attached_encoder(connector);
  109. if (!connector->encoder
  110. || connector->encoder->crtc != crtc)
  111. continue;
  112. switch (gma_encoder->type) {
  113. case INTEL_OUTPUT_LVDS:
  114. is_lvds = true;
  115. break;
  116. case INTEL_OUTPUT_SDVO:
  117. is_sdvo = true;
  118. break;
  119. case INTEL_OUTPUT_TVOUT:
  120. is_tv = true;
  121. break;
  122. }
  123. break;
  124. }
  125. drm_connector_list_iter_end(&conn_iter);
  126. refclk = 96000;
  127. limit = gma_crtc->clock_funcs->limit(crtc, refclk);
  128. ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk,
  129. &clock);
  130. if (!ok) {
  131. DRM_ERROR("Couldn't find PLL settings for mode! target: %d, actual: %d",
  132. adjusted_mode->clock, clock.dot);
  133. return 0;
  134. }
  135. fp = clock.n << 16 | clock.m1 << 8 | clock.m2;
  136. dpll = DPLL_VGA_MODE_DIS;
  137. if (is_lvds) {
  138. dpll |= DPLLB_MODE_LVDS;
  139. dpll |= DPLL_DVO_HIGH_SPEED;
  140. } else
  141. dpll |= DPLLB_MODE_DAC_SERIAL;
  142. if (is_sdvo) {
  143. int sdvo_pixel_multiply =
  144. adjusted_mode->clock / mode->clock;
  145. dpll |= DPLL_DVO_HIGH_SPEED;
  146. dpll |=
  147. (sdvo_pixel_multiply - 1) << SDVO_MULTIPLIER_SHIFT_HIRES;
  148. }
  149. /* compute bitmask from p1 value */
  150. dpll |= (1 << (clock.p1 - 1)) << 16;
  151. switch (clock.p2) {
  152. case 5:
  153. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  154. break;
  155. case 7:
  156. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  157. break;
  158. case 10:
  159. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  160. break;
  161. case 14:
  162. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  163. break;
  164. }
  165. if (is_tv) {
  166. /* XXX: just matching BIOS for now */
  167. /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
  168. dpll |= 3;
  169. }
  170. dpll |= PLL_REF_INPUT_DREFCLK;
  171. /* setup pipeconf */
  172. pipeconf = REG_READ(map->conf);
  173. /* Set up the display plane register */
  174. dspcntr = DISPPLANE_GAMMA_ENABLE;
  175. if (pipe == 0)
  176. dspcntr |= DISPPLANE_SEL_PIPE_A;
  177. else
  178. dspcntr |= DISPPLANE_SEL_PIPE_B;
  179. dspcntr |= DISPLAY_PLANE_ENABLE;
  180. pipeconf |= PIPEACONF_ENABLE;
  181. dpll |= DPLL_VCO_ENABLE;
  182. /* Disable the panel fitter if it was on our pipe */
  183. if (psb_intel_panel_fitter_pipe(dev) == pipe)
  184. REG_WRITE(PFIT_CONTROL, 0);
  185. drm_mode_debug_printmodeline(mode);
  186. if (dpll & DPLL_VCO_ENABLE) {
  187. REG_WRITE(map->fp0, fp);
  188. REG_WRITE(map->dpll, dpll & ~DPLL_VCO_ENABLE);
  189. REG_READ(map->dpll);
  190. udelay(150);
  191. }
  192. /* The LVDS pin pair needs to be on before the DPLLs are enabled.
  193. * This is an exception to the general rule that mode_set doesn't turn
  194. * things on.
  195. */
  196. if (is_lvds) {
  197. u32 lvds = REG_READ(LVDS);
  198. lvds &= ~LVDS_PIPEB_SELECT;
  199. if (pipe == 1)
  200. lvds |= LVDS_PIPEB_SELECT;
  201. lvds |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP;
  202. /* Set the B0-B3 data pairs corresponding to
  203. * whether we're going to
  204. * set the DPLLs for dual-channel mode or not.
  205. */
  206. lvds &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
  207. if (clock.p2 == 7)
  208. lvds |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
  209. /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
  210. * appropriately here, but we need to look more
  211. * thoroughly into how panels behave in the two modes.
  212. */
  213. REG_WRITE(LVDS, lvds);
  214. REG_READ(LVDS);
  215. }
  216. REG_WRITE(map->fp0, fp);
  217. REG_WRITE(map->dpll, dpll);
  218. REG_READ(map->dpll);
  219. /* Wait for the clocks to stabilize. */
  220. udelay(150);
  221. /* write it again -- the BIOS does, after all */
  222. REG_WRITE(map->dpll, dpll);
  223. REG_READ(map->dpll);
  224. /* Wait for the clocks to stabilize. */
  225. udelay(150);
  226. REG_WRITE(map->htotal, (adjusted_mode->crtc_hdisplay - 1) |
  227. ((adjusted_mode->crtc_htotal - 1) << 16));
  228. REG_WRITE(map->hblank, (adjusted_mode->crtc_hblank_start - 1) |
  229. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  230. REG_WRITE(map->hsync, (adjusted_mode->crtc_hsync_start - 1) |
  231. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  232. REG_WRITE(map->vtotal, (adjusted_mode->crtc_vdisplay - 1) |
  233. ((adjusted_mode->crtc_vtotal - 1) << 16));
  234. REG_WRITE(map->vblank, (adjusted_mode->crtc_vblank_start - 1) |
  235. ((adjusted_mode->crtc_vblank_end - 1) << 16));
  236. REG_WRITE(map->vsync, (adjusted_mode->crtc_vsync_start - 1) |
  237. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  238. /* pipesrc and dspsize control the size that is scaled from,
  239. * which should always be the user's requested size.
  240. */
  241. REG_WRITE(map->size,
  242. ((mode->vdisplay - 1) << 16) | (mode->hdisplay - 1));
  243. REG_WRITE(map->pos, 0);
  244. REG_WRITE(map->src,
  245. ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
  246. REG_WRITE(map->conf, pipeconf);
  247. REG_READ(map->conf);
  248. gma_wait_for_vblank(dev);
  249. REG_WRITE(map->cntr, dspcntr);
  250. /* Flush the plane changes */
  251. crtc_funcs->mode_set_base(crtc, x, y, old_fb);
  252. gma_wait_for_vblank(dev);
  253. return 0;
  254. }
  255. /* Returns the clock of the currently programmed mode of the given pipe. */
  256. static int psb_intel_crtc_clock_get(struct drm_device *dev,
  257. struct drm_crtc *crtc)
  258. {
  259. struct gma_crtc *gma_crtc = to_gma_crtc(crtc);
  260. struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
  261. int pipe = gma_crtc->pipe;
  262. const struct psb_offset *map = &dev_priv->regmap[pipe];
  263. u32 dpll;
  264. u32 fp;
  265. struct gma_clock_t clock;
  266. bool is_lvds;
  267. struct psb_pipe *p = &dev_priv->regs.pipe[pipe];
  268. if (gma_power_begin(dev, false)) {
  269. dpll = REG_READ(map->dpll);
  270. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  271. fp = REG_READ(map->fp0);
  272. else
  273. fp = REG_READ(map->fp1);
  274. is_lvds = (pipe == 1) && (REG_READ(LVDS) & LVDS_PORT_EN);
  275. gma_power_end(dev);
  276. } else {
  277. dpll = p->dpll;
  278. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  279. fp = p->fp0;
  280. else
  281. fp = p->fp1;
  282. is_lvds = (pipe == 1) && (dev_priv->regs.psb.saveLVDS &
  283. LVDS_PORT_EN);
  284. }
  285. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  286. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  287. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  288. if (is_lvds) {
  289. clock.p1 =
  290. ffs((dpll &
  291. DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  292. DPLL_FPA01_P1_POST_DIV_SHIFT);
  293. clock.p2 = 14;
  294. if ((dpll & PLL_REF_INPUT_MASK) ==
  295. PLLB_REF_INPUT_SPREADSPECTRUMIN) {
  296. /* XXX: might not be 66MHz */
  297. psb_intel_clock(66000, &clock);
  298. } else
  299. psb_intel_clock(48000, &clock);
  300. } else {
  301. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  302. clock.p1 = 2;
  303. else {
  304. clock.p1 =
  305. ((dpll &
  306. DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  307. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  308. }
  309. if (dpll & PLL_P2_DIVIDE_BY_4)
  310. clock.p2 = 4;
  311. else
  312. clock.p2 = 2;
  313. psb_intel_clock(48000, &clock);
  314. }
  315. /* XXX: It would be nice to validate the clocks, but we can't reuse
  316. * i830PllIsValid() because it relies on the xf86_config connector
  317. * configuration being accurate, which it isn't necessarily.
  318. */
  319. return clock.dot;
  320. }
  321. /** Returns the currently programmed mode of the given pipe. */
  322. struct drm_display_mode *psb_intel_crtc_mode_get(struct drm_device *dev,
  323. struct drm_crtc *crtc)
  324. {
  325. struct gma_crtc *gma_crtc = to_gma_crtc(crtc);
  326. int pipe = gma_crtc->pipe;
  327. struct drm_display_mode *mode;
  328. int htot;
  329. int hsync;
  330. int vtot;
  331. int vsync;
  332. struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
  333. struct psb_pipe *p = &dev_priv->regs.pipe[pipe];
  334. const struct psb_offset *map = &dev_priv->regmap[pipe];
  335. if (gma_power_begin(dev, false)) {
  336. htot = REG_READ(map->htotal);
  337. hsync = REG_READ(map->hsync);
  338. vtot = REG_READ(map->vtotal);
  339. vsync = REG_READ(map->vsync);
  340. gma_power_end(dev);
  341. } else {
  342. htot = p->htotal;
  343. hsync = p->hsync;
  344. vtot = p->vtotal;
  345. vsync = p->vsync;
  346. }
  347. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  348. if (!mode)
  349. return NULL;
  350. mode->clock = psb_intel_crtc_clock_get(dev, crtc);
  351. mode->hdisplay = (htot & 0xffff) + 1;
  352. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  353. mode->hsync_start = (hsync & 0xffff) + 1;
  354. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  355. mode->vdisplay = (vtot & 0xffff) + 1;
  356. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  357. mode->vsync_start = (vsync & 0xffff) + 1;
  358. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  359. drm_mode_set_name(mode);
  360. drm_mode_set_crtcinfo(mode, 0);
  361. return mode;
  362. }
  363. const struct drm_crtc_helper_funcs psb_intel_helper_funcs = {
  364. .dpms = gma_crtc_dpms,
  365. .mode_set = psb_intel_crtc_mode_set,
  366. .mode_set_base = gma_pipe_set_base,
  367. .prepare = gma_crtc_prepare,
  368. .commit = gma_crtc_commit,
  369. .disable = gma_crtc_disable,
  370. };
  371. const struct gma_clock_funcs psb_clock_funcs = {
  372. .clock = psb_intel_clock,
  373. .limit = psb_intel_limit,
  374. .pll_is_valid = gma_pll_is_valid,
  375. };
  376. /*
  377. * Set the default value of cursor control and base register
  378. * to zero. This is a workaround for h/w defect on Oaktrail
  379. */
  380. static void psb_intel_cursor_init(struct drm_device *dev,
  381. struct gma_crtc *gma_crtc)
  382. {
  383. struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
  384. u32 control[3] = { CURACNTR, CURBCNTR, CURCCNTR };
  385. u32 base[3] = { CURABASE, CURBBASE, CURCBASE };
  386. struct psb_gem_object *cursor_pobj;
  387. if (dev_priv->ops->cursor_needs_phys) {
  388. /* Allocate 4 pages of stolen mem for a hardware cursor. That
  389. * is enough for the 64 x 64 ARGB cursors we support.
  390. */
  391. cursor_pobj = psb_gem_create(dev, 4 * PAGE_SIZE, "cursor", true, PAGE_SIZE);
  392. if (IS_ERR(cursor_pobj)) {
  393. gma_crtc->cursor_pobj = NULL;
  394. goto out;
  395. }
  396. gma_crtc->cursor_pobj = cursor_pobj;
  397. gma_crtc->cursor_addr = dev_priv->stolen_base + cursor_pobj->offset;
  398. } else {
  399. gma_crtc->cursor_pobj = NULL;
  400. }
  401. out:
  402. REG_WRITE(control[gma_crtc->pipe], 0);
  403. REG_WRITE(base[gma_crtc->pipe], 0);
  404. }
  405. void psb_intel_crtc_init(struct drm_device *dev, int pipe,
  406. struct psb_intel_mode_device *mode_dev)
  407. {
  408. struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
  409. struct gma_crtc *gma_crtc;
  410. int i;
  411. /* We allocate a extra array of drm_connector pointers
  412. * for fbdev after the crtc */
  413. gma_crtc = kzalloc(sizeof(struct gma_crtc) +
  414. (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)),
  415. GFP_KERNEL);
  416. if (gma_crtc == NULL)
  417. return;
  418. gma_crtc->crtc_state =
  419. kzalloc(sizeof(struct psb_intel_crtc_state), GFP_KERNEL);
  420. if (!gma_crtc->crtc_state) {
  421. dev_err(dev->dev, "Crtc state error: No memory\n");
  422. kfree(gma_crtc);
  423. return;
  424. }
  425. drm_crtc_init(dev, &gma_crtc->base, &gma_crtc_funcs);
  426. /* Set the CRTC clock functions from chip specific data */
  427. gma_crtc->clock_funcs = dev_priv->ops->clock_funcs;
  428. drm_mode_crtc_set_gamma_size(&gma_crtc->base, 256);
  429. gma_crtc->pipe = pipe;
  430. gma_crtc->plane = pipe;
  431. for (i = 0; i < 256; i++)
  432. gma_crtc->lut_adj[i] = 0;
  433. gma_crtc->mode_dev = mode_dev;
  434. gma_crtc->cursor_addr = 0;
  435. drm_crtc_helper_add(&gma_crtc->base,
  436. dev_priv->ops->crtc_helper);
  437. /* Setup the array of drm_connector pointer array */
  438. gma_crtc->mode_set.crtc = &gma_crtc->base;
  439. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  440. dev_priv->plane_to_crtc_mapping[gma_crtc->plane] != NULL);
  441. dev_priv->plane_to_crtc_mapping[gma_crtc->plane] = &gma_crtc->base;
  442. dev_priv->pipe_to_crtc_mapping[gma_crtc->pipe] = &gma_crtc->base;
  443. gma_crtc->mode_set.connectors = (struct drm_connector **)(gma_crtc + 1);
  444. gma_crtc->mode_set.num_connectors = 0;
  445. psb_intel_cursor_init(dev, gma_crtc);
  446. /* Set to true so that the pipe is forced off on initial config. */
  447. gma_crtc->active = true;
  448. }
  449. struct drm_crtc *psb_intel_get_crtc_from_pipe(struct drm_device *dev, int pipe)
  450. {
  451. struct drm_crtc *crtc;
  452. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  453. struct gma_crtc *gma_crtc = to_gma_crtc(crtc);
  454. if (gma_crtc->pipe == pipe)
  455. return crtc;
  456. }
  457. return NULL;
  458. }
  459. int gma_connector_clones(struct drm_device *dev, int type_mask)
  460. {
  461. struct drm_connector_list_iter conn_iter;
  462. struct drm_connector *connector;
  463. int index_mask = 0;
  464. int entry = 0;
  465. drm_connector_list_iter_begin(dev, &conn_iter);
  466. drm_for_each_connector_iter(connector, &conn_iter) {
  467. struct gma_encoder *gma_encoder = gma_attached_encoder(connector);
  468. if (type_mask & (1 << gma_encoder->type))
  469. index_mask |= (1 << entry);
  470. entry++;
  471. }
  472. drm_connector_list_iter_end(&conn_iter);
  473. return index_mask;
  474. }