of: MSI: Simplify irqdomain lookup
[linux/fpc-iii.git] / drivers / rtc / rtc-rv3029c2.c
blobe9ac5a43be1a87c2f84f98e3e8d11f0cd922b7bd
1 /*
2 * Micro Crystal RV-3029C2 rtc class driver
4 * Author: Gregory Hermant <gregory.hermant@calao-systems.com>
6 * based on previously existing rtc class drivers
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.
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.
17 #include <linux/module.h>
18 #include <linux/i2c.h>
19 #include <linux/bcd.h>
20 #include <linux/rtc.h>
22 /* Register map */
23 /* control section */
24 #define RV3029C2_ONOFF_CTRL 0x00
25 #define RV3029C2_IRQ_CTRL 0x01
26 #define RV3029C2_IRQ_CTRL_AIE (1 << 0)
27 #define RV3029C2_IRQ_FLAGS 0x02
28 #define RV3029C2_IRQ_FLAGS_AF (1 << 0)
29 #define RV3029C2_STATUS 0x03
30 #define RV3029C2_STATUS_VLOW1 (1 << 2)
31 #define RV3029C2_STATUS_VLOW2 (1 << 3)
32 #define RV3029C2_STATUS_SR (1 << 4)
33 #define RV3029C2_STATUS_PON (1 << 5)
34 #define RV3029C2_STATUS_EEBUSY (1 << 7)
35 #define RV3029C2_RST_CTRL 0x04
36 #define RV3029C2_CONTROL_SECTION_LEN 0x05
38 /* watch section */
39 #define RV3029C2_W_SEC 0x08
40 #define RV3029C2_W_MINUTES 0x09
41 #define RV3029C2_W_HOURS 0x0A
42 #define RV3029C2_REG_HR_12_24 (1<<6) /* 24h/12h mode */
43 #define RV3029C2_REG_HR_PM (1<<5) /* PM/AM bit in 12h mode */
44 #define RV3029C2_W_DATE 0x0B
45 #define RV3029C2_W_DAYS 0x0C
46 #define RV3029C2_W_MONTHS 0x0D
47 #define RV3029C2_W_YEARS 0x0E
48 #define RV3029C2_WATCH_SECTION_LEN 0x07
50 /* alarm section */
51 #define RV3029C2_A_SC 0x10
52 #define RV3029C2_A_MN 0x11
53 #define RV3029C2_A_HR 0x12
54 #define RV3029C2_A_DT 0x13
55 #define RV3029C2_A_DW 0x14
56 #define RV3029C2_A_MO 0x15
57 #define RV3029C2_A_YR 0x16
58 #define RV3029C2_ALARM_SECTION_LEN 0x07
60 /* timer section */
61 #define RV3029C2_TIMER_LOW 0x18
62 #define RV3029C2_TIMER_HIGH 0x19
64 /* temperature section */
65 #define RV3029C2_TEMP_PAGE 0x20
67 /* eeprom data section */
68 #define RV3029C2_E2P_EEDATA1 0x28
69 #define RV3029C2_E2P_EEDATA2 0x29
71 /* eeprom control section */
72 #define RV3029C2_CONTROL_E2P_EECTRL 0x30
73 #define RV3029C2_TRICKLE_1K (1<<0) /* 1K resistance */
74 #define RV3029C2_TRICKLE_5K (1<<1) /* 5K resistance */
75 #define RV3029C2_TRICKLE_20K (1<<2) /* 20K resistance */
76 #define RV3029C2_TRICKLE_80K (1<<3) /* 80K resistance */
77 #define RV3029C2_CONTROL_E2P_XTALOFFSET 0x31
78 #define RV3029C2_CONTROL_E2P_QCOEF 0x32
79 #define RV3029C2_CONTROL_E2P_TURNOVER 0x33
81 /* user ram section */
82 #define RV3029C2_USR1_RAM_PAGE 0x38
83 #define RV3029C2_USR1_SECTION_LEN 0x04
84 #define RV3029C2_USR2_RAM_PAGE 0x3C
85 #define RV3029C2_USR2_SECTION_LEN 0x04
87 static int
88 rv3029c2_i2c_read_regs(struct i2c_client *client, u8 reg, u8 *buf,
89 unsigned len)
91 int ret;
93 if ((reg > RV3029C2_USR1_RAM_PAGE + 7) ||
94 (reg + len > RV3029C2_USR1_RAM_PAGE + 8))
95 return -EINVAL;
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;
105 static int
106 rv3029c2_i2c_write_regs(struct i2c_client *client, u8 reg, u8 const buf[],
107 unsigned len)
109 if ((reg > RV3029C2_USR1_RAM_PAGE + 7) ||
110 (reg + len > RV3029C2_USR1_RAM_PAGE + 8))
111 return -EINVAL;
113 return i2c_smbus_write_i2c_block_data(client, reg, len, buf);
116 static int
117 rv3029c2_i2c_get_sr(struct i2c_client *client, u8 *buf)
119 int ret = rv3029c2_i2c_read_regs(client, RV3029C2_STATUS, buf, 1);
121 if (ret < 0)
122 return -EIO;
123 dev_dbg(&client->dev, "status = 0x%.2x (%d)\n", buf[0], buf[0]);
124 return 0;
127 static int
128 rv3029c2_i2c_set_sr(struct i2c_client *client, u8 val)
130 u8 buf[1];
131 int sr;
133 buf[0] = val;
134 sr = rv3029c2_i2c_write_regs(client, RV3029C2_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;
141 static int
142 rv3029c2_i2c_read_time(struct i2c_client *client, struct rtc_time *tm)
144 u8 buf[1];
145 int ret;
146 u8 regs[RV3029C2_WATCH_SECTION_LEN] = { 0, };
148 ret = rv3029c2_i2c_get_sr(client, buf);
149 if (ret < 0) {
150 dev_err(&client->dev, "%s: reading SR failed\n", __func__);
151 return -EIO;
154 ret = rv3029c2_i2c_read_regs(client, RV3029C2_W_SEC , regs,
155 RV3029C2_WATCH_SECTION_LEN);
156 if (ret < 0) {
157 dev_err(&client->dev, "%s: reading RTC section failed\n",
158 __func__);
159 return ret;
162 tm->tm_sec = bcd2bin(regs[RV3029C2_W_SEC-RV3029C2_W_SEC]);
163 tm->tm_min = bcd2bin(regs[RV3029C2_W_MINUTES-RV3029C2_W_SEC]);
165 /* HR field has a more complex interpretation */
167 const u8 _hr = regs[RV3029C2_W_HOURS-RV3029C2_W_SEC];
168 if (_hr & RV3029C2_REG_HR_12_24) {
169 /* 12h format */
170 tm->tm_hour = bcd2bin(_hr & 0x1f);
171 if (_hr & RV3029C2_REG_HR_PM) /* PM flag set */
172 tm->tm_hour += 12;
173 } else /* 24h format */
174 tm->tm_hour = bcd2bin(_hr & 0x3f);
177 tm->tm_mday = bcd2bin(regs[RV3029C2_W_DATE-RV3029C2_W_SEC]);
178 tm->tm_mon = bcd2bin(regs[RV3029C2_W_MONTHS-RV3029C2_W_SEC]) - 1;
179 tm->tm_year = bcd2bin(regs[RV3029C2_W_YEARS-RV3029C2_W_SEC]) + 100;
180 tm->tm_wday = bcd2bin(regs[RV3029C2_W_DAYS-RV3029C2_W_SEC]) - 1;
182 return 0;
185 static int rv3029c2_rtc_read_time(struct device *dev, struct rtc_time *tm)
187 return rv3029c2_i2c_read_time(to_i2c_client(dev), tm);
190 static int
191 rv3029c2_i2c_read_alarm(struct i2c_client *client, struct rtc_wkalrm *alarm)
193 struct rtc_time *const tm = &alarm->time;
194 int ret;
195 u8 regs[8];
197 ret = rv3029c2_i2c_get_sr(client, regs);
198 if (ret < 0) {
199 dev_err(&client->dev, "%s: reading SR failed\n", __func__);
200 return -EIO;
203 ret = rv3029c2_i2c_read_regs(client, RV3029C2_A_SC, regs,
204 RV3029C2_ALARM_SECTION_LEN);
206 if (ret < 0) {
207 dev_err(&client->dev, "%s: reading alarm section failed\n",
208 __func__);
209 return ret;
212 tm->tm_sec = bcd2bin(regs[RV3029C2_A_SC-RV3029C2_A_SC] & 0x7f);
213 tm->tm_min = bcd2bin(regs[RV3029C2_A_MN-RV3029C2_A_SC] & 0x7f);
214 tm->tm_hour = bcd2bin(regs[RV3029C2_A_HR-RV3029C2_A_SC] & 0x3f);
215 tm->tm_mday = bcd2bin(regs[RV3029C2_A_DT-RV3029C2_A_SC] & 0x3f);
216 tm->tm_mon = bcd2bin(regs[RV3029C2_A_MO-RV3029C2_A_SC] & 0x1f) - 1;
217 tm->tm_year = bcd2bin(regs[RV3029C2_A_YR-RV3029C2_A_SC] & 0x7f) + 100;
218 tm->tm_wday = bcd2bin(regs[RV3029C2_A_DW-RV3029C2_A_SC] & 0x07) - 1;
220 return 0;
223 static int
224 rv3029c2_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
226 return rv3029c2_i2c_read_alarm(to_i2c_client(dev), alarm);
229 static int rv3029c2_rtc_i2c_alarm_set_irq(struct i2c_client *client,
230 int enable)
232 int ret;
233 u8 buf[1];
235 /* enable AIE irq */
236 ret = rv3029c2_i2c_read_regs(client, RV3029C2_IRQ_CTRL, buf, 1);
237 if (ret < 0) {
238 dev_err(&client->dev, "can't read INT reg\n");
239 return ret;
241 if (enable)
242 buf[0] |= RV3029C2_IRQ_CTRL_AIE;
243 else
244 buf[0] &= ~RV3029C2_IRQ_CTRL_AIE;
246 ret = rv3029c2_i2c_write_regs(client, RV3029C2_IRQ_CTRL, buf, 1);
247 if (ret < 0) {
248 dev_err(&client->dev, "can't set INT reg\n");
249 return ret;
252 return 0;
255 static int rv3029c2_rtc_i2c_set_alarm(struct i2c_client *client,
256 struct rtc_wkalrm *alarm)
258 struct rtc_time *const tm = &alarm->time;
259 int ret;
260 u8 regs[8];
263 * The clock has an 8 bit wide bcd-coded register (they never learn)
264 * for the year. tm_year is an offset from 1900 and we are interested
265 * in the 2000-2099 range, so any value less than 100 is invalid.
267 if (tm->tm_year < 100)
268 return -EINVAL;
270 ret = rv3029c2_i2c_get_sr(client, regs);
271 if (ret < 0) {
272 dev_err(&client->dev, "%s: reading SR failed\n", __func__);
273 return -EIO;
275 regs[RV3029C2_A_SC-RV3029C2_A_SC] = bin2bcd(tm->tm_sec & 0x7f);
276 regs[RV3029C2_A_MN-RV3029C2_A_SC] = bin2bcd(tm->tm_min & 0x7f);
277 regs[RV3029C2_A_HR-RV3029C2_A_SC] = bin2bcd(tm->tm_hour & 0x3f);
278 regs[RV3029C2_A_DT-RV3029C2_A_SC] = bin2bcd(tm->tm_mday & 0x3f);
279 regs[RV3029C2_A_MO-RV3029C2_A_SC] = bin2bcd((tm->tm_mon & 0x1f) - 1);
280 regs[RV3029C2_A_DW-RV3029C2_A_SC] = bin2bcd((tm->tm_wday & 7) - 1);
281 regs[RV3029C2_A_YR-RV3029C2_A_SC] = bin2bcd((tm->tm_year & 0x7f) - 100);
283 ret = rv3029c2_i2c_write_regs(client, RV3029C2_A_SC, regs,
284 RV3029C2_ALARM_SECTION_LEN);
285 if (ret < 0)
286 return ret;
288 if (alarm->enabled) {
289 u8 buf[1];
291 /* clear AF flag */
292 ret = rv3029c2_i2c_read_regs(client, RV3029C2_IRQ_FLAGS,
293 buf, 1);
294 if (ret < 0) {
295 dev_err(&client->dev, "can't read alarm flag\n");
296 return ret;
298 buf[0] &= ~RV3029C2_IRQ_FLAGS_AF;
299 ret = rv3029c2_i2c_write_regs(client, RV3029C2_IRQ_FLAGS,
300 buf, 1);
301 if (ret < 0) {
302 dev_err(&client->dev, "can't set alarm flag\n");
303 return ret;
305 /* enable AIE irq */
306 ret = rv3029c2_rtc_i2c_alarm_set_irq(client, 1);
307 if (ret)
308 return ret;
310 dev_dbg(&client->dev, "alarm IRQ armed\n");
311 } else {
312 /* disable AIE irq */
313 ret = rv3029c2_rtc_i2c_alarm_set_irq(client, 0);
314 if (ret)
315 return ret;
317 dev_dbg(&client->dev, "alarm IRQ disabled\n");
320 return 0;
323 static int rv3029c2_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
325 return rv3029c2_rtc_i2c_set_alarm(to_i2c_client(dev), alarm);
328 static int
329 rv3029c2_i2c_set_time(struct i2c_client *client, struct rtc_time const *tm)
331 u8 regs[8];
332 int ret;
335 * The clock has an 8 bit wide bcd-coded register (they never learn)
336 * for the year. tm_year is an offset from 1900 and we are interested
337 * in the 2000-2099 range, so any value less than 100 is invalid.
339 if (tm->tm_year < 100)
340 return -EINVAL;
342 regs[RV3029C2_W_SEC-RV3029C2_W_SEC] = bin2bcd(tm->tm_sec);
343 regs[RV3029C2_W_MINUTES-RV3029C2_W_SEC] = bin2bcd(tm->tm_min);
344 regs[RV3029C2_W_HOURS-RV3029C2_W_SEC] = bin2bcd(tm->tm_hour);
345 regs[RV3029C2_W_DATE-RV3029C2_W_SEC] = bin2bcd(tm->tm_mday);
346 regs[RV3029C2_W_MONTHS-RV3029C2_W_SEC] = bin2bcd(tm->tm_mon+1);
347 regs[RV3029C2_W_DAYS-RV3029C2_W_SEC] = bin2bcd((tm->tm_wday & 7)+1);
348 regs[RV3029C2_W_YEARS-RV3029C2_W_SEC] = bin2bcd(tm->tm_year - 100);
350 ret = rv3029c2_i2c_write_regs(client, RV3029C2_W_SEC, regs,
351 RV3029C2_WATCH_SECTION_LEN);
352 if (ret < 0)
353 return ret;
355 ret = rv3029c2_i2c_get_sr(client, regs);
356 if (ret < 0) {
357 dev_err(&client->dev, "%s: reading SR failed\n", __func__);
358 return ret;
360 /* clear PON bit */
361 ret = rv3029c2_i2c_set_sr(client, (regs[0] & ~RV3029C2_STATUS_PON));
362 if (ret < 0) {
363 dev_err(&client->dev, "%s: reading SR failed\n", __func__);
364 return ret;
367 return 0;
370 static int rv3029c2_rtc_set_time(struct device *dev, struct rtc_time *tm)
372 return rv3029c2_i2c_set_time(to_i2c_client(dev), tm);
375 static const struct rtc_class_ops rv3029c2_rtc_ops = {
376 .read_time = rv3029c2_rtc_read_time,
377 .set_time = rv3029c2_rtc_set_time,
378 .read_alarm = rv3029c2_rtc_read_alarm,
379 .set_alarm = rv3029c2_rtc_set_alarm,
382 static struct i2c_device_id rv3029c2_id[] = {
383 { "rv3029c2", 0 },
386 MODULE_DEVICE_TABLE(i2c, rv3029c2_id);
388 static int rv3029c2_probe(struct i2c_client *client,
389 const struct i2c_device_id *id)
391 struct rtc_device *rtc;
392 int rc = 0;
393 u8 buf[1];
395 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_EMUL))
396 return -ENODEV;
398 rc = rv3029c2_i2c_get_sr(client, buf);
399 if (rc < 0) {
400 dev_err(&client->dev, "reading status failed\n");
401 return rc;
404 rtc = devm_rtc_device_register(&client->dev, client->name,
405 &rv3029c2_rtc_ops, THIS_MODULE);
407 if (IS_ERR(rtc))
408 return PTR_ERR(rtc);
410 i2c_set_clientdata(client, rtc);
412 return 0;
415 static struct i2c_driver rv3029c2_driver = {
416 .driver = {
417 .name = "rtc-rv3029c2",
419 .probe = rv3029c2_probe,
420 .id_table = rv3029c2_id,
423 module_i2c_driver(rv3029c2_driver);
425 MODULE_AUTHOR("Gregory Hermant <gregory.hermant@calao-systems.com>");
426 MODULE_DESCRIPTION("Micro Crystal RV3029C2 RTC driver");
427 MODULE_LICENSE("GPL");