blob: c973ba94c422bc182c8b994dfd877dac967030bc [file] [log] [blame]
G. Liakhovetski9c0c5702006-06-25 05:48:18 -07001/*
2 * drivers/rtc/rtc-pcf8583.c
3 *
4 * Copyright (C) 2000 Russell King
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * Driver for PCF8583 RTC & RAM chip
11 *
12 * Converted to the generic RTC susbsystem by G. Liakhovetski (2006)
13 */
14#include <linux/module.h>
15#include <linux/i2c.h>
16#include <linux/slab.h>
17#include <linux/string.h>
18#include <linux/mc146818rtc.h>
19#include <linux/init.h>
20#include <linux/errno.h>
21#include <linux/bcd.h>
22
23struct rtc_mem {
24 unsigned int loc;
25 unsigned int nr;
26 unsigned char *data;
27};
28
29struct pcf8583 {
30 struct i2c_client client;
31 struct rtc_device *rtc;
32 unsigned char ctrl;
33};
34
35#define CTRL_STOP 0x80
36#define CTRL_HOLD 0x40
37#define CTRL_32KHZ 0x00
38#define CTRL_MASK 0x08
39#define CTRL_ALARMEN 0x04
40#define CTRL_ALARM 0x02
41#define CTRL_TIMER 0x01
42
Jean Delvare922539a2008-01-27 18:14:47 +010043static const unsigned short normal_i2c[] = { 0x50, I2C_CLIENT_END };
G. Liakhovetski9c0c5702006-06-25 05:48:18 -070044
45/* Module parameters */
46I2C_CLIENT_INSMOD;
47
48static struct i2c_driver pcf8583_driver;
49
50#define get_ctrl(x) ((struct pcf8583 *)i2c_get_clientdata(x))->ctrl
51#define set_ctrl(x, v) get_ctrl(x) = v
52
53#define CMOS_YEAR (64 + 128)
54#define CMOS_CHECKSUM (63)
55
56static int pcf8583_get_datetime(struct i2c_client *client, struct rtc_time *dt)
57{
58 unsigned char buf[8], addr[1] = { 1 };
59 struct i2c_msg msgs[2] = {
60 {
61 .addr = client->addr,
62 .flags = 0,
63 .len = 1,
64 .buf = addr,
65 }, {
66 .addr = client->addr,
67 .flags = I2C_M_RD,
68 .len = 6,
69 .buf = buf,
70 }
71 };
72 int ret;
73
74 memset(buf, 0, sizeof(buf));
75
76 ret = i2c_transfer(client->adapter, msgs, 2);
77 if (ret == 2) {
78 dt->tm_year = buf[4] >> 6;
79 dt->tm_wday = buf[5] >> 5;
80
81 buf[4] &= 0x3f;
82 buf[5] &= 0x1f;
83
Russell Kingf5e5b732007-03-04 20:12:07 +000084 dt->tm_sec = BCD2BIN(buf[1]);
85 dt->tm_min = BCD2BIN(buf[2]);
86 dt->tm_hour = BCD2BIN(buf[3]);
87 dt->tm_mday = BCD2BIN(buf[4]);
Russell King0ed8f212007-03-04 20:13:13 +000088 dt->tm_mon = BCD2BIN(buf[5]) - 1;
G. Liakhovetski9c0c5702006-06-25 05:48:18 -070089 }
90
91 return ret == 2 ? 0 : -EIO;
92}
93
94static int pcf8583_set_datetime(struct i2c_client *client, struct rtc_time *dt, int datetoo)
95{
96 unsigned char buf[8];
97 int ret, len = 6;
98
99 buf[0] = 0;
100 buf[1] = get_ctrl(client) | 0x80;
101 buf[2] = 0;
Russell Kingf5e5b732007-03-04 20:12:07 +0000102 buf[3] = BIN2BCD(dt->tm_sec);
103 buf[4] = BIN2BCD(dt->tm_min);
104 buf[5] = BIN2BCD(dt->tm_hour);
G. Liakhovetski9c0c5702006-06-25 05:48:18 -0700105
106 if (datetoo) {
107 len = 8;
Russell Kingf5e5b732007-03-04 20:12:07 +0000108 buf[6] = BIN2BCD(dt->tm_mday) | (dt->tm_year << 6);
Russell King0ed8f212007-03-04 20:13:13 +0000109 buf[7] = BIN2BCD(dt->tm_mon + 1) | (dt->tm_wday << 5);
G. Liakhovetski9c0c5702006-06-25 05:48:18 -0700110 }
111
112 ret = i2c_master_send(client, (char *)buf, len);
113 if (ret != len)
114 return -EIO;
115
116 buf[1] = get_ctrl(client);
117 ret = i2c_master_send(client, (char *)buf, 2);
118
119 return ret == 2 ? 0 : -EIO;
120}
121
122static int pcf8583_get_ctrl(struct i2c_client *client, unsigned char *ctrl)
123{
124 *ctrl = get_ctrl(client);
125 return 0;
126}
127
128static int pcf8583_set_ctrl(struct i2c_client *client, unsigned char *ctrl)
129{
130 unsigned char buf[2];
131
132 buf[0] = 0;
133 buf[1] = *ctrl;
134 set_ctrl(client, *ctrl);
135
136 return i2c_master_send(client, (char *)buf, 2);
137}
138
139static int pcf8583_read_mem(struct i2c_client *client, struct rtc_mem *mem)
140{
141 unsigned char addr[1];
142 struct i2c_msg msgs[2] = {
143 {
144 .addr = client->addr,
145 .flags = 0,
146 .len = 1,
147 .buf = addr,
148 }, {
149 .addr = client->addr,
150 .flags = I2C_M_RD,
151 .len = mem->nr,
152 .buf = mem->data,
153 }
154 };
155
156 if (mem->loc < 8)
157 return -EINVAL;
158
159 addr[0] = mem->loc;
160
161 return i2c_transfer(client->adapter, msgs, 2) == 2 ? 0 : -EIO;
162}
163
164static int pcf8583_write_mem(struct i2c_client *client, struct rtc_mem *mem)
165{
166 unsigned char addr[1];
167 struct i2c_msg msgs[2] = {
168 {
169 .addr = client->addr,
170 .flags = 0,
171 .len = 1,
172 .buf = addr,
173 }, {
174 .addr = client->addr,
175 .flags = I2C_M_NOSTART,
176 .len = mem->nr,
177 .buf = mem->data,
178 }
179 };
180
181 if (mem->loc < 8)
182 return -EINVAL;
183
184 addr[0] = mem->loc;
185
186 return i2c_transfer(client->adapter, msgs, 2) == 2 ? 0 : -EIO;
187}
188
189static int pcf8583_rtc_read_time(struct device *dev, struct rtc_time *tm)
190{
191 struct i2c_client *client = to_i2c_client(dev);
192 unsigned char ctrl, year[2];
193 struct rtc_mem mem = { CMOS_YEAR, sizeof(year), year };
194 int real_year, year_offset, err;
195
196 /*
197 * Ensure that the RTC is running.
198 */
199 pcf8583_get_ctrl(client, &ctrl);
200 if (ctrl & (CTRL_STOP | CTRL_HOLD)) {
201 unsigned char new_ctrl = ctrl & ~(CTRL_STOP | CTRL_HOLD);
202
203 printk(KERN_WARNING "RTC: resetting control %02x -> %02x\n",
204 ctrl, new_ctrl);
205
206 if ((err = pcf8583_set_ctrl(client, &new_ctrl)) < 0)
207 return err;
208 }
209
210 if (pcf8583_get_datetime(client, tm) ||
211 pcf8583_read_mem(client, &mem))
212 return -EIO;
213
214 real_year = year[0];
215
216 /*
217 * The RTC year holds the LSB two bits of the current
218 * year, which should reflect the LSB two bits of the
219 * CMOS copy of the year. Any difference indicates
220 * that we have to correct the CMOS version.
221 */
222 year_offset = tm->tm_year - (real_year & 3);
223 if (year_offset < 0)
224 /*
225 * RTC year wrapped. Adjust it appropriately.
226 */
227 year_offset += 4;
228
Russell King0ed8f212007-03-04 20:13:13 +0000229 tm->tm_year = (real_year + year_offset + year[1] * 100) - 1900;
G. Liakhovetski9c0c5702006-06-25 05:48:18 -0700230
231 return 0;
232}
233
234static int pcf8583_rtc_set_time(struct device *dev, struct rtc_time *tm)
235{
236 struct i2c_client *client = to_i2c_client(dev);
237 unsigned char year[2], chk;
238 struct rtc_mem cmos_year = { CMOS_YEAR, sizeof(year), year };
239 struct rtc_mem cmos_check = { CMOS_CHECKSUM, 1, &chk };
Russell King0ed8f212007-03-04 20:13:13 +0000240 unsigned int proper_year = tm->tm_year + 1900;
G. Liakhovetski9c0c5702006-06-25 05:48:18 -0700241 int ret;
242
243 /*
244 * The RTC's own 2-bit year must reflect the least
245 * significant two bits of the CMOS year.
246 */
247
248 ret = pcf8583_set_datetime(client, tm, 1);
249 if (ret)
250 return ret;
251
252 ret = pcf8583_read_mem(client, &cmos_check);
253 if (ret)
254 return ret;
255
256 ret = pcf8583_read_mem(client, &cmos_year);
257 if (ret)
258 return ret;
259
260 chk -= year[1] + year[0];
261
Russell King0ed8f212007-03-04 20:13:13 +0000262 year[1] = proper_year / 100;
263 year[0] = proper_year % 100;
G. Liakhovetski9c0c5702006-06-25 05:48:18 -0700264
265 chk += year[1] + year[0];
266
267 ret = pcf8583_write_mem(client, &cmos_year);
268
269 if (ret)
270 return ret;
271
272 ret = pcf8583_write_mem(client, &cmos_check);
273
274 return ret;
275}
276
David Brownellff8371a2006-09-30 23:28:17 -0700277static const struct rtc_class_ops pcf8583_rtc_ops = {
G. Liakhovetski9c0c5702006-06-25 05:48:18 -0700278 .read_time = pcf8583_rtc_read_time,
279 .set_time = pcf8583_rtc_set_time,
280};
281
282static int pcf8583_probe(struct i2c_adapter *adap, int addr, int kind);
283
284static int pcf8583_attach(struct i2c_adapter *adap)
285{
286 return i2c_probe(adap, &addr_data, pcf8583_probe);
287}
288
289static int pcf8583_detach(struct i2c_client *client)
290{
291 int err;
292 struct pcf8583 *pcf = i2c_get_clientdata(client);
293 struct rtc_device *rtc = pcf->rtc;
294
295 if (rtc)
296 rtc_device_unregister(rtc);
297
298 if ((err = i2c_detach_client(client)))
299 return err;
300
301 kfree(pcf);
302 return 0;
303}
304
305static struct i2c_driver pcf8583_driver = {
306 .driver = {
307 .name = "pcf8583",
308 },
309 .id = I2C_DRIVERID_PCF8583,
310 .attach_adapter = pcf8583_attach,
311 .detach_client = pcf8583_detach,
312};
313
314static int pcf8583_probe(struct i2c_adapter *adap, int addr, int kind)
315{
316 struct pcf8583 *pcf;
317 struct i2c_client *client;
318 struct rtc_device *rtc;
319 unsigned char buf[1], ad[1] = { 0 };
320 int err;
321 struct i2c_msg msgs[2] = {
322 {
323 .addr = addr,
324 .flags = 0,
325 .len = 1,
326 .buf = ad,
327 }, {
328 .addr = addr,
329 .flags = I2C_M_RD,
330 .len = 1,
331 .buf = buf,
332 }
333 };
334
Jean Delvarec0186642007-10-16 01:28:18 -0700335 if (!i2c_check_functionality(adap, I2C_FUNC_I2C))
336 return 0;
337
G. Liakhovetski9c0c5702006-06-25 05:48:18 -0700338 pcf = kzalloc(sizeof(*pcf), GFP_KERNEL);
339 if (!pcf)
340 return -ENOMEM;
341
342 client = &pcf->client;
343
344 client->addr = addr;
345 client->adapter = adap;
346 client->driver = &pcf8583_driver;
347
348 strlcpy(client->name, pcf8583_driver.driver.name, I2C_NAME_SIZE);
349
350 if (i2c_transfer(client->adapter, msgs, 2) != 2) {
351 err = -EIO;
352 goto exit_kfree;
353 }
354
355 err = i2c_attach_client(client);
356
357 if (err)
358 goto exit_kfree;
359
360 rtc = rtc_device_register(pcf8583_driver.driver.name, &client->dev,
361 &pcf8583_rtc_ops, THIS_MODULE);
362
363 if (IS_ERR(rtc)) {
364 err = PTR_ERR(rtc);
365 goto exit_detach;
366 }
367
368 pcf->rtc = rtc;
369 i2c_set_clientdata(client, pcf);
370 set_ctrl(client, buf[0]);
371
372 return 0;
373
374exit_detach:
375 i2c_detach_client(client);
376
377exit_kfree:
378 kfree(pcf);
379
380 return err;
381}
382
383static __init int pcf8583_init(void)
384{
385 return i2c_add_driver(&pcf8583_driver);
386}
387
388static __exit void pcf8583_exit(void)
389{
390 i2c_del_driver(&pcf8583_driver);
391}
392
393module_init(pcf8583_init);
394module_exit(pcf8583_exit);
395
396MODULE_AUTHOR("Russell King");
397MODULE_DESCRIPTION("PCF8583 I2C RTC driver");
398MODULE_LICENSE("GPL");