aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/rtc
diff options
context:
space:
mode:
authorQiao Zhou <zhouqiao@marvell.com>2012-07-09 02:37:34 -0400
committerSamuel Ortiz <sameo@linux.intel.com>2012-07-09 09:17:43 -0400
commit2985c29c196418b8f666bc9240c922aa56bff599 (patch)
tree3666c1953de540654514d36fe71574d38c5e0f50 /drivers/rtc
parentaf65a361d543100962c03cc4cdb7333b14c9d119 (diff)
rtc: Add rtc support to 88PM80X PMIC
add rtc driver for MARVELL 88PM80X PMIC and enable rtc function. Cc: Alessandro Zummo <a.zummo@towertech.it> Signed-off-by: Qiao Zhou <zhouqiao@marvell.com> Signed-off-by: Samuel Ortiz <sameo@linux.intel.com>
Diffstat (limited to 'drivers/rtc')
-rw-r--r--drivers/rtc/Kconfig10
-rw-r--r--drivers/rtc/Makefile1
-rw-r--r--drivers/rtc/rtc-88pm80x.c371
3 files changed, 382 insertions, 0 deletions
diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig
index 08cbdb900a18..f049c02413ce 100644
--- a/drivers/rtc/Kconfig
+++ b/drivers/rtc/Kconfig
@@ -135,6 +135,16 @@ config RTC_DRV_88PM860X
135 This driver can also be built as a module. If so, the module 135 This driver can also be built as a module. If so, the module
136 will be called rtc-88pm860x. 136 will be called rtc-88pm860x.
137 137
138config RTC_DRV_88PM80X
139 tristate "Marvell 88PM80x"
140 depends on RTC_CLASS && I2C && MFD_88PM800
141 help
142 If you say yes here you get support for RTC function in Marvell
143 88PM80x chips.
144
145 This driver can also be built as a module. If so, the module
146 will be called rtc-88pm80x.
147
138config RTC_DRV_DS1307 148config RTC_DRV_DS1307
139 tristate "Dallas/Maxim DS1307/37/38/39/40, ST M41T00, EPSON RX-8025" 149 tristate "Dallas/Maxim DS1307/37/38/39/40, ST M41T00, EPSON RX-8025"
140 help 150 help
diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile
index 2973921c30d8..0d5b2b66f90d 100644
--- a/drivers/rtc/Makefile
+++ b/drivers/rtc/Makefile
@@ -16,6 +16,7 @@ rtc-core-$(CONFIG_RTC_INTF_SYSFS) += rtc-sysfs.o
16# Keep the list ordered. 16# Keep the list ordered.
17 17
18obj-$(CONFIG_RTC_DRV_88PM860X) += rtc-88pm860x.o 18obj-$(CONFIG_RTC_DRV_88PM860X) += rtc-88pm860x.o
19obj-$(CONFIG_RTC_DRV_88PM80X) += rtc-88pm80x.o
19obj-$(CONFIG_RTC_DRV_AB3100) += rtc-ab3100.o 20obj-$(CONFIG_RTC_DRV_AB3100) += rtc-ab3100.o
20obj-$(CONFIG_RTC_DRV_AB8500) += rtc-ab8500.o 21obj-$(CONFIG_RTC_DRV_AB8500) += rtc-ab8500.o
21obj-$(CONFIG_RTC_DRV_AT32AP700X)+= rtc-at32ap700x.o 22obj-$(CONFIG_RTC_DRV_AT32AP700X)+= rtc-at32ap700x.o
diff --git a/drivers/rtc/rtc-88pm80x.c b/drivers/rtc/rtc-88pm80x.c
new file mode 100644
index 000000000000..a2f956d90de0
--- /dev/null
+++ b/drivers/rtc/rtc-88pm80x.c
@@ -0,0 +1,371 @@
1/*
2 * Real Time Clock driver for Marvell 88PM80x PMIC
3 *
4 * Copyright (c) 2012 Marvell International Ltd.
5 * Wenzeng Chen<wzch@marvell.com>
6 * Qiao Zhou <zhouqiao@marvell.com>
7 *
8 * This file is subject to the terms and conditions of the GNU General
9 * Public License. See the file "COPYING" in the main directory of this
10 * archive for more details.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22#include <linux/kernel.h>
23#include <linux/module.h>
24#include <linux/slab.h>
25#include <linux/regmap.h>
26#include <linux/mfd/core.h>
27#include <linux/mfd/88pm80x.h>
28#include <linux/rtc.h>
29
30#define PM800_RTC_COUNTER1 (0xD1)
31#define PM800_RTC_COUNTER2 (0xD2)
32#define PM800_RTC_COUNTER3 (0xD3)
33#define PM800_RTC_COUNTER4 (0xD4)
34#define PM800_RTC_EXPIRE1_1 (0xD5)
35#define PM800_RTC_EXPIRE1_2 (0xD6)
36#define PM800_RTC_EXPIRE1_3 (0xD7)
37#define PM800_RTC_EXPIRE1_4 (0xD8)
38#define PM800_RTC_TRIM1 (0xD9)
39#define PM800_RTC_TRIM2 (0xDA)
40#define PM800_RTC_TRIM3 (0xDB)
41#define PM800_RTC_TRIM4 (0xDC)
42#define PM800_RTC_EXPIRE2_1 (0xDD)
43#define PM800_RTC_EXPIRE2_2 (0xDE)
44#define PM800_RTC_EXPIRE2_3 (0xDF)
45#define PM800_RTC_EXPIRE2_4 (0xE0)
46
47#define PM800_POWER_DOWN_LOG1 (0xE5)
48#define PM800_POWER_DOWN_LOG2 (0xE6)
49
50struct pm80x_rtc_info {
51 struct pm80x_chip *chip;
52 struct regmap *map;
53 struct rtc_device *rtc_dev;
54 struct device *dev;
55 struct delayed_work calib_work;
56
57 int irq;
58 int vrtc;
59};
60
61static irqreturn_t rtc_update_handler(int irq, void *data)
62{
63 struct pm80x_rtc_info *info = (struct pm80x_rtc_info *)data;
64 int mask;
65
66 mask = PM800_ALARM | PM800_ALARM_WAKEUP;
67 regmap_update_bits(info->map, PM800_RTC_CONTROL, mask | PM800_ALARM1_EN,
68 mask);
69 rtc_update_irq(info->rtc_dev, 1, RTC_AF);
70 return IRQ_HANDLED;
71}
72
73static int pm80x_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
74{
75 struct pm80x_rtc_info *info = dev_get_drvdata(dev);
76
77 if (enabled)
78 regmap_update_bits(info->map, PM800_RTC_CONTROL,
79 PM800_ALARM1_EN, PM800_ALARM1_EN);
80 else
81 regmap_update_bits(info->map, PM800_RTC_CONTROL,
82 PM800_ALARM1_EN, 0);
83 return 0;
84}
85
86/*
87 * Calculate the next alarm time given the requested alarm time mask
88 * and the current time.
89 */
90static void rtc_next_alarm_time(struct rtc_time *next, struct rtc_time *now,
91 struct rtc_time *alrm)
92{
93 unsigned long next_time;
94 unsigned long now_time;
95
96 next->tm_year = now->tm_year;
97 next->tm_mon = now->tm_mon;
98 next->tm_mday = now->tm_mday;
99 next->tm_hour = alrm->tm_hour;
100 next->tm_min = alrm->tm_min;
101 next->tm_sec = alrm->tm_sec;
102
103 rtc_tm_to_time(now, &now_time);
104 rtc_tm_to_time(next, &next_time);
105
106 if (next_time < now_time) {
107 /* Advance one day */
108 next_time += 60 * 60 * 24;
109 rtc_time_to_tm(next_time, next);
110 }
111}
112
113static int pm80x_rtc_read_time(struct device *dev, struct rtc_time *tm)
114{
115 struct pm80x_rtc_info *info = dev_get_drvdata(dev);
116 unsigned char buf[4];
117 unsigned long ticks, base, data;
118 regmap_raw_read(info->map, PM800_RTC_EXPIRE2_1, buf, 4);
119 base = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
120 dev_dbg(info->dev, "%x-%x-%x-%x\n", buf[0], buf[1], buf[2], buf[3]);
121
122 /* load 32-bit read-only counter */
123 regmap_raw_read(info->map, PM800_RTC_COUNTER1, buf, 4);
124 data = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
125 ticks = base + data;
126 dev_dbg(info->dev, "get base:0x%lx, RO count:0x%lx, ticks:0x%lx\n",
127 base, data, ticks);
128 rtc_time_to_tm(ticks, tm);
129 return 0;
130}
131
132static int pm80x_rtc_set_time(struct device *dev, struct rtc_time *tm)
133{
134 struct pm80x_rtc_info *info = dev_get_drvdata(dev);
135 unsigned char buf[4];
136 unsigned long ticks, base, data;
137 if ((tm->tm_year < 70) || (tm->tm_year > 138)) {
138 dev_dbg(info->dev,
139 "Set time %d out of range. Please set time between 1970 to 2038.\n",
140 1900 + tm->tm_year);
141 return -EINVAL;
142 }
143 rtc_tm_to_time(tm, &ticks);
144
145 /* load 32-bit read-only counter */
146 regmap_raw_read(info->map, PM800_RTC_COUNTER1, buf, 4);
147 data = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
148 base = ticks - data;
149 dev_dbg(info->dev, "set base:0x%lx, RO count:0x%lx, ticks:0x%lx\n",
150 base, data, ticks);
151 buf[0] = base & 0xFF;
152 buf[1] = (base >> 8) & 0xFF;
153 buf[2] = (base >> 16) & 0xFF;
154 buf[3] = (base >> 24) & 0xFF;
155 regmap_raw_write(info->map, PM800_RTC_EXPIRE2_1, buf, 4);
156
157 return 0;
158}
159
160static int pm80x_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
161{
162 struct pm80x_rtc_info *info = dev_get_drvdata(dev);
163 unsigned char buf[4];
164 unsigned long ticks, base, data;
165 int ret;
166
167 regmap_raw_read(info->map, PM800_RTC_EXPIRE2_1, buf, 4);
168 base = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
169 dev_dbg(info->dev, "%x-%x-%x-%x\n", buf[0], buf[1], buf[2], buf[3]);
170
171 regmap_raw_read(info->map, PM800_RTC_EXPIRE1_1, buf, 4);
172 data = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
173 ticks = base + data;
174 dev_dbg(info->dev, "get base:0x%lx, RO count:0x%lx, ticks:0x%lx\n",
175 base, data, ticks);
176
177 rtc_time_to_tm(ticks, &alrm->time);
178 regmap_read(info->map, PM800_RTC_CONTROL, &ret);
179 alrm->enabled = (ret & PM800_ALARM1_EN) ? 1 : 0;
180 alrm->pending = (ret & (PM800_ALARM | PM800_ALARM_WAKEUP)) ? 1 : 0;
181 return 0;
182}
183
184static int pm80x_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
185{
186 struct pm80x_rtc_info *info = dev_get_drvdata(dev);
187 struct rtc_time now_tm, alarm_tm;
188 unsigned long ticks, base, data;
189 unsigned char buf[4];
190 int mask;
191
192 regmap_update_bits(info->map, PM800_RTC_CONTROL, PM800_ALARM1_EN, 0);
193
194 regmap_raw_read(info->map, PM800_RTC_EXPIRE2_1, buf, 4);
195 base = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
196 dev_dbg(info->dev, "%x-%x-%x-%x\n", buf[0], buf[1], buf[2], buf[3]);
197
198 /* load 32-bit read-only counter */
199 regmap_raw_read(info->map, PM800_RTC_COUNTER1, buf, 4);
200 data = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
201 ticks = base + data;
202 dev_dbg(info->dev, "get base:0x%lx, RO count:0x%lx, ticks:0x%lx\n",
203 base, data, ticks);
204
205 rtc_time_to_tm(ticks, &now_tm);
206 dev_dbg(info->dev, "%s, now time : %lu\n", __func__, ticks);
207 rtc_next_alarm_time(&alarm_tm, &now_tm, &alrm->time);
208 /* get new ticks for alarm in 24 hours */
209 rtc_tm_to_time(&alarm_tm, &ticks);
210 dev_dbg(info->dev, "%s, alarm time: %lu\n", __func__, ticks);
211 data = ticks - base;
212
213 buf[0] = data & 0xff;
214 buf[1] = (data >> 8) & 0xff;
215 buf[2] = (data >> 16) & 0xff;
216 buf[3] = (data >> 24) & 0xff;
217 regmap_raw_write(info->map, PM800_RTC_EXPIRE1_1, buf, 4);
218 if (alrm->enabled) {
219 mask = PM800_ALARM | PM800_ALARM_WAKEUP | PM800_ALARM1_EN;
220 regmap_update_bits(info->map, PM800_RTC_CONTROL, mask, mask);
221 } else {
222 mask = PM800_ALARM | PM800_ALARM_WAKEUP | PM800_ALARM1_EN;
223 regmap_update_bits(info->map, PM800_RTC_CONTROL, mask,
224 PM800_ALARM | PM800_ALARM_WAKEUP);
225 }
226 return 0;
227}
228
229static const struct rtc_class_ops pm80x_rtc_ops = {
230 .read_time = pm80x_rtc_read_time,
231 .set_time = pm80x_rtc_set_time,
232 .read_alarm = pm80x_rtc_read_alarm,
233 .set_alarm = pm80x_rtc_set_alarm,
234 .alarm_irq_enable = pm80x_rtc_alarm_irq_enable,
235};
236
237#ifdef CONFIG_PM
238static int pm80x_rtc_suspend(struct device *dev)
239{
240 return pm80x_dev_suspend(dev);
241}
242
243static int pm80x_rtc_resume(struct device *dev)
244{
245 return pm80x_dev_resume(dev);
246}
247#endif
248
249static SIMPLE_DEV_PM_OPS(pm80x_rtc_pm_ops, pm80x_rtc_suspend, pm80x_rtc_resume);
250
251static int __devinit pm80x_rtc_probe(struct platform_device *pdev)
252{
253 struct pm80x_chip *chip = dev_get_drvdata(pdev->dev.parent);
254 struct pm80x_platform_data *pm80x_pdata;
255 struct pm80x_rtc_pdata *pdata = NULL;
256 struct pm80x_rtc_info *info;
257 struct rtc_time tm;
258 unsigned long ticks = 0;
259 int ret;
260
261 pdata = pdev->dev.platform_data;
262 if (pdata == NULL)
263 dev_warn(&pdev->dev, "No platform data!\n");
264
265 info =
266 devm_kzalloc(&pdev->dev, sizeof(struct pm80x_rtc_info), GFP_KERNEL);
267 if (!info)
268 return -ENOMEM;
269 info->irq = platform_get_irq(pdev, 0);
270 if (info->irq < 0) {
271 dev_err(&pdev->dev, "No IRQ resource!\n");
272 ret = -EINVAL;
273 goto out;
274 }
275
276 info->chip = chip;
277 info->map = chip->regmap;
278 if (!info->map) {
279 dev_err(&pdev->dev, "no regmap!\n");
280 ret = -EINVAL;
281 goto out;
282 }
283
284 info->dev = &pdev->dev;
285 dev_set_drvdata(&pdev->dev, info);
286
287 ret = pm80x_request_irq(chip, info->irq, rtc_update_handler,
288 IRQF_ONESHOT, "rtc", info);
289 if (ret < 0) {
290 dev_err(chip->dev, "Failed to request IRQ: #%d: %d\n",
291 info->irq, ret);
292 goto out;
293 }
294
295 ret = pm80x_rtc_read_time(&pdev->dev, &tm);
296 if (ret < 0) {
297 dev_err(&pdev->dev, "Failed to read initial time.\n");
298 goto out_rtc;
299 }
300 if ((tm.tm_year < 70) || (tm.tm_year > 138)) {
301 tm.tm_year = 70;
302 tm.tm_mon = 0;
303 tm.tm_mday = 1;
304 tm.tm_hour = 0;
305 tm.tm_min = 0;
306 tm.tm_sec = 0;
307 ret = pm80x_rtc_set_time(&pdev->dev, &tm);
308 if (ret < 0) {
309 dev_err(&pdev->dev, "Failed to set initial time.\n");
310 goto out_rtc;
311 }
312 }
313 rtc_tm_to_time(&tm, &ticks);
314
315 info->rtc_dev = rtc_device_register("88pm80x-rtc", &pdev->dev,
316 &pm80x_rtc_ops, THIS_MODULE);
317 ret = PTR_ERR(info->rtc_dev);
318 if (IS_ERR(info->rtc_dev)) {
319 dev_err(&pdev->dev, "Failed to register RTC device: %d\n", ret);
320 goto out_rtc;
321 }
322 /*
323 * enable internal XO instead of internal 3.25MHz clock since it can
324 * free running in PMIC power-down state.
325 */
326 regmap_update_bits(info->map, PM800_RTC_CONTROL, PM800_RTC1_USE_XO,
327 PM800_RTC1_USE_XO);
328
329 if (pdev->dev.parent->platform_data) {
330 pm80x_pdata = pdev->dev.parent->platform_data;
331 pdata = pm80x_pdata->rtc;
332 if (pdata)
333 info->rtc_dev->dev.platform_data = &pdata->rtc_wakeup;
334 }
335
336 device_init_wakeup(&pdev->dev, 1);
337
338 return 0;
339out_rtc:
340 pm80x_free_irq(chip, info->irq, info);
341out:
342 devm_kfree(&pdev->dev, info);
343 return ret;
344}
345
346static int __devexit pm80x_rtc_remove(struct platform_device *pdev)
347{
348 struct pm80x_rtc_info *info = platform_get_drvdata(pdev);
349 platform_set_drvdata(pdev, NULL);
350 rtc_device_unregister(info->rtc_dev);
351 pm80x_free_irq(info->chip, info->irq, info);
352 devm_kfree(&pdev->dev, info);
353 return 0;
354}
355
356static struct platform_driver pm80x_rtc_driver = {
357 .driver = {
358 .name = "88pm80x-rtc",
359 .owner = THIS_MODULE,
360 .pm = &pm80x_rtc_pm_ops,
361 },
362 .probe = pm80x_rtc_probe,
363 .remove = __devexit_p(pm80x_rtc_remove),
364};
365
366module_platform_driver(pm80x_rtc_driver);
367
368MODULE_LICENSE("GPL");
369MODULE_DESCRIPTION("Marvell 88PM80x RTC driver");
370MODULE_AUTHOR("Qiao Zhou <zhouqiao@marvell.com>");
371MODULE_ALIAS("platform:88pm80x-rtc");