cam_custom_dev.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 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. static int cam_custom_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_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 const struct v4l2_subdev_internal_ops cam_custom_subdev_internal_ops = {
  73. .close = cam_custom_subdev_close,
  74. .open = cam_custom_subdev_open,
  75. };
  76. static int cam_custom_component_bind(struct device *dev,
  77. struct device *master_dev, void *data)
  78. {
  79. int rc = -EINVAL;
  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_custom_dev.sd.internal_ops = &cam_custom_subdev_internal_ops;
  86. rc = cam_subdev_probe(&g_custom_dev.sd, pdev, CAM_CUSTOM_DEV_NAME,
  87. CAM_CUSTOM_DEVICE_TYPE);
  88. if (rc) {
  89. CAM_ERR(CAM_CUSTOM, "Custom device cam_subdev_probe failed!");
  90. goto err;
  91. }
  92. node = (struct cam_node *) g_custom_dev.sd.token;
  93. memset(&hw_mgr_intf, 0, sizeof(hw_mgr_intf));
  94. rc = cam_custom_hw_mgr_init(pdev->dev.of_node,
  95. &hw_mgr_intf, &iommu_hdl);
  96. if (rc != 0) {
  97. CAM_ERR(CAM_CUSTOM, "Can not initialized Custom HW manager!");
  98. goto unregister;
  99. }
  100. for (i = 0; i < CAM_CUSTOM_HW_MAX_INSTANCES; i++) {
  101. rc = cam_custom_dev_context_init(&g_custom_dev.ctx_custom[i],
  102. &g_custom_dev.ctx[i],
  103. &node->crm_node_intf,
  104. &node->hw_mgr_intf,
  105. i);
  106. if (rc) {
  107. CAM_ERR(CAM_CUSTOM, "Custom context init failed!");
  108. goto unregister;
  109. }
  110. }
  111. rc = cam_node_init(node, &hw_mgr_intf, g_custom_dev.ctx,
  112. CAM_CUSTOM_HW_MAX_INSTANCES, CAM_CUSTOM_DEV_NAME);
  113. if (rc) {
  114. CAM_ERR(CAM_CUSTOM, "Custom HW node init failed!");
  115. goto unregister;
  116. }
  117. cam_smmu_set_client_page_fault_handler(iommu_hdl,
  118. cam_custom_dev_iommu_fault_handler, node);
  119. mutex_init(&g_custom_dev.custom_dev_mutex);
  120. CAM_DBG(CAM_CUSTOM, "%s component bound successfully", pdev->name);
  121. return 0;
  122. unregister:
  123. rc = cam_subdev_remove(&g_custom_dev.sd);
  124. err:
  125. return rc;
  126. }
  127. static void cam_custom_component_unbind(struct device *dev,
  128. struct device *master_dev, void *data)
  129. {
  130. int rc = 0;
  131. int i;
  132. /* clean up resources */
  133. for (i = 0; i < CAM_CUSTOM_HW_MAX_INSTANCES; i++) {
  134. rc = cam_custom_dev_context_deinit(&g_custom_dev.ctx_custom[i]);
  135. if (rc)
  136. CAM_ERR(CAM_CUSTOM,
  137. "Custom context %d deinit failed", i);
  138. }
  139. rc = cam_subdev_remove(&g_custom_dev.sd);
  140. if (rc)
  141. CAM_ERR(CAM_CUSTOM, "Unregister failed");
  142. memset(&g_custom_dev, 0, sizeof(g_custom_dev));
  143. }
  144. const static struct component_ops cam_custom_component_ops = {
  145. .bind = cam_custom_component_bind,
  146. .unbind = cam_custom_component_unbind,
  147. };
  148. static int cam_custom_dev_remove(struct platform_device *pdev)
  149. {
  150. component_del(&pdev->dev, &cam_custom_component_ops);
  151. return 0;
  152. }
  153. static int cam_custom_dev_probe(struct platform_device *pdev)
  154. {
  155. int rc = 0;
  156. CAM_DBG(CAM_CUSTOM, "Adding Custom HW component");
  157. rc = component_add(&pdev->dev, &cam_custom_component_ops);
  158. if (rc)
  159. CAM_ERR(CAM_CUSTOM, "failed to add component rc: %d", rc);
  160. return rc;
  161. }
  162. struct platform_driver custom_driver = {
  163. .probe = cam_custom_dev_probe,
  164. .remove = cam_custom_dev_remove,
  165. .driver = {
  166. .name = "cam_custom",
  167. .of_match_table = cam_custom_dt_match,
  168. .suppress_bind_attrs = true,
  169. },
  170. };
  171. int cam_custom_dev_init_module(void)
  172. {
  173. return platform_driver_register(&custom_driver);
  174. }
  175. void cam_custom_dev_exit_module(void)
  176. {
  177. platform_driver_unregister(&custom_driver);
  178. }
  179. MODULE_DESCRIPTION("MSM CUSTOM driver");
  180. MODULE_LICENSE("GPL v2");