psb_intel_display.c 15 KB

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