aboutsummaryrefslogtreecommitdiff
path: root/drivers/platform/msm/qca1530.c
blob: fbba4aedf37cac1fb68586ee1ffd123363a4d1f2 (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
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
/* Copyright (c) 2013-2014, The Linux Foundation. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 and
 * only version 2 as published by the Free Software Foundation.
 *
 * 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.
 */

#define pr_fmt(fmt) "%s:%d] " fmt "\n", __func__, __LINE__

#include <linux/init.h>
#include <linux/clk.h>
#include <linux/err.h>
#include <linux/fs.h>
#include <linux/gpio.h>
#include <linux/io.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/regulator/consumer.h>
#include <linux/platform_device.h>
#include <linux/uaccess.h>
#include <linux/types.h>
#include <linux/kobject.h>
#include <linux/string.h>
#include <linux/sysfs.h>

/* RTC clock name for lookup */
#define QCA1530_RTC_CLK_ID		"qca,rtc_clk"
/* TCXO clock name for lookup */
#define QCA1530_TCXO_CLK_ID		"qca,tcxo_clk"
/* SoC power regulator prefix for DTS */
#define QCA1530_OF_PWR_REG_NAME		"qca,pwr"
/* SoC optional power regulator prefix for DTS */
#define QCA1530_OF_PWR_OPT_REG_NAME	"qca,pwr2"
/* SoC power regulator pin for DTS */
#define QCA1530_OF_PWR_GPIO_NAME	"qca,pwr-gpio"
/* Reset power regulator prefix for DTS */
#define QCA1530_OF_RESET_REG_NAME	"qca,reset"
/* Reset pin name for DTS */
#define QCA1530_OF_RESET_GPIO_NAME	"qca,reset-gpio"
/* Clock pin name for DTS */
#define QCA1530_OF_CLK_GPIO_NAME	"qca,clk-gpio"
/* xLNA power regulator prefix for DTS */
#define QCA1530_OF_XLNA_REG_NAME	"qca,xlna"
/* xLNA voltage property name in DTS */
#define QCA1530_OF_XLNA_POWER_VOLTAGE	"qca,xlna-voltage-level"
/* xLNA current property name in DTS */
#define QCA1530_OF_XLNA_POWER_CURRENT	"qca,xlna-current-level"
/* xLNA pin name for DTS */
#define QCA1530_OF_XLNA_GPIO_NAME	"qca,xlna-gpio"

#define QCA1530_ALL_FLG		0x0f
#define QCA1530_POWER_FLG	0x01
#define QCA1530_XLNA_FLG	0x02
#define QCA1530_CLK_FLG		0x04
#define QCA1530_RESET_FLG	0x08

#define GPIO_OPTIONAL_FLG	0x01
#define GPIO_EXPORT_FLG		0x02
#define GPIO_OUTDIR_FLG		0x04

#define SYSFS_NODE_NAME			"qca1530"

/**
 * struct qca1530_static - keeps all driver instance variables
 * @pdev:          Platform device data
 * @reset_gpio:    Number of GPIO pin for reset control, or -1
 * @reset_reg:     Handle to power regulator for reset control, or NULL
 * @rtc_clk:       RTC clock handle (32KHz)
 * @rtc_clk_gpio:  RTC clock gppio, or -1
 * @tcxo_clk:      TCXO clock handle
 * @pwr_reg:       Main SoC power regulator handle, or NULL
 * @pwr_gpio:      Main SoC power regulator GPIO, or -1
 * @xlna_reg:      xLNA power regulator handle, or NULL
 * @xlna_gpio:     xLNA power GPIO, or -1
 *
 * The structure contains all driver instance variables.
 */
struct qca1530_static {
	struct platform_device	*pdev;
	int			reset_gpio;
	struct regulator	*reset_reg;
	struct clk		*rtc_clk;
	int			rtc_clk_gpio;
	struct clk		*tcxo_clk;
	struct regulator	*pwr_reg;
	struct regulator	*pwr_opt_reg;
	int			pwr_gpio;
	struct regulator	*xlna_reg;
	int			xlna_gpio;
	int			chip_state;
};

/*
 * qca1530_data - driver instance data
 */
static struct qca1530_static qca1530_data = {
	.reset_gpio = -1,
	.rtc_clk_gpio = -1,
	.pwr_gpio = -1,
	.xlna_gpio = -1,
	.chip_state = 0,
};

/**
 * qca1530_deinit_gpio() - release GPIO resource
 * @pdev: platform device data
 * @pgpio: pointer to GPIO handle
 * @flags: flags showing how gpio was initialized
 *
 * Function releases GPIO handle allocated by the driver. By default the
 * GPIO pin is removed from user space, switched to input direction and
 * then released.
 *
 * GPIO handle is set to -1.
 */
static void qca1530_deinit_gpio(struct platform_device *pdev, int *pgpio,
	int flags)
{
	if (*pgpio >= 0) {
		pr_debug("Releasing GPIO: %d, %d", *pgpio, flags);
		if (flags & GPIO_EXPORT_FLG)
			gpio_unexport(*pgpio);
		gpio_direction_input(*pgpio);
		devm_gpio_free(&pdev->dev, *pgpio);
		*pgpio = -1;
	}
}

/**
 * qca1530_init_gpio() - initialize GPIO resource
 * @pdev: platform device data
 * @pgpio: pointer to GPIO handle
 * @pgio_name: name of gpio in device tree
 * @flags: flags to control export, direction, etc
 *
 * Function initializes gpio and returns 0 on sucess.
 *
 */
static int qca1530_init_gpio(struct platform_device *pdev, int *pgpio,
	const char *gpio_name, int flags)
{
	int ret;

	pr_debug("Initializing gpio %s, flags %d", gpio_name, flags);

	ret = of_get_named_gpio(pdev->dev.of_node, gpio_name, 0);

	if (ret == -ENOENT && (flags & GPIO_OPTIONAL_FLG)) {
		*pgpio = -1;
		pr_debug("Optional GPIO is not defined");
		return 0;
	} else if (ret < 0) {
		pr_err("Error getting GPIO from config: %d", ret);
		goto err_gpio_init;
	}
	*pgpio = ret;

	ret = devm_gpio_request(&pdev->dev, *pgpio, gpio_name);
	if (ret < 0) {
		pr_err("failed to request gpio %d, %d", *pgpio, ret);
		goto err_gpio_init;
	}

	if (flags & GPIO_OUTDIR_FLG) {
		ret = gpio_direction_output(*pgpio, 0);
		if (ret < 0) {
			pr_err("failed to change direction for gpio %d, %d",
				*pgpio, ret);
			goto err_gpio_set_dir;
		}
	}

	if (flags & GPIO_EXPORT_FLG) {
		ret = gpio_export(*pgpio, false);
		if (ret < 0) {
			pr_err("failed to export gpio %d for user, %d",
				*pgpio, ret);
			goto err_gpio_export;
		}
	}

	pr_debug("Initialized gpio %s: %d", gpio_name, *pgpio);
	return 0;

err_gpio_export:
	gpio_direction_input(*pgpio);

err_gpio_set_dir:
	devm_gpio_free(&pdev->dev, *pgpio);

err_gpio_init:
	*pgpio = -1;
	return ret;
}

/**
 * qca1530_deinit_regulator() - release power regulator resource
 * @ppwr: pointer to power regulator handle
 *
 * Function releases power regulator and sets handle to NULL.
 */
static void qca1530_deinit_regulator(struct regulator **ppwr)
{
	if (*ppwr) {
		devm_regulator_put(*ppwr);
		*ppwr = NULL;
	}
}

/**
 * qca1530_clk_prepare() - prepare or unprepare clock
 * @clk:  clock handle
 * @mode: 0 to unprepare, 1 to prepare
 *
 * Function prepares or unprepares clock and logs the result.
 *
 * Return: 0 on success, error code on failure
 */
static int qca1530_clk_prepare(struct clk *clk, int mode)
{
	int ret = 0;

	if (mode)
		ret = clk_prepare_enable(clk);
	else
		clk_disable_unprepare(clk);

	pr_debug("Configured clock (%p): mode=%d ret=%d", clk, mode, ret);

	return ret;
}

/**
 * qca1530_clk_set_gpio() - enable or disable RTC GPIO pin
 * @mode: 1 to enable, 0 to disable
 *
 * Function enables or disable clock GPIO pin and logs the result.
 */
static void qca1530_clk_set_gpio(int mode)
{
	gpio_set_value(qca1530_data.rtc_clk_gpio, mode ? 1 : 0);
	pr_debug("Set clk GPIO (%d): mode=%d", qca1530_data.rtc_clk_gpio, mode);
}

/**
 * qca1530_clk_set() - start/stop initialized clocks
 * @mode: 1 to start clocks, 0 to stop
 *
 * Function turns clocks on or off. Clock configuration is optional, however
 * when configured, the following order is used: RTC GPIO pin, RTC clock,
 * TCXO clock. When switching off, the order is reveresed.
 *
 * Return: 0 on success, error code on failure.
 */
static int qca1530_clk_set(int mode)
{
	int ret = 0;

	if (qca1530_data.rtc_clk_gpio < 0 &&
		!qca1530_data.rtc_clk &&
		!qca1530_data.tcxo_clk) {
		ret = -ENOSYS;
	} else {
		if (qca1530_data.rtc_clk_gpio >= 0)
			qca1530_clk_set_gpio(mode);
		if (qca1530_data.rtc_clk)
			ret = qca1530_clk_prepare(qca1530_data.rtc_clk, mode);
		if (!ret && qca1530_data.tcxo_clk)
			ret = qca1530_clk_prepare(qca1530_data.tcxo_clk, mode);
	}

	pr_debug("Configured clk: mode=%d ret=%d", mode, ret);
	return ret;
}

/**
 * qca1530_clk_release_clock() - release clocks
 * @pdev: platform device data
 * @clk_name: clock name
 * @clk: pointer to clock handle pointer
 *
 * Function releases initialized clocks and sets clock handle
 * pointer to NULL.
 */
static void qca1530_clk_release_clock(
	struct platform_device *pdev,
	const char *clk_name,
	struct clk **clk)
{
	if (*clk) {
		pr_debug("Unregistering CLK: name=%s", clk_name);
		devm_clk_put(&pdev->dev, *clk);
		*clk = NULL;
	}
}

/**
 * qca1530_clk_release_clocks() - release clocks
 * @pdev: platform device data
 *
 * Function releases initialized clocks and sets clock handle
 * pointers to NULL.
 */
static void qca1530_clk_release_clocks(struct platform_device *pdev)
{
	qca1530_deinit_gpio(pdev, &qca1530_data.rtc_clk_gpio,
		GPIO_OPTIONAL_FLG | GPIO_OUTDIR_FLG);
	qca1530_clk_release_clock(pdev, QCA1530_TCXO_CLK_ID,
		&qca1530_data.tcxo_clk);
	qca1530_clk_release_clock(pdev, QCA1530_RTC_CLK_ID,
		&qca1530_data.rtc_clk);
}

/**
 * qca1530_clk_init() - allocate and initialize clock input resources
 * @pdev: platform device data
 *
 * Function tries to connect to RTC and TCXO clocks and obtain clock GPIO
 * pin.
 *
 * Return: 0 on success, error code on failure.
 */
static int qca1530_clk_init(struct platform_device *pdev)
{
	int ret;

	pr_debug("Initializing clock");

	qca1530_data.rtc_clk = devm_clk_get(&pdev->dev, QCA1530_RTC_CLK_ID);

	if (IS_ERR(qca1530_data.rtc_clk)) {
		ret = PTR_ERR(qca1530_data.rtc_clk);
		qca1530_data.rtc_clk = NULL;
		if (ret == -ENOENT) {
			pr_debug("No RTC clock controller");
		} else {
			pr_err("Clock init error: device=%s clock=%s ret=%d",
				dev_name(&pdev->dev), QCA1530_RTC_CLK_ID,
				ret);
			goto err_0;
		}
	} else
		pr_debug("Ref to clock %s obtained: %p",
				QCA1530_RTC_CLK_ID, qca1530_data.rtc_clk);

	qca1530_data.tcxo_clk = devm_clk_get(&pdev->dev, QCA1530_TCXO_CLK_ID);
	if (IS_ERR(qca1530_data.tcxo_clk)) {
		ret = PTR_ERR(qca1530_data.tcxo_clk);
		qca1530_data.tcxo_clk = NULL;
		if (ret == -ENOENT) {
			pr_debug("No TCXO clock controller");
		} else {
			pr_err("Clock init error: device=%s clock=%s ret=%d",
				dev_name(&pdev->dev), QCA1530_TCXO_CLK_ID,
				ret);
			goto err_1;
		}
	} else
		pr_debug("Ref to clock %s obtained: %p",
				QCA1530_TCXO_CLK_ID, qca1530_data.tcxo_clk);

	ret = qca1530_init_gpio(pdev, &qca1530_data.rtc_clk_gpio,
		QCA1530_OF_CLK_GPIO_NAME, GPIO_OPTIONAL_FLG | GPIO_OUTDIR_FLG);
	if (ret)
		goto err_1;

	ret = qca1530_clk_set(1);
	if (ret < 0) {
		pr_err("Clock set error: ret=%d", ret);
		goto err_1;
	}

	pr_debug("Clock init done: GPIO=%s RTC=%s TCXO=%s",
		qca1530_data.rtc_clk_gpio >= 0 ? "ok" : "unused",
		qca1530_data.rtc_clk ? "ok" : "unused",
		qca1530_data.tcxo_clk ? "ok" : "unused");

	return 0;

err_1:
	qca1530_clk_release_clocks(pdev);
err_0:
	pr_err("init error: ret=%d", ret);

	return ret;
}

/**
 * qca1530_clk_deinit() - release clock control resources
 * @pdev: platform device data
 *
 * Function disables clock control and releases GPIO and clock resources.
 */
static void qca1530_clk_deinit(struct platform_device *pdev)
{
	qca1530_clk_set(0);
	qca1530_deinit_gpio(pdev, &qca1530_data.rtc_clk_gpio,
		GPIO_OPTIONAL_FLG | GPIO_OUTDIR_FLG);
	qca1530_clk_release_clocks(pdev);
}

/**
 * qca1530_pwr_set_gpio() - set power GPIO line
 * @mode: 1 to enable, 0 to disable
 *
 * Function sets GPIO pin value and logs the result.
 */
static void qca1530_pwr_set_gpio(int mode)
{
	gpio_set_value(qca1530_data.pwr_gpio, mode ? 1 : 0);

	pr_debug("Configuring power(GPIO): mode=%d", mode);
}

/**
 * qca1530_pwr_set_regulator() - control regulator and log results
 * @reg:  regulator to control
 * @mode: 1 to enable, 0 to disable
 *
 * Function controls power regulator and logs results.
 *
 * Return: 0 on success, error code otherwise
 */
static int qca1530_pwr_set_regulator(struct regulator *reg, int mode)
{
	int ret;

	pr_debug("Setting regulator: mode=%d regulator=%p", mode, reg);

	if (mode) {
		ret = regulator_enable(reg);
		if (ret)
			pr_err("Failed to enable regulator, rc=%d", ret);
		else
			pr_debug("Regulator %p was enabled (%d)", reg, ret);

	} else {
		if (!regulator_is_enabled(reg)) {
			ret = 0;
		} else {
			ret = regulator_disable(reg);
			if (ret)
				pr_err("Failed to disable regulator, rc=%d",
					ret);
			else
				pr_debug("Regulator %p was disabled (%d)", reg,
					ret);
		}
	}

	pr_debug("Regulator set result: regulator=%p mode=%d ret=%d", reg, mode,
		ret);

	return ret;
}

/**
 * qca1530_pwr_init_regulator() - initialize power regulator if configured
 * @pdev: platform device data
 * @name: regulator name prefix in DTS file
 * @ppwr: pointer to resulting variable
 *
 * Function initializes power regulator if DTS configuration is present.
 *
 * Return: 0 on success, error code otherwise
 */
static int qca1530_pwr_init_regulator(
	struct platform_device *pdev,
	const char *name, struct regulator **ppwr)
{
	int ret;
	struct regulator *pwr;

	pwr = devm_regulator_get(&pdev->dev, name);
	if (IS_ERR_OR_NULL(pwr)) {
		ret = PTR_ERR(pwr);
		*ppwr = NULL;
		if (ret == -ENODEV) {
			pr_debug("Power regulator %s is not defined",
				name);
			ret = 0;
		} else
			pr_err("Failed to get regulator, ret=%d", ret);
	} else {
		pr_debug("Ref to regulator %s obtained: %p", name, pwr);
		*ppwr = pwr;
		ret = 0;
	}

	return ret;
}
/**
 * qca1530_power_set() - enable or disable SoC power
 * @mode: 1 to enable, 0 to disable
 *
 * Function enables or disables SoC power line.
 *
 * Return: 0 on success, error code otherwise
 */
static int qca1530_pwr_set(int mode)
{
	int ret = 0;
	if (!qca1530_data.pwr_reg &&
		!qca1530_data.pwr_opt_reg &&
		qca1530_data.pwr_gpio < 0) {
		ret = -ENOSYS;
	} else {
		if (qca1530_data.pwr_reg)
			ret = qca1530_pwr_set_regulator(
				qca1530_data.pwr_reg, mode);
		if (!ret && qca1530_data.pwr_opt_reg)
			ret = qca1530_pwr_set_regulator(
				qca1530_data.pwr_opt_reg, mode);
		if (!ret && qca1530_data.pwr_gpio >= 0)
			qca1530_pwr_set_gpio(mode);
	}
	return ret;
}

/**
 * qca1530_pwr_deinit() - release main SoC power control
 * @pdev: platform device data
 *
 * Function releases power regulator and power GPIO pin.
 */
static void qca1530_pwr_deinit(struct platform_device *pdev)
{
	qca1530_pwr_set(0);
	qca1530_deinit_gpio(pdev, &qca1530_data.pwr_gpio,
		GPIO_OPTIONAL_FLG | GPIO_OUTDIR_FLG);
	qca1530_deinit_regulator(&qca1530_data.pwr_opt_reg);
	qca1530_deinit_regulator(&qca1530_data.pwr_reg);
}

/**
 * qca1530_pwr_init() - allocate SoC power control
 * @pdev: platform device data
 *
 * Function allocates and enables SoC power control.
 *
 * Return: 0 on success, error code otherwise
 */
static int qca1530_pwr_init(struct platform_device *pdev)
{
	int ret = 0;

	pr_debug("Initializing power control");

	ret = qca1530_pwr_init_regulator(
		pdev, QCA1530_OF_PWR_REG_NAME, &qca1530_data.pwr_reg);
	if (ret)
		goto err_0;

	ret = qca1530_pwr_init_regulator(
		pdev, QCA1530_OF_PWR_OPT_REG_NAME, &qca1530_data.pwr_opt_reg);
	if (ret)
		goto err_0;

	ret = qca1530_init_gpio(pdev, &qca1530_data.pwr_gpio,
		QCA1530_OF_PWR_GPIO_NAME, GPIO_OPTIONAL_FLG | GPIO_OUTDIR_FLG);
	if (ret)
		goto err_0;

	if (qca1530_data.pwr_reg ||
		qca1530_data.pwr_gpio >= 0 ||
		qca1530_data.pwr_opt_reg) {
		ret = qca1530_pwr_set(1);
		if (ret) {
			pr_err("Failed to enable power, rc=%d", ret);
			goto err_0;
		}
		pr_debug("Configured: reg=%p gpio=%d",
			qca1530_data.pwr_reg,
			qca1530_data.pwr_gpio);
	} else {
		pr_debug("Power control is not available");
	}

	return ret;

err_0:
	qca1530_pwr_deinit(pdev);
	return ret;
}

/**
 * qca1530_reset_deinit() - release reset control resources
 * @pdev: platform device data
 *
 * Function releases reset line GPIO and switches off and releases power
 * regulator.
 */
static void qca1530_reset_deinit(struct platform_device *pdev)
{
	pr_debug("Releasing reset control");
	qca1530_deinit_gpio(pdev, &qca1530_data.reset_gpio,
		GPIO_OUTDIR_FLG);
	if (qca1530_data.reset_reg) {
		qca1530_pwr_set_regulator(qca1530_data.reset_reg, 0);
		qca1530_deinit_regulator(&qca1530_data.reset_reg);
	}
}

/**
 * qca1530_reset_init() - initialize SoC reset line resources
 * @pdev: platform device data
 *
 * Function initializes reset line resources, including configuration of GPIO
 * pin and power regulator.
 *
 * Return: 0 on success, error code otherwise
 */
static int qca1530_reset_init(struct platform_device *pdev)
{
	int ret;

	pr_debug("Initializing reset control");

	ret = qca1530_init_gpio(pdev, &qca1530_data.reset_gpio,
		QCA1530_OF_RESET_GPIO_NAME, GPIO_OUTDIR_FLG);

	if (ret < 0) {
		goto err_0;
	}

	ret = qca1530_pwr_init_regulator(
		pdev, QCA1530_OF_RESET_REG_NAME, &qca1530_data.reset_reg);
	if (ret)
		goto err_0;

	if (qca1530_data.reset_reg) {
		ret = qca1530_pwr_set_regulator(qca1530_data.reset_reg, 1);
		if (ret) {
			pr_err("failed to turn on reset regulator");
			goto err_0;
		}
	}

	return 0;

err_0:
	qca1530_reset_deinit(pdev);
	return ret;
}

/**
 * qca1530_xlna_set() - enables and disables xLNA
 * @mode: 0 to disable, 1 to enable
 *
 * Function controls xLNA. It configures power regulator and GPIO pin to
 * enable or disable the amplifier.
 *
 * Return: 0 on success, error code on error
 */
static int qca1530_xlna_set(int mode)
{
	int ret = 0;

	if (!qca1530_data.xlna_reg && qca1530_data.xlna_gpio < 0)
		ret = -ENOSYS;
	else if (mode) {
		if (qca1530_data.xlna_reg) {
			ret = qca1530_pwr_set_regulator(
				qca1530_data.xlna_reg, mode);
		}

		if (!ret && qca1530_data.xlna_gpio >= 0)
			gpio_set_value(qca1530_data.xlna_gpio, 1);
	} else {
		if (qca1530_data.xlna_gpio >= 0)
			gpio_set_value(qca1530_data.xlna_gpio, 0);
		if (qca1530_data.xlna_reg)
			ret = qca1530_pwr_set_regulator(
				qca1530_data.xlna_reg, mode);
	}
	return ret;
}

/**
 * qca1530_read_u32_arr_property() - read u32 values property
 * @pdev: platform device data
 * @pname: property name
 * @num_values: array size
 * @values: array to put results to
 *
 * Function reads property with given name. Filles passed array of
 * u32 values.
 *
 * Return: 0 on successful read, error code on error.
 */
static int qca1530_read_u32_arr_property(struct platform_device *pdev,
	const char *pname, const int num_values, u32 *values)
{
	int len = 0;
	int ret = 0;

	if (pdev->dev.of_node) {
		const void *rp = of_get_property(pdev->dev.of_node,
			pname, &len);
		if (NULL != rp && len == sizeof(u32)*num_values)
			ret = of_property_read_u32_array(pdev->dev.of_node,
				pname, values, num_values);
		else
			ret = -ENODATA;
	} else{
		ret = -EINVAL;
	}
	if (ret)
		pr_err("Error reading property %s, ret:%d", pname, ret);
	return ret;
}

/**
 * qca1530_xlna_deinit() - release all xLNA resources
 * @pdev: platform device data
 *
 * Function switches off xLNA and releases all allocated resources.
 */
static void qca1530_xlna_deinit(struct platform_device *pdev)
{
	qca1530_xlna_set(0);
	qca1530_deinit_gpio(pdev, &qca1530_data.xlna_gpio,
		GPIO_OPTIONAL_FLG | GPIO_OUTDIR_FLG);
	qca1530_deinit_regulator(&qca1530_data.xlna_reg);
}

/**
 * qca1530_xlna_init() - allocates xLNA resources
 * @pdev: platform device data
 *
 * Function allocates xLNA resources according to DTS configuration.
 * When configured, the following facilities are used:
 *   - power regulator (optionally)
 *   - GPIO pin (optionally)
 *
 * After initialization, xLNA is enabled.
 *
 * Return: 0 on successful initialization, error code on error.
 */
static int qca1530_xlna_init(struct platform_device *pdev)
{
	int ret = 0;
	u32 tmp[2];

	pr_debug("Initializing xLNA control");

	ret = qca1530_pwr_init_regulator(
		pdev, QCA1530_OF_XLNA_REG_NAME, &qca1530_data.xlna_reg);
	if (ret)
		goto err_0;

	if (qca1530_data.xlna_reg) {
		ret = qca1530_read_u32_arr_property(pdev,
			QCA1530_OF_XLNA_POWER_VOLTAGE, 2, tmp);
		if (!ret) {
			ret = regulator_set_voltage(qca1530_data.xlna_reg,
				tmp[0], tmp[1]);
			if (ret)
				pr_warn("Failed to set voltage, ret=%d", ret);
			else
				pr_debug("Regulator %p voltage was set (%d)",
					qca1530_data.xlna_reg, ret);
		}

		ret = qca1530_read_u32_arr_property(pdev,
			QCA1530_OF_XLNA_POWER_CURRENT, 2, tmp);
		if (!ret) {
			ret = regulator_set_optimum_mode(qca1530_data.xlna_reg,
				tmp[0]);
			if (ret < 0)
				pr_warn("Failed to set optimum mode, ret=%d",
					ret);
			else
				pr_debug("Optimum mode for %p was set (%d)",
					qca1530_data.xlna_reg, ret);
		}
	}

	ret = qca1530_init_gpio(pdev, &qca1530_data.xlna_gpio,
		QCA1530_OF_XLNA_GPIO_NAME, GPIO_OPTIONAL_FLG | GPIO_OUTDIR_FLG);
	if (ret)
		goto err_0;

	if (qca1530_data.xlna_reg || qca1530_data.xlna_gpio >= 0) {
		ret = qca1530_xlna_set(1);
		if (ret) {
			pr_err("Failed to enable xLNA, rc=%d", ret);
			goto err_0;
		}
		pr_debug("Configured: reg=%p gpio=%d",
			qca1530_data.xlna_reg,
			qca1530_data.xlna_gpio);
	} else {
		pr_debug("xLNA control is not available");
	}

	return ret;

err_0:
	qca1530_xlna_deinit(pdev);
	return ret;
}

/**
 * qca1530_set_chip_state() - performs driver initialization
 * @pdev: platform device data
 * @on: target state, 1 - on, 0 - off
 *
 * The driver probing includes initialization of the subsystems in the
 * following order:
 *   - reset control
 *   - RTC and TXCO Clock control
 *   - xLNA control
 *   - SoC power control
 */
static int qca1530_set_chip_state(struct platform_device *pdev,
	const int on_mask)
{
	int ret;
	int on_req;

	ret = 0;
	on_req = on_mask;
	pr_debug("on_mask=%d", on_mask);

	pr_debug("Switching on");

	if (!(qca1530_data.chip_state & QCA1530_RESET_FLG) &&
		(on_req & QCA1530_RESET_FLG)) {
		ret = qca1530_reset_init(pdev);
		if (ret < 0) {
			pr_err("failed to init reset: %d", ret);
			on_req = 0;
			goto switching_off;
		} else {
			qca1530_data.chip_state |= QCA1530_RESET_FLG;
		}
	}

	if (!(qca1530_data.chip_state & QCA1530_CLK_FLG) &&
		(on_req & QCA1530_CLK_FLG)) {
		ret = qca1530_clk_init(pdev);
		if (ret) {
			pr_err("failed to initialize clock: %d", ret);
			on_req = 0;
			goto switching_off;
		} else {
			qca1530_data.chip_state |= QCA1530_CLK_FLG;
		}
	}

	if (!(qca1530_data.chip_state & QCA1530_XLNA_FLG) &&
		(on_req & QCA1530_XLNA_FLG)) {
		ret = qca1530_xlna_init(pdev);
		if (ret) {
			pr_err("failed to initialize xLNA: %d", ret);
			on_req = 0;
			goto switching_off;
		} else {
			qca1530_data.chip_state |= QCA1530_XLNA_FLG;
		}
	}

	if (!(qca1530_data.chip_state & QCA1530_POWER_FLG) &&
		(on_req & QCA1530_POWER_FLG)) {
		ret = qca1530_pwr_init(pdev);
		if (ret < 0) {
			pr_err("failed to init power: %d", ret);
			on_req = 0;
			goto switching_off;
		} else {
			qca1530_data.chip_state |= QCA1530_POWER_FLG;
		}
	}

	pr_debug("Chip on section over, qca1530_data.chip_state=%d",
		qca1530_data.chip_state);

switching_off:
	pr_debug("Switching off");

	if ((qca1530_data.chip_state & QCA1530_POWER_FLG) &&
		!(on_req & QCA1530_POWER_FLG)) {
			qca1530_pwr_deinit(pdev);
			qca1530_data.chip_state &= ~QCA1530_POWER_FLG;
	}

	if ((qca1530_data.chip_state & QCA1530_XLNA_FLG) &&
		!(on_req & QCA1530_XLNA_FLG)) {
			qca1530_xlna_deinit(pdev);
			qca1530_data.chip_state &= ~QCA1530_XLNA_FLG;
	}

	if ((qca1530_data.chip_state & QCA1530_CLK_FLG) &&
		!(on_req & QCA1530_CLK_FLG)) {
			qca1530_clk_deinit(pdev);
			qca1530_data.chip_state &= ~QCA1530_CLK_FLG;
	}

	if ((qca1530_data.chip_state & QCA1530_RESET_FLG) &&
		!(on_req & QCA1530_RESET_FLG)) {
			qca1530_reset_deinit(pdev);
			qca1530_data.chip_state &= ~QCA1530_RESET_FLG;
	}

	pr_debug("Chip off section over, qca1530_data.chip_state=%d",
		qca1530_data.chip_state);

	return ret;
}

/**
 * qca1530_attr_chip_state_show() - provides current chip state through sysfs
 */
static ssize_t qca1530_attr_chip_state_show(struct kobject *kobj,
	struct kobj_attribute *attr,
	char *buf)
{
	return snprintf(buf, 16, "%d\n", qca1530_data.chip_state);
}

/**
 * qca1530_attr_chip_state_store() - sets new chip state
 */
static ssize_t qca1530_attr_chip_state_store(struct kobject *kobj,
	struct kobj_attribute *attr,
	const char *buf, size_t count)
{
	int retval;
	int new_state;

	sscanf(buf, "%d", &new_state);
	pr_debug("new_state=%d", new_state);
	if (count &&
		((new_state <= QCA1530_ALL_FLG) && (new_state >= 0))) {
		pr_debug("qca1530_data.chip_state=%d", qca1530_data.chip_state);
		retval = qca1530_set_chip_state(qca1530_data.pdev, new_state);
		pr_debug("qca1530_set_chip_state() returned %d", retval);
		pr_debug("qca1530_data.chip_state=%d", qca1530_data.chip_state);
	}

	return count;
}

/**
 * qca1530_attr_reset_show() - provides current reset state through sysfs
 */
static ssize_t qca1530_attr_reset_show(struct kobject *kobj,
	struct kobj_attribute *attr,
	char *buf)
{
	int v = -1;

	if (qca1530_data.chip_state & QCA1530_RESET_FLG)
		v = gpio_get_value(qca1530_data.reset_gpio);

	return snprintf(buf, 16, "%d\n", v);
}

/**
 * qca1530_attr_reset_store() - sets new reset state
 */
static ssize_t qca1530_attr_reset_store(struct kobject *kobj,
	struct kobj_attribute *attr,
	const char *buf, size_t count)
{
	int v;

	if (sscanf(buf, "%d", &v) == 1
		&& (qca1530_data.chip_state & QCA1530_RESET_FLG))
		gpio_set_value(qca1530_data.reset_gpio, v ? 1 : 0);

	return count;
}

/*
 * qca1530_attributes - sysfs attribute definition array
 */
static struct kobj_attribute qca1530_attributes[] = {
	__ATTR(chip_state,
		S_IRUSR | S_IWUSR,
		qca1530_attr_chip_state_show, qca1530_attr_chip_state_store),
	__ATTR(reset,
		S_IRUSR | S_IWUSR,
		qca1530_attr_reset_show, qca1530_attr_reset_store),
};

/*
 * qca1530_attrs - sysfs attributes for attribute group
 */
static struct attribute *qca1530_attrs[] = {
	&qca1530_attributes[0].attr,
	&qca1530_attributes[1].attr,
	NULL,
};

/*
 * qca1530_attr_group - driver sysfs attribute group
 */
static struct attribute_group qca1530_attr_group = {
	.attrs = qca1530_attrs,
};

static struct kobject *qca1530_kobject;

/**
 * qca1530_create_sysfs_node() - creates sysfs nodes for control
 *
 * Function exports two control nodes: for controlling chip control signals
 * and for reset control.
 */
static int qca1530_create_sysfs_node(void)
{
	int retval;

	pr_debug("Creating sysfs node");

	qca1530_kobject = kobject_create_and_add(SYSFS_NODE_NAME, kernel_kobj);
	pr_debug("qca1530_kobject=%p", qca1530_kobject);
	if (!qca1530_kobject)
		return -ENOMEM;

	retval = sysfs_create_group(qca1530_kobject, &qca1530_attr_group);
	pr_debug("sysfs_create_group() returned %d", retval);
	if (retval)
		kobject_put(qca1530_kobject);

	return retval;
}

/**
 * qca1530_remove_sysfs_node() - removes sysfs nodes
 */
static void qca1530_remove_sysfs_node(void)
{
	pr_debug("Removing sysfs node");
	if (qca1530_kobject) {
		sysfs_remove_group(qca1530_kobject, &qca1530_attr_group);
		kobject_put(qca1530_kobject);
		qca1530_kobject = NULL;
	}
}

/**
 * qca1530_probe() - performs driver initialization
 * @pdev: platform device data
 *
 * Function tried to turn on all required signals.
 * Refer to qca1530_set_chip_state() documentation for details.
 */
static int qca1530_probe(struct platform_device *pdev)
{
	int retval;

	pr_debug("Probing to install");

	retval = qca1530_set_chip_state(pdev, QCA1530_ALL_FLG);
	if (!retval) {
		qca1530_data.pdev = pdev;
		retval = qca1530_create_sysfs_node();
		if (retval) {
			qca1530_set_chip_state(pdev, 0);
			qca1530_data.pdev = NULL;
			pr_debug("qca1530_create_sysfs_node() returned %d",
			retval);
		}
	} else
		pr_debug("qca1530_set_chip_state() returned %d", retval);

	return retval;
}

/**
 * qca1530_remove() - releases all resources
 * @pdev: platform device data
 *
 * Driver's removal stops subsystems in the following order:
 *   - Power control
 *   - xLNA control
 *   - RTC and TCXO clock control
 *   - Reset control
 */
static int qca1530_remove(struct platform_device *pdev)
{
	int retval;
	qca1530_remove_sysfs_node();
	retval = qca1530_set_chip_state(pdev, 0);
	qca1530_data.pdev = NULL;
	return retval;
}

/*
 * qca1530_of_match - DTS driver name
 */
static struct of_device_id qca1530_of_match[] = {
	{.compatible = "qca,qca1530", },
	{ },
};

/*
 * qca1530_driver - driver registration data
 */
static struct platform_driver qca1530_driver = {
	.probe		= qca1530_probe,
	.remove         = qca1530_remove,
	.driver         = {
		.name = "qca1530",
		.owner = THIS_MODULE,
		.of_match_table = of_match_ptr(qca1530_of_match),
	},
};

/**
 * qca1530_init() - registers the driver
 */
static int __init qca1530_init(void)
{
	return platform_driver_register(&qca1530_driver);
}

/**
 * qca1530_exit() - unregisters the driver
 */
static void __exit qca1530_exit(void)
{
	platform_driver_unregister(&qca1530_driver);
}

module_init(qca1530_init);
module_exit(qca1530_exit);

MODULE_LICENSE("GPL v2");
MODULE_DESCRIPTION("qca1530 SoC chip driver");
MODULE_ALIAS("qca1530");