Skip to content

Commit ebaa06f

Browse files
LorenzoBianconibjorn-helgaas
authored andcommitted
PCI/pwrctrl: tc9563: Rely on regmap APIs
Replace the custom tc9563_pwrctrl_i2c_{read,write,bulk_write} functions with standard regmap APIs. The device uses 24-bit big-endian register addresses and 32-bit little-endian register values over I2C, which is directly supported by regmap. This removes the open-coded I2C transfer handling and lets the regmap core handle formatting, locking, and multi-register coalescing. Signed-off-by: Lorenzo Bianconi <lorenzo.bianconi@oss.qualcomm.com> Signed-off-by: Bjorn Helgaas <bhelgaas@google.com> Reviewed-by: Manivannan Sadhasivam <mani@kernel.org> Reviewed-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com> Link: https://patch.msgid.link/20260729-pci-pwrctrl-tc956x-regmap-v1-1-20ce61a3ce12@oss.qualcomm.com
1 parent cee9395 commit ebaa06f

2 files changed

Lines changed: 44 additions & 110 deletions

File tree

‎drivers/pci/pwrctrl/Kconfig‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ config PCI_PWRCTRL_TC9563
2929
select PCI_PWRCTRL
3030
default m if ARCH_QCOM
3131
depends on I2C
32+
select REGMAP_I2C
3233
help
3334
Say Y here to enable the PCI Power Control driver of TC9563 PCIe
3435
switch.

‎drivers/pci/pwrctrl/pci-pwrctrl-tc9563.c‎

Lines changed: 43 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
#include <linux/pci.h>
1717
#include <linux/pci-pwrctrl.h>
1818
#include <linux/platform_device.h>
19+
#include <linux/regmap.h>
1920
#include <linux/regulator/consumer.h>
2021
#include <linux/string.h>
2122
#include <linux/types.h>
22-
#include <linux/unaligned.h>
2323

2424
#include "../pci.h"
2525

@@ -70,11 +70,6 @@
7070

7171
#define TC9563_L0S_L1_DELAY_UNIT_NS 256 /* Each unit represents 256 ns */
7272

73-
struct tc9563_pwrctrl_reg_setting {
74-
unsigned int offset;
75-
unsigned int val;
76-
};
77-
7873
enum tc9563_pwrctrl_ports {
7974
TC9563_USP,
8075
TC9563_DSP1,
@@ -111,13 +106,14 @@ struct tc9563_pwrctrl {
111106
struct gpio_desc *reset_gpio;
112107
struct i2c_adapter *adapter;
113108
struct i2c_client *client;
109+
struct regmap *regmap;
114110
};
115111

116112
/*
117113
* downstream port power off sequence, hardcoding the address
118114
* as we don't know register names for these register offsets.
119115
*/
120-
static const struct tc9563_pwrctrl_reg_setting common_pwroff_seq[] = {
116+
static const struct reg_sequence common_pwroff_seq[] = {
121117
{0x82900c, 0x1},
122118
{0x829010, 0x1},
123119
{0x829018, 0x0},
@@ -140,101 +136,27 @@ static const struct tc9563_pwrctrl_reg_setting common_pwroff_seq[] = {
140136
{0x829114, 0x1},
141137
};
142138

143-
static const struct tc9563_pwrctrl_reg_setting dsp1_pwroff_seq[] = {
139+
static const struct reg_sequence dsp1_pwroff_seq[] = {
144140
{TC9563_PORT_ACCESS_ENABLE, 0x2},
145141
{TC9563_PORT_LANE_ACCESS_ENABLE, 0x3},
146142
{TC9563_POWER_CONTROL, 0x014f4804},
147143
{TC9563_POWER_CONTROL_OVREN, 0x1},
148144
{TC9563_PORT_ACCESS_ENABLE, 0x4},
149145
};
150146

151-
static const struct tc9563_pwrctrl_reg_setting dsp2_pwroff_seq[] = {
147+
static const struct reg_sequence dsp2_pwroff_seq[] = {
152148
{TC9563_PORT_ACCESS_ENABLE, 0x8},
153149
{TC9563_PORT_LANE_ACCESS_ENABLE, 0x1},
154150
{TC9563_POWER_CONTROL, 0x014f4804},
155151
{TC9563_POWER_CONTROL_OVREN, 0x1},
156152
{TC9563_PORT_ACCESS_ENABLE, 0x8},
157153
};
158154

159-
/*
160-
* Since all transfers are initiated by the probe, no locks are necessary,
161-
* as there are no concurrent calls.
162-
*/
163-
static int tc9563_pwrctrl_i2c_write(struct i2c_client *client,
164-
u32 reg_addr, u32 reg_val)
165-
{
166-
struct i2c_msg msg;
167-
u8 msg_buf[7];
168-
int ret;
169-
170-
msg.addr = client->addr;
171-
msg.len = 7;
172-
msg.flags = 0;
173-
174-
/* Big Endian for reg addr */
175-
put_unaligned_be24(reg_addr, &msg_buf[0]);
176-
177-
/* Little Endian for reg val */
178-
put_unaligned_le32(reg_val, &msg_buf[3]);
179-
180-
msg.buf = msg_buf;
181-
ret = i2c_transfer(client->adapter, &msg, 1);
182-
return ret == 1 ? 0 : ret;
183-
}
184-
185-
static int tc9563_pwrctrl_i2c_read(struct i2c_client *client,
186-
u32 reg_addr, u32 *reg_val)
187-
{
188-
struct i2c_msg msg[2];
189-
u8 wr_data[3];
190-
u32 rd_data;
191-
int ret;
192-
193-
msg[0].addr = client->addr;
194-
msg[0].len = 3;
195-
msg[0].flags = 0;
196-
197-
/* Big Endian for reg addr */
198-
put_unaligned_be24(reg_addr, &wr_data[0]);
199-
200-
msg[0].buf = wr_data;
201-
202-
msg[1].addr = client->addr;
203-
msg[1].len = 4;
204-
msg[1].flags = I2C_M_RD;
205-
206-
msg[1].buf = (u8 *)&rd_data;
207-
208-
ret = i2c_transfer(client->adapter, &msg[0], 2);
209-
if (ret == 2) {
210-
*reg_val = get_unaligned_le32(&rd_data);
211-
return 0;
212-
}
213-
214-
/* If only one message successfully completed, return -EIO */
215-
return ret == 1 ? -EIO : ret;
216-
}
217-
218-
static int tc9563_pwrctrl_i2c_bulk_write(struct i2c_client *client,
219-
const struct tc9563_pwrctrl_reg_setting *seq,
220-
int len)
221-
{
222-
int ret, i;
223-
224-
for (i = 0; i < len; i++) {
225-
ret = tc9563_pwrctrl_i2c_write(client, seq[i].offset, seq[i].val);
226-
if (ret)
227-
return ret;
228-
}
229-
230-
return 0;
231-
}
232-
233155
static int tc9563_pwrctrl_disable_port(struct tc9563_pwrctrl *tc9563,
234156
enum tc9563_pwrctrl_ports port)
235157
{
236158
struct tc9563_pwrctrl_cfg *cfg = &tc9563->cfg[port];
237-
const struct tc9563_pwrctrl_reg_setting *seq;
159+
const struct reg_sequence *seq;
238160
int ret, len;
239161

240162
if (!cfg->disable_port)
@@ -254,12 +176,12 @@ static int tc9563_pwrctrl_disable_port(struct tc9563_pwrctrl *tc9563,
254176
return 0;
255177
}
256178

257-
ret = tc9563_pwrctrl_i2c_bulk_write(tc9563->client, seq, len);
179+
ret = regmap_multi_reg_write(tc9563->regmap, seq, len);
258180
if (ret)
259181
return ret;
260182

261-
return tc9563_pwrctrl_i2c_bulk_write(tc9563->client, common_pwroff_seq,
262-
ARRAY_SIZE(common_pwroff_seq));
183+
return regmap_multi_reg_write(tc9563->regmap, common_pwroff_seq,
184+
ARRAY_SIZE(common_pwroff_seq));
263185
}
264186

265187
static int tc9563_pwrctrl_set_port_l0s_l1_entry_delay(struct tc9563_pwrctrl *tc9563,
@@ -275,14 +197,13 @@ static int tc9563_pwrctrl_set_port_l0s_l1_entry_delay(struct tc9563_pwrctrl *tc9
275197
/* convert to units of 256ns */
276198
units = ns / TC9563_L0S_L1_DELAY_UNIT_NS;
277199

278-
ret = tc9563_pwrctrl_i2c_write(tc9563->client, TC9563_PORT_SELECT,
279-
BIT(port));
200+
ret = regmap_write(tc9563->regmap, TC9563_PORT_SELECT, BIT(port));
280201
if (ret)
281202
return ret;
282203

283-
return tc9563_pwrctrl_i2c_write(tc9563->client,
284-
is_l1 ? TC9563_PORT_L1_DELAY : TC9563_PORT_L0S_DELAY,
285-
units);
204+
return regmap_write(tc9563->regmap,
205+
is_l1 ? TC9563_PORT_L1_DELAY : TC9563_PORT_L0S_DELAY,
206+
units);
286207
}
287208

288209
static int tc9563_pwrctrl_set_eth_l0s_l1_entry_delay(struct tc9563_pwrctrl *tc9563,
@@ -297,8 +218,7 @@ static int tc9563_pwrctrl_set_eth_l0s_l1_entry_delay(struct tc9563_pwrctrl *tc95
297218
/* convert to units of 256ns */
298219
units = ns / TC9563_L0S_L1_DELAY_UNIT_NS;
299220

300-
ret = tc9563_pwrctrl_i2c_read(tc9563->client, TC9563_EMBEDDED_ETH_DELAY,
301-
&rd_val);
221+
ret = regmap_read(tc9563->regmap, TC9563_EMBEDDED_ETH_DELAY, &rd_val);
302222
if (ret)
303223
return ret;
304224

@@ -309,8 +229,7 @@ static int tc9563_pwrctrl_set_eth_l0s_l1_entry_delay(struct tc9563_pwrctrl *tc95
309229
rd_val = u32_replace_bits(rd_val, units,
310230
TC9563_ETH_L0S_DELAY_MASK);
311231

312-
return tc9563_pwrctrl_i2c_write(tc9563->client, TC9563_EMBEDDED_ETH_DELAY,
313-
rd_val);
232+
return regmap_write(tc9563->regmap, TC9563_EMBEDDED_ETH_DELAY, rd_val);
314233
}
315234

316235
static int tc9563_pwrctrl_set_tx_amplitude(struct tc9563_pwrctrl *tc9563,
@@ -343,14 +262,14 @@ static int tc9563_pwrctrl_set_tx_amplitude(struct tc9563_pwrctrl *tc9563,
343262
return -EINVAL;
344263
}
345264

346-
struct tc9563_pwrctrl_reg_setting tx_amp_seq[] = {
265+
struct reg_sequence tx_amp_seq[] = {
347266
{TC9563_PORT_ACCESS_ENABLE, port_access},
348267
{TC9563_PORT_LANE_ACCESS_ENABLE, 0x3},
349268
{TC9563_TX_MARGIN, amp},
350269
};
351270

352-
return tc9563_pwrctrl_i2c_bulk_write(tc9563->client, tx_amp_seq,
353-
ARRAY_SIZE(tx_amp_seq));
271+
return regmap_multi_reg_write(tc9563->regmap, tx_amp_seq,
272+
ARRAY_SIZE(tx_amp_seq));
354273
}
355274

356275
static int tc9563_pwrctrl_disable_dfe(struct tc9563_pwrctrl *tc9563,
@@ -383,7 +302,7 @@ static int tc9563_pwrctrl_disable_dfe(struct tc9563_pwrctrl *tc9563,
383302
return -EINVAL;
384303
}
385304

386-
struct tc9563_pwrctrl_reg_setting disable_dfe_seq[] = {
305+
struct reg_sequence disable_dfe_seq[] = {
387306
{TC9563_PORT_ACCESS_ENABLE, port_access},
388307
{TC9563_PORT_LANE_ACCESS_ENABLE, lane_access},
389308
{TC9563_DFE_ENABLE, 0x0},
@@ -397,15 +316,15 @@ static int tc9563_pwrctrl_disable_dfe(struct tc9563_pwrctrl *tc9563,
397316
{TC9563_PHY_RATE_CHANGE_OVERRIDE, 0x0},
398317
};
399318

400-
return tc9563_pwrctrl_i2c_bulk_write(tc9563->client, disable_dfe_seq,
401-
ARRAY_SIZE(disable_dfe_seq));
319+
return regmap_multi_reg_write(tc9563->regmap, disable_dfe_seq,
320+
ARRAY_SIZE(disable_dfe_seq));
402321
}
403322

404323
static int tc9563_pwrctrl_set_nfts(struct tc9563_pwrctrl *tc9563,
405324
enum tc9563_pwrctrl_ports port)
406325
{
407326
u8 *nfts = tc9563->cfg[port].nfts;
408-
struct tc9563_pwrctrl_reg_setting nfts_seq[] = {
327+
struct reg_sequence nfts_seq[] = {
409328
{TC9563_NFTS_2_5_GT, nfts[0]},
410329
{TC9563_NFTS_5_GT, nfts[1]},
411330
};
@@ -420,28 +339,27 @@ static int tc9563_pwrctrl_set_nfts(struct tc9563_pwrctrl *tc9563,
420339
return 0;
421340
}
422341

423-
ret = tc9563_pwrctrl_i2c_write(tc9563->client, TC9563_PORT_SELECT,
424-
BIT(port));
342+
ret = regmap_write(tc9563->regmap, TC9563_PORT_SELECT, BIT(port));
425343
if (ret)
426344
return ret;
427345

428-
return tc9563_pwrctrl_i2c_bulk_write(tc9563->client, nfts_seq,
429-
ARRAY_SIZE(nfts_seq));
346+
return regmap_multi_reg_write(tc9563->regmap, nfts_seq,
347+
ARRAY_SIZE(nfts_seq));
430348
}
431349

432350
static int tc9563_pwrctrl_assert_deassert_reset(struct tc9563_pwrctrl *tc9563,
433351
bool deassert)
434352
{
435353
int ret, val;
436354

437-
ret = tc9563_pwrctrl_i2c_write(tc9563->client, TC9563_GPIO_CONFIG,
438-
TC9563_GPIO_MASK);
355+
ret = regmap_write(tc9563->regmap, TC9563_GPIO_CONFIG,
356+
TC9563_GPIO_MASK);
439357
if (ret)
440358
return ret;
441359

442360
val = deassert ? TC9563_GPIO_DEASSERT_BITS : 0;
443361

444-
return tc9563_pwrctrl_i2c_write(tc9563->client, TC9563_RESET_GPIO, val);
362+
return regmap_write(tc9563->regmap, TC9563_RESET_GPIO, val);
445363
}
446364

447365
static int tc9563_pwrctrl_parse_device_dt(struct device_node *node,
@@ -572,6 +490,13 @@ static int tc9563_pwrctrl_power_on(struct pci_pwrctrl *pwrctrl)
572490
return ret;
573491
}
574492

493+
static const struct regmap_config tc9563_regmap_config = {
494+
.reg_bits = 24,
495+
.val_bits = 32,
496+
.reg_format_endian = REGMAP_ENDIAN_BIG,
497+
.val_format_endian = REGMAP_ENDIAN_LITTLE,
498+
};
499+
575500
static int tc9563_pwrctrl_probe(struct platform_device *pdev)
576501
{
577502
struct device_node *node = pdev->dev.of_node;
@@ -602,6 +527,14 @@ static int tc9563_pwrctrl_probe(struct platform_device *pdev)
602527
return PTR_ERR(tc9563->client);
603528
}
604529

530+
tc9563->regmap = devm_regmap_init_i2c(tc9563->client,
531+
&tc9563_regmap_config);
532+
if (IS_ERR(tc9563->regmap)) {
533+
ret = dev_err_probe(dev, PTR_ERR(tc9563->regmap),
534+
"Failed to allocate register map\n");
535+
goto remove_i2c;
536+
}
537+
605538
for (int i = 0; i < ARRAY_SIZE(tc9563_supply_names); i++)
606539
tc9563->supplies[i].supply = tc9563_supply_names[i];
607540

0 commit comments

Comments
 (0)