exynos_mipi_dsi_common.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  1. /* linux/drivers/video/exynos/exynos_mipi_dsi_common.c
  2. *
  3. * Samsung SoC MIPI-DSI common driver.
  4. *
  5. * Copyright (c) 2012 Samsung Electronics Co., Ltd
  6. *
  7. * InKi Dae, <inki.dae@samsung.com>
  8. * Donghwa Lee, <dh09.lee@samsung.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #include <linux/module.h>
  15. #include <linux/kernel.h>
  16. #include <linux/errno.h>
  17. #include <linux/mutex.h>
  18. #include <linux/wait.h>
  19. #include <linux/fs.h>
  20. #include <linux/mm.h>
  21. #include <linux/fb.h>
  22. #include <linux/ctype.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/io.h>
  25. #include <linux/memory.h>
  26. #include <linux/delay.h>
  27. #include <linux/irqreturn.h>
  28. #include <linux/kthread.h>
  29. #include <video/mipi_display.h>
  30. #include <video/exynos_mipi_dsim.h>
  31. #include "exynos_mipi_dsi_regs.h"
  32. #include "exynos_mipi_dsi_lowlevel.h"
  33. #include "exynos_mipi_dsi_common.h"
  34. #define MIPI_FIFO_TIMEOUT msecs_to_jiffies(250)
  35. #define MIPI_RX_FIFO_READ_DONE 0x30800002
  36. #define MIPI_MAX_RX_FIFO 20
  37. #define MHZ (1000 * 1000)
  38. #define FIN_HZ (24 * MHZ)
  39. #define DFIN_PLL_MIN_HZ (6 * MHZ)
  40. #define DFIN_PLL_MAX_HZ (12 * MHZ)
  41. #define DFVCO_MIN_HZ (500 * MHZ)
  42. #define DFVCO_MAX_HZ (1000 * MHZ)
  43. #define TRY_GET_FIFO_TIMEOUT (5000 * 2)
  44. #define TRY_FIFO_CLEAR (10)
  45. /* MIPI-DSIM status types. */
  46. enum {
  47. DSIM_STATE_INIT, /* should be initialized. */
  48. DSIM_STATE_STOP, /* CPU and LCDC are LP mode. */
  49. DSIM_STATE_HSCLKEN, /* HS clock was enabled. */
  50. DSIM_STATE_ULPS
  51. };
  52. /* define DSI lane types. */
  53. enum {
  54. DSIM_LANE_CLOCK = (1 << 0),
  55. DSIM_LANE_DATA0 = (1 << 1),
  56. DSIM_LANE_DATA1 = (1 << 2),
  57. DSIM_LANE_DATA2 = (1 << 3),
  58. DSIM_LANE_DATA3 = (1 << 4)
  59. };
  60. static unsigned int dpll_table[15] = {
  61. 100, 120, 170, 220, 270,
  62. 320, 390, 450, 510, 560,
  63. 640, 690, 770, 870, 950
  64. };
  65. irqreturn_t exynos_mipi_dsi_interrupt_handler(int irq, void *dev_id)
  66. {
  67. struct mipi_dsim_device *dsim = dev_id;
  68. unsigned int intsrc, intmsk;
  69. intsrc = exynos_mipi_dsi_read_interrupt(dsim);
  70. intmsk = exynos_mipi_dsi_read_interrupt_mask(dsim);
  71. intmsk = ~intmsk & intsrc;
  72. if (intsrc & INTMSK_RX_DONE) {
  73. complete(&dsim_rd_comp);
  74. dev_dbg(dsim->dev, "MIPI INTMSK_RX_DONE\n");
  75. }
  76. if (intsrc & INTMSK_FIFO_EMPTY) {
  77. complete(&dsim_wr_comp);
  78. dev_dbg(dsim->dev, "MIPI INTMSK_FIFO_EMPTY\n");
  79. }
  80. exynos_mipi_dsi_clear_interrupt(dsim, intmsk);
  81. return IRQ_HANDLED;
  82. }
  83. /*
  84. * write long packet to mipi dsi slave
  85. * @dsim: mipi dsim device structure.
  86. * @data0: packet data to send.
  87. * @data1: size of packet data
  88. */
  89. static void exynos_mipi_dsi_long_data_wr(struct mipi_dsim_device *dsim,
  90. const unsigned char *data0, unsigned int data_size)
  91. {
  92. unsigned int data_cnt = 0, payload = 0;
  93. /* in case that data count is more then 4 */
  94. for (data_cnt = 0; data_cnt < data_size; data_cnt += 4) {
  95. /*
  96. * after sending 4bytes per one time,
  97. * send remainder data less then 4.
  98. */
  99. if ((data_size - data_cnt) < 4) {
  100. if ((data_size - data_cnt) == 3) {
  101. payload = data0[data_cnt] |
  102. data0[data_cnt + 1] << 8 |
  103. data0[data_cnt + 2] << 16;
  104. dev_dbg(dsim->dev, "count = 3 payload = %x, %x %x %x\n",
  105. payload, data0[data_cnt],
  106. data0[data_cnt + 1],
  107. data0[data_cnt + 2]);
  108. } else if ((data_size - data_cnt) == 2) {
  109. payload = data0[data_cnt] |
  110. data0[data_cnt + 1] << 8;
  111. dev_dbg(dsim->dev,
  112. "count = 2 payload = %x, %x %x\n", payload,
  113. data0[data_cnt],
  114. data0[data_cnt + 1]);
  115. } else if ((data_size - data_cnt) == 1) {
  116. payload = data0[data_cnt];
  117. }
  118. exynos_mipi_dsi_wr_tx_data(dsim, payload);
  119. /* send 4bytes per one time. */
  120. } else {
  121. payload = data0[data_cnt] |
  122. data0[data_cnt + 1] << 8 |
  123. data0[data_cnt + 2] << 16 |
  124. data0[data_cnt + 3] << 24;
  125. dev_dbg(dsim->dev,
  126. "count = 4 payload = %x, %x %x %x %x\n",
  127. payload, *(u8 *)(data0 + data_cnt),
  128. data0[data_cnt + 1],
  129. data0[data_cnt + 2],
  130. data0[data_cnt + 3]);
  131. exynos_mipi_dsi_wr_tx_data(dsim, payload);
  132. }
  133. }
  134. }
  135. int exynos_mipi_dsi_wr_data(struct mipi_dsim_device *dsim, unsigned int data_id,
  136. const unsigned char *data0, unsigned int data_size)
  137. {
  138. unsigned int check_rx_ack = 0;
  139. if (dsim->state == DSIM_STATE_ULPS) {
  140. dev_err(dsim->dev, "state is ULPS.\n");
  141. return -EINVAL;
  142. }
  143. /* FIXME!!! why does it need this delay? */
  144. msleep(20);
  145. mutex_lock(&dsim->lock);
  146. switch (data_id) {
  147. /* short packet types of packet types for command. */
  148. case MIPI_DSI_GENERIC_SHORT_WRITE_0_PARAM:
  149. case MIPI_DSI_GENERIC_SHORT_WRITE_1_PARAM:
  150. case MIPI_DSI_GENERIC_SHORT_WRITE_2_PARAM:
  151. case MIPI_DSI_DCS_SHORT_WRITE:
  152. case MIPI_DSI_DCS_SHORT_WRITE_PARAM:
  153. case MIPI_DSI_SET_MAXIMUM_RETURN_PACKET_SIZE:
  154. exynos_mipi_dsi_wr_tx_header(dsim, data_id, data0[0], data0[1]);
  155. if (check_rx_ack) {
  156. /* process response func should be implemented */
  157. mutex_unlock(&dsim->lock);
  158. return 0;
  159. } else {
  160. mutex_unlock(&dsim->lock);
  161. return -EINVAL;
  162. }
  163. /* general command */
  164. case MIPI_DSI_COLOR_MODE_OFF:
  165. case MIPI_DSI_COLOR_MODE_ON:
  166. case MIPI_DSI_SHUTDOWN_PERIPHERAL:
  167. case MIPI_DSI_TURN_ON_PERIPHERAL:
  168. exynos_mipi_dsi_wr_tx_header(dsim, data_id, data0[0], data0[1]);
  169. if (check_rx_ack) {
  170. /* process response func should be implemented. */
  171. mutex_unlock(&dsim->lock);
  172. return 0;
  173. } else {
  174. mutex_unlock(&dsim->lock);
  175. return -EINVAL;
  176. }
  177. /* packet types for video data */
  178. case MIPI_DSI_V_SYNC_START:
  179. case MIPI_DSI_V_SYNC_END:
  180. case MIPI_DSI_H_SYNC_START:
  181. case MIPI_DSI_H_SYNC_END:
  182. case MIPI_DSI_END_OF_TRANSMISSION:
  183. mutex_unlock(&dsim->lock);
  184. return 0;
  185. /* long packet type and null packet */
  186. case MIPI_DSI_NULL_PACKET:
  187. case MIPI_DSI_BLANKING_PACKET:
  188. mutex_unlock(&dsim->lock);
  189. return 0;
  190. case MIPI_DSI_GENERIC_LONG_WRITE:
  191. case MIPI_DSI_DCS_LONG_WRITE:
  192. {
  193. unsigned int size, payload = 0;
  194. reinit_completion(&dsim_wr_comp);
  195. size = data_size * 4;
  196. /* if data count is less then 4, then send 3bytes data. */
  197. if (data_size < 4) {
  198. payload = data0[0] |
  199. data0[1] << 8 |
  200. data0[2] << 16;
  201. exynos_mipi_dsi_wr_tx_data(dsim, payload);
  202. dev_dbg(dsim->dev, "count = %d payload = %x,%x %x %x\n",
  203. data_size, payload, data0[0],
  204. data0[1], data0[2]);
  205. /* in case that data count is more then 4 */
  206. } else
  207. exynos_mipi_dsi_long_data_wr(dsim, data0, data_size);
  208. /* put data into header fifo */
  209. exynos_mipi_dsi_wr_tx_header(dsim, data_id, data_size & 0xff,
  210. (data_size & 0xff00) >> 8);
  211. if (!wait_for_completion_interruptible_timeout(&dsim_wr_comp,
  212. MIPI_FIFO_TIMEOUT)) {
  213. dev_warn(dsim->dev, "command write timeout.\n");
  214. mutex_unlock(&dsim->lock);
  215. return -EAGAIN;
  216. }
  217. if (check_rx_ack) {
  218. /* process response func should be implemented. */
  219. mutex_unlock(&dsim->lock);
  220. return 0;
  221. } else {
  222. mutex_unlock(&dsim->lock);
  223. return -EINVAL;
  224. }
  225. }
  226. /* packet typo for video data */
  227. case MIPI_DSI_PACKED_PIXEL_STREAM_16:
  228. case MIPI_DSI_PACKED_PIXEL_STREAM_18:
  229. case MIPI_DSI_PIXEL_STREAM_3BYTE_18:
  230. case MIPI_DSI_PACKED_PIXEL_STREAM_24:
  231. if (check_rx_ack) {
  232. /* process response func should be implemented. */
  233. mutex_unlock(&dsim->lock);
  234. return 0;
  235. } else {
  236. mutex_unlock(&dsim->lock);
  237. return -EINVAL;
  238. }
  239. default:
  240. dev_warn(dsim->dev,
  241. "data id %x is not supported current DSI spec.\n",
  242. data_id);
  243. mutex_unlock(&dsim->lock);
  244. return -EINVAL;
  245. }
  246. }
  247. static unsigned int exynos_mipi_dsi_long_data_rd(struct mipi_dsim_device *dsim,
  248. unsigned int req_size, unsigned int rx_data, u8 *rx_buf)
  249. {
  250. unsigned int rcv_pkt, i, j;
  251. u16 rxsize;
  252. /* for long packet */
  253. rxsize = (u16)((rx_data & 0x00ffff00) >> 8);
  254. dev_dbg(dsim->dev, "mipi dsi rx size : %d\n", rxsize);
  255. if (rxsize != req_size) {
  256. dev_dbg(dsim->dev,
  257. "received size mismatch received: %d, requested: %d\n",
  258. rxsize, req_size);
  259. goto err;
  260. }
  261. for (i = 0; i < (rxsize >> 2); i++) {
  262. rcv_pkt = exynos_mipi_dsi_rd_rx_fifo(dsim);
  263. dev_dbg(dsim->dev, "received pkt : %08x\n", rcv_pkt);
  264. for (j = 0; j < 4; j++) {
  265. rx_buf[(i * 4) + j] =
  266. (u8)(rcv_pkt >> (j * 8)) & 0xff;
  267. dev_dbg(dsim->dev, "received value : %02x\n",
  268. (rcv_pkt >> (j * 8)) & 0xff);
  269. }
  270. }
  271. if (rxsize % 4) {
  272. rcv_pkt = exynos_mipi_dsi_rd_rx_fifo(dsim);
  273. dev_dbg(dsim->dev, "received pkt : %08x\n", rcv_pkt);
  274. for (j = 0; j < (rxsize % 4); j++) {
  275. rx_buf[(i * 4) + j] =
  276. (u8)(rcv_pkt >> (j * 8)) & 0xff;
  277. dev_dbg(dsim->dev, "received value : %02x\n",
  278. (rcv_pkt >> (j * 8)) & 0xff);
  279. }
  280. }
  281. return rxsize;
  282. err:
  283. return -EINVAL;
  284. }
  285. static unsigned int exynos_mipi_dsi_response_size(unsigned int req_size)
  286. {
  287. switch (req_size) {
  288. case 1:
  289. return MIPI_DSI_RX_GENERIC_SHORT_READ_RESPONSE_1BYTE;
  290. case 2:
  291. return MIPI_DSI_RX_GENERIC_SHORT_READ_RESPONSE_2BYTE;
  292. default:
  293. return MIPI_DSI_RX_GENERIC_LONG_READ_RESPONSE;
  294. }
  295. }
  296. int exynos_mipi_dsi_rd_data(struct mipi_dsim_device *dsim, unsigned int data_id,
  297. unsigned int data0, unsigned int req_size, u8 *rx_buf)
  298. {
  299. unsigned int rx_data, rcv_pkt, i;
  300. u8 response = 0;
  301. u16 rxsize;
  302. if (dsim->state == DSIM_STATE_ULPS) {
  303. dev_err(dsim->dev, "state is ULPS.\n");
  304. return -EINVAL;
  305. }
  306. /* FIXME!!! */
  307. msleep(20);
  308. mutex_lock(&dsim->lock);
  309. reinit_completion(&dsim_rd_comp);
  310. exynos_mipi_dsi_rd_tx_header(dsim,
  311. MIPI_DSI_SET_MAXIMUM_RETURN_PACKET_SIZE, req_size);
  312. response = exynos_mipi_dsi_response_size(req_size);
  313. switch (data_id) {
  314. case MIPI_DSI_GENERIC_READ_REQUEST_0_PARAM:
  315. case MIPI_DSI_GENERIC_READ_REQUEST_1_PARAM:
  316. case MIPI_DSI_GENERIC_READ_REQUEST_2_PARAM:
  317. case MIPI_DSI_DCS_READ:
  318. exynos_mipi_dsi_rd_tx_header(dsim,
  319. data_id, data0);
  320. /* process response func should be implemented. */
  321. break;
  322. default:
  323. dev_warn(dsim->dev,
  324. "data id %x is not supported current DSI spec.\n",
  325. data_id);
  326. mutex_unlock(&dsim->lock);
  327. return -EINVAL;
  328. }
  329. if (!wait_for_completion_interruptible_timeout(&dsim_rd_comp,
  330. MIPI_FIFO_TIMEOUT)) {
  331. pr_err("RX done interrupt timeout\n");
  332. mutex_unlock(&dsim->lock);
  333. return 0;
  334. }
  335. msleep(20);
  336. rx_data = exynos_mipi_dsi_rd_rx_fifo(dsim);
  337. if ((u8)(rx_data & 0xff) != response) {
  338. printk(KERN_ERR
  339. "mipi dsi wrong response rx_data : %x, response:%x\n",
  340. rx_data, response);
  341. goto clear_rx_fifo;
  342. }
  343. if (req_size <= 2) {
  344. /* for short packet */
  345. for (i = 0; i < req_size; i++)
  346. rx_buf[i] = (rx_data >> (8 + (i * 8))) & 0xff;
  347. rxsize = req_size;
  348. } else {
  349. /* for long packet */
  350. rxsize = exynos_mipi_dsi_long_data_rd(dsim, req_size, rx_data,
  351. rx_buf);
  352. if (rxsize != req_size)
  353. goto clear_rx_fifo;
  354. }
  355. rcv_pkt = exynos_mipi_dsi_rd_rx_fifo(dsim);
  356. msleep(20);
  357. if (rcv_pkt != MIPI_RX_FIFO_READ_DONE) {
  358. dev_info(dsim->dev,
  359. "Can't found RX FIFO READ DONE FLAG : %x\n", rcv_pkt);
  360. goto clear_rx_fifo;
  361. }
  362. mutex_unlock(&dsim->lock);
  363. return rxsize;
  364. clear_rx_fifo:
  365. i = 0;
  366. while (1) {
  367. rcv_pkt = exynos_mipi_dsi_rd_rx_fifo(dsim);
  368. if ((rcv_pkt == MIPI_RX_FIFO_READ_DONE)
  369. || (i > MIPI_MAX_RX_FIFO))
  370. break;
  371. dev_dbg(dsim->dev,
  372. "mipi dsi clear rx fifo : %08x\n", rcv_pkt);
  373. i++;
  374. }
  375. dev_info(dsim->dev,
  376. "mipi dsi rx done count : %d, rcv_pkt : %08x\n", i, rcv_pkt);
  377. mutex_unlock(&dsim->lock);
  378. return 0;
  379. }
  380. static int exynos_mipi_dsi_pll_on(struct mipi_dsim_device *dsim,
  381. unsigned int enable)
  382. {
  383. int sw_timeout;
  384. if (enable) {
  385. sw_timeout = 1000;
  386. exynos_mipi_dsi_enable_pll(dsim, 1);
  387. while (1) {
  388. sw_timeout--;
  389. if (exynos_mipi_dsi_is_pll_stable(dsim))
  390. return 0;
  391. if (sw_timeout == 0)
  392. return -EINVAL;
  393. }
  394. } else
  395. exynos_mipi_dsi_enable_pll(dsim, 0);
  396. return 0;
  397. }
  398. static unsigned long exynos_mipi_dsi_change_pll(struct mipi_dsim_device *dsim,
  399. unsigned int pre_divider, unsigned int main_divider,
  400. unsigned int scaler)
  401. {
  402. unsigned long dfin_pll, dfvco, dpll_out;
  403. unsigned int i, freq_band = 0xf;
  404. dfin_pll = (FIN_HZ / pre_divider);
  405. /******************************************************
  406. * Serial Clock(=ByteClk X 8) FreqBand[3:0] *
  407. ******************************************************
  408. * ~ 99.99 MHz 0000
  409. * 100 ~ 119.99 MHz 0001
  410. * 120 ~ 159.99 MHz 0010
  411. * 160 ~ 199.99 MHz 0011
  412. * 200 ~ 239.99 MHz 0100
  413. * 140 ~ 319.99 MHz 0101
  414. * 320 ~ 389.99 MHz 0110
  415. * 390 ~ 449.99 MHz 0111
  416. * 450 ~ 509.99 MHz 1000
  417. * 510 ~ 559.99 MHz 1001
  418. * 560 ~ 639.99 MHz 1010
  419. * 640 ~ 689.99 MHz 1011
  420. * 690 ~ 769.99 MHz 1100
  421. * 770 ~ 869.99 MHz 1101
  422. * 870 ~ 949.99 MHz 1110
  423. * 950 ~ 1000 MHz 1111
  424. ******************************************************/
  425. if (dfin_pll < DFIN_PLL_MIN_HZ || dfin_pll > DFIN_PLL_MAX_HZ) {
  426. dev_warn(dsim->dev, "fin_pll range should be 6MHz ~ 12MHz\n");
  427. exynos_mipi_dsi_enable_afc(dsim, 0, 0);
  428. } else {
  429. if (dfin_pll < 7 * MHZ)
  430. exynos_mipi_dsi_enable_afc(dsim, 1, 0x1);
  431. else if (dfin_pll < 8 * MHZ)
  432. exynos_mipi_dsi_enable_afc(dsim, 1, 0x0);
  433. else if (dfin_pll < 9 * MHZ)
  434. exynos_mipi_dsi_enable_afc(dsim, 1, 0x3);
  435. else if (dfin_pll < 10 * MHZ)
  436. exynos_mipi_dsi_enable_afc(dsim, 1, 0x2);
  437. else if (dfin_pll < 11 * MHZ)
  438. exynos_mipi_dsi_enable_afc(dsim, 1, 0x5);
  439. else
  440. exynos_mipi_dsi_enable_afc(dsim, 1, 0x4);
  441. }
  442. dfvco = dfin_pll * main_divider;
  443. dev_dbg(dsim->dev, "dfvco = %lu, dfin_pll = %lu, main_divider = %d\n",
  444. dfvco, dfin_pll, main_divider);
  445. if (dfvco < DFVCO_MIN_HZ || dfvco > DFVCO_MAX_HZ)
  446. dev_warn(dsim->dev, "fvco range should be 500MHz ~ 1000MHz\n");
  447. dpll_out = dfvco / (1 << scaler);
  448. dev_dbg(dsim->dev, "dpll_out = %lu, dfvco = %lu, scaler = %d\n",
  449. dpll_out, dfvco, scaler);
  450. for (i = 0; i < ARRAY_SIZE(dpll_table); i++) {
  451. if (dpll_out < dpll_table[i] * MHZ) {
  452. freq_band = i;
  453. break;
  454. }
  455. }
  456. dev_dbg(dsim->dev, "freq_band = %d\n", freq_band);
  457. exynos_mipi_dsi_pll_freq(dsim, pre_divider, main_divider, scaler);
  458. exynos_mipi_dsi_hs_zero_ctrl(dsim, 0);
  459. exynos_mipi_dsi_prep_ctrl(dsim, 0);
  460. /* Freq Band */
  461. exynos_mipi_dsi_pll_freq_band(dsim, freq_band);
  462. /* Stable time */
  463. exynos_mipi_dsi_pll_stable_time(dsim, dsim->dsim_config->pll_stable_time);
  464. /* Enable PLL */
  465. dev_dbg(dsim->dev, "FOUT of mipi dphy pll is %luMHz\n",
  466. (dpll_out / MHZ));
  467. return dpll_out;
  468. }
  469. static int exynos_mipi_dsi_set_clock(struct mipi_dsim_device *dsim,
  470. unsigned int byte_clk_sel, unsigned int enable)
  471. {
  472. unsigned int esc_div;
  473. unsigned long esc_clk_error_rate;
  474. unsigned long hs_clk = 0, byte_clk = 0, escape_clk = 0;
  475. if (enable) {
  476. dsim->e_clk_src = byte_clk_sel;
  477. /* Escape mode clock and byte clock source */
  478. exynos_mipi_dsi_set_byte_clock_src(dsim, byte_clk_sel);
  479. /* DPHY, DSIM Link : D-PHY clock out */
  480. if (byte_clk_sel == DSIM_PLL_OUT_DIV8) {
  481. hs_clk = exynos_mipi_dsi_change_pll(dsim,
  482. dsim->dsim_config->p, dsim->dsim_config->m,
  483. dsim->dsim_config->s);
  484. if (hs_clk == 0) {
  485. dev_err(dsim->dev,
  486. "failed to get hs clock.\n");
  487. return -EINVAL;
  488. }
  489. byte_clk = hs_clk / 8;
  490. exynos_mipi_dsi_enable_pll_bypass(dsim, 0);
  491. exynos_mipi_dsi_pll_on(dsim, 1);
  492. /* DPHY : D-PHY clock out, DSIM link : external clock out */
  493. } else if (byte_clk_sel == DSIM_EXT_CLK_DIV8) {
  494. dev_warn(dsim->dev, "this project is not support\n");
  495. dev_warn(dsim->dev,
  496. "external clock source for MIPI DSIM.\n");
  497. } else if (byte_clk_sel == DSIM_EXT_CLK_BYPASS) {
  498. dev_warn(dsim->dev, "this project is not support\n");
  499. dev_warn(dsim->dev,
  500. "external clock source for MIPI DSIM\n");
  501. }
  502. /* escape clock divider */
  503. esc_div = byte_clk / (dsim->dsim_config->esc_clk);
  504. dev_dbg(dsim->dev,
  505. "esc_div = %d, byte_clk = %lu, esc_clk = %lu\n",
  506. esc_div, byte_clk, dsim->dsim_config->esc_clk);
  507. if ((byte_clk / esc_div) >= (20 * MHZ) ||
  508. (byte_clk / esc_div) >
  509. dsim->dsim_config->esc_clk)
  510. esc_div += 1;
  511. escape_clk = byte_clk / esc_div;
  512. dev_dbg(dsim->dev,
  513. "escape_clk = %lu, byte_clk = %lu, esc_div = %d\n",
  514. escape_clk, byte_clk, esc_div);
  515. /* enable escape clock. */
  516. exynos_mipi_dsi_enable_byte_clock(dsim, 1);
  517. /* enable byte clk and escape clock */
  518. exynos_mipi_dsi_set_esc_clk_prs(dsim, 1, esc_div);
  519. /* escape clock on lane */
  520. exynos_mipi_dsi_enable_esc_clk_on_lane(dsim,
  521. (DSIM_LANE_CLOCK | dsim->data_lane), 1);
  522. dev_dbg(dsim->dev, "byte clock is %luMHz\n",
  523. (byte_clk / MHZ));
  524. dev_dbg(dsim->dev, "escape clock that user's need is %lu\n",
  525. (dsim->dsim_config->esc_clk / MHZ));
  526. dev_dbg(dsim->dev, "escape clock divider is %x\n", esc_div);
  527. dev_dbg(dsim->dev, "escape clock is %luMHz\n",
  528. ((byte_clk / esc_div) / MHZ));
  529. if ((byte_clk / esc_div) > escape_clk) {
  530. esc_clk_error_rate = escape_clk /
  531. (byte_clk / esc_div);
  532. dev_warn(dsim->dev, "error rate is %lu over.\n",
  533. (esc_clk_error_rate / 100));
  534. } else if ((byte_clk / esc_div) < (escape_clk)) {
  535. esc_clk_error_rate = (byte_clk / esc_div) /
  536. escape_clk;
  537. dev_warn(dsim->dev, "error rate is %lu under.\n",
  538. (esc_clk_error_rate / 100));
  539. }
  540. } else {
  541. exynos_mipi_dsi_enable_esc_clk_on_lane(dsim,
  542. (DSIM_LANE_CLOCK | dsim->data_lane), 0);
  543. exynos_mipi_dsi_set_esc_clk_prs(dsim, 0, 0);
  544. /* disable escape clock. */
  545. exynos_mipi_dsi_enable_byte_clock(dsim, 0);
  546. if (byte_clk_sel == DSIM_PLL_OUT_DIV8)
  547. exynos_mipi_dsi_pll_on(dsim, 0);
  548. }
  549. return 0;
  550. }
  551. int exynos_mipi_dsi_init_dsim(struct mipi_dsim_device *dsim)
  552. {
  553. dsim->state = DSIM_STATE_INIT;
  554. switch (dsim->dsim_config->e_no_data_lane) {
  555. case DSIM_DATA_LANE_1:
  556. dsim->data_lane = DSIM_LANE_DATA0;
  557. break;
  558. case DSIM_DATA_LANE_2:
  559. dsim->data_lane = DSIM_LANE_DATA0 | DSIM_LANE_DATA1;
  560. break;
  561. case DSIM_DATA_LANE_3:
  562. dsim->data_lane = DSIM_LANE_DATA0 | DSIM_LANE_DATA1 |
  563. DSIM_LANE_DATA2;
  564. break;
  565. case DSIM_DATA_LANE_4:
  566. dsim->data_lane = DSIM_LANE_DATA0 | DSIM_LANE_DATA1 |
  567. DSIM_LANE_DATA2 | DSIM_LANE_DATA3;
  568. break;
  569. default:
  570. dev_info(dsim->dev, "data lane is invalid.\n");
  571. return -EINVAL;
  572. }
  573. exynos_mipi_dsi_sw_reset(dsim);
  574. exynos_mipi_dsi_func_reset(dsim);
  575. exynos_mipi_dsi_dp_dn_swap(dsim, 0);
  576. return 0;
  577. }
  578. void exynos_mipi_dsi_init_interrupt(struct mipi_dsim_device *dsim)
  579. {
  580. unsigned int src = 0;
  581. src = (INTSRC_SFR_FIFO_EMPTY | INTSRC_RX_DATA_DONE);
  582. exynos_mipi_dsi_set_interrupt(dsim, src, 1);
  583. src = 0;
  584. src = ~(INTMSK_RX_DONE | INTMSK_FIFO_EMPTY);
  585. exynos_mipi_dsi_set_interrupt_mask(dsim, src, 1);
  586. }
  587. int exynos_mipi_dsi_enable_frame_done_int(struct mipi_dsim_device *dsim,
  588. unsigned int enable)
  589. {
  590. /* enable only frame done interrupt */
  591. exynos_mipi_dsi_set_interrupt_mask(dsim, INTMSK_FRAME_DONE, enable);
  592. return 0;
  593. }
  594. void exynos_mipi_dsi_stand_by(struct mipi_dsim_device *dsim,
  595. unsigned int enable)
  596. {
  597. /* consider Main display and Sub display. */
  598. exynos_mipi_dsi_set_main_stand_by(dsim, enable);
  599. }
  600. int exynos_mipi_dsi_set_display_mode(struct mipi_dsim_device *dsim,
  601. struct mipi_dsim_config *dsim_config)
  602. {
  603. struct mipi_dsim_platform_data *dsim_pd;
  604. struct fb_videomode *timing;
  605. dsim_pd = (struct mipi_dsim_platform_data *)dsim->pd;
  606. timing = (struct fb_videomode *)dsim_pd->lcd_panel_info;
  607. /* in case of VIDEO MODE (RGB INTERFACE), it sets polarities. */
  608. if (dsim_config->e_interface == (u32) DSIM_VIDEO) {
  609. if (dsim_config->auto_vertical_cnt == 0) {
  610. exynos_mipi_dsi_set_main_disp_vporch(dsim,
  611. dsim_config->cmd_allow,
  612. timing->lower_margin,
  613. timing->upper_margin);
  614. exynos_mipi_dsi_set_main_disp_hporch(dsim,
  615. timing->right_margin,
  616. timing->left_margin);
  617. exynos_mipi_dsi_set_main_disp_sync_area(dsim,
  618. timing->vsync_len,
  619. timing->hsync_len);
  620. }
  621. }
  622. exynos_mipi_dsi_set_main_disp_resol(dsim, timing->xres,
  623. timing->yres);
  624. exynos_mipi_dsi_display_config(dsim, dsim_config);
  625. dev_info(dsim->dev, "lcd panel ==> width = %d, height = %d\n",
  626. timing->xres, timing->yres);
  627. return 0;
  628. }
  629. int exynos_mipi_dsi_init_link(struct mipi_dsim_device *dsim)
  630. {
  631. unsigned int time_out = 100;
  632. switch (dsim->state) {
  633. case DSIM_STATE_INIT:
  634. exynos_mipi_dsi_init_fifo_pointer(dsim, 0x1f);
  635. /* dsi configuration */
  636. exynos_mipi_dsi_init_config(dsim);
  637. exynos_mipi_dsi_enable_lane(dsim, DSIM_LANE_CLOCK, 1);
  638. exynos_mipi_dsi_enable_lane(dsim, dsim->data_lane, 1);
  639. /* set clock configuration */
  640. exynos_mipi_dsi_set_clock(dsim, dsim->dsim_config->e_byte_clk, 1);
  641. /* check clock and data lane state are stop state */
  642. while (!(exynos_mipi_dsi_is_lane_state(dsim))) {
  643. time_out--;
  644. if (time_out == 0) {
  645. dev_err(dsim->dev,
  646. "DSI Master is not stop state.\n");
  647. dev_err(dsim->dev,
  648. "Check initialization process\n");
  649. return -EINVAL;
  650. }
  651. }
  652. if (time_out != 0) {
  653. dev_info(dsim->dev,
  654. "DSI Master driver has been completed.\n");
  655. dev_info(dsim->dev, "DSI Master state is stop state\n");
  656. }
  657. dsim->state = DSIM_STATE_STOP;
  658. /* BTA sequence counters */
  659. exynos_mipi_dsi_set_stop_state_counter(dsim,
  660. dsim->dsim_config->stop_holding_cnt);
  661. exynos_mipi_dsi_set_bta_timeout(dsim,
  662. dsim->dsim_config->bta_timeout);
  663. exynos_mipi_dsi_set_lpdr_timeout(dsim,
  664. dsim->dsim_config->rx_timeout);
  665. return 0;
  666. default:
  667. dev_info(dsim->dev, "DSI Master is already init.\n");
  668. return 0;
  669. }
  670. return 0;
  671. }
  672. int exynos_mipi_dsi_set_hs_enable(struct mipi_dsim_device *dsim)
  673. {
  674. if (dsim->state != DSIM_STATE_STOP) {
  675. dev_warn(dsim->dev, "DSIM is not in stop state.\n");
  676. return 0;
  677. }
  678. if (dsim->e_clk_src == DSIM_EXT_CLK_BYPASS) {
  679. dev_warn(dsim->dev, "clock source is external bypass.\n");
  680. return 0;
  681. }
  682. dsim->state = DSIM_STATE_HSCLKEN;
  683. /* set LCDC and CPU transfer mode to HS. */
  684. exynos_mipi_dsi_set_lcdc_transfer_mode(dsim, 0);
  685. exynos_mipi_dsi_set_cpu_transfer_mode(dsim, 0);
  686. exynos_mipi_dsi_enable_hs_clock(dsim, 1);
  687. return 0;
  688. }
  689. int exynos_mipi_dsi_set_data_transfer_mode(struct mipi_dsim_device *dsim,
  690. unsigned int mode)
  691. {
  692. if (mode) {
  693. if (dsim->state != DSIM_STATE_HSCLKEN) {
  694. dev_err(dsim->dev, "HS Clock lane is not enabled.\n");
  695. return -EINVAL;
  696. }
  697. exynos_mipi_dsi_set_lcdc_transfer_mode(dsim, 0);
  698. } else {
  699. if (dsim->state == DSIM_STATE_INIT || dsim->state ==
  700. DSIM_STATE_ULPS) {
  701. dev_err(dsim->dev,
  702. "DSI Master is not STOP or HSDT state.\n");
  703. return -EINVAL;
  704. }
  705. exynos_mipi_dsi_set_cpu_transfer_mode(dsim, 0);
  706. }
  707. return 0;
  708. }
  709. int exynos_mipi_dsi_get_frame_done_status(struct mipi_dsim_device *dsim)
  710. {
  711. return _exynos_mipi_dsi_get_frame_done_status(dsim);
  712. }
  713. int exynos_mipi_dsi_clear_frame_done(struct mipi_dsim_device *dsim)
  714. {
  715. _exynos_mipi_dsi_clear_frame_done(dsim);
  716. return 0;
  717. }
  718. int exynos_mipi_dsi_fifo_clear(struct mipi_dsim_device *dsim,
  719. unsigned int val)
  720. {
  721. int try = TRY_FIFO_CLEAR;
  722. exynos_mipi_dsi_sw_reset_release(dsim);
  723. exynos_mipi_dsi_func_reset(dsim);
  724. do {
  725. if (exynos_mipi_dsi_get_sw_reset_release(dsim)) {
  726. exynos_mipi_dsi_init_interrupt(dsim);
  727. dev_dbg(dsim->dev, "reset release done.\n");
  728. return 0;
  729. }
  730. } while (--try);
  731. dev_err(dsim->dev, "failed to clear dsim fifo.\n");
  732. return -EAGAIN;
  733. }
  734. MODULE_AUTHOR("InKi Dae <inki.dae@samsung.com>");
  735. MODULE_DESCRIPTION("Samsung SoC MIPI-DSI common driver");
  736. MODULE_LICENSE("GPL");