blob: 1460f6b769f2e92975206485bedc9da19be2625e [file] [log] [blame]
Alessandro Zummo7520b942006-03-27 01:16:45 -08001/*
2 * An I2C driver for the Ricoh RS5C372 RTC
3 *
4 * Copyright (C) 2005 Pavel Mironchik <pmironchik@optifacio.net>
5 * Copyright (C) 2006 Tower Technologies
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
12#include <linux/i2c.h>
13#include <linux/rtc.h>
14#include <linux/bcd.h>
15
Riku Voipioc6f24f92006-12-06 20:39:13 -080016#define DRV_VERSION "0.3"
Alessandro Zummo7520b942006-03-27 01:16:45 -080017
18/* Addresses to scan */
19static unsigned short normal_i2c[] = { /* 0x32,*/ I2C_CLIENT_END };
20
21/* Insmod parameters */
22I2C_CLIENT_INSMOD;
23
24#define RS5C372_REG_SECS 0
25#define RS5C372_REG_MINS 1
26#define RS5C372_REG_HOURS 2
27#define RS5C372_REG_WDAY 3
28#define RS5C372_REG_DAY 4
29#define RS5C372_REG_MONTH 5
30#define RS5C372_REG_YEAR 6
31#define RS5C372_REG_TRIM 7
32
33#define RS5C372_TRIM_XSL 0x80
34#define RS5C372_TRIM_MASK 0x7F
35
36#define RS5C372_REG_BASE 0
37
38static int rs5c372_attach(struct i2c_adapter *adapter);
39static int rs5c372_detach(struct i2c_client *client);
40static int rs5c372_probe(struct i2c_adapter *adapter, int address, int kind);
41
Riku Voipioc6f24f92006-12-06 20:39:13 -080042struct rs5c372 {
43 u8 reg_addr;
44 u8 regs[17];
45 struct i2c_msg msg[1];
46 struct i2c_client client;
47 struct rtc_device *rtc;
48};
49
Alessandro Zummo7520b942006-03-27 01:16:45 -080050static struct i2c_driver rs5c372_driver = {
51 .driver = {
52 .name = "rs5c372",
53 },
54 .attach_adapter = &rs5c372_attach,
55 .detach_client = &rs5c372_detach,
56};
57
58static int rs5c372_get_datetime(struct i2c_client *client, struct rtc_time *tm)
59{
Alessandro Zummo7520b942006-03-27 01:16:45 -080060
Riku Voipioc6f24f92006-12-06 20:39:13 -080061 struct rs5c372 *rs5c372 = i2c_get_clientdata(client);
62 u8 *buf = &(rs5c372->regs[1]);
63
64 /* this implements the 3rd reading method, according
65 * to the datasheet. rs5c372 defaults to internal
66 * address 0xF, so 0x0 is in regs[1]
Alessandro Zummo7520b942006-03-27 01:16:45 -080067 */
Alessandro Zummo7520b942006-03-27 01:16:45 -080068
Riku Voipioc6f24f92006-12-06 20:39:13 -080069 if ((i2c_transfer(client->adapter, rs5c372->msg, 1)) != 1) {
Alessandro Zummo7520b942006-03-27 01:16:45 -080070 dev_err(&client->dev, "%s: read error\n", __FUNCTION__);
71 return -EIO;
72 }
73
74 tm->tm_sec = BCD2BIN(buf[RS5C372_REG_SECS] & 0x7f);
75 tm->tm_min = BCD2BIN(buf[RS5C372_REG_MINS] & 0x7f);
76 tm->tm_hour = BCD2BIN(buf[RS5C372_REG_HOURS] & 0x3f);
77 tm->tm_wday = BCD2BIN(buf[RS5C372_REG_WDAY] & 0x07);
78 tm->tm_mday = BCD2BIN(buf[RS5C372_REG_DAY] & 0x3f);
79
80 /* tm->tm_mon is zero-based */
81 tm->tm_mon = BCD2BIN(buf[RS5C372_REG_MONTH] & 0x1f) - 1;
82
83 /* year is 1900 + tm->tm_year */
84 tm->tm_year = BCD2BIN(buf[RS5C372_REG_YEAR]) + 100;
85
86 dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
87 "mday=%d, mon=%d, year=%d, wday=%d\n",
88 __FUNCTION__,
89 tm->tm_sec, tm->tm_min, tm->tm_hour,
90 tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
91
92 return 0;
93}
94
95static int rs5c372_set_datetime(struct i2c_client *client, struct rtc_time *tm)
96{
97 unsigned char buf[8] = { RS5C372_REG_BASE };
98
99 dev_dbg(&client->dev,
Jeff Garzik11966ad2006-10-04 04:41:53 -0400100 "%s: secs=%d, mins=%d, hours=%d "
Alessandro Zummo7520b942006-03-27 01:16:45 -0800101 "mday=%d, mon=%d, year=%d, wday=%d\n",
102 __FUNCTION__, tm->tm_sec, tm->tm_min, tm->tm_hour,
103 tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
104
105 buf[1] = BIN2BCD(tm->tm_sec);
106 buf[2] = BIN2BCD(tm->tm_min);
107 buf[3] = BIN2BCD(tm->tm_hour);
108 buf[4] = BIN2BCD(tm->tm_wday);
109 buf[5] = BIN2BCD(tm->tm_mday);
110 buf[6] = BIN2BCD(tm->tm_mon + 1);
111 buf[7] = BIN2BCD(tm->tm_year - 100);
112
113 if ((i2c_master_send(client, buf, 8)) != 8) {
114 dev_err(&client->dev, "%s: write error\n", __FUNCTION__);
115 return -EIO;
116 }
117
118 return 0;
119}
120
121static int rs5c372_get_trim(struct i2c_client *client, int *osc, int *trim)
122{
Riku Voipioc6f24f92006-12-06 20:39:13 -0800123 struct rs5c372 *rs5c372 = i2c_get_clientdata(client);
124 u8 tmp = rs5c372->regs[RS5C372_REG_TRIM + 1];
Alessandro Zummo7520b942006-03-27 01:16:45 -0800125
Alessandro Zummo7520b942006-03-27 01:16:45 -0800126 if (osc)
Riku Voipioc6f24f92006-12-06 20:39:13 -0800127 *osc = (tmp & RS5C372_TRIM_XSL) ? 32000 : 32768;
Alessandro Zummo7520b942006-03-27 01:16:45 -0800128
Adrian Bunk17ad78e2006-11-25 11:09:29 -0800129 if (trim) {
Riku Voipioc6f24f92006-12-06 20:39:13 -0800130 *trim = tmp & RS5C372_TRIM_MASK;
Adrian Bunk17ad78e2006-11-25 11:09:29 -0800131 dev_dbg(&client->dev, "%s: raw trim=%x\n", __FUNCTION__, *trim);
132 }
Alessandro Zummo7520b942006-03-27 01:16:45 -0800133
134 return 0;
135}
136
137static int rs5c372_rtc_read_time(struct device *dev, struct rtc_time *tm)
138{
139 return rs5c372_get_datetime(to_i2c_client(dev), tm);
140}
141
142static int rs5c372_rtc_set_time(struct device *dev, struct rtc_time *tm)
143{
144 return rs5c372_set_datetime(to_i2c_client(dev), tm);
145}
146
147static int rs5c372_rtc_proc(struct device *dev, struct seq_file *seq)
148{
149 int err, osc, trim;
150
Alessandro Zummoadfb4342006-04-10 22:54:43 -0700151 err = rs5c372_get_trim(to_i2c_client(dev), &osc, &trim);
152 if (err == 0) {
Alessandro Zummo7520b942006-03-27 01:16:45 -0800153 seq_printf(seq, "%d.%03d KHz\n", osc / 1000, osc % 1000);
154 seq_printf(seq, "trim\t: %d\n", trim);
155 }
156
157 return 0;
158}
159
David Brownellff8371a2006-09-30 23:28:17 -0700160static const struct rtc_class_ops rs5c372_rtc_ops = {
Alessandro Zummo7520b942006-03-27 01:16:45 -0800161 .proc = rs5c372_rtc_proc,
162 .read_time = rs5c372_rtc_read_time,
163 .set_time = rs5c372_rtc_set_time,
164};
165
166static ssize_t rs5c372_sysfs_show_trim(struct device *dev,
167 struct device_attribute *attr, char *buf)
168{
Alessandro Zummo82896072006-04-10 22:54:44 -0700169 int err, trim;
Alessandro Zummo7520b942006-03-27 01:16:45 -0800170
Alessandro Zummo82896072006-04-10 22:54:44 -0700171 err = rs5c372_get_trim(to_i2c_client(dev), NULL, &trim);
172 if (err)
173 return err;
Alessandro Zummo7520b942006-03-27 01:16:45 -0800174
Alessandro Zummo82896072006-04-10 22:54:44 -0700175 return sprintf(buf, "0x%2x\n", trim);
Alessandro Zummo7520b942006-03-27 01:16:45 -0800176}
177static DEVICE_ATTR(trim, S_IRUGO, rs5c372_sysfs_show_trim, NULL);
178
179static ssize_t rs5c372_sysfs_show_osc(struct device *dev,
180 struct device_attribute *attr, char *buf)
181{
Alessandro Zummo82896072006-04-10 22:54:44 -0700182 int err, osc;
Alessandro Zummo7520b942006-03-27 01:16:45 -0800183
Alessandro Zummo82896072006-04-10 22:54:44 -0700184 err = rs5c372_get_trim(to_i2c_client(dev), &osc, NULL);
185 if (err)
186 return err;
Alessandro Zummo7520b942006-03-27 01:16:45 -0800187
Alessandro Zummo82896072006-04-10 22:54:44 -0700188 return sprintf(buf, "%d.%03d KHz\n", osc / 1000, osc % 1000);
Alessandro Zummo7520b942006-03-27 01:16:45 -0800189}
190static DEVICE_ATTR(osc, S_IRUGO, rs5c372_sysfs_show_osc, NULL);
191
192static int rs5c372_attach(struct i2c_adapter *adapter)
193{
Alessandro Zummo7520b942006-03-27 01:16:45 -0800194 return i2c_probe(adapter, &addr_data, rs5c372_probe);
195}
196
197static int rs5c372_probe(struct i2c_adapter *adapter, int address, int kind)
198{
199 int err = 0;
200 struct i2c_client *client;
Riku Voipioc6f24f92006-12-06 20:39:13 -0800201 struct rs5c372 *rs5c372;
Alessandro Zummo7520b942006-03-27 01:16:45 -0800202
David Brownella14e1892006-12-10 02:19:02 -0800203 dev_dbg(adapter->class_dev.dev, "%s\n", __FUNCTION__);
Alessandro Zummo7520b942006-03-27 01:16:45 -0800204
205 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
206 err = -ENODEV;
207 goto exit;
208 }
209
Riku Voipioc6f24f92006-12-06 20:39:13 -0800210 if (!(rs5c372 = kzalloc(sizeof(struct rs5c372), GFP_KERNEL))) {
Alessandro Zummo7520b942006-03-27 01:16:45 -0800211 err = -ENOMEM;
212 goto exit;
213 }
Riku Voipioc6f24f92006-12-06 20:39:13 -0800214 client = &rs5c372->client;
Alessandro Zummo7520b942006-03-27 01:16:45 -0800215
216 /* I2C client */
217 client->addr = address;
218 client->driver = &rs5c372_driver;
219 client->adapter = adapter;
220
221 strlcpy(client->name, rs5c372_driver.driver.name, I2C_NAME_SIZE);
222
Riku Voipioc6f24f92006-12-06 20:39:13 -0800223 i2c_set_clientdata(client, rs5c372);
224
225 rs5c372->msg[0].addr = address;
226 rs5c372->msg[0].flags = I2C_M_RD;
227 rs5c372->msg[0].len = sizeof(rs5c372->regs);
228 rs5c372->msg[0].buf = rs5c372->regs;
229
Alessandro Zummo7520b942006-03-27 01:16:45 -0800230 /* Inform the i2c layer */
231 if ((err = i2c_attach_client(client)))
232 goto exit_kfree;
233
234 dev_info(&client->dev, "chip found, driver version " DRV_VERSION "\n");
235
Riku Voipioc6f24f92006-12-06 20:39:13 -0800236 rs5c372->rtc = rtc_device_register(rs5c372_driver.driver.name,
237 &client->dev, &rs5c372_rtc_ops, THIS_MODULE);
Alessandro Zummo7520b942006-03-27 01:16:45 -0800238
Riku Voipioc6f24f92006-12-06 20:39:13 -0800239 if (IS_ERR(rs5c372->rtc)) {
240 err = PTR_ERR(rs5c372->rtc);
Alessandro Zummo7520b942006-03-27 01:16:45 -0800241 goto exit_detach;
242 }
243
Jeff Garzik91046a82006-12-06 20:35:34 -0800244 err = device_create_file(&client->dev, &dev_attr_trim);
Riku Voipioc6f24f92006-12-06 20:39:13 -0800245 if (err)
246 goto exit_devreg;
Jeff Garzik91046a82006-12-06 20:35:34 -0800247 err = device_create_file(&client->dev, &dev_attr_osc);
Riku Voipioc6f24f92006-12-06 20:39:13 -0800248 if (err)
249 goto exit_trim;
Alessandro Zummo7520b942006-03-27 01:16:45 -0800250
251 return 0;
252
Jeff Garzik91046a82006-12-06 20:35:34 -0800253exit_trim:
254 device_remove_file(&client->dev, &dev_attr_trim);
255
256exit_devreg:
Riku Voipioc6f24f92006-12-06 20:39:13 -0800257 rtc_device_unregister(rs5c372->rtc);
Jeff Garzik91046a82006-12-06 20:35:34 -0800258
Alessandro Zummo7520b942006-03-27 01:16:45 -0800259exit_detach:
260 i2c_detach_client(client);
261
262exit_kfree:
Riku Voipioc6f24f92006-12-06 20:39:13 -0800263 kfree(rs5c372);
Alessandro Zummo7520b942006-03-27 01:16:45 -0800264
265exit:
266 return err;
267}
268
269static int rs5c372_detach(struct i2c_client *client)
270{
271 int err;
Riku Voipioc6f24f92006-12-06 20:39:13 -0800272 struct rs5c372 *rs5c372 = i2c_get_clientdata(client);
Alessandro Zummo7520b942006-03-27 01:16:45 -0800273
Riku Voipioc6f24f92006-12-06 20:39:13 -0800274 if (rs5c372->rtc)
275 rtc_device_unregister(rs5c372->rtc);
Alessandro Zummo7520b942006-03-27 01:16:45 -0800276
277 if ((err = i2c_detach_client(client)))
278 return err;
279
Riku Voipioc6f24f92006-12-06 20:39:13 -0800280 kfree(rs5c372);
Alessandro Zummo7520b942006-03-27 01:16:45 -0800281 return 0;
282}
283
284static __init int rs5c372_init(void)
285{
286 return i2c_add_driver(&rs5c372_driver);
287}
288
289static __exit void rs5c372_exit(void)
290{
291 i2c_del_driver(&rs5c372_driver);
292}
293
294module_init(rs5c372_init);
295module_exit(rs5c372_exit);
296
297MODULE_AUTHOR(
298 "Pavel Mironchik <pmironchik@optifacio.net>, "
299 "Alessandro Zummo <a.zummo@towertech.it>");
300MODULE_DESCRIPTION("Ricoh RS5C372 RTC driver");
301MODULE_LICENSE("GPL");
302MODULE_VERSION(DRV_VERSION);