Blame view
drivers/rtc/rtc-ds1672.c
5.06 KB
edf1aaa31 [PATCH] RTC subsy... |
1 2 |
/* * An rtc/i2c driver for the Dallas DS1672 |
3903586ab [PATCH] RTC subsy... |
3 4 5 |
* Copyright 2005-06 Tower Technologies * * Author: Alessandro Zummo <a.zummo@towertech.it> |
edf1aaa31 [PATCH] RTC subsy... |
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 [PATCH] RTC subsy... |
11 12 |
#include <linux/i2c.h> #include <linux/rtc.h> |
1716b0fea rtc-ds1672 new st... |
13 |
#define DRV_VERSION "0.4" |
edf1aaa31 [PATCH] RTC subsy... |
14 15 16 17 18 19 |
/* Registers */ #define DS1672_REG_CNT_BASE 0 #define DS1672_REG_CONTROL 4 #define DS1672_REG_TRICKLE 5 |
3903586ab [PATCH] RTC subsy... |
20 |
#define DS1672_REG_CONTROL_EOSC 0x80 |
edf1aaa31 [PATCH] RTC subsy... |
21 |
|
1716b0fea rtc-ds1672 new st... |
22 |
static struct i2c_driver ds1672_driver; |
edf1aaa31 [PATCH] RTC subsy... |
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[] = { |
1716b0fea rtc-ds1672 new st... |
36 37 |
{client->addr, 0, 1, &addr}, /* setup read ptr */ {client->addr, I2C_M_RD, 4, buf}, /* read date */ |
edf1aaa31 [PATCH] RTC subsy... |
38 39 40 41 |
}; /* read date registers */ if ((i2c_transfer(client->adapter, &msgs[0], 2)) != 2) { |
2a4e2b878 rtc: replace rema... |
42 43 |
dev_err(&client->dev, "%s: read error ", __func__); |
edf1aaa31 [PATCH] RTC subsy... |
44 45 46 47 |
return -EIO; } dev_dbg(&client->dev, |
11966adc3 [PATCH] RTC: buil... |
48 49 |
"%s: raw read data - counters=%02x,%02x,%02x,%02x ", |
2a4e2b878 rtc: replace rema... |
50 |
__func__, buf[0], buf[1], buf[2], buf[3]); |
edf1aaa31 [PATCH] RTC subsy... |
51 52 53 54 55 56 57 58 |
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 rtc: replace rema... |
59 |
__func__, tm->tm_sec, tm->tm_min, tm->tm_hour, |
edf1aaa31 [PATCH] RTC subsy... |
60 61 62 63 64 65 66 67 |
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 [PATCH] RTC subsy... |
68 |
unsigned char buf[6]; |
edf1aaa31 [PATCH] RTC subsy... |
69 70 71 72 73 74 |
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 rtc-ds1672 new st... |
75 |
buf[5] = 0; /* set control reg to enable counting */ |
edf1aaa31 [PATCH] RTC subsy... |
76 |
|
8a95b252d [PATCH] RTC subsy... |
77 78 |
xfer = i2c_master_send(client, buf, 6); if (xfer != 6) { |
2a4e2b878 rtc: replace rema... |
79 80 |
dev_err(&client->dev, "%s: send: %d ", __func__, xfer); |
edf1aaa31 [PATCH] RTC subsy... |
81 82 83 84 85 |
return -EIO; } return 0; } |
edf1aaa31 [PATCH] RTC subsy... |
86 87 88 89 |
static int ds1672_rtc_read_time(struct device *dev, struct rtc_time *tm) { return ds1672_get_datetime(to_i2c_client(dev), tm); } |
edf1aaa31 [PATCH] RTC subsy... |
90 91 92 93 |
static int ds1672_rtc_set_mmss(struct device *dev, unsigned long secs) { return ds1672_set_mmss(to_i2c_client(dev), secs); } |
8a95b252d [PATCH] RTC subsy... |
94 95 96 97 98 |
static int ds1672_get_control(struct i2c_client *client, u8 *status) { unsigned char addr = DS1672_REG_CONTROL; struct i2c_msg msgs[] = { |
1716b0fea rtc-ds1672 new st... |
99 100 |
{client->addr, 0, 1, &addr}, /* setup read ptr */ {client->addr, I2C_M_RD, 1, status}, /* read control */ |
8a95b252d [PATCH] RTC subsy... |
101 102 103 104 |
}; /* read control register */ if ((i2c_transfer(client->adapter, &msgs[0], 2)) != 2) { |
2a4e2b878 rtc: replace rema... |
105 106 |
dev_err(&client->dev, "%s: read error ", __func__); |
8a95b252d [PATCH] RTC subsy... |
107 108 109 110 111 112 113 |
return -EIO; } return 0; } /* following are the sysfs callback functions */ |
1716b0fea rtc-ds1672 new st... |
114 115 |
static ssize_t show_control(struct device *dev, struct device_attribute *attr, char *buf) |
8a95b252d [PATCH] RTC subsy... |
116 117 |
{ struct i2c_client *client = to_i2c_client(dev); |
8a95b252d [PATCH] RTC subsy... |
118 119 120 121 122 123 |
u8 control; int err; err = ds1672_get_control(client, &control); if (err) return err; |
3903586ab [PATCH] RTC subsy... |
124 125 |
return sprintf(buf, "%s ", (control & DS1672_REG_CONTROL_EOSC) |
1716b0fea rtc-ds1672 new st... |
126 |
? "disabled" : "enabled"); |
8a95b252d [PATCH] RTC subsy... |
127 |
} |
1716b0fea rtc-ds1672 new st... |
128 |
|
8a95b252d [PATCH] RTC subsy... |
129 |
static DEVICE_ATTR(control, S_IRUGO, show_control, NULL); |
ff8371ac9 [PATCH] constify ... |
130 |
static const struct rtc_class_ops ds1672_rtc_ops = { |
1716b0fea rtc-ds1672 new st... |
131 |
.read_time = ds1672_rtc_read_time, |
1716b0fea rtc-ds1672 new st... |
132 |
.set_mmss = ds1672_rtc_set_mmss, |
edf1aaa31 [PATCH] RTC subsy... |
133 |
}; |
1716b0fea rtc-ds1672 new st... |
134 |
static int ds1672_remove(struct i2c_client *client) |
edf1aaa31 [PATCH] RTC subsy... |
135 |
{ |
edf1aaa31 [PATCH] RTC subsy... |
136 |
struct rtc_device *rtc = i2c_get_clientdata(client); |
1716b0fea rtc-ds1672 new st... |
137 |
if (rtc) |
edf1aaa31 [PATCH] RTC subsy... |
138 |
rtc_device_unregister(rtc); |
edf1aaa31 [PATCH] RTC subsy... |
139 140 |
return 0; } |
1716b0fea rtc-ds1672 new st... |
141 142 |
static int ds1672_probe(struct i2c_client *client, const struct i2c_device_id *id) |
edf1aaa31 [PATCH] RTC subsy... |
143 144 |
{ int err = 0; |
8a95b252d [PATCH] RTC subsy... |
145 |
u8 control; |
edf1aaa31 [PATCH] RTC subsy... |
146 |
struct rtc_device *rtc; |
1716b0fea rtc-ds1672 new st... |
147 148 |
dev_dbg(&client->dev, "%s ", __func__); |
edf1aaa31 [PATCH] RTC subsy... |
149 |
|
1716b0fea rtc-ds1672 new st... |
150 151 |
if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) return -ENODEV; |
edf1aaa31 [PATCH] RTC subsy... |
152 153 154 155 156 |
dev_info(&client->dev, "chip found, driver version " DRV_VERSION " "); rtc = rtc_device_register(ds1672_driver.driver.name, &client->dev, |
1716b0fea rtc-ds1672 new st... |
157 |
&ds1672_rtc_ops, THIS_MODULE); |
edf1aaa31 [PATCH] RTC subsy... |
158 |
|
1716b0fea rtc-ds1672 new st... |
159 160 |
if (IS_ERR(rtc)) return PTR_ERR(rtc); |
edf1aaa31 [PATCH] RTC subsy... |
161 162 |
i2c_set_clientdata(client, rtc); |
8a95b252d [PATCH] RTC subsy... |
163 164 |
/* read control register */ err = ds1672_get_control(client, &control); |
3903586ab [PATCH] RTC subsy... |
165 |
if (err) |
91046a8a6 [PATCH] RTC: hand... |
166 |
goto exit_devreg; |
8a95b252d [PATCH] RTC subsy... |
167 168 169 |
if (control & DS1672_REG_CONTROL_EOSC) dev_warn(&client->dev, "Oscillator not enabled. " |
1716b0fea rtc-ds1672 new st... |
170 171 |
"Set time to enable. "); |
8a95b252d [PATCH] RTC subsy... |
172 173 |
/* Register sysfs hooks */ |
91046a8a6 [PATCH] RTC: hand... |
174 175 176 |
err = device_create_file(&client->dev, &dev_attr_control); if (err) goto exit_devreg; |
8a95b252d [PATCH] RTC subsy... |
177 |
|
edf1aaa31 [PATCH] RTC subsy... |
178 |
return 0; |
1716b0fea rtc-ds1672 new st... |
179 |
exit_devreg: |
91046a8a6 [PATCH] RTC: hand... |
180 |
rtc_device_unregister(rtc); |
edf1aaa31 [PATCH] RTC subsy... |
181 182 |
return err; } |
fe102c71a rtc: fix missing ... |
183 184 185 186 |
static struct i2c_device_id ds1672_id[] = { { "ds1672", 0 }, { } }; |
1716b0fea rtc-ds1672 new st... |
187 188 189 190 191 192 |
static struct i2c_driver ds1672_driver = { .driver = { .name = "rtc-ds1672", }, .probe = &ds1672_probe, .remove = &ds1672_remove, |
fe102c71a rtc: fix missing ... |
193 |
.id_table = ds1672_id, |
1716b0fea rtc-ds1672 new st... |
194 |
}; |
edf1aaa31 [PATCH] RTC subsy... |
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
static int __init ds1672_init(void) { return i2c_add_driver(&ds1672_driver); } static void __exit ds1672_exit(void) { i2c_del_driver(&ds1672_driver); } MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>"); MODULE_DESCRIPTION("Dallas/Maxim DS1672 timekeeper driver"); MODULE_LICENSE("GPL"); MODULE_VERSION(DRV_VERSION); module_init(ds1672_init); module_exit(ds1672_exit); |