devices-common.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright (C) ST-Ericsson SA 2010
  3. *
  4. * Author: Rabin Vincent <rabin.vincent@stericsson.com> for ST-Ericsson
  5. * License terms: GNU General Public License (GPL), version 2.
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/dma-mapping.h>
  9. #include <linux/err.h>
  10. #include <linux/irq.h>
  11. #include <linux/slab.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/amba/bus.h>
  14. #include <plat/gpio-nomadik.h>
  15. #include <mach/hardware.h>
  16. #include "devices-common.h"
  17. struct amba_device *
  18. dbx500_add_amba_device(const char *name, resource_size_t base,
  19. int irq, void *pdata, unsigned int periphid)
  20. {
  21. struct amba_device *dev;
  22. int ret;
  23. dev = amba_device_alloc(name, base, SZ_4K);
  24. if (!dev)
  25. return ERR_PTR(-ENOMEM);
  26. dev->dma_mask = DMA_BIT_MASK(32);
  27. dev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
  28. dev->irq[0] = irq;
  29. dev->periphid = periphid;
  30. dev->dev.platform_data = pdata;
  31. ret = amba_device_add(dev, &iomem_resource);
  32. if (ret) {
  33. amba_device_put(dev);
  34. return ERR_PTR(ret);
  35. }
  36. return dev;
  37. }
  38. static struct platform_device *
  39. dbx500_add_platform_device(const char *name, int id, void *pdata,
  40. struct resource *res, int resnum)
  41. {
  42. struct platform_device *dev;
  43. int ret;
  44. dev = platform_device_alloc(name, id);
  45. if (!dev)
  46. return ERR_PTR(-ENOMEM);
  47. dev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
  48. dev->dev.dma_mask = &dev->dev.coherent_dma_mask;
  49. ret = platform_device_add_resources(dev, res, resnum);
  50. if (ret)
  51. goto out_free;
  52. dev->dev.platform_data = pdata;
  53. ret = platform_device_add(dev);
  54. if (ret)
  55. goto out_free;
  56. return dev;
  57. out_free:
  58. platform_device_put(dev);
  59. return ERR_PTR(ret);
  60. }
  61. struct platform_device *
  62. dbx500_add_platform_device_4k1irq(const char *name, int id,
  63. resource_size_t base,
  64. int irq, void *pdata)
  65. {
  66. struct resource resources[] = {
  67. [0] = {
  68. .start = base,
  69. .end = base + SZ_4K - 1,
  70. .flags = IORESOURCE_MEM,
  71. },
  72. [1] = {
  73. .start = irq,
  74. .end = irq,
  75. .flags = IORESOURCE_IRQ,
  76. }
  77. };
  78. return dbx500_add_platform_device(name, id, pdata, resources,
  79. ARRAY_SIZE(resources));
  80. }
  81. static struct platform_device *
  82. dbx500_add_gpio(int id, resource_size_t addr, int irq,
  83. struct nmk_gpio_platform_data *pdata)
  84. {
  85. struct resource resources[] = {
  86. {
  87. .start = addr,
  88. .end = addr + 127,
  89. .flags = IORESOURCE_MEM,
  90. },
  91. {
  92. .start = irq,
  93. .end = irq,
  94. .flags = IORESOURCE_IRQ,
  95. }
  96. };
  97. return platform_device_register_resndata(NULL, "gpio", id,
  98. resources, ARRAY_SIZE(resources),
  99. pdata, sizeof(*pdata));
  100. }
  101. void dbx500_add_gpios(resource_size_t *base, int num, int irq,
  102. struct nmk_gpio_platform_data *pdata)
  103. {
  104. int first = 0;
  105. int i;
  106. for (i = 0; i < num; i++, first += 32, irq++) {
  107. pdata->first_gpio = first;
  108. pdata->first_irq = NOMADIK_GPIO_TO_IRQ(first);
  109. pdata->num_gpio = 32;
  110. dbx500_add_gpio(i, base[i], irq, pdata);
  111. }
  112. }