cam_lrme_dev.c 5.0 KB

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