cam_isp_dev.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2017-2021, 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_internal(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 int cam_isp_subdev_close(struct v4l2_subdev *sd,
  71. struct v4l2_subdev_fh *fh)
  72. {
  73. bool crm_active = cam_req_mgr_is_open();
  74. if (crm_active) {
  75. CAM_DBG(CAM_ISP, "CRM is ACTIVE, close should be from CRM");
  76. return 0;
  77. }
  78. return cam_isp_subdev_close_internal(sd, fh);
  79. }
  80. static const struct v4l2_subdev_internal_ops cam_isp_subdev_internal_ops = {
  81. .close = cam_isp_subdev_close,
  82. .open = cam_isp_subdev_open,
  83. };
  84. static int cam_isp_dev_component_bind(struct device *dev,
  85. struct device *master_dev, void *data)
  86. {
  87. int rc = -1;
  88. int i;
  89. struct cam_hw_mgr_intf hw_mgr_intf;
  90. struct cam_node *node;
  91. const char *compat_str = NULL;
  92. struct platform_device *pdev = to_platform_device(dev);
  93. int iommu_hdl = -1;
  94. rc = of_property_read_string_index(pdev->dev.of_node, "arch-compat", 0,
  95. (const char **)&compat_str);
  96. g_isp_dev.sd.internal_ops = &cam_isp_subdev_internal_ops;
  97. g_isp_dev.sd.close_seq_prior = CAM_SD_CLOSE_HIGH_PRIORITY;
  98. /* Initialize the v4l2 subdevice first. (create cam_node) */
  99. if (strnstr(compat_str, "ife", strlen(compat_str))) {
  100. rc = cam_subdev_probe(&g_isp_dev.sd, pdev, CAM_ISP_DEV_NAME,
  101. CAM_IFE_DEVICE_TYPE);
  102. g_isp_dev.isp_device_type = CAM_IFE_DEVICE_TYPE;
  103. g_isp_dev.max_context = CAM_IFE_CTX_MAX;
  104. } else if (strnstr(compat_str, "tfe", strlen(compat_str))) {
  105. rc = cam_subdev_probe(&g_isp_dev.sd, pdev, CAM_ISP_DEV_NAME,
  106. CAM_TFE_DEVICE_TYPE);
  107. g_isp_dev.isp_device_type = CAM_TFE_DEVICE_TYPE;
  108. g_isp_dev.max_context = CAM_TFE_CTX_MAX;
  109. } else {
  110. CAM_ERR(CAM_ISP, "Invalid ISP hw type %s", compat_str);
  111. rc = -EINVAL;
  112. goto err;
  113. }
  114. if (rc) {
  115. CAM_ERR(CAM_ISP, "ISP cam_subdev_probe failed!");
  116. goto err;
  117. }
  118. node = (struct cam_node *) g_isp_dev.sd.token;
  119. memset(&hw_mgr_intf, 0, sizeof(hw_mgr_intf));
  120. g_isp_dev.ctx = kcalloc(g_isp_dev.max_context,
  121. sizeof(struct cam_context),
  122. GFP_KERNEL);
  123. if (!g_isp_dev.ctx) {
  124. CAM_ERR(CAM_ISP,
  125. "Mem Allocation failed for ISP base context");
  126. goto unregister;
  127. }
  128. g_isp_dev.ctx_isp = kcalloc(g_isp_dev.max_context,
  129. sizeof(struct cam_isp_context),
  130. GFP_KERNEL);
  131. if (!g_isp_dev.ctx_isp) {
  132. CAM_ERR(CAM_ISP,
  133. "Mem Allocation failed for Isp private context");
  134. kfree(g_isp_dev.ctx);
  135. g_isp_dev.ctx = NULL;
  136. goto unregister;
  137. }
  138. rc = cam_isp_hw_mgr_init(compat_str, &hw_mgr_intf, &iommu_hdl);
  139. if (rc != 0) {
  140. CAM_ERR(CAM_ISP, "Can not initialized ISP HW manager!");
  141. goto kfree;
  142. }
  143. for (i = 0; i < g_isp_dev.max_context; i++) {
  144. rc = cam_isp_context_init(&g_isp_dev.ctx_isp[i],
  145. &g_isp_dev.ctx[i],
  146. &node->crm_node_intf,
  147. &node->hw_mgr_intf,
  148. i,
  149. g_isp_dev.isp_device_type, iommu_hdl);
  150. if (rc) {
  151. CAM_ERR(CAM_ISP, "ISP context init failed!");
  152. goto kfree;
  153. }
  154. }
  155. rc = cam_node_init(node, &hw_mgr_intf, g_isp_dev.ctx,
  156. g_isp_dev.max_context, CAM_ISP_DEV_NAME);
  157. if (rc) {
  158. CAM_ERR(CAM_ISP, "ISP node init failed!");
  159. goto kfree;
  160. }
  161. node->sd_handler = cam_isp_subdev_close_internal;
  162. cam_smmu_set_client_page_fault_handler(iommu_hdl,
  163. cam_isp_dev_iommu_fault_handler, node);
  164. mutex_init(&g_isp_dev.isp_mutex);
  165. CAM_DBG(CAM_ISP, "Component bound successfully");
  166. return 0;
  167. kfree:
  168. kfree(g_isp_dev.ctx);
  169. g_isp_dev.ctx = NULL;
  170. kfree(g_isp_dev.ctx_isp);
  171. g_isp_dev.ctx_isp = NULL;
  172. unregister:
  173. rc = cam_subdev_remove(&g_isp_dev.sd);
  174. err:
  175. return rc;
  176. }
  177. static void cam_isp_dev_component_unbind(struct device *dev,
  178. struct device *master_dev, void *data)
  179. {
  180. int rc = 0;
  181. int i;
  182. const char *compat_str = NULL;
  183. struct platform_device *pdev = to_platform_device(dev);
  184. rc = of_property_read_string_index(pdev->dev.of_node, "arch-compat", 0,
  185. (const char **)&compat_str);
  186. cam_isp_hw_mgr_deinit(compat_str);
  187. /* clean up resources */
  188. for (i = 0; i < g_isp_dev.max_context; i++) {
  189. rc = cam_isp_context_deinit(&g_isp_dev.ctx_isp[i]);
  190. if (rc)
  191. CAM_ERR(CAM_ISP, "ISP context %d deinit failed",
  192. i);
  193. }
  194. kfree(g_isp_dev.ctx);
  195. g_isp_dev.ctx = NULL;
  196. kfree(g_isp_dev.ctx_isp);
  197. g_isp_dev.ctx_isp = NULL;
  198. rc = cam_subdev_remove(&g_isp_dev.sd);
  199. if (rc)
  200. CAM_ERR(CAM_ISP, "Unregister failed rc: %d", rc);
  201. memset(&g_isp_dev, 0, sizeof(g_isp_dev));
  202. }
  203. const static struct component_ops cam_isp_dev_component_ops = {
  204. .bind = cam_isp_dev_component_bind,
  205. .unbind = cam_isp_dev_component_unbind,
  206. };
  207. static int cam_isp_dev_remove(struct platform_device *pdev)
  208. {
  209. component_del(&pdev->dev, &cam_isp_dev_component_ops);
  210. return 0;
  211. }
  212. static int cam_isp_dev_probe(struct platform_device *pdev)
  213. {
  214. int rc = 0;
  215. CAM_DBG(CAM_ISP, "Adding ISP dev component");
  216. rc = component_add(&pdev->dev, &cam_isp_dev_component_ops);
  217. if (rc)
  218. CAM_ERR(CAM_ISP, "failed to add component rc: %d", rc);
  219. return rc;
  220. }
  221. struct platform_driver isp_driver = {
  222. .probe = cam_isp_dev_probe,
  223. .remove = cam_isp_dev_remove,
  224. .driver = {
  225. .name = "cam_isp",
  226. .owner = THIS_MODULE,
  227. .of_match_table = cam_isp_dt_match,
  228. .suppress_bind_attrs = true,
  229. },
  230. };
  231. int cam_isp_dev_init_module(void)
  232. {
  233. return platform_driver_register(&isp_driver);
  234. }
  235. void cam_isp_dev_exit_module(void)
  236. {
  237. platform_driver_unregister(&isp_driver);
  238. }
  239. MODULE_DESCRIPTION("MSM ISP driver");
  240. MODULE_LICENSE("GPL v2");