Linux 4.19.133
[linux/fpc-iii.git] / drivers / fpga / fpga-bridge.c
blobc983dac97501b910e020996d53ec04609d99a265
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * FPGA Bridge Framework Driver
5 * Copyright (C) 2013-2016 Altera Corporation, All Rights Reserved.
6 * Copyright (C) 2017 Intel Corporation
7 */
8 #include <linux/fpga/fpga-bridge.h>
9 #include <linux/idr.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;
27 /**
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);
41 return 0;
43 EXPORT_SYMBOL_GPL(fpga_bridge_enable);
45 /**
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);
59 return 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;
67 int ret = -ENODEV;
69 bridge = to_fpga_bridge(dev);
71 bridge->info = info;
73 if (!mutex_trylock(&bridge->mutex)) {
74 ret = -EBUSY;
75 goto err_dev;
78 if (!try_module_get(dev->parent->driver->owner))
79 goto err_ll_mod;
81 dev_dbg(&bridge->dev, "get\n");
83 return bridge;
85 err_ll_mod:
86 mutex_unlock(&bridge->mutex);
87 err_dev:
88 put_device(dev);
89 return ERR_PTR(ret);
92 /**
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)
105 struct device *dev;
107 dev = class_find_device(fpga_bridge_class, NULL, np,
108 fpga_bridge_of_node_match);
109 if (!dev)
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);
137 if (!bridge_dev)
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");
153 bridge->info = NULL;
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;
171 int ret;
173 list_for_each_entry(bridge, bridge_list, node) {
174 ret = fpga_bridge_enable(bridge);
175 if (ret)
176 return ret;
179 return 0;
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;
195 int ret;
197 list_for_each_entry(bridge, bridge_list, node) {
198 ret = fpga_bridge_disable(bridge);
199 if (ret)
200 return ret;
203 return 0;
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;
218 unsigned long flags;
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;
246 unsigned long flags;
248 bridge = of_fpga_bridge_get(np, info);
249 if (IS_ERR(bridge))
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);
256 return 0;
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;
276 unsigned long flags;
278 bridge = fpga_bridge_get(dev, info);
279 if (IS_ERR(bridge))
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);
286 return 0;
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);
302 int enable = 1;
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[] = {
314 &dev_attr_name.attr,
315 &dev_attr_state.attr,
316 NULL,
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 * Return: struct fpga_bridge or NULL
329 struct fpga_bridge *fpga_bridge_create(struct device *dev, const char *name,
330 const struct fpga_bridge_ops *br_ops,
331 void *priv)
333 struct fpga_bridge *bridge;
334 int id, ret = 0;
336 if (!name || !strlen(name)) {
337 dev_err(dev, "Attempt to register with no name!\n");
338 return NULL;
341 bridge = kzalloc(sizeof(*bridge), GFP_KERNEL);
342 if (!bridge)
343 return NULL;
345 id = ida_simple_get(&fpga_bridge_ida, 0, 0, GFP_KERNEL);
346 if (id < 0) {
347 ret = id;
348 goto error_kfree;
351 mutex_init(&bridge->mutex);
352 INIT_LIST_HEAD(&bridge->node);
354 bridge->name = name;
355 bridge->br_ops = br_ops;
356 bridge->priv = priv;
358 device_initialize(&bridge->dev);
359 bridge->dev.groups = br_ops->groups;
360 bridge->dev.class = fpga_bridge_class;
361 bridge->dev.parent = dev;
362 bridge->dev.of_node = dev->of_node;
363 bridge->dev.id = id;
365 ret = dev_set_name(&bridge->dev, "br%d", id);
366 if (ret)
367 goto error_device;
369 return bridge;
371 error_device:
372 ida_simple_remove(&fpga_bridge_ida, id);
373 error_kfree:
374 kfree(bridge);
376 return NULL;
378 EXPORT_SYMBOL_GPL(fpga_bridge_create);
381 * fpga_bridge_free - free a fpga bridge and its id
382 * @bridge: FPGA bridge struct created by fpga_bridge_create
384 void fpga_bridge_free(struct fpga_bridge *bridge)
386 ida_simple_remove(&fpga_bridge_ida, bridge->dev.id);
387 kfree(bridge);
389 EXPORT_SYMBOL_GPL(fpga_bridge_free);
392 * fpga_bridge_register - register a fpga bridge
393 * @bridge: FPGA bridge struct created by fpga_bridge_create
395 * Return: 0 for success, error code otherwise.
397 int fpga_bridge_register(struct fpga_bridge *bridge)
399 struct device *dev = &bridge->dev;
400 int ret;
402 ret = device_add(dev);
403 if (ret)
404 return ret;
406 of_platform_populate(dev->of_node, NULL, NULL, dev);
408 dev_info(dev->parent, "fpga bridge [%s] registered\n", bridge->name);
410 return 0;
412 EXPORT_SYMBOL_GPL(fpga_bridge_register);
415 * fpga_bridge_unregister - unregister and free a fpga bridge
416 * @bridge: FPGA bridge struct created by fpga_bridge_create
418 void fpga_bridge_unregister(struct fpga_bridge *bridge)
421 * If the low level driver provides a method for putting bridge into
422 * a desired state upon unregister, do it.
424 if (bridge->br_ops && bridge->br_ops->fpga_bridge_remove)
425 bridge->br_ops->fpga_bridge_remove(bridge);
427 device_unregister(&bridge->dev);
429 EXPORT_SYMBOL_GPL(fpga_bridge_unregister);
431 static void fpga_bridge_dev_release(struct device *dev)
433 struct fpga_bridge *bridge = to_fpga_bridge(dev);
435 fpga_bridge_free(bridge);
438 static int __init fpga_bridge_dev_init(void)
440 spin_lock_init(&bridge_list_lock);
442 fpga_bridge_class = class_create(THIS_MODULE, "fpga_bridge");
443 if (IS_ERR(fpga_bridge_class))
444 return PTR_ERR(fpga_bridge_class);
446 fpga_bridge_class->dev_groups = fpga_bridge_groups;
447 fpga_bridge_class->dev_release = fpga_bridge_dev_release;
449 return 0;
452 static void __exit fpga_bridge_dev_exit(void)
454 class_destroy(fpga_bridge_class);
455 ida_destroy(&fpga_bridge_ida);
458 MODULE_DESCRIPTION("FPGA Bridge Driver");
459 MODULE_AUTHOR("Alan Tull <atull@kernel.org>");
460 MODULE_LICENSE("GPL v2");
462 subsys_initcall(fpga_bridge_dev_init);
463 module_exit(fpga_bridge_dev_exit);