mfd-core.c 6.5 KB

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