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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
|
/*
* Copyright (C) ST-Ericsson SA 2011
*
* License terms: GNU General Public License (GPL) version 2
* Author: Rabin Vincent <rabin.vincent@stericsson.com>
*/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/platform_device.h>
#include <linux/rtc.h>
#include <linux/interrupt.h>
#include <linux/delay.h>
#include <linux/mfd/abx500.h>
#include <linux/mfd/abx500/ab5500.h>
#define AB5500_RTC_CLOCK_RATE 32768
#define AB5500_RTC 0x00
#define AB5500_RTC_ALARM (1 << 1)
#define AB5500_READREQ 0x01
#define AB5500_READREQ_REQ 0x01
#define AB5500_AL0 0x02
#define AB5500_TI0 0x06
/**
* struct ab_rtc - variant specific data
* @irqname: optional name for the alarm interrupt resource
* @epoch: epoch to adjust year to
* @bank: AB bank where this block is present
* @rtc: address of the "RTC" (control) register
* @rtc_alarmon: mask of the alarm enable bit in the above register
* @ti0: address of the TI0 register. The rest of the TI
* registers are assumed to contiguously follow this one.
* @nr_ti: number of TI* registers
* @al0: address of the AL0 register. The rest of the
* AL registers are assumed to contiguously follow this one.
* @nr_al: number of AL* registers
* @startup: optional function to initialize the RTC
* @alarm_to_regs: function to convert alarm time in seconds
* to a list of AL register values
* @time_to_regs: function to convert alarm time in seconds
* to a list of TI register values
* @regs_to_alarm: function to convert a list of AL register
* values to the alarm time in seconds
* @regs_to_time: function to convert a list of TI register
* values to the alarm time in seconds
* @request_read: optional function to request a read from the TI* registers
* @request_write: optional function to request a write to the TI* registers
*/
struct ab_rtc {
const char *irqname;
unsigned int epoch;
u8 bank;
u8 rtc;
u8 rtc_alarmon;
u8 ti0;
int nr_ti;
u8 al0;
int nr_al;
int (*startup)(struct device *dev);
void (*alarm_to_regs)(struct device *dev, unsigned long secs, u8 *regs);
void (*time_to_regs)(struct device *dev, unsigned long secs, u8 *regs);
unsigned long (*regs_to_alarm)(struct device *dev, u8 *regs);
unsigned long (*regs_to_time)(struct device *dev, u8 *regs);
int (*request_read)(struct device *dev);
int (*request_write)(struct device *dev);
};
static const struct ab_rtc *to_ab_rtc(struct device *dev)
{
struct platform_device *pdev = to_platform_device(dev);
return (struct ab_rtc *)pdev->id_entry->driver_data;
}
/* Calculate the number of seconds since year, for epoch adjustment */
static unsigned long ab_rtc_get_elapsed_seconds(unsigned int year)
{
unsigned long secs;
struct rtc_time tm = {
.tm_year = year - 1900,
.tm_mday = 1,
};
rtc_tm_to_time(&tm, &secs);
return secs;
}
static int ab5500_rtc_request_read(struct device *dev)
{
const struct ab_rtc *variant = to_ab_rtc(dev);
unsigned long timeout;
int err;
err = abx500_set_register_interruptible(dev, variant->bank,
AB5500_READREQ,
AB5500_READREQ_REQ);
if (err < 0)
return err;
timeout = jiffies + HZ;
while (time_before(jiffies, timeout)) {
u8 value;
err = abx500_get_register_interruptible(dev, variant->bank,
AB5500_READREQ, &value);
if (err < 0)
return err;
if (!(value & AB5500_READREQ_REQ))
return 0;
msleep(1);
}
return -EIO;
}
static void
ab5500_rtc_time_to_regs(struct device *dev, unsigned long secs, u8 *regs)
{
unsigned long mins = secs / 60;
u64 fat_time;
secs %= 60;
fat_time = secs * AB5500_RTC_CLOCK_RATE;
fat_time |= (u64)mins << 21;
regs[0] = (fat_time) & 0xFF;
regs[1] = (fat_time >> 8) & 0xFF;
regs[2] = (fat_time >> 16) & 0xFF;
regs[3] = (fat_time >> 24) & 0xFF;
regs[4] = (fat_time >> 32) & 0xFF;
regs[5] = (fat_time >> 40) & 0xFF;
}
static unsigned long
ab5500_rtc_regs_to_time(struct device *dev, u8 *regs)
{
u64 fat_time = ((u64)regs[5] << 40) | ((u64)regs[4] << 32) |
((u64)regs[3] << 24) | ((u64)regs[2] << 16) |
((u64)regs[1] << 8) | regs[0];
unsigned long secs = (fat_time & 0x1fffff) / AB5500_RTC_CLOCK_RATE;
unsigned long mins = fat_time >> 21;
return mins * 60 + secs;
}
static void
ab5500_rtc_alarm_to_regs(struct device *dev, unsigned long secs, u8 *regs)
{
unsigned long mins = secs / 60;
#ifdef CONFIG_ANDROID
/*
* Needed because Android believes all hw have a wake-up resolution in
* seconds.
*/
mins++;
#endif
regs[0] = mins & 0xFF;
regs[1] = (mins >> 8) & 0xFF;
regs[2] = (mins >> 16) & 0xFF;
}
static unsigned long
ab5500_rtc_regs_to_alarm(struct device *dev, u8 *regs)
{
unsigned long mins = ((unsigned long)regs[2] << 16) |
((unsigned long)regs[1] << 8) |
regs[0];
unsigned long secs = mins * 60;
return secs;
}
static const struct ab_rtc ab5500_rtc = {
.irqname = "RTC_Alarm",
.bank = AB5500_BANK_RTC,
.rtc = AB5500_RTC,
.rtc_alarmon = AB5500_RTC_ALARM,
.ti0 = AB5500_TI0,
.nr_ti = 6,
.al0 = AB5500_AL0,
.nr_al = 3,
.epoch = 2000,
.time_to_regs = ab5500_rtc_time_to_regs,
.regs_to_time = ab5500_rtc_regs_to_time,
.alarm_to_regs = ab5500_rtc_alarm_to_regs,
.regs_to_alarm = ab5500_rtc_regs_to_alarm,
.request_read = ab5500_rtc_request_read,
};
static int ab_rtc_request_read(struct device *dev)
{
const struct ab_rtc *variant = to_ab_rtc(dev);
if (!variant->request_read)
return 0;
return variant->request_read(dev);
}
static int ab_rtc_request_write(struct device *dev)
{
const struct ab_rtc *variant = to_ab_rtc(dev);
if (!variant->request_write)
return 0;
return variant->request_write(dev);
}
static bool ab_rtc_valid_time(struct device *dev, struct rtc_time *time)
{
const struct ab_rtc *variant = to_ab_rtc(dev);
if (!variant->epoch)
return true;
return time->tm_year >= variant->epoch - 1900;
}
static int
ab_rtc_tm_to_time(struct device *dev, struct rtc_time *tm, unsigned long *secs)
{
const struct ab_rtc *variant = to_ab_rtc(dev);
rtc_tm_to_time(tm, secs);
if (variant->epoch)
*secs -= ab_rtc_get_elapsed_seconds(variant->epoch);
return 0;
}
static int
ab_rtc_time_to_tm(struct device *dev, unsigned long secs, struct rtc_time *tm)
{
const struct ab_rtc *variant = to_ab_rtc(dev);
if (variant->epoch)
secs += ab_rtc_get_elapsed_seconds(variant->epoch);
rtc_time_to_tm(secs, tm);
return 0;
}
static int ab_rtc_read_time(struct device *dev, struct rtc_time *tm)
{
const struct ab_rtc *variant = to_ab_rtc(dev);
unsigned char buf[variant->nr_ti];
unsigned long secs;
int err;
err = ab_rtc_request_read(dev);
if (err)
return err;
err = abx500_get_register_page_interruptible(dev, variant->bank,
variant->ti0,
buf, variant->nr_ti);
if (err)
return err;
secs = variant->regs_to_time(dev, buf);
ab_rtc_time_to_tm(dev, secs, tm);
return rtc_valid_tm(tm);
}
static int ab_rtc_set_time(struct device *dev, struct rtc_time *tm)
{
const struct ab_rtc *variant = to_ab_rtc(dev);
unsigned char buf[variant->nr_ti];
unsigned long secs;
u8 reg = variant->ti0;
int err;
int i;
if (!ab_rtc_valid_time(dev, tm))
return -EINVAL;
ab_rtc_tm_to_time(dev, tm, &secs);
variant->time_to_regs(dev, secs, buf);
for (i = 0; i < variant->nr_ti; i++, reg++) {
err = abx500_set_register_interruptible(dev, variant->bank,
reg, buf[i]);
if (err)
return err;
}
return ab_rtc_request_write(dev);
}
static int ab_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
{
const struct ab_rtc *variant = to_ab_rtc(dev);
unsigned long secs;
u8 buf[variant->nr_al];
u8 rtcval;
int err;
err = abx500_get_register_interruptible(dev, variant->bank,
variant->rtc, &rtcval);
if (err)
return err;
alarm->enabled = !!(rtcval & variant->rtc_alarmon);
alarm->pending = 0;
err = abx500_get_register_page_interruptible(dev, variant->bank,
variant->al0, buf,
variant->nr_al);
if (err)
return err;
secs = variant->regs_to_alarm(dev, buf);
ab_rtc_time_to_tm(dev, secs, &alarm->time);
return rtc_valid_tm(&alarm->time);
}
static int ab_rtc_alarm_enable(struct device *dev, unsigned int enabled)
{
const struct ab_rtc *variant = to_ab_rtc(dev);
u8 mask = variant->rtc_alarmon;
u8 value = enabled ? mask : 0;
return abx500_mask_and_set_register_interruptible(dev, variant->bank,
variant->rtc, mask,
value);
}
static int ab_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
{
const struct ab_rtc *variant = to_ab_rtc(dev);
unsigned char buf[variant->nr_al];
unsigned long secs;
u8 reg = variant->al0;
int err;
int i;
if (!ab_rtc_valid_time(dev, &alarm->time))
return -EINVAL;
ab_rtc_tm_to_time(dev, &alarm->time, &secs);
variant->alarm_to_regs(dev, secs, buf);
/*
* Disable alarm first. Otherwise the RTC may not detect an alarm
* reprogrammed for the same time without disabling the alarm in
* between the programmings.
*/
err = ab_rtc_alarm_enable(dev, false);
if (err)
return err;
for (i = 0; i < variant->nr_al; i++, reg++) {
err = abx500_set_register_interruptible(dev, variant->bank,
reg, buf[i]);
if (err)
return err;
}
return alarm->enabled ? ab_rtc_alarm_enable(dev, true) : 0;
}
static const struct rtc_class_ops ab_rtc_ops = {
.read_time = ab_rtc_read_time,
.set_time = ab_rtc_set_time,
.read_alarm = ab_rtc_read_alarm,
.set_alarm = ab_rtc_set_alarm,
.alarm_irq_enable = ab_rtc_alarm_enable,
};
static irqreturn_t ab_rtc_irq(int irq, void *dev_id)
{
unsigned long events = RTC_IRQF | RTC_AF;
struct rtc_device *rtc = dev_id;
rtc_update_irq(rtc, 1, events);
return IRQ_HANDLED;
}
static int __devinit ab_rtc_probe(struct platform_device *pdev)
{
const struct ab_rtc *variant = to_ab_rtc(&pdev->dev);
int err;
struct rtc_device *rtc;
int irq = -ENXIO;
if (variant->irqname) {
irq = platform_get_irq_byname(pdev, variant->irqname);
if (irq < 0)
return irq;
}
if (variant->startup) {
err = variant->startup(&pdev->dev);
if (err)
return err;
}
device_init_wakeup(&pdev->dev, true);
rtc = rtc_device_register("ab8500-rtc", &pdev->dev, &ab_rtc_ops,
THIS_MODULE);
if (IS_ERR(rtc)) {
dev_err(&pdev->dev, "Registration failed\n");
err = PTR_ERR(rtc);
return err;
}
if (irq >= 0) {
err = request_any_context_irq(irq, ab_rtc_irq,
IRQF_NO_SUSPEND,
pdev->id_entry->name,
rtc);
if (err < 0) {
dev_err(&pdev->dev, "could not get irq: %d\n", err);
goto out_unregister;
}
}
platform_set_drvdata(pdev, rtc);
return 0;
out_unregister:
rtc_device_unregister(rtc);
return err;
}
static int __devexit ab_rtc_remove(struct platform_device *pdev)
{
const struct ab_rtc *variant = to_ab_rtc(&pdev->dev);
struct rtc_device *rtc = platform_get_drvdata(pdev);
int irq = platform_get_irq_byname(pdev, variant->irqname);
if (irq >= 0)
free_irq(irq, rtc);
rtc_device_unregister(rtc);
platform_set_drvdata(pdev, NULL);
return 0;
}
static struct platform_device_id ab_rtc_id_table[] = {
{ "ab5500-rtc", (kernel_ulong_t)&ab5500_rtc, },
{ },
};
MODULE_DEVICE_TABLE(platform, ab_rtc_id_table);
static struct platform_driver ab_rtc_driver = {
.driver.name = "ab-rtc",
.driver.owner = THIS_MODULE,
.id_table = ab_rtc_id_table,
.probe = ab_rtc_probe,
.remove = __devexit_p(ab_rtc_remove),
};
static int __init ab_rtc_init(void)
{
return platform_driver_register(&ab_rtc_driver);
}
module_init(ab_rtc_init);
static void __exit ab_rtc_exit(void)
{
platform_driver_unregister(&ab_rtc_driver);
}
module_exit(ab_rtc_exit);
MODULE_AUTHOR("Rabin Vincent <rabin.vincent@stericsson.com>");
MODULE_DESCRIPTION("AB5500 RTC Driver");
MODULE_LICENSE("GPL v2");
|