rcar-fcp.c 4.4 KB

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