exynos_mipi_dsi_common.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  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. }
  251. static unsigned int exynos_mipi_dsi_long_data_rd(struct mipi_dsim_device *dsim,
  252. unsigned int req_size, unsigned int rx_data, u8 *rx_buf)
  253. {
  254. unsigned int rcv_pkt, i, j;
  255. u16 rxsize;
  256. /* for long packet */
  257. rxsize = (u16)((rx_data & 0x00ffff00) >> 8);
  258. dev_dbg(dsim->dev, "mipi dsi rx size : %d\n", rxsize);
  259. if (rxsize != req_size) {
  260. dev_dbg(dsim->dev,
  261. "received size mismatch received: %d, requested: %d\n",
  262. rxsize, req_size);
  263. goto err;
  264. }
  265. for (i = 0; i < (rxsize >> 2); i++) {
  266. rcv_pkt = exynos_mipi_dsi_rd_rx_fifo(dsim);
  267. dev_dbg(dsim->dev, "received pkt : %08x\n", rcv_pkt);
  268. for (j = 0; j < 4; j++) {
  269. rx_buf[(i * 4) + j] =
  270. (u8)(rcv_pkt >> (j * 8)) & 0xff;
  271. dev_dbg(dsim->dev, "received value : %02x\n",
  272. (rcv_pkt >> (j * 8)) & 0xff);
  273. }
  274. }
  275. if (rxsize % 4) {
  276. rcv_pkt = exynos_mipi_dsi_rd_rx_fifo(dsim);
  277. dev_dbg(dsim->dev, "received pkt : %08x\n", rcv_pkt);
  278. for (j = 0; j < (rxsize % 4); j++) {
  279. rx_buf[(i * 4) + j] =
  280. (u8)(rcv_pkt >> (j * 8)) & 0xff;
  281. dev_dbg(dsim->dev, "received value : %02x\n",
  282. (rcv_pkt >> (j * 8)) & 0xff);
  283. }
  284. }
  285. return rxsize;
  286. err:
  287. return -EINVAL;
  288. }
  289. static unsigned int exynos_mipi_dsi_response_size(unsigned int req_size)
  290. {
  291. switch (req_size) {
  292. case 1:
  293. return MIPI_DSI_RX_GENERIC_SHORT_READ_RESPONSE_1BYTE;
  294. case 2:
  295. return MIPI_DSI_RX_GENERIC_SHORT_READ_RESPONSE_2BYTE;
  296. default:
  297. return MIPI_DSI_RX_GENERIC_LONG_READ_RESPONSE;
  298. }
  299. }
  300. int exynos_mipi_dsi_rd_data(struct mipi_dsim_device *dsim, unsigned int data_id,
  301. unsigned int data0, unsigned int req_size, u8 *rx_buf)
  302. {
  303. unsigned int rx_data, rcv_pkt, i;
  304. u8 response = 0;
  305. u16 rxsize;
  306. if (dsim->state == DSIM_STATE_ULPS) {
  307. dev_err(dsim->dev, "state is ULPS.\n");
  308. return -EINVAL;
  309. }
  310. /* FIXME!!! */
  311. msleep(20);
  312. mutex_lock(&dsim->lock);
  313. INIT_COMPLETION(dsim_rd_comp);
  314. exynos_mipi_dsi_rd_tx_header(dsim,
  315. MIPI_DSI_SET_MAXIMUM_RETURN_PACKET_SIZE, req_size);
  316. response = exynos_mipi_dsi_response_size(req_size);
  317. switch (data_id) {
  318. case MIPI_DSI_GENERIC_READ_REQUEST_0_PARAM:
  319. case MIPI_DSI_GENERIC_READ_REQUEST_1_PARAM:
  320. case MIPI_DSI_GENERIC_READ_REQUEST_2_PARAM:
  321. case MIPI_DSI_DCS_READ:
  322. exynos_mipi_dsi_rd_tx_header(dsim,
  323. data_id, data0);
  324. /* process response func should be implemented. */
  325. break;
  326. default:
  327. dev_warn(dsim->dev,
  328. "data id %x is not supported current DSI spec.\n",
  329. data_id);
  330. return -EINVAL;
  331. }
  332. if (!wait_for_completion_interruptible_timeout(&dsim_rd_comp,
  333. MIPI_FIFO_TIMEOUT)) {
  334. pr_err("RX done interrupt timeout\n");
  335. mutex_unlock(&dsim->lock);
  336. return 0;
  337. }
  338. msleep(20);
  339. rx_data = exynos_mipi_dsi_rd_rx_fifo(dsim);
  340. if ((u8)(rx_data & 0xff) != response) {
  341. printk(KERN_ERR
  342. "mipi dsi wrong response rx_data : %x, response:%x\n",
  343. rx_data, response);
  344. goto clear_rx_fifo;
  345. }
  346. if (req_size <= 2) {
  347. /* for short packet */
  348. for (i = 0; i < req_size; i++)
  349. rx_buf[i] = (rx_data >> (8 + (i * 8))) & 0xff;
  350. rxsize = req_size;
  351. } else {
  352. /* for long packet */
  353. rxsize = exynos_mipi_dsi_long_data_rd(dsim, req_size, rx_data,
  354. rx_buf);
  355. if (rxsize != req_size)
  356. goto clear_rx_fifo;
  357. }
  358. rcv_pkt = exynos_mipi_dsi_rd_rx_fifo(dsim);
  359. msleep(20);
  360. if (rcv_pkt != MIPI_RX_FIFO_READ_DONE) {
  361. dev_info(dsim->dev,
  362. "Can't found RX FIFO READ DONE FLAG : %x\n", rcv_pkt);
  363. goto clear_rx_fifo;
  364. }
  365. mutex_unlock(&dsim->lock);
  366. return rxsize;
  367. clear_rx_fifo:
  368. i = 0;
  369. while (1) {
  370. rcv_pkt = exynos_mipi_dsi_rd_rx_fifo(dsim);
  371. if ((rcv_pkt == MIPI_RX_FIFO_READ_DONE)
  372. || (i > MIPI_MAX_RX_FIFO))
  373. break;
  374. dev_dbg(dsim->dev,
  375. "mipi dsi clear rx fifo : %08x\n", rcv_pkt);
  376. i++;
  377. }
  378. dev_info(dsim->dev,
  379. "mipi dsi rx done count : %d, rcv_pkt : %08x\n", i, rcv_pkt);
  380. mutex_unlock(&dsim->lock);
  381. return 0;
  382. }
  383. static int exynos_mipi_dsi_pll_on(struct mipi_dsim_device *dsim,
  384. unsigned int enable)
  385. {
  386. int sw_timeout;
  387. if (enable) {
  388. sw_timeout = 1000;
  389. exynos_mipi_dsi_enable_pll(dsim, 1);
  390. while (1) {
  391. sw_timeout--;
  392. if (exynos_mipi_dsi_is_pll_stable(dsim))
  393. return 0;
  394. if (sw_timeout == 0)
  395. return -EINVAL;
  396. }
  397. } else
  398. exynos_mipi_dsi_enable_pll(dsim, 0);
  399. return 0;
  400. }
  401. static unsigned long exynos_mipi_dsi_change_pll(struct mipi_dsim_device *dsim,
  402. unsigned int pre_divider, unsigned int main_divider,
  403. unsigned int scaler)
  404. {
  405. unsigned long dfin_pll, dfvco, dpll_out;
  406. unsigned int i, freq_band = 0xf;
  407. dfin_pll = (FIN_HZ / pre_divider);
  408. /******************************************************
  409. * Serial Clock(=ByteClk X 8) FreqBand[3:0] *
  410. ******************************************************
  411. * ~ 99.99 MHz 0000
  412. * 100 ~ 119.99 MHz 0001
  413. * 120 ~ 159.99 MHz 0010
  414. * 160 ~ 199.99 MHz 0011
  415. * 200 ~ 239.99 MHz 0100
  416. * 140 ~ 319.99 MHz 0101
  417. * 320 ~ 389.99 MHz 0110
  418. * 390 ~ 449.99 MHz 0111
  419. * 450 ~ 509.99 MHz 1000
  420. * 510 ~ 559.99 MHz 1001
  421. * 560 ~ 639.99 MHz 1010
  422. * 640 ~ 689.99 MHz 1011
  423. * 690 ~ 769.99 MHz 1100
  424. * 770 ~ 869.99 MHz 1101
  425. * 870 ~ 949.99 MHz 1110
  426. * 950 ~ 1000 MHz 1111
  427. ******************************************************/
  428. if (dfin_pll < DFIN_PLL_MIN_HZ || dfin_pll > DFIN_PLL_MAX_HZ) {
  429. dev_warn(dsim->dev, "fin_pll range should be 6MHz ~ 12MHz\n");
  430. exynos_mipi_dsi_enable_afc(dsim, 0, 0);
  431. } else {
  432. if (dfin_pll < 7 * MHZ)
  433. exynos_mipi_dsi_enable_afc(dsim, 1, 0x1);
  434. else if (dfin_pll < 8 * MHZ)
  435. exynos_mipi_dsi_enable_afc(dsim, 1, 0x0);
  436. else if (dfin_pll < 9 * MHZ)
  437. exynos_mipi_dsi_enable_afc(dsim, 1, 0x3);
  438. else if (dfin_pll < 10 * MHZ)
  439. exynos_mipi_dsi_enable_afc(dsim, 1, 0x2);
  440. else if (dfin_pll < 11 * MHZ)
  441. exynos_mipi_dsi_enable_afc(dsim, 1, 0x5);
  442. else
  443. exynos_mipi_dsi_enable_afc(dsim, 1, 0x4);
  444. }
  445. dfvco = dfin_pll * main_divider;
  446. dev_dbg(dsim->dev, "dfvco = %lu, dfin_pll = %lu, main_divider = %d\n",
  447. dfvco, dfin_pll, main_divider);
  448. if (dfvco < DFVCO_MIN_HZ || dfvco > DFVCO_MAX_HZ)
  449. dev_warn(dsim->dev, "fvco range should be 500MHz ~ 1000MHz\n");
  450. dpll_out = dfvco / (1 << scaler);
  451. dev_dbg(dsim->dev, "dpll_out = %lu, dfvco = %lu, scaler = %d\n",
  452. dpll_out, dfvco, scaler);
  453. for (i = 0; i < ARRAY_SIZE(dpll_table); i++) {
  454. if (dpll_out < dpll_table[i] * MHZ) {
  455. freq_band = i;
  456. break;
  457. }
  458. }
  459. dev_dbg(dsim->dev, "freq_band = %d\n", freq_band);
  460. exynos_mipi_dsi_pll_freq(dsim, pre_divider, main_divider, scaler);
  461. exynos_mipi_dsi_hs_zero_ctrl(dsim, 0);
  462. exynos_mipi_dsi_prep_ctrl(dsim, 0);
  463. /* Freq Band */
  464. exynos_mipi_dsi_pll_freq_band(dsim, freq_band);
  465. /* Stable time */
  466. exynos_mipi_dsi_pll_stable_time(dsim, dsim->dsim_config->pll_stable_time);
  467. /* Enable PLL */
  468. dev_dbg(dsim->dev, "FOUT of mipi dphy pll is %luMHz\n",
  469. (dpll_out / MHZ));
  470. return dpll_out;
  471. }
  472. static int exynos_mipi_dsi_set_clock(struct mipi_dsim_device *dsim,
  473. unsigned int byte_clk_sel, unsigned int enable)
  474. {
  475. unsigned int esc_div;
  476. unsigned long esc_clk_error_rate;
  477. unsigned long hs_clk = 0, byte_clk = 0, escape_clk = 0;
  478. if (enable) {
  479. dsim->e_clk_src = byte_clk_sel;
  480. /* Escape mode clock and byte clock source */
  481. exynos_mipi_dsi_set_byte_clock_src(dsim, byte_clk_sel);
  482. /* DPHY, DSIM Link : D-PHY clock out */
  483. if (byte_clk_sel == DSIM_PLL_OUT_DIV8) {
  484. hs_clk = exynos_mipi_dsi_change_pll(dsim,
  485. dsim->dsim_config->p, dsim->dsim_config->m,
  486. dsim->dsim_config->s);
  487. if (hs_clk == 0) {
  488. dev_err(dsim->dev,
  489. "failed to get hs clock.\n");
  490. return -EINVAL;
  491. }
  492. byte_clk = hs_clk / 8;
  493. exynos_mipi_dsi_enable_pll_bypass(dsim, 0);
  494. exynos_mipi_dsi_pll_on(dsim, 1);
  495. /* DPHY : D-PHY clock out, DSIM link : external clock out */
  496. } else if (byte_clk_sel == DSIM_EXT_CLK_DIV8) {
  497. dev_warn(dsim->dev, "this project is not support\n");
  498. dev_warn(dsim->dev,
  499. "external clock source for MIPI DSIM.\n");
  500. } else if (byte_clk_sel == DSIM_EXT_CLK_BYPASS) {
  501. dev_warn(dsim->dev, "this project is not support\n");
  502. dev_warn(dsim->dev,
  503. "external clock source for MIPI DSIM\n");
  504. }
  505. /* escape clock divider */
  506. esc_div = byte_clk / (dsim->dsim_config->esc_clk);
  507. dev_dbg(dsim->dev,
  508. "esc_div = %d, byte_clk = %lu, esc_clk = %lu\n",
  509. esc_div, byte_clk, dsim->dsim_config->esc_clk);
  510. if ((byte_clk / esc_div) >= (20 * MHZ) ||
  511. (byte_clk / esc_div) >
  512. dsim->dsim_config->esc_clk)
  513. esc_div += 1;
  514. escape_clk = byte_clk / esc_div;
  515. dev_dbg(dsim->dev,
  516. "escape_clk = %lu, byte_clk = %lu, esc_div = %d\n",
  517. escape_clk, byte_clk, esc_div);
  518. /* enable escape clock. */
  519. exynos_mipi_dsi_enable_byte_clock(dsim, 1);
  520. /* enable byte clk and escape clock */
  521. exynos_mipi_dsi_set_esc_clk_prs(dsim, 1, esc_div);
  522. /* escape clock on lane */
  523. exynos_mipi_dsi_enable_esc_clk_on_lane(dsim,
  524. (DSIM_LANE_CLOCK | dsim->data_lane), 1);
  525. dev_dbg(dsim->dev, "byte clock is %luMHz\n",
  526. (byte_clk / MHZ));
  527. dev_dbg(dsim->dev, "escape clock that user's need is %lu\n",
  528. (dsim->dsim_config->esc_clk / MHZ));
  529. dev_dbg(dsim->dev, "escape clock divider is %x\n", esc_div);
  530. dev_dbg(dsim->dev, "escape clock is %luMHz\n",
  531. ((byte_clk / esc_div) / MHZ));
  532. if ((byte_clk / esc_div) > escape_clk) {
  533. esc_clk_error_rate = escape_clk /
  534. (byte_clk / esc_div);
  535. dev_warn(dsim->dev, "error rate is %lu over.\n",
  536. (esc_clk_error_rate / 100));
  537. } else if ((byte_clk / esc_div) < (escape_clk)) {
  538. esc_clk_error_rate = (byte_clk / esc_div) /
  539. escape_clk;
  540. dev_warn(dsim->dev, "error rate is %lu under.\n",
  541. (esc_clk_error_rate / 100));
  542. }
  543. } else {
  544. exynos_mipi_dsi_enable_esc_clk_on_lane(dsim,
  545. (DSIM_LANE_CLOCK | dsim->data_lane), 0);
  546. exynos_mipi_dsi_set_esc_clk_prs(dsim, 0, 0);
  547. /* disable escape clock. */
  548. exynos_mipi_dsi_enable_byte_clock(dsim, 0);
  549. if (byte_clk_sel == DSIM_PLL_OUT_DIV8)
  550. exynos_mipi_dsi_pll_on(dsim, 0);
  551. }
  552. return 0;
  553. }
  554. int exynos_mipi_dsi_init_dsim(struct mipi_dsim_device *dsim)
  555. {
  556. dsim->state = DSIM_STATE_INIT;
  557. switch (dsim->dsim_config->e_no_data_lane) {
  558. case DSIM_DATA_LANE_1:
  559. dsim->data_lane = DSIM_LANE_DATA0;
  560. break;
  561. case DSIM_DATA_LANE_2:
  562. dsim->data_lane = DSIM_LANE_DATA0 | DSIM_LANE_DATA1;
  563. break;
  564. case DSIM_DATA_LANE_3:
  565. dsim->data_lane = DSIM_LANE_DATA0 | DSIM_LANE_DATA1 |
  566. DSIM_LANE_DATA2;
  567. break;
  568. case DSIM_DATA_LANE_4:
  569. dsim->data_lane = DSIM_LANE_DATA0 | DSIM_LANE_DATA1 |
  570. DSIM_LANE_DATA2 | DSIM_LANE_DATA3;
  571. break;
  572. default:
  573. dev_info(dsim->dev, "data lane is invalid.\n");
  574. return -EINVAL;
  575. };
  576. exynos_mipi_dsi_sw_reset(dsim);
  577. exynos_mipi_dsi_func_reset(dsim);
  578. exynos_mipi_dsi_dp_dn_swap(dsim, 0);
  579. return 0;
  580. }
  581. void exynos_mipi_dsi_init_interrupt(struct mipi_dsim_device *dsim)
  582. {
  583. unsigned int src = 0;
  584. src = (INTSRC_SFR_FIFO_EMPTY | INTSRC_RX_DATA_DONE);
  585. exynos_mipi_dsi_set_interrupt(dsim, src, 1);
  586. src = 0;
  587. src = ~(INTMSK_RX_DONE | INTMSK_FIFO_EMPTY);
  588. exynos_mipi_dsi_set_interrupt_mask(dsim, src, 1);
  589. }
  590. int exynos_mipi_dsi_enable_frame_done_int(struct mipi_dsim_device *dsim,
  591. unsigned int enable)
  592. {
  593. /* enable only frame done interrupt */
  594. exynos_mipi_dsi_set_interrupt_mask(dsim, INTMSK_FRAME_DONE, enable);
  595. return 0;
  596. }
  597. void exynos_mipi_dsi_stand_by(struct mipi_dsim_device *dsim,
  598. unsigned int enable)
  599. {
  600. /* consider Main display and Sub display. */
  601. exynos_mipi_dsi_set_main_stand_by(dsim, enable);
  602. }
  603. int exynos_mipi_dsi_set_display_mode(struct mipi_dsim_device *dsim,
  604. struct mipi_dsim_config *dsim_config)
  605. {
  606. struct mipi_dsim_platform_data *dsim_pd;
  607. struct fb_videomode *timing;
  608. dsim_pd = (struct mipi_dsim_platform_data *)dsim->pd;
  609. timing = (struct fb_videomode *)dsim_pd->lcd_panel_info;
  610. /* in case of VIDEO MODE (RGB INTERFACE), it sets polarities. */
  611. if (dsim_config->e_interface == (u32) DSIM_VIDEO) {
  612. if (dsim_config->auto_vertical_cnt == 0) {
  613. exynos_mipi_dsi_set_main_disp_vporch(dsim,
  614. dsim_config->cmd_allow,
  615. timing->lower_margin,
  616. timing->upper_margin);
  617. exynos_mipi_dsi_set_main_disp_hporch(dsim,
  618. timing->right_margin,
  619. timing->left_margin);
  620. exynos_mipi_dsi_set_main_disp_sync_area(dsim,
  621. timing->vsync_len,
  622. timing->hsync_len);
  623. }
  624. }
  625. exynos_mipi_dsi_set_main_disp_resol(dsim, timing->xres,
  626. timing->yres);
  627. exynos_mipi_dsi_display_config(dsim, dsim_config);
  628. dev_info(dsim->dev, "lcd panel ==> width = %d, height = %d\n",
  629. timing->xres, timing->yres);
  630. return 0;
  631. }
  632. int exynos_mipi_dsi_init_link(struct mipi_dsim_device *dsim)
  633. {
  634. unsigned int time_out = 100;
  635. switch (dsim->state) {
  636. case DSIM_STATE_INIT:
  637. exynos_mipi_dsi_init_fifo_pointer(dsim, 0x1f);
  638. /* dsi configuration */
  639. exynos_mipi_dsi_init_config(dsim);
  640. exynos_mipi_dsi_enable_lane(dsim, DSIM_LANE_CLOCK, 1);
  641. exynos_mipi_dsi_enable_lane(dsim, dsim->data_lane, 1);
  642. /* set clock configuration */
  643. exynos_mipi_dsi_set_clock(dsim, dsim->dsim_config->e_byte_clk, 1);
  644. /* check clock and data lane state are stop state */
  645. while (!(exynos_mipi_dsi_is_lane_state(dsim))) {
  646. time_out--;
  647. if (time_out == 0) {
  648. dev_err(dsim->dev,
  649. "DSI Master is not stop state.\n");
  650. dev_err(dsim->dev,
  651. "Check initialization process\n");
  652. return -EINVAL;
  653. }
  654. }
  655. if (time_out != 0) {
  656. dev_info(dsim->dev,
  657. "DSI Master driver has been completed.\n");
  658. dev_info(dsim->dev, "DSI Master state is stop state\n");
  659. }
  660. dsim->state = DSIM_STATE_STOP;
  661. /* BTA sequence counters */
  662. exynos_mipi_dsi_set_stop_state_counter(dsim,
  663. dsim->dsim_config->stop_holding_cnt);
  664. exynos_mipi_dsi_set_bta_timeout(dsim,
  665. dsim->dsim_config->bta_timeout);
  666. exynos_mipi_dsi_set_lpdr_timeout(dsim,
  667. dsim->dsim_config->rx_timeout);
  668. return 0;
  669. default:
  670. dev_info(dsim->dev, "DSI Master is already init.\n");
  671. return 0;
  672. }
  673. return 0;
  674. }
  675. int exynos_mipi_dsi_set_hs_enable(struct mipi_dsim_device *dsim)
  676. {
  677. if (dsim->state != DSIM_STATE_STOP) {
  678. dev_warn(dsim->dev, "DSIM is not in stop state.\n");
  679. return 0;
  680. }
  681. if (dsim->e_clk_src == DSIM_EXT_CLK_BYPASS) {
  682. dev_warn(dsim->dev, "clock source is external bypass.\n");
  683. return 0;
  684. }
  685. dsim->state = DSIM_STATE_HSCLKEN;
  686. /* set LCDC and CPU transfer mode to HS. */
  687. exynos_mipi_dsi_set_lcdc_transfer_mode(dsim, 0);
  688. exynos_mipi_dsi_set_cpu_transfer_mode(dsim, 0);
  689. exynos_mipi_dsi_enable_hs_clock(dsim, 1);
  690. return 0;
  691. }
  692. int exynos_mipi_dsi_set_data_transfer_mode(struct mipi_dsim_device *dsim,
  693. unsigned int mode)
  694. {
  695. if (mode) {
  696. if (dsim->state != DSIM_STATE_HSCLKEN) {
  697. dev_err(dsim->dev, "HS Clock lane is not enabled.\n");
  698. return -EINVAL;
  699. }
  700. exynos_mipi_dsi_set_lcdc_transfer_mode(dsim, 0);
  701. } else {
  702. if (dsim->state == DSIM_STATE_INIT || dsim->state ==
  703. DSIM_STATE_ULPS) {
  704. dev_err(dsim->dev,
  705. "DSI Master is not STOP or HSDT state.\n");
  706. return -EINVAL;
  707. }
  708. exynos_mipi_dsi_set_cpu_transfer_mode(dsim, 0);
  709. }
  710. return 0;
  711. }
  712. int exynos_mipi_dsi_get_frame_done_status(struct mipi_dsim_device *dsim)
  713. {
  714. return _exynos_mipi_dsi_get_frame_done_status(dsim);
  715. }
  716. int exynos_mipi_dsi_clear_frame_done(struct mipi_dsim_device *dsim)
  717. {
  718. _exynos_mipi_dsi_clear_frame_done(dsim);
  719. return 0;
  720. }
  721. int exynos_mipi_dsi_fifo_clear(struct mipi_dsim_device *dsim,
  722. unsigned int val)
  723. {
  724. int try = TRY_FIFO_CLEAR;
  725. exynos_mipi_dsi_sw_reset_release(dsim);
  726. exynos_mipi_dsi_func_reset(dsim);
  727. do {
  728. if (exynos_mipi_dsi_get_sw_reset_release(dsim)) {
  729. exynos_mipi_dsi_init_interrupt(dsim);
  730. dev_dbg(dsim->dev, "reset release done.\n");
  731. return 0;
  732. }
  733. } while (--try);
  734. dev_err(dsim->dev, "failed to clear dsim fifo.\n");
  735. return -EAGAIN;
  736. }
  737. MODULE_AUTHOR("InKi Dae <inki.dae@samsung.com>");
  738. MODULE_DESCRIPTION("Samusung SoC MIPI-DSI common driver");
  739. MODULE_LICENSE("GPL");