Linux Audio

Check our new training course

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