cam_lrme_dev.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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_lrme_context.h"
  13. #include "cam_lrme_hw_mgr.h"
  14. #include "cam_lrme_hw_mgr_intf.h"
  15. #include "camera_main.h"
  16. #define CAM_LRME_DEV_NAME "cam-lrme"
  17. /**
  18. * struct cam_lrme_dev
  19. *
  20. * @sd : Subdev information
  21. * @ctx : List of base contexts
  22. * @lrme_ctx : List of LRME contexts
  23. * @lock : Mutex for LRME subdev
  24. * @open_cnt : Open count of LRME subdev
  25. */
  26. struct cam_lrme_dev {
  27. struct cam_subdev sd;
  28. struct cam_context ctx[CAM_CTX_MAX];
  29. struct cam_lrme_context lrme_ctx[CAM_CTX_MAX];
  30. struct mutex lock;
  31. uint32_t open_cnt;
  32. };
  33. static struct cam_lrme_dev *g_lrme_dev;
  34. static int cam_lrme_dev_buf_done_cb(void *ctxt_to_hw_map, uint32_t evt_id,
  35. void *evt_data)
  36. {
  37. uint64_t index;
  38. struct cam_context *ctx;
  39. int rc;
  40. index = CAM_LRME_DECODE_CTX_INDEX(ctxt_to_hw_map);
  41. CAM_DBG(CAM_LRME, "ctx index %llu, evt_id %u\n", index, evt_id);
  42. ctx = &g_lrme_dev->ctx[index];
  43. rc = ctx->irq_cb_intf(ctx, evt_id, evt_data);
  44. if (rc)
  45. CAM_ERR(CAM_LRME, "irq callback failed");
  46. return rc;
  47. }
  48. static int cam_lrme_dev_open(struct v4l2_subdev *sd,
  49. struct v4l2_subdev_fh *fh)
  50. {
  51. struct cam_lrme_dev *lrme_dev = g_lrme_dev;
  52. if (!lrme_dev) {
  53. CAM_ERR(CAM_LRME,
  54. "LRME Dev not initialized, dev=%pK", lrme_dev);
  55. return -ENODEV;
  56. }
  57. mutex_lock(&lrme_dev->lock);
  58. lrme_dev->open_cnt++;
  59. mutex_unlock(&lrme_dev->lock);
  60. return 0;
  61. }
  62. static int cam_lrme_dev_close_internal(struct v4l2_subdev *sd,
  63. struct v4l2_subdev_fh *fh)
  64. {
  65. int rc = 0;
  66. struct cam_lrme_dev *lrme_dev = g_lrme_dev;
  67. struct cam_node *node = v4l2_get_subdevdata(sd);
  68. if (!lrme_dev) {
  69. CAM_ERR(CAM_LRME, "Invalid args");
  70. return -ENODEV;
  71. }
  72. mutex_lock(&lrme_dev->lock);
  73. if (lrme_dev->open_cnt <= 0) {
  74. CAM_DBG(CAM_LRME, "LRME subdev is already closed");
  75. rc = -EINVAL;
  76. goto end;
  77. }
  78. lrme_dev->open_cnt--;
  79. if (!node) {
  80. CAM_ERR(CAM_LRME, "Node is NULL");
  81. rc = -EINVAL;
  82. goto end;
  83. }
  84. if (lrme_dev->open_cnt == 0)
  85. cam_node_shutdown(node);
  86. end:
  87. mutex_unlock(&lrme_dev->lock);
  88. return rc;
  89. }
  90. static int cam_lrme_dev_close(struct v4l2_subdev *sd,
  91. struct v4l2_subdev_fh *fh)
  92. {
  93. bool crm_active = cam_req_mgr_is_open();
  94. if (crm_active) {
  95. CAM_DBG(CAM_LRME, "CRM is ACTIVE, close should be from CRM");
  96. return 0;
  97. }
  98. return cam_lrme_dev_close_internal(sd, fh);
  99. }
  100. static const struct v4l2_subdev_internal_ops cam_lrme_subdev_internal_ops = {
  101. .open = cam_lrme_dev_open,
  102. .close = cam_lrme_dev_close,
  103. };
  104. static int cam_lrme_component_bind(struct device *dev,
  105. struct device *master_dev, void *data)
  106. {
  107. int rc;
  108. int i;
  109. struct cam_hw_mgr_intf hw_mgr_intf;
  110. struct cam_node *node;
  111. struct platform_device *pdev = to_platform_device(dev);
  112. g_lrme_dev = kzalloc(sizeof(struct cam_lrme_dev), GFP_KERNEL);
  113. if (!g_lrme_dev) {
  114. CAM_ERR(CAM_LRME, "No memory");
  115. return -ENOMEM;
  116. }
  117. g_lrme_dev->sd.internal_ops = &cam_lrme_subdev_internal_ops;
  118. g_lrme_dev->sd.close_seq_prior = CAM_SD_CLOSE_MEDIUM_PRIORITY;
  119. mutex_init(&g_lrme_dev->lock);
  120. rc = cam_subdev_probe(&g_lrme_dev->sd, pdev, CAM_LRME_DEV_NAME,
  121. CAM_LRME_DEVICE_TYPE);
  122. if (rc) {
  123. CAM_ERR(CAM_LRME, "LRME cam_subdev_probe failed");
  124. goto free_mem;
  125. }
  126. node = (struct cam_node *)g_lrme_dev->sd.token;
  127. rc = cam_lrme_hw_mgr_init(&hw_mgr_intf, cam_lrme_dev_buf_done_cb);
  128. if (rc) {
  129. CAM_ERR(CAM_LRME, "Can not initialized LRME HW manager");
  130. goto unregister;
  131. }
  132. for (i = 0; i < CAM_CTX_MAX; i++) {
  133. rc = cam_lrme_context_init(&g_lrme_dev->lrme_ctx[i],
  134. &g_lrme_dev->ctx[i],
  135. &node->hw_mgr_intf, i, -1);
  136. if (rc) {
  137. CAM_ERR(CAM_LRME, "LRME context init failed");
  138. goto deinit_ctx;
  139. }
  140. }
  141. rc = cam_node_init(node, &hw_mgr_intf, g_lrme_dev->ctx, CAM_CTX_MAX,
  142. CAM_LRME_DEV_NAME);
  143. if (rc) {
  144. CAM_ERR(CAM_LRME, "LRME node init failed");
  145. goto deinit_ctx;
  146. }
  147. node->sd_handler = cam_lrme_dev_close_internal;
  148. CAM_DBG(CAM_LRME, "Component bound successfully");
  149. return 0;
  150. deinit_ctx:
  151. for (--i; i >= 0; i--) {
  152. if (cam_lrme_context_deinit(&g_lrme_dev->lrme_ctx[i]))
  153. CAM_ERR(CAM_LRME, "LRME context %d deinit failed", i);
  154. }
  155. unregister:
  156. if (cam_subdev_remove(&g_lrme_dev->sd))
  157. CAM_ERR(CAM_LRME, "Failed in subdev remove");
  158. free_mem:
  159. kfree(g_lrme_dev);
  160. return rc;
  161. }
  162. static void cam_lrme_component_unbind(struct device *dev,
  163. struct device *master_dev, void *data)
  164. {
  165. int i;
  166. int rc = 0;
  167. for (i = 0; i < CAM_CTX_MAX; i++) {
  168. rc = cam_lrme_context_deinit(&g_lrme_dev->lrme_ctx[i]);
  169. if (rc)
  170. CAM_ERR(CAM_LRME, "LRME context %d deinit failed", i);
  171. }
  172. rc = cam_lrme_hw_mgr_deinit();
  173. if (rc)
  174. CAM_ERR(CAM_LRME, "Failed in hw mgr deinit, rc=%d", rc);
  175. rc = cam_subdev_remove(&g_lrme_dev->sd);
  176. if (rc)
  177. CAM_ERR(CAM_LRME, "Unregister failed rc: %d", rc);
  178. mutex_destroy(&g_lrme_dev->lock);
  179. kfree(g_lrme_dev);
  180. g_lrme_dev = NULL;
  181. }
  182. const static struct component_ops cam_lrme_component_ops = {
  183. .bind = cam_lrme_component_bind,
  184. .unbind = cam_lrme_component_unbind,
  185. };
  186. static int cam_lrme_dev_probe(struct platform_device *pdev)
  187. {
  188. int rc = 0;
  189. CAM_DBG(CAM_LRME, "Adding LRME component");
  190. rc = component_add(&pdev->dev, &cam_lrme_component_ops);
  191. if (rc)
  192. CAM_ERR(CAM_LRME, "failed to add component rc: %d", rc);
  193. return rc;
  194. }
  195. static int cam_lrme_dev_remove(struct platform_device *pdev)
  196. {
  197. component_del(&pdev->dev, &cam_lrme_component_ops);
  198. return 0;
  199. }
  200. static const struct of_device_id cam_lrme_dt_match[] = {
  201. {
  202. .compatible = "qcom,cam-lrme"
  203. },
  204. {}
  205. };
  206. struct platform_driver cam_lrme_driver = {
  207. .probe = cam_lrme_dev_probe,
  208. .remove = cam_lrme_dev_remove,
  209. .driver = {
  210. .name = "cam_lrme",
  211. .owner = THIS_MODULE,
  212. .of_match_table = cam_lrme_dt_match,
  213. .suppress_bind_attrs = true,
  214. },
  215. };
  216. int cam_lrme_dev_init_module(void)
  217. {
  218. return platform_driver_register(&cam_lrme_driver);
  219. }
  220. void cam_lrme_dev_exit_module(void)
  221. {
  222. platform_driver_unregister(&cam_lrme_driver);
  223. }
  224. MODULE_DESCRIPTION("MSM LRME driver");
  225. MODULE_LICENSE("GPL v2");