cam_fd_dev.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2017-2020, 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(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. fd_dev->open_cnt--;
  61. CAM_DBG(CAM_FD, "FD Subdev open count %d", fd_dev->open_cnt);
  62. mutex_unlock(&fd_dev->lock);
  63. if (!node) {
  64. CAM_ERR(CAM_FD, "Node ptr is NULL");
  65. return -EINVAL;
  66. }
  67. cam_node_shutdown(node);
  68. return 0;
  69. }
  70. static const struct v4l2_subdev_internal_ops cam_fd_subdev_internal_ops = {
  71. .open = cam_fd_dev_open,
  72. .close = cam_fd_dev_close,
  73. };
  74. static int cam_fd_dev_component_bind(struct device *dev,
  75. struct device *master_dev, void *data)
  76. {
  77. int rc;
  78. int i;
  79. struct cam_hw_mgr_intf hw_mgr_intf;
  80. struct cam_node *node;
  81. struct platform_device *pdev = to_platform_device(dev);
  82. g_fd_dev.sd.internal_ops = &cam_fd_subdev_internal_ops;
  83. /* Initialize the v4l2 subdevice first. (create cam_node) */
  84. rc = cam_subdev_probe(&g_fd_dev.sd, pdev, CAM_FD_DEV_NAME,
  85. CAM_FD_DEVICE_TYPE);
  86. if (rc) {
  87. CAM_ERR(CAM_FD, "FD cam_subdev_probe failed, rc=%d", rc);
  88. return rc;
  89. }
  90. node = (struct cam_node *) g_fd_dev.sd.token;
  91. rc = cam_fd_hw_mgr_init(pdev->dev.of_node, &hw_mgr_intf);
  92. if (rc) {
  93. CAM_ERR(CAM_FD, "Failed in initializing FD HW manager, rc=%d",
  94. rc);
  95. goto unregister_subdev;
  96. }
  97. for (i = 0; i < CAM_CTX_MAX; i++) {
  98. rc = cam_fd_context_init(&g_fd_dev.fd_ctx[i],
  99. &g_fd_dev.base_ctx[i], &node->hw_mgr_intf, i);
  100. if (rc) {
  101. CAM_ERR(CAM_FD, "FD context init failed i=%d, rc=%d",
  102. i, rc);
  103. goto deinit_ctx;
  104. }
  105. }
  106. rc = cam_node_init(node, &hw_mgr_intf, g_fd_dev.base_ctx, CAM_CTX_MAX,
  107. CAM_FD_DEV_NAME);
  108. if (rc) {
  109. CAM_ERR(CAM_FD, "FD node init failed, rc=%d", rc);
  110. goto deinit_ctx;
  111. }
  112. mutex_init(&g_fd_dev.lock);
  113. g_fd_dev.probe_done = true;
  114. CAM_DBG(CAM_FD, "Component bound successfully");
  115. return 0;
  116. deinit_ctx:
  117. for (--i; i >= 0; i--) {
  118. if (cam_fd_context_deinit(&g_fd_dev.fd_ctx[i]))
  119. CAM_ERR(CAM_FD, "FD context %d deinit failed", i);
  120. }
  121. unregister_subdev:
  122. if (cam_subdev_remove(&g_fd_dev.sd))
  123. CAM_ERR(CAM_FD, "Failed in subdev remove");
  124. return rc;
  125. }
  126. static void cam_fd_dev_component_unbind(struct device *dev,
  127. struct device *master_dev, void *data)
  128. {
  129. int i, rc;
  130. struct platform_device *pdev = to_platform_device(dev);
  131. for (i = 0; i < CAM_CTX_MAX; i++) {
  132. rc = cam_fd_context_deinit(&g_fd_dev.fd_ctx[i]);
  133. if (rc)
  134. CAM_ERR(CAM_FD, "FD context %d deinit failed, rc=%d",
  135. i, rc);
  136. }
  137. rc = cam_fd_hw_mgr_deinit(pdev->dev.of_node);
  138. if (rc)
  139. CAM_ERR(CAM_FD, "Failed in hw mgr deinit, rc=%d", rc);
  140. rc = cam_subdev_remove(&g_fd_dev.sd);
  141. if (rc)
  142. CAM_ERR(CAM_FD, "Unregister failed, rc=%d", rc);
  143. mutex_destroy(&g_fd_dev.lock);
  144. g_fd_dev.probe_done = false;
  145. }
  146. const static struct component_ops cam_fd_dev_component_ops = {
  147. .bind = cam_fd_dev_component_bind,
  148. .unbind = cam_fd_dev_component_unbind,
  149. };
  150. static int cam_fd_dev_probe(struct platform_device *pdev)
  151. {
  152. int rc = 0;
  153. CAM_DBG(CAM_FD, "Adding FD dev component");
  154. rc = component_add(&pdev->dev, &cam_fd_dev_component_ops);
  155. if (rc)
  156. CAM_ERR(CAM_FD, "failed to add component rc: %d", rc);
  157. return rc;
  158. }
  159. static int cam_fd_dev_remove(struct platform_device *pdev)
  160. {
  161. component_del(&pdev->dev, &cam_fd_dev_component_ops);
  162. return 0;
  163. }
  164. static const struct of_device_id cam_fd_dt_match[] = {
  165. {
  166. .compatible = "qcom,cam-fd"
  167. },
  168. {}
  169. };
  170. struct platform_driver cam_fd_driver = {
  171. .probe = cam_fd_dev_probe,
  172. .remove = cam_fd_dev_remove,
  173. .driver = {
  174. .name = "cam_fd",
  175. .owner = THIS_MODULE,
  176. .of_match_table = cam_fd_dt_match,
  177. .suppress_bind_attrs = true,
  178. },
  179. };
  180. int cam_fd_dev_init_module(void)
  181. {
  182. return platform_driver_register(&cam_fd_driver);
  183. }
  184. void cam_fd_dev_exit_module(void)
  185. {
  186. platform_driver_unregister(&cam_fd_driver);
  187. }
  188. MODULE_DESCRIPTION("MSM FD driver");
  189. MODULE_LICENSE("GPL v2");