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