Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 1 | /* |
| 2 | * phy.h -- generic phy header file |
| 3 | * |
| 4 | * Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com |
| 5 | * |
| 6 | * Author: Kishon Vijay Abraham I <kishon@ti.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | */ |
| 13 | |
| 14 | #ifndef __DRIVERS_PHY_H |
| 15 | #define __DRIVERS_PHY_H |
| 16 | |
| 17 | #include <linux/err.h> |
| 18 | #include <linux/of.h> |
| 19 | #include <linux/device.h> |
| 20 | #include <linux/pm_runtime.h> |
Roger Quadros | 3be8812 | 2014-07-04 12:55:45 +0300 | [diff] [blame] | 21 | #include <linux/regulator/consumer.h> |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 22 | |
| 23 | struct phy; |
| 24 | |
| 25 | /** |
| 26 | * struct phy_ops - set of function pointers for performing phy operations |
| 27 | * @init: operation to be performed for initializing phy |
| 28 | * @exit: operation to be performed while exiting |
| 29 | * @power_on: powering on the phy |
| 30 | * @power_off: powering off the phy |
| 31 | * @owner: the module owner containing the ops |
| 32 | */ |
| 33 | struct phy_ops { |
| 34 | int (*init)(struct phy *phy); |
| 35 | int (*exit)(struct phy *phy); |
| 36 | int (*power_on)(struct phy *phy); |
| 37 | int (*power_off)(struct phy *phy); |
| 38 | struct module *owner; |
| 39 | }; |
| 40 | |
| 41 | /** |
Matt Porter | 8feed34 | 2013-12-19 09:23:02 -0500 | [diff] [blame] | 42 | * struct phy_attrs - represents phy attributes |
| 43 | * @bus_width: Data path width implemented by PHY |
| 44 | */ |
| 45 | struct phy_attrs { |
| 46 | u32 bus_width; |
| 47 | }; |
| 48 | |
| 49 | /** |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 50 | * struct phy - represents the phy device |
| 51 | * @dev: phy device |
| 52 | * @id: id of the phy device |
| 53 | * @ops: function pointers for performing phy operations |
| 54 | * @init_data: list of PHY consumers (non-dt only) |
| 55 | * @mutex: mutex to protect phy_ops |
| 56 | * @init_count: used to protect when the PHY is used by multiple consumers |
| 57 | * @power_count: used to protect when the PHY is used by multiple consumers |
Matt Porter | 8feed34 | 2013-12-19 09:23:02 -0500 | [diff] [blame] | 58 | * @phy_attrs: used to specify PHY specific attributes |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 59 | */ |
| 60 | struct phy { |
| 61 | struct device dev; |
| 62 | int id; |
| 63 | const struct phy_ops *ops; |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 64 | struct mutex mutex; |
| 65 | int init_count; |
| 66 | int power_count; |
Matt Porter | 8feed34 | 2013-12-19 09:23:02 -0500 | [diff] [blame] | 67 | struct phy_attrs attrs; |
Roger Quadros | 3be8812 | 2014-07-04 12:55:45 +0300 | [diff] [blame] | 68 | struct regulator *pwr; |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | /** |
| 72 | * struct phy_provider - represents the phy provider |
| 73 | * @dev: phy provider device |
| 74 | * @owner: the module owner having of_xlate |
| 75 | * @of_xlate: function pointer to obtain phy instance from phy pointer |
| 76 | * @list: to maintain a linked list of PHY providers |
| 77 | */ |
| 78 | struct phy_provider { |
| 79 | struct device *dev; |
Thierry Reding | 1140f7c | 2016-04-05 17:17:34 +0200 | [diff] [blame] | 80 | struct device_node *children; |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 81 | struct module *owner; |
| 82 | struct list_head list; |
| 83 | struct phy * (*of_xlate)(struct device *dev, |
| 84 | struct of_phandle_args *args); |
| 85 | }; |
| 86 | |
Heikki Krogerus | b7bc15b | 2014-11-19 17:28:18 +0200 | [diff] [blame] | 87 | struct phy_lookup { |
| 88 | struct list_head node; |
| 89 | const char *dev_id; |
| 90 | const char *con_id; |
| 91 | struct phy *phy; |
| 92 | }; |
| 93 | |
Heikki Krogerus | d451057 | 2014-11-19 17:28:17 +0200 | [diff] [blame] | 94 | #define to_phy(a) (container_of((a), struct phy, dev)) |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 95 | |
| 96 | #define of_phy_provider_register(dev, xlate) \ |
Thierry Reding | 1140f7c | 2016-04-05 17:17:34 +0200 | [diff] [blame] | 97 | __of_phy_provider_register((dev), NULL, THIS_MODULE, (xlate)) |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 98 | |
| 99 | #define devm_of_phy_provider_register(dev, xlate) \ |
Thierry Reding | 1140f7c | 2016-04-05 17:17:34 +0200 | [diff] [blame] | 100 | __devm_of_phy_provider_register((dev), NULL, THIS_MODULE, (xlate)) |
| 101 | |
| 102 | #define of_phy_provider_register_full(dev, children, xlate) \ |
| 103 | __of_phy_provider_register(dev, children, THIS_MODULE, xlate) |
| 104 | |
| 105 | #define devm_of_phy_provider_register_full(dev, children, xlate) \ |
| 106 | __devm_of_phy_provider_register(dev, children, THIS_MODULE, xlate) |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 107 | |
| 108 | static inline void phy_set_drvdata(struct phy *phy, void *data) |
| 109 | { |
| 110 | dev_set_drvdata(&phy->dev, data); |
| 111 | } |
| 112 | |
| 113 | static inline void *phy_get_drvdata(struct phy *phy) |
| 114 | { |
| 115 | return dev_get_drvdata(&phy->dev); |
| 116 | } |
| 117 | |
| 118 | #if IS_ENABLED(CONFIG_GENERIC_PHY) |
| 119 | int phy_pm_runtime_get(struct phy *phy); |
| 120 | int phy_pm_runtime_get_sync(struct phy *phy); |
| 121 | int phy_pm_runtime_put(struct phy *phy); |
| 122 | int phy_pm_runtime_put_sync(struct phy *phy); |
| 123 | void phy_pm_runtime_allow(struct phy *phy); |
| 124 | void phy_pm_runtime_forbid(struct phy *phy); |
| 125 | int phy_init(struct phy *phy); |
| 126 | int phy_exit(struct phy *phy); |
| 127 | int phy_power_on(struct phy *phy); |
| 128 | int phy_power_off(struct phy *phy); |
Matt Porter | 8feed34 | 2013-12-19 09:23:02 -0500 | [diff] [blame] | 129 | static inline int phy_get_bus_width(struct phy *phy) |
| 130 | { |
| 131 | return phy->attrs.bus_width; |
| 132 | } |
| 133 | static inline void phy_set_bus_width(struct phy *phy, int bus_width) |
| 134 | { |
| 135 | phy->attrs.bus_width = bus_width; |
| 136 | } |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 137 | struct phy *phy_get(struct device *dev, const char *string); |
Andrew Lunn | 788a4d5 | 2014-02-04 18:33:12 +0100 | [diff] [blame] | 138 | struct phy *phy_optional_get(struct device *dev, const char *string); |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 139 | struct phy *devm_phy_get(struct device *dev, const char *string); |
Andrew Lunn | 788a4d5 | 2014-02-04 18:33:12 +0100 | [diff] [blame] | 140 | struct phy *devm_phy_optional_get(struct device *dev, const char *string); |
Kamil Debski | b5d682f | 2014-03-06 12:16:47 +0100 | [diff] [blame] | 141 | struct phy *devm_of_phy_get(struct device *dev, struct device_node *np, |
| 142 | const char *con_id); |
Arun Ramamurthy | 6be109b | 2015-04-22 16:04:11 -0700 | [diff] [blame] | 143 | struct phy *devm_of_phy_get_by_index(struct device *dev, struct device_node *np, |
| 144 | int index); |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 145 | void phy_put(struct phy *phy); |
| 146 | void devm_phy_put(struct device *dev, struct phy *phy); |
Kamil Debski | 0b3f3b2 | 2014-03-06 12:16:46 +0100 | [diff] [blame] | 147 | struct phy *of_phy_get(struct device_node *np, const char *con_id); |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 148 | struct phy *of_phy_simple_xlate(struct device *dev, |
| 149 | struct of_phandle_args *args); |
Kishon Vijay Abraham I | f0ed817 | 2014-07-14 15:55:02 +0530 | [diff] [blame] | 150 | struct phy *phy_create(struct device *dev, struct device_node *node, |
Heikki Krogerus | dbc9863 | 2014-11-19 17:28:21 +0200 | [diff] [blame] | 151 | const struct phy_ops *ops); |
Kishon Vijay Abraham I | f0ed817 | 2014-07-14 15:55:02 +0530 | [diff] [blame] | 152 | struct phy *devm_phy_create(struct device *dev, struct device_node *node, |
Heikki Krogerus | dbc9863 | 2014-11-19 17:28:21 +0200 | [diff] [blame] | 153 | const struct phy_ops *ops); |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 154 | void phy_destroy(struct phy *phy); |
| 155 | void devm_phy_destroy(struct device *dev, struct phy *phy); |
| 156 | struct phy_provider *__of_phy_provider_register(struct device *dev, |
Thierry Reding | 1140f7c | 2016-04-05 17:17:34 +0200 | [diff] [blame] | 157 | struct device_node *children, struct module *owner, |
| 158 | struct phy * (*of_xlate)(struct device *dev, |
| 159 | struct of_phandle_args *args)); |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 160 | struct phy_provider *__devm_of_phy_provider_register(struct device *dev, |
Thierry Reding | 1140f7c | 2016-04-05 17:17:34 +0200 | [diff] [blame] | 161 | struct device_node *children, struct module *owner, |
| 162 | struct phy * (*of_xlate)(struct device *dev, |
| 163 | struct of_phandle_args *args)); |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 164 | void of_phy_provider_unregister(struct phy_provider *phy_provider); |
| 165 | void devm_of_phy_provider_unregister(struct device *dev, |
| 166 | struct phy_provider *phy_provider); |
Heikki Krogerus | b7bc15b | 2014-11-19 17:28:18 +0200 | [diff] [blame] | 167 | int phy_create_lookup(struct phy *phy, const char *con_id, const char *dev_id); |
| 168 | void phy_remove_lookup(struct phy *phy, const char *con_id, const char *dev_id); |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 169 | #else |
| 170 | static inline int phy_pm_runtime_get(struct phy *phy) |
| 171 | { |
Grygorii Strashko | 2b97789 | 2014-04-19 08:51:44 +0530 | [diff] [blame] | 172 | if (!phy) |
| 173 | return 0; |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 174 | return -ENOSYS; |
| 175 | } |
| 176 | |
| 177 | static inline int phy_pm_runtime_get_sync(struct phy *phy) |
| 178 | { |
Grygorii Strashko | 2b97789 | 2014-04-19 08:51:44 +0530 | [diff] [blame] | 179 | if (!phy) |
| 180 | return 0; |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 181 | return -ENOSYS; |
| 182 | } |
| 183 | |
| 184 | static inline int phy_pm_runtime_put(struct phy *phy) |
| 185 | { |
Grygorii Strashko | 2b97789 | 2014-04-19 08:51:44 +0530 | [diff] [blame] | 186 | if (!phy) |
| 187 | return 0; |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 188 | return -ENOSYS; |
| 189 | } |
| 190 | |
| 191 | static inline int phy_pm_runtime_put_sync(struct phy *phy) |
| 192 | { |
Grygorii Strashko | 2b97789 | 2014-04-19 08:51:44 +0530 | [diff] [blame] | 193 | if (!phy) |
| 194 | return 0; |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 195 | return -ENOSYS; |
| 196 | } |
| 197 | |
| 198 | static inline void phy_pm_runtime_allow(struct phy *phy) |
| 199 | { |
| 200 | return; |
| 201 | } |
| 202 | |
| 203 | static inline void phy_pm_runtime_forbid(struct phy *phy) |
| 204 | { |
| 205 | return; |
| 206 | } |
| 207 | |
| 208 | static inline int phy_init(struct phy *phy) |
| 209 | { |
Grygorii Strashko | 2b97789 | 2014-04-19 08:51:44 +0530 | [diff] [blame] | 210 | if (!phy) |
| 211 | return 0; |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 212 | return -ENOSYS; |
| 213 | } |
| 214 | |
| 215 | static inline int phy_exit(struct phy *phy) |
| 216 | { |
Grygorii Strashko | 2b97789 | 2014-04-19 08:51:44 +0530 | [diff] [blame] | 217 | if (!phy) |
| 218 | return 0; |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 219 | return -ENOSYS; |
| 220 | } |
| 221 | |
| 222 | static inline int phy_power_on(struct phy *phy) |
| 223 | { |
Grygorii Strashko | 2b97789 | 2014-04-19 08:51:44 +0530 | [diff] [blame] | 224 | if (!phy) |
| 225 | return 0; |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 226 | return -ENOSYS; |
| 227 | } |
| 228 | |
| 229 | static inline int phy_power_off(struct phy *phy) |
| 230 | { |
Grygorii Strashko | 2b97789 | 2014-04-19 08:51:44 +0530 | [diff] [blame] | 231 | if (!phy) |
| 232 | return 0; |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 233 | return -ENOSYS; |
| 234 | } |
| 235 | |
Matt Porter | 8feed34 | 2013-12-19 09:23:02 -0500 | [diff] [blame] | 236 | static inline int phy_get_bus_width(struct phy *phy) |
| 237 | { |
| 238 | return -ENOSYS; |
| 239 | } |
| 240 | |
| 241 | static inline void phy_set_bus_width(struct phy *phy, int bus_width) |
| 242 | { |
| 243 | return; |
| 244 | } |
| 245 | |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 246 | static inline struct phy *phy_get(struct device *dev, const char *string) |
| 247 | { |
| 248 | return ERR_PTR(-ENOSYS); |
| 249 | } |
| 250 | |
Andrew Lunn | 788a4d5 | 2014-02-04 18:33:12 +0100 | [diff] [blame] | 251 | static inline struct phy *phy_optional_get(struct device *dev, |
| 252 | const char *string) |
| 253 | { |
| 254 | return ERR_PTR(-ENOSYS); |
| 255 | } |
| 256 | |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 257 | static inline struct phy *devm_phy_get(struct device *dev, const char *string) |
| 258 | { |
| 259 | return ERR_PTR(-ENOSYS); |
| 260 | } |
| 261 | |
Andrew Lunn | 788a4d5 | 2014-02-04 18:33:12 +0100 | [diff] [blame] | 262 | static inline struct phy *devm_phy_optional_get(struct device *dev, |
| 263 | const char *string) |
| 264 | { |
| 265 | return ERR_PTR(-ENOSYS); |
| 266 | } |
| 267 | |
Kamil Debski | b5d682f | 2014-03-06 12:16:47 +0100 | [diff] [blame] | 268 | static inline struct phy *devm_of_phy_get(struct device *dev, |
| 269 | struct device_node *np, |
| 270 | const char *con_id) |
| 271 | { |
| 272 | return ERR_PTR(-ENOSYS); |
| 273 | } |
| 274 | |
Arun Ramamurthy | 6be109b | 2015-04-22 16:04:11 -0700 | [diff] [blame] | 275 | static inline struct phy *devm_of_phy_get_by_index(struct device *dev, |
| 276 | struct device_node *np, |
| 277 | int index) |
| 278 | { |
| 279 | return ERR_PTR(-ENOSYS); |
| 280 | } |
| 281 | |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 282 | static inline void phy_put(struct phy *phy) |
| 283 | { |
| 284 | } |
| 285 | |
| 286 | static inline void devm_phy_put(struct device *dev, struct phy *phy) |
| 287 | { |
| 288 | } |
| 289 | |
Kamil Debski | 0b3f3b2 | 2014-03-06 12:16:46 +0100 | [diff] [blame] | 290 | static inline struct phy *of_phy_get(struct device_node *np, const char *con_id) |
| 291 | { |
| 292 | return ERR_PTR(-ENOSYS); |
| 293 | } |
| 294 | |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 295 | static inline struct phy *of_phy_simple_xlate(struct device *dev, |
| 296 | struct of_phandle_args *args) |
| 297 | { |
| 298 | return ERR_PTR(-ENOSYS); |
| 299 | } |
| 300 | |
| 301 | static inline struct phy *phy_create(struct device *dev, |
Kishon Vijay Abraham I | f0ed817 | 2014-07-14 15:55:02 +0530 | [diff] [blame] | 302 | struct device_node *node, |
Heikki Krogerus | dbc9863 | 2014-11-19 17:28:21 +0200 | [diff] [blame] | 303 | const struct phy_ops *ops) |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 304 | { |
| 305 | return ERR_PTR(-ENOSYS); |
| 306 | } |
| 307 | |
| 308 | static inline struct phy *devm_phy_create(struct device *dev, |
Kishon Vijay Abraham I | f0ed817 | 2014-07-14 15:55:02 +0530 | [diff] [blame] | 309 | struct device_node *node, |
Heikki Krogerus | dbc9863 | 2014-11-19 17:28:21 +0200 | [diff] [blame] | 310 | const struct phy_ops *ops) |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 311 | { |
| 312 | return ERR_PTR(-ENOSYS); |
| 313 | } |
| 314 | |
| 315 | static inline void phy_destroy(struct phy *phy) |
| 316 | { |
| 317 | } |
| 318 | |
| 319 | static inline void devm_phy_destroy(struct device *dev, struct phy *phy) |
| 320 | { |
| 321 | } |
| 322 | |
| 323 | static inline struct phy_provider *__of_phy_provider_register( |
Thierry Reding | 1140f7c | 2016-04-05 17:17:34 +0200 | [diff] [blame] | 324 | struct device *dev, struct device_node *children, struct module *owner, |
| 325 | struct phy * (*of_xlate)(struct device *dev, |
| 326 | struct of_phandle_args *args)) |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 327 | { |
| 328 | return ERR_PTR(-ENOSYS); |
| 329 | } |
| 330 | |
| 331 | static inline struct phy_provider *__devm_of_phy_provider_register(struct device |
Thierry Reding | 1140f7c | 2016-04-05 17:17:34 +0200 | [diff] [blame] | 332 | *dev, struct device_node *children, struct module *owner, |
| 333 | struct phy * (*of_xlate)(struct device *dev, |
| 334 | struct of_phandle_args *args)) |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 335 | { |
| 336 | return ERR_PTR(-ENOSYS); |
| 337 | } |
| 338 | |
| 339 | static inline void of_phy_provider_unregister(struct phy_provider *phy_provider) |
| 340 | { |
| 341 | } |
| 342 | |
| 343 | static inline void devm_of_phy_provider_unregister(struct device *dev, |
| 344 | struct phy_provider *phy_provider) |
| 345 | { |
| 346 | } |
Heikki Krogerus | b7bc15b | 2014-11-19 17:28:18 +0200 | [diff] [blame] | 347 | static inline int |
| 348 | phy_create_lookup(struct phy *phy, const char *con_id, const char *dev_id) |
| 349 | { |
| 350 | return 0; |
| 351 | } |
| 352 | static inline void phy_remove_lookup(struct phy *phy, const char *con_id, |
| 353 | const char *dev_id) { } |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 354 | #endif |
| 355 | |
| 356 | #endif /* __DRIVERS_PHY_H */ |