• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * core.c  --  Voltage/Current Regulator framework.
3  *
4  * Copyright 2007, 2008 Wolfson Microelectronics PLC.
5  * Copyright 2008 SlimLogic Ltd.
6  *
7  * Author: Liam Girdwood <lrg@slimlogic.co.uk>
8  *
9  *  This program is free software; you can redistribute  it and/or modify it
10  *  under  the terms of  the GNU General  Public License as published by the
11  *  Free Software Foundation;  either version 2 of the  License, or (at your
12  *  option) any later version.
13  *
14  */
15 
16 #include <linux/kernel.h>
17 #include <linux/init.h>
18 #include <linux/debugfs.h>
19 #include <linux/device.h>
20 #include <linux/slab.h>
21 #include <linux/async.h>
22 #include <linux/err.h>
23 #include <linux/mutex.h>
24 #include <linux/suspend.h>
25 #include <linux/delay.h>
26 #include <linux/gpio.h>
27 #include <linux/of.h>
28 #include <linux/regmap.h>
29 #include <linux/regulator/of_regulator.h>
30 #include <linux/regulator/consumer.h>
31 #include <linux/regulator/driver.h>
32 #include <linux/regulator/machine.h>
33 #include <linux/module.h>
34 
35 #define CREATE_TRACE_POINTS
36 #include <trace/events/regulator.h>
37 
38 #include "dummy.h"
39 
40 #define rdev_crit(rdev, fmt, ...)					\
41 	pr_crit("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
42 #define rdev_err(rdev, fmt, ...)					\
43 	pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
44 #define rdev_warn(rdev, fmt, ...)					\
45 	pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
46 #define rdev_info(rdev, fmt, ...)					\
47 	pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
48 #define rdev_dbg(rdev, fmt, ...)					\
49 	pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
50 
51 static DEFINE_MUTEX(regulator_list_mutex);
52 static LIST_HEAD(regulator_list);
53 static LIST_HEAD(regulator_map_list);
54 static LIST_HEAD(regulator_ena_gpio_list);
55 static bool has_full_constraints;
56 static bool board_wants_dummy_regulator;
57 
58 static struct dentry *debugfs_root;
59 
60 /*
61  * struct regulator_map
62  *
63  * Used to provide symbolic supply names to devices.
64  */
65 struct regulator_map {
66 	struct list_head list;
67 	const char *dev_name;   /* The dev_name() for the consumer */
68 	const char *supply;
69 	struct regulator_dev *regulator;
70 };
71 
72 /*
73  * struct regulator_enable_gpio
74  *
75  * Management for shared enable GPIO pin
76  */
77 struct regulator_enable_gpio {
78 	struct list_head list;
79 	int gpio;
80 	u32 enable_count;	/* a number of enabled shared GPIO */
81 	u32 request_count;	/* a number of requested shared GPIO */
82 	unsigned int ena_gpio_invert:1;
83 };
84 
85 /*
86  * struct regulator
87  *
88  * One for each consumer device.
89  */
90 struct regulator {
91 	struct device *dev;
92 	struct list_head list;
93 	unsigned int always_on:1;
94 	unsigned int bypass:1;
95 	int uA_load;
96 	int min_uV;
97 	int max_uV;
98 	char *supply_name;
99 	struct device_attribute dev_attr;
100 	struct regulator_dev *rdev;
101 	struct dentry *debugfs;
102 };
103 
104 static int _regulator_is_enabled(struct regulator_dev *rdev);
105 static int _regulator_disable(struct regulator_dev *rdev);
106 static int _regulator_get_voltage(struct regulator_dev *rdev);
107 static int _regulator_get_current_limit(struct regulator_dev *rdev);
108 static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
109 static void _notifier_call_chain(struct regulator_dev *rdev,
110 				  unsigned long event, void *data);
111 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
112 				     int min_uV, int max_uV);
113 static struct regulator *create_regulator(struct regulator_dev *rdev,
114 					  struct device *dev,
115 					  const char *supply_name);
116 
rdev_get_name(struct regulator_dev * rdev)117 static const char *rdev_get_name(struct regulator_dev *rdev)
118 {
119 	if (rdev->constraints && rdev->constraints->name)
120 		return rdev->constraints->name;
121 	else if (rdev->desc->name)
122 		return rdev->desc->name;
123 	else
124 		return "";
125 }
126 
127 /**
128  * of_get_regulator - get a regulator device node based on supply name
129  * @dev: Device pointer for the consumer (of regulator) device
130  * @supply: regulator supply name
131  *
132  * Extract the regulator device node corresponding to the supply name.
133  * returns the device node corresponding to the regulator if found, else
134  * returns NULL.
135  */
of_get_regulator(struct device * dev,const char * supply)136 static struct device_node *of_get_regulator(struct device *dev, const char *supply)
137 {
138 	struct device_node *regnode = NULL;
139 	char prop_name[32]; /* 32 is max size of property name */
140 
141 	dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
142 
143 	snprintf(prop_name, 32, "%s-supply", supply);
144 	regnode = of_parse_phandle(dev->of_node, prop_name, 0);
145 
146 	if (!regnode) {
147 		dev_dbg(dev, "Looking up %s property in node %s failed",
148 				prop_name, dev->of_node->full_name);
149 		return NULL;
150 	}
151 	return regnode;
152 }
153 
_regulator_can_change_status(struct regulator_dev * rdev)154 static int _regulator_can_change_status(struct regulator_dev *rdev)
155 {
156 	if (!rdev->constraints)
157 		return 0;
158 
159 	if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
160 		return 1;
161 	else
162 		return 0;
163 }
164 
165 /* Platform voltage constraint check */
regulator_check_voltage(struct regulator_dev * rdev,int * min_uV,int * max_uV)166 static int regulator_check_voltage(struct regulator_dev *rdev,
167 				   int *min_uV, int *max_uV)
168 {
169 	BUG_ON(*min_uV > *max_uV);
170 
171 	if (!rdev->constraints) {
172 		rdev_err(rdev, "no constraints\n");
173 		return -ENODEV;
174 	}
175 	if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
176 		rdev_err(rdev, "operation not allowed\n");
177 		return -EPERM;
178 	}
179 
180 	if (*max_uV > rdev->constraints->max_uV)
181 		*max_uV = rdev->constraints->max_uV;
182 	if (*min_uV < rdev->constraints->min_uV)
183 		*min_uV = rdev->constraints->min_uV;
184 
185 	if (*min_uV > *max_uV) {
186 		rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
187 			 *min_uV, *max_uV);
188 		return -EINVAL;
189 	}
190 
191 	return 0;
192 }
193 
194 /* Make sure we select a voltage that suits the needs of all
195  * regulator consumers
196  */
regulator_check_consumers(struct regulator_dev * rdev,int * min_uV,int * max_uV)197 static int regulator_check_consumers(struct regulator_dev *rdev,
198 				     int *min_uV, int *max_uV)
199 {
200 	struct regulator *regulator;
201 
202 	list_for_each_entry(regulator, &rdev->consumer_list, list) {
203 		/*
204 		 * Assume consumers that didn't say anything are OK
205 		 * with anything in the constraint range.
206 		 */
207 		if (!regulator->min_uV && !regulator->max_uV)
208 			continue;
209 
210 		if (*max_uV > regulator->max_uV)
211 			*max_uV = regulator->max_uV;
212 		if (*min_uV < regulator->min_uV)
213 			*min_uV = regulator->min_uV;
214 	}
215 
216 	if (*min_uV > *max_uV) {
217 		rdev_err(rdev, "Restricting voltage, %u-%uuV\n",
218 			*min_uV, *max_uV);
219 		return -EINVAL;
220 	}
221 
222 	return 0;
223 }
224 
225 /* current constraint check */
regulator_check_current_limit(struct regulator_dev * rdev,int * min_uA,int * max_uA)226 static int regulator_check_current_limit(struct regulator_dev *rdev,
227 					int *min_uA, int *max_uA)
228 {
229 	BUG_ON(*min_uA > *max_uA);
230 
231 	if (!rdev->constraints) {
232 		rdev_err(rdev, "no constraints\n");
233 		return -ENODEV;
234 	}
235 	if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
236 		rdev_err(rdev, "operation not allowed\n");
237 		return -EPERM;
238 	}
239 
240 	if (*max_uA > rdev->constraints->max_uA)
241 		*max_uA = rdev->constraints->max_uA;
242 	if (*min_uA < rdev->constraints->min_uA)
243 		*min_uA = rdev->constraints->min_uA;
244 
245 	if (*min_uA > *max_uA) {
246 		rdev_err(rdev, "unsupportable current range: %d-%duA\n",
247 			 *min_uA, *max_uA);
248 		return -EINVAL;
249 	}
250 
251 	return 0;
252 }
253 
254 /* operating mode constraint check */
regulator_mode_constrain(struct regulator_dev * rdev,int * mode)255 static int regulator_mode_constrain(struct regulator_dev *rdev, int *mode)
256 {
257 	switch (*mode) {
258 	case REGULATOR_MODE_FAST:
259 	case REGULATOR_MODE_NORMAL:
260 	case REGULATOR_MODE_IDLE:
261 	case REGULATOR_MODE_STANDBY:
262 		break;
263 	default:
264 		rdev_err(rdev, "invalid mode %x specified\n", *mode);
265 		return -EINVAL;
266 	}
267 
268 	if (!rdev->constraints) {
269 		rdev_err(rdev, "no constraints\n");
270 		return -ENODEV;
271 	}
272 	if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
273 		rdev_err(rdev, "operation not allowed\n");
274 		return -EPERM;
275 	}
276 
277 	/* The modes are bitmasks, the most power hungry modes having
278 	 * the lowest values. If the requested mode isn't supported
279 	 * try higher modes. */
280 	while (*mode) {
281 		if (rdev->constraints->valid_modes_mask & *mode)
282 			return 0;
283 		*mode /= 2;
284 	}
285 
286 	return -EINVAL;
287 }
288 
289 /* dynamic regulator mode switching constraint check */
regulator_check_drms(struct regulator_dev * rdev)290 static int regulator_check_drms(struct regulator_dev *rdev)
291 {
292 	if (!rdev->constraints) {
293 		rdev_err(rdev, "no constraints\n");
294 		return -ENODEV;
295 	}
296 	if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
297 		rdev_err(rdev, "operation not allowed\n");
298 		return -EPERM;
299 	}
300 	return 0;
301 }
302 
regulator_uV_show(struct device * dev,struct device_attribute * attr,char * buf)303 static ssize_t regulator_uV_show(struct device *dev,
304 				struct device_attribute *attr, char *buf)
305 {
306 	struct regulator_dev *rdev = dev_get_drvdata(dev);
307 	ssize_t ret;
308 
309 	mutex_lock(&rdev->mutex);
310 	ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
311 	mutex_unlock(&rdev->mutex);
312 
313 	return ret;
314 }
315 static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
316 
regulator_uA_show(struct device * dev,struct device_attribute * attr,char * buf)317 static ssize_t regulator_uA_show(struct device *dev,
318 				struct device_attribute *attr, char *buf)
319 {
320 	struct regulator_dev *rdev = dev_get_drvdata(dev);
321 
322 	return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
323 }
324 static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
325 
regulator_name_show(struct device * dev,struct device_attribute * attr,char * buf)326 static ssize_t regulator_name_show(struct device *dev,
327 			     struct device_attribute *attr, char *buf)
328 {
329 	struct regulator_dev *rdev = dev_get_drvdata(dev);
330 
331 	return sprintf(buf, "%s\n", rdev_get_name(rdev));
332 }
333 
regulator_print_opmode(char * buf,int mode)334 static ssize_t regulator_print_opmode(char *buf, int mode)
335 {
336 	switch (mode) {
337 	case REGULATOR_MODE_FAST:
338 		return sprintf(buf, "fast\n");
339 	case REGULATOR_MODE_NORMAL:
340 		return sprintf(buf, "normal\n");
341 	case REGULATOR_MODE_IDLE:
342 		return sprintf(buf, "idle\n");
343 	case REGULATOR_MODE_STANDBY:
344 		return sprintf(buf, "standby\n");
345 	}
346 	return sprintf(buf, "unknown\n");
347 }
348 
regulator_opmode_show(struct device * dev,struct device_attribute * attr,char * buf)349 static ssize_t regulator_opmode_show(struct device *dev,
350 				    struct device_attribute *attr, char *buf)
351 {
352 	struct regulator_dev *rdev = dev_get_drvdata(dev);
353 
354 	return regulator_print_opmode(buf, _regulator_get_mode(rdev));
355 }
356 static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
357 
regulator_print_state(char * buf,int state)358 static ssize_t regulator_print_state(char *buf, int state)
359 {
360 	if (state > 0)
361 		return sprintf(buf, "enabled\n");
362 	else if (state == 0)
363 		return sprintf(buf, "disabled\n");
364 	else
365 		return sprintf(buf, "unknown\n");
366 }
367 
regulator_state_show(struct device * dev,struct device_attribute * attr,char * buf)368 static ssize_t regulator_state_show(struct device *dev,
369 				   struct device_attribute *attr, char *buf)
370 {
371 	struct regulator_dev *rdev = dev_get_drvdata(dev);
372 	ssize_t ret;
373 
374 	mutex_lock(&rdev->mutex);
375 	ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
376 	mutex_unlock(&rdev->mutex);
377 
378 	return ret;
379 }
380 static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
381 
regulator_status_show(struct device * dev,struct device_attribute * attr,char * buf)382 static ssize_t regulator_status_show(struct device *dev,
383 				   struct device_attribute *attr, char *buf)
384 {
385 	struct regulator_dev *rdev = dev_get_drvdata(dev);
386 	int status;
387 	char *label;
388 
389 	status = rdev->desc->ops->get_status(rdev);
390 	if (status < 0)
391 		return status;
392 
393 	switch (status) {
394 	case REGULATOR_STATUS_OFF:
395 		label = "off";
396 		break;
397 	case REGULATOR_STATUS_ON:
398 		label = "on";
399 		break;
400 	case REGULATOR_STATUS_ERROR:
401 		label = "error";
402 		break;
403 	case REGULATOR_STATUS_FAST:
404 		label = "fast";
405 		break;
406 	case REGULATOR_STATUS_NORMAL:
407 		label = "normal";
408 		break;
409 	case REGULATOR_STATUS_IDLE:
410 		label = "idle";
411 		break;
412 	case REGULATOR_STATUS_STANDBY:
413 		label = "standby";
414 		break;
415 	case REGULATOR_STATUS_BYPASS:
416 		label = "bypass";
417 		break;
418 	case REGULATOR_STATUS_UNDEFINED:
419 		label = "undefined";
420 		break;
421 	default:
422 		return -ERANGE;
423 	}
424 
425 	return sprintf(buf, "%s\n", label);
426 }
427 static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
428 
regulator_min_uA_show(struct device * dev,struct device_attribute * attr,char * buf)429 static ssize_t regulator_min_uA_show(struct device *dev,
430 				    struct device_attribute *attr, char *buf)
431 {
432 	struct regulator_dev *rdev = dev_get_drvdata(dev);
433 
434 	if (!rdev->constraints)
435 		return sprintf(buf, "constraint not defined\n");
436 
437 	return sprintf(buf, "%d\n", rdev->constraints->min_uA);
438 }
439 static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
440 
regulator_max_uA_show(struct device * dev,struct device_attribute * attr,char * buf)441 static ssize_t regulator_max_uA_show(struct device *dev,
442 				    struct device_attribute *attr, char *buf)
443 {
444 	struct regulator_dev *rdev = dev_get_drvdata(dev);
445 
446 	if (!rdev->constraints)
447 		return sprintf(buf, "constraint not defined\n");
448 
449 	return sprintf(buf, "%d\n", rdev->constraints->max_uA);
450 }
451 static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
452 
regulator_min_uV_show(struct device * dev,struct device_attribute * attr,char * buf)453 static ssize_t regulator_min_uV_show(struct device *dev,
454 				    struct device_attribute *attr, char *buf)
455 {
456 	struct regulator_dev *rdev = dev_get_drvdata(dev);
457 
458 	if (!rdev->constraints)
459 		return sprintf(buf, "constraint not defined\n");
460 
461 	return sprintf(buf, "%d\n", rdev->constraints->min_uV);
462 }
463 static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
464 
regulator_max_uV_show(struct device * dev,struct device_attribute * attr,char * buf)465 static ssize_t regulator_max_uV_show(struct device *dev,
466 				    struct device_attribute *attr, char *buf)
467 {
468 	struct regulator_dev *rdev = dev_get_drvdata(dev);
469 
470 	if (!rdev->constraints)
471 		return sprintf(buf, "constraint not defined\n");
472 
473 	return sprintf(buf, "%d\n", rdev->constraints->max_uV);
474 }
475 static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
476 
regulator_total_uA_show(struct device * dev,struct device_attribute * attr,char * buf)477 static ssize_t regulator_total_uA_show(struct device *dev,
478 				      struct device_attribute *attr, char *buf)
479 {
480 	struct regulator_dev *rdev = dev_get_drvdata(dev);
481 	struct regulator *regulator;
482 	int uA = 0;
483 
484 	mutex_lock(&rdev->mutex);
485 	list_for_each_entry(regulator, &rdev->consumer_list, list)
486 		uA += regulator->uA_load;
487 	mutex_unlock(&rdev->mutex);
488 	return sprintf(buf, "%d\n", uA);
489 }
490 static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
491 
regulator_num_users_show(struct device * dev,struct device_attribute * attr,char * buf)492 static ssize_t regulator_num_users_show(struct device *dev,
493 				      struct device_attribute *attr, char *buf)
494 {
495 	struct regulator_dev *rdev = dev_get_drvdata(dev);
496 	return sprintf(buf, "%d\n", rdev->use_count);
497 }
498 
regulator_type_show(struct device * dev,struct device_attribute * attr,char * buf)499 static ssize_t regulator_type_show(struct device *dev,
500 				  struct device_attribute *attr, char *buf)
501 {
502 	struct regulator_dev *rdev = dev_get_drvdata(dev);
503 
504 	switch (rdev->desc->type) {
505 	case REGULATOR_VOLTAGE:
506 		return sprintf(buf, "voltage\n");
507 	case REGULATOR_CURRENT:
508 		return sprintf(buf, "current\n");
509 	}
510 	return sprintf(buf, "unknown\n");
511 }
512 
regulator_suspend_mem_uV_show(struct device * dev,struct device_attribute * attr,char * buf)513 static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
514 				struct device_attribute *attr, char *buf)
515 {
516 	struct regulator_dev *rdev = dev_get_drvdata(dev);
517 
518 	return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
519 }
520 static DEVICE_ATTR(suspend_mem_microvolts, 0444,
521 		regulator_suspend_mem_uV_show, NULL);
522 
regulator_suspend_disk_uV_show(struct device * dev,struct device_attribute * attr,char * buf)523 static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
524 				struct device_attribute *attr, char *buf)
525 {
526 	struct regulator_dev *rdev = dev_get_drvdata(dev);
527 
528 	return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
529 }
530 static DEVICE_ATTR(suspend_disk_microvolts, 0444,
531 		regulator_suspend_disk_uV_show, NULL);
532 
regulator_suspend_standby_uV_show(struct device * dev,struct device_attribute * attr,char * buf)533 static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
534 				struct device_attribute *attr, char *buf)
535 {
536 	struct regulator_dev *rdev = dev_get_drvdata(dev);
537 
538 	return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
539 }
540 static DEVICE_ATTR(suspend_standby_microvolts, 0444,
541 		regulator_suspend_standby_uV_show, NULL);
542 
regulator_suspend_mem_mode_show(struct device * dev,struct device_attribute * attr,char * buf)543 static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
544 				struct device_attribute *attr, char *buf)
545 {
546 	struct regulator_dev *rdev = dev_get_drvdata(dev);
547 
548 	return regulator_print_opmode(buf,
549 		rdev->constraints->state_mem.mode);
550 }
551 static DEVICE_ATTR(suspend_mem_mode, 0444,
552 		regulator_suspend_mem_mode_show, NULL);
553 
regulator_suspend_disk_mode_show(struct device * dev,struct device_attribute * attr,char * buf)554 static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
555 				struct device_attribute *attr, char *buf)
556 {
557 	struct regulator_dev *rdev = dev_get_drvdata(dev);
558 
559 	return regulator_print_opmode(buf,
560 		rdev->constraints->state_disk.mode);
561 }
562 static DEVICE_ATTR(suspend_disk_mode, 0444,
563 		regulator_suspend_disk_mode_show, NULL);
564 
regulator_suspend_standby_mode_show(struct device * dev,struct device_attribute * attr,char * buf)565 static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
566 				struct device_attribute *attr, char *buf)
567 {
568 	struct regulator_dev *rdev = dev_get_drvdata(dev);
569 
570 	return regulator_print_opmode(buf,
571 		rdev->constraints->state_standby.mode);
572 }
573 static DEVICE_ATTR(suspend_standby_mode, 0444,
574 		regulator_suspend_standby_mode_show, NULL);
575 
regulator_suspend_mem_state_show(struct device * dev,struct device_attribute * attr,char * buf)576 static ssize_t regulator_suspend_mem_state_show(struct device *dev,
577 				   struct device_attribute *attr, char *buf)
578 {
579 	struct regulator_dev *rdev = dev_get_drvdata(dev);
580 
581 	return regulator_print_state(buf,
582 			rdev->constraints->state_mem.enabled);
583 }
584 static DEVICE_ATTR(suspend_mem_state, 0444,
585 		regulator_suspend_mem_state_show, NULL);
586 
regulator_suspend_disk_state_show(struct device * dev,struct device_attribute * attr,char * buf)587 static ssize_t regulator_suspend_disk_state_show(struct device *dev,
588 				   struct device_attribute *attr, char *buf)
589 {
590 	struct regulator_dev *rdev = dev_get_drvdata(dev);
591 
592 	return regulator_print_state(buf,
593 			rdev->constraints->state_disk.enabled);
594 }
595 static DEVICE_ATTR(suspend_disk_state, 0444,
596 		regulator_suspend_disk_state_show, NULL);
597 
regulator_suspend_standby_state_show(struct device * dev,struct device_attribute * attr,char * buf)598 static ssize_t regulator_suspend_standby_state_show(struct device *dev,
599 				   struct device_attribute *attr, char *buf)
600 {
601 	struct regulator_dev *rdev = dev_get_drvdata(dev);
602 
603 	return regulator_print_state(buf,
604 			rdev->constraints->state_standby.enabled);
605 }
606 static DEVICE_ATTR(suspend_standby_state, 0444,
607 		regulator_suspend_standby_state_show, NULL);
608 
regulator_bypass_show(struct device * dev,struct device_attribute * attr,char * buf)609 static ssize_t regulator_bypass_show(struct device *dev,
610 				     struct device_attribute *attr, char *buf)
611 {
612 	struct regulator_dev *rdev = dev_get_drvdata(dev);
613 	const char *report;
614 	bool bypass;
615 	int ret;
616 
617 	ret = rdev->desc->ops->get_bypass(rdev, &bypass);
618 
619 	if (ret != 0)
620 		report = "unknown";
621 	else if (bypass)
622 		report = "enabled";
623 	else
624 		report = "disabled";
625 
626 	return sprintf(buf, "%s\n", report);
627 }
628 static DEVICE_ATTR(bypass, 0444,
629 		   regulator_bypass_show, NULL);
630 
631 /*
632  * These are the only attributes are present for all regulators.
633  * Other attributes are a function of regulator functionality.
634  */
635 static struct device_attribute regulator_dev_attrs[] = {
636 	__ATTR(name, 0444, regulator_name_show, NULL),
637 	__ATTR(num_users, 0444, regulator_num_users_show, NULL),
638 	__ATTR(type, 0444, regulator_type_show, NULL),
639 	__ATTR_NULL,
640 };
641 
regulator_dev_release(struct device * dev)642 static void regulator_dev_release(struct device *dev)
643 {
644 	struct regulator_dev *rdev = dev_get_drvdata(dev);
645 	kfree(rdev);
646 }
647 
648 static struct class regulator_class = {
649 	.name = "regulator",
650 	.dev_release = regulator_dev_release,
651 	.dev_attrs = regulator_dev_attrs,
652 };
653 
654 /* Calculate the new optimum regulator operating mode based on the new total
655  * consumer load. All locks held by caller */
drms_uA_update(struct regulator_dev * rdev)656 static void drms_uA_update(struct regulator_dev *rdev)
657 {
658 	struct regulator *sibling;
659 	int current_uA = 0, output_uV, input_uV, err;
660 	unsigned int mode;
661 
662 	err = regulator_check_drms(rdev);
663 	if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
664 	    (!rdev->desc->ops->get_voltage &&
665 	     !rdev->desc->ops->get_voltage_sel) ||
666 	    !rdev->desc->ops->set_mode)
667 		return;
668 
669 	/* get output voltage */
670 	output_uV = _regulator_get_voltage(rdev);
671 	if (output_uV <= 0)
672 		return;
673 
674 	/* get input voltage */
675 	input_uV = 0;
676 	if (rdev->supply)
677 		input_uV = regulator_get_voltage(rdev->supply);
678 	if (input_uV <= 0)
679 		input_uV = rdev->constraints->input_uV;
680 	if (input_uV <= 0)
681 		return;
682 
683 	/* calc total requested load */
684 	list_for_each_entry(sibling, &rdev->consumer_list, list)
685 		current_uA += sibling->uA_load;
686 
687 	/* now get the optimum mode for our new total regulator load */
688 	mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
689 						  output_uV, current_uA);
690 
691 	/* check the new mode is allowed */
692 	err = regulator_mode_constrain(rdev, &mode);
693 	if (err == 0)
694 		rdev->desc->ops->set_mode(rdev, mode);
695 }
696 
suspend_set_state(struct regulator_dev * rdev,struct regulator_state * rstate)697 static int suspend_set_state(struct regulator_dev *rdev,
698 	struct regulator_state *rstate)
699 {
700 	int ret = 0;
701 
702 	/* If we have no suspend mode configration don't set anything;
703 	 * only warn if the driver implements set_suspend_voltage or
704 	 * set_suspend_mode callback.
705 	 */
706 	if (!rstate->enabled && !rstate->disabled) {
707 		if (rdev->desc->ops->set_suspend_voltage ||
708 		    rdev->desc->ops->set_suspend_mode)
709 			rdev_warn(rdev, "No configuration\n");
710 		return 0;
711 	}
712 
713 	if (rstate->enabled && rstate->disabled) {
714 		rdev_err(rdev, "invalid configuration\n");
715 		return -EINVAL;
716 	}
717 
718 	if (rstate->enabled && rdev->desc->ops->set_suspend_enable)
719 		ret = rdev->desc->ops->set_suspend_enable(rdev);
720 	else if (rstate->disabled && rdev->desc->ops->set_suspend_disable)
721 		ret = rdev->desc->ops->set_suspend_disable(rdev);
722 	else /* OK if set_suspend_enable or set_suspend_disable is NULL */
723 		ret = 0;
724 
725 	if (ret < 0) {
726 		rdev_err(rdev, "failed to enabled/disable\n");
727 		return ret;
728 	}
729 
730 	if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
731 		ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
732 		if (ret < 0) {
733 			rdev_err(rdev, "failed to set voltage\n");
734 			return ret;
735 		}
736 	}
737 
738 	if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
739 		ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
740 		if (ret < 0) {
741 			rdev_err(rdev, "failed to set mode\n");
742 			return ret;
743 		}
744 	}
745 	return ret;
746 }
747 
748 /* locks held by caller */
suspend_prepare(struct regulator_dev * rdev,suspend_state_t state)749 static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
750 {
751 	if (!rdev->constraints)
752 		return -EINVAL;
753 
754 	switch (state) {
755 	case PM_SUSPEND_STANDBY:
756 		return suspend_set_state(rdev,
757 			&rdev->constraints->state_standby);
758 	case PM_SUSPEND_MEM:
759 		return suspend_set_state(rdev,
760 			&rdev->constraints->state_mem);
761 	case PM_SUSPEND_MAX:
762 		return suspend_set_state(rdev,
763 			&rdev->constraints->state_disk);
764 	default:
765 		return -EINVAL;
766 	}
767 }
768 
print_constraints(struct regulator_dev * rdev)769 static void print_constraints(struct regulator_dev *rdev)
770 {
771 	struct regulation_constraints *constraints = rdev->constraints;
772 	char buf[80] = "";
773 	int count = 0;
774 	int ret;
775 
776 	if (constraints->min_uV && constraints->max_uV) {
777 		if (constraints->min_uV == constraints->max_uV)
778 			count += sprintf(buf + count, "%d mV ",
779 					 constraints->min_uV / 1000);
780 		else
781 			count += sprintf(buf + count, "%d <--> %d mV ",
782 					 constraints->min_uV / 1000,
783 					 constraints->max_uV / 1000);
784 	}
785 
786 	if (!constraints->min_uV ||
787 	    constraints->min_uV != constraints->max_uV) {
788 		ret = _regulator_get_voltage(rdev);
789 		if (ret > 0)
790 			count += sprintf(buf + count, "at %d mV ", ret / 1000);
791 	}
792 
793 	if (constraints->uV_offset)
794 		count += sprintf(buf, "%dmV offset ",
795 				 constraints->uV_offset / 1000);
796 
797 	if (constraints->min_uA && constraints->max_uA) {
798 		if (constraints->min_uA == constraints->max_uA)
799 			count += sprintf(buf + count, "%d mA ",
800 					 constraints->min_uA / 1000);
801 		else
802 			count += sprintf(buf + count, "%d <--> %d mA ",
803 					 constraints->min_uA / 1000,
804 					 constraints->max_uA / 1000);
805 	}
806 
807 	if (!constraints->min_uA ||
808 	    constraints->min_uA != constraints->max_uA) {
809 		ret = _regulator_get_current_limit(rdev);
810 		if (ret > 0)
811 			count += sprintf(buf + count, "at %d mA ", ret / 1000);
812 	}
813 
814 	if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
815 		count += sprintf(buf + count, "fast ");
816 	if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
817 		count += sprintf(buf + count, "normal ");
818 	if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
819 		count += sprintf(buf + count, "idle ");
820 	if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
821 		count += sprintf(buf + count, "standby");
822 
823 	if (!count)
824 		sprintf(buf, "no parameters");
825 
826 	rdev_info(rdev, "%s\n", buf);
827 
828 	if ((constraints->min_uV != constraints->max_uV) &&
829 	    !(constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE))
830 		rdev_warn(rdev,
831 			  "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
832 }
833 
machine_constraints_voltage(struct regulator_dev * rdev,struct regulation_constraints * constraints)834 static int machine_constraints_voltage(struct regulator_dev *rdev,
835 	struct regulation_constraints *constraints)
836 {
837 	struct regulator_ops *ops = rdev->desc->ops;
838 	int ret;
839 
840 	/* do we need to apply the constraint voltage */
841 	if (rdev->constraints->apply_uV &&
842 	    rdev->constraints->min_uV == rdev->constraints->max_uV) {
843 		ret = _regulator_do_set_voltage(rdev,
844 						rdev->constraints->min_uV,
845 						rdev->constraints->max_uV);
846 		if (ret < 0) {
847 			rdev_err(rdev, "failed to apply %duV constraint\n",
848 				 rdev->constraints->min_uV);
849 			return ret;
850 		}
851 	}
852 
853 	/* constrain machine-level voltage specs to fit
854 	 * the actual range supported by this regulator.
855 	 */
856 	if (ops->list_voltage && rdev->desc->n_voltages) {
857 		int	count = rdev->desc->n_voltages;
858 		int	i;
859 		int	min_uV = INT_MAX;
860 		int	max_uV = INT_MIN;
861 		int	cmin = constraints->min_uV;
862 		int	cmax = constraints->max_uV;
863 
864 		/* it's safe to autoconfigure fixed-voltage supplies
865 		   and the constraints are used by list_voltage. */
866 		if (count == 1 && !cmin) {
867 			cmin = 1;
868 			cmax = INT_MAX;
869 			constraints->min_uV = cmin;
870 			constraints->max_uV = cmax;
871 		}
872 
873 		/* voltage constraints are optional */
874 		if ((cmin == 0) && (cmax == 0))
875 			return 0;
876 
877 		/* else require explicit machine-level constraints */
878 		if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
879 			rdev_err(rdev, "invalid voltage constraints\n");
880 			return -EINVAL;
881 		}
882 
883 		/* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
884 		for (i = 0; i < count; i++) {
885 			int	value;
886 
887 			value = ops->list_voltage(rdev, i);
888 			if (value <= 0)
889 				continue;
890 
891 			/* maybe adjust [min_uV..max_uV] */
892 			if (value >= cmin && value < min_uV)
893 				min_uV = value;
894 			if (value <= cmax && value > max_uV)
895 				max_uV = value;
896 		}
897 
898 		/* final: [min_uV..max_uV] valid iff constraints valid */
899 		if (max_uV < min_uV) {
900 			rdev_err(rdev,
901 				 "unsupportable voltage constraints %u-%uuV\n",
902 				 min_uV, max_uV);
903 			return -EINVAL;
904 		}
905 
906 		/* use regulator's subset of machine constraints */
907 		if (constraints->min_uV < min_uV) {
908 			rdev_dbg(rdev, "override min_uV, %d -> %d\n",
909 				 constraints->min_uV, min_uV);
910 			constraints->min_uV = min_uV;
911 		}
912 		if (constraints->max_uV > max_uV) {
913 			rdev_dbg(rdev, "override max_uV, %d -> %d\n",
914 				 constraints->max_uV, max_uV);
915 			constraints->max_uV = max_uV;
916 		}
917 	}
918 
919 	return 0;
920 }
921 
922 /**
923  * set_machine_constraints - sets regulator constraints
924  * @rdev: regulator source
925  * @constraints: constraints to apply
926  *
927  * Allows platform initialisation code to define and constrain
928  * regulator circuits e.g. valid voltage/current ranges, etc.  NOTE:
929  * Constraints *must* be set by platform code in order for some
930  * regulator operations to proceed i.e. set_voltage, set_current_limit,
931  * set_mode.
932  */
set_machine_constraints(struct regulator_dev * rdev,const struct regulation_constraints * constraints)933 static int set_machine_constraints(struct regulator_dev *rdev,
934 	const struct regulation_constraints *constraints)
935 {
936 	int ret = 0;
937 	struct regulator_ops *ops = rdev->desc->ops;
938 
939 	if (constraints)
940 		rdev->constraints = kmemdup(constraints, sizeof(*constraints),
941 					    GFP_KERNEL);
942 	else
943 		rdev->constraints = kzalloc(sizeof(*constraints),
944 					    GFP_KERNEL);
945 	if (!rdev->constraints)
946 		return -ENOMEM;
947 
948 	ret = machine_constraints_voltage(rdev, rdev->constraints);
949 	if (ret != 0)
950 		goto out;
951 
952 	/* do we need to setup our suspend state */
953 	if (rdev->constraints->initial_state) {
954 		ret = suspend_prepare(rdev, rdev->constraints->initial_state);
955 		if (ret < 0) {
956 			rdev_err(rdev, "failed to set suspend state\n");
957 			goto out;
958 		}
959 	}
960 
961 	if (rdev->constraints->initial_mode) {
962 		if (!ops->set_mode) {
963 			rdev_err(rdev, "no set_mode operation\n");
964 			ret = -EINVAL;
965 			goto out;
966 		}
967 
968 		ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
969 		if (ret < 0) {
970 			rdev_err(rdev, "failed to set initial mode: %d\n", ret);
971 			goto out;
972 		}
973 	}
974 
975 	/* If the constraints say the regulator should be on at this point
976 	 * and we have control then make sure it is enabled.
977 	 */
978 	if ((rdev->constraints->always_on || rdev->constraints->boot_on) &&
979 	    ops->enable) {
980 		ret = ops->enable(rdev);
981 		if (ret < 0) {
982 			rdev_err(rdev, "failed to enable\n");
983 			goto out;
984 		}
985 	}
986 
987 	if (rdev->constraints->ramp_delay && ops->set_ramp_delay) {
988 		ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay);
989 		if (ret < 0) {
990 			rdev_err(rdev, "failed to set ramp_delay\n");
991 			goto out;
992 		}
993 	}
994 
995 	print_constraints(rdev);
996 	return 0;
997 out:
998 	kfree(rdev->constraints);
999 	rdev->constraints = NULL;
1000 	return ret;
1001 }
1002 
1003 /**
1004  * set_supply - set regulator supply regulator
1005  * @rdev: regulator name
1006  * @supply_rdev: supply regulator name
1007  *
1008  * Called by platform initialisation code to set the supply regulator for this
1009  * regulator. This ensures that a regulators supply will also be enabled by the
1010  * core if it's child is enabled.
1011  */
set_supply(struct regulator_dev * rdev,struct regulator_dev * supply_rdev)1012 static int set_supply(struct regulator_dev *rdev,
1013 		      struct regulator_dev *supply_rdev)
1014 {
1015 	int err;
1016 
1017 	rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
1018 
1019 	rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
1020 	if (rdev->supply == NULL) {
1021 		err = -ENOMEM;
1022 		return err;
1023 	}
1024 	supply_rdev->open_count++;
1025 
1026 	return 0;
1027 }
1028 
1029 /**
1030  * set_consumer_device_supply - Bind a regulator to a symbolic supply
1031  * @rdev:         regulator source
1032  * @consumer_dev_name: dev_name() string for device supply applies to
1033  * @supply:       symbolic name for supply
1034  *
1035  * Allows platform initialisation code to map physical regulator
1036  * sources to symbolic names for supplies for use by devices.  Devices
1037  * should use these symbolic names to request regulators, avoiding the
1038  * need to provide board-specific regulator names as platform data.
1039  */
set_consumer_device_supply(struct regulator_dev * rdev,const char * consumer_dev_name,const char * supply)1040 static int set_consumer_device_supply(struct regulator_dev *rdev,
1041 				      const char *consumer_dev_name,
1042 				      const char *supply)
1043 {
1044 	struct regulator_map *node;
1045 	int has_dev;
1046 
1047 	if (supply == NULL)
1048 		return -EINVAL;
1049 
1050 	if (consumer_dev_name != NULL)
1051 		has_dev = 1;
1052 	else
1053 		has_dev = 0;
1054 
1055 	list_for_each_entry(node, &regulator_map_list, list) {
1056 		if (node->dev_name && consumer_dev_name) {
1057 			if (strcmp(node->dev_name, consumer_dev_name) != 0)
1058 				continue;
1059 		} else if (node->dev_name || consumer_dev_name) {
1060 			continue;
1061 		}
1062 
1063 		if (strcmp(node->supply, supply) != 0)
1064 			continue;
1065 
1066 		pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1067 			 consumer_dev_name,
1068 			 dev_name(&node->regulator->dev),
1069 			 node->regulator->desc->name,
1070 			 supply,
1071 			 dev_name(&rdev->dev), rdev_get_name(rdev));
1072 		return -EBUSY;
1073 	}
1074 
1075 	node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
1076 	if (node == NULL)
1077 		return -ENOMEM;
1078 
1079 	node->regulator = rdev;
1080 	node->supply = supply;
1081 
1082 	if (has_dev) {
1083 		node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1084 		if (node->dev_name == NULL) {
1085 			kfree(node);
1086 			return -ENOMEM;
1087 		}
1088 	}
1089 
1090 	list_add(&node->list, &regulator_map_list);
1091 	return 0;
1092 }
1093 
unset_regulator_supplies(struct regulator_dev * rdev)1094 static void unset_regulator_supplies(struct regulator_dev *rdev)
1095 {
1096 	struct regulator_map *node, *n;
1097 
1098 	list_for_each_entry_safe(node, n, &regulator_map_list, list) {
1099 		if (rdev == node->regulator) {
1100 			list_del(&node->list);
1101 			kfree(node->dev_name);
1102 			kfree(node);
1103 		}
1104 	}
1105 }
1106 
1107 #define REG_STR_SIZE	64
1108 
create_regulator(struct regulator_dev * rdev,struct device * dev,const char * supply_name)1109 static struct regulator *create_regulator(struct regulator_dev *rdev,
1110 					  struct device *dev,
1111 					  const char *supply_name)
1112 {
1113 	struct regulator *regulator;
1114 	char buf[REG_STR_SIZE];
1115 	int err, size;
1116 
1117 	regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1118 	if (regulator == NULL)
1119 		return NULL;
1120 
1121 	mutex_lock(&rdev->mutex);
1122 	regulator->rdev = rdev;
1123 	list_add(&regulator->list, &rdev->consumer_list);
1124 
1125 	if (dev) {
1126 		regulator->dev = dev;
1127 
1128 		/* Add a link to the device sysfs entry */
1129 		size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1130 				 dev->kobj.name, supply_name);
1131 		if (size >= REG_STR_SIZE)
1132 			goto overflow_err;
1133 
1134 		regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1135 		if (regulator->supply_name == NULL)
1136 			goto overflow_err;
1137 
1138 		err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
1139 					buf);
1140 		if (err) {
1141 			rdev_warn(rdev, "could not add device link %s err %d\n",
1142 				  dev->kobj.name, err);
1143 			/* non-fatal */
1144 		}
1145 	} else {
1146 		regulator->supply_name = kstrdup(supply_name, GFP_KERNEL);
1147 		if (regulator->supply_name == NULL)
1148 			goto overflow_err;
1149 	}
1150 
1151 	regulator->debugfs = debugfs_create_dir(regulator->supply_name,
1152 						rdev->debugfs);
1153 	if (!regulator->debugfs) {
1154 		rdev_warn(rdev, "Failed to create debugfs directory\n");
1155 	} else {
1156 		debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1157 				   &regulator->uA_load);
1158 		debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1159 				   &regulator->min_uV);
1160 		debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1161 				   &regulator->max_uV);
1162 	}
1163 
1164 	/*
1165 	 * Check now if the regulator is an always on regulator - if
1166 	 * it is then we don't need to do nearly so much work for
1167 	 * enable/disable calls.
1168 	 */
1169 	if (!_regulator_can_change_status(rdev) &&
1170 	    _regulator_is_enabled(rdev))
1171 		regulator->always_on = true;
1172 
1173 	mutex_unlock(&rdev->mutex);
1174 	return regulator;
1175 overflow_err:
1176 	list_del(&regulator->list);
1177 	kfree(regulator);
1178 	mutex_unlock(&rdev->mutex);
1179 	return NULL;
1180 }
1181 
_regulator_get_enable_time(struct regulator_dev * rdev)1182 static int _regulator_get_enable_time(struct regulator_dev *rdev)
1183 {
1184 	if (!rdev->desc->ops->enable_time)
1185 		return rdev->desc->enable_time;
1186 	return rdev->desc->ops->enable_time(rdev);
1187 }
1188 
regulator_dev_lookup(struct device * dev,const char * supply,int * ret)1189 static struct regulator_dev *regulator_dev_lookup(struct device *dev,
1190 						  const char *supply,
1191 						  int *ret)
1192 {
1193 	struct regulator_dev *r;
1194 	struct device_node *node;
1195 	struct regulator_map *map;
1196 	const char *devname = NULL;
1197 
1198 	/* first do a dt based lookup */
1199 	if (dev && dev->of_node) {
1200 		node = of_get_regulator(dev, supply);
1201 		if (node) {
1202 			list_for_each_entry(r, &regulator_list, list)
1203 				if (r->dev.parent &&
1204 					node == r->dev.of_node)
1205 					return r;
1206 		} else {
1207 			/*
1208 			 * If we couldn't even get the node then it's
1209 			 * not just that the device didn't register
1210 			 * yet, there's no node and we'll never
1211 			 * succeed.
1212 			 */
1213 			*ret = -ENODEV;
1214 		}
1215 	}
1216 
1217 	/* if not found, try doing it non-dt way */
1218 	if (dev)
1219 		devname = dev_name(dev);
1220 
1221 	list_for_each_entry(r, &regulator_list, list)
1222 		if (strcmp(rdev_get_name(r), supply) == 0)
1223 			return r;
1224 
1225 	list_for_each_entry(map, &regulator_map_list, list) {
1226 		/* If the mapping has a device set up it must match */
1227 		if (map->dev_name &&
1228 		    (!devname || strcmp(map->dev_name, devname)))
1229 			continue;
1230 
1231 		if (strcmp(map->supply, supply) == 0)
1232 			return map->regulator;
1233 	}
1234 
1235 
1236 	return NULL;
1237 }
1238 
1239 /* Internal regulator request function */
_regulator_get(struct device * dev,const char * id,int exclusive)1240 static struct regulator *_regulator_get(struct device *dev, const char *id,
1241 					int exclusive)
1242 {
1243 	struct regulator_dev *rdev;
1244 	struct regulator *regulator = ERR_PTR(-EPROBE_DEFER);
1245 	const char *devname = NULL;
1246 	int ret = 0;
1247 
1248 	if (id == NULL) {
1249 		pr_err("get() with no identifier\n");
1250 		return regulator;
1251 	}
1252 
1253 	if (dev)
1254 		devname = dev_name(dev);
1255 
1256 	mutex_lock(&regulator_list_mutex);
1257 
1258 	rdev = regulator_dev_lookup(dev, id, &ret);
1259 	if (rdev)
1260 		goto found;
1261 
1262 	/*
1263 	 * If we have return value from dev_lookup fail, we do not expect to
1264 	 * succeed, so, quit with appropriate error value
1265 	 */
1266 	if (ret) {
1267 		regulator = ERR_PTR(ret);
1268 		goto out;
1269 	}
1270 
1271 	if (board_wants_dummy_regulator) {
1272 		rdev = dummy_regulator_rdev;
1273 		goto found;
1274 	}
1275 
1276 #ifdef CONFIG_REGULATOR_DUMMY
1277 	if (!devname)
1278 		devname = "deviceless";
1279 
1280 	/* If the board didn't flag that it was fully constrained then
1281 	 * substitute in a dummy regulator so consumers can continue.
1282 	 */
1283 	if (!has_full_constraints) {
1284 		pr_warn("%s supply %s not found, using dummy regulator\n",
1285 			devname, id);
1286 		rdev = dummy_regulator_rdev;
1287 		goto found;
1288 	}
1289 #endif
1290 
1291 	mutex_unlock(&regulator_list_mutex);
1292 	return regulator;
1293 
1294 found:
1295 	if (rdev->exclusive) {
1296 		regulator = ERR_PTR(-EPERM);
1297 		goto out;
1298 	}
1299 
1300 	if (exclusive && rdev->open_count) {
1301 		regulator = ERR_PTR(-EBUSY);
1302 		goto out;
1303 	}
1304 
1305 	if (!try_module_get(rdev->owner))
1306 		goto out;
1307 
1308 	regulator = create_regulator(rdev, dev, id);
1309 	if (regulator == NULL) {
1310 		regulator = ERR_PTR(-ENOMEM);
1311 		module_put(rdev->owner);
1312 		goto out;
1313 	}
1314 
1315 	rdev->open_count++;
1316 	if (exclusive) {
1317 		rdev->exclusive = 1;
1318 
1319 		ret = _regulator_is_enabled(rdev);
1320 		if (ret > 0)
1321 			rdev->use_count = 1;
1322 		else
1323 			rdev->use_count = 0;
1324 	}
1325 
1326 out:
1327 	mutex_unlock(&regulator_list_mutex);
1328 
1329 	return regulator;
1330 }
1331 
1332 /**
1333  * regulator_get - lookup and obtain a reference to a regulator.
1334  * @dev: device for regulator "consumer"
1335  * @id: Supply name or regulator ID.
1336  *
1337  * Returns a struct regulator corresponding to the regulator producer,
1338  * or IS_ERR() condition containing errno.
1339  *
1340  * Use of supply names configured via regulator_set_device_supply() is
1341  * strongly encouraged.  It is recommended that the supply name used
1342  * should match the name used for the supply and/or the relevant
1343  * device pins in the datasheet.
1344  */
regulator_get(struct device * dev,const char * id)1345 struct regulator *regulator_get(struct device *dev, const char *id)
1346 {
1347 	return _regulator_get(dev, id, 0);
1348 }
1349 EXPORT_SYMBOL_GPL(regulator_get);
1350 
devm_regulator_release(struct device * dev,void * res)1351 static void devm_regulator_release(struct device *dev, void *res)
1352 {
1353 	regulator_put(*(struct regulator **)res);
1354 }
1355 
1356 /**
1357  * devm_regulator_get - Resource managed regulator_get()
1358  * @dev: device for regulator "consumer"
1359  * @id: Supply name or regulator ID.
1360  *
1361  * Managed regulator_get(). Regulators returned from this function are
1362  * automatically regulator_put() on driver detach. See regulator_get() for more
1363  * information.
1364  */
devm_regulator_get(struct device * dev,const char * id)1365 struct regulator *devm_regulator_get(struct device *dev, const char *id)
1366 {
1367 	struct regulator **ptr, *regulator;
1368 
1369 	ptr = devres_alloc(devm_regulator_release, sizeof(*ptr), GFP_KERNEL);
1370 	if (!ptr)
1371 		return ERR_PTR(-ENOMEM);
1372 
1373 	regulator = regulator_get(dev, id);
1374 	if (!IS_ERR(regulator)) {
1375 		*ptr = regulator;
1376 		devres_add(dev, ptr);
1377 	} else {
1378 		devres_free(ptr);
1379 	}
1380 
1381 	return regulator;
1382 }
1383 EXPORT_SYMBOL_GPL(devm_regulator_get);
1384 
1385 /**
1386  * regulator_get_exclusive - obtain exclusive access to a regulator.
1387  * @dev: device for regulator "consumer"
1388  * @id: Supply name or regulator ID.
1389  *
1390  * Returns a struct regulator corresponding to the regulator producer,
1391  * or IS_ERR() condition containing errno.  Other consumers will be
1392  * unable to obtain this reference is held and the use count for the
1393  * regulator will be initialised to reflect the current state of the
1394  * regulator.
1395  *
1396  * This is intended for use by consumers which cannot tolerate shared
1397  * use of the regulator such as those which need to force the
1398  * regulator off for correct operation of the hardware they are
1399  * controlling.
1400  *
1401  * Use of supply names configured via regulator_set_device_supply() is
1402  * strongly encouraged.  It is recommended that the supply name used
1403  * should match the name used for the supply and/or the relevant
1404  * device pins in the datasheet.
1405  */
regulator_get_exclusive(struct device * dev,const char * id)1406 struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1407 {
1408 	return _regulator_get(dev, id, 1);
1409 }
1410 EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1411 
1412 /* Locks held by regulator_put() */
_regulator_put(struct regulator * regulator)1413 static void _regulator_put(struct regulator *regulator)
1414 {
1415 	struct regulator_dev *rdev;
1416 
1417 	if (regulator == NULL || IS_ERR(regulator))
1418 		return;
1419 
1420 	rdev = regulator->rdev;
1421 
1422 	debugfs_remove_recursive(regulator->debugfs);
1423 
1424 	/* remove any sysfs entries */
1425 	if (regulator->dev)
1426 		sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
1427 	kfree(regulator->supply_name);
1428 	list_del(&regulator->list);
1429 	kfree(regulator);
1430 
1431 	rdev->open_count--;
1432 	rdev->exclusive = 0;
1433 
1434 	module_put(rdev->owner);
1435 }
1436 
1437 /**
1438  * regulator_put - "free" the regulator source
1439  * @regulator: regulator source
1440  *
1441  * Note: drivers must ensure that all regulator_enable calls made on this
1442  * regulator source are balanced by regulator_disable calls prior to calling
1443  * this function.
1444  */
regulator_put(struct regulator * regulator)1445 void regulator_put(struct regulator *regulator)
1446 {
1447 	mutex_lock(&regulator_list_mutex);
1448 	_regulator_put(regulator);
1449 	mutex_unlock(&regulator_list_mutex);
1450 }
1451 EXPORT_SYMBOL_GPL(regulator_put);
1452 
devm_regulator_match(struct device * dev,void * res,void * data)1453 static int devm_regulator_match(struct device *dev, void *res, void *data)
1454 {
1455 	struct regulator **r = res;
1456 	if (!r || !*r) {
1457 		WARN_ON(!r || !*r);
1458 		return 0;
1459 	}
1460 	return *r == data;
1461 }
1462 
1463 /**
1464  * devm_regulator_put - Resource managed regulator_put()
1465  * @regulator: regulator to free
1466  *
1467  * Deallocate a regulator allocated with devm_regulator_get(). Normally
1468  * this function will not need to be called and the resource management
1469  * code will ensure that the resource is freed.
1470  */
devm_regulator_put(struct regulator * regulator)1471 void devm_regulator_put(struct regulator *regulator)
1472 {
1473 	int rc;
1474 
1475 	rc = devres_release(regulator->dev, devm_regulator_release,
1476 			    devm_regulator_match, regulator);
1477 	if (rc != 0)
1478 		WARN_ON(rc);
1479 }
1480 EXPORT_SYMBOL_GPL(devm_regulator_put);
1481 
1482 /* Manage enable GPIO list. Same GPIO pin can be shared among regulators */
regulator_ena_gpio_request(struct regulator_dev * rdev,const struct regulator_config * config)1483 static int regulator_ena_gpio_request(struct regulator_dev *rdev,
1484 				const struct regulator_config *config)
1485 {
1486 	struct regulator_enable_gpio *pin;
1487 	int ret;
1488 
1489 	list_for_each_entry(pin, &regulator_ena_gpio_list, list) {
1490 		if (pin->gpio == config->ena_gpio) {
1491 			rdev_dbg(rdev, "GPIO %d is already used\n",
1492 				config->ena_gpio);
1493 			goto update_ena_gpio_to_rdev;
1494 		}
1495 	}
1496 
1497 	ret = gpio_request_one(config->ena_gpio,
1498 				GPIOF_DIR_OUT | config->ena_gpio_flags,
1499 				rdev_get_name(rdev));
1500 	if (ret)
1501 		return ret;
1502 
1503 	pin = kzalloc(sizeof(struct regulator_enable_gpio), GFP_KERNEL);
1504 	if (pin == NULL) {
1505 		gpio_free(config->ena_gpio);
1506 		return -ENOMEM;
1507 	}
1508 
1509 	pin->gpio = config->ena_gpio;
1510 	pin->ena_gpio_invert = config->ena_gpio_invert;
1511 	list_add(&pin->list, &regulator_ena_gpio_list);
1512 
1513 update_ena_gpio_to_rdev:
1514 	pin->request_count++;
1515 	rdev->ena_pin = pin;
1516 	return 0;
1517 }
1518 
regulator_ena_gpio_free(struct regulator_dev * rdev)1519 static void regulator_ena_gpio_free(struct regulator_dev *rdev)
1520 {
1521 	struct regulator_enable_gpio *pin, *n;
1522 
1523 	if (!rdev->ena_pin)
1524 		return;
1525 
1526 	/* Free the GPIO only in case of no use */
1527 	list_for_each_entry_safe(pin, n, &regulator_ena_gpio_list, list) {
1528 		if (pin->gpio == rdev->ena_pin->gpio) {
1529 			if (pin->request_count <= 1) {
1530 				pin->request_count = 0;
1531 				gpio_free(pin->gpio);
1532 				list_del(&pin->list);
1533 				kfree(pin);
1534 				rdev->ena_pin = NULL;
1535 				return;
1536 			} else {
1537 				pin->request_count--;
1538 			}
1539 		}
1540 	}
1541 }
1542 
1543 /**
1544  * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control
1545  * @rdev: regulator_dev structure
1546  * @enable: enable GPIO at initial use?
1547  *
1548  * GPIO is enabled in case of initial use. (enable_count is 0)
1549  * GPIO is disabled when it is not shared any more. (enable_count <= 1)
1550  */
regulator_ena_gpio_ctrl(struct regulator_dev * rdev,bool enable)1551 static int regulator_ena_gpio_ctrl(struct regulator_dev *rdev, bool enable)
1552 {
1553 	struct regulator_enable_gpio *pin = rdev->ena_pin;
1554 
1555 	if (!pin)
1556 		return -EINVAL;
1557 
1558 	if (enable) {
1559 		/* Enable GPIO at initial use */
1560 		if (pin->enable_count == 0)
1561 			gpio_set_value_cansleep(pin->gpio,
1562 						!pin->ena_gpio_invert);
1563 
1564 		pin->enable_count++;
1565 	} else {
1566 		if (pin->enable_count > 1) {
1567 			pin->enable_count--;
1568 			return 0;
1569 		}
1570 
1571 		/* Disable GPIO if not used */
1572 		if (pin->enable_count <= 1) {
1573 			gpio_set_value_cansleep(pin->gpio,
1574 						pin->ena_gpio_invert);
1575 			pin->enable_count = 0;
1576 		}
1577 	}
1578 
1579 	return 0;
1580 }
1581 
_regulator_do_enable(struct regulator_dev * rdev)1582 static int _regulator_do_enable(struct regulator_dev *rdev)
1583 {
1584 	int ret, delay;
1585 
1586 	/* Query before enabling in case configuration dependent.  */
1587 	ret = _regulator_get_enable_time(rdev);
1588 	if (ret >= 0) {
1589 		delay = ret;
1590 	} else {
1591 		rdev_warn(rdev, "enable_time() failed: %d\n", ret);
1592 		delay = 0;
1593 	}
1594 
1595 	trace_regulator_enable(rdev_get_name(rdev));
1596 
1597 	if (rdev->ena_pin) {
1598 		ret = regulator_ena_gpio_ctrl(rdev, true);
1599 		if (ret < 0)
1600 			return ret;
1601 		rdev->ena_gpio_state = 1;
1602 	} else if (rdev->desc->ops->enable) {
1603 		ret = rdev->desc->ops->enable(rdev);
1604 		if (ret < 0)
1605 			return ret;
1606 	} else {
1607 		return -EINVAL;
1608 	}
1609 
1610 	/* Allow the regulator to ramp; it would be useful to extend
1611 	 * this for bulk operations so that the regulators can ramp
1612 	 * together.  */
1613 	trace_regulator_enable_delay(rdev_get_name(rdev));
1614 
1615 	if (delay >= 1000) {
1616 		mdelay(delay / 1000);
1617 		udelay(delay % 1000);
1618 	} else if (delay) {
1619 		udelay(delay);
1620 	}
1621 
1622 	trace_regulator_enable_complete(rdev_get_name(rdev));
1623 
1624 	return 0;
1625 }
1626 
1627 /* locks held by regulator_enable() */
_regulator_enable(struct regulator_dev * rdev)1628 static int _regulator_enable(struct regulator_dev *rdev)
1629 {
1630 	int ret;
1631 
1632 	/* check voltage and requested load before enabling */
1633 	if (rdev->constraints &&
1634 	    (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1635 		drms_uA_update(rdev);
1636 
1637 	if (rdev->use_count == 0) {
1638 		/* The regulator may on if it's not switchable or left on */
1639 		ret = _regulator_is_enabled(rdev);
1640 		if (ret == -EINVAL || ret == 0) {
1641 			if (!_regulator_can_change_status(rdev))
1642 				return -EPERM;
1643 
1644 			ret = _regulator_do_enable(rdev);
1645 			if (ret < 0)
1646 				return ret;
1647 
1648 		} else if (ret < 0) {
1649 			rdev_err(rdev, "is_enabled() failed: %d\n", ret);
1650 			return ret;
1651 		}
1652 		/* Fallthrough on positive return values - already enabled */
1653 	}
1654 
1655 	rdev->use_count++;
1656 
1657 	return 0;
1658 }
1659 
1660 /**
1661  * regulator_enable - enable regulator output
1662  * @regulator: regulator source
1663  *
1664  * Request that the regulator be enabled with the regulator output at
1665  * the predefined voltage or current value.  Calls to regulator_enable()
1666  * must be balanced with calls to regulator_disable().
1667  *
1668  * NOTE: the output value can be set by other drivers, boot loader or may be
1669  * hardwired in the regulator.
1670  */
regulator_enable(struct regulator * regulator)1671 int regulator_enable(struct regulator *regulator)
1672 {
1673 	struct regulator_dev *rdev = regulator->rdev;
1674 	int ret = 0;
1675 
1676 	if (regulator->always_on)
1677 		return 0;
1678 
1679 	if (rdev->supply) {
1680 		ret = regulator_enable(rdev->supply);
1681 		if (ret != 0)
1682 			return ret;
1683 	}
1684 
1685 	mutex_lock(&rdev->mutex);
1686 	ret = _regulator_enable(rdev);
1687 	mutex_unlock(&rdev->mutex);
1688 
1689 	if (ret != 0 && rdev->supply)
1690 		regulator_disable(rdev->supply);
1691 
1692 	return ret;
1693 }
1694 EXPORT_SYMBOL_GPL(regulator_enable);
1695 
_regulator_do_disable(struct regulator_dev * rdev)1696 static int _regulator_do_disable(struct regulator_dev *rdev)
1697 {
1698 	int ret;
1699 
1700 	trace_regulator_disable(rdev_get_name(rdev));
1701 
1702 	if (rdev->ena_pin) {
1703 		ret = regulator_ena_gpio_ctrl(rdev, false);
1704 		if (ret < 0)
1705 			return ret;
1706 		rdev->ena_gpio_state = 0;
1707 
1708 	} else if (rdev->desc->ops->disable) {
1709 		ret = rdev->desc->ops->disable(rdev);
1710 		if (ret != 0)
1711 			return ret;
1712 	}
1713 
1714 	trace_regulator_disable_complete(rdev_get_name(rdev));
1715 
1716 	_notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
1717 			     NULL);
1718 	return 0;
1719 }
1720 
1721 /* locks held by regulator_disable() */
_regulator_disable(struct regulator_dev * rdev)1722 static int _regulator_disable(struct regulator_dev *rdev)
1723 {
1724 	int ret = 0;
1725 
1726 	if (WARN(rdev->use_count <= 0,
1727 		 "unbalanced disables for %s\n", rdev_get_name(rdev)))
1728 		return -EIO;
1729 
1730 	/* are we the last user and permitted to disable ? */
1731 	if (rdev->use_count == 1 &&
1732 	    (rdev->constraints && !rdev->constraints->always_on)) {
1733 
1734 		/* we are last user */
1735 		if (_regulator_can_change_status(rdev)) {
1736 			ret = _regulator_do_disable(rdev);
1737 			if (ret < 0) {
1738 				rdev_err(rdev, "failed to disable\n");
1739 				return ret;
1740 			}
1741 		}
1742 
1743 		rdev->use_count = 0;
1744 	} else if (rdev->use_count > 1) {
1745 
1746 		if (rdev->constraints &&
1747 			(rdev->constraints->valid_ops_mask &
1748 			REGULATOR_CHANGE_DRMS))
1749 			drms_uA_update(rdev);
1750 
1751 		rdev->use_count--;
1752 	}
1753 
1754 	return ret;
1755 }
1756 
1757 /**
1758  * regulator_disable - disable regulator output
1759  * @regulator: regulator source
1760  *
1761  * Disable the regulator output voltage or current.  Calls to
1762  * regulator_enable() must be balanced with calls to
1763  * regulator_disable().
1764  *
1765  * NOTE: this will only disable the regulator output if no other consumer
1766  * devices have it enabled, the regulator device supports disabling and
1767  * machine constraints permit this operation.
1768  */
regulator_disable(struct regulator * regulator)1769 int regulator_disable(struct regulator *regulator)
1770 {
1771 	struct regulator_dev *rdev = regulator->rdev;
1772 	int ret = 0;
1773 
1774 	if (regulator->always_on)
1775 		return 0;
1776 
1777 	mutex_lock(&rdev->mutex);
1778 	ret = _regulator_disable(rdev);
1779 	mutex_unlock(&rdev->mutex);
1780 
1781 	if (ret == 0 && rdev->supply)
1782 		regulator_disable(rdev->supply);
1783 
1784 	return ret;
1785 }
1786 EXPORT_SYMBOL_GPL(regulator_disable);
1787 
1788 /* locks held by regulator_force_disable() */
_regulator_force_disable(struct regulator_dev * rdev)1789 static int _regulator_force_disable(struct regulator_dev *rdev)
1790 {
1791 	int ret = 0;
1792 
1793 	/* force disable */
1794 	if (rdev->desc->ops->disable) {
1795 		/* ah well, who wants to live forever... */
1796 		ret = rdev->desc->ops->disable(rdev);
1797 		if (ret < 0) {
1798 			rdev_err(rdev, "failed to force disable\n");
1799 			return ret;
1800 		}
1801 		/* notify other consumers that power has been forced off */
1802 		_notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
1803 			REGULATOR_EVENT_DISABLE, NULL);
1804 	}
1805 
1806 	return ret;
1807 }
1808 
1809 /**
1810  * regulator_force_disable - force disable regulator output
1811  * @regulator: regulator source
1812  *
1813  * Forcibly disable the regulator output voltage or current.
1814  * NOTE: this *will* disable the regulator output even if other consumer
1815  * devices have it enabled. This should be used for situations when device
1816  * damage will likely occur if the regulator is not disabled (e.g. over temp).
1817  */
regulator_force_disable(struct regulator * regulator)1818 int regulator_force_disable(struct regulator *regulator)
1819 {
1820 	struct regulator_dev *rdev = regulator->rdev;
1821 	int ret;
1822 
1823 	mutex_lock(&rdev->mutex);
1824 	regulator->uA_load = 0;
1825 	ret = _regulator_force_disable(regulator->rdev);
1826 	mutex_unlock(&rdev->mutex);
1827 
1828 	if (rdev->supply)
1829 		while (rdev->open_count--)
1830 			regulator_disable(rdev->supply);
1831 
1832 	return ret;
1833 }
1834 EXPORT_SYMBOL_GPL(regulator_force_disable);
1835 
regulator_disable_work(struct work_struct * work)1836 static void regulator_disable_work(struct work_struct *work)
1837 {
1838 	struct regulator_dev *rdev = container_of(work, struct regulator_dev,
1839 						  disable_work.work);
1840 	int count, i, ret;
1841 
1842 	mutex_lock(&rdev->mutex);
1843 
1844 	BUG_ON(!rdev->deferred_disables);
1845 
1846 	count = rdev->deferred_disables;
1847 	rdev->deferred_disables = 0;
1848 
1849 	for (i = 0; i < count; i++) {
1850 		ret = _regulator_disable(rdev);
1851 		if (ret != 0)
1852 			rdev_err(rdev, "Deferred disable failed: %d\n", ret);
1853 	}
1854 
1855 	mutex_unlock(&rdev->mutex);
1856 
1857 	if (rdev->supply) {
1858 		for (i = 0; i < count; i++) {
1859 			ret = regulator_disable(rdev->supply);
1860 			if (ret != 0) {
1861 				rdev_err(rdev,
1862 					 "Supply disable failed: %d\n", ret);
1863 			}
1864 		}
1865 	}
1866 }
1867 
1868 /**
1869  * regulator_disable_deferred - disable regulator output with delay
1870  * @regulator: regulator source
1871  * @ms: miliseconds until the regulator is disabled
1872  *
1873  * Execute regulator_disable() on the regulator after a delay.  This
1874  * is intended for use with devices that require some time to quiesce.
1875  *
1876  * NOTE: this will only disable the regulator output if no other consumer
1877  * devices have it enabled, the regulator device supports disabling and
1878  * machine constraints permit this operation.
1879  */
regulator_disable_deferred(struct regulator * regulator,int ms)1880 int regulator_disable_deferred(struct regulator *regulator, int ms)
1881 {
1882 	struct regulator_dev *rdev = regulator->rdev;
1883 	int ret;
1884 
1885 	if (regulator->always_on)
1886 		return 0;
1887 
1888 	if (!ms)
1889 		return regulator_disable(regulator);
1890 
1891 	mutex_lock(&rdev->mutex);
1892 	rdev->deferred_disables++;
1893 	mutex_unlock(&rdev->mutex);
1894 
1895 	ret = schedule_delayed_work(&rdev->disable_work,
1896 				    msecs_to_jiffies(ms));
1897 	if (ret < 0)
1898 		return ret;
1899 	else
1900 		return 0;
1901 }
1902 EXPORT_SYMBOL_GPL(regulator_disable_deferred);
1903 
1904 /**
1905  * regulator_is_enabled_regmap - standard is_enabled() for regmap users
1906  *
1907  * @rdev: regulator to operate on
1908  *
1909  * Regulators that use regmap for their register I/O can set the
1910  * enable_reg and enable_mask fields in their descriptor and then use
1911  * this as their is_enabled operation, saving some code.
1912  */
regulator_is_enabled_regmap(struct regulator_dev * rdev)1913 int regulator_is_enabled_regmap(struct regulator_dev *rdev)
1914 {
1915 	unsigned int val;
1916 	int ret;
1917 
1918 	ret = regmap_read(rdev->regmap, rdev->desc->enable_reg, &val);
1919 	if (ret != 0)
1920 		return ret;
1921 
1922 	if (rdev->desc->enable_is_inverted)
1923 		return (val & rdev->desc->enable_mask) == 0;
1924 	else
1925 		return (val & rdev->desc->enable_mask) != 0;
1926 }
1927 EXPORT_SYMBOL_GPL(regulator_is_enabled_regmap);
1928 
1929 /**
1930  * regulator_enable_regmap - standard enable() for regmap users
1931  *
1932  * @rdev: regulator to operate on
1933  *
1934  * Regulators that use regmap for their register I/O can set the
1935  * enable_reg and enable_mask fields in their descriptor and then use
1936  * this as their enable() operation, saving some code.
1937  */
regulator_enable_regmap(struct regulator_dev * rdev)1938 int regulator_enable_regmap(struct regulator_dev *rdev)
1939 {
1940 	unsigned int val;
1941 
1942 	if (rdev->desc->enable_is_inverted)
1943 		val = 0;
1944 	else
1945 		val = rdev->desc->enable_mask;
1946 
1947 	return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
1948 				  rdev->desc->enable_mask, val);
1949 }
1950 EXPORT_SYMBOL_GPL(regulator_enable_regmap);
1951 
1952 /**
1953  * regulator_disable_regmap - standard disable() for regmap users
1954  *
1955  * @rdev: regulator to operate on
1956  *
1957  * Regulators that use regmap for their register I/O can set the
1958  * enable_reg and enable_mask fields in their descriptor and then use
1959  * this as their disable() operation, saving some code.
1960  */
regulator_disable_regmap(struct regulator_dev * rdev)1961 int regulator_disable_regmap(struct regulator_dev *rdev)
1962 {
1963 	unsigned int val;
1964 
1965 	if (rdev->desc->enable_is_inverted)
1966 		val = rdev->desc->enable_mask;
1967 	else
1968 		val = 0;
1969 
1970 	return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
1971 				  rdev->desc->enable_mask, val);
1972 }
1973 EXPORT_SYMBOL_GPL(regulator_disable_regmap);
1974 
_regulator_is_enabled(struct regulator_dev * rdev)1975 static int _regulator_is_enabled(struct regulator_dev *rdev)
1976 {
1977 	/* A GPIO control always takes precedence */
1978 	if (rdev->ena_pin)
1979 		return rdev->ena_gpio_state;
1980 
1981 	/* If we don't know then assume that the regulator is always on */
1982 	if (!rdev->desc->ops->is_enabled)
1983 		return 1;
1984 
1985 	return rdev->desc->ops->is_enabled(rdev);
1986 }
1987 
1988 /**
1989  * regulator_is_enabled - is the regulator output enabled
1990  * @regulator: regulator source
1991  *
1992  * Returns positive if the regulator driver backing the source/client
1993  * has requested that the device be enabled, zero if it hasn't, else a
1994  * negative errno code.
1995  *
1996  * Note that the device backing this regulator handle can have multiple
1997  * users, so it might be enabled even if regulator_enable() was never
1998  * called for this particular source.
1999  */
regulator_is_enabled(struct regulator * regulator)2000 int regulator_is_enabled(struct regulator *regulator)
2001 {
2002 	int ret;
2003 
2004 	if (regulator->always_on)
2005 		return 1;
2006 
2007 	mutex_lock(&regulator->rdev->mutex);
2008 	ret = _regulator_is_enabled(regulator->rdev);
2009 	mutex_unlock(&regulator->rdev->mutex);
2010 
2011 	return ret;
2012 }
2013 EXPORT_SYMBOL_GPL(regulator_is_enabled);
2014 
2015 /**
2016  * regulator_can_change_voltage - check if regulator can change voltage
2017  * @regulator: regulator source
2018  *
2019  * Returns positive if the regulator driver backing the source/client
2020  * can change its voltage, false otherwise. Usefull for detecting fixed
2021  * or dummy regulators and disabling voltage change logic in the client
2022  * driver.
2023  */
regulator_can_change_voltage(struct regulator * regulator)2024 int regulator_can_change_voltage(struct regulator *regulator)
2025 {
2026 	struct regulator_dev	*rdev = regulator->rdev;
2027 
2028 	if (rdev->constraints &&
2029 	    (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
2030 		if (rdev->desc->n_voltages - rdev->desc->linear_min_sel > 1)
2031 			return 1;
2032 
2033 		if (rdev->desc->continuous_voltage_range &&
2034 		    rdev->constraints->min_uV && rdev->constraints->max_uV &&
2035 		    rdev->constraints->min_uV != rdev->constraints->max_uV)
2036 			return 1;
2037 	}
2038 
2039 	return 0;
2040 }
2041 EXPORT_SYMBOL_GPL(regulator_can_change_voltage);
2042 
2043 /**
2044  * regulator_count_voltages - count regulator_list_voltage() selectors
2045  * @regulator: regulator source
2046  *
2047  * Returns number of selectors, or negative errno.  Selectors are
2048  * numbered starting at zero, and typically correspond to bitfields
2049  * in hardware registers.
2050  */
regulator_count_voltages(struct regulator * regulator)2051 int regulator_count_voltages(struct regulator *regulator)
2052 {
2053 	struct regulator_dev	*rdev = regulator->rdev;
2054 
2055 	return rdev->desc->n_voltages ? : -EINVAL;
2056 }
2057 EXPORT_SYMBOL_GPL(regulator_count_voltages);
2058 
2059 /**
2060  * regulator_list_voltage_linear - List voltages with simple calculation
2061  *
2062  * @rdev: Regulator device
2063  * @selector: Selector to convert into a voltage
2064  *
2065  * Regulators with a simple linear mapping between voltages and
2066  * selectors can set min_uV and uV_step in the regulator descriptor
2067  * and then use this function as their list_voltage() operation,
2068  */
regulator_list_voltage_linear(struct regulator_dev * rdev,unsigned int selector)2069 int regulator_list_voltage_linear(struct regulator_dev *rdev,
2070 				  unsigned int selector)
2071 {
2072 	if (selector >= rdev->desc->n_voltages)
2073 		return -EINVAL;
2074 	if (selector < rdev->desc->linear_min_sel)
2075 		return 0;
2076 
2077 	selector -= rdev->desc->linear_min_sel;
2078 
2079 	return rdev->desc->min_uV + (rdev->desc->uV_step * selector);
2080 }
2081 EXPORT_SYMBOL_GPL(regulator_list_voltage_linear);
2082 
2083 /**
2084  * regulator_list_voltage_table - List voltages with table based mapping
2085  *
2086  * @rdev: Regulator device
2087  * @selector: Selector to convert into a voltage
2088  *
2089  * Regulators with table based mapping between voltages and
2090  * selectors can set volt_table in the regulator descriptor
2091  * and then use this function as their list_voltage() operation.
2092  */
regulator_list_voltage_table(struct regulator_dev * rdev,unsigned int selector)2093 int regulator_list_voltage_table(struct regulator_dev *rdev,
2094 				 unsigned int selector)
2095 {
2096 	if (!rdev->desc->volt_table) {
2097 		BUG_ON(!rdev->desc->volt_table);
2098 		return -EINVAL;
2099 	}
2100 
2101 	if (selector >= rdev->desc->n_voltages)
2102 		return -EINVAL;
2103 
2104 	return rdev->desc->volt_table[selector];
2105 }
2106 EXPORT_SYMBOL_GPL(regulator_list_voltage_table);
2107 
2108 /**
2109  * regulator_list_voltage - enumerate supported voltages
2110  * @regulator: regulator source
2111  * @selector: identify voltage to list
2112  * Context: can sleep
2113  *
2114  * Returns a voltage that can be passed to @regulator_set_voltage(),
2115  * zero if this selector code can't be used on this system, or a
2116  * negative errno.
2117  */
regulator_list_voltage(struct regulator * regulator,unsigned selector)2118 int regulator_list_voltage(struct regulator *regulator, unsigned selector)
2119 {
2120 	struct regulator_dev	*rdev = regulator->rdev;
2121 	struct regulator_ops	*ops = rdev->desc->ops;
2122 	int			ret;
2123 
2124 	if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
2125 		return -EINVAL;
2126 
2127 	mutex_lock(&rdev->mutex);
2128 	ret = ops->list_voltage(rdev, selector);
2129 	mutex_unlock(&rdev->mutex);
2130 
2131 	if (ret > 0) {
2132 		if (ret < rdev->constraints->min_uV)
2133 			ret = 0;
2134 		else if (ret > rdev->constraints->max_uV)
2135 			ret = 0;
2136 	}
2137 
2138 	return ret;
2139 }
2140 EXPORT_SYMBOL_GPL(regulator_list_voltage);
2141 
2142 /**
2143  * regulator_is_supported_voltage - check if a voltage range can be supported
2144  *
2145  * @regulator: Regulator to check.
2146  * @min_uV: Minimum required voltage in uV.
2147  * @max_uV: Maximum required voltage in uV.
2148  *
2149  * Returns a boolean or a negative error code.
2150  */
regulator_is_supported_voltage(struct regulator * regulator,int min_uV,int max_uV)2151 int regulator_is_supported_voltage(struct regulator *regulator,
2152 				   int min_uV, int max_uV)
2153 {
2154 	struct regulator_dev *rdev = regulator->rdev;
2155 	int i, voltages, ret;
2156 
2157 	/* If we can't change voltage check the current voltage */
2158 	if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
2159 		ret = regulator_get_voltage(regulator);
2160 		if (ret >= 0)
2161 			return (min_uV <= ret && ret <= max_uV);
2162 		else
2163 			return ret;
2164 	}
2165 
2166 	/* Any voltage within constrains range is fine? */
2167 	if (rdev->desc->continuous_voltage_range)
2168 		return min_uV >= rdev->constraints->min_uV &&
2169 				max_uV <= rdev->constraints->max_uV;
2170 
2171 	ret = regulator_count_voltages(regulator);
2172 	if (ret < 0)
2173 		return ret;
2174 	voltages = ret;
2175 
2176 	for (i = 0; i < voltages; i++) {
2177 		ret = regulator_list_voltage(regulator, i);
2178 
2179 		if (ret >= min_uV && ret <= max_uV)
2180 			return 1;
2181 	}
2182 
2183 	return 0;
2184 }
2185 EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
2186 
2187 /**
2188  * regulator_get_voltage_sel_regmap - standard get_voltage_sel for regmap users
2189  *
2190  * @rdev: regulator to operate on
2191  *
2192  * Regulators that use regmap for their register I/O can set the
2193  * vsel_reg and vsel_mask fields in their descriptor and then use this
2194  * as their get_voltage_vsel operation, saving some code.
2195  */
regulator_get_voltage_sel_regmap(struct regulator_dev * rdev)2196 int regulator_get_voltage_sel_regmap(struct regulator_dev *rdev)
2197 {
2198 	unsigned int val;
2199 	int ret;
2200 
2201 	ret = regmap_read(rdev->regmap, rdev->desc->vsel_reg, &val);
2202 	if (ret != 0)
2203 		return ret;
2204 
2205 	val &= rdev->desc->vsel_mask;
2206 	val >>= ffs(rdev->desc->vsel_mask) - 1;
2207 
2208 	return val;
2209 }
2210 EXPORT_SYMBOL_GPL(regulator_get_voltage_sel_regmap);
2211 
2212 /**
2213  * regulator_set_voltage_sel_regmap - standard set_voltage_sel for regmap users
2214  *
2215  * @rdev: regulator to operate on
2216  * @sel: Selector to set
2217  *
2218  * Regulators that use regmap for their register I/O can set the
2219  * vsel_reg and vsel_mask fields in their descriptor and then use this
2220  * as their set_voltage_vsel operation, saving some code.
2221  */
regulator_set_voltage_sel_regmap(struct regulator_dev * rdev,unsigned sel)2222 int regulator_set_voltage_sel_regmap(struct regulator_dev *rdev, unsigned sel)
2223 {
2224 	int ret;
2225 
2226 	sel <<= ffs(rdev->desc->vsel_mask) - 1;
2227 
2228 	ret = regmap_update_bits(rdev->regmap, rdev->desc->vsel_reg,
2229 				  rdev->desc->vsel_mask, sel);
2230 	if (ret)
2231 		return ret;
2232 
2233 	if (rdev->desc->apply_bit)
2234 		ret = regmap_update_bits(rdev->regmap, rdev->desc->apply_reg,
2235 					 rdev->desc->apply_bit,
2236 					 rdev->desc->apply_bit);
2237 	return ret;
2238 }
2239 EXPORT_SYMBOL_GPL(regulator_set_voltage_sel_regmap);
2240 
2241 /**
2242  * regulator_map_voltage_iterate - map_voltage() based on list_voltage()
2243  *
2244  * @rdev: Regulator to operate on
2245  * @min_uV: Lower bound for voltage
2246  * @max_uV: Upper bound for voltage
2247  *
2248  * Drivers implementing set_voltage_sel() and list_voltage() can use
2249  * this as their map_voltage() operation.  It will find a suitable
2250  * voltage by calling list_voltage() until it gets something in bounds
2251  * for the requested voltages.
2252  */
regulator_map_voltage_iterate(struct regulator_dev * rdev,int min_uV,int max_uV)2253 int regulator_map_voltage_iterate(struct regulator_dev *rdev,
2254 				  int min_uV, int max_uV)
2255 {
2256 	int best_val = INT_MAX;
2257 	int selector = 0;
2258 	int i, ret;
2259 
2260 	/* Find the smallest voltage that falls within the specified
2261 	 * range.
2262 	 */
2263 	for (i = 0; i < rdev->desc->n_voltages; i++) {
2264 		ret = rdev->desc->ops->list_voltage(rdev, i);
2265 		if (ret < 0)
2266 			continue;
2267 
2268 		if (ret < best_val && ret >= min_uV && ret <= max_uV) {
2269 			best_val = ret;
2270 			selector = i;
2271 		}
2272 	}
2273 
2274 	if (best_val != INT_MAX)
2275 		return selector;
2276 	else
2277 		return -EINVAL;
2278 }
2279 EXPORT_SYMBOL_GPL(regulator_map_voltage_iterate);
2280 
2281 /**
2282  * regulator_map_voltage_ascend - map_voltage() for ascendant voltage list
2283  *
2284  * @rdev: Regulator to operate on
2285  * @min_uV: Lower bound for voltage
2286  * @max_uV: Upper bound for voltage
2287  *
2288  * Drivers that have ascendant voltage list can use this as their
2289  * map_voltage() operation.
2290  */
regulator_map_voltage_ascend(struct regulator_dev * rdev,int min_uV,int max_uV)2291 int regulator_map_voltage_ascend(struct regulator_dev *rdev,
2292 				 int min_uV, int max_uV)
2293 {
2294 	int i, ret;
2295 
2296 	for (i = 0; i < rdev->desc->n_voltages; i++) {
2297 		ret = rdev->desc->ops->list_voltage(rdev, i);
2298 		if (ret < 0)
2299 			continue;
2300 
2301 		if (ret > max_uV)
2302 			break;
2303 
2304 		if (ret >= min_uV && ret <= max_uV)
2305 			return i;
2306 	}
2307 
2308 	return -EINVAL;
2309 }
2310 EXPORT_SYMBOL_GPL(regulator_map_voltage_ascend);
2311 
2312 /**
2313  * regulator_map_voltage_linear - map_voltage() for simple linear mappings
2314  *
2315  * @rdev: Regulator to operate on
2316  * @min_uV: Lower bound for voltage
2317  * @max_uV: Upper bound for voltage
2318  *
2319  * Drivers providing min_uV and uV_step in their regulator_desc can
2320  * use this as their map_voltage() operation.
2321  */
regulator_map_voltage_linear(struct regulator_dev * rdev,int min_uV,int max_uV)2322 int regulator_map_voltage_linear(struct regulator_dev *rdev,
2323 				 int min_uV, int max_uV)
2324 {
2325 	int ret, voltage;
2326 
2327 	/* Allow uV_step to be 0 for fixed voltage */
2328 	if (rdev->desc->n_voltages == 1 && rdev->desc->uV_step == 0) {
2329 		if (min_uV <= rdev->desc->min_uV && rdev->desc->min_uV <= max_uV)
2330 			return 0;
2331 		else
2332 			return -EINVAL;
2333 	}
2334 
2335 	if (!rdev->desc->uV_step) {
2336 		BUG_ON(!rdev->desc->uV_step);
2337 		return -EINVAL;
2338 	}
2339 
2340 	if (min_uV < rdev->desc->min_uV)
2341 		min_uV = rdev->desc->min_uV;
2342 
2343 	ret = DIV_ROUND_UP(min_uV - rdev->desc->min_uV, rdev->desc->uV_step);
2344 	if (ret < 0)
2345 		return ret;
2346 
2347 	ret += rdev->desc->linear_min_sel;
2348 
2349 	/* Map back into a voltage to verify we're still in bounds */
2350 	voltage = rdev->desc->ops->list_voltage(rdev, ret);
2351 	if (voltage < min_uV || voltage > max_uV)
2352 		return -EINVAL;
2353 
2354 	return ret;
2355 }
2356 EXPORT_SYMBOL_GPL(regulator_map_voltage_linear);
2357 
_regulator_do_set_voltage(struct regulator_dev * rdev,int min_uV,int max_uV)2358 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
2359 				     int min_uV, int max_uV)
2360 {
2361 	int ret;
2362 	int delay = 0;
2363 	int best_val = 0;
2364 	unsigned int selector;
2365 	int old_selector = -1;
2366 
2367 	trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
2368 
2369 	min_uV += rdev->constraints->uV_offset;
2370 	max_uV += rdev->constraints->uV_offset;
2371 
2372 	/*
2373 	 * If we can't obtain the old selector there is not enough
2374 	 * info to call set_voltage_time_sel().
2375 	 */
2376 	if (_regulator_is_enabled(rdev) &&
2377 	    rdev->desc->ops->set_voltage_time_sel &&
2378 	    rdev->desc->ops->get_voltage_sel) {
2379 		old_selector = rdev->desc->ops->get_voltage_sel(rdev);
2380 		if (old_selector < 0)
2381 			return old_selector;
2382 	}
2383 
2384 	if (rdev->desc->ops->set_voltage) {
2385 		ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV,
2386 						   &selector);
2387 
2388 		if (ret >= 0) {
2389 			if (rdev->desc->ops->list_voltage)
2390 				best_val = rdev->desc->ops->list_voltage(rdev,
2391 									 selector);
2392 			else
2393 				best_val = _regulator_get_voltage(rdev);
2394 		}
2395 
2396 	} else if (rdev->desc->ops->set_voltage_sel) {
2397 		if (rdev->desc->ops->map_voltage) {
2398 			ret = rdev->desc->ops->map_voltage(rdev, min_uV,
2399 							   max_uV);
2400 		} else {
2401 			if (rdev->desc->ops->list_voltage ==
2402 			    regulator_list_voltage_linear)
2403 				ret = regulator_map_voltage_linear(rdev,
2404 								min_uV, max_uV);
2405 			else
2406 				ret = regulator_map_voltage_iterate(rdev,
2407 								min_uV, max_uV);
2408 		}
2409 
2410 		if (ret >= 0) {
2411 			best_val = rdev->desc->ops->list_voltage(rdev, ret);
2412 			if (min_uV <= best_val && max_uV >= best_val) {
2413 				selector = ret;
2414 				if (old_selector == selector)
2415 					ret = 0;
2416 				else
2417 					ret = rdev->desc->ops->set_voltage_sel(
2418 								rdev, ret);
2419 			} else {
2420 				ret = -EINVAL;
2421 			}
2422 		}
2423 	} else {
2424 		ret = -EINVAL;
2425 	}
2426 
2427 	/* Call set_voltage_time_sel if successfully obtained old_selector */
2428 	if (ret == 0 && _regulator_is_enabled(rdev) && old_selector >= 0 &&
2429 	    old_selector != selector && rdev->desc->ops->set_voltage_time_sel) {
2430 
2431 		delay = rdev->desc->ops->set_voltage_time_sel(rdev,
2432 						old_selector, selector);
2433 		if (delay < 0) {
2434 			rdev_warn(rdev, "set_voltage_time_sel() failed: %d\n",
2435 				  delay);
2436 			delay = 0;
2437 		}
2438 
2439 		/* Insert any necessary delays */
2440 		if (delay >= 1000) {
2441 			mdelay(delay / 1000);
2442 			udelay(delay % 1000);
2443 		} else if (delay) {
2444 			udelay(delay);
2445 		}
2446 	}
2447 
2448 	if (ret == 0 && best_val >= 0) {
2449 		unsigned long data = best_val;
2450 
2451 		_notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
2452 				     (void *)data);
2453 	}
2454 
2455 	trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
2456 
2457 	return ret;
2458 }
2459 
2460 /**
2461  * regulator_set_voltage - set regulator output voltage
2462  * @regulator: regulator source
2463  * @min_uV: Minimum required voltage in uV
2464  * @max_uV: Maximum acceptable voltage in uV
2465  *
2466  * Sets a voltage regulator to the desired output voltage. This can be set
2467  * during any regulator state. IOW, regulator can be disabled or enabled.
2468  *
2469  * If the regulator is enabled then the voltage will change to the new value
2470  * immediately otherwise if the regulator is disabled the regulator will
2471  * output at the new voltage when enabled.
2472  *
2473  * NOTE: If the regulator is shared between several devices then the lowest
2474  * request voltage that meets the system constraints will be used.
2475  * Regulator system constraints must be set for this regulator before
2476  * calling this function otherwise this call will fail.
2477  */
regulator_set_voltage(struct regulator * regulator,int min_uV,int max_uV)2478 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
2479 {
2480 	struct regulator_dev *rdev = regulator->rdev;
2481 	int ret = 0;
2482 	int old_min_uV, old_max_uV;
2483 
2484 	mutex_lock(&rdev->mutex);
2485 
2486 	/* If we're setting the same range as last time the change
2487 	 * should be a noop (some cpufreq implementations use the same
2488 	 * voltage for multiple frequencies, for example).
2489 	 */
2490 	if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
2491 		goto out;
2492 
2493 	/* sanity check */
2494 	if (!rdev->desc->ops->set_voltage &&
2495 	    !rdev->desc->ops->set_voltage_sel) {
2496 		ret = -EINVAL;
2497 		goto out;
2498 	}
2499 
2500 	/* constraints check */
2501 	ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2502 	if (ret < 0)
2503 		goto out;
2504 
2505 	/* restore original values in case of error */
2506 	old_min_uV = regulator->min_uV;
2507 	old_max_uV = regulator->max_uV;
2508 	regulator->min_uV = min_uV;
2509 	regulator->max_uV = max_uV;
2510 
2511 	ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2512 	if (ret < 0)
2513 		goto out2;
2514 
2515 	ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
2516 	if (ret < 0)
2517 		goto out2;
2518 
2519 out:
2520 	mutex_unlock(&rdev->mutex);
2521 	return ret;
2522 out2:
2523 	regulator->min_uV = old_min_uV;
2524 	regulator->max_uV = old_max_uV;
2525 	mutex_unlock(&rdev->mutex);
2526 	return ret;
2527 }
2528 EXPORT_SYMBOL_GPL(regulator_set_voltage);
2529 
2530 /**
2531  * regulator_set_voltage_time - get raise/fall time
2532  * @regulator: regulator source
2533  * @old_uV: starting voltage in microvolts
2534  * @new_uV: target voltage in microvolts
2535  *
2536  * Provided with the starting and ending voltage, this function attempts to
2537  * calculate the time in microseconds required to rise or fall to this new
2538  * voltage.
2539  */
regulator_set_voltage_time(struct regulator * regulator,int old_uV,int new_uV)2540 int regulator_set_voltage_time(struct regulator *regulator,
2541 			       int old_uV, int new_uV)
2542 {
2543 	struct regulator_dev	*rdev = regulator->rdev;
2544 	struct regulator_ops	*ops = rdev->desc->ops;
2545 	int old_sel = -1;
2546 	int new_sel = -1;
2547 	int voltage;
2548 	int i;
2549 
2550 	/* Currently requires operations to do this */
2551 	if (!ops->list_voltage || !ops->set_voltage_time_sel
2552 	    || !rdev->desc->n_voltages)
2553 		return -EINVAL;
2554 
2555 	for (i = 0; i < rdev->desc->n_voltages; i++) {
2556 		/* We only look for exact voltage matches here */
2557 		voltage = regulator_list_voltage(regulator, i);
2558 		if (voltage < 0)
2559 			return -EINVAL;
2560 		if (voltage == 0)
2561 			continue;
2562 		if (voltage == old_uV)
2563 			old_sel = i;
2564 		if (voltage == new_uV)
2565 			new_sel = i;
2566 	}
2567 
2568 	if (old_sel < 0 || new_sel < 0)
2569 		return -EINVAL;
2570 
2571 	return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
2572 }
2573 EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
2574 
2575 /**
2576  * regulator_set_voltage_time_sel - get raise/fall time
2577  * @rdev: regulator source device
2578  * @old_selector: selector for starting voltage
2579  * @new_selector: selector for target voltage
2580  *
2581  * Provided with the starting and target voltage selectors, this function
2582  * returns time in microseconds required to rise or fall to this new voltage
2583  *
2584  * Drivers providing ramp_delay in regulation_constraints can use this as their
2585  * set_voltage_time_sel() operation.
2586  */
regulator_set_voltage_time_sel(struct regulator_dev * rdev,unsigned int old_selector,unsigned int new_selector)2587 int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
2588 				   unsigned int old_selector,
2589 				   unsigned int new_selector)
2590 {
2591 	unsigned int ramp_delay = 0;
2592 	int old_volt, new_volt;
2593 
2594 	if (rdev->constraints->ramp_delay)
2595 		ramp_delay = rdev->constraints->ramp_delay;
2596 	else if (rdev->desc->ramp_delay)
2597 		ramp_delay = rdev->desc->ramp_delay;
2598 
2599 	if (ramp_delay == 0) {
2600 		rdev_warn(rdev, "ramp_delay not set\n");
2601 		return 0;
2602 	}
2603 
2604 	/* sanity check */
2605 	if (!rdev->desc->ops->list_voltage)
2606 		return -EINVAL;
2607 
2608 	old_volt = rdev->desc->ops->list_voltage(rdev, old_selector);
2609 	new_volt = rdev->desc->ops->list_voltage(rdev, new_selector);
2610 
2611 	return DIV_ROUND_UP(abs(new_volt - old_volt), ramp_delay);
2612 }
2613 EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel);
2614 
2615 /**
2616  * regulator_sync_voltage - re-apply last regulator output voltage
2617  * @regulator: regulator source
2618  *
2619  * Re-apply the last configured voltage.  This is intended to be used
2620  * where some external control source the consumer is cooperating with
2621  * has caused the configured voltage to change.
2622  */
regulator_sync_voltage(struct regulator * regulator)2623 int regulator_sync_voltage(struct regulator *regulator)
2624 {
2625 	struct regulator_dev *rdev = regulator->rdev;
2626 	int ret, min_uV, max_uV;
2627 
2628 	mutex_lock(&rdev->mutex);
2629 
2630 	if (!rdev->desc->ops->set_voltage &&
2631 	    !rdev->desc->ops->set_voltage_sel) {
2632 		ret = -EINVAL;
2633 		goto out;
2634 	}
2635 
2636 	/* This is only going to work if we've had a voltage configured. */
2637 	if (!regulator->min_uV && !regulator->max_uV) {
2638 		ret = -EINVAL;
2639 		goto out;
2640 	}
2641 
2642 	min_uV = regulator->min_uV;
2643 	max_uV = regulator->max_uV;
2644 
2645 	/* This should be a paranoia check... */
2646 	ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2647 	if (ret < 0)
2648 		goto out;
2649 
2650 	ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2651 	if (ret < 0)
2652 		goto out;
2653 
2654 	ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
2655 
2656 out:
2657 	mutex_unlock(&rdev->mutex);
2658 	return ret;
2659 }
2660 EXPORT_SYMBOL_GPL(regulator_sync_voltage);
2661 
_regulator_get_voltage(struct regulator_dev * rdev)2662 static int _regulator_get_voltage(struct regulator_dev *rdev)
2663 {
2664 	int sel, ret;
2665 
2666 	if (rdev->desc->ops->get_voltage_sel) {
2667 		sel = rdev->desc->ops->get_voltage_sel(rdev);
2668 		if (sel < 0)
2669 			return sel;
2670 		ret = rdev->desc->ops->list_voltage(rdev, sel);
2671 	} else if (rdev->desc->ops->get_voltage) {
2672 		ret = rdev->desc->ops->get_voltage(rdev);
2673 	} else if (rdev->desc->ops->list_voltage) {
2674 		ret = rdev->desc->ops->list_voltage(rdev, 0);
2675 	} else {
2676 		return -EINVAL;
2677 	}
2678 
2679 	if (ret < 0)
2680 		return ret;
2681 	return ret - rdev->constraints->uV_offset;
2682 }
2683 
2684 /**
2685  * regulator_get_voltage - get regulator output voltage
2686  * @regulator: regulator source
2687  *
2688  * This returns the current regulator voltage in uV.
2689  *
2690  * NOTE: If the regulator is disabled it will return the voltage value. This
2691  * function should not be used to determine regulator state.
2692  */
regulator_get_voltage(struct regulator * regulator)2693 int regulator_get_voltage(struct regulator *regulator)
2694 {
2695 	int ret;
2696 
2697 	mutex_lock(&regulator->rdev->mutex);
2698 
2699 	ret = _regulator_get_voltage(regulator->rdev);
2700 
2701 	mutex_unlock(&regulator->rdev->mutex);
2702 
2703 	return ret;
2704 }
2705 EXPORT_SYMBOL_GPL(regulator_get_voltage);
2706 
2707 /**
2708  * regulator_set_current_limit - set regulator output current limit
2709  * @regulator: regulator source
2710  * @min_uA: Minimum supported current in uA
2711  * @max_uA: Maximum supported current in uA
2712  *
2713  * Sets current sink to the desired output current. This can be set during
2714  * any regulator state. IOW, regulator can be disabled or enabled.
2715  *
2716  * If the regulator is enabled then the current will change to the new value
2717  * immediately otherwise if the regulator is disabled the regulator will
2718  * output at the new current when enabled.
2719  *
2720  * NOTE: Regulator system constraints must be set for this regulator before
2721  * calling this function otherwise this call will fail.
2722  */
regulator_set_current_limit(struct regulator * regulator,int min_uA,int max_uA)2723 int regulator_set_current_limit(struct regulator *regulator,
2724 			       int min_uA, int max_uA)
2725 {
2726 	struct regulator_dev *rdev = regulator->rdev;
2727 	int ret;
2728 
2729 	mutex_lock(&rdev->mutex);
2730 
2731 	/* sanity check */
2732 	if (!rdev->desc->ops->set_current_limit) {
2733 		ret = -EINVAL;
2734 		goto out;
2735 	}
2736 
2737 	/* constraints check */
2738 	ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
2739 	if (ret < 0)
2740 		goto out;
2741 
2742 	ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
2743 out:
2744 	mutex_unlock(&rdev->mutex);
2745 	return ret;
2746 }
2747 EXPORT_SYMBOL_GPL(regulator_set_current_limit);
2748 
_regulator_get_current_limit(struct regulator_dev * rdev)2749 static int _regulator_get_current_limit(struct regulator_dev *rdev)
2750 {
2751 	int ret;
2752 
2753 	mutex_lock(&rdev->mutex);
2754 
2755 	/* sanity check */
2756 	if (!rdev->desc->ops->get_current_limit) {
2757 		ret = -EINVAL;
2758 		goto out;
2759 	}
2760 
2761 	ret = rdev->desc->ops->get_current_limit(rdev);
2762 out:
2763 	mutex_unlock(&rdev->mutex);
2764 	return ret;
2765 }
2766 
2767 /**
2768  * regulator_get_current_limit - get regulator output current
2769  * @regulator: regulator source
2770  *
2771  * This returns the current supplied by the specified current sink in uA.
2772  *
2773  * NOTE: If the regulator is disabled it will return the current value. This
2774  * function should not be used to determine regulator state.
2775  */
regulator_get_current_limit(struct regulator * regulator)2776 int regulator_get_current_limit(struct regulator *regulator)
2777 {
2778 	return _regulator_get_current_limit(regulator->rdev);
2779 }
2780 EXPORT_SYMBOL_GPL(regulator_get_current_limit);
2781 
2782 /**
2783  * regulator_set_mode - set regulator operating mode
2784  * @regulator: regulator source
2785  * @mode: operating mode - one of the REGULATOR_MODE constants
2786  *
2787  * Set regulator operating mode to increase regulator efficiency or improve
2788  * regulation performance.
2789  *
2790  * NOTE: Regulator system constraints must be set for this regulator before
2791  * calling this function otherwise this call will fail.
2792  */
regulator_set_mode(struct regulator * regulator,unsigned int mode)2793 int regulator_set_mode(struct regulator *regulator, unsigned int mode)
2794 {
2795 	struct regulator_dev *rdev = regulator->rdev;
2796 	int ret;
2797 	int regulator_curr_mode;
2798 
2799 	mutex_lock(&rdev->mutex);
2800 
2801 	/* sanity check */
2802 	if (!rdev->desc->ops->set_mode) {
2803 		ret = -EINVAL;
2804 		goto out;
2805 	}
2806 
2807 	/* return if the same mode is requested */
2808 	if (rdev->desc->ops->get_mode) {
2809 		regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
2810 		if (regulator_curr_mode == mode) {
2811 			ret = 0;
2812 			goto out;
2813 		}
2814 	}
2815 
2816 	/* constraints check */
2817 	ret = regulator_mode_constrain(rdev, &mode);
2818 	if (ret < 0)
2819 		goto out;
2820 
2821 	ret = rdev->desc->ops->set_mode(rdev, mode);
2822 out:
2823 	mutex_unlock(&rdev->mutex);
2824 	return ret;
2825 }
2826 EXPORT_SYMBOL_GPL(regulator_set_mode);
2827 
_regulator_get_mode(struct regulator_dev * rdev)2828 static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
2829 {
2830 	int ret;
2831 
2832 	mutex_lock(&rdev->mutex);
2833 
2834 	/* sanity check */
2835 	if (!rdev->desc->ops->get_mode) {
2836 		ret = -EINVAL;
2837 		goto out;
2838 	}
2839 
2840 	ret = rdev->desc->ops->get_mode(rdev);
2841 out:
2842 	mutex_unlock(&rdev->mutex);
2843 	return ret;
2844 }
2845 
2846 /**
2847  * regulator_get_mode - get regulator operating mode
2848  * @regulator: regulator source
2849  *
2850  * Get the current regulator operating mode.
2851  */
regulator_get_mode(struct regulator * regulator)2852 unsigned int regulator_get_mode(struct regulator *regulator)
2853 {
2854 	return _regulator_get_mode(regulator->rdev);
2855 }
2856 EXPORT_SYMBOL_GPL(regulator_get_mode);
2857 
2858 /**
2859  * regulator_set_optimum_mode - set regulator optimum operating mode
2860  * @regulator: regulator source
2861  * @uA_load: load current
2862  *
2863  * Notifies the regulator core of a new device load. This is then used by
2864  * DRMS (if enabled by constraints) to set the most efficient regulator
2865  * operating mode for the new regulator loading.
2866  *
2867  * Consumer devices notify their supply regulator of the maximum power
2868  * they will require (can be taken from device datasheet in the power
2869  * consumption tables) when they change operational status and hence power
2870  * state. Examples of operational state changes that can affect power
2871  * consumption are :-
2872  *
2873  *    o Device is opened / closed.
2874  *    o Device I/O is about to begin or has just finished.
2875  *    o Device is idling in between work.
2876  *
2877  * This information is also exported via sysfs to userspace.
2878  *
2879  * DRMS will sum the total requested load on the regulator and change
2880  * to the most efficient operating mode if platform constraints allow.
2881  *
2882  * Returns the new regulator mode or error.
2883  */
regulator_set_optimum_mode(struct regulator * regulator,int uA_load)2884 int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
2885 {
2886 	struct regulator_dev *rdev = regulator->rdev;
2887 	struct regulator *consumer;
2888 	int ret, output_uV, input_uV = 0, total_uA_load = 0;
2889 	unsigned int mode;
2890 
2891 	if (rdev->supply)
2892 		input_uV = regulator_get_voltage(rdev->supply);
2893 
2894 	mutex_lock(&rdev->mutex);
2895 
2896 	/*
2897 	 * first check to see if we can set modes at all, otherwise just
2898 	 * tell the consumer everything is OK.
2899 	 */
2900 	regulator->uA_load = uA_load;
2901 	ret = regulator_check_drms(rdev);
2902 	if (ret < 0) {
2903 		ret = 0;
2904 		goto out;
2905 	}
2906 
2907 	if (!rdev->desc->ops->get_optimum_mode)
2908 		goto out;
2909 
2910 	/*
2911 	 * we can actually do this so any errors are indicators of
2912 	 * potential real failure.
2913 	 */
2914 	ret = -EINVAL;
2915 
2916 	if (!rdev->desc->ops->set_mode)
2917 		goto out;
2918 
2919 	/* get output voltage */
2920 	output_uV = _regulator_get_voltage(rdev);
2921 	if (output_uV <= 0) {
2922 		rdev_err(rdev, "invalid output voltage found\n");
2923 		goto out;
2924 	}
2925 
2926 	/* No supply? Use constraint voltage */
2927 	if (input_uV <= 0)
2928 		input_uV = rdev->constraints->input_uV;
2929 	if (input_uV <= 0) {
2930 		rdev_err(rdev, "invalid input voltage found\n");
2931 		goto out;
2932 	}
2933 
2934 	/* calc total requested load for this regulator */
2935 	list_for_each_entry(consumer, &rdev->consumer_list, list)
2936 		total_uA_load += consumer->uA_load;
2937 
2938 	mode = rdev->desc->ops->get_optimum_mode(rdev,
2939 						 input_uV, output_uV,
2940 						 total_uA_load);
2941 	ret = regulator_mode_constrain(rdev, &mode);
2942 	if (ret < 0) {
2943 		rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
2944 			 total_uA_load, input_uV, output_uV);
2945 		goto out;
2946 	}
2947 
2948 	ret = rdev->desc->ops->set_mode(rdev, mode);
2949 	if (ret < 0) {
2950 		rdev_err(rdev, "failed to set optimum mode %x\n", mode);
2951 		goto out;
2952 	}
2953 	ret = mode;
2954 out:
2955 	mutex_unlock(&rdev->mutex);
2956 	return ret;
2957 }
2958 EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
2959 
2960 /**
2961  * regulator_set_bypass_regmap - Default set_bypass() using regmap
2962  *
2963  * @rdev: device to operate on.
2964  * @enable: state to set.
2965  */
regulator_set_bypass_regmap(struct regulator_dev * rdev,bool enable)2966 int regulator_set_bypass_regmap(struct regulator_dev *rdev, bool enable)
2967 {
2968 	unsigned int val;
2969 
2970 	if (enable)
2971 		val = rdev->desc->bypass_mask;
2972 	else
2973 		val = 0;
2974 
2975 	return regmap_update_bits(rdev->regmap, rdev->desc->bypass_reg,
2976 				  rdev->desc->bypass_mask, val);
2977 }
2978 EXPORT_SYMBOL_GPL(regulator_set_bypass_regmap);
2979 
2980 /**
2981  * regulator_get_bypass_regmap - Default get_bypass() using regmap
2982  *
2983  * @rdev: device to operate on.
2984  * @enable: current state.
2985  */
regulator_get_bypass_regmap(struct regulator_dev * rdev,bool * enable)2986 int regulator_get_bypass_regmap(struct regulator_dev *rdev, bool *enable)
2987 {
2988 	unsigned int val;
2989 	int ret;
2990 
2991 	ret = regmap_read(rdev->regmap, rdev->desc->bypass_reg, &val);
2992 	if (ret != 0)
2993 		return ret;
2994 
2995 	*enable = val & rdev->desc->bypass_mask;
2996 
2997 	return 0;
2998 }
2999 EXPORT_SYMBOL_GPL(regulator_get_bypass_regmap);
3000 
3001 /**
3002  * regulator_allow_bypass - allow the regulator to go into bypass mode
3003  *
3004  * @regulator: Regulator to configure
3005  * @enable: enable or disable bypass mode
3006  *
3007  * Allow the regulator to go into bypass mode if all other consumers
3008  * for the regulator also enable bypass mode and the machine
3009  * constraints allow this.  Bypass mode means that the regulator is
3010  * simply passing the input directly to the output with no regulation.
3011  */
regulator_allow_bypass(struct regulator * regulator,bool enable)3012 int regulator_allow_bypass(struct regulator *regulator, bool enable)
3013 {
3014 	struct regulator_dev *rdev = regulator->rdev;
3015 	int ret = 0;
3016 
3017 	if (!rdev->desc->ops->set_bypass)
3018 		return 0;
3019 
3020 	if (rdev->constraints &&
3021 	    !(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_BYPASS))
3022 		return 0;
3023 
3024 	mutex_lock(&rdev->mutex);
3025 
3026 	if (enable && !regulator->bypass) {
3027 		rdev->bypass_count++;
3028 
3029 		if (rdev->bypass_count == rdev->open_count) {
3030 			ret = rdev->desc->ops->set_bypass(rdev, enable);
3031 			if (ret != 0)
3032 				rdev->bypass_count--;
3033 		}
3034 
3035 	} else if (!enable && regulator->bypass) {
3036 		rdev->bypass_count--;
3037 
3038 		if (rdev->bypass_count != rdev->open_count) {
3039 			ret = rdev->desc->ops->set_bypass(rdev, enable);
3040 			if (ret != 0)
3041 				rdev->bypass_count++;
3042 		}
3043 	}
3044 
3045 	if (ret == 0)
3046 		regulator->bypass = enable;
3047 
3048 	mutex_unlock(&rdev->mutex);
3049 
3050 	return ret;
3051 }
3052 EXPORT_SYMBOL_GPL(regulator_allow_bypass);
3053 
3054 /**
3055  * regulator_register_notifier - register regulator event notifier
3056  * @regulator: regulator source
3057  * @nb: notifier block
3058  *
3059  * Register notifier block to receive regulator events.
3060  */
regulator_register_notifier(struct regulator * regulator,struct notifier_block * nb)3061 int regulator_register_notifier(struct regulator *regulator,
3062 			      struct notifier_block *nb)
3063 {
3064 	return blocking_notifier_chain_register(&regulator->rdev->notifier,
3065 						nb);
3066 }
3067 EXPORT_SYMBOL_GPL(regulator_register_notifier);
3068 
3069 /**
3070  * regulator_unregister_notifier - unregister regulator event notifier
3071  * @regulator: regulator source
3072  * @nb: notifier block
3073  *
3074  * Unregister regulator event notifier block.
3075  */
regulator_unregister_notifier(struct regulator * regulator,struct notifier_block * nb)3076 int regulator_unregister_notifier(struct regulator *regulator,
3077 				struct notifier_block *nb)
3078 {
3079 	return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
3080 						  nb);
3081 }
3082 EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
3083 
3084 /* notify regulator consumers and downstream regulator consumers.
3085  * Note mutex must be held by caller.
3086  */
_notifier_call_chain(struct regulator_dev * rdev,unsigned long event,void * data)3087 static void _notifier_call_chain(struct regulator_dev *rdev,
3088 				  unsigned long event, void *data)
3089 {
3090 	/* call rdev chain first */
3091 	blocking_notifier_call_chain(&rdev->notifier, event, data);
3092 }
3093 
3094 /**
3095  * regulator_bulk_get - get multiple regulator consumers
3096  *
3097  * @dev:           Device to supply
3098  * @num_consumers: Number of consumers to register
3099  * @consumers:     Configuration of consumers; clients are stored here.
3100  *
3101  * @return 0 on success, an errno on failure.
3102  *
3103  * This helper function allows drivers to get several regulator
3104  * consumers in one operation.  If any of the regulators cannot be
3105  * acquired then any regulators that were allocated will be freed
3106  * before returning to the caller.
3107  */
regulator_bulk_get(struct device * dev,int num_consumers,struct regulator_bulk_data * consumers)3108 int regulator_bulk_get(struct device *dev, int num_consumers,
3109 		       struct regulator_bulk_data *consumers)
3110 {
3111 	int i;
3112 	int ret;
3113 
3114 	for (i = 0; i < num_consumers; i++)
3115 		consumers[i].consumer = NULL;
3116 
3117 	for (i = 0; i < num_consumers; i++) {
3118 		consumers[i].consumer = regulator_get(dev,
3119 						      consumers[i].supply);
3120 		if (IS_ERR(consumers[i].consumer)) {
3121 			ret = PTR_ERR(consumers[i].consumer);
3122 			dev_err(dev, "Failed to get supply '%s': %d\n",
3123 				consumers[i].supply, ret);
3124 			consumers[i].consumer = NULL;
3125 			goto err;
3126 		}
3127 	}
3128 
3129 	return 0;
3130 
3131 err:
3132 	while (--i >= 0)
3133 		regulator_put(consumers[i].consumer);
3134 
3135 	return ret;
3136 }
3137 EXPORT_SYMBOL_GPL(regulator_bulk_get);
3138 
3139 /**
3140  * devm_regulator_bulk_get - managed get multiple regulator consumers
3141  *
3142  * @dev:           Device to supply
3143  * @num_consumers: Number of consumers to register
3144  * @consumers:     Configuration of consumers; clients are stored here.
3145  *
3146  * @return 0 on success, an errno on failure.
3147  *
3148  * This helper function allows drivers to get several regulator
3149  * consumers in one operation with management, the regulators will
3150  * automatically be freed when the device is unbound.  If any of the
3151  * regulators cannot be acquired then any regulators that were
3152  * allocated will be freed before returning to the caller.
3153  */
devm_regulator_bulk_get(struct device * dev,int num_consumers,struct regulator_bulk_data * consumers)3154 int devm_regulator_bulk_get(struct device *dev, int num_consumers,
3155 			    struct regulator_bulk_data *consumers)
3156 {
3157 	int i;
3158 	int ret;
3159 
3160 	for (i = 0; i < num_consumers; i++)
3161 		consumers[i].consumer = NULL;
3162 
3163 	for (i = 0; i < num_consumers; i++) {
3164 		consumers[i].consumer = devm_regulator_get(dev,
3165 							   consumers[i].supply);
3166 		if (IS_ERR(consumers[i].consumer)) {
3167 			ret = PTR_ERR(consumers[i].consumer);
3168 			dev_err(dev, "Failed to get supply '%s': %d\n",
3169 				consumers[i].supply, ret);
3170 			consumers[i].consumer = NULL;
3171 			goto err;
3172 		}
3173 	}
3174 
3175 	return 0;
3176 
3177 err:
3178 	for (i = 0; i < num_consumers && consumers[i].consumer; i++)
3179 		devm_regulator_put(consumers[i].consumer);
3180 
3181 	return ret;
3182 }
3183 EXPORT_SYMBOL_GPL(devm_regulator_bulk_get);
3184 
regulator_bulk_enable_async(void * data,async_cookie_t cookie)3185 static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
3186 {
3187 	struct regulator_bulk_data *bulk = data;
3188 
3189 	bulk->ret = regulator_enable(bulk->consumer);
3190 }
3191 
3192 /**
3193  * regulator_bulk_enable - enable multiple regulator consumers
3194  *
3195  * @num_consumers: Number of consumers
3196  * @consumers:     Consumer data; clients are stored here.
3197  * @return         0 on success, an errno on failure
3198  *
3199  * This convenience API allows consumers to enable multiple regulator
3200  * clients in a single API call.  If any consumers cannot be enabled
3201  * then any others that were enabled will be disabled again prior to
3202  * return.
3203  */
regulator_bulk_enable(int num_consumers,struct regulator_bulk_data * consumers)3204 int regulator_bulk_enable(int num_consumers,
3205 			  struct regulator_bulk_data *consumers)
3206 {
3207 	ASYNC_DOMAIN_EXCLUSIVE(async_domain);
3208 	int i;
3209 	int ret = 0;
3210 
3211 	for (i = 0; i < num_consumers; i++) {
3212 		if (consumers[i].consumer->always_on)
3213 			consumers[i].ret = 0;
3214 		else
3215 			async_schedule_domain(regulator_bulk_enable_async,
3216 					      &consumers[i], &async_domain);
3217 	}
3218 
3219 	async_synchronize_full_domain(&async_domain);
3220 
3221 	/* If any consumer failed we need to unwind any that succeeded */
3222 	for (i = 0; i < num_consumers; i++) {
3223 		if (consumers[i].ret != 0) {
3224 			ret = consumers[i].ret;
3225 			goto err;
3226 		}
3227 	}
3228 
3229 	return 0;
3230 
3231 err:
3232 	for (i = 0; i < num_consumers; i++) {
3233 		if (consumers[i].ret < 0)
3234 			pr_err("Failed to enable %s: %d\n", consumers[i].supply,
3235 			       consumers[i].ret);
3236 		else
3237 			regulator_disable(consumers[i].consumer);
3238 	}
3239 
3240 	return ret;
3241 }
3242 EXPORT_SYMBOL_GPL(regulator_bulk_enable);
3243 
3244 /**
3245  * regulator_bulk_disable - disable multiple regulator consumers
3246  *
3247  * @num_consumers: Number of consumers
3248  * @consumers:     Consumer data; clients are stored here.
3249  * @return         0 on success, an errno on failure
3250  *
3251  * This convenience API allows consumers to disable multiple regulator
3252  * clients in a single API call.  If any consumers cannot be disabled
3253  * then any others that were disabled will be enabled again prior to
3254  * return.
3255  */
regulator_bulk_disable(int num_consumers,struct regulator_bulk_data * consumers)3256 int regulator_bulk_disable(int num_consumers,
3257 			   struct regulator_bulk_data *consumers)
3258 {
3259 	int i;
3260 	int ret, r;
3261 
3262 	for (i = num_consumers - 1; i >= 0; --i) {
3263 		ret = regulator_disable(consumers[i].consumer);
3264 		if (ret != 0)
3265 			goto err;
3266 	}
3267 
3268 	return 0;
3269 
3270 err:
3271 	pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
3272 	for (++i; i < num_consumers; ++i) {
3273 		r = regulator_enable(consumers[i].consumer);
3274 		if (r != 0)
3275 			pr_err("Failed to reename %s: %d\n",
3276 			       consumers[i].supply, r);
3277 	}
3278 
3279 	return ret;
3280 }
3281 EXPORT_SYMBOL_GPL(regulator_bulk_disable);
3282 
3283 /**
3284  * regulator_bulk_force_disable - force disable multiple regulator consumers
3285  *
3286  * @num_consumers: Number of consumers
3287  * @consumers:     Consumer data; clients are stored here.
3288  * @return         0 on success, an errno on failure
3289  *
3290  * This convenience API allows consumers to forcibly disable multiple regulator
3291  * clients in a single API call.
3292  * NOTE: This should be used for situations when device damage will
3293  * likely occur if the regulators are not disabled (e.g. over temp).
3294  * Although regulator_force_disable function call for some consumers can
3295  * return error numbers, the function is called for all consumers.
3296  */
regulator_bulk_force_disable(int num_consumers,struct regulator_bulk_data * consumers)3297 int regulator_bulk_force_disable(int num_consumers,
3298 			   struct regulator_bulk_data *consumers)
3299 {
3300 	int i;
3301 	int ret;
3302 
3303 	for (i = 0; i < num_consumers; i++)
3304 		consumers[i].ret =
3305 			    regulator_force_disable(consumers[i].consumer);
3306 
3307 	for (i = 0; i < num_consumers; i++) {
3308 		if (consumers[i].ret != 0) {
3309 			ret = consumers[i].ret;
3310 			goto out;
3311 		}
3312 	}
3313 
3314 	return 0;
3315 out:
3316 	return ret;
3317 }
3318 EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
3319 
3320 /**
3321  * regulator_bulk_free - free multiple regulator consumers
3322  *
3323  * @num_consumers: Number of consumers
3324  * @consumers:     Consumer data; clients are stored here.
3325  *
3326  * This convenience API allows consumers to free multiple regulator
3327  * clients in a single API call.
3328  */
regulator_bulk_free(int num_consumers,struct regulator_bulk_data * consumers)3329 void regulator_bulk_free(int num_consumers,
3330 			 struct regulator_bulk_data *consumers)
3331 {
3332 	int i;
3333 
3334 	for (i = 0; i < num_consumers; i++) {
3335 		regulator_put(consumers[i].consumer);
3336 		consumers[i].consumer = NULL;
3337 	}
3338 }
3339 EXPORT_SYMBOL_GPL(regulator_bulk_free);
3340 
3341 /**
3342  * regulator_notifier_call_chain - call regulator event notifier
3343  * @rdev: regulator source
3344  * @event: notifier block
3345  * @data: callback-specific data.
3346  *
3347  * Called by regulator drivers to notify clients a regulator event has
3348  * occurred. We also notify regulator clients downstream.
3349  * Note lock must be held by caller.
3350  */
regulator_notifier_call_chain(struct regulator_dev * rdev,unsigned long event,void * data)3351 int regulator_notifier_call_chain(struct regulator_dev *rdev,
3352 				  unsigned long event, void *data)
3353 {
3354 	_notifier_call_chain(rdev, event, data);
3355 	return NOTIFY_DONE;
3356 
3357 }
3358 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
3359 
3360 /**
3361  * regulator_mode_to_status - convert a regulator mode into a status
3362  *
3363  * @mode: Mode to convert
3364  *
3365  * Convert a regulator mode into a status.
3366  */
regulator_mode_to_status(unsigned int mode)3367 int regulator_mode_to_status(unsigned int mode)
3368 {
3369 	switch (mode) {
3370 	case REGULATOR_MODE_FAST:
3371 		return REGULATOR_STATUS_FAST;
3372 	case REGULATOR_MODE_NORMAL:
3373 		return REGULATOR_STATUS_NORMAL;
3374 	case REGULATOR_MODE_IDLE:
3375 		return REGULATOR_STATUS_IDLE;
3376 	case REGULATOR_MODE_STANDBY:
3377 		return REGULATOR_STATUS_STANDBY;
3378 	default:
3379 		return REGULATOR_STATUS_UNDEFINED;
3380 	}
3381 }
3382 EXPORT_SYMBOL_GPL(regulator_mode_to_status);
3383 
3384 /*
3385  * To avoid cluttering sysfs (and memory) with useless state, only
3386  * create attributes that can be meaningfully displayed.
3387  */
add_regulator_attributes(struct regulator_dev * rdev)3388 static int add_regulator_attributes(struct regulator_dev *rdev)
3389 {
3390 	struct device		*dev = &rdev->dev;
3391 	struct regulator_ops	*ops = rdev->desc->ops;
3392 	int			status = 0;
3393 
3394 	/* some attributes need specific methods to be displayed */
3395 	if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
3396 	    (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) ||
3397 	    (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0)) {
3398 		status = device_create_file(dev, &dev_attr_microvolts);
3399 		if (status < 0)
3400 			return status;
3401 	}
3402 	if (ops->get_current_limit) {
3403 		status = device_create_file(dev, &dev_attr_microamps);
3404 		if (status < 0)
3405 			return status;
3406 	}
3407 	if (ops->get_mode) {
3408 		status = device_create_file(dev, &dev_attr_opmode);
3409 		if (status < 0)
3410 			return status;
3411 	}
3412 	if (rdev->ena_pin || ops->is_enabled) {
3413 		status = device_create_file(dev, &dev_attr_state);
3414 		if (status < 0)
3415 			return status;
3416 	}
3417 	if (ops->get_status) {
3418 		status = device_create_file(dev, &dev_attr_status);
3419 		if (status < 0)
3420 			return status;
3421 	}
3422 	if (ops->get_bypass) {
3423 		status = device_create_file(dev, &dev_attr_bypass);
3424 		if (status < 0)
3425 			return status;
3426 	}
3427 
3428 	/* some attributes are type-specific */
3429 	if (rdev->desc->type == REGULATOR_CURRENT) {
3430 		status = device_create_file(dev, &dev_attr_requested_microamps);
3431 		if (status < 0)
3432 			return status;
3433 	}
3434 
3435 	/* all the other attributes exist to support constraints;
3436 	 * don't show them if there are no constraints, or if the
3437 	 * relevant supporting methods are missing.
3438 	 */
3439 	if (!rdev->constraints)
3440 		return status;
3441 
3442 	/* constraints need specific supporting methods */
3443 	if (ops->set_voltage || ops->set_voltage_sel) {
3444 		status = device_create_file(dev, &dev_attr_min_microvolts);
3445 		if (status < 0)
3446 			return status;
3447 		status = device_create_file(dev, &dev_attr_max_microvolts);
3448 		if (status < 0)
3449 			return status;
3450 	}
3451 	if (ops->set_current_limit) {
3452 		status = device_create_file(dev, &dev_attr_min_microamps);
3453 		if (status < 0)
3454 			return status;
3455 		status = device_create_file(dev, &dev_attr_max_microamps);
3456 		if (status < 0)
3457 			return status;
3458 	}
3459 
3460 	status = device_create_file(dev, &dev_attr_suspend_standby_state);
3461 	if (status < 0)
3462 		return status;
3463 	status = device_create_file(dev, &dev_attr_suspend_mem_state);
3464 	if (status < 0)
3465 		return status;
3466 	status = device_create_file(dev, &dev_attr_suspend_disk_state);
3467 	if (status < 0)
3468 		return status;
3469 
3470 	if (ops->set_suspend_voltage) {
3471 		status = device_create_file(dev,
3472 				&dev_attr_suspend_standby_microvolts);
3473 		if (status < 0)
3474 			return status;
3475 		status = device_create_file(dev,
3476 				&dev_attr_suspend_mem_microvolts);
3477 		if (status < 0)
3478 			return status;
3479 		status = device_create_file(dev,
3480 				&dev_attr_suspend_disk_microvolts);
3481 		if (status < 0)
3482 			return status;
3483 	}
3484 
3485 	if (ops->set_suspend_mode) {
3486 		status = device_create_file(dev,
3487 				&dev_attr_suspend_standby_mode);
3488 		if (status < 0)
3489 			return status;
3490 		status = device_create_file(dev,
3491 				&dev_attr_suspend_mem_mode);
3492 		if (status < 0)
3493 			return status;
3494 		status = device_create_file(dev,
3495 				&dev_attr_suspend_disk_mode);
3496 		if (status < 0)
3497 			return status;
3498 	}
3499 
3500 	return status;
3501 }
3502 
rdev_init_debugfs(struct regulator_dev * rdev)3503 static void rdev_init_debugfs(struct regulator_dev *rdev)
3504 {
3505 	rdev->debugfs = debugfs_create_dir(rdev_get_name(rdev), debugfs_root);
3506 	if (!rdev->debugfs) {
3507 		rdev_warn(rdev, "Failed to create debugfs directory\n");
3508 		return;
3509 	}
3510 
3511 	debugfs_create_u32("use_count", 0444, rdev->debugfs,
3512 			   &rdev->use_count);
3513 	debugfs_create_u32("open_count", 0444, rdev->debugfs,
3514 			   &rdev->open_count);
3515 	debugfs_create_u32("bypass_count", 0444, rdev->debugfs,
3516 			   &rdev->bypass_count);
3517 }
3518 
3519 /**
3520  * regulator_register - register regulator
3521  * @regulator_desc: regulator to register
3522  * @config: runtime configuration for regulator
3523  *
3524  * Called by regulator drivers to register a regulator.
3525  * Returns a valid pointer to struct regulator_dev on success
3526  * or an ERR_PTR() on error.
3527  */
3528 struct regulator_dev *
regulator_register(const struct regulator_desc * regulator_desc,const struct regulator_config * config)3529 regulator_register(const struct regulator_desc *regulator_desc,
3530 		   const struct regulator_config *config)
3531 {
3532 	const struct regulation_constraints *constraints = NULL;
3533 	const struct regulator_init_data *init_data;
3534 	static atomic_t regulator_no = ATOMIC_INIT(0);
3535 	struct regulator_dev *rdev;
3536 	struct device *dev;
3537 	int ret, i;
3538 	const char *supply = NULL;
3539 
3540 	if (regulator_desc == NULL || config == NULL)
3541 		return ERR_PTR(-EINVAL);
3542 
3543 	dev = config->dev;
3544 	WARN_ON(!dev);
3545 
3546 	if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
3547 		return ERR_PTR(-EINVAL);
3548 
3549 	if (regulator_desc->type != REGULATOR_VOLTAGE &&
3550 	    regulator_desc->type != REGULATOR_CURRENT)
3551 		return ERR_PTR(-EINVAL);
3552 
3553 	/* Only one of each should be implemented */
3554 	WARN_ON(regulator_desc->ops->get_voltage &&
3555 		regulator_desc->ops->get_voltage_sel);
3556 	WARN_ON(regulator_desc->ops->set_voltage &&
3557 		regulator_desc->ops->set_voltage_sel);
3558 
3559 	/* If we're using selectors we must implement list_voltage. */
3560 	if (regulator_desc->ops->get_voltage_sel &&
3561 	    !regulator_desc->ops->list_voltage) {
3562 		return ERR_PTR(-EINVAL);
3563 	}
3564 	if (regulator_desc->ops->set_voltage_sel &&
3565 	    !regulator_desc->ops->list_voltage) {
3566 		return ERR_PTR(-EINVAL);
3567 	}
3568 
3569 	init_data = config->init_data;
3570 
3571 	rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
3572 	if (rdev == NULL)
3573 		return ERR_PTR(-ENOMEM);
3574 
3575 	mutex_lock(&regulator_list_mutex);
3576 
3577 	mutex_init(&rdev->mutex);
3578 	rdev->reg_data = config->driver_data;
3579 	rdev->owner = regulator_desc->owner;
3580 	rdev->desc = regulator_desc;
3581 	if (config->regmap)
3582 		rdev->regmap = config->regmap;
3583 	else if (dev_get_regmap(dev, NULL))
3584 		rdev->regmap = dev_get_regmap(dev, NULL);
3585 	else if (dev->parent)
3586 		rdev->regmap = dev_get_regmap(dev->parent, NULL);
3587 	INIT_LIST_HEAD(&rdev->consumer_list);
3588 	INIT_LIST_HEAD(&rdev->list);
3589 	BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
3590 	INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
3591 
3592 	/* preform any regulator specific init */
3593 	if (init_data && init_data->regulator_init) {
3594 		ret = init_data->regulator_init(rdev->reg_data);
3595 		if (ret < 0)
3596 			goto clean;
3597 	}
3598 
3599 	/* register with sysfs */
3600 	rdev->dev.class = &regulator_class;
3601 	rdev->dev.of_node = config->of_node;
3602 	rdev->dev.parent = dev;
3603 	dev_set_name(&rdev->dev, "regulator.%d",
3604 		     atomic_inc_return(&regulator_no) - 1);
3605 	ret = device_register(&rdev->dev);
3606 	if (ret != 0) {
3607 		put_device(&rdev->dev);
3608 		goto clean;
3609 	}
3610 
3611 	dev_set_drvdata(&rdev->dev, rdev);
3612 
3613 	if (config->ena_gpio && gpio_is_valid(config->ena_gpio)) {
3614 		ret = regulator_ena_gpio_request(rdev, config);
3615 		if (ret != 0) {
3616 			rdev_err(rdev, "Failed to request enable GPIO%d: %d\n",
3617 				 config->ena_gpio, ret);
3618 			goto wash;
3619 		}
3620 
3621 		if (config->ena_gpio_flags & GPIOF_OUT_INIT_HIGH)
3622 			rdev->ena_gpio_state = 1;
3623 
3624 		if (config->ena_gpio_invert)
3625 			rdev->ena_gpio_state = !rdev->ena_gpio_state;
3626 	}
3627 
3628 	/* set regulator constraints */
3629 	if (init_data)
3630 		constraints = &init_data->constraints;
3631 
3632 	ret = set_machine_constraints(rdev, constraints);
3633 	if (ret < 0)
3634 		goto scrub;
3635 
3636 	/* add attributes supported by this regulator */
3637 	ret = add_regulator_attributes(rdev);
3638 	if (ret < 0)
3639 		goto scrub;
3640 
3641 	if (init_data && init_data->supply_regulator)
3642 		supply = init_data->supply_regulator;
3643 	else if (regulator_desc->supply_name)
3644 		supply = regulator_desc->supply_name;
3645 
3646 	if (supply) {
3647 		struct regulator_dev *r;
3648 
3649 		r = regulator_dev_lookup(dev, supply, &ret);
3650 
3651 		if (ret == -ENODEV) {
3652 			/*
3653 			 * No supply was specified for this regulator and
3654 			 * there will never be one.
3655 			 */
3656 			ret = 0;
3657 			goto add_dev;
3658 		} else if (!r) {
3659 			dev_err(dev, "Failed to find supply %s\n", supply);
3660 			ret = -EPROBE_DEFER;
3661 			goto scrub;
3662 		}
3663 
3664 		ret = set_supply(rdev, r);
3665 		if (ret < 0)
3666 			goto scrub;
3667 
3668 		/* Enable supply if rail is enabled */
3669 		if (_regulator_is_enabled(rdev)) {
3670 			ret = regulator_enable(rdev->supply);
3671 			if (ret < 0)
3672 				goto scrub;
3673 		}
3674 	}
3675 
3676 add_dev:
3677 	/* add consumers devices */
3678 	if (init_data) {
3679 		for (i = 0; i < init_data->num_consumer_supplies; i++) {
3680 			ret = set_consumer_device_supply(rdev,
3681 				init_data->consumer_supplies[i].dev_name,
3682 				init_data->consumer_supplies[i].supply);
3683 			if (ret < 0) {
3684 				dev_err(dev, "Failed to set supply %s\n",
3685 					init_data->consumer_supplies[i].supply);
3686 				goto unset_supplies;
3687 			}
3688 		}
3689 	}
3690 
3691 	list_add(&rdev->list, &regulator_list);
3692 
3693 	rdev_init_debugfs(rdev);
3694 out:
3695 	mutex_unlock(&regulator_list_mutex);
3696 	return rdev;
3697 
3698 unset_supplies:
3699 	unset_regulator_supplies(rdev);
3700 
3701 scrub:
3702 	if (rdev->supply)
3703 		_regulator_put(rdev->supply);
3704 	regulator_ena_gpio_free(rdev);
3705 	kfree(rdev->constraints);
3706 wash:
3707 	device_unregister(&rdev->dev);
3708 	/* device core frees rdev */
3709 	rdev = ERR_PTR(ret);
3710 	goto out;
3711 
3712 clean:
3713 	kfree(rdev);
3714 	rdev = ERR_PTR(ret);
3715 	goto out;
3716 }
3717 EXPORT_SYMBOL_GPL(regulator_register);
3718 
3719 /**
3720  * regulator_unregister - unregister regulator
3721  * @rdev: regulator to unregister
3722  *
3723  * Called by regulator drivers to unregister a regulator.
3724  */
regulator_unregister(struct regulator_dev * rdev)3725 void regulator_unregister(struct regulator_dev *rdev)
3726 {
3727 	if (rdev == NULL)
3728 		return;
3729 
3730 	if (rdev->supply)
3731 		regulator_put(rdev->supply);
3732 	mutex_lock(&regulator_list_mutex);
3733 	debugfs_remove_recursive(rdev->debugfs);
3734 	flush_work(&rdev->disable_work.work);
3735 	WARN_ON(rdev->open_count);
3736 	unset_regulator_supplies(rdev);
3737 	list_del(&rdev->list);
3738 	kfree(rdev->constraints);
3739 	regulator_ena_gpio_free(rdev);
3740 	device_unregister(&rdev->dev);
3741 	mutex_unlock(&regulator_list_mutex);
3742 }
3743 EXPORT_SYMBOL_GPL(regulator_unregister);
3744 
3745 /**
3746  * regulator_suspend_prepare - prepare regulators for system wide suspend
3747  * @state: system suspend state
3748  *
3749  * Configure each regulator with it's suspend operating parameters for state.
3750  * This will usually be called by machine suspend code prior to supending.
3751  */
regulator_suspend_prepare(suspend_state_t state)3752 int regulator_suspend_prepare(suspend_state_t state)
3753 {
3754 	struct regulator_dev *rdev;
3755 	int ret = 0;
3756 
3757 	/* ON is handled by regulator active state */
3758 	if (state == PM_SUSPEND_ON)
3759 		return -EINVAL;
3760 
3761 	mutex_lock(&regulator_list_mutex);
3762 	list_for_each_entry(rdev, &regulator_list, list) {
3763 
3764 		mutex_lock(&rdev->mutex);
3765 		ret = suspend_prepare(rdev, state);
3766 		mutex_unlock(&rdev->mutex);
3767 
3768 		if (ret < 0) {
3769 			rdev_err(rdev, "failed to prepare\n");
3770 			goto out;
3771 		}
3772 	}
3773 out:
3774 	mutex_unlock(&regulator_list_mutex);
3775 	return ret;
3776 }
3777 EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
3778 
3779 /**
3780  * regulator_suspend_finish - resume regulators from system wide suspend
3781  *
3782  * Turn on regulators that might be turned off by regulator_suspend_prepare
3783  * and that should be turned on according to the regulators properties.
3784  */
regulator_suspend_finish(void)3785 int regulator_suspend_finish(void)
3786 {
3787 	struct regulator_dev *rdev;
3788 	int ret = 0, error;
3789 
3790 	mutex_lock(&regulator_list_mutex);
3791 	list_for_each_entry(rdev, &regulator_list, list) {
3792 		struct regulator_ops *ops = rdev->desc->ops;
3793 
3794 		mutex_lock(&rdev->mutex);
3795 		if ((rdev->use_count > 0  || rdev->constraints->always_on) &&
3796 				ops->enable) {
3797 			error = ops->enable(rdev);
3798 			if (error)
3799 				ret = error;
3800 		} else {
3801 			if (!has_full_constraints)
3802 				goto unlock;
3803 			if (!ops->disable)
3804 				goto unlock;
3805 			if (!_regulator_is_enabled(rdev))
3806 				goto unlock;
3807 
3808 			error = ops->disable(rdev);
3809 			if (error)
3810 				ret = error;
3811 		}
3812 unlock:
3813 		mutex_unlock(&rdev->mutex);
3814 	}
3815 	mutex_unlock(&regulator_list_mutex);
3816 	return ret;
3817 }
3818 EXPORT_SYMBOL_GPL(regulator_suspend_finish);
3819 
3820 /**
3821  * regulator_has_full_constraints - the system has fully specified constraints
3822  *
3823  * Calling this function will cause the regulator API to disable all
3824  * regulators which have a zero use count and don't have an always_on
3825  * constraint in a late_initcall.
3826  *
3827  * The intention is that this will become the default behaviour in a
3828  * future kernel release so users are encouraged to use this facility
3829  * now.
3830  */
regulator_has_full_constraints(void)3831 void regulator_has_full_constraints(void)
3832 {
3833 	has_full_constraints = 1;
3834 }
3835 EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
3836 
3837 /**
3838  * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
3839  *
3840  * Calling this function will cause the regulator API to provide a
3841  * dummy regulator to consumers if no physical regulator is found,
3842  * allowing most consumers to proceed as though a regulator were
3843  * configured.  This allows systems such as those with software
3844  * controllable regulators for the CPU core only to be brought up more
3845  * readily.
3846  */
regulator_use_dummy_regulator(void)3847 void regulator_use_dummy_regulator(void)
3848 {
3849 	board_wants_dummy_regulator = true;
3850 }
3851 EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator);
3852 
3853 /**
3854  * rdev_get_drvdata - get rdev regulator driver data
3855  * @rdev: regulator
3856  *
3857  * Get rdev regulator driver private data. This call can be used in the
3858  * regulator driver context.
3859  */
rdev_get_drvdata(struct regulator_dev * rdev)3860 void *rdev_get_drvdata(struct regulator_dev *rdev)
3861 {
3862 	return rdev->reg_data;
3863 }
3864 EXPORT_SYMBOL_GPL(rdev_get_drvdata);
3865 
3866 /**
3867  * regulator_get_drvdata - get regulator driver data
3868  * @regulator: regulator
3869  *
3870  * Get regulator driver private data. This call can be used in the consumer
3871  * driver context when non API regulator specific functions need to be called.
3872  */
regulator_get_drvdata(struct regulator * regulator)3873 void *regulator_get_drvdata(struct regulator *regulator)
3874 {
3875 	return regulator->rdev->reg_data;
3876 }
3877 EXPORT_SYMBOL_GPL(regulator_get_drvdata);
3878 
3879 /**
3880  * regulator_set_drvdata - set regulator driver data
3881  * @regulator: regulator
3882  * @data: data
3883  */
regulator_set_drvdata(struct regulator * regulator,void * data)3884 void regulator_set_drvdata(struct regulator *regulator, void *data)
3885 {
3886 	regulator->rdev->reg_data = data;
3887 }
3888 EXPORT_SYMBOL_GPL(regulator_set_drvdata);
3889 
3890 /**
3891  * regulator_get_id - get regulator ID
3892  * @rdev: regulator
3893  */
rdev_get_id(struct regulator_dev * rdev)3894 int rdev_get_id(struct regulator_dev *rdev)
3895 {
3896 	return rdev->desc->id;
3897 }
3898 EXPORT_SYMBOL_GPL(rdev_get_id);
3899 
rdev_get_dev(struct regulator_dev * rdev)3900 struct device *rdev_get_dev(struct regulator_dev *rdev)
3901 {
3902 	return &rdev->dev;
3903 }
3904 EXPORT_SYMBOL_GPL(rdev_get_dev);
3905 
regulator_get_init_drvdata(struct regulator_init_data * reg_init_data)3906 void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
3907 {
3908 	return reg_init_data->driver_data;
3909 }
3910 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
3911 
3912 #ifdef CONFIG_DEBUG_FS
supply_map_read_file(struct file * file,char __user * user_buf,size_t count,loff_t * ppos)3913 static ssize_t supply_map_read_file(struct file *file, char __user *user_buf,
3914 				    size_t count, loff_t *ppos)
3915 {
3916 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3917 	ssize_t len, ret = 0;
3918 	struct regulator_map *map;
3919 
3920 	if (!buf)
3921 		return -ENOMEM;
3922 
3923 	list_for_each_entry(map, &regulator_map_list, list) {
3924 		len = snprintf(buf + ret, PAGE_SIZE - ret,
3925 			       "%s -> %s.%s\n",
3926 			       rdev_get_name(map->regulator), map->dev_name,
3927 			       map->supply);
3928 		if (len >= 0)
3929 			ret += len;
3930 		if (ret > PAGE_SIZE) {
3931 			ret = PAGE_SIZE;
3932 			break;
3933 		}
3934 	}
3935 
3936 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
3937 
3938 	kfree(buf);
3939 
3940 	return ret;
3941 }
3942 #endif
3943 
3944 static const struct file_operations supply_map_fops = {
3945 #ifdef CONFIG_DEBUG_FS
3946 	.read = supply_map_read_file,
3947 	.llseek = default_llseek,
3948 #endif
3949 };
3950 
regulator_init(void)3951 static int __init regulator_init(void)
3952 {
3953 	int ret;
3954 
3955 	ret = class_register(&regulator_class);
3956 
3957 	debugfs_root = debugfs_create_dir("regulator", NULL);
3958 	if (!debugfs_root)
3959 		pr_warn("regulator: Failed to create debugfs directory\n");
3960 
3961 	debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
3962 			    &supply_map_fops);
3963 
3964 	regulator_dummy_init();
3965 
3966 	return ret;
3967 }
3968 
3969 /* init early to allow our consumers to complete system booting */
3970 core_initcall(regulator_init);
3971 
regulator_init_complete(void)3972 static int __init regulator_init_complete(void)
3973 {
3974 	struct regulator_dev *rdev;
3975 	struct regulator_ops *ops;
3976 	struct regulation_constraints *c;
3977 	int enabled, ret;
3978 
3979 	/*
3980 	 * Since DT doesn't provide an idiomatic mechanism for
3981 	 * enabling full constraints and since it's much more natural
3982 	 * with DT to provide them just assume that a DT enabled
3983 	 * system has full constraints.
3984 	 */
3985 	if (of_have_populated_dt())
3986 		has_full_constraints = true;
3987 
3988 	mutex_lock(&regulator_list_mutex);
3989 
3990 	/* If we have a full configuration then disable any regulators
3991 	 * which are not in use or always_on.  This will become the
3992 	 * default behaviour in the future.
3993 	 */
3994 	list_for_each_entry(rdev, &regulator_list, list) {
3995 		ops = rdev->desc->ops;
3996 		c = rdev->constraints;
3997 
3998 		if (!ops->disable || (c && c->always_on))
3999 			continue;
4000 
4001 		mutex_lock(&rdev->mutex);
4002 
4003 		if (rdev->use_count)
4004 			goto unlock;
4005 
4006 		/* If we can't read the status assume it's on. */
4007 		if (ops->is_enabled)
4008 			enabled = ops->is_enabled(rdev);
4009 		else
4010 			enabled = 1;
4011 
4012 		if (!enabled)
4013 			goto unlock;
4014 
4015 		if (has_full_constraints) {
4016 			/* We log since this may kill the system if it
4017 			 * goes wrong. */
4018 			rdev_info(rdev, "disabling\n");
4019 			ret = ops->disable(rdev);
4020 			if (ret != 0) {
4021 				rdev_err(rdev, "couldn't disable: %d\n", ret);
4022 			}
4023 		} else {
4024 			/* The intention is that in future we will
4025 			 * assume that full constraints are provided
4026 			 * so warn even if we aren't going to do
4027 			 * anything here.
4028 			 */
4029 			rdev_warn(rdev, "incomplete constraints, leaving on\n");
4030 		}
4031 
4032 unlock:
4033 		mutex_unlock(&rdev->mutex);
4034 	}
4035 
4036 	mutex_unlock(&regulator_list_mutex);
4037 
4038 	return 0;
4039 }
4040 late_initcall(regulator_init_complete);
4041