blob: a5b0fc09f0c673976720b10eca2cc3ecf8ff03bb [file] [log] [blame]
Scott Woodbf4994d2007-10-16 01:28:19 -07001/*
2 * RTC client/driver for the Maxim/Dallas DS1374 Real-Time Clock over I2C
3 *
4 * Based on code by Randy Vinson <rvinson@mvista.com>,
5 * which was based on the m41t00.c by Mark Greer <mgreer@mvista.com>.
6 *
7 * Copyright (C) 2006-2007 Freescale Semiconductor
8 *
9 * 2005 (c) MontaVista Software, Inc. This file is licensed under
10 * the terms of the GNU General Public License version 2. This program
11 * is licensed "as is" without any warranty of any kind, whether express
12 * or implied.
13 */
14/*
15 * It would be more efficient to use i2c msgs/i2c_transfer directly but, as
16 * recommened in .../Documentation/i2c/writing-clients section
17 * "Sending and receiving", using SMBus level communication is preferred.
18 */
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
28#define DS1374_REG_TOD0 0x00 /* Time of Day */
29#define DS1374_REG_TOD1 0x01
30#define DS1374_REG_TOD2 0x02
31#define DS1374_REG_TOD3 0x03
32#define DS1374_REG_WDALM0 0x04 /* Watchdog/Alarm */
33#define DS1374_REG_WDALM1 0x05
34#define DS1374_REG_WDALM2 0x06
35#define DS1374_REG_CR 0x07 /* Control */
36#define DS1374_REG_CR_AIE 0x01 /* Alarm Int. Enable */
37#define DS1374_REG_CR_WDALM 0x20 /* 1=Watchdog, 0=Alarm */
38#define DS1374_REG_CR_WACE 0x40 /* WD/Alarm counter enable */
39#define DS1374_REG_SR 0x08 /* Status */
40#define DS1374_REG_SR_OSF 0x80 /* Oscillator Stop Flag */
41#define DS1374_REG_SR_AF 0x01 /* Alarm Flag */
42#define DS1374_REG_TCR 0x09 /* Trickle Charge */
43
Jean Delvare3760f732008-04-29 23:11:40 +020044static const struct i2c_device_id ds1374_id[] = {
Jean Delvaref2eb4322008-06-05 22:46:18 -070045 { "ds1374", 0 },
Jean Delvare3760f732008-04-29 23:11:40 +020046 { }
47};
48MODULE_DEVICE_TABLE(i2c, ds1374_id);
49
Scott Woodbf4994d2007-10-16 01:28:19 -070050struct ds1374 {
51 struct i2c_client *client;
52 struct rtc_device *rtc;
53 struct work_struct work;
54
55 /* The mutex protects alarm operations, and prevents a race
56 * between the enable_irq() in the workqueue and the free_irq()
57 * in the remove function.
58 */
59 struct mutex mutex;
60 int exiting;
61};
62
63static struct i2c_driver ds1374_driver;
64
65static int ds1374_read_rtc(struct i2c_client *client, u32 *time,
66 int reg, int nbytes)
67{
68 u8 buf[4];
69 int ret;
70 int i;
71
72 if (nbytes > 4) {
73 WARN_ON(1);
74 return -EINVAL;
75 }
76
77 ret = i2c_smbus_read_i2c_block_data(client, reg, nbytes, buf);
78
79 if (ret < 0)
80 return ret;
81 if (ret < nbytes)
82 return -EIO;
83
84 for (i = nbytes - 1, *time = 0; i >= 0; i--)
85 *time = (*time << 8) | buf[i];
86
87 return 0;
88}
89
90static int ds1374_write_rtc(struct i2c_client *client, u32 time,
91 int reg, int nbytes)
92{
93 u8 buf[4];
94 int i;
95
96 if (nbytes > 4) {
97 WARN_ON(1);
98 return -EINVAL;
99 }
100
101 for (i = 0; i < nbytes; i++) {
102 buf[i] = time & 0xff;
103 time >>= 8;
104 }
105
106 return i2c_smbus_write_i2c_block_data(client, reg, nbytes, buf);
107}
108
109static int ds1374_check_rtc_status(struct i2c_client *client)
110{
111 int ret = 0;
112 int control, stat;
113
114 stat = i2c_smbus_read_byte_data(client, DS1374_REG_SR);
115 if (stat < 0)
116 return stat;
117
118 if (stat & DS1374_REG_SR_OSF)
119 dev_warn(&client->dev,
120 "oscillator discontinuity flagged, "
121 "time unreliable\n");
122
123 stat &= ~(DS1374_REG_SR_OSF | DS1374_REG_SR_AF);
124
125 ret = i2c_smbus_write_byte_data(client, DS1374_REG_SR, stat);
126 if (ret < 0)
127 return ret;
128
129 /* If the alarm is pending, clear it before requesting
130 * the interrupt, so an interrupt event isn't reported
131 * before everything is initialized.
132 */
133
134 control = i2c_smbus_read_byte_data(client, DS1374_REG_CR);
135 if (control < 0)
136 return control;
137
138 control &= ~(DS1374_REG_CR_WACE | DS1374_REG_CR_AIE);
139 return i2c_smbus_write_byte_data(client, DS1374_REG_CR, control);
140}
141
142static int ds1374_read_time(struct device *dev, struct rtc_time *time)
143{
144 struct i2c_client *client = to_i2c_client(dev);
145 u32 itime;
146 int ret;
147
148 ret = ds1374_read_rtc(client, &itime, DS1374_REG_TOD0, 4);
149 if (!ret)
150 rtc_time_to_tm(itime, time);
151
152 return ret;
153}
154
155static int ds1374_set_time(struct device *dev, struct rtc_time *time)
156{
157 struct i2c_client *client = to_i2c_client(dev);
158 unsigned long itime;
159
160 rtc_tm_to_time(time, &itime);
161 return ds1374_write_rtc(client, itime, DS1374_REG_TOD0, 4);
162}
163
164/* The ds1374 has a decrementer for an alarm, rather than a comparator.
165 * If the time of day is changed, then the alarm will need to be
166 * reset.
167 */
168static int ds1374_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
169{
170 struct i2c_client *client = to_i2c_client(dev);
171 struct ds1374 *ds1374 = i2c_get_clientdata(client);
172 u32 now, cur_alarm;
173 int cr, sr;
174 int ret = 0;
175
Anton Vorontsovb42f9312008-08-20 14:09:21 -0700176 if (client->irq <= 0)
Scott Woodbf4994d2007-10-16 01:28:19 -0700177 return -EINVAL;
178
179 mutex_lock(&ds1374->mutex);
180
181 cr = ret = i2c_smbus_read_byte_data(client, DS1374_REG_CR);
182 if (ret < 0)
183 goto out;
184
185 sr = ret = i2c_smbus_read_byte_data(client, DS1374_REG_SR);
186 if (ret < 0)
187 goto out;
188
189 ret = ds1374_read_rtc(client, &now, DS1374_REG_TOD0, 4);
190 if (ret)
191 goto out;
192
193 ret = ds1374_read_rtc(client, &cur_alarm, DS1374_REG_WDALM0, 3);
194 if (ret)
195 goto out;
196
197 rtc_time_to_tm(now + cur_alarm, &alarm->time);
198 alarm->enabled = !!(cr & DS1374_REG_CR_WACE);
199 alarm->pending = !!(sr & DS1374_REG_SR_AF);
200
201out:
202 mutex_unlock(&ds1374->mutex);
203 return ret;
204}
205
206static int ds1374_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
207{
208 struct i2c_client *client = to_i2c_client(dev);
209 struct ds1374 *ds1374 = i2c_get_clientdata(client);
210 struct rtc_time now;
211 unsigned long new_alarm, itime;
212 int cr;
213 int ret = 0;
214
Anton Vorontsovb42f9312008-08-20 14:09:21 -0700215 if (client->irq <= 0)
Scott Woodbf4994d2007-10-16 01:28:19 -0700216 return -EINVAL;
217
218 ret = ds1374_read_time(dev, &now);
219 if (ret < 0)
220 return ret;
221
222 rtc_tm_to_time(&alarm->time, &new_alarm);
223 rtc_tm_to_time(&now, &itime);
224
225 new_alarm -= itime;
226
227 /* This can happen due to races, in addition to dates that are
228 * truly in the past. To avoid requiring the caller to check for
229 * races, dates in the past are assumed to be in the recent past
230 * (i.e. not something that we'd rather the caller know about via
231 * an error), and the alarm is set to go off as soon as possible.
232 */
233 if (new_alarm <= 0)
234 new_alarm = 1;
235
236 mutex_lock(&ds1374->mutex);
237
238 ret = cr = i2c_smbus_read_byte_data(client, DS1374_REG_CR);
239 if (ret < 0)
240 goto out;
241
242 /* Disable any existing alarm before setting the new one
243 * (or lack thereof). */
244 cr &= ~DS1374_REG_CR_WACE;
245
246 ret = i2c_smbus_write_byte_data(client, DS1374_REG_CR, cr);
247 if (ret < 0)
248 goto out;
249
250 ret = ds1374_write_rtc(client, new_alarm, DS1374_REG_WDALM0, 3);
251 if (ret)
252 goto out;
253
254 if (alarm->enabled) {
255 cr |= DS1374_REG_CR_WACE | DS1374_REG_CR_AIE;
256 cr &= ~DS1374_REG_CR_WDALM;
257
258 ret = i2c_smbus_write_byte_data(client, DS1374_REG_CR, cr);
259 }
260
261out:
262 mutex_unlock(&ds1374->mutex);
263 return ret;
264}
265
266static irqreturn_t ds1374_irq(int irq, void *dev_id)
267{
268 struct i2c_client *client = dev_id;
269 struct ds1374 *ds1374 = i2c_get_clientdata(client);
270
271 disable_irq_nosync(irq);
272 schedule_work(&ds1374->work);
273 return IRQ_HANDLED;
274}
275
276static void ds1374_work(struct work_struct *work)
277{
278 struct ds1374 *ds1374 = container_of(work, struct ds1374, work);
279 struct i2c_client *client = ds1374->client;
280 int stat, control;
281
282 mutex_lock(&ds1374->mutex);
283
284 stat = i2c_smbus_read_byte_data(client, DS1374_REG_SR);
285 if (stat < 0)
286 return;
287
288 if (stat & DS1374_REG_SR_AF) {
289 stat &= ~DS1374_REG_SR_AF;
290 i2c_smbus_write_byte_data(client, DS1374_REG_SR, stat);
291
292 control = i2c_smbus_read_byte_data(client, DS1374_REG_CR);
293 if (control < 0)
294 goto out;
295
296 control &= ~(DS1374_REG_CR_WACE | DS1374_REG_CR_AIE);
297 i2c_smbus_write_byte_data(client, DS1374_REG_CR, control);
298
299 /* rtc_update_irq() assumes that it is called
300 * from IRQ-disabled context.
301 */
302 local_irq_disable();
303 rtc_update_irq(ds1374->rtc, 1, RTC_AF | RTC_IRQF);
304 local_irq_enable();
305 }
306
307out:
308 if (!ds1374->exiting)
309 enable_irq(client->irq);
310
311 mutex_unlock(&ds1374->mutex);
312}
313
314static int ds1374_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
315{
316 struct i2c_client *client = to_i2c_client(dev);
317 struct ds1374 *ds1374 = i2c_get_clientdata(client);
318 int ret = -ENOIOCTLCMD;
319
320 mutex_lock(&ds1374->mutex);
321
322 switch (cmd) {
323 case RTC_AIE_OFF:
324 ret = i2c_smbus_read_byte_data(client, DS1374_REG_CR);
325 if (ret < 0)
326 goto out;
327
328 ret &= ~DS1374_REG_CR_WACE;
329
330 ret = i2c_smbus_write_byte_data(client, DS1374_REG_CR, ret);
331 if (ret < 0)
332 goto out;
333
334 break;
335
336 case RTC_AIE_ON:
337 ret = i2c_smbus_read_byte_data(client, DS1374_REG_CR);
338 if (ret < 0)
339 goto out;
340
341 ret |= DS1374_REG_CR_WACE | DS1374_REG_CR_AIE;
342 ret &= ~DS1374_REG_CR_WDALM;
343
344 ret = i2c_smbus_write_byte_data(client, DS1374_REG_CR, ret);
345 if (ret < 0)
346 goto out;
347
348 break;
349 }
350
351out:
352 mutex_unlock(&ds1374->mutex);
353 return ret;
354}
355
356static const struct rtc_class_ops ds1374_rtc_ops = {
357 .read_time = ds1374_read_time,
358 .set_time = ds1374_set_time,
359 .read_alarm = ds1374_read_alarm,
360 .set_alarm = ds1374_set_alarm,
361 .ioctl = ds1374_ioctl,
362};
363
Jean Delvared2653e92008-04-29 23:11:39 +0200364static int ds1374_probe(struct i2c_client *client,
365 const struct i2c_device_id *id)
Scott Woodbf4994d2007-10-16 01:28:19 -0700366{
367 struct ds1374 *ds1374;
368 int ret;
369
370 ds1374 = kzalloc(sizeof(struct ds1374), GFP_KERNEL);
371 if (!ds1374)
372 return -ENOMEM;
373
374 ds1374->client = client;
375 i2c_set_clientdata(client, ds1374);
376
377 INIT_WORK(&ds1374->work, ds1374_work);
378 mutex_init(&ds1374->mutex);
379
380 ret = ds1374_check_rtc_status(client);
381 if (ret)
382 goto out_free;
383
Anton Vorontsovb42f9312008-08-20 14:09:21 -0700384 if (client->irq > 0) {
Scott Woodbf4994d2007-10-16 01:28:19 -0700385 ret = request_irq(client->irq, ds1374_irq, 0,
386 "ds1374", client);
387 if (ret) {
388 dev_err(&client->dev, "unable to request IRQ\n");
389 goto out_free;
390 }
391 }
392
393 ds1374->rtc = rtc_device_register(client->name, &client->dev,
394 &ds1374_rtc_ops, THIS_MODULE);
395 if (IS_ERR(ds1374->rtc)) {
396 ret = PTR_ERR(ds1374->rtc);
397 dev_err(&client->dev, "unable to register the class device\n");
398 goto out_irq;
399 }
400
401 return 0;
402
403out_irq:
Anton Vorontsovb42f9312008-08-20 14:09:21 -0700404 if (client->irq > 0)
Scott Woodbf4994d2007-10-16 01:28:19 -0700405 free_irq(client->irq, client);
406
407out_free:
408 i2c_set_clientdata(client, NULL);
409 kfree(ds1374);
410 return ret;
411}
412
413static int __devexit ds1374_remove(struct i2c_client *client)
414{
415 struct ds1374 *ds1374 = i2c_get_clientdata(client);
416
Anton Vorontsovb42f9312008-08-20 14:09:21 -0700417 if (client->irq > 0) {
Scott Woodbf4994d2007-10-16 01:28:19 -0700418 mutex_lock(&ds1374->mutex);
419 ds1374->exiting = 1;
420 mutex_unlock(&ds1374->mutex);
421
422 free_irq(client->irq, client);
423 flush_scheduled_work();
424 }
425
426 rtc_device_unregister(ds1374->rtc);
427 i2c_set_clientdata(client, NULL);
428 kfree(ds1374);
429 return 0;
430}
431
Marc Pignat986e36a2008-10-15 22:02:56 -0700432#ifdef CONFIG_PM
433static int ds1374_suspend(struct i2c_client *client, pm_message_t state)
434{
435 if (client->irq >= 0 && device_may_wakeup(&client->dev))
436 enable_irq_wake(client->irq);
437 return 0;
438}
439
440static int ds1374_resume(struct i2c_client *client)
441{
442 if (client->irq >= 0 && device_may_wakeup(&client->dev))
443 disable_irq_wake(client->irq);
444 return 0;
445}
446#else
447#define ds1374_suspend NULL
448#define ds1374_resume NULL
449#endif
450
Scott Woodbf4994d2007-10-16 01:28:19 -0700451static struct i2c_driver ds1374_driver = {
452 .driver = {
453 .name = "rtc-ds1374",
454 .owner = THIS_MODULE,
455 },
456 .probe = ds1374_probe,
Marc Pignat986e36a2008-10-15 22:02:56 -0700457 .suspend = ds1374_suspend,
458 .resume = ds1374_resume,
Scott Woodbf4994d2007-10-16 01:28:19 -0700459 .remove = __devexit_p(ds1374_remove),
Jean Delvare3760f732008-04-29 23:11:40 +0200460 .id_table = ds1374_id,
Scott Woodbf4994d2007-10-16 01:28:19 -0700461};
462
463static int __init ds1374_init(void)
464{
465 return i2c_add_driver(&ds1374_driver);
466}
467
468static void __exit ds1374_exit(void)
469{
470 i2c_del_driver(&ds1374_driver);
471}
472
473module_init(ds1374_init);
474module_exit(ds1374_exit);
475
476MODULE_AUTHOR("Scott Wood <scottwood@freescale.com>");
477MODULE_DESCRIPTION("Maxim/Dallas DS1374 RTC Driver");
478MODULE_LICENSE("GPL");