Commit | Line | Data |
---|---|---|
aa5bd7e9 DF |
1 | /* |
2 | * rtc class driver for the Maxim MAX6900 chip | |
3 | * | |
4 | * Author: Dale Farnsworth <dale@farnsworth.org> | |
5 | * | |
6 | * based on previously existing rtc class drivers | |
7 | * | |
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. | |
12 | */ | |
13 | ||
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> | |
19 | ||
20 | #define DRV_NAME "max6900" | |
21 | #define DRV_VERSION "0.1" | |
22 | ||
23 | /* | |
24 | * register indices | |
25 | */ | |
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 | |
35 | ||
36 | #define MAX6900_REG_CT_WP (1 << 7) /* Write Protect */ | |
37 | ||
38 | /* | |
39 | * register read/write commands | |
40 | */ | |
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 | |
45 | ||
46 | #define MAX6900_IDLE_TIME_AFTER_WRITE 3 /* specification says 2.5 mS */ | |
47 | ||
48 | #define MAX6900_I2C_ADDR 0xa0 | |
49 | ||
50 | static unsigned short normal_i2c[] = { | |
51 | MAX6900_I2C_ADDR >> 1, | |
52 | I2C_CLIENT_END | |
53 | }; | |
54 | ||
55 | I2C_CLIENT_INSMOD; /* defines addr_data */ | |
56 | ||
57 | static int max6900_probe(struct i2c_adapter *adapter, int addr, int kind); | |
58 | ||
59 | static int max6900_i2c_read_regs(struct i2c_client *client, u8 *buf) | |
60 | { | |
61 | u8 reg_addr[1] = { MAX6900_REG_BURST_READ }; | |
62 | struct i2c_msg msgs[2] = { | |
63 | { | |
64 | .addr = client->addr, | |
65 | .flags = 0, /* write */ | |
66 | .len = sizeof(reg_addr), | |
67 | .buf = reg_addr | |
68 | }, | |
69 | { | |
70 | .addr = client->addr, | |
71 | .flags = I2C_M_RD, | |
72 | .len = MAX6900_REG_LEN, | |
73 | .buf = buf | |
74 | } | |
75 | }; | |
76 | int rc; | |
77 | ||
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; | |
83 | } | |
84 | return 0; | |
85 | } | |
86 | ||
87 | static int max6900_i2c_write_regs(struct i2c_client *client, u8 const *buf) | |
88 | { | |
89 | u8 i2c_buf[MAX6900_REG_LEN + 1] = { MAX6900_REG_BURST_WRITE }; | |
90 | struct i2c_msg msgs[1] = { | |
91 | { | |
92 | .addr = client->addr, | |
93 | .flags = 0, /* write */ | |
94 | .len = MAX6900_REG_LEN + 1, | |
95 | .buf = i2c_buf | |
96 | } | |
97 | }; | |
98 | int rc; | |
99 | ||
100 | memcpy(&i2c_buf[1], buf, MAX6900_REG_LEN); | |
101 | ||
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; | |
107 | } | |
108 | msleep(MAX6900_IDLE_TIME_AFTER_WRITE); | |
109 | return 0; | |
110 | } | |
111 | ||
112 | static int max6900_i2c_validate_client(struct i2c_client *client) | |
113 | { | |
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 */ | |
124 | }; | |
125 | int i; | |
126 | int rc; | |
127 | int reserved; | |
128 | ||
129 | reserved = i2c_smbus_read_byte_data(client, MAX6900_REG_RESERVED_READ); | |
130 | if (reserved != 0x07) | |
131 | return -ENODEV; | |
132 | ||
133 | rc = max6900_i2c_read_regs(client, regs); | |
134 | if (rc < 0) | |
135 | return rc; | |
136 | ||
137 | for (i = 0; i < MAX6900_REG_LEN; ++i) { | |
138 | if (regs[i] & zero_mask[i]) | |
139 | return -ENODEV; | |
140 | } | |
141 | ||
142 | return 0; | |
143 | } | |
144 | ||
145 | static int max6900_i2c_read_time(struct i2c_client *client, struct rtc_time *tm) | |
146 | { | |
147 | int rc; | |
148 | u8 regs[MAX6900_REG_LEN]; | |
149 | ||
150 | rc = max6900_i2c_read_regs(client, regs); | |
151 | if (rc < 0) | |
152 | return rc; | |
153 | ||
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]); | |
161 | ||
162 | return 0; | |
163 | } | |
164 | ||
165 | static int max6900_i2c_clear_write_protect(struct i2c_client *client) | |
166 | { | |
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; | |
173 | } | |
174 | return 0; | |
175 | } | |
176 | ||
177 | static int max6900_i2c_set_time(struct i2c_client *client, | |
178 | struct rtc_time const *tm) | |
179 | { | |
180 | u8 regs[MAX6900_REG_LEN]; | |
181 | int rc; | |
182 | ||
183 | rc = max6900_i2c_clear_write_protect(client); | |
184 | if (rc < 0) | |
185 | return rc; | |
186 | ||
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 */ | |
195 | ||
196 | rc = max6900_i2c_write_regs(client, regs); | |
197 | if (rc < 0) | |
198 | return rc; | |
199 | ||
200 | return 0; | |
201 | } | |
202 | ||
203 | static int max6900_rtc_read_time(struct device *dev, struct rtc_time *tm) | |
204 | { | |
205 | return max6900_i2c_read_time(to_i2c_client(dev), tm); | |
206 | } | |
207 | ||
208 | static int max6900_rtc_set_time(struct device *dev, struct rtc_time *tm) | |
209 | { | |
210 | return max6900_i2c_set_time(to_i2c_client(dev), tm); | |
211 | } | |
212 | ||
213 | static int max6900_attach_adapter(struct i2c_adapter *adapter) | |
214 | { | |
215 | return i2c_probe(adapter, &addr_data, max6900_probe); | |
216 | } | |
217 | ||
218 | static int max6900_detach_client(struct i2c_client *client) | |
219 | { | |
220 | struct rtc_device *const rtc = i2c_get_clientdata(client); | |
221 | ||
222 | if (rtc) | |
223 | rtc_device_unregister(rtc); | |
224 | ||
225 | return i2c_detach_client(client); | |
226 | } | |
227 | ||
228 | static struct i2c_driver max6900_driver = { | |
229 | .driver = { | |
230 | .name = DRV_NAME, | |
231 | }, | |
232 | .id = I2C_DRIVERID_MAX6900, | |
233 | .attach_adapter = max6900_attach_adapter, | |
234 | .detach_client = max6900_detach_client, | |
235 | }; | |
236 | ||
237 | static const struct rtc_class_ops max6900_rtc_ops = { | |
238 | .read_time = max6900_rtc_read_time, | |
239 | .set_time = max6900_rtc_set_time, | |
240 | }; | |
241 | ||
242 | static int max6900_probe(struct i2c_adapter *adapter, int addr, int kind) | |
243 | { | |
244 | int rc = 0; | |
245 | struct i2c_client *client = NULL; | |
246 | struct rtc_device *rtc = NULL; | |
247 | ||
248 | if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) { | |
249 | rc = -ENODEV; | |
250 | goto failout; | |
251 | } | |
252 | ||
253 | client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL); | |
254 | if (client == NULL) { | |
255 | rc = -ENOMEM; | |
256 | goto failout; | |
257 | } | |
258 | ||
259 | client->addr = addr; | |
260 | client->adapter = adapter; | |
261 | client->driver = &max6900_driver; | |
262 | strlcpy(client->name, DRV_NAME, I2C_NAME_SIZE); | |
263 | ||
264 | if (kind < 0) { | |
265 | rc = max6900_i2c_validate_client(client); | |
266 | if (rc < 0) | |
267 | goto failout; | |
268 | } | |
269 | ||
270 | rc = i2c_attach_client(client); | |
271 | if (rc < 0) | |
272 | goto failout; | |
273 | ||
274 | dev_info(&client->dev, | |
275 | "chip found, driver version " DRV_VERSION "\n"); | |
276 | ||
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; | |
283 | } | |
284 | ||
285 | i2c_set_clientdata(client, rtc); | |
286 | ||
287 | return 0; | |
288 | ||
289 | failout_detach: | |
290 | i2c_detach_client(client); | |
291 | failout: | |
292 | kfree(client); | |
293 | return rc; | |
294 | } | |
295 | ||
296 | static int __init max6900_init(void) | |
297 | { | |
298 | return i2c_add_driver(&max6900_driver); | |
299 | } | |
300 | ||
301 | static void __exit max6900_exit(void) | |
302 | { | |
303 | i2c_del_driver(&max6900_driver); | |
304 | } | |
305 | ||
306 | MODULE_DESCRIPTION("Maxim MAX6900 RTC driver"); | |
307 | MODULE_LICENSE("GPL"); | |
308 | MODULE_VERSION(DRV_VERSION); | |
309 | ||
310 | module_init(max6900_init); | |
311 | module_exit(max6900_exit); |