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 <[email protected]> 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> 21 #include <linux/regulator/consumer.h> 22 23 struct phy; 24 25 enum phy_mode { 26 PHY_MODE_INVALID, 27 PHY_MODE_USB_HOST, 28 PHY_MODE_USB_HOST_LS, 29 PHY_MODE_USB_HOST_FS, 30 PHY_MODE_USB_HOST_HS, 31 PHY_MODE_USB_HOST_SS, 32 PHY_MODE_USB_DEVICE, 33 PHY_MODE_USB_DEVICE_LS, 34 PHY_MODE_USB_DEVICE_FS, 35 PHY_MODE_USB_DEVICE_HS, 36 PHY_MODE_USB_DEVICE_SS, 37 PHY_MODE_USB_OTG, 38 PHY_MODE_UFS_HS_A, 39 PHY_MODE_UFS_HS_B, 40 PHY_MODE_PCIE, 41 PHY_MODE_ETHERNET, 42 PHY_MODE_MIPI_DPHY, 43 }; 44 45 /** 46 * struct phy_ops - set of function pointers for performing phy operations 47 * @init: operation to be performed for initializing phy 48 * @exit: operation to be performed while exiting 49 * @power_on: powering on the phy 50 * @power_off: powering off the phy 51 * @set_mode: set the mode of the phy 52 * @reset: resetting the phy 53 * @calibrate: calibrate the phy 54 * @owner: the module owner containing the ops 55 */ 56 struct phy_ops { 57 int (*init)(struct phy *phy); 58 int (*exit)(struct phy *phy); 59 int (*power_on)(struct phy *phy); 60 int (*power_off)(struct phy *phy); 61 int (*set_mode)(struct phy *phy, enum phy_mode mode, int submode); 62 int (*reset)(struct phy *phy); 63 int (*calibrate)(struct phy *phy); 64 struct module *owner; 65 }; 66 67 /** 68 * struct phy_attrs - represents phy attributes 69 * @bus_width: Data path width implemented by PHY 70 */ 71 struct phy_attrs { 72 u32 bus_width; 73 enum phy_mode mode; 74 }; 75 76 /** 77 * struct phy - represents the phy device 78 * @dev: phy device 79 * @id: id of the phy device 80 * @ops: function pointers for performing phy operations 81 * @init_data: list of PHY consumers (non-dt only) 82 * @mutex: mutex to protect phy_ops 83 * @init_count: used to protect when the PHY is used by multiple consumers 84 * @power_count: used to protect when the PHY is used by multiple consumers 85 * @attrs: used to specify PHY specific attributes 86 * @pwr: power regulator associated with the phy 87 */ 88 struct phy { 89 struct device dev; 90 int id; 91 const struct phy_ops *ops; 92 struct mutex mutex; 93 int init_count; 94 int power_count; 95 struct phy_attrs attrs; 96 struct regulator *pwr; 97 }; 98 99 /** 100 * struct phy_provider - represents the phy provider 101 * @dev: phy provider device 102 * @children: can be used to override the default (dev->of_node) child node 103 * @owner: the module owner having of_xlate 104 * @list: to maintain a linked list of PHY providers 105 * @of_xlate: function pointer to obtain phy instance from phy pointer 106 */ 107 struct phy_provider { 108 struct device *dev; 109 struct device_node *children; 110 struct module *owner; 111 struct list_head list; 112 struct phy * (*of_xlate)(struct device *dev, 113 struct of_phandle_args *args); 114 }; 115 116 /** 117 * struct phy_lookup - PHY association in list of phys managed by the phy driver 118 * @node: list node 119 * @dev_id: the device of the association 120 * @con_id: connection ID string on device 121 * @phy: the phy of the association 122 */ 123 struct phy_lookup { 124 struct list_head node; 125 const char *dev_id; 126 const char *con_id; 127 struct phy *phy; 128 }; 129 130 #define to_phy(a) (container_of((a), struct phy, dev)) 131 132 #define of_phy_provider_register(dev, xlate) \ 133 __of_phy_provider_register((dev), NULL, THIS_MODULE, (xlate)) 134 135 #define devm_of_phy_provider_register(dev, xlate) \ 136 __devm_of_phy_provider_register((dev), NULL, THIS_MODULE, (xlate)) 137 138 #define of_phy_provider_register_full(dev, children, xlate) \ 139 __of_phy_provider_register(dev, children, THIS_MODULE, xlate) 140 141 #define devm_of_phy_provider_register_full(dev, children, xlate) \ 142 __devm_of_phy_provider_register(dev, children, THIS_MODULE, xlate) 143 144 static inline void phy_set_drvdata(struct phy *phy, void *data) 145 { 146 dev_set_drvdata(&phy->dev, data); 147 } 148 149 static inline void *phy_get_drvdata(struct phy *phy) 150 { 151 return dev_get_drvdata(&phy->dev); 152 } 153 154 #if IS_ENABLED(CONFIG_GENERIC_PHY) 155 int phy_pm_runtime_get(struct phy *phy); 156 int phy_pm_runtime_get_sync(struct phy *phy); 157 int phy_pm_runtime_put(struct phy *phy); 158 int phy_pm_runtime_put_sync(struct phy *phy); 159 void phy_pm_runtime_allow(struct phy *phy); 160 void phy_pm_runtime_forbid(struct phy *phy); 161 int phy_init(struct phy *phy); 162 int phy_exit(struct phy *phy); 163 int phy_power_on(struct phy *phy); 164 int phy_power_off(struct phy *phy); 165 int phy_set_mode_ext(struct phy *phy, enum phy_mode mode, int submode); 166 #define phy_set_mode(phy, mode) \ 167 phy_set_mode_ext(phy, mode, 0) 168 169 static inline enum phy_mode phy_get_mode(struct phy *phy) 170 { 171 return phy->attrs.mode; 172 } 173 int phy_reset(struct phy *phy); 174 int phy_calibrate(struct phy *phy); 175 static inline int phy_get_bus_width(struct phy *phy) 176 { 177 return phy->attrs.bus_width; 178 } 179 static inline void phy_set_bus_width(struct phy *phy, int bus_width) 180 { 181 phy->attrs.bus_width = bus_width; 182 } 183 struct phy *phy_get(struct device *dev, const char *string); 184 struct phy *phy_optional_get(struct device *dev, const char *string); 185 struct phy *devm_phy_get(struct device *dev, const char *string); 186 struct phy *devm_phy_optional_get(struct device *dev, const char *string); 187 struct phy *devm_of_phy_get(struct device *dev, struct device_node *np, 188 const char *con_id); 189 struct phy *devm_of_phy_get_by_index(struct device *dev, struct device_node *np, 190 int index); 191 void phy_put(struct phy *phy); 192 void devm_phy_put(struct device *dev, struct phy *phy); 193 struct phy *of_phy_get(struct device_node *np, const char *con_id); 194 struct phy *of_phy_simple_xlate(struct device *dev, 195 struct of_phandle_args *args); 196 struct phy *phy_create(struct device *dev, struct device_node *node, 197 const struct phy_ops *ops); 198 struct phy *devm_phy_create(struct device *dev, struct device_node *node, 199 const struct phy_ops *ops); 200 void phy_destroy(struct phy *phy); 201 void devm_phy_destroy(struct device *dev, struct phy *phy); 202 struct phy_provider *__of_phy_provider_register(struct device *dev, 203 struct device_node *children, struct module *owner, 204 struct phy * (*of_xlate)(struct device *dev, 205 struct of_phandle_args *args)); 206 struct phy_provider *__devm_of_phy_provider_register(struct device *dev, 207 struct device_node *children, struct module *owner, 208 struct phy * (*of_xlate)(struct device *dev, 209 struct of_phandle_args *args)); 210 void of_phy_provider_unregister(struct phy_provider *phy_provider); 211 void devm_of_phy_provider_unregister(struct device *dev, 212 struct phy_provider *phy_provider); 213 int phy_create_lookup(struct phy *phy, const char *con_id, const char *dev_id); 214 void phy_remove_lookup(struct phy *phy, const char *con_id, const char *dev_id); 215 #else 216 static inline int phy_pm_runtime_get(struct phy *phy) 217 { 218 if (!phy) 219 return 0; 220 return -ENOSYS; 221 } 222 223 static inline int phy_pm_runtime_get_sync(struct phy *phy) 224 { 225 if (!phy) 226 return 0; 227 return -ENOSYS; 228 } 229 230 static inline int phy_pm_runtime_put(struct phy *phy) 231 { 232 if (!phy) 233 return 0; 234 return -ENOSYS; 235 } 236 237 static inline int phy_pm_runtime_put_sync(struct phy *phy) 238 { 239 if (!phy) 240 return 0; 241 return -ENOSYS; 242 } 243 244 static inline void phy_pm_runtime_allow(struct phy *phy) 245 { 246 return; 247 } 248 249 static inline void phy_pm_runtime_forbid(struct phy *phy) 250 { 251 return; 252 } 253 254 static inline int phy_init(struct phy *phy) 255 { 256 if (!phy) 257 return 0; 258 return -ENOSYS; 259 } 260 261 static inline int phy_exit(struct phy *phy) 262 { 263 if (!phy) 264 return 0; 265 return -ENOSYS; 266 } 267 268 static inline int phy_power_on(struct phy *phy) 269 { 270 if (!phy) 271 return 0; 272 return -ENOSYS; 273 } 274 275 static inline int phy_power_off(struct phy *phy) 276 { 277 if (!phy) 278 return 0; 279 return -ENOSYS; 280 } 281 282 static inline int phy_set_mode_ext(struct phy *phy, enum phy_mode mode, 283 int submode) 284 { 285 if (!phy) 286 return 0; 287 return -ENOSYS; 288 } 289 290 #define phy_set_mode(phy, mode) \ 291 phy_set_mode_ext(phy, mode, 0) 292 293 static inline enum phy_mode phy_get_mode(struct phy *phy) 294 { 295 return PHY_MODE_INVALID; 296 } 297 298 static inline int phy_reset(struct phy *phy) 299 { 300 if (!phy) 301 return 0; 302 return -ENOSYS; 303 } 304 305 static inline int phy_calibrate(struct phy *phy) 306 { 307 if (!phy) 308 return 0; 309 return -ENOSYS; 310 } 311 312 static inline int phy_get_bus_width(struct phy *phy) 313 { 314 return -ENOSYS; 315 } 316 317 static inline void phy_set_bus_width(struct phy *phy, int bus_width) 318 { 319 return; 320 } 321 322 static inline struct phy *phy_get(struct device *dev, const char *string) 323 { 324 return ERR_PTR(-ENOSYS); 325 } 326 327 static inline struct phy *phy_optional_get(struct device *dev, 328 const char *string) 329 { 330 return ERR_PTR(-ENOSYS); 331 } 332 333 static inline struct phy *devm_phy_get(struct device *dev, const char *string) 334 { 335 return ERR_PTR(-ENOSYS); 336 } 337 338 static inline struct phy *devm_phy_optional_get(struct device *dev, 339 const char *string) 340 { 341 return NULL; 342 } 343 344 static inline struct phy *devm_of_phy_get(struct device *dev, 345 struct device_node *np, 346 const char *con_id) 347 { 348 return ERR_PTR(-ENOSYS); 349 } 350 351 static inline struct phy *devm_of_phy_get_by_index(struct device *dev, 352 struct device_node *np, 353 int index) 354 { 355 return ERR_PTR(-ENOSYS); 356 } 357 358 static inline void phy_put(struct phy *phy) 359 { 360 } 361 362 static inline void devm_phy_put(struct device *dev, struct phy *phy) 363 { 364 } 365 366 static inline struct phy *of_phy_get(struct device_node *np, const char *con_id) 367 { 368 return ERR_PTR(-ENOSYS); 369 } 370 371 static inline struct phy *of_phy_simple_xlate(struct device *dev, 372 struct of_phandle_args *args) 373 { 374 return ERR_PTR(-ENOSYS); 375 } 376 377 static inline struct phy *phy_create(struct device *dev, 378 struct device_node *node, 379 const struct phy_ops *ops) 380 { 381 return ERR_PTR(-ENOSYS); 382 } 383 384 static inline struct phy *devm_phy_create(struct device *dev, 385 struct device_node *node, 386 const struct phy_ops *ops) 387 { 388 return ERR_PTR(-ENOSYS); 389 } 390 391 static inline void phy_destroy(struct phy *phy) 392 { 393 } 394 395 static inline void devm_phy_destroy(struct device *dev, struct phy *phy) 396 { 397 } 398 399 static inline struct phy_provider *__of_phy_provider_register( 400 struct device *dev, struct device_node *children, struct module *owner, 401 struct phy * (*of_xlate)(struct device *dev, 402 struct of_phandle_args *args)) 403 { 404 return ERR_PTR(-ENOSYS); 405 } 406 407 static inline struct phy_provider *__devm_of_phy_provider_register(struct device 408 *dev, struct device_node *children, struct module *owner, 409 struct phy * (*of_xlate)(struct device *dev, 410 struct of_phandle_args *args)) 411 { 412 return ERR_PTR(-ENOSYS); 413 } 414 415 static inline void of_phy_provider_unregister(struct phy_provider *phy_provider) 416 { 417 } 418 419 static inline void devm_of_phy_provider_unregister(struct device *dev, 420 struct phy_provider *phy_provider) 421 { 422 } 423 static inline int 424 phy_create_lookup(struct phy *phy, const char *con_id, const char *dev_id) 425 { 426 return 0; 427 } 428 static inline void phy_remove_lookup(struct phy *phy, const char *con_id, 429 const char *dev_id) { } 430 #endif 431 432 #endif /* __DRIVERS_PHY_H */ 433