aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/capability.c
blob: 4e17041963f57073a4fb3836c415b9a681e40383 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
 * linux/kernel/capability.c
 *
 * Copyright (C) 1997  Andrew Main <zefram@fysh.org>
 *
 * Integrated into 2.1.97+,  Andrew G. Morgan <morgan@kernel.org>
 * 30 May 2002:	Cleanup, Robert M. Love <rml@tech9.net>
 */

#include <linux/audit.h>
#include <linux/capability.h>
#include <linux/mm.h>
#include <linux/module.h>
#include <linux/security.h>
#include <linux/syscalls.h>
#include <linux/pid_namespace.h>
#include <asm/uaccess.h>
#include "cred-internals.h"

/*
 * Leveraged for setting/resetting capabilities
 */

const kernel_cap_t __cap_empty_set = CAP_EMPTY_SET;
const kernel_cap_t __cap_full_set = CAP_FULL_SET;
const kernel_cap_t __cap_init_eff_set = CAP_INIT_EFF_SET;

EXPORT_SYMBOL(__cap_empty_set);
EXPORT_SYMBOL(__cap_full_set);
EXPORT_SYMBOL(__cap_init_eff_set);

#ifdef CONFIG_SECURITY_FILE_CAPABILITIES
int file_caps_enabled = 1;

static int __init file_caps_disable(char *str)
{
	file_caps_enabled = 0;
	return 1;
}
__setup("no_file_caps", file_caps_disable);
#endif

/*
 * More recent versions of libcap are available from:
 *
 *   http://www.kernel.org/pub/linux/libs/security/linux-privs/
 */

static void warn_legacy_capability_use(void)
{
	static int warned;
	if (!warned) {
		char name[sizeof(current->comm)];

		printk(KERN_INFO "warning: `%s' uses 32-bit capabilities"
		       " (legacy support in use)\n",
		       get_task_comm(name, current));
		warned = 1;
	}
}

/*
 * Version 2 capabilities worked fine, but the linux/capability.h file
 * that accompanied their introduction encouraged their use without
 * the necessary user-space source code changes. As such, we have
 * created a version 3 with equivalent functionality to version 2, but
 * with a header change to protect legacy source code from using
 * version 2 when it wanted to use version 1. If your system has code
 * that trips the following warning, it is using version 2 specific
 * capabilities and may be doing so insecurely.
 *
 * The remedy is to either upgrade your version of libcap (to 2.10+,
 * if the application is linked against it), or recompile your
 * application with modern kernel headers and this warning will go
 * away.
 */

static void warn_deprecated_v2(void)
{
	static int warned;

	if (!warned) {
		char name[sizeof(current->comm)];

		printk(KERN_INFO "warning: `%s' uses deprecated v2"
		       " capabilities in a way that may be insecure.\n",
		       get_task_comm(name, current));
		warned = 1;
	}
}

/*
 * Version check. Return the number of u32s in each capability flag
 * array, or a negative value on error.
 */
static int cap_validate_magic(cap_user_header_t header, unsigned *tocopy)
{
	__u32 version;

	if (get_user(version, &header->version))
		return -EFAULT;

	switch (version) {
	case _LINUX_CAPABILITY_VERSION_1:
		warn_legacy_capability_use();
		*tocopy = _LINUX_CAPABILITY_U32S_1;
		break;
	case _LINUX_CAPABILITY_VERSION_2:
		warn_deprecated_v2();
		/*
		 * fall through - v3 is otherwise equivalent to v2.
		 */
	case _LINUX_CAPABILITY_VERSION_3:
		*tocopy = _LINUX_CAPABILITY_U32S_3;
		break;
	default:
		if (put_user((u32)_KERNEL_CAPABILITY_VERSION, &header->version))
			return -EFAULT;
		return -EINVAL;
	}

	return 0;
}

/*
 * The only thing that can change the capabilities of the current
 * process is the current process. As such, we can't be in this code
 * at the same time as we are in the process of setting capabilities
 * in this process. The net result is that we can limit our use of
 * locks to when we are reading the caps of another process.
 */
static inline int cap_get_target_pid(pid_t pid, kernel_cap_t *pEp,
				     kernel_cap_t *pIp, kernel_cap_t *pPp)
{
	int ret;

	if (pid && (pid != task_pid_vnr(current))) {
		struct task_struct *target;

		read_lock(&tasklist_lock);

		target = find_task_by_vpid(pid);
		if (!target)
			ret = -ESRCH;
		else
			ret = security_capget(target, pEp, pIp, pPp);

		read_unlock(&tasklist_lock);
	} else
		ret = security_capget(current, pEp, pIp, pPp);

	return ret;
}

/**
 * sys_capget - get the capabilities of a given process.
 * @header: pointer to struct that contains capability version and
 *	target pid data
 * @dataptr: pointer to struct that contains the effective, permitted,
 *	and inheritable capabilities that are returned
 *
 * Returns 0 on success and < 0 on error.
 */
SYSCALL_DEFINE2(capget, cap_user_header_t, header, cap_user_data_t, dataptr)
{
	int ret = 0;
	pid_t pid;
	unsigned tocopy;
	kernel_cap_t pE, pI, pP;

	ret = cap_validate_magic(header, &tocopy);
	if (ret != 0)
		return ret;

	if (get_user(pid, &header->pid))
		return -EFAULT;

	if (pid < 0)
		return -EINVAL;

	ret = cap_get_target_pid(pid, &pE, &pI, &pP);
	if (!ret) {
		struct __user_cap_data_struct kdata[_KERNEL_CAPABILITY_U32S];
		unsigned i;

		for (i = 0; i < tocopy; i++) {
			kdata[i].effective = pE.cap[i];
			kdata[i].permitted = pP.cap[i];
			kdata[i].inheritable = pI.cap[i];
		}

		/*
		 * Note, in the case, tocopy < _KERNEL_CAPABILITY_U32S,
		 * we silently drop the upper capabilities here. This
		 * has the effect of making older libcap
		 * implementations implicitly drop upper capability
		 * bits when they perform a: capget/modify/capset
		 * sequence.
		 *
		 * This behavior is considered fail-safe
		 * behavior. Upgrading the application to a newer
		 * version of libcap will enable access to the newer
		 * capabilities.
		 *
		 * An alternative would be to return an error here
		 * (-ERANGE), but that causes legacy applications to
		 * unexpectidly fail; the capget/modify/capset aborts
		 * before modification is attempted and the application
		 * fails.
		 */
		if (copy_to_user(dataptr, kdata, tocopy
				 * sizeof(struct __user_cap_data_struct))) {
			return -EFAULT;
		}
	}

	return ret;
}

/**
 * sys_capset - set capabilities for a process or (*) a group of processes
 * @header: pointer to struct that contains capability version and
 *	target pid data
 * @data: pointer to struct that contains the effective, permitted,
 *	and inheritable capabilities
 *
 * Set capabilities for the current process only.  The ability to any other
 * process(es) has been deprecated and removed.
 *
 * The restrictions on setting capabilities are specified as:
 *
 * I: any raised capabilities must be a subset of the old permitted
 * P: any raised capabilities must be a subset of the old permitted
 * E: must be set to a subset of new permitted
 *
 * Returns 0 on success and < 0 on error.
 */
SYSCALL_DEFINE2(capset, cap_user_header_t, header, const cap_user_data_t, data)
{
	struct __user_cap_data_struct kdata[_KERNEL_CAPABILITY_U32S];
	unsigned i, tocopy;
	kernel_cap_t inheritable, permitted, effective;
	struct cred *new;
	int ret;
	pid_t pid;

	ret = cap_validate_magic(header, &tocopy);
	if (ret != 0)
		return ret;

	if (get_user(pid, &header->pid))
		return -EFAULT;

	/* may only affect current now */
	if (pid != 0 && pid != task_pid_vnr(current))
		return -EPERM;

	if (copy_from_user(&kdata, data,
			   tocopy * sizeof(struct __user_cap_data_struct)))
		return -EFAULT;

	for (i = 0; i < tocopy; i++) {
		effective.cap[i] = kdata[i].effective;
		permitted.cap[i] = kdata[i].permitted;
		inheritable.cap[i] = kdata[i].inheritable;
	}
	while (i < _KERNEL_CAPABILITY_U32S) {
		effective.cap[i] = 0;
		permitted.cap[i] = 0;
		inheritable.cap[i] = 0;
		i++;
	}

	new = prepare_creds();
	if (!new)
		return -ENOMEM;

	ret = security_capset(new, current_cred(),
			      &effective, &inheritable, &permitted);
	if (ret < 0)
		goto error;

	audit_log_capset(pid, new, current_cred());

	return commit_creds(new);

error:
	abort_creds(new);
	return ret;
}

/**
 * capable - Determine if the current task has a superior capability in effect
 * @cap: The capability to be tested for
 *
 * Return true if the current task has the given superior capability currently
 * available for use, false if not.
 *
 * This sets PF_SUPERPRIV on the task if the capability is available on the
 * assumption that it's about to be used.
 */
int capable(int cap)
{
	if (unlikely(!cap_valid(cap))) {
		printk(KERN_CRIT "capable() called with invalid cap=%u\n", cap);
		BUG();
	}

	if (security_capable(cap) == 0) {
		current->flags |= PF_SUPERPRIV;
		return 1;
	}
	return 0;
}
EXPORT_SYMBOL(capable);
div>
7d38a3cb9b9f

0651eed5e094
518fb721de36
03746dcbde75





518fb721de36

a320e3c3d635

33a6943d2c7f
19228a6a5925
0651eed5e094
c2f8efd7641b

33a6943d2c7f
19228a6a5925
7d38a3cb9b9f

0651eed5e094
a320e3c3d635

33a6943d2c7f
19228a6a5925
7be531883a81
0651eed5e094
a320e3c3d635

33a6943d2c7f
19228a6a5925
7be531883a81
0651eed5e094
a320e3c3d635

33a6943d2c7f
7be531883a81
0651eed5e094
a320e3c3d635

33a6943d2c7f
19228a6a5925
7be531883a81
0651eed5e094
a320e3c3d635

33a6943d2c7f
19228a6a5925
7be531883a81
0651eed5e094
a320e3c3d635

33a6943d2c7f
19228a6a5925
7be531883a81
0651eed5e094
a320e3c3d635

33a6943d2c7f
19228a6a5925
7be531883a81
0651eed5e094
a320e3c3d635

33a6943d2c7f
19228a6a5925
7be531883a81
0651eed5e094
a320e3c3d635

33a6943d2c7f
19228a6a5925
7be531883a81
0651eed5e094
a320e3c3d635

33a6943d2c7f
19228a6a5925
7be531883a81
0651eed5e094
a320e3c3d635

33a6943d2c7f
19228a6a5925
7be531883a81
0651eed5e094
a320e3c3d635


1e0c66f49762
































518fb721de36
39aa9b6e3cb1
518fb721de36
39aa9b6e3cb1

39aa9b6e3cb1
518fb721de36
a320e3c3d635
1e0c66f49762

518fb721de36

518fb721de36




























03746dcbde75

518fb721de36




a320e3c3d635

































518fb721de36




a320e3c3d635

518fb721de36




faa95fde43f7


518fb721de36

3f7e82759c69
518fb721de36
3f7e82759c69
518fb721de36







faa95fde43f7
518fb721de36
a320e3c3d635
518fb721de36


faa95fde43f7


518fb721de36
585993932ccc
518fb721de36






18039e0f16d5
518fb721de36

faa95fde43f7
a320e3c3d635
518fb721de36


faa95fde43f7





518fb721de36
faa95fde43f7


518fb721de36


a320e3c3d635
518fb721de36

faa95fde43f7





518fb721de36
faa95fde43f7


518fb721de36


a320e3c3d635


faa95fde43f7







a320e3c3d635



518fb721de36




4b57927045c3
518fb721de36

a320e3c3d635




18039e0f16d5
518fb721de36

a320e3c3d635




18039e0f16d5
518fb721de36
18039e0f16d5
518fb721de36

1f904fd1cdbd
518fb721de36

faa95fde43f7
518fb721de36
a320e3c3d635
518fb721de36


faa95fde43f7


518fb721de36













03746dcbde75



518fb721de36



1f904fd1cdbd
518fb721de36



d9fe28f9621b
518fb721de36

1f904fd1cdbd
a320e3c3d635

faa95fde43f7

a320e3c3d635


faa95fde43f7


a320e3c3d635






a320e3c3d635







a320e3c3d635

e882eae80f37

1f904fd1cdbd
a320e3c3d635



1f904fd1cdbd
a320e3c3d635

94732b97c398

518fb721de36


a320e3c3d635
518fb721de36
a320e3c3d635

780dc9ba4eb6
a320e3c3d635

780dc9ba4eb6
518fb721de36
faa95fde43f7



a320e3c3d635

780dc9ba4eb6
a320e3c3d635

780dc9ba4eb6
a320e3c3d635
faa95fde43f7



a320e3c3d635

c4632aed3e5b
faa95fde43f7
518fb721de36




94732b97c398

518fb721de36



a320e3c3d635
518fb721de36












faa95fde43f7

03746dcbde75


518fb721de36




94732b97c398

a320e3c3d635











faa95fde43f7

a320e3c3d635




faa95fde43f7

e882eae80f37
faa95fde43f7

03746dcbde75


a320e3c3d635





518fb721de36


a320e3c3d635
518fb721de36
a320e3c3d635


780dc9ba4eb6
a320e3c3d635
4b57927045c3
d04156bca629
a320e3c3d635

d04156bca629



a320e3c3d635
518fb721de36



a320e3c3d635




4b57927045c3
a320e3c3d635
























d9fe28f9621b
a320e3c3d635






518fb721de36

a40a9c436b62


518fb721de36

18039e0f16d5
94732b97c398
01bc3a140037
518fb721de36
9fa8175f0f6e
518fb721de36


a40a9c436b62


518fb721de36


d9fe28f9621b
9fa8175f0f6e
518fb721de36

03746dcbde75











518fb721de36
a40a9c436b62


518fb721de36

1f904fd1cdbd
94732b97c398
d9fe28f9621b
9fa8175f0f6e
518fb721de36

a320e3c3d635
a40a9c436b62


a320e3c3d635

1f904fd1cdbd
94732b97c398
a320e3c3d635
9fa8175f0f6e
a320e3c3d635

1e0c66f49762



















f30b0716feae

1e0c66f49762










3f7e82759c69
1e0c66f49762

3f7e82759c69
1e0c66f49762








3f7e82759c69
1e0c66f49762

3f7e82759c69
1e0c66f49762










3f7e82759c69
1e0c66f49762

3f7e82759c69
1e0c66f49762










3f7e82759c69
1e0c66f49762

3f7e82759c69
1e0c66f49762

















faa95fde43f7








1e0c66f49762

faa95fde43f7


1e0c66f49762





faa95fde43f7
1e0c66f49762
6d3be300c658
1e0c66f49762



3f7e82759c69
1e0c66f49762
f30b0716feae

3f7e82759c69
f30b0716feae

3f7e82759c69
f30b0716feae

1e0c66f49762


f30b0716feae
1e0c66f49762


6790178f55d4


33a6943d2c7f












03746dcbde75
6790178f55d4


33a6943d2c7f












6790178f55d4


84df8c1241be

6790178f55d4


c92f5dd2c42f
6790178f55d4





bcb2c0d69178
6790178f55d4
6790178f55d4
b8b27a44ddf1
4ae1ff7fe8ad
92ab953bd647



6790178f55d4










c92f5dd2c42f
7e9a57e62675
6790178f55d4


08337fdac215
c92f5dd2c42f
6790178f55d4





84df8c1241be

6790178f55d4
23b1134838ef
6790178f55d4








19228a6a5925
6790178f55d4





84df8c1241be

6790178f55d4
84df8c1241be
74ea0e599215
6790178f55d4


a5023574d120
518fb721de36

c172708d38a4
a320e3c3d635
518fb721de36


84df8c1241be
518fb721de36


6790178f55d4
84df8c1241be

6790178f55d4
7e9a57e62675

518fb721de36
7e9a57e62675
518fb721de36
9eb0c4218aa4
bcb2c0d69178
518fb721de36

518fb721de36



3f7e82759c69
518fb721de36

4b57927045c3
a320e3c3d635
fe953904f346
a320e3c3d635
39aa9b6e3cb1
1e0c66f49762
a320e3c3d635
8f9165c981fe





d04156bca629
a320e3c3d635
fe953904f346
a320e3c3d635
39aa9b6e3cb1
1e0c66f49762
a320e3c3d635
d04156bca629
a320e3c3d635
7e9a57e62675
a320e3c3d635


68d8c1cd5158
39aa9b6e3cb1
bcb2c0d69178
68d8c1cd5158
39aa9b6e3cb1
68d8c1cd5158
39aa9b6e3cb1
bcb2c0d69178
68d8c1cd5158
39aa9b6e3cb1
68d8c1cd5158
39aa9b6e3cb1
bcb2c0d69178
68d8c1cd5158
39aa9b6e3cb1
fe953904f346
518fb721de36



d2cfdb055d77
77fa44d0e107
7d38a3cb9b9f
94f48ab32b5a
518fb721de36
a320e3c3d635
518fb721de36
780dc9ba4eb6

01bc3a140037
a320e3c3d635
01bc3a140037
a320e3c3d635
d9fe28f9621b
01bc3a140037
a320e3c3d635
01bc3a140037

03746dcbde75



a320e3c3d635
d9fe28f9621b
a320e3c3d635
d9fe28f9621b

a320e3c3d635
d9fe28f9621b
a320e3c3d635
518fb721de36
1e0c66f49762









518fb721de36

a40a9c436b62
b8903eb9836e
518fb721de36
c172708d38a4
23b1134838ef
c172708d38a4
a40a9c436b62
c172708d38a4
84df8c1241be

6790178f55d4
95095e422e94

518fb721de36



95095e422e94
518fb721de36





518fb721de36

1e0c66f49762





























518fb721de36


518fb721de36

1e0c66f49762
518fb721de36














ae0e6544347d
518fb721de36

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
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
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
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
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276


















                                                                           
                     


                                    


                               
                                         
 
                                           

                                                                         

                                                                         
 


                                              

  

                                                       




                                               


                                               

  


                                                

  


                                                

  


                                               

  


                                               

  


                                                

  


                                                

  


                                                 

  


                                               

  




                                              

                         
                             
                      
                                          
                           



                                          
                               
                                   

                                                          
                                       

          
                              
                                    

                                                         
                                      

          
                               
                                   
                                      

          
                               
                                   
                                      

          
                               

                                                          
                                      

          
                                
                                   

                                                           
                                      

          
                                
                                   

                                                           
                                      

          
                               
                                   

                                                          
                                      

          
                               
                                   

                                                          
                                      

          
                                
                                   

                                                           
                                      

          
                                
                                   

                                                           
                                      

          
                                 
                                   

                                                            
                                      

          
                               
                                   

                                                          
                                      
          





                                                         

  

                                          
                               
                                   
                                       

          
                              
                                    

                                                         
                                      

          
                               
                                   
                                   
                                      

          
                               
                                   
                                   
                                      

          
                                  
                                   
                                      

          
                               
                                   
                                   
                                      

          
                               
                                   
                                   
                                      

          
                               
                                   
                                   
                                      

          
                               
                                   
                                   
                                      

          
                               
                                   
                                   
                                      

          
                               
                                   
                                   
                                      

          
                               
                                   
                                   
                                      

          
                               
                                   
                                   
                                      


          
































                                                                               
                     
                                    
                             

                                    
                           
                 
                                  

                                                          

  




























                                             

                                     




                               

































                                             




                                                                          

                                     




                                   


                                                                                

                                                        
                                                              
                                    
                                                                        







                                                                
                                                   
 
                                     


                           


                                                        
 
                                     






                                              
                                                                   

                                                          
                                       
                                                                  


                               





                                                                              
                                                                            


                                                                              


                                               
                             

                               





                                                                              
                                                                            


                                                                              


                                               


                                  







                                                                       



                                                  




                                              
                         

                 




                                              
                                  

                




                                             
                                  
         
                       

 
                                                              

                                                          
                                                   
 
                                     


                           


                                                        













                                        



                                           



                               
                     



                                                               
                                        

 
                                                              

                                                          

                                       


                                     


                                                        






                                        







                                        

                              

                                        
                      



                               
                     

 

                                                                   


                                                          
                          
 

                               
                                                                  

                                    
                                                             
 



                                                                            

                               
                                                                  

                                    
                                                             
 



                                                                            

                                  
                                    
                                                                         




                 

                                                              



                                                          
                                     












                                 

                                                                             


                                                                                




                       

                                                              











                                                          

                                                                              




                               

                                                                              
                              

                                                                             


                                                                                





                       


                                                                
                                                  
 


                               
                                                             
                                        
                                                                          
                      

                                                                      



                               
         



                                  




                                                                              
                     
























                                                                         
                                                               






                                                          

                                                 


                                                              

                                                    
                                                                
                                                                
                                                                 
                                                             
                                                               


                                                 


                                                              


                                                            
                                                               
                                                               

  











                                                                
                                            


                                                              

                                                    
                                                           
                                                           
                                                               
                                                               

  
                                            


                                                              

                                                    
                                                           
                                                           
                                                        
                                                               

  



















                                                                             

                                                                              










                                                                               
                                                

                                                                         
                                                  








                                                                         
                                                

                                                                         
                                                  










                                                                            
                                                        

                                                                         
                                                          










                                                                               
                                                  

                                                                       
                                                          

















                                                                               








                                                                        

                                                               


                                                                       





                                                                              
                                                                   
                              
                                                                            



                                   
                                          
                                                                       

                                                                              
                                                        

                                                                               
                                                          

                                                                               


                                                                 
 


                   


                                                       












                                                                             
                                                                             


                                                       












                                                                             


                                                         

                                                                 


                                                                      
                                            





                                                                          
                            
                            
 
                                       
                                                            



                                                                  










                                                     
                                        
                                                                  


                            
                                                                         
                                





                                                                              

                                        
                                           
                                          








                                                                                
 





                                                                

                                                                         
 
                                     
                    


      
                                                       

                                                                      
                                             
                              


                                              
                                                               


                                                         
                                                      

                                                                       
 

                                                                 
                               
         
 
                                                                   
                  

                               



                                                          
                                                          

                                                             
                                             
                      
                                                                            
                                                                 
                                                                 
                                                                     
                                     





                                                                         
                      
                      
                                                                            
                                                                 
                                                                 
                                                                     
                                     
                      
                
                                                                  


                               
                                                                    
                                                                   
                        
                               
 
                                                                    
                                                               
                        
                               
 
                                                                    
                                                                    
                        
                               
 
                                                            



                                                
                                                           
                                     
                                                            
                                                                 
 
                                                                       
                                                               

                                                                               
                                                         
                                                    
                                                                     
                                                                       
                                                                               
                                
                                                                       

                                                                



                                                                         
                        
                                                                     
                                                                  

                                                                               
                                                                  
                         
                 
 









                                                                                

                                                       
                                                                 
                                                                          
 
                                           
                                                                              
                                          
                                                 
 

                                                                         
 

                                                                          



                                                                    
                                             





                                                

 





























                                                                            


                                                 

                                
                                      














                                                          
                                                                 

                                       
/*
 * tps65910.c  --  TI tps65910
 *
 * Copyright 2010 Texas Instruments Inc.
 *
 * Author: Graeme Gregory <gg@slimlogic.co.uk>
 * Author: Jorge Eduardo Candelaria <jedu@slimlogic.co.uk>
 *
 *  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/module.h>
#include <linux/init.h>
#include <linux/err.h>
#include <linux/of.h>
#include <linux/platform_device.h>
#include <linux/regulator/driver.h>
#include <linux/regulator/machine.h>
#include <linux/slab.h>
#include <linux/gpio.h>
#include <linux/mfd/tps65910.h>
#include <linux/regulator/of_regulator.h>

#define TPS65910_SUPPLY_STATE_ENABLED	0x1
#define EXT_SLEEP_CONTROL (TPS65910_SLEEP_CONTROL_EXT_INPUT_EN1 |	\
			TPS65910_SLEEP_CONTROL_EXT_INPUT_EN2 |		\
			TPS65910_SLEEP_CONTROL_EXT_INPUT_EN3 |		\
			TPS65911_SLEEP_CONTROL_EXT_INPUT_SLEEP)

/* supported VIO voltages in microvolts */
static const unsigned int VIO_VSEL_table[] = {
	1500000, 1800000, 2500000, 3300000,
};

/* VSEL tables for TPS65910 specific LDOs and dcdc's */

/* supported VRTC voltages in microvolts */
static const unsigned int VRTC_VSEL_table[] = {
	1800000,
};

/* supported VDD3 voltages in microvolts */
static const unsigned int VDD3_VSEL_table[] = {
	5000000,
};

/* supported VDIG1 voltages in microvolts */
static const unsigned int VDIG1_VSEL_table[] = {
	1200000, 1500000, 1800000, 2700000,
};

/* supported VDIG2 voltages in microvolts */
static const unsigned int VDIG2_VSEL_table[] = {
	1000000, 1100000, 1200000, 1800000,
};

/* supported VPLL voltages in microvolts */
static const unsigned int VPLL_VSEL_table[] = {
	1000000, 1100000, 1800000, 2500000,
};

/* supported VDAC voltages in microvolts */
static const unsigned int VDAC_VSEL_table[] = {
	1800000, 2600000, 2800000, 2850000,
};

/* supported VAUX1 voltages in microvolts */
static const unsigned int VAUX1_VSEL_table[] = {
	1800000, 2500000, 2800000, 2850000,
};

/* supported VAUX2 voltages in microvolts */
static const unsigned int VAUX2_VSEL_table[] = {
	1800000, 2800000, 2900000, 3300000,
};

/* supported VAUX33 voltages in microvolts */
static const unsigned int VAUX33_VSEL_table[] = {
	1800000, 2000000, 2800000, 3300000,
};

/* supported VMMC voltages in microvolts */
static const unsigned int VMMC_VSEL_table[] = {
	1800000, 2800000, 3000000, 3300000,
};

/* supported BBCH voltages in microvolts */
static const unsigned int VBB_VSEL_table[] = {
	3000000, 2520000, 3150000, 5000000,
};

struct tps_info {
	const char *name;
	const char *vin_name;
	u8 n_voltages;
	const unsigned int *voltage_table;
	int enable_time_us;
};

static struct tps_info tps65910_regs[] = {
	{
		.name = "vrtc",
		.vin_name = "vcc7",
		.n_voltages = ARRAY_SIZE(VRTC_VSEL_table),
		.voltage_table = VRTC_VSEL_table,
		.enable_time_us = 2200,
	},
	{
		.name = "vio",
		.vin_name = "vccio",
		.n_voltages = ARRAY_SIZE(VIO_VSEL_table),
		.voltage_table = VIO_VSEL_table,
		.enable_time_us = 350,
	},
	{
		.name = "vdd1",
		.vin_name = "vcc1",
		.enable_time_us = 350,
	},
	{
		.name = "vdd2",
		.vin_name = "vcc2",
		.enable_time_us = 350,
	},
	{
		.name = "vdd3",
		.n_voltages = ARRAY_SIZE(VDD3_VSEL_table),
		.voltage_table = VDD3_VSEL_table,
		.enable_time_us = 200,
	},
	{
		.name = "vdig1",
		.vin_name = "vcc6",
		.n_voltages = ARRAY_SIZE(VDIG1_VSEL_table),
		.voltage_table = VDIG1_VSEL_table,
		.enable_time_us = 100,
	},
	{
		.name = "vdig2",
		.vin_name = "vcc6",
		.n_voltages = ARRAY_SIZE(VDIG2_VSEL_table),
		.voltage_table = VDIG2_VSEL_table,
		.enable_time_us = 100,
	},
	{
		.name = "vpll",
		.vin_name = "vcc5",
		.n_voltages = ARRAY_SIZE(VPLL_VSEL_table),
		.voltage_table = VPLL_VSEL_table,
		.enable_time_us = 100,
	},
	{
		.name = "vdac",
		.vin_name = "vcc5",
		.n_voltages = ARRAY_SIZE(VDAC_VSEL_table),
		.voltage_table = VDAC_VSEL_table,
		.enable_time_us = 100,
	},
	{
		.name = "vaux1",
		.vin_name = "vcc4",
		.n_voltages = ARRAY_SIZE(VAUX1_VSEL_table),
		.voltage_table = VAUX1_VSEL_table,
		.enable_time_us = 100,
	},
	{
		.name = "vaux2",
		.vin_name = "vcc4",
		.n_voltages = ARRAY_SIZE(VAUX2_VSEL_table),
		.voltage_table = VAUX2_VSEL_table,
		.enable_time_us = 100,
	},
	{
		.name = "vaux33",
		.vin_name = "vcc3",
		.n_voltages = ARRAY_SIZE(VAUX33_VSEL_table),
		.voltage_table = VAUX33_VSEL_table,
		.enable_time_us = 100,
	},
	{
		.name = "vmmc",
		.vin_name = "vcc3",
		.n_voltages = ARRAY_SIZE(VMMC_VSEL_table),
		.voltage_table = VMMC_VSEL_table,
		.enable_time_us = 100,
	},
	{
		.name = "vbb",
		.vin_name = "vcc7",
		.n_voltages = ARRAY_SIZE(VBB_VSEL_table),
		.voltage_table = VBB_VSEL_table,
	},
};

static struct tps_info tps65911_regs[] = {
	{
		.name = "vrtc",
		.vin_name = "vcc7",
		.enable_time_us = 2200,
	},
	{
		.name = "vio",
		.vin_name = "vccio",
		.n_voltages = ARRAY_SIZE(VIO_VSEL_table),
		.voltage_table = VIO_VSEL_table,
		.enable_time_us = 350,
	},
	{
		.name = "vdd1",
		.vin_name = "vcc1",
		.n_voltages = 0x4C,
		.enable_time_us = 350,
	},
	{
		.name = "vdd2",
		.vin_name = "vcc2",
		.n_voltages = 0x4C,
		.enable_time_us = 350,
	},
	{
		.name = "vddctrl",
		.n_voltages = 0x44,
		.enable_time_us = 900,
	},
	{
		.name = "ldo1",
		.vin_name = "vcc6",
		.n_voltages = 0x33,
		.enable_time_us = 420,
	},
	{
		.name = "ldo2",
		.vin_name = "vcc6",
		.n_voltages = 0x33,
		.enable_time_us = 420,
	},
	{
		.name = "ldo3",
		.vin_name = "vcc5",
		.n_voltages = 0x1A,
		.enable_time_us = 230,
	},
	{
		.name = "ldo4",
		.vin_name = "vcc5",
		.n_voltages = 0x33,
		.enable_time_us = 230,
	},
	{
		.name = "ldo5",
		.vin_name = "vcc4",
		.n_voltages = 0x1A,
		.enable_time_us = 230,
	},
	{
		.name = "ldo6",
		.vin_name = "vcc3",
		.n_voltages = 0x1A,
		.enable_time_us = 230,
	},
	{
		.name = "ldo7",
		.vin_name = "vcc3",
		.n_voltages = 0x1A,
		.enable_time_us = 230,
	},
	{
		.name = "ldo8",
		.vin_name = "vcc3",
		.n_voltages = 0x1A,
		.enable_time_us = 230,
	},
};

#define EXT_CONTROL_REG_BITS(id, regs_offs, bits) (((regs_offs) << 8) | (bits))
static unsigned int tps65910_ext_sleep_control[] = {
	0,
	EXT_CONTROL_REG_BITS(VIO,    1, 0),
	EXT_CONTROL_REG_BITS(VDD1,   1, 1),
	EXT_CONTROL_REG_BITS(VDD2,   1, 2),
	EXT_CONTROL_REG_BITS(VDD3,   1, 3),
	EXT_CONTROL_REG_BITS(VDIG1,  0, 1),
	EXT_CONTROL_REG_BITS(VDIG2,  0, 2),
	EXT_CONTROL_REG_BITS(VPLL,   0, 6),
	EXT_CONTROL_REG_BITS(VDAC,   0, 7),
	EXT_CONTROL_REG_BITS(VAUX1,  0, 3),
	EXT_CONTROL_REG_BITS(VAUX2,  0, 4),
	EXT_CONTROL_REG_BITS(VAUX33, 0, 5),
	EXT_CONTROL_REG_BITS(VMMC,   0, 0),
};

static unsigned int tps65911_ext_sleep_control[] = {
	0,
	EXT_CONTROL_REG_BITS(VIO,     1, 0),
	EXT_CONTROL_REG_BITS(VDD1,    1, 1),
	EXT_CONTROL_REG_BITS(VDD2,    1, 2),
	EXT_CONTROL_REG_BITS(VDDCTRL, 1, 3),
	EXT_CONTROL_REG_BITS(LDO1,    0, 1),
	EXT_CONTROL_REG_BITS(LDO2,    0, 2),
	EXT_CONTROL_REG_BITS(LDO3,    0, 7),
	EXT_CONTROL_REG_BITS(LDO4,    0, 6),
	EXT_CONTROL_REG_BITS(LDO5,    0, 3),
	EXT_CONTROL_REG_BITS(LDO6,    0, 0),
	EXT_CONTROL_REG_BITS(LDO7,    0, 5),
	EXT_CONTROL_REG_BITS(LDO8,    0, 4),
};

struct tps65910_reg {
	struct regulator_desc *desc;
	struct tps65910 *mfd;
	struct regulator_dev **rdev;
	struct tps_info **info;
	int num_regulators;
	int mode;
	int  (*get_ctrl_reg)(int);
	unsigned int *ext_sleep_control;
	unsigned int board_ext_control[TPS65910_NUM_REGS];
};

static int tps65910_get_ctrl_register(int id)
{
	switch (id) {
	case TPS65910_REG_VRTC:
		return TPS65910_VRTC;
	case TPS65910_REG_VIO:
		return TPS65910_VIO;
	case TPS65910_REG_VDD1:
		return TPS65910_VDD1;
	case TPS65910_REG_VDD2:
		return TPS65910_VDD2;
	case TPS65910_REG_VDD3:
		return TPS65910_VDD3;
	case TPS65910_REG_VDIG1:
		return TPS65910_VDIG1;
	case TPS65910_REG_VDIG2:
		return TPS65910_VDIG2;
	case TPS65910_REG_VPLL:
		return TPS65910_VPLL;
	case TPS65910_REG_VDAC:
		return TPS65910_VDAC;
	case TPS65910_REG_VAUX1:
		return TPS65910_VAUX1;
	case TPS65910_REG_VAUX2:
		return TPS65910_VAUX2;
	case TPS65910_REG_VAUX33:
		return TPS65910_VAUX33;
	case TPS65910_REG_VMMC:
		return TPS65910_VMMC;
	case TPS65910_REG_VBB:
		return TPS65910_BBCH;
	default:
		return -EINVAL;
	}
}

static int tps65911_get_ctrl_register(int id)
{
	switch (id) {
	case TPS65910_REG_VRTC:
		return TPS65910_VRTC;
	case TPS65910_REG_VIO:
		return TPS65910_VIO;
	case TPS65910_REG_VDD1:
		return TPS65910_VDD1;
	case TPS65910_REG_VDD2:
		return TPS65910_VDD2;
	case TPS65911_REG_VDDCTRL:
		return TPS65911_VDDCTRL;
	case TPS65911_REG_LDO1:
		return TPS65911_LDO1;
	case TPS65911_REG_LDO2:
		return TPS65911_LDO2;
	case TPS65911_REG_LDO3:
		return TPS65911_LDO3;
	case TPS65911_REG_LDO4:
		return TPS65911_LDO4;
	case TPS65911_REG_LDO5:
		return TPS65911_LDO5;
	case TPS65911_REG_LDO6:
		return TPS65911_LDO6;
	case TPS65911_REG_LDO7:
		return TPS65911_LDO7;
	case TPS65911_REG_LDO8:
		return TPS65911_LDO8;
	default:
		return -EINVAL;
	}
}

static int tps65910_set_mode(struct regulator_dev *dev, unsigned int mode)
{
	struct tps65910_reg *pmic = rdev_get_drvdata(dev);
	struct tps65910 *mfd = pmic->mfd;
	int reg, value, id = rdev_get_id(dev);

	reg = pmic->get_ctrl_reg(id);
	if (reg < 0)
		return reg;

	switch (mode) {
	case REGULATOR_MODE_NORMAL:
		return tps65910_reg_update_bits(pmic->mfd, reg,
						LDO_ST_MODE_BIT | LDO_ST_ON_BIT,
						LDO_ST_ON_BIT);
	case REGULATOR_MODE_IDLE:
		value = LDO_ST_ON_BIT | LDO_ST_MODE_BIT;
		return tps65910_reg_set_bits(mfd, reg, value);
	case REGULATOR_MODE_STANDBY:
		return tps65910_reg_clear_bits(mfd, reg, LDO_ST_ON_BIT);
	}

	return -EINVAL;
}

static unsigned int tps65910_get_mode(struct regulator_dev *dev)
{
	struct tps65910_reg *pmic = rdev_get_drvdata(dev);
	int ret, reg, value, id = rdev_get_id(dev);

	reg = pmic->get_ctrl_reg(id);
	if (reg < 0)
		return reg;

	ret = tps65910_reg_read(pmic->mfd, reg, &value);
	if (ret < 0)
		return ret;

	if (!(value & LDO_ST_ON_BIT))
		return REGULATOR_MODE_STANDBY;
	else if (value & LDO_ST_MODE_BIT)
		return REGULATOR_MODE_IDLE;
	else
		return REGULATOR_MODE_NORMAL;
}

static int tps65910_get_voltage_dcdc_sel(struct regulator_dev *dev)
{
	struct tps65910_reg *pmic = rdev_get_drvdata(dev);
	int ret, id = rdev_get_id(dev);
	int opvsel = 0, srvsel = 0, vselmax = 0, mult = 0, sr = 0;

	switch (id) {
	case TPS65910_REG_VDD1:
		ret = tps65910_reg_read(pmic->mfd, TPS65910_VDD1_OP, &opvsel);
		if (ret < 0)
			return ret;
		ret = tps65910_reg_read(pmic->mfd, TPS65910_VDD1, &mult);
		if (ret < 0)
			return ret;
		mult = (mult & VDD1_VGAIN_SEL_MASK) >> VDD1_VGAIN_SEL_SHIFT;
		ret = tps65910_reg_read(pmic->mfd, TPS65910_VDD1_SR, &srvsel);
		if (ret < 0)
			return ret;
		sr = opvsel & VDD1_OP_CMD_MASK;
		opvsel &= VDD1_OP_SEL_MASK;
		srvsel &= VDD1_SR_SEL_MASK;
		vselmax = 75;
		break;
	case TPS65910_REG_VDD2:
		ret = tps65910_reg_read(pmic->mfd, TPS65910_VDD2_OP, &opvsel);
		if (ret < 0)
			return ret;
		ret = tps65910_reg_read(pmic->mfd, TPS65910_VDD2, &mult);
		if (ret < 0)
			return ret;
		mult = (mult & VDD2_VGAIN_SEL_MASK) >> VDD2_VGAIN_SEL_SHIFT;
		ret = tps65910_reg_read(pmic->mfd, TPS65910_VDD2_SR, &srvsel);
		if (ret < 0)
			return ret;
		sr = opvsel & VDD2_OP_CMD_MASK;
		opvsel &= VDD2_OP_SEL_MASK;
		srvsel &= VDD2_SR_SEL_MASK;
		vselmax = 75;
		break;
	case TPS65911_REG_VDDCTRL:
		ret = tps65910_reg_read(pmic->mfd, TPS65911_VDDCTRL_OP,
					&opvsel);
		if (ret < 0)
			return ret;
		ret = tps65910_reg_read(pmic->mfd, TPS65911_VDDCTRL_SR,
					&srvsel);
		if (ret < 0)
			return ret;
		sr = opvsel & VDDCTRL_OP_CMD_MASK;
		opvsel &= VDDCTRL_OP_SEL_MASK;
		srvsel &= VDDCTRL_SR_SEL_MASK;
		vselmax = 64;
		break;
	}

	/* multiplier 0 == 1 but 2,3 normal */
	if (!mult)
		mult = 1;

	if (sr) {
		/* normalise to valid range */
		if (srvsel < 3)
			srvsel = 3;
		if (srvsel > vselmax)
			srvsel = vselmax;
		return srvsel - 3;
	} else {

		/* normalise to valid range*/
		if (opvsel < 3)
			opvsel = 3;
		if (opvsel > vselmax)
			opvsel = vselmax;
		return opvsel - 3;
	}
	return -EINVAL;
}

static int tps65910_get_voltage_sel(struct regulator_dev *dev)
{
	struct tps65910_reg *pmic = rdev_get_drvdata(dev);
	int ret, reg, value, id = rdev_get_id(dev);

	reg = pmic->get_ctrl_reg(id);
	if (reg < 0)
		return reg;

	ret = tps65910_reg_read(pmic->mfd, reg, &value);
	if (ret < 0)
		return ret;

	switch (id) {
	case TPS65910_REG_VIO:
	case TPS65910_REG_VDIG1:
	case TPS65910_REG_VDIG2:
	case TPS65910_REG_VPLL:
	case TPS65910_REG_VDAC:
	case TPS65910_REG_VAUX1:
	case TPS65910_REG_VAUX2:
	case TPS65910_REG_VAUX33:
	case TPS65910_REG_VMMC:
		value &= LDO_SEL_MASK;
		value >>= LDO_SEL_SHIFT;
		break;
	case TPS65910_REG_VBB:
		value &= BBCH_BBSEL_MASK;
		value >>= BBCH_BBSEL_SHIFT;
		break;
	default:
		return -EINVAL;
	}

	return value;
}

static int tps65910_get_voltage_vdd3(struct regulator_dev *dev)
{
	return dev->desc->volt_table[0];
}

static int tps65911_get_voltage_sel(struct regulator_dev *dev)
{
	struct tps65910_reg *pmic = rdev_get_drvdata(dev);
	int ret, id = rdev_get_id(dev);
	unsigned int value, reg;

	reg = pmic->get_ctrl_reg(id);

	ret = tps65910_reg_read(pmic->mfd, reg, &value);
	if (ret < 0)
		return ret;

	switch (id) {
	case TPS65911_REG_LDO1:
	case TPS65911_REG_LDO2:
	case TPS65911_REG_LDO4:
		value &= LDO1_SEL_MASK;
		value >>= LDO_SEL_SHIFT;
		break;
	case TPS65911_REG_LDO3:
	case TPS65911_REG_LDO5:
	case TPS65911_REG_LDO6:
	case TPS65911_REG_LDO7:
	case TPS65911_REG_LDO8: