cam_custom_dev.c 5.4 KB

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