mfd-core.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * drivers/mfd/mfd-core.c
  3. *
  4. * core MFD support
  5. * Copyright (c) 2006 Ian Molton
  6. * Copyright (c) 2007,2008 Dmitry Baryshkov
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/acpi.h>
  16. #include <linux/mfd/core.h>
  17. static int mfd_add_device(struct device *parent, int id,
  18. const struct mfd_cell *cell,
  19. struct resource *mem_base,
  20. int irq_base)
  21. {
  22. struct resource *res;
  23. struct platform_device *pdev;
  24. int ret = -ENOMEM;
  25. int r;
  26. pdev = platform_device_alloc(cell->name, id + cell->id);
  27. if (!pdev)
  28. goto fail_alloc;
  29. res = kzalloc(sizeof(*res) * cell->num_resources, GFP_KERNEL);
  30. if (!res)
  31. goto fail_device;
  32. pdev->dev.parent = parent;
  33. platform_set_drvdata(pdev, cell->driver_data);
  34. ret = platform_device_add_data(pdev,
  35. cell->platform_data, cell->data_size);
  36. if (ret)
  37. goto fail_res;
  38. for (r = 0; r < cell->num_resources; r++) {
  39. res[r].name = cell->resources[r].name;
  40. res[r].flags = cell->resources[r].flags;
  41. /* Find out base to use */
  42. if (cell->resources[r].flags & IORESOURCE_MEM) {
  43. res[r].parent = mem_base;
  44. res[r].start = mem_base->start +
  45. cell->resources[r].start;
  46. res[r].end = mem_base->start +
  47. cell->resources[r].end;
  48. } else if (cell->resources[r].flags & IORESOURCE_IRQ) {
  49. res[r].start = irq_base +
  50. cell->resources[r].start;
  51. res[r].end = irq_base +
  52. cell->resources[r].end;
  53. } else {
  54. res[r].parent = cell->resources[r].parent;
  55. res[r].start = cell->resources[r].start;
  56. res[r].end = cell->resources[r].end;
  57. }
  58. ret = acpi_check_resource_conflict(res);
  59. if (ret)
  60. goto fail_res;
  61. }
  62. platform_device_add_resources(pdev, res, cell->num_resources);
  63. ret = platform_device_add(pdev);
  64. if (ret)
  65. goto fail_res;
  66. kfree(res);
  67. return 0;
  68. /* platform_device_del(pdev); */
  69. fail_res:
  70. kfree(res);
  71. fail_device:
  72. platform_device_put(pdev);
  73. fail_alloc:
  74. return ret;
  75. }
  76. int mfd_add_devices(struct device *parent, int id,
  77. const struct mfd_cell *cells, int n_devs,
  78. struct resource *mem_base,
  79. int irq_base)
  80. {
  81. int i;
  82. int ret = 0;
  83. for (i = 0; i < n_devs; i++) {
  84. ret = mfd_add_device(parent, id, cells + i, mem_base, irq_base);
  85. if (ret)
  86. break;
  87. }
  88. if (ret)
  89. mfd_remove_devices(parent);
  90. return ret;
  91. }
  92. EXPORT_SYMBOL(mfd_add_devices);
  93. static int mfd_remove_devices_fn(struct device *dev, void *unused)
  94. {
  95. platform_device_unregister(to_platform_device(dev));
  96. return 0;
  97. }
  98. void mfd_remove_devices(struct device *parent)
  99. {
  100. device_for_each_child(parent, NULL, mfd_remove_devices_fn);
  101. }
  102. EXPORT_SYMBOL(mfd_remove_devices);
  103. MODULE_LICENSE("GPL");
  104. MODULE_AUTHOR("Ian Molton, Dmitry Baryshkov");