cam_cre_dev.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 i;
  90. int rc = 0;
  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. g_cre_dev.sd.pdev = pdev;
  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. hw_mgr_intf = kzalloc(sizeof(*hw_mgr_intf), GFP_KERNEL);
  105. if (!hw_mgr_intf) {
  106. CAM_ERR(CAM_CRE, "Error allocating memory");
  107. rc = -ENOMEM;
  108. goto hw_alloc_fail;
  109. }
  110. rc = cam_cre_hw_mgr_init(pdev->dev.of_node, hw_mgr_intf,
  111. &iommu_hdl);
  112. if (rc) {
  113. CAM_ERR(CAM_CRE, "Can not initialize CRE HWmanager %d", rc);
  114. goto hw_init_fail;
  115. }
  116. memset(g_cre_dev.ctx_cre, 0, sizeof(g_cre_dev.ctx_cre));
  117. for (i = 0; i < CAM_CRE_CTX_MAX; i++) {
  118. g_cre_dev.ctx_cre[i].base = &g_cre_dev.ctx[i];
  119. rc = cam_cre_context_init(&g_cre_dev.ctx_cre[i],
  120. hw_mgr_intf, i, iommu_hdl);
  121. if (rc) {
  122. CAM_ERR(CAM_CRE, "CRE context init failed %d %d",
  123. i, rc);
  124. goto ctx_init_fail;
  125. }
  126. }
  127. rc = cam_node_init(node, hw_mgr_intf, g_cre_dev.ctx,
  128. CAM_CRE_CTX_MAX, CAM_CRE_DEV_NAME);
  129. if (rc) {
  130. CAM_ERR(CAM_CRE, "CRE node init failed %d", rc);
  131. goto ctx_init_fail;
  132. }
  133. node->sd_handler = cam_cre_subdev_close_internal;
  134. cam_smmu_set_client_page_fault_handler(iommu_hdl,
  135. cam_cre_dev_iommu_fault_handler, node);
  136. g_cre_dev.open_cnt = 0;
  137. mutex_init(&g_cre_dev.cre_lock);
  138. CAM_DBG(CAM_CRE, "Component bound successfully");
  139. return rc;
  140. ctx_init_fail:
  141. for (--i; i >= 0; i--)
  142. if (cam_cre_context_deinit(&g_cre_dev.ctx_cre[i]))
  143. CAM_ERR(CAM_CRE, "deinit fail %d %d", i, rc);
  144. hw_init_fail:
  145. kfree(hw_mgr_intf);
  146. hw_alloc_fail:
  147. if (cam_subdev_remove(&g_cre_dev.sd))
  148. CAM_ERR(CAM_CRE, "remove fail %d", rc);
  149. err:
  150. return rc;
  151. }
  152. static void cam_cre_subdev_component_unbind(struct device *dev,
  153. struct device *master_dev, void *data)
  154. {
  155. int i;
  156. for (i = 0; i < CRE_CTX_MAX; i++)
  157. cam_cre_context_deinit(&g_cre_dev.ctx_cre[i]);
  158. cam_node_deinit(g_cre_dev.node);
  159. cam_subdev_remove(&g_cre_dev.sd);
  160. mutex_destroy(&g_cre_dev.cre_lock);
  161. }
  162. const static struct component_ops cam_cre_subdev_component_ops = {
  163. .bind = cam_cre_subdev_component_bind,
  164. .unbind = cam_cre_subdev_component_unbind,
  165. };
  166. static int cam_cre_subdev_remove(struct platform_device *pdev)
  167. {
  168. component_del(&pdev->dev, &cam_cre_subdev_component_ops);
  169. return 0;
  170. }
  171. static int cam_cre_subdev_probe(struct platform_device *pdev)
  172. {
  173. int rc = 0;
  174. CAM_DBG(CAM_CRE, "Adding CRE sub component");
  175. rc = component_add(&pdev->dev, &cam_cre_subdev_component_ops);
  176. if (rc)
  177. CAM_ERR(CAM_CRE, "failed to add component rc: %d", rc);
  178. return rc;
  179. }
  180. static const struct of_device_id cam_cre_subdev_dt_match[] = {
  181. {
  182. .compatible = "qcom,cam-cre",
  183. },
  184. {}
  185. };
  186. MODULE_DEVICE_TABLE(of, cam_cre_subdev_dt_match);
  187. struct platform_driver cam_cre_subdev_driver = {
  188. .probe = cam_cre_subdev_probe,
  189. .remove = cam_cre_subdev_remove,
  190. .driver = {
  191. .name = "cam_cre",
  192. .of_match_table = cam_cre_subdev_dt_match,
  193. .suppress_bind_attrs = true,
  194. },
  195. };
  196. int cam_cre_subdev_init_module(void)
  197. {
  198. return platform_driver_register(&cam_cre_subdev_driver);
  199. }
  200. void cam_cre_subdev_exit_module(void)
  201. {
  202. platform_driver_unregister(&cam_cre_subdev_driver);
  203. }
  204. MODULE_DESCRIPTION("MSM CRE driver");
  205. MODULE_LICENSE("GPL v2");