2 * Copyright (c) 2016-2017 Lucas Stach, Pengutronix
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.
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
14 #include <drm/drm_fourcc.h>
15 #include <linux/clk.h>
16 #include <linux/err.h>
17 #include <linux/iopoll.h>
18 #include <linux/mfd/syscon.h>
19 #include <linux/mfd/syscon/imx6q-iomuxc-gpr.h>
20 #include <linux/module.h>
22 #include <linux/platform_device.h>
23 #include <linux/regmap.h>
24 #include <video/imx-ipu-v3.h>
28 #define IPU_PRG_CTL 0x00
29 #define IPU_PRG_CTL_BYPASS(i) (1 << (0 + i))
30 #define IPU_PRG_CTL_SOFT_ARID_MASK 0x3
31 #define IPU_PRG_CTL_SOFT_ARID_SHIFT(i) (8 + i * 2)
32 #define IPU_PRG_CTL_SOFT_ARID(i, v) ((v & 0x3) << (8 + 2 * i))
33 #define IPU_PRG_CTL_SO(i) (1 << (16 + i))
34 #define IPU_PRG_CTL_VFLIP(i) (1 << (19 + i))
35 #define IPU_PRG_CTL_BLOCK_MODE(i) (1 << (22 + i))
36 #define IPU_PRG_CTL_CNT_LOAD_EN(i) (1 << (25 + i))
37 #define IPU_PRG_CTL_SOFTRST (1 << 30)
38 #define IPU_PRG_CTL_SHADOW_EN (1 << 31)
40 #define IPU_PRG_STATUS 0x04
41 #define IPU_PRG_STATUS_BUFFER0_READY(i) (1 << (0 + i * 2))
42 #define IPU_PRG_STATUS_BUFFER1_READY(i) (1 << (1 + i * 2))
44 #define IPU_PRG_QOS 0x08
45 #define IPU_PRG_QOS_ARID_MASK 0xf
46 #define IPU_PRG_QOS_ARID_SHIFT(i) (0 + i * 4)
48 #define IPU_PRG_REG_UPDATE 0x0c
49 #define IPU_PRG_REG_UPDATE_REG_UPDATE (1 << 0)
51 #define IPU_PRG_STRIDE(i) (0x10 + i * 0x4)
52 #define IPU_PRG_STRIDE_STRIDE_MASK 0x3fff
54 #define IPU_PRG_CROP_LINE 0x1c
56 #define IPU_PRG_THD 0x20
58 #define IPU_PRG_BADDR(i) (0x24 + i * 0x4)
60 #define IPU_PRG_OFFSET(i) (0x30 + i * 0x4)
62 #define IPU_PRG_ILO(i) (0x3c + i * 0x4)
64 #define IPU_PRG_HEIGHT(i) (0x48 + i * 0x4)
65 #define IPU_PRG_HEIGHT_PRE_HEIGHT_MASK 0xfff
66 #define IPU_PRG_HEIGHT_PRE_HEIGHT_SHIFT 0
67 #define IPU_PRG_HEIGHT_IPU_HEIGHT_MASK 0xfff
68 #define IPU_PRG_HEIGHT_IPU_HEIGHT_SHIFT 16
70 struct ipu_prg_channel {
76 struct list_head list;
81 struct clk *clk_ipg, *clk_axi;
82 struct regmap *iomuxc_gpr;
83 struct ipu_pre *pres[3];
85 struct ipu_prg_channel chan[3];
88 static DEFINE_MUTEX(ipu_prg_list_mutex);
89 static LIST_HEAD(ipu_prg_list);
92 ipu_prg_lookup_by_phandle(struct device *dev, const char *name, int ipu_id)
94 struct device_node *prg_node = of_parse_phandle(dev->of_node,
98 mutex_lock(&ipu_prg_list_mutex);
99 list_for_each_entry(prg, &ipu_prg_list, list) {
100 if (prg_node == prg->dev->of_node) {
101 mutex_unlock(&ipu_prg_list_mutex);
102 device_link_add(dev, prg->dev, DL_FLAG_AUTOREMOVE);
104 of_node_put(prg_node);
108 mutex_unlock(&ipu_prg_list_mutex);
110 of_node_put(prg_node);
115 int ipu_prg_max_active_channels(void)
117 return ipu_pre_get_available_count();
119 EXPORT_SYMBOL_GPL(ipu_prg_max_active_channels);
121 bool ipu_prg_present(struct ipu_soc *ipu)
128 EXPORT_SYMBOL_GPL(ipu_prg_present);
130 bool ipu_prg_format_supported(struct ipu_soc *ipu, uint32_t format,
133 const struct drm_format_info *info = drm_format_info(format);
135 if (info->num_planes != 1)
140 EXPORT_SYMBOL_GPL(ipu_prg_format_supported);
142 int ipu_prg_enable(struct ipu_soc *ipu)
144 struct ipu_prg *prg = ipu->prg_priv;
150 ret = clk_prepare_enable(prg->clk_axi);
152 goto fail_disable_ipg;
157 clk_disable_unprepare(prg->clk_ipg);
161 EXPORT_SYMBOL_GPL(ipu_prg_enable);
163 void ipu_prg_disable(struct ipu_soc *ipu)
165 struct ipu_prg *prg = ipu->prg_priv;
170 clk_disable_unprepare(prg->clk_axi);
172 EXPORT_SYMBOL_GPL(ipu_prg_disable);
175 * The channel configuartion functions below are not thread safe, as they
176 * must be only called from the atomic commit path in the DRM driver, which
177 * is properly serialized.
179 static int ipu_prg_ipu_to_prg_chan(int ipu_chan)
182 * This isn't clearly documented in the RM, but IPU to PRG channel
183 * assignment is fixed, as only with this mapping the control signals
187 case IPUV3_CHANNEL_MEM_BG_SYNC:
189 case IPUV3_CHANNEL_MEM_FG_SYNC:
191 case IPUV3_CHANNEL_MEM_DC_SYNC:
198 static int ipu_prg_get_pre(struct ipu_prg *prg, int prg_chan)
202 /* channel 0 is special as it is hardwired to one of the PREs */
204 ret = ipu_pre_get(prg->pres[0]);
207 prg->chan[prg_chan].used_pre = 0;
211 for (i = 1; i < 3; i++) {
212 ret = ipu_pre_get(prg->pres[i]);
217 prg->chan[prg_chan].used_pre = i;
219 /* configure the PRE to PRG channel mux */
220 shift = (i == 1) ? 12 : 14;
221 mux = (prg->id << 1) | (prg_chan - 1);
222 regmap_update_bits(prg->iomuxc_gpr, IOMUXC_GPR5,
223 0x3 << shift, mux << shift);
225 /* check other mux, must not point to same channel */
226 shift = (i == 1) ? 14 : 12;
227 regmap_read(prg->iomuxc_gpr, IOMUXC_GPR5, &val);
228 if (((val >> shift) & 0x3) == mux) {
229 regmap_update_bits(prg->iomuxc_gpr, IOMUXC_GPR5,
231 (mux ^ 0x1) << shift);
239 dev_err(prg->dev, "could not get PRE for PRG chan %d", prg_chan);
243 static void ipu_prg_put_pre(struct ipu_prg *prg, int prg_chan)
245 struct ipu_prg_channel *chan = &prg->chan[prg_chan];
247 ipu_pre_put(prg->pres[chan->used_pre]);
251 void ipu_prg_channel_disable(struct ipuv3_channel *ipu_chan)
253 int prg_chan = ipu_prg_ipu_to_prg_chan(ipu_chan->num);
254 struct ipu_prg *prg = ipu_chan->ipu->prg_priv;
255 struct ipu_prg_channel *chan;
261 chan = &prg->chan[prg_chan];
265 clk_prepare_enable(prg->clk_ipg);
267 val = readl(prg->regs + IPU_PRG_CTL);
268 val |= IPU_PRG_CTL_BYPASS(prg_chan);
269 writel(val, prg->regs + IPU_PRG_CTL);
271 val = IPU_PRG_REG_UPDATE_REG_UPDATE;
272 writel(val, prg->regs + IPU_PRG_REG_UPDATE);
274 clk_disable_unprepare(prg->clk_ipg);
276 ipu_prg_put_pre(prg, prg_chan);
278 chan->enabled = false;
280 EXPORT_SYMBOL_GPL(ipu_prg_channel_disable);
282 int ipu_prg_channel_configure(struct ipuv3_channel *ipu_chan,
283 unsigned int axi_id, unsigned int width,
284 unsigned int height, unsigned int stride,
285 u32 format, unsigned long *eba)
287 int prg_chan = ipu_prg_ipu_to_prg_chan(ipu_chan->num);
288 struct ipu_prg *prg = ipu_chan->ipu->prg_priv;
289 struct ipu_prg_channel *chan;
296 chan = &prg->chan[prg_chan];
299 ipu_pre_update(prg->pres[chan->used_pre], *eba);
303 ret = ipu_prg_get_pre(prg, prg_chan);
307 ipu_pre_configure(prg->pres[chan->used_pre],
308 width, height, stride, format, *eba);
311 ret = clk_prepare_enable(prg->clk_ipg);
313 ipu_prg_put_pre(prg, prg_chan);
317 val = (stride - 1) & IPU_PRG_STRIDE_STRIDE_MASK;
318 writel(val, prg->regs + IPU_PRG_STRIDE(prg_chan));
320 val = ((height & IPU_PRG_HEIGHT_PRE_HEIGHT_MASK) <<
321 IPU_PRG_HEIGHT_PRE_HEIGHT_SHIFT) |
322 ((height & IPU_PRG_HEIGHT_IPU_HEIGHT_MASK) <<
323 IPU_PRG_HEIGHT_IPU_HEIGHT_SHIFT);
324 writel(val, prg->regs + IPU_PRG_HEIGHT(prg_chan));
326 val = ipu_pre_get_baddr(prg->pres[chan->used_pre]);
328 writel(val, prg->regs + IPU_PRG_BADDR(prg_chan));
330 val = readl(prg->regs + IPU_PRG_CTL);
332 val &= ~(IPU_PRG_CTL_SOFT_ARID_MASK <<
333 IPU_PRG_CTL_SOFT_ARID_SHIFT(prg_chan));
334 val |= IPU_PRG_CTL_SOFT_ARID(prg_chan, axi_id);
336 val &= ~IPU_PRG_CTL_BYPASS(prg_chan);
337 writel(val, prg->regs + IPU_PRG_CTL);
339 val = IPU_PRG_REG_UPDATE_REG_UPDATE;
340 writel(val, prg->regs + IPU_PRG_REG_UPDATE);
342 /* wait for both double buffers to be filled */
343 readl_poll_timeout(prg->regs + IPU_PRG_STATUS, val,
344 (val & IPU_PRG_STATUS_BUFFER0_READY(prg_chan)) &&
345 (val & IPU_PRG_STATUS_BUFFER1_READY(prg_chan)),
348 clk_disable_unprepare(prg->clk_ipg);
350 chan->enabled = true;
353 EXPORT_SYMBOL_GPL(ipu_prg_channel_configure);
355 static int ipu_prg_probe(struct platform_device *pdev)
357 struct device *dev = &pdev->dev;
358 struct resource *res;
363 prg = devm_kzalloc(dev, sizeof(*prg), GFP_KERNEL);
367 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
368 prg->regs = devm_ioremap_resource(&pdev->dev, res);
369 if (IS_ERR(prg->regs))
370 return PTR_ERR(prg->regs);
373 prg->clk_ipg = devm_clk_get(dev, "ipg");
374 if (IS_ERR(prg->clk_ipg))
375 return PTR_ERR(prg->clk_ipg);
377 prg->clk_axi = devm_clk_get(dev, "axi");
378 if (IS_ERR(prg->clk_axi))
379 return PTR_ERR(prg->clk_axi);
382 syscon_regmap_lookup_by_compatible("fsl,imx6q-iomuxc-gpr");
383 if (IS_ERR(prg->iomuxc_gpr))
384 return PTR_ERR(prg->iomuxc_gpr);
386 for (i = 0; i < 3; i++) {
387 prg->pres[i] = ipu_pre_lookup_by_phandle(dev, "fsl,pres", i);
389 return -EPROBE_DEFER;
392 ret = clk_prepare_enable(prg->clk_ipg);
396 /* init to free running mode */
397 val = readl(prg->regs + IPU_PRG_CTL);
398 val |= IPU_PRG_CTL_SHADOW_EN;
399 writel(val, prg->regs + IPU_PRG_CTL);
401 /* disable address threshold */
402 writel(0xffffffff, prg->regs + IPU_PRG_THD);
404 clk_disable_unprepare(prg->clk_ipg);
407 platform_set_drvdata(pdev, prg);
408 mutex_lock(&ipu_prg_list_mutex);
409 list_add(&prg->list, &ipu_prg_list);
410 mutex_unlock(&ipu_prg_list_mutex);
415 static int ipu_prg_remove(struct platform_device *pdev)
417 struct ipu_prg *prg = platform_get_drvdata(pdev);
419 mutex_lock(&ipu_prg_list_mutex);
420 list_del(&prg->list);
421 mutex_unlock(&ipu_prg_list_mutex);
426 static const struct of_device_id ipu_prg_dt_ids[] = {
427 { .compatible = "fsl,imx6qp-prg", },
431 struct platform_driver ipu_prg_drv = {
432 .probe = ipu_prg_probe,
433 .remove = ipu_prg_remove,
435 .name = "imx-ipu-prg",
436 .of_match_table = ipu_prg_dt_ids,