mfd-core.c 6.3 KB

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