2 * drivers/i2c/chips/m41t00.c
4 * I2C client/driver for the ST M41T00 Real-Time Clock chip.
6 * Author: Mark A. Greer <mgreer@mvista.com>
8 * 2005 (c) MontaVista Software, Inc. This file is licensed under
9 * the terms of the GNU General Public License version 2. This program
10 * is licensed "as is" without any warranty of any kind, whether express
14 * This i2c client/driver wedges between the drivers/char/genrtc.c RTC
15 * interface and the SMBus interface of the i2c subsystem.
16 * It would be more efficient to use i2c msgs/i2c_transfer directly but, as
17 * recommened in .../Documentation/i2c/writing-clients section
18 * "Sending and receiving", using SMBus level communication is preferred.
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/interrupt.h>
24 #include <linux/i2c.h>
25 #include <linux/rtc.h>
26 #include <linux/bcd.h>
27 #include <linux/mutex.h>
28 #include <linux/workqueue.h>
33 #define M41T00_DRV_NAME "m41t00"
35 static DEFINE_MUTEX(m41t00_mutex);
37 static struct i2c_driver m41t00_driver;
38 static struct i2c_client *save_client;
40 static unsigned short ignore[] = { I2C_CLIENT_END };
41 static unsigned short normal_addr[] = { 0x68, I2C_CLIENT_END };
43 static struct i2c_client_address_data addr_data = {
44 .normal_i2c = normal_addr,
50 m41t00_get_rtc_time(void)
52 s32 sec, min, hour, day, mon, year;
53 s32 sec1, min1, hour1, day1, mon1, year1;
56 sec = min = hour = day = mon = year = 0;
57 sec1 = min1 = hour1 = day1 = mon1 = year1 = 0;
59 mutex_lock(&m41t00_mutex);
61 if (((sec = i2c_smbus_read_byte_data(save_client, 0)) >= 0)
62 && ((min = i2c_smbus_read_byte_data(save_client, 1))
64 && ((hour = i2c_smbus_read_byte_data(save_client, 2))
66 && ((day = i2c_smbus_read_byte_data(save_client, 4))
68 && ((mon = i2c_smbus_read_byte_data(save_client, 5))
70 && ((year = i2c_smbus_read_byte_data(save_client, 6))
72 && ((sec == sec1) && (min == min1) && (hour == hour1)
73 && (day == day1) && (mon == mon1)
84 } while (--limit > 0);
85 mutex_unlock(&m41t00_mutex);
88 dev_warn(&save_client->dev,
89 "m41t00: can't read rtc chip\n");
90 sec = min = hour = day = mon = year = 0;
111 return mktime(year, mon, day, hour, min, sec);
115 m41t00_set(void *arg)
118 ulong nowtime = *(ulong *)arg;
121 tm.tm_year = (tm.tm_year - 1900) % 100;
123 BIN_TO_BCD(tm.tm_sec);
124 BIN_TO_BCD(tm.tm_min);
125 BIN_TO_BCD(tm.tm_hour);
126 BIN_TO_BCD(tm.tm_mon);
127 BIN_TO_BCD(tm.tm_mday);
128 BIN_TO_BCD(tm.tm_year);
130 mutex_lock(&m41t00_mutex);
131 if ((i2c_smbus_write_byte_data(save_client, 0, tm.tm_sec & 0x7f) < 0)
132 || (i2c_smbus_write_byte_data(save_client, 1, tm.tm_min & 0x7f)
134 || (i2c_smbus_write_byte_data(save_client, 2, tm.tm_hour & 0x3f)
136 || (i2c_smbus_write_byte_data(save_client, 4, tm.tm_mday & 0x3f)
138 || (i2c_smbus_write_byte_data(save_client, 5, tm.tm_mon & 0x1f)
140 || (i2c_smbus_write_byte_data(save_client, 6, tm.tm_year & 0xff)
143 dev_warn(&save_client->dev,"m41t00: can't write to rtc chip\n");
145 mutex_unlock(&m41t00_mutex);
149 static ulong new_time;
150 static struct workqueue_struct *m41t00_wq;
151 static DECLARE_WORK(m41t00_work, m41t00_set, &new_time);
154 m41t00_set_rtc_time(ulong nowtime)
159 queue_work(m41t00_wq, &m41t00_work);
161 m41t00_set(&new_time);
167 *****************************************************************************
171 *****************************************************************************
174 m41t00_probe(struct i2c_adapter *adap, int addr, int kind)
176 struct i2c_client *client;
179 client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
183 strncpy(client->name, M41T00_DRV_NAME, I2C_NAME_SIZE);
185 client->adapter = adap;
186 client->driver = &m41t00_driver;
188 if ((rc = i2c_attach_client(client)) != 0) {
193 m41t00_wq = create_singlethread_workqueue("m41t00");
194 save_client = client;
199 m41t00_attach(struct i2c_adapter *adap)
201 return i2c_probe(adap, &addr_data, m41t00_probe);
205 m41t00_detach(struct i2c_client *client)
209 if ((rc = i2c_detach_client(client)) == 0) {
211 destroy_workqueue(m41t00_wq);
216 static struct i2c_driver m41t00_driver = {
218 .name = M41T00_DRV_NAME,
220 .id = I2C_DRIVERID_STM41T00,
221 .attach_adapter = m41t00_attach,
222 .detach_client = m41t00_detach,
228 return i2c_add_driver(&m41t00_driver);
234 i2c_del_driver(&m41t00_driver);
238 module_init(m41t00_init);
239 module_exit(m41t00_exit);
241 MODULE_AUTHOR("Mark A. Greer <mgreer@mvista.com>");
242 MODULE_DESCRIPTION("ST Microelectronics M41T00 RTC I2C Client Driver");
243 MODULE_LICENSE("GPL");