| Mark Brown | 077eaf5 | 2008-11-12 13:27:04 -0800 | [diff] [blame] | 1 | /* | 
 | 2 |  *	Real Time Clock driver for Wolfson Microelectronics WM8350 | 
 | 3 |  * | 
 | 4 |  *	Copyright (C) 2007, 2008 Wolfson Microelectronics PLC. | 
 | 5 |  * | 
 | 6 |  *  Author: Liam Girdwood | 
 | 7 |  *          linux@wolfsonmicro.com | 
 | 8 |  * | 
 | 9 |  *  This program is free software; you can redistribute  it and/or modify it | 
 | 10 |  *  under  the terms of  the GNU General  Public License as published by the | 
 | 11 |  *  Free Software Foundation;  either version 2 of the  License, or (at your | 
 | 12 |  *  option) any later version. | 
 | 13 |  * | 
 | 14 |  */ | 
 | 15 |  | 
 | 16 | #include <linux/module.h> | 
 | 17 | #include <linux/kernel.h> | 
 | 18 | #include <linux/time.h> | 
 | 19 | #include <linux/rtc.h> | 
 | 20 | #include <linux/bcd.h> | 
 | 21 | #include <linux/interrupt.h> | 
 | 22 | #include <linux/ioctl.h> | 
 | 23 | #include <linux/completion.h> | 
 | 24 | #include <linux/mfd/wm8350/rtc.h> | 
 | 25 | #include <linux/mfd/wm8350/core.h> | 
 | 26 | #include <linux/delay.h> | 
 | 27 | #include <linux/platform_device.h> | 
 | 28 |  | 
 | 29 | #define WM8350_SET_ALM_RETRIES	5 | 
 | 30 | #define WM8350_SET_TIME_RETRIES	5 | 
 | 31 | #define WM8350_GET_TIME_RETRIES	5 | 
 | 32 |  | 
 | 33 | #define to_wm8350_from_rtc_dev(d) container_of(d, struct wm8350, rtc.pdev.dev) | 
 | 34 |  | 
 | 35 | /* | 
 | 36 |  * Read current time and date in RTC | 
 | 37 |  */ | 
 | 38 | static int wm8350_rtc_readtime(struct device *dev, struct rtc_time *tm) | 
 | 39 | { | 
 | 40 | 	struct wm8350 *wm8350 = dev_get_drvdata(dev); | 
 | 41 | 	u16 time1[4], time2[4]; | 
 | 42 | 	int retries = WM8350_GET_TIME_RETRIES, ret; | 
 | 43 |  | 
 | 44 | 	/* | 
 | 45 | 	 * Read the time twice and compare. | 
 | 46 | 	 * If time1 == time2, then time is valid else retry. | 
 | 47 | 	 */ | 
 | 48 | 	do { | 
 | 49 | 		ret = wm8350_block_read(wm8350, WM8350_RTC_SECONDS_MINUTES, | 
 | 50 | 					4, time1); | 
 | 51 | 		if (ret < 0) | 
 | 52 | 			return ret; | 
 | 53 | 		ret = wm8350_block_read(wm8350, WM8350_RTC_SECONDS_MINUTES, | 
 | 54 | 					4, time2); | 
 | 55 | 		if (ret < 0) | 
 | 56 | 			return ret; | 
 | 57 |  | 
 | 58 | 		if (memcmp(time1, time2, sizeof(time1)) == 0) { | 
 | 59 | 			tm->tm_sec = time1[0] & WM8350_RTC_SECS_MASK; | 
 | 60 |  | 
 | 61 | 			tm->tm_min = (time1[0] & WM8350_RTC_MINS_MASK) | 
 | 62 | 			    >> WM8350_RTC_MINS_SHIFT; | 
 | 63 |  | 
 | 64 | 			tm->tm_hour = time1[1] & WM8350_RTC_HRS_MASK; | 
 | 65 |  | 
 | 66 | 			tm->tm_wday = ((time1[1] >> WM8350_RTC_DAY_SHIFT) | 
 | 67 | 				       & 0x7) - 1; | 
 | 68 |  | 
 | 69 | 			tm->tm_mon = ((time1[2] & WM8350_RTC_MTH_MASK) | 
 | 70 | 				      >> WM8350_RTC_MTH_SHIFT) - 1; | 
 | 71 |  | 
 | 72 | 			tm->tm_mday = (time1[2] & WM8350_RTC_DATE_MASK); | 
 | 73 |  | 
 | 74 | 			tm->tm_year = ((time1[3] & WM8350_RTC_YHUNDREDS_MASK) | 
 | 75 | 				       >> WM8350_RTC_YHUNDREDS_SHIFT) * 100; | 
 | 76 | 			tm->tm_year += time1[3] & WM8350_RTC_YUNITS_MASK; | 
 | 77 |  | 
 | 78 | 			tm->tm_yday = rtc_year_days(tm->tm_mday, tm->tm_mon, | 
 | 79 | 						    tm->tm_year); | 
 | 80 | 			tm->tm_year -= 1900; | 
 | 81 |  | 
 | 82 | 			dev_dbg(dev, "Read (%d left): %04x %04x %04x %04x\n", | 
 | 83 | 				retries, | 
 | 84 | 				time1[0], time1[1], time1[2], time1[3]); | 
 | 85 |  | 
 | 86 | 			return 0; | 
 | 87 | 		} | 
 | 88 | 	} while (retries--); | 
 | 89 |  | 
 | 90 | 	dev_err(dev, "timed out reading RTC time\n"); | 
 | 91 | 	return -EIO; | 
 | 92 | } | 
 | 93 |  | 
 | 94 | /* | 
 | 95 |  * Set current time and date in RTC | 
 | 96 |  */ | 
 | 97 | static int wm8350_rtc_settime(struct device *dev, struct rtc_time *tm) | 
 | 98 | { | 
 | 99 | 	struct wm8350 *wm8350 = dev_get_drvdata(dev); | 
 | 100 | 	u16 time[4]; | 
 | 101 | 	u16 rtc_ctrl; | 
 | 102 | 	int ret, retries = WM8350_SET_TIME_RETRIES; | 
 | 103 |  | 
 | 104 | 	time[0] = tm->tm_sec; | 
 | 105 | 	time[0] |= tm->tm_min << WM8350_RTC_MINS_SHIFT; | 
 | 106 | 	time[1] = tm->tm_hour; | 
 | 107 | 	time[1] |= (tm->tm_wday + 1) << WM8350_RTC_DAY_SHIFT; | 
 | 108 | 	time[2] = tm->tm_mday; | 
 | 109 | 	time[2] |= (tm->tm_mon + 1) << WM8350_RTC_MTH_SHIFT; | 
 | 110 | 	time[3] = ((tm->tm_year + 1900) / 100) << WM8350_RTC_YHUNDREDS_SHIFT; | 
 | 111 | 	time[3] |= (tm->tm_year + 1900) % 100; | 
 | 112 |  | 
 | 113 | 	dev_dbg(dev, "Setting: %04x %04x %04x %04x\n", | 
 | 114 | 		time[0], time[1], time[2], time[3]); | 
 | 115 |  | 
 | 116 | 	/* Set RTC_SET to stop the clock */ | 
 | 117 | 	ret = wm8350_set_bits(wm8350, WM8350_RTC_TIME_CONTROL, WM8350_RTC_SET); | 
 | 118 | 	if (ret < 0) | 
 | 119 | 		return ret; | 
 | 120 |  | 
 | 121 | 	/* Wait until confirmation of stopping */ | 
 | 122 | 	do { | 
 | 123 | 		rtc_ctrl = wm8350_reg_read(wm8350, WM8350_RTC_TIME_CONTROL); | 
 | 124 | 		schedule_timeout_uninterruptible(msecs_to_jiffies(1)); | 
| Roel Kluin | 62da659 | 2009-03-31 15:24:59 -0700 | [diff] [blame] | 125 | 	} while (--retries && !(rtc_ctrl & WM8350_RTC_STS)); | 
| Mark Brown | 077eaf5 | 2008-11-12 13:27:04 -0800 | [diff] [blame] | 126 |  | 
 | 127 | 	if (!retries) { | 
 | 128 | 		dev_err(dev, "timed out on set confirmation\n"); | 
 | 129 | 		return -EIO; | 
 | 130 | 	} | 
 | 131 |  | 
 | 132 | 	/* Write time to RTC */ | 
 | 133 | 	ret = wm8350_block_write(wm8350, WM8350_RTC_SECONDS_MINUTES, 4, time); | 
 | 134 | 	if (ret < 0) | 
 | 135 | 		return ret; | 
 | 136 |  | 
 | 137 | 	/* Clear RTC_SET to start the clock */ | 
 | 138 | 	ret = wm8350_clear_bits(wm8350, WM8350_RTC_TIME_CONTROL, | 
 | 139 | 				WM8350_RTC_SET); | 
 | 140 | 	return ret; | 
 | 141 | } | 
 | 142 |  | 
 | 143 | /* | 
 | 144 |  * Read alarm time and date in RTC | 
 | 145 |  */ | 
 | 146 | static int wm8350_rtc_readalarm(struct device *dev, struct rtc_wkalrm *alrm) | 
 | 147 | { | 
 | 148 | 	struct wm8350 *wm8350 = dev_get_drvdata(dev); | 
 | 149 | 	struct rtc_time *tm = &alrm->time; | 
 | 150 | 	u16 time[4]; | 
 | 151 | 	int ret; | 
 | 152 |  | 
 | 153 | 	ret = wm8350_block_read(wm8350, WM8350_ALARM_SECONDS_MINUTES, 4, time); | 
 | 154 | 	if (ret < 0) | 
 | 155 | 		return ret; | 
 | 156 |  | 
 | 157 | 	tm->tm_sec = time[0] & WM8350_RTC_ALMSECS_MASK; | 
 | 158 | 	if (tm->tm_sec == WM8350_RTC_ALMSECS_MASK) | 
 | 159 | 		tm->tm_sec = -1; | 
 | 160 |  | 
 | 161 | 	tm->tm_min = time[0] & WM8350_RTC_ALMMINS_MASK; | 
 | 162 | 	if (tm->tm_min == WM8350_RTC_ALMMINS_MASK) | 
 | 163 | 		tm->tm_min = -1; | 
 | 164 | 	else | 
 | 165 | 		tm->tm_min >>= WM8350_RTC_ALMMINS_SHIFT; | 
 | 166 |  | 
 | 167 | 	tm->tm_hour = time[1] & WM8350_RTC_ALMHRS_MASK; | 
 | 168 | 	if (tm->tm_hour == WM8350_RTC_ALMHRS_MASK) | 
 | 169 | 		tm->tm_hour = -1; | 
 | 170 |  | 
 | 171 | 	tm->tm_wday = ((time[1] >> WM8350_RTC_ALMDAY_SHIFT) & 0x7) - 1; | 
 | 172 | 	if (tm->tm_wday > 7) | 
 | 173 | 		tm->tm_wday = -1; | 
 | 174 |  | 
 | 175 | 	tm->tm_mon = time[2] & WM8350_RTC_ALMMTH_MASK; | 
 | 176 | 	if (tm->tm_mon == WM8350_RTC_ALMMTH_MASK) | 
 | 177 | 		tm->tm_mon = -1; | 
 | 178 | 	else | 
 | 179 | 		tm->tm_mon = (tm->tm_mon >> WM8350_RTC_ALMMTH_SHIFT) - 1; | 
 | 180 |  | 
 | 181 | 	tm->tm_mday = (time[2] & WM8350_RTC_ALMDATE_MASK); | 
 | 182 | 	if (tm->tm_mday == WM8350_RTC_ALMDATE_MASK) | 
 | 183 | 		tm->tm_mday = -1; | 
 | 184 |  | 
 | 185 | 	tm->tm_year = -1; | 
 | 186 |  | 
 | 187 | 	alrm->enabled = !(time[3] & WM8350_RTC_ALMSTS); | 
 | 188 |  | 
 | 189 | 	return 0; | 
 | 190 | } | 
 | 191 |  | 
 | 192 | static int wm8350_rtc_stop_alarm(struct wm8350 *wm8350) | 
 | 193 | { | 
 | 194 | 	int retries = WM8350_SET_ALM_RETRIES; | 
 | 195 | 	u16 rtc_ctrl; | 
 | 196 | 	int ret; | 
 | 197 |  | 
 | 198 | 	/* Set RTC_SET to stop the clock */ | 
 | 199 | 	ret = wm8350_set_bits(wm8350, WM8350_RTC_TIME_CONTROL, | 
 | 200 | 			      WM8350_RTC_ALMSET); | 
 | 201 | 	if (ret < 0) | 
 | 202 | 		return ret; | 
 | 203 |  | 
 | 204 | 	/* Wait until confirmation of stopping */ | 
 | 205 | 	do { | 
 | 206 | 		rtc_ctrl = wm8350_reg_read(wm8350, WM8350_RTC_TIME_CONTROL); | 
 | 207 | 		schedule_timeout_uninterruptible(msecs_to_jiffies(1)); | 
 | 208 | 	} while (retries-- && !(rtc_ctrl & WM8350_RTC_ALMSTS)); | 
 | 209 |  | 
 | 210 | 	if (!(rtc_ctrl & WM8350_RTC_ALMSTS)) | 
 | 211 | 		return -ETIMEDOUT; | 
 | 212 |  | 
 | 213 | 	return 0; | 
 | 214 | } | 
 | 215 |  | 
 | 216 | static int wm8350_rtc_start_alarm(struct wm8350 *wm8350) | 
 | 217 | { | 
 | 218 | 	int ret; | 
 | 219 | 	int retries = WM8350_SET_ALM_RETRIES; | 
 | 220 | 	u16 rtc_ctrl; | 
 | 221 |  | 
 | 222 | 	ret = wm8350_clear_bits(wm8350, WM8350_RTC_TIME_CONTROL, | 
 | 223 | 				WM8350_RTC_ALMSET); | 
 | 224 | 	if (ret < 0) | 
 | 225 | 		return ret; | 
 | 226 |  | 
 | 227 | 	/* Wait until confirmation */ | 
 | 228 | 	do { | 
 | 229 | 		rtc_ctrl = wm8350_reg_read(wm8350, WM8350_RTC_TIME_CONTROL); | 
 | 230 | 		schedule_timeout_uninterruptible(msecs_to_jiffies(1)); | 
 | 231 | 	} while (retries-- && rtc_ctrl & WM8350_RTC_ALMSTS); | 
 | 232 |  | 
 | 233 | 	if (rtc_ctrl & WM8350_RTC_ALMSTS) | 
 | 234 | 		return -ETIMEDOUT; | 
 | 235 |  | 
 | 236 | 	return 0; | 
 | 237 | } | 
 | 238 |  | 
| Mark Brown | 47367a3 | 2009-03-31 15:24:46 -0700 | [diff] [blame] | 239 | static int wm8350_rtc_alarm_irq_enable(struct device *dev, | 
 | 240 | 				       unsigned int enabled) | 
 | 241 | { | 
 | 242 | 	struct wm8350 *wm8350 = dev_get_drvdata(dev); | 
 | 243 |  | 
 | 244 | 	if (enabled) | 
 | 245 | 		return wm8350_rtc_start_alarm(wm8350); | 
 | 246 | 	else | 
 | 247 | 		return wm8350_rtc_stop_alarm(wm8350); | 
 | 248 | } | 
 | 249 |  | 
| Mark Brown | 077eaf5 | 2008-11-12 13:27:04 -0800 | [diff] [blame] | 250 | static int wm8350_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm) | 
 | 251 | { | 
 | 252 | 	struct wm8350 *wm8350 = dev_get_drvdata(dev); | 
 | 253 | 	struct rtc_time *tm = &alrm->time; | 
 | 254 | 	u16 time[3]; | 
 | 255 | 	int ret; | 
 | 256 |  | 
 | 257 | 	memset(time, 0, sizeof(time)); | 
 | 258 |  | 
 | 259 | 	if (tm->tm_sec != -1) | 
 | 260 | 		time[0] |= tm->tm_sec; | 
 | 261 | 	else | 
 | 262 | 		time[0] |= WM8350_RTC_ALMSECS_MASK; | 
 | 263 |  | 
 | 264 | 	if (tm->tm_min != -1) | 
 | 265 | 		time[0] |= tm->tm_min << WM8350_RTC_ALMMINS_SHIFT; | 
 | 266 | 	else | 
 | 267 | 		time[0] |= WM8350_RTC_ALMMINS_MASK; | 
 | 268 |  | 
 | 269 | 	if (tm->tm_hour != -1) | 
 | 270 | 		time[1] |= tm->tm_hour; | 
 | 271 | 	else | 
 | 272 | 		time[1] |= WM8350_RTC_ALMHRS_MASK; | 
 | 273 |  | 
 | 274 | 	if (tm->tm_wday != -1) | 
 | 275 | 		time[1] |= (tm->tm_wday + 1) << WM8350_RTC_ALMDAY_SHIFT; | 
 | 276 | 	else | 
 | 277 | 		time[1] |= WM8350_RTC_ALMDAY_MASK; | 
 | 278 |  | 
 | 279 | 	if (tm->tm_mday != -1) | 
 | 280 | 		time[2] |= tm->tm_mday; | 
 | 281 | 	else | 
 | 282 | 		time[2] |= WM8350_RTC_ALMDATE_MASK; | 
 | 283 |  | 
 | 284 | 	if (tm->tm_mon != -1) | 
 | 285 | 		time[2] |= (tm->tm_mon + 1) << WM8350_RTC_ALMMTH_SHIFT; | 
 | 286 | 	else | 
 | 287 | 		time[2] |= WM8350_RTC_ALMMTH_MASK; | 
 | 288 |  | 
 | 289 | 	ret = wm8350_rtc_stop_alarm(wm8350); | 
 | 290 | 	if (ret < 0) | 
 | 291 | 		return ret; | 
 | 292 |  | 
 | 293 | 	/* Write time to RTC */ | 
 | 294 | 	ret = wm8350_block_write(wm8350, WM8350_ALARM_SECONDS_MINUTES, | 
 | 295 | 				 3, time); | 
 | 296 | 	if (ret < 0) | 
 | 297 | 		return ret; | 
 | 298 |  | 
 | 299 | 	if (alrm->enabled) | 
 | 300 | 		ret = wm8350_rtc_start_alarm(wm8350); | 
 | 301 |  | 
 | 302 | 	return ret; | 
 | 303 | } | 
 | 304 |  | 
| Mark Brown | 47367a3 | 2009-03-31 15:24:46 -0700 | [diff] [blame] | 305 | static int wm8350_rtc_update_irq_enable(struct device *dev, | 
 | 306 | 					unsigned int enabled) | 
| Mark Brown | 077eaf5 | 2008-11-12 13:27:04 -0800 | [diff] [blame] | 307 | { | 
 | 308 | 	struct wm8350 *wm8350 = dev_get_drvdata(dev); | 
 | 309 |  | 
| Mark Brown | 29c71b1 | 2010-01-05 13:59:08 +0000 | [diff] [blame] | 310 | 	/* Suppress duplicate changes since genirq nests enable and | 
 | 311 | 	 * disable calls. */ | 
 | 312 | 	if (enabled == wm8350->rtc.update_enabled) | 
 | 313 | 		return 0; | 
 | 314 |  | 
| Mark Brown | 47367a3 | 2009-03-31 15:24:46 -0700 | [diff] [blame] | 315 | 	if (enabled) | 
| Mark Brown | 077eaf5 | 2008-11-12 13:27:04 -0800 | [diff] [blame] | 316 | 		wm8350_unmask_irq(wm8350, WM8350_IRQ_RTC_SEC); | 
| Mark Brown | 47367a3 | 2009-03-31 15:24:46 -0700 | [diff] [blame] | 317 | 	else | 
 | 318 | 		wm8350_mask_irq(wm8350, WM8350_IRQ_RTC_SEC); | 
| Mark Brown | 077eaf5 | 2008-11-12 13:27:04 -0800 | [diff] [blame] | 319 |  | 
| Mark Brown | 29c71b1 | 2010-01-05 13:59:08 +0000 | [diff] [blame] | 320 | 	wm8350->rtc.update_enabled = enabled; | 
 | 321 |  | 
| Mark Brown | 077eaf5 | 2008-11-12 13:27:04 -0800 | [diff] [blame] | 322 | 	return 0; | 
 | 323 | } | 
 | 324 |  | 
| Mark Brown | 5a65edb | 2009-11-04 16:10:51 +0000 | [diff] [blame] | 325 | static irqreturn_t wm8350_rtc_alarm_handler(int irq, void *data) | 
| Mark Brown | 077eaf5 | 2008-11-12 13:27:04 -0800 | [diff] [blame] | 326 | { | 
| Mark Brown | 5a65edb | 2009-11-04 16:10:51 +0000 | [diff] [blame] | 327 | 	struct wm8350 *wm8350 = data; | 
| Mark Brown | 077eaf5 | 2008-11-12 13:27:04 -0800 | [diff] [blame] | 328 | 	struct rtc_device *rtc = wm8350->rtc.rtc; | 
 | 329 | 	int ret; | 
 | 330 |  | 
 | 331 | 	rtc_update_irq(rtc, 1, RTC_IRQF | RTC_AF); | 
 | 332 |  | 
 | 333 | 	/* Make it one shot */ | 
 | 334 | 	ret = wm8350_set_bits(wm8350, WM8350_RTC_TIME_CONTROL, | 
 | 335 | 			      WM8350_RTC_ALMSET); | 
 | 336 | 	if (ret != 0) { | 
 | 337 | 		dev_err(&(wm8350->rtc.pdev->dev), | 
 | 338 | 			"Failed to disable alarm: %d\n", ret); | 
 | 339 | 	} | 
| Mark Brown | 5a65edb | 2009-11-04 16:10:51 +0000 | [diff] [blame] | 340 |  | 
 | 341 | 	return IRQ_HANDLED; | 
| Mark Brown | 077eaf5 | 2008-11-12 13:27:04 -0800 | [diff] [blame] | 342 | } | 
 | 343 |  | 
| Mark Brown | 5a65edb | 2009-11-04 16:10:51 +0000 | [diff] [blame] | 344 | static irqreturn_t wm8350_rtc_update_handler(int irq, void *data) | 
| Mark Brown | 077eaf5 | 2008-11-12 13:27:04 -0800 | [diff] [blame] | 345 | { | 
| Mark Brown | 5a65edb | 2009-11-04 16:10:51 +0000 | [diff] [blame] | 346 | 	struct wm8350 *wm8350 = data; | 
| Mark Brown | 077eaf5 | 2008-11-12 13:27:04 -0800 | [diff] [blame] | 347 | 	struct rtc_device *rtc = wm8350->rtc.rtc; | 
 | 348 |  | 
 | 349 | 	rtc_update_irq(rtc, 1, RTC_IRQF | RTC_UF); | 
| Mark Brown | 5a65edb | 2009-11-04 16:10:51 +0000 | [diff] [blame] | 350 |  | 
 | 351 | 	return IRQ_HANDLED; | 
| Mark Brown | 077eaf5 | 2008-11-12 13:27:04 -0800 | [diff] [blame] | 352 | } | 
 | 353 |  | 
 | 354 | static const struct rtc_class_ops wm8350_rtc_ops = { | 
| Mark Brown | 077eaf5 | 2008-11-12 13:27:04 -0800 | [diff] [blame] | 355 | 	.read_time = wm8350_rtc_readtime, | 
 | 356 | 	.set_time = wm8350_rtc_settime, | 
 | 357 | 	.read_alarm = wm8350_rtc_readalarm, | 
 | 358 | 	.set_alarm = wm8350_rtc_setalarm, | 
| Mark Brown | 47367a3 | 2009-03-31 15:24:46 -0700 | [diff] [blame] | 359 | 	.alarm_irq_enable = wm8350_rtc_alarm_irq_enable, | 
 | 360 | 	.update_irq_enable = wm8350_rtc_update_irq_enable, | 
| Mark Brown | 077eaf5 | 2008-11-12 13:27:04 -0800 | [diff] [blame] | 361 | }; | 
 | 362 |  | 
 | 363 | #ifdef CONFIG_PM | 
| Mark Brown | 6f38b04 | 2009-12-15 16:46:09 -0800 | [diff] [blame] | 364 | static int wm8350_rtc_suspend(struct device *dev) | 
| Mark Brown | 077eaf5 | 2008-11-12 13:27:04 -0800 | [diff] [blame] | 365 | { | 
| Mark Brown | 6f38b04 | 2009-12-15 16:46:09 -0800 | [diff] [blame] | 366 | 	struct platform_device *pdev = to_platform_device(dev); | 
| Mark Brown | 077eaf5 | 2008-11-12 13:27:04 -0800 | [diff] [blame] | 367 | 	struct wm8350 *wm8350 = dev_get_drvdata(&pdev->dev); | 
 | 368 | 	int ret = 0; | 
 | 369 | 	u16 reg; | 
 | 370 |  | 
 | 371 | 	reg = wm8350_reg_read(wm8350, WM8350_RTC_TIME_CONTROL); | 
 | 372 |  | 
 | 373 | 	if (device_may_wakeup(&wm8350->rtc.pdev->dev) && | 
 | 374 | 	    reg & WM8350_RTC_ALMSTS) { | 
 | 375 | 		ret = wm8350_rtc_stop_alarm(wm8350); | 
 | 376 | 		if (ret != 0) | 
 | 377 | 			dev_err(&pdev->dev, "Failed to stop RTC alarm: %d\n", | 
 | 378 | 				ret); | 
 | 379 | 	} | 
 | 380 |  | 
 | 381 | 	return ret; | 
 | 382 | } | 
 | 383 |  | 
| Mark Brown | 6f38b04 | 2009-12-15 16:46:09 -0800 | [diff] [blame] | 384 | static int wm8350_rtc_resume(struct device *dev) | 
| Mark Brown | 077eaf5 | 2008-11-12 13:27:04 -0800 | [diff] [blame] | 385 | { | 
| Mark Brown | 6f38b04 | 2009-12-15 16:46:09 -0800 | [diff] [blame] | 386 | 	struct platform_device *pdev = to_platform_device(dev); | 
| Mark Brown | 077eaf5 | 2008-11-12 13:27:04 -0800 | [diff] [blame] | 387 | 	struct wm8350 *wm8350 = dev_get_drvdata(&pdev->dev); | 
 | 388 | 	int ret; | 
 | 389 |  | 
 | 390 | 	if (wm8350->rtc.alarm_enabled) { | 
 | 391 | 		ret = wm8350_rtc_start_alarm(wm8350); | 
 | 392 | 		if (ret != 0) | 
 | 393 | 			dev_err(&pdev->dev, | 
 | 394 | 				"Failed to restart RTC alarm: %d\n", ret); | 
 | 395 | 	} | 
 | 396 |  | 
 | 397 | 	return 0; | 
 | 398 | } | 
 | 399 |  | 
 | 400 | #else | 
 | 401 | #define wm8350_rtc_suspend NULL | 
 | 402 | #define wm8350_rtc_resume NULL | 
 | 403 | #endif | 
 | 404 |  | 
 | 405 | static int wm8350_rtc_probe(struct platform_device *pdev) | 
 | 406 | { | 
 | 407 | 	struct wm8350 *wm8350 = platform_get_drvdata(pdev); | 
 | 408 | 	struct wm8350_rtc *wm_rtc = &wm8350->rtc; | 
 | 409 | 	int ret = 0; | 
 | 410 | 	u16 timectl, power5; | 
 | 411 |  | 
 | 412 | 	timectl = wm8350_reg_read(wm8350, WM8350_RTC_TIME_CONTROL); | 
 | 413 | 	if (timectl & WM8350_RTC_BCD) { | 
 | 414 | 		dev_err(&pdev->dev, "RTC BCD mode not supported\n"); | 
 | 415 | 		return -EINVAL; | 
 | 416 | 	} | 
 | 417 | 	if (timectl & WM8350_RTC_12HR) { | 
 | 418 | 		dev_err(&pdev->dev, "RTC 12 hour mode not supported\n"); | 
 | 419 | 		return -EINVAL; | 
 | 420 | 	} | 
 | 421 |  | 
 | 422 | 	/* enable the RTC if it's not already enabled */ | 
 | 423 | 	power5 = wm8350_reg_read(wm8350, WM8350_POWER_MGMT_5); | 
 | 424 | 	if (!(power5 &  WM8350_RTC_TICK_ENA)) { | 
 | 425 | 		dev_info(wm8350->dev, "Starting RTC\n"); | 
 | 426 |  | 
 | 427 | 		wm8350_reg_unlock(wm8350); | 
 | 428 |  | 
 | 429 | 		ret = wm8350_set_bits(wm8350, WM8350_POWER_MGMT_5, | 
 | 430 | 				      WM8350_RTC_TICK_ENA); | 
 | 431 | 		if (ret < 0) { | 
 | 432 | 			dev_err(&pdev->dev, "failed to enable RTC: %d\n", ret); | 
 | 433 | 			return ret; | 
 | 434 | 		} | 
 | 435 |  | 
 | 436 | 		wm8350_reg_lock(wm8350); | 
 | 437 | 	} | 
 | 438 |  | 
 | 439 | 	if (timectl & WM8350_RTC_STS) { | 
 | 440 | 		int retries; | 
 | 441 |  | 
 | 442 | 		ret = wm8350_clear_bits(wm8350, WM8350_RTC_TIME_CONTROL, | 
 | 443 | 					WM8350_RTC_SET); | 
 | 444 | 		if (ret < 0) { | 
 | 445 | 			dev_err(&pdev->dev, "failed to start: %d\n", ret); | 
 | 446 | 			return ret; | 
 | 447 | 		} | 
 | 448 |  | 
 | 449 | 		retries = WM8350_SET_TIME_RETRIES; | 
 | 450 | 		do { | 
 | 451 | 			timectl = wm8350_reg_read(wm8350, | 
 | 452 | 						  WM8350_RTC_TIME_CONTROL); | 
| Roel Kluin | 62da659 | 2009-03-31 15:24:59 -0700 | [diff] [blame] | 453 | 		} while (timectl & WM8350_RTC_STS && --retries); | 
| Mark Brown | 077eaf5 | 2008-11-12 13:27:04 -0800 | [diff] [blame] | 454 |  | 
 | 455 | 		if (retries == 0) { | 
 | 456 | 			dev_err(&pdev->dev, "failed to start: timeout\n"); | 
 | 457 | 			return -ENODEV; | 
 | 458 | 		} | 
 | 459 | 	} | 
 | 460 |  | 
 | 461 | 	device_init_wakeup(&pdev->dev, 1); | 
 | 462 |  | 
 | 463 | 	wm_rtc->rtc = rtc_device_register("wm8350", &pdev->dev, | 
 | 464 | 					  &wm8350_rtc_ops, THIS_MODULE); | 
 | 465 | 	if (IS_ERR(wm_rtc->rtc)) { | 
 | 466 | 		ret = PTR_ERR(wm_rtc->rtc); | 
 | 467 | 		dev_err(&pdev->dev, "failed to register RTC: %d\n", ret); | 
 | 468 | 		return ret; | 
 | 469 | 	} | 
 | 470 |  | 
| Mark Brown | 077eaf5 | 2008-11-12 13:27:04 -0800 | [diff] [blame] | 471 | 	wm8350_register_irq(wm8350, WM8350_IRQ_RTC_SEC, | 
| Mark Brown | 5a65edb | 2009-11-04 16:10:51 +0000 | [diff] [blame] | 472 | 			    wm8350_rtc_update_handler, 0, | 
 | 473 | 			    "RTC Seconds", wm8350); | 
| Mark Brown | 6a61274 | 2009-11-04 16:10:52 +0000 | [diff] [blame] | 474 | 	wm8350_mask_irq(wm8350, WM8350_IRQ_RTC_SEC); | 
| Mark Brown | 077eaf5 | 2008-11-12 13:27:04 -0800 | [diff] [blame] | 475 |  | 
 | 476 | 	wm8350_register_irq(wm8350, WM8350_IRQ_RTC_ALM, | 
| Mark Brown | 5a65edb | 2009-11-04 16:10:51 +0000 | [diff] [blame] | 477 | 			    wm8350_rtc_alarm_handler, 0, | 
 | 478 | 			    "RTC Alarm", wm8350); | 
| Mark Brown | 077eaf5 | 2008-11-12 13:27:04 -0800 | [diff] [blame] | 479 |  | 
 | 480 | 	return 0; | 
 | 481 | } | 
 | 482 |  | 
 | 483 | static int __devexit wm8350_rtc_remove(struct platform_device *pdev) | 
 | 484 | { | 
 | 485 | 	struct wm8350 *wm8350 = platform_get_drvdata(pdev); | 
 | 486 | 	struct wm8350_rtc *wm_rtc = &wm8350->rtc; | 
 | 487 |  | 
| Mark Brown | f99344f | 2010-01-05 13:59:07 +0000 | [diff] [blame] | 488 | 	wm8350_free_irq(wm8350, WM8350_IRQ_RTC_SEC, wm8350); | 
 | 489 | 	wm8350_free_irq(wm8350, WM8350_IRQ_RTC_ALM, wm8350); | 
| Mark Brown | 077eaf5 | 2008-11-12 13:27:04 -0800 | [diff] [blame] | 490 |  | 
 | 491 | 	rtc_device_unregister(wm_rtc->rtc); | 
 | 492 |  | 
 | 493 | 	return 0; | 
 | 494 | } | 
 | 495 |  | 
| Mark Brown | 6f38b04 | 2009-12-15 16:46:09 -0800 | [diff] [blame] | 496 | static struct dev_pm_ops wm8350_rtc_pm_ops = { | 
 | 497 | 	.suspend = wm8350_rtc_suspend, | 
 | 498 | 	.resume = wm8350_rtc_resume, | 
 | 499 | }; | 
 | 500 |  | 
| Mark Brown | 077eaf5 | 2008-11-12 13:27:04 -0800 | [diff] [blame] | 501 | static struct platform_driver wm8350_rtc_driver = { | 
 | 502 | 	.probe = wm8350_rtc_probe, | 
 | 503 | 	.remove = __devexit_p(wm8350_rtc_remove), | 
| Mark Brown | 077eaf5 | 2008-11-12 13:27:04 -0800 | [diff] [blame] | 504 | 	.driver = { | 
 | 505 | 		.name = "wm8350-rtc", | 
| Mark Brown | 6f38b04 | 2009-12-15 16:46:09 -0800 | [diff] [blame] | 506 | 		.pm = &wm8350_rtc_pm_ops, | 
| Mark Brown | 077eaf5 | 2008-11-12 13:27:04 -0800 | [diff] [blame] | 507 | 	}, | 
 | 508 | }; | 
 | 509 |  | 
 | 510 | static int __init wm8350_rtc_init(void) | 
 | 511 | { | 
 | 512 | 	return platform_driver_register(&wm8350_rtc_driver); | 
 | 513 | } | 
 | 514 | module_init(wm8350_rtc_init); | 
 | 515 |  | 
 | 516 | static void __exit wm8350_rtc_exit(void) | 
 | 517 | { | 
 | 518 | 	platform_driver_unregister(&wm8350_rtc_driver); | 
 | 519 | } | 
 | 520 | module_exit(wm8350_rtc_exit); | 
 | 521 |  | 
 | 522 | MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>"); | 
 | 523 | MODULE_DESCRIPTION("RTC driver for the WM8350"); | 
 | 524 | MODULE_LICENSE("GPL"); | 
 | 525 | MODULE_ALIAS("platform:wm8350-rtc"); |