Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-or-later */
0002 /*
0003  * phy.h -- generic phy header file
0004  *
0005  * Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com
0006  *
0007  * Author: Kishon Vijay Abraham I <kishon@ti.com>
0008  */
0009 
0010 #ifndef __DRIVERS_PHY_H
0011 #define __DRIVERS_PHY_H
0012 
0013 #include <linux/err.h>
0014 #include <linux/of.h>
0015 #include <linux/device.h>
0016 #include <linux/pm_runtime.h>
0017 #include <linux/regulator/consumer.h>
0018 
0019 #include <linux/phy/phy-dp.h>
0020 #include <linux/phy/phy-lvds.h>
0021 #include <linux/phy/phy-mipi-dphy.h>
0022 
0023 struct phy;
0024 
0025 enum phy_mode {
0026     PHY_MODE_INVALID,
0027     PHY_MODE_USB_HOST,
0028     PHY_MODE_USB_HOST_LS,
0029     PHY_MODE_USB_HOST_FS,
0030     PHY_MODE_USB_HOST_HS,
0031     PHY_MODE_USB_HOST_SS,
0032     PHY_MODE_USB_DEVICE,
0033     PHY_MODE_USB_DEVICE_LS,
0034     PHY_MODE_USB_DEVICE_FS,
0035     PHY_MODE_USB_DEVICE_HS,
0036     PHY_MODE_USB_DEVICE_SS,
0037     PHY_MODE_USB_OTG,
0038     PHY_MODE_UFS_HS_A,
0039     PHY_MODE_UFS_HS_B,
0040     PHY_MODE_PCIE,
0041     PHY_MODE_ETHERNET,
0042     PHY_MODE_MIPI_DPHY,
0043     PHY_MODE_SATA,
0044     PHY_MODE_LVDS,
0045     PHY_MODE_DP
0046 };
0047 
0048 enum phy_media {
0049     PHY_MEDIA_DEFAULT,
0050     PHY_MEDIA_SR,
0051     PHY_MEDIA_DAC,
0052 };
0053 
0054 /**
0055  * union phy_configure_opts - Opaque generic phy configuration
0056  *
0057  * @mipi_dphy:  Configuration set applicable for phys supporting
0058  *      the MIPI_DPHY phy mode.
0059  * @dp:     Configuration set applicable for phys supporting
0060  *      the DisplayPort protocol.
0061  * @lvds:   Configuration set applicable for phys supporting
0062  *      the LVDS phy mode.
0063  */
0064 union phy_configure_opts {
0065     struct phy_configure_opts_mipi_dphy mipi_dphy;
0066     struct phy_configure_opts_dp        dp;
0067     struct phy_configure_opts_lvds      lvds;
0068 };
0069 
0070 /**
0071  * struct phy_ops - set of function pointers for performing phy operations
0072  * @init: operation to be performed for initializing phy
0073  * @exit: operation to be performed while exiting
0074  * @power_on: powering on the phy
0075  * @power_off: powering off the phy
0076  * @set_mode: set the mode of the phy
0077  * @set_media: set the media type of the phy (optional)
0078  * @set_speed: set the speed of the phy (optional)
0079  * @reset: resetting the phy
0080  * @calibrate: calibrate the phy
0081  * @release: ops to be performed while the consumer relinquishes the PHY
0082  * @owner: the module owner containing the ops
0083  */
0084 struct phy_ops {
0085     int (*init)(struct phy *phy);
0086     int (*exit)(struct phy *phy);
0087     int (*power_on)(struct phy *phy);
0088     int (*power_off)(struct phy *phy);
0089     int (*set_mode)(struct phy *phy, enum phy_mode mode, int submode);
0090     int (*set_media)(struct phy *phy, enum phy_media media);
0091     int (*set_speed)(struct phy *phy, int speed);
0092 
0093     /**
0094      * @configure:
0095      *
0096      * Optional.
0097      *
0098      * Used to change the PHY parameters. phy_init() must have
0099      * been called on the phy.
0100      *
0101      * Returns: 0 if successful, an negative error code otherwise
0102      */
0103     int (*configure)(struct phy *phy, union phy_configure_opts *opts);
0104 
0105     /**
0106      * @validate:
0107      *
0108      * Optional.
0109      *
0110      * Used to check that the current set of parameters can be
0111      * handled by the phy. Implementations are free to tune the
0112      * parameters passed as arguments if needed by some
0113      * implementation detail or constraints. It must not change
0114      * any actual configuration of the PHY, so calling it as many
0115      * times as deemed fit by the consumer must have no side
0116      * effect.
0117      *
0118      * Returns: 0 if the configuration can be applied, an negative
0119      * error code otherwise
0120      */
0121     int (*validate)(struct phy *phy, enum phy_mode mode, int submode,
0122                 union phy_configure_opts *opts);
0123     int (*reset)(struct phy *phy);
0124     int (*calibrate)(struct phy *phy);
0125     void    (*release)(struct phy *phy);
0126     struct module *owner;
0127 };
0128 
0129 /**
0130  * struct phy_attrs - represents phy attributes
0131  * @bus_width: Data path width implemented by PHY
0132  * @max_link_rate: Maximum link rate supported by PHY (units to be decided by producer and consumer)
0133  * @mode: PHY mode
0134  */
0135 struct phy_attrs {
0136     u32         bus_width;
0137     u32         max_link_rate;
0138     enum phy_mode       mode;
0139 };
0140 
0141 /**
0142  * struct phy - represents the phy device
0143  * @dev: phy device
0144  * @id: id of the phy device
0145  * @ops: function pointers for performing phy operations
0146  * @mutex: mutex to protect phy_ops
0147  * @init_count: used to protect when the PHY is used by multiple consumers
0148  * @power_count: used to protect when the PHY is used by multiple consumers
0149  * @attrs: used to specify PHY specific attributes
0150  * @pwr: power regulator associated with the phy
0151  */
0152 struct phy {
0153     struct device       dev;
0154     int         id;
0155     const struct phy_ops    *ops;
0156     struct mutex        mutex;
0157     int         init_count;
0158     int         power_count;
0159     struct phy_attrs    attrs;
0160     struct regulator    *pwr;
0161 };
0162 
0163 /**
0164  * struct phy_provider - represents the phy provider
0165  * @dev: phy provider device
0166  * @children: can be used to override the default (dev->of_node) child node
0167  * @owner: the module owner having of_xlate
0168  * @list: to maintain a linked list of PHY providers
0169  * @of_xlate: function pointer to obtain phy instance from phy pointer
0170  */
0171 struct phy_provider {
0172     struct device       *dev;
0173     struct device_node  *children;
0174     struct module       *owner;
0175     struct list_head    list;
0176     struct phy * (*of_xlate)(struct device *dev,
0177         struct of_phandle_args *args);
0178 };
0179 
0180 /**
0181  * struct phy_lookup - PHY association in list of phys managed by the phy driver
0182  * @node: list node
0183  * @dev_id: the device of the association
0184  * @con_id: connection ID string on device
0185  * @phy: the phy of the association
0186  */
0187 struct phy_lookup {
0188     struct list_head node;
0189     const char *dev_id;
0190     const char *con_id;
0191     struct phy *phy;
0192 };
0193 
0194 #define to_phy(a)   (container_of((a), struct phy, dev))
0195 
0196 #define of_phy_provider_register(dev, xlate)    \
0197     __of_phy_provider_register((dev), NULL, THIS_MODULE, (xlate))
0198 
0199 #define devm_of_phy_provider_register(dev, xlate)   \
0200     __devm_of_phy_provider_register((dev), NULL, THIS_MODULE, (xlate))
0201 
0202 #define of_phy_provider_register_full(dev, children, xlate) \
0203     __of_phy_provider_register(dev, children, THIS_MODULE, xlate)
0204 
0205 #define devm_of_phy_provider_register_full(dev, children, xlate) \
0206     __devm_of_phy_provider_register(dev, children, THIS_MODULE, xlate)
0207 
0208 static inline void phy_set_drvdata(struct phy *phy, void *data)
0209 {
0210     dev_set_drvdata(&phy->dev, data);
0211 }
0212 
0213 static inline void *phy_get_drvdata(struct phy *phy)
0214 {
0215     return dev_get_drvdata(&phy->dev);
0216 }
0217 
0218 #if IS_ENABLED(CONFIG_GENERIC_PHY)
0219 int phy_pm_runtime_get(struct phy *phy);
0220 int phy_pm_runtime_get_sync(struct phy *phy);
0221 int phy_pm_runtime_put(struct phy *phy);
0222 int phy_pm_runtime_put_sync(struct phy *phy);
0223 void phy_pm_runtime_allow(struct phy *phy);
0224 void phy_pm_runtime_forbid(struct phy *phy);
0225 int phy_init(struct phy *phy);
0226 int phy_exit(struct phy *phy);
0227 int phy_power_on(struct phy *phy);
0228 int phy_power_off(struct phy *phy);
0229 int phy_set_mode_ext(struct phy *phy, enum phy_mode mode, int submode);
0230 #define phy_set_mode(phy, mode) \
0231     phy_set_mode_ext(phy, mode, 0)
0232 int phy_set_media(struct phy *phy, enum phy_media media);
0233 int phy_set_speed(struct phy *phy, int speed);
0234 int phy_configure(struct phy *phy, union phy_configure_opts *opts);
0235 int phy_validate(struct phy *phy, enum phy_mode mode, int submode,
0236          union phy_configure_opts *opts);
0237 
0238 static inline enum phy_mode phy_get_mode(struct phy *phy)
0239 {
0240     return phy->attrs.mode;
0241 }
0242 int phy_reset(struct phy *phy);
0243 int phy_calibrate(struct phy *phy);
0244 static inline int phy_get_bus_width(struct phy *phy)
0245 {
0246     return phy->attrs.bus_width;
0247 }
0248 static inline void phy_set_bus_width(struct phy *phy, int bus_width)
0249 {
0250     phy->attrs.bus_width = bus_width;
0251 }
0252 struct phy *phy_get(struct device *dev, const char *string);
0253 struct phy *phy_optional_get(struct device *dev, const char *string);
0254 struct phy *devm_phy_get(struct device *dev, const char *string);
0255 struct phy *devm_phy_optional_get(struct device *dev, const char *string);
0256 struct phy *devm_of_phy_get(struct device *dev, struct device_node *np,
0257                 const char *con_id);
0258 struct phy *devm_of_phy_get_by_index(struct device *dev, struct device_node *np,
0259                      int index);
0260 void of_phy_put(struct phy *phy);
0261 void phy_put(struct device *dev, struct phy *phy);
0262 void devm_phy_put(struct device *dev, struct phy *phy);
0263 struct phy *of_phy_get(struct device_node *np, const char *con_id);
0264 struct phy *of_phy_simple_xlate(struct device *dev,
0265     struct of_phandle_args *args);
0266 struct phy *phy_create(struct device *dev, struct device_node *node,
0267                const struct phy_ops *ops);
0268 struct phy *devm_phy_create(struct device *dev, struct device_node *node,
0269                 const struct phy_ops *ops);
0270 void phy_destroy(struct phy *phy);
0271 void devm_phy_destroy(struct device *dev, struct phy *phy);
0272 struct phy_provider *__of_phy_provider_register(struct device *dev,
0273     struct device_node *children, struct module *owner,
0274     struct phy * (*of_xlate)(struct device *dev,
0275                  struct of_phandle_args *args));
0276 struct phy_provider *__devm_of_phy_provider_register(struct device *dev,
0277     struct device_node *children, struct module *owner,
0278     struct phy * (*of_xlate)(struct device *dev,
0279                  struct of_phandle_args *args));
0280 void of_phy_provider_unregister(struct phy_provider *phy_provider);
0281 void devm_of_phy_provider_unregister(struct device *dev,
0282     struct phy_provider *phy_provider);
0283 int phy_create_lookup(struct phy *phy, const char *con_id, const char *dev_id);
0284 void phy_remove_lookup(struct phy *phy, const char *con_id, const char *dev_id);
0285 #else
0286 static inline int phy_pm_runtime_get(struct phy *phy)
0287 {
0288     if (!phy)
0289         return 0;
0290     return -ENOSYS;
0291 }
0292 
0293 static inline int phy_pm_runtime_get_sync(struct phy *phy)
0294 {
0295     if (!phy)
0296         return 0;
0297     return -ENOSYS;
0298 }
0299 
0300 static inline int phy_pm_runtime_put(struct phy *phy)
0301 {
0302     if (!phy)
0303         return 0;
0304     return -ENOSYS;
0305 }
0306 
0307 static inline int phy_pm_runtime_put_sync(struct phy *phy)
0308 {
0309     if (!phy)
0310         return 0;
0311     return -ENOSYS;
0312 }
0313 
0314 static inline void phy_pm_runtime_allow(struct phy *phy)
0315 {
0316     return;
0317 }
0318 
0319 static inline void phy_pm_runtime_forbid(struct phy *phy)
0320 {
0321     return;
0322 }
0323 
0324 static inline int phy_init(struct phy *phy)
0325 {
0326     if (!phy)
0327         return 0;
0328     return -ENOSYS;
0329 }
0330 
0331 static inline int phy_exit(struct phy *phy)
0332 {
0333     if (!phy)
0334         return 0;
0335     return -ENOSYS;
0336 }
0337 
0338 static inline int phy_power_on(struct phy *phy)
0339 {
0340     if (!phy)
0341         return 0;
0342     return -ENOSYS;
0343 }
0344 
0345 static inline int phy_power_off(struct phy *phy)
0346 {
0347     if (!phy)
0348         return 0;
0349     return -ENOSYS;
0350 }
0351 
0352 static inline int phy_set_mode_ext(struct phy *phy, enum phy_mode mode,
0353                    int submode)
0354 {
0355     if (!phy)
0356         return 0;
0357     return -ENOSYS;
0358 }
0359 
0360 #define phy_set_mode(phy, mode) \
0361     phy_set_mode_ext(phy, mode, 0)
0362 
0363 static inline int phy_set_media(struct phy *phy, enum phy_media media)
0364 {
0365     if (!phy)
0366         return 0;
0367     return -ENODEV;
0368 }
0369 
0370 static inline int phy_set_speed(struct phy *phy, int speed)
0371 {
0372     if (!phy)
0373         return 0;
0374     return -ENODEV;
0375 }
0376 
0377 static inline enum phy_mode phy_get_mode(struct phy *phy)
0378 {
0379     return PHY_MODE_INVALID;
0380 }
0381 
0382 static inline int phy_reset(struct phy *phy)
0383 {
0384     if (!phy)
0385         return 0;
0386     return -ENOSYS;
0387 }
0388 
0389 static inline int phy_calibrate(struct phy *phy)
0390 {
0391     if (!phy)
0392         return 0;
0393     return -ENOSYS;
0394 }
0395 
0396 static inline int phy_configure(struct phy *phy,
0397                 union phy_configure_opts *opts)
0398 {
0399     if (!phy)
0400         return 0;
0401 
0402     return -ENOSYS;
0403 }
0404 
0405 static inline int phy_validate(struct phy *phy, enum phy_mode mode, int submode,
0406                    union phy_configure_opts *opts)
0407 {
0408     if (!phy)
0409         return 0;
0410 
0411     return -ENOSYS;
0412 }
0413 
0414 static inline int phy_get_bus_width(struct phy *phy)
0415 {
0416     return -ENOSYS;
0417 }
0418 
0419 static inline void phy_set_bus_width(struct phy *phy, int bus_width)
0420 {
0421     return;
0422 }
0423 
0424 static inline struct phy *phy_get(struct device *dev, const char *string)
0425 {
0426     return ERR_PTR(-ENOSYS);
0427 }
0428 
0429 static inline struct phy *phy_optional_get(struct device *dev,
0430                        const char *string)
0431 {
0432     return ERR_PTR(-ENOSYS);
0433 }
0434 
0435 static inline struct phy *devm_phy_get(struct device *dev, const char *string)
0436 {
0437     return ERR_PTR(-ENOSYS);
0438 }
0439 
0440 static inline struct phy *devm_phy_optional_get(struct device *dev,
0441                         const char *string)
0442 {
0443     return NULL;
0444 }
0445 
0446 static inline struct phy *devm_of_phy_get(struct device *dev,
0447                       struct device_node *np,
0448                       const char *con_id)
0449 {
0450     return ERR_PTR(-ENOSYS);
0451 }
0452 
0453 static inline struct phy *devm_of_phy_get_by_index(struct device *dev,
0454                            struct device_node *np,
0455                            int index)
0456 {
0457     return ERR_PTR(-ENOSYS);
0458 }
0459 
0460 static inline void of_phy_put(struct phy *phy)
0461 {
0462 }
0463 
0464 static inline void phy_put(struct device *dev, struct phy *phy)
0465 {
0466 }
0467 
0468 static inline void devm_phy_put(struct device *dev, struct phy *phy)
0469 {
0470 }
0471 
0472 static inline struct phy *of_phy_get(struct device_node *np, const char *con_id)
0473 {
0474     return ERR_PTR(-ENOSYS);
0475 }
0476 
0477 static inline struct phy *of_phy_simple_xlate(struct device *dev,
0478     struct of_phandle_args *args)
0479 {
0480     return ERR_PTR(-ENOSYS);
0481 }
0482 
0483 static inline struct phy *phy_create(struct device *dev,
0484                      struct device_node *node,
0485                      const struct phy_ops *ops)
0486 {
0487     return ERR_PTR(-ENOSYS);
0488 }
0489 
0490 static inline struct phy *devm_phy_create(struct device *dev,
0491                       struct device_node *node,
0492                       const struct phy_ops *ops)
0493 {
0494     return ERR_PTR(-ENOSYS);
0495 }
0496 
0497 static inline void phy_destroy(struct phy *phy)
0498 {
0499 }
0500 
0501 static inline void devm_phy_destroy(struct device *dev, struct phy *phy)
0502 {
0503 }
0504 
0505 static inline struct phy_provider *__of_phy_provider_register(
0506     struct device *dev, struct device_node *children, struct module *owner,
0507     struct phy * (*of_xlate)(struct device *dev,
0508                  struct of_phandle_args *args))
0509 {
0510     return ERR_PTR(-ENOSYS);
0511 }
0512 
0513 static inline struct phy_provider *__devm_of_phy_provider_register(struct device
0514     *dev, struct device_node *children, struct module *owner,
0515     struct phy * (*of_xlate)(struct device *dev,
0516                  struct of_phandle_args *args))
0517 {
0518     return ERR_PTR(-ENOSYS);
0519 }
0520 
0521 static inline void of_phy_provider_unregister(struct phy_provider *phy_provider)
0522 {
0523 }
0524 
0525 static inline void devm_of_phy_provider_unregister(struct device *dev,
0526     struct phy_provider *phy_provider)
0527 {
0528 }
0529 static inline int
0530 phy_create_lookup(struct phy *phy, const char *con_id, const char *dev_id)
0531 {
0532     return 0;
0533 }
0534 static inline void phy_remove_lookup(struct phy *phy, const char *con_id,
0535                      const char *dev_id) { }
0536 #endif
0537 
0538 #endif /* __DRIVERS_PHY_H */