xref: /linux/drivers/rtc/rtc-ab8500.c (revision e5c86679d5e864947a52fb31e45a425dea3e7fa9)
1 /*
2  * Copyright (C) ST-Ericsson SA 2010
3  *
4  * License terms: GNU General Public License (GPL) version 2
5  * Author: Virupax Sadashivpetimath <virupax.sadashivpetimath@stericsson.com>
6  *
7  * RTC clock driver for the RTC part of the AB8500 Power management chip.
8  * Based on RTC clock driver for the AB3100 Analog Baseband Chip by
9  * Linus Walleij <linus.walleij@stericsson.com>
10  */
11 
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/init.h>
15 #include <linux/platform_device.h>
16 #include <linux/rtc.h>
17 #include <linux/mfd/abx500.h>
18 #include <linux/mfd/abx500/ab8500.h>
19 #include <linux/delay.h>
20 #include <linux/of.h>
21 #include <linux/pm_wakeirq.h>
22 
23 #define AB8500_RTC_SOFF_STAT_REG	0x00
24 #define AB8500_RTC_CC_CONF_REG		0x01
25 #define AB8500_RTC_READ_REQ_REG		0x02
26 #define AB8500_RTC_WATCH_TSECMID_REG	0x03
27 #define AB8500_RTC_WATCH_TSECHI_REG	0x04
28 #define AB8500_RTC_WATCH_TMIN_LOW_REG	0x05
29 #define AB8500_RTC_WATCH_TMIN_MID_REG	0x06
30 #define AB8500_RTC_WATCH_TMIN_HI_REG	0x07
31 #define AB8500_RTC_ALRM_MIN_LOW_REG	0x08
32 #define AB8500_RTC_ALRM_MIN_MID_REG	0x09
33 #define AB8500_RTC_ALRM_MIN_HI_REG	0x0A
34 #define AB8500_RTC_STAT_REG		0x0B
35 #define AB8500_RTC_BKUP_CHG_REG		0x0C
36 #define AB8500_RTC_FORCE_BKUP_REG	0x0D
37 #define AB8500_RTC_CALIB_REG		0x0E
38 #define AB8500_RTC_SWITCH_STAT_REG	0x0F
39 #define AB8540_RTC_ALRM_SEC		0x22
40 #define AB8540_RTC_ALRM_MIN_LOW_REG	0x23
41 #define AB8540_RTC_ALRM_MIN_MID_REG	0x24
42 #define AB8540_RTC_ALRM_MIN_HI_REG	0x25
43 
44 /* RtcReadRequest bits */
45 #define RTC_READ_REQUEST		0x01
46 #define RTC_WRITE_REQUEST		0x02
47 
48 /* RtcCtrl bits */
49 #define RTC_ALARM_ENA			0x04
50 #define RTC_STATUS_DATA			0x01
51 
52 #define COUNTS_PER_SEC			(0xF000 / 60)
53 #define AB8500_RTC_EPOCH		2000
54 
55 static const u8 ab8500_rtc_time_regs[] = {
56 	AB8500_RTC_WATCH_TMIN_HI_REG, AB8500_RTC_WATCH_TMIN_MID_REG,
57 	AB8500_RTC_WATCH_TMIN_LOW_REG, AB8500_RTC_WATCH_TSECHI_REG,
58 	AB8500_RTC_WATCH_TSECMID_REG
59 };
60 
61 static const u8 ab8500_rtc_alarm_regs[] = {
62 	AB8500_RTC_ALRM_MIN_HI_REG, AB8500_RTC_ALRM_MIN_MID_REG,
63 	AB8500_RTC_ALRM_MIN_LOW_REG
64 };
65 
66 static const u8 ab8540_rtc_alarm_regs[] = {
67 	AB8540_RTC_ALRM_MIN_HI_REG, AB8540_RTC_ALRM_MIN_MID_REG,
68 	AB8540_RTC_ALRM_MIN_LOW_REG, AB8540_RTC_ALRM_SEC
69 };
70 
71 /* Calculate the seconds from 1970 to 01-01-2000 00:00:00 */
72 static unsigned long get_elapsed_seconds(int year)
73 {
74 	unsigned long secs;
75 	struct rtc_time tm = {
76 		.tm_year = year - 1900,
77 		.tm_mday = 1,
78 	};
79 
80 	/*
81 	 * This function calculates secs from 1970 and not from
82 	 * 1900, even if we supply the offset from year 1900.
83 	 */
84 	rtc_tm_to_time(&tm, &secs);
85 	return secs;
86 }
87 
88 static int ab8500_rtc_read_time(struct device *dev, struct rtc_time *tm)
89 {
90 	unsigned long timeout = jiffies + HZ;
91 	int retval, i;
92 	unsigned long mins, secs;
93 	unsigned char buf[ARRAY_SIZE(ab8500_rtc_time_regs)];
94 	u8 value;
95 
96 	/* Request a data read */
97 	retval = abx500_set_register_interruptible(dev,
98 		AB8500_RTC, AB8500_RTC_READ_REQ_REG, RTC_READ_REQUEST);
99 	if (retval < 0)
100 		return retval;
101 
102 	/* Wait for some cycles after enabling the rtc read in ab8500 */
103 	while (time_before(jiffies, timeout)) {
104 		retval = abx500_get_register_interruptible(dev,
105 			AB8500_RTC, AB8500_RTC_READ_REQ_REG, &value);
106 		if (retval < 0)
107 			return retval;
108 
109 		if (!(value & RTC_READ_REQUEST))
110 			break;
111 
112 		usleep_range(1000, 5000);
113 	}
114 
115 	/* Read the Watchtime registers */
116 	for (i = 0; i < ARRAY_SIZE(ab8500_rtc_time_regs); i++) {
117 		retval = abx500_get_register_interruptible(dev,
118 			AB8500_RTC, ab8500_rtc_time_regs[i], &value);
119 		if (retval < 0)
120 			return retval;
121 		buf[i] = value;
122 	}
123 
124 	mins = (buf[0] << 16) | (buf[1] << 8) | buf[2];
125 
126 	secs =	(buf[3] << 8) | buf[4];
127 	secs =	secs / COUNTS_PER_SEC;
128 	secs =	secs + (mins * 60);
129 
130 	/* Add back the initially subtracted number of seconds */
131 	secs += get_elapsed_seconds(AB8500_RTC_EPOCH);
132 
133 	rtc_time_to_tm(secs, tm);
134 	return rtc_valid_tm(tm);
135 }
136 
137 static int ab8500_rtc_set_time(struct device *dev, struct rtc_time *tm)
138 {
139 	int retval, i;
140 	unsigned char buf[ARRAY_SIZE(ab8500_rtc_time_regs)];
141 	unsigned long no_secs, no_mins, secs = 0;
142 
143 	if (tm->tm_year < (AB8500_RTC_EPOCH - 1900)) {
144 		dev_dbg(dev, "year should be equal to or greater than %d\n",
145 				AB8500_RTC_EPOCH);
146 		return -EINVAL;
147 	}
148 
149 	/* Get the number of seconds since 1970 */
150 	rtc_tm_to_time(tm, &secs);
151 
152 	/*
153 	 * Convert it to the number of seconds since 01-01-2000 00:00:00, since
154 	 * we only have a small counter in the RTC.
155 	 */
156 	secs -= get_elapsed_seconds(AB8500_RTC_EPOCH);
157 
158 	no_mins = secs / 60;
159 
160 	no_secs = secs % 60;
161 	/* Make the seconds count as per the RTC resolution */
162 	no_secs = no_secs * COUNTS_PER_SEC;
163 
164 	buf[4] = no_secs & 0xFF;
165 	buf[3] = (no_secs >> 8) & 0xFF;
166 
167 	buf[2] = no_mins & 0xFF;
168 	buf[1] = (no_mins >> 8) & 0xFF;
169 	buf[0] = (no_mins >> 16) & 0xFF;
170 
171 	for (i = 0; i < ARRAY_SIZE(ab8500_rtc_time_regs); i++) {
172 		retval = abx500_set_register_interruptible(dev, AB8500_RTC,
173 			ab8500_rtc_time_regs[i], buf[i]);
174 		if (retval < 0)
175 			return retval;
176 	}
177 
178 	/* Request a data write */
179 	return abx500_set_register_interruptible(dev, AB8500_RTC,
180 		AB8500_RTC_READ_REQ_REG, RTC_WRITE_REQUEST);
181 }
182 
183 static int ab8500_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
184 {
185 	int retval, i;
186 	u8 rtc_ctrl, value;
187 	unsigned char buf[ARRAY_SIZE(ab8500_rtc_alarm_regs)];
188 	unsigned long secs, mins;
189 
190 	/* Check if the alarm is enabled or not */
191 	retval = abx500_get_register_interruptible(dev, AB8500_RTC,
192 		AB8500_RTC_STAT_REG, &rtc_ctrl);
193 	if (retval < 0)
194 		return retval;
195 
196 	if (rtc_ctrl & RTC_ALARM_ENA)
197 		alarm->enabled = 1;
198 	else
199 		alarm->enabled = 0;
200 
201 	alarm->pending = 0;
202 
203 	for (i = 0; i < ARRAY_SIZE(ab8500_rtc_alarm_regs); i++) {
204 		retval = abx500_get_register_interruptible(dev, AB8500_RTC,
205 			ab8500_rtc_alarm_regs[i], &value);
206 		if (retval < 0)
207 			return retval;
208 		buf[i] = value;
209 	}
210 
211 	mins = (buf[0] << 16) | (buf[1] << 8) | (buf[2]);
212 	secs = mins * 60;
213 
214 	/* Add back the initially subtracted number of seconds */
215 	secs += get_elapsed_seconds(AB8500_RTC_EPOCH);
216 
217 	rtc_time_to_tm(secs, &alarm->time);
218 
219 	return rtc_valid_tm(&alarm->time);
220 }
221 
222 static int ab8500_rtc_irq_enable(struct device *dev, unsigned int enabled)
223 {
224 	return abx500_mask_and_set_register_interruptible(dev, AB8500_RTC,
225 		AB8500_RTC_STAT_REG, RTC_ALARM_ENA,
226 		enabled ? RTC_ALARM_ENA : 0);
227 }
228 
229 static int ab8500_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
230 {
231 	int retval, i;
232 	unsigned char buf[ARRAY_SIZE(ab8500_rtc_alarm_regs)];
233 	unsigned long mins, secs = 0, cursec = 0;
234 	struct rtc_time curtm;
235 
236 	if (alarm->time.tm_year < (AB8500_RTC_EPOCH - 1900)) {
237 		dev_dbg(dev, "year should be equal to or greater than %d\n",
238 				AB8500_RTC_EPOCH);
239 		return -EINVAL;
240 	}
241 
242 	/* Get the number of seconds since 1970 */
243 	rtc_tm_to_time(&alarm->time, &secs);
244 
245 	/*
246 	 * Check whether alarm is set less than 1min.
247 	 * Since our RTC doesn't support alarm resolution less than 1min,
248 	 * return -EINVAL, so UIE EMUL can take it up, incase of UIE_ON
249 	 */
250 	ab8500_rtc_read_time(dev, &curtm); /* Read current time */
251 	rtc_tm_to_time(&curtm, &cursec);
252 	if ((secs - cursec) < 59) {
253 		dev_dbg(dev, "Alarm less than 1 minute not supported\r\n");
254 		return -EINVAL;
255 	}
256 
257 	/*
258 	 * Convert it to the number of seconds since 01-01-2000 00:00:00, since
259 	 * we only have a small counter in the RTC.
260 	 */
261 	secs -= get_elapsed_seconds(AB8500_RTC_EPOCH);
262 
263 	mins = secs / 60;
264 
265 	buf[2] = mins & 0xFF;
266 	buf[1] = (mins >> 8) & 0xFF;
267 	buf[0] = (mins >> 16) & 0xFF;
268 
269 	/* Set the alarm time */
270 	for (i = 0; i < ARRAY_SIZE(ab8500_rtc_alarm_regs); i++) {
271 		retval = abx500_set_register_interruptible(dev, AB8500_RTC,
272 			ab8500_rtc_alarm_regs[i], buf[i]);
273 		if (retval < 0)
274 			return retval;
275 	}
276 
277 	return ab8500_rtc_irq_enable(dev, alarm->enabled);
278 }
279 
280 static int ab8540_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
281 {
282 	int retval, i;
283 	unsigned char buf[ARRAY_SIZE(ab8540_rtc_alarm_regs)];
284 	unsigned long mins, secs = 0;
285 
286 	if (alarm->time.tm_year < (AB8500_RTC_EPOCH - 1900)) {
287 		dev_dbg(dev, "year should be equal to or greater than %d\n",
288 				AB8500_RTC_EPOCH);
289 		return -EINVAL;
290 	}
291 
292 	/* Get the number of seconds since 1970 */
293 	rtc_tm_to_time(&alarm->time, &secs);
294 
295 	/*
296 	 * Convert it to the number of seconds since 01-01-2000 00:00:00
297 	 */
298 	secs -= get_elapsed_seconds(AB8500_RTC_EPOCH);
299 	mins = secs / 60;
300 
301 	buf[3] = secs % 60;
302 	buf[2] = mins & 0xFF;
303 	buf[1] = (mins >> 8) & 0xFF;
304 	buf[0] = (mins >> 16) & 0xFF;
305 
306 	/* Set the alarm time */
307 	for (i = 0; i < ARRAY_SIZE(ab8540_rtc_alarm_regs); i++) {
308 		retval = abx500_set_register_interruptible(dev, AB8500_RTC,
309 			ab8540_rtc_alarm_regs[i], buf[i]);
310 		if (retval < 0)
311 			return retval;
312 	}
313 
314 	return ab8500_rtc_irq_enable(dev, alarm->enabled);
315 }
316 
317 static int ab8500_rtc_set_calibration(struct device *dev, int calibration)
318 {
319 	int retval;
320 	u8  rtccal = 0;
321 
322 	/*
323 	 * Check that the calibration value (which is in units of 0.5
324 	 * parts-per-million) is in the AB8500's range for RtcCalibration
325 	 * register. -128 (0x80) is not permitted because the AB8500 uses
326 	 * a sign-bit rather than two's complement, so 0x80 is just another
327 	 * representation of zero.
328 	 */
329 	if ((calibration < -127) || (calibration > 127)) {
330 		dev_err(dev, "RtcCalibration value outside permitted range\n");
331 		return -EINVAL;
332 	}
333 
334 	/*
335 	 * The AB8500 uses sign (in bit7) and magnitude (in bits0-7)
336 	 * so need to convert to this sort of representation before writing
337 	 * into RtcCalibration register...
338 	 */
339 	if (calibration >= 0)
340 		rtccal = 0x7F & calibration;
341 	else
342 		rtccal = ~(calibration - 1) | 0x80;
343 
344 	retval = abx500_set_register_interruptible(dev, AB8500_RTC,
345 			AB8500_RTC_CALIB_REG, rtccal);
346 
347 	return retval;
348 }
349 
350 static int ab8500_rtc_get_calibration(struct device *dev, int *calibration)
351 {
352 	int retval;
353 	u8  rtccal = 0;
354 
355 	retval =  abx500_get_register_interruptible(dev, AB8500_RTC,
356 			AB8500_RTC_CALIB_REG, &rtccal);
357 	if (retval >= 0) {
358 		/*
359 		 * The AB8500 uses sign (in bit7) and magnitude (in bits0-7)
360 		 * so need to convert value from RtcCalibration register into
361 		 * a two's complement signed value...
362 		 */
363 		if (rtccal & 0x80)
364 			*calibration = 0 - (rtccal & 0x7F);
365 		else
366 			*calibration = 0x7F & rtccal;
367 	}
368 
369 	return retval;
370 }
371 
372 static ssize_t ab8500_sysfs_store_rtc_calibration(struct device *dev,
373 				struct device_attribute *attr,
374 				const char *buf, size_t count)
375 {
376 	int retval;
377 	int calibration = 0;
378 
379 	if (sscanf(buf, " %i ", &calibration) != 1) {
380 		dev_err(dev, "Failed to store RTC calibration attribute\n");
381 		return -EINVAL;
382 	}
383 
384 	retval = ab8500_rtc_set_calibration(dev, calibration);
385 
386 	return retval ? retval : count;
387 }
388 
389 static ssize_t ab8500_sysfs_show_rtc_calibration(struct device *dev,
390 				struct device_attribute *attr, char *buf)
391 {
392 	int  retval = 0;
393 	int  calibration = 0;
394 
395 	retval = ab8500_rtc_get_calibration(dev, &calibration);
396 	if (retval < 0) {
397 		dev_err(dev, "Failed to read RTC calibration attribute\n");
398 		sprintf(buf, "0\n");
399 		return retval;
400 	}
401 
402 	return sprintf(buf, "%d\n", calibration);
403 }
404 
405 static DEVICE_ATTR(rtc_calibration, S_IRUGO | S_IWUSR,
406 		   ab8500_sysfs_show_rtc_calibration,
407 		   ab8500_sysfs_store_rtc_calibration);
408 
409 static int ab8500_sysfs_rtc_register(struct device *dev)
410 {
411 	return device_create_file(dev, &dev_attr_rtc_calibration);
412 }
413 
414 static void ab8500_sysfs_rtc_unregister(struct device *dev)
415 {
416 	device_remove_file(dev, &dev_attr_rtc_calibration);
417 }
418 
419 static irqreturn_t rtc_alarm_handler(int irq, void *data)
420 {
421 	struct rtc_device *rtc = data;
422 	unsigned long events = RTC_IRQF | RTC_AF;
423 
424 	dev_dbg(&rtc->dev, "%s\n", __func__);
425 	rtc_update_irq(rtc, 1, events);
426 
427 	return IRQ_HANDLED;
428 }
429 
430 static const struct rtc_class_ops ab8500_rtc_ops = {
431 	.read_time		= ab8500_rtc_read_time,
432 	.set_time		= ab8500_rtc_set_time,
433 	.read_alarm		= ab8500_rtc_read_alarm,
434 	.set_alarm		= ab8500_rtc_set_alarm,
435 	.alarm_irq_enable	= ab8500_rtc_irq_enable,
436 };
437 
438 static const struct rtc_class_ops ab8540_rtc_ops = {
439 	.read_time		= ab8500_rtc_read_time,
440 	.set_time		= ab8500_rtc_set_time,
441 	.read_alarm		= ab8500_rtc_read_alarm,
442 	.set_alarm		= ab8540_rtc_set_alarm,
443 	.alarm_irq_enable	= ab8500_rtc_irq_enable,
444 };
445 
446 static const struct platform_device_id ab85xx_rtc_ids[] = {
447 	{ "ab8500-rtc", (kernel_ulong_t)&ab8500_rtc_ops, },
448 	{ "ab8540-rtc", (kernel_ulong_t)&ab8540_rtc_ops, },
449 	{ /* sentinel */ }
450 };
451 MODULE_DEVICE_TABLE(platform, ab85xx_rtc_ids);
452 
453 static int ab8500_rtc_probe(struct platform_device *pdev)
454 {
455 	const struct platform_device_id *platid = platform_get_device_id(pdev);
456 	int err;
457 	struct rtc_device *rtc;
458 	u8 rtc_ctrl;
459 	int irq;
460 
461 	irq = platform_get_irq_byname(pdev, "ALARM");
462 	if (irq < 0)
463 		return irq;
464 
465 	/* For RTC supply test */
466 	err = abx500_mask_and_set_register_interruptible(&pdev->dev, AB8500_RTC,
467 		AB8500_RTC_STAT_REG, RTC_STATUS_DATA, RTC_STATUS_DATA);
468 	if (err < 0)
469 		return err;
470 
471 	/* Wait for reset by the PorRtc */
472 	usleep_range(1000, 5000);
473 
474 	err = abx500_get_register_interruptible(&pdev->dev, AB8500_RTC,
475 		AB8500_RTC_STAT_REG, &rtc_ctrl);
476 	if (err < 0)
477 		return err;
478 
479 	/* Check if the RTC Supply fails */
480 	if (!(rtc_ctrl & RTC_STATUS_DATA)) {
481 		dev_err(&pdev->dev, "RTC supply failure\n");
482 		return -ENODEV;
483 	}
484 
485 	device_init_wakeup(&pdev->dev, true);
486 
487 	rtc = devm_rtc_device_register(&pdev->dev, "ab8500-rtc",
488 				(struct rtc_class_ops *)platid->driver_data,
489 				THIS_MODULE);
490 	if (IS_ERR(rtc)) {
491 		dev_err(&pdev->dev, "Registration failed\n");
492 		err = PTR_ERR(rtc);
493 		return err;
494 	}
495 
496 	err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
497 			rtc_alarm_handler, IRQF_ONESHOT,
498 			"ab8500-rtc", rtc);
499 	if (err < 0)
500 		return err;
501 
502 	dev_pm_set_wake_irq(&pdev->dev, irq);
503 	platform_set_drvdata(pdev, rtc);
504 
505 	err = ab8500_sysfs_rtc_register(&pdev->dev);
506 	if (err) {
507 		dev_err(&pdev->dev, "sysfs RTC failed to register\n");
508 		return err;
509 	}
510 
511 	rtc->uie_unsupported = 1;
512 
513 	return 0;
514 }
515 
516 static int ab8500_rtc_remove(struct platform_device *pdev)
517 {
518 	dev_pm_clear_wake_irq(&pdev->dev);
519 	device_init_wakeup(&pdev->dev, false);
520 	ab8500_sysfs_rtc_unregister(&pdev->dev);
521 
522 	return 0;
523 }
524 
525 static struct platform_driver ab8500_rtc_driver = {
526 	.driver = {
527 		.name = "ab8500-rtc",
528 	},
529 	.probe	= ab8500_rtc_probe,
530 	.remove = ab8500_rtc_remove,
531 	.id_table = ab85xx_rtc_ids,
532 };
533 
534 module_platform_driver(ab8500_rtc_driver);
535 
536 MODULE_AUTHOR("Virupax Sadashivpetimath <virupax.sadashivpetimath@stericsson.com>");
537 MODULE_DESCRIPTION("AB8500 RTC Driver");
538 MODULE_LICENSE("GPL v2");
539