omap-ocp2scp.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * omap-ocp2scp.c - transform ocp interface protocol to scp protocol
  3. *
  4. * Copyright (C) 2012 Texas Instruments Incorporated - http://www.ti.com
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * Author: Kishon Vijay Abraham I <kishon@ti.com>
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. */
  18. #include <linux/module.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/err.h>
  21. #include <linux/pm_runtime.h>
  22. #include <linux/of.h>
  23. #include <linux/of_platform.h>
  24. #include <linux/platform_data/omap_ocp2scp.h>
  25. /**
  26. * _count_resources - count for the number of resources
  27. * @res: struct resource *
  28. *
  29. * Count and return the number of resources populated for the device that is
  30. * connected to ocp2scp.
  31. */
  32. static unsigned _count_resources(struct resource *res)
  33. {
  34. int cnt = 0;
  35. while (res->start != res->end) {
  36. cnt++;
  37. res++;
  38. }
  39. return cnt;
  40. }
  41. static int ocp2scp_remove_devices(struct device *dev, void *c)
  42. {
  43. struct platform_device *pdev = to_platform_device(dev);
  44. platform_device_unregister(pdev);
  45. return 0;
  46. }
  47. static int omap_ocp2scp_probe(struct platform_device *pdev)
  48. {
  49. int ret;
  50. unsigned res_cnt, i;
  51. struct device_node *np = pdev->dev.of_node;
  52. struct platform_device *pdev_child;
  53. struct omap_ocp2scp_platform_data *pdata = pdev->dev.platform_data;
  54. struct omap_ocp2scp_dev *dev;
  55. if (np) {
  56. ret = of_platform_populate(np, NULL, NULL, &pdev->dev);
  57. if (ret) {
  58. dev_err(&pdev->dev,
  59. "failed to add resources for ocp2scp child\n");
  60. goto err0;
  61. }
  62. } else if (pdata) {
  63. for (i = 0, dev = *pdata->devices; i < pdata->dev_cnt; i++,
  64. dev++) {
  65. res_cnt = _count_resources(dev->res);
  66. pdev_child = platform_device_alloc(dev->drv_name,
  67. PLATFORM_DEVID_AUTO);
  68. if (!pdev_child) {
  69. dev_err(&pdev->dev,
  70. "failed to allocate mem for ocp2scp child\n");
  71. goto err0;
  72. }
  73. ret = platform_device_add_resources(pdev_child,
  74. dev->res, res_cnt);
  75. if (ret) {
  76. dev_err(&pdev->dev,
  77. "failed to add resources for ocp2scp child\n");
  78. goto err1;
  79. }
  80. pdev_child->dev.parent = &pdev->dev;
  81. ret = platform_device_add(pdev_child);
  82. if (ret) {
  83. dev_err(&pdev->dev,
  84. "failed to register ocp2scp child device\n");
  85. goto err1;
  86. }
  87. }
  88. } else {
  89. dev_err(&pdev->dev, "OCP2SCP initialized without plat data\n");
  90. return -EINVAL;
  91. }
  92. pm_runtime_enable(&pdev->dev);
  93. return 0;
  94. err1:
  95. platform_device_put(pdev_child);
  96. err0:
  97. device_for_each_child(&pdev->dev, NULL, ocp2scp_remove_devices);
  98. return ret;
  99. }
  100. static int omap_ocp2scp_remove(struct platform_device *pdev)
  101. {
  102. pm_runtime_disable(&pdev->dev);
  103. device_for_each_child(&pdev->dev, NULL, ocp2scp_remove_devices);
  104. return 0;
  105. }
  106. #ifdef CONFIG_OF
  107. static const struct of_device_id omap_ocp2scp_id_table[] = {
  108. { .compatible = "ti,omap-ocp2scp" },
  109. {}
  110. };
  111. MODULE_DEVICE_TABLE(of, omap_ocp2scp_id_table);
  112. #endif
  113. static struct platform_driver omap_ocp2scp_driver = {
  114. .probe = omap_ocp2scp_probe,
  115. .remove = omap_ocp2scp_remove,
  116. .driver = {
  117. .name = "omap-ocp2scp",
  118. .owner = THIS_MODULE,
  119. .of_match_table = of_match_ptr(omap_ocp2scp_id_table),
  120. },
  121. };
  122. module_platform_driver(omap_ocp2scp_driver);
  123. MODULE_ALIAS("platform: omap-ocp2scp");
  124. MODULE_AUTHOR("Texas Instruments Inc.");
  125. MODULE_DESCRIPTION("OMAP OCP2SCP driver");
  126. MODULE_LICENSE("GPL v2");