rcar-fcp.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * rcar-fcp.c -- R-Car Frame Compression Processor Driver
  4. *
  5. * Copyright (C) 2016 Renesas Electronics Corporation
  6. *
  7. * Contact: Laurent Pinchart ([email protected])
  8. */
  9. #include <linux/device.h>
  10. #include <linux/dma-mapping.h>
  11. #include <linux/list.h>
  12. #include <linux/module.h>
  13. #include <linux/mod_devicetable.h>
  14. #include <linux/mutex.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/pm_runtime.h>
  17. #include <linux/slab.h>
  18. #include <media/rcar-fcp.h>
  19. struct rcar_fcp_device {
  20. struct list_head list;
  21. struct device *dev;
  22. };
  23. static LIST_HEAD(fcp_devices);
  24. static DEFINE_MUTEX(fcp_lock);
  25. /* -----------------------------------------------------------------------------
  26. * Public API
  27. */
  28. /**
  29. * rcar_fcp_get - Find and acquire a reference to an FCP instance
  30. * @np: Device node of the FCP instance
  31. *
  32. * Search the list of registered FCP instances for the instance corresponding to
  33. * the given device node.
  34. *
  35. * Return a pointer to the FCP instance, or an ERR_PTR if the instance can't be
  36. * found.
  37. */
  38. struct rcar_fcp_device *rcar_fcp_get(const struct device_node *np)
  39. {
  40. struct rcar_fcp_device *fcp;
  41. mutex_lock(&fcp_lock);
  42. list_for_each_entry(fcp, &fcp_devices, list) {
  43. if (fcp->dev->of_node != np)
  44. continue;
  45. get_device(fcp->dev);
  46. goto done;
  47. }
  48. fcp = ERR_PTR(-EPROBE_DEFER);
  49. done:
  50. mutex_unlock(&fcp_lock);
  51. return fcp;
  52. }
  53. EXPORT_SYMBOL_GPL(rcar_fcp_get);
  54. /**
  55. * rcar_fcp_put - Release a reference to an FCP instance
  56. * @fcp: The FCP instance
  57. *
  58. * Release the FCP instance acquired by a call to rcar_fcp_get().
  59. */
  60. void rcar_fcp_put(struct rcar_fcp_device *fcp)
  61. {
  62. if (fcp)
  63. put_device(fcp->dev);
  64. }
  65. EXPORT_SYMBOL_GPL(rcar_fcp_put);
  66. struct device *rcar_fcp_get_device(struct rcar_fcp_device *fcp)
  67. {
  68. return fcp->dev;
  69. }
  70. EXPORT_SYMBOL_GPL(rcar_fcp_get_device);
  71. /**
  72. * rcar_fcp_enable - Enable an FCP
  73. * @fcp: The FCP instance
  74. *
  75. * Before any memory access through an FCP is performed by a module, the FCP
  76. * must be enabled by a call to this function. The enable calls are reference
  77. * counted, each successful call must be followed by one rcar_fcp_disable()
  78. * call when no more memory transfer can occur through the FCP.
  79. *
  80. * Return 0 on success or a negative error code if an error occurs. The enable
  81. * reference count isn't increased when this function returns an error.
  82. */
  83. int rcar_fcp_enable(struct rcar_fcp_device *fcp)
  84. {
  85. if (!fcp)
  86. return 0;
  87. return pm_runtime_resume_and_get(fcp->dev);
  88. }
  89. EXPORT_SYMBOL_GPL(rcar_fcp_enable);
  90. /**
  91. * rcar_fcp_disable - Disable an FCP
  92. * @fcp: The FCP instance
  93. *
  94. * This function is the counterpart of rcar_fcp_enable(). As enable calls are
  95. * reference counted a disable call may not disable the FCP synchronously.
  96. */
  97. void rcar_fcp_disable(struct rcar_fcp_device *fcp)
  98. {
  99. if (fcp)
  100. pm_runtime_put(fcp->dev);
  101. }
  102. EXPORT_SYMBOL_GPL(rcar_fcp_disable);
  103. /* -----------------------------------------------------------------------------
  104. * Platform Driver
  105. */
  106. static int rcar_fcp_probe(struct platform_device *pdev)
  107. {
  108. struct rcar_fcp_device *fcp;
  109. fcp = devm_kzalloc(&pdev->dev, sizeof(*fcp), GFP_KERNEL);
  110. if (fcp == NULL)
  111. return -ENOMEM;
  112. fcp->dev = &pdev->dev;
  113. dma_set_max_seg_size(fcp->dev, UINT_MAX);
  114. pm_runtime_enable(&pdev->dev);
  115. mutex_lock(&fcp_lock);
  116. list_add_tail(&fcp->list, &fcp_devices);
  117. mutex_unlock(&fcp_lock);
  118. platform_set_drvdata(pdev, fcp);
  119. return 0;
  120. }
  121. static int rcar_fcp_remove(struct platform_device *pdev)
  122. {
  123. struct rcar_fcp_device *fcp = platform_get_drvdata(pdev);
  124. mutex_lock(&fcp_lock);
  125. list_del(&fcp->list);
  126. mutex_unlock(&fcp_lock);
  127. pm_runtime_disable(&pdev->dev);
  128. return 0;
  129. }
  130. static const struct of_device_id rcar_fcp_of_match[] = {
  131. { .compatible = "renesas,fcpf" },
  132. { .compatible = "renesas,fcpv" },
  133. { },
  134. };
  135. MODULE_DEVICE_TABLE(of, rcar_fcp_of_match);
  136. static struct platform_driver rcar_fcp_platform_driver = {
  137. .probe = rcar_fcp_probe,
  138. .remove = rcar_fcp_remove,
  139. .driver = {
  140. .name = "rcar-fcp",
  141. .of_match_table = rcar_fcp_of_match,
  142. .suppress_bind_attrs = true,
  143. },
  144. };
  145. module_platform_driver(rcar_fcp_platform_driver);
  146. MODULE_ALIAS("rcar-fcp");
  147. MODULE_AUTHOR("Laurent Pinchart <[email protected]>");
  148. MODULE_DESCRIPTION("Renesas FCP Driver");
  149. MODULE_LICENSE("GPL");