mdio-i2c.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * MDIO I2C bridge
  3. *
  4. * Copyright (C) 2015-2016 Russell King
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * Network PHYs can appear on I2C buses when they are part of SFP module.
  11. * This driver exposes these PHYs to the networking PHY code, allowing
  12. * our PHY drivers access to these PHYs, and so allowing configuration
  13. * of their settings.
  14. */
  15. #include <linux/i2c.h>
  16. #include <linux/phy.h>
  17. #include "mdio-i2c.h"
  18. /*
  19. * I2C bus addresses 0x50 and 0x51 are normally an EEPROM, which is
  20. * specified to be present in SFP modules. These correspond with PHY
  21. * addresses 16 and 17. Disallow access to these "phy" addresses.
  22. */
  23. static bool i2c_mii_valid_phy_id(int phy_id)
  24. {
  25. return phy_id != 0x10 && phy_id != 0x11;
  26. }
  27. static unsigned int i2c_mii_phy_addr(int phy_id)
  28. {
  29. return phy_id + 0x40;
  30. }
  31. static int i2c_mii_read(struct mii_bus *bus, int phy_id, int reg)
  32. {
  33. struct i2c_adapter *i2c = bus->priv;
  34. struct i2c_msg msgs[2];
  35. u8 data[2], dev_addr = reg;
  36. int bus_addr, ret;
  37. if (!i2c_mii_valid_phy_id(phy_id))
  38. return 0xffff;
  39. bus_addr = i2c_mii_phy_addr(phy_id);
  40. msgs[0].addr = bus_addr;
  41. msgs[0].flags = 0;
  42. msgs[0].len = 1;
  43. msgs[0].buf = &dev_addr;
  44. msgs[1].addr = bus_addr;
  45. msgs[1].flags = I2C_M_RD;
  46. msgs[1].len = sizeof(data);
  47. msgs[1].buf = data;
  48. ret = i2c_transfer(i2c, msgs, ARRAY_SIZE(msgs));
  49. if (ret != ARRAY_SIZE(msgs))
  50. return 0xffff;
  51. return data[0] << 8 | data[1];
  52. }
  53. static int i2c_mii_write(struct mii_bus *bus, int phy_id, int reg, u16 val)
  54. {
  55. struct i2c_adapter *i2c = bus->priv;
  56. struct i2c_msg msg;
  57. int ret;
  58. u8 data[3];
  59. if (!i2c_mii_valid_phy_id(phy_id))
  60. return 0;
  61. data[0] = reg;
  62. data[1] = val >> 8;
  63. data[2] = val;
  64. msg.addr = i2c_mii_phy_addr(phy_id);
  65. msg.flags = 0;
  66. msg.len = 3;
  67. msg.buf = data;
  68. ret = i2c_transfer(i2c, &msg, 1);
  69. return ret < 0 ? ret : 0;
  70. }
  71. struct mii_bus *mdio_i2c_alloc(struct device *parent, struct i2c_adapter *i2c)
  72. {
  73. struct mii_bus *mii;
  74. if (!i2c_check_functionality(i2c, I2C_FUNC_I2C))
  75. return ERR_PTR(-EINVAL);
  76. mii = mdiobus_alloc();
  77. if (!mii)
  78. return ERR_PTR(-ENOMEM);
  79. snprintf(mii->id, MII_BUS_ID_SIZE, "i2c:%s", dev_name(parent));
  80. mii->parent = parent;
  81. mii->read = i2c_mii_read;
  82. mii->write = i2c_mii_write;
  83. mii->priv = i2c;
  84. return mii;
  85. }
  86. EXPORT_SYMBOL_GPL(mdio_i2c_alloc);
  87. MODULE_AUTHOR("Russell King");
  88. MODULE_DESCRIPTION("MDIO I2C bridge library");
  89. MODULE_LICENSE("GPL v2");