intel_th: pci: Add Tiger Lake CPU support
[linux/fpc-iii.git] / drivers / gpu / ipu-v3 / ipu-smfc.c
blob46ffc0a5906d69d88c6cc3864d1dce308177faba
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright 2008-2010 Freescale Semiconductor, Inc. All Rights Reserved.
4 */
5 #include <linux/export.h>
6 #include <linux/types.h>
7 #include <linux/init.h>
8 #include <linux/io.h>
9 #include <linux/errno.h>
10 #include <linux/spinlock.h>
11 #include <linux/delay.h>
12 #include <linux/clk.h>
13 #include <video/imx-ipu-v3.h>
15 #include "ipu-prv.h"
17 struct ipu_smfc {
18 struct ipu_smfc_priv *priv;
19 int chno;
20 bool inuse;
23 struct ipu_smfc_priv {
24 void __iomem *base;
25 spinlock_t lock;
26 struct ipu_soc *ipu;
27 struct ipu_smfc channel[4];
28 int use_count;
31 /*SMFC Registers */
32 #define SMFC_MAP 0x0000
33 #define SMFC_WMC 0x0004
34 #define SMFC_BS 0x0008
36 int ipu_smfc_set_burstsize(struct ipu_smfc *smfc, int burstsize)
38 struct ipu_smfc_priv *priv = smfc->priv;
39 unsigned long flags;
40 u32 val, shift;
42 spin_lock_irqsave(&priv->lock, flags);
44 shift = smfc->chno * 4;
45 val = readl(priv->base + SMFC_BS);
46 val &= ~(0xf << shift);
47 val |= burstsize << shift;
48 writel(val, priv->base + SMFC_BS);
50 spin_unlock_irqrestore(&priv->lock, flags);
52 return 0;
54 EXPORT_SYMBOL_GPL(ipu_smfc_set_burstsize);
56 int ipu_smfc_map_channel(struct ipu_smfc *smfc, int csi_id, int mipi_id)
58 struct ipu_smfc_priv *priv = smfc->priv;
59 unsigned long flags;
60 u32 val, shift;
62 spin_lock_irqsave(&priv->lock, flags);
64 shift = smfc->chno * 3;
65 val = readl(priv->base + SMFC_MAP);
66 val &= ~(0x7 << shift);
67 val |= ((csi_id << 2) | mipi_id) << shift;
68 writel(val, priv->base + SMFC_MAP);
70 spin_unlock_irqrestore(&priv->lock, flags);
72 return 0;
74 EXPORT_SYMBOL_GPL(ipu_smfc_map_channel);
76 int ipu_smfc_set_watermark(struct ipu_smfc *smfc, u32 set_level, u32 clr_level)
78 struct ipu_smfc_priv *priv = smfc->priv;
79 unsigned long flags;
80 u32 val, shift;
82 spin_lock_irqsave(&priv->lock, flags);
84 shift = smfc->chno * 6 + (smfc->chno > 1 ? 4 : 0);
85 val = readl(priv->base + SMFC_WMC);
86 val &= ~(0x3f << shift);
87 val |= ((clr_level << 3) | set_level) << shift;
88 writel(val, priv->base + SMFC_WMC);
90 spin_unlock_irqrestore(&priv->lock, flags);
92 return 0;
94 EXPORT_SYMBOL_GPL(ipu_smfc_set_watermark);
96 int ipu_smfc_enable(struct ipu_smfc *smfc)
98 struct ipu_smfc_priv *priv = smfc->priv;
99 unsigned long flags;
101 spin_lock_irqsave(&priv->lock, flags);
103 if (!priv->use_count)
104 ipu_module_enable(priv->ipu, IPU_CONF_SMFC_EN);
106 priv->use_count++;
108 spin_unlock_irqrestore(&priv->lock, flags);
110 return 0;
112 EXPORT_SYMBOL_GPL(ipu_smfc_enable);
114 int ipu_smfc_disable(struct ipu_smfc *smfc)
116 struct ipu_smfc_priv *priv = smfc->priv;
117 unsigned long flags;
119 spin_lock_irqsave(&priv->lock, flags);
121 priv->use_count--;
123 if (!priv->use_count)
124 ipu_module_disable(priv->ipu, IPU_CONF_SMFC_EN);
126 if (priv->use_count < 0)
127 priv->use_count = 0;
129 spin_unlock_irqrestore(&priv->lock, flags);
131 return 0;
133 EXPORT_SYMBOL_GPL(ipu_smfc_disable);
135 struct ipu_smfc *ipu_smfc_get(struct ipu_soc *ipu, unsigned int chno)
137 struct ipu_smfc_priv *priv = ipu->smfc_priv;
138 struct ipu_smfc *smfc, *ret;
139 unsigned long flags;
141 if (chno >= 4)
142 return ERR_PTR(-EINVAL);
144 smfc = &priv->channel[chno];
145 ret = smfc;
147 spin_lock_irqsave(&priv->lock, flags);
149 if (smfc->inuse) {
150 ret = ERR_PTR(-EBUSY);
151 goto unlock;
154 smfc->inuse = true;
155 unlock:
156 spin_unlock_irqrestore(&priv->lock, flags);
157 return ret;
159 EXPORT_SYMBOL_GPL(ipu_smfc_get);
161 void ipu_smfc_put(struct ipu_smfc *smfc)
163 struct ipu_smfc_priv *priv = smfc->priv;
164 unsigned long flags;
166 spin_lock_irqsave(&priv->lock, flags);
167 smfc->inuse = false;
168 spin_unlock_irqrestore(&priv->lock, flags);
170 EXPORT_SYMBOL_GPL(ipu_smfc_put);
172 int ipu_smfc_init(struct ipu_soc *ipu, struct device *dev,
173 unsigned long base)
175 struct ipu_smfc_priv *priv;
176 int i;
178 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
179 if (!priv)
180 return -ENOMEM;
182 ipu->smfc_priv = priv;
183 spin_lock_init(&priv->lock);
184 priv->ipu = ipu;
186 priv->base = devm_ioremap(dev, base, PAGE_SIZE);
187 if (!priv->base)
188 return -ENOMEM;
190 for (i = 0; i < 4; i++) {
191 priv->channel[i].priv = priv;
192 priv->channel[i].chno = i;
195 pr_debug("%s: ioremap 0x%08lx -> %p\n", __func__, base, priv->base);
197 return 0;
200 void ipu_smfc_exit(struct ipu_soc *ipu)