cam_custom_dev.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2019-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2023 Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include <linux/delay.h>
  7. #include <linux/io.h>
  8. #include <linux/of.h>
  9. #include <linux/module.h>
  10. #include <linux/iommu.h>
  11. #include <linux/timer.h>
  12. #include <linux/kernel.h>
  13. #include <dt-bindings/msm-camera.h>
  14. #include <media/cam_req_mgr.h>
  15. #include "cam_custom_dev.h"
  16. #include "cam_hw_mgr_intf.h"
  17. #include "cam_custom_hw_mgr_intf.h"
  18. #include "cam_node.h"
  19. #include "cam_debug_util.h"
  20. #include "cam_smmu_api.h"
  21. #include "cam_compat.h"
  22. #include "camera_main.h"
  23. static struct cam_custom_dev g_custom_dev;
  24. static uint32_t g_num_custom_hws;
  25. static void cam_custom_dev_iommu_fault_handler(
  26. struct cam_smmu_pf_info *pf_info)
  27. {
  28. int i = 0;
  29. struct cam_node *node = NULL;
  30. if (!pf_info || !pf_info->token) {
  31. CAM_ERR(CAM_CUSTOM, "invalid token in page handler cb");
  32. return;
  33. }
  34. node = (struct cam_node *)pf_info->token;
  35. for (i = 0; i < node->ctx_size; i++)
  36. cam_context_dump_pf_info(&(node->ctx_list[i]), pf_info);
  37. }
  38. static const struct of_device_id cam_custom_dt_match[] = {
  39. {
  40. .compatible = "qcom,cam-custom"
  41. },
  42. {}
  43. };
  44. static int cam_custom_subdev_open(struct v4l2_subdev *sd,
  45. struct v4l2_subdev_fh *fh)
  46. {
  47. mutex_lock(&g_custom_dev.custom_dev_mutex);
  48. g_custom_dev.open_cnt++;
  49. mutex_unlock(&g_custom_dev.custom_dev_mutex);
  50. return 0;
  51. }
  52. static int cam_custom_subdev_close_internal(struct v4l2_subdev *sd,
  53. struct v4l2_subdev_fh *fh)
  54. {
  55. int rc = 0;
  56. struct cam_node *node = v4l2_get_subdevdata(sd);
  57. mutex_lock(&g_custom_dev.custom_dev_mutex);
  58. if (g_custom_dev.open_cnt <= 0) {
  59. CAM_DBG(CAM_CUSTOM, "Custom subdev is already closed");
  60. rc = -EINVAL;
  61. goto end;
  62. }
  63. g_custom_dev.open_cnt--;
  64. if (!node) {
  65. CAM_ERR(CAM_CUSTOM, "Node ptr is NULL");
  66. rc = -EINVAL;
  67. goto end;
  68. }
  69. if (g_custom_dev.open_cnt == 0)
  70. cam_node_shutdown(node);
  71. end:
  72. mutex_unlock(&g_custom_dev.custom_dev_mutex);
  73. return rc;
  74. }
  75. static int cam_custom_subdev_close(struct v4l2_subdev *sd,
  76. struct v4l2_subdev_fh *fh)
  77. {
  78. bool crm_active = cam_req_mgr_is_open();
  79. if (crm_active) {
  80. CAM_DBG(CAM_CUSTOM, "CRM is ACTIVE, close should be from CRM");
  81. return 0;
  82. }
  83. return cam_custom_subdev_close_internal(sd, fh);
  84. }
  85. static const struct v4l2_subdev_internal_ops cam_custom_subdev_internal_ops = {
  86. .close = cam_custom_subdev_close,
  87. .open = cam_custom_subdev_open,
  88. };
  89. static int cam_custom_component_bind(struct device *dev,
  90. struct device *master_dev, void *data)
  91. {
  92. int rc = -EINVAL;
  93. int i;
  94. struct cam_hw_mgr_intf hw_mgr_intf;
  95. struct cam_node *node;
  96. int iommu_hdl = -1;
  97. struct platform_device *pdev = to_platform_device(dev);
  98. g_custom_dev.sd.internal_ops = &cam_custom_subdev_internal_ops;
  99. g_custom_dev.sd.close_seq_prior = CAM_SD_CLOSE_HIGH_PRIORITY;
  100. if (!cam_cpas_is_feature_supported(CAM_CPAS_CUSTOM_FUSE, BIT(0), NULL)) {
  101. CAM_DBG(CAM_CUSTOM, "CUSTOM:0 is not supported");
  102. goto err;
  103. }
  104. rc = cam_subdev_probe(&g_custom_dev.sd, pdev, CAM_CUSTOM_DEV_NAME,
  105. CAM_CUSTOM_DEVICE_TYPE);
  106. if (rc) {
  107. CAM_ERR(CAM_CUSTOM, "Custom device cam_subdev_probe failed!");
  108. goto err;
  109. }
  110. node = (struct cam_node *) g_custom_dev.sd.token;
  111. memset(&hw_mgr_intf, 0, sizeof(hw_mgr_intf));
  112. rc = cam_custom_hw_mgr_init(pdev->dev.of_node,
  113. &hw_mgr_intf, &iommu_hdl);
  114. if (rc != 0) {
  115. CAM_ERR(CAM_CUSTOM, "Can not initialized Custom HW manager!");
  116. goto unregister;
  117. }
  118. for (i = 0; i < CAM_CUSTOM_HW_MAX_INSTANCES; i++) {
  119. rc = cam_custom_dev_context_init(&g_custom_dev.ctx_custom[i],
  120. &g_custom_dev.ctx[i],
  121. &node->crm_node_intf,
  122. &node->hw_mgr_intf,
  123. i, iommu_hdl);
  124. if (rc) {
  125. CAM_ERR(CAM_CUSTOM, "Custom context init failed!");
  126. goto unregister;
  127. }
  128. }
  129. rc = cam_node_init(node, &hw_mgr_intf, g_custom_dev.ctx,
  130. CAM_CUSTOM_HW_MAX_INSTANCES, CAM_CUSTOM_DEV_NAME);
  131. if (rc) {
  132. CAM_ERR(CAM_CUSTOM, "Custom HW node init failed!");
  133. goto unregister;
  134. }
  135. node->sd_handler = cam_custom_subdev_close_internal;
  136. cam_smmu_set_client_page_fault_handler(iommu_hdl,
  137. cam_custom_dev_iommu_fault_handler, node);
  138. mutex_init(&g_custom_dev.custom_dev_mutex);
  139. CAM_DBG(CAM_CUSTOM, "%s component bound successfully", pdev->name);
  140. return 0;
  141. unregister:
  142. rc = cam_subdev_remove(&g_custom_dev.sd);
  143. err:
  144. return rc;
  145. }
  146. void cam_custom_get_num_hws(uint32_t *custom_num)
  147. {
  148. if (custom_num)
  149. *custom_num = g_num_custom_hws;
  150. else
  151. CAM_ERR(CAM_CUSTOM, "Invalid argument, g_num_custom_hws: %u", g_num_custom_hws);
  152. }
  153. static void cam_custom_component_unbind(struct device *dev,
  154. struct device *master_dev, void *data)
  155. {
  156. int rc = 0;
  157. int i;
  158. /* clean up resources */
  159. for (i = 0; i < CAM_CUSTOM_HW_MAX_INSTANCES; i++) {
  160. rc = cam_custom_dev_context_deinit(&g_custom_dev.ctx_custom[i]);
  161. if (rc)
  162. CAM_ERR(CAM_CUSTOM,
  163. "Custom context %d deinit failed", i);
  164. }
  165. rc = cam_subdev_remove(&g_custom_dev.sd);
  166. if (rc)
  167. CAM_ERR(CAM_CUSTOM, "Unregister failed");
  168. memset(&g_custom_dev, 0, sizeof(g_custom_dev));
  169. }
  170. const static struct component_ops cam_custom_component_ops = {
  171. .bind = cam_custom_component_bind,
  172. .unbind = cam_custom_component_unbind,
  173. };
  174. static int cam_custom_dev_remove(struct platform_device *pdev)
  175. {
  176. component_del(&pdev->dev, &cam_custom_component_ops);
  177. return 0;
  178. }
  179. static int cam_custom_dev_probe(struct platform_device *pdev)
  180. {
  181. int rc = 0;
  182. CAM_DBG(CAM_CUSTOM, "Adding Custom HW component");
  183. g_num_custom_hws++;
  184. rc = component_add(&pdev->dev, &cam_custom_component_ops);
  185. if (rc)
  186. CAM_ERR(CAM_CUSTOM, "failed to add component rc: %d", rc);
  187. return rc;
  188. }
  189. struct platform_driver custom_driver = {
  190. .probe = cam_custom_dev_probe,
  191. .remove = cam_custom_dev_remove,
  192. .driver = {
  193. .name = "cam_custom",
  194. .of_match_table = cam_custom_dt_match,
  195. .suppress_bind_attrs = true,
  196. },
  197. };
  198. int cam_custom_dev_init_module(void)
  199. {
  200. return platform_driver_register(&custom_driver);
  201. }
  202. void cam_custom_dev_exit_module(void)
  203. {
  204. platform_driver_unregister(&custom_driver);
  205. }
  206. MODULE_DESCRIPTION("MSM CUSTOM driver");
  207. MODULE_LICENSE("GPL v2");