aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/rtc/rtc-max8998.c
diff options
context:
space:
mode:
authorJoonyoung Shim <jy0922.shim@samsung.com>2010-08-05 22:28:08 -0400
committerSamuel Ortiz <sameo@linux.intel.com>2010-10-28 18:29:06 -0400
commit9b16c0a43b74393cc18666a7748293812c61af1f (patch)
treef75874e30996e6d6b1dc5a975d19f7455c3e44d6 /drivers/rtc/rtc-max8998.c
parent2c7e6f5797140b33ec2b967ff28941e1c7eff4b2 (diff)
rtc: Add MAX8998 rtc driver
This adds support for the RTC provided by the Maxim 8998 chip. This driver was tested on a GONI board by using the rtc-test application from the Documentation/rtc.txt. Signed-off-by: Joonyoung Shim <jy0922.shim@samsung.com> Signed-off-by: Kyungmin Park <kyungmin.park@samsung.com> Acked-by: Alessandro Zummo <a.zummo@towertech.it> Signed-off-by: Samuel Ortiz <sameo@linux.intel.com>
Diffstat (limited to 'drivers/rtc/rtc-max8998.c')
-rw-r--r--drivers/rtc/rtc-max8998.c300
1 files changed, 300 insertions, 0 deletions
diff --git a/drivers/rtc/rtc-max8998.c b/drivers/rtc/rtc-max8998.c
new file mode 100644
index 000000000000..f22dee35f330
--- /dev/null
+++ b/drivers/rtc/rtc-max8998.c
@@ -0,0 +1,300 @@
1/*
2 * RTC driver for Maxim MAX8998
3 *
4 * Copyright (C) 2010 Samsung Electronics Co.Ltd
5 * Author: Minkyu Kang <mk7.kang@samsung.com>
6 * Author: Joonyoung Shim <jy0922.shim@samsung.com>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
12 *
13 */
14
15#include <linux/module.h>
16#include <linux/i2c.h>
17#include <linux/slab.h>
18#include <linux/bcd.h>
19#include <linux/rtc.h>
20#include <linux/platform_device.h>
21#include <linux/mfd/max8998.h>
22#include <linux/mfd/max8998-private.h>
23
24#define MAX8998_RTC_SEC 0x00
25#define MAX8998_RTC_MIN 0x01
26#define MAX8998_RTC_HOUR 0x02
27#define MAX8998_RTC_WEEKDAY 0x03
28#define MAX8998_RTC_DATE 0x04
29#define MAX8998_RTC_MONTH 0x05
30#define MAX8998_RTC_YEAR1 0x06
31#define MAX8998_RTC_YEAR2 0x07
32#define MAX8998_ALARM0_SEC 0x08
33#define MAX8998_ALARM0_MIN 0x09
34#define MAX8998_ALARM0_HOUR 0x0a
35#define MAX8998_ALARM0_WEEKDAY 0x0b
36#define MAX8998_ALARM0_DATE 0x0c
37#define MAX8998_ALARM0_MONTH 0x0d
38#define MAX8998_ALARM0_YEAR1 0x0e
39#define MAX8998_ALARM0_YEAR2 0x0f
40#define MAX8998_ALARM1_SEC 0x10
41#define MAX8998_ALARM1_MIN 0x11
42#define MAX8998_ALARM1_HOUR 0x12
43#define MAX8998_ALARM1_WEEKDAY 0x13
44#define MAX8998_ALARM1_DATE 0x14
45#define MAX8998_ALARM1_MONTH 0x15
46#define MAX8998_ALARM1_YEAR1 0x16
47#define MAX8998_ALARM1_YEAR2 0x17
48#define MAX8998_ALARM0_CONF 0x18
49#define MAX8998_ALARM1_CONF 0x19
50#define MAX8998_RTC_STATUS 0x1a
51#define MAX8998_WTSR_SMPL_CNTL 0x1b
52#define MAX8998_TEST 0x1f
53
54#define HOUR_12 (1 << 7)
55#define HOUR_PM (1 << 5)
56#define ALARM0_STATUS (1 << 1)
57#define ALARM1_STATUS (1 << 2)
58
59enum {
60 RTC_SEC = 0,
61 RTC_MIN,
62 RTC_HOUR,
63 RTC_WEEKDAY,
64 RTC_DATE,
65 RTC_MONTH,
66 RTC_YEAR1,
67 RTC_YEAR2,
68};
69
70struct max8998_rtc_info {
71 struct device *dev;
72 struct max8998_dev *max8998;
73 struct i2c_client *rtc;
74 struct rtc_device *rtc_dev;
75 int irq;
76};
77
78static void max8998_data_to_tm(u8 *data, struct rtc_time *tm)
79{
80 tm->tm_sec = bcd2bin(data[RTC_SEC]);
81 tm->tm_min = bcd2bin(data[RTC_MIN]);
82 if (data[RTC_HOUR] & HOUR_12) {
83 tm->tm_hour = bcd2bin(data[RTC_HOUR] & 0x1f);
84 if (data[RTC_HOUR] & HOUR_PM)
85 tm->tm_hour += 12;
86 } else
87 tm->tm_hour = bcd2bin(data[RTC_HOUR] & 0x3f);
88
89 tm->tm_wday = data[RTC_WEEKDAY] & 0x07;
90 tm->tm_mday = bcd2bin(data[RTC_DATE]);
91 tm->tm_mon = bcd2bin(data[RTC_MONTH]);
92 tm->tm_year = bcd2bin(data[RTC_YEAR1]) + bcd2bin(data[RTC_YEAR2]) * 100;
93 tm->tm_year -= 1900;
94}
95
96static void max8998_tm_to_data(struct rtc_time *tm, u8 *data)
97{
98 data[RTC_SEC] = bin2bcd(tm->tm_sec);
99 data[RTC_MIN] = bin2bcd(tm->tm_min);
100 data[RTC_HOUR] = bin2bcd(tm->tm_hour);
101 data[RTC_WEEKDAY] = tm->tm_wday;
102 data[RTC_DATE] = bin2bcd(tm->tm_mday);
103 data[RTC_MONTH] = bin2bcd(tm->tm_mon);
104 data[RTC_YEAR1] = bin2bcd(tm->tm_year % 100);
105 data[RTC_YEAR2] = bin2bcd((tm->tm_year + 1900) / 100);
106}
107
108static int max8998_rtc_read_time(struct device *dev, struct rtc_time *tm)
109{
110 struct max8998_rtc_info *info = dev_get_drvdata(dev);
111 u8 data[8];
112 int ret;
113
114 ret = max8998_bulk_read(info->rtc, MAX8998_RTC_SEC, 8, data);
115 if (ret < 0)
116 return ret;
117
118 max8998_data_to_tm(data, tm);
119
120 return rtc_valid_tm(tm);
121}
122
123static int max8998_rtc_set_time(struct device *dev, struct rtc_time *tm)
124{
125 struct max8998_rtc_info *info = dev_get_drvdata(dev);
126 u8 data[8];
127
128 max8998_tm_to_data(tm, data);
129
130 return max8998_bulk_write(info->rtc, MAX8998_RTC_SEC, 8, data);
131}
132
133static int max8998_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
134{
135 struct max8998_rtc_info *info = dev_get_drvdata(dev);
136 u8 data[8];
137 u8 val;
138 int ret;
139
140 ret = max8998_bulk_read(info->rtc, MAX8998_ALARM0_SEC, 8, data);
141 if (ret < 0)
142 return ret;
143
144 max8998_data_to_tm(data, &alrm->time);
145
146 ret = max8998_read_reg(info->rtc, MAX8998_ALARM0_CONF, &val);
147 if (ret < 0)
148 return ret;
149
150 alrm->enabled = !!val;
151
152 ret = max8998_read_reg(info->rtc, MAX8998_RTC_STATUS, &val);
153 if (ret < 0)
154 return ret;
155
156 if (val & ALARM0_STATUS)
157 alrm->pending = 1;
158 else
159 alrm->pending = 0;
160
161 return 0;
162}
163
164static int max8998_rtc_stop_alarm(struct max8998_rtc_info *info)
165{
166 return max8998_write_reg(info->rtc, MAX8998_ALARM0_CONF, 0);
167}
168
169static int max8998_rtc_start_alarm(struct max8998_rtc_info *info)
170{
171 return max8998_write_reg(info->rtc, MAX8998_ALARM0_CONF, 0x77);
172}
173
174static int max8998_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
175{
176 struct max8998_rtc_info *info = dev_get_drvdata(dev);
177 u8 data[8];
178 int ret;
179
180 max8998_tm_to_data(&alrm->time, data);
181
182 ret = max8998_rtc_stop_alarm(info);
183 if (ret < 0)
184 return ret;
185
186 ret = max8998_bulk_write(info->rtc, MAX8998_ALARM0_SEC, 8, data);
187 if (ret < 0)
188 return ret;
189
190 if (alrm->enabled)
191 return max8998_rtc_start_alarm(info);
192
193 return 0;
194}
195
196static int max8998_rtc_alarm_irq_enable(struct device *dev,
197 unsigned int enabled)
198{
199 struct max8998_rtc_info *info = dev_get_drvdata(dev);
200
201 if (enabled)
202 return max8998_rtc_start_alarm(info);
203 else
204 return max8998_rtc_stop_alarm(info);
205}
206
207static irqreturn_t max8998_rtc_alarm_irq(int irq, void *data)
208{
209 struct max8998_rtc_info *info = data;
210
211 rtc_update_irq(info->rtc_dev, 1, RTC_IRQF | RTC_AF);
212
213 return IRQ_HANDLED;
214}
215
216static const struct rtc_class_ops max8998_rtc_ops = {
217 .read_time = max8998_rtc_read_time,
218 .set_time = max8998_rtc_set_time,
219 .read_alarm = max8998_rtc_read_alarm,
220 .set_alarm = max8998_rtc_set_alarm,
221 .alarm_irq_enable = max8998_rtc_alarm_irq_enable,
222};
223
224static int __devinit max8998_rtc_probe(struct platform_device *pdev)
225{
226 struct max8998_dev *max8998 = dev_get_drvdata(pdev->dev.parent);
227 struct max8998_rtc_info *info;
228 int ret;
229
230 info = kzalloc(sizeof(struct max8998_rtc_info), GFP_KERNEL);
231 if (!info)
232 return -ENOMEM;
233
234 info->dev = &pdev->dev;
235 info->max8998 = max8998;
236 info->rtc = max8998->rtc;
237 info->irq = max8998->irq_base + MAX8998_IRQ_ALARM0;
238
239 info->rtc_dev = rtc_device_register("max8998-rtc", &pdev->dev,
240 &max8998_rtc_ops, THIS_MODULE);
241
242 if (IS_ERR(info->rtc_dev)) {
243 ret = PTR_ERR(info->rtc_dev);
244 dev_err(&pdev->dev, "Failed to register RTC device: %d\n", ret);
245 goto out_rtc;
246 }
247
248 platform_set_drvdata(pdev, info);
249
250 ret = request_threaded_irq(info->irq, NULL, max8998_rtc_alarm_irq, 0,
251 "rtc-alarm0", info);
252 if (ret < 0)
253 dev_err(&pdev->dev, "Failed to request alarm IRQ: %d: %d\n",
254 info->irq, ret);
255
256 return 0;
257
258out_rtc:
259 kfree(info);
260 return ret;
261}
262
263static int __devexit max8998_rtc_remove(struct platform_device *pdev)
264{
265 struct max8998_rtc_info *info = platform_get_drvdata(pdev);
266
267 if (info) {
268 free_irq(info->irq, info);
269 rtc_device_unregister(info->rtc_dev);
270 kfree(info);
271 }
272
273 return 0;
274}
275
276static struct platform_driver max8998_rtc_driver = {
277 .driver = {
278 .name = "max8998-rtc",
279 .owner = THIS_MODULE,
280 },
281 .probe = max8998_rtc_probe,
282 .remove = __devexit_p(max8998_rtc_remove),
283};
284
285static int __init max8998_rtc_init(void)
286{
287 return platform_driver_register(&max8998_rtc_driver);
288}
289module_init(max8998_rtc_init);
290
291static void __exit max8998_rtc_exit(void)
292{
293 platform_driver_unregister(&max8998_rtc_driver);
294}
295module_exit(max8998_rtc_exit);
296
297MODULE_AUTHOR("Minkyu Kang <mk7.kang@samsung.com>");
298MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
299MODULE_DESCRIPTION("Maxim MAX8998 RTC driver");
300MODULE_LICENSE("GPL");