1 // SPDX-License-Identifier: GPL-2.0-only
3 * Driver for the TI TPS23881 PoE PSE Controller driver (I2C bus)
8 #include <linux/bitfield.h>
9 #include <linux/delay.h>
10 #include <linux/firmware.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/i2c.h>
13 #include <linux/module.h>
15 #include <linux/platform_device.h>
16 #include <linux/pse-pd/pse.h>
18 #define TPS23881_MAX_CHANS 8
20 #define TPS23881_REG_PW_STATUS 0x10
21 #define TPS23881_REG_OP_MODE 0x12
22 #define TPS23881_OP_MODE_SEMIAUTO 0xaaaa
23 #define TPS23881_REG_DIS_EN 0x13
24 #define TPS23881_REG_DET_CLA_EN 0x14
25 #define TPS23881_REG_GEN_MASK 0x17
26 #define TPS23881_REG_NBITACC BIT(5)
27 #define TPS23881_REG_PW_EN 0x19
28 #define TPS23881_REG_2PAIR_POL1 0x1e
29 #define TPS23881_REG_PORT_MAP 0x26
30 #define TPS23881_REG_PORT_POWER 0x29
31 #define TPS23881_REG_4PAIR_POL1 0x2a
32 #define TPS23881_REG_INPUT_V 0x2e
33 #define TPS23881_REG_CHAN1_A 0x30
34 #define TPS23881_REG_CHAN1_V 0x32
35 #define TPS23881_REG_FOLDBACK 0x40
36 #define TPS23881_REG_TPON BIT(0)
37 #define TPS23881_REG_FWREV 0x41
38 #define TPS23881_REG_DEVID 0x43
39 #define TPS23881_REG_DEVID_MASK 0xF0
40 #define TPS23881_DEVICE_ID 0x02
41 #define TPS23881_REG_CHAN1_CLASS 0x4c
42 #define TPS23881_REG_SRAM_CTRL 0x60
43 #define TPS23881_REG_SRAM_DATA 0x61
45 #define TPS23881_UV_STEP 3662
46 #define TPS23881_NA_STEP 70190
47 #define TPS23881_MW_STEP 500
48 #define TPS23881_MIN_PI_PW_LIMIT_MW 2000
50 struct tps23881_port_desc {
56 struct tps23881_priv {
57 struct i2c_client *client;
58 struct pse_controller_dev pcdev;
59 struct device_node *np;
60 struct tps23881_port_desc port[TPS23881_MAX_CHANS];
63 static struct tps23881_priv *to_tps23881_priv(struct pse_controller_dev *pcdev)
65 return container_of(pcdev, struct tps23881_priv, pcdev);
69 * Helper to extract a value from a u16 register value, which is made of two
70 * u8 registers. The function calculates the bit offset based on the channel
71 * and extracts the relevant bits using a provided field mask.
73 * @param reg_val: The u16 register value (composed of two u8 registers).
74 * @param chan: The channel number (0-7).
75 * @param field_offset: The base bit offset to apply (e.g., 0 or 4).
76 * @param field_mask: The mask to apply to extract the required bits.
77 * @return: The extracted value for the specific channel.
79 static u16 tps23881_calc_val(u16 reg_val, u8 chan, u8 field_offset,
85 return (reg_val >> field_offset) & field_mask;
89 * Helper to combine individual channel values into a u16 register value.
90 * The function sets the value for a specific channel in the appropriate
93 * @param reg_val: The current u16 register value.
94 * @param chan: The channel number (0-7).
95 * @param field_offset: The base bit offset to apply (e.g., 0 or 4).
96 * @param field_mask: The mask to apply for the field (e.g., 0x0F).
97 * @param field_val: The value to set for the specific channel (masked by
99 * @return: The updated u16 register value with the channel value set.
101 static u16 tps23881_set_val(u16 reg_val, u8 chan, u8 field_offset,
102 u16 field_mask, u16 field_val)
104 field_val &= field_mask;
107 reg_val &= ~(field_mask << field_offset);
108 reg_val |= (field_val << field_offset);
110 reg_val &= ~(field_mask << (field_offset + 8));
111 reg_val |= (field_val << (field_offset + 8));
118 tps23881_pi_set_pw_pol_limit(struct tps23881_priv *priv, int id, u8 pw_pol,
121 struct i2c_client *client = priv->client;
126 chan = priv->port[id].chan[0];
128 reg = TPS23881_REG_2PAIR_POL1 + (chan % 4);
130 /* One chan is enough to configure the 4p PI power limit */
132 reg = TPS23881_REG_4PAIR_POL1;
134 reg = TPS23881_REG_4PAIR_POL1 + 1;
137 ret = i2c_smbus_read_word_data(client, reg);
141 val = tps23881_set_val(ret, chan, 0, 0xff, pw_pol);
142 return i2c_smbus_write_word_data(client, reg, val);
145 static int tps23881_pi_enable_manual_pol(struct tps23881_priv *priv, int id)
147 struct i2c_client *client = priv->client;
152 ret = i2c_smbus_read_byte_data(client, TPS23881_REG_FOLDBACK);
156 /* No need to test if the chan is PoE4 as setting either bit for a
157 * 4P configured port disables the automatic configuration on both
160 chan = priv->port[id].chan[0];
161 val = tps23881_set_val(ret, chan, 0, BIT(chan % 4), BIT(chan % 4));
162 return i2c_smbus_write_byte_data(client, TPS23881_REG_FOLDBACK, val);
165 static int tps23881_pi_enable(struct pse_controller_dev *pcdev, int id)
167 struct tps23881_priv *priv = to_tps23881_priv(pcdev);
168 struct i2c_client *client = priv->client;
172 if (id >= TPS23881_MAX_CHANS)
175 chan = priv->port[id].chan[0];
176 val = tps23881_set_val(0, chan, 0, BIT(chan % 4), BIT(chan % 4));
178 if (priv->port[id].is_4p) {
179 chan = priv->port[id].chan[1];
180 val = tps23881_set_val(val, chan, 0, BIT(chan % 4),
184 return i2c_smbus_write_word_data(client, TPS23881_REG_PW_EN, val);
187 static int tps23881_pi_disable(struct pse_controller_dev *pcdev, int id)
189 struct tps23881_priv *priv = to_tps23881_priv(pcdev);
190 struct i2c_client *client = priv->client;
195 if (id >= TPS23881_MAX_CHANS)
198 chan = priv->port[id].chan[0];
199 val = tps23881_set_val(0, chan, 4, BIT(chan % 4), BIT(chan % 4));
201 if (priv->port[id].is_4p) {
202 chan = priv->port[id].chan[1];
203 val = tps23881_set_val(val, chan, 4, BIT(chan % 4),
207 ret = i2c_smbus_write_word_data(client, TPS23881_REG_PW_EN, val);
211 /* PWOFF command resets lots of register which need to be
212 * configured again. According to the datasheet "It may take upwards
213 * of 5ms after PWOFFn command for all register values to be updated"
217 /* Enable detection and classification */
218 ret = i2c_smbus_read_word_data(client, TPS23881_REG_DET_CLA_EN);
222 chan = priv->port[id].chan[0];
223 val = tps23881_set_val(ret, chan, 0, BIT(chan % 4), BIT(chan % 4));
224 val = tps23881_set_val(val, chan, 4, BIT(chan % 4), BIT(chan % 4));
226 if (priv->port[id].is_4p) {
227 chan = priv->port[id].chan[1];
228 val = tps23881_set_val(ret, chan, 0, BIT(chan % 4),
230 val = tps23881_set_val(val, chan, 4, BIT(chan % 4),
234 ret = i2c_smbus_write_word_data(client, TPS23881_REG_DET_CLA_EN, val);
238 /* No power policy */
239 if (priv->port[id].pw_pol < 0)
242 ret = tps23881_pi_enable_manual_pol(priv, id);
246 /* Set power policy */
247 return tps23881_pi_set_pw_pol_limit(priv, id, priv->port[id].pw_pol,
248 priv->port[id].is_4p);
252 tps23881_pi_get_admin_state(struct pse_controller_dev *pcdev, int id,
253 struct pse_admin_state *admin_state)
255 struct tps23881_priv *priv = to_tps23881_priv(pcdev);
256 struct i2c_client *client = priv->client;
262 ret = i2c_smbus_read_word_data(client, TPS23881_REG_PW_STATUS);
266 chan = priv->port[id].chan[0];
267 val = tps23881_calc_val(ret, chan, 0, BIT(chan % 4));
270 if (priv->port[id].is_4p) {
271 chan = priv->port[id].chan[1];
272 val = tps23881_calc_val(ret, chan, 0, BIT(chan % 4));
276 /* Return enabled status only if both channel are on this state */
278 admin_state->c33_admin_state =
279 ETHTOOL_C33_PSE_ADMIN_STATE_ENABLED;
281 admin_state->c33_admin_state =
282 ETHTOOL_C33_PSE_ADMIN_STATE_DISABLED;
288 tps23881_pi_get_pw_status(struct pse_controller_dev *pcdev, int id,
289 struct pse_pw_status *pw_status)
291 struct tps23881_priv *priv = to_tps23881_priv(pcdev);
292 struct i2c_client *client = priv->client;
298 ret = i2c_smbus_read_word_data(client, TPS23881_REG_PW_STATUS);
302 chan = priv->port[id].chan[0];
303 val = tps23881_calc_val(ret, chan, 4, BIT(chan % 4));
304 delivering = !!(val);
306 if (priv->port[id].is_4p) {
307 chan = priv->port[id].chan[1];
308 val = tps23881_calc_val(ret, chan, 4, BIT(chan % 4));
309 delivering &= !!(val);
312 /* Return delivering status only if both channel are on this state */
314 pw_status->c33_pw_status =
315 ETHTOOL_C33_PSE_PW_D_STATUS_DELIVERING;
317 pw_status->c33_pw_status =
318 ETHTOOL_C33_PSE_PW_D_STATUS_DISABLED;
323 static int tps23881_pi_get_voltage(struct pse_controller_dev *pcdev, int id)
325 struct tps23881_priv *priv = to_tps23881_priv(pcdev);
326 struct i2c_client *client = priv->client;
330 ret = i2c_smbus_read_word_data(client, TPS23881_REG_INPUT_V);
335 uV *= TPS23881_UV_STEP;
341 tps23881_pi_get_chan_current(struct tps23881_priv *priv, u8 chan)
343 struct i2c_client *client = priv->client;
347 /* Registers 0x30 to 0x3d */
348 reg = TPS23881_REG_CHAN1_A + (chan % 4) * 4 + (chan >= 4);
349 ret = i2c_smbus_read_word_data(client, reg);
353 tmp_64 = ret & 0x3fff;
354 tmp_64 *= TPS23881_NA_STEP;
356 tmp_64 = DIV_ROUND_CLOSEST_ULL(tmp_64, 1000);
360 static int tps23881_pi_get_pw_class(struct pse_controller_dev *pcdev,
363 struct tps23881_priv *priv = to_tps23881_priv(pcdev);
364 struct i2c_client *client = priv->client;
368 chan = priv->port[id].chan[0];
369 reg = TPS23881_REG_CHAN1_CLASS + (chan % 4);
370 ret = i2c_smbus_read_word_data(client, reg);
374 return tps23881_calc_val(ret, chan, 4, 0x0f);
378 tps23881_pi_get_actual_pw(struct pse_controller_dev *pcdev, int id)
380 struct tps23881_priv *priv = to_tps23881_priv(pcdev);
385 ret = tps23881_pi_get_voltage(&priv->pcdev, id);
390 chan = priv->port[id].chan[0];
391 ret = tps23881_pi_get_chan_current(priv, chan);
396 if (priv->port[id].is_4p) {
397 chan = priv->port[id].chan[1];
398 ret = tps23881_pi_get_chan_current(priv, chan);
406 /* mW = uV * uA / 1000000000 */
407 return DIV_ROUND_CLOSEST_ULL(tmp_64, 1000000000);
411 tps23881_pi_get_pw_limit_chan(struct tps23881_priv *priv, u8 chan)
413 struct i2c_client *client = priv->client;
417 reg = TPS23881_REG_2PAIR_POL1 + (chan % 4);
418 ret = i2c_smbus_read_word_data(client, reg);
422 val = tps23881_calc_val(ret, chan, 0, 0xff);
423 return val * TPS23881_MW_STEP;
426 static int tps23881_pi_get_pw_limit(struct pse_controller_dev *pcdev, int id)
428 struct tps23881_priv *priv = to_tps23881_priv(pcdev);
432 chan = priv->port[id].chan[0];
433 ret = tps23881_pi_get_pw_limit_chan(priv, chan);
438 if (priv->port[id].is_4p) {
439 chan = priv->port[id].chan[1];
440 ret = tps23881_pi_get_pw_limit_chan(priv, chan);
449 static int tps23881_pi_set_pw_limit(struct pse_controller_dev *pcdev,
452 struct tps23881_priv *priv = to_tps23881_priv(pcdev);
456 if (max_mW < TPS23881_MIN_PI_PW_LIMIT_MW || MAX_PI_PW < max_mW) {
457 dev_err(&priv->client->dev,
458 "power limit %d out of ranges [%d,%d]",
459 max_mW, TPS23881_MIN_PI_PW_LIMIT_MW, MAX_PI_PW);
463 ret = tps23881_pi_enable_manual_pol(priv, id);
467 pw_pol = DIV_ROUND_CLOSEST_ULL(max_mW, TPS23881_MW_STEP);
469 /* Save power policy to reconfigure it after a disabled call */
470 priv->port[id].pw_pol = pw_pol;
471 return tps23881_pi_set_pw_pol_limit(priv, id, pw_pol,
472 priv->port[id].is_4p);
476 tps23881_pi_get_pw_limit_ranges(struct pse_controller_dev *pcdev, int id,
477 struct pse_pw_limit_ranges *pw_limit_ranges)
479 struct ethtool_c33_pse_pw_limit_range *c33_pw_limit_ranges;
481 c33_pw_limit_ranges = kzalloc(sizeof(*c33_pw_limit_ranges),
483 if (!c33_pw_limit_ranges)
486 c33_pw_limit_ranges->min = TPS23881_MIN_PI_PW_LIMIT_MW;
487 c33_pw_limit_ranges->max = MAX_PI_PW;
488 pw_limit_ranges->c33_pw_limit_ranges = c33_pw_limit_ranges;
490 /* Return the number of ranges */
494 /* Parse managers subnode into a array of device node */
496 tps23881_get_of_channels(struct tps23881_priv *priv,
497 struct device_node *chan_node[TPS23881_MAX_CHANS])
499 struct device_node *channels_node, *node;
505 channels_node = of_find_node_by_name(priv->np, "channels");
509 for_each_child_of_node(channels_node, node) {
512 if (!of_node_name_eq(node, "channel"))
515 ret = of_property_read_u32(node, "reg", &chan_id);
521 if (chan_id >= TPS23881_MAX_CHANS || chan_node[chan_id]) {
522 dev_err(&priv->client->dev,
523 "wrong number of port (%d)\n", chan_id);
529 chan_node[chan_id] = node;
532 of_node_put(channels_node);
536 for (i = 0; i < TPS23881_MAX_CHANS; i++) {
537 of_node_put(chan_node[i]);
542 of_node_put(channels_node);
546 struct tps23881_port_matrix {
555 tps23881_match_channel(const struct pse_pi_pairset *pairset,
556 struct device_node *chan_node[TPS23881_MAX_CHANS])
560 /* Look on every channels */
561 for (i = 0; i < TPS23881_MAX_CHANS; i++) {
562 if (pairset->np == chan_node[i])
570 tps23881_is_chan_free(struct tps23881_port_matrix port_matrix[TPS23881_MAX_CHANS],
575 for (i = 0; i < TPS23881_MAX_CHANS; i++) {
576 if (port_matrix[i].exist &&
577 (port_matrix[i].hw_chan[0] == chan ||
578 port_matrix[i].hw_chan[1] == chan))
585 /* Fill port matrix with the matching channels */
587 tps23881_match_port_matrix(struct pse_pi *pi, int pi_id,
588 struct device_node *chan_node[TPS23881_MAX_CHANS],
589 struct tps23881_port_matrix port_matrix[TPS23881_MAX_CHANS])
593 if (!pi->pairset[0].np)
596 ret = tps23881_match_channel(&pi->pairset[0], chan_node);
600 if (!tps23881_is_chan_free(port_matrix, ret)) {
601 pr_err("tps23881: channel %d already used\n", ret);
605 port_matrix[pi_id].hw_chan[0] = ret;
606 port_matrix[pi_id].exist = true;
608 if (!pi->pairset[1].np)
611 ret = tps23881_match_channel(&pi->pairset[1], chan_node);
615 if (!tps23881_is_chan_free(port_matrix, ret)) {
616 pr_err("tps23881: channel %d already used\n", ret);
620 if (port_matrix[pi_id].hw_chan[0] / 4 != ret / 4) {
621 pr_err("tps23881: 4-pair PSE can only be set within the same 4 ports group");
625 port_matrix[pi_id].hw_chan[1] = ret;
626 port_matrix[pi_id].is_4p = true;
632 tps23881_get_unused_chan(struct tps23881_port_matrix port_matrix[TPS23881_MAX_CHANS],
638 for (i = 0; i < TPS23881_MAX_CHANS; i++) {
641 for (j = 0; j < port_cnt; j++) {
642 if (port_matrix[j].hw_chan[0] == i) {
647 if (port_matrix[j].is_4p &&
648 port_matrix[j].hw_chan[1] == i) {
661 /* Sort the port matrix to following particular hardware ports matrix
662 * specification of the tps23881. The device has two 4-ports groups and
663 * each 4-pair powered device has to be configured to use two consecutive
664 * logical channel in each 4 ports group (1 and 2 or 3 and 4). Also the
665 * hardware matrix has to be fully configured even with unused chan to be
669 tps23881_sort_port_matrix(struct tps23881_port_matrix port_matrix[TPS23881_MAX_CHANS])
671 struct tps23881_port_matrix tmp_port_matrix[TPS23881_MAX_CHANS] = {0};
672 int i, ret, port_cnt = 0, cnt_4ch_grp1 = 0, cnt_4ch_grp2 = 4;
674 /* Configure 4p port matrix */
675 for (i = 0; i < TPS23881_MAX_CHANS; i++) {
678 if (!port_matrix[i].exist || !port_matrix[i].is_4p)
681 if (port_matrix[i].hw_chan[0] < 4)
686 tmp_port_matrix[port_cnt].exist = true;
687 tmp_port_matrix[port_cnt].is_4p = true;
688 tmp_port_matrix[port_cnt].pi_id = i;
689 tmp_port_matrix[port_cnt].hw_chan[0] = port_matrix[i].hw_chan[0];
690 tmp_port_matrix[port_cnt].hw_chan[1] = port_matrix[i].hw_chan[1];
692 /* 4-pair ports have to be configured with consecutive
693 * logical channels 0 and 1, 2 and 3.
695 tmp_port_matrix[port_cnt].lgcl_chan[0] = (*cnt)++;
696 tmp_port_matrix[port_cnt].lgcl_chan[1] = (*cnt)++;
701 /* Configure 2p port matrix */
702 for (i = 0; i < TPS23881_MAX_CHANS; i++) {
705 if (!port_matrix[i].exist || port_matrix[i].is_4p)
708 if (port_matrix[i].hw_chan[0] < 4)
713 tmp_port_matrix[port_cnt].exist = true;
714 tmp_port_matrix[port_cnt].pi_id = i;
715 tmp_port_matrix[port_cnt].lgcl_chan[0] = (*cnt)++;
716 tmp_port_matrix[port_cnt].hw_chan[0] = port_matrix[i].hw_chan[0];
721 /* Complete the rest of the first 4 port group matrix even if
722 * channels are unused
724 while (cnt_4ch_grp1 < 4) {
725 ret = tps23881_get_unused_chan(tmp_port_matrix, port_cnt);
727 pr_err("tps23881: port matrix issue, no chan available\n");
731 if (port_cnt >= TPS23881_MAX_CHANS) {
732 pr_err("tps23881: wrong number of channels\n");
735 tmp_port_matrix[port_cnt].lgcl_chan[0] = cnt_4ch_grp1;
736 tmp_port_matrix[port_cnt].hw_chan[0] = ret;
741 /* Complete the rest of the second 4 port group matrix even if
742 * channels are unused
744 while (cnt_4ch_grp2 < 8) {
745 ret = tps23881_get_unused_chan(tmp_port_matrix, port_cnt);
747 pr_err("tps23881: port matrix issue, no chan available\n");
751 if (port_cnt >= TPS23881_MAX_CHANS) {
752 pr_err("tps23881: wrong number of channels\n");
755 tmp_port_matrix[port_cnt].lgcl_chan[0] = cnt_4ch_grp2;
756 tmp_port_matrix[port_cnt].hw_chan[0] = ret;
761 memcpy(port_matrix, tmp_port_matrix, sizeof(tmp_port_matrix));
766 /* Write port matrix to the hardware port matrix and the software port
770 tps23881_write_port_matrix(struct tps23881_priv *priv,
771 struct tps23881_port_matrix port_matrix[TPS23881_MAX_CHANS],
774 struct i2c_client *client = priv->client;
775 u8 pi_id, lgcl_chan, hw_chan;
779 for (i = 0; i < port_cnt; i++) {
780 pi_id = port_matrix[i].pi_id;
781 lgcl_chan = port_matrix[i].lgcl_chan[0];
782 hw_chan = port_matrix[i].hw_chan[0] % 4;
784 /* Set software port matrix for existing ports */
785 if (port_matrix[i].exist)
786 priv->port[pi_id].chan[0] = lgcl_chan;
788 /* Initialize power policy internal value */
789 priv->port[pi_id].pw_pol = -1;
791 /* Set hardware port matrix for all ports */
792 val |= hw_chan << (lgcl_chan * 2);
794 if (!port_matrix[i].is_4p)
797 lgcl_chan = port_matrix[i].lgcl_chan[1];
798 hw_chan = port_matrix[i].hw_chan[1] % 4;
800 /* Set software port matrix for existing ports */
801 if (port_matrix[i].exist) {
802 priv->port[pi_id].is_4p = true;
803 priv->port[pi_id].chan[1] = lgcl_chan;
806 /* Set hardware port matrix for all ports */
807 val |= hw_chan << (lgcl_chan * 2);
810 /* Write hardware ports matrix */
811 return i2c_smbus_write_word_data(client, TPS23881_REG_PORT_MAP, val);
815 tps23881_set_ports_conf(struct tps23881_priv *priv,
816 struct tps23881_port_matrix port_matrix[TPS23881_MAX_CHANS])
818 struct i2c_client *client = priv->client;
822 /* Set operating mode */
823 ret = i2c_smbus_write_word_data(client, TPS23881_REG_OP_MODE,
824 TPS23881_OP_MODE_SEMIAUTO);
828 /* Disable DC disconnect */
829 ret = i2c_smbus_write_word_data(client, TPS23881_REG_DIS_EN, 0x0);
833 /* Set port power allocation */
835 for (i = 0; i < TPS23881_MAX_CHANS; i++) {
836 if (!port_matrix[i].exist)
839 if (port_matrix[i].is_4p)
840 val |= 0xf << ((port_matrix[i].lgcl_chan[0] / 2) * 4);
842 val |= 0x3 << ((port_matrix[i].lgcl_chan[0] / 2) * 4);
844 ret = i2c_smbus_write_word_data(client, TPS23881_REG_PORT_POWER, val);
848 /* Enable detection and classification */
850 for (i = 0; i < TPS23881_MAX_CHANS; i++) {
851 if (!port_matrix[i].exist)
854 val |= BIT(port_matrix[i].lgcl_chan[0]) |
855 BIT(port_matrix[i].lgcl_chan[0] + 4);
856 if (port_matrix[i].is_4p)
857 val |= BIT(port_matrix[i].lgcl_chan[1]) |
858 BIT(port_matrix[i].lgcl_chan[1] + 4);
860 return i2c_smbus_write_word_data(client, TPS23881_REG_DET_CLA_EN, val);
864 tps23881_set_ports_matrix(struct tps23881_priv *priv,
865 struct device_node *chan_node[TPS23881_MAX_CHANS])
867 struct tps23881_port_matrix port_matrix[TPS23881_MAX_CHANS] = {0};
870 /* Update with values for every PSE PIs */
871 for (i = 0; i < TPS23881_MAX_CHANS; i++) {
872 ret = tps23881_match_port_matrix(&priv->pcdev.pi[i], i,
873 chan_node, port_matrix);
878 ret = tps23881_sort_port_matrix(port_matrix);
882 ret = tps23881_write_port_matrix(priv, port_matrix, ret);
886 return tps23881_set_ports_conf(priv, port_matrix);
889 static int tps23881_setup_pi_matrix(struct pse_controller_dev *pcdev)
891 struct device_node *chan_node[TPS23881_MAX_CHANS] = {NULL};
892 struct tps23881_priv *priv = to_tps23881_priv(pcdev);
895 ret = tps23881_get_of_channels(priv, chan_node);
897 dev_warn(&priv->client->dev,
898 "Unable to parse port-matrix, default matrix will be used\n");
902 ret = tps23881_set_ports_matrix(priv, chan_node);
904 for (i = 0; i < TPS23881_MAX_CHANS; i++)
905 of_node_put(chan_node[i]);
910 static const struct pse_controller_ops tps23881_ops = {
911 .setup_pi_matrix = tps23881_setup_pi_matrix,
912 .pi_enable = tps23881_pi_enable,
913 .pi_disable = tps23881_pi_disable,
914 .pi_get_admin_state = tps23881_pi_get_admin_state,
915 .pi_get_pw_status = tps23881_pi_get_pw_status,
916 .pi_get_pw_class = tps23881_pi_get_pw_class,
917 .pi_get_actual_pw = tps23881_pi_get_actual_pw,
918 .pi_get_voltage = tps23881_pi_get_voltage,
919 .pi_get_pw_limit = tps23881_pi_get_pw_limit,
920 .pi_set_pw_limit = tps23881_pi_set_pw_limit,
921 .pi_get_pw_limit_ranges = tps23881_pi_get_pw_limit_ranges,
924 static const char fw_parity_name[] = "ti/tps23881/tps23881-parity-14.bin";
925 static const char fw_sram_name[] = "ti/tps23881/tps23881-sram-14.bin";
927 struct tps23881_fw_conf {
932 static const struct tps23881_fw_conf tps23881_fw_parity_conf[] = {
933 {.reg = 0x60, .val = 0x01},
934 {.reg = 0x62, .val = 0x00},
935 {.reg = 0x63, .val = 0x80},
936 {.reg = 0x60, .val = 0xC4},
937 {.reg = 0x1D, .val = 0xBC},
938 {.reg = 0xD7, .val = 0x02},
939 {.reg = 0x91, .val = 0x00},
940 {.reg = 0x90, .val = 0x00},
941 {.reg = 0xD7, .val = 0x00},
942 {.reg = 0x1D, .val = 0x00},
946 static const struct tps23881_fw_conf tps23881_fw_sram_conf[] = {
947 {.reg = 0x60, .val = 0xC5},
948 {.reg = 0x62, .val = 0x00},
949 {.reg = 0x63, .val = 0x80},
950 {.reg = 0x60, .val = 0xC0},
951 {.reg = 0x1D, .val = 0xBC},
952 {.reg = 0xD7, .val = 0x02},
953 {.reg = 0x91, .val = 0x00},
954 {.reg = 0x90, .val = 0x00},
955 {.reg = 0xD7, .val = 0x00},
956 {.reg = 0x1D, .val = 0x00},
960 static int tps23881_flash_sram_fw_part(struct i2c_client *client,
962 const struct tps23881_fw_conf *fw_conf)
964 const struct firmware *fw = NULL;
967 ret = request_firmware(&fw, fw_name, &client->dev);
971 dev_dbg(&client->dev, "Flashing %s\n", fw_name);
973 /* Prepare device for RAM download */
974 while (fw_conf->reg) {
975 ret = i2c_smbus_write_byte_data(client, fw_conf->reg,
983 /* Flash the firmware file */
984 for (i = 0; i < fw->size; i++) {
985 ret = i2c_smbus_write_byte_data(client,
986 TPS23881_REG_SRAM_DATA,
993 release_firmware(fw);
997 static int tps23881_flash_sram_fw(struct i2c_client *client)
1001 ret = tps23881_flash_sram_fw_part(client, fw_parity_name,
1002 tps23881_fw_parity_conf);
1006 ret = tps23881_flash_sram_fw_part(client, fw_sram_name,
1007 tps23881_fw_sram_conf);
1011 ret = i2c_smbus_write_byte_data(client, TPS23881_REG_SRAM_CTRL, 0x18);
1020 static int tps23881_i2c_probe(struct i2c_client *client)
1022 struct device *dev = &client->dev;
1023 struct tps23881_priv *priv;
1024 struct gpio_desc *reset;
1028 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
1029 dev_err(dev, "i2c check functionality failed\n");
1033 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
1037 reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
1039 return dev_err_probe(&client->dev, PTR_ERR(reset), "Failed to get reset GPIO\n");
1042 /* TPS23880 datasheet (Rev G) indicates minimum reset pulse is 5us */
1043 usleep_range(5, 10);
1044 gpiod_set_value_cansleep(reset, 0); /* De-assert reset */
1046 /* TPS23880 datasheet indicates the minimum time after power on reset
1047 * should be 20ms, but the document describing how to load SRAM ("How
1048 * to Load TPS2388x SRAM and Parity Code over I2C" (Rev E))
1049 * indicates we should delay that programming by at least 50ms. So
1050 * we'll wait the entire 50ms here to ensure we're safe to go to the
1051 * SRAM loading proceedure.
1056 ret = i2c_smbus_read_byte_data(client, TPS23881_REG_DEVID);
1060 if (FIELD_GET(TPS23881_REG_DEVID_MASK, ret) != TPS23881_DEVICE_ID) {
1061 dev_err(dev, "Wrong device ID\n");
1065 ret = tps23881_flash_sram_fw(client);
1069 ret = i2c_smbus_read_byte_data(client, TPS23881_REG_FWREV);
1073 dev_info(&client->dev, "Firmware revision 0x%x\n", ret);
1075 /* Set configuration B, 16 bit access on a single device address */
1076 ret = i2c_smbus_read_byte_data(client, TPS23881_REG_GEN_MASK);
1080 val = ret | TPS23881_REG_NBITACC;
1081 ret = i2c_smbus_write_byte_data(client, TPS23881_REG_GEN_MASK, val);
1085 priv->client = client;
1086 i2c_set_clientdata(client, priv);
1087 priv->np = dev->of_node;
1089 priv->pcdev.owner = THIS_MODULE;
1090 priv->pcdev.ops = &tps23881_ops;
1091 priv->pcdev.dev = dev;
1092 priv->pcdev.types = ETHTOOL_PSE_C33;
1093 priv->pcdev.nr_lines = TPS23881_MAX_CHANS;
1094 ret = devm_pse_controller_register(dev, &priv->pcdev);
1096 return dev_err_probe(dev, ret,
1097 "failed to register PSE controller\n");
1103 static const struct i2c_device_id tps23881_id[] = {
1107 MODULE_DEVICE_TABLE(i2c, tps23881_id);
1109 static const struct of_device_id tps23881_of_match[] = {
1110 { .compatible = "ti,tps23881", },
1113 MODULE_DEVICE_TABLE(of, tps23881_of_match);
1115 static struct i2c_driver tps23881_driver = {
1116 .probe = tps23881_i2c_probe,
1117 .id_table = tps23881_id,
1120 .of_match_table = tps23881_of_match,
1123 module_i2c_driver(tps23881_driver);
1126 MODULE_DESCRIPTION("TI TPS23881 PoE PSE Controller driver");
1127 MODULE_LICENSE("GPL");