ACPI: ibm-acpi: implement fan watchdog command
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / rtc / rtc-rs5c372.c
blob2a86632580f16e2cc0d010fb938af67c6df81be8
1 /*
2 * An I2C driver for the Ricoh RS5C372 RTC
4 * Copyright (C) 2005 Pavel Mironchik <pmironchik@optifacio.net>
5 * Copyright (C) 2006 Tower Technologies
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.
12 #include <linux/i2c.h>
13 #include <linux/rtc.h>
14 #include <linux/bcd.h>
16 #define DRV_VERSION "0.2"
18 /* Addresses to scan */
19 static unsigned short normal_i2c[] = { /* 0x32,*/ I2C_CLIENT_END };
21 /* Insmod parameters */
22 I2C_CLIENT_INSMOD;
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
33 #define RS5C372_TRIM_XSL 0x80
34 #define RS5C372_TRIM_MASK 0x7F
36 #define RS5C372_REG_BASE 0
38 static int rs5c372_attach(struct i2c_adapter *adapter);
39 static int rs5c372_detach(struct i2c_client *client);
40 static int rs5c372_probe(struct i2c_adapter *adapter, int address, int kind);
42 static struct i2c_driver rs5c372_driver = {
43 .driver = {
44 .name = "rs5c372",
46 .attach_adapter = &rs5c372_attach,
47 .detach_client = &rs5c372_detach,
50 static int rs5c372_get_datetime(struct i2c_client *client, struct rtc_time *tm)
52 unsigned char buf[7] = { RS5C372_REG_BASE };
54 /* this implements the 1st reading method, according
55 * to the datasheet. buf[0] is initialized with
56 * address ptr and transmission format register.
58 struct i2c_msg msgs[] = {
59 { client->addr, 0, 1, buf },
60 { client->addr, I2C_M_RD, 7, buf },
63 if ((i2c_transfer(client->adapter, msgs, 2)) != 2) {
64 dev_err(&client->dev, "%s: read error\n", __FUNCTION__);
65 return -EIO;
68 tm->tm_sec = BCD2BIN(buf[RS5C372_REG_SECS] & 0x7f);
69 tm->tm_min = BCD2BIN(buf[RS5C372_REG_MINS] & 0x7f);
70 tm->tm_hour = BCD2BIN(buf[RS5C372_REG_HOURS] & 0x3f);
71 tm->tm_wday = BCD2BIN(buf[RS5C372_REG_WDAY] & 0x07);
72 tm->tm_mday = BCD2BIN(buf[RS5C372_REG_DAY] & 0x3f);
74 /* tm->tm_mon is zero-based */
75 tm->tm_mon = BCD2BIN(buf[RS5C372_REG_MONTH] & 0x1f) - 1;
77 /* year is 1900 + tm->tm_year */
78 tm->tm_year = BCD2BIN(buf[RS5C372_REG_YEAR]) + 100;
80 dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
81 "mday=%d, mon=%d, year=%d, wday=%d\n",
82 __FUNCTION__,
83 tm->tm_sec, tm->tm_min, tm->tm_hour,
84 tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
86 return 0;
89 static int rs5c372_set_datetime(struct i2c_client *client, struct rtc_time *tm)
91 unsigned char buf[8] = { RS5C372_REG_BASE };
93 dev_dbg(&client->dev,
94 "%s: secs=%d, mins=%d, hours=%d "
95 "mday=%d, mon=%d, year=%d, wday=%d\n",
96 __FUNCTION__, tm->tm_sec, tm->tm_min, tm->tm_hour,
97 tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
99 buf[1] = BIN2BCD(tm->tm_sec);
100 buf[2] = BIN2BCD(tm->tm_min);
101 buf[3] = BIN2BCD(tm->tm_hour);
102 buf[4] = BIN2BCD(tm->tm_wday);
103 buf[5] = BIN2BCD(tm->tm_mday);
104 buf[6] = BIN2BCD(tm->tm_mon + 1);
105 buf[7] = BIN2BCD(tm->tm_year - 100);
107 if ((i2c_master_send(client, buf, 8)) != 8) {
108 dev_err(&client->dev, "%s: write error\n", __FUNCTION__);
109 return -EIO;
112 return 0;
115 static int rs5c372_get_trim(struct i2c_client *client, int *osc, int *trim)
117 unsigned char buf = RS5C372_REG_TRIM;
119 struct i2c_msg msgs[] = {
120 { client->addr, 0, 1, &buf },
121 { client->addr, I2C_M_RD, 1, &buf },
124 if ((i2c_transfer(client->adapter, msgs, 2)) != 2) {
125 dev_err(&client->dev, "%s: read error\n", __FUNCTION__);
126 return -EIO;
129 dev_dbg(&client->dev, "%s: raw trim=%x\n", __FUNCTION__, *trim);
131 if (osc)
132 *osc = (buf & RS5C372_TRIM_XSL) ? 32000 : 32768;
134 if (trim)
135 *trim = buf & RS5C372_TRIM_MASK;
137 return 0;
140 static int rs5c372_rtc_read_time(struct device *dev, struct rtc_time *tm)
142 return rs5c372_get_datetime(to_i2c_client(dev), tm);
145 static int rs5c372_rtc_set_time(struct device *dev, struct rtc_time *tm)
147 return rs5c372_set_datetime(to_i2c_client(dev), tm);
150 static int rs5c372_rtc_proc(struct device *dev, struct seq_file *seq)
152 int err, osc, trim;
154 err = rs5c372_get_trim(to_i2c_client(dev), &osc, &trim);
155 if (err == 0) {
156 seq_printf(seq, "%d.%03d KHz\n", osc / 1000, osc % 1000);
157 seq_printf(seq, "trim\t: %d\n", trim);
160 return 0;
163 static const struct rtc_class_ops rs5c372_rtc_ops = {
164 .proc = rs5c372_rtc_proc,
165 .read_time = rs5c372_rtc_read_time,
166 .set_time = rs5c372_rtc_set_time,
169 static ssize_t rs5c372_sysfs_show_trim(struct device *dev,
170 struct device_attribute *attr, char *buf)
172 int err, trim;
174 err = rs5c372_get_trim(to_i2c_client(dev), NULL, &trim);
175 if (err)
176 return err;
178 return sprintf(buf, "0x%2x\n", trim);
180 static DEVICE_ATTR(trim, S_IRUGO, rs5c372_sysfs_show_trim, NULL);
182 static ssize_t rs5c372_sysfs_show_osc(struct device *dev,
183 struct device_attribute *attr, char *buf)
185 int err, osc;
187 err = rs5c372_get_trim(to_i2c_client(dev), &osc, NULL);
188 if (err)
189 return err;
191 return sprintf(buf, "%d.%03d KHz\n", osc / 1000, osc % 1000);
193 static DEVICE_ATTR(osc, S_IRUGO, rs5c372_sysfs_show_osc, NULL);
195 static int rs5c372_attach(struct i2c_adapter *adapter)
197 return i2c_probe(adapter, &addr_data, rs5c372_probe);
200 static int rs5c372_probe(struct i2c_adapter *adapter, int address, int kind)
202 int err = 0;
203 struct i2c_client *client;
204 struct rtc_device *rtc;
206 dev_dbg(&adapter->dev, "%s\n", __FUNCTION__);
208 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
209 err = -ENODEV;
210 goto exit;
213 if (!(client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL))) {
214 err = -ENOMEM;
215 goto exit;
218 /* I2C client */
219 client->addr = address;
220 client->driver = &rs5c372_driver;
221 client->adapter = adapter;
223 strlcpy(client->name, rs5c372_driver.driver.name, I2C_NAME_SIZE);
225 /* Inform the i2c layer */
226 if ((err = i2c_attach_client(client)))
227 goto exit_kfree;
229 dev_info(&client->dev, "chip found, driver version " DRV_VERSION "\n");
231 rtc = rtc_device_register(rs5c372_driver.driver.name, &client->dev,
232 &rs5c372_rtc_ops, THIS_MODULE);
234 if (IS_ERR(rtc)) {
235 err = PTR_ERR(rtc);
236 goto exit_detach;
239 i2c_set_clientdata(client, rtc);
241 device_create_file(&client->dev, &dev_attr_trim);
242 device_create_file(&client->dev, &dev_attr_osc);
244 return 0;
246 exit_detach:
247 i2c_detach_client(client);
249 exit_kfree:
250 kfree(client);
252 exit:
253 return err;
256 static int rs5c372_detach(struct i2c_client *client)
258 int err;
259 struct rtc_device *rtc = i2c_get_clientdata(client);
261 if (rtc)
262 rtc_device_unregister(rtc);
264 if ((err = i2c_detach_client(client)))
265 return err;
267 kfree(client);
269 return 0;
272 static __init int rs5c372_init(void)
274 return i2c_add_driver(&rs5c372_driver);
277 static __exit void rs5c372_exit(void)
279 i2c_del_driver(&rs5c372_driver);
282 module_init(rs5c372_init);
283 module_exit(rs5c372_exit);
285 MODULE_AUTHOR(
286 "Pavel Mironchik <pmironchik@optifacio.net>, "
287 "Alessandro Zummo <a.zummo@towertech.it>");
288 MODULE_DESCRIPTION("Ricoh RS5C372 RTC driver");
289 MODULE_LICENSE("GPL");
290 MODULE_VERSION(DRV_VERSION);