psb_intel_display.c 40 KB

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