blob: fc00aaccb5f72979868991c6383ea4b78fa4ed47 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Dmitry Baryshkovaa613de2008-06-27 10:37:19 +01002/*
3 * drivers/mfd/mfd-core.c
4 *
5 * core MFD support
6 * Copyright (c) 2006 Ian Molton
7 * Copyright (c) 2007,2008 Dmitry Baryshkov
Dmitry Baryshkovaa613de2008-06-27 10:37:19 +01008 */
9
10#include <linux/kernel.h>
11#include <linux/platform_device.h>
Samuel Ortiz91feded2010-02-19 11:07:59 +010012#include <linux/acpi.h>
Lee Jones466a62d2020-06-11 07:35:33 +010013#include <linux/list.h>
Andy Shevchenko4d215ca2015-11-30 17:11:40 +020014#include <linux/property.h>
Dmitry Baryshkovaa613de2008-06-27 10:37:19 +010015#include <linux/mfd/core.h>
Mark Brown4c90aa92010-11-26 17:19:34 +000016#include <linux/pm_runtime.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Paul Gortmaker4e36dd32011-07-03 15:13:27 -040018#include <linux/module.h>
Lee Jonesc94bb232012-06-29 19:01:03 +020019#include <linux/irqdomain.h>
20#include <linux/of.h>
Lee Jones466a62d2020-06-11 07:35:33 +010021#include <linux/of_address.h>
Charles Keepax7fcd4272013-10-15 20:14:21 +010022#include <linux/regulator/consumer.h>
Dmitry Baryshkovaa613de2008-06-27 10:37:19 +010023
Lee Jones466a62d2020-06-11 07:35:33 +010024static 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
Charles Keepaxb9fbb622012-11-09 16:15:28 +000032static struct device_type mfd_dev_type = {
33 .name = "mfd_device",
34};
35
Andres Salomonf77289a2011-03-03 09:51:58 -080036int mfd_cell_enable(struct platform_device *pdev)
Andres Salomon1e29af62011-02-17 19:07:34 -080037{
38 const struct mfd_cell *cell = mfd_get_cell(pdev);
Andres Salomon1e29af62011-02-17 19:07:34 -080039
Lee Jonesb195e1012019-10-21 10:16:34 +010040 if (!cell->enable) {
41 dev_dbg(&pdev->dev, "No .enable() call-back registered\n");
42 return 0;
43 }
44
Lee Jones5a47c0f2019-10-21 10:47:37 +010045 return cell->enable(pdev);
Andres Salomon1e29af62011-02-17 19:07:34 -080046}
Andres Salomonf77289a2011-03-03 09:51:58 -080047EXPORT_SYMBOL(mfd_cell_enable);
Andres Salomon1e29af62011-02-17 19:07:34 -080048
Andres Salomonf77289a2011-03-03 09:51:58 -080049int mfd_cell_disable(struct platform_device *pdev)
Andres Salomon1e29af62011-02-17 19:07:34 -080050{
51 const struct mfd_cell *cell = mfd_get_cell(pdev);
Andres Salomon1e29af62011-02-17 19:07:34 -080052
Lee Jonesb195e1012019-10-21 10:16:34 +010053 if (!cell->disable) {
54 dev_dbg(&pdev->dev, "No .disable() call-back registered\n");
55 return 0;
56 }
57
Lee Jones5a47c0f2019-10-21 10:47:37 +010058 return cell->disable(pdev);
Andres Salomon1e29af62011-02-17 19:07:34 -080059}
Andres Salomonf77289a2011-03-03 09:51:58 -080060EXPORT_SYMBOL(mfd_cell_disable);
Andres Salomon1e29af62011-02-17 19:07:34 -080061
Mika Westerberg6ab34302014-09-16 14:52:36 +030062#if IS_ENABLED(CONFIG_ACPI)
63static void mfd_acpi_add_device(const struct mfd_cell *cell,
64 struct platform_device *pdev)
65{
Andy Shevchenko98a3be42015-10-23 12:16:41 +030066 const struct mfd_cell_acpi_match *match = cell->acpi_match;
67 struct acpi_device *parent, *child;
Mika Westerberg6ab34302014-09-16 14:52:36 +030068 struct acpi_device *adev;
69
Andy Shevchenko98a3be42015-10-23 12:16:41 +030070 parent = ACPI_COMPANION(pdev->dev.parent);
71 if (!parent)
Mika Westerberg6ab34302014-09-16 14:52:36 +030072 return;
73
74 /*
Andy Shevchenko98a3be42015-10-23 12:16:41 +030075 * MFD child device gets its ACPI handle either from the ACPI device
76 * directly under the parent that matches the either _HID or _CID, or
77 * _ADR or it will use the parent handle if is no ID is given.
78 *
79 * Note that use of _ADR is a grey area in the ACPI specification,
80 * though Intel Galileo Gen2 is using it to distinguish the children
81 * devices.
Mika Westerberg6ab34302014-09-16 14:52:36 +030082 */
Andy Shevchenko98a3be42015-10-23 12:16:41 +030083 adev = parent;
84 if (match) {
85 if (match->pnpid) {
86 struct acpi_device_id ids[2] = {};
Mika Westerberg6ab34302014-09-16 14:52:36 +030087
Andy Shevchenko98a3be42015-10-23 12:16:41 +030088 strlcpy(ids[0].id, match->pnpid, sizeof(ids[0].id));
89 list_for_each_entry(child, &parent->children, node) {
Irina Tirdeaee414de2016-03-13 03:02:58 +020090 if (!acpi_match_device_ids(child, ids)) {
Andy Shevchenko98a3be42015-10-23 12:16:41 +030091 adev = child;
92 break;
93 }
Mika Westerberg6ab34302014-09-16 14:52:36 +030094 }
Andy Shevchenko98a3be42015-10-23 12:16:41 +030095 } else {
96 unsigned long long adr;
97 acpi_status status;
98
99 list_for_each_entry(child, &parent->children, node) {
100 status = acpi_evaluate_integer(child->handle,
101 "_ADR", NULL,
102 &adr);
103 if (ACPI_SUCCESS(status) && match->adr == adr) {
104 adev = child;
105 break;
106 }
107 }
108 }
Mika Westerberg6ab34302014-09-16 14:52:36 +0300109 }
110
111 ACPI_COMPANION_SET(&pdev->dev, adev);
112}
113#else
114static inline void mfd_acpi_add_device(const struct mfd_cell *cell,
115 struct platform_device *pdev)
116{
117}
118#endif
119
Lee Jones466a62d2020-06-11 07:35:33 +0100120static int mfd_match_of_node_to_dev(struct platform_device *pdev,
121 struct device_node *np,
122 const struct mfd_cell *cell)
123{
124#if IS_ENABLED(CONFIG_OF)
125 struct mfd_of_node_entry *of_entry;
126 const __be32 *reg;
127 u64 of_node_addr;
128
Lee Jones466a62d2020-06-11 07:35:33 +0100129 /* Skip if OF node has previously been allocated to a device */
130 list_for_each_entry(of_entry, &mfd_of_node_list, list)
131 if (of_entry->np == np)
132 return -EAGAIN;
133
134 if (!cell->use_of_reg)
135 /* No of_reg defined - allocate first free compatible match */
136 goto allocate_of_node;
137
138 /* We only care about each node's first defined address */
139 reg = of_get_address(np, 0, NULL, NULL);
140 if (!reg)
141 /* OF node does not contatin a 'reg' property to match to */
142 return -EAGAIN;
143
144 of_node_addr = of_read_number(reg, of_n_addr_cells(np));
145
146 if (cell->of_reg != of_node_addr)
147 /* No match */
148 return -EAGAIN;
149
150allocate_of_node:
151 of_entry = kzalloc(sizeof(*of_entry), GFP_KERNEL);
152 if (!of_entry)
153 return -ENOMEM;
154
155 of_entry->dev = &pdev->dev;
156 of_entry->np = np;
157 list_add_tail(&of_entry->list, &mfd_of_node_list);
158
159 pdev->dev.of_node = np;
160 pdev->dev.fwnode = &np->fwnode;
161#endif
162 return 0;
163}
164
Dmitry Baryshkov424f5252008-07-29 01:30:26 +0200165static int mfd_add_device(struct device *parent, int id,
Lee Jones5a47c0f2019-10-21 10:47:37 +0100166 const struct mfd_cell *cell,
Ben Dooks7f71ac92008-07-28 18:29:09 +0200167 struct resource *mem_base,
Mark Brown0848c942012-09-11 15:16:36 +0800168 int irq_base, struct irq_domain *domain)
Dmitry Baryshkovaa613de2008-06-27 10:37:19 +0100169{
Ian Moltona87903f2008-07-25 22:59:29 +0100170 struct resource *res;
Dmitry Baryshkovaa613de2008-06-27 10:37:19 +0100171 struct platform_device *pdev;
Lee Jonesc94bb232012-06-29 19:01:03 +0200172 struct device_node *np = NULL;
Lee Jones466a62d2020-06-11 07:35:33 +0100173 struct mfd_of_node_entry *of_entry, *tmp;
Dmitry Baryshkovaa613de2008-06-27 10:37:19 +0100174 int ret = -ENOMEM;
Johan Hovold6e3f62f2014-09-26 12:55:33 +0200175 int platform_id;
Dmitry Baryshkovaa613de2008-06-27 10:37:19 +0100176 int r;
177
Johan Hovolda77c50b2015-03-25 12:07:05 +0100178 if (id == PLATFORM_DEVID_AUTO)
Johan Hovold6e3f62f2014-09-26 12:55:33 +0200179 platform_id = id;
180 else
181 platform_id = id + cell->id;
182
183 pdev = platform_device_alloc(cell->name, platform_id);
Dmitry Baryshkovaa613de2008-06-27 10:37:19 +0100184 if (!pdev)
185 goto fail_alloc;
186
Lee Jonesb944a682019-10-21 10:55:23 +0100187 pdev->mfd_cell = kmemdup(cell, sizeof(*cell), GFP_KERNEL);
188 if (!pdev->mfd_cell)
189 goto fail_device;
190
Kees Cook6396bb22018-06-12 14:03:40 -0700191 res = kcalloc(cell->num_resources, sizeof(*res), GFP_KERNEL);
Ian Moltona87903f2008-07-25 22:59:29 +0100192 if (!res)
193 goto fail_device;
194
Dmitry Baryshkov424f5252008-07-29 01:30:26 +0200195 pdev->dev.parent = parent;
Charles Keepaxb9fbb622012-11-09 16:15:28 +0000196 pdev->dev.type = &mfd_dev_type;
Benedikt Sprangerb018e132013-08-13 11:08:38 +0200197 pdev->dev.dma_mask = parent->dma_mask;
198 pdev->dev.dma_parms = parent->dma_parms;
Boris BREZILLON4f08df12014-09-22 21:37:55 +0200199 pdev->dev.coherent_dma_mask = parent->coherent_dma_mask;
Dmitry Baryshkovaa613de2008-06-27 10:37:19 +0100200
Charles Keepaxd137be02014-04-24 18:27:25 +0100201 ret = regulator_bulk_register_supply_alias(
Charles Keepax7fcd4272013-10-15 20:14:21 +0100202 &pdev->dev, cell->parent_supplies,
203 parent, cell->parent_supplies,
204 cell->num_parent_supplies);
205 if (ret < 0)
206 goto fail_res;
207
Lee Jones466a62d2020-06-11 07:35:33 +0100208 if (IS_ENABLED(CONFIG_OF) && parent->of_node && cell->of_compatible) {
Lee Jonesc94bb232012-06-29 19:01:03 +0200209 for_each_child_of_node(parent->of_node, np) {
210 if (of_device_is_compatible(np, cell->of_compatible)) {
Lee Jones22380b62020-08-19 09:16:50 +0100211 /* Ignore 'disabled' devices error free */
212 if (!of_device_is_available(np)) {
213 ret = 0;
214 goto fail_alias;
215 }
216
Lee Jones466a62d2020-06-11 07:35:33 +0100217 ret = mfd_match_of_node_to_dev(pdev, np, cell);
218 if (ret == -EAGAIN)
219 continue;
220 if (ret)
Lee Jones6b5c3502019-11-07 11:19:50 +0000221 goto fail_alias;
Lee Jones466a62d2020-06-11 07:35:33 +0100222
Lee Jonesc94bb232012-06-29 19:01:03 +0200223 break;
224 }
225 }
Lee Jones466a62d2020-06-11 07:35:33 +0100226
227 if (!pdev->dev.of_node)
228 pr_warn("%s: Failed to locate of_node [id: %d]\n",
229 cell->name, platform_id);
Lee Jonesc94bb232012-06-29 19:01:03 +0200230 }
231
Mika Westerberg6ab34302014-09-16 14:52:36 +0300232 mfd_acpi_add_device(cell, pdev);
233
Samuel Ortizeb895602011-04-06 16:52:52 +0200234 if (cell->pdata_size) {
235 ret = platform_device_add_data(pdev,
236 cell->platform_data, cell->pdata_size);
237 if (ret)
Lee Jones466a62d2020-06-11 07:35:33 +0100238 goto fail_of_entry;
Samuel Ortizeb895602011-04-06 16:52:52 +0200239 }
240
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300241 if (cell->properties) {
242 ret = platform_device_add_properties(pdev, cell->properties);
Andy Shevchenko4d215ca2015-11-30 17:11:40 +0200243 if (ret)
Lee Jones466a62d2020-06-11 07:35:33 +0100244 goto fail_of_entry;
Andy Shevchenko4d215ca2015-11-30 17:11:40 +0200245 }
246
Dmitry Baryshkovaa613de2008-06-27 10:37:19 +0100247 for (r = 0; r < cell->num_resources; r++) {
248 res[r].name = cell->resources[r].name;
249 res[r].flags = cell->resources[r].flags;
250
251 /* Find out base to use */
Samuel Ortizf03cfcb2010-03-26 01:09:04 +0100252 if ((cell->resources[r].flags & IORESOURCE_MEM) && mem_base) {
Dmitry Baryshkovaa613de2008-06-27 10:37:19 +0100253 res[r].parent = mem_base;
254 res[r].start = mem_base->start +
255 cell->resources[r].start;
256 res[r].end = mem_base->start +
257 cell->resources[r].end;
258 } else if (cell->resources[r].flags & IORESOURCE_IRQ) {
Lee Jonesc94bb232012-06-29 19:01:03 +0200259 if (domain) {
260 /* Unable to create mappings for IRQ ranges. */
261 WARN_ON(cell->resources[r].start !=
262 cell->resources[r].end);
263 res[r].start = res[r].end = irq_create_mapping(
264 domain, cell->resources[r].start);
265 } else {
266 res[r].start = irq_base +
267 cell->resources[r].start;
268 res[r].end = irq_base +
269 cell->resources[r].end;
270 }
Dmitry Baryshkovaa613de2008-06-27 10:37:19 +0100271 } else {
272 res[r].parent = cell->resources[r].parent;
273 res[r].start = cell->resources[r].start;
274 res[r].end = cell->resources[r].end;
275 }
Samuel Ortiz91feded2010-02-19 11:07:59 +0100276
Daniel Drake5f2545f2010-09-30 21:55:36 +0100277 if (!cell->ignore_resource_conflicts) {
Lorenzo Pieralisiec40c602015-05-01 10:41:10 +0100278 if (has_acpi_companion(&pdev->dev)) {
279 ret = acpi_check_resource_conflict(&res[r]);
280 if (ret)
Lee Jones466a62d2020-06-11 07:35:33 +0100281 goto fail_of_entry;
Lorenzo Pieralisiec40c602015-05-01 10:41:10 +0100282 }
Daniel Drake5f2545f2010-09-30 21:55:36 +0100283 }
Dmitry Baryshkovaa613de2008-06-27 10:37:19 +0100284 }
285
Axel Lin8af5fe32010-05-31 17:30:55 +0800286 ret = platform_device_add_resources(pdev, res, cell->num_resources);
287 if (ret)
Lee Jones466a62d2020-06-11 07:35:33 +0100288 goto fail_of_entry;
Dmitry Baryshkovaa613de2008-06-27 10:37:19 +0100289
290 ret = platform_device_add(pdev);
291 if (ret)
Lee Jones466a62d2020-06-11 07:35:33 +0100292 goto fail_of_entry;
Ian Moltona87903f2008-07-25 22:59:29 +0100293
Mark Brown4c90aa92010-11-26 17:19:34 +0000294 if (cell->pm_runtime_no_callbacks)
295 pm_runtime_no_callbacks(&pdev->dev);
296
Ian Moltona87903f2008-07-25 22:59:29 +0100297 kfree(res);
Dmitry Baryshkovaa613de2008-06-27 10:37:19 +0100298
299 return 0;
300
Lee Jones466a62d2020-06-11 07:35:33 +0100301fail_of_entry:
302 list_for_each_entry_safe(of_entry, tmp, &mfd_of_node_list, list)
303 if (of_entry->dev == &pdev->dev) {
304 list_del(&of_entry->list);
305 kfree(of_entry);
306 }
Charles Keepax7fcd4272013-10-15 20:14:21 +0100307fail_alias:
Charles Keepaxd137be02014-04-24 18:27:25 +0100308 regulator_bulk_unregister_supply_alias(&pdev->dev,
309 cell->parent_supplies,
310 cell->num_parent_supplies);
Ian Moltona87903f2008-07-25 22:59:29 +0100311fail_res:
312 kfree(res);
Dmitry Baryshkovaa613de2008-06-27 10:37:19 +0100313fail_device:
314 platform_device_put(pdev);
315fail_alloc:
316 return ret;
317}
318
Bartosz Golaszewski1946f9962019-04-23 11:04:45 +0200319/**
320 * mfd_add_devices - register child devices
321 *
322 * @parent: Pointer to parent device.
323 * @id: Can be PLATFORM_DEVID_AUTO to let the Platform API take care
324 * of device numbering, or will be added to a device's cell_id.
325 * @cells: Array of (struct mfd_cell)s describing child devices.
326 * @n_devs: Number of child devices to register.
327 * @mem_base: Parent register range resource for child devices.
328 * @irq_base: Base of the range of virtual interrupt numbers allocated for
329 * this MFD device. Unused if @domain is specified.
330 * @domain: Interrupt domain to create mappings for hardware interrupts.
331 */
Dmitry Baryshkov424f5252008-07-29 01:30:26 +0200332int mfd_add_devices(struct device *parent, int id,
Geert Uytterhoeven03e361b2013-10-29 10:03:04 +0100333 const struct mfd_cell *cells, int n_devs,
Ben Dooks7f71ac92008-07-28 18:29:09 +0200334 struct resource *mem_base,
Mark Brown0848c942012-09-11 15:16:36 +0800335 int irq_base, struct irq_domain *domain)
Dmitry Baryshkovaa613de2008-06-27 10:37:19 +0100336{
337 int i;
Geert Uytterhoeven0b208e42013-10-29 15:47:22 +0100338 int ret;
Dmitry Baryshkovaa613de2008-06-27 10:37:19 +0100339
340 for (i = 0; i < n_devs; i++) {
Lee Jones5a47c0f2019-10-21 10:47:37 +0100341 ret = mfd_add_device(parent, id, cells + i, mem_base,
Mark Brown0848c942012-09-11 15:16:36 +0800342 irq_base, domain);
Dmitry Baryshkovaa613de2008-06-27 10:37:19 +0100343 if (ret)
Geert Uytterhoeven0b208e42013-10-29 15:47:22 +0100344 goto fail;
Dmitry Baryshkovaa613de2008-06-27 10:37:19 +0100345 }
346
Geert Uytterhoeven0b208e42013-10-29 15:47:22 +0100347 return 0;
Dmitry Baryshkovaa613de2008-06-27 10:37:19 +0100348
Geert Uytterhoeven0b208e42013-10-29 15:47:22 +0100349fail:
350 if (i)
351 mfd_remove_devices(parent);
Lee Jones5a47c0f2019-10-21 10:47:37 +0100352
Dmitry Baryshkovaa613de2008-06-27 10:37:19 +0100353 return ret;
354}
355EXPORT_SYMBOL(mfd_add_devices);
356
Lee Jones5a47c0f2019-10-21 10:47:37 +0100357static int mfd_remove_devices_fn(struct device *dev, void *data)
Dmitry Baryshkovaa613de2008-06-27 10:37:19 +0100358{
Charles Keepaxb9fbb622012-11-09 16:15:28 +0000359 struct platform_device *pdev;
360 const struct mfd_cell *cell;
Charles Keepax114294d2020-07-23 11:54:58 +0100361 int *level = data;
Andres Salomon1e29af62011-02-17 19:07:34 -0800362
Charles Keepaxb9fbb622012-11-09 16:15:28 +0000363 if (dev->type != &mfd_dev_type)
364 return 0;
365
366 pdev = to_platform_device(dev);
367 cell = mfd_get_cell(pdev);
368
Charles Keepax114294d2020-07-23 11:54:58 +0100369 if (level && cell->level > *level)
370 return 0;
371
Charles Keepaxd137be02014-04-24 18:27:25 +0100372 regulator_bulk_unregister_supply_alias(dev, cell->parent_supplies,
373 cell->num_parent_supplies);
374
Andres Salomon1e29af62011-02-17 19:07:34 -0800375 platform_device_unregister(pdev);
Dmitry Baryshkovaa613de2008-06-27 10:37:19 +0100376 return 0;
377}
378
Charles Keepax114294d2020-07-23 11:54:58 +0100379void mfd_remove_devices_late(struct device *parent)
380{
381 int level = MFD_DEP_LEVEL_HIGH;
382
383 device_for_each_child_reverse(parent, &level, mfd_remove_devices_fn);
384}
385EXPORT_SYMBOL(mfd_remove_devices_late);
386
Dmitry Baryshkov424f5252008-07-29 01:30:26 +0200387void mfd_remove_devices(struct device *parent)
Dmitry Baryshkovaa613de2008-06-27 10:37:19 +0100388{
Charles Keepax114294d2020-07-23 11:54:58 +0100389 int level = MFD_DEP_LEVEL_NORMAL;
390
391 device_for_each_child_reverse(parent, &level, mfd_remove_devices_fn);
Dmitry Baryshkovaa613de2008-06-27 10:37:19 +0100392}
393EXPORT_SYMBOL(mfd_remove_devices);
394
Laxman Dewangana8f447be2016-04-08 00:12:55 +0530395static void devm_mfd_dev_release(struct device *dev, void *res)
396{
397 mfd_remove_devices(dev);
398}
399
400/**
401 * devm_mfd_add_devices - Resource managed version of mfd_add_devices()
402 *
403 * Returns 0 on success or an appropriate negative error number on failure.
404 * All child-devices of the MFD will automatically be removed when it gets
405 * unbinded.
Lee Jones5a0ffef2020-06-23 15:51:28 +0100406 *
407 * @dev: Pointer to parent device.
408 * @id: Can be PLATFORM_DEVID_AUTO to let the Platform API take care
409 * of device numbering, or will be added to a device's cell_id.
410 * @cells: Array of (struct mfd_cell)s describing child devices.
411 * @n_devs: Number of child devices to register.
412 * @mem_base: Parent register range resource for child devices.
413 * @irq_base: Base of the range of virtual interrupt numbers allocated for
414 * this MFD device. Unused if @domain is specified.
415 * @domain: Interrupt domain to create mappings for hardware interrupts.
Laxman Dewangana8f447be2016-04-08 00:12:55 +0530416 */
417int devm_mfd_add_devices(struct device *dev, int id,
418 const struct mfd_cell *cells, int n_devs,
419 struct resource *mem_base,
420 int irq_base, struct irq_domain *domain)
421{
422 struct device **ptr;
423 int ret;
424
425 ptr = devres_alloc(devm_mfd_dev_release, sizeof(*ptr), GFP_KERNEL);
426 if (!ptr)
427 return -ENOMEM;
428
429 ret = mfd_add_devices(dev, id, cells, n_devs, mem_base,
430 irq_base, domain);
431 if (ret < 0) {
432 devres_free(ptr);
433 return ret;
434 }
435
436 *ptr = dev;
437 devres_add(dev, ptr);
438
439 return ret;
440}
441EXPORT_SYMBOL(devm_mfd_add_devices);
442
Dmitry Baryshkovaa613de2008-06-27 10:37:19 +0100443MODULE_LICENSE("GPL");
444MODULE_AUTHOR("Ian Molton, Dmitry Baryshkov");