rcar-fcp.c 4.4 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. /*
  48. * Make sure the module won't be unloaded behind our back. This
  49. * is a poor man's safety net, the module should really not be
  50. * unloaded while FCP users can be active.
  51. */
  52. if (!try_module_get(fcp->dev->driver->owner))
  53. fcp = NULL;
  54. goto done;
  55. }
  56. fcp = ERR_PTR(-EPROBE_DEFER);
  57. done:
  58. mutex_unlock(&fcp_lock);
  59. return fcp;
  60. }
  61. EXPORT_SYMBOL_GPL(rcar_fcp_get);
  62. /**
  63. * rcar_fcp_put - Release a reference to an FCP instance
  64. * @fcp: The FCP instance
  65. *
  66. * Release the FCP instance acquired by a call to rcar_fcp_get().
  67. */
  68. void rcar_fcp_put(struct rcar_fcp_device *fcp)
  69. {
  70. if (fcp)
  71. module_put(fcp->dev->driver->owner);
  72. }
  73. EXPORT_SYMBOL_GPL(rcar_fcp_put);
  74. /**
  75. * rcar_fcp_enable - Enable an FCP
  76. * @fcp: The FCP instance
  77. *
  78. * Before any memory access through an FCP is performed by a module, the FCP
  79. * must be enabled by a call to this function. The enable calls are reference
  80. * counted, each successful call must be followed by one rcar_fcp_disable()
  81. * call when no more memory transfer can occur through the FCP.
  82. *
  83. * Return 0 on success or a negative error code if an error occurs. The enable
  84. * reference count isn't increased when this function returns an error.
  85. */
  86. int rcar_fcp_enable(struct rcar_fcp_device *fcp)
  87. {
  88. int ret;
  89. if (!fcp)
  90. return 0;
  91. ret = pm_runtime_get_sync(fcp->dev);
  92. if (ret < 0)
  93. return ret;
  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. pm_runtime_enable(&pdev->dev);
  121. mutex_lock(&fcp_lock);
  122. list_add_tail(&fcp->list, &fcp_devices);
  123. mutex_unlock(&fcp_lock);
  124. platform_set_drvdata(pdev, fcp);
  125. return 0;
  126. }
  127. static int rcar_fcp_remove(struct platform_device *pdev)
  128. {
  129. struct rcar_fcp_device *fcp = platform_get_drvdata(pdev);
  130. mutex_lock(&fcp_lock);
  131. list_del(&fcp->list);
  132. mutex_unlock(&fcp_lock);
  133. pm_runtime_disable(&pdev->dev);
  134. return 0;
  135. }
  136. static const struct of_device_id rcar_fcp_of_match[] = {
  137. { .compatible = "renesas,fcpf" },
  138. { .compatible = "renesas,fcpv" },
  139. { },
  140. };
  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");