video.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /*
  2. * Novena video output support
  3. *
  4. * IT6251 code based on code Copyright (C) 2014 Sean Cross
  5. * from https://github.com/xobs/novena-linux.git commit
  6. * 3d85836ee1377d445531928361809612aa0a18db
  7. *
  8. * Copyright (C) 2014 Marek Vasut <marex@denx.de>
  9. *
  10. * SPDX-License-Identifier: GPL-2.0+
  11. */
  12. #include <common.h>
  13. #include <linux/errno.h>
  14. #include <asm/gpio.h>
  15. #include <asm/io.h>
  16. #include <asm/arch/clock.h>
  17. #include <asm/arch/crm_regs.h>
  18. #include <asm/arch/imx-regs.h>
  19. #include <asm/arch/iomux.h>
  20. #include <asm/arch/mxc_hdmi.h>
  21. #include <asm/arch/sys_proto.h>
  22. #include <asm/imx-common/iomux-v3.h>
  23. #include <asm/imx-common/mxc_i2c.h>
  24. #include <asm/imx-common/video.h>
  25. #include <i2c.h>
  26. #include <input.h>
  27. #include <ipu_pixfmt.h>
  28. #include <linux/fb.h>
  29. #include <linux/input.h>
  30. #include <malloc.h>
  31. #include <stdio_dev.h>
  32. #include "novena.h"
  33. #define IT6251_VENDOR_ID_LOW 0x00
  34. #define IT6251_VENDOR_ID_HIGH 0x01
  35. #define IT6251_DEVICE_ID_LOW 0x02
  36. #define IT6251_DEVICE_ID_HIGH 0x03
  37. #define IT6251_SYSTEM_STATUS 0x0d
  38. #define IT6251_SYSTEM_STATUS_RINTSTATUS (1 << 0)
  39. #define IT6251_SYSTEM_STATUS_RHPDSTATUS (1 << 1)
  40. #define IT6251_SYSTEM_STATUS_RVIDEOSTABLE (1 << 2)
  41. #define IT6251_SYSTEM_STATUS_RPLL_IOLOCK (1 << 3)
  42. #define IT6251_SYSTEM_STATUS_RPLL_XPLOCK (1 << 4)
  43. #define IT6251_SYSTEM_STATUS_RPLL_SPLOCK (1 << 5)
  44. #define IT6251_SYSTEM_STATUS_RAUXFREQ_LOCK (1 << 6)
  45. #define IT6251_REF_STATE 0x0e
  46. #define IT6251_REF_STATE_MAIN_LINK_DISABLED (1 << 0)
  47. #define IT6251_REF_STATE_AUX_CHANNEL_READ (1 << 1)
  48. #define IT6251_REF_STATE_CR_PATTERN (1 << 2)
  49. #define IT6251_REF_STATE_EQ_PATTERN (1 << 3)
  50. #define IT6251_REF_STATE_NORMAL_OPERATION (1 << 4)
  51. #define IT6251_REF_STATE_MUTED (1 << 5)
  52. #define IT6251_REG_PCLK_CNT_LOW 0x57
  53. #define IT6251_REG_PCLK_CNT_HIGH 0x58
  54. #define IT6521_RETRY_MAX 20
  55. static int it6251_is_stable(void)
  56. {
  57. const unsigned int caddr = NOVENA_IT6251_CHIPADDR;
  58. const unsigned int laddr = NOVENA_IT6251_LVDSADDR;
  59. int status;
  60. int clkcnt;
  61. int rpclkcnt;
  62. int refstate;
  63. rpclkcnt = (i2c_reg_read(caddr, 0x13) & 0xff) |
  64. ((i2c_reg_read(caddr, 0x14) << 8) & 0x0f00);
  65. debug("RPCLKCnt: %d\n", rpclkcnt);
  66. status = i2c_reg_read(caddr, IT6251_SYSTEM_STATUS);
  67. debug("System status: 0x%02x\n", status);
  68. clkcnt = (i2c_reg_read(laddr, IT6251_REG_PCLK_CNT_LOW) & 0xff) |
  69. ((i2c_reg_read(laddr, IT6251_REG_PCLK_CNT_HIGH) << 8) &
  70. 0x0f00);
  71. debug("Clock: 0x%02x\n", clkcnt);
  72. refstate = i2c_reg_read(laddr, IT6251_REF_STATE);
  73. debug("Ref Link State: 0x%02x\n", refstate);
  74. if ((refstate & 0x1f) != 0)
  75. return 0;
  76. /* If video is muted, that's a failure */
  77. if (refstate & IT6251_REF_STATE_MUTED)
  78. return 0;
  79. if (!(status & IT6251_SYSTEM_STATUS_RVIDEOSTABLE))
  80. return 0;
  81. return 1;
  82. }
  83. static int it6251_ready(void)
  84. {
  85. const unsigned int caddr = NOVENA_IT6251_CHIPADDR;
  86. /* Test if the IT6251 came out of reset by reading ID regs. */
  87. if (i2c_reg_read(caddr, IT6251_VENDOR_ID_LOW) != 0x15)
  88. return 0;
  89. if (i2c_reg_read(caddr, IT6251_VENDOR_ID_HIGH) != 0xca)
  90. return 0;
  91. if (i2c_reg_read(caddr, IT6251_DEVICE_ID_LOW) != 0x51)
  92. return 0;
  93. if (i2c_reg_read(caddr, IT6251_DEVICE_ID_HIGH) != 0x62)
  94. return 0;
  95. return 1;
  96. }
  97. static void it6251_program_regs(void)
  98. {
  99. const unsigned int caddr = NOVENA_IT6251_CHIPADDR;
  100. const unsigned int laddr = NOVENA_IT6251_LVDSADDR;
  101. i2c_reg_write(caddr, 0x05, 0x00);
  102. mdelay(1);
  103. /* set LVDSRX address, and enable */
  104. i2c_reg_write(caddr, 0xfd, 0xbc);
  105. i2c_reg_write(caddr, 0xfe, 0x01);
  106. /*
  107. * LVDSRX
  108. */
  109. /* This write always fails, because the chip goes into reset */
  110. /* reset LVDSRX */
  111. i2c_reg_write(laddr, 0x05, 0xff);
  112. i2c_reg_write(laddr, 0x05, 0x00);
  113. /* reset LVDSRX PLL */
  114. i2c_reg_write(laddr, 0x3b, 0x42);
  115. i2c_reg_write(laddr, 0x3b, 0x43);
  116. /* something with SSC PLL */
  117. i2c_reg_write(laddr, 0x3c, 0x08);
  118. /* don't swap links, but writing reserved registers */
  119. i2c_reg_write(laddr, 0x0b, 0x88);
  120. /* JEIDA, 8-bit depth 0x11, orig 0x42 */
  121. i2c_reg_write(laddr, 0x2c, 0x01);
  122. /* "reserved" */
  123. i2c_reg_write(laddr, 0x32, 0x04);
  124. /* "reserved" */
  125. i2c_reg_write(laddr, 0x35, 0xe0);
  126. /* "reserved" + clock delay */
  127. i2c_reg_write(laddr, 0x2b, 0x24);
  128. /* reset LVDSRX pix clock */
  129. i2c_reg_write(laddr, 0x05, 0x02);
  130. i2c_reg_write(laddr, 0x05, 0x00);
  131. /*
  132. * DPTX
  133. */
  134. /* set for two lane mode, normal op, no swapping, no downspread */
  135. i2c_reg_write(caddr, 0x16, 0x02);
  136. /* some AUX channel EDID magic */
  137. i2c_reg_write(caddr, 0x23, 0x40);
  138. /* power down lanes 3-0 */
  139. i2c_reg_write(caddr, 0x5c, 0xf3);
  140. /* enable DP scrambling, change EQ CR phase */
  141. i2c_reg_write(caddr, 0x5f, 0x06);
  142. /* color mode RGB, pclk/2 */
  143. i2c_reg_write(caddr, 0x60, 0x02);
  144. /* dual pixel input mode, no EO swap, no RGB swap */
  145. i2c_reg_write(caddr, 0x61, 0x04);
  146. /* M444B24 video format */
  147. i2c_reg_write(caddr, 0x62, 0x01);
  148. /* vesa range / not interlace / vsync high / hsync high */
  149. i2c_reg_write(caddr, 0xa0, 0x0F);
  150. /* hpd event timer set to 1.6-ish ms */
  151. i2c_reg_write(caddr, 0xc9, 0xf5);
  152. /* more reserved magic */
  153. i2c_reg_write(caddr, 0xca, 0x4d);
  154. i2c_reg_write(caddr, 0xcb, 0x37);
  155. /* enhanced framing mode, auto video fifo reset, video mute disable */
  156. i2c_reg_write(caddr, 0xd3, 0x03);
  157. /* "vidstmp" and some reserved stuff */
  158. i2c_reg_write(caddr, 0xd4, 0x45);
  159. /* queue number -- reserved */
  160. i2c_reg_write(caddr, 0xe7, 0xa0);
  161. /* info frame packets and reserved */
  162. i2c_reg_write(caddr, 0xe8, 0x33);
  163. /* more AVI stuff */
  164. i2c_reg_write(caddr, 0xec, 0x00);
  165. /* select PC master reg for aux channel? */
  166. i2c_reg_write(caddr, 0x23, 0x42);
  167. /* send PC request commands */
  168. i2c_reg_write(caddr, 0x24, 0x00);
  169. i2c_reg_write(caddr, 0x25, 0x00);
  170. i2c_reg_write(caddr, 0x26, 0x00);
  171. /* native aux read */
  172. i2c_reg_write(caddr, 0x2b, 0x00);
  173. /* back to internal */
  174. i2c_reg_write(caddr, 0x23, 0x40);
  175. /* voltage swing level 3 */
  176. i2c_reg_write(caddr, 0x19, 0xff);
  177. /* pre-emphasis level 3 */
  178. i2c_reg_write(caddr, 0x1a, 0xff);
  179. /* start link training */
  180. i2c_reg_write(caddr, 0x17, 0x01);
  181. }
  182. static int it6251_init(void)
  183. {
  184. const unsigned int caddr = NOVENA_IT6251_CHIPADDR;
  185. int reg;
  186. int tries, retries = 0;
  187. for (retries = 0; retries < IT6521_RETRY_MAX; retries++) {
  188. /* Program the chip. */
  189. it6251_program_regs();
  190. /* Wait for video stable. */
  191. for (tries = 0; tries < 100; tries++) {
  192. reg = i2c_reg_read(caddr, 0x17);
  193. /* Test Link CFG, STS, LCS read done. */
  194. if ((reg & 0xe0) != 0xe0) {
  195. /* Not yet, wait a bit more. */
  196. mdelay(2);
  197. continue;
  198. }
  199. /* Test if the video input is stable. */
  200. if (it6251_is_stable())
  201. return 0;
  202. }
  203. /*
  204. * If we couldn't stabilize, requeue and try again,
  205. * because it means that the LVDS channel isn't
  206. * stable yet.
  207. */
  208. printf("Display didn't stabilize.\n");
  209. printf("This may be because the LVDS port is still in powersave mode.\n");
  210. mdelay(50);
  211. }
  212. return -EINVAL;
  213. }
  214. static void enable_hdmi(struct display_info_t const *dev)
  215. {
  216. imx_enable_hdmi_phy();
  217. }
  218. static int lvds_enabled;
  219. static void enable_lvds(struct display_info_t const *dev)
  220. {
  221. if (lvds_enabled)
  222. return;
  223. /* ITE IT6251 power enable. */
  224. gpio_direction_output(NOVENA_ITE6251_PWR_GPIO, 0);
  225. mdelay(10);
  226. gpio_direction_output(NOVENA_ITE6251_PWR_GPIO, 1);
  227. mdelay(20);
  228. lvds_enabled = 1;
  229. }
  230. static int detect_lvds(struct display_info_t const *dev)
  231. {
  232. int ret, loops = 250;
  233. enable_lvds(dev);
  234. ret = i2c_set_bus_num(NOVENA_IT6251_I2C_BUS);
  235. if (ret) {
  236. puts("Cannot select IT6251 I2C bus.\n");
  237. return 0;
  238. }
  239. /* Wait up-to ~250 mS for the LVDS to come up. */
  240. while (--loops) {
  241. ret = it6251_ready();
  242. if (ret)
  243. return ret;
  244. mdelay(1);
  245. }
  246. return 0;
  247. }
  248. struct display_info_t const displays[] = {
  249. {
  250. /* HDMI Output */
  251. .bus = -1,
  252. .addr = 0,
  253. .pixfmt = IPU_PIX_FMT_RGB24,
  254. .detect = detect_hdmi,
  255. .enable = enable_hdmi,
  256. .mode = {
  257. .name = "HDMI",
  258. .refresh = 60,
  259. .xres = 1024,
  260. .yres = 768,
  261. .pixclock = 15384,
  262. .left_margin = 220,
  263. .right_margin = 40,
  264. .upper_margin = 21,
  265. .lower_margin = 7,
  266. .hsync_len = 60,
  267. .vsync_len = 10,
  268. .sync = FB_SYNC_EXT,
  269. .vmode = FB_VMODE_NONINTERLACED
  270. },
  271. }, {
  272. /* LVDS Output: N133HSE-EA1 Rev. C1 */
  273. .bus = -1,
  274. .pixfmt = IPU_PIX_FMT_RGB24,
  275. .detect = detect_lvds,
  276. .enable = enable_lvds,
  277. .mode = {
  278. .name = "Chimei-FHD",
  279. .refresh = 60,
  280. .xres = 1920,
  281. .yres = 1080,
  282. .pixclock = 15384,
  283. .left_margin = 148,
  284. .right_margin = 88,
  285. .upper_margin = 36,
  286. .lower_margin = 4,
  287. .hsync_len = 44,
  288. .vsync_len = 5,
  289. .sync = FB_SYNC_HOR_HIGH_ACT |
  290. FB_SYNC_VERT_HIGH_ACT |
  291. FB_SYNC_EXT,
  292. .vmode = FB_VMODE_NONINTERLACED,
  293. },
  294. },
  295. };
  296. size_t display_count = ARRAY_SIZE(displays);
  297. static void enable_vpll(void)
  298. {
  299. struct mxc_ccm_reg *ccm = (struct mxc_ccm_reg *)CCM_BASE_ADDR;
  300. int timeout = 100000;
  301. setbits_le32(&ccm->analog_pll_video, BM_ANADIG_PLL_VIDEO_POWERDOWN);
  302. clrsetbits_le32(&ccm->analog_pll_video,
  303. BM_ANADIG_PLL_VIDEO_DIV_SELECT |
  304. BM_ANADIG_PLL_VIDEO_POST_DIV_SELECT,
  305. BF_ANADIG_PLL_VIDEO_DIV_SELECT(37) |
  306. BF_ANADIG_PLL_VIDEO_POST_DIV_SELECT(1));
  307. writel(BF_ANADIG_PLL_VIDEO_NUM_A(11), &ccm->analog_pll_video_num);
  308. writel(BF_ANADIG_PLL_VIDEO_DENOM_B(12), &ccm->analog_pll_video_denom);
  309. clrbits_le32(&ccm->analog_pll_video, BM_ANADIG_PLL_VIDEO_POWERDOWN);
  310. while (timeout--)
  311. if (readl(&ccm->analog_pll_video) & BM_ANADIG_PLL_VIDEO_LOCK)
  312. break;
  313. if (timeout < 0)
  314. printf("Warning: video pll lock timeout!\n");
  315. clrsetbits_le32(&ccm->analog_pll_video,
  316. BM_ANADIG_PLL_VIDEO_BYPASS,
  317. BM_ANADIG_PLL_VIDEO_ENABLE);
  318. }
  319. void setup_display_clock(void)
  320. {
  321. struct mxc_ccm_reg *mxc_ccm = (struct mxc_ccm_reg *)CCM_BASE_ADDR;
  322. struct iomuxc *iomux = (struct iomuxc *)IOMUXC_BASE_ADDR;
  323. enable_ipu_clock();
  324. enable_vpll();
  325. imx_setup_hdmi();
  326. /* Turn on IPU LDB DI0 clocks */
  327. setbits_le32(&mxc_ccm->CCGR3, MXC_CCM_CCGR3_LDB_DI0_MASK);
  328. /* Switch LDB DI0 to PLL5 (Video PLL) */
  329. clrsetbits_le32(&mxc_ccm->cs2cdr,
  330. MXC_CCM_CS2CDR_LDB_DI0_CLK_SEL_MASK,
  331. (0 << MXC_CCM_CS2CDR_LDB_DI0_CLK_SEL_OFFSET));
  332. /* LDB clock div by 3.5 */
  333. clrbits_le32(&mxc_ccm->cscmr2, MXC_CCM_CSCMR2_LDB_DI0_IPU_DIV);
  334. /* DI0 clock derived from ldb_di0_clk */
  335. clrsetbits_le32(&mxc_ccm->chsccdr,
  336. MXC_CCM_CHSCCDR_IPU1_DI0_CLK_SEL_MASK,
  337. (CHSCCDR_CLK_SEL_LDB_DI0 <<
  338. MXC_CCM_CHSCCDR_IPU1_DI0_CLK_SEL_OFFSET)
  339. );
  340. /* Enable both LVDS channels, both connected to DI0. */
  341. writel(IOMUXC_GPR2_DI0_VS_POLARITY_ACTIVE_HIGH |
  342. IOMUXC_GPR2_BIT_MAPPING_CH1_JEIDA |
  343. IOMUXC_GPR2_DATA_WIDTH_CH1_24BIT |
  344. IOMUXC_GPR2_BIT_MAPPING_CH0_JEIDA |
  345. IOMUXC_GPR2_DATA_WIDTH_CH0_24BIT |
  346. IOMUXC_GPR2_SPLIT_MODE_EN_MASK |
  347. IOMUXC_GPR2_LVDS_CH1_MODE_ENABLED_DI0 |
  348. IOMUXC_GPR2_LVDS_CH0_MODE_ENABLED_DI0,
  349. &iomux->gpr[2]);
  350. clrsetbits_le32(&iomux->gpr[3],
  351. IOMUXC_GPR3_LVDS0_MUX_CTL_MASK |
  352. IOMUXC_GPR3_LVDS1_MUX_CTL_MASK,
  353. (IOMUXC_GPR3_MUX_SRC_IPU1_DI0 <<
  354. IOMUXC_GPR3_LVDS0_MUX_CTL_OFFSET) |
  355. (IOMUXC_GPR3_MUX_SRC_IPU1_DI0 <<
  356. IOMUXC_GPR3_LVDS1_MUX_CTL_OFFSET)
  357. );
  358. }
  359. void setup_display_lvds(void)
  360. {
  361. int ret;
  362. ret = i2c_set_bus_num(NOVENA_IT6251_I2C_BUS);
  363. if (ret) {
  364. puts("Cannot select LVDS-to-eDP I2C bus.\n");
  365. return;
  366. }
  367. /* The IT6251 should be ready now, if it's not, it's not connected. */
  368. ret = it6251_ready();
  369. if (!ret)
  370. return;
  371. /* Init the LVDS-to-eDP chip and if it succeeded, enable backlight. */
  372. ret = it6251_init();
  373. if (!ret) {
  374. /* Backlight power enable. */
  375. gpio_direction_output(NOVENA_BACKLIGHT_PWR_GPIO, 1);
  376. /* PWM backlight pin, always on for full brightness. */
  377. gpio_direction_output(NOVENA_BACKLIGHT_PWM_GPIO, 1);
  378. }
  379. }