Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 | // SPDX-License-Identifier: GPL-2.0-only /* * Copyright (c) 2022 MediaTek Inc. * Author: Ping-Hsun Wu <ping-hsun.wu@mediatek.com> */ #include <linux/clk.h> #include <linux/module.h> #include <linux/of_platform.h> #include <linux/platform_device.h> #include <linux/pm_runtime.h> #include <linux/remoteproc.h> #include <linux/remoteproc/mtk_scp.h> #include <media/videobuf2-dma-contig.h> #include "mtk-mdp3-core.h" #include "mtk-mdp3-cfg.h" #include "mtk-mdp3-m2m.h" static const struct of_device_id mdp_of_ids[] = { { .compatible = "mediatek,mt8183-mdp3-rdma", .data = &mt8183_mdp_driver_data, }, { .compatible = "mediatek,mt8195-mdp3-rdma", .data = &mt8195_mdp_driver_data, }, { .compatible = "mediatek,mt8195-mdp3-wrot", .data = &mt8195_mdp_driver_data, }, {}, }; MODULE_DEVICE_TABLE(of, mdp_of_ids); static struct platform_device *__get_pdev_by_id(struct platform_device *pdev, struct platform_device *from, enum mdp_infra_id id) { struct device_node *node, *f = NULL; struct platform_device *mdp_pdev = NULL; const struct mtk_mdp_driver_data *mdp_data; const char *compat; if (!pdev) return NULL; if (id < MDP_INFRA_MMSYS || id >= MDP_INFRA_MAX) { dev_err(&pdev->dev, "Illegal infra id %d\n", id); return NULL; } mdp_data = of_device_get_match_data(&pdev->dev); if (!mdp_data) { dev_err(&pdev->dev, "have no driver data to find node\n"); return NULL; } compat = mdp_data->mdp_probe_infra[id].compatible; if (strlen(compat) == 0) return NULL; if (from) f = from->dev.of_node; node = of_find_compatible_node(f, NULL, compat); if (WARN_ON(!node)) { dev_err(&pdev->dev, "find node from id %d failed\n", id); return NULL; } mdp_pdev = of_find_device_by_node(node); of_node_put(node); if (WARN_ON(!mdp_pdev)) { dev_err(&pdev->dev, "find pdev from id %d failed\n", id); return NULL; } return mdp_pdev; } struct platform_device *mdp_get_plat_device(struct platform_device *pdev) { struct device *dev = &pdev->dev; struct device_node *mdp_node; struct platform_device *mdp_pdev; mdp_node = of_parse_phandle(dev->of_node, MDP_PHANDLE_NAME, 0); if (!mdp_node) { dev_err(dev, "can't get node %s\n", MDP_PHANDLE_NAME); return NULL; } mdp_pdev = of_find_device_by_node(mdp_node); of_node_put(mdp_node); return mdp_pdev; } EXPORT_SYMBOL_GPL(mdp_get_plat_device); int mdp_vpu_get_locked(struct mdp_dev *mdp) { int ret = 0; if (mdp->vpu_count++ == 0) { ret = rproc_boot(mdp->rproc_handle); if (ret) { dev_err(&mdp->pdev->dev, "vpu_load_firmware failed %d\n", ret); goto err_load_vpu; } ret = mdp_vpu_register(mdp); if (ret) { dev_err(&mdp->pdev->dev, "mdp_vpu register failed %d\n", ret); goto err_reg_vpu; } ret = mdp_vpu_dev_init(&mdp->vpu, mdp->scp, &mdp->vpu_lock); if (ret) { dev_err(&mdp->pdev->dev, "mdp_vpu device init failed %d\n", ret); goto err_init_vpu; } } return 0; err_init_vpu: mdp_vpu_unregister(mdp); err_reg_vpu: err_load_vpu: mdp->vpu_count--; return ret; } void mdp_vpu_put_locked(struct mdp_dev *mdp) { if (--mdp->vpu_count == 0) { mdp_vpu_dev_deinit(&mdp->vpu); mdp_vpu_unregister(mdp); } } void mdp_video_device_release(struct video_device *vdev) { struct mdp_dev *mdp = (struct mdp_dev *)video_get_drvdata(vdev); int i; for (i = 0; i < mdp->mdp_data->pp_used; i++) if (mdp->cmdq_clt[i]) cmdq_mbox_destroy(mdp->cmdq_clt[i]); scp_put(mdp->scp); destroy_workqueue(mdp->job_wq); destroy_workqueue(mdp->clock_wq); pm_runtime_disable(&mdp->pdev->dev); vb2_dma_contig_clear_max_seg_size(&mdp->pdev->dev); mdp_comp_destroy(mdp); for (i = 0; i < mdp->mdp_data->pipe_info_len; i++) { enum mdp_mm_subsys_id idx; struct mtk_mutex *m; u32 m_id; idx = mdp->mdp_data->pipe_info[i].sub_id; m_id = mdp->mdp_data->pipe_info[i].mutex_id; m = mdp->mm_subsys[idx].mdp_mutex[m_id]; if (!IS_ERR_OR_NULL(m)) mtk_mutex_put(m); } mdp_vpu_shared_mem_free(&mdp->vpu); v4l2_m2m_release(mdp->m2m_dev); kfree(mdp); } static int mdp_mm_subsys_deploy(struct mdp_dev *mdp, enum mdp_infra_id id) { struct platform_device *mm_pdev = NULL; struct device **dev; int i; if (!mdp) return -EINVAL; for (i = 0; i < MDP_MM_SUBSYS_MAX; i++) { const char *compat; enum mdp_infra_id sub_id = id + i; switch (id) { case MDP_INFRA_MMSYS: dev = &mdp->mm_subsys[i].mmsys; break; case MDP_INFRA_MUTEX: dev = &mdp->mm_subsys[i].mutex; break; default: dev_err(&mdp->pdev->dev, "Unknown infra id %d", id); return -EINVAL; } /* * Not every chip has multiple multimedia subsystems, so * the config may be null. */ compat = mdp->mdp_data->mdp_probe_infra[sub_id].compatible; if (strlen(compat) == 0) continue; mm_pdev = __get_pdev_by_id(mdp->pdev, mm_pdev, sub_id); if (WARN_ON(!mm_pdev)) return -ENODEV; *dev = &mm_pdev->dev; } return 0; } static int mdp_probe(struct platform_device *pdev) { struct device *dev = &pdev->dev; struct mdp_dev *mdp; struct platform_device *mm_pdev; struct resource *res; int ret, i, mutex_id; mdp = kzalloc(sizeof(*mdp), GFP_KERNEL); if (!mdp) { ret = -ENOMEM; goto err_return; } mdp->pdev = pdev; mdp->mdp_data = of_device_get_match_data(&pdev->dev); res = platform_get_resource(pdev, IORESOURCE_MEM, 0); if (res->start != mdp->mdp_data->mdp_con_res) { platform_set_drvdata(pdev, mdp); goto success_return; } ret = mdp_mm_subsys_deploy(mdp, MDP_INFRA_MMSYS); if (ret) goto err_destroy_device; ret = mdp_mm_subsys_deploy(mdp, MDP_INFRA_MUTEX); if (ret) goto err_destroy_device; for (i = 0; i < mdp->mdp_data->pipe_info_len; i++) { enum mdp_mm_subsys_id idx; struct mtk_mutex **m; idx = mdp->mdp_data->pipe_info[i].sub_id; mutex_id = mdp->mdp_data->pipe_info[i].mutex_id; m = &mdp->mm_subsys[idx].mdp_mutex[mutex_id]; if (!IS_ERR_OR_NULL(*m)) continue; *m = mtk_mutex_get(mdp->mm_subsys[idx].mutex); if (IS_ERR(*m)) { ret = PTR_ERR(*m); goto err_free_mutex; } } ret = mdp_comp_config(mdp); if (ret) { dev_err(dev, "Failed to config mdp components\n"); goto err_free_mutex; } mdp->job_wq = alloc_workqueue(MDP_MODULE_NAME, WQ_FREEZABLE, 0); if (!mdp->job_wq) { dev_err(dev, "Unable to create job workqueue\n"); ret = -ENOMEM; goto err_deinit_comp; } mdp->clock_wq = alloc_workqueue(MDP_MODULE_NAME "-clock", WQ_FREEZABLE, 0); if (!mdp->clock_wq) { dev_err(dev, "Unable to create clock workqueue\n"); ret = -ENOMEM; goto err_destroy_job_wq; } mdp->scp = scp_get(pdev); if (!mdp->scp) { mm_pdev = __get_pdev_by_id(pdev, NULL, MDP_INFRA_SCP); if (WARN_ON(!mm_pdev)) { dev_err(&pdev->dev, "Could not get scp device\n"); ret = -ENODEV; goto err_destroy_clock_wq; } mdp->scp = platform_get_drvdata(mm_pdev); } mdp->rproc_handle = scp_get_rproc(mdp->scp); dev_dbg(&pdev->dev, "MDP rproc_handle: %pK", mdp->rproc_handle); mutex_init(&mdp->vpu_lock); mutex_init(&mdp->m2m_lock); for (i = 0; i < mdp->mdp_data->pp_used; i++) { mdp->cmdq_clt[i] = cmdq_mbox_create(dev, i); if (IS_ERR(mdp->cmdq_clt[i])) { ret = PTR_ERR(mdp->cmdq_clt[i]); goto err_mbox_destroy; } } init_waitqueue_head(&mdp->callback_wq); ida_init(&mdp->mdp_ida); platform_set_drvdata(pdev, mdp); vb2_dma_contig_set_max_seg_size(&pdev->dev, DMA_BIT_MASK(32)); ret = v4l2_device_register(dev, &mdp->v4l2_dev); if (ret) { dev_err(dev, "Failed to register v4l2 device\n"); ret = -EINVAL; goto err_mbox_destroy; } ret = mdp_m2m_device_register(mdp); if (ret) { v4l2_err(&mdp->v4l2_dev, "Failed to register m2m device\n"); goto err_unregister_device; } success_return: dev_dbg(dev, "mdp-%d registered successfully\n", pdev->id); return 0; err_unregister_device: v4l2_device_unregister(&mdp->v4l2_dev); err_mbox_destroy: while (--i >= 0) cmdq_mbox_destroy(mdp->cmdq_clt[i]); scp_put(mdp->scp); err_destroy_clock_wq: destroy_workqueue(mdp->clock_wq); err_destroy_job_wq: destroy_workqueue(mdp->job_wq); err_deinit_comp: mdp_comp_destroy(mdp); err_free_mutex: for (i = 0; i < mdp->mdp_data->pipe_info_len; i++) { enum mdp_mm_subsys_id idx; struct mtk_mutex *m; idx = mdp->mdp_data->pipe_info[i].sub_id; mutex_id = mdp->mdp_data->pipe_info[i].mutex_id; m = mdp->mm_subsys[idx].mdp_mutex[mutex_id]; if (!IS_ERR_OR_NULL(m)) mtk_mutex_put(m); } err_destroy_device: kfree(mdp); err_return: dev_dbg(dev, "Errno %d\n", ret); return ret; } static void mdp_remove(struct platform_device *pdev) { struct mdp_dev *mdp = platform_get_drvdata(pdev); v4l2_device_unregister(&mdp->v4l2_dev); dev_dbg(&pdev->dev, "%s driver unloaded\n", pdev->name); } static int __maybe_unused mdp_suspend(struct device *dev) { struct mdp_dev *mdp = dev_get_drvdata(dev); int ret; atomic_set(&mdp->suspended, 1); if (atomic_read(&mdp->job_count)) { ret = wait_event_timeout(mdp->callback_wq, !atomic_read(&mdp->job_count), 2 * HZ); if (ret == 0) { dev_err(dev, "%s:flushed cmdq task incomplete, count=%d\n", __func__, atomic_read(&mdp->job_count)); return -EBUSY; } } return 0; } static int __maybe_unused mdp_resume(struct device *dev) { struct mdp_dev *mdp = dev_get_drvdata(dev); atomic_set(&mdp->suspended, 0); return 0; } static const struct dev_pm_ops mdp_pm_ops = { SET_SYSTEM_SLEEP_PM_OPS(mdp_suspend, mdp_resume) }; static struct platform_driver mdp_driver = { .probe = mdp_probe, .remove_new = mdp_remove, .driver = { .name = MDP_MODULE_NAME, .pm = &mdp_pm_ops, .of_match_table = mdp_of_ids, }, }; module_platform_driver(mdp_driver); MODULE_AUTHOR("Ping-Hsun Wu <ping-hsun.wu@mediatek.com>"); MODULE_DESCRIPTION("MediaTek image processor 3 driver"); MODULE_LICENSE("GPL"); |