Merge git://git.kernel.org/pub/scm/linux/kernel/git/herbert/crypto-2.6
[deliverable/linux.git] / drivers / rtc / rtc-rx8025.c
CommitLineData
3c2b9075
WG
1/*
2 * Driver for Epson's RTC module RX-8025 SA/NB
3 *
4 * Copyright (C) 2009 Wolfgang Grandegger <wg@grandegger.com>
5 *
6 * Copyright (C) 2005 by Digi International Inc.
7 * All rights reserved.
8 *
9 * Modified by fengjh at rising.com.cn
10 * <http://lists.lm-sensors.org/mailman/listinfo/lm-sensors>
11 * 2006.11
12 *
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>
16 *
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.
20 */
3c2b9075 21#include <linux/bcd.h>
2ddd1869 22#include <linux/bitops.h>
3c2b9075 23#include <linux/i2c.h>
15d3bdc2
AB
24#include <linux/kernel.h>
25#include <linux/module.h>
3c2b9075
WG
26#include <linux/rtc.h>
27
28/* Register definitions */
29#define RX8025_REG_SEC 0x00
30#define RX8025_REG_MIN 0x01
31#define RX8025_REG_HOUR 0x02
32#define RX8025_REG_WDAY 0x03
33#define RX8025_REG_MDAY 0x04
34#define RX8025_REG_MONTH 0x05
35#define RX8025_REG_YEAR 0x06
36#define RX8025_REG_DIGOFF 0x07
37#define RX8025_REG_ALWMIN 0x08
38#define RX8025_REG_ALWHOUR 0x09
39#define RX8025_REG_ALWWDAY 0x0a
40#define RX8025_REG_ALDMIN 0x0b
41#define RX8025_REG_ALDHOUR 0x0c
42/* 0x0d is reserved */
43#define RX8025_REG_CTRL1 0x0e
44#define RX8025_REG_CTRL2 0x0f
45
46#define RX8025_BIT_CTRL1_CT (7 << 0)
47/* 1 Hz periodic level irq */
48#define RX8025_BIT_CTRL1_CT_1HZ 4
2ddd1869
AB
49#define RX8025_BIT_CTRL1_TEST BIT(3)
50#define RX8025_BIT_CTRL1_1224 BIT(5)
51#define RX8025_BIT_CTRL1_DALE BIT(6)
52#define RX8025_BIT_CTRL1_WALE BIT(7)
3c2b9075 53
2ddd1869
AB
54#define RX8025_BIT_CTRL2_DAFG BIT(0)
55#define RX8025_BIT_CTRL2_WAFG BIT(1)
56#define RX8025_BIT_CTRL2_CTFG BIT(2)
57#define RX8025_BIT_CTRL2_PON BIT(4)
58#define RX8025_BIT_CTRL2_XST BIT(5)
59#define RX8025_BIT_CTRL2_VDET BIT(6)
3c2b9075
WG
60
61/* Clock precision adjustment */
62#define RX8025_ADJ_RESOLUTION 3050 /* in ppb */
63#define RX8025_ADJ_DATA_MAX 62
64#define RX8025_ADJ_DATA_MIN -62
65
66static const struct i2c_device_id rx8025_id[] = {
67 { "rx8025", 0 },
68 { }
69};
70MODULE_DEVICE_TABLE(i2c, rx8025_id);
71
72struct rx8025_data {
73 struct i2c_client *client;
74 struct rtc_device *rtc;
3c2b9075 75 u8 ctrl1;
3c2b9075
WG
76};
77
fd9061fb 78static s32 rx8025_read_reg(const struct i2c_client *client, u8 number)
3c2b9075 79{
fd9061fb 80 return i2c_smbus_read_byte_data(client, number << 4);
3c2b9075
WG
81}
82
fd9061fb
AB
83static int rx8025_read_regs(const struct i2c_client *client,
84 u8 number, u8 length, u8 *values)
3c2b9075 85{
fd9061fb
AB
86 int ret = i2c_smbus_read_i2c_block_data(client, number << 4, length,
87 values);
88 if (ret != length)
3c2b9075 89 return ret < 0 ? ret : -EIO;
3c2b9075
WG
90
91 return 0;
92}
93
fd9061fb
AB
94static s32 rx8025_write_reg(const struct i2c_client *client, u8 number,
95 u8 value)
3c2b9075 96{
fd9061fb 97 return i2c_smbus_write_byte_data(client, number << 4, value);
3c2b9075
WG
98}
99
fd9061fb
AB
100static s32 rx8025_write_regs(const struct i2c_client *client,
101 u8 number, u8 length, const u8 *values)
3c2b9075 102{
fd9061fb
AB
103 return i2c_smbus_write_i2c_block_data(client, number << 4,
104 length, values);
3c2b9075
WG
105}
106
efbbb4fd
AB
107static int rx8025_check_validity(struct device *dev)
108{
109 struct rx8025_data *rx8025 = dev_get_drvdata(dev);
110 int ctrl2;
111
112 ctrl2 = rx8025_read_reg(rx8025->client, RX8025_REG_CTRL2);
113 if (ctrl2 < 0)
114 return ctrl2;
115
116 if (ctrl2 & RX8025_BIT_CTRL2_VDET)
117 dev_warn(dev, "power voltage drop detected\n");
118
119 if (ctrl2 & RX8025_BIT_CTRL2_PON) {
120 dev_warn(dev, "power-on reset detected, date is invalid\n");
121 return -EINVAL;
122 }
123
124 if (!(ctrl2 & RX8025_BIT_CTRL2_XST)) {
125 dev_warn(dev, "crystal stopped, date is invalid\n");
126 return -EINVAL;
127 }
128
129 return 0;
130}
131
8c4a4467
AB
132static int rx8025_reset_validity(struct i2c_client *client)
133{
134 int ctrl2 = rx8025_read_reg(client, RX8025_REG_CTRL2);
135
136 if (ctrl2 < 0)
137 return ctrl2;
138
139 ctrl2 &= ~(RX8025_BIT_CTRL2_PON | RX8025_BIT_CTRL2_VDET);
140
141 return rx8025_write_reg(client, RX8025_REG_CTRL2,
142 ctrl2 | RX8025_BIT_CTRL2_XST);
143}
144
b6a57c95 145static irqreturn_t rx8025_handle_irq(int irq, void *dev_id)
3c2b9075
WG
146{
147 struct i2c_client *client = dev_id;
148 struct rx8025_data *rx8025 = i2c_get_clientdata(client);
fd9061fb 149 int status;
3c2b9075 150
fd9061fb
AB
151 status = rx8025_read_reg(client, RX8025_REG_CTRL2);
152 if (status < 0)
3c2b9075
WG
153 goto out;
154
155 if (!(status & RX8025_BIT_CTRL2_XST))
156 dev_warn(&client->dev, "Oscillation stop was detected,"
157 "you may have to readjust the clock\n");
158
159 if (status & RX8025_BIT_CTRL2_CTFG) {
160 /* periodic */
161 status &= ~RX8025_BIT_CTRL2_CTFG;
3c2b9075 162 rtc_update_irq(rx8025->rtc, 1, RTC_PF | RTC_IRQF);
3c2b9075
WG
163 }
164
165 if (status & RX8025_BIT_CTRL2_DAFG) {
166 /* alarm */
167 status &= RX8025_BIT_CTRL2_DAFG;
168 if (rx8025_write_reg(client, RX8025_REG_CTRL1,
169 rx8025->ctrl1 & ~RX8025_BIT_CTRL1_DALE))
170 goto out;
3c2b9075 171 rtc_update_irq(rx8025->rtc, 1, RTC_AF | RTC_IRQF);
3c2b9075
WG
172 }
173
3c2b9075 174out:
b6a57c95 175 return IRQ_HANDLED;
3c2b9075
WG
176}
177
178static int rx8025_get_time(struct device *dev, struct rtc_time *dt)
179{
180 struct rx8025_data *rx8025 = dev_get_drvdata(dev);
fd9061fb 181 u8 date[7];
efbbb4fd 182 int err;
6f0a8cfe 183
efbbb4fd
AB
184 err = rx8025_check_validity(dev);
185 if (err)
186 return err;
6f0a8cfe 187
3c2b9075
WG
188 err = rx8025_read_regs(rx8025->client, RX8025_REG_SEC, 7, date);
189 if (err)
190 return err;
191
192 dev_dbg(dev, "%s: read 0x%02x 0x%02x "
193 "0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n", __func__,
194 date[0], date[1], date[2], date[3], date[4],
195 date[5], date[6]);
196
197 dt->tm_sec = bcd2bin(date[RX8025_REG_SEC] & 0x7f);
198 dt->tm_min = bcd2bin(date[RX8025_REG_MIN] & 0x7f);
199 if (rx8025->ctrl1 & RX8025_BIT_CTRL1_1224)
200 dt->tm_hour = bcd2bin(date[RX8025_REG_HOUR] & 0x3f);
201 else
202 dt->tm_hour = bcd2bin(date[RX8025_REG_HOUR] & 0x1f) % 12
203 + (date[RX8025_REG_HOUR] & 0x20 ? 12 : 0);
204
205 dt->tm_mday = bcd2bin(date[RX8025_REG_MDAY] & 0x3f);
206 dt->tm_mon = bcd2bin(date[RX8025_REG_MONTH] & 0x1f) - 1;
32672c55 207 dt->tm_year = bcd2bin(date[RX8025_REG_YEAR]) + 100;
3c2b9075
WG
208
209 dev_dbg(dev, "%s: date %ds %dm %dh %dmd %dm %dy\n", __func__,
210 dt->tm_sec, dt->tm_min, dt->tm_hour,
211 dt->tm_mday, dt->tm_mon, dt->tm_year);
212
213 return rtc_valid_tm(dt);
214}
215
216static int rx8025_set_time(struct device *dev, struct rtc_time *dt)
217{
218 struct rx8025_data *rx8025 = dev_get_drvdata(dev);
219 u8 date[7];
8c4a4467 220 int ret;
3c2b9075 221
32672c55
AB
222 if ((dt->tm_year < 100) || (dt->tm_year > 199))
223 return -EINVAL;
3c2b9075
WG
224
225 /*
226 * Here the read-only bits are written as "0". I'm not sure if that
227 * is sound.
228 */
229 date[RX8025_REG_SEC] = bin2bcd(dt->tm_sec);
230 date[RX8025_REG_MIN] = bin2bcd(dt->tm_min);
231 if (rx8025->ctrl1 & RX8025_BIT_CTRL1_1224)
232 date[RX8025_REG_HOUR] = bin2bcd(dt->tm_hour);
233 else
234 date[RX8025_REG_HOUR] = (dt->tm_hour >= 12 ? 0x20 : 0)
235 | bin2bcd((dt->tm_hour + 11) % 12 + 1);
236
237 date[RX8025_REG_WDAY] = bin2bcd(dt->tm_wday);
238 date[RX8025_REG_MDAY] = bin2bcd(dt->tm_mday);
239 date[RX8025_REG_MONTH] = bin2bcd(dt->tm_mon + 1);
32672c55 240 date[RX8025_REG_YEAR] = bin2bcd(dt->tm_year - 100);
3c2b9075
WG
241
242 dev_dbg(dev,
243 "%s: write 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
244 __func__,
245 date[0], date[1], date[2], date[3], date[4], date[5], date[6]);
246
8c4a4467
AB
247 ret = rx8025_write_regs(rx8025->client, RX8025_REG_SEC, 7, date);
248 if (ret < 0)
249 return ret;
250
251 return rx8025_reset_validity(rx8025->client);
3c2b9075
WG
252}
253
6f0a8cfe 254static int rx8025_init_client(struct i2c_client *client)
3c2b9075
WG
255{
256 struct rx8025_data *rx8025 = i2c_get_clientdata(client);
257 u8 ctrl[2], ctrl2;
258 int need_clear = 0;
259 int err;
260
261 err = rx8025_read_regs(rx8025->client, RX8025_REG_CTRL1, 2, ctrl);
262 if (err)
263 goto out;
264
265 /* Keep test bit zero ! */
266 rx8025->ctrl1 = ctrl[0] & ~RX8025_BIT_CTRL1_TEST;
267
3c2b9075
WG
268 if (ctrl[1] & (RX8025_BIT_CTRL2_DAFG | RX8025_BIT_CTRL2_WAFG)) {
269 dev_warn(&client->dev, "Alarm was detected\n");
270 need_clear = 1;
271 }
272
5c66e1e0 273 if (ctrl[1] & RX8025_BIT_CTRL2_CTFG)
3c2b9075
WG
274 need_clear = 1;
275
6f0a8cfe 276 if (need_clear) {
a27c7bf6 277 ctrl2 = ctrl[1];
8c4a4467 278 ctrl2 &= ~(RX8025_BIT_CTRL2_CTFG | RX8025_BIT_CTRL2_WAFG |
3c2b9075 279 RX8025_BIT_CTRL2_DAFG);
3c2b9075
WG
280
281 err = rx8025_write_reg(client, RX8025_REG_CTRL2, ctrl2);
282 }
283out:
284 return err;
285}
286
287/* Alarm support */
288static int rx8025_read_alarm(struct device *dev, struct rtc_wkalrm *t)
289{
290 struct rx8025_data *rx8025 = dev_get_drvdata(dev);
291 struct i2c_client *client = rx8025->client;
fd9061fb
AB
292 u8 ald[2];
293 int ctrl2, err;
3c2b9075
WG
294
295 if (client->irq <= 0)
296 return -EINVAL;
297
298 err = rx8025_read_regs(client, RX8025_REG_ALDMIN, 2, ald);
299 if (err)
300 return err;
301
fd9061fb
AB
302 ctrl2 = rx8025_read_reg(client, RX8025_REG_CTRL2);
303 if (ctrl2 < 0)
304 return ctrl2;
3c2b9075
WG
305
306 dev_dbg(dev, "%s: read alarm 0x%02x 0x%02x ctrl2 %02x\n",
307 __func__, ald[0], ald[1], ctrl2);
308
309 /* Hardware alarms precision is 1 minute! */
310 t->time.tm_sec = 0;
311 t->time.tm_min = bcd2bin(ald[0] & 0x7f);
312 if (rx8025->ctrl1 & RX8025_BIT_CTRL1_1224)
313 t->time.tm_hour = bcd2bin(ald[1] & 0x3f);
314 else
315 t->time.tm_hour = bcd2bin(ald[1] & 0x1f) % 12
316 + (ald[1] & 0x20 ? 12 : 0);
317
318 t->time.tm_wday = -1;
319 t->time.tm_mday = -1;
320 t->time.tm_mon = -1;
321 t->time.tm_year = -1;
322
323 dev_dbg(dev, "%s: date: %ds %dm %dh %dmd %dm %dy\n",
324 __func__,
325 t->time.tm_sec, t->time.tm_min, t->time.tm_hour,
326 t->time.tm_mday, t->time.tm_mon, t->time.tm_year);
327 t->enabled = !!(rx8025->ctrl1 & RX8025_BIT_CTRL1_DALE);
328 t->pending = (ctrl2 & RX8025_BIT_CTRL2_DAFG) && t->enabled;
329
330 return err;
331}
332
333static int rx8025_set_alarm(struct device *dev, struct rtc_wkalrm *t)
334{
335 struct i2c_client *client = to_i2c_client(dev);
336 struct rx8025_data *rx8025 = dev_get_drvdata(dev);
337 u8 ald[2];
338 int err;
339
340 if (client->irq <= 0)
341 return -EINVAL;
342
343 /* Hardware alarm precision is 1 minute! */
344 ald[0] = bin2bcd(t->time.tm_min);
345 if (rx8025->ctrl1 & RX8025_BIT_CTRL1_1224)
346 ald[1] = bin2bcd(t->time.tm_hour);
347 else
348 ald[1] = (t->time.tm_hour >= 12 ? 0x20 : 0)
349 | bin2bcd((t->time.tm_hour + 11) % 12 + 1);
350
351 dev_dbg(dev, "%s: write 0x%02x 0x%02x\n", __func__, ald[0], ald[1]);
352
353 if (rx8025->ctrl1 & RX8025_BIT_CTRL1_DALE) {
354 rx8025->ctrl1 &= ~RX8025_BIT_CTRL1_DALE;
355 err = rx8025_write_reg(rx8025->client, RX8025_REG_CTRL1,
356 rx8025->ctrl1);
357 if (err)
358 return err;
359 }
360 err = rx8025_write_regs(rx8025->client, RX8025_REG_ALDMIN, 2, ald);
361 if (err)
362 return err;
363
364 if (t->enabled) {
365 rx8025->ctrl1 |= RX8025_BIT_CTRL1_DALE;
366 err = rx8025_write_reg(rx8025->client, RX8025_REG_CTRL1,
367 rx8025->ctrl1);
368 if (err)
369 return err;
370 }
371
372 return 0;
373}
374
375static int rx8025_alarm_irq_enable(struct device *dev, unsigned int enabled)
376{
377 struct rx8025_data *rx8025 = dev_get_drvdata(dev);
378 u8 ctrl1;
379 int err;
380
381 ctrl1 = rx8025->ctrl1;
382 if (enabled)
383 ctrl1 |= RX8025_BIT_CTRL1_DALE;
384 else
385 ctrl1 &= ~RX8025_BIT_CTRL1_DALE;
386
387 if (ctrl1 != rx8025->ctrl1) {
388 rx8025->ctrl1 = ctrl1;
389 err = rx8025_write_reg(rx8025->client, RX8025_REG_CTRL1,
390 rx8025->ctrl1);
391 if (err)
392 return err;
393 }
394 return 0;
395}
396
3c2b9075
WG
397static struct rtc_class_ops rx8025_rtc_ops = {
398 .read_time = rx8025_get_time,
399 .set_time = rx8025_set_time,
400 .read_alarm = rx8025_read_alarm,
401 .set_alarm = rx8025_set_alarm,
402 .alarm_irq_enable = rx8025_alarm_irq_enable,
3c2b9075
WG
403};
404
405/*
406 * Clock precision adjustment support
407 *
408 * According to the RX8025 SA/NB application manual the frequency and
b770ffd4 409 * temperature characteristics can be approximated using the following
3c2b9075
WG
410 * equation:
411 *
412 * df = a * (ut - t)**2
413 *
414 * df: Frequency deviation in any temperature
415 * a : Coefficient = (-35 +-5) * 10**-9
416 * ut: Ultimate temperature in degree = +25 +-5 degree
417 * t : Any temperature in degree
418 *
419 * Note that the clock adjustment in ppb must be entered (which is
420 * the negative value of the deviation).
421 */
422static int rx8025_get_clock_adjust(struct device *dev, int *adj)
423{
424 struct i2c_client *client = to_i2c_client(dev);
fd9061fb 425 int digoff;
3c2b9075 426
fd9061fb
AB
427 digoff = rx8025_read_reg(client, RX8025_REG_DIGOFF);
428 if (digoff < 0)
429 return digoff;
3c2b9075
WG
430
431 *adj = digoff >= 64 ? digoff - 128 : digoff;
432 if (*adj > 0)
433 (*adj)--;
434 *adj *= -RX8025_ADJ_RESOLUTION;
435
436 return 0;
437}
438
439static int rx8025_set_clock_adjust(struct device *dev, int adj)
440{
441 struct i2c_client *client = to_i2c_client(dev);
442 u8 digoff;
443 int err;
444
445 adj /= -RX8025_ADJ_RESOLUTION;
446 if (adj > RX8025_ADJ_DATA_MAX)
447 adj = RX8025_ADJ_DATA_MAX;
448 else if (adj < RX8025_ADJ_DATA_MIN)
449 adj = RX8025_ADJ_DATA_MIN;
450 else if (adj > 0)
451 adj++;
452 else if (adj < 0)
453 adj += 128;
454 digoff = adj;
455
456 err = rx8025_write_reg(client, RX8025_REG_DIGOFF, digoff);
457 if (err)
458 return err;
459
460 dev_dbg(dev, "%s: write 0x%02x\n", __func__, digoff);
461
462 return 0;
463}
464
465static ssize_t rx8025_sysfs_show_clock_adjust(struct device *dev,
466 struct device_attribute *attr,
467 char *buf)
468{
469 int err, adj;
470
471 err = rx8025_get_clock_adjust(dev, &adj);
472 if (err)
473 return err;
474
475 return sprintf(buf, "%d\n", adj);
476}
477
478static ssize_t rx8025_sysfs_store_clock_adjust(struct device *dev,
479 struct device_attribute *attr,
480 const char *buf, size_t count)
481{
482 int adj, err;
483
484 if (sscanf(buf, "%i", &adj) != 1)
485 return -EINVAL;
486
487 err = rx8025_set_clock_adjust(dev, adj);
488
489 return err ? err : count;
490}
491
492static DEVICE_ATTR(clock_adjust_ppb, S_IRUGO | S_IWUSR,
493 rx8025_sysfs_show_clock_adjust,
494 rx8025_sysfs_store_clock_adjust);
495
496static int rx8025_sysfs_register(struct device *dev)
497{
498 return device_create_file(dev, &dev_attr_clock_adjust_ppb);
499}
500
501static void rx8025_sysfs_unregister(struct device *dev)
502{
503 device_remove_file(dev, &dev_attr_clock_adjust_ppb);
504}
505
5a167f45
GKH
506static int rx8025_probe(struct i2c_client *client,
507 const struct i2c_device_id *id)
3c2b9075
WG
508{
509 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
510 struct rx8025_data *rx8025;
6f0a8cfe 511 int err = 0;
3c2b9075
WG
512
513 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA
514 | I2C_FUNC_SMBUS_I2C_BLOCK)) {
515 dev_err(&adapter->dev,
516 "doesn't support required functionality\n");
dbcce7cf 517 return -EIO;
3c2b9075
WG
518 }
519
fac42b41 520 rx8025 = devm_kzalloc(&client->dev, sizeof(*rx8025), GFP_KERNEL);
3c2b9075 521 if (!rx8025) {
dbcce7cf 522 return -ENOMEM;
3c2b9075
WG
523 }
524
525 rx8025->client = client;
526 i2c_set_clientdata(client, rx8025);
3c2b9075 527
6f0a8cfe 528 err = rx8025_init_client(client);
3c2b9075 529 if (err)
dbcce7cf 530 return err;
3c2b9075 531
fac42b41 532 rx8025->rtc = devm_rtc_device_register(&client->dev, client->name,
3c2b9075
WG
533 &rx8025_rtc_ops, THIS_MODULE);
534 if (IS_ERR(rx8025->rtc)) {
3c2b9075 535 dev_err(&client->dev, "unable to register the class device\n");
dbcce7cf 536 return PTR_ERR(rx8025->rtc);
3c2b9075
WG
537 }
538
539 if (client->irq > 0) {
540 dev_info(&client->dev, "IRQ %d supplied\n", client->irq);
f0b63a1d
AB
541 err = devm_request_threaded_irq(&client->dev, client->irq, NULL,
542 rx8025_handle_irq, 0, "rx8025",
543 client);
3c2b9075 544 if (err) {
8a06513d
AB
545 dev_err(&client->dev, "unable to request IRQ, alarms disabled\n");
546 client->irq = 0;
3c2b9075
WG
547 }
548 }
549
3c2b9075
WG
550 rx8025->rtc->max_user_freq = 1;
551
552 err = rx8025_sysfs_register(&client->dev);
3c2b9075
WG
553 return err;
554}
555
5a167f45 556static int rx8025_remove(struct i2c_client *client)
3c2b9075 557{
3c2b9075 558 rx8025_sysfs_unregister(&client->dev);
3c2b9075
WG
559 return 0;
560}
561
562static struct i2c_driver rx8025_driver = {
563 .driver = {
564 .name = "rtc-rx8025",
3c2b9075
WG
565 },
566 .probe = rx8025_probe,
5a167f45 567 .remove = rx8025_remove,
3c2b9075
WG
568 .id_table = rx8025_id,
569};
570
0abc9201 571module_i2c_driver(rx8025_driver);
3c2b9075
WG
572
573MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
574MODULE_DESCRIPTION("RX-8025 SA/NB RTC driver");
575MODULE_LICENSE("GPL");
This page took 0.431823 seconds and 5 git commands to generate.