mfd-core.c 5.5 KB

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