|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
| 2 | +// Copyright (c) 2018-2021 Intel Corporation |
| 3 | + |
| 4 | +#include <linux/bug.h> |
| 5 | +#include <linux/device.h> |
| 6 | +#include <linux/export.h> |
| 7 | +#include <linux/idr.h> |
| 8 | +#include <linux/module.h> |
| 9 | +#include <linux/of.h> |
| 10 | +#include <linux/peci.h> |
| 11 | +#include <linux/pm_runtime.h> |
| 12 | +#include <linux/property.h> |
| 13 | +#include <linux/slab.h> |
| 14 | + |
| 15 | +#include "internal.h" |
| 16 | + |
| 17 | +static DEFINE_IDA(peci_controller_ida); |
| 18 | + |
| 19 | +static void peci_controller_dev_release(struct device *dev) |
| 20 | +{ |
| 21 | + struct peci_controller *controller = to_peci_controller(dev); |
| 22 | + |
| 23 | + mutex_destroy(&controller->bus_lock); |
| 24 | + ida_free(&peci_controller_ida, controller->id); |
| 25 | + kfree(controller); |
| 26 | +} |
| 27 | + |
| 28 | +struct device_type peci_controller_type = { |
| 29 | + .release = peci_controller_dev_release, |
| 30 | +}; |
| 31 | + |
| 32 | +static struct peci_controller *peci_controller_alloc(struct device *dev, |
| 33 | + struct peci_controller_ops *ops) |
| 34 | +{ |
| 35 | + struct peci_controller *controller; |
| 36 | + int ret; |
| 37 | + |
| 38 | + if (!ops->xfer) |
| 39 | + return ERR_PTR(-EINVAL); |
| 40 | + |
| 41 | + controller = kzalloc(sizeof(*controller), GFP_KERNEL); |
| 42 | + if (!controller) |
| 43 | + return ERR_PTR(-ENOMEM); |
| 44 | + |
| 45 | + ret = ida_alloc_max(&peci_controller_ida, U8_MAX, GFP_KERNEL); |
| 46 | + if (ret < 0) |
| 47 | + goto err; |
| 48 | + controller->id = ret; |
| 49 | + |
| 50 | + controller->ops = ops; |
| 51 | + |
| 52 | + controller->dev.parent = dev; |
| 53 | + controller->dev.bus = &peci_bus_type; |
| 54 | + controller->dev.type = &peci_controller_type; |
| 55 | + |
| 56 | + device_initialize(&controller->dev); |
| 57 | + |
| 58 | + mutex_init(&controller->bus_lock); |
| 59 | + |
| 60 | + return controller; |
| 61 | + |
| 62 | +err: |
| 63 | + kfree(controller); |
| 64 | + return ERR_PTR(ret); |
| 65 | +} |
| 66 | + |
| 67 | +static void unregister_controller(void *_controller) |
| 68 | +{ |
| 69 | + struct peci_controller *controller = _controller; |
| 70 | + |
| 71 | + device_unregister(&controller->dev); |
| 72 | + |
| 73 | + fwnode_handle_put(controller->dev.fwnode); |
| 74 | + |
| 75 | + pm_runtime_disable(&controller->dev); |
| 76 | +} |
| 77 | + |
| 78 | +/** |
| 79 | + * devm_peci_controller_add() - add PECI controller |
| 80 | + * @dev: device for devm operations |
| 81 | + * @ops: pointer to controller specific methods |
| 82 | + * |
| 83 | + * In final stage of its probe(), peci_controller driver calls |
| 84 | + * devm_peci_controller_add() to register itself with the PECI bus. |
| 85 | + * |
| 86 | + * Return: Pointer to the newly allocated controller or ERR_PTR() in case of failure. |
| 87 | + */ |
| 88 | +struct peci_controller *devm_peci_controller_add(struct device *dev, |
| 89 | + struct peci_controller_ops *ops) |
| 90 | +{ |
| 91 | + struct peci_controller *controller; |
| 92 | + int ret; |
| 93 | + |
| 94 | + controller = peci_controller_alloc(dev, ops); |
| 95 | + if (IS_ERR(controller)) |
| 96 | + return controller; |
| 97 | + |
| 98 | + ret = dev_set_name(&controller->dev, "peci-%d", controller->id); |
| 99 | + if (ret) |
| 100 | + goto err_put; |
| 101 | + |
| 102 | + pm_runtime_no_callbacks(&controller->dev); |
| 103 | + pm_suspend_ignore_children(&controller->dev, true); |
| 104 | + pm_runtime_enable(&controller->dev); |
| 105 | + |
| 106 | + device_set_node(&controller->dev, fwnode_handle_get(dev_fwnode(dev))); |
| 107 | + |
| 108 | + ret = device_add(&controller->dev); |
| 109 | + if (ret) |
| 110 | + goto err_fwnode; |
| 111 | + |
| 112 | + ret = devm_add_action_or_reset(dev, unregister_controller, controller); |
| 113 | + if (ret) |
| 114 | + return ERR_PTR(ret); |
| 115 | + |
| 116 | + return controller; |
| 117 | + |
| 118 | +err_fwnode: |
| 119 | + fwnode_handle_put(controller->dev.fwnode); |
| 120 | + |
| 121 | + pm_runtime_disable(&controller->dev); |
| 122 | + |
| 123 | +err_put: |
| 124 | + put_device(&controller->dev); |
| 125 | + |
| 126 | + return ERR_PTR(ret); |
| 127 | +} |
| 128 | +EXPORT_SYMBOL_NS_GPL(devm_peci_controller_add, PECI); |
| 129 | + |
| 130 | +struct bus_type peci_bus_type = { |
| 131 | + .name = "peci", |
| 132 | +}; |
| 133 | + |
| 134 | +static int __init peci_init(void) |
| 135 | +{ |
| 136 | + int ret; |
| 137 | + |
| 138 | + ret = bus_register(&peci_bus_type); |
| 139 | + if (ret < 0) { |
| 140 | + pr_err("peci: failed to register PECI bus type!\n"); |
| 141 | + return ret; |
| 142 | + } |
| 143 | + |
| 144 | + return 0; |
| 145 | +} |
| 146 | +module_init(peci_init); |
| 147 | + |
| 148 | +static void __exit peci_exit(void) |
| 149 | +{ |
| 150 | + bus_unregister(&peci_bus_type); |
| 151 | +} |
| 152 | +module_exit(peci_exit); |
| 153 | + |
| 154 | +MODULE_AUTHOR( "Jason M Bills <[email protected]>"); |
| 155 | +MODULE_AUTHOR( "Jae Hyun Yoo <[email protected]>"); |
| 156 | +MODULE_AUTHOR( "Iwona Winiarska <[email protected]>"); |
| 157 | +MODULE_DESCRIPTION("PECI bus core module"); |
| 158 | +MODULE_LICENSE("GPL"); |
0 commit comments