cam_ope_subdev.c 6.8 KB

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