cam_cre_dev.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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(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 const struct v4l2_subdev_internal_ops cam_cre_subdev_internal_ops = {
  73. .close = cam_cre_subdev_close,
  74. .open = cam_cre_subdev_open,
  75. };
  76. static int cam_cre_subdev_component_bind(struct device *dev,
  77. struct device *master_dev, void *data)
  78. {
  79. int rc;
  80. int i;
  81. struct cam_hw_mgr_intf hw_mgr_intf;
  82. struct cam_node *node;
  83. int iommu_hdl = -1;
  84. struct platform_device *pdev = to_platform_device(dev);
  85. CAM_DBG(CAM_CRE, "CRE Subdev Component bind");
  86. g_cre_dev.sd.internal_ops = &cam_cre_subdev_internal_ops;
  87. rc = cam_subdev_probe(&g_cre_dev.sd, pdev, CAM_CRE_DEV_NAME,
  88. CAM_CRE_DEVICE_TYPE);
  89. if (rc) {
  90. CAM_ERR(CAM_CRE, "CRE cam_subdev_probe failed %d", rc);
  91. goto err;
  92. }
  93. node = (struct cam_node *)g_cre_dev.sd.token;
  94. rc = cam_cre_hw_mgr_init(pdev->dev.of_node,
  95. (uint64_t *)&hw_mgr_intf, &iommu_hdl);
  96. if (rc) {
  97. CAM_ERR(CAM_CRE, "Can not initialize CRE HWmanager %d", rc);
  98. goto unregister;
  99. }
  100. for (i = 0; i < CAM_CRE_CTX_MAX; i++) {
  101. rc = cam_cre_context_init(&g_cre_dev.ctx_cre[i],
  102. &g_cre_dev.ctx[i],
  103. &node->hw_mgr_intf,
  104. i);
  105. if (rc) {
  106. CAM_ERR(CAM_CRE, "CRE context init failed %d %d",
  107. i, rc);
  108. goto ctx_init_fail;
  109. }
  110. }
  111. rc = cam_node_init(node, &hw_mgr_intf, g_cre_dev.ctx, CAM_CRE_CTX_MAX,
  112. CAM_CRE_DEV_NAME);
  113. if (rc) {
  114. CAM_ERR(CAM_CRE, "CRE node init failed %d", rc);
  115. goto ctx_init_fail;
  116. }
  117. cam_smmu_set_client_page_fault_handler(iommu_hdl,
  118. cam_cre_dev_iommu_fault_handler, node);
  119. mutex_init(&g_cre_dev.cre_lock);
  120. CAM_DBG(CAM_CRE, "Component bound successfully");
  121. return rc;
  122. ctx_init_fail:
  123. for (--i; i >= 0; i--)
  124. if (cam_cre_context_deinit(&g_cre_dev.ctx_cre[i]))
  125. CAM_ERR(CAM_CRE, "deinit fail %d %d", i, rc);
  126. unregister:
  127. if (cam_subdev_remove(&g_cre_dev.sd))
  128. CAM_ERR(CAM_CRE, "remove fail %d", rc);
  129. err:
  130. return rc;
  131. }
  132. static void cam_cre_subdev_component_unbind(struct device *dev,
  133. struct device *master_dev, void *data)
  134. {
  135. int rc;
  136. int i;
  137. for (i = 0; i < CAM_CTX_MAX; i++) {
  138. rc = cam_cre_context_deinit(&g_cre_dev.ctx_cre[i]);
  139. if (rc)
  140. CAM_ERR(CAM_CRE, "CRE context %d deinit failed %d",
  141. i, rc);
  142. }
  143. rc = cam_subdev_remove(&g_cre_dev.sd);
  144. if (rc)
  145. CAM_ERR(CAM_CRE, "Unregister failed %d", rc);
  146. }
  147. const static struct component_ops cam_cre_subdev_component_ops = {
  148. .bind = cam_cre_subdev_component_bind,
  149. .unbind = cam_cre_subdev_component_unbind,
  150. };
  151. static int cam_cre_subdev_remove(struct platform_device *pdev)
  152. {
  153. component_del(&pdev->dev, &cam_cre_subdev_component_ops);
  154. return 0;
  155. }
  156. static int cam_cre_subdev_probe(struct platform_device *pdev)
  157. {
  158. int rc = 0;
  159. CAM_DBG(CAM_CRE, "Adding CRE sub component");
  160. rc = component_add(&pdev->dev, &cam_cre_subdev_component_ops);
  161. if (rc)
  162. CAM_ERR(CAM_CRE, "failed to add component rc: %d", rc);
  163. return rc;
  164. }
  165. static const struct of_device_id cam_cre_subdev_dt_match[] = {
  166. {
  167. .compatible = "qcom,cam-cre",
  168. },
  169. {}
  170. };
  171. MODULE_DEVICE_TABLE(of, cam_cre_subdev_dt_match);
  172. struct platform_driver cam_cre_subdev_driver = {
  173. .probe = cam_cre_subdev_probe,
  174. .remove = cam_cre_subdev_remove,
  175. .driver = {
  176. .name = "cam_cre",
  177. .of_match_table = cam_cre_subdev_dt_match,
  178. .suppress_bind_attrs = true,
  179. },
  180. };
  181. int cam_cre_subdev_init_module(void)
  182. {
  183. return platform_driver_register(&cam_cre_subdev_driver);
  184. }
  185. void cam_cre_subdev_exit_module(void)
  186. {
  187. platform_driver_unregister(&cam_cre_subdev_driver);
  188. }
  189. MODULE_DESCRIPTION("MSM CRE driver");
  190. MODULE_LICENSE("GPL v2");