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