blob: d99a705bec07ac28103bc1d19ce52b97ffd25385 [file] [log] [blame]
Mark Zhan2e774c72007-07-17 04:05:05 -07001/*
2 * ST M48T59 RTC driver
3 *
4 * Copyright (c) 2007 Wind River Systems, Inc.
5 *
6 * Author: Mark Zhan <rongkai.zhan@windriver.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12
13#include <linux/kernel.h>
14#include <linux/module.h>
15#include <linux/init.h>
16#include <linux/io.h>
17#include <linux/device.h>
18#include <linux/platform_device.h>
19#include <linux/rtc.h>
20#include <linux/rtc/m48t59.h>
21#include <linux/bcd.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Mark Zhan2e774c72007-07-17 04:05:05 -070023
24#ifndef NO_IRQ
25#define NO_IRQ (-1)
26#endif
27
Krzysztof Helt94fe7422008-09-03 15:12:34 -070028#define M48T59_READ(reg) (pdata->read_byte(dev, pdata->offset + reg))
29#define M48T59_WRITE(val, reg) \
30 (pdata->write_byte(dev, pdata->offset + reg, val))
Mark Zhan2e774c72007-07-17 04:05:05 -070031
32#define M48T59_SET_BITS(mask, reg) \
33 M48T59_WRITE((M48T59_READ(reg) | (mask)), (reg))
34#define M48T59_CLEAR_BITS(mask, reg) \
35 M48T59_WRITE((M48T59_READ(reg) & ~(mask)), (reg))
36
37struct m48t59_private {
38 void __iomem *ioaddr;
Mark Zhan04392082007-07-31 00:39:47 -070039 int irq;
Mark Zhan2e774c72007-07-17 04:05:05 -070040 struct rtc_device *rtc;
41 spinlock_t lock; /* serialize the NVRAM and RTC access */
42};
43
44/*
45 * This is the generic access method when the chip is memory-mapped
46 */
47static void
48m48t59_mem_writeb(struct device *dev, u32 ofs, u8 val)
49{
50 struct platform_device *pdev = to_platform_device(dev);
51 struct m48t59_private *m48t59 = platform_get_drvdata(pdev);
52
53 writeb(val, m48t59->ioaddr+ofs);
54}
55
56static u8
57m48t59_mem_readb(struct device *dev, u32 ofs)
58{
59 struct platform_device *pdev = to_platform_device(dev);
60 struct m48t59_private *m48t59 = platform_get_drvdata(pdev);
61
62 return readb(m48t59->ioaddr+ofs);
63}
64
65/*
66 * NOTE: M48T59 only uses BCD mode
67 */
68static int m48t59_rtc_read_time(struct device *dev, struct rtc_time *tm)
69{
70 struct platform_device *pdev = to_platform_device(dev);
Jingoo Han81360322013-11-12 15:10:44 -080071 struct m48t59_plat_data *pdata = dev_get_platdata(&pdev->dev);
Mark Zhan2e774c72007-07-17 04:05:05 -070072 struct m48t59_private *m48t59 = platform_get_drvdata(pdev);
73 unsigned long flags;
74 u8 val;
75
76 spin_lock_irqsave(&m48t59->lock, flags);
77 /* Issue the READ command */
78 M48T59_SET_BITS(M48T59_CNTL_READ, M48T59_CNTL);
79
Adrian Bunkfe20ba72008-10-18 20:28:41 -070080 tm->tm_year = bcd2bin(M48T59_READ(M48T59_YEAR));
Mark Zhan2e774c72007-07-17 04:05:05 -070081 /* tm_mon is 0-11 */
Adrian Bunkfe20ba72008-10-18 20:28:41 -070082 tm->tm_mon = bcd2bin(M48T59_READ(M48T59_MONTH)) - 1;
83 tm->tm_mday = bcd2bin(M48T59_READ(M48T59_MDAY));
Mark Zhan2e774c72007-07-17 04:05:05 -070084
85 val = M48T59_READ(M48T59_WDAY);
Robert Reif833be4e2008-09-09 19:38:17 -070086 if ((pdata->type == M48T59RTC_TYPE_M48T59) &&
87 (val & M48T59_WDAY_CEB) && (val & M48T59_WDAY_CB)) {
Mark Zhan2e774c72007-07-17 04:05:05 -070088 dev_dbg(dev, "Century bit is enabled\n");
89 tm->tm_year += 100; /* one century */
90 }
Krzysztof Helt12a9ee32008-10-29 15:35:24 -070091#ifdef CONFIG_SPARC
92 /* Sun SPARC machines count years since 1968 */
93 tm->tm_year += 68;
94#endif
Mark Zhan2e774c72007-07-17 04:05:05 -070095
Adrian Bunkfe20ba72008-10-18 20:28:41 -070096 tm->tm_wday = bcd2bin(val & 0x07);
97 tm->tm_hour = bcd2bin(M48T59_READ(M48T59_HOUR) & 0x3F);
98 tm->tm_min = bcd2bin(M48T59_READ(M48T59_MIN) & 0x7F);
99 tm->tm_sec = bcd2bin(M48T59_READ(M48T59_SEC) & 0x7F);
Mark Zhan2e774c72007-07-17 04:05:05 -0700100
101 /* Clear the READ bit */
102 M48T59_CLEAR_BITS(M48T59_CNTL_READ, M48T59_CNTL);
103 spin_unlock_irqrestore(&m48t59->lock, flags);
104
105 dev_dbg(dev, "RTC read time %04d-%02d-%02d %02d/%02d/%02d\n",
106 tm->tm_year + 1900, tm->tm_mon, tm->tm_mday,
107 tm->tm_hour, tm->tm_min, tm->tm_sec);
Wan ZongShuncaf1e102010-08-10 18:02:17 -0700108 return rtc_valid_tm(tm);
Mark Zhan2e774c72007-07-17 04:05:05 -0700109}
110
111static int m48t59_rtc_set_time(struct device *dev, struct rtc_time *tm)
112{
113 struct platform_device *pdev = to_platform_device(dev);
Jingoo Han81360322013-11-12 15:10:44 -0800114 struct m48t59_plat_data *pdata = dev_get_platdata(&pdev->dev);
Mark Zhan2e774c72007-07-17 04:05:05 -0700115 struct m48t59_private *m48t59 = platform_get_drvdata(pdev);
116 unsigned long flags;
117 u8 val = 0;
Krzysztof Helt12a9ee32008-10-29 15:35:24 -0700118 int year = tm->tm_year;
119
120#ifdef CONFIG_SPARC
121 /* Sun SPARC machines count years since 1968 */
122 year -= 68;
123#endif
Mark Zhan2e774c72007-07-17 04:05:05 -0700124
125 dev_dbg(dev, "RTC set time %04d-%02d-%02d %02d/%02d/%02d\n",
Krzysztof Helt12a9ee32008-10-29 15:35:24 -0700126 year + 1900, tm->tm_mon, tm->tm_mday,
Mark Zhan2e774c72007-07-17 04:05:05 -0700127 tm->tm_hour, tm->tm_min, tm->tm_sec);
128
Krzysztof Helt12a9ee32008-10-29 15:35:24 -0700129 if (year < 0)
130 return -EINVAL;
131
Mark Zhan2e774c72007-07-17 04:05:05 -0700132 spin_lock_irqsave(&m48t59->lock, flags);
133 /* Issue the WRITE command */
134 M48T59_SET_BITS(M48T59_CNTL_WRITE, M48T59_CNTL);
135
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700136 M48T59_WRITE((bin2bcd(tm->tm_sec) & 0x7F), M48T59_SEC);
137 M48T59_WRITE((bin2bcd(tm->tm_min) & 0x7F), M48T59_MIN);
138 M48T59_WRITE((bin2bcd(tm->tm_hour) & 0x3F), M48T59_HOUR);
139 M48T59_WRITE((bin2bcd(tm->tm_mday) & 0x3F), M48T59_MDAY);
Mark Zhan2e774c72007-07-17 04:05:05 -0700140 /* tm_mon is 0-11 */
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700141 M48T59_WRITE((bin2bcd(tm->tm_mon + 1) & 0x1F), M48T59_MONTH);
Krzysztof Helt12a9ee32008-10-29 15:35:24 -0700142 M48T59_WRITE(bin2bcd(year % 100), M48T59_YEAR);
Mark Zhan2e774c72007-07-17 04:05:05 -0700143
Krzysztof Helt12a9ee32008-10-29 15:35:24 -0700144 if (pdata->type == M48T59RTC_TYPE_M48T59 && (year / 100))
Mark Zhan2e774c72007-07-17 04:05:05 -0700145 val = (M48T59_WDAY_CEB | M48T59_WDAY_CB);
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700146 val |= (bin2bcd(tm->tm_wday) & 0x07);
Mark Zhan2e774c72007-07-17 04:05:05 -0700147 M48T59_WRITE(val, M48T59_WDAY);
148
149 /* Clear the WRITE bit */
150 M48T59_CLEAR_BITS(M48T59_CNTL_WRITE, M48T59_CNTL);
151 spin_unlock_irqrestore(&m48t59->lock, flags);
152 return 0;
153}
154
155/*
156 * Read alarm time and date in RTC
157 */
158static int m48t59_rtc_readalarm(struct device *dev, struct rtc_wkalrm *alrm)
159{
160 struct platform_device *pdev = to_platform_device(dev);
Jingoo Han81360322013-11-12 15:10:44 -0800161 struct m48t59_plat_data *pdata = dev_get_platdata(&pdev->dev);
Mark Zhan2e774c72007-07-17 04:05:05 -0700162 struct m48t59_private *m48t59 = platform_get_drvdata(pdev);
163 struct rtc_time *tm = &alrm->time;
164 unsigned long flags;
165 u8 val;
166
167 /* If no irq, we don't support ALARM */
168 if (m48t59->irq == NO_IRQ)
169 return -EIO;
170
171 spin_lock_irqsave(&m48t59->lock, flags);
172 /* Issue the READ command */
173 M48T59_SET_BITS(M48T59_CNTL_READ, M48T59_CNTL);
174
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700175 tm->tm_year = bcd2bin(M48T59_READ(M48T59_YEAR));
Krzysztof Helt12a9ee32008-10-29 15:35:24 -0700176#ifdef CONFIG_SPARC
177 /* Sun SPARC machines count years since 1968 */
178 tm->tm_year += 68;
179#endif
Mark Zhan2e774c72007-07-17 04:05:05 -0700180 /* tm_mon is 0-11 */
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700181 tm->tm_mon = bcd2bin(M48T59_READ(M48T59_MONTH)) - 1;
Mark Zhan2e774c72007-07-17 04:05:05 -0700182
183 val = M48T59_READ(M48T59_WDAY);
184 if ((val & M48T59_WDAY_CEB) && (val & M48T59_WDAY_CB))
185 tm->tm_year += 100; /* one century */
186
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700187 tm->tm_mday = bcd2bin(M48T59_READ(M48T59_ALARM_DATE));
188 tm->tm_hour = bcd2bin(M48T59_READ(M48T59_ALARM_HOUR));
189 tm->tm_min = bcd2bin(M48T59_READ(M48T59_ALARM_MIN));
190 tm->tm_sec = bcd2bin(M48T59_READ(M48T59_ALARM_SEC));
Mark Zhan2e774c72007-07-17 04:05:05 -0700191
192 /* Clear the READ bit */
193 M48T59_CLEAR_BITS(M48T59_CNTL_READ, M48T59_CNTL);
194 spin_unlock_irqrestore(&m48t59->lock, flags);
195
196 dev_dbg(dev, "RTC read alarm time %04d-%02d-%02d %02d/%02d/%02d\n",
197 tm->tm_year + 1900, tm->tm_mon, tm->tm_mday,
198 tm->tm_hour, tm->tm_min, tm->tm_sec);
Wan ZongShuncaf1e102010-08-10 18:02:17 -0700199 return rtc_valid_tm(tm);
Mark Zhan2e774c72007-07-17 04:05:05 -0700200}
201
202/*
203 * Set alarm time and date in RTC
204 */
205static int m48t59_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)
206{
207 struct platform_device *pdev = to_platform_device(dev);
Jingoo Han81360322013-11-12 15:10:44 -0800208 struct m48t59_plat_data *pdata = dev_get_platdata(&pdev->dev);
Mark Zhan2e774c72007-07-17 04:05:05 -0700209 struct m48t59_private *m48t59 = platform_get_drvdata(pdev);
210 struct rtc_time *tm = &alrm->time;
211 u8 mday, hour, min, sec;
212 unsigned long flags;
Krzysztof Helt12a9ee32008-10-29 15:35:24 -0700213 int year = tm->tm_year;
214
215#ifdef CONFIG_SPARC
216 /* Sun SPARC machines count years since 1968 */
217 year -= 68;
218#endif
Mark Zhan2e774c72007-07-17 04:05:05 -0700219
220 /* If no irq, we don't support ALARM */
221 if (m48t59->irq == NO_IRQ)
222 return -EIO;
223
Krzysztof Helt12a9ee32008-10-29 15:35:24 -0700224 if (year < 0)
225 return -EINVAL;
226
Mark Zhan2e774c72007-07-17 04:05:05 -0700227 /*
228 * 0xff means "always match"
229 */
230 mday = tm->tm_mday;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700231 mday = (mday >= 1 && mday <= 31) ? bin2bcd(mday) : 0xff;
Mark Zhan2e774c72007-07-17 04:05:05 -0700232 if (mday == 0xff)
233 mday = M48T59_READ(M48T59_MDAY);
234
235 hour = tm->tm_hour;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700236 hour = (hour < 24) ? bin2bcd(hour) : 0x00;
Mark Zhan2e774c72007-07-17 04:05:05 -0700237
238 min = tm->tm_min;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700239 min = (min < 60) ? bin2bcd(min) : 0x00;
Mark Zhan2e774c72007-07-17 04:05:05 -0700240
241 sec = tm->tm_sec;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700242 sec = (sec < 60) ? bin2bcd(sec) : 0x00;
Mark Zhan2e774c72007-07-17 04:05:05 -0700243
244 spin_lock_irqsave(&m48t59->lock, flags);
245 /* Issue the WRITE command */
246 M48T59_SET_BITS(M48T59_CNTL_WRITE, M48T59_CNTL);
247
248 M48T59_WRITE(mday, M48T59_ALARM_DATE);
249 M48T59_WRITE(hour, M48T59_ALARM_HOUR);
250 M48T59_WRITE(min, M48T59_ALARM_MIN);
251 M48T59_WRITE(sec, M48T59_ALARM_SEC);
252
253 /* Clear the WRITE bit */
254 M48T59_CLEAR_BITS(M48T59_CNTL_WRITE, M48T59_CNTL);
255 spin_unlock_irqrestore(&m48t59->lock, flags);
256
257 dev_dbg(dev, "RTC set alarm time %04d-%02d-%02d %02d/%02d/%02d\n",
Krzysztof Helt12a9ee32008-10-29 15:35:24 -0700258 year + 1900, tm->tm_mon, tm->tm_mday,
Mark Zhan2e774c72007-07-17 04:05:05 -0700259 tm->tm_hour, tm->tm_min, tm->tm_sec);
260 return 0;
261}
262
263/*
264 * Handle commands from user-space
265 */
John Stultz16380c12011-02-02 17:02:41 -0800266static int m48t59_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
Mark Zhan2e774c72007-07-17 04:05:05 -0700267{
268 struct platform_device *pdev = to_platform_device(dev);
Jingoo Han81360322013-11-12 15:10:44 -0800269 struct m48t59_plat_data *pdata = dev_get_platdata(&pdev->dev);
Mark Zhan2e774c72007-07-17 04:05:05 -0700270 struct m48t59_private *m48t59 = platform_get_drvdata(pdev);
271 unsigned long flags;
Mark Zhan2e774c72007-07-17 04:05:05 -0700272
273 spin_lock_irqsave(&m48t59->lock, flags);
John Stultz16380c12011-02-02 17:02:41 -0800274 if (enabled)
Mark Zhan2e774c72007-07-17 04:05:05 -0700275 M48T59_WRITE(M48T59_INTR_AFE, M48T59_INTR);
John Stultz16380c12011-02-02 17:02:41 -0800276 else
277 M48T59_WRITE(0x00, M48T59_INTR);
Mark Zhan2e774c72007-07-17 04:05:05 -0700278 spin_unlock_irqrestore(&m48t59->lock, flags);
279
John Stultz16380c12011-02-02 17:02:41 -0800280 return 0;
Mark Zhan2e774c72007-07-17 04:05:05 -0700281}
282
283static int m48t59_rtc_proc(struct device *dev, struct seq_file *seq)
284{
285 struct platform_device *pdev = to_platform_device(dev);
Jingoo Han81360322013-11-12 15:10:44 -0800286 struct m48t59_plat_data *pdata = dev_get_platdata(&pdev->dev);
Mark Zhan2e774c72007-07-17 04:05:05 -0700287 struct m48t59_private *m48t59 = platform_get_drvdata(pdev);
288 unsigned long flags;
289 u8 val;
290
291 spin_lock_irqsave(&m48t59->lock, flags);
292 val = M48T59_READ(M48T59_FLAGS);
293 spin_unlock_irqrestore(&m48t59->lock, flags);
294
295 seq_printf(seq, "battery\t\t: %s\n",
296 (val & M48T59_FLAGS_BF) ? "low" : "normal");
297 return 0;
298}
299
300/*
301 * IRQ handler for the RTC
302 */
303static irqreturn_t m48t59_rtc_interrupt(int irq, void *dev_id)
304{
305 struct device *dev = (struct device *)dev_id;
306 struct platform_device *pdev = to_platform_device(dev);
Jingoo Han81360322013-11-12 15:10:44 -0800307 struct m48t59_plat_data *pdata = dev_get_platdata(&pdev->dev);
Mark Zhan2e774c72007-07-17 04:05:05 -0700308 struct m48t59_private *m48t59 = platform_get_drvdata(pdev);
309 u8 event;
310
311 spin_lock(&m48t59->lock);
312 event = M48T59_READ(M48T59_FLAGS);
313 spin_unlock(&m48t59->lock);
314
315 if (event & M48T59_FLAGS_AF) {
316 rtc_update_irq(m48t59->rtc, 1, (RTC_AF | RTC_IRQF));
317 return IRQ_HANDLED;
318 }
319
320 return IRQ_NONE;
321}
322
323static const struct rtc_class_ops m48t59_rtc_ops = {
Mark Zhan2e774c72007-07-17 04:05:05 -0700324 .read_time = m48t59_rtc_read_time,
325 .set_time = m48t59_rtc_set_time,
326 .read_alarm = m48t59_rtc_readalarm,
327 .set_alarm = m48t59_rtc_setalarm,
328 .proc = m48t59_rtc_proc,
John Stultz16380c12011-02-02 17:02:41 -0800329 .alarm_irq_enable = m48t59_rtc_alarm_irq_enable,
Mark Zhan2e774c72007-07-17 04:05:05 -0700330};
331
Krzysztof Helt94fe7422008-09-03 15:12:34 -0700332static const struct rtc_class_ops m48t02_rtc_ops = {
333 .read_time = m48t59_rtc_read_time,
334 .set_time = m48t59_rtc_set_time,
335};
336
Chris Wright2c3c8be2010-05-12 18:28:57 -0700337static ssize_t m48t59_nvram_read(struct file *filp, struct kobject *kobj,
Mark Zhan2e774c72007-07-17 04:05:05 -0700338 struct bin_attribute *bin_attr,
339 char *buf, loff_t pos, size_t size)
340{
341 struct device *dev = container_of(kobj, struct device, kobj);
342 struct platform_device *pdev = to_platform_device(dev);
Jingoo Han81360322013-11-12 15:10:44 -0800343 struct m48t59_plat_data *pdata = dev_get_platdata(&pdev->dev);
Mark Zhan2e774c72007-07-17 04:05:05 -0700344 struct m48t59_private *m48t59 = platform_get_drvdata(pdev);
345 ssize_t cnt = 0;
346 unsigned long flags;
347
Vladimir Zapolskiy99be3e32015-08-05 21:13:02 +0300348 spin_lock_irqsave(&m48t59->lock, flags);
349
350 for (; cnt < size; cnt++)
Mark Zhan2e774c72007-07-17 04:05:05 -0700351 *buf++ = M48T59_READ(cnt);
Vladimir Zapolskiy99be3e32015-08-05 21:13:02 +0300352
353 spin_unlock_irqrestore(&m48t59->lock, flags);
Mark Zhan2e774c72007-07-17 04:05:05 -0700354
355 return cnt;
356}
357
Chris Wright2c3c8be2010-05-12 18:28:57 -0700358static ssize_t m48t59_nvram_write(struct file *filp, struct kobject *kobj,
Mark Zhan2e774c72007-07-17 04:05:05 -0700359 struct bin_attribute *bin_attr,
360 char *buf, loff_t pos, size_t size)
361{
362 struct device *dev = container_of(kobj, struct device, kobj);
363 struct platform_device *pdev = to_platform_device(dev);
Jingoo Han81360322013-11-12 15:10:44 -0800364 struct m48t59_plat_data *pdata = dev_get_platdata(&pdev->dev);
Mark Zhan2e774c72007-07-17 04:05:05 -0700365 struct m48t59_private *m48t59 = platform_get_drvdata(pdev);
366 ssize_t cnt = 0;
367 unsigned long flags;
368
Vladimir Zapolskiy99be3e32015-08-05 21:13:02 +0300369 spin_lock_irqsave(&m48t59->lock, flags);
370
371 for (; cnt < size; cnt++)
Mark Zhan2e774c72007-07-17 04:05:05 -0700372 M48T59_WRITE(*buf++, cnt);
Vladimir Zapolskiy99be3e32015-08-05 21:13:02 +0300373
374 spin_unlock_irqrestore(&m48t59->lock, flags);
Mark Zhan2e774c72007-07-17 04:05:05 -0700375
376 return cnt;
377}
378
379static struct bin_attribute m48t59_nvram_attr = {
380 .attr = {
381 .name = "nvram",
David Brownella4b1d502007-11-14 16:58:30 -0800382 .mode = S_IRUGO | S_IWUSR,
Mark Zhan2e774c72007-07-17 04:05:05 -0700383 },
384 .read = m48t59_nvram_read,
385 .write = m48t59_nvram_write,
386};
387
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800388static int m48t59_rtc_probe(struct platform_device *pdev)
Mark Zhan2e774c72007-07-17 04:05:05 -0700389{
Jingoo Han81360322013-11-12 15:10:44 -0800390 struct m48t59_plat_data *pdata = dev_get_platdata(&pdev->dev);
Mark Zhan2e774c72007-07-17 04:05:05 -0700391 struct m48t59_private *m48t59 = NULL;
392 struct resource *res;
393 int ret = -ENOMEM;
Krzysztof Helt94fe7422008-09-03 15:12:34 -0700394 char *name;
395 const struct rtc_class_ops *ops;
Mark Zhan2e774c72007-07-17 04:05:05 -0700396
397 /* This chip could be memory-mapped or I/O-mapped */
398 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
399 if (!res) {
400 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
401 if (!res)
402 return -EINVAL;
403 }
404
405 if (res->flags & IORESOURCE_IO) {
406 /* If we are I/O-mapped, the platform should provide
407 * the operations accessing chip registers.
408 */
409 if (!pdata || !pdata->write_byte || !pdata->read_byte)
410 return -EINVAL;
411 } else if (res->flags & IORESOURCE_MEM) {
412 /* we are memory-mapped */
413 if (!pdata) {
Jingoo Han19b8d882013-07-03 15:07:08 -0700414 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata),
415 GFP_KERNEL);
Mark Zhan2e774c72007-07-17 04:05:05 -0700416 if (!pdata)
417 return -ENOMEM;
418 /* Ensure we only kmalloc platform data once */
419 pdev->dev.platform_data = pdata;
420 }
Krzysztof Helt94fe7422008-09-03 15:12:34 -0700421 if (!pdata->type)
422 pdata->type = M48T59RTC_TYPE_M48T59;
Mark Zhan2e774c72007-07-17 04:05:05 -0700423
424 /* Try to use the generic memory read/write ops */
425 if (!pdata->write_byte)
426 pdata->write_byte = m48t59_mem_writeb;
427 if (!pdata->read_byte)
428 pdata->read_byte = m48t59_mem_readb;
429 }
430
Jingoo Han19b8d882013-07-03 15:07:08 -0700431 m48t59 = devm_kzalloc(&pdev->dev, sizeof(*m48t59), GFP_KERNEL);
Mark Zhan2e774c72007-07-17 04:05:05 -0700432 if (!m48t59)
433 return -ENOMEM;
434
Krzysztof Helt64151ad2008-09-03 15:41:57 -0700435 m48t59->ioaddr = pdata->ioaddr;
436
437 if (!m48t59->ioaddr) {
438 /* ioaddr not mapped externally */
Jingoo Han19b8d882013-07-03 15:07:08 -0700439 m48t59->ioaddr = devm_ioremap(&pdev->dev, res->start,
440 resource_size(res));
Krzysztof Helt64151ad2008-09-03 15:41:57 -0700441 if (!m48t59->ioaddr)
Jingoo Han19b8d882013-07-03 15:07:08 -0700442 return ret;
Krzysztof Helt64151ad2008-09-03 15:41:57 -0700443 }
Mark Zhan2e774c72007-07-17 04:05:05 -0700444
445 /* Try to get irq number. We also can work in
446 * the mode without IRQ.
447 */
448 m48t59->irq = platform_get_irq(pdev, 0);
Anton Vorontsov2fac6672009-01-06 14:42:11 -0800449 if (m48t59->irq <= 0)
Mark Zhan2e774c72007-07-17 04:05:05 -0700450 m48t59->irq = NO_IRQ;
451
452 if (m48t59->irq != NO_IRQ) {
Jingoo Han19b8d882013-07-03 15:07:08 -0700453 ret = devm_request_irq(&pdev->dev, m48t59->irq,
454 m48t59_rtc_interrupt, IRQF_SHARED,
455 "rtc-m48t59", &pdev->dev);
Mark Zhan2e774c72007-07-17 04:05:05 -0700456 if (ret)
Jingoo Han19b8d882013-07-03 15:07:08 -0700457 return ret;
Mark Zhan2e774c72007-07-17 04:05:05 -0700458 }
Krzysztof Helt94fe7422008-09-03 15:12:34 -0700459 switch (pdata->type) {
460 case M48T59RTC_TYPE_M48T59:
461 name = "m48t59";
462 ops = &m48t59_rtc_ops;
463 pdata->offset = 0x1ff0;
464 break;
465 case M48T59RTC_TYPE_M48T02:
466 name = "m48t02";
467 ops = &m48t02_rtc_ops;
468 pdata->offset = 0x7f0;
469 break;
470 case M48T59RTC_TYPE_M48T08:
471 name = "m48t08";
472 ops = &m48t02_rtc_ops;
473 pdata->offset = 0x1ff0;
474 break;
475 default:
476 dev_err(&pdev->dev, "Unknown RTC type\n");
Jingoo Han19b8d882013-07-03 15:07:08 -0700477 return -ENODEV;
Krzysztof Helt94fe7422008-09-03 15:12:34 -0700478 }
Mark Zhan2e774c72007-07-17 04:05:05 -0700479
Alessandro Zummob74d2ca2009-12-15 16:45:53 -0800480 spin_lock_init(&m48t59->lock);
481 platform_set_drvdata(pdev, m48t59);
482
Jingoo Han19b8d882013-07-03 15:07:08 -0700483 m48t59->rtc = devm_rtc_device_register(&pdev->dev, name, ops,
484 THIS_MODULE);
485 if (IS_ERR(m48t59->rtc))
486 return PTR_ERR(m48t59->rtc);
Mark Zhan2e774c72007-07-17 04:05:05 -0700487
Krzysztof Helt94fe7422008-09-03 15:12:34 -0700488 m48t59_nvram_attr.size = pdata->offset;
489
Mark Zhan2e774c72007-07-17 04:05:05 -0700490 ret = sysfs_create_bin_file(&pdev->dev.kobj, &m48t59_nvram_attr);
Jingoo Han19b8d882013-07-03 15:07:08 -0700491 if (ret)
492 return ret;
Mark Zhan2e774c72007-07-17 04:05:05 -0700493
Mark Zhan2e774c72007-07-17 04:05:05 -0700494 return 0;
Mark Zhan2e774c72007-07-17 04:05:05 -0700495}
496
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800497static int m48t59_rtc_remove(struct platform_device *pdev)
Mark Zhan2e774c72007-07-17 04:05:05 -0700498{
Mark Zhan2e774c72007-07-17 04:05:05 -0700499 sysfs_remove_bin_file(&pdev->dev.kobj, &m48t59_nvram_attr);
Mark Zhan2e774c72007-07-17 04:05:05 -0700500 return 0;
501}
502
Kay Sieversad28a072008-04-10 21:29:25 -0700503/* work with hotplug and coldplug */
504MODULE_ALIAS("platform:rtc-m48t59");
505
Randy Dunlap12730922007-11-05 14:51:00 -0800506static struct platform_driver m48t59_rtc_driver = {
Mark Zhan2e774c72007-07-17 04:05:05 -0700507 .driver = {
508 .name = "rtc-m48t59",
Mark Zhan2e774c72007-07-17 04:05:05 -0700509 },
510 .probe = m48t59_rtc_probe,
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800511 .remove = m48t59_rtc_remove,
Mark Zhan2e774c72007-07-17 04:05:05 -0700512};
513
Axel Lin0c4eae62012-01-10 15:10:48 -0800514module_platform_driver(m48t59_rtc_driver);
Mark Zhan2e774c72007-07-17 04:05:05 -0700515
516MODULE_AUTHOR("Mark Zhan <rongkai.zhan@windriver.com>");
Krzysztof Helt94fe7422008-09-03 15:12:34 -0700517MODULE_DESCRIPTION("M48T59/M48T02/M48T08 RTC driver");
Mark Zhan2e774c72007-07-17 04:05:05 -0700518MODULE_LICENSE("GPL");