1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2012-2016, The Linux Foundation. All rights reserved.
4 * Copyright (C) 2017 Linaro Ltd.
7 #include <linux/init.h>
8 #include <linux/interconnect.h>
9 #include <linux/ioctl.h>
10 #include <linux/list.h>
11 #include <linux/module.h>
12 #include <linux/of_device.h>
13 #include <linux/platform_device.h>
14 #include <linux/slab.h>
15 #include <linux/types.h>
16 #include <linux/pm_runtime.h>
17 #include <media/videobuf2-v4l2.h>
18 #include <media/v4l2-mem2mem.h>
19 #include <media/v4l2-ioctl.h>
26 static void venus_event_notify(struct venus_core *core, u32 event)
28 struct venus_inst *inst;
31 case EVT_SYS_WATCHDOG_TIMEOUT:
38 mutex_lock(&core->lock);
39 core->sys_error = true;
40 list_for_each_entry(inst, &core->instances, list)
41 inst->ops->event_notify(inst, EVT_SESSION_ERROR, NULL);
42 mutex_unlock(&core->lock);
44 disable_irq_nosync(core->irq);
47 * Delay recovery to ensure venus has completed any pending cache
48 * operations. Without this sleep, we see device reset when firmware is
49 * unloaded after a system error.
51 schedule_delayed_work(&core->work, msecs_to_jiffies(100));
54 static const struct hfi_core_ops venus_core_ops = {
55 .event_notify = venus_event_notify,
58 static void venus_sys_error_handler(struct work_struct *work)
60 struct venus_core *core =
61 container_of(work, struct venus_core, work.work);
64 dev_warn(core->dev, "system error has occurred, starting recovery!\n");
66 pm_runtime_get_sync(core->dev);
68 hfi_core_deinit(core, true);
70 mutex_lock(&core->lock);
73 pm_runtime_put_sync(core->dev);
75 ret |= hfi_create(core, &venus_core_ops);
77 pm_runtime_get_sync(core->dev);
79 ret |= venus_boot(core);
81 ret |= hfi_core_resume(core, true);
83 enable_irq(core->irq);
85 mutex_unlock(&core->lock);
87 ret |= hfi_core_init(core);
89 pm_runtime_put_sync(core->dev);
92 disable_irq_nosync(core->irq);
93 dev_warn(core->dev, "recovery failed (%d)\n", ret);
94 schedule_delayed_work(&core->work, msecs_to_jiffies(10));
98 mutex_lock(&core->lock);
99 core->sys_error = false;
100 mutex_unlock(&core->lock);
103 static int venus_clks_get(struct venus_core *core)
105 const struct venus_resources *res = core->res;
106 struct device *dev = core->dev;
109 for (i = 0; i < res->clks_num; i++) {
110 core->clks[i] = devm_clk_get(dev, res->clks[i]);
111 if (IS_ERR(core->clks[i]))
112 return PTR_ERR(core->clks[i]);
118 static int venus_clks_enable(struct venus_core *core)
120 const struct venus_resources *res = core->res;
124 for (i = 0; i < res->clks_num; i++) {
125 ret = clk_prepare_enable(core->clks[i]);
133 clk_disable_unprepare(core->clks[i]);
138 static void venus_clks_disable(struct venus_core *core)
140 const struct venus_resources *res = core->res;
141 unsigned int i = res->clks_num;
144 clk_disable_unprepare(core->clks[i]);
147 static u32 to_v4l2_codec_type(u32 codec)
150 case HFI_VIDEO_CODEC_H264:
151 return V4L2_PIX_FMT_H264;
152 case HFI_VIDEO_CODEC_H263:
153 return V4L2_PIX_FMT_H263;
154 case HFI_VIDEO_CODEC_MPEG1:
155 return V4L2_PIX_FMT_MPEG1;
156 case HFI_VIDEO_CODEC_MPEG2:
157 return V4L2_PIX_FMT_MPEG2;
158 case HFI_VIDEO_CODEC_MPEG4:
159 return V4L2_PIX_FMT_MPEG4;
160 case HFI_VIDEO_CODEC_VC1:
161 return V4L2_PIX_FMT_VC1_ANNEX_G;
162 case HFI_VIDEO_CODEC_VP8:
163 return V4L2_PIX_FMT_VP8;
164 case HFI_VIDEO_CODEC_VP9:
165 return V4L2_PIX_FMT_VP9;
166 case HFI_VIDEO_CODEC_DIVX:
167 case HFI_VIDEO_CODEC_DIVX_311:
168 return V4L2_PIX_FMT_XVID;
174 static int venus_enumerate_codecs(struct venus_core *core, u32 type)
176 const struct hfi_inst_ops dummy_ops = {};
177 struct venus_inst *inst;
182 if (core->res->hfi_version != HFI_VERSION_1XX)
185 inst = kzalloc(sizeof(*inst), GFP_KERNEL);
189 mutex_init(&inst->lock);
191 inst->session_type = type;
192 if (type == VIDC_SESSION_TYPE_DEC)
193 codecs = core->dec_codecs;
195 codecs = core->enc_codecs;
197 ret = hfi_session_create(inst, &dummy_ops);
201 for (i = 0; i < MAX_CODEC_NUM; i++) {
202 codec = (1UL << i) & codecs;
206 ret = hfi_session_init(inst, to_v4l2_codec_type(codec));
210 ret = hfi_session_deinit(inst);
216 hfi_session_destroy(inst);
218 mutex_destroy(&inst->lock);
224 static int venus_probe(struct platform_device *pdev)
226 struct device *dev = &pdev->dev;
227 struct venus_core *core;
231 core = devm_kzalloc(dev, sizeof(*core), GFP_KERNEL);
236 platform_set_drvdata(pdev, core);
238 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
239 core->base = devm_ioremap_resource(dev, r);
240 if (IS_ERR(core->base))
241 return PTR_ERR(core->base);
243 core->video_path = of_icc_get(dev, "video-mem");
244 if (IS_ERR(core->video_path))
245 return PTR_ERR(core->video_path);
247 core->cpucfg_path = of_icc_get(dev, "cpu-cfg");
248 if (IS_ERR(core->cpucfg_path))
249 return PTR_ERR(core->cpucfg_path);
251 core->irq = platform_get_irq(pdev, 0);
255 core->res = of_device_get_match_data(dev);
259 ret = venus_clks_get(core);
263 ret = dma_set_mask_and_coherent(dev, core->res->dma_mask);
267 if (!dev->dma_parms) {
268 dev->dma_parms = devm_kzalloc(dev, sizeof(*dev->dma_parms),
273 dma_set_max_seg_size(dev, DMA_BIT_MASK(32));
275 INIT_LIST_HEAD(&core->instances);
276 mutex_init(&core->lock);
277 INIT_DELAYED_WORK(&core->work, venus_sys_error_handler);
279 ret = devm_request_threaded_irq(dev, core->irq, hfi_isr, hfi_isr_thread,
280 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
285 ret = icc_set_bw(core->cpucfg_path, 0, kbps_to_icc(1000));
289 ret = hfi_create(core, &venus_core_ops);
293 pm_runtime_enable(dev);
295 ret = pm_runtime_get_sync(dev);
297 goto err_runtime_disable;
299 ret = of_platform_populate(dev->of_node, NULL, NULL, dev);
301 goto err_runtime_disable;
303 ret = venus_firmware_init(core);
305 goto err_runtime_disable;
307 ret = venus_boot(core);
309 goto err_runtime_disable;
311 ret = hfi_core_resume(core, true);
313 goto err_venus_shutdown;
315 ret = hfi_core_init(core);
317 goto err_venus_shutdown;
319 ret = venus_enumerate_codecs(core, VIDC_SESSION_TYPE_DEC);
321 goto err_venus_shutdown;
323 ret = venus_enumerate_codecs(core, VIDC_SESSION_TYPE_ENC);
325 goto err_venus_shutdown;
327 ret = v4l2_device_register(dev, &core->v4l2_dev);
329 goto err_core_deinit;
331 ret = pm_runtime_put_sync(dev);
333 goto err_dev_unregister;
338 v4l2_device_unregister(&core->v4l2_dev);
340 hfi_core_deinit(core, false);
342 venus_shutdown(core);
344 pm_runtime_set_suspended(dev);
345 pm_runtime_disable(dev);
350 static int venus_remove(struct platform_device *pdev)
352 struct venus_core *core = platform_get_drvdata(pdev);
353 struct device *dev = core->dev;
356 ret = pm_runtime_get_sync(dev);
359 ret = hfi_core_deinit(core, true);
363 venus_shutdown(core);
364 of_platform_depopulate(dev);
366 venus_firmware_deinit(core);
368 pm_runtime_put_sync(dev);
369 pm_runtime_disable(dev);
371 icc_put(core->video_path);
372 icc_put(core->cpucfg_path);
374 v4l2_device_unregister(&core->v4l2_dev);
379 static __maybe_unused int venus_runtime_suspend(struct device *dev)
381 struct venus_core *core = dev_get_drvdata(dev);
384 ret = hfi_core_suspend(core);
386 venus_clks_disable(core);
391 static __maybe_unused int venus_runtime_resume(struct device *dev)
393 struct venus_core *core = dev_get_drvdata(dev);
396 ret = venus_clks_enable(core);
400 ret = hfi_core_resume(core, false);
402 goto err_clks_disable;
407 venus_clks_disable(core);
411 static const struct dev_pm_ops venus_pm_ops = {
412 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
413 pm_runtime_force_resume)
414 SET_RUNTIME_PM_OPS(venus_runtime_suspend, venus_runtime_resume, NULL)
417 static const struct freq_tbl msm8916_freq_table[] = {
418 { 352800, 228570000 }, /* 1920x1088 @ 30 + 1280x720 @ 30 */
419 { 244800, 160000000 }, /* 1920x1088 @ 30 */
420 { 108000, 100000000 }, /* 1280x720 @ 30 */
423 static const struct reg_val msm8916_reg_preset[] = {
424 { 0xe0020, 0x05555556 },
425 { 0xe0024, 0x05555556 },
426 { 0x80124, 0x00000003 },
429 static const struct venus_resources msm8916_res = {
430 .freq_tbl = msm8916_freq_table,
431 .freq_tbl_size = ARRAY_SIZE(msm8916_freq_table),
432 .reg_tbl = msm8916_reg_preset,
433 .reg_tbl_size = ARRAY_SIZE(msm8916_reg_preset),
434 .clks = { "core", "iface", "bus", },
436 .max_load = 352800, /* 720p@30 + 1080p@30 */
437 .hfi_version = HFI_VERSION_1XX,
438 .vmem_id = VIDC_RESOURCE_NONE,
441 .dma_mask = 0xddc00000 - 1,
442 .fwname = "qcom/venus-1.8/venus.mdt",
445 static const struct freq_tbl msm8996_freq_table[] = {
446 { 1944000, 520000000 }, /* 4k UHD @ 60 (decode only) */
447 { 972000, 520000000 }, /* 4k UHD @ 30 */
448 { 489600, 346666667 }, /* 1080p @ 60 */
449 { 244800, 150000000 }, /* 1080p @ 30 */
450 { 108000, 75000000 }, /* 720p @ 30 */
453 static const struct reg_val msm8996_reg_preset[] = {
454 { 0x80010, 0xffffffff },
455 { 0x80018, 0x00001556 },
456 { 0x8001C, 0x00001556 },
459 static const struct venus_resources msm8996_res = {
460 .freq_tbl = msm8996_freq_table,
461 .freq_tbl_size = ARRAY_SIZE(msm8996_freq_table),
462 .reg_tbl = msm8996_reg_preset,
463 .reg_tbl_size = ARRAY_SIZE(msm8996_reg_preset),
464 .clks = {"core", "iface", "bus", "mbus" },
467 .hfi_version = HFI_VERSION_3XX,
468 .vmem_id = VIDC_RESOURCE_NONE,
471 .dma_mask = 0xddc00000 - 1,
472 .fwname = "qcom/venus-4.2/venus.mdt",
475 static const struct freq_tbl sdm845_freq_table[] = {
476 { 3110400, 533000000 }, /* 4096x2160@90 */
477 { 2073600, 444000000 }, /* 4096x2160@60 */
478 { 1944000, 404000000 }, /* 3840x2160@60 */
479 { 972000, 330000000 }, /* 3840x2160@30 */
480 { 489600, 200000000 }, /* 1920x1080@60 */
481 { 244800, 100000000 }, /* 1920x1080@30 */
484 static const struct bw_tbl sdm845_bw_table_enc[] = {
485 { 1944000, 1612000, 0, 2416000, 0 }, /* 3840x2160@60 */
486 { 972000, 951000, 0, 1434000, 0 }, /* 3840x2160@30 */
487 { 489600, 723000, 0, 973000, 0 }, /* 1920x1080@60 */
488 { 244800, 370000, 0, 495000, 0 }, /* 1920x1080@30 */
491 static const struct bw_tbl sdm845_bw_table_dec[] = {
492 { 2073600, 3929000, 0, 5551000, 0 }, /* 4096x2160@60 */
493 { 1036800, 1987000, 0, 2797000, 0 }, /* 4096x2160@30 */
494 { 489600, 1040000, 0, 1298000, 0 }, /* 1920x1080@60 */
495 { 244800, 530000, 0, 659000, 0 }, /* 1920x1080@30 */
498 static const struct venus_resources sdm845_res = {
499 .freq_tbl = sdm845_freq_table,
500 .freq_tbl_size = ARRAY_SIZE(sdm845_freq_table),
501 .bw_tbl_enc = sdm845_bw_table_enc,
502 .bw_tbl_enc_size = ARRAY_SIZE(sdm845_bw_table_enc),
503 .bw_tbl_dec = sdm845_bw_table_dec,
504 .bw_tbl_dec_size = ARRAY_SIZE(sdm845_bw_table_dec),
505 .clks = {"core", "iface", "bus" },
507 .max_load = 3110400, /* 4096x2160@90 */
508 .hfi_version = HFI_VERSION_4XX,
509 .vmem_id = VIDC_RESOURCE_NONE,
512 .dma_mask = 0xe0000000 - 1,
513 .fwname = "qcom/venus-5.2/venus.mdt",
516 static const struct of_device_id venus_dt_match[] = {
517 { .compatible = "qcom,msm8916-venus", .data = &msm8916_res, },
518 { .compatible = "qcom,msm8996-venus", .data = &msm8996_res, },
519 { .compatible = "qcom,sdm845-venus", .data = &sdm845_res, },
522 MODULE_DEVICE_TABLE(of, venus_dt_match);
524 static struct platform_driver qcom_venus_driver = {
525 .probe = venus_probe,
526 .remove = venus_remove,
528 .name = "qcom-venus",
529 .of_match_table = venus_dt_match,
533 module_platform_driver(qcom_venus_driver);
535 MODULE_ALIAS("platform:qcom-venus");
536 MODULE_DESCRIPTION("Qualcomm Venus video encoder and decoder driver");
537 MODULE_LICENSE("GPL v2");