blob: b35f9bfa2af4db00df60d0ae74cec8bc0251dfd2 [file] [log] [blame]
Magnus Damm75b61022008-02-06 01:38:54 -08001/*
Magnus Damm2805b962008-02-06 01:38:53 -08002 * Driver for Epson RTC-9701JE
3 *
4 * Copyright (C) 2008 Magnus Damm
5 *
Magnus Damm75b61022008-02-06 01:38:54 -08006 * Based on rtc-max6902.c
Magnus Damm2805b962008-02-06 01:38:53 -08007 *
8 * Copyright (C) 2006 8D Technologies inc.
9 * Copyright (C) 2004 Compulab Ltd.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
14 */
15
16#include <linux/module.h>
17#include <linux/version.h>
18#include <linux/kernel.h>
19#include <linux/platform_device.h>
20#include <linux/device.h>
21#include <linux/init.h>
22#include <linux/rtc.h>
23#include <linux/spi/spi.h>
24#include <linux/bcd.h>
25#include <linux/delay.h>
26#include <linux/bitops.h>
27
28#define RSECCNT 0x00 /* Second Counter */
29#define RMINCNT 0x01 /* Minute Counter */
30#define RHRCNT 0x02 /* Hour Counter */
31#define RWKCNT 0x03 /* Week Counter */
32#define RDAYCNT 0x04 /* Day Counter */
33#define RMONCNT 0x05 /* Month Counter */
34#define RYRCNT 0x06 /* Year Counter */
35#define R100CNT 0x07 /* Y100 Counter */
36#define RMINAR 0x08 /* Minute Alarm */
37#define RHRAR 0x09 /* Hour Alarm */
38#define RWKAR 0x0a /* Week/Day Alarm */
39#define RTIMCNT 0x0c /* Interval Timer */
40#define REXT 0x0d /* Extension Register */
41#define RFLAG 0x0e /* RTC Flag Register */
42#define RCR 0x0f /* RTC Control Register */
43
44static int write_reg(struct device *dev, int address, unsigned char data)
45{
46 struct spi_device *spi = to_spi_device(dev);
47 unsigned char buf[2];
48
49 buf[0] = address & 0x7f;
50 buf[1] = data;
51
52 return spi_write(spi, buf, ARRAY_SIZE(buf));
53}
54
55static int read_regs(struct device *dev, unsigned char *regs, int no_regs)
56{
57 struct spi_device *spi = to_spi_device(dev);
58 u8 txbuf[1], rxbuf[1];
59 int k, ret;
60
61 ret = 0;
62
63 for (k = 0; ret == 0 && k < no_regs; k++) {
64 txbuf[0] = 0x80 | regs[k];
65 ret = spi_write_then_read(spi, txbuf, 1, rxbuf, 1);
66 regs[k] = rxbuf[0];
67 }
68
69 return ret;
70}
71
72static int r9701_get_datetime(struct device *dev, struct rtc_time *dt)
73{
Magnus Damm2805b962008-02-06 01:38:53 -080074 int ret;
75 unsigned char buf[] = { RSECCNT, RMINCNT, RHRCNT,
76 RDAYCNT, RMONCNT, RYRCNT };
77
Magnus Damm75b61022008-02-06 01:38:54 -080078 ret = read_regs(dev, buf, ARRAY_SIZE(buf));
Magnus Damm2805b962008-02-06 01:38:53 -080079 if (ret)
80 return ret;
81
82 memset(dt, 0, sizeof(*dt));
83
84 dt->tm_sec = BCD2BIN(buf[0]); /* RSECCNT */
85 dt->tm_min = BCD2BIN(buf[1]); /* RMINCNT */
86 dt->tm_hour = BCD2BIN(buf[2]); /* RHRCNT */
87
88 dt->tm_mday = BCD2BIN(buf[3]); /* RDAYCNT */
89 dt->tm_mon = BCD2BIN(buf[4]) - 1; /* RMONCNT */
90 dt->tm_year = BCD2BIN(buf[5]) + 100; /* RYRCNT */
91
92 /* the rtc device may contain illegal values on power up
93 * according to the data sheet. make sure they are valid.
94 */
95
Magnus Damm75b61022008-02-06 01:38:54 -080096 return rtc_valid_tm(dt);
Magnus Damm2805b962008-02-06 01:38:53 -080097}
98
99static int r9701_set_datetime(struct device *dev, struct rtc_time *dt)
100{
101 int ret, year;
102
103 year = dt->tm_year + 1900;
104 if (year >= 2100 || year < 2000)
105 return -EINVAL;
106
107 ret = write_reg(dev, RHRCNT, BIN2BCD(dt->tm_hour));
Magnus Damm75b61022008-02-06 01:38:54 -0800108 ret = ret ? ret : write_reg(dev, RMINCNT, BIN2BCD(dt->tm_min));
109 ret = ret ? ret : write_reg(dev, RSECCNT, BIN2BCD(dt->tm_sec));
110 ret = ret ? ret : write_reg(dev, RDAYCNT, BIN2BCD(dt->tm_mday));
111 ret = ret ? ret : write_reg(dev, RMONCNT, BIN2BCD(dt->tm_mon + 1));
112 ret = ret ? ret : write_reg(dev, RYRCNT, BIN2BCD(dt->tm_year - 100));
113 ret = ret ? ret : write_reg(dev, RWKCNT, 1 << dt->tm_wday);
Magnus Damm2805b962008-02-06 01:38:53 -0800114
115 return ret;
116}
117
118static const struct rtc_class_ops r9701_rtc_ops = {
119 .read_time = r9701_get_datetime,
120 .set_time = r9701_set_datetime,
121};
122
123static int __devinit r9701_probe(struct spi_device *spi)
124{
125 struct rtc_device *rtc;
126 unsigned char tmp;
127 int res;
128
129 rtc = rtc_device_register("r9701",
130 &spi->dev, &r9701_rtc_ops, THIS_MODULE);
131 if (IS_ERR(rtc))
132 return PTR_ERR(rtc);
133
134 dev_set_drvdata(&spi->dev, rtc);
135
Magnus Damm2805b962008-02-06 01:38:53 -0800136 tmp = R100CNT;
137 res = read_regs(&spi->dev, &tmp, 1);
138 if (res || tmp != 0x20) {
139 rtc_device_unregister(rtc);
140 return res;
141 }
142
143 return 0;
144}
145
146static int __devexit r9701_remove(struct spi_device *spi)
147{
148 struct rtc_device *rtc = dev_get_drvdata(&spi->dev);
149
150 rtc_device_unregister(rtc);
151 return 0;
152}
153
154static struct spi_driver r9701_driver = {
155 .driver = {
156 .name = "rtc-r9701",
Magnus Damm2805b962008-02-06 01:38:53 -0800157 .owner = THIS_MODULE,
158 },
159 .probe = r9701_probe,
160 .remove = __devexit_p(r9701_remove),
161};
162
163static __init int r9701_init(void)
164{
165 return spi_register_driver(&r9701_driver);
166}
167module_init(r9701_init);
168
169static __exit void r9701_exit(void)
170{
171 spi_unregister_driver(&r9701_driver);
172}
173module_exit(r9701_exit);
174
175MODULE_DESCRIPTION("r9701 spi RTC driver");
176MODULE_AUTHOR("Magnus Damm <damm@opensource.se>");
177MODULE_LICENSE("GPL");