ipu-common.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  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/module.h>
  16. #include <linux/export.h>
  17. #include <linux/types.h>
  18. #include <linux/reset.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/err.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/delay.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/io.h>
  25. #include <linux/clk.h>
  26. #include <linux/list.h>
  27. #include <linux/irq.h>
  28. #include <linux/irqchip/chained_irq.h>
  29. #include <linux/irqdomain.h>
  30. #include <linux/of_device.h>
  31. #include <drm/drm_fourcc.h>
  32. #include <video/imx-ipu-v3.h>
  33. #include "ipu-prv.h"
  34. static inline u32 ipu_cm_read(struct ipu_soc *ipu, unsigned offset)
  35. {
  36. return readl(ipu->cm_reg + offset);
  37. }
  38. static inline void ipu_cm_write(struct ipu_soc *ipu, u32 value, unsigned offset)
  39. {
  40. writel(value, ipu->cm_reg + offset);
  41. }
  42. void ipu_srm_dp_sync_update(struct ipu_soc *ipu)
  43. {
  44. u32 val;
  45. val = ipu_cm_read(ipu, IPU_SRM_PRI2);
  46. val |= 0x8;
  47. ipu_cm_write(ipu, val, IPU_SRM_PRI2);
  48. }
  49. EXPORT_SYMBOL_GPL(ipu_srm_dp_sync_update);
  50. enum ipu_color_space ipu_drm_fourcc_to_colorspace(u32 drm_fourcc)
  51. {
  52. switch (drm_fourcc) {
  53. case DRM_FORMAT_RGB565:
  54. case DRM_FORMAT_BGR565:
  55. case DRM_FORMAT_RGB888:
  56. case DRM_FORMAT_BGR888:
  57. case DRM_FORMAT_XRGB8888:
  58. case DRM_FORMAT_XBGR8888:
  59. case DRM_FORMAT_RGBX8888:
  60. case DRM_FORMAT_BGRX8888:
  61. case DRM_FORMAT_ARGB8888:
  62. case DRM_FORMAT_ABGR8888:
  63. case DRM_FORMAT_RGBA8888:
  64. case DRM_FORMAT_BGRA8888:
  65. return IPUV3_COLORSPACE_RGB;
  66. case DRM_FORMAT_YUYV:
  67. case DRM_FORMAT_UYVY:
  68. case DRM_FORMAT_YUV420:
  69. case DRM_FORMAT_YVU420:
  70. return IPUV3_COLORSPACE_YUV;
  71. default:
  72. return IPUV3_COLORSPACE_UNKNOWN;
  73. }
  74. }
  75. EXPORT_SYMBOL_GPL(ipu_drm_fourcc_to_colorspace);
  76. enum ipu_color_space ipu_pixelformat_to_colorspace(u32 pixelformat)
  77. {
  78. switch (pixelformat) {
  79. case V4L2_PIX_FMT_YUV420:
  80. case V4L2_PIX_FMT_YVU420:
  81. case V4L2_PIX_FMT_UYVY:
  82. case V4L2_PIX_FMT_YUYV:
  83. return IPUV3_COLORSPACE_YUV;
  84. case V4L2_PIX_FMT_RGB32:
  85. case V4L2_PIX_FMT_BGR32:
  86. case V4L2_PIX_FMT_RGB24:
  87. case V4L2_PIX_FMT_BGR24:
  88. case V4L2_PIX_FMT_RGB565:
  89. return IPUV3_COLORSPACE_RGB;
  90. default:
  91. return IPUV3_COLORSPACE_UNKNOWN;
  92. }
  93. }
  94. EXPORT_SYMBOL_GPL(ipu_pixelformat_to_colorspace);
  95. struct ipuv3_channel *ipu_idmac_get(struct ipu_soc *ipu, unsigned num)
  96. {
  97. struct ipuv3_channel *channel;
  98. dev_dbg(ipu->dev, "%s %d\n", __func__, num);
  99. if (num > 63)
  100. return ERR_PTR(-ENODEV);
  101. mutex_lock(&ipu->channel_lock);
  102. channel = &ipu->channel[num];
  103. if (channel->busy) {
  104. channel = ERR_PTR(-EBUSY);
  105. goto out;
  106. }
  107. channel->busy = true;
  108. channel->num = num;
  109. out:
  110. mutex_unlock(&ipu->channel_lock);
  111. return channel;
  112. }
  113. EXPORT_SYMBOL_GPL(ipu_idmac_get);
  114. void ipu_idmac_put(struct ipuv3_channel *channel)
  115. {
  116. struct ipu_soc *ipu = channel->ipu;
  117. dev_dbg(ipu->dev, "%s %d\n", __func__, channel->num);
  118. mutex_lock(&ipu->channel_lock);
  119. channel->busy = false;
  120. mutex_unlock(&ipu->channel_lock);
  121. }
  122. EXPORT_SYMBOL_GPL(ipu_idmac_put);
  123. #define idma_mask(ch) (1 << (ch & 0x1f))
  124. void ipu_idmac_set_double_buffer(struct ipuv3_channel *channel,
  125. bool doublebuffer)
  126. {
  127. struct ipu_soc *ipu = channel->ipu;
  128. unsigned long flags;
  129. u32 reg;
  130. spin_lock_irqsave(&ipu->lock, flags);
  131. reg = ipu_cm_read(ipu, IPU_CHA_DB_MODE_SEL(channel->num));
  132. if (doublebuffer)
  133. reg |= idma_mask(channel->num);
  134. else
  135. reg &= ~idma_mask(channel->num);
  136. ipu_cm_write(ipu, reg, IPU_CHA_DB_MODE_SEL(channel->num));
  137. spin_unlock_irqrestore(&ipu->lock, flags);
  138. }
  139. EXPORT_SYMBOL_GPL(ipu_idmac_set_double_buffer);
  140. int ipu_module_enable(struct ipu_soc *ipu, u32 mask)
  141. {
  142. unsigned long lock_flags;
  143. u32 val;
  144. spin_lock_irqsave(&ipu->lock, lock_flags);
  145. val = ipu_cm_read(ipu, IPU_DISP_GEN);
  146. if (mask & IPU_CONF_DI0_EN)
  147. val |= IPU_DI0_COUNTER_RELEASE;
  148. if (mask & IPU_CONF_DI1_EN)
  149. val |= IPU_DI1_COUNTER_RELEASE;
  150. ipu_cm_write(ipu, val, IPU_DISP_GEN);
  151. val = ipu_cm_read(ipu, IPU_CONF);
  152. val |= mask;
  153. ipu_cm_write(ipu, val, IPU_CONF);
  154. spin_unlock_irqrestore(&ipu->lock, lock_flags);
  155. return 0;
  156. }
  157. EXPORT_SYMBOL_GPL(ipu_module_enable);
  158. int ipu_module_disable(struct ipu_soc *ipu, u32 mask)
  159. {
  160. unsigned long lock_flags;
  161. u32 val;
  162. spin_lock_irqsave(&ipu->lock, lock_flags);
  163. val = ipu_cm_read(ipu, IPU_CONF);
  164. val &= ~mask;
  165. ipu_cm_write(ipu, val, IPU_CONF);
  166. val = ipu_cm_read(ipu, IPU_DISP_GEN);
  167. if (mask & IPU_CONF_DI0_EN)
  168. val &= ~IPU_DI0_COUNTER_RELEASE;
  169. if (mask & IPU_CONF_DI1_EN)
  170. val &= ~IPU_DI1_COUNTER_RELEASE;
  171. ipu_cm_write(ipu, val, IPU_DISP_GEN);
  172. spin_unlock_irqrestore(&ipu->lock, lock_flags);
  173. return 0;
  174. }
  175. EXPORT_SYMBOL_GPL(ipu_module_disable);
  176. int ipu_csi_enable(struct ipu_soc *ipu, int csi)
  177. {
  178. return ipu_module_enable(ipu, csi ? IPU_CONF_CSI1_EN : IPU_CONF_CSI0_EN);
  179. }
  180. EXPORT_SYMBOL_GPL(ipu_csi_enable);
  181. int ipu_csi_disable(struct ipu_soc *ipu, int csi)
  182. {
  183. return ipu_module_disable(ipu, csi ? IPU_CONF_CSI1_EN : IPU_CONF_CSI0_EN);
  184. }
  185. EXPORT_SYMBOL_GPL(ipu_csi_disable);
  186. int ipu_smfc_enable(struct ipu_soc *ipu)
  187. {
  188. return ipu_module_enable(ipu, IPU_CONF_SMFC_EN);
  189. }
  190. EXPORT_SYMBOL_GPL(ipu_smfc_enable);
  191. int ipu_smfc_disable(struct ipu_soc *ipu)
  192. {
  193. return ipu_module_disable(ipu, IPU_CONF_SMFC_EN);
  194. }
  195. EXPORT_SYMBOL_GPL(ipu_smfc_disable);
  196. int ipu_idmac_get_current_buffer(struct ipuv3_channel *channel)
  197. {
  198. struct ipu_soc *ipu = channel->ipu;
  199. unsigned int chno = channel->num;
  200. return (ipu_cm_read(ipu, IPU_CHA_CUR_BUF(chno)) & idma_mask(chno)) ? 1 : 0;
  201. }
  202. EXPORT_SYMBOL_GPL(ipu_idmac_get_current_buffer);
  203. void ipu_idmac_select_buffer(struct ipuv3_channel *channel, u32 buf_num)
  204. {
  205. struct ipu_soc *ipu = channel->ipu;
  206. unsigned int chno = channel->num;
  207. unsigned long flags;
  208. spin_lock_irqsave(&ipu->lock, flags);
  209. /* Mark buffer as ready. */
  210. if (buf_num == 0)
  211. ipu_cm_write(ipu, idma_mask(chno), IPU_CHA_BUF0_RDY(chno));
  212. else
  213. ipu_cm_write(ipu, idma_mask(chno), IPU_CHA_BUF1_RDY(chno));
  214. spin_unlock_irqrestore(&ipu->lock, flags);
  215. }
  216. EXPORT_SYMBOL_GPL(ipu_idmac_select_buffer);
  217. int ipu_idmac_enable_channel(struct ipuv3_channel *channel)
  218. {
  219. struct ipu_soc *ipu = channel->ipu;
  220. u32 val;
  221. unsigned long flags;
  222. spin_lock_irqsave(&ipu->lock, flags);
  223. val = ipu_idmac_read(ipu, IDMAC_CHA_EN(channel->num));
  224. val |= idma_mask(channel->num);
  225. ipu_idmac_write(ipu, val, IDMAC_CHA_EN(channel->num));
  226. spin_unlock_irqrestore(&ipu->lock, flags);
  227. return 0;
  228. }
  229. EXPORT_SYMBOL_GPL(ipu_idmac_enable_channel);
  230. bool ipu_idmac_channel_busy(struct ipu_soc *ipu, unsigned int chno)
  231. {
  232. return (ipu_idmac_read(ipu, IDMAC_CHA_BUSY(chno)) & idma_mask(chno));
  233. }
  234. EXPORT_SYMBOL_GPL(ipu_idmac_channel_busy);
  235. int ipu_idmac_wait_busy(struct ipuv3_channel *channel, int ms)
  236. {
  237. struct ipu_soc *ipu = channel->ipu;
  238. unsigned long timeout;
  239. timeout = jiffies + msecs_to_jiffies(ms);
  240. while (ipu_idmac_read(ipu, IDMAC_CHA_BUSY(channel->num)) &
  241. idma_mask(channel->num)) {
  242. if (time_after(jiffies, timeout))
  243. return -ETIMEDOUT;
  244. cpu_relax();
  245. }
  246. return 0;
  247. }
  248. EXPORT_SYMBOL_GPL(ipu_idmac_wait_busy);
  249. int ipu_wait_interrupt(struct ipu_soc *ipu, int irq, int ms)
  250. {
  251. unsigned long timeout;
  252. timeout = jiffies + msecs_to_jiffies(ms);
  253. ipu_cm_write(ipu, BIT(irq % 32), IPU_INT_STAT(irq / 32));
  254. while (!(ipu_cm_read(ipu, IPU_INT_STAT(irq / 32) & BIT(irq % 32)))) {
  255. if (time_after(jiffies, timeout))
  256. return -ETIMEDOUT;
  257. cpu_relax();
  258. }
  259. return 0;
  260. }
  261. EXPORT_SYMBOL_GPL(ipu_wait_interrupt);
  262. int ipu_idmac_disable_channel(struct ipuv3_channel *channel)
  263. {
  264. struct ipu_soc *ipu = channel->ipu;
  265. u32 val;
  266. unsigned long flags;
  267. spin_lock_irqsave(&ipu->lock, flags);
  268. /* Disable DMA channel(s) */
  269. val = ipu_idmac_read(ipu, IDMAC_CHA_EN(channel->num));
  270. val &= ~idma_mask(channel->num);
  271. ipu_idmac_write(ipu, val, IDMAC_CHA_EN(channel->num));
  272. /* Set channel buffers NOT to be ready */
  273. ipu_cm_write(ipu, 0xf0000000, IPU_GPR); /* write one to clear */
  274. if (ipu_cm_read(ipu, IPU_CHA_BUF0_RDY(channel->num)) &
  275. idma_mask(channel->num)) {
  276. ipu_cm_write(ipu, idma_mask(channel->num),
  277. IPU_CHA_BUF0_RDY(channel->num));
  278. }
  279. if (ipu_cm_read(ipu, IPU_CHA_BUF1_RDY(channel->num)) &
  280. idma_mask(channel->num)) {
  281. ipu_cm_write(ipu, idma_mask(channel->num),
  282. IPU_CHA_BUF1_RDY(channel->num));
  283. }
  284. ipu_cm_write(ipu, 0x0, IPU_GPR); /* write one to set */
  285. /* Reset the double buffer */
  286. val = ipu_cm_read(ipu, IPU_CHA_DB_MODE_SEL(channel->num));
  287. val &= ~idma_mask(channel->num);
  288. ipu_cm_write(ipu, val, IPU_CHA_DB_MODE_SEL(channel->num));
  289. spin_unlock_irqrestore(&ipu->lock, flags);
  290. return 0;
  291. }
  292. EXPORT_SYMBOL_GPL(ipu_idmac_disable_channel);
  293. static int ipu_memory_reset(struct ipu_soc *ipu)
  294. {
  295. unsigned long timeout;
  296. ipu_cm_write(ipu, 0x807FFFFF, IPU_MEM_RST);
  297. timeout = jiffies + msecs_to_jiffies(1000);
  298. while (ipu_cm_read(ipu, IPU_MEM_RST) & 0x80000000) {
  299. if (time_after(jiffies, timeout))
  300. return -ETIME;
  301. cpu_relax();
  302. }
  303. return 0;
  304. }
  305. struct ipu_devtype {
  306. const char *name;
  307. unsigned long cm_ofs;
  308. unsigned long cpmem_ofs;
  309. unsigned long srm_ofs;
  310. unsigned long tpm_ofs;
  311. unsigned long disp0_ofs;
  312. unsigned long disp1_ofs;
  313. unsigned long dc_tmpl_ofs;
  314. unsigned long vdi_ofs;
  315. enum ipuv3_type type;
  316. };
  317. static struct ipu_devtype ipu_type_imx51 = {
  318. .name = "IPUv3EX",
  319. .cm_ofs = 0x1e000000,
  320. .cpmem_ofs = 0x1f000000,
  321. .srm_ofs = 0x1f040000,
  322. .tpm_ofs = 0x1f060000,
  323. .disp0_ofs = 0x1e040000,
  324. .disp1_ofs = 0x1e048000,
  325. .dc_tmpl_ofs = 0x1f080000,
  326. .vdi_ofs = 0x1e068000,
  327. .type = IPUV3EX,
  328. };
  329. static struct ipu_devtype ipu_type_imx53 = {
  330. .name = "IPUv3M",
  331. .cm_ofs = 0x06000000,
  332. .cpmem_ofs = 0x07000000,
  333. .srm_ofs = 0x07040000,
  334. .tpm_ofs = 0x07060000,
  335. .disp0_ofs = 0x06040000,
  336. .disp1_ofs = 0x06048000,
  337. .dc_tmpl_ofs = 0x07080000,
  338. .vdi_ofs = 0x06068000,
  339. .type = IPUV3M,
  340. };
  341. static struct ipu_devtype ipu_type_imx6q = {
  342. .name = "IPUv3H",
  343. .cm_ofs = 0x00200000,
  344. .cpmem_ofs = 0x00300000,
  345. .srm_ofs = 0x00340000,
  346. .tpm_ofs = 0x00360000,
  347. .disp0_ofs = 0x00240000,
  348. .disp1_ofs = 0x00248000,
  349. .dc_tmpl_ofs = 0x00380000,
  350. .vdi_ofs = 0x00268000,
  351. .type = IPUV3H,
  352. };
  353. static const struct of_device_id imx_ipu_dt_ids[] = {
  354. { .compatible = "fsl,imx51-ipu", .data = &ipu_type_imx51, },
  355. { .compatible = "fsl,imx53-ipu", .data = &ipu_type_imx53, },
  356. { .compatible = "fsl,imx6q-ipu", .data = &ipu_type_imx6q, },
  357. { /* sentinel */ }
  358. };
  359. MODULE_DEVICE_TABLE(of, imx_ipu_dt_ids);
  360. static int ipu_submodules_init(struct ipu_soc *ipu,
  361. struct platform_device *pdev, unsigned long ipu_base,
  362. struct clk *ipu_clk)
  363. {
  364. char *unit;
  365. int ret;
  366. struct device *dev = &pdev->dev;
  367. const struct ipu_devtype *devtype = ipu->devtype;
  368. ret = ipu_cpmem_init(ipu, dev, ipu_base + devtype->cpmem_ofs);
  369. if (ret) {
  370. unit = "cpmem";
  371. goto err_cpmem;
  372. }
  373. ret = ipu_di_init(ipu, dev, 0, ipu_base + devtype->disp0_ofs,
  374. IPU_CONF_DI0_EN, ipu_clk);
  375. if (ret) {
  376. unit = "di0";
  377. goto err_di_0;
  378. }
  379. ret = ipu_di_init(ipu, dev, 1, ipu_base + devtype->disp1_ofs,
  380. IPU_CONF_DI1_EN, ipu_clk);
  381. if (ret) {
  382. unit = "di1";
  383. goto err_di_1;
  384. }
  385. ret = ipu_dc_init(ipu, dev, ipu_base + devtype->cm_ofs +
  386. IPU_CM_DC_REG_OFS, ipu_base + devtype->dc_tmpl_ofs);
  387. if (ret) {
  388. unit = "dc_template";
  389. goto err_dc;
  390. }
  391. ret = ipu_dmfc_init(ipu, dev, ipu_base +
  392. devtype->cm_ofs + IPU_CM_DMFC_REG_OFS, ipu_clk);
  393. if (ret) {
  394. unit = "dmfc";
  395. goto err_dmfc;
  396. }
  397. ret = ipu_dp_init(ipu, dev, ipu_base + devtype->srm_ofs);
  398. if (ret) {
  399. unit = "dp";
  400. goto err_dp;
  401. }
  402. ret = ipu_smfc_init(ipu, dev, ipu_base +
  403. devtype->cm_ofs + IPU_CM_SMFC_REG_OFS);
  404. if (ret) {
  405. unit = "smfc";
  406. goto err_smfc;
  407. }
  408. return 0;
  409. err_smfc:
  410. ipu_dp_exit(ipu);
  411. err_dp:
  412. ipu_dmfc_exit(ipu);
  413. err_dmfc:
  414. ipu_dc_exit(ipu);
  415. err_dc:
  416. ipu_di_exit(ipu, 1);
  417. err_di_1:
  418. ipu_di_exit(ipu, 0);
  419. err_di_0:
  420. ipu_cpmem_exit(ipu);
  421. err_cpmem:
  422. dev_err(&pdev->dev, "init %s failed with %d\n", unit, ret);
  423. return ret;
  424. }
  425. static void ipu_irq_handle(struct ipu_soc *ipu, const int *regs, int num_regs)
  426. {
  427. unsigned long status;
  428. int i, bit, irq;
  429. for (i = 0; i < num_regs; i++) {
  430. status = ipu_cm_read(ipu, IPU_INT_STAT(regs[i]));
  431. status &= ipu_cm_read(ipu, IPU_INT_CTRL(regs[i]));
  432. for_each_set_bit(bit, &status, 32) {
  433. irq = irq_linear_revmap(ipu->domain,
  434. regs[i] * 32 + bit);
  435. if (irq)
  436. generic_handle_irq(irq);
  437. }
  438. }
  439. }
  440. static void ipu_irq_handler(unsigned int irq, struct irq_desc *desc)
  441. {
  442. struct ipu_soc *ipu = irq_desc_get_handler_data(desc);
  443. const int int_reg[] = { 0, 1, 2, 3, 10, 11, 12, 13, 14};
  444. struct irq_chip *chip = irq_get_chip(irq);
  445. chained_irq_enter(chip, desc);
  446. ipu_irq_handle(ipu, int_reg, ARRAY_SIZE(int_reg));
  447. chained_irq_exit(chip, desc);
  448. }
  449. static void ipu_err_irq_handler(unsigned int irq, struct irq_desc *desc)
  450. {
  451. struct ipu_soc *ipu = irq_desc_get_handler_data(desc);
  452. const int int_reg[] = { 4, 5, 8, 9};
  453. struct irq_chip *chip = irq_get_chip(irq);
  454. chained_irq_enter(chip, desc);
  455. ipu_irq_handle(ipu, int_reg, ARRAY_SIZE(int_reg));
  456. chained_irq_exit(chip, desc);
  457. }
  458. int ipu_map_irq(struct ipu_soc *ipu, int irq)
  459. {
  460. int virq;
  461. virq = irq_linear_revmap(ipu->domain, irq);
  462. if (!virq)
  463. virq = irq_create_mapping(ipu->domain, irq);
  464. return virq;
  465. }
  466. EXPORT_SYMBOL_GPL(ipu_map_irq);
  467. int ipu_idmac_channel_irq(struct ipu_soc *ipu, struct ipuv3_channel *channel,
  468. enum ipu_channel_irq irq_type)
  469. {
  470. return ipu_map_irq(ipu, irq_type + channel->num);
  471. }
  472. EXPORT_SYMBOL_GPL(ipu_idmac_channel_irq);
  473. static void ipu_submodules_exit(struct ipu_soc *ipu)
  474. {
  475. ipu_smfc_exit(ipu);
  476. ipu_dp_exit(ipu);
  477. ipu_dmfc_exit(ipu);
  478. ipu_dc_exit(ipu);
  479. ipu_di_exit(ipu, 1);
  480. ipu_di_exit(ipu, 0);
  481. ipu_cpmem_exit(ipu);
  482. }
  483. static int platform_remove_devices_fn(struct device *dev, void *unused)
  484. {
  485. struct platform_device *pdev = to_platform_device(dev);
  486. platform_device_unregister(pdev);
  487. return 0;
  488. }
  489. static void platform_device_unregister_children(struct platform_device *pdev)
  490. {
  491. device_for_each_child(&pdev->dev, NULL, platform_remove_devices_fn);
  492. }
  493. struct ipu_platform_reg {
  494. struct ipu_client_platformdata pdata;
  495. const char *name;
  496. int reg_offset;
  497. };
  498. static const struct ipu_platform_reg client_reg[] = {
  499. {
  500. .pdata = {
  501. .di = 0,
  502. .dc = 5,
  503. .dp = IPU_DP_FLOW_SYNC_BG,
  504. .dma[0] = IPUV3_CHANNEL_MEM_BG_SYNC,
  505. .dma[1] = IPUV3_CHANNEL_MEM_FG_SYNC,
  506. },
  507. .name = "imx-ipuv3-crtc",
  508. }, {
  509. .pdata = {
  510. .di = 1,
  511. .dc = 1,
  512. .dp = -EINVAL,
  513. .dma[0] = IPUV3_CHANNEL_MEM_DC_SYNC,
  514. .dma[1] = -EINVAL,
  515. },
  516. .name = "imx-ipuv3-crtc",
  517. }, {
  518. .pdata = {
  519. .csi = 0,
  520. .dma[0] = IPUV3_CHANNEL_CSI0,
  521. .dma[1] = -EINVAL,
  522. },
  523. .reg_offset = IPU_CM_CSI0_REG_OFS,
  524. .name = "imx-ipuv3-camera",
  525. }, {
  526. .pdata = {
  527. .csi = 1,
  528. .dma[0] = IPUV3_CHANNEL_CSI1,
  529. .dma[1] = -EINVAL,
  530. },
  531. .reg_offset = IPU_CM_CSI1_REG_OFS,
  532. .name = "imx-ipuv3-camera",
  533. },
  534. };
  535. static DEFINE_MUTEX(ipu_client_id_mutex);
  536. static int ipu_client_id;
  537. static int ipu_add_client_devices(struct ipu_soc *ipu, unsigned long ipu_base)
  538. {
  539. struct device *dev = ipu->dev;
  540. unsigned i;
  541. int id, ret;
  542. mutex_lock(&ipu_client_id_mutex);
  543. id = ipu_client_id;
  544. ipu_client_id += ARRAY_SIZE(client_reg);
  545. mutex_unlock(&ipu_client_id_mutex);
  546. for (i = 0; i < ARRAY_SIZE(client_reg); i++) {
  547. const struct ipu_platform_reg *reg = &client_reg[i];
  548. struct platform_device *pdev;
  549. struct resource res;
  550. if (reg->reg_offset) {
  551. memset(&res, 0, sizeof(res));
  552. res.flags = IORESOURCE_MEM;
  553. res.start = ipu_base + ipu->devtype->cm_ofs + reg->reg_offset;
  554. res.end = res.start + PAGE_SIZE - 1;
  555. pdev = platform_device_register_resndata(dev, reg->name,
  556. id++, &res, 1, &reg->pdata, sizeof(reg->pdata));
  557. } else {
  558. pdev = platform_device_register_data(dev, reg->name,
  559. id++, &reg->pdata, sizeof(reg->pdata));
  560. }
  561. if (IS_ERR(pdev))
  562. goto err_register;
  563. }
  564. return 0;
  565. err_register:
  566. platform_device_unregister_children(to_platform_device(dev));
  567. return ret;
  568. }
  569. static int ipu_irq_init(struct ipu_soc *ipu)
  570. {
  571. struct irq_chip_generic *gc;
  572. struct irq_chip_type *ct;
  573. unsigned long unused[IPU_NUM_IRQS / 32] = {
  574. 0x400100d0, 0xffe000fd,
  575. 0x400100d0, 0xffe000fd,
  576. 0x400100d0, 0xffe000fd,
  577. 0x4077ffff, 0xffe7e1fd,
  578. 0x23fffffe, 0x8880fff0,
  579. 0xf98fe7d0, 0xfff81fff,
  580. 0x400100d0, 0xffe000fd,
  581. 0x00000000,
  582. };
  583. int ret, i;
  584. ipu->domain = irq_domain_add_linear(ipu->dev->of_node, IPU_NUM_IRQS,
  585. &irq_generic_chip_ops, ipu);
  586. if (!ipu->domain) {
  587. dev_err(ipu->dev, "failed to add irq domain\n");
  588. return -ENODEV;
  589. }
  590. ret = irq_alloc_domain_generic_chips(ipu->domain, 32, 1, "IPU",
  591. handle_level_irq, 0,
  592. IRQF_VALID, 0);
  593. if (ret < 0) {
  594. dev_err(ipu->dev, "failed to alloc generic irq chips\n");
  595. irq_domain_remove(ipu->domain);
  596. return ret;
  597. }
  598. for (i = 0; i < IPU_NUM_IRQS; i += 32) {
  599. gc = irq_get_domain_generic_chip(ipu->domain, i);
  600. gc->reg_base = ipu->cm_reg;
  601. gc->unused = unused[i / 32];
  602. ct = gc->chip_types;
  603. ct->chip.irq_ack = irq_gc_ack_set_bit;
  604. ct->chip.irq_mask = irq_gc_mask_clr_bit;
  605. ct->chip.irq_unmask = irq_gc_mask_set_bit;
  606. ct->regs.ack = IPU_INT_STAT(i / 32);
  607. ct->regs.mask = IPU_INT_CTRL(i / 32);
  608. }
  609. irq_set_chained_handler(ipu->irq_sync, ipu_irq_handler);
  610. irq_set_handler_data(ipu->irq_sync, ipu);
  611. irq_set_chained_handler(ipu->irq_err, ipu_err_irq_handler);
  612. irq_set_handler_data(ipu->irq_err, ipu);
  613. return 0;
  614. }
  615. static void ipu_irq_exit(struct ipu_soc *ipu)
  616. {
  617. int i, irq;
  618. irq_set_chained_handler(ipu->irq_err, NULL);
  619. irq_set_handler_data(ipu->irq_err, NULL);
  620. irq_set_chained_handler(ipu->irq_sync, NULL);
  621. irq_set_handler_data(ipu->irq_sync, NULL);
  622. /* TODO: remove irq_domain_generic_chips */
  623. for (i = 0; i < IPU_NUM_IRQS; i++) {
  624. irq = irq_linear_revmap(ipu->domain, i);
  625. if (irq)
  626. irq_dispose_mapping(irq);
  627. }
  628. irq_domain_remove(ipu->domain);
  629. }
  630. static int ipu_probe(struct platform_device *pdev)
  631. {
  632. const struct of_device_id *of_id =
  633. of_match_device(imx_ipu_dt_ids, &pdev->dev);
  634. struct ipu_soc *ipu;
  635. struct resource *res;
  636. unsigned long ipu_base;
  637. int i, ret, irq_sync, irq_err;
  638. const struct ipu_devtype *devtype;
  639. devtype = of_id->data;
  640. irq_sync = platform_get_irq(pdev, 0);
  641. irq_err = platform_get_irq(pdev, 1);
  642. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  643. dev_dbg(&pdev->dev, "irq_sync: %d irq_err: %d\n",
  644. irq_sync, irq_err);
  645. if (!res || irq_sync < 0 || irq_err < 0)
  646. return -ENODEV;
  647. ipu_base = res->start;
  648. ipu = devm_kzalloc(&pdev->dev, sizeof(*ipu), GFP_KERNEL);
  649. if (!ipu)
  650. return -ENODEV;
  651. for (i = 0; i < 64; i++)
  652. ipu->channel[i].ipu = ipu;
  653. ipu->devtype = devtype;
  654. ipu->ipu_type = devtype->type;
  655. spin_lock_init(&ipu->lock);
  656. mutex_init(&ipu->channel_lock);
  657. dev_dbg(&pdev->dev, "cm_reg: 0x%08lx\n",
  658. ipu_base + devtype->cm_ofs);
  659. dev_dbg(&pdev->dev, "idmac: 0x%08lx\n",
  660. ipu_base + devtype->cm_ofs + IPU_CM_IDMAC_REG_OFS);
  661. dev_dbg(&pdev->dev, "cpmem: 0x%08lx\n",
  662. ipu_base + devtype->cpmem_ofs);
  663. dev_dbg(&pdev->dev, "disp0: 0x%08lx\n",
  664. ipu_base + devtype->disp0_ofs);
  665. dev_dbg(&pdev->dev, "disp1: 0x%08lx\n",
  666. ipu_base + devtype->disp1_ofs);
  667. dev_dbg(&pdev->dev, "srm: 0x%08lx\n",
  668. ipu_base + devtype->srm_ofs);
  669. dev_dbg(&pdev->dev, "tpm: 0x%08lx\n",
  670. ipu_base + devtype->tpm_ofs);
  671. dev_dbg(&pdev->dev, "dc: 0x%08lx\n",
  672. ipu_base + devtype->cm_ofs + IPU_CM_DC_REG_OFS);
  673. dev_dbg(&pdev->dev, "ic: 0x%08lx\n",
  674. ipu_base + devtype->cm_ofs + IPU_CM_IC_REG_OFS);
  675. dev_dbg(&pdev->dev, "dmfc: 0x%08lx\n",
  676. ipu_base + devtype->cm_ofs + IPU_CM_DMFC_REG_OFS);
  677. dev_dbg(&pdev->dev, "vdi: 0x%08lx\n",
  678. ipu_base + devtype->vdi_ofs);
  679. ipu->cm_reg = devm_ioremap(&pdev->dev,
  680. ipu_base + devtype->cm_ofs, PAGE_SIZE);
  681. ipu->idmac_reg = devm_ioremap(&pdev->dev,
  682. ipu_base + devtype->cm_ofs + IPU_CM_IDMAC_REG_OFS,
  683. PAGE_SIZE);
  684. if (!ipu->cm_reg || !ipu->idmac_reg)
  685. return -ENOMEM;
  686. ipu->clk = devm_clk_get(&pdev->dev, "bus");
  687. if (IS_ERR(ipu->clk)) {
  688. ret = PTR_ERR(ipu->clk);
  689. dev_err(&pdev->dev, "clk_get failed with %d", ret);
  690. return ret;
  691. }
  692. platform_set_drvdata(pdev, ipu);
  693. ret = clk_prepare_enable(ipu->clk);
  694. if (ret) {
  695. dev_err(&pdev->dev, "clk_prepare_enable failed: %d\n", ret);
  696. return ret;
  697. }
  698. ipu->dev = &pdev->dev;
  699. ipu->irq_sync = irq_sync;
  700. ipu->irq_err = irq_err;
  701. ret = ipu_irq_init(ipu);
  702. if (ret)
  703. goto out_failed_irq;
  704. ret = device_reset(&pdev->dev);
  705. if (ret) {
  706. dev_err(&pdev->dev, "failed to reset: %d\n", ret);
  707. goto out_failed_reset;
  708. }
  709. ret = ipu_memory_reset(ipu);
  710. if (ret)
  711. goto out_failed_reset;
  712. /* Set MCU_T to divide MCU access window into 2 */
  713. ipu_cm_write(ipu, 0x00400000L | (IPU_MCU_T_DEFAULT << 18),
  714. IPU_DISP_GEN);
  715. ret = ipu_submodules_init(ipu, pdev, ipu_base, ipu->clk);
  716. if (ret)
  717. goto failed_submodules_init;
  718. ret = ipu_add_client_devices(ipu, ipu_base);
  719. if (ret) {
  720. dev_err(&pdev->dev, "adding client devices failed with %d\n",
  721. ret);
  722. goto failed_add_clients;
  723. }
  724. dev_info(&pdev->dev, "%s probed\n", devtype->name);
  725. return 0;
  726. failed_add_clients:
  727. ipu_submodules_exit(ipu);
  728. failed_submodules_init:
  729. out_failed_reset:
  730. ipu_irq_exit(ipu);
  731. out_failed_irq:
  732. clk_disable_unprepare(ipu->clk);
  733. return ret;
  734. }
  735. static int ipu_remove(struct platform_device *pdev)
  736. {
  737. struct ipu_soc *ipu = platform_get_drvdata(pdev);
  738. platform_device_unregister_children(pdev);
  739. ipu_submodules_exit(ipu);
  740. ipu_irq_exit(ipu);
  741. clk_disable_unprepare(ipu->clk);
  742. return 0;
  743. }
  744. static struct platform_driver imx_ipu_driver = {
  745. .driver = {
  746. .name = "imx-ipuv3",
  747. .of_match_table = imx_ipu_dt_ids,
  748. },
  749. .probe = ipu_probe,
  750. .remove = ipu_remove,
  751. };
  752. module_platform_driver(imx_ipu_driver);
  753. MODULE_ALIAS("platform:imx-ipuv3");
  754. MODULE_DESCRIPTION("i.MX IPU v3 driver");
  755. MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
  756. MODULE_LICENSE("GPL");