1 // SPDX-License-Identifier: GPL-2.0
3 * FPGA Bridge Framework Driver
5 * Copyright (C) 2013-2016 Altera Corporation, All Rights Reserved.
6 * Copyright (C) 2017 Intel Corporation
8 #include <linux/fpga/fpga-bridge.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/of_platform.h>
13 #include <linux/slab.h>
14 #include <linux/spinlock.h>
16 static DEFINE_IDA(fpga_bridge_ida);
17 static const struct class fpga_bridge_class;
19 /* Lock for adding/removing bridges to linked lists*/
20 static DEFINE_SPINLOCK(bridge_list_lock);
23 * fpga_bridge_enable - Enable transactions on the bridge
25 * @bridge: FPGA bridge
27 * Return: 0 for success, error code otherwise.
29 int fpga_bridge_enable(struct fpga_bridge *bridge)
31 dev_dbg(&bridge->dev, "enable\n");
33 if (bridge->br_ops && bridge->br_ops->enable_set)
34 return bridge->br_ops->enable_set(bridge, 1);
38 EXPORT_SYMBOL_GPL(fpga_bridge_enable);
41 * fpga_bridge_disable - Disable transactions on the bridge
43 * @bridge: FPGA bridge
45 * Return: 0 for success, error code otherwise.
47 int fpga_bridge_disable(struct fpga_bridge *bridge)
49 dev_dbg(&bridge->dev, "disable\n");
51 if (bridge->br_ops && bridge->br_ops->enable_set)
52 return bridge->br_ops->enable_set(bridge, 0);
56 EXPORT_SYMBOL_GPL(fpga_bridge_disable);
58 static struct fpga_bridge *__fpga_bridge_get(struct device *dev,
59 struct fpga_image_info *info)
61 struct fpga_bridge *bridge;
64 bridge = to_fpga_bridge(dev);
68 if (!mutex_trylock(&bridge->mutex)) {
73 if (!try_module_get(dev->parent->driver->owner))
76 dev_dbg(&bridge->dev, "get\n");
81 mutex_unlock(&bridge->mutex);
88 * of_fpga_bridge_get - get an exclusive reference to an fpga bridge
90 * @np: node pointer of an FPGA bridge.
91 * @info: fpga image specific information.
94 * * fpga_bridge struct pointer if successful.
95 * * -EBUSY if someone already has a reference to the bridge.
96 * * -ENODEV if @np is not an FPGA Bridge or can't take parent driver refcount.
98 struct fpga_bridge *of_fpga_bridge_get(struct device_node *np,
99 struct fpga_image_info *info)
103 dev = class_find_device_by_of_node(&fpga_bridge_class, np);
105 return ERR_PTR(-ENODEV);
107 return __fpga_bridge_get(dev, info);
109 EXPORT_SYMBOL_GPL(of_fpga_bridge_get);
111 static int fpga_bridge_dev_match(struct device *dev, const void *data)
113 return dev->parent == data;
117 * fpga_bridge_get - get an exclusive reference to an fpga bridge
118 * @dev: parent device that fpga bridge was registered with
119 * @info: fpga image specific information
121 * Given a device, get an exclusive reference to an fpga bridge.
123 * Return: fpga bridge struct or IS_ERR() condition containing error code.
125 struct fpga_bridge *fpga_bridge_get(struct device *dev,
126 struct fpga_image_info *info)
128 struct device *bridge_dev;
130 bridge_dev = class_find_device(&fpga_bridge_class, NULL, dev,
131 fpga_bridge_dev_match);
133 return ERR_PTR(-ENODEV);
135 return __fpga_bridge_get(bridge_dev, info);
137 EXPORT_SYMBOL_GPL(fpga_bridge_get);
140 * fpga_bridge_put - release a reference to a bridge
142 * @bridge: FPGA bridge
144 void fpga_bridge_put(struct fpga_bridge *bridge)
146 dev_dbg(&bridge->dev, "put\n");
149 module_put(bridge->dev.parent->driver->owner);
150 mutex_unlock(&bridge->mutex);
151 put_device(&bridge->dev);
153 EXPORT_SYMBOL_GPL(fpga_bridge_put);
156 * fpga_bridges_enable - enable bridges in a list
157 * @bridge_list: list of FPGA bridges
159 * Enable each bridge in the list. If list is empty, do nothing.
161 * Return: 0 for success or empty bridge list or an error code otherwise.
163 int fpga_bridges_enable(struct list_head *bridge_list)
165 struct fpga_bridge *bridge;
168 list_for_each_entry(bridge, bridge_list, node) {
169 ret = fpga_bridge_enable(bridge);
176 EXPORT_SYMBOL_GPL(fpga_bridges_enable);
179 * fpga_bridges_disable - disable bridges in a list
181 * @bridge_list: list of FPGA bridges
183 * Disable each bridge in the list. If list is empty, do nothing.
185 * Return: 0 for success or empty bridge list or an error code otherwise.
187 int fpga_bridges_disable(struct list_head *bridge_list)
189 struct fpga_bridge *bridge;
192 list_for_each_entry(bridge, bridge_list, node) {
193 ret = fpga_bridge_disable(bridge);
200 EXPORT_SYMBOL_GPL(fpga_bridges_disable);
203 * fpga_bridges_put - put bridges
205 * @bridge_list: list of FPGA bridges
207 * For each bridge in the list, put the bridge and remove it from the list.
208 * If list is empty, do nothing.
210 void fpga_bridges_put(struct list_head *bridge_list)
212 struct fpga_bridge *bridge, *next;
215 list_for_each_entry_safe(bridge, next, bridge_list, node) {
216 fpga_bridge_put(bridge);
218 spin_lock_irqsave(&bridge_list_lock, flags);
219 list_del(&bridge->node);
220 spin_unlock_irqrestore(&bridge_list_lock, flags);
223 EXPORT_SYMBOL_GPL(fpga_bridges_put);
226 * of_fpga_bridge_get_to_list - get a bridge, add it to a list
228 * @np: node pointer of an FPGA bridge
229 * @info: fpga image specific information
230 * @bridge_list: list of FPGA bridges
232 * Get an exclusive reference to the bridge and it to the list.
234 * Return: 0 for success, error code from of_fpga_bridge_get() otherwise.
236 int of_fpga_bridge_get_to_list(struct device_node *np,
237 struct fpga_image_info *info,
238 struct list_head *bridge_list)
240 struct fpga_bridge *bridge;
243 bridge = of_fpga_bridge_get(np, info);
245 return PTR_ERR(bridge);
247 spin_lock_irqsave(&bridge_list_lock, flags);
248 list_add(&bridge->node, bridge_list);
249 spin_unlock_irqrestore(&bridge_list_lock, flags);
253 EXPORT_SYMBOL_GPL(of_fpga_bridge_get_to_list);
256 * fpga_bridge_get_to_list - given device, get a bridge, add it to a list
258 * @dev: FPGA bridge device
259 * @info: fpga image specific information
260 * @bridge_list: list of FPGA bridges
262 * Get an exclusive reference to the bridge and it to the list.
264 * Return: 0 for success, error code from fpga_bridge_get() otherwise.
266 int fpga_bridge_get_to_list(struct device *dev,
267 struct fpga_image_info *info,
268 struct list_head *bridge_list)
270 struct fpga_bridge *bridge;
273 bridge = fpga_bridge_get(dev, info);
275 return PTR_ERR(bridge);
277 spin_lock_irqsave(&bridge_list_lock, flags);
278 list_add(&bridge->node, bridge_list);
279 spin_unlock_irqrestore(&bridge_list_lock, flags);
283 EXPORT_SYMBOL_GPL(fpga_bridge_get_to_list);
285 static ssize_t name_show(struct device *dev,
286 struct device_attribute *attr, char *buf)
288 struct fpga_bridge *bridge = to_fpga_bridge(dev);
290 return sprintf(buf, "%s\n", bridge->name);
293 static ssize_t state_show(struct device *dev,
294 struct device_attribute *attr, char *buf)
296 struct fpga_bridge *bridge = to_fpga_bridge(dev);
299 if (bridge->br_ops && bridge->br_ops->enable_show) {
300 state = bridge->br_ops->enable_show(bridge);
305 return sysfs_emit(buf, "%s\n", state ? "enabled" : "disabled");
308 static DEVICE_ATTR_RO(name);
309 static DEVICE_ATTR_RO(state);
311 static struct attribute *fpga_bridge_attrs[] = {
313 &dev_attr_state.attr,
316 ATTRIBUTE_GROUPS(fpga_bridge);
319 * fpga_bridge_register - create and register an FPGA Bridge device
320 * @parent: FPGA bridge device from pdev
321 * @name: FPGA bridge name
322 * @br_ops: pointer to structure of fpga bridge ops
323 * @priv: FPGA bridge private data
325 * Return: struct fpga_bridge pointer or ERR_PTR()
328 fpga_bridge_register(struct device *parent, const char *name,
329 const struct fpga_bridge_ops *br_ops,
332 struct fpga_bridge *bridge;
336 dev_err(parent, "Attempt to register without fpga_bridge_ops\n");
337 return ERR_PTR(-EINVAL);
340 if (!name || !strlen(name)) {
341 dev_err(parent, "Attempt to register with no name!\n");
342 return ERR_PTR(-EINVAL);
345 bridge = kzalloc(sizeof(*bridge), GFP_KERNEL);
347 return ERR_PTR(-ENOMEM);
349 id = ida_alloc(&fpga_bridge_ida, GFP_KERNEL);
355 mutex_init(&bridge->mutex);
356 INIT_LIST_HEAD(&bridge->node);
359 bridge->br_ops = br_ops;
362 bridge->dev.groups = br_ops->groups;
363 bridge->dev.class = &fpga_bridge_class;
364 bridge->dev.parent = parent;
365 bridge->dev.of_node = parent->of_node;
368 ret = dev_set_name(&bridge->dev, "br%d", id);
372 ret = device_register(&bridge->dev);
374 put_device(&bridge->dev);
378 of_platform_populate(bridge->dev.of_node, NULL, NULL, &bridge->dev);
383 ida_free(&fpga_bridge_ida, id);
389 EXPORT_SYMBOL_GPL(fpga_bridge_register);
392 * fpga_bridge_unregister - unregister an FPGA bridge
394 * @bridge: FPGA bridge struct
396 * This function is intended for use in an FPGA bridge driver's remove function.
398 void fpga_bridge_unregister(struct fpga_bridge *bridge)
401 * If the low level driver provides a method for putting bridge into
402 * a desired state upon unregister, do it.
404 if (bridge->br_ops && bridge->br_ops->fpga_bridge_remove)
405 bridge->br_ops->fpga_bridge_remove(bridge);
407 device_unregister(&bridge->dev);
409 EXPORT_SYMBOL_GPL(fpga_bridge_unregister);
411 static void fpga_bridge_dev_release(struct device *dev)
413 struct fpga_bridge *bridge = to_fpga_bridge(dev);
415 ida_free(&fpga_bridge_ida, bridge->dev.id);
419 static const struct class fpga_bridge_class = {
420 .name = "fpga_bridge",
421 .dev_groups = fpga_bridge_groups,
422 .dev_release = fpga_bridge_dev_release,
425 static int __init fpga_bridge_dev_init(void)
427 return class_register(&fpga_bridge_class);
430 static void __exit fpga_bridge_dev_exit(void)
432 class_unregister(&fpga_bridge_class);
433 ida_destroy(&fpga_bridge_ida);
436 MODULE_DESCRIPTION("FPGA Bridge Driver");
437 MODULE_AUTHOR("Alan Tull <atull@kernel.org>");
438 MODULE_LICENSE("GPL v2");
440 subsys_initcall(fpga_bridge_dev_init);
441 module_exit(fpga_bridge_dev_exit);