cam_isp_dev.c 6.3 KB

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