at v6.16-rc4 556 lines 14 kB view raw
1/* SPDX-License-Identifier: GPL-2.0-or-later */ 2/* 3 * phy.h -- generic phy header file 4 * 5 * Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com 6 * 7 * Author: Kishon Vijay Abraham I <kishon@ti.com> 8 */ 9 10#ifndef __DRIVERS_PHY_H 11#define __DRIVERS_PHY_H 12 13#include <linux/err.h> 14#include <linux/of.h> 15#include <linux/device.h> 16#include <linux/pm_runtime.h> 17#include <linux/regulator/consumer.h> 18 19#include <linux/phy/phy-dp.h> 20#include <linux/phy/phy-hdmi.h> 21#include <linux/phy/phy-lvds.h> 22#include <linux/phy/phy-mipi-dphy.h> 23 24struct phy; 25 26enum phy_mode { 27 PHY_MODE_INVALID, 28 PHY_MODE_USB_HOST, 29 PHY_MODE_USB_HOST_LS, 30 PHY_MODE_USB_HOST_FS, 31 PHY_MODE_USB_HOST_HS, 32 PHY_MODE_USB_HOST_SS, 33 PHY_MODE_USB_DEVICE, 34 PHY_MODE_USB_DEVICE_LS, 35 PHY_MODE_USB_DEVICE_FS, 36 PHY_MODE_USB_DEVICE_HS, 37 PHY_MODE_USB_DEVICE_SS, 38 PHY_MODE_USB_OTG, 39 PHY_MODE_UFS_HS_A, 40 PHY_MODE_UFS_HS_B, 41 PHY_MODE_PCIE, 42 PHY_MODE_ETHERNET, 43 PHY_MODE_MIPI_DPHY, 44 PHY_MODE_SATA, 45 PHY_MODE_LVDS, 46 PHY_MODE_DP, 47 PHY_MODE_HDMI, 48}; 49 50enum phy_media { 51 PHY_MEDIA_DEFAULT, 52 PHY_MEDIA_SR, 53 PHY_MEDIA_DAC, 54}; 55 56/** 57 * union phy_configure_opts - Opaque generic phy configuration 58 * 59 * @mipi_dphy: Configuration set applicable for phys supporting 60 * the MIPI_DPHY phy mode. 61 * @dp: Configuration set applicable for phys supporting 62 * the DisplayPort protocol. 63 * @lvds: Configuration set applicable for phys supporting 64 * the LVDS phy mode. 65 * @hdmi: Configuration set applicable for phys supporting 66 * the HDMI phy mode. 67 */ 68union phy_configure_opts { 69 struct phy_configure_opts_mipi_dphy mipi_dphy; 70 struct phy_configure_opts_dp dp; 71 struct phy_configure_opts_lvds lvds; 72 struct phy_configure_opts_hdmi hdmi; 73}; 74 75/** 76 * struct phy_ops - set of function pointers for performing phy operations 77 * @init: operation to be performed for initializing phy 78 * @exit: operation to be performed while exiting 79 * @power_on: powering on the phy 80 * @power_off: powering off the phy 81 * @set_mode: set the mode of the phy 82 * @set_media: set the media type of the phy (optional) 83 * @set_speed: set the speed of the phy (optional) 84 * @reset: resetting the phy 85 * @calibrate: calibrate the phy 86 * @release: ops to be performed while the consumer relinquishes the PHY 87 * @owner: the module owner containing the ops 88 */ 89struct phy_ops { 90 int (*init)(struct phy *phy); 91 int (*exit)(struct phy *phy); 92 int (*power_on)(struct phy *phy); 93 int (*power_off)(struct phy *phy); 94 int (*set_mode)(struct phy *phy, enum phy_mode mode, int submode); 95 int (*set_media)(struct phy *phy, enum phy_media media); 96 int (*set_speed)(struct phy *phy, int speed); 97 98 /** 99 * @configure: 100 * 101 * Optional. 102 * 103 * Used to change the PHY parameters. phy_init() must have 104 * been called on the phy. 105 * 106 * Returns: 0 if successful, an negative error code otherwise 107 */ 108 int (*configure)(struct phy *phy, union phy_configure_opts *opts); 109 110 /** 111 * @validate: 112 * 113 * Optional. 114 * 115 * Used to check that the current set of parameters can be 116 * handled by the phy. Implementations are free to tune the 117 * parameters passed as arguments if needed by some 118 * implementation detail or constraints. It must not change 119 * any actual configuration of the PHY, so calling it as many 120 * times as deemed fit by the consumer must have no side 121 * effect. 122 * 123 * Returns: 0 if the configuration can be applied, an negative 124 * error code otherwise 125 */ 126 int (*validate)(struct phy *phy, enum phy_mode mode, int submode, 127 union phy_configure_opts *opts); 128 int (*reset)(struct phy *phy); 129 int (*calibrate)(struct phy *phy); 130 131 /* notify phy connect status change */ 132 int (*connect)(struct phy *phy, int port); 133 int (*disconnect)(struct phy *phy, int port); 134 135 void (*release)(struct phy *phy); 136 struct module *owner; 137}; 138 139/** 140 * struct phy_attrs - represents phy attributes 141 * @bus_width: Data path width implemented by PHY 142 * @max_link_rate: Maximum link rate supported by PHY (units to be decided by producer and consumer) 143 * @mode: PHY mode 144 */ 145struct phy_attrs { 146 u32 bus_width; 147 u32 max_link_rate; 148 enum phy_mode mode; 149}; 150 151/** 152 * struct phy - represents the phy device 153 * @dev: phy device 154 * @id: id of the phy device 155 * @ops: function pointers for performing phy operations 156 * @mutex: mutex to protect phy_ops 157 * @init_count: used to protect when the PHY is used by multiple consumers 158 * @power_count: used to protect when the PHY is used by multiple consumers 159 * @attrs: used to specify PHY specific attributes 160 * @pwr: power regulator associated with the phy 161 * @debugfs: debugfs directory 162 */ 163struct phy { 164 struct device dev; 165 int id; 166 const struct phy_ops *ops; 167 struct mutex mutex; 168 int init_count; 169 int power_count; 170 struct phy_attrs attrs; 171 struct regulator *pwr; 172 struct dentry *debugfs; 173}; 174 175/** 176 * struct phy_provider - represents the phy provider 177 * @dev: phy provider device 178 * @children: can be used to override the default (dev->of_node) child node 179 * @owner: the module owner having of_xlate 180 * @list: to maintain a linked list of PHY providers 181 * @of_xlate: function pointer to obtain phy instance from phy pointer 182 */ 183struct phy_provider { 184 struct device *dev; 185 struct device_node *children; 186 struct module *owner; 187 struct list_head list; 188 struct phy * (*of_xlate)(struct device *dev, 189 const struct of_phandle_args *args); 190}; 191 192/** 193 * struct phy_lookup - PHY association in list of phys managed by the phy driver 194 * @node: list node 195 * @dev_id: the device of the association 196 * @con_id: connection ID string on device 197 * @phy: the phy of the association 198 */ 199struct phy_lookup { 200 struct list_head node; 201 const char *dev_id; 202 const char *con_id; 203 struct phy *phy; 204}; 205 206#define to_phy(a) (container_of((a), struct phy, dev)) 207 208#define of_phy_provider_register(dev, xlate) \ 209 __of_phy_provider_register((dev), NULL, THIS_MODULE, (xlate)) 210 211#define devm_of_phy_provider_register(dev, xlate) \ 212 __devm_of_phy_provider_register((dev), NULL, THIS_MODULE, (xlate)) 213 214#define of_phy_provider_register_full(dev, children, xlate) \ 215 __of_phy_provider_register(dev, children, THIS_MODULE, xlate) 216 217#define devm_of_phy_provider_register_full(dev, children, xlate) \ 218 __devm_of_phy_provider_register(dev, children, THIS_MODULE, xlate) 219 220static inline void phy_set_drvdata(struct phy *phy, void *data) 221{ 222 dev_set_drvdata(&phy->dev, data); 223} 224 225static inline void *phy_get_drvdata(struct phy *phy) 226{ 227 return dev_get_drvdata(&phy->dev); 228} 229 230#if IS_ENABLED(CONFIG_GENERIC_PHY) 231int phy_pm_runtime_get(struct phy *phy); 232int phy_pm_runtime_get_sync(struct phy *phy); 233int phy_pm_runtime_put(struct phy *phy); 234int phy_pm_runtime_put_sync(struct phy *phy); 235int phy_init(struct phy *phy); 236int phy_exit(struct phy *phy); 237int phy_power_on(struct phy *phy); 238int phy_power_off(struct phy *phy); 239int phy_set_mode_ext(struct phy *phy, enum phy_mode mode, int submode); 240#define phy_set_mode(phy, mode) \ 241 phy_set_mode_ext(phy, mode, 0) 242int phy_set_media(struct phy *phy, enum phy_media media); 243int phy_set_speed(struct phy *phy, int speed); 244int phy_configure(struct phy *phy, union phy_configure_opts *opts); 245int phy_validate(struct phy *phy, enum phy_mode mode, int submode, 246 union phy_configure_opts *opts); 247 248static inline enum phy_mode phy_get_mode(struct phy *phy) 249{ 250 return phy->attrs.mode; 251} 252int phy_reset(struct phy *phy); 253int phy_calibrate(struct phy *phy); 254int phy_notify_connect(struct phy *phy, int port); 255int phy_notify_disconnect(struct phy *phy, int port); 256static inline int phy_get_bus_width(struct phy *phy) 257{ 258 return phy->attrs.bus_width; 259} 260static inline void phy_set_bus_width(struct phy *phy, int bus_width) 261{ 262 phy->attrs.bus_width = bus_width; 263} 264struct phy *phy_get(struct device *dev, const char *string); 265struct phy *devm_phy_get(struct device *dev, const char *string); 266struct phy *devm_phy_optional_get(struct device *dev, const char *string); 267struct phy *devm_of_phy_get(struct device *dev, struct device_node *np, 268 const char *con_id); 269struct phy *devm_of_phy_optional_get(struct device *dev, struct device_node *np, 270 const char *con_id); 271struct phy *devm_of_phy_get_by_index(struct device *dev, struct device_node *np, 272 int index); 273void of_phy_put(struct phy *phy); 274void phy_put(struct device *dev, struct phy *phy); 275void devm_phy_put(struct device *dev, struct phy *phy); 276struct phy *of_phy_get(struct device_node *np, const char *con_id); 277struct phy *of_phy_simple_xlate(struct device *dev, 278 const struct of_phandle_args *args); 279struct phy *phy_create(struct device *dev, struct device_node *node, 280 const struct phy_ops *ops); 281struct phy *devm_phy_create(struct device *dev, struct device_node *node, 282 const struct phy_ops *ops); 283void phy_destroy(struct phy *phy); 284void devm_phy_destroy(struct device *dev, struct phy *phy); 285struct phy_provider *__of_phy_provider_register(struct device *dev, 286 struct device_node *children, struct module *owner, 287 struct phy * (*of_xlate)(struct device *dev, 288 const struct of_phandle_args *args)); 289struct phy_provider *__devm_of_phy_provider_register(struct device *dev, 290 struct device_node *children, struct module *owner, 291 struct phy * (*of_xlate)(struct device *dev, 292 const struct of_phandle_args *args)); 293void of_phy_provider_unregister(struct phy_provider *phy_provider); 294void devm_of_phy_provider_unregister(struct device *dev, 295 struct phy_provider *phy_provider); 296int phy_create_lookup(struct phy *phy, const char *con_id, const char *dev_id); 297void phy_remove_lookup(struct phy *phy, const char *con_id, const char *dev_id); 298#else 299static inline int phy_pm_runtime_get(struct phy *phy) 300{ 301 if (!phy) 302 return 0; 303 return -ENOSYS; 304} 305 306static inline int phy_pm_runtime_get_sync(struct phy *phy) 307{ 308 if (!phy) 309 return 0; 310 return -ENOSYS; 311} 312 313static inline int phy_pm_runtime_put(struct phy *phy) 314{ 315 if (!phy) 316 return 0; 317 return -ENOSYS; 318} 319 320static inline int phy_pm_runtime_put_sync(struct phy *phy) 321{ 322 if (!phy) 323 return 0; 324 return -ENOSYS; 325} 326 327static inline int phy_init(struct phy *phy) 328{ 329 if (!phy) 330 return 0; 331 return -ENOSYS; 332} 333 334static inline int phy_exit(struct phy *phy) 335{ 336 if (!phy) 337 return 0; 338 return -ENOSYS; 339} 340 341static inline int phy_power_on(struct phy *phy) 342{ 343 if (!phy) 344 return 0; 345 return -ENOSYS; 346} 347 348static inline int phy_power_off(struct phy *phy) 349{ 350 if (!phy) 351 return 0; 352 return -ENOSYS; 353} 354 355static inline int phy_set_mode_ext(struct phy *phy, enum phy_mode mode, 356 int submode) 357{ 358 if (!phy) 359 return 0; 360 return -ENOSYS; 361} 362 363#define phy_set_mode(phy, mode) \ 364 phy_set_mode_ext(phy, mode, 0) 365 366static inline int phy_set_media(struct phy *phy, enum phy_media media) 367{ 368 if (!phy) 369 return 0; 370 return -ENODEV; 371} 372 373static inline int phy_set_speed(struct phy *phy, int speed) 374{ 375 if (!phy) 376 return 0; 377 return -ENODEV; 378} 379 380static inline enum phy_mode phy_get_mode(struct phy *phy) 381{ 382 return PHY_MODE_INVALID; 383} 384 385static inline int phy_reset(struct phy *phy) 386{ 387 if (!phy) 388 return 0; 389 return -ENOSYS; 390} 391 392static inline int phy_calibrate(struct phy *phy) 393{ 394 if (!phy) 395 return 0; 396 return -ENOSYS; 397} 398 399static inline int phy_notify_connect(struct phy *phy, int index) 400{ 401 if (!phy) 402 return 0; 403 return -ENOSYS; 404} 405 406static inline int phy_notify_disconnect(struct phy *phy, int index) 407{ 408 if (!phy) 409 return 0; 410 return -ENOSYS; 411} 412 413static inline int phy_configure(struct phy *phy, 414 union phy_configure_opts *opts) 415{ 416 if (!phy) 417 return 0; 418 419 return -ENOSYS; 420} 421 422static inline int phy_validate(struct phy *phy, enum phy_mode mode, int submode, 423 union phy_configure_opts *opts) 424{ 425 if (!phy) 426 return 0; 427 428 return -ENOSYS; 429} 430 431static inline int phy_get_bus_width(struct phy *phy) 432{ 433 return -ENOSYS; 434} 435 436static inline void phy_set_bus_width(struct phy *phy, int bus_width) 437{ 438 return; 439} 440 441static inline struct phy *phy_get(struct device *dev, const char *string) 442{ 443 return ERR_PTR(-ENOSYS); 444} 445 446static inline struct phy *devm_phy_get(struct device *dev, const char *string) 447{ 448 return ERR_PTR(-ENOSYS); 449} 450 451static inline struct phy *devm_phy_optional_get(struct device *dev, 452 const char *string) 453{ 454 return NULL; 455} 456 457static inline struct phy *devm_of_phy_get(struct device *dev, 458 struct device_node *np, 459 const char *con_id) 460{ 461 return ERR_PTR(-ENOSYS); 462} 463 464static inline struct phy *devm_of_phy_optional_get(struct device *dev, 465 struct device_node *np, 466 const char *con_id) 467{ 468 return NULL; 469} 470 471static inline struct phy *devm_of_phy_get_by_index(struct device *dev, 472 struct device_node *np, 473 int index) 474{ 475 return ERR_PTR(-ENOSYS); 476} 477 478static inline void of_phy_put(struct phy *phy) 479{ 480} 481 482static inline void phy_put(struct device *dev, struct phy *phy) 483{ 484} 485 486static inline void devm_phy_put(struct device *dev, struct phy *phy) 487{ 488} 489 490static inline struct phy *of_phy_get(struct device_node *np, const char *con_id) 491{ 492 return ERR_PTR(-ENOSYS); 493} 494 495static inline struct phy *of_phy_simple_xlate(struct device *dev, 496 const struct of_phandle_args *args) 497{ 498 return ERR_PTR(-ENOSYS); 499} 500 501static inline struct phy *phy_create(struct device *dev, 502 struct device_node *node, 503 const struct phy_ops *ops) 504{ 505 return ERR_PTR(-ENOSYS); 506} 507 508static inline struct phy *devm_phy_create(struct device *dev, 509 struct device_node *node, 510 const struct phy_ops *ops) 511{ 512 return ERR_PTR(-ENOSYS); 513} 514 515static inline void phy_destroy(struct phy *phy) 516{ 517} 518 519static inline void devm_phy_destroy(struct device *dev, struct phy *phy) 520{ 521} 522 523static inline struct phy_provider *__of_phy_provider_register( 524 struct device *dev, struct device_node *children, struct module *owner, 525 struct phy * (*of_xlate)(struct device *dev, 526 const struct of_phandle_args *args)) 527{ 528 return ERR_PTR(-ENOSYS); 529} 530 531static inline struct phy_provider *__devm_of_phy_provider_register(struct device 532 *dev, struct device_node *children, struct module *owner, 533 struct phy * (*of_xlate)(struct device *dev, 534 const struct of_phandle_args *args)) 535{ 536 return ERR_PTR(-ENOSYS); 537} 538 539static inline void of_phy_provider_unregister(struct phy_provider *phy_provider) 540{ 541} 542 543static inline void devm_of_phy_provider_unregister(struct device *dev, 544 struct phy_provider *phy_provider) 545{ 546} 547static inline int 548phy_create_lookup(struct phy *phy, const char *con_id, const char *dev_id) 549{ 550 return 0; 551} 552static inline void phy_remove_lookup(struct phy *phy, const char *con_id, 553 const char *dev_id) { } 554#endif 555 556#endif /* __DRIVERS_PHY_H */