core.c 98.3 KB
Newer Older
1
2
3
4
/*
 * core.c  --  Voltage/Current Regulator framework.
 *
 * Copyright 2007, 2008 Wolfson Microelectronics PLC.
5
 * Copyright 2008 SlimLogic Ltd.
6
 *
7
 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
8
9
10
11
12
13
14
15
16
17
 *
 *  This program is free software; you can redistribute  it and/or modify it
 *  under  the terms of  the GNU General  Public License as published by the
 *  Free Software Foundation;  either version 2 of the  License, or (at your
 *  option) any later version.
 *
 */

#include <linux/kernel.h>
#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
23
24
#include <linux/err.h>
#include <linux/mutex.h>
#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
31
32
#include <linux/regulator/consumer.h>
#include <linux/regulator/driver.h>
#include <linux/regulator/machine.h>
33
#include <linux/module.h>
34

35
36
37
#define CREATE_TRACE_POINTS
#include <trace/events/regulator.h>

38
#include "dummy.h"
39
#include "internal.h"
40

Mark Brown's avatar
Mark Brown committed
41
42
#define rdev_crit(rdev, fmt, ...)					\
	pr_crit("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
43
44
45
46
47
48
49
50
51
#define rdev_err(rdev, fmt, ...)					\
	pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
#define rdev_warn(rdev, fmt, ...)					\
	pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
#define rdev_info(rdev, fmt, ...)					\
	pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
#define rdev_dbg(rdev, fmt, ...)					\
	pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)

52
53
54
static DEFINE_MUTEX(regulator_list_mutex);
static LIST_HEAD(regulator_list);
static LIST_HEAD(regulator_map_list);
55
static LIST_HEAD(regulator_ena_gpio_list);
56
static LIST_HEAD(regulator_supply_alias_list);
57
static bool has_full_constraints;
58

59
60
static struct dentry *debugfs_root;

61
/*
62
63
64
65
66
67
 * struct regulator_map
 *
 * Used to provide symbolic supply names to devices.
 */
struct regulator_map {
	struct list_head list;
68
	const char *dev_name;   /* The dev_name() for the consumer */
69
	const char *supply;
70
	struct regulator_dev *regulator;
71
72
};

73
74
75
76
77
78
79
80
81
82
83
84
85
/*
 * struct regulator_enable_gpio
 *
 * Management for shared enable GPIO pin
 */
struct regulator_enable_gpio {
	struct list_head list;
	int gpio;
	u32 enable_count;	/* a number of enabled shared GPIO */
	u32 request_count;	/* a number of requested shared GPIO */
	unsigned int ena_gpio_invert:1;
};

86
87
88
89
90
91
92
93
94
95
96
97
98
/*
 * struct regulator_supply_alias
 *
 * Used to map lookups for a supply onto an alternative device.
 */
struct regulator_supply_alias {
	struct list_head list;
	struct device *src_dev;
	const char *src_supply;
	struct device *alias_dev;
	const char *alias_supply;
};

99
static int _regulator_is_enabled(struct regulator_dev *rdev);
100
static int _regulator_disable(struct regulator_dev *rdev);
101
102
103
104
105
static int _regulator_get_voltage(struct regulator_dev *rdev);
static int _regulator_get_current_limit(struct regulator_dev *rdev);
static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
static void _notifier_call_chain(struct regulator_dev *rdev,
				  unsigned long event, void *data);
106
107
static int _regulator_do_set_voltage(struct regulator_dev *rdev,
				     int min_uV, int max_uV);
108
109
110
static struct regulator *create_regulator(struct regulator_dev *rdev,
					  struct device *dev,
					  const char *supply_name);
111

112
113
114
115
116
117
118
119
120
121
static const char *rdev_get_name(struct regulator_dev *rdev)
{
	if (rdev->constraints && rdev->constraints->name)
		return rdev->constraints->name;
	else if (rdev->desc->name)
		return rdev->desc->name;
	else
		return "";
}

122
123
static bool have_full_constraints(void)
{
124
	return has_full_constraints || of_have_populated_dt();
125
126
}

127
128
129
130
131
132
/**
 * of_get_regulator - get a regulator device node based on supply name
 * @dev: Device pointer for the consumer (of regulator) device
 * @supply: regulator supply name
 *
 * Extract the regulator device node corresponding to the supply name.
133
 * returns the device node corresponding to the regulator if found, else
134
135
136
137
138
139
140
141
142
143
144
145
146
 * returns NULL.
 */
static struct device_node *of_get_regulator(struct device *dev, const char *supply)
{
	struct device_node *regnode = NULL;
	char prop_name[32]; /* 32 is max size of property name */

	dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);

	snprintf(prop_name, 32, "%s-supply", supply);
	regnode = of_parse_phandle(dev->of_node, prop_name, 0);

	if (!regnode) {
147
		dev_dbg(dev, "Looking up %s property in node %s failed",
148
149
150
151
152
153
				prop_name, dev->of_node->full_name);
		return NULL;
	}
	return regnode;
}

154
155
156
157
158
159
160
161
162
163
164
static int _regulator_can_change_status(struct regulator_dev *rdev)
{
	if (!rdev->constraints)
		return 0;

	if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
		return 1;
	else
		return 0;
}

165
166
167
168
169
170
171
/* Platform voltage constraint check */
static int regulator_check_voltage(struct regulator_dev *rdev,
				   int *min_uV, int *max_uV)
{
	BUG_ON(*min_uV > *max_uV);

	if (!rdev->constraints) {
172
		rdev_err(rdev, "no constraints\n");
173
174
175
		return -ENODEV;
	}
	if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
176
		rdev_err(rdev, "operation not allowed\n");
177
178
179
180
181
182
183
184
		return -EPERM;
	}

	if (*max_uV > rdev->constraints->max_uV)
		*max_uV = rdev->constraints->max_uV;
	if (*min_uV < rdev->constraints->min_uV)
		*min_uV = rdev->constraints->min_uV;

185
186
	if (*min_uV > *max_uV) {
		rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
187
			 *min_uV, *max_uV);
188
		return -EINVAL;
189
	}
190
191
192
193

	return 0;
}

194
195
196
197
198
199
200
201
202
/* Make sure we select a voltage that suits the needs of all
 * regulator consumers
 */
static int regulator_check_consumers(struct regulator_dev *rdev,
				     int *min_uV, int *max_uV)
{
	struct regulator *regulator;

	list_for_each_entry(regulator, &rdev->consumer_list, list) {
203
204
205
206
207
208
209
		/*
		 * Assume consumers that didn't say anything are OK
		 * with anything in the constraint range.
		 */
		if (!regulator->min_uV && !regulator->max_uV)
			continue;

210
211
212
213
214
215
		if (*max_uV > regulator->max_uV)
			*max_uV = regulator->max_uV;
		if (*min_uV < regulator->min_uV)
			*min_uV = regulator->min_uV;
	}

216
	if (*min_uV > *max_uV) {
217
218
		rdev_err(rdev, "Restricting voltage, %u-%uuV\n",
			*min_uV, *max_uV);
219
		return -EINVAL;
220
	}
221
222
223
224

	return 0;
}

225
226
227
228
229
230
231
/* current constraint check */
static int regulator_check_current_limit(struct regulator_dev *rdev,
					int *min_uA, int *max_uA)
{
	BUG_ON(*min_uA > *max_uA);

	if (!rdev->constraints) {
232
		rdev_err(rdev, "no constraints\n");
233
234
235
		return -ENODEV;
	}
	if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
236
		rdev_err(rdev, "operation not allowed\n");
237
238
239
240
241
242
243
244
		return -EPERM;
	}

	if (*max_uA > rdev->constraints->max_uA)
		*max_uA = rdev->constraints->max_uA;
	if (*min_uA < rdev->constraints->min_uA)
		*min_uA = rdev->constraints->min_uA;

245
246
	if (*min_uA > *max_uA) {
		rdev_err(rdev, "unsupportable current range: %d-%duA\n",
247
			 *min_uA, *max_uA);
248
		return -EINVAL;
249
	}
250
251
252
253
254

	return 0;
}

/* operating mode constraint check */
255
static int regulator_mode_constrain(struct regulator_dev *rdev, int *mode)
256
{
257
	switch (*mode) {
258
259
260
261
262
263
	case REGULATOR_MODE_FAST:
	case REGULATOR_MODE_NORMAL:
	case REGULATOR_MODE_IDLE:
	case REGULATOR_MODE_STANDBY:
		break;
	default:
264
		rdev_err(rdev, "invalid mode %x specified\n", *mode);
265
266
267
		return -EINVAL;
	}

268
	if (!rdev->constraints) {
269
		rdev_err(rdev, "no constraints\n");
270
271
272
		return -ENODEV;
	}
	if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
273
		rdev_err(rdev, "operation not allowed\n");
274
275
		return -EPERM;
	}
276
277
278
279
280
281
282
283

	/* The modes are bitmasks, the most power hungry modes having
	 * the lowest values. If the requested mode isn't supported
	 * try higher modes. */
	while (*mode) {
		if (rdev->constraints->valid_modes_mask & *mode)
			return 0;
		*mode /= 2;
284
	}
285
286

	return -EINVAL;
287
288
289
290
291
292
}

/* dynamic regulator mode switching constraint check */
static int regulator_check_drms(struct regulator_dev *rdev)
{
	if (!rdev->constraints) {
293
		rdev_err(rdev, "no constraints\n");
294
295
296
		return -ENODEV;
	}
	if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
297
		rdev_err(rdev, "operation not allowed\n");
298
299
300
301
302
303
304
305
		return -EPERM;
	}
	return 0;
}

static ssize_t regulator_uV_show(struct device *dev,
				struct device_attribute *attr, char *buf)
{
306
	struct regulator_dev *rdev = dev_get_drvdata(dev);
307
308
309
310
311
312
313
314
	ssize_t ret;

	mutex_lock(&rdev->mutex);
	ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
	mutex_unlock(&rdev->mutex);

	return ret;
}
315
static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
316
317
318
319

static ssize_t regulator_uA_show(struct device *dev,
				struct device_attribute *attr, char *buf)
{
320
	struct regulator_dev *rdev = dev_get_drvdata(dev);
321
322
323

	return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
}
324
static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
325

326
327
static ssize_t name_show(struct device *dev, struct device_attribute *attr,
			 char *buf)
328
329
330
{
	struct regulator_dev *rdev = dev_get_drvdata(dev);

331
	return sprintf(buf, "%s\n", rdev_get_name(rdev));
332
}
333
static DEVICE_ATTR_RO(name);
334

David Brownell's avatar
David Brownell committed
335
static ssize_t regulator_print_opmode(char *buf, int mode)
336
337
338
339
340
341
342
343
344
345
346
347
348
349
{
	switch (mode) {
	case REGULATOR_MODE_FAST:
		return sprintf(buf, "fast\n");
	case REGULATOR_MODE_NORMAL:
		return sprintf(buf, "normal\n");
	case REGULATOR_MODE_IDLE:
		return sprintf(buf, "idle\n");
	case REGULATOR_MODE_STANDBY:
		return sprintf(buf, "standby\n");
	}
	return sprintf(buf, "unknown\n");
}

David Brownell's avatar
David Brownell committed
350
351
static ssize_t regulator_opmode_show(struct device *dev,
				    struct device_attribute *attr, char *buf)
352
{
353
	struct regulator_dev *rdev = dev_get_drvdata(dev);
354

David Brownell's avatar
David Brownell committed
355
356
	return regulator_print_opmode(buf, _regulator_get_mode(rdev));
}
357
static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
David Brownell's avatar
David Brownell committed
358
359
360

static ssize_t regulator_print_state(char *buf, int state)
{
361
362
363
364
365
366
367
368
	if (state > 0)
		return sprintf(buf, "enabled\n");
	else if (state == 0)
		return sprintf(buf, "disabled\n");
	else
		return sprintf(buf, "unknown\n");
}

David Brownell's avatar
David Brownell committed
369
370
371
372
static ssize_t regulator_state_show(struct device *dev,
				   struct device_attribute *attr, char *buf)
{
	struct regulator_dev *rdev = dev_get_drvdata(dev);
373
374
375
376
377
	ssize_t ret;

	mutex_lock(&rdev->mutex);
	ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
	mutex_unlock(&rdev->mutex);
David Brownell's avatar
David Brownell committed
378

379
	return ret;
David Brownell's avatar
David Brownell committed
380
}
381
static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
David Brownell's avatar
David Brownell committed
382

David Brownell's avatar
David Brownell committed
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
static ssize_t regulator_status_show(struct device *dev,
				   struct device_attribute *attr, char *buf)
{
	struct regulator_dev *rdev = dev_get_drvdata(dev);
	int status;
	char *label;

	status = rdev->desc->ops->get_status(rdev);
	if (status < 0)
		return status;

	switch (status) {
	case REGULATOR_STATUS_OFF:
		label = "off";
		break;
	case REGULATOR_STATUS_ON:
		label = "on";
		break;
	case REGULATOR_STATUS_ERROR:
		label = "error";
		break;
	case REGULATOR_STATUS_FAST:
		label = "fast";
		break;
	case REGULATOR_STATUS_NORMAL:
		label = "normal";
		break;
	case REGULATOR_STATUS_IDLE:
		label = "idle";
		break;
	case REGULATOR_STATUS_STANDBY:
		label = "standby";
		break;
416
417
418
	case REGULATOR_STATUS_BYPASS:
		label = "bypass";
		break;
419
420
421
	case REGULATOR_STATUS_UNDEFINED:
		label = "undefined";
		break;
David Brownell's avatar
David Brownell committed
422
423
424
425
426
427
428
429
	default:
		return -ERANGE;
	}

	return sprintf(buf, "%s\n", label);
}
static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);

430
431
432
static ssize_t regulator_min_uA_show(struct device *dev,
				    struct device_attribute *attr, char *buf)
{
433
	struct regulator_dev *rdev = dev_get_drvdata(dev);
434
435
436
437
438
439

	if (!rdev->constraints)
		return sprintf(buf, "constraint not defined\n");

	return sprintf(buf, "%d\n", rdev->constraints->min_uA);
}
440
static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
441
442
443
444

static ssize_t regulator_max_uA_show(struct device *dev,
				    struct device_attribute *attr, char *buf)
{
445
	struct regulator_dev *rdev = dev_get_drvdata(dev);
446
447
448
449
450
451

	if (!rdev->constraints)
		return sprintf(buf, "constraint not defined\n");

	return sprintf(buf, "%d\n", rdev->constraints->max_uA);
}
452
static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
453
454
455
456

static ssize_t regulator_min_uV_show(struct device *dev,
				    struct device_attribute *attr, char *buf)
{
457
	struct regulator_dev *rdev = dev_get_drvdata(dev);
458
459
460
461
462
463

	if (!rdev->constraints)
		return sprintf(buf, "constraint not defined\n");

	return sprintf(buf, "%d\n", rdev->constraints->min_uV);
}
464
static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
465
466
467
468

static ssize_t regulator_max_uV_show(struct device *dev,
				    struct device_attribute *attr, char *buf)
{
469
	struct regulator_dev *rdev = dev_get_drvdata(dev);
470
471
472
473
474
475

	if (!rdev->constraints)
		return sprintf(buf, "constraint not defined\n");

	return sprintf(buf, "%d\n", rdev->constraints->max_uV);
}
476
static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
477
478
479
480

static ssize_t regulator_total_uA_show(struct device *dev,
				      struct device_attribute *attr, char *buf)
{
481
	struct regulator_dev *rdev = dev_get_drvdata(dev);
482
483
484
485
486
	struct regulator *regulator;
	int uA = 0;

	mutex_lock(&rdev->mutex);
	list_for_each_entry(regulator, &rdev->consumer_list, list)
487
		uA += regulator->uA_load;
488
489
490
	mutex_unlock(&rdev->mutex);
	return sprintf(buf, "%d\n", uA);
}
491
static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
492

493
494
static ssize_t num_users_show(struct device *dev, struct device_attribute *attr,
			      char *buf)
495
{
496
	struct regulator_dev *rdev = dev_get_drvdata(dev);
497
498
	return sprintf(buf, "%d\n", rdev->use_count);
}
499
static DEVICE_ATTR_RO(num_users);
500

501
502
static ssize_t type_show(struct device *dev, struct device_attribute *attr,
			 char *buf)
503
{
504
	struct regulator_dev *rdev = dev_get_drvdata(dev);
505
506
507
508
509
510
511
512
513

	switch (rdev->desc->type) {
	case REGULATOR_VOLTAGE:
		return sprintf(buf, "voltage\n");
	case REGULATOR_CURRENT:
		return sprintf(buf, "current\n");
	}
	return sprintf(buf, "unknown\n");
}
514
static DEVICE_ATTR_RO(type);
515
516
517
518

static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
				struct device_attribute *attr, char *buf)
{
519
	struct regulator_dev *rdev = dev_get_drvdata(dev);
520
521
522

	return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
}
523
524
static DEVICE_ATTR(suspend_mem_microvolts, 0444,
		regulator_suspend_mem_uV_show, NULL);
525
526
527
528

static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
				struct device_attribute *attr, char *buf)
{
529
	struct regulator_dev *rdev = dev_get_drvdata(dev);
530
531
532

	return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
}
533
534
static DEVICE_ATTR(suspend_disk_microvolts, 0444,
		regulator_suspend_disk_uV_show, NULL);
535
536
537
538

static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
				struct device_attribute *attr, char *buf)
{
539
	struct regulator_dev *rdev = dev_get_drvdata(dev);
540
541
542

	return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
}
543
544
static DEVICE_ATTR(suspend_standby_microvolts, 0444,
		regulator_suspend_standby_uV_show, NULL);
545
546
547
548

static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
				struct device_attribute *attr, char *buf)
{
549
	struct regulator_dev *rdev = dev_get_drvdata(dev);
550

David Brownell's avatar
David Brownell committed
551
552
	return regulator_print_opmode(buf,
		rdev->constraints->state_mem.mode);
553
}
554
555
static DEVICE_ATTR(suspend_mem_mode, 0444,
		regulator_suspend_mem_mode_show, NULL);
556
557
558
559

static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
				struct device_attribute *attr, char *buf)
{
560
	struct regulator_dev *rdev = dev_get_drvdata(dev);
561

David Brownell's avatar
David Brownell committed
562
563
	return regulator_print_opmode(buf,
		rdev->constraints->state_disk.mode);
564
}
565
566
static DEVICE_ATTR(suspend_disk_mode, 0444,
		regulator_suspend_disk_mode_show, NULL);
567
568
569
570

static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
				struct device_attribute *attr, char *buf)
{
571
	struct regulator_dev *rdev = dev_get_drvdata(dev);
572

David Brownell's avatar
David Brownell committed
573
574
	return regulator_print_opmode(buf,
		rdev->constraints->state_standby.mode);
575
}
576
577
static DEVICE_ATTR(suspend_standby_mode, 0444,
		regulator_suspend_standby_mode_show, NULL);
578
579
580
581

static ssize_t regulator_suspend_mem_state_show(struct device *dev,
				   struct device_attribute *attr, char *buf)
{
582
	struct regulator_dev *rdev = dev_get_drvdata(dev);
583

David Brownell's avatar
David Brownell committed
584
585
	return regulator_print_state(buf,
			rdev->constraints->state_mem.enabled);
586
}
587
588
static DEVICE_ATTR(suspend_mem_state, 0444,
		regulator_suspend_mem_state_show, NULL);
589
590
591
592

static ssize_t regulator_suspend_disk_state_show(struct device *dev,
				   struct device_attribute *attr, char *buf)
{
593
	struct regulator_dev *rdev = dev_get_drvdata(dev);
594

David Brownell's avatar
David Brownell committed
595
596
	return regulator_print_state(buf,
			rdev->constraints->state_disk.enabled);
597
}
598
599
static DEVICE_ATTR(suspend_disk_state, 0444,
		regulator_suspend_disk_state_show, NULL);
600
601
602
603

static ssize_t regulator_suspend_standby_state_show(struct device *dev,
				   struct device_attribute *attr, char *buf)
{
604
	struct regulator_dev *rdev = dev_get_drvdata(dev);
605

David Brownell's avatar
David Brownell committed
606
607
	return regulator_print_state(buf,
			rdev->constraints->state_standby.enabled);
608
}
609
610
611
static DEVICE_ATTR(suspend_standby_state, 0444,
		regulator_suspend_standby_state_show, NULL);

612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
static ssize_t regulator_bypass_show(struct device *dev,
				     struct device_attribute *attr, char *buf)
{
	struct regulator_dev *rdev = dev_get_drvdata(dev);
	const char *report;
	bool bypass;
	int ret;

	ret = rdev->desc->ops->get_bypass(rdev, &bypass);

	if (ret != 0)
		report = "unknown";
	else if (bypass)
		report = "enabled";
	else
		report = "disabled";

	return sprintf(buf, "%s\n", report);
}
static DEVICE_ATTR(bypass, 0444,
		   regulator_bypass_show, NULL);
633

634
635
636
637
/*
 * These are the only attributes are present for all regulators.
 * Other attributes are a function of regulator functionality.
 */
638
639
640
641
642
static struct attribute *regulator_dev_attrs[] = {
	&dev_attr_name.attr,
	&dev_attr_num_users.attr,
	&dev_attr_type.attr,
	NULL,
643
};
644
ATTRIBUTE_GROUPS(regulator_dev);
645
646
647

static void regulator_dev_release(struct device *dev)
{
648
	struct regulator_dev *rdev = dev_get_drvdata(dev);
649
650
651
652
653
654
	kfree(rdev);
}

static struct class regulator_class = {
	.name = "regulator",
	.dev_release = regulator_dev_release,
655
	.dev_groups = regulator_dev_groups,
656
657
658
659
660
661
662
663
664
665
666
667
};

/* Calculate the new optimum regulator operating mode based on the new total
 * consumer load. All locks held by caller */
static void drms_uA_update(struct regulator_dev *rdev)
{
	struct regulator *sibling;
	int current_uA = 0, output_uV, input_uV, err;
	unsigned int mode;

	err = regulator_check_drms(rdev);
	if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
668
669
670
	    (!rdev->desc->ops->get_voltage &&
	     !rdev->desc->ops->get_voltage_sel) ||
	    !rdev->desc->ops->set_mode)
671
		return;
672
673

	/* get output voltage */
674
	output_uV = _regulator_get_voltage(rdev);
675
676
677
678
	if (output_uV <= 0)
		return;

	/* get input voltage */
679
680
	input_uV = 0;
	if (rdev->supply)
681
		input_uV = regulator_get_voltage(rdev->supply);
682
	if (input_uV <= 0)
683
684
685
686
687
688
		input_uV = rdev->constraints->input_uV;
	if (input_uV <= 0)
		return;

	/* calc total requested load */
	list_for_each_entry(sibling, &rdev->consumer_list, list)
689
		current_uA += sibling->uA_load;
690
691
692
693
694
695

	/* now get the optimum mode for our new total regulator load */
	mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
						  output_uV, current_uA);

	/* check the new mode is allowed */
696
	err = regulator_mode_constrain(rdev, &mode);
697
698
699
700
701
702
703
704
	if (err == 0)
		rdev->desc->ops->set_mode(rdev, mode);
}

static int suspend_set_state(struct regulator_dev *rdev,
	struct regulator_state *rstate)
{
	int ret = 0;
705
706

	/* If we have no suspend mode configration don't set anything;
707
708
	 * only warn if the driver implements set_suspend_voltage or
	 * set_suspend_mode callback.
709
710
	 */
	if (!rstate->enabled && !rstate->disabled) {
711
712
		if (rdev->desc->ops->set_suspend_voltage ||
		    rdev->desc->ops->set_suspend_mode)
713
			rdev_warn(rdev, "No configuration\n");
714
715
716
717
		return 0;
	}

	if (rstate->enabled && rstate->disabled) {
718
		rdev_err(rdev, "invalid configuration\n");
719
720
		return -EINVAL;
	}
721

722
	if (rstate->enabled && rdev->desc->ops->set_suspend_enable)
723
		ret = rdev->desc->ops->set_suspend_enable(rdev);
724
	else if (rstate->disabled && rdev->desc->ops->set_suspend_disable)
725
		ret = rdev->desc->ops->set_suspend_disable(rdev);
726
727
728
	else /* OK if set_suspend_enable or set_suspend_disable is NULL */
		ret = 0;

729
	if (ret < 0) {
730
		rdev_err(rdev, "failed to enabled/disable\n");
731
732
733
734
735
736
		return ret;
	}

	if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
		ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
		if (ret < 0) {
737
			rdev_err(rdev, "failed to set voltage\n");
738
739
740
741
742
743
744
			return ret;
		}
	}

	if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
		ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
		if (ret < 0) {
745
			rdev_err(rdev, "failed to set mode\n");
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
			return ret;
		}
	}
	return ret;
}

/* locks held by caller */
static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
{
	if (!rdev->constraints)
		return -EINVAL;

	switch (state) {
	case PM_SUSPEND_STANDBY:
		return suspend_set_state(rdev,
			&rdev->constraints->state_standby);
	case PM_SUSPEND_MEM:
		return suspend_set_state(rdev,
			&rdev->constraints->state_mem);
	case PM_SUSPEND_MAX:
		return suspend_set_state(rdev,
			&rdev->constraints->state_disk);
	default:
		return -EINVAL;
	}
}

static void print_constraints(struct regulator_dev *rdev)
{
	struct regulation_constraints *constraints = rdev->constraints;
776
	char buf[80] = "";
777
778
	int count = 0;
	int ret;
779

780
	if (constraints->min_uV && constraints->max_uV) {
781
		if (constraints->min_uV == constraints->max_uV)
782
783
			count += sprintf(buf + count, "%d mV ",
					 constraints->min_uV / 1000);
784
		else
785
786
787
788
789
790
791
792
793
794
795
796
			count += sprintf(buf + count, "%d <--> %d mV ",
					 constraints->min_uV / 1000,
					 constraints->max_uV / 1000);
	}

	if (!constraints->min_uV ||
	    constraints->min_uV != constraints->max_uV) {
		ret = _regulator_get_voltage(rdev);
		if (ret > 0)
			count += sprintf(buf + count, "at %d mV ", ret / 1000);
	}

797
798
799
800
	if (constraints->uV_offset)
		count += sprintf(buf, "%dmV offset ",
				 constraints->uV_offset / 1000);

801
	if (constraints->min_uA && constraints->max_uA) {
802
		if (constraints->min_uA == constraints->max_uA)
803
804
			count += sprintf(buf + count, "%d mA ",
					 constraints->min_uA / 1000);
805
		else
806
807
808
809
810
811
812
813
814
			count += sprintf(buf + count, "%d <--> %d mA ",
					 constraints->min_uA / 1000,
					 constraints->max_uA / 1000);
	}

	if (!constraints->min_uA ||
	    constraints->min_uA != constraints->max_uA) {
		ret = _regulator_get_current_limit(rdev);
		if (ret > 0)
815
			count += sprintf(buf + count, "at %d mA ", ret / 1000);
816
	}
817

818
819
820
821
822
823
824
825
826
	if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
		count += sprintf(buf + count, "fast ");
	if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
		count += sprintf(buf + count, "normal ");
	if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
		count += sprintf(buf + count, "idle ");
	if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
		count += sprintf(buf + count, "standby");

827
828
829
	if (!count)
		sprintf(buf, "no parameters");

Mark Brown's avatar
Mark Brown committed
830
	rdev_info(rdev, "%s\n", buf);
831
832
833
834
835

	if ((constraints->min_uV != constraints->max_uV) &&
	    !(constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE))
		rdev_warn(rdev,
			  "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
836
837
}

838
static int machine_constraints_voltage(struct regulator_dev *rdev,
839
	struct regulation_constraints *constraints)
840
{
841
	struct regulator_ops *ops = rdev->desc->ops;
842
843
844
845
	int ret;

	/* do we need to apply the constraint voltage */
	if (rdev->constraints->apply_uV &&
846
847
848
849
850
851
852
853
854
	    rdev->constraints->min_uV == rdev->constraints->max_uV) {
		ret = _regulator_do_set_voltage(rdev,
						rdev->constraints->min_uV,
						rdev->constraints->max_uV);
		if (ret < 0) {
			rdev_err(rdev, "failed to apply %duV constraint\n",
				 rdev->constraints->min_uV);
			return ret;
		}
855
	}
856

857
858
859
860
861
862
863
864
865
866
867
	/* constrain machine-level voltage specs to fit
	 * the actual range supported by this regulator.
	 */
	if (ops->list_voltage && rdev->desc->n_voltages) {
		int	count = rdev->desc->n_voltages;
		int	i;
		int	min_uV = INT_MAX;
		int	max_uV = INT_MIN;
		int	cmin = constraints->min_uV;
		int	cmax = constraints->max_uV;

868
869
		/* it's safe to autoconfigure fixed-voltage supplies
		   and the constraints are used by list_voltage. */
870
		if (count == 1 && !cmin) {
871
			cmin = 1;
872
			cmax = INT_MAX;
873
874
			constraints->min_uV = cmin;
			constraints->max_uV = cmax;
875
876
		}

877
878
		/* voltage constraints are optional */
		if ((cmin == 0) && (cmax == 0))
879
			return 0;
880

881
		/* else require explicit machine-level constraints */
882
		if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
883
			rdev_err(rdev, "invalid voltage constraints\n");
884
			return -EINVAL;
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
		}

		/* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
		for (i = 0; i < count; i++) {
			int	value;

			value = ops->list_voltage(rdev, i);
			if (value <= 0)
				continue;

			/* maybe adjust [min_uV..max_uV] */
			if (value >= cmin && value < min_uV)
				min_uV = value;
			if (value <= cmax && value > max_uV)
				max_uV = value;
		}

		/* final: [min_uV..max_uV] valid iff constraints valid */
		if (max_uV < min_uV) {
904
905
906
			rdev_err(rdev,
				 "unsupportable voltage constraints %u-%uuV\n",
				 min_uV, max_uV);
907
			return -EINVAL;
908
909
910
911
		}

		/* use regulator's subset of machine constraints */
		if (constraints->min_uV < min_uV) {
912
913
			rdev_dbg(rdev, "override min_uV, %d -> %d\n",
				 constraints->min_uV, min_uV);
914
915
916
			constraints->min_uV = min_uV;
		}
		if (constraints->max_uV > max_uV) {
917
918
			rdev_dbg(rdev, "override max_uV, %d -> %d\n",
				 constraints->max_uV, max_uV);
919
920
921
922
			constraints->max_uV = max_uV;
		}
	}

923
924
925
	return 0;
}

926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
static int machine_constraints_current(struct regulator_dev *rdev,
	struct regulation_constraints *constraints)
{
	struct regulator_ops *ops = rdev->desc->ops;
	int ret;

	if (!constraints->min_uA && !constraints->max_uA)
		return 0;

	if (constraints->min_uA > constraints->max_uA) {
		rdev_err(rdev, "Invalid current constraints\n");
		return -EINVAL;
	}

	if (!ops->set_current_limit || !ops->get_current_limit) {
		rdev_warn(rdev, "Operation of current configuration missing\n");
		return 0;
	}

	/* Set regulator current in constraints range */
	ret = ops->set_current_limit(rdev, constraints->min_uA,
			constraints->max_uA);
	if (ret < 0) {
		rdev_err(rdev, "Failed to set current constraint, %d\n", ret);
		return ret;
	}

	return 0;
}

956
957
958
959
960
961
962
963
964
965
966
967
/**
 * set_machine_constraints - sets regulator constraints
 * @rdev: regulator source
 * @constraints: constraints to apply
 *
 * Allows platform initialisation code to define and constrain
 * regulator circuits e.g. valid voltage/current ranges, etc.  NOTE:
 * Constraints *must* be set by platform code in order for some
 * regulator operations to proceed i.e. set_voltage, set_current_limit,
 * set_mode.
 */
static int set_machine_constraints(struct regulator_dev *rdev,
968
	const struct regulation_constraints *constraints)
969
970
971
972
{
	int ret = 0;
	struct regulator_ops *ops = rdev->desc->ops;

973
974
975
976
977
978
	if (constraints)
		rdev->constraints = kmemdup(constraints, sizeof(*constraints),
					    GFP_KERNEL);
	else
		rdev->constraints = kzalloc(sizeof(*constraints),
					    GFP_KERNEL);
979
980
	if (!rdev->constraints)
		return -ENOMEM;
981

982
	ret = machine_constraints_voltage(rdev, rdev->constraints);
983
984
985
	if (ret != 0)
		goto out;

986
	ret = machine_constraints_current(rdev, rdev->constraints);
987
988
989
	if (ret != 0)
		goto out;

990
	/* do we need to setup our suspend state */
991
	if (rdev->constraints->initial_state) {
992
		ret = suspend_prepare(rdev, rdev->constraints->initial_state);
993
		if (ret < 0) {
994
			rdev_err(rdev, "failed to set suspend state\n");
995
996
997
			goto out;
		}
	}
998

999
	if (rdev->constraints->initial_mode) {
1000
		if (!ops->set_mode) {
1001
			rdev_err(rdev, "no set_mode operation\n");
1002
1003
1004
1005
			ret = -EINVAL;
			goto out;
		}

1006
		ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
1007
		if (ret < 0) {
1008
			rdev_err(rdev, "failed to set initial mode: %d\n", ret);
1009
1010
1011
1012
			goto out;
		}
	}

1013
1014
1015
	/* If the constraints say the regulator should be on at this point
	 * and we have control then make sure it is enabled.
	 */
1016
1017
	if ((rdev->constraints->always_on || rdev->constraints->boot_on) &&
	    ops->enable) {
1018
1019
		ret = ops->enable(rdev);
		if (ret < 0) {
1020
			rdev_err(rdev, "failed to enable\n");
1021
1022
1023
1024
			goto out;
		}
	}

1025
1026
	if ((rdev->constraints->ramp_delay || rdev->constraints->ramp_disable)
		&& ops->set_ramp_delay) {
1027
1028
1029
1030
1031
1032
1033
		ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay);
		if (ret < 0) {
			rdev_err(rdev, "failed to set ramp_delay\n");
			goto out;
		}
	}

1034
	print_constraints(rdev);
1035
	return 0;
1036
out:
1037
1038
	kfree(rdev->constraints);
	rdev->constraints = NULL;
1039
1040
1041
1042
1043
	return ret;
}

/**
 * set_supply - set regulator supply regulator
1044
1045
 * @rdev: regulator name
 * @supply_rdev: supply regulator name
1046
1047
1048
1049
1050
1051
 *
 * Called by platform initialisation code to set the supply regulator for this
 * regulator. This ensures that a regulators supply will also be enabled by the
 * core if it's child is enabled.
 */
static int set_supply(struct regulator_dev *rdev,
1052
		      struct regulator_dev *supply_rdev)
1053
1054
1055
{
	int err;

1056
1057
1058
	rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));

	rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
1059
1060
	if (rdev->supply == NULL) {
		err = -ENOMEM;
1061
		return err;
1062
	}
1063
	supply_rdev->open_count++;
1064
1065

	return 0;
1066
1067
1068
}

/**
1069
 * set_consumer_device_supply - Bind a regulator to a symbolic supply
1070
 * @rdev:         regulator source
1071
 * @consumer_dev_name: dev_name() string for device supply applies to
1072
 * @supply:       symbolic name for supply
1073
1074
1075
1076
1077
1078
1079
 *
 * Allows platform initialisation code to map physical regulator
 * sources to symbolic names for supplies for use by devices.  Devices
 * should use these symbolic names to request regulators, avoiding the
 * need to provide board-specific regulator names as platform data.
 */
static int set_consumer_device_supply(struct regulator_dev *rdev,
1080
1081
				      const char *consumer_dev_name,
				      const char *supply)
1082
1083
{
	struct regulator_map *node;
1084
	int has_dev;
1085
1086
1087
1088

	if (supply == NULL)
		return -EINVAL;

1089
1090
1091
1092
1093
	if (consumer_dev_name != NULL)
		has_dev = 1;
	else
		has_dev = 0;

1094
	list_for_each_entry(node, &regulator_map_list, list) {
1095
1096
1097
1098
		if (node->dev_name && consumer_dev_name) {
			if (strcmp(node->dev_name, consumer_dev_name) != 0)
				continue;
		} else if (node->dev_name || consumer_dev_name) {
1099
			continue;
1100
1101
		}

1102
1103
1104
		if (strcmp(node->supply, supply) != 0)
			continue;

1105
1106
1107
1108
1109
1110
		pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
			 consumer_dev_name,
			 dev_name(&node->regulator->dev),
			 node->regulator->desc->name,
			 supply,
			 dev_name(&rdev->dev), rdev_get_name(rdev));
1111
1112
1113
		return -EBUSY;
	}

1114
	node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
1115
1116
1117
1118
1119
1120
	if (node == NULL)
		return -ENOMEM;

	node->regulator = rdev;
	node->supply = supply;

1121
1122
1123
1124
1125
1126
	if (has_dev) {
		node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
		if (node->dev_name == NULL) {
			kfree(node);
			return -ENOMEM;
		}
1127
1128
	}

1129
1130
1131
1132
	list_add(&node->list, &regulator_map_list);
	return 0;
}

1133
1134
1135
1136
1137
1138
1139
static void unset_regulator_supplies(struct regulator_dev *rdev)
{
	struct regulator_map *node, *n;

	list_for_each_entry_safe(node, n, &regulator_map_list, list) {
		if (rdev == node->regulator) {
			list_del(&node->list);
1140
			kfree(node->dev_name);
1141
1142
1143
1144
1145
			kfree(node);
		}
	}
}

1146
#define REG_STR_SIZE	64
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164

static struct regulator *create_regulator(struct regulator_dev *rdev,
					  struct device *dev,
					  const char *supply_name)
{
	struct regulator *regulator;
	char buf[REG_STR_SIZE];
	int err, size;

	regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
	if (regulator == NULL)
		return NULL;

	mutex_lock(&rdev->mutex);
	regulator->rdev = rdev;
	list_add(&regulator->list, &rdev->consumer_list);

	if (dev) {
1165
1166
		regulator->dev = dev;

1167
		/* Add a link to the device sysfs entry */
1168
1169
1170
		size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
				 dev->kobj.name, supply_name);
		if (size >= REG_STR_SIZE)
1171
			goto overflow_err;
1172
1173
1174

		regulator->supply_name = kstrdup(buf, GFP_KERNEL);
		if (regulator->supply_name == NULL)
1175
			goto overflow_err;
1176
1177
1178
1179

		err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
					buf);
		if (err) {
1180
1181
			rdev_warn(rdev, "could not add device link %s err %d\n",
				  dev->kobj.name, err);
1182
			/* non-fatal */
1183
		}
1184
1185
1186
<