psb_intel_display.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361
  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, 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. return -ENOMEM;
  758. }
  759. gt = container_of(obj, struct gtt_range, gem);
  760. /* Pin the memory into the GTT */
  761. ret = psb_gtt_pin(gt);
  762. if (ret) {
  763. dev_err(dev->dev, "Can not pin down handle 0x%x\n", handle);
  764. return ret;
  765. }
  766. if (dev_priv->ops->cursor_needs_phys) {
  767. if (cursor_gt == NULL) {
  768. dev_err(dev->dev, "No hardware cursor mem available");
  769. return -ENOMEM;
  770. }
  771. /* Prevent overflow */
  772. if (gt->npage > 4)
  773. cursor_pages = 4;
  774. else
  775. cursor_pages = gt->npage;
  776. /* Copy the cursor to cursor mem */
  777. tmp_dst = dev_priv->vram_addr + cursor_gt->offset;
  778. for (i = 0; i < cursor_pages; i++) {
  779. tmp_src = kmap(gt->pages[i]);
  780. memcpy(tmp_dst, tmp_src, PAGE_SIZE);
  781. kunmap(gt->pages[i]);
  782. tmp_dst += PAGE_SIZE;
  783. }
  784. addr = psb_intel_crtc->cursor_addr;
  785. } else {
  786. addr = gt->offset; /* Or resource.start ??? */
  787. psb_intel_crtc->cursor_addr = addr;
  788. }
  789. temp = 0;
  790. /* set the pipe for the cursor */
  791. temp |= (pipe << 28);
  792. temp |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
  793. if (gma_power_begin(dev, false)) {
  794. REG_WRITE(control, temp);
  795. REG_WRITE(base, addr);
  796. gma_power_end(dev);
  797. }
  798. /* unpin the old bo */
  799. if (psb_intel_crtc->cursor_obj) {
  800. gt = container_of(psb_intel_crtc->cursor_obj,
  801. struct gtt_range, gem);
  802. psb_gtt_unpin(gt);
  803. drm_gem_object_unreference(psb_intel_crtc->cursor_obj);
  804. psb_intel_crtc->cursor_obj = obj;
  805. }
  806. return 0;
  807. }
  808. static int psb_intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
  809. {
  810. struct drm_device *dev = crtc->dev;
  811. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  812. int pipe = psb_intel_crtc->pipe;
  813. uint32_t temp = 0;
  814. uint32_t addr;
  815. if (x < 0) {
  816. temp |= (CURSOR_POS_SIGN << CURSOR_X_SHIFT);
  817. x = -x;
  818. }
  819. if (y < 0) {
  820. temp |= (CURSOR_POS_SIGN << CURSOR_Y_SHIFT);
  821. y = -y;
  822. }
  823. temp |= ((x & CURSOR_POS_MASK) << CURSOR_X_SHIFT);
  824. temp |= ((y & CURSOR_POS_MASK) << CURSOR_Y_SHIFT);
  825. addr = psb_intel_crtc->cursor_addr;
  826. if (gma_power_begin(dev, false)) {
  827. REG_WRITE((pipe == 0) ? CURAPOS : CURBPOS, temp);
  828. REG_WRITE((pipe == 0) ? CURABASE : CURBBASE, addr);
  829. gma_power_end(dev);
  830. }
  831. return 0;
  832. }
  833. static void psb_intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red,
  834. u16 *green, u16 *blue, uint32_t type, uint32_t size)
  835. {
  836. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  837. int i;
  838. if (size != 256)
  839. return;
  840. for (i = 0; i < 256; i++) {
  841. psb_intel_crtc->lut_r[i] = red[i] >> 8;
  842. psb_intel_crtc->lut_g[i] = green[i] >> 8;
  843. psb_intel_crtc->lut_b[i] = blue[i] >> 8;
  844. }
  845. psb_intel_crtc_load_lut(crtc);
  846. }
  847. static int psb_crtc_set_config(struct drm_mode_set *set)
  848. {
  849. int ret;
  850. struct drm_device *dev = set->crtc->dev;
  851. struct drm_psb_private *dev_priv = dev->dev_private;
  852. if (!dev_priv->rpm_enabled)
  853. return drm_crtc_helper_set_config(set);
  854. pm_runtime_forbid(&dev->pdev->dev);
  855. ret = drm_crtc_helper_set_config(set);
  856. pm_runtime_allow(&dev->pdev->dev);
  857. return ret;
  858. }
  859. /* Returns the clock of the currently programmed mode of the given pipe. */
  860. static int psb_intel_crtc_clock_get(struct drm_device *dev,
  861. struct drm_crtc *crtc)
  862. {
  863. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  864. struct drm_psb_private *dev_priv = dev->dev_private;
  865. int pipe = psb_intel_crtc->pipe;
  866. const struct psb_offset *map = &dev_priv->regmap[pipe];
  867. u32 dpll;
  868. u32 fp;
  869. struct psb_intel_clock_t clock;
  870. bool is_lvds;
  871. struct psb_pipe *p = &dev_priv->regs.pipe[pipe];
  872. if (gma_power_begin(dev, false)) {
  873. dpll = REG_READ(map->dpll);
  874. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  875. fp = REG_READ(map->fp0);
  876. else
  877. fp = REG_READ(map->fp1);
  878. is_lvds = (pipe == 1) && (REG_READ(LVDS) & LVDS_PORT_EN);
  879. gma_power_end(dev);
  880. } else {
  881. dpll = p->dpll;
  882. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  883. fp = p->fp0;
  884. else
  885. fp = p->fp1;
  886. is_lvds = (pipe == 1) && (dev_priv->regs.psb.saveLVDS &
  887. LVDS_PORT_EN);
  888. }
  889. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  890. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  891. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  892. if (is_lvds) {
  893. clock.p1 =
  894. ffs((dpll &
  895. DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  896. DPLL_FPA01_P1_POST_DIV_SHIFT);
  897. clock.p2 = 14;
  898. if ((dpll & PLL_REF_INPUT_MASK) ==
  899. PLLB_REF_INPUT_SPREADSPECTRUMIN) {
  900. /* XXX: might not be 66MHz */
  901. psb_intel_clock(66000, &clock);
  902. } else
  903. psb_intel_clock(48000, &clock);
  904. } else {
  905. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  906. clock.p1 = 2;
  907. else {
  908. clock.p1 =
  909. ((dpll &
  910. DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  911. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  912. }
  913. if (dpll & PLL_P2_DIVIDE_BY_4)
  914. clock.p2 = 4;
  915. else
  916. clock.p2 = 2;
  917. psb_intel_clock(48000, &clock);
  918. }
  919. /* XXX: It would be nice to validate the clocks, but we can't reuse
  920. * i830PllIsValid() because it relies on the xf86_config connector
  921. * configuration being accurate, which it isn't necessarily.
  922. */
  923. return clock.dot;
  924. }
  925. /** Returns the currently programmed mode of the given pipe. */
  926. struct drm_display_mode *psb_intel_crtc_mode_get(struct drm_device *dev,
  927. struct drm_crtc *crtc)
  928. {
  929. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  930. int pipe = psb_intel_crtc->pipe;
  931. struct drm_display_mode *mode;
  932. int htot;
  933. int hsync;
  934. int vtot;
  935. int vsync;
  936. struct drm_psb_private *dev_priv = dev->dev_private;
  937. struct psb_pipe *p = &dev_priv->regs.pipe[pipe];
  938. const struct psb_offset *map = &dev_priv->regmap[pipe];
  939. if (gma_power_begin(dev, false)) {
  940. htot = REG_READ(map->htotal);
  941. hsync = REG_READ(map->hsync);
  942. vtot = REG_READ(map->vtotal);
  943. vsync = REG_READ(map->vsync);
  944. gma_power_end(dev);
  945. } else {
  946. htot = p->htotal;
  947. hsync = p->hsync;
  948. vtot = p->vtotal;
  949. vsync = p->vsync;
  950. }
  951. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  952. if (!mode)
  953. return NULL;
  954. mode->clock = psb_intel_crtc_clock_get(dev, crtc);
  955. mode->hdisplay = (htot & 0xffff) + 1;
  956. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  957. mode->hsync_start = (hsync & 0xffff) + 1;
  958. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  959. mode->vdisplay = (vtot & 0xffff) + 1;
  960. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  961. mode->vsync_start = (vsync & 0xffff) + 1;
  962. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  963. drm_mode_set_name(mode);
  964. drm_mode_set_crtcinfo(mode, 0);
  965. return mode;
  966. }
  967. static void psb_intel_crtc_destroy(struct drm_crtc *crtc)
  968. {
  969. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  970. struct gtt_range *gt;
  971. /* Unpin the old GEM object */
  972. if (psb_intel_crtc->cursor_obj) {
  973. gt = container_of(psb_intel_crtc->cursor_obj,
  974. struct gtt_range, gem);
  975. psb_gtt_unpin(gt);
  976. drm_gem_object_unreference(psb_intel_crtc->cursor_obj);
  977. psb_intel_crtc->cursor_obj = NULL;
  978. }
  979. if (psb_intel_crtc->cursor_gt != NULL)
  980. psb_gtt_free_range(crtc->dev, psb_intel_crtc->cursor_gt);
  981. kfree(psb_intel_crtc->crtc_state);
  982. drm_crtc_cleanup(crtc);
  983. kfree(psb_intel_crtc);
  984. }
  985. static void psb_intel_crtc_disable(struct drm_crtc *crtc)
  986. {
  987. struct gtt_range *gt;
  988. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  989. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
  990. if (crtc->fb) {
  991. gt = to_psb_fb(crtc->fb)->gtt;
  992. psb_gtt_unpin(gt);
  993. }
  994. }
  995. const struct drm_crtc_helper_funcs psb_intel_helper_funcs = {
  996. .dpms = psb_intel_crtc_dpms,
  997. .mode_fixup = psb_intel_crtc_mode_fixup,
  998. .mode_set = psb_intel_crtc_mode_set,
  999. .mode_set_base = psb_intel_pipe_set_base,
  1000. .prepare = psb_intel_crtc_prepare,
  1001. .commit = psb_intel_crtc_commit,
  1002. .disable = psb_intel_crtc_disable,
  1003. };
  1004. const struct drm_crtc_funcs psb_intel_crtc_funcs = {
  1005. .save = psb_intel_crtc_save,
  1006. .restore = psb_intel_crtc_restore,
  1007. .cursor_set = psb_intel_crtc_cursor_set,
  1008. .cursor_move = psb_intel_crtc_cursor_move,
  1009. .gamma_set = psb_intel_crtc_gamma_set,
  1010. .set_config = psb_crtc_set_config,
  1011. .destroy = psb_intel_crtc_destroy,
  1012. };
  1013. /*
  1014. * Set the default value of cursor control and base register
  1015. * to zero. This is a workaround for h/w defect on Oaktrail
  1016. */
  1017. static void psb_intel_cursor_init(struct drm_device *dev,
  1018. struct psb_intel_crtc *psb_intel_crtc)
  1019. {
  1020. struct drm_psb_private *dev_priv = dev->dev_private;
  1021. u32 control[3] = { CURACNTR, CURBCNTR, CURCCNTR };
  1022. u32 base[3] = { CURABASE, CURBBASE, CURCBASE };
  1023. struct gtt_range *cursor_gt;
  1024. if (dev_priv->ops->cursor_needs_phys) {
  1025. /* Allocate 4 pages of stolen mem for a hardware cursor. That
  1026. * is enough for the 64 x 64 ARGB cursors we support.
  1027. */
  1028. cursor_gt = psb_gtt_alloc_range(dev, 4 * PAGE_SIZE, "cursor", 1);
  1029. if (!cursor_gt) {
  1030. psb_intel_crtc->cursor_gt = NULL;
  1031. goto out;
  1032. }
  1033. psb_intel_crtc->cursor_gt = cursor_gt;
  1034. psb_intel_crtc->cursor_addr = dev_priv->stolen_base +
  1035. cursor_gt->offset;
  1036. } else {
  1037. psb_intel_crtc->cursor_gt = NULL;
  1038. }
  1039. out:
  1040. REG_WRITE(control[psb_intel_crtc->pipe], 0);
  1041. REG_WRITE(base[psb_intel_crtc->pipe], 0);
  1042. }
  1043. void psb_intel_crtc_init(struct drm_device *dev, int pipe,
  1044. struct psb_intel_mode_device *mode_dev)
  1045. {
  1046. struct drm_psb_private *dev_priv = dev->dev_private;
  1047. struct psb_intel_crtc *psb_intel_crtc;
  1048. int i;
  1049. uint16_t *r_base, *g_base, *b_base;
  1050. /* We allocate a extra array of drm_connector pointers
  1051. * for fbdev after the crtc */
  1052. psb_intel_crtc =
  1053. kzalloc(sizeof(struct psb_intel_crtc) +
  1054. (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)),
  1055. GFP_KERNEL);
  1056. if (psb_intel_crtc == NULL)
  1057. return;
  1058. psb_intel_crtc->crtc_state =
  1059. kzalloc(sizeof(struct psb_intel_crtc_state), GFP_KERNEL);
  1060. if (!psb_intel_crtc->crtc_state) {
  1061. dev_err(dev->dev, "Crtc state error: No memory\n");
  1062. kfree(psb_intel_crtc);
  1063. return;
  1064. }
  1065. /* Set the CRTC operations from the chip specific data */
  1066. drm_crtc_init(dev, &psb_intel_crtc->base, dev_priv->ops->crtc_funcs);
  1067. drm_mode_crtc_set_gamma_size(&psb_intel_crtc->base, 256);
  1068. psb_intel_crtc->pipe = pipe;
  1069. psb_intel_crtc->plane = pipe;
  1070. r_base = psb_intel_crtc->base.gamma_store;
  1071. g_base = r_base + 256;
  1072. b_base = g_base + 256;
  1073. for (i = 0; i < 256; i++) {
  1074. psb_intel_crtc->lut_r[i] = i;
  1075. psb_intel_crtc->lut_g[i] = i;
  1076. psb_intel_crtc->lut_b[i] = i;
  1077. r_base[i] = i << 8;
  1078. g_base[i] = i << 8;
  1079. b_base[i] = i << 8;
  1080. psb_intel_crtc->lut_adj[i] = 0;
  1081. }
  1082. psb_intel_crtc->mode_dev = mode_dev;
  1083. psb_intel_crtc->cursor_addr = 0;
  1084. drm_crtc_helper_add(&psb_intel_crtc->base,
  1085. dev_priv->ops->crtc_helper);
  1086. /* Setup the array of drm_connector pointer array */
  1087. psb_intel_crtc->mode_set.crtc = &psb_intel_crtc->base;
  1088. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  1089. dev_priv->plane_to_crtc_mapping[psb_intel_crtc->plane] != NULL);
  1090. dev_priv->plane_to_crtc_mapping[psb_intel_crtc->plane] =
  1091. &psb_intel_crtc->base;
  1092. dev_priv->pipe_to_crtc_mapping[psb_intel_crtc->pipe] =
  1093. &psb_intel_crtc->base;
  1094. psb_intel_crtc->mode_set.connectors =
  1095. (struct drm_connector **) (psb_intel_crtc + 1);
  1096. psb_intel_crtc->mode_set.num_connectors = 0;
  1097. psb_intel_cursor_init(dev, psb_intel_crtc);
  1098. /* Set to true so that the pipe is forced off on initial config. */
  1099. psb_intel_crtc->active = true;
  1100. }
  1101. int psb_intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  1102. struct drm_file *file_priv)
  1103. {
  1104. struct drm_psb_private *dev_priv = dev->dev_private;
  1105. struct drm_psb_get_pipe_from_crtc_id_arg *pipe_from_crtc_id = data;
  1106. struct drm_mode_object *drmmode_obj;
  1107. struct psb_intel_crtc *crtc;
  1108. if (!dev_priv) {
  1109. dev_err(dev->dev, "called with no initialization\n");
  1110. return -EINVAL;
  1111. }
  1112. drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
  1113. DRM_MODE_OBJECT_CRTC);
  1114. if (!drmmode_obj) {
  1115. dev_err(dev->dev, "no such CRTC id\n");
  1116. return -EINVAL;
  1117. }
  1118. crtc = to_psb_intel_crtc(obj_to_crtc(drmmode_obj));
  1119. pipe_from_crtc_id->pipe = crtc->pipe;
  1120. return 0;
  1121. }
  1122. struct drm_crtc *psb_intel_get_crtc_from_pipe(struct drm_device *dev, int pipe)
  1123. {
  1124. struct drm_crtc *crtc = NULL;
  1125. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  1126. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  1127. if (psb_intel_crtc->pipe == pipe)
  1128. break;
  1129. }
  1130. return crtc;
  1131. }
  1132. int psb_intel_connector_clones(struct drm_device *dev, int type_mask)
  1133. {
  1134. int index_mask = 0;
  1135. struct drm_connector *connector;
  1136. int entry = 0;
  1137. list_for_each_entry(connector, &dev->mode_config.connector_list,
  1138. head) {
  1139. struct psb_intel_encoder *psb_intel_encoder =
  1140. psb_intel_attached_encoder(connector);
  1141. if (type_mask & (1 << psb_intel_encoder->type))
  1142. index_mask |= (1 << entry);
  1143. entry++;
  1144. }
  1145. return index_mask;
  1146. }
  1147. /* current intel driver doesn't take advantage of encoders
  1148. always give back the encoder for the connector
  1149. */
  1150. struct drm_encoder *psb_intel_best_encoder(struct drm_connector *connector)
  1151. {
  1152. struct psb_intel_encoder *psb_intel_encoder =
  1153. psb_intel_attached_encoder(connector);
  1154. return &psb_intel_encoder->base;
  1155. }
  1156. void psb_intel_connector_attach_encoder(struct psb_intel_connector *connector,
  1157. struct psb_intel_encoder *encoder)
  1158. {
  1159. connector->encoder = encoder;
  1160. drm_mode_connector_attach_encoder(&connector->base,
  1161. &encoder->base);
  1162. }