Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

authored by

Biju Das and committed by
Linus Walleij
2e44555b b03f7aa8

+93 -96
+8
drivers/pinctrl/pinctrl-mcp23s08.h
··· 22 22 23 23 struct pinctrl_dev; 24 24 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 + 25 33 struct mcp23s08 { 26 34 u8 addr; 27 35 bool irq_active_high;
+41 -52
drivers/pinctrl/pinctrl-mcp23s08_i2c.c
··· 10 10 11 11 static int mcp230xx_probe(struct i2c_client *client) 12 12 { 13 + const struct mcp23s08_info *info; 13 14 struct device *dev = &client->dev; 14 15 struct mcp23s08 *mcp; 15 - unsigned int type; 16 16 int ret; 17 17 18 18 mcp = devm_kzalloc(dev, sizeof(*mcp), GFP_KERNEL); 19 19 if (!mcp) 20 20 return -ENOMEM; 21 21 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; 22 + info = i2c_get_match_data(client); 23 + if (!info) 24 + return dev_err_probe(dev, -EINVAL, "invalid device type\n"); 30 25 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 - } 49 - 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); 50 30 if (IS_ERR(mcp->regmap)) 51 31 return PTR_ERR(mcp->regmap); 52 32 53 33 mcp->irq = client->irq; 54 34 mcp->pinctrl_desc.name = "mcp23xxx-pinctrl"; 55 35 56 - ret = mcp23s08_probe_one(mcp, dev, client->addr, type, -1); 36 + ret = mcp23s08_probe_one(mcp, dev, client->addr, info->type, -1); 57 37 if (ret) 58 38 return ret; 59 39 ··· 42 62 return 0; 43 63 } 44 64 65 + static const struct mcp23s08_info mcp23008_i2c = { 66 + .regmap = &mcp23x08_regmap, 67 + .label = "mcp23008", 68 + .type = MCP_TYPE_008, 69 + .ngpio = 8, 70 + .reg_shift = 0, 71 + }; 72 + 73 + static const struct mcp23s08_info mcp23017_i2c = { 74 + .regmap = &mcp23x17_regmap, 75 + .label = "mcp23017", 76 + .type = MCP_TYPE_017, 77 + .ngpio = 16, 78 + .reg_shift = 1, 79 + }; 80 + 81 + static const struct mcp23s08_info mcp23018_i2c = { 82 + .regmap = &mcp23x17_regmap, 83 + .label = "mcp23018", 84 + .type = MCP_TYPE_018, 85 + .ngpio = 16, 86 + .reg_shift = 1, 87 + }; 88 + 45 89 static const struct i2c_device_id mcp230xx_id[] = { 46 - { "mcp23008", MCP_TYPE_008 }, 47 - { "mcp23017", MCP_TYPE_017 }, 48 - { "mcp23018", MCP_TYPE_018 }, 90 + { "mcp23008", (kernel_ulong_t)&mcp23008_i2c }, 91 + { "mcp23017", (kernel_ulong_t)&mcp23017_i2c }, 92 + { "mcp23018", (kernel_ulong_t)&mcp23018_i2c }, 49 93 { } 50 94 }; 51 95 MODULE_DEVICE_TABLE(i2c, mcp230xx_id); 52 96 53 97 static const struct of_device_id mcp23s08_i2c_of_match[] = { 54 - { 55 - .compatible = "microchip,mcp23008", 56 - .data = (void *) MCP_TYPE_008, 57 - }, 58 - { 59 - .compatible = "microchip,mcp23017", 60 - .data = (void *) MCP_TYPE_017, 61 - }, 62 - { 63 - .compatible = "microchip,mcp23018", 64 - .data = (void *) MCP_TYPE_018, 65 - }, 98 + { .compatible = "microchip,mcp23008", .data = &mcp23008_i2c }, 99 + { .compatible = "microchip,mcp23017", .data = &mcp23017_i2c }, 100 + { .compatible = "microchip,mcp23018", .data = &mcp23018_i2c }, 66 101 /* NOTE: The use of the mcp prefix is deprecated and will be removed. */ 67 - { 68 - .compatible = "mcp,mcp23008", 69 - .data = (void *) MCP_TYPE_008, 70 - }, 71 - { 72 - .compatible = "mcp,mcp23017", 73 - .data = (void *) MCP_TYPE_017, 74 - }, 102 + { .compatible = "mcp,mcp23008", .data = &mcp23008_i2c }, 103 + { .compatible = "mcp,mcp23017", .data = &mcp23017_i2c }, 75 104 { } 76 105 }; 77 106 MODULE_DEVICE_TABLE(of, mcp23s08_i2c_of_match);
+44 -44
drivers/pinctrl/pinctrl-mcp23s08_spi.c
··· 80 80 }; 81 81 82 82 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) 84 85 { 85 - const struct regmap_config *config; 86 86 struct regmap_config *copy; 87 87 const char *name; 88 88 89 - switch (type) { 89 + switch (info->type) { 90 90 case MCP_TYPE_S08: 91 - mcp->reg_shift = 0; 92 - mcp->chip.ngpio = 8; 93 91 mcp->chip.label = devm_kasprintf(dev, GFP_KERNEL, "mcp23s08.%d", addr); 94 92 if (!mcp->chip.label) 95 93 return -ENOMEM; 96 94 97 - config = &mcp23x08_regmap; 98 95 name = devm_kasprintf(dev, GFP_KERNEL, "%d", addr); 99 96 if (!name) 100 97 return -ENOMEM; ··· 99 102 break; 100 103 101 104 case MCP_TYPE_S17: 102 - mcp->reg_shift = 1; 103 - mcp->chip.ngpio = 16; 104 105 mcp->chip.label = devm_kasprintf(dev, GFP_KERNEL, "mcp23s17.%d", addr); 105 106 if (!mcp->chip.label) 106 107 return -ENOMEM; 107 108 108 - config = &mcp23x17_regmap; 109 109 name = devm_kasprintf(dev, GFP_KERNEL, "%d", addr); 110 110 if (!name) 111 111 return -ENOMEM; ··· 110 116 break; 111 117 112 118 case MCP_TYPE_S18: 113 - mcp->reg_shift = 1; 114 - mcp->chip.ngpio = 16; 115 - mcp->chip.label = "mcp23s18"; 116 - 117 - config = &mcp23x17_regmap; 118 - name = config->name; 119 + mcp->chip.label = info->label; 120 + name = info->regmap->name; 119 121 break; 120 122 121 123 default: 122 - dev_err(dev, "invalid device type (%d)\n", type); 124 + dev_err(dev, "invalid device type (%d)\n", info->type); 123 125 return -EINVAL; 124 126 } 125 127 126 - copy = devm_kmemdup(dev, config, sizeof(*config), GFP_KERNEL); 128 + mcp->reg_shift = info->reg_shift; 129 + mcp->chip.ngpio = info->ngpio; 130 + copy = devm_kmemdup(dev, info->regmap, sizeof(*info->regmap), GFP_KERNEL); 127 131 if (!copy) 128 132 return -ENOMEM; 129 133 ··· 136 144 static int mcp23s08_probe(struct spi_device *spi) 137 145 { 138 146 struct mcp23s08_driver_data *data; 147 + const struct mcp23s08_info *info; 139 148 struct device *dev = &spi->dev; 140 149 unsigned long spi_present_mask; 141 150 unsigned int ngpio = 0; 142 - unsigned int type; 143 151 unsigned int addr; 144 152 int chips; 145 153 int ret; 146 154 u32 v; 147 155 148 - type = (uintptr_t)spi_get_device_match_data(spi); 156 + info = spi_get_device_match_data(spi); 149 157 150 158 ret = device_property_read_u32(dev, "microchip,spi-present-mask", &v); 151 159 if (ret) { ··· 174 182 data->mcp[addr] = &data->chip[--chips]; 175 183 data->mcp[addr]->irq = spi->irq; 176 184 177 - ret = mcp23s08_spi_regmap_init(data->mcp[addr], dev, addr, type); 185 + ret = mcp23s08_spi_regmap_init(data->mcp[addr], dev, addr, info); 178 186 if (ret) 179 187 return ret; 180 188 ··· 184 192 if (!data->mcp[addr]->pinctrl_desc.name) 185 193 return -ENOMEM; 186 194 187 - ret = mcp23s08_probe_one(data->mcp[addr], dev, 0x40 | (addr << 1), type, -1); 195 + ret = mcp23s08_probe_one(data->mcp[addr], dev, 0x40 | (addr << 1), 196 + info->type, -1); 188 197 if (ret < 0) 189 198 return ret; 190 199 ··· 196 203 return 0; 197 204 } 198 205 206 + static const struct mcp23s08_info mcp23s08_spi = { 207 + .regmap = &mcp23x08_regmap, 208 + .type = MCP_TYPE_S08, 209 + .ngpio = 8, 210 + .reg_shift = 0, 211 + }; 212 + 213 + static const struct mcp23s08_info mcp23s17_spi = { 214 + .regmap = &mcp23x17_regmap, 215 + .type = MCP_TYPE_S17, 216 + .ngpio = 16, 217 + .reg_shift = 1, 218 + }; 219 + 220 + static const struct mcp23s08_info mcp23s18_spi = { 221 + .regmap = &mcp23x17_regmap, 222 + .label = "mcp23s18", 223 + .type = MCP_TYPE_S18, 224 + .ngpio = 16, 225 + .reg_shift = 1, 226 + }; 227 + 199 228 static const struct spi_device_id mcp23s08_ids[] = { 200 - { "mcp23s08", MCP_TYPE_S08 }, 201 - { "mcp23s17", MCP_TYPE_S17 }, 202 - { "mcp23s18", MCP_TYPE_S18 }, 229 + { "mcp23s08", (kernel_ulong_t)&mcp23s08_spi }, 230 + { "mcp23s17", (kernel_ulong_t)&mcp23s17_spi }, 231 + { "mcp23s18", (kernel_ulong_t)&mcp23s18_spi }, 203 232 { } 204 233 }; 205 234 MODULE_DEVICE_TABLE(spi, mcp23s08_ids); 206 235 207 236 static const struct of_device_id mcp23s08_spi_of_match[] = { 208 - { 209 - .compatible = "microchip,mcp23s08", 210 - .data = (void *) MCP_TYPE_S08, 211 - }, 212 - { 213 - .compatible = "microchip,mcp23s17", 214 - .data = (void *) MCP_TYPE_S17, 215 - }, 216 - { 217 - .compatible = "microchip,mcp23s18", 218 - .data = (void *) MCP_TYPE_S18, 219 - }, 237 + { .compatible = "microchip,mcp23s08", .data = &mcp23s08_spi }, 238 + { .compatible = "microchip,mcp23s17", .data = &mcp23s17_spi }, 239 + { .compatible = "microchip,mcp23s18", .data = &mcp23s18_spi }, 220 240 /* NOTE: The use of the mcp prefix is deprecated and will be removed. */ 221 - { 222 - .compatible = "mcp,mcp23s08", 223 - .data = (void *) MCP_TYPE_S08, 224 - }, 225 - { 226 - .compatible = "mcp,mcp23s17", 227 - .data = (void *) MCP_TYPE_S17, 228 - }, 241 + { .compatible = "mcp,mcp23s08", .data = &mcp23s08_spi }, 242 + { .compatible = "mcp,mcp23s17", .data = &mcp23s17_spi }, 229 243 { } 230 244 }; 231 245 MODULE_DEVICE_TABLE(of, mcp23s08_spi_of_match);