cam_jpeg_dev.c 4.2 KB

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