2 * I2C client/driver for the ST M41T00 Real-Time Clock chip.
4 * Author: Mark A. Greer <mgreer@mvista.com>
6 * 2005 (c) MontaVista Software, Inc. This file is licensed under
7 * the terms of the GNU General Public License version 2. This program
8 * is licensed "as is" without any warranty of any kind, whether express
12 * This i2c client/driver wedges between the drivers/char/genrtc.c RTC
13 * interface and the SMBus interface of the i2c subsystem.
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/interrupt.h>
19 #include <linux/i2c.h>
20 #include <linux/rtc.h>
21 #include <linux/bcd.h>
22 #include <linux/mutex.h>
23 #include <linux/workqueue.h>
28 #define M41T00_DRV_NAME "m41t00"
30 static DEFINE_MUTEX(m41t00_mutex);
32 static struct i2c_driver m41t00_driver;
33 static struct i2c_client *save_client;
35 static unsigned short ignore[] = { I2C_CLIENT_END };
36 static unsigned short normal_addr[] = { 0x68, I2C_CLIENT_END };
38 static struct i2c_client_address_data addr_data = {
39 .normal_i2c = normal_addr,
45 m41t00_get_rtc_time(void)
47 s32 sec, min, hour, day, mon, year;
48 s32 sec1, min1, hour1, day1, mon1, year1;
51 sec = min = hour = day = mon = year = 0;
52 sec1 = min1 = hour1 = day1 = mon1 = year1 = 0;
54 mutex_lock(&m41t00_mutex);
56 if (((sec = i2c_smbus_read_byte_data(save_client, 0)) >= 0)
57 && ((min = i2c_smbus_read_byte_data(save_client, 1))
59 && ((hour = i2c_smbus_read_byte_data(save_client, 2))
61 && ((day = i2c_smbus_read_byte_data(save_client, 4))
63 && ((mon = i2c_smbus_read_byte_data(save_client, 5))
65 && ((year = i2c_smbus_read_byte_data(save_client, 6))
67 && ((sec == sec1) && (min == min1) && (hour == hour1)
68 && (day == day1) && (mon == mon1)
79 } while (--limit > 0);
80 mutex_unlock(&m41t00_mutex);
83 dev_warn(&save_client->dev,
84 "m41t00: can't read rtc chip\n");
85 sec = min = hour = day = mon = year = 0;
100 year = BCD2BIN(year);
106 return mktime(year, mon, day, hour, min, sec);
110 m41t00_set(void *arg)
113 ulong nowtime = *(ulong *)arg;
116 tm.tm_year = (tm.tm_year - 1900) % 100;
118 tm.tm_sec = BIN2BCD(tm.tm_sec);
119 tm.tm_min = BIN2BCD(tm.tm_min);
120 tm.tm_hour = BIN2BCD(tm.tm_hour);
121 tm.tm_mon = BIN2BCD(tm.tm_mon);
122 tm.tm_mday = BIN2BCD(tm.tm_mday);
123 tm.tm_year = BIN2BCD(tm.tm_year);
125 mutex_lock(&m41t00_mutex);
126 if ((i2c_smbus_write_byte_data(save_client, 0, tm.tm_sec & 0x7f) < 0)
127 || (i2c_smbus_write_byte_data(save_client, 1, tm.tm_min & 0x7f)
129 || (i2c_smbus_write_byte_data(save_client, 2, tm.tm_hour & 0x3f)
131 || (i2c_smbus_write_byte_data(save_client, 4, tm.tm_mday & 0x3f)
133 || (i2c_smbus_write_byte_data(save_client, 5, tm.tm_mon & 0x1f)
135 || (i2c_smbus_write_byte_data(save_client, 6, tm.tm_year & 0xff)
138 dev_warn(&save_client->dev,"m41t00: can't write to rtc chip\n");
140 mutex_unlock(&m41t00_mutex);
143 static ulong new_time;
144 static struct workqueue_struct *m41t00_wq;
145 static DECLARE_WORK(m41t00_work, m41t00_set, &new_time);
148 m41t00_set_rtc_time(ulong nowtime)
153 queue_work(m41t00_wq, &m41t00_work);
155 m41t00_set(&new_time);
161 *****************************************************************************
165 *****************************************************************************
168 m41t00_probe(struct i2c_adapter *adap, int addr, int kind)
170 struct i2c_client *client;
173 client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
177 strlcpy(client->name, M41T00_DRV_NAME, I2C_NAME_SIZE);
179 client->adapter = adap;
180 client->driver = &m41t00_driver;
182 if ((rc = i2c_attach_client(client)) != 0) {
187 m41t00_wq = create_singlethread_workqueue("m41t00");
188 save_client = client;
193 m41t00_attach(struct i2c_adapter *adap)
195 return i2c_probe(adap, &addr_data, m41t00_probe);
199 m41t00_detach(struct i2c_client *client)
203 if ((rc = i2c_detach_client(client)) == 0) {
205 destroy_workqueue(m41t00_wq);
210 static struct i2c_driver m41t00_driver = {
212 .name = M41T00_DRV_NAME,
214 .id = I2C_DRIVERID_STM41T00,
215 .attach_adapter = m41t00_attach,
216 .detach_client = m41t00_detach,
222 return i2c_add_driver(&m41t00_driver);
228 i2c_del_driver(&m41t00_driver);
231 module_init(m41t00_init);
232 module_exit(m41t00_exit);
234 MODULE_AUTHOR("Mark A. Greer <mgreer@mvista.com>");
235 MODULE_DESCRIPTION("ST Microelectronics M41T00 RTC I2C Client Driver");
236 MODULE_LICENSE("GPL");