aboutsummaryrefslogtreecommitdiff
path: root/drivers/rtc/rtc-pcf50606.c
blob: 5004987100d689de4637c62de3facc61b370d2f2 (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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/* NXP PCF50606 RTC Driver
 *
 * (C) 2006-2008 by Openmoko, Inc.
 * Author: Balaji Rao <balajirrao@openmoko.org>
 * All rights reserved.
 *
 * Broken down from monstrous PCF50606 driver mainly by
 * Harald Welte, Andy Green and Werner Almesberger
 *
 *  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.
 *
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/platform_device.h>
#include <linux/rtc.h>
#include <linux/bcd.h>
#include <linux/err.h>

#include <linux/mfd/pcf50606/core.h>

#define PCF50606_REG_RTCSC	0x0a /* Second */
#define PCF50606_REG_RTCMN	0x0b /* Minute */
#define PCF50606_REG_RTCHR	0x0c /* Hour */
#define PCF50606_REG_RTCWD	0x0d /* Weekday */
#define PCF50606_REG_RTCDT	0x0e /* Day */
#define PCF50606_REG_RTCMT	0x0f /* Month */
#define PCF50606_REG_RTCYR	0x10 /* Year */
#define PCF50606_REG_RTCSCA	0x11 /* Alarm Second */
#define PCF50606_REG_RTCMNA	0x12 /* Alarm Minute */
#define PCF50606_REG_RTCHRA	0x13 /* Alarm Hour */
#define PCF50606_REG_RTCWDA	0x14 /* Alarm Weekday */
#define PCF50606_REG_RTCDTA	0x15 /* Alarm Day */
#define PCF50606_REG_RTCMTA	0x16 /* Alarm Month */
#define PCF50606_REG_RTCYRA	0x17 /* Alarm Year */

enum pcf50606_time_indexes {
	PCF50606_TI_SEC,
	PCF50606_TI_MIN,
	PCF50606_TI_HOUR,
	PCF50606_TI_WKDAY,
	PCF50606_TI_DAY,
	PCF50606_TI_MONTH,
	PCF50606_TI_YEAR,
	PCF50606_TI_EXTENT /* always last */
};

struct pcf50606_time {
	uint8_t time[PCF50606_TI_EXTENT];
};

struct pcf50606_rtc {
	int alarm_enabled;
	int second_enabled;
	int alarm_pending;

	struct pcf50606 *pcf;
	struct rtc_device *rtc_dev;
};

static void pcf2rtc_time(struct rtc_time *rtc, struct pcf50606_time *pcf)
{
	rtc->tm_sec = bcd2bin(pcf->time[PCF50606_TI_SEC]);
	rtc->tm_min = bcd2bin(pcf->time[PCF50606_TI_MIN]);
	rtc->tm_hour = bcd2bin(pcf->time[PCF50606_TI_HOUR]);
	rtc->tm_wday = bcd2bin(pcf->time[PCF50606_TI_WKDAY]);
	rtc->tm_mday = bcd2bin(pcf->time[PCF50606_TI_DAY]);
	rtc->tm_mon = bcd2bin(pcf->time[PCF50606_TI_MONTH]) - 1;
	rtc->tm_year = bcd2bin(pcf->time[PCF50606_TI_YEAR]) + 100;
}

static void rtc2pcf_time(struct pcf50606_time *pcf, struct rtc_time *rtc)
{
	pcf->time[PCF50606_TI_SEC] = bin2bcd(rtc->tm_sec);
	pcf->time[PCF50606_TI_MIN] = bin2bcd(rtc->tm_min);
	pcf->time[PCF50606_TI_HOUR] = bin2bcd(rtc->tm_hour);
	pcf->time[PCF50606_TI_WKDAY] = bin2bcd(rtc->tm_wday);
	pcf->time[PCF50606_TI_DAY] = bin2bcd(rtc->tm_mday);
	pcf->time[PCF50606_TI_MONTH] = bin2bcd(rtc->tm_mon + 1);
	pcf->time[PCF50606_TI_YEAR] = bin2bcd(rtc->tm_year % 100);
}

static int
pcf50606_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
{
	struct pcf50606_rtc *rtc = dev_get_drvdata(dev);

	switch (cmd) {
	case RTC_AIE_OFF:
		rtc->alarm_enabled = 0;
		pcf50606_irq_mask(rtc->pcf, PCF50606_IRQ_ALARM);
		return 0;
	case RTC_AIE_ON:
		rtc->alarm_enabled = 1;
		pcf50606_irq_unmask(rtc->pcf, PCF50606_IRQ_ALARM);
		return 0;
	case RTC_UIE_OFF:
		rtc->second_enabled = 0;
		pcf50606_irq_mask(rtc->pcf, PCF50606_IRQ_SECOND);
		return 0;
	case RTC_UIE_ON:
		rtc->second_enabled = 1;
		pcf50606_irq_unmask(rtc->pcf, PCF50606_IRQ_SECOND);
		return 0;
	}

	return -ENOIOCTLCMD;
}

static int pcf50606_rtc_read_time(struct device *dev, struct rtc_time *tm)
{
	struct pcf50606_rtc *rtc;
	struct pcf50606_time pcf_tm;
	int ret;

	rtc = dev_get_drvdata(dev);

	ret = pcf50606_read_block(rtc->pcf, PCF50606_REG_RTCSC,
					    PCF50606_TI_EXTENT,
					    &pcf_tm.time[0]);
	if (ret != PCF50606_TI_EXTENT) {
		dev_err(dev, "Failed to read time\n");
		return -EIO;
	}

	dev_dbg(dev, "PCF_TIME: %02x.%02x.%02x %02x:%02x:%02x\n",
		pcf_tm.time[PCF50606_TI_DAY],
		pcf_tm.time[PCF50606_TI_MONTH],
		pcf_tm.time[PCF50606_TI_YEAR],
		pcf_tm.time[PCF50606_TI_HOUR],
		pcf_tm.time[PCF50606_TI_MIN],
		pcf_tm.time[PCF50606_TI_SEC]);

	pcf2rtc_time(tm, &pcf_tm);

	dev_dbg(dev, "RTC_TIME: %u.%u.%u %u:%u:%u\n",
		tm->tm_mday, tm->tm_mon, tm->tm_year,
		tm->tm_hour, tm->tm_min, tm->tm_sec);

	return rtc_valid_tm(tm);
}

static int pcf50606_rtc_set_time(struct device *dev, struct rtc_time *tm)
{
	struct pcf50606_rtc *rtc;
	struct pcf50606_time pcf_tm;
	int second_masked, alarm_masked, ret = 0;

	rtc = dev_get_drvdata(dev);

	dev_dbg(dev, "RTC_TIME: %u.%u.%u %u:%u:%u\n",
		tm->tm_mday, tm->tm_mon, tm->tm_year,
		tm->tm_hour, tm->tm_min, tm->tm_sec);

	rtc2pcf_time(&pcf_tm, tm);

	dev_dbg(dev, "PCF_TIME: %02x.%02x.%02x %02x:%02x:%02x\n",
		pcf_tm.time[PCF50606_TI_DAY],
		pcf_tm.time[PCF50606_TI_MONTH],
		pcf_tm.time[PCF50606_TI_YEAR],
		pcf_tm.time[PCF50606_TI_HOUR],
		pcf_tm.time[PCF50606_TI_MIN],
		pcf_tm.time[PCF50606_TI_SEC]);


	second_masked = pcf50606_irq_mask_get(rtc->pcf, PCF50606_IRQ_SECOND);
	alarm_masked = pcf50606_irq_mask_get(rtc->pcf, PCF50606_IRQ_ALARM);

	if (!second_masked)
		pcf50606_irq_mask(rtc->pcf, PCF50606_IRQ_SECOND);
	if (!alarm_masked)
		pcf50606_irq_mask(rtc->pcf, PCF50606_IRQ_ALARM);

	/* Returns 0 on success */
	ret = pcf50606_write_block(rtc->pcf, PCF50606_REG_RTCSC,
					     PCF50606_TI_EXTENT,
					     &pcf_tm.time[0]);

	if (!second_masked)
		pcf50606_irq_unmask(rtc->pcf, PCF50606_IRQ_SECOND);
	if (!alarm_masked)
		pcf50606_irq_unmask(rtc->pcf, PCF50606_IRQ_ALARM);

	return ret;
}

static int pcf50606_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
{
	struct pcf50606_rtc *rtc;
	struct pcf50606_time pcf_tm;
	int ret = 0;

	rtc = dev_get_drvdata(dev);

	alrm->enabled = rtc->alarm_enabled;
	alrm->pending = rtc->alarm_pending;

	ret = pcf50606_read_block(rtc->pcf, PCF50606_REG_RTCSCA,
				PCF50606_TI_EXTENT, &pcf_tm.time[0]);
	if (ret != PCF50606_TI_EXTENT) {
		dev_err(dev, "Failed to read time\n");
		return -EIO;
	}

	pcf2rtc_time(&alrm->time, &pcf_tm);

	return rtc_valid_tm(&alrm->time);
}

static int pcf50606_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
{
	struct pcf50606_rtc *rtc;
	struct pcf50606_time pcf_tm;
	int alarm_masked, ret = 0;

	rtc = dev_get_drvdata(dev);

	rtc2pcf_time(&pcf_tm, &alrm->time);

	/* do like mktime does and ignore tm_wday */
	pcf_tm.time[PCF50606_TI_WKDAY] = 7;

	alarm_masked = pcf50606_irq_mask_get(rtc->pcf, PCF50606_IRQ_ALARM);

	/* disable alarm interrupt */
	if (!alarm_masked)
		pcf50606_irq_mask(rtc->pcf, PCF50606_IRQ_ALARM);

	/* Returns 0 on success */
	ret = pcf50606_write_block(rtc->pcf, PCF50606_REG_RTCSCA,
				PCF50606_TI_EXTENT, &pcf_tm.time[0]);

	if (!alrm->enabled)
		rtc->alarm_pending = 0;

	if (!alarm_masked || alrm->enabled)
		pcf50606_irq_unmask(rtc->pcf, PCF50606_IRQ_ALARM);
	rtc->alarm_enabled = alrm->enabled;

	return ret;
}

static struct rtc_class_ops pcf50606_rtc_ops = {
	.ioctl		= pcf50606_rtc_ioctl,
	.read_time	= pcf50606_rtc_read_time,
	.set_time	= pcf50606_rtc_set_time,
	.read_alarm	= pcf50606_rtc_read_alarm,
	.set_alarm	= pcf50606_rtc_set_alarm,
};

static void pcf50606_rtc_irq(int irq, void *data)
{
	struct pcf50606_rtc *rtc = data;

	switch (irq) {
	case PCF50606_IRQ_ALARM:
		rtc_update_irq(rtc->rtc_dev, 1, RTC_AF | RTC_IRQF);
		rtc->alarm_pending = 1;
		break;
	case PCF50606_IRQ_SECOND:
		rtc_update_irq(rtc->rtc_dev, 1, RTC_UF | RTC_IRQF);
		break;
	}
}

static int __devinit pcf50606_rtc_probe(struct platform_device *pdev)
{
	struct pcf50606_rtc *rtc;

	rtc = kzalloc(sizeof(*rtc), GFP_KERNEL);
	if (!rtc)
		return -ENOMEM;

	rtc->pcf = dev_to_pcf50606(pdev->dev.parent);
	platform_set_drvdata(pdev, rtc);
	rtc->rtc_dev = rtc_device_register("pcf50606-rtc", &pdev->dev,
				&pcf50606_rtc_ops, THIS_MODULE);

	if (IS_ERR(rtc->rtc_dev)) {
		kfree(rtc);
		return PTR_ERR(rtc->rtc_dev);
	}

	pcf50606_register_irq(rtc->pcf, PCF50606_IRQ_ALARM,
					pcf50606_rtc_irq, rtc);
	pcf50606_register_irq(rtc->pcf, PCF50606_IRQ_SECOND,
					pcf50606_rtc_irq, rtc);

	return 0;
}


static int __devexit pcf50606_rtc_remove(struct platform_device *pdev)
{
	struct pcf50606_rtc *rtc;

	rtc = platform_get_drvdata(pdev);

	rtc_device_unregister(rtc->rtc_dev);

	pcf50606_free_irq(rtc->pcf, PCF50606_IRQ_ALARM);
	pcf50606_free_irq(rtc->pcf, PCF50606_IRQ_SECOND);

	platform_set_drvdata(pdev, NULL);
	kfree(rtc);

	return 0;
}


static struct platform_driver pcf50606_rtc_driver = {
	.driver = {
		.name = "pcf50606-rtc",
		.owner = THIS_MODULE,
	},
	.probe = pcf50606_rtc_probe,
	.remove = __devexit_p(pcf50606_rtc_remove),
};

static int __init pcf50606_rtc_init(void)
{
	return platform_driver_register(&pcf50606_rtc_driver);
}
module_init(pcf50606_rtc_init);

static void __exit pcf50606_rtc_exit(void)
{
	platform_driver_unregister(&pcf50606_rtc_driver);
}
module_exit(pcf50606_rtc_exit);

MODULE_DESCRIPTION("PCF50606 RTC driver");
MODULE_AUTHOR("Balaji Rao <balajirrao@openmoko.org>");
MODULE_LICENSE("GPL");