mfd-core.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. #include <linux/pm_runtime.h>
  18. #include <linux/slab.h>
  19. static int mfd_add_device(struct device *parent, int id,
  20. const struct mfd_cell *cell,
  21. struct resource *mem_base,
  22. int irq_base)
  23. {
  24. struct resource *res;
  25. struct platform_device *pdev;
  26. int ret = -ENOMEM;
  27. int r;
  28. pdev = platform_device_alloc(cell->name, id + cell->id);
  29. if (!pdev)
  30. goto fail_alloc;
  31. res = kzalloc(sizeof(*res) * cell->num_resources, GFP_KERNEL);
  32. if (!res)
  33. goto fail_device;
  34. pdev->dev.parent = parent;
  35. platform_set_drvdata(pdev, cell->driver_data);
  36. if (cell->data_size) {
  37. ret = platform_device_add_data(pdev,
  38. cell->platform_data, cell->data_size);
  39. if (ret)
  40. goto fail_res;
  41. }
  42. for (r = 0; r < cell->num_resources; r++) {
  43. res[r].name = cell->resources[r].name;
  44. res[r].flags = cell->resources[r].flags;
  45. /* Find out base to use */
  46. if ((cell->resources[r].flags & IORESOURCE_MEM) && mem_base) {
  47. res[r].parent = mem_base;
  48. res[r].start = mem_base->start +
  49. cell->resources[r].start;
  50. res[r].end = mem_base->start +
  51. cell->resources[r].end;
  52. } else if (cell->resources[r].flags & IORESOURCE_IRQ) {
  53. res[r].start = irq_base +
  54. cell->resources[r].start;
  55. res[r].end = irq_base +
  56. cell->resources[r].end;
  57. } else {
  58. res[r].parent = cell->resources[r].parent;
  59. res[r].start = cell->resources[r].start;
  60. res[r].end = cell->resources[r].end;
  61. }
  62. if (!cell->ignore_resource_conflicts) {
  63. ret = acpi_check_resource_conflict(res);
  64. if (ret)
  65. goto fail_res;
  66. }
  67. }
  68. ret = platform_device_add_resources(pdev, res, cell->num_resources);
  69. if (ret)
  70. goto fail_res;
  71. ret = platform_device_add(pdev);
  72. if (ret)
  73. goto fail_res;
  74. if (cell->pm_runtime_no_callbacks)
  75. pm_runtime_no_callbacks(&pdev->dev);
  76. kfree(res);
  77. return 0;
  78. /* platform_device_del(pdev); */
  79. fail_res:
  80. kfree(res);
  81. fail_device:
  82. platform_device_put(pdev);
  83. fail_alloc:
  84. return ret;
  85. }
  86. int mfd_add_devices(struct device *parent, int id,
  87. const struct mfd_cell *cells, int n_devs,
  88. struct resource *mem_base,
  89. int irq_base)
  90. {
  91. int i;
  92. int ret = 0;
  93. for (i = 0; i < n_devs; i++) {
  94. ret = mfd_add_device(parent, id, cells + i, mem_base, irq_base);
  95. if (ret)
  96. break;
  97. }
  98. if (ret)
  99. mfd_remove_devices(parent);
  100. return ret;
  101. }
  102. EXPORT_SYMBOL(mfd_add_devices);
  103. static int mfd_remove_devices_fn(struct device *dev, void *unused)
  104. {
  105. platform_device_unregister(to_platform_device(dev));
  106. return 0;
  107. }
  108. void mfd_remove_devices(struct device *parent)
  109. {
  110. device_for_each_child(parent, NULL, mfd_remove_devices_fn);
  111. }
  112. EXPORT_SYMBOL(mfd_remove_devices);
  113. MODULE_LICENSE("GPL");
  114. MODULE_AUTHOR("Ian Molton, Dmitry Baryshkov");