]>
Commit | Line | Data |
---|---|---|
c03675f0 RZ |
1 | /* |
2 | * RTC client/driver for the Maxim/Dallas DS3232 Real-Time Clock over I2C | |
3 | * | |
4 | * Copyright (C) 2009-2010 Freescale Semiconductor. | |
5 | * | |
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. | |
10 | */ | |
11 | /* | |
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. | |
15 | */ | |
16 | ||
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> | |
25 | ||
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 | |
42 | ||
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 | |
48 | ||
49 | struct ds3232 { | |
50 | struct i2c_client *client; | |
51 | struct rtc_device *rtc; | |
52 | struct work_struct work; | |
53 | ||
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. | |
57 | */ | |
58 | struct mutex mutex; | |
59 | int exiting; | |
60 | }; | |
61 | ||
62 | static struct i2c_driver ds3232_driver; | |
63 | ||
64 | static int ds3232_check_rtc_status(struct i2c_client *client) | |
65 | { | |
66 | int ret = 0; | |
67 | int control, stat; | |
68 | ||
69 | stat = i2c_smbus_read_byte_data(client, DS3232_REG_SR); | |
70 | if (stat < 0) | |
71 | return stat; | |
72 | ||
73 | if (stat & DS3232_REG_SR_OSF) | |
74 | dev_warn(&client->dev, | |
75 | "oscillator discontinuity flagged, " | |
76 | "time unreliable\n"); | |
77 | ||
78 | stat &= ~(DS3232_REG_SR_OSF | DS3232_REG_SR_A1F | DS3232_REG_SR_A2F); | |
79 | ||
80 | ret = i2c_smbus_write_byte_data(client, DS3232_REG_SR, stat); | |
81 | if (ret < 0) | |
82 | return ret; | |
83 | ||
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. | |
87 | */ | |
88 | ||
89 | control = i2c_smbus_read_byte_data(client, DS3232_REG_CR); | |
90 | if (control < 0) | |
91 | return control; | |
92 | ||
93 | control &= ~(DS3232_REG_CR_A1IE | DS3232_REG_CR_A2IE); | |
94 | control |= DS3232_REG_CR_INTCN; | |
95 | ||
96 | return i2c_smbus_write_byte_data(client, DS3232_REG_CR, control); | |
97 | } | |
98 | ||
99 | static int ds3232_read_time(struct device *dev, struct rtc_time *time) | |
100 | { | |
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; | |
107 | ||
108 | ret = i2c_smbus_read_i2c_block_data(client, DS3232_REG_SECONDS, 7, buf); | |
109 | ||
110 | if (ret < 0) | |
111 | return ret; | |
112 | if (ret < 7) | |
113 | return -EIO; | |
114 | ||
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]; | |
122 | ||
123 | /* Extract additional information for AM/PM and century */ | |
124 | ||
125 | twelve_hr = hour & 0x40; | |
126 | am_pm = hour & 0x20; | |
127 | century = month & 0x80; | |
128 | ||
129 | /* Write to rtc_time structure */ | |
130 | ||
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); | |
141 | } | |
142 | ||
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; | |
148 | ||
149 | time->tm_year = bcd2bin(year) + add_century; | |
150 | ||
151 | return rtc_valid_tm(time); | |
152 | } | |
153 | ||
154 | static int ds3232_set_time(struct device *dev, struct rtc_time *time) | |
155 | { | |
156 | struct i2c_client *client = to_i2c_client(dev); | |
157 | u8 buf[7]; | |
158 | ||
159 | /* Extract time from rtc_time and load into ds3232*/ | |
160 | ||
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); | |
172 | } | |
173 | ||
174 | return i2c_smbus_write_i2c_block_data(client, | |
175 | DS3232_REG_SECONDS, 7, buf); | |
176 | } | |
177 | ||
178 | static irqreturn_t ds3232_irq(int irq, void *dev_id) | |
179 | { | |
180 | struct i2c_client *client = dev_id; | |
181 | struct ds3232 *ds3232 = i2c_get_clientdata(client); | |
182 | ||
183 | disable_irq_nosync(irq); | |
184 | schedule_work(&ds3232->work); | |
185 | return IRQ_HANDLED; | |
186 | } | |
187 | ||
188 | static void ds3232_work(struct work_struct *work) | |
189 | { | |
190 | struct ds3232 *ds3232 = container_of(work, struct ds3232, work); | |
191 | struct i2c_client *client = ds3232->client; | |
192 | int stat, control; | |
193 | ||
194 | mutex_lock(&ds3232->mutex); | |
195 | ||
196 | stat = i2c_smbus_read_byte_data(client, DS3232_REG_SR); | |
197 | if (stat < 0) | |
198 | goto unlock; | |
199 | ||
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); | |
207 | ||
208 | /* clear the alarm pend flag */ | |
209 | stat &= ~DS3232_REG_SR_A1F; | |
210 | i2c_smbus_write_byte_data(client, DS3232_REG_SR, stat); | |
211 | ||
212 | rtc_update_irq(ds3232->rtc, 1, RTC_AF | RTC_IRQF); | |
213 | } | |
214 | ||
215 | out: | |
216 | if (!ds3232->exiting) | |
217 | enable_irq(client->irq); | |
218 | unlock: | |
219 | mutex_unlock(&ds3232->mutex); | |
220 | } | |
221 | ||
222 | static const struct rtc_class_ops ds3232_rtc_ops = { | |
223 | .read_time = ds3232_read_time, | |
224 | .set_time = ds3232_set_time, | |
225 | }; | |
226 | ||
227 | static int __devinit ds3232_probe(struct i2c_client *client, | |
228 | const struct i2c_device_id *id) | |
229 | { | |
230 | struct ds3232 *ds3232; | |
231 | int ret; | |
232 | ||
233 | ds3232 = kzalloc(sizeof(struct ds3232), GFP_KERNEL); | |
234 | if (!ds3232) | |
235 | return -ENOMEM; | |
236 | ||
237 | ds3232->client = client; | |
238 | i2c_set_clientdata(client, ds3232); | |
239 | ||
240 | INIT_WORK(&ds3232->work, ds3232_work); | |
241 | mutex_init(&ds3232->mutex); | |
242 | ||
243 | ret = ds3232_check_rtc_status(client); | |
244 | if (ret) | |
245 | goto out_free; | |
246 | ||
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; | |
253 | } | |
254 | ||
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; | |
261 | } | |
262 | } | |
263 | ||
264 | return 0; | |
265 | ||
266 | out_irq: | |
267 | if (client->irq >= 0) | |
268 | free_irq(client->irq, client); | |
269 | ||
270 | out_free: | |
271 | i2c_set_clientdata(client, NULL); | |
272 | kfree(ds3232); | |
273 | return ret; | |
274 | } | |
275 | ||
276 | static int __devexit ds3232_remove(struct i2c_client *client) | |
277 | { | |
278 | struct ds3232 *ds3232 = i2c_get_clientdata(client); | |
279 | ||
280 | if (client->irq >= 0) { | |
281 | mutex_lock(&ds3232->mutex); | |
282 | ds3232->exiting = 1; | |
283 | mutex_unlock(&ds3232->mutex); | |
284 | ||
285 | free_irq(client->irq, client); | |
286 | flush_scheduled_work(); | |
287 | } | |
288 | ||
289 | rtc_device_unregister(ds3232->rtc); | |
290 | i2c_set_clientdata(client, NULL); | |
291 | kfree(ds3232); | |
292 | return 0; | |
293 | } | |
294 | ||
295 | static const struct i2c_device_id ds3232_id[] = { | |
296 | { "ds3232", 0 }, | |
297 | { } | |
298 | }; | |
299 | MODULE_DEVICE_TABLE(i2c, ds3232_id); | |
300 | ||
301 | static struct i2c_driver ds3232_driver = { | |
302 | .driver = { | |
303 | .name = "rtc-ds3232", | |
304 | .owner = THIS_MODULE, | |
305 | }, | |
306 | .probe = ds3232_probe, | |
307 | .remove = __devexit_p(ds3232_remove), | |
308 | .id_table = ds3232_id, | |
309 | }; | |
310 | ||
311 | static int __init ds3232_init(void) | |
312 | { | |
313 | return i2c_add_driver(&ds3232_driver); | |
314 | } | |
315 | ||
316 | static void __exit ds3232_exit(void) | |
317 | { | |
318 | i2c_del_driver(&ds3232_driver); | |
319 | } | |
320 | ||
321 | module_init(ds3232_init); | |
322 | module_exit(ds3232_exit); | |
323 | ||
324 | MODULE_AUTHOR("Srikanth Srinivasan <srikanth.srinivasan@freescale.com>"); | |
325 | MODULE_DESCRIPTION("Maxim/Dallas DS3232 RTC Driver"); | |
326 | MODULE_LICENSE("GPL"); |