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/err.h>
  22. #include <linux/platform_device.h>
  23. #include <mach/common.h>
  24. int __init mxc_register_device(struct platform_device *pdev, void *data)
  25. {
  26. int ret;
  27. pdev->dev.platform_data = data;
  28. ret = platform_device_register(pdev);
  29. if (ret)
  30. pr_debug("Unable to register platform device '%s': %d\n",
  31. pdev->name, ret);
  32. return ret;
  33. }
  34. struct platform_device *__init imx_add_platform_device_dmamask(
  35. const char *name, int id,
  36. const struct resource *res, unsigned int num_resources,
  37. const void *data, size_t size_data, u64 dmamask)
  38. {
  39. int ret = -ENOMEM;
  40. struct platform_device *pdev;
  41. pdev = platform_device_alloc(name, id);
  42. if (!pdev)
  43. goto err;
  44. if (dmamask) {
  45. /*
  46. * This memory isn't freed when the device is put,
  47. * I don't have a nice idea for that though. Conceptually
  48. * dma_mask in struct device should not be a pointer.
  49. * See http://thread.gmane.org/gmane.linux.kernel.pci/9081
  50. */
  51. pdev->dev.dma_mask =
  52. kmalloc(sizeof(*pdev->dev.dma_mask), GFP_KERNEL);
  53. if (!pdev->dev.dma_mask)
  54. /* ret is still -ENOMEM; */
  55. goto err;
  56. *pdev->dev.dma_mask = dmamask;
  57. pdev->dev.coherent_dma_mask = dmamask;
  58. }
  59. if (res) {
  60. ret = platform_device_add_resources(pdev, res, num_resources);
  61. if (ret)
  62. goto err;
  63. }
  64. if (data) {
  65. ret = platform_device_add_data(pdev, data, size_data);
  66. if (ret)
  67. goto err;
  68. }
  69. ret = platform_device_add(pdev);
  70. if (ret) {
  71. err:
  72. if (dmamask)
  73. kfree(pdev->dev.dma_mask);
  74. platform_device_put(pdev);
  75. return ERR_PTR(ret);
  76. }
  77. return pdev;
  78. }