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