[media] cx231xx: add support for Hauppauge EXETER
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / rtc / rtc-ds3232.c
blob9de8516e3531e70bad818747f41de4b8052486bd
1 /*
2 * RTC client/driver for the Maxim/Dallas DS3232 Real-Time Clock over I2C
4 * Copyright (C) 2009-2010 Freescale Semiconductor.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version.
12 * It would be more efficient to use i2c msgs/i2c_transfer directly but, as
13 * recommened in .../Documentation/i2c/writing-clients section
14 * "Sending and receiving", using SMBus level communication is preferred.
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/interrupt.h>
20 #include <linux/i2c.h>
21 #include <linux/rtc.h>
22 #include <linux/bcd.h>
23 #include <linux/workqueue.h>
24 #include <linux/slab.h>
26 #define DS3232_REG_SECONDS 0x00
27 #define DS3232_REG_MINUTES 0x01
28 #define DS3232_REG_HOURS 0x02
29 #define DS3232_REG_AMPM 0x02
30 #define DS3232_REG_DAY 0x03
31 #define DS3232_REG_DATE 0x04
32 #define DS3232_REG_MONTH 0x05
33 #define DS3232_REG_CENTURY 0x05
34 #define DS3232_REG_YEAR 0x06
35 #define DS3232_REG_ALARM1 0x07 /* Alarm 1 BASE */
36 #define DS3232_REG_ALARM2 0x0B /* Alarm 2 BASE */
37 #define DS3232_REG_CR 0x0E /* Control register */
38 # define DS3232_REG_CR_nEOSC 0x80
39 # define DS3232_REG_CR_INTCN 0x04
40 # define DS3232_REG_CR_A2IE 0x02
41 # define DS3232_REG_CR_A1IE 0x01
43 #define DS3232_REG_SR 0x0F /* control/status register */
44 # define DS3232_REG_SR_OSF 0x80
45 # define DS3232_REG_SR_BSY 0x04
46 # define DS3232_REG_SR_A2F 0x02
47 # define DS3232_REG_SR_A1F 0x01
49 struct ds3232 {
50 struct i2c_client *client;
51 struct rtc_device *rtc;
52 struct work_struct work;
54 /* The mutex protects alarm operations, and prevents a race
55 * between the enable_irq() in the workqueue and the free_irq()
56 * in the remove function.
58 struct mutex mutex;
59 int exiting;
62 static struct i2c_driver ds3232_driver;
64 static int ds3232_check_rtc_status(struct i2c_client *client)
66 int ret = 0;
67 int control, stat;
69 stat = i2c_smbus_read_byte_data(client, DS3232_REG_SR);
70 if (stat < 0)
71 return stat;
73 if (stat & DS3232_REG_SR_OSF)
74 dev_warn(&client->dev,
75 "oscillator discontinuity flagged, "
76 "time unreliable\n");
78 stat &= ~(DS3232_REG_SR_OSF | DS3232_REG_SR_A1F | DS3232_REG_SR_A2F);
80 ret = i2c_smbus_write_byte_data(client, DS3232_REG_SR, stat);
81 if (ret < 0)
82 return ret;
84 /* If the alarm is pending, clear it before requesting
85 * the interrupt, so an interrupt event isn't reported
86 * before everything is initialized.
89 control = i2c_smbus_read_byte_data(client, DS3232_REG_CR);
90 if (control < 0)
91 return control;
93 control &= ~(DS3232_REG_CR_A1IE | DS3232_REG_CR_A2IE);
94 control |= DS3232_REG_CR_INTCN;
96 return i2c_smbus_write_byte_data(client, DS3232_REG_CR, control);
99 static int ds3232_read_time(struct device *dev, struct rtc_time *time)
101 struct i2c_client *client = to_i2c_client(dev);
102 int ret;
103 u8 buf[7];
104 unsigned int year, month, day, hour, minute, second;
105 unsigned int week, twelve_hr, am_pm;
106 unsigned int century, add_century = 0;
108 ret = i2c_smbus_read_i2c_block_data(client, DS3232_REG_SECONDS, 7, buf);
110 if (ret < 0)
111 return ret;
112 if (ret < 7)
113 return -EIO;
115 second = buf[0];
116 minute = buf[1];
117 hour = buf[2];
118 week = buf[3];
119 day = buf[4];
120 month = buf[5];
121 year = buf[6];
123 /* Extract additional information for AM/PM and century */
125 twelve_hr = hour & 0x40;
126 am_pm = hour & 0x20;
127 century = month & 0x80;
129 /* Write to rtc_time structure */
131 time->tm_sec = bcd2bin(second);
132 time->tm_min = bcd2bin(minute);
133 if (twelve_hr) {
134 /* Convert to 24 hr */
135 if (am_pm)
136 time->tm_hour = bcd2bin(hour & 0x1F) + 12;
137 else
138 time->tm_hour = bcd2bin(hour & 0x1F);
139 } else {
140 time->tm_hour = bcd2bin(hour);
143 time->tm_wday = bcd2bin(week);
144 time->tm_mday = bcd2bin(day);
145 time->tm_mon = bcd2bin(month & 0x7F);
146 if (century)
147 add_century = 100;
149 time->tm_year = bcd2bin(year) + add_century;
151 return rtc_valid_tm(time);
154 static int ds3232_set_time(struct device *dev, struct rtc_time *time)
156 struct i2c_client *client = to_i2c_client(dev);
157 u8 buf[7];
159 /* Extract time from rtc_time and load into ds3232*/
161 buf[0] = bin2bcd(time->tm_sec);
162 buf[1] = bin2bcd(time->tm_min);
163 buf[2] = bin2bcd(time->tm_hour);
164 buf[3] = bin2bcd(time->tm_wday); /* Day of the week */
165 buf[4] = bin2bcd(time->tm_mday); /* Date */
166 buf[5] = bin2bcd(time->tm_mon);
167 if (time->tm_year >= 100) {
168 buf[5] |= 0x80;
169 buf[6] = bin2bcd(time->tm_year - 100);
170 } else {
171 buf[6] = bin2bcd(time->tm_year);
174 return i2c_smbus_write_i2c_block_data(client,
175 DS3232_REG_SECONDS, 7, buf);
178 static irqreturn_t ds3232_irq(int irq, void *dev_id)
180 struct i2c_client *client = dev_id;
181 struct ds3232 *ds3232 = i2c_get_clientdata(client);
183 disable_irq_nosync(irq);
184 schedule_work(&ds3232->work);
185 return IRQ_HANDLED;
188 static void ds3232_work(struct work_struct *work)
190 struct ds3232 *ds3232 = container_of(work, struct ds3232, work);
191 struct i2c_client *client = ds3232->client;
192 int stat, control;
194 mutex_lock(&ds3232->mutex);
196 stat = i2c_smbus_read_byte_data(client, DS3232_REG_SR);
197 if (stat < 0)
198 goto unlock;
200 if (stat & DS3232_REG_SR_A1F) {
201 control = i2c_smbus_read_byte_data(client, DS3232_REG_CR);
202 if (control < 0)
203 goto out;
204 /* disable alarm1 interrupt */
205 control &= ~(DS3232_REG_CR_A1IE);
206 i2c_smbus_write_byte_data(client, DS3232_REG_CR, control);
208 /* clear the alarm pend flag */
209 stat &= ~DS3232_REG_SR_A1F;
210 i2c_smbus_write_byte_data(client, DS3232_REG_SR, stat);
212 rtc_update_irq(ds3232->rtc, 1, RTC_AF | RTC_IRQF);
215 out:
216 if (!ds3232->exiting)
217 enable_irq(client->irq);
218 unlock:
219 mutex_unlock(&ds3232->mutex);
222 static const struct rtc_class_ops ds3232_rtc_ops = {
223 .read_time = ds3232_read_time,
224 .set_time = ds3232_set_time,
227 static int __devinit ds3232_probe(struct i2c_client *client,
228 const struct i2c_device_id *id)
230 struct ds3232 *ds3232;
231 int ret;
233 ds3232 = kzalloc(sizeof(struct ds3232), GFP_KERNEL);
234 if (!ds3232)
235 return -ENOMEM;
237 ds3232->client = client;
238 i2c_set_clientdata(client, ds3232);
240 INIT_WORK(&ds3232->work, ds3232_work);
241 mutex_init(&ds3232->mutex);
243 ret = ds3232_check_rtc_status(client);
244 if (ret)
245 goto out_free;
247 ds3232->rtc = rtc_device_register(client->name, &client->dev,
248 &ds3232_rtc_ops, THIS_MODULE);
249 if (IS_ERR(ds3232->rtc)) {
250 ret = PTR_ERR(ds3232->rtc);
251 dev_err(&client->dev, "unable to register the class device\n");
252 goto out_irq;
255 if (client->irq >= 0) {
256 ret = request_irq(client->irq, ds3232_irq, 0,
257 "ds3232", client);
258 if (ret) {
259 dev_err(&client->dev, "unable to request IRQ\n");
260 goto out_free;
264 return 0;
266 out_irq:
267 if (client->irq >= 0)
268 free_irq(client->irq, client);
270 out_free:
271 kfree(ds3232);
272 return ret;
275 static int __devexit ds3232_remove(struct i2c_client *client)
277 struct ds3232 *ds3232 = i2c_get_clientdata(client);
279 if (client->irq >= 0) {
280 mutex_lock(&ds3232->mutex);
281 ds3232->exiting = 1;
282 mutex_unlock(&ds3232->mutex);
284 free_irq(client->irq, client);
285 flush_scheduled_work();
288 rtc_device_unregister(ds3232->rtc);
289 kfree(ds3232);
290 return 0;
293 static const struct i2c_device_id ds3232_id[] = {
294 { "ds3232", 0 },
297 MODULE_DEVICE_TABLE(i2c, ds3232_id);
299 static struct i2c_driver ds3232_driver = {
300 .driver = {
301 .name = "rtc-ds3232",
302 .owner = THIS_MODULE,
304 .probe = ds3232_probe,
305 .remove = __devexit_p(ds3232_remove),
306 .id_table = ds3232_id,
309 static int __init ds3232_init(void)
311 return i2c_add_driver(&ds3232_driver);
314 static void __exit ds3232_exit(void)
316 i2c_del_driver(&ds3232_driver);
319 module_init(ds3232_init);
320 module_exit(ds3232_exit);
322 MODULE_AUTHOR("Srikanth Srinivasan <srikanth.srinivasan@freescale.com>");
323 MODULE_DESCRIPTION("Maxim/Dallas DS3232 RTC Driver");
324 MODULE_LICENSE("GPL");