cam_isp_dev.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2017-2019, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/delay.h>
  6. #include <linux/io.h>
  7. #include <linux/of.h>
  8. #include <linux/module.h>
  9. #include <linux/iommu.h>
  10. #include <linux/timer.h>
  11. #include <linux/kernel.h>
  12. #include <media/cam_req_mgr.h>
  13. #include "cam_isp_dev.h"
  14. #include "cam_hw_mgr_intf.h"
  15. #include "cam_isp_hw_mgr_intf.h"
  16. #include "cam_node.h"
  17. #include "cam_debug_util.h"
  18. #include "cam_smmu_api.h"
  19. static struct cam_isp_dev g_isp_dev;
  20. static void cam_isp_dev_iommu_fault_handler(
  21. struct iommu_domain *domain, struct device *dev, unsigned long iova,
  22. int flags, void *token, uint32_t buf_info)
  23. {
  24. int i = 0;
  25. struct cam_node *node = NULL;
  26. if (!token) {
  27. CAM_ERR(CAM_ISP, "invalid token in page handler cb");
  28. return;
  29. }
  30. node = (struct cam_node *)token;
  31. for (i = 0; i < node->ctx_size; i++)
  32. cam_context_dump_pf_info(&(node->ctx_list[i]), iova,
  33. buf_info);
  34. }
  35. static const struct of_device_id cam_isp_dt_match[] = {
  36. {
  37. .compatible = "qcom,cam-isp"
  38. },
  39. {}
  40. };
  41. static int cam_isp_subdev_open(struct v4l2_subdev *sd,
  42. struct v4l2_subdev_fh *fh)
  43. {
  44. mutex_lock(&g_isp_dev.isp_mutex);
  45. g_isp_dev.open_cnt++;
  46. mutex_unlock(&g_isp_dev.isp_mutex);
  47. return 0;
  48. }
  49. static int cam_isp_subdev_close(struct v4l2_subdev *sd,
  50. struct v4l2_subdev_fh *fh)
  51. {
  52. int rc = 0;
  53. struct cam_node *node = v4l2_get_subdevdata(sd);
  54. mutex_lock(&g_isp_dev.isp_mutex);
  55. if (g_isp_dev.open_cnt <= 0) {
  56. CAM_DBG(CAM_ISP, "ISP subdev is already closed");
  57. rc = -EINVAL;
  58. goto end;
  59. }
  60. g_isp_dev.open_cnt--;
  61. if (!node) {
  62. CAM_ERR(CAM_ISP, "Node ptr is NULL");
  63. rc = -EINVAL;
  64. goto end;
  65. }
  66. if (g_isp_dev.open_cnt == 0)
  67. cam_node_shutdown(node);
  68. end:
  69. mutex_unlock(&g_isp_dev.isp_mutex);
  70. return rc;
  71. }
  72. static const struct v4l2_subdev_internal_ops cam_isp_subdev_internal_ops = {
  73. .close = cam_isp_subdev_close,
  74. .open = cam_isp_subdev_open,
  75. };
  76. static int cam_isp_dev_remove(struct platform_device *pdev)
  77. {
  78. int rc = 0;
  79. int i;
  80. /* clean up resources */
  81. for (i = 0; i < CAM_CTX_MAX; i++) {
  82. rc = cam_isp_context_deinit(&g_isp_dev.ctx_isp[i]);
  83. if (rc)
  84. CAM_ERR(CAM_ISP, "ISP context %d deinit failed",
  85. i);
  86. }
  87. rc = cam_subdev_remove(&g_isp_dev.sd);
  88. if (rc)
  89. CAM_ERR(CAM_ISP, "Unregister failed");
  90. memset(&g_isp_dev, 0, sizeof(g_isp_dev));
  91. return 0;
  92. }
  93. static int cam_isp_dev_probe(struct platform_device *pdev)
  94. {
  95. int rc = -1;
  96. int i;
  97. struct cam_hw_mgr_intf hw_mgr_intf;
  98. struct cam_node *node;
  99. int iommu_hdl = -1;
  100. g_isp_dev.sd.internal_ops = &cam_isp_subdev_internal_ops;
  101. /* Initialize the v4l2 subdevice first. (create cam_node) */
  102. rc = cam_subdev_probe(&g_isp_dev.sd, pdev, CAM_ISP_DEV_NAME,
  103. CAM_IFE_DEVICE_TYPE);
  104. if (rc) {
  105. CAM_ERR(CAM_ISP, "ISP cam_subdev_probe failed!");
  106. goto err;
  107. }
  108. node = (struct cam_node *) g_isp_dev.sd.token;
  109. memset(&hw_mgr_intf, 0, sizeof(hw_mgr_intf));
  110. rc = cam_isp_hw_mgr_init(pdev->dev.of_node, &hw_mgr_intf, &iommu_hdl);
  111. if (rc != 0) {
  112. CAM_ERR(CAM_ISP, "Can not initialized ISP HW manager!");
  113. goto unregister;
  114. }
  115. for (i = 0; i < CAM_CTX_MAX; i++) {
  116. rc = cam_isp_context_init(&g_isp_dev.ctx_isp[i],
  117. &g_isp_dev.ctx[i],
  118. &node->crm_node_intf,
  119. &node->hw_mgr_intf,
  120. i);
  121. if (rc) {
  122. CAM_ERR(CAM_ISP, "ISP context init failed!");
  123. goto unregister;
  124. }
  125. }
  126. rc = cam_node_init(node, &hw_mgr_intf, g_isp_dev.ctx, CAM_CTX_MAX,
  127. CAM_ISP_DEV_NAME);
  128. if (rc) {
  129. CAM_ERR(CAM_ISP, "ISP node init failed!");
  130. goto unregister;
  131. }
  132. cam_smmu_set_client_page_fault_handler(iommu_hdl,
  133. cam_isp_dev_iommu_fault_handler, node);
  134. mutex_init(&g_isp_dev.isp_mutex);
  135. CAM_INFO(CAM_ISP, "Camera ISP probe complete");
  136. return 0;
  137. unregister:
  138. rc = cam_subdev_remove(&g_isp_dev.sd);
  139. err:
  140. return rc;
  141. }
  142. static struct platform_driver isp_driver = {
  143. .probe = cam_isp_dev_probe,
  144. .remove = cam_isp_dev_remove,
  145. .driver = {
  146. .name = "cam_isp",
  147. .owner = THIS_MODULE,
  148. .of_match_table = cam_isp_dt_match,
  149. .suppress_bind_attrs = true,
  150. },
  151. };
  152. static int __init cam_isp_dev_init_module(void)
  153. {
  154. return platform_driver_register(&isp_driver);
  155. }
  156. static void __exit cam_isp_dev_exit_module(void)
  157. {
  158. platform_driver_unregister(&isp_driver);
  159. }
  160. module_init(cam_isp_dev_init_module);
  161. module_exit(cam_isp_dev_exit_module);
  162. MODULE_DESCRIPTION("MSM ISP driver");
  163. MODULE_LICENSE("GPL v2");