2 * Seiko Instruments S-35390A RTC Driver
4 * Copyright (c) 2007 Byron Bradley
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
12 #include <linux/module.h>
13 #include <linux/rtc.h>
14 #include <linux/i2c.h>
15 #include <linux/bitrev.h>
16 #include <linux/bcd.h>
17 #include <linux/slab.h>
19 #define S35390A_CMD_STATUS1 0
20 #define S35390A_CMD_STATUS2 1
21 #define S35390A_CMD_TIME1 2
23 #define S35390A_BYTE_YEAR 0
24 #define S35390A_BYTE_MONTH 1
25 #define S35390A_BYTE_DAY 2
26 #define S35390A_BYTE_WDAY 3
27 #define S35390A_BYTE_HOURS 4
28 #define S35390A_BYTE_MINS 5
29 #define S35390A_BYTE_SECS 6
31 #define S35390A_FLAG_POC 0x01
32 #define S35390A_FLAG_BLD 0x02
33 #define S35390A_FLAG_24H 0x40
34 #define S35390A_FLAG_RESET 0x80
35 #define S35390A_FLAG_TEST 0x01
38 struct i2c_client *client[8];
39 struct rtc_device *rtc;
43 static int s35390a_set_reg(struct s35390a *s35390a, int reg, char *buf, int len)
45 struct i2c_client *client = s35390a->client[reg];
46 struct i2c_msg msg[] = {
47 { client->addr, 0, len, buf },
50 if ((i2c_transfer(client->adapter, msg, 1)) != 1)
56 static int s35390a_get_reg(struct s35390a *s35390a, int reg, char *buf, int len)
58 struct i2c_client *client = s35390a->client[reg];
59 struct i2c_msg msg[] = {
60 { client->addr, I2C_M_RD, len, buf },
63 if ((i2c_transfer(client->adapter, msg, 1)) != 1)
69 static int s35390a_reset(struct s35390a *s35390a)
73 if (s35390a_get_reg(s35390a, S35390A_CMD_STATUS1, buf, sizeof(buf)) < 0)
76 if (!(buf[0] & (S35390A_FLAG_POC | S35390A_FLAG_BLD)))
79 buf[0] |= (S35390A_FLAG_RESET | S35390A_FLAG_24H);
81 return s35390a_set_reg(s35390a, S35390A_CMD_STATUS1, buf, sizeof(buf));
84 static int s35390a_disable_test_mode(struct s35390a *s35390a)
88 if (s35390a_get_reg(s35390a, S35390A_CMD_STATUS2, buf, sizeof(buf)) < 0)
91 if (!(buf[0] & S35390A_FLAG_TEST))
94 buf[0] &= ~S35390A_FLAG_TEST;
95 return s35390a_set_reg(s35390a, S35390A_CMD_STATUS2, buf, sizeof(buf));
98 static char s35390a_hr2reg(struct s35390a *s35390a, int hour)
100 if (s35390a->twentyfourhour)
101 return BIN2BCD(hour);
104 return BIN2BCD(hour);
106 return 0x40 | BIN2BCD(hour - 12);
109 static int s35390a_reg2hr(struct s35390a *s35390a, char reg)
113 if (s35390a->twentyfourhour)
114 return BCD2BIN(reg & 0x3f);
116 hour = BCD2BIN(reg & 0x3f);
123 static int s35390a_set_datetime(struct i2c_client *client, struct rtc_time *tm)
125 struct s35390a *s35390a = i2c_get_clientdata(client);
129 dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d mday=%d, "
130 "mon=%d, year=%d, wday=%d\n", __func__, tm->tm_sec,
131 tm->tm_min, tm->tm_hour, tm->tm_mday, tm->tm_mon, tm->tm_year,
134 buf[S35390A_BYTE_YEAR] = BIN2BCD(tm->tm_year - 100);
135 buf[S35390A_BYTE_MONTH] = BIN2BCD(tm->tm_mon + 1);
136 buf[S35390A_BYTE_DAY] = BIN2BCD(tm->tm_mday);
137 buf[S35390A_BYTE_WDAY] = BIN2BCD(tm->tm_wday);
138 buf[S35390A_BYTE_HOURS] = s35390a_hr2reg(s35390a, tm->tm_hour);
139 buf[S35390A_BYTE_MINS] = BIN2BCD(tm->tm_min);
140 buf[S35390A_BYTE_SECS] = BIN2BCD(tm->tm_sec);
142 /* This chip expects the bits of each byte to be in reverse order */
143 for (i = 0; i < 7; ++i)
144 buf[i] = bitrev8(buf[i]);
146 err = s35390a_set_reg(s35390a, S35390A_CMD_TIME1, buf, sizeof(buf));
151 static int s35390a_get_datetime(struct i2c_client *client, struct rtc_time *tm)
153 struct s35390a *s35390a = i2c_get_clientdata(client);
157 err = s35390a_get_reg(s35390a, S35390A_CMD_TIME1, buf, sizeof(buf));
161 /* This chip returns the bits of each byte in reverse order */
162 for (i = 0; i < 7; ++i)
163 buf[i] = bitrev8(buf[i]);
165 tm->tm_sec = BCD2BIN(buf[S35390A_BYTE_SECS]);
166 tm->tm_min = BCD2BIN(buf[S35390A_BYTE_MINS]);
167 tm->tm_hour = s35390a_reg2hr(s35390a, buf[S35390A_BYTE_HOURS]);
168 tm->tm_wday = BCD2BIN(buf[S35390A_BYTE_WDAY]);
169 tm->tm_mday = BCD2BIN(buf[S35390A_BYTE_DAY]);
170 tm->tm_mon = BCD2BIN(buf[S35390A_BYTE_MONTH]) - 1;
171 tm->tm_year = BCD2BIN(buf[S35390A_BYTE_YEAR]) + 100;
173 dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d, mday=%d, "
174 "mon=%d, year=%d, wday=%d\n", __func__, tm->tm_sec,
175 tm->tm_min, tm->tm_hour, tm->tm_mday, tm->tm_mon, tm->tm_year,
178 return rtc_valid_tm(tm);
181 static int s35390a_rtc_read_time(struct device *dev, struct rtc_time *tm)
183 return s35390a_get_datetime(to_i2c_client(dev), tm);
186 static int s35390a_rtc_set_time(struct device *dev, struct rtc_time *tm)
188 return s35390a_set_datetime(to_i2c_client(dev), tm);
191 static const struct rtc_class_ops s35390a_rtc_ops = {
192 .read_time = s35390a_rtc_read_time,
193 .set_time = s35390a_rtc_set_time,
196 static struct i2c_driver s35390a_driver;
198 static int s35390a_probe(struct i2c_client *client)
202 struct s35390a *s35390a;
206 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
211 s35390a = kzalloc(sizeof(struct s35390a), GFP_KERNEL);
217 s35390a->client[0] = client;
218 i2c_set_clientdata(client, s35390a);
220 /* This chip uses multiple addresses, use dummy devices for them */
221 for (i = 1; i < 8; ++i) {
222 s35390a->client[i] = i2c_new_dummy(client->adapter,
223 client->addr + i, "rtc-s35390a");
224 if (!s35390a->client[i]) {
225 dev_err(&client->dev, "Address %02x unavailable\n",
232 err = s35390a_reset(s35390a);
234 dev_err(&client->dev, "error resetting chip\n");
238 err = s35390a_disable_test_mode(s35390a);
240 dev_err(&client->dev, "error disabling test mode\n");
244 err = s35390a_get_reg(s35390a, S35390A_CMD_STATUS1, buf, sizeof(buf));
246 dev_err(&client->dev, "error checking 12/24 hour mode\n");
249 if (buf[0] & S35390A_FLAG_24H)
250 s35390a->twentyfourhour = 1;
252 s35390a->twentyfourhour = 0;
254 if (s35390a_get_datetime(client, &tm) < 0)
255 dev_warn(&client->dev, "clock needs to be set\n");
257 s35390a->rtc = rtc_device_register(s35390a_driver.driver.name,
258 &client->dev, &s35390a_rtc_ops, THIS_MODULE);
260 if (IS_ERR(s35390a->rtc)) {
261 err = PTR_ERR(s35390a->rtc);
267 for (i = 1; i < 8; ++i)
268 if (s35390a->client[i])
269 i2c_unregister_device(s35390a->client[i]);
271 i2c_set_clientdata(client, NULL);
277 static int s35390a_remove(struct i2c_client *client)
281 struct s35390a *s35390a = i2c_get_clientdata(client);
282 for (i = 1; i < 8; ++i)
283 if (s35390a->client[i])
284 i2c_unregister_device(s35390a->client[i]);
286 rtc_device_unregister(s35390a->rtc);
288 i2c_set_clientdata(client, NULL);
293 static struct i2c_driver s35390a_driver = {
295 .name = "rtc-s35390a",
297 .probe = s35390a_probe,
298 .remove = s35390a_remove,
301 static int __init s35390a_rtc_init(void)
303 return i2c_add_driver(&s35390a_driver);
306 static void __exit s35390a_rtc_exit(void)
308 i2c_del_driver(&s35390a_driver);
311 MODULE_AUTHOR("Byron Bradley <byron.bbradley@gmail.com>");
312 MODULE_DESCRIPTION("S35390A RTC driver");
313 MODULE_LICENSE("GPL");
315 module_init(s35390a_rtc_init);
316 module_exit(s35390a_rtc_exit);