xref: /linux/drivers/rtc/rtc-ds3232.c (revision ca55b2fef3a9373fcfc30f82fd26bc7fccbda732)
1 /*
2  * RTC client/driver for the Maxim/Dallas DS3232 Real-Time Clock over I2C
3  *
4  * Copyright (C) 2009-2011 Freescale Semiconductor.
5  * Author: Jack Lan <jack.lan@freescale.com>
6  *
7  * This program is free software; you can redistribute  it and/or modify it
8  * under  the terms of  the GNU General  Public License as published by the
9  * Free Software Foundation;  either version 2 of the  License, or (at your
10  * option) any later version.
11  */
12 /*
13  * It would be more efficient to use i2c msgs/i2c_transfer directly but, as
14  * recommened in .../Documentation/i2c/writing-clients section
15  * "Sending and receiving", using SMBus level communication is preferred.
16  */
17 
18 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19 
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/interrupt.h>
23 #include <linux/i2c.h>
24 #include <linux/rtc.h>
25 #include <linux/bcd.h>
26 #include <linux/workqueue.h>
27 #include <linux/slab.h>
28 
29 #define DS3232_REG_SECONDS	0x00
30 #define DS3232_REG_MINUTES	0x01
31 #define DS3232_REG_HOURS	0x02
32 #define DS3232_REG_AMPM		0x02
33 #define DS3232_REG_DAY		0x03
34 #define DS3232_REG_DATE		0x04
35 #define DS3232_REG_MONTH	0x05
36 #define DS3232_REG_CENTURY	0x05
37 #define DS3232_REG_YEAR		0x06
38 #define DS3232_REG_ALARM1         0x07	/* Alarm 1 BASE */
39 #define DS3232_REG_ALARM2         0x0B	/* Alarm 2 BASE */
40 #define DS3232_REG_CR		0x0E	/* Control register */
41 #	define DS3232_REG_CR_nEOSC        0x80
42 #       define DS3232_REG_CR_INTCN        0x04
43 #       define DS3232_REG_CR_A2IE        0x02
44 #       define DS3232_REG_CR_A1IE        0x01
45 
46 #define DS3232_REG_SR	0x0F	/* control/status register */
47 #	define DS3232_REG_SR_OSF   0x80
48 #       define DS3232_REG_SR_BSY   0x04
49 #       define DS3232_REG_SR_A2F   0x02
50 #       define DS3232_REG_SR_A1F   0x01
51 
52 struct ds3232 {
53 	struct i2c_client *client;
54 	struct rtc_device *rtc;
55 	struct work_struct work;
56 
57 	/* The mutex protects alarm operations, and prevents a race
58 	 * between the enable_irq() in the workqueue and the free_irq()
59 	 * in the remove function.
60 	 */
61 	struct mutex mutex;
62 	bool suspended;
63 	int exiting;
64 };
65 
66 static struct i2c_driver ds3232_driver;
67 
68 static int ds3232_check_rtc_status(struct i2c_client *client)
69 {
70 	int ret = 0;
71 	int control, stat;
72 
73 	stat = i2c_smbus_read_byte_data(client, DS3232_REG_SR);
74 	if (stat < 0)
75 		return stat;
76 
77 	if (stat & DS3232_REG_SR_OSF)
78 		dev_warn(&client->dev,
79 				"oscillator discontinuity flagged, "
80 				"time unreliable\n");
81 
82 	stat &= ~(DS3232_REG_SR_OSF | DS3232_REG_SR_A1F | DS3232_REG_SR_A2F);
83 
84 	ret = i2c_smbus_write_byte_data(client, DS3232_REG_SR, stat);
85 	if (ret < 0)
86 		return ret;
87 
88 	/* If the alarm is pending, clear it before requesting
89 	 * the interrupt, so an interrupt event isn't reported
90 	 * before everything is initialized.
91 	 */
92 
93 	control = i2c_smbus_read_byte_data(client, DS3232_REG_CR);
94 	if (control < 0)
95 		return control;
96 
97 	control &= ~(DS3232_REG_CR_A1IE | DS3232_REG_CR_A2IE);
98 	control |= DS3232_REG_CR_INTCN;
99 
100 	return i2c_smbus_write_byte_data(client, DS3232_REG_CR, control);
101 }
102 
103 static int ds3232_read_time(struct device *dev, struct rtc_time *time)
104 {
105 	struct i2c_client *client = to_i2c_client(dev);
106 	int ret;
107 	u8 buf[7];
108 	unsigned int year, month, day, hour, minute, second;
109 	unsigned int week, twelve_hr, am_pm;
110 	unsigned int century, add_century = 0;
111 
112 	ret = i2c_smbus_read_i2c_block_data(client, DS3232_REG_SECONDS, 7, buf);
113 
114 	if (ret < 0)
115 		return ret;
116 	if (ret < 7)
117 		return -EIO;
118 
119 	second = buf[0];
120 	minute = buf[1];
121 	hour = buf[2];
122 	week = buf[3];
123 	day = buf[4];
124 	month = buf[5];
125 	year = buf[6];
126 
127 	/* Extract additional information for AM/PM and century */
128 
129 	twelve_hr = hour & 0x40;
130 	am_pm = hour & 0x20;
131 	century = month & 0x80;
132 
133 	/* Write to rtc_time structure */
134 
135 	time->tm_sec = bcd2bin(second);
136 	time->tm_min = bcd2bin(minute);
137 	if (twelve_hr) {
138 		/* Convert to 24 hr */
139 		if (am_pm)
140 			time->tm_hour = bcd2bin(hour & 0x1F) + 12;
141 		else
142 			time->tm_hour = bcd2bin(hour & 0x1F);
143 	} else {
144 		time->tm_hour = bcd2bin(hour);
145 	}
146 
147 	/* Day of the week in linux range is 0~6 while 1~7 in RTC chip */
148 	time->tm_wday = bcd2bin(week) - 1;
149 	time->tm_mday = bcd2bin(day);
150 	/* linux tm_mon range:0~11, while month range is 1~12 in RTC chip */
151 	time->tm_mon = bcd2bin(month & 0x7F) - 1;
152 	if (century)
153 		add_century = 100;
154 
155 	time->tm_year = bcd2bin(year) + add_century;
156 
157 	return rtc_valid_tm(time);
158 }
159 
160 static int ds3232_set_time(struct device *dev, struct rtc_time *time)
161 {
162 	struct i2c_client *client = to_i2c_client(dev);
163 	u8 buf[7];
164 
165 	/* Extract time from rtc_time and load into ds3232*/
166 
167 	buf[0] = bin2bcd(time->tm_sec);
168 	buf[1] = bin2bcd(time->tm_min);
169 	buf[2] = bin2bcd(time->tm_hour);
170 	/* Day of the week in linux range is 0~6 while 1~7 in RTC chip */
171 	buf[3] = bin2bcd(time->tm_wday + 1);
172 	buf[4] = bin2bcd(time->tm_mday); /* Date */
173 	/* linux tm_mon range:0~11, while month range is 1~12 in RTC chip */
174 	buf[5] = bin2bcd(time->tm_mon + 1);
175 	if (time->tm_year >= 100) {
176 		buf[5] |= 0x80;
177 		buf[6] = bin2bcd(time->tm_year - 100);
178 	} else {
179 		buf[6] = bin2bcd(time->tm_year);
180 	}
181 
182 	return i2c_smbus_write_i2c_block_data(client,
183 					      DS3232_REG_SECONDS, 7, buf);
184 }
185 
186 /*
187  * DS3232 has two alarm, we only use alarm1
188  * According to linux specification, only support one-shot alarm
189  * no periodic alarm mode
190  */
191 static int ds3232_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
192 {
193 	struct i2c_client *client = to_i2c_client(dev);
194 	struct ds3232 *ds3232 = i2c_get_clientdata(client);
195 	int control, stat;
196 	int ret;
197 	u8 buf[4];
198 
199 	mutex_lock(&ds3232->mutex);
200 
201 	ret = i2c_smbus_read_byte_data(client, DS3232_REG_SR);
202 	if (ret < 0)
203 		goto out;
204 	stat = ret;
205 	ret = i2c_smbus_read_byte_data(client, DS3232_REG_CR);
206 	if (ret < 0)
207 		goto out;
208 	control = ret;
209 	ret = i2c_smbus_read_i2c_block_data(client, DS3232_REG_ALARM1, 4, buf);
210 	if (ret < 0)
211 		goto out;
212 
213 	alarm->time.tm_sec = bcd2bin(buf[0] & 0x7F);
214 	alarm->time.tm_min = bcd2bin(buf[1] & 0x7F);
215 	alarm->time.tm_hour = bcd2bin(buf[2] & 0x7F);
216 	alarm->time.tm_mday = bcd2bin(buf[3] & 0x7F);
217 
218 	alarm->time.tm_mon = -1;
219 	alarm->time.tm_year = -1;
220 	alarm->time.tm_wday = -1;
221 	alarm->time.tm_yday = -1;
222 	alarm->time.tm_isdst = -1;
223 
224 	alarm->enabled = !!(control & DS3232_REG_CR_A1IE);
225 	alarm->pending = !!(stat & DS3232_REG_SR_A1F);
226 
227 	ret = 0;
228 out:
229 	mutex_unlock(&ds3232->mutex);
230 	return ret;
231 }
232 
233 /*
234  * linux rtc-module does not support wday alarm
235  * and only 24h time mode supported indeed
236  */
237 static int ds3232_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
238 {
239 	struct i2c_client *client = to_i2c_client(dev);
240 	struct ds3232 *ds3232 = i2c_get_clientdata(client);
241 	int control, stat;
242 	int ret;
243 	u8 buf[4];
244 
245 	if (client->irq <= 0)
246 		return -EINVAL;
247 
248 	mutex_lock(&ds3232->mutex);
249 
250 	buf[0] = bin2bcd(alarm->time.tm_sec);
251 	buf[1] = bin2bcd(alarm->time.tm_min);
252 	buf[2] = bin2bcd(alarm->time.tm_hour);
253 	buf[3] = bin2bcd(alarm->time.tm_mday);
254 
255 	/* clear alarm interrupt enable bit */
256 	ret = i2c_smbus_read_byte_data(client, DS3232_REG_CR);
257 	if (ret < 0)
258 		goto out;
259 	control = ret;
260 	control &= ~(DS3232_REG_CR_A1IE | DS3232_REG_CR_A2IE);
261 	ret = i2c_smbus_write_byte_data(client, DS3232_REG_CR, control);
262 	if (ret < 0)
263 		goto out;
264 
265 	/* clear any pending alarm flag */
266 	ret = i2c_smbus_read_byte_data(client, DS3232_REG_SR);
267 	if (ret < 0)
268 		goto out;
269 	stat = ret;
270 	stat &= ~(DS3232_REG_SR_A1F | DS3232_REG_SR_A2F);
271 	ret = i2c_smbus_write_byte_data(client, DS3232_REG_SR, stat);
272 	if (ret < 0)
273 		goto out;
274 
275 	ret = i2c_smbus_write_i2c_block_data(client, DS3232_REG_ALARM1, 4, buf);
276 
277 	if (alarm->enabled) {
278 		control |= DS3232_REG_CR_A1IE;
279 		ret = i2c_smbus_write_byte_data(client, DS3232_REG_CR, control);
280 	}
281 out:
282 	mutex_unlock(&ds3232->mutex);
283 	return ret;
284 }
285 
286 static void ds3232_update_alarm(struct i2c_client *client)
287 {
288 	struct ds3232 *ds3232 = i2c_get_clientdata(client);
289 	int control;
290 	int ret;
291 	u8 buf[4];
292 
293 	mutex_lock(&ds3232->mutex);
294 
295 	ret = i2c_smbus_read_i2c_block_data(client, DS3232_REG_ALARM1, 4, buf);
296 	if (ret < 0)
297 		goto unlock;
298 
299 	buf[0] = bcd2bin(buf[0]) < 0 || (ds3232->rtc->irq_data & RTC_UF) ?
300 								0x80 : buf[0];
301 	buf[1] = bcd2bin(buf[1]) < 0 || (ds3232->rtc->irq_data & RTC_UF) ?
302 								0x80 : buf[1];
303 	buf[2] = bcd2bin(buf[2]) < 0 || (ds3232->rtc->irq_data & RTC_UF) ?
304 								0x80 : buf[2];
305 	buf[3] = bcd2bin(buf[3]) < 0 || (ds3232->rtc->irq_data & RTC_UF) ?
306 								0x80 : buf[3];
307 
308 	ret = i2c_smbus_write_i2c_block_data(client, DS3232_REG_ALARM1, 4, buf);
309 	if (ret < 0)
310 		goto unlock;
311 
312 	control = i2c_smbus_read_byte_data(client, DS3232_REG_CR);
313 	if (control < 0)
314 		goto unlock;
315 
316 	if (ds3232->rtc->irq_data & (RTC_AF | RTC_UF))
317 		/* enable alarm1 interrupt */
318 		control |= DS3232_REG_CR_A1IE;
319 	else
320 		/* disable alarm1 interrupt */
321 		control &= ~(DS3232_REG_CR_A1IE);
322 	i2c_smbus_write_byte_data(client, DS3232_REG_CR, control);
323 
324 unlock:
325 	mutex_unlock(&ds3232->mutex);
326 }
327 
328 static int ds3232_alarm_irq_enable(struct device *dev, unsigned int enabled)
329 {
330 	struct i2c_client *client = to_i2c_client(dev);
331 	struct ds3232 *ds3232 = i2c_get_clientdata(client);
332 
333 	if (client->irq <= 0)
334 		return -EINVAL;
335 
336 	if (enabled)
337 		ds3232->rtc->irq_data |= RTC_AF;
338 	else
339 		ds3232->rtc->irq_data &= ~RTC_AF;
340 
341 	ds3232_update_alarm(client);
342 	return 0;
343 }
344 
345 static irqreturn_t ds3232_irq(int irq, void *dev_id)
346 {
347 	struct i2c_client *client = dev_id;
348 	struct ds3232 *ds3232 = i2c_get_clientdata(client);
349 
350 	disable_irq_nosync(irq);
351 
352 	/*
353 	 * If rtc as a wakeup source, can't schedule the work
354 	 * at system resume flow, because at this time the i2c bus
355 	 * has not been resumed.
356 	 */
357 	if (!ds3232->suspended)
358 		schedule_work(&ds3232->work);
359 
360 	return IRQ_HANDLED;
361 }
362 
363 static void ds3232_work(struct work_struct *work)
364 {
365 	struct ds3232 *ds3232 = container_of(work, struct ds3232, work);
366 	struct i2c_client *client = ds3232->client;
367 	int stat, control;
368 
369 	mutex_lock(&ds3232->mutex);
370 
371 	stat = i2c_smbus_read_byte_data(client, DS3232_REG_SR);
372 	if (stat < 0)
373 		goto unlock;
374 
375 	if (stat & DS3232_REG_SR_A1F) {
376 		control = i2c_smbus_read_byte_data(client, DS3232_REG_CR);
377 		if (control < 0) {
378 			pr_warn("Read Control Register error - Disable IRQ%d\n",
379 				client->irq);
380 		} else {
381 			/* disable alarm1 interrupt */
382 			control &= ~(DS3232_REG_CR_A1IE);
383 			i2c_smbus_write_byte_data(client, DS3232_REG_CR,
384 						control);
385 
386 			/* clear the alarm pend flag */
387 			stat &= ~DS3232_REG_SR_A1F;
388 			i2c_smbus_write_byte_data(client, DS3232_REG_SR, stat);
389 
390 			rtc_update_irq(ds3232->rtc, 1, RTC_AF | RTC_IRQF);
391 
392 			if (!ds3232->exiting)
393 				enable_irq(client->irq);
394 		}
395 	}
396 
397 unlock:
398 	mutex_unlock(&ds3232->mutex);
399 }
400 
401 static const struct rtc_class_ops ds3232_rtc_ops = {
402 	.read_time = ds3232_read_time,
403 	.set_time = ds3232_set_time,
404 	.read_alarm = ds3232_read_alarm,
405 	.set_alarm = ds3232_set_alarm,
406 	.alarm_irq_enable = ds3232_alarm_irq_enable,
407 };
408 
409 static int ds3232_probe(struct i2c_client *client,
410 			const struct i2c_device_id *id)
411 {
412 	struct ds3232 *ds3232;
413 	int ret;
414 
415 	ds3232 = devm_kzalloc(&client->dev, sizeof(struct ds3232), GFP_KERNEL);
416 	if (!ds3232)
417 		return -ENOMEM;
418 
419 	ds3232->client = client;
420 	i2c_set_clientdata(client, ds3232);
421 
422 	INIT_WORK(&ds3232->work, ds3232_work);
423 	mutex_init(&ds3232->mutex);
424 
425 	ret = ds3232_check_rtc_status(client);
426 	if (ret)
427 		return ret;
428 
429 	if (client->irq > 0) {
430 		ret = devm_request_irq(&client->dev, client->irq, ds3232_irq,
431 				       IRQF_SHARED, "ds3232", client);
432 		if (ret) {
433 			dev_err(&client->dev, "unable to request IRQ\n");
434 		}
435 		device_init_wakeup(&client->dev, 1);
436 	}
437 	ds3232->rtc = devm_rtc_device_register(&client->dev, client->name,
438 					  &ds3232_rtc_ops, THIS_MODULE);
439 	return PTR_ERR_OR_ZERO(ds3232->rtc);
440 }
441 
442 static int ds3232_remove(struct i2c_client *client)
443 {
444 	struct ds3232 *ds3232 = i2c_get_clientdata(client);
445 
446 	if (client->irq > 0) {
447 		mutex_lock(&ds3232->mutex);
448 		ds3232->exiting = 1;
449 		mutex_unlock(&ds3232->mutex);
450 
451 		devm_free_irq(&client->dev, client->irq, client);
452 		cancel_work_sync(&ds3232->work);
453 	}
454 
455 	return 0;
456 }
457 
458 #ifdef CONFIG_PM_SLEEP
459 static int ds3232_suspend(struct device *dev)
460 {
461 	struct ds3232 *ds3232 = dev_get_drvdata(dev);
462 	struct i2c_client *client = to_i2c_client(dev);
463 
464 	if (device_can_wakeup(dev)) {
465 		ds3232->suspended = true;
466 		if (irq_set_irq_wake(client->irq, 1)) {
467 			dev_warn_once(dev, "Cannot set wakeup source\n");
468 			ds3232->suspended = false;
469 		}
470 	}
471 
472 	return 0;
473 }
474 
475 static int ds3232_resume(struct device *dev)
476 {
477 	struct ds3232 *ds3232 = dev_get_drvdata(dev);
478 	struct i2c_client *client = to_i2c_client(dev);
479 
480 	if (ds3232->suspended) {
481 		ds3232->suspended = false;
482 
483 		/* Clear the hardware alarm pend flag */
484 		schedule_work(&ds3232->work);
485 
486 		irq_set_irq_wake(client->irq, 0);
487 	}
488 
489 	return 0;
490 }
491 #endif
492 
493 static const struct dev_pm_ops ds3232_pm_ops = {
494 	SET_SYSTEM_SLEEP_PM_OPS(ds3232_suspend, ds3232_resume)
495 };
496 
497 static const struct i2c_device_id ds3232_id[] = {
498 	{ "ds3232", 0 },
499 	{ }
500 };
501 MODULE_DEVICE_TABLE(i2c, ds3232_id);
502 
503 static struct i2c_driver ds3232_driver = {
504 	.driver = {
505 		.name = "rtc-ds3232",
506 		.pm	= &ds3232_pm_ops,
507 	},
508 	.probe = ds3232_probe,
509 	.remove = ds3232_remove,
510 	.id_table = ds3232_id,
511 };
512 
513 module_i2c_driver(ds3232_driver);
514 
515 MODULE_AUTHOR("Srikanth Srinivasan <srikanth.srinivasan@freescale.com>");
516 MODULE_DESCRIPTION("Maxim/Dallas DS3232 RTC Driver");
517 MODULE_LICENSE("GPL");
518