psb_intel_display.c 36 KB

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