xref: /openbmc/linux/drivers/mfd/mfd-core.c (revision eb895607)
1aa613de6SDmitry Baryshkov /*
2aa613de6SDmitry Baryshkov  * drivers/mfd/mfd-core.c
3aa613de6SDmitry Baryshkov  *
4aa613de6SDmitry Baryshkov  * core MFD support
5aa613de6SDmitry Baryshkov  * Copyright (c) 2006 Ian Molton
6aa613de6SDmitry Baryshkov  * Copyright (c) 2007,2008 Dmitry Baryshkov
7aa613de6SDmitry Baryshkov  *
8aa613de6SDmitry Baryshkov  * This program is free software; you can redistribute it and/or modify
9aa613de6SDmitry Baryshkov  * it under the terms of the GNU General Public License version 2 as
10aa613de6SDmitry Baryshkov  * published by the Free Software Foundation.
11aa613de6SDmitry Baryshkov  *
12aa613de6SDmitry Baryshkov  */
13aa613de6SDmitry Baryshkov 
14aa613de6SDmitry Baryshkov #include <linux/kernel.h>
15aa613de6SDmitry Baryshkov #include <linux/platform_device.h>
1691fededeSSamuel Ortiz #include <linux/acpi.h>
17aa613de6SDmitry Baryshkov #include <linux/mfd/core.h>
184c90aa94SMark Brown #include <linux/pm_runtime.h>
195a0e3ad6STejun Heo #include <linux/slab.h>
20aa613de6SDmitry Baryshkov 
21f77289acSAndres Salomon int mfd_cell_enable(struct platform_device *pdev)
221e29af62SAndres Salomon {
231e29af62SAndres Salomon 	const struct mfd_cell *cell = mfd_get_cell(pdev);
241e29af62SAndres Salomon 	int err = 0;
251e29af62SAndres Salomon 
261e29af62SAndres Salomon 	/* only call enable hook if the cell wasn't previously enabled */
271e29af62SAndres Salomon 	if (atomic_inc_return(cell->usage_count) == 1)
281e29af62SAndres Salomon 		err = cell->enable(pdev);
291e29af62SAndres Salomon 
301e29af62SAndres Salomon 	/* if the enable hook failed, decrement counter to allow retries */
311e29af62SAndres Salomon 	if (err)
321e29af62SAndres Salomon 		atomic_dec(cell->usage_count);
331e29af62SAndres Salomon 
341e29af62SAndres Salomon 	return err;
351e29af62SAndres Salomon }
36f77289acSAndres Salomon EXPORT_SYMBOL(mfd_cell_enable);
371e29af62SAndres Salomon 
38f77289acSAndres Salomon int mfd_cell_disable(struct platform_device *pdev)
391e29af62SAndres Salomon {
401e29af62SAndres Salomon 	const struct mfd_cell *cell = mfd_get_cell(pdev);
411e29af62SAndres Salomon 	int err = 0;
421e29af62SAndres Salomon 
431e29af62SAndres Salomon 	/* only disable if no other clients are using it */
441e29af62SAndres Salomon 	if (atomic_dec_return(cell->usage_count) == 0)
451e29af62SAndres Salomon 		err = cell->disable(pdev);
461e29af62SAndres Salomon 
471e29af62SAndres Salomon 	/* if the disable hook failed, increment to allow retries */
481e29af62SAndres Salomon 	if (err)
491e29af62SAndres Salomon 		atomic_inc(cell->usage_count);
501e29af62SAndres Salomon 
511e29af62SAndres Salomon 	/* sanity check; did someone call disable too many times? */
521e29af62SAndres Salomon 	WARN_ON(atomic_read(cell->usage_count) < 0);
531e29af62SAndres Salomon 
541e29af62SAndres Salomon 	return err;
551e29af62SAndres Salomon }
56f77289acSAndres Salomon EXPORT_SYMBOL(mfd_cell_disable);
571e29af62SAndres Salomon 
58e710d7d5SSamuel Ortiz static int mfd_platform_add_cell(struct platform_device *pdev,
59e710d7d5SSamuel Ortiz 				 const struct mfd_cell *cell)
60e710d7d5SSamuel Ortiz {
61e710d7d5SSamuel Ortiz 	if (!cell)
62e710d7d5SSamuel Ortiz 		return 0;
63e710d7d5SSamuel Ortiz 
64e710d7d5SSamuel Ortiz 	pdev->mfd_cell = kmemdup(cell, sizeof(*cell), GFP_KERNEL);
65e710d7d5SSamuel Ortiz 	if (!pdev->mfd_cell)
66e710d7d5SSamuel Ortiz 		return -ENOMEM;
67e710d7d5SSamuel Ortiz 
68e710d7d5SSamuel Ortiz 	return 0;
69e710d7d5SSamuel Ortiz }
70e710d7d5SSamuel Ortiz 
71424f525aSDmitry Baryshkov static int mfd_add_device(struct device *parent, int id,
72aa613de6SDmitry Baryshkov 			  const struct mfd_cell *cell,
73aa613de6SDmitry Baryshkov 			  struct resource *mem_base,
74aa613de6SDmitry Baryshkov 			  int irq_base)
75aa613de6SDmitry Baryshkov {
76a87903f3SIan Molton 	struct resource *res;
77aa613de6SDmitry Baryshkov 	struct platform_device *pdev;
78aa613de6SDmitry Baryshkov 	int ret = -ENOMEM;
79aa613de6SDmitry Baryshkov 	int r;
80aa613de6SDmitry Baryshkov 
813bed6e41SMark Brown 	pdev = platform_device_alloc(cell->name, id + cell->id);
82aa613de6SDmitry Baryshkov 	if (!pdev)
83aa613de6SDmitry Baryshkov 		goto fail_alloc;
84aa613de6SDmitry Baryshkov 
85a87903f3SIan Molton 	res = kzalloc(sizeof(*res) * cell->num_resources, GFP_KERNEL);
86a87903f3SIan Molton 	if (!res)
87a87903f3SIan Molton 		goto fail_device;
88a87903f3SIan Molton 
89424f525aSDmitry Baryshkov 	pdev->dev.parent = parent;
90aa613de6SDmitry Baryshkov 
91eb895607SSamuel Ortiz 	if (cell->pdata_size) {
92eb895607SSamuel Ortiz 		ret = platform_device_add_data(pdev,
93eb895607SSamuel Ortiz 					cell->platform_data, cell->pdata_size);
94eb895607SSamuel Ortiz 		if (ret)
95eb895607SSamuel Ortiz 			goto fail_res;
96eb895607SSamuel Ortiz 	}
97eb895607SSamuel Ortiz 
98e710d7d5SSamuel Ortiz 	ret = mfd_platform_add_cell(pdev, cell);
99aa613de6SDmitry Baryshkov 	if (ret)
100a87903f3SIan Molton 		goto fail_res;
101aa613de6SDmitry Baryshkov 
102aa613de6SDmitry Baryshkov 	for (r = 0; r < cell->num_resources; r++) {
103aa613de6SDmitry Baryshkov 		res[r].name = cell->resources[r].name;
104aa613de6SDmitry Baryshkov 		res[r].flags = cell->resources[r].flags;
105aa613de6SDmitry Baryshkov 
106aa613de6SDmitry Baryshkov 		/* Find out base to use */
107f03cfcbcSSamuel Ortiz 		if ((cell->resources[r].flags & IORESOURCE_MEM) && mem_base) {
108aa613de6SDmitry Baryshkov 			res[r].parent = mem_base;
109aa613de6SDmitry Baryshkov 			res[r].start = mem_base->start +
110aa613de6SDmitry Baryshkov 				cell->resources[r].start;
111aa613de6SDmitry Baryshkov 			res[r].end = mem_base->start +
112aa613de6SDmitry Baryshkov 				cell->resources[r].end;
113aa613de6SDmitry Baryshkov 		} else if (cell->resources[r].flags & IORESOURCE_IRQ) {
114aa613de6SDmitry Baryshkov 			res[r].start = irq_base +
115aa613de6SDmitry Baryshkov 				cell->resources[r].start;
116aa613de6SDmitry Baryshkov 			res[r].end   = irq_base +
117aa613de6SDmitry Baryshkov 				cell->resources[r].end;
118aa613de6SDmitry Baryshkov 		} else {
119aa613de6SDmitry Baryshkov 			res[r].parent = cell->resources[r].parent;
120aa613de6SDmitry Baryshkov 			res[r].start = cell->resources[r].start;
121aa613de6SDmitry Baryshkov 			res[r].end   = cell->resources[r].end;
122aa613de6SDmitry Baryshkov 		}
12391fededeSSamuel Ortiz 
1245f2545faSDaniel Drake 		if (!cell->ignore_resource_conflicts) {
12591fededeSSamuel Ortiz 			ret = acpi_check_resource_conflict(res);
12691fededeSSamuel Ortiz 			if (ret)
12791fededeSSamuel Ortiz 				goto fail_res;
128aa613de6SDmitry Baryshkov 		}
1295f2545faSDaniel Drake 	}
130aa613de6SDmitry Baryshkov 
1318af5fe3bSAxel Lin 	ret = platform_device_add_resources(pdev, res, cell->num_resources);
1328af5fe3bSAxel Lin 	if (ret)
1338af5fe3bSAxel Lin 		goto fail_res;
134aa613de6SDmitry Baryshkov 
135aa613de6SDmitry Baryshkov 	ret = platform_device_add(pdev);
136aa613de6SDmitry Baryshkov 	if (ret)
137a87903f3SIan Molton 		goto fail_res;
138a87903f3SIan Molton 
1394c90aa94SMark Brown 	if (cell->pm_runtime_no_callbacks)
1404c90aa94SMark Brown 		pm_runtime_no_callbacks(&pdev->dev);
1414c90aa94SMark Brown 
142a87903f3SIan Molton 	kfree(res);
143aa613de6SDmitry Baryshkov 
144aa613de6SDmitry Baryshkov 	return 0;
145aa613de6SDmitry Baryshkov 
146a87903f3SIan Molton fail_res:
147a87903f3SIan Molton 	kfree(res);
148aa613de6SDmitry Baryshkov fail_device:
149aa613de6SDmitry Baryshkov 	platform_device_put(pdev);
150aa613de6SDmitry Baryshkov fail_alloc:
151aa613de6SDmitry Baryshkov 	return ret;
152aa613de6SDmitry Baryshkov }
153aa613de6SDmitry Baryshkov 
154424f525aSDmitry Baryshkov int mfd_add_devices(struct device *parent, int id,
1551e29af62SAndres Salomon 		    struct mfd_cell *cells, int n_devs,
156aa613de6SDmitry Baryshkov 		    struct resource *mem_base,
157aa613de6SDmitry Baryshkov 		    int irq_base)
158aa613de6SDmitry Baryshkov {
159aa613de6SDmitry Baryshkov 	int i;
160aa613de6SDmitry Baryshkov 	int ret = 0;
1611e29af62SAndres Salomon 	atomic_t *cnts;
1621e29af62SAndres Salomon 
1631e29af62SAndres Salomon 	/* initialize reference counting for all cells */
1641e29af62SAndres Salomon 	cnts = kcalloc(sizeof(*cnts), n_devs, GFP_KERNEL);
1651e29af62SAndres Salomon 	if (!cnts)
1661e29af62SAndres Salomon 		return -ENOMEM;
167aa613de6SDmitry Baryshkov 
168aa613de6SDmitry Baryshkov 	for (i = 0; i < n_devs; i++) {
1691e29af62SAndres Salomon 		atomic_set(&cnts[i], 0);
1701e29af62SAndres Salomon 		cells[i].usage_count = &cnts[i];
171424f525aSDmitry Baryshkov 		ret = mfd_add_device(parent, id, cells + i, mem_base, irq_base);
172aa613de6SDmitry Baryshkov 		if (ret)
173aa613de6SDmitry Baryshkov 			break;
174aa613de6SDmitry Baryshkov 	}
175aa613de6SDmitry Baryshkov 
176aa613de6SDmitry Baryshkov 	if (ret)
177aa613de6SDmitry Baryshkov 		mfd_remove_devices(parent);
178aa613de6SDmitry Baryshkov 
179aa613de6SDmitry Baryshkov 	return ret;
180aa613de6SDmitry Baryshkov }
181aa613de6SDmitry Baryshkov EXPORT_SYMBOL(mfd_add_devices);
182aa613de6SDmitry Baryshkov 
1831e29af62SAndres Salomon static int mfd_remove_devices_fn(struct device *dev, void *c)
184aa613de6SDmitry Baryshkov {
1851e29af62SAndres Salomon 	struct platform_device *pdev = to_platform_device(dev);
1861e29af62SAndres Salomon 	const struct mfd_cell *cell = mfd_get_cell(pdev);
1871e29af62SAndres Salomon 	atomic_t **usage_count = c;
1881e29af62SAndres Salomon 
1891e29af62SAndres Salomon 	/* find the base address of usage_count pointers (for freeing) */
1901e29af62SAndres Salomon 	if (!*usage_count || (cell->usage_count < *usage_count))
1911e29af62SAndres Salomon 		*usage_count = cell->usage_count;
1921e29af62SAndres Salomon 
1931e29af62SAndres Salomon 	platform_device_unregister(pdev);
194aa613de6SDmitry Baryshkov 	return 0;
195aa613de6SDmitry Baryshkov }
196aa613de6SDmitry Baryshkov 
197424f525aSDmitry Baryshkov void mfd_remove_devices(struct device *parent)
198aa613de6SDmitry Baryshkov {
1991e29af62SAndres Salomon 	atomic_t *cnts = NULL;
2001e29af62SAndres Salomon 
2011e29af62SAndres Salomon 	device_for_each_child(parent, &cnts, mfd_remove_devices_fn);
2021e29af62SAndres Salomon 	kfree(cnts);
203aa613de6SDmitry Baryshkov }
204aa613de6SDmitry Baryshkov EXPORT_SYMBOL(mfd_remove_devices);
205aa613de6SDmitry Baryshkov 
206fa1df691SAndres Salomon int mfd_clone_cell(const char *cell, const char **clones, size_t n_clones)
207a9bbba99SAndres Salomon {
208a9bbba99SAndres Salomon 	struct mfd_cell cell_entry;
209a9bbba99SAndres Salomon 	struct device *dev;
210a9bbba99SAndres Salomon 	struct platform_device *pdev;
211fa1df691SAndres Salomon 	int i;
212a9bbba99SAndres Salomon 
213a9bbba99SAndres Salomon 	/* fetch the parent cell's device (should already be registered!) */
214a9bbba99SAndres Salomon 	dev = bus_find_device_by_name(&platform_bus_type, NULL, cell);
215a9bbba99SAndres Salomon 	if (!dev) {
216a9bbba99SAndres Salomon 		printk(KERN_ERR "failed to find device for cell %s\n", cell);
217a9bbba99SAndres Salomon 		return -ENODEV;
218a9bbba99SAndres Salomon 	}
219a9bbba99SAndres Salomon 	pdev = to_platform_device(dev);
220a9bbba99SAndres Salomon 	memcpy(&cell_entry, mfd_get_cell(pdev), sizeof(cell_entry));
221a9bbba99SAndres Salomon 
222a9bbba99SAndres Salomon 	WARN_ON(!cell_entry.enable);
223a9bbba99SAndres Salomon 
224fa1df691SAndres Salomon 	for (i = 0; i < n_clones; i++) {
225fa1df691SAndres Salomon 		cell_entry.name = clones[i];
226fa1df691SAndres Salomon 		/* don't give up if a single call fails; just report error */
227fa1df691SAndres Salomon 		if (mfd_add_device(pdev->dev.parent, -1, &cell_entry, NULL, 0))
228fa1df691SAndres Salomon 			dev_err(dev, "failed to create platform device '%s'\n",
229fa1df691SAndres Salomon 					clones[i]);
230a9bbba99SAndres Salomon 	}
231a9bbba99SAndres Salomon 
232fa1df691SAndres Salomon 	return 0;
233a9bbba99SAndres Salomon }
234fa1df691SAndres Salomon EXPORT_SYMBOL(mfd_clone_cell);
235a9bbba99SAndres Salomon 
236aa613de6SDmitry Baryshkov MODULE_LICENSE("GPL");
237aa613de6SDmitry Baryshkov MODULE_AUTHOR("Ian Molton, Dmitry Baryshkov");
238