generic-phy.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * Copyright (C) 2017 Texas Instruments Incorporated - http://www.ti.com/
  4. * Written by Jean-Jacques Hiblot <jjhiblot@ti.com>
  5. */
  6. #ifndef __GENERIC_PHY_H
  7. #define __GENERIC_PHY_H
  8. #include <dm/ofnode.h>
  9. struct ofnode_phandle_args;
  10. enum phy_mode {
  11. PHY_MODE_INVALID,
  12. PHY_MODE_USB_HOST,
  13. PHY_MODE_USB_HOST_LS,
  14. PHY_MODE_USB_HOST_FS,
  15. PHY_MODE_USB_HOST_HS,
  16. PHY_MODE_USB_HOST_SS,
  17. PHY_MODE_USB_DEVICE,
  18. PHY_MODE_USB_DEVICE_LS,
  19. PHY_MODE_USB_DEVICE_FS,
  20. PHY_MODE_USB_DEVICE_HS,
  21. PHY_MODE_USB_DEVICE_SS,
  22. PHY_MODE_USB_OTG,
  23. PHY_MODE_UFS_HS_A,
  24. PHY_MODE_UFS_HS_B,
  25. PHY_MODE_PCIE,
  26. PHY_MODE_ETHERNET,
  27. PHY_MODE_MIPI_DPHY,
  28. PHY_MODE_SATA,
  29. PHY_MODE_LVDS,
  30. PHY_MODE_DP
  31. };
  32. /**
  33. * struct phy - A handle to (allowing control of) a single phy port.
  34. *
  35. * Clients provide storage for phy handles. The content of the structure is
  36. * managed solely by the PHY API and PHY drivers. A phy struct is
  37. * initialized by "get"ing the phy struct. The phy struct is passed to all
  38. * other phy APIs to identify which PHY port to operate upon.
  39. *
  40. * @dev: The device which implements the PHY port.
  41. * @id: The PHY ID within the provider.
  42. *
  43. */
  44. struct phy {
  45. struct udevice *dev;
  46. unsigned long id;
  47. };
  48. /*
  49. * struct udevice_ops - set of function pointers for phy operations
  50. * @init: operation to be performed for initializing phy (optional)
  51. * @exit: operation to be performed while exiting (optional)
  52. * @reset: reset the phy (optional).
  53. * @power_on: powering on the phy (optional)
  54. * @power_off: powering off the phy (optional)
  55. */
  56. struct phy_ops {
  57. /**
  58. * of_xlate - Translate a client's device-tree (OF) phy specifier.
  59. *
  60. * The PHY core calls this function as the first step in implementing
  61. * a client's generic_phy_get_by_*() call.
  62. *
  63. * If this function pointer is set to NULL, the PHY core will use a
  64. * default implementation, which assumes #phy-cells = <0> or
  65. * #phy-cells = <1>, and in the later case that the DT cell
  66. * contains a simple integer PHY port ID.
  67. *
  68. * @phy: The phy struct to hold the translation result.
  69. * @args: The phy specifier values from device tree.
  70. * @return 0 if OK, or a negative error code.
  71. */
  72. int (*of_xlate)(struct phy *phy, struct ofnode_phandle_args *args);
  73. /**
  74. * init - initialize the hardware.
  75. *
  76. * Hardware intialization should not be done in during probe() but
  77. * should be implemented in this init() function. It could be starting
  78. * PLL, taking a controller out of reset, routing, etc. This function
  79. * is typically called only once per PHY port.
  80. * If power_on() is not implemented, it must power up the phy.
  81. *
  82. * @phy: the PHY port to initialize
  83. * @return 0 if OK, or a negative error code.
  84. */
  85. int (*init)(struct phy *phy);
  86. /**
  87. * exit - de-initialize the PHY device
  88. *
  89. * Hardware de-intialization should be done here. Every step done in
  90. * init() should be undone here.
  91. * This could be used to suspend the phy to reduce power consumption or
  92. * to put the phy in a known condition before booting the OS (though it
  93. * is NOT called automatically before booting the OS)
  94. * If power_off() is not implemented, it must power down the phy.
  95. *
  96. * @phy: PHY port to be de-initialized
  97. * Return: 0 if OK, or a negative error code
  98. */
  99. int (*exit)(struct phy *phy);
  100. /**
  101. * reset - resets a PHY device without shutting down
  102. *
  103. * @phy: PHY port to be reset
  104. *
  105. * During runtime, the PHY may need to be reset in order to
  106. * re-establish connection etc without being shut down or exit.
  107. *
  108. * Return: 0 if OK, or a negative error code
  109. */
  110. int (*reset)(struct phy *phy);
  111. /**
  112. * power_on - power on a PHY device
  113. *
  114. * @phy: PHY port to be powered on
  115. *
  116. * During runtime, the PHY may need to be powered on or off several
  117. * times. This function is used to power on the PHY. It relies on the
  118. * setup done in init(). If init() is not implemented, it must take care
  119. * of setting up the context (PLLs, ...)
  120. *
  121. * Return: 0 if OK, or a negative error code
  122. */
  123. int (*power_on)(struct phy *phy);
  124. /**
  125. * power_off - power off a PHY device
  126. *
  127. * @phy: PHY port to be powered off
  128. *
  129. * During runtime, the PHY may need to be powered on or off several
  130. * times. This function is used to power off the PHY. Except if
  131. * init()/deinit() are not implemented, it must not de-initialize
  132. * everything.
  133. *
  134. * Return: 0 if OK, or a negative error code
  135. */
  136. int (*power_off)(struct phy *phy);
  137. /**
  138. * configure - configure a PHY device
  139. *
  140. * @phy: PHY port to be configured
  141. * @params: PHY Parameters, underlying data is specific to the PHY function
  142. *
  143. * During runtime, the PHY may need to be configured for it's main function.
  144. * This function configures the PHY for it's main function following
  145. * power_on/off() after being initialized.
  146. *
  147. * Return: 0 if OK, or a negative error code
  148. */
  149. int (*configure)(struct phy *phy, void *params);
  150. /**
  151. * set_mode - set PHY device mode
  152. *
  153. * @phy: PHY port to be configured
  154. * @mode: PHY mode
  155. * @submode: PHY submode
  156. *
  157. * Configure PHY mode (e.g. USB, Ethernet, ...) and submode
  158. * (e.g. for Ethernet this can be RGMII).
  159. *
  160. * Return: 0 if OK, or a negative error code
  161. */
  162. int (*set_mode)(struct phy *phy, enum phy_mode mode, int submode);
  163. /**
  164. * set_speed - set PHY device speed
  165. *
  166. * @phy: PHY port to be configured
  167. * @speed: PHY speed
  168. *
  169. * Configure PHY speed (e.g. for Ethernet, this could be 10 or 100 ...).
  170. *
  171. * Return: 0 if OK, or a negative error code
  172. */
  173. int (*set_speed)(struct phy *phy, int speed);
  174. };
  175. /**
  176. * struct phy_bulk - A handle to (allowing control of) a bulk of phys.
  177. *
  178. * Consumers provide storage for the phy bulk. The content of the structure is
  179. * managed solely by the phy API. A phy bulk struct is initialized
  180. * by "get"ing the phy bulk struct.
  181. * The phy bulk struct is passed to all other bulk phy APIs to apply
  182. * the API to all the phy in the bulk struct.
  183. *
  184. * @phys: An array of phy handles.
  185. * @count: The number of phy handles in the phys array.
  186. */
  187. struct phy_bulk {
  188. struct phy *phys;
  189. unsigned int count;
  190. };
  191. #if CONFIG_IS_ENABLED(PHY)
  192. /**
  193. * generic_phy_init() - initialize the PHY port
  194. *
  195. * @phy: the PHY port to initialize
  196. * Return: 0 if OK, or a negative error code
  197. */
  198. int generic_phy_init(struct phy *phy);
  199. /**
  200. * generic_phy_init() - de-initialize the PHY device
  201. *
  202. * @phy: PHY port to be de-initialized
  203. * Return: 0 if OK, or a negative error code
  204. */
  205. int generic_phy_exit(struct phy *phy);
  206. /**
  207. * generic_phy_reset() - resets a PHY device without shutting down
  208. *
  209. * @phy: PHY port to be reset
  210. *Return: 0 if OK, or a negative error code
  211. */
  212. int generic_phy_reset(struct phy *phy);
  213. /**
  214. * generic_phy_power_on() - power on a PHY device
  215. *
  216. * @phy: PHY port to be powered on
  217. * Return: 0 if OK, or a negative error code
  218. */
  219. int generic_phy_power_on(struct phy *phy);
  220. /**
  221. * generic_phy_power_off() - power off a PHY device
  222. *
  223. * @phy: PHY port to be powered off
  224. * Return: 0 if OK, or a negative error code
  225. */
  226. int generic_phy_power_off(struct phy *phy);
  227. /**
  228. * generic_phy_configure() - configure a PHY device
  229. *
  230. * @phy: PHY port to be configured
  231. * @params: PHY Parameters, underlying data is specific to the PHY function
  232. * Return: 0 if OK, or a negative error code
  233. */
  234. int generic_phy_configure(struct phy *phy, void *params);
  235. /**
  236. * generic_phy_set_mode() - set PHY device mode
  237. *
  238. * @phy: PHY port to be configured
  239. * @mode: PHY mode
  240. * @submode: PHY submode
  241. * Return: 0 if OK, or a negative error code
  242. */
  243. int generic_phy_set_mode(struct phy *phy, enum phy_mode mode, int submode);
  244. /**
  245. * generic_phy_set_speed() - set PHY device speed
  246. *
  247. * @phy: PHY port to be configured
  248. * @speed: PHY speed
  249. * Return: 0 if OK, or a negative error code
  250. */
  251. int generic_phy_set_speed(struct phy *phy, int speed);
  252. /**
  253. * generic_phy_get_by_index() - Get a PHY device by integer index.
  254. *
  255. * @user: the client device
  256. * @index: The index in the list of available PHYs
  257. * @phy: A pointer to the PHY port
  258. *
  259. * This looks up a PHY device for a client device based on its position in the
  260. * list of the possible PHYs.
  261. *
  262. * example:
  263. * usb1: usb_otg_ss@xxx {
  264. * compatible = "xxx";
  265. * reg = <xxx>;
  266. * .
  267. * .
  268. * phys = <&usb2_phy>, <&usb3_phy>;
  269. * .
  270. * .
  271. * };
  272. * the USB2 phy can be accessed by passing index '0' and the USB3 phy can
  273. * be accessed by passing index '1'
  274. *
  275. * Return: 0 if OK, or a negative error code
  276. */
  277. int generic_phy_get_by_index(struct udevice *user, int index,
  278. struct phy *phy);
  279. /**
  280. * generic_phy_get_by_index_nodev() - Get a PHY device by integer index
  281. * without a device
  282. *
  283. * @node: The client ofnode.
  284. * @index: The index in the list of available PHYs
  285. * @phy: A pointer to the PHY port
  286. *
  287. * This is a version of generic_phy_get_by_index() that does not use a device.
  288. *
  289. * This looks up a PHY device for a client device based on its ofnode and on
  290. * its position in the list of the possible PHYs.
  291. *
  292. * example:
  293. * usb1: usb_otg_ss@xxx {
  294. * compatible = "xxx";
  295. * reg = <xxx>;
  296. * .
  297. * .
  298. * phys = <&usb2_phy>, <&usb3_phy>;
  299. * .
  300. * .
  301. * };
  302. * the USB2 phy can be accessed by passing index '0' and the USB3 phy can
  303. * be accessed by passing index '1'
  304. *
  305. * Return: 0 if OK, or a negative error code
  306. */
  307. int generic_phy_get_by_index_nodev(ofnode node, int index, struct phy *phy);
  308. /**
  309. * generic_phy_get_by_name() - Get a PHY device by its name.
  310. *
  311. * @user: the client device
  312. * @phy_name: The name of the PHY in the list of possible PHYs
  313. * @phy: A pointer to the PHY port
  314. *
  315. * This looks up a PHY device for a client device in the
  316. * list of the possible PHYs based on its name.
  317. *
  318. * example:
  319. * usb1: usb_otg_ss@xxx {
  320. * compatible = "xxx";
  321. * reg = <xxx>;
  322. * .
  323. * .
  324. * phys = <&usb2_phy>, <&usb3_phy>;
  325. * phy-names = "usb2phy", "usb3phy";
  326. * .
  327. * .
  328. * };
  329. * the USB3 phy can be accessed using "usb3phy", and USB2 by using "usb2phy"
  330. *
  331. * Return: 0 if OK, or a negative error code
  332. */
  333. int generic_phy_get_by_name(struct udevice *user, const char *phy_name,
  334. struct phy *phy);
  335. /**
  336. * generic_phy_get_bulk - Get all phys of a device.
  337. *
  338. * This looks up and gets all phys of the consumer device; each device is
  339. * assumed to have n phys associated with it somehow, and this function finds
  340. * and gets all of them in a separate structure.
  341. *
  342. * @dev: The consumer device.
  343. * @bulk A pointer to a phy bulk struct to initialize.
  344. * Return: 0 if OK, or a negative error code.
  345. */
  346. int generic_phy_get_bulk(struct udevice *dev, struct phy_bulk *bulk);
  347. /**
  348. * generic_phy_init_bulk() - Initialize all phys in a phy bulk struct.
  349. *
  350. * @bulk: A phy bulk struct that was previously successfully requested
  351. * by generic_phy_get_bulk().
  352. * Return: 0 if OK, or negative error code.
  353. */
  354. int generic_phy_init_bulk(struct phy_bulk *bulk);
  355. /**
  356. * generic_phy_exit_bulk() - de-initialize all phys in a phy bulk struct.
  357. *
  358. * @bulk: A phy bulk struct that was previously successfully requested
  359. * by generic_phy_get_bulk().
  360. * Return: 0 if OK, or negative error code.
  361. */
  362. int generic_phy_exit_bulk(struct phy_bulk *bulk);
  363. /**
  364. * generic_phy_power_on_bulk() - Power on all phys in a phy bulk struct.
  365. *
  366. * @bulk: A phy bulk struct that was previously successfully requested
  367. * by generic_phy_get_bulk().
  368. * Return: 0 if OK, or negative error code.
  369. */
  370. int generic_phy_power_on_bulk(struct phy_bulk *bulk);
  371. /**
  372. * generic_phy_power_off_bulk() - Power off all phys in a phy bulk struct.
  373. *
  374. * @bulk: A phy bulk struct that was previously successfully requested
  375. * by generic_phy_get_bulk().
  376. * Return: 0 if OK, or negative error code.
  377. */
  378. int generic_phy_power_off_bulk(struct phy_bulk *bulk);
  379. /**
  380. * generic_setup_phy() - Get, initialize and power on phy.
  381. *
  382. * @dev: The consumer device.
  383. * @phy: A pointer to the PHY port
  384. * @index: The index in the list of available PHYs
  385. *
  386. * Return: 0 if OK, or negative error code.
  387. */
  388. int generic_setup_phy(struct udevice *dev, struct phy *phy, int index);
  389. /**
  390. * generic_shutdown_phy() - Power off and de-initialize phy.
  391. *
  392. * @phy: A pointer to the PHY port.
  393. *
  394. * Return: 0 if OK, or negative error code.
  395. */
  396. int generic_shutdown_phy(struct phy *phy);
  397. #else /* CONFIG_PHY */
  398. static inline int generic_phy_init(struct phy *phy)
  399. {
  400. return 0;
  401. }
  402. static inline int generic_phy_exit(struct phy *phy)
  403. {
  404. return 0;
  405. }
  406. static inline int generic_phy_reset(struct phy *phy)
  407. {
  408. return 0;
  409. }
  410. static inline int generic_phy_power_on(struct phy *phy)
  411. {
  412. return 0;
  413. }
  414. static inline int generic_phy_power_off(struct phy *phy)
  415. {
  416. return 0;
  417. }
  418. static inline int generic_phy_configure(struct phy *phy, void *params)
  419. {
  420. return 0;
  421. }
  422. static inline int generic_phy_set_mode(struct phy *phy, enum phy_mode mode, int submode)
  423. {
  424. return 0;
  425. }
  426. static inline int generic_phy_set_speed(struct phy *phy, int speed)
  427. {
  428. return 0;
  429. }
  430. static inline int generic_phy_get_by_index(struct udevice *user, int index,
  431. struct phy *phy)
  432. {
  433. return 0;
  434. }
  435. static inline int generic_phy_get_by_name(struct udevice *user, const char *phy_name,
  436. struct phy *phy)
  437. {
  438. return 0;
  439. }
  440. static inline int
  441. generic_phy_get_bulk(struct udevice *dev, struct phy_bulk *bulk)
  442. {
  443. return 0;
  444. }
  445. static inline int generic_phy_init_bulk(struct phy_bulk *bulk)
  446. {
  447. return 0;
  448. }
  449. static inline int generic_phy_exit_bulk(struct phy_bulk *bulk)
  450. {
  451. return 0;
  452. }
  453. static inline int generic_phy_power_on_bulk(struct phy_bulk *bulk)
  454. {
  455. return 0;
  456. }
  457. static inline int generic_phy_power_off_bulk(struct phy_bulk *bulk)
  458. {
  459. return 0;
  460. }
  461. static inline int generic_setup_phy(struct udevice *dev, struct phy *phy, int index)
  462. {
  463. return 0;
  464. }
  465. static inline int generic_shutdown_phy(struct phy *phy)
  466. {
  467. return 0;
  468. }
  469. #endif /* CONFIG_PHY */
  470. /**
  471. * generic_phy_valid() - check if PHY port is valid
  472. *
  473. * @phy: the PHY port to check
  474. * Return: TRUE if valid, or FALSE
  475. */
  476. static inline bool generic_phy_valid(struct phy *phy)
  477. {
  478. return phy && phy->dev;
  479. }
  480. #endif /*__GENERIC_PHY_H */