Linux-libre 5.4.48-gnu
[librecmc/linux-libre.git] / drivers / media / platform / mtk-mdp / mtk_mdp_core.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2015-2016 MediaTek Inc.
4  * Author: Houlong Wei <houlong.wei@mediatek.com>
5  *         Ming Hsiu Tsai <minghsiu.tsai@mediatek.com>
6  */
7
8 #include <linux/clk.h>
9 #include <linux/device.h>
10 #include <linux/errno.h>
11 #include <linux/interrupt.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/of.h>
15 #include <linux/of_address.h>
16 #include <linux/of_platform.h>
17 #include <linux/platform_device.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/workqueue.h>
20 #include <soc/mediatek/smi.h>
21
22 #include "mtk_mdp_core.h"
23 #include "mtk_mdp_m2m.h"
24 #include "mtk_vpu.h"
25
26 /* MDP debug log level (0-3). 3 shows all the logs. */
27 int mtk_mdp_dbg_level;
28 EXPORT_SYMBOL(mtk_mdp_dbg_level);
29
30 module_param(mtk_mdp_dbg_level, int, 0644);
31
32 static const struct of_device_id mtk_mdp_comp_dt_ids[] = {
33         {
34                 .compatible = "mediatek,mt8173-mdp-rdma",
35                 .data = (void *)MTK_MDP_RDMA
36         }, {
37                 .compatible = "mediatek,mt8173-mdp-rsz",
38                 .data = (void *)MTK_MDP_RSZ
39         }, {
40                 .compatible = "mediatek,mt8173-mdp-wdma",
41                 .data = (void *)MTK_MDP_WDMA
42         }, {
43                 .compatible = "mediatek,mt8173-mdp-wrot",
44                 .data = (void *)MTK_MDP_WROT
45         },
46         { },
47 };
48
49 static const struct of_device_id mtk_mdp_of_ids[] = {
50         { .compatible = "mediatek,mt8173-mdp", },
51         { },
52 };
53 MODULE_DEVICE_TABLE(of, mtk_mdp_of_ids);
54
55 static void mtk_mdp_clock_on(struct mtk_mdp_dev *mdp)
56 {
57         struct device *dev = &mdp->pdev->dev;
58         int i;
59
60         for (i = 0; i < ARRAY_SIZE(mdp->comp); i++)
61                 mtk_mdp_comp_clock_on(dev, mdp->comp[i]);
62 }
63
64 static void mtk_mdp_clock_off(struct mtk_mdp_dev *mdp)
65 {
66         struct device *dev = &mdp->pdev->dev;
67         int i;
68
69         for (i = 0; i < ARRAY_SIZE(mdp->comp); i++)
70                 mtk_mdp_comp_clock_off(dev, mdp->comp[i]);
71 }
72
73 static void mtk_mdp_wdt_worker(struct work_struct *work)
74 {
75         struct mtk_mdp_dev *mdp =
76                         container_of(work, struct mtk_mdp_dev, wdt_work);
77         struct mtk_mdp_ctx *ctx;
78
79         mtk_mdp_err("Watchdog timeout");
80
81         list_for_each_entry(ctx, &mdp->ctx_list, list) {
82                 mtk_mdp_dbg(0, "[%d] Change as state error", ctx->id);
83                 mtk_mdp_ctx_state_lock_set(ctx, MTK_MDP_CTX_ERROR);
84         }
85 }
86
87 static void mtk_mdp_reset_handler(void *priv)
88 {
89         struct mtk_mdp_dev *mdp = priv;
90
91         queue_work(mdp->wdt_wq, &mdp->wdt_work);
92 }
93
94 static int mtk_mdp_probe(struct platform_device *pdev)
95 {
96         struct mtk_mdp_dev *mdp;
97         struct device *dev = &pdev->dev;
98         struct device_node *node, *parent;
99         int i, ret = 0;
100
101         mdp = devm_kzalloc(dev, sizeof(*mdp), GFP_KERNEL);
102         if (!mdp)
103                 return -ENOMEM;
104
105         mdp->id = pdev->id;
106         mdp->pdev = pdev;
107         INIT_LIST_HEAD(&mdp->ctx_list);
108
109         mutex_init(&mdp->lock);
110         mutex_init(&mdp->vpulock);
111
112         /* Old dts had the components as child nodes */
113         node = of_get_next_child(dev->of_node, NULL);
114         if (node) {
115                 of_node_put(node);
116                 parent = dev->of_node;
117                 dev_warn(dev, "device tree is out of date\n");
118         } else {
119                 parent = dev->of_node->parent;
120         }
121
122         /* Iterate over sibling MDP function blocks */
123         for_each_child_of_node(parent, node) {
124                 const struct of_device_id *of_id;
125                 enum mtk_mdp_comp_type comp_type;
126                 int comp_id;
127                 struct mtk_mdp_comp *comp;
128
129                 of_id = of_match_node(mtk_mdp_comp_dt_ids, node);
130                 if (!of_id)
131                         continue;
132
133                 if (!of_device_is_available(node)) {
134                         dev_err(dev, "Skipping disabled component %pOF\n",
135                                 node);
136                         continue;
137                 }
138
139                 comp_type = (enum mtk_mdp_comp_type)of_id->data;
140                 comp_id = mtk_mdp_comp_get_id(dev, node, comp_type);
141                 if (comp_id < 0) {
142                         dev_warn(dev, "Skipping unknown component %pOF\n",
143                                  node);
144                         continue;
145                 }
146
147                 comp = devm_kzalloc(dev, sizeof(*comp), GFP_KERNEL);
148                 if (!comp) {
149                         ret = -ENOMEM;
150                         of_node_put(node);
151                         goto err_comp;
152                 }
153                 mdp->comp[comp_id] = comp;
154
155                 ret = mtk_mdp_comp_init(dev, node, comp, comp_id);
156                 if (ret) {
157                         of_node_put(node);
158                         goto err_comp;
159                 }
160         }
161
162         mdp->job_wq = create_singlethread_workqueue(MTK_MDP_MODULE_NAME);
163         if (!mdp->job_wq) {
164                 dev_err(&pdev->dev, "unable to alloc job workqueue\n");
165                 ret = -ENOMEM;
166                 goto err_alloc_job_wq;
167         }
168
169         mdp->wdt_wq = create_singlethread_workqueue("mdp_wdt_wq");
170         if (!mdp->wdt_wq) {
171                 dev_err(&pdev->dev, "unable to alloc wdt workqueue\n");
172                 ret = -ENOMEM;
173                 goto err_alloc_wdt_wq;
174         }
175         INIT_WORK(&mdp->wdt_work, mtk_mdp_wdt_worker);
176
177         ret = v4l2_device_register(dev, &mdp->v4l2_dev);
178         if (ret) {
179                 dev_err(&pdev->dev, "Failed to register v4l2 device\n");
180                 ret = -EINVAL;
181                 goto err_dev_register;
182         }
183
184         ret = mtk_mdp_register_m2m_device(mdp);
185         if (ret) {
186                 v4l2_err(&mdp->v4l2_dev, "Failed to init mem2mem device\n");
187                 goto err_m2m_register;
188         }
189
190         mdp->vpu_dev = vpu_get_plat_device(pdev);
191         vpu_wdt_reg_handler(mdp->vpu_dev, mtk_mdp_reset_handler, mdp,
192                             VPU_RST_MDP);
193
194         platform_set_drvdata(pdev, mdp);
195
196         vb2_dma_contig_set_max_seg_size(&pdev->dev, DMA_BIT_MASK(32));
197
198         pm_runtime_enable(dev);
199         dev_dbg(dev, "mdp-%d registered successfully\n", mdp->id);
200
201         return 0;
202
203 err_m2m_register:
204         v4l2_device_unregister(&mdp->v4l2_dev);
205
206 err_dev_register:
207         destroy_workqueue(mdp->wdt_wq);
208
209 err_alloc_wdt_wq:
210         destroy_workqueue(mdp->job_wq);
211
212 err_alloc_job_wq:
213
214 err_comp:
215         for (i = 0; i < ARRAY_SIZE(mdp->comp); i++)
216                 mtk_mdp_comp_deinit(dev, mdp->comp[i]);
217
218         dev_dbg(dev, "err %d\n", ret);
219         return ret;
220 }
221
222 static int mtk_mdp_remove(struct platform_device *pdev)
223 {
224         struct mtk_mdp_dev *mdp = platform_get_drvdata(pdev);
225         int i;
226
227         pm_runtime_disable(&pdev->dev);
228         vb2_dma_contig_clear_max_seg_size(&pdev->dev);
229         mtk_mdp_unregister_m2m_device(mdp);
230         v4l2_device_unregister(&mdp->v4l2_dev);
231
232         flush_workqueue(mdp->job_wq);
233         destroy_workqueue(mdp->job_wq);
234
235         for (i = 0; i < ARRAY_SIZE(mdp->comp); i++)
236                 mtk_mdp_comp_deinit(&pdev->dev, mdp->comp[i]);
237
238         dev_dbg(&pdev->dev, "%s driver unloaded\n", pdev->name);
239         return 0;
240 }
241
242 static int __maybe_unused mtk_mdp_pm_suspend(struct device *dev)
243 {
244         struct mtk_mdp_dev *mdp = dev_get_drvdata(dev);
245
246         mtk_mdp_clock_off(mdp);
247
248         return 0;
249 }
250
251 static int __maybe_unused mtk_mdp_pm_resume(struct device *dev)
252 {
253         struct mtk_mdp_dev *mdp = dev_get_drvdata(dev);
254
255         mtk_mdp_clock_on(mdp);
256
257         return 0;
258 }
259
260 static int __maybe_unused mtk_mdp_suspend(struct device *dev)
261 {
262         if (pm_runtime_suspended(dev))
263                 return 0;
264
265         return mtk_mdp_pm_suspend(dev);
266 }
267
268 static int __maybe_unused mtk_mdp_resume(struct device *dev)
269 {
270         if (pm_runtime_suspended(dev))
271                 return 0;
272
273         return mtk_mdp_pm_resume(dev);
274 }
275
276 static const struct dev_pm_ops mtk_mdp_pm_ops = {
277         SET_SYSTEM_SLEEP_PM_OPS(mtk_mdp_suspend, mtk_mdp_resume)
278         SET_RUNTIME_PM_OPS(mtk_mdp_pm_suspend, mtk_mdp_pm_resume, NULL)
279 };
280
281 static struct platform_driver mtk_mdp_driver = {
282         .probe          = mtk_mdp_probe,
283         .remove         = mtk_mdp_remove,
284         .driver = {
285                 .name   = MTK_MDP_MODULE_NAME,
286                 .pm     = &mtk_mdp_pm_ops,
287                 .of_match_table = mtk_mdp_of_ids,
288         }
289 };
290
291 module_platform_driver(mtk_mdp_driver);
292
293 MODULE_AUTHOR("Houlong Wei <houlong.wei@mediatek.com>");
294 MODULE_DESCRIPTION("Mediatek image processor driver");
295 MODULE_LICENSE("GPL v2");