ipu-di.c 19 KB

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