[PATCH] i2c: cleanup m41t00
[powerpc.git] / drivers / i2c / chips / m41t00.c
1 /*
2  * 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.
14  */
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>
22 #include <linux/mutex.h>
23 #include <linux/workqueue.h>
24
25 #include <asm/time.h>
26 #include <asm/rtc.h>
27
28 #define M41T00_DRV_NAME         "m41t00"
29
30 static DEFINE_MUTEX(m41t00_mutex);
31
32 static struct i2c_driver m41t00_driver;
33 static struct i2c_client *save_client;
34
35 static unsigned short ignore[] = { I2C_CLIENT_END };
36 static unsigned short normal_addr[] = { 0x68, I2C_CLIENT_END };
37
38 static struct i2c_client_address_data addr_data = {
39         .normal_i2c     = normal_addr,
40         .probe          = ignore,
41         .ignore         = ignore,
42 };
43
44 ulong
45 m41t00_get_rtc_time(void)
46 {
47         s32 sec, min, hour, day, mon, year;
48         s32 sec1, min1, hour1, day1, mon1, year1;
49         ulong limit = 10;
50
51         sec = min = hour = day = mon = year = 0;
52         sec1 = min1 = hour1 = day1 = mon1 = year1 = 0;
53
54         mutex_lock(&m41t00_mutex);
55         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);
80         mutex_unlock(&m41t00_mutex);
81
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
95         sec = BCD2BIN(sec);
96         min = BCD2BIN(min);
97         hour = BCD2BIN(hour);
98         day = BCD2BIN(day);
99         mon = BCD2BIN(mon);
100         year = BCD2BIN(year);
101
102         year += 1900;
103         if (year < 1970)
104                 year += 100;
105
106         return mktime(year, mon, day, hour, min, sec);
107 }
108
109 static void
110 m41t00_set(void *arg)
111 {
112         struct rtc_time tm;
113         ulong nowtime = *(ulong *)arg;
114
115         to_tm(nowtime, &tm);
116         tm.tm_year = (tm.tm_year - 1900) % 100;
117
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);
124
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)
128                         < 0)
129                 || (i2c_smbus_write_byte_data(save_client, 2, tm.tm_hour & 0x3f)
130                         < 0)
131                 || (i2c_smbus_write_byte_data(save_client, 4, tm.tm_mday & 0x3f)
132                         < 0)
133                 || (i2c_smbus_write_byte_data(save_client, 5, tm.tm_mon & 0x1f)
134                         < 0)
135                 || (i2c_smbus_write_byte_data(save_client, 6, tm.tm_year & 0xff)
136                         < 0))
137
138                 dev_warn(&save_client->dev,"m41t00: can't write to rtc chip\n");
139
140         mutex_unlock(&m41t00_mutex);
141 }
142
143 static ulong new_time;
144 static struct workqueue_struct *m41t00_wq;
145 static DECLARE_WORK(m41t00_work, m41t00_set, &new_time);
146
147 int
148 m41t00_set_rtc_time(ulong nowtime)
149 {
150         new_time = nowtime;
151
152         if (in_interrupt())
153                 queue_work(m41t00_wq, &m41t00_work);
154         else
155                 m41t00_set(&new_time);
156
157         return 0;
158 }
159
160 /*
161  *****************************************************************************
162  *
163  *      Driver Interface
164  *
165  *****************************************************************************
166  */
167 static int
168 m41t00_probe(struct i2c_adapter *adap, int addr, int kind)
169 {
170         struct i2c_client *client;
171         int rc;
172
173         client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
174         if (!client)
175                 return -ENOMEM;
176
177         strlcpy(client->name, M41T00_DRV_NAME, I2C_NAME_SIZE);
178         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
187         m41t00_wq = create_singlethread_workqueue("m41t00");
188         save_client = client;
189         return 0;
190 }
191
192 static int
193 m41t00_attach(struct i2c_adapter *adap)
194 {
195         return i2c_probe(adap, &addr_data, m41t00_probe);
196 }
197
198 static int
199 m41t00_detach(struct i2c_client *client)
200 {
201         int rc;
202
203         if ((rc = i2c_detach_client(client)) == 0) {
204                 kfree(client);
205                 destroy_workqueue(m41t00_wq);
206         }
207         return rc;
208 }
209
210 static struct i2c_driver m41t00_driver = {
211         .driver = {
212                 .name   = M41T00_DRV_NAME,
213         },
214         .id             = I2C_DRIVERID_STM41T00,
215         .attach_adapter = m41t00_attach,
216         .detach_client  = m41t00_detach,
217 };
218
219 static int __init
220 m41t00_init(void)
221 {
222         return i2c_add_driver(&m41t00_driver);
223 }
224
225 static void __exit
226 m41t00_exit(void)
227 {
228         i2c_del_driver(&m41t00_driver);
229 }
230
231 module_init(m41t00_init);
232 module_exit(m41t00_exit);
233
234 MODULE_AUTHOR("Mark A. Greer <mgreer@mvista.com>");
235 MODULE_DESCRIPTION("ST Microelectronics M41T00 RTC I2C Client Driver");
236 MODULE_LICENSE("GPL");