summaryrefslogtreecommitdiff
path: root/arch/arm/mach-tegra/tegra_bbc_proxy.c
blob: 1b638e4309a882cbae6e4a09fc73a844e8c20f50 (plain)
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
/*
 * arch/arm/mach-tegra/tegra_bbc_proxy.c
 *
 * Copyright (C) 2013 NVIDIA Corporation. All rights reserved.
 *
 *
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 */
#include <linux/err.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/regulator/consumer.h>
#include <linux/slab.h>
#include <linux/edp.h>

#include <mach/isomgr.h>
#include <mach/latency_allowance.h>
#include <mach/tegra_bbc_proxy.h>
#include <mach/tegra_bbc_thermal.h>

#define MAX_MODEM_EDP_STATES 10

#define MAX_ISO_BW_REQ  1200000

struct tegra_bbc_proxy {
	struct edp_client *modem_boot_edp_client;
	struct edp_client modem_edp_client;
	unsigned int modem_edp_states[MAX_MODEM_EDP_STATES];
	int edp_client_registered;
	int edp_boot_client_registered;
	int edp_initialized;
	char *edp_manager_name;
	char *ap_name;
	unsigned int i_breach_ppm; /* percent time current exceeds i_thresh */
	unsigned int i_thresh_3g_adjperiod; /* 3g i_thresh adj period */
	unsigned int i_thresh_lte_adjperiod; /* lte i_thresh adj period */
	unsigned int threshold; /* current edp threshold value */
	unsigned int state; /* current edp state value */
	struct mutex edp_lock; /* lock for edp operations */

	tegra_isomgr_handle isomgr_handle;
	/* last iso settings with proxy driver */
	unsigned int last_bw;
	unsigned int last_ult;
	unsigned int margin; /* current iso margin bw request */
	unsigned int bw; /* current iso bandwidth request */
	unsigned int lt; /* current iso latency tolerance for emc frequency
			    switches */
	struct mutex iso_lock; /* lock for iso operations */

	struct regulator *sim0;
	struct regulator *sim1;
	struct regulator *rf1v7;
	struct regulator *rf2v65;
};


#define EDP_INT_ATTR(field)						\
static ssize_t								\
field ## _show(struct device *pdev, struct device_attribute *attr,      \
	       char *buf)						\
{									\
	struct tegra_bbc_proxy *bbc = dev_get_drvdata(pdev);		\
									\
	if (!bbc)							\
		return -EAGAIN;						\
									\
	return sprintf(buf, "%d\n", bbc->field);			\
}									\
static DEVICE_ATTR(field, S_IRUSR, field ## _show, NULL);

EDP_INT_ATTR(i_breach_ppm);
EDP_INT_ATTR(i_thresh_3g_adjperiod);
EDP_INT_ATTR(i_thresh_lte_adjperiod);

static ssize_t i_max_store(struct device *pdev, struct device_attribute *attr,
			   const char *buff, size_t size)
{
	char *s, *state_i_max, buf[50];
	unsigned int states[MAX_MODEM_EDP_STATES];
	unsigned int num_states = 0;
	int ret;

	/* retrieve max current for supported states */
	strlcpy(buf, buff, sizeof(buf));
	s = strim(buf);
	while (s && (num_states < MAX_MODEM_EDP_STATES)) {
		state_i_max = strsep(&s, ",");
		ret = kstrtoul(state_i_max, 10,
			(unsigned long *)&states[num_states]);
		if (ret) {
			dev_err(pdev, "invalid bbc state-current setting\n");
			goto done;
		}
		num_states++;
	}

	if (s && (num_states >= MAX_MODEM_EDP_STATES)) {
		dev_err(pdev, "number of bbc EDP states exceeded max\n");
		ret = -EINVAL;
		goto done;
	}

	ret = tegra_bbc_proxy_edp_register(pdev, num_states, states);
	if (ret)
		goto done;

	ret = size;
done:
	return ret;
}
static DEVICE_ATTR(i_max, S_IWUSR, NULL, i_max_store);

int tegra_bbc_proxy_edp_register(struct device *dev, u32 num_states,
				u32 *states)
{
	struct edp_manager *mgr;
	struct edp_client *ap;
	int ret;
	int i;
	struct tegra_bbc_proxy *bbc = dev_get_drvdata(dev);

	mutex_lock(&bbc->edp_lock);

	/* client should only be registered once per modem boot */
	if (bbc->edp_client_registered) {
		dev_err(dev, "bbc edp client already registered\n");
		ret = -EBUSY;
		goto done;
	}

	memset(bbc->modem_edp_states, 0, sizeof(bbc->modem_edp_states));
	memset(&bbc->modem_edp_client, 0, sizeof(bbc->modem_edp_client));

	/* retrieve max current for supported states */
	for (i = 0; i < num_states; i++) {
		bbc->modem_edp_states[i] = *states;
		states++;
	}

	strncpy(bbc->modem_edp_client.name, "bbc", EDP_NAME_LEN);
	bbc->modem_edp_client.name[EDP_NAME_LEN - 1] = '\0';
	bbc->modem_edp_client.states = bbc->modem_edp_states;
	bbc->modem_edp_client.num_states = num_states;
	bbc->modem_edp_client.e0_index = 0;
	bbc->modem_edp_client.max_borrowers = 1;
	bbc->modem_edp_client.priority = EDP_MAX_PRIO;

	mgr = edp_get_manager(bbc->edp_manager_name);
	if (!mgr) {
		dev_err(dev, "can't get edp manager\n");
		ret = -EINVAL;
		goto done;
	}

	/* unregister modem_boot_client */
	ret = edp_unregister_client(bbc->modem_boot_edp_client);
	if (ret) {
		dev_err(dev, "unable to register bbc boot edp client\n");
		goto done;
	}

	bbc->edp_boot_client_registered = 0;

	/* register modem client */
	ret = edp_register_client(mgr, &bbc->modem_edp_client);
	if (ret) {
		dev_err(dev, "unable to register bbc edp client\n");
		goto done;
	}

	bbc->edp_client_registered = 1;

	ap = edp_get_client(bbc->ap_name);
	if (!ap) {
		dev_err(dev, "can't get ap client\n");
		goto done;
	}

	ret = edp_register_loan(&bbc->modem_edp_client, ap);
	if (ret && ret != -EEXIST) {
		dev_err(dev, "unable to register bbc loan to ap\n");
		goto done;
	}

done:
	mutex_unlock(&bbc->edp_lock);
	return ret;
}
EXPORT_SYMBOL(tegra_bbc_proxy_edp_register);

static int bbc_edp_request_unlocked(struct device *dev, u32 mode, u32 state,
				u32 threshold)
{
	int ret;
	struct edp_client *c;
	struct tegra_bbc_proxy *bbc = dev_get_drvdata(dev);

	if (!bbc->edp_client_registered)
		return -EINVAL;

	if (state != bbc->state) {
		c = &bbc->modem_edp_client;
		if (state >= c->num_states)
			return -EINVAL;

		ret = edp_update_client_request(c, state, NULL);
		if (ret) {
			dev_err(dev, "state update to %u failed\n", state);
			return ret;
		}
		bbc->state = state;
	}

	if (threshold != bbc->threshold) {
		ret = edp_update_loan_threshold(&bbc->modem_edp_client,
						threshold);
		if (ret) {
			dev_err(dev, "threshold update to %u failed\n",
				threshold);
			return ret;
		}
		bbc->threshold = threshold;
	}

	return 0;
}

int tegra_bbc_proxy_edp_request(struct device *dev, u32 mode, u32 state,
				u32 threshold)
{
	int ret;
	struct tegra_bbc_proxy *bbc = dev_get_drvdata(dev);

	mutex_lock(&bbc->edp_lock);
	ret = bbc_edp_request_unlocked(dev, mode, state, threshold);
	mutex_unlock(&bbc->edp_lock);

	return ret;
}
EXPORT_SYMBOL(tegra_bbc_proxy_edp_request);

static ssize_t request_store(struct device *pdev, struct device_attribute *attr,
			     const char *buff, size_t size)
{
	struct tegra_bbc_proxy *bbc = dev_get_drvdata(pdev);
	unsigned int id;
	int ret;

	if (sscanf(buff, "%u", &id) != 1)
		return -EINVAL;

	if (!bbc->edp_client_registered)
		return -EINVAL;

	mutex_lock(&bbc->edp_lock);
	ret = bbc_edp_request_unlocked(pdev, 0, id, bbc->threshold);
	mutex_unlock(&bbc->edp_lock);

	return ret ? ret : size;
}
static DEVICE_ATTR(request, S_IWUSR, NULL, request_store);

static ssize_t threshold_store(struct device *pdev,
			       struct device_attribute *attr,
			       const char *buff, size_t size)
{
	struct tegra_bbc_proxy *bbc = dev_get_drvdata(pdev);
	unsigned int tv;
	int ret;

	if (sscanf(buff, "%u", &tv) != 1)
		return -EINVAL;

	mutex_lock(&bbc->edp_lock);
	ret = bbc_edp_request_unlocked(pdev, 0, bbc->state, tv);
	mutex_unlock(&bbc->edp_lock);

	return ret ? ret : size;
}
static DEVICE_ATTR(threshold, S_IWUSR, NULL, threshold_store);

static struct device_attribute *edp_attributes[] = {
	&dev_attr_i_breach_ppm,
	&dev_attr_i_thresh_3g_adjperiod,
	&dev_attr_i_thresh_lte_adjperiod,
	&dev_attr_i_max,
	&dev_attr_request,
	&dev_attr_threshold,
	NULL
};

static ssize_t la_bbcr_store(struct device *dev,
		struct device_attribute *attr, const char *buf, size_t size)
{
	unsigned int la_val;

	if (sscanf(buf, "%u", &la_val) != 1)
		return -EINVAL;
	if (la_val > (MAX_ISO_BW_REQ / 1000))
		return -EINVAL;

	tegra_set_latency_allowance(TEGRA_LA_BBCR, la_val);

	return size;
}

static ssize_t la_bbcw_store(struct device *dev,
		struct device_attribute *attr, const char *buf, size_t size)
{
	unsigned int la_val;

	if (sscanf(buf, "%u", &la_val) != 1)
		return -EINVAL;
	if (la_val > (MAX_ISO_BW_REQ / 1000))
		return -EINVAL;

	tegra_set_latency_allowance(TEGRA_LA_BBCW, la_val);

	return size;
}

static int bbc_bw_request_unlocked(struct device *dev, u32 mode, u32 bw,
				u32 lt, u32 margin)
{
	int ret;
	struct tegra_bbc_proxy *bbc = dev_get_drvdata(dev);

	if (bw > MAX_ISO_BW_REQ)
		return -EINVAL;

	if (margin > MAX_ISO_BW_REQ)
		return -EINVAL;

	if (margin != bbc->margin) {
		ret = tegra_isomgr_set_margin(TEGRA_ISO_CLIENT_BBC_0,
			margin, true);
		if (ret) {
			dev_err(dev, "can't margin for bbc bw\n");
			return ret;
		}

		bbc->margin = margin;
	}

	if ((bw != bbc->bw) || (lt != bbc->lt)) {
		ret = tegra_isomgr_reserve(bbc->isomgr_handle, bw, lt);
		if (!ret) {
			dev_err(dev, "can't reserve iso bw\n");
			return ret;
		}
		bbc->bw = bw;

		ret = tegra_isomgr_realize(bbc->isomgr_handle);
		if (!ret) {
			dev_err(dev, "can't realize iso bw\n");
			return ret;
		}
		bbc->lt = lt;

		tegra_set_latency_allowance(TEGRA_LA_BBCR, bw / 1000);
		tegra_set_latency_allowance(TEGRA_LA_BBCW, bw / 1000);
	}

	return 0;
}

int tegra_bbc_proxy_bw_request(struct device *dev, u32 mode, u32 bw, u32 lt,
			u32 margin)
{
	int ret;
	struct tegra_bbc_proxy *bbc = dev_get_drvdata(dev);

	mutex_lock(&bbc->iso_lock);
	bbc->last_bw = bw;
	bbc->last_ult = lt;

	ret = bbc_bw_request_unlocked(dev, mode, bw, lt, margin);
	mutex_unlock(&bbc->iso_lock);

	return ret;
}
EXPORT_SYMBOL(tegra_bbc_proxy_bw_request);

int tegra_bbc_proxy_restore_iso(struct device *dev)
{
	int ret;
	struct tegra_bbc_proxy *bbc = dev_get_drvdata(dev);

	if (!bbc)
		return -EINVAL;

	mutex_lock(&bbc->iso_lock);
	ret = bbc_bw_request_unlocked(dev, 0, bbc->last_bw,
					bbc->last_ult, bbc->margin);
	mutex_unlock(&bbc->iso_lock);

	return 0;
}
EXPORT_SYMBOL(tegra_bbc_proxy_restore_iso);

int tegra_bbc_proxy_clear_iso(struct device *dev)
{
	int ret;
	struct tegra_bbc_proxy *bbc = dev_get_drvdata(dev);

	if (!bbc)
		return -EINVAL;

	mutex_lock(&bbc->iso_lock);
	ret = bbc_bw_request_unlocked(dev, 0, 0,
					1000, bbc->margin);
	mutex_unlock(&bbc->iso_lock);

	return 0;
}
EXPORT_SYMBOL(tegra_bbc_proxy_clear_iso);

static ssize_t iso_res_realize_store(struct device *dev,
		struct device_attribute *attr, const char *buf, size_t size)
{

	unsigned int bw;
	unsigned int ult = 4;
	char buff[50];
	char *val, *s;
	int ret;
	struct tegra_bbc_proxy *bbc = dev_get_drvdata(dev);

	strlcpy(buff, buf, sizeof(buff));
	s = strim(buff);

	/* first param is bw */
	val = strsep(&s, ",");
	ret = kstrtouint(val, 10, &bw);
	if (ret) {
		pr_err("invalid bw setting\n");
		return -EINVAL;
	}

	/* second param is latency */
	if (s) {
		ret = kstrtouint(s, 10, &ult);
		if (ret) {
			pr_err("invalid latency setting\n");
			return -EINVAL;
		}
	}

	mutex_lock(&bbc->iso_lock);
	bbc->last_bw = bw;
	bbc->last_ult = ult;

	ret = bbc_bw_request_unlocked(dev, 0, bw, ult, bbc->margin);
	mutex_unlock(&bbc->iso_lock);

	return ret ? ret : size;
}

static ssize_t iso_register_store(struct device *dev,
		struct device_attribute *attr, const char *buf, size_t size)
{

	unsigned int bw;
	int ret;

	if (sscanf(buf, "%u", &bw) != 1)
		return -EINVAL;

	ret = tegra_bbc_proxy_bw_register(dev, bw);

	return ret ? ret : size;
}

int tegra_bbc_proxy_bw_register(struct device *dev, u32 bw)
{
	int ret;
	struct tegra_bbc_proxy *bbc = dev_get_drvdata(dev);

	mutex_lock(&bbc->iso_lock);

	if (bbc->isomgr_handle)
		tegra_isomgr_unregister(bbc->isomgr_handle);

	bbc->isomgr_handle = tegra_isomgr_register(TEGRA_ISO_CLIENT_BBC_0,
		bw, NULL, NULL);
	ret = PTR_RET(bbc->isomgr_handle);
	if (ret)
		dev_err(dev, "error registering bbc with isomgr\n");

	ret = tegra_isomgr_set_margin(TEGRA_ISO_CLIENT_BBC_0,
		bw, true);
	if (ret)
		dev_err(dev, "can't margin for bbc bw\n");
	else
		bbc->margin = bw;

	mutex_unlock(&bbc->iso_lock);

	return ret;
}
EXPORT_SYMBOL(tegra_bbc_proxy_bw_register);

static ssize_t iso_margin_store(struct device *dev,
		struct device_attribute *attr, const char *buf, size_t size)
{

	unsigned int margin;
	int ret;
	struct tegra_bbc_proxy *bbc = dev_get_drvdata(dev);

	if (sscanf(buf, "%u", &margin) != 1)
		return -EINVAL;

	mutex_lock(&bbc->iso_lock);
	ret = bbc_bw_request_unlocked(dev, 0, bbc->bw, bbc->lt, margin);
	mutex_unlock(&bbc->iso_lock);

	return ret ? ret : size;
}


static DEVICE_ATTR(la_bbcr, S_IWUSR, NULL, la_bbcr_store);
static DEVICE_ATTR(la_bbcw, S_IWUSR, NULL, la_bbcw_store);
static DEVICE_ATTR(iso_reserve_realize, S_IWUSR, NULL, iso_res_realize_store);
static DEVICE_ATTR(iso_register, S_IWUSR, NULL, iso_register_store);
static DEVICE_ATTR(iso_margin, S_IWUSR, NULL, iso_margin_store);


static struct device_attribute *mc_attributes[] = {
	&dev_attr_la_bbcr,
	&dev_attr_la_bbcw,
	&dev_attr_iso_reserve_realize,
	&dev_attr_iso_register,
	&dev_attr_iso_margin,
	NULL
};


#define REG_ATTR(field)							\
static ssize_t								\
field ## _show_state(struct device *dev, struct device_attribute *attr,	\
		     char *buf)						\
{									\
	struct tegra_bbc_proxy *bbc = dev_get_drvdata(dev);		\
									\
	if (!bbc)							\
		return -EAGAIN;						\
									\
	if (regulator_is_enabled(bbc->field))				\
		return sprintf(buf, "enabled\n");			\
	else								\
		return sprintf(buf, "disabled\n");			\
}									\
									\
static ssize_t								\
field ## _store_state(struct device *dev, struct device_attribute *attr,\
		      const char *buf, size_t count)			\
{									\
	struct tegra_bbc_proxy *bbc = dev_get_drvdata(dev);		\
									\
	if (!bbc)							\
		return -EAGAIN;						\
									\
	if (sysfs_streq(buf, "enabled\n") || sysfs_streq(buf, "1"))	\
		regulator_enable(bbc->field);				\
	else if (sysfs_streq(buf, "disabled\n") ||			\
		 sysfs_streq(buf, "0"))					\
		regulator_disable(bbc->field);				\
									\
	return count;							\
}									\
static DEVICE_ATTR(field ## _state, 0644,				\
		   field ## _show_state, field ## _store_state);	\
									\
static ssize_t								\
field ## _show_microvolts(struct device *dev,				\
			  struct device_attribute *attr, char *buf)	\
{									\
	struct tegra_bbc_proxy *bbc = dev_get_drvdata(dev);		\
									\
	if (!bbc)							\
		return -EAGAIN;						\
									\
	return sprintf(buf, "%d\n", regulator_get_voltage(bbc->field));	\
}									\
									\
static ssize_t								\
field ## _store_microvolts(struct device *dev,				\
	struct device_attribute *attr, const char *buf, size_t count)	\
{									\
	struct tegra_bbc_proxy *bbc = dev_get_drvdata(dev);		\
	int value;							\
									\
	if (!bbc)							\
		return -EAGAIN;						\
									\
	if (sscanf(buf, "%d", &value) != 1)				\
		return -EINVAL;						\
									\
	if (value)							\
		regulator_set_voltage(bbc->field, value, value);	\
									\
	return count;							\
}									\
static DEVICE_ATTR(field ## _microvolts, 0644,				\
		   field ## _show_microvolts,				\
		   field ## _store_microvolts);				\
									\
static ssize_t								\
field ## _show_mode(struct device *dev, struct device_attribute *attr,	\
		     char *buf)						\
{									\
	struct tegra_bbc_proxy *bbc = dev_get_drvdata(dev);		\
									\
	if (!bbc)							\
		return -EAGAIN;						\
									\
	switch (regulator_get_mode(bbc->field)) {			\
	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");				\
}									\
									\
static ssize_t								\
field ## _store_mode(struct device *dev, struct device_attribute *attr,	\
		      const char *buf, size_t count)			\
{									\
	struct tegra_bbc_proxy *bbc = dev_get_drvdata(dev);		\
	int mode = 0;							\
									\
	if (!bbc)							\
		return -EAGAIN;						\
									\
	if (sysfs_streq(buf, "fast\n"))					\
		mode = REGULATOR_MODE_FAST;				\
	else if (sysfs_streq(buf, "normal\n"))				\
		mode = REGULATOR_MODE_NORMAL;				\
	else if (sysfs_streq(buf, "idle\n"))				\
		mode = REGULATOR_MODE_IDLE;				\
	else if (sysfs_streq(buf, "standby\n"))				\
		mode = REGULATOR_MODE_STANDBY;				\
	else								\
		return -EINVAL;						\
									\
	if (regulator_set_mode(bbc->field, mode))			\
		return -EINVAL;						\
									\
	return count;							\
}									\
static DEVICE_ATTR(field ## _mode, 0644,				\
		   field ## _show_mode, field ## _store_mode);

REG_ATTR(sim0);
REG_ATTR(sim1);
REG_ATTR(rf1v7);
REG_ATTR(rf2v65);

static struct device_attribute *sim_attributes[] = {
	&dev_attr_sim0_state,
	&dev_attr_sim0_microvolts,
	&dev_attr_sim0_mode,
	&dev_attr_sim1_state,
	&dev_attr_sim1_microvolts,
	&dev_attr_sim1_mode,
	NULL
};

static struct device_attribute *rf_attributes[] = {
	&dev_attr_rf1v7_state,
	&dev_attr_rf1v7_microvolts,
	&dev_attr_rf1v7_mode,
	&dev_attr_rf2v65_state,
	&dev_attr_rf2v65_microvolts,
	&dev_attr_rf2v65_mode,
	NULL
};

static int tegra_bbc_proxy_probe(struct platform_device *pdev)
{
	struct tegra_bbc_proxy_platform_data *pdata = pdev->dev.platform_data;
	struct tegra_bbc_proxy *bbc;
	struct edp_manager *mgr;
	struct device_attribute **attrs;
	struct device_attribute *attr;
	int ret = 0;

	/* check for platform data */
	if (!pdata) {
		dev_err(&pdev->dev, "platform data not available\n");
		return -ENODEV;
	}

	bbc = kzalloc(sizeof(struct tegra_bbc_proxy), GFP_KERNEL);
	if (!bbc) {
		dev_err(&pdev->dev, "failed to allocate memory\n");
		return -ENOMEM;
	}

	if (pdata->modem_boot_edp_client && pdata->edp_manager_name) {
		mutex_init(&bbc->edp_lock);

		/* register bbc boot client */
		bbc->edp_manager_name = pdata->edp_manager_name;
		mgr = edp_get_manager(pdata->edp_manager_name);
		if (!mgr) {
			dev_err(&pdev->dev, "can't get edp manager\n");
			goto error;
		}

		bbc->modem_boot_edp_client = pdata->modem_boot_edp_client;
		ret = edp_register_client(mgr, bbc->modem_boot_edp_client);
		if (ret) {
			dev_err(&pdev->dev,
				"unable to register bbc boot edp client\n");
			goto error;
		}

		/* request E0 */
		ret = edp_update_client_request(bbc->modem_boot_edp_client,
						0, NULL);
		if (ret) {
			dev_err(&pdev->dev,
				"unable to set e0 state\n");
			goto edp_req_error;
		}

		bbc->edp_boot_client_registered = 1;

		bbc->i_breach_ppm = pdata->i_breach_ppm;
		bbc->i_thresh_3g_adjperiod = pdata->i_thresh_3g_adjperiod;
		bbc->i_thresh_lte_adjperiod = pdata->i_thresh_lte_adjperiod;

		attrs = edp_attributes;
		while ((attr = *attrs++)) {
			ret = device_create_file(&pdev->dev, attr);
			if (ret) {
				dev_err(&pdev->dev,
					"can't create sysfs file\n");
				goto edp_req_error;
			}
		}

		bbc->edp_initialized = 1;
		bbc->ap_name = pdata->ap_name;
	}

	mutex_init(&bbc->iso_lock);

	bbc->isomgr_handle = tegra_isomgr_register(TEGRA_ISO_CLIENT_BBC_0,
		MAX_ISO_BW_REQ, NULL, NULL);
	if (!bbc->isomgr_handle)
		goto iso_error;

	tegra_set_latency_allowance(TEGRA_LA_BBCLLR, 640);

	/* statically margin for bbc bw */
	ret = tegra_isomgr_set_margin(TEGRA_ISO_CLIENT_BBC_0,
		MAX_ISO_BW_REQ, true);
	if (ret)
		dev_err(&pdev->dev, "can't margin for bbc bw\n");
	else
		bbc->margin = MAX_ISO_BW_REQ;

	/* thermal zones from bbc */
	tegra_bbc_thermal_init();

	attrs = mc_attributes;
	while ((attr = *attrs++)) {
		ret = device_create_file(&pdev->dev, attr);
		if (ret) {
			dev_err(&pdev->dev, "can't create sysfs file\n");
			goto mc_error;
		}
	}

	bbc->sim0 = regulator_get(&pdev->dev, "vddio_sim0");
	if (IS_ERR(bbc->sim0)) {
		dev_err(&pdev->dev, "vddio_sim0 regulator get failed\n");
		bbc->sim0 = NULL;
		goto sim_error;
	}

	bbc->sim1 = regulator_get(&pdev->dev, "vddio_sim1");
	if (IS_ERR(bbc->sim1)) {
		dev_err(&pdev->dev, "vddio_sim1 regulator get failed\n");
		bbc->sim1 = NULL;
		goto sim_error;
	}

	attrs = sim_attributes;
	while ((attr = *attrs++)) {
		ret = device_create_file(&pdev->dev, attr);
		if (ret) {
			dev_err(&pdev->dev, "can't create sysfs file\n");
			goto sim_error;
		}
	}

	bbc->rf1v7 = regulator_get(&pdev->dev, "vdd_1v7_rf");
	if (IS_ERR(bbc->rf1v7)) {
		dev_info(&pdev->dev,
			 "vdd_1v7_rf regulator not available\n");
		bbc->rf1v7 = NULL;
	}

	bbc->rf2v65 = regulator_get(&pdev->dev, "vdd_2v65_rf");
	if (IS_ERR(bbc->rf2v65)) {
		dev_info(&pdev->dev,
			 "vdd_2v65_rf regulator not available\n");
		bbc->rf2v65 = NULL;
	}

	if (bbc->rf1v7 && bbc->rf2v65) {
		attrs = rf_attributes;
		while ((attr = *attrs++)) {
			ret = device_create_file(&pdev->dev, attr);
			if (ret) {
				dev_err(&pdev->dev,
					"can't create sysfs file\n");
				goto rf_error;
			}
		}
	}

	dev_set_drvdata(&pdev->dev, bbc);

	return 0;

rf_error:
	regulator_put(bbc->rf1v7);
	regulator_put(bbc->rf2v65);
sim_error:
	regulator_put(bbc->sim0);
	regulator_put(bbc->sim1);

	attrs = mc_attributes;
	while ((attr = *attrs++))
		device_remove_file(&pdev->dev, attr);

mc_error:
	tegra_isomgr_unregister(bbc->isomgr_handle);

iso_error:
	if (bbc->edp_initialized) {
		attrs = edp_attributes;
		while ((attr = *attrs++))
			device_remove_file(&pdev->dev, attr);
	}

edp_req_error:
	if (bbc->edp_boot_client_registered)
		edp_unregister_client(bbc->modem_boot_edp_client);

error:
	kfree(bbc);

	return ret;
}

static int __exit tegra_bbc_proxy_remove(struct platform_device *pdev)
{
	struct tegra_bbc_proxy *bbc = platform_get_drvdata(pdev);
	struct device_attribute **attrs;
	struct device_attribute *attr;


	if (bbc->rf1v7 && bbc->rf2v65) {
		attrs = rf_attributes;
		while ((attr = *attrs++))
			device_remove_file(&pdev->dev, attr);
		regulator_put(bbc->rf1v7);
		regulator_put(bbc->rf2v65);
	}

	attrs = sim_attributes;
	while ((attr = *attrs++))
		device_remove_file(&pdev->dev, attr);

	regulator_put(bbc->sim0);
	regulator_put(bbc->sim1);

	attrs = mc_attributes;
	while ((attr = *attrs++))
		device_remove_file(&pdev->dev, attr);

	tegra_isomgr_unregister(bbc->isomgr_handle);

	if (bbc->edp_initialized) {
		attrs = edp_attributes;
		while ((attr = *attrs++))
			device_remove_file(&pdev->dev, attr);
	}

	if (bbc->edp_boot_client_registered)
		edp_unregister_client(bbc->modem_boot_edp_client);

	if (bbc->edp_client_registered)
		edp_unregister_client(&bbc->modem_edp_client);

	kfree(bbc);

	return 0;
}

#ifdef CONFIG_PM
static int tegra_bbc_proxy_suspend(struct platform_device *pdev,
					pm_message_t state)
{
	return 0;
}

static int tegra_bbc_proxy_resume(struct platform_device *pdev)
{
	return 0;
}
#endif

static struct platform_driver tegra_bbc_proxy_driver = {
	.driver = {
		.name = "tegra_bbc_proxy",
		.owner = THIS_MODULE,
	},
	.probe = tegra_bbc_proxy_probe,
	.remove = tegra_bbc_proxy_remove,
#ifdef CONFIG_PM
	.suspend = tegra_bbc_proxy_suspend,
	.resume = tegra_bbc_proxy_resume,
#endif
};

static int __init tegra_bbc_proxy_init(void)
{
	return platform_driver_register(&tegra_bbc_proxy_driver);
}

static void __exit tegra_bbc_proxy_exit(void)
{
	platform_driver_unregister(&tegra_bbc_proxy_driver);
}

module_init(tegra_bbc_proxy_init);
module_exit(tegra_bbc_proxy_exit);

MODULE_DESCRIPTION("Tegra T148 BBC Proxy Module");
MODULE_LICENSE("GPL");