summaryrefslogtreecommitdiff
path: root/drivers/rtc/rtc-tegra-odm.c
blob: 2a90d60b06b8bdb3f1545d6c0a8148ce51bf6878 (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
/*
 * drivers/rtc/rtc-tegra-odm.c
 *
 * Tegra ODM kit wrapper for RTC functionality implemented in Tegra
 * ODM PMU adaptation
 *
 * 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 <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/ioport.h>
#include <linux/delay.h>
#include <linux/rtc.h>
#include <linux/bcd.h>
#include <linux/platform_device.h>
#include "nvodm_pmu.h"

#define PMU_IOCTL_ENABLE  1

/* Create a custom rtc structrue and move this to that structure */
static NvOdmPmuDeviceHandle hPmu = NULL;

static int tegra_rtc_read_time(struct device *dev, struct rtc_time *tm)
{
	NvU32 now;

	if (hPmu == NULL)
		return -1;

	if (!NvOdmPmuReadRtc(hPmu, &now)) {
		printk("NvOdmPmuReadRtc failed\n");
		return -1;
	}

	rtc_time_to_tm(now, tm);
	return 0;
}

static int tegra_rtc_set_time(struct device *dev, struct rtc_time *tm)
{
	unsigned long now;
	int ret;

	if (hPmu == NULL)
		return -1;

	ret = rtc_tm_to_time(tm, &now);
	if (ret != 0)
		return -1;

	if (!NvOdmPmuWriteRtc(hPmu, (NvU32)now)) {
		printk("NvOdmPmuWriteRtc failed\n");
		return -1;
	}
	return 0;
}

#if (PMU_IOCTL_ENABLE)
static int tegra_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *wkalrm)
{

	struct rtc_time *time = &wkalrm->time;

	NvU32 alarm_sec = 0;
	if(!NvOdmPmuReadAlarm(hPmu, &alarm_sec))
		return -EINVAL;

	rtc_time_to_tm(alarm_sec, time);

	return 0;
}

static int tegra_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *wkalrm)
{
	struct rtc_time *time = &wkalrm->time;
	NvU32 now;
	NvU32 alarm_sec;
	struct rtc_time now_time;

	pr_debug("wkalrm->enabled = %d\n", wkalrm->enabled);
	if (wkalrm->enabled == 0)
		return 0;

	if (!NvOdmPmuReadRtc(hPmu, &now)) {
		pr_debug("NvOdmPmuReadRtc failed\n");
		return -1;
	}

	rtc_time_to_tm(now, &now_time);
	pr_debug( "read  now_time %02d:%02d:%02d %02d/%02d/%04d\n",
		now_time.tm_hour, now_time.tm_min, now_time.tm_sec,
		now_time.tm_mon + 1, now_time.tm_mday, now_time.tm_year + 1900);

	pr_debug("write alarm_time %02d:%02d:%02d %02d/%02d/%04d\n",
		time->tm_hour, time->tm_min, time->tm_sec,
		time->tm_mon+1, time->tm_mday, time->tm_yday+1900);

	alarm_sec = (NvU32)mktime(now_time.tm_year + 1900, time->tm_mon+1, time->tm_mday,
				time->tm_hour, time->tm_min, time->tm_sec);
	if (alarm_sec < now)
		alarm_sec = (NvU32)mktime(now_time.tm_year + 1901, time->tm_mon+1, time->tm_mday,
				time->tm_hour, time->tm_min, time->tm_sec);

	pr_debug("alarm_sec = %u\n", alarm_sec);

	if(!NvOdmPmuWriteAlarm(hPmu, alarm_sec-now))
		return -EINVAL;

	return 0;
}
#endif

static struct rtc_class_ops tegra_rtc_ops = {
	.read_time	= tegra_rtc_read_time,
	.set_time	= tegra_rtc_set_time,
#if (PMU_IOCTL_ENABLE)
	.read_alarm	= tegra_rtc_read_alarm,
	.set_alarm	= tegra_rtc_set_alarm,
#endif
};

static int __init tegra_rtc_probe(struct platform_device *pdev)
{
	struct rtc_device *rtc;
	NvU32 initial;

	if (NvOdmPmuDeviceOpen(&hPmu) == NV_FALSE) {
		pr_debug("%s: NvOdmPmuDeviceOpen failed\n", pdev->name);
		return -ENXIO;
	}

	/* if the SoCs PMU has't been properly initialized, a bogus large
	 * value may be returned which triggers the Y2038 bug in the kernel.
	 * work-around this issue by checking the initial value of the PMU
	 * and then clobbering it if the value is bogus */

	if (NvOdmPmuReadRtc(hPmu, &initial) && ((time_t)initial < 0))
	{
		if(!NvOdmPmuWriteRtc(hPmu, 0))
			return -EINVAL;
	}


	rtc = rtc_device_register(pdev->name, &pdev->dev,
		&tegra_rtc_ops, THIS_MODULE);

	if (IS_ERR(rtc)) {
		pr_debug("%s: can't register RTC device, err %ld\n",
			pdev->name, PTR_ERR(rtc));
		NvOdmPmuDeviceClose(hPmu);
		return -1;
	}
	platform_set_drvdata(pdev, rtc);

	return 0;
}

static int __exit tegra_rtc_remove(struct platform_device *pdev)
{
	struct rtc_device *rtc = platform_get_drvdata(pdev);;

	rtc_device_unregister(rtc);
	return 0;
}

#ifdef CONFIG_PM

static int tegra_rtc_suspend(struct platform_device *pdev, pm_message_t state)
{
	return 0;
}

static int tegra_rtc_resume(struct platform_device *pdev)
{
	return 0;
}

#endif

static void tegra_rtc_shutdown(struct platform_device *pdev)
{
}

MODULE_ALIAS("platform:tegra_rtc_odm");

static struct platform_driver tegra_rtc_driver = {
	.remove		= __exit_p(tegra_rtc_remove),
	.shutdown	= tegra_rtc_shutdown,
#ifdef CONFIG_PM
	.suspend	= tegra_rtc_suspend,
	.resume		= tegra_rtc_resume,
#endif
	.driver		=  {
		.name  = "tegra_rtc_odm",
		.owner = THIS_MODULE,
	},
};

static int __init rtc_init(void)
{
	return platform_driver_probe(&tegra_rtc_driver, tegra_rtc_probe);
}
module_init(rtc_init);

static void __exit rtc_exit(void)
{
	platform_driver_unregister(&tegra_rtc_driver);
}
module_exit(rtc_exit);