cam_jpeg_dev.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2017-2021, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/delay.h>
  6. #include <linux/io.h>
  7. #include <linux/of.h>
  8. #include <linux/module.h>
  9. #include <linux/kernel.h>
  10. #include "cam_node.h"
  11. #include "cam_hw_mgr_intf.h"
  12. #include "cam_jpeg_hw_mgr_intf.h"
  13. #include "cam_jpeg_dev.h"
  14. #include "cam_debug_util.h"
  15. #include "cam_smmu_api.h"
  16. #include "camera_main.h"
  17. #define CAM_JPEG_DEV_NAME "cam-jpeg"
  18. static struct cam_jpeg_dev g_jpeg_dev;
  19. static void cam_jpeg_dev_iommu_fault_handler(
  20. struct cam_smmu_pf_info *pf_info)
  21. {
  22. int i = 0;
  23. struct cam_node *node = NULL;
  24. if (!pf_info || !pf_info->token) {
  25. CAM_ERR(CAM_ISP, "invalid token in page handler cb");
  26. return;
  27. }
  28. node = (struct cam_node *)pf_info->token;
  29. for (i = 0; i < node->ctx_size; i++)
  30. cam_context_dump_pf_info(&(node->ctx_list[i]), pf_info);
  31. }
  32. static void cam_jpeg_dev_mini_dump_cb(void *priv, void *args)
  33. {
  34. struct cam_context *ctx = NULL;
  35. if (!priv || !args) {
  36. CAM_ERR(CAM_JPEG, "Invalid param priv %pK %pK args", priv, args);
  37. return;
  38. }
  39. ctx = (struct cam_context *)priv;
  40. cam_context_mini_dump_from_hw(ctx, args);
  41. }
  42. static const struct of_device_id cam_jpeg_dt_match[] = {
  43. {
  44. .compatible = "qcom,cam-jpeg"
  45. },
  46. { }
  47. };
  48. static int cam_jpeg_subdev_open(struct v4l2_subdev *sd,
  49. struct v4l2_subdev_fh *fh)
  50. {
  51. mutex_lock(&g_jpeg_dev.jpeg_mutex);
  52. g_jpeg_dev.open_cnt++;
  53. mutex_unlock(&g_jpeg_dev.jpeg_mutex);
  54. return 0;
  55. }
  56. static int cam_jpeg_subdev_close_internal(struct v4l2_subdev *sd,
  57. struct v4l2_subdev_fh *fh)
  58. {
  59. int rc = 0;
  60. struct cam_node *node = v4l2_get_subdevdata(sd);
  61. mutex_lock(&g_jpeg_dev.jpeg_mutex);
  62. if (g_jpeg_dev.open_cnt <= 0) {
  63. CAM_DBG(CAM_JPEG, "JPEG subdev is already closed");
  64. rc = -EINVAL;
  65. goto end;
  66. }
  67. g_jpeg_dev.open_cnt--;
  68. if (!node) {
  69. CAM_ERR(CAM_JPEG, "Node ptr is NULL");
  70. rc = -EINVAL;
  71. goto end;
  72. }
  73. if (g_jpeg_dev.open_cnt == 0)
  74. cam_node_shutdown(node);
  75. end:
  76. mutex_unlock(&g_jpeg_dev.jpeg_mutex);
  77. return rc;
  78. }
  79. static int cam_jpeg_subdev_close(struct v4l2_subdev *sd,
  80. struct v4l2_subdev_fh *fh)
  81. {
  82. bool crm_active = cam_req_mgr_is_open();
  83. if (crm_active) {
  84. CAM_DBG(CAM_JPEG, "CRM is ACTIVE, close should be from CRM");
  85. return 0;
  86. }
  87. return cam_jpeg_subdev_close_internal(sd, fh);
  88. }
  89. static const struct v4l2_subdev_internal_ops cam_jpeg_subdev_internal_ops = {
  90. .close = cam_jpeg_subdev_close,
  91. .open = cam_jpeg_subdev_open,
  92. };
  93. static int cam_jpeg_dev_component_bind(struct device *dev,
  94. struct device *master_dev, void *data)
  95. {
  96. int rc;
  97. int i;
  98. struct cam_hw_mgr_intf hw_mgr_intf;
  99. struct cam_node *node;
  100. int iommu_hdl = -1;
  101. struct platform_device *pdev = to_platform_device(dev);
  102. g_jpeg_dev.sd.internal_ops = &cam_jpeg_subdev_internal_ops;
  103. g_jpeg_dev.sd.close_seq_prior = CAM_SD_CLOSE_MEDIUM_PRIORITY;
  104. rc = cam_subdev_probe(&g_jpeg_dev.sd, pdev, CAM_JPEG_DEV_NAME,
  105. CAM_JPEG_DEVICE_TYPE);
  106. if (rc) {
  107. CAM_ERR(CAM_JPEG, "JPEG cam_subdev_probe failed %d", rc);
  108. goto err;
  109. }
  110. node = (struct cam_node *)g_jpeg_dev.sd.token;
  111. rc = cam_jpeg_hw_mgr_init(pdev->dev.of_node,
  112. (uint64_t *)&hw_mgr_intf, &iommu_hdl,
  113. cam_jpeg_dev_mini_dump_cb);
  114. if (rc) {
  115. CAM_ERR(CAM_JPEG, "Can not initialize JPEG HWmanager %d", rc);
  116. goto unregister;
  117. }
  118. for (i = 0; i < CAM_JPEG_CTX_MAX; i++) {
  119. rc = cam_jpeg_context_init(&g_jpeg_dev.ctx_jpeg[i],
  120. &g_jpeg_dev.ctx[i],
  121. &node->hw_mgr_intf,
  122. i, iommu_hdl);
  123. if (rc) {
  124. CAM_ERR(CAM_JPEG, "JPEG context init failed %d %d",
  125. i, rc);
  126. goto ctx_init_fail;
  127. }
  128. }
  129. rc = cam_node_init(node, &hw_mgr_intf, g_jpeg_dev.ctx, CAM_JPEG_CTX_MAX,
  130. CAM_JPEG_DEV_NAME);
  131. if (rc) {
  132. CAM_ERR(CAM_JPEG, "JPEG node init failed %d", rc);
  133. goto ctx_init_fail;
  134. }
  135. node->sd_handler = cam_jpeg_subdev_close_internal;
  136. cam_smmu_set_client_page_fault_handler(iommu_hdl,
  137. cam_jpeg_dev_iommu_fault_handler, node);
  138. mutex_init(&g_jpeg_dev.jpeg_mutex);
  139. CAM_DBG(CAM_JPEG, "Component bound successfully");
  140. return rc;
  141. ctx_init_fail:
  142. for (--i; i >= 0; i--)
  143. if (cam_jpeg_context_deinit(&g_jpeg_dev.ctx_jpeg[i]))
  144. CAM_ERR(CAM_JPEG, "deinit fail %d %d", i, rc);
  145. unregister:
  146. if (cam_subdev_remove(&g_jpeg_dev.sd))
  147. CAM_ERR(CAM_JPEG, "remove fail %d", rc);
  148. err:
  149. return rc;
  150. }
  151. static void cam_jpeg_dev_component_unbind(struct device *dev,
  152. struct device *master_dev, void *data)
  153. {
  154. int rc;
  155. int i;
  156. for (i = 0; i < CAM_CTX_MAX; i++) {
  157. rc = cam_jpeg_context_deinit(&g_jpeg_dev.ctx_jpeg[i]);
  158. if (rc)
  159. CAM_ERR(CAM_JPEG, "JPEG context %d deinit failed %d",
  160. i, rc);
  161. }
  162. rc = cam_subdev_remove(&g_jpeg_dev.sd);
  163. if (rc)
  164. CAM_ERR(CAM_JPEG, "Unregister failed %d", rc);
  165. }
  166. const static struct component_ops cam_jpeg_dev_component_ops = {
  167. .bind = cam_jpeg_dev_component_bind,
  168. .unbind = cam_jpeg_dev_component_unbind,
  169. };
  170. static int cam_jpeg_dev_remove(struct platform_device *pdev)
  171. {
  172. component_del(&pdev->dev, &cam_jpeg_dev_component_ops);
  173. return 0;
  174. }
  175. static int cam_jpeg_dev_probe(struct platform_device *pdev)
  176. {
  177. int rc = 0;
  178. CAM_DBG(CAM_JPEG, "Adding JPEG component");
  179. rc = component_add(&pdev->dev, &cam_jpeg_dev_component_ops);
  180. if (rc)
  181. CAM_ERR(CAM_JPEG, "failed to add component rc: %d", rc);
  182. return rc;
  183. }
  184. struct platform_driver jpeg_driver = {
  185. .probe = cam_jpeg_dev_probe,
  186. .remove = cam_jpeg_dev_remove,
  187. .driver = {
  188. .name = "cam_jpeg",
  189. .owner = THIS_MODULE,
  190. .of_match_table = cam_jpeg_dt_match,
  191. .suppress_bind_attrs = true,
  192. },
  193. };
  194. int cam_jpeg_dev_init_module(void)
  195. {
  196. return platform_driver_register(&jpeg_driver);
  197. }
  198. void cam_jpeg_dev_exit_module(void)
  199. {
  200. platform_driver_unregister(&jpeg_driver);
  201. }
  202. MODULE_DESCRIPTION("MSM JPEG driver");
  203. MODULE_LICENSE("GPL v2");