cam_isp_dev.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2017-2020, 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. const char *compat_str = NULL;
  100. uint32_t isp_device_type;
  101. int iommu_hdl = -1;
  102. rc = of_property_read_string_index(pdev->dev.of_node, "arch-compat", 0,
  103. (const char **)&compat_str);
  104. g_isp_dev.sd.internal_ops = &cam_isp_subdev_internal_ops;
  105. /* Initialize the v4l2 subdevice first. (create cam_node) */
  106. if (strnstr(compat_str, "ife", strlen(compat_str))) {
  107. rc = cam_subdev_probe(&g_isp_dev.sd, pdev, CAM_ISP_DEV_NAME,
  108. CAM_IFE_DEVICE_TYPE);
  109. isp_device_type = CAM_IFE_DEVICE_TYPE;
  110. } else if (strnstr(compat_str, "tfe", strlen(compat_str))) {
  111. rc = cam_subdev_probe(&g_isp_dev.sd, pdev, CAM_ISP_DEV_NAME,
  112. CAM_TFE_DEVICE_TYPE);
  113. isp_device_type = CAM_TFE_DEVICE_TYPE;
  114. } else {
  115. CAM_ERR(CAM_ISP, "Invalid ISP hw type %s", compat_str);
  116. rc = -EINVAL;
  117. goto err;
  118. }
  119. if (rc) {
  120. CAM_ERR(CAM_ISP, "ISP cam_subdev_probe failed!");
  121. goto err;
  122. }
  123. node = (struct cam_node *) g_isp_dev.sd.token;
  124. memset(&hw_mgr_intf, 0, sizeof(hw_mgr_intf));
  125. rc = cam_isp_hw_mgr_init(compat_str, &hw_mgr_intf, &iommu_hdl);
  126. if (rc != 0) {
  127. CAM_ERR(CAM_ISP, "Can not initialized ISP HW manager!");
  128. goto unregister;
  129. }
  130. for (i = 0; i < CAM_CTX_MAX; i++) {
  131. rc = cam_isp_context_init(&g_isp_dev.ctx_isp[i],
  132. &g_isp_dev.ctx[i],
  133. &node->crm_node_intf,
  134. &node->hw_mgr_intf,
  135. i,
  136. isp_device_type);
  137. if (rc) {
  138. CAM_ERR(CAM_ISP, "ISP context init failed!");
  139. goto unregister;
  140. }
  141. }
  142. rc = cam_node_init(node, &hw_mgr_intf, g_isp_dev.ctx, CAM_CTX_MAX,
  143. CAM_ISP_DEV_NAME);
  144. if (rc) {
  145. CAM_ERR(CAM_ISP, "ISP node init failed!");
  146. goto unregister;
  147. }
  148. cam_smmu_set_client_page_fault_handler(iommu_hdl,
  149. cam_isp_dev_iommu_fault_handler, node);
  150. mutex_init(&g_isp_dev.isp_mutex);
  151. CAM_INFO(CAM_ISP, "Camera ISP probe complete");
  152. return 0;
  153. unregister:
  154. rc = cam_subdev_remove(&g_isp_dev.sd);
  155. err:
  156. return rc;
  157. }
  158. static struct platform_driver isp_driver = {
  159. .probe = cam_isp_dev_probe,
  160. .remove = cam_isp_dev_remove,
  161. .driver = {
  162. .name = "cam_isp",
  163. .owner = THIS_MODULE,
  164. .of_match_table = cam_isp_dt_match,
  165. .suppress_bind_attrs = true,
  166. },
  167. };
  168. int cam_isp_dev_init_module(void)
  169. {
  170. return platform_driver_register(&isp_driver);
  171. }
  172. void cam_isp_dev_exit_module(void)
  173. {
  174. platform_driver_unregister(&isp_driver);
  175. }
  176. MODULE_DESCRIPTION("MSM ISP driver");
  177. MODULE_LICENSE("GPL v2");