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