1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2015-2016 MediaTek Inc.
4 * Author: Houlong Wei <houlong.wei@mediatek.com>
5 * Ming Hsiu Tsai <minghsiu.tsai@mediatek.com>
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>
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>
22 #include "mtk_mdp_core.h"
23 #include "mtk_mdp_m2m.h"
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);
30 module_param(mtk_mdp_dbg_level, int, 0644);
32 static const struct of_device_id mtk_mdp_comp_dt_ids[] = {
34 .compatible = "mediatek,mt8173-mdp-rdma",
35 .data = (void *)MTK_MDP_RDMA
37 .compatible = "mediatek,mt8173-mdp-rsz",
38 .data = (void *)MTK_MDP_RSZ
40 .compatible = "mediatek,mt8173-mdp-wdma",
41 .data = (void *)MTK_MDP_WDMA
43 .compatible = "mediatek,mt8173-mdp-wrot",
44 .data = (void *)MTK_MDP_WROT
49 static const struct of_device_id mtk_mdp_of_ids[] = {
50 { .compatible = "mediatek,mt8173-mdp", },
53 MODULE_DEVICE_TABLE(of, mtk_mdp_of_ids);
55 static void mtk_mdp_clock_on(struct mtk_mdp_dev *mdp)
57 struct device *dev = &mdp->pdev->dev;
58 struct mtk_mdp_comp *comp_node;
60 list_for_each_entry(comp_node, &mdp->comp_list, node)
61 mtk_mdp_comp_clock_on(dev, comp_node);
64 static void mtk_mdp_clock_off(struct mtk_mdp_dev *mdp)
66 struct device *dev = &mdp->pdev->dev;
67 struct mtk_mdp_comp *comp_node;
69 list_for_each_entry(comp_node, &mdp->comp_list, node)
70 mtk_mdp_comp_clock_off(dev, comp_node);
73 static void mtk_mdp_wdt_worker(struct work_struct *work)
75 struct mtk_mdp_dev *mdp =
76 container_of(work, struct mtk_mdp_dev, wdt_work);
77 struct mtk_mdp_ctx *ctx;
79 mtk_mdp_err("Watchdog timeout");
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);
87 static void mtk_mdp_reset_handler(void *priv)
89 struct mtk_mdp_dev *mdp = priv;
91 queue_work(mdp->wdt_wq, &mdp->wdt_work);
94 void mtk_mdp_register_component(struct mtk_mdp_dev *mdp,
95 struct mtk_mdp_comp *comp)
97 list_add(&comp->node, &mdp->comp_list);
100 void mtk_mdp_unregister_component(struct mtk_mdp_dev *mdp,
101 struct mtk_mdp_comp *comp)
103 list_del(&comp->node);
106 static int mtk_mdp_probe(struct platform_device *pdev)
108 struct mtk_mdp_dev *mdp;
109 struct device *dev = &pdev->dev;
110 struct device_node *node, *parent;
111 struct mtk_mdp_comp *comp, *comp_temp;
114 mdp = devm_kzalloc(dev, sizeof(*mdp), GFP_KERNEL);
120 INIT_LIST_HEAD(&mdp->comp_list);
121 INIT_LIST_HEAD(&mdp->ctx_list);
123 mutex_init(&mdp->lock);
124 mutex_init(&mdp->vpulock);
126 /* Old dts had the components as child nodes */
127 node = of_get_next_child(dev->of_node, NULL);
130 parent = dev->of_node;
131 dev_warn(dev, "device tree is out of date\n");
133 parent = dev->of_node->parent;
136 /* Iterate over sibling MDP function blocks */
137 for_each_child_of_node(parent, node) {
138 const struct of_device_id *of_id;
139 enum mtk_mdp_comp_type comp_type;
141 of_id = of_match_node(mtk_mdp_comp_dt_ids, node);
145 if (!of_device_is_available(node)) {
146 dev_err(dev, "Skipping disabled component %pOF\n",
151 comp_type = (enum mtk_mdp_comp_type)of_id->data;
153 comp = devm_kzalloc(dev, sizeof(*comp), GFP_KERNEL);
160 ret = mtk_mdp_comp_init(dev, node, comp, comp_type);
166 mtk_mdp_register_component(mdp, comp);
169 mdp->job_wq = create_singlethread_workqueue(MTK_MDP_MODULE_NAME);
171 dev_err(&pdev->dev, "unable to alloc job workqueue\n");
173 goto err_alloc_job_wq;
176 mdp->wdt_wq = create_singlethread_workqueue("mdp_wdt_wq");
178 dev_err(&pdev->dev, "unable to alloc wdt workqueue\n");
180 goto err_alloc_wdt_wq;
182 INIT_WORK(&mdp->wdt_work, mtk_mdp_wdt_worker);
184 ret = v4l2_device_register(dev, &mdp->v4l2_dev);
186 dev_err(&pdev->dev, "Failed to register v4l2 device\n");
188 goto err_dev_register;
191 ret = mtk_mdp_register_m2m_device(mdp);
193 v4l2_err(&mdp->v4l2_dev, "Failed to init mem2mem device\n");
194 goto err_m2m_register;
197 mdp->vpu_dev = vpu_get_plat_device(pdev);
198 ret = vpu_wdt_reg_handler(mdp->vpu_dev, mtk_mdp_reset_handler, mdp,
201 dev_err(&pdev->dev, "Failed to register reset handler\n");
202 goto err_m2m_register;
205 platform_set_drvdata(pdev, mdp);
207 ret = vb2_dma_contig_set_max_seg_size(&pdev->dev, DMA_BIT_MASK(32));
209 dev_err(&pdev->dev, "Failed to set vb2 dma mag seg size\n");
210 goto err_m2m_register;
213 pm_runtime_enable(dev);
214 dev_dbg(dev, "mdp-%d registered successfully\n", mdp->id);
219 v4l2_device_unregister(&mdp->v4l2_dev);
222 destroy_workqueue(mdp->wdt_wq);
225 destroy_workqueue(mdp->job_wq);
230 list_for_each_entry_safe(comp, comp_temp, &mdp->comp_list, node) {
231 mtk_mdp_unregister_component(mdp, comp);
232 mtk_mdp_comp_deinit(dev, comp);
235 dev_dbg(dev, "err %d\n", ret);
239 static int mtk_mdp_remove(struct platform_device *pdev)
241 struct mtk_mdp_dev *mdp = platform_get_drvdata(pdev);
242 struct mtk_mdp_comp *comp, *comp_temp;
244 pm_runtime_disable(&pdev->dev);
245 vb2_dma_contig_clear_max_seg_size(&pdev->dev);
246 mtk_mdp_unregister_m2m_device(mdp);
247 v4l2_device_unregister(&mdp->v4l2_dev);
249 flush_workqueue(mdp->wdt_wq);
250 destroy_workqueue(mdp->wdt_wq);
252 flush_workqueue(mdp->job_wq);
253 destroy_workqueue(mdp->job_wq);
255 list_for_each_entry_safe(comp, comp_temp, &mdp->comp_list, node) {
256 mtk_mdp_unregister_component(mdp, comp);
257 mtk_mdp_comp_deinit(&pdev->dev, comp);
260 dev_dbg(&pdev->dev, "%s driver unloaded\n", pdev->name);
264 static int __maybe_unused mtk_mdp_pm_suspend(struct device *dev)
266 struct mtk_mdp_dev *mdp = dev_get_drvdata(dev);
268 mtk_mdp_clock_off(mdp);
273 static int __maybe_unused mtk_mdp_pm_resume(struct device *dev)
275 struct mtk_mdp_dev *mdp = dev_get_drvdata(dev);
277 mtk_mdp_clock_on(mdp);
282 static int __maybe_unused mtk_mdp_suspend(struct device *dev)
284 if (pm_runtime_suspended(dev))
287 return mtk_mdp_pm_suspend(dev);
290 static int __maybe_unused mtk_mdp_resume(struct device *dev)
292 if (pm_runtime_suspended(dev))
295 return mtk_mdp_pm_resume(dev);
298 static const struct dev_pm_ops mtk_mdp_pm_ops = {
299 SET_SYSTEM_SLEEP_PM_OPS(mtk_mdp_suspend, mtk_mdp_resume)
300 SET_RUNTIME_PM_OPS(mtk_mdp_pm_suspend, mtk_mdp_pm_resume, NULL)
303 static struct platform_driver mtk_mdp_driver = {
304 .probe = mtk_mdp_probe,
305 .remove = mtk_mdp_remove,
307 .name = MTK_MDP_MODULE_NAME,
308 .pm = &mtk_mdp_pm_ops,
309 .of_match_table = mtk_mdp_of_ids,
313 module_platform_driver(mtk_mdp_driver);
315 MODULE_AUTHOR("Houlong Wei <houlong.wei@mediatek.com>");
316 MODULE_DESCRIPTION("Mediatek image processor driver");
317 MODULE_LICENSE("GPL v2");