gma_display.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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. * Patrik Jakobsson <patrik.r.jakobsson@gmail.com>
  20. */
  21. #include <drm/drmP.h>
  22. #include "gma_display.h"
  23. #include "psb_intel_drv.h"
  24. #include "psb_intel_reg.h"
  25. #include "psb_drv.h"
  26. #include "framebuffer.h"
  27. /**
  28. * Returns whether any output on the specified pipe is of the specified type
  29. */
  30. bool gma_pipe_has_type(struct drm_crtc *crtc, int type)
  31. {
  32. struct drm_device *dev = crtc->dev;
  33. struct drm_mode_config *mode_config = &dev->mode_config;
  34. struct drm_connector *l_entry;
  35. list_for_each_entry(l_entry, &mode_config->connector_list, head) {
  36. if (l_entry->encoder && l_entry->encoder->crtc == crtc) {
  37. struct psb_intel_encoder *psb_intel_encoder =
  38. psb_intel_attached_encoder(l_entry);
  39. if (psb_intel_encoder->type == type)
  40. return true;
  41. }
  42. }
  43. return false;
  44. }
  45. void gma_wait_for_vblank(struct drm_device *dev)
  46. {
  47. /* Wait for 20ms, i.e. one cycle at 50hz. */
  48. mdelay(20);
  49. }
  50. int gma_pipe_set_base(struct drm_crtc *crtc, int x, int y,
  51. struct drm_framebuffer *old_fb)
  52. {
  53. struct drm_device *dev = crtc->dev;
  54. struct drm_psb_private *dev_priv = dev->dev_private;
  55. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  56. struct psb_framebuffer *psbfb = to_psb_fb(crtc->fb);
  57. int pipe = psb_intel_crtc->pipe;
  58. const struct psb_offset *map = &dev_priv->regmap[pipe];
  59. unsigned long start, offset;
  60. u32 dspcntr;
  61. int ret = 0;
  62. if (!gma_power_begin(dev, true))
  63. return 0;
  64. /* no fb bound */
  65. if (!crtc->fb) {
  66. dev_err(dev->dev, "No FB bound\n");
  67. goto gma_pipe_cleaner;
  68. }
  69. /* We are displaying this buffer, make sure it is actually loaded
  70. into the GTT */
  71. ret = psb_gtt_pin(psbfb->gtt);
  72. if (ret < 0)
  73. goto gma_pipe_set_base_exit;
  74. start = psbfb->gtt->offset;
  75. offset = y * crtc->fb->pitches[0] + x * (crtc->fb->bits_per_pixel / 8);
  76. REG_WRITE(map->stride, crtc->fb->pitches[0]);
  77. dspcntr = REG_READ(map->cntr);
  78. dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
  79. switch (crtc->fb->bits_per_pixel) {
  80. case 8:
  81. dspcntr |= DISPPLANE_8BPP;
  82. break;
  83. case 16:
  84. if (crtc->fb->depth == 15)
  85. dspcntr |= DISPPLANE_15_16BPP;
  86. else
  87. dspcntr |= DISPPLANE_16BPP;
  88. break;
  89. case 24:
  90. case 32:
  91. dspcntr |= DISPPLANE_32BPP_NO_ALPHA;
  92. break;
  93. default:
  94. dev_err(dev->dev, "Unknown color depth\n");
  95. ret = -EINVAL;
  96. goto gma_pipe_set_base_exit;
  97. }
  98. REG_WRITE(map->cntr, dspcntr);
  99. dev_dbg(dev->dev,
  100. "Writing base %08lX %08lX %d %d\n", start, offset, x, y);
  101. /* FIXME: Investigate whether this really is the base for psb and why
  102. the linear offset is named base for the other chips. map->surf
  103. should be the base and map->linoff the offset for all chips */
  104. if (IS_PSB(dev)) {
  105. REG_WRITE(map->base, offset + start);
  106. REG_READ(map->base);
  107. } else {
  108. REG_WRITE(map->base, offset);
  109. REG_READ(map->base);
  110. REG_WRITE(map->surf, start);
  111. REG_READ(map->surf);
  112. }
  113. gma_pipe_cleaner:
  114. /* If there was a previous display we can now unpin it */
  115. if (old_fb)
  116. psb_gtt_unpin(to_psb_fb(old_fb)->gtt);
  117. gma_pipe_set_base_exit:
  118. gma_power_end(dev);
  119. return ret;
  120. }
  121. /* Loads the palette/gamma unit for the CRTC with the prepared values */
  122. void gma_crtc_load_lut(struct drm_crtc *crtc)
  123. {
  124. struct drm_device *dev = crtc->dev;
  125. struct drm_psb_private *dev_priv = dev->dev_private;
  126. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  127. const struct psb_offset *map = &dev_priv->regmap[psb_intel_crtc->pipe];
  128. int palreg = map->palette;
  129. int i;
  130. /* The clocks have to be on to load the palette. */
  131. if (!crtc->enabled)
  132. return;
  133. if (gma_power_begin(dev, false)) {
  134. for (i = 0; i < 256; i++) {
  135. REG_WRITE(palreg + 4 * i,
  136. ((psb_intel_crtc->lut_r[i] +
  137. psb_intel_crtc->lut_adj[i]) << 16) |
  138. ((psb_intel_crtc->lut_g[i] +
  139. psb_intel_crtc->lut_adj[i]) << 8) |
  140. (psb_intel_crtc->lut_b[i] +
  141. psb_intel_crtc->lut_adj[i]));
  142. }
  143. gma_power_end(dev);
  144. } else {
  145. for (i = 0; i < 256; i++) {
  146. /* FIXME: Why pipe[0] and not pipe[..._crtc->pipe]? */
  147. dev_priv->regs.pipe[0].palette[i] =
  148. ((psb_intel_crtc->lut_r[i] +
  149. psb_intel_crtc->lut_adj[i]) << 16) |
  150. ((psb_intel_crtc->lut_g[i] +
  151. psb_intel_crtc->lut_adj[i]) << 8) |
  152. (psb_intel_crtc->lut_b[i] +
  153. psb_intel_crtc->lut_adj[i]);
  154. }
  155. }
  156. }
  157. void gma_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green, u16 *blue,
  158. u32 start, u32 size)
  159. {
  160. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  161. int i;
  162. int end = (start + size > 256) ? 256 : start + size;
  163. for (i = start; i < end; i++) {
  164. psb_intel_crtc->lut_r[i] = red[i] >> 8;
  165. psb_intel_crtc->lut_g[i] = green[i] >> 8;
  166. psb_intel_crtc->lut_b[i] = blue[i] >> 8;
  167. }
  168. gma_crtc_load_lut(crtc);
  169. }
  170. /**
  171. * Sets the power management mode of the pipe and plane.
  172. *
  173. * This code should probably grow support for turning the cursor off and back
  174. * on appropriately at the same time as we're turning the pipe off/on.
  175. */
  176. void gma_crtc_dpms(struct drm_crtc *crtc, int mode)
  177. {
  178. struct drm_device *dev = crtc->dev;
  179. struct drm_psb_private *dev_priv = dev->dev_private;
  180. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  181. int pipe = psb_intel_crtc->pipe;
  182. const struct psb_offset *map = &dev_priv->regmap[pipe];
  183. u32 temp;
  184. /* XXX: When our outputs are all unaware of DPMS modes other than off
  185. * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
  186. */
  187. /* FIXME: Uncomment this when we move cdv to generic dpms
  188. if (IS_CDV(dev))
  189. cdv_intel_disable_self_refresh(dev);
  190. */
  191. switch (mode) {
  192. case DRM_MODE_DPMS_ON:
  193. case DRM_MODE_DPMS_STANDBY:
  194. case DRM_MODE_DPMS_SUSPEND:
  195. if (psb_intel_crtc->active)
  196. break;
  197. psb_intel_crtc->active = true;
  198. /* Enable the DPLL */
  199. temp = REG_READ(map->dpll);
  200. if ((temp & DPLL_VCO_ENABLE) == 0) {
  201. REG_WRITE(map->dpll, temp);
  202. REG_READ(map->dpll);
  203. /* Wait for the clocks to stabilize. */
  204. udelay(150);
  205. REG_WRITE(map->dpll, temp | DPLL_VCO_ENABLE);
  206. REG_READ(map->dpll);
  207. /* Wait for the clocks to stabilize. */
  208. udelay(150);
  209. REG_WRITE(map->dpll, temp | DPLL_VCO_ENABLE);
  210. REG_READ(map->dpll);
  211. /* Wait for the clocks to stabilize. */
  212. udelay(150);
  213. }
  214. /* Enable the plane */
  215. temp = REG_READ(map->cntr);
  216. if ((temp & DISPLAY_PLANE_ENABLE) == 0) {
  217. REG_WRITE(map->cntr,
  218. temp | DISPLAY_PLANE_ENABLE);
  219. /* Flush the plane changes */
  220. REG_WRITE(map->base, REG_READ(map->base));
  221. }
  222. udelay(150);
  223. /* Enable the pipe */
  224. temp = REG_READ(map->conf);
  225. if ((temp & PIPEACONF_ENABLE) == 0)
  226. REG_WRITE(map->conf, temp | PIPEACONF_ENABLE);
  227. temp = REG_READ(map->status);
  228. temp &= ~(0xFFFF);
  229. temp |= PIPE_FIFO_UNDERRUN;
  230. REG_WRITE(map->status, temp);
  231. REG_READ(map->status);
  232. gma_crtc_load_lut(crtc);
  233. /* Give the overlay scaler a chance to enable
  234. * if it's on this pipe */
  235. /* psb_intel_crtc_dpms_video(crtc, true); TODO */
  236. break;
  237. case DRM_MODE_DPMS_OFF:
  238. if (!psb_intel_crtc->active)
  239. break;
  240. psb_intel_crtc->active = false;
  241. /* Give the overlay scaler a chance to disable
  242. * if it's on this pipe */
  243. /* psb_intel_crtc_dpms_video(crtc, FALSE); TODO */
  244. /* Disable the VGA plane that we never use */
  245. REG_WRITE(VGACNTRL, VGA_DISP_DISABLE);
  246. /* Turn off vblank interrupts */
  247. drm_vblank_off(dev, pipe);
  248. /* Wait for vblank for the disable to take effect */
  249. gma_wait_for_vblank(dev);
  250. /* Disable plane */
  251. temp = REG_READ(map->cntr);
  252. if ((temp & DISPLAY_PLANE_ENABLE) != 0) {
  253. REG_WRITE(map->cntr,
  254. temp & ~DISPLAY_PLANE_ENABLE);
  255. /* Flush the plane changes */
  256. REG_WRITE(map->base, REG_READ(map->base));
  257. REG_READ(map->base);
  258. }
  259. /* Disable pipe */
  260. temp = REG_READ(map->conf);
  261. if ((temp & PIPEACONF_ENABLE) != 0) {
  262. REG_WRITE(map->conf, temp & ~PIPEACONF_ENABLE);
  263. REG_READ(map->conf);
  264. }
  265. /* Wait for vblank for the disable to take effect. */
  266. gma_wait_for_vblank(dev);
  267. udelay(150);
  268. /* Disable DPLL */
  269. temp = REG_READ(map->dpll);
  270. if ((temp & DPLL_VCO_ENABLE) != 0) {
  271. REG_WRITE(map->dpll, temp & ~DPLL_VCO_ENABLE);
  272. REG_READ(map->dpll);
  273. }
  274. /* Wait for the clocks to turn off. */
  275. udelay(150);
  276. break;
  277. }
  278. /* FIXME: Uncomment this when we move cdv to generic dpms
  279. if (IS_CDV(dev))
  280. cdv_intel_update_watermark(dev, crtc);
  281. */
  282. /* Set FIFO watermarks */
  283. REG_WRITE(DSPARB, 0x3F3E);
  284. }
  285. bool gma_crtc_mode_fixup(struct drm_crtc *crtc,
  286. const struct drm_display_mode *mode,
  287. struct drm_display_mode *adjusted_mode)
  288. {
  289. return true;
  290. }
  291. void gma_crtc_prepare(struct drm_crtc *crtc)
  292. {
  293. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  294. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
  295. }
  296. void gma_crtc_commit(struct drm_crtc *crtc)
  297. {
  298. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  299. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
  300. }
  301. void gma_crtc_disable(struct drm_crtc *crtc)
  302. {
  303. struct gtt_range *gt;
  304. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  305. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
  306. if (crtc->fb) {
  307. gt = to_psb_fb(crtc->fb)->gtt;
  308. psb_gtt_unpin(gt);
  309. }
  310. }
  311. void gma_crtc_destroy(struct drm_crtc *crtc)
  312. {
  313. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  314. kfree(psb_intel_crtc->crtc_state);
  315. drm_crtc_cleanup(crtc);
  316. kfree(psb_intel_crtc);
  317. }
  318. #define GMA_PLL_INVALID(s) { /* DRM_ERROR(s); */ return false; }
  319. bool gma_pll_is_valid(struct drm_crtc *crtc,
  320. const struct gma_limit_t *limit,
  321. struct gma_clock_t *clock)
  322. {
  323. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  324. GMA_PLL_INVALID("p1 out of range");
  325. if (clock->p < limit->p.min || limit->p.max < clock->p)
  326. GMA_PLL_INVALID("p out of range");
  327. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  328. GMA_PLL_INVALID("m2 out of range");
  329. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  330. GMA_PLL_INVALID("m1 out of range");
  331. /* On CDV m1 is always 0 */
  332. if (clock->m1 <= clock->m2 && clock->m1 != 0)
  333. GMA_PLL_INVALID("m1 <= m2 && m1 != 0");
  334. if (clock->m < limit->m.min || limit->m.max < clock->m)
  335. GMA_PLL_INVALID("m out of range");
  336. if (clock->n < limit->n.min || limit->n.max < clock->n)
  337. GMA_PLL_INVALID("n out of range");
  338. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  339. GMA_PLL_INVALID("vco out of range");
  340. /* XXX: We may need to be checking "Dot clock"
  341. * depending on the multiplier, connector, etc.,
  342. * rather than just a single range.
  343. */
  344. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  345. GMA_PLL_INVALID("dot out of range");
  346. return true;
  347. }
  348. bool gma_find_best_pll(const struct gma_limit_t *limit,
  349. struct drm_crtc *crtc, int target, int refclk,
  350. struct gma_clock_t *best_clock)
  351. {
  352. struct drm_device *dev = crtc->dev;
  353. const struct gma_clock_funcs *clock_funcs =
  354. to_psb_intel_crtc(crtc)->clock_funcs;
  355. struct gma_clock_t clock;
  356. int err = target;
  357. if (gma_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
  358. (REG_READ(LVDS) & LVDS_PORT_EN) != 0) {
  359. /*
  360. * For LVDS, if the panel is on, just rely on its current
  361. * settings for dual-channel. We haven't figured out how to
  362. * reliably set up different single/dual channel state, if we
  363. * even can.
  364. */
  365. if ((REG_READ(LVDS) & LVDS_CLKB_POWER_MASK) ==
  366. LVDS_CLKB_POWER_UP)
  367. clock.p2 = limit->p2.p2_fast;
  368. else
  369. clock.p2 = limit->p2.p2_slow;
  370. } else {
  371. if (target < limit->p2.dot_limit)
  372. clock.p2 = limit->p2.p2_slow;
  373. else
  374. clock.p2 = limit->p2.p2_fast;
  375. }
  376. memset(best_clock, 0, sizeof(*best_clock));
  377. /* m1 is always 0 on CDV so the outmost loop will run just once */
  378. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  379. for (clock.m2 = limit->m2.min;
  380. (clock.m2 < clock.m1 || clock.m1 == 0) &&
  381. clock.m2 <= limit->m2.max; clock.m2++) {
  382. for (clock.n = limit->n.min;
  383. clock.n <= limit->n.max; clock.n++) {
  384. for (clock.p1 = limit->p1.min;
  385. clock.p1 <= limit->p1.max;
  386. clock.p1++) {
  387. int this_err;
  388. clock_funcs->clock(refclk, &clock);
  389. if (!clock_funcs->pll_is_valid(crtc,
  390. limit, &clock))
  391. continue;
  392. this_err = abs(clock.dot - target);
  393. if (this_err < err) {
  394. *best_clock = clock;
  395. err = this_err;
  396. }
  397. }
  398. }
  399. }
  400. }
  401. return err != target;
  402. }