phy.h 9.0 KB

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