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

regulator: max1586: add device-tree support

Add device-tree support to max1586.
The driver can still be used with the legacy platform data, or the new
device-tree way.

This work is heavily inspired by the device-tree support of its cousin
max8660 driver.

Signed-off-by: Robert Jarzmik <robert.jarzmik@free.fr>
Signed-off-by: Mark Brown <broonie@kernel.org>

authored by

Robert Jarzmik and committed by
Mark Brown
4e005179 7d1311b9

+80 -3
+79 -2
drivers/regulator/max1586.c
··· 24 24 #include <linux/regulator/driver.h> 25 25 #include <linux/slab.h> 26 26 #include <linux/regulator/max1586.h> 27 + #include <linux/of_device.h> 28 + #include <linux/regulator/of_regulator.h> 27 29 28 30 #define MAX1586_V3_MAX_VSEL 31 29 31 #define MAX1586_V6_MAX_VSEL 3 ··· 159 157 }, 160 158 }; 161 159 160 + int of_get_max1586_platform_data(struct device *dev, 161 + struct max1586_platform_data *pdata) 162 + { 163 + struct max1586_subdev_data *sub; 164 + struct of_regulator_match rmatch[ARRAY_SIZE(max1586_reg)]; 165 + struct device_node *np = dev->of_node; 166 + int i, matched; 167 + 168 + if (of_property_read_u32(np, "v3-gain", 169 + &pdata->v3_gain) < 0) { 170 + dev_err(dev, "%s has no 'v3-gain' property\n", np->full_name); 171 + return -EINVAL; 172 + } 173 + 174 + np = of_get_child_by_name(np, "regulators"); 175 + if (!np) { 176 + dev_err(dev, "missing 'regulators' subnode in DT\n"); 177 + return -EINVAL; 178 + } 179 + 180 + for (i = 0; i < ARRAY_SIZE(rmatch); i++) 181 + rmatch[i].name = max1586_reg[i].name; 182 + 183 + matched = of_regulator_match(dev, np, rmatch, ARRAY_SIZE(rmatch)); 184 + of_node_put(np); 185 + /* 186 + * If matched is 0, ie. neither Output_V3 nor Output_V6 have been found, 187 + * return 0, which signals the normal situation where no subregulator is 188 + * available. This is normal because the max1586 doesn't provide any 189 + * readback support, so the subregulators can't report any status 190 + * anyway. If matched < 0, return the error. 191 + */ 192 + if (matched <= 0) 193 + return matched; 194 + 195 + pdata->subdevs = devm_kzalloc(dev, sizeof(struct max1586_subdev_data) * 196 + matched, GFP_KERNEL); 197 + if (!pdata->subdevs) 198 + return -ENOMEM; 199 + 200 + pdata->num_subdevs = matched; 201 + sub = pdata->subdevs; 202 + 203 + for (i = 0; i < matched; i++) { 204 + sub->id = i; 205 + sub->name = rmatch[i].of_node->name; 206 + sub->platform_data = rmatch[i].init_data; 207 + sub++; 208 + } 209 + 210 + return 0; 211 + } 212 + 213 + static const struct of_device_id max1586_of_match[] = { 214 + { .compatible = "maxim,max1586", }, 215 + {}, 216 + }; 217 + MODULE_DEVICE_TABLE(of, max1586_of_match); 218 + 162 219 static int max1586_pmic_probe(struct i2c_client *client, 163 220 const struct i2c_device_id *i2c_id) 164 221 { 165 - struct max1586_platform_data *pdata = dev_get_platdata(&client->dev); 222 + struct max1586_platform_data *pdata, pdata_of; 166 223 struct regulator_config config = { }; 167 224 struct max1586_data *max1586; 168 - int i, id; 225 + int i, id, ret; 226 + const struct of_device_id *match; 227 + 228 + pdata = dev_get_platdata(&client->dev); 229 + if (client->dev.of_node && !pdata) { 230 + match = of_match_device(of_match_ptr(max1586_of_match), 231 + &client->dev); 232 + if (!match) { 233 + dev_err(&client->dev, "Error: No device match found\n"); 234 + return -ENODEV; 235 + } 236 + ret = of_get_max1586_platform_data(&client->dev, &pdata_of); 237 + if (ret < 0) 238 + return ret; 239 + pdata = &pdata_of; 240 + } 169 241 170 242 max1586 = devm_kzalloc(&client->dev, sizeof(struct max1586_data), 171 243 GFP_KERNEL); ··· 305 229 .driver = { 306 230 .name = "max1586", 307 231 .owner = THIS_MODULE, 232 + .of_match_table = of_match_ptr(max1586_of_match), 308 233 }, 309 234 .id_table = max1586_id, 310 235 };
+1 -1
include/linux/regulator/max1586.h
··· 40 40 */ 41 41 struct max1586_subdev_data { 42 42 int id; 43 - char *name; 43 + const char *name; 44 44 struct regulator_init_data *platform_data; 45 45 }; 46 46