mfd-core.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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/property.h>
  17. #include <linux/mfd/core.h>
  18. #include <linux/pm_runtime.h>
  19. #include <linux/slab.h>
  20. #include <linux/module.h>
  21. #include <linux/irqdomain.h>
  22. #include <linux/of.h>
  23. #include <linux/regulator/consumer.h>
  24. static struct device_type mfd_dev_type = {
  25. .name = "mfd_device",
  26. };
  27. int mfd_cell_enable(struct platform_device *pdev)
  28. {
  29. const struct mfd_cell *cell = mfd_get_cell(pdev);
  30. int err = 0;
  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. /* only disable if no other clients are using it */
  45. if (atomic_dec_return(cell->usage_count) == 0)
  46. err = cell->disable(pdev);
  47. /* if the disable hook failed, increment to allow retries */
  48. if (err)
  49. atomic_inc(cell->usage_count);
  50. /* sanity check; did someone call disable too many times? */
  51. WARN_ON(atomic_read(cell->usage_count) < 0);
  52. return err;
  53. }
  54. EXPORT_SYMBOL(mfd_cell_disable);
  55. static int mfd_platform_add_cell(struct platform_device *pdev,
  56. const struct mfd_cell *cell,
  57. atomic_t *usage_count)
  58. {
  59. if (!cell)
  60. return 0;
  61. pdev->mfd_cell = kmemdup(cell, sizeof(*cell), GFP_KERNEL);
  62. if (!pdev->mfd_cell)
  63. return -ENOMEM;
  64. pdev->mfd_cell->usage_count = usage_count;
  65. return 0;
  66. }
  67. #if IS_ENABLED(CONFIG_ACPI)
  68. static void mfd_acpi_add_device(const struct mfd_cell *cell,
  69. struct platform_device *pdev)
  70. {
  71. const struct mfd_cell_acpi_match *match = cell->acpi_match;
  72. struct acpi_device *parent, *child;
  73. struct acpi_device *adev;
  74. parent = ACPI_COMPANION(pdev->dev.parent);
  75. if (!parent)
  76. return;
  77. /*
  78. * MFD child device gets its ACPI handle either from the ACPI device
  79. * directly under the parent that matches the either _HID or _CID, or
  80. * _ADR or it will use the parent handle if is no ID is given.
  81. *
  82. * Note that use of _ADR is a grey area in the ACPI specification,
  83. * though Intel Galileo Gen2 is using it to distinguish the children
  84. * devices.
  85. */
  86. adev = parent;
  87. if (match) {
  88. if (match->pnpid) {
  89. struct acpi_device_id ids[2] = {};
  90. strlcpy(ids[0].id, match->pnpid, sizeof(ids[0].id));
  91. list_for_each_entry(child, &parent->children, node) {
  92. if (!acpi_match_device_ids(child, ids)) {
  93. adev = child;
  94. break;
  95. }
  96. }
  97. } else {
  98. unsigned long long adr;
  99. acpi_status status;
  100. list_for_each_entry(child, &parent->children, node) {
  101. status = acpi_evaluate_integer(child->handle,
  102. "_ADR", NULL,
  103. &adr);
  104. if (ACPI_SUCCESS(status) && match->adr == adr) {
  105. adev = child;
  106. break;
  107. }
  108. }
  109. }
  110. }
  111. ACPI_COMPANION_SET(&pdev->dev, adev);
  112. }
  113. #else
  114. static inline void mfd_acpi_add_device(const struct mfd_cell *cell,
  115. struct platform_device *pdev)
  116. {
  117. }
  118. #endif
  119. static int mfd_add_device(struct device *parent, int id,
  120. const struct mfd_cell *cell, atomic_t *usage_count,
  121. struct resource *mem_base,
  122. int irq_base, struct irq_domain *domain)
  123. {
  124. struct resource *res;
  125. struct platform_device *pdev;
  126. struct device_node *np = NULL;
  127. int ret = -ENOMEM;
  128. int platform_id;
  129. int r;
  130. if (id == PLATFORM_DEVID_AUTO)
  131. platform_id = id;
  132. else
  133. platform_id = id + cell->id;
  134. pdev = platform_device_alloc(cell->name, platform_id);
  135. if (!pdev)
  136. goto fail_alloc;
  137. res = kzalloc(sizeof(*res) * cell->num_resources, GFP_KERNEL);
  138. if (!res)
  139. goto fail_device;
  140. pdev->dev.parent = parent;
  141. pdev->dev.type = &mfd_dev_type;
  142. pdev->dev.dma_mask = parent->dma_mask;
  143. pdev->dev.dma_parms = parent->dma_parms;
  144. pdev->dev.coherent_dma_mask = parent->coherent_dma_mask;
  145. ret = regulator_bulk_register_supply_alias(
  146. &pdev->dev, cell->parent_supplies,
  147. parent, cell->parent_supplies,
  148. cell->num_parent_supplies);
  149. if (ret < 0)
  150. goto fail_res;
  151. if (parent->of_node && cell->of_compatible) {
  152. for_each_child_of_node(parent->of_node, np) {
  153. if (of_device_is_compatible(np, cell->of_compatible)) {
  154. pdev->dev.of_node = np;
  155. break;
  156. }
  157. }
  158. }
  159. mfd_acpi_add_device(cell, pdev);
  160. if (cell->pdata_size) {
  161. ret = platform_device_add_data(pdev,
  162. cell->platform_data, cell->pdata_size);
  163. if (ret)
  164. goto fail_alias;
  165. }
  166. if (cell->properties) {
  167. ret = platform_device_add_properties(pdev, cell->properties);
  168. if (ret)
  169. goto fail_alias;
  170. }
  171. ret = mfd_platform_add_cell(pdev, cell, usage_count);
  172. if (ret)
  173. goto fail_alias;
  174. for (r = 0; r < cell->num_resources; r++) {
  175. res[r].name = cell->resources[r].name;
  176. res[r].flags = cell->resources[r].flags;
  177. /* Find out base to use */
  178. if ((cell->resources[r].flags & IORESOURCE_MEM) && mem_base) {
  179. res[r].parent = mem_base;
  180. res[r].start = mem_base->start +
  181. cell->resources[r].start;
  182. res[r].end = mem_base->start +
  183. cell->resources[r].end;
  184. } else if (cell->resources[r].flags & IORESOURCE_IRQ) {
  185. if (domain) {
  186. /* Unable to create mappings for IRQ ranges. */
  187. WARN_ON(cell->resources[r].start !=
  188. cell->resources[r].end);
  189. res[r].start = res[r].end = irq_create_mapping(
  190. domain, cell->resources[r].start);
  191. } else {
  192. res[r].start = irq_base +
  193. cell->resources[r].start;
  194. res[r].end = irq_base +
  195. cell->resources[r].end;
  196. }
  197. } else {
  198. res[r].parent = cell->resources[r].parent;
  199. res[r].start = cell->resources[r].start;
  200. res[r].end = cell->resources[r].end;
  201. }
  202. if (!cell->ignore_resource_conflicts) {
  203. if (has_acpi_companion(&pdev->dev)) {
  204. ret = acpi_check_resource_conflict(&res[r]);
  205. if (ret)
  206. goto fail_alias;
  207. }
  208. }
  209. }
  210. ret = platform_device_add_resources(pdev, res, cell->num_resources);
  211. if (ret)
  212. goto fail_alias;
  213. ret = platform_device_add(pdev);
  214. if (ret)
  215. goto fail_alias;
  216. if (cell->pm_runtime_no_callbacks)
  217. pm_runtime_no_callbacks(&pdev->dev);
  218. kfree(res);
  219. return 0;
  220. fail_alias:
  221. regulator_bulk_unregister_supply_alias(&pdev->dev,
  222. cell->parent_supplies,
  223. cell->num_parent_supplies);
  224. fail_res:
  225. kfree(res);
  226. fail_device:
  227. platform_device_put(pdev);
  228. fail_alloc:
  229. return ret;
  230. }
  231. int mfd_add_devices(struct device *parent, int id,
  232. const struct mfd_cell *cells, int n_devs,
  233. struct resource *mem_base,
  234. int irq_base, struct irq_domain *domain)
  235. {
  236. int i;
  237. int ret;
  238. atomic_t *cnts;
  239. /* initialize reference counting for all cells */
  240. cnts = kcalloc(n_devs, sizeof(*cnts), GFP_KERNEL);
  241. if (!cnts)
  242. return -ENOMEM;
  243. for (i = 0; i < n_devs; i++) {
  244. atomic_set(&cnts[i], 0);
  245. ret = mfd_add_device(parent, id, cells + i, cnts + i, mem_base,
  246. irq_base, domain);
  247. if (ret)
  248. goto fail;
  249. }
  250. return 0;
  251. fail:
  252. if (i)
  253. mfd_remove_devices(parent);
  254. else
  255. kfree(cnts);
  256. return ret;
  257. }
  258. EXPORT_SYMBOL(mfd_add_devices);
  259. static int mfd_remove_devices_fn(struct device *dev, void *c)
  260. {
  261. struct platform_device *pdev;
  262. const struct mfd_cell *cell;
  263. atomic_t **usage_count = c;
  264. if (dev->type != &mfd_dev_type)
  265. return 0;
  266. pdev = to_platform_device(dev);
  267. cell = mfd_get_cell(pdev);
  268. regulator_bulk_unregister_supply_alias(dev, cell->parent_supplies,
  269. cell->num_parent_supplies);
  270. /* find the base address of usage_count pointers (for freeing) */
  271. if (!*usage_count || (cell->usage_count < *usage_count))
  272. *usage_count = cell->usage_count;
  273. platform_device_unregister(pdev);
  274. return 0;
  275. }
  276. void mfd_remove_devices(struct device *parent)
  277. {
  278. atomic_t *cnts = NULL;
  279. device_for_each_child_reverse(parent, &cnts, mfd_remove_devices_fn);
  280. kfree(cnts);
  281. }
  282. EXPORT_SYMBOL(mfd_remove_devices);
  283. static void devm_mfd_dev_release(struct device *dev, void *res)
  284. {
  285. mfd_remove_devices(dev);
  286. }
  287. /**
  288. * devm_mfd_add_devices - Resource managed version of mfd_add_devices()
  289. *
  290. * Returns 0 on success or an appropriate negative error number on failure.
  291. * All child-devices of the MFD will automatically be removed when it gets
  292. * unbinded.
  293. */
  294. int devm_mfd_add_devices(struct device *dev, int id,
  295. const struct mfd_cell *cells, int n_devs,
  296. struct resource *mem_base,
  297. int irq_base, struct irq_domain *domain)
  298. {
  299. struct device **ptr;
  300. int ret;
  301. ptr = devres_alloc(devm_mfd_dev_release, sizeof(*ptr), GFP_KERNEL);
  302. if (!ptr)
  303. return -ENOMEM;
  304. ret = mfd_add_devices(dev, id, cells, n_devs, mem_base,
  305. irq_base, domain);
  306. if (ret < 0) {
  307. devres_free(ptr);
  308. return ret;
  309. }
  310. *ptr = dev;
  311. devres_add(dev, ptr);
  312. return ret;
  313. }
  314. EXPORT_SYMBOL(devm_mfd_add_devices);
  315. int mfd_clone_cell(const char *cell, const char **clones, size_t n_clones)
  316. {
  317. struct mfd_cell cell_entry;
  318. struct device *dev;
  319. struct platform_device *pdev;
  320. int i;
  321. /* fetch the parent cell's device (should already be registered!) */
  322. dev = bus_find_device_by_name(&platform_bus_type, NULL, cell);
  323. if (!dev) {
  324. printk(KERN_ERR "failed to find device for cell %s\n", cell);
  325. return -ENODEV;
  326. }
  327. pdev = to_platform_device(dev);
  328. memcpy(&cell_entry, mfd_get_cell(pdev), sizeof(cell_entry));
  329. WARN_ON(!cell_entry.enable);
  330. for (i = 0; i < n_clones; i++) {
  331. cell_entry.name = clones[i];
  332. /* don't give up if a single call fails; just report error */
  333. if (mfd_add_device(pdev->dev.parent, -1, &cell_entry,
  334. cell_entry.usage_count, NULL, 0, NULL))
  335. dev_err(dev, "failed to create platform device '%s'\n",
  336. clones[i]);
  337. }
  338. put_device(dev);
  339. return 0;
  340. }
  341. EXPORT_SYMBOL(mfd_clone_cell);
  342. MODULE_LICENSE("GPL");
  343. MODULE_AUTHOR("Ian Molton, Dmitry Baryshkov");