phy.h 9.6 KB

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