mdio_bus.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  1. /* MDIO Bus interface
  2. *
  3. * Author: Andy Fleming
  4. *
  5. * Copyright (c) 2004 Freescale Semiconductor, Inc.
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2 of the License, or (at your
  10. * option) any later version.
  11. *
  12. */
  13. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  14. #include <linux/kernel.h>
  15. #include <linux/string.h>
  16. #include <linux/errno.h>
  17. #include <linux/unistd.h>
  18. #include <linux/slab.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/init.h>
  21. #include <linux/delay.h>
  22. #include <linux/device.h>
  23. #include <linux/gpio.h>
  24. #include <linux/gpio/consumer.h>
  25. #include <linux/of_device.h>
  26. #include <linux/of_mdio.h>
  27. #include <linux/of_gpio.h>
  28. #include <linux/netdevice.h>
  29. #include <linux/etherdevice.h>
  30. #include <linux/skbuff.h>
  31. #include <linux/spinlock.h>
  32. #include <linux/mm.h>
  33. #include <linux/module.h>
  34. #include <linux/mii.h>
  35. #include <linux/ethtool.h>
  36. #include <linux/phy.h>
  37. #include <linux/io.h>
  38. #include <linux/uaccess.h>
  39. #include <asm/irq.h>
  40. #define CREATE_TRACE_POINTS
  41. #include <trace/events/mdio.h>
  42. #include "mdio-boardinfo.h"
  43. int mdiobus_register_device(struct mdio_device *mdiodev)
  44. {
  45. if (mdiodev->bus->mdio_map[mdiodev->addr])
  46. return -EBUSY;
  47. mdiodev->bus->mdio_map[mdiodev->addr] = mdiodev;
  48. return 0;
  49. }
  50. EXPORT_SYMBOL(mdiobus_register_device);
  51. int mdiobus_unregister_device(struct mdio_device *mdiodev)
  52. {
  53. if (mdiodev->bus->mdio_map[mdiodev->addr] != mdiodev)
  54. return -EINVAL;
  55. mdiodev->bus->mdio_map[mdiodev->addr] = NULL;
  56. return 0;
  57. }
  58. EXPORT_SYMBOL(mdiobus_unregister_device);
  59. struct phy_device *mdiobus_get_phy(struct mii_bus *bus, int addr)
  60. {
  61. struct mdio_device *mdiodev = bus->mdio_map[addr];
  62. if (!mdiodev)
  63. return NULL;
  64. if (!(mdiodev->flags & MDIO_DEVICE_FLAG_PHY))
  65. return NULL;
  66. return container_of(mdiodev, struct phy_device, mdio);
  67. }
  68. EXPORT_SYMBOL(mdiobus_get_phy);
  69. bool mdiobus_is_registered_device(struct mii_bus *bus, int addr)
  70. {
  71. return bus->mdio_map[addr];
  72. }
  73. EXPORT_SYMBOL(mdiobus_is_registered_device);
  74. /**
  75. * mdiobus_alloc_size - allocate a mii_bus structure
  76. * @size: extra amount of memory to allocate for private storage.
  77. * If non-zero, then bus->priv is points to that memory.
  78. *
  79. * Description: called by a bus driver to allocate an mii_bus
  80. * structure to fill in.
  81. */
  82. struct mii_bus *mdiobus_alloc_size(size_t size)
  83. {
  84. struct mii_bus *bus;
  85. size_t aligned_size = ALIGN(sizeof(*bus), NETDEV_ALIGN);
  86. size_t alloc_size;
  87. int i;
  88. /* If we alloc extra space, it should be aligned */
  89. if (size)
  90. alloc_size = aligned_size + size;
  91. else
  92. alloc_size = sizeof(*bus);
  93. bus = kzalloc(alloc_size, GFP_KERNEL);
  94. if (!bus)
  95. return NULL;
  96. bus->state = MDIOBUS_ALLOCATED;
  97. if (size)
  98. bus->priv = (void *)bus + aligned_size;
  99. /* Initialise the interrupts to polling */
  100. for (i = 0; i < PHY_MAX_ADDR; i++)
  101. bus->irq[i] = PHY_POLL;
  102. return bus;
  103. }
  104. EXPORT_SYMBOL(mdiobus_alloc_size);
  105. static void _devm_mdiobus_free(struct device *dev, void *res)
  106. {
  107. mdiobus_free(*(struct mii_bus **)res);
  108. }
  109. static int devm_mdiobus_match(struct device *dev, void *res, void *data)
  110. {
  111. struct mii_bus **r = res;
  112. if (WARN_ON(!r || !*r))
  113. return 0;
  114. return *r == data;
  115. }
  116. /**
  117. * devm_mdiobus_alloc_size - Resource-managed mdiobus_alloc_size()
  118. * @dev: Device to allocate mii_bus for
  119. * @sizeof_priv: Space to allocate for private structure.
  120. *
  121. * Managed mdiobus_alloc_size. mii_bus allocated with this function is
  122. * automatically freed on driver detach.
  123. *
  124. * If an mii_bus allocated with this function needs to be freed separately,
  125. * devm_mdiobus_free() must be used.
  126. *
  127. * RETURNS:
  128. * Pointer to allocated mii_bus on success, NULL on failure.
  129. */
  130. struct mii_bus *devm_mdiobus_alloc_size(struct device *dev, int sizeof_priv)
  131. {
  132. struct mii_bus **ptr, *bus;
  133. ptr = devres_alloc(_devm_mdiobus_free, sizeof(*ptr), GFP_KERNEL);
  134. if (!ptr)
  135. return NULL;
  136. /* use raw alloc_dr for kmalloc caller tracing */
  137. bus = mdiobus_alloc_size(sizeof_priv);
  138. if (bus) {
  139. *ptr = bus;
  140. devres_add(dev, ptr);
  141. } else {
  142. devres_free(ptr);
  143. }
  144. return bus;
  145. }
  146. EXPORT_SYMBOL_GPL(devm_mdiobus_alloc_size);
  147. /**
  148. * devm_mdiobus_free - Resource-managed mdiobus_free()
  149. * @dev: Device this mii_bus belongs to
  150. * @bus: the mii_bus associated with the device
  151. *
  152. * Free mii_bus allocated with devm_mdiobus_alloc_size().
  153. */
  154. void devm_mdiobus_free(struct device *dev, struct mii_bus *bus)
  155. {
  156. int rc;
  157. rc = devres_release(dev, _devm_mdiobus_free,
  158. devm_mdiobus_match, bus);
  159. WARN_ON(rc);
  160. }
  161. EXPORT_SYMBOL_GPL(devm_mdiobus_free);
  162. /**
  163. * mdiobus_release - mii_bus device release callback
  164. * @d: the target struct device that contains the mii_bus
  165. *
  166. * Description: called when the last reference to an mii_bus is
  167. * dropped, to free the underlying memory.
  168. */
  169. static void mdiobus_release(struct device *d)
  170. {
  171. struct mii_bus *bus = to_mii_bus(d);
  172. BUG_ON(bus->state != MDIOBUS_RELEASED &&
  173. /* for compatibility with error handling in drivers */
  174. bus->state != MDIOBUS_ALLOCATED);
  175. kfree(bus);
  176. }
  177. static struct class mdio_bus_class = {
  178. .name = "mdio_bus",
  179. .dev_release = mdiobus_release,
  180. };
  181. #if IS_ENABLED(CONFIG_OF_MDIO)
  182. /* Helper function for of_mdio_find_bus */
  183. static int of_mdio_bus_match(struct device *dev, const void *mdio_bus_np)
  184. {
  185. return dev->of_node == mdio_bus_np;
  186. }
  187. /**
  188. * of_mdio_find_bus - Given an mii_bus node, find the mii_bus.
  189. * @mdio_bus_np: Pointer to the mii_bus.
  190. *
  191. * Returns a reference to the mii_bus, or NULL if none found. The
  192. * embedded struct device will have its reference count incremented,
  193. * and this must be put once the bus is finished with.
  194. *
  195. * Because the association of a device_node and mii_bus is made via
  196. * of_mdiobus_register(), the mii_bus cannot be found before it is
  197. * registered with of_mdiobus_register().
  198. *
  199. */
  200. struct mii_bus *of_mdio_find_bus(struct device_node *mdio_bus_np)
  201. {
  202. struct device *d;
  203. if (!mdio_bus_np)
  204. return NULL;
  205. d = class_find_device(&mdio_bus_class, NULL, mdio_bus_np,
  206. of_mdio_bus_match);
  207. return d ? to_mii_bus(d) : NULL;
  208. }
  209. EXPORT_SYMBOL(of_mdio_find_bus);
  210. /* Walk the list of subnodes of a mdio bus and look for a node that
  211. * matches the mdio device's address with its 'reg' property. If
  212. * found, set the of_node pointer for the mdio device. This allows
  213. * auto-probed phy devices to be supplied with information passed in
  214. * via DT.
  215. */
  216. static void of_mdiobus_link_mdiodev(struct mii_bus *bus,
  217. struct mdio_device *mdiodev)
  218. {
  219. struct device *dev = &mdiodev->dev;
  220. struct device_node *child;
  221. if (dev->of_node || !bus->dev.of_node)
  222. return;
  223. for_each_available_child_of_node(bus->dev.of_node, child) {
  224. int addr;
  225. int ret;
  226. ret = of_property_read_u32(child, "reg", &addr);
  227. if (ret < 0) {
  228. dev_err(dev, "%s has invalid MDIO address\n",
  229. child->full_name);
  230. continue;
  231. }
  232. /* A MDIO device must have a reg property in the range [0-31] */
  233. if (addr >= PHY_MAX_ADDR) {
  234. dev_err(dev, "%s MDIO address %i is too large\n",
  235. child->full_name, addr);
  236. continue;
  237. }
  238. if (addr == mdiodev->addr) {
  239. dev->of_node = child;
  240. return;
  241. }
  242. }
  243. }
  244. #else /* !IS_ENABLED(CONFIG_OF_MDIO) */
  245. static inline void of_mdiobus_link_mdiodev(struct mii_bus *mdio,
  246. struct mdio_device *mdiodev)
  247. {
  248. }
  249. #endif
  250. /**
  251. * mdiobus_create_device_from_board_info - create a full MDIO device given
  252. * a mdio_board_info structure
  253. * @bus: MDIO bus to create the devices on
  254. * @bi: mdio_board_info structure describing the devices
  255. *
  256. * Returns 0 on success or < 0 on error.
  257. */
  258. static int mdiobus_create_device(struct mii_bus *bus,
  259. struct mdio_board_info *bi)
  260. {
  261. struct mdio_device *mdiodev;
  262. int ret = 0;
  263. mdiodev = mdio_device_create(bus, bi->mdio_addr);
  264. if (IS_ERR(mdiodev))
  265. return -ENODEV;
  266. strncpy(mdiodev->modalias, bi->modalias,
  267. sizeof(mdiodev->modalias));
  268. mdiodev->bus_match = mdio_device_bus_match;
  269. mdiodev->dev.platform_data = (void *)bi->platform_data;
  270. ret = mdio_device_register(mdiodev);
  271. if (ret)
  272. mdio_device_free(mdiodev);
  273. return ret;
  274. }
  275. /**
  276. * __mdiobus_register - bring up all the PHYs on a given bus and attach them to bus
  277. * @bus: target mii_bus
  278. * @owner: module containing bus accessor functions
  279. *
  280. * Description: Called by a bus driver to bring up all the PHYs
  281. * on a given bus, and attach them to the bus. Drivers should use
  282. * mdiobus_register() rather than __mdiobus_register() unless they
  283. * need to pass a specific owner module. MDIO devices which are not
  284. * PHYs will not be brought up by this function. They are expected to
  285. * to be explicitly listed in DT and instantiated by of_mdiobus_register().
  286. *
  287. * Returns 0 on success or < 0 on error.
  288. */
  289. int __mdiobus_register(struct mii_bus *bus, struct module *owner)
  290. {
  291. struct mdio_device *mdiodev;
  292. int i, err;
  293. struct gpio_desc *gpiod;
  294. if (NULL == bus || NULL == bus->name ||
  295. NULL == bus->read || NULL == bus->write)
  296. return -EINVAL;
  297. BUG_ON(bus->state != MDIOBUS_ALLOCATED &&
  298. bus->state != MDIOBUS_UNREGISTERED);
  299. bus->owner = owner;
  300. bus->dev.parent = bus->parent;
  301. bus->dev.class = &mdio_bus_class;
  302. bus->dev.groups = NULL;
  303. dev_set_name(&bus->dev, "%s", bus->id);
  304. err = device_register(&bus->dev);
  305. if (err) {
  306. pr_err("mii_bus %s failed to register\n", bus->id);
  307. put_device(&bus->dev);
  308. return -EINVAL;
  309. }
  310. mutex_init(&bus->mdio_lock);
  311. /* de-assert bus level PHY GPIO resets */
  312. if (bus->num_reset_gpios > 0) {
  313. bus->reset_gpiod = devm_kcalloc(&bus->dev,
  314. bus->num_reset_gpios,
  315. sizeof(struct gpio_desc *),
  316. GFP_KERNEL);
  317. if (!bus->reset_gpiod)
  318. return -ENOMEM;
  319. }
  320. for (i = 0; i < bus->num_reset_gpios; i++) {
  321. gpiod = devm_gpiod_get_index(&bus->dev, "reset", i,
  322. GPIOD_OUT_LOW);
  323. if (IS_ERR(gpiod)) {
  324. err = PTR_ERR(gpiod);
  325. if (err != -ENOENT) {
  326. dev_err(&bus->dev,
  327. "mii_bus %s couldn't get reset GPIO\n",
  328. bus->id);
  329. return err;
  330. }
  331. } else {
  332. bus->reset_gpiod[i] = gpiod;
  333. gpiod_set_value_cansleep(gpiod, 1);
  334. udelay(bus->reset_delay_us);
  335. gpiod_set_value_cansleep(gpiod, 0);
  336. }
  337. }
  338. if (bus->reset)
  339. bus->reset(bus);
  340. for (i = 0; i < PHY_MAX_ADDR; i++) {
  341. if ((bus->phy_mask & (1 << i)) == 0) {
  342. struct phy_device *phydev;
  343. phydev = mdiobus_scan(bus, i);
  344. if (IS_ERR(phydev) && (PTR_ERR(phydev) != -ENODEV)) {
  345. err = PTR_ERR(phydev);
  346. goto error;
  347. }
  348. }
  349. }
  350. mdiobus_setup_mdiodev_from_board_info(bus, mdiobus_create_device);
  351. bus->state = MDIOBUS_REGISTERED;
  352. pr_info("%s: probed\n", bus->name);
  353. return 0;
  354. error:
  355. while (--i >= 0) {
  356. mdiodev = bus->mdio_map[i];
  357. if (!mdiodev)
  358. continue;
  359. mdiodev->device_remove(mdiodev);
  360. mdiodev->device_free(mdiodev);
  361. }
  362. /* Put PHYs in RESET to save power */
  363. for (i = 0; i < bus->num_reset_gpios; i++) {
  364. if (bus->reset_gpiod[i])
  365. gpiod_set_value_cansleep(bus->reset_gpiod[i], 1);
  366. }
  367. device_del(&bus->dev);
  368. return err;
  369. }
  370. EXPORT_SYMBOL(__mdiobus_register);
  371. void mdiobus_unregister(struct mii_bus *bus)
  372. {
  373. struct mdio_device *mdiodev;
  374. int i;
  375. BUG_ON(bus->state != MDIOBUS_REGISTERED);
  376. bus->state = MDIOBUS_UNREGISTERED;
  377. for (i = 0; i < PHY_MAX_ADDR; i++) {
  378. mdiodev = bus->mdio_map[i];
  379. if (!mdiodev)
  380. continue;
  381. mdiodev->device_remove(mdiodev);
  382. mdiodev->device_free(mdiodev);
  383. }
  384. /* Put PHYs in RESET to save power */
  385. for (i = 0; i < bus->num_reset_gpios; i++) {
  386. if (bus->reset_gpiod[i])
  387. gpiod_set_value_cansleep(bus->reset_gpiod[i], 1);
  388. }
  389. device_del(&bus->dev);
  390. }
  391. EXPORT_SYMBOL(mdiobus_unregister);
  392. /**
  393. * mdiobus_free - free a struct mii_bus
  394. * @bus: mii_bus to free
  395. *
  396. * This function releases the reference to the underlying device
  397. * object in the mii_bus. If this is the last reference, the mii_bus
  398. * will be freed.
  399. */
  400. void mdiobus_free(struct mii_bus *bus)
  401. {
  402. /* For compatibility with error handling in drivers. */
  403. if (bus->state == MDIOBUS_ALLOCATED) {
  404. kfree(bus);
  405. return;
  406. }
  407. BUG_ON(bus->state != MDIOBUS_UNREGISTERED);
  408. bus->state = MDIOBUS_RELEASED;
  409. put_device(&bus->dev);
  410. }
  411. EXPORT_SYMBOL(mdiobus_free);
  412. /**
  413. * mdiobus_scan - scan a bus for MDIO devices.
  414. * @bus: mii_bus to scan
  415. * @addr: address on bus to scan
  416. *
  417. * This function scans the MDIO bus, looking for devices which can be
  418. * identified using a vendor/product ID in registers 2 and 3. Not all
  419. * MDIO devices have such registers, but PHY devices typically
  420. * do. Hence this function assumes anything found is a PHY, or can be
  421. * treated as a PHY. Other MDIO devices, such as switches, will
  422. * probably not be found during the scan.
  423. */
  424. struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr)
  425. {
  426. struct phy_device *phydev;
  427. int err;
  428. phydev = get_phy_device(bus, addr, false);
  429. if (IS_ERR(phydev))
  430. return phydev;
  431. /*
  432. * For DT, see if the auto-probed phy has a correspoding child
  433. * in the bus node, and set the of_node pointer in this case.
  434. */
  435. of_mdiobus_link_mdiodev(bus, &phydev->mdio);
  436. err = phy_device_register(phydev);
  437. if (err) {
  438. phy_device_free(phydev);
  439. return ERR_PTR(-ENODEV);
  440. }
  441. return phydev;
  442. }
  443. EXPORT_SYMBOL(mdiobus_scan);
  444. /**
  445. * mdiobus_read_nested - Nested version of the mdiobus_read function
  446. * @bus: the mii_bus struct
  447. * @addr: the phy address
  448. * @regnum: register number to read
  449. *
  450. * In case of nested MDIO bus access avoid lockdep false positives by
  451. * using mutex_lock_nested().
  452. *
  453. * NOTE: MUST NOT be called from interrupt context,
  454. * because the bus read/write functions may wait for an interrupt
  455. * to conclude the operation.
  456. */
  457. int mdiobus_read_nested(struct mii_bus *bus, int addr, u32 regnum)
  458. {
  459. int retval;
  460. BUG_ON(in_interrupt());
  461. mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
  462. retval = bus->read(bus, addr, regnum);
  463. mutex_unlock(&bus->mdio_lock);
  464. trace_mdio_access(bus, 1, addr, regnum, retval, retval);
  465. return retval;
  466. }
  467. EXPORT_SYMBOL(mdiobus_read_nested);
  468. /**
  469. * mdiobus_read - Convenience function for reading a given MII mgmt register
  470. * @bus: the mii_bus struct
  471. * @addr: the phy address
  472. * @regnum: register number to read
  473. *
  474. * NOTE: MUST NOT be called from interrupt context,
  475. * because the bus read/write functions may wait for an interrupt
  476. * to conclude the operation.
  477. */
  478. int mdiobus_read(struct mii_bus *bus, int addr, u32 regnum)
  479. {
  480. int retval;
  481. BUG_ON(in_interrupt());
  482. mutex_lock(&bus->mdio_lock);
  483. retval = bus->read(bus, addr, regnum);
  484. mutex_unlock(&bus->mdio_lock);
  485. trace_mdio_access(bus, 1, addr, regnum, retval, retval);
  486. return retval;
  487. }
  488. EXPORT_SYMBOL(mdiobus_read);
  489. /**
  490. * mdiobus_write_nested - Nested version of the mdiobus_write function
  491. * @bus: the mii_bus struct
  492. * @addr: the phy address
  493. * @regnum: register number to write
  494. * @val: value to write to @regnum
  495. *
  496. * In case of nested MDIO bus access avoid lockdep false positives by
  497. * using mutex_lock_nested().
  498. *
  499. * NOTE: MUST NOT be called from interrupt context,
  500. * because the bus read/write functions may wait for an interrupt
  501. * to conclude the operation.
  502. */
  503. int mdiobus_write_nested(struct mii_bus *bus, int addr, u32 regnum, u16 val)
  504. {
  505. int err;
  506. BUG_ON(in_interrupt());
  507. mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
  508. err = bus->write(bus, addr, regnum, val);
  509. mutex_unlock(&bus->mdio_lock);
  510. trace_mdio_access(bus, 0, addr, regnum, val, err);
  511. return err;
  512. }
  513. EXPORT_SYMBOL(mdiobus_write_nested);
  514. /**
  515. * mdiobus_write - Convenience function for writing a given MII mgmt register
  516. * @bus: the mii_bus struct
  517. * @addr: the phy address
  518. * @regnum: register number to write
  519. * @val: value to write to @regnum
  520. *
  521. * NOTE: MUST NOT be called from interrupt context,
  522. * because the bus read/write functions may wait for an interrupt
  523. * to conclude the operation.
  524. */
  525. int mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val)
  526. {
  527. int err;
  528. BUG_ON(in_interrupt());
  529. mutex_lock(&bus->mdio_lock);
  530. err = bus->write(bus, addr, regnum, val);
  531. mutex_unlock(&bus->mdio_lock);
  532. trace_mdio_access(bus, 0, addr, regnum, val, err);
  533. return err;
  534. }
  535. EXPORT_SYMBOL(mdiobus_write);
  536. /**
  537. * mdio_bus_match - determine if given MDIO driver supports the given
  538. * MDIO device
  539. * @dev: target MDIO device
  540. * @drv: given MDIO driver
  541. *
  542. * Description: Given a MDIO device, and a MDIO driver, return 1 if
  543. * the driver supports the device. Otherwise, return 0. This may
  544. * require calling the devices own match function, since different classes
  545. * of MDIO devices have different match criteria.
  546. */
  547. static int mdio_bus_match(struct device *dev, struct device_driver *drv)
  548. {
  549. struct mdio_device *mdio = to_mdio_device(dev);
  550. if (of_driver_match_device(dev, drv))
  551. return 1;
  552. if (mdio->bus_match)
  553. return mdio->bus_match(dev, drv);
  554. return 0;
  555. }
  556. #ifdef CONFIG_PM
  557. static int mdio_bus_suspend(struct device *dev)
  558. {
  559. struct mdio_device *mdio = to_mdio_device(dev);
  560. if (mdio->pm_ops && mdio->pm_ops->suspend)
  561. return mdio->pm_ops->suspend(dev);
  562. return 0;
  563. }
  564. static int mdio_bus_resume(struct device *dev)
  565. {
  566. struct mdio_device *mdio = to_mdio_device(dev);
  567. if (mdio->pm_ops && mdio->pm_ops->resume)
  568. return mdio->pm_ops->resume(dev);
  569. return 0;
  570. }
  571. static int mdio_bus_restore(struct device *dev)
  572. {
  573. struct mdio_device *mdio = to_mdio_device(dev);
  574. if (mdio->pm_ops && mdio->pm_ops->restore)
  575. return mdio->pm_ops->restore(dev);
  576. return 0;
  577. }
  578. static const struct dev_pm_ops mdio_bus_pm_ops = {
  579. .suspend = mdio_bus_suspend,
  580. .resume = mdio_bus_resume,
  581. .freeze = mdio_bus_suspend,
  582. .thaw = mdio_bus_resume,
  583. .restore = mdio_bus_restore,
  584. };
  585. #define MDIO_BUS_PM_OPS (&mdio_bus_pm_ops)
  586. #else
  587. #define MDIO_BUS_PM_OPS NULL
  588. #endif /* CONFIG_PM */
  589. struct bus_type mdio_bus_type = {
  590. .name = "mdio_bus",
  591. .match = mdio_bus_match,
  592. .pm = MDIO_BUS_PM_OPS,
  593. };
  594. EXPORT_SYMBOL(mdio_bus_type);
  595. int __init mdio_bus_init(void)
  596. {
  597. int ret;
  598. ret = class_register(&mdio_bus_class);
  599. if (!ret) {
  600. ret = bus_register(&mdio_bus_type);
  601. if (ret)
  602. class_unregister(&mdio_bus_class);
  603. }
  604. return ret;
  605. }
  606. EXPORT_SYMBOL_GPL(mdio_bus_init);
  607. #if IS_ENABLED(CONFIG_PHYLIB)
  608. void mdio_bus_exit(void)
  609. {
  610. class_unregister(&mdio_bus_class);
  611. bus_unregister(&mdio_bus_type);
  612. }
  613. EXPORT_SYMBOL_GPL(mdio_bus_exit);
  614. #else
  615. module_init(mdio_bus_init);
  616. /* no module_exit, intentional */
  617. MODULE_LICENSE("GPL");
  618. MODULE_DESCRIPTION("MDIO bus/device layer");
  619. #endif