tps23881.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Driver for the TI TPS23881 PoE PSE Controller driver (I2C bus)
  4. *
  5. * Copyright (c) 2023 Bootlin, Kory Maincent <kory.maincent@bootlin.com>
  6. */
  7. #include <linux/bitfield.h>
  8. #include <linux/delay.h>
  9. #include <linux/firmware.h>
  10. #include <linux/gpio/consumer.h>
  11. #include <linux/i2c.h>
  12. #include <linux/module.h>
  13. #include <linux/of.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/pse-pd/pse.h>
  16. #define TPS23881_MAX_CHANS 8
  17. #define TPS23881_REG_PW_STATUS 0x10
  18. #define TPS23881_REG_OP_MODE 0x12
  19. #define TPS23881_OP_MODE_SEMIAUTO 0xaaaa
  20. #define TPS23881_REG_DIS_EN 0x13
  21. #define TPS23881_REG_DET_CLA_EN 0x14
  22. #define TPS23881_REG_GEN_MASK 0x17
  23. #define TPS23881_REG_NBITACC BIT(5)
  24. #define TPS23881_REG_PW_EN 0x19
  25. #define TPS23881_REG_PORT_MAP 0x26
  26. #define TPS23881_REG_PORT_POWER 0x29
  27. #define TPS23881_REG_POEPLUS 0x40
  28. #define TPS23881_REG_TPON BIT(0)
  29. #define TPS23881_REG_FWREV 0x41
  30. #define TPS23881_REG_DEVID 0x43
  31. #define TPS23881_REG_DEVID_MASK 0xF0
  32. #define TPS23881_DEVICE_ID 0x02
  33. #define TPS23881_REG_SRAM_CTRL 0x60
  34. #define TPS23881_REG_SRAM_DATA 0x61
  35. struct tps23881_port_desc {
  36. u8 chan[2];
  37. bool is_4p;
  38. };
  39. struct tps23881_priv {
  40. struct i2c_client *client;
  41. struct pse_controller_dev pcdev;
  42. struct device_node *np;
  43. struct tps23881_port_desc port[TPS23881_MAX_CHANS];
  44. };
  45. static struct tps23881_priv *to_tps23881_priv(struct pse_controller_dev *pcdev)
  46. {
  47. return container_of(pcdev, struct tps23881_priv, pcdev);
  48. }
  49. static int tps23881_pi_enable(struct pse_controller_dev *pcdev, int id)
  50. {
  51. struct tps23881_priv *priv = to_tps23881_priv(pcdev);
  52. struct i2c_client *client = priv->client;
  53. u8 chan;
  54. u16 val;
  55. int ret;
  56. if (id >= TPS23881_MAX_CHANS)
  57. return -ERANGE;
  58. chan = priv->port[id].chan[0];
  59. if (chan < 4)
  60. val = BIT(chan);
  61. else
  62. val = BIT(chan + 4);
  63. if (priv->port[id].is_4p) {
  64. chan = priv->port[id].chan[1];
  65. if (chan < 4)
  66. val |= BIT(chan);
  67. else
  68. val |= BIT(chan + 4);
  69. }
  70. ret = i2c_smbus_write_word_data(client, TPS23881_REG_PW_EN, val);
  71. if (ret)
  72. return ret;
  73. return 0;
  74. }
  75. static int tps23881_pi_disable(struct pse_controller_dev *pcdev, int id)
  76. {
  77. struct tps23881_priv *priv = to_tps23881_priv(pcdev);
  78. struct i2c_client *client = priv->client;
  79. u8 chan;
  80. u16 val;
  81. int ret;
  82. if (id >= TPS23881_MAX_CHANS)
  83. return -ERANGE;
  84. chan = priv->port[id].chan[0];
  85. if (chan < 4)
  86. val = BIT(chan + 4);
  87. else
  88. val = BIT(chan + 8);
  89. if (priv->port[id].is_4p) {
  90. chan = priv->port[id].chan[1];
  91. if (chan < 4)
  92. val |= BIT(chan + 4);
  93. else
  94. val |= BIT(chan + 8);
  95. }
  96. ret = i2c_smbus_write_word_data(client, TPS23881_REG_PW_EN, val);
  97. if (ret)
  98. return ret;
  99. return 0;
  100. }
  101. static int tps23881_pi_is_enabled(struct pse_controller_dev *pcdev, int id)
  102. {
  103. struct tps23881_priv *priv = to_tps23881_priv(pcdev);
  104. struct i2c_client *client = priv->client;
  105. bool enabled;
  106. u8 chan;
  107. int ret;
  108. ret = i2c_smbus_read_word_data(client, TPS23881_REG_PW_STATUS);
  109. if (ret < 0)
  110. return ret;
  111. chan = priv->port[id].chan[0];
  112. if (chan < 4)
  113. enabled = ret & BIT(chan);
  114. else
  115. enabled = ret & BIT(chan + 4);
  116. if (priv->port[id].is_4p) {
  117. chan = priv->port[id].chan[1];
  118. if (chan < 4)
  119. enabled &= !!(ret & BIT(chan));
  120. else
  121. enabled &= !!(ret & BIT(chan + 4));
  122. }
  123. /* Return enabled status only if both channel are on this state */
  124. return enabled;
  125. }
  126. static int tps23881_ethtool_get_status(struct pse_controller_dev *pcdev,
  127. unsigned long id,
  128. struct netlink_ext_ack *extack,
  129. struct pse_control_status *status)
  130. {
  131. struct tps23881_priv *priv = to_tps23881_priv(pcdev);
  132. struct i2c_client *client = priv->client;
  133. bool enabled, delivering;
  134. u8 chan;
  135. int ret;
  136. ret = i2c_smbus_read_word_data(client, TPS23881_REG_PW_STATUS);
  137. if (ret < 0)
  138. return ret;
  139. chan = priv->port[id].chan[0];
  140. if (chan < 4) {
  141. enabled = ret & BIT(chan);
  142. delivering = ret & BIT(chan + 4);
  143. } else {
  144. enabled = ret & BIT(chan + 4);
  145. delivering = ret & BIT(chan + 8);
  146. }
  147. if (priv->port[id].is_4p) {
  148. chan = priv->port[id].chan[1];
  149. if (chan < 4) {
  150. enabled &= !!(ret & BIT(chan));
  151. delivering &= !!(ret & BIT(chan + 4));
  152. } else {
  153. enabled &= !!(ret & BIT(chan + 4));
  154. delivering &= !!(ret & BIT(chan + 8));
  155. }
  156. }
  157. /* Return delivering status only if both channel are on this state */
  158. if (delivering)
  159. status->c33_pw_status = ETHTOOL_C33_PSE_PW_D_STATUS_DELIVERING;
  160. else
  161. status->c33_pw_status = ETHTOOL_C33_PSE_PW_D_STATUS_DISABLED;
  162. /* Return enabled status only if both channel are on this state */
  163. if (enabled)
  164. status->c33_admin_state = ETHTOOL_C33_PSE_ADMIN_STATE_ENABLED;
  165. else
  166. status->c33_admin_state = ETHTOOL_C33_PSE_ADMIN_STATE_DISABLED;
  167. return 0;
  168. }
  169. /* Parse managers subnode into a array of device node */
  170. static int
  171. tps23881_get_of_channels(struct tps23881_priv *priv,
  172. struct device_node *chan_node[TPS23881_MAX_CHANS])
  173. {
  174. struct device_node *channels_node, *node;
  175. int i, ret;
  176. if (!priv->np)
  177. return -EINVAL;
  178. channels_node = of_find_node_by_name(priv->np, "channels");
  179. if (!channels_node)
  180. return -EINVAL;
  181. for_each_child_of_node(channels_node, node) {
  182. u32 chan_id;
  183. if (!of_node_name_eq(node, "channel"))
  184. continue;
  185. ret = of_property_read_u32(node, "reg", &chan_id);
  186. if (ret) {
  187. ret = -EINVAL;
  188. goto out;
  189. }
  190. if (chan_id >= TPS23881_MAX_CHANS || chan_node[chan_id]) {
  191. dev_err(&priv->client->dev,
  192. "wrong number of port (%d)\n", chan_id);
  193. ret = -EINVAL;
  194. goto out;
  195. }
  196. of_node_get(node);
  197. chan_node[chan_id] = node;
  198. }
  199. of_node_put(channels_node);
  200. return 0;
  201. out:
  202. for (i = 0; i < TPS23881_MAX_CHANS; i++) {
  203. of_node_put(chan_node[i]);
  204. chan_node[i] = NULL;
  205. }
  206. of_node_put(node);
  207. of_node_put(channels_node);
  208. return ret;
  209. }
  210. struct tps23881_port_matrix {
  211. u8 pi_id;
  212. u8 lgcl_chan[2];
  213. u8 hw_chan[2];
  214. bool is_4p;
  215. bool exist;
  216. };
  217. static int
  218. tps23881_match_channel(const struct pse_pi_pairset *pairset,
  219. struct device_node *chan_node[TPS23881_MAX_CHANS])
  220. {
  221. int i;
  222. /* Look on every channels */
  223. for (i = 0; i < TPS23881_MAX_CHANS; i++) {
  224. if (pairset->np == chan_node[i])
  225. return i;
  226. }
  227. return -ENODEV;
  228. }
  229. static bool
  230. tps23881_is_chan_free(struct tps23881_port_matrix port_matrix[TPS23881_MAX_CHANS],
  231. int chan)
  232. {
  233. int i;
  234. for (i = 0; i < TPS23881_MAX_CHANS; i++) {
  235. if (port_matrix[i].exist &&
  236. (port_matrix[i].hw_chan[0] == chan ||
  237. port_matrix[i].hw_chan[1] == chan))
  238. return false;
  239. }
  240. return true;
  241. }
  242. /* Fill port matrix with the matching channels */
  243. static int
  244. tps23881_match_port_matrix(struct pse_pi *pi, int pi_id,
  245. struct device_node *chan_node[TPS23881_MAX_CHANS],
  246. struct tps23881_port_matrix port_matrix[TPS23881_MAX_CHANS])
  247. {
  248. int ret;
  249. if (!pi->pairset[0].np)
  250. return 0;
  251. ret = tps23881_match_channel(&pi->pairset[0], chan_node);
  252. if (ret < 0)
  253. return ret;
  254. if (!tps23881_is_chan_free(port_matrix, ret)) {
  255. pr_err("tps23881: channel %d already used\n", ret);
  256. return -ENODEV;
  257. }
  258. port_matrix[pi_id].hw_chan[0] = ret;
  259. port_matrix[pi_id].exist = true;
  260. if (!pi->pairset[1].np)
  261. return 0;
  262. ret = tps23881_match_channel(&pi->pairset[1], chan_node);
  263. if (ret < 0)
  264. return ret;
  265. if (!tps23881_is_chan_free(port_matrix, ret)) {
  266. pr_err("tps23881: channel %d already used\n", ret);
  267. return -ENODEV;
  268. }
  269. if (port_matrix[pi_id].hw_chan[0] / 4 != ret / 4) {
  270. pr_err("tps23881: 4-pair PSE can only be set within the same 4 ports group");
  271. return -ENODEV;
  272. }
  273. port_matrix[pi_id].hw_chan[1] = ret;
  274. port_matrix[pi_id].is_4p = true;
  275. return 0;
  276. }
  277. static int
  278. tps23881_get_unused_chan(struct tps23881_port_matrix port_matrix[TPS23881_MAX_CHANS],
  279. int port_cnt)
  280. {
  281. bool used;
  282. int i, j;
  283. for (i = 0; i < TPS23881_MAX_CHANS; i++) {
  284. used = false;
  285. for (j = 0; j < port_cnt; j++) {
  286. if (port_matrix[j].hw_chan[0] == i) {
  287. used = true;
  288. break;
  289. }
  290. if (port_matrix[j].is_4p &&
  291. port_matrix[j].hw_chan[1] == i) {
  292. used = true;
  293. break;
  294. }
  295. }
  296. if (!used)
  297. return i;
  298. }
  299. return -ENODEV;
  300. }
  301. /* Sort the port matrix to following particular hardware ports matrix
  302. * specification of the tps23881. The device has two 4-ports groups and
  303. * each 4-pair powered device has to be configured to use two consecutive
  304. * logical channel in each 4 ports group (1 and 2 or 3 and 4). Also the
  305. * hardware matrix has to be fully configured even with unused chan to be
  306. * valid.
  307. */
  308. static int
  309. tps23881_sort_port_matrix(struct tps23881_port_matrix port_matrix[TPS23881_MAX_CHANS])
  310. {
  311. struct tps23881_port_matrix tmp_port_matrix[TPS23881_MAX_CHANS] = {0};
  312. int i, ret, port_cnt = 0, cnt_4ch_grp1 = 0, cnt_4ch_grp2 = 4;
  313. /* Configure 4p port matrix */
  314. for (i = 0; i < TPS23881_MAX_CHANS; i++) {
  315. int *cnt;
  316. if (!port_matrix[i].exist || !port_matrix[i].is_4p)
  317. continue;
  318. if (port_matrix[i].hw_chan[0] < 4)
  319. cnt = &cnt_4ch_grp1;
  320. else
  321. cnt = &cnt_4ch_grp2;
  322. tmp_port_matrix[port_cnt].exist = true;
  323. tmp_port_matrix[port_cnt].is_4p = true;
  324. tmp_port_matrix[port_cnt].pi_id = i;
  325. tmp_port_matrix[port_cnt].hw_chan[0] = port_matrix[i].hw_chan[0];
  326. tmp_port_matrix[port_cnt].hw_chan[1] = port_matrix[i].hw_chan[1];
  327. /* 4-pair ports have to be configured with consecutive
  328. * logical channels 0 and 1, 2 and 3.
  329. */
  330. tmp_port_matrix[port_cnt].lgcl_chan[0] = (*cnt)++;
  331. tmp_port_matrix[port_cnt].lgcl_chan[1] = (*cnt)++;
  332. port_cnt++;
  333. }
  334. /* Configure 2p port matrix */
  335. for (i = 0; i < TPS23881_MAX_CHANS; i++) {
  336. int *cnt;
  337. if (!port_matrix[i].exist || port_matrix[i].is_4p)
  338. continue;
  339. if (port_matrix[i].hw_chan[0] < 4)
  340. cnt = &cnt_4ch_grp1;
  341. else
  342. cnt = &cnt_4ch_grp2;
  343. tmp_port_matrix[port_cnt].exist = true;
  344. tmp_port_matrix[port_cnt].pi_id = i;
  345. tmp_port_matrix[port_cnt].lgcl_chan[0] = (*cnt)++;
  346. tmp_port_matrix[port_cnt].hw_chan[0] = port_matrix[i].hw_chan[0];
  347. port_cnt++;
  348. }
  349. /* Complete the rest of the first 4 port group matrix even if
  350. * channels are unused
  351. */
  352. while (cnt_4ch_grp1 < 4) {
  353. ret = tps23881_get_unused_chan(tmp_port_matrix, port_cnt);
  354. if (ret < 0) {
  355. pr_err("tps23881: port matrix issue, no chan available\n");
  356. return ret;
  357. }
  358. if (port_cnt >= TPS23881_MAX_CHANS) {
  359. pr_err("tps23881: wrong number of channels\n");
  360. return -ENODEV;
  361. }
  362. tmp_port_matrix[port_cnt].lgcl_chan[0] = cnt_4ch_grp1;
  363. tmp_port_matrix[port_cnt].hw_chan[0] = ret;
  364. cnt_4ch_grp1++;
  365. port_cnt++;
  366. }
  367. /* Complete the rest of the second 4 port group matrix even if
  368. * channels are unused
  369. */
  370. while (cnt_4ch_grp2 < 8) {
  371. ret = tps23881_get_unused_chan(tmp_port_matrix, port_cnt);
  372. if (ret < 0) {
  373. pr_err("tps23881: port matrix issue, no chan available\n");
  374. return -ENODEV;
  375. }
  376. if (port_cnt >= TPS23881_MAX_CHANS) {
  377. pr_err("tps23881: wrong number of channels\n");
  378. return -ENODEV;
  379. }
  380. tmp_port_matrix[port_cnt].lgcl_chan[0] = cnt_4ch_grp2;
  381. tmp_port_matrix[port_cnt].hw_chan[0] = ret;
  382. cnt_4ch_grp2++;
  383. port_cnt++;
  384. }
  385. memcpy(port_matrix, tmp_port_matrix, sizeof(tmp_port_matrix));
  386. return port_cnt;
  387. }
  388. /* Write port matrix to the hardware port matrix and the software port
  389. * matrix.
  390. */
  391. static int
  392. tps23881_write_port_matrix(struct tps23881_priv *priv,
  393. struct tps23881_port_matrix port_matrix[TPS23881_MAX_CHANS],
  394. int port_cnt)
  395. {
  396. struct i2c_client *client = priv->client;
  397. u8 pi_id, lgcl_chan, hw_chan;
  398. u16 val = 0;
  399. int i, ret;
  400. for (i = 0; i < port_cnt; i++) {
  401. pi_id = port_matrix[i].pi_id;
  402. lgcl_chan = port_matrix[i].lgcl_chan[0];
  403. hw_chan = port_matrix[i].hw_chan[0] % 4;
  404. /* Set software port matrix for existing ports */
  405. if (port_matrix[i].exist)
  406. priv->port[pi_id].chan[0] = lgcl_chan;
  407. /* Set hardware port matrix for all ports */
  408. val |= hw_chan << (lgcl_chan * 2);
  409. if (!port_matrix[i].is_4p)
  410. continue;
  411. lgcl_chan = port_matrix[i].lgcl_chan[1];
  412. hw_chan = port_matrix[i].hw_chan[1] % 4;
  413. /* Set software port matrix for existing ports */
  414. if (port_matrix[i].exist) {
  415. priv->port[pi_id].is_4p = true;
  416. priv->port[pi_id].chan[1] = lgcl_chan;
  417. }
  418. /* Set hardware port matrix for all ports */
  419. val |= hw_chan << (lgcl_chan * 2);
  420. }
  421. /* Write hardware ports matrix */
  422. ret = i2c_smbus_write_word_data(client, TPS23881_REG_PORT_MAP, val);
  423. if (ret)
  424. return ret;
  425. return 0;
  426. }
  427. static int
  428. tps23881_set_ports_conf(struct tps23881_priv *priv,
  429. struct tps23881_port_matrix port_matrix[TPS23881_MAX_CHANS])
  430. {
  431. struct i2c_client *client = priv->client;
  432. int i, ret;
  433. u16 val;
  434. /* Set operating mode */
  435. ret = i2c_smbus_write_word_data(client, TPS23881_REG_OP_MODE,
  436. TPS23881_OP_MODE_SEMIAUTO);
  437. if (ret)
  438. return ret;
  439. /* Disable DC disconnect */
  440. ret = i2c_smbus_write_word_data(client, TPS23881_REG_DIS_EN, 0x0);
  441. if (ret)
  442. return ret;
  443. /* Set port power allocation */
  444. val = 0;
  445. for (i = 0; i < TPS23881_MAX_CHANS; i++) {
  446. if (!port_matrix[i].exist)
  447. continue;
  448. if (port_matrix[i].is_4p)
  449. val |= 0xf << ((port_matrix[i].lgcl_chan[0] / 2) * 4);
  450. else
  451. val |= 0x3 << ((port_matrix[i].lgcl_chan[0] / 2) * 4);
  452. }
  453. ret = i2c_smbus_write_word_data(client, TPS23881_REG_PORT_POWER, val);
  454. if (ret)
  455. return ret;
  456. /* Enable detection and classification */
  457. val = 0;
  458. for (i = 0; i < TPS23881_MAX_CHANS; i++) {
  459. if (!port_matrix[i].exist)
  460. continue;
  461. val |= BIT(port_matrix[i].lgcl_chan[0]) |
  462. BIT(port_matrix[i].lgcl_chan[0] + 4);
  463. if (port_matrix[i].is_4p)
  464. val |= BIT(port_matrix[i].lgcl_chan[1]) |
  465. BIT(port_matrix[i].lgcl_chan[1] + 4);
  466. }
  467. ret = i2c_smbus_write_word_data(client, TPS23881_REG_DET_CLA_EN, val);
  468. if (ret)
  469. return ret;
  470. return 0;
  471. }
  472. static int
  473. tps23881_set_ports_matrix(struct tps23881_priv *priv,
  474. struct device_node *chan_node[TPS23881_MAX_CHANS])
  475. {
  476. struct tps23881_port_matrix port_matrix[TPS23881_MAX_CHANS] = {0};
  477. int i, ret;
  478. /* Update with values for every PSE PIs */
  479. for (i = 0; i < TPS23881_MAX_CHANS; i++) {
  480. ret = tps23881_match_port_matrix(&priv->pcdev.pi[i], i,
  481. chan_node, port_matrix);
  482. if (ret)
  483. return ret;
  484. }
  485. ret = tps23881_sort_port_matrix(port_matrix);
  486. if (ret < 0)
  487. return ret;
  488. ret = tps23881_write_port_matrix(priv, port_matrix, ret);
  489. if (ret)
  490. return ret;
  491. ret = tps23881_set_ports_conf(priv, port_matrix);
  492. if (ret)
  493. return ret;
  494. return 0;
  495. }
  496. static int tps23881_setup_pi_matrix(struct pse_controller_dev *pcdev)
  497. {
  498. struct device_node *chan_node[TPS23881_MAX_CHANS] = {NULL};
  499. struct tps23881_priv *priv = to_tps23881_priv(pcdev);
  500. int ret, i;
  501. ret = tps23881_get_of_channels(priv, chan_node);
  502. if (ret < 0) {
  503. dev_warn(&priv->client->dev,
  504. "Unable to parse port-matrix, default matrix will be used\n");
  505. return 0;
  506. }
  507. ret = tps23881_set_ports_matrix(priv, chan_node);
  508. for (i = 0; i < TPS23881_MAX_CHANS; i++)
  509. of_node_put(chan_node[i]);
  510. return ret;
  511. }
  512. static const struct pse_controller_ops tps23881_ops = {
  513. .setup_pi_matrix = tps23881_setup_pi_matrix,
  514. .pi_enable = tps23881_pi_enable,
  515. .pi_disable = tps23881_pi_disable,
  516. .pi_is_enabled = tps23881_pi_is_enabled,
  517. .ethtool_get_status = tps23881_ethtool_get_status,
  518. };
  519. static const char fw_parity_name[] = "ti/tps23881/tps23881-parity-14.bin";
  520. static const char fw_sram_name[] = "ti/tps23881/tps23881-sram-14.bin";
  521. struct tps23881_fw_conf {
  522. u8 reg;
  523. u8 val;
  524. };
  525. static const struct tps23881_fw_conf tps23881_fw_parity_conf[] = {
  526. {.reg = 0x60, .val = 0x01},
  527. {.reg = 0x62, .val = 0x00},
  528. {.reg = 0x63, .val = 0x80},
  529. {.reg = 0x60, .val = 0xC4},
  530. {.reg = 0x1D, .val = 0xBC},
  531. {.reg = 0xD7, .val = 0x02},
  532. {.reg = 0x91, .val = 0x00},
  533. {.reg = 0x90, .val = 0x00},
  534. {.reg = 0xD7, .val = 0x00},
  535. {.reg = 0x1D, .val = 0x00},
  536. { /* sentinel */ }
  537. };
  538. static const struct tps23881_fw_conf tps23881_fw_sram_conf[] = {
  539. {.reg = 0x60, .val = 0xC5},
  540. {.reg = 0x62, .val = 0x00},
  541. {.reg = 0x63, .val = 0x80},
  542. {.reg = 0x60, .val = 0xC0},
  543. {.reg = 0x1D, .val = 0xBC},
  544. {.reg = 0xD7, .val = 0x02},
  545. {.reg = 0x91, .val = 0x00},
  546. {.reg = 0x90, .val = 0x00},
  547. {.reg = 0xD7, .val = 0x00},
  548. {.reg = 0x1D, .val = 0x00},
  549. { /* sentinel */ }
  550. };
  551. static int tps23881_flash_sram_fw_part(struct i2c_client *client,
  552. const char *fw_name,
  553. const struct tps23881_fw_conf *fw_conf)
  554. {
  555. const struct firmware *fw = NULL;
  556. int i, ret;
  557. ret = request_firmware(&fw, fw_name, &client->dev);
  558. if (ret)
  559. return ret;
  560. dev_dbg(&client->dev, "Flashing %s\n", fw_name);
  561. /* Prepare device for RAM download */
  562. while (fw_conf->reg) {
  563. ret = i2c_smbus_write_byte_data(client, fw_conf->reg,
  564. fw_conf->val);
  565. if (ret)
  566. goto out;
  567. fw_conf++;
  568. }
  569. /* Flash the firmware file */
  570. for (i = 0; i < fw->size; i++) {
  571. ret = i2c_smbus_write_byte_data(client,
  572. TPS23881_REG_SRAM_DATA,
  573. fw->data[i]);
  574. if (ret)
  575. goto out;
  576. }
  577. out:
  578. release_firmware(fw);
  579. return ret;
  580. }
  581. static int tps23881_flash_sram_fw(struct i2c_client *client)
  582. {
  583. int ret;
  584. ret = tps23881_flash_sram_fw_part(client, fw_parity_name,
  585. tps23881_fw_parity_conf);
  586. if (ret)
  587. return ret;
  588. ret = tps23881_flash_sram_fw_part(client, fw_sram_name,
  589. tps23881_fw_sram_conf);
  590. if (ret)
  591. return ret;
  592. ret = i2c_smbus_write_byte_data(client, TPS23881_REG_SRAM_CTRL, 0x18);
  593. if (ret)
  594. return ret;
  595. mdelay(12);
  596. return 0;
  597. }
  598. static int tps23881_i2c_probe(struct i2c_client *client)
  599. {
  600. struct device *dev = &client->dev;
  601. struct tps23881_priv *priv;
  602. struct gpio_desc *reset;
  603. int ret;
  604. u8 val;
  605. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  606. dev_err(dev, "i2c check functionality failed\n");
  607. return -ENXIO;
  608. }
  609. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  610. if (!priv)
  611. return -ENOMEM;
  612. reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
  613. if (IS_ERR(reset))
  614. return dev_err_probe(&client->dev, PTR_ERR(reset), "Failed to get reset GPIO\n");
  615. if (reset) {
  616. /* TPS23880 datasheet (Rev G) indicates minimum reset pulse is 5us */
  617. usleep_range(5, 10);
  618. gpiod_set_value_cansleep(reset, 0); /* De-assert reset */
  619. /* TPS23880 datasheet indicates the minimum time after power on reset
  620. * should be 20ms, but the document describing how to load SRAM ("How
  621. * to Load TPS2388x SRAM and Parity Code over I2C" (Rev E))
  622. * indicates we should delay that programming by at least 50ms. So
  623. * we'll wait the entire 50ms here to ensure we're safe to go to the
  624. * SRAM loading proceedure.
  625. */
  626. msleep(50);
  627. }
  628. ret = i2c_smbus_read_byte_data(client, TPS23881_REG_DEVID);
  629. if (ret < 0)
  630. return ret;
  631. if (FIELD_GET(TPS23881_REG_DEVID_MASK, ret) != TPS23881_DEVICE_ID) {
  632. dev_err(dev, "Wrong device ID\n");
  633. return -ENXIO;
  634. }
  635. ret = tps23881_flash_sram_fw(client);
  636. if (ret < 0)
  637. return ret;
  638. ret = i2c_smbus_read_byte_data(client, TPS23881_REG_FWREV);
  639. if (ret < 0)
  640. return ret;
  641. dev_info(&client->dev, "Firmware revision 0x%x\n", ret);
  642. /* Set configuration B, 16 bit access on a single device address */
  643. ret = i2c_smbus_read_byte_data(client, TPS23881_REG_GEN_MASK);
  644. if (ret < 0)
  645. return ret;
  646. val = ret | TPS23881_REG_NBITACC;
  647. ret = i2c_smbus_write_byte_data(client, TPS23881_REG_GEN_MASK, val);
  648. if (ret)
  649. return ret;
  650. priv->client = client;
  651. i2c_set_clientdata(client, priv);
  652. priv->np = dev->of_node;
  653. priv->pcdev.owner = THIS_MODULE;
  654. priv->pcdev.ops = &tps23881_ops;
  655. priv->pcdev.dev = dev;
  656. priv->pcdev.types = ETHTOOL_PSE_C33;
  657. priv->pcdev.nr_lines = TPS23881_MAX_CHANS;
  658. ret = devm_pse_controller_register(dev, &priv->pcdev);
  659. if (ret) {
  660. return dev_err_probe(dev, ret,
  661. "failed to register PSE controller\n");
  662. }
  663. return ret;
  664. }
  665. static const struct i2c_device_id tps23881_id[] = {
  666. { "tps23881" },
  667. { }
  668. };
  669. MODULE_DEVICE_TABLE(i2c, tps23881_id);
  670. static const struct of_device_id tps23881_of_match[] = {
  671. { .compatible = "ti,tps23881", },
  672. { },
  673. };
  674. MODULE_DEVICE_TABLE(of, tps23881_of_match);
  675. static struct i2c_driver tps23881_driver = {
  676. .probe = tps23881_i2c_probe,
  677. .id_table = tps23881_id,
  678. .driver = {
  679. .name = "tps23881",
  680. .of_match_table = tps23881_of_match,
  681. },
  682. };
  683. module_i2c_driver(tps23881_driver);
  684. MODULE_AUTHOR("Kory Maincent <kory.maincent@bootlin.com>");
  685. MODULE_DESCRIPTION("TI TPS23881 PoE PSE Controller driver");
  686. MODULE_LICENSE("GPL");