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