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