cam_cre_dev.c 5.0 KB

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