blob: 2dc3d48375fca829f58a9e63328735cdc2583db9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/i2c/chips/m41t00.c
3 *
4 * I2C client/driver for the ST M41T00 Real-Time Clock chip.
5 *
6 * Author: Mark A. Greer <mgreer@mvista.com>
7 *
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
11 * or implied.
12 */
13/*
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.
19 */
20
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
28#include <asm/time.h>
29#include <asm/rtc.h>
30
31#define M41T00_DRV_NAME "m41t00"
32
33static DECLARE_MUTEX(m41t00_mutex);
34
35static struct i2c_driver m41t00_driver;
36static struct i2c_client *save_client;
37
38static unsigned short ignore[] = { I2C_CLIENT_END };
39static unsigned short normal_addr[] = { 0x68, I2C_CLIENT_END };
40
41static struct i2c_client_address_data addr_data = {
42 .normal_i2c = normal_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 .probe = ignore,
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 .ignore = ignore,
Linus Torvalds1da177e2005-04-16 15:20:36 -070045};
46
47ulong
48m41t00_get_rtc_time(void)
49{
50 s32 sec, min, hour, day, mon, year;
51 s32 sec1, min1, hour1, day1, mon1, year1;
52 ulong limit = 10;
53
54 sec = min = hour = day = mon = year = 0;
55 sec1 = min1 = hour1 = day1 = mon1 = year1 = 0;
56
57 down(&m41t00_mutex);
58 do {
59 if (((sec = i2c_smbus_read_byte_data(save_client, 0)) >= 0)
60 && ((min = i2c_smbus_read_byte_data(save_client, 1))
61 >= 0)
62 && ((hour = i2c_smbus_read_byte_data(save_client, 2))
63 >= 0)
64 && ((day = i2c_smbus_read_byte_data(save_client, 4))
65 >= 0)
66 && ((mon = i2c_smbus_read_byte_data(save_client, 5))
67 >= 0)
68 && ((year = i2c_smbus_read_byte_data(save_client, 6))
69 >= 0)
70 && ((sec == sec1) && (min == min1) && (hour == hour1)
71 && (day == day1) && (mon == mon1)
72 && (year == year1)))
73
74 break;
75
76 sec1 = sec;
77 min1 = min;
78 hour1 = hour;
79 day1 = day;
80 mon1 = mon;
81 year1 = year;
82 } while (--limit > 0);
83 up(&m41t00_mutex);
84
85 if (limit == 0) {
86 dev_warn(&save_client->dev,
87 "m41t00: can't read rtc chip\n");
88 sec = min = hour = day = mon = year = 0;
89 }
90
91 sec &= 0x7f;
92 min &= 0x7f;
93 hour &= 0x3f;
94 day &= 0x3f;
95 mon &= 0x1f;
96 year &= 0xff;
97
98 BCD_TO_BIN(sec);
99 BCD_TO_BIN(min);
100 BCD_TO_BIN(hour);
101 BCD_TO_BIN(day);
102 BCD_TO_BIN(mon);
103 BCD_TO_BIN(year);
104
105 year += 1900;
106 if (year < 1970)
107 year += 100;
108
109 return mktime(year, mon, day, hour, min, sec);
110}
111
112static void
113m41t00_set_tlet(ulong arg)
114{
115 struct rtc_time tm;
116 ulong nowtime = *(ulong *)arg;
117
118 to_tm(nowtime, &tm);
119 tm.tm_year = (tm.tm_year - 1900) % 100;
120
121 BIN_TO_BCD(tm.tm_sec);
122 BIN_TO_BCD(tm.tm_min);
123 BIN_TO_BCD(tm.tm_hour);
124 BIN_TO_BCD(tm.tm_mon);
125 BIN_TO_BCD(tm.tm_mday);
126 BIN_TO_BCD(tm.tm_year);
127
128 down(&m41t00_mutex);
129 if ((i2c_smbus_write_byte_data(save_client, 0, tm.tm_sec & 0x7f) < 0)
130 || (i2c_smbus_write_byte_data(save_client, 1, tm.tm_min & 0x7f)
131 < 0)
132 || (i2c_smbus_write_byte_data(save_client, 2, tm.tm_hour & 0x7f)
133 < 0)
134 || (i2c_smbus_write_byte_data(save_client, 4, tm.tm_mday & 0x7f)
135 < 0)
136 || (i2c_smbus_write_byte_data(save_client, 5, tm.tm_mon & 0x7f)
137 < 0)
138 || (i2c_smbus_write_byte_data(save_client, 6, tm.tm_year & 0x7f)
139 < 0))
140
141 dev_warn(&save_client->dev,"m41t00: can't write to rtc chip\n");
142
143 up(&m41t00_mutex);
144 return;
145}
146
Mark A. Greera44e40b2005-09-01 18:09:54 -0700147static ulong new_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149DECLARE_TASKLET_DISABLED(m41t00_tasklet, m41t00_set_tlet, (ulong)&new_time);
150
151int
152m41t00_set_rtc_time(ulong nowtime)
153{
154 new_time = nowtime;
155
156 if (in_interrupt())
157 tasklet_schedule(&m41t00_tasklet);
158 else
159 m41t00_set_tlet((ulong)&new_time);
160
161 return 0;
162}
163
164/*
165 *****************************************************************************
166 *
167 * Driver Interface
168 *
169 *****************************************************************************
170 */
171static int
172m41t00_probe(struct i2c_adapter *adap, int addr, int kind)
173{
174 struct i2c_client *client;
175 int rc;
176
Deepak Saxena5263ebb2005-10-17 23:09:43 +0200177 client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 if (!client)
179 return -ENOMEM;
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 strncpy(client->name, M41T00_DRV_NAME, I2C_NAME_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 client->addr = addr;
183 client->adapter = adap;
184 client->driver = &m41t00_driver;
185
186 if ((rc = i2c_attach_client(client)) != 0) {
187 kfree(client);
188 return rc;
189 }
190
191 save_client = client;
192 return 0;
193}
194
195static int
196m41t00_attach(struct i2c_adapter *adap)
197{
198 return i2c_probe(adap, &addr_data, m41t00_probe);
199}
200
201static int
202m41t00_detach(struct i2c_client *client)
203{
204 int rc;
205
206 if ((rc = i2c_detach_client(client)) == 0) {
Jean Delvare5da69ba2005-07-01 14:28:15 +0200207 kfree(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 tasklet_kill(&m41t00_tasklet);
209 }
210 return rc;
211}
212
213static struct i2c_driver m41t00_driver = {
Laurent Riffarda9718b02005-11-26 20:36:00 +0100214 .driver = {
Laurent Riffarda9718b02005-11-26 20:36:00 +0100215 .name = M41T00_DRV_NAME,
216 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 .id = I2C_DRIVERID_STM41T00,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 .attach_adapter = m41t00_attach,
219 .detach_client = m41t00_detach,
220};
221
222static int __init
223m41t00_init(void)
224{
225 return i2c_add_driver(&m41t00_driver);
226}
227
228static void __exit
229m41t00_exit(void)
230{
231 i2c_del_driver(&m41t00_driver);
232 return;
233}
234
235module_init(m41t00_init);
236module_exit(m41t00_exit);
237
238MODULE_AUTHOR("Mark A. Greer <mgreer@mvista.com>");
239MODULE_DESCRIPTION("ST Microelectronics M41T00 RTC I2C Client Driver");
240MODULE_LICENSE("GPL");