ipu-prg.c 11 KB

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