xref: /openbmc/linux/drivers/mfd/mfd-core.c (revision 82fa83fb)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2aa613de6SDmitry Baryshkov /*
3aa613de6SDmitry Baryshkov  * drivers/mfd/mfd-core.c
4aa613de6SDmitry Baryshkov  *
5aa613de6SDmitry Baryshkov  * core MFD support
6aa613de6SDmitry Baryshkov  * Copyright (c) 2006 Ian Molton
7aa613de6SDmitry Baryshkov  * Copyright (c) 2007,2008 Dmitry Baryshkov
8aa613de6SDmitry Baryshkov  */
9aa613de6SDmitry Baryshkov 
10aa613de6SDmitry Baryshkov #include <linux/kernel.h>
11aa613de6SDmitry Baryshkov #include <linux/platform_device.h>
1291fededeSSamuel Ortiz #include <linux/acpi.h>
13466a62d7SLee Jones #include <linux/list.h>
144d215cabSAndy Shevchenko #include <linux/property.h>
15aa613de6SDmitry Baryshkov #include <linux/mfd/core.h>
164c90aa94SMark Brown #include <linux/pm_runtime.h>
175a0e3ad6STejun Heo #include <linux/slab.h>
184e36dd33SPaul Gortmaker #include <linux/module.h>
19c94bb233SLee Jones #include <linux/irqdomain.h>
20c94bb233SLee Jones #include <linux/of.h>
21466a62d7SLee Jones #include <linux/of_address.h>
227fcd4274SCharles Keepax #include <linux/regulator/consumer.h>
23aa613de6SDmitry Baryshkov 
24466a62d7SLee Jones static LIST_HEAD(mfd_of_node_list);
25466a62d7SLee Jones 
26466a62d7SLee Jones struct mfd_of_node_entry {
27466a62d7SLee Jones 	struct list_head list;
28466a62d7SLee Jones 	struct device *dev;
29466a62d7SLee Jones 	struct device_node *np;
30466a62d7SLee Jones };
31466a62d7SLee Jones 
32b9fbb62eSCharles Keepax static struct device_type mfd_dev_type = {
33b9fbb62eSCharles Keepax 	.name	= "mfd_device",
34b9fbb62eSCharles Keepax };
35b9fbb62eSCharles Keepax 
366ab34301SMika Westerberg #if IS_ENABLED(CONFIG_ACPI)
370c9b9c2aSRafael J. Wysocki struct match_ids_walk_data {
380c9b9c2aSRafael J. Wysocki 	struct acpi_device_id *ids;
390c9b9c2aSRafael J. Wysocki 	struct acpi_device *adev;
400c9b9c2aSRafael J. Wysocki };
410c9b9c2aSRafael J. Wysocki 
match_device_ids(struct acpi_device * adev,void * data)420c9b9c2aSRafael J. Wysocki static int match_device_ids(struct acpi_device *adev, void *data)
430c9b9c2aSRafael J. Wysocki {
440c9b9c2aSRafael J. Wysocki 	struct match_ids_walk_data *wd = data;
450c9b9c2aSRafael J. Wysocki 
460c9b9c2aSRafael J. Wysocki 	if (!acpi_match_device_ids(adev, wd->ids)) {
470c9b9c2aSRafael J. Wysocki 		wd->adev = adev;
480c9b9c2aSRafael J. Wysocki 		return 1;
490c9b9c2aSRafael J. Wysocki 	}
500c9b9c2aSRafael J. Wysocki 
510c9b9c2aSRafael J. Wysocki 	return 0;
520c9b9c2aSRafael J. Wysocki }
530c9b9c2aSRafael J. Wysocki 
mfd_acpi_add_device(const struct mfd_cell * cell,struct platform_device * pdev)546ab34301SMika Westerberg static void mfd_acpi_add_device(const struct mfd_cell *cell,
556ab34301SMika Westerberg 				struct platform_device *pdev)
566ab34301SMika Westerberg {
5798a3be44SAndy Shevchenko 	const struct mfd_cell_acpi_match *match = cell->acpi_match;
5840cb71f3SAndy Shevchenko 	struct acpi_device *adev = NULL;
590c9b9c2aSRafael J. Wysocki 	struct acpi_device *parent;
606ab34301SMika Westerberg 
6198a3be44SAndy Shevchenko 	parent = ACPI_COMPANION(pdev->dev.parent);
6298a3be44SAndy Shevchenko 	if (!parent)
636ab34301SMika Westerberg 		return;
646ab34301SMika Westerberg 
656ab34301SMika Westerberg 	/*
6698a3be44SAndy Shevchenko 	 * MFD child device gets its ACPI handle either from the ACPI device
6798a3be44SAndy Shevchenko 	 * directly under the parent that matches the either _HID or _CID, or
6898a3be44SAndy Shevchenko 	 * _ADR or it will use the parent handle if is no ID is given.
6998a3be44SAndy Shevchenko 	 *
7098a3be44SAndy Shevchenko 	 * Note that use of _ADR is a grey area in the ACPI specification,
7140cb71f3SAndy Shevchenko 	 * though at least Intel Galileo Gen 2 is using it to distinguish
7240cb71f3SAndy Shevchenko 	 * the children devices.
736ab34301SMika Westerberg 	 */
7498a3be44SAndy Shevchenko 	if (match) {
7598a3be44SAndy Shevchenko 		if (match->pnpid) {
766ab34301SMika Westerberg 			struct acpi_device_id ids[2] = {};
770c9b9c2aSRafael J. Wysocki 			struct match_ids_walk_data wd = {
780c9b9c2aSRafael J. Wysocki 				.adev = NULL,
790c9b9c2aSRafael J. Wysocki 				.ids = ids,
800c9b9c2aSRafael J. Wysocki 			};
816ab34301SMika Westerberg 
826a32d399SWolfram Sang 			strscpy(ids[0].id, match->pnpid, sizeof(ids[0].id));
830c9b9c2aSRafael J. Wysocki 			acpi_dev_for_each_child(parent, match_device_ids, &wd);
840c9b9c2aSRafael J. Wysocki 			adev = wd.adev;
8598a3be44SAndy Shevchenko 		} else {
8640cb71f3SAndy Shevchenko 			adev = acpi_find_child_device(parent, match->adr, false);
8798a3be44SAndy Shevchenko 		}
8898a3be44SAndy Shevchenko 	}
896ab34301SMika Westerberg 
9040cb71f3SAndy Shevchenko 	ACPI_COMPANION_SET(&pdev->dev, adev ?: parent);
916ab34301SMika Westerberg }
926ab34301SMika Westerberg #else
mfd_acpi_add_device(const struct mfd_cell * cell,struct platform_device * pdev)936ab34301SMika Westerberg static inline void mfd_acpi_add_device(const struct mfd_cell *cell,
946ab34301SMika Westerberg 				       struct platform_device *pdev)
956ab34301SMika Westerberg {
966ab34301SMika Westerberg }
976ab34301SMika Westerberg #endif
986ab34301SMika Westerberg 
mfd_match_of_node_to_dev(struct platform_device * pdev,struct device_node * np,const struct mfd_cell * cell)99466a62d7SLee Jones static int mfd_match_of_node_to_dev(struct platform_device *pdev,
100466a62d7SLee Jones 				    struct device_node *np,
101466a62d7SLee Jones 				    const struct mfd_cell *cell)
102466a62d7SLee Jones {
103466a62d7SLee Jones #if IS_ENABLED(CONFIG_OF)
104466a62d7SLee Jones 	struct mfd_of_node_entry *of_entry;
105466a62d7SLee Jones 	u64 of_node_addr;
106466a62d7SLee Jones 
107466a62d7SLee Jones 	/* Skip if OF node has previously been allocated to a device */
108466a62d7SLee Jones 	list_for_each_entry(of_entry, &mfd_of_node_list, list)
109466a62d7SLee Jones 		if (of_entry->np == np)
110466a62d7SLee Jones 			return -EAGAIN;
111466a62d7SLee Jones 
112466a62d7SLee Jones 	if (!cell->use_of_reg)
113466a62d7SLee Jones 		/* No of_reg defined - allocate first free compatible match */
114466a62d7SLee Jones 		goto allocate_of_node;
115466a62d7SLee Jones 
116466a62d7SLee Jones 	/* We only care about each node's first defined address */
117c7fe3bbfSRob Herring 	if (of_property_read_reg(np, 0, &of_node_addr, NULL))
118466a62d7SLee Jones 		/* OF node does not contatin a 'reg' property to match to */
119466a62d7SLee Jones 		return -EAGAIN;
120466a62d7SLee Jones 
121466a62d7SLee Jones 	if (cell->of_reg != of_node_addr)
122466a62d7SLee Jones 		/* No match */
123466a62d7SLee Jones 		return -EAGAIN;
124466a62d7SLee Jones 
125466a62d7SLee Jones allocate_of_node:
126466a62d7SLee Jones 	of_entry = kzalloc(sizeof(*of_entry), GFP_KERNEL);
127466a62d7SLee Jones 	if (!of_entry)
128466a62d7SLee Jones 		return -ENOMEM;
129466a62d7SLee Jones 
130466a62d7SLee Jones 	of_entry->dev = &pdev->dev;
131466a62d7SLee Jones 	of_entry->np = np;
132466a62d7SLee Jones 	list_add_tail(&of_entry->list, &mfd_of_node_list);
133466a62d7SLee Jones 
134466a62d7SLee Jones 	pdev->dev.of_node = np;
135466a62d7SLee Jones 	pdev->dev.fwnode = &np->fwnode;
136466a62d7SLee Jones #endif
137466a62d7SLee Jones 	return 0;
138466a62d7SLee Jones }
139466a62d7SLee Jones 
mfd_add_device(struct device * parent,int id,const struct mfd_cell * cell,struct resource * mem_base,int irq_base,struct irq_domain * domain)140424f525aSDmitry Baryshkov static int mfd_add_device(struct device *parent, int id,
1415a47c0fbSLee Jones 			  const struct mfd_cell *cell,
142aa613de6SDmitry Baryshkov 			  struct resource *mem_base,
1430848c94fSMark Brown 			  int irq_base, struct irq_domain *domain)
144aa613de6SDmitry Baryshkov {
145a87903f3SIan Molton 	struct resource *res;
146aa613de6SDmitry Baryshkov 	struct platform_device *pdev;
147c94bb233SLee Jones 	struct device_node *np = NULL;
148466a62d7SLee Jones 	struct mfd_of_node_entry *of_entry, *tmp;
149*82fa83fbSHerve Codina 	bool disabled = false;
150aa613de6SDmitry Baryshkov 	int ret = -ENOMEM;
1516e3f62f0SJohan Hovold 	int platform_id;
152aa613de6SDmitry Baryshkov 	int r;
153aa613de6SDmitry Baryshkov 
154a77c50b4SJohan Hovold 	if (id == PLATFORM_DEVID_AUTO)
1556e3f62f0SJohan Hovold 		platform_id = id;
1566e3f62f0SJohan Hovold 	else
1576e3f62f0SJohan Hovold 		platform_id = id + cell->id;
1586e3f62f0SJohan Hovold 
1596e3f62f0SJohan Hovold 	pdev = platform_device_alloc(cell->name, platform_id);
160aa613de6SDmitry Baryshkov 	if (!pdev)
161aa613de6SDmitry Baryshkov 		goto fail_alloc;
162aa613de6SDmitry Baryshkov 
163b944a688SLee Jones 	pdev->mfd_cell = kmemdup(cell, sizeof(*cell), GFP_KERNEL);
164b944a688SLee Jones 	if (!pdev->mfd_cell)
165b944a688SLee Jones 		goto fail_device;
166b944a688SLee Jones 
1676396bb22SKees Cook 	res = kcalloc(cell->num_resources, sizeof(*res), GFP_KERNEL);
168a87903f3SIan Molton 	if (!res)
169a87903f3SIan Molton 		goto fail_device;
170a87903f3SIan Molton 
171424f525aSDmitry Baryshkov 	pdev->dev.parent = parent;
172b9fbb62eSCharles Keepax 	pdev->dev.type = &mfd_dev_type;
173b018e136SBenedikt Spranger 	pdev->dev.dma_mask = parent->dma_mask;
174b018e136SBenedikt Spranger 	pdev->dev.dma_parms = parent->dma_parms;
1754f08df1bSBoris BREZILLON 	pdev->dev.coherent_dma_mask = parent->coherent_dma_mask;
176aa613de6SDmitry Baryshkov 
177d137be00SCharles Keepax 	ret = regulator_bulk_register_supply_alias(
1787fcd4274SCharles Keepax 			&pdev->dev, cell->parent_supplies,
1797fcd4274SCharles Keepax 			parent, cell->parent_supplies,
1807fcd4274SCharles Keepax 			cell->num_parent_supplies);
1817fcd4274SCharles Keepax 	if (ret < 0)
1827fcd4274SCharles Keepax 		goto fail_res;
1837fcd4274SCharles Keepax 
184466a62d7SLee Jones 	if (IS_ENABLED(CONFIG_OF) && parent->of_node && cell->of_compatible) {
185c94bb233SLee Jones 		for_each_child_of_node(parent->of_node, np) {
186c94bb233SLee Jones 			if (of_device_is_compatible(np, cell->of_compatible)) {
187*82fa83fbSHerve Codina 				/* Skip 'disabled' devices */
18822380b65SLee Jones 				if (!of_device_is_available(np)) {
189*82fa83fbSHerve Codina 					disabled = true;
190*82fa83fbSHerve Codina 					continue;
19122380b65SLee Jones 				}
19222380b65SLee Jones 
193466a62d7SLee Jones 				ret = mfd_match_of_node_to_dev(pdev, np, cell);
194466a62d7SLee Jones 				if (ret == -EAGAIN)
195466a62d7SLee Jones 					continue;
196002be811SKrzysztof Kozlowski 				of_node_put(np);
197466a62d7SLee Jones 				if (ret)
1986b5c3506SLee Jones 					goto fail_alias;
199466a62d7SLee Jones 
200*82fa83fbSHerve Codina 				goto match;
201c94bb233SLee Jones 			}
202c94bb233SLee Jones 		}
203466a62d7SLee Jones 
204*82fa83fbSHerve Codina 		if (disabled) {
205*82fa83fbSHerve Codina 			/* Ignore 'disabled' devices error free */
206*82fa83fbSHerve Codina 			ret = 0;
207*82fa83fbSHerve Codina 			goto fail_alias;
208*82fa83fbSHerve Codina 		}
209*82fa83fbSHerve Codina 
210*82fa83fbSHerve Codina match:
211466a62d7SLee Jones 		if (!pdev->dev.of_node)
212466a62d7SLee Jones 			pr_warn("%s: Failed to locate of_node [id: %d]\n",
213466a62d7SLee Jones 				cell->name, platform_id);
214c94bb233SLee Jones 	}
215c94bb233SLee Jones 
2166ab34301SMika Westerberg 	mfd_acpi_add_device(cell, pdev);
2176ab34301SMika Westerberg 
218eb895607SSamuel Ortiz 	if (cell->pdata_size) {
219eb895607SSamuel Ortiz 		ret = platform_device_add_data(pdev,
220eb895607SSamuel Ortiz 					cell->platform_data, cell->pdata_size);
221eb895607SSamuel Ortiz 		if (ret)
222466a62d7SLee Jones 			goto fail_of_entry;
223eb895607SSamuel Ortiz 	}
224eb895607SSamuel Ortiz 
22542e59982SHeikki Krogerus 	if (cell->swnode) {
22642e59982SHeikki Krogerus 		ret = device_add_software_node(&pdev->dev, cell->swnode);
22742e59982SHeikki Krogerus 		if (ret)
22842e59982SHeikki Krogerus 			goto fail_of_entry;
22942e59982SHeikki Krogerus 	}
23042e59982SHeikki Krogerus 
231aa613de6SDmitry Baryshkov 	for (r = 0; r < cell->num_resources; r++) {
232aa613de6SDmitry Baryshkov 		res[r].name = cell->resources[r].name;
233aa613de6SDmitry Baryshkov 		res[r].flags = cell->resources[r].flags;
234aa613de6SDmitry Baryshkov 
235aa613de6SDmitry Baryshkov 		/* Find out base to use */
236f03cfcbcSSamuel Ortiz 		if ((cell->resources[r].flags & IORESOURCE_MEM) && mem_base) {
237aa613de6SDmitry Baryshkov 			res[r].parent = mem_base;
238aa613de6SDmitry Baryshkov 			res[r].start = mem_base->start +
239aa613de6SDmitry Baryshkov 				cell->resources[r].start;
240aa613de6SDmitry Baryshkov 			res[r].end = mem_base->start +
241aa613de6SDmitry Baryshkov 				cell->resources[r].end;
242aa613de6SDmitry Baryshkov 		} else if (cell->resources[r].flags & IORESOURCE_IRQ) {
243c94bb233SLee Jones 			if (domain) {
244c94bb233SLee Jones 				/* Unable to create mappings for IRQ ranges. */
245c94bb233SLee Jones 				WARN_ON(cell->resources[r].start !=
246c94bb233SLee Jones 					cell->resources[r].end);
247c94bb233SLee Jones 				res[r].start = res[r].end = irq_create_mapping(
248c94bb233SLee Jones 					domain, cell->resources[r].start);
249c94bb233SLee Jones 			} else {
250aa613de6SDmitry Baryshkov 				res[r].start = irq_base +
251aa613de6SDmitry Baryshkov 					cell->resources[r].start;
252aa613de6SDmitry Baryshkov 				res[r].end   = irq_base +
253aa613de6SDmitry Baryshkov 					cell->resources[r].end;
254c94bb233SLee Jones 			}
255aa613de6SDmitry Baryshkov 		} else {
256aa613de6SDmitry Baryshkov 			res[r].parent = cell->resources[r].parent;
257aa613de6SDmitry Baryshkov 			res[r].start = cell->resources[r].start;
258aa613de6SDmitry Baryshkov 			res[r].end   = cell->resources[r].end;
259aa613de6SDmitry Baryshkov 		}
26091fededeSSamuel Ortiz 
2615f2545faSDaniel Drake 		if (!cell->ignore_resource_conflicts) {
262ec40c606SLorenzo Pieralisi 			if (has_acpi_companion(&pdev->dev)) {
263855cc454SJean Delvare 				ret = acpi_check_resource_conflict(&res[r]);
26491fededeSSamuel Ortiz 				if (ret)
2655a23e8b0SAndy Shevchenko 					goto fail_res_conflict;
266aa613de6SDmitry Baryshkov 			}
2675f2545faSDaniel Drake 		}
268ec40c606SLorenzo Pieralisi 	}
269aa613de6SDmitry Baryshkov 
2708af5fe3bSAxel Lin 	ret = platform_device_add_resources(pdev, res, cell->num_resources);
2718af5fe3bSAxel Lin 	if (ret)
2725a23e8b0SAndy Shevchenko 		goto fail_res_conflict;
273aa613de6SDmitry Baryshkov 
274aa613de6SDmitry Baryshkov 	ret = platform_device_add(pdev);
275aa613de6SDmitry Baryshkov 	if (ret)
2765a23e8b0SAndy Shevchenko 		goto fail_res_conflict;
277a87903f3SIan Molton 
2784c90aa94SMark Brown 	if (cell->pm_runtime_no_callbacks)
2794c90aa94SMark Brown 		pm_runtime_no_callbacks(&pdev->dev);
2804c90aa94SMark Brown 
281a87903f3SIan Molton 	kfree(res);
282aa613de6SDmitry Baryshkov 
283aa613de6SDmitry Baryshkov 	return 0;
284aa613de6SDmitry Baryshkov 
2855a23e8b0SAndy Shevchenko fail_res_conflict:
2865a23e8b0SAndy Shevchenko 	if (cell->swnode)
2875a23e8b0SAndy Shevchenko 		device_remove_software_node(&pdev->dev);
288466a62d7SLee Jones fail_of_entry:
289466a62d7SLee Jones 	list_for_each_entry_safe(of_entry, tmp, &mfd_of_node_list, list)
290466a62d7SLee Jones 		if (of_entry->dev == &pdev->dev) {
291466a62d7SLee Jones 			list_del(&of_entry->list);
292466a62d7SLee Jones 			kfree(of_entry);
293466a62d7SLee Jones 		}
2947fcd4274SCharles Keepax fail_alias:
295d137be00SCharles Keepax 	regulator_bulk_unregister_supply_alias(&pdev->dev,
2967fcd4274SCharles Keepax 					       cell->parent_supplies,
2977fcd4274SCharles Keepax 					       cell->num_parent_supplies);
298a87903f3SIan Molton fail_res:
299a87903f3SIan Molton 	kfree(res);
300aa613de6SDmitry Baryshkov fail_device:
301aa613de6SDmitry Baryshkov 	platform_device_put(pdev);
302aa613de6SDmitry Baryshkov fail_alloc:
303aa613de6SDmitry Baryshkov 	return ret;
304aa613de6SDmitry Baryshkov }
305aa613de6SDmitry Baryshkov 
3061946f996SBartosz Golaszewski /**
3071946f996SBartosz Golaszewski  * mfd_add_devices - register child devices
3081946f996SBartosz Golaszewski  *
3091946f996SBartosz Golaszewski  * @parent:	Pointer to parent device.
3101946f996SBartosz Golaszewski  * @id:		Can be PLATFORM_DEVID_AUTO to let the Platform API take care
3111946f996SBartosz Golaszewski  *		of device numbering, or will be added to a device's cell_id.
3121946f996SBartosz Golaszewski  * @cells:	Array of (struct mfd_cell)s describing child devices.
3131946f996SBartosz Golaszewski  * @n_devs:	Number of child devices to register.
3141946f996SBartosz Golaszewski  * @mem_base:	Parent register range resource for child devices.
3151946f996SBartosz Golaszewski  * @irq_base:	Base of the range of virtual interrupt numbers allocated for
3161946f996SBartosz Golaszewski  *		this MFD device. Unused if @domain is specified.
3171946f996SBartosz Golaszewski  * @domain:	Interrupt domain to create mappings for hardware interrupts.
3181946f996SBartosz Golaszewski  */
mfd_add_devices(struct device * parent,int id,const struct mfd_cell * cells,int n_devs,struct resource * mem_base,int irq_base,struct irq_domain * domain)319424f525aSDmitry Baryshkov int mfd_add_devices(struct device *parent, int id,
32003e361b2SGeert Uytterhoeven 		    const struct mfd_cell *cells, int n_devs,
321aa613de6SDmitry Baryshkov 		    struct resource *mem_base,
3220848c94fSMark Brown 		    int irq_base, struct irq_domain *domain)
323aa613de6SDmitry Baryshkov {
324aa613de6SDmitry Baryshkov 	int i;
3250b208e41SGeert Uytterhoeven 	int ret;
326aa613de6SDmitry Baryshkov 
327aa613de6SDmitry Baryshkov 	for (i = 0; i < n_devs; i++) {
3285a47c0fbSLee Jones 		ret = mfd_add_device(parent, id, cells + i, mem_base,
3290848c94fSMark Brown 				     irq_base, domain);
330aa613de6SDmitry Baryshkov 		if (ret)
3310b208e41SGeert Uytterhoeven 			goto fail;
332aa613de6SDmitry Baryshkov 	}
333aa613de6SDmitry Baryshkov 
3340b208e41SGeert Uytterhoeven 	return 0;
335aa613de6SDmitry Baryshkov 
3360b208e41SGeert Uytterhoeven fail:
3370b208e41SGeert Uytterhoeven 	if (i)
3380b208e41SGeert Uytterhoeven 		mfd_remove_devices(parent);
3395a47c0fbSLee Jones 
340aa613de6SDmitry Baryshkov 	return ret;
341aa613de6SDmitry Baryshkov }
342aa613de6SDmitry Baryshkov EXPORT_SYMBOL(mfd_add_devices);
343aa613de6SDmitry Baryshkov 
mfd_remove_devices_fn(struct device * dev,void * data)3445a47c0fbSLee Jones static int mfd_remove_devices_fn(struct device *dev, void *data)
345aa613de6SDmitry Baryshkov {
346b9fbb62eSCharles Keepax 	struct platform_device *pdev;
347b9fbb62eSCharles Keepax 	const struct mfd_cell *cell;
3488e88c61dSMichal Oleszczyk 	struct mfd_of_node_entry *of_entry, *tmp;
349114294d2SCharles Keepax 	int *level = data;
3501e29af62SAndres Salomon 
351b9fbb62eSCharles Keepax 	if (dev->type != &mfd_dev_type)
352b9fbb62eSCharles Keepax 		return 0;
353b9fbb62eSCharles Keepax 
354b9fbb62eSCharles Keepax 	pdev = to_platform_device(dev);
355b9fbb62eSCharles Keepax 	cell = mfd_get_cell(pdev);
356b9fbb62eSCharles Keepax 
357114294d2SCharles Keepax 	if (level && cell->level > *level)
358114294d2SCharles Keepax 		return 0;
359114294d2SCharles Keepax 
3605a23e8b0SAndy Shevchenko 	if (cell->swnode)
3615a23e8b0SAndy Shevchenko 		device_remove_software_node(&pdev->dev);
3625a23e8b0SAndy Shevchenko 
3638e88c61dSMichal Oleszczyk 	list_for_each_entry_safe(of_entry, tmp, &mfd_of_node_list, list)
3648e88c61dSMichal Oleszczyk 		if (of_entry->dev == &pdev->dev) {
3658e88c61dSMichal Oleszczyk 			list_del(&of_entry->list);
3668e88c61dSMichal Oleszczyk 			kfree(of_entry);
3678e88c61dSMichal Oleszczyk 		}
3688e88c61dSMichal Oleszczyk 
369d137be00SCharles Keepax 	regulator_bulk_unregister_supply_alias(dev, cell->parent_supplies,
370d137be00SCharles Keepax 					       cell->num_parent_supplies);
371d137be00SCharles Keepax 
3721e29af62SAndres Salomon 	platform_device_unregister(pdev);
373aa613de6SDmitry Baryshkov 	return 0;
374aa613de6SDmitry Baryshkov }
375aa613de6SDmitry Baryshkov 
mfd_remove_devices_late(struct device * parent)376114294d2SCharles Keepax void mfd_remove_devices_late(struct device *parent)
377114294d2SCharles Keepax {
378114294d2SCharles Keepax 	int level = MFD_DEP_LEVEL_HIGH;
379114294d2SCharles Keepax 
380114294d2SCharles Keepax 	device_for_each_child_reverse(parent, &level, mfd_remove_devices_fn);
381114294d2SCharles Keepax }
382114294d2SCharles Keepax EXPORT_SYMBOL(mfd_remove_devices_late);
383114294d2SCharles Keepax 
mfd_remove_devices(struct device * parent)384424f525aSDmitry Baryshkov void mfd_remove_devices(struct device *parent)
385aa613de6SDmitry Baryshkov {
386114294d2SCharles Keepax 	int level = MFD_DEP_LEVEL_NORMAL;
387114294d2SCharles Keepax 
388114294d2SCharles Keepax 	device_for_each_child_reverse(parent, &level, mfd_remove_devices_fn);
389aa613de6SDmitry Baryshkov }
390aa613de6SDmitry Baryshkov EXPORT_SYMBOL(mfd_remove_devices);
391aa613de6SDmitry Baryshkov 
devm_mfd_dev_release(struct device * dev,void * res)392a8f447beSLaxman Dewangan static void devm_mfd_dev_release(struct device *dev, void *res)
393a8f447beSLaxman Dewangan {
394a8f447beSLaxman Dewangan 	mfd_remove_devices(dev);
395a8f447beSLaxman Dewangan }
396a8f447beSLaxman Dewangan 
397a8f447beSLaxman Dewangan /**
398a8f447beSLaxman Dewangan  * devm_mfd_add_devices - Resource managed version of mfd_add_devices()
399a8f447beSLaxman Dewangan  *
400a8f447beSLaxman Dewangan  * Returns 0 on success or an appropriate negative error number on failure.
401a8f447beSLaxman Dewangan  * All child-devices of the MFD will automatically be removed when it gets
402a8f447beSLaxman Dewangan  * unbinded.
4035a0ffef8SLee Jones  *
4045a0ffef8SLee Jones  * @dev:	Pointer to parent device.
4055a0ffef8SLee Jones  * @id:		Can be PLATFORM_DEVID_AUTO to let the Platform API take care
4065a0ffef8SLee Jones  *		of device numbering, or will be added to a device's cell_id.
4075a0ffef8SLee Jones  * @cells:	Array of (struct mfd_cell)s describing child devices.
4085a0ffef8SLee Jones  * @n_devs:	Number of child devices to register.
4095a0ffef8SLee Jones  * @mem_base:	Parent register range resource for child devices.
4105a0ffef8SLee Jones  * @irq_base:	Base of the range of virtual interrupt numbers allocated for
4115a0ffef8SLee Jones  *		this MFD device. Unused if @domain is specified.
4125a0ffef8SLee Jones  * @domain:	Interrupt domain to create mappings for hardware interrupts.
413a8f447beSLaxman Dewangan  */
devm_mfd_add_devices(struct device * dev,int id,const struct mfd_cell * cells,int n_devs,struct resource * mem_base,int irq_base,struct irq_domain * domain)414a8f447beSLaxman Dewangan int devm_mfd_add_devices(struct device *dev, int id,
415a8f447beSLaxman Dewangan 			 const struct mfd_cell *cells, int n_devs,
416a8f447beSLaxman Dewangan 			 struct resource *mem_base,
417a8f447beSLaxman Dewangan 			 int irq_base, struct irq_domain *domain)
418a8f447beSLaxman Dewangan {
419a8f447beSLaxman Dewangan 	struct device **ptr;
420a8f447beSLaxman Dewangan 	int ret;
421a8f447beSLaxman Dewangan 
422a8f447beSLaxman Dewangan 	ptr = devres_alloc(devm_mfd_dev_release, sizeof(*ptr), GFP_KERNEL);
423a8f447beSLaxman Dewangan 	if (!ptr)
424a8f447beSLaxman Dewangan 		return -ENOMEM;
425a8f447beSLaxman Dewangan 
426a8f447beSLaxman Dewangan 	ret = mfd_add_devices(dev, id, cells, n_devs, mem_base,
427a8f447beSLaxman Dewangan 			      irq_base, domain);
428a8f447beSLaxman Dewangan 	if (ret < 0) {
429a8f447beSLaxman Dewangan 		devres_free(ptr);
430a8f447beSLaxman Dewangan 		return ret;
431a8f447beSLaxman Dewangan 	}
432a8f447beSLaxman Dewangan 
433a8f447beSLaxman Dewangan 	*ptr = dev;
434a8f447beSLaxman Dewangan 	devres_add(dev, ptr);
435a8f447beSLaxman Dewangan 
436a8f447beSLaxman Dewangan 	return ret;
437a8f447beSLaxman Dewangan }
438a8f447beSLaxman Dewangan EXPORT_SYMBOL(devm_mfd_add_devices);
439a8f447beSLaxman Dewangan 
440aa613de6SDmitry Baryshkov MODULE_LICENSE("GPL");
441aa613de6SDmitry Baryshkov MODULE_AUTHOR("Ian Molton, Dmitry Baryshkov");
442