blob: 113493b5214952b2740a9c1f270525572bc07545 [file] [log] [blame]
David S. Millercca4c232008-08-29 01:29:53 -07001/* rtc-bq4802.c: TI BQ4802 RTC driver.
2 *
3 * Copyright (C) 2008 David S. Miller <davem@davemloft.net>
4 */
5
6#include <linux/kernel.h>
7#include <linux/module.h>
8#include <linux/init.h>
9#include <linux/io.h>
10#include <linux/platform_device.h>
11#include <linux/rtc.h>
12#include <linux/bcd.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
David S. Millercca4c232008-08-29 01:29:53 -070014
15MODULE_AUTHOR("David S. Miller <davem@davemloft.net>");
16MODULE_DESCRIPTION("TI BQ4802 RTC driver");
17MODULE_LICENSE("GPL");
18
19struct bq4802 {
20 void __iomem *regs;
David S. Miller503acc82008-09-09 19:50:04 -070021 unsigned long ioport;
David S. Millercca4c232008-08-29 01:29:53 -070022 struct rtc_device *rtc;
23 spinlock_t lock;
24 struct resource *r;
25 u8 (*read)(struct bq4802 *, int);
26 void (*write)(struct bq4802 *, int, u8);
27};
28
29static u8 bq4802_read_io(struct bq4802 *p, int off)
30{
David S. Miller503acc82008-09-09 19:50:04 -070031 return inb(p->ioport + off);
David S. Millercca4c232008-08-29 01:29:53 -070032}
33
34static void bq4802_write_io(struct bq4802 *p, int off, u8 val)
35{
David S. Miller503acc82008-09-09 19:50:04 -070036 outb(val, p->ioport + off);
David S. Millercca4c232008-08-29 01:29:53 -070037}
38
39static u8 bq4802_read_mem(struct bq4802 *p, int off)
40{
41 return readb(p->regs + off);
42}
43
44static void bq4802_write_mem(struct bq4802 *p, int off, u8 val)
45{
David S. Miller503acc82008-09-09 19:50:04 -070046 writeb(val, p->regs + off);
David S. Millercca4c232008-08-29 01:29:53 -070047}
48
49static int bq4802_read_time(struct device *dev, struct rtc_time *tm)
50{
Wolfram Sang85368bb2018-04-19 16:06:14 +020051 struct bq4802 *p = dev_get_drvdata(dev);
David S. Millercca4c232008-08-29 01:29:53 -070052 unsigned long flags;
53 unsigned int century;
54 u8 val;
55
56 spin_lock_irqsave(&p->lock, flags);
57
58 val = p->read(p, 0x0e);
59 p->write(p, 0xe, val | 0x08);
60
61 tm->tm_sec = p->read(p, 0x00);
62 tm->tm_min = p->read(p, 0x02);
63 tm->tm_hour = p->read(p, 0x04);
64 tm->tm_mday = p->read(p, 0x06);
65 tm->tm_mon = p->read(p, 0x09);
66 tm->tm_year = p->read(p, 0x0a);
67 tm->tm_wday = p->read(p, 0x08);
68 century = p->read(p, 0x0f);
69
70 p->write(p, 0x0e, val);
71
72 spin_unlock_irqrestore(&p->lock, flags);
73
Andrew Mortone232cfd2008-10-18 20:28:43 -070074 tm->tm_sec = bcd2bin(tm->tm_sec);
75 tm->tm_min = bcd2bin(tm->tm_min);
76 tm->tm_hour = bcd2bin(tm->tm_hour);
77 tm->tm_mday = bcd2bin(tm->tm_mday);
78 tm->tm_mon = bcd2bin(tm->tm_mon);
79 tm->tm_year = bcd2bin(tm->tm_year);
80 tm->tm_wday = bcd2bin(tm->tm_wday);
81 century = bcd2bin(century);
David S. Millercca4c232008-08-29 01:29:53 -070082
83 tm->tm_year += (century * 100);
84 tm->tm_year -= 1900;
85
86 tm->tm_mon--;
87
88 return 0;
89}
90
91static int bq4802_set_time(struct device *dev, struct rtc_time *tm)
92{
Wolfram Sang85368bb2018-04-19 16:06:14 +020093 struct bq4802 *p = dev_get_drvdata(dev);
David S. Millercca4c232008-08-29 01:29:53 -070094 u8 sec, min, hrs, day, mon, yrs, century, val;
95 unsigned long flags;
96 unsigned int year;
97
98 year = tm->tm_year + 1900;
99 century = year / 100;
100 yrs = year % 100;
101
102 mon = tm->tm_mon + 1; /* tm_mon starts at zero */
103 day = tm->tm_mday;
104 hrs = tm->tm_hour;
105 min = tm->tm_min;
106 sec = tm->tm_sec;
107
Andrew Mortone232cfd2008-10-18 20:28:43 -0700108 sec = bin2bcd(sec);
109 min = bin2bcd(min);
110 hrs = bin2bcd(hrs);
111 day = bin2bcd(day);
112 mon = bin2bcd(mon);
113 yrs = bin2bcd(yrs);
114 century = bin2bcd(century);
David S. Millercca4c232008-08-29 01:29:53 -0700115
116 spin_lock_irqsave(&p->lock, flags);
117
118 val = p->read(p, 0x0e);
119 p->write(p, 0x0e, val | 0x08);
120
121 p->write(p, 0x00, sec);
122 p->write(p, 0x02, min);
123 p->write(p, 0x04, hrs);
124 p->write(p, 0x06, day);
125 p->write(p, 0x09, mon);
126 p->write(p, 0x0a, yrs);
127 p->write(p, 0x0f, century);
128
129 p->write(p, 0x0e, val);
130
131 spin_unlock_irqrestore(&p->lock, flags);
132
133 return 0;
134}
135
136static const struct rtc_class_ops bq4802_ops = {
137 .read_time = bq4802_read_time,
138 .set_time = bq4802_set_time,
139};
140
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800141static int bq4802_probe(struct platform_device *pdev)
David S. Millercca4c232008-08-29 01:29:53 -0700142{
Jingoo Han26c5f7d2013-04-29 16:20:36 -0700143 struct bq4802 *p = devm_kzalloc(&pdev->dev, sizeof(*p), GFP_KERNEL);
David S. Millercca4c232008-08-29 01:29:53 -0700144 int err = -ENOMEM;
145
146 if (!p)
147 goto out;
148
149 spin_lock_init(&p->lock);
150
151 p->r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
152 if (!p->r) {
153 p->r = platform_get_resource(pdev, IORESOURCE_IO, 0);
154 err = -EINVAL;
155 if (!p->r)
Jingoo Han26c5f7d2013-04-29 16:20:36 -0700156 goto out;
David S. Millercca4c232008-08-29 01:29:53 -0700157 }
158 if (p->r->flags & IORESOURCE_IO) {
David S. Miller503acc82008-09-09 19:50:04 -0700159 p->ioport = p->r->start;
David S. Millercca4c232008-08-29 01:29:53 -0700160 p->read = bq4802_read_io;
161 p->write = bq4802_write_io;
162 } else if (p->r->flags & IORESOURCE_MEM) {
Jingoo Han26c5f7d2013-04-29 16:20:36 -0700163 p->regs = devm_ioremap(&pdev->dev, p->r->start,
164 resource_size(p->r));
Zhouyang Jia7874b912018-06-12 12:40:03 +0800165 if (!p->regs){
166 err = -ENOMEM;
167 goto out;
168 }
David S. Millercca4c232008-08-29 01:29:53 -0700169 p->read = bq4802_read_mem;
170 p->write = bq4802_write_mem;
171 } else {
172 err = -EINVAL;
Jingoo Han26c5f7d2013-04-29 16:20:36 -0700173 goto out;
David S. Millercca4c232008-08-29 01:29:53 -0700174 }
175
Alessandro Zummob74d2ca2009-12-15 16:45:53 -0800176 platform_set_drvdata(pdev, p);
177
Jingoo Han26c5f7d2013-04-29 16:20:36 -0700178 p->rtc = devm_rtc_device_register(&pdev->dev, "bq4802",
179 &bq4802_ops, THIS_MODULE);
David S. Millercca4c232008-08-29 01:29:53 -0700180 if (IS_ERR(p->rtc)) {
181 err = PTR_ERR(p->rtc);
Jingoo Han26c5f7d2013-04-29 16:20:36 -0700182 goto out;
David S. Millercca4c232008-08-29 01:29:53 -0700183 }
184
David S. Millercca4c232008-08-29 01:29:53 -0700185 err = 0;
186out:
187 return err;
188
David S. Millercca4c232008-08-29 01:29:53 -0700189}
190
David S. Millercca4c232008-08-29 01:29:53 -0700191/* work with hotplug and coldplug */
192MODULE_ALIAS("platform:rtc-bq4802");
193
194static struct platform_driver bq4802_driver = {
195 .driver = {
196 .name = "rtc-bq4802",
David S. Millercca4c232008-08-29 01:29:53 -0700197 },
198 .probe = bq4802_probe,
David S. Millercca4c232008-08-29 01:29:53 -0700199};
200
Axel Lin0c4eae62012-01-10 15:10:48 -0800201module_platform_driver(bq4802_driver);