ipu-di.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  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->mode.hactive + sig->mode.hsync_len +
  181. sig->mode.hback_porch + sig->mode.hfront_porch;
  182. u32 v_total = sig->mode.vactive + sig->mode.vsync_len +
  183. sig->mode.vback_porch + sig->mode.vfront_porch;
  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->mode.vback_porch,
  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->mode.vactive / 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->mode.hback_porch,
  223. .offset_src = DI_SYNC_CLK,
  224. .repeat_count = sig->mode.hactive,
  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->mode.hactive + sig->mode.hsync_len +
  247. sig->mode.hback_porch + sig->mode.hfront_porch;
  248. u32 v_total = sig->mode.vactive + sig->mode.vsync_len +
  249. sig->mode.vback_porch + sig->mode.vfront_porch;
  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->mode.hsync_len * 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->mode.vsync_len * 2,
  271. } , {
  272. /* 4: Line Active */
  273. .run_src = DI_SYNC_HSYNC,
  274. .offset_count = sig->mode.vsync_len +
  275. sig->mode.vback_porch,
  276. .offset_src = DI_SYNC_HSYNC,
  277. .repeat_count = sig->mode.vactive,
  278. .cnt_clr_src = DI_SYNC_VSYNC,
  279. } , {
  280. /* 5: Pixel Active, referenced by DC */
  281. .run_src = DI_SYNC_CLK,
  282. .offset_count = sig->mode.hsync_len +
  283. sig->mode.hback_porch,
  284. .offset_src = DI_SYNC_CLK,
  285. .repeat_count = sig->mode.hactive,
  286. .cnt_clr_src = 5, /* Line Active */
  287. } , {
  288. /* unused */
  289. } , {
  290. /* unused */
  291. } , {
  292. /* unused */
  293. } , {
  294. /* unused */
  295. },
  296. };
  297. /* can't use #7 and #8 for line active and pixel active counters */
  298. struct di_sync_config cfg_vga[] = {
  299. {
  300. /* 1: INT_HSYNC */
  301. .run_count = h_total - 1,
  302. .run_src = DI_SYNC_CLK,
  303. } , {
  304. /* 2: VSYNC */
  305. .run_count = v_total - 1,
  306. .run_src = DI_SYNC_INT_HSYNC,
  307. } , {
  308. /* 3: Line Active */
  309. .run_src = DI_SYNC_INT_HSYNC,
  310. .offset_count = sig->mode.vsync_len +
  311. sig->mode.vback_porch,
  312. .offset_src = DI_SYNC_INT_HSYNC,
  313. .repeat_count = sig->mode.vactive,
  314. .cnt_clr_src = 3 /* VSYNC */,
  315. } , {
  316. /* PIN4: HSYNC for VGA via TVEv2 on TQ MBa53 */
  317. .run_count = h_total - 1,
  318. .run_src = DI_SYNC_CLK,
  319. .offset_count = div * sig->v_to_h_sync + 18, /* magic value from Freescale TVE driver */
  320. .offset_src = DI_SYNC_CLK,
  321. .cnt_polarity_gen_en = 1,
  322. .cnt_polarity_trigger_src = DI_SYNC_CLK,
  323. .cnt_down = sig->mode.hsync_len * 2,
  324. } , {
  325. /* 5: Pixel Active signal to DC */
  326. .run_src = DI_SYNC_CLK,
  327. .offset_count = sig->mode.hsync_len +
  328. sig->mode.hback_porch,
  329. .offset_src = DI_SYNC_CLK,
  330. .repeat_count = sig->mode.hactive,
  331. .cnt_clr_src = 4, /* Line Active */
  332. } , {
  333. /* PIN6: VSYNC for VGA via TVEv2 on TQ MBa53 */
  334. .run_count = v_total - 1,
  335. .run_src = DI_SYNC_INT_HSYNC,
  336. .offset_count = 1, /* magic value from Freescale TVE driver */
  337. .offset_src = DI_SYNC_INT_HSYNC,
  338. .cnt_polarity_gen_en = 1,
  339. .cnt_polarity_trigger_src = DI_SYNC_INT_HSYNC,
  340. .cnt_down = sig->mode.vsync_len * 2,
  341. } , {
  342. /* PIN4: HSYNC for VGA via TVEv2 on i.MX53-QSB */
  343. .run_count = h_total - 1,
  344. .run_src = DI_SYNC_CLK,
  345. .offset_count = div * sig->v_to_h_sync + 18, /* magic value from Freescale TVE driver */
  346. .offset_src = DI_SYNC_CLK,
  347. .cnt_polarity_gen_en = 1,
  348. .cnt_polarity_trigger_src = DI_SYNC_CLK,
  349. .cnt_down = sig->mode.hsync_len * 2,
  350. } , {
  351. /* PIN6: VSYNC for VGA via TVEv2 on i.MX53-QSB */
  352. .run_count = v_total - 1,
  353. .run_src = DI_SYNC_INT_HSYNC,
  354. .offset_count = 1, /* magic value from Freescale TVE driver */
  355. .offset_src = DI_SYNC_INT_HSYNC,
  356. .cnt_polarity_gen_en = 1,
  357. .cnt_polarity_trigger_src = DI_SYNC_INT_HSYNC,
  358. .cnt_down = sig->mode.vsync_len * 2,
  359. } , {
  360. /* unused */
  361. },
  362. };
  363. ipu_di_write(di, v_total - 1, DI_SCR_CONF);
  364. if (sig->hsync_pin == 2 && sig->vsync_pin == 3)
  365. ipu_di_sync_config(di, cfg, 0, ARRAY_SIZE(cfg));
  366. else
  367. ipu_di_sync_config(di, cfg_vga, 0, ARRAY_SIZE(cfg_vga));
  368. }
  369. static void ipu_di_config_clock(struct ipu_di *di,
  370. const struct ipu_di_signal_cfg *sig)
  371. {
  372. struct clk *clk;
  373. unsigned clkgen0;
  374. uint32_t val;
  375. if (sig->clkflags & IPU_DI_CLKMODE_EXT) {
  376. /*
  377. * CLKMODE_EXT means we must use the DI clock: this is
  378. * needed for things like LVDS which needs to feed the
  379. * DI and LDB with the same pixel clock.
  380. */
  381. clk = di->clk_di;
  382. if (sig->clkflags & IPU_DI_CLKMODE_SYNC) {
  383. /*
  384. * CLKMODE_SYNC means that we want the DI to be
  385. * clocked at the same rate as the parent clock.
  386. * This is needed (eg) for LDB which needs to be
  387. * fed with the same pixel clock. We assume that
  388. * the LDB clock has already been set correctly.
  389. */
  390. clkgen0 = 1 << 4;
  391. } else {
  392. /*
  393. * We can use the divider. We should really have
  394. * a flag here indicating whether the bridge can
  395. * cope with a fractional divider or not. For the
  396. * time being, let's go for simplicitly and
  397. * reliability.
  398. */
  399. unsigned long in_rate;
  400. unsigned div;
  401. clk_set_rate(clk, sig->mode.pixelclock);
  402. in_rate = clk_get_rate(clk);
  403. div = DIV_ROUND_CLOSEST(in_rate, sig->mode.pixelclock);
  404. div = clamp(div, 1U, 255U);
  405. clkgen0 = div << 4;
  406. }
  407. } else {
  408. /*
  409. * For other interfaces, we can arbitarily select between
  410. * the DI specific clock and the internal IPU clock. See
  411. * DI_GENERAL bit 20. We select the IPU clock if it can
  412. * give us a clock rate within 1% of the requested frequency,
  413. * otherwise we use the DI clock.
  414. */
  415. unsigned long rate, clkrate;
  416. unsigned div, error;
  417. clkrate = clk_get_rate(di->clk_ipu);
  418. div = DIV_ROUND_CLOSEST(clkrate, sig->mode.pixelclock);
  419. div = clamp(div, 1U, 255U);
  420. rate = clkrate / div;
  421. error = rate / (sig->mode.pixelclock / 1000);
  422. dev_dbg(di->ipu->dev, " IPU clock can give %lu with divider %u, error %d.%u%%\n",
  423. rate, div, (signed)(error - 1000) / 10, error % 10);
  424. /* Allow a 1% error */
  425. if (error < 1010 && error >= 990) {
  426. clk = di->clk_ipu;
  427. clkgen0 = div << 4;
  428. } else {
  429. unsigned long in_rate;
  430. unsigned div;
  431. clk = di->clk_di;
  432. clk_set_rate(clk, sig->mode.pixelclock);
  433. in_rate = clk_get_rate(clk);
  434. div = DIV_ROUND_CLOSEST(in_rate, sig->mode.pixelclock);
  435. div = clamp(div, 1U, 255U);
  436. clkgen0 = div << 4;
  437. }
  438. }
  439. di->clk_di_pixel = clk;
  440. /* Set the divider */
  441. ipu_di_write(di, clkgen0, DI_BS_CLKGEN0);
  442. /*
  443. * Set the high/low periods. Bits 24:16 give us the falling edge,
  444. * and bits 8:0 give the rising edge. LSB is fraction, and is
  445. * based on the divider above. We want a 50% duty cycle, so set
  446. * the falling edge to be half the divider.
  447. */
  448. ipu_di_write(di, (clkgen0 >> 4) << 16, DI_BS_CLKGEN1);
  449. /* Finally select the input clock */
  450. val = ipu_di_read(di, DI_GENERAL) & ~DI_GEN_DI_CLK_EXT;
  451. if (clk == di->clk_di)
  452. val |= DI_GEN_DI_CLK_EXT;
  453. ipu_di_write(di, val, DI_GENERAL);
  454. dev_dbg(di->ipu->dev, "Want %luHz IPU %luHz DI %luHz using %s, %luHz\n",
  455. sig->mode.pixelclock,
  456. clk_get_rate(di->clk_ipu),
  457. clk_get_rate(di->clk_di),
  458. clk == di->clk_di ? "DI" : "IPU",
  459. clk_get_rate(di->clk_di_pixel) / (clkgen0 >> 4));
  460. }
  461. /*
  462. * This function is called to adjust a video mode to IPU restrictions.
  463. * It is meant to be called from drm crtc mode_fixup() methods.
  464. */
  465. int ipu_di_adjust_videomode(struct ipu_di *di, struct videomode *mode)
  466. {
  467. u32 diff;
  468. if (mode->vfront_porch >= 2)
  469. return 0;
  470. diff = 2 - mode->vfront_porch;
  471. if (mode->vback_porch >= diff) {
  472. mode->vfront_porch = 2;
  473. mode->vback_porch -= diff;
  474. } else if (mode->vsync_len > diff) {
  475. mode->vfront_porch = 2;
  476. mode->vsync_len = mode->vsync_len - diff;
  477. } else {
  478. dev_warn(di->ipu->dev, "failed to adjust videomode\n");
  479. return -EINVAL;
  480. }
  481. dev_warn(di->ipu->dev, "videomode adapted for IPU restrictions\n");
  482. return 0;
  483. }
  484. EXPORT_SYMBOL_GPL(ipu_di_adjust_videomode);
  485. int ipu_di_init_sync_panel(struct ipu_di *di, struct ipu_di_signal_cfg *sig)
  486. {
  487. u32 reg;
  488. u32 di_gen, vsync_cnt;
  489. u32 div;
  490. dev_dbg(di->ipu->dev, "disp %d: panel size = %d x %d\n",
  491. di->id, sig->mode.hactive, sig->mode.vactive);
  492. if ((sig->mode.vsync_len == 0) || (sig->mode.hsync_len == 0))
  493. return -EINVAL;
  494. dev_dbg(di->ipu->dev, "Clocks: IPU %luHz DI %luHz Needed %luHz\n",
  495. clk_get_rate(di->clk_ipu),
  496. clk_get_rate(di->clk_di),
  497. sig->mode.pixelclock);
  498. mutex_lock(&di_mutex);
  499. ipu_di_config_clock(di, sig);
  500. div = ipu_di_read(di, DI_BS_CLKGEN0) & 0xfff;
  501. div = div / 16; /* Now divider is integer portion */
  502. /* Setup pixel clock timing */
  503. /* Down time is half of period */
  504. ipu_di_write(di, (div << 16), DI_BS_CLKGEN1);
  505. ipu_di_data_wave_config(di, SYNC_WAVE, div - 1, div - 1);
  506. ipu_di_data_pin_config(di, SYNC_WAVE, DI_PIN15, 3, 0, div * 2);
  507. di_gen = ipu_di_read(di, DI_GENERAL) & DI_GEN_DI_CLK_EXT;
  508. di_gen |= DI_GEN_DI_VSYNC_EXT;
  509. if (sig->mode.flags & DISPLAY_FLAGS_INTERLACED) {
  510. ipu_di_sync_config_interlaced(di, sig);
  511. /* set y_sel = 1 */
  512. di_gen |= 0x10000000;
  513. di_gen |= DI_GEN_POLARITY_5;
  514. di_gen |= DI_GEN_POLARITY_8;
  515. vsync_cnt = 7;
  516. if (sig->mode.flags & DISPLAY_FLAGS_HSYNC_HIGH)
  517. di_gen |= DI_GEN_POLARITY_3;
  518. if (sig->mode.flags & DISPLAY_FLAGS_VSYNC_HIGH)
  519. di_gen |= DI_GEN_POLARITY_2;
  520. } else {
  521. ipu_di_sync_config_noninterlaced(di, sig, div);
  522. vsync_cnt = 3;
  523. if (di->id == 1)
  524. /*
  525. * TODO: change only for TVEv2, parallel display
  526. * uses pin 2 / 3
  527. */
  528. if (!(sig->hsync_pin == 2 && sig->vsync_pin == 3))
  529. vsync_cnt = 6;
  530. if (sig->mode.flags & DISPLAY_FLAGS_HSYNC_HIGH) {
  531. if (sig->hsync_pin == 2)
  532. di_gen |= DI_GEN_POLARITY_2;
  533. else if (sig->hsync_pin == 4)
  534. di_gen |= DI_GEN_POLARITY_4;
  535. else if (sig->hsync_pin == 7)
  536. di_gen |= DI_GEN_POLARITY_7;
  537. }
  538. if (sig->mode.flags & DISPLAY_FLAGS_VSYNC_HIGH) {
  539. if (sig->vsync_pin == 3)
  540. di_gen |= DI_GEN_POLARITY_3;
  541. else if (sig->vsync_pin == 6)
  542. di_gen |= DI_GEN_POLARITY_6;
  543. else if (sig->vsync_pin == 8)
  544. di_gen |= DI_GEN_POLARITY_8;
  545. }
  546. }
  547. if (sig->clk_pol)
  548. di_gen |= DI_GEN_POLARITY_DISP_CLK;
  549. ipu_di_write(di, di_gen, DI_GENERAL);
  550. ipu_di_write(di, (--vsync_cnt << DI_VSYNC_SEL_OFFSET) | 0x00000002,
  551. DI_SYNC_AS_GEN);
  552. reg = ipu_di_read(di, DI_POL);
  553. reg &= ~(DI_POL_DRDY_DATA_POLARITY | DI_POL_DRDY_POLARITY_15);
  554. if (sig->enable_pol)
  555. reg |= DI_POL_DRDY_POLARITY_15;
  556. if (sig->data_pol)
  557. reg |= DI_POL_DRDY_DATA_POLARITY;
  558. ipu_di_write(di, reg, DI_POL);
  559. mutex_unlock(&di_mutex);
  560. return 0;
  561. }
  562. EXPORT_SYMBOL_GPL(ipu_di_init_sync_panel);
  563. int ipu_di_enable(struct ipu_di *di)
  564. {
  565. int ret;
  566. WARN_ON(IS_ERR(di->clk_di_pixel));
  567. ret = clk_prepare_enable(di->clk_di_pixel);
  568. if (ret)
  569. return ret;
  570. ipu_module_enable(di->ipu, di->module);
  571. return 0;
  572. }
  573. EXPORT_SYMBOL_GPL(ipu_di_enable);
  574. int ipu_di_disable(struct ipu_di *di)
  575. {
  576. WARN_ON(IS_ERR(di->clk_di_pixel));
  577. ipu_module_disable(di->ipu, di->module);
  578. clk_disable_unprepare(di->clk_di_pixel);
  579. return 0;
  580. }
  581. EXPORT_SYMBOL_GPL(ipu_di_disable);
  582. int ipu_di_get_num(struct ipu_di *di)
  583. {
  584. return di->id;
  585. }
  586. EXPORT_SYMBOL_GPL(ipu_di_get_num);
  587. static DEFINE_MUTEX(ipu_di_lock);
  588. struct ipu_di *ipu_di_get(struct ipu_soc *ipu, int disp)
  589. {
  590. struct ipu_di *di;
  591. if (disp > 1)
  592. return ERR_PTR(-EINVAL);
  593. di = ipu->di_priv[disp];
  594. mutex_lock(&ipu_di_lock);
  595. if (di->inuse) {
  596. di = ERR_PTR(-EBUSY);
  597. goto out;
  598. }
  599. di->inuse = true;
  600. out:
  601. mutex_unlock(&ipu_di_lock);
  602. return di;
  603. }
  604. EXPORT_SYMBOL_GPL(ipu_di_get);
  605. void ipu_di_put(struct ipu_di *di)
  606. {
  607. mutex_lock(&ipu_di_lock);
  608. di->inuse = false;
  609. mutex_unlock(&ipu_di_lock);
  610. }
  611. EXPORT_SYMBOL_GPL(ipu_di_put);
  612. int ipu_di_init(struct ipu_soc *ipu, struct device *dev, int id,
  613. unsigned long base,
  614. u32 module, struct clk *clk_ipu)
  615. {
  616. struct ipu_di *di;
  617. if (id > 1)
  618. return -ENODEV;
  619. di = devm_kzalloc(dev, sizeof(*di), GFP_KERNEL);
  620. if (!di)
  621. return -ENOMEM;
  622. ipu->di_priv[id] = di;
  623. di->clk_di = devm_clk_get(dev, id ? "di1" : "di0");
  624. if (IS_ERR(di->clk_di))
  625. return PTR_ERR(di->clk_di);
  626. di->module = module;
  627. di->id = id;
  628. di->clk_ipu = clk_ipu;
  629. di->base = devm_ioremap(dev, base, PAGE_SIZE);
  630. if (!di->base)
  631. return -ENOMEM;
  632. ipu_di_write(di, 0x10, DI_BS_CLKGEN0);
  633. dev_dbg(dev, "DI%d base: 0x%08lx remapped to %p\n",
  634. id, base, di->base);
  635. di->inuse = false;
  636. di->ipu = ipu;
  637. return 0;
  638. }
  639. void ipu_di_exit(struct ipu_soc *ipu, int id)
  640. {
  641. }