mfd-core.c 6.1 KB

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