ipu-di.c 19 KB

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