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