Linux Audio

Check our new training course

Loading...
v3.5.6
 
  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 int tm2bcd(struct rtc_time *tm)
174{
175	if (rtc_valid_tm(tm) != 0)
176		return -EINVAL;
177	tm->tm_sec = bin2bcd(tm->tm_sec);
178	tm->tm_min = bin2bcd(tm->tm_min);
179	tm->tm_hour = bin2bcd(tm->tm_hour);
180	tm->tm_mday = bin2bcd(tm->tm_mday);
181	tm->tm_mon = bin2bcd(tm->tm_mon + 1);
182	tm->tm_year = bin2bcd(tm->tm_year);
183
184	return 0;
185}
186
187static void bcd2tm(struct rtc_time *tm)
188{
189	tm->tm_sec = bcd2bin(tm->tm_sec);
190	tm->tm_min = bcd2bin(tm->tm_min);
191	tm->tm_hour = bcd2bin(tm->tm_hour);
192	tm->tm_mday = bcd2bin(tm->tm_mday);
193	tm->tm_mon = bcd2bin(tm->tm_mon) - 1;
194	/* epoch == 1900 */
195	tm->tm_year = bcd2bin(tm->tm_year);
196}
197
198/*
199 * spear_rtc_read_time - set the time
200 * @dev: rtc device in use
201 * @tm: holds date and time
202 *
203 * This function read time and date. On success it will return 0
204 * otherwise -ve error is returned.
205 */
206static int spear_rtc_read_time(struct device *dev, struct rtc_time *tm)
207{
208	struct spear_rtc_config *config = dev_get_drvdata(dev);
209	unsigned int time, date;
210
211	/* we don't report wday/yday/isdst ... */
212	rtc_wait_not_busy(config);
213
214	time = readl(config->ioaddr + TIME_REG);
215	date = readl(config->ioaddr + DATE_REG);
 
 
216	tm->tm_sec = (time >> SECOND_SHIFT) & SECOND_MASK;
217	tm->tm_min = (time >> MINUTE_SHIFT) & MIN_MASK;
218	tm->tm_hour = (time >> HOUR_SHIFT) & HOUR_MASK;
219	tm->tm_mday = (date >> MDAY_SHIFT) & DAY_MASK;
220	tm->tm_mon = (date >> MONTH_SHIFT) & MONTH_MASK;
221	tm->tm_year = (date >> YEAR_SHIFT) & YEAR_MASK;
222
223	bcd2tm(tm);
224	return 0;
225}
226
227/*
228 * spear_rtc_set_time - set the time
229 * @dev: rtc device in use
230 * @tm: holds date and time
231 *
232 * This function set time and date. On success it will return 0
233 * otherwise -ve error is returned.
234 */
235static int spear_rtc_set_time(struct device *dev, struct rtc_time *tm)
236{
237	struct spear_rtc_config *config = dev_get_drvdata(dev);
238	unsigned int time, date, err = 0;
239
240	if (tm2bcd(tm) < 0)
241		return -EINVAL;
242
243	rtc_wait_not_busy(config);
244	time = (tm->tm_sec << SECOND_SHIFT) | (tm->tm_min << MINUTE_SHIFT) |
245		(tm->tm_hour << HOUR_SHIFT);
246	date = (tm->tm_mday << MDAY_SHIFT) | (tm->tm_mon << MONTH_SHIFT) |
247		(tm->tm_year << YEAR_SHIFT);
248	writel(time, config->ioaddr + TIME_REG);
249	writel(date, config->ioaddr + DATE_REG);
250	err = is_write_complete(config);
251	if (err < 0)
252		return err;
253
254	return 0;
255}
256
257/*
258 * spear_rtc_read_alarm - read the alarm time
259 * @dev: rtc device in use
260 * @alm: holds alarm date and time
261 *
262 * This function read alarm time and date. On success it will return 0
263 * otherwise -ve error is returned.
264 */
265static int spear_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
266{
267	struct spear_rtc_config *config = dev_get_drvdata(dev);
268	unsigned int time, date;
269
270	rtc_wait_not_busy(config);
271
272	time = readl(config->ioaddr + ALARM_TIME_REG);
273	date = readl(config->ioaddr + ALARM_DATE_REG);
274	alm->time.tm_sec = (time >> SECOND_SHIFT) & SECOND_MASK;
275	alm->time.tm_min = (time >> MINUTE_SHIFT) & MIN_MASK;
276	alm->time.tm_hour = (time >> HOUR_SHIFT) & HOUR_MASK;
277	alm->time.tm_mday = (date >> MDAY_SHIFT) & DAY_MASK;
278	alm->time.tm_mon = (date >> MONTH_SHIFT) & MONTH_MASK;
279	alm->time.tm_year = (date >> YEAR_SHIFT) & YEAR_MASK;
280
281	bcd2tm(&alm->time);
282	alm->enabled = readl(config->ioaddr + CTRL_REG) & INT_ENABLE;
283
284	return 0;
285}
286
287/*
288 * spear_rtc_set_alarm - set the alarm time
289 * @dev: rtc device in use
290 * @alm: holds alarm date and time
291 *
292 * This function set alarm time and date. On success it will return 0
293 * otherwise -ve error is returned.
294 */
295static int spear_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
296{
297	struct spear_rtc_config *config = dev_get_drvdata(dev);
298	unsigned int time, date, err = 0;
 
299
300	if (tm2bcd(&alm->time) < 0)
301		return -EINVAL;
302
303	rtc_wait_not_busy(config);
304
305	time = (alm->time.tm_sec << SECOND_SHIFT) | (alm->time.tm_min <<
306			MINUTE_SHIFT) |	(alm->time.tm_hour << HOUR_SHIFT);
307	date = (alm->time.tm_mday << MDAY_SHIFT) | (alm->time.tm_mon <<
308			MONTH_SHIFT) | (alm->time.tm_year << YEAR_SHIFT);
309
310	writel(time, config->ioaddr + ALARM_TIME_REG);
311	writel(date, config->ioaddr + ALARM_DATE_REG);
312	err = is_write_complete(config);
313	if (err < 0)
314		return err;
315
316	if (alm->enabled)
317		spear_rtc_enable_interrupt(config);
318	else
319		spear_rtc_disable_interrupt(config);
320
321	return 0;
322}
323
324static int spear_alarm_irq_enable(struct device *dev, unsigned int enabled)
325{
326	struct spear_rtc_config *config = dev_get_drvdata(dev);
327	int ret = 0;
328
329	spear_rtc_clear_interrupt(config);
330
331	switch (enabled) {
332	case 0:
333		/* alarm off */
334		spear_rtc_disable_interrupt(config);
335		break;
336	case 1:
337		/* alarm on */
338		spear_rtc_enable_interrupt(config);
339		break;
340	default:
341		ret = -EINVAL;
342		break;
343	}
344
345	return ret;
346}
347
348static struct rtc_class_ops spear_rtc_ops = {
349	.read_time = spear_rtc_read_time,
350	.set_time = spear_rtc_set_time,
351	.read_alarm = spear_rtc_read_alarm,
352	.set_alarm = spear_rtc_set_alarm,
353	.alarm_irq_enable = spear_alarm_irq_enable,
354};
355
356static int __devinit spear_rtc_probe(struct platform_device *pdev)
357{
358	struct resource *res;
359	struct spear_rtc_config *config;
360	unsigned int status = 0;
361	int irq;
362
363	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
364	if (!res) {
365		dev_err(&pdev->dev, "no resource defined\n");
366		return -EBUSY;
367	}
368	if (!request_mem_region(res->start, resource_size(res), pdev->name)) {
369		dev_err(&pdev->dev, "rtc region already claimed\n");
370		return -EBUSY;
371	}
372
373	config = kzalloc(sizeof(*config), GFP_KERNEL);
374	if (!config) {
375		dev_err(&pdev->dev, "out of memory\n");
376		status = -ENOMEM;
377		goto err_release_region;
378	}
379
380	config->clk = clk_get(&pdev->dev, NULL);
381	if (IS_ERR(config->clk)) {
382		status = PTR_ERR(config->clk);
383		goto err_kfree;
 
 
384	}
385
386	status = clk_enable(config->clk);
387	if (status < 0)
388		goto err_clk_put;
389
390	config->ioaddr = ioremap(res->start, resource_size(res));
391	if (!config->ioaddr) {
392		dev_err(&pdev->dev, "ioremap fail\n");
393		status = -ENOMEM;
394		goto err_disable_clock;
395	}
 
396
397	spin_lock_init(&config->lock);
398	platform_set_drvdata(pdev, config);
399
400	config->rtc = rtc_device_register(pdev->name, &pdev->dev,
401			&spear_rtc_ops, THIS_MODULE);
402	if (IS_ERR(config->rtc)) {
403		dev_err(&pdev->dev, "can't register RTC device, err %ld\n",
404				PTR_ERR(config->rtc));
405		status = PTR_ERR(config->rtc);
406		goto err_iounmap;
407	}
408
409	/* alarm irqs */
410	irq = platform_get_irq(pdev, 0);
411	if (irq < 0) {
412		dev_err(&pdev->dev, "no update irq?\n");
413		status = irq;
414		goto err_clear_platdata;
415	}
416
417	status = request_irq(irq, spear_rtc_irq, 0, pdev->name, config);
418	if (status) {
419		dev_err(&pdev->dev, "Alarm interrupt IRQ%d already \
420				claimed\n", irq);
421		goto err_clear_platdata;
422	}
423
424	if (!device_can_wakeup(&pdev->dev))
425		device_init_wakeup(&pdev->dev, 1);
426
427	return 0;
428
429err_clear_platdata:
430	platform_set_drvdata(pdev, NULL);
431	rtc_device_unregister(config->rtc);
432err_iounmap:
433	iounmap(config->ioaddr);
434err_disable_clock:
435	clk_disable(config->clk);
436err_clk_put:
437	clk_put(config->clk);
438err_kfree:
439	kfree(config);
440err_release_region:
441	release_mem_region(res->start, resource_size(res));
442
443	return status;
444}
445
446static int __devexit spear_rtc_remove(struct platform_device *pdev)
447{
448	struct spear_rtc_config *config = platform_get_drvdata(pdev);
449	int irq;
450	struct resource *res;
451
452	/* leave rtc running, but disable irqs */
453	spear_rtc_disable_interrupt(config);
 
454	device_init_wakeup(&pdev->dev, 0);
455	irq = platform_get_irq(pdev, 0);
456	if (irq)
457		free_irq(irq, pdev);
458	clk_disable(config->clk);
459	clk_put(config->clk);
460	iounmap(config->ioaddr);
461	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
462	if (res)
463		release_mem_region(res->start, resource_size(res));
464	platform_set_drvdata(pdev, NULL);
465	rtc_device_unregister(config->rtc);
466	kfree(config);
467
468	return 0;
469}
470
471#ifdef CONFIG_PM
472
473static int spear_rtc_suspend(struct platform_device *pdev, pm_message_t state)
474{
 
475	struct spear_rtc_config *config = platform_get_drvdata(pdev);
476	int irq;
477
478	irq = platform_get_irq(pdev, 0);
479	if (device_may_wakeup(&pdev->dev)) {
480		if (!enable_irq_wake(irq))
481			config->irq_wake = 1;
482	} else {
483		spear_rtc_disable_interrupt(config);
484		clk_disable(config->clk);
485	}
486
487	return 0;
488}
489
490static int spear_rtc_resume(struct platform_device *pdev)
491{
 
492	struct spear_rtc_config *config = platform_get_drvdata(pdev);
493	int irq;
494
495	irq = platform_get_irq(pdev, 0);
496
497	if (device_may_wakeup(&pdev->dev)) {
498		if (config->irq_wake) {
499			disable_irq_wake(irq);
500			config->irq_wake = 0;
501		}
502	} else {
503		clk_enable(config->clk);
504		spear_rtc_enable_interrupt(config);
505	}
506
507	return 0;
508}
509
510#else
511#define spear_rtc_suspend	NULL
512#define spear_rtc_resume	NULL
513#endif
514
 
 
515static void spear_rtc_shutdown(struct platform_device *pdev)
516{
517	struct spear_rtc_config *config = platform_get_drvdata(pdev);
518
519	spear_rtc_disable_interrupt(config);
520	clk_disable(config->clk);
521}
522
523#ifdef CONFIG_OF
524static const struct of_device_id spear_rtc_id_table[] = {
525	{ .compatible = "st,spear600-rtc" },
526	{}
527};
528MODULE_DEVICE_TABLE(of, spear_rtc_id_table);
529#endif
530
531static struct platform_driver spear_rtc_driver = {
532	.probe = spear_rtc_probe,
533	.remove = __devexit_p(spear_rtc_remove),
534	.suspend = spear_rtc_suspend,
535	.resume = spear_rtc_resume,
536	.shutdown = spear_rtc_shutdown,
537	.driver = {
538		.name = "rtc-spear",
 
539		.of_match_table = of_match_ptr(spear_rtc_id_table),
540	},
541};
542
543module_platform_driver(spear_rtc_driver);
544
545MODULE_ALIAS("platform:rtc-spear");
546MODULE_AUTHOR("Rajeev Kumar <rajeev-dlh.kumar@st.com>");
547MODULE_DESCRIPTION("ST SPEAr Realtime Clock Driver (RTC)");
548MODULE_LICENSE("GPL");
v6.13.7
  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	do {
208		time = readl(config->ioaddr + TIME_REG);
209		date = readl(config->ioaddr + DATE_REG);
210	} while (time == readl(config->ioaddr + TIME_REG));
211	tm->tm_sec = (time >> SECOND_SHIFT) & SECOND_MASK;
212	tm->tm_min = (time >> MINUTE_SHIFT) & MIN_MASK;
213	tm->tm_hour = (time >> HOUR_SHIFT) & HOUR_MASK;
214	tm->tm_mday = (date >> MDAY_SHIFT) & DAY_MASK;
215	tm->tm_mon = (date >> MONTH_SHIFT) & MONTH_MASK;
216	tm->tm_year = (date >> YEAR_SHIFT) & YEAR_MASK;
217
218	bcd2tm(tm);
219	return 0;
220}
221
222/*
223 * spear_rtc_set_time - set the time
224 * @dev: rtc device in use
225 * @tm: holds date and time
226 *
227 * This function set time and date. On success it will return 0
228 * otherwise -ve error is returned.
229 */
230static int spear_rtc_set_time(struct device *dev, struct rtc_time *tm)
231{
232	struct spear_rtc_config *config = dev_get_drvdata(dev);
233	unsigned int time, date;
234
235	tm2bcd(tm);
 
236
237	rtc_wait_not_busy(config);
238	time = (tm->tm_sec << SECOND_SHIFT) | (tm->tm_min << MINUTE_SHIFT) |
239		(tm->tm_hour << HOUR_SHIFT);
240	date = (tm->tm_mday << MDAY_SHIFT) | (tm->tm_mon << MONTH_SHIFT) |
241		(tm->tm_year << YEAR_SHIFT);
242	writel(time, config->ioaddr + TIME_REG);
243	writel(date, config->ioaddr + DATE_REG);
 
 
 
244
245	return is_write_complete(config);
246}
247
248/*
249 * spear_rtc_read_alarm - read the alarm time
250 * @dev: rtc device in use
251 * @alm: holds alarm date and time
252 *
253 * This function read alarm time and date. On success it will return 0
254 * otherwise -ve error is returned.
255 */
256static int spear_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
257{
258	struct spear_rtc_config *config = dev_get_drvdata(dev);
259	unsigned int time, date;
260
261	rtc_wait_not_busy(config);
262
263	time = readl(config->ioaddr + ALARM_TIME_REG);
264	date = readl(config->ioaddr + ALARM_DATE_REG);
265	alm->time.tm_sec = (time >> SECOND_SHIFT) & SECOND_MASK;
266	alm->time.tm_min = (time >> MINUTE_SHIFT) & MIN_MASK;
267	alm->time.tm_hour = (time >> HOUR_SHIFT) & HOUR_MASK;
268	alm->time.tm_mday = (date >> MDAY_SHIFT) & DAY_MASK;
269	alm->time.tm_mon = (date >> MONTH_SHIFT) & MONTH_MASK;
270	alm->time.tm_year = (date >> YEAR_SHIFT) & YEAR_MASK;
271
272	bcd2tm(&alm->time);
273	alm->enabled = readl(config->ioaddr + CTRL_REG) & INT_ENABLE;
274
275	return 0;
276}
277
278/*
279 * spear_rtc_set_alarm - set the alarm time
280 * @dev: rtc device in use
281 * @alm: holds alarm date and time
282 *
283 * This function set alarm time and date. On success it will return 0
284 * otherwise -ve error is returned.
285 */
286static int spear_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
287{
288	struct spear_rtc_config *config = dev_get_drvdata(dev);
289	unsigned int time, date;
290	int err;
291
292	tm2bcd(&alm->time);
 
293
294	rtc_wait_not_busy(config);
295
296	time = (alm->time.tm_sec << SECOND_SHIFT) | (alm->time.tm_min <<
297			MINUTE_SHIFT) |	(alm->time.tm_hour << HOUR_SHIFT);
298	date = (alm->time.tm_mday << MDAY_SHIFT) | (alm->time.tm_mon <<
299			MONTH_SHIFT) | (alm->time.tm_year << YEAR_SHIFT);
300
301	writel(time, config->ioaddr + ALARM_TIME_REG);
302	writel(date, config->ioaddr + ALARM_DATE_REG);
303	err = is_write_complete(config);
304	if (err < 0)
305		return err;
306
307	if (alm->enabled)
308		spear_rtc_enable_interrupt(config);
309	else
310		spear_rtc_disable_interrupt(config);
311
312	return 0;
313}
314
315static int spear_alarm_irq_enable(struct device *dev, unsigned int enabled)
316{
317	struct spear_rtc_config *config = dev_get_drvdata(dev);
318	int ret = 0;
319
320	spear_rtc_clear_interrupt(config);
321
322	switch (enabled) {
323	case 0:
324		/* alarm off */
325		spear_rtc_disable_interrupt(config);
326		break;
327	case 1:
328		/* alarm on */
329		spear_rtc_enable_interrupt(config);
330		break;
331	default:
332		ret = -EINVAL;
333		break;
334	}
335
336	return ret;
337}
338
339static const struct rtc_class_ops spear_rtc_ops = {
340	.read_time = spear_rtc_read_time,
341	.set_time = spear_rtc_set_time,
342	.read_alarm = spear_rtc_read_alarm,
343	.set_alarm = spear_rtc_set_alarm,
344	.alarm_irq_enable = spear_alarm_irq_enable,
345};
346
347static int spear_rtc_probe(struct platform_device *pdev)
348{
 
349	struct spear_rtc_config *config;
350	int status = 0;
351	int irq;
352
353	config = devm_kzalloc(&pdev->dev, sizeof(*config), GFP_KERNEL);
354	if (!config)
355		return -ENOMEM;
356
357	config->rtc = devm_rtc_allocate_device(&pdev->dev);
358	if (IS_ERR(config->rtc))
359		return PTR_ERR(config->rtc);
 
 
360
361	/* alarm irqs */
362	irq = platform_get_irq(pdev, 0);
363	if (irq < 0)
364		return irq;
 
 
365
366	status = devm_request_irq(&pdev->dev, irq, spear_rtc_irq, 0, pdev->name,
367			config);
368	if (status) {
369		dev_err(&pdev->dev, "Alarm interrupt IRQ%d already claimed\n",
370				irq);
371		return status;
372	}
373
374	config->ioaddr = devm_platform_ioremap_resource(pdev, 0);
375	if (IS_ERR(config->ioaddr))
376		return PTR_ERR(config->ioaddr);
377
378	config->clk = devm_clk_get(&pdev->dev, NULL);
379	if (IS_ERR(config->clk))
380		return PTR_ERR(config->clk);
381
382	status = clk_prepare_enable(config->clk);
383	if (status < 0)
384		return status;
385
386	spin_lock_init(&config->lock);
387	platform_set_drvdata(pdev, config);
388
389	config->rtc->ops = &spear_rtc_ops;
390	config->rtc->range_min = RTC_TIMESTAMP_BEGIN_0000;
391	config->rtc->range_max = RTC_TIMESTAMP_END_9999;
 
 
 
 
 
 
 
 
 
 
 
 
 
392
393	status = devm_rtc_register_device(config->rtc);
394	if (status)
395		goto err_disable_clock;
 
 
 
396
397	if (!device_can_wakeup(&pdev->dev))
398		device_init_wakeup(&pdev->dev, 1);
399
400	return 0;
401
 
 
 
 
 
402err_disable_clock:
403	clk_disable_unprepare(config->clk);
 
 
 
 
 
 
404
405	return status;
406}
407
408static void spear_rtc_remove(struct platform_device *pdev)
409{
410	struct spear_rtc_config *config = platform_get_drvdata(pdev);
 
 
411
 
412	spear_rtc_disable_interrupt(config);
413	clk_disable_unprepare(config->clk);
414	device_init_wakeup(&pdev->dev, 0);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
415}
416
417#ifdef CONFIG_PM_SLEEP
418static int spear_rtc_suspend(struct device *dev)
 
419{
420	struct platform_device *pdev = to_platform_device(dev);
421	struct spear_rtc_config *config = platform_get_drvdata(pdev);
422	int irq;
423
424	irq = platform_get_irq(pdev, 0);
425	if (device_may_wakeup(&pdev->dev)) {
426		if (!enable_irq_wake(irq))
427			config->irq_wake = 1;
428	} else {
429		spear_rtc_disable_interrupt(config);
430		clk_disable(config->clk);
431	}
432
433	return 0;
434}
435
436static int spear_rtc_resume(struct device *dev)
437{
438	struct platform_device *pdev = to_platform_device(dev);
439	struct spear_rtc_config *config = platform_get_drvdata(pdev);
440	int irq;
441
442	irq = platform_get_irq(pdev, 0);
443
444	if (device_may_wakeup(&pdev->dev)) {
445		if (config->irq_wake) {
446			disable_irq_wake(irq);
447			config->irq_wake = 0;
448		}
449	} else {
450		clk_enable(config->clk);
451		spear_rtc_enable_interrupt(config);
452	}
453
454	return 0;
455}
 
 
 
 
456#endif
457
458static SIMPLE_DEV_PM_OPS(spear_rtc_pm_ops, spear_rtc_suspend, spear_rtc_resume);
459
460static void spear_rtc_shutdown(struct platform_device *pdev)
461{
462	struct spear_rtc_config *config = platform_get_drvdata(pdev);
463
464	spear_rtc_disable_interrupt(config);
465	clk_disable(config->clk);
466}
467
468#ifdef CONFIG_OF
469static const struct of_device_id spear_rtc_id_table[] = {
470	{ .compatible = "st,spear600-rtc" },
471	{}
472};
473MODULE_DEVICE_TABLE(of, spear_rtc_id_table);
474#endif
475
476static struct platform_driver spear_rtc_driver = {
477	.probe = spear_rtc_probe,
478	.remove = spear_rtc_remove,
 
 
479	.shutdown = spear_rtc_shutdown,
480	.driver = {
481		.name = "rtc-spear",
482		.pm = &spear_rtc_pm_ops,
483		.of_match_table = of_match_ptr(spear_rtc_id_table),
484	},
485};
486
487module_platform_driver(spear_rtc_driver);
488
489MODULE_ALIAS("platform:rtc-spear");
490MODULE_AUTHOR("Rajeev Kumar <rajeev-dlh.kumar@st.com>");
491MODULE_DESCRIPTION("ST SPEAr Realtime Clock Driver (RTC)");
492MODULE_LICENSE("GPL");