exynos_mipi_dsi_common.c 22 KB

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