18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * rtc class driver for the Maxim MAX6900 chip
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Author: Dale Farnsworth <dale@farnsworth.org>
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * based on previously existing rtc class drivers
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * 2007 (c) MontaVista, Software, Inc.  This file is licensed under
98c2ecf20Sopenharmony_ci * the terms of the GNU General Public License version 2.  This program
108c2ecf20Sopenharmony_ci * is licensed "as is" without any warranty of any kind, whether express
118c2ecf20Sopenharmony_ci * or implied.
128c2ecf20Sopenharmony_ci */
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#include <linux/module.h>
158c2ecf20Sopenharmony_ci#include <linux/i2c.h>
168c2ecf20Sopenharmony_ci#include <linux/bcd.h>
178c2ecf20Sopenharmony_ci#include <linux/rtc.h>
188c2ecf20Sopenharmony_ci#include <linux/delay.h>
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci/*
218c2ecf20Sopenharmony_ci * register indices
228c2ecf20Sopenharmony_ci */
238c2ecf20Sopenharmony_ci#define MAX6900_REG_SC			0	/* seconds      00-59 */
248c2ecf20Sopenharmony_ci#define MAX6900_REG_MN			1	/* minutes      00-59 */
258c2ecf20Sopenharmony_ci#define MAX6900_REG_HR			2	/* hours        00-23 */
268c2ecf20Sopenharmony_ci#define MAX6900_REG_DT			3	/* day of month 00-31 */
278c2ecf20Sopenharmony_ci#define MAX6900_REG_MO			4	/* month        01-12 */
288c2ecf20Sopenharmony_ci#define MAX6900_REG_DW			5	/* day of week   1-7  */
298c2ecf20Sopenharmony_ci#define MAX6900_REG_YR			6	/* year         00-99 */
308c2ecf20Sopenharmony_ci#define MAX6900_REG_CT			7	/* control */
318c2ecf20Sopenharmony_ci						/* register 8 is undocumented */
328c2ecf20Sopenharmony_ci#define MAX6900_REG_CENTURY		9	/* century */
338c2ecf20Sopenharmony_ci#define MAX6900_REG_LEN			10
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci#define MAX6900_BURST_LEN		8	/* can burst r/w first 8 regs */
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci#define MAX6900_REG_CT_WP		(1 << 7)	/* Write Protect */
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci/*
408c2ecf20Sopenharmony_ci * register read/write commands
418c2ecf20Sopenharmony_ci */
428c2ecf20Sopenharmony_ci#define MAX6900_REG_CONTROL_WRITE	0x8e
438c2ecf20Sopenharmony_ci#define MAX6900_REG_CENTURY_WRITE	0x92
448c2ecf20Sopenharmony_ci#define MAX6900_REG_CENTURY_READ	0x93
458c2ecf20Sopenharmony_ci#define MAX6900_REG_RESERVED_READ	0x96
468c2ecf20Sopenharmony_ci#define MAX6900_REG_BURST_WRITE		0xbe
478c2ecf20Sopenharmony_ci#define MAX6900_REG_BURST_READ		0xbf
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci#define MAX6900_IDLE_TIME_AFTER_WRITE	3	/* specification says 2.5 mS */
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_cistatic struct i2c_driver max6900_driver;
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_cistatic int max6900_i2c_read_regs(struct i2c_client *client, u8 *buf)
548c2ecf20Sopenharmony_ci{
558c2ecf20Sopenharmony_ci	u8 reg_burst_read[1] = { MAX6900_REG_BURST_READ };
568c2ecf20Sopenharmony_ci	u8 reg_century_read[1] = { MAX6900_REG_CENTURY_READ };
578c2ecf20Sopenharmony_ci	struct i2c_msg msgs[4] = {
588c2ecf20Sopenharmony_ci		{
598c2ecf20Sopenharmony_ci		 .addr = client->addr,
608c2ecf20Sopenharmony_ci		 .flags = 0,	/* write */
618c2ecf20Sopenharmony_ci		 .len = sizeof(reg_burst_read),
628c2ecf20Sopenharmony_ci		 .buf = reg_burst_read}
638c2ecf20Sopenharmony_ci		,
648c2ecf20Sopenharmony_ci		{
658c2ecf20Sopenharmony_ci		 .addr = client->addr,
668c2ecf20Sopenharmony_ci		 .flags = I2C_M_RD,
678c2ecf20Sopenharmony_ci		 .len = MAX6900_BURST_LEN,
688c2ecf20Sopenharmony_ci		 .buf = buf}
698c2ecf20Sopenharmony_ci		,
708c2ecf20Sopenharmony_ci		{
718c2ecf20Sopenharmony_ci		 .addr = client->addr,
728c2ecf20Sopenharmony_ci		 .flags = 0,	/* write */
738c2ecf20Sopenharmony_ci		 .len = sizeof(reg_century_read),
748c2ecf20Sopenharmony_ci		 .buf = reg_century_read}
758c2ecf20Sopenharmony_ci		,
768c2ecf20Sopenharmony_ci		{
778c2ecf20Sopenharmony_ci		 .addr = client->addr,
788c2ecf20Sopenharmony_ci		 .flags = I2C_M_RD,
798c2ecf20Sopenharmony_ci		 .len = sizeof(buf[MAX6900_REG_CENTURY]),
808c2ecf20Sopenharmony_ci		 .buf = &buf[MAX6900_REG_CENTURY]
818c2ecf20Sopenharmony_ci		 }
828c2ecf20Sopenharmony_ci	};
838c2ecf20Sopenharmony_ci	int rc;
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci	rc = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
868c2ecf20Sopenharmony_ci	if (rc != ARRAY_SIZE(msgs)) {
878c2ecf20Sopenharmony_ci		dev_err(&client->dev, "%s: register read failed\n", __func__);
888c2ecf20Sopenharmony_ci		return -EIO;
898c2ecf20Sopenharmony_ci	}
908c2ecf20Sopenharmony_ci	return 0;
918c2ecf20Sopenharmony_ci}
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_cistatic int max6900_i2c_write_regs(struct i2c_client *client, u8 const *buf)
948c2ecf20Sopenharmony_ci{
958c2ecf20Sopenharmony_ci	u8 i2c_century_buf[1 + 1] = { MAX6900_REG_CENTURY_WRITE };
968c2ecf20Sopenharmony_ci	struct i2c_msg century_msgs[1] = {
978c2ecf20Sopenharmony_ci		{
988c2ecf20Sopenharmony_ci		 .addr = client->addr,
998c2ecf20Sopenharmony_ci		 .flags = 0,	/* write */
1008c2ecf20Sopenharmony_ci		 .len = sizeof(i2c_century_buf),
1018c2ecf20Sopenharmony_ci		 .buf = i2c_century_buf}
1028c2ecf20Sopenharmony_ci	};
1038c2ecf20Sopenharmony_ci	u8 i2c_burst_buf[MAX6900_BURST_LEN + 1] = { MAX6900_REG_BURST_WRITE };
1048c2ecf20Sopenharmony_ci	struct i2c_msg burst_msgs[1] = {
1058c2ecf20Sopenharmony_ci		{
1068c2ecf20Sopenharmony_ci		 .addr = client->addr,
1078c2ecf20Sopenharmony_ci		 .flags = 0,	/* write */
1088c2ecf20Sopenharmony_ci		 .len = sizeof(i2c_burst_buf),
1098c2ecf20Sopenharmony_ci		 .buf = i2c_burst_buf}
1108c2ecf20Sopenharmony_ci	};
1118c2ecf20Sopenharmony_ci	int rc;
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	/*
1148c2ecf20Sopenharmony_ci	 * We have to make separate calls to i2c_transfer because of
1158c2ecf20Sopenharmony_ci	 * the need to delay after each write to the chip.  Also,
1168c2ecf20Sopenharmony_ci	 * we write the century byte first, since we set the write-protect
1178c2ecf20Sopenharmony_ci	 * bit as part of the burst write.
1188c2ecf20Sopenharmony_ci	 */
1198c2ecf20Sopenharmony_ci	i2c_century_buf[1] = buf[MAX6900_REG_CENTURY];
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci	rc = i2c_transfer(client->adapter, century_msgs,
1228c2ecf20Sopenharmony_ci			  ARRAY_SIZE(century_msgs));
1238c2ecf20Sopenharmony_ci	if (rc != ARRAY_SIZE(century_msgs))
1248c2ecf20Sopenharmony_ci		goto write_failed;
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	msleep(MAX6900_IDLE_TIME_AFTER_WRITE);
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci	memcpy(&i2c_burst_buf[1], buf, MAX6900_BURST_LEN);
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci	rc = i2c_transfer(client->adapter, burst_msgs, ARRAY_SIZE(burst_msgs));
1318c2ecf20Sopenharmony_ci	if (rc != ARRAY_SIZE(burst_msgs))
1328c2ecf20Sopenharmony_ci		goto write_failed;
1338c2ecf20Sopenharmony_ci	msleep(MAX6900_IDLE_TIME_AFTER_WRITE);
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	return 0;
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci write_failed:
1388c2ecf20Sopenharmony_ci	dev_err(&client->dev, "%s: register write failed\n", __func__);
1398c2ecf20Sopenharmony_ci	return -EIO;
1408c2ecf20Sopenharmony_ci}
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_cistatic int max6900_rtc_read_time(struct device *dev, struct rtc_time *tm)
1438c2ecf20Sopenharmony_ci{
1448c2ecf20Sopenharmony_ci	struct i2c_client *client = to_i2c_client(dev);
1458c2ecf20Sopenharmony_ci	int rc;
1468c2ecf20Sopenharmony_ci	u8 regs[MAX6900_REG_LEN];
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci	rc = max6900_i2c_read_regs(client, regs);
1498c2ecf20Sopenharmony_ci	if (rc < 0)
1508c2ecf20Sopenharmony_ci		return rc;
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci	tm->tm_sec = bcd2bin(regs[MAX6900_REG_SC]);
1538c2ecf20Sopenharmony_ci	tm->tm_min = bcd2bin(regs[MAX6900_REG_MN]);
1548c2ecf20Sopenharmony_ci	tm->tm_hour = bcd2bin(regs[MAX6900_REG_HR] & 0x3f);
1558c2ecf20Sopenharmony_ci	tm->tm_mday = bcd2bin(regs[MAX6900_REG_DT]);
1568c2ecf20Sopenharmony_ci	tm->tm_mon = bcd2bin(regs[MAX6900_REG_MO]) - 1;
1578c2ecf20Sopenharmony_ci	tm->tm_year = bcd2bin(regs[MAX6900_REG_YR]) +
1588c2ecf20Sopenharmony_ci		      bcd2bin(regs[MAX6900_REG_CENTURY]) * 100 - 1900;
1598c2ecf20Sopenharmony_ci	tm->tm_wday = bcd2bin(regs[MAX6900_REG_DW]);
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci	return 0;
1628c2ecf20Sopenharmony_ci}
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_cistatic int max6900_i2c_clear_write_protect(struct i2c_client *client)
1658c2ecf20Sopenharmony_ci{
1668c2ecf20Sopenharmony_ci	return i2c_smbus_write_byte_data(client, MAX6900_REG_CONTROL_WRITE, 0);
1678c2ecf20Sopenharmony_ci}
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_cistatic int max6900_rtc_set_time(struct device *dev, struct rtc_time *tm)
1708c2ecf20Sopenharmony_ci{
1718c2ecf20Sopenharmony_ci	struct i2c_client *client = to_i2c_client(dev);
1728c2ecf20Sopenharmony_ci	u8 regs[MAX6900_REG_LEN];
1738c2ecf20Sopenharmony_ci	int rc;
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci	rc = max6900_i2c_clear_write_protect(client);
1768c2ecf20Sopenharmony_ci	if (rc < 0)
1778c2ecf20Sopenharmony_ci		return rc;
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci	regs[MAX6900_REG_SC] = bin2bcd(tm->tm_sec);
1808c2ecf20Sopenharmony_ci	regs[MAX6900_REG_MN] = bin2bcd(tm->tm_min);
1818c2ecf20Sopenharmony_ci	regs[MAX6900_REG_HR] = bin2bcd(tm->tm_hour);
1828c2ecf20Sopenharmony_ci	regs[MAX6900_REG_DT] = bin2bcd(tm->tm_mday);
1838c2ecf20Sopenharmony_ci	regs[MAX6900_REG_MO] = bin2bcd(tm->tm_mon + 1);
1848c2ecf20Sopenharmony_ci	regs[MAX6900_REG_DW] = bin2bcd(tm->tm_wday);
1858c2ecf20Sopenharmony_ci	regs[MAX6900_REG_YR] = bin2bcd(tm->tm_year % 100);
1868c2ecf20Sopenharmony_ci	regs[MAX6900_REG_CENTURY] = bin2bcd((tm->tm_year + 1900) / 100);
1878c2ecf20Sopenharmony_ci	/* set write protect */
1888c2ecf20Sopenharmony_ci	regs[MAX6900_REG_CT] = MAX6900_REG_CT_WP;
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci	rc = max6900_i2c_write_regs(client, regs);
1918c2ecf20Sopenharmony_ci	if (rc < 0)
1928c2ecf20Sopenharmony_ci		return rc;
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci	return 0;
1958c2ecf20Sopenharmony_ci}
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_cistatic const struct rtc_class_ops max6900_rtc_ops = {
1988c2ecf20Sopenharmony_ci	.read_time = max6900_rtc_read_time,
1998c2ecf20Sopenharmony_ci	.set_time = max6900_rtc_set_time,
2008c2ecf20Sopenharmony_ci};
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_cistatic int
2038c2ecf20Sopenharmony_cimax6900_probe(struct i2c_client *client, const struct i2c_device_id *id)
2048c2ecf20Sopenharmony_ci{
2058c2ecf20Sopenharmony_ci	struct rtc_device *rtc;
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
2088c2ecf20Sopenharmony_ci		return -ENODEV;
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci	rtc = devm_rtc_device_register(&client->dev, max6900_driver.driver.name,
2118c2ecf20Sopenharmony_ci					&max6900_rtc_ops, THIS_MODULE);
2128c2ecf20Sopenharmony_ci	if (IS_ERR(rtc))
2138c2ecf20Sopenharmony_ci		return PTR_ERR(rtc);
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ci	i2c_set_clientdata(client, rtc);
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci	return 0;
2188c2ecf20Sopenharmony_ci}
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_cistatic const struct i2c_device_id max6900_id[] = {
2218c2ecf20Sopenharmony_ci	{ "max6900", 0 },
2228c2ecf20Sopenharmony_ci	{ }
2238c2ecf20Sopenharmony_ci};
2248c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, max6900_id);
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_cistatic struct i2c_driver max6900_driver = {
2278c2ecf20Sopenharmony_ci	.driver = {
2288c2ecf20Sopenharmony_ci		   .name = "rtc-max6900",
2298c2ecf20Sopenharmony_ci		   },
2308c2ecf20Sopenharmony_ci	.probe = max6900_probe,
2318c2ecf20Sopenharmony_ci	.id_table = max6900_id,
2328c2ecf20Sopenharmony_ci};
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_cimodule_i2c_driver(max6900_driver);
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Maxim MAX6900 RTC driver");
2378c2ecf20Sopenharmony_ciMODULE_AUTHOR("Dale Farnsworth <dale@farnsworth.org>");
2388c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
239