cam_fd_dev.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2017-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include <linux/device.h>
  7. #include <linux/platform_device.h>
  8. #include <linux/slab.h>
  9. #include <linux/module.h>
  10. #include <linux/kernel.h>
  11. #include "cam_subdev.h"
  12. #include "cam_node.h"
  13. #include "cam_fd_context.h"
  14. #include "cam_fd_hw_mgr.h"
  15. #include "cam_fd_hw_mgr_intf.h"
  16. #include "camera_main.h"
  17. #define CAM_FD_DEV_NAME "cam-fd"
  18. /**
  19. * struct cam_fd_dev - FD device information
  20. *
  21. * @sd: Subdev information
  22. * @base_ctx: List of base contexts
  23. * @fd_ctx: List of FD contexts
  24. * @lock: Mutex handle
  25. * @open_cnt: FD subdev open count
  26. * @probe_done: Whether FD probe is completed
  27. */
  28. struct cam_fd_dev {
  29. struct cam_subdev sd;
  30. struct cam_context base_ctx[CAM_CTX_MAX];
  31. struct cam_fd_context fd_ctx[CAM_CTX_MAX];
  32. struct mutex lock;
  33. uint32_t open_cnt;
  34. bool probe_done;
  35. };
  36. static struct cam_fd_dev g_fd_dev;
  37. static int cam_fd_dev_open(struct v4l2_subdev *sd,
  38. struct v4l2_subdev_fh *fh)
  39. {
  40. struct cam_fd_dev *fd_dev = &g_fd_dev;
  41. cam_req_mgr_rwsem_read_op(CAM_SUBDEV_LOCK);
  42. if (!fd_dev->probe_done) {
  43. CAM_ERR(CAM_FD, "FD Dev not initialized, fd_dev=%pK", fd_dev);
  44. cam_req_mgr_rwsem_read_op(CAM_SUBDEV_UNLOCK);
  45. return -ENODEV;
  46. }
  47. mutex_lock(&fd_dev->lock);
  48. fd_dev->open_cnt++;
  49. CAM_DBG(CAM_FD, "FD Subdev open count %d", fd_dev->open_cnt);
  50. mutex_unlock(&fd_dev->lock);
  51. cam_req_mgr_rwsem_read_op(CAM_SUBDEV_UNLOCK);
  52. return 0;
  53. }
  54. static int cam_fd_dev_close_internal(struct v4l2_subdev *sd,
  55. struct v4l2_subdev_fh *fh)
  56. {
  57. struct cam_fd_dev *fd_dev = &g_fd_dev;
  58. struct cam_node *node = v4l2_get_subdevdata(sd);
  59. if (!fd_dev->probe_done) {
  60. CAM_ERR(CAM_FD, "FD Dev not initialized, fd_dev=%pK", fd_dev);
  61. return -ENODEV;
  62. }
  63. mutex_lock(&fd_dev->lock);
  64. if (fd_dev->open_cnt == 0) {
  65. CAM_WARN(CAM_FD, "device already closed");
  66. mutex_unlock(&fd_dev->lock);
  67. return 0;
  68. }
  69. fd_dev->open_cnt--;
  70. CAM_DBG(CAM_FD, "FD Subdev open count %d", fd_dev->open_cnt);
  71. mutex_unlock(&fd_dev->lock);
  72. if (!node) {
  73. CAM_ERR(CAM_FD, "Node ptr is NULL");
  74. return -EINVAL;
  75. }
  76. cam_node_shutdown(node);
  77. return 0;
  78. }
  79. static int cam_fd_dev_close(struct v4l2_subdev *sd,
  80. struct v4l2_subdev_fh *fh)
  81. {
  82. bool crm_active = cam_req_mgr_is_open();
  83. if (crm_active) {
  84. CAM_DBG(CAM_FD, "CRM is ACTIVE, close should be from CRM");
  85. return 0;
  86. }
  87. return cam_fd_dev_close_internal(sd, fh);
  88. }
  89. static const struct v4l2_subdev_internal_ops cam_fd_subdev_internal_ops = {
  90. .open = cam_fd_dev_open,
  91. .close = cam_fd_dev_close,
  92. };
  93. static int cam_fd_dev_component_bind(struct device *dev,
  94. struct device *master_dev, void *data)
  95. {
  96. int rc;
  97. int i;
  98. struct cam_hw_mgr_intf hw_mgr_intf;
  99. struct cam_node *node;
  100. struct platform_device *pdev = to_platform_device(dev);
  101. g_fd_dev.sd.internal_ops = &cam_fd_subdev_internal_ops;
  102. g_fd_dev.sd.close_seq_prior = CAM_SD_CLOSE_MEDIUM_PRIORITY;
  103. /* Initialize the v4l2 subdevice first. (create cam_node) */
  104. rc = cam_subdev_probe(&g_fd_dev.sd, pdev, CAM_FD_DEV_NAME,
  105. CAM_FD_DEVICE_TYPE);
  106. if (rc) {
  107. CAM_ERR(CAM_FD, "FD cam_subdev_probe failed, rc=%d", rc);
  108. return rc;
  109. }
  110. node = (struct cam_node *) g_fd_dev.sd.token;
  111. rc = cam_fd_hw_mgr_init(pdev->dev.of_node, &hw_mgr_intf);
  112. if (rc) {
  113. CAM_ERR(CAM_FD, "Failed in initializing FD HW manager, rc=%d",
  114. rc);
  115. goto unregister_subdev;
  116. }
  117. for (i = 0; i < CAM_CTX_MAX; i++) {
  118. rc = cam_fd_context_init(&g_fd_dev.fd_ctx[i],
  119. &g_fd_dev.base_ctx[i], &node->hw_mgr_intf, i, -1);
  120. if (rc) {
  121. CAM_ERR(CAM_FD, "FD context init failed i=%d, rc=%d",
  122. i, rc);
  123. goto deinit_ctx;
  124. }
  125. }
  126. rc = cam_node_init(node, &hw_mgr_intf, g_fd_dev.base_ctx, CAM_CTX_MAX,
  127. CAM_FD_DEV_NAME);
  128. if (rc) {
  129. CAM_ERR(CAM_FD, "FD node init failed, rc=%d", rc);
  130. goto deinit_ctx;
  131. }
  132. node->sd_handler = cam_fd_dev_close_internal;
  133. mutex_init(&g_fd_dev.lock);
  134. g_fd_dev.probe_done = true;
  135. CAM_DBG(CAM_FD, "Component bound successfully");
  136. return 0;
  137. deinit_ctx:
  138. for (--i; i >= 0; i--) {
  139. if (cam_fd_context_deinit(&g_fd_dev.fd_ctx[i]))
  140. CAM_ERR(CAM_FD, "FD context %d deinit failed", i);
  141. }
  142. unregister_subdev:
  143. if (cam_subdev_remove(&g_fd_dev.sd))
  144. CAM_ERR(CAM_FD, "Failed in subdev remove");
  145. return rc;
  146. }
  147. static void cam_fd_dev_component_unbind(struct device *dev,
  148. struct device *master_dev, void *data)
  149. {
  150. int i, rc;
  151. struct platform_device *pdev = to_platform_device(dev);
  152. for (i = 0; i < CAM_CTX_MAX; i++) {
  153. rc = cam_fd_context_deinit(&g_fd_dev.fd_ctx[i]);
  154. if (rc)
  155. CAM_ERR(CAM_FD, "FD context %d deinit failed, rc=%d",
  156. i, rc);
  157. }
  158. rc = cam_fd_hw_mgr_deinit(pdev->dev.of_node);
  159. if (rc)
  160. CAM_ERR(CAM_FD, "Failed in hw mgr deinit, rc=%d", rc);
  161. rc = cam_subdev_remove(&g_fd_dev.sd);
  162. if (rc)
  163. CAM_ERR(CAM_FD, "Unregister failed, rc=%d", rc);
  164. mutex_destroy(&g_fd_dev.lock);
  165. g_fd_dev.probe_done = false;
  166. }
  167. const static struct component_ops cam_fd_dev_component_ops = {
  168. .bind = cam_fd_dev_component_bind,
  169. .unbind = cam_fd_dev_component_unbind,
  170. };
  171. static int cam_fd_dev_probe(struct platform_device *pdev)
  172. {
  173. int rc = 0;
  174. CAM_DBG(CAM_FD, "Adding FD dev component");
  175. rc = component_add(&pdev->dev, &cam_fd_dev_component_ops);
  176. if (rc)
  177. CAM_ERR(CAM_FD, "failed to add component rc: %d", rc);
  178. return rc;
  179. }
  180. static int cam_fd_dev_remove(struct platform_device *pdev)
  181. {
  182. component_del(&pdev->dev, &cam_fd_dev_component_ops);
  183. return 0;
  184. }
  185. static const struct of_device_id cam_fd_dt_match[] = {
  186. {
  187. .compatible = "qcom,cam-fd"
  188. },
  189. {}
  190. };
  191. struct platform_driver cam_fd_driver = {
  192. .probe = cam_fd_dev_probe,
  193. .remove = cam_fd_dev_remove,
  194. .driver = {
  195. .name = "cam_fd",
  196. .owner = THIS_MODULE,
  197. .of_match_table = cam_fd_dt_match,
  198. .suppress_bind_attrs = true,
  199. },
  200. };
  201. int cam_fd_dev_init_module(void)
  202. {
  203. return platform_driver_register(&cam_fd_driver);
  204. }
  205. void cam_fd_dev_exit_module(void)
  206. {
  207. platform_driver_unregister(&cam_fd_driver);
  208. }
  209. MODULE_DESCRIPTION("MSM FD driver");
  210. MODULE_LICENSE("GPL v2");