2 * TX4939 internal RTC driver
3 * Based on RBTX49xx patch from CELF patch archive.
5 * This file is subject to the terms and conditions of the GNU General Public
6 * License. See the file "COPYING" in the main directory of this archive
9 * (C) Copyright TOSHIBA CORPORATION 2005-2007
11 #include <linux/rtc.h>
12 #include <linux/platform_device.h>
13 #include <linux/interrupt.h>
15 #include <linux/gfp.h>
16 #include <asm/txx9/tx4939.h>
18 struct tx4939rtc_plat_data
{
19 struct rtc_device
*rtc
;
20 struct tx4939_rtc_reg __iomem
*rtcreg
;
24 static struct tx4939rtc_plat_data
*get_tx4939rtc_plat_data(struct device
*dev
)
26 return platform_get_drvdata(to_platform_device(dev
));
29 static int tx4939_rtc_cmd(struct tx4939_rtc_reg __iomem
*rtcreg
, int cmd
)
33 __raw_writel(cmd
, &rtcreg
->ctl
);
34 /* This might take 30us (next 32.768KHz clock) */
35 while (__raw_readl(&rtcreg
->ctl
) & TX4939_RTCCTL_BUSY
) {
36 /* timeout on approx. 100us (@ GBUS200MHz) */
44 static int tx4939_rtc_set_mmss(struct device
*dev
, unsigned long secs
)
46 struct tx4939rtc_plat_data
*pdata
= get_tx4939rtc_plat_data(dev
);
47 struct tx4939_rtc_reg __iomem
*rtcreg
= pdata
->rtcreg
;
57 spin_lock_irq(&pdata
->lock
);
58 __raw_writel(0, &rtcreg
->adr
);
59 for (i
= 0; i
< 6; i
++)
60 __raw_writel(buf
[i
], &rtcreg
->dat
);
61 ret
= tx4939_rtc_cmd(rtcreg
,
62 TX4939_RTCCTL_COMMAND_SETTIME
|
63 (__raw_readl(&rtcreg
->ctl
) & TX4939_RTCCTL_ALME
));
64 spin_unlock_irq(&pdata
->lock
);
68 static int tx4939_rtc_read_time(struct device
*dev
, struct rtc_time
*tm
)
70 struct tx4939rtc_plat_data
*pdata
= get_tx4939rtc_plat_data(dev
);
71 struct tx4939_rtc_reg __iomem
*rtcreg
= pdata
->rtcreg
;
76 spin_lock_irq(&pdata
->lock
);
77 ret
= tx4939_rtc_cmd(rtcreg
,
78 TX4939_RTCCTL_COMMAND_GETTIME
|
79 (__raw_readl(&rtcreg
->ctl
) & TX4939_RTCCTL_ALME
));
81 spin_unlock_irq(&pdata
->lock
);
84 __raw_writel(2, &rtcreg
->adr
);
85 for (i
= 2; i
< 6; i
++)
86 buf
[i
] = __raw_readl(&rtcreg
->dat
);
87 spin_unlock_irq(&pdata
->lock
);
88 sec
= (buf
[5] << 24) | (buf
[4] << 16) | (buf
[3] << 8) | buf
[2];
89 rtc_time_to_tm(sec
, tm
);
90 return rtc_valid_tm(tm
);
93 static int tx4939_rtc_set_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
95 struct tx4939rtc_plat_data
*pdata
= get_tx4939rtc_plat_data(dev
);
96 struct tx4939_rtc_reg __iomem
*rtcreg
= pdata
->rtcreg
;
101 if (alrm
->time
.tm_sec
< 0 ||
102 alrm
->time
.tm_min
< 0 ||
103 alrm
->time
.tm_hour
< 0 ||
104 alrm
->time
.tm_mday
< 0 ||
105 alrm
->time
.tm_mon
< 0 ||
106 alrm
->time
.tm_year
< 0)
108 rtc_tm_to_time(&alrm
->time
, &sec
);
115 spin_lock_irq(&pdata
->lock
);
116 __raw_writel(0, &rtcreg
->adr
);
117 for (i
= 0; i
< 6; i
++)
118 __raw_writel(buf
[i
], &rtcreg
->dat
);
119 ret
= tx4939_rtc_cmd(rtcreg
, TX4939_RTCCTL_COMMAND_SETALARM
|
120 (alrm
->enabled
? TX4939_RTCCTL_ALME
: 0));
121 spin_unlock_irq(&pdata
->lock
);
125 static int tx4939_rtc_read_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
127 struct tx4939rtc_plat_data
*pdata
= get_tx4939rtc_plat_data(dev
);
128 struct tx4939_rtc_reg __iomem
*rtcreg
= pdata
->rtcreg
;
131 unsigned char buf
[6];
134 spin_lock_irq(&pdata
->lock
);
135 ret
= tx4939_rtc_cmd(rtcreg
,
136 TX4939_RTCCTL_COMMAND_GETALARM
|
137 (__raw_readl(&rtcreg
->ctl
) & TX4939_RTCCTL_ALME
));
139 spin_unlock_irq(&pdata
->lock
);
142 __raw_writel(2, &rtcreg
->adr
);
143 for (i
= 2; i
< 6; i
++)
144 buf
[i
] = __raw_readl(&rtcreg
->dat
);
145 ctl
= __raw_readl(&rtcreg
->ctl
);
146 alrm
->enabled
= (ctl
& TX4939_RTCCTL_ALME
) ? 1 : 0;
147 alrm
->pending
= (ctl
& TX4939_RTCCTL_ALMD
) ? 1 : 0;
148 spin_unlock_irq(&pdata
->lock
);
149 sec
= (buf
[5] << 24) | (buf
[4] << 16) | (buf
[3] << 8) | buf
[2];
150 rtc_time_to_tm(sec
, &alrm
->time
);
151 return rtc_valid_tm(&alrm
->time
);
154 static int tx4939_rtc_alarm_irq_enable(struct device
*dev
, unsigned int enabled
)
156 struct tx4939rtc_plat_data
*pdata
= get_tx4939rtc_plat_data(dev
);
158 spin_lock_irq(&pdata
->lock
);
159 tx4939_rtc_cmd(pdata
->rtcreg
,
160 TX4939_RTCCTL_COMMAND_NOP
|
161 (enabled
? TX4939_RTCCTL_ALME
: 0));
162 spin_unlock_irq(&pdata
->lock
);
166 static irqreturn_t
tx4939_rtc_interrupt(int irq
, void *dev_id
)
168 struct tx4939rtc_plat_data
*pdata
= get_tx4939rtc_plat_data(dev_id
);
169 struct tx4939_rtc_reg __iomem
*rtcreg
= pdata
->rtcreg
;
170 unsigned long events
= RTC_IRQF
;
172 spin_lock(&pdata
->lock
);
173 if (__raw_readl(&rtcreg
->ctl
) & TX4939_RTCCTL_ALMD
) {
175 tx4939_rtc_cmd(rtcreg
, TX4939_RTCCTL_COMMAND_NOP
);
177 spin_unlock(&pdata
->lock
);
178 if (likely(pdata
->rtc
))
179 rtc_update_irq(pdata
->rtc
, 1, events
);
183 static const struct rtc_class_ops tx4939_rtc_ops
= {
184 .read_time
= tx4939_rtc_read_time
,
185 .read_alarm
= tx4939_rtc_read_alarm
,
186 .set_alarm
= tx4939_rtc_set_alarm
,
187 .set_mmss
= tx4939_rtc_set_mmss
,
188 .alarm_irq_enable
= tx4939_rtc_alarm_irq_enable
,
191 static ssize_t
tx4939_rtc_nvram_read(struct file
*filp
, struct kobject
*kobj
,
192 struct bin_attribute
*bin_attr
,
193 char *buf
, loff_t pos
, size_t size
)
195 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
196 struct tx4939rtc_plat_data
*pdata
= get_tx4939rtc_plat_data(dev
);
197 struct tx4939_rtc_reg __iomem
*rtcreg
= pdata
->rtcreg
;
200 spin_lock_irq(&pdata
->lock
);
201 for (count
= 0; size
> 0 && pos
< TX4939_RTC_REG_RAMSIZE
;
203 __raw_writel(pos
++, &rtcreg
->adr
);
204 *buf
++ = __raw_readl(&rtcreg
->dat
);
206 spin_unlock_irq(&pdata
->lock
);
210 static ssize_t
tx4939_rtc_nvram_write(struct file
*filp
, struct kobject
*kobj
,
211 struct bin_attribute
*bin_attr
,
212 char *buf
, loff_t pos
, size_t size
)
214 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
215 struct tx4939rtc_plat_data
*pdata
= get_tx4939rtc_plat_data(dev
);
216 struct tx4939_rtc_reg __iomem
*rtcreg
= pdata
->rtcreg
;
219 spin_lock_irq(&pdata
->lock
);
220 for (count
= 0; size
> 0 && pos
< TX4939_RTC_REG_RAMSIZE
;
222 __raw_writel(pos
++, &rtcreg
->adr
);
223 __raw_writel(*buf
++, &rtcreg
->dat
);
225 spin_unlock_irq(&pdata
->lock
);
229 static struct bin_attribute tx4939_rtc_nvram_attr
= {
232 .mode
= S_IRUGO
| S_IWUSR
,
234 .size
= TX4939_RTC_REG_RAMSIZE
,
235 .read
= tx4939_rtc_nvram_read
,
236 .write
= tx4939_rtc_nvram_write
,
239 static int __init
tx4939_rtc_probe(struct platform_device
*pdev
)
241 struct rtc_device
*rtc
;
242 struct tx4939rtc_plat_data
*pdata
;
243 struct resource
*res
;
246 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
249 irq
= platform_get_irq(pdev
, 0);
252 pdata
= devm_kzalloc(&pdev
->dev
, sizeof(*pdata
), GFP_KERNEL
);
255 platform_set_drvdata(pdev
, pdata
);
257 if (!devm_request_mem_region(&pdev
->dev
, res
->start
,
258 resource_size(res
), pdev
->name
))
260 pdata
->rtcreg
= devm_ioremap(&pdev
->dev
, res
->start
,
265 spin_lock_init(&pdata
->lock
);
266 tx4939_rtc_cmd(pdata
->rtcreg
, TX4939_RTCCTL_COMMAND_NOP
);
267 if (devm_request_irq(&pdev
->dev
, irq
, tx4939_rtc_interrupt
,
268 IRQF_DISABLED
, pdev
->name
, &pdev
->dev
) < 0)
270 rtc
= rtc_device_register(pdev
->name
, &pdev
->dev
,
271 &tx4939_rtc_ops
, THIS_MODULE
);
275 ret
= sysfs_create_bin_file(&pdev
->dev
.kobj
, &tx4939_rtc_nvram_attr
);
277 rtc_device_unregister(rtc
);
281 static int __exit
tx4939_rtc_remove(struct platform_device
*pdev
)
283 struct tx4939rtc_plat_data
*pdata
= platform_get_drvdata(pdev
);
285 sysfs_remove_bin_file(&pdev
->dev
.kobj
, &tx4939_rtc_nvram_attr
);
286 rtc_device_unregister(pdata
->rtc
);
287 spin_lock_irq(&pdata
->lock
);
288 tx4939_rtc_cmd(pdata
->rtcreg
, TX4939_RTCCTL_COMMAND_NOP
);
289 spin_unlock_irq(&pdata
->lock
);
293 static struct platform_driver tx4939_rtc_driver
= {
294 .remove
= __exit_p(tx4939_rtc_remove
),
297 .owner
= THIS_MODULE
,
301 static int __init
tx4939rtc_init(void)
303 return platform_driver_probe(&tx4939_rtc_driver
, tx4939_rtc_probe
);
306 static void __exit
tx4939rtc_exit(void)
308 platform_driver_unregister(&tx4939_rtc_driver
);
311 module_init(tx4939rtc_init
);
312 module_exit(tx4939rtc_exit
);
314 MODULE_AUTHOR("Atsushi Nemoto <anemo@mba.ocn.ne.jp>");
315 MODULE_DESCRIPTION("TX4939 internal RTC driver");
316 MODULE_LICENSE("GPL");
317 MODULE_ALIAS("platform:tx4939rtc");