phy.h 13 KB

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