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
->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
->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
*bridge_dev
,
59 struct fpga_image_info
*info
)
61 struct fpga_bridge
*bridge
;
63 bridge
= to_fpga_bridge(bridge_dev
);
67 if (!mutex_trylock(&bridge
->mutex
))
68 return ERR_PTR(-EBUSY
);
70 if (!try_module_get(bridge
->br_ops_owner
)) {
71 mutex_unlock(&bridge
->mutex
);
72 return ERR_PTR(-ENODEV
);
75 dev_dbg(&bridge
->dev
, "get\n");
81 * of_fpga_bridge_get - get an exclusive reference to an fpga bridge
83 * @np: node pointer of an FPGA bridge.
84 * @info: fpga image specific information.
87 * * fpga_bridge struct pointer if successful.
88 * * -EBUSY if someone already has a reference to the bridge.
89 * * -ENODEV if @np is not an FPGA Bridge or can't take parent driver refcount.
91 struct fpga_bridge
*of_fpga_bridge_get(struct device_node
*np
,
92 struct fpga_image_info
*info
)
94 struct fpga_bridge
*bridge
;
95 struct device
*bridge_dev
;
97 bridge_dev
= class_find_device_by_of_node(&fpga_bridge_class
, np
);
99 return ERR_PTR(-ENODEV
);
101 bridge
= __fpga_bridge_get(bridge_dev
, info
);
103 put_device(bridge_dev
);
107 EXPORT_SYMBOL_GPL(of_fpga_bridge_get
);
109 static int fpga_bridge_dev_match(struct device
*dev
, const void *data
)
111 return dev
->parent
== data
;
115 * fpga_bridge_get - get an exclusive reference to an fpga bridge
116 * @dev: parent device that fpga bridge was registered with
117 * @info: fpga image specific information
119 * Given a device, get an exclusive reference to an fpga bridge.
121 * Return: fpga bridge struct or IS_ERR() condition containing error code.
123 struct fpga_bridge
*fpga_bridge_get(struct device
*dev
,
124 struct fpga_image_info
*info
)
126 struct fpga_bridge
*bridge
;
127 struct device
*bridge_dev
;
129 bridge_dev
= class_find_device(&fpga_bridge_class
, NULL
, dev
,
130 fpga_bridge_dev_match
);
132 return ERR_PTR(-ENODEV
);
134 bridge
= __fpga_bridge_get(bridge_dev
, info
);
136 put_device(bridge_dev
);
140 EXPORT_SYMBOL_GPL(fpga_bridge_get
);
143 * fpga_bridge_put - release a reference to a bridge
145 * @bridge: FPGA bridge
147 void fpga_bridge_put(struct fpga_bridge
*bridge
)
149 dev_dbg(&bridge
->dev
, "put\n");
152 module_put(bridge
->br_ops_owner
);
153 mutex_unlock(&bridge
->mutex
);
154 put_device(&bridge
->dev
);
156 EXPORT_SYMBOL_GPL(fpga_bridge_put
);
159 * fpga_bridges_enable - enable bridges in a list
160 * @bridge_list: list of FPGA bridges
162 * Enable each bridge in the list. If list is empty, do nothing.
164 * Return: 0 for success or empty bridge list or an error code otherwise.
166 int fpga_bridges_enable(struct list_head
*bridge_list
)
168 struct fpga_bridge
*bridge
;
171 list_for_each_entry(bridge
, bridge_list
, node
) {
172 ret
= fpga_bridge_enable(bridge
);
179 EXPORT_SYMBOL_GPL(fpga_bridges_enable
);
182 * fpga_bridges_disable - disable bridges in a list
184 * @bridge_list: list of FPGA bridges
186 * Disable each bridge in the list. If list is empty, do nothing.
188 * Return: 0 for success or empty bridge list or an error code otherwise.
190 int fpga_bridges_disable(struct list_head
*bridge_list
)
192 struct fpga_bridge
*bridge
;
195 list_for_each_entry(bridge
, bridge_list
, node
) {
196 ret
= fpga_bridge_disable(bridge
);
203 EXPORT_SYMBOL_GPL(fpga_bridges_disable
);
206 * fpga_bridges_put - put bridges
208 * @bridge_list: list of FPGA bridges
210 * For each bridge in the list, put the bridge and remove it from the list.
211 * If list is empty, do nothing.
213 void fpga_bridges_put(struct list_head
*bridge_list
)
215 struct fpga_bridge
*bridge
, *next
;
218 list_for_each_entry_safe(bridge
, next
, bridge_list
, node
) {
219 fpga_bridge_put(bridge
);
221 spin_lock_irqsave(&bridge_list_lock
, flags
);
222 list_del(&bridge
->node
);
223 spin_unlock_irqrestore(&bridge_list_lock
, flags
);
226 EXPORT_SYMBOL_GPL(fpga_bridges_put
);
229 * of_fpga_bridge_get_to_list - get a bridge, add it to a list
231 * @np: node pointer of an FPGA bridge
232 * @info: fpga image specific information
233 * @bridge_list: list of FPGA bridges
235 * Get an exclusive reference to the bridge and it to the list.
237 * Return: 0 for success, error code from of_fpga_bridge_get() otherwise.
239 int of_fpga_bridge_get_to_list(struct device_node
*np
,
240 struct fpga_image_info
*info
,
241 struct list_head
*bridge_list
)
243 struct fpga_bridge
*bridge
;
246 bridge
= of_fpga_bridge_get(np
, info
);
248 return PTR_ERR(bridge
);
250 spin_lock_irqsave(&bridge_list_lock
, flags
);
251 list_add(&bridge
->node
, bridge_list
);
252 spin_unlock_irqrestore(&bridge_list_lock
, flags
);
256 EXPORT_SYMBOL_GPL(of_fpga_bridge_get_to_list
);
259 * fpga_bridge_get_to_list - given device, get a bridge, add it to a list
261 * @dev: FPGA bridge device
262 * @info: fpga image specific information
263 * @bridge_list: list of FPGA bridges
265 * Get an exclusive reference to the bridge and it to the list.
267 * Return: 0 for success, error code from fpga_bridge_get() otherwise.
269 int fpga_bridge_get_to_list(struct device
*dev
,
270 struct fpga_image_info
*info
,
271 struct list_head
*bridge_list
)
273 struct fpga_bridge
*bridge
;
276 bridge
= fpga_bridge_get(dev
, info
);
278 return PTR_ERR(bridge
);
280 spin_lock_irqsave(&bridge_list_lock
, flags
);
281 list_add(&bridge
->node
, bridge_list
);
282 spin_unlock_irqrestore(&bridge_list_lock
, flags
);
286 EXPORT_SYMBOL_GPL(fpga_bridge_get_to_list
);
288 static ssize_t
name_show(struct device
*dev
,
289 struct device_attribute
*attr
, char *buf
)
291 struct fpga_bridge
*bridge
= to_fpga_bridge(dev
);
293 return sprintf(buf
, "%s\n", bridge
->name
);
296 static ssize_t
state_show(struct device
*dev
,
297 struct device_attribute
*attr
, char *buf
)
299 struct fpga_bridge
*bridge
= to_fpga_bridge(dev
);
302 if (bridge
->br_ops
->enable_show
) {
303 state
= bridge
->br_ops
->enable_show(bridge
);
308 return sysfs_emit(buf
, "%s\n", state
? "enabled" : "disabled");
311 static DEVICE_ATTR_RO(name
);
312 static DEVICE_ATTR_RO(state
);
314 static struct attribute
*fpga_bridge_attrs
[] = {
316 &dev_attr_state
.attr
,
319 ATTRIBUTE_GROUPS(fpga_bridge
);
322 * __fpga_bridge_register - create and register an FPGA Bridge device
323 * @parent: FPGA bridge device from pdev
324 * @name: FPGA bridge name
325 * @br_ops: pointer to structure of fpga bridge ops
326 * @priv: FPGA bridge private data
327 * @owner: owner module containing the br_ops
329 * Return: struct fpga_bridge pointer or ERR_PTR()
332 __fpga_bridge_register(struct device
*parent
, const char *name
,
333 const struct fpga_bridge_ops
*br_ops
,
334 void *priv
, struct module
*owner
)
336 struct fpga_bridge
*bridge
;
340 dev_err(parent
, "Attempt to register without fpga_bridge_ops\n");
341 return ERR_PTR(-EINVAL
);
344 if (!name
|| !strlen(name
)) {
345 dev_err(parent
, "Attempt to register with no name!\n");
346 return ERR_PTR(-EINVAL
);
349 bridge
= kzalloc(sizeof(*bridge
), GFP_KERNEL
);
351 return ERR_PTR(-ENOMEM
);
353 id
= ida_alloc(&fpga_bridge_ida
, GFP_KERNEL
);
359 mutex_init(&bridge
->mutex
);
360 INIT_LIST_HEAD(&bridge
->node
);
363 bridge
->br_ops
= br_ops
;
364 bridge
->br_ops_owner
= owner
;
367 bridge
->dev
.groups
= br_ops
->groups
;
368 bridge
->dev
.class = &fpga_bridge_class
;
369 bridge
->dev
.parent
= parent
;
370 bridge
->dev
.of_node
= parent
->of_node
;
373 ret
= dev_set_name(&bridge
->dev
, "br%d", id
);
377 ret
= device_register(&bridge
->dev
);
379 put_device(&bridge
->dev
);
383 of_platform_populate(bridge
->dev
.of_node
, NULL
, NULL
, &bridge
->dev
);
388 ida_free(&fpga_bridge_ida
, id
);
394 EXPORT_SYMBOL_GPL(__fpga_bridge_register
);
397 * fpga_bridge_unregister - unregister an FPGA bridge
399 * @bridge: FPGA bridge struct
401 * This function is intended for use in an FPGA bridge driver's remove function.
403 void fpga_bridge_unregister(struct fpga_bridge
*bridge
)
406 * If the low level driver provides a method for putting bridge into
407 * a desired state upon unregister, do it.
409 if (bridge
->br_ops
->fpga_bridge_remove
)
410 bridge
->br_ops
->fpga_bridge_remove(bridge
);
412 device_unregister(&bridge
->dev
);
414 EXPORT_SYMBOL_GPL(fpga_bridge_unregister
);
416 static void fpga_bridge_dev_release(struct device
*dev
)
418 struct fpga_bridge
*bridge
= to_fpga_bridge(dev
);
420 ida_free(&fpga_bridge_ida
, bridge
->dev
.id
);
424 static const struct class fpga_bridge_class
= {
425 .name
= "fpga_bridge",
426 .dev_groups
= fpga_bridge_groups
,
427 .dev_release
= fpga_bridge_dev_release
,
430 static int __init
fpga_bridge_dev_init(void)
432 return class_register(&fpga_bridge_class
);
435 static void __exit
fpga_bridge_dev_exit(void)
437 class_unregister(&fpga_bridge_class
);
438 ida_destroy(&fpga_bridge_ida
);
441 MODULE_DESCRIPTION("FPGA Bridge Driver");
442 MODULE_AUTHOR("Alan Tull <atull@kernel.org>");
443 MODULE_LICENSE("GPL v2");
445 subsys_initcall(fpga_bridge_dev_init
);
446 module_exit(fpga_bridge_dev_exit
);