rcar-fcp.c 4.3 KB

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