xref: /OK3568_Linux_fs/kernel/include/linux/greybus.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Greybus driver and device API
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright 2014-2015 Google Inc.
6*4882a593Smuzhiyun  * Copyright 2014-2015 Linaro Ltd.
7*4882a593Smuzhiyun  */
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun #ifndef __LINUX_GREYBUS_H
10*4882a593Smuzhiyun #define __LINUX_GREYBUS_H
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun #ifdef __KERNEL__
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun #include <linux/kernel.h>
15*4882a593Smuzhiyun #include <linux/types.h>
16*4882a593Smuzhiyun #include <linux/list.h>
17*4882a593Smuzhiyun #include <linux/slab.h>
18*4882a593Smuzhiyun #include <linux/device.h>
19*4882a593Smuzhiyun #include <linux/module.h>
20*4882a593Smuzhiyun #include <linux/pm_runtime.h>
21*4882a593Smuzhiyun #include <linux/idr.h>
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun #include <linux/greybus/greybus_id.h>
24*4882a593Smuzhiyun #include <linux/greybus/greybus_manifest.h>
25*4882a593Smuzhiyun #include <linux/greybus/greybus_protocols.h>
26*4882a593Smuzhiyun #include <linux/greybus/manifest.h>
27*4882a593Smuzhiyun #include <linux/greybus/hd.h>
28*4882a593Smuzhiyun #include <linux/greybus/svc.h>
29*4882a593Smuzhiyun #include <linux/greybus/control.h>
30*4882a593Smuzhiyun #include <linux/greybus/module.h>
31*4882a593Smuzhiyun #include <linux/greybus/interface.h>
32*4882a593Smuzhiyun #include <linux/greybus/bundle.h>
33*4882a593Smuzhiyun #include <linux/greybus/connection.h>
34*4882a593Smuzhiyun #include <linux/greybus/operation.h>
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun /* Matches up with the Greybus Protocol specification document */
37*4882a593Smuzhiyun #define GREYBUS_VERSION_MAJOR	0x00
38*4882a593Smuzhiyun #define GREYBUS_VERSION_MINOR	0x01
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun #define GREYBUS_ID_MATCH_DEVICE \
41*4882a593Smuzhiyun 	(GREYBUS_ID_MATCH_VENDOR | GREYBUS_ID_MATCH_PRODUCT)
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun #define GREYBUS_DEVICE(v, p)					\
44*4882a593Smuzhiyun 	.match_flags	= GREYBUS_ID_MATCH_DEVICE,		\
45*4882a593Smuzhiyun 	.vendor		= (v),					\
46*4882a593Smuzhiyun 	.product	= (p),
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun #define GREYBUS_DEVICE_CLASS(c)					\
49*4882a593Smuzhiyun 	.match_flags	= GREYBUS_ID_MATCH_CLASS,		\
50*4882a593Smuzhiyun 	.class		= (c),
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun /* Maximum number of CPorts */
53*4882a593Smuzhiyun #define CPORT_ID_MAX	4095		/* UniPro max id is 4095 */
54*4882a593Smuzhiyun #define CPORT_ID_BAD	U16_MAX
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun struct greybus_driver {
57*4882a593Smuzhiyun 	const char *name;
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun 	int (*probe)(struct gb_bundle *bundle,
60*4882a593Smuzhiyun 		     const struct greybus_bundle_id *id);
61*4882a593Smuzhiyun 	void (*disconnect)(struct gb_bundle *bundle);
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun 	const struct greybus_bundle_id *id_table;
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun 	struct device_driver driver;
66*4882a593Smuzhiyun };
67*4882a593Smuzhiyun #define to_greybus_driver(d) container_of(d, struct greybus_driver, driver)
68*4882a593Smuzhiyun 
greybus_set_drvdata(struct gb_bundle * bundle,void * data)69*4882a593Smuzhiyun static inline void greybus_set_drvdata(struct gb_bundle *bundle, void *data)
70*4882a593Smuzhiyun {
71*4882a593Smuzhiyun 	dev_set_drvdata(&bundle->dev, data);
72*4882a593Smuzhiyun }
73*4882a593Smuzhiyun 
greybus_get_drvdata(struct gb_bundle * bundle)74*4882a593Smuzhiyun static inline void *greybus_get_drvdata(struct gb_bundle *bundle)
75*4882a593Smuzhiyun {
76*4882a593Smuzhiyun 	return dev_get_drvdata(&bundle->dev);
77*4882a593Smuzhiyun }
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun /* Don't call these directly, use the module_greybus_driver() macro instead */
80*4882a593Smuzhiyun int greybus_register_driver(struct greybus_driver *driver,
81*4882a593Smuzhiyun 			    struct module *module, const char *mod_name);
82*4882a593Smuzhiyun void greybus_deregister_driver(struct greybus_driver *driver);
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun /* define to get proper THIS_MODULE and KBUILD_MODNAME values */
85*4882a593Smuzhiyun #define greybus_register(driver) \
86*4882a593Smuzhiyun 	greybus_register_driver(driver, THIS_MODULE, KBUILD_MODNAME)
87*4882a593Smuzhiyun #define greybus_deregister(driver) \
88*4882a593Smuzhiyun 	greybus_deregister_driver(driver)
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun /**
91*4882a593Smuzhiyun  * module_greybus_driver() - Helper macro for registering a Greybus driver
92*4882a593Smuzhiyun  * @__greybus_driver: greybus_driver structure
93*4882a593Smuzhiyun  *
94*4882a593Smuzhiyun  * Helper macro for Greybus drivers to set up proper module init / exit
95*4882a593Smuzhiyun  * functions.  Replaces module_init() and module_exit() and keeps people from
96*4882a593Smuzhiyun  * printing pointless things to the kernel log when their driver is loaded.
97*4882a593Smuzhiyun  */
98*4882a593Smuzhiyun #define module_greybus_driver(__greybus_driver)	\
99*4882a593Smuzhiyun 	module_driver(__greybus_driver, greybus_register, greybus_deregister)
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun int greybus_disabled(void);
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun void gb_debugfs_init(void);
104*4882a593Smuzhiyun void gb_debugfs_cleanup(void);
105*4882a593Smuzhiyun struct dentry *gb_debugfs_get(void);
106*4882a593Smuzhiyun 
107*4882a593Smuzhiyun extern struct bus_type greybus_bus_type;
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun extern struct device_type greybus_hd_type;
110*4882a593Smuzhiyun extern struct device_type greybus_module_type;
111*4882a593Smuzhiyun extern struct device_type greybus_interface_type;
112*4882a593Smuzhiyun extern struct device_type greybus_control_type;
113*4882a593Smuzhiyun extern struct device_type greybus_bundle_type;
114*4882a593Smuzhiyun extern struct device_type greybus_svc_type;
115*4882a593Smuzhiyun 
is_gb_host_device(const struct device * dev)116*4882a593Smuzhiyun static inline int is_gb_host_device(const struct device *dev)
117*4882a593Smuzhiyun {
118*4882a593Smuzhiyun 	return dev->type == &greybus_hd_type;
119*4882a593Smuzhiyun }
120*4882a593Smuzhiyun 
is_gb_module(const struct device * dev)121*4882a593Smuzhiyun static inline int is_gb_module(const struct device *dev)
122*4882a593Smuzhiyun {
123*4882a593Smuzhiyun 	return dev->type == &greybus_module_type;
124*4882a593Smuzhiyun }
125*4882a593Smuzhiyun 
is_gb_interface(const struct device * dev)126*4882a593Smuzhiyun static inline int is_gb_interface(const struct device *dev)
127*4882a593Smuzhiyun {
128*4882a593Smuzhiyun 	return dev->type == &greybus_interface_type;
129*4882a593Smuzhiyun }
130*4882a593Smuzhiyun 
is_gb_control(const struct device * dev)131*4882a593Smuzhiyun static inline int is_gb_control(const struct device *dev)
132*4882a593Smuzhiyun {
133*4882a593Smuzhiyun 	return dev->type == &greybus_control_type;
134*4882a593Smuzhiyun }
135*4882a593Smuzhiyun 
is_gb_bundle(const struct device * dev)136*4882a593Smuzhiyun static inline int is_gb_bundle(const struct device *dev)
137*4882a593Smuzhiyun {
138*4882a593Smuzhiyun 	return dev->type == &greybus_bundle_type;
139*4882a593Smuzhiyun }
140*4882a593Smuzhiyun 
is_gb_svc(const struct device * dev)141*4882a593Smuzhiyun static inline int is_gb_svc(const struct device *dev)
142*4882a593Smuzhiyun {
143*4882a593Smuzhiyun 	return dev->type == &greybus_svc_type;
144*4882a593Smuzhiyun }
145*4882a593Smuzhiyun 
cport_id_valid(struct gb_host_device * hd,u16 cport_id)146*4882a593Smuzhiyun static inline bool cport_id_valid(struct gb_host_device *hd, u16 cport_id)
147*4882a593Smuzhiyun {
148*4882a593Smuzhiyun 	return cport_id != CPORT_ID_BAD && cport_id < hd->num_cports;
149*4882a593Smuzhiyun }
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun #endif /* __KERNEL__ */
152*4882a593Smuzhiyun #endif /* __LINUX_GREYBUS_H */
153