Blame view
drivers/rtc/rtc-ds1672.c
4.86 KB
edf1aaa31
|
1 2 |
/* * An rtc/i2c driver for the Dallas DS1672 |
3903586ab
|
3 4 5 |
* Copyright 2005-06 Tower Technologies * * Author: Alessandro Zummo <a.zummo@towertech.it> |
edf1aaa31
|
6 7 8 9 10 |
* * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. */ |
edf1aaa31
|
11 12 |
#include <linux/i2c.h> #include <linux/rtc.h> |
2113852b2
|
13 |
#include <linux/module.h> |
edf1aaa31
|
14 |
|
edf1aaa31
|
15 16 17 18 19 |
/* Registers */ #define DS1672_REG_CNT_BASE 0 #define DS1672_REG_CONTROL 4 #define DS1672_REG_TRICKLE 5 |
3903586ab
|
20 |
#define DS1672_REG_CONTROL_EOSC 0x80 |
edf1aaa31
|
21 |
|
1716b0fea
|
22 |
static struct i2c_driver ds1672_driver; |
edf1aaa31
|
23 24 25 26 27 28 29 30 31 32 33 34 35 |
/* * In the routines that deal directly with the ds1672 hardware, we use * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch * Epoch is initialized as 2000. Time is set to UTC. */ static int ds1672_get_datetime(struct i2c_client *client, struct rtc_time *tm) { unsigned long time; unsigned char addr = DS1672_REG_CNT_BASE; unsigned char buf[4]; struct i2c_msg msgs[] = { |
2bfc37dff
|
36 37 38 39 40 41 42 43 44 45 46 |
{/* setup read ptr */ .addr = client->addr, .len = 1, .buf = &addr }, {/* read date */ .addr = client->addr, .flags = I2C_M_RD, .len = 4, .buf = buf }, |
edf1aaa31
|
47 48 49 50 |
}; /* read date registers */ if ((i2c_transfer(client->adapter, &msgs[0], 2)) != 2) { |
2a4e2b878
|
51 52 |
dev_err(&client->dev, "%s: read error ", __func__); |
edf1aaa31
|
53 54 55 56 |
return -EIO; } dev_dbg(&client->dev, |
11966adc3
|
57 58 |
"%s: raw read data - counters=%02x,%02x,%02x,%02x ", |
2a4e2b878
|
59 |
__func__, buf[0], buf[1], buf[2], buf[3]); |
edf1aaa31
|
60 61 62 63 64 65 66 67 |
time = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0]; rtc_time_to_tm(time, tm); dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d, " "mday=%d, mon=%d, year=%d, wday=%d ", |
2a4e2b878
|
68 |
__func__, tm->tm_sec, tm->tm_min, tm->tm_hour, |
edf1aaa31
|
69 70 71 72 73 74 75 76 |
tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday); return 0; } static int ds1672_set_mmss(struct i2c_client *client, unsigned long secs) { int xfer; |
8a95b252d
|
77 |
unsigned char buf[6]; |
edf1aaa31
|
78 79 80 81 82 83 |
buf[0] = DS1672_REG_CNT_BASE; buf[1] = secs & 0x000000FF; buf[2] = (secs & 0x0000FF00) >> 8; buf[3] = (secs & 0x00FF0000) >> 16; buf[4] = (secs & 0xFF000000) >> 24; |
1716b0fea
|
84 |
buf[5] = 0; /* set control reg to enable counting */ |
edf1aaa31
|
85 |
|
8a95b252d
|
86 87 |
xfer = i2c_master_send(client, buf, 6); if (xfer != 6) { |
2a4e2b878
|
88 89 |
dev_err(&client->dev, "%s: send: %d ", __func__, xfer); |
edf1aaa31
|
90 91 92 93 94 |
return -EIO; } return 0; } |
edf1aaa31
|
95 96 97 98 |
static int ds1672_rtc_read_time(struct device *dev, struct rtc_time *tm) { return ds1672_get_datetime(to_i2c_client(dev), tm); } |
edf1aaa31
|
99 100 101 102 |
static int ds1672_rtc_set_mmss(struct device *dev, unsigned long secs) { return ds1672_set_mmss(to_i2c_client(dev), secs); } |
8a95b252d
|
103 104 105 106 107 |
static int ds1672_get_control(struct i2c_client *client, u8 *status) { unsigned char addr = DS1672_REG_CONTROL; struct i2c_msg msgs[] = { |
2bfc37dff
|
108 109 110 111 112 113 114 115 116 117 118 |
{/* setup read ptr */ .addr = client->addr, .len = 1, .buf = &addr }, {/* read control */ .addr = client->addr, .flags = I2C_M_RD, .len = 1, .buf = status }, |
8a95b252d
|
119 120 121 122 |
}; /* read control register */ if ((i2c_transfer(client->adapter, &msgs[0], 2)) != 2) { |
2a4e2b878
|
123 124 |
dev_err(&client->dev, "%s: read error ", __func__); |
8a95b252d
|
125 126 127 128 129 130 131 |
return -EIO; } return 0; } /* following are the sysfs callback functions */ |
1716b0fea
|
132 133 |
static ssize_t show_control(struct device *dev, struct device_attribute *attr, char *buf) |
8a95b252d
|
134 135 |
{ struct i2c_client *client = to_i2c_client(dev); |
8a95b252d
|
136 137 138 139 140 141 |
u8 control; int err; err = ds1672_get_control(client, &control); if (err) return err; |
3903586ab
|
142 143 |
return sprintf(buf, "%s ", (control & DS1672_REG_CONTROL_EOSC) |
1716b0fea
|
144 |
? "disabled" : "enabled"); |
8a95b252d
|
145 |
} |
1716b0fea
|
146 |
|
8a95b252d
|
147 |
static DEVICE_ATTR(control, S_IRUGO, show_control, NULL); |
ff8371ac9
|
148 |
static const struct rtc_class_ops ds1672_rtc_ops = { |
1716b0fea
|
149 |
.read_time = ds1672_rtc_read_time, |
1716b0fea
|
150 |
.set_mmss = ds1672_rtc_set_mmss, |
edf1aaa31
|
151 |
}; |
1716b0fea
|
152 153 |
static int ds1672_probe(struct i2c_client *client, const struct i2c_device_id *id) |
edf1aaa31
|
154 155 |
{ int err = 0; |
8a95b252d
|
156 |
u8 control; |
edf1aaa31
|
157 |
struct rtc_device *rtc; |
1716b0fea
|
158 159 |
dev_dbg(&client->dev, "%s ", __func__); |
edf1aaa31
|
160 |
|
1716b0fea
|
161 162 |
if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) return -ENODEV; |
edf1aaa31
|
163 |
|
aac8703c1
|
164 |
rtc = devm_rtc_device_register(&client->dev, ds1672_driver.driver.name, |
1716b0fea
|
165 |
&ds1672_rtc_ops, THIS_MODULE); |
edf1aaa31
|
166 |
|
1716b0fea
|
167 168 |
if (IS_ERR(rtc)) return PTR_ERR(rtc); |
edf1aaa31
|
169 170 |
i2c_set_clientdata(client, rtc); |
8a95b252d
|
171 172 |
/* read control register */ err = ds1672_get_control(client, &control); |
4071ea25c
|
173 174 175 176 |
if (err) { dev_warn(&client->dev, "Unable to read the control register "); } |
8a95b252d
|
177 178 179 |
if (control & DS1672_REG_CONTROL_EOSC) dev_warn(&client->dev, "Oscillator not enabled. " |
1716b0fea
|
180 181 |
"Set time to enable. "); |
8a95b252d
|
182 183 |
/* Register sysfs hooks */ |
91046a8a6
|
184 185 |
err = device_create_file(&client->dev, &dev_attr_control); if (err) |
4071ea25c
|
186 187 188 |
dev_err(&client->dev, "Unable to create sysfs entry: %s ", dev_attr_control.attr.name); |
8a95b252d
|
189 |
|
edf1aaa31
|
190 |
return 0; |
edf1aaa31
|
191 |
} |
fe102c71a
|
192 193 194 195 |
static struct i2c_device_id ds1672_id[] = { { "ds1672", 0 }, { } }; |
8ad0f5b68
|
196 |
MODULE_DEVICE_TABLE(i2c, ds1672_id); |
fe102c71a
|
197 |
|
1716b0fea
|
198 199 200 201 202 |
static struct i2c_driver ds1672_driver = { .driver = { .name = "rtc-ds1672", }, .probe = &ds1672_probe, |
fe102c71a
|
203 |
.id_table = ds1672_id, |
1716b0fea
|
204 |
}; |
0abc92011
|
205 |
module_i2c_driver(ds1672_driver); |
edf1aaa31
|
206 207 208 209 |
MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>"); MODULE_DESCRIPTION("Dallas/Maxim DS1672 timekeeper driver"); MODULE_LICENSE("GPL"); |