| Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 1 | /* | 
 | 2 |  * SuperH On-Chip RTC Support | 
 | 3 |  * | 
| Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 4 |  * Copyright (C) 2006, 2007, 2008  Paul Mundt | 
| Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 5 |  * Copyright (C) 2006  Jamie Lenehan | 
| Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 6 |  * Copyright (C) 2008  Angelo Castello | 
| Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 7 |  * | 
 | 8 |  * Based on the old arch/sh/kernel/cpu/rtc.c by: | 
 | 9 |  * | 
 | 10 |  *  Copyright (C) 2000  Philipp Rumpf <prumpf@tux.org> | 
 | 11 |  *  Copyright (C) 1999  Tetsuya Okada & Niibe Yutaka | 
 | 12 |  * | 
 | 13 |  * This file is subject to the terms and conditions of the GNU General Public | 
 | 14 |  * License.  See the file "COPYING" in the main directory of this archive | 
 | 15 |  * for more details. | 
 | 16 |  */ | 
 | 17 | #include <linux/module.h> | 
 | 18 | #include <linux/kernel.h> | 
 | 19 | #include <linux/bcd.h> | 
 | 20 | #include <linux/rtc.h> | 
 | 21 | #include <linux/init.h> | 
 | 22 | #include <linux/platform_device.h> | 
 | 23 | #include <linux/seq_file.h> | 
 | 24 | #include <linux/interrupt.h> | 
 | 25 | #include <linux/spinlock.h> | 
| Jamie Lenehan | 31ccb08 | 2006-12-07 17:23:50 +0900 | [diff] [blame] | 26 | #include <linux/io.h> | 
| Jonathan Cameron | 5d2a503 | 2009-01-06 14:42:12 -0800 | [diff] [blame] | 27 | #include <linux/log2.h> | 
| Paul Mundt | ad89f87 | 2007-08-03 14:19:58 +0900 | [diff] [blame] | 28 | #include <asm/rtc.h> | 
| Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 29 |  | 
| Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 30 | #define DRV_NAME	"sh-rtc" | 
| Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 31 | #define DRV_VERSION	"0.2.0" | 
| Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 32 |  | 
 | 33 | #define RTC_REG(r)	((r) * rtc_reg_size) | 
 | 34 |  | 
| Jamie Lenehan | 31ccb08 | 2006-12-07 17:23:50 +0900 | [diff] [blame] | 35 | #define R64CNT		RTC_REG(0) | 
| Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 36 |  | 
 | 37 | #define RSECCNT		RTC_REG(1)	/* RTC sec */ | 
 | 38 | #define RMINCNT		RTC_REG(2)	/* RTC min */ | 
 | 39 | #define RHRCNT		RTC_REG(3)	/* RTC hour */ | 
 | 40 | #define RWKCNT		RTC_REG(4)	/* RTC week */ | 
 | 41 | #define RDAYCNT		RTC_REG(5)	/* RTC day */ | 
 | 42 | #define RMONCNT		RTC_REG(6)	/* RTC month */ | 
 | 43 | #define RYRCNT		RTC_REG(7)	/* RTC year */ | 
 | 44 | #define RSECAR		RTC_REG(8)	/* ALARM sec */ | 
 | 45 | #define RMINAR		RTC_REG(9)	/* ALARM min */ | 
 | 46 | #define RHRAR		RTC_REG(10)	/* ALARM hour */ | 
 | 47 | #define RWKAR		RTC_REG(11)	/* ALARM week */ | 
 | 48 | #define RDAYAR		RTC_REG(12)	/* ALARM day */ | 
 | 49 | #define RMONAR		RTC_REG(13)	/* ALARM month */ | 
 | 50 | #define RCR1		RTC_REG(14)	/* Control */ | 
 | 51 | #define RCR2		RTC_REG(15)	/* Control */ | 
 | 52 |  | 
| Paul Mundt | ff1b750 | 2007-11-26 17:56:31 +0900 | [diff] [blame] | 53 | /* | 
 | 54 |  * Note on RYRAR and RCR3: Up until this point most of the register | 
 | 55 |  * definitions are consistent across all of the available parts. However, | 
 | 56 |  * the placement of the optional RYRAR and RCR3 (the RYRAR control | 
 | 57 |  * register used to control RYRCNT/RYRAR compare) varies considerably | 
 | 58 |  * across various parts, occasionally being mapped in to a completely | 
 | 59 |  * unrelated address space. For proper RYRAR support a separate resource | 
 | 60 |  * would have to be handed off, but as this is purely optional in | 
 | 61 |  * practice, we simply opt not to support it, thereby keeping the code | 
 | 62 |  * quite a bit more simplified. | 
 | 63 |  */ | 
 | 64 |  | 
| Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 65 | /* ALARM Bits - or with BCD encoded value */ | 
 | 66 | #define AR_ENB		0x80	/* Enable for alarm cmp   */ | 
| Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 67 |  | 
| Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 68 | /* Period Bits */ | 
 | 69 | #define PF_HP		0x100	/* Enable Half Period to support 8,32,128Hz */ | 
 | 70 | #define PF_COUNT	0x200	/* Half periodic counter */ | 
 | 71 | #define PF_OXS		0x400	/* Periodic One x Second */ | 
 | 72 | #define PF_KOU		0x800	/* Kernel or User periodic request 1=kernel */ | 
 | 73 | #define PF_MASK		0xf00 | 
 | 74 |  | 
| Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 75 | /* RCR1 Bits */ | 
 | 76 | #define RCR1_CF		0x80	/* Carry Flag             */ | 
 | 77 | #define RCR1_CIE	0x10	/* Carry Interrupt Enable */ | 
 | 78 | #define RCR1_AIE	0x08	/* Alarm Interrupt Enable */ | 
 | 79 | #define RCR1_AF		0x01	/* Alarm Flag             */ | 
 | 80 |  | 
 | 81 | /* RCR2 Bits */ | 
 | 82 | #define RCR2_PEF	0x80	/* PEriodic interrupt Flag */ | 
 | 83 | #define RCR2_PESMASK	0x70	/* Periodic interrupt Set  */ | 
 | 84 | #define RCR2_RTCEN	0x08	/* ENable RTC              */ | 
 | 85 | #define RCR2_ADJ	0x04	/* ADJustment (30-second)  */ | 
 | 86 | #define RCR2_RESET	0x02	/* Reset bit               */ | 
 | 87 | #define RCR2_START	0x01	/* Start bit               */ | 
 | 88 |  | 
 | 89 | struct sh_rtc { | 
 | 90 | 	void __iomem *regbase; | 
 | 91 | 	unsigned long regsize; | 
 | 92 | 	struct resource *res; | 
| Anton Vorontsov | 2fac667 | 2009-01-06 14:42:11 -0800 | [diff] [blame] | 93 | 	int alarm_irq; | 
 | 94 | 	int periodic_irq; | 
 | 95 | 	int carry_irq; | 
| Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 96 | 	struct rtc_device *rtc_dev; | 
 | 97 | 	spinlock_t lock; | 
| Paul Mundt | ad89f87 | 2007-08-03 14:19:58 +0900 | [diff] [blame] | 98 | 	unsigned long capabilities;	/* See asm-sh/rtc.h for cap bits */ | 
| Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 99 | 	unsigned short periodic_freq; | 
| Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 100 | }; | 
 | 101 |  | 
| Jamie Lenehan | 31ccb08 | 2006-12-07 17:23:50 +0900 | [diff] [blame] | 102 | static irqreturn_t sh_rtc_interrupt(int irq, void *dev_id) | 
| Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 103 | { | 
| Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 104 | 	struct sh_rtc *rtc = dev_id; | 
 | 105 | 	unsigned int tmp; | 
| Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 106 |  | 
 | 107 | 	spin_lock(&rtc->lock); | 
 | 108 |  | 
 | 109 | 	tmp = readb(rtc->regbase + RCR1); | 
| Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 110 | 	tmp &= ~RCR1_CF; | 
| Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 111 | 	writeb(tmp, rtc->regbase + RCR1); | 
 | 112 |  | 
| Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 113 | 	/* Users have requested One x Second IRQ */ | 
 | 114 | 	if (rtc->periodic_freq & PF_OXS) | 
 | 115 | 		rtc_update_irq(rtc->rtc_dev, 1, RTC_UF | RTC_IRQF); | 
| Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 116 |  | 
 | 117 | 	spin_unlock(&rtc->lock); | 
 | 118 |  | 
 | 119 | 	return IRQ_HANDLED; | 
 | 120 | } | 
 | 121 |  | 
| Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 122 | static irqreturn_t sh_rtc_alarm(int irq, void *dev_id) | 
 | 123 | { | 
| Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 124 | 	struct sh_rtc *rtc = dev_id; | 
 | 125 | 	unsigned int tmp; | 
| Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 126 |  | 
 | 127 | 	spin_lock(&rtc->lock); | 
 | 128 |  | 
 | 129 | 	tmp = readb(rtc->regbase + RCR1); | 
| Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 130 | 	tmp &= ~(RCR1_AF | RCR1_AIE); | 
| Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 131 | 		writeb(tmp, rtc->regbase + RCR1); | 
 | 132 |  | 
| Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 133 | 	rtc_update_irq(rtc->rtc_dev, 1, RTC_AF | RTC_IRQF); | 
| Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 134 |  | 
 | 135 | 	spin_unlock(&rtc->lock); | 
| Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 136 |  | 
| Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 137 | 	return IRQ_HANDLED; | 
 | 138 | } | 
 | 139 |  | 
| Jamie Lenehan | 31ccb08 | 2006-12-07 17:23:50 +0900 | [diff] [blame] | 140 | static irqreturn_t sh_rtc_periodic(int irq, void *dev_id) | 
| Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 141 | { | 
| Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 142 | 	struct sh_rtc *rtc = dev_id; | 
 | 143 | 	struct rtc_device *rtc_dev = rtc->rtc_dev; | 
 | 144 | 	unsigned int tmp; | 
| Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 145 |  | 
 | 146 | 	spin_lock(&rtc->lock); | 
 | 147 |  | 
| Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 148 | 	tmp = readb(rtc->regbase + RCR2); | 
 | 149 | 	tmp &= ~RCR2_PEF; | 
 | 150 | 	writeb(tmp, rtc->regbase + RCR2); | 
 | 151 |  | 
 | 152 | 	/* Half period enabled than one skipped and the next notified */ | 
 | 153 | 	if ((rtc->periodic_freq & PF_HP) && (rtc->periodic_freq & PF_COUNT)) | 
 | 154 | 		rtc->periodic_freq &= ~PF_COUNT; | 
 | 155 | 	else { | 
 | 156 | 		if (rtc->periodic_freq & PF_HP) | 
 | 157 | 			rtc->periodic_freq |= PF_COUNT; | 
 | 158 | 		if (rtc->periodic_freq & PF_KOU) { | 
 | 159 | 			spin_lock(&rtc_dev->irq_task_lock); | 
 | 160 | 			if (rtc_dev->irq_task) | 
 | 161 | 				rtc_dev->irq_task->func(rtc_dev->irq_task->private_data); | 
 | 162 | 			spin_unlock(&rtc_dev->irq_task_lock); | 
 | 163 | 		} else | 
 | 164 | 			rtc_update_irq(rtc->rtc_dev, 1, RTC_PF | RTC_IRQF); | 
 | 165 | 	} | 
| Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 166 |  | 
 | 167 | 	spin_unlock(&rtc->lock); | 
 | 168 |  | 
 | 169 | 	return IRQ_HANDLED; | 
 | 170 | } | 
 | 171 |  | 
 | 172 | static inline void sh_rtc_setpie(struct device *dev, unsigned int enable) | 
 | 173 | { | 
 | 174 | 	struct sh_rtc *rtc = dev_get_drvdata(dev); | 
 | 175 | 	unsigned int tmp; | 
 | 176 |  | 
 | 177 | 	spin_lock_irq(&rtc->lock); | 
 | 178 |  | 
 | 179 | 	tmp = readb(rtc->regbase + RCR2); | 
 | 180 |  | 
 | 181 | 	if (enable) { | 
| Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 182 | 		tmp &= ~RCR2_PEF;	/* Clear PES bit */ | 
 | 183 | 		tmp |= (rtc->periodic_freq & ~PF_HP);	/* Set PES2-0 */ | 
| Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 184 | 	} else | 
 | 185 | 		tmp &= ~(RCR2_PESMASK | RCR2_PEF); | 
 | 186 |  | 
 | 187 | 	writeb(tmp, rtc->regbase + RCR2); | 
 | 188 |  | 
 | 189 | 	spin_unlock_irq(&rtc->lock); | 
 | 190 | } | 
 | 191 |  | 
| Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 192 | static inline int sh_rtc_setfreq(struct device *dev, unsigned int freq) | 
 | 193 | { | 
 | 194 | 	struct sh_rtc *rtc = dev_get_drvdata(dev); | 
 | 195 | 	int tmp, ret = 0; | 
 | 196 |  | 
 | 197 | 	spin_lock_irq(&rtc->lock); | 
 | 198 | 	tmp = rtc->periodic_freq & PF_MASK; | 
 | 199 |  | 
 | 200 | 	switch (freq) { | 
 | 201 | 	case 0: | 
 | 202 | 		rtc->periodic_freq = 0x00; | 
 | 203 | 		break; | 
 | 204 | 	case 1: | 
 | 205 | 		rtc->periodic_freq = 0x60; | 
 | 206 | 		break; | 
 | 207 | 	case 2: | 
 | 208 | 		rtc->periodic_freq = 0x50; | 
 | 209 | 		break; | 
 | 210 | 	case 4: | 
 | 211 | 		rtc->periodic_freq = 0x40; | 
 | 212 | 		break; | 
 | 213 | 	case 8: | 
 | 214 | 		rtc->periodic_freq = 0x30 | PF_HP; | 
 | 215 | 		break; | 
 | 216 | 	case 16: | 
 | 217 | 		rtc->periodic_freq = 0x30; | 
 | 218 | 		break; | 
 | 219 | 	case 32: | 
 | 220 | 		rtc->periodic_freq = 0x20 | PF_HP; | 
 | 221 | 		break; | 
 | 222 | 	case 64: | 
 | 223 | 		rtc->periodic_freq = 0x20; | 
 | 224 | 		break; | 
 | 225 | 	case 128: | 
 | 226 | 		rtc->periodic_freq = 0x10 | PF_HP; | 
 | 227 | 		break; | 
 | 228 | 	case 256: | 
 | 229 | 		rtc->periodic_freq = 0x10; | 
 | 230 | 		break; | 
 | 231 | 	default: | 
 | 232 | 		ret = -ENOTSUPP; | 
 | 233 | 	} | 
 | 234 |  | 
 | 235 | 	if (ret == 0) { | 
 | 236 | 		rtc->periodic_freq |= tmp; | 
 | 237 | 		rtc->rtc_dev->irq_freq = freq; | 
 | 238 | 	} | 
 | 239 |  | 
 | 240 | 	spin_unlock_irq(&rtc->lock); | 
 | 241 | 	return ret; | 
 | 242 | } | 
 | 243 |  | 
| Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 244 | static inline void sh_rtc_setaie(struct device *dev, unsigned int enable) | 
 | 245 | { | 
 | 246 | 	struct sh_rtc *rtc = dev_get_drvdata(dev); | 
 | 247 | 	unsigned int tmp; | 
 | 248 |  | 
 | 249 | 	spin_lock_irq(&rtc->lock); | 
 | 250 |  | 
 | 251 | 	tmp = readb(rtc->regbase + RCR1); | 
 | 252 |  | 
| Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 253 | 	if (!enable) | 
| Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 254 | 		tmp &= ~RCR1_AIE; | 
| Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 255 | 	else | 
| Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 256 | 		tmp |= RCR1_AIE; | 
| Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 257 |  | 
 | 258 | 	writeb(tmp, rtc->regbase + RCR1); | 
 | 259 |  | 
 | 260 | 	spin_unlock_irq(&rtc->lock); | 
 | 261 | } | 
 | 262 |  | 
| Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 263 | static int sh_rtc_proc(struct device *dev, struct seq_file *seq) | 
 | 264 | { | 
 | 265 | 	struct sh_rtc *rtc = dev_get_drvdata(dev); | 
 | 266 | 	unsigned int tmp; | 
 | 267 |  | 
 | 268 | 	tmp = readb(rtc->regbase + RCR1); | 
| Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 269 | 	seq_printf(seq, "carry_IRQ\t: %s\n", (tmp & RCR1_CIE) ? "yes" : "no"); | 
| Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 270 |  | 
 | 271 | 	tmp = readb(rtc->regbase + RCR2); | 
 | 272 | 	seq_printf(seq, "periodic_IRQ\t: %s\n", | 
| Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 273 | 		   (tmp & RCR2_PESMASK) ? "yes" : "no"); | 
| Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 274 |  | 
 | 275 | 	return 0; | 
 | 276 | } | 
 | 277 |  | 
 | 278 | static int sh_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg) | 
 | 279 | { | 
| Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 280 | 	struct sh_rtc *rtc = dev_get_drvdata(dev); | 
 | 281 | 	unsigned int ret = 0; | 
| Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 282 |  | 
 | 283 | 	switch (cmd) { | 
 | 284 | 	case RTC_PIE_OFF: | 
 | 285 | 	case RTC_PIE_ON: | 
 | 286 | 		sh_rtc_setpie(dev, cmd == RTC_PIE_ON); | 
| Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 287 | 		break; | 
 | 288 | 	case RTC_AIE_OFF: | 
 | 289 | 	case RTC_AIE_ON: | 
 | 290 | 		sh_rtc_setaie(dev, cmd == RTC_AIE_ON); | 
| Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 291 | 		break; | 
| Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 292 | 	case RTC_UIE_OFF: | 
 | 293 | 		rtc->periodic_freq &= ~PF_OXS; | 
 | 294 | 		break; | 
 | 295 | 	case RTC_UIE_ON: | 
 | 296 | 		rtc->periodic_freq |= PF_OXS; | 
 | 297 | 		break; | 
 | 298 | 	case RTC_IRQP_READ: | 
 | 299 | 		ret = put_user(rtc->rtc_dev->irq_freq, | 
 | 300 | 			       (unsigned long __user *)arg); | 
 | 301 | 		break; | 
 | 302 | 	case RTC_IRQP_SET: | 
 | 303 | 		ret = sh_rtc_setfreq(dev, arg); | 
 | 304 | 		break; | 
 | 305 | 	default: | 
 | 306 | 		ret = -ENOIOCTLCMD; | 
| Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 307 | 	} | 
 | 308 |  | 
 | 309 | 	return ret; | 
 | 310 | } | 
 | 311 |  | 
 | 312 | static int sh_rtc_read_time(struct device *dev, struct rtc_time *tm) | 
 | 313 | { | 
 | 314 | 	struct platform_device *pdev = to_platform_device(dev); | 
 | 315 | 	struct sh_rtc *rtc = platform_get_drvdata(pdev); | 
 | 316 | 	unsigned int sec128, sec2, yr, yr100, cf_bit; | 
 | 317 |  | 
 | 318 | 	do { | 
 | 319 | 		unsigned int tmp; | 
 | 320 |  | 
 | 321 | 		spin_lock_irq(&rtc->lock); | 
 | 322 |  | 
 | 323 | 		tmp = readb(rtc->regbase + RCR1); | 
 | 324 | 		tmp &= ~RCR1_CF; /* Clear CF-bit */ | 
 | 325 | 		tmp |= RCR1_CIE; | 
 | 326 | 		writeb(tmp, rtc->regbase + RCR1); | 
 | 327 |  | 
 | 328 | 		sec128 = readb(rtc->regbase + R64CNT); | 
 | 329 |  | 
| Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 330 | 		tm->tm_sec	= bcd2bin(readb(rtc->regbase + RSECCNT)); | 
 | 331 | 		tm->tm_min	= bcd2bin(readb(rtc->regbase + RMINCNT)); | 
 | 332 | 		tm->tm_hour	= bcd2bin(readb(rtc->regbase + RHRCNT)); | 
 | 333 | 		tm->tm_wday	= bcd2bin(readb(rtc->regbase + RWKCNT)); | 
 | 334 | 		tm->tm_mday	= bcd2bin(readb(rtc->regbase + RDAYCNT)); | 
 | 335 | 		tm->tm_mon	= bcd2bin(readb(rtc->regbase + RMONCNT)) - 1; | 
| Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 336 |  | 
| Paul Mundt | ad89f87 | 2007-08-03 14:19:58 +0900 | [diff] [blame] | 337 | 		if (rtc->capabilities & RTC_CAP_4_DIGIT_YEAR) { | 
 | 338 | 			yr  = readw(rtc->regbase + RYRCNT); | 
| Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 339 | 			yr100 = bcd2bin(yr >> 8); | 
| Paul Mundt | ad89f87 | 2007-08-03 14:19:58 +0900 | [diff] [blame] | 340 | 			yr &= 0xff; | 
 | 341 | 		} else { | 
 | 342 | 			yr  = readb(rtc->regbase + RYRCNT); | 
| Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 343 | 			yr100 = bcd2bin((yr == 0x99) ? 0x19 : 0x20); | 
| Paul Mundt | ad89f87 | 2007-08-03 14:19:58 +0900 | [diff] [blame] | 344 | 		} | 
| Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 345 |  | 
| Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 346 | 		tm->tm_year = (yr100 * 100 + bcd2bin(yr)) - 1900; | 
| Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 347 |  | 
 | 348 | 		sec2 = readb(rtc->regbase + R64CNT); | 
 | 349 | 		cf_bit = readb(rtc->regbase + RCR1) & RCR1_CF; | 
 | 350 |  | 
 | 351 | 		spin_unlock_irq(&rtc->lock); | 
 | 352 | 	} while (cf_bit != 0 || ((sec128 ^ sec2) & RTC_BIT_INVERTED) != 0); | 
 | 353 |  | 
 | 354 | #if RTC_BIT_INVERTED != 0 | 
 | 355 | 	if ((sec128 & RTC_BIT_INVERTED)) | 
 | 356 | 		tm->tm_sec--; | 
 | 357 | #endif | 
 | 358 |  | 
| Paul Mundt | 435c55d | 2007-05-08 11:56:27 +0900 | [diff] [blame] | 359 | 	dev_dbg(dev, "%s: tm is secs=%d, mins=%d, hours=%d, " | 
| Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 360 | 		"mday=%d, mon=%d, year=%d, wday=%d\n", | 
| Harvey Harrison | 2a4e2b8 | 2008-04-28 02:12:00 -0700 | [diff] [blame] | 361 | 		__func__, | 
| Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 362 | 		tm->tm_sec, tm->tm_min, tm->tm_hour, | 
| Jamie Lenehan | a161479 | 2006-12-08 14:49:30 +0900 | [diff] [blame] | 363 | 		tm->tm_mday, tm->tm_mon + 1, tm->tm_year, tm->tm_wday); | 
| Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 364 |  | 
| Paul Mundt | 0ac554b | 2007-11-07 20:13:24 +0900 | [diff] [blame] | 365 | 	if (rtc_valid_tm(tm) < 0) { | 
| Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 366 | 		dev_err(dev, "invalid date\n"); | 
| Paul Mundt | 0ac554b | 2007-11-07 20:13:24 +0900 | [diff] [blame] | 367 | 		rtc_time_to_tm(0, tm); | 
 | 368 | 	} | 
| Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 369 |  | 
 | 370 | 	return 0; | 
 | 371 | } | 
 | 372 |  | 
 | 373 | static int sh_rtc_set_time(struct device *dev, struct rtc_time *tm) | 
 | 374 | { | 
 | 375 | 	struct platform_device *pdev = to_platform_device(dev); | 
 | 376 | 	struct sh_rtc *rtc = platform_get_drvdata(pdev); | 
 | 377 | 	unsigned int tmp; | 
 | 378 | 	int year; | 
 | 379 |  | 
 | 380 | 	spin_lock_irq(&rtc->lock); | 
 | 381 |  | 
 | 382 | 	/* Reset pre-scaler & stop RTC */ | 
 | 383 | 	tmp = readb(rtc->regbase + RCR2); | 
 | 384 | 	tmp |= RCR2_RESET; | 
| Markus Brunner | 699bc66 | 2007-07-26 17:31:28 +0900 | [diff] [blame] | 385 | 	tmp &= ~RCR2_START; | 
| Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 386 | 	writeb(tmp, rtc->regbase + RCR2); | 
 | 387 |  | 
| Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 388 | 	writeb(bin2bcd(tm->tm_sec),  rtc->regbase + RSECCNT); | 
 | 389 | 	writeb(bin2bcd(tm->tm_min),  rtc->regbase + RMINCNT); | 
 | 390 | 	writeb(bin2bcd(tm->tm_hour), rtc->regbase + RHRCNT); | 
 | 391 | 	writeb(bin2bcd(tm->tm_wday), rtc->regbase + RWKCNT); | 
 | 392 | 	writeb(bin2bcd(tm->tm_mday), rtc->regbase + RDAYCNT); | 
 | 393 | 	writeb(bin2bcd(tm->tm_mon + 1), rtc->regbase + RMONCNT); | 
| Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 394 |  | 
| Paul Mundt | ad89f87 | 2007-08-03 14:19:58 +0900 | [diff] [blame] | 395 | 	if (rtc->capabilities & RTC_CAP_4_DIGIT_YEAR) { | 
| Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 396 | 		year = (bin2bcd((tm->tm_year + 1900) / 100) << 8) | | 
 | 397 | 			bin2bcd(tm->tm_year % 100); | 
| Paul Mundt | ad89f87 | 2007-08-03 14:19:58 +0900 | [diff] [blame] | 398 | 		writew(year, rtc->regbase + RYRCNT); | 
 | 399 | 	} else { | 
 | 400 | 		year = tm->tm_year % 100; | 
| Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 401 | 		writeb(bin2bcd(year), rtc->regbase + RYRCNT); | 
| Paul Mundt | ad89f87 | 2007-08-03 14:19:58 +0900 | [diff] [blame] | 402 | 	} | 
| Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 403 |  | 
 | 404 | 	/* Start RTC */ | 
 | 405 | 	tmp = readb(rtc->regbase + RCR2); | 
 | 406 | 	tmp &= ~RCR2_RESET; | 
 | 407 | 	tmp |= RCR2_RTCEN | RCR2_START; | 
 | 408 | 	writeb(tmp, rtc->regbase + RCR2); | 
 | 409 |  | 
 | 410 | 	spin_unlock_irq(&rtc->lock); | 
 | 411 |  | 
 | 412 | 	return 0; | 
 | 413 | } | 
 | 414 |  | 
| Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 415 | static inline int sh_rtc_read_alarm_value(struct sh_rtc *rtc, int reg_off) | 
 | 416 | { | 
 | 417 | 	unsigned int byte; | 
 | 418 | 	int value = 0xff;	/* return 0xff for ignored values */ | 
 | 419 |  | 
 | 420 | 	byte = readb(rtc->regbase + reg_off); | 
 | 421 | 	if (byte & AR_ENB) { | 
 | 422 | 		byte &= ~AR_ENB;	/* strip the enable bit */ | 
| Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 423 | 		value = bcd2bin(byte); | 
| Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 424 | 	} | 
 | 425 |  | 
 | 426 | 	return value; | 
 | 427 | } | 
 | 428 |  | 
 | 429 | static int sh_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *wkalrm) | 
 | 430 | { | 
 | 431 | 	struct platform_device *pdev = to_platform_device(dev); | 
 | 432 | 	struct sh_rtc *rtc = platform_get_drvdata(pdev); | 
| Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 433 | 	struct rtc_time *tm = &wkalrm->time; | 
| Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 434 |  | 
 | 435 | 	spin_lock_irq(&rtc->lock); | 
 | 436 |  | 
 | 437 | 	tm->tm_sec	= sh_rtc_read_alarm_value(rtc, RSECAR); | 
 | 438 | 	tm->tm_min	= sh_rtc_read_alarm_value(rtc, RMINAR); | 
 | 439 | 	tm->tm_hour	= sh_rtc_read_alarm_value(rtc, RHRAR); | 
 | 440 | 	tm->tm_wday	= sh_rtc_read_alarm_value(rtc, RWKAR); | 
 | 441 | 	tm->tm_mday	= sh_rtc_read_alarm_value(rtc, RDAYAR); | 
 | 442 | 	tm->tm_mon	= sh_rtc_read_alarm_value(rtc, RMONAR); | 
 | 443 | 	if (tm->tm_mon > 0) | 
 | 444 | 		tm->tm_mon -= 1; /* RTC is 1-12, tm_mon is 0-11 */ | 
 | 445 | 	tm->tm_year     = 0xffff; | 
 | 446 |  | 
| David Brownell | 0d103e9 | 2007-01-10 23:15:32 -0800 | [diff] [blame] | 447 | 	wkalrm->enabled = (readb(rtc->regbase + RCR1) & RCR1_AIE) ? 1 : 0; | 
 | 448 |  | 
| Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 449 | 	spin_unlock_irq(&rtc->lock); | 
 | 450 |  | 
 | 451 | 	return 0; | 
 | 452 | } | 
 | 453 |  | 
 | 454 | static inline void sh_rtc_write_alarm_value(struct sh_rtc *rtc, | 
 | 455 | 					    int value, int reg_off) | 
 | 456 | { | 
 | 457 | 	/* < 0 for a value that is ignored */ | 
 | 458 | 	if (value < 0) | 
 | 459 | 		writeb(0, rtc->regbase + reg_off); | 
 | 460 | 	else | 
| Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 461 | 		writeb(bin2bcd(value) | AR_ENB,  rtc->regbase + reg_off); | 
| Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 462 | } | 
 | 463 |  | 
| Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 464 | static int sh_rtc_check_alarm(struct rtc_time *tm) | 
| Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 465 | { | 
 | 466 | 	/* | 
 | 467 | 	 * The original rtc says anything > 0xc0 is "don't care" or "match | 
 | 468 | 	 * all" - most users use 0xff but rtc-dev uses -1 for the same thing. | 
 | 469 | 	 * The original rtc doesn't support years - some things use -1 and | 
 | 470 | 	 * some 0xffff. We use -1 to make out tests easier. | 
 | 471 | 	 */ | 
 | 472 | 	if (tm->tm_year == 0xffff) | 
 | 473 | 		tm->tm_year = -1; | 
 | 474 | 	if (tm->tm_mon >= 0xff) | 
 | 475 | 		tm->tm_mon = -1; | 
 | 476 | 	if (tm->tm_mday >= 0xff) | 
 | 477 | 		tm->tm_mday = -1; | 
 | 478 | 	if (tm->tm_wday >= 0xff) | 
 | 479 | 		tm->tm_wday = -1; | 
 | 480 | 	if (tm->tm_hour >= 0xff) | 
 | 481 | 		tm->tm_hour = -1; | 
 | 482 | 	if (tm->tm_min >= 0xff) | 
 | 483 | 		tm->tm_min = -1; | 
 | 484 | 	if (tm->tm_sec >= 0xff) | 
 | 485 | 		tm->tm_sec = -1; | 
 | 486 |  | 
 | 487 | 	if (tm->tm_year > 9999 || | 
 | 488 | 		tm->tm_mon >= 12 || | 
 | 489 | 		tm->tm_mday == 0 || tm->tm_mday >= 32 || | 
 | 490 | 		tm->tm_wday >= 7 || | 
 | 491 | 		tm->tm_hour >= 24 || | 
 | 492 | 		tm->tm_min >= 60 || | 
 | 493 | 		tm->tm_sec >= 60) | 
 | 494 | 		return -EINVAL; | 
 | 495 |  | 
 | 496 | 	return 0; | 
 | 497 | } | 
 | 498 |  | 
 | 499 | static int sh_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *wkalrm) | 
 | 500 | { | 
 | 501 | 	struct platform_device *pdev = to_platform_device(dev); | 
 | 502 | 	struct sh_rtc *rtc = platform_get_drvdata(pdev); | 
 | 503 | 	unsigned int rcr1; | 
 | 504 | 	struct rtc_time *tm = &wkalrm->time; | 
 | 505 | 	int mon, err; | 
 | 506 |  | 
 | 507 | 	err = sh_rtc_check_alarm(tm); | 
 | 508 | 	if (unlikely(err < 0)) | 
 | 509 | 		return err; | 
 | 510 |  | 
 | 511 | 	spin_lock_irq(&rtc->lock); | 
 | 512 |  | 
| Jamie Lenehan | 15c945c | 2007-01-22 20:40:41 -0800 | [diff] [blame] | 513 | 	/* disable alarm interrupt and clear the alarm flag */ | 
| Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 514 | 	rcr1 = readb(rtc->regbase + RCR1); | 
| Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 515 | 	rcr1 &= ~(RCR1_AF | RCR1_AIE); | 
| Jamie Lenehan | 15c945c | 2007-01-22 20:40:41 -0800 | [diff] [blame] | 516 | 	writeb(rcr1, rtc->regbase + RCR1); | 
| Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 517 |  | 
| Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 518 | 	/* set alarm time */ | 
 | 519 | 	sh_rtc_write_alarm_value(rtc, tm->tm_sec,  RSECAR); | 
 | 520 | 	sh_rtc_write_alarm_value(rtc, tm->tm_min,  RMINAR); | 
 | 521 | 	sh_rtc_write_alarm_value(rtc, tm->tm_hour, RHRAR); | 
 | 522 | 	sh_rtc_write_alarm_value(rtc, tm->tm_wday, RWKAR); | 
 | 523 | 	sh_rtc_write_alarm_value(rtc, tm->tm_mday, RDAYAR); | 
 | 524 | 	mon = tm->tm_mon; | 
 | 525 | 	if (mon >= 0) | 
 | 526 | 		mon += 1; | 
 | 527 | 	sh_rtc_write_alarm_value(rtc, mon, RMONAR); | 
 | 528 |  | 
| Jamie Lenehan | 15c945c | 2007-01-22 20:40:41 -0800 | [diff] [blame] | 529 | 	if (wkalrm->enabled) { | 
 | 530 | 		rcr1 |= RCR1_AIE; | 
 | 531 | 		writeb(rcr1, rtc->regbase + RCR1); | 
 | 532 | 	} | 
| Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 533 |  | 
 | 534 | 	spin_unlock_irq(&rtc->lock); | 
 | 535 |  | 
 | 536 | 	return 0; | 
 | 537 | } | 
 | 538 |  | 
| Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 539 | static int sh_rtc_irq_set_state(struct device *dev, int enabled) | 
 | 540 | { | 
 | 541 | 	struct platform_device *pdev = to_platform_device(dev); | 
 | 542 | 	struct sh_rtc *rtc = platform_get_drvdata(pdev); | 
 | 543 |  | 
 | 544 | 	if (enabled) { | 
 | 545 | 		rtc->periodic_freq |= PF_KOU; | 
 | 546 | 		return sh_rtc_ioctl(dev, RTC_PIE_ON, 0); | 
 | 547 | 	} else { | 
 | 548 | 		rtc->periodic_freq &= ~PF_KOU; | 
 | 549 | 		return sh_rtc_ioctl(dev, RTC_PIE_OFF, 0); | 
 | 550 | 	} | 
 | 551 | } | 
 | 552 |  | 
 | 553 | static int sh_rtc_irq_set_freq(struct device *dev, int freq) | 
 | 554 | { | 
| Jonathan Cameron | 5d2a503 | 2009-01-06 14:42:12 -0800 | [diff] [blame] | 555 | 	if (!is_power_of_2(freq)) | 
 | 556 | 		return -EINVAL; | 
| Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 557 | 	return sh_rtc_ioctl(dev, RTC_IRQP_SET, freq); | 
 | 558 | } | 
 | 559 |  | 
| Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 560 | static struct rtc_class_ops sh_rtc_ops = { | 
| Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 561 | 	.ioctl		= sh_rtc_ioctl, | 
 | 562 | 	.read_time	= sh_rtc_read_time, | 
 | 563 | 	.set_time	= sh_rtc_set_time, | 
| Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 564 | 	.read_alarm	= sh_rtc_read_alarm, | 
 | 565 | 	.set_alarm	= sh_rtc_set_alarm, | 
| Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 566 | 	.irq_set_state	= sh_rtc_irq_set_state, | 
 | 567 | 	.irq_set_freq	= sh_rtc_irq_set_freq, | 
| Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 568 | 	.proc		= sh_rtc_proc, | 
 | 569 | }; | 
 | 570 |  | 
 | 571 | static int __devinit sh_rtc_probe(struct platform_device *pdev) | 
 | 572 | { | 
 | 573 | 	struct sh_rtc *rtc; | 
 | 574 | 	struct resource *res; | 
| Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 575 | 	unsigned int tmp; | 
| roel kluin | 2641dc9 | 2008-09-10 19:34:44 +0200 | [diff] [blame] | 576 | 	int ret; | 
| Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 577 |  | 
 | 578 | 	rtc = kzalloc(sizeof(struct sh_rtc), GFP_KERNEL); | 
 | 579 | 	if (unlikely(!rtc)) | 
 | 580 | 		return -ENOMEM; | 
 | 581 |  | 
 | 582 | 	spin_lock_init(&rtc->lock); | 
 | 583 |  | 
| Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 584 | 	/* get periodic/carry/alarm irqs */ | 
| roel kluin | 2641dc9 | 2008-09-10 19:34:44 +0200 | [diff] [blame] | 585 | 	ret = platform_get_irq(pdev, 0); | 
| Anton Vorontsov | 2fac667 | 2009-01-06 14:42:11 -0800 | [diff] [blame] | 586 | 	if (unlikely(ret <= 0)) { | 
| roel kluin | 2641dc9 | 2008-09-10 19:34:44 +0200 | [diff] [blame] | 587 | 		ret = -ENOENT; | 
| Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 588 | 		dev_err(&pdev->dev, "No IRQ for period\n"); | 
 | 589 | 		goto err_badres; | 
 | 590 | 	} | 
| roel kluin | 2641dc9 | 2008-09-10 19:34:44 +0200 | [diff] [blame] | 591 | 	rtc->periodic_irq = ret; | 
| Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 592 |  | 
| roel kluin | 2641dc9 | 2008-09-10 19:34:44 +0200 | [diff] [blame] | 593 | 	ret = platform_get_irq(pdev, 1); | 
| Anton Vorontsov | 2fac667 | 2009-01-06 14:42:11 -0800 | [diff] [blame] | 594 | 	if (unlikely(ret <= 0)) { | 
| roel kluin | 2641dc9 | 2008-09-10 19:34:44 +0200 | [diff] [blame] | 595 | 		ret = -ENOENT; | 
| Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 596 | 		dev_err(&pdev->dev, "No IRQ for carry\n"); | 
 | 597 | 		goto err_badres; | 
 | 598 | 	} | 
| roel kluin | 2641dc9 | 2008-09-10 19:34:44 +0200 | [diff] [blame] | 599 | 	rtc->carry_irq = ret; | 
| Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 600 |  | 
| roel kluin | 2641dc9 | 2008-09-10 19:34:44 +0200 | [diff] [blame] | 601 | 	ret = platform_get_irq(pdev, 2); | 
| Anton Vorontsov | 2fac667 | 2009-01-06 14:42:11 -0800 | [diff] [blame] | 602 | 	if (unlikely(ret <= 0)) { | 
| roel kluin | 2641dc9 | 2008-09-10 19:34:44 +0200 | [diff] [blame] | 603 | 		ret = -ENOENT; | 
| Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 604 | 		dev_err(&pdev->dev, "No IRQ for alarm\n"); | 
 | 605 | 		goto err_badres; | 
 | 606 | 	} | 
| roel kluin | 2641dc9 | 2008-09-10 19:34:44 +0200 | [diff] [blame] | 607 | 	rtc->alarm_irq = ret; | 
| Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 608 |  | 
 | 609 | 	res = platform_get_resource(pdev, IORESOURCE_IO, 0); | 
 | 610 | 	if (unlikely(res == NULL)) { | 
| roel kluin | 2641dc9 | 2008-09-10 19:34:44 +0200 | [diff] [blame] | 611 | 		ret = -ENOENT; | 
| Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 612 | 		dev_err(&pdev->dev, "No IO resource\n"); | 
 | 613 | 		goto err_badres; | 
 | 614 | 	} | 
 | 615 |  | 
 | 616 | 	rtc->regsize = res->end - res->start + 1; | 
 | 617 |  | 
 | 618 | 	rtc->res = request_mem_region(res->start, rtc->regsize, pdev->name); | 
 | 619 | 	if (unlikely(!rtc->res)) { | 
 | 620 | 		ret = -EBUSY; | 
 | 621 | 		goto err_badres; | 
 | 622 | 	} | 
 | 623 |  | 
| Paul Mundt | 0305794 | 2008-04-25 17:58:42 +0900 | [diff] [blame] | 624 | 	rtc->regbase = ioremap_nocache(rtc->res->start, rtc->regsize); | 
| Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 625 | 	if (unlikely(!rtc->regbase)) { | 
 | 626 | 		ret = -EINVAL; | 
 | 627 | 		goto err_badmap; | 
 | 628 | 	} | 
 | 629 |  | 
 | 630 | 	rtc->rtc_dev = rtc_device_register("sh", &pdev->dev, | 
 | 631 | 					   &sh_rtc_ops, THIS_MODULE); | 
| Paul Mundt | 29dd0da | 2007-11-07 14:58:09 +0900 | [diff] [blame] | 632 | 	if (IS_ERR(rtc->rtc_dev)) { | 
| Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 633 | 		ret = PTR_ERR(rtc->rtc_dev); | 
| Paul Mundt | 0305794 | 2008-04-25 17:58:42 +0900 | [diff] [blame] | 634 | 		goto err_unmap; | 
| Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 635 | 	} | 
 | 636 |  | 
| Paul Mundt | ad89f87 | 2007-08-03 14:19:58 +0900 | [diff] [blame] | 637 | 	rtc->capabilities = RTC_DEF_CAPABILITIES; | 
 | 638 | 	if (pdev->dev.platform_data) { | 
 | 639 | 		struct sh_rtc_platform_info *pinfo = pdev->dev.platform_data; | 
 | 640 |  | 
 | 641 | 		/* | 
 | 642 | 		 * Some CPUs have special capabilities in addition to the | 
 | 643 | 		 * default set. Add those in here. | 
 | 644 | 		 */ | 
 | 645 | 		rtc->capabilities |= pinfo->capabilities; | 
 | 646 | 	} | 
 | 647 |  | 
| Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 648 | 	rtc->rtc_dev->max_user_freq = 256; | 
 | 649 | 	rtc->rtc_dev->irq_freq = 1; | 
 | 650 | 	rtc->periodic_freq = 0x60; | 
 | 651 |  | 
| Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 652 | 	platform_set_drvdata(pdev, rtc); | 
 | 653 |  | 
| Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 654 | 	/* register periodic/carry/alarm irqs */ | 
 | 655 | 	ret = request_irq(rtc->periodic_irq, sh_rtc_periodic, IRQF_DISABLED, | 
 | 656 | 			  "sh-rtc period", rtc); | 
 | 657 | 	if (unlikely(ret)) { | 
 | 658 | 		dev_err(&pdev->dev, | 
 | 659 | 			"request period IRQ failed with %d, IRQ %d\n", ret, | 
 | 660 | 			rtc->periodic_irq); | 
| Paul Mundt | 0305794 | 2008-04-25 17:58:42 +0900 | [diff] [blame] | 661 | 		goto err_unmap; | 
| Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 662 | 	} | 
 | 663 |  | 
 | 664 | 	ret = request_irq(rtc->carry_irq, sh_rtc_interrupt, IRQF_DISABLED, | 
 | 665 | 			  "sh-rtc carry", rtc); | 
 | 666 | 	if (unlikely(ret)) { | 
 | 667 | 		dev_err(&pdev->dev, | 
 | 668 | 			"request carry IRQ failed with %d, IRQ %d\n", ret, | 
 | 669 | 			rtc->carry_irq); | 
 | 670 | 		free_irq(rtc->periodic_irq, rtc); | 
| Paul Mundt | 0305794 | 2008-04-25 17:58:42 +0900 | [diff] [blame] | 671 | 		goto err_unmap; | 
| Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 672 | 	} | 
 | 673 |  | 
 | 674 | 	ret = request_irq(rtc->alarm_irq, sh_rtc_alarm, IRQF_DISABLED, | 
 | 675 | 			  "sh-rtc alarm", rtc); | 
 | 676 | 	if (unlikely(ret)) { | 
 | 677 | 		dev_err(&pdev->dev, | 
 | 678 | 			"request alarm IRQ failed with %d, IRQ %d\n", ret, | 
 | 679 | 			rtc->alarm_irq); | 
 | 680 | 		free_irq(rtc->carry_irq, rtc); | 
 | 681 | 		free_irq(rtc->periodic_irq, rtc); | 
| Paul Mundt | 0305794 | 2008-04-25 17:58:42 +0900 | [diff] [blame] | 682 | 		goto err_unmap; | 
| Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 683 | 	} | 
 | 684 |  | 
 | 685 | 	tmp = readb(rtc->regbase + RCR1); | 
 | 686 | 	tmp &= ~RCR1_CF; | 
 | 687 | 	tmp |= RCR1_CIE; | 
 | 688 | 	writeb(tmp, rtc->regbase + RCR1); | 
 | 689 |  | 
| Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 690 | 	return 0; | 
 | 691 |  | 
| Paul Mundt | 0305794 | 2008-04-25 17:58:42 +0900 | [diff] [blame] | 692 | err_unmap: | 
 | 693 | 	iounmap(rtc->regbase); | 
| Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 694 | err_badmap: | 
 | 695 | 	release_resource(rtc->res); | 
 | 696 | err_badres: | 
 | 697 | 	kfree(rtc); | 
 | 698 |  | 
 | 699 | 	return ret; | 
 | 700 | } | 
 | 701 |  | 
 | 702 | static int __devexit sh_rtc_remove(struct platform_device *pdev) | 
 | 703 | { | 
 | 704 | 	struct sh_rtc *rtc = platform_get_drvdata(pdev); | 
 | 705 |  | 
 | 706 | 	if (likely(rtc->rtc_dev)) | 
 | 707 | 		rtc_device_unregister(rtc->rtc_dev); | 
 | 708 |  | 
 | 709 | 	sh_rtc_setpie(&pdev->dev, 0); | 
 | 710 | 	sh_rtc_setaie(&pdev->dev, 0); | 
 | 711 |  | 
| Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 712 | 	free_irq(rtc->carry_irq, rtc); | 
 | 713 | 	free_irq(rtc->periodic_irq, rtc); | 
 | 714 | 	free_irq(rtc->alarm_irq, rtc); | 
 | 715 |  | 
| Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 716 | 	release_resource(rtc->res); | 
 | 717 |  | 
| Paul Mundt | 0305794 | 2008-04-25 17:58:42 +0900 | [diff] [blame] | 718 | 	iounmap(rtc->regbase); | 
 | 719 |  | 
| Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 720 | 	platform_set_drvdata(pdev, NULL); | 
 | 721 |  | 
 | 722 | 	kfree(rtc); | 
 | 723 |  | 
 | 724 | 	return 0; | 
 | 725 | } | 
 | 726 | static struct platform_driver sh_rtc_platform_driver = { | 
 | 727 | 	.driver		= { | 
| Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 728 | 		.name	= DRV_NAME, | 
| Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 729 | 		.owner	= THIS_MODULE, | 
 | 730 | 	}, | 
 | 731 | 	.probe		= sh_rtc_probe, | 
 | 732 | 	.remove		= __devexit_p(sh_rtc_remove), | 
 | 733 | }; | 
 | 734 |  | 
 | 735 | static int __init sh_rtc_init(void) | 
 | 736 | { | 
 | 737 | 	return platform_driver_register(&sh_rtc_platform_driver); | 
 | 738 | } | 
 | 739 |  | 
 | 740 | static void __exit sh_rtc_exit(void) | 
 | 741 | { | 
 | 742 | 	platform_driver_unregister(&sh_rtc_platform_driver); | 
 | 743 | } | 
 | 744 |  | 
 | 745 | module_init(sh_rtc_init); | 
 | 746 | module_exit(sh_rtc_exit); | 
 | 747 |  | 
 | 748 | MODULE_DESCRIPTION("SuperH on-chip RTC driver"); | 
| Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 749 | MODULE_VERSION(DRV_VERSION); | 
| Angelo Castello | b420b1a | 2008-03-06 12:50:53 +0900 | [diff] [blame] | 750 | MODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>, " | 
 | 751 | 	      "Jamie Lenehan <lenehan@twibble.org>, " | 
 | 752 | 	      "Angelo Castello <angelo.castello@st.com>"); | 
| Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 753 | MODULE_LICENSE("GPL"); | 
| Kay Sievers | ad28a07 | 2008-04-10 21:29:25 -0700 | [diff] [blame] | 754 | MODULE_ALIAS("platform:" DRV_NAME); |