cam_cre_dev.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 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/kernel.h>
  10. #include "cam_node.h"
  11. #include "cam_hw_mgr_intf.h"
  12. #include "cam_cre_hw_mgr.h"
  13. #include "cam_cre_dev.h"
  14. #include "cam_debug_util.h"
  15. #include "cam_smmu_api.h"
  16. #include "camera_main.h"
  17. #define CAM_CRE_DEV_NAME "cam-cre"
  18. struct cam_cre_subdev {
  19. struct cam_subdev sd;
  20. struct cam_node *node;
  21. struct cam_context ctx[CRE_CTX_MAX];
  22. struct cam_cre_context ctx_cre[CRE_CTX_MAX];
  23. struct mutex cre_lock;
  24. int32_t open_cnt;
  25. int32_t reserved;
  26. };
  27. static struct cam_cre_subdev g_cre_dev;
  28. static void cam_cre_dev_iommu_fault_handler(
  29. struct cam_smmu_pf_info *pf_info)
  30. {
  31. int i = 0;
  32. struct cam_node *node = NULL;
  33. if (!pf_info || !pf_info->token) {
  34. CAM_ERR(CAM_ISP, "invalid token in page handler cb");
  35. return;
  36. }
  37. node = (struct cam_node *)pf_info->token;
  38. for (i = 0; i < node->ctx_size; i++)
  39. cam_context_dump_pf_info(&(node->ctx_list[i]), pf_info);
  40. }
  41. static int cam_cre_subdev_open(struct v4l2_subdev *sd,
  42. struct v4l2_subdev_fh *fh)
  43. {
  44. mutex_lock(&g_cre_dev.cre_lock);
  45. g_cre_dev.open_cnt++;
  46. mutex_unlock(&g_cre_dev.cre_lock);
  47. return 0;
  48. }
  49. static int cam_cre_subdev_close_internal(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_cre_dev.cre_lock);
  55. if (g_cre_dev.open_cnt <= 0) {
  56. CAM_DBG(CAM_CRE, "CRE subdev is already closed");
  57. rc = -EINVAL;
  58. goto end;
  59. }
  60. g_cre_dev.open_cnt--;
  61. if (!node) {
  62. CAM_ERR(CAM_CRE, "Node ptr is NULL");
  63. rc = -EINVAL;
  64. goto end;
  65. }
  66. if (g_cre_dev.open_cnt == 0)
  67. cam_node_shutdown(node);
  68. end:
  69. mutex_unlock(&g_cre_dev.cre_lock);
  70. return rc;
  71. }
  72. static int cam_cre_subdev_close(struct v4l2_subdev *sd,
  73. struct v4l2_subdev_fh *fh)
  74. {
  75. bool crm_active = cam_req_mgr_is_open();
  76. if (crm_active) {
  77. CAM_DBG(CAM_ICP, "CRM is ACTIVE, close should be from CRM");
  78. return 0;
  79. }
  80. return cam_cre_subdev_close_internal(sd, fh);
  81. }
  82. static const struct v4l2_subdev_internal_ops cam_cre_subdev_internal_ops = {
  83. .close = cam_cre_subdev_close,
  84. .open = cam_cre_subdev_open,
  85. };
  86. static int cam_cre_subdev_component_bind(struct device *dev,
  87. struct device *master_dev, void *data)
  88. {
  89. int rc;
  90. int i;
  91. struct cam_hw_mgr_intf hw_mgr_intf;
  92. struct cam_node *node;
  93. int iommu_hdl = -1;
  94. struct platform_device *pdev = to_platform_device(dev);
  95. CAM_DBG(CAM_CRE, "CRE Subdev Component bind");
  96. g_cre_dev.sd.internal_ops = &cam_cre_subdev_internal_ops;
  97. rc = cam_subdev_probe(&g_cre_dev.sd, pdev, CAM_CRE_DEV_NAME,
  98. CAM_CRE_DEVICE_TYPE);
  99. if (rc) {
  100. CAM_ERR(CAM_CRE, "CRE cam_subdev_probe failed %d", rc);
  101. goto err;
  102. }
  103. node = (struct cam_node *)g_cre_dev.sd.token;
  104. rc = cam_cre_hw_mgr_init(pdev->dev.of_node,
  105. (uint64_t *)&hw_mgr_intf, &iommu_hdl);
  106. if (rc) {
  107. CAM_ERR(CAM_CRE, "Can not initialize CRE HWmanager %d", rc);
  108. goto unregister;
  109. }
  110. for (i = 0; i < CAM_CRE_CTX_MAX; i++) {
  111. rc = cam_cre_context_init(&g_cre_dev.ctx_cre[i],
  112. &g_cre_dev.ctx[i],
  113. &node->hw_mgr_intf,
  114. i);
  115. if (rc) {
  116. CAM_ERR(CAM_CRE, "CRE context init failed %d %d",
  117. i, rc);
  118. goto ctx_init_fail;
  119. }
  120. }
  121. rc = cam_node_init(node, &hw_mgr_intf, g_cre_dev.ctx, CAM_CRE_CTX_MAX,
  122. CAM_CRE_DEV_NAME);
  123. if (rc) {
  124. CAM_ERR(CAM_CRE, "CRE node init failed %d", rc);
  125. goto ctx_init_fail;
  126. }
  127. node->sd_handler = cam_cre_subdev_close_internal;
  128. cam_smmu_set_client_page_fault_handler(iommu_hdl,
  129. cam_cre_dev_iommu_fault_handler, node);
  130. mutex_init(&g_cre_dev.cre_lock);
  131. CAM_DBG(CAM_CRE, "Component bound successfully");
  132. return rc;
  133. ctx_init_fail:
  134. for (--i; i >= 0; i--)
  135. if (cam_cre_context_deinit(&g_cre_dev.ctx_cre[i]))
  136. CAM_ERR(CAM_CRE, "deinit fail %d %d", i, rc);
  137. unregister:
  138. if (cam_subdev_remove(&g_cre_dev.sd))
  139. CAM_ERR(CAM_CRE, "remove fail %d", rc);
  140. err:
  141. return rc;
  142. }
  143. static void cam_cre_subdev_component_unbind(struct device *dev,
  144. struct device *master_dev, void *data)
  145. {
  146. int rc;
  147. int i;
  148. for (i = 0; i < CAM_CTX_MAX; i++) {
  149. rc = cam_cre_context_deinit(&g_cre_dev.ctx_cre[i]);
  150. if (rc)
  151. CAM_ERR(CAM_CRE, "CRE context %d deinit failed %d",
  152. i, rc);
  153. }
  154. rc = cam_subdev_remove(&g_cre_dev.sd);
  155. if (rc)
  156. CAM_ERR(CAM_CRE, "Unregister failed %d", rc);
  157. }
  158. const static struct component_ops cam_cre_subdev_component_ops = {
  159. .bind = cam_cre_subdev_component_bind,
  160. .unbind = cam_cre_subdev_component_unbind,
  161. };
  162. static int cam_cre_subdev_remove(struct platform_device *pdev)
  163. {
  164. component_del(&pdev->dev, &cam_cre_subdev_component_ops);
  165. return 0;
  166. }
  167. static int cam_cre_subdev_probe(struct platform_device *pdev)
  168. {
  169. int rc = 0;
  170. CAM_DBG(CAM_CRE, "Adding CRE sub component");
  171. rc = component_add(&pdev->dev, &cam_cre_subdev_component_ops);
  172. if (rc)
  173. CAM_ERR(CAM_CRE, "failed to add component rc: %d", rc);
  174. return rc;
  175. }
  176. static const struct of_device_id cam_cre_subdev_dt_match[] = {
  177. {
  178. .compatible = "qcom,cam-cre",
  179. },
  180. {}
  181. };
  182. MODULE_DEVICE_TABLE(of, cam_cre_subdev_dt_match);
  183. struct platform_driver cam_cre_subdev_driver = {
  184. .probe = cam_cre_subdev_probe,
  185. .remove = cam_cre_subdev_remove,
  186. .driver = {
  187. .name = "cam_cre",
  188. .of_match_table = cam_cre_subdev_dt_match,
  189. .suppress_bind_attrs = true,
  190. },
  191. };
  192. int cam_cre_subdev_init_module(void)
  193. {
  194. return platform_driver_register(&cam_cre_subdev_driver);
  195. }
  196. void cam_cre_subdev_exit_module(void)
  197. {
  198. platform_driver_unregister(&cam_cre_subdev_driver);
  199. }
  200. MODULE_DESCRIPTION("MSM CRE driver");
  201. MODULE_LICENSE("GPL v2");