cam_lrme_dev.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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_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(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 const struct v4l2_subdev_internal_ops cam_lrme_subdev_internal_ops = {
  91. .open = cam_lrme_dev_open,
  92. .close = cam_lrme_dev_close,
  93. };
  94. static int cam_lrme_component_bind(struct device *dev,
  95. struct device *master_dev, void *data)
  96. {
  97. int rc;
  98. int i;
  99. struct cam_hw_mgr_intf hw_mgr_intf;
  100. struct cam_node *node;
  101. struct platform_device *pdev = to_platform_device(dev);
  102. g_lrme_dev = kzalloc(sizeof(struct cam_lrme_dev), GFP_KERNEL);
  103. if (!g_lrme_dev) {
  104. CAM_ERR(CAM_LRME, "No memory");
  105. return -ENOMEM;
  106. }
  107. g_lrme_dev->sd.internal_ops = &cam_lrme_subdev_internal_ops;
  108. mutex_init(&g_lrme_dev->lock);
  109. rc = cam_subdev_probe(&g_lrme_dev->sd, pdev, CAM_LRME_DEV_NAME,
  110. CAM_LRME_DEVICE_TYPE);
  111. if (rc) {
  112. CAM_ERR(CAM_LRME, "LRME cam_subdev_probe failed");
  113. goto free_mem;
  114. }
  115. node = (struct cam_node *)g_lrme_dev->sd.token;
  116. rc = cam_lrme_hw_mgr_init(&hw_mgr_intf, cam_lrme_dev_buf_done_cb);
  117. if (rc) {
  118. CAM_ERR(CAM_LRME, "Can not initialized LRME HW manager");
  119. goto unregister;
  120. }
  121. for (i = 0; i < CAM_CTX_MAX; i++) {
  122. rc = cam_lrme_context_init(&g_lrme_dev->lrme_ctx[i],
  123. &g_lrme_dev->ctx[i],
  124. &node->hw_mgr_intf, i);
  125. if (rc) {
  126. CAM_ERR(CAM_LRME, "LRME context init failed");
  127. goto deinit_ctx;
  128. }
  129. }
  130. rc = cam_node_init(node, &hw_mgr_intf, g_lrme_dev->ctx, CAM_CTX_MAX,
  131. CAM_LRME_DEV_NAME);
  132. if (rc) {
  133. CAM_ERR(CAM_LRME, "LRME node init failed");
  134. goto deinit_ctx;
  135. }
  136. CAM_DBG(CAM_LRME, "Component bound successfully");
  137. return 0;
  138. deinit_ctx:
  139. for (--i; i >= 0; i--) {
  140. if (cam_lrme_context_deinit(&g_lrme_dev->lrme_ctx[i]))
  141. CAM_ERR(CAM_LRME, "LRME context %d deinit failed", i);
  142. }
  143. unregister:
  144. if (cam_subdev_remove(&g_lrme_dev->sd))
  145. CAM_ERR(CAM_LRME, "Failed in subdev remove");
  146. free_mem:
  147. kfree(g_lrme_dev);
  148. return rc;
  149. }
  150. static void cam_lrme_component_unbind(struct device *dev,
  151. struct device *master_dev, void *data)
  152. {
  153. int i;
  154. int rc = 0;
  155. for (i = 0; i < CAM_CTX_MAX; i++) {
  156. rc = cam_lrme_context_deinit(&g_lrme_dev->lrme_ctx[i]);
  157. if (rc)
  158. CAM_ERR(CAM_LRME, "LRME context %d deinit failed", i);
  159. }
  160. rc = cam_lrme_hw_mgr_deinit();
  161. if (rc)
  162. CAM_ERR(CAM_LRME, "Failed in hw mgr deinit, rc=%d", rc);
  163. rc = cam_subdev_remove(&g_lrme_dev->sd);
  164. if (rc)
  165. CAM_ERR(CAM_LRME, "Unregister failed rc: %d", rc);
  166. mutex_destroy(&g_lrme_dev->lock);
  167. kfree(g_lrme_dev);
  168. g_lrme_dev = NULL;
  169. }
  170. const static struct component_ops cam_lrme_component_ops = {
  171. .bind = cam_lrme_component_bind,
  172. .unbind = cam_lrme_component_unbind,
  173. };
  174. static int cam_lrme_dev_probe(struct platform_device *pdev)
  175. {
  176. int rc = 0;
  177. CAM_DBG(CAM_LRME, "Adding LRME component");
  178. rc = component_add(&pdev->dev, &cam_lrme_component_ops);
  179. if (rc)
  180. CAM_ERR(CAM_LRME, "failed to add component rc: %d", rc);
  181. return rc;
  182. }
  183. static int cam_lrme_dev_remove(struct platform_device *pdev)
  184. {
  185. component_del(&pdev->dev, &cam_lrme_component_ops);
  186. return 0;
  187. }
  188. static const struct of_device_id cam_lrme_dt_match[] = {
  189. {
  190. .compatible = "qcom,cam-lrme"
  191. },
  192. {}
  193. };
  194. struct platform_driver cam_lrme_driver = {
  195. .probe = cam_lrme_dev_probe,
  196. .remove = cam_lrme_dev_remove,
  197. .driver = {
  198. .name = "cam_lrme",
  199. .owner = THIS_MODULE,
  200. .of_match_table = cam_lrme_dt_match,
  201. .suppress_bind_attrs = true,
  202. },
  203. };
  204. int cam_lrme_dev_init_module(void)
  205. {
  206. return platform_driver_register(&cam_lrme_driver);
  207. }
  208. void cam_lrme_dev_exit_module(void)
  209. {
  210. platform_driver_unregister(&cam_lrme_driver);
  211. }
  212. MODULE_DESCRIPTION("MSM LRME driver");
  213. MODULE_LICENSE("GPL v2");