phy.h 10 KB

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