cam_lrme_context.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2017-2019, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/module.h>
  6. #include <linux/kernel.h>
  7. #include "cam_debug_util.h"
  8. #include "cam_lrme_context.h"
  9. static const char lrme_dev_name[] = "cam-lrme";
  10. static int __cam_lrme_ctx_acquire_dev_in_available(struct cam_context *ctx,
  11. struct cam_acquire_dev_cmd *cmd)
  12. {
  13. int rc = 0;
  14. uintptr_t ctxt_to_hw_map = (uintptr_t)ctx->ctxt_to_hw_map;
  15. struct cam_lrme_context *lrme_ctx = ctx->ctx_priv;
  16. CAM_DBG(CAM_LRME, "Enter ctx %d", ctx->ctx_id);
  17. rc = cam_context_acquire_dev_to_hw(ctx, cmd);
  18. if (rc) {
  19. CAM_ERR(CAM_LRME, "Failed to acquire");
  20. return rc;
  21. }
  22. ctxt_to_hw_map |= (lrme_ctx->index << CAM_LRME_CTX_INDEX_SHIFT);
  23. ctx->ctxt_to_hw_map = (void *)ctxt_to_hw_map;
  24. ctx->state = CAM_CTX_ACQUIRED;
  25. return rc;
  26. }
  27. static int __cam_lrme_ctx_release_dev_in_acquired(struct cam_context *ctx,
  28. struct cam_release_dev_cmd *cmd)
  29. {
  30. int rc = 0;
  31. CAM_DBG(CAM_LRME, "Enter ctx %d", ctx->ctx_id);
  32. rc = cam_context_release_dev_to_hw(ctx, cmd);
  33. if (rc) {
  34. CAM_ERR(CAM_LRME, "Failed to release");
  35. return rc;
  36. }
  37. ctx->state = CAM_CTX_AVAILABLE;
  38. return rc;
  39. }
  40. static int __cam_lrme_ctx_start_dev_in_acquired(struct cam_context *ctx,
  41. struct cam_start_stop_dev_cmd *cmd)
  42. {
  43. int rc = 0;
  44. CAM_DBG(CAM_LRME, "Enter ctx %d", ctx->ctx_id);
  45. rc = cam_context_start_dev_to_hw(ctx, cmd);
  46. if (rc) {
  47. CAM_ERR(CAM_LRME, "Failed to start");
  48. return rc;
  49. }
  50. ctx->state = CAM_CTX_ACTIVATED;
  51. return rc;
  52. }
  53. static int __cam_lrme_ctx_config_dev_in_activated(struct cam_context *ctx,
  54. struct cam_config_dev_cmd *cmd)
  55. {
  56. int rc;
  57. CAM_DBG(CAM_LRME, "Enter ctx %d", ctx->ctx_id);
  58. rc = cam_context_prepare_dev_to_hw(ctx, cmd);
  59. if (rc) {
  60. CAM_ERR(CAM_LRME, "Failed to config");
  61. return rc;
  62. }
  63. return rc;
  64. }
  65. static int __cam_lrme_ctx_flush_dev_in_activated(struct cam_context *ctx,
  66. struct cam_flush_dev_cmd *cmd)
  67. {
  68. int rc;
  69. CAM_DBG(CAM_LRME, "Enter ctx %d", ctx->ctx_id);
  70. rc = cam_context_flush_dev_to_hw(ctx, cmd);
  71. if (rc)
  72. CAM_ERR(CAM_LRME, "Failed to flush device");
  73. return rc;
  74. }
  75. static int __cam_lrme_ctx_stop_dev_in_activated(struct cam_context *ctx,
  76. struct cam_start_stop_dev_cmd *cmd)
  77. {
  78. int rc = 0;
  79. CAM_DBG(CAM_LRME, "Enter ctx %d", ctx->ctx_id);
  80. rc = cam_context_stop_dev_to_hw(ctx);
  81. if (rc) {
  82. CAM_ERR(CAM_LRME, "Failed to stop dev");
  83. return rc;
  84. }
  85. ctx->state = CAM_CTX_ACQUIRED;
  86. return rc;
  87. }
  88. static int __cam_lrme_ctx_release_dev_in_activated(struct cam_context *ctx,
  89. struct cam_release_dev_cmd *cmd)
  90. {
  91. int rc = 0;
  92. CAM_DBG(CAM_LRME, "Enter ctx %d", ctx->ctx_id);
  93. rc = __cam_lrme_ctx_stop_dev_in_activated(ctx, NULL);
  94. if (rc) {
  95. CAM_ERR(CAM_LRME, "Failed to stop");
  96. return rc;
  97. }
  98. rc = cam_context_release_dev_to_hw(ctx, cmd);
  99. if (rc) {
  100. CAM_ERR(CAM_LRME, "Failed to release");
  101. return rc;
  102. }
  103. ctx->state = CAM_CTX_AVAILABLE;
  104. return rc;
  105. }
  106. static int __cam_lrme_ctx_handle_irq_in_activated(void *context,
  107. uint32_t evt_id, void *evt_data)
  108. {
  109. int rc;
  110. CAM_DBG(CAM_LRME, "Enter");
  111. rc = cam_context_buf_done_from_hw(context, evt_data, evt_id);
  112. if (rc) {
  113. CAM_ERR(CAM_LRME, "Failed in buf done, rc=%d", rc);
  114. return rc;
  115. }
  116. return rc;
  117. }
  118. /* top state machine */
  119. static struct cam_ctx_ops
  120. cam_lrme_ctx_state_machine[CAM_CTX_STATE_MAX] = {
  121. /* Uninit */
  122. {
  123. .ioctl_ops = {},
  124. .crm_ops = {},
  125. .irq_ops = NULL,
  126. },
  127. /* Available */
  128. {
  129. .ioctl_ops = {
  130. .acquire_dev = __cam_lrme_ctx_acquire_dev_in_available,
  131. },
  132. .crm_ops = {},
  133. .irq_ops = NULL,
  134. },
  135. /* Acquired */
  136. {
  137. .ioctl_ops = {
  138. .config_dev = __cam_lrme_ctx_config_dev_in_activated,
  139. .release_dev = __cam_lrme_ctx_release_dev_in_acquired,
  140. .start_dev = __cam_lrme_ctx_start_dev_in_acquired,
  141. },
  142. .crm_ops = {},
  143. .irq_ops = NULL,
  144. },
  145. /* Ready */
  146. {
  147. .ioctl_ops = {},
  148. .crm_ops = {},
  149. .irq_ops = NULL,
  150. },
  151. /* Flushed */
  152. {},
  153. /* Activate */
  154. {
  155. .ioctl_ops = {
  156. .config_dev = __cam_lrme_ctx_config_dev_in_activated,
  157. .release_dev = __cam_lrme_ctx_release_dev_in_activated,
  158. .stop_dev = __cam_lrme_ctx_stop_dev_in_activated,
  159. .flush_dev = __cam_lrme_ctx_flush_dev_in_activated,
  160. },
  161. .crm_ops = {},
  162. .irq_ops = __cam_lrme_ctx_handle_irq_in_activated,
  163. },
  164. };
  165. int cam_lrme_context_init(struct cam_lrme_context *lrme_ctx,
  166. struct cam_context *base_ctx,
  167. struct cam_hw_mgr_intf *hw_intf,
  168. uint32_t index)
  169. {
  170. int rc = 0;
  171. CAM_DBG(CAM_LRME, "Enter");
  172. if (!base_ctx || !lrme_ctx) {
  173. CAM_ERR(CAM_LRME, "Invalid input");
  174. return -EINVAL;
  175. }
  176. memset(lrme_ctx, 0, sizeof(*lrme_ctx));
  177. rc = cam_context_init(base_ctx, lrme_dev_name, CAM_LRME, index,
  178. NULL, hw_intf, lrme_ctx->req_base, CAM_CTX_REQ_MAX);
  179. if (rc) {
  180. CAM_ERR(CAM_LRME, "Failed to init context");
  181. return rc;
  182. }
  183. lrme_ctx->base = base_ctx;
  184. lrme_ctx->index = index;
  185. base_ctx->ctx_priv = lrme_ctx;
  186. base_ctx->state_machine = cam_lrme_ctx_state_machine;
  187. return rc;
  188. }
  189. int cam_lrme_context_deinit(struct cam_lrme_context *lrme_ctx)
  190. {
  191. int rc = 0;
  192. CAM_DBG(CAM_LRME, "Enter");
  193. if (!lrme_ctx) {
  194. CAM_ERR(CAM_LRME, "No ctx to deinit");
  195. return -EINVAL;
  196. }
  197. rc = cam_context_deinit(lrme_ctx->base);
  198. memset(lrme_ctx, 0, sizeof(*lrme_ctx));
  199. return rc;
  200. }