blob: 99ab4ec343902e8d5f3054fba8ffd301cfa2f168 [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>
Arjan van de Venb3585e42006-01-11 10:50:26 +010027#include <linux/mutex.h>
Mark A. Greer8c750c02006-03-31 23:06:03 +020028#include <linux/workqueue.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30#include <asm/time.h>
31#include <asm/rtc.h>
32
33#define M41T00_DRV_NAME "m41t00"
34
Arjan van de Venb3585e42006-01-11 10:50:26 +010035static DEFINE_MUTEX(m41t00_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37static struct i2c_driver m41t00_driver;
38static struct i2c_client *save_client;
39
40static unsigned short ignore[] = { I2C_CLIENT_END };
41static unsigned short normal_addr[] = { 0x68, I2C_CLIENT_END };
42
43static struct i2c_client_address_data addr_data = {
44 .normal_i2c = normal_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 .probe = ignore,
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 .ignore = ignore,
Linus Torvalds1da177e2005-04-16 15:20:36 -070047};
48
49ulong
50m41t00_get_rtc_time(void)
51{
52 s32 sec, min, hour, day, mon, year;
53 s32 sec1, min1, hour1, day1, mon1, year1;
54 ulong limit = 10;
55
56 sec = min = hour = day = mon = year = 0;
57 sec1 = min1 = hour1 = day1 = mon1 = year1 = 0;
58
Arjan van de Venb3585e42006-01-11 10:50:26 +010059 mutex_lock(&m41t00_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 do {
61 if (((sec = i2c_smbus_read_byte_data(save_client, 0)) >= 0)
62 && ((min = i2c_smbus_read_byte_data(save_client, 1))
63 >= 0)
64 && ((hour = i2c_smbus_read_byte_data(save_client, 2))
65 >= 0)
66 && ((day = i2c_smbus_read_byte_data(save_client, 4))
67 >= 0)
68 && ((mon = i2c_smbus_read_byte_data(save_client, 5))
69 >= 0)
70 && ((year = i2c_smbus_read_byte_data(save_client, 6))
71 >= 0)
72 && ((sec == sec1) && (min == min1) && (hour == hour1)
73 && (day == day1) && (mon == mon1)
74 && (year == year1)))
75
76 break;
77
78 sec1 = sec;
79 min1 = min;
80 hour1 = hour;
81 day1 = day;
82 mon1 = mon;
83 year1 = year;
84 } while (--limit > 0);
Arjan van de Venb3585e42006-01-11 10:50:26 +010085 mutex_unlock(&m41t00_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87 if (limit == 0) {
88 dev_warn(&save_client->dev,
89 "m41t00: can't read rtc chip\n");
90 sec = min = hour = day = mon = year = 0;
91 }
92
93 sec &= 0x7f;
94 min &= 0x7f;
95 hour &= 0x3f;
96 day &= 0x3f;
97 mon &= 0x1f;
98 year &= 0xff;
99
100 BCD_TO_BIN(sec);
101 BCD_TO_BIN(min);
102 BCD_TO_BIN(hour);
103 BCD_TO_BIN(day);
104 BCD_TO_BIN(mon);
105 BCD_TO_BIN(year);
106
107 year += 1900;
108 if (year < 1970)
109 year += 100;
110
111 return mktime(year, mon, day, hour, min, sec);
112}
113
114static void
Mark A. Greer8c750c02006-03-31 23:06:03 +0200115m41t00_set(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
117 struct rtc_time tm;
118 ulong nowtime = *(ulong *)arg;
119
120 to_tm(nowtime, &tm);
121 tm.tm_year = (tm.tm_year - 1900) % 100;
122
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);
129
Arjan van de Venb3585e42006-01-11 10:50:26 +0100130 mutex_lock(&m41t00_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 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)
133 < 0)
David Barksdale8db08de2006-04-18 22:20:27 -0700134 || (i2c_smbus_write_byte_data(save_client, 2, tm.tm_hour & 0x3f)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 < 0)
David Barksdale8db08de2006-04-18 22:20:27 -0700136 || (i2c_smbus_write_byte_data(save_client, 4, tm.tm_mday & 0x3f)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 < 0)
David Barksdale8db08de2006-04-18 22:20:27 -0700138 || (i2c_smbus_write_byte_data(save_client, 5, tm.tm_mon & 0x1f)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 < 0)
David Barksdale8db08de2006-04-18 22:20:27 -0700140 || (i2c_smbus_write_byte_data(save_client, 6, tm.tm_year & 0xff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 < 0))
142
143 dev_warn(&save_client->dev,"m41t00: can't write to rtc chip\n");
144
Arjan van de Venb3585e42006-01-11 10:50:26 +0100145 mutex_unlock(&m41t00_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 return;
147}
148
Mark A. Greer8c750c02006-03-31 23:06:03 +0200149static ulong new_time;
150static struct workqueue_struct *m41t00_wq;
151static DECLARE_WORK(m41t00_work, m41t00_set, &new_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153int
154m41t00_set_rtc_time(ulong nowtime)
155{
156 new_time = nowtime;
157
158 if (in_interrupt())
Mark A. Greer8c750c02006-03-31 23:06:03 +0200159 queue_work(m41t00_wq, &m41t00_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 else
Mark A. Greer8c750c02006-03-31 23:06:03 +0200161 m41t00_set(&new_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163 return 0;
164}
165
166/*
167 *****************************************************************************
168 *
169 * Driver Interface
170 *
171 *****************************************************************************
172 */
173static int
174m41t00_probe(struct i2c_adapter *adap, int addr, int kind)
175{
176 struct i2c_client *client;
177 int rc;
178
Deepak Saxena5263ebb2005-10-17 23:09:43 +0200179 client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 if (!client)
181 return -ENOMEM;
182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 strncpy(client->name, M41T00_DRV_NAME, I2C_NAME_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 client->addr = addr;
185 client->adapter = adap;
186 client->driver = &m41t00_driver;
187
188 if ((rc = i2c_attach_client(client)) != 0) {
189 kfree(client);
190 return rc;
191 }
192
Mark A. Greer8c750c02006-03-31 23:06:03 +0200193 m41t00_wq = create_singlethread_workqueue("m41t00");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 save_client = client;
195 return 0;
196}
197
198static int
199m41t00_attach(struct i2c_adapter *adap)
200{
201 return i2c_probe(adap, &addr_data, m41t00_probe);
202}
203
204static int
205m41t00_detach(struct i2c_client *client)
206{
207 int rc;
208
209 if ((rc = i2c_detach_client(client)) == 0) {
Jean Delvare5da69ba2005-07-01 14:28:15 +0200210 kfree(client);
Mark A. Greer8c750c02006-03-31 23:06:03 +0200211 destroy_workqueue(m41t00_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 }
213 return rc;
214}
215
216static struct i2c_driver m41t00_driver = {
Laurent Riffarda9718b02005-11-26 20:36:00 +0100217 .driver = {
Laurent Riffarda9718b02005-11-26 20:36:00 +0100218 .name = M41T00_DRV_NAME,
219 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 .id = I2C_DRIVERID_STM41T00,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 .attach_adapter = m41t00_attach,
222 .detach_client = m41t00_detach,
223};
224
225static int __init
226m41t00_init(void)
227{
228 return i2c_add_driver(&m41t00_driver);
229}
230
231static void __exit
232m41t00_exit(void)
233{
234 i2c_del_driver(&m41t00_driver);
235 return;
236}
237
238module_init(m41t00_init);
239module_exit(m41t00_exit);
240
241MODULE_AUTHOR("Mark A. Greer <mgreer@mvista.com>");
242MODULE_DESCRIPTION("ST Microelectronics M41T00 RTC I2C Client Driver");
243MODULE_LICENSE("GPL");