mfd-core.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. #include <linux/module.h>
  20. int mfd_cell_enable(struct platform_device *pdev)
  21. {
  22. const struct mfd_cell *cell = mfd_get_cell(pdev);
  23. int err = 0;
  24. /* only call enable hook if the cell wasn't previously enabled */
  25. if (atomic_inc_return(cell->usage_count) == 1)
  26. err = cell->enable(pdev);
  27. /* if the enable hook failed, decrement counter to allow retries */
  28. if (err)
  29. atomic_dec(cell->usage_count);
  30. return err;
  31. }
  32. EXPORT_SYMBOL(mfd_cell_enable);
  33. int mfd_cell_disable(struct platform_device *pdev)
  34. {
  35. const struct mfd_cell *cell = mfd_get_cell(pdev);
  36. int err = 0;
  37. /* only disable if no other clients are using it */
  38. if (atomic_dec_return(cell->usage_count) == 0)
  39. err = cell->disable(pdev);
  40. /* if the disable hook failed, increment to allow retries */
  41. if (err)
  42. atomic_inc(cell->usage_count);
  43. /* sanity check; did someone call disable too many times? */
  44. WARN_ON(atomic_read(cell->usage_count) < 0);
  45. return err;
  46. }
  47. EXPORT_SYMBOL(mfd_cell_disable);
  48. static int mfd_platform_add_cell(struct platform_device *pdev,
  49. const struct mfd_cell *cell)
  50. {
  51. if (!cell)
  52. return 0;
  53. pdev->mfd_cell = kmemdup(cell, sizeof(*cell), GFP_KERNEL);
  54. if (!pdev->mfd_cell)
  55. return -ENOMEM;
  56. return 0;
  57. }
  58. static int mfd_add_device(struct device *parent, int id,
  59. const struct mfd_cell *cell,
  60. struct resource *mem_base,
  61. int irq_base)
  62. {
  63. struct resource *res;
  64. struct platform_device *pdev;
  65. int ret = -ENOMEM;
  66. int r;
  67. pdev = platform_device_alloc(cell->name, id + cell->id);
  68. if (!pdev)
  69. goto fail_alloc;
  70. res = kzalloc(sizeof(*res) * cell->num_resources, GFP_KERNEL);
  71. if (!res)
  72. goto fail_device;
  73. pdev->dev.parent = parent;
  74. if (cell->pdata_size) {
  75. ret = platform_device_add_data(pdev,
  76. cell->platform_data, cell->pdata_size);
  77. if (ret)
  78. goto fail_res;
  79. }
  80. ret = mfd_platform_add_cell(pdev, cell);
  81. if (ret)
  82. goto fail_res;
  83. for (r = 0; r < cell->num_resources; r++) {
  84. res[r].name = cell->resources[r].name;
  85. res[r].flags = cell->resources[r].flags;
  86. /* Find out base to use */
  87. if ((cell->resources[r].flags & IORESOURCE_MEM) && mem_base) {
  88. res[r].parent = mem_base;
  89. res[r].start = mem_base->start +
  90. cell->resources[r].start;
  91. res[r].end = mem_base->start +
  92. cell->resources[r].end;
  93. } else if (cell->resources[r].flags & IORESOURCE_IRQ) {
  94. res[r].start = irq_base +
  95. cell->resources[r].start;
  96. res[r].end = irq_base +
  97. cell->resources[r].end;
  98. } else {
  99. res[r].parent = cell->resources[r].parent;
  100. res[r].start = cell->resources[r].start;
  101. res[r].end = cell->resources[r].end;
  102. }
  103. if (!cell->ignore_resource_conflicts) {
  104. ret = acpi_check_resource_conflict(res);
  105. if (ret)
  106. goto fail_res;
  107. }
  108. }
  109. ret = platform_device_add_resources(pdev, res, cell->num_resources);
  110. if (ret)
  111. goto fail_res;
  112. ret = platform_device_add(pdev);
  113. if (ret)
  114. goto fail_res;
  115. if (cell->pm_runtime_no_callbacks)
  116. pm_runtime_no_callbacks(&pdev->dev);
  117. kfree(res);
  118. return 0;
  119. fail_res:
  120. kfree(res);
  121. fail_device:
  122. platform_device_put(pdev);
  123. fail_alloc:
  124. return ret;
  125. }
  126. int mfd_add_devices(struct device *parent, int id,
  127. struct mfd_cell *cells, int n_devs,
  128. struct resource *mem_base,
  129. int irq_base)
  130. {
  131. int i;
  132. int ret = 0;
  133. atomic_t *cnts;
  134. /* initialize reference counting for all cells */
  135. cnts = kcalloc(sizeof(*cnts), n_devs, GFP_KERNEL);
  136. if (!cnts)
  137. return -ENOMEM;
  138. for (i = 0; i < n_devs; i++) {
  139. atomic_set(&cnts[i], 0);
  140. cells[i].usage_count = &cnts[i];
  141. ret = mfd_add_device(parent, id, cells + i, mem_base, irq_base);
  142. if (ret)
  143. break;
  144. }
  145. if (ret)
  146. mfd_remove_devices(parent);
  147. return ret;
  148. }
  149. EXPORT_SYMBOL(mfd_add_devices);
  150. static int mfd_remove_devices_fn(struct device *dev, void *c)
  151. {
  152. struct platform_device *pdev = to_platform_device(dev);
  153. const struct mfd_cell *cell = mfd_get_cell(pdev);
  154. atomic_t **usage_count = c;
  155. /* find the base address of usage_count pointers (for freeing) */
  156. if (!*usage_count || (cell->usage_count < *usage_count))
  157. *usage_count = cell->usage_count;
  158. platform_device_unregister(pdev);
  159. return 0;
  160. }
  161. void mfd_remove_devices(struct device *parent)
  162. {
  163. atomic_t *cnts = NULL;
  164. device_for_each_child(parent, &cnts, mfd_remove_devices_fn);
  165. kfree(cnts);
  166. }
  167. EXPORT_SYMBOL(mfd_remove_devices);
  168. int mfd_clone_cell(const char *cell, const char **clones, size_t n_clones)
  169. {
  170. struct mfd_cell cell_entry;
  171. struct device *dev;
  172. struct platform_device *pdev;
  173. int i;
  174. /* fetch the parent cell's device (should already be registered!) */
  175. dev = bus_find_device_by_name(&platform_bus_type, NULL, cell);
  176. if (!dev) {
  177. printk(KERN_ERR "failed to find device for cell %s\n", cell);
  178. return -ENODEV;
  179. }
  180. pdev = to_platform_device(dev);
  181. memcpy(&cell_entry, mfd_get_cell(pdev), sizeof(cell_entry));
  182. WARN_ON(!cell_entry.enable);
  183. for (i = 0; i < n_clones; i++) {
  184. cell_entry.name = clones[i];
  185. /* don't give up if a single call fails; just report error */
  186. if (mfd_add_device(pdev->dev.parent, -1, &cell_entry, NULL, 0))
  187. dev_err(dev, "failed to create platform device '%s'\n",
  188. clones[i]);
  189. }
  190. return 0;
  191. }
  192. EXPORT_SYMBOL(mfd_clone_cell);
  193. MODULE_LICENSE("GPL");
  194. MODULE_AUTHOR("Ian Molton, Dmitry Baryshkov");