xref: /openbmc/linux/drivers/mfd/mfd-core.c (revision 7190d0ff)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * drivers/mfd/mfd-core.c
4  *
5  * core MFD support
6  * Copyright (c) 2006 Ian Molton
7  * Copyright (c) 2007,2008 Dmitry Baryshkov
8  */
9 
10 #include <linux/kernel.h>
11 #include <linux/platform_device.h>
12 #include <linux/acpi.h>
13 #include <linux/list.h>
14 #include <linux/property.h>
15 #include <linux/mfd/core.h>
16 #include <linux/pm_runtime.h>
17 #include <linux/slab.h>
18 #include <linux/module.h>
19 #include <linux/irqdomain.h>
20 #include <linux/of.h>
21 #include <linux/of_address.h>
22 #include <linux/regulator/consumer.h>
23 
24 static LIST_HEAD(mfd_of_node_list);
25 
26 struct mfd_of_node_entry {
27 	struct list_head list;
28 	struct device *dev;
29 	struct device_node *np;
30 };
31 
32 static struct device_type mfd_dev_type = {
33 	.name	= "mfd_device",
34 };
35 
36 #if IS_ENABLED(CONFIG_ACPI)
37 struct match_ids_walk_data {
38 	struct acpi_device_id *ids;
39 	struct acpi_device *adev;
40 };
41 
42 static int match_device_ids(struct acpi_device *adev, void *data)
43 {
44 	struct match_ids_walk_data *wd = data;
45 
46 	if (!acpi_match_device_ids(adev, wd->ids)) {
47 		wd->adev = adev;
48 		return 1;
49 	}
50 
51 	return 0;
52 }
53 
54 static void mfd_acpi_add_device(const struct mfd_cell *cell,
55 				struct platform_device *pdev)
56 {
57 	const struct mfd_cell_acpi_match *match = cell->acpi_match;
58 	struct acpi_device *adev = NULL;
59 	struct acpi_device *parent;
60 
61 	parent = ACPI_COMPANION(pdev->dev.parent);
62 	if (!parent)
63 		return;
64 
65 	/*
66 	 * MFD child device gets its ACPI handle either from the ACPI device
67 	 * directly under the parent that matches the either _HID or _CID, or
68 	 * _ADR or it will use the parent handle if is no ID is given.
69 	 *
70 	 * Note that use of _ADR is a grey area in the ACPI specification,
71 	 * though at least Intel Galileo Gen 2 is using it to distinguish
72 	 * the children devices.
73 	 */
74 	if (match) {
75 		if (match->pnpid) {
76 			struct acpi_device_id ids[2] = {};
77 			struct match_ids_walk_data wd = {
78 				.adev = NULL,
79 				.ids = ids,
80 			};
81 
82 			strscpy(ids[0].id, match->pnpid, sizeof(ids[0].id));
83 			acpi_dev_for_each_child(parent, match_device_ids, &wd);
84 			adev = wd.adev;
85 		} else {
86 			adev = acpi_find_child_device(parent, match->adr, false);
87 		}
88 	}
89 
90 	ACPI_COMPANION_SET(&pdev->dev, adev ?: parent);
91 }
92 #else
93 static inline void mfd_acpi_add_device(const struct mfd_cell *cell,
94 				       struct platform_device *pdev)
95 {
96 }
97 #endif
98 
99 static int mfd_match_of_node_to_dev(struct platform_device *pdev,
100 				    struct device_node *np,
101 				    const struct mfd_cell *cell)
102 {
103 #if IS_ENABLED(CONFIG_OF)
104 	struct mfd_of_node_entry *of_entry;
105 	const __be32 *reg;
106 	u64 of_node_addr;
107 
108 	/* Skip if OF node has previously been allocated to a device */
109 	list_for_each_entry(of_entry, &mfd_of_node_list, list)
110 		if (of_entry->np == np)
111 			return -EAGAIN;
112 
113 	if (!cell->use_of_reg)
114 		/* No of_reg defined - allocate first free compatible match */
115 		goto allocate_of_node;
116 
117 	/* We only care about each node's first defined address */
118 	reg = of_get_address(np, 0, NULL, NULL);
119 	if (!reg)
120 		/* OF node does not contatin a 'reg' property to match to */
121 		return -EAGAIN;
122 
123 	of_node_addr = of_read_number(reg, of_n_addr_cells(np));
124 
125 	if (cell->of_reg != of_node_addr)
126 		/* No match */
127 		return -EAGAIN;
128 
129 allocate_of_node:
130 	of_entry = kzalloc(sizeof(*of_entry), GFP_KERNEL);
131 	if (!of_entry)
132 		return -ENOMEM;
133 
134 	of_entry->dev = &pdev->dev;
135 	of_entry->np = np;
136 	list_add_tail(&of_entry->list, &mfd_of_node_list);
137 
138 	pdev->dev.of_node = np;
139 	pdev->dev.fwnode = &np->fwnode;
140 #endif
141 	return 0;
142 }
143 
144 static int mfd_add_device(struct device *parent, int id,
145 			  const struct mfd_cell *cell,
146 			  struct resource *mem_base,
147 			  int irq_base, struct irq_domain *domain)
148 {
149 	struct resource *res;
150 	struct platform_device *pdev;
151 	struct device_node *np = NULL;
152 	struct mfd_of_node_entry *of_entry, *tmp;
153 	int ret = -ENOMEM;
154 	int platform_id;
155 	int r;
156 
157 	if (id == PLATFORM_DEVID_AUTO)
158 		platform_id = id;
159 	else
160 		platform_id = id + cell->id;
161 
162 	pdev = platform_device_alloc(cell->name, platform_id);
163 	if (!pdev)
164 		goto fail_alloc;
165 
166 	pdev->mfd_cell = kmemdup(cell, sizeof(*cell), GFP_KERNEL);
167 	if (!pdev->mfd_cell)
168 		goto fail_device;
169 
170 	res = kcalloc(cell->num_resources, sizeof(*res), GFP_KERNEL);
171 	if (!res)
172 		goto fail_device;
173 
174 	pdev->dev.parent = parent;
175 	pdev->dev.type = &mfd_dev_type;
176 	pdev->dev.dma_mask = parent->dma_mask;
177 	pdev->dev.dma_parms = parent->dma_parms;
178 	pdev->dev.coherent_dma_mask = parent->coherent_dma_mask;
179 
180 	ret = regulator_bulk_register_supply_alias(
181 			&pdev->dev, cell->parent_supplies,
182 			parent, cell->parent_supplies,
183 			cell->num_parent_supplies);
184 	if (ret < 0)
185 		goto fail_res;
186 
187 	if (IS_ENABLED(CONFIG_OF) && parent->of_node && cell->of_compatible) {
188 		for_each_child_of_node(parent->of_node, np) {
189 			if (of_device_is_compatible(np, cell->of_compatible)) {
190 				/* Ignore 'disabled' devices error free */
191 				if (!of_device_is_available(np)) {
192 					of_node_put(np);
193 					ret = 0;
194 					goto fail_alias;
195 				}
196 
197 				ret = mfd_match_of_node_to_dev(pdev, np, cell);
198 				if (ret == -EAGAIN)
199 					continue;
200 				of_node_put(np);
201 				if (ret)
202 					goto fail_alias;
203 
204 				break;
205 			}
206 		}
207 
208 		if (!pdev->dev.of_node)
209 			pr_warn("%s: Failed to locate of_node [id: %d]\n",
210 				cell->name, platform_id);
211 	}
212 
213 	mfd_acpi_add_device(cell, pdev);
214 
215 	if (cell->pdata_size) {
216 		ret = platform_device_add_data(pdev,
217 					cell->platform_data, cell->pdata_size);
218 		if (ret)
219 			goto fail_of_entry;
220 	}
221 
222 	if (cell->swnode) {
223 		ret = device_add_software_node(&pdev->dev, cell->swnode);
224 		if (ret)
225 			goto fail_of_entry;
226 	}
227 
228 	for (r = 0; r < cell->num_resources; r++) {
229 		res[r].name = cell->resources[r].name;
230 		res[r].flags = cell->resources[r].flags;
231 
232 		/* Find out base to use */
233 		if ((cell->resources[r].flags & IORESOURCE_MEM) && mem_base) {
234 			res[r].parent = mem_base;
235 			res[r].start = mem_base->start +
236 				cell->resources[r].start;
237 			res[r].end = mem_base->start +
238 				cell->resources[r].end;
239 		} else if (cell->resources[r].flags & IORESOURCE_IRQ) {
240 			if (domain) {
241 				/* Unable to create mappings for IRQ ranges. */
242 				WARN_ON(cell->resources[r].start !=
243 					cell->resources[r].end);
244 				res[r].start = res[r].end = irq_create_mapping(
245 					domain, cell->resources[r].start);
246 			} else {
247 				res[r].start = irq_base +
248 					cell->resources[r].start;
249 				res[r].end   = irq_base +
250 					cell->resources[r].end;
251 			}
252 		} else {
253 			res[r].parent = cell->resources[r].parent;
254 			res[r].start = cell->resources[r].start;
255 			res[r].end   = cell->resources[r].end;
256 		}
257 
258 		if (!cell->ignore_resource_conflicts) {
259 			if (has_acpi_companion(&pdev->dev)) {
260 				ret = acpi_check_resource_conflict(&res[r]);
261 				if (ret)
262 					goto fail_res_conflict;
263 			}
264 		}
265 	}
266 
267 	ret = platform_device_add_resources(pdev, res, cell->num_resources);
268 	if (ret)
269 		goto fail_res_conflict;
270 
271 	ret = platform_device_add(pdev);
272 	if (ret)
273 		goto fail_res_conflict;
274 
275 	if (cell->pm_runtime_no_callbacks)
276 		pm_runtime_no_callbacks(&pdev->dev);
277 
278 	kfree(res);
279 
280 	return 0;
281 
282 fail_res_conflict:
283 	if (cell->swnode)
284 		device_remove_software_node(&pdev->dev);
285 fail_of_entry:
286 	list_for_each_entry_safe(of_entry, tmp, &mfd_of_node_list, list)
287 		if (of_entry->dev == &pdev->dev) {
288 			list_del(&of_entry->list);
289 			kfree(of_entry);
290 		}
291 fail_alias:
292 	regulator_bulk_unregister_supply_alias(&pdev->dev,
293 					       cell->parent_supplies,
294 					       cell->num_parent_supplies);
295 fail_res:
296 	kfree(res);
297 fail_device:
298 	platform_device_put(pdev);
299 fail_alloc:
300 	return ret;
301 }
302 
303 /**
304  * mfd_add_devices - register child devices
305  *
306  * @parent:	Pointer to parent device.
307  * @id:		Can be PLATFORM_DEVID_AUTO to let the Platform API take care
308  *		of device numbering, or will be added to a device's cell_id.
309  * @cells:	Array of (struct mfd_cell)s describing child devices.
310  * @n_devs:	Number of child devices to register.
311  * @mem_base:	Parent register range resource for child devices.
312  * @irq_base:	Base of the range of virtual interrupt numbers allocated for
313  *		this MFD device. Unused if @domain is specified.
314  * @domain:	Interrupt domain to create mappings for hardware interrupts.
315  */
316 int mfd_add_devices(struct device *parent, int id,
317 		    const struct mfd_cell *cells, int n_devs,
318 		    struct resource *mem_base,
319 		    int irq_base, struct irq_domain *domain)
320 {
321 	int i;
322 	int ret;
323 
324 	for (i = 0; i < n_devs; i++) {
325 		ret = mfd_add_device(parent, id, cells + i, mem_base,
326 				     irq_base, domain);
327 		if (ret)
328 			goto fail;
329 	}
330 
331 	return 0;
332 
333 fail:
334 	if (i)
335 		mfd_remove_devices(parent);
336 
337 	return ret;
338 }
339 EXPORT_SYMBOL(mfd_add_devices);
340 
341 static int mfd_remove_devices_fn(struct device *dev, void *data)
342 {
343 	struct platform_device *pdev;
344 	const struct mfd_cell *cell;
345 	struct mfd_of_node_entry *of_entry, *tmp;
346 	int *level = data;
347 
348 	if (dev->type != &mfd_dev_type)
349 		return 0;
350 
351 	pdev = to_platform_device(dev);
352 	cell = mfd_get_cell(pdev);
353 
354 	if (level && cell->level > *level)
355 		return 0;
356 
357 	if (cell->swnode)
358 		device_remove_software_node(&pdev->dev);
359 
360 	list_for_each_entry_safe(of_entry, tmp, &mfd_of_node_list, list)
361 		if (of_entry->dev == &pdev->dev) {
362 			list_del(&of_entry->list);
363 			kfree(of_entry);
364 		}
365 
366 	regulator_bulk_unregister_supply_alias(dev, cell->parent_supplies,
367 					       cell->num_parent_supplies);
368 
369 	platform_device_unregister(pdev);
370 	return 0;
371 }
372 
373 void mfd_remove_devices_late(struct device *parent)
374 {
375 	int level = MFD_DEP_LEVEL_HIGH;
376 
377 	device_for_each_child_reverse(parent, &level, mfd_remove_devices_fn);
378 }
379 EXPORT_SYMBOL(mfd_remove_devices_late);
380 
381 void mfd_remove_devices(struct device *parent)
382 {
383 	int level = MFD_DEP_LEVEL_NORMAL;
384 
385 	device_for_each_child_reverse(parent, &level, mfd_remove_devices_fn);
386 }
387 EXPORT_SYMBOL(mfd_remove_devices);
388 
389 static void devm_mfd_dev_release(struct device *dev, void *res)
390 {
391 	mfd_remove_devices(dev);
392 }
393 
394 /**
395  * devm_mfd_add_devices - Resource managed version of mfd_add_devices()
396  *
397  * Returns 0 on success or an appropriate negative error number on failure.
398  * All child-devices of the MFD will automatically be removed when it gets
399  * unbinded.
400  *
401  * @dev:	Pointer to parent device.
402  * @id:		Can be PLATFORM_DEVID_AUTO to let the Platform API take care
403  *		of device numbering, or will be added to a device's cell_id.
404  * @cells:	Array of (struct mfd_cell)s describing child devices.
405  * @n_devs:	Number of child devices to register.
406  * @mem_base:	Parent register range resource for child devices.
407  * @irq_base:	Base of the range of virtual interrupt numbers allocated for
408  *		this MFD device. Unused if @domain is specified.
409  * @domain:	Interrupt domain to create mappings for hardware interrupts.
410  */
411 int devm_mfd_add_devices(struct device *dev, int id,
412 			 const struct mfd_cell *cells, int n_devs,
413 			 struct resource *mem_base,
414 			 int irq_base, struct irq_domain *domain)
415 {
416 	struct device **ptr;
417 	int ret;
418 
419 	ptr = devres_alloc(devm_mfd_dev_release, sizeof(*ptr), GFP_KERNEL);
420 	if (!ptr)
421 		return -ENOMEM;
422 
423 	ret = mfd_add_devices(dev, id, cells, n_devs, mem_base,
424 			      irq_base, domain);
425 	if (ret < 0) {
426 		devres_free(ptr);
427 		return ret;
428 	}
429 
430 	*ptr = dev;
431 	devres_add(dev, ptr);
432 
433 	return ret;
434 }
435 EXPORT_SYMBOL(devm_mfd_add_devices);
436 
437 MODULE_LICENSE("GPL");
438 MODULE_AUTHOR("Ian Molton, Dmitry Baryshkov");
439