coresight-replicator.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /* Copyright (c) 2011-2012, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/device.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/io.h>
  17. #include <linux/err.h>
  18. #include <linux/slab.h>
  19. #include <linux/pm_runtime.h>
  20. #include <linux/clk.h>
  21. #include <linux/of.h>
  22. #include <linux/coresight.h>
  23. #include "coresight-priv.h"
  24. /**
  25. * struct replicator_drvdata - specifics associated to a replicator component
  26. * @dev: the device entity associated with this component
  27. * @atclk: optional clock for the core parts of the replicator.
  28. * @csdev: component vitals needed by the framework
  29. */
  30. struct replicator_drvdata {
  31. struct device *dev;
  32. struct clk *atclk;
  33. struct coresight_device *csdev;
  34. };
  35. static int replicator_enable(struct coresight_device *csdev, int inport,
  36. int outport)
  37. {
  38. struct replicator_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
  39. pm_runtime_get_sync(drvdata->dev);
  40. dev_info(drvdata->dev, "REPLICATOR enabled\n");
  41. return 0;
  42. }
  43. static void replicator_disable(struct coresight_device *csdev, int inport,
  44. int outport)
  45. {
  46. struct replicator_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
  47. pm_runtime_put(drvdata->dev);
  48. dev_info(drvdata->dev, "REPLICATOR disabled\n");
  49. }
  50. static const struct coresight_ops_link replicator_link_ops = {
  51. .enable = replicator_enable,
  52. .disable = replicator_disable,
  53. };
  54. static const struct coresight_ops replicator_cs_ops = {
  55. .link_ops = &replicator_link_ops,
  56. };
  57. static int replicator_probe(struct platform_device *pdev)
  58. {
  59. int ret;
  60. struct device *dev = &pdev->dev;
  61. struct coresight_platform_data *pdata = NULL;
  62. struct replicator_drvdata *drvdata;
  63. struct coresight_desc *desc;
  64. struct device_node *np = pdev->dev.of_node;
  65. if (np) {
  66. pdata = of_get_coresight_platform_data(dev, np);
  67. if (IS_ERR(pdata))
  68. return PTR_ERR(pdata);
  69. pdev->dev.platform_data = pdata;
  70. }
  71. drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
  72. if (!drvdata)
  73. return -ENOMEM;
  74. drvdata->dev = &pdev->dev;
  75. drvdata->atclk = devm_clk_get(&pdev->dev, "atclk"); /* optional */
  76. if (!IS_ERR(drvdata->atclk)) {
  77. ret = clk_prepare_enable(drvdata->atclk);
  78. if (ret)
  79. return ret;
  80. }
  81. pm_runtime_get_noresume(&pdev->dev);
  82. pm_runtime_set_active(&pdev->dev);
  83. pm_runtime_enable(&pdev->dev);
  84. platform_set_drvdata(pdev, drvdata);
  85. desc = devm_kzalloc(dev, sizeof(*desc), GFP_KERNEL);
  86. if (!desc) {
  87. ret = -ENOMEM;
  88. goto out_disable_pm;
  89. }
  90. desc->type = CORESIGHT_DEV_TYPE_LINK;
  91. desc->subtype.link_subtype = CORESIGHT_DEV_SUBTYPE_LINK_SPLIT;
  92. desc->ops = &replicator_cs_ops;
  93. desc->pdata = pdev->dev.platform_data;
  94. desc->dev = &pdev->dev;
  95. drvdata->csdev = coresight_register(desc);
  96. if (IS_ERR(drvdata->csdev)) {
  97. ret = PTR_ERR(drvdata->csdev);
  98. goto out_disable_pm;
  99. }
  100. pm_runtime_put(&pdev->dev);
  101. dev_info(dev, "REPLICATOR initialized\n");
  102. return 0;
  103. out_disable_pm:
  104. if (!IS_ERR(drvdata->atclk))
  105. clk_disable_unprepare(drvdata->atclk);
  106. pm_runtime_put_noidle(&pdev->dev);
  107. pm_runtime_disable(&pdev->dev);
  108. return ret;
  109. }
  110. static int replicator_remove(struct platform_device *pdev)
  111. {
  112. struct replicator_drvdata *drvdata = platform_get_drvdata(pdev);
  113. coresight_unregister(drvdata->csdev);
  114. pm_runtime_get_sync(&pdev->dev);
  115. if (!IS_ERR(drvdata->atclk))
  116. clk_disable_unprepare(drvdata->atclk);
  117. pm_runtime_put_noidle(&pdev->dev);
  118. pm_runtime_disable(&pdev->dev);
  119. return 0;
  120. }
  121. #ifdef CONFIG_PM
  122. static int replicator_runtime_suspend(struct device *dev)
  123. {
  124. struct replicator_drvdata *drvdata = dev_get_drvdata(dev);
  125. if (drvdata && !IS_ERR(drvdata->atclk))
  126. clk_disable_unprepare(drvdata->atclk);
  127. return 0;
  128. }
  129. static int replicator_runtime_resume(struct device *dev)
  130. {
  131. struct replicator_drvdata *drvdata = dev_get_drvdata(dev);
  132. if (drvdata && !IS_ERR(drvdata->atclk))
  133. clk_prepare_enable(drvdata->atclk);
  134. return 0;
  135. }
  136. #endif
  137. static const struct dev_pm_ops replicator_dev_pm_ops = {
  138. SET_RUNTIME_PM_OPS(replicator_runtime_suspend,
  139. replicator_runtime_resume, NULL)
  140. };
  141. static const struct of_device_id replicator_match[] = {
  142. {.compatible = "arm,coresight-replicator"},
  143. {}
  144. };
  145. static struct platform_driver replicator_driver = {
  146. .probe = replicator_probe,
  147. .remove = replicator_remove,
  148. .driver = {
  149. .name = "coresight-replicator",
  150. .of_match_table = replicator_match,
  151. .pm = &replicator_dev_pm_ops,
  152. },
  153. };
  154. builtin_platform_driver(replicator_driver);
  155. MODULE_LICENSE("GPL v2");
  156. MODULE_DESCRIPTION("CoreSight Replicator driver");