cam_isp_dev.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. #include "camera_main.h"
  20. static struct cam_isp_dev g_isp_dev;
  21. static void cam_isp_dev_iommu_fault_handler(
  22. struct iommu_domain *domain, struct device *dev, unsigned long iova,
  23. int flags, void *token, uint32_t buf_info)
  24. {
  25. int i = 0;
  26. struct cam_node *node = NULL;
  27. if (!token) {
  28. CAM_ERR(CAM_ISP, "invalid token in page handler cb");
  29. return;
  30. }
  31. node = (struct cam_node *)token;
  32. for (i = 0; i < node->ctx_size; i++)
  33. cam_context_dump_pf_info(&(node->ctx_list[i]), iova,
  34. buf_info);
  35. }
  36. static const struct of_device_id cam_isp_dt_match[] = {
  37. {
  38. .compatible = "qcom,cam-isp"
  39. },
  40. {}
  41. };
  42. static int cam_isp_subdev_open(struct v4l2_subdev *sd,
  43. struct v4l2_subdev_fh *fh)
  44. {
  45. mutex_lock(&g_isp_dev.isp_mutex);
  46. g_isp_dev.open_cnt++;
  47. mutex_unlock(&g_isp_dev.isp_mutex);
  48. return 0;
  49. }
  50. static int cam_isp_subdev_close(struct v4l2_subdev *sd,
  51. struct v4l2_subdev_fh *fh)
  52. {
  53. int rc = 0;
  54. struct cam_node *node = v4l2_get_subdevdata(sd);
  55. mutex_lock(&g_isp_dev.isp_mutex);
  56. if (g_isp_dev.open_cnt <= 0) {
  57. CAM_DBG(CAM_ISP, "ISP subdev is already closed");
  58. rc = -EINVAL;
  59. goto end;
  60. }
  61. g_isp_dev.open_cnt--;
  62. if (!node) {
  63. CAM_ERR(CAM_ISP, "Node ptr is NULL");
  64. rc = -EINVAL;
  65. goto end;
  66. }
  67. if (g_isp_dev.open_cnt == 0)
  68. cam_node_shutdown(node);
  69. end:
  70. mutex_unlock(&g_isp_dev.isp_mutex);
  71. return rc;
  72. }
  73. static const struct v4l2_subdev_internal_ops cam_isp_subdev_internal_ops = {
  74. .close = cam_isp_subdev_close,
  75. .open = cam_isp_subdev_open,
  76. };
  77. static int cam_isp_dev_component_bind(struct device *dev,
  78. struct device *master_dev, void *data)
  79. {
  80. int rc = -1;
  81. int i;
  82. struct cam_hw_mgr_intf hw_mgr_intf;
  83. struct cam_node *node;
  84. const char *compat_str = NULL;
  85. uint32_t isp_device_type;
  86. struct platform_device *pdev = to_platform_device(dev);
  87. int iommu_hdl = -1;
  88. rc = of_property_read_string_index(pdev->dev.of_node, "arch-compat", 0,
  89. (const char **)&compat_str);
  90. g_isp_dev.sd.internal_ops = &cam_isp_subdev_internal_ops;
  91. /* Initialize the v4l2 subdevice first. (create cam_node) */
  92. if (strnstr(compat_str, "ife", strlen(compat_str))) {
  93. rc = cam_subdev_probe(&g_isp_dev.sd, pdev, CAM_ISP_DEV_NAME,
  94. CAM_IFE_DEVICE_TYPE);
  95. isp_device_type = CAM_IFE_DEVICE_TYPE;
  96. } else if (strnstr(compat_str, "tfe", strlen(compat_str))) {
  97. rc = cam_subdev_probe(&g_isp_dev.sd, pdev, CAM_ISP_DEV_NAME,
  98. CAM_TFE_DEVICE_TYPE);
  99. isp_device_type = CAM_TFE_DEVICE_TYPE;
  100. } else {
  101. CAM_ERR(CAM_ISP, "Invalid ISP hw type %s", compat_str);
  102. rc = -EINVAL;
  103. goto err;
  104. }
  105. if (rc) {
  106. CAM_ERR(CAM_ISP, "ISP cam_subdev_probe failed!");
  107. goto err;
  108. }
  109. node = (struct cam_node *) g_isp_dev.sd.token;
  110. memset(&hw_mgr_intf, 0, sizeof(hw_mgr_intf));
  111. rc = cam_isp_hw_mgr_init(compat_str, &hw_mgr_intf, &iommu_hdl);
  112. if (rc != 0) {
  113. CAM_ERR(CAM_ISP, "Can not initialized ISP HW manager!");
  114. goto unregister;
  115. }
  116. for (i = 0; i < CAM_CTX_MAX; i++) {
  117. rc = cam_isp_context_init(&g_isp_dev.ctx_isp[i],
  118. &g_isp_dev.ctx[i],
  119. &node->crm_node_intf,
  120. &node->hw_mgr_intf,
  121. i,
  122. isp_device_type);
  123. if (rc) {
  124. CAM_ERR(CAM_ISP, "ISP context init failed!");
  125. goto unregister;
  126. }
  127. }
  128. rc = cam_node_init(node, &hw_mgr_intf, g_isp_dev.ctx, CAM_CTX_MAX,
  129. CAM_ISP_DEV_NAME);
  130. if (rc) {
  131. CAM_ERR(CAM_ISP, "ISP node init failed!");
  132. goto unregister;
  133. }
  134. cam_smmu_set_client_page_fault_handler(iommu_hdl,
  135. cam_isp_dev_iommu_fault_handler, node);
  136. mutex_init(&g_isp_dev.isp_mutex);
  137. CAM_DBG(CAM_ISP, "Component bound successfully");
  138. return 0;
  139. unregister:
  140. rc = cam_subdev_remove(&g_isp_dev.sd);
  141. err:
  142. return rc;
  143. }
  144. static void cam_isp_dev_component_unbind(struct device *dev,
  145. struct device *master_dev, void *data)
  146. {
  147. int rc = 0;
  148. int i;
  149. const char *compat_str = NULL;
  150. struct platform_device *pdev = to_platform_device(dev);
  151. rc = of_property_read_string_index(pdev->dev.of_node, "arch-compat", 0,
  152. (const char **)&compat_str);
  153. cam_isp_hw_mgr_deinit(compat_str);
  154. /* clean up resources */
  155. for (i = 0; i < CAM_CTX_MAX; i++) {
  156. rc = cam_isp_context_deinit(&g_isp_dev.ctx_isp[i]);
  157. if (rc)
  158. CAM_ERR(CAM_ISP, "ISP context %d deinit failed",
  159. i);
  160. }
  161. rc = cam_subdev_remove(&g_isp_dev.sd);
  162. if (rc)
  163. CAM_ERR(CAM_ISP, "Unregister failed rc: %d", rc);
  164. memset(&g_isp_dev, 0, sizeof(g_isp_dev));
  165. }
  166. const static struct component_ops cam_isp_dev_component_ops = {
  167. .bind = cam_isp_dev_component_bind,
  168. .unbind = cam_isp_dev_component_unbind,
  169. };
  170. static int cam_isp_dev_remove(struct platform_device *pdev)
  171. {
  172. component_del(&pdev->dev, &cam_isp_dev_component_ops);
  173. return 0;
  174. }
  175. static int cam_isp_dev_probe(struct platform_device *pdev)
  176. {
  177. int rc = 0;
  178. CAM_DBG(CAM_ISP, "Adding ISP dev component");
  179. rc = component_add(&pdev->dev, &cam_isp_dev_component_ops);
  180. if (rc)
  181. CAM_ERR(CAM_ISP, "failed to add component rc: %d", rc);
  182. return rc;
  183. }
  184. struct platform_driver isp_driver = {
  185. .probe = cam_isp_dev_probe,
  186. .remove = cam_isp_dev_remove,
  187. .driver = {
  188. .name = "cam_isp",
  189. .owner = THIS_MODULE,
  190. .of_match_table = cam_isp_dt_match,
  191. .suppress_bind_attrs = true,
  192. },
  193. };
  194. int cam_isp_dev_init_module(void)
  195. {
  196. return platform_driver_register(&isp_driver);
  197. }
  198. void cam_isp_dev_exit_module(void)
  199. {
  200. platform_driver_unregister(&isp_driver);
  201. }
  202. MODULE_DESCRIPTION("MSM ISP driver");
  203. MODULE_LICENSE("GPL v2");