cam_custom_dev.c 5.1 KB

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