ipu-dc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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/delay.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/io.h>
  22. #include <video/imx-ipu-v3.h>
  23. #include "ipu-prv.h"
  24. #define DC_MAP_CONF_PTR(n) (0x108 + ((n) & ~0x1) * 2)
  25. #define DC_MAP_CONF_VAL(n) (0x144 + ((n) & ~0x1) * 2)
  26. #define DC_EVT_NF 0
  27. #define DC_EVT_NL 1
  28. #define DC_EVT_EOF 2
  29. #define DC_EVT_NFIELD 3
  30. #define DC_EVT_EOL 4
  31. #define DC_EVT_EOFIELD 5
  32. #define DC_EVT_NEW_ADDR 6
  33. #define DC_EVT_NEW_CHAN 7
  34. #define DC_EVT_NEW_DATA 8
  35. #define DC_EVT_NEW_ADDR_W_0 0
  36. #define DC_EVT_NEW_ADDR_W_1 1
  37. #define DC_EVT_NEW_CHAN_W_0 2
  38. #define DC_EVT_NEW_CHAN_W_1 3
  39. #define DC_EVT_NEW_DATA_W_0 4
  40. #define DC_EVT_NEW_DATA_W_1 5
  41. #define DC_EVT_NEW_ADDR_R_0 6
  42. #define DC_EVT_NEW_ADDR_R_1 7
  43. #define DC_EVT_NEW_CHAN_R_0 8
  44. #define DC_EVT_NEW_CHAN_R_1 9
  45. #define DC_EVT_NEW_DATA_R_0 10
  46. #define DC_EVT_NEW_DATA_R_1 11
  47. #define DC_WR_CH_CONF 0x0
  48. #define DC_WR_CH_ADDR 0x4
  49. #define DC_RL_CH(evt) (8 + ((evt) & ~0x1) * 2)
  50. #define DC_GEN 0xd4
  51. #define DC_DISP_CONF1(disp) (0xd8 + (disp) * 4)
  52. #define DC_DISP_CONF2(disp) (0xe8 + (disp) * 4)
  53. #define DC_STAT 0x1c8
  54. #define WROD(lf) (0x18 | ((lf) << 1))
  55. #define WRG 0x01
  56. #define WCLK 0xc9
  57. #define SYNC_WAVE 0
  58. #define NULL_WAVE (-1)
  59. #define DC_GEN_SYNC_1_6_SYNC (2 << 1)
  60. #define DC_GEN_SYNC_PRIORITY_1 (1 << 7)
  61. #define DC_WR_CH_CONF_WORD_SIZE_8 (0 << 0)
  62. #define DC_WR_CH_CONF_WORD_SIZE_16 (1 << 0)
  63. #define DC_WR_CH_CONF_WORD_SIZE_24 (2 << 0)
  64. #define DC_WR_CH_CONF_WORD_SIZE_32 (3 << 0)
  65. #define DC_WR_CH_CONF_DISP_ID_PARALLEL(i) (((i) & 0x1) << 3)
  66. #define DC_WR_CH_CONF_DISP_ID_SERIAL (2 << 3)
  67. #define DC_WR_CH_CONF_DISP_ID_ASYNC (3 << 4)
  68. #define DC_WR_CH_CONF_FIELD_MODE (1 << 9)
  69. #define DC_WR_CH_CONF_PROG_TYPE_NORMAL (4 << 5)
  70. #define DC_WR_CH_CONF_PROG_TYPE_MASK (7 << 5)
  71. #define DC_WR_CH_CONF_PROG_DI_ID (1 << 2)
  72. #define DC_WR_CH_CONF_PROG_DISP_ID(i) (((i) & 0x1) << 3)
  73. #define IPU_DC_NUM_CHANNELS 10
  74. struct ipu_dc_priv;
  75. enum ipu_dc_map {
  76. IPU_DC_MAP_RGB24,
  77. IPU_DC_MAP_RGB565,
  78. IPU_DC_MAP_GBR24, /* TVEv2 */
  79. IPU_DC_MAP_BGR666,
  80. IPU_DC_MAP_LVDS666,
  81. IPU_DC_MAP_BGR24,
  82. };
  83. struct ipu_dc {
  84. /* The display interface number assigned to this dc channel */
  85. unsigned int di;
  86. void __iomem *base;
  87. struct ipu_dc_priv *priv;
  88. int chno;
  89. bool in_use;
  90. };
  91. struct ipu_dc_priv {
  92. void __iomem *dc_reg;
  93. void __iomem *dc_tmpl_reg;
  94. struct ipu_soc *ipu;
  95. struct device *dev;
  96. struct ipu_dc channels[IPU_DC_NUM_CHANNELS];
  97. struct mutex mutex;
  98. struct completion comp;
  99. int dc_irq;
  100. int dp_irq;
  101. int use_count;
  102. };
  103. static void dc_link_event(struct ipu_dc *dc, int event, int addr, int priority)
  104. {
  105. u32 reg;
  106. reg = readl(dc->base + DC_RL_CH(event));
  107. reg &= ~(0xffff << (16 * (event & 0x1)));
  108. reg |= ((addr << 8) | priority) << (16 * (event & 0x1));
  109. writel(reg, dc->base + DC_RL_CH(event));
  110. }
  111. static void dc_write_tmpl(struct ipu_dc *dc, int word, u32 opcode, u32 operand,
  112. int map, int wave, int glue, int sync, int stop)
  113. {
  114. struct ipu_dc_priv *priv = dc->priv;
  115. u32 reg1, reg2;
  116. if (opcode == WCLK) {
  117. reg1 = (operand << 20) & 0xfff00000;
  118. reg2 = operand >> 12 | opcode << 1 | stop << 9;
  119. } else if (opcode == WRG) {
  120. reg1 = sync | glue << 4 | ++wave << 11 | ((operand << 15) & 0xffff8000);
  121. reg2 = operand >> 17 | opcode << 7 | stop << 9;
  122. } else {
  123. reg1 = sync | glue << 4 | ++wave << 11 | ++map << 15 | ((operand << 20) & 0xfff00000);
  124. reg2 = operand >> 12 | opcode << 4 | stop << 9;
  125. }
  126. writel(reg1, priv->dc_tmpl_reg + word * 8);
  127. writel(reg2, priv->dc_tmpl_reg + word * 8 + 4);
  128. }
  129. static int ipu_bus_format_to_map(u32 fmt)
  130. {
  131. switch (fmt) {
  132. case MEDIA_BUS_FMT_RGB888_1X24:
  133. return IPU_DC_MAP_RGB24;
  134. case MEDIA_BUS_FMT_RGB565_1X16:
  135. return IPU_DC_MAP_RGB565;
  136. case MEDIA_BUS_FMT_GBR888_1X24:
  137. return IPU_DC_MAP_GBR24;
  138. case MEDIA_BUS_FMT_RGB666_1X18:
  139. return IPU_DC_MAP_BGR666;
  140. case MEDIA_BUS_FMT_RGB666_1X24_CPADHI:
  141. return IPU_DC_MAP_LVDS666;
  142. case MEDIA_BUS_FMT_BGR888_1X24:
  143. return IPU_DC_MAP_BGR24;
  144. default:
  145. return -EINVAL;
  146. }
  147. }
  148. int ipu_dc_init_sync(struct ipu_dc *dc, struct ipu_di *di, bool interlaced,
  149. u32 bus_format, u32 width)
  150. {
  151. struct ipu_dc_priv *priv = dc->priv;
  152. u32 reg = 0;
  153. int map;
  154. dc->di = ipu_di_get_num(di);
  155. map = ipu_bus_format_to_map(bus_format);
  156. if (map < 0) {
  157. dev_dbg(priv->dev, "IPU_DISP: No MAP\n");
  158. return map;
  159. }
  160. if (interlaced) {
  161. int addr;
  162. if (dc->di)
  163. addr = 1;
  164. else
  165. addr = 0;
  166. dc_link_event(dc, DC_EVT_NL, addr, 3);
  167. dc_link_event(dc, DC_EVT_EOL, addr, 2);
  168. dc_link_event(dc, DC_EVT_NEW_DATA, addr, 1);
  169. /* Init template microcode */
  170. dc_write_tmpl(dc, addr, WROD(0), 0, map, SYNC_WAVE, 0, 6, 1);
  171. } else {
  172. if (dc->di) {
  173. dc_link_event(dc, DC_EVT_NL, 2, 3);
  174. dc_link_event(dc, DC_EVT_EOL, 3, 2);
  175. dc_link_event(dc, DC_EVT_NEW_DATA, 1, 1);
  176. /* Init template microcode */
  177. dc_write_tmpl(dc, 2, WROD(0), 0, map, SYNC_WAVE, 8, 5, 1);
  178. dc_write_tmpl(dc, 3, WROD(0), 0, map, SYNC_WAVE, 4, 5, 0);
  179. dc_write_tmpl(dc, 4, WRG, 0, map, NULL_WAVE, 0, 0, 1);
  180. dc_write_tmpl(dc, 1, WROD(0), 0, map, SYNC_WAVE, 0, 5, 1);
  181. } else {
  182. dc_link_event(dc, DC_EVT_NL, 5, 3);
  183. dc_link_event(dc, DC_EVT_EOL, 6, 2);
  184. dc_link_event(dc, DC_EVT_NEW_DATA, 8, 1);
  185. /* Init template microcode */
  186. dc_write_tmpl(dc, 5, WROD(0), 0, map, SYNC_WAVE, 8, 5, 1);
  187. dc_write_tmpl(dc, 6, WROD(0), 0, map, SYNC_WAVE, 4, 5, 0);
  188. dc_write_tmpl(dc, 7, WRG, 0, map, NULL_WAVE, 0, 0, 1);
  189. dc_write_tmpl(dc, 8, WROD(0), 0, map, SYNC_WAVE, 0, 5, 1);
  190. }
  191. }
  192. dc_link_event(dc, DC_EVT_NF, 0, 0);
  193. dc_link_event(dc, DC_EVT_NFIELD, 0, 0);
  194. dc_link_event(dc, DC_EVT_EOF, 0, 0);
  195. dc_link_event(dc, DC_EVT_EOFIELD, 0, 0);
  196. dc_link_event(dc, DC_EVT_NEW_CHAN, 0, 0);
  197. dc_link_event(dc, DC_EVT_NEW_ADDR, 0, 0);
  198. reg = readl(dc->base + DC_WR_CH_CONF);
  199. if (interlaced)
  200. reg |= DC_WR_CH_CONF_FIELD_MODE;
  201. else
  202. reg &= ~DC_WR_CH_CONF_FIELD_MODE;
  203. writel(reg, dc->base + DC_WR_CH_CONF);
  204. writel(0x0, dc->base + DC_WR_CH_ADDR);
  205. writel(width, priv->dc_reg + DC_DISP_CONF2(dc->di));
  206. return 0;
  207. }
  208. EXPORT_SYMBOL_GPL(ipu_dc_init_sync);
  209. void ipu_dc_enable(struct ipu_soc *ipu)
  210. {
  211. struct ipu_dc_priv *priv = ipu->dc_priv;
  212. mutex_lock(&priv->mutex);
  213. if (!priv->use_count)
  214. ipu_module_enable(priv->ipu, IPU_CONF_DC_EN);
  215. priv->use_count++;
  216. mutex_unlock(&priv->mutex);
  217. }
  218. EXPORT_SYMBOL_GPL(ipu_dc_enable);
  219. void ipu_dc_enable_channel(struct ipu_dc *dc)
  220. {
  221. int di;
  222. u32 reg;
  223. di = dc->di;
  224. reg = readl(dc->base + DC_WR_CH_CONF);
  225. reg |= DC_WR_CH_CONF_PROG_TYPE_NORMAL;
  226. writel(reg, dc->base + DC_WR_CH_CONF);
  227. }
  228. EXPORT_SYMBOL_GPL(ipu_dc_enable_channel);
  229. static irqreturn_t dc_irq_handler(int irq, void *dev_id)
  230. {
  231. struct ipu_dc *dc = dev_id;
  232. u32 reg;
  233. reg = readl(dc->base + DC_WR_CH_CONF);
  234. reg &= ~DC_WR_CH_CONF_PROG_TYPE_MASK;
  235. writel(reg, dc->base + DC_WR_CH_CONF);
  236. /* The Freescale BSP kernel clears DIx_COUNTER_RELEASE here */
  237. complete(&dc->priv->comp);
  238. return IRQ_HANDLED;
  239. }
  240. void ipu_dc_disable_channel(struct ipu_dc *dc)
  241. {
  242. struct ipu_dc_priv *priv = dc->priv;
  243. int irq;
  244. unsigned long ret;
  245. u32 val;
  246. /* TODO: Handle MEM_FG_SYNC differently from MEM_BG_SYNC */
  247. if (dc->chno == 1)
  248. irq = priv->dc_irq;
  249. else if (dc->chno == 5)
  250. irq = priv->dp_irq;
  251. else
  252. return;
  253. init_completion(&priv->comp);
  254. enable_irq(irq);
  255. ret = wait_for_completion_timeout(&priv->comp, msecs_to_jiffies(50));
  256. disable_irq(irq);
  257. if (ret == 0) {
  258. dev_warn(priv->dev, "DC stop timeout after 50 ms\n");
  259. val = readl(dc->base + DC_WR_CH_CONF);
  260. val &= ~DC_WR_CH_CONF_PROG_TYPE_MASK;
  261. writel(val, dc->base + DC_WR_CH_CONF);
  262. }
  263. }
  264. EXPORT_SYMBOL_GPL(ipu_dc_disable_channel);
  265. void ipu_dc_disable(struct ipu_soc *ipu)
  266. {
  267. struct ipu_dc_priv *priv = ipu->dc_priv;
  268. mutex_lock(&priv->mutex);
  269. priv->use_count--;
  270. if (!priv->use_count)
  271. ipu_module_disable(priv->ipu, IPU_CONF_DC_EN);
  272. if (priv->use_count < 0)
  273. priv->use_count = 0;
  274. mutex_unlock(&priv->mutex);
  275. }
  276. EXPORT_SYMBOL_GPL(ipu_dc_disable);
  277. static void ipu_dc_map_config(struct ipu_dc_priv *priv, enum ipu_dc_map map,
  278. int byte_num, int offset, int mask)
  279. {
  280. int ptr = map * 3 + byte_num;
  281. u32 reg;
  282. reg = readl(priv->dc_reg + DC_MAP_CONF_VAL(ptr));
  283. reg &= ~(0xffff << (16 * (ptr & 0x1)));
  284. reg |= ((offset << 8) | mask) << (16 * (ptr & 0x1));
  285. writel(reg, priv->dc_reg + DC_MAP_CONF_VAL(ptr));
  286. reg = readl(priv->dc_reg + DC_MAP_CONF_PTR(map));
  287. reg &= ~(0x1f << ((16 * (map & 0x1)) + (5 * byte_num)));
  288. reg |= ptr << ((16 * (map & 0x1)) + (5 * byte_num));
  289. writel(reg, priv->dc_reg + DC_MAP_CONF_PTR(map));
  290. }
  291. static void ipu_dc_map_clear(struct ipu_dc_priv *priv, int map)
  292. {
  293. u32 reg = readl(priv->dc_reg + DC_MAP_CONF_PTR(map));
  294. writel(reg & ~(0xffff << (16 * (map & 0x1))),
  295. priv->dc_reg + DC_MAP_CONF_PTR(map));
  296. }
  297. struct ipu_dc *ipu_dc_get(struct ipu_soc *ipu, int channel)
  298. {
  299. struct ipu_dc_priv *priv = ipu->dc_priv;
  300. struct ipu_dc *dc;
  301. if (channel >= IPU_DC_NUM_CHANNELS)
  302. return ERR_PTR(-ENODEV);
  303. dc = &priv->channels[channel];
  304. mutex_lock(&priv->mutex);
  305. if (dc->in_use) {
  306. mutex_unlock(&priv->mutex);
  307. return ERR_PTR(-EBUSY);
  308. }
  309. dc->in_use = true;
  310. mutex_unlock(&priv->mutex);
  311. return dc;
  312. }
  313. EXPORT_SYMBOL_GPL(ipu_dc_get);
  314. void ipu_dc_put(struct ipu_dc *dc)
  315. {
  316. struct ipu_dc_priv *priv = dc->priv;
  317. mutex_lock(&priv->mutex);
  318. dc->in_use = false;
  319. mutex_unlock(&priv->mutex);
  320. }
  321. EXPORT_SYMBOL_GPL(ipu_dc_put);
  322. int ipu_dc_init(struct ipu_soc *ipu, struct device *dev,
  323. unsigned long base, unsigned long template_base)
  324. {
  325. struct ipu_dc_priv *priv;
  326. static int channel_offsets[] = { 0, 0x1c, 0x38, 0x54, 0x58, 0x5c,
  327. 0x78, 0, 0x94, 0xb4};
  328. int i, ret;
  329. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  330. if (!priv)
  331. return -ENOMEM;
  332. mutex_init(&priv->mutex);
  333. priv->dev = dev;
  334. priv->ipu = ipu;
  335. priv->dc_reg = devm_ioremap(dev, base, PAGE_SIZE);
  336. priv->dc_tmpl_reg = devm_ioremap(dev, template_base, PAGE_SIZE);
  337. if (!priv->dc_reg || !priv->dc_tmpl_reg)
  338. return -ENOMEM;
  339. for (i = 0; i < IPU_DC_NUM_CHANNELS; i++) {
  340. priv->channels[i].chno = i;
  341. priv->channels[i].priv = priv;
  342. priv->channels[i].base = priv->dc_reg + channel_offsets[i];
  343. }
  344. priv->dc_irq = ipu_map_irq(ipu, IPU_IRQ_DC_FC_1);
  345. if (!priv->dc_irq)
  346. return -EINVAL;
  347. ret = devm_request_irq(dev, priv->dc_irq, dc_irq_handler, 0, NULL,
  348. &priv->channels[1]);
  349. if (ret < 0)
  350. return ret;
  351. disable_irq(priv->dc_irq);
  352. priv->dp_irq = ipu_map_irq(ipu, IPU_IRQ_DP_SF_END);
  353. if (!priv->dp_irq)
  354. return -EINVAL;
  355. ret = devm_request_irq(dev, priv->dp_irq, dc_irq_handler, 0, NULL,
  356. &priv->channels[5]);
  357. if (ret < 0)
  358. return ret;
  359. disable_irq(priv->dp_irq);
  360. writel(DC_WR_CH_CONF_WORD_SIZE_24 | DC_WR_CH_CONF_DISP_ID_PARALLEL(1) |
  361. DC_WR_CH_CONF_PROG_DI_ID,
  362. priv->channels[1].base + DC_WR_CH_CONF);
  363. writel(DC_WR_CH_CONF_WORD_SIZE_24 | DC_WR_CH_CONF_DISP_ID_PARALLEL(0),
  364. priv->channels[5].base + DC_WR_CH_CONF);
  365. writel(DC_GEN_SYNC_1_6_SYNC | DC_GEN_SYNC_PRIORITY_1,
  366. priv->dc_reg + DC_GEN);
  367. ipu->dc_priv = priv;
  368. dev_dbg(dev, "DC base: 0x%08lx template base: 0x%08lx\n",
  369. base, template_base);
  370. /* rgb24 */
  371. ipu_dc_map_clear(priv, IPU_DC_MAP_RGB24);
  372. ipu_dc_map_config(priv, IPU_DC_MAP_RGB24, 0, 7, 0xff); /* blue */
  373. ipu_dc_map_config(priv, IPU_DC_MAP_RGB24, 1, 15, 0xff); /* green */
  374. ipu_dc_map_config(priv, IPU_DC_MAP_RGB24, 2, 23, 0xff); /* red */
  375. /* rgb565 */
  376. ipu_dc_map_clear(priv, IPU_DC_MAP_RGB565);
  377. ipu_dc_map_config(priv, IPU_DC_MAP_RGB565, 0, 4, 0xf8); /* blue */
  378. ipu_dc_map_config(priv, IPU_DC_MAP_RGB565, 1, 10, 0xfc); /* green */
  379. ipu_dc_map_config(priv, IPU_DC_MAP_RGB565, 2, 15, 0xf8); /* red */
  380. /* gbr24 */
  381. ipu_dc_map_clear(priv, IPU_DC_MAP_GBR24);
  382. ipu_dc_map_config(priv, IPU_DC_MAP_GBR24, 2, 15, 0xff); /* green */
  383. ipu_dc_map_config(priv, IPU_DC_MAP_GBR24, 1, 7, 0xff); /* blue */
  384. ipu_dc_map_config(priv, IPU_DC_MAP_GBR24, 0, 23, 0xff); /* red */
  385. /* bgr666 */
  386. ipu_dc_map_clear(priv, IPU_DC_MAP_BGR666);
  387. ipu_dc_map_config(priv, IPU_DC_MAP_BGR666, 0, 5, 0xfc); /* blue */
  388. ipu_dc_map_config(priv, IPU_DC_MAP_BGR666, 1, 11, 0xfc); /* green */
  389. ipu_dc_map_config(priv, IPU_DC_MAP_BGR666, 2, 17, 0xfc); /* red */
  390. /* lvds666 */
  391. ipu_dc_map_clear(priv, IPU_DC_MAP_LVDS666);
  392. ipu_dc_map_config(priv, IPU_DC_MAP_LVDS666, 0, 5, 0xfc); /* blue */
  393. ipu_dc_map_config(priv, IPU_DC_MAP_LVDS666, 1, 13, 0xfc); /* green */
  394. ipu_dc_map_config(priv, IPU_DC_MAP_LVDS666, 2, 21, 0xfc); /* red */
  395. /* bgr24 */
  396. ipu_dc_map_clear(priv, IPU_DC_MAP_BGR24);
  397. ipu_dc_map_config(priv, IPU_DC_MAP_BGR24, 2, 7, 0xff); /* red */
  398. ipu_dc_map_config(priv, IPU_DC_MAP_BGR24, 1, 15, 0xff); /* green */
  399. ipu_dc_map_config(priv, IPU_DC_MAP_BGR24, 0, 23, 0xff); /* blue */
  400. return 0;
  401. }
  402. void ipu_dc_exit(struct ipu_soc *ipu)
  403. {
  404. }