Blame view

drivers/rtc/rtc-at32ap700x.c 7.33 KB
fa04e78b2   Hans-Christian Egtvedt   Driver for the At...
1
2
3
4
5
6
7
8
9
10
11
12
13
  /*
   * An RTC driver for the AVR32 AT32AP700x processor series.
   *
   * Copyright (C) 2007 Atmel Corporation
   *
   * This program is free software; you can redistribute it and/or modify it
   * under the terms of the GNU General Public License version 2 as published
   * by the Free Software Foundation.
   */
  
  #include <linux/module.h>
  #include <linux/kernel.h>
  #include <linux/platform_device.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
14
  #include <linux/slab.h>
fa04e78b2   Hans-Christian Egtvedt   Driver for the At...
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
  #include <linux/rtc.h>
  #include <linux/io.h>
  
  /*
   * This is a bare-bones RTC. It runs during most system sleep states, but has
   * no battery backup and gets reset during system restart.  It must be
   * initialized from an external clock (network, I2C, etc) before it can be of
   * much use.
   *
   * The alarm functionality is limited by the hardware, not supporting
   * periodic interrupts.
   */
  
  #define RTC_CTRL		0x00
  #define RTC_CTRL_EN		   0
  #define RTC_CTRL_PCLR		   1
  #define RTC_CTRL_TOPEN		   2
  #define RTC_CTRL_PSEL		   8
  
  #define RTC_VAL			0x04
  
  #define RTC_TOP			0x08
  
  #define RTC_IER			0x10
  #define RTC_IER_TOPI		   0
  
  #define RTC_IDR			0x14
  #define RTC_IDR_TOPI		   0
  
  #define RTC_IMR			0x18
  #define RTC_IMR_TOPI		   0
  
  #define RTC_ISR			0x1c
  #define RTC_ISR_TOPI		   0
  
  #define RTC_ICR			0x20
  #define RTC_ICR_TOPI		   0
  
  #define RTC_BIT(name)		(1 << RTC_##name)
  #define RTC_BF(name, value)	((value) << RTC_##name)
  
  #define rtc_readl(dev, reg)				\
  	__raw_readl((dev)->regs + RTC_##reg)
  #define rtc_writel(dev, reg, value)			\
  	__raw_writel((value), (dev)->regs + RTC_##reg)
  
  struct rtc_at32ap700x {
  	struct rtc_device	*rtc;
  	void __iomem		*regs;
  	unsigned long		alarm_time;
  	unsigned long		irq;
  	/* Protect against concurrent register access. */
  	spinlock_t		lock;
  };
  
  static int at32_rtc_readtime(struct device *dev, struct rtc_time *tm)
  {
  	struct rtc_at32ap700x *rtc = dev_get_drvdata(dev);
  	unsigned long now;
  
  	now = rtc_readl(rtc, VAL);
  	rtc_time_to_tm(now, tm);
  
  	return 0;
  }
  
  static int at32_rtc_settime(struct device *dev, struct rtc_time *tm)
  {
  	struct rtc_at32ap700x *rtc = dev_get_drvdata(dev);
  	unsigned long now;
  	int ret;
  
  	ret = rtc_tm_to_time(tm, &now);
  	if (ret == 0)
  		rtc_writel(rtc, VAL, now);
  
  	return ret;
  }
  
  static int at32_rtc_readalarm(struct device *dev, struct rtc_wkalrm *alrm)
  {
  	struct rtc_at32ap700x *rtc = dev_get_drvdata(dev);
529a4f4ec   Haavard Skinnemoen   rtc-at32ap700x: f...
97
  	spin_lock_irq(&rtc->lock);
fa04e78b2   Hans-Christian Egtvedt   Driver for the At...
98
  	rtc_time_to_tm(rtc->alarm_time, &alrm->time);
529a4f4ec   Haavard Skinnemoen   rtc-at32ap700x: f...
99
100
101
  	alrm->enabled = rtc_readl(rtc, IMR) & RTC_BIT(IMR_TOPI) ? 1 : 0;
  	alrm->pending = rtc_readl(rtc, ISR) & RTC_BIT(ISR_TOPI) ? 1 : 0;
  	spin_unlock_irq(&rtc->lock);
fa04e78b2   Hans-Christian Egtvedt   Driver for the At...
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
  
  	return 0;
  }
  
  static int at32_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)
  {
  	struct rtc_at32ap700x *rtc = dev_get_drvdata(dev);
  	unsigned long rtc_unix_time;
  	unsigned long alarm_unix_time;
  	int ret;
  
  	rtc_unix_time = rtc_readl(rtc, VAL);
  
  	ret = rtc_tm_to_time(&alrm->time, &alarm_unix_time);
  	if (ret)
  		return ret;
  
  	if (alarm_unix_time < rtc_unix_time)
  		return -EINVAL;
  
  	spin_lock_irq(&rtc->lock);
  	rtc->alarm_time = alarm_unix_time;
  	rtc_writel(rtc, TOP, rtc->alarm_time);
529a4f4ec   Haavard Skinnemoen   rtc-at32ap700x: f...
125
  	if (alrm->enabled)
fa04e78b2   Hans-Christian Egtvedt   Driver for the At...
126
127
128
129
130
131
132
133
134
  		rtc_writel(rtc, CTRL, rtc_readl(rtc, CTRL)
  				| RTC_BIT(CTRL_TOPEN));
  	else
  		rtc_writel(rtc, CTRL, rtc_readl(rtc, CTRL)
  				& ~RTC_BIT(CTRL_TOPEN));
  	spin_unlock_irq(&rtc->lock);
  
  	return ret;
  }
16380c153   John Stultz   RTC: Convert rtc ...
135
  static int at32_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
fa04e78b2   Hans-Christian Egtvedt   Driver for the At...
136
137
138
139
140
  {
  	struct rtc_at32ap700x *rtc = dev_get_drvdata(dev);
  	int ret = 0;
  
  	spin_lock_irq(&rtc->lock);
16380c153   John Stultz   RTC: Convert rtc ...
141
  	if(enabled) {
fa04e78b2   Hans-Christian Egtvedt   Driver for the At...
142
143
  		if (rtc_readl(rtc, VAL) > rtc->alarm_time) {
  			ret = -EINVAL;
16380c153   John Stultz   RTC: Convert rtc ...
144
  			goto out;
fa04e78b2   Hans-Christian Egtvedt   Driver for the At...
145
146
147
148
149
  		}
  		rtc_writel(rtc, CTRL, rtc_readl(rtc, CTRL)
  				| RTC_BIT(CTRL_TOPEN));
  		rtc_writel(rtc, ICR, RTC_BIT(ICR_TOPI));
  		rtc_writel(rtc, IER, RTC_BIT(IER_TOPI));
16380c153   John Stultz   RTC: Convert rtc ...
150
  	} else {
fa04e78b2   Hans-Christian Egtvedt   Driver for the At...
151
152
153
154
  		rtc_writel(rtc, CTRL, rtc_readl(rtc, CTRL)
  				& ~RTC_BIT(CTRL_TOPEN));
  		rtc_writel(rtc, IDR, RTC_BIT(IDR_TOPI));
  		rtc_writel(rtc, ICR, RTC_BIT(ICR_TOPI));
fa04e78b2   Hans-Christian Egtvedt   Driver for the At...
155
  	}
16380c153   John Stultz   RTC: Convert rtc ...
156
  out:
fa04e78b2   Hans-Christian Egtvedt   Driver for the At...
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
  	spin_unlock_irq(&rtc->lock);
  
  	return ret;
  }
  
  static irqreturn_t at32_rtc_interrupt(int irq, void *dev_id)
  {
  	struct rtc_at32ap700x *rtc = (struct rtc_at32ap700x *)dev_id;
  	unsigned long isr = rtc_readl(rtc, ISR);
  	unsigned long events = 0;
  	int ret = IRQ_NONE;
  
  	spin_lock(&rtc->lock);
  
  	if (isr & RTC_BIT(ISR_TOPI)) {
  		rtc_writel(rtc, ICR, RTC_BIT(ICR_TOPI));
  		rtc_writel(rtc, IDR, RTC_BIT(IDR_TOPI));
  		rtc_writel(rtc, CTRL, rtc_readl(rtc, CTRL)
  				& ~RTC_BIT(CTRL_TOPEN));
  		rtc_writel(rtc, VAL, rtc->alarm_time);
  		events = RTC_AF | RTC_IRQF;
  		rtc_update_irq(rtc->rtc, 1, events);
  		ret = IRQ_HANDLED;
  	}
  
  	spin_unlock(&rtc->lock);
  
  	return ret;
  }
  
  static struct rtc_class_ops at32_rtc_ops = {
fa04e78b2   Hans-Christian Egtvedt   Driver for the At...
188
189
190
191
  	.read_time	= at32_rtc_readtime,
  	.set_time	= at32_rtc_settime,
  	.read_alarm	= at32_rtc_readalarm,
  	.set_alarm	= at32_rtc_setalarm,
16380c153   John Stultz   RTC: Convert rtc ...
192
  	.alarm_irq_enable = at32_rtc_alarm_irq_enable,
fa04e78b2   Hans-Christian Egtvedt   Driver for the At...
193
194
195
196
197
198
  };
  
  static int __init at32_rtc_probe(struct platform_device *pdev)
  {
  	struct resource	*regs;
  	struct rtc_at32ap700x *rtc;
2fac6674d   Anton Vorontsov   rtc: bunch of dri...
199
  	int irq;
fa04e78b2   Hans-Christian Egtvedt   Driver for the At...
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
  	int ret;
  
  	rtc = kzalloc(sizeof(struct rtc_at32ap700x), GFP_KERNEL);
  	if (!rtc) {
  		dev_dbg(&pdev->dev, "out of memory
  ");
  		return -ENOMEM;
  	}
  
  	regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  	if (!regs) {
  		dev_dbg(&pdev->dev, "no mmio resource defined
  ");
  		ret = -ENXIO;
  		goto out;
  	}
  
  	irq = platform_get_irq(pdev, 0);
2fac6674d   Anton Vorontsov   rtc: bunch of dri...
218
  	if (irq <= 0) {
fa04e78b2   Hans-Christian Egtvedt   Driver for the At...
219
220
221
222
223
  		dev_dbg(&pdev->dev, "could not get irq
  ");
  		ret = -ENXIO;
  		goto out;
  	}
fa04e78b2   Hans-Christian Egtvedt   Driver for the At...
224
  	rtc->irq = irq;
28f65c11f   Joe Perches   treewide: Convert...
225
  	rtc->regs = ioremap(regs->start, resource_size(regs));
fa04e78b2   Hans-Christian Egtvedt   Driver for the At...
226
227
228
229
  	if (!rtc->regs) {
  		ret = -ENOMEM;
  		dev_dbg(&pdev->dev, "could not map I/O memory
  ");
8d431dbef   David Brownell   rtc-at32ap700x: f...
230
  		goto out;
fa04e78b2   Hans-Christian Egtvedt   Driver for the At...
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
  	}
  	spin_lock_init(&rtc->lock);
  
  	/*
  	 * Maybe init RTC: count from zero at 1 Hz, disable wrap irq.
  	 *
  	 * Do not reset VAL register, as it can hold an old time
  	 * from last JTAG reset.
  	 */
  	if (!(rtc_readl(rtc, CTRL) & RTC_BIT(CTRL_EN))) {
  		rtc_writel(rtc, CTRL, RTC_BIT(CTRL_PCLR));
  		rtc_writel(rtc, IDR, RTC_BIT(IDR_TOPI));
  		rtc_writel(rtc, CTRL, RTC_BF(CTRL_PSEL, 0xe)
  				| RTC_BIT(CTRL_EN));
  	}
8d431dbef   David Brownell   rtc-at32ap700x: f...
246
247
248
249
250
251
  	ret = request_irq(irq, at32_rtc_interrupt, IRQF_SHARED, "rtc", rtc);
  	if (ret) {
  		dev_dbg(&pdev->dev, "could not request irq %d
  ", irq);
  		goto out_iounmap;
  	}
b74d2caa6   Alessandro Zummo   rtc: fix driver d...
252
  	platform_set_drvdata(pdev, rtc);
fa04e78b2   Hans-Christian Egtvedt   Driver for the At...
253
254
255
256
257
258
  	rtc->rtc = rtc_device_register(pdev->name, &pdev->dev,
  				&at32_rtc_ops, THIS_MODULE);
  	if (IS_ERR(rtc->rtc)) {
  		dev_dbg(&pdev->dev, "could not register rtc device
  ");
  		ret = PTR_ERR(rtc->rtc);
8d431dbef   David Brownell   rtc-at32ap700x: f...
259
  		goto out_free_irq;
fa04e78b2   Hans-Christian Egtvedt   Driver for the At...
260
  	}
f3a24e1e2   Haavard Skinnemoen   rtc-at32ap700x: E...
261
  	device_init_wakeup(&pdev->dev, 1);
fa04e78b2   Hans-Christian Egtvedt   Driver for the At...
262
263
264
265
266
267
  
  	dev_info(&pdev->dev, "Atmel RTC for AT32AP700x at %08lx irq %ld
  ",
  			(unsigned long)rtc->regs, rtc->irq);
  
  	return 0;
fa04e78b2   Hans-Christian Egtvedt   Driver for the At...
268
  out_free_irq:
b74d2caa6   Alessandro Zummo   rtc: fix driver d...
269
  	platform_set_drvdata(pdev, NULL);
fa04e78b2   Hans-Christian Egtvedt   Driver for the At...
270
  	free_irq(irq, rtc);
8d431dbef   David Brownell   rtc-at32ap700x: f...
271
272
  out_iounmap:
  	iounmap(rtc->regs);
fa04e78b2   Hans-Christian Egtvedt   Driver for the At...
273
274
275
276
277
278
279
280
  out:
  	kfree(rtc);
  	return ret;
  }
  
  static int __exit at32_rtc_remove(struct platform_device *pdev)
  {
  	struct rtc_at32ap700x *rtc = platform_get_drvdata(pdev);
f3a24e1e2   Haavard Skinnemoen   rtc-at32ap700x: E...
281
  	device_init_wakeup(&pdev->dev, 0);
fa04e78b2   Hans-Christian Egtvedt   Driver for the At...
282
283
284
285
286
287
288
289
  	free_irq(rtc->irq, rtc);
  	iounmap(rtc->regs);
  	rtc_device_unregister(rtc->rtc);
  	kfree(rtc);
  	platform_set_drvdata(pdev, NULL);
  
  	return 0;
  }
ad28a07bc   Kay Sievers   rtc: fix platform...
290
  MODULE_ALIAS("platform:at32ap700x_rtc");
fa04e78b2   Hans-Christian Egtvedt   Driver for the At...
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
  
  static struct platform_driver at32_rtc_driver = {
  	.remove		= __exit_p(at32_rtc_remove),
  	.driver		= {
  		.name	= "at32ap700x_rtc",
  		.owner	= THIS_MODULE,
  	},
  };
  
  static int __init at32_rtc_init(void)
  {
  	return platform_driver_probe(&at32_rtc_driver, at32_rtc_probe);
  }
  module_init(at32_rtc_init);
  
  static void __exit at32_rtc_exit(void)
  {
  	platform_driver_unregister(&at32_rtc_driver);
  }
  module_exit(at32_rtc_exit);
  
  MODULE_AUTHOR("Hans-Christian Egtvedt <hcegtvedt@atmel.com>");
  MODULE_DESCRIPTION("Real time clock for AVR32 AT32AP700x");
  MODULE_LICENSE("GPL");