phy.h 9.5 KB

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