Linux-libre 4.9.174-gnu
[librecmc/linux-libre.git] / drivers / staging / greybus / gbphy.h
1 /*
2  * Greybus Bridged-Phy Bus driver
3  *
4  * Copyright 2016 Google Inc.
5  *
6  * Released under the GPLv2 only.
7  */
8
9 #ifndef __GBPHY_H
10 #define __GBPHY_H
11
12 struct gbphy_device {
13         u32 id;
14         struct greybus_descriptor_cport *cport_desc;
15         struct gb_bundle *bundle;
16         struct list_head list;
17         struct device dev;
18 };
19 #define to_gbphy_dev(d) container_of(d, struct gbphy_device, dev)
20
21 static inline void *gb_gbphy_get_data(struct gbphy_device *gdev)
22 {
23         return dev_get_drvdata(&gdev->dev);
24 }
25
26 static inline void gb_gbphy_set_data(struct gbphy_device *gdev, void *data)
27 {
28         dev_set_drvdata(&gdev->dev, data);
29 }
30
31 struct gbphy_device_id {
32         __u8 protocol_id;
33 };
34
35 #define GBPHY_PROTOCOL(p)               \
36         .protocol_id    = (p),
37
38 struct gbphy_driver {
39         const char *name;
40         int (*probe)(struct gbphy_device *,
41                      const struct gbphy_device_id *id);
42         void (*remove)(struct gbphy_device *);
43         const struct gbphy_device_id *id_table;
44
45         struct device_driver driver;
46 };
47 #define to_gbphy_driver(d) container_of(d, struct gbphy_driver, driver)
48
49 int gb_gbphy_register_driver(struct gbphy_driver *driver,
50                              struct module *owner, const char *mod_name);
51 void gb_gbphy_deregister_driver(struct gbphy_driver *driver);
52
53 #define gb_gbphy_register(driver) \
54         gb_gbphy_register_driver(driver, THIS_MODULE, KBUILD_MODNAME)
55 #define gb_gbphy_deregister(driver) \
56         gb_gbphy_deregister_driver(driver)
57
58 /**
59  * module_gbphy_driver() - Helper macro for registering a gbphy driver
60  * @__gbphy_driver: gbphy_driver structure
61  *
62  * Helper macro for gbphy drivers to set up proper module init / exit
63  * functions.  Replaces module_init() and module_exit() and keeps people from
64  * printing pointless things to the kernel log when their driver is loaded.
65  */
66 #define module_gbphy_driver(__gbphy_driver)     \
67         module_driver(__gbphy_driver, gb_gbphy_register, gb_gbphy_deregister)
68
69 #ifdef CONFIG_PM
70 static inline int gbphy_runtime_get_sync(struct gbphy_device *gbphy_dev)
71 {
72         struct device *dev = &gbphy_dev->dev;
73         int ret;
74
75         ret = pm_runtime_get_sync(dev);
76         if (ret < 0) {
77                 dev_err(dev, "pm_runtime_get_sync failed: %d\n", ret);
78                 pm_runtime_put_noidle(dev);
79                 return ret;
80         }
81
82         return 0;
83 }
84
85 static inline void gbphy_runtime_put_autosuspend(struct gbphy_device *gbphy_dev)
86 {
87         struct device *dev = &gbphy_dev->dev;
88
89         pm_runtime_mark_last_busy(dev);
90         pm_runtime_put_autosuspend(dev);
91 }
92
93 static inline void gbphy_runtime_get_noresume(struct gbphy_device *gbphy_dev)
94 {
95         pm_runtime_get_noresume(&gbphy_dev->dev);
96 }
97
98 static inline void gbphy_runtime_put_noidle(struct gbphy_device *gbphy_dev)
99 {
100         pm_runtime_put_noidle(&gbphy_dev->dev);
101 }
102 #else
103 static inline int gbphy_runtime_get_sync(struct gbphy_device *gbphy_dev) { return 0; }
104 static inline void gbphy_runtime_put_autosuspend(struct gbphy_device *gbphy_dev) {}
105 static inline void gbphy_runtime_get_noresume(struct gbphy_device *gbphy_dev) {}
106 static inline void gbphy_runtime_put_noidle(struct gbphy_device *gbphy_dev) {}
107 #endif
108
109 #endif /* __GBPHY_H */
110