xref: /openbmc/linux/drivers/rtc/rtc-pcf2127.c (revision 4d8318bc97a1c37142d0e7618061f09b37d319b6)
118cb6368SRenaud Cerrato /*
218cb6368SRenaud Cerrato  * An I2C driver for the NXP PCF2127 RTC
318cb6368SRenaud Cerrato  * Copyright 2013 Til-Technologies
418cb6368SRenaud Cerrato  *
518cb6368SRenaud Cerrato  * Author: Renaud Cerrato <r.cerrato@til-technologies.fr>
618cb6368SRenaud Cerrato  *
718cb6368SRenaud Cerrato  * based on the other drivers in this same directory.
818cb6368SRenaud Cerrato  *
918cb6368SRenaud Cerrato  * http://www.nxp.com/documents/data_sheet/PCF2127AT.pdf
1018cb6368SRenaud Cerrato  *
1118cb6368SRenaud Cerrato  * This program is free software; you can redistribute it and/or modify
1218cb6368SRenaud Cerrato  * it under the terms of the GNU General Public License version 2 as
1318cb6368SRenaud Cerrato  * published by the Free Software Foundation.
1418cb6368SRenaud Cerrato  */
1518cb6368SRenaud Cerrato 
1618cb6368SRenaud Cerrato #include <linux/i2c.h>
1718cb6368SRenaud Cerrato #include <linux/bcd.h>
1818cb6368SRenaud Cerrato #include <linux/rtc.h>
1918cb6368SRenaud Cerrato #include <linux/slab.h>
2018cb6368SRenaud Cerrato #include <linux/module.h>
2118cb6368SRenaud Cerrato #include <linux/of.h>
2218cb6368SRenaud Cerrato 
2318cb6368SRenaud Cerrato #define DRV_VERSION "0.0.1"
2418cb6368SRenaud Cerrato 
2518cb6368SRenaud Cerrato #define PCF2127_REG_CTRL1       (0x00)  /* Control Register 1 */
2618cb6368SRenaud Cerrato #define PCF2127_REG_CTRL2       (0x01)  /* Control Register 2 */
2718cb6368SRenaud Cerrato #define PCF2127_REG_CTRL3       (0x02)  /* Control Register 3 */
2818cb6368SRenaud Cerrato #define PCF2127_REG_SC          (0x03)  /* datetime */
2918cb6368SRenaud Cerrato #define PCF2127_REG_MN          (0x04)
3018cb6368SRenaud Cerrato #define PCF2127_REG_HR          (0x05)
3118cb6368SRenaud Cerrato #define PCF2127_REG_DM          (0x06)
3218cb6368SRenaud Cerrato #define PCF2127_REG_DW          (0x07)
3318cb6368SRenaud Cerrato #define PCF2127_REG_MO          (0x08)
3418cb6368SRenaud Cerrato #define PCF2127_REG_YR          (0x09)
3518cb6368SRenaud Cerrato 
36653ebd75SAndrea Scian #define PCF2127_OSF             BIT(7)  /* Oscillator Fail flag */
37653ebd75SAndrea Scian 
3818cb6368SRenaud Cerrato static struct i2c_driver pcf2127_driver;
3918cb6368SRenaud Cerrato 
4018cb6368SRenaud Cerrato struct pcf2127 {
4118cb6368SRenaud Cerrato 	struct rtc_device *rtc;
4218cb6368SRenaud Cerrato 	int voltage_low; /* indicates if a low_voltage was detected */
43653ebd75SAndrea Scian 	int oscillator_failed; /* OSF was detected and date is unreliable */
4418cb6368SRenaud Cerrato };
4518cb6368SRenaud Cerrato 
4618cb6368SRenaud Cerrato /*
4718cb6368SRenaud Cerrato  * In the routines that deal directly with the pcf2127 hardware, we use
4818cb6368SRenaud Cerrato  * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch.
4918cb6368SRenaud Cerrato  */
5018cb6368SRenaud Cerrato static int pcf2127_get_datetime(struct i2c_client *client, struct rtc_time *tm)
5118cb6368SRenaud Cerrato {
5218cb6368SRenaud Cerrato 	struct pcf2127 *pcf2127 = i2c_get_clientdata(client);
5318cb6368SRenaud Cerrato 	unsigned char buf[10] = { PCF2127_REG_CTRL1 };
5418cb6368SRenaud Cerrato 
5518cb6368SRenaud Cerrato 	/* read registers */
5618cb6368SRenaud Cerrato 	if (i2c_master_send(client, buf, 1) != 1 ||
5718cb6368SRenaud Cerrato 		i2c_master_recv(client, buf, sizeof(buf)) != sizeof(buf)) {
5818cb6368SRenaud Cerrato 		dev_err(&client->dev, "%s: read error\n", __func__);
5918cb6368SRenaud Cerrato 		return -EIO;
6018cb6368SRenaud Cerrato 	}
6118cb6368SRenaud Cerrato 
6218cb6368SRenaud Cerrato 	if (buf[PCF2127_REG_CTRL3] & 0x04) {
6318cb6368SRenaud Cerrato 		pcf2127->voltage_low = 1;
6418cb6368SRenaud Cerrato 		dev_info(&client->dev,
65653ebd75SAndrea Scian 			"low voltage detected, check/replace RTC battery.\n");
66653ebd75SAndrea Scian 	}
67653ebd75SAndrea Scian 
68653ebd75SAndrea Scian 	if (buf[PCF2127_REG_SC] & PCF2127_OSF) {
69653ebd75SAndrea Scian 		/*
70653ebd75SAndrea Scian 		 * no need clear the flag here,
71653ebd75SAndrea Scian 		 * it will be cleared once the new date is saved
72653ebd75SAndrea Scian 		 */
73653ebd75SAndrea Scian 		pcf2127->oscillator_failed = 1;
74653ebd75SAndrea Scian 		dev_warn(&client->dev,
75653ebd75SAndrea Scian 			 "oscillator stop detected, date/time is not reliable\n");
76653ebd75SAndrea Scian 		return -EINVAL;
7718cb6368SRenaud Cerrato 	}
7818cb6368SRenaud Cerrato 
7918cb6368SRenaud Cerrato 	dev_dbg(&client->dev,
8018cb6368SRenaud Cerrato 		"%s: raw data is cr1=%02x, cr2=%02x, cr3=%02x, "
8118cb6368SRenaud Cerrato 		"sec=%02x, min=%02x, hr=%02x, "
8218cb6368SRenaud Cerrato 		"mday=%02x, wday=%02x, mon=%02x, year=%02x\n",
8318cb6368SRenaud Cerrato 		__func__,
8418cb6368SRenaud Cerrato 		buf[0], buf[1], buf[2],
8518cb6368SRenaud Cerrato 		buf[3], buf[4], buf[5],
8618cb6368SRenaud Cerrato 		buf[6], buf[7], buf[8], buf[9]);
8718cb6368SRenaud Cerrato 
8818cb6368SRenaud Cerrato 
8918cb6368SRenaud Cerrato 	tm->tm_sec = bcd2bin(buf[PCF2127_REG_SC] & 0x7F);
9018cb6368SRenaud Cerrato 	tm->tm_min = bcd2bin(buf[PCF2127_REG_MN] & 0x7F);
9118cb6368SRenaud Cerrato 	tm->tm_hour = bcd2bin(buf[PCF2127_REG_HR] & 0x3F); /* rtc hr 0-23 */
9218cb6368SRenaud Cerrato 	tm->tm_mday = bcd2bin(buf[PCF2127_REG_DM] & 0x3F);
9318cb6368SRenaud Cerrato 	tm->tm_wday = buf[PCF2127_REG_DW] & 0x07;
9418cb6368SRenaud Cerrato 	tm->tm_mon = bcd2bin(buf[PCF2127_REG_MO] & 0x1F) - 1; /* rtc mn 1-12 */
9518cb6368SRenaud Cerrato 	tm->tm_year = bcd2bin(buf[PCF2127_REG_YR]);
9618cb6368SRenaud Cerrato 	if (tm->tm_year < 70)
9718cb6368SRenaud Cerrato 		tm->tm_year += 100;	/* assume we are in 1970...2069 */
9818cb6368SRenaud Cerrato 
9918cb6368SRenaud Cerrato 	dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
10018cb6368SRenaud Cerrato 		"mday=%d, mon=%d, year=%d, wday=%d\n",
10118cb6368SRenaud Cerrato 		__func__,
10218cb6368SRenaud Cerrato 		tm->tm_sec, tm->tm_min, tm->tm_hour,
10318cb6368SRenaud Cerrato 		tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
10418cb6368SRenaud Cerrato 
105821f51c4SAndrea Scian 	return rtc_valid_tm(tm);
10618cb6368SRenaud Cerrato }
10718cb6368SRenaud Cerrato 
10818cb6368SRenaud Cerrato static int pcf2127_set_datetime(struct i2c_client *client, struct rtc_time *tm)
10918cb6368SRenaud Cerrato {
110653ebd75SAndrea Scian 	struct pcf2127 *pcf2127 = i2c_get_clientdata(client);
11118cb6368SRenaud Cerrato 	unsigned char buf[8];
11218cb6368SRenaud Cerrato 	int i = 0, err;
11318cb6368SRenaud Cerrato 
11418cb6368SRenaud Cerrato 	dev_dbg(&client->dev, "%s: secs=%d, mins=%d, hours=%d, "
11518cb6368SRenaud Cerrato 		"mday=%d, mon=%d, year=%d, wday=%d\n",
11618cb6368SRenaud Cerrato 		__func__,
11718cb6368SRenaud Cerrato 		tm->tm_sec, tm->tm_min, tm->tm_hour,
11818cb6368SRenaud Cerrato 		tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
11918cb6368SRenaud Cerrato 
12018cb6368SRenaud Cerrato 	/* start register address */
12118cb6368SRenaud Cerrato 	buf[i++] = PCF2127_REG_SC;
12218cb6368SRenaud Cerrato 
12318cb6368SRenaud Cerrato 	/* hours, minutes and seconds */
124653ebd75SAndrea Scian 	buf[i++] = bin2bcd(tm->tm_sec);	/* this will also clear OSF flag */
12518cb6368SRenaud Cerrato 	buf[i++] = bin2bcd(tm->tm_min);
12618cb6368SRenaud Cerrato 	buf[i++] = bin2bcd(tm->tm_hour);
12718cb6368SRenaud Cerrato 	buf[i++] = bin2bcd(tm->tm_mday);
12818cb6368SRenaud Cerrato 	buf[i++] = tm->tm_wday & 0x07;
12918cb6368SRenaud Cerrato 
13018cb6368SRenaud Cerrato 	/* month, 1 - 12 */
13118cb6368SRenaud Cerrato 	buf[i++] = bin2bcd(tm->tm_mon + 1);
13218cb6368SRenaud Cerrato 
13318cb6368SRenaud Cerrato 	/* year */
13418cb6368SRenaud Cerrato 	buf[i++] = bin2bcd(tm->tm_year % 100);
13518cb6368SRenaud Cerrato 
13618cb6368SRenaud Cerrato 	/* write register's data */
13718cb6368SRenaud Cerrato 	err = i2c_master_send(client, buf, i);
13818cb6368SRenaud Cerrato 	if (err != i) {
13918cb6368SRenaud Cerrato 		dev_err(&client->dev,
14018cb6368SRenaud Cerrato 			"%s: err=%d", __func__, err);
14118cb6368SRenaud Cerrato 		return -EIO;
14218cb6368SRenaud Cerrato 	}
14318cb6368SRenaud Cerrato 
144653ebd75SAndrea Scian 	/* clear OSF flag in client data */
145653ebd75SAndrea Scian 	pcf2127->oscillator_failed = 0;
146653ebd75SAndrea Scian 
14718cb6368SRenaud Cerrato 	return 0;
14818cb6368SRenaud Cerrato }
14918cb6368SRenaud Cerrato 
15018cb6368SRenaud Cerrato #ifdef CONFIG_RTC_INTF_DEV
15118cb6368SRenaud Cerrato static int pcf2127_rtc_ioctl(struct device *dev,
15218cb6368SRenaud Cerrato 				unsigned int cmd, unsigned long arg)
15318cb6368SRenaud Cerrato {
15418cb6368SRenaud Cerrato 	struct pcf2127 *pcf2127 = i2c_get_clientdata(to_i2c_client(dev));
15518cb6368SRenaud Cerrato 
15618cb6368SRenaud Cerrato 	switch (cmd) {
15718cb6368SRenaud Cerrato 	case RTC_VL_READ:
15818cb6368SRenaud Cerrato 		if (pcf2127->voltage_low)
159653ebd75SAndrea Scian 			dev_info(dev, "low voltage detected, check/replace battery\n");
160653ebd75SAndrea Scian 		if (pcf2127->oscillator_failed)
161653ebd75SAndrea Scian 			dev_info(dev, "oscillator stop detected, date/time is not reliable\n");
16218cb6368SRenaud Cerrato 
16318cb6368SRenaud Cerrato 		if (copy_to_user((void __user *)arg, &pcf2127->voltage_low,
16418cb6368SRenaud Cerrato 					sizeof(int)))
16518cb6368SRenaud Cerrato 			return -EFAULT;
16618cb6368SRenaud Cerrato 		return 0;
16718cb6368SRenaud Cerrato 	default:
16818cb6368SRenaud Cerrato 		return -ENOIOCTLCMD;
16918cb6368SRenaud Cerrato 	}
17018cb6368SRenaud Cerrato }
17118cb6368SRenaud Cerrato #else
17218cb6368SRenaud Cerrato #define pcf2127_rtc_ioctl NULL
17318cb6368SRenaud Cerrato #endif
17418cb6368SRenaud Cerrato 
17518cb6368SRenaud Cerrato static int pcf2127_rtc_read_time(struct device *dev, struct rtc_time *tm)
17618cb6368SRenaud Cerrato {
17718cb6368SRenaud Cerrato 	return pcf2127_get_datetime(to_i2c_client(dev), tm);
17818cb6368SRenaud Cerrato }
17918cb6368SRenaud Cerrato 
18018cb6368SRenaud Cerrato static int pcf2127_rtc_set_time(struct device *dev, struct rtc_time *tm)
18118cb6368SRenaud Cerrato {
18218cb6368SRenaud Cerrato 	return pcf2127_set_datetime(to_i2c_client(dev), tm);
18318cb6368SRenaud Cerrato }
18418cb6368SRenaud Cerrato 
18518cb6368SRenaud Cerrato static const struct rtc_class_ops pcf2127_rtc_ops = {
18618cb6368SRenaud Cerrato 	.ioctl		= pcf2127_rtc_ioctl,
18718cb6368SRenaud Cerrato 	.read_time	= pcf2127_rtc_read_time,
18818cb6368SRenaud Cerrato 	.set_time	= pcf2127_rtc_set_time,
18918cb6368SRenaud Cerrato };
19018cb6368SRenaud Cerrato 
19118cb6368SRenaud Cerrato static int pcf2127_probe(struct i2c_client *client,
19218cb6368SRenaud Cerrato 				const struct i2c_device_id *id)
19318cb6368SRenaud Cerrato {
19418cb6368SRenaud Cerrato 	struct pcf2127 *pcf2127;
19518cb6368SRenaud Cerrato 
19618cb6368SRenaud Cerrato 	dev_dbg(&client->dev, "%s\n", __func__);
19718cb6368SRenaud Cerrato 
19818cb6368SRenaud Cerrato 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
19918cb6368SRenaud Cerrato 		return -ENODEV;
20018cb6368SRenaud Cerrato 
20118cb6368SRenaud Cerrato 	pcf2127 = devm_kzalloc(&client->dev, sizeof(struct pcf2127),
20218cb6368SRenaud Cerrato 				GFP_KERNEL);
20318cb6368SRenaud Cerrato 	if (!pcf2127)
20418cb6368SRenaud Cerrato 		return -ENOMEM;
20518cb6368SRenaud Cerrato 
20618cb6368SRenaud Cerrato 	dev_info(&client->dev, "chip found, driver version " DRV_VERSION "\n");
20718cb6368SRenaud Cerrato 
20818cb6368SRenaud Cerrato 	i2c_set_clientdata(client, pcf2127);
20918cb6368SRenaud Cerrato 
21018cb6368SRenaud Cerrato 	pcf2127->rtc = devm_rtc_device_register(&client->dev,
21118cb6368SRenaud Cerrato 				pcf2127_driver.driver.name,
21218cb6368SRenaud Cerrato 				&pcf2127_rtc_ops, THIS_MODULE);
21318cb6368SRenaud Cerrato 
2147ab26cd1SDuan Jiong 	return PTR_ERR_OR_ZERO(pcf2127->rtc);
21518cb6368SRenaud Cerrato }
21618cb6368SRenaud Cerrato 
21718cb6368SRenaud Cerrato static const struct i2c_device_id pcf2127_id[] = {
21818cb6368SRenaud Cerrato 	{ "pcf2127", 0 },
21918cb6368SRenaud Cerrato 	{ }
22018cb6368SRenaud Cerrato };
22118cb6368SRenaud Cerrato MODULE_DEVICE_TABLE(i2c, pcf2127_id);
22218cb6368SRenaud Cerrato 
22318cb6368SRenaud Cerrato #ifdef CONFIG_OF
22418cb6368SRenaud Cerrato static const struct of_device_id pcf2127_of_match[] = {
22518cb6368SRenaud Cerrato 	{ .compatible = "nxp,pcf2127" },
22618cb6368SRenaud Cerrato 	{}
22718cb6368SRenaud Cerrato };
22818cb6368SRenaud Cerrato MODULE_DEVICE_TABLE(of, pcf2127_of_match);
22918cb6368SRenaud Cerrato #endif
23018cb6368SRenaud Cerrato 
23118cb6368SRenaud Cerrato static struct i2c_driver pcf2127_driver = {
23218cb6368SRenaud Cerrato 	.driver		= {
23318cb6368SRenaud Cerrato 		.name	= "rtc-pcf2127",
23418cb6368SRenaud Cerrato 		.of_match_table = of_match_ptr(pcf2127_of_match),
23518cb6368SRenaud Cerrato 	},
23618cb6368SRenaud Cerrato 	.probe		= pcf2127_probe,
23718cb6368SRenaud Cerrato 	.id_table	= pcf2127_id,
23818cb6368SRenaud Cerrato };
23918cb6368SRenaud Cerrato 
24018cb6368SRenaud Cerrato module_i2c_driver(pcf2127_driver);
24118cb6368SRenaud Cerrato 
24218cb6368SRenaud Cerrato MODULE_AUTHOR("Renaud Cerrato <r.cerrato@til-technologies.fr>");
24318cb6368SRenaud Cerrato MODULE_DESCRIPTION("NXP PCF2127 RTC driver");
244*4d8318bcSUwe Kleine-König MODULE_LICENSE("GPL v2");
24518cb6368SRenaud Cerrato MODULE_VERSION(DRV_VERSION);
246