exynos_mipi_dsi_common.c 22 KB

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