Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Dmitry Baryshkov | aa613de | 2008-06-27 10:37:19 +0100 | [diff] [blame] | 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 |
Dmitry Baryshkov | aa613de | 2008-06-27 10:37:19 +0100 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <linux/kernel.h> |
| 11 | #include <linux/platform_device.h> |
Samuel Ortiz | 91feded | 2010-02-19 11:07:59 +0100 | [diff] [blame] | 12 | #include <linux/acpi.h> |
Lee Jones | 466a62d | 2020-06-11 07:35:33 +0100 | [diff] [blame] | 13 | #include <linux/list.h> |
Andy Shevchenko | 4d215ca | 2015-11-30 17:11:40 +0200 | [diff] [blame] | 14 | #include <linux/property.h> |
Dmitry Baryshkov | aa613de | 2008-06-27 10:37:19 +0100 | [diff] [blame] | 15 | #include <linux/mfd/core.h> |
Mark Brown | 4c90aa9 | 2010-11-26 17:19:34 +0000 | [diff] [blame] | 16 | #include <linux/pm_runtime.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 17 | #include <linux/slab.h> |
Paul Gortmaker | 4e36dd3 | 2011-07-03 15:13:27 -0400 | [diff] [blame] | 18 | #include <linux/module.h> |
Lee Jones | c94bb23 | 2012-06-29 19:01:03 +0200 | [diff] [blame] | 19 | #include <linux/irqdomain.h> |
| 20 | #include <linux/of.h> |
Lee Jones | 466a62d | 2020-06-11 07:35:33 +0100 | [diff] [blame] | 21 | #include <linux/of_address.h> |
Charles Keepax | 7fcd427 | 2013-10-15 20:14:21 +0100 | [diff] [blame] | 22 | #include <linux/regulator/consumer.h> |
Dmitry Baryshkov | aa613de | 2008-06-27 10:37:19 +0100 | [diff] [blame] | 23 | |
Lee Jones | 466a62d | 2020-06-11 07:35:33 +0100 | [diff] [blame] | 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 | |
Charles Keepax | b9fbb62 | 2012-11-09 16:15:28 +0000 | [diff] [blame] | 32 | static struct device_type mfd_dev_type = { |
| 33 | .name = "mfd_device", |
| 34 | }; |
| 35 | |
Andres Salomon | f77289a | 2011-03-03 09:51:58 -0800 | [diff] [blame] | 36 | int mfd_cell_enable(struct platform_device *pdev) |
Andres Salomon | 1e29af6 | 2011-02-17 19:07:34 -0800 | [diff] [blame] | 37 | { |
| 38 | const struct mfd_cell *cell = mfd_get_cell(pdev); |
Andres Salomon | 1e29af6 | 2011-02-17 19:07:34 -0800 | [diff] [blame] | 39 | |
Lee Jones | b195e101 | 2019-10-21 10:16:34 +0100 | [diff] [blame] | 40 | if (!cell->enable) { |
| 41 | dev_dbg(&pdev->dev, "No .enable() call-back registered\n"); |
| 42 | return 0; |
| 43 | } |
| 44 | |
Lee Jones | 5a47c0f | 2019-10-21 10:47:37 +0100 | [diff] [blame] | 45 | return cell->enable(pdev); |
Andres Salomon | 1e29af6 | 2011-02-17 19:07:34 -0800 | [diff] [blame] | 46 | } |
Andres Salomon | f77289a | 2011-03-03 09:51:58 -0800 | [diff] [blame] | 47 | EXPORT_SYMBOL(mfd_cell_enable); |
Andres Salomon | 1e29af6 | 2011-02-17 19:07:34 -0800 | [diff] [blame] | 48 | |
Andres Salomon | f77289a | 2011-03-03 09:51:58 -0800 | [diff] [blame] | 49 | int mfd_cell_disable(struct platform_device *pdev) |
Andres Salomon | 1e29af6 | 2011-02-17 19:07:34 -0800 | [diff] [blame] | 50 | { |
| 51 | const struct mfd_cell *cell = mfd_get_cell(pdev); |
Andres Salomon | 1e29af6 | 2011-02-17 19:07:34 -0800 | [diff] [blame] | 52 | |
Lee Jones | b195e101 | 2019-10-21 10:16:34 +0100 | [diff] [blame] | 53 | if (!cell->disable) { |
| 54 | dev_dbg(&pdev->dev, "No .disable() call-back registered\n"); |
| 55 | return 0; |
| 56 | } |
| 57 | |
Lee Jones | 5a47c0f | 2019-10-21 10:47:37 +0100 | [diff] [blame] | 58 | return cell->disable(pdev); |
Andres Salomon | 1e29af6 | 2011-02-17 19:07:34 -0800 | [diff] [blame] | 59 | } |
Andres Salomon | f77289a | 2011-03-03 09:51:58 -0800 | [diff] [blame] | 60 | EXPORT_SYMBOL(mfd_cell_disable); |
Andres Salomon | 1e29af6 | 2011-02-17 19:07:34 -0800 | [diff] [blame] | 61 | |
Mika Westerberg | 6ab3430 | 2014-09-16 14:52:36 +0300 | [diff] [blame] | 62 | #if IS_ENABLED(CONFIG_ACPI) |
| 63 | static void mfd_acpi_add_device(const struct mfd_cell *cell, |
| 64 | struct platform_device *pdev) |
| 65 | { |
Andy Shevchenko | 98a3be4 | 2015-10-23 12:16:41 +0300 | [diff] [blame] | 66 | const struct mfd_cell_acpi_match *match = cell->acpi_match; |
| 67 | struct acpi_device *parent, *child; |
Mika Westerberg | 6ab3430 | 2014-09-16 14:52:36 +0300 | [diff] [blame] | 68 | struct acpi_device *adev; |
| 69 | |
Andy Shevchenko | 98a3be4 | 2015-10-23 12:16:41 +0300 | [diff] [blame] | 70 | parent = ACPI_COMPANION(pdev->dev.parent); |
| 71 | if (!parent) |
Mika Westerberg | 6ab3430 | 2014-09-16 14:52:36 +0300 | [diff] [blame] | 72 | return; |
| 73 | |
| 74 | /* |
Andy Shevchenko | 98a3be4 | 2015-10-23 12:16:41 +0300 | [diff] [blame] | 75 | * 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 Westerberg | 6ab3430 | 2014-09-16 14:52:36 +0300 | [diff] [blame] | 82 | */ |
Andy Shevchenko | 98a3be4 | 2015-10-23 12:16:41 +0300 | [diff] [blame] | 83 | adev = parent; |
| 84 | if (match) { |
| 85 | if (match->pnpid) { |
| 86 | struct acpi_device_id ids[2] = {}; |
Mika Westerberg | 6ab3430 | 2014-09-16 14:52:36 +0300 | [diff] [blame] | 87 | |
Andy Shevchenko | 98a3be4 | 2015-10-23 12:16:41 +0300 | [diff] [blame] | 88 | strlcpy(ids[0].id, match->pnpid, sizeof(ids[0].id)); |
| 89 | list_for_each_entry(child, &parent->children, node) { |
Irina Tirdea | ee414de | 2016-03-13 03:02:58 +0200 | [diff] [blame] | 90 | if (!acpi_match_device_ids(child, ids)) { |
Andy Shevchenko | 98a3be4 | 2015-10-23 12:16:41 +0300 | [diff] [blame] | 91 | adev = child; |
| 92 | break; |
| 93 | } |
Mika Westerberg | 6ab3430 | 2014-09-16 14:52:36 +0300 | [diff] [blame] | 94 | } |
Andy Shevchenko | 98a3be4 | 2015-10-23 12:16:41 +0300 | [diff] [blame] | 95 | } 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 Westerberg | 6ab3430 | 2014-09-16 14:52:36 +0300 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | ACPI_COMPANION_SET(&pdev->dev, adev); |
| 112 | } |
| 113 | #else |
| 114 | static inline void mfd_acpi_add_device(const struct mfd_cell *cell, |
| 115 | struct platform_device *pdev) |
| 116 | { |
| 117 | } |
| 118 | #endif |
| 119 | |
Lee Jones | 466a62d | 2020-06-11 07:35:33 +0100 | [diff] [blame] | 120 | static 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 Jones | 466a62d | 2020-06-11 07:35:33 +0100 | [diff] [blame] | 129 | /* 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 | |
| 150 | allocate_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 Baryshkov | 424f525 | 2008-07-29 01:30:26 +0200 | [diff] [blame] | 165 | static int mfd_add_device(struct device *parent, int id, |
Lee Jones | 5a47c0f | 2019-10-21 10:47:37 +0100 | [diff] [blame] | 166 | const struct mfd_cell *cell, |
Ben Dooks | 7f71ac9 | 2008-07-28 18:29:09 +0200 | [diff] [blame] | 167 | struct resource *mem_base, |
Mark Brown | 0848c94 | 2012-09-11 15:16:36 +0800 | [diff] [blame] | 168 | int irq_base, struct irq_domain *domain) |
Dmitry Baryshkov | aa613de | 2008-06-27 10:37:19 +0100 | [diff] [blame] | 169 | { |
Ian Molton | a87903f | 2008-07-25 22:59:29 +0100 | [diff] [blame] | 170 | struct resource *res; |
Dmitry Baryshkov | aa613de | 2008-06-27 10:37:19 +0100 | [diff] [blame] | 171 | struct platform_device *pdev; |
Lee Jones | c94bb23 | 2012-06-29 19:01:03 +0200 | [diff] [blame] | 172 | struct device_node *np = NULL; |
Lee Jones | 466a62d | 2020-06-11 07:35:33 +0100 | [diff] [blame] | 173 | struct mfd_of_node_entry *of_entry, *tmp; |
Dmitry Baryshkov | aa613de | 2008-06-27 10:37:19 +0100 | [diff] [blame] | 174 | int ret = -ENOMEM; |
Johan Hovold | 6e3f62f | 2014-09-26 12:55:33 +0200 | [diff] [blame] | 175 | int platform_id; |
Dmitry Baryshkov | aa613de | 2008-06-27 10:37:19 +0100 | [diff] [blame] | 176 | int r; |
| 177 | |
Johan Hovold | a77c50b | 2015-03-25 12:07:05 +0100 | [diff] [blame] | 178 | if (id == PLATFORM_DEVID_AUTO) |
Johan Hovold | 6e3f62f | 2014-09-26 12:55:33 +0200 | [diff] [blame] | 179 | platform_id = id; |
| 180 | else |
| 181 | platform_id = id + cell->id; |
| 182 | |
| 183 | pdev = platform_device_alloc(cell->name, platform_id); |
Dmitry Baryshkov | aa613de | 2008-06-27 10:37:19 +0100 | [diff] [blame] | 184 | if (!pdev) |
| 185 | goto fail_alloc; |
| 186 | |
Lee Jones | b944a68 | 2019-10-21 10:55:23 +0100 | [diff] [blame] | 187 | pdev->mfd_cell = kmemdup(cell, sizeof(*cell), GFP_KERNEL); |
| 188 | if (!pdev->mfd_cell) |
| 189 | goto fail_device; |
| 190 | |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 191 | res = kcalloc(cell->num_resources, sizeof(*res), GFP_KERNEL); |
Ian Molton | a87903f | 2008-07-25 22:59:29 +0100 | [diff] [blame] | 192 | if (!res) |
| 193 | goto fail_device; |
| 194 | |
Dmitry Baryshkov | 424f525 | 2008-07-29 01:30:26 +0200 | [diff] [blame] | 195 | pdev->dev.parent = parent; |
Charles Keepax | b9fbb62 | 2012-11-09 16:15:28 +0000 | [diff] [blame] | 196 | pdev->dev.type = &mfd_dev_type; |
Benedikt Spranger | b018e13 | 2013-08-13 11:08:38 +0200 | [diff] [blame] | 197 | pdev->dev.dma_mask = parent->dma_mask; |
| 198 | pdev->dev.dma_parms = parent->dma_parms; |
Boris BREZILLON | 4f08df1 | 2014-09-22 21:37:55 +0200 | [diff] [blame] | 199 | pdev->dev.coherent_dma_mask = parent->coherent_dma_mask; |
Dmitry Baryshkov | aa613de | 2008-06-27 10:37:19 +0100 | [diff] [blame] | 200 | |
Charles Keepax | d137be0 | 2014-04-24 18:27:25 +0100 | [diff] [blame] | 201 | ret = regulator_bulk_register_supply_alias( |
Charles Keepax | 7fcd427 | 2013-10-15 20:14:21 +0100 | [diff] [blame] | 202 | &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 Jones | 466a62d | 2020-06-11 07:35:33 +0100 | [diff] [blame] | 208 | if (IS_ENABLED(CONFIG_OF) && parent->of_node && cell->of_compatible) { |
Lee Jones | c94bb23 | 2012-06-29 19:01:03 +0200 | [diff] [blame] | 209 | for_each_child_of_node(parent->of_node, np) { |
| 210 | if (of_device_is_compatible(np, cell->of_compatible)) { |
Lee Jones | 22380b6 | 2020-08-19 09:16:50 +0100 | [diff] [blame] | 211 | /* Ignore 'disabled' devices error free */ |
| 212 | if (!of_device_is_available(np)) { |
| 213 | ret = 0; |
| 214 | goto fail_alias; |
| 215 | } |
| 216 | |
Lee Jones | 466a62d | 2020-06-11 07:35:33 +0100 | [diff] [blame] | 217 | ret = mfd_match_of_node_to_dev(pdev, np, cell); |
| 218 | if (ret == -EAGAIN) |
| 219 | continue; |
| 220 | if (ret) |
Lee Jones | 6b5c350 | 2019-11-07 11:19:50 +0000 | [diff] [blame] | 221 | goto fail_alias; |
Lee Jones | 466a62d | 2020-06-11 07:35:33 +0100 | [diff] [blame] | 222 | |
Lee Jones | c94bb23 | 2012-06-29 19:01:03 +0200 | [diff] [blame] | 223 | break; |
| 224 | } |
| 225 | } |
Lee Jones | 466a62d | 2020-06-11 07:35:33 +0100 | [diff] [blame] | 226 | |
| 227 | if (!pdev->dev.of_node) |
| 228 | pr_warn("%s: Failed to locate of_node [id: %d]\n", |
| 229 | cell->name, platform_id); |
Lee Jones | c94bb23 | 2012-06-29 19:01:03 +0200 | [diff] [blame] | 230 | } |
| 231 | |
Mika Westerberg | 6ab3430 | 2014-09-16 14:52:36 +0300 | [diff] [blame] | 232 | mfd_acpi_add_device(cell, pdev); |
| 233 | |
Samuel Ortiz | eb89560 | 2011-04-06 16:52:52 +0200 | [diff] [blame] | 234 | if (cell->pdata_size) { |
| 235 | ret = platform_device_add_data(pdev, |
| 236 | cell->platform_data, cell->pdata_size); |
| 237 | if (ret) |
Lee Jones | 466a62d | 2020-06-11 07:35:33 +0100 | [diff] [blame] | 238 | goto fail_of_entry; |
Samuel Ortiz | eb89560 | 2011-04-06 16:52:52 +0200 | [diff] [blame] | 239 | } |
| 240 | |
Heikki Krogerus | f4d0526 | 2016-03-29 14:52:23 +0300 | [diff] [blame] | 241 | if (cell->properties) { |
| 242 | ret = platform_device_add_properties(pdev, cell->properties); |
Andy Shevchenko | 4d215ca | 2015-11-30 17:11:40 +0200 | [diff] [blame] | 243 | if (ret) |
Lee Jones | 466a62d | 2020-06-11 07:35:33 +0100 | [diff] [blame] | 244 | goto fail_of_entry; |
Andy Shevchenko | 4d215ca | 2015-11-30 17:11:40 +0200 | [diff] [blame] | 245 | } |
| 246 | |
Dmitry Baryshkov | aa613de | 2008-06-27 10:37:19 +0100 | [diff] [blame] | 247 | 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 Ortiz | f03cfcb | 2010-03-26 01:09:04 +0100 | [diff] [blame] | 252 | if ((cell->resources[r].flags & IORESOURCE_MEM) && mem_base) { |
Dmitry Baryshkov | aa613de | 2008-06-27 10:37:19 +0100 | [diff] [blame] | 253 | 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 Jones | c94bb23 | 2012-06-29 19:01:03 +0200 | [diff] [blame] | 259 | 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 Baryshkov | aa613de | 2008-06-27 10:37:19 +0100 | [diff] [blame] | 271 | } 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 Ortiz | 91feded | 2010-02-19 11:07:59 +0100 | [diff] [blame] | 276 | |
Daniel Drake | 5f2545f | 2010-09-30 21:55:36 +0100 | [diff] [blame] | 277 | if (!cell->ignore_resource_conflicts) { |
Lorenzo Pieralisi | ec40c60 | 2015-05-01 10:41:10 +0100 | [diff] [blame] | 278 | if (has_acpi_companion(&pdev->dev)) { |
| 279 | ret = acpi_check_resource_conflict(&res[r]); |
| 280 | if (ret) |
Lee Jones | 466a62d | 2020-06-11 07:35:33 +0100 | [diff] [blame] | 281 | goto fail_of_entry; |
Lorenzo Pieralisi | ec40c60 | 2015-05-01 10:41:10 +0100 | [diff] [blame] | 282 | } |
Daniel Drake | 5f2545f | 2010-09-30 21:55:36 +0100 | [diff] [blame] | 283 | } |
Dmitry Baryshkov | aa613de | 2008-06-27 10:37:19 +0100 | [diff] [blame] | 284 | } |
| 285 | |
Axel Lin | 8af5fe3 | 2010-05-31 17:30:55 +0800 | [diff] [blame] | 286 | ret = platform_device_add_resources(pdev, res, cell->num_resources); |
| 287 | if (ret) |
Lee Jones | 466a62d | 2020-06-11 07:35:33 +0100 | [diff] [blame] | 288 | goto fail_of_entry; |
Dmitry Baryshkov | aa613de | 2008-06-27 10:37:19 +0100 | [diff] [blame] | 289 | |
| 290 | ret = platform_device_add(pdev); |
| 291 | if (ret) |
Lee Jones | 466a62d | 2020-06-11 07:35:33 +0100 | [diff] [blame] | 292 | goto fail_of_entry; |
Ian Molton | a87903f | 2008-07-25 22:59:29 +0100 | [diff] [blame] | 293 | |
Mark Brown | 4c90aa9 | 2010-11-26 17:19:34 +0000 | [diff] [blame] | 294 | if (cell->pm_runtime_no_callbacks) |
| 295 | pm_runtime_no_callbacks(&pdev->dev); |
| 296 | |
Ian Molton | a87903f | 2008-07-25 22:59:29 +0100 | [diff] [blame] | 297 | kfree(res); |
Dmitry Baryshkov | aa613de | 2008-06-27 10:37:19 +0100 | [diff] [blame] | 298 | |
| 299 | return 0; |
| 300 | |
Lee Jones | 466a62d | 2020-06-11 07:35:33 +0100 | [diff] [blame] | 301 | fail_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 Keepax | 7fcd427 | 2013-10-15 20:14:21 +0100 | [diff] [blame] | 307 | fail_alias: |
Charles Keepax | d137be0 | 2014-04-24 18:27:25 +0100 | [diff] [blame] | 308 | regulator_bulk_unregister_supply_alias(&pdev->dev, |
| 309 | cell->parent_supplies, |
| 310 | cell->num_parent_supplies); |
Ian Molton | a87903f | 2008-07-25 22:59:29 +0100 | [diff] [blame] | 311 | fail_res: |
| 312 | kfree(res); |
Dmitry Baryshkov | aa613de | 2008-06-27 10:37:19 +0100 | [diff] [blame] | 313 | fail_device: |
| 314 | platform_device_put(pdev); |
| 315 | fail_alloc: |
| 316 | return ret; |
| 317 | } |
| 318 | |
Bartosz Golaszewski | 1946f996 | 2019-04-23 11:04:45 +0200 | [diff] [blame] | 319 | /** |
| 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 Baryshkov | 424f525 | 2008-07-29 01:30:26 +0200 | [diff] [blame] | 332 | int mfd_add_devices(struct device *parent, int id, |
Geert Uytterhoeven | 03e361b | 2013-10-29 10:03:04 +0100 | [diff] [blame] | 333 | const struct mfd_cell *cells, int n_devs, |
Ben Dooks | 7f71ac9 | 2008-07-28 18:29:09 +0200 | [diff] [blame] | 334 | struct resource *mem_base, |
Mark Brown | 0848c94 | 2012-09-11 15:16:36 +0800 | [diff] [blame] | 335 | int irq_base, struct irq_domain *domain) |
Dmitry Baryshkov | aa613de | 2008-06-27 10:37:19 +0100 | [diff] [blame] | 336 | { |
| 337 | int i; |
Geert Uytterhoeven | 0b208e4 | 2013-10-29 15:47:22 +0100 | [diff] [blame] | 338 | int ret; |
Dmitry Baryshkov | aa613de | 2008-06-27 10:37:19 +0100 | [diff] [blame] | 339 | |
| 340 | for (i = 0; i < n_devs; i++) { |
Lee Jones | 5a47c0f | 2019-10-21 10:47:37 +0100 | [diff] [blame] | 341 | ret = mfd_add_device(parent, id, cells + i, mem_base, |
Mark Brown | 0848c94 | 2012-09-11 15:16:36 +0800 | [diff] [blame] | 342 | irq_base, domain); |
Dmitry Baryshkov | aa613de | 2008-06-27 10:37:19 +0100 | [diff] [blame] | 343 | if (ret) |
Geert Uytterhoeven | 0b208e4 | 2013-10-29 15:47:22 +0100 | [diff] [blame] | 344 | goto fail; |
Dmitry Baryshkov | aa613de | 2008-06-27 10:37:19 +0100 | [diff] [blame] | 345 | } |
| 346 | |
Geert Uytterhoeven | 0b208e4 | 2013-10-29 15:47:22 +0100 | [diff] [blame] | 347 | return 0; |
Dmitry Baryshkov | aa613de | 2008-06-27 10:37:19 +0100 | [diff] [blame] | 348 | |
Geert Uytterhoeven | 0b208e4 | 2013-10-29 15:47:22 +0100 | [diff] [blame] | 349 | fail: |
| 350 | if (i) |
| 351 | mfd_remove_devices(parent); |
Lee Jones | 5a47c0f | 2019-10-21 10:47:37 +0100 | [diff] [blame] | 352 | |
Dmitry Baryshkov | aa613de | 2008-06-27 10:37:19 +0100 | [diff] [blame] | 353 | return ret; |
| 354 | } |
| 355 | EXPORT_SYMBOL(mfd_add_devices); |
| 356 | |
Lee Jones | 5a47c0f | 2019-10-21 10:47:37 +0100 | [diff] [blame] | 357 | static int mfd_remove_devices_fn(struct device *dev, void *data) |
Dmitry Baryshkov | aa613de | 2008-06-27 10:37:19 +0100 | [diff] [blame] | 358 | { |
Charles Keepax | b9fbb62 | 2012-11-09 16:15:28 +0000 | [diff] [blame] | 359 | struct platform_device *pdev; |
| 360 | const struct mfd_cell *cell; |
Charles Keepax | 114294d | 2020-07-23 11:54:58 +0100 | [diff] [blame] | 361 | int *level = data; |
Andres Salomon | 1e29af6 | 2011-02-17 19:07:34 -0800 | [diff] [blame] | 362 | |
Charles Keepax | b9fbb62 | 2012-11-09 16:15:28 +0000 | [diff] [blame] | 363 | if (dev->type != &mfd_dev_type) |
| 364 | return 0; |
| 365 | |
| 366 | pdev = to_platform_device(dev); |
| 367 | cell = mfd_get_cell(pdev); |
| 368 | |
Charles Keepax | 114294d | 2020-07-23 11:54:58 +0100 | [diff] [blame] | 369 | if (level && cell->level > *level) |
| 370 | return 0; |
| 371 | |
Charles Keepax | d137be0 | 2014-04-24 18:27:25 +0100 | [diff] [blame] | 372 | regulator_bulk_unregister_supply_alias(dev, cell->parent_supplies, |
| 373 | cell->num_parent_supplies); |
| 374 | |
Andres Salomon | 1e29af6 | 2011-02-17 19:07:34 -0800 | [diff] [blame] | 375 | platform_device_unregister(pdev); |
Dmitry Baryshkov | aa613de | 2008-06-27 10:37:19 +0100 | [diff] [blame] | 376 | return 0; |
| 377 | } |
| 378 | |
Charles Keepax | 114294d | 2020-07-23 11:54:58 +0100 | [diff] [blame] | 379 | void 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 | } |
| 385 | EXPORT_SYMBOL(mfd_remove_devices_late); |
| 386 | |
Dmitry Baryshkov | 424f525 | 2008-07-29 01:30:26 +0200 | [diff] [blame] | 387 | void mfd_remove_devices(struct device *parent) |
Dmitry Baryshkov | aa613de | 2008-06-27 10:37:19 +0100 | [diff] [blame] | 388 | { |
Charles Keepax | 114294d | 2020-07-23 11:54:58 +0100 | [diff] [blame] | 389 | int level = MFD_DEP_LEVEL_NORMAL; |
| 390 | |
| 391 | device_for_each_child_reverse(parent, &level, mfd_remove_devices_fn); |
Dmitry Baryshkov | aa613de | 2008-06-27 10:37:19 +0100 | [diff] [blame] | 392 | } |
| 393 | EXPORT_SYMBOL(mfd_remove_devices); |
| 394 | |
Laxman Dewangan | a8f447be | 2016-04-08 00:12:55 +0530 | [diff] [blame] | 395 | static 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 Jones | 5a0ffef | 2020-06-23 15:51:28 +0100 | [diff] [blame] | 406 | * |
| 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 Dewangan | a8f447be | 2016-04-08 00:12:55 +0530 | [diff] [blame] | 416 | */ |
| 417 | int 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 | } |
| 441 | EXPORT_SYMBOL(devm_mfd_add_devices); |
| 442 | |
Dmitry Baryshkov | aa613de | 2008-06-27 10:37:19 +0100 | [diff] [blame] | 443 | MODULE_LICENSE("GPL"); |
| 444 | MODULE_AUTHOR("Ian Molton, Dmitry Baryshkov"); |