Blame view

drivers/rtc/rtc-pcf8583.c 7.02 KB
9c0c57057   G. Liakhovetski   [PATCH] drivers/a...
1
2
3
4
  /*
   *  drivers/rtc/rtc-pcf8583.c
   *
   *  Copyright (C) 2000 Russell King
02bb584f3   Wolfram Sang   rtc: convert the ...
5
   *  Copyright (C) 2008 Wolfram Sang & Juergen Beisert, Pengutronix
9c0c57057   G. Liakhovetski   [PATCH] drivers/a...
6
7
8
9
10
11
12
13
14
15
16
17
   *
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU General Public License version 2 as
   * published by the Free Software Foundation.
   *
   *  Driver for PCF8583 RTC & RAM chip
   *
   *  Converted to the generic RTC susbsystem by G. Liakhovetski (2006)
   */
  #include <linux/module.h>
  #include <linux/i2c.h>
  #include <linux/slab.h>
77459b059   David Brownell   rtc-pcf8583 build...
18
  #include <linux/rtc.h>
9c0c57057   G. Liakhovetski   [PATCH] drivers/a...
19
20
21
22
23
24
25
26
27
28
29
  #include <linux/init.h>
  #include <linux/errno.h>
  #include <linux/bcd.h>
  
  struct rtc_mem {
  	unsigned int	loc;
  	unsigned int	nr;
  	unsigned char	*data;
  };
  
  struct pcf8583 {
9c0c57057   G. Liakhovetski   [PATCH] drivers/a...
30
31
32
33
34
35
36
37
38
39
40
  	struct rtc_device *rtc;
  	unsigned char ctrl;
  };
  
  #define CTRL_STOP	0x80
  #define CTRL_HOLD	0x40
  #define CTRL_32KHZ	0x00
  #define CTRL_MASK	0x08
  #define CTRL_ALARMEN	0x04
  #define CTRL_ALARM	0x02
  #define CTRL_TIMER	0x01
9c0c57057   G. Liakhovetski   [PATCH] drivers/a...
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
  
  static struct i2c_driver pcf8583_driver;
  
  #define get_ctrl(x)    ((struct pcf8583 *)i2c_get_clientdata(x))->ctrl
  #define set_ctrl(x, v) get_ctrl(x) = v
  
  #define CMOS_YEAR	(64 + 128)
  #define CMOS_CHECKSUM	(63)
  
  static int pcf8583_get_datetime(struct i2c_client *client, struct rtc_time *dt)
  {
  	unsigned char buf[8], addr[1] = { 1 };
  	struct i2c_msg msgs[2] = {
  		{
  			.addr = client->addr,
  			.flags = 0,
  			.len = 1,
  			.buf = addr,
  		}, {
  			.addr = client->addr,
  			.flags = I2C_M_RD,
  			.len = 6,
  			.buf = buf,
  		}
  	};
  	int ret;
  
  	memset(buf, 0, sizeof(buf));
  
  	ret = i2c_transfer(client->adapter, msgs, 2);
  	if (ret == 2) {
  		dt->tm_year = buf[4] >> 6;
  		dt->tm_wday = buf[5] >> 5;
  
  		buf[4] &= 0x3f;
  		buf[5] &= 0x1f;
fe20ba70a   Adrian Bunk   drivers/rtc/: use...
77
78
79
80
81
  		dt->tm_sec = bcd2bin(buf[1]);
  		dt->tm_min = bcd2bin(buf[2]);
  		dt->tm_hour = bcd2bin(buf[3]);
  		dt->tm_mday = bcd2bin(buf[4]);
  		dt->tm_mon = bcd2bin(buf[5]) - 1;
9c0c57057   G. Liakhovetski   [PATCH] drivers/a...
82
83
84
85
86
87
88
89
90
91
92
93
94
  	}
  
  	return ret == 2 ? 0 : -EIO;
  }
  
  static int pcf8583_set_datetime(struct i2c_client *client, struct rtc_time *dt, int datetoo)
  {
  	unsigned char buf[8];
  	int ret, len = 6;
  
  	buf[0] = 0;
  	buf[1] = get_ctrl(client) | 0x80;
  	buf[2] = 0;
fe20ba70a   Adrian Bunk   drivers/rtc/: use...
95
96
97
  	buf[3] = bin2bcd(dt->tm_sec);
  	buf[4] = bin2bcd(dt->tm_min);
  	buf[5] = bin2bcd(dt->tm_hour);
9c0c57057   G. Liakhovetski   [PATCH] drivers/a...
98
99
100
  
  	if (datetoo) {
  		len = 8;
fe20ba70a   Adrian Bunk   drivers/rtc/: use...
101
102
  		buf[6] = bin2bcd(dt->tm_mday) | (dt->tm_year << 6);
  		buf[7] = bin2bcd(dt->tm_mon + 1)  | (dt->tm_wday << 5);
9c0c57057   G. Liakhovetski   [PATCH] drivers/a...
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
  	}
  
  	ret = i2c_master_send(client, (char *)buf, len);
  	if (ret != len)
  		return -EIO;
  
  	buf[1] = get_ctrl(client);
  	ret = i2c_master_send(client, (char *)buf, 2);
  
  	return ret == 2 ? 0 : -EIO;
  }
  
  static int pcf8583_get_ctrl(struct i2c_client *client, unsigned char *ctrl)
  {
  	*ctrl = get_ctrl(client);
  	return 0;
  }
  
  static int pcf8583_set_ctrl(struct i2c_client *client, unsigned char *ctrl)
  {
  	unsigned char buf[2];
  
  	buf[0] = 0;
  	buf[1] = *ctrl;
  	set_ctrl(client, *ctrl);
  
  	return i2c_master_send(client, (char *)buf, 2);
  }
  
  static int pcf8583_read_mem(struct i2c_client *client, struct rtc_mem *mem)
  {
  	unsigned char addr[1];
  	struct i2c_msg msgs[2] = {
  		{
  			.addr = client->addr,
  			.flags = 0,
  			.len = 1,
  			.buf = addr,
  		}, {
  			.addr = client->addr,
  			.flags = I2C_M_RD,
  			.len = mem->nr,
  			.buf = mem->data,
  		}
  	};
  
  	if (mem->loc < 8)
  		return -EINVAL;
  
  	addr[0] = mem->loc;
  
  	return i2c_transfer(client->adapter, msgs, 2) == 2 ? 0 : -EIO;
  }
  
  static int pcf8583_write_mem(struct i2c_client *client, struct rtc_mem *mem)
  {
037e291cc   Jean Delvare   rtc-pcf8583: Don'...
159
160
  	unsigned char buf[9];
  	int ret;
9c0c57057   G. Liakhovetski   [PATCH] drivers/a...
161

037e291cc   Jean Delvare   rtc-pcf8583: Don'...
162
  	if (mem->loc < 8 || mem->nr > 8)
9c0c57057   G. Liakhovetski   [PATCH] drivers/a...
163
  		return -EINVAL;
037e291cc   Jean Delvare   rtc-pcf8583: Don'...
164
165
  	buf[0] = mem->loc;
  	memcpy(buf + 1, mem->data, mem->nr);
9c0c57057   G. Liakhovetski   [PATCH] drivers/a...
166

037e291cc   Jean Delvare   rtc-pcf8583: Don'...
167
168
  	ret = i2c_master_send(client, buf, mem->nr + 1);
  	return ret == mem->nr + 1 ? 0 : -EIO;
9c0c57057   G. Liakhovetski   [PATCH] drivers/a...
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
  }
  
  static int pcf8583_rtc_read_time(struct device *dev, struct rtc_time *tm)
  {
  	struct i2c_client *client = to_i2c_client(dev);
  	unsigned char ctrl, year[2];
  	struct rtc_mem mem = { CMOS_YEAR, sizeof(year), year };
  	int real_year, year_offset, err;
  
  	/*
  	 * Ensure that the RTC is running.
  	 */
  	pcf8583_get_ctrl(client, &ctrl);
  	if (ctrl & (CTRL_STOP | CTRL_HOLD)) {
  		unsigned char new_ctrl = ctrl & ~(CTRL_STOP | CTRL_HOLD);
  
  		printk(KERN_WARNING "RTC: resetting control %02x -> %02x
  ",
  		       ctrl, new_ctrl);
  
  		if ((err = pcf8583_set_ctrl(client, &new_ctrl)) < 0)
  			return err;
  	}
  
  	if (pcf8583_get_datetime(client, tm) ||
  	    pcf8583_read_mem(client, &mem))
  		return -EIO;
  
  	real_year = year[0];
  
  	/*
  	 * The RTC year holds the LSB two bits of the current
  	 * year, which should reflect the LSB two bits of the
  	 * CMOS copy of the year.  Any difference indicates
  	 * that we have to correct the CMOS version.
  	 */
  	year_offset = tm->tm_year - (real_year & 3);
  	if (year_offset < 0)
  		/*
  		 * RTC year wrapped.  Adjust it appropriately.
  		 */
  		year_offset += 4;
0ed8f210e   Russell King   [ARM] rtc-pcf8583...
211
  	tm->tm_year = (real_year + year_offset + year[1] * 100) - 1900;
9c0c57057   G. Liakhovetski   [PATCH] drivers/a...
212
213
214
215
216
217
218
219
220
221
  
  	return 0;
  }
  
  static int pcf8583_rtc_set_time(struct device *dev, struct rtc_time *tm)
  {
  	struct i2c_client *client = to_i2c_client(dev);
  	unsigned char year[2], chk;
  	struct rtc_mem cmos_year  = { CMOS_YEAR, sizeof(year), year };
  	struct rtc_mem cmos_check = { CMOS_CHECKSUM, 1, &chk };
0ed8f210e   Russell King   [ARM] rtc-pcf8583...
222
  	unsigned int proper_year = tm->tm_year + 1900;
9c0c57057   G. Liakhovetski   [PATCH] drivers/a...
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
  	int ret;
  
  	/*
  	 * The RTC's own 2-bit year must reflect the least
  	 * significant two bits of the CMOS year.
  	 */
  
  	ret = pcf8583_set_datetime(client, tm, 1);
  	if (ret)
  		return ret;
  
  	ret = pcf8583_read_mem(client, &cmos_check);
  	if (ret)
  		return ret;
  
  	ret = pcf8583_read_mem(client, &cmos_year);
  	if (ret)
  		return ret;
  
  	chk -= year[1] + year[0];
0ed8f210e   Russell King   [ARM] rtc-pcf8583...
243
244
  	year[1] = proper_year / 100;
  	year[0] = proper_year % 100;
9c0c57057   G. Liakhovetski   [PATCH] drivers/a...
245
246
247
248
249
250
251
252
253
254
255
256
  
  	chk += year[1] + year[0];
  
  	ret = pcf8583_write_mem(client, &cmos_year);
  
  	if (ret)
  		return ret;
  
  	ret = pcf8583_write_mem(client, &cmos_check);
  
  	return ret;
  }
ff8371ac9   David Brownell   [PATCH] constify ...
257
  static const struct rtc_class_ops pcf8583_rtc_ops = {
9c0c57057   G. Liakhovetski   [PATCH] drivers/a...
258
259
260
  	.read_time	= pcf8583_rtc_read_time,
  	.set_time	= pcf8583_rtc_set_time,
  };
02bb584f3   Wolfram Sang   rtc: convert the ...
261
262
  static int pcf8583_probe(struct i2c_client *client,
  				const struct i2c_device_id *id)
9c0c57057   G. Liakhovetski   [PATCH] drivers/a...
263
  {
02bb584f3   Wolfram Sang   rtc: convert the ...
264
  	struct pcf8583 *pcf8583;
9c0c57057   G. Liakhovetski   [PATCH] drivers/a...
265
  	int err;
9c0c57057   G. Liakhovetski   [PATCH] drivers/a...
266

02bb584f3   Wolfram Sang   rtc: convert the ...
267
268
  	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
  		return -ENODEV;
c018664c5   Jean Delvare   rtc-pcf8583: Chec...
269

02bb584f3   Wolfram Sang   rtc: convert the ...
270
271
  	pcf8583 = kzalloc(sizeof(struct pcf8583), GFP_KERNEL);
  	if (!pcf8583)
9c0c57057   G. Liakhovetski   [PATCH] drivers/a...
272
  		return -ENOMEM;
b74d2caa6   Alessandro Zummo   rtc: fix driver d...
273
  	i2c_set_clientdata(client, pcf8583);
02bb584f3   Wolfram Sang   rtc: convert the ...
274
275
  	pcf8583->rtc = rtc_device_register(pcf8583_driver.driver.name,
  			&client->dev, &pcf8583_rtc_ops, THIS_MODULE);
9c0c57057   G. Liakhovetski   [PATCH] drivers/a...
276

02bb584f3   Wolfram Sang   rtc: convert the ...
277
278
  	if (IS_ERR(pcf8583->rtc)) {
  		err = PTR_ERR(pcf8583->rtc);
9c0c57057   G. Liakhovetski   [PATCH] drivers/a...
279
280
  		goto exit_kfree;
  	}
02bb584f3   Wolfram Sang   rtc: convert the ...
281
  	return 0;
9c0c57057   G. Liakhovetski   [PATCH] drivers/a...
282

02bb584f3   Wolfram Sang   rtc: convert the ...
283
284
285
286
  exit_kfree:
  	kfree(pcf8583);
  	return err;
  }
9c0c57057   G. Liakhovetski   [PATCH] drivers/a...
287

02bb584f3   Wolfram Sang   rtc: convert the ...
288
289
290
  static int __devexit pcf8583_remove(struct i2c_client *client)
  {
  	struct pcf8583 *pcf8583 = i2c_get_clientdata(client);
9c0c57057   G. Liakhovetski   [PATCH] drivers/a...
291

02bb584f3   Wolfram Sang   rtc: convert the ...
292
293
294
  	if (pcf8583->rtc)
  		rtc_device_unregister(pcf8583->rtc);
  	kfree(pcf8583);
9c0c57057   G. Liakhovetski   [PATCH] drivers/a...
295
  	return 0;
02bb584f3   Wolfram Sang   rtc: convert the ...
296
  }
9c0c57057   G. Liakhovetski   [PATCH] drivers/a...
297

02bb584f3   Wolfram Sang   rtc: convert the ...
298
299
300
301
302
  static const struct i2c_device_id pcf8583_id[] = {
  	{ "pcf8583", 0 },
  	{ }
  };
  MODULE_DEVICE_TABLE(i2c, pcf8583_id);
9c0c57057   G. Liakhovetski   [PATCH] drivers/a...
303

02bb584f3   Wolfram Sang   rtc: convert the ...
304
305
306
307
308
309
310
311
312
  static struct i2c_driver pcf8583_driver = {
  	.driver = {
  		.name	= "pcf8583",
  		.owner	= THIS_MODULE,
  	},
  	.probe		= pcf8583_probe,
  	.remove		= __devexit_p(pcf8583_remove),
  	.id_table	= pcf8583_id,
  };
9c0c57057   G. Liakhovetski   [PATCH] drivers/a...
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
  
  static __init int pcf8583_init(void)
  {
  	return i2c_add_driver(&pcf8583_driver);
  }
  
  static __exit void pcf8583_exit(void)
  {
  	i2c_del_driver(&pcf8583_driver);
  }
  
  module_init(pcf8583_init);
  module_exit(pcf8583_exit);
  
  MODULE_AUTHOR("Russell King");
  MODULE_DESCRIPTION("PCF8583 I2C RTC driver");
  MODULE_LICENSE("GPL");