1 // SPDX-License-Identifier: GPL-2.0-only
3 * drivers/rtc/rtc-pcf8583.c
5 * Copyright (C) 2000 Russell King
6 * Copyright (C) 2008 Wolfram Sang & Juergen Beisert, Pengutronix
8 * Driver for PCF8583 RTC & RAM chip
10 * Converted to the generic RTC susbsystem by G. Liakhovetski (2006)
12 #include <linux/module.h>
13 #include <linux/i2c.h>
14 #include <linux/slab.h>
15 #include <linux/rtc.h>
16 #include <linux/init.h>
17 #include <linux/err.h>
18 #include <linux/errno.h>
19 #include <linux/bcd.h>
28 struct rtc_device *rtc;
32 #define CTRL_STOP 0x80
33 #define CTRL_HOLD 0x40
34 #define CTRL_32KHZ 0x00
35 #define CTRL_MASK 0x08
36 #define CTRL_ALARMEN 0x04
37 #define CTRL_ALARM 0x02
38 #define CTRL_TIMER 0x01
41 static struct i2c_driver pcf8583_driver;
43 #define get_ctrl(x) ((struct pcf8583 *)i2c_get_clientdata(x))->ctrl
44 #define set_ctrl(x, v) get_ctrl(x) = v
46 #define CMOS_YEAR (64 + 128)
47 #define CMOS_CHECKSUM (63)
49 static int pcf8583_get_datetime(struct i2c_client *client, struct rtc_time *dt)
51 unsigned char buf[8], addr[1] = { 1 };
52 struct i2c_msg msgs[2] = {
67 memset(buf, 0, sizeof(buf));
69 ret = i2c_transfer(client->adapter, msgs, 2);
71 dt->tm_year = buf[4] >> 6;
72 dt->tm_wday = buf[5] >> 5;
77 dt->tm_sec = bcd2bin(buf[1]);
78 dt->tm_min = bcd2bin(buf[2]);
79 dt->tm_hour = bcd2bin(buf[3]);
80 dt->tm_mday = bcd2bin(buf[4]);
81 dt->tm_mon = bcd2bin(buf[5]) - 1;
84 return ret == 2 ? 0 : -EIO;
87 static int pcf8583_set_datetime(struct i2c_client *client, struct rtc_time *dt, int datetoo)
93 buf[1] = get_ctrl(client) | 0x80;
95 buf[3] = bin2bcd(dt->tm_sec);
96 buf[4] = bin2bcd(dt->tm_min);
97 buf[5] = bin2bcd(dt->tm_hour);
101 buf[6] = bin2bcd(dt->tm_mday) | (dt->tm_year << 6);
102 buf[7] = bin2bcd(dt->tm_mon + 1) | (dt->tm_wday << 5);
105 ret = i2c_master_send(client, (char *)buf, len);
109 buf[1] = get_ctrl(client);
110 ret = i2c_master_send(client, (char *)buf, 2);
112 return ret == 2 ? 0 : -EIO;
115 static int pcf8583_get_ctrl(struct i2c_client *client, unsigned char *ctrl)
117 *ctrl = get_ctrl(client);
121 static int pcf8583_set_ctrl(struct i2c_client *client, unsigned char *ctrl)
123 unsigned char buf[2];
127 set_ctrl(client, *ctrl);
129 return i2c_master_send(client, (char *)buf, 2);
132 static int pcf8583_read_mem(struct i2c_client *client, struct rtc_mem *mem)
134 unsigned char addr[1];
135 struct i2c_msg msgs[2] = {
137 .addr = client->addr,
142 .addr = client->addr,
154 return i2c_transfer(client->adapter, msgs, 2) == 2 ? 0 : -EIO;
157 static int pcf8583_write_mem(struct i2c_client *client, struct rtc_mem *mem)
159 unsigned char buf[9];
162 if (mem->loc < 8 || mem->nr > 8)
166 memcpy(buf + 1, mem->data, mem->nr);
168 ret = i2c_master_send(client, buf, mem->nr + 1);
169 return ret == mem->nr + 1 ? 0 : -EIO;
172 static int pcf8583_rtc_read_time(struct device *dev, struct rtc_time *tm)
174 struct i2c_client *client = to_i2c_client(dev);
175 unsigned char ctrl, year[2];
176 struct rtc_mem mem = {
181 int real_year, year_offset, err;
184 * Ensure that the RTC is running.
186 pcf8583_get_ctrl(client, &ctrl);
187 if (ctrl & (CTRL_STOP | CTRL_HOLD)) {
188 unsigned char new_ctrl = ctrl & ~(CTRL_STOP | CTRL_HOLD);
190 dev_warn(dev, "resetting control %02x -> %02x\n",
193 err = pcf8583_set_ctrl(client, &new_ctrl);
198 if (pcf8583_get_datetime(client, tm) ||
199 pcf8583_read_mem(client, &mem))
205 * The RTC year holds the LSB two bits of the current
206 * year, which should reflect the LSB two bits of the
207 * CMOS copy of the year. Any difference indicates
208 * that we have to correct the CMOS version.
210 year_offset = tm->tm_year - (real_year & 3);
213 * RTC year wrapped. Adjust it appropriately.
217 tm->tm_year = (real_year + year_offset + year[1] * 100) - 1900;
222 static int pcf8583_rtc_set_time(struct device *dev, struct rtc_time *tm)
224 struct i2c_client *client = to_i2c_client(dev);
225 unsigned char year[2], chk;
226 struct rtc_mem cmos_year = {
231 struct rtc_mem cmos_check = {
232 .loc = CMOS_CHECKSUM,
236 unsigned int proper_year = tm->tm_year + 1900;
240 * The RTC's own 2-bit year must reflect the least
241 * significant two bits of the CMOS year.
244 ret = pcf8583_set_datetime(client, tm, 1);
248 ret = pcf8583_read_mem(client, &cmos_check);
252 ret = pcf8583_read_mem(client, &cmos_year);
256 chk -= year[1] + year[0];
258 year[1] = proper_year / 100;
259 year[0] = proper_year % 100;
261 chk += year[1] + year[0];
263 ret = pcf8583_write_mem(client, &cmos_year);
268 ret = pcf8583_write_mem(client, &cmos_check);
273 static const struct rtc_class_ops pcf8583_rtc_ops = {
274 .read_time = pcf8583_rtc_read_time,
275 .set_time = pcf8583_rtc_set_time,
278 static int pcf8583_probe(struct i2c_client *client)
280 struct pcf8583 *pcf8583;
282 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
285 pcf8583 = devm_kzalloc(&client->dev, sizeof(struct pcf8583),
290 i2c_set_clientdata(client, pcf8583);
292 pcf8583->rtc = devm_rtc_device_register(&client->dev,
293 pcf8583_driver.driver.name,
294 &pcf8583_rtc_ops, THIS_MODULE);
296 return PTR_ERR_OR_ZERO(pcf8583->rtc);
299 static const struct i2c_device_id pcf8583_id[] = {
303 MODULE_DEVICE_TABLE(i2c, pcf8583_id);
305 static struct i2c_driver pcf8583_driver = {
309 .probe_new = pcf8583_probe,
310 .id_table = pcf8583_id,
313 module_i2c_driver(pcf8583_driver);
315 MODULE_AUTHOR("Russell King");
316 MODULE_DESCRIPTION("PCF8583 I2C RTC driver");
317 MODULE_LICENSE("GPL");