phy.h 9.3 KB

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