ipu-prg.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. /*
  2. * Copyright (c) 2016-2017 Lucas Stach, Pengutronix
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. */
  13. #include <drm/drm_fourcc.h>
  14. #include <linux/clk.h>
  15. #include <linux/err.h>
  16. #include <linux/iopoll.h>
  17. #include <linux/mfd/syscon.h>
  18. #include <linux/mfd/syscon/imx6q-iomuxc-gpr.h>
  19. #include <linux/module.h>
  20. #include <linux/of.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/regmap.h>
  23. #include <video/imx-ipu-v3.h>
  24. #include "ipu-prv.h"
  25. #define IPU_PRG_CTL 0x00
  26. #define IPU_PRG_CTL_BYPASS(i) (1 << (0 + i))
  27. #define IPU_PRG_CTL_SOFT_ARID_MASK 0x3
  28. #define IPU_PRG_CTL_SOFT_ARID_SHIFT(i) (8 + i * 2)
  29. #define IPU_PRG_CTL_SOFT_ARID(i, v) ((v & 0x3) << (8 + 2 * i))
  30. #define IPU_PRG_CTL_SO(i) (1 << (16 + i))
  31. #define IPU_PRG_CTL_VFLIP(i) (1 << (19 + i))
  32. #define IPU_PRG_CTL_BLOCK_MODE(i) (1 << (22 + i))
  33. #define IPU_PRG_CTL_CNT_LOAD_EN(i) (1 << (25 + i))
  34. #define IPU_PRG_CTL_SOFTRST (1 << 30)
  35. #define IPU_PRG_CTL_SHADOW_EN (1 << 31)
  36. #define IPU_PRG_STATUS 0x04
  37. #define IPU_PRG_STATUS_BUFFER0_READY(i) (1 << (0 + i * 2))
  38. #define IPU_PRG_STATUS_BUFFER1_READY(i) (1 << (1 + i * 2))
  39. #define IPU_PRG_QOS 0x08
  40. #define IPU_PRG_QOS_ARID_MASK 0xf
  41. #define IPU_PRG_QOS_ARID_SHIFT(i) (0 + i * 4)
  42. #define IPU_PRG_REG_UPDATE 0x0c
  43. #define IPU_PRG_REG_UPDATE_REG_UPDATE (1 << 0)
  44. #define IPU_PRG_STRIDE(i) (0x10 + i * 0x4)
  45. #define IPU_PRG_STRIDE_STRIDE_MASK 0x3fff
  46. #define IPU_PRG_CROP_LINE 0x1c
  47. #define IPU_PRG_THD 0x20
  48. #define IPU_PRG_BADDR(i) (0x24 + i * 0x4)
  49. #define IPU_PRG_OFFSET(i) (0x30 + i * 0x4)
  50. #define IPU_PRG_ILO(i) (0x3c + i * 0x4)
  51. #define IPU_PRG_HEIGHT(i) (0x48 + i * 0x4)
  52. #define IPU_PRG_HEIGHT_PRE_HEIGHT_MASK 0xfff
  53. #define IPU_PRG_HEIGHT_PRE_HEIGHT_SHIFT 0
  54. #define IPU_PRG_HEIGHT_IPU_HEIGHT_MASK 0xfff
  55. #define IPU_PRG_HEIGHT_IPU_HEIGHT_SHIFT 16
  56. struct ipu_prg_channel {
  57. bool enabled;
  58. int used_pre;
  59. };
  60. struct ipu_prg {
  61. struct list_head list;
  62. struct device *dev;
  63. int id;
  64. void __iomem *regs;
  65. struct clk *clk_ipg, *clk_axi;
  66. struct regmap *iomuxc_gpr;
  67. struct ipu_pre *pres[3];
  68. struct ipu_prg_channel chan[3];
  69. };
  70. static DEFINE_MUTEX(ipu_prg_list_mutex);
  71. static LIST_HEAD(ipu_prg_list);
  72. struct ipu_prg *
  73. ipu_prg_lookup_by_phandle(struct device *dev, const char *name, int ipu_id)
  74. {
  75. struct device_node *prg_node = of_parse_phandle(dev->of_node,
  76. name, 0);
  77. struct ipu_prg *prg;
  78. mutex_lock(&ipu_prg_list_mutex);
  79. list_for_each_entry(prg, &ipu_prg_list, list) {
  80. if (prg_node == prg->dev->of_node) {
  81. mutex_unlock(&ipu_prg_list_mutex);
  82. device_link_add(dev, prg->dev, DL_FLAG_AUTOREMOVE);
  83. prg->id = ipu_id;
  84. return prg;
  85. }
  86. }
  87. mutex_unlock(&ipu_prg_list_mutex);
  88. return NULL;
  89. }
  90. int ipu_prg_max_active_channels(void)
  91. {
  92. return ipu_pre_get_available_count();
  93. }
  94. EXPORT_SYMBOL_GPL(ipu_prg_max_active_channels);
  95. bool ipu_prg_present(struct ipu_soc *ipu)
  96. {
  97. if (ipu->prg_priv)
  98. return true;
  99. return false;
  100. }
  101. EXPORT_SYMBOL_GPL(ipu_prg_present);
  102. bool ipu_prg_format_supported(struct ipu_soc *ipu, uint32_t format,
  103. uint64_t modifier)
  104. {
  105. const struct drm_format_info *info = drm_format_info(format);
  106. if (info->num_planes != 1)
  107. return false;
  108. return true;
  109. }
  110. EXPORT_SYMBOL_GPL(ipu_prg_format_supported);
  111. int ipu_prg_enable(struct ipu_soc *ipu)
  112. {
  113. struct ipu_prg *prg = ipu->prg_priv;
  114. int ret;
  115. if (!prg)
  116. return 0;
  117. ret = clk_prepare_enable(prg->clk_axi);
  118. if (ret)
  119. goto fail_disable_ipg;
  120. return 0;
  121. fail_disable_ipg:
  122. clk_disable_unprepare(prg->clk_ipg);
  123. return ret;
  124. }
  125. EXPORT_SYMBOL_GPL(ipu_prg_enable);
  126. void ipu_prg_disable(struct ipu_soc *ipu)
  127. {
  128. struct ipu_prg *prg = ipu->prg_priv;
  129. if (!prg)
  130. return;
  131. clk_disable_unprepare(prg->clk_axi);
  132. }
  133. EXPORT_SYMBOL_GPL(ipu_prg_disable);
  134. /*
  135. * The channel configuartion functions below are not thread safe, as they
  136. * must be only called from the atomic commit path in the DRM driver, which
  137. * is properly serialized.
  138. */
  139. static int ipu_prg_ipu_to_prg_chan(int ipu_chan)
  140. {
  141. /*
  142. * This isn't clearly documented in the RM, but IPU to PRG channel
  143. * assignment is fixed, as only with this mapping the control signals
  144. * match up.
  145. */
  146. switch (ipu_chan) {
  147. case IPUV3_CHANNEL_MEM_BG_SYNC:
  148. return 0;
  149. case IPUV3_CHANNEL_MEM_FG_SYNC:
  150. return 1;
  151. case IPUV3_CHANNEL_MEM_DC_SYNC:
  152. return 2;
  153. default:
  154. return -EINVAL;
  155. }
  156. }
  157. static int ipu_prg_get_pre(struct ipu_prg *prg, int prg_chan)
  158. {
  159. int i, ret;
  160. /* channel 0 is special as it is hardwired to one of the PREs */
  161. if (prg_chan == 0) {
  162. ret = ipu_pre_get(prg->pres[0]);
  163. if (ret)
  164. goto fail;
  165. prg->chan[prg_chan].used_pre = 0;
  166. return 0;
  167. }
  168. for (i = 1; i < 3; i++) {
  169. ret = ipu_pre_get(prg->pres[i]);
  170. if (!ret) {
  171. u32 val, mux;
  172. int shift;
  173. prg->chan[prg_chan].used_pre = i;
  174. /* configure the PRE to PRG channel mux */
  175. shift = (i == 1) ? 12 : 14;
  176. mux = (prg->id << 1) | (prg_chan - 1);
  177. regmap_update_bits(prg->iomuxc_gpr, IOMUXC_GPR5,
  178. 0x3 << shift, mux << shift);
  179. /* check other mux, must not point to same channel */
  180. shift = (i == 1) ? 14 : 12;
  181. regmap_read(prg->iomuxc_gpr, IOMUXC_GPR5, &val);
  182. if (((val >> shift) & 0x3) == mux) {
  183. regmap_update_bits(prg->iomuxc_gpr, IOMUXC_GPR5,
  184. 0x3 << shift,
  185. (mux ^ 0x1) << shift);
  186. }
  187. return 0;
  188. }
  189. }
  190. fail:
  191. dev_err(prg->dev, "could not get PRE for PRG chan %d", prg_chan);
  192. return ret;
  193. }
  194. static void ipu_prg_put_pre(struct ipu_prg *prg, int prg_chan)
  195. {
  196. struct ipu_prg_channel *chan = &prg->chan[prg_chan];
  197. ipu_pre_put(prg->pres[chan->used_pre]);
  198. chan->used_pre = -1;
  199. }
  200. void ipu_prg_channel_disable(struct ipuv3_channel *ipu_chan)
  201. {
  202. int prg_chan = ipu_prg_ipu_to_prg_chan(ipu_chan->num);
  203. struct ipu_prg *prg = ipu_chan->ipu->prg_priv;
  204. struct ipu_prg_channel *chan = &prg->chan[prg_chan];
  205. u32 val;
  206. if (!chan->enabled || prg_chan < 0)
  207. return;
  208. clk_prepare_enable(prg->clk_ipg);
  209. val = readl(prg->regs + IPU_PRG_CTL);
  210. val |= IPU_PRG_CTL_BYPASS(prg_chan);
  211. writel(val, prg->regs + IPU_PRG_CTL);
  212. val = IPU_PRG_REG_UPDATE_REG_UPDATE;
  213. writel(val, prg->regs + IPU_PRG_REG_UPDATE);
  214. clk_disable_unprepare(prg->clk_ipg);
  215. ipu_prg_put_pre(prg, prg_chan);
  216. chan->enabled = false;
  217. }
  218. EXPORT_SYMBOL_GPL(ipu_prg_channel_disable);
  219. int ipu_prg_channel_configure(struct ipuv3_channel *ipu_chan,
  220. unsigned int axi_id, unsigned int width,
  221. unsigned int height, unsigned int stride,
  222. u32 format, unsigned long *eba)
  223. {
  224. int prg_chan = ipu_prg_ipu_to_prg_chan(ipu_chan->num);
  225. struct ipu_prg *prg = ipu_chan->ipu->prg_priv;
  226. struct ipu_prg_channel *chan = &prg->chan[prg_chan];
  227. u32 val;
  228. int ret;
  229. if (prg_chan < 0)
  230. return prg_chan;
  231. if (chan->enabled) {
  232. ipu_pre_update(prg->pres[chan->used_pre], *eba);
  233. return 0;
  234. }
  235. ret = ipu_prg_get_pre(prg, prg_chan);
  236. if (ret)
  237. return ret;
  238. ipu_pre_configure(prg->pres[chan->used_pre],
  239. width, height, stride, format, *eba);
  240. ret = clk_prepare_enable(prg->clk_ipg);
  241. if (ret) {
  242. ipu_prg_put_pre(prg, prg_chan);
  243. return ret;
  244. }
  245. val = (stride - 1) & IPU_PRG_STRIDE_STRIDE_MASK;
  246. writel(val, prg->regs + IPU_PRG_STRIDE(prg_chan));
  247. val = ((height & IPU_PRG_HEIGHT_PRE_HEIGHT_MASK) <<
  248. IPU_PRG_HEIGHT_PRE_HEIGHT_SHIFT) |
  249. ((height & IPU_PRG_HEIGHT_IPU_HEIGHT_MASK) <<
  250. IPU_PRG_HEIGHT_IPU_HEIGHT_SHIFT);
  251. writel(val, prg->regs + IPU_PRG_HEIGHT(prg_chan));
  252. val = ipu_pre_get_baddr(prg->pres[chan->used_pre]);
  253. *eba = val;
  254. writel(val, prg->regs + IPU_PRG_BADDR(prg_chan));
  255. val = readl(prg->regs + IPU_PRG_CTL);
  256. /* config AXI ID */
  257. val &= ~(IPU_PRG_CTL_SOFT_ARID_MASK <<
  258. IPU_PRG_CTL_SOFT_ARID_SHIFT(prg_chan));
  259. val |= IPU_PRG_CTL_SOFT_ARID(prg_chan, axi_id);
  260. /* enable channel */
  261. val &= ~IPU_PRG_CTL_BYPASS(prg_chan);
  262. writel(val, prg->regs + IPU_PRG_CTL);
  263. val = IPU_PRG_REG_UPDATE_REG_UPDATE;
  264. writel(val, prg->regs + IPU_PRG_REG_UPDATE);
  265. /* wait for both double buffers to be filled */
  266. readl_poll_timeout(prg->regs + IPU_PRG_STATUS, val,
  267. (val & IPU_PRG_STATUS_BUFFER0_READY(prg_chan)) &&
  268. (val & IPU_PRG_STATUS_BUFFER1_READY(prg_chan)),
  269. 5, 1000);
  270. clk_disable_unprepare(prg->clk_ipg);
  271. chan->enabled = true;
  272. return 0;
  273. }
  274. EXPORT_SYMBOL_GPL(ipu_prg_channel_configure);
  275. static int ipu_prg_probe(struct platform_device *pdev)
  276. {
  277. struct device *dev = &pdev->dev;
  278. struct resource *res;
  279. struct ipu_prg *prg;
  280. u32 val;
  281. int i, ret;
  282. prg = devm_kzalloc(dev, sizeof(*prg), GFP_KERNEL);
  283. if (!prg)
  284. return -ENOMEM;
  285. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  286. prg->regs = devm_ioremap_resource(&pdev->dev, res);
  287. if (IS_ERR(prg->regs))
  288. return PTR_ERR(prg->regs);
  289. prg->clk_ipg = devm_clk_get(dev, "ipg");
  290. if (IS_ERR(prg->clk_ipg))
  291. return PTR_ERR(prg->clk_ipg);
  292. prg->clk_axi = devm_clk_get(dev, "axi");
  293. if (IS_ERR(prg->clk_axi))
  294. return PTR_ERR(prg->clk_axi);
  295. prg->iomuxc_gpr =
  296. syscon_regmap_lookup_by_compatible("fsl,imx6q-iomuxc-gpr");
  297. if (IS_ERR(prg->iomuxc_gpr))
  298. return PTR_ERR(prg->iomuxc_gpr);
  299. for (i = 0; i < 3; i++) {
  300. prg->pres[i] = ipu_pre_lookup_by_phandle(dev, "fsl,pres", i);
  301. if (!prg->pres[i])
  302. return -EPROBE_DEFER;
  303. }
  304. ret = clk_prepare_enable(prg->clk_ipg);
  305. if (ret)
  306. return ret;
  307. /* init to free running mode */
  308. val = readl(prg->regs + IPU_PRG_CTL);
  309. val |= IPU_PRG_CTL_SHADOW_EN;
  310. writel(val, prg->regs + IPU_PRG_CTL);
  311. /* disable address threshold */
  312. writel(0xffffffff, prg->regs + IPU_PRG_THD);
  313. clk_disable_unprepare(prg->clk_ipg);
  314. prg->dev = dev;
  315. platform_set_drvdata(pdev, prg);
  316. mutex_lock(&ipu_prg_list_mutex);
  317. list_add(&prg->list, &ipu_prg_list);
  318. mutex_unlock(&ipu_prg_list_mutex);
  319. return 0;
  320. }
  321. static int ipu_prg_remove(struct platform_device *pdev)
  322. {
  323. struct ipu_prg *prg = platform_get_drvdata(pdev);
  324. mutex_lock(&ipu_prg_list_mutex);
  325. list_del(&prg->list);
  326. mutex_unlock(&ipu_prg_list_mutex);
  327. return 0;
  328. }
  329. static const struct of_device_id ipu_prg_dt_ids[] = {
  330. { .compatible = "fsl,imx6qp-prg", },
  331. { /* sentinel */ },
  332. };
  333. struct platform_driver ipu_prg_drv = {
  334. .probe = ipu_prg_probe,
  335. .remove = ipu_prg_remove,
  336. .driver = {
  337. .name = "imx-ipu-prg",
  338. .of_match_table = ipu_prg_dt_ids,
  339. },
  340. };