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
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"
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,
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] = {
65 .flags = 0, /* write */
66 .len = sizeof(reg_addr),
72 .len = MAX6900_REG_LEN,
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",
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] = {
93 .flags = 0, /* write */
94 .len = MAX6900_REG_LEN + 1,
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",
108 msleep(MAX6900_IDLE_TIME_AFTER_WRITE);
112 static int max6900_i2c_validate_client(struct i2c_client *client)
114 u8 regs[MAX6900_REG_LEN];
115 u8 zero_mask[MAX6900_REG_LEN] = {
119 0xc0, /* day of month */
121 0xf8, /* day of week */
129 reserved = i2c_smbus_read_byte_data(client, MAX6900_REG_RESERVED_READ);
130 if (reserved != 0x07)
133 rc = max6900_i2c_read_regs(client, regs);
137 for (i = 0; i < MAX6900_REG_LEN; ++i) {
138 if (regs[i] & zero_mask[i])
145 static int max6900_i2c_read_time(struct i2c_client *client, struct rtc_time *tm)
148 u8 regs[MAX6900_REG_LEN];
150 rc = max6900_i2c_read_regs(client, regs);
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]);
165 static int max6900_i2c_clear_write_protect(struct i2c_client *client)
168 rc = i2c_smbus_write_byte_data (client, MAX6900_REG_CONTROL_WRITE, 0);
170 dev_err(&client->dev, "%s: control register write failed\n",
177 static int max6900_i2c_set_time(struct i2c_client *client,
178 struct rtc_time const *tm)
180 u8 regs[MAX6900_REG_LEN];
183 rc = max6900_i2c_clear_write_protect(client);
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);
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);
223 rtc_device_unregister(rtc);
225 return i2c_detach_client(client);
228 static struct i2c_driver max6900_driver = {
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)
245 struct i2c_client *client = NULL;
246 struct rtc_device *rtc = NULL;
248 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
253 client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
254 if (client == NULL) {
260 client->adapter = adapter;
261 client->driver = &max6900_driver;
262 strlcpy(client->name, DRV_NAME, I2C_NAME_SIZE);
265 rc = max6900_i2c_validate_client(client);
270 rc = i2c_attach_client(client);
274 dev_info(&client->dev,
275 "chip found, driver version " DRV_VERSION "\n");
277 rtc = rtc_device_register(max6900_driver.driver.name,
279 &max6900_rtc_ops, THIS_MODULE);
285 i2c_set_clientdata(client, rtc);
290 i2c_detach_client(client);
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);