aboutsummaryrefslogtreecommitdiff
path: root/drivers/rtc/rtc-msm7x00a.c
blob: 690bc398cd6c00aa6237fa00fb64b8dd1fef1330 (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
/* drivers/rtc/rtc-msm7x00a.c
 *
 * Copyright (C) 2008 Google, Inc.
 * Author: San Mehat <san@google.com>
 *
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 *
 * 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.
 *
 */

#include <linux/module.h>
#include <linux/version.h>

#include <linux/kernel.h>
#include <linux/platform_device.h>
#include <linux/init.h>
#include <linux/types.h>
#include <linux/rtc.h>
#include <linux/msm_rpcrouter.h>

#include <mach/msm_rpcrouter.h>

#define RTC_DEBUG 0

extern void msm_pm_set_max_sleep_time(int64_t sleep_time_ns);

#if CONFIG_MSM_AMSS_VERSION >= 6350 || defined(CONFIG_ARCH_QSD8X50)
#define APP_TIMEREMOTE_PDEV_NAME "rs30000048:00010000"
#else
#define APP_TIMEREMOTE_PDEV_NAME "rs30000048:0da5b528"
#endif

#define TIMEREMOTE_PROCEEDURE_SET_JULIAN	6
#define TIMEREMOTE_PROCEEDURE_GET_JULIAN	7

struct rpc_time_julian {
	uint32_t year;
	uint32_t month;
	uint32_t day;
	uint32_t hour;
	uint32_t minute;
	uint32_t second;
	uint32_t day_of_week;
};

static struct msm_rpc_endpoint *ep;
static struct rtc_device *rtc;
static unsigned long rtcalarm_time;

static int
msmrtc_timeremote_set_time(struct device *dev, struct rtc_time *tm)
{
	int rc;

	struct timeremote_set_julian_req {
		struct rpc_request_hdr hdr;
		uint32_t opt_arg;

		struct rpc_time_julian time;
	} req;

	struct timeremote_set_julian_rep {
		struct rpc_reply_hdr hdr;
	} rep;

	if (tm->tm_year < 1900)
		tm->tm_year += 1900;

	if (tm->tm_year < 1970)
		return -EINVAL;

#if RTC_DEBUG
	printk(KERN_DEBUG "%s: %.2u/%.2u/%.4u %.2u:%.2u:%.2u (%.2u)\n",
	       __func__, tm->tm_mon, tm->tm_mday, tm->tm_year,
	       tm->tm_hour, tm->tm_min, tm->tm_sec, tm->tm_wday);
#endif

	req.opt_arg = cpu_to_be32(1);
	req.time.year = cpu_to_be32(tm->tm_year);
	req.time.month = cpu_to_be32(tm->tm_mon + 1);
	req.time.day = cpu_to_be32(tm->tm_mday);
	req.time.hour = cpu_to_be32(tm->tm_hour);
	req.time.minute = cpu_to_be32(tm->tm_min);
	req.time.second = cpu_to_be32(tm->tm_sec);
	req.time.day_of_week = cpu_to_be32(tm->tm_wday);


	rc = msm_rpc_call_reply(ep, TIMEREMOTE_PROCEEDURE_SET_JULIAN,
				&req, sizeof(req),
				&rep, sizeof(rep),
				5 * HZ);
	return rc;
}

static int
msmrtc_timeremote_read_time(struct device *dev, struct rtc_time *tm)
{
	int rc;

	struct timeremote_get_julian_req {
		struct rpc_request_hdr hdr;
		uint32_t julian_time_not_null;
	} req;

	struct timeremote_get_julian_rep {
		struct rpc_reply_hdr hdr;
		uint32_t opt_arg;
		struct rpc_time_julian time;
	} rep;

	req.julian_time_not_null = cpu_to_be32(1);

	rc = msm_rpc_call_reply(ep, TIMEREMOTE_PROCEEDURE_GET_JULIAN,
				&req, sizeof(req),
				&rep, sizeof(rep),
				5 * HZ);
	if (rc < 0)
		return rc;

	if (!be32_to_cpu(rep.opt_arg)) {
		printk(KERN_ERR "%s: No data from RTC\n", __func__);
		return -ENODATA;
	}

	tm->tm_year = be32_to_cpu(rep.time.year);
	tm->tm_mon = be32_to_cpu(rep.time.month);
	tm->tm_mday = be32_to_cpu(rep.time.day);
	tm->tm_hour = be32_to_cpu(rep.time.hour);
	tm->tm_min = be32_to_cpu(rep.time.minute);
	tm->tm_sec = be32_to_cpu(rep.time.second);
	tm->tm_wday = be32_to_cpu(rep.time.day_of_week);

#if RTC_DEBUG
	printk(KERN_DEBUG "%s: %.2u/%.2u/%.4u %.2u:%.2u:%.2u (%.2u)\n",
	       __func__, tm->tm_mon, tm->tm_mday, tm->tm_year,
	       tm->tm_hour, tm->tm_min, tm->tm_sec, tm->tm_wday);
#endif

	tm->tm_year -= 1900;	/* RTC layer expects years to start at 1900 */
	tm->tm_mon--;		/* RTC layer expects mons to be 0 based */

	if (rtc_valid_tm(tm) < 0) {
		dev_err(dev, "retrieved date/time is not valid.\n");
		rtc_time_to_tm(0, tm);
	}

	return 0;
}


static int
msmrtc_virtual_alarm_set(struct device *dev, struct rtc_wkalrm *a)
{
	unsigned long now = get_seconds();

	if (!a->enabled) {
		rtcalarm_time = 0;
		return 0;
	} else
		rtc_tm_to_time(&a->time, &rtcalarm_time);

	if (now > rtcalarm_time) {
		printk(KERN_ERR "%s: Attempt to set alarm in the past\n",
		       __func__);
		rtcalarm_time = 0;
		return -EINVAL;
	}

	return 0;
}

static struct rtc_class_ops msm_rtc_ops = {
	.read_time	= msmrtc_timeremote_read_time,
	.set_time	= msmrtc_timeremote_set_time,
	.set_alarm	= msmrtc_virtual_alarm_set,
};

static void
msmrtc_alarmtimer_expired(unsigned long _data)
{
#if RTC_DEBUG
	printk(KERN_DEBUG "%s: Generating alarm event (src %lu)\n",
	       rtc->name, _data);
#endif
	rtc_update_irq(rtc, 1, RTC_IRQF | RTC_AF);
	rtcalarm_time = 0;
}

static int
msmrtc_probe(struct platform_device *pdev)
{
	struct rpcsvr_platform_device *rdev =
		container_of(pdev, struct rpcsvr_platform_device, base);

	ep = msm_rpc_connect(rdev->prog, rdev->vers, 0);
	if (IS_ERR(ep)) {
		printk(KERN_ERR "%s: init rpc failed! rc = %ld\n",
		       __func__, PTR_ERR(ep));
		return PTR_ERR(ep);
	}

	rtc = rtc_device_register("msm_rtc",
				  &pdev->dev,
				  &msm_rtc_ops,
				  THIS_MODULE);
	if (IS_ERR(rtc)) {
		printk(KERN_ERR "%s: Can't register RTC device (%ld)\n",
		       pdev->name, PTR_ERR(rtc));
		return PTR_ERR(rtc);
	}
	return 0;
}


static unsigned long msmrtc_get_seconds(void)
{
	struct rtc_time tm;
	unsigned long now;

	msmrtc_timeremote_read_time(NULL, &tm);
	rtc_tm_to_time(&tm, &now);
	return now;
}

static int
msmrtc_suspend(struct platform_device *dev, pm_message_t state)
{
	if (rtcalarm_time) {
		unsigned long now = msmrtc_get_seconds();
		int diff = rtcalarm_time - now;
		if (diff <= 0) {
			msmrtc_alarmtimer_expired(1);
			msm_pm_set_max_sleep_time(0);
			return 0;
		}
		msm_pm_set_max_sleep_time((int64_t) ((int64_t) diff * NSEC_PER_SEC));
	} else
		msm_pm_set_max_sleep_time(0);
	return 0;
}

static int
msmrtc_resume(struct platform_device *dev)
{
	if (rtcalarm_time) {
		unsigned long now = msmrtc_get_seconds();
		int diff = rtcalarm_time - now;
		if (diff <= 0)
			msmrtc_alarmtimer_expired(2);
	}
	return 0;
}

static struct platform_driver msmrtc_driver = {
	.probe		= msmrtc_probe,
	.suspend	= msmrtc_suspend,
	.resume		= msmrtc_resume,
	.driver	= {
		.name	= APP_TIMEREMOTE_PDEV_NAME,
		.owner	= THIS_MODULE,
	},
};

static int __init msmrtc_init(void)
{
	rtcalarm_time = 0;
	return platform_driver_register(&msmrtc_driver);
}

module_init(msmrtc_init);

MODULE_DESCRIPTION("RTC driver for Qualcomm MSM7x00a chipsets");
MODULE_AUTHOR("San Mehat <san@android.com>");
MODULE_LICENSE("GPL");