si3474.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Driver for the Skyworks Si3474 PoE PSE Controller
  4. *
  5. * Chip Architecture & Terminology:
  6. *
  7. * The Si3474 is a single-chip PoE PSE controller managing 8 physical power
  8. * delivery channels. Internally, it's structured into two logical "Quads".
  9. *
  10. * Quad 0: Manages physical channels ('ports' in datasheet) 0, 1, 2, 3
  11. * Quad 1: Manages physical channels ('ports' in datasheet) 4, 5, 6, 7
  12. *
  13. * Each Quad is accessed via a separate I2C address. The base address range is
  14. * set by hardware pins A1-A4, and the specific address selects Quad 0 (usually
  15. * the lower/even address) or Quad 1 (usually the higher/odd address).
  16. * See datasheet Table 2.2 for the address mapping.
  17. *
  18. * While the Quads manage channel-specific operations, the Si3474 package has
  19. * several resources shared across the entire chip:
  20. * - Single RESETb input pin.
  21. * - Single INTb output pin (signals interrupts from *either* Quad).
  22. * - Single OSS input pin (Emergency Shutdown).
  23. * - Global I2C Address (0x7F) used for firmware updates.
  24. * - Global status monitoring (Temperature, VDD/VPWR Undervoltage Lockout).
  25. *
  26. * Driver Architecture:
  27. *
  28. * To handle the mix of per-Quad access and shared resources correctly, this
  29. * driver treats the entire Si3474 package as one logical device. The driver
  30. * instance associated with the primary I2C address (Quad 0) takes ownership.
  31. * It discovers and manages the I2C client for the secondary address (Quad 1).
  32. * This primary instance handles shared resources like IRQ management and
  33. * registers a single PSE controller device representing all logical PIs.
  34. * Internal functions route I2C commands to the appropriate Quad's i2c_client
  35. * based on the target channel or PI.
  36. *
  37. * Terminology Mapping:
  38. *
  39. * - "PI" (Power Interface): Refers to the logical PSE port as defined by
  40. * IEEE 802.3 (typically corresponds to an RJ45 connector). This is the
  41. * `id` (0-7) used in the pse_controller_ops.
  42. * - "Channel": Refers to one of the 8 physical power control paths within
  43. * the Si3474 chip itself (hardware channels 0-7). This terminology is
  44. * used internally within the driver to avoid confusion with 'ports'.
  45. * - "Quad": One of the two internal 4-channel management units within the
  46. * Si3474, each accessed via its own I2C address.
  47. *
  48. * Relationship:
  49. * - A 2-Pair PoE PI uses 1 Channel.
  50. * - A 4-Pair PoE PI uses 2 Channels.
  51. *
  52. * ASCII Schematic:
  53. *
  54. * +-----------------------------------------------------+
  55. * | Si3474 Chip |
  56. * | |
  57. * | +---------------------+ +---------------------+ |
  58. * | | Quad 0 | | Quad 1 | |
  59. * | | Channels 0, 1, 2, 3 | | Channels 4, 5, 6, 7 | |
  60. * | +----------^----------+ +-------^-------------+ |
  61. * | I2C Addr 0 | | I2C Addr 1 |
  62. * | +------------------------+ |
  63. * | (Primary Driver Instance) (Managed by Primary) |
  64. * | |
  65. * | Shared Resources (affect whole chip): |
  66. * | - Single INTb Output -> Handled by Primary |
  67. * | - Single RESETb Input |
  68. * | - Single OSS Input -> Handled by Primary |
  69. * | - Global I2C Addr (0x7F) for Firmware Update |
  70. * | - Global Status (Temp, VDD/VPWR UVLO) |
  71. * +-----------------------------------------------------+
  72. * | | | | | | | |
  73. * Ch0 Ch1 Ch2 Ch3 Ch4 Ch5 Ch6 Ch7 (Physical Channels)
  74. *
  75. * Example Mapping (Logical PI to Physical Channel(s)):
  76. * * 2-Pair Mode (8 PIs):
  77. * PI 0 -> Ch 0
  78. * PI 1 -> Ch 1
  79. * ...
  80. * PI 7 -> Ch 7
  81. * * 4-Pair Mode (4 PIs):
  82. * PI 0 -> Ch 0 + Ch 1 (Managed via Quad 0 Addr)
  83. * PI 1 -> Ch 2 + Ch 3 (Managed via Quad 0 Addr)
  84. * PI 2 -> Ch 4 + Ch 5 (Managed via Quad 1 Addr)
  85. * PI 3 -> Ch 6 + Ch 7 (Managed via Quad 1 Addr)
  86. * (Note: Actual mapping depends on Device Tree and PORT_REMAP config)
  87. */
  88. #include <linux/i2c.h>
  89. #include <linux/module.h>
  90. #include <linux/of.h>
  91. #include <linux/platform_device.h>
  92. #include <linux/pse-pd/pse.h>
  93. #define SI3474_MAX_CHANS 8
  94. #define MANUFACTURER_ID 0x08
  95. #define IC_ID 0x05
  96. #define SI3474_DEVICE_ID (MANUFACTURER_ID << 3 | IC_ID)
  97. /* Misc registers */
  98. #define VENDOR_IC_ID_REG 0x1B
  99. #define TEMPERATURE_REG 0x2C
  100. #define FIRMWARE_REVISION_REG 0x41
  101. #define CHIP_REVISION_REG 0x43
  102. /* Main status registers */
  103. #define POWER_STATUS_REG 0x10
  104. #define PORT_MODE_REG 0x12
  105. #define DETECT_CLASS_ENABLE_REG 0x14
  106. /* PORTn Current */
  107. #define PORT1_CURRENT_LSB_REG 0x30
  108. /* PORTn Current [mA], return in [nA] */
  109. /* 1000 * ((PORTn_CURRENT_MSB << 8) + PORTn_CURRENT_LSB) / 16384 */
  110. #define SI3474_NA_STEP (1000 * 1000 * 1000 / 16384)
  111. /* VPWR Voltage */
  112. #define VPWR_LSB_REG 0x2E
  113. #define VPWR_MSB_REG 0x2F
  114. /* PORTn Voltage */
  115. #define PORT1_VOLTAGE_LSB_REG 0x32
  116. /* VPWR Voltage [V], return in [uV] */
  117. /* 60 * (( VPWR_MSB << 8) + VPWR_LSB) / 16384 */
  118. #define SI3474_UV_STEP (1000 * 1000 * 60 / 16384)
  119. /* Helper macros */
  120. #define CHAN_IDX(chan) ((chan) % 4)
  121. #define CHAN_BIT(chan) BIT(CHAN_IDX(chan))
  122. #define CHAN_UPPER_BIT(chan) BIT(CHAN_IDX(chan) + 4)
  123. #define CHAN_MASK(chan) (0x03U << (2 * CHAN_IDX(chan)))
  124. #define CHAN_REG(base, chan) ((base) + (CHAN_IDX(chan) * 4))
  125. struct si3474_pi_desc {
  126. u8 chan[2];
  127. bool is_4p;
  128. };
  129. struct si3474_priv {
  130. struct i2c_client *client[2];
  131. struct pse_controller_dev pcdev;
  132. struct device_node *np;
  133. struct si3474_pi_desc pi[SI3474_MAX_CHANS];
  134. };
  135. static struct si3474_priv *to_si3474_priv(struct pse_controller_dev *pcdev)
  136. {
  137. return container_of(pcdev, struct si3474_priv, pcdev);
  138. }
  139. static void si3474_get_channels(struct si3474_priv *priv, int id,
  140. u8 *chan0, u8 *chan1)
  141. {
  142. *chan0 = priv->pi[id].chan[0];
  143. *chan1 = priv->pi[id].chan[1];
  144. }
  145. static struct i2c_client *si3474_get_chan_client(struct si3474_priv *priv,
  146. u8 chan)
  147. {
  148. return (chan < 4) ? priv->client[0] : priv->client[1];
  149. }
  150. static int si3474_pi_get_admin_state(struct pse_controller_dev *pcdev, int id,
  151. struct pse_admin_state *admin_state)
  152. {
  153. struct si3474_priv *priv = to_si3474_priv(pcdev);
  154. struct i2c_client *client;
  155. bool is_enabled;
  156. u8 chan0, chan1;
  157. s32 ret;
  158. si3474_get_channels(priv, id, &chan0, &chan1);
  159. client = si3474_get_chan_client(priv, chan0);
  160. ret = i2c_smbus_read_byte_data(client, PORT_MODE_REG);
  161. if (ret < 0) {
  162. admin_state->c33_admin_state =
  163. ETHTOOL_C33_PSE_ADMIN_STATE_UNKNOWN;
  164. return ret;
  165. }
  166. is_enabled = ret & (CHAN_MASK(chan0) | CHAN_MASK(chan1));
  167. if (is_enabled)
  168. admin_state->c33_admin_state =
  169. ETHTOOL_C33_PSE_ADMIN_STATE_ENABLED;
  170. else
  171. admin_state->c33_admin_state =
  172. ETHTOOL_C33_PSE_ADMIN_STATE_DISABLED;
  173. return 0;
  174. }
  175. static int si3474_pi_get_pw_status(struct pse_controller_dev *pcdev, int id,
  176. struct pse_pw_status *pw_status)
  177. {
  178. struct si3474_priv *priv = to_si3474_priv(pcdev);
  179. struct i2c_client *client;
  180. bool delivering;
  181. u8 chan0, chan1;
  182. s32 ret;
  183. si3474_get_channels(priv, id, &chan0, &chan1);
  184. client = si3474_get_chan_client(priv, chan0);
  185. ret = i2c_smbus_read_byte_data(client, POWER_STATUS_REG);
  186. if (ret < 0) {
  187. pw_status->c33_pw_status = ETHTOOL_C33_PSE_PW_D_STATUS_UNKNOWN;
  188. return ret;
  189. }
  190. delivering = ret & (CHAN_UPPER_BIT(chan0) | CHAN_UPPER_BIT(chan1));
  191. if (delivering)
  192. pw_status->c33_pw_status =
  193. ETHTOOL_C33_PSE_PW_D_STATUS_DELIVERING;
  194. else
  195. pw_status->c33_pw_status = ETHTOOL_C33_PSE_PW_D_STATUS_DISABLED;
  196. return 0;
  197. }
  198. static int si3474_get_of_channels(struct si3474_priv *priv)
  199. {
  200. struct pse_pi *pi;
  201. u32 chan_id;
  202. u8 pi_no;
  203. s32 ret;
  204. for (pi_no = 0; pi_no < SI3474_MAX_CHANS; pi_no++) {
  205. pi = &priv->pcdev.pi[pi_no];
  206. bool pairset_found = false;
  207. u8 pairset_no;
  208. for (pairset_no = 0; pairset_no < 2; pairset_no++) {
  209. if (!pi->pairset[pairset_no].np)
  210. continue;
  211. pairset_found = true;
  212. ret = of_property_read_u32(pi->pairset[pairset_no].np,
  213. "reg", &chan_id);
  214. if (ret) {
  215. dev_err(&priv->client[0]->dev,
  216. "Failed to read channel reg property\n");
  217. return ret;
  218. }
  219. if (chan_id > SI3474_MAX_CHANS) {
  220. dev_err(&priv->client[0]->dev,
  221. "Incorrect channel number: %d\n", chan_id);
  222. return -EINVAL;
  223. }
  224. priv->pi[pi_no].chan[pairset_no] = chan_id;
  225. /* Mark as 4-pair if second pairset is present */
  226. priv->pi[pi_no].is_4p = (pairset_no == 1);
  227. }
  228. if (pairset_found && !priv->pi[pi_no].is_4p) {
  229. dev_err(&priv->client[0]->dev,
  230. "Second pairset is missing for PI %pOF, only 4p configs are supported\n",
  231. pi->np);
  232. return -EINVAL;
  233. }
  234. }
  235. return 0;
  236. }
  237. static int si3474_setup_pi_matrix(struct pse_controller_dev *pcdev)
  238. {
  239. struct si3474_priv *priv = to_si3474_priv(pcdev);
  240. s32 ret;
  241. ret = si3474_get_of_channels(priv);
  242. if (ret < 0)
  243. dev_warn(&priv->client[0]->dev,
  244. "Unable to parse DT PSE power interface matrix\n");
  245. return ret;
  246. }
  247. static int si3474_pi_enable(struct pse_controller_dev *pcdev, int id)
  248. {
  249. struct si3474_priv *priv = to_si3474_priv(pcdev);
  250. struct i2c_client *client;
  251. u8 chan0, chan1;
  252. s32 ret;
  253. u8 val;
  254. si3474_get_channels(priv, id, &chan0, &chan1);
  255. client = si3474_get_chan_client(priv, chan0);
  256. /* Release PI from shutdown */
  257. ret = i2c_smbus_read_byte_data(client, PORT_MODE_REG);
  258. if (ret < 0)
  259. return ret;
  260. val = (u8)ret;
  261. val |= CHAN_MASK(chan0);
  262. val |= CHAN_MASK(chan1);
  263. ret = i2c_smbus_write_byte_data(client, PORT_MODE_REG, val);
  264. if (ret)
  265. return ret;
  266. /* DETECT_CLASS_ENABLE must be set when using AUTO mode,
  267. * otherwise PI does not power up - datasheet section 2.10.2
  268. */
  269. val = CHAN_BIT(chan0) | CHAN_UPPER_BIT(chan0) |
  270. CHAN_BIT(chan1) | CHAN_UPPER_BIT(chan1);
  271. ret = i2c_smbus_write_byte_data(client, DETECT_CLASS_ENABLE_REG, val);
  272. if (ret)
  273. return ret;
  274. return 0;
  275. }
  276. static int si3474_pi_disable(struct pse_controller_dev *pcdev, int id)
  277. {
  278. struct si3474_priv *priv = to_si3474_priv(pcdev);
  279. struct i2c_client *client;
  280. u8 chan0, chan1;
  281. s32 ret;
  282. u8 val;
  283. si3474_get_channels(priv, id, &chan0, &chan1);
  284. client = si3474_get_chan_client(priv, chan0);
  285. /* Set PI in shutdown mode */
  286. ret = i2c_smbus_read_byte_data(client, PORT_MODE_REG);
  287. if (ret < 0)
  288. return ret;
  289. val = (u8)ret;
  290. val &= ~CHAN_MASK(chan0);
  291. val &= ~CHAN_MASK(chan1);
  292. ret = i2c_smbus_write_byte_data(client, PORT_MODE_REG, val);
  293. if (ret)
  294. return ret;
  295. return 0;
  296. }
  297. static int si3474_pi_get_chan_current(struct si3474_priv *priv, u8 chan)
  298. {
  299. struct i2c_client *client;
  300. u64 tmp_64;
  301. s32 ret;
  302. u8 reg;
  303. client = si3474_get_chan_client(priv, chan);
  304. /* Registers 0x30 to 0x3d */
  305. reg = CHAN_REG(PORT1_CURRENT_LSB_REG, chan);
  306. ret = i2c_smbus_read_word_data(client, reg);
  307. if (ret < 0)
  308. return ret;
  309. tmp_64 = ret * SI3474_NA_STEP;
  310. /* uA = nA / 1000 */
  311. tmp_64 = DIV_ROUND_CLOSEST_ULL(tmp_64, 1000);
  312. return (int)tmp_64;
  313. }
  314. static int si3474_pi_get_chan_voltage(struct si3474_priv *priv, u8 chan)
  315. {
  316. struct i2c_client *client;
  317. s32 ret;
  318. u32 val;
  319. u8 reg;
  320. client = si3474_get_chan_client(priv, chan);
  321. /* Registers 0x32 to 0x3f */
  322. reg = CHAN_REG(PORT1_VOLTAGE_LSB_REG, chan);
  323. ret = i2c_smbus_read_word_data(client, reg);
  324. if (ret < 0)
  325. return ret;
  326. val = ret * SI3474_UV_STEP;
  327. return (int)val;
  328. }
  329. static int si3474_pi_get_voltage(struct pse_controller_dev *pcdev, int id)
  330. {
  331. struct si3474_priv *priv = to_si3474_priv(pcdev);
  332. struct i2c_client *client;
  333. u8 chan0, chan1;
  334. s32 ret;
  335. si3474_get_channels(priv, id, &chan0, &chan1);
  336. client = si3474_get_chan_client(priv, chan0);
  337. /* Check which channels are enabled*/
  338. ret = i2c_smbus_read_byte_data(client, POWER_STATUS_REG);
  339. if (ret < 0)
  340. return ret;
  341. /* Take voltage from the first enabled channel */
  342. if (ret & CHAN_BIT(chan0))
  343. ret = si3474_pi_get_chan_voltage(priv, chan0);
  344. else if (ret & CHAN_BIT(chan1))
  345. ret = si3474_pi_get_chan_voltage(priv, chan1);
  346. else
  347. /* 'should' be no voltage in this case */
  348. return 0;
  349. return ret;
  350. }
  351. static int si3474_pi_get_actual_pw(struct pse_controller_dev *pcdev, int id)
  352. {
  353. struct si3474_priv *priv = to_si3474_priv(pcdev);
  354. u8 chan0, chan1;
  355. u32 uV, uA;
  356. u64 tmp_64;
  357. s32 ret;
  358. ret = si3474_pi_get_voltage(&priv->pcdev, id);
  359. /* Do not read currents if voltage is 0 */
  360. if (ret <= 0)
  361. return ret;
  362. uV = ret;
  363. si3474_get_channels(priv, id, &chan0, &chan1);
  364. ret = si3474_pi_get_chan_current(priv, chan0);
  365. if (ret < 0)
  366. return ret;
  367. uA = ret;
  368. ret = si3474_pi_get_chan_current(priv, chan1);
  369. if (ret < 0)
  370. return ret;
  371. uA += ret;
  372. tmp_64 = uV;
  373. tmp_64 *= uA;
  374. /* mW = uV * uA / 1000000000 */
  375. return DIV_ROUND_CLOSEST_ULL(tmp_64, 1000000000);
  376. }
  377. static const struct pse_controller_ops si3474_ops = {
  378. .setup_pi_matrix = si3474_setup_pi_matrix,
  379. .pi_enable = si3474_pi_enable,
  380. .pi_disable = si3474_pi_disable,
  381. .pi_get_actual_pw = si3474_pi_get_actual_pw,
  382. .pi_get_voltage = si3474_pi_get_voltage,
  383. .pi_get_admin_state = si3474_pi_get_admin_state,
  384. .pi_get_pw_status = si3474_pi_get_pw_status,
  385. };
  386. static void si3474_ancillary_i2c_remove(void *data)
  387. {
  388. struct i2c_client *client = data;
  389. i2c_unregister_device(client);
  390. }
  391. static int si3474_i2c_probe(struct i2c_client *client)
  392. {
  393. struct device *dev = &client->dev;
  394. struct si3474_priv *priv;
  395. u8 fw_version;
  396. s32 ret;
  397. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  398. dev_err(dev, "i2c check functionality failed\n");
  399. return -ENXIO;
  400. }
  401. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  402. if (!priv)
  403. return -ENOMEM;
  404. ret = i2c_smbus_read_byte_data(client, VENDOR_IC_ID_REG);
  405. if (ret < 0)
  406. return ret;
  407. if (ret != SI3474_DEVICE_ID) {
  408. dev_err(dev, "Wrong device ID: 0x%x\n", ret);
  409. return -ENXIO;
  410. }
  411. ret = i2c_smbus_read_byte_data(client, FIRMWARE_REVISION_REG);
  412. if (ret < 0)
  413. return ret;
  414. fw_version = ret;
  415. ret = i2c_smbus_read_byte_data(client, CHIP_REVISION_REG);
  416. if (ret < 0)
  417. return ret;
  418. dev_dbg(dev, "Chip revision: 0x%x, firmware version: 0x%x\n",
  419. ret, fw_version);
  420. priv->client[0] = client;
  421. i2c_set_clientdata(client, priv);
  422. priv->client[1] = i2c_new_ancillary_device(priv->client[0], "secondary",
  423. priv->client[0]->addr + 1);
  424. if (IS_ERR(priv->client[1]))
  425. return PTR_ERR(priv->client[1]);
  426. ret = devm_add_action_or_reset(dev, si3474_ancillary_i2c_remove, priv->client[1]);
  427. if (ret < 0) {
  428. dev_err(&priv->client[1]->dev, "Cannot register remove callback\n");
  429. return ret;
  430. }
  431. ret = i2c_smbus_read_byte_data(priv->client[1], VENDOR_IC_ID_REG);
  432. if (ret < 0) {
  433. dev_err(&priv->client[1]->dev, "Cannot access secondary PSE controller\n");
  434. return ret;
  435. }
  436. if (ret != SI3474_DEVICE_ID) {
  437. dev_err(&priv->client[1]->dev,
  438. "Wrong device ID for secondary PSE controller: 0x%x\n", ret);
  439. return -ENXIO;
  440. }
  441. priv->np = dev->of_node;
  442. priv->pcdev.owner = THIS_MODULE;
  443. priv->pcdev.ops = &si3474_ops;
  444. priv->pcdev.dev = dev;
  445. priv->pcdev.types = ETHTOOL_PSE_C33;
  446. priv->pcdev.nr_lines = SI3474_MAX_CHANS;
  447. ret = devm_pse_controller_register(dev, &priv->pcdev);
  448. if (ret) {
  449. dev_err(dev, "Failed to register PSE controller: 0x%x\n", ret);
  450. return ret;
  451. }
  452. return 0;
  453. }
  454. static const struct i2c_device_id si3474_id[] = {
  455. { "si3474" },
  456. {}
  457. };
  458. MODULE_DEVICE_TABLE(i2c, si3474_id);
  459. static const struct of_device_id si3474_of_match[] = {
  460. {
  461. .compatible = "skyworks,si3474",
  462. },
  463. {},
  464. };
  465. MODULE_DEVICE_TABLE(of, si3474_of_match);
  466. static struct i2c_driver si3474_driver = {
  467. .probe = si3474_i2c_probe,
  468. .id_table = si3474_id,
  469. .driver = {
  470. .name = "si3474",
  471. .of_match_table = si3474_of_match,
  472. },
  473. };
  474. module_i2c_driver(si3474_driver);
  475. MODULE_AUTHOR("Piotr Kubik <piotr.kubik@adtran.com>");
  476. MODULE_DESCRIPTION("Skyworks Si3474 PoE PSE Controller driver");
  477. MODULE_LICENSE("GPL");