cam_jpeg_dev.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 const struct of_device_id cam_jpeg_dt_match[] = {
  33. {
  34. .compatible = "qcom,cam-jpeg"
  35. },
  36. { }
  37. };
  38. static int cam_jpeg_subdev_open(struct v4l2_subdev *sd,
  39. struct v4l2_subdev_fh *fh)
  40. {
  41. mutex_lock(&g_jpeg_dev.jpeg_mutex);
  42. g_jpeg_dev.open_cnt++;
  43. mutex_unlock(&g_jpeg_dev.jpeg_mutex);
  44. return 0;
  45. }
  46. int cam_jpeg_subdev_close_internal(struct v4l2_subdev *sd,
  47. struct v4l2_subdev_fh *fh)
  48. {
  49. int rc = 0;
  50. struct cam_node *node = v4l2_get_subdevdata(sd);
  51. mutex_lock(&g_jpeg_dev.jpeg_mutex);
  52. if (g_jpeg_dev.open_cnt <= 0) {
  53. CAM_DBG(CAM_JPEG, "JPEG subdev is already closed");
  54. rc = -EINVAL;
  55. goto end;
  56. }
  57. g_jpeg_dev.open_cnt--;
  58. if (!node) {
  59. CAM_ERR(CAM_JPEG, "Node ptr is NULL");
  60. rc = -EINVAL;
  61. goto end;
  62. }
  63. if (g_jpeg_dev.open_cnt == 0)
  64. cam_node_shutdown(node);
  65. end:
  66. mutex_unlock(&g_jpeg_dev.jpeg_mutex);
  67. return rc;
  68. }
  69. static int cam_jpeg_subdev_close(struct v4l2_subdev *sd,
  70. struct v4l2_subdev_fh *fh)
  71. {
  72. bool crm_active = cam_req_mgr_is_open(CAM_JPEG);
  73. if (crm_active) {
  74. CAM_DBG(CAM_JPEG, "CRM is ACTIVE, close should be from CRM");
  75. return 0;
  76. }
  77. return cam_jpeg_subdev_close_internal(sd, fh);
  78. }
  79. static const struct v4l2_subdev_internal_ops cam_jpeg_subdev_internal_ops = {
  80. .close = cam_jpeg_subdev_close,
  81. .open = cam_jpeg_subdev_open,
  82. };
  83. static int cam_jpeg_dev_component_bind(struct device *dev,
  84. struct device *master_dev, void *data)
  85. {
  86. int rc;
  87. int i;
  88. struct cam_hw_mgr_intf hw_mgr_intf;
  89. struct cam_node *node;
  90. int iommu_hdl = -1;
  91. struct platform_device *pdev = to_platform_device(dev);
  92. g_jpeg_dev.sd.internal_ops = &cam_jpeg_subdev_internal_ops;
  93. g_jpeg_dev.sd.close_seq_prior = CAM_SD_CLOSE_MEDIUM_PRIORITY;
  94. rc = cam_subdev_probe(&g_jpeg_dev.sd, pdev, CAM_JPEG_DEV_NAME,
  95. CAM_JPEG_DEVICE_TYPE);
  96. if (rc) {
  97. CAM_ERR(CAM_JPEG, "JPEG cam_subdev_probe failed %d", rc);
  98. goto err;
  99. }
  100. node = (struct cam_node *)g_jpeg_dev.sd.token;
  101. rc = cam_jpeg_hw_mgr_init(pdev->dev.of_node,
  102. (uint64_t *)&hw_mgr_intf, &iommu_hdl);
  103. if (rc) {
  104. CAM_ERR(CAM_JPEG, "Can not initialize JPEG HWmanager %d", rc);
  105. goto unregister;
  106. }
  107. for (i = 0; i < CAM_JPEG_CTX_MAX; i++) {
  108. rc = cam_jpeg_context_init(&g_jpeg_dev.ctx_jpeg[i],
  109. &g_jpeg_dev.ctx[i],
  110. &node->hw_mgr_intf,
  111. i);
  112. if (rc) {
  113. CAM_ERR(CAM_JPEG, "JPEG context init failed %d %d",
  114. i, rc);
  115. goto ctx_init_fail;
  116. }
  117. }
  118. rc = cam_node_init(node, &hw_mgr_intf, g_jpeg_dev.ctx, CAM_JPEG_CTX_MAX,
  119. CAM_JPEG_DEV_NAME);
  120. if (rc) {
  121. CAM_ERR(CAM_JPEG, "JPEG node init failed %d", rc);
  122. goto ctx_init_fail;
  123. }
  124. cam_smmu_set_client_page_fault_handler(iommu_hdl,
  125. cam_jpeg_dev_iommu_fault_handler, node);
  126. mutex_init(&g_jpeg_dev.jpeg_mutex);
  127. CAM_DBG(CAM_JPEG, "Component bound successfully");
  128. return rc;
  129. ctx_init_fail:
  130. for (--i; i >= 0; i--)
  131. if (cam_jpeg_context_deinit(&g_jpeg_dev.ctx_jpeg[i]))
  132. CAM_ERR(CAM_JPEG, "deinit fail %d %d", i, rc);
  133. unregister:
  134. if (cam_subdev_remove(&g_jpeg_dev.sd))
  135. CAM_ERR(CAM_JPEG, "remove fail %d", rc);
  136. err:
  137. return rc;
  138. }
  139. static void cam_jpeg_dev_component_unbind(struct device *dev,
  140. struct device *master_dev, void *data)
  141. {
  142. int rc;
  143. int i;
  144. for (i = 0; i < CAM_CTX_MAX; i++) {
  145. rc = cam_jpeg_context_deinit(&g_jpeg_dev.ctx_jpeg[i]);
  146. if (rc)
  147. CAM_ERR(CAM_JPEG, "JPEG context %d deinit failed %d",
  148. i, rc);
  149. }
  150. rc = cam_subdev_remove(&g_jpeg_dev.sd);
  151. if (rc)
  152. CAM_ERR(CAM_JPEG, "Unregister failed %d", rc);
  153. }
  154. const static struct component_ops cam_jpeg_dev_component_ops = {
  155. .bind = cam_jpeg_dev_component_bind,
  156. .unbind = cam_jpeg_dev_component_unbind,
  157. };
  158. static int cam_jpeg_dev_remove(struct platform_device *pdev)
  159. {
  160. component_del(&pdev->dev, &cam_jpeg_dev_component_ops);
  161. return 0;
  162. }
  163. static int cam_jpeg_dev_probe(struct platform_device *pdev)
  164. {
  165. int rc = 0;
  166. CAM_DBG(CAM_JPEG, "Adding JPEG component");
  167. rc = component_add(&pdev->dev, &cam_jpeg_dev_component_ops);
  168. if (rc)
  169. CAM_ERR(CAM_JPEG, "failed to add component rc: %d", rc);
  170. return rc;
  171. }
  172. struct platform_driver jpeg_driver = {
  173. .probe = cam_jpeg_dev_probe,
  174. .remove = cam_jpeg_dev_remove,
  175. .driver = {
  176. .name = "cam_jpeg",
  177. .owner = THIS_MODULE,
  178. .of_match_table = cam_jpeg_dt_match,
  179. .suppress_bind_attrs = true,
  180. },
  181. };
  182. int cam_jpeg_dev_init_module(void)
  183. {
  184. return platform_driver_register(&jpeg_driver);
  185. }
  186. void cam_jpeg_dev_exit_module(void)
  187. {
  188. platform_driver_unregister(&jpeg_driver);
  189. }
  190. MODULE_DESCRIPTION("MSM JPEG driver");
  191. MODULE_LICENSE("GPL v2");