ipu-di.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  1. /*
  2. * Copyright (c) 2010 Sascha Hauer <s.hauer@pengutronix.de>
  3. * Copyright (C) 2005-2009 Freescale Semiconductor, Inc.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the
  7. * Free Software Foundation; either version 2 of the License, or (at your
  8. * option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  12. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  13. * for more details.
  14. */
  15. #include <linux/export.h>
  16. #include <linux/module.h>
  17. #include <linux/types.h>
  18. #include <linux/errno.h>
  19. #include <linux/io.h>
  20. #include <linux/err.h>
  21. #include <linux/platform_device.h>
  22. #include <video/imx-ipu-v3.h>
  23. #include "ipu-prv.h"
  24. struct ipu_di {
  25. void __iomem *base;
  26. int id;
  27. u32 module;
  28. struct clk *clk_di; /* display input clock */
  29. struct clk *clk_ipu; /* IPU bus clock */
  30. struct clk *clk_di_pixel; /* resulting pixel clock */
  31. bool inuse;
  32. struct ipu_soc *ipu;
  33. };
  34. static DEFINE_MUTEX(di_mutex);
  35. struct di_sync_config {
  36. int run_count;
  37. int run_src;
  38. int offset_count;
  39. int offset_src;
  40. int repeat_count;
  41. int cnt_clr_src;
  42. int cnt_polarity_gen_en;
  43. int cnt_polarity_clr_src;
  44. int cnt_polarity_trigger_src;
  45. int cnt_up;
  46. int cnt_down;
  47. };
  48. enum di_pins {
  49. DI_PIN11 = 0,
  50. DI_PIN12 = 1,
  51. DI_PIN13 = 2,
  52. DI_PIN14 = 3,
  53. DI_PIN15 = 4,
  54. DI_PIN16 = 5,
  55. DI_PIN17 = 6,
  56. DI_PIN_CS = 7,
  57. DI_PIN_SER_CLK = 0,
  58. DI_PIN_SER_RS = 1,
  59. };
  60. enum di_sync_wave {
  61. DI_SYNC_NONE = 0,
  62. DI_SYNC_CLK = 1,
  63. DI_SYNC_INT_HSYNC = 2,
  64. DI_SYNC_HSYNC = 3,
  65. DI_SYNC_VSYNC = 4,
  66. DI_SYNC_DE = 6,
  67. };
  68. #define SYNC_WAVE 0
  69. #define DI_GENERAL 0x0000
  70. #define DI_BS_CLKGEN0 0x0004
  71. #define DI_BS_CLKGEN1 0x0008
  72. #define DI_SW_GEN0(gen) (0x000c + 4 * ((gen) - 1))
  73. #define DI_SW_GEN1(gen) (0x0030 + 4 * ((gen) - 1))
  74. #define DI_STP_REP(gen) (0x0148 + 4 * (((gen) - 1)/2))
  75. #define DI_SYNC_AS_GEN 0x0054
  76. #define DI_DW_GEN(gen) (0x0058 + 4 * (gen))
  77. #define DI_DW_SET(gen, set) (0x0088 + 4 * ((gen) + 0xc * (set)))
  78. #define DI_SER_CONF 0x015c
  79. #define DI_SSC 0x0160
  80. #define DI_POL 0x0164
  81. #define DI_AW0 0x0168
  82. #define DI_AW1 0x016c
  83. #define DI_SCR_CONF 0x0170
  84. #define DI_STAT 0x0174
  85. #define DI_SW_GEN0_RUN_COUNT(x) ((x) << 19)
  86. #define DI_SW_GEN0_RUN_SRC(x) ((x) << 16)
  87. #define DI_SW_GEN0_OFFSET_COUNT(x) ((x) << 3)
  88. #define DI_SW_GEN0_OFFSET_SRC(x) ((x) << 0)
  89. #define DI_SW_GEN1_CNT_POL_GEN_EN(x) ((x) << 29)
  90. #define DI_SW_GEN1_CNT_CLR_SRC(x) ((x) << 25)
  91. #define DI_SW_GEN1_CNT_POL_TRIGGER_SRC(x) ((x) << 12)
  92. #define DI_SW_GEN1_CNT_POL_CLR_SRC(x) ((x) << 9)
  93. #define DI_SW_GEN1_CNT_DOWN(x) ((x) << 16)
  94. #define DI_SW_GEN1_CNT_UP(x) (x)
  95. #define DI_SW_GEN1_AUTO_RELOAD (0x10000000)
  96. #define DI_DW_GEN_ACCESS_SIZE_OFFSET 24
  97. #define DI_DW_GEN_COMPONENT_SIZE_OFFSET 16
  98. #define DI_GEN_POLARITY_1 (1 << 0)
  99. #define DI_GEN_POLARITY_2 (1 << 1)
  100. #define DI_GEN_POLARITY_3 (1 << 2)
  101. #define DI_GEN_POLARITY_4 (1 << 3)
  102. #define DI_GEN_POLARITY_5 (1 << 4)
  103. #define DI_GEN_POLARITY_6 (1 << 5)
  104. #define DI_GEN_POLARITY_7 (1 << 6)
  105. #define DI_GEN_POLARITY_8 (1 << 7)
  106. #define DI_GEN_POLARITY_DISP_CLK (1 << 17)
  107. #define DI_GEN_DI_CLK_EXT (1 << 20)
  108. #define DI_GEN_DI_VSYNC_EXT (1 << 21)
  109. #define DI_POL_DRDY_DATA_POLARITY (1 << 7)
  110. #define DI_POL_DRDY_POLARITY_15 (1 << 4)
  111. #define DI_VSYNC_SEL_OFFSET 13
  112. static inline u32 ipu_di_read(struct ipu_di *di, unsigned offset)
  113. {
  114. return readl(di->base + offset);
  115. }
  116. static inline void ipu_di_write(struct ipu_di *di, u32 value, unsigned offset)
  117. {
  118. writel(value, di->base + offset);
  119. }
  120. static void ipu_di_data_wave_config(struct ipu_di *di,
  121. int wave_gen,
  122. int access_size, int component_size)
  123. {
  124. u32 reg;
  125. reg = (access_size << DI_DW_GEN_ACCESS_SIZE_OFFSET) |
  126. (component_size << DI_DW_GEN_COMPONENT_SIZE_OFFSET);
  127. ipu_di_write(di, reg, DI_DW_GEN(wave_gen));
  128. }
  129. static void ipu_di_data_pin_config(struct ipu_di *di, int wave_gen, int di_pin,
  130. int set, int up, int down)
  131. {
  132. u32 reg;
  133. reg = ipu_di_read(di, DI_DW_GEN(wave_gen));
  134. reg &= ~(0x3 << (di_pin * 2));
  135. reg |= set << (di_pin * 2);
  136. ipu_di_write(di, reg, DI_DW_GEN(wave_gen));
  137. ipu_di_write(di, (down << 16) | up, DI_DW_SET(wave_gen, set));
  138. }
  139. static void ipu_di_sync_config(struct ipu_di *di, struct di_sync_config *config,
  140. int start, int count)
  141. {
  142. u32 reg;
  143. int i;
  144. for (i = 0; i < count; i++) {
  145. struct di_sync_config *c = &config[i];
  146. int wave_gen = start + i + 1;
  147. if ((c->run_count >= 0x1000) || (c->offset_count >= 0x1000) ||
  148. (c->repeat_count >= 0x1000) ||
  149. (c->cnt_up >= 0x400) ||
  150. (c->cnt_down >= 0x400)) {
  151. dev_err(di->ipu->dev, "DI%d counters out of range.\n",
  152. di->id);
  153. return;
  154. }
  155. reg = DI_SW_GEN0_RUN_COUNT(c->run_count) |
  156. DI_SW_GEN0_RUN_SRC(c->run_src) |
  157. DI_SW_GEN0_OFFSET_COUNT(c->offset_count) |
  158. DI_SW_GEN0_OFFSET_SRC(c->offset_src);
  159. ipu_di_write(di, reg, DI_SW_GEN0(wave_gen));
  160. reg = DI_SW_GEN1_CNT_POL_GEN_EN(c->cnt_polarity_gen_en) |
  161. DI_SW_GEN1_CNT_CLR_SRC(c->cnt_clr_src) |
  162. DI_SW_GEN1_CNT_POL_TRIGGER_SRC(
  163. c->cnt_polarity_trigger_src) |
  164. DI_SW_GEN1_CNT_POL_CLR_SRC(c->cnt_polarity_clr_src) |
  165. DI_SW_GEN1_CNT_DOWN(c->cnt_down) |
  166. DI_SW_GEN1_CNT_UP(c->cnt_up);
  167. /* Enable auto reload */
  168. if (c->repeat_count == 0)
  169. reg |= DI_SW_GEN1_AUTO_RELOAD;
  170. ipu_di_write(di, reg, DI_SW_GEN1(wave_gen));
  171. reg = ipu_di_read(di, DI_STP_REP(wave_gen));
  172. reg &= ~(0xffff << (16 * ((wave_gen - 1) & 0x1)));
  173. reg |= c->repeat_count << (16 * ((wave_gen - 1) & 0x1));
  174. ipu_di_write(di, reg, DI_STP_REP(wave_gen));
  175. }
  176. }
  177. static void ipu_di_sync_config_interlaced(struct ipu_di *di,
  178. struct ipu_di_signal_cfg *sig)
  179. {
  180. u32 h_total = sig->width + sig->h_sync_width +
  181. sig->h_start_width + sig->h_end_width;
  182. u32 v_total = sig->height + sig->v_sync_width +
  183. sig->v_start_width + sig->v_end_width;
  184. u32 reg;
  185. struct di_sync_config cfg[] = {
  186. {
  187. .run_count = h_total / 2 - 1,
  188. .run_src = DI_SYNC_CLK,
  189. }, {
  190. .run_count = h_total - 11,
  191. .run_src = DI_SYNC_CLK,
  192. .cnt_down = 4,
  193. }, {
  194. .run_count = v_total * 2 - 1,
  195. .run_src = DI_SYNC_INT_HSYNC,
  196. .offset_count = 1,
  197. .offset_src = DI_SYNC_INT_HSYNC,
  198. .cnt_down = 4,
  199. }, {
  200. .run_count = v_total / 2 - 1,
  201. .run_src = DI_SYNC_HSYNC,
  202. .offset_count = sig->v_start_width,
  203. .offset_src = DI_SYNC_HSYNC,
  204. .repeat_count = 2,
  205. .cnt_clr_src = DI_SYNC_VSYNC,
  206. }, {
  207. .run_src = DI_SYNC_HSYNC,
  208. .repeat_count = sig->height / 2,
  209. .cnt_clr_src = 4,
  210. }, {
  211. .run_count = v_total - 1,
  212. .run_src = DI_SYNC_HSYNC,
  213. }, {
  214. .run_count = v_total / 2 - 1,
  215. .run_src = DI_SYNC_HSYNC,
  216. .offset_count = 9,
  217. .offset_src = DI_SYNC_HSYNC,
  218. .repeat_count = 2,
  219. .cnt_clr_src = DI_SYNC_VSYNC,
  220. }, {
  221. .run_src = DI_SYNC_CLK,
  222. .offset_count = sig->h_start_width,
  223. .offset_src = DI_SYNC_CLK,
  224. .repeat_count = sig->width,
  225. .cnt_clr_src = 5,
  226. }, {
  227. .run_count = v_total - 1,
  228. .run_src = DI_SYNC_INT_HSYNC,
  229. .offset_count = v_total / 2,
  230. .offset_src = DI_SYNC_INT_HSYNC,
  231. .cnt_clr_src = DI_SYNC_HSYNC,
  232. .cnt_down = 4,
  233. }
  234. };
  235. ipu_di_sync_config(di, cfg, 0, ARRAY_SIZE(cfg));
  236. /* set gentime select and tag sel */
  237. reg = ipu_di_read(di, DI_SW_GEN1(9));
  238. reg &= 0x1FFFFFFF;
  239. reg |= (3 - 1) << 29 | 0x00008000;
  240. ipu_di_write(di, reg, DI_SW_GEN1(9));
  241. ipu_di_write(di, v_total / 2 - 1, DI_SCR_CONF);
  242. }
  243. static void ipu_di_sync_config_noninterlaced(struct ipu_di *di,
  244. struct ipu_di_signal_cfg *sig, int div)
  245. {
  246. u32 h_total = sig->width + sig->h_sync_width + sig->h_start_width +
  247. sig->h_end_width;
  248. u32 v_total = sig->height + sig->v_sync_width + sig->v_start_width +
  249. sig->v_end_width;
  250. struct di_sync_config cfg[] = {
  251. {
  252. /* 1: INT_HSYNC */
  253. .run_count = h_total - 1,
  254. .run_src = DI_SYNC_CLK,
  255. } , {
  256. /* PIN2: HSYNC */
  257. .run_count = h_total - 1,
  258. .run_src = DI_SYNC_CLK,
  259. .offset_count = div * sig->v_to_h_sync,
  260. .offset_src = DI_SYNC_CLK,
  261. .cnt_polarity_gen_en = 1,
  262. .cnt_polarity_trigger_src = DI_SYNC_CLK,
  263. .cnt_down = sig->h_sync_width * 2,
  264. } , {
  265. /* PIN3: VSYNC */
  266. .run_count = v_total - 1,
  267. .run_src = DI_SYNC_INT_HSYNC,
  268. .cnt_polarity_gen_en = 1,
  269. .cnt_polarity_trigger_src = DI_SYNC_INT_HSYNC,
  270. .cnt_down = sig->v_sync_width * 2,
  271. } , {
  272. /* 4: Line Active */
  273. .run_src = DI_SYNC_HSYNC,
  274. .offset_count = sig->v_sync_width + sig->v_start_width,
  275. .offset_src = DI_SYNC_HSYNC,
  276. .repeat_count = sig->height,
  277. .cnt_clr_src = DI_SYNC_VSYNC,
  278. } , {
  279. /* 5: Pixel Active, referenced by DC */
  280. .run_src = DI_SYNC_CLK,
  281. .offset_count = sig->h_sync_width + sig->h_start_width,
  282. .offset_src = DI_SYNC_CLK,
  283. .repeat_count = sig->width,
  284. .cnt_clr_src = 5, /* Line Active */
  285. } , {
  286. /* unused */
  287. } , {
  288. /* unused */
  289. } , {
  290. /* unused */
  291. } , {
  292. /* unused */
  293. },
  294. };
  295. /* can't use #7 and #8 for line active and pixel active counters */
  296. struct di_sync_config cfg_vga[] = {
  297. {
  298. /* 1: INT_HSYNC */
  299. .run_count = h_total - 1,
  300. .run_src = DI_SYNC_CLK,
  301. } , {
  302. /* 2: VSYNC */
  303. .run_count = v_total - 1,
  304. .run_src = DI_SYNC_INT_HSYNC,
  305. } , {
  306. /* 3: Line Active */
  307. .run_src = DI_SYNC_INT_HSYNC,
  308. .offset_count = sig->v_sync_width + sig->v_start_width,
  309. .offset_src = DI_SYNC_INT_HSYNC,
  310. .repeat_count = sig->height,
  311. .cnt_clr_src = 3 /* VSYNC */,
  312. } , {
  313. /* PIN4: HSYNC for VGA via TVEv2 on TQ MBa53 */
  314. .run_count = h_total - 1,
  315. .run_src = DI_SYNC_CLK,
  316. .offset_count = div * sig->v_to_h_sync + 18, /* magic value from Freescale TVE driver */
  317. .offset_src = DI_SYNC_CLK,
  318. .cnt_polarity_gen_en = 1,
  319. .cnt_polarity_trigger_src = DI_SYNC_CLK,
  320. .cnt_down = sig->h_sync_width * 2,
  321. } , {
  322. /* 5: Pixel Active signal to DC */
  323. .run_src = DI_SYNC_CLK,
  324. .offset_count = sig->h_sync_width + sig->h_start_width,
  325. .offset_src = DI_SYNC_CLK,
  326. .repeat_count = sig->width,
  327. .cnt_clr_src = 4, /* Line Active */
  328. } , {
  329. /* PIN6: VSYNC for VGA via TVEv2 on TQ MBa53 */
  330. .run_count = v_total - 1,
  331. .run_src = DI_SYNC_INT_HSYNC,
  332. .offset_count = 1, /* magic value from Freescale TVE driver */
  333. .offset_src = DI_SYNC_INT_HSYNC,
  334. .cnt_polarity_gen_en = 1,
  335. .cnt_polarity_trigger_src = DI_SYNC_INT_HSYNC,
  336. .cnt_down = sig->v_sync_width * 2,
  337. } , {
  338. /* PIN4: HSYNC for VGA via TVEv2 on i.MX53-QSB */
  339. .run_count = h_total - 1,
  340. .run_src = DI_SYNC_CLK,
  341. .offset_count = div * sig->v_to_h_sync + 18, /* magic value from Freescale TVE driver */
  342. .offset_src = DI_SYNC_CLK,
  343. .cnt_polarity_gen_en = 1,
  344. .cnt_polarity_trigger_src = DI_SYNC_CLK,
  345. .cnt_down = sig->h_sync_width * 2,
  346. } , {
  347. /* PIN6: VSYNC for VGA via TVEv2 on i.MX53-QSB */
  348. .run_count = v_total - 1,
  349. .run_src = DI_SYNC_INT_HSYNC,
  350. .offset_count = 1, /* magic value from Freescale TVE driver */
  351. .offset_src = DI_SYNC_INT_HSYNC,
  352. .cnt_polarity_gen_en = 1,
  353. .cnt_polarity_trigger_src = DI_SYNC_INT_HSYNC,
  354. .cnt_down = sig->v_sync_width * 2,
  355. } , {
  356. /* unused */
  357. },
  358. };
  359. ipu_di_write(di, v_total - 1, DI_SCR_CONF);
  360. if (sig->hsync_pin == 2 && sig->vsync_pin == 3)
  361. ipu_di_sync_config(di, cfg, 0, ARRAY_SIZE(cfg));
  362. else
  363. ipu_di_sync_config(di, cfg_vga, 0, ARRAY_SIZE(cfg_vga));
  364. }
  365. static void ipu_di_config_clock(struct ipu_di *di,
  366. const struct ipu_di_signal_cfg *sig)
  367. {
  368. struct clk *clk;
  369. unsigned clkgen0;
  370. uint32_t val;
  371. if (sig->clkflags & IPU_DI_CLKMODE_EXT) {
  372. /*
  373. * CLKMODE_EXT means we must use the DI clock: this is
  374. * needed for things like LVDS which needs to feed the
  375. * DI and LDB with the same pixel clock.
  376. */
  377. clk = di->clk_di;
  378. if (sig->clkflags & IPU_DI_CLKMODE_SYNC) {
  379. /*
  380. * CLKMODE_SYNC means that we want the DI to be
  381. * clocked at the same rate as the parent clock.
  382. * This is needed (eg) for LDB which needs to be
  383. * fed with the same pixel clock. We assume that
  384. * the LDB clock has already been set correctly.
  385. */
  386. clkgen0 = 1 << 4;
  387. } else {
  388. /*
  389. * We can use the divider. We should really have
  390. * a flag here indicating whether the bridge can
  391. * cope with a fractional divider or not. For the
  392. * time being, let's go for simplicitly and
  393. * reliability.
  394. */
  395. unsigned long in_rate;
  396. unsigned div;
  397. clk_set_rate(clk, sig->pixelclock);
  398. in_rate = clk_get_rate(clk);
  399. div = (in_rate + sig->pixelclock / 2) / sig->pixelclock;
  400. if (div == 0)
  401. div = 1;
  402. clkgen0 = div << 4;
  403. }
  404. } else {
  405. /*
  406. * For other interfaces, we can arbitarily select between
  407. * the DI specific clock and the internal IPU clock. See
  408. * DI_GENERAL bit 20. We select the IPU clock if it can
  409. * give us a clock rate within 1% of the requested frequency,
  410. * otherwise we use the DI clock.
  411. */
  412. unsigned long rate, clkrate;
  413. unsigned div, error;
  414. clkrate = clk_get_rate(di->clk_ipu);
  415. div = (clkrate + sig->pixelclock / 2) / sig->pixelclock;
  416. rate = clkrate / div;
  417. error = rate / (sig->pixelclock / 1000);
  418. dev_dbg(di->ipu->dev, " IPU clock can give %lu with divider %u, error %d.%u%%\n",
  419. rate, div, (signed)(error - 1000) / 10, error % 10);
  420. /* Allow a 1% error */
  421. if (error < 1010 && error >= 990) {
  422. clk = di->clk_ipu;
  423. clkgen0 = div << 4;
  424. } else {
  425. unsigned long in_rate;
  426. unsigned div;
  427. clk = di->clk_di;
  428. clk_set_rate(clk, sig->pixelclock);
  429. in_rate = clk_get_rate(clk);
  430. div = (in_rate + sig->pixelclock / 2) / sig->pixelclock;
  431. if (div == 0)
  432. div = 1;
  433. clkgen0 = div << 4;
  434. }
  435. }
  436. di->clk_di_pixel = clk;
  437. /* Set the divider */
  438. ipu_di_write(di, clkgen0, DI_BS_CLKGEN0);
  439. /*
  440. * Set the high/low periods. Bits 24:16 give us the falling edge,
  441. * and bits 8:0 give the rising edge. LSB is fraction, and is
  442. * based on the divider above. We want a 50% duty cycle, so set
  443. * the falling edge to be half the divider.
  444. */
  445. ipu_di_write(di, (clkgen0 >> 4) << 16, DI_BS_CLKGEN1);
  446. /* Finally select the input clock */
  447. val = ipu_di_read(di, DI_GENERAL) & ~DI_GEN_DI_CLK_EXT;
  448. if (clk == di->clk_di)
  449. val |= DI_GEN_DI_CLK_EXT;
  450. ipu_di_write(di, val, DI_GENERAL);
  451. dev_dbg(di->ipu->dev, "Want %luHz IPU %luHz DI %luHz using %s, %luHz\n",
  452. sig->pixelclock,
  453. clk_get_rate(di->clk_ipu),
  454. clk_get_rate(di->clk_di),
  455. clk == di->clk_di ? "DI" : "IPU",
  456. clk_get_rate(di->clk_di_pixel) / (clkgen0 >> 4));
  457. }
  458. int ipu_di_init_sync_panel(struct ipu_di *di, struct ipu_di_signal_cfg *sig)
  459. {
  460. u32 reg;
  461. u32 di_gen, vsync_cnt;
  462. u32 div;
  463. u32 h_total, v_total;
  464. dev_dbg(di->ipu->dev, "disp %d: panel size = %d x %d\n",
  465. di->id, sig->width, sig->height);
  466. if ((sig->v_sync_width == 0) || (sig->h_sync_width == 0))
  467. return -EINVAL;
  468. h_total = sig->width + sig->h_sync_width + sig->h_start_width +
  469. sig->h_end_width;
  470. v_total = sig->height + sig->v_sync_width + sig->v_start_width +
  471. sig->v_end_width;
  472. dev_dbg(di->ipu->dev, "Clocks: IPU %luHz DI %luHz Needed %luHz\n",
  473. clk_get_rate(di->clk_ipu),
  474. clk_get_rate(di->clk_di),
  475. sig->pixelclock);
  476. mutex_lock(&di_mutex);
  477. ipu_di_config_clock(di, sig);
  478. div = ipu_di_read(di, DI_BS_CLKGEN0) & 0xfff;
  479. div = div / 16; /* Now divider is integer portion */
  480. /* Setup pixel clock timing */
  481. /* Down time is half of period */
  482. ipu_di_write(di, (div << 16), DI_BS_CLKGEN1);
  483. ipu_di_data_wave_config(di, SYNC_WAVE, div - 1, div - 1);
  484. ipu_di_data_pin_config(di, SYNC_WAVE, DI_PIN15, 3, 0, div * 2);
  485. di_gen = ipu_di_read(di, DI_GENERAL) & DI_GEN_DI_CLK_EXT;
  486. di_gen |= DI_GEN_DI_VSYNC_EXT;
  487. if (sig->interlaced) {
  488. ipu_di_sync_config_interlaced(di, sig);
  489. /* set y_sel = 1 */
  490. di_gen |= 0x10000000;
  491. di_gen |= DI_GEN_POLARITY_5;
  492. di_gen |= DI_GEN_POLARITY_8;
  493. vsync_cnt = 7;
  494. if (sig->Hsync_pol)
  495. di_gen |= DI_GEN_POLARITY_3;
  496. if (sig->Vsync_pol)
  497. di_gen |= DI_GEN_POLARITY_2;
  498. } else {
  499. ipu_di_sync_config_noninterlaced(di, sig, div);
  500. vsync_cnt = 3;
  501. if (di->id == 1)
  502. /*
  503. * TODO: change only for TVEv2, parallel display
  504. * uses pin 2 / 3
  505. */
  506. if (!(sig->hsync_pin == 2 && sig->vsync_pin == 3))
  507. vsync_cnt = 6;
  508. if (sig->Hsync_pol) {
  509. if (sig->hsync_pin == 2)
  510. di_gen |= DI_GEN_POLARITY_2;
  511. else if (sig->hsync_pin == 4)
  512. di_gen |= DI_GEN_POLARITY_4;
  513. else if (sig->hsync_pin == 7)
  514. di_gen |= DI_GEN_POLARITY_7;
  515. }
  516. if (sig->Vsync_pol) {
  517. if (sig->vsync_pin == 3)
  518. di_gen |= DI_GEN_POLARITY_3;
  519. else if (sig->vsync_pin == 6)
  520. di_gen |= DI_GEN_POLARITY_6;
  521. else if (sig->vsync_pin == 8)
  522. di_gen |= DI_GEN_POLARITY_8;
  523. }
  524. }
  525. if (sig->clk_pol)
  526. di_gen |= DI_GEN_POLARITY_DISP_CLK;
  527. ipu_di_write(di, di_gen, DI_GENERAL);
  528. ipu_di_write(di, (--vsync_cnt << DI_VSYNC_SEL_OFFSET) | 0x00000002,
  529. DI_SYNC_AS_GEN);
  530. reg = ipu_di_read(di, DI_POL);
  531. reg &= ~(DI_POL_DRDY_DATA_POLARITY | DI_POL_DRDY_POLARITY_15);
  532. if (sig->enable_pol)
  533. reg |= DI_POL_DRDY_POLARITY_15;
  534. if (sig->data_pol)
  535. reg |= DI_POL_DRDY_DATA_POLARITY;
  536. ipu_di_write(di, reg, DI_POL);
  537. mutex_unlock(&di_mutex);
  538. return 0;
  539. }
  540. EXPORT_SYMBOL_GPL(ipu_di_init_sync_panel);
  541. int ipu_di_enable(struct ipu_di *di)
  542. {
  543. int ret;
  544. WARN_ON(IS_ERR(di->clk_di_pixel));
  545. ret = clk_prepare_enable(di->clk_di_pixel);
  546. if (ret)
  547. return ret;
  548. ipu_module_enable(di->ipu, di->module);
  549. return 0;
  550. }
  551. EXPORT_SYMBOL_GPL(ipu_di_enable);
  552. int ipu_di_disable(struct ipu_di *di)
  553. {
  554. WARN_ON(IS_ERR(di->clk_di_pixel));
  555. ipu_module_disable(di->ipu, di->module);
  556. clk_disable_unprepare(di->clk_di_pixel);
  557. return 0;
  558. }
  559. EXPORT_SYMBOL_GPL(ipu_di_disable);
  560. int ipu_di_get_num(struct ipu_di *di)
  561. {
  562. return di->id;
  563. }
  564. EXPORT_SYMBOL_GPL(ipu_di_get_num);
  565. static DEFINE_MUTEX(ipu_di_lock);
  566. struct ipu_di *ipu_di_get(struct ipu_soc *ipu, int disp)
  567. {
  568. struct ipu_di *di;
  569. if (disp > 1)
  570. return ERR_PTR(-EINVAL);
  571. di = ipu->di_priv[disp];
  572. mutex_lock(&ipu_di_lock);
  573. if (di->inuse) {
  574. di = ERR_PTR(-EBUSY);
  575. goto out;
  576. }
  577. di->inuse = true;
  578. out:
  579. mutex_unlock(&ipu_di_lock);
  580. return di;
  581. }
  582. EXPORT_SYMBOL_GPL(ipu_di_get);
  583. void ipu_di_put(struct ipu_di *di)
  584. {
  585. mutex_lock(&ipu_di_lock);
  586. di->inuse = false;
  587. mutex_unlock(&ipu_di_lock);
  588. }
  589. EXPORT_SYMBOL_GPL(ipu_di_put);
  590. int ipu_di_init(struct ipu_soc *ipu, struct device *dev, int id,
  591. unsigned long base,
  592. u32 module, struct clk *clk_ipu)
  593. {
  594. struct ipu_di *di;
  595. if (id > 1)
  596. return -ENODEV;
  597. di = devm_kzalloc(dev, sizeof(*di), GFP_KERNEL);
  598. if (!di)
  599. return -ENOMEM;
  600. ipu->di_priv[id] = di;
  601. di->clk_di = devm_clk_get(dev, id ? "di1" : "di0");
  602. if (IS_ERR(di->clk_di))
  603. return PTR_ERR(di->clk_di);
  604. di->module = module;
  605. di->id = id;
  606. di->clk_ipu = clk_ipu;
  607. di->base = devm_ioremap(dev, base, PAGE_SIZE);
  608. if (!di->base)
  609. return -ENOMEM;
  610. ipu_di_write(di, 0x10, DI_BS_CLKGEN0);
  611. dev_dbg(dev, "DI%d base: 0x%08lx remapped to %p\n",
  612. id, base, di->base);
  613. di->inuse = false;
  614. di->ipu = ipu;
  615. return 0;
  616. }
  617. void ipu_di_exit(struct ipu_soc *ipu, int id)
  618. {
  619. }