Linux Audio

Check our new training course

Loading...
v5.4
 
  1/*
  2 * drivers/rtc/rtc-spear.c
  3 *
  4 * Copyright (C) 2010 ST Microelectronics
  5 * Rajeev Kumar<rajeev-dlh.kumar@st.com>
  6 *
  7 * This file is licensed under the terms of the GNU General Public
  8 * License version 2. This program is licensed "as is" without any
  9 * warranty of any kind, whether express or implied.
 10 */
 11
 12#include <linux/bcd.h>
 13#include <linux/clk.h>
 14#include <linux/delay.h>
 15#include <linux/init.h>
 16#include <linux/io.h>
 17#include <linux/irq.h>
 18#include <linux/module.h>
 19#include <linux/of.h>
 20#include <linux/platform_device.h>
 21#include <linux/rtc.h>
 22#include <linux/slab.h>
 23#include <linux/spinlock.h>
 24
 25/* RTC registers */
 26#define TIME_REG		0x00
 27#define DATE_REG		0x04
 28#define ALARM_TIME_REG		0x08
 29#define ALARM_DATE_REG		0x0C
 30#define CTRL_REG		0x10
 31#define STATUS_REG		0x14
 32
 33/* TIME_REG & ALARM_TIME_REG */
 34#define SECONDS_UNITS		(0xf<<0)	/* seconds units position */
 35#define SECONDS_TENS		(0x7<<4)	/* seconds tens position */
 36#define MINUTES_UNITS		(0xf<<8)	/* minutes units position */
 37#define MINUTES_TENS		(0x7<<12)	/* minutes tens position */
 38#define HOURS_UNITS		(0xf<<16)	/* hours units position */
 39#define HOURS_TENS		(0x3<<20)	/* hours tens position */
 40
 41/* DATE_REG & ALARM_DATE_REG */
 42#define DAYS_UNITS		(0xf<<0)	/* days units position */
 43#define DAYS_TENS		(0x3<<4)	/* days tens position */
 44#define MONTHS_UNITS		(0xf<<8)	/* months units position */
 45#define MONTHS_TENS		(0x1<<12)	/* months tens position */
 46#define YEARS_UNITS		(0xf<<16)	/* years units position */
 47#define YEARS_TENS		(0xf<<20)	/* years tens position */
 48#define YEARS_HUNDREDS		(0xf<<24)	/* years hundereds position */
 49#define YEARS_MILLENIUMS	(0xf<<28)	/* years millenium position */
 50
 51/* MASK SHIFT TIME_REG & ALARM_TIME_REG*/
 52#define SECOND_SHIFT		0x00		/* seconds units */
 53#define MINUTE_SHIFT		0x08		/* minutes units position */
 54#define HOUR_SHIFT		0x10		/* hours units position */
 55#define MDAY_SHIFT		0x00		/* Month day shift */
 56#define MONTH_SHIFT		0x08		/* Month shift */
 57#define YEAR_SHIFT		0x10		/* Year shift */
 58
 59#define SECOND_MASK		0x7F
 60#define MIN_MASK		0x7F
 61#define HOUR_MASK		0x3F
 62#define DAY_MASK		0x3F
 63#define MONTH_MASK		0x7F
 64#define YEAR_MASK		0xFFFF
 65
 66/* date reg equal to time reg, for debug only */
 67#define TIME_BYP		(1<<9)
 68#define INT_ENABLE		(1<<31)		/* interrupt enable */
 69
 70/* STATUS_REG */
 71#define CLK_UNCONNECTED		(1<<0)
 72#define PEND_WR_TIME		(1<<2)
 73#define PEND_WR_DATE		(1<<3)
 74#define LOST_WR_TIME		(1<<4)
 75#define LOST_WR_DATE		(1<<5)
 76#define RTC_INT_MASK		(1<<31)
 77#define STATUS_BUSY		(PEND_WR_TIME | PEND_WR_DATE)
 78#define STATUS_FAIL		(LOST_WR_TIME | LOST_WR_DATE)
 79
 80struct spear_rtc_config {
 81	struct rtc_device *rtc;
 82	struct clk *clk;
 83	spinlock_t lock;
 84	void __iomem *ioaddr;
 85	unsigned int irq_wake;
 86};
 87
 88static inline void spear_rtc_clear_interrupt(struct spear_rtc_config *config)
 89{
 90	unsigned int val;
 91	unsigned long flags;
 92
 93	spin_lock_irqsave(&config->lock, flags);
 94	val = readl(config->ioaddr + STATUS_REG);
 95	val |= RTC_INT_MASK;
 96	writel(val, config->ioaddr + STATUS_REG);
 97	spin_unlock_irqrestore(&config->lock, flags);
 98}
 99
100static inline void spear_rtc_enable_interrupt(struct spear_rtc_config *config)
101{
102	unsigned int val;
103
104	val = readl(config->ioaddr + CTRL_REG);
105	if (!(val & INT_ENABLE)) {
106		spear_rtc_clear_interrupt(config);
107		val |= INT_ENABLE;
108		writel(val, config->ioaddr + CTRL_REG);
109	}
110}
111
112static inline void spear_rtc_disable_interrupt(struct spear_rtc_config *config)
113{
114	unsigned int val;
115
116	val = readl(config->ioaddr + CTRL_REG);
117	if (val & INT_ENABLE) {
118		val &= ~INT_ENABLE;
119		writel(val, config->ioaddr + CTRL_REG);
120	}
121}
122
123static inline int is_write_complete(struct spear_rtc_config *config)
124{
125	int ret = 0;
126	unsigned long flags;
127
128	spin_lock_irqsave(&config->lock, flags);
129	if ((readl(config->ioaddr + STATUS_REG)) & STATUS_FAIL)
130		ret = -EIO;
131	spin_unlock_irqrestore(&config->lock, flags);
132
133	return ret;
134}
135
136static void rtc_wait_not_busy(struct spear_rtc_config *config)
137{
138	int status, count = 0;
139	unsigned long flags;
140
141	/* Assuming BUSY may stay active for 80 msec) */
142	for (count = 0; count < 80; count++) {
143		spin_lock_irqsave(&config->lock, flags);
144		status = readl(config->ioaddr + STATUS_REG);
145		spin_unlock_irqrestore(&config->lock, flags);
146		if ((status & STATUS_BUSY) == 0)
147			break;
148		/* check status busy, after each msec */
149		msleep(1);
150	}
151}
152
153static irqreturn_t spear_rtc_irq(int irq, void *dev_id)
154{
155	struct spear_rtc_config *config = dev_id;
156	unsigned long flags, events = 0;
157	unsigned int irq_data;
158
159	spin_lock_irqsave(&config->lock, flags);
160	irq_data = readl(config->ioaddr + STATUS_REG);
161	spin_unlock_irqrestore(&config->lock, flags);
162
163	if ((irq_data & RTC_INT_MASK)) {
164		spear_rtc_clear_interrupt(config);
165		events = RTC_IRQF | RTC_AF;
166		rtc_update_irq(config->rtc, 1, events);
167		return IRQ_HANDLED;
168	} else
169		return IRQ_NONE;
170
171}
172
173static void tm2bcd(struct rtc_time *tm)
174{
175	tm->tm_sec = bin2bcd(tm->tm_sec);
176	tm->tm_min = bin2bcd(tm->tm_min);
177	tm->tm_hour = bin2bcd(tm->tm_hour);
178	tm->tm_mday = bin2bcd(tm->tm_mday);
179	tm->tm_mon = bin2bcd(tm->tm_mon + 1);
180	tm->tm_year = bin2bcd(tm->tm_year);
181}
182
183static void bcd2tm(struct rtc_time *tm)
184{
185	tm->tm_sec = bcd2bin(tm->tm_sec);
186	tm->tm_min = bcd2bin(tm->tm_min);
187	tm->tm_hour = bcd2bin(tm->tm_hour);
188	tm->tm_mday = bcd2bin(tm->tm_mday);
189	tm->tm_mon = bcd2bin(tm->tm_mon) - 1;
190	/* epoch == 1900 */
191	tm->tm_year = bcd2bin(tm->tm_year);
192}
193
194/*
195 * spear_rtc_read_time - set the time
196 * @dev: rtc device in use
197 * @tm: holds date and time
198 *
199 * This function read time and date. On success it will return 0
200 * otherwise -ve error is returned.
201 */
202static int spear_rtc_read_time(struct device *dev, struct rtc_time *tm)
203{
204	struct spear_rtc_config *config = dev_get_drvdata(dev);
205	unsigned int time, date;
206
207	/* we don't report wday/yday/isdst ... */
208	rtc_wait_not_busy(config);
209
210	time = readl(config->ioaddr + TIME_REG);
211	date = readl(config->ioaddr + DATE_REG);
212	tm->tm_sec = (time >> SECOND_SHIFT) & SECOND_MASK;
213	tm->tm_min = (time >> MINUTE_SHIFT) & MIN_MASK;
214	tm->tm_hour = (time >> HOUR_SHIFT) & HOUR_MASK;
215	tm->tm_mday = (date >> MDAY_SHIFT) & DAY_MASK;
216	tm->tm_mon = (date >> MONTH_SHIFT) & MONTH_MASK;
217	tm->tm_year = (date >> YEAR_SHIFT) & YEAR_MASK;
218
219	bcd2tm(tm);
220	return 0;
221}
222
223/*
224 * spear_rtc_set_time - set the time
225 * @dev: rtc device in use
226 * @tm: holds date and time
227 *
228 * This function set time and date. On success it will return 0
229 * otherwise -ve error is returned.
230 */
231static int spear_rtc_set_time(struct device *dev, struct rtc_time *tm)
232{
233	struct spear_rtc_config *config = dev_get_drvdata(dev);
234	unsigned int time, date;
235
236	tm2bcd(tm);
237
238	rtc_wait_not_busy(config);
239	time = (tm->tm_sec << SECOND_SHIFT) | (tm->tm_min << MINUTE_SHIFT) |
240		(tm->tm_hour << HOUR_SHIFT);
241	date = (tm->tm_mday << MDAY_SHIFT) | (tm->tm_mon << MONTH_SHIFT) |
242		(tm->tm_year << YEAR_SHIFT);
243	writel(time, config->ioaddr + TIME_REG);
244	writel(date, config->ioaddr + DATE_REG);
245
246	return is_write_complete(config);
247}
248
249/*
250 * spear_rtc_read_alarm - read the alarm time
251 * @dev: rtc device in use
252 * @alm: holds alarm date and time
253 *
254 * This function read alarm time and date. On success it will return 0
255 * otherwise -ve error is returned.
256 */
257static int spear_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
258{
259	struct spear_rtc_config *config = dev_get_drvdata(dev);
260	unsigned int time, date;
261
262	rtc_wait_not_busy(config);
263
264	time = readl(config->ioaddr + ALARM_TIME_REG);
265	date = readl(config->ioaddr + ALARM_DATE_REG);
266	alm->time.tm_sec = (time >> SECOND_SHIFT) & SECOND_MASK;
267	alm->time.tm_min = (time >> MINUTE_SHIFT) & MIN_MASK;
268	alm->time.tm_hour = (time >> HOUR_SHIFT) & HOUR_MASK;
269	alm->time.tm_mday = (date >> MDAY_SHIFT) & DAY_MASK;
270	alm->time.tm_mon = (date >> MONTH_SHIFT) & MONTH_MASK;
271	alm->time.tm_year = (date >> YEAR_SHIFT) & YEAR_MASK;
272
273	bcd2tm(&alm->time);
274	alm->enabled = readl(config->ioaddr + CTRL_REG) & INT_ENABLE;
275
276	return 0;
277}
278
279/*
280 * spear_rtc_set_alarm - set the alarm time
281 * @dev: rtc device in use
282 * @alm: holds alarm date and time
283 *
284 * This function set alarm time and date. On success it will return 0
285 * otherwise -ve error is returned.
286 */
287static int spear_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
288{
289	struct spear_rtc_config *config = dev_get_drvdata(dev);
290	unsigned int time, date;
291	int err;
292
293	tm2bcd(&alm->time);
294
295	rtc_wait_not_busy(config);
296
297	time = (alm->time.tm_sec << SECOND_SHIFT) | (alm->time.tm_min <<
298			MINUTE_SHIFT) |	(alm->time.tm_hour << HOUR_SHIFT);
299	date = (alm->time.tm_mday << MDAY_SHIFT) | (alm->time.tm_mon <<
300			MONTH_SHIFT) | (alm->time.tm_year << YEAR_SHIFT);
301
302	writel(time, config->ioaddr + ALARM_TIME_REG);
303	writel(date, config->ioaddr + ALARM_DATE_REG);
304	err = is_write_complete(config);
305	if (err < 0)
306		return err;
307
308	if (alm->enabled)
309		spear_rtc_enable_interrupt(config);
310	else
311		spear_rtc_disable_interrupt(config);
312
313	return 0;
314}
315
316static int spear_alarm_irq_enable(struct device *dev, unsigned int enabled)
317{
318	struct spear_rtc_config *config = dev_get_drvdata(dev);
319	int ret = 0;
320
321	spear_rtc_clear_interrupt(config);
322
323	switch (enabled) {
324	case 0:
325		/* alarm off */
326		spear_rtc_disable_interrupt(config);
327		break;
328	case 1:
329		/* alarm on */
330		spear_rtc_enable_interrupt(config);
331		break;
332	default:
333		ret = -EINVAL;
334		break;
335	}
336
337	return ret;
338}
339
340static const struct rtc_class_ops spear_rtc_ops = {
341	.read_time = spear_rtc_read_time,
342	.set_time = spear_rtc_set_time,
343	.read_alarm = spear_rtc_read_alarm,
344	.set_alarm = spear_rtc_set_alarm,
345	.alarm_irq_enable = spear_alarm_irq_enable,
346};
347
348static int spear_rtc_probe(struct platform_device *pdev)
349{
350	struct resource *res;
351	struct spear_rtc_config *config;
352	int status = 0;
353	int irq;
354
355	config = devm_kzalloc(&pdev->dev, sizeof(*config), GFP_KERNEL);
356	if (!config)
357		return -ENOMEM;
358
359	/* alarm irqs */
360	irq = platform_get_irq(pdev, 0);
361	if (irq < 0)
362		return irq;
363
364	status = devm_request_irq(&pdev->dev, irq, spear_rtc_irq, 0, pdev->name,
365			config);
366	if (status) {
367		dev_err(&pdev->dev, "Alarm interrupt IRQ%d already claimed\n",
368				irq);
369		return status;
370	}
371
372	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
373	config->ioaddr = devm_ioremap_resource(&pdev->dev, res);
374	if (IS_ERR(config->ioaddr))
375		return PTR_ERR(config->ioaddr);
376
377	config->clk = devm_clk_get(&pdev->dev, NULL);
378	if (IS_ERR(config->clk))
379		return PTR_ERR(config->clk);
380
381	status = clk_prepare_enable(config->clk);
382	if (status < 0)
383		return status;
384
385	spin_lock_init(&config->lock);
386	platform_set_drvdata(pdev, config);
387
388	config->rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
389					&spear_rtc_ops, THIS_MODULE);
390	if (IS_ERR(config->rtc)) {
391		dev_err(&pdev->dev, "can't register RTC device, err %ld\n",
392				PTR_ERR(config->rtc));
393		status = PTR_ERR(config->rtc);
394		goto err_disable_clock;
395	}
396
397	config->rtc->uie_unsupported = 1;
398
399	if (!device_can_wakeup(&pdev->dev))
400		device_init_wakeup(&pdev->dev, 1);
401
402	return 0;
403
404err_disable_clock:
405	clk_disable_unprepare(config->clk);
406
407	return status;
408}
409
410static int spear_rtc_remove(struct platform_device *pdev)
411{
412	struct spear_rtc_config *config = platform_get_drvdata(pdev);
413
414	spear_rtc_disable_interrupt(config);
415	clk_disable_unprepare(config->clk);
416	device_init_wakeup(&pdev->dev, 0);
417
418	return 0;
419}
420
421#ifdef CONFIG_PM_SLEEP
422static int spear_rtc_suspend(struct device *dev)
423{
424	struct platform_device *pdev = to_platform_device(dev);
425	struct spear_rtc_config *config = platform_get_drvdata(pdev);
426	int irq;
427
428	irq = platform_get_irq(pdev, 0);
429	if (device_may_wakeup(&pdev->dev)) {
430		if (!enable_irq_wake(irq))
431			config->irq_wake = 1;
432	} else {
433		spear_rtc_disable_interrupt(config);
434		clk_disable(config->clk);
435	}
436
437	return 0;
438}
439
440static int spear_rtc_resume(struct device *dev)
441{
442	struct platform_device *pdev = to_platform_device(dev);
443	struct spear_rtc_config *config = platform_get_drvdata(pdev);
444	int irq;
445
446	irq = platform_get_irq(pdev, 0);
447
448	if (device_may_wakeup(&pdev->dev)) {
449		if (config->irq_wake) {
450			disable_irq_wake(irq);
451			config->irq_wake = 0;
452		}
453	} else {
454		clk_enable(config->clk);
455		spear_rtc_enable_interrupt(config);
456	}
457
458	return 0;
459}
460#endif
461
462static SIMPLE_DEV_PM_OPS(spear_rtc_pm_ops, spear_rtc_suspend, spear_rtc_resume);
463
464static void spear_rtc_shutdown(struct platform_device *pdev)
465{
466	struct spear_rtc_config *config = platform_get_drvdata(pdev);
467
468	spear_rtc_disable_interrupt(config);
469	clk_disable(config->clk);
470}
471
472#ifdef CONFIG_OF
473static const struct of_device_id spear_rtc_id_table[] = {
474	{ .compatible = "st,spear600-rtc" },
475	{}
476};
477MODULE_DEVICE_TABLE(of, spear_rtc_id_table);
478#endif
479
480static struct platform_driver spear_rtc_driver = {
481	.probe = spear_rtc_probe,
482	.remove = spear_rtc_remove,
483	.shutdown = spear_rtc_shutdown,
484	.driver = {
485		.name = "rtc-spear",
486		.pm = &spear_rtc_pm_ops,
487		.of_match_table = of_match_ptr(spear_rtc_id_table),
488	},
489};
490
491module_platform_driver(spear_rtc_driver);
492
493MODULE_ALIAS("platform:rtc-spear");
494MODULE_AUTHOR("Rajeev Kumar <rajeev-dlh.kumar@st.com>");
495MODULE_DESCRIPTION("ST SPEAr Realtime Clock Driver (RTC)");
496MODULE_LICENSE("GPL");
v5.14.15
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * drivers/rtc/rtc-spear.c
  4 *
  5 * Copyright (C) 2010 ST Microelectronics
  6 * Rajeev Kumar<rajeev-dlh.kumar@st.com>
 
 
 
 
  7 */
  8
  9#include <linux/bcd.h>
 10#include <linux/clk.h>
 11#include <linux/delay.h>
 12#include <linux/init.h>
 13#include <linux/io.h>
 14#include <linux/irq.h>
 15#include <linux/module.h>
 16#include <linux/of.h>
 17#include <linux/platform_device.h>
 18#include <linux/rtc.h>
 19#include <linux/slab.h>
 20#include <linux/spinlock.h>
 21
 22/* RTC registers */
 23#define TIME_REG		0x00
 24#define DATE_REG		0x04
 25#define ALARM_TIME_REG		0x08
 26#define ALARM_DATE_REG		0x0C
 27#define CTRL_REG		0x10
 28#define STATUS_REG		0x14
 29
 30/* TIME_REG & ALARM_TIME_REG */
 31#define SECONDS_UNITS		(0xf<<0)	/* seconds units position */
 32#define SECONDS_TENS		(0x7<<4)	/* seconds tens position */
 33#define MINUTES_UNITS		(0xf<<8)	/* minutes units position */
 34#define MINUTES_TENS		(0x7<<12)	/* minutes tens position */
 35#define HOURS_UNITS		(0xf<<16)	/* hours units position */
 36#define HOURS_TENS		(0x3<<20)	/* hours tens position */
 37
 38/* DATE_REG & ALARM_DATE_REG */
 39#define DAYS_UNITS		(0xf<<0)	/* days units position */
 40#define DAYS_TENS		(0x3<<4)	/* days tens position */
 41#define MONTHS_UNITS		(0xf<<8)	/* months units position */
 42#define MONTHS_TENS		(0x1<<12)	/* months tens position */
 43#define YEARS_UNITS		(0xf<<16)	/* years units position */
 44#define YEARS_TENS		(0xf<<20)	/* years tens position */
 45#define YEARS_HUNDREDS		(0xf<<24)	/* years hundereds position */
 46#define YEARS_MILLENIUMS	(0xf<<28)	/* years millenium position */
 47
 48/* MASK SHIFT TIME_REG & ALARM_TIME_REG*/
 49#define SECOND_SHIFT		0x00		/* seconds units */
 50#define MINUTE_SHIFT		0x08		/* minutes units position */
 51#define HOUR_SHIFT		0x10		/* hours units position */
 52#define MDAY_SHIFT		0x00		/* Month day shift */
 53#define MONTH_SHIFT		0x08		/* Month shift */
 54#define YEAR_SHIFT		0x10		/* Year shift */
 55
 56#define SECOND_MASK		0x7F
 57#define MIN_MASK		0x7F
 58#define HOUR_MASK		0x3F
 59#define DAY_MASK		0x3F
 60#define MONTH_MASK		0x7F
 61#define YEAR_MASK		0xFFFF
 62
 63/* date reg equal to time reg, for debug only */
 64#define TIME_BYP		(1<<9)
 65#define INT_ENABLE		(1<<31)		/* interrupt enable */
 66
 67/* STATUS_REG */
 68#define CLK_UNCONNECTED		(1<<0)
 69#define PEND_WR_TIME		(1<<2)
 70#define PEND_WR_DATE		(1<<3)
 71#define LOST_WR_TIME		(1<<4)
 72#define LOST_WR_DATE		(1<<5)
 73#define RTC_INT_MASK		(1<<31)
 74#define STATUS_BUSY		(PEND_WR_TIME | PEND_WR_DATE)
 75#define STATUS_FAIL		(LOST_WR_TIME | LOST_WR_DATE)
 76
 77struct spear_rtc_config {
 78	struct rtc_device *rtc;
 79	struct clk *clk;
 80	spinlock_t lock;
 81	void __iomem *ioaddr;
 82	unsigned int irq_wake;
 83};
 84
 85static inline void spear_rtc_clear_interrupt(struct spear_rtc_config *config)
 86{
 87	unsigned int val;
 88	unsigned long flags;
 89
 90	spin_lock_irqsave(&config->lock, flags);
 91	val = readl(config->ioaddr + STATUS_REG);
 92	val |= RTC_INT_MASK;
 93	writel(val, config->ioaddr + STATUS_REG);
 94	spin_unlock_irqrestore(&config->lock, flags);
 95}
 96
 97static inline void spear_rtc_enable_interrupt(struct spear_rtc_config *config)
 98{
 99	unsigned int val;
100
101	val = readl(config->ioaddr + CTRL_REG);
102	if (!(val & INT_ENABLE)) {
103		spear_rtc_clear_interrupt(config);
104		val |= INT_ENABLE;
105		writel(val, config->ioaddr + CTRL_REG);
106	}
107}
108
109static inline void spear_rtc_disable_interrupt(struct spear_rtc_config *config)
110{
111	unsigned int val;
112
113	val = readl(config->ioaddr + CTRL_REG);
114	if (val & INT_ENABLE) {
115		val &= ~INT_ENABLE;
116		writel(val, config->ioaddr + CTRL_REG);
117	}
118}
119
120static inline int is_write_complete(struct spear_rtc_config *config)
121{
122	int ret = 0;
123	unsigned long flags;
124
125	spin_lock_irqsave(&config->lock, flags);
126	if ((readl(config->ioaddr + STATUS_REG)) & STATUS_FAIL)
127		ret = -EIO;
128	spin_unlock_irqrestore(&config->lock, flags);
129
130	return ret;
131}
132
133static void rtc_wait_not_busy(struct spear_rtc_config *config)
134{
135	int status, count = 0;
136	unsigned long flags;
137
138	/* Assuming BUSY may stay active for 80 msec) */
139	for (count = 0; count < 80; count++) {
140		spin_lock_irqsave(&config->lock, flags);
141		status = readl(config->ioaddr + STATUS_REG);
142		spin_unlock_irqrestore(&config->lock, flags);
143		if ((status & STATUS_BUSY) == 0)
144			break;
145		/* check status busy, after each msec */
146		msleep(1);
147	}
148}
149
150static irqreturn_t spear_rtc_irq(int irq, void *dev_id)
151{
152	struct spear_rtc_config *config = dev_id;
153	unsigned long events = 0;
154	unsigned int irq_data;
155
156	spin_lock(&config->lock);
157	irq_data = readl(config->ioaddr + STATUS_REG);
158	spin_unlock(&config->lock);
159
160	if ((irq_data & RTC_INT_MASK)) {
161		spear_rtc_clear_interrupt(config);
162		events = RTC_IRQF | RTC_AF;
163		rtc_update_irq(config->rtc, 1, events);
164		return IRQ_HANDLED;
165	} else
166		return IRQ_NONE;
167
168}
169
170static void tm2bcd(struct rtc_time *tm)
171{
172	tm->tm_sec = bin2bcd(tm->tm_sec);
173	tm->tm_min = bin2bcd(tm->tm_min);
174	tm->tm_hour = bin2bcd(tm->tm_hour);
175	tm->tm_mday = bin2bcd(tm->tm_mday);
176	tm->tm_mon = bin2bcd(tm->tm_mon + 1);
177	tm->tm_year = bin2bcd(tm->tm_year);
178}
179
180static void bcd2tm(struct rtc_time *tm)
181{
182	tm->tm_sec = bcd2bin(tm->tm_sec);
183	tm->tm_min = bcd2bin(tm->tm_min);
184	tm->tm_hour = bcd2bin(tm->tm_hour);
185	tm->tm_mday = bcd2bin(tm->tm_mday);
186	tm->tm_mon = bcd2bin(tm->tm_mon) - 1;
187	/* epoch == 1900 */
188	tm->tm_year = bcd2bin(tm->tm_year);
189}
190
191/*
192 * spear_rtc_read_time - set the time
193 * @dev: rtc device in use
194 * @tm: holds date and time
195 *
196 * This function read time and date. On success it will return 0
197 * otherwise -ve error is returned.
198 */
199static int spear_rtc_read_time(struct device *dev, struct rtc_time *tm)
200{
201	struct spear_rtc_config *config = dev_get_drvdata(dev);
202	unsigned int time, date;
203
204	/* we don't report wday/yday/isdst ... */
205	rtc_wait_not_busy(config);
206
207	time = readl(config->ioaddr + TIME_REG);
208	date = readl(config->ioaddr + DATE_REG);
209	tm->tm_sec = (time >> SECOND_SHIFT) & SECOND_MASK;
210	tm->tm_min = (time >> MINUTE_SHIFT) & MIN_MASK;
211	tm->tm_hour = (time >> HOUR_SHIFT) & HOUR_MASK;
212	tm->tm_mday = (date >> MDAY_SHIFT) & DAY_MASK;
213	tm->tm_mon = (date >> MONTH_SHIFT) & MONTH_MASK;
214	tm->tm_year = (date >> YEAR_SHIFT) & YEAR_MASK;
215
216	bcd2tm(tm);
217	return 0;
218}
219
220/*
221 * spear_rtc_set_time - set the time
222 * @dev: rtc device in use
223 * @tm: holds date and time
224 *
225 * This function set time and date. On success it will return 0
226 * otherwise -ve error is returned.
227 */
228static int spear_rtc_set_time(struct device *dev, struct rtc_time *tm)
229{
230	struct spear_rtc_config *config = dev_get_drvdata(dev);
231	unsigned int time, date;
232
233	tm2bcd(tm);
234
235	rtc_wait_not_busy(config);
236	time = (tm->tm_sec << SECOND_SHIFT) | (tm->tm_min << MINUTE_SHIFT) |
237		(tm->tm_hour << HOUR_SHIFT);
238	date = (tm->tm_mday << MDAY_SHIFT) | (tm->tm_mon << MONTH_SHIFT) |
239		(tm->tm_year << YEAR_SHIFT);
240	writel(time, config->ioaddr + TIME_REG);
241	writel(date, config->ioaddr + DATE_REG);
242
243	return is_write_complete(config);
244}
245
246/*
247 * spear_rtc_read_alarm - read the alarm time
248 * @dev: rtc device in use
249 * @alm: holds alarm date and time
250 *
251 * This function read alarm time and date. On success it will return 0
252 * otherwise -ve error is returned.
253 */
254static int spear_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
255{
256	struct spear_rtc_config *config = dev_get_drvdata(dev);
257	unsigned int time, date;
258
259	rtc_wait_not_busy(config);
260
261	time = readl(config->ioaddr + ALARM_TIME_REG);
262	date = readl(config->ioaddr + ALARM_DATE_REG);
263	alm->time.tm_sec = (time >> SECOND_SHIFT) & SECOND_MASK;
264	alm->time.tm_min = (time >> MINUTE_SHIFT) & MIN_MASK;
265	alm->time.tm_hour = (time >> HOUR_SHIFT) & HOUR_MASK;
266	alm->time.tm_mday = (date >> MDAY_SHIFT) & DAY_MASK;
267	alm->time.tm_mon = (date >> MONTH_SHIFT) & MONTH_MASK;
268	alm->time.tm_year = (date >> YEAR_SHIFT) & YEAR_MASK;
269
270	bcd2tm(&alm->time);
271	alm->enabled = readl(config->ioaddr + CTRL_REG) & INT_ENABLE;
272
273	return 0;
274}
275
276/*
277 * spear_rtc_set_alarm - set the alarm time
278 * @dev: rtc device in use
279 * @alm: holds alarm date and time
280 *
281 * This function set alarm time and date. On success it will return 0
282 * otherwise -ve error is returned.
283 */
284static int spear_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
285{
286	struct spear_rtc_config *config = dev_get_drvdata(dev);
287	unsigned int time, date;
288	int err;
289
290	tm2bcd(&alm->time);
291
292	rtc_wait_not_busy(config);
293
294	time = (alm->time.tm_sec << SECOND_SHIFT) | (alm->time.tm_min <<
295			MINUTE_SHIFT) |	(alm->time.tm_hour << HOUR_SHIFT);
296	date = (alm->time.tm_mday << MDAY_SHIFT) | (alm->time.tm_mon <<
297			MONTH_SHIFT) | (alm->time.tm_year << YEAR_SHIFT);
298
299	writel(time, config->ioaddr + ALARM_TIME_REG);
300	writel(date, config->ioaddr + ALARM_DATE_REG);
301	err = is_write_complete(config);
302	if (err < 0)
303		return err;
304
305	if (alm->enabled)
306		spear_rtc_enable_interrupt(config);
307	else
308		spear_rtc_disable_interrupt(config);
309
310	return 0;
311}
312
313static int spear_alarm_irq_enable(struct device *dev, unsigned int enabled)
314{
315	struct spear_rtc_config *config = dev_get_drvdata(dev);
316	int ret = 0;
317
318	spear_rtc_clear_interrupt(config);
319
320	switch (enabled) {
321	case 0:
322		/* alarm off */
323		spear_rtc_disable_interrupt(config);
324		break;
325	case 1:
326		/* alarm on */
327		spear_rtc_enable_interrupt(config);
328		break;
329	default:
330		ret = -EINVAL;
331		break;
332	}
333
334	return ret;
335}
336
337static const struct rtc_class_ops spear_rtc_ops = {
338	.read_time = spear_rtc_read_time,
339	.set_time = spear_rtc_set_time,
340	.read_alarm = spear_rtc_read_alarm,
341	.set_alarm = spear_rtc_set_alarm,
342	.alarm_irq_enable = spear_alarm_irq_enable,
343};
344
345static int spear_rtc_probe(struct platform_device *pdev)
346{
 
347	struct spear_rtc_config *config;
348	int status = 0;
349	int irq;
350
351	config = devm_kzalloc(&pdev->dev, sizeof(*config), GFP_KERNEL);
352	if (!config)
353		return -ENOMEM;
354
355	/* alarm irqs */
356	irq = platform_get_irq(pdev, 0);
357	if (irq < 0)
358		return irq;
359
360	status = devm_request_irq(&pdev->dev, irq, spear_rtc_irq, 0, pdev->name,
361			config);
362	if (status) {
363		dev_err(&pdev->dev, "Alarm interrupt IRQ%d already claimed\n",
364				irq);
365		return status;
366	}
367
368	config->ioaddr = devm_platform_ioremap_resource(pdev, 0);
 
369	if (IS_ERR(config->ioaddr))
370		return PTR_ERR(config->ioaddr);
371
372	config->clk = devm_clk_get(&pdev->dev, NULL);
373	if (IS_ERR(config->clk))
374		return PTR_ERR(config->clk);
375
376	status = clk_prepare_enable(config->clk);
377	if (status < 0)
378		return status;
379
380	spin_lock_init(&config->lock);
381	platform_set_drvdata(pdev, config);
382
383	config->rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
384					&spear_rtc_ops, THIS_MODULE);
385	if (IS_ERR(config->rtc)) {
386		dev_err(&pdev->dev, "can't register RTC device, err %ld\n",
387				PTR_ERR(config->rtc));
388		status = PTR_ERR(config->rtc);
389		goto err_disable_clock;
390	}
391
392	config->rtc->uie_unsupported = 1;
393
394	if (!device_can_wakeup(&pdev->dev))
395		device_init_wakeup(&pdev->dev, 1);
396
397	return 0;
398
399err_disable_clock:
400	clk_disable_unprepare(config->clk);
401
402	return status;
403}
404
405static int spear_rtc_remove(struct platform_device *pdev)
406{
407	struct spear_rtc_config *config = platform_get_drvdata(pdev);
408
409	spear_rtc_disable_interrupt(config);
410	clk_disable_unprepare(config->clk);
411	device_init_wakeup(&pdev->dev, 0);
412
413	return 0;
414}
415
416#ifdef CONFIG_PM_SLEEP
417static int spear_rtc_suspend(struct device *dev)
418{
419	struct platform_device *pdev = to_platform_device(dev);
420	struct spear_rtc_config *config = platform_get_drvdata(pdev);
421	int irq;
422
423	irq = platform_get_irq(pdev, 0);
424	if (device_may_wakeup(&pdev->dev)) {
425		if (!enable_irq_wake(irq))
426			config->irq_wake = 1;
427	} else {
428		spear_rtc_disable_interrupt(config);
429		clk_disable(config->clk);
430	}
431
432	return 0;
433}
434
435static int spear_rtc_resume(struct device *dev)
436{
437	struct platform_device *pdev = to_platform_device(dev);
438	struct spear_rtc_config *config = platform_get_drvdata(pdev);
439	int irq;
440
441	irq = platform_get_irq(pdev, 0);
442
443	if (device_may_wakeup(&pdev->dev)) {
444		if (config->irq_wake) {
445			disable_irq_wake(irq);
446			config->irq_wake = 0;
447		}
448	} else {
449		clk_enable(config->clk);
450		spear_rtc_enable_interrupt(config);
451	}
452
453	return 0;
454}
455#endif
456
457static SIMPLE_DEV_PM_OPS(spear_rtc_pm_ops, spear_rtc_suspend, spear_rtc_resume);
458
459static void spear_rtc_shutdown(struct platform_device *pdev)
460{
461	struct spear_rtc_config *config = platform_get_drvdata(pdev);
462
463	spear_rtc_disable_interrupt(config);
464	clk_disable(config->clk);
465}
466
467#ifdef CONFIG_OF
468static const struct of_device_id spear_rtc_id_table[] = {
469	{ .compatible = "st,spear600-rtc" },
470	{}
471};
472MODULE_DEVICE_TABLE(of, spear_rtc_id_table);
473#endif
474
475static struct platform_driver spear_rtc_driver = {
476	.probe = spear_rtc_probe,
477	.remove = spear_rtc_remove,
478	.shutdown = spear_rtc_shutdown,
479	.driver = {
480		.name = "rtc-spear",
481		.pm = &spear_rtc_pm_ops,
482		.of_match_table = of_match_ptr(spear_rtc_id_table),
483	},
484};
485
486module_platform_driver(spear_rtc_driver);
487
488MODULE_ALIAS("platform:rtc-spear");
489MODULE_AUTHOR("Rajeev Kumar <rajeev-dlh.kumar@st.com>");
490MODULE_DESCRIPTION("ST SPEAr Realtime Clock Driver (RTC)");
491MODULE_LICENSE("GPL");