1*18cb6368SRenaud Cerrato /* 2*18cb6368SRenaud Cerrato * An I2C driver for the NXP PCF2127 RTC 3*18cb6368SRenaud Cerrato * Copyright 2013 Til-Technologies 4*18cb6368SRenaud Cerrato * 5*18cb6368SRenaud Cerrato * Author: Renaud Cerrato <r.cerrato@til-technologies.fr> 6*18cb6368SRenaud Cerrato * 7*18cb6368SRenaud Cerrato * based on the other drivers in this same directory. 8*18cb6368SRenaud Cerrato * 9*18cb6368SRenaud Cerrato * http://www.nxp.com/documents/data_sheet/PCF2127AT.pdf 10*18cb6368SRenaud Cerrato * 11*18cb6368SRenaud Cerrato * This program is free software; you can redistribute it and/or modify 12*18cb6368SRenaud Cerrato * it under the terms of the GNU General Public License version 2 as 13*18cb6368SRenaud Cerrato * published by the Free Software Foundation. 14*18cb6368SRenaud Cerrato */ 15*18cb6368SRenaud Cerrato 16*18cb6368SRenaud Cerrato #include <linux/i2c.h> 17*18cb6368SRenaud Cerrato #include <linux/bcd.h> 18*18cb6368SRenaud Cerrato #include <linux/rtc.h> 19*18cb6368SRenaud Cerrato #include <linux/slab.h> 20*18cb6368SRenaud Cerrato #include <linux/module.h> 21*18cb6368SRenaud Cerrato #include <linux/of.h> 22*18cb6368SRenaud Cerrato 23*18cb6368SRenaud Cerrato #define DRV_VERSION "0.0.1" 24*18cb6368SRenaud Cerrato 25*18cb6368SRenaud Cerrato #define PCF2127_REG_CTRL1 (0x00) /* Control Register 1 */ 26*18cb6368SRenaud Cerrato #define PCF2127_REG_CTRL2 (0x01) /* Control Register 2 */ 27*18cb6368SRenaud Cerrato #define PCF2127_REG_CTRL3 (0x02) /* Control Register 3 */ 28*18cb6368SRenaud Cerrato #define PCF2127_REG_SC (0x03) /* datetime */ 29*18cb6368SRenaud Cerrato #define PCF2127_REG_MN (0x04) 30*18cb6368SRenaud Cerrato #define PCF2127_REG_HR (0x05) 31*18cb6368SRenaud Cerrato #define PCF2127_REG_DM (0x06) 32*18cb6368SRenaud Cerrato #define PCF2127_REG_DW (0x07) 33*18cb6368SRenaud Cerrato #define PCF2127_REG_MO (0x08) 34*18cb6368SRenaud Cerrato #define PCF2127_REG_YR (0x09) 35*18cb6368SRenaud Cerrato 36*18cb6368SRenaud Cerrato static struct i2c_driver pcf2127_driver; 37*18cb6368SRenaud Cerrato 38*18cb6368SRenaud Cerrato struct pcf2127 { 39*18cb6368SRenaud Cerrato struct rtc_device *rtc; 40*18cb6368SRenaud Cerrato int voltage_low; /* indicates if a low_voltage was detected */ 41*18cb6368SRenaud Cerrato }; 42*18cb6368SRenaud Cerrato 43*18cb6368SRenaud Cerrato /* 44*18cb6368SRenaud Cerrato * In the routines that deal directly with the pcf2127 hardware, we use 45*18cb6368SRenaud Cerrato * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch. 46*18cb6368SRenaud Cerrato */ 47*18cb6368SRenaud Cerrato static int pcf2127_get_datetime(struct i2c_client *client, struct rtc_time *tm) 48*18cb6368SRenaud Cerrato { 49*18cb6368SRenaud Cerrato struct pcf2127 *pcf2127 = i2c_get_clientdata(client); 50*18cb6368SRenaud Cerrato unsigned char buf[10] = { PCF2127_REG_CTRL1 }; 51*18cb6368SRenaud Cerrato 52*18cb6368SRenaud Cerrato /* read registers */ 53*18cb6368SRenaud Cerrato if (i2c_master_send(client, buf, 1) != 1 || 54*18cb6368SRenaud Cerrato i2c_master_recv(client, buf, sizeof(buf)) != sizeof(buf)) { 55*18cb6368SRenaud Cerrato dev_err(&client->dev, "%s: read error\n", __func__); 56*18cb6368SRenaud Cerrato return -EIO; 57*18cb6368SRenaud Cerrato } 58*18cb6368SRenaud Cerrato 59*18cb6368SRenaud Cerrato if (buf[PCF2127_REG_CTRL3] & 0x04) { 60*18cb6368SRenaud Cerrato pcf2127->voltage_low = 1; 61*18cb6368SRenaud Cerrato dev_info(&client->dev, 62*18cb6368SRenaud Cerrato "low voltage detected, date/time is not reliable.\n"); 63*18cb6368SRenaud Cerrato } 64*18cb6368SRenaud Cerrato 65*18cb6368SRenaud Cerrato dev_dbg(&client->dev, 66*18cb6368SRenaud Cerrato "%s: raw data is cr1=%02x, cr2=%02x, cr3=%02x, " 67*18cb6368SRenaud Cerrato "sec=%02x, min=%02x, hr=%02x, " 68*18cb6368SRenaud Cerrato "mday=%02x, wday=%02x, mon=%02x, year=%02x\n", 69*18cb6368SRenaud Cerrato __func__, 70*18cb6368SRenaud Cerrato buf[0], buf[1], buf[2], 71*18cb6368SRenaud Cerrato buf[3], buf[4], buf[5], 72*18cb6368SRenaud Cerrato buf[6], buf[7], buf[8], buf[9]); 73*18cb6368SRenaud Cerrato 74*18cb6368SRenaud Cerrato 75*18cb6368SRenaud Cerrato tm->tm_sec = bcd2bin(buf[PCF2127_REG_SC] & 0x7F); 76*18cb6368SRenaud Cerrato tm->tm_min = bcd2bin(buf[PCF2127_REG_MN] & 0x7F); 77*18cb6368SRenaud Cerrato tm->tm_hour = bcd2bin(buf[PCF2127_REG_HR] & 0x3F); /* rtc hr 0-23 */ 78*18cb6368SRenaud Cerrato tm->tm_mday = bcd2bin(buf[PCF2127_REG_DM] & 0x3F); 79*18cb6368SRenaud Cerrato tm->tm_wday = buf[PCF2127_REG_DW] & 0x07; 80*18cb6368SRenaud Cerrato tm->tm_mon = bcd2bin(buf[PCF2127_REG_MO] & 0x1F) - 1; /* rtc mn 1-12 */ 81*18cb6368SRenaud Cerrato tm->tm_year = bcd2bin(buf[PCF2127_REG_YR]); 82*18cb6368SRenaud Cerrato if (tm->tm_year < 70) 83*18cb6368SRenaud Cerrato tm->tm_year += 100; /* assume we are in 1970...2069 */ 84*18cb6368SRenaud Cerrato 85*18cb6368SRenaud Cerrato dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d, " 86*18cb6368SRenaud Cerrato "mday=%d, mon=%d, year=%d, wday=%d\n", 87*18cb6368SRenaud Cerrato __func__, 88*18cb6368SRenaud Cerrato tm->tm_sec, tm->tm_min, tm->tm_hour, 89*18cb6368SRenaud Cerrato tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday); 90*18cb6368SRenaud Cerrato 91*18cb6368SRenaud Cerrato /* the clock can give out invalid datetime, but we cannot return 92*18cb6368SRenaud Cerrato * -EINVAL otherwise hwclock will refuse to set the time on bootup. 93*18cb6368SRenaud Cerrato */ 94*18cb6368SRenaud Cerrato if (rtc_valid_tm(tm) < 0) 95*18cb6368SRenaud Cerrato dev_err(&client->dev, "retrieved date/time is not valid.\n"); 96*18cb6368SRenaud Cerrato 97*18cb6368SRenaud Cerrato return 0; 98*18cb6368SRenaud Cerrato } 99*18cb6368SRenaud Cerrato 100*18cb6368SRenaud Cerrato static int pcf2127_set_datetime(struct i2c_client *client, struct rtc_time *tm) 101*18cb6368SRenaud Cerrato { 102*18cb6368SRenaud Cerrato unsigned char buf[8]; 103*18cb6368SRenaud Cerrato int i = 0, err; 104*18cb6368SRenaud Cerrato 105*18cb6368SRenaud Cerrato dev_dbg(&client->dev, "%s: secs=%d, mins=%d, hours=%d, " 106*18cb6368SRenaud Cerrato "mday=%d, mon=%d, year=%d, wday=%d\n", 107*18cb6368SRenaud Cerrato __func__, 108*18cb6368SRenaud Cerrato tm->tm_sec, tm->tm_min, tm->tm_hour, 109*18cb6368SRenaud Cerrato tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday); 110*18cb6368SRenaud Cerrato 111*18cb6368SRenaud Cerrato /* start register address */ 112*18cb6368SRenaud Cerrato buf[i++] = PCF2127_REG_SC; 113*18cb6368SRenaud Cerrato 114*18cb6368SRenaud Cerrato /* hours, minutes and seconds */ 115*18cb6368SRenaud Cerrato buf[i++] = bin2bcd(tm->tm_sec); 116*18cb6368SRenaud Cerrato buf[i++] = bin2bcd(tm->tm_min); 117*18cb6368SRenaud Cerrato buf[i++] = bin2bcd(tm->tm_hour); 118*18cb6368SRenaud Cerrato buf[i++] = bin2bcd(tm->tm_mday); 119*18cb6368SRenaud Cerrato buf[i++] = tm->tm_wday & 0x07; 120*18cb6368SRenaud Cerrato 121*18cb6368SRenaud Cerrato /* month, 1 - 12 */ 122*18cb6368SRenaud Cerrato buf[i++] = bin2bcd(tm->tm_mon + 1); 123*18cb6368SRenaud Cerrato 124*18cb6368SRenaud Cerrato /* year */ 125*18cb6368SRenaud Cerrato buf[i++] = bin2bcd(tm->tm_year % 100); 126*18cb6368SRenaud Cerrato 127*18cb6368SRenaud Cerrato /* write register's data */ 128*18cb6368SRenaud Cerrato err = i2c_master_send(client, buf, i); 129*18cb6368SRenaud Cerrato if (err != i) { 130*18cb6368SRenaud Cerrato dev_err(&client->dev, 131*18cb6368SRenaud Cerrato "%s: err=%d", __func__, err); 132*18cb6368SRenaud Cerrato return -EIO; 133*18cb6368SRenaud Cerrato } 134*18cb6368SRenaud Cerrato 135*18cb6368SRenaud Cerrato return 0; 136*18cb6368SRenaud Cerrato } 137*18cb6368SRenaud Cerrato 138*18cb6368SRenaud Cerrato #ifdef CONFIG_RTC_INTF_DEV 139*18cb6368SRenaud Cerrato static int pcf2127_rtc_ioctl(struct device *dev, 140*18cb6368SRenaud Cerrato unsigned int cmd, unsigned long arg) 141*18cb6368SRenaud Cerrato { 142*18cb6368SRenaud Cerrato struct pcf2127 *pcf2127 = i2c_get_clientdata(to_i2c_client(dev)); 143*18cb6368SRenaud Cerrato 144*18cb6368SRenaud Cerrato switch (cmd) { 145*18cb6368SRenaud Cerrato case RTC_VL_READ: 146*18cb6368SRenaud Cerrato if (pcf2127->voltage_low) 147*18cb6368SRenaud Cerrato dev_info(dev, "low voltage detected, date/time is not reliable.\n"); 148*18cb6368SRenaud Cerrato 149*18cb6368SRenaud Cerrato if (copy_to_user((void __user *)arg, &pcf2127->voltage_low, 150*18cb6368SRenaud Cerrato sizeof(int))) 151*18cb6368SRenaud Cerrato return -EFAULT; 152*18cb6368SRenaud Cerrato return 0; 153*18cb6368SRenaud Cerrato default: 154*18cb6368SRenaud Cerrato return -ENOIOCTLCMD; 155*18cb6368SRenaud Cerrato } 156*18cb6368SRenaud Cerrato } 157*18cb6368SRenaud Cerrato #else 158*18cb6368SRenaud Cerrato #define pcf2127_rtc_ioctl NULL 159*18cb6368SRenaud Cerrato #endif 160*18cb6368SRenaud Cerrato 161*18cb6368SRenaud Cerrato static int pcf2127_rtc_read_time(struct device *dev, struct rtc_time *tm) 162*18cb6368SRenaud Cerrato { 163*18cb6368SRenaud Cerrato return pcf2127_get_datetime(to_i2c_client(dev), tm); 164*18cb6368SRenaud Cerrato } 165*18cb6368SRenaud Cerrato 166*18cb6368SRenaud Cerrato static int pcf2127_rtc_set_time(struct device *dev, struct rtc_time *tm) 167*18cb6368SRenaud Cerrato { 168*18cb6368SRenaud Cerrato return pcf2127_set_datetime(to_i2c_client(dev), tm); 169*18cb6368SRenaud Cerrato } 170*18cb6368SRenaud Cerrato 171*18cb6368SRenaud Cerrato static const struct rtc_class_ops pcf2127_rtc_ops = { 172*18cb6368SRenaud Cerrato .ioctl = pcf2127_rtc_ioctl, 173*18cb6368SRenaud Cerrato .read_time = pcf2127_rtc_read_time, 174*18cb6368SRenaud Cerrato .set_time = pcf2127_rtc_set_time, 175*18cb6368SRenaud Cerrato }; 176*18cb6368SRenaud Cerrato 177*18cb6368SRenaud Cerrato static int pcf2127_probe(struct i2c_client *client, 178*18cb6368SRenaud Cerrato const struct i2c_device_id *id) 179*18cb6368SRenaud Cerrato { 180*18cb6368SRenaud Cerrato struct pcf2127 *pcf2127; 181*18cb6368SRenaud Cerrato 182*18cb6368SRenaud Cerrato dev_dbg(&client->dev, "%s\n", __func__); 183*18cb6368SRenaud Cerrato 184*18cb6368SRenaud Cerrato if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) 185*18cb6368SRenaud Cerrato return -ENODEV; 186*18cb6368SRenaud Cerrato 187*18cb6368SRenaud Cerrato pcf2127 = devm_kzalloc(&client->dev, sizeof(struct pcf2127), 188*18cb6368SRenaud Cerrato GFP_KERNEL); 189*18cb6368SRenaud Cerrato if (!pcf2127) 190*18cb6368SRenaud Cerrato return -ENOMEM; 191*18cb6368SRenaud Cerrato 192*18cb6368SRenaud Cerrato dev_info(&client->dev, "chip found, driver version " DRV_VERSION "\n"); 193*18cb6368SRenaud Cerrato 194*18cb6368SRenaud Cerrato i2c_set_clientdata(client, pcf2127); 195*18cb6368SRenaud Cerrato 196*18cb6368SRenaud Cerrato pcf2127->rtc = devm_rtc_device_register(&client->dev, 197*18cb6368SRenaud Cerrato pcf2127_driver.driver.name, 198*18cb6368SRenaud Cerrato &pcf2127_rtc_ops, THIS_MODULE); 199*18cb6368SRenaud Cerrato 200*18cb6368SRenaud Cerrato if (IS_ERR(pcf2127->rtc)) 201*18cb6368SRenaud Cerrato return PTR_ERR(pcf2127->rtc); 202*18cb6368SRenaud Cerrato 203*18cb6368SRenaud Cerrato return 0; 204*18cb6368SRenaud Cerrato } 205*18cb6368SRenaud Cerrato 206*18cb6368SRenaud Cerrato static int pcf2127_remove(struct i2c_client *client) 207*18cb6368SRenaud Cerrato { 208*18cb6368SRenaud Cerrato return 0; 209*18cb6368SRenaud Cerrato } 210*18cb6368SRenaud Cerrato 211*18cb6368SRenaud Cerrato static const struct i2c_device_id pcf2127_id[] = { 212*18cb6368SRenaud Cerrato { "pcf2127", 0 }, 213*18cb6368SRenaud Cerrato { } 214*18cb6368SRenaud Cerrato }; 215*18cb6368SRenaud Cerrato MODULE_DEVICE_TABLE(i2c, pcf2127_id); 216*18cb6368SRenaud Cerrato 217*18cb6368SRenaud Cerrato #ifdef CONFIG_OF 218*18cb6368SRenaud Cerrato static const struct of_device_id pcf2127_of_match[] = { 219*18cb6368SRenaud Cerrato { .compatible = "nxp,pcf2127" }, 220*18cb6368SRenaud Cerrato {} 221*18cb6368SRenaud Cerrato }; 222*18cb6368SRenaud Cerrato MODULE_DEVICE_TABLE(of, pcf2127_of_match); 223*18cb6368SRenaud Cerrato #endif 224*18cb6368SRenaud Cerrato 225*18cb6368SRenaud Cerrato static struct i2c_driver pcf2127_driver = { 226*18cb6368SRenaud Cerrato .driver = { 227*18cb6368SRenaud Cerrato .name = "rtc-pcf2127", 228*18cb6368SRenaud Cerrato .owner = THIS_MODULE, 229*18cb6368SRenaud Cerrato .of_match_table = of_match_ptr(pcf2127_of_match), 230*18cb6368SRenaud Cerrato }, 231*18cb6368SRenaud Cerrato .probe = pcf2127_probe, 232*18cb6368SRenaud Cerrato .remove = pcf2127_remove, 233*18cb6368SRenaud Cerrato .id_table = pcf2127_id, 234*18cb6368SRenaud Cerrato }; 235*18cb6368SRenaud Cerrato 236*18cb6368SRenaud Cerrato module_i2c_driver(pcf2127_driver); 237*18cb6368SRenaud Cerrato 238*18cb6368SRenaud Cerrato MODULE_AUTHOR("Renaud Cerrato <r.cerrato@til-technologies.fr>"); 239*18cb6368SRenaud Cerrato MODULE_DESCRIPTION("NXP PCF2127 RTC driver"); 240*18cb6368SRenaud Cerrato MODULE_LICENSE("GPL"); 241*18cb6368SRenaud Cerrato MODULE_VERSION(DRV_VERSION); 242