kernel: rename is_single_threaded(task) to current_is_single_threaded(void)
[linux-2.6/mini2440.git] / drivers / rtc / rtc-rx8025.c
blobb1a29bcfdf1339b06ec6b4ff190dcaa2cbf04d21
1 /*
2 * Driver for Epson's RTC module RX-8025 SA/NB
4 * Copyright (C) 2009 Wolfgang Grandegger <wg@grandegger.com>
6 * Copyright (C) 2005 by Digi International Inc.
7 * All rights reserved.
9 * Modified by fengjh at rising.com.cn
10 * <http://lists.lm-sensors.org/mailman/listinfo/lm-sensors>
11 * 2006.11
13 * Code cleanup by Sergei Poselenov, <sposelenov@emcraft.com>
14 * Converted to new style by Wolfgang Grandegger <wg@grandegger.com>
15 * Alarm and periodic interrupt added by Dmitry Rakhchev <rda@emcraft.com>
17 * This program is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU General Public License
19 * version 2 as published by the Free Software Foundation.
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/init.h>
24 #include <linux/bcd.h>
25 #include <linux/i2c.h>
26 #include <linux/list.h>
27 #include <linux/rtc.h>
29 /* Register definitions */
30 #define RX8025_REG_SEC 0x00
31 #define RX8025_REG_MIN 0x01
32 #define RX8025_REG_HOUR 0x02
33 #define RX8025_REG_WDAY 0x03
34 #define RX8025_REG_MDAY 0x04
35 #define RX8025_REG_MONTH 0x05
36 #define RX8025_REG_YEAR 0x06
37 #define RX8025_REG_DIGOFF 0x07
38 #define RX8025_REG_ALWMIN 0x08
39 #define RX8025_REG_ALWHOUR 0x09
40 #define RX8025_REG_ALWWDAY 0x0a
41 #define RX8025_REG_ALDMIN 0x0b
42 #define RX8025_REG_ALDHOUR 0x0c
43 /* 0x0d is reserved */
44 #define RX8025_REG_CTRL1 0x0e
45 #define RX8025_REG_CTRL2 0x0f
47 #define RX8025_BIT_CTRL1_CT (7 << 0)
48 /* 1 Hz periodic level irq */
49 #define RX8025_BIT_CTRL1_CT_1HZ 4
50 #define RX8025_BIT_CTRL1_TEST (1 << 3)
51 #define RX8025_BIT_CTRL1_1224 (1 << 5)
52 #define RX8025_BIT_CTRL1_DALE (1 << 6)
53 #define RX8025_BIT_CTRL1_WALE (1 << 7)
55 #define RX8025_BIT_CTRL2_DAFG (1 << 0)
56 #define RX8025_BIT_CTRL2_WAFG (1 << 1)
57 #define RX8025_BIT_CTRL2_CTFG (1 << 2)
58 #define RX8025_BIT_CTRL2_PON (1 << 4)
59 #define RX8025_BIT_CTRL2_XST (1 << 5)
60 #define RX8025_BIT_CTRL2_VDET (1 << 6)
62 /* Clock precision adjustment */
63 #define RX8025_ADJ_RESOLUTION 3050 /* in ppb */
64 #define RX8025_ADJ_DATA_MAX 62
65 #define RX8025_ADJ_DATA_MIN -62
67 static const struct i2c_device_id rx8025_id[] = {
68 { "rx8025", 0 },
69 { }
71 MODULE_DEVICE_TABLE(i2c, rx8025_id);
73 struct rx8025_data {
74 struct i2c_client *client;
75 struct rtc_device *rtc;
76 struct work_struct work;
77 u8 ctrl1;
78 unsigned exiting:1;
81 static int rx8025_read_reg(struct i2c_client *client, int number, u8 *value)
83 int ret = i2c_smbus_read_byte_data(client, (number << 4) | 0x08);
85 if (ret < 0) {
86 dev_err(&client->dev, "Unable to read register #%d\n", number);
87 return ret;
90 *value = ret;
91 return 0;
94 static int rx8025_read_regs(struct i2c_client *client,
95 int number, u8 length, u8 *values)
97 int ret = i2c_smbus_read_i2c_block_data(client, (number << 4) | 0x08,
98 length, values);
100 if (ret != length) {
101 dev_err(&client->dev, "Unable to read registers #%d..#%d\n",
102 number, number + length - 1);
103 return ret < 0 ? ret : -EIO;
106 return 0;
109 static int rx8025_write_reg(struct i2c_client *client, int number, u8 value)
111 int ret = i2c_smbus_write_byte_data(client, number << 4, value);
113 if (ret)
114 dev_err(&client->dev, "Unable to write register #%d\n",
115 number);
117 return ret;
120 static int rx8025_write_regs(struct i2c_client *client,
121 int number, u8 length, u8 *values)
123 int ret = i2c_smbus_write_i2c_block_data(client, (number << 4) | 0x08,
124 length, values);
126 if (ret)
127 dev_err(&client->dev, "Unable to write registers #%d..#%d\n",
128 number, number + length - 1);
130 return ret;
133 static irqreturn_t rx8025_irq(int irq, void *dev_id)
135 struct i2c_client *client = dev_id;
136 struct rx8025_data *rx8025 = i2c_get_clientdata(client);
138 disable_irq_nosync(irq);
139 schedule_work(&rx8025->work);
140 return IRQ_HANDLED;
143 static void rx8025_work(struct work_struct *work)
145 struct rx8025_data *rx8025 = container_of(work, struct rx8025_data,
146 work);
147 struct i2c_client *client = rx8025->client;
148 struct mutex *lock = &rx8025->rtc->ops_lock;
149 u8 status;
151 mutex_lock(lock);
153 if (rx8025_read_reg(client, RX8025_REG_CTRL2, &status))
154 goto out;
156 if (!(status & RX8025_BIT_CTRL2_XST))
157 dev_warn(&client->dev, "Oscillation stop was detected,"
158 "you may have to readjust the clock\n");
160 if (status & RX8025_BIT_CTRL2_CTFG) {
161 /* periodic */
162 status &= ~RX8025_BIT_CTRL2_CTFG;
163 local_irq_disable();
164 rtc_update_irq(rx8025->rtc, 1, RTC_PF | RTC_IRQF);
165 local_irq_enable();
168 if (status & RX8025_BIT_CTRL2_DAFG) {
169 /* alarm */
170 status &= RX8025_BIT_CTRL2_DAFG;
171 if (rx8025_write_reg(client, RX8025_REG_CTRL1,
172 rx8025->ctrl1 & ~RX8025_BIT_CTRL1_DALE))
173 goto out;
174 local_irq_disable();
175 rtc_update_irq(rx8025->rtc, 1, RTC_AF | RTC_IRQF);
176 local_irq_enable();
179 /* acknowledge IRQ */
180 rx8025_write_reg(client, RX8025_REG_CTRL2,
181 status | RX8025_BIT_CTRL2_XST);
183 out:
184 if (!rx8025->exiting)
185 enable_irq(client->irq);
187 mutex_unlock(lock);
190 static int rx8025_get_time(struct device *dev, struct rtc_time *dt)
192 struct rx8025_data *rx8025 = dev_get_drvdata(dev);
193 u8 date[7];
194 int err;
196 err = rx8025_read_regs(rx8025->client, RX8025_REG_SEC, 7, date);
197 if (err)
198 return err;
200 dev_dbg(dev, "%s: read 0x%02x 0x%02x "
201 "0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n", __func__,
202 date[0], date[1], date[2], date[3], date[4],
203 date[5], date[6]);
205 dt->tm_sec = bcd2bin(date[RX8025_REG_SEC] & 0x7f);
206 dt->tm_min = bcd2bin(date[RX8025_REG_MIN] & 0x7f);
207 if (rx8025->ctrl1 & RX8025_BIT_CTRL1_1224)
208 dt->tm_hour = bcd2bin(date[RX8025_REG_HOUR] & 0x3f);
209 else
210 dt->tm_hour = bcd2bin(date[RX8025_REG_HOUR] & 0x1f) % 12
211 + (date[RX8025_REG_HOUR] & 0x20 ? 12 : 0);
213 dt->tm_mday = bcd2bin(date[RX8025_REG_MDAY] & 0x3f);
214 dt->tm_mon = bcd2bin(date[RX8025_REG_MONTH] & 0x1f) - 1;
215 dt->tm_year = bcd2bin(date[RX8025_REG_YEAR]);
217 if (dt->tm_year < 70)
218 dt->tm_year += 100;
220 dev_dbg(dev, "%s: date %ds %dm %dh %dmd %dm %dy\n", __func__,
221 dt->tm_sec, dt->tm_min, dt->tm_hour,
222 dt->tm_mday, dt->tm_mon, dt->tm_year);
224 return rtc_valid_tm(dt);
227 static int rx8025_set_time(struct device *dev, struct rtc_time *dt)
229 struct rx8025_data *rx8025 = dev_get_drvdata(dev);
230 u8 date[7];
233 * BUG: The HW assumes every year that is a multiple of 4 to be a leap
234 * year. Next time this is wrong is 2100, which will not be a leap
235 * year.
239 * Here the read-only bits are written as "0". I'm not sure if that
240 * is sound.
242 date[RX8025_REG_SEC] = bin2bcd(dt->tm_sec);
243 date[RX8025_REG_MIN] = bin2bcd(dt->tm_min);
244 if (rx8025->ctrl1 & RX8025_BIT_CTRL1_1224)
245 date[RX8025_REG_HOUR] = bin2bcd(dt->tm_hour);
246 else
247 date[RX8025_REG_HOUR] = (dt->tm_hour >= 12 ? 0x20 : 0)
248 | bin2bcd((dt->tm_hour + 11) % 12 + 1);
250 date[RX8025_REG_WDAY] = bin2bcd(dt->tm_wday);
251 date[RX8025_REG_MDAY] = bin2bcd(dt->tm_mday);
252 date[RX8025_REG_MONTH] = bin2bcd(dt->tm_mon + 1);
253 date[RX8025_REG_YEAR] = bin2bcd(dt->tm_year % 100);
255 dev_dbg(dev,
256 "%s: write 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
257 __func__,
258 date[0], date[1], date[2], date[3], date[4], date[5], date[6]);
260 return rx8025_write_regs(rx8025->client, RX8025_REG_SEC, 7, date);
263 static int rx8025_init_client(struct i2c_client *client, int *need_reset)
265 struct rx8025_data *rx8025 = i2c_get_clientdata(client);
266 u8 ctrl[2], ctrl2;
267 int need_clear = 0;
268 int err;
270 err = rx8025_read_regs(rx8025->client, RX8025_REG_CTRL1, 2, ctrl);
271 if (err)
272 goto out;
274 /* Keep test bit zero ! */
275 rx8025->ctrl1 = ctrl[0] & ~RX8025_BIT_CTRL1_TEST;
277 if (ctrl[1] & RX8025_BIT_CTRL2_PON) {
278 dev_warn(&client->dev, "power-on reset was detected, "
279 "you may have to readjust the clock\n");
280 *need_reset = 1;
283 if (ctrl[1] & RX8025_BIT_CTRL2_VDET) {
284 dev_warn(&client->dev, "a power voltage drop was detected, "
285 "you may have to readjust the clock\n");
286 *need_reset = 1;
289 if (!(ctrl[1] & RX8025_BIT_CTRL2_XST)) {
290 dev_warn(&client->dev, "Oscillation stop was detected,"
291 "you may have to readjust the clock\n");
292 *need_reset = 1;
295 if (ctrl[1] & (RX8025_BIT_CTRL2_DAFG | RX8025_BIT_CTRL2_WAFG)) {
296 dev_warn(&client->dev, "Alarm was detected\n");
297 need_clear = 1;
300 if (!(ctrl[1] & RX8025_BIT_CTRL2_CTFG))
301 need_clear = 1;
303 if (*need_reset || need_clear) {
304 ctrl2 = ctrl[0];
305 ctrl2 &= ~(RX8025_BIT_CTRL2_PON | RX8025_BIT_CTRL2_VDET |
306 RX8025_BIT_CTRL2_CTFG | RX8025_BIT_CTRL2_WAFG |
307 RX8025_BIT_CTRL2_DAFG);
308 ctrl2 |= RX8025_BIT_CTRL2_XST;
310 err = rx8025_write_reg(client, RX8025_REG_CTRL2, ctrl2);
312 out:
313 return err;
316 /* Alarm support */
317 static int rx8025_read_alarm(struct device *dev, struct rtc_wkalrm *t)
319 struct rx8025_data *rx8025 = dev_get_drvdata(dev);
320 struct i2c_client *client = rx8025->client;
321 u8 ctrl2, ald[2];
322 int err;
324 if (client->irq <= 0)
325 return -EINVAL;
327 err = rx8025_read_regs(client, RX8025_REG_ALDMIN, 2, ald);
328 if (err)
329 return err;
331 err = rx8025_read_reg(client, RX8025_REG_CTRL2, &ctrl2);
332 if (err)
333 return err;
335 dev_dbg(dev, "%s: read alarm 0x%02x 0x%02x ctrl2 %02x\n",
336 __func__, ald[0], ald[1], ctrl2);
338 /* Hardware alarms precision is 1 minute! */
339 t->time.tm_sec = 0;
340 t->time.tm_min = bcd2bin(ald[0] & 0x7f);
341 if (rx8025->ctrl1 & RX8025_BIT_CTRL1_1224)
342 t->time.tm_hour = bcd2bin(ald[1] & 0x3f);
343 else
344 t->time.tm_hour = bcd2bin(ald[1] & 0x1f) % 12
345 + (ald[1] & 0x20 ? 12 : 0);
347 t->time.tm_wday = -1;
348 t->time.tm_mday = -1;
349 t->time.tm_mon = -1;
350 t->time.tm_year = -1;
352 dev_dbg(dev, "%s: date: %ds %dm %dh %dmd %dm %dy\n",
353 __func__,
354 t->time.tm_sec, t->time.tm_min, t->time.tm_hour,
355 t->time.tm_mday, t->time.tm_mon, t->time.tm_year);
356 t->enabled = !!(rx8025->ctrl1 & RX8025_BIT_CTRL1_DALE);
357 t->pending = (ctrl2 & RX8025_BIT_CTRL2_DAFG) && t->enabled;
359 return err;
362 static int rx8025_set_alarm(struct device *dev, struct rtc_wkalrm *t)
364 struct i2c_client *client = to_i2c_client(dev);
365 struct rx8025_data *rx8025 = dev_get_drvdata(dev);
366 u8 ald[2];
367 int err;
369 if (client->irq <= 0)
370 return -EINVAL;
372 /* Hardware alarm precision is 1 minute! */
373 ald[0] = bin2bcd(t->time.tm_min);
374 if (rx8025->ctrl1 & RX8025_BIT_CTRL1_1224)
375 ald[1] = bin2bcd(t->time.tm_hour);
376 else
377 ald[1] = (t->time.tm_hour >= 12 ? 0x20 : 0)
378 | bin2bcd((t->time.tm_hour + 11) % 12 + 1);
380 dev_dbg(dev, "%s: write 0x%02x 0x%02x\n", __func__, ald[0], ald[1]);
382 if (rx8025->ctrl1 & RX8025_BIT_CTRL1_DALE) {
383 rx8025->ctrl1 &= ~RX8025_BIT_CTRL1_DALE;
384 err = rx8025_write_reg(rx8025->client, RX8025_REG_CTRL1,
385 rx8025->ctrl1);
386 if (err)
387 return err;
389 err = rx8025_write_regs(rx8025->client, RX8025_REG_ALDMIN, 2, ald);
390 if (err)
391 return err;
393 if (t->enabled) {
394 rx8025->ctrl1 |= RX8025_BIT_CTRL1_DALE;
395 err = rx8025_write_reg(rx8025->client, RX8025_REG_CTRL1,
396 rx8025->ctrl1);
397 if (err)
398 return err;
401 return 0;
404 static int rx8025_alarm_irq_enable(struct device *dev, unsigned int enabled)
406 struct rx8025_data *rx8025 = dev_get_drvdata(dev);
407 u8 ctrl1;
408 int err;
410 ctrl1 = rx8025->ctrl1;
411 if (enabled)
412 ctrl1 |= RX8025_BIT_CTRL1_DALE;
413 else
414 ctrl1 &= ~RX8025_BIT_CTRL1_DALE;
416 if (ctrl1 != rx8025->ctrl1) {
417 rx8025->ctrl1 = ctrl1;
418 err = rx8025_write_reg(rx8025->client, RX8025_REG_CTRL1,
419 rx8025->ctrl1);
420 if (err)
421 return err;
423 return 0;
426 static int rx8025_irq_set_state(struct device *dev, int enabled)
428 struct i2c_client *client = to_i2c_client(dev);
429 struct rx8025_data *rx8025 = i2c_get_clientdata(client);
430 int ctrl1;
431 int err;
433 if (client->irq <= 0)
434 return -ENXIO;
436 ctrl1 = rx8025->ctrl1 & ~RX8025_BIT_CTRL1_CT;
437 if (enabled)
438 ctrl1 |= RX8025_BIT_CTRL1_CT_1HZ;
439 if (ctrl1 != rx8025->ctrl1) {
440 rx8025->ctrl1 = ctrl1;
441 err = rx8025_write_reg(rx8025->client, RX8025_REG_CTRL1,
442 rx8025->ctrl1);
443 if (err)
444 return err;
447 return 0;
450 static struct rtc_class_ops rx8025_rtc_ops = {
451 .read_time = rx8025_get_time,
452 .set_time = rx8025_set_time,
453 .read_alarm = rx8025_read_alarm,
454 .set_alarm = rx8025_set_alarm,
455 .alarm_irq_enable = rx8025_alarm_irq_enable,
456 .irq_set_state = rx8025_irq_set_state,
460 * Clock precision adjustment support
462 * According to the RX8025 SA/NB application manual the frequency and
463 * temperature charateristics can be approximated using the following
464 * equation:
466 * df = a * (ut - t)**2
468 * df: Frequency deviation in any temperature
469 * a : Coefficient = (-35 +-5) * 10**-9
470 * ut: Ultimate temperature in degree = +25 +-5 degree
471 * t : Any temperature in degree
473 * Note that the clock adjustment in ppb must be entered (which is
474 * the negative value of the deviation).
476 static int rx8025_get_clock_adjust(struct device *dev, int *adj)
478 struct i2c_client *client = to_i2c_client(dev);
479 u8 digoff;
480 int err;
482 err = rx8025_read_reg(client, RX8025_REG_DIGOFF, &digoff);
483 if (err)
484 return err;
486 *adj = digoff >= 64 ? digoff - 128 : digoff;
487 if (*adj > 0)
488 (*adj)--;
489 *adj *= -RX8025_ADJ_RESOLUTION;
491 return 0;
494 static int rx8025_set_clock_adjust(struct device *dev, int adj)
496 struct i2c_client *client = to_i2c_client(dev);
497 u8 digoff;
498 int err;
500 adj /= -RX8025_ADJ_RESOLUTION;
501 if (adj > RX8025_ADJ_DATA_MAX)
502 adj = RX8025_ADJ_DATA_MAX;
503 else if (adj < RX8025_ADJ_DATA_MIN)
504 adj = RX8025_ADJ_DATA_MIN;
505 else if (adj > 0)
506 adj++;
507 else if (adj < 0)
508 adj += 128;
509 digoff = adj;
511 err = rx8025_write_reg(client, RX8025_REG_DIGOFF, digoff);
512 if (err)
513 return err;
515 dev_dbg(dev, "%s: write 0x%02x\n", __func__, digoff);
517 return 0;
520 static ssize_t rx8025_sysfs_show_clock_adjust(struct device *dev,
521 struct device_attribute *attr,
522 char *buf)
524 int err, adj;
526 err = rx8025_get_clock_adjust(dev, &adj);
527 if (err)
528 return err;
530 return sprintf(buf, "%d\n", adj);
533 static ssize_t rx8025_sysfs_store_clock_adjust(struct device *dev,
534 struct device_attribute *attr,
535 const char *buf, size_t count)
537 int adj, err;
539 if (sscanf(buf, "%i", &adj) != 1)
540 return -EINVAL;
542 err = rx8025_set_clock_adjust(dev, adj);
544 return err ? err : count;
547 static DEVICE_ATTR(clock_adjust_ppb, S_IRUGO | S_IWUSR,
548 rx8025_sysfs_show_clock_adjust,
549 rx8025_sysfs_store_clock_adjust);
551 static int rx8025_sysfs_register(struct device *dev)
553 return device_create_file(dev, &dev_attr_clock_adjust_ppb);
556 static void rx8025_sysfs_unregister(struct device *dev)
558 device_remove_file(dev, &dev_attr_clock_adjust_ppb);
561 static int __devinit rx8025_probe(struct i2c_client *client,
562 const struct i2c_device_id *id)
564 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
565 struct rx8025_data *rx8025;
566 int err, need_reset = 0;
568 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA
569 | I2C_FUNC_SMBUS_I2C_BLOCK)) {
570 dev_err(&adapter->dev,
571 "doesn't support required functionality\n");
572 err = -EIO;
573 goto errout;
576 rx8025 = kzalloc(sizeof(*rx8025), GFP_KERNEL);
577 if (!rx8025) {
578 dev_err(&adapter->dev, "failed to alloc memory\n");
579 err = -ENOMEM;
580 goto errout;
583 rx8025->client = client;
584 i2c_set_clientdata(client, rx8025);
585 INIT_WORK(&rx8025->work, rx8025_work);
587 err = rx8025_init_client(client, &need_reset);
588 if (err)
589 goto errout_free;
591 if (need_reset) {
592 struct rtc_time tm;
593 dev_info(&client->dev,
594 "bad conditions detected, resetting date\n");
595 rtc_time_to_tm(0, &tm); /* 1970/1/1 */
596 rx8025_set_time(&client->dev, &tm);
599 rx8025->rtc = rtc_device_register(client->name, &client->dev,
600 &rx8025_rtc_ops, THIS_MODULE);
601 if (IS_ERR(rx8025->rtc)) {
602 err = PTR_ERR(rx8025->rtc);
603 dev_err(&client->dev, "unable to register the class device\n");
604 goto errout_free;
607 if (client->irq > 0) {
608 dev_info(&client->dev, "IRQ %d supplied\n", client->irq);
609 err = request_irq(client->irq, rx8025_irq,
610 0, "rx8025", client);
611 if (err) {
612 dev_err(&client->dev, "unable to request IRQ\n");
613 goto errout_reg;
617 rx8025->rtc->irq_freq = 1;
618 rx8025->rtc->max_user_freq = 1;
620 err = rx8025_sysfs_register(&client->dev);
621 if (err)
622 goto errout_irq;
624 return 0;
626 errout_irq:
627 if (client->irq > 0)
628 free_irq(client->irq, client);
630 errout_reg:
631 rtc_device_unregister(rx8025->rtc);
633 errout_free:
634 i2c_set_clientdata(client, NULL);
635 kfree(rx8025);
637 errout:
638 dev_err(&adapter->dev, "probing for rx8025 failed\n");
639 return err;
642 static int __devexit rx8025_remove(struct i2c_client *client)
644 struct rx8025_data *rx8025 = i2c_get_clientdata(client);
645 struct mutex *lock = &rx8025->rtc->ops_lock;
647 if (client->irq > 0) {
648 mutex_lock(lock);
649 rx8025->exiting = 1;
650 mutex_unlock(lock);
652 free_irq(client->irq, client);
653 flush_scheduled_work();
656 rx8025_sysfs_unregister(&client->dev);
657 rtc_device_unregister(rx8025->rtc);
658 i2c_set_clientdata(client, NULL);
659 kfree(rx8025);
660 return 0;
663 static struct i2c_driver rx8025_driver = {
664 .driver = {
665 .name = "rtc-rx8025",
666 .owner = THIS_MODULE,
668 .probe = rx8025_probe,
669 .remove = __devexit_p(rx8025_remove),
670 .id_table = rx8025_id,
673 static int __init rx8025_init(void)
675 return i2c_add_driver(&rx8025_driver);
678 static void __exit rx8025_exit(void)
680 i2c_del_driver(&rx8025_driver);
683 MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
684 MODULE_DESCRIPTION("RX-8025 SA/NB RTC driver");
685 MODULE_LICENSE("GPL");
687 module_init(rx8025_init);
688 module_exit(rx8025_exit);