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 <asm/txx9/tx4939.h>
17 struct tx4939rtc_plat_data
{
18 struct rtc_device
*rtc
;
19 struct tx4939_rtc_reg __iomem
*rtcreg
;
23 static struct tx4939rtc_plat_data
*get_tx4939rtc_plat_data(struct device
*dev
)
25 return platform_get_drvdata(to_platform_device(dev
));
28 static int tx4939_rtc_cmd(struct tx4939_rtc_reg __iomem
*rtcreg
, int cmd
)
32 __raw_writel(cmd
, &rtcreg
->ctl
);
33 /* This might take 30us (next 32.768KHz clock) */
34 while (__raw_readl(&rtcreg
->ctl
) & TX4939_RTCCTL_BUSY
) {
35 /* timeout on approx. 100us (@ GBUS200MHz) */
43 static int tx4939_rtc_set_mmss(struct device
*dev
, unsigned long secs
)
45 struct tx4939rtc_plat_data
*pdata
= get_tx4939rtc_plat_data(dev
);
46 struct tx4939_rtc_reg __iomem
*rtcreg
= pdata
->rtcreg
;
56 spin_lock_irq(&pdata
->lock
);
57 __raw_writel(0, &rtcreg
->adr
);
58 for (i
= 0; i
< 6; i
++)
59 __raw_writel(buf
[i
], &rtcreg
->dat
);
60 ret
= tx4939_rtc_cmd(rtcreg
,
61 TX4939_RTCCTL_COMMAND_SETTIME
|
62 (__raw_readl(&rtcreg
->ctl
) & TX4939_RTCCTL_ALME
));
63 spin_unlock_irq(&pdata
->lock
);
67 static int tx4939_rtc_read_time(struct device
*dev
, struct rtc_time
*tm
)
69 struct tx4939rtc_plat_data
*pdata
= get_tx4939rtc_plat_data(dev
);
70 struct tx4939_rtc_reg __iomem
*rtcreg
= pdata
->rtcreg
;
75 spin_lock_irq(&pdata
->lock
);
76 ret
= tx4939_rtc_cmd(rtcreg
,
77 TX4939_RTCCTL_COMMAND_GETTIME
|
78 (__raw_readl(&rtcreg
->ctl
) & TX4939_RTCCTL_ALME
));
80 spin_unlock_irq(&pdata
->lock
);
83 __raw_writel(2, &rtcreg
->adr
);
84 for (i
= 2; i
< 6; i
++)
85 buf
[i
] = __raw_readl(&rtcreg
->dat
);
86 spin_unlock_irq(&pdata
->lock
);
87 sec
= (buf
[5] << 24) | (buf
[4] << 16) | (buf
[3] << 8) | buf
[2];
88 rtc_time_to_tm(sec
, tm
);
89 return rtc_valid_tm(tm
);
92 static int tx4939_rtc_set_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
94 struct tx4939rtc_plat_data
*pdata
= get_tx4939rtc_plat_data(dev
);
95 struct tx4939_rtc_reg __iomem
*rtcreg
= pdata
->rtcreg
;
100 if (alrm
->time
.tm_sec
< 0 ||
101 alrm
->time
.tm_min
< 0 ||
102 alrm
->time
.tm_hour
< 0 ||
103 alrm
->time
.tm_mday
< 0 ||
104 alrm
->time
.tm_mon
< 0 ||
105 alrm
->time
.tm_year
< 0)
107 rtc_tm_to_time(&alrm
->time
, &sec
);
114 spin_lock_irq(&pdata
->lock
);
115 __raw_writel(0, &rtcreg
->adr
);
116 for (i
= 0; i
< 6; i
++)
117 __raw_writel(buf
[i
], &rtcreg
->dat
);
118 ret
= tx4939_rtc_cmd(rtcreg
, TX4939_RTCCTL_COMMAND_SETALARM
|
119 (alrm
->enabled
? TX4939_RTCCTL_ALME
: 0));
120 spin_unlock_irq(&pdata
->lock
);
124 static int tx4939_rtc_read_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
126 struct tx4939rtc_plat_data
*pdata
= get_tx4939rtc_plat_data(dev
);
127 struct tx4939_rtc_reg __iomem
*rtcreg
= pdata
->rtcreg
;
130 unsigned char buf
[6];
133 spin_lock_irq(&pdata
->lock
);
134 ret
= tx4939_rtc_cmd(rtcreg
,
135 TX4939_RTCCTL_COMMAND_GETALARM
|
136 (__raw_readl(&rtcreg
->ctl
) & TX4939_RTCCTL_ALME
));
138 spin_unlock_irq(&pdata
->lock
);
141 __raw_writel(2, &rtcreg
->adr
);
142 for (i
= 2; i
< 6; i
++)
143 buf
[i
] = __raw_readl(&rtcreg
->dat
);
144 ctl
= __raw_readl(&rtcreg
->ctl
);
145 alrm
->enabled
= (ctl
& TX4939_RTCCTL_ALME
) ? 1 : 0;
146 alrm
->pending
= (ctl
& TX4939_RTCCTL_ALMD
) ? 1 : 0;
147 spin_unlock_irq(&pdata
->lock
);
148 sec
= (buf
[5] << 24) | (buf
[4] << 16) | (buf
[3] << 8) | buf
[2];
149 rtc_time_to_tm(sec
, &alrm
->time
);
150 return rtc_valid_tm(&alrm
->time
);
153 static int tx4939_rtc_alarm_irq_enable(struct device
*dev
, unsigned int enabled
)
155 struct tx4939rtc_plat_data
*pdata
= get_tx4939rtc_plat_data(dev
);
157 spin_lock_irq(&pdata
->lock
);
158 tx4939_rtc_cmd(pdata
->rtcreg
,
159 TX4939_RTCCTL_COMMAND_NOP
|
160 (enabled
? TX4939_RTCCTL_ALME
: 0));
161 spin_unlock_irq(&pdata
->lock
);
165 static irqreturn_t
tx4939_rtc_interrupt(int irq
, void *dev_id
)
167 struct tx4939rtc_plat_data
*pdata
= get_tx4939rtc_plat_data(dev_id
);
168 struct tx4939_rtc_reg __iomem
*rtcreg
= pdata
->rtcreg
;
169 unsigned long events
= RTC_IRQF
;
171 spin_lock(&pdata
->lock
);
172 if (__raw_readl(&rtcreg
->ctl
) & TX4939_RTCCTL_ALMD
) {
174 tx4939_rtc_cmd(rtcreg
, TX4939_RTCCTL_COMMAND_NOP
);
176 spin_unlock(&pdata
->lock
);
177 if (likely(pdata
->rtc
))
178 rtc_update_irq(pdata
->rtc
, 1, events
);
182 static const struct rtc_class_ops tx4939_rtc_ops
= {
183 .read_time
= tx4939_rtc_read_time
,
184 .read_alarm
= tx4939_rtc_read_alarm
,
185 .set_alarm
= tx4939_rtc_set_alarm
,
186 .set_mmss
= tx4939_rtc_set_mmss
,
187 .alarm_irq_enable
= tx4939_rtc_alarm_irq_enable
,
190 static ssize_t
tx4939_rtc_nvram_read(struct kobject
*kobj
,
191 struct bin_attribute
*bin_attr
,
192 char *buf
, loff_t pos
, size_t size
)
194 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
195 struct tx4939rtc_plat_data
*pdata
= get_tx4939rtc_plat_data(dev
);
196 struct tx4939_rtc_reg __iomem
*rtcreg
= pdata
->rtcreg
;
199 spin_lock_irq(&pdata
->lock
);
200 for (count
= 0; size
> 0 && pos
< TX4939_RTC_REG_RAMSIZE
;
202 __raw_writel(pos
++, &rtcreg
->adr
);
203 *buf
++ = __raw_readl(&rtcreg
->dat
);
205 spin_unlock_irq(&pdata
->lock
);
209 static ssize_t
tx4939_rtc_nvram_write(struct kobject
*kobj
,
210 struct bin_attribute
*bin_attr
,
211 char *buf
, loff_t pos
, size_t size
)
213 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
214 struct tx4939rtc_plat_data
*pdata
= get_tx4939rtc_plat_data(dev
);
215 struct tx4939_rtc_reg __iomem
*rtcreg
= pdata
->rtcreg
;
218 spin_lock_irq(&pdata
->lock
);
219 for (count
= 0; size
> 0 && pos
< TX4939_RTC_REG_RAMSIZE
;
221 __raw_writel(pos
++, &rtcreg
->adr
);
222 __raw_writel(*buf
++, &rtcreg
->dat
);
224 spin_unlock_irq(&pdata
->lock
);
228 static struct bin_attribute tx4939_rtc_nvram_attr
= {
231 .mode
= S_IRUGO
| S_IWUSR
,
233 .size
= TX4939_RTC_REG_RAMSIZE
,
234 .read
= tx4939_rtc_nvram_read
,
235 .write
= tx4939_rtc_nvram_write
,
238 static int __init
tx4939_rtc_probe(struct platform_device
*pdev
)
240 struct rtc_device
*rtc
;
241 struct tx4939rtc_plat_data
*pdata
;
242 struct resource
*res
;
245 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
248 irq
= platform_get_irq(pdev
, 0);
251 pdata
= devm_kzalloc(&pdev
->dev
, sizeof(*pdata
), GFP_KERNEL
);
254 platform_set_drvdata(pdev
, pdata
);
256 if (!devm_request_mem_region(&pdev
->dev
, res
->start
,
257 resource_size(res
), pdev
->name
))
259 pdata
->rtcreg
= devm_ioremap(&pdev
->dev
, res
->start
,
264 spin_lock_init(&pdata
->lock
);
265 tx4939_rtc_cmd(pdata
->rtcreg
, TX4939_RTCCTL_COMMAND_NOP
);
266 if (devm_request_irq(&pdev
->dev
, irq
, tx4939_rtc_interrupt
,
267 IRQF_DISABLED
, pdev
->name
, &pdev
->dev
) < 0)
269 rtc
= rtc_device_register(pdev
->name
, &pdev
->dev
,
270 &tx4939_rtc_ops
, THIS_MODULE
);
274 ret
= sysfs_create_bin_file(&pdev
->dev
.kobj
, &tx4939_rtc_nvram_attr
);
276 rtc_device_unregister(rtc
);
280 static int __exit
tx4939_rtc_remove(struct platform_device
*pdev
)
282 struct tx4939rtc_plat_data
*pdata
= platform_get_drvdata(pdev
);
284 sysfs_remove_bin_file(&pdev
->dev
.kobj
, &tx4939_rtc_nvram_attr
);
285 rtc_device_unregister(pdata
->rtc
);
286 spin_lock_irq(&pdata
->lock
);
287 tx4939_rtc_cmd(pdata
->rtcreg
, TX4939_RTCCTL_COMMAND_NOP
);
288 spin_unlock_irq(&pdata
->lock
);
292 static struct platform_driver tx4939_rtc_driver
= {
293 .remove
= __exit_p(tx4939_rtc_remove
),
296 .owner
= THIS_MODULE
,
300 static int __init
tx4939rtc_init(void)
302 return platform_driver_probe(&tx4939_rtc_driver
, tx4939_rtc_probe
);
305 static void __exit
tx4939rtc_exit(void)
307 platform_driver_unregister(&tx4939_rtc_driver
);
310 module_init(tx4939rtc_init
);
311 module_exit(tx4939rtc_exit
);
313 MODULE_AUTHOR("Atsushi Nemoto <anemo@mba.ocn.ne.jp>");
314 MODULE_DESCRIPTION("TX4939 internal RTC driver");
315 MODULE_LICENSE("GPL");
316 MODULE_ALIAS("platform:tx4939rtc");