1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3 * MEN Chameleon Bus.
4 *
5 * Copyright (C) 2014 MEN Mikroelektronik GmbH (www.men.de)
6 * Author: Johannes Thumshirn <johannes.thumshirn@men.de>
7 */
8 #ifndef _LINUX_MCB_H
9 #define _LINUX_MCB_H
10
11 #include <linux/mod_devicetable.h>
12 #include <linux/device.h>
13 #include <linux/irqreturn.h>
14
15 #define CHAMELEON_FILENAME_LEN 12
16
17 struct mcb_driver;
18 struct mcb_device;
19
20 /**
21 * struct mcb_bus - MEN Chameleon Bus
22 *
23 * @dev: bus device
24 * @carrier: pointer to carrier device
25 * @bus_nr: mcb bus number
26 * @get_irq: callback to get IRQ number
27 * @revision: the FPGA's revision number
28 * @model: the FPGA's model number
29 * @filename: the FPGA's name
30 */
31 struct mcb_bus {
32 struct device dev;
33 struct device *carrier;
34 int bus_nr;
35 u8 revision;
36 char model;
37 u8 minor;
38 char name[CHAMELEON_FILENAME_LEN + 1];
39 int (*get_irq)(struct mcb_device *dev);
40 };
41
to_mcb_bus(struct device * dev)42 static inline struct mcb_bus *to_mcb_bus(struct device *dev)
43 {
44 return container_of(dev, struct mcb_bus, dev);
45 }
46
47 /**
48 * struct mcb_device - MEN Chameleon Bus device
49 *
50 * @dev: device in kernel representation
51 * @bus: mcb bus the device is plugged to
52 * @is_added: flag to check if device is added to bus
53 * @driver: associated mcb_driver
54 * @id: mcb device id
55 * @inst: instance in Chameleon table
56 * @group: group in Chameleon table
57 * @var: variant in Chameleon table
58 * @bar: BAR in Chameleon table
59 * @rev: revision in Chameleon table
60 * @irq: IRQ resource
61 * @memory: memory resource
62 */
63 struct mcb_device {
64 struct device dev;
65 struct mcb_bus *bus;
66 struct mcb_driver *driver;
67 u16 id;
68 int inst;
69 int group;
70 int var;
71 int bar;
72 int rev;
73 struct resource irq;
74 struct resource mem;
75 struct device *dma_dev;
76 };
77
78 #define to_mcb_device(__dev) container_of_const(__dev, struct mcb_device, dev)
79
80 /**
81 * struct mcb_driver - MEN Chameleon Bus device driver
82 *
83 * @driver: device_driver
84 * @id_table: mcb id table
85 * @probe: probe callback
86 * @remove: remove callback
87 * @shutdown: shutdown callback
88 */
89 struct mcb_driver {
90 struct device_driver driver;
91 const struct mcb_device_id *id_table;
92 int (*probe)(struct mcb_device *mdev, const struct mcb_device_id *id);
93 void (*remove)(struct mcb_device *mdev);
94 void (*shutdown)(struct mcb_device *mdev);
95 };
96
97 #define to_mcb_driver(__drv) container_of_const(__drv, struct mcb_driver, driver)
98
mcb_get_drvdata(struct mcb_device * dev)99 static inline void *mcb_get_drvdata(struct mcb_device *dev)
100 {
101 return dev_get_drvdata(&dev->dev);
102 }
103
mcb_set_drvdata(struct mcb_device * dev,void * data)104 static inline void mcb_set_drvdata(struct mcb_device *dev, void *data)
105 {
106 dev_set_drvdata(&dev->dev, data);
107 }
108
109 extern int __must_check __mcb_register_driver(struct mcb_driver *drv,
110 struct module *owner,
111 const char *mod_name);
112 #define mcb_register_driver(driver) \
113 __mcb_register_driver(driver, THIS_MODULE, KBUILD_MODNAME)
114 extern void mcb_unregister_driver(struct mcb_driver *driver);
115 #define module_mcb_driver(__mcb_driver) \
116 module_driver(__mcb_driver, mcb_register_driver, mcb_unregister_driver)
117 extern void mcb_bus_add_devices(const struct mcb_bus *bus);
118 extern int mcb_device_register(struct mcb_bus *bus, struct mcb_device *dev);
119 extern struct mcb_bus *mcb_alloc_bus(struct device *carrier);
120 extern struct mcb_bus *mcb_bus_get(struct mcb_bus *bus);
121 extern void mcb_bus_put(struct mcb_bus *bus);
122 extern struct mcb_device *mcb_alloc_dev(struct mcb_bus *bus);
123 extern void mcb_free_dev(struct mcb_device *dev);
124 extern void mcb_release_bus(struct mcb_bus *bus);
125 extern struct resource *mcb_request_mem(struct mcb_device *dev,
126 const char *name);
127 extern void mcb_release_mem(struct resource *mem);
128 extern int mcb_get_irq(struct mcb_device *dev);
129 extern struct resource *mcb_get_resource(struct mcb_device *dev,
130 unsigned int type);
131
132 #endif /* _LINUX_MCB_H */
133