cam_ope_subdev.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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/kernel.h>
  10. #include <linux/iommu.h>
  11. #include <linux/timer.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/videodev2.h>
  14. #include <media/v4l2-fh.h>
  15. #include <media/v4l2-device.h>
  16. #include <media/v4l2-event.h>
  17. #include <media/v4l2-ioctl.h>
  18. #include <media/v4l2-subdev.h>
  19. #include <media/cam_req_mgr.h>
  20. #include <media/cam_defs.h>
  21. #include <media/cam_ope.h>
  22. #include "cam_req_mgr_dev.h"
  23. #include "cam_subdev.h"
  24. #include "cam_node.h"
  25. #include "cam_context.h"
  26. #include "cam_ope_context.h"
  27. #include "cam_ope_hw_mgr_intf.h"
  28. #include "cam_hw_mgr_intf.h"
  29. #include "cam_debug_util.h"
  30. #include "cam_smmu_api.h"
  31. #include "camera_main.h"
  32. #define OPE_DEV_NAME "cam-ope"
  33. struct cam_ope_subdev {
  34. struct cam_subdev sd;
  35. struct cam_node *node;
  36. struct cam_context ctx[OPE_CTX_MAX];
  37. struct cam_ope_context ctx_ope[OPE_CTX_MAX];
  38. struct mutex ope_lock;
  39. int32_t open_cnt;
  40. int32_t reserved;
  41. };
  42. static struct cam_ope_subdev g_ope_dev;
  43. static void cam_ope_dev_iommu_fault_handler(
  44. struct cam_smmu_pf_info *pf_info)
  45. {
  46. int i = 0;
  47. struct cam_node *node = NULL;
  48. if (!pf_info || !pf_info->token) {
  49. CAM_ERR(CAM_ISP, "invalid token in page handler cb");
  50. return;
  51. }
  52. node = (struct cam_node *)pf_info->token;
  53. for (i = 0; i < node->ctx_size; i++)
  54. cam_context_dump_pf_info(&(node->ctx_list[i]), pf_info);
  55. }
  56. static int cam_ope_subdev_open(struct v4l2_subdev *sd,
  57. struct v4l2_subdev_fh *fh)
  58. {
  59. struct cam_hw_mgr_intf *hw_mgr_intf = NULL;
  60. struct cam_node *node = v4l2_get_subdevdata(sd);
  61. int rc = 0;
  62. mutex_lock(&g_ope_dev.ope_lock);
  63. if (g_ope_dev.open_cnt >= 1) {
  64. CAM_ERR(CAM_OPE, "OPE subdev is already opened");
  65. rc = -EALREADY;
  66. goto end;
  67. }
  68. if (!node) {
  69. CAM_ERR(CAM_OPE, "Invalid args");
  70. rc = -EINVAL;
  71. goto end;
  72. }
  73. hw_mgr_intf = &node->hw_mgr_intf;
  74. rc = hw_mgr_intf->hw_open(hw_mgr_intf->hw_mgr_priv, NULL);
  75. if (rc < 0) {
  76. CAM_ERR(CAM_OPE, "OPE HW open failed: %d", rc);
  77. goto end;
  78. }
  79. g_ope_dev.open_cnt++;
  80. CAM_DBG(CAM_OPE, "OPE HW open success: %d", rc);
  81. end:
  82. mutex_unlock(&g_ope_dev.ope_lock);
  83. return rc;
  84. }
  85. static int cam_ope_subdev_close(struct v4l2_subdev *sd,
  86. struct v4l2_subdev_fh *fh)
  87. {
  88. int rc = 0;
  89. struct cam_hw_mgr_intf *hw_mgr_intf = NULL;
  90. struct cam_node *node = v4l2_get_subdevdata(sd);
  91. mutex_lock(&g_ope_dev.ope_lock);
  92. if (g_ope_dev.open_cnt <= 0) {
  93. CAM_DBG(CAM_OPE, "OPE subdev is already closed");
  94. rc = -EINVAL;
  95. goto end;
  96. }
  97. g_ope_dev.open_cnt--;
  98. if (!node) {
  99. CAM_ERR(CAM_OPE, "Invalid args");
  100. rc = -EINVAL;
  101. goto end;
  102. }
  103. hw_mgr_intf = &node->hw_mgr_intf;
  104. if (!hw_mgr_intf) {
  105. CAM_ERR(CAM_OPE, "hw_mgr_intf is not initialized");
  106. rc = -EINVAL;
  107. goto end;
  108. }
  109. rc = cam_node_shutdown(node);
  110. if (rc < 0) {
  111. CAM_ERR(CAM_OPE, "HW close failed");
  112. goto end;
  113. }
  114. CAM_DBG(CAM_OPE, "OPE HW close success: %d", rc);
  115. end:
  116. mutex_unlock(&g_ope_dev.ope_lock);
  117. return rc;
  118. }
  119. const struct v4l2_subdev_internal_ops cam_ope_subdev_internal_ops = {
  120. .open = cam_ope_subdev_open,
  121. .close = cam_ope_subdev_close,
  122. };
  123. static int cam_ope_subdev_component_bind(struct device *dev,
  124. struct device *master_dev, void *data)
  125. {
  126. int rc = 0, i = 0;
  127. struct cam_node *node;
  128. struct cam_hw_mgr_intf *hw_mgr_intf;
  129. int iommu_hdl = -1;
  130. struct platform_device *pdev = to_platform_device(dev);
  131. CAM_DBG(CAM_OPE, "Binding OPE subdev component");
  132. if (!pdev) {
  133. CAM_ERR(CAM_OPE, "pdev is NULL");
  134. return -EINVAL;
  135. }
  136. g_ope_dev.sd.pdev = pdev;
  137. g_ope_dev.sd.internal_ops = &cam_ope_subdev_internal_ops;
  138. rc = cam_subdev_probe(&g_ope_dev.sd, pdev, OPE_DEV_NAME,
  139. CAM_OPE_DEVICE_TYPE);
  140. if (rc) {
  141. CAM_ERR(CAM_OPE, "OPE cam_subdev_probe failed:%d", rc);
  142. return rc;
  143. }
  144. node = (struct cam_node *) g_ope_dev.sd.token;
  145. hw_mgr_intf = kzalloc(sizeof(*hw_mgr_intf), GFP_KERNEL);
  146. if (!hw_mgr_intf) {
  147. rc = -EINVAL;
  148. goto hw_alloc_fail;
  149. }
  150. rc = cam_ope_hw_mgr_init(pdev->dev.of_node, (uint64_t *)hw_mgr_intf,
  151. &iommu_hdl);
  152. if (rc) {
  153. CAM_ERR(CAM_OPE, "OPE HW manager init failed: %d", rc);
  154. goto hw_init_fail;
  155. }
  156. for (i = 0; i < OPE_CTX_MAX; i++) {
  157. g_ope_dev.ctx_ope[i].base = &g_ope_dev.ctx[i];
  158. rc = cam_ope_context_init(&g_ope_dev.ctx_ope[i],
  159. hw_mgr_intf, i);
  160. if (rc) {
  161. CAM_ERR(CAM_OPE, "OPE context init failed");
  162. goto ctx_fail;
  163. }
  164. }
  165. rc = cam_node_init(node, hw_mgr_intf, g_ope_dev.ctx,
  166. OPE_CTX_MAX, OPE_DEV_NAME);
  167. if (rc) {
  168. CAM_ERR(CAM_OPE, "OPE node init failed");
  169. goto ctx_fail;
  170. }
  171. cam_smmu_set_client_page_fault_handler(iommu_hdl,
  172. cam_ope_dev_iommu_fault_handler, node);
  173. g_ope_dev.open_cnt = 0;
  174. mutex_init(&g_ope_dev.ope_lock);
  175. CAM_DBG(CAM_OPE, "Subdev component bound successfully");
  176. return rc;
  177. ctx_fail:
  178. for (--i; i >= 0; i--)
  179. cam_ope_context_deinit(&g_ope_dev.ctx_ope[i]);
  180. hw_init_fail:
  181. kfree(hw_mgr_intf);
  182. hw_alloc_fail:
  183. cam_subdev_remove(&g_ope_dev.sd);
  184. return rc;
  185. }
  186. static void cam_ope_subdev_component_unbind(struct device *dev,
  187. struct device *master_dev, void *data)
  188. {
  189. int i;
  190. struct v4l2_subdev *sd;
  191. struct cam_subdev *subdev;
  192. struct platform_device *pdev = to_platform_device(dev);
  193. if (!pdev) {
  194. CAM_ERR(CAM_OPE, "pdev is NULL");
  195. return;
  196. }
  197. sd = platform_get_drvdata(pdev);
  198. if (!sd) {
  199. CAM_ERR(CAM_OPE, "V4l2 subdev is NULL");
  200. return;
  201. }
  202. subdev = v4l2_get_subdevdata(sd);
  203. if (!subdev) {
  204. CAM_ERR(CAM_OPE, "cam subdev is NULL");
  205. return;
  206. }
  207. for (i = 0; i < OPE_CTX_MAX; i++)
  208. cam_ope_context_deinit(&g_ope_dev.ctx_ope[i]);
  209. cam_node_deinit(g_ope_dev.node);
  210. cam_subdev_remove(&g_ope_dev.sd);
  211. mutex_destroy(&g_ope_dev.ope_lock);
  212. }
  213. const static struct component_ops cam_ope_subdev_component_ops = {
  214. .bind = cam_ope_subdev_component_bind,
  215. .unbind = cam_ope_subdev_component_unbind,
  216. };
  217. static int cam_ope_subdev_probe(struct platform_device *pdev)
  218. {
  219. int rc = 0;
  220. CAM_DBG(CAM_OPE, "Adding OPE subdev component");
  221. rc = component_add(&pdev->dev, &cam_ope_subdev_component_ops);
  222. if (rc)
  223. CAM_ERR(CAM_OPE, "failed to add component rc: %d", rc);
  224. return rc;
  225. }
  226. static int cam_ope_subdev_remove(struct platform_device *pdev)
  227. {
  228. component_del(&pdev->dev, &cam_ope_subdev_component_ops);
  229. return 0;
  230. }
  231. static const struct of_device_id cam_ope_dt_match[] = {
  232. {.compatible = "qcom,cam-ope"},
  233. {}
  234. };
  235. struct platform_driver cam_ope_subdev_driver = {
  236. .probe = cam_ope_subdev_probe,
  237. .remove = cam_ope_subdev_remove,
  238. .driver = {
  239. .name = "cam_ope",
  240. .of_match_table = cam_ope_dt_match,
  241. .suppress_bind_attrs = true,
  242. },
  243. };
  244. int cam_ope_subdev_init_module(void)
  245. {
  246. return platform_driver_register(&cam_ope_subdev_driver);
  247. }
  248. void cam_ope_subdev_exit_module(void)
  249. {
  250. platform_driver_unregister(&cam_ope_subdev_driver);
  251. }
  252. MODULE_DESCRIPTION("MSM OPE driver");
  253. MODULE_LICENSE("GPL v2");