cam_jpeg_dev.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2017-2020, 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. static int cam_jpeg_subdev_close(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 const struct v4l2_subdev_internal_ops cam_jpeg_subdev_internal_ops = {
  70. .close = cam_jpeg_subdev_close,
  71. .open = cam_jpeg_subdev_open,
  72. };
  73. static int cam_jpeg_dev_component_bind(struct device *dev,
  74. struct device *master_dev, void *data)
  75. {
  76. int rc;
  77. int i;
  78. struct cam_hw_mgr_intf hw_mgr_intf;
  79. struct cam_node *node;
  80. int iommu_hdl = -1;
  81. struct platform_device *pdev = to_platform_device(dev);
  82. g_jpeg_dev.sd.internal_ops = &cam_jpeg_subdev_internal_ops;
  83. rc = cam_subdev_probe(&g_jpeg_dev.sd, pdev, CAM_JPEG_DEV_NAME,
  84. CAM_JPEG_DEVICE_TYPE);
  85. if (rc) {
  86. CAM_ERR(CAM_JPEG, "JPEG cam_subdev_probe failed %d", rc);
  87. goto err;
  88. }
  89. node = (struct cam_node *)g_jpeg_dev.sd.token;
  90. rc = cam_jpeg_hw_mgr_init(pdev->dev.of_node,
  91. (uint64_t *)&hw_mgr_intf, &iommu_hdl);
  92. if (rc) {
  93. CAM_ERR(CAM_JPEG, "Can not initialize JPEG HWmanager %d", rc);
  94. goto unregister;
  95. }
  96. for (i = 0; i < CAM_JPEG_CTX_MAX; i++) {
  97. rc = cam_jpeg_context_init(&g_jpeg_dev.ctx_jpeg[i],
  98. &g_jpeg_dev.ctx[i],
  99. &node->hw_mgr_intf,
  100. i);
  101. if (rc) {
  102. CAM_ERR(CAM_JPEG, "JPEG context init failed %d %d",
  103. i, rc);
  104. goto ctx_init_fail;
  105. }
  106. }
  107. rc = cam_node_init(node, &hw_mgr_intf, g_jpeg_dev.ctx, CAM_JPEG_CTX_MAX,
  108. CAM_JPEG_DEV_NAME);
  109. if (rc) {
  110. CAM_ERR(CAM_JPEG, "JPEG node init failed %d", rc);
  111. goto ctx_init_fail;
  112. }
  113. cam_smmu_set_client_page_fault_handler(iommu_hdl,
  114. cam_jpeg_dev_iommu_fault_handler, node);
  115. mutex_init(&g_jpeg_dev.jpeg_mutex);
  116. CAM_DBG(CAM_JPEG, "Component bound successfully");
  117. return rc;
  118. ctx_init_fail:
  119. for (--i; i >= 0; i--)
  120. if (cam_jpeg_context_deinit(&g_jpeg_dev.ctx_jpeg[i]))
  121. CAM_ERR(CAM_JPEG, "deinit fail %d %d", i, rc);
  122. unregister:
  123. if (cam_subdev_remove(&g_jpeg_dev.sd))
  124. CAM_ERR(CAM_JPEG, "remove fail %d", rc);
  125. err:
  126. return rc;
  127. }
  128. static void cam_jpeg_dev_component_unbind(struct device *dev,
  129. struct device *master_dev, void *data)
  130. {
  131. int rc;
  132. int i;
  133. for (i = 0; i < CAM_CTX_MAX; i++) {
  134. rc = cam_jpeg_context_deinit(&g_jpeg_dev.ctx_jpeg[i]);
  135. if (rc)
  136. CAM_ERR(CAM_JPEG, "JPEG context %d deinit failed %d",
  137. i, rc);
  138. }
  139. rc = cam_subdev_remove(&g_jpeg_dev.sd);
  140. if (rc)
  141. CAM_ERR(CAM_JPEG, "Unregister failed %d", rc);
  142. }
  143. const static struct component_ops cam_jpeg_dev_component_ops = {
  144. .bind = cam_jpeg_dev_component_bind,
  145. .unbind = cam_jpeg_dev_component_unbind,
  146. };
  147. static int cam_jpeg_dev_remove(struct platform_device *pdev)
  148. {
  149. component_del(&pdev->dev, &cam_jpeg_dev_component_ops);
  150. return 0;
  151. }
  152. static int cam_jpeg_dev_probe(struct platform_device *pdev)
  153. {
  154. int rc = 0;
  155. CAM_DBG(CAM_JPEG, "Adding JPEG component");
  156. rc = component_add(&pdev->dev, &cam_jpeg_dev_component_ops);
  157. if (rc)
  158. CAM_ERR(CAM_JPEG, "failed to add component rc: %d", rc);
  159. return rc;
  160. }
  161. struct platform_driver jpeg_driver = {
  162. .probe = cam_jpeg_dev_probe,
  163. .remove = cam_jpeg_dev_remove,
  164. .driver = {
  165. .name = "cam_jpeg",
  166. .owner = THIS_MODULE,
  167. .of_match_table = cam_jpeg_dt_match,
  168. .suppress_bind_attrs = true,
  169. },
  170. };
  171. int cam_jpeg_dev_init_module(void)
  172. {
  173. return platform_driver_register(&jpeg_driver);
  174. }
  175. void cam_jpeg_dev_exit_module(void)
  176. {
  177. platform_driver_unregister(&jpeg_driver);
  178. }
  179. MODULE_DESCRIPTION("MSM JPEG driver");
  180. MODULE_LICENSE("GPL v2");