Linux Audio

Check our new training course

Loading...
v6.2
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2//
  3// helpers.c  --  Voltage/Current Regulator framework helper functions.
  4//
  5// Copyright 2007, 2008 Wolfson Microelectronics PLC.
  6// Copyright 2008 SlimLogic Ltd.
 
 
 
 
 
 
  7
  8#include <linux/kernel.h>
  9#include <linux/err.h>
 10#include <linux/delay.h>
 11#include <linux/regmap.h>
 12#include <linux/regulator/consumer.h>
 13#include <linux/regulator/driver.h>
 14#include <linux/module.h>
 15
 16#include "internal.h"
 17
 18/**
 19 * regulator_is_enabled_regmap - standard is_enabled() for regmap users
 20 *
 21 * @rdev: regulator to operate on
 22 *
 23 * Regulators that use regmap for their register I/O can set the
 24 * enable_reg and enable_mask fields in their descriptor and then use
 25 * this as their is_enabled operation, saving some code.
 26 */
 27int regulator_is_enabled_regmap(struct regulator_dev *rdev)
 28{
 29	unsigned int val;
 30	int ret;
 31
 32	ret = regmap_read(rdev->regmap, rdev->desc->enable_reg, &val);
 33	if (ret != 0)
 34		return ret;
 35
 36	val &= rdev->desc->enable_mask;
 37
 38	if (rdev->desc->enable_is_inverted) {
 39		if (rdev->desc->enable_val)
 40			return val != rdev->desc->enable_val;
 41		return val == 0;
 42	} else {
 43		if (rdev->desc->enable_val)
 44			return val == rdev->desc->enable_val;
 45		return val != 0;
 46	}
 47}
 48EXPORT_SYMBOL_GPL(regulator_is_enabled_regmap);
 49
 50/**
 51 * regulator_enable_regmap - standard enable() for regmap users
 52 *
 53 * @rdev: regulator to operate on
 54 *
 55 * Regulators that use regmap for their register I/O can set the
 56 * enable_reg and enable_mask fields in their descriptor and then use
 57 * this as their enable() operation, saving some code.
 58 */
 59int regulator_enable_regmap(struct regulator_dev *rdev)
 60{
 61	unsigned int val;
 62
 63	if (rdev->desc->enable_is_inverted) {
 64		val = rdev->desc->disable_val;
 65	} else {
 66		val = rdev->desc->enable_val;
 67		if (!val)
 68			val = rdev->desc->enable_mask;
 69	}
 70
 71	return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
 72				  rdev->desc->enable_mask, val);
 73}
 74EXPORT_SYMBOL_GPL(regulator_enable_regmap);
 75
 76/**
 77 * regulator_disable_regmap - standard disable() for regmap users
 78 *
 79 * @rdev: regulator to operate on
 80 *
 81 * Regulators that use regmap for their register I/O can set the
 82 * enable_reg and enable_mask fields in their descriptor and then use
 83 * this as their disable() operation, saving some code.
 84 */
 85int regulator_disable_regmap(struct regulator_dev *rdev)
 86{
 87	unsigned int val;
 88
 89	if (rdev->desc->enable_is_inverted) {
 90		val = rdev->desc->enable_val;
 91		if (!val)
 92			val = rdev->desc->enable_mask;
 93	} else {
 94		val = rdev->desc->disable_val;
 95	}
 96
 97	return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
 98				  rdev->desc->enable_mask, val);
 99}
100EXPORT_SYMBOL_GPL(regulator_disable_regmap);
101
102static int regulator_range_selector_to_index(struct regulator_dev *rdev,
103					     unsigned int rval)
104{
105	int i;
106
107	if (!rdev->desc->linear_range_selectors)
108		return -EINVAL;
109
110	rval &= rdev->desc->vsel_range_mask;
111
112	for (i = 0; i < rdev->desc->n_linear_ranges; i++) {
113		if (rdev->desc->linear_range_selectors[i] == rval)
114			return i;
115	}
116	return -EINVAL;
117}
118
119/**
120 * regulator_get_voltage_sel_pickable_regmap - pickable range get_voltage_sel
121 *
122 * @rdev: regulator to operate on
123 *
124 * Regulators that use regmap for their register I/O and use pickable
125 * ranges can set the vsel_reg, vsel_mask, vsel_range_reg and vsel_range_mask
126 * fields in their descriptor and then use this as their get_voltage_vsel
127 * operation, saving some code.
128 */
129int regulator_get_voltage_sel_pickable_regmap(struct regulator_dev *rdev)
130{
131	unsigned int r_val;
132	int range;
133	unsigned int val;
134	int ret;
135	unsigned int voltages = 0;
136	const struct linear_range *r = rdev->desc->linear_ranges;
137
138	if (!r)
139		return -EINVAL;
140
141	ret = regmap_read(rdev->regmap, rdev->desc->vsel_reg, &val);
142	if (ret != 0)
143		return ret;
144
145	ret = regmap_read(rdev->regmap, rdev->desc->vsel_range_reg, &r_val);
146	if (ret != 0)
147		return ret;
148
149	val &= rdev->desc->vsel_mask;
150	val >>= ffs(rdev->desc->vsel_mask) - 1;
151
152	range = regulator_range_selector_to_index(rdev, r_val);
153	if (range < 0)
154		return -EINVAL;
155
156	voltages = linear_range_values_in_range_array(r, range);
157
158	return val + voltages;
159}
160EXPORT_SYMBOL_GPL(regulator_get_voltage_sel_pickable_regmap);
161
162/**
163 * regulator_set_voltage_sel_pickable_regmap - pickable range set_voltage_sel
164 *
165 * @rdev: regulator to operate on
166 * @sel: Selector to set
167 *
168 * Regulators that use regmap for their register I/O and use pickable
169 * ranges can set the vsel_reg, vsel_mask, vsel_range_reg and vsel_range_mask
170 * fields in their descriptor and then use this as their set_voltage_vsel
171 * operation, saving some code.
172 */
173int regulator_set_voltage_sel_pickable_regmap(struct regulator_dev *rdev,
174					      unsigned int sel)
175{
176	unsigned int range;
177	int ret, i;
178	unsigned int voltages_in_range = 0;
179
180	for (i = 0; i < rdev->desc->n_linear_ranges; i++) {
181		const struct linear_range *r;
182
183		r = &rdev->desc->linear_ranges[i];
184		voltages_in_range = linear_range_values_in_range(r);
185
186		if (sel < voltages_in_range)
187			break;
188		sel -= voltages_in_range;
189	}
190
191	if (i == rdev->desc->n_linear_ranges)
192		return -EINVAL;
193
194	sel <<= ffs(rdev->desc->vsel_mask) - 1;
195	sel += rdev->desc->linear_ranges[i].min_sel;
196
197	range = rdev->desc->linear_range_selectors[i];
198
199	if (rdev->desc->vsel_reg == rdev->desc->vsel_range_reg) {
200		ret = regmap_update_bits(rdev->regmap,
201					 rdev->desc->vsel_reg,
202					 rdev->desc->vsel_range_mask |
203					 rdev->desc->vsel_mask, sel | range);
204	} else {
205		ret = regmap_update_bits(rdev->regmap,
206					 rdev->desc->vsel_range_reg,
207					 rdev->desc->vsel_range_mask, range);
208		if (ret)
209			return ret;
210
211		ret = regmap_update_bits(rdev->regmap, rdev->desc->vsel_reg,
212				  rdev->desc->vsel_mask, sel);
213	}
214
215	if (ret)
216		return ret;
217
218	if (rdev->desc->apply_bit)
219		ret = regmap_update_bits(rdev->regmap, rdev->desc->apply_reg,
220					 rdev->desc->apply_bit,
221					 rdev->desc->apply_bit);
222	return ret;
223}
224EXPORT_SYMBOL_GPL(regulator_set_voltage_sel_pickable_regmap);
225
226/**
227 * regulator_get_voltage_sel_regmap - standard get_voltage_sel for regmap users
228 *
229 * @rdev: regulator to operate on
230 *
231 * Regulators that use regmap for their register I/O can set the
232 * vsel_reg and vsel_mask fields in their descriptor and then use this
233 * as their get_voltage_vsel operation, saving some code.
234 */
235int regulator_get_voltage_sel_regmap(struct regulator_dev *rdev)
236{
237	unsigned int val;
238	int ret;
239
240	ret = regmap_read(rdev->regmap, rdev->desc->vsel_reg, &val);
241	if (ret != 0)
242		return ret;
243
244	val &= rdev->desc->vsel_mask;
245	val >>= ffs(rdev->desc->vsel_mask) - 1;
246
247	return val;
248}
249EXPORT_SYMBOL_GPL(regulator_get_voltage_sel_regmap);
250
251/**
252 * regulator_set_voltage_sel_regmap - standard set_voltage_sel for regmap users
253 *
254 * @rdev: regulator to operate on
255 * @sel: Selector to set
256 *
257 * Regulators that use regmap for their register I/O can set the
258 * vsel_reg and vsel_mask fields in their descriptor and then use this
259 * as their set_voltage_vsel operation, saving some code.
260 */
261int regulator_set_voltage_sel_regmap(struct regulator_dev *rdev, unsigned sel)
262{
263	int ret;
264
265	sel <<= ffs(rdev->desc->vsel_mask) - 1;
266
267	ret = regmap_update_bits(rdev->regmap, rdev->desc->vsel_reg,
268				  rdev->desc->vsel_mask, sel);
269	if (ret)
270		return ret;
271
272	if (rdev->desc->apply_bit)
273		ret = regmap_update_bits(rdev->regmap, rdev->desc->apply_reg,
274					 rdev->desc->apply_bit,
275					 rdev->desc->apply_bit);
276	return ret;
277}
278EXPORT_SYMBOL_GPL(regulator_set_voltage_sel_regmap);
279
280/**
281 * regulator_map_voltage_iterate - map_voltage() based on list_voltage()
282 *
283 * @rdev: Regulator to operate on
284 * @min_uV: Lower bound for voltage
285 * @max_uV: Upper bound for voltage
286 *
287 * Drivers implementing set_voltage_sel() and list_voltage() can use
288 * this as their map_voltage() operation.  It will find a suitable
289 * voltage by calling list_voltage() until it gets something in bounds
290 * for the requested voltages.
291 */
292int regulator_map_voltage_iterate(struct regulator_dev *rdev,
293				  int min_uV, int max_uV)
294{
295	int best_val = INT_MAX;
296	int selector = 0;
297	int i, ret;
298
299	/* Find the smallest voltage that falls within the specified
300	 * range.
301	 */
302	for (i = 0; i < rdev->desc->n_voltages; i++) {
303		ret = rdev->desc->ops->list_voltage(rdev, i);
304		if (ret < 0)
305			continue;
306
307		if (ret < best_val && ret >= min_uV && ret <= max_uV) {
308			best_val = ret;
309			selector = i;
310		}
311	}
312
313	if (best_val != INT_MAX)
314		return selector;
315	else
316		return -EINVAL;
317}
318EXPORT_SYMBOL_GPL(regulator_map_voltage_iterate);
319
320/**
321 * regulator_map_voltage_ascend - map_voltage() for ascendant voltage list
322 *
323 * @rdev: Regulator to operate on
324 * @min_uV: Lower bound for voltage
325 * @max_uV: Upper bound for voltage
326 *
327 * Drivers that have ascendant voltage list can use this as their
328 * map_voltage() operation.
329 */
330int regulator_map_voltage_ascend(struct regulator_dev *rdev,
331				 int min_uV, int max_uV)
332{
333	int i, ret;
334
335	for (i = 0; i < rdev->desc->n_voltages; i++) {
336		ret = rdev->desc->ops->list_voltage(rdev, i);
337		if (ret < 0)
338			continue;
339
340		if (ret > max_uV)
341			break;
342
343		if (ret >= min_uV && ret <= max_uV)
344			return i;
345	}
346
347	return -EINVAL;
348}
349EXPORT_SYMBOL_GPL(regulator_map_voltage_ascend);
350
351/**
352 * regulator_map_voltage_linear - map_voltage() for simple linear mappings
353 *
354 * @rdev: Regulator to operate on
355 * @min_uV: Lower bound for voltage
356 * @max_uV: Upper bound for voltage
357 *
358 * Drivers providing min_uV and uV_step in their regulator_desc can
359 * use this as their map_voltage() operation.
360 */
361int regulator_map_voltage_linear(struct regulator_dev *rdev,
362				 int min_uV, int max_uV)
363{
364	int ret, voltage;
365
366	/* Allow uV_step to be 0 for fixed voltage */
367	if (rdev->desc->n_voltages == 1 && rdev->desc->uV_step == 0) {
368		if (min_uV <= rdev->desc->min_uV && rdev->desc->min_uV <= max_uV)
369			return 0;
370		else
371			return -EINVAL;
372	}
373
374	if (!rdev->desc->uV_step) {
375		BUG_ON(!rdev->desc->uV_step);
376		return -EINVAL;
377	}
378
379	if (min_uV < rdev->desc->min_uV)
380		min_uV = rdev->desc->min_uV;
381
382	ret = DIV_ROUND_UP(min_uV - rdev->desc->min_uV, rdev->desc->uV_step);
383	if (ret < 0)
384		return ret;
385
386	ret += rdev->desc->linear_min_sel;
387
388	/* Map back into a voltage to verify we're still in bounds */
389	voltage = rdev->desc->ops->list_voltage(rdev, ret);
390	if (voltage < min_uV || voltage > max_uV)
391		return -EINVAL;
392
393	return ret;
394}
395EXPORT_SYMBOL_GPL(regulator_map_voltage_linear);
396
397/**
398 * regulator_map_voltage_linear_range - map_voltage() for multiple linear ranges
399 *
400 * @rdev: Regulator to operate on
401 * @min_uV: Lower bound for voltage
402 * @max_uV: Upper bound for voltage
403 *
404 * Drivers providing linear_ranges in their descriptor can use this as
405 * their map_voltage() callback.
406 */
407int regulator_map_voltage_linear_range(struct regulator_dev *rdev,
408				       int min_uV, int max_uV)
409{
410	const struct linear_range *range;
411	int ret = -EINVAL;
412	unsigned int sel;
413	bool found;
414	int voltage, i;
415
416	if (!rdev->desc->n_linear_ranges) {
417		BUG_ON(!rdev->desc->n_linear_ranges);
418		return -EINVAL;
419	}
420
421	for (i = 0; i < rdev->desc->n_linear_ranges; i++) {
422		range = &rdev->desc->linear_ranges[i];
423
424		ret = linear_range_get_selector_high(range, min_uV, &sel,
425						     &found);
426		if (ret)
427			continue;
428		ret = sel;
429
430		/*
431		 * Map back into a voltage to verify we're still in bounds.
432		 * If we are not, then continue checking rest of the ranges.
433		 */
434		voltage = rdev->desc->ops->list_voltage(rdev, sel);
435		if (voltage >= min_uV && voltage <= max_uV)
436			break;
437	}
438
439	if (i == rdev->desc->n_linear_ranges)
440		return -EINVAL;
441
442	return ret;
443}
444EXPORT_SYMBOL_GPL(regulator_map_voltage_linear_range);
445
446/**
447 * regulator_map_voltage_pickable_linear_range - map_voltage, pickable ranges
448 *
449 * @rdev: Regulator to operate on
450 * @min_uV: Lower bound for voltage
451 * @max_uV: Upper bound for voltage
452 *
453 * Drivers providing pickable linear_ranges in their descriptor can use
454 * this as their map_voltage() callback.
455 */
456int regulator_map_voltage_pickable_linear_range(struct regulator_dev *rdev,
457						int min_uV, int max_uV)
458{
459	const struct linear_range *range;
460	int ret = -EINVAL;
461	int voltage, i;
462	unsigned int selector = 0;
463
464	if (!rdev->desc->n_linear_ranges) {
465		BUG_ON(!rdev->desc->n_linear_ranges);
466		return -EINVAL;
467	}
468
469	for (i = 0; i < rdev->desc->n_linear_ranges; i++) {
470		int linear_max_uV;
471		bool found;
472		unsigned int sel;
473
474		range = &rdev->desc->linear_ranges[i];
475		linear_max_uV = linear_range_get_max_value(range);
 
476
477		if (!(min_uV <= linear_max_uV && max_uV >= range->min)) {
478			selector += linear_range_values_in_range(range);
479			continue;
480		}
481
482		ret = linear_range_get_selector_high(range, min_uV, &sel,
483						     &found);
484		if (ret) {
485			selector += linear_range_values_in_range(range);
486			continue;
487		}
488
489		ret = selector + sel - range->min_sel;
 
 
 
 
 
 
 
 
490
491		voltage = rdev->desc->ops->list_voltage(rdev, ret);
492
493		/*
494		 * Map back into a voltage to verify we're still in bounds.
495		 * We may have overlapping voltage ranges. Hence we don't
496		 * exit but retry until we have checked all ranges.
497		 */
498		if (voltage < min_uV || voltage > max_uV)
499			selector += linear_range_values_in_range(range);
500		else
501			break;
502	}
503
504	if (i == rdev->desc->n_linear_ranges)
505		return -EINVAL;
506
507	return ret;
508}
509EXPORT_SYMBOL_GPL(regulator_map_voltage_pickable_linear_range);
510
511/**
512 * regulator_desc_list_voltage_linear - List voltages with simple calculation
513 *
514 * @desc: Regulator desc for regulator which volatges are to be listed
515 * @selector: Selector to convert into a voltage
516 *
517 * Regulators with a simple linear mapping between voltages and
518 * selectors can set min_uV and uV_step in the regulator descriptor
519 * and then use this function prior regulator registration to list
520 * the voltages. This is useful when voltages need to be listed during
521 * device-tree parsing.
522 */
523int regulator_desc_list_voltage_linear(const struct regulator_desc *desc,
524				       unsigned int selector)
525{
526	if (selector >= desc->n_voltages)
527		return -EINVAL;
528
529	if (selector < desc->linear_min_sel)
530		return 0;
531
532	selector -= desc->linear_min_sel;
533
534	return desc->min_uV + (desc->uV_step * selector);
535}
536EXPORT_SYMBOL_GPL(regulator_desc_list_voltage_linear);
537
538/**
539 * regulator_list_voltage_linear - List voltages with simple calculation
540 *
541 * @rdev: Regulator device
542 * @selector: Selector to convert into a voltage
543 *
544 * Regulators with a simple linear mapping between voltages and
545 * selectors can set min_uV and uV_step in the regulator descriptor
546 * and then use this function as their list_voltage() operation,
547 */
548int regulator_list_voltage_linear(struct regulator_dev *rdev,
549				  unsigned int selector)
550{
551	return regulator_desc_list_voltage_linear(rdev->desc, selector);
 
 
 
 
 
 
 
552}
553EXPORT_SYMBOL_GPL(regulator_list_voltage_linear);
554
555/**
556 * regulator_list_voltage_pickable_linear_range - pickable range list voltages
557 *
558 * @rdev: Regulator device
559 * @selector: Selector to convert into a voltage
560 *
561 * list_voltage() operation, intended to be used by drivers utilizing pickable
562 * ranges helpers.
 
563 */
564int regulator_list_voltage_pickable_linear_range(struct regulator_dev *rdev,
565						 unsigned int selector)
566{
567	const struct linear_range *range;
568	int i;
569	unsigned int all_sels = 0;
570
571	if (!rdev->desc->n_linear_ranges) {
572		BUG_ON(!rdev->desc->n_linear_ranges);
573		return -EINVAL;
574	}
575
576	for (i = 0; i < rdev->desc->n_linear_ranges; i++) {
577		unsigned int sel_indexes;
578
579		range = &rdev->desc->linear_ranges[i];
580
581		sel_indexes = linear_range_values_in_range(range) - 1;
 
 
582
583		if (all_sels + sel_indexes >= selector) {
584			selector -= all_sels;
585			/*
586			 * As we see here, pickable ranges work only as
587			 * long as the first selector for each pickable
588			 * range is 0, and the each subsequent range for
589			 * this 'pick' follow immediately at next unused
590			 * selector (Eg. there is no gaps between ranges).
591			 * I think this is fine but it probably should be
592			 * documented. OTOH, whole pickable range stuff
593			 * might benefit from some documentation
594			 */
595			return range->min + (range->step * selector);
596		}
597
598		all_sels += (sel_indexes + 1);
599	}
600
601	return -EINVAL;
602}
603EXPORT_SYMBOL_GPL(regulator_list_voltage_pickable_linear_range);
604
605/**
606 * regulator_desc_list_voltage_linear_range - List voltages for linear ranges
607 *
608 * @desc: Regulator desc for regulator which volatges are to be listed
609 * @selector: Selector to convert into a voltage
610 *
611 * Regulators with a series of simple linear mappings between voltages
612 * and selectors who have set linear_ranges in the regulator descriptor
613 * can use this function prior regulator registration to list voltages.
614 * This is useful when voltages need to be listed during device-tree
615 * parsing.
616 */
617int regulator_desc_list_voltage_linear_range(const struct regulator_desc *desc,
618					     unsigned int selector)
619{
620	unsigned int val;
621	int ret;
622
623	BUG_ON(!desc->n_linear_ranges);
624
625	ret = linear_range_get_value_array(desc->linear_ranges,
626					   desc->n_linear_ranges, selector,
627					   &val);
628	if (ret)
629		return ret;
630
631	return val;
632}
633EXPORT_SYMBOL_GPL(regulator_desc_list_voltage_linear_range);
634
635/**
636 * regulator_list_voltage_linear_range - List voltages for linear ranges
637 *
638 * @rdev: Regulator device
639 * @selector: Selector to convert into a voltage
640 *
641 * Regulators with a series of simple linear mappings between voltages
642 * and selectors can set linear_ranges in the regulator descriptor and
643 * then use this function as their list_voltage() operation,
644 */
645int regulator_list_voltage_linear_range(struct regulator_dev *rdev,
646					unsigned int selector)
647{
648	return regulator_desc_list_voltage_linear_range(rdev->desc, selector);
649}
650EXPORT_SYMBOL_GPL(regulator_list_voltage_linear_range);
651
652/**
653 * regulator_list_voltage_table - List voltages with table based mapping
654 *
655 * @rdev: Regulator device
656 * @selector: Selector to convert into a voltage
657 *
658 * Regulators with table based mapping between voltages and
659 * selectors can set volt_table in the regulator descriptor
660 * and then use this function as their list_voltage() operation.
661 */
662int regulator_list_voltage_table(struct regulator_dev *rdev,
663				 unsigned int selector)
664{
665	if (!rdev->desc->volt_table) {
666		BUG_ON(!rdev->desc->volt_table);
667		return -EINVAL;
668	}
669
670	if (selector >= rdev->desc->n_voltages)
671		return -EINVAL;
672	if (selector < rdev->desc->linear_min_sel)
673		return 0;
674
675	return rdev->desc->volt_table[selector];
676}
677EXPORT_SYMBOL_GPL(regulator_list_voltage_table);
678
679/**
680 * regulator_set_bypass_regmap - Default set_bypass() using regmap
681 *
682 * @rdev: device to operate on.
683 * @enable: state to set.
684 */
685int regulator_set_bypass_regmap(struct regulator_dev *rdev, bool enable)
686{
687	unsigned int val;
688
689	if (enable) {
690		val = rdev->desc->bypass_val_on;
691		if (!val)
692			val = rdev->desc->bypass_mask;
693	} else {
694		val = rdev->desc->bypass_val_off;
695	}
696
697	return regmap_update_bits(rdev->regmap, rdev->desc->bypass_reg,
698				  rdev->desc->bypass_mask, val);
699}
700EXPORT_SYMBOL_GPL(regulator_set_bypass_regmap);
701
702/**
703 * regulator_set_soft_start_regmap - Default set_soft_start() using regmap
704 *
705 * @rdev: device to operate on.
706 */
707int regulator_set_soft_start_regmap(struct regulator_dev *rdev)
708{
709	unsigned int val;
710
711	val = rdev->desc->soft_start_val_on;
712	if (!val)
713		val = rdev->desc->soft_start_mask;
714
715	return regmap_update_bits(rdev->regmap, rdev->desc->soft_start_reg,
716				  rdev->desc->soft_start_mask, val);
717}
718EXPORT_SYMBOL_GPL(regulator_set_soft_start_regmap);
719
720/**
721 * regulator_set_pull_down_regmap - Default set_pull_down() using regmap
722 *
723 * @rdev: device to operate on.
724 */
725int regulator_set_pull_down_regmap(struct regulator_dev *rdev)
726{
727	unsigned int val;
728
729	val = rdev->desc->pull_down_val_on;
730	if (!val)
731		val = rdev->desc->pull_down_mask;
732
733	return regmap_update_bits(rdev->regmap, rdev->desc->pull_down_reg,
734				  rdev->desc->pull_down_mask, val);
735}
736EXPORT_SYMBOL_GPL(regulator_set_pull_down_regmap);
737
738/**
739 * regulator_get_bypass_regmap - Default get_bypass() using regmap
740 *
741 * @rdev: device to operate on.
742 * @enable: current state.
743 */
744int regulator_get_bypass_regmap(struct regulator_dev *rdev, bool *enable)
745{
746	unsigned int val;
747	unsigned int val_on = rdev->desc->bypass_val_on;
748	int ret;
749
750	ret = regmap_read(rdev->regmap, rdev->desc->bypass_reg, &val);
751	if (ret != 0)
752		return ret;
753
754	if (!val_on)
755		val_on = rdev->desc->bypass_mask;
756
757	*enable = (val & rdev->desc->bypass_mask) == val_on;
758
759	return 0;
760}
761EXPORT_SYMBOL_GPL(regulator_get_bypass_regmap);
762
763/**
764 * regulator_set_active_discharge_regmap - Default set_active_discharge()
765 *					   using regmap
766 *
767 * @rdev: device to operate on.
768 * @enable: state to set, 0 to disable and 1 to enable.
769 */
770int regulator_set_active_discharge_regmap(struct regulator_dev *rdev,
771					  bool enable)
772{
773	unsigned int val;
774
775	if (enable)
776		val = rdev->desc->active_discharge_on;
777	else
778		val = rdev->desc->active_discharge_off;
779
780	return regmap_update_bits(rdev->regmap,
781				  rdev->desc->active_discharge_reg,
782				  rdev->desc->active_discharge_mask, val);
783}
784EXPORT_SYMBOL_GPL(regulator_set_active_discharge_regmap);
785
786/**
787 * regulator_set_current_limit_regmap - set_current_limit for regmap users
788 *
789 * @rdev: regulator to operate on
790 * @min_uA: Lower bound for current limit
791 * @max_uA: Upper bound for current limit
792 *
793 * Regulators that use regmap for their register I/O can set curr_table,
794 * csel_reg and csel_mask fields in their descriptor and then use this
795 * as their set_current_limit operation, saving some code.
796 */
797int regulator_set_current_limit_regmap(struct regulator_dev *rdev,
798				       int min_uA, int max_uA)
799{
800	unsigned int n_currents = rdev->desc->n_current_limits;
801	int i, sel = -1;
802
803	if (n_currents == 0)
804		return -EINVAL;
805
806	if (rdev->desc->curr_table) {
807		const unsigned int *curr_table = rdev->desc->curr_table;
808		bool ascend = curr_table[n_currents - 1] > curr_table[0];
809
810		/* search for closest to maximum */
811		if (ascend) {
812			for (i = n_currents - 1; i >= 0; i--) {
813				if (min_uA <= curr_table[i] &&
814				    curr_table[i] <= max_uA) {
815					sel = i;
816					break;
817				}
818			}
819		} else {
820			for (i = 0; i < n_currents; i++) {
821				if (min_uA <= curr_table[i] &&
822				    curr_table[i] <= max_uA) {
823					sel = i;
824					break;
825				}
826			}
827		}
828	}
829
830	if (sel < 0)
831		return -EINVAL;
832
833	sel <<= ffs(rdev->desc->csel_mask) - 1;
834
835	return regmap_update_bits(rdev->regmap, rdev->desc->csel_reg,
836				  rdev->desc->csel_mask, sel);
837}
838EXPORT_SYMBOL_GPL(regulator_set_current_limit_regmap);
839
840/**
841 * regulator_get_current_limit_regmap - get_current_limit for regmap users
842 *
843 * @rdev: regulator to operate on
844 *
845 * Regulators that use regmap for their register I/O can set the
846 * csel_reg and csel_mask fields in their descriptor and then use this
847 * as their get_current_limit operation, saving some code.
848 */
849int regulator_get_current_limit_regmap(struct regulator_dev *rdev)
850{
851	unsigned int val;
852	int ret;
853
854	ret = regmap_read(rdev->regmap, rdev->desc->csel_reg, &val);
855	if (ret != 0)
856		return ret;
857
858	val &= rdev->desc->csel_mask;
859	val >>= ffs(rdev->desc->csel_mask) - 1;
860
861	if (rdev->desc->curr_table) {
862		if (val >= rdev->desc->n_current_limits)
863			return -EINVAL;
864
865		return rdev->desc->curr_table[val];
866	}
867
868	return -EINVAL;
869}
870EXPORT_SYMBOL_GPL(regulator_get_current_limit_regmap);
871
872/**
873 * regulator_bulk_set_supply_names - initialize the 'supply' fields in an array
874 *                                   of regulator_bulk_data structs
875 *
876 * @consumers: array of regulator_bulk_data entries to initialize
877 * @supply_names: array of supply name strings
878 * @num_supplies: number of supply names to initialize
879 *
880 * Note: the 'consumers' array must be the size of 'num_supplies'.
881 */
882void regulator_bulk_set_supply_names(struct regulator_bulk_data *consumers,
883				     const char *const *supply_names,
884				     unsigned int num_supplies)
885{
886	unsigned int i;
887
888	for (i = 0; i < num_supplies; i++)
889		consumers[i].supply = supply_names[i];
890}
891EXPORT_SYMBOL_GPL(regulator_bulk_set_supply_names);
892
893/**
894 * regulator_is_equal - test whether two regulators are the same
895 *
896 * @reg1: first regulator to operate on
897 * @reg2: second regulator to operate on
898 */
899bool regulator_is_equal(struct regulator *reg1, struct regulator *reg2)
900{
901	return reg1->rdev == reg2->rdev;
902}
903EXPORT_SYMBOL_GPL(regulator_is_equal);
904
905static int find_closest_bigger(unsigned int target, const unsigned int *table,
906			       unsigned int num_sel, unsigned int *sel)
907{
908	unsigned int s, tmp, max, maxsel = 0;
909	bool found = false;
910
911	max = table[0];
912
913	for (s = 0; s < num_sel; s++) {
914		if (table[s] > max) {
915			max = table[s];
916			maxsel = s;
917		}
918		if (table[s] >= target) {
919			if (!found || table[s] - target < tmp - target) {
920				tmp = table[s];
921				*sel = s;
922				found = true;
923				if (tmp == target)
924					break;
925			}
926		}
927	}
928
929	if (!found) {
930		*sel = maxsel;
931		return -EINVAL;
932	}
933
934	return 0;
935}
936
937/**
938 * regulator_set_ramp_delay_regmap - set_ramp_delay() helper
939 *
940 * @rdev: regulator to operate on
941 *
942 * Regulators that use regmap for their register I/O can set the ramp_reg
943 * and ramp_mask fields in their descriptor and then use this as their
944 * set_ramp_delay operation, saving some code.
945 */
946int regulator_set_ramp_delay_regmap(struct regulator_dev *rdev, int ramp_delay)
947{
948	int ret;
949	unsigned int sel;
950
951	if (WARN_ON(!rdev->desc->n_ramp_values || !rdev->desc->ramp_delay_table))
952		return -EINVAL;
953
954	ret = find_closest_bigger(ramp_delay, rdev->desc->ramp_delay_table,
955				  rdev->desc->n_ramp_values, &sel);
956
957	if (ret) {
958		dev_warn(rdev_get_dev(rdev),
959			 "Can't set ramp-delay %u, setting %u\n", ramp_delay,
960			 rdev->desc->ramp_delay_table[sel]);
961	}
962
963	sel <<= ffs(rdev->desc->ramp_mask) - 1;
964
965	return regmap_update_bits(rdev->regmap, rdev->desc->ramp_reg,
966				  rdev->desc->ramp_mask, sel);
967}
968EXPORT_SYMBOL_GPL(regulator_set_ramp_delay_regmap);
v4.17
  1/*
  2 * helpers.c  --  Voltage/Current Regulator framework helper functions.
  3 *
  4 * Copyright 2007, 2008 Wolfson Microelectronics PLC.
  5 * Copyright 2008 SlimLogic Ltd.
  6 *
  7 *  This program is free software; you can redistribute  it and/or modify it
  8 *  under  the terms of  the GNU General  Public License as published by the
  9 *  Free Software Foundation;  either version 2 of the  License, or (at your
 10 *  option) any later version.
 11 *
 12 */
 13
 14#include <linux/kernel.h>
 15#include <linux/err.h>
 16#include <linux/delay.h>
 17#include <linux/regmap.h>
 18#include <linux/regulator/consumer.h>
 19#include <linux/regulator/driver.h>
 20#include <linux/module.h>
 21
 
 
 22/**
 23 * regulator_is_enabled_regmap - standard is_enabled() for regmap users
 24 *
 25 * @rdev: regulator to operate on
 26 *
 27 * Regulators that use regmap for their register I/O can set the
 28 * enable_reg and enable_mask fields in their descriptor and then use
 29 * this as their is_enabled operation, saving some code.
 30 */
 31int regulator_is_enabled_regmap(struct regulator_dev *rdev)
 32{
 33	unsigned int val;
 34	int ret;
 35
 36	ret = regmap_read(rdev->regmap, rdev->desc->enable_reg, &val);
 37	if (ret != 0)
 38		return ret;
 39
 40	val &= rdev->desc->enable_mask;
 41
 42	if (rdev->desc->enable_is_inverted) {
 43		if (rdev->desc->enable_val)
 44			return val != rdev->desc->enable_val;
 45		return val == 0;
 46	} else {
 47		if (rdev->desc->enable_val)
 48			return val == rdev->desc->enable_val;
 49		return val != 0;
 50	}
 51}
 52EXPORT_SYMBOL_GPL(regulator_is_enabled_regmap);
 53
 54/**
 55 * regulator_enable_regmap - standard enable() for regmap users
 56 *
 57 * @rdev: regulator to operate on
 58 *
 59 * Regulators that use regmap for their register I/O can set the
 60 * enable_reg and enable_mask fields in their descriptor and then use
 61 * this as their enable() operation, saving some code.
 62 */
 63int regulator_enable_regmap(struct regulator_dev *rdev)
 64{
 65	unsigned int val;
 66
 67	if (rdev->desc->enable_is_inverted) {
 68		val = rdev->desc->disable_val;
 69	} else {
 70		val = rdev->desc->enable_val;
 71		if (!val)
 72			val = rdev->desc->enable_mask;
 73	}
 74
 75	return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
 76				  rdev->desc->enable_mask, val);
 77}
 78EXPORT_SYMBOL_GPL(regulator_enable_regmap);
 79
 80/**
 81 * regulator_disable_regmap - standard disable() for regmap users
 82 *
 83 * @rdev: regulator to operate on
 84 *
 85 * Regulators that use regmap for their register I/O can set the
 86 * enable_reg and enable_mask fields in their descriptor and then use
 87 * this as their disable() operation, saving some code.
 88 */
 89int regulator_disable_regmap(struct regulator_dev *rdev)
 90{
 91	unsigned int val;
 92
 93	if (rdev->desc->enable_is_inverted) {
 94		val = rdev->desc->enable_val;
 95		if (!val)
 96			val = rdev->desc->enable_mask;
 97	} else {
 98		val = rdev->desc->disable_val;
 99	}
100
101	return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
102				  rdev->desc->enable_mask, val);
103}
104EXPORT_SYMBOL_GPL(regulator_disable_regmap);
105
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
106/**
107 * regulator_get_voltage_sel_regmap - standard get_voltage_sel for regmap users
108 *
109 * @rdev: regulator to operate on
110 *
111 * Regulators that use regmap for their register I/O can set the
112 * vsel_reg and vsel_mask fields in their descriptor and then use this
113 * as their get_voltage_vsel operation, saving some code.
114 */
115int regulator_get_voltage_sel_regmap(struct regulator_dev *rdev)
116{
117	unsigned int val;
118	int ret;
119
120	ret = regmap_read(rdev->regmap, rdev->desc->vsel_reg, &val);
121	if (ret != 0)
122		return ret;
123
124	val &= rdev->desc->vsel_mask;
125	val >>= ffs(rdev->desc->vsel_mask) - 1;
126
127	return val;
128}
129EXPORT_SYMBOL_GPL(regulator_get_voltage_sel_regmap);
130
131/**
132 * regulator_set_voltage_sel_regmap - standard set_voltage_sel for regmap users
133 *
134 * @rdev: regulator to operate on
135 * @sel: Selector to set
136 *
137 * Regulators that use regmap for their register I/O can set the
138 * vsel_reg and vsel_mask fields in their descriptor and then use this
139 * as their set_voltage_vsel operation, saving some code.
140 */
141int regulator_set_voltage_sel_regmap(struct regulator_dev *rdev, unsigned sel)
142{
143	int ret;
144
145	sel <<= ffs(rdev->desc->vsel_mask) - 1;
146
147	ret = regmap_update_bits(rdev->regmap, rdev->desc->vsel_reg,
148				  rdev->desc->vsel_mask, sel);
149	if (ret)
150		return ret;
151
152	if (rdev->desc->apply_bit)
153		ret = regmap_update_bits(rdev->regmap, rdev->desc->apply_reg,
154					 rdev->desc->apply_bit,
155					 rdev->desc->apply_bit);
156	return ret;
157}
158EXPORT_SYMBOL_GPL(regulator_set_voltage_sel_regmap);
159
160/**
161 * regulator_map_voltage_iterate - map_voltage() based on list_voltage()
162 *
163 * @rdev: Regulator to operate on
164 * @min_uV: Lower bound for voltage
165 * @max_uV: Upper bound for voltage
166 *
167 * Drivers implementing set_voltage_sel() and list_voltage() can use
168 * this as their map_voltage() operation.  It will find a suitable
169 * voltage by calling list_voltage() until it gets something in bounds
170 * for the requested voltages.
171 */
172int regulator_map_voltage_iterate(struct regulator_dev *rdev,
173				  int min_uV, int max_uV)
174{
175	int best_val = INT_MAX;
176	int selector = 0;
177	int i, ret;
178
179	/* Find the smallest voltage that falls within the specified
180	 * range.
181	 */
182	for (i = 0; i < rdev->desc->n_voltages; i++) {
183		ret = rdev->desc->ops->list_voltage(rdev, i);
184		if (ret < 0)
185			continue;
186
187		if (ret < best_val && ret >= min_uV && ret <= max_uV) {
188			best_val = ret;
189			selector = i;
190		}
191	}
192
193	if (best_val != INT_MAX)
194		return selector;
195	else
196		return -EINVAL;
197}
198EXPORT_SYMBOL_GPL(regulator_map_voltage_iterate);
199
200/**
201 * regulator_map_voltage_ascend - map_voltage() for ascendant voltage list
202 *
203 * @rdev: Regulator to operate on
204 * @min_uV: Lower bound for voltage
205 * @max_uV: Upper bound for voltage
206 *
207 * Drivers that have ascendant voltage list can use this as their
208 * map_voltage() operation.
209 */
210int regulator_map_voltage_ascend(struct regulator_dev *rdev,
211				 int min_uV, int max_uV)
212{
213	int i, ret;
214
215	for (i = 0; i < rdev->desc->n_voltages; i++) {
216		ret = rdev->desc->ops->list_voltage(rdev, i);
217		if (ret < 0)
218			continue;
219
220		if (ret > max_uV)
221			break;
222
223		if (ret >= min_uV && ret <= max_uV)
224			return i;
225	}
226
227	return -EINVAL;
228}
229EXPORT_SYMBOL_GPL(regulator_map_voltage_ascend);
230
231/**
232 * regulator_map_voltage_linear - map_voltage() for simple linear mappings
233 *
234 * @rdev: Regulator to operate on
235 * @min_uV: Lower bound for voltage
236 * @max_uV: Upper bound for voltage
237 *
238 * Drivers providing min_uV and uV_step in their regulator_desc can
239 * use this as their map_voltage() operation.
240 */
241int regulator_map_voltage_linear(struct regulator_dev *rdev,
242				 int min_uV, int max_uV)
243{
244	int ret, voltage;
245
246	/* Allow uV_step to be 0 for fixed voltage */
247	if (rdev->desc->n_voltages == 1 && rdev->desc->uV_step == 0) {
248		if (min_uV <= rdev->desc->min_uV && rdev->desc->min_uV <= max_uV)
249			return 0;
250		else
251			return -EINVAL;
252	}
253
254	if (!rdev->desc->uV_step) {
255		BUG_ON(!rdev->desc->uV_step);
256		return -EINVAL;
257	}
258
259	if (min_uV < rdev->desc->min_uV)
260		min_uV = rdev->desc->min_uV;
261
262	ret = DIV_ROUND_UP(min_uV - rdev->desc->min_uV, rdev->desc->uV_step);
263	if (ret < 0)
264		return ret;
265
266	ret += rdev->desc->linear_min_sel;
267
268	/* Map back into a voltage to verify we're still in bounds */
269	voltage = rdev->desc->ops->list_voltage(rdev, ret);
270	if (voltage < min_uV || voltage > max_uV)
271		return -EINVAL;
272
273	return ret;
274}
275EXPORT_SYMBOL_GPL(regulator_map_voltage_linear);
276
277/**
278 * regulator_map_voltage_linear_range - map_voltage() for multiple linear ranges
279 *
280 * @rdev: Regulator to operate on
281 * @min_uV: Lower bound for voltage
282 * @max_uV: Upper bound for voltage
283 *
284 * Drivers providing linear_ranges in their descriptor can use this as
285 * their map_voltage() callback.
286 */
287int regulator_map_voltage_linear_range(struct regulator_dev *rdev,
288				       int min_uV, int max_uV)
289{
290	const struct regulator_linear_range *range;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
291	int ret = -EINVAL;
292	int voltage, i;
 
293
294	if (!rdev->desc->n_linear_ranges) {
295		BUG_ON(!rdev->desc->n_linear_ranges);
296		return -EINVAL;
297	}
298
299	for (i = 0; i < rdev->desc->n_linear_ranges; i++) {
300		int linear_max_uV;
 
 
301
302		range = &rdev->desc->linear_ranges[i];
303		linear_max_uV = range->min_uV +
304			(range->max_sel - range->min_sel) * range->uV_step;
305
306		if (!(min_uV <= linear_max_uV && max_uV >= range->min_uV))
 
307			continue;
 
308
309		if (min_uV <= range->min_uV)
310			min_uV = range->min_uV;
 
 
 
 
311
312		/* range->uV_step == 0 means fixed voltage range */
313		if (range->uV_step == 0) {
314			ret = 0;
315		} else {
316			ret = DIV_ROUND_UP(min_uV - range->min_uV,
317					   range->uV_step);
318			if (ret < 0)
319				return ret;
320		}
321
322		ret += range->min_sel;
323
324		break;
 
 
 
 
 
 
 
 
325	}
326
327	if (i == rdev->desc->n_linear_ranges)
328		return -EINVAL;
329
330	/* Map back into a voltage to verify we're still in bounds */
331	voltage = rdev->desc->ops->list_voltage(rdev, ret);
332	if (voltage < min_uV || voltage > max_uV)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
333		return -EINVAL;
334
335	return ret;
 
 
 
 
 
336}
337EXPORT_SYMBOL_GPL(regulator_map_voltage_linear_range);
338
339/**
340 * regulator_list_voltage_linear - List voltages with simple calculation
341 *
342 * @rdev: Regulator device
343 * @selector: Selector to convert into a voltage
344 *
345 * Regulators with a simple linear mapping between voltages and
346 * selectors can set min_uV and uV_step in the regulator descriptor
347 * and then use this function as their list_voltage() operation,
348 */
349int regulator_list_voltage_linear(struct regulator_dev *rdev,
350				  unsigned int selector)
351{
352	if (selector >= rdev->desc->n_voltages)
353		return -EINVAL;
354	if (selector < rdev->desc->linear_min_sel)
355		return 0;
356
357	selector -= rdev->desc->linear_min_sel;
358
359	return rdev->desc->min_uV + (rdev->desc->uV_step * selector);
360}
361EXPORT_SYMBOL_GPL(regulator_list_voltage_linear);
362
363/**
364 * regulator_list_voltage_linear_range - List voltages for linear ranges
365 *
366 * @rdev: Regulator device
367 * @selector: Selector to convert into a voltage
368 *
369 * Regulators with a series of simple linear mappings between voltages
370 * and selectors can set linear_ranges in the regulator descriptor and
371 * then use this function as their list_voltage() operation,
372 */
373int regulator_list_voltage_linear_range(struct regulator_dev *rdev,
374					unsigned int selector)
375{
376	const struct regulator_linear_range *range;
377	int i;
 
378
379	if (!rdev->desc->n_linear_ranges) {
380		BUG_ON(!rdev->desc->n_linear_ranges);
381		return -EINVAL;
382	}
383
384	for (i = 0; i < rdev->desc->n_linear_ranges; i++) {
 
 
385		range = &rdev->desc->linear_ranges[i];
386
387		if (!(selector >= range->min_sel &&
388		      selector <= range->max_sel))
389			continue;
390
391		selector -= range->min_sel;
 
 
 
 
 
 
 
 
 
 
 
 
 
392
393		return range->min_uV + (range->uV_step * selector);
394	}
395
396	return -EINVAL;
397}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
398EXPORT_SYMBOL_GPL(regulator_list_voltage_linear_range);
399
400/**
401 * regulator_list_voltage_table - List voltages with table based mapping
402 *
403 * @rdev: Regulator device
404 * @selector: Selector to convert into a voltage
405 *
406 * Regulators with table based mapping between voltages and
407 * selectors can set volt_table in the regulator descriptor
408 * and then use this function as their list_voltage() operation.
409 */
410int regulator_list_voltage_table(struct regulator_dev *rdev,
411				 unsigned int selector)
412{
413	if (!rdev->desc->volt_table) {
414		BUG_ON(!rdev->desc->volt_table);
415		return -EINVAL;
416	}
417
418	if (selector >= rdev->desc->n_voltages)
419		return -EINVAL;
 
 
420
421	return rdev->desc->volt_table[selector];
422}
423EXPORT_SYMBOL_GPL(regulator_list_voltage_table);
424
425/**
426 * regulator_set_bypass_regmap - Default set_bypass() using regmap
427 *
428 * @rdev: device to operate on.
429 * @enable: state to set.
430 */
431int regulator_set_bypass_regmap(struct regulator_dev *rdev, bool enable)
432{
433	unsigned int val;
434
435	if (enable) {
436		val = rdev->desc->bypass_val_on;
437		if (!val)
438			val = rdev->desc->bypass_mask;
439	} else {
440		val = rdev->desc->bypass_val_off;
441	}
442
443	return regmap_update_bits(rdev->regmap, rdev->desc->bypass_reg,
444				  rdev->desc->bypass_mask, val);
445}
446EXPORT_SYMBOL_GPL(regulator_set_bypass_regmap);
447
448/**
449 * regulator_set_soft_start_regmap - Default set_soft_start() using regmap
450 *
451 * @rdev: device to operate on.
452 */
453int regulator_set_soft_start_regmap(struct regulator_dev *rdev)
454{
455	unsigned int val;
456
457	val = rdev->desc->soft_start_val_on;
458	if (!val)
459		val = rdev->desc->soft_start_mask;
460
461	return regmap_update_bits(rdev->regmap, rdev->desc->soft_start_reg,
462				  rdev->desc->soft_start_mask, val);
463}
464EXPORT_SYMBOL_GPL(regulator_set_soft_start_regmap);
465
466/**
467 * regulator_set_pull_down_regmap - Default set_pull_down() using regmap
468 *
469 * @rdev: device to operate on.
470 */
471int regulator_set_pull_down_regmap(struct regulator_dev *rdev)
472{
473	unsigned int val;
474
475	val = rdev->desc->pull_down_val_on;
476	if (!val)
477		val = rdev->desc->pull_down_mask;
478
479	return regmap_update_bits(rdev->regmap, rdev->desc->pull_down_reg,
480				  rdev->desc->pull_down_mask, val);
481}
482EXPORT_SYMBOL_GPL(regulator_set_pull_down_regmap);
483
484/**
485 * regulator_get_bypass_regmap - Default get_bypass() using regmap
486 *
487 * @rdev: device to operate on.
488 * @enable: current state.
489 */
490int regulator_get_bypass_regmap(struct regulator_dev *rdev, bool *enable)
491{
492	unsigned int val;
493	unsigned int val_on = rdev->desc->bypass_val_on;
494	int ret;
495
496	ret = regmap_read(rdev->regmap, rdev->desc->bypass_reg, &val);
497	if (ret != 0)
498		return ret;
499
500	if (!val_on)
501		val_on = rdev->desc->bypass_mask;
502
503	*enable = (val & rdev->desc->bypass_mask) == val_on;
504
505	return 0;
506}
507EXPORT_SYMBOL_GPL(regulator_get_bypass_regmap);
508
509/**
510 * regulator_set_active_discharge_regmap - Default set_active_discharge()
511 *					   using regmap
512 *
513 * @rdev: device to operate on.
514 * @enable: state to set, 0 to disable and 1 to enable.
515 */
516int regulator_set_active_discharge_regmap(struct regulator_dev *rdev,
517					  bool enable)
518{
519	unsigned int val;
520
521	if (enable)
522		val = rdev->desc->active_discharge_on;
523	else
524		val = rdev->desc->active_discharge_off;
525
526	return regmap_update_bits(rdev->regmap,
527				  rdev->desc->active_discharge_reg,
528				  rdev->desc->active_discharge_mask, val);
529}
530EXPORT_SYMBOL_GPL(regulator_set_active_discharge_regmap);