Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
CTCaer
GitHub Repository: CTCaer/hekate
Path: blob/master/bdk/rtc/max77620-rtc.h
1476 views
1
/*
2
* PMIC Real Time Clock driver for Nintendo Switch's MAX77620-RTC
3
*
4
* Copyright (c) 2018-2022 CTCaer
5
*
6
* This program is free software; you can redistribute it and/or modify it
7
* under the terms and conditions of the GNU General Public License,
8
* version 2, as published by the Free Software Foundation.
9
*
10
* This program is distributed in the hope it will be useful, but WITHOUT
11
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13
* more details.
14
*
15
* You should have received a copy of the GNU General Public License
16
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17
*/
18
19
#ifndef _MFD_MAX77620_RTC_H_
20
#define _MFD_MAX77620_RTC_H_
21
22
#include <utils/types.h>
23
24
#define MAX77620_RTC_I2C_ADDR 0x68
25
26
#define MAX77620_RTC_NR_TIME_REGS 7
27
28
#define MAX77620_RTC_RTCINT_REG 0x00
29
#define MAX77620_RTC_RTCINTM_REG 0x01
30
#define MAX77620_RTC_CONTROLM_REG 0x02
31
#define MAX77620_RTC_CONTROL_REG 0x03
32
#define MAX77620_RTC_BIN_FORMAT BIT(0)
33
#define MAX77620_RTC_24H BIT(1)
34
35
#define MAX77620_RTC_UPDATE0_REG 0x04
36
#define MAX77620_RTC_WRITE_UPDATE BIT(0)
37
#define MAX77620_RTC_READ_UPDATE BIT(4)
38
39
#define MAX77620_RTC_UPDATE1_REG 0x05
40
#define MAX77620_RTC_RTCSMPL_REG 0x06
41
42
#define MAX77620_RTC_SEC_REG 0x07
43
#define MAX77620_RTC_MIN_REG 0x08
44
#define MAX77620_RTC_HOUR_REG 0x09
45
#define MAX77620_RTC_HOUR_PM_MASK BIT(6)
46
#define MAX77620_RTC_WEEKDAY_REG 0x0A
47
#define MAX77620_RTC_MONTH_REG 0x0B
48
#define MAX77620_RTC_YEAR_REG 0x0C
49
#define MAX77620_RTC_DATE_REG 0x0D
50
51
#define MAX77620_ALARM1_SEC_REG 0x0E
52
#define MAX77620_ALARM1_MIN_REG 0x0F
53
#define MAX77620_ALARM1_HOUR_REG 0x10
54
#define MAX77620_ALARM1_WEEKDAY_REG 0x11
55
#define MAX77620_ALARM1_MONTH_REG 0x12
56
#define MAX77620_ALARM1_YEAR_REG 0x13
57
#define MAX77620_ALARM1_DATE_REG 0x14
58
#define MAX77620_ALARM2_SEC_REG 0x15
59
#define MAX77620_ALARM2_MIN_REG 0x16
60
#define MAX77620_ALARM2_HOUR_REG 0x17
61
#define MAX77620_ALARM2_WEEKDAY_REG 0x18
62
#define MAX77620_ALARM2_MONTH_REG 0x19
63
#define MAX77620_ALARM2_YEAR_REG 0x1A
64
#define MAX77620_ALARM2_DATE_REG 0x1B
65
#define MAX77620_RTC_ALARM_EN_MASK BIT(7)
66
67
typedef struct _rtc_time_t {
68
u8 weekday;
69
u8 sec;
70
u8 min;
71
u8 hour;
72
u8 day;
73
u8 month;
74
u16 year;
75
} rtc_time_t;
76
77
#define RTC_REBOOT_REASON_MAGIC 0x77 // 7-bit reg.
78
79
enum {
80
REBOOT_REASON_NOP = 0, // Use [config].
81
REBOOT_REASON_SELF = 1, // Use autoboot_idx/autoboot_list.
82
REBOOT_REASON_MENU = 2, // Force menu.
83
REBOOT_REASON_UMS = 3, // Force selected UMS partition.
84
REBOOT_REASON_REC = 4, // Set PMC_SCRATCH0_MODE_RECOVERY and reboot to self.
85
REBOOT_REASON_PANIC = 5 // Inform bootloader that panic occured if T210B01.
86
};
87
88
typedef struct _rtc_rr_decoded_t
89
{
90
u16 reason:4;
91
u16 autoboot_idx:4;
92
u16 autoboot_list:1;
93
u16 ums_idx:3;
94
} rtc_rr_decoded_t;
95
96
typedef struct _rtc_rr_encoded_t
97
{
98
u16 val1:6; // 6-bit reg.
99
u16 val2:6; // 6-bit reg.
100
} rtc_rr_encoded_t;
101
102
typedef struct _rtc_reboot_reason_t
103
{
104
union {
105
rtc_rr_decoded_t dec;
106
rtc_rr_encoded_t enc;
107
};
108
} rtc_reboot_reason_t;
109
110
void max77620_rtc_prep_read();
111
void max77620_rtc_get_time(rtc_time_t *time);
112
void max77620_rtc_get_time_adjusted(rtc_time_t *time);
113
void max77620_rtc_set_epoch_offset(int offset);
114
void max77620_rtc_stop_alarm();
115
void max77620_rtc_epoch_to_date(u32 epoch, rtc_time_t *time);
116
u32 max77620_rtc_date_to_epoch(const rtc_time_t *time);
117
void max77620_rtc_set_reboot_reason(rtc_reboot_reason_t *rr);
118
bool max77620_rtc_get_reboot_reason(rtc_reboot_reason_t *rr);
119
120
#endif /* _MFD_MAX77620_RTC_H_ */
121
122