cam_jpeg_dev.c 4.9 KB

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