mfd-core.c 5.7 KB

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