mfd-core.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. #include <linux/regulator/consumer.h>
  23. static struct device_type mfd_dev_type = {
  24. .name = "mfd_device",
  25. };
  26. int mfd_cell_enable(struct platform_device *pdev)
  27. {
  28. const struct mfd_cell *cell = mfd_get_cell(pdev);
  29. int err = 0;
  30. /* only call enable hook if the cell wasn't previously enabled */
  31. if (atomic_inc_return(cell->usage_count) == 1)
  32. err = cell->enable(pdev);
  33. /* if the enable hook failed, decrement counter to allow retries */
  34. if (err)
  35. atomic_dec(cell->usage_count);
  36. return err;
  37. }
  38. EXPORT_SYMBOL(mfd_cell_enable);
  39. int mfd_cell_disable(struct platform_device *pdev)
  40. {
  41. const struct mfd_cell *cell = mfd_get_cell(pdev);
  42. int err = 0;
  43. /* only disable if no other clients are using it */
  44. if (atomic_dec_return(cell->usage_count) == 0)
  45. err = cell->disable(pdev);
  46. /* if the disable hook failed, increment to allow retries */
  47. if (err)
  48. atomic_inc(cell->usage_count);
  49. /* sanity check; did someone call disable too many times? */
  50. WARN_ON(atomic_read(cell->usage_count) < 0);
  51. return err;
  52. }
  53. EXPORT_SYMBOL(mfd_cell_disable);
  54. static int mfd_platform_add_cell(struct platform_device *pdev,
  55. const struct mfd_cell *cell,
  56. atomic_t *usage_count)
  57. {
  58. if (!cell)
  59. return 0;
  60. pdev->mfd_cell = kmemdup(cell, sizeof(*cell), GFP_KERNEL);
  61. if (!pdev->mfd_cell)
  62. return -ENOMEM;
  63. pdev->mfd_cell->usage_count = usage_count;
  64. return 0;
  65. }
  66. #if IS_ENABLED(CONFIG_ACPI)
  67. static void mfd_acpi_add_device(const struct mfd_cell *cell,
  68. struct platform_device *pdev)
  69. {
  70. struct acpi_device *parent_adev;
  71. struct acpi_device *adev;
  72. parent_adev = ACPI_COMPANION(pdev->dev.parent);
  73. if (!parent_adev)
  74. return;
  75. /*
  76. * MFD child device gets its ACPI handle either from the ACPI
  77. * device directly under the parent that matches the acpi_pnpid or
  78. * it will use the parent handle if is no acpi_pnpid is given.
  79. */
  80. adev = parent_adev;
  81. if (cell->acpi_pnpid) {
  82. struct acpi_device_id ids[2] = {};
  83. struct acpi_device *child_adev;
  84. strlcpy(ids[0].id, cell->acpi_pnpid, sizeof(ids[0].id));
  85. list_for_each_entry(child_adev, &parent_adev->children, node)
  86. if (acpi_match_device_ids(child_adev, ids)) {
  87. adev = child_adev;
  88. break;
  89. }
  90. }
  91. ACPI_COMPANION_SET(&pdev->dev, adev);
  92. }
  93. #else
  94. static inline void mfd_acpi_add_device(const struct mfd_cell *cell,
  95. struct platform_device *pdev)
  96. {
  97. }
  98. #endif
  99. static int mfd_add_device(struct device *parent, int id,
  100. const struct mfd_cell *cell, atomic_t *usage_count,
  101. struct resource *mem_base,
  102. int irq_base, struct irq_domain *domain)
  103. {
  104. struct resource *res;
  105. struct platform_device *pdev;
  106. struct device_node *np = NULL;
  107. int ret = -ENOMEM;
  108. int platform_id;
  109. int r;
  110. if (id < 0)
  111. platform_id = id;
  112. else
  113. platform_id = id + cell->id;
  114. pdev = platform_device_alloc(cell->name, platform_id);
  115. if (!pdev)
  116. goto fail_alloc;
  117. res = kzalloc(sizeof(*res) * cell->num_resources, GFP_KERNEL);
  118. if (!res)
  119. goto fail_device;
  120. pdev->dev.parent = parent;
  121. pdev->dev.type = &mfd_dev_type;
  122. pdev->dev.dma_mask = parent->dma_mask;
  123. pdev->dev.dma_parms = parent->dma_parms;
  124. pdev->dev.coherent_dma_mask = parent->coherent_dma_mask;
  125. ret = regulator_bulk_register_supply_alias(
  126. &pdev->dev, cell->parent_supplies,
  127. parent, cell->parent_supplies,
  128. cell->num_parent_supplies);
  129. if (ret < 0)
  130. goto fail_res;
  131. if (parent->of_node && cell->of_compatible) {
  132. for_each_child_of_node(parent->of_node, np) {
  133. if (of_device_is_compatible(np, cell->of_compatible)) {
  134. pdev->dev.of_node = np;
  135. break;
  136. }
  137. }
  138. }
  139. mfd_acpi_add_device(cell, pdev);
  140. if (cell->pdata_size) {
  141. ret = platform_device_add_data(pdev,
  142. cell->platform_data, cell->pdata_size);
  143. if (ret)
  144. goto fail_alias;
  145. }
  146. ret = mfd_platform_add_cell(pdev, cell, usage_count);
  147. if (ret)
  148. goto fail_alias;
  149. for (r = 0; r < cell->num_resources; r++) {
  150. res[r].name = cell->resources[r].name;
  151. res[r].flags = cell->resources[r].flags;
  152. /* Find out base to use */
  153. if ((cell->resources[r].flags & IORESOURCE_MEM) && mem_base) {
  154. res[r].parent = mem_base;
  155. res[r].start = mem_base->start +
  156. cell->resources[r].start;
  157. res[r].end = mem_base->start +
  158. cell->resources[r].end;
  159. } else if (cell->resources[r].flags & IORESOURCE_IRQ) {
  160. if (domain) {
  161. /* Unable to create mappings for IRQ ranges. */
  162. WARN_ON(cell->resources[r].start !=
  163. cell->resources[r].end);
  164. res[r].start = res[r].end = irq_create_mapping(
  165. domain, cell->resources[r].start);
  166. } else {
  167. res[r].start = irq_base +
  168. cell->resources[r].start;
  169. res[r].end = irq_base +
  170. cell->resources[r].end;
  171. }
  172. } else {
  173. res[r].parent = cell->resources[r].parent;
  174. res[r].start = cell->resources[r].start;
  175. res[r].end = cell->resources[r].end;
  176. }
  177. if (!cell->ignore_resource_conflicts) {
  178. ret = acpi_check_resource_conflict(&res[r]);
  179. if (ret)
  180. goto fail_alias;
  181. }
  182. }
  183. ret = platform_device_add_resources(pdev, res, cell->num_resources);
  184. if (ret)
  185. goto fail_alias;
  186. ret = platform_device_add(pdev);
  187. if (ret)
  188. goto fail_alias;
  189. if (cell->pm_runtime_no_callbacks)
  190. pm_runtime_no_callbacks(&pdev->dev);
  191. kfree(res);
  192. return 0;
  193. fail_alias:
  194. regulator_bulk_unregister_supply_alias(&pdev->dev,
  195. cell->parent_supplies,
  196. cell->num_parent_supplies);
  197. fail_res:
  198. kfree(res);
  199. fail_device:
  200. platform_device_put(pdev);
  201. fail_alloc:
  202. return ret;
  203. }
  204. int mfd_add_devices(struct device *parent, int id,
  205. const struct mfd_cell *cells, int n_devs,
  206. struct resource *mem_base,
  207. int irq_base, struct irq_domain *domain)
  208. {
  209. int i;
  210. int ret;
  211. atomic_t *cnts;
  212. /* initialize reference counting for all cells */
  213. cnts = kcalloc(n_devs, sizeof(*cnts), GFP_KERNEL);
  214. if (!cnts)
  215. return -ENOMEM;
  216. for (i = 0; i < n_devs; i++) {
  217. atomic_set(&cnts[i], 0);
  218. ret = mfd_add_device(parent, id, cells + i, cnts + i, mem_base,
  219. irq_base, domain);
  220. if (ret)
  221. goto fail;
  222. }
  223. return 0;
  224. fail:
  225. if (i)
  226. mfd_remove_devices(parent);
  227. else
  228. kfree(cnts);
  229. return ret;
  230. }
  231. EXPORT_SYMBOL(mfd_add_devices);
  232. static int mfd_remove_devices_fn(struct device *dev, void *c)
  233. {
  234. struct platform_device *pdev;
  235. const struct mfd_cell *cell;
  236. atomic_t **usage_count = c;
  237. if (dev->type != &mfd_dev_type)
  238. return 0;
  239. pdev = to_platform_device(dev);
  240. cell = mfd_get_cell(pdev);
  241. regulator_bulk_unregister_supply_alias(dev, cell->parent_supplies,
  242. cell->num_parent_supplies);
  243. /* find the base address of usage_count pointers (for freeing) */
  244. if (!*usage_count || (cell->usage_count < *usage_count))
  245. *usage_count = cell->usage_count;
  246. platform_device_unregister(pdev);
  247. return 0;
  248. }
  249. void mfd_remove_devices(struct device *parent)
  250. {
  251. atomic_t *cnts = NULL;
  252. device_for_each_child(parent, &cnts, mfd_remove_devices_fn);
  253. kfree(cnts);
  254. }
  255. EXPORT_SYMBOL(mfd_remove_devices);
  256. int mfd_clone_cell(const char *cell, const char **clones, size_t n_clones)
  257. {
  258. struct mfd_cell cell_entry;
  259. struct device *dev;
  260. struct platform_device *pdev;
  261. int i;
  262. /* fetch the parent cell's device (should already be registered!) */
  263. dev = bus_find_device_by_name(&platform_bus_type, NULL, cell);
  264. if (!dev) {
  265. printk(KERN_ERR "failed to find device for cell %s\n", cell);
  266. return -ENODEV;
  267. }
  268. pdev = to_platform_device(dev);
  269. memcpy(&cell_entry, mfd_get_cell(pdev), sizeof(cell_entry));
  270. WARN_ON(!cell_entry.enable);
  271. for (i = 0; i < n_clones; i++) {
  272. cell_entry.name = clones[i];
  273. /* don't give up if a single call fails; just report error */
  274. if (mfd_add_device(pdev->dev.parent, -1, &cell_entry,
  275. cell_entry.usage_count, NULL, 0, NULL))
  276. dev_err(dev, "failed to create platform device '%s'\n",
  277. clones[i]);
  278. }
  279. return 0;
  280. }
  281. EXPORT_SYMBOL(mfd_clone_cell);
  282. MODULE_LICENSE("GPL");
  283. MODULE_AUTHOR("Ian Molton, Dmitry Baryshkov");