blob: 9d5f255f324d34bb6c4aac30013515c8e55216b8 [file] [log] [blame]
Srinu Gorlecf8c6752018-01-19 18:36:13 +05301/* Copyright (c) 2012-2018, The Linux Foundation. All rights reserved.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 */
13
14#include <linux/debugfs.h>
15#include <linux/dma-mapping.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_platform.h>
21#include <linux/platform_device.h>
22#include <linux/slab.h>
23#include <linux/types.h>
24#include <linux/version.h>
25#include <linux/io.h>
26#include <media/msm_vidc.h>
27#include "msm_vidc_common.h"
28#include "msm_vidc_debug.h"
29#include "msm_vidc_internal.h"
30#include "msm_vidc_res_parse.h"
31#include "msm_vidc_resources.h"
32#include "venus_boot.h"
33#include "vidc_hfi_api.h"
34
35#define BASE_DEVICE_NUMBER 32
36
37struct msm_vidc_drv *vidc_driver;
38
39uint32_t msm_vidc_pwr_collapse_delay = 2000;
40
41static inline struct msm_vidc_inst *get_vidc_inst(struct file *filp, void *fh)
42{
43 return container_of(filp->private_data,
44 struct msm_vidc_inst, event_handler);
45}
46
47static int msm_v4l2_open(struct file *filp)
48{
49 struct video_device *vdev = video_devdata(filp);
50 struct msm_video_device *vid_dev =
51 container_of(vdev, struct msm_video_device, vdev);
52 struct msm_vidc_core *core = video_drvdata(filp);
53 struct msm_vidc_inst *vidc_inst;
54
55 trace_msm_v4l2_vidc_open_start("msm_v4l2_open start");
56 vidc_inst = msm_vidc_open(core->id, vid_dev->type);
57 if (!vidc_inst) {
58 dprintk(VIDC_ERR,
59 "Failed to create video instance, core: %d, type = %d\n",
60 core->id, vid_dev->type);
61 return -ENOMEM;
62 }
63 clear_bit(V4L2_FL_USES_V4L2_FH, &vdev->flags);
64 filp->private_data = &(vidc_inst->event_handler);
65 trace_msm_v4l2_vidc_open_end("msm_v4l2_open end");
66 return 0;
67}
68
69static int msm_v4l2_close(struct file *filp)
70{
71 int rc = 0;
72 struct msm_vidc_inst *vidc_inst;
73
74 trace_msm_v4l2_vidc_close_start("msm_v4l2_close start");
75 vidc_inst = get_vidc_inst(filp, NULL);
76 rc = msm_vidc_release_buffers(vidc_inst,
77 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
78 if (rc)
79 dprintk(VIDC_WARN,
80 "Failed in %s for release output buffers\n", __func__);
81
82 rc = msm_vidc_close(vidc_inst);
83 trace_msm_v4l2_vidc_close_end("msm_v4l2_close end");
84 return rc;
85}
86
87static int msm_v4l2_querycap(struct file *filp, void *fh,
88 struct v4l2_capability *cap)
89{
90 struct msm_vidc_inst *vidc_inst = get_vidc_inst(filp, fh);
91
92 return msm_vidc_querycap((void *)vidc_inst, cap);
93}
94
95int msm_v4l2_enum_fmt(struct file *file, void *fh,
96 struct v4l2_fmtdesc *f)
97{
98 struct msm_vidc_inst *vidc_inst = get_vidc_inst(file, fh);
99
100 return msm_vidc_enum_fmt((void *)vidc_inst, f);
101}
102
103int msm_v4l2_s_fmt(struct file *file, void *fh,
104 struct v4l2_format *f)
105{
106 struct msm_vidc_inst *vidc_inst = get_vidc_inst(file, fh);
107
108 return msm_vidc_s_fmt((void *)vidc_inst, f);
109}
110
111int msm_v4l2_g_fmt(struct file *file, void *fh,
112 struct v4l2_format *f)
113{
114 struct msm_vidc_inst *vidc_inst = get_vidc_inst(file, fh);
115
116 return msm_vidc_g_fmt((void *)vidc_inst, f);
117}
118
119int msm_v4l2_s_ctrl(struct file *file, void *fh,
120 struct v4l2_control *a)
121{
122 struct msm_vidc_inst *vidc_inst = get_vidc_inst(file, fh);
123
124 return msm_vidc_s_ctrl((void *)vidc_inst, a);
125}
126
127int msm_v4l2_g_ctrl(struct file *file, void *fh,
128 struct v4l2_control *a)
129{
130 struct msm_vidc_inst *vidc_inst = get_vidc_inst(file, fh);
131
132 return msm_vidc_g_ctrl((void *)vidc_inst, a);
133}
134
135int msm_v4l2_s_ext_ctrl(struct file *file, void *fh,
136 struct v4l2_ext_controls *a)
137{
138 struct msm_vidc_inst *vidc_inst = get_vidc_inst(file, fh);
139
140 return msm_vidc_s_ext_ctrl((void *)vidc_inst, a);
141}
142
143int msm_v4l2_reqbufs(struct file *file, void *fh,
144 struct v4l2_requestbuffers *b)
145{
146 struct msm_vidc_inst *vidc_inst = get_vidc_inst(file, fh);
147 int rc = 0;
148
149 if (!b->count)
150 rc = msm_vidc_release_buffers(vidc_inst, b->type);
151 if (rc)
152 dprintk(VIDC_WARN,
153 "Failed in %s for release output buffers\n", __func__);
154 return msm_vidc_reqbufs((void *)vidc_inst, b);
155}
156
157int msm_v4l2_prepare_buf(struct file *file, void *fh,
158 struct v4l2_buffer *b)
159{
160 return msm_vidc_prepare_buf(get_vidc_inst(file, fh), b);
161}
162
163int msm_v4l2_qbuf(struct file *file, void *fh,
164 struct v4l2_buffer *b)
165{
166 return msm_vidc_qbuf(get_vidc_inst(file, fh), b);
167}
168
169int msm_v4l2_dqbuf(struct file *file, void *fh,
170 struct v4l2_buffer *b)
171{
172 return msm_vidc_dqbuf(get_vidc_inst(file, fh), b);
173}
174
175int msm_v4l2_streamon(struct file *file, void *fh,
176 enum v4l2_buf_type i)
177{
178 struct msm_vidc_inst *vidc_inst = get_vidc_inst(file, fh);
179
180 return msm_vidc_streamon((void *)vidc_inst, i);
181}
182
183int msm_v4l2_streamoff(struct file *file, void *fh,
184 enum v4l2_buf_type i)
185{
186 struct msm_vidc_inst *vidc_inst = get_vidc_inst(file, fh);
187
188 return msm_vidc_streamoff((void *)vidc_inst, i);
189}
190
191static int msm_v4l2_subscribe_event(struct v4l2_fh *fh,
192 const struct v4l2_event_subscription *sub)
193{
194 struct msm_vidc_inst *vidc_inst = container_of(fh,
195 struct msm_vidc_inst, event_handler);
196
197 return msm_vidc_subscribe_event((void *)vidc_inst, sub);
198}
199
200static int msm_v4l2_unsubscribe_event(struct v4l2_fh *fh,
201 const struct v4l2_event_subscription *sub)
202{
203 struct msm_vidc_inst *vidc_inst = container_of(fh,
204 struct msm_vidc_inst, event_handler);
205
206 return msm_vidc_unsubscribe_event((void *)vidc_inst, sub);
207}
208
209static int msm_v4l2_decoder_cmd(struct file *file, void *fh,
210 struct v4l2_decoder_cmd *dec)
211{
212 struct msm_vidc_inst *vidc_inst = get_vidc_inst(file, fh);
213
214 return msm_vidc_comm_cmd((void *)vidc_inst, (union msm_v4l2_cmd *)dec);
215}
216
217static int msm_v4l2_encoder_cmd(struct file *file, void *fh,
218 struct v4l2_encoder_cmd *enc)
219{
220 struct msm_vidc_inst *vidc_inst = get_vidc_inst(file, fh);
221
222 return msm_vidc_comm_cmd((void *)vidc_inst, (union msm_v4l2_cmd *)enc);
223}
224static int msm_v4l2_s_parm(struct file *file, void *fh,
225 struct v4l2_streamparm *a)
226{
227 struct msm_vidc_inst *vidc_inst = get_vidc_inst(file, fh);
228
229 return msm_vidc_comm_s_parm(vidc_inst, a);
230}
231static int msm_v4l2_g_parm(struct file *file, void *fh,
232 struct v4l2_streamparm *a)
233{
234 return 0;
235}
236
237static int msm_v4l2_enum_framesizes(struct file *file, void *fh,
238 struct v4l2_frmsizeenum *fsize)
239{
240 struct msm_vidc_inst *vidc_inst = get_vidc_inst(file, fh);
241
242 return msm_vidc_enum_framesizes((void *)vidc_inst, fsize);
243}
244
245static const struct v4l2_ioctl_ops msm_v4l2_ioctl_ops = {
246 .vidioc_querycap = msm_v4l2_querycap,
247 .vidioc_enum_fmt_vid_cap_mplane = msm_v4l2_enum_fmt,
248 .vidioc_enum_fmt_vid_out_mplane = msm_v4l2_enum_fmt,
249 .vidioc_s_fmt_vid_cap_mplane = msm_v4l2_s_fmt,
250 .vidioc_s_fmt_vid_out_mplane = msm_v4l2_s_fmt,
251 .vidioc_g_fmt_vid_cap_mplane = msm_v4l2_g_fmt,
252 .vidioc_g_fmt_vid_out_mplane = msm_v4l2_g_fmt,
253 .vidioc_reqbufs = msm_v4l2_reqbufs,
254 .vidioc_prepare_buf = msm_v4l2_prepare_buf,
255 .vidioc_qbuf = msm_v4l2_qbuf,
256 .vidioc_dqbuf = msm_v4l2_dqbuf,
257 .vidioc_streamon = msm_v4l2_streamon,
258 .vidioc_streamoff = msm_v4l2_streamoff,
259 .vidioc_s_ctrl = msm_v4l2_s_ctrl,
260 .vidioc_g_ctrl = msm_v4l2_g_ctrl,
261 .vidioc_s_ext_ctrls = msm_v4l2_s_ext_ctrl,
262 .vidioc_subscribe_event = msm_v4l2_subscribe_event,
263 .vidioc_unsubscribe_event = msm_v4l2_unsubscribe_event,
264 .vidioc_decoder_cmd = msm_v4l2_decoder_cmd,
265 .vidioc_encoder_cmd = msm_v4l2_encoder_cmd,
266 .vidioc_s_parm = msm_v4l2_s_parm,
267 .vidioc_g_parm = msm_v4l2_g_parm,
268 .vidioc_enum_framesizes = msm_v4l2_enum_framesizes,
269};
270
271static const struct v4l2_ioctl_ops msm_v4l2_enc_ioctl_ops = {
272};
273
274static unsigned int msm_v4l2_poll(struct file *filp,
275 struct poll_table_struct *pt)
276{
277 struct msm_vidc_inst *vidc_inst = get_vidc_inst(filp, NULL);
278
279 return msm_vidc_poll((void *)vidc_inst, filp, pt);
280}
281
282static const struct v4l2_file_operations msm_v4l2_vidc_fops = {
283 .owner = THIS_MODULE,
284 .open = msm_v4l2_open,
285 .release = msm_v4l2_close,
286 .unlocked_ioctl = video_ioctl2,
287 .poll = msm_v4l2_poll,
288};
289
290void msm_vidc_release_video_device(struct video_device *pvdev)
291{
292}
293
294static int read_platform_resources(struct msm_vidc_core *core,
295 struct platform_device *pdev)
296{
297 if (!core || !pdev) {
298 dprintk(VIDC_ERR, "%s: Invalid params %pK %pK\n",
299 __func__, core, pdev);
300 return -EINVAL;
301 }
302
303 core->hfi_type = VIDC_HFI_VENUS;
304 core->resources.pdev = pdev;
305 if (pdev->dev.of_node) {
306 /* Target supports DT, parse from it */
307 return read_platform_resources_from_dt(&core->resources);
308 }
309 dprintk(VIDC_ERR, "pdev node is NULL\n");
310 return -EINVAL;
311}
312
313static int msm_vidc_initialize_core(struct platform_device *pdev,
314 struct msm_vidc_core *core)
315{
316 int i = 0;
317 int rc = 0;
318
319 if (!core)
320 return -EINVAL;
321 rc = read_platform_resources(core, pdev);
322 if (rc) {
323 dprintk(VIDC_ERR, "Failed to get platform resources\n");
324 return rc;
325 }
326
327 INIT_LIST_HEAD(&core->instances);
328 mutex_init(&core->lock);
329
330 core->state = VIDC_CORE_UNINIT;
331 for (i = SYS_MSG_INDEX(SYS_MSG_START);
332 i <= SYS_MSG_INDEX(SYS_MSG_END); i++) {
333 init_completion(&core->completions[i]);
334 }
335
336 INIT_DELAYED_WORK(&core->fw_unload_work, msm_vidc_fw_unload_handler);
337 return rc;
338}
339
340static ssize_t msm_vidc_link_name_show(struct device *dev,
341 struct device_attribute *attr,
342 char *buf)
343{
344 struct msm_vidc_core *core = dev_get_drvdata(dev);
345
346 if (core)
347 if (dev == &core->vdev[MSM_VIDC_DECODER].vdev.dev)
348 return snprintf(buf, PAGE_SIZE, "venus_dec");
349 else if (dev == &core->vdev[MSM_VIDC_ENCODER].vdev.dev)
350 return snprintf(buf, PAGE_SIZE, "venus_enc");
351 else
352 return 0;
353 else
354 return 0;
355}
356
357static DEVICE_ATTR(link_name, 0444, msm_vidc_link_name_show, NULL);
358
359static ssize_t store_pwr_collapse_delay(struct device *dev,
360 struct device_attribute *attr,
361 const char *buf, size_t count)
362{
363 unsigned long val = 0;
364 int rc = 0;
365
366 rc = kstrtoul(buf, 0, &val);
367 if (rc)
368 return rc;
369 else if (!val)
370 return -EINVAL;
371 msm_vidc_pwr_collapse_delay = val;
372 return count;
373}
374
375static ssize_t show_pwr_collapse_delay(struct device *dev,
376 struct device_attribute *attr,
377 char *buf)
378{
379 return snprintf(buf, PAGE_SIZE, "%u\n", msm_vidc_pwr_collapse_delay);
380}
381
382static DEVICE_ATTR(pwr_collapse_delay, 0644, show_pwr_collapse_delay,
383 store_pwr_collapse_delay);
384
385static ssize_t show_thermal_level(struct device *dev,
386 struct device_attribute *attr,
387 char *buf)
388{
389 return snprintf(buf, PAGE_SIZE, "%d\n", vidc_driver->thermal_level);
390}
391
392static ssize_t store_thermal_level(struct device *dev,
393 struct device_attribute *attr,
394 const char *buf, size_t count)
395{
396 int rc = 0, val = 0;
397
398 rc = kstrtoint(buf, 0, &val);
399 if (rc || val < 0) {
400 dprintk(VIDC_WARN,
401 "Invalid thermal level value: %s\n", buf);
402 return -EINVAL;
403 }
404 dprintk(VIDC_DBG, "Thermal level old %d new %d\n",
405 vidc_driver->thermal_level, val);
406
407 if (val == vidc_driver->thermal_level)
408 return count;
409 vidc_driver->thermal_level = val;
410
411 msm_comm_handle_thermal_event();
412 return count;
413}
414
415static DEVICE_ATTR(thermal_level, 0644, show_thermal_level,
416 store_thermal_level);
417
418static ssize_t show_platform_version(struct device *dev,
419 struct device_attribute *attr, char *buf)
420{
421 return scnprintf(buf, PAGE_SIZE, "%d",
422 vidc_driver->platform_version);
423}
424
425static ssize_t store_platform_version(struct device *dev,
426 struct device_attribute *attr, const char *buf,
427 size_t count)
428{
429 dprintk(VIDC_WARN, "store platform version is not allowed\n");
430 return count;
431}
432
433static DEVICE_ATTR(platform_version, 0444, show_platform_version,
434 store_platform_version);
435
436static ssize_t show_capability_version(struct device *dev,
437 struct device_attribute *attr, char *buf)
438{
439 return scnprintf(buf, PAGE_SIZE, "%d",
440 vidc_driver->capability_version);
441}
442
443static ssize_t store_capability_version(struct device *dev,
444 struct device_attribute *attr, const char *buf,
445 size_t count)
446{
447 dprintk(VIDC_WARN, "store capability version is not allowed\n");
448 return count;
449}
450
451static DEVICE_ATTR(capability_version, 0444, show_capability_version,
452 store_capability_version);
453
454static struct attribute *msm_vidc_core_attrs[] = {
455 &dev_attr_pwr_collapse_delay.attr,
456 &dev_attr_thermal_level.attr,
457 &dev_attr_platform_version.attr,
458 &dev_attr_capability_version.attr,
459 NULL
460};
461
462static struct attribute_group msm_vidc_core_attr_group = {
463 .attrs = msm_vidc_core_attrs,
464};
465
466static const struct of_device_id msm_vidc_dt_match[] = {
467 {.compatible = "qcom,msm-vidc"},
468 {.compatible = "qcom,msm-vidc,context-bank"},
469 {.compatible = "qcom,msm-vidc,bus"},
470 {}
471};
472
473static u32 msm_vidc_read_efuse_version(struct platform_device *pdev,
474 struct version_table *table, const char *fuse_name)
475{
476 void __iomem *base;
477 struct resource *res;
478 u32 ret = 0;
479
480 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, fuse_name);
481 if (!res) {
482 dprintk(VIDC_DBG, "Failed to get resource %s\n", fuse_name);
483 goto exit;
484 }
485 base = devm_ioremap(&pdev->dev, res->start, resource_size(res));
486 if (!base) {
487 dprintk(VIDC_ERR,
488 "failed ioremap: res->start %#x, size %d\n",
489 (u32)res->start, (u32)resource_size(res));
490 goto exit;
491 } else {
492 ret = readl_relaxed(base);
493 ret = (ret & table->version_mask) >>
494 table->version_shift;
495
496 devm_iounmap(&pdev->dev, base);
497 }
498exit:
499 return ret;
500}
501
502static int msm_vidc_probe_vidc_device(struct platform_device *pdev)
503{
504 int rc = 0;
505 struct msm_vidc_core *core;
506 struct device *dev;
507 int nr = BASE_DEVICE_NUMBER;
508
509 if (!vidc_driver) {
510 dprintk(VIDC_ERR, "Invalid vidc driver\n");
511 return -EINVAL;
512 }
513
514 core = kzalloc(sizeof(*core), GFP_KERNEL);
515 if (!core) {
516 dprintk(VIDC_ERR,
517 "Failed to allocate memory for device core\n");
518 return -ENOMEM;
519 }
520
521 dev_set_drvdata(&pdev->dev, core);
522 rc = msm_vidc_initialize_core(pdev, core);
523 if (rc) {
524 dprintk(VIDC_ERR, "Failed to init core\n");
525 goto err_core_init;
526 }
527 rc = sysfs_create_group(&pdev->dev.kobj, &msm_vidc_core_attr_group);
528 if (rc) {
529 dprintk(VIDC_ERR,
530 "Failed to create attributes\n");
531 goto err_core_init;
532 }
533
534 core->id = MSM_VIDC_CORE_VENUS;
535
536 rc = v4l2_device_register(&pdev->dev, &core->v4l2_dev);
537 if (rc) {
538 dprintk(VIDC_ERR, "Failed to register v4l2 device\n");
539 goto err_v4l2_register;
540 }
541
542 /* setup the decoder device */
543 core->vdev[MSM_VIDC_DECODER].vdev.release =
544 msm_vidc_release_video_device;
545 core->vdev[MSM_VIDC_DECODER].vdev.fops = &msm_v4l2_vidc_fops;
546 core->vdev[MSM_VIDC_DECODER].vdev.ioctl_ops = &msm_v4l2_ioctl_ops;
547 core->vdev[MSM_VIDC_DECODER].vdev.vfl_dir = VFL_DIR_M2M;
548 core->vdev[MSM_VIDC_DECODER].type = MSM_VIDC_DECODER;
549 core->vdev[MSM_VIDC_DECODER].vdev.v4l2_dev = &core->v4l2_dev;
550 rc = video_register_device(&core->vdev[MSM_VIDC_DECODER].vdev,
551 VFL_TYPE_GRABBER, nr);
552 if (rc) {
553 dprintk(VIDC_ERR, "Failed to register video decoder device");
554 goto err_dec_register;
555 }
556
557 video_set_drvdata(&core->vdev[MSM_VIDC_DECODER].vdev, core);
558 dev = &core->vdev[MSM_VIDC_DECODER].vdev.dev;
559 rc = device_create_file(dev, &dev_attr_link_name);
560 if (rc) {
561 dprintk(VIDC_ERR,
562 "Failed to create link name sysfs for decoder");
563 goto err_dec_attr_link_name;
564 }
565
566 /* setup the encoder device */
567 core->vdev[MSM_VIDC_ENCODER].vdev.release =
568 msm_vidc_release_video_device;
569 core->vdev[MSM_VIDC_ENCODER].vdev.fops = &msm_v4l2_vidc_fops;
570 core->vdev[MSM_VIDC_ENCODER].vdev.ioctl_ops = &msm_v4l2_ioctl_ops;
571 core->vdev[MSM_VIDC_ENCODER].vdev.vfl_dir = VFL_DIR_M2M;
572 core->vdev[MSM_VIDC_ENCODER].type = MSM_VIDC_ENCODER;
573 core->vdev[MSM_VIDC_ENCODER].vdev.v4l2_dev = &core->v4l2_dev;
574 rc = video_register_device(&core->vdev[MSM_VIDC_ENCODER].vdev,
575 VFL_TYPE_GRABBER, nr + 1);
576 if (rc) {
577 dprintk(VIDC_ERR, "Failed to register video encoder device");
578 goto err_enc_register;
579 }
580
581 video_set_drvdata(&core->vdev[MSM_VIDC_ENCODER].vdev, core);
582 dev = &core->vdev[MSM_VIDC_ENCODER].vdev.dev;
583 rc = device_create_file(dev, &dev_attr_link_name);
584 if (rc) {
585 dprintk(VIDC_ERR,
586 "Failed to create link name sysfs for encoder");
587 goto err_enc_attr_link_name;
588 }
589
590 /* finish setting up the 'core' */
591 mutex_lock(&vidc_driver->lock);
592 if (vidc_driver->num_cores + 1 > MSM_VIDC_CORES_MAX) {
593 mutex_unlock(&vidc_driver->lock);
594 dprintk(VIDC_ERR, "Maximum cores already exist, core_no = %d\n",
595 vidc_driver->num_cores);
596 goto err_cores_exceeded;
597 }
598 vidc_driver->num_cores++;
599 mutex_unlock(&vidc_driver->lock);
600
601 core->device = vidc_hfi_initialize(core->hfi_type, core->id,
602 &core->resources, &handle_cmd_response);
603 if (IS_ERR_OR_NULL(core->device)) {
604 mutex_lock(&vidc_driver->lock);
605 vidc_driver->num_cores--;
606 mutex_unlock(&vidc_driver->lock);
607
608 rc = PTR_ERR(core->device) ?: -EBADHANDLE;
609 if (rc != -EPROBE_DEFER)
610 dprintk(VIDC_ERR, "Failed to create HFI device\n");
611 else
612 dprintk(VIDC_DBG, "msm_vidc: request probe defer\n");
613 goto err_cores_exceeded;
614 }
615
616 mutex_lock(&vidc_driver->lock);
617 list_add_tail(&core->list, &vidc_driver->cores);
618 mutex_unlock(&vidc_driver->lock);
619
620 core->debugfs_root = msm_vidc_debugfs_init_core(
621 core, vidc_driver->debugfs_root);
622
623 vidc_driver->platform_version =
624 msm_vidc_read_efuse_version(pdev,
625 core->resources.pf_ver_tbl, "efuse");
626
627 vidc_driver->capability_version =
628 msm_vidc_read_efuse_version(
629 pdev, core->resources.pf_cap_tbl, "efuse2");
630
631 dprintk(VIDC_DBG, "populating sub devices\n");
632 /*
633 * Trigger probe for each sub-device i.e. qcom,msm-vidc,context-bank.
634 * When msm_vidc_probe is called for each sub-device, parse the
635 * context-bank details and store it in core->resources.context_banks
636 * list.
637 */
638 rc = of_platform_populate(pdev->dev.of_node, msm_vidc_dt_match, NULL,
639 &pdev->dev);
640 if (rc) {
641 dprintk(VIDC_ERR, "Failed to trigger probe for sub-devices\n");
642 goto err_fail_sub_device_probe;
643 }
644
645 return rc;
646
647err_fail_sub_device_probe:
648 vidc_hfi_deinitialize(core->hfi_type, core->device);
649err_cores_exceeded:
650 device_remove_file(&core->vdev[MSM_VIDC_ENCODER].vdev.dev,
651 &dev_attr_link_name);
652err_enc_attr_link_name:
653 video_unregister_device(&core->vdev[MSM_VIDC_ENCODER].vdev);
654err_enc_register:
655 device_remove_file(&core->vdev[MSM_VIDC_DECODER].vdev.dev,
656 &dev_attr_link_name);
657err_dec_attr_link_name:
658 video_unregister_device(&core->vdev[MSM_VIDC_DECODER].vdev);
659err_dec_register:
660 v4l2_device_unregister(&core->v4l2_dev);
661err_v4l2_register:
662 sysfs_remove_group(&pdev->dev.kobj, &msm_vidc_core_attr_group);
663err_core_init:
664 dev_set_drvdata(&pdev->dev, NULL);
665 kfree(core);
666 return rc;
667}
668
669static int msm_vidc_probe_context_bank(struct platform_device *pdev)
670{
671 return read_context_bank_resources_from_dt(pdev);
672}
673
674static int msm_vidc_probe_bus(struct platform_device *pdev)
675{
676 return read_bus_resources_from_dt(pdev);
677}
678
679static int msm_vidc_probe(struct platform_device *pdev)
680{
681 /*
682 * Sub devices probe will be triggered by of_platform_populate() towards
683 * the end of the probe function after msm-vidc device probe is
684 * completed. Return immediately after completing sub-device probe.
685 */
686 if (of_device_is_compatible(pdev->dev.of_node, "qcom,msm-vidc")) {
687 return msm_vidc_probe_vidc_device(pdev);
688 } else if (of_device_is_compatible(pdev->dev.of_node,
689 "qcom,msm-vidc,bus")) {
690 return msm_vidc_probe_bus(pdev);
691 } else if (of_device_is_compatible(pdev->dev.of_node,
692 "qcom,msm-vidc,context-bank")) {
693 return msm_vidc_probe_context_bank(pdev);
694 }
695 /* How did we end up here? */
696 WARN_ON(VIDC_DBG_WARN_ENABLE);
697 return -EINVAL;
698}
699
700static int msm_vidc_remove(struct platform_device *pdev)
701{
702 int rc = 0;
703 struct msm_vidc_core *core;
704
705 if (!pdev) {
706 dprintk(VIDC_ERR, "%s invalid input %pK", __func__, pdev);
707 return -EINVAL;
708 }
709
710 core = dev_get_drvdata(&pdev->dev);
711 if (!core) {
712 dprintk(VIDC_ERR, "%s invalid core", __func__);
713 return -EINVAL;
714 }
715
716 if (core->resources.use_non_secure_pil)
717 venus_boot_deinit();
718
719 vidc_hfi_deinitialize(core->hfi_type, core->device);
720 device_remove_file(&core->vdev[MSM_VIDC_ENCODER].vdev.dev,
721 &dev_attr_link_name);
722 video_unregister_device(&core->vdev[MSM_VIDC_ENCODER].vdev);
723 device_remove_file(&core->vdev[MSM_VIDC_DECODER].vdev.dev,
724 &dev_attr_link_name);
725 video_unregister_device(&core->vdev[MSM_VIDC_DECODER].vdev);
726 v4l2_device_unregister(&core->v4l2_dev);
727
728 msm_vidc_free_platform_resources(&core->resources);
729 sysfs_remove_group(&pdev->dev.kobj, &msm_vidc_core_attr_group);
730 dev_set_drvdata(&pdev->dev, NULL);
731 kfree(core);
732 return rc;
733}
734
735static int msm_vidc_pm_suspend(struct device *dev)
736{
737 int rc = 0;
738 struct msm_vidc_core *core;
739
740 /*
741 * Bail out if
742 * - driver possibly not probed yet
743 * - not the main device. We don't support power management on
744 * subdevices (e.g. context banks)
745 */
746 if (!dev || !dev->driver ||
747 !of_device_is_compatible(dev->of_node, "qcom,msm-vidc"))
748 return 0;
749
750 core = dev_get_drvdata(dev);
751 if (!core) {
752 dprintk(VIDC_ERR, "%s invalid core\n", __func__);
753 return -EINVAL;
754 }
755
756 rc = msm_vidc_suspend(core->id);
757 if (rc == -ENOTSUPP)
758 rc = 0;
759 else if (rc)
760 dprintk(VIDC_WARN, "Failed to suspend: %d\n", rc);
761
762
763 return rc;
764}
765
766static int msm_vidc_pm_resume(struct device *dev)
767{
768 dprintk(VIDC_INFO, "%s\n", __func__);
769 return 0;
770}
771
772static const struct dev_pm_ops msm_vidc_pm_ops = {
773 SET_SYSTEM_SLEEP_PM_OPS(msm_vidc_pm_suspend, msm_vidc_pm_resume)
774};
775
776MODULE_DEVICE_TABLE(of, msm_vidc_dt_match);
777
778static struct platform_driver msm_vidc_driver = {
779 .probe = msm_vidc_probe,
780 .remove = msm_vidc_remove,
781 .driver = {
782 .name = "msm_vidc_v4l2",
783 .owner = THIS_MODULE,
784 .of_match_table = msm_vidc_dt_match,
785 .pm = &msm_vidc_pm_ops,
786 },
787};
788
789static int __init msm_vidc_init(void)
790{
791 int rc = 0;
792
793 vidc_driver = kzalloc(sizeof(*vidc_driver),
794 GFP_KERNEL);
795 if (!vidc_driver) {
796 dprintk(VIDC_ERR,
797 "Failed to allocate memroy for msm_vidc_drv\n");
798 return -ENOMEM;
799 }
800
801 INIT_LIST_HEAD(&vidc_driver->cores);
802 mutex_init(&vidc_driver->lock);
803 vidc_driver->debugfs_root = msm_vidc_debugfs_init_drv();
804 if (!vidc_driver->debugfs_root)
805 dprintk(VIDC_ERR,
806 "Failed to create debugfs for msm_vidc\n");
807
808 rc = platform_driver_register(&msm_vidc_driver);
809 if (rc) {
810 dprintk(VIDC_ERR,
811 "Failed to register platform driver\n");
812 debugfs_remove_recursive(vidc_driver->debugfs_root);
813 kfree(vidc_driver);
814 vidc_driver = NULL;
815 }
816
817 return rc;
818}
819
820static void __exit msm_vidc_exit(void)
821{
822 platform_driver_unregister(&msm_vidc_driver);
823 debugfs_remove_recursive(vidc_driver->debugfs_root);
824 kfree(vidc_driver);
825 vidc_driver = NULL;
826}
827
828module_init(msm_vidc_init);
829module_exit(msm_vidc_exit);