oaktrail_hdmi.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  1. /*
  2. * Copyright © 2010 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Li Peng <peng.li@intel.com>
  25. */
  26. #include <drm/drmP.h>
  27. #include <drm/drm.h>
  28. #include "psb_intel_drv.h"
  29. #include "psb_intel_reg.h"
  30. #include "psb_drv.h"
  31. #define HDMI_READ(reg) readl(hdmi_dev->regs + (reg))
  32. #define HDMI_WRITE(reg, val) writel(val, hdmi_dev->regs + (reg))
  33. #define HDMI_HCR 0x1000
  34. #define HCR_ENABLE_HDCP (1 << 5)
  35. #define HCR_ENABLE_AUDIO (1 << 2)
  36. #define HCR_ENABLE_PIXEL (1 << 1)
  37. #define HCR_ENABLE_TMDS (1 << 0)
  38. #define HDMI_HICR 0x1004
  39. #define HDMI_HSR 0x1008
  40. #define HDMI_HISR 0x100C
  41. #define HDMI_DETECT_HDP (1 << 0)
  42. #define HDMI_VIDEO_REG 0x3000
  43. #define HDMI_UNIT_EN (1 << 7)
  44. #define HDMI_MODE_OUTPUT (1 << 0)
  45. #define HDMI_HBLANK_A 0x3100
  46. #define HDMI_AUDIO_CTRL 0x4000
  47. #define HDMI_ENABLE_AUDIO (1 << 0)
  48. #define PCH_HTOTAL_B 0x3100
  49. #define PCH_HBLANK_B 0x3104
  50. #define PCH_HSYNC_B 0x3108
  51. #define PCH_VTOTAL_B 0x310C
  52. #define PCH_VBLANK_B 0x3110
  53. #define PCH_VSYNC_B 0x3114
  54. #define PCH_PIPEBSRC 0x311C
  55. #define PCH_PIPEB_DSL 0x3800
  56. #define PCH_PIPEB_SLC 0x3804
  57. #define PCH_PIPEBCONF 0x3808
  58. #define PCH_PIPEBSTAT 0x3824
  59. #define CDVO_DFT 0x5000
  60. #define CDVO_SLEWRATE 0x5004
  61. #define CDVO_STRENGTH 0x5008
  62. #define CDVO_RCOMP 0x500C
  63. #define DPLL_CTRL 0x6000
  64. #define DPLL_PDIV_SHIFT 16
  65. #define DPLL_PDIV_MASK (0xf << 16)
  66. #define DPLL_PWRDN (1 << 4)
  67. #define DPLL_RESET (1 << 3)
  68. #define DPLL_FASTEN (1 << 2)
  69. #define DPLL_ENSTAT (1 << 1)
  70. #define DPLL_DITHEN (1 << 0)
  71. #define DPLL_DIV_CTRL 0x6004
  72. #define DPLL_CLKF_MASK 0xffffffc0
  73. #define DPLL_CLKR_MASK (0x3f)
  74. #define DPLL_CLK_ENABLE 0x6008
  75. #define DPLL_EN_DISP (1 << 31)
  76. #define DPLL_SEL_HDMI (1 << 8)
  77. #define DPLL_EN_HDMI (1 << 1)
  78. #define DPLL_EN_VGA (1 << 0)
  79. #define DPLL_ADJUST 0x600C
  80. #define DPLL_STATUS 0x6010
  81. #define DPLL_UPDATE 0x6014
  82. #define DPLL_DFT 0x6020
  83. struct intel_range {
  84. int min, max;
  85. };
  86. struct oaktrail_hdmi_limit {
  87. struct intel_range vco, np, nr, nf;
  88. };
  89. struct oaktrail_hdmi_clock {
  90. int np;
  91. int nr;
  92. int nf;
  93. int dot;
  94. };
  95. #define VCO_MIN 320000
  96. #define VCO_MAX 1650000
  97. #define NP_MIN 1
  98. #define NP_MAX 15
  99. #define NR_MIN 1
  100. #define NR_MAX 64
  101. #define NF_MIN 2
  102. #define NF_MAX 4095
  103. static const struct oaktrail_hdmi_limit oaktrail_hdmi_limit = {
  104. .vco = { .min = VCO_MIN, .max = VCO_MAX },
  105. .np = { .min = NP_MIN, .max = NP_MAX },
  106. .nr = { .min = NR_MIN, .max = NR_MAX },
  107. .nf = { .min = NF_MIN, .max = NF_MAX },
  108. };
  109. static void wait_for_vblank(struct drm_device *dev)
  110. {
  111. /* FIXME: Can we do this as a sleep ? */
  112. /* Wait for 20ms, i.e. one cycle at 50hz. */
  113. mdelay(20);
  114. }
  115. static void scu_busy_loop(void *scu_base)
  116. {
  117. u32 status = 0;
  118. u32 loop_count = 0;
  119. status = readl(scu_base + 0x04);
  120. while (status & 1) {
  121. udelay(1); /* scu processing time is in few u secods */
  122. status = readl(scu_base + 0x04);
  123. loop_count++;
  124. /* break if scu doesn't reset busy bit after huge retry */
  125. if (loop_count > 1000) {
  126. DRM_DEBUG_KMS("SCU IPC timed out");
  127. return;
  128. }
  129. }
  130. }
  131. static void oaktrail_hdmi_reset(struct drm_device *dev)
  132. {
  133. void *base;
  134. /* FIXME: at least make these defines */
  135. unsigned int scu_ipc_mmio = 0xff11c000;
  136. int scu_len = 1024;
  137. base = ioremap((resource_size_t)scu_ipc_mmio, scu_len);
  138. if (base == NULL) {
  139. DRM_ERROR("failed to map SCU mmio\n");
  140. return;
  141. }
  142. /* scu ipc: assert hdmi controller reset */
  143. writel(0xff11d118, base + 0x0c);
  144. writel(0x7fffffdf, base + 0x80);
  145. writel(0x42005, base + 0x0);
  146. scu_busy_loop(base);
  147. /* scu ipc: de-assert hdmi controller reset */
  148. writel(0xff11d118, base + 0x0c);
  149. writel(0x7fffffff, base + 0x80);
  150. writel(0x42005, base + 0x0);
  151. scu_busy_loop(base);
  152. iounmap(base);
  153. }
  154. static void oaktrail_hdmi_audio_enable(struct drm_device *dev)
  155. {
  156. struct drm_psb_private *dev_priv = dev->dev_private;
  157. struct oaktrail_hdmi_dev *hdmi_dev = dev_priv->hdmi_priv;
  158. HDMI_WRITE(HDMI_HCR, 0x67);
  159. HDMI_READ(HDMI_HCR);
  160. HDMI_WRITE(0x51a8, 0x10);
  161. HDMI_READ(0x51a8);
  162. HDMI_WRITE(HDMI_AUDIO_CTRL, 0x1);
  163. HDMI_READ(HDMI_AUDIO_CTRL);
  164. }
  165. static void oaktrail_hdmi_audio_disable(struct drm_device *dev)
  166. {
  167. struct drm_psb_private *dev_priv = dev->dev_private;
  168. struct oaktrail_hdmi_dev *hdmi_dev = dev_priv->hdmi_priv;
  169. HDMI_WRITE(0x51a8, 0x0);
  170. HDMI_READ(0x51a8);
  171. HDMI_WRITE(HDMI_AUDIO_CTRL, 0x0);
  172. HDMI_READ(HDMI_AUDIO_CTRL);
  173. HDMI_WRITE(HDMI_HCR, 0x47);
  174. HDMI_READ(HDMI_HCR);
  175. }
  176. void oaktrail_crtc_hdmi_dpms(struct drm_crtc *crtc, int mode)
  177. {
  178. struct drm_device *dev = crtc->dev;
  179. u32 temp;
  180. switch (mode) {
  181. case DRM_MODE_DPMS_OFF:
  182. /* Disable VGACNTRL */
  183. REG_WRITE(VGACNTRL, 0x80000000);
  184. /* Disable plane */
  185. temp = REG_READ(DSPBCNTR);
  186. if ((temp & DISPLAY_PLANE_ENABLE) != 0) {
  187. REG_WRITE(DSPBCNTR, temp & ~DISPLAY_PLANE_ENABLE);
  188. REG_READ(DSPBCNTR);
  189. /* Flush the plane changes */
  190. REG_WRITE(DSPBSURF, REG_READ(DSPBSURF));
  191. REG_READ(DSPBSURF);
  192. }
  193. /* Disable pipe B */
  194. temp = REG_READ(PIPEBCONF);
  195. if ((temp & PIPEACONF_ENABLE) != 0) {
  196. REG_WRITE(PIPEBCONF, temp & ~PIPEACONF_ENABLE);
  197. REG_READ(PIPEBCONF);
  198. }
  199. /* Disable LNW Pipes, etc */
  200. temp = REG_READ(PCH_PIPEBCONF);
  201. if ((temp & PIPEACONF_ENABLE) != 0) {
  202. REG_WRITE(PCH_PIPEBCONF, temp & ~PIPEACONF_ENABLE);
  203. REG_READ(PCH_PIPEBCONF);
  204. }
  205. /* wait for pipe off */
  206. udelay(150);
  207. /* Disable dpll */
  208. temp = REG_READ(DPLL_CTRL);
  209. if ((temp & DPLL_PWRDN) == 0) {
  210. REG_WRITE(DPLL_CTRL, temp | (DPLL_PWRDN | DPLL_RESET));
  211. REG_WRITE(DPLL_STATUS, 0x1);
  212. }
  213. /* wait for dpll off */
  214. udelay(150);
  215. break;
  216. case DRM_MODE_DPMS_ON:
  217. case DRM_MODE_DPMS_STANDBY:
  218. case DRM_MODE_DPMS_SUSPEND:
  219. /* Enable dpll */
  220. temp = REG_READ(DPLL_CTRL);
  221. if ((temp & DPLL_PWRDN) != 0) {
  222. REG_WRITE(DPLL_CTRL, temp & ~(DPLL_PWRDN | DPLL_RESET));
  223. temp = REG_READ(DPLL_CLK_ENABLE);
  224. REG_WRITE(DPLL_CLK_ENABLE, temp | DPLL_EN_DISP | DPLL_SEL_HDMI | DPLL_EN_HDMI);
  225. REG_READ(DPLL_CLK_ENABLE);
  226. }
  227. /* wait for dpll warm up */
  228. udelay(150);
  229. /* Enable pipe B */
  230. temp = REG_READ(PIPEBCONF);
  231. if ((temp & PIPEACONF_ENABLE) == 0) {
  232. REG_WRITE(PIPEBCONF, temp | PIPEACONF_ENABLE);
  233. REG_READ(PIPEBCONF);
  234. }
  235. /* Enable LNW Pipe B */
  236. temp = REG_READ(PCH_PIPEBCONF);
  237. if ((temp & PIPEACONF_ENABLE) == 0) {
  238. REG_WRITE(PCH_PIPEBCONF, temp | PIPEACONF_ENABLE);
  239. REG_READ(PCH_PIPEBCONF);
  240. }
  241. wait_for_vblank(dev);
  242. /* Enable plane */
  243. temp = REG_READ(DSPBCNTR);
  244. if ((temp & DISPLAY_PLANE_ENABLE) == 0) {
  245. REG_WRITE(DSPBCNTR, temp | DISPLAY_PLANE_ENABLE);
  246. /* Flush the plane changes */
  247. REG_WRITE(DSPBSURF, REG_READ(DSPBSURF));
  248. REG_READ(DSPBSURF);
  249. }
  250. psb_intel_crtc_load_lut(crtc);
  251. }
  252. /* DSPARB */
  253. REG_WRITE(DSPARB, 0x00003fbf);
  254. /* FW1 */
  255. REG_WRITE(0x70034, 0x3f880a0a);
  256. /* FW2 */
  257. REG_WRITE(0x70038, 0x0b060808);
  258. /* FW4 */
  259. REG_WRITE(0x70050, 0x08030404);
  260. /* FW5 */
  261. REG_WRITE(0x70054, 0x04040404);
  262. /* LNC Chicken Bits */
  263. REG_WRITE(0x70400, 0x4000);
  264. }
  265. static void oaktrail_hdmi_dpms(struct drm_encoder *encoder, int mode)
  266. {
  267. static int dpms_mode = -1;
  268. struct drm_device *dev = encoder->dev;
  269. struct drm_psb_private *dev_priv = dev->dev_private;
  270. struct oaktrail_hdmi_dev *hdmi_dev = dev_priv->hdmi_priv;
  271. u32 temp;
  272. if (dpms_mode == mode)
  273. return;
  274. if (mode != DRM_MODE_DPMS_ON)
  275. temp = 0x0;
  276. else
  277. temp = 0x99;
  278. dpms_mode = mode;
  279. HDMI_WRITE(HDMI_VIDEO_REG, temp);
  280. }
  281. static unsigned int htotal_calculate(struct drm_display_mode *mode)
  282. {
  283. u32 htotal, new_crtc_htotal;
  284. htotal = (mode->crtc_hdisplay - 1) | ((mode->crtc_htotal - 1) << 16);
  285. /*
  286. * 1024 x 768 new_crtc_htotal = 0x1024;
  287. * 1280 x 1024 new_crtc_htotal = 0x0c34;
  288. */
  289. new_crtc_htotal = (mode->crtc_htotal - 1) * 200 * 1000 / mode->clock;
  290. return (mode->crtc_hdisplay - 1) | (new_crtc_htotal << 16);
  291. }
  292. static void oaktrail_hdmi_find_dpll(struct drm_crtc *crtc, int target,
  293. int refclk, struct oaktrail_hdmi_clock *best_clock)
  294. {
  295. int np_min, np_max, nr_min, nr_max;
  296. int np, nr, nf;
  297. np_min = DIV_ROUND_UP(oaktrail_hdmi_limit.vco.min, target * 10);
  298. np_max = oaktrail_hdmi_limit.vco.max / (target * 10);
  299. if (np_min < oaktrail_hdmi_limit.np.min)
  300. np_min = oaktrail_hdmi_limit.np.min;
  301. if (np_max > oaktrail_hdmi_limit.np.max)
  302. np_max = oaktrail_hdmi_limit.np.max;
  303. nr_min = DIV_ROUND_UP((refclk * 1000), (target * 10 * np_max));
  304. nr_max = DIV_ROUND_UP((refclk * 1000), (target * 10 * np_min));
  305. if (nr_min < oaktrail_hdmi_limit.nr.min)
  306. nr_min = oaktrail_hdmi_limit.nr.min;
  307. if (nr_max > oaktrail_hdmi_limit.nr.max)
  308. nr_max = oaktrail_hdmi_limit.nr.max;
  309. np = DIV_ROUND_UP((refclk * 1000), (target * 10 * nr_max));
  310. nr = DIV_ROUND_UP((refclk * 1000), (target * 10 * np));
  311. nf = DIV_ROUND_CLOSEST((target * 10 * np * nr), refclk);
  312. DRM_DEBUG_KMS("np, nr, nf %d %d %d\n", np, nr, nf);
  313. /*
  314. * 1024 x 768 np = 1; nr = 0x26; nf = 0x0fd8000;
  315. * 1280 x 1024 np = 1; nr = 0x17; nf = 0x1034000;
  316. */
  317. best_clock->np = np;
  318. best_clock->nr = nr - 1;
  319. best_clock->nf = (nf << 14);
  320. }
  321. int oaktrail_crtc_hdmi_mode_set(struct drm_crtc *crtc,
  322. struct drm_display_mode *mode,
  323. struct drm_display_mode *adjusted_mode,
  324. int x, int y,
  325. struct drm_framebuffer *old_fb)
  326. {
  327. struct drm_device *dev = crtc->dev;
  328. struct drm_psb_private *dev_priv = dev->dev_private;
  329. struct oaktrail_hdmi_dev *hdmi_dev = dev_priv->hdmi_priv;
  330. int pipe = 1;
  331. int htot_reg = (pipe == 0) ? HTOTAL_A : HTOTAL_B;
  332. int hblank_reg = (pipe == 0) ? HBLANK_A : HBLANK_B;
  333. int hsync_reg = (pipe == 0) ? HSYNC_A : HSYNC_B;
  334. int vtot_reg = (pipe == 0) ? VTOTAL_A : VTOTAL_B;
  335. int vblank_reg = (pipe == 0) ? VBLANK_A : VBLANK_B;
  336. int vsync_reg = (pipe == 0) ? VSYNC_A : VSYNC_B;
  337. int dspsize_reg = (pipe == 0) ? DSPASIZE : DSPBSIZE;
  338. int dsppos_reg = (pipe == 0) ? DSPAPOS : DSPBPOS;
  339. int pipesrc_reg = (pipe == 0) ? PIPEASRC : PIPEBSRC;
  340. int pipeconf_reg = (pipe == 0) ? PIPEACONF : PIPEBCONF;
  341. int refclk;
  342. struct oaktrail_hdmi_clock clock;
  343. u32 dspcntr, pipeconf, dpll, temp;
  344. int dspcntr_reg = DSPBCNTR;
  345. /* Disable the VGA plane that we never use */
  346. REG_WRITE(VGACNTRL, VGA_DISP_DISABLE);
  347. /* XXX: Disable the panel fitter if it was on our pipe */
  348. /* Disable dpll if necessary */
  349. dpll = REG_READ(DPLL_CTRL);
  350. if ((dpll & DPLL_PWRDN) == 0) {
  351. REG_WRITE(DPLL_CTRL, dpll | (DPLL_PWRDN | DPLL_RESET));
  352. REG_WRITE(DPLL_DIV_CTRL, 0x00000000);
  353. REG_WRITE(DPLL_STATUS, 0x1);
  354. }
  355. udelay(150);
  356. /* reset controller: FIXME - can we sort out the ioremap mess ? */
  357. iounmap(hdmi_dev->regs);
  358. oaktrail_hdmi_reset(dev);
  359. /* program and enable dpll */
  360. refclk = 25000;
  361. oaktrail_hdmi_find_dpll(crtc, adjusted_mode->clock, refclk, &clock);
  362. /* Setting DPLL */
  363. dpll = REG_READ(DPLL_CTRL);
  364. dpll &= ~DPLL_PDIV_MASK;
  365. dpll &= ~(DPLL_PWRDN | DPLL_RESET);
  366. REG_WRITE(DPLL_CTRL, 0x00000008);
  367. REG_WRITE(DPLL_DIV_CTRL, ((clock.nf << 6) | clock.nr));
  368. REG_WRITE(DPLL_ADJUST, ((clock.nf >> 14) - 1));
  369. REG_WRITE(DPLL_CTRL, (dpll | (clock.np << DPLL_PDIV_SHIFT) | DPLL_ENSTAT | DPLL_DITHEN));
  370. REG_WRITE(DPLL_UPDATE, 0x80000000);
  371. REG_WRITE(DPLL_CLK_ENABLE, 0x80050102);
  372. udelay(150);
  373. hdmi_dev->regs = ioremap(hdmi_dev->mmio, hdmi_dev->mmio_len);
  374. if (hdmi_dev->regs == NULL) {
  375. DRM_ERROR("failed to do hdmi mmio mapping\n");
  376. return -ENOMEM;
  377. }
  378. /* configure HDMI */
  379. HDMI_WRITE(0x1004, 0x1fd);
  380. HDMI_WRITE(0x2000, 0x1);
  381. HDMI_WRITE(0x2008, 0x0);
  382. HDMI_WRITE(0x3130, 0x8);
  383. HDMI_WRITE(0x101c, 0x1800810);
  384. temp = htotal_calculate(adjusted_mode);
  385. REG_WRITE(htot_reg, temp);
  386. REG_WRITE(hblank_reg, (adjusted_mode->crtc_hblank_start - 1) | ((adjusted_mode->crtc_hblank_end - 1) << 16));
  387. REG_WRITE(hsync_reg, (adjusted_mode->crtc_hsync_start - 1) | ((adjusted_mode->crtc_hsync_end - 1) << 16));
  388. REG_WRITE(vtot_reg, (adjusted_mode->crtc_vdisplay - 1) | ((adjusted_mode->crtc_vtotal - 1) << 16));
  389. REG_WRITE(vblank_reg, (adjusted_mode->crtc_vblank_start - 1) | ((adjusted_mode->crtc_vblank_end - 1) << 16));
  390. REG_WRITE(vsync_reg, (adjusted_mode->crtc_vsync_start - 1) | ((adjusted_mode->crtc_vsync_end - 1) << 16));
  391. REG_WRITE(pipesrc_reg,
  392. ((mode->crtc_hdisplay - 1) << 16) | (mode->crtc_vdisplay - 1));
  393. REG_WRITE(PCH_HTOTAL_B, (adjusted_mode->crtc_hdisplay - 1) | ((adjusted_mode->crtc_htotal - 1) << 16));
  394. REG_WRITE(PCH_HBLANK_B, (adjusted_mode->crtc_hblank_start - 1) | ((adjusted_mode->crtc_hblank_end - 1) << 16));
  395. REG_WRITE(PCH_HSYNC_B, (adjusted_mode->crtc_hsync_start - 1) | ((adjusted_mode->crtc_hsync_end - 1) << 16));
  396. REG_WRITE(PCH_VTOTAL_B, (adjusted_mode->crtc_vdisplay - 1) | ((adjusted_mode->crtc_vtotal - 1) << 16));
  397. REG_WRITE(PCH_VBLANK_B, (adjusted_mode->crtc_vblank_start - 1) | ((adjusted_mode->crtc_vblank_end - 1) << 16));
  398. REG_WRITE(PCH_VSYNC_B, (adjusted_mode->crtc_vsync_start - 1) | ((adjusted_mode->crtc_vsync_end - 1) << 16));
  399. REG_WRITE(PCH_PIPEBSRC,
  400. ((mode->crtc_hdisplay - 1) << 16) | (mode->crtc_vdisplay - 1));
  401. temp = adjusted_mode->crtc_hblank_end - adjusted_mode->crtc_hblank_start;
  402. HDMI_WRITE(HDMI_HBLANK_A, ((adjusted_mode->crtc_hdisplay - 1) << 16) | temp);
  403. REG_WRITE(dspsize_reg,
  404. ((mode->vdisplay - 1) << 16) | (mode->hdisplay - 1));
  405. REG_WRITE(dsppos_reg, 0);
  406. /* Flush the plane changes */
  407. {
  408. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  409. crtc_funcs->mode_set_base(crtc, x, y, old_fb);
  410. }
  411. /* Set up the display plane register */
  412. dspcntr = REG_READ(dspcntr_reg);
  413. dspcntr |= DISPPLANE_GAMMA_ENABLE;
  414. dspcntr |= DISPPLANE_SEL_PIPE_B;
  415. dspcntr |= DISPLAY_PLANE_ENABLE;
  416. /* setup pipeconf */
  417. pipeconf = REG_READ(pipeconf_reg);
  418. pipeconf |= PIPEACONF_ENABLE;
  419. REG_WRITE(pipeconf_reg, pipeconf);
  420. REG_READ(pipeconf_reg);
  421. REG_WRITE(PCH_PIPEBCONF, pipeconf);
  422. REG_READ(PCH_PIPEBCONF);
  423. wait_for_vblank(dev);
  424. REG_WRITE(dspcntr_reg, dspcntr);
  425. wait_for_vblank(dev);
  426. return 0;
  427. }
  428. static int oaktrail_hdmi_mode_valid(struct drm_connector *connector,
  429. struct drm_display_mode *mode)
  430. {
  431. struct drm_psb_private *dev_priv = connector->dev->dev_private;
  432. if (mode->clock > 165000)
  433. return MODE_CLOCK_HIGH;
  434. if (mode->clock < 20000)
  435. return MODE_CLOCK_LOW;
  436. if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
  437. return MODE_NO_DBLESCAN;
  438. /* We assume worst case scenario of 32 bpp here, since we don't know */
  439. if ((ALIGN(mode->hdisplay * 4, 64) * mode->vdisplay) >
  440. dev_priv->vram_stolen_size)
  441. return MODE_MEM;
  442. return MODE_OK;
  443. }
  444. static bool oaktrail_hdmi_mode_fixup(struct drm_encoder *encoder,
  445. struct drm_display_mode *mode,
  446. struct drm_display_mode *adjusted_mode)
  447. {
  448. return true;
  449. }
  450. static enum drm_connector_status
  451. oaktrail_hdmi_detect(struct drm_connector *connector, bool force)
  452. {
  453. enum drm_connector_status status;
  454. struct drm_device *dev = connector->dev;
  455. struct drm_psb_private *dev_priv = dev->dev_private;
  456. struct oaktrail_hdmi_dev *hdmi_dev = dev_priv->hdmi_priv;
  457. u32 temp;
  458. temp = HDMI_READ(HDMI_HSR);
  459. DRM_DEBUG_KMS("HDMI_HSR %x\n", temp);
  460. if ((temp & HDMI_DETECT_HDP) != 0)
  461. status = connector_status_connected;
  462. else
  463. status = connector_status_disconnected;
  464. return status;
  465. }
  466. static const unsigned char raw_edid[] = {
  467. 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x10, 0xac, 0x2f, 0xa0,
  468. 0x53, 0x55, 0x33, 0x30, 0x16, 0x13, 0x01, 0x03, 0x0e, 0x3a, 0x24, 0x78,
  469. 0xea, 0xe9, 0xf5, 0xac, 0x51, 0x30, 0xb4, 0x25, 0x11, 0x50, 0x54, 0xa5,
  470. 0x4b, 0x00, 0x81, 0x80, 0xa9, 0x40, 0x71, 0x4f, 0xb3, 0x00, 0x01, 0x01,
  471. 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x28, 0x3c, 0x80, 0xa0, 0x70, 0xb0,
  472. 0x23, 0x40, 0x30, 0x20, 0x36, 0x00, 0x46, 0x6c, 0x21, 0x00, 0x00, 0x1a,
  473. 0x00, 0x00, 0x00, 0xff, 0x00, 0x47, 0x4e, 0x37, 0x32, 0x31, 0x39, 0x35,
  474. 0x52, 0x30, 0x33, 0x55, 0x53, 0x0a, 0x00, 0x00, 0x00, 0xfc, 0x00, 0x44,
  475. 0x45, 0x4c, 0x4c, 0x20, 0x32, 0x37, 0x30, 0x39, 0x57, 0x0a, 0x20, 0x20,
  476. 0x00, 0x00, 0x00, 0xfd, 0x00, 0x38, 0x4c, 0x1e, 0x53, 0x11, 0x00, 0x0a,
  477. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x8d
  478. };
  479. static int oaktrail_hdmi_get_modes(struct drm_connector *connector)
  480. {
  481. struct drm_device *dev = connector->dev;
  482. struct drm_psb_private *dev_priv = dev->dev_private;
  483. struct i2c_adapter *i2c_adap;
  484. struct edid *edid;
  485. struct drm_display_mode *mode, *t;
  486. int i = 0, ret = 0;
  487. i2c_adap = i2c_get_adapter(3);
  488. if (i2c_adap == NULL) {
  489. DRM_ERROR("No ddc adapter available!\n");
  490. edid = (struct edid *)raw_edid;
  491. } else {
  492. edid = (struct edid *)raw_edid;
  493. /* FIXME ? edid = drm_get_edid(connector, i2c_adap); */
  494. }
  495. if (edid) {
  496. drm_mode_connector_update_edid_property(connector, edid);
  497. ret = drm_add_edid_modes(connector, edid);
  498. connector->display_info.raw_edid = NULL;
  499. }
  500. /*
  501. * prune modes that require frame buffer bigger than stolen mem
  502. */
  503. list_for_each_entry_safe(mode, t, &connector->probed_modes, head) {
  504. if ((mode->hdisplay * mode->vdisplay * 4) >= dev_priv->vram_stolen_size) {
  505. i++;
  506. drm_mode_remove(connector, mode);
  507. }
  508. }
  509. return ret - i;
  510. }
  511. static void oaktrail_hdmi_mode_set(struct drm_encoder *encoder,
  512. struct drm_display_mode *mode,
  513. struct drm_display_mode *adjusted_mode)
  514. {
  515. struct drm_device *dev = encoder->dev;
  516. oaktrail_hdmi_audio_enable(dev);
  517. return;
  518. }
  519. static void oaktrail_hdmi_destroy(struct drm_connector *connector)
  520. {
  521. return;
  522. }
  523. static const struct drm_encoder_helper_funcs oaktrail_hdmi_helper_funcs = {
  524. .dpms = oaktrail_hdmi_dpms,
  525. .mode_fixup = oaktrail_hdmi_mode_fixup,
  526. .prepare = psb_intel_encoder_prepare,
  527. .mode_set = oaktrail_hdmi_mode_set,
  528. .commit = psb_intel_encoder_commit,
  529. };
  530. static const struct drm_connector_helper_funcs
  531. oaktrail_hdmi_connector_helper_funcs = {
  532. .get_modes = oaktrail_hdmi_get_modes,
  533. .mode_valid = oaktrail_hdmi_mode_valid,
  534. .best_encoder = psb_intel_best_encoder,
  535. };
  536. static const struct drm_connector_funcs oaktrail_hdmi_connector_funcs = {
  537. .dpms = drm_helper_connector_dpms,
  538. .detect = oaktrail_hdmi_detect,
  539. .fill_modes = drm_helper_probe_single_connector_modes,
  540. .destroy = oaktrail_hdmi_destroy,
  541. };
  542. static void oaktrail_hdmi_enc_destroy(struct drm_encoder *encoder)
  543. {
  544. drm_encoder_cleanup(encoder);
  545. }
  546. static const struct drm_encoder_funcs oaktrail_hdmi_enc_funcs = {
  547. .destroy = oaktrail_hdmi_enc_destroy,
  548. };
  549. void oaktrail_hdmi_init(struct drm_device *dev,
  550. struct psb_intel_mode_device *mode_dev)
  551. {
  552. struct psb_intel_encoder *psb_intel_encoder;
  553. struct psb_intel_connector *psb_intel_connector;
  554. struct drm_connector *connector;
  555. struct drm_encoder *encoder;
  556. psb_intel_encoder = kzalloc(sizeof(struct psb_intel_encoder), GFP_KERNEL);
  557. if (!psb_intel_encoder)
  558. return;
  559. psb_intel_connector = kzalloc(sizeof(struct psb_intel_connector), GFP_KERNEL);
  560. if (!psb_intel_connector)
  561. goto failed_connector;
  562. connector = &psb_intel_connector->base;
  563. encoder = &psb_intel_encoder->base;
  564. drm_connector_init(dev, connector,
  565. &oaktrail_hdmi_connector_funcs,
  566. DRM_MODE_CONNECTOR_DVID);
  567. drm_encoder_init(dev, encoder,
  568. &oaktrail_hdmi_enc_funcs,
  569. DRM_MODE_ENCODER_TMDS);
  570. psb_intel_connector_attach_encoder(psb_intel_connector,
  571. psb_intel_encoder);
  572. psb_intel_encoder->type = INTEL_OUTPUT_HDMI;
  573. drm_encoder_helper_add(encoder, &oaktrail_hdmi_helper_funcs);
  574. drm_connector_helper_add(connector, &oaktrail_hdmi_connector_helper_funcs);
  575. connector->display_info.subpixel_order = SubPixelHorizontalRGB;
  576. connector->interlace_allowed = false;
  577. connector->doublescan_allowed = false;
  578. drm_sysfs_connector_add(connector);
  579. return;
  580. failed_connector:
  581. kfree(psb_intel_encoder);
  582. }
  583. static DEFINE_PCI_DEVICE_TABLE(hdmi_ids) = {
  584. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x080d) },
  585. {}
  586. };
  587. void oaktrail_hdmi_setup(struct drm_device *dev)
  588. {
  589. struct drm_psb_private *dev_priv = dev->dev_private;
  590. struct pci_dev *pdev;
  591. struct oaktrail_hdmi_dev *hdmi_dev;
  592. int ret;
  593. pdev = pci_get_device(PCI_VENDOR_ID_INTEL, 0x080d, NULL);
  594. if (!pdev)
  595. return;
  596. hdmi_dev = kzalloc(sizeof(struct oaktrail_hdmi_dev), GFP_KERNEL);
  597. if (!hdmi_dev) {
  598. dev_err(dev->dev, "failed to allocate memory\n");
  599. goto out;
  600. }
  601. ret = pci_enable_device(pdev);
  602. if (ret) {
  603. dev_err(dev->dev, "failed to enable hdmi controller\n");
  604. goto free;
  605. }
  606. hdmi_dev->mmio = pci_resource_start(pdev, 0);
  607. hdmi_dev->mmio_len = pci_resource_len(pdev, 0);
  608. hdmi_dev->regs = ioremap(hdmi_dev->mmio, hdmi_dev->mmio_len);
  609. if (!hdmi_dev->regs) {
  610. dev_err(dev->dev, "failed to map hdmi mmio\n");
  611. goto free;
  612. }
  613. hdmi_dev->dev = pdev;
  614. pci_set_drvdata(pdev, hdmi_dev);
  615. /* Initialize i2c controller */
  616. ret = oaktrail_hdmi_i2c_init(hdmi_dev->dev);
  617. if (ret)
  618. dev_err(dev->dev, "HDMI I2C initialization failed\n");
  619. dev_priv->hdmi_priv = hdmi_dev;
  620. oaktrail_hdmi_audio_disable(dev);
  621. return;
  622. free:
  623. kfree(hdmi_dev);
  624. out:
  625. return;
  626. }
  627. void oaktrail_hdmi_teardown(struct drm_device *dev)
  628. {
  629. struct drm_psb_private *dev_priv = dev->dev_private;
  630. struct oaktrail_hdmi_dev *hdmi_dev = dev_priv->hdmi_priv;
  631. struct pci_dev *pdev;
  632. if (hdmi_dev) {
  633. pdev = hdmi_dev->dev;
  634. pci_set_drvdata(pdev, NULL);
  635. oaktrail_hdmi_i2c_exit(pdev);
  636. iounmap(hdmi_dev->regs);
  637. kfree(hdmi_dev);
  638. pci_dev_put(pdev);
  639. }
  640. }
  641. /* save HDMI register state */
  642. void oaktrail_hdmi_save(struct drm_device *dev)
  643. {
  644. struct drm_psb_private *dev_priv = dev->dev_private;
  645. struct oaktrail_hdmi_dev *hdmi_dev = dev_priv->hdmi_priv;
  646. int i;
  647. /* dpll */
  648. hdmi_dev->saveDPLL_CTRL = PSB_RVDC32(DPLL_CTRL);
  649. hdmi_dev->saveDPLL_DIV_CTRL = PSB_RVDC32(DPLL_DIV_CTRL);
  650. hdmi_dev->saveDPLL_ADJUST = PSB_RVDC32(DPLL_ADJUST);
  651. hdmi_dev->saveDPLL_UPDATE = PSB_RVDC32(DPLL_UPDATE);
  652. hdmi_dev->saveDPLL_CLK_ENABLE = PSB_RVDC32(DPLL_CLK_ENABLE);
  653. /* pipe B */
  654. dev_priv->savePIPEBCONF = PSB_RVDC32(PIPEBCONF);
  655. dev_priv->savePIPEBSRC = PSB_RVDC32(PIPEBSRC);
  656. dev_priv->saveHTOTAL_B = PSB_RVDC32(HTOTAL_B);
  657. dev_priv->saveHBLANK_B = PSB_RVDC32(HBLANK_B);
  658. dev_priv->saveHSYNC_B = PSB_RVDC32(HSYNC_B);
  659. dev_priv->saveVTOTAL_B = PSB_RVDC32(VTOTAL_B);
  660. dev_priv->saveVBLANK_B = PSB_RVDC32(VBLANK_B);
  661. dev_priv->saveVSYNC_B = PSB_RVDC32(VSYNC_B);
  662. hdmi_dev->savePCH_PIPEBCONF = PSB_RVDC32(PCH_PIPEBCONF);
  663. hdmi_dev->savePCH_PIPEBSRC = PSB_RVDC32(PCH_PIPEBSRC);
  664. hdmi_dev->savePCH_HTOTAL_B = PSB_RVDC32(PCH_HTOTAL_B);
  665. hdmi_dev->savePCH_HBLANK_B = PSB_RVDC32(PCH_HBLANK_B);
  666. hdmi_dev->savePCH_HSYNC_B = PSB_RVDC32(PCH_HSYNC_B);
  667. hdmi_dev->savePCH_VTOTAL_B = PSB_RVDC32(PCH_VTOTAL_B);
  668. hdmi_dev->savePCH_VBLANK_B = PSB_RVDC32(PCH_VBLANK_B);
  669. hdmi_dev->savePCH_VSYNC_B = PSB_RVDC32(PCH_VSYNC_B);
  670. /* plane */
  671. dev_priv->saveDSPBCNTR = PSB_RVDC32(DSPBCNTR);
  672. dev_priv->saveDSPBSTRIDE = PSB_RVDC32(DSPBSTRIDE);
  673. dev_priv->saveDSPBADDR = PSB_RVDC32(DSPBBASE);
  674. dev_priv->saveDSPBSURF = PSB_RVDC32(DSPBSURF);
  675. dev_priv->saveDSPBLINOFF = PSB_RVDC32(DSPBLINOFF);
  676. dev_priv->saveDSPBTILEOFF = PSB_RVDC32(DSPBTILEOFF);
  677. /* cursor B */
  678. dev_priv->saveDSPBCURSOR_CTRL = PSB_RVDC32(CURBCNTR);
  679. dev_priv->saveDSPBCURSOR_BASE = PSB_RVDC32(CURBBASE);
  680. dev_priv->saveDSPBCURSOR_POS = PSB_RVDC32(CURBPOS);
  681. /* save palette */
  682. for (i = 0; i < 256; i++)
  683. dev_priv->save_palette_b[i] = PSB_RVDC32(PALETTE_B + (i << 2));
  684. }
  685. /* restore HDMI register state */
  686. void oaktrail_hdmi_restore(struct drm_device *dev)
  687. {
  688. struct drm_psb_private *dev_priv = dev->dev_private;
  689. struct oaktrail_hdmi_dev *hdmi_dev = dev_priv->hdmi_priv;
  690. int i;
  691. /* dpll */
  692. PSB_WVDC32(hdmi_dev->saveDPLL_CTRL, DPLL_CTRL);
  693. PSB_WVDC32(hdmi_dev->saveDPLL_DIV_CTRL, DPLL_DIV_CTRL);
  694. PSB_WVDC32(hdmi_dev->saveDPLL_ADJUST, DPLL_ADJUST);
  695. PSB_WVDC32(hdmi_dev->saveDPLL_UPDATE, DPLL_UPDATE);
  696. PSB_WVDC32(hdmi_dev->saveDPLL_CLK_ENABLE, DPLL_CLK_ENABLE);
  697. DRM_UDELAY(150);
  698. /* pipe */
  699. PSB_WVDC32(dev_priv->savePIPEBSRC, PIPEBSRC);
  700. PSB_WVDC32(dev_priv->saveHTOTAL_B, HTOTAL_B);
  701. PSB_WVDC32(dev_priv->saveHBLANK_B, HBLANK_B);
  702. PSB_WVDC32(dev_priv->saveHSYNC_B, HSYNC_B);
  703. PSB_WVDC32(dev_priv->saveVTOTAL_B, VTOTAL_B);
  704. PSB_WVDC32(dev_priv->saveVBLANK_B, VBLANK_B);
  705. PSB_WVDC32(dev_priv->saveVSYNC_B, VSYNC_B);
  706. PSB_WVDC32(hdmi_dev->savePCH_PIPEBSRC, PCH_PIPEBSRC);
  707. PSB_WVDC32(hdmi_dev->savePCH_HTOTAL_B, PCH_HTOTAL_B);
  708. PSB_WVDC32(hdmi_dev->savePCH_HBLANK_B, PCH_HBLANK_B);
  709. PSB_WVDC32(hdmi_dev->savePCH_HSYNC_B, PCH_HSYNC_B);
  710. PSB_WVDC32(hdmi_dev->savePCH_VTOTAL_B, PCH_VTOTAL_B);
  711. PSB_WVDC32(hdmi_dev->savePCH_VBLANK_B, PCH_VBLANK_B);
  712. PSB_WVDC32(hdmi_dev->savePCH_VSYNC_B, PCH_VSYNC_B);
  713. PSB_WVDC32(dev_priv->savePIPEBCONF, PIPEBCONF);
  714. PSB_WVDC32(hdmi_dev->savePCH_PIPEBCONF, PCH_PIPEBCONF);
  715. /* plane */
  716. PSB_WVDC32(dev_priv->saveDSPBLINOFF, DSPBLINOFF);
  717. PSB_WVDC32(dev_priv->saveDSPBSTRIDE, DSPBSTRIDE);
  718. PSB_WVDC32(dev_priv->saveDSPBTILEOFF, DSPBTILEOFF);
  719. PSB_WVDC32(dev_priv->saveDSPBCNTR, DSPBCNTR);
  720. PSB_WVDC32(dev_priv->saveDSPBSURF, DSPBSURF);
  721. /* cursor B */
  722. PSB_WVDC32(dev_priv->saveDSPBCURSOR_CTRL, CURBCNTR);
  723. PSB_WVDC32(dev_priv->saveDSPBCURSOR_POS, CURBPOS);
  724. PSB_WVDC32(dev_priv->saveDSPBCURSOR_BASE, CURBBASE);
  725. /* restore palette */
  726. for (i = 0; i < 256; i++)
  727. PSB_WVDC32(dev_priv->save_palette_b[i], PALETTE_B + (i << 2));
  728. }