blob: 30804b00985e5f84770189a68eacad9854bea335 [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>
Tomasz Figa443c6ae2013-06-24 14:39:52 +020019#include <linux/irqdomain.h>
Joonyoung Shim9b16c0a2010-08-06 11:28:08 +090020#include <linux/rtc.h>
21#include <linux/platform_device.h>
22#include <linux/mfd/max8998.h>
23#include <linux/mfd/max8998-private.h>
MyungJoo Ham337ce5d2011-01-04 14:17:39 +090024#include <linux/delay.h>
Joonyoung Shim9b16c0a2010-08-06 11:28:08 +090025
26#define MAX8998_RTC_SEC 0x00
27#define MAX8998_RTC_MIN 0x01
28#define MAX8998_RTC_HOUR 0x02
29#define MAX8998_RTC_WEEKDAY 0x03
30#define MAX8998_RTC_DATE 0x04
31#define MAX8998_RTC_MONTH 0x05
32#define MAX8998_RTC_YEAR1 0x06
33#define MAX8998_RTC_YEAR2 0x07
34#define MAX8998_ALARM0_SEC 0x08
35#define MAX8998_ALARM0_MIN 0x09
36#define MAX8998_ALARM0_HOUR 0x0a
37#define MAX8998_ALARM0_WEEKDAY 0x0b
38#define MAX8998_ALARM0_DATE 0x0c
39#define MAX8998_ALARM0_MONTH 0x0d
40#define MAX8998_ALARM0_YEAR1 0x0e
41#define MAX8998_ALARM0_YEAR2 0x0f
42#define MAX8998_ALARM1_SEC 0x10
43#define MAX8998_ALARM1_MIN 0x11
44#define MAX8998_ALARM1_HOUR 0x12
45#define MAX8998_ALARM1_WEEKDAY 0x13
46#define MAX8998_ALARM1_DATE 0x14
47#define MAX8998_ALARM1_MONTH 0x15
48#define MAX8998_ALARM1_YEAR1 0x16
49#define MAX8998_ALARM1_YEAR2 0x17
50#define MAX8998_ALARM0_CONF 0x18
51#define MAX8998_ALARM1_CONF 0x19
52#define MAX8998_RTC_STATUS 0x1a
53#define MAX8998_WTSR_SMPL_CNTL 0x1b
54#define MAX8998_TEST 0x1f
55
56#define HOUR_12 (1 << 7)
57#define HOUR_PM (1 << 5)
58#define ALARM0_STATUS (1 << 1)
59#define ALARM1_STATUS (1 << 2)
60
61enum {
62 RTC_SEC = 0,
63 RTC_MIN,
64 RTC_HOUR,
65 RTC_WEEKDAY,
66 RTC_DATE,
67 RTC_MONTH,
68 RTC_YEAR1,
69 RTC_YEAR2,
70};
71
72struct max8998_rtc_info {
73 struct device *dev;
74 struct max8998_dev *max8998;
75 struct i2c_client *rtc;
76 struct rtc_device *rtc_dev;
77 int irq;
MyungJoo Ham337ce5d2011-01-04 14:17:39 +090078 bool lp3974_bug_workaround;
Joonyoung Shim9b16c0a2010-08-06 11:28:08 +090079};
80
81static void max8998_data_to_tm(u8 *data, struct rtc_time *tm)
82{
83 tm->tm_sec = bcd2bin(data[RTC_SEC]);
84 tm->tm_min = bcd2bin(data[RTC_MIN]);
85 if (data[RTC_HOUR] & HOUR_12) {
86 tm->tm_hour = bcd2bin(data[RTC_HOUR] & 0x1f);
87 if (data[RTC_HOUR] & HOUR_PM)
88 tm->tm_hour += 12;
89 } else
90 tm->tm_hour = bcd2bin(data[RTC_HOUR] & 0x3f);
91
92 tm->tm_wday = data[RTC_WEEKDAY] & 0x07;
93 tm->tm_mday = bcd2bin(data[RTC_DATE]);
94 tm->tm_mon = bcd2bin(data[RTC_MONTH]);
95 tm->tm_year = bcd2bin(data[RTC_YEAR1]) + bcd2bin(data[RTC_YEAR2]) * 100;
96 tm->tm_year -= 1900;
97}
98
99static void max8998_tm_to_data(struct rtc_time *tm, u8 *data)
100{
101 data[RTC_SEC] = bin2bcd(tm->tm_sec);
102 data[RTC_MIN] = bin2bcd(tm->tm_min);
103 data[RTC_HOUR] = bin2bcd(tm->tm_hour);
104 data[RTC_WEEKDAY] = tm->tm_wday;
105 data[RTC_DATE] = bin2bcd(tm->tm_mday);
106 data[RTC_MONTH] = bin2bcd(tm->tm_mon);
107 data[RTC_YEAR1] = bin2bcd(tm->tm_year % 100);
108 data[RTC_YEAR2] = bin2bcd((tm->tm_year + 1900) / 100);
109}
110
111static int max8998_rtc_read_time(struct device *dev, struct rtc_time *tm)
112{
113 struct max8998_rtc_info *info = dev_get_drvdata(dev);
114 u8 data[8];
115 int ret;
116
117 ret = max8998_bulk_read(info->rtc, MAX8998_RTC_SEC, 8, data);
118 if (ret < 0)
119 return ret;
120
121 max8998_data_to_tm(data, tm);
122
123 return rtc_valid_tm(tm);
124}
125
126static int max8998_rtc_set_time(struct device *dev, struct rtc_time *tm)
127{
128 struct max8998_rtc_info *info = dev_get_drvdata(dev);
129 u8 data[8];
MyungJoo Ham337ce5d2011-01-04 14:17:39 +0900130 int ret;
Joonyoung Shim9b16c0a2010-08-06 11:28:08 +0900131
132 max8998_tm_to_data(tm, data);
133
MyungJoo Ham337ce5d2011-01-04 14:17:39 +0900134 ret = max8998_bulk_write(info->rtc, MAX8998_RTC_SEC, 8, data);
135
136 if (info->lp3974_bug_workaround)
137 msleep(2000);
138
139 return ret;
Joonyoung Shim9b16c0a2010-08-06 11:28:08 +0900140}
141
142static int max8998_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
143{
144 struct max8998_rtc_info *info = dev_get_drvdata(dev);
145 u8 data[8];
146 u8 val;
147 int ret;
148
149 ret = max8998_bulk_read(info->rtc, MAX8998_ALARM0_SEC, 8, data);
150 if (ret < 0)
151 return ret;
152
153 max8998_data_to_tm(data, &alrm->time);
154
155 ret = max8998_read_reg(info->rtc, MAX8998_ALARM0_CONF, &val);
156 if (ret < 0)
157 return ret;
158
159 alrm->enabled = !!val;
160
161 ret = max8998_read_reg(info->rtc, MAX8998_RTC_STATUS, &val);
162 if (ret < 0)
163 return ret;
164
165 if (val & ALARM0_STATUS)
166 alrm->pending = 1;
167 else
168 alrm->pending = 0;
169
170 return 0;
171}
172
173static int max8998_rtc_stop_alarm(struct max8998_rtc_info *info)
174{
MyungJoo Ham337ce5d2011-01-04 14:17:39 +0900175 int ret = max8998_write_reg(info->rtc, MAX8998_ALARM0_CONF, 0);
176
177 if (info->lp3974_bug_workaround)
178 msleep(2000);
179
180 return ret;
Joonyoung Shim9b16c0a2010-08-06 11:28:08 +0900181}
182
183static int max8998_rtc_start_alarm(struct max8998_rtc_info *info)
184{
MyungJoo Ham337ce5d2011-01-04 14:17:39 +0900185 int ret;
186 u8 alarm0_conf = 0x77;
187
188 /* LP3974 with delay bug chips has rtc alarm bugs with "MONTH" field */
189 if (info->lp3974_bug_workaround)
190 alarm0_conf = 0x57;
191
192 ret = max8998_write_reg(info->rtc, MAX8998_ALARM0_CONF, alarm0_conf);
193
194 if (info->lp3974_bug_workaround)
195 msleep(2000);
196
197 return ret;
Joonyoung Shim9b16c0a2010-08-06 11:28:08 +0900198}
199
200static int max8998_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
201{
202 struct max8998_rtc_info *info = dev_get_drvdata(dev);
203 u8 data[8];
204 int ret;
205
206 max8998_tm_to_data(&alrm->time, data);
207
208 ret = max8998_rtc_stop_alarm(info);
209 if (ret < 0)
210 return ret;
211
212 ret = max8998_bulk_write(info->rtc, MAX8998_ALARM0_SEC, 8, data);
213 if (ret < 0)
214 return ret;
215
MyungJoo Ham337ce5d2011-01-04 14:17:39 +0900216 if (info->lp3974_bug_workaround)
217 msleep(2000);
Joonyoung Shim9b16c0a2010-08-06 11:28:08 +0900218
MyungJoo Ham337ce5d2011-01-04 14:17:39 +0900219 if (alrm->enabled)
220 ret = max8998_rtc_start_alarm(info);
221
222 return ret;
Joonyoung Shim9b16c0a2010-08-06 11:28:08 +0900223}
224
225static int max8998_rtc_alarm_irq_enable(struct device *dev,
226 unsigned int enabled)
227{
228 struct max8998_rtc_info *info = dev_get_drvdata(dev);
229
230 if (enabled)
231 return max8998_rtc_start_alarm(info);
232 else
233 return max8998_rtc_stop_alarm(info);
234}
235
236static irqreturn_t max8998_rtc_alarm_irq(int irq, void *data)
237{
238 struct max8998_rtc_info *info = data;
239
240 rtc_update_irq(info->rtc_dev, 1, RTC_IRQF | RTC_AF);
241
242 return IRQ_HANDLED;
243}
244
245static const struct rtc_class_ops max8998_rtc_ops = {
246 .read_time = max8998_rtc_read_time,
247 .set_time = max8998_rtc_set_time,
248 .read_alarm = max8998_rtc_read_alarm,
249 .set_alarm = max8998_rtc_set_alarm,
250 .alarm_irq_enable = max8998_rtc_alarm_irq_enable,
251};
252
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800253static int max8998_rtc_probe(struct platform_device *pdev)
Joonyoung Shim9b16c0a2010-08-06 11:28:08 +0900254{
255 struct max8998_dev *max8998 = dev_get_drvdata(pdev->dev.parent);
Tomasz Figaee999fb2013-06-25 16:08:10 +0200256 struct max8998_platform_data *pdata = max8998->pdata;
Joonyoung Shim9b16c0a2010-08-06 11:28:08 +0900257 struct max8998_rtc_info *info;
258 int ret;
259
Jingoo Han8925e312013-04-29 16:20:43 -0700260 info = devm_kzalloc(&pdev->dev, sizeof(struct max8998_rtc_info),
261 GFP_KERNEL);
Joonyoung Shim9b16c0a2010-08-06 11:28:08 +0900262 if (!info)
263 return -ENOMEM;
264
265 info->dev = &pdev->dev;
266 info->max8998 = max8998;
267 info->rtc = max8998->rtc;
Joonyoung Shim9b16c0a2010-08-06 11:28:08 +0900268
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);
Jingoo Han4802f222013-07-03 15:06:28 -0700277 return ret;
Joonyoung Shim9b16c0a2010-08-06 11:28:08 +0900278 }
279
Tomasz Figa443c6ae2013-06-24 14:39:52 +0200280 if (!max8998->irq_domain)
281 goto no_irq;
282
283 info->irq = irq_create_mapping(max8998->irq_domain, MAX8998_IRQ_ALARM0);
284 if (!info->irq) {
285 dev_warn(&pdev->dev, "Failed to map alarm IRQ\n");
286 goto no_irq;
287 }
288
Jingoo Han8925e312013-04-29 16:20:43 -0700289 ret = devm_request_threaded_irq(&pdev->dev, info->irq, NULL,
290 max8998_rtc_alarm_irq, 0, "rtc-alarm0", info);
MyungJoo Ham337ce5d2011-01-04 14:17:39 +0900291
Joonyoung Shim9b16c0a2010-08-06 11:28:08 +0900292 if (ret < 0)
293 dev_err(&pdev->dev, "Failed to request alarm IRQ: %d: %d\n",
294 info->irq, ret);
295
Tomasz Figa443c6ae2013-06-24 14:39:52 +0200296no_irq:
MyungJoo Ham337ce5d2011-01-04 14:17:39 +0900297 dev_info(&pdev->dev, "RTC CHIP NAME: %s\n", pdev->id_entry->name);
Tomasz Figa43c523b2013-05-24 15:55:35 -0700298 if (pdata && pdata->rtc_delay) {
MyungJoo Ham337ce5d2011-01-04 14:17:39 +0900299 info->lp3974_bug_workaround = true;
300 dev_warn(&pdev->dev, "LP3974 with RTC REGERR option."
301 " RTC updates will be extremely slow.\n");
302 }
303
Joonyoung Shim9b16c0a2010-08-06 11:28:08 +0900304 return 0;
Joonyoung Shim9b16c0a2010-08-06 11:28:08 +0900305}
306
MyungJoo Ham337ce5d2011-01-04 14:17:39 +0900307static const struct platform_device_id max8998_rtc_id[] = {
308 { "max8998-rtc", TYPE_MAX8998 },
309 { "lp3974-rtc", TYPE_LP3974 },
310 { }
311};
Axel Lin8ad0f5b2011-04-18 20:16:57 +0800312MODULE_DEVICE_TABLE(platform, max8998_rtc_id);
MyungJoo Ham337ce5d2011-01-04 14:17:39 +0900313
Joonyoung Shim9b16c0a2010-08-06 11:28:08 +0900314static struct platform_driver max8998_rtc_driver = {
315 .driver = {
316 .name = "max8998-rtc",
Joonyoung Shim9b16c0a2010-08-06 11:28:08 +0900317 },
318 .probe = max8998_rtc_probe,
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");