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 <mach/map.h>
  32. #include "exynos_mipi_dsi_regs.h"
  33. #include "exynos_mipi_dsi_lowlevel.h"
  34. #include "exynos_mipi_dsi_common.h"
  35. #define MIPI_FIFO_TIMEOUT msecs_to_jiffies(250)
  36. #define MIPI_RX_FIFO_READ_DONE 0x30800002
  37. #define MIPI_MAX_RX_FIFO 20
  38. #define MHZ (1000 * 1000)
  39. #define FIN_HZ (24 * MHZ)
  40. #define DFIN_PLL_MIN_HZ (6 * MHZ)
  41. #define DFIN_PLL_MAX_HZ (12 * MHZ)
  42. #define DFVCO_MIN_HZ (500 * MHZ)
  43. #define DFVCO_MAX_HZ (1000 * MHZ)
  44. #define TRY_GET_FIFO_TIMEOUT (5000 * 2)
  45. #define TRY_FIFO_CLEAR (10)
  46. /* MIPI-DSIM status types. */
  47. enum {
  48. DSIM_STATE_INIT, /* should be initialized. */
  49. DSIM_STATE_STOP, /* CPU and LCDC are LP mode. */
  50. DSIM_STATE_HSCLKEN, /* HS clock was enabled. */
  51. DSIM_STATE_ULPS
  52. };
  53. /* define DSI lane types. */
  54. enum {
  55. DSIM_LANE_CLOCK = (1 << 0),
  56. DSIM_LANE_DATA0 = (1 << 1),
  57. DSIM_LANE_DATA1 = (1 << 2),
  58. DSIM_LANE_DATA2 = (1 << 3),
  59. DSIM_LANE_DATA3 = (1 << 4)
  60. };
  61. static unsigned int dpll_table[15] = {
  62. 100, 120, 170, 220, 270,
  63. 320, 390, 450, 510, 560,
  64. 640, 690, 770, 870, 950
  65. };
  66. irqreturn_t exynos_mipi_dsi_interrupt_handler(int irq, void *dev_id)
  67. {
  68. struct mipi_dsim_device *dsim = dev_id;
  69. unsigned int intsrc, intmsk;
  70. intsrc = exynos_mipi_dsi_read_interrupt(dsim);
  71. intmsk = exynos_mipi_dsi_read_interrupt_mask(dsim);
  72. intmsk = ~intmsk & intsrc;
  73. if (intsrc & INTMSK_RX_DONE) {
  74. complete(&dsim_rd_comp);
  75. dev_dbg(dsim->dev, "MIPI INTMSK_RX_DONE\n");
  76. }
  77. if (intsrc & INTMSK_FIFO_EMPTY) {
  78. complete(&dsim_wr_comp);
  79. dev_dbg(dsim->dev, "MIPI INTMSK_FIFO_EMPTY\n");
  80. }
  81. exynos_mipi_dsi_clear_interrupt(dsim, intmsk);
  82. return IRQ_HANDLED;
  83. }
  84. /*
  85. * write long packet to mipi dsi slave
  86. * @dsim: mipi dsim device structure.
  87. * @data0: packet data to send.
  88. * @data1: size of packet data
  89. */
  90. static void exynos_mipi_dsi_long_data_wr(struct mipi_dsim_device *dsim,
  91. const unsigned char *data0, unsigned int data_size)
  92. {
  93. unsigned int data_cnt = 0, payload = 0;
  94. /* in case that data count is more then 4 */
  95. for (data_cnt = 0; data_cnt < data_size; data_cnt += 4) {
  96. /*
  97. * after sending 4bytes per one time,
  98. * send remainder data less then 4.
  99. */
  100. if ((data_size - data_cnt) < 4) {
  101. if ((data_size - data_cnt) == 3) {
  102. payload = data0[data_cnt] |
  103. data0[data_cnt + 1] << 8 |
  104. data0[data_cnt + 2] << 16;
  105. dev_dbg(dsim->dev, "count = 3 payload = %x, %x %x %x\n",
  106. payload, data0[data_cnt],
  107. data0[data_cnt + 1],
  108. data0[data_cnt + 2]);
  109. } else if ((data_size - data_cnt) == 2) {
  110. payload = data0[data_cnt] |
  111. data0[data_cnt + 1] << 8;
  112. dev_dbg(dsim->dev,
  113. "count = 2 payload = %x, %x %x\n", payload,
  114. data0[data_cnt],
  115. data0[data_cnt + 1]);
  116. } else if ((data_size - data_cnt) == 1) {
  117. payload = data0[data_cnt];
  118. }
  119. exynos_mipi_dsi_wr_tx_data(dsim, payload);
  120. /* send 4bytes per one time. */
  121. } else {
  122. payload = data0[data_cnt] |
  123. data0[data_cnt + 1] << 8 |
  124. data0[data_cnt + 2] << 16 |
  125. data0[data_cnt + 3] << 24;
  126. dev_dbg(dsim->dev,
  127. "count = 4 payload = %x, %x %x %x %x\n",
  128. payload, *(u8 *)(data0 + data_cnt),
  129. data0[data_cnt + 1],
  130. data0[data_cnt + 2],
  131. data0[data_cnt + 3]);
  132. exynos_mipi_dsi_wr_tx_data(dsim, payload);
  133. }
  134. }
  135. }
  136. int exynos_mipi_dsi_wr_data(struct mipi_dsim_device *dsim, unsigned int data_id,
  137. const unsigned char *data0, unsigned int data_size)
  138. {
  139. unsigned int check_rx_ack = 0;
  140. if (dsim->state == DSIM_STATE_ULPS) {
  141. dev_err(dsim->dev, "state is ULPS.\n");
  142. return -EINVAL;
  143. }
  144. /* FIXME!!! why does it need this delay? */
  145. msleep(20);
  146. mutex_lock(&dsim->lock);
  147. switch (data_id) {
  148. /* short packet types of packet types for command. */
  149. case MIPI_DSI_GENERIC_SHORT_WRITE_0_PARAM:
  150. case MIPI_DSI_GENERIC_SHORT_WRITE_1_PARAM:
  151. case MIPI_DSI_GENERIC_SHORT_WRITE_2_PARAM:
  152. case MIPI_DSI_DCS_SHORT_WRITE:
  153. case MIPI_DSI_DCS_SHORT_WRITE_PARAM:
  154. case MIPI_DSI_SET_MAXIMUM_RETURN_PACKET_SIZE:
  155. exynos_mipi_dsi_wr_tx_header(dsim, data_id, data0[0], data0[1]);
  156. if (check_rx_ack) {
  157. /* process response func should be implemented */
  158. mutex_unlock(&dsim->lock);
  159. return 0;
  160. } else {
  161. mutex_unlock(&dsim->lock);
  162. return -EINVAL;
  163. }
  164. /* general command */
  165. case MIPI_DSI_COLOR_MODE_OFF:
  166. case MIPI_DSI_COLOR_MODE_ON:
  167. case MIPI_DSI_SHUTDOWN_PERIPHERAL:
  168. case MIPI_DSI_TURN_ON_PERIPHERAL:
  169. exynos_mipi_dsi_wr_tx_header(dsim, data_id, data0[0], data0[1]);
  170. if (check_rx_ack) {
  171. /* process response func should be implemented. */
  172. mutex_unlock(&dsim->lock);
  173. return 0;
  174. } else {
  175. mutex_unlock(&dsim->lock);
  176. return -EINVAL;
  177. }
  178. /* packet types for video data */
  179. case MIPI_DSI_V_SYNC_START:
  180. case MIPI_DSI_V_SYNC_END:
  181. case MIPI_DSI_H_SYNC_START:
  182. case MIPI_DSI_H_SYNC_END:
  183. case MIPI_DSI_END_OF_TRANSMISSION:
  184. mutex_unlock(&dsim->lock);
  185. return 0;
  186. /* long packet type and null packet */
  187. case MIPI_DSI_NULL_PACKET:
  188. case MIPI_DSI_BLANKING_PACKET:
  189. mutex_unlock(&dsim->lock);
  190. return 0;
  191. case MIPI_DSI_GENERIC_LONG_WRITE:
  192. case MIPI_DSI_DCS_LONG_WRITE:
  193. {
  194. unsigned int size, payload = 0;
  195. INIT_COMPLETION(dsim_wr_comp);
  196. size = data_size * 4;
  197. /* if data count is less then 4, then send 3bytes data. */
  198. if (data_size < 4) {
  199. payload = data0[0] |
  200. data0[1] << 8 |
  201. data0[2] << 16;
  202. exynos_mipi_dsi_wr_tx_data(dsim, payload);
  203. dev_dbg(dsim->dev, "count = %d payload = %x,%x %x %x\n",
  204. data_size, payload, data0[0],
  205. data0[1], data0[2]);
  206. /* in case that data count is more then 4 */
  207. } else
  208. exynos_mipi_dsi_long_data_wr(dsim, data0, data_size);
  209. /* put data into header fifo */
  210. exynos_mipi_dsi_wr_tx_header(dsim, data_id, data_size & 0xff,
  211. (data_size & 0xff00) >> 8);
  212. if (!wait_for_completion_interruptible_timeout(&dsim_wr_comp,
  213. MIPI_FIFO_TIMEOUT)) {
  214. dev_warn(dsim->dev, "command write timeout.\n");
  215. mutex_unlock(&dsim->lock);
  216. return -EAGAIN;
  217. }
  218. if (check_rx_ack) {
  219. /* process response func should be implemented. */
  220. mutex_unlock(&dsim->lock);
  221. return 0;
  222. } else {
  223. mutex_unlock(&dsim->lock);
  224. return -EINVAL;
  225. }
  226. }
  227. /* packet typo for video data */
  228. case MIPI_DSI_PACKED_PIXEL_STREAM_16:
  229. case MIPI_DSI_PACKED_PIXEL_STREAM_18:
  230. case MIPI_DSI_PIXEL_STREAM_3BYTE_18:
  231. case MIPI_DSI_PACKED_PIXEL_STREAM_24:
  232. if (check_rx_ack) {
  233. /* process response func should be implemented. */
  234. mutex_unlock(&dsim->lock);
  235. return 0;
  236. } else {
  237. mutex_unlock(&dsim->lock);
  238. return -EINVAL;
  239. }
  240. default:
  241. dev_warn(dsim->dev,
  242. "data id %x is not supported current DSI spec.\n",
  243. data_id);
  244. mutex_unlock(&dsim->lock);
  245. return -EINVAL;
  246. }
  247. }
  248. static unsigned int exynos_mipi_dsi_long_data_rd(struct mipi_dsim_device *dsim,
  249. unsigned int req_size, unsigned int rx_data, u8 *rx_buf)
  250. {
  251. unsigned int rcv_pkt, i, j;
  252. u16 rxsize;
  253. /* for long packet */
  254. rxsize = (u16)((rx_data & 0x00ffff00) >> 8);
  255. dev_dbg(dsim->dev, "mipi dsi rx size : %d\n", rxsize);
  256. if (rxsize != req_size) {
  257. dev_dbg(dsim->dev,
  258. "received size mismatch received: %d, requested: %d\n",
  259. rxsize, req_size);
  260. goto err;
  261. }
  262. for (i = 0; i < (rxsize >> 2); i++) {
  263. rcv_pkt = exynos_mipi_dsi_rd_rx_fifo(dsim);
  264. dev_dbg(dsim->dev, "received pkt : %08x\n", rcv_pkt);
  265. for (j = 0; j < 4; j++) {
  266. rx_buf[(i * 4) + j] =
  267. (u8)(rcv_pkt >> (j * 8)) & 0xff;
  268. dev_dbg(dsim->dev, "received value : %02x\n",
  269. (rcv_pkt >> (j * 8)) & 0xff);
  270. }
  271. }
  272. if (rxsize % 4) {
  273. rcv_pkt = exynos_mipi_dsi_rd_rx_fifo(dsim);
  274. dev_dbg(dsim->dev, "received pkt : %08x\n", rcv_pkt);
  275. for (j = 0; j < (rxsize % 4); j++) {
  276. rx_buf[(i * 4) + j] =
  277. (u8)(rcv_pkt >> (j * 8)) & 0xff;
  278. dev_dbg(dsim->dev, "received value : %02x\n",
  279. (rcv_pkt >> (j * 8)) & 0xff);
  280. }
  281. }
  282. return rxsize;
  283. err:
  284. return -EINVAL;
  285. }
  286. static unsigned int exynos_mipi_dsi_response_size(unsigned int req_size)
  287. {
  288. switch (req_size) {
  289. case 1:
  290. return MIPI_DSI_RX_GENERIC_SHORT_READ_RESPONSE_1BYTE;
  291. case 2:
  292. return MIPI_DSI_RX_GENERIC_SHORT_READ_RESPONSE_2BYTE;
  293. default:
  294. return MIPI_DSI_RX_GENERIC_LONG_READ_RESPONSE;
  295. }
  296. }
  297. int exynos_mipi_dsi_rd_data(struct mipi_dsim_device *dsim, unsigned int data_id,
  298. unsigned int data0, unsigned int req_size, u8 *rx_buf)
  299. {
  300. unsigned int rx_data, rcv_pkt, i;
  301. u8 response = 0;
  302. u16 rxsize;
  303. if (dsim->state == DSIM_STATE_ULPS) {
  304. dev_err(dsim->dev, "state is ULPS.\n");
  305. return -EINVAL;
  306. }
  307. /* FIXME!!! */
  308. msleep(20);
  309. mutex_lock(&dsim->lock);
  310. INIT_COMPLETION(dsim_rd_comp);
  311. exynos_mipi_dsi_rd_tx_header(dsim,
  312. MIPI_DSI_SET_MAXIMUM_RETURN_PACKET_SIZE, req_size);
  313. response = exynos_mipi_dsi_response_size(req_size);
  314. switch (data_id) {
  315. case MIPI_DSI_GENERIC_READ_REQUEST_0_PARAM:
  316. case MIPI_DSI_GENERIC_READ_REQUEST_1_PARAM:
  317. case MIPI_DSI_GENERIC_READ_REQUEST_2_PARAM:
  318. case MIPI_DSI_DCS_READ:
  319. exynos_mipi_dsi_rd_tx_header(dsim,
  320. data_id, data0);
  321. /* process response func should be implemented. */
  322. break;
  323. default:
  324. dev_warn(dsim->dev,
  325. "data id %x is not supported current DSI spec.\n",
  326. data_id);
  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("Samusung SoC MIPI-DSI common driver");
  736. MODULE_LICENSE("GPL");