fix a kmap leak in virtio_console
[linux/fpc-iii.git] / drivers / rtc / rtc-pcf8583.c
blobc2639845186b3f8433551116074ae19197ee1866
1 /*
2 * drivers/rtc/rtc-pcf8583.c
4 * Copyright (C) 2000 Russell King
5 * Copyright (C) 2008 Wolfram Sang & Juergen Beisert, Pengutronix
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * Driver for PCF8583 RTC & RAM chip
13 * Converted to the generic RTC susbsystem by G. Liakhovetski (2006)
15 #include <linux/module.h>
16 #include <linux/i2c.h>
17 #include <linux/slab.h>
18 #include <linux/rtc.h>
19 #include <linux/init.h>
20 #include <linux/err.h>
21 #include <linux/errno.h>
22 #include <linux/bcd.h>
24 struct rtc_mem {
25 unsigned int loc;
26 unsigned int nr;
27 unsigned char *data;
30 struct pcf8583 {
31 struct rtc_device *rtc;
32 unsigned char ctrl;
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
44 static struct i2c_driver pcf8583_driver;
46 #define get_ctrl(x) ((struct pcf8583 *)i2c_get_clientdata(x))->ctrl
47 #define set_ctrl(x, v) get_ctrl(x) = v
49 #define CMOS_YEAR (64 + 128)
50 #define CMOS_CHECKSUM (63)
52 static int pcf8583_get_datetime(struct i2c_client *client, struct rtc_time *dt)
54 unsigned char buf[8], addr[1] = { 1 };
55 struct i2c_msg msgs[2] = {
57 .addr = client->addr,
58 .flags = 0,
59 .len = 1,
60 .buf = addr,
61 }, {
62 .addr = client->addr,
63 .flags = I2C_M_RD,
64 .len = 6,
65 .buf = buf,
68 int ret;
70 memset(buf, 0, sizeof(buf));
72 ret = i2c_transfer(client->adapter, msgs, 2);
73 if (ret == 2) {
74 dt->tm_year = buf[4] >> 6;
75 dt->tm_wday = buf[5] >> 5;
77 buf[4] &= 0x3f;
78 buf[5] &= 0x1f;
80 dt->tm_sec = bcd2bin(buf[1]);
81 dt->tm_min = bcd2bin(buf[2]);
82 dt->tm_hour = bcd2bin(buf[3]);
83 dt->tm_mday = bcd2bin(buf[4]);
84 dt->tm_mon = bcd2bin(buf[5]) - 1;
87 return ret == 2 ? 0 : -EIO;
90 static int pcf8583_set_datetime(struct i2c_client *client, struct rtc_time *dt, int datetoo)
92 unsigned char buf[8];
93 int ret, len = 6;
95 buf[0] = 0;
96 buf[1] = get_ctrl(client) | 0x80;
97 buf[2] = 0;
98 buf[3] = bin2bcd(dt->tm_sec);
99 buf[4] = bin2bcd(dt->tm_min);
100 buf[5] = bin2bcd(dt->tm_hour);
102 if (datetoo) {
103 len = 8;
104 buf[6] = bin2bcd(dt->tm_mday) | (dt->tm_year << 6);
105 buf[7] = bin2bcd(dt->tm_mon + 1) | (dt->tm_wday << 5);
108 ret = i2c_master_send(client, (char *)buf, len);
109 if (ret != len)
110 return -EIO;
112 buf[1] = get_ctrl(client);
113 ret = i2c_master_send(client, (char *)buf, 2);
115 return ret == 2 ? 0 : -EIO;
118 static int pcf8583_get_ctrl(struct i2c_client *client, unsigned char *ctrl)
120 *ctrl = get_ctrl(client);
121 return 0;
124 static int pcf8583_set_ctrl(struct i2c_client *client, unsigned char *ctrl)
126 unsigned char buf[2];
128 buf[0] = 0;
129 buf[1] = *ctrl;
130 set_ctrl(client, *ctrl);
132 return i2c_master_send(client, (char *)buf, 2);
135 static int pcf8583_read_mem(struct i2c_client *client, struct rtc_mem *mem)
137 unsigned char addr[1];
138 struct i2c_msg msgs[2] = {
140 .addr = client->addr,
141 .flags = 0,
142 .len = 1,
143 .buf = addr,
144 }, {
145 .addr = client->addr,
146 .flags = I2C_M_RD,
147 .len = mem->nr,
148 .buf = mem->data,
152 if (mem->loc < 8)
153 return -EINVAL;
155 addr[0] = mem->loc;
157 return i2c_transfer(client->adapter, msgs, 2) == 2 ? 0 : -EIO;
160 static int pcf8583_write_mem(struct i2c_client *client, struct rtc_mem *mem)
162 unsigned char buf[9];
163 int ret;
165 if (mem->loc < 8 || mem->nr > 8)
166 return -EINVAL;
168 buf[0] = mem->loc;
169 memcpy(buf + 1, mem->data, mem->nr);
171 ret = i2c_master_send(client, buf, mem->nr + 1);
172 return ret == mem->nr + 1 ? 0 : -EIO;
175 static int pcf8583_rtc_read_time(struct device *dev, struct rtc_time *tm)
177 struct i2c_client *client = to_i2c_client(dev);
178 unsigned char ctrl, year[2];
179 struct rtc_mem mem = { CMOS_YEAR, sizeof(year), year };
180 int real_year, year_offset, err;
183 * Ensure that the RTC is running.
185 pcf8583_get_ctrl(client, &ctrl);
186 if (ctrl & (CTRL_STOP | CTRL_HOLD)) {
187 unsigned char new_ctrl = ctrl & ~(CTRL_STOP | CTRL_HOLD);
189 dev_warn(dev, "resetting control %02x -> %02x\n",
190 ctrl, new_ctrl);
192 err = pcf8583_set_ctrl(client, &new_ctrl);
193 if (err < 0)
194 return err;
197 if (pcf8583_get_datetime(client, tm) ||
198 pcf8583_read_mem(client, &mem))
199 return -EIO;
201 real_year = year[0];
204 * The RTC year holds the LSB two bits of the current
205 * year, which should reflect the LSB two bits of the
206 * CMOS copy of the year. Any difference indicates
207 * that we have to correct the CMOS version.
209 year_offset = tm->tm_year - (real_year & 3);
210 if (year_offset < 0)
212 * RTC year wrapped. Adjust it appropriately.
214 year_offset += 4;
216 tm->tm_year = (real_year + year_offset + year[1] * 100) - 1900;
218 return 0;
221 static int pcf8583_rtc_set_time(struct device *dev, struct rtc_time *tm)
223 struct i2c_client *client = to_i2c_client(dev);
224 unsigned char year[2], chk;
225 struct rtc_mem cmos_year = { CMOS_YEAR, sizeof(year), year };
226 struct rtc_mem cmos_check = { CMOS_CHECKSUM, 1, &chk };
227 unsigned int proper_year = tm->tm_year + 1900;
228 int ret;
231 * The RTC's own 2-bit year must reflect the least
232 * significant two bits of the CMOS year.
235 ret = pcf8583_set_datetime(client, tm, 1);
236 if (ret)
237 return ret;
239 ret = pcf8583_read_mem(client, &cmos_check);
240 if (ret)
241 return ret;
243 ret = pcf8583_read_mem(client, &cmos_year);
244 if (ret)
245 return ret;
247 chk -= year[1] + year[0];
249 year[1] = proper_year / 100;
250 year[0] = proper_year % 100;
252 chk += year[1] + year[0];
254 ret = pcf8583_write_mem(client, &cmos_year);
256 if (ret)
257 return ret;
259 ret = pcf8583_write_mem(client, &cmos_check);
261 return ret;
264 static const struct rtc_class_ops pcf8583_rtc_ops = {
265 .read_time = pcf8583_rtc_read_time,
266 .set_time = pcf8583_rtc_set_time,
269 static int pcf8583_probe(struct i2c_client *client,
270 const struct i2c_device_id *id)
272 struct pcf8583 *pcf8583;
274 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
275 return -ENODEV;
277 pcf8583 = devm_kzalloc(&client->dev, sizeof(struct pcf8583),
278 GFP_KERNEL);
279 if (!pcf8583)
280 return -ENOMEM;
282 i2c_set_clientdata(client, pcf8583);
284 pcf8583->rtc = devm_rtc_device_register(&client->dev,
285 pcf8583_driver.driver.name,
286 &pcf8583_rtc_ops, THIS_MODULE);
288 return PTR_ERR_OR_ZERO(pcf8583->rtc);
291 static const struct i2c_device_id pcf8583_id[] = {
292 { "pcf8583", 0 },
295 MODULE_DEVICE_TABLE(i2c, pcf8583_id);
297 static struct i2c_driver pcf8583_driver = {
298 .driver = {
299 .name = "pcf8583",
300 .owner = THIS_MODULE,
302 .probe = pcf8583_probe,
303 .id_table = pcf8583_id,
306 module_i2c_driver(pcf8583_driver);
308 MODULE_AUTHOR("Russell King");
309 MODULE_DESCRIPTION("PCF8583 I2C RTC driver");
310 MODULE_LICENSE("GPL");