cam_fd_dev.c 5.7 KB

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