mfd-core.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. const struct mfd_cell_acpi_match *match = cell->acpi_match;
  71. struct acpi_device *parent, *child;
  72. struct acpi_device *adev;
  73. parent = ACPI_COMPANION(pdev->dev.parent);
  74. if (!parent)
  75. return;
  76. /*
  77. * MFD child device gets its ACPI handle either from the ACPI device
  78. * directly under the parent that matches the either _HID or _CID, or
  79. * _ADR or it will use the parent handle if is no ID is given.
  80. *
  81. * Note that use of _ADR is a grey area in the ACPI specification,
  82. * though Intel Galileo Gen2 is using it to distinguish the children
  83. * devices.
  84. */
  85. adev = parent;
  86. if (match) {
  87. if (match->pnpid) {
  88. struct acpi_device_id ids[2] = {};
  89. strlcpy(ids[0].id, match->pnpid, sizeof(ids[0].id));
  90. list_for_each_entry(child, &parent->children, node) {
  91. if (acpi_match_device_ids(child, ids)) {
  92. adev = child;
  93. break;
  94. }
  95. }
  96. } else {
  97. unsigned long long adr;
  98. acpi_status status;
  99. list_for_each_entry(child, &parent->children, node) {
  100. status = acpi_evaluate_integer(child->handle,
  101. "_ADR", NULL,
  102. &adr);
  103. if (ACPI_SUCCESS(status) && match->adr == adr) {
  104. adev = child;
  105. break;
  106. }
  107. }
  108. }
  109. }
  110. ACPI_COMPANION_SET(&pdev->dev, adev);
  111. }
  112. #else
  113. static inline void mfd_acpi_add_device(const struct mfd_cell *cell,
  114. struct platform_device *pdev)
  115. {
  116. }
  117. #endif
  118. static int mfd_add_device(struct device *parent, int id,
  119. const struct mfd_cell *cell, atomic_t *usage_count,
  120. struct resource *mem_base,
  121. int irq_base, struct irq_domain *domain)
  122. {
  123. struct resource *res;
  124. struct platform_device *pdev;
  125. struct device_node *np = NULL;
  126. int ret = -ENOMEM;
  127. int platform_id;
  128. int r;
  129. if (id == PLATFORM_DEVID_AUTO)
  130. platform_id = id;
  131. else
  132. platform_id = id + cell->id;
  133. pdev = platform_device_alloc(cell->name, platform_id);
  134. if (!pdev)
  135. goto fail_alloc;
  136. res = kzalloc(sizeof(*res) * cell->num_resources, GFP_KERNEL);
  137. if (!res)
  138. goto fail_device;
  139. pdev->dev.parent = parent;
  140. pdev->dev.type = &mfd_dev_type;
  141. pdev->dev.dma_mask = parent->dma_mask;
  142. pdev->dev.dma_parms = parent->dma_parms;
  143. pdev->dev.coherent_dma_mask = parent->coherent_dma_mask;
  144. ret = regulator_bulk_register_supply_alias(
  145. &pdev->dev, cell->parent_supplies,
  146. parent, cell->parent_supplies,
  147. cell->num_parent_supplies);
  148. if (ret < 0)
  149. goto fail_res;
  150. if (parent->of_node && cell->of_compatible) {
  151. for_each_child_of_node(parent->of_node, np) {
  152. if (of_device_is_compatible(np, cell->of_compatible)) {
  153. pdev->dev.of_node = np;
  154. break;
  155. }
  156. }
  157. }
  158. mfd_acpi_add_device(cell, pdev);
  159. if (cell->pdata_size) {
  160. ret = platform_device_add_data(pdev,
  161. cell->platform_data, cell->pdata_size);
  162. if (ret)
  163. goto fail_alias;
  164. }
  165. ret = mfd_platform_add_cell(pdev, cell, usage_count);
  166. if (ret)
  167. goto fail_alias;
  168. for (r = 0; r < cell->num_resources; r++) {
  169. res[r].name = cell->resources[r].name;
  170. res[r].flags = cell->resources[r].flags;
  171. /* Find out base to use */
  172. if ((cell->resources[r].flags & IORESOURCE_MEM) && mem_base) {
  173. res[r].parent = mem_base;
  174. res[r].start = mem_base->start +
  175. cell->resources[r].start;
  176. res[r].end = mem_base->start +
  177. cell->resources[r].end;
  178. } else if (cell->resources[r].flags & IORESOURCE_IRQ) {
  179. if (domain) {
  180. /* Unable to create mappings for IRQ ranges. */
  181. WARN_ON(cell->resources[r].start !=
  182. cell->resources[r].end);
  183. res[r].start = res[r].end = irq_create_mapping(
  184. domain, cell->resources[r].start);
  185. } else {
  186. res[r].start = irq_base +
  187. cell->resources[r].start;
  188. res[r].end = irq_base +
  189. cell->resources[r].end;
  190. }
  191. } else {
  192. res[r].parent = cell->resources[r].parent;
  193. res[r].start = cell->resources[r].start;
  194. res[r].end = cell->resources[r].end;
  195. }
  196. if (!cell->ignore_resource_conflicts) {
  197. if (has_acpi_companion(&pdev->dev)) {
  198. ret = acpi_check_resource_conflict(&res[r]);
  199. if (ret)
  200. goto fail_alias;
  201. }
  202. }
  203. }
  204. ret = platform_device_add_resources(pdev, res, cell->num_resources);
  205. if (ret)
  206. goto fail_alias;
  207. ret = platform_device_add(pdev);
  208. if (ret)
  209. goto fail_alias;
  210. if (cell->pm_runtime_no_callbacks)
  211. pm_runtime_no_callbacks(&pdev->dev);
  212. kfree(res);
  213. return 0;
  214. fail_alias:
  215. regulator_bulk_unregister_supply_alias(&pdev->dev,
  216. cell->parent_supplies,
  217. cell->num_parent_supplies);
  218. fail_res:
  219. kfree(res);
  220. fail_device:
  221. platform_device_put(pdev);
  222. fail_alloc:
  223. return ret;
  224. }
  225. int mfd_add_devices(struct device *parent, int id,
  226. const struct mfd_cell *cells, int n_devs,
  227. struct resource *mem_base,
  228. int irq_base, struct irq_domain *domain)
  229. {
  230. int i;
  231. int ret;
  232. atomic_t *cnts;
  233. /* initialize reference counting for all cells */
  234. cnts = kcalloc(n_devs, sizeof(*cnts), GFP_KERNEL);
  235. if (!cnts)
  236. return -ENOMEM;
  237. for (i = 0; i < n_devs; i++) {
  238. atomic_set(&cnts[i], 0);
  239. ret = mfd_add_device(parent, id, cells + i, cnts + i, mem_base,
  240. irq_base, domain);
  241. if (ret)
  242. goto fail;
  243. }
  244. return 0;
  245. fail:
  246. if (i)
  247. mfd_remove_devices(parent);
  248. else
  249. kfree(cnts);
  250. return ret;
  251. }
  252. EXPORT_SYMBOL(mfd_add_devices);
  253. static int mfd_remove_devices_fn(struct device *dev, void *c)
  254. {
  255. struct platform_device *pdev;
  256. const struct mfd_cell *cell;
  257. atomic_t **usage_count = c;
  258. if (dev->type != &mfd_dev_type)
  259. return 0;
  260. pdev = to_platform_device(dev);
  261. cell = mfd_get_cell(pdev);
  262. regulator_bulk_unregister_supply_alias(dev, cell->parent_supplies,
  263. cell->num_parent_supplies);
  264. /* find the base address of usage_count pointers (for freeing) */
  265. if (!*usage_count || (cell->usage_count < *usage_count))
  266. *usage_count = cell->usage_count;
  267. platform_device_unregister(pdev);
  268. return 0;
  269. }
  270. void mfd_remove_devices(struct device *parent)
  271. {
  272. atomic_t *cnts = NULL;
  273. device_for_each_child_reverse(parent, &cnts, mfd_remove_devices_fn);
  274. kfree(cnts);
  275. }
  276. EXPORT_SYMBOL(mfd_remove_devices);
  277. int mfd_clone_cell(const char *cell, const char **clones, size_t n_clones)
  278. {
  279. struct mfd_cell cell_entry;
  280. struct device *dev;
  281. struct platform_device *pdev;
  282. int i;
  283. /* fetch the parent cell's device (should already be registered!) */
  284. dev = bus_find_device_by_name(&platform_bus_type, NULL, cell);
  285. if (!dev) {
  286. printk(KERN_ERR "failed to find device for cell %s\n", cell);
  287. return -ENODEV;
  288. }
  289. pdev = to_platform_device(dev);
  290. memcpy(&cell_entry, mfd_get_cell(pdev), sizeof(cell_entry));
  291. WARN_ON(!cell_entry.enable);
  292. for (i = 0; i < n_clones; i++) {
  293. cell_entry.name = clones[i];
  294. /* don't give up if a single call fails; just report error */
  295. if (mfd_add_device(pdev->dev.parent, -1, &cell_entry,
  296. cell_entry.usage_count, NULL, 0, NULL))
  297. dev_err(dev, "failed to create platform device '%s'\n",
  298. clones[i]);
  299. }
  300. put_device(dev);
  301. return 0;
  302. }
  303. EXPORT_SYMBOL(mfd_clone_cell);
  304. MODULE_LICENSE("GPL");
  305. MODULE_AUTHOR("Ian Molton, Dmitry Baryshkov");