Lines Matching +full:system +full:- +full:critical +full:- +full:regulator
1 // SPDX-License-Identifier: GPL-2.0-or-later
3 // core.c -- Voltage/Current Regulator framework.
24 #include <linux/regulator/of_regulator.h>
25 #include <linux/regulator/consumer.h>
26 #include <linux/regulator/coupler.h>
27 #include <linux/regulator/driver.h>
28 #include <linux/regulator/machine.h>
32 #include <trace/events/regulator.h>
58 struct regulator_dev *regulator; member
87 static int _regulator_disable(struct regulator *regulator);
97 static struct regulator *create_regulator(struct regulator_dev *rdev,
100 static void destroy_regulator(struct regulator *regulator);
101 static void _regulator_put(struct regulator *regulator);
105 if (rdev->constraints && rdev->constraints->name) in rdev_get_name()
106 return rdev->constraints->name; in rdev_get_name()
107 else if (rdev->desc->name) in rdev_get_name()
108 return rdev->desc->name; in rdev_get_name()
121 if (!rdev->constraints) { in regulator_ops_is_valid()
126 if (rdev->constraints->valid_ops_mask & ops) in regulator_ops_is_valid()
133 * regulator_lock_nested - lock a single regulator
134 * @rdev: regulator source
138 * a single regulator and its mutex will be locked only
151 if (!ww_mutex_trylock(&rdev->mutex, ww_ctx)) { in regulator_lock_nested()
152 if (rdev->mutex_owner == current) in regulator_lock_nested()
153 rdev->ref_cnt++; in regulator_lock_nested()
159 ret = ww_mutex_lock(&rdev->mutex, ww_ctx); in regulator_lock_nested()
166 if (lock && ret != -EDEADLK) { in regulator_lock_nested()
167 rdev->ref_cnt++; in regulator_lock_nested()
168 rdev->mutex_owner = current; in regulator_lock_nested()
177 * regulator_lock - lock a single regulator
178 * @rdev: regulator source
181 * a single regulator and its mutex will be locked only
192 * regulator_unlock - unlock a single regulator
202 if (--rdev->ref_cnt == 0) { in regulator_unlock()
203 rdev->mutex_owner = NULL; in regulator_unlock()
204 ww_mutex_unlock(&rdev->mutex); in regulator_unlock()
207 WARN_ON_ONCE(rdev->ref_cnt < 0); in regulator_unlock()
213 * regulator_lock_two - lock two regulators
214 * @rdev1: first regulator
215 * @rdev2: second regulator
233 if (ret != -EDEADLOCK) { in regulator_lock_two()
243 ww_mutex_lock_slow(&contended->mutex, ww_ctx); in regulator_lock_two()
244 contended->ref_cnt++; in regulator_lock_two()
245 contended->mutex_owner = current; in regulator_lock_two()
249 if (ret != -EDEADLOCK) { in regulator_lock_two()
260 * regulator_unlock_two - unlock two regulators
261 * @rdev1: first regulator
262 * @rdev2: second regulator
282 for (i = 1; i < rdev->coupling_desc.n_coupled; i++) { in regulator_supply_is_couple()
283 c_rdev = rdev->coupling_desc.coupled_rdevs[i]; in regulator_supply_is_couple()
285 if (rdev->supply->rdev == c_rdev) in regulator_supply_is_couple()
298 for (i = n_coupled; i > 0; i--) { in regulator_unlock_recursive()
299 c_rdev = rdev->coupling_desc.coupled_rdevs[i - 1]; in regulator_unlock_recursive()
304 if (c_rdev->supply && !regulator_supply_is_couple(c_rdev)) { in regulator_unlock_recursive()
305 supply_rdev = c_rdev->supply->rdev; in regulator_unlock_recursive()
306 supply_n_coupled = supply_rdev->coupling_desc.n_coupled; in regulator_unlock_recursive()
324 for (i = 0; i < rdev->coupling_desc.n_coupled; i++) { in regulator_lock_recursive()
325 c_rdev = rdev->coupling_desc.coupled_rdevs[i]; in regulator_lock_recursive()
333 if (err == -EDEADLK) { in regulator_lock_recursive()
339 WARN_ON_ONCE(err != -EALREADY); in regulator_lock_recursive()
345 if (c_rdev->supply && !regulator_supply_is_couple(c_rdev)) { in regulator_lock_recursive()
346 err = regulator_lock_recursive(c_rdev->supply->rdev, in regulator_lock_recursive()
366 * regulator_unlock_dependent - unlock regulator's suppliers and coupled
368 * @rdev: regulator source
376 regulator_unlock_recursive(rdev, rdev->coupling_desc.n_coupled); in regulator_unlock_dependent()
381 * regulator_lock_dependent - lock regulator's suppliers and coupled regulators
382 * @rdev: regulator source
401 ww_mutex_lock_slow(&new_contended_rdev->mutex, ww_ctx); in regulator_lock_dependent()
403 old_contended_rdev->ref_cnt++; in regulator_lock_dependent()
404 old_contended_rdev->mutex_owner = current; in regulator_lock_dependent()
415 } while (err == -EDEADLK); in regulator_lock_dependent()
423 * of_get_child_regulator - get a child regulator device node
426 * @prop_name: Combination regulator supply name and "-supply"
429 * Extract the child regulator device node corresponding to the supply name.
430 * returns the device node corresponding to the regulator if found, else
458 * of_get_regulator - get a regulator device node based on supply name
459 * @dev: Device pointer for the consumer (of regulator) device
460 * @supply: regulator supply name
462 * Extract the regulator device node corresponding to the supply name.
463 * returns the device node corresponding to the regulator if found, else
471 dev_dbg(dev, "Looking up %s-supply from device tree\n", supply); in of_get_regulator()
473 snprintf(prop_name, 64, "%s-supply", supply); in of_get_regulator()
474 regnode = of_parse_phandle(dev->of_node, prop_name, 0); in of_get_regulator()
477 regnode = of_get_child_regulator(dev->of_node, prop_name); in of_get_regulator()
482 prop_name, dev->of_node); in of_get_regulator()
496 return -EPERM; in regulator_check_voltage()
499 if (*max_uV > rdev->constraints->max_uV) in regulator_check_voltage()
500 *max_uV = rdev->constraints->max_uV; in regulator_check_voltage()
501 if (*min_uV < rdev->constraints->min_uV) in regulator_check_voltage()
502 *min_uV = rdev->constraints->min_uV; in regulator_check_voltage()
505 rdev_err(rdev, "unsupportable voltage range: %d-%duV\n", in regulator_check_voltage()
507 return -EINVAL; in regulator_check_voltage()
520 * regulator consumers
526 struct regulator *regulator; in regulator_check_consumers() local
529 list_for_each_entry(regulator, &rdev->consumer_list, list) { in regulator_check_consumers()
530 voltage = ®ulator->voltage[state]; in regulator_check_consumers()
535 if (!voltage->min_uV && !voltage->max_uV) in regulator_check_consumers()
538 if (*max_uV > voltage->max_uV) in regulator_check_consumers()
539 *max_uV = voltage->max_uV; in regulator_check_consumers()
540 if (*min_uV < voltage->min_uV) in regulator_check_consumers()
541 *min_uV = voltage->min_uV; in regulator_check_consumers()
545 rdev_err(rdev, "Restricting voltage, %u-%uuV\n", in regulator_check_consumers()
547 return -EINVAL; in regulator_check_consumers()
561 return -EPERM; in regulator_check_current_limit()
564 if (*max_uA > rdev->constraints->max_uA) in regulator_check_current_limit()
565 *max_uA = rdev->constraints->max_uA; in regulator_check_current_limit()
566 if (*min_uA < rdev->constraints->min_uA) in regulator_check_current_limit()
567 *min_uA = rdev->constraints->min_uA; in regulator_check_current_limit()
570 rdev_err(rdev, "unsupportable current range: %d-%duA\n", in regulator_check_current_limit()
572 return -EINVAL; in regulator_check_current_limit()
590 return -EINVAL; in regulator_mode_constrain()
595 return -EPERM; in regulator_mode_constrain()
603 if (rdev->constraints->valid_modes_mask & *mode) in regulator_mode_constrain()
608 return -EINVAL; in regulator_mode_constrain()
614 if (rdev->constraints == NULL) in regulator_get_suspend_state()
619 return &rdev->constraints->state_standby; in regulator_get_suspend_state()
621 return &rdev->constraints->state_mem; in regulator_get_suspend_state()
623 return &rdev->constraints->state_disk; in regulator_get_suspend_state()
642 if (rstate->enabled != ENABLE_IN_SUSPEND && in regulator_get_suspend_state_check()
643 rstate->enabled != DISABLE_IN_SUSPEND) { in regulator_get_suspend_state_check()
644 if (rdev->desc->ops->set_suspend_voltage || in regulator_get_suspend_state_check()
645 rdev->desc->ops->set_suspend_mode) in regulator_get_suspend_state_check()
747 status = rdev->desc->ops->get_status(rdev); in status_show()
780 return -ERANGE; in status_show()
792 if (!rdev->constraints) in min_microamps_show()
795 return sprintf(buf, "%d\n", rdev->constraints->min_uA); in min_microamps_show()
804 if (!rdev->constraints) in max_microamps_show()
807 return sprintf(buf, "%d\n", rdev->constraints->max_uA); in max_microamps_show()
816 if (!rdev->constraints) in min_microvolts_show()
819 return sprintf(buf, "%d\n", rdev->constraints->min_uV); in min_microvolts_show()
828 if (!rdev->constraints) in max_microvolts_show()
831 return sprintf(buf, "%d\n", rdev->constraints->max_uV); in max_microvolts_show()
839 struct regulator *regulator; in requested_microamps_show() local
843 list_for_each_entry(regulator, &rdev->consumer_list, list) { in requested_microamps_show()
844 if (regulator->enable_count) in requested_microamps_show()
845 uA += regulator->uA_load; in requested_microamps_show()
856 return sprintf(buf, "%d\n", rdev->use_count); in num_users_show()
865 switch (rdev->desc->type) { in type_show()
880 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV); in suspend_mem_microvolts_show()
889 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV); in suspend_disk_microvolts_show()
898 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV); in suspend_standby_microvolts_show()
908 rdev->constraints->state_mem.mode); in suspend_mem_mode_show()
918 rdev->constraints->state_disk.mode); in suspend_disk_mode_show()
928 rdev->constraints->state_standby.mode); in suspend_standby_mode_show()
938 rdev->constraints->state_mem.enabled); in suspend_mem_state_show()
948 rdev->constraints->state_disk.enabled); in suspend_disk_state_show()
958 rdev->constraints->state_standby.enabled); in suspend_standby_state_show()
970 ret = rdev->desc->ops->get_bypass(rdev, &bypass); in bypass_show()
1007 /* Calculate the new optimum regulator operating mode based on the new total
1012 struct regulator *sibling; in drms_uA_update()
1025 if (!rdev->desc->ops->get_optimum_mode && in drms_uA_update()
1026 !rdev->desc->ops->set_load) in drms_uA_update()
1029 if (!rdev->desc->ops->set_mode && in drms_uA_update()
1030 !rdev->desc->ops->set_load) in drms_uA_update()
1031 return -EINVAL; in drms_uA_update()
1034 list_for_each_entry(sibling, &rdev->consumer_list, list) { in drms_uA_update()
1035 if (sibling->enable_count) in drms_uA_update()
1036 current_uA += sibling->uA_load; in drms_uA_update()
1039 current_uA += rdev->constraints->system_load; in drms_uA_update()
1041 if (rdev->desc->ops->set_load) { in drms_uA_update()
1042 /* set the optimum mode for our new total regulator load */ in drms_uA_update()
1043 err = rdev->desc->ops->set_load(rdev, current_uA); in drms_uA_update()
1049 * Unfortunately in some cases the constraints->valid_ops has in drms_uA_update()
1055 if (!rdev->constraints->valid_modes_mask) { in drms_uA_update()
1064 * Don't return an error; if regulator driver cares about in drms_uA_update()
1072 if (rdev->supply) in drms_uA_update()
1073 input_uV = regulator_get_voltage_rdev(rdev->supply->rdev); in drms_uA_update()
1075 input_uV = rdev->constraints->input_uV; in drms_uA_update()
1078 * Don't return an error; if regulator driver cares about in drms_uA_update()
1084 /* now get the optimum mode for our new total regulator load */ in drms_uA_update()
1085 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV, in drms_uA_update()
1091 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV: %pe\n", in drms_uA_update()
1096 err = rdev->desc->ops->set_mode(rdev, mode); in drms_uA_update()
1110 if (rstate->enabled == ENABLE_IN_SUSPEND && in __suspend_set_state()
1111 rdev->desc->ops->set_suspend_enable) in __suspend_set_state()
1112 ret = rdev->desc->ops->set_suspend_enable(rdev); in __suspend_set_state()
1113 else if (rstate->enabled == DISABLE_IN_SUSPEND && in __suspend_set_state()
1114 rdev->desc->ops->set_suspend_disable) in __suspend_set_state()
1115 ret = rdev->desc->ops->set_suspend_disable(rdev); in __suspend_set_state()
1124 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) { in __suspend_set_state()
1125 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV); in __suspend_set_state()
1132 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) { in __suspend_set_state()
1133 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode); in __suspend_set_state()
1148 rdev->constraints->initial_state); in suspend_set_initial_state()
1158 struct regulation_constraints *constraints = rdev->constraints; in print_constraints_debug()
1160 size_t len = sizeof(buf) - 1; in print_constraints_debug()
1164 if (constraints->min_uV && constraints->max_uV) { in print_constraints_debug()
1165 if (constraints->min_uV == constraints->max_uV) in print_constraints_debug()
1166 count += scnprintf(buf + count, len - count, "%d mV ", in print_constraints_debug()
1167 constraints->min_uV / 1000); in print_constraints_debug()
1169 count += scnprintf(buf + count, len - count, in print_constraints_debug()
1170 "%d <--> %d mV ", in print_constraints_debug()
1171 constraints->min_uV / 1000, in print_constraints_debug()
1172 constraints->max_uV / 1000); in print_constraints_debug()
1175 if (!constraints->min_uV || in print_constraints_debug()
1176 constraints->min_uV != constraints->max_uV) { in print_constraints_debug()
1179 count += scnprintf(buf + count, len - count, in print_constraints_debug()
1183 if (constraints->uV_offset) in print_constraints_debug()
1184 count += scnprintf(buf + count, len - count, "%dmV offset ", in print_constraints_debug()
1185 constraints->uV_offset / 1000); in print_constraints_debug()
1187 if (constraints->min_uA && constraints->max_uA) { in print_constraints_debug()
1188 if (constraints->min_uA == constraints->max_uA) in print_constraints_debug()
1189 count += scnprintf(buf + count, len - count, "%d mA ", in print_constraints_debug()
1190 constraints->min_uA / 1000); in print_constraints_debug()
1192 count += scnprintf(buf + count, len - count, in print_constraints_debug()
1193 "%d <--> %d mA ", in print_constraints_debug()
1194 constraints->min_uA / 1000, in print_constraints_debug()
1195 constraints->max_uA / 1000); in print_constraints_debug()
1198 if (!constraints->min_uA || in print_constraints_debug()
1199 constraints->min_uA != constraints->max_uA) { in print_constraints_debug()
1202 count += scnprintf(buf + count, len - count, in print_constraints_debug()
1206 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST) in print_constraints_debug()
1207 count += scnprintf(buf + count, len - count, "fast "); in print_constraints_debug()
1208 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL) in print_constraints_debug()
1209 count += scnprintf(buf + count, len - count, "normal "); in print_constraints_debug()
1210 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE) in print_constraints_debug()
1211 count += scnprintf(buf + count, len - count, "idle "); in print_constraints_debug()
1212 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY) in print_constraints_debug()
1213 count += scnprintf(buf + count, len - count, "standby "); in print_constraints_debug()
1218 --count; in print_constraints_debug()
1220 count += scnprintf(buf + count, len - count, ", %s", in print_constraints_debug()
1231 struct regulation_constraints *constraints = rdev->constraints; in print_constraints()
1235 if ((constraints->min_uV != constraints->max_uV) && in print_constraints()
1244 const struct regulator_ops *ops = rdev->desc->ops; in machine_constraints_voltage()
1248 if (rdev->constraints->apply_uV && in machine_constraints_voltage()
1249 rdev->constraints->min_uV && rdev->constraints->max_uV) { in machine_constraints_voltage()
1253 if (current_uV == -ENOTRECOVERABLE) { in machine_constraints_voltage()
1254 /* This regulator can't be read and must be initialized */ in machine_constraints_voltage()
1255 rdev_info(rdev, "Setting %d-%duV\n", in machine_constraints_voltage()
1256 rdev->constraints->min_uV, in machine_constraints_voltage()
1257 rdev->constraints->max_uV); in machine_constraints_voltage()
1259 rdev->constraints->min_uV, in machine_constraints_voltage()
1260 rdev->constraints->max_uV); in machine_constraints_voltage()
1265 if (current_uV != -EPROBE_DEFER) in machine_constraints_voltage()
1280 if (current_uV < rdev->constraints->min_uV) { in machine_constraints_voltage()
1281 target_min = rdev->constraints->min_uV; in machine_constraints_voltage()
1282 target_max = rdev->constraints->min_uV; in machine_constraints_voltage()
1285 if (current_uV > rdev->constraints->max_uV) { in machine_constraints_voltage()
1286 target_min = rdev->constraints->max_uV; in machine_constraints_voltage()
1287 target_max = rdev->constraints->max_uV; in machine_constraints_voltage()
1291 rdev_info(rdev, "Bringing %duV into %d-%duV\n", in machine_constraints_voltage()
1297 "failed to apply %d-%duV constraint: %pe\n", in machine_constraints_voltage()
1304 /* constrain machine-level voltage specs to fit in machine_constraints_voltage()
1305 * the actual range supported by this regulator. in machine_constraints_voltage()
1307 if (ops->list_voltage && rdev->desc->n_voltages) { in machine_constraints_voltage()
1308 int count = rdev->desc->n_voltages; in machine_constraints_voltage()
1312 int cmin = constraints->min_uV; in machine_constraints_voltage()
1313 int cmax = constraints->max_uV; in machine_constraints_voltage()
1315 /* it's safe to autoconfigure fixed-voltage supplies in machine_constraints_voltage()
1321 constraints->min_uV = cmin; in machine_constraints_voltage()
1322 constraints->max_uV = cmax; in machine_constraints_voltage()
1329 /* else require explicit machine-level constraints */ in machine_constraints_voltage()
1332 return -EINVAL; in machine_constraints_voltage()
1336 if (rdev->desc->continuous_voltage_range) in machine_constraints_voltage()
1343 value = ops->list_voltage(rdev, i); in machine_constraints_voltage()
1357 "unsupportable voltage constraints %u-%uuV\n", in machine_constraints_voltage()
1359 return -EINVAL; in machine_constraints_voltage()
1362 /* use regulator's subset of machine constraints */ in machine_constraints_voltage()
1363 if (constraints->min_uV < min_uV) { in machine_constraints_voltage()
1364 rdev_dbg(rdev, "override min_uV, %d -> %d\n", in machine_constraints_voltage()
1365 constraints->min_uV, min_uV); in machine_constraints_voltage()
1366 constraints->min_uV = min_uV; in machine_constraints_voltage()
1368 if (constraints->max_uV > max_uV) { in machine_constraints_voltage()
1369 rdev_dbg(rdev, "override max_uV, %d -> %d\n", in machine_constraints_voltage()
1370 constraints->max_uV, max_uV); in machine_constraints_voltage()
1371 constraints->max_uV = max_uV; in machine_constraints_voltage()
1381 const struct regulator_ops *ops = rdev->desc->ops; in machine_constraints_current()
1384 if (!constraints->min_uA && !constraints->max_uA) in machine_constraints_current()
1387 if (constraints->min_uA > constraints->max_uA) { in machine_constraints_current()
1389 return -EINVAL; in machine_constraints_current()
1392 if (!ops->set_current_limit || !ops->get_current_limit) { in machine_constraints_current()
1397 /* Set regulator current in constraints range */ in machine_constraints_current()
1398 ret = ops->set_current_limit(rdev, constraints->min_uA, in machine_constraints_current()
1399 constraints->max_uA); in machine_constraints_current()
1436 return -EOPNOTSUPP; in handle_notify_limits()
1438 if (limits->prot) in handle_notify_limits()
1439 ret = notif_set_limit(rdev, set, limits->prot, in handle_notify_limits()
1444 if (limits->err) in handle_notify_limits()
1445 ret = notif_set_limit(rdev, set, limits->err, in handle_notify_limits()
1450 if (limits->warn) in handle_notify_limits()
1451 ret = notif_set_limit(rdev, set, limits->warn, in handle_notify_limits()
1457 * set_machine_constraints - sets regulator constraints
1458 * @rdev: regulator source
1461 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
1463 * regulator operations to proceed i.e. set_voltage, set_current_limit,
1469 const struct regulator_ops *ops = rdev->desc->ops; in set_machine_constraints()
1471 ret = machine_constraints_voltage(rdev, rdev->constraints); in set_machine_constraints()
1475 ret = machine_constraints_current(rdev, rdev->constraints); in set_machine_constraints()
1479 if (rdev->constraints->ilim_uA && ops->set_input_current_limit) { in set_machine_constraints()
1480 ret = ops->set_input_current_limit(rdev, in set_machine_constraints()
1481 rdev->constraints->ilim_uA); in set_machine_constraints()
1489 if (rdev->constraints->initial_state) { in set_machine_constraints()
1497 if (rdev->constraints->initial_mode) { in set_machine_constraints()
1498 if (!ops->set_mode) { in set_machine_constraints()
1500 return -EINVAL; in set_machine_constraints()
1503 ret = ops->set_mode(rdev, rdev->constraints->initial_mode); in set_machine_constraints()
1508 } else if (rdev->constraints->system_load) { in set_machine_constraints()
1510 * We'll only apply the initial system load if an in set_machine_constraints()
1516 if ((rdev->constraints->ramp_delay || rdev->constraints->ramp_disable) in set_machine_constraints()
1517 && ops->set_ramp_delay) { in set_machine_constraints()
1518 ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay); in set_machine_constraints()
1525 if (rdev->constraints->pull_down && ops->set_pull_down) { in set_machine_constraints()
1526 ret = ops->set_pull_down(rdev); in set_machine_constraints()
1533 if (rdev->constraints->soft_start && ops->set_soft_start) { in set_machine_constraints()
1534 ret = ops->set_soft_start(rdev); in set_machine_constraints()
1545 * PMIC on board to another type - and the another PMIC's driver does in set_machine_constraints()
1547 * the DT limits are respected - especially if the new PMIC HW also in set_machine_constraints()
1552 * over-curret protection and detection and get rid of this special in set_machine_constraints()
1555 if (rdev->constraints->over_current_protection in set_machine_constraints()
1556 && ops->set_over_current_protection) { in set_machine_constraints()
1557 int lim = rdev->constraints->over_curr_limits.prot; in set_machine_constraints()
1559 ret = ops->set_over_current_protection(rdev, lim, in set_machine_constraints()
1569 if (rdev->constraints->over_current_detection) in set_machine_constraints()
1571 ops->set_over_current_protection, in set_machine_constraints()
1572 &rdev->constraints->over_curr_limits); in set_machine_constraints()
1574 if (ret != -EOPNOTSUPP) { in set_machine_constraints()
1580 "IC does not support requested over-current limits\n"); in set_machine_constraints()
1583 if (rdev->constraints->over_voltage_detection) in set_machine_constraints()
1585 ops->set_over_voltage_protection, in set_machine_constraints()
1586 &rdev->constraints->over_voltage_limits); in set_machine_constraints()
1588 if (ret != -EOPNOTSUPP) { in set_machine_constraints()
1597 if (rdev->constraints->under_voltage_detection) in set_machine_constraints()
1599 ops->set_under_voltage_protection, in set_machine_constraints()
1600 &rdev->constraints->under_voltage_limits); in set_machine_constraints()
1602 if (ret != -EOPNOTSUPP) { in set_machine_constraints()
1611 if (rdev->constraints->over_temp_detection) in set_machine_constraints()
1613 ops->set_thermal_protection, in set_machine_constraints()
1614 &rdev->constraints->temp_limits); in set_machine_constraints()
1616 if (ret != -EOPNOTSUPP) { in set_machine_constraints()
1625 if (rdev->constraints->active_discharge && ops->set_active_discharge) { in set_machine_constraints()
1626 bool ad_state = (rdev->constraints->active_discharge == in set_machine_constraints()
1629 ret = ops->set_active_discharge(rdev, ad_state); in set_machine_constraints()
1637 * If there is no mechanism for controlling the regulator then in set_machine_constraints()
1640 * a supply to control the output on a regulator that has no in set_machine_constraints()
1643 if (!rdev->ena_pin && !ops->enable) { in set_machine_constraints()
1644 if (rdev->supply_name && !rdev->supply) in set_machine_constraints()
1645 return -EPROBE_DEFER; in set_machine_constraints()
1647 if (rdev->supply) in set_machine_constraints()
1648 rdev->constraints->always_on = in set_machine_constraints()
1649 rdev->supply->rdev->constraints->always_on; in set_machine_constraints()
1651 rdev->constraints->always_on = true; in set_machine_constraints()
1654 /* If the constraints say the regulator should be on at this point in set_machine_constraints()
1657 if (rdev->constraints->always_on || rdev->constraints->boot_on) { in set_machine_constraints()
1658 /* If we want to enable this regulator, make sure that we know in set_machine_constraints()
1659 * the supplying regulator. in set_machine_constraints()
1661 if (rdev->supply_name && !rdev->supply) in set_machine_constraints()
1662 return -EPROBE_DEFER; in set_machine_constraints()
1664 /* If supplying regulator has already been enabled, in set_machine_constraints()
1666 * when rdev is only boot-on. in set_machine_constraints()
1668 if (rdev->supply && in set_machine_constraints()
1669 (rdev->constraints->always_on || in set_machine_constraints()
1670 !regulator_is_enabled(rdev->supply))) { in set_machine_constraints()
1671 ret = regulator_enable(rdev->supply); in set_machine_constraints()
1673 _regulator_put(rdev->supply); in set_machine_constraints()
1674 rdev->supply = NULL; in set_machine_constraints()
1680 if (ret < 0 && ret != -EINVAL) { in set_machine_constraints()
1685 if (rdev->constraints->always_on) in set_machine_constraints()
1686 rdev->use_count++; in set_machine_constraints()
1687 } else if (rdev->desc->off_on_delay) { in set_machine_constraints()
1688 rdev->last_off = ktime_get(); in set_machine_constraints()
1696 * set_supply - set regulator supply regulator
1697 * @rdev: regulator (locked)
1698 * @supply_rdev: supply regulator (locked))
1700 * Called by platform initialisation code to set the supply regulator for this
1701 * regulator. This ensures that a regulators supply will also be enabled by the
1711 if (!try_module_get(supply_rdev->owner)) in set_supply()
1712 return -ENODEV; in set_supply()
1714 rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY"); in set_supply()
1715 if (rdev->supply == NULL) { in set_supply()
1716 module_put(supply_rdev->owner); in set_supply()
1717 err = -ENOMEM; in set_supply()
1720 supply_rdev->open_count++; in set_supply()
1726 * set_consumer_device_supply - Bind a regulator to a symbolic supply
1727 * @rdev: regulator source
1731 * Allows platform initialisation code to map physical regulator
1734 * need to provide board-specific regulator names as platform data.
1744 return -EINVAL; in set_consumer_device_supply()
1753 return -ENOMEM; in set_consumer_device_supply()
1755 new_node->regulator = rdev; in set_consumer_device_supply()
1756 new_node->supply = supply; in set_consumer_device_supply()
1759 new_node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL); in set_consumer_device_supply()
1760 if (new_node->dev_name == NULL) { in set_consumer_device_supply()
1762 return -ENOMEM; in set_consumer_device_supply()
1768 if (node->dev_name && consumer_dev_name) { in set_consumer_device_supply()
1769 if (strcmp(node->dev_name, consumer_dev_name) != 0) in set_consumer_device_supply()
1771 } else if (node->dev_name || consumer_dev_name) { in set_consumer_device_supply()
1775 if (strcmp(node->supply, supply) != 0) in set_consumer_device_supply()
1780 dev_name(&node->regulator->dev), in set_consumer_device_supply()
1781 node->regulator->desc->name, in set_consumer_device_supply()
1783 dev_name(&rdev->dev), rdev_get_name(rdev)); in set_consumer_device_supply()
1787 list_add(&new_node->list, ®ulator_map_list); in set_consumer_device_supply()
1794 kfree(new_node->dev_name); in set_consumer_device_supply()
1796 return -EBUSY; in set_consumer_device_supply()
1804 if (rdev == node->regulator) { in unset_regulator_supplies()
1805 list_del(&node->list); in unset_regulator_supplies()
1806 kfree(node->dev_name); in unset_regulator_supplies()
1817 const struct regulator *regulator = file->private_data; in constraint_flags_read_file() local
1818 const struct regulation_constraints *c = regulator->rdev->constraints; in constraint_flags_read_file()
1827 return -ENOMEM; in constraint_flags_read_file()
1837 c->always_on, in constraint_flags_read_file()
1838 c->boot_on, in constraint_flags_read_file()
1839 c->apply_uV, in constraint_flags_read_file()
1840 c->ramp_disable, in constraint_flags_read_file()
1841 c->soft_start, in constraint_flags_read_file()
1842 c->pull_down, in constraint_flags_read_file()
1843 c->over_current_protection); in constraint_flags_read_file()
1863 static struct regulator *create_regulator(struct regulator_dev *rdev, in create_regulator()
1867 struct regulator *regulator; in create_regulator() local
1870 lockdep_assert_held_once(&rdev->mutex.base); in create_regulator()
1876 size = snprintf(buf, REG_STR_SIZE, "%s-%s", in create_regulator()
1877 dev->kobj.name, supply_name); in create_regulator()
1890 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL); in create_regulator()
1891 if (regulator == NULL) { in create_regulator()
1896 regulator->rdev = rdev; in create_regulator()
1897 regulator->supply_name = supply_name; in create_regulator()
1899 list_add(®ulator->list, &rdev->consumer_list); in create_regulator()
1902 regulator->dev = dev; in create_regulator()
1905 err = sysfs_create_link_nowarn(&rdev->dev.kobj, &dev->kobj, in create_regulator()
1909 dev->kobj.name, ERR_PTR(err)); in create_regulator()
1910 /* non-fatal */ in create_regulator()
1914 if (err != -EEXIST) in create_regulator()
1915 regulator->debugfs = debugfs_create_dir(supply_name, rdev->debugfs); in create_regulator()
1916 if (IS_ERR(regulator->debugfs)) in create_regulator()
1919 debugfs_create_u32("uA_load", 0444, regulator->debugfs, in create_regulator()
1920 ®ulator->uA_load); in create_regulator()
1921 debugfs_create_u32("min_uV", 0444, regulator->debugfs, in create_regulator()
1922 ®ulator->voltage[PM_SUSPEND_ON].min_uV); in create_regulator()
1923 debugfs_create_u32("max_uV", 0444, regulator->debugfs, in create_regulator()
1924 ®ulator->voltage[PM_SUSPEND_ON].max_uV); in create_regulator()
1925 debugfs_create_file("constraint_flags", 0444, regulator->debugfs, in create_regulator()
1926 regulator, &constraint_flags_fops); in create_regulator()
1929 * Check now if the regulator is an always on regulator - if in create_regulator()
1935 regulator->always_on = true; in create_regulator()
1937 return regulator; in create_regulator()
1942 if (rdev->constraints && rdev->constraints->enable_time) in _regulator_get_enable_time()
1943 return rdev->constraints->enable_time; in _regulator_get_enable_time()
1944 if (rdev->desc->ops->enable_time) in _regulator_get_enable_time()
1945 return rdev->desc->ops->enable_time(rdev); in _regulator_get_enable_time()
1946 return rdev->desc->enable_time; in _regulator_get_enable_time()
1955 if (map->src_dev == dev && strcmp(map->src_supply, supply) == 0) in regulator_find_supply_alias()
1968 *supply, map->alias_supply, in regulator_supply_alias()
1969 dev_name(map->alias_dev)); in regulator_supply_alias()
1970 *dev = map->alias_dev; in regulator_supply_alias()
1971 *supply = map->alias_supply; in regulator_supply_alias()
1992 * regulator_dev_lookup - lookup a regulator device.
1993 * @dev: device for regulator "consumer".
1994 * @supply: Supply name or regulator ID.
1999 * On failure one of the following ERR-PTR-encoded values is returned:
2000 * -ENODEV if lookup fails permanently, -EPROBE_DEFER if lookup could succeed
2014 if (dev && dev->of_node) { in regulator_dev_lookup()
2026 return ERR_PTR(-EPROBE_DEFER); in regulator_dev_lookup()
2030 /* if not found, try doing it non-dt way */ in regulator_dev_lookup()
2037 if (map->dev_name && in regulator_dev_lookup()
2038 (!devname || strcmp(map->dev_name, devname))) in regulator_dev_lookup()
2041 if (strcmp(map->supply, supply) == 0 && in regulator_dev_lookup()
2042 get_device(&map->regulator->dev)) { in regulator_dev_lookup()
2043 r = map->regulator; in regulator_dev_lookup()
2056 return ERR_PTR(-ENODEV); in regulator_dev_lookup()
2062 struct device *dev = rdev->dev.parent; in regulator_resolve_supply()
2067 if (!rdev->supply_name) in regulator_resolve_supply()
2070 /* Supply already resolved? (fast-path without locking contention) */ in regulator_resolve_supply()
2071 if (rdev->supply) in regulator_resolve_supply()
2074 r = regulator_dev_lookup(dev, rdev->supply_name); in regulator_resolve_supply()
2079 if (ret == -EPROBE_DEFER) in regulator_resolve_supply()
2084 get_device(&r->dev); in regulator_resolve_supply()
2086 dev_err(dev, "Failed to resolve %s-supply for %s\n", in regulator_resolve_supply()
2087 rdev->supply_name, rdev->desc->name); in regulator_resolve_supply()
2088 ret = -EPROBE_DEFER; in regulator_resolve_supply()
2095 rdev->desc->name, rdev->supply_name); in regulator_resolve_supply()
2097 ret = -EINVAL; in regulator_resolve_supply()
2101 get_device(&r->dev); in regulator_resolve_supply()
2106 * regulator's parent device, then ensure the parent device in regulator_resolve_supply()
2110 if (r->dev.parent && r->dev.parent != rdev->dev.parent) { in regulator_resolve_supply()
2111 if (!device_is_bound(r->dev.parent)) { in regulator_resolve_supply()
2112 put_device(&r->dev); in regulator_resolve_supply()
2113 ret = -EPROBE_DEFER; in regulator_resolve_supply()
2121 put_device(&r->dev); in regulator_resolve_supply()
2126 * Recheck rdev->supply with rdev->mutex lock held to avoid a race in regulator_resolve_supply()
2127 * between rdev->supply null check and setting rdev->supply in in regulator_resolve_supply()
2133 if (rdev->supply) { in regulator_resolve_supply()
2135 put_device(&r->dev); in regulator_resolve_supply()
2142 put_device(&r->dev); in regulator_resolve_supply()
2149 * In set_machine_constraints() we may have turned this regulator on in regulator_resolve_supply()
2153 if (rdev->use_count) { in regulator_resolve_supply()
2154 ret = regulator_enable(rdev->supply); in regulator_resolve_supply()
2156 _regulator_put(rdev->supply); in regulator_resolve_supply()
2157 rdev->supply = NULL; in regulator_resolve_supply()
2166 /* Internal regulator request function */
2167 struct regulator *_regulator_get(struct device *dev, const char *id, in _regulator_get()
2171 struct regulator *regulator; in _regulator_get() local
2177 return ERR_PTR(-EINVAL); in _regulator_get()
2182 return ERR_PTR(-EINVAL); in _regulator_get()
2191 * than -ENODEV our job here is done, we simply return it. in _regulator_get()
2193 if (ret != -ENODEV) in _regulator_get()
2199 return ERR_PTR(-ENODEV); in _regulator_get()
2205 * Assume that a regulator is physically present and in _regulator_get()
2209 dev_warn(dev, "supply %s not found, using dummy regulator\n", id); in _regulator_get()
2211 get_device(&rdev->dev); in _regulator_get()
2220 return ERR_PTR(-ENODEV); in _regulator_get()
2224 if (rdev->exclusive) { in _regulator_get()
2225 regulator = ERR_PTR(-EPERM); in _regulator_get()
2226 put_device(&rdev->dev); in _regulator_get()
2227 return regulator; in _regulator_get()
2230 if (get_type == EXCLUSIVE_GET && rdev->open_count) { in _regulator_get()
2231 regulator = ERR_PTR(-EBUSY); in _regulator_get()
2232 put_device(&rdev->dev); in _regulator_get()
2233 return regulator; in _regulator_get()
2237 ret = (rdev->coupling_desc.n_resolved != rdev->coupling_desc.n_coupled); in _regulator_get()
2241 regulator = ERR_PTR(-EPROBE_DEFER); in _regulator_get()
2242 put_device(&rdev->dev); in _regulator_get()
2243 return regulator; in _regulator_get()
2248 regulator = ERR_PTR(ret); in _regulator_get()
2249 put_device(&rdev->dev); in _regulator_get()
2250 return regulator; in _regulator_get()
2253 if (!try_module_get(rdev->owner)) { in _regulator_get()
2254 regulator = ERR_PTR(-EPROBE_DEFER); in _regulator_get()
2255 put_device(&rdev->dev); in _regulator_get()
2256 return regulator; in _regulator_get()
2260 regulator = create_regulator(rdev, dev, id); in _regulator_get()
2262 if (regulator == NULL) { in _regulator_get()
2263 regulator = ERR_PTR(-ENOMEM); in _regulator_get()
2264 module_put(rdev->owner); in _regulator_get()
2265 put_device(&rdev->dev); in _regulator_get()
2266 return regulator; in _regulator_get()
2269 rdev->open_count++; in _regulator_get()
2271 rdev->exclusive = 1; in _regulator_get()
2275 rdev->use_count = 1; in _regulator_get()
2276 regulator->enable_count = 1; in _regulator_get()
2278 rdev->use_count = 0; in _regulator_get()
2279 regulator->enable_count = 0; in _regulator_get()
2283 link = device_link_add(dev, &rdev->dev, DL_FLAG_STATELESS); in _regulator_get()
2285 regulator->device_link = true; in _regulator_get()
2287 return regulator; in _regulator_get()
2291 * regulator_get - lookup and obtain a reference to a regulator.
2292 * @dev: device for regulator "consumer"
2293 * @id: Supply name or regulator ID.
2295 * Returns a struct regulator corresponding to the regulator producer,
2303 struct regulator *regulator_get(struct device *dev, const char *id) in regulator_get()
2310 * regulator_get_exclusive - obtain exclusive access to a regulator.
2311 * @dev: device for regulator "consumer"
2312 * @id: Supply name or regulator ID.
2314 * Returns a struct regulator corresponding to the regulator producer,
2316 * unable to obtain this regulator while this reference is held and the
2317 * use count for the regulator will be initialised to reflect the current
2318 * state of the regulator.
2321 * use of the regulator such as those which need to force the
2322 * regulator off for correct operation of the hardware they are
2330 struct regulator *regulator_get_exclusive(struct device *dev, const char *id) in regulator_get_exclusive()
2337 * regulator_get_optional - obtain optional access to a regulator.
2338 * @dev: device for regulator "consumer"
2339 * @id: Supply name or regulator ID.
2341 * Returns a struct regulator corresponding to the regulator producer,
2346 * It can allow the regulator core to provide stub supplies for other
2356 struct regulator *regulator_get_optional(struct device *dev, const char *id) in regulator_get_optional()
2362 static void destroy_regulator(struct regulator *regulator) in destroy_regulator() argument
2364 struct regulator_dev *rdev = regulator->rdev; in destroy_regulator()
2366 debugfs_remove_recursive(regulator->debugfs); in destroy_regulator()
2368 if (regulator->dev) { in destroy_regulator()
2369 if (regulator->device_link) in destroy_regulator()
2370 device_link_remove(regulator->dev, &rdev->dev); in destroy_regulator()
2373 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name); in destroy_regulator()
2377 list_del(®ulator->list); in destroy_regulator()
2379 rdev->open_count--; in destroy_regulator()
2380 rdev->exclusive = 0; in destroy_regulator()
2383 kfree_const(regulator->supply_name); in destroy_regulator()
2384 kfree(regulator); in destroy_regulator()
2388 static void _regulator_put(struct regulator *regulator) in _regulator_put() argument
2392 if (IS_ERR_OR_NULL(regulator)) in _regulator_put()
2398 WARN_ON(regulator->enable_count); in _regulator_put()
2400 rdev = regulator->rdev; in _regulator_put()
2402 destroy_regulator(regulator); in _regulator_put()
2404 module_put(rdev->owner); in _regulator_put()
2405 put_device(&rdev->dev); in _regulator_put()
2409 * regulator_put - "free" the regulator source
2410 * @regulator: regulator source
2413 * regulator source are balanced by regulator_disable calls prior to calling
2416 void regulator_put(struct regulator *regulator) in regulator_put() argument
2419 _regulator_put(regulator); in regulator_put()
2425 * regulator_register_supply_alias - Provide device alias for supply lookup
2427 * @dev: device that will be given as the regulator "consumer"
2428 * @id: Supply name or regulator ID
2430 * @alias_id: Supply name or regulator ID that should be used to lookup the
2444 return -EEXIST; in regulator_register_supply_alias()
2448 return -ENOMEM; in regulator_register_supply_alias()
2450 map->src_dev = dev; in regulator_register_supply_alias()
2451 map->src_supply = id; in regulator_register_supply_alias()
2452 map->alias_dev = alias_dev; in regulator_register_supply_alias()
2453 map->alias_supply = alias_id; in regulator_register_supply_alias()
2455 list_add(&map->list, ®ulator_supply_alias_list); in regulator_register_supply_alias()
2457 pr_info("Adding alias for supply %s,%s -> %s,%s\n", in regulator_register_supply_alias()
2465 * regulator_unregister_supply_alias - Remove device alias
2467 * @dev: device that will be given as the regulator "consumer"
2468 * @id: Supply name or regulator ID
2478 list_del(&map->list); in regulator_unregister_supply_alias()
2485 * regulator_bulk_register_supply_alias - register multiple aliases
2487 * @dev: device that will be given as the regulator "consumer"
2488 * @id: List of supply names or regulator IDs
2490 * @alias_id: List of supply names or regulator IDs that should be used to
2521 "Failed to create supply alias %s,%s -> %s,%s\n", in regulator_bulk_register_supply_alias()
2524 while (--i >= 0) in regulator_bulk_register_supply_alias()
2532 * regulator_bulk_unregister_supply_alias - unregister multiple aliases
2534 * @dev: device that will be given as the regulator "consumer"
2535 * @id: List of supply names or regulator IDs
2560 gpiod = config->ena_gpiod; in regulator_ena_gpio_request()
2566 if (pin->gpiod == gpiod) { in regulator_ena_gpio_request()
2574 return -ENOMEM; in regulator_ena_gpio_request()
2580 pin->gpiod = gpiod; in regulator_ena_gpio_request()
2581 list_add(&pin->list, ®ulator_ena_gpio_list); in regulator_ena_gpio_request()
2584 pin->request_count++; in regulator_ena_gpio_request()
2585 rdev->ena_pin = pin; in regulator_ena_gpio_request()
2597 if (!rdev->ena_pin) in regulator_ena_gpio_free()
2602 if (pin != rdev->ena_pin) in regulator_ena_gpio_free()
2605 if (--pin->request_count) in regulator_ena_gpio_free()
2608 gpiod_put(pin->gpiod); in regulator_ena_gpio_free()
2609 list_del(&pin->list); in regulator_ena_gpio_free()
2614 rdev->ena_pin = NULL; in regulator_ena_gpio_free()
2618 * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control
2627 struct regulator_enable_gpio *pin = rdev->ena_pin; in regulator_ena_gpio_ctrl()
2630 return -EINVAL; in regulator_ena_gpio_ctrl()
2634 if (pin->enable_count == 0) in regulator_ena_gpio_ctrl()
2635 gpiod_set_value_cansleep(pin->gpiod, 1); in regulator_ena_gpio_ctrl()
2637 pin->enable_count++; in regulator_ena_gpio_ctrl()
2639 if (pin->enable_count > 1) { in regulator_ena_gpio_ctrl()
2640 pin->enable_count--; in regulator_ena_gpio_ctrl()
2645 if (pin->enable_count <= 1) { in regulator_ena_gpio_ctrl()
2646 gpiod_set_value_cansleep(pin->gpiod, 0); in regulator_ena_gpio_ctrl()
2647 pin->enable_count = 0; in regulator_ena_gpio_ctrl()
2655 * _regulator_delay_helper - a delay helper function
2660 * Documentation/timers/timers-howto.rst
2662 * The assumption here is that these regulator operations will never used in
2672 * For small enough values, handle super-millisecond in _regulator_delay_helper()
2684 * bother setting up high-resolution timers and just busy- in _regulator_delay_helper()
2696 * A helper function to check if the regulator status can be interpreted
2697 * as 'regulator is enabled'.
2698 * @rdev: the regulator device to check
2701 * * 1 - if status shows regulator is in enabled state
2702 * * 0 - if not enabled state
2703 * * Error Value - as received from ops->get_status()
2707 int ret = rdev->desc->ops->get_status(rdev); in _regulator_check_status_enabled()
2739 if (rdev->desc->off_on_delay) { in _regulator_do_enable()
2741 * this regulator was disabled. in _regulator_do_enable()
2743 ktime_t end = ktime_add_us(rdev->last_off, rdev->desc->off_on_delay); in _regulator_do_enable()
2750 if (rdev->ena_pin) { in _regulator_do_enable()
2751 if (!rdev->ena_gpio_state) { in _regulator_do_enable()
2755 rdev->ena_gpio_state = 1; in _regulator_do_enable()
2757 } else if (rdev->desc->ops->enable) { in _regulator_do_enable()
2758 ret = rdev->desc->ops->enable(rdev); in _regulator_do_enable()
2762 return -EINVAL; in _regulator_do_enable()
2765 /* Allow the regulator to ramp; it would be useful to extend in _regulator_do_enable()
2772 * above, delaying poll_enabled_time uS to check if the regulator in _regulator_do_enable()
2774 * If the regulator isn't enabled after our delay helper has expired, in _regulator_do_enable()
2775 * return -ETIMEDOUT. in _regulator_do_enable()
2777 if (rdev->desc->poll_enabled_time) { in _regulator_do_enable()
2781 _regulator_delay_helper(rdev->desc->poll_enabled_time); in _regulator_do_enable()
2783 if (rdev->desc->ops->get_status) { in _regulator_do_enable()
2789 } else if (rdev->desc->ops->is_enabled(rdev)) in _regulator_do_enable()
2792 time_remaining -= rdev->desc->poll_enabled_time; in _regulator_do_enable()
2797 return -ETIMEDOUT; in _regulator_do_enable()
2809 * _regulator_handle_consumer_enable - handle that a consumer enabled
2810 * @regulator: regulator source
2812 * Some things on a regulator consumer (like the contribution towards total
2813 * load on the regulator) only have an effect when the consumer wants the
2814 * regulator enabled. Explained in example with two consumers of the same
2815 * regulator:
2823 * responsible for keeping track of the refcount for a given regulator consumer
2826 * Returns 0 upon no error; -error upon error.
2828 static int _regulator_handle_consumer_enable(struct regulator *regulator) in _regulator_handle_consumer_enable() argument
2831 struct regulator_dev *rdev = regulator->rdev; in _regulator_handle_consumer_enable()
2833 lockdep_assert_held_once(&rdev->mutex.base); in _regulator_handle_consumer_enable()
2835 regulator->enable_count++; in _regulator_handle_consumer_enable()
2836 if (regulator->uA_load && regulator->enable_count == 1) { in _regulator_handle_consumer_enable()
2839 regulator->enable_count--; in _regulator_handle_consumer_enable()
2847 * _regulator_handle_consumer_disable - handle that a consumer disabled
2848 * @regulator: regulator source
2852 * Returns 0 upon no error; -error upon error.
2854 static int _regulator_handle_consumer_disable(struct regulator *regulator) in _regulator_handle_consumer_disable() argument
2856 struct regulator_dev *rdev = regulator->rdev; in _regulator_handle_consumer_disable()
2858 lockdep_assert_held_once(&rdev->mutex.base); in _regulator_handle_consumer_disable()
2860 if (!regulator->enable_count) { in _regulator_handle_consumer_disable()
2861 rdev_err(rdev, "Underflow of regulator enable count\n"); in _regulator_handle_consumer_disable()
2862 return -EINVAL; in _regulator_handle_consumer_disable()
2865 regulator->enable_count--; in _regulator_handle_consumer_disable()
2866 if (regulator->uA_load && regulator->enable_count == 0) in _regulator_handle_consumer_disable()
2873 static int _regulator_enable(struct regulator *regulator) in _regulator_enable() argument
2875 struct regulator_dev *rdev = regulator->rdev; in _regulator_enable()
2878 lockdep_assert_held_once(&rdev->mutex.base); in _regulator_enable()
2880 if (rdev->use_count == 0 && rdev->supply) { in _regulator_enable()
2881 ret = _regulator_enable(rdev->supply); in _regulator_enable()
2887 if (rdev->coupling_desc.n_coupled > 1) { in _regulator_enable()
2893 ret = _regulator_handle_consumer_enable(regulator); in _regulator_enable()
2897 if (rdev->use_count == 0) { in _regulator_enable()
2899 * The regulator may already be enabled if it's not switchable in _regulator_enable()
2903 if (ret == -EINVAL || ret == 0) { in _regulator_enable()
2906 ret = -EPERM; in _regulator_enable()
2920 /* Fallthrough on positive return values - already enabled */ in _regulator_enable()
2923 if (regulator->enable_count == 1) in _regulator_enable()
2924 rdev->use_count++; in _regulator_enable()
2929 _regulator_handle_consumer_disable(regulator); in _regulator_enable()
2932 if (rdev->use_count == 0 && rdev->supply) in _regulator_enable()
2933 _regulator_disable(rdev->supply); in _regulator_enable()
2939 * regulator_enable - enable regulator output
2940 * @regulator: regulator source
2942 * Request that the regulator be enabled with the regulator output at
2947 * hardwired in the regulator.
2949 int regulator_enable(struct regulator *regulator) in regulator_enable() argument
2951 struct regulator_dev *rdev = regulator->rdev; in regulator_enable()
2956 ret = _regulator_enable(regulator); in regulator_enable()
2969 if (rdev->ena_pin) { in _regulator_do_disable()
2970 if (rdev->ena_gpio_state) { in _regulator_do_disable()
2974 rdev->ena_gpio_state = 0; in _regulator_do_disable()
2977 } else if (rdev->desc->ops->disable) { in _regulator_do_disable()
2978 ret = rdev->desc->ops->disable(rdev); in _regulator_do_disable()
2983 if (rdev->desc->off_on_delay) in _regulator_do_disable()
2984 rdev->last_off = ktime_get_boottime(); in _regulator_do_disable()
2992 static int _regulator_disable(struct regulator *regulator) in _regulator_disable() argument
2994 struct regulator_dev *rdev = regulator->rdev; in _regulator_disable()
2997 lockdep_assert_held_once(&rdev->mutex.base); in _regulator_disable()
2999 if (WARN(regulator->enable_count == 0, in _regulator_disable()
3001 return -EIO; in _regulator_disable()
3003 if (regulator->enable_count == 1) { in _regulator_disable()
3004 /* disabling last enable_count from this regulator */ in _regulator_disable()
3006 if (rdev->use_count == 1 && in _regulator_disable()
3007 (rdev->constraints && !rdev->constraints->always_on)) { in _regulator_disable()
3015 return -EINVAL; in _regulator_disable()
3029 rdev->use_count = 0; in _regulator_disable()
3030 } else if (rdev->use_count > 1) { in _regulator_disable()
3031 rdev->use_count--; in _regulator_disable()
3036 ret = _regulator_handle_consumer_disable(regulator); in _regulator_disable()
3038 if (ret == 0 && rdev->coupling_desc.n_coupled > 1) in _regulator_disable()
3041 if (ret == 0 && rdev->use_count == 0 && rdev->supply) in _regulator_disable()
3042 ret = _regulator_disable(rdev->supply); in _regulator_disable()
3048 * regulator_disable - disable regulator output
3049 * @regulator: regulator source
3051 * Disable the regulator output voltage or current. Calls to
3055 * NOTE: this will only disable the regulator output if no other consumer
3056 * devices have it enabled, the regulator device supports disabling and
3059 int regulator_disable(struct regulator *regulator) in regulator_disable() argument
3061 struct regulator_dev *rdev = regulator->rdev; in regulator_disable()
3066 ret = _regulator_disable(regulator); in regulator_disable()
3078 lockdep_assert_held_once(&rdev->mutex.base); in _regulator_force_disable()
3083 return -EINVAL; in _regulator_force_disable()
3100 * regulator_force_disable - force disable regulator output
3101 * @regulator: regulator source
3103 * Forcibly disable the regulator output voltage or current.
3104 * NOTE: this *will* disable the regulator output even if other consumer
3106 * damage will likely occur if the regulator is not disabled (e.g. over temp).
3108 int regulator_force_disable(struct regulator *regulator) in regulator_force_disable() argument
3110 struct regulator_dev *rdev = regulator->rdev; in regulator_force_disable()
3116 ret = _regulator_force_disable(regulator->rdev); in regulator_force_disable()
3118 if (rdev->coupling_desc.n_coupled > 1) in regulator_force_disable()
3121 if (regulator->uA_load) { in regulator_force_disable()
3122 regulator->uA_load = 0; in regulator_force_disable()
3126 if (rdev->use_count != 0 && rdev->supply) in regulator_force_disable()
3127 _regulator_disable(rdev->supply); in regulator_force_disable()
3141 struct regulator *regulator; in regulator_disable_work() local
3152 cancel_delayed_work(&rdev->disable_work); in regulator_disable_work()
3154 list_for_each_entry(regulator, &rdev->consumer_list, list) { in regulator_disable_work()
3155 count = regulator->deferred_disables; in regulator_disable_work()
3161 regulator->deferred_disables = 0; in regulator_disable_work()
3164 ret = _regulator_disable(regulator); in regulator_disable_work()
3172 if (rdev->coupling_desc.n_coupled > 1) in regulator_disable_work()
3179 * regulator_disable_deferred - disable regulator output with delay
3180 * @regulator: regulator source
3181 * @ms: milliseconds until the regulator is disabled
3183 * Execute regulator_disable() on the regulator after a delay. This
3186 * NOTE: this will only disable the regulator output if no other consumer
3187 * devices have it enabled, the regulator device supports disabling and
3190 int regulator_disable_deferred(struct regulator *regulator, int ms) in regulator_disable_deferred() argument
3192 struct regulator_dev *rdev = regulator->rdev; in regulator_disable_deferred()
3195 return regulator_disable(regulator); in regulator_disable_deferred()
3198 regulator->deferred_disables++; in regulator_disable_deferred()
3199 mod_delayed_work(system_power_efficient_wq, &rdev->disable_work, in regulator_disable_deferred()
3210 if (rdev->ena_pin) in _regulator_is_enabled()
3211 return rdev->ena_gpio_state; in _regulator_is_enabled()
3213 /* If we don't know then assume that the regulator is always on */ in _regulator_is_enabled()
3214 if (!rdev->desc->ops->is_enabled) in _regulator_is_enabled()
3217 return rdev->desc->ops->is_enabled(rdev); in _regulator_is_enabled()
3223 const struct regulator_ops *ops = rdev->desc->ops; in _regulator_list_voltage()
3226 if (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1 && !selector) in _regulator_list_voltage()
3227 return rdev->desc->fixed_uV; in _regulator_list_voltage()
3229 if (ops->list_voltage) { in _regulator_list_voltage()
3230 if (selector >= rdev->desc->n_voltages) in _regulator_list_voltage()
3231 return -EINVAL; in _regulator_list_voltage()
3232 if (selector < rdev->desc->linear_min_sel) in _regulator_list_voltage()
3236 ret = ops->list_voltage(rdev, selector); in _regulator_list_voltage()
3239 } else if (rdev->is_switch && rdev->supply) { in _regulator_list_voltage()
3240 ret = _regulator_list_voltage(rdev->supply->rdev, in _regulator_list_voltage()
3243 return -EINVAL; in _regulator_list_voltage()
3247 if (ret < rdev->constraints->min_uV) in _regulator_list_voltage()
3249 else if (ret > rdev->constraints->max_uV) in _regulator_list_voltage()
3257 * regulator_is_enabled - is the regulator output enabled
3258 * @regulator: regulator source
3260 * Returns positive if the regulator driver backing the source/client
3264 * Note that the device backing this regulator handle can have multiple
3268 int regulator_is_enabled(struct regulator *regulator) in regulator_is_enabled() argument
3272 if (regulator->always_on) in regulator_is_enabled()
3275 regulator_lock(regulator->rdev); in regulator_is_enabled()
3276 ret = _regulator_is_enabled(regulator->rdev); in regulator_is_enabled()
3277 regulator_unlock(regulator->rdev); in regulator_is_enabled()
3284 * regulator_count_voltages - count regulator_list_voltage() selectors
3285 * @regulator: regulator source
3291 int regulator_count_voltages(struct regulator *regulator) in regulator_count_voltages() argument
3293 struct regulator_dev *rdev = regulator->rdev; in regulator_count_voltages()
3295 if (rdev->desc->n_voltages) in regulator_count_voltages()
3296 return rdev->desc->n_voltages; in regulator_count_voltages()
3298 if (!rdev->is_switch || !rdev->supply) in regulator_count_voltages()
3299 return -EINVAL; in regulator_count_voltages()
3301 return regulator_count_voltages(rdev->supply); in regulator_count_voltages()
3306 * regulator_list_voltage - enumerate supported voltages
3307 * @regulator: regulator source
3312 * zero if this selector code can't be used on this system, or a
3315 int regulator_list_voltage(struct regulator *regulator, unsigned selector) in regulator_list_voltage() argument
3317 return _regulator_list_voltage(regulator->rdev, selector, 1); in regulator_list_voltage()
3322 * regulator_get_regmap - get the regulator's register map
3323 * @regulator: regulator source
3325 * Returns the register map for the given regulator, or an ERR_PTR value
3326 * if the regulator doesn't use regmap.
3328 struct regmap *regulator_get_regmap(struct regulator *regulator) in regulator_get_regmap() argument
3330 struct regmap *map = regulator->rdev->regmap; in regulator_get_regmap()
3332 return map ? map : ERR_PTR(-EOPNOTSUPP); in regulator_get_regmap()
3336 * regulator_get_hardware_vsel_register - get the HW voltage selector register
3337 * @regulator: regulator source
3342 * regulator voltage. This might be useful when configuring voltage-scaling
3349 int regulator_get_hardware_vsel_register(struct regulator *regulator, in regulator_get_hardware_vsel_register() argument
3353 struct regulator_dev *rdev = regulator->rdev; in regulator_get_hardware_vsel_register()
3354 const struct regulator_ops *ops = rdev->desc->ops; in regulator_get_hardware_vsel_register()
3356 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap) in regulator_get_hardware_vsel_register()
3357 return -EOPNOTSUPP; in regulator_get_hardware_vsel_register()
3359 *vsel_reg = rdev->desc->vsel_reg; in regulator_get_hardware_vsel_register()
3360 *vsel_mask = rdev->desc->vsel_mask; in regulator_get_hardware_vsel_register()
3367 * regulator_list_hardware_vsel - get the HW-specific register value for a selector
3368 * @regulator: regulator source
3371 * Converts the selector to a hardware-specific voltage selector that can be
3372 * directly written to the regulator registers. The address of the voltage
3377 int regulator_list_hardware_vsel(struct regulator *regulator, in regulator_list_hardware_vsel() argument
3380 struct regulator_dev *rdev = regulator->rdev; in regulator_list_hardware_vsel()
3381 const struct regulator_ops *ops = rdev->desc->ops; in regulator_list_hardware_vsel()
3383 if (selector >= rdev->desc->n_voltages) in regulator_list_hardware_vsel()
3384 return -EINVAL; in regulator_list_hardware_vsel()
3385 if (selector < rdev->desc->linear_min_sel) in regulator_list_hardware_vsel()
3387 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap) in regulator_list_hardware_vsel()
3388 return -EOPNOTSUPP; in regulator_list_hardware_vsel()
3395 * regulator_get_linear_step - return the voltage step size between VSEL values
3396 * @regulator: regulator source
3399 * regulators, or return 0 if the regulator isn't a linear regulator.
3401 unsigned int regulator_get_linear_step(struct regulator *regulator) in regulator_get_linear_step() argument
3403 struct regulator_dev *rdev = regulator->rdev; in regulator_get_linear_step()
3405 return rdev->desc->uV_step; in regulator_get_linear_step()
3410 * regulator_is_supported_voltage - check if a voltage range can be supported
3412 * @regulator: Regulator to check.
3418 int regulator_is_supported_voltage(struct regulator *regulator, in regulator_is_supported_voltage() argument
3421 struct regulator_dev *rdev = regulator->rdev; in regulator_is_supported_voltage()
3426 ret = regulator_get_voltage(regulator); in regulator_is_supported_voltage()
3434 if (rdev->desc->continuous_voltage_range) in regulator_is_supported_voltage()
3435 return min_uV >= rdev->constraints->min_uV && in regulator_is_supported_voltage()
3436 max_uV <= rdev->constraints->max_uV; in regulator_is_supported_voltage()
3438 ret = regulator_count_voltages(regulator); in regulator_is_supported_voltage()
3444 ret = regulator_list_voltage(regulator, i); in regulator_is_supported_voltage()
3457 const struct regulator_desc *desc = rdev->desc; in regulator_map_voltage()
3459 if (desc->ops->map_voltage) in regulator_map_voltage()
3460 return desc->ops->map_voltage(rdev, min_uV, max_uV); in regulator_map_voltage()
3462 if (desc->ops->list_voltage == regulator_list_voltage_linear) in regulator_map_voltage()
3465 if (desc->ops->list_voltage == regulator_list_voltage_linear_range) in regulator_map_voltage()
3468 if (desc->ops->list_voltage == in regulator_map_voltage()
3489 return -EINVAL; in _regulator_call_set_voltage()
3491 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV, selector); in _regulator_call_set_voltage()
3513 return -EINVAL; in _regulator_call_set_voltage_sel()
3515 ret = rdev->desc->ops->set_voltage_sel(rdev, selector); in _regulator_call_set_voltage_sel()
3528 const struct regulator_ops *ops = rdev->desc->ops; in _regulator_set_voltage_sel_step()
3531 /* Stepping is only needed if the regulator is enabled. */ in _regulator_set_voltage_sel_step()
3535 if (!ops->get_voltage_sel) in _regulator_set_voltage_sel_step()
3536 return -EINVAL; in _regulator_set_voltage_sel_step()
3538 old_sel = ops->get_voltage_sel(rdev); in _regulator_set_voltage_sel_step()
3542 diff = new_selector - old_sel; in _regulator_set_voltage_sel_step()
3548 for (curr_sel = old_sel + rdev->desc->vsel_step; in _regulator_set_voltage_sel_step()
3550 curr_sel += rdev->desc->vsel_step) { in _regulator_set_voltage_sel_step()
3557 ret = ops->set_voltage_sel(rdev, curr_sel); in _regulator_set_voltage_sel_step()
3563 for (curr_sel = old_sel - rdev->desc->vsel_step; in _regulator_set_voltage_sel_step()
3565 curr_sel -= rdev->desc->vsel_step) { in _regulator_set_voltage_sel_step()
3566 ret = ops->set_voltage_sel(rdev, curr_sel); in _regulator_set_voltage_sel_step()
3581 (void)ops->set_voltage_sel(rdev, old_sel); in _regulator_set_voltage_sel_step()
3590 if (rdev->constraints->ramp_delay) in _regulator_set_voltage_time()
3591 ramp_delay = rdev->constraints->ramp_delay; in _regulator_set_voltage_time()
3592 else if (rdev->desc->ramp_delay) in _regulator_set_voltage_time()
3593 ramp_delay = rdev->desc->ramp_delay; in _regulator_set_voltage_time()
3594 else if (rdev->constraints->settling_time) in _regulator_set_voltage_time()
3595 return rdev->constraints->settling_time; in _regulator_set_voltage_time()
3596 else if (rdev->constraints->settling_time_up && in _regulator_set_voltage_time()
3598 return rdev->constraints->settling_time_up; in _regulator_set_voltage_time()
3599 else if (rdev->constraints->settling_time_down && in _regulator_set_voltage_time()
3601 return rdev->constraints->settling_time_down; in _regulator_set_voltage_time()
3606 return DIV_ROUND_UP(abs(new_uV - old_uV), ramp_delay); in _regulator_set_voltage_time()
3616 int old_selector = -1; in _regulator_do_set_voltage()
3617 const struct regulator_ops *ops = rdev->desc->ops; in _regulator_do_set_voltage()
3622 min_uV += rdev->constraints->uV_offset; in _regulator_do_set_voltage()
3623 max_uV += rdev->constraints->uV_offset; in _regulator_do_set_voltage()
3630 ops->set_voltage_time_sel && ops->get_voltage_sel) { in _regulator_do_set_voltage()
3631 old_selector = ops->get_voltage_sel(rdev); in _regulator_do_set_voltage()
3636 if (ops->set_voltage) { in _regulator_do_set_voltage()
3641 if (ops->list_voltage) in _regulator_do_set_voltage()
3642 best_val = ops->list_voltage(rdev, in _regulator_do_set_voltage()
3648 } else if (ops->set_voltage_sel) { in _regulator_do_set_voltage()
3651 best_val = ops->list_voltage(rdev, ret); in _regulator_do_set_voltage()
3656 else if (rdev->desc->vsel_step) in _regulator_do_set_voltage()
3663 ret = -EINVAL; in _regulator_do_set_voltage()
3667 ret = -EINVAL; in _regulator_do_set_voltage()
3673 if (ops->set_voltage_time_sel) { in _regulator_do_set_voltage()
3679 delay = ops->set_voltage_time_sel(rdev, old_selector, in _regulator_do_set_voltage()
3683 if (ops->set_voltage_time) in _regulator_do_set_voltage()
3684 delay = ops->set_voltage_time(rdev, old_uV, in _regulator_do_set_voltage()
3722 return -EINVAL; in _regulator_do_set_suspend_voltage()
3724 if (min_uV < rstate->min_uV) in _regulator_do_set_suspend_voltage()
3725 min_uV = rstate->min_uV; in _regulator_do_set_suspend_voltage()
3726 if (max_uV > rstate->max_uV) in _regulator_do_set_suspend_voltage()
3727 max_uV = rstate->max_uV; in _regulator_do_set_suspend_voltage()
3733 uV = rdev->desc->ops->list_voltage(rdev, sel); in _regulator_do_set_suspend_voltage()
3735 rstate->uV = uV; in _regulator_do_set_suspend_voltage()
3740 static int regulator_set_voltage_unlocked(struct regulator *regulator, in regulator_set_voltage_unlocked() argument
3744 struct regulator_dev *rdev = regulator->rdev; in regulator_set_voltage_unlocked()
3745 struct regulator_voltage *voltage = ®ulator->voltage[state]; in regulator_set_voltage_unlocked()
3754 if (voltage->min_uV == min_uV && voltage->max_uV == max_uV) in regulator_set_voltage_unlocked()
3758 * return successfully even though the regulator does not support in regulator_set_voltage_unlocked()
3764 voltage->min_uV = min_uV; in regulator_set_voltage_unlocked()
3765 voltage->max_uV = max_uV; in regulator_set_voltage_unlocked()
3771 if (!rdev->desc->ops->set_voltage && in regulator_set_voltage_unlocked()
3772 !rdev->desc->ops->set_voltage_sel) { in regulator_set_voltage_unlocked()
3773 ret = -EINVAL; in regulator_set_voltage_unlocked()
3783 old_min_uV = voltage->min_uV; in regulator_set_voltage_unlocked()
3784 old_max_uV = voltage->max_uV; in regulator_set_voltage_unlocked()
3785 voltage->min_uV = min_uV; in regulator_set_voltage_unlocked()
3786 voltage->max_uV = max_uV; in regulator_set_voltage_unlocked()
3791 voltage->min_uV = old_min_uV; in regulator_set_voltage_unlocked()
3792 voltage->max_uV = old_max_uV; in regulator_set_voltage_unlocked()
3806 if (rdev->supply && in regulator_set_voltage_rdev()
3807 regulator_ops_is_valid(rdev->supply->rdev, in regulator_set_voltage_rdev()
3809 (rdev->desc->min_dropout_uV || !(rdev->desc->ops->get_voltage || in regulator_set_voltage_rdev()
3810 rdev->desc->ops->get_voltage_sel))) { in regulator_set_voltage_rdev()
3826 best_supply_uV += rdev->desc->min_dropout_uV; in regulator_set_voltage_rdev()
3828 current_supply_uV = regulator_get_voltage_rdev(rdev->supply->rdev); in regulator_set_voltage_rdev()
3834 supply_change_uV = best_supply_uV - current_supply_uV; in regulator_set_voltage_rdev()
3838 ret = regulator_set_voltage_unlocked(rdev->supply, in regulator_set_voltage_rdev()
3841 dev_err(&rdev->dev, "Failed to increase supply voltage: %pe\n", in regulator_set_voltage_rdev()
3856 ret = regulator_set_voltage_unlocked(rdev->supply, in regulator_set_voltage_rdev()
3859 dev_warn(&rdev->dev, "Failed to decrease supply voltage: %pe\n", in regulator_set_voltage_rdev()
3873 struct regulation_constraints *constraints = rdev->constraints; in regulator_limit_voltage_step()
3876 if (!constraints->max_uV_step || !_regulator_is_enabled(rdev)) in regulator_limit_voltage_step()
3886 if (abs(*current_uV - *min_uV) <= constraints->max_uV_step) in regulator_limit_voltage_step()
3891 *min_uV = min(*current_uV + constraints->max_uV_step, in regulator_limit_voltage_step()
3894 *min_uV = max(*current_uV - constraints->max_uV_step, in regulator_limit_voltage_step()
3906 struct coupling_desc *c_desc = &rdev->coupling_desc; in regulator_get_optimal_voltage()
3907 struct regulator_dev **c_rdevs = c_desc->coupled_rdevs; in regulator_get_optimal_voltage()
3908 struct regulation_constraints *constraints = rdev->constraints; in regulator_get_optimal_voltage()
3915 *current_uV = -1; in regulator_get_optimal_voltage()
3926 desired_min_uV = constraints->min_uV; in regulator_get_optimal_voltage()
3927 desired_max_uV = constraints->max_uV; in regulator_get_optimal_voltage()
3946 lockdep_assert_held_once(&c_rdevs[i]->mutex.base); in regulator_get_optimal_voltage()
3966 max_spread = constraints->max_spread[0]; in regulator_get_optimal_voltage()
3973 target_uV = max(desired_min_uV, highest_min_uV - max_spread); in regulator_get_optimal_voltage()
4001 possible_uV = max(target_uV, max_current_uV - max_spread); in regulator_get_optimal_voltage()
4006 return -EINVAL; in regulator_get_optimal_voltage()
4024 if (n_coupled > 1 && *current_uV == -1) { in regulator_get_optimal_voltage()
4048 struct coupling_desc *c_desc = &rdev->coupling_desc; in regulator_do_balance_voltage()
4054 c_rdevs = c_desc->coupled_rdevs; in regulator_do_balance_voltage()
4055 n_coupled = skip_coupled ? 1 : c_desc->n_coupled; in regulator_do_balance_voltage()
4076 * i-th regulator at the moment without violating in regulator_do_balance_voltage()
4093 delta = abs(optimal_uV - current_uV); in regulator_do_balance_voltage()
4129 struct coupling_desc *c_desc = &rdev->coupling_desc; in regulator_balance_voltage()
4130 struct regulator_coupler *coupler = c_desc->coupler; in regulator_balance_voltage()
4134 * If system is in a state other than PM_SUSPEND_ON, don't check in regulator_balance_voltage()
4140 if (c_desc->n_resolved < c_desc->n_coupled) { in regulator_balance_voltage()
4142 return -EPERM; in regulator_balance_voltage()
4146 if (coupler && coupler->balance_voltage) in regulator_balance_voltage()
4147 return coupler->balance_voltage(coupler, rdev, state); in regulator_balance_voltage()
4153 * regulator_set_voltage - set regulator output voltage
4154 * @regulator: regulator source
4158 * Sets a voltage regulator to the desired output voltage. This can be set
4159 * during any regulator state. IOW, regulator can be disabled or enabled.
4161 * If the regulator is enabled then the voltage will change to the new value
4162 * immediately otherwise if the regulator is disabled the regulator will
4165 * NOTE: If the regulator is shared between several devices then the lowest
4166 * request voltage that meets the system constraints will be used.
4167 * Regulator system constraints must be set for this regulator before
4170 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV) in regulator_set_voltage() argument
4175 regulator_lock_dependent(regulator->rdev, &ww_ctx); in regulator_set_voltage()
4177 ret = regulator_set_voltage_unlocked(regulator, min_uV, max_uV, in regulator_set_voltage()
4180 regulator_unlock_dependent(regulator->rdev, &ww_ctx); in regulator_set_voltage()
4193 return -EINVAL; in regulator_suspend_toggle()
4195 if (!rstate->changeable) in regulator_suspend_toggle()
4196 return -EPERM; in regulator_suspend_toggle()
4198 rstate->enabled = (en) ? ENABLE_IN_SUSPEND : DISABLE_IN_SUSPEND; in regulator_suspend_toggle()
4213 struct regulator *regulator; in regulator_suspend_disable() local
4217 * if any consumer wants this regulator device keeping on in in regulator_suspend_disable()
4220 list_for_each_entry(regulator, &rdev->consumer_list, list) { in regulator_suspend_disable()
4221 voltage = ®ulator->voltage[state]; in regulator_suspend_disable()
4222 if (voltage->min_uV || voltage->max_uV) in regulator_suspend_disable()
4230 static int _regulator_set_suspend_voltage(struct regulator *regulator, in _regulator_set_suspend_voltage() argument
4234 struct regulator_dev *rdev = regulator->rdev; in _regulator_set_suspend_voltage()
4239 return -EINVAL; in _regulator_set_suspend_voltage()
4241 if (rstate->min_uV == rstate->max_uV) { in _regulator_set_suspend_voltage()
4243 return -EPERM; in _regulator_set_suspend_voltage()
4246 return regulator_set_voltage_unlocked(regulator, min_uV, max_uV, state); in _regulator_set_suspend_voltage()
4249 int regulator_set_suspend_voltage(struct regulator *regulator, int min_uV, in regulator_set_suspend_voltage() argument
4257 return -EINVAL; in regulator_set_suspend_voltage()
4259 regulator_lock_dependent(regulator->rdev, &ww_ctx); in regulator_set_suspend_voltage()
4261 ret = _regulator_set_suspend_voltage(regulator, min_uV, in regulator_set_suspend_voltage()
4264 regulator_unlock_dependent(regulator->rdev, &ww_ctx); in regulator_set_suspend_voltage()
4271 * regulator_set_voltage_time - get raise/fall time
4272 * @regulator: regulator source
4280 int regulator_set_voltage_time(struct regulator *regulator, in regulator_set_voltage_time() argument
4283 struct regulator_dev *rdev = regulator->rdev; in regulator_set_voltage_time()
4284 const struct regulator_ops *ops = rdev->desc->ops; in regulator_set_voltage_time()
4285 int old_sel = -1; in regulator_set_voltage_time()
4286 int new_sel = -1; in regulator_set_voltage_time()
4290 if (ops->set_voltage_time) in regulator_set_voltage_time()
4291 return ops->set_voltage_time(rdev, old_uV, new_uV); in regulator_set_voltage_time()
4292 else if (!ops->set_voltage_time_sel) in regulator_set_voltage_time()
4296 if (!ops->list_voltage || !rdev->desc->n_voltages) in regulator_set_voltage_time()
4297 return -EINVAL; in regulator_set_voltage_time()
4299 for (i = 0; i < rdev->desc->n_voltages; i++) { in regulator_set_voltage_time()
4301 if (i < rdev->desc->linear_min_sel) in regulator_set_voltage_time()
4307 voltage = regulator_list_voltage(regulator, i); in regulator_set_voltage_time()
4309 return -EINVAL; in regulator_set_voltage_time()
4319 return -EINVAL; in regulator_set_voltage_time()
4321 return ops->set_voltage_time_sel(rdev, old_sel, new_sel); in regulator_set_voltage_time()
4326 * regulator_set_voltage_time_sel - get raise/fall time
4327 * @rdev: regulator source device
4344 if (!rdev->desc->ops->list_voltage) in regulator_set_voltage_time_sel()
4345 return -EINVAL; in regulator_set_voltage_time_sel()
4347 old_volt = rdev->desc->ops->list_voltage(rdev, old_selector); in regulator_set_voltage_time_sel()
4348 new_volt = rdev->desc->ops->list_voltage(rdev, new_selector); in regulator_set_voltage_time_sel()
4350 if (rdev->desc->ops->set_voltage_time) in regulator_set_voltage_time_sel()
4351 return rdev->desc->ops->set_voltage_time(rdev, old_volt, in regulator_set_voltage_time_sel()
4364 if (!rdev->desc->ops->set_voltage && in regulator_sync_voltage_rdev()
4365 !rdev->desc->ops->set_voltage_sel) { in regulator_sync_voltage_rdev()
4366 ret = -EINVAL; in regulator_sync_voltage_rdev()
4370 /* balance only, if regulator is coupled */ in regulator_sync_voltage_rdev()
4371 if (rdev->coupling_desc.n_coupled > 1) in regulator_sync_voltage_rdev()
4374 ret = -EOPNOTSUPP; in regulator_sync_voltage_rdev()
4382 * regulator_sync_voltage - re-apply last regulator output voltage
4383 * @regulator: regulator source
4385 * Re-apply the last configured voltage. This is intended to be used
4389 int regulator_sync_voltage(struct regulator *regulator) in regulator_sync_voltage() argument
4391 struct regulator_dev *rdev = regulator->rdev; in regulator_sync_voltage()
4392 struct regulator_voltage *voltage = ®ulator->voltage[PM_SUSPEND_ON]; in regulator_sync_voltage()
4400 if (!rdev->desc->ops->set_voltage && in regulator_sync_voltage()
4401 !rdev->desc->ops->set_voltage_sel) { in regulator_sync_voltage()
4402 ret = -EINVAL; in regulator_sync_voltage()
4407 if (!voltage->min_uV && !voltage->max_uV) { in regulator_sync_voltage()
4408 ret = -EINVAL; in regulator_sync_voltage()
4412 min_uV = voltage->min_uV; in regulator_sync_voltage()
4413 max_uV = voltage->max_uV; in regulator_sync_voltage()
4424 /* balance only, if regulator is coupled */ in regulator_sync_voltage()
4425 if (rdev->coupling_desc.n_coupled > 1) in regulator_sync_voltage()
4441 if (rdev->desc->ops->get_bypass) { in regulator_get_voltage_rdev()
4442 ret = rdev->desc->ops->get_bypass(rdev, &bypassed); in regulator_get_voltage_rdev()
4446 /* if bypassed the regulator must have a supply */ in regulator_get_voltage_rdev()
4447 if (!rdev->supply) { in regulator_get_voltage_rdev()
4449 "bypassed regulator has no supply!\n"); in regulator_get_voltage_rdev()
4450 return -EPROBE_DEFER; in regulator_get_voltage_rdev()
4453 return regulator_get_voltage_rdev(rdev->supply->rdev); in regulator_get_voltage_rdev()
4457 if (rdev->desc->ops->get_voltage_sel) { in regulator_get_voltage_rdev()
4458 sel = rdev->desc->ops->get_voltage_sel(rdev); in regulator_get_voltage_rdev()
4461 ret = rdev->desc->ops->list_voltage(rdev, sel); in regulator_get_voltage_rdev()
4462 } else if (rdev->desc->ops->get_voltage) { in regulator_get_voltage_rdev()
4463 ret = rdev->desc->ops->get_voltage(rdev); in regulator_get_voltage_rdev()
4464 } else if (rdev->desc->ops->list_voltage) { in regulator_get_voltage_rdev()
4465 ret = rdev->desc->ops->list_voltage(rdev, 0); in regulator_get_voltage_rdev()
4466 } else if (rdev->desc->fixed_uV && (rdev->desc->n_voltages == 1)) { in regulator_get_voltage_rdev()
4467 ret = rdev->desc->fixed_uV; in regulator_get_voltage_rdev()
4468 } else if (rdev->supply) { in regulator_get_voltage_rdev()
4469 ret = regulator_get_voltage_rdev(rdev->supply->rdev); in regulator_get_voltage_rdev()
4470 } else if (rdev->supply_name) { in regulator_get_voltage_rdev()
4471 return -EPROBE_DEFER; in regulator_get_voltage_rdev()
4473 return -EINVAL; in regulator_get_voltage_rdev()
4478 return ret - rdev->constraints->uV_offset; in regulator_get_voltage_rdev()
4483 * regulator_get_voltage - get regulator output voltage
4484 * @regulator: regulator source
4486 * This returns the current regulator voltage in uV.
4488 * NOTE: If the regulator is disabled it will return the voltage value. This
4489 * function should not be used to determine regulator state.
4491 int regulator_get_voltage(struct regulator *regulator) in regulator_get_voltage() argument
4496 regulator_lock_dependent(regulator->rdev, &ww_ctx); in regulator_get_voltage()
4497 ret = regulator_get_voltage_rdev(regulator->rdev); in regulator_get_voltage()
4498 regulator_unlock_dependent(regulator->rdev, &ww_ctx); in regulator_get_voltage()
4505 * regulator_set_current_limit - set regulator output current limit
4506 * @regulator: regulator source
4511 * any regulator state. IOW, regulator can be disabled or enabled.
4513 * If the regulator is enabled then the current will change to the new value
4514 * immediately otherwise if the regulator is disabled the regulator will
4517 * NOTE: Regulator system constraints must be set for this regulator before
4520 int regulator_set_current_limit(struct regulator *regulator, in regulator_set_current_limit() argument
4523 struct regulator_dev *rdev = regulator->rdev; in regulator_set_current_limit()
4529 if (!rdev->desc->ops->set_current_limit) { in regulator_set_current_limit()
4530 ret = -EINVAL; in regulator_set_current_limit()
4539 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA); in regulator_set_current_limit()
4549 if (!rdev->desc->ops->get_current_limit) in _regulator_get_current_limit_unlocked()
4550 return -EINVAL; in _regulator_get_current_limit_unlocked()
4552 return rdev->desc->ops->get_current_limit(rdev); in _regulator_get_current_limit_unlocked()
4567 * regulator_get_current_limit - get regulator output current
4568 * @regulator: regulator source
4572 * NOTE: If the regulator is disabled it will return the current value. This
4573 * function should not be used to determine regulator state.
4575 int regulator_get_current_limit(struct regulator *regulator) in regulator_get_current_limit() argument
4577 return _regulator_get_current_limit(regulator->rdev); in regulator_get_current_limit()
4582 * regulator_set_mode - set regulator operating mode
4583 * @regulator: regulator source
4584 * @mode: operating mode - one of the REGULATOR_MODE constants
4586 * Set regulator operating mode to increase regulator efficiency or improve
4589 * NOTE: Regulator system constraints must be set for this regulator before
4592 int regulator_set_mode(struct regulator *regulator, unsigned int mode) in regulator_set_mode() argument
4594 struct regulator_dev *rdev = regulator->rdev; in regulator_set_mode()
4601 if (!rdev->desc->ops->set_mode) { in regulator_set_mode()
4602 ret = -EINVAL; in regulator_set_mode()
4607 if (rdev->desc->ops->get_mode) { in regulator_set_mode()
4608 regulator_curr_mode = rdev->desc->ops->get_mode(rdev); in regulator_set_mode()
4620 ret = rdev->desc->ops->set_mode(rdev, mode); in regulator_set_mode()
4630 if (!rdev->desc->ops->get_mode) in _regulator_get_mode_unlocked()
4631 return -EINVAL; in _regulator_get_mode_unlocked()
4633 return rdev->desc->ops->get_mode(rdev); in _regulator_get_mode_unlocked()
4648 * regulator_get_mode - get regulator operating mode
4649 * @regulator: regulator source
4651 * Get the current regulator operating mode.
4653 unsigned int regulator_get_mode(struct regulator *regulator) in regulator_get_mode() argument
4655 return _regulator_get_mode(regulator->rdev); in regulator_get_mode()
4663 if (rdev->use_cached_err) { in rdev_get_cached_err_flags()
4664 spin_lock(&rdev->err_lock); in rdev_get_cached_err_flags()
4665 ret = rdev->cached_err; in rdev_get_cached_err_flags()
4666 spin_unlock(&rdev->err_lock); in rdev_get_cached_err_flags()
4680 if (rdev->desc->ops->get_error_flags) in _regulator_get_error_flags()
4681 ret = rdev->desc->ops->get_error_flags(rdev, flags); in _regulator_get_error_flags()
4682 else if (!rdev->use_cached_err) in _regulator_get_error_flags()
4683 ret = -EINVAL; in _regulator_get_error_flags()
4693 * regulator_get_error_flags - get regulator error information
4694 * @regulator: regulator source
4697 * Get the current regulator error information.
4699 int regulator_get_error_flags(struct regulator *regulator, in regulator_get_error_flags() argument
4702 return _regulator_get_error_flags(regulator->rdev, flags); in regulator_get_error_flags()
4707 * regulator_set_load - set regulator load
4708 * @regulator: regulator source
4711 * Notifies the regulator core of a new device load. This is then used by
4712 * DRMS (if enabled by constraints) to set the most efficient regulator
4713 * operating mode for the new regulator loading.
4715 * Consumer devices notify their supply regulator of the maximum power
4719 * consumption are :-
4727 * DRMS will sum the total requested load on the regulator and change
4730 * NOTE: when a regulator consumer requests to have a regulator
4732 * toward the total requested load. If the regulator is re-enabled
4735 * If a regulator is an always-on regulator then an individual consumer's
4740 int regulator_set_load(struct regulator *regulator, int uA_load) in regulator_set_load() argument
4742 struct regulator_dev *rdev = regulator->rdev; in regulator_set_load()
4747 old_uA_load = regulator->uA_load; in regulator_set_load()
4748 regulator->uA_load = uA_load; in regulator_set_load()
4749 if (regulator->enable_count && old_uA_load != uA_load) { in regulator_set_load()
4752 regulator->uA_load = old_uA_load; in regulator_set_load()
4761 * regulator_allow_bypass - allow the regulator to go into bypass mode
4763 * @regulator: Regulator to configure
4766 * Allow the regulator to go into bypass mode if all other consumers
4767 * for the regulator also enable bypass mode and the machine
4768 * constraints allow this. Bypass mode means that the regulator is
4771 int regulator_allow_bypass(struct regulator *regulator, bool enable) in regulator_allow_bypass() argument
4773 struct regulator_dev *rdev = regulator->rdev; in regulator_allow_bypass()
4777 if (!rdev->desc->ops->set_bypass) in regulator_allow_bypass()
4785 if (enable && !regulator->bypass) { in regulator_allow_bypass()
4786 rdev->bypass_count++; in regulator_allow_bypass()
4788 if (rdev->bypass_count == rdev->open_count) { in regulator_allow_bypass()
4791 ret = rdev->desc->ops->set_bypass(rdev, enable); in regulator_allow_bypass()
4793 rdev->bypass_count--; in regulator_allow_bypass()
4798 } else if (!enable && regulator->bypass) { in regulator_allow_bypass()
4799 rdev->bypass_count--; in regulator_allow_bypass()
4801 if (rdev->bypass_count != rdev->open_count) { in regulator_allow_bypass()
4804 ret = rdev->desc->ops->set_bypass(rdev, enable); in regulator_allow_bypass()
4806 rdev->bypass_count++; in regulator_allow_bypass()
4813 regulator->bypass = enable; in regulator_allow_bypass()
4822 * regulator_register_notifier - register regulator event notifier
4823 * @regulator: regulator source
4826 * Register notifier block to receive regulator events.
4828 int regulator_register_notifier(struct regulator *regulator, in regulator_register_notifier() argument
4831 return blocking_notifier_chain_register(®ulator->rdev->notifier, in regulator_register_notifier()
4837 * regulator_unregister_notifier - unregister regulator event notifier
4838 * @regulator: regulator source
4841 * Unregister regulator event notifier block.
4843 int regulator_unregister_notifier(struct regulator *regulator, in regulator_unregister_notifier() argument
4846 return blocking_notifier_chain_unregister(®ulator->rdev->notifier, in regulator_unregister_notifier()
4851 /* notify regulator consumers and downstream regulator consumers.
4858 int ret = blocking_notifier_call_chain(&rdev->notifier, event, data); in _notifier_call_chain()
4861 struct device *parent = rdev->dev.parent; in _notifier_call_chain()
4866 if (parent && rname == rdev->desc->name) { in _notifier_call_chain()
4867 snprintf(name, sizeof(name), "%s-%s", dev_name(parent), in _notifier_call_chain()
4910 while (--i >= 0) in _regulator_bulk_get()
4917 * regulator_bulk_get - get multiple regulator consumers
4925 * This helper function allows drivers to get several regulator
4941 bulk->ret = regulator_enable(bulk->consumer); in regulator_bulk_enable_async()
4945 * regulator_bulk_enable - enable multiple regulator consumers
4951 * This convenience API allows consumers to enable multiple regulator
4994 * regulator_bulk_disable - disable multiple regulator consumers
5000 * This convenience API allows consumers to disable multiple regulator
5011 for (i = num_consumers - 1; i >= 0; --i) { in regulator_bulk_disable()
5024 pr_err("Failed to re-enable %s: %pe\n", in regulator_bulk_disable()
5033 * regulator_bulk_force_disable - force disable multiple regulator consumers
5039 * This convenience API allows consumers to forcibly disable multiple regulator
5066 * regulator_bulk_free - free multiple regulator consumers
5071 * This convenience API allows consumers to free multiple regulator
5087 * regulator_handle_critical - Handle events for system-critical regulators.
5088 * @rdev: The regulator device.
5091 * This function handles critical events such as under-voltage, over-current,
5092 * and unknown errors for regulators deemed system-critical. On detecting such
5100 if (!rdev->constraints->system_critical) in regulator_handle_critical()
5105 reason = "System critical regulator: voltage drop detected"; in regulator_handle_critical()
5108 reason = "System critical regulator: over-current detected"; in regulator_handle_critical()
5111 reason = "System critical regulator: unknown error"; in regulator_handle_critical()
5118 rdev->constraints->uv_less_critical_window_ms); in regulator_handle_critical()
5122 * regulator_notifier_call_chain - call regulator event notifier
5123 * @rdev: regulator source
5125 * @data: callback-specific data.
5127 * Called by regulator drivers to notify clients a regulator event has
5142 * regulator_mode_to_status - convert a regulator mode into a status
5146 * Convert a regulator mode into a status.
5210 const struct regulator_ops *ops = rdev->desc->ops; in regulator_attr_is_visible()
5211 umode_t mode = attr->mode; in regulator_attr_is_visible()
5221 if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) || in regulator_attr_is_visible()
5222 (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) || in regulator_attr_is_visible()
5223 (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0) || in regulator_attr_is_visible()
5224 (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1)) in regulator_attr_is_visible()
5230 return ops->get_current_limit ? mode : 0; in regulator_attr_is_visible()
5233 return ops->get_mode ? mode : 0; in regulator_attr_is_visible()
5236 return (rdev->ena_pin || ops->is_enabled) ? mode : 0; in regulator_attr_is_visible()
5239 return ops->get_status ? mode : 0; in regulator_attr_is_visible()
5242 return ops->get_bypass ? mode : 0; in regulator_attr_is_visible()
5253 return ops->get_error_flags ? mode : 0; in regulator_attr_is_visible()
5258 return (ops->set_voltage || ops->set_voltage_sel) ? mode : 0; in regulator_attr_is_visible()
5262 return ops->set_current_limit ? mode : 0; in regulator_attr_is_visible()
5272 return ops->set_suspend_voltage ? mode : 0; in regulator_attr_is_visible()
5277 return ops->set_suspend_mode ? mode : 0; in regulator_attr_is_visible()
5296 debugfs_remove_recursive(rdev->debugfs); in regulator_dev_release()
5297 kfree(rdev->constraints); in regulator_dev_release()
5298 of_node_put(rdev->dev.of_node); in regulator_dev_release()
5304 struct device *parent = rdev->dev.parent; in rdev_init_debugfs()
5309 if (parent && rname == rdev->desc->name) { in rdev_init_debugfs()
5310 snprintf(name, sizeof(name), "%s-%s", dev_name(parent), in rdev_init_debugfs()
5315 rdev->debugfs = debugfs_create_dir(rname, debugfs_root); in rdev_init_debugfs()
5316 if (IS_ERR(rdev->debugfs)) in rdev_init_debugfs()
5319 debugfs_create_u32("use_count", 0444, rdev->debugfs, in rdev_init_debugfs()
5320 &rdev->use_count); in rdev_init_debugfs()
5321 debugfs_create_u32("open_count", 0444, rdev->debugfs, in rdev_init_debugfs()
5322 &rdev->open_count); in rdev_init_debugfs()
5323 debugfs_create_u32("bypass_count", 0444, rdev->debugfs, in rdev_init_debugfs()
5324 &rdev->bypass_count); in rdev_init_debugfs()
5340 list_add_tail(&coupler->list, ®ulator_coupler_list); in regulator_coupler_register()
5358 err = coupler->attach_regulator(coupler, rdev); in regulator_find_coupler()
5360 if (!coupler->balance_voltage && in regulator_find_coupler()
5361 rdev->coupling_desc.n_coupled > 2) in regulator_find_coupler()
5376 return ERR_PTR(-EINVAL); in regulator_find_coupler()
5379 if (coupler->detach_regulator) in regulator_find_coupler()
5380 coupler->detach_regulator(coupler, rdev); in regulator_find_coupler()
5383 "Voltage balancing for multiple regulator couples is unimplemented\n"); in regulator_find_coupler()
5385 return ERR_PTR(-EPERM); in regulator_find_coupler()
5390 struct regulator_coupler *coupler = rdev->coupling_desc.coupler; in regulator_resolve_coupling()
5391 struct coupling_desc *c_desc = &rdev->coupling_desc; in regulator_resolve_coupling()
5392 int n_coupled = c_desc->n_coupled; in regulator_resolve_coupling()
5398 if (c_desc->coupled_rdevs[i]) in regulator_resolve_coupling()
5401 c_rdev = of_parse_coupled_regulator(rdev, i - 1); in regulator_resolve_coupling()
5406 if (c_rdev->coupling_desc.coupler != coupler) { in regulator_resolve_coupling()
5412 c_desc->coupled_rdevs[i] = c_rdev; in regulator_resolve_coupling()
5413 c_desc->n_resolved++; in regulator_resolve_coupling()
5421 struct regulator_coupler *coupler = rdev->coupling_desc.coupler; in regulator_remove_coupling()
5422 struct coupling_desc *__c_desc, *c_desc = &rdev->coupling_desc; in regulator_remove_coupling()
5428 n_coupled = c_desc->n_coupled; in regulator_remove_coupling()
5431 c_rdev = c_desc->coupled_rdevs[i]; in regulator_remove_coupling()
5438 __c_desc = &c_rdev->coupling_desc; in regulator_remove_coupling()
5439 __n_coupled = __c_desc->n_coupled; in regulator_remove_coupling()
5442 __c_rdev = __c_desc->coupled_rdevs[k]; in regulator_remove_coupling()
5445 __c_desc->coupled_rdevs[k] = NULL; in regulator_remove_coupling()
5446 __c_desc->n_resolved--; in regulator_remove_coupling()
5453 c_desc->coupled_rdevs[i] = NULL; in regulator_remove_coupling()
5454 c_desc->n_resolved--; in regulator_remove_coupling()
5457 if (coupler && coupler->detach_regulator) { in regulator_remove_coupling()
5458 err = coupler->detach_regulator(coupler, rdev); in regulator_remove_coupling()
5464 kfree(rdev->coupling_desc.coupled_rdevs); in regulator_remove_coupling()
5465 rdev->coupling_desc.coupled_rdevs = NULL; in regulator_remove_coupling()
5480 return -ENOMEM; in regulator_init_coupling()
5482 rdev->coupling_desc.coupled_rdevs = coupled; in regulator_init_coupling()
5485 * Every regulator should always have coupling descriptor filled with in regulator_init_coupling()
5488 rdev->coupling_desc.coupled_rdevs[0] = rdev; in regulator_init_coupling()
5489 rdev->coupling_desc.n_coupled = n_phandles + 1; in regulator_init_coupling()
5490 rdev->coupling_desc.n_resolved++; in regulator_init_coupling()
5492 /* regulator isn't coupled */ in regulator_init_coupling()
5497 return -EPERM; in regulator_init_coupling()
5500 rdev->coupling_desc.coupler = regulator_find_coupler(rdev); in regulator_init_coupling()
5503 if (IS_ERR(rdev->coupling_desc.coupler)) { in regulator_init_coupling()
5504 err = PTR_ERR(rdev->coupling_desc.coupler); in regulator_init_coupling()
5515 if (rdev->coupling_desc.n_coupled > 2) { in generic_coupler_attach()
5517 "Voltage balancing for multiple regulator couples is unimplemented\n"); in generic_coupler_attach()
5518 return -EPERM; in generic_coupler_attach()
5521 if (!rdev->constraints->always_on) { in generic_coupler_attach()
5523 "Coupling of a non always-on regulator is unimplemented\n"); in generic_coupler_attach()
5524 return -ENOTSUPP; in generic_coupler_attach()
5535 * regulator_register - register regulator
5536 * @dev: the device that drive the regulator
5537 * @regulator_desc: regulator to register
5538 * @cfg: runtime configuration for regulator
5540 * Called by regulator drivers to register a regulator.
5551 static atomic_t regulator_no = ATOMIC_INIT(-1); in regulator_register()
5559 return ERR_PTR(-EINVAL); in regulator_register()
5560 if (cfg->ena_gpiod) in regulator_register()
5563 ret = -EINVAL; in regulator_register()
5567 WARN_ON(!dev || !cfg->dev); in regulator_register()
5569 if (regulator_desc->name == NULL || regulator_desc->ops == NULL) { in regulator_register()
5570 ret = -EINVAL; in regulator_register()
5574 if (regulator_desc->type != REGULATOR_VOLTAGE && in regulator_register()
5575 regulator_desc->type != REGULATOR_CURRENT) { in regulator_register()
5576 ret = -EINVAL; in regulator_register()
5581 WARN_ON(regulator_desc->ops->get_voltage && in regulator_register()
5582 regulator_desc->ops->get_voltage_sel); in regulator_register()
5583 WARN_ON(regulator_desc->ops->set_voltage && in regulator_register()
5584 regulator_desc->ops->set_voltage_sel); in regulator_register()
5587 if (regulator_desc->ops->get_voltage_sel && in regulator_register()
5588 !regulator_desc->ops->list_voltage) { in regulator_register()
5589 ret = -EINVAL; in regulator_register()
5592 if (regulator_desc->ops->set_voltage_sel && in regulator_register()
5593 !regulator_desc->ops->list_voltage) { in regulator_register()
5594 ret = -EINVAL; in regulator_register()
5600 ret = -ENOMEM; in regulator_register()
5603 device_initialize(&rdev->dev); in regulator_register()
5604 dev_set_drvdata(&rdev->dev, rdev); in regulator_register()
5605 rdev->dev.class = ®ulator_class; in regulator_register()
5606 spin_lock_init(&rdev->err_lock); in regulator_register()
5614 ret = -ENOMEM; in regulator_register()
5619 &rdev->dev.of_node); in regulator_register()
5626 if (PTR_ERR(init_data) == -EPROBE_DEFER) { in regulator_register()
5627 ret = -EPROBE_DEFER; in regulator_register()
5639 if (!cfg->ena_gpiod && config->ena_gpiod) in regulator_register()
5642 init_data = config->init_data; in regulator_register()
5643 rdev->dev.of_node = of_node_get(config->of_node); in regulator_register()
5646 ww_mutex_init(&rdev->mutex, ®ulator_ww_class); in regulator_register()
5647 rdev->reg_data = config->driver_data; in regulator_register()
5648 rdev->owner = regulator_desc->owner; in regulator_register()
5649 rdev->desc = regulator_desc; in regulator_register()
5650 if (config->regmap) in regulator_register()
5651 rdev->regmap = config->regmap; in regulator_register()
5653 rdev->regmap = dev_get_regmap(dev, NULL); in regulator_register()
5654 else if (dev->parent) in regulator_register()
5655 rdev->regmap = dev_get_regmap(dev->parent, NULL); in regulator_register()
5656 INIT_LIST_HEAD(&rdev->consumer_list); in regulator_register()
5657 INIT_LIST_HEAD(&rdev->list); in regulator_register()
5658 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier); in regulator_register()
5659 INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work); in regulator_register()
5661 if (init_data && init_data->supply_regulator) in regulator_register()
5662 rdev->supply_name = init_data->supply_regulator; in regulator_register()
5663 else if (regulator_desc->supply_name) in regulator_register()
5664 rdev->supply_name = regulator_desc->supply_name; in regulator_register()
5667 rdev->dev.parent = config->dev; in regulator_register()
5668 dev_set_name(&rdev->dev, "regulator.%lu", in regulator_register()
5671 /* set regulator constraints */ in regulator_register()
5673 rdev->constraints = kmemdup(&init_data->constraints, in regulator_register()
5674 sizeof(*rdev->constraints), in regulator_register()
5677 rdev->constraints = kzalloc(sizeof(*rdev->constraints), in regulator_register()
5679 if (!rdev->constraints) { in regulator_register()
5680 ret = -ENOMEM; in regulator_register()
5684 if ((rdev->supply_name && !rdev->supply) && in regulator_register()
5685 (rdev->constraints->always_on || in regulator_register()
5686 rdev->constraints->boot_on)) { in regulator_register()
5695 /* perform any regulator specific init */ in regulator_register()
5696 if (init_data && init_data->regulator_init) { in regulator_register()
5697 ret = init_data->regulator_init(rdev->reg_data); in regulator_register()
5702 if (config->ena_gpiod) { in regulator_register()
5709 /* The regulator core took over the GPIO descriptor */ in regulator_register()
5715 if (ret == -EPROBE_DEFER && !resolved_early) { in regulator_register()
5716 /* Regulator might be in bypass mode and so needs its supply in regulator_register()
5719 /* FIXME: this currently triggers a chicken-and-egg problem in regulator_register()
5720 * when creating -SUPPLY symlink in sysfs to a regulator in regulator_register()
5724 rdev->supply_name); in regulator_register()
5741 for (i = 0; i < init_data->num_consumer_supplies; i++) { in regulator_register()
5743 init_data->consumer_supplies[i].dev_name, in regulator_register()
5744 init_data->consumer_supplies[i].supply); in regulator_register()
5747 init_data->consumer_supplies[i].supply); in regulator_register()
5753 if (!rdev->desc->ops->get_voltage && in regulator_register()
5754 !rdev->desc->ops->list_voltage && in regulator_register()
5755 !rdev->desc->fixed_uV) in regulator_register()
5756 rdev->is_switch = true; in regulator_register()
5758 ret = device_add(&rdev->dev); in regulator_register()
5781 regulator_put(rdev->supply); in regulator_register()
5782 kfree(rdev->coupling_desc.coupled_rdevs); in regulator_register()
5788 gpiod_put(config->ena_gpiod); in regulator_register()
5790 put_device(&rdev->dev); in regulator_register()
5793 gpiod_put(cfg->ena_gpiod); in regulator_register()
5799 * regulator_unregister - unregister regulator
5800 * @rdev: regulator to unregister
5802 * Called by regulator drivers to unregister a regulator.
5809 if (rdev->supply) { in regulator_unregister()
5810 while (rdev->use_count--) in regulator_unregister()
5811 regulator_disable(rdev->supply); in regulator_unregister()
5812 regulator_put(rdev->supply); in regulator_unregister()
5815 flush_work(&rdev->disable_work.work); in regulator_unregister()
5819 WARN_ON(rdev->open_count); in regulator_unregister()
5822 list_del(&rdev->list); in regulator_unregister()
5824 device_unregister(&rdev->dev); in regulator_unregister()
5832 * regulator_suspend - prepare regulators for system wide suspend
5835 * Configure each regulator with it's suspend operating parameters for state.
5867 if (!rdev->desc->ops->resume) in regulator_resume()
5872 if (rstate->enabled == ENABLE_IN_SUSPEND || in regulator_resume()
5873 rstate->enabled == DISABLE_IN_SUSPEND) in regulator_resume()
5874 ret = rdev->desc->ops->resume(rdev); in regulator_resume()
5895 .name = "regulator",
5903 * regulator_has_full_constraints - the system has fully specified constraints
5905 * Calling this function will cause the regulator API to disable all
5920 * rdev_get_drvdata - get rdev regulator driver data
5921 * @rdev: regulator
5923 * Get rdev regulator driver private data. This call can be used in the
5924 * regulator driver context.
5928 return rdev->reg_data; in rdev_get_drvdata()
5933 * regulator_get_drvdata - get regulator driver data
5934 * @regulator: regulator
5936 * Get regulator driver private data. This call can be used in the consumer
5937 * driver context when non API regulator specific functions need to be called.
5939 void *regulator_get_drvdata(struct regulator *regulator) in regulator_get_drvdata() argument
5941 return regulator->rdev->reg_data; in regulator_get_drvdata()
5946 * regulator_set_drvdata - set regulator driver data
5947 * @regulator: regulator
5950 void regulator_set_drvdata(struct regulator *regulator, void *data) in regulator_set_drvdata() argument
5952 regulator->rdev->reg_data = data; in regulator_set_drvdata()
5957 * rdev_get_id - get regulator ID
5958 * @rdev: regulator
5962 return rdev->desc->id; in rdev_get_id()
5968 return &rdev->dev; in rdev_get_dev()
5974 return rdev->regmap; in rdev_get_regmap()
5980 return reg_init_data->driver_data; in regulator_get_init_drvdata()
5990 seq_printf(sf, "%s -> %s.%s\n", in supply_map_show()
5991 rdev_get_name(map->regulator), map->dev_name, in supply_map_show()
5992 map->supply); in supply_map_show()
6014 if (rdev->supply && rdev->supply->rdev == summary_data->parent) in regulator_summary_show_children()
6015 regulator_summary_show_subtree(summary_data->s, rdev, in regulator_summary_show_children()
6016 summary_data->level + 1); in regulator_summary_show_children()
6026 struct regulator *consumer; in regulator_summary_show_subtree()
6034 seq_printf(s, "%*s%-*s %3d %4d %6d %7s ", in regulator_summary_show_subtree()
6036 30 - level * 3, rdev_get_name(rdev), in regulator_summary_show_subtree()
6037 rdev->use_count, rdev->open_count, rdev->bypass_count, in regulator_summary_show_subtree()
6044 c = rdev->constraints; in regulator_summary_show_subtree()
6046 switch (rdev->desc->type) { in regulator_summary_show_subtree()
6049 c->min_uV / 1000, c->max_uV / 1000); in regulator_summary_show_subtree()
6053 c->min_uA / 1000, c->max_uA / 1000); in regulator_summary_show_subtree()
6060 list_for_each_entry(consumer, &rdev->consumer_list, list) { in regulator_summary_show_subtree()
6061 if (consumer->dev && consumer->dev->class == ®ulator_class) in regulator_summary_show_subtree()
6064 seq_printf(s, "%*s%-*s ", in regulator_summary_show_subtree()
6066 30 - (level + 1) * 3, in regulator_summary_show_subtree()
6067 consumer->supply_name ? consumer->supply_name : in regulator_summary_show_subtree()
6068 consumer->dev ? dev_name(consumer->dev) : "deviceless"); in regulator_summary_show_subtree()
6070 switch (rdev->desc->type) { in regulator_summary_show_subtree()
6073 consumer->enable_count, in regulator_summary_show_subtree()
6074 consumer->uA_load / 1000, in regulator_summary_show_subtree()
6075 consumer->uA_load && !consumer->enable_count ? in regulator_summary_show_subtree()
6077 consumer->voltage[PM_SUSPEND_ON].min_uV / 1000, in regulator_summary_show_subtree()
6078 consumer->voltage[PM_SUSPEND_ON].max_uV / 1000); in regulator_summary_show_subtree()
6107 if (rdev != *lock_data->old_contended_rdev) { in regulator_summary_lock_one()
6108 ret = regulator_lock_nested(rdev, lock_data->ww_ctx); in regulator_summary_lock_one()
6110 if (ret == -EDEADLK) in regulator_summary_lock_one()
6111 *lock_data->new_contended_rdev = rdev; in regulator_summary_lock_one()
6115 *lock_data->old_contended_rdev = NULL; in regulator_summary_lock_one()
6127 if (rdev == *lock_data->new_contended_rdev) in regulator_summary_unlock_one()
6128 return -EDEADLK; in regulator_summary_unlock_one()
6168 ww_mutex_lock_slow(&new_contended_rdev->mutex, ww_ctx); in regulator_summary_lock()
6170 old_contended_rdev->ref_cnt++; in regulator_summary_lock()
6171 old_contended_rdev->mutex_owner = current; in regulator_summary_lock()
6181 } while (err == -EDEADLK); in regulator_summary_lock()
6200 if (!rdev->supply) in regulator_summary_show_roots()
6210 …seq_puts(s, " regulator use open bypass opmode voltage current min m… in regulator_summary_show()
6211 …seq_puts(s, "-------------------------------------------------------------------------------------… in regulator_summary_show()
6231 debugfs_root = debugfs_create_dir("regulator", NULL); in regulator_init()
6233 pr_debug("regulator: Failed to create debugfs directory\n"); in regulator_init()
6249 /* init early to allow our consumers to complete system booting */
6255 struct regulation_constraints *c = rdev->constraints; in regulator_late_cleanup()
6258 if (c && c->always_on) in regulator_late_cleanup()
6266 if (rdev->use_count) in regulator_late_cleanup()
6274 /* We log since this may kill the system if it goes in regulator_late_cleanup()
6321 pr_warn("regulator: Not disabling unused regulators\n"); in regulator_init_complete_work_function()
6343 * system has full constraints. in regulator_init_complete()