rtc: rv3029: Add "rv3029" I2C device id
[deliverable/linux.git] / drivers / rtc / rtc-rv3029c2.c
1 /*
2 * Micro Crystal RV-3029 rtc class driver
3 *
4 * Author: Gregory Hermant <gregory.hermant@calao-systems.com>
5 *
6 * based on previously existing rtc class drivers
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 *
12 * NOTE: Currently this driver only supports the bare minimum for read
13 * and write the RTC and alarms. The extra features provided by this chip
14 * (trickle charger, eeprom, T° compensation) are unavailable.
15 */
16
17 #include <linux/module.h>
18 #include <linux/i2c.h>
19 #include <linux/bcd.h>
20 #include <linux/rtc.h>
21
22 /* Register map */
23 /* control section */
24 #define RV3029_ONOFF_CTRL 0x00
25 #define RV3029_IRQ_CTRL 0x01
26 #define RV3029_IRQ_CTRL_AIE (1 << 0)
27 #define RV3029_IRQ_FLAGS 0x02
28 #define RV3029_IRQ_FLAGS_AF (1 << 0)
29 #define RV3029_STATUS 0x03
30 #define RV3029_STATUS_VLOW1 (1 << 2)
31 #define RV3029_STATUS_VLOW2 (1 << 3)
32 #define RV3029_STATUS_SR (1 << 4)
33 #define RV3029_STATUS_PON (1 << 5)
34 #define RV3029_STATUS_EEBUSY (1 << 7)
35 #define RV3029_RST_CTRL 0x04
36 #define RV3029_CONTROL_SECTION_LEN 0x05
37
38 /* watch section */
39 #define RV3029_W_SEC 0x08
40 #define RV3029_W_MINUTES 0x09
41 #define RV3029_W_HOURS 0x0A
42 #define RV3029_REG_HR_12_24 (1<<6) /* 24h/12h mode */
43 #define RV3029_REG_HR_PM (1<<5) /* PM/AM bit in 12h mode */
44 #define RV3029_W_DATE 0x0B
45 #define RV3029_W_DAYS 0x0C
46 #define RV3029_W_MONTHS 0x0D
47 #define RV3029_W_YEARS 0x0E
48 #define RV3029_WATCH_SECTION_LEN 0x07
49
50 /* alarm section */
51 #define RV3029_A_SC 0x10
52 #define RV3029_A_MN 0x11
53 #define RV3029_A_HR 0x12
54 #define RV3029_A_DT 0x13
55 #define RV3029_A_DW 0x14
56 #define RV3029_A_MO 0x15
57 #define RV3029_A_YR 0x16
58 #define RV3029_ALARM_SECTION_LEN 0x07
59
60 /* timer section */
61 #define RV3029_TIMER_LOW 0x18
62 #define RV3029_TIMER_HIGH 0x19
63
64 /* temperature section */
65 #define RV3029_TEMP_PAGE 0x20
66
67 /* eeprom data section */
68 #define RV3029_E2P_EEDATA1 0x28
69 #define RV3029_E2P_EEDATA2 0x29
70
71 /* eeprom control section */
72 #define RV3029_CONTROL_E2P_EECTRL 0x30
73 #define RV3029_TRICKLE_1K (1<<0) /* 1K resistance */
74 #define RV3029_TRICKLE_5K (1<<1) /* 5K resistance */
75 #define RV3029_TRICKLE_20K (1<<2) /* 20K resistance */
76 #define RV3029_TRICKLE_80K (1<<3) /* 80K resistance */
77 #define RV3029_CONTROL_E2P_XTALOFFSET 0x31
78 #define RV3029_CONTROL_E2P_QCOEF 0x32
79 #define RV3029_CONTROL_E2P_TURNOVER 0x33
80
81 /* user ram section */
82 #define RV3029_USR1_RAM_PAGE 0x38
83 #define RV3029_USR1_SECTION_LEN 0x04
84 #define RV3029_USR2_RAM_PAGE 0x3C
85 #define RV3029_USR2_SECTION_LEN 0x04
86
87 static int
88 rv3029_i2c_read_regs(struct i2c_client *client, u8 reg, u8 *buf,
89 unsigned len)
90 {
91 int ret;
92
93 if ((reg > RV3029_USR1_RAM_PAGE + 7) ||
94 (reg + len > RV3029_USR1_RAM_PAGE + 8))
95 return -EINVAL;
96
97 ret = i2c_smbus_read_i2c_block_data(client, reg, len, buf);
98 if (ret < 0)
99 return ret;
100 if (ret < len)
101 return -EIO;
102 return 0;
103 }
104
105 static int
106 rv3029_i2c_write_regs(struct i2c_client *client, u8 reg, u8 const buf[],
107 unsigned len)
108 {
109 if ((reg > RV3029_USR1_RAM_PAGE + 7) ||
110 (reg + len > RV3029_USR1_RAM_PAGE + 8))
111 return -EINVAL;
112
113 return i2c_smbus_write_i2c_block_data(client, reg, len, buf);
114 }
115
116 static int
117 rv3029_i2c_get_sr(struct i2c_client *client, u8 *buf)
118 {
119 int ret = rv3029_i2c_read_regs(client, RV3029_STATUS, buf, 1);
120
121 if (ret < 0)
122 return -EIO;
123 dev_dbg(&client->dev, "status = 0x%.2x (%d)\n", buf[0], buf[0]);
124 return 0;
125 }
126
127 static int
128 rv3029_i2c_set_sr(struct i2c_client *client, u8 val)
129 {
130 u8 buf[1];
131 int sr;
132
133 buf[0] = val;
134 sr = rv3029_i2c_write_regs(client, RV3029_STATUS, buf, 1);
135 dev_dbg(&client->dev, "status = 0x%.2x (%d)\n", buf[0], buf[0]);
136 if (sr < 0)
137 return -EIO;
138 return 0;
139 }
140
141 static int
142 rv3029_i2c_read_time(struct i2c_client *client, struct rtc_time *tm)
143 {
144 u8 buf[1];
145 int ret;
146 u8 regs[RV3029_WATCH_SECTION_LEN] = { 0, };
147
148 ret = rv3029_i2c_get_sr(client, buf);
149 if (ret < 0) {
150 dev_err(&client->dev, "%s: reading SR failed\n", __func__);
151 return -EIO;
152 }
153
154 ret = rv3029_i2c_read_regs(client, RV3029_W_SEC, regs,
155 RV3029_WATCH_SECTION_LEN);
156 if (ret < 0) {
157 dev_err(&client->dev, "%s: reading RTC section failed\n",
158 __func__);
159 return ret;
160 }
161
162 tm->tm_sec = bcd2bin(regs[RV3029_W_SEC-RV3029_W_SEC]);
163 tm->tm_min = bcd2bin(regs[RV3029_W_MINUTES-RV3029_W_SEC]);
164
165 /* HR field has a more complex interpretation */
166 {
167 const u8 _hr = regs[RV3029_W_HOURS-RV3029_W_SEC];
168
169 if (_hr & RV3029_REG_HR_12_24) {
170 /* 12h format */
171 tm->tm_hour = bcd2bin(_hr & 0x1f);
172 if (_hr & RV3029_REG_HR_PM) /* PM flag set */
173 tm->tm_hour += 12;
174 } else /* 24h format */
175 tm->tm_hour = bcd2bin(_hr & 0x3f);
176 }
177
178 tm->tm_mday = bcd2bin(regs[RV3029_W_DATE-RV3029_W_SEC]);
179 tm->tm_mon = bcd2bin(regs[RV3029_W_MONTHS-RV3029_W_SEC]) - 1;
180 tm->tm_year = bcd2bin(regs[RV3029_W_YEARS-RV3029_W_SEC]) + 100;
181 tm->tm_wday = bcd2bin(regs[RV3029_W_DAYS-RV3029_W_SEC]) - 1;
182
183 return 0;
184 }
185
186 static int rv3029_rtc_read_time(struct device *dev, struct rtc_time *tm)
187 {
188 return rv3029_i2c_read_time(to_i2c_client(dev), tm);
189 }
190
191 static int
192 rv3029_i2c_read_alarm(struct i2c_client *client, struct rtc_wkalrm *alarm)
193 {
194 struct rtc_time *const tm = &alarm->time;
195 int ret;
196 u8 regs[8];
197
198 ret = rv3029_i2c_get_sr(client, regs);
199 if (ret < 0) {
200 dev_err(&client->dev, "%s: reading SR failed\n", __func__);
201 return -EIO;
202 }
203
204 ret = rv3029_i2c_read_regs(client, RV3029_A_SC, regs,
205 RV3029_ALARM_SECTION_LEN);
206
207 if (ret < 0) {
208 dev_err(&client->dev, "%s: reading alarm section failed\n",
209 __func__);
210 return ret;
211 }
212
213 tm->tm_sec = bcd2bin(regs[RV3029_A_SC-RV3029_A_SC] & 0x7f);
214 tm->tm_min = bcd2bin(regs[RV3029_A_MN-RV3029_A_SC] & 0x7f);
215 tm->tm_hour = bcd2bin(regs[RV3029_A_HR-RV3029_A_SC] & 0x3f);
216 tm->tm_mday = bcd2bin(regs[RV3029_A_DT-RV3029_A_SC] & 0x3f);
217 tm->tm_mon = bcd2bin(regs[RV3029_A_MO-RV3029_A_SC] & 0x1f) - 1;
218 tm->tm_year = bcd2bin(regs[RV3029_A_YR-RV3029_A_SC] & 0x7f) + 100;
219 tm->tm_wday = bcd2bin(regs[RV3029_A_DW-RV3029_A_SC] & 0x07) - 1;
220
221 return 0;
222 }
223
224 static int
225 rv3029_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
226 {
227 return rv3029_i2c_read_alarm(to_i2c_client(dev), alarm);
228 }
229
230 static int rv3029_rtc_i2c_alarm_set_irq(struct i2c_client *client,
231 int enable)
232 {
233 int ret;
234 u8 buf[1];
235
236 /* enable AIE irq */
237 ret = rv3029_i2c_read_regs(client, RV3029_IRQ_CTRL, buf, 1);
238 if (ret < 0) {
239 dev_err(&client->dev, "can't read INT reg\n");
240 return ret;
241 }
242 if (enable)
243 buf[0] |= RV3029_IRQ_CTRL_AIE;
244 else
245 buf[0] &= ~RV3029_IRQ_CTRL_AIE;
246
247 ret = rv3029_i2c_write_regs(client, RV3029_IRQ_CTRL, buf, 1);
248 if (ret < 0) {
249 dev_err(&client->dev, "can't set INT reg\n");
250 return ret;
251 }
252
253 return 0;
254 }
255
256 static int rv3029_rtc_i2c_set_alarm(struct i2c_client *client,
257 struct rtc_wkalrm *alarm)
258 {
259 struct rtc_time *const tm = &alarm->time;
260 int ret;
261 u8 regs[8];
262
263 /*
264 * The clock has an 8 bit wide bcd-coded register (they never learn)
265 * for the year. tm_year is an offset from 1900 and we are interested
266 * in the 2000-2099 range, so any value less than 100 is invalid.
267 */
268 if (tm->tm_year < 100)
269 return -EINVAL;
270
271 ret = rv3029_i2c_get_sr(client, regs);
272 if (ret < 0) {
273 dev_err(&client->dev, "%s: reading SR failed\n", __func__);
274 return -EIO;
275 }
276 regs[RV3029_A_SC-RV3029_A_SC] = bin2bcd(tm->tm_sec & 0x7f);
277 regs[RV3029_A_MN-RV3029_A_SC] = bin2bcd(tm->tm_min & 0x7f);
278 regs[RV3029_A_HR-RV3029_A_SC] = bin2bcd(tm->tm_hour & 0x3f);
279 regs[RV3029_A_DT-RV3029_A_SC] = bin2bcd(tm->tm_mday & 0x3f);
280 regs[RV3029_A_MO-RV3029_A_SC] = bin2bcd((tm->tm_mon & 0x1f) - 1);
281 regs[RV3029_A_DW-RV3029_A_SC] = bin2bcd((tm->tm_wday & 7) - 1);
282 regs[RV3029_A_YR-RV3029_A_SC] = bin2bcd((tm->tm_year & 0x7f) - 100);
283
284 ret = rv3029_i2c_write_regs(client, RV3029_A_SC, regs,
285 RV3029_ALARM_SECTION_LEN);
286 if (ret < 0)
287 return ret;
288
289 if (alarm->enabled) {
290 u8 buf[1];
291
292 /* clear AF flag */
293 ret = rv3029_i2c_read_regs(client, RV3029_IRQ_FLAGS,
294 buf, 1);
295 if (ret < 0) {
296 dev_err(&client->dev, "can't read alarm flag\n");
297 return ret;
298 }
299 buf[0] &= ~RV3029_IRQ_FLAGS_AF;
300 ret = rv3029_i2c_write_regs(client, RV3029_IRQ_FLAGS,
301 buf, 1);
302 if (ret < 0) {
303 dev_err(&client->dev, "can't set alarm flag\n");
304 return ret;
305 }
306 /* enable AIE irq */
307 ret = rv3029_rtc_i2c_alarm_set_irq(client, 1);
308 if (ret)
309 return ret;
310
311 dev_dbg(&client->dev, "alarm IRQ armed\n");
312 } else {
313 /* disable AIE irq */
314 ret = rv3029_rtc_i2c_alarm_set_irq(client, 0);
315 if (ret)
316 return ret;
317
318 dev_dbg(&client->dev, "alarm IRQ disabled\n");
319 }
320
321 return 0;
322 }
323
324 static int rv3029_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
325 {
326 return rv3029_rtc_i2c_set_alarm(to_i2c_client(dev), alarm);
327 }
328
329 static int
330 rv3029_i2c_set_time(struct i2c_client *client, struct rtc_time const *tm)
331 {
332 u8 regs[8];
333 int ret;
334
335 /*
336 * The clock has an 8 bit wide bcd-coded register (they never learn)
337 * for the year. tm_year is an offset from 1900 and we are interested
338 * in the 2000-2099 range, so any value less than 100 is invalid.
339 */
340 if (tm->tm_year < 100)
341 return -EINVAL;
342
343 regs[RV3029_W_SEC-RV3029_W_SEC] = bin2bcd(tm->tm_sec);
344 regs[RV3029_W_MINUTES-RV3029_W_SEC] = bin2bcd(tm->tm_min);
345 regs[RV3029_W_HOURS-RV3029_W_SEC] = bin2bcd(tm->tm_hour);
346 regs[RV3029_W_DATE-RV3029_W_SEC] = bin2bcd(tm->tm_mday);
347 regs[RV3029_W_MONTHS-RV3029_W_SEC] = bin2bcd(tm->tm_mon+1);
348 regs[RV3029_W_DAYS-RV3029_W_SEC] = bin2bcd((tm->tm_wday & 7)+1);
349 regs[RV3029_W_YEARS-RV3029_W_SEC] = bin2bcd(tm->tm_year - 100);
350
351 ret = rv3029_i2c_write_regs(client, RV3029_W_SEC, regs,
352 RV3029_WATCH_SECTION_LEN);
353 if (ret < 0)
354 return ret;
355
356 ret = rv3029_i2c_get_sr(client, regs);
357 if (ret < 0) {
358 dev_err(&client->dev, "%s: reading SR failed\n", __func__);
359 return ret;
360 }
361 /* clear PON bit */
362 ret = rv3029_i2c_set_sr(client, (regs[0] & ~RV3029_STATUS_PON));
363 if (ret < 0) {
364 dev_err(&client->dev, "%s: reading SR failed\n", __func__);
365 return ret;
366 }
367
368 return 0;
369 }
370
371 static int rv3029_rtc_set_time(struct device *dev, struct rtc_time *tm)
372 {
373 return rv3029_i2c_set_time(to_i2c_client(dev), tm);
374 }
375
376 static const struct rtc_class_ops rv3029_rtc_ops = {
377 .read_time = rv3029_rtc_read_time,
378 .set_time = rv3029_rtc_set_time,
379 .read_alarm = rv3029_rtc_read_alarm,
380 .set_alarm = rv3029_rtc_set_alarm,
381 };
382
383 static struct i2c_device_id rv3029_id[] = {
384 { "rv3029", 0 },
385 { "rv3029c2", 0 },
386 { }
387 };
388 MODULE_DEVICE_TABLE(i2c, rv3029_id);
389
390 static int rv3029_probe(struct i2c_client *client,
391 const struct i2c_device_id *id)
392 {
393 struct rtc_device *rtc;
394 int rc = 0;
395 u8 buf[1];
396
397 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_EMUL))
398 return -ENODEV;
399
400 rc = rv3029_i2c_get_sr(client, buf);
401 if (rc < 0) {
402 dev_err(&client->dev, "reading status failed\n");
403 return rc;
404 }
405
406 rtc = devm_rtc_device_register(&client->dev, client->name,
407 &rv3029_rtc_ops, THIS_MODULE);
408
409 if (IS_ERR(rtc))
410 return PTR_ERR(rtc);
411
412 i2c_set_clientdata(client, rtc);
413
414 return 0;
415 }
416
417 static struct i2c_driver rv3029_driver = {
418 .driver = {
419 .name = "rtc-rv3029c2",
420 },
421 .probe = rv3029_probe,
422 .id_table = rv3029_id,
423 };
424
425 module_i2c_driver(rv3029_driver);
426
427 MODULE_AUTHOR("Gregory Hermant <gregory.hermant@calao-systems.com>");
428 MODULE_DESCRIPTION("Micro Crystal RV3029 RTC driver");
429 MODULE_LICENSE("GPL");
This page took 0.038737 seconds and 6 git commands to generate.