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