summaryrefslogtreecommitdiff
path: root/arch/arm/mach-tegra/odm_kit/adaptations/pmu/pcf50626/pcf50626_rtc.c
blob: 26bd9d6291076d481d68bd7b6ea8229578cca08a (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
/*
 * Copyright (c) 2009 NVIDIA Corporation.
 *
 * 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.
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include "pcf50626_rtc.h"
#include "pcf50626_i2c.h"
#include "pcf50626_reg.h"

/* Read RTC count register */

static NvBool bRtcNotInitialized = NV_TRUE;

NvBool 
Pcf50626RtcCountRead(
    NvOdmPmuDeviceHandle hDevice, 
    NvU32* Count)
{
    if (Pcf50626RtcWasStartUpFromNoPower(hDevice) && bRtcNotInitialized)
    {
        if (!Pcf50626I2cWrite32 (hDevice, PCF50626_RTC1_ADDR, 0))
        {
            return NV_FALSE;
        }
        bRtcNotInitialized = NV_FALSE;
        *Count = 0;
        return NV_TRUE;
    } else
    {   
        return ( Pcf50626I2cRead32 (hDevice, PCF50626_RTC1_ADDR, Count) );
    }   
}

/* Write RTC count register */

NvBool 
Pcf50626RtcCountWrite(
    NvOdmPmuDeviceHandle hDevice, 
    NvU32 Count)
{
    NvBool ret;

    ret = Pcf50626I2cWrite32 (hDevice, PCF50626_RTC1_ADDR, Count);

    if (ret && bRtcNotInitialized)
        bRtcNotInitialized = NV_FALSE;
    
    return ret;
}

/* Read RTC alarm count register */

NvBool 
Pcf50626RtcAlarmCountRead(
    NvOdmPmuDeviceHandle hDevice, 
    NvU32* Count)
{
    return ( Pcf50626I2cRead32 (hDevice, PCF50626_RTC1A_ADDR, Count) );
}

/* Write RTC alarm count register */

NvBool 
Pcf50626RtcAlarmCountWrite(
    NvOdmPmuDeviceHandle hDevice, 
    NvU32 Count)
{
    return ( Pcf50626I2cWrite32 (hDevice, PCF50626_RTC1A_ADDR, Count) );
}

/* Reads RTC alarm interrupt mask status */

NvBool 
Pcf50626RtcIsAlarmIntEnabled(NvOdmPmuDeviceHandle hDevice)
{
    NvU8    Mask;

    if(Pcf50626I2cRead8 (hDevice, PCF50626_INT1M_ADDR, &Mask))
    {
        return ((Mask & 0x8)? NV_FALSE:NV_TRUE);
    }

    return NV_FALSE;
}

/* Enables / Disables the RTC alarm interrupt */

NvBool 
Pcf50626RtcAlarmIntEnable(
    NvOdmPmuDeviceHandle hDevice, 
    NvBool Enable)
{
    NvBool  Status = NV_FALSE;
    NvU8    Mask;

    if ((Status = Pcf50626I2cRead8(hDevice, PCF50626_INT1M_ADDR, &Mask)) == NV_TRUE)
    {
        (Mask = Enable? (Mask & ~0x8):(Mask|0x8));
        Status = Pcf50626I2cWrite8 (hDevice, PCF50626_INT1M_ADDR, Mask);
    }

    return Status;
}

/* Checks if boot was from nopower / powered state */

NvBool 
Pcf50626RtcWasStartUpFromNoPower(NvOdmPmuDeviceHandle hDevice)
{
    NvU8 Data;

    // Check "nopower" bit of the ERROR status register.
    // Make sure the backup battery charger is enabled (bbce and vsaveen bits). This is done by
    // the bootloader. If this is not done, the "nopower" bit will remain stuck at 0x1.
    if ((Pcf50626I2cRead8(hDevice, PCF50626_ERROR_ADDR, &Data)) == NV_TRUE)
    {
        return ((Data & 0x20)? NV_TRUE : NV_FALSE);
    }

    return NV_FALSE;
}

NvBool
Pcf50626IsRtcInitialized(NvOdmPmuDeviceHandle hDevice)
{
    return ((Pcf50626RtcWasStartUpFromNoPower(hDevice))? NV_FALSE : NV_TRUE);
}