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 struct class *fpga_bridge_class
;
19 /* Lock for adding/removing bridges to linked lists*/
20 static spinlock_t bridge_list_lock
;
22 static int fpga_bridge_of_node_match(struct device
*dev
, const void *data
)
24 return dev
->of_node
== data
;
28 * fpga_bridge_enable - Enable transactions on the bridge
30 * @bridge: FPGA bridge
32 * Return: 0 for success, error code otherwise.
34 int fpga_bridge_enable(struct fpga_bridge
*bridge
)
36 dev_dbg(&bridge
->dev
, "enable\n");
38 if (bridge
->br_ops
&& bridge
->br_ops
->enable_set
)
39 return bridge
->br_ops
->enable_set(bridge
, 1);
43 EXPORT_SYMBOL_GPL(fpga_bridge_enable
);
46 * fpga_bridge_disable - Disable transactions on the bridge
48 * @bridge: FPGA bridge
50 * Return: 0 for success, error code otherwise.
52 int fpga_bridge_disable(struct fpga_bridge
*bridge
)
54 dev_dbg(&bridge
->dev
, "disable\n");
56 if (bridge
->br_ops
&& bridge
->br_ops
->enable_set
)
57 return bridge
->br_ops
->enable_set(bridge
, 0);
61 EXPORT_SYMBOL_GPL(fpga_bridge_disable
);
63 static struct fpga_bridge
*__fpga_bridge_get(struct device
*dev
,
64 struct fpga_image_info
*info
)
66 struct fpga_bridge
*bridge
;
69 bridge
= to_fpga_bridge(dev
);
73 if (!mutex_trylock(&bridge
->mutex
)) {
78 if (!try_module_get(dev
->parent
->driver
->owner
))
81 dev_dbg(&bridge
->dev
, "get\n");
86 mutex_unlock(&bridge
->mutex
);
93 * of_fpga_bridge_get - get an exclusive reference to a fpga bridge
95 * @np: node pointer of a FPGA bridge
96 * @info: fpga image specific information
98 * Return fpga_bridge struct if successful.
99 * Return -EBUSY if someone already has a reference to the bridge.
100 * Return -ENODEV if @np is not a FPGA Bridge.
102 struct fpga_bridge
*of_fpga_bridge_get(struct device_node
*np
,
103 struct fpga_image_info
*info
)
107 dev
= class_find_device(fpga_bridge_class
, NULL
, np
,
108 fpga_bridge_of_node_match
);
110 return ERR_PTR(-ENODEV
);
112 return __fpga_bridge_get(dev
, info
);
114 EXPORT_SYMBOL_GPL(of_fpga_bridge_get
);
116 static int fpga_bridge_dev_match(struct device
*dev
, const void *data
)
118 return dev
->parent
== data
;
122 * fpga_bridge_get - get an exclusive reference to a fpga bridge
123 * @dev: parent device that fpga bridge was registered with
124 * @info: fpga manager info
126 * Given a device, get an exclusive reference to a fpga bridge.
128 * Return: fpga bridge struct or IS_ERR() condition containing error code.
130 struct fpga_bridge
*fpga_bridge_get(struct device
*dev
,
131 struct fpga_image_info
*info
)
133 struct device
*bridge_dev
;
135 bridge_dev
= class_find_device(fpga_bridge_class
, NULL
, dev
,
136 fpga_bridge_dev_match
);
138 return ERR_PTR(-ENODEV
);
140 return __fpga_bridge_get(bridge_dev
, info
);
142 EXPORT_SYMBOL_GPL(fpga_bridge_get
);
145 * fpga_bridge_put - release a reference to a bridge
147 * @bridge: FPGA bridge
149 void fpga_bridge_put(struct fpga_bridge
*bridge
)
151 dev_dbg(&bridge
->dev
, "put\n");
154 module_put(bridge
->dev
.parent
->driver
->owner
);
155 mutex_unlock(&bridge
->mutex
);
156 put_device(&bridge
->dev
);
158 EXPORT_SYMBOL_GPL(fpga_bridge_put
);
161 * fpga_bridges_enable - enable bridges in a list
162 * @bridge_list: list of FPGA bridges
164 * Enable each bridge in the list. If list is empty, do nothing.
166 * Return 0 for success or empty bridge list; return error code otherwise.
168 int fpga_bridges_enable(struct list_head
*bridge_list
)
170 struct fpga_bridge
*bridge
;
173 list_for_each_entry(bridge
, bridge_list
, node
) {
174 ret
= fpga_bridge_enable(bridge
);
181 EXPORT_SYMBOL_GPL(fpga_bridges_enable
);
184 * fpga_bridges_disable - disable bridges in a list
186 * @bridge_list: list of FPGA bridges
188 * Disable each bridge in the list. If list is empty, do nothing.
190 * Return 0 for success or empty bridge list; return error code otherwise.
192 int fpga_bridges_disable(struct list_head
*bridge_list
)
194 struct fpga_bridge
*bridge
;
197 list_for_each_entry(bridge
, bridge_list
, node
) {
198 ret
= fpga_bridge_disable(bridge
);
205 EXPORT_SYMBOL_GPL(fpga_bridges_disable
);
208 * fpga_bridges_put - put bridges
210 * @bridge_list: list of FPGA bridges
212 * For each bridge in the list, put the bridge and remove it from the list.
213 * If list is empty, do nothing.
215 void fpga_bridges_put(struct list_head
*bridge_list
)
217 struct fpga_bridge
*bridge
, *next
;
220 list_for_each_entry_safe(bridge
, next
, bridge_list
, node
) {
221 fpga_bridge_put(bridge
);
223 spin_lock_irqsave(&bridge_list_lock
, flags
);
224 list_del(&bridge
->node
);
225 spin_unlock_irqrestore(&bridge_list_lock
, flags
);
228 EXPORT_SYMBOL_GPL(fpga_bridges_put
);
231 * of_fpga_bridge_get_to_list - get a bridge, add it to a list
233 * @np: node pointer of a FPGA bridge
234 * @info: fpga image specific information
235 * @bridge_list: list of FPGA bridges
237 * Get an exclusive reference to the bridge and and it to the list.
239 * Return 0 for success, error code from of_fpga_bridge_get() othewise.
241 int of_fpga_bridge_get_to_list(struct device_node
*np
,
242 struct fpga_image_info
*info
,
243 struct list_head
*bridge_list
)
245 struct fpga_bridge
*bridge
;
248 bridge
= of_fpga_bridge_get(np
, info
);
250 return PTR_ERR(bridge
);
252 spin_lock_irqsave(&bridge_list_lock
, flags
);
253 list_add(&bridge
->node
, bridge_list
);
254 spin_unlock_irqrestore(&bridge_list_lock
, flags
);
258 EXPORT_SYMBOL_GPL(of_fpga_bridge_get_to_list
);
261 * fpga_bridge_get_to_list - given device, get a bridge, add it to a list
263 * @dev: FPGA bridge device
264 * @info: fpga image specific information
265 * @bridge_list: list of FPGA bridges
267 * Get an exclusive reference to the bridge and and it to the list.
269 * Return 0 for success, error code from fpga_bridge_get() othewise.
271 int fpga_bridge_get_to_list(struct device
*dev
,
272 struct fpga_image_info
*info
,
273 struct list_head
*bridge_list
)
275 struct fpga_bridge
*bridge
;
278 bridge
= fpga_bridge_get(dev
, info
);
280 return PTR_ERR(bridge
);
282 spin_lock_irqsave(&bridge_list_lock
, flags
);
283 list_add(&bridge
->node
, bridge_list
);
284 spin_unlock_irqrestore(&bridge_list_lock
, flags
);
288 EXPORT_SYMBOL_GPL(fpga_bridge_get_to_list
);
290 static ssize_t
name_show(struct device
*dev
,
291 struct device_attribute
*attr
, char *buf
)
293 struct fpga_bridge
*bridge
= to_fpga_bridge(dev
);
295 return sprintf(buf
, "%s\n", bridge
->name
);
298 static ssize_t
state_show(struct device
*dev
,
299 struct device_attribute
*attr
, char *buf
)
301 struct fpga_bridge
*bridge
= to_fpga_bridge(dev
);
304 if (bridge
->br_ops
&& bridge
->br_ops
->enable_show
)
305 enable
= bridge
->br_ops
->enable_show(bridge
);
307 return sprintf(buf
, "%s\n", enable
? "enabled" : "disabled");
310 static DEVICE_ATTR_RO(name
);
311 static DEVICE_ATTR_RO(state
);
313 static struct attribute
*fpga_bridge_attrs
[] = {
315 &dev_attr_state
.attr
,
318 ATTRIBUTE_GROUPS(fpga_bridge
);
321 * fpga_bridge_create - create and initialize a struct fpga_bridge
322 * @dev: FPGA bridge device from pdev
323 * @name: FPGA bridge name
324 * @br_ops: pointer to structure of fpga bridge ops
325 * @priv: FPGA bridge private data
327 * The caller of this function is responsible for freeing the bridge with
328 * fpga_bridge_free(). Using devm_fpga_bridge_create() instead is recommended.
330 * Return: struct fpga_bridge or NULL
332 struct fpga_bridge
*fpga_bridge_create(struct device
*dev
, const char *name
,
333 const struct fpga_bridge_ops
*br_ops
,
336 struct fpga_bridge
*bridge
;
339 if (!name
|| !strlen(name
)) {
340 dev_err(dev
, "Attempt to register with no name!\n");
344 bridge
= kzalloc(sizeof(*bridge
), GFP_KERNEL
);
348 id
= ida_simple_get(&fpga_bridge_ida
, 0, 0, GFP_KERNEL
);
354 mutex_init(&bridge
->mutex
);
355 INIT_LIST_HEAD(&bridge
->node
);
358 bridge
->br_ops
= br_ops
;
361 device_initialize(&bridge
->dev
);
362 bridge
->dev
.groups
= br_ops
->groups
;
363 bridge
->dev
.class = fpga_bridge_class
;
364 bridge
->dev
.parent
= dev
;
365 bridge
->dev
.of_node
= dev
->of_node
;
368 ret
= dev_set_name(&bridge
->dev
, "br%d", id
);
375 ida_simple_remove(&fpga_bridge_ida
, id
);
381 EXPORT_SYMBOL_GPL(fpga_bridge_create
);
384 * fpga_bridge_free - free a fpga bridge created by fpga_bridge_create()
385 * @bridge: FPGA bridge struct
387 void fpga_bridge_free(struct fpga_bridge
*bridge
)
389 ida_simple_remove(&fpga_bridge_ida
, bridge
->dev
.id
);
392 EXPORT_SYMBOL_GPL(fpga_bridge_free
);
394 static void devm_fpga_bridge_release(struct device
*dev
, void *res
)
396 struct fpga_bridge
*bridge
= *(struct fpga_bridge
**)res
;
398 fpga_bridge_free(bridge
);
402 * devm_fpga_bridge_create - create and init a managed struct fpga_bridge
403 * @dev: FPGA bridge device from pdev
404 * @name: FPGA bridge name
405 * @br_ops: pointer to structure of fpga bridge ops
406 * @priv: FPGA bridge private data
408 * This function is intended for use in a FPGA bridge driver's probe function.
409 * After the bridge driver creates the struct with devm_fpga_bridge_create(), it
410 * should register the bridge with fpga_bridge_register(). The bridge driver's
411 * remove function should call fpga_bridge_unregister(). The bridge struct
412 * allocated with this function will be freed automatically on driver detach.
413 * This includes the case of a probe function returning error before calling
414 * fpga_bridge_register(), the struct will still get cleaned up.
416 * Return: struct fpga_bridge or NULL
419 *devm_fpga_bridge_create(struct device
*dev
, const char *name
,
420 const struct fpga_bridge_ops
*br_ops
, void *priv
)
422 struct fpga_bridge
**ptr
, *bridge
;
424 ptr
= devres_alloc(devm_fpga_bridge_release
, sizeof(*ptr
), GFP_KERNEL
);
428 bridge
= fpga_bridge_create(dev
, name
, br_ops
, priv
);
433 devres_add(dev
, ptr
);
438 EXPORT_SYMBOL_GPL(devm_fpga_bridge_create
);
441 * fpga_bridge_register - register a FPGA bridge
443 * @bridge: FPGA bridge struct
445 * Return: 0 for success, error code otherwise.
447 int fpga_bridge_register(struct fpga_bridge
*bridge
)
449 struct device
*dev
= &bridge
->dev
;
452 ret
= device_add(dev
);
456 of_platform_populate(dev
->of_node
, NULL
, NULL
, dev
);
458 dev_info(dev
->parent
, "fpga bridge [%s] registered\n", bridge
->name
);
462 EXPORT_SYMBOL_GPL(fpga_bridge_register
);
465 * fpga_bridge_unregister - unregister a FPGA bridge
467 * @bridge: FPGA bridge struct
469 * This function is intended for use in a FPGA bridge driver's remove function.
471 void fpga_bridge_unregister(struct fpga_bridge
*bridge
)
474 * If the low level driver provides a method for putting bridge into
475 * a desired state upon unregister, do it.
477 if (bridge
->br_ops
&& bridge
->br_ops
->fpga_bridge_remove
)
478 bridge
->br_ops
->fpga_bridge_remove(bridge
);
480 device_unregister(&bridge
->dev
);
482 EXPORT_SYMBOL_GPL(fpga_bridge_unregister
);
484 static void fpga_bridge_dev_release(struct device
*dev
)
488 static int __init
fpga_bridge_dev_init(void)
490 spin_lock_init(&bridge_list_lock
);
492 fpga_bridge_class
= class_create(THIS_MODULE
, "fpga_bridge");
493 if (IS_ERR(fpga_bridge_class
))
494 return PTR_ERR(fpga_bridge_class
);
496 fpga_bridge_class
->dev_groups
= fpga_bridge_groups
;
497 fpga_bridge_class
->dev_release
= fpga_bridge_dev_release
;
502 static void __exit
fpga_bridge_dev_exit(void)
504 class_destroy(fpga_bridge_class
);
505 ida_destroy(&fpga_bridge_ida
);
508 MODULE_DESCRIPTION("FPGA Bridge Driver");
509 MODULE_AUTHOR("Alan Tull <atull@kernel.org>");
510 MODULE_LICENSE("GPL v2");
512 subsys_initcall(fpga_bridge_dev_init
);
513 module_exit(fpga_bridge_dev_exit
);