Skip to content

Commit 2e44555

Browse files
bijudasLinus Walleij
authored andcommitted
pinctrl: mcp23s08: Simplify probe()/mcp23s08_spi_regmap_init()
Add struct mcp23s08_info and simplify probe()/mcp23s08_spi_regmap_init() by replacing match data 'type' with 'struct mcp23s08_info'. While at it, replace 'dev_err()'->'dev_err_probe()' and drop printing 'type' in error path for i2c_get_match_data(). Signed-off-by: Biju Das <biju.das.jz@bp.renesas.com> Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com> Link: https://lore.kernel.org/r/20231001150113.7752-4-biju.das.jz@bp.renesas.com Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
1 parent b03f7aa commit 2e44555

3 files changed

Lines changed: 93 additions & 96 deletions

File tree

drivers/pinctrl/pinctrl-mcp23s08.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ struct regmap;
2222

2323
struct pinctrl_dev;
2424

25+
struct mcp23s08_info {
26+
const struct regmap_config *regmap;
27+
const char *label;
28+
unsigned int type;
29+
u16 ngpio;
30+
bool reg_shift;
31+
};
32+
2533
struct mcp23s08 {
2634
u8 addr;
2735
bool irq_active_high;

drivers/pinctrl/pinctrl-mcp23s08_i2c.c

Lines changed: 41 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -10,50 +10,30 @@
1010

1111
static int mcp230xx_probe(struct i2c_client *client)
1212
{
13+
const struct mcp23s08_info *info;
1314
struct device *dev = &client->dev;
1415
struct mcp23s08 *mcp;
15-
unsigned int type;
1616
int ret;
1717

1818
mcp = devm_kzalloc(dev, sizeof(*mcp), GFP_KERNEL);
1919
if (!mcp)
2020
return -ENOMEM;
2121

22-
type = (uintptr_t)i2c_get_match_data(client);
23-
switch (type) {
24-
case MCP_TYPE_008:
25-
mcp->regmap = devm_regmap_init_i2c(client, &mcp23x08_regmap);
26-
mcp->reg_shift = 0;
27-
mcp->chip.ngpio = 8;
28-
mcp->chip.label = "mcp23008";
29-
break;
30-
31-
case MCP_TYPE_017:
32-
mcp->regmap = devm_regmap_init_i2c(client, &mcp23x17_regmap);
33-
mcp->reg_shift = 1;
34-
mcp->chip.ngpio = 16;
35-
mcp->chip.label = "mcp23017";
36-
break;
37-
38-
case MCP_TYPE_018:
39-
mcp->regmap = devm_regmap_init_i2c(client, &mcp23x17_regmap);
40-
mcp->reg_shift = 1;
41-
mcp->chip.ngpio = 16;
42-
mcp->chip.label = "mcp23018";
43-
break;
44-
45-
default:
46-
dev_err(dev, "invalid device type (%d)\n", type);
47-
return -EINVAL;
48-
}
22+
info = i2c_get_match_data(client);
23+
if (!info)
24+
return dev_err_probe(dev, -EINVAL, "invalid device type\n");
4925

26+
mcp->reg_shift = info->reg_shift;
27+
mcp->chip.ngpio = info->ngpio;
28+
mcp->chip.label = info->label;
29+
mcp->regmap = devm_regmap_init_i2c(client, info->regmap);
5030
if (IS_ERR(mcp->regmap))
5131
return PTR_ERR(mcp->regmap);
5232

5333
mcp->irq = client->irq;
5434
mcp->pinctrl_desc.name = "mcp23xxx-pinctrl";
5535

56-
ret = mcp23s08_probe_one(mcp, dev, client->addr, type, -1);
36+
ret = mcp23s08_probe_one(mcp, dev, client->addr, info->type, -1);
5737
if (ret)
5838
return ret;
5939

@@ -62,36 +42,45 @@ static int mcp230xx_probe(struct i2c_client *client)
6242
return 0;
6343
}
6444

45+
static const struct mcp23s08_info mcp23008_i2c = {
46+
.regmap = &mcp23x08_regmap,
47+
.label = "mcp23008",
48+
.type = MCP_TYPE_008,
49+
.ngpio = 8,
50+
.reg_shift = 0,
51+
};
52+
53+
static const struct mcp23s08_info mcp23017_i2c = {
54+
.regmap = &mcp23x17_regmap,
55+
.label = "mcp23017",
56+
.type = MCP_TYPE_017,
57+
.ngpio = 16,
58+
.reg_shift = 1,
59+
};
60+
61+
static const struct mcp23s08_info mcp23018_i2c = {
62+
.regmap = &mcp23x17_regmap,
63+
.label = "mcp23018",
64+
.type = MCP_TYPE_018,
65+
.ngpio = 16,
66+
.reg_shift = 1,
67+
};
68+
6569
static const struct i2c_device_id mcp230xx_id[] = {
66-
{ "mcp23008", MCP_TYPE_008 },
67-
{ "mcp23017", MCP_TYPE_017 },
68-
{ "mcp23018", MCP_TYPE_018 },
70+
{ "mcp23008", (kernel_ulong_t)&mcp23008_i2c },
71+
{ "mcp23017", (kernel_ulong_t)&mcp23017_i2c },
72+
{ "mcp23018", (kernel_ulong_t)&mcp23018_i2c },
6973
{ }
7074
};
7175
MODULE_DEVICE_TABLE(i2c, mcp230xx_id);
7276

7377
static const struct of_device_id mcp23s08_i2c_of_match[] = {
74-
{
75-
.compatible = "microchip,mcp23008",
76-
.data = (void *) MCP_TYPE_008,
77-
},
78-
{
79-
.compatible = "microchip,mcp23017",
80-
.data = (void *) MCP_TYPE_017,
81-
},
82-
{
83-
.compatible = "microchip,mcp23018",
84-
.data = (void *) MCP_TYPE_018,
85-
},
78+
{ .compatible = "microchip,mcp23008", .data = &mcp23008_i2c },
79+
{ .compatible = "microchip,mcp23017", .data = &mcp23017_i2c },
80+
{ .compatible = "microchip,mcp23018", .data = &mcp23018_i2c },
8681
/* NOTE: The use of the mcp prefix is deprecated and will be removed. */
87-
{
88-
.compatible = "mcp,mcp23008",
89-
.data = (void *) MCP_TYPE_008,
90-
},
91-
{
92-
.compatible = "mcp,mcp23017",
93-
.data = (void *) MCP_TYPE_017,
94-
},
82+
{ .compatible = "mcp,mcp23008", .data = &mcp23008_i2c },
83+
{ .compatible = "mcp,mcp23017", .data = &mcp23017_i2c },
9584
{ }
9685
};
9786
MODULE_DEVICE_TABLE(of, mcp23s08_i2c_of_match);

drivers/pinctrl/pinctrl-mcp23s08_spi.c

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -80,56 +80,48 @@ static const struct regmap_bus mcp23sxx_spi_regmap = {
8080
};
8181

8282
static int mcp23s08_spi_regmap_init(struct mcp23s08 *mcp, struct device *dev,
83-
unsigned int addr, unsigned int type)
83+
unsigned int addr,
84+
const struct mcp23s08_info *info)
8485
{
85-
const struct regmap_config *config;
8686
struct regmap_config *copy;
8787
const char *name;
8888

89-
switch (type) {
89+
switch (info->type) {
9090
case MCP_TYPE_S08:
91-
mcp->reg_shift = 0;
92-
mcp->chip.ngpio = 8;
9391
mcp->chip.label = devm_kasprintf(dev, GFP_KERNEL, "mcp23s08.%d", addr);
9492
if (!mcp->chip.label)
9593
return -ENOMEM;
9694

97-
config = &mcp23x08_regmap;
9895
name = devm_kasprintf(dev, GFP_KERNEL, "%d", addr);
9996
if (!name)
10097
return -ENOMEM;
10198

10299
break;
103100

104101
case MCP_TYPE_S17:
105-
mcp->reg_shift = 1;
106-
mcp->chip.ngpio = 16;
107102
mcp->chip.label = devm_kasprintf(dev, GFP_KERNEL, "mcp23s17.%d", addr);
108103
if (!mcp->chip.label)
109104
return -ENOMEM;
110105

111-
config = &mcp23x17_regmap;
112106
name = devm_kasprintf(dev, GFP_KERNEL, "%d", addr);
113107
if (!name)
114108
return -ENOMEM;
115109

116110
break;
117111

118112
case MCP_TYPE_S18:
119-
mcp->reg_shift = 1;
120-
mcp->chip.ngpio = 16;
121-
mcp->chip.label = "mcp23s18";
122-
123-
config = &mcp23x17_regmap;
124-
name = config->name;
113+
mcp->chip.label = info->label;
114+
name = info->regmap->name;
125115
break;
126116

127117
default:
128-
dev_err(dev, "invalid device type (%d)\n", type);
118+
dev_err(dev, "invalid device type (%d)\n", info->type);
129119
return -EINVAL;
130120
}
131121

132-
copy = devm_kmemdup(dev, config, sizeof(*config), GFP_KERNEL);
122+
mcp->reg_shift = info->reg_shift;
123+
mcp->chip.ngpio = info->ngpio;
124+
copy = devm_kmemdup(dev, info->regmap, sizeof(*info->regmap), GFP_KERNEL);
133125
if (!copy)
134126
return -ENOMEM;
135127

@@ -144,16 +136,16 @@ static int mcp23s08_spi_regmap_init(struct mcp23s08 *mcp, struct device *dev,
144136
static int mcp23s08_probe(struct spi_device *spi)
145137
{
146138
struct mcp23s08_driver_data *data;
139+
const struct mcp23s08_info *info;
147140
struct device *dev = &spi->dev;
148141
unsigned long spi_present_mask;
149142
unsigned int ngpio = 0;
150-
unsigned int type;
151143
unsigned int addr;
152144
int chips;
153145
int ret;
154146
u32 v;
155147

156-
type = (uintptr_t)spi_get_device_match_data(spi);
148+
info = spi_get_device_match_data(spi);
157149

158150
ret = device_property_read_u32(dev, "microchip,spi-present-mask", &v);
159151
if (ret) {
@@ -182,7 +174,7 @@ static int mcp23s08_probe(struct spi_device *spi)
182174
data->mcp[addr] = &data->chip[--chips];
183175
data->mcp[addr]->irq = spi->irq;
184176

185-
ret = mcp23s08_spi_regmap_init(data->mcp[addr], dev, addr, type);
177+
ret = mcp23s08_spi_regmap_init(data->mcp[addr], dev, addr, info);
186178
if (ret)
187179
return ret;
188180

@@ -192,7 +184,8 @@ static int mcp23s08_probe(struct spi_device *spi)
192184
if (!data->mcp[addr]->pinctrl_desc.name)
193185
return -ENOMEM;
194186

195-
ret = mcp23s08_probe_one(data->mcp[addr], dev, 0x40 | (addr << 1), type, -1);
187+
ret = mcp23s08_probe_one(data->mcp[addr], dev, 0x40 | (addr << 1),
188+
info->type, -1);
196189
if (ret < 0)
197190
return ret;
198191

@@ -203,36 +196,43 @@ static int mcp23s08_probe(struct spi_device *spi)
203196
return 0;
204197
}
205198

199+
static const struct mcp23s08_info mcp23s08_spi = {
200+
.regmap = &mcp23x08_regmap,
201+
.type = MCP_TYPE_S08,
202+
.ngpio = 8,
203+
.reg_shift = 0,
204+
};
205+
206+
static const struct mcp23s08_info mcp23s17_spi = {
207+
.regmap = &mcp23x17_regmap,
208+
.type = MCP_TYPE_S17,
209+
.ngpio = 16,
210+
.reg_shift = 1,
211+
};
212+
213+
static const struct mcp23s08_info mcp23s18_spi = {
214+
.regmap = &mcp23x17_regmap,
215+
.label = "mcp23s18",
216+
.type = MCP_TYPE_S18,
217+
.ngpio = 16,
218+
.reg_shift = 1,
219+
};
220+
206221
static const struct spi_device_id mcp23s08_ids[] = {
207-
{ "mcp23s08", MCP_TYPE_S08 },
208-
{ "mcp23s17", MCP_TYPE_S17 },
209-
{ "mcp23s18", MCP_TYPE_S18 },
222+
{ "mcp23s08", (kernel_ulong_t)&mcp23s08_spi },
223+
{ "mcp23s17", (kernel_ulong_t)&mcp23s17_spi },
224+
{ "mcp23s18", (kernel_ulong_t)&mcp23s18_spi },
210225
{ }
211226
};
212227
MODULE_DEVICE_TABLE(spi, mcp23s08_ids);
213228

214229
static const struct of_device_id mcp23s08_spi_of_match[] = {
215-
{
216-
.compatible = "microchip,mcp23s08",
217-
.data = (void *) MCP_TYPE_S08,
218-
},
219-
{
220-
.compatible = "microchip,mcp23s17",
221-
.data = (void *) MCP_TYPE_S17,
222-
},
223-
{
224-
.compatible = "microchip,mcp23s18",
225-
.data = (void *) MCP_TYPE_S18,
226-
},
230+
{ .compatible = "microchip,mcp23s08", .data = &mcp23s08_spi },
231+
{ .compatible = "microchip,mcp23s17", .data = &mcp23s17_spi },
232+
{ .compatible = "microchip,mcp23s18", .data = &mcp23s18_spi },
227233
/* NOTE: The use of the mcp prefix is deprecated and will be removed. */
228-
{
229-
.compatible = "mcp,mcp23s08",
230-
.data = (void *) MCP_TYPE_S08,
231-
},
232-
{
233-
.compatible = "mcp,mcp23s17",
234-
.data = (void *) MCP_TYPE_S17,
235-
},
234+
{ .compatible = "mcp,mcp23s08", .data = &mcp23s08_spi },
235+
{ .compatible = "mcp,mcp23s17", .data = &mcp23s17_spi },
236236
{ }
237237
};
238238
MODULE_DEVICE_TABLE(of, mcp23s08_spi_of_match);

0 commit comments

Comments
 (0)