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
;
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 a fpga bridge
90 * @np: node pointer of a FPGA bridge
91 * @info: fpga image specific information
93 * Return fpga_bridge struct if successful.
94 * Return -EBUSY if someone already has a reference to the bridge.
95 * Return -ENODEV if @np is not a FPGA Bridge.
97 struct fpga_bridge
*of_fpga_bridge_get(struct device_node
*np
,
98 struct fpga_image_info
*info
)
102 dev
= class_find_device_by_of_node(fpga_bridge_class
, np
);
104 return ERR_PTR(-ENODEV
);
106 return __fpga_bridge_get(dev
, info
);
108 EXPORT_SYMBOL_GPL(of_fpga_bridge_get
);
110 static int fpga_bridge_dev_match(struct device
*dev
, const void *data
)
112 return dev
->parent
== data
;
116 * fpga_bridge_get - get an exclusive reference to a fpga bridge
117 * @dev: parent device that fpga bridge was registered with
118 * @info: fpga manager info
120 * Given a device, get an exclusive reference to a fpga bridge.
122 * Return: fpga bridge struct or IS_ERR() condition containing error code.
124 struct fpga_bridge
*fpga_bridge_get(struct device
*dev
,
125 struct fpga_image_info
*info
)
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 return __fpga_bridge_get(bridge_dev
, info
);
136 EXPORT_SYMBOL_GPL(fpga_bridge_get
);
139 * fpga_bridge_put - release a reference to a bridge
141 * @bridge: FPGA bridge
143 void fpga_bridge_put(struct fpga_bridge
*bridge
)
145 dev_dbg(&bridge
->dev
, "put\n");
148 module_put(bridge
->dev
.parent
->driver
->owner
);
149 mutex_unlock(&bridge
->mutex
);
150 put_device(&bridge
->dev
);
152 EXPORT_SYMBOL_GPL(fpga_bridge_put
);
155 * fpga_bridges_enable - enable bridges in a list
156 * @bridge_list: list of FPGA bridges
158 * Enable each bridge in the list. If list is empty, do nothing.
160 * Return 0 for success or empty bridge list; return error code otherwise.
162 int fpga_bridges_enable(struct list_head
*bridge_list
)
164 struct fpga_bridge
*bridge
;
167 list_for_each_entry(bridge
, bridge_list
, node
) {
168 ret
= fpga_bridge_enable(bridge
);
175 EXPORT_SYMBOL_GPL(fpga_bridges_enable
);
178 * fpga_bridges_disable - disable bridges in a list
180 * @bridge_list: list of FPGA bridges
182 * Disable each bridge in the list. If list is empty, do nothing.
184 * Return 0 for success or empty bridge list; return error code otherwise.
186 int fpga_bridges_disable(struct list_head
*bridge_list
)
188 struct fpga_bridge
*bridge
;
191 list_for_each_entry(bridge
, bridge_list
, node
) {
192 ret
= fpga_bridge_disable(bridge
);
199 EXPORT_SYMBOL_GPL(fpga_bridges_disable
);
202 * fpga_bridges_put - put bridges
204 * @bridge_list: list of FPGA bridges
206 * For each bridge in the list, put the bridge and remove it from the list.
207 * If list is empty, do nothing.
209 void fpga_bridges_put(struct list_head
*bridge_list
)
211 struct fpga_bridge
*bridge
, *next
;
214 list_for_each_entry_safe(bridge
, next
, bridge_list
, node
) {
215 fpga_bridge_put(bridge
);
217 spin_lock_irqsave(&bridge_list_lock
, flags
);
218 list_del(&bridge
->node
);
219 spin_unlock_irqrestore(&bridge_list_lock
, flags
);
222 EXPORT_SYMBOL_GPL(fpga_bridges_put
);
225 * of_fpga_bridge_get_to_list - get a bridge, add it to a list
227 * @np: node pointer of a FPGA bridge
228 * @info: fpga image specific information
229 * @bridge_list: list of FPGA bridges
231 * Get an exclusive reference to the bridge and and it to the list.
233 * Return 0 for success, error code from of_fpga_bridge_get() othewise.
235 int of_fpga_bridge_get_to_list(struct device_node
*np
,
236 struct fpga_image_info
*info
,
237 struct list_head
*bridge_list
)
239 struct fpga_bridge
*bridge
;
242 bridge
= of_fpga_bridge_get(np
, info
);
244 return PTR_ERR(bridge
);
246 spin_lock_irqsave(&bridge_list_lock
, flags
);
247 list_add(&bridge
->node
, bridge_list
);
248 spin_unlock_irqrestore(&bridge_list_lock
, flags
);
252 EXPORT_SYMBOL_GPL(of_fpga_bridge_get_to_list
);
255 * fpga_bridge_get_to_list - given device, get a bridge, add it to a list
257 * @dev: FPGA bridge device
258 * @info: fpga image specific information
259 * @bridge_list: list of FPGA bridges
261 * Get an exclusive reference to the bridge and and it to the list.
263 * Return 0 for success, error code from fpga_bridge_get() othewise.
265 int fpga_bridge_get_to_list(struct device
*dev
,
266 struct fpga_image_info
*info
,
267 struct list_head
*bridge_list
)
269 struct fpga_bridge
*bridge
;
272 bridge
= fpga_bridge_get(dev
, info
);
274 return PTR_ERR(bridge
);
276 spin_lock_irqsave(&bridge_list_lock
, flags
);
277 list_add(&bridge
->node
, bridge_list
);
278 spin_unlock_irqrestore(&bridge_list_lock
, flags
);
282 EXPORT_SYMBOL_GPL(fpga_bridge_get_to_list
);
284 static ssize_t
name_show(struct device
*dev
,
285 struct device_attribute
*attr
, char *buf
)
287 struct fpga_bridge
*bridge
= to_fpga_bridge(dev
);
289 return sprintf(buf
, "%s\n", bridge
->name
);
292 static ssize_t
state_show(struct device
*dev
,
293 struct device_attribute
*attr
, char *buf
)
295 struct fpga_bridge
*bridge
= to_fpga_bridge(dev
);
298 if (bridge
->br_ops
&& bridge
->br_ops
->enable_show
)
299 enable
= bridge
->br_ops
->enable_show(bridge
);
301 return sprintf(buf
, "%s\n", enable
? "enabled" : "disabled");
304 static DEVICE_ATTR_RO(name
);
305 static DEVICE_ATTR_RO(state
);
307 static struct attribute
*fpga_bridge_attrs
[] = {
309 &dev_attr_state
.attr
,
312 ATTRIBUTE_GROUPS(fpga_bridge
);
315 * fpga_bridge_create - create and initialize a struct fpga_bridge
316 * @dev: FPGA bridge device from pdev
317 * @name: FPGA bridge name
318 * @br_ops: pointer to structure of fpga bridge ops
319 * @priv: FPGA bridge private data
321 * The caller of this function is responsible for freeing the bridge with
322 * fpga_bridge_free(). Using devm_fpga_bridge_create() instead is recommended.
324 * Return: struct fpga_bridge or NULL
326 struct fpga_bridge
*fpga_bridge_create(struct device
*dev
, const char *name
,
327 const struct fpga_bridge_ops
*br_ops
,
330 struct fpga_bridge
*bridge
;
333 if (!name
|| !strlen(name
)) {
334 dev_err(dev
, "Attempt to register with no name!\n");
338 bridge
= kzalloc(sizeof(*bridge
), GFP_KERNEL
);
342 id
= ida_simple_get(&fpga_bridge_ida
, 0, 0, GFP_KERNEL
);
346 mutex_init(&bridge
->mutex
);
347 INIT_LIST_HEAD(&bridge
->node
);
350 bridge
->br_ops
= br_ops
;
353 device_initialize(&bridge
->dev
);
354 bridge
->dev
.groups
= br_ops
->groups
;
355 bridge
->dev
.class = fpga_bridge_class
;
356 bridge
->dev
.parent
= dev
;
357 bridge
->dev
.of_node
= dev
->of_node
;
360 ret
= dev_set_name(&bridge
->dev
, "br%d", id
);
367 ida_simple_remove(&fpga_bridge_ida
, id
);
373 EXPORT_SYMBOL_GPL(fpga_bridge_create
);
376 * fpga_bridge_free - free a fpga bridge created by fpga_bridge_create()
377 * @bridge: FPGA bridge struct
379 void fpga_bridge_free(struct fpga_bridge
*bridge
)
381 ida_simple_remove(&fpga_bridge_ida
, bridge
->dev
.id
);
384 EXPORT_SYMBOL_GPL(fpga_bridge_free
);
386 static void devm_fpga_bridge_release(struct device
*dev
, void *res
)
388 struct fpga_bridge
*bridge
= *(struct fpga_bridge
**)res
;
390 fpga_bridge_free(bridge
);
394 * devm_fpga_bridge_create - create and init a managed struct fpga_bridge
395 * @dev: FPGA bridge device from pdev
396 * @name: FPGA bridge name
397 * @br_ops: pointer to structure of fpga bridge ops
398 * @priv: FPGA bridge private data
400 * This function is intended for use in a FPGA bridge driver's probe function.
401 * After the bridge driver creates the struct with devm_fpga_bridge_create(), it
402 * should register the bridge with fpga_bridge_register(). The bridge driver's
403 * remove function should call fpga_bridge_unregister(). The bridge struct
404 * allocated with this function will be freed automatically on driver detach.
405 * This includes the case of a probe function returning error before calling
406 * fpga_bridge_register(), the struct will still get cleaned up.
408 * Return: struct fpga_bridge or NULL
411 *devm_fpga_bridge_create(struct device
*dev
, const char *name
,
412 const struct fpga_bridge_ops
*br_ops
, void *priv
)
414 struct fpga_bridge
**ptr
, *bridge
;
416 ptr
= devres_alloc(devm_fpga_bridge_release
, sizeof(*ptr
), GFP_KERNEL
);
420 bridge
= fpga_bridge_create(dev
, name
, br_ops
, priv
);
425 devres_add(dev
, ptr
);
430 EXPORT_SYMBOL_GPL(devm_fpga_bridge_create
);
433 * fpga_bridge_register - register a FPGA bridge
435 * @bridge: FPGA bridge struct
437 * Return: 0 for success, error code otherwise.
439 int fpga_bridge_register(struct fpga_bridge
*bridge
)
441 struct device
*dev
= &bridge
->dev
;
444 ret
= device_add(dev
);
448 of_platform_populate(dev
->of_node
, NULL
, NULL
, dev
);
450 dev_info(dev
->parent
, "fpga bridge [%s] registered\n", bridge
->name
);
454 EXPORT_SYMBOL_GPL(fpga_bridge_register
);
457 * fpga_bridge_unregister - unregister a FPGA bridge
459 * @bridge: FPGA bridge struct
461 * This function is intended for use in a FPGA bridge driver's remove function.
463 void fpga_bridge_unregister(struct fpga_bridge
*bridge
)
466 * If the low level driver provides a method for putting bridge into
467 * a desired state upon unregister, do it.
469 if (bridge
->br_ops
&& bridge
->br_ops
->fpga_bridge_remove
)
470 bridge
->br_ops
->fpga_bridge_remove(bridge
);
472 device_unregister(&bridge
->dev
);
474 EXPORT_SYMBOL_GPL(fpga_bridge_unregister
);
476 static void fpga_bridge_dev_release(struct device
*dev
)
480 static int __init
fpga_bridge_dev_init(void)
482 spin_lock_init(&bridge_list_lock
);
484 fpga_bridge_class
= class_create(THIS_MODULE
, "fpga_bridge");
485 if (IS_ERR(fpga_bridge_class
))
486 return PTR_ERR(fpga_bridge_class
);
488 fpga_bridge_class
->dev_groups
= fpga_bridge_groups
;
489 fpga_bridge_class
->dev_release
= fpga_bridge_dev_release
;
494 static void __exit
fpga_bridge_dev_exit(void)
496 class_destroy(fpga_bridge_class
);
497 ida_destroy(&fpga_bridge_ida
);
500 MODULE_DESCRIPTION("FPGA Bridge Driver");
501 MODULE_AUTHOR("Alan Tull <atull@kernel.org>");
502 MODULE_LICENSE("GPL v2");
504 subsys_initcall(fpga_bridge_dev_init
);
505 module_exit(fpga_bridge_dev_exit
);