mfd-core.c 9.9 KB

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