xref: /linux/drivers/rtc/rtc-pcf8583.c (revision c58310bf4933986513020fa90b4190c7492995ae)
1 /*
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 
23 struct rtc_mem {
24 	unsigned int	loc;
25 	unsigned int	nr;
26 	unsigned char	*data;
27 };
28 
29 struct 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 
43 static const unsigned short normal_i2c[] = { 0x50, I2C_CLIENT_END };
44 
45 /* Module parameters */
46 I2C_CLIENT_INSMOD;
47 
48 static 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 
56 static 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 
84 		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]);
88 		dt->tm_mon = BCD2BIN(buf[5]) - 1;
89 	}
90 
91 	return ret == 2 ? 0 : -EIO;
92 }
93 
94 static 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;
102 	buf[3] = BIN2BCD(dt->tm_sec);
103 	buf[4] = BIN2BCD(dt->tm_min);
104 	buf[5] = BIN2BCD(dt->tm_hour);
105 
106 	if (datetoo) {
107 		len = 8;
108 		buf[6] = BIN2BCD(dt->tm_mday) | (dt->tm_year << 6);
109 		buf[7] = BIN2BCD(dt->tm_mon + 1)  | (dt->tm_wday << 5);
110 	}
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 
122 static int pcf8583_get_ctrl(struct i2c_client *client, unsigned char *ctrl)
123 {
124 	*ctrl = get_ctrl(client);
125 	return 0;
126 }
127 
128 static 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 
139 static 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 
164 static int pcf8583_write_mem(struct i2c_client *client, struct rtc_mem *mem)
165 {
166 	unsigned char buf[9];
167 	int ret;
168 
169 	if (mem->loc < 8 || mem->nr > 8)
170 		return -EINVAL;
171 
172 	buf[0] = mem->loc;
173 	memcpy(buf + 1, mem->data, mem->nr);
174 
175 	ret = i2c_master_send(client, buf, mem->nr + 1);
176 	return ret == mem->nr + 1 ? 0 : -EIO;
177 }
178 
179 static int pcf8583_rtc_read_time(struct device *dev, struct rtc_time *tm)
180 {
181 	struct i2c_client *client = to_i2c_client(dev);
182 	unsigned char ctrl, year[2];
183 	struct rtc_mem mem = { CMOS_YEAR, sizeof(year), year };
184 	int real_year, year_offset, err;
185 
186 	/*
187 	 * Ensure that the RTC is running.
188 	 */
189 	pcf8583_get_ctrl(client, &ctrl);
190 	if (ctrl & (CTRL_STOP | CTRL_HOLD)) {
191 		unsigned char new_ctrl = ctrl & ~(CTRL_STOP | CTRL_HOLD);
192 
193 		printk(KERN_WARNING "RTC: resetting control %02x -> %02x\n",
194 		       ctrl, new_ctrl);
195 
196 		if ((err = pcf8583_set_ctrl(client, &new_ctrl)) < 0)
197 			return err;
198 	}
199 
200 	if (pcf8583_get_datetime(client, tm) ||
201 	    pcf8583_read_mem(client, &mem))
202 		return -EIO;
203 
204 	real_year = year[0];
205 
206 	/*
207 	 * The RTC year holds the LSB two bits of the current
208 	 * year, which should reflect the LSB two bits of the
209 	 * CMOS copy of the year.  Any difference indicates
210 	 * that we have to correct the CMOS version.
211 	 */
212 	year_offset = tm->tm_year - (real_year & 3);
213 	if (year_offset < 0)
214 		/*
215 		 * RTC year wrapped.  Adjust it appropriately.
216 		 */
217 		year_offset += 4;
218 
219 	tm->tm_year = (real_year + year_offset + year[1] * 100) - 1900;
220 
221 	return 0;
222 }
223 
224 static int pcf8583_rtc_set_time(struct device *dev, struct rtc_time *tm)
225 {
226 	struct i2c_client *client = to_i2c_client(dev);
227 	unsigned char year[2], chk;
228 	struct rtc_mem cmos_year  = { CMOS_YEAR, sizeof(year), year };
229 	struct rtc_mem cmos_check = { CMOS_CHECKSUM, 1, &chk };
230 	unsigned int proper_year = tm->tm_year + 1900;
231 	int ret;
232 
233 	/*
234 	 * The RTC's own 2-bit year must reflect the least
235 	 * significant two bits of the CMOS year.
236 	 */
237 
238 	ret = pcf8583_set_datetime(client, tm, 1);
239 	if (ret)
240 		return ret;
241 
242 	ret = pcf8583_read_mem(client, &cmos_check);
243 	if (ret)
244 		return ret;
245 
246 	ret = pcf8583_read_mem(client, &cmos_year);
247 	if (ret)
248 		return ret;
249 
250 	chk -= year[1] + year[0];
251 
252 	year[1] = proper_year / 100;
253 	year[0] = proper_year % 100;
254 
255 	chk += year[1] + year[0];
256 
257 	ret = pcf8583_write_mem(client, &cmos_year);
258 
259 	if (ret)
260 		return ret;
261 
262 	ret = pcf8583_write_mem(client, &cmos_check);
263 
264 	return ret;
265 }
266 
267 static const struct rtc_class_ops pcf8583_rtc_ops = {
268 	.read_time	= pcf8583_rtc_read_time,
269 	.set_time	= pcf8583_rtc_set_time,
270 };
271 
272 static int pcf8583_probe(struct i2c_adapter *adap, int addr, int kind);
273 
274 static int pcf8583_attach(struct i2c_adapter *adap)
275 {
276 	return i2c_probe(adap, &addr_data, pcf8583_probe);
277 }
278 
279 static int pcf8583_detach(struct i2c_client *client)
280 {
281 	int err;
282 	struct pcf8583 *pcf = i2c_get_clientdata(client);
283 	struct rtc_device *rtc = pcf->rtc;
284 
285 	if (rtc)
286 		rtc_device_unregister(rtc);
287 
288 	if ((err = i2c_detach_client(client)))
289 		return err;
290 
291 	kfree(pcf);
292 	return 0;
293 }
294 
295 static struct i2c_driver pcf8583_driver = {
296 	.driver = {
297 		.name	= "pcf8583",
298 	},
299 	.id		= I2C_DRIVERID_PCF8583,
300 	.attach_adapter	= pcf8583_attach,
301 	.detach_client	= pcf8583_detach,
302 };
303 
304 static int pcf8583_probe(struct i2c_adapter *adap, int addr, int kind)
305 {
306 	struct pcf8583 *pcf;
307 	struct i2c_client *client;
308 	struct rtc_device *rtc;
309 	unsigned char buf[1], ad[1] = { 0 };
310 	int err;
311 	struct i2c_msg msgs[2] = {
312 		{
313 			.addr = addr,
314 			.flags = 0,
315 			.len = 1,
316 			.buf = ad,
317 		}, {
318 			.addr = addr,
319 			.flags = I2C_M_RD,
320 			.len = 1,
321 			.buf = buf,
322 		}
323 	};
324 
325 	if (!i2c_check_functionality(adap, I2C_FUNC_I2C))
326 		return 0;
327 
328 	pcf = kzalloc(sizeof(*pcf), GFP_KERNEL);
329 	if (!pcf)
330 		return -ENOMEM;
331 
332 	client = &pcf->client;
333 
334 	client->addr		= addr;
335 	client->adapter	= adap;
336 	client->driver	= &pcf8583_driver;
337 
338 	strlcpy(client->name, pcf8583_driver.driver.name, I2C_NAME_SIZE);
339 
340 	if (i2c_transfer(client->adapter, msgs, 2) != 2) {
341 		err = -EIO;
342 		goto exit_kfree;
343 	}
344 
345 	err = i2c_attach_client(client);
346 
347 	if (err)
348 		goto exit_kfree;
349 
350 	rtc = rtc_device_register(pcf8583_driver.driver.name, &client->dev,
351 				  &pcf8583_rtc_ops, THIS_MODULE);
352 
353 	if (IS_ERR(rtc)) {
354 		err = PTR_ERR(rtc);
355 		goto exit_detach;
356 	}
357 
358 	pcf->rtc = rtc;
359 	i2c_set_clientdata(client, pcf);
360 	set_ctrl(client, buf[0]);
361 
362 	return 0;
363 
364 exit_detach:
365 	i2c_detach_client(client);
366 
367 exit_kfree:
368 	kfree(pcf);
369 
370 	return err;
371 }
372 
373 static __init int pcf8583_init(void)
374 {
375 	return i2c_add_driver(&pcf8583_driver);
376 }
377 
378 static __exit void pcf8583_exit(void)
379 {
380 	i2c_del_driver(&pcf8583_driver);
381 }
382 
383 module_init(pcf8583_init);
384 module_exit(pcf8583_exit);
385 
386 MODULE_AUTHOR("Russell King");
387 MODULE_DESCRIPTION("PCF8583 I2C RTC driver");
388 MODULE_LICENSE("GPL");
389