blob: d5af7baa48b56c893e449b418d158f7b25448628 [file] [log] [blame]
Joonyoung Shim9b16c0a2010-08-06 11:28:08 +09001/*
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>
MyungJoo Ham337ce5d2011-01-04 14:17:39 +090023#include <linux/delay.h>
Joonyoung Shim9b16c0a2010-08-06 11:28:08 +090024
25#define MAX8998_RTC_SEC 0x00
26#define MAX8998_RTC_MIN 0x01
27#define MAX8998_RTC_HOUR 0x02
28#define MAX8998_RTC_WEEKDAY 0x03
29#define MAX8998_RTC_DATE 0x04
30#define MAX8998_RTC_MONTH 0x05
31#define MAX8998_RTC_YEAR1 0x06
32#define MAX8998_RTC_YEAR2 0x07
33#define MAX8998_ALARM0_SEC 0x08
34#define MAX8998_ALARM0_MIN 0x09
35#define MAX8998_ALARM0_HOUR 0x0a
36#define MAX8998_ALARM0_WEEKDAY 0x0b
37#define MAX8998_ALARM0_DATE 0x0c
38#define MAX8998_ALARM0_MONTH 0x0d
39#define MAX8998_ALARM0_YEAR1 0x0e
40#define MAX8998_ALARM0_YEAR2 0x0f
41#define MAX8998_ALARM1_SEC 0x10
42#define MAX8998_ALARM1_MIN 0x11
43#define MAX8998_ALARM1_HOUR 0x12
44#define MAX8998_ALARM1_WEEKDAY 0x13
45#define MAX8998_ALARM1_DATE 0x14
46#define MAX8998_ALARM1_MONTH 0x15
47#define MAX8998_ALARM1_YEAR1 0x16
48#define MAX8998_ALARM1_YEAR2 0x17
49#define MAX8998_ALARM0_CONF 0x18
50#define MAX8998_ALARM1_CONF 0x19
51#define MAX8998_RTC_STATUS 0x1a
52#define MAX8998_WTSR_SMPL_CNTL 0x1b
53#define MAX8998_TEST 0x1f
54
55#define HOUR_12 (1 << 7)
56#define HOUR_PM (1 << 5)
57#define ALARM0_STATUS (1 << 1)
58#define ALARM1_STATUS (1 << 2)
59
60enum {
61 RTC_SEC = 0,
62 RTC_MIN,
63 RTC_HOUR,
64 RTC_WEEKDAY,
65 RTC_DATE,
66 RTC_MONTH,
67 RTC_YEAR1,
68 RTC_YEAR2,
69};
70
71struct max8998_rtc_info {
72 struct device *dev;
73 struct max8998_dev *max8998;
74 struct i2c_client *rtc;
75 struct rtc_device *rtc_dev;
76 int irq;
MyungJoo Ham337ce5d2011-01-04 14:17:39 +090077 bool lp3974_bug_workaround;
Joonyoung Shim9b16c0a2010-08-06 11:28:08 +090078};
79
80static void max8998_data_to_tm(u8 *data, struct rtc_time *tm)
81{
82 tm->tm_sec = bcd2bin(data[RTC_SEC]);
83 tm->tm_min = bcd2bin(data[RTC_MIN]);
84 if (data[RTC_HOUR] & HOUR_12) {
85 tm->tm_hour = bcd2bin(data[RTC_HOUR] & 0x1f);
86 if (data[RTC_HOUR] & HOUR_PM)
87 tm->tm_hour += 12;
88 } else
89 tm->tm_hour = bcd2bin(data[RTC_HOUR] & 0x3f);
90
91 tm->tm_wday = data[RTC_WEEKDAY] & 0x07;
92 tm->tm_mday = bcd2bin(data[RTC_DATE]);
93 tm->tm_mon = bcd2bin(data[RTC_MONTH]);
94 tm->tm_year = bcd2bin(data[RTC_YEAR1]) + bcd2bin(data[RTC_YEAR2]) * 100;
95 tm->tm_year -= 1900;
96}
97
98static void max8998_tm_to_data(struct rtc_time *tm, u8 *data)
99{
100 data[RTC_SEC] = bin2bcd(tm->tm_sec);
101 data[RTC_MIN] = bin2bcd(tm->tm_min);
102 data[RTC_HOUR] = bin2bcd(tm->tm_hour);
103 data[RTC_WEEKDAY] = tm->tm_wday;
104 data[RTC_DATE] = bin2bcd(tm->tm_mday);
105 data[RTC_MONTH] = bin2bcd(tm->tm_mon);
106 data[RTC_YEAR1] = bin2bcd(tm->tm_year % 100);
107 data[RTC_YEAR2] = bin2bcd((tm->tm_year + 1900) / 100);
108}
109
110static int max8998_rtc_read_time(struct device *dev, struct rtc_time *tm)
111{
112 struct max8998_rtc_info *info = dev_get_drvdata(dev);
113 u8 data[8];
114 int ret;
115
116 ret = max8998_bulk_read(info->rtc, MAX8998_RTC_SEC, 8, data);
117 if (ret < 0)
118 return ret;
119
120 max8998_data_to_tm(data, tm);
121
122 return rtc_valid_tm(tm);
123}
124
125static int max8998_rtc_set_time(struct device *dev, struct rtc_time *tm)
126{
127 struct max8998_rtc_info *info = dev_get_drvdata(dev);
128 u8 data[8];
MyungJoo Ham337ce5d2011-01-04 14:17:39 +0900129 int ret;
Joonyoung Shim9b16c0a2010-08-06 11:28:08 +0900130
131 max8998_tm_to_data(tm, data);
132
MyungJoo Ham337ce5d2011-01-04 14:17:39 +0900133 ret = max8998_bulk_write(info->rtc, MAX8998_RTC_SEC, 8, data);
134
135 if (info->lp3974_bug_workaround)
136 msleep(2000);
137
138 return ret;
Joonyoung Shim9b16c0a2010-08-06 11:28:08 +0900139}
140
141static int max8998_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
142{
143 struct max8998_rtc_info *info = dev_get_drvdata(dev);
144 u8 data[8];
145 u8 val;
146 int ret;
147
148 ret = max8998_bulk_read(info->rtc, MAX8998_ALARM0_SEC, 8, data);
149 if (ret < 0)
150 return ret;
151
152 max8998_data_to_tm(data, &alrm->time);
153
154 ret = max8998_read_reg(info->rtc, MAX8998_ALARM0_CONF, &val);
155 if (ret < 0)
156 return ret;
157
158 alrm->enabled = !!val;
159
160 ret = max8998_read_reg(info->rtc, MAX8998_RTC_STATUS, &val);
161 if (ret < 0)
162 return ret;
163
164 if (val & ALARM0_STATUS)
165 alrm->pending = 1;
166 else
167 alrm->pending = 0;
168
169 return 0;
170}
171
172static int max8998_rtc_stop_alarm(struct max8998_rtc_info *info)
173{
MyungJoo Ham337ce5d2011-01-04 14:17:39 +0900174 int ret = max8998_write_reg(info->rtc, MAX8998_ALARM0_CONF, 0);
175
176 if (info->lp3974_bug_workaround)
177 msleep(2000);
178
179 return ret;
Joonyoung Shim9b16c0a2010-08-06 11:28:08 +0900180}
181
182static int max8998_rtc_start_alarm(struct max8998_rtc_info *info)
183{
MyungJoo Ham337ce5d2011-01-04 14:17:39 +0900184 int ret;
185 u8 alarm0_conf = 0x77;
186
187 /* LP3974 with delay bug chips has rtc alarm bugs with "MONTH" field */
188 if (info->lp3974_bug_workaround)
189 alarm0_conf = 0x57;
190
191 ret = max8998_write_reg(info->rtc, MAX8998_ALARM0_CONF, alarm0_conf);
192
193 if (info->lp3974_bug_workaround)
194 msleep(2000);
195
196 return ret;
Joonyoung Shim9b16c0a2010-08-06 11:28:08 +0900197}
198
199static int max8998_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
200{
201 struct max8998_rtc_info *info = dev_get_drvdata(dev);
202 u8 data[8];
203 int ret;
204
205 max8998_tm_to_data(&alrm->time, data);
206
207 ret = max8998_rtc_stop_alarm(info);
208 if (ret < 0)
209 return ret;
210
211 ret = max8998_bulk_write(info->rtc, MAX8998_ALARM0_SEC, 8, data);
212 if (ret < 0)
213 return ret;
214
MyungJoo Ham337ce5d2011-01-04 14:17:39 +0900215 if (info->lp3974_bug_workaround)
216 msleep(2000);
Joonyoung Shim9b16c0a2010-08-06 11:28:08 +0900217
MyungJoo Ham337ce5d2011-01-04 14:17:39 +0900218 if (alrm->enabled)
219 ret = max8998_rtc_start_alarm(info);
220
221 return ret;
Joonyoung Shim9b16c0a2010-08-06 11:28:08 +0900222}
223
224static int max8998_rtc_alarm_irq_enable(struct device *dev,
225 unsigned int enabled)
226{
227 struct max8998_rtc_info *info = dev_get_drvdata(dev);
228
229 if (enabled)
230 return max8998_rtc_start_alarm(info);
231 else
232 return max8998_rtc_stop_alarm(info);
233}
234
235static irqreturn_t max8998_rtc_alarm_irq(int irq, void *data)
236{
237 struct max8998_rtc_info *info = data;
238
239 rtc_update_irq(info->rtc_dev, 1, RTC_IRQF | RTC_AF);
240
241 return IRQ_HANDLED;
242}
243
244static const struct rtc_class_ops max8998_rtc_ops = {
245 .read_time = max8998_rtc_read_time,
246 .set_time = max8998_rtc_set_time,
247 .read_alarm = max8998_rtc_read_alarm,
248 .set_alarm = max8998_rtc_set_alarm,
249 .alarm_irq_enable = max8998_rtc_alarm_irq_enable,
250};
251
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800252static int max8998_rtc_probe(struct platform_device *pdev)
Joonyoung Shim9b16c0a2010-08-06 11:28:08 +0900253{
254 struct max8998_dev *max8998 = dev_get_drvdata(pdev->dev.parent);
MyungJoo Ham337ce5d2011-01-04 14:17:39 +0900255 struct max8998_platform_data *pdata = dev_get_platdata(max8998->dev);
Joonyoung Shim9b16c0a2010-08-06 11:28:08 +0900256 struct max8998_rtc_info *info;
257 int ret;
258
Jingoo Han8925e312013-04-29 16:20:43 -0700259 info = devm_kzalloc(&pdev->dev, sizeof(struct max8998_rtc_info),
260 GFP_KERNEL);
Joonyoung Shim9b16c0a2010-08-06 11:28:08 +0900261 if (!info)
262 return -ENOMEM;
263
264 info->dev = &pdev->dev;
265 info->max8998 = max8998;
266 info->rtc = max8998->rtc;
267 info->irq = max8998->irq_base + MAX8998_IRQ_ALARM0;
268
John Stultz03cf7c42011-05-06 17:27:07 -0700269 platform_set_drvdata(pdev, info);
270
Jingoo Han8925e312013-04-29 16:20:43 -0700271 info->rtc_dev = devm_rtc_device_register(&pdev->dev, "max8998-rtc",
Joonyoung Shim9b16c0a2010-08-06 11:28:08 +0900272 &max8998_rtc_ops, THIS_MODULE);
273
274 if (IS_ERR(info->rtc_dev)) {
275 ret = PTR_ERR(info->rtc_dev);
276 dev_err(&pdev->dev, "Failed to register RTC device: %d\n", ret);
277 goto out_rtc;
278 }
279
Jingoo Han8925e312013-04-29 16:20:43 -0700280 ret = devm_request_threaded_irq(&pdev->dev, info->irq, NULL,
281 max8998_rtc_alarm_irq, 0, "rtc-alarm0", info);
MyungJoo Ham337ce5d2011-01-04 14:17:39 +0900282
Joonyoung Shim9b16c0a2010-08-06 11:28:08 +0900283 if (ret < 0)
284 dev_err(&pdev->dev, "Failed to request alarm IRQ: %d: %d\n",
285 info->irq, ret);
286
MyungJoo Ham337ce5d2011-01-04 14:17:39 +0900287 dev_info(&pdev->dev, "RTC CHIP NAME: %s\n", pdev->id_entry->name);
Tomasz Figa43c523b2013-05-24 15:55:35 -0700288 if (pdata && pdata->rtc_delay) {
MyungJoo Ham337ce5d2011-01-04 14:17:39 +0900289 info->lp3974_bug_workaround = true;
290 dev_warn(&pdev->dev, "LP3974 with RTC REGERR option."
291 " RTC updates will be extremely slow.\n");
292 }
293
Joonyoung Shim9b16c0a2010-08-06 11:28:08 +0900294 return 0;
295
296out_rtc:
John Stultz03cf7c42011-05-06 17:27:07 -0700297 platform_set_drvdata(pdev, NULL);
Joonyoung Shim9b16c0a2010-08-06 11:28:08 +0900298 return ret;
299}
300
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800301static int max8998_rtc_remove(struct platform_device *pdev)
Joonyoung Shim9b16c0a2010-08-06 11:28:08 +0900302{
Joonyoung Shim9b16c0a2010-08-06 11:28:08 +0900303 return 0;
304}
305
MyungJoo Ham337ce5d2011-01-04 14:17:39 +0900306static const struct platform_device_id max8998_rtc_id[] = {
307 { "max8998-rtc", TYPE_MAX8998 },
308 { "lp3974-rtc", TYPE_LP3974 },
309 { }
310};
311
Joonyoung Shim9b16c0a2010-08-06 11:28:08 +0900312static struct platform_driver max8998_rtc_driver = {
313 .driver = {
314 .name = "max8998-rtc",
315 .owner = THIS_MODULE,
316 },
317 .probe = max8998_rtc_probe,
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800318 .remove = max8998_rtc_remove,
MyungJoo Ham337ce5d2011-01-04 14:17:39 +0900319 .id_table = max8998_rtc_id,
Joonyoung Shim9b16c0a2010-08-06 11:28:08 +0900320};
321
Axel Lin0c4eae62012-01-10 15:10:48 -0800322module_platform_driver(max8998_rtc_driver);
Joonyoung Shim9b16c0a2010-08-06 11:28:08 +0900323
324MODULE_AUTHOR("Minkyu Kang <mk7.kang@samsung.com>");
325MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
326MODULE_DESCRIPTION("Maxim MAX8998 RTC driver");
327MODULE_LICENSE("GPL");