rtc_mt6359p.c 2.86 KB
Newer Older
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
/*
 * Copyright (c) 2020, MediaTek Inc. All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <common/debug.h>
#include <drivers/delay_timer.h>
#include <rtc.h>


static void RTC_Config_Interface(uint32_t addr, uint16_t data,
			    uint16_t mask, uint16_t shift)
{
	uint16_t pmic_reg;

	pmic_reg = RTC_Read(addr);

	pmic_reg &= ~(mask << shift);
	pmic_reg |= (data << shift);

	RTC_Write(addr, pmic_reg);
}

static int32_t rtc_disable_2sec_reboot(void)
{
	uint16_t reboot;

	reboot = (RTC_Read(RTC_AL_SEC) & ~RTC_BBPU_2SEC_EN) &
		 ~RTC_BBPU_AUTO_PDN_SEL;
	RTC_Write(RTC_AL_SEC, reboot);

	return RTC_Write_Trigger();
}

static int32_t rtc_enable_k_eosc(void)
{
	uint16_t alm_dow, alm_sec;
	int16_t ret;

	/* Turning on eosc cali mode clock */
	RTC_Config_Interface(PMIC_RG_SCK_TOP_CKPDN_CON0_CLR, 1,
			PMIC_RG_RTC_EOSC32_CK_PDN_MASK,
			PMIC_RG_RTC_EOSC32_CK_PDN_SHIFT);

	alm_sec = RTC_Read(RTC_AL_SEC) & (~RTC_LPD_OPT_MASK);
	RTC_Write(RTC_AL_SEC, alm_sec);
	ret = RTC_Write_Trigger();
	if (ret == 0) {
		return 0;
	}

	RTC_Write(RTC_CON, RTC_LPD_EN);
	ret = RTC_Write_Trigger();
	if (ret == 0) {
		return 0;
	}

	RTC_Write(RTC_CON, RTC_LPD_RST);
	ret = RTC_Write_Trigger();
	if (ret == 0) {
		return 0;
	}

	RTC_Write(RTC_CON, RTC_LPD_EN);
	ret = RTC_Write_Trigger();
	if (ret == 0) {
		return 0;
	}

	RTC_Write(RTC_POWERKEY1, RTC_POWERKEY1_KEY);
	RTC_Write(RTC_POWERKEY2, RTC_POWERKEY2_KEY);
	ret = RTC_Write_Trigger();
	if (ret == 0) {
		return 0;
	}

	/* set RTC EOSC calibration period = 8sec */
	alm_dow = (RTC_Read(RTC_AL_DOW) & (~RTC_RG_EOSC_CALI_TD_MASK)) |
		  RTC_RG_EOSC_CALI_TD_8SEC;
	RTC_Write(RTC_AL_DOW, alm_dow);
	ret = RTC_Write_Trigger();
	if (ret == 0) {
		return 0;
	}

	RTC_Write(RTC_BBPU,
		  RTC_Read(RTC_BBPU) | RTC_BBPU_KEY | RTC_BBPU_RELOAD);
	ret = RTC_Write_Trigger();
	if (ret == 0) {
		return 0;
	}

	/* Enable K EOSC mode :use solution1 of eosc cali to fix mt6359p 32K*/
	RTC_Write(RTC_AL_YEA, (((RTC_Read(RTC_AL_YEA) | RTC_K_EOSC_RSV_0)
				& (~RTC_K_EOSC_RSV_1)) | (RTC_K_EOSC_RSV_2)));
	ret = RTC_Write_Trigger();
	if (ret == 0) {
		return 0;
	}

	INFO("[RTC] RTC_enable_k_eosc\n");

	return 1;
}

void rtc_power_off_sequence(void)
{
	uint16_t bbpu;
	int16_t ret;

	ret = rtc_disable_2sec_reboot();
	if (ret == 0) {
		return;
	}

	ret = rtc_enable_k_eosc();
	if (ret == 0) {
		return;
	}

	bbpu = RTC_BBPU_KEY | RTC_BBPU_PWREN;

	if (Writeif_unlock() != 0) {
		RTC_Write(RTC_BBPU,
			  bbpu | RTC_BBPU_RESET_ALARM | RTC_BBPU_RESET_SPAR);
		RTC_Write(RTC_AL_MASK, RTC_AL_MASK_DOW);
		ret = RTC_Write_Trigger();
		if (ret == 0) {
			return;
		}
		mdelay(1);

		bbpu = RTC_Read(RTC_BBPU);

		if (((bbpu & RTC_BBPU_RESET_ALARM) > 0) ||
		    ((bbpu & RTC_BBPU_RESET_SPAR) > 0)) {
			INFO("[RTC] timeout\n");
		}

		bbpu = RTC_Read(RTC_BBPU) | RTC_BBPU_KEY | RTC_BBPU_RELOAD;
		RTC_Write(RTC_BBPU, bbpu);
		ret = RTC_Write_Trigger();
		if (ret == 0) {
			return;
		}
	}
}