ssd2828.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) 2015 Siarhei Siamashka <siarhei.siamashka@gmail.com>
  4. */
  5. /*
  6. * Support for the SSD2828 bridge chip, which can take pixel data coming
  7. * from a parallel LCD interface and translate it on the flight into MIPI DSI
  8. * interface for driving a MIPI compatible TFT display.
  9. */
  10. #include <common.h>
  11. #include <mipi_display.h>
  12. #include <asm/arch/gpio.h>
  13. #include <asm/gpio.h>
  14. #include "videomodes.h"
  15. #include "ssd2828.h"
  16. #define SSD2828_DIR 0xB0
  17. #define SSD2828_VICR1 0xB1
  18. #define SSD2828_VICR2 0xB2
  19. #define SSD2828_VICR3 0xB3
  20. #define SSD2828_VICR4 0xB4
  21. #define SSD2828_VICR5 0xB5
  22. #define SSD2828_VICR6 0xB6
  23. #define SSD2828_CFGR 0xB7
  24. #define SSD2828_VCR 0xB8
  25. #define SSD2828_PCR 0xB9
  26. #define SSD2828_PLCR 0xBA
  27. #define SSD2828_CCR 0xBB
  28. #define SSD2828_PSCR1 0xBC
  29. #define SSD2828_PSCR2 0xBD
  30. #define SSD2828_PSCR3 0xBE
  31. #define SSD2828_PDR 0xBF
  32. #define SSD2828_OCR 0xC0
  33. #define SSD2828_MRSR 0xC1
  34. #define SSD2828_RDCR 0xC2
  35. #define SSD2828_ARSR 0xC3
  36. #define SSD2828_LCR 0xC4
  37. #define SSD2828_ICR 0xC5
  38. #define SSD2828_ISR 0xC6
  39. #define SSD2828_ESR 0xC7
  40. #define SSD2828_DAR1 0xC9
  41. #define SSD2828_DAR2 0xCA
  42. #define SSD2828_DAR3 0xCB
  43. #define SSD2828_DAR4 0xCC
  44. #define SSD2828_DAR5 0xCD
  45. #define SSD2828_DAR6 0xCE
  46. #define SSD2828_HTTR1 0xCF
  47. #define SSD2828_HTTR2 0xD0
  48. #define SSD2828_LRTR1 0xD1
  49. #define SSD2828_LRTR2 0xD2
  50. #define SSD2828_TSR 0xD3
  51. #define SSD2828_LRR 0xD4
  52. #define SSD2828_PLLR 0xD5
  53. #define SSD2828_TR 0xD6
  54. #define SSD2828_TECR 0xD7
  55. #define SSD2828_ACR1 0xD8
  56. #define SSD2828_ACR2 0xD9
  57. #define SSD2828_ACR3 0xDA
  58. #define SSD2828_ACR4 0xDB
  59. #define SSD2828_IOCR 0xDC
  60. #define SSD2828_VICR7 0xDD
  61. #define SSD2828_LCFR 0xDE
  62. #define SSD2828_DAR7 0xDF
  63. #define SSD2828_PUCR1 0xE0
  64. #define SSD2828_PUCR2 0xE1
  65. #define SSD2828_PUCR3 0xE2
  66. #define SSD2828_CBCR1 0xE9
  67. #define SSD2828_CBCR2 0xEA
  68. #define SSD2828_CBSR 0xEB
  69. #define SSD2828_ECR 0xEC
  70. #define SSD2828_VSDR 0xED
  71. #define SSD2828_TMR 0xEE
  72. #define SSD2828_GPIO1 0xEF
  73. #define SSD2828_GPIO2 0xF0
  74. #define SSD2828_DLYA01 0xF1
  75. #define SSD2828_DLYA23 0xF2
  76. #define SSD2828_DLYB01 0xF3
  77. #define SSD2828_DLYB23 0xF4
  78. #define SSD2828_DLYC01 0xF5
  79. #define SSD2828_DLYC23 0xF6
  80. #define SSD2828_ACR5 0xF7
  81. #define SSD2828_RR 0xFF
  82. #define SSD2828_CFGR_HS (1 << 0)
  83. #define SSD2828_CFGR_CKE (1 << 1)
  84. #define SSD2828_CFGR_SLP (1 << 2)
  85. #define SSD2828_CFGR_VEN (1 << 3)
  86. #define SSD2828_CFGR_HCLK (1 << 4)
  87. #define SSD2828_CFGR_CSS (1 << 5)
  88. #define SSD2828_CFGR_DCS (1 << 6)
  89. #define SSD2828_CFGR_REN (1 << 7)
  90. #define SSD2828_CFGR_ECD (1 << 8)
  91. #define SSD2828_CFGR_EOT (1 << 9)
  92. #define SSD2828_CFGR_LPE (1 << 10)
  93. #define SSD2828_CFGR_TXD (1 << 11)
  94. #define SSD2828_VIDEO_MODE_NON_BURST_WITH_SYNC_PULSES (0 << 2)
  95. #define SSD2828_VIDEO_MODE_NON_BURST_WITH_SYNC_EVENTS (1 << 2)
  96. #define SSD2828_VIDEO_MODE_BURST (2 << 2)
  97. #define SSD2828_VIDEO_PIXEL_FORMAT_16BPP 0
  98. #define SSD2828_VIDEO_PIXEL_FORMAT_18BPP_PACKED 1
  99. #define SSD2828_VIDEO_PIXEL_FORMAT_18BPP_LOOSELY_PACKED 2
  100. #define SSD2828_VIDEO_PIXEL_FORMAT_24BPP 3
  101. #define SSD2828_LP_CLOCK_DIVIDER(n) (((n) - 1) & 0x3F)
  102. /*
  103. * SPI transfer, using the "24-bit 3 wire" mode (that's how it is called in
  104. * the SSD2828 documentation). The 'dout' input parameter specifies 24-bits
  105. * of data to be written to SSD2828. Returns the lowest 16-bits of data,
  106. * that is received back.
  107. */
  108. static u32 soft_spi_xfer_24bit_3wire(const struct ssd2828_config *drv, u32 dout)
  109. {
  110. int j, bitlen = 24;
  111. u32 tmpdin = 0;
  112. /*
  113. * According to the "24 Bit 3 Wire SPI Interface Timing Characteristics"
  114. * and "TX_CLK Timing Characteristics" tables in the SSD2828 datasheet,
  115. * the lowest possible 'tx_clk' clock frequency is 8MHz, and SPI runs
  116. * at 1/8 of that after reset. So using 1 microsecond delays is safe in
  117. * the main loop. But the delays around chip select pin manipulations
  118. * need to be longer (up to 16 'tx_clk' cycles, or 2 microseconds in
  119. * the worst case).
  120. */
  121. const int spi_delay_us = 1;
  122. const int spi_cs_delay_us = 2;
  123. gpio_set_value(drv->csx_pin, 0);
  124. udelay(spi_cs_delay_us);
  125. for (j = bitlen - 1; j >= 0; j--) {
  126. gpio_set_value(drv->sck_pin, 0);
  127. gpio_set_value(drv->sdi_pin, (dout & (1 << j)) != 0);
  128. udelay(spi_delay_us);
  129. if (drv->sdo_pin != -1)
  130. tmpdin = (tmpdin << 1) | gpio_get_value(drv->sdo_pin);
  131. gpio_set_value(drv->sck_pin, 1);
  132. udelay(spi_delay_us);
  133. }
  134. udelay(spi_cs_delay_us);
  135. gpio_set_value(drv->csx_pin, 1);
  136. udelay(spi_cs_delay_us);
  137. return tmpdin & 0xFFFF;
  138. }
  139. /*
  140. * Read from a SSD2828 hardware register (regnum >= 0xB0)
  141. */
  142. static u32 read_hw_register(const struct ssd2828_config *cfg, u8 regnum)
  143. {
  144. soft_spi_xfer_24bit_3wire(cfg, 0x700000 | regnum);
  145. return soft_spi_xfer_24bit_3wire(cfg, 0x730000);
  146. }
  147. /*
  148. * Write to a SSD2828 hardware register (regnum >= 0xB0)
  149. */
  150. static void write_hw_register(const struct ssd2828_config *cfg, u8 regnum,
  151. u16 val)
  152. {
  153. soft_spi_xfer_24bit_3wire(cfg, 0x700000 | regnum);
  154. soft_spi_xfer_24bit_3wire(cfg, 0x720000 | val);
  155. }
  156. /*
  157. * Send MIPI command to the LCD panel (cmdnum < 0xB0)
  158. */
  159. static void send_mipi_dcs_command(const struct ssd2828_config *cfg, u8 cmdnum)
  160. {
  161. /* Set packet size to 1 (a single command with no parameters) */
  162. write_hw_register(cfg, SSD2828_PSCR1, 1);
  163. /* Send the command */
  164. write_hw_register(cfg, SSD2828_PDR, cmdnum);
  165. }
  166. /*
  167. * Reset SSD2828
  168. */
  169. static void ssd2828_reset(const struct ssd2828_config *cfg)
  170. {
  171. /* RESET needs 10 milliseconds according to the datasheet */
  172. gpio_set_value(cfg->reset_pin, 0);
  173. mdelay(10);
  174. gpio_set_value(cfg->reset_pin, 1);
  175. mdelay(10);
  176. }
  177. static int ssd2828_enable_gpio(const struct ssd2828_config *cfg)
  178. {
  179. if (gpio_request(cfg->csx_pin, "ssd2828_csx")) {
  180. printf("SSD2828: request for 'ssd2828_csx' pin failed\n");
  181. return 1;
  182. }
  183. if (gpio_request(cfg->sck_pin, "ssd2828_sck")) {
  184. gpio_free(cfg->csx_pin);
  185. printf("SSD2828: request for 'ssd2828_sck' pin failed\n");
  186. return 1;
  187. }
  188. if (gpio_request(cfg->sdi_pin, "ssd2828_sdi")) {
  189. gpio_free(cfg->csx_pin);
  190. gpio_free(cfg->sck_pin);
  191. printf("SSD2828: request for 'ssd2828_sdi' pin failed\n");
  192. return 1;
  193. }
  194. if (gpio_request(cfg->reset_pin, "ssd2828_reset")) {
  195. gpio_free(cfg->csx_pin);
  196. gpio_free(cfg->sck_pin);
  197. gpio_free(cfg->sdi_pin);
  198. printf("SSD2828: request for 'ssd2828_reset' pin failed\n");
  199. return 1;
  200. }
  201. if (cfg->sdo_pin != -1 && gpio_request(cfg->sdo_pin, "ssd2828_sdo")) {
  202. gpio_free(cfg->csx_pin);
  203. gpio_free(cfg->sck_pin);
  204. gpio_free(cfg->sdi_pin);
  205. gpio_free(cfg->reset_pin);
  206. printf("SSD2828: request for 'ssd2828_sdo' pin failed\n");
  207. return 1;
  208. }
  209. gpio_direction_output(cfg->reset_pin, 0);
  210. gpio_direction_output(cfg->csx_pin, 1);
  211. gpio_direction_output(cfg->sck_pin, 1);
  212. gpio_direction_output(cfg->sdi_pin, 1);
  213. if (cfg->sdo_pin != -1)
  214. gpio_direction_input(cfg->sdo_pin);
  215. return 0;
  216. }
  217. static int ssd2828_free_gpio(const struct ssd2828_config *cfg)
  218. {
  219. gpio_free(cfg->csx_pin);
  220. gpio_free(cfg->sck_pin);
  221. gpio_free(cfg->sdi_pin);
  222. gpio_free(cfg->reset_pin);
  223. if (cfg->sdo_pin != -1)
  224. gpio_free(cfg->sdo_pin);
  225. return 1;
  226. }
  227. /*
  228. * PLL configuration register settings.
  229. *
  230. * See the "PLL Configuration Register Description" in the SSD2828 datasheet.
  231. */
  232. static u32 construct_pll_config(u32 desired_pll_freq_kbps,
  233. u32 reference_freq_khz)
  234. {
  235. u32 div_factor = 1, mul_factor, fr = 0;
  236. u32 output_freq_kbps;
  237. /* The intermediate clock after division can't be less than 5MHz */
  238. while (reference_freq_khz / (div_factor + 1) >= 5000)
  239. div_factor++;
  240. if (div_factor > 31)
  241. div_factor = 31;
  242. mul_factor = DIV_ROUND_UP(desired_pll_freq_kbps * div_factor,
  243. reference_freq_khz);
  244. output_freq_kbps = reference_freq_khz * mul_factor / div_factor;
  245. if (output_freq_kbps >= 501000)
  246. fr = 3;
  247. else if (output_freq_kbps >= 251000)
  248. fr = 2;
  249. else if (output_freq_kbps >= 126000)
  250. fr = 1;
  251. return (fr << 14) | (div_factor << 8) | mul_factor;
  252. }
  253. static u32 decode_pll_config(u32 pll_config, u32 reference_freq_khz)
  254. {
  255. u32 mul_factor = pll_config & 0xFF;
  256. u32 div_factor = (pll_config >> 8) & 0x1F;
  257. if (mul_factor == 0)
  258. mul_factor = 1;
  259. if (div_factor == 0)
  260. div_factor = 1;
  261. return reference_freq_khz * mul_factor / div_factor;
  262. }
  263. static int ssd2828_configure_video_interface(const struct ssd2828_config *cfg,
  264. const struct ctfb_res_modes *mode)
  265. {
  266. u32 val;
  267. /* RGB Interface Control Register 1 */
  268. write_hw_register(cfg, SSD2828_VICR1, (mode->vsync_len << 8) |
  269. (mode->hsync_len));
  270. /* RGB Interface Control Register 2 */
  271. u32 vbp = mode->vsync_len + mode->upper_margin;
  272. u32 hbp = mode->hsync_len + mode->left_margin;
  273. write_hw_register(cfg, SSD2828_VICR2, (vbp << 8) | hbp);
  274. /* RGB Interface Control Register 3 */
  275. write_hw_register(cfg, SSD2828_VICR3, (mode->lower_margin << 8) |
  276. (mode->right_margin));
  277. /* RGB Interface Control Register 4 */
  278. write_hw_register(cfg, SSD2828_VICR4, mode->xres);
  279. /* RGB Interface Control Register 5 */
  280. write_hw_register(cfg, SSD2828_VICR5, mode->yres);
  281. /* RGB Interface Control Register 6 */
  282. val = SSD2828_VIDEO_MODE_BURST;
  283. switch (cfg->ssd2828_color_depth) {
  284. case 16:
  285. val |= SSD2828_VIDEO_PIXEL_FORMAT_16BPP;
  286. break;
  287. case 18:
  288. val |= cfg->mipi_dsi_loosely_packed_pixel_format ?
  289. SSD2828_VIDEO_PIXEL_FORMAT_18BPP_LOOSELY_PACKED :
  290. SSD2828_VIDEO_PIXEL_FORMAT_18BPP_PACKED;
  291. break;
  292. case 24:
  293. val |= SSD2828_VIDEO_PIXEL_FORMAT_24BPP;
  294. break;
  295. default:
  296. printf("SSD2828: unsupported color depth\n");
  297. return 1;
  298. }
  299. write_hw_register(cfg, SSD2828_VICR6, val);
  300. /* Lane Configuration Register */
  301. write_hw_register(cfg, SSD2828_LCFR,
  302. cfg->mipi_dsi_number_of_data_lanes - 1);
  303. return 0;
  304. }
  305. int ssd2828_init(const struct ssd2828_config *cfg,
  306. const struct ctfb_res_modes *mode)
  307. {
  308. u32 lp_div, pll_freq_kbps, reference_freq_khz, pll_config;
  309. /* The LP clock speed is limited by 10MHz */
  310. const u32 mipi_dsi_low_power_clk_khz = 10000;
  311. /*
  312. * This is just the reset default value of CFGR register (0x301).
  313. * Because we are not always able to read back from SPI, have
  314. * it initialized here.
  315. */
  316. u32 cfgr_reg = SSD2828_CFGR_EOT | /* EOT Packet Enable */
  317. SSD2828_CFGR_ECD | /* Disable ECC and CRC */
  318. SSD2828_CFGR_HS; /* Data lanes are in HS mode */
  319. /* Initialize the pins */
  320. if (ssd2828_enable_gpio(cfg) != 0)
  321. return 1;
  322. /* Reset the chip */
  323. ssd2828_reset(cfg);
  324. /*
  325. * If there is a pin to read data back from SPI, then we are lucky. Try
  326. * to check if SPI is configured correctly and SSD2828 is actually able
  327. * to talk back.
  328. */
  329. if (cfg->sdo_pin != -1) {
  330. if (read_hw_register(cfg, SSD2828_DIR) != 0x2828 ||
  331. read_hw_register(cfg, SSD2828_CFGR) != cfgr_reg) {
  332. printf("SSD2828: SPI communication failed.\n");
  333. ssd2828_free_gpio(cfg);
  334. return 1;
  335. }
  336. }
  337. /*
  338. * Pick the reference clock for PLL. If we know the exact 'tx_clk'
  339. * clock speed, then everything is good. If not, then we can fallback
  340. * to 'pclk' (pixel clock from the parallel LCD interface). In the
  341. * case of using this fallback, it is necessary to have parallel LCD
  342. * already initialized and running at this point.
  343. */
  344. reference_freq_khz = cfg->ssd2828_tx_clk_khz;
  345. if (reference_freq_khz == 0) {
  346. reference_freq_khz = mode->pixclock_khz;
  347. /* Use 'pclk' as the reference clock for PLL */
  348. cfgr_reg |= SSD2828_CFGR_CSS;
  349. }
  350. /*
  351. * Setup the parallel LCD timings in the appropriate registers.
  352. */
  353. if (ssd2828_configure_video_interface(cfg, mode) != 0) {
  354. ssd2828_free_gpio(cfg);
  355. return 1;
  356. }
  357. /* Configuration Register */
  358. cfgr_reg &= ~SSD2828_CFGR_HS; /* Data lanes are in LP mode */
  359. cfgr_reg |= SSD2828_CFGR_CKE; /* Clock lane is in HS mode */
  360. cfgr_reg |= SSD2828_CFGR_DCS; /* Only use DCS packets */
  361. write_hw_register(cfg, SSD2828_CFGR, cfgr_reg);
  362. /* PLL Configuration Register */
  363. pll_config = construct_pll_config(
  364. cfg->mipi_dsi_bitrate_per_data_lane_mbps * 1000,
  365. reference_freq_khz);
  366. write_hw_register(cfg, SSD2828_PLCR, pll_config);
  367. pll_freq_kbps = decode_pll_config(pll_config, reference_freq_khz);
  368. lp_div = DIV_ROUND_UP(pll_freq_kbps, mipi_dsi_low_power_clk_khz * 8);
  369. /* VC Control Register */
  370. write_hw_register(cfg, SSD2828_VCR, 0);
  371. /* Clock Control Register */
  372. write_hw_register(cfg, SSD2828_CCR, SSD2828_LP_CLOCK_DIVIDER(lp_div));
  373. /* PLL Control Register */
  374. write_hw_register(cfg, SSD2828_PCR, 1); /* Enable PLL */
  375. /* Wait for PLL lock */
  376. udelay(500);
  377. send_mipi_dcs_command(cfg, MIPI_DCS_EXIT_SLEEP_MODE);
  378. mdelay(cfg->mipi_dsi_delay_after_exit_sleep_mode_ms);
  379. send_mipi_dcs_command(cfg, MIPI_DCS_SET_DISPLAY_ON);
  380. mdelay(cfg->mipi_dsi_delay_after_set_display_on_ms);
  381. cfgr_reg |= SSD2828_CFGR_HS; /* Enable HS mode for data lanes */
  382. cfgr_reg |= SSD2828_CFGR_VEN; /* Enable video pipeline */
  383. write_hw_register(cfg, SSD2828_CFGR, cfgr_reg);
  384. return 0;
  385. }