ARM: 8481/2: drivers: psci: replace psci firmware calls
[linux/fpc-iii.git] / drivers / rtc / rtc-tx4939.c
bloba9528083061d50d9f6b59be99ec42a386d51e23e
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/module.h>
15 #include <linux/io.h>
16 #include <linux/gfp.h>
17 #include <asm/txx9/tx4939.h>
19 struct tx4939rtc_plat_data {
20 struct rtc_device *rtc;
21 struct tx4939_rtc_reg __iomem *rtcreg;
22 spinlock_t lock;
25 static struct tx4939rtc_plat_data *get_tx4939rtc_plat_data(struct device *dev)
27 return platform_get_drvdata(to_platform_device(dev));
30 static int tx4939_rtc_cmd(struct tx4939_rtc_reg __iomem *rtcreg, int cmd)
32 int i = 0;
34 __raw_writel(cmd, &rtcreg->ctl);
35 /* This might take 30us (next 32.768KHz clock) */
36 while (__raw_readl(&rtcreg->ctl) & TX4939_RTCCTL_BUSY) {
37 /* timeout on approx. 100us (@ GBUS200MHz) */
38 if (i++ > 200 * 100)
39 return -EBUSY;
40 cpu_relax();
42 return 0;
45 static int tx4939_rtc_set_mmss(struct device *dev, unsigned long secs)
47 struct tx4939rtc_plat_data *pdata = get_tx4939rtc_plat_data(dev);
48 struct tx4939_rtc_reg __iomem *rtcreg = pdata->rtcreg;
49 int i, ret;
50 unsigned char buf[6];
52 buf[0] = 0;
53 buf[1] = 0;
54 buf[2] = secs;
55 buf[3] = secs >> 8;
56 buf[4] = secs >> 16;
57 buf[5] = secs >> 24;
58 spin_lock_irq(&pdata->lock);
59 __raw_writel(0, &rtcreg->adr);
60 for (i = 0; i < 6; i++)
61 __raw_writel(buf[i], &rtcreg->dat);
62 ret = tx4939_rtc_cmd(rtcreg,
63 TX4939_RTCCTL_COMMAND_SETTIME |
64 (__raw_readl(&rtcreg->ctl) & TX4939_RTCCTL_ALME));
65 spin_unlock_irq(&pdata->lock);
66 return ret;
69 static int tx4939_rtc_read_time(struct device *dev, struct rtc_time *tm)
71 struct tx4939rtc_plat_data *pdata = get_tx4939rtc_plat_data(dev);
72 struct tx4939_rtc_reg __iomem *rtcreg = pdata->rtcreg;
73 int i, ret;
74 unsigned long sec;
75 unsigned char buf[6];
77 spin_lock_irq(&pdata->lock);
78 ret = tx4939_rtc_cmd(rtcreg,
79 TX4939_RTCCTL_COMMAND_GETTIME |
80 (__raw_readl(&rtcreg->ctl) & TX4939_RTCCTL_ALME));
81 if (ret) {
82 spin_unlock_irq(&pdata->lock);
83 return ret;
85 __raw_writel(2, &rtcreg->adr);
86 for (i = 2; i < 6; i++)
87 buf[i] = __raw_readl(&rtcreg->dat);
88 spin_unlock_irq(&pdata->lock);
89 sec = ((unsigned long)buf[5] << 24) | (buf[4] << 16) |
90 (buf[3] << 8) | buf[2];
91 rtc_time_to_tm(sec, tm);
92 return rtc_valid_tm(tm);
95 static int tx4939_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
97 struct tx4939rtc_plat_data *pdata = get_tx4939rtc_plat_data(dev);
98 struct tx4939_rtc_reg __iomem *rtcreg = pdata->rtcreg;
99 int i, ret;
100 unsigned long sec;
101 unsigned char buf[6];
103 if (alrm->time.tm_sec < 0 ||
104 alrm->time.tm_min < 0 ||
105 alrm->time.tm_hour < 0 ||
106 alrm->time.tm_mday < 0 ||
107 alrm->time.tm_mon < 0 ||
108 alrm->time.tm_year < 0)
109 return -EINVAL;
110 rtc_tm_to_time(&alrm->time, &sec);
111 buf[0] = 0;
112 buf[1] = 0;
113 buf[2] = sec;
114 buf[3] = sec >> 8;
115 buf[4] = sec >> 16;
116 buf[5] = sec >> 24;
117 spin_lock_irq(&pdata->lock);
118 __raw_writel(0, &rtcreg->adr);
119 for (i = 0; i < 6; i++)
120 __raw_writel(buf[i], &rtcreg->dat);
121 ret = tx4939_rtc_cmd(rtcreg, TX4939_RTCCTL_COMMAND_SETALARM |
122 (alrm->enabled ? TX4939_RTCCTL_ALME : 0));
123 spin_unlock_irq(&pdata->lock);
124 return ret;
127 static int tx4939_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
129 struct tx4939rtc_plat_data *pdata = get_tx4939rtc_plat_data(dev);
130 struct tx4939_rtc_reg __iomem *rtcreg = pdata->rtcreg;
131 int i, ret;
132 unsigned long sec;
133 unsigned char buf[6];
134 u32 ctl;
136 spin_lock_irq(&pdata->lock);
137 ret = tx4939_rtc_cmd(rtcreg,
138 TX4939_RTCCTL_COMMAND_GETALARM |
139 (__raw_readl(&rtcreg->ctl) & TX4939_RTCCTL_ALME));
140 if (ret) {
141 spin_unlock_irq(&pdata->lock);
142 return ret;
144 __raw_writel(2, &rtcreg->adr);
145 for (i = 2; i < 6; i++)
146 buf[i] = __raw_readl(&rtcreg->dat);
147 ctl = __raw_readl(&rtcreg->ctl);
148 alrm->enabled = (ctl & TX4939_RTCCTL_ALME) ? 1 : 0;
149 alrm->pending = (ctl & TX4939_RTCCTL_ALMD) ? 1 : 0;
150 spin_unlock_irq(&pdata->lock);
151 sec = ((unsigned long)buf[5] << 24) | (buf[4] << 16) |
152 (buf[3] << 8) | buf[2];
153 rtc_time_to_tm(sec, &alrm->time);
154 return rtc_valid_tm(&alrm->time);
157 static int tx4939_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
159 struct tx4939rtc_plat_data *pdata = get_tx4939rtc_plat_data(dev);
161 spin_lock_irq(&pdata->lock);
162 tx4939_rtc_cmd(pdata->rtcreg,
163 TX4939_RTCCTL_COMMAND_NOP |
164 (enabled ? TX4939_RTCCTL_ALME : 0));
165 spin_unlock_irq(&pdata->lock);
166 return 0;
169 static irqreturn_t tx4939_rtc_interrupt(int irq, void *dev_id)
171 struct tx4939rtc_plat_data *pdata = get_tx4939rtc_plat_data(dev_id);
172 struct tx4939_rtc_reg __iomem *rtcreg = pdata->rtcreg;
173 unsigned long events = RTC_IRQF;
175 spin_lock(&pdata->lock);
176 if (__raw_readl(&rtcreg->ctl) & TX4939_RTCCTL_ALMD) {
177 events |= RTC_AF;
178 tx4939_rtc_cmd(rtcreg, TX4939_RTCCTL_COMMAND_NOP);
180 spin_unlock(&pdata->lock);
181 rtc_update_irq(pdata->rtc, 1, events);
183 return IRQ_HANDLED;
186 static const struct rtc_class_ops tx4939_rtc_ops = {
187 .read_time = tx4939_rtc_read_time,
188 .read_alarm = tx4939_rtc_read_alarm,
189 .set_alarm = tx4939_rtc_set_alarm,
190 .set_mmss = tx4939_rtc_set_mmss,
191 .alarm_irq_enable = tx4939_rtc_alarm_irq_enable,
194 static ssize_t tx4939_rtc_nvram_read(struct file *filp, struct kobject *kobj,
195 struct bin_attribute *bin_attr,
196 char *buf, loff_t pos, size_t size)
198 struct device *dev = container_of(kobj, struct device, kobj);
199 struct tx4939rtc_plat_data *pdata = get_tx4939rtc_plat_data(dev);
200 struct tx4939_rtc_reg __iomem *rtcreg = pdata->rtcreg;
201 ssize_t count;
203 spin_lock_irq(&pdata->lock);
204 for (count = 0; count < size; count++) {
205 __raw_writel(pos++, &rtcreg->adr);
206 *buf++ = __raw_readl(&rtcreg->dat);
208 spin_unlock_irq(&pdata->lock);
209 return count;
212 static ssize_t tx4939_rtc_nvram_write(struct file *filp, struct kobject *kobj,
213 struct bin_attribute *bin_attr,
214 char *buf, loff_t pos, size_t size)
216 struct device *dev = container_of(kobj, struct device, kobj);
217 struct tx4939rtc_plat_data *pdata = get_tx4939rtc_plat_data(dev);
218 struct tx4939_rtc_reg __iomem *rtcreg = pdata->rtcreg;
219 ssize_t count;
221 spin_lock_irq(&pdata->lock);
222 for (count = 0; count < size; count++) {
223 __raw_writel(pos++, &rtcreg->adr);
224 __raw_writel(*buf++, &rtcreg->dat);
226 spin_unlock_irq(&pdata->lock);
227 return count;
230 static struct bin_attribute tx4939_rtc_nvram_attr = {
231 .attr = {
232 .name = "nvram",
233 .mode = S_IRUGO | S_IWUSR,
235 .size = TX4939_RTC_REG_RAMSIZE,
236 .read = tx4939_rtc_nvram_read,
237 .write = tx4939_rtc_nvram_write,
240 static int __init tx4939_rtc_probe(struct platform_device *pdev)
242 struct rtc_device *rtc;
243 struct tx4939rtc_plat_data *pdata;
244 struct resource *res;
245 int irq, ret;
247 irq = platform_get_irq(pdev, 0);
248 if (irq < 0)
249 return -ENODEV;
250 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
251 if (!pdata)
252 return -ENOMEM;
253 platform_set_drvdata(pdev, pdata);
255 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
256 pdata->rtcreg = devm_ioremap_resource(&pdev->dev, res);
257 if (IS_ERR(pdata->rtcreg))
258 return PTR_ERR(pdata->rtcreg);
260 spin_lock_init(&pdata->lock);
261 tx4939_rtc_cmd(pdata->rtcreg, TX4939_RTCCTL_COMMAND_NOP);
262 if (devm_request_irq(&pdev->dev, irq, tx4939_rtc_interrupt,
263 0, pdev->name, &pdev->dev) < 0)
264 return -EBUSY;
265 rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
266 &tx4939_rtc_ops, THIS_MODULE);
267 if (IS_ERR(rtc))
268 return PTR_ERR(rtc);
269 pdata->rtc = rtc;
270 ret = sysfs_create_bin_file(&pdev->dev.kobj, &tx4939_rtc_nvram_attr);
272 return ret;
275 static int __exit tx4939_rtc_remove(struct platform_device *pdev)
277 struct tx4939rtc_plat_data *pdata = platform_get_drvdata(pdev);
279 sysfs_remove_bin_file(&pdev->dev.kobj, &tx4939_rtc_nvram_attr);
280 spin_lock_irq(&pdata->lock);
281 tx4939_rtc_cmd(pdata->rtcreg, TX4939_RTCCTL_COMMAND_NOP);
282 spin_unlock_irq(&pdata->lock);
283 return 0;
286 static struct platform_driver tx4939_rtc_driver = {
287 .remove = __exit_p(tx4939_rtc_remove),
288 .driver = {
289 .name = "tx4939rtc",
293 module_platform_driver_probe(tx4939_rtc_driver, tx4939_rtc_probe);
295 MODULE_AUTHOR("Atsushi Nemoto <anemo@mba.ocn.ne.jp>");
296 MODULE_DESCRIPTION("TX4939 internal RTC driver");
297 MODULE_LICENSE("GPL");
298 MODULE_ALIAS("platform:tx4939rtc");