blob: dfef1637bfb8a1a69dc1848d75e2b490a458ddaf [file] [log] [blame]
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -08001/*
2 * An rtc/i2c driver for the Dallas DS1672
Alessandro Zummo39035862006-04-10 22:54:41 -07003 * Copyright 2005-06 Tower Technologies
4 *
5 * Author: Alessandro Zummo <a.zummo@towertech.it>
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -08006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
12#include <linux/module.h>
13#include <linux/i2c.h>
14#include <linux/rtc.h>
15
Alessandro Zummo39035862006-04-10 22:54:41 -070016#define DRV_VERSION "0.3"
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -080017
18/* Addresses to scan: none. This chip cannot be detected. */
19static unsigned short normal_i2c[] = { I2C_CLIENT_END };
20
21/* Insmod parameters */
22I2C_CLIENT_INSMOD;
23
24/* Registers */
25
26#define DS1672_REG_CNT_BASE 0
27#define DS1672_REG_CONTROL 4
28#define DS1672_REG_TRICKLE 5
29
Alessandro Zummo39035862006-04-10 22:54:41 -070030#define DS1672_REG_CONTROL_EOSC 0x80
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -080031
32/* Prototypes */
33static int ds1672_probe(struct i2c_adapter *adapter, int address, int kind);
34
35/*
36 * In the routines that deal directly with the ds1672 hardware, we use
37 * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch
38 * Epoch is initialized as 2000. Time is set to UTC.
39 */
40static int ds1672_get_datetime(struct i2c_client *client, struct rtc_time *tm)
41{
42 unsigned long time;
43 unsigned char addr = DS1672_REG_CNT_BASE;
44 unsigned char buf[4];
45
46 struct i2c_msg msgs[] = {
47 { client->addr, 0, 1, &addr }, /* setup read ptr */
48 { client->addr, I2C_M_RD, 4, buf }, /* read date */
49 };
50
51 /* read date registers */
52 if ((i2c_transfer(client->adapter, &msgs[0], 2)) != 2) {
53 dev_err(&client->dev, "%s: read error\n", __FUNCTION__);
54 return -EIO;
55 }
56
57 dev_dbg(&client->dev,
Jeff Garzik11966ad2006-10-04 04:41:53 -040058 "%s: raw read data - counters=%02x,%02x,%02x,%02x\n",
Alessandro Zummo39035862006-04-10 22:54:41 -070059 __FUNCTION__, buf[0], buf[1], buf[2], buf[3]);
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -080060
61 time = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
62
63 rtc_time_to_tm(time, tm);
64
65 dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
66 "mday=%d, mon=%d, year=%d, wday=%d\n",
Alessandro Zummo39035862006-04-10 22:54:41 -070067 __FUNCTION__, tm->tm_sec, tm->tm_min, tm->tm_hour,
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -080068 tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
69
70 return 0;
71}
72
73static int ds1672_set_mmss(struct i2c_client *client, unsigned long secs)
74{
75 int xfer;
Kumar Gala8a95b252006-04-10 22:54:39 -070076 unsigned char buf[6];
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -080077
78 buf[0] = DS1672_REG_CNT_BASE;
79 buf[1] = secs & 0x000000FF;
80 buf[2] = (secs & 0x0000FF00) >> 8;
81 buf[3] = (secs & 0x00FF0000) >> 16;
82 buf[4] = (secs & 0xFF000000) >> 24;
Kumar Gala8a95b252006-04-10 22:54:39 -070083 buf[5] = 0; /* set control reg to enable counting */
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -080084
Kumar Gala8a95b252006-04-10 22:54:39 -070085 xfer = i2c_master_send(client, buf, 6);
86 if (xfer != 6) {
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -080087 dev_err(&client->dev, "%s: send: %d\n", __FUNCTION__, xfer);
88 return -EIO;
89 }
90
91 return 0;
92}
93
94static int ds1672_set_datetime(struct i2c_client *client, struct rtc_time *tm)
95{
96 unsigned long secs;
97
98 dev_dbg(&client->dev,
Jeff Garzik11966ad2006-10-04 04:41:53 -040099 "%s: secs=%d, mins=%d, hours=%d, "
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -0800100 "mday=%d, mon=%d, year=%d, wday=%d\n",
101 __FUNCTION__,
102 tm->tm_sec, tm->tm_min, tm->tm_hour,
103 tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
104
105 rtc_tm_to_time(tm, &secs);
106
107 return ds1672_set_mmss(client, secs);
108}
109
110static int ds1672_rtc_read_time(struct device *dev, struct rtc_time *tm)
111{
112 return ds1672_get_datetime(to_i2c_client(dev), tm);
113}
114
115static int ds1672_rtc_set_time(struct device *dev, struct rtc_time *tm)
116{
117 return ds1672_set_datetime(to_i2c_client(dev), tm);
118}
119
120static int ds1672_rtc_set_mmss(struct device *dev, unsigned long secs)
121{
122 return ds1672_set_mmss(to_i2c_client(dev), secs);
123}
124
Kumar Gala8a95b252006-04-10 22:54:39 -0700125static int ds1672_get_control(struct i2c_client *client, u8 *status)
126{
127 unsigned char addr = DS1672_REG_CONTROL;
128
129 struct i2c_msg msgs[] = {
130 { client->addr, 0, 1, &addr }, /* setup read ptr */
131 { client->addr, I2C_M_RD, 1, status }, /* read control */
132 };
133
134 /* read control register */
135 if ((i2c_transfer(client->adapter, &msgs[0], 2)) != 2) {
136 dev_err(&client->dev, "%s: read error\n", __FUNCTION__);
137 return -EIO;
138 }
139
140 return 0;
141}
142
143/* following are the sysfs callback functions */
144static ssize_t show_control(struct device *dev, struct device_attribute *attr, char *buf)
145{
146 struct i2c_client *client = to_i2c_client(dev);
Kumar Gala8a95b252006-04-10 22:54:39 -0700147 u8 control;
148 int err;
149
150 err = ds1672_get_control(client, &control);
151 if (err)
152 return err;
153
Alessandro Zummo39035862006-04-10 22:54:41 -0700154 return sprintf(buf, "%s\n", (control & DS1672_REG_CONTROL_EOSC)
155 ? "disabled" : "enabled");
Kumar Gala8a95b252006-04-10 22:54:39 -0700156}
Kumar Gala8a95b252006-04-10 22:54:39 -0700157static DEVICE_ATTR(control, S_IRUGO, show_control, NULL);
158
David Brownellff8371a2006-09-30 23:28:17 -0700159static const struct rtc_class_ops ds1672_rtc_ops = {
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -0800160 .read_time = ds1672_rtc_read_time,
161 .set_time = ds1672_rtc_set_time,
162 .set_mmss = ds1672_rtc_set_mmss,
163};
164
165static int ds1672_attach(struct i2c_adapter *adapter)
166{
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -0800167 return i2c_probe(adapter, &addr_data, ds1672_probe);
168}
169
170static int ds1672_detach(struct i2c_client *client)
171{
172 int err;
173 struct rtc_device *rtc = i2c_get_clientdata(client);
174
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -0800175 if (rtc)
176 rtc_device_unregister(rtc);
177
178 if ((err = i2c_detach_client(client)))
179 return err;
180
181 kfree(client);
182
183 return 0;
184}
185
186static struct i2c_driver ds1672_driver = {
187 .driver = {
188 .name = "ds1672",
189 },
190 .id = I2C_DRIVERID_DS1672,
191 .attach_adapter = &ds1672_attach,
192 .detach_client = &ds1672_detach,
193};
194
195static int ds1672_probe(struct i2c_adapter *adapter, int address, int kind)
196{
197 int err = 0;
Kumar Gala8a95b252006-04-10 22:54:39 -0700198 u8 control;
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -0800199 struct i2c_client *client;
200 struct rtc_device *rtc;
201
Jean Delvare5dd3ffa2007-02-13 22:09:04 +0100202 dev_dbg(&adapter->dev, "%s\n", __FUNCTION__);
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -0800203
204 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
205 err = -ENODEV;
206 goto exit;
207 }
208
209 if (!(client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL))) {
210 err = -ENOMEM;
211 goto exit;
212 }
213
214 /* I2C client */
215 client->addr = address;
216 client->driver = &ds1672_driver;
217 client->adapter = adapter;
218
219 strlcpy(client->name, ds1672_driver.driver.name, I2C_NAME_SIZE);
220
221 /* Inform the i2c layer */
222 if ((err = i2c_attach_client(client)))
223 goto exit_kfree;
224
225 dev_info(&client->dev, "chip found, driver version " DRV_VERSION "\n");
226
227 rtc = rtc_device_register(ds1672_driver.driver.name, &client->dev,
228 &ds1672_rtc_ops, THIS_MODULE);
229
230 if (IS_ERR(rtc)) {
231 err = PTR_ERR(rtc);
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -0800232 goto exit_detach;
233 }
234
235 i2c_set_clientdata(client, rtc);
236
Kumar Gala8a95b252006-04-10 22:54:39 -0700237 /* read control register */
238 err = ds1672_get_control(client, &control);
Alessandro Zummo39035862006-04-10 22:54:41 -0700239 if (err)
Jeff Garzik91046a82006-12-06 20:35:34 -0800240 goto exit_devreg;
Kumar Gala8a95b252006-04-10 22:54:39 -0700241
242 if (control & DS1672_REG_CONTROL_EOSC)
243 dev_warn(&client->dev, "Oscillator not enabled. "
244 "Set time to enable.\n");
245
246 /* Register sysfs hooks */
Jeff Garzik91046a82006-12-06 20:35:34 -0800247 err = device_create_file(&client->dev, &dev_attr_control);
248 if (err)
249 goto exit_devreg;
Kumar Gala8a95b252006-04-10 22:54:39 -0700250
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -0800251 return 0;
252
Jeff Garzik91046a82006-12-06 20:35:34 -0800253exit_devreg:
254 rtc_device_unregister(rtc);
255
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -0800256exit_detach:
257 i2c_detach_client(client);
258
259exit_kfree:
260 kfree(client);
261
262exit:
263 return err;
264}
265
266static int __init ds1672_init(void)
267{
268 return i2c_add_driver(&ds1672_driver);
269}
270
271static void __exit ds1672_exit(void)
272{
273 i2c_del_driver(&ds1672_driver);
274}
275
276MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
277MODULE_DESCRIPTION("Dallas/Maxim DS1672 timekeeper driver");
278MODULE_LICENSE("GPL");
279MODULE_VERSION(DRV_VERSION);
280
281module_init(ds1672_init);
282module_exit(ds1672_exit);