xref: /linux/drivers/rtc/rtc-ds1672.c (revision aac8703c1313c4b7e1a4cf82bcfe62ab0ad79b0d)
1edf1aaa3SAlessandro Zummo /*
2edf1aaa3SAlessandro Zummo  * An rtc/i2c driver for the Dallas DS1672
33903586aSAlessandro Zummo  * Copyright 2005-06 Tower Technologies
43903586aSAlessandro Zummo  *
53903586aSAlessandro Zummo  * Author: Alessandro Zummo <a.zummo@towertech.it>
6edf1aaa3SAlessandro Zummo  *
7edf1aaa3SAlessandro Zummo  * This program is free software; you can redistribute it and/or modify
8edf1aaa3SAlessandro Zummo  * it under the terms of the GNU General Public License version 2 as
9edf1aaa3SAlessandro Zummo  * published by the Free Software Foundation.
10edf1aaa3SAlessandro Zummo  */
11edf1aaa3SAlessandro Zummo 
12edf1aaa3SAlessandro Zummo #include <linux/i2c.h>
13edf1aaa3SAlessandro Zummo #include <linux/rtc.h>
142113852bSPaul Gortmaker #include <linux/module.h>
15edf1aaa3SAlessandro Zummo 
161716b0feSAlessandro Zummo #define DRV_VERSION "0.4"
17edf1aaa3SAlessandro Zummo 
18edf1aaa3SAlessandro Zummo /* Registers */
19edf1aaa3SAlessandro Zummo 
20edf1aaa3SAlessandro Zummo #define DS1672_REG_CNT_BASE	0
21edf1aaa3SAlessandro Zummo #define DS1672_REG_CONTROL	4
22edf1aaa3SAlessandro Zummo #define DS1672_REG_TRICKLE	5
23edf1aaa3SAlessandro Zummo 
243903586aSAlessandro Zummo #define DS1672_REG_CONTROL_EOSC	0x80
25edf1aaa3SAlessandro Zummo 
261716b0feSAlessandro Zummo static struct i2c_driver ds1672_driver;
27edf1aaa3SAlessandro Zummo 
28edf1aaa3SAlessandro Zummo /*
29edf1aaa3SAlessandro Zummo  * In the routines that deal directly with the ds1672 hardware, we use
30edf1aaa3SAlessandro Zummo  * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch
31edf1aaa3SAlessandro Zummo  * Epoch is initialized as 2000. Time is set to UTC.
32edf1aaa3SAlessandro Zummo  */
33edf1aaa3SAlessandro Zummo static int ds1672_get_datetime(struct i2c_client *client, struct rtc_time *tm)
34edf1aaa3SAlessandro Zummo {
35edf1aaa3SAlessandro Zummo 	unsigned long time;
36edf1aaa3SAlessandro Zummo 	unsigned char addr = DS1672_REG_CNT_BASE;
37edf1aaa3SAlessandro Zummo 	unsigned char buf[4];
38edf1aaa3SAlessandro Zummo 
39edf1aaa3SAlessandro Zummo 	struct i2c_msg msgs[] = {
402bfc37dfSShubhrajyoti D 		{/* setup read ptr */
412bfc37dfSShubhrajyoti D 			.addr = client->addr,
422bfc37dfSShubhrajyoti D 			.len = 1,
432bfc37dfSShubhrajyoti D 			.buf = &addr
442bfc37dfSShubhrajyoti D 		},
452bfc37dfSShubhrajyoti D 		{/* read date */
462bfc37dfSShubhrajyoti D 			.addr = client->addr,
472bfc37dfSShubhrajyoti D 			.flags = I2C_M_RD,
482bfc37dfSShubhrajyoti D 			.len = 4,
492bfc37dfSShubhrajyoti D 			.buf = buf
502bfc37dfSShubhrajyoti D 		},
51edf1aaa3SAlessandro Zummo 	};
52edf1aaa3SAlessandro Zummo 
53edf1aaa3SAlessandro Zummo 	/* read date registers */
54edf1aaa3SAlessandro Zummo 	if ((i2c_transfer(client->adapter, &msgs[0], 2)) != 2) {
552a4e2b87SHarvey Harrison 		dev_err(&client->dev, "%s: read error\n", __func__);
56edf1aaa3SAlessandro Zummo 		return -EIO;
57edf1aaa3SAlessandro Zummo 	}
58edf1aaa3SAlessandro Zummo 
59edf1aaa3SAlessandro Zummo 	dev_dbg(&client->dev,
6011966adcSJeff Garzik 		"%s: raw read data - counters=%02x,%02x,%02x,%02x\n",
612a4e2b87SHarvey Harrison 		__func__, buf[0], buf[1], buf[2], buf[3]);
62edf1aaa3SAlessandro Zummo 
63edf1aaa3SAlessandro Zummo 	time = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
64edf1aaa3SAlessandro Zummo 
65edf1aaa3SAlessandro Zummo 	rtc_time_to_tm(time, tm);
66edf1aaa3SAlessandro Zummo 
67edf1aaa3SAlessandro Zummo 	dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
68edf1aaa3SAlessandro Zummo 		"mday=%d, mon=%d, year=%d, wday=%d\n",
692a4e2b87SHarvey Harrison 		__func__, tm->tm_sec, tm->tm_min, tm->tm_hour,
70edf1aaa3SAlessandro Zummo 		tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
71edf1aaa3SAlessandro Zummo 
72edf1aaa3SAlessandro Zummo 	return 0;
73edf1aaa3SAlessandro Zummo }
74edf1aaa3SAlessandro Zummo 
75edf1aaa3SAlessandro Zummo static int ds1672_set_mmss(struct i2c_client *client, unsigned long secs)
76edf1aaa3SAlessandro Zummo {
77edf1aaa3SAlessandro Zummo 	int xfer;
788a95b252SKumar Gala 	unsigned char buf[6];
79edf1aaa3SAlessandro Zummo 
80edf1aaa3SAlessandro Zummo 	buf[0] = DS1672_REG_CNT_BASE;
81edf1aaa3SAlessandro Zummo 	buf[1] = secs & 0x000000FF;
82edf1aaa3SAlessandro Zummo 	buf[2] = (secs & 0x0000FF00) >> 8;
83edf1aaa3SAlessandro Zummo 	buf[3] = (secs & 0x00FF0000) >> 16;
84edf1aaa3SAlessandro Zummo 	buf[4] = (secs & 0xFF000000) >> 24;
858a95b252SKumar Gala 	buf[5] = 0;		/* set control reg to enable counting */
86edf1aaa3SAlessandro Zummo 
878a95b252SKumar Gala 	xfer = i2c_master_send(client, buf, 6);
888a95b252SKumar Gala 	if (xfer != 6) {
892a4e2b87SHarvey Harrison 		dev_err(&client->dev, "%s: send: %d\n", __func__, xfer);
90edf1aaa3SAlessandro Zummo 		return -EIO;
91edf1aaa3SAlessandro Zummo 	}
92edf1aaa3SAlessandro Zummo 
93edf1aaa3SAlessandro Zummo 	return 0;
94edf1aaa3SAlessandro Zummo }
95edf1aaa3SAlessandro Zummo 
96edf1aaa3SAlessandro Zummo static int ds1672_rtc_read_time(struct device *dev, struct rtc_time *tm)
97edf1aaa3SAlessandro Zummo {
98edf1aaa3SAlessandro Zummo 	return ds1672_get_datetime(to_i2c_client(dev), tm);
99edf1aaa3SAlessandro Zummo }
100edf1aaa3SAlessandro Zummo 
101edf1aaa3SAlessandro Zummo static int ds1672_rtc_set_mmss(struct device *dev, unsigned long secs)
102edf1aaa3SAlessandro Zummo {
103edf1aaa3SAlessandro Zummo 	return ds1672_set_mmss(to_i2c_client(dev), secs);
104edf1aaa3SAlessandro Zummo }
105edf1aaa3SAlessandro Zummo 
1068a95b252SKumar Gala static int ds1672_get_control(struct i2c_client *client, u8 *status)
1078a95b252SKumar Gala {
1088a95b252SKumar Gala 	unsigned char addr = DS1672_REG_CONTROL;
1098a95b252SKumar Gala 
1108a95b252SKumar Gala 	struct i2c_msg msgs[] = {
1112bfc37dfSShubhrajyoti D 		{/* setup read ptr */
1122bfc37dfSShubhrajyoti D 			.addr = client->addr,
1132bfc37dfSShubhrajyoti D 			.len = 1,
1142bfc37dfSShubhrajyoti D 			.buf = &addr
1152bfc37dfSShubhrajyoti D 		},
1162bfc37dfSShubhrajyoti D 		{/* read control */
1172bfc37dfSShubhrajyoti D 			.addr = client->addr,
1182bfc37dfSShubhrajyoti D 			.flags = I2C_M_RD,
1192bfc37dfSShubhrajyoti D 			.len = 1,
1202bfc37dfSShubhrajyoti D 			.buf = status
1212bfc37dfSShubhrajyoti D 		},
1228a95b252SKumar Gala 	};
1238a95b252SKumar Gala 
1248a95b252SKumar Gala 	/* read control register */
1258a95b252SKumar Gala 	if ((i2c_transfer(client->adapter, &msgs[0], 2)) != 2) {
1262a4e2b87SHarvey Harrison 		dev_err(&client->dev, "%s: read error\n", __func__);
1278a95b252SKumar Gala 		return -EIO;
1288a95b252SKumar Gala 	}
1298a95b252SKumar Gala 
1308a95b252SKumar Gala 	return 0;
1318a95b252SKumar Gala }
1328a95b252SKumar Gala 
1338a95b252SKumar Gala /* following are the sysfs callback functions */
1341716b0feSAlessandro Zummo static ssize_t show_control(struct device *dev, struct device_attribute *attr,
1351716b0feSAlessandro Zummo 			    char *buf)
1368a95b252SKumar Gala {
1378a95b252SKumar Gala 	struct i2c_client *client = to_i2c_client(dev);
1388a95b252SKumar Gala 	u8 control;
1398a95b252SKumar Gala 	int err;
1408a95b252SKumar Gala 
1418a95b252SKumar Gala 	err = ds1672_get_control(client, &control);
1428a95b252SKumar Gala 	if (err)
1438a95b252SKumar Gala 		return err;
1448a95b252SKumar Gala 
1453903586aSAlessandro Zummo 	return sprintf(buf, "%s\n", (control & DS1672_REG_CONTROL_EOSC)
1463903586aSAlessandro Zummo 		       ? "disabled" : "enabled");
1478a95b252SKumar Gala }
1481716b0feSAlessandro Zummo 
1498a95b252SKumar Gala static DEVICE_ATTR(control, S_IRUGO, show_control, NULL);
1508a95b252SKumar Gala 
151ff8371acSDavid Brownell static const struct rtc_class_ops ds1672_rtc_ops = {
152edf1aaa3SAlessandro Zummo 	.read_time = ds1672_rtc_read_time,
153edf1aaa3SAlessandro Zummo 	.set_mmss = ds1672_rtc_set_mmss,
154edf1aaa3SAlessandro Zummo };
155edf1aaa3SAlessandro Zummo 
1561716b0feSAlessandro Zummo static int ds1672_remove(struct i2c_client *client)
157edf1aaa3SAlessandro Zummo {
158edf1aaa3SAlessandro Zummo 	return 0;
159edf1aaa3SAlessandro Zummo }
160edf1aaa3SAlessandro Zummo 
1611716b0feSAlessandro Zummo static int ds1672_probe(struct i2c_client *client,
1621716b0feSAlessandro Zummo 			const struct i2c_device_id *id)
163edf1aaa3SAlessandro Zummo {
164edf1aaa3SAlessandro Zummo 	int err = 0;
1658a95b252SKumar Gala 	u8 control;
166edf1aaa3SAlessandro Zummo 	struct rtc_device *rtc;
167edf1aaa3SAlessandro Zummo 
1681716b0feSAlessandro Zummo 	dev_dbg(&client->dev, "%s\n", __func__);
169edf1aaa3SAlessandro Zummo 
1701716b0feSAlessandro Zummo 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
1711716b0feSAlessandro Zummo 		return -ENODEV;
172edf1aaa3SAlessandro Zummo 
173edf1aaa3SAlessandro Zummo 	dev_info(&client->dev, "chip found, driver version " DRV_VERSION "\n");
174edf1aaa3SAlessandro Zummo 
175*aac8703cSJingoo Han 	rtc = devm_rtc_device_register(&client->dev, ds1672_driver.driver.name,
176edf1aaa3SAlessandro Zummo 				  &ds1672_rtc_ops, THIS_MODULE);
177edf1aaa3SAlessandro Zummo 
1781716b0feSAlessandro Zummo 	if (IS_ERR(rtc))
1791716b0feSAlessandro Zummo 		return PTR_ERR(rtc);
180edf1aaa3SAlessandro Zummo 
181edf1aaa3SAlessandro Zummo 	i2c_set_clientdata(client, rtc);
182edf1aaa3SAlessandro Zummo 
1838a95b252SKumar Gala 	/* read control register */
1848a95b252SKumar Gala 	err = ds1672_get_control(client, &control);
1853903586aSAlessandro Zummo 	if (err)
18691046a8aSJeff Garzik 		goto exit_devreg;
1878a95b252SKumar Gala 
1888a95b252SKumar Gala 	if (control & DS1672_REG_CONTROL_EOSC)
1898a95b252SKumar Gala 		dev_warn(&client->dev, "Oscillator not enabled. "
1908a95b252SKumar Gala 			 "Set time to enable.\n");
1918a95b252SKumar Gala 
1928a95b252SKumar Gala 	/* Register sysfs hooks */
19391046a8aSJeff Garzik 	err = device_create_file(&client->dev, &dev_attr_control);
19491046a8aSJeff Garzik 	if (err)
19591046a8aSJeff Garzik 		goto exit_devreg;
1968a95b252SKumar Gala 
197edf1aaa3SAlessandro Zummo 	return 0;
198edf1aaa3SAlessandro Zummo 
19991046a8aSJeff Garzik  exit_devreg:
200edf1aaa3SAlessandro Zummo 	return err;
201edf1aaa3SAlessandro Zummo }
202edf1aaa3SAlessandro Zummo 
203fe102c71SAlessandro Zummo static struct i2c_device_id ds1672_id[] = {
204fe102c71SAlessandro Zummo 	{ "ds1672", 0 },
205fe102c71SAlessandro Zummo 	{ }
206fe102c71SAlessandro Zummo };
207fe102c71SAlessandro Zummo 
2081716b0feSAlessandro Zummo static struct i2c_driver ds1672_driver = {
2091716b0feSAlessandro Zummo 	.driver = {
2101716b0feSAlessandro Zummo 		   .name = "rtc-ds1672",
2111716b0feSAlessandro Zummo 		   },
2121716b0feSAlessandro Zummo 	.probe = &ds1672_probe,
2131716b0feSAlessandro Zummo 	.remove = &ds1672_remove,
214fe102c71SAlessandro Zummo 	.id_table = ds1672_id,
2151716b0feSAlessandro Zummo };
2161716b0feSAlessandro Zummo 
2170abc9201SAxel Lin module_i2c_driver(ds1672_driver);
218edf1aaa3SAlessandro Zummo 
219edf1aaa3SAlessandro Zummo MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
220edf1aaa3SAlessandro Zummo MODULE_DESCRIPTION("Dallas/Maxim DS1672 timekeeper driver");
221edf1aaa3SAlessandro Zummo MODULE_LICENSE("GPL");
222edf1aaa3SAlessandro Zummo MODULE_VERSION(DRV_VERSION);
223