psb_intel_display.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375
  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 "psb_intel_display.h"
  28. #include "power.h"
  29. struct psb_intel_clock_t {
  30. /* given values */
  31. int n;
  32. int m1, m2;
  33. int p1, p2;
  34. /* derived values */
  35. int dot;
  36. int vco;
  37. int m;
  38. int p;
  39. };
  40. struct psb_intel_range_t {
  41. int min, max;
  42. };
  43. struct psb_intel_p2_t {
  44. int dot_limit;
  45. int p2_slow, p2_fast;
  46. };
  47. #define INTEL_P2_NUM 2
  48. struct psb_intel_limit_t {
  49. struct psb_intel_range_t dot, vco, n, m, m1, m2, p, p1;
  50. struct psb_intel_p2_t p2;
  51. };
  52. #define I9XX_DOT_MIN 20000
  53. #define I9XX_DOT_MAX 400000
  54. #define I9XX_VCO_MIN 1400000
  55. #define I9XX_VCO_MAX 2800000
  56. #define I9XX_N_MIN 1
  57. #define I9XX_N_MAX 6
  58. #define I9XX_M_MIN 70
  59. #define I9XX_M_MAX 120
  60. #define I9XX_M1_MIN 8
  61. #define I9XX_M1_MAX 18
  62. #define I9XX_M2_MIN 3
  63. #define I9XX_M2_MAX 7
  64. #define I9XX_P_SDVO_DAC_MIN 5
  65. #define I9XX_P_SDVO_DAC_MAX 80
  66. #define I9XX_P_LVDS_MIN 7
  67. #define I9XX_P_LVDS_MAX 98
  68. #define I9XX_P1_MIN 1
  69. #define I9XX_P1_MAX 8
  70. #define I9XX_P2_SDVO_DAC_SLOW 10
  71. #define I9XX_P2_SDVO_DAC_FAST 5
  72. #define I9XX_P2_SDVO_DAC_SLOW_LIMIT 200000
  73. #define I9XX_P2_LVDS_SLOW 14
  74. #define I9XX_P2_LVDS_FAST 7
  75. #define I9XX_P2_LVDS_SLOW_LIMIT 112000
  76. #define INTEL_LIMIT_I9XX_SDVO_DAC 0
  77. #define INTEL_LIMIT_I9XX_LVDS 1
  78. static const struct psb_intel_limit_t psb_intel_limits[] = {
  79. { /* INTEL_LIMIT_I9XX_SDVO_DAC */
  80. .dot = {.min = I9XX_DOT_MIN, .max = I9XX_DOT_MAX},
  81. .vco = {.min = I9XX_VCO_MIN, .max = I9XX_VCO_MAX},
  82. .n = {.min = I9XX_N_MIN, .max = I9XX_N_MAX},
  83. .m = {.min = I9XX_M_MIN, .max = I9XX_M_MAX},
  84. .m1 = {.min = I9XX_M1_MIN, .max = I9XX_M1_MAX},
  85. .m2 = {.min = I9XX_M2_MIN, .max = I9XX_M2_MAX},
  86. .p = {.min = I9XX_P_SDVO_DAC_MIN, .max = I9XX_P_SDVO_DAC_MAX},
  87. .p1 = {.min = I9XX_P1_MIN, .max = I9XX_P1_MAX},
  88. .p2 = {.dot_limit = I9XX_P2_SDVO_DAC_SLOW_LIMIT,
  89. .p2_slow = I9XX_P2_SDVO_DAC_SLOW, .p2_fast =
  90. I9XX_P2_SDVO_DAC_FAST},
  91. },
  92. { /* INTEL_LIMIT_I9XX_LVDS */
  93. .dot = {.min = I9XX_DOT_MIN, .max = I9XX_DOT_MAX},
  94. .vco = {.min = I9XX_VCO_MIN, .max = I9XX_VCO_MAX},
  95. .n = {.min = I9XX_N_MIN, .max = I9XX_N_MAX},
  96. .m = {.min = I9XX_M_MIN, .max = I9XX_M_MAX},
  97. .m1 = {.min = I9XX_M1_MIN, .max = I9XX_M1_MAX},
  98. .m2 = {.min = I9XX_M2_MIN, .max = I9XX_M2_MAX},
  99. .p = {.min = I9XX_P_LVDS_MIN, .max = I9XX_P_LVDS_MAX},
  100. .p1 = {.min = I9XX_P1_MIN, .max = I9XX_P1_MAX},
  101. /* The single-channel range is 25-112Mhz, and dual-channel
  102. * is 80-224Mhz. Prefer single channel as much as possible.
  103. */
  104. .p2 = {.dot_limit = I9XX_P2_LVDS_SLOW_LIMIT,
  105. .p2_slow = I9XX_P2_LVDS_SLOW, .p2_fast = I9XX_P2_LVDS_FAST},
  106. },
  107. };
  108. static const struct psb_intel_limit_t *psb_intel_limit(struct drm_crtc *crtc)
  109. {
  110. const struct psb_intel_limit_t *limit;
  111. if (psb_intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  112. limit = &psb_intel_limits[INTEL_LIMIT_I9XX_LVDS];
  113. else
  114. limit = &psb_intel_limits[INTEL_LIMIT_I9XX_SDVO_DAC];
  115. return limit;
  116. }
  117. static void psb_intel_clock(int refclk, struct psb_intel_clock_t *clock)
  118. {
  119. clock->m = 5 * (clock->m1 + 2) + (clock->m2 + 2);
  120. clock->p = clock->p1 * clock->p2;
  121. clock->vco = refclk * clock->m / (clock->n + 2);
  122. clock->dot = clock->vco / clock->p;
  123. }
  124. /**
  125. * Returns whether any output on the specified pipe is of the specified type
  126. */
  127. bool psb_intel_pipe_has_type(struct drm_crtc *crtc, int type)
  128. {
  129. struct drm_device *dev = crtc->dev;
  130. struct drm_mode_config *mode_config = &dev->mode_config;
  131. struct drm_connector *l_entry;
  132. list_for_each_entry(l_entry, &mode_config->connector_list, head) {
  133. if (l_entry->encoder && l_entry->encoder->crtc == crtc) {
  134. struct psb_intel_encoder *psb_intel_encoder =
  135. psb_intel_attached_encoder(l_entry);
  136. if (psb_intel_encoder->type == type)
  137. return true;
  138. }
  139. }
  140. return false;
  141. }
  142. #define INTELPllInvalid(s) { /* ErrorF (s) */; return false; }
  143. /**
  144. * Returns whether the given set of divisors are valid for a given refclk with
  145. * the given connectors.
  146. */
  147. static bool psb_intel_PLL_is_valid(struct drm_crtc *crtc,
  148. struct psb_intel_clock_t *clock)
  149. {
  150. const struct psb_intel_limit_t *limit = psb_intel_limit(crtc);
  151. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  152. INTELPllInvalid("p1 out of range\n");
  153. if (clock->p < limit->p.min || limit->p.max < clock->p)
  154. INTELPllInvalid("p out of range\n");
  155. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  156. INTELPllInvalid("m2 out of range\n");
  157. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  158. INTELPllInvalid("m1 out of range\n");
  159. if (clock->m1 <= clock->m2)
  160. INTELPllInvalid("m1 <= m2\n");
  161. if (clock->m < limit->m.min || limit->m.max < clock->m)
  162. INTELPllInvalid("m out of range\n");
  163. if (clock->n < limit->n.min || limit->n.max < clock->n)
  164. INTELPllInvalid("n out of range\n");
  165. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  166. INTELPllInvalid("vco out of range\n");
  167. /* XXX: We may need to be checking "Dot clock"
  168. * depending on the multiplier, connector, etc.,
  169. * rather than just a single range.
  170. */
  171. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  172. INTELPllInvalid("dot out of range\n");
  173. return true;
  174. }
  175. /**
  176. * Returns a set of divisors for the desired target clock with the given
  177. * refclk, or FALSE. The returned values represent the clock equation:
  178. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  179. */
  180. static bool psb_intel_find_best_PLL(struct drm_crtc *crtc, int target,
  181. int refclk,
  182. struct psb_intel_clock_t *best_clock)
  183. {
  184. struct drm_device *dev = crtc->dev;
  185. struct psb_intel_clock_t clock;
  186. const struct psb_intel_limit_t *limit = psb_intel_limit(crtc);
  187. int err = target;
  188. if (psb_intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
  189. (REG_READ(LVDS) & LVDS_PORT_EN) != 0) {
  190. /*
  191. * For LVDS, if the panel is on, just rely on its current
  192. * settings for dual-channel. We haven't figured out how to
  193. * reliably set up different single/dual channel state, if we
  194. * even can.
  195. */
  196. if ((REG_READ(LVDS) & LVDS_CLKB_POWER_MASK) ==
  197. LVDS_CLKB_POWER_UP)
  198. clock.p2 = limit->p2.p2_fast;
  199. else
  200. clock.p2 = limit->p2.p2_slow;
  201. } else {
  202. if (target < limit->p2.dot_limit)
  203. clock.p2 = limit->p2.p2_slow;
  204. else
  205. clock.p2 = limit->p2.p2_fast;
  206. }
  207. memset(best_clock, 0, sizeof(*best_clock));
  208. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  209. clock.m1++) {
  210. for (clock.m2 = limit->m2.min;
  211. clock.m2 < clock.m1 && clock.m2 <= limit->m2.max;
  212. clock.m2++) {
  213. for (clock.n = limit->n.min;
  214. clock.n <= limit->n.max; clock.n++) {
  215. for (clock.p1 = limit->p1.min;
  216. clock.p1 <= limit->p1.max;
  217. clock.p1++) {
  218. int this_err;
  219. psb_intel_clock(refclk, &clock);
  220. if (!psb_intel_PLL_is_valid
  221. (crtc, &clock))
  222. continue;
  223. this_err = abs(clock.dot - target);
  224. if (this_err < err) {
  225. *best_clock = clock;
  226. err = this_err;
  227. }
  228. }
  229. }
  230. }
  231. }
  232. return err != target;
  233. }
  234. void psb_intel_wait_for_vblank(struct drm_device *dev)
  235. {
  236. /* Wait for 20ms, i.e. one cycle at 50hz. */
  237. mdelay(20);
  238. }
  239. static int psb_intel_pipe_set_base(struct drm_crtc *crtc,
  240. int x, int y, struct drm_framebuffer *old_fb)
  241. {
  242. struct drm_device *dev = crtc->dev;
  243. struct drm_psb_private *dev_priv = dev->dev_private;
  244. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  245. struct psb_framebuffer *psbfb = to_psb_fb(crtc->fb);
  246. int pipe = psb_intel_crtc->pipe;
  247. const struct psb_offset *map = &dev_priv->regmap[pipe];
  248. unsigned long start, offset;
  249. u32 dspcntr;
  250. int ret = 0;
  251. if (!gma_power_begin(dev, true))
  252. return 0;
  253. /* no fb bound */
  254. if (!crtc->fb) {
  255. dev_dbg(dev->dev, "No FB bound\n");
  256. goto psb_intel_pipe_cleaner;
  257. }
  258. /* We are displaying this buffer, make sure it is actually loaded
  259. into the GTT */
  260. ret = psb_gtt_pin(psbfb->gtt);
  261. if (ret < 0)
  262. goto psb_intel_pipe_set_base_exit;
  263. start = psbfb->gtt->offset;
  264. offset = y * crtc->fb->pitches[0] + x * (crtc->fb->bits_per_pixel / 8);
  265. REG_WRITE(map->stride, crtc->fb->pitches[0]);
  266. dspcntr = REG_READ(map->cntr);
  267. dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
  268. switch (crtc->fb->bits_per_pixel) {
  269. case 8:
  270. dspcntr |= DISPPLANE_8BPP;
  271. break;
  272. case 16:
  273. if (crtc->fb->depth == 15)
  274. dspcntr |= DISPPLANE_15_16BPP;
  275. else
  276. dspcntr |= DISPPLANE_16BPP;
  277. break;
  278. case 24:
  279. case 32:
  280. dspcntr |= DISPPLANE_32BPP_NO_ALPHA;
  281. break;
  282. default:
  283. dev_err(dev->dev, "Unknown color depth\n");
  284. ret = -EINVAL;
  285. psb_gtt_unpin(psbfb->gtt);
  286. goto psb_intel_pipe_set_base_exit;
  287. }
  288. REG_WRITE(map->cntr, dspcntr);
  289. REG_WRITE(map->base, start + offset);
  290. REG_READ(map->base);
  291. psb_intel_pipe_cleaner:
  292. /* If there was a previous display we can now unpin it */
  293. if (old_fb)
  294. psb_gtt_unpin(to_psb_fb(old_fb)->gtt);
  295. psb_intel_pipe_set_base_exit:
  296. gma_power_end(dev);
  297. return ret;
  298. }
  299. /**
  300. * Sets the power management mode of the pipe and plane.
  301. *
  302. * This code should probably grow support for turning the cursor off and back
  303. * on appropriately at the same time as we're turning the pipe off/on.
  304. */
  305. static void psb_intel_crtc_dpms(struct drm_crtc *crtc, int mode)
  306. {
  307. struct drm_device *dev = crtc->dev;
  308. struct drm_psb_private *dev_priv = dev->dev_private;
  309. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  310. int pipe = psb_intel_crtc->pipe;
  311. const struct psb_offset *map = &dev_priv->regmap[pipe];
  312. u32 temp;
  313. /* XXX: When our outputs are all unaware of DPMS modes other than off
  314. * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
  315. */
  316. switch (mode) {
  317. case DRM_MODE_DPMS_ON:
  318. case DRM_MODE_DPMS_STANDBY:
  319. case DRM_MODE_DPMS_SUSPEND:
  320. /* Enable the DPLL */
  321. temp = REG_READ(map->dpll);
  322. if ((temp & DPLL_VCO_ENABLE) == 0) {
  323. REG_WRITE(map->dpll, temp);
  324. REG_READ(map->dpll);
  325. /* Wait for the clocks to stabilize. */
  326. udelay(150);
  327. REG_WRITE(map->dpll, temp | DPLL_VCO_ENABLE);
  328. REG_READ(map->dpll);
  329. /* Wait for the clocks to stabilize. */
  330. udelay(150);
  331. REG_WRITE(map->dpll, temp | DPLL_VCO_ENABLE);
  332. REG_READ(map->dpll);
  333. /* Wait for the clocks to stabilize. */
  334. udelay(150);
  335. }
  336. /* Enable the pipe */
  337. temp = REG_READ(map->conf);
  338. if ((temp & PIPEACONF_ENABLE) == 0)
  339. REG_WRITE(map->conf, temp | PIPEACONF_ENABLE);
  340. /* Enable the plane */
  341. temp = REG_READ(map->cntr);
  342. if ((temp & DISPLAY_PLANE_ENABLE) == 0) {
  343. REG_WRITE(map->cntr,
  344. temp | DISPLAY_PLANE_ENABLE);
  345. /* Flush the plane changes */
  346. REG_WRITE(map->base, REG_READ(map->base));
  347. }
  348. psb_intel_crtc_load_lut(crtc);
  349. /* Give the overlay scaler a chance to enable
  350. * if it's on this pipe */
  351. /* psb_intel_crtc_dpms_video(crtc, true); TODO */
  352. break;
  353. case DRM_MODE_DPMS_OFF:
  354. /* Give the overlay scaler a chance to disable
  355. * if it's on this pipe */
  356. /* psb_intel_crtc_dpms_video(crtc, FALSE); TODO */
  357. /* Disable the VGA plane that we never use */
  358. REG_WRITE(VGACNTRL, VGA_DISP_DISABLE);
  359. /* Disable display plane */
  360. temp = REG_READ(map->cntr);
  361. if ((temp & DISPLAY_PLANE_ENABLE) != 0) {
  362. REG_WRITE(map->cntr,
  363. temp & ~DISPLAY_PLANE_ENABLE);
  364. /* Flush the plane changes */
  365. REG_WRITE(map->base, REG_READ(map->base));
  366. REG_READ(map->base);
  367. }
  368. /* Next, disable display pipes */
  369. temp = REG_READ(map->conf);
  370. if ((temp & PIPEACONF_ENABLE) != 0) {
  371. REG_WRITE(map->conf, temp & ~PIPEACONF_ENABLE);
  372. REG_READ(map->conf);
  373. }
  374. /* Wait for vblank for the disable to take effect. */
  375. psb_intel_wait_for_vblank(dev);
  376. temp = REG_READ(map->dpll);
  377. if ((temp & DPLL_VCO_ENABLE) != 0) {
  378. REG_WRITE(map->dpll, temp & ~DPLL_VCO_ENABLE);
  379. REG_READ(map->dpll);
  380. }
  381. /* Wait for the clocks to turn off. */
  382. udelay(150);
  383. break;
  384. }
  385. /*Set FIFO Watermarks*/
  386. REG_WRITE(DSPARB, 0x3F3E);
  387. }
  388. static void psb_intel_crtc_prepare(struct drm_crtc *crtc)
  389. {
  390. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  391. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
  392. }
  393. static void psb_intel_crtc_commit(struct drm_crtc *crtc)
  394. {
  395. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  396. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
  397. }
  398. void psb_intel_encoder_prepare(struct drm_encoder *encoder)
  399. {
  400. struct drm_encoder_helper_funcs *encoder_funcs =
  401. encoder->helper_private;
  402. /* lvds has its own version of prepare see psb_intel_lvds_prepare */
  403. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
  404. }
  405. void psb_intel_encoder_commit(struct drm_encoder *encoder)
  406. {
  407. struct drm_encoder_helper_funcs *encoder_funcs =
  408. encoder->helper_private;
  409. /* lvds has its own version of commit see psb_intel_lvds_commit */
  410. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
  411. }
  412. void psb_intel_encoder_destroy(struct drm_encoder *encoder)
  413. {
  414. struct psb_intel_encoder *intel_encoder = to_psb_intel_encoder(encoder);
  415. drm_encoder_cleanup(encoder);
  416. kfree(intel_encoder);
  417. }
  418. static bool psb_intel_crtc_mode_fixup(struct drm_crtc *crtc,
  419. const struct drm_display_mode *mode,
  420. struct drm_display_mode *adjusted_mode)
  421. {
  422. return true;
  423. }
  424. /**
  425. * Return the pipe currently connected to the panel fitter,
  426. * or -1 if the panel fitter is not present or not in use
  427. */
  428. static int psb_intel_panel_fitter_pipe(struct drm_device *dev)
  429. {
  430. u32 pfit_control;
  431. pfit_control = REG_READ(PFIT_CONTROL);
  432. /* See if the panel fitter is in use */
  433. if ((pfit_control & PFIT_ENABLE) == 0)
  434. return -1;
  435. /* Must be on PIPE 1 for PSB */
  436. return 1;
  437. }
  438. static int psb_intel_crtc_mode_set(struct drm_crtc *crtc,
  439. struct drm_display_mode *mode,
  440. struct drm_display_mode *adjusted_mode,
  441. int x, int y,
  442. struct drm_framebuffer *old_fb)
  443. {
  444. struct drm_device *dev = crtc->dev;
  445. struct drm_psb_private *dev_priv = dev->dev_private;
  446. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  447. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  448. int pipe = psb_intel_crtc->pipe;
  449. const struct psb_offset *map = &dev_priv->regmap[pipe];
  450. int refclk;
  451. struct psb_intel_clock_t clock;
  452. u32 dpll = 0, fp = 0, dspcntr, pipeconf;
  453. bool ok, is_sdvo = false;
  454. bool is_lvds = false, is_tv = false;
  455. struct drm_mode_config *mode_config = &dev->mode_config;
  456. struct drm_connector *connector;
  457. /* No scan out no play */
  458. if (crtc->fb == NULL) {
  459. crtc_funcs->mode_set_base(crtc, x, y, old_fb);
  460. return 0;
  461. }
  462. list_for_each_entry(connector, &mode_config->connector_list, head) {
  463. struct psb_intel_encoder *psb_intel_encoder =
  464. psb_intel_attached_encoder(connector);
  465. if (!connector->encoder
  466. || connector->encoder->crtc != crtc)
  467. continue;
  468. switch (psb_intel_encoder->type) {
  469. case INTEL_OUTPUT_LVDS:
  470. is_lvds = true;
  471. break;
  472. case INTEL_OUTPUT_SDVO:
  473. is_sdvo = true;
  474. break;
  475. case INTEL_OUTPUT_TVOUT:
  476. is_tv = true;
  477. break;
  478. }
  479. }
  480. refclk = 96000;
  481. ok = psb_intel_find_best_PLL(crtc, adjusted_mode->clock, refclk,
  482. &clock);
  483. if (!ok) {
  484. dev_err(dev->dev, "Couldn't find PLL settings for mode!\n");
  485. return 0;
  486. }
  487. fp = clock.n << 16 | clock.m1 << 8 | clock.m2;
  488. dpll = DPLL_VGA_MODE_DIS;
  489. if (is_lvds) {
  490. dpll |= DPLLB_MODE_LVDS;
  491. dpll |= DPLL_DVO_HIGH_SPEED;
  492. } else
  493. dpll |= DPLLB_MODE_DAC_SERIAL;
  494. if (is_sdvo) {
  495. int sdvo_pixel_multiply =
  496. adjusted_mode->clock / mode->clock;
  497. dpll |= DPLL_DVO_HIGH_SPEED;
  498. dpll |=
  499. (sdvo_pixel_multiply - 1) << SDVO_MULTIPLIER_SHIFT_HIRES;
  500. }
  501. /* compute bitmask from p1 value */
  502. dpll |= (1 << (clock.p1 - 1)) << 16;
  503. switch (clock.p2) {
  504. case 5:
  505. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  506. break;
  507. case 7:
  508. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  509. break;
  510. case 10:
  511. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  512. break;
  513. case 14:
  514. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  515. break;
  516. }
  517. if (is_tv) {
  518. /* XXX: just matching BIOS for now */
  519. /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
  520. dpll |= 3;
  521. }
  522. dpll |= PLL_REF_INPUT_DREFCLK;
  523. /* setup pipeconf */
  524. pipeconf = REG_READ(map->conf);
  525. /* Set up the display plane register */
  526. dspcntr = DISPPLANE_GAMMA_ENABLE;
  527. if (pipe == 0)
  528. dspcntr |= DISPPLANE_SEL_PIPE_A;
  529. else
  530. dspcntr |= DISPPLANE_SEL_PIPE_B;
  531. dspcntr |= DISPLAY_PLANE_ENABLE;
  532. pipeconf |= PIPEACONF_ENABLE;
  533. dpll |= DPLL_VCO_ENABLE;
  534. /* Disable the panel fitter if it was on our pipe */
  535. if (psb_intel_panel_fitter_pipe(dev) == pipe)
  536. REG_WRITE(PFIT_CONTROL, 0);
  537. drm_mode_debug_printmodeline(mode);
  538. if (dpll & DPLL_VCO_ENABLE) {
  539. REG_WRITE(map->fp0, fp);
  540. REG_WRITE(map->dpll, dpll & ~DPLL_VCO_ENABLE);
  541. REG_READ(map->dpll);
  542. udelay(150);
  543. }
  544. /* The LVDS pin pair needs to be on before the DPLLs are enabled.
  545. * This is an exception to the general rule that mode_set doesn't turn
  546. * things on.
  547. */
  548. if (is_lvds) {
  549. u32 lvds = REG_READ(LVDS);
  550. lvds &= ~LVDS_PIPEB_SELECT;
  551. if (pipe == 1)
  552. lvds |= LVDS_PIPEB_SELECT;
  553. lvds |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP;
  554. /* Set the B0-B3 data pairs corresponding to
  555. * whether we're going to
  556. * set the DPLLs for dual-channel mode or not.
  557. */
  558. lvds &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
  559. if (clock.p2 == 7)
  560. lvds |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
  561. /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
  562. * appropriately here, but we need to look more
  563. * thoroughly into how panels behave in the two modes.
  564. */
  565. REG_WRITE(LVDS, lvds);
  566. REG_READ(LVDS);
  567. }
  568. REG_WRITE(map->fp0, fp);
  569. REG_WRITE(map->dpll, dpll);
  570. REG_READ(map->dpll);
  571. /* Wait for the clocks to stabilize. */
  572. udelay(150);
  573. /* write it again -- the BIOS does, after all */
  574. REG_WRITE(map->dpll, dpll);
  575. REG_READ(map->dpll);
  576. /* Wait for the clocks to stabilize. */
  577. udelay(150);
  578. REG_WRITE(map->htotal, (adjusted_mode->crtc_hdisplay - 1) |
  579. ((adjusted_mode->crtc_htotal - 1) << 16));
  580. REG_WRITE(map->hblank, (adjusted_mode->crtc_hblank_start - 1) |
  581. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  582. REG_WRITE(map->hsync, (adjusted_mode->crtc_hsync_start - 1) |
  583. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  584. REG_WRITE(map->vtotal, (adjusted_mode->crtc_vdisplay - 1) |
  585. ((adjusted_mode->crtc_vtotal - 1) << 16));
  586. REG_WRITE(map->vblank, (adjusted_mode->crtc_vblank_start - 1) |
  587. ((adjusted_mode->crtc_vblank_end - 1) << 16));
  588. REG_WRITE(map->vsync, (adjusted_mode->crtc_vsync_start - 1) |
  589. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  590. /* pipesrc and dspsize control the size that is scaled from,
  591. * which should always be the user's requested size.
  592. */
  593. REG_WRITE(map->size,
  594. ((mode->vdisplay - 1) << 16) | (mode->hdisplay - 1));
  595. REG_WRITE(map->pos, 0);
  596. REG_WRITE(map->src,
  597. ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
  598. REG_WRITE(map->conf, pipeconf);
  599. REG_READ(map->conf);
  600. psb_intel_wait_for_vblank(dev);
  601. REG_WRITE(map->cntr, dspcntr);
  602. /* Flush the plane changes */
  603. crtc_funcs->mode_set_base(crtc, x, y, old_fb);
  604. psb_intel_wait_for_vblank(dev);
  605. return 0;
  606. }
  607. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  608. void psb_intel_crtc_load_lut(struct drm_crtc *crtc)
  609. {
  610. struct drm_device *dev = crtc->dev;
  611. struct drm_psb_private *dev_priv = dev->dev_private;
  612. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  613. const struct psb_offset *map = &dev_priv->regmap[psb_intel_crtc->pipe];
  614. int palreg = map->palette;
  615. int i;
  616. /* The clocks have to be on to load the palette. */
  617. if (!crtc->enabled)
  618. return;
  619. switch (psb_intel_crtc->pipe) {
  620. case 0:
  621. case 1:
  622. break;
  623. default:
  624. dev_err(dev->dev, "Illegal Pipe Number.\n");
  625. return;
  626. }
  627. if (gma_power_begin(dev, false)) {
  628. for (i = 0; i < 256; i++) {
  629. REG_WRITE(palreg + 4 * i,
  630. ((psb_intel_crtc->lut_r[i] +
  631. psb_intel_crtc->lut_adj[i]) << 16) |
  632. ((psb_intel_crtc->lut_g[i] +
  633. psb_intel_crtc->lut_adj[i]) << 8) |
  634. (psb_intel_crtc->lut_b[i] +
  635. psb_intel_crtc->lut_adj[i]));
  636. }
  637. gma_power_end(dev);
  638. } else {
  639. for (i = 0; i < 256; i++) {
  640. dev_priv->regs.pipe[0].palette[i] =
  641. ((psb_intel_crtc->lut_r[i] +
  642. psb_intel_crtc->lut_adj[i]) << 16) |
  643. ((psb_intel_crtc->lut_g[i] +
  644. psb_intel_crtc->lut_adj[i]) << 8) |
  645. (psb_intel_crtc->lut_b[i] +
  646. psb_intel_crtc->lut_adj[i]);
  647. }
  648. }
  649. }
  650. /**
  651. * Save HW states of giving crtc
  652. */
  653. static void psb_intel_crtc_save(struct drm_crtc *crtc)
  654. {
  655. struct drm_device *dev = crtc->dev;
  656. struct drm_psb_private *dev_priv = dev->dev_private;
  657. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  658. struct psb_intel_crtc_state *crtc_state = psb_intel_crtc->crtc_state;
  659. const struct psb_offset *map = &dev_priv->regmap[psb_intel_crtc->pipe];
  660. uint32_t paletteReg;
  661. int i;
  662. if (!crtc_state) {
  663. dev_err(dev->dev, "No CRTC state found\n");
  664. return;
  665. }
  666. crtc_state->saveDSPCNTR = REG_READ(map->cntr);
  667. crtc_state->savePIPECONF = REG_READ(map->conf);
  668. crtc_state->savePIPESRC = REG_READ(map->src);
  669. crtc_state->saveFP0 = REG_READ(map->fp0);
  670. crtc_state->saveFP1 = REG_READ(map->fp1);
  671. crtc_state->saveDPLL = REG_READ(map->dpll);
  672. crtc_state->saveHTOTAL = REG_READ(map->htotal);
  673. crtc_state->saveHBLANK = REG_READ(map->hblank);
  674. crtc_state->saveHSYNC = REG_READ(map->hsync);
  675. crtc_state->saveVTOTAL = REG_READ(map->vtotal);
  676. crtc_state->saveVBLANK = REG_READ(map->vblank);
  677. crtc_state->saveVSYNC = REG_READ(map->vsync);
  678. crtc_state->saveDSPSTRIDE = REG_READ(map->stride);
  679. /*NOTE: DSPSIZE DSPPOS only for psb*/
  680. crtc_state->saveDSPSIZE = REG_READ(map->size);
  681. crtc_state->saveDSPPOS = REG_READ(map->pos);
  682. crtc_state->saveDSPBASE = REG_READ(map->base);
  683. paletteReg = map->palette;
  684. for (i = 0; i < 256; ++i)
  685. crtc_state->savePalette[i] = REG_READ(paletteReg + (i << 2));
  686. }
  687. /**
  688. * Restore HW states of giving crtc
  689. */
  690. static void psb_intel_crtc_restore(struct drm_crtc *crtc)
  691. {
  692. struct drm_device *dev = crtc->dev;
  693. struct drm_psb_private *dev_priv = dev->dev_private;
  694. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  695. struct psb_intel_crtc_state *crtc_state = psb_intel_crtc->crtc_state;
  696. const struct psb_offset *map = &dev_priv->regmap[psb_intel_crtc->pipe];
  697. uint32_t paletteReg;
  698. int i;
  699. if (!crtc_state) {
  700. dev_err(dev->dev, "No crtc state\n");
  701. return;
  702. }
  703. if (crtc_state->saveDPLL & DPLL_VCO_ENABLE) {
  704. REG_WRITE(map->dpll,
  705. crtc_state->saveDPLL & ~DPLL_VCO_ENABLE);
  706. REG_READ(map->dpll);
  707. udelay(150);
  708. }
  709. REG_WRITE(map->fp0, crtc_state->saveFP0);
  710. REG_READ(map->fp0);
  711. REG_WRITE(map->fp1, crtc_state->saveFP1);
  712. REG_READ(map->fp1);
  713. REG_WRITE(map->dpll, crtc_state->saveDPLL);
  714. REG_READ(map->dpll);
  715. udelay(150);
  716. REG_WRITE(map->htotal, crtc_state->saveHTOTAL);
  717. REG_WRITE(map->hblank, crtc_state->saveHBLANK);
  718. REG_WRITE(map->hsync, crtc_state->saveHSYNC);
  719. REG_WRITE(map->vtotal, crtc_state->saveVTOTAL);
  720. REG_WRITE(map->vblank, crtc_state->saveVBLANK);
  721. REG_WRITE(map->vsync, crtc_state->saveVSYNC);
  722. REG_WRITE(map->stride, crtc_state->saveDSPSTRIDE);
  723. REG_WRITE(map->size, crtc_state->saveDSPSIZE);
  724. REG_WRITE(map->pos, crtc_state->saveDSPPOS);
  725. REG_WRITE(map->src, crtc_state->savePIPESRC);
  726. REG_WRITE(map->base, crtc_state->saveDSPBASE);
  727. REG_WRITE(map->conf, crtc_state->savePIPECONF);
  728. psb_intel_wait_for_vblank(dev);
  729. REG_WRITE(map->cntr, crtc_state->saveDSPCNTR);
  730. REG_WRITE(map->base, crtc_state->saveDSPBASE);
  731. psb_intel_wait_for_vblank(dev);
  732. paletteReg = map->palette;
  733. for (i = 0; i < 256; ++i)
  734. REG_WRITE(paletteReg + (i << 2), crtc_state->savePalette[i]);
  735. }
  736. static int psb_intel_crtc_cursor_set(struct drm_crtc *crtc,
  737. struct drm_file *file_priv,
  738. uint32_t handle,
  739. uint32_t width, uint32_t height)
  740. {
  741. struct drm_device *dev = crtc->dev;
  742. struct drm_psb_private *dev_priv = dev->dev_private;
  743. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  744. int pipe = psb_intel_crtc->pipe;
  745. uint32_t control = (pipe == 0) ? CURACNTR : CURBCNTR;
  746. uint32_t base = (pipe == 0) ? CURABASE : CURBBASE;
  747. uint32_t temp;
  748. size_t addr = 0;
  749. struct gtt_range *gt;
  750. struct gtt_range *cursor_gt = psb_intel_crtc->cursor_gt;
  751. struct drm_gem_object *obj;
  752. void *tmp_dst, *tmp_src;
  753. int ret, i, cursor_pages;
  754. /* if we want to turn of the cursor ignore width and height */
  755. if (!handle) {
  756. /* turn off the cursor */
  757. temp = CURSOR_MODE_DISABLE;
  758. if (gma_power_begin(dev, false)) {
  759. REG_WRITE(control, temp);
  760. REG_WRITE(base, 0);
  761. gma_power_end(dev);
  762. }
  763. /* Unpin the old GEM object */
  764. if (psb_intel_crtc->cursor_obj) {
  765. gt = container_of(psb_intel_crtc->cursor_obj,
  766. struct gtt_range, gem);
  767. psb_gtt_unpin(gt);
  768. drm_gem_object_unreference(psb_intel_crtc->cursor_obj);
  769. psb_intel_crtc->cursor_obj = NULL;
  770. }
  771. return 0;
  772. }
  773. /* Currently we only support 64x64 cursors */
  774. if (width != 64 || height != 64) {
  775. dev_dbg(dev->dev, "we currently only support 64x64 cursors\n");
  776. return -EINVAL;
  777. }
  778. obj = drm_gem_object_lookup(dev, file_priv, handle);
  779. if (!obj)
  780. return -ENOENT;
  781. if (obj->size < width * height * 4) {
  782. dev_dbg(dev->dev, "buffer is to small\n");
  783. return -ENOMEM;
  784. }
  785. gt = container_of(obj, struct gtt_range, gem);
  786. /* Pin the memory into the GTT */
  787. ret = psb_gtt_pin(gt);
  788. if (ret) {
  789. dev_err(dev->dev, "Can not pin down handle 0x%x\n", handle);
  790. return ret;
  791. }
  792. if (dev_priv->ops->cursor_needs_phys) {
  793. if (cursor_gt == NULL) {
  794. dev_err(dev->dev, "No hardware cursor mem available");
  795. return -ENOMEM;
  796. }
  797. /* Prevent overflow */
  798. if (gt->npage > 4)
  799. cursor_pages = 4;
  800. else
  801. cursor_pages = gt->npage;
  802. /* Copy the cursor to cursor mem */
  803. tmp_dst = dev_priv->vram_addr + cursor_gt->offset;
  804. for (i = 0; i < cursor_pages; i++) {
  805. tmp_src = kmap(gt->pages[i]);
  806. memcpy(tmp_dst, tmp_src, PAGE_SIZE);
  807. kunmap(gt->pages[i]);
  808. tmp_dst += PAGE_SIZE;
  809. }
  810. addr = psb_intel_crtc->cursor_addr;
  811. } else {
  812. addr = gt->offset; /* Or resource.start ??? */
  813. psb_intel_crtc->cursor_addr = addr;
  814. }
  815. temp = 0;
  816. /* set the pipe for the cursor */
  817. temp |= (pipe << 28);
  818. temp |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
  819. if (gma_power_begin(dev, false)) {
  820. REG_WRITE(control, temp);
  821. REG_WRITE(base, addr);
  822. gma_power_end(dev);
  823. }
  824. /* unpin the old bo */
  825. if (psb_intel_crtc->cursor_obj) {
  826. gt = container_of(psb_intel_crtc->cursor_obj,
  827. struct gtt_range, gem);
  828. psb_gtt_unpin(gt);
  829. drm_gem_object_unreference(psb_intel_crtc->cursor_obj);
  830. psb_intel_crtc->cursor_obj = obj;
  831. }
  832. return 0;
  833. }
  834. static int psb_intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
  835. {
  836. struct drm_device *dev = crtc->dev;
  837. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  838. int pipe = psb_intel_crtc->pipe;
  839. uint32_t temp = 0;
  840. uint32_t addr;
  841. if (x < 0) {
  842. temp |= (CURSOR_POS_SIGN << CURSOR_X_SHIFT);
  843. x = -x;
  844. }
  845. if (y < 0) {
  846. temp |= (CURSOR_POS_SIGN << CURSOR_Y_SHIFT);
  847. y = -y;
  848. }
  849. temp |= ((x & CURSOR_POS_MASK) << CURSOR_X_SHIFT);
  850. temp |= ((y & CURSOR_POS_MASK) << CURSOR_Y_SHIFT);
  851. addr = psb_intel_crtc->cursor_addr;
  852. if (gma_power_begin(dev, false)) {
  853. REG_WRITE((pipe == 0) ? CURAPOS : CURBPOS, temp);
  854. REG_WRITE((pipe == 0) ? CURABASE : CURBBASE, addr);
  855. gma_power_end(dev);
  856. }
  857. return 0;
  858. }
  859. void psb_intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red,
  860. u16 *green, u16 *blue, uint32_t type, uint32_t size)
  861. {
  862. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  863. int i;
  864. if (size != 256)
  865. return;
  866. for (i = 0; i < 256; i++) {
  867. psb_intel_crtc->lut_r[i] = red[i] >> 8;
  868. psb_intel_crtc->lut_g[i] = green[i] >> 8;
  869. psb_intel_crtc->lut_b[i] = blue[i] >> 8;
  870. }
  871. psb_intel_crtc_load_lut(crtc);
  872. }
  873. static int psb_crtc_set_config(struct drm_mode_set *set)
  874. {
  875. int ret;
  876. struct drm_device *dev = set->crtc->dev;
  877. struct drm_psb_private *dev_priv = dev->dev_private;
  878. if (!dev_priv->rpm_enabled)
  879. return drm_crtc_helper_set_config(set);
  880. pm_runtime_forbid(&dev->pdev->dev);
  881. ret = drm_crtc_helper_set_config(set);
  882. pm_runtime_allow(&dev->pdev->dev);
  883. return ret;
  884. }
  885. /* Returns the clock of the currently programmed mode of the given pipe. */
  886. static int psb_intel_crtc_clock_get(struct drm_device *dev,
  887. struct drm_crtc *crtc)
  888. {
  889. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  890. struct drm_psb_private *dev_priv = dev->dev_private;
  891. int pipe = psb_intel_crtc->pipe;
  892. const struct psb_offset *map = &dev_priv->regmap[pipe];
  893. u32 dpll;
  894. u32 fp;
  895. struct psb_intel_clock_t clock;
  896. bool is_lvds;
  897. struct psb_pipe *p = &dev_priv->regs.pipe[pipe];
  898. if (gma_power_begin(dev, false)) {
  899. dpll = REG_READ(map->dpll);
  900. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  901. fp = REG_READ(map->fp0);
  902. else
  903. fp = REG_READ(map->fp1);
  904. is_lvds = (pipe == 1) && (REG_READ(LVDS) & LVDS_PORT_EN);
  905. gma_power_end(dev);
  906. } else {
  907. dpll = p->dpll;
  908. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  909. fp = p->fp0;
  910. else
  911. fp = p->fp1;
  912. is_lvds = (pipe == 1) && (dev_priv->regs.psb.saveLVDS &
  913. LVDS_PORT_EN);
  914. }
  915. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  916. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  917. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  918. if (is_lvds) {
  919. clock.p1 =
  920. ffs((dpll &
  921. DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  922. DPLL_FPA01_P1_POST_DIV_SHIFT);
  923. clock.p2 = 14;
  924. if ((dpll & PLL_REF_INPUT_MASK) ==
  925. PLLB_REF_INPUT_SPREADSPECTRUMIN) {
  926. /* XXX: might not be 66MHz */
  927. psb_intel_clock(66000, &clock);
  928. } else
  929. psb_intel_clock(48000, &clock);
  930. } else {
  931. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  932. clock.p1 = 2;
  933. else {
  934. clock.p1 =
  935. ((dpll &
  936. DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  937. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  938. }
  939. if (dpll & PLL_P2_DIVIDE_BY_4)
  940. clock.p2 = 4;
  941. else
  942. clock.p2 = 2;
  943. psb_intel_clock(48000, &clock);
  944. }
  945. /* XXX: It would be nice to validate the clocks, but we can't reuse
  946. * i830PllIsValid() because it relies on the xf86_config connector
  947. * configuration being accurate, which it isn't necessarily.
  948. */
  949. return clock.dot;
  950. }
  951. /** Returns the currently programmed mode of the given pipe. */
  952. struct drm_display_mode *psb_intel_crtc_mode_get(struct drm_device *dev,
  953. struct drm_crtc *crtc)
  954. {
  955. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  956. int pipe = psb_intel_crtc->pipe;
  957. struct drm_display_mode *mode;
  958. int htot;
  959. int hsync;
  960. int vtot;
  961. int vsync;
  962. struct drm_psb_private *dev_priv = dev->dev_private;
  963. struct psb_pipe *p = &dev_priv->regs.pipe[pipe];
  964. const struct psb_offset *map = &dev_priv->regmap[pipe];
  965. if (gma_power_begin(dev, false)) {
  966. htot = REG_READ(map->htotal);
  967. hsync = REG_READ(map->hsync);
  968. vtot = REG_READ(map->vtotal);
  969. vsync = REG_READ(map->vsync);
  970. gma_power_end(dev);
  971. } else {
  972. htot = p->htotal;
  973. hsync = p->hsync;
  974. vtot = p->vtotal;
  975. vsync = p->vsync;
  976. }
  977. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  978. if (!mode)
  979. return NULL;
  980. mode->clock = psb_intel_crtc_clock_get(dev, crtc);
  981. mode->hdisplay = (htot & 0xffff) + 1;
  982. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  983. mode->hsync_start = (hsync & 0xffff) + 1;
  984. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  985. mode->vdisplay = (vtot & 0xffff) + 1;
  986. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  987. mode->vsync_start = (vsync & 0xffff) + 1;
  988. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  989. drm_mode_set_name(mode);
  990. drm_mode_set_crtcinfo(mode, 0);
  991. return mode;
  992. }
  993. void psb_intel_crtc_destroy(struct drm_crtc *crtc)
  994. {
  995. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  996. struct gtt_range *gt;
  997. /* Unpin the old GEM object */
  998. if (psb_intel_crtc->cursor_obj) {
  999. gt = container_of(psb_intel_crtc->cursor_obj,
  1000. struct gtt_range, gem);
  1001. psb_gtt_unpin(gt);
  1002. drm_gem_object_unreference(psb_intel_crtc->cursor_obj);
  1003. psb_intel_crtc->cursor_obj = NULL;
  1004. }
  1005. if (psb_intel_crtc->cursor_gt != NULL)
  1006. psb_gtt_free_range(crtc->dev, psb_intel_crtc->cursor_gt);
  1007. kfree(psb_intel_crtc->crtc_state);
  1008. drm_crtc_cleanup(crtc);
  1009. kfree(psb_intel_crtc);
  1010. }
  1011. const struct drm_crtc_helper_funcs psb_intel_helper_funcs = {
  1012. .dpms = psb_intel_crtc_dpms,
  1013. .mode_fixup = psb_intel_crtc_mode_fixup,
  1014. .mode_set = psb_intel_crtc_mode_set,
  1015. .mode_set_base = psb_intel_pipe_set_base,
  1016. .prepare = psb_intel_crtc_prepare,
  1017. .commit = psb_intel_crtc_commit,
  1018. };
  1019. const struct drm_crtc_funcs psb_intel_crtc_funcs = {
  1020. .save = psb_intel_crtc_save,
  1021. .restore = psb_intel_crtc_restore,
  1022. .cursor_set = psb_intel_crtc_cursor_set,
  1023. .cursor_move = psb_intel_crtc_cursor_move,
  1024. .gamma_set = psb_intel_crtc_gamma_set,
  1025. .set_config = psb_crtc_set_config,
  1026. .destroy = psb_intel_crtc_destroy,
  1027. };
  1028. /*
  1029. * Set the default value of cursor control and base register
  1030. * to zero. This is a workaround for h/w defect on Oaktrail
  1031. */
  1032. static void psb_intel_cursor_init(struct drm_device *dev,
  1033. struct psb_intel_crtc *psb_intel_crtc)
  1034. {
  1035. struct drm_psb_private *dev_priv = dev->dev_private;
  1036. u32 control[3] = { CURACNTR, CURBCNTR, CURCCNTR };
  1037. u32 base[3] = { CURABASE, CURBBASE, CURCBASE };
  1038. struct gtt_range *cursor_gt;
  1039. if (dev_priv->ops->cursor_needs_phys) {
  1040. /* Allocate 4 pages of stolen mem for a hardware cursor. That
  1041. * is enough for the 64 x 64 ARGB cursors we support.
  1042. */
  1043. cursor_gt = psb_gtt_alloc_range(dev, 4 * PAGE_SIZE, "cursor", 1);
  1044. if (!cursor_gt) {
  1045. psb_intel_crtc->cursor_gt = NULL;
  1046. goto out;
  1047. }
  1048. psb_intel_crtc->cursor_gt = cursor_gt;
  1049. psb_intel_crtc->cursor_addr = dev_priv->stolen_base +
  1050. cursor_gt->offset;
  1051. } else {
  1052. psb_intel_crtc->cursor_gt = NULL;
  1053. }
  1054. out:
  1055. REG_WRITE(control[psb_intel_crtc->pipe], 0);
  1056. REG_WRITE(base[psb_intel_crtc->pipe], 0);
  1057. }
  1058. void psb_intel_crtc_init(struct drm_device *dev, int pipe,
  1059. struct psb_intel_mode_device *mode_dev)
  1060. {
  1061. struct drm_psb_private *dev_priv = dev->dev_private;
  1062. struct psb_intel_crtc *psb_intel_crtc;
  1063. int i;
  1064. uint16_t *r_base, *g_base, *b_base;
  1065. /* We allocate a extra array of drm_connector pointers
  1066. * for fbdev after the crtc */
  1067. psb_intel_crtc =
  1068. kzalloc(sizeof(struct psb_intel_crtc) +
  1069. (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)),
  1070. GFP_KERNEL);
  1071. if (psb_intel_crtc == NULL)
  1072. return;
  1073. psb_intel_crtc->crtc_state =
  1074. kzalloc(sizeof(struct psb_intel_crtc_state), GFP_KERNEL);
  1075. if (!psb_intel_crtc->crtc_state) {
  1076. dev_err(dev->dev, "Crtc state error: No memory\n");
  1077. kfree(psb_intel_crtc);
  1078. return;
  1079. }
  1080. /* Set the CRTC operations from the chip specific data */
  1081. drm_crtc_init(dev, &psb_intel_crtc->base, dev_priv->ops->crtc_funcs);
  1082. drm_mode_crtc_set_gamma_size(&psb_intel_crtc->base, 256);
  1083. psb_intel_crtc->pipe = pipe;
  1084. psb_intel_crtc->plane = pipe;
  1085. r_base = psb_intel_crtc->base.gamma_store;
  1086. g_base = r_base + 256;
  1087. b_base = g_base + 256;
  1088. for (i = 0; i < 256; i++) {
  1089. psb_intel_crtc->lut_r[i] = i;
  1090. psb_intel_crtc->lut_g[i] = i;
  1091. psb_intel_crtc->lut_b[i] = i;
  1092. r_base[i] = i << 8;
  1093. g_base[i] = i << 8;
  1094. b_base[i] = i << 8;
  1095. psb_intel_crtc->lut_adj[i] = 0;
  1096. }
  1097. psb_intel_crtc->mode_dev = mode_dev;
  1098. psb_intel_crtc->cursor_addr = 0;
  1099. drm_crtc_helper_add(&psb_intel_crtc->base,
  1100. dev_priv->ops->crtc_helper);
  1101. /* Setup the array of drm_connector pointer array */
  1102. psb_intel_crtc->mode_set.crtc = &psb_intel_crtc->base;
  1103. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  1104. dev_priv->plane_to_crtc_mapping[psb_intel_crtc->plane] != NULL);
  1105. dev_priv->plane_to_crtc_mapping[psb_intel_crtc->plane] =
  1106. &psb_intel_crtc->base;
  1107. dev_priv->pipe_to_crtc_mapping[psb_intel_crtc->pipe] =
  1108. &psb_intel_crtc->base;
  1109. psb_intel_crtc->mode_set.connectors =
  1110. (struct drm_connector **) (psb_intel_crtc + 1);
  1111. psb_intel_crtc->mode_set.num_connectors = 0;
  1112. psb_intel_cursor_init(dev, psb_intel_crtc);
  1113. /* Set to true so that the pipe is forced off on initial config. */
  1114. psb_intel_crtc->active = true;
  1115. }
  1116. int psb_intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  1117. struct drm_file *file_priv)
  1118. {
  1119. struct drm_psb_private *dev_priv = dev->dev_private;
  1120. struct drm_psb_get_pipe_from_crtc_id_arg *pipe_from_crtc_id = data;
  1121. struct drm_mode_object *drmmode_obj;
  1122. struct psb_intel_crtc *crtc;
  1123. if (!dev_priv) {
  1124. dev_err(dev->dev, "called with no initialization\n");
  1125. return -EINVAL;
  1126. }
  1127. drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
  1128. DRM_MODE_OBJECT_CRTC);
  1129. if (!drmmode_obj) {
  1130. dev_err(dev->dev, "no such CRTC id\n");
  1131. return -EINVAL;
  1132. }
  1133. crtc = to_psb_intel_crtc(obj_to_crtc(drmmode_obj));
  1134. pipe_from_crtc_id->pipe = crtc->pipe;
  1135. return 0;
  1136. }
  1137. struct drm_crtc *psb_intel_get_crtc_from_pipe(struct drm_device *dev, int pipe)
  1138. {
  1139. struct drm_crtc *crtc = NULL;
  1140. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  1141. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  1142. if (psb_intel_crtc->pipe == pipe)
  1143. break;
  1144. }
  1145. return crtc;
  1146. }
  1147. int psb_intel_connector_clones(struct drm_device *dev, int type_mask)
  1148. {
  1149. int index_mask = 0;
  1150. struct drm_connector *connector;
  1151. int entry = 0;
  1152. list_for_each_entry(connector, &dev->mode_config.connector_list,
  1153. head) {
  1154. struct psb_intel_encoder *psb_intel_encoder =
  1155. psb_intel_attached_encoder(connector);
  1156. if (type_mask & (1 << psb_intel_encoder->type))
  1157. index_mask |= (1 << entry);
  1158. entry++;
  1159. }
  1160. return index_mask;
  1161. }
  1162. /* current intel driver doesn't take advantage of encoders
  1163. always give back the encoder for the connector
  1164. */
  1165. struct drm_encoder *psb_intel_best_encoder(struct drm_connector *connector)
  1166. {
  1167. struct psb_intel_encoder *psb_intel_encoder =
  1168. psb_intel_attached_encoder(connector);
  1169. return &psb_intel_encoder->base;
  1170. }
  1171. void psb_intel_connector_attach_encoder(struct psb_intel_connector *connector,
  1172. struct psb_intel_encoder *encoder)
  1173. {
  1174. connector->encoder = encoder;
  1175. drm_mode_connector_attach_encoder(&connector->base,
  1176. &encoder->base);
  1177. }