cam_fd_dev.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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();
  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, -1);
  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. node->sd_handler = cam_fd_dev_close_internal;
  129. mutex_init(&g_fd_dev.lock);
  130. g_fd_dev.probe_done = true;
  131. CAM_DBG(CAM_FD, "Component bound successfully");
  132. return 0;
  133. deinit_ctx:
  134. for (--i; i >= 0; i--) {
  135. if (cam_fd_context_deinit(&g_fd_dev.fd_ctx[i]))
  136. CAM_ERR(CAM_FD, "FD context %d deinit failed", i);
  137. }
  138. unregister_subdev:
  139. if (cam_subdev_remove(&g_fd_dev.sd))
  140. CAM_ERR(CAM_FD, "Failed in subdev remove");
  141. return rc;
  142. }
  143. static void cam_fd_dev_component_unbind(struct device *dev,
  144. struct device *master_dev, void *data)
  145. {
  146. int i, rc;
  147. struct platform_device *pdev = to_platform_device(dev);
  148. for (i = 0; i < CAM_CTX_MAX; i++) {
  149. rc = cam_fd_context_deinit(&g_fd_dev.fd_ctx[i]);
  150. if (rc)
  151. CAM_ERR(CAM_FD, "FD context %d deinit failed, rc=%d",
  152. i, rc);
  153. }
  154. rc = cam_fd_hw_mgr_deinit(pdev->dev.of_node);
  155. if (rc)
  156. CAM_ERR(CAM_FD, "Failed in hw mgr deinit, rc=%d", rc);
  157. rc = cam_subdev_remove(&g_fd_dev.sd);
  158. if (rc)
  159. CAM_ERR(CAM_FD, "Unregister failed, rc=%d", rc);
  160. mutex_destroy(&g_fd_dev.lock);
  161. g_fd_dev.probe_done = false;
  162. }
  163. const static struct component_ops cam_fd_dev_component_ops = {
  164. .bind = cam_fd_dev_component_bind,
  165. .unbind = cam_fd_dev_component_unbind,
  166. };
  167. static int cam_fd_dev_probe(struct platform_device *pdev)
  168. {
  169. int rc = 0;
  170. CAM_DBG(CAM_FD, "Adding FD dev component");
  171. rc = component_add(&pdev->dev, &cam_fd_dev_component_ops);
  172. if (rc)
  173. CAM_ERR(CAM_FD, "failed to add component rc: %d", rc);
  174. return rc;
  175. }
  176. static int cam_fd_dev_remove(struct platform_device *pdev)
  177. {
  178. component_del(&pdev->dev, &cam_fd_dev_component_ops);
  179. return 0;
  180. }
  181. static const struct of_device_id cam_fd_dt_match[] = {
  182. {
  183. .compatible = "qcom,cam-fd"
  184. },
  185. {}
  186. };
  187. struct platform_driver cam_fd_driver = {
  188. .probe = cam_fd_dev_probe,
  189. .remove = cam_fd_dev_remove,
  190. .driver = {
  191. .name = "cam_fd",
  192. .owner = THIS_MODULE,
  193. .of_match_table = cam_fd_dt_match,
  194. .suppress_bind_attrs = true,
  195. },
  196. };
  197. int cam_fd_dev_init_module(void)
  198. {
  199. return platform_driver_register(&cam_fd_driver);
  200. }
  201. void cam_fd_dev_exit_module(void)
  202. {
  203. platform_driver_unregister(&cam_fd_driver);
  204. }
  205. MODULE_DESCRIPTION("MSM FD driver");
  206. MODULE_LICENSE("GPL v2");