devices.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Copyright 2008 Sascha Hauer, kernel@pengutronix.de
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor,
  16. * Boston, MA 02110-1301, USA.
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/slab.h>
  20. #include <linux/init.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/amba/bus.h>
  23. struct platform_device *__init mxs_add_platform_device_dmamask(
  24. const char *name, int id,
  25. const struct resource *res, unsigned int num_resources,
  26. const void *data, size_t size_data, u64 dmamask)
  27. {
  28. int ret = -ENOMEM;
  29. struct platform_device *pdev;
  30. pdev = platform_device_alloc(name, id);
  31. if (!pdev)
  32. goto err;
  33. if (dmamask) {
  34. /*
  35. * This memory isn't freed when the device is put,
  36. * I don't have a nice idea for that though. Conceptually
  37. * dma_mask in struct device should not be a pointer.
  38. * See http://thread.gmane.org/gmane.linux.kernel.pci/9081
  39. */
  40. pdev->dev.dma_mask =
  41. kmalloc(sizeof(*pdev->dev.dma_mask), GFP_KERNEL);
  42. if (!pdev->dev.dma_mask)
  43. /* ret is still -ENOMEM; */
  44. goto err;
  45. *pdev->dev.dma_mask = dmamask;
  46. pdev->dev.coherent_dma_mask = dmamask;
  47. }
  48. if (res) {
  49. ret = platform_device_add_resources(pdev, res, num_resources);
  50. if (ret)
  51. goto err;
  52. }
  53. if (data) {
  54. ret = platform_device_add_data(pdev, data, size_data);
  55. if (ret)
  56. goto err;
  57. }
  58. ret = platform_device_add(pdev);
  59. if (ret) {
  60. err:
  61. if (dmamask)
  62. kfree(pdev->dev.dma_mask);
  63. platform_device_put(pdev);
  64. return ERR_PTR(ret);
  65. }
  66. return pdev;
  67. }
  68. int __init mxs_add_amba_device(const struct amba_device *dev)
  69. {
  70. struct amba_device *adev = kmalloc(sizeof(*adev), GFP_KERNEL);
  71. if (!adev) {
  72. pr_err("%s: failed to allocate memory", __func__);
  73. return -ENOMEM;
  74. }
  75. *adev = *dev;
  76. return amba_device_register(adev, &iomem_resource);
  77. }