Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  *  drivers/rtc/rtc-pcf8583.c
0004  *
0005  *  Copyright (C) 2000 Russell King
0006  *  Copyright (C) 2008 Wolfram Sang & Juergen Beisert, Pengutronix
0007  *
0008  *  Driver for PCF8583 RTC & RAM chip
0009  *
0010  *  Converted to the generic RTC susbsystem by G. Liakhovetski (2006)
0011  */
0012 #include <linux/module.h>
0013 #include <linux/i2c.h>
0014 #include <linux/slab.h>
0015 #include <linux/rtc.h>
0016 #include <linux/init.h>
0017 #include <linux/err.h>
0018 #include <linux/errno.h>
0019 #include <linux/bcd.h>
0020 
0021 struct rtc_mem {
0022     unsigned int    loc;
0023     unsigned int    nr;
0024     unsigned char   *data;
0025 };
0026 
0027 struct pcf8583 {
0028     struct rtc_device *rtc;
0029     unsigned char ctrl;
0030 };
0031 
0032 #define CTRL_STOP   0x80
0033 #define CTRL_HOLD   0x40
0034 #define CTRL_32KHZ  0x00
0035 #define CTRL_MASK   0x08
0036 #define CTRL_ALARMEN    0x04
0037 #define CTRL_ALARM  0x02
0038 #define CTRL_TIMER  0x01
0039 
0040 
0041 static struct i2c_driver pcf8583_driver;
0042 
0043 #define get_ctrl(x)    ((struct pcf8583 *)i2c_get_clientdata(x))->ctrl
0044 #define set_ctrl(x, v) get_ctrl(x) = v
0045 
0046 #define CMOS_YEAR   (64 + 128)
0047 #define CMOS_CHECKSUM   (63)
0048 
0049 static int pcf8583_get_datetime(struct i2c_client *client, struct rtc_time *dt)
0050 {
0051     unsigned char buf[8], addr[1] = { 1 };
0052     struct i2c_msg msgs[2] = {
0053         {
0054             .addr = client->addr,
0055             .flags = 0,
0056             .len = 1,
0057             .buf = addr,
0058         }, {
0059             .addr = client->addr,
0060             .flags = I2C_M_RD,
0061             .len = 6,
0062             .buf = buf,
0063         }
0064     };
0065     int ret;
0066 
0067     memset(buf, 0, sizeof(buf));
0068 
0069     ret = i2c_transfer(client->adapter, msgs, 2);
0070     if (ret == 2) {
0071         dt->tm_year = buf[4] >> 6;
0072         dt->tm_wday = buf[5] >> 5;
0073 
0074         buf[4] &= 0x3f;
0075         buf[5] &= 0x1f;
0076 
0077         dt->tm_sec = bcd2bin(buf[1]);
0078         dt->tm_min = bcd2bin(buf[2]);
0079         dt->tm_hour = bcd2bin(buf[3]);
0080         dt->tm_mday = bcd2bin(buf[4]);
0081         dt->tm_mon = bcd2bin(buf[5]) - 1;
0082     }
0083 
0084     return ret == 2 ? 0 : -EIO;
0085 }
0086 
0087 static int pcf8583_set_datetime(struct i2c_client *client, struct rtc_time *dt, int datetoo)
0088 {
0089     unsigned char buf[8];
0090     int ret, len = 6;
0091 
0092     buf[0] = 0;
0093     buf[1] = get_ctrl(client) | 0x80;
0094     buf[2] = 0;
0095     buf[3] = bin2bcd(dt->tm_sec);
0096     buf[4] = bin2bcd(dt->tm_min);
0097     buf[5] = bin2bcd(dt->tm_hour);
0098 
0099     if (datetoo) {
0100         len = 8;
0101         buf[6] = bin2bcd(dt->tm_mday) | (dt->tm_year << 6);
0102         buf[7] = bin2bcd(dt->tm_mon + 1)  | (dt->tm_wday << 5);
0103     }
0104 
0105     ret = i2c_master_send(client, (char *)buf, len);
0106     if (ret != len)
0107         return -EIO;
0108 
0109     buf[1] = get_ctrl(client);
0110     ret = i2c_master_send(client, (char *)buf, 2);
0111 
0112     return ret == 2 ? 0 : -EIO;
0113 }
0114 
0115 static int pcf8583_get_ctrl(struct i2c_client *client, unsigned char *ctrl)
0116 {
0117     *ctrl = get_ctrl(client);
0118     return 0;
0119 }
0120 
0121 static int pcf8583_set_ctrl(struct i2c_client *client, unsigned char *ctrl)
0122 {
0123     unsigned char buf[2];
0124 
0125     buf[0] = 0;
0126     buf[1] = *ctrl;
0127     set_ctrl(client, *ctrl);
0128 
0129     return i2c_master_send(client, (char *)buf, 2);
0130 }
0131 
0132 static int pcf8583_read_mem(struct i2c_client *client, struct rtc_mem *mem)
0133 {
0134     unsigned char addr[1];
0135     struct i2c_msg msgs[2] = {
0136         {
0137             .addr = client->addr,
0138             .flags = 0,
0139             .len = 1,
0140             .buf = addr,
0141         }, {
0142             .addr = client->addr,
0143             .flags = I2C_M_RD,
0144             .len = mem->nr,
0145             .buf = mem->data,
0146         }
0147     };
0148 
0149     if (mem->loc < 8)
0150         return -EINVAL;
0151 
0152     addr[0] = mem->loc;
0153 
0154     return i2c_transfer(client->adapter, msgs, 2) == 2 ? 0 : -EIO;
0155 }
0156 
0157 static int pcf8583_write_mem(struct i2c_client *client, struct rtc_mem *mem)
0158 {
0159     unsigned char buf[9];
0160     int ret;
0161 
0162     if (mem->loc < 8 || mem->nr > 8)
0163         return -EINVAL;
0164 
0165     buf[0] = mem->loc;
0166     memcpy(buf + 1, mem->data, mem->nr);
0167 
0168     ret = i2c_master_send(client, buf, mem->nr + 1);
0169     return ret == mem->nr + 1 ? 0 : -EIO;
0170 }
0171 
0172 static int pcf8583_rtc_read_time(struct device *dev, struct rtc_time *tm)
0173 {
0174     struct i2c_client *client = to_i2c_client(dev);
0175     unsigned char ctrl, year[2];
0176     struct rtc_mem mem = {
0177         .loc = CMOS_YEAR,
0178         .nr = sizeof(year),
0179         .data = year
0180     };
0181     int real_year, year_offset, err;
0182 
0183     /*
0184      * Ensure that the RTC is running.
0185      */
0186     pcf8583_get_ctrl(client, &ctrl);
0187     if (ctrl & (CTRL_STOP | CTRL_HOLD)) {
0188         unsigned char new_ctrl = ctrl & ~(CTRL_STOP | CTRL_HOLD);
0189 
0190         dev_warn(dev, "resetting control %02x -> %02x\n",
0191             ctrl, new_ctrl);
0192 
0193         err = pcf8583_set_ctrl(client, &new_ctrl);
0194         if (err < 0)
0195             return err;
0196     }
0197 
0198     if (pcf8583_get_datetime(client, tm) ||
0199         pcf8583_read_mem(client, &mem))
0200         return -EIO;
0201 
0202     real_year = year[0];
0203 
0204     /*
0205      * The RTC year holds the LSB two bits of the current
0206      * year, which should reflect the LSB two bits of the
0207      * CMOS copy of the year.  Any difference indicates
0208      * that we have to correct the CMOS version.
0209      */
0210     year_offset = tm->tm_year - (real_year & 3);
0211     if (year_offset < 0)
0212         /*
0213          * RTC year wrapped.  Adjust it appropriately.
0214          */
0215         year_offset += 4;
0216 
0217     tm->tm_year = (real_year + year_offset + year[1] * 100) - 1900;
0218 
0219     return 0;
0220 }
0221 
0222 static int pcf8583_rtc_set_time(struct device *dev, struct rtc_time *tm)
0223 {
0224     struct i2c_client *client = to_i2c_client(dev);
0225     unsigned char year[2], chk;
0226     struct rtc_mem cmos_year  = {
0227         .loc = CMOS_YEAR,
0228         .nr = sizeof(year),
0229         .data = year
0230     };
0231     struct rtc_mem cmos_check = {
0232         .loc = CMOS_CHECKSUM,
0233         .nr = 1,
0234         .data = &chk
0235     };
0236     unsigned int proper_year = tm->tm_year + 1900;
0237     int ret;
0238 
0239     /*
0240      * The RTC's own 2-bit year must reflect the least
0241      * significant two bits of the CMOS year.
0242      */
0243 
0244     ret = pcf8583_set_datetime(client, tm, 1);
0245     if (ret)
0246         return ret;
0247 
0248     ret = pcf8583_read_mem(client, &cmos_check);
0249     if (ret)
0250         return ret;
0251 
0252     ret = pcf8583_read_mem(client, &cmos_year);
0253     if (ret)
0254         return ret;
0255 
0256     chk -= year[1] + year[0];
0257 
0258     year[1] = proper_year / 100;
0259     year[0] = proper_year % 100;
0260 
0261     chk += year[1] + year[0];
0262 
0263     ret = pcf8583_write_mem(client, &cmos_year);
0264 
0265     if (ret)
0266         return ret;
0267 
0268     ret = pcf8583_write_mem(client, &cmos_check);
0269 
0270     return ret;
0271 }
0272 
0273 static const struct rtc_class_ops pcf8583_rtc_ops = {
0274     .read_time  = pcf8583_rtc_read_time,
0275     .set_time   = pcf8583_rtc_set_time,
0276 };
0277 
0278 static int pcf8583_probe(struct i2c_client *client)
0279 {
0280     struct pcf8583 *pcf8583;
0281 
0282     if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
0283         return -ENODEV;
0284 
0285     pcf8583 = devm_kzalloc(&client->dev, sizeof(struct pcf8583),
0286                 GFP_KERNEL);
0287     if (!pcf8583)
0288         return -ENOMEM;
0289 
0290     i2c_set_clientdata(client, pcf8583);
0291 
0292     pcf8583->rtc = devm_rtc_device_register(&client->dev,
0293                 pcf8583_driver.driver.name,
0294                 &pcf8583_rtc_ops, THIS_MODULE);
0295 
0296     return PTR_ERR_OR_ZERO(pcf8583->rtc);
0297 }
0298 
0299 static const struct i2c_device_id pcf8583_id[] = {
0300     { "pcf8583", 0 },
0301     { }
0302 };
0303 MODULE_DEVICE_TABLE(i2c, pcf8583_id);
0304 
0305 static struct i2c_driver pcf8583_driver = {
0306     .driver = {
0307         .name   = "pcf8583",
0308     },
0309     .probe_new  = pcf8583_probe,
0310     .id_table   = pcf8583_id,
0311 };
0312 
0313 module_i2c_driver(pcf8583_driver);
0314 
0315 MODULE_AUTHOR("Russell King");
0316 MODULE_DESCRIPTION("PCF8583 I2C RTC driver");
0317 MODULE_LICENSE("GPL");