phy.h 9.8 KB

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