2 * Copyright (c) 2012-2016, The Linux Foundation. All rights reserved.
3 * Copyright (C) 2017 Linaro Ltd.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 and
7 * only version 2 as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
15 #include <linux/clk.h>
16 #include <linux/init.h>
17 #include <linux/ioctl.h>
18 #include <linux/list.h>
19 #include <linux/module.h>
20 #include <linux/of_device.h>
21 #include <linux/platform_device.h>
22 #include <linux/slab.h>
23 #include <linux/types.h>
24 #include <linux/pm_runtime.h>
25 #include <media/videobuf2-v4l2.h>
26 #include <media/v4l2-mem2mem.h>
27 #include <media/v4l2-ioctl.h>
34 static void venus_event_notify(struct venus_core *core, u32 event)
36 struct venus_inst *inst;
39 case EVT_SYS_WATCHDOG_TIMEOUT:
46 mutex_lock(&core->lock);
47 core->sys_error = true;
48 list_for_each_entry(inst, &core->instances, list)
49 inst->ops->event_notify(inst, EVT_SESSION_ERROR, NULL);
50 mutex_unlock(&core->lock);
52 disable_irq_nosync(core->irq);
55 * Delay recovery to ensure venus has completed any pending cache
56 * operations. Without this sleep, we see device reset when firmware is
57 * unloaded after a system error.
59 schedule_delayed_work(&core->work, msecs_to_jiffies(100));
62 static const struct hfi_core_ops venus_core_ops = {
63 .event_notify = venus_event_notify,
66 static void venus_sys_error_handler(struct work_struct *work)
68 struct venus_core *core =
69 container_of(work, struct venus_core, work.work);
72 dev_warn(core->dev, "system error has occurred, starting recovery!\n");
74 pm_runtime_get_sync(core->dev);
76 hfi_core_deinit(core, true);
78 mutex_lock(&core->lock);
79 venus_shutdown(core->dev);
81 pm_runtime_put_sync(core->dev);
83 ret |= hfi_create(core, &venus_core_ops);
85 pm_runtime_get_sync(core->dev);
87 ret |= venus_boot(core->dev, core->res->fwname);
89 ret |= hfi_core_resume(core, true);
91 enable_irq(core->irq);
93 mutex_unlock(&core->lock);
95 ret |= hfi_core_init(core);
97 pm_runtime_put_sync(core->dev);
100 disable_irq_nosync(core->irq);
101 dev_warn(core->dev, "recovery failed (%d)\n", ret);
102 schedule_delayed_work(&core->work, msecs_to_jiffies(10));
106 mutex_lock(&core->lock);
107 core->sys_error = false;
108 mutex_unlock(&core->lock);
111 static int venus_clks_get(struct venus_core *core)
113 const struct venus_resources *res = core->res;
114 struct device *dev = core->dev;
117 for (i = 0; i < res->clks_num; i++) {
118 core->clks[i] = devm_clk_get(dev, res->clks[i]);
119 if (IS_ERR(core->clks[i]))
120 return PTR_ERR(core->clks[i]);
126 static int venus_clks_enable(struct venus_core *core)
128 const struct venus_resources *res = core->res;
132 for (i = 0; i < res->clks_num; i++) {
133 ret = clk_prepare_enable(core->clks[i]);
141 clk_disable_unprepare(core->clks[i]);
146 static void venus_clks_disable(struct venus_core *core)
148 const struct venus_resources *res = core->res;
149 unsigned int i = res->clks_num;
152 clk_disable_unprepare(core->clks[i]);
155 static int venus_probe(struct platform_device *pdev)
157 struct device *dev = &pdev->dev;
158 struct venus_core *core;
162 core = devm_kzalloc(dev, sizeof(*core), GFP_KERNEL);
167 platform_set_drvdata(pdev, core);
169 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
170 core->base = devm_ioremap_resource(dev, r);
171 if (IS_ERR(core->base))
172 return PTR_ERR(core->base);
174 core->irq = platform_get_irq(pdev, 0);
178 core->res = of_device_get_match_data(dev);
182 ret = venus_clks_get(core);
186 ret = dma_set_mask_and_coherent(dev, core->res->dma_mask);
190 if (!dev->dma_parms) {
191 dev->dma_parms = devm_kzalloc(dev, sizeof(*dev->dma_parms),
196 dma_set_max_seg_size(dev, DMA_BIT_MASK(32));
198 INIT_LIST_HEAD(&core->instances);
199 mutex_init(&core->lock);
200 INIT_DELAYED_WORK(&core->work, venus_sys_error_handler);
202 ret = devm_request_threaded_irq(dev, core->irq, hfi_isr, hfi_isr_thread,
203 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
208 ret = hfi_create(core, &venus_core_ops);
212 pm_runtime_enable(dev);
214 ret = pm_runtime_get_sync(dev);
216 goto err_runtime_disable;
218 ret = venus_boot(dev, core->res->fwname);
220 goto err_runtime_disable;
222 ret = hfi_core_resume(core, true);
224 goto err_venus_shutdown;
226 ret = hfi_core_init(core);
228 goto err_venus_shutdown;
230 ret = v4l2_device_register(dev, &core->v4l2_dev);
232 goto err_core_deinit;
234 ret = of_platform_populate(dev->of_node, NULL, NULL, dev);
236 goto err_dev_unregister;
238 ret = pm_runtime_put_sync(dev);
240 pm_runtime_get_noresume(dev);
241 goto err_dev_unregister;
247 v4l2_device_unregister(&core->v4l2_dev);
249 hfi_core_deinit(core, false);
253 pm_runtime_put_noidle(dev);
254 pm_runtime_set_suspended(dev);
255 pm_runtime_disable(dev);
260 static int venus_remove(struct platform_device *pdev)
262 struct venus_core *core = platform_get_drvdata(pdev);
263 struct device *dev = core->dev;
266 ret = pm_runtime_get_sync(dev);
269 ret = hfi_core_deinit(core, true);
274 of_platform_depopulate(dev);
276 pm_runtime_put_sync(dev);
277 pm_runtime_disable(dev);
279 v4l2_device_unregister(&core->v4l2_dev);
284 static __maybe_unused int venus_runtime_suspend(struct device *dev)
286 struct venus_core *core = dev_get_drvdata(dev);
289 ret = hfi_core_suspend(core);
291 venus_clks_disable(core);
296 static __maybe_unused int venus_runtime_resume(struct device *dev)
298 struct venus_core *core = dev_get_drvdata(dev);
301 ret = venus_clks_enable(core);
305 ret = hfi_core_resume(core, false);
307 goto err_clks_disable;
312 venus_clks_disable(core);
316 static const struct dev_pm_ops venus_pm_ops = {
317 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
318 pm_runtime_force_resume)
319 SET_RUNTIME_PM_OPS(venus_runtime_suspend, venus_runtime_resume, NULL)
322 static const struct freq_tbl msm8916_freq_table[] = {
323 { 352800, 228570000 }, /* 1920x1088 @ 30 + 1280x720 @ 30 */
324 { 244800, 160000000 }, /* 1920x1088 @ 30 */
325 { 108000, 100000000 }, /* 1280x720 @ 30 */
328 static const struct reg_val msm8916_reg_preset[] = {
329 { 0xe0020, 0x05555556 },
330 { 0xe0024, 0x05555556 },
331 { 0x80124, 0x00000003 },
334 static const struct venus_resources msm8916_res = {
335 .freq_tbl = msm8916_freq_table,
336 .freq_tbl_size = ARRAY_SIZE(msm8916_freq_table),
337 .reg_tbl = msm8916_reg_preset,
338 .reg_tbl_size = ARRAY_SIZE(msm8916_reg_preset),
339 .clks = { "core", "iface", "bus", },
341 .max_load = 352800, /* 720p@30 + 1080p@30 */
342 .hfi_version = HFI_VERSION_1XX,
343 .vmem_id = VIDC_RESOURCE_NONE,
346 .dma_mask = 0xddc00000 - 1,
347 .fwname = "/*(DEBLOBBED)*/",
350 static const struct freq_tbl msm8996_freq_table[] = {
351 { 1944000, 520000000 }, /* 4k UHD @ 60 (decode only) */
352 { 972000, 520000000 }, /* 4k UHD @ 30 */
353 { 489600, 346666667 }, /* 1080p @ 60 */
354 { 244800, 150000000 }, /* 1080p @ 30 */
355 { 108000, 75000000 }, /* 720p @ 30 */
358 static const struct reg_val msm8996_reg_preset[] = {
359 { 0x80010, 0xffffffff },
360 { 0x80018, 0x00001556 },
361 { 0x8001C, 0x00001556 },
364 static const struct venus_resources msm8996_res = {
365 .freq_tbl = msm8996_freq_table,
366 .freq_tbl_size = ARRAY_SIZE(msm8996_freq_table),
367 .reg_tbl = msm8996_reg_preset,
368 .reg_tbl_size = ARRAY_SIZE(msm8996_reg_preset),
369 .clks = {"core", "iface", "bus", "mbus" },
372 .hfi_version = HFI_VERSION_3XX,
373 .vmem_id = VIDC_RESOURCE_NONE,
376 .dma_mask = 0xddc00000 - 1,
377 .fwname = "/*(DEBLOBBED)*/",
380 static const struct of_device_id venus_dt_match[] = {
381 { .compatible = "qcom,msm8916-venus", .data = &msm8916_res, },
382 { .compatible = "qcom,msm8996-venus", .data = &msm8996_res, },
385 MODULE_DEVICE_TABLE(of, venus_dt_match);
387 static struct platform_driver qcom_venus_driver = {
388 .probe = venus_probe,
389 .remove = venus_remove,
391 .name = "qcom-venus",
392 .of_match_table = venus_dt_match,
396 module_platform_driver(qcom_venus_driver);
398 MODULE_ALIAS("platform:qcom-venus");
399 MODULE_DESCRIPTION("Qualcomm Venus video encoder and decoder driver");
400 MODULE_LICENSE("GPL v2");