Lines Matching +full:regulator +full:- +full:allow +full:- +full:bypass
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
153 if (!ww_mutex_trylock(&rdev->mutex, ww_ctx)) { in regulator_lock_nested()
154 if (rdev->mutex_owner == current) in regulator_lock_nested()
155 rdev->ref_cnt++; in regulator_lock_nested()
161 ret = ww_mutex_lock(&rdev->mutex, ww_ctx); in regulator_lock_nested()
168 if (lock && ret != -EDEADLK) { in regulator_lock_nested()
169 rdev->ref_cnt++; in regulator_lock_nested()
170 rdev->mutex_owner = current; in regulator_lock_nested()
179 * regulator_lock - lock a single regulator
180 * @rdev: regulator source
183 * a single regulator and its mutex will be locked only
194 * regulator_unlock - unlock a single regulator
204 if (--rdev->ref_cnt == 0) { in regulator_unlock()
205 rdev->mutex_owner = NULL; in regulator_unlock()
206 ww_mutex_unlock(&rdev->mutex); in regulator_unlock()
209 WARN_ON_ONCE(rdev->ref_cnt < 0); in regulator_unlock()
215 * regulator_lock_two - lock two regulators
216 * @rdev1: first regulator
217 * @rdev2: second regulator
235 if (ret != -EDEADLOCK) { in regulator_lock_two()
245 ww_mutex_lock_slow(&contended->mutex, ww_ctx); in regulator_lock_two()
246 contended->ref_cnt++; in regulator_lock_two()
247 contended->mutex_owner = current; in regulator_lock_two()
251 if (ret != -EDEADLOCK) { in regulator_lock_two()
262 * regulator_unlock_two - unlock two regulators
263 * @rdev1: first regulator
264 * @rdev2: second regulator
284 for (i = 1; i < rdev->coupling_desc.n_coupled; i++) { in regulator_supply_is_couple()
285 c_rdev = rdev->coupling_desc.coupled_rdevs[i]; in regulator_supply_is_couple()
287 if (rdev->supply->rdev == c_rdev) in regulator_supply_is_couple()
300 for (i = n_coupled; i > 0; i--) { in regulator_unlock_recursive()
301 c_rdev = rdev->coupling_desc.coupled_rdevs[i - 1]; in regulator_unlock_recursive()
306 if (c_rdev->supply && !regulator_supply_is_couple(c_rdev)) { in regulator_unlock_recursive()
307 supply_rdev = c_rdev->supply->rdev; in regulator_unlock_recursive()
308 supply_n_coupled = supply_rdev->coupling_desc.n_coupled; in regulator_unlock_recursive()
326 for (i = 0; i < rdev->coupling_desc.n_coupled; i++) { in regulator_lock_recursive()
327 c_rdev = rdev->coupling_desc.coupled_rdevs[i]; in regulator_lock_recursive()
335 if (err == -EDEADLK) { in regulator_lock_recursive()
341 WARN_ON_ONCE(err != -EALREADY); in regulator_lock_recursive()
347 if (c_rdev->supply && !regulator_supply_is_couple(c_rdev)) { in regulator_lock_recursive()
348 err = regulator_lock_recursive(c_rdev->supply->rdev, in regulator_lock_recursive()
368 * regulator_unlock_dependent - unlock regulator's suppliers and coupled
370 * @rdev: regulator source
378 regulator_unlock_recursive(rdev, rdev->coupling_desc.n_coupled); in regulator_unlock_dependent()
383 * regulator_lock_dependent - lock regulator's suppliers and coupled regulators
384 * @rdev: regulator source
403 ww_mutex_lock_slow(&new_contended_rdev->mutex, ww_ctx); in regulator_lock_dependent()
405 old_contended_rdev->ref_cnt++; in regulator_lock_dependent()
406 old_contended_rdev->mutex_owner = current; in regulator_lock_dependent()
417 } while (err == -EDEADLK); in regulator_lock_dependent()
432 return -EPERM; in regulator_check_voltage()
435 if (*max_uV > rdev->constraints->max_uV) in regulator_check_voltage()
436 *max_uV = rdev->constraints->max_uV; in regulator_check_voltage()
437 if (*min_uV < rdev->constraints->min_uV) in regulator_check_voltage()
438 *min_uV = rdev->constraints->min_uV; in regulator_check_voltage()
441 rdev_err(rdev, "unsupportable voltage range: %d-%duV\n", in regulator_check_voltage()
443 return -EINVAL; in regulator_check_voltage()
456 * regulator consumers
462 struct regulator *regulator; in regulator_check_consumers() local
465 list_for_each_entry(regulator, &rdev->consumer_list, list) { in regulator_check_consumers()
466 voltage = ®ulator->voltage[state]; in regulator_check_consumers()
471 if (!voltage->min_uV && !voltage->max_uV) in regulator_check_consumers()
474 if (*max_uV > voltage->max_uV) in regulator_check_consumers()
475 *max_uV = voltage->max_uV; in regulator_check_consumers()
476 if (*min_uV < voltage->min_uV) in regulator_check_consumers()
477 *min_uV = voltage->min_uV; in regulator_check_consumers()
481 rdev_err(rdev, "Restricting voltage, %u-%uuV\n", in regulator_check_consumers()
483 return -EINVAL; in regulator_check_consumers()
497 return -EPERM; in regulator_check_current_limit()
500 if (*max_uA > rdev->constraints->max_uA) in regulator_check_current_limit()
501 *max_uA = rdev->constraints->max_uA; in regulator_check_current_limit()
502 if (*min_uA < rdev->constraints->min_uA) in regulator_check_current_limit()
503 *min_uA = rdev->constraints->min_uA; in regulator_check_current_limit()
506 rdev_err(rdev, "unsupportable current range: %d-%duA\n", in regulator_check_current_limit()
508 return -EINVAL; in regulator_check_current_limit()
526 return -EINVAL; in regulator_mode_constrain()
531 return -EPERM; in regulator_mode_constrain()
539 if (rdev->constraints->valid_modes_mask & *mode) in regulator_mode_constrain()
544 return -EINVAL; in regulator_mode_constrain()
550 if (rdev->constraints == NULL) in regulator_get_suspend_state()
555 return &rdev->constraints->state_standby; in regulator_get_suspend_state()
557 return &rdev->constraints->state_mem; in regulator_get_suspend_state()
559 return &rdev->constraints->state_disk; in regulator_get_suspend_state()
578 if (rstate->enabled != ENABLE_IN_SUSPEND && in regulator_get_suspend_state_check()
579 rstate->enabled != DISABLE_IN_SUSPEND) { in regulator_get_suspend_state_check()
580 if (rdev->desc->ops->set_suspend_voltage || in regulator_get_suspend_state_check()
581 rdev->desc->ops->set_suspend_mode) in regulator_get_suspend_state_check()
683 status = rdev->desc->ops->get_status(rdev); in status_show()
710 label = "bypass"; in status_show()
716 return -ERANGE; in status_show()
728 if (!rdev->constraints) in min_microamps_show()
731 return sprintf(buf, "%d\n", rdev->constraints->min_uA); in min_microamps_show()
740 if (!rdev->constraints) in max_microamps_show()
743 return sprintf(buf, "%d\n", rdev->constraints->max_uA); in max_microamps_show()
752 if (!rdev->constraints) in min_microvolts_show()
755 return sprintf(buf, "%d\n", rdev->constraints->min_uV); in min_microvolts_show()
764 if (!rdev->constraints) in max_microvolts_show()
767 return sprintf(buf, "%d\n", rdev->constraints->max_uV); in max_microvolts_show()
775 struct regulator *regulator; in requested_microamps_show() local
779 list_for_each_entry(regulator, &rdev->consumer_list, list) { in requested_microamps_show()
780 if (regulator->enable_count) in requested_microamps_show()
781 uA += regulator->uA_load; in requested_microamps_show()
792 return sprintf(buf, "%d\n", rdev->use_count); in num_users_show()
801 switch (rdev->desc->type) { in type_show()
816 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV); in suspend_mem_microvolts_show()
825 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV); in suspend_disk_microvolts_show()
834 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV); in suspend_standby_microvolts_show()
844 rdev->constraints->state_mem.mode); in suspend_mem_mode_show()
854 rdev->constraints->state_disk.mode); in suspend_disk_mode_show()
864 rdev->constraints->state_standby.mode); in suspend_standby_mode_show()
874 rdev->constraints->state_mem.enabled); in suspend_mem_state_show()
884 rdev->constraints->state_disk.enabled); in suspend_disk_state_show()
894 rdev->constraints->state_standby.enabled); in suspend_standby_state_show()
903 bool bypass; in bypass_show() local
906 ret = rdev->desc->ops->get_bypass(rdev, &bypass); in bypass_show()
910 else if (bypass) in bypass_show()
917 static DEVICE_ATTR_RO(bypass);
943 /* Calculate the new optimum regulator operating mode based on the new total
948 struct regulator *sibling; in drms_uA_update()
961 if (!rdev->desc->ops->get_optimum_mode && in drms_uA_update()
962 !rdev->desc->ops->set_load) in drms_uA_update()
965 if (!rdev->desc->ops->set_mode && in drms_uA_update()
966 !rdev->desc->ops->set_load) in drms_uA_update()
967 return -EINVAL; in drms_uA_update()
970 list_for_each_entry(sibling, &rdev->consumer_list, list) { in drms_uA_update()
971 if (sibling->enable_count) in drms_uA_update()
972 current_uA += sibling->uA_load; in drms_uA_update()
975 current_uA += rdev->constraints->system_load; in drms_uA_update()
977 if (rdev->desc->ops->set_load) { in drms_uA_update()
978 /* set the optimum mode for our new total regulator load */ in drms_uA_update()
979 err = rdev->desc->ops->set_load(rdev, current_uA); in drms_uA_update()
985 * Unfortunately in some cases the constraints->valid_ops has in drms_uA_update()
991 if (!rdev->constraints->valid_modes_mask) { in drms_uA_update()
1000 * Don't return an error; if regulator driver cares about in drms_uA_update()
1008 if (rdev->supply) in drms_uA_update()
1009 input_uV = regulator_get_voltage_rdev(rdev->supply->rdev); in drms_uA_update()
1011 input_uV = rdev->constraints->input_uV; in drms_uA_update()
1014 * Don't return an error; if regulator driver cares about in drms_uA_update()
1020 /* now get the optimum mode for our new total regulator load */ in drms_uA_update()
1021 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV, in drms_uA_update()
1027 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV: %pe\n", in drms_uA_update()
1032 err = rdev->desc->ops->set_mode(rdev, mode); in drms_uA_update()
1046 if (rstate->enabled == ENABLE_IN_SUSPEND && in __suspend_set_state()
1047 rdev->desc->ops->set_suspend_enable) in __suspend_set_state()
1048 ret = rdev->desc->ops->set_suspend_enable(rdev); in __suspend_set_state()
1049 else if (rstate->enabled == DISABLE_IN_SUSPEND && in __suspend_set_state()
1050 rdev->desc->ops->set_suspend_disable) in __suspend_set_state()
1051 ret = rdev->desc->ops->set_suspend_disable(rdev); in __suspend_set_state()
1060 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) { in __suspend_set_state()
1061 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV); in __suspend_set_state()
1068 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) { in __suspend_set_state()
1069 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode); in __suspend_set_state()
1084 rdev->constraints->initial_state); in suspend_set_initial_state()
1094 struct regulation_constraints *constraints = rdev->constraints; in print_constraints_debug()
1096 size_t len = sizeof(buf) - 1; in print_constraints_debug()
1100 if (constraints->min_uV && constraints->max_uV) { in print_constraints_debug()
1101 if (constraints->min_uV == constraints->max_uV) in print_constraints_debug()
1102 count += scnprintf(buf + count, len - count, "%d mV ", in print_constraints_debug()
1103 constraints->min_uV / 1000); in print_constraints_debug()
1105 count += scnprintf(buf + count, len - count, in print_constraints_debug()
1106 "%d <--> %d mV ", in print_constraints_debug()
1107 constraints->min_uV / 1000, in print_constraints_debug()
1108 constraints->max_uV / 1000); in print_constraints_debug()
1111 if (!constraints->min_uV || in print_constraints_debug()
1112 constraints->min_uV != constraints->max_uV) { in print_constraints_debug()
1115 count += scnprintf(buf + count, len - count, in print_constraints_debug()
1119 if (constraints->uV_offset) in print_constraints_debug()
1120 count += scnprintf(buf + count, len - count, "%dmV offset ", in print_constraints_debug()
1121 constraints->uV_offset / 1000); in print_constraints_debug()
1123 if (constraints->min_uA && constraints->max_uA) { in print_constraints_debug()
1124 if (constraints->min_uA == constraints->max_uA) in print_constraints_debug()
1125 count += scnprintf(buf + count, len - count, "%d mA ", in print_constraints_debug()
1126 constraints->min_uA / 1000); in print_constraints_debug()
1128 count += scnprintf(buf + count, len - count, in print_constraints_debug()
1129 "%d <--> %d mA ", in print_constraints_debug()
1130 constraints->min_uA / 1000, in print_constraints_debug()
1131 constraints->max_uA / 1000); in print_constraints_debug()
1134 if (!constraints->min_uA || in print_constraints_debug()
1135 constraints->min_uA != constraints->max_uA) { in print_constraints_debug()
1138 count += scnprintf(buf + count, len - count, in print_constraints_debug()
1142 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST) in print_constraints_debug()
1143 count += scnprintf(buf + count, len - count, "fast "); in print_constraints_debug()
1144 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL) in print_constraints_debug()
1145 count += scnprintf(buf + count, len - count, "normal "); in print_constraints_debug()
1146 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE) in print_constraints_debug()
1147 count += scnprintf(buf + count, len - count, "idle "); in print_constraints_debug()
1148 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY) in print_constraints_debug()
1149 count += scnprintf(buf + count, len - count, "standby "); in print_constraints_debug()
1154 --count; in print_constraints_debug()
1156 count += scnprintf(buf + count, len - count, ", %s", in print_constraints_debug()
1167 struct regulation_constraints *constraints = rdev->constraints; in print_constraints()
1171 if ((constraints->min_uV != constraints->max_uV) && in print_constraints()
1180 const struct regulator_ops *ops = rdev->desc->ops; in machine_constraints_voltage()
1184 if (rdev->constraints->apply_uV && in machine_constraints_voltage()
1185 rdev->constraints->min_uV && rdev->constraints->max_uV) { in machine_constraints_voltage()
1189 if (current_uV == -ENOTRECOVERABLE) { in machine_constraints_voltage()
1190 /* This regulator can't be read and must be initialized */ in machine_constraints_voltage()
1191 rdev_info(rdev, "Setting %d-%duV\n", in machine_constraints_voltage()
1192 rdev->constraints->min_uV, in machine_constraints_voltage()
1193 rdev->constraints->max_uV); in machine_constraints_voltage()
1195 rdev->constraints->min_uV, in machine_constraints_voltage()
1196 rdev->constraints->max_uV); in machine_constraints_voltage()
1201 if (current_uV != -EPROBE_DEFER) in machine_constraints_voltage()
1216 if (current_uV < rdev->constraints->min_uV) { in machine_constraints_voltage()
1217 target_min = rdev->constraints->min_uV; in machine_constraints_voltage()
1218 target_max = rdev->constraints->min_uV; in machine_constraints_voltage()
1221 if (current_uV > rdev->constraints->max_uV) { in machine_constraints_voltage()
1222 target_min = rdev->constraints->max_uV; in machine_constraints_voltage()
1223 target_max = rdev->constraints->max_uV; in machine_constraints_voltage()
1227 rdev_info(rdev, "Bringing %duV into %d-%duV\n", in machine_constraints_voltage()
1233 "failed to apply %d-%duV constraint: %pe\n", in machine_constraints_voltage()
1240 /* constrain machine-level voltage specs to fit in machine_constraints_voltage()
1241 * the actual range supported by this regulator. in machine_constraints_voltage()
1243 if (ops->list_voltage && rdev->desc->n_voltages) { in machine_constraints_voltage()
1244 int count = rdev->desc->n_voltages; in machine_constraints_voltage()
1248 int cmin = constraints->min_uV; in machine_constraints_voltage()
1249 int cmax = constraints->max_uV; in machine_constraints_voltage()
1251 /* it's safe to autoconfigure fixed-voltage supplies in machine_constraints_voltage()
1257 constraints->min_uV = cmin; in machine_constraints_voltage()
1258 constraints->max_uV = cmax; in machine_constraints_voltage()
1265 /* else require explicit machine-level constraints */ in machine_constraints_voltage()
1268 return -EINVAL; in machine_constraints_voltage()
1272 if (rdev->desc->continuous_voltage_range) in machine_constraints_voltage()
1279 value = ops->list_voltage(rdev, i); in machine_constraints_voltage()
1293 "unsupportable voltage constraints %u-%uuV\n", in machine_constraints_voltage()
1295 return -EINVAL; in machine_constraints_voltage()
1298 /* use regulator's subset of machine constraints */ in machine_constraints_voltage()
1299 if (constraints->min_uV < min_uV) { in machine_constraints_voltage()
1300 rdev_dbg(rdev, "override min_uV, %d -> %d\n", in machine_constraints_voltage()
1301 constraints->min_uV, min_uV); in machine_constraints_voltage()
1302 constraints->min_uV = min_uV; in machine_constraints_voltage()
1304 if (constraints->max_uV > max_uV) { in machine_constraints_voltage()
1305 rdev_dbg(rdev, "override max_uV, %d -> %d\n", in machine_constraints_voltage()
1306 constraints->max_uV, max_uV); in machine_constraints_voltage()
1307 constraints->max_uV = max_uV; in machine_constraints_voltage()
1317 const struct regulator_ops *ops = rdev->desc->ops; in machine_constraints_current()
1320 if (!constraints->min_uA && !constraints->max_uA) in machine_constraints_current()
1323 if (constraints->min_uA > constraints->max_uA) { in machine_constraints_current()
1325 return -EINVAL; in machine_constraints_current()
1328 if (!ops->set_current_limit || !ops->get_current_limit) { in machine_constraints_current()
1333 /* Set regulator current in constraints range */ in machine_constraints_current()
1334 ret = ops->set_current_limit(rdev, constraints->min_uA, in machine_constraints_current()
1335 constraints->max_uA); in machine_constraints_current()
1372 return -EOPNOTSUPP; in handle_notify_limits()
1374 if (limits->prot) in handle_notify_limits()
1375 ret = notif_set_limit(rdev, set, limits->prot, in handle_notify_limits()
1380 if (limits->err) in handle_notify_limits()
1381 ret = notif_set_limit(rdev, set, limits->err, in handle_notify_limits()
1386 if (limits->warn) in handle_notify_limits()
1387 ret = notif_set_limit(rdev, set, limits->warn, in handle_notify_limits()
1393 * set_machine_constraints - sets regulator constraints
1394 * @rdev: regulator source
1397 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
1399 * regulator operations to proceed i.e. set_voltage, set_current_limit,
1407 const struct regulator_ops *ops = rdev->desc->ops; in set_machine_constraints()
1409 ret = machine_constraints_voltage(rdev, rdev->constraints); in set_machine_constraints()
1413 ret = machine_constraints_current(rdev, rdev->constraints); in set_machine_constraints()
1417 if (rdev->constraints->ilim_uA && ops->set_input_current_limit) { in set_machine_constraints()
1418 ret = ops->set_input_current_limit(rdev, in set_machine_constraints()
1419 rdev->constraints->ilim_uA); in set_machine_constraints()
1427 if (rdev->constraints->initial_state) { in set_machine_constraints()
1435 if (rdev->constraints->initial_mode) { in set_machine_constraints()
1436 if (!ops->set_mode) { in set_machine_constraints()
1438 return -EINVAL; in set_machine_constraints()
1441 ret = ops->set_mode(rdev, rdev->constraints->initial_mode); in set_machine_constraints()
1446 } else if (rdev->constraints->system_load) { in set_machine_constraints()
1454 if ((rdev->constraints->ramp_delay || rdev->constraints->ramp_disable) in set_machine_constraints()
1455 && ops->set_ramp_delay) { in set_machine_constraints()
1456 ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay); in set_machine_constraints()
1463 if (rdev->constraints->pull_down && ops->set_pull_down) { in set_machine_constraints()
1464 ret = ops->set_pull_down(rdev); in set_machine_constraints()
1471 if (rdev->constraints->soft_start && ops->set_soft_start) { in set_machine_constraints()
1472 ret = ops->set_soft_start(rdev); in set_machine_constraints()
1483 * PMIC on board to another type - and the another PMIC's driver does in set_machine_constraints()
1485 * the DT limits are respected - especially if the new PMIC HW also in set_machine_constraints()
1490 * over-curret protection and detection and get rid of this special in set_machine_constraints()
1493 if (rdev->constraints->over_current_protection in set_machine_constraints()
1494 && ops->set_over_current_protection) { in set_machine_constraints()
1495 int lim = rdev->constraints->over_curr_limits.prot; in set_machine_constraints()
1497 ret = ops->set_over_current_protection(rdev, lim, in set_machine_constraints()
1507 if (rdev->constraints->over_current_detection) in set_machine_constraints()
1509 ops->set_over_current_protection, in set_machine_constraints()
1510 &rdev->constraints->over_curr_limits); in set_machine_constraints()
1512 if (ret != -EOPNOTSUPP) { in set_machine_constraints()
1518 "IC does not support requested over-current limits\n"); in set_machine_constraints()
1521 if (rdev->constraints->over_voltage_detection) in set_machine_constraints()
1523 ops->set_over_voltage_protection, in set_machine_constraints()
1524 &rdev->constraints->over_voltage_limits); in set_machine_constraints()
1526 if (ret != -EOPNOTSUPP) { in set_machine_constraints()
1535 if (rdev->constraints->under_voltage_detection) in set_machine_constraints()
1537 ops->set_under_voltage_protection, in set_machine_constraints()
1538 &rdev->constraints->under_voltage_limits); in set_machine_constraints()
1540 if (ret != -EOPNOTSUPP) { in set_machine_constraints()
1549 if (rdev->constraints->over_temp_detection) in set_machine_constraints()
1551 ops->set_thermal_protection, in set_machine_constraints()
1552 &rdev->constraints->temp_limits); in set_machine_constraints()
1554 if (ret != -EOPNOTSUPP) { in set_machine_constraints()
1563 if (rdev->constraints->active_discharge && ops->set_active_discharge) { in set_machine_constraints()
1564 bool ad_state = (rdev->constraints->active_discharge == in set_machine_constraints()
1567 ret = ops->set_active_discharge(rdev, ad_state); in set_machine_constraints()
1575 * If there is no mechanism for controlling the regulator then in set_machine_constraints()
1578 * a supply to control the output on a regulator that has no in set_machine_constraints()
1581 if (!rdev->ena_pin && !ops->enable) { in set_machine_constraints()
1582 if (rdev->supply_name && !rdev->supply) in set_machine_constraints()
1583 return -EPROBE_DEFER; in set_machine_constraints()
1585 if (rdev->supply) in set_machine_constraints()
1586 rdev->constraints->always_on = in set_machine_constraints()
1587 rdev->supply->rdev->constraints->always_on; in set_machine_constraints()
1589 rdev->constraints->always_on = true; in set_machine_constraints()
1592 /* If the constraints say the regulator should be on at this point in set_machine_constraints()
1595 if (rdev->constraints->always_on || rdev->constraints->boot_on) { in set_machine_constraints()
1596 /* If we want to enable this regulator, make sure that we know in set_machine_constraints()
1597 * the supplying regulator. in set_machine_constraints()
1599 if (rdev->supply_name && !rdev->supply) in set_machine_constraints()
1600 return -EPROBE_DEFER; in set_machine_constraints()
1602 /* If supplying regulator has already been enabled, in set_machine_constraints()
1604 * when rdev is only boot-on. in set_machine_constraints()
1606 if (rdev->supply && in set_machine_constraints()
1607 (rdev->constraints->always_on || in set_machine_constraints()
1608 !regulator_is_enabled(rdev->supply))) { in set_machine_constraints()
1609 ret = regulator_enable(rdev->supply); in set_machine_constraints()
1611 _regulator_put(rdev->supply); in set_machine_constraints()
1612 rdev->supply = NULL; in set_machine_constraints()
1618 if (ret < 0 && ret != -EINVAL) { in set_machine_constraints()
1623 if (rdev->constraints->always_on) in set_machine_constraints()
1624 rdev->use_count++; in set_machine_constraints()
1625 } else if (rdev->desc->off_on_delay) { in set_machine_constraints()
1626 rdev->last_off = ktime_get(); in set_machine_constraints()
1634 * set_supply - set regulator supply regulator
1635 * @rdev: regulator (locked)
1636 * @supply_rdev: supply regulator (locked))
1638 * Called by platform initialisation code to set the supply regulator for this
1639 * regulator. This ensures that a regulators supply will also be enabled by the
1651 if (!try_module_get(supply_rdev->owner)) in set_supply()
1652 return -ENODEV; in set_supply()
1654 rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY"); in set_supply()
1655 if (rdev->supply == NULL) { in set_supply()
1656 module_put(supply_rdev->owner); in set_supply()
1657 err = -ENOMEM; in set_supply()
1660 supply_rdev->open_count++; in set_supply()
1666 * set_consumer_device_supply - Bind a regulator to a symbolic supply
1667 * @rdev: regulator source
1671 * Allows platform initialisation code to map physical regulator
1674 * need to provide board-specific regulator names as platform data.
1686 return -EINVAL; in set_consumer_device_supply()
1695 return -ENOMEM; in set_consumer_device_supply()
1697 new_node->regulator = rdev; in set_consumer_device_supply()
1698 new_node->supply = supply; in set_consumer_device_supply()
1701 new_node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL); in set_consumer_device_supply()
1702 if (new_node->dev_name == NULL) { in set_consumer_device_supply()
1704 return -ENOMEM; in set_consumer_device_supply()
1710 if (node->dev_name && consumer_dev_name) { in set_consumer_device_supply()
1711 if (strcmp(node->dev_name, consumer_dev_name) != 0) in set_consumer_device_supply()
1713 } else if (node->dev_name || consumer_dev_name) { in set_consumer_device_supply()
1717 if (strcmp(node->supply, supply) != 0) in set_consumer_device_supply()
1722 dev_name(&node->regulator->dev), in set_consumer_device_supply()
1723 node->regulator->desc->name, in set_consumer_device_supply()
1725 dev_name(&rdev->dev), rdev_get_name(rdev)); in set_consumer_device_supply()
1729 list_add(&new_node->list, ®ulator_map_list); in set_consumer_device_supply()
1736 kfree(new_node->dev_name); in set_consumer_device_supply()
1738 return -EBUSY; in set_consumer_device_supply()
1746 if (rdev == node->regulator) { in unset_regulator_supplies()
1747 list_del(&node->list); in unset_regulator_supplies()
1748 kfree(node->dev_name); in unset_regulator_supplies()
1759 const struct regulator *regulator = file->private_data; in constraint_flags_read_file() local
1760 const struct regulation_constraints *c = regulator->rdev->constraints; in constraint_flags_read_file()
1769 return -ENOMEM; in constraint_flags_read_file()
1779 c->always_on, in constraint_flags_read_file()
1780 c->boot_on, in constraint_flags_read_file()
1781 c->apply_uV, in constraint_flags_read_file()
1782 c->ramp_disable, in constraint_flags_read_file()
1783 c->soft_start, in constraint_flags_read_file()
1784 c->pull_down, in constraint_flags_read_file()
1785 c->over_current_protection); in constraint_flags_read_file()
1805 static struct regulator *create_regulator(struct regulator_dev *rdev, in create_regulator()
1809 struct regulator *regulator; in create_regulator() local
1812 lockdep_assert_held_once(&rdev->mutex.base); in create_regulator()
1818 size = snprintf(buf, REG_STR_SIZE, "%s-%s", in create_regulator()
1819 dev->kobj.name, supply_name); in create_regulator()
1832 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL); in create_regulator()
1833 if (regulator == NULL) { in create_regulator()
1838 regulator->rdev = rdev; in create_regulator()
1839 regulator->supply_name = supply_name; in create_regulator()
1841 list_add(®ulator->list, &rdev->consumer_list); in create_regulator()
1844 regulator->dev = dev; in create_regulator()
1847 err = sysfs_create_link_nowarn(&rdev->dev.kobj, &dev->kobj, in create_regulator()
1851 dev->kobj.name, ERR_PTR(err)); in create_regulator()
1852 /* non-fatal */ in create_regulator()
1856 if (err != -EEXIST) { in create_regulator()
1857 regulator->debugfs = debugfs_create_dir(supply_name, rdev->debugfs); in create_regulator()
1858 if (IS_ERR(regulator->debugfs)) { in create_regulator()
1860 regulator->debugfs = NULL; in create_regulator()
1864 if (regulator->debugfs) { in create_regulator()
1865 debugfs_create_u32("uA_load", 0444, regulator->debugfs, in create_regulator()
1866 ®ulator->uA_load); in create_regulator()
1867 debugfs_create_u32("min_uV", 0444, regulator->debugfs, in create_regulator()
1868 ®ulator->voltage[PM_SUSPEND_ON].min_uV); in create_regulator()
1869 debugfs_create_u32("max_uV", 0444, regulator->debugfs, in create_regulator()
1870 ®ulator->voltage[PM_SUSPEND_ON].max_uV); in create_regulator()
1871 debugfs_create_file("constraint_flags", 0444, regulator->debugfs, in create_regulator()
1872 regulator, &constraint_flags_fops); in create_regulator()
1876 * Check now if the regulator is an always on regulator - if in create_regulator()
1882 regulator->always_on = true; in create_regulator()
1884 return regulator; in create_regulator()
1889 if (rdev->constraints && rdev->constraints->enable_time) in _regulator_get_enable_time()
1890 return rdev->constraints->enable_time; in _regulator_get_enable_time()
1891 if (rdev->desc->ops->enable_time) in _regulator_get_enable_time()
1892 return rdev->desc->ops->enable_time(rdev); in _regulator_get_enable_time()
1893 return rdev->desc->enable_time; in _regulator_get_enable_time()
1902 if (map->src_dev == dev && strcmp(map->src_supply, supply) == 0) in regulator_find_supply_alias()
1915 *supply, map->alias_supply, in regulator_supply_alias()
1916 dev_name(map->alias_dev)); in regulator_supply_alias()
1917 *dev = map->alias_dev; in regulator_supply_alias()
1918 *supply = map->alias_supply; in regulator_supply_alias()
1939 * regulator_dev_lookup - lookup a regulator device.
1940 * @dev: device for regulator "consumer".
1941 * @supply: Supply name or regulator ID.
1949 * -%ENODEV if lookup fails permanently, -%EPROBE_DEFER if lookup could succeed
1962 if (dev && dev->of_node) { in regulator_dev_lookup()
1966 if (PTR_ERR(r) == -EPROBE_DEFER) in regulator_dev_lookup()
1969 if (PTR_ERR(r) == -ENODEV) in regulator_dev_lookup()
1973 /* if not found, try doing it non-dt way */ in regulator_dev_lookup()
1980 if (map->dev_name && in regulator_dev_lookup()
1981 (!devname || strcmp(map->dev_name, devname))) in regulator_dev_lookup()
1984 if (strcmp(map->supply, supply) == 0 && in regulator_dev_lookup()
1985 get_device(&map->regulator->dev)) { in regulator_dev_lookup()
1986 r = map->regulator; in regulator_dev_lookup()
1999 return ERR_PTR(-ENODEV); in regulator_dev_lookup()
2005 struct device *dev = rdev->dev.parent; in regulator_resolve_supply()
2010 if (!rdev->supply_name) in regulator_resolve_supply()
2013 /* Supply already resolved? (fast-path without locking contention) */ in regulator_resolve_supply()
2014 if (rdev->supply) in regulator_resolve_supply()
2017 r = regulator_dev_lookup(dev, rdev->supply_name); in regulator_resolve_supply()
2022 if (ret == -EPROBE_DEFER) in regulator_resolve_supply()
2027 get_device(&r->dev); in regulator_resolve_supply()
2029 dev_err(dev, "Failed to resolve %s-supply for %s\n", in regulator_resolve_supply()
2030 rdev->supply_name, rdev->desc->name); in regulator_resolve_supply()
2031 ret = -EPROBE_DEFER; in regulator_resolve_supply()
2038 rdev->desc->name, rdev->supply_name); in regulator_resolve_supply()
2040 ret = -EINVAL; in regulator_resolve_supply()
2044 get_device(&r->dev); in regulator_resolve_supply()
2049 * regulator's parent device, then ensure the parent device in regulator_resolve_supply()
2053 if (r->dev.parent && r->dev.parent != rdev->dev.parent) { in regulator_resolve_supply()
2054 if (!device_is_bound(r->dev.parent)) { in regulator_resolve_supply()
2055 put_device(&r->dev); in regulator_resolve_supply()
2056 ret = -EPROBE_DEFER; in regulator_resolve_supply()
2064 put_device(&r->dev); in regulator_resolve_supply()
2069 * Recheck rdev->supply with rdev->mutex lock held to avoid a race in regulator_resolve_supply()
2070 * between rdev->supply null check and setting rdev->supply in in regulator_resolve_supply()
2076 if (rdev->supply) { in regulator_resolve_supply()
2078 put_device(&r->dev); in regulator_resolve_supply()
2085 put_device(&r->dev); in regulator_resolve_supply()
2092 * In set_machine_constraints() we may have turned this regulator on in regulator_resolve_supply()
2096 if (rdev->use_count) { in regulator_resolve_supply()
2097 ret = regulator_enable(rdev->supply); in regulator_resolve_supply()
2099 _regulator_put(rdev->supply); in regulator_resolve_supply()
2100 rdev->supply = NULL; in regulator_resolve_supply()
2109 /* common pre-checks for regulator requests */
2115 return -EINVAL; in _regulator_get_common_check()
2119 dev_err(dev, "regulator request with no identifier\n"); in _regulator_get_common_check()
2120 return -EINVAL; in _regulator_get_common_check()
2127 * _regulator_get_common - Common code for regulator requests
2128 * @rdev: regulator device pointer as returned by *regulator_dev_lookup()
2131 * @id: Supply name or regulator ID
2134 * Returns: pointer to struct regulator corresponding to @rdev, or ERR_PTR()
2139 struct regulator *_regulator_get_common(struct regulator_dev *rdev, struct device *dev, in _regulator_get_common()
2142 struct regulator *regulator; in _regulator_get_common() local
2151 * than -ENODEV our job here is done, we simply return it. in _regulator_get_common()
2153 if (ret != -ENODEV) in _regulator_get_common()
2159 return ERR_PTR(-ENODEV); in _regulator_get_common()
2165 * Assume that a regulator is physically present and in _regulator_get_common()
2169 dev_warn(dev, "supply %s not found, using dummy regulator\n", id); in _regulator_get_common()
2171 get_device(&rdev->dev); in _regulator_get_common()
2180 return ERR_PTR(-ENODEV); in _regulator_get_common()
2184 if (rdev->exclusive) { in _regulator_get_common()
2185 regulator = ERR_PTR(-EPERM); in _regulator_get_common()
2186 put_device(&rdev->dev); in _regulator_get_common()
2187 return regulator; in _regulator_get_common()
2190 if (get_type == EXCLUSIVE_GET && rdev->open_count) { in _regulator_get_common()
2191 regulator = ERR_PTR(-EBUSY); in _regulator_get_common()
2192 put_device(&rdev->dev); in _regulator_get_common()
2193 return regulator; in _regulator_get_common()
2197 ret = (rdev->coupling_desc.n_resolved != rdev->coupling_desc.n_coupled); in _regulator_get_common()
2201 regulator = ERR_PTR(-EPROBE_DEFER); in _regulator_get_common()
2202 put_device(&rdev->dev); in _regulator_get_common()
2203 return regulator; in _regulator_get_common()
2208 regulator = ERR_PTR(ret); in _regulator_get_common()
2209 put_device(&rdev->dev); in _regulator_get_common()
2210 return regulator; in _regulator_get_common()
2213 if (!try_module_get(rdev->owner)) { in _regulator_get_common()
2214 regulator = ERR_PTR(-EPROBE_DEFER); in _regulator_get_common()
2215 put_device(&rdev->dev); in _regulator_get_common()
2216 return regulator; in _regulator_get_common()
2220 regulator = create_regulator(rdev, dev, id); in _regulator_get_common()
2222 if (regulator == NULL) { in _regulator_get_common()
2223 regulator = ERR_PTR(-ENOMEM); in _regulator_get_common()
2224 module_put(rdev->owner); in _regulator_get_common()
2225 put_device(&rdev->dev); in _regulator_get_common()
2226 return regulator; in _regulator_get_common()
2229 rdev->open_count++; in _regulator_get_common()
2231 rdev->exclusive = 1; in _regulator_get_common()
2235 rdev->use_count = 1; in _regulator_get_common()
2236 regulator->enable_count = 1; in _regulator_get_common()
2238 /* Propagate the regulator state to its supply */ in _regulator_get_common()
2239 if (rdev->supply) { in _regulator_get_common()
2240 ret = regulator_enable(rdev->supply); in _regulator_get_common()
2242 destroy_regulator(regulator); in _regulator_get_common()
2243 module_put(rdev->owner); in _regulator_get_common()
2244 put_device(&rdev->dev); in _regulator_get_common()
2249 rdev->use_count = 0; in _regulator_get_common()
2250 regulator->enable_count = 0; in _regulator_get_common()
2254 link = device_link_add(dev, &rdev->dev, DL_FLAG_STATELESS); in _regulator_get_common()
2256 regulator->device_link = true; in _regulator_get_common()
2258 return regulator; in _regulator_get_common()
2261 /* Internal regulator request function */
2262 struct regulator *_regulator_get(struct device *dev, const char *id, in _regulator_get()
2277 * regulator_get - lookup and obtain a reference to a regulator.
2278 * @dev: device for regulator "consumer"
2279 * @id: Supply name or regulator ID.
2286 * Return: Pointer to a &struct regulator corresponding to the regulator
2289 struct regulator *regulator_get(struct device *dev, const char *id) in regulator_get()
2296 * regulator_get_exclusive - obtain exclusive access to a regulator.
2297 * @dev: device for regulator "consumer"
2298 * @id: Supply name or regulator ID.
2300 * Other consumers will be unable to obtain this regulator while this
2301 * reference is held and the use count for the regulator will be
2302 * initialised to reflect the current state of the regulator.
2305 * use of the regulator such as those which need to force the
2306 * regulator off for correct operation of the hardware they are
2314 * Return: Pointer to a &struct regulator corresponding to the regulator
2317 struct regulator *regulator_get_exclusive(struct device *dev, const char *id) in regulator_get_exclusive()
2324 * regulator_get_optional - obtain optional access to a regulator.
2325 * @dev: device for regulator "consumer"
2326 * @id: Supply name or regulator ID.
2330 * It can allow the regulator core to provide stub supplies for other
2340 * Return: Pointer to a &struct regulator corresponding to the regulator
2343 struct regulator *regulator_get_optional(struct device *dev, const char *id) in regulator_get_optional()
2349 static void destroy_regulator(struct regulator *regulator) in destroy_regulator() argument
2351 struct regulator_dev *rdev = regulator->rdev; in destroy_regulator()
2353 debugfs_remove_recursive(regulator->debugfs); in destroy_regulator()
2355 if (regulator->dev) { in destroy_regulator()
2356 if (regulator->device_link) in destroy_regulator()
2357 device_link_remove(regulator->dev, &rdev->dev); in destroy_regulator()
2360 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name); in destroy_regulator()
2364 list_del(®ulator->list); in destroy_regulator()
2366 rdev->open_count--; in destroy_regulator()
2367 rdev->exclusive = 0; in destroy_regulator()
2370 kfree_const(regulator->supply_name); in destroy_regulator()
2371 kfree(regulator); in destroy_regulator()
2375 static void _regulator_put(struct regulator *regulator) in _regulator_put() argument
2379 if (IS_ERR_OR_NULL(regulator)) in _regulator_put()
2385 WARN_ON(regulator->enable_count); in _regulator_put()
2387 rdev = regulator->rdev; in _regulator_put()
2389 destroy_regulator(regulator); in _regulator_put()
2391 module_put(rdev->owner); in _regulator_put()
2392 put_device(&rdev->dev); in _regulator_put()
2396 * regulator_put - "free" the regulator source
2397 * @regulator: regulator source
2400 * regulator source are balanced by regulator_disable calls prior to calling
2403 void regulator_put(struct regulator *regulator) in regulator_put() argument
2406 _regulator_put(regulator); in regulator_put()
2412 * regulator_register_supply_alias - Provide device alias for supply lookup
2414 * @dev: device that will be given as the regulator "consumer"
2415 * @id: Supply name or regulator ID
2417 * @alias_id: Supply name or regulator ID that should be used to lookup the
2433 return -EEXIST; in regulator_register_supply_alias()
2437 return -ENOMEM; in regulator_register_supply_alias()
2439 map->src_dev = dev; in regulator_register_supply_alias()
2440 map->src_supply = id; in regulator_register_supply_alias()
2441 map->alias_dev = alias_dev; in regulator_register_supply_alias()
2442 map->alias_supply = alias_id; in regulator_register_supply_alias()
2444 list_add(&map->list, ®ulator_supply_alias_list); in regulator_register_supply_alias()
2446 pr_info("Adding alias for supply %s,%s -> %s,%s\n", in regulator_register_supply_alias()
2454 * regulator_unregister_supply_alias - Remove device alias
2456 * @dev: device that will be given as the regulator "consumer"
2457 * @id: Supply name or regulator ID
2467 list_del(&map->list); in regulator_unregister_supply_alias()
2474 * regulator_bulk_register_supply_alias - register multiple aliases
2476 * @dev: device that will be given as the regulator "consumer"
2477 * @id: List of supply names or regulator IDs
2479 * @alias_id: List of supply names or regulator IDs that should be used to
2510 "Failed to create supply alias %s,%s -> %s,%s\n", in regulator_bulk_register_supply_alias()
2513 while (--i >= 0) in regulator_bulk_register_supply_alias()
2521 * regulator_bulk_unregister_supply_alias - unregister multiple aliases
2523 * @dev: device that will be given as the regulator "consumer"
2524 * @id: List of supply names or regulator IDs
2549 gpiod = config->ena_gpiod; in regulator_ena_gpio_request()
2555 if (pin->gpiod == gpiod) { in regulator_ena_gpio_request()
2563 return -ENOMEM; in regulator_ena_gpio_request()
2569 pin->gpiod = gpiod; in regulator_ena_gpio_request()
2570 list_add(&pin->list, ®ulator_ena_gpio_list); in regulator_ena_gpio_request()
2573 pin->request_count++; in regulator_ena_gpio_request()
2574 rdev->ena_pin = pin; in regulator_ena_gpio_request()
2586 if (!rdev->ena_pin) in regulator_ena_gpio_free()
2591 if (pin != rdev->ena_pin) in regulator_ena_gpio_free()
2594 if (--pin->request_count) in regulator_ena_gpio_free()
2597 gpiod_put(pin->gpiod); in regulator_ena_gpio_free()
2598 list_del(&pin->list); in regulator_ena_gpio_free()
2603 rdev->ena_pin = NULL; in regulator_ena_gpio_free()
2607 * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control
2618 struct regulator_enable_gpio *pin = rdev->ena_pin; in regulator_ena_gpio_ctrl()
2621 return -EINVAL; in regulator_ena_gpio_ctrl()
2625 if (pin->enable_count == 0) in regulator_ena_gpio_ctrl()
2626 gpiod_set_value_cansleep(pin->gpiod, 1); in regulator_ena_gpio_ctrl()
2628 pin->enable_count++; in regulator_ena_gpio_ctrl()
2630 if (pin->enable_count > 1) { in regulator_ena_gpio_ctrl()
2631 pin->enable_count--; in regulator_ena_gpio_ctrl()
2636 if (pin->enable_count <= 1) { in regulator_ena_gpio_ctrl()
2637 gpiod_set_value_cansleep(pin->gpiod, 0); in regulator_ena_gpio_ctrl()
2638 pin->enable_count = 0; in regulator_ena_gpio_ctrl()
2646 * _regulator_delay_helper - a delay helper function
2651 * Documentation/timers/timers-howto.rst
2653 * The assumption here is that these regulator operations will never used in
2663 * For small enough values, handle super-millisecond in _regulator_delay_helper()
2675 * bother setting up high-resolution timers and just busy- in _regulator_delay_helper()
2685 * _regulator_check_status_enabled - check if regulator status can be
2686 * interpreted as "regulator is enabled"
2687 * @rdev: the regulator device to check
2690 * * 1 - if status shows regulator is in enabled state
2691 * * 0 - if not enabled state
2692 * * Error Value - as received from ops->get_status()
2696 int ret = rdev->desc->ops->get_status(rdev); in _regulator_check_status_enabled()
2728 if (rdev->desc->off_on_delay) { in _regulator_do_enable()
2730 * this regulator was disabled. in _regulator_do_enable()
2732 ktime_t end = ktime_add_us(rdev->last_off, rdev->desc->off_on_delay); in _regulator_do_enable()
2739 if (rdev->ena_pin) { in _regulator_do_enable()
2740 if (!rdev->ena_gpio_state) { in _regulator_do_enable()
2744 rdev->ena_gpio_state = 1; in _regulator_do_enable()
2746 } else if (rdev->desc->ops->enable) { in _regulator_do_enable()
2747 ret = rdev->desc->ops->enable(rdev); in _regulator_do_enable()
2751 return -EINVAL; in _regulator_do_enable()
2754 /* Allow the regulator to ramp; it would be useful to extend in _regulator_do_enable()
2761 * above, delaying poll_enabled_time uS to check if the regulator in _regulator_do_enable()
2763 * If the regulator isn't enabled after our delay helper has expired, in _regulator_do_enable()
2764 * return -ETIMEDOUT. in _regulator_do_enable()
2766 if (rdev->desc->poll_enabled_time) { in _regulator_do_enable()
2770 _regulator_delay_helper(rdev->desc->poll_enabled_time); in _regulator_do_enable()
2772 if (rdev->desc->ops->get_status) { in _regulator_do_enable()
2778 } else if (rdev->desc->ops->is_enabled(rdev)) in _regulator_do_enable()
2781 time_remaining -= rdev->desc->poll_enabled_time; in _regulator_do_enable()
2786 return -ETIMEDOUT; in _regulator_do_enable()
2798 * _regulator_handle_consumer_enable - handle that a consumer enabled
2799 * @regulator: regulator source
2801 * Some things on a regulator consumer (like the contribution towards total
2802 * load on the regulator) only have an effect when the consumer wants the
2803 * regulator enabled. Explained in example with two consumers of the same
2804 * regulator:
2812 * responsible for keeping track of the refcount for a given regulator consumer
2817 static int _regulator_handle_consumer_enable(struct regulator *regulator) in _regulator_handle_consumer_enable() argument
2820 struct regulator_dev *rdev = regulator->rdev; in _regulator_handle_consumer_enable()
2822 lockdep_assert_held_once(&rdev->mutex.base); in _regulator_handle_consumer_enable()
2824 regulator->enable_count++; in _regulator_handle_consumer_enable()
2825 if (regulator->uA_load && regulator->enable_count == 1) { in _regulator_handle_consumer_enable()
2828 regulator->enable_count--; in _regulator_handle_consumer_enable()
2836 * _regulator_handle_consumer_disable - handle that a consumer disabled
2837 * @regulator: regulator source
2843 static int _regulator_handle_consumer_disable(struct regulator *regulator) in _regulator_handle_consumer_disable() argument
2845 struct regulator_dev *rdev = regulator->rdev; in _regulator_handle_consumer_disable()
2847 lockdep_assert_held_once(&rdev->mutex.base); in _regulator_handle_consumer_disable()
2849 if (!regulator->enable_count) { in _regulator_handle_consumer_disable()
2850 rdev_err(rdev, "Underflow of regulator enable count\n"); in _regulator_handle_consumer_disable()
2851 return -EINVAL; in _regulator_handle_consumer_disable()
2854 regulator->enable_count--; in _regulator_handle_consumer_disable()
2855 if (regulator->uA_load && regulator->enable_count == 0) in _regulator_handle_consumer_disable()
2862 static int _regulator_enable(struct regulator *regulator) in _regulator_enable() argument
2864 struct regulator_dev *rdev = regulator->rdev; in _regulator_enable()
2867 lockdep_assert_held_once(&rdev->mutex.base); in _regulator_enable()
2869 if (rdev->use_count == 0 && rdev->supply) { in _regulator_enable()
2870 ret = _regulator_enable(rdev->supply); in _regulator_enable()
2876 if (rdev->coupling_desc.n_coupled > 1) { in _regulator_enable()
2882 ret = _regulator_handle_consumer_enable(regulator); in _regulator_enable()
2886 if (rdev->use_count == 0) { in _regulator_enable()
2888 * The regulator may already be enabled if it's not switchable in _regulator_enable()
2892 if (ret == -EINVAL || ret == 0) { in _regulator_enable()
2895 ret = -EPERM; in _regulator_enable()
2909 /* Fallthrough on positive return values - already enabled */ in _regulator_enable()
2912 if (regulator->enable_count == 1) in _regulator_enable()
2913 rdev->use_count++; in _regulator_enable()
2918 _regulator_handle_consumer_disable(regulator); in _regulator_enable()
2921 if (rdev->use_count == 0 && rdev->supply) in _regulator_enable()
2922 _regulator_disable(rdev->supply); in _regulator_enable()
2928 * regulator_enable - enable regulator output
2929 * @regulator: regulator source
2931 * Request that the regulator be enabled with the regulator output at
2936 * hardwired in the regulator.
2940 int regulator_enable(struct regulator *regulator) in regulator_enable() argument
2942 struct regulator_dev *rdev = regulator->rdev; in regulator_enable()
2947 ret = _regulator_enable(regulator); in regulator_enable()
2960 if (rdev->ena_pin) { in _regulator_do_disable()
2961 if (rdev->ena_gpio_state) { in _regulator_do_disable()
2965 rdev->ena_gpio_state = 0; in _regulator_do_disable()
2968 } else if (rdev->desc->ops->disable) { in _regulator_do_disable()
2969 ret = rdev->desc->ops->disable(rdev); in _regulator_do_disable()
2974 if (rdev->desc->off_on_delay) in _regulator_do_disable()
2975 rdev->last_off = ktime_get_boottime(); in _regulator_do_disable()
2983 static int _regulator_disable(struct regulator *regulator) in _regulator_disable() argument
2985 struct regulator_dev *rdev = regulator->rdev; in _regulator_disable()
2988 lockdep_assert_held_once(&rdev->mutex.base); in _regulator_disable()
2990 if (WARN(regulator->enable_count == 0, in _regulator_disable()
2992 return -EIO; in _regulator_disable()
2994 if (regulator->enable_count == 1) { in _regulator_disable()
2995 /* disabling last enable_count from this regulator */ in _regulator_disable()
2997 if (rdev->use_count == 1 && in _regulator_disable()
2998 (rdev->constraints && !rdev->constraints->always_on)) { in _regulator_disable()
3006 return -EINVAL; in _regulator_disable()
3020 rdev->use_count = 0; in _regulator_disable()
3021 } else if (rdev->use_count > 1) { in _regulator_disable()
3022 rdev->use_count--; in _regulator_disable()
3027 ret = _regulator_handle_consumer_disable(regulator); in _regulator_disable()
3029 if (ret == 0 && rdev->coupling_desc.n_coupled > 1) in _regulator_disable()
3032 if (ret == 0 && rdev->use_count == 0 && rdev->supply) in _regulator_disable()
3033 ret = _regulator_disable(rdev->supply); in _regulator_disable()
3039 * regulator_disable - disable regulator output
3040 * @regulator: regulator source
3042 * Disable the regulator output voltage or current. Calls to
3046 * NOTE: this will only disable the regulator output if no other consumer
3047 * devices have it enabled, the regulator device supports disabling and
3052 int regulator_disable(struct regulator *regulator) in regulator_disable() argument
3054 struct regulator_dev *rdev = regulator->rdev; in regulator_disable()
3059 ret = _regulator_disable(regulator); in regulator_disable()
3071 lockdep_assert_held_once(&rdev->mutex.base); in _regulator_force_disable()
3076 return -EINVAL; in _regulator_force_disable()
3093 * regulator_force_disable - force disable regulator output
3094 * @regulator: regulator source
3096 * Forcibly disable the regulator output voltage or current.
3097 * NOTE: this *will* disable the regulator output even if other consumer
3099 * damage will likely occur if the regulator is not disabled (e.g. over temp).
3103 int regulator_force_disable(struct regulator *regulator) in regulator_force_disable() argument
3105 struct regulator_dev *rdev = regulator->rdev; in regulator_force_disable()
3111 ret = _regulator_force_disable(regulator->rdev); in regulator_force_disable()
3113 if (rdev->coupling_desc.n_coupled > 1) in regulator_force_disable()
3116 if (regulator->uA_load) { in regulator_force_disable()
3117 regulator->uA_load = 0; in regulator_force_disable()
3121 if (rdev->use_count != 0 && rdev->supply) in regulator_force_disable()
3122 _regulator_disable(rdev->supply); in regulator_force_disable()
3136 struct regulator *regulator; in regulator_disable_work() local
3147 cancel_delayed_work(&rdev->disable_work); in regulator_disable_work()
3149 list_for_each_entry(regulator, &rdev->consumer_list, list) { in regulator_disable_work()
3150 count = regulator->deferred_disables; in regulator_disable_work()
3156 regulator->deferred_disables = 0; in regulator_disable_work()
3159 ret = _regulator_disable(regulator); in regulator_disable_work()
3167 if (rdev->coupling_desc.n_coupled > 1) in regulator_disable_work()
3174 * regulator_disable_deferred - disable regulator output with delay
3175 * @regulator: regulator source
3176 * @ms: milliseconds until the regulator is disabled
3178 * Execute regulator_disable() on the regulator after a delay. This
3181 * NOTE: this will only disable the regulator output if no other consumer
3182 * devices have it enabled, the regulator device supports disabling and
3187 int regulator_disable_deferred(struct regulator *regulator, int ms) in regulator_disable_deferred() argument
3189 struct regulator_dev *rdev = regulator->rdev; in regulator_disable_deferred()
3192 return regulator_disable(regulator); in regulator_disable_deferred()
3195 regulator->deferred_disables++; in regulator_disable_deferred()
3196 mod_delayed_work(system_power_efficient_wq, &rdev->disable_work, in regulator_disable_deferred()
3207 if (rdev->ena_pin) in _regulator_is_enabled()
3208 return rdev->ena_gpio_state; in _regulator_is_enabled()
3210 /* If we don't know then assume that the regulator is always on */ in _regulator_is_enabled()
3211 if (!rdev->desc->ops->is_enabled) in _regulator_is_enabled()
3214 return rdev->desc->ops->is_enabled(rdev); in _regulator_is_enabled()
3220 const struct regulator_ops *ops = rdev->desc->ops; in _regulator_list_voltage()
3223 if (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1 && !selector) in _regulator_list_voltage()
3224 return rdev->desc->fixed_uV; in _regulator_list_voltage()
3226 if (ops->list_voltage) { in _regulator_list_voltage()
3227 if (selector >= rdev->desc->n_voltages) in _regulator_list_voltage()
3228 return -EINVAL; in _regulator_list_voltage()
3229 if (selector < rdev->desc->linear_min_sel) in _regulator_list_voltage()
3233 ret = ops->list_voltage(rdev, selector); in _regulator_list_voltage()
3236 } else if (rdev->is_switch && rdev->supply) { in _regulator_list_voltage()
3237 ret = _regulator_list_voltage(rdev->supply->rdev, in _regulator_list_voltage()
3240 return -EINVAL; in _regulator_list_voltage()
3244 if (ret < rdev->constraints->min_uV) in _regulator_list_voltage()
3246 else if (ret > rdev->constraints->max_uV) in _regulator_list_voltage()
3254 * regulator_is_enabled - is the regulator output enabled
3255 * @regulator: regulator source
3257 * Note that the device backing this regulator handle can have multiple
3261 * Return: Positive if the regulator driver backing the source/client
3265 int regulator_is_enabled(struct regulator *regulator) in regulator_is_enabled() argument
3269 if (regulator->always_on) in regulator_is_enabled()
3272 regulator_lock(regulator->rdev); in regulator_is_enabled()
3273 ret = _regulator_is_enabled(regulator->rdev); in regulator_is_enabled()
3274 regulator_unlock(regulator->rdev); in regulator_is_enabled()
3281 * regulator_count_voltages - count regulator_list_voltage() selectors
3282 * @regulator: regulator source
3284 * Return: Number of selectors for @regulator, or negative error number.
3289 int regulator_count_voltages(struct regulator *regulator) in regulator_count_voltages() argument
3291 struct regulator_dev *rdev = regulator->rdev; in regulator_count_voltages()
3293 if (rdev->desc->n_voltages) in regulator_count_voltages()
3294 return rdev->desc->n_voltages; in regulator_count_voltages()
3296 if (!rdev->is_switch || !rdev->supply) in regulator_count_voltages()
3297 return -EINVAL; in regulator_count_voltages()
3299 return regulator_count_voltages(rdev->supply); in regulator_count_voltages()
3304 * regulator_list_voltage - enumerate supported voltages
3305 * @regulator: regulator source
3313 int regulator_list_voltage(struct regulator *regulator, unsigned selector) in regulator_list_voltage() argument
3315 return _regulator_list_voltage(regulator->rdev, selector, 1); in regulator_list_voltage()
3320 * regulator_get_regmap - get the regulator's register map
3321 * @regulator: regulator source
3323 * Return: Pointer to the &struct regmap for @regulator, or ERR_PTR()
3324 * encoded -%EOPNOTSUPP if @regulator doesn't use regmap.
3326 struct regmap *regulator_get_regmap(struct regulator *regulator) in regulator_get_regmap() argument
3328 struct regmap *map = regulator->rdev->regmap; in regulator_get_regmap()
3330 return map ? map : ERR_PTR(-EOPNOTSUPP); in regulator_get_regmap()
3335 * regulator_get_hardware_vsel_register - get the HW voltage selector register
3336 * @regulator: regulator source
3341 * regulator voltage. This might be useful when configuring voltage-scaling
3345 * Return: 0 on success, or -%EOPNOTSUPP if the regulator does not support
3351 int regulator_get_hardware_vsel_register(struct regulator *regulator, in regulator_get_hardware_vsel_register() argument
3355 struct regulator_dev *rdev = regulator->rdev; in regulator_get_hardware_vsel_register()
3356 const struct regulator_ops *ops = rdev->desc->ops; in regulator_get_hardware_vsel_register()
3358 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap) in regulator_get_hardware_vsel_register()
3359 return -EOPNOTSUPP; in regulator_get_hardware_vsel_register()
3361 *vsel_reg = rdev->desc->vsel_reg; in regulator_get_hardware_vsel_register()
3362 *vsel_mask = rdev->desc->vsel_mask; in regulator_get_hardware_vsel_register()
3369 * regulator_list_hardware_vsel - get the HW-specific register value for a selector
3370 * @regulator: regulator source
3373 * Converts the selector to a hardware-specific voltage selector that can be
3374 * directly written to the regulator registers. The address of the voltage
3377 * Return: 0 on success, -%EINVAL if the selector is outside the supported
3378 * range, or -%EOPNOTSUPP if the regulator does not support voltage
3381 int regulator_list_hardware_vsel(struct regulator *regulator, in regulator_list_hardware_vsel() argument
3384 struct regulator_dev *rdev = regulator->rdev; in regulator_list_hardware_vsel()
3385 const struct regulator_ops *ops = rdev->desc->ops; in regulator_list_hardware_vsel()
3387 if (selector >= rdev->desc->n_voltages) in regulator_list_hardware_vsel()
3388 return -EINVAL; in regulator_list_hardware_vsel()
3389 if (selector < rdev->desc->linear_min_sel) in regulator_list_hardware_vsel()
3391 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap) in regulator_list_hardware_vsel()
3392 return -EOPNOTSUPP; in regulator_list_hardware_vsel()
3399 * regulator_hardware_enable - access the HW for enable/disable regulator
3400 * @regulator: regulator source
3403 * Request that the regulator be enabled/disabled with the regulator output at
3408 int regulator_hardware_enable(struct regulator *regulator, bool enable) in regulator_hardware_enable() argument
3410 struct regulator_dev *rdev = regulator->rdev; in regulator_hardware_enable()
3411 const struct regulator_ops *ops = rdev->desc->ops; in regulator_hardware_enable()
3412 int ret = -EOPNOTSUPP; in regulator_hardware_enable()
3414 if (!rdev->exclusive || !ops || !ops->enable || !ops->disable) in regulator_hardware_enable()
3418 ret = ops->enable(rdev); in regulator_hardware_enable()
3420 ret = ops->disable(rdev); in regulator_hardware_enable()
3427 * regulator_get_linear_step - return the voltage step size between VSEL values
3428 * @regulator: regulator source
3431 * or 0 if the regulator isn't a linear regulator.
3433 unsigned int regulator_get_linear_step(struct regulator *regulator) in regulator_get_linear_step() argument
3435 struct regulator_dev *rdev = regulator->rdev; in regulator_get_linear_step()
3437 return rdev->desc->uV_step; in regulator_get_linear_step()
3442 * regulator_is_supported_voltage - check if a voltage range can be supported
3444 * @regulator: Regulator to check.
3449 * number if @regulator's voltage can't be changed and voltage readback
3452 int regulator_is_supported_voltage(struct regulator *regulator, in regulator_is_supported_voltage() argument
3455 struct regulator_dev *rdev = regulator->rdev; in regulator_is_supported_voltage()
3460 ret = regulator_get_voltage(regulator); in regulator_is_supported_voltage()
3468 if (rdev->desc->continuous_voltage_range) in regulator_is_supported_voltage()
3469 return min_uV >= rdev->constraints->min_uV && in regulator_is_supported_voltage()
3470 max_uV <= rdev->constraints->max_uV; in regulator_is_supported_voltage()
3472 ret = regulator_count_voltages(regulator); in regulator_is_supported_voltage()
3478 ret = regulator_list_voltage(regulator, i); in regulator_is_supported_voltage()
3491 const struct regulator_desc *desc = rdev->desc; in regulator_map_voltage()
3493 if (desc->ops->map_voltage) in regulator_map_voltage()
3494 return desc->ops->map_voltage(rdev, min_uV, max_uV); in regulator_map_voltage()
3496 if (desc->ops->list_voltage == regulator_list_voltage_linear) in regulator_map_voltage()
3499 if (desc->ops->list_voltage == regulator_list_voltage_linear_range) in regulator_map_voltage()
3502 if (desc->ops->list_voltage == in regulator_map_voltage()
3523 return -EINVAL; in _regulator_call_set_voltage()
3525 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV, selector); in _regulator_call_set_voltage()
3547 return -EINVAL; in _regulator_call_set_voltage_sel()
3549 ret = rdev->desc->ops->set_voltage_sel(rdev, selector); in _regulator_call_set_voltage_sel()
3562 const struct regulator_ops *ops = rdev->desc->ops; in _regulator_set_voltage_sel_step()
3565 /* Stepping is only needed if the regulator is enabled. */ in _regulator_set_voltage_sel_step()
3569 if (!ops->get_voltage_sel) in _regulator_set_voltage_sel_step()
3570 return -EINVAL; in _regulator_set_voltage_sel_step()
3572 old_sel = ops->get_voltage_sel(rdev); in _regulator_set_voltage_sel_step()
3576 diff = new_selector - old_sel; in _regulator_set_voltage_sel_step()
3582 for (curr_sel = old_sel + rdev->desc->vsel_step; in _regulator_set_voltage_sel_step()
3584 curr_sel += rdev->desc->vsel_step) { in _regulator_set_voltage_sel_step()
3591 ret = ops->set_voltage_sel(rdev, curr_sel); in _regulator_set_voltage_sel_step()
3597 for (curr_sel = old_sel - rdev->desc->vsel_step; in _regulator_set_voltage_sel_step()
3599 curr_sel -= rdev->desc->vsel_step) { in _regulator_set_voltage_sel_step()
3600 ret = ops->set_voltage_sel(rdev, curr_sel); in _regulator_set_voltage_sel_step()
3615 (void)ops->set_voltage_sel(rdev, old_sel); in _regulator_set_voltage_sel_step()
3624 if (rdev->constraints->ramp_delay) in _regulator_set_voltage_time()
3625 ramp_delay = rdev->constraints->ramp_delay; in _regulator_set_voltage_time()
3626 else if (rdev->desc->ramp_delay) in _regulator_set_voltage_time()
3627 ramp_delay = rdev->desc->ramp_delay; in _regulator_set_voltage_time()
3628 else if (rdev->constraints->settling_time) in _regulator_set_voltage_time()
3629 return rdev->constraints->settling_time; in _regulator_set_voltage_time()
3630 else if (rdev->constraints->settling_time_up && in _regulator_set_voltage_time()
3632 return rdev->constraints->settling_time_up; in _regulator_set_voltage_time()
3633 else if (rdev->constraints->settling_time_down && in _regulator_set_voltage_time()
3635 return rdev->constraints->settling_time_down; in _regulator_set_voltage_time()
3640 return DIV_ROUND_UP(abs(new_uV - old_uV), ramp_delay); in _regulator_set_voltage_time()
3650 int old_selector = -1; in _regulator_do_set_voltage()
3651 const struct regulator_ops *ops = rdev->desc->ops; in _regulator_do_set_voltage()
3656 min_uV += rdev->constraints->uV_offset; in _regulator_do_set_voltage()
3657 max_uV += rdev->constraints->uV_offset; in _regulator_do_set_voltage()
3664 ops->set_voltage_time_sel && ops->get_voltage_sel) { in _regulator_do_set_voltage()
3665 old_selector = ops->get_voltage_sel(rdev); in _regulator_do_set_voltage()
3670 if (ops->set_voltage) { in _regulator_do_set_voltage()
3675 if (ops->list_voltage) in _regulator_do_set_voltage()
3676 best_val = ops->list_voltage(rdev, in _regulator_do_set_voltage()
3682 } else if (ops->set_voltage_sel) { in _regulator_do_set_voltage()
3685 best_val = ops->list_voltage(rdev, ret); in _regulator_do_set_voltage()
3690 else if (rdev->desc->vsel_step) in _regulator_do_set_voltage()
3697 ret = -EINVAL; in _regulator_do_set_voltage()
3701 ret = -EINVAL; in _regulator_do_set_voltage()
3707 if (ops->set_voltage_time_sel) { in _regulator_do_set_voltage()
3713 delay = ops->set_voltage_time_sel(rdev, old_selector, in _regulator_do_set_voltage()
3717 if (ops->set_voltage_time) in _regulator_do_set_voltage()
3718 delay = ops->set_voltage_time(rdev, old_uV, in _regulator_do_set_voltage()
3756 return -EINVAL; in _regulator_do_set_suspend_voltage()
3758 if (min_uV < rstate->min_uV) in _regulator_do_set_suspend_voltage()
3759 min_uV = rstate->min_uV; in _regulator_do_set_suspend_voltage()
3760 if (max_uV > rstate->max_uV) in _regulator_do_set_suspend_voltage()
3761 max_uV = rstate->max_uV; in _regulator_do_set_suspend_voltage()
3767 uV = rdev->desc->ops->list_voltage(rdev, sel); in _regulator_do_set_suspend_voltage()
3769 rstate->uV = uV; in _regulator_do_set_suspend_voltage()
3774 static int regulator_set_voltage_unlocked(struct regulator *regulator, in regulator_set_voltage_unlocked() argument
3778 struct regulator_dev *rdev = regulator->rdev; in regulator_set_voltage_unlocked()
3779 struct regulator_voltage *voltage = ®ulator->voltage[state]; in regulator_set_voltage_unlocked()
3788 if (voltage->min_uV == min_uV && voltage->max_uV == max_uV) in regulator_set_voltage_unlocked()
3792 * return successfully even though the regulator does not support in regulator_set_voltage_unlocked()
3798 voltage->min_uV = min_uV; in regulator_set_voltage_unlocked()
3799 voltage->max_uV = max_uV; in regulator_set_voltage_unlocked()
3805 if (!rdev->desc->ops->set_voltage && in regulator_set_voltage_unlocked()
3806 !rdev->desc->ops->set_voltage_sel) { in regulator_set_voltage_unlocked()
3807 ret = -EINVAL; in regulator_set_voltage_unlocked()
3817 old_min_uV = voltage->min_uV; in regulator_set_voltage_unlocked()
3818 old_max_uV = voltage->max_uV; in regulator_set_voltage_unlocked()
3819 voltage->min_uV = min_uV; in regulator_set_voltage_unlocked()
3820 voltage->max_uV = max_uV; in regulator_set_voltage_unlocked()
3825 voltage->min_uV = old_min_uV; in regulator_set_voltage_unlocked()
3826 voltage->max_uV = old_max_uV; in regulator_set_voltage_unlocked()
3840 if (rdev->supply && in regulator_set_voltage_rdev()
3841 regulator_ops_is_valid(rdev->supply->rdev, in regulator_set_voltage_rdev()
3843 (rdev->desc->min_dropout_uV || !(rdev->desc->ops->get_voltage || in regulator_set_voltage_rdev()
3844 rdev->desc->ops->get_voltage_sel))) { in regulator_set_voltage_rdev()
3860 best_supply_uV += rdev->desc->min_dropout_uV; in regulator_set_voltage_rdev()
3862 current_supply_uV = regulator_get_voltage_rdev(rdev->supply->rdev); in regulator_set_voltage_rdev()
3868 supply_change_uV = best_supply_uV - current_supply_uV; in regulator_set_voltage_rdev()
3872 ret = regulator_set_voltage_unlocked(rdev->supply, in regulator_set_voltage_rdev()
3875 dev_err(&rdev->dev, "Failed to increase supply voltage: %pe\n", in regulator_set_voltage_rdev()
3890 ret = regulator_set_voltage_unlocked(rdev->supply, in regulator_set_voltage_rdev()
3893 dev_warn(&rdev->dev, "Failed to decrease supply voltage: %pe\n", in regulator_set_voltage_rdev()
3907 struct regulation_constraints *constraints = rdev->constraints; in regulator_limit_voltage_step()
3910 if (!constraints->max_uV_step || !_regulator_is_enabled(rdev)) in regulator_limit_voltage_step()
3920 if (abs(*current_uV - *min_uV) <= constraints->max_uV_step) in regulator_limit_voltage_step()
3925 *min_uV = min(*current_uV + constraints->max_uV_step, in regulator_limit_voltage_step()
3928 *min_uV = max(*current_uV - constraints->max_uV_step, in regulator_limit_voltage_step()
3940 struct coupling_desc *c_desc = &rdev->coupling_desc; in regulator_get_optimal_voltage()
3941 struct regulator_dev **c_rdevs = c_desc->coupled_rdevs; in regulator_get_optimal_voltage()
3942 struct regulation_constraints *constraints = rdev->constraints; in regulator_get_optimal_voltage()
3949 *current_uV = -1; in regulator_get_optimal_voltage()
3960 desired_min_uV = constraints->min_uV; in regulator_get_optimal_voltage()
3961 desired_max_uV = constraints->max_uV; in regulator_get_optimal_voltage()
3979 lockdep_assert_held_once(&c_rdevs[i]->mutex.base); in regulator_get_optimal_voltage()
3999 max_spread = constraints->max_spread[0]; in regulator_get_optimal_voltage()
4006 target_uV = max(desired_min_uV, highest_min_uV - max_spread); in regulator_get_optimal_voltage()
4034 possible_uV = max(target_uV, max_current_uV - max_spread); in regulator_get_optimal_voltage()
4039 return -EINVAL; in regulator_get_optimal_voltage()
4057 if (n_coupled > 1 && *current_uV == -1) { in regulator_get_optimal_voltage()
4081 struct coupling_desc *c_desc = &rdev->coupling_desc; in regulator_do_balance_voltage()
4087 c_rdevs = c_desc->coupled_rdevs; in regulator_do_balance_voltage()
4088 n_coupled = skip_coupled ? 1 : c_desc->n_coupled; in regulator_do_balance_voltage()
4109 * i-th regulator at the moment without violating in regulator_do_balance_voltage()
4126 delta = abs(optimal_uV - current_uV); in regulator_do_balance_voltage()
4162 struct coupling_desc *c_desc = &rdev->coupling_desc; in regulator_balance_voltage()
4163 struct regulator_coupler *coupler = c_desc->coupler; in regulator_balance_voltage()
4173 if (c_desc->n_resolved < c_desc->n_coupled) { in regulator_balance_voltage()
4175 return -EPERM; in regulator_balance_voltage()
4179 if (coupler && coupler->balance_voltage) in regulator_balance_voltage()
4180 return coupler->balance_voltage(coupler, rdev, state); in regulator_balance_voltage()
4186 * regulator_set_voltage - set regulator output voltage
4187 * @regulator: regulator source
4191 * Sets a voltage regulator to the desired output voltage. This can be set
4192 * during any regulator state. IOW, regulator can be disabled or enabled.
4194 * If the regulator is enabled then the voltage will change to the new value
4195 * immediately otherwise if the regulator is disabled the regulator will
4198 * NOTE: If the regulator is shared between several devices then the lowest
4200 * Regulator system constraints must be set for this regulator before
4205 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV) in regulator_set_voltage() argument
4210 regulator_lock_dependent(regulator->rdev, &ww_ctx); in regulator_set_voltage()
4212 ret = regulator_set_voltage_unlocked(regulator, min_uV, max_uV, in regulator_set_voltage()
4215 regulator_unlock_dependent(regulator->rdev, &ww_ctx); in regulator_set_voltage()
4228 return -EINVAL; in regulator_suspend_toggle()
4230 if (!rstate->changeable) in regulator_suspend_toggle()
4231 return -EPERM; in regulator_suspend_toggle()
4233 rstate->enabled = (en) ? ENABLE_IN_SUSPEND : DISABLE_IN_SUSPEND; in regulator_suspend_toggle()
4248 struct regulator *regulator; in regulator_suspend_disable() local
4252 * if any consumer wants this regulator device keeping on in in regulator_suspend_disable()
4255 list_for_each_entry(regulator, &rdev->consumer_list, list) { in regulator_suspend_disable()
4256 voltage = ®ulator->voltage[state]; in regulator_suspend_disable()
4257 if (voltage->min_uV || voltage->max_uV) in regulator_suspend_disable()
4265 static int _regulator_set_suspend_voltage(struct regulator *regulator, in _regulator_set_suspend_voltage() argument
4269 struct regulator_dev *rdev = regulator->rdev; in _regulator_set_suspend_voltage()
4274 return -EINVAL; in _regulator_set_suspend_voltage()
4276 if (rstate->min_uV == rstate->max_uV) { in _regulator_set_suspend_voltage()
4278 return -EPERM; in _regulator_set_suspend_voltage()
4281 return regulator_set_voltage_unlocked(regulator, min_uV, max_uV, state); in _regulator_set_suspend_voltage()
4284 int regulator_set_suspend_voltage(struct regulator *regulator, int min_uV, in regulator_set_suspend_voltage() argument
4292 return -EINVAL; in regulator_set_suspend_voltage()
4294 regulator_lock_dependent(regulator->rdev, &ww_ctx); in regulator_set_suspend_voltage()
4296 ret = _regulator_set_suspend_voltage(regulator, min_uV, in regulator_set_suspend_voltage()
4299 regulator_unlock_dependent(regulator->rdev, &ww_ctx); in regulator_set_suspend_voltage()
4306 * regulator_set_voltage_time - get raise/fall time
4307 * @regulator: regulator source
4317 int regulator_set_voltage_time(struct regulator *regulator, in regulator_set_voltage_time() argument
4320 struct regulator_dev *rdev = regulator->rdev; in regulator_set_voltage_time()
4321 const struct regulator_ops *ops = rdev->desc->ops; in regulator_set_voltage_time()
4322 int old_sel = -1; in regulator_set_voltage_time()
4323 int new_sel = -1; in regulator_set_voltage_time()
4327 if (ops->set_voltage_time) in regulator_set_voltage_time()
4328 return ops->set_voltage_time(rdev, old_uV, new_uV); in regulator_set_voltage_time()
4329 else if (!ops->set_voltage_time_sel) in regulator_set_voltage_time()
4333 if (!ops->list_voltage || !rdev->desc->n_voltages) in regulator_set_voltage_time()
4334 return -EINVAL; in regulator_set_voltage_time()
4336 for (i = 0; i < rdev->desc->n_voltages; i++) { in regulator_set_voltage_time()
4338 if (i < rdev->desc->linear_min_sel) in regulator_set_voltage_time()
4344 voltage = regulator_list_voltage(regulator, i); in regulator_set_voltage_time()
4346 return -EINVAL; in regulator_set_voltage_time()
4356 return -EINVAL; in regulator_set_voltage_time()
4358 return ops->set_voltage_time_sel(rdev, old_sel, new_sel); in regulator_set_voltage_time()
4363 * regulator_set_voltage_time_sel - get raise/fall time
4364 * @rdev: regulator source device
4383 if (!rdev->desc->ops->list_voltage) in regulator_set_voltage_time_sel()
4384 return -EINVAL; in regulator_set_voltage_time_sel()
4386 old_volt = rdev->desc->ops->list_voltage(rdev, old_selector); in regulator_set_voltage_time_sel()
4387 new_volt = rdev->desc->ops->list_voltage(rdev, new_selector); in regulator_set_voltage_time_sel()
4389 if (rdev->desc->ops->set_voltage_time) in regulator_set_voltage_time_sel()
4390 return rdev->desc->ops->set_voltage_time(rdev, old_volt, in regulator_set_voltage_time_sel()
4403 if (!rdev->desc->ops->set_voltage && in regulator_sync_voltage_rdev()
4404 !rdev->desc->ops->set_voltage_sel) { in regulator_sync_voltage_rdev()
4405 ret = -EINVAL; in regulator_sync_voltage_rdev()
4409 /* balance only, if regulator is coupled */ in regulator_sync_voltage_rdev()
4410 if (rdev->coupling_desc.n_coupled > 1) in regulator_sync_voltage_rdev()
4413 ret = -EOPNOTSUPP; in regulator_sync_voltage_rdev()
4421 * regulator_sync_voltage - re-apply last regulator output voltage
4422 * @regulator: regulator source
4424 * Re-apply the last configured voltage. This is intended to be used
4430 int regulator_sync_voltage(struct regulator *regulator) in regulator_sync_voltage() argument
4432 struct regulator_dev *rdev = regulator->rdev; in regulator_sync_voltage()
4433 struct regulator_voltage *voltage = ®ulator->voltage[PM_SUSPEND_ON]; in regulator_sync_voltage()
4441 if (!rdev->desc->ops->set_voltage && in regulator_sync_voltage()
4442 !rdev->desc->ops->set_voltage_sel) { in regulator_sync_voltage()
4443 ret = -EINVAL; in regulator_sync_voltage()
4448 if (!voltage->min_uV && !voltage->max_uV) { in regulator_sync_voltage()
4449 ret = -EINVAL; in regulator_sync_voltage()
4453 min_uV = voltage->min_uV; in regulator_sync_voltage()
4454 max_uV = voltage->max_uV; in regulator_sync_voltage()
4465 /* balance only, if regulator is coupled */ in regulator_sync_voltage()
4466 if (rdev->coupling_desc.n_coupled > 1) in regulator_sync_voltage()
4482 if (rdev->desc->ops->get_bypass) { in regulator_get_voltage_rdev()
4483 ret = rdev->desc->ops->get_bypass(rdev, &bypassed); in regulator_get_voltage_rdev()
4487 /* if bypassed the regulator must have a supply */ in regulator_get_voltage_rdev()
4488 if (!rdev->supply) { in regulator_get_voltage_rdev()
4490 "bypassed regulator has no supply!\n"); in regulator_get_voltage_rdev()
4491 return -EPROBE_DEFER; in regulator_get_voltage_rdev()
4494 return regulator_get_voltage_rdev(rdev->supply->rdev); in regulator_get_voltage_rdev()
4498 if (rdev->desc->ops->get_voltage_sel) { in regulator_get_voltage_rdev()
4499 sel = rdev->desc->ops->get_voltage_sel(rdev); in regulator_get_voltage_rdev()
4502 ret = rdev->desc->ops->list_voltage(rdev, sel); in regulator_get_voltage_rdev()
4503 } else if (rdev->desc->ops->get_voltage) { in regulator_get_voltage_rdev()
4504 ret = rdev->desc->ops->get_voltage(rdev); in regulator_get_voltage_rdev()
4505 } else if (rdev->desc->ops->list_voltage) { in regulator_get_voltage_rdev()
4506 ret = rdev->desc->ops->list_voltage(rdev, 0); in regulator_get_voltage_rdev()
4507 } else if (rdev->desc->fixed_uV && (rdev->desc->n_voltages == 1)) { in regulator_get_voltage_rdev()
4508 ret = rdev->desc->fixed_uV; in regulator_get_voltage_rdev()
4509 } else if (rdev->supply) { in regulator_get_voltage_rdev()
4510 ret = regulator_get_voltage_rdev(rdev->supply->rdev); in regulator_get_voltage_rdev()
4511 } else if (rdev->supply_name) { in regulator_get_voltage_rdev()
4512 return -EPROBE_DEFER; in regulator_get_voltage_rdev()
4514 return -EINVAL; in regulator_get_voltage_rdev()
4519 return ret - rdev->constraints->uV_offset; in regulator_get_voltage_rdev()
4524 * regulator_get_voltage - get regulator output voltage
4525 * @regulator: regulator source
4527 * Return: Current regulator voltage in uV, or a negative error number on failure.
4529 * NOTE: If the regulator is disabled it will return the voltage value. This
4530 * function should not be used to determine regulator state.
4532 int regulator_get_voltage(struct regulator *regulator) in regulator_get_voltage() argument
4537 regulator_lock_dependent(regulator->rdev, &ww_ctx); in regulator_get_voltage()
4538 ret = regulator_get_voltage_rdev(regulator->rdev); in regulator_get_voltage()
4539 regulator_unlock_dependent(regulator->rdev, &ww_ctx); in regulator_get_voltage()
4546 * regulator_set_current_limit - set regulator output current limit
4547 * @regulator: regulator source
4552 * any regulator state. IOW, regulator can be disabled or enabled.
4554 * If the regulator is enabled then the current will change to the new value
4555 * immediately otherwise if the regulator is disabled the regulator will
4558 * NOTE: Regulator system constraints must be set for this regulator before
4563 int regulator_set_current_limit(struct regulator *regulator, in regulator_set_current_limit() argument
4566 struct regulator_dev *rdev = regulator->rdev; in regulator_set_current_limit()
4572 if (!rdev->desc->ops->set_current_limit) { in regulator_set_current_limit()
4573 ret = -EINVAL; in regulator_set_current_limit()
4582 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA); in regulator_set_current_limit()
4592 if (!rdev->desc->ops->get_current_limit) in _regulator_get_current_limit_unlocked()
4593 return -EINVAL; in _regulator_get_current_limit_unlocked()
4595 return rdev->desc->ops->get_current_limit(rdev); in _regulator_get_current_limit_unlocked()
4610 * regulator_get_current_limit - get regulator output current
4611 * @regulator: regulator source
4616 * NOTE: If the regulator is disabled it will return the current value. This
4617 * function should not be used to determine regulator state.
4619 int regulator_get_current_limit(struct regulator *regulator) in regulator_get_current_limit() argument
4621 return _regulator_get_current_limit(regulator->rdev); in regulator_get_current_limit()
4626 * regulator_set_mode - set regulator operating mode
4627 * @regulator: regulator source
4628 * @mode: operating mode - one of the REGULATOR_MODE constants
4630 * Set regulator operating mode to increase regulator efficiency or improve
4633 * NOTE: Regulator system constraints must be set for this regulator before
4638 int regulator_set_mode(struct regulator *regulator, unsigned int mode) in regulator_set_mode() argument
4640 struct regulator_dev *rdev = regulator->rdev; in regulator_set_mode()
4647 if (!rdev->desc->ops->set_mode) { in regulator_set_mode()
4648 ret = -EINVAL; in regulator_set_mode()
4653 if (rdev->desc->ops->get_mode) { in regulator_set_mode()
4654 regulator_curr_mode = rdev->desc->ops->get_mode(rdev); in regulator_set_mode()
4666 ret = rdev->desc->ops->set_mode(rdev, mode); in regulator_set_mode()
4676 if (!rdev->desc->ops->get_mode) in _regulator_get_mode_unlocked()
4677 return -EINVAL; in _regulator_get_mode_unlocked()
4679 return rdev->desc->ops->get_mode(rdev); in _regulator_get_mode_unlocked()
4694 * regulator_get_mode - get regulator operating mode
4695 * @regulator: regulator source
4697 * Get the current regulator operating mode.
4702 unsigned int regulator_get_mode(struct regulator *regulator) in regulator_get_mode() argument
4704 return _regulator_get_mode(regulator->rdev); in regulator_get_mode()
4712 if (rdev->use_cached_err) { in rdev_get_cached_err_flags()
4713 spin_lock(&rdev->err_lock); in rdev_get_cached_err_flags()
4714 ret = rdev->cached_err; in rdev_get_cached_err_flags()
4715 spin_unlock(&rdev->err_lock); in rdev_get_cached_err_flags()
4729 if (rdev->desc->ops->get_error_flags) in _regulator_get_error_flags()
4730 ret = rdev->desc->ops->get_error_flags(rdev, flags); in _regulator_get_error_flags()
4731 else if (!rdev->use_cached_err) in _regulator_get_error_flags()
4732 ret = -EINVAL; in _regulator_get_error_flags()
4742 * regulator_get_error_flags - get regulator error information
4743 * @regulator: regulator source
4746 * Get the current regulator error information.
4750 int regulator_get_error_flags(struct regulator *regulator, in regulator_get_error_flags() argument
4753 return _regulator_get_error_flags(regulator->rdev, flags); in regulator_get_error_flags()
4758 * regulator_set_load - set regulator load
4759 * @regulator: regulator source
4762 * Notifies the regulator core of a new device load. This is then used by
4763 * DRMS (if enabled by constraints) to set the most efficient regulator
4764 * operating mode for the new regulator loading.
4766 * Consumer devices notify their supply regulator of the maximum power
4770 * consumption are :-
4778 * DRMS will sum the total requested load on the regulator and change
4779 * to the most efficient operating mode if platform constraints allow.
4781 * NOTE: when a regulator consumer requests to have a regulator
4783 * toward the total requested load. If the regulator is re-enabled
4786 * If a regulator is an always-on regulator then an individual consumer's
4791 int regulator_set_load(struct regulator *regulator, int uA_load) in regulator_set_load() argument
4793 struct regulator_dev *rdev = regulator->rdev; in regulator_set_load()
4798 old_uA_load = regulator->uA_load; in regulator_set_load()
4799 regulator->uA_load = uA_load; in regulator_set_load()
4800 if (regulator->enable_count && old_uA_load != uA_load) { in regulator_set_load()
4803 regulator->uA_load = old_uA_load; in regulator_set_load()
4812 * regulator_allow_bypass - allow the regulator to go into bypass mode
4814 * @regulator: Regulator to configure
4815 * @enable: enable or disable bypass mode
4817 * Allow the regulator to go into bypass mode if all other consumers
4818 * for the regulator also enable bypass mode and the machine
4819 * constraints allow this. Bypass mode means that the regulator is
4822 * Return: 0 on success or if changing bypass is not possible, or
4825 int regulator_allow_bypass(struct regulator *regulator, bool enable) in regulator_allow_bypass() argument
4827 struct regulator_dev *rdev = regulator->rdev; in regulator_allow_bypass()
4831 if (!rdev->desc->ops->set_bypass) in regulator_allow_bypass()
4839 if (enable && !regulator->bypass) { in regulator_allow_bypass()
4840 rdev->bypass_count++; in regulator_allow_bypass()
4842 if (rdev->bypass_count == rdev->open_count) { in regulator_allow_bypass()
4845 ret = rdev->desc->ops->set_bypass(rdev, enable); in regulator_allow_bypass()
4847 rdev->bypass_count--; in regulator_allow_bypass()
4852 } else if (!enable && regulator->bypass) { in regulator_allow_bypass()
4853 rdev->bypass_count--; in regulator_allow_bypass()
4855 if (rdev->bypass_count != rdev->open_count) { in regulator_allow_bypass()
4858 ret = rdev->desc->ops->set_bypass(rdev, enable); in regulator_allow_bypass()
4860 rdev->bypass_count++; in regulator_allow_bypass()
4867 regulator->bypass = enable; in regulator_allow_bypass()
4876 * regulator_register_notifier - register regulator event notifier
4877 * @regulator: regulator source
4880 * Register notifier block to receive regulator events.
4884 int regulator_register_notifier(struct regulator *regulator, in regulator_register_notifier() argument
4887 return blocking_notifier_chain_register(®ulator->rdev->notifier, in regulator_register_notifier()
4893 * regulator_unregister_notifier - unregister regulator event notifier
4894 * @regulator: regulator source
4897 * Unregister regulator event notifier block.
4901 int regulator_unregister_notifier(struct regulator *regulator, in regulator_unregister_notifier() argument
4904 return blocking_notifier_chain_unregister(®ulator->rdev->notifier, in regulator_unregister_notifier()
4909 /* notify regulator consumers and downstream regulator consumers.
4916 int ret = blocking_notifier_call_chain(&rdev->notifier, event, data); in _notifier_call_chain()
4919 struct device *parent = rdev->dev.parent; in _notifier_call_chain()
4924 if (parent && rname == rdev->desc->name) { in _notifier_call_chain()
4925 snprintf(name, sizeof(name), "%s-%s", dev_name(parent), in _notifier_call_chain()
4968 while (--i >= 0) in _regulator_bulk_get()
4975 * regulator_bulk_get - get multiple regulator consumers
4981 * This helper function allows drivers to get several regulator
4999 bulk->ret = regulator_enable(bulk->consumer); in regulator_bulk_enable_async()
5003 * regulator_bulk_enable - enable multiple regulator consumers
5008 * This convenience API allows consumers to enable multiple regulator
5053 * regulator_bulk_disable - disable multiple regulator consumers
5058 * This convenience API allows consumers to disable multiple regulator
5071 for (i = num_consumers - 1; i >= 0; --i) { in regulator_bulk_disable()
5084 pr_err("Failed to re-enable %s: %pe\n", in regulator_bulk_disable()
5093 * regulator_bulk_force_disable - force disable multiple regulator consumers
5098 * This convenience API allows consumers to forcibly disable multiple regulator
5127 * regulator_bulk_free - free multiple regulator consumers
5132 * This convenience API allows consumers to free multiple regulator
5148 * regulator_handle_critical - Handle events for system-critical regulators.
5149 * @rdev: The regulator device.
5152 * This function handles critical events such as under-voltage, over-current,
5153 * and unknown errors for regulators deemed system-critical. On detecting such
5161 if (!rdev->constraints->system_critical) in regulator_handle_critical()
5166 reason = "System critical regulator: voltage drop detected"; in regulator_handle_critical()
5169 reason = "System critical regulator: over-current detected"; in regulator_handle_critical()
5172 reason = "System critical regulator: unknown error"; in regulator_handle_critical()
5179 rdev->constraints->uv_less_critical_window_ms); in regulator_handle_critical()
5183 * regulator_notifier_call_chain - call regulator event notifier
5184 * @rdev: regulator source
5186 * @data: callback-specific data.
5188 * Called by regulator drivers to notify clients a regulator event has
5205 * regulator_mode_to_status - convert a regulator mode into a status
5209 * Convert a regulator mode into a status.
5275 const struct regulator_ops *ops = rdev->desc->ops; in regulator_attr_is_visible()
5276 umode_t mode = attr->mode; in regulator_attr_is_visible()
5286 if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) || in regulator_attr_is_visible()
5287 (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) || in regulator_attr_is_visible()
5288 (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0) || in regulator_attr_is_visible()
5289 (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1)) in regulator_attr_is_visible()
5295 return ops->get_current_limit ? mode : 0; in regulator_attr_is_visible()
5298 return ops->get_mode ? mode : 0; in regulator_attr_is_visible()
5301 return (rdev->ena_pin || ops->is_enabled) ? mode : 0; in regulator_attr_is_visible()
5304 return ops->get_status ? mode : 0; in regulator_attr_is_visible()
5307 return ops->get_bypass ? mode : 0; in regulator_attr_is_visible()
5318 return ops->get_error_flags ? mode : 0; in regulator_attr_is_visible()
5323 return (ops->set_voltage || ops->set_voltage_sel) ? mode : 0; in regulator_attr_is_visible()
5327 return ops->set_current_limit ? mode : 0; in regulator_attr_is_visible()
5337 return ops->set_suspend_voltage ? mode : 0; in regulator_attr_is_visible()
5342 return ops->set_suspend_mode ? mode : 0; in regulator_attr_is_visible()
5361 debugfs_remove_recursive(rdev->debugfs); in regulator_dev_release()
5362 kfree(rdev->constraints); in regulator_dev_release()
5363 of_node_put(rdev->dev.of_node); in regulator_dev_release()
5369 struct device *parent = rdev->dev.parent; in rdev_init_debugfs()
5374 if (parent && rname == rdev->desc->name) { in rdev_init_debugfs()
5375 snprintf(name, sizeof(name), "%s-%s", dev_name(parent), in rdev_init_debugfs()
5380 rdev->debugfs = debugfs_create_dir(rname, debugfs_root); in rdev_init_debugfs()
5381 if (IS_ERR(rdev->debugfs)) in rdev_init_debugfs()
5384 debugfs_create_u32("use_count", 0444, rdev->debugfs, in rdev_init_debugfs()
5385 &rdev->use_count); in rdev_init_debugfs()
5386 debugfs_create_u32("open_count", 0444, rdev->debugfs, in rdev_init_debugfs()
5387 &rdev->open_count); in rdev_init_debugfs()
5388 debugfs_create_u32("bypass_count", 0444, rdev->debugfs, in rdev_init_debugfs()
5389 &rdev->bypass_count); in rdev_init_debugfs()
5405 list_add_tail(&coupler->list, ®ulator_coupler_list); in regulator_coupler_register()
5423 err = coupler->attach_regulator(coupler, rdev); in regulator_find_coupler()
5425 if (!coupler->balance_voltage && in regulator_find_coupler()
5426 rdev->coupling_desc.n_coupled > 2) in regulator_find_coupler()
5441 return ERR_PTR(-EINVAL); in regulator_find_coupler()
5444 if (coupler->detach_regulator) in regulator_find_coupler()
5445 coupler->detach_regulator(coupler, rdev); in regulator_find_coupler()
5448 "Voltage balancing for multiple regulator couples is unimplemented\n"); in regulator_find_coupler()
5450 return ERR_PTR(-EPERM); in regulator_find_coupler()
5455 struct regulator_coupler *coupler = rdev->coupling_desc.coupler; in regulator_resolve_coupling()
5456 struct coupling_desc *c_desc = &rdev->coupling_desc; in regulator_resolve_coupling()
5457 int n_coupled = c_desc->n_coupled; in regulator_resolve_coupling()
5463 if (c_desc->coupled_rdevs[i]) in regulator_resolve_coupling()
5466 c_rdev = of_parse_coupled_regulator(rdev, i - 1); in regulator_resolve_coupling()
5471 if (c_rdev->coupling_desc.coupler != coupler) { in regulator_resolve_coupling()
5477 c_desc->coupled_rdevs[i] = c_rdev; in regulator_resolve_coupling()
5478 c_desc->n_resolved++; in regulator_resolve_coupling()
5486 struct regulator_coupler *coupler = rdev->coupling_desc.coupler; in regulator_remove_coupling()
5487 struct coupling_desc *__c_desc, *c_desc = &rdev->coupling_desc; in regulator_remove_coupling()
5493 n_coupled = c_desc->n_coupled; in regulator_remove_coupling()
5496 c_rdev = c_desc->coupled_rdevs[i]; in regulator_remove_coupling()
5503 __c_desc = &c_rdev->coupling_desc; in regulator_remove_coupling()
5504 __n_coupled = __c_desc->n_coupled; in regulator_remove_coupling()
5507 __c_rdev = __c_desc->coupled_rdevs[k]; in regulator_remove_coupling()
5510 __c_desc->coupled_rdevs[k] = NULL; in regulator_remove_coupling()
5511 __c_desc->n_resolved--; in regulator_remove_coupling()
5518 c_desc->coupled_rdevs[i] = NULL; in regulator_remove_coupling()
5519 c_desc->n_resolved--; in regulator_remove_coupling()
5522 if (coupler && coupler->detach_regulator) { in regulator_remove_coupling()
5523 err = coupler->detach_regulator(coupler, rdev); in regulator_remove_coupling()
5529 kfree(rdev->coupling_desc.coupled_rdevs); in regulator_remove_coupling()
5530 rdev->coupling_desc.coupled_rdevs = NULL; in regulator_remove_coupling()
5545 return -ENOMEM; in regulator_init_coupling()
5547 rdev->coupling_desc.coupled_rdevs = coupled; in regulator_init_coupling()
5550 * Every regulator should always have coupling descriptor filled with in regulator_init_coupling()
5553 rdev->coupling_desc.coupled_rdevs[0] = rdev; in regulator_init_coupling()
5554 rdev->coupling_desc.n_coupled = n_phandles + 1; in regulator_init_coupling()
5555 rdev->coupling_desc.n_resolved++; in regulator_init_coupling()
5557 /* regulator isn't coupled */ in regulator_init_coupling()
5562 return -EPERM; in regulator_init_coupling()
5565 rdev->coupling_desc.coupler = regulator_find_coupler(rdev); in regulator_init_coupling()
5568 if (IS_ERR(rdev->coupling_desc.coupler)) { in regulator_init_coupling()
5569 err = PTR_ERR(rdev->coupling_desc.coupler); in regulator_init_coupling()
5580 if (rdev->coupling_desc.n_coupled > 2) { in generic_coupler_attach()
5582 "Voltage balancing for multiple regulator couples is unimplemented\n"); in generic_coupler_attach()
5583 return -EPERM; in generic_coupler_attach()
5586 if (!rdev->constraints->always_on) { in generic_coupler_attach()
5588 "Coupling of a non always-on regulator is unimplemented\n"); in generic_coupler_attach()
5589 return -ENOTSUPP; in generic_coupler_attach()
5600 * regulator_register - register regulator
5601 * @dev: the device that drive the regulator
5602 * @regulator_desc: regulator to register
5603 * @cfg: runtime configuration for regulator
5605 * Called by regulator drivers to register a regulator.
5617 static atomic_t regulator_no = ATOMIC_INIT(-1); in regulator_register()
5625 return ERR_PTR(-EINVAL); in regulator_register()
5626 if (cfg->ena_gpiod) in regulator_register()
5629 ret = -EINVAL; in regulator_register()
5633 WARN_ON(!dev || !cfg->dev); in regulator_register()
5635 if (regulator_desc->name == NULL || regulator_desc->ops == NULL) { in regulator_register()
5636 ret = -EINVAL; in regulator_register()
5640 if (regulator_desc->type != REGULATOR_VOLTAGE && in regulator_register()
5641 regulator_desc->type != REGULATOR_CURRENT) { in regulator_register()
5642 ret = -EINVAL; in regulator_register()
5647 WARN_ON(regulator_desc->ops->get_voltage && in regulator_register()
5648 regulator_desc->ops->get_voltage_sel); in regulator_register()
5649 WARN_ON(regulator_desc->ops->set_voltage && in regulator_register()
5650 regulator_desc->ops->set_voltage_sel); in regulator_register()
5653 if (regulator_desc->ops->get_voltage_sel && in regulator_register()
5654 !regulator_desc->ops->list_voltage) { in regulator_register()
5655 ret = -EINVAL; in regulator_register()
5658 if (regulator_desc->ops->set_voltage_sel && in regulator_register()
5659 !regulator_desc->ops->list_voltage) { in regulator_register()
5660 ret = -EINVAL; in regulator_register()
5666 ret = -ENOMEM; in regulator_register()
5669 device_initialize(&rdev->dev); in regulator_register()
5670 dev_set_drvdata(&rdev->dev, rdev); in regulator_register()
5671 rdev->dev.class = ®ulator_class; in regulator_register()
5672 spin_lock_init(&rdev->err_lock); in regulator_register()
5680 ret = -ENOMEM; in regulator_register()
5685 &rdev->dev.of_node); in regulator_register()
5692 if (PTR_ERR(init_data) == -EPROBE_DEFER) { in regulator_register()
5693 ret = -EPROBE_DEFER; in regulator_register()
5705 if (!cfg->ena_gpiod && config->ena_gpiod) in regulator_register()
5708 init_data = config->init_data; in regulator_register()
5709 rdev->dev.of_node = of_node_get(config->of_node); in regulator_register()
5712 ww_mutex_init(&rdev->mutex, ®ulator_ww_class); in regulator_register()
5713 rdev->reg_data = config->driver_data; in regulator_register()
5714 rdev->owner = regulator_desc->owner; in regulator_register()
5715 rdev->desc = regulator_desc; in regulator_register()
5716 if (config->regmap) in regulator_register()
5717 rdev->regmap = config->regmap; in regulator_register()
5719 rdev->regmap = dev_get_regmap(dev, NULL); in regulator_register()
5720 else if (dev->parent) in regulator_register()
5721 rdev->regmap = dev_get_regmap(dev->parent, NULL); in regulator_register()
5722 INIT_LIST_HEAD(&rdev->consumer_list); in regulator_register()
5723 INIT_LIST_HEAD(&rdev->list); in regulator_register()
5724 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier); in regulator_register()
5725 INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work); in regulator_register()
5727 if (init_data && init_data->supply_regulator) in regulator_register()
5728 rdev->supply_name = init_data->supply_regulator; in regulator_register()
5729 else if (regulator_desc->supply_name) in regulator_register()
5730 rdev->supply_name = regulator_desc->supply_name; in regulator_register()
5733 rdev->dev.parent = config->dev; in regulator_register()
5734 dev_set_name(&rdev->dev, "regulator.%lu", in regulator_register()
5737 /* set regulator constraints */ in regulator_register()
5739 rdev->constraints = kmemdup(&init_data->constraints, in regulator_register()
5740 sizeof(*rdev->constraints), in regulator_register()
5743 rdev->constraints = kzalloc(sizeof(*rdev->constraints), in regulator_register()
5745 if (!rdev->constraints) { in regulator_register()
5746 ret = -ENOMEM; in regulator_register()
5750 if ((rdev->supply_name && !rdev->supply) && in regulator_register()
5751 (rdev->constraints->always_on || in regulator_register()
5752 rdev->constraints->boot_on)) { in regulator_register()
5761 /* perform any regulator specific init */ in regulator_register()
5762 if (init_data && init_data->regulator_init) { in regulator_register()
5763 ret = init_data->regulator_init(rdev->reg_data); in regulator_register()
5768 if (config->ena_gpiod) { in regulator_register()
5775 /* The regulator core took over the GPIO descriptor */ in regulator_register()
5781 if (ret == -EPROBE_DEFER && !resolved_early) { in regulator_register()
5782 /* Regulator might be in bypass mode and so needs its supply in regulator_register()
5785 /* FIXME: this currently triggers a chicken-and-egg problem in regulator_register()
5786 * when creating -SUPPLY symlink in sysfs to a regulator in regulator_register()
5790 rdev->supply_name); in regulator_register()
5807 for (i = 0; i < init_data->num_consumer_supplies; i++) { in regulator_register()
5809 init_data->consumer_supplies[i].dev_name, in regulator_register()
5810 init_data->consumer_supplies[i].supply); in regulator_register()
5813 init_data->consumer_supplies[i].supply); in regulator_register()
5819 if (!rdev->desc->ops->get_voltage && in regulator_register()
5820 !rdev->desc->ops->list_voltage && in regulator_register()
5821 !rdev->desc->fixed_uV) in regulator_register()
5822 rdev->is_switch = true; in regulator_register()
5824 ret = device_add(&rdev->dev); in regulator_register()
5847 regulator_put(rdev->supply); in regulator_register()
5848 kfree(rdev->coupling_desc.coupled_rdevs); in regulator_register()
5854 gpiod_put(config->ena_gpiod); in regulator_register()
5856 put_device(&rdev->dev); in regulator_register()
5859 gpiod_put(cfg->ena_gpiod); in regulator_register()
5865 * regulator_unregister - unregister regulator
5866 * @rdev: regulator to unregister
5868 * Called by regulator drivers to unregister a regulator.
5875 if (rdev->supply) { in regulator_unregister()
5876 while (rdev->use_count--) in regulator_unregister()
5877 regulator_disable(rdev->supply); in regulator_unregister()
5878 regulator_put(rdev->supply); in regulator_unregister()
5881 flush_work(&rdev->disable_work.work); in regulator_unregister()
5885 WARN_ON(rdev->open_count); in regulator_unregister()
5888 list_del(&rdev->list); in regulator_unregister()
5890 device_unregister(&rdev->dev); in regulator_unregister()
5898 * regulator_suspend - prepare regulators for system wide suspend
5901 * Configure each regulator with it's suspend operating parameters for state.
5935 if (!rdev->desc->ops->resume) in regulator_resume()
5940 if (rstate->enabled == ENABLE_IN_SUSPEND || in regulator_resume()
5941 rstate->enabled == DISABLE_IN_SUSPEND) in regulator_resume()
5942 ret = rdev->desc->ops->resume(rdev); in regulator_resume()
5963 .name = "regulator",
5971 * regulator_has_full_constraints - the system has fully specified constraints
5973 * Calling this function will cause the regulator API to disable all
5988 * rdev_get_drvdata - get rdev regulator driver data
5989 * @rdev: regulator
5991 * Get rdev regulator driver private data. This call can be used in the
5992 * regulator driver context.
5994 * Return: Pointer to regulator driver private data.
5998 return rdev->reg_data; in rdev_get_drvdata()
6003 * regulator_get_drvdata - get regulator driver data
6004 * @regulator: regulator
6006 * Get regulator driver private data. This call can be used in the consumer
6007 * driver context when non API regulator specific functions need to be called.
6009 * Return: Pointer to regulator driver private data.
6011 void *regulator_get_drvdata(struct regulator *regulator) in regulator_get_drvdata() argument
6013 return regulator->rdev->reg_data; in regulator_get_drvdata()
6018 * regulator_set_drvdata - set regulator driver data
6019 * @regulator: regulator
6022 void regulator_set_drvdata(struct regulator *regulator, void *data) in regulator_set_drvdata() argument
6024 regulator->rdev->reg_data = data; in regulator_set_drvdata()
6029 * rdev_get_id - get regulator ID
6030 * @rdev: regulator
6032 * Return: Regulator ID for @rdev.
6036 return rdev->desc->id; in rdev_get_id()
6042 return &rdev->dev; in rdev_get_dev()
6048 return rdev->regmap; in rdev_get_regmap()
6054 return reg_init_data->driver_data; in regulator_get_init_drvdata()
6064 seq_printf(sf, "%s -> %s.%s\n", in supply_map_show()
6065 rdev_get_name(map->regulator), map->dev_name, in supply_map_show()
6066 map->supply); in supply_map_show()
6088 if (rdev->supply && rdev->supply->rdev == summary_data->parent) in regulator_summary_show_children()
6089 regulator_summary_show_subtree(summary_data->s, rdev, in regulator_summary_show_children()
6090 summary_data->level + 1); in regulator_summary_show_children()
6100 struct regulator *consumer; in regulator_summary_show_subtree()
6108 seq_printf(s, "%*s%-*s %3d %4d %6d %7s ", in regulator_summary_show_subtree()
6110 30 - level * 3, rdev_get_name(rdev), in regulator_summary_show_subtree()
6111 rdev->use_count, rdev->open_count, rdev->bypass_count, in regulator_summary_show_subtree()
6118 c = rdev->constraints; in regulator_summary_show_subtree()
6120 switch (rdev->desc->type) { in regulator_summary_show_subtree()
6123 c->min_uV / 1000, c->max_uV / 1000); in regulator_summary_show_subtree()
6127 c->min_uA / 1000, c->max_uA / 1000); in regulator_summary_show_subtree()
6134 list_for_each_entry(consumer, &rdev->consumer_list, list) { in regulator_summary_show_subtree()
6135 if (consumer->dev && consumer->dev->class == ®ulator_class) in regulator_summary_show_subtree()
6138 seq_printf(s, "%*s%-*s ", in regulator_summary_show_subtree()
6140 30 - (level + 1) * 3, in regulator_summary_show_subtree()
6141 consumer->supply_name ? consumer->supply_name : in regulator_summary_show_subtree()
6142 consumer->dev ? dev_name(consumer->dev) : "deviceless"); in regulator_summary_show_subtree()
6144 switch (rdev->desc->type) { in regulator_summary_show_subtree()
6147 consumer->enable_count, in regulator_summary_show_subtree()
6148 consumer->uA_load / 1000, in regulator_summary_show_subtree()
6149 consumer->uA_load && !consumer->enable_count ? in regulator_summary_show_subtree()
6151 consumer->voltage[PM_SUSPEND_ON].min_uV / 1000, in regulator_summary_show_subtree()
6152 consumer->voltage[PM_SUSPEND_ON].max_uV / 1000); in regulator_summary_show_subtree()
6181 if (rdev != *lock_data->old_contended_rdev) { in regulator_summary_lock_one()
6182 ret = regulator_lock_nested(rdev, lock_data->ww_ctx); in regulator_summary_lock_one()
6184 if (ret == -EDEADLK) in regulator_summary_lock_one()
6185 *lock_data->new_contended_rdev = rdev; in regulator_summary_lock_one()
6189 *lock_data->old_contended_rdev = NULL; in regulator_summary_lock_one()
6201 if (rdev == *lock_data->new_contended_rdev) in regulator_summary_unlock_one()
6202 return -EDEADLK; in regulator_summary_unlock_one()
6242 ww_mutex_lock_slow(&new_contended_rdev->mutex, ww_ctx); in regulator_summary_lock()
6244 old_contended_rdev->ref_cnt++; in regulator_summary_lock()
6245 old_contended_rdev->mutex_owner = current; in regulator_summary_lock()
6255 } while (err == -EDEADLK); in regulator_summary_lock()
6274 if (!rdev->supply) in regulator_summary_show_roots()
6284 …seq_puts(s, " regulator use open bypass opmode voltage current min m… in regulator_summary_show()
6285 …seq_puts(s, "-------------------------------------------------------------------------------------… in regulator_summary_show()
6305 debugfs_root = debugfs_create_dir("regulator", NULL); in regulator_init()
6307 pr_debug("regulator: Failed to create debugfs directory\n"); in regulator_init()
6323 /* init early to allow our consumers to complete system booting */
6329 struct regulation_constraints *c = rdev->constraints; in regulator_late_cleanup()
6332 if (c && c->always_on) in regulator_late_cleanup()
6340 if (rdev->use_count) in regulator_late_cleanup()
6395 pr_warn("regulator: Not disabling unused regulators\n"); in regulator_init_complete_work_function()