cam_isp_dev.c 7.2 KB

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