psb_intel_display.c 37 KB

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