rcar-fcp.c 4.2 KB

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