Btrfs: remove a BUG_ON() in btrfs_commit_transaction()
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / rtc / rtc-tx4939.c
blobec6313d153599d5f745550760b5ad1990d7e7b3c
1 /*
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
7 * for more details.
9 * (C) Copyright TOSHIBA CORPORATION 2005-2007
11 #include <linux/rtc.h>
12 #include <linux/platform_device.h>
13 #include <linux/interrupt.h>
14 #include <linux/io.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;
21 spinlock_t lock;
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)
31 int i = 0;
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) */
37 if (i++ > 200 * 100)
38 return -EBUSY;
39 cpu_relax();
41 return 0;
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;
48 int i, ret;
49 unsigned char buf[6];
51 buf[0] = 0;
52 buf[1] = 0;
53 buf[2] = secs;
54 buf[3] = secs >> 8;
55 buf[4] = secs >> 16;
56 buf[5] = secs >> 24;
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);
65 return ret;
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;
72 int i, ret;
73 unsigned long sec;
74 unsigned char buf[6];
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));
80 if (ret) {
81 spin_unlock_irq(&pdata->lock);
82 return ret;
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;
97 int i, ret;
98 unsigned long sec;
99 unsigned char buf[6];
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)
107 return -EINVAL;
108 rtc_tm_to_time(&alrm->time, &sec);
109 buf[0] = 0;
110 buf[1] = 0;
111 buf[2] = sec;
112 buf[3] = sec >> 8;
113 buf[4] = sec >> 16;
114 buf[5] = sec >> 24;
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);
122 return ret;
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;
129 int i, ret;
130 unsigned long sec;
131 unsigned char buf[6];
132 u32 ctl;
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));
138 if (ret) {
139 spin_unlock_irq(&pdata->lock);
140 return ret;
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);
163 return 0;
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) {
174 events |= RTC_AF;
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);
180 return IRQ_HANDLED;
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;
198 ssize_t count;
200 spin_lock_irq(&pdata->lock);
201 for (count = 0; size > 0 && pos < TX4939_RTC_REG_RAMSIZE;
202 count++, size--) {
203 __raw_writel(pos++, &rtcreg->adr);
204 *buf++ = __raw_readl(&rtcreg->dat);
206 spin_unlock_irq(&pdata->lock);
207 return count;
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;
217 ssize_t count;
219 spin_lock_irq(&pdata->lock);
220 for (count = 0; size > 0 && pos < TX4939_RTC_REG_RAMSIZE;
221 count++, size--) {
222 __raw_writel(pos++, &rtcreg->adr);
223 __raw_writel(*buf++, &rtcreg->dat);
225 spin_unlock_irq(&pdata->lock);
226 return count;
229 static struct bin_attribute tx4939_rtc_nvram_attr = {
230 .attr = {
231 .name = "nvram",
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;
244 int irq, ret;
246 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
247 if (!res)
248 return -ENODEV;
249 irq = platform_get_irq(pdev, 0);
250 if (irq < 0)
251 return -ENODEV;
252 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
253 if (!pdata)
254 return -ENOMEM;
255 platform_set_drvdata(pdev, pdata);
257 if (!devm_request_mem_region(&pdev->dev, res->start,
258 resource_size(res), pdev->name))
259 return -EBUSY;
260 pdata->rtcreg = devm_ioremap(&pdev->dev, res->start,
261 resource_size(res));
262 if (!pdata->rtcreg)
263 return -EBUSY;
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)
269 return -EBUSY;
270 rtc = rtc_device_register(pdev->name, &pdev->dev,
271 &tx4939_rtc_ops, THIS_MODULE);
272 if (IS_ERR(rtc))
273 return PTR_ERR(rtc);
274 pdata->rtc = rtc;
275 ret = sysfs_create_bin_file(&pdev->dev.kobj, &tx4939_rtc_nvram_attr);
276 if (ret)
277 rtc_device_unregister(rtc);
278 return ret;
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);
290 return 0;
293 static struct platform_driver tx4939_rtc_driver = {
294 .remove = __exit_p(tx4939_rtc_remove),
295 .driver = {
296 .name = "tx4939rtc",
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");