blob: eb2be3e4416c37623eeb1680b487871957f19b18 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * I2C client/driver for the ST M41T00 Real-Time Clock chip.
3 *
4 * Author: Mark A. Greer <mgreer@mvista.com>
5 *
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
9 * or implied.
10 */
11/*
12 * This i2c client/driver wedges between the drivers/char/genrtc.c RTC
13 * interface and the SMBus interface of the i2c subsystem.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 */
15
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>
Arjan van de Venb3585e42006-01-11 10:50:26 +010022#include <linux/mutex.h>
Mark A. Greer8c750c02006-03-31 23:06:03 +020023#include <linux/workqueue.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#include <asm/time.h>
26#include <asm/rtc.h>
27
28#define M41T00_DRV_NAME "m41t00"
29
Arjan van de Venb3585e42006-01-11 10:50:26 +010030static DEFINE_MUTEX(m41t00_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32static struct i2c_driver m41t00_driver;
33static struct i2c_client *save_client;
34
35static unsigned short ignore[] = { I2C_CLIENT_END };
36static unsigned short normal_addr[] = { 0x68, I2C_CLIENT_END };
37
38static struct i2c_client_address_data addr_data = {
Mark A. Greere931b8d2006-03-31 23:06:46 +020039 .normal_i2c = normal_addr,
40 .probe = ignore,
41 .ignore = ignore,
Linus Torvalds1da177e2005-04-16 15:20:36 -070042};
43
44ulong
45m41t00_get_rtc_time(void)
46{
Mark A. Greere931b8d2006-03-31 23:06:46 +020047 s32 sec, min, hour, day, mon, year;
48 s32 sec1, min1, hour1, day1, mon1, year1;
49 ulong limit = 10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51 sec = min = hour = day = mon = year = 0;
52 sec1 = min1 = hour1 = day1 = mon1 = year1 = 0;
53
Arjan van de Venb3585e42006-01-11 10:50:26 +010054 mutex_lock(&m41t00_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 do {
56 if (((sec = i2c_smbus_read_byte_data(save_client, 0)) >= 0)
57 && ((min = i2c_smbus_read_byte_data(save_client, 1))
58 >= 0)
59 && ((hour = i2c_smbus_read_byte_data(save_client, 2))
60 >= 0)
61 && ((day = i2c_smbus_read_byte_data(save_client, 4))
62 >= 0)
63 && ((mon = i2c_smbus_read_byte_data(save_client, 5))
64 >= 0)
65 && ((year = i2c_smbus_read_byte_data(save_client, 6))
66 >= 0)
67 && ((sec == sec1) && (min == min1) && (hour == hour1)
68 && (day == day1) && (mon == mon1)
69 && (year == year1)))
70
71 break;
72
73 sec1 = sec;
74 min1 = min;
75 hour1 = hour;
76 day1 = day;
77 mon1 = mon;
78 year1 = year;
79 } while (--limit > 0);
Arjan van de Venb3585e42006-01-11 10:50:26 +010080 mutex_unlock(&m41t00_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82 if (limit == 0) {
83 dev_warn(&save_client->dev,
84 "m41t00: can't read rtc chip\n");
85 sec = min = hour = day = mon = year = 0;
86 }
87
88 sec &= 0x7f;
89 min &= 0x7f;
90 hour &= 0x3f;
91 day &= 0x3f;
92 mon &= 0x1f;
93 year &= 0xff;
94
Mark A. Greere931b8d2006-03-31 23:06:46 +020095 sec = BCD2BIN(sec);
96 min = BCD2BIN(min);
97 hour = BCD2BIN(hour);
98 day = BCD2BIN(day);
99 mon = BCD2BIN(mon);
100 year = BCD2BIN(year);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102 year += 1900;
103 if (year < 1970)
104 year += 100;
105
106 return mktime(year, mon, day, hour, min, sec);
107}
108
109static void
Mark A. Greer8c750c02006-03-31 23:06:03 +0200110m41t00_set(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
112 struct rtc_time tm;
Mark A. Greere931b8d2006-03-31 23:06:46 +0200113 ulong nowtime = *(ulong *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115 to_tm(nowtime, &tm);
116 tm.tm_year = (tm.tm_year - 1900) % 100;
117
Mark A. Greere931b8d2006-03-31 23:06:46 +0200118 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Arjan van de Venb3585e42006-01-11 10:50:26 +0100125 mutex_lock(&m41t00_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 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)
128 < 0)
David Barksdale8db08de2006-04-18 22:20:27 -0700129 || (i2c_smbus_write_byte_data(save_client, 2, tm.tm_hour & 0x3f)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 < 0)
David Barksdale8db08de2006-04-18 22:20:27 -0700131 || (i2c_smbus_write_byte_data(save_client, 4, tm.tm_mday & 0x3f)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 < 0)
David Barksdale8db08de2006-04-18 22:20:27 -0700133 || (i2c_smbus_write_byte_data(save_client, 5, tm.tm_mon & 0x1f)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 < 0)
David Barksdale8db08de2006-04-18 22:20:27 -0700135 || (i2c_smbus_write_byte_data(save_client, 6, tm.tm_year & 0xff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 < 0))
137
138 dev_warn(&save_client->dev,"m41t00: can't write to rtc chip\n");
139
Arjan van de Venb3585e42006-01-11 10:50:26 +0100140 mutex_unlock(&m41t00_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
142
Mark A. Greer8c750c02006-03-31 23:06:03 +0200143static ulong new_time;
144static struct workqueue_struct *m41t00_wq;
145static DECLARE_WORK(m41t00_work, m41t00_set, &new_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147int
148m41t00_set_rtc_time(ulong nowtime)
149{
150 new_time = nowtime;
151
152 if (in_interrupt())
Mark A. Greer8c750c02006-03-31 23:06:03 +0200153 queue_work(m41t00_wq, &m41t00_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 else
Mark A. Greer8c750c02006-03-31 23:06:03 +0200155 m41t00_set(&new_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157 return 0;
158}
159
160/*
161 *****************************************************************************
162 *
163 * Driver Interface
164 *
165 *****************************************************************************
166 */
167static int
168m41t00_probe(struct i2c_adapter *adap, int addr, int kind)
169{
170 struct i2c_client *client;
171 int rc;
172
Deepak Saxena5263ebb2005-10-17 23:09:43 +0200173 client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 if (!client)
175 return -ENOMEM;
176
Mark A. Greere931b8d2006-03-31 23:06:46 +0200177 strlcpy(client->name, M41T00_DRV_NAME, I2C_NAME_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 client->addr = addr;
179 client->adapter = adap;
180 client->driver = &m41t00_driver;
181
182 if ((rc = i2c_attach_client(client)) != 0) {
183 kfree(client);
184 return rc;
185 }
186
Mark A. Greer8c750c02006-03-31 23:06:03 +0200187 m41t00_wq = create_singlethread_workqueue("m41t00");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 save_client = client;
189 return 0;
190}
191
192static int
193m41t00_attach(struct i2c_adapter *adap)
194{
195 return i2c_probe(adap, &addr_data, m41t00_probe);
196}
197
198static int
199m41t00_detach(struct i2c_client *client)
200{
Mark A. Greere931b8d2006-03-31 23:06:46 +0200201 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203 if ((rc = i2c_detach_client(client)) == 0) {
Jean Delvare5da69ba2005-07-01 14:28:15 +0200204 kfree(client);
Mark A. Greer8c750c02006-03-31 23:06:03 +0200205 destroy_workqueue(m41t00_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 }
207 return rc;
208}
209
210static struct i2c_driver m41t00_driver = {
Laurent Riffarda9718b02005-11-26 20:36:00 +0100211 .driver = {
Laurent Riffarda9718b02005-11-26 20:36:00 +0100212 .name = M41T00_DRV_NAME,
213 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 .id = I2C_DRIVERID_STM41T00,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 .attach_adapter = m41t00_attach,
216 .detach_client = m41t00_detach,
217};
218
219static int __init
220m41t00_init(void)
221{
222 return i2c_add_driver(&m41t00_driver);
223}
224
225static void __exit
226m41t00_exit(void)
227{
228 i2c_del_driver(&m41t00_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229}
230
231module_init(m41t00_init);
232module_exit(m41t00_exit);
233
234MODULE_AUTHOR("Mark A. Greer <mgreer@mvista.com>");
235MODULE_DESCRIPTION("ST Microelectronics M41T00 RTC I2C Client Driver");
236MODULE_LICENSE("GPL");