[POWERPC] Add for_each_compatible_node()
[usb.git] / drivers / rtc / rtc-max6900.c
blobeee4ee5bb75af15a9688072b46616b80d5949857
1 /*
2 * rtc class driver for the Maxim MAX6900 chip
4 * Author: Dale Farnsworth <dale@farnsworth.org>
6 * based on previously existing rtc class drivers
8 * 2007 (c) MontaVista, Software, Inc. This file is licensed under
9 * the terms of the GNU General Public License version 2. This program
10 * is licensed "as is" without any warranty of any kind, whether express
11 * or implied.
14 #include <linux/module.h>
15 #include <linux/i2c.h>
16 #include <linux/bcd.h>
17 #include <linux/rtc.h>
18 #include <linux/delay.h>
20 #define DRV_NAME "max6900"
21 #define DRV_VERSION "0.1"
24 * register indices
26 #define MAX6900_REG_SC 0 /* seconds 00-59 */
27 #define MAX6900_REG_MN 1 /* minutes 00-59 */
28 #define MAX6900_REG_HR 2 /* hours 00-23 */
29 #define MAX6900_REG_DT 3 /* day of month 00-31 */
30 #define MAX6900_REG_MO 4 /* month 01-12 */
31 #define MAX6900_REG_DW 5 /* day of week 1-7 */
32 #define MAX6900_REG_YR 6 /* year 00-99 */
33 #define MAX6900_REG_CT 7 /* control */
34 #define MAX6900_REG_LEN 8
36 #define MAX6900_REG_CT_WP (1 << 7) /* Write Protect */
39 * register read/write commands
41 #define MAX6900_REG_CONTROL_WRITE 0x8e
42 #define MAX6900_REG_BURST_READ 0xbf
43 #define MAX6900_REG_BURST_WRITE 0xbe
44 #define MAX6900_REG_RESERVED_READ 0x96
46 #define MAX6900_IDLE_TIME_AFTER_WRITE 3 /* specification says 2.5 mS */
48 #define MAX6900_I2C_ADDR 0xa0
50 static unsigned short normal_i2c[] = {
51 MAX6900_I2C_ADDR >> 1,
52 I2C_CLIENT_END
55 I2C_CLIENT_INSMOD; /* defines addr_data */
57 static int max6900_probe(struct i2c_adapter *adapter, int addr, int kind);
59 static int max6900_i2c_read_regs(struct i2c_client *client, u8 *buf)
61 u8 reg_addr[1] = { MAX6900_REG_BURST_READ };
62 struct i2c_msg msgs[2] = {
64 .addr = client->addr,
65 .flags = 0, /* write */
66 .len = sizeof(reg_addr),
67 .buf = reg_addr
70 .addr = client->addr,
71 .flags = I2C_M_RD,
72 .len = MAX6900_REG_LEN,
73 .buf = buf
76 int rc;
78 rc = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
79 if (rc != ARRAY_SIZE(msgs)) {
80 dev_err(&client->dev, "%s: register read failed\n",
81 __FUNCTION__);
82 return -EIO;
84 return 0;
87 static int max6900_i2c_write_regs(struct i2c_client *client, u8 const *buf)
89 u8 i2c_buf[MAX6900_REG_LEN + 1] = { MAX6900_REG_BURST_WRITE };
90 struct i2c_msg msgs[1] = {
92 .addr = client->addr,
93 .flags = 0, /* write */
94 .len = MAX6900_REG_LEN + 1,
95 .buf = i2c_buf
98 int rc;
100 memcpy(&i2c_buf[1], buf, MAX6900_REG_LEN);
102 rc = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
103 if (rc != ARRAY_SIZE(msgs)) {
104 dev_err(&client->dev, "%s: register write failed\n",
105 __FUNCTION__);
106 return -EIO;
108 msleep(MAX6900_IDLE_TIME_AFTER_WRITE);
109 return 0;
112 static int max6900_i2c_validate_client(struct i2c_client *client)
114 u8 regs[MAX6900_REG_LEN];
115 u8 zero_mask[MAX6900_REG_LEN] = {
116 0x80, /* seconds */
117 0x80, /* minutes */
118 0x40, /* hours */
119 0xc0, /* day of month */
120 0xe0, /* month */
121 0xf8, /* day of week */
122 0x00, /* year */
123 0x7f, /* control */
125 int i;
126 int rc;
127 int reserved;
129 reserved = i2c_smbus_read_byte_data(client, MAX6900_REG_RESERVED_READ);
130 if (reserved != 0x07)
131 return -ENODEV;
133 rc = max6900_i2c_read_regs(client, regs);
134 if (rc < 0)
135 return rc;
137 for (i = 0; i < MAX6900_REG_LEN; ++i) {
138 if (regs[i] & zero_mask[i])
139 return -ENODEV;
142 return 0;
145 static int max6900_i2c_read_time(struct i2c_client *client, struct rtc_time *tm)
147 int rc;
148 u8 regs[MAX6900_REG_LEN];
150 rc = max6900_i2c_read_regs(client, regs);
151 if (rc < 0)
152 return rc;
154 tm->tm_sec = BCD2BIN(regs[MAX6900_REG_SC]);
155 tm->tm_min = BCD2BIN(regs[MAX6900_REG_MN]);
156 tm->tm_hour = BCD2BIN(regs[MAX6900_REG_HR] & 0x3f);
157 tm->tm_mday = BCD2BIN(regs[MAX6900_REG_DT]);
158 tm->tm_mon = BCD2BIN(regs[MAX6900_REG_MO]) - 1;
159 tm->tm_year = BCD2BIN(regs[MAX6900_REG_YR]) + 100;
160 tm->tm_wday = BCD2BIN(regs[MAX6900_REG_DW]);
162 return 0;
165 static int max6900_i2c_clear_write_protect(struct i2c_client *client)
167 int rc;
168 rc = i2c_smbus_write_byte_data (client, MAX6900_REG_CONTROL_WRITE, 0);
169 if (rc < 0) {
170 dev_err(&client->dev, "%s: control register write failed\n",
171 __FUNCTION__);
172 return -EIO;
174 return 0;
177 static int max6900_i2c_set_time(struct i2c_client *client,
178 struct rtc_time const *tm)
180 u8 regs[MAX6900_REG_LEN];
181 int rc;
183 rc = max6900_i2c_clear_write_protect(client);
184 if (rc < 0)
185 return rc;
187 regs[MAX6900_REG_SC] = BIN2BCD(tm->tm_sec);
188 regs[MAX6900_REG_MN] = BIN2BCD(tm->tm_min);
189 regs[MAX6900_REG_HR] = BIN2BCD(tm->tm_hour);
190 regs[MAX6900_REG_DT] = BIN2BCD(tm->tm_mday);
191 regs[MAX6900_REG_MO] = BIN2BCD(tm->tm_mon + 1);
192 regs[MAX6900_REG_YR] = BIN2BCD(tm->tm_year - 100);
193 regs[MAX6900_REG_DW] = BIN2BCD(tm->tm_wday);
194 regs[MAX6900_REG_CT] = MAX6900_REG_CT_WP; /* set write protect */
196 rc = max6900_i2c_write_regs(client, regs);
197 if (rc < 0)
198 return rc;
200 return 0;
203 static int max6900_rtc_read_time(struct device *dev, struct rtc_time *tm)
205 return max6900_i2c_read_time(to_i2c_client(dev), tm);
208 static int max6900_rtc_set_time(struct device *dev, struct rtc_time *tm)
210 return max6900_i2c_set_time(to_i2c_client(dev), tm);
213 static int max6900_attach_adapter(struct i2c_adapter *adapter)
215 return i2c_probe(adapter, &addr_data, max6900_probe);
218 static int max6900_detach_client(struct i2c_client *client)
220 struct rtc_device *const rtc = i2c_get_clientdata(client);
222 if (rtc)
223 rtc_device_unregister(rtc);
225 return i2c_detach_client(client);
228 static struct i2c_driver max6900_driver = {
229 .driver = {
230 .name = DRV_NAME,
232 .id = I2C_DRIVERID_MAX6900,
233 .attach_adapter = max6900_attach_adapter,
234 .detach_client = max6900_detach_client,
237 static const struct rtc_class_ops max6900_rtc_ops = {
238 .read_time = max6900_rtc_read_time,
239 .set_time = max6900_rtc_set_time,
242 static int max6900_probe(struct i2c_adapter *adapter, int addr, int kind)
244 int rc = 0;
245 struct i2c_client *client = NULL;
246 struct rtc_device *rtc = NULL;
248 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
249 rc = -ENODEV;
250 goto failout;
253 client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
254 if (client == NULL) {
255 rc = -ENOMEM;
256 goto failout;
259 client->addr = addr;
260 client->adapter = adapter;
261 client->driver = &max6900_driver;
262 strlcpy(client->name, DRV_NAME, I2C_NAME_SIZE);
264 if (kind < 0) {
265 rc = max6900_i2c_validate_client(client);
266 if (rc < 0)
267 goto failout;
270 rc = i2c_attach_client(client);
271 if (rc < 0)
272 goto failout;
274 dev_info(&client->dev,
275 "chip found, driver version " DRV_VERSION "\n");
277 rtc = rtc_device_register(max6900_driver.driver.name,
278 &client->dev,
279 &max6900_rtc_ops, THIS_MODULE);
280 if (IS_ERR(rtc)) {
281 rc = PTR_ERR(rtc);
282 goto failout_detach;
285 i2c_set_clientdata(client, rtc);
287 return 0;
289 failout_detach:
290 i2c_detach_client(client);
291 failout:
292 kfree(client);
293 return rc;
296 static int __init max6900_init(void)
298 return i2c_add_driver(&max6900_driver);
301 static void __exit max6900_exit(void)
303 i2c_del_driver(&max6900_driver);
306 MODULE_DESCRIPTION("Maxim MAX6900 RTC driver");
307 MODULE_LICENSE("GPL");
308 MODULE_VERSION(DRV_VERSION);
310 module_init(max6900_init);
311 module_exit(max6900_exit);