2 * FPGA Bridge Framework Driver
4 * Copyright (C) 2013-2016 Altera Corporation, All Rights Reserved.
5 * Copyright (C) 2017 Intel Corporation
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms and conditions of the GNU General Public License,
9 * version 2, as published by the Free Software Foundation.
11 * This program is distributed in the hope it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * You should have received a copy of the GNU General Public License along with
17 * this program. If not, see <http://www.gnu.org/licenses/>.
19 #include <linux/fpga/fpga-bridge.h>
20 #include <linux/idr.h>
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/of_platform.h>
24 #include <linux/slab.h>
25 #include <linux/spinlock.h>
27 static DEFINE_IDA(fpga_bridge_ida);
28 static struct class *fpga_bridge_class;
30 /* Lock for adding/removing bridges to linked lists*/
31 static spinlock_t bridge_list_lock;
33 static int fpga_bridge_of_node_match(struct device *dev, const void *data)
35 return dev->of_node == data;
39 * fpga_bridge_enable - Enable transactions on the bridge
41 * @bridge: FPGA bridge
43 * Return: 0 for success, error code otherwise.
45 int fpga_bridge_enable(struct fpga_bridge *bridge)
47 dev_dbg(&bridge->dev, "enable\n");
49 if (bridge->br_ops && bridge->br_ops->enable_set)
50 return bridge->br_ops->enable_set(bridge, 1);
54 EXPORT_SYMBOL_GPL(fpga_bridge_enable);
57 * fpga_bridge_disable - Disable transactions on the bridge
59 * @bridge: FPGA bridge
61 * Return: 0 for success, error code otherwise.
63 int fpga_bridge_disable(struct fpga_bridge *bridge)
65 dev_dbg(&bridge->dev, "disable\n");
67 if (bridge->br_ops && bridge->br_ops->enable_set)
68 return bridge->br_ops->enable_set(bridge, 0);
72 EXPORT_SYMBOL_GPL(fpga_bridge_disable);
74 static struct fpga_bridge *__fpga_bridge_get(struct device *dev,
75 struct fpga_image_info *info)
77 struct fpga_bridge *bridge;
80 bridge = to_fpga_bridge(dev);
86 if (!mutex_trylock(&bridge->mutex)) {
91 if (!try_module_get(dev->parent->driver->owner))
94 dev_dbg(&bridge->dev, "get\n");
99 mutex_unlock(&bridge->mutex);
106 * of_fpga_bridge_get - get an exclusive reference to a fpga bridge
108 * @np: node pointer of a FPGA bridge
109 * @info: fpga image specific information
111 * Return fpga_bridge struct if successful.
112 * Return -EBUSY if someone already has a reference to the bridge.
113 * Return -ENODEV if @np is not a FPGA Bridge.
115 struct fpga_bridge *of_fpga_bridge_get(struct device_node *np,
116 struct fpga_image_info *info)
120 dev = class_find_device(fpga_bridge_class, NULL, np,
121 fpga_bridge_of_node_match);
123 return ERR_PTR(-ENODEV);
125 return __fpga_bridge_get(dev, info);
127 EXPORT_SYMBOL_GPL(of_fpga_bridge_get);
129 static int fpga_bridge_dev_match(struct device *dev, const void *data)
131 return dev->parent == data;
135 * fpga_bridge_get - get an exclusive reference to a fpga bridge
136 * @dev: parent device that fpga bridge was registered with
138 * Given a device, get an exclusive reference to a fpga bridge.
140 * Return: fpga manager struct or IS_ERR() condition containing error code.
142 struct fpga_bridge *fpga_bridge_get(struct device *dev,
143 struct fpga_image_info *info)
145 struct device *bridge_dev;
147 bridge_dev = class_find_device(fpga_bridge_class, NULL, dev,
148 fpga_bridge_dev_match);
150 return ERR_PTR(-ENODEV);
152 return __fpga_bridge_get(bridge_dev, info);
154 EXPORT_SYMBOL_GPL(fpga_bridge_get);
157 * fpga_bridge_put - release a reference to a bridge
159 * @bridge: FPGA bridge
161 void fpga_bridge_put(struct fpga_bridge *bridge)
163 dev_dbg(&bridge->dev, "put\n");
166 module_put(bridge->dev.parent->driver->owner);
167 mutex_unlock(&bridge->mutex);
168 put_device(&bridge->dev);
170 EXPORT_SYMBOL_GPL(fpga_bridge_put);
173 * fpga_bridges_enable - enable bridges in a list
174 * @bridge_list: list of FPGA bridges
176 * Enable each bridge in the list. If list is empty, do nothing.
178 * Return 0 for success or empty bridge list; return error code otherwise.
180 int fpga_bridges_enable(struct list_head *bridge_list)
182 struct fpga_bridge *bridge;
185 list_for_each_entry(bridge, bridge_list, node) {
186 ret = fpga_bridge_enable(bridge);
193 EXPORT_SYMBOL_GPL(fpga_bridges_enable);
196 * fpga_bridges_disable - disable bridges in a list
198 * @bridge_list: list of FPGA bridges
200 * Disable each bridge in the list. If list is empty, do nothing.
202 * Return 0 for success or empty bridge list; return error code otherwise.
204 int fpga_bridges_disable(struct list_head *bridge_list)
206 struct fpga_bridge *bridge;
209 list_for_each_entry(bridge, bridge_list, node) {
210 ret = fpga_bridge_disable(bridge);
217 EXPORT_SYMBOL_GPL(fpga_bridges_disable);
220 * fpga_bridges_put - put bridges
222 * @bridge_list: list of FPGA bridges
224 * For each bridge in the list, put the bridge and remove it from the list.
225 * If list is empty, do nothing.
227 void fpga_bridges_put(struct list_head *bridge_list)
229 struct fpga_bridge *bridge, *next;
232 list_for_each_entry_safe(bridge, next, bridge_list, node) {
233 fpga_bridge_put(bridge);
235 spin_lock_irqsave(&bridge_list_lock, flags);
236 list_del(&bridge->node);
237 spin_unlock_irqrestore(&bridge_list_lock, flags);
240 EXPORT_SYMBOL_GPL(fpga_bridges_put);
243 * of_fpga_bridge_get_to_list - get a bridge, add it to a list
245 * @np: node pointer of a FPGA bridge
246 * @info: fpga image specific information
247 * @bridge_list: list of FPGA bridges
249 * Get an exclusive reference to the bridge and and it to the list.
251 * Return 0 for success, error code from of_fpga_bridge_get() othewise.
253 int of_fpga_bridge_get_to_list(struct device_node *np,
254 struct fpga_image_info *info,
255 struct list_head *bridge_list)
257 struct fpga_bridge *bridge;
260 bridge = of_fpga_bridge_get(np, info);
262 return PTR_ERR(bridge);
264 spin_lock_irqsave(&bridge_list_lock, flags);
265 list_add(&bridge->node, bridge_list);
266 spin_unlock_irqrestore(&bridge_list_lock, flags);
270 EXPORT_SYMBOL_GPL(of_fpga_bridge_get_to_list);
273 * fpga_bridge_get_to_list - given device, get a bridge, add it to a list
275 * @dev: FPGA bridge device
276 * @info: fpga image specific information
277 * @bridge_list: list of FPGA bridges
279 * Get an exclusive reference to the bridge and and it to the list.
281 * Return 0 for success, error code from fpga_bridge_get() othewise.
283 int fpga_bridge_get_to_list(struct device *dev,
284 struct fpga_image_info *info,
285 struct list_head *bridge_list)
287 struct fpga_bridge *bridge;
290 bridge = fpga_bridge_get(dev, info);
292 return PTR_ERR(bridge);
294 spin_lock_irqsave(&bridge_list_lock, flags);
295 list_add(&bridge->node, bridge_list);
296 spin_unlock_irqrestore(&bridge_list_lock, flags);
300 EXPORT_SYMBOL_GPL(fpga_bridge_get_to_list);
302 static ssize_t name_show(struct device *dev,
303 struct device_attribute *attr, char *buf)
305 struct fpga_bridge *bridge = to_fpga_bridge(dev);
307 return sprintf(buf, "%s\n", bridge->name);
310 static ssize_t state_show(struct device *dev,
311 struct device_attribute *attr, char *buf)
313 struct fpga_bridge *bridge = to_fpga_bridge(dev);
316 if (bridge->br_ops && bridge->br_ops->enable_show)
317 enable = bridge->br_ops->enable_show(bridge);
319 return sprintf(buf, "%s\n", enable ? "enabled" : "disabled");
322 static DEVICE_ATTR_RO(name);
323 static DEVICE_ATTR_RO(state);
325 static struct attribute *fpga_bridge_attrs[] = {
327 &dev_attr_state.attr,
330 ATTRIBUTE_GROUPS(fpga_bridge);
333 * fpga_bridge_register - register a fpga bridge driver
334 * @dev: FPGA bridge device from pdev
335 * @name: FPGA bridge name
336 * @br_ops: pointer to structure of fpga bridge ops
337 * @priv: FPGA bridge private data
339 * Return: 0 for success, error code otherwise.
341 int fpga_bridge_register(struct device *dev, const char *name,
342 const struct fpga_bridge_ops *br_ops, void *priv)
344 struct fpga_bridge *bridge;
347 if (!name || !strlen(name)) {
348 dev_err(dev, "Attempt to register with no name!\n");
352 bridge = kzalloc(sizeof(*bridge), GFP_KERNEL);
356 id = ida_simple_get(&fpga_bridge_ida, 0, 0, GFP_KERNEL);
362 mutex_init(&bridge->mutex);
363 INIT_LIST_HEAD(&bridge->node);
366 bridge->br_ops = br_ops;
369 device_initialize(&bridge->dev);
370 bridge->dev.class = fpga_bridge_class;
371 bridge->dev.parent = dev;
372 bridge->dev.of_node = dev->of_node;
374 dev_set_drvdata(dev, bridge);
376 ret = dev_set_name(&bridge->dev, "br%d", id);
380 ret = device_add(&bridge->dev);
384 of_platform_populate(dev->of_node, NULL, NULL, dev);
386 dev_info(bridge->dev.parent, "fpga bridge [%s] registered\n",
392 ida_simple_remove(&fpga_bridge_ida, id);
398 EXPORT_SYMBOL_GPL(fpga_bridge_register);
401 * fpga_bridge_unregister - unregister a fpga bridge driver
402 * @dev: FPGA bridge device from pdev
404 void fpga_bridge_unregister(struct device *dev)
406 struct fpga_bridge *bridge = dev_get_drvdata(dev);
409 * If the low level driver provides a method for putting bridge into
410 * a desired state upon unregister, do it.
412 if (bridge->br_ops && bridge->br_ops->fpga_bridge_remove)
413 bridge->br_ops->fpga_bridge_remove(bridge);
415 device_unregister(&bridge->dev);
417 EXPORT_SYMBOL_GPL(fpga_bridge_unregister);
419 static void fpga_bridge_dev_release(struct device *dev)
421 struct fpga_bridge *bridge = to_fpga_bridge(dev);
423 ida_simple_remove(&fpga_bridge_ida, bridge->dev.id);
427 static int __init fpga_bridge_dev_init(void)
429 spin_lock_init(&bridge_list_lock);
431 fpga_bridge_class = class_create(THIS_MODULE, "fpga_bridge");
432 if (IS_ERR(fpga_bridge_class))
433 return PTR_ERR(fpga_bridge_class);
435 fpga_bridge_class->dev_groups = fpga_bridge_groups;
436 fpga_bridge_class->dev_release = fpga_bridge_dev_release;
441 static void __exit fpga_bridge_dev_exit(void)
443 class_destroy(fpga_bridge_class);
444 ida_destroy(&fpga_bridge_ida);
447 MODULE_DESCRIPTION("FPGA Bridge Driver");
449 MODULE_LICENSE("GPL v2");
451 subsys_initcall(fpga_bridge_dev_init);
452 module_exit(fpga_bridge_dev_exit);