0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <linux/module.h>
0010 #include <linux/slab.h>
0011 #include <linux/of.h>
0012 #include <linux/regulator/machine.h>
0013 #include <linux/regulator/driver.h>
0014 #include <linux/regulator/of_regulator.h>
0015
0016 #include "internal.h"
0017
0018 static const char *const regulator_states[PM_SUSPEND_MAX + 1] = {
0019 [PM_SUSPEND_STANDBY] = "regulator-state-standby",
0020 [PM_SUSPEND_MEM] = "regulator-state-mem",
0021 [PM_SUSPEND_MAX] = "regulator-state-disk",
0022 };
0023
0024 static void fill_limit(int *limit, int val)
0025 {
0026 if (val)
0027 if (val == 1)
0028 *limit = REGULATOR_NOTIF_LIMIT_ENABLE;
0029 else
0030 *limit = val;
0031 else
0032 *limit = REGULATOR_NOTIF_LIMIT_DISABLE;
0033 }
0034
0035 static void of_get_regulator_prot_limits(struct device_node *np,
0036 struct regulation_constraints *constraints)
0037 {
0038 u32 pval;
0039 int i;
0040 static const char *const props[] = {
0041 "regulator-oc-%s-microamp",
0042 "regulator-ov-%s-microvolt",
0043 "regulator-temp-%s-kelvin",
0044 "regulator-uv-%s-microvolt",
0045 };
0046 struct notification_limit *limits[] = {
0047 &constraints->over_curr_limits,
0048 &constraints->over_voltage_limits,
0049 &constraints->temp_limits,
0050 &constraints->under_voltage_limits,
0051 };
0052 bool set[4] = {0};
0053
0054
0055 for (i = 0; i < ARRAY_SIZE(props); i++) {
0056 char prop[255];
0057 bool found;
0058 int j;
0059 static const char *const lvl[] = {
0060 "protection", "error", "warn"
0061 };
0062 int *l[] = {
0063 &limits[i]->prot, &limits[i]->err, &limits[i]->warn,
0064 };
0065
0066 for (j = 0; j < ARRAY_SIZE(lvl); j++) {
0067 snprintf(prop, 255, props[i], lvl[j]);
0068 found = !of_property_read_u32(np, prop, &pval);
0069 if (found)
0070 fill_limit(l[j], pval);
0071 set[i] |= found;
0072 }
0073 }
0074 constraints->over_current_detection = set[0];
0075 constraints->over_voltage_detection = set[1];
0076 constraints->over_temp_detection = set[2];
0077 constraints->under_voltage_detection = set[3];
0078 }
0079
0080 static int of_get_regulation_constraints(struct device *dev,
0081 struct device_node *np,
0082 struct regulator_init_data **init_data,
0083 const struct regulator_desc *desc)
0084 {
0085 struct regulation_constraints *constraints = &(*init_data)->constraints;
0086 struct regulator_state *suspend_state;
0087 struct device_node *suspend_np;
0088 unsigned int mode;
0089 int ret, i, len;
0090 int n_phandles;
0091 u32 pval;
0092
0093 n_phandles = of_count_phandle_with_args(np, "regulator-coupled-with",
0094 NULL);
0095 n_phandles = max(n_phandles, 0);
0096
0097 constraints->name = of_get_property(np, "regulator-name", NULL);
0098
0099 if (!of_property_read_u32(np, "regulator-min-microvolt", &pval))
0100 constraints->min_uV = pval;
0101
0102 if (!of_property_read_u32(np, "regulator-max-microvolt", &pval))
0103 constraints->max_uV = pval;
0104
0105
0106 if (constraints->min_uV != constraints->max_uV)
0107 constraints->valid_ops_mask |= REGULATOR_CHANGE_VOLTAGE;
0108
0109
0110 if (constraints->min_uV && constraints->max_uV)
0111 constraints->apply_uV = true;
0112
0113 if (!of_property_read_u32(np, "regulator-microvolt-offset", &pval))
0114 constraints->uV_offset = pval;
0115 if (!of_property_read_u32(np, "regulator-min-microamp", &pval))
0116 constraints->min_uA = pval;
0117 if (!of_property_read_u32(np, "regulator-max-microamp", &pval))
0118 constraints->max_uA = pval;
0119
0120 if (!of_property_read_u32(np, "regulator-input-current-limit-microamp",
0121 &pval))
0122 constraints->ilim_uA = pval;
0123
0124
0125 if (constraints->min_uA != constraints->max_uA)
0126 constraints->valid_ops_mask |= REGULATOR_CHANGE_CURRENT;
0127
0128 constraints->boot_on = of_property_read_bool(np, "regulator-boot-on");
0129 constraints->always_on = of_property_read_bool(np, "regulator-always-on");
0130 if (!constraints->always_on)
0131 constraints->valid_ops_mask |= REGULATOR_CHANGE_STATUS;
0132
0133 constraints->pull_down = of_property_read_bool(np, "regulator-pull-down");
0134
0135 if (of_property_read_bool(np, "regulator-allow-bypass"))
0136 constraints->valid_ops_mask |= REGULATOR_CHANGE_BYPASS;
0137
0138 if (of_property_read_bool(np, "regulator-allow-set-load"))
0139 constraints->valid_ops_mask |= REGULATOR_CHANGE_DRMS;
0140
0141 ret = of_property_read_u32(np, "regulator-ramp-delay", &pval);
0142 if (!ret) {
0143 if (pval)
0144 constraints->ramp_delay = pval;
0145 else
0146 constraints->ramp_disable = true;
0147 }
0148
0149 ret = of_property_read_u32(np, "regulator-settling-time-us", &pval);
0150 if (!ret)
0151 constraints->settling_time = pval;
0152
0153 ret = of_property_read_u32(np, "regulator-settling-time-up-us", &pval);
0154 if (!ret)
0155 constraints->settling_time_up = pval;
0156 if (constraints->settling_time_up && constraints->settling_time) {
0157 pr_warn("%pOFn: ambiguous configuration for settling time, ignoring 'regulator-settling-time-up-us'\n",
0158 np);
0159 constraints->settling_time_up = 0;
0160 }
0161
0162 ret = of_property_read_u32(np, "regulator-settling-time-down-us",
0163 &pval);
0164 if (!ret)
0165 constraints->settling_time_down = pval;
0166 if (constraints->settling_time_down && constraints->settling_time) {
0167 pr_warn("%pOFn: ambiguous configuration for settling time, ignoring 'regulator-settling-time-down-us'\n",
0168 np);
0169 constraints->settling_time_down = 0;
0170 }
0171
0172 ret = of_property_read_u32(np, "regulator-enable-ramp-delay", &pval);
0173 if (!ret)
0174 constraints->enable_time = pval;
0175
0176 constraints->soft_start = of_property_read_bool(np,
0177 "regulator-soft-start");
0178 ret = of_property_read_u32(np, "regulator-active-discharge", &pval);
0179 if (!ret) {
0180 constraints->active_discharge =
0181 (pval) ? REGULATOR_ACTIVE_DISCHARGE_ENABLE :
0182 REGULATOR_ACTIVE_DISCHARGE_DISABLE;
0183 }
0184
0185 if (!of_property_read_u32(np, "regulator-initial-mode", &pval)) {
0186 if (desc && desc->of_map_mode) {
0187 mode = desc->of_map_mode(pval);
0188 if (mode == REGULATOR_MODE_INVALID)
0189 pr_err("%pOFn: invalid mode %u\n", np, pval);
0190 else
0191 constraints->initial_mode = mode;
0192 } else {
0193 pr_warn("%pOFn: mapping for mode %d not defined\n",
0194 np, pval);
0195 }
0196 }
0197
0198 len = of_property_count_elems_of_size(np, "regulator-allowed-modes",
0199 sizeof(u32));
0200 if (len > 0) {
0201 if (desc && desc->of_map_mode) {
0202 for (i = 0; i < len; i++) {
0203 ret = of_property_read_u32_index(np,
0204 "regulator-allowed-modes", i, &pval);
0205 if (ret) {
0206 pr_err("%pOFn: couldn't read allowed modes index %d, ret=%d\n",
0207 np, i, ret);
0208 break;
0209 }
0210 mode = desc->of_map_mode(pval);
0211 if (mode == REGULATOR_MODE_INVALID)
0212 pr_err("%pOFn: invalid regulator-allowed-modes element %u\n",
0213 np, pval);
0214 else
0215 constraints->valid_modes_mask |= mode;
0216 }
0217 if (constraints->valid_modes_mask)
0218 constraints->valid_ops_mask
0219 |= REGULATOR_CHANGE_MODE;
0220 } else {
0221 pr_warn("%pOFn: mode mapping not defined\n", np);
0222 }
0223 }
0224
0225 if (!of_property_read_u32(np, "regulator-system-load", &pval))
0226 constraints->system_load = pval;
0227
0228 if (n_phandles) {
0229 constraints->max_spread = devm_kzalloc(dev,
0230 sizeof(*constraints->max_spread) * n_phandles,
0231 GFP_KERNEL);
0232
0233 if (!constraints->max_spread)
0234 return -ENOMEM;
0235
0236 of_property_read_u32_array(np, "regulator-coupled-max-spread",
0237 constraints->max_spread, n_phandles);
0238 }
0239
0240 if (!of_property_read_u32(np, "regulator-max-step-microvolt",
0241 &pval))
0242 constraints->max_uV_step = pval;
0243
0244 constraints->over_current_protection = of_property_read_bool(np,
0245 "regulator-over-current-protection");
0246
0247 of_get_regulator_prot_limits(np, constraints);
0248
0249 for (i = 0; i < ARRAY_SIZE(regulator_states); i++) {
0250 switch (i) {
0251 case PM_SUSPEND_MEM:
0252 suspend_state = &constraints->state_mem;
0253 break;
0254 case PM_SUSPEND_MAX:
0255 suspend_state = &constraints->state_disk;
0256 break;
0257 case PM_SUSPEND_STANDBY:
0258 suspend_state = &constraints->state_standby;
0259 break;
0260 case PM_SUSPEND_ON:
0261 case PM_SUSPEND_TO_IDLE:
0262 default:
0263 continue;
0264 }
0265
0266 suspend_np = of_get_child_by_name(np, regulator_states[i]);
0267 if (!suspend_np)
0268 continue;
0269 if (!suspend_state) {
0270 of_node_put(suspend_np);
0271 continue;
0272 }
0273
0274 if (!of_property_read_u32(suspend_np, "regulator-mode",
0275 &pval)) {
0276 if (desc && desc->of_map_mode) {
0277 mode = desc->of_map_mode(pval);
0278 if (mode == REGULATOR_MODE_INVALID)
0279 pr_err("%pOFn: invalid mode %u\n",
0280 np, pval);
0281 else
0282 suspend_state->mode = mode;
0283 } else {
0284 pr_warn("%pOFn: mapping for mode %d not defined\n",
0285 np, pval);
0286 }
0287 }
0288
0289 if (of_property_read_bool(suspend_np,
0290 "regulator-on-in-suspend"))
0291 suspend_state->enabled = ENABLE_IN_SUSPEND;
0292 else if (of_property_read_bool(suspend_np,
0293 "regulator-off-in-suspend"))
0294 suspend_state->enabled = DISABLE_IN_SUSPEND;
0295
0296 if (!of_property_read_u32(suspend_np,
0297 "regulator-suspend-min-microvolt", &pval))
0298 suspend_state->min_uV = pval;
0299
0300 if (!of_property_read_u32(suspend_np,
0301 "regulator-suspend-max-microvolt", &pval))
0302 suspend_state->max_uV = pval;
0303
0304 if (!of_property_read_u32(suspend_np,
0305 "regulator-suspend-microvolt", &pval))
0306 suspend_state->uV = pval;
0307 else
0308 suspend_state->uV = suspend_state->min_uV;
0309
0310 if (of_property_read_bool(suspend_np,
0311 "regulator-changeable-in-suspend"))
0312 suspend_state->changeable = true;
0313
0314 if (i == PM_SUSPEND_MEM)
0315 constraints->initial_state = PM_SUSPEND_MEM;
0316
0317 of_node_put(suspend_np);
0318 suspend_state = NULL;
0319 suspend_np = NULL;
0320 }
0321
0322 return 0;
0323 }
0324
0325
0326
0327
0328
0329
0330
0331
0332
0333
0334
0335 struct regulator_init_data *of_get_regulator_init_data(struct device *dev,
0336 struct device_node *node,
0337 const struct regulator_desc *desc)
0338 {
0339 struct regulator_init_data *init_data;
0340
0341 if (!node)
0342 return NULL;
0343
0344 init_data = devm_kzalloc(dev, sizeof(*init_data), GFP_KERNEL);
0345 if (!init_data)
0346 return NULL;
0347
0348 if (of_get_regulation_constraints(dev, node, &init_data, desc))
0349 return NULL;
0350
0351 return init_data;
0352 }
0353 EXPORT_SYMBOL_GPL(of_get_regulator_init_data);
0354
0355 struct devm_of_regulator_matches {
0356 struct of_regulator_match *matches;
0357 unsigned int num_matches;
0358 };
0359
0360 static void devm_of_regulator_put_matches(struct device *dev, void *res)
0361 {
0362 struct devm_of_regulator_matches *devm_matches = res;
0363 int i;
0364
0365 for (i = 0; i < devm_matches->num_matches; i++)
0366 of_node_put(devm_matches->matches[i].of_node);
0367 }
0368
0369
0370
0371
0372
0373
0374
0375
0376
0377
0378
0379
0380
0381
0382
0383
0384
0385
0386
0387 int of_regulator_match(struct device *dev, struct device_node *node,
0388 struct of_regulator_match *matches,
0389 unsigned int num_matches)
0390 {
0391 unsigned int count = 0;
0392 unsigned int i;
0393 const char *name;
0394 struct device_node *child;
0395 struct devm_of_regulator_matches *devm_matches;
0396
0397 if (!dev || !node)
0398 return -EINVAL;
0399
0400 devm_matches = devres_alloc(devm_of_regulator_put_matches,
0401 sizeof(struct devm_of_regulator_matches),
0402 GFP_KERNEL);
0403 if (!devm_matches)
0404 return -ENOMEM;
0405
0406 devm_matches->matches = matches;
0407 devm_matches->num_matches = num_matches;
0408
0409 devres_add(dev, devm_matches);
0410
0411 for (i = 0; i < num_matches; i++) {
0412 struct of_regulator_match *match = &matches[i];
0413 match->init_data = NULL;
0414 match->of_node = NULL;
0415 }
0416
0417 for_each_child_of_node(node, child) {
0418 name = of_get_property(child,
0419 "regulator-compatible", NULL);
0420 if (!name)
0421 name = child->name;
0422 for (i = 0; i < num_matches; i++) {
0423 struct of_regulator_match *match = &matches[i];
0424 if (match->of_node)
0425 continue;
0426
0427 if (strcmp(match->name, name))
0428 continue;
0429
0430 match->init_data =
0431 of_get_regulator_init_data(dev, child,
0432 match->desc);
0433 if (!match->init_data) {
0434 dev_err(dev,
0435 "failed to parse DT for regulator %pOFn\n",
0436 child);
0437 of_node_put(child);
0438 return -EINVAL;
0439 }
0440 match->of_node = of_node_get(child);
0441 count++;
0442 break;
0443 }
0444 }
0445
0446 return count;
0447 }
0448 EXPORT_SYMBOL_GPL(of_regulator_match);
0449
0450 static struct
0451 device_node *regulator_of_get_init_node(struct device *dev,
0452 const struct regulator_desc *desc)
0453 {
0454 struct device_node *search, *child;
0455 const char *name;
0456
0457 if (!dev->of_node || !desc->of_match)
0458 return NULL;
0459
0460 if (desc->regulators_node) {
0461 search = of_get_child_by_name(dev->of_node,
0462 desc->regulators_node);
0463 } else {
0464 search = of_node_get(dev->of_node);
0465
0466 if (!strcmp(desc->of_match, search->name))
0467 return search;
0468 }
0469
0470 if (!search) {
0471 dev_dbg(dev, "Failed to find regulator container node '%s'\n",
0472 desc->regulators_node);
0473 return NULL;
0474 }
0475
0476 for_each_available_child_of_node(search, child) {
0477 name = of_get_property(child, "regulator-compatible", NULL);
0478 if (!name) {
0479 if (!desc->of_match_full_name)
0480 name = child->name;
0481 else
0482 name = child->full_name;
0483 }
0484
0485 if (!strcmp(desc->of_match, name)) {
0486 of_node_put(search);
0487
0488
0489
0490
0491 return child;
0492 }
0493 }
0494
0495 of_node_put(search);
0496
0497 return NULL;
0498 }
0499
0500 struct regulator_init_data *regulator_of_get_init_data(struct device *dev,
0501 const struct regulator_desc *desc,
0502 struct regulator_config *config,
0503 struct device_node **node)
0504 {
0505 struct device_node *child;
0506 struct regulator_init_data *init_data = NULL;
0507
0508 child = regulator_of_get_init_node(dev, desc);
0509 if (!child)
0510 return NULL;
0511
0512 init_data = of_get_regulator_init_data(dev, child, desc);
0513 if (!init_data) {
0514 dev_err(dev, "failed to parse DT for regulator %pOFn\n", child);
0515 goto error;
0516 }
0517
0518 if (desc->of_parse_cb) {
0519 int ret;
0520
0521 ret = desc->of_parse_cb(child, desc, config);
0522 if (ret) {
0523 if (ret == -EPROBE_DEFER) {
0524 of_node_put(child);
0525 return ERR_PTR(-EPROBE_DEFER);
0526 }
0527 dev_err(dev,
0528 "driver callback failed to parse DT for regulator %pOFn\n",
0529 child);
0530 goto error;
0531 }
0532 }
0533
0534 *node = child;
0535
0536 return init_data;
0537
0538 error:
0539 of_node_put(child);
0540
0541 return NULL;
0542 }
0543
0544 struct regulator_dev *of_find_regulator_by_node(struct device_node *np)
0545 {
0546 struct device *dev;
0547
0548 dev = class_find_device_by_of_node(®ulator_class, np);
0549
0550 return dev ? dev_to_rdev(dev) : NULL;
0551 }
0552
0553
0554
0555
0556 int of_get_n_coupled(struct regulator_dev *rdev)
0557 {
0558 struct device_node *node = rdev->dev.of_node;
0559 int n_phandles;
0560
0561 n_phandles = of_count_phandle_with_args(node,
0562 "regulator-coupled-with",
0563 NULL);
0564
0565 return (n_phandles > 0) ? n_phandles : 0;
0566 }
0567
0568
0569 static bool of_coupling_find_node(struct device_node *src,
0570 struct device_node *to_find,
0571 int *index)
0572 {
0573 int n_phandles, i;
0574 bool found = false;
0575
0576 n_phandles = of_count_phandle_with_args(src,
0577 "regulator-coupled-with",
0578 NULL);
0579
0580 for (i = 0; i < n_phandles; i++) {
0581 struct device_node *tmp = of_parse_phandle(src,
0582 "regulator-coupled-with", i);
0583
0584 if (!tmp)
0585 break;
0586
0587
0588 if (tmp == to_find)
0589 found = true;
0590
0591 of_node_put(tmp);
0592
0593 if (found) {
0594 *index = i;
0595 break;
0596 }
0597 }
0598
0599 return found;
0600 }
0601
0602
0603
0604
0605
0606
0607
0608
0609
0610
0611
0612
0613
0614
0615 bool of_check_coupling_data(struct regulator_dev *rdev)
0616 {
0617 struct device_node *node = rdev->dev.of_node;
0618 int n_phandles = of_get_n_coupled(rdev);
0619 struct device_node *c_node;
0620 int index;
0621 int i;
0622 bool ret = true;
0623
0624
0625 for (i = 0; i < n_phandles; i++) {
0626 int max_spread = rdev->constraints->max_spread[i];
0627 int c_max_spread, c_n_phandles;
0628
0629 if (max_spread <= 0) {
0630 dev_err(&rdev->dev, "max_spread value invalid\n");
0631 return false;
0632 }
0633
0634 c_node = of_parse_phandle(node,
0635 "regulator-coupled-with", i);
0636
0637 if (!c_node)
0638 ret = false;
0639
0640 c_n_phandles = of_count_phandle_with_args(c_node,
0641 "regulator-coupled-with",
0642 NULL);
0643
0644 if (c_n_phandles != n_phandles) {
0645 dev_err(&rdev->dev, "number of coupled reg phandles mismatch\n");
0646 ret = false;
0647 goto clean;
0648 }
0649
0650 if (!of_coupling_find_node(c_node, node, &index)) {
0651 dev_err(&rdev->dev, "missing 2-way linking for coupled regulators\n");
0652 ret = false;
0653 goto clean;
0654 }
0655
0656 if (of_property_read_u32_index(c_node, "regulator-coupled-max-spread",
0657 index, &c_max_spread)) {
0658 ret = false;
0659 goto clean;
0660 }
0661
0662 if (c_max_spread != max_spread) {
0663 dev_err(&rdev->dev,
0664 "coupled regulators max_spread mismatch\n");
0665 ret = false;
0666 goto clean;
0667 }
0668
0669 clean:
0670 of_node_put(c_node);
0671 if (!ret)
0672 break;
0673 }
0674
0675 return ret;
0676 }
0677
0678
0679
0680
0681
0682
0683
0684
0685
0686
0687 struct regulator_dev *of_parse_coupled_regulator(struct regulator_dev *rdev,
0688 int index)
0689 {
0690 struct device_node *node = rdev->dev.of_node;
0691 struct device_node *c_node;
0692 struct regulator_dev *c_rdev;
0693
0694 c_node = of_parse_phandle(node, "regulator-coupled-with", index);
0695 if (!c_node)
0696 return NULL;
0697
0698 c_rdev = of_find_regulator_by_node(c_node);
0699
0700 of_node_put(c_node);
0701
0702 return c_rdev;
0703 }