cam_custom_dev.c 4.4 KB

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