thermal: armada: Use PTR_ERR_OR_ZERO in armada_thermal_probe_legacy()
[sfrench/cifs-2.6.git] / drivers / rtc / rtc-isl1208.c
1 /*
2  * Intersil ISL1208 rtc class driver
3  *
4  * Copyright 2005,2006 Hebert Valerio Riedel <hvr@gnu.org>
5  *
6  *  This program is free software; you can redistribute  it and/or modify it
7  *  under  the terms of  the GNU General  Public License as published by the
8  *  Free Software Foundation;  either version 2 of the  License, or (at your
9  *  option) any later version.
10  *
11  */
12
13 #include <linux/bcd.h>
14 #include <linux/i2c.h>
15 #include <linux/module.h>
16 #include <linux/of_irq.h>
17 #include <linux/rtc.h>
18
19 /* Register map */
20 /* rtc section */
21 #define ISL1208_REG_SC  0x00
22 #define ISL1208_REG_MN  0x01
23 #define ISL1208_REG_HR  0x02
24 #define ISL1208_REG_HR_MIL     (1<<7)   /* 24h/12h mode */
25 #define ISL1208_REG_HR_PM      (1<<5)   /* PM/AM bit in 12h mode */
26 #define ISL1208_REG_DT  0x03
27 #define ISL1208_REG_MO  0x04
28 #define ISL1208_REG_YR  0x05
29 #define ISL1208_REG_DW  0x06
30 #define ISL1208_RTC_SECTION_LEN 7
31
32 /* control/status section */
33 #define ISL1208_REG_SR  0x07
34 #define ISL1208_REG_SR_ARST    (1<<7)   /* auto reset */
35 #define ISL1208_REG_SR_XTOSCB  (1<<6)   /* crystal oscillator */
36 #define ISL1208_REG_SR_WRTC    (1<<4)   /* write rtc */
37 #define ISL1208_REG_SR_EVT     (1<<3)   /* event */
38 #define ISL1208_REG_SR_ALM     (1<<2)   /* alarm */
39 #define ISL1208_REG_SR_BAT     (1<<1)   /* battery */
40 #define ISL1208_REG_SR_RTCF    (1<<0)   /* rtc fail */
41 #define ISL1208_REG_INT 0x08
42 #define ISL1208_REG_INT_ALME   (1<<6)   /* alarm enable */
43 #define ISL1208_REG_INT_IM     (1<<7)   /* interrupt/alarm mode */
44 #define ISL1219_REG_EV  0x09
45 #define ISL1219_REG_EV_EVEN    (1<<4)   /* event detection enable */
46 #define ISL1219_REG_EV_EVIENB  (1<<7)   /* event in pull-up disable */
47 #define ISL1208_REG_ATR 0x0a
48 #define ISL1208_REG_DTR 0x0b
49
50 /* alarm section */
51 #define ISL1208_REG_SCA 0x0c
52 #define ISL1208_REG_MNA 0x0d
53 #define ISL1208_REG_HRA 0x0e
54 #define ISL1208_REG_DTA 0x0f
55 #define ISL1208_REG_MOA 0x10
56 #define ISL1208_REG_DWA 0x11
57 #define ISL1208_ALARM_SECTION_LEN 6
58
59 /* user section */
60 #define ISL1208_REG_USR1 0x12
61 #define ISL1208_REG_USR2 0x13
62 #define ISL1208_USR_SECTION_LEN 2
63
64 /* event section */
65 #define ISL1219_REG_SCT 0x14
66 #define ISL1219_REG_MNT 0x15
67 #define ISL1219_REG_HRT 0x16
68 #define ISL1219_REG_DTT 0x17
69 #define ISL1219_REG_MOT 0x18
70 #define ISL1219_REG_YRT 0x19
71 #define ISL1219_EVT_SECTION_LEN 6
72
73 static struct i2c_driver isl1208_driver;
74
75 /* ISL1208 various variants */
76 enum {
77         TYPE_ISL1208 = 0,
78         TYPE_ISL1218,
79         TYPE_ISL1219,
80 };
81
82 /* block read */
83 static int
84 isl1208_i2c_read_regs(struct i2c_client *client, u8 reg, u8 buf[],
85                       unsigned len)
86 {
87         u8 reg_addr[1] = { reg };
88         struct i2c_msg msgs[2] = {
89                 {
90                         .addr = client->addr,
91                         .len = sizeof(reg_addr),
92                         .buf = reg_addr
93                 },
94                 {
95                         .addr = client->addr,
96                         .flags = I2C_M_RD,
97                         .len = len,
98                         .buf = buf
99                 }
100         };
101         int ret;
102
103         WARN_ON(reg > ISL1219_REG_YRT);
104         WARN_ON(reg + len > ISL1219_REG_YRT + 1);
105
106         ret = i2c_transfer(client->adapter, msgs, 2);
107         if (ret > 0)
108                 ret = 0;
109         return ret;
110 }
111
112 /* block write */
113 static int
114 isl1208_i2c_set_regs(struct i2c_client *client, u8 reg, u8 const buf[],
115                      unsigned len)
116 {
117         u8 i2c_buf[ISL1208_REG_USR2 + 2];
118         struct i2c_msg msgs[1] = {
119                 {
120                         .addr = client->addr,
121                         .len = len + 1,
122                         .buf = i2c_buf
123                 }
124         };
125         int ret;
126
127         WARN_ON(reg > ISL1219_REG_YRT);
128         WARN_ON(reg + len > ISL1219_REG_YRT + 1);
129
130         i2c_buf[0] = reg;
131         memcpy(&i2c_buf[1], &buf[0], len);
132
133         ret = i2c_transfer(client->adapter, msgs, 1);
134         if (ret > 0)
135                 ret = 0;
136         return ret;
137 }
138
139 /* simple check to see whether we have a isl1208 */
140 static int
141 isl1208_i2c_validate_client(struct i2c_client *client)
142 {
143         u8 regs[ISL1208_RTC_SECTION_LEN] = { 0, };
144         u8 zero_mask[ISL1208_RTC_SECTION_LEN] = {
145                 0x80, 0x80, 0x40, 0xc0, 0xe0, 0x00, 0xf8
146         };
147         int i;
148         int ret;
149
150         ret = isl1208_i2c_read_regs(client, 0, regs, ISL1208_RTC_SECTION_LEN);
151         if (ret < 0)
152                 return ret;
153
154         for (i = 0; i < ISL1208_RTC_SECTION_LEN; ++i) {
155                 if (regs[i] & zero_mask[i])     /* check if bits are cleared */
156                         return -ENODEV;
157         }
158
159         return 0;
160 }
161
162 static int
163 isl1208_i2c_get_sr(struct i2c_client *client)
164 {
165         return i2c_smbus_read_byte_data(client, ISL1208_REG_SR);
166 }
167
168 static int
169 isl1208_i2c_get_atr(struct i2c_client *client)
170 {
171         int atr = i2c_smbus_read_byte_data(client, ISL1208_REG_ATR);
172         if (atr < 0)
173                 return atr;
174
175         /* The 6bit value in the ATR register controls the load
176          * capacitance C_load * in steps of 0.25pF
177          *
178          * bit (1<<5) of the ATR register is inverted
179          *
180          * C_load(ATR=0x20) =  4.50pF
181          * C_load(ATR=0x00) = 12.50pF
182          * C_load(ATR=0x1f) = 20.25pF
183          *
184          */
185
186         atr &= 0x3f;            /* mask out lsb */
187         atr ^= 1 << 5;          /* invert 6th bit */
188         atr += 2 * 9;           /* add offset of 4.5pF; unit[atr] = 0.25pF */
189
190         return atr;
191 }
192
193 static int
194 isl1208_i2c_get_dtr(struct i2c_client *client)
195 {
196         int dtr = i2c_smbus_read_byte_data(client, ISL1208_REG_DTR);
197         if (dtr < 0)
198                 return -EIO;
199
200         /* dtr encodes adjustments of {-60,-40,-20,0,20,40,60} ppm */
201         dtr = ((dtr & 0x3) * 20) * (dtr & (1 << 2) ? -1 : 1);
202
203         return dtr;
204 }
205
206 static int
207 isl1208_i2c_get_usr(struct i2c_client *client)
208 {
209         u8 buf[ISL1208_USR_SECTION_LEN] = { 0, };
210         int ret;
211
212         ret = isl1208_i2c_read_regs(client, ISL1208_REG_USR1, buf,
213                                     ISL1208_USR_SECTION_LEN);
214         if (ret < 0)
215                 return ret;
216
217         return (buf[1] << 8) | buf[0];
218 }
219
220 static int
221 isl1208_i2c_set_usr(struct i2c_client *client, u16 usr)
222 {
223         u8 buf[ISL1208_USR_SECTION_LEN];
224
225         buf[0] = usr & 0xff;
226         buf[1] = (usr >> 8) & 0xff;
227
228         return isl1208_i2c_set_regs(client, ISL1208_REG_USR1, buf,
229                                     ISL1208_USR_SECTION_LEN);
230 }
231
232 static int
233 isl1208_rtc_toggle_alarm(struct i2c_client *client, int enable)
234 {
235         int icr = i2c_smbus_read_byte_data(client, ISL1208_REG_INT);
236
237         if (icr < 0) {
238                 dev_err(&client->dev, "%s: reading INT failed\n", __func__);
239                 return icr;
240         }
241
242         if (enable)
243                 icr |= ISL1208_REG_INT_ALME | ISL1208_REG_INT_IM;
244         else
245                 icr &= ~(ISL1208_REG_INT_ALME | ISL1208_REG_INT_IM);
246
247         icr = i2c_smbus_write_byte_data(client, ISL1208_REG_INT, icr);
248         if (icr < 0) {
249                 dev_err(&client->dev, "%s: writing INT failed\n", __func__);
250                 return icr;
251         }
252
253         return 0;
254 }
255
256 static int
257 isl1208_rtc_proc(struct device *dev, struct seq_file *seq)
258 {
259         struct i2c_client *const client = to_i2c_client(dev);
260         int sr, dtr, atr, usr;
261
262         sr = isl1208_i2c_get_sr(client);
263         if (sr < 0) {
264                 dev_err(&client->dev, "%s: reading SR failed\n", __func__);
265                 return sr;
266         }
267
268         seq_printf(seq, "status_reg\t:%s%s%s%s%s%s (0x%.2x)\n",
269                    (sr & ISL1208_REG_SR_RTCF) ? " RTCF" : "",
270                    (sr & ISL1208_REG_SR_BAT) ? " BAT" : "",
271                    (sr & ISL1208_REG_SR_ALM) ? " ALM" : "",
272                    (sr & ISL1208_REG_SR_WRTC) ? " WRTC" : "",
273                    (sr & ISL1208_REG_SR_XTOSCB) ? " XTOSCB" : "",
274                    (sr & ISL1208_REG_SR_ARST) ? " ARST" : "", sr);
275
276         seq_printf(seq, "batt_status\t: %s\n",
277                    (sr & ISL1208_REG_SR_RTCF) ? "bad" : "okay");
278
279         dtr = isl1208_i2c_get_dtr(client);
280         if (dtr >= 0 - 1)
281                 seq_printf(seq, "digital_trim\t: %d ppm\n", dtr);
282
283         atr = isl1208_i2c_get_atr(client);
284         if (atr >= 0)
285                 seq_printf(seq, "analog_trim\t: %d.%.2d pF\n",
286                            atr >> 2, (atr & 0x3) * 25);
287
288         usr = isl1208_i2c_get_usr(client);
289         if (usr >= 0)
290                 seq_printf(seq, "user_data\t: 0x%.4x\n", usr);
291
292         return 0;
293 }
294
295 static int
296 isl1208_i2c_read_time(struct i2c_client *client, struct rtc_time *tm)
297 {
298         int sr;
299         u8 regs[ISL1208_RTC_SECTION_LEN] = { 0, };
300
301         sr = isl1208_i2c_get_sr(client);
302         if (sr < 0) {
303                 dev_err(&client->dev, "%s: reading SR failed\n", __func__);
304                 return -EIO;
305         }
306
307         sr = isl1208_i2c_read_regs(client, 0, regs, ISL1208_RTC_SECTION_LEN);
308         if (sr < 0) {
309                 dev_err(&client->dev, "%s: reading RTC section failed\n",
310                         __func__);
311                 return sr;
312         }
313
314         tm->tm_sec = bcd2bin(regs[ISL1208_REG_SC]);
315         tm->tm_min = bcd2bin(regs[ISL1208_REG_MN]);
316
317         /* HR field has a more complex interpretation */
318         {
319                 const u8 _hr = regs[ISL1208_REG_HR];
320                 if (_hr & ISL1208_REG_HR_MIL)   /* 24h format */
321                         tm->tm_hour = bcd2bin(_hr & 0x3f);
322                 else {
323                         /* 12h format */
324                         tm->tm_hour = bcd2bin(_hr & 0x1f);
325                         if (_hr & ISL1208_REG_HR_PM)    /* PM flag set */
326                                 tm->tm_hour += 12;
327                 }
328         }
329
330         tm->tm_mday = bcd2bin(regs[ISL1208_REG_DT]);
331         tm->tm_mon = bcd2bin(regs[ISL1208_REG_MO]) - 1; /* rtc starts at 1 */
332         tm->tm_year = bcd2bin(regs[ISL1208_REG_YR]) + 100;
333         tm->tm_wday = bcd2bin(regs[ISL1208_REG_DW]);
334
335         return 0;
336 }
337
338 static int
339 isl1208_i2c_read_alarm(struct i2c_client *client, struct rtc_wkalrm *alarm)
340 {
341         struct rtc_time *const tm = &alarm->time;
342         u8 regs[ISL1208_ALARM_SECTION_LEN] = { 0, };
343         int icr, yr, sr = isl1208_i2c_get_sr(client);
344
345         if (sr < 0) {
346                 dev_err(&client->dev, "%s: reading SR failed\n", __func__);
347                 return sr;
348         }
349
350         sr = isl1208_i2c_read_regs(client, ISL1208_REG_SCA, regs,
351                                    ISL1208_ALARM_SECTION_LEN);
352         if (sr < 0) {
353                 dev_err(&client->dev, "%s: reading alarm section failed\n",
354                         __func__);
355                 return sr;
356         }
357
358         /* MSB of each alarm register is an enable bit */
359         tm->tm_sec = bcd2bin(regs[ISL1208_REG_SCA - ISL1208_REG_SCA] & 0x7f);
360         tm->tm_min = bcd2bin(regs[ISL1208_REG_MNA - ISL1208_REG_SCA] & 0x7f);
361         tm->tm_hour = bcd2bin(regs[ISL1208_REG_HRA - ISL1208_REG_SCA] & 0x3f);
362         tm->tm_mday = bcd2bin(regs[ISL1208_REG_DTA - ISL1208_REG_SCA] & 0x3f);
363         tm->tm_mon =
364                 bcd2bin(regs[ISL1208_REG_MOA - ISL1208_REG_SCA] & 0x1f) - 1;
365         tm->tm_wday = bcd2bin(regs[ISL1208_REG_DWA - ISL1208_REG_SCA] & 0x03);
366
367         /* The alarm doesn't store the year so get it from the rtc section */
368         yr = i2c_smbus_read_byte_data(client, ISL1208_REG_YR);
369         if (yr < 0) {
370                 dev_err(&client->dev, "%s: reading RTC YR failed\n", __func__);
371                 return yr;
372         }
373         tm->tm_year = bcd2bin(yr) + 100;
374
375         icr = i2c_smbus_read_byte_data(client, ISL1208_REG_INT);
376         if (icr < 0) {
377                 dev_err(&client->dev, "%s: reading INT failed\n", __func__);
378                 return icr;
379         }
380         alarm->enabled = !!(icr & ISL1208_REG_INT_ALME);
381
382         return 0;
383 }
384
385 static int
386 isl1208_i2c_set_alarm(struct i2c_client *client, struct rtc_wkalrm *alarm)
387 {
388         struct rtc_time *alarm_tm = &alarm->time;
389         u8 regs[ISL1208_ALARM_SECTION_LEN] = { 0, };
390         const int offs = ISL1208_REG_SCA;
391         struct rtc_time rtc_tm;
392         int err, enable;
393
394         err = isl1208_i2c_read_time(client, &rtc_tm);
395         if (err)
396                 return err;
397
398         /* If the alarm time is before the current time disable the alarm */
399         if (!alarm->enabled || rtc_tm_sub(alarm_tm, &rtc_tm) <= 0)
400                 enable = 0x00;
401         else
402                 enable = 0x80;
403
404         /* Program the alarm and enable it for each setting */
405         regs[ISL1208_REG_SCA - offs] = bin2bcd(alarm_tm->tm_sec) | enable;
406         regs[ISL1208_REG_MNA - offs] = bin2bcd(alarm_tm->tm_min) | enable;
407         regs[ISL1208_REG_HRA - offs] = bin2bcd(alarm_tm->tm_hour) |
408                 ISL1208_REG_HR_MIL | enable;
409
410         regs[ISL1208_REG_DTA - offs] = bin2bcd(alarm_tm->tm_mday) | enable;
411         regs[ISL1208_REG_MOA - offs] = bin2bcd(alarm_tm->tm_mon + 1) | enable;
412         regs[ISL1208_REG_DWA - offs] = bin2bcd(alarm_tm->tm_wday & 7) | enable;
413
414         /* write ALARM registers */
415         err = isl1208_i2c_set_regs(client, offs, regs,
416                                   ISL1208_ALARM_SECTION_LEN);
417         if (err < 0) {
418                 dev_err(&client->dev, "%s: writing ALARM section failed\n",
419                         __func__);
420                 return err;
421         }
422
423         err = isl1208_rtc_toggle_alarm(client, enable);
424         if (err)
425                 return err;
426
427         return 0;
428 }
429
430 static int
431 isl1208_rtc_read_time(struct device *dev, struct rtc_time *tm)
432 {
433         return isl1208_i2c_read_time(to_i2c_client(dev), tm);
434 }
435
436 static int
437 isl1208_i2c_set_time(struct i2c_client *client, struct rtc_time const *tm)
438 {
439         int sr;
440         u8 regs[ISL1208_RTC_SECTION_LEN] = { 0, };
441
442         /* The clock has an 8 bit wide bcd-coded register (they never learn)
443          * for the year. tm_year is an offset from 1900 and we are interested
444          * in the 2000-2099 range, so any value less than 100 is invalid.
445          */
446         if (tm->tm_year < 100)
447                 return -EINVAL;
448
449         regs[ISL1208_REG_SC] = bin2bcd(tm->tm_sec);
450         regs[ISL1208_REG_MN] = bin2bcd(tm->tm_min);
451         regs[ISL1208_REG_HR] = bin2bcd(tm->tm_hour) | ISL1208_REG_HR_MIL;
452
453         regs[ISL1208_REG_DT] = bin2bcd(tm->tm_mday);
454         regs[ISL1208_REG_MO] = bin2bcd(tm->tm_mon + 1);
455         regs[ISL1208_REG_YR] = bin2bcd(tm->tm_year - 100);
456
457         regs[ISL1208_REG_DW] = bin2bcd(tm->tm_wday & 7);
458
459         sr = isl1208_i2c_get_sr(client);
460         if (sr < 0) {
461                 dev_err(&client->dev, "%s: reading SR failed\n", __func__);
462                 return sr;
463         }
464
465         /* set WRTC */
466         sr = i2c_smbus_write_byte_data(client, ISL1208_REG_SR,
467                                        sr | ISL1208_REG_SR_WRTC);
468         if (sr < 0) {
469                 dev_err(&client->dev, "%s: writing SR failed\n", __func__);
470                 return sr;
471         }
472
473         /* write RTC registers */
474         sr = isl1208_i2c_set_regs(client, 0, regs, ISL1208_RTC_SECTION_LEN);
475         if (sr < 0) {
476                 dev_err(&client->dev, "%s: writing RTC section failed\n",
477                         __func__);
478                 return sr;
479         }
480
481         /* clear WRTC again */
482         sr = isl1208_i2c_get_sr(client);
483         if (sr < 0) {
484                 dev_err(&client->dev, "%s: reading SR failed\n", __func__);
485                 return sr;
486         }
487         sr = i2c_smbus_write_byte_data(client, ISL1208_REG_SR,
488                                        sr & ~ISL1208_REG_SR_WRTC);
489         if (sr < 0) {
490                 dev_err(&client->dev, "%s: writing SR failed\n", __func__);
491                 return sr;
492         }
493
494         return 0;
495 }
496
497
498 static int
499 isl1208_rtc_set_time(struct device *dev, struct rtc_time *tm)
500 {
501         return isl1208_i2c_set_time(to_i2c_client(dev), tm);
502 }
503
504 static int
505 isl1208_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
506 {
507         return isl1208_i2c_read_alarm(to_i2c_client(dev), alarm);
508 }
509
510 static int
511 isl1208_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
512 {
513         return isl1208_i2c_set_alarm(to_i2c_client(dev), alarm);
514 }
515
516 static ssize_t timestamp0_store(struct device *dev,
517                                 struct device_attribute *attr,
518                                 const char *buf, size_t count)
519 {
520         struct i2c_client *client = to_i2c_client(dev->parent);
521         int sr;
522
523         sr = isl1208_i2c_get_sr(client);
524         if (sr < 0) {
525                 dev_err(dev, "%s: reading SR failed\n", __func__);
526                 return sr;
527         }
528
529         sr &= ~ISL1208_REG_SR_EVT;
530
531         sr = i2c_smbus_write_byte_data(client, ISL1208_REG_SR, sr);
532         if (sr < 0)
533                 dev_err(dev, "%s: writing SR failed\n",
534                         __func__);
535
536         return count;
537 };
538
539 static ssize_t timestamp0_show(struct device *dev,
540                                struct device_attribute *attr, char *buf)
541 {
542         struct i2c_client *client = to_i2c_client(dev->parent);
543         u8 regs[ISL1219_EVT_SECTION_LEN] = { 0, };
544         struct rtc_time tm;
545         int sr;
546
547         sr = isl1208_i2c_get_sr(client);
548         if (sr < 0) {
549                 dev_err(dev, "%s: reading SR failed\n", __func__);
550                 return sr;
551         }
552
553         if (!(sr & ISL1208_REG_SR_EVT))
554                 return 0;
555
556         sr = isl1208_i2c_read_regs(client, ISL1219_REG_SCT, regs,
557                                    ISL1219_EVT_SECTION_LEN);
558         if (sr < 0) {
559                 dev_err(dev, "%s: reading event section failed\n",
560                         __func__);
561                 return 0;
562         }
563
564         /* MSB of each alarm register is an enable bit */
565         tm.tm_sec = bcd2bin(regs[ISL1219_REG_SCT - ISL1219_REG_SCT] & 0x7f);
566         tm.tm_min = bcd2bin(regs[ISL1219_REG_MNT - ISL1219_REG_SCT] & 0x7f);
567         tm.tm_hour = bcd2bin(regs[ISL1219_REG_HRT - ISL1219_REG_SCT] & 0x3f);
568         tm.tm_mday = bcd2bin(regs[ISL1219_REG_DTT - ISL1219_REG_SCT] & 0x3f);
569         tm.tm_mon =
570                 bcd2bin(regs[ISL1219_REG_MOT - ISL1219_REG_SCT] & 0x1f) - 1;
571         tm.tm_year = bcd2bin(regs[ISL1219_REG_YRT - ISL1219_REG_SCT]) + 100;
572
573         sr = rtc_valid_tm(&tm);
574         if (sr)
575                 return sr;
576
577         return sprintf(buf, "%llu\n",
578                                 (unsigned long long)rtc_tm_to_time64(&tm));
579 };
580
581 static DEVICE_ATTR_RW(timestamp0);
582
583 static irqreturn_t
584 isl1208_rtc_interrupt(int irq, void *data)
585 {
586         unsigned long timeout = jiffies + msecs_to_jiffies(1000);
587         struct i2c_client *client = data;
588         struct rtc_device *rtc = i2c_get_clientdata(client);
589         int handled = 0, sr, err;
590
591         /*
592          * I2C reads get NAK'ed if we read straight away after an interrupt?
593          * Using a mdelay/msleep didn't seem to help either, so we work around
594          * this by continually trying to read the register for a short time.
595          */
596         while (1) {
597                 sr = isl1208_i2c_get_sr(client);
598                 if (sr >= 0)
599                         break;
600
601                 if (time_after(jiffies, timeout)) {
602                         dev_err(&client->dev, "%s: reading SR failed\n",
603                                 __func__);
604                         return sr;
605                 }
606         }
607
608         if (sr & ISL1208_REG_SR_ALM) {
609                 dev_dbg(&client->dev, "alarm!\n");
610
611                 rtc_update_irq(rtc, 1, RTC_IRQF | RTC_AF);
612
613                 /* Clear the alarm */
614                 sr &= ~ISL1208_REG_SR_ALM;
615                 sr = i2c_smbus_write_byte_data(client, ISL1208_REG_SR, sr);
616                 if (sr < 0)
617                         dev_err(&client->dev, "%s: writing SR failed\n",
618                                 __func__);
619                 else
620                         handled = 1;
621
622                 /* Disable the alarm */
623                 err = isl1208_rtc_toggle_alarm(client, 0);
624                 if (err)
625                         return err;
626         }
627
628         if (sr & ISL1208_REG_SR_EVT) {
629                 sysfs_notify(&rtc->dev.kobj, NULL,
630                              dev_attr_timestamp0.attr.name);
631                 dev_warn(&client->dev, "event detected");
632                 handled = 1;
633         }
634
635         return handled ? IRQ_HANDLED : IRQ_NONE;
636 }
637
638 static const struct rtc_class_ops isl1208_rtc_ops = {
639         .proc = isl1208_rtc_proc,
640         .read_time = isl1208_rtc_read_time,
641         .set_time = isl1208_rtc_set_time,
642         .read_alarm = isl1208_rtc_read_alarm,
643         .set_alarm = isl1208_rtc_set_alarm,
644 };
645
646 /* sysfs interface */
647
648 static ssize_t
649 isl1208_sysfs_show_atrim(struct device *dev,
650                          struct device_attribute *attr, char *buf)
651 {
652         int atr = isl1208_i2c_get_atr(to_i2c_client(dev->parent));
653         if (atr < 0)
654                 return atr;
655
656         return sprintf(buf, "%d.%.2d pF\n", atr >> 2, (atr & 0x3) * 25);
657 }
658
659 static DEVICE_ATTR(atrim, S_IRUGO, isl1208_sysfs_show_atrim, NULL);
660
661 static ssize_t
662 isl1208_sysfs_show_dtrim(struct device *dev,
663                          struct device_attribute *attr, char *buf)
664 {
665         int dtr = isl1208_i2c_get_dtr(to_i2c_client(dev->parent));
666         if (dtr < 0)
667                 return dtr;
668
669         return sprintf(buf, "%d ppm\n", dtr);
670 }
671
672 static DEVICE_ATTR(dtrim, S_IRUGO, isl1208_sysfs_show_dtrim, NULL);
673
674 static ssize_t
675 isl1208_sysfs_show_usr(struct device *dev,
676                        struct device_attribute *attr, char *buf)
677 {
678         int usr = isl1208_i2c_get_usr(to_i2c_client(dev->parent));
679         if (usr < 0)
680                 return usr;
681
682         return sprintf(buf, "0x%.4x\n", usr);
683 }
684
685 static ssize_t
686 isl1208_sysfs_store_usr(struct device *dev,
687                         struct device_attribute *attr,
688                         const char *buf, size_t count)
689 {
690         int usr = -1;
691
692         if (buf[0] == '0' && (buf[1] == 'x' || buf[1] == 'X')) {
693                 if (sscanf(buf, "%x", &usr) != 1)
694                         return -EINVAL;
695         } else {
696                 if (sscanf(buf, "%d", &usr) != 1)
697                         return -EINVAL;
698         }
699
700         if (usr < 0 || usr > 0xffff)
701                 return -EINVAL;
702
703         if (isl1208_i2c_set_usr(to_i2c_client(dev->parent), usr))
704                 return -EIO;
705
706         return count;
707 }
708
709 static DEVICE_ATTR(usr, S_IRUGO | S_IWUSR, isl1208_sysfs_show_usr,
710                    isl1208_sysfs_store_usr);
711
712 static struct attribute *isl1208_rtc_attrs[] = {
713         &dev_attr_atrim.attr,
714         &dev_attr_dtrim.attr,
715         &dev_attr_usr.attr,
716         NULL
717 };
718
719 static const struct attribute_group isl1208_rtc_sysfs_files = {
720         .attrs  = isl1208_rtc_attrs,
721 };
722
723 static struct attribute *isl1219_rtc_attrs[] = {
724         &dev_attr_timestamp0.attr,
725         NULL
726 };
727
728 static const struct attribute_group isl1219_rtc_sysfs_files = {
729         .attrs  = isl1219_rtc_attrs,
730 };
731
732 static int isl1208_setup_irq(struct i2c_client *client, int irq)
733 {
734         int rc = devm_request_threaded_irq(&client->dev, irq, NULL,
735                                         isl1208_rtc_interrupt,
736                                         IRQF_SHARED | IRQF_ONESHOT,
737                                         isl1208_driver.driver.name,
738                                         client);
739         if (!rc) {
740                 device_init_wakeup(&client->dev, 1);
741                 enable_irq_wake(irq);
742         } else {
743                 dev_err(&client->dev,
744                         "Unable to request irq %d, no alarm support\n",
745                         irq);
746         }
747         return rc;
748 }
749
750 static int
751 isl1208_probe(struct i2c_client *client, const struct i2c_device_id *id)
752 {
753         int rc = 0;
754         struct rtc_device *rtc;
755         int evdet_irq = -1;
756
757         if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
758                 return -ENODEV;
759
760         if (isl1208_i2c_validate_client(client) < 0)
761                 return -ENODEV;
762
763         rtc = devm_rtc_allocate_device(&client->dev);
764         if (IS_ERR(rtc))
765                 return PTR_ERR(rtc);
766
767         rtc->ops = &isl1208_rtc_ops;
768
769         i2c_set_clientdata(client, rtc);
770
771         rc = isl1208_i2c_get_sr(client);
772         if (rc < 0) {
773                 dev_err(&client->dev, "reading status failed\n");
774                 return rc;
775         }
776
777         if (rc & ISL1208_REG_SR_RTCF)
778                 dev_warn(&client->dev, "rtc power failure detected, "
779                          "please set clock.\n");
780
781         if (id->driver_data == TYPE_ISL1219) {
782                 struct device_node *np = client->dev.of_node;
783                 u32 evienb;
784
785                 rc = i2c_smbus_read_byte_data(client, ISL1219_REG_EV);
786                 if (rc < 0) {
787                         dev_err(&client->dev, "failed to read EV reg\n");
788                         return rc;
789                 }
790                 rc |= ISL1219_REG_EV_EVEN;
791                 if (!of_property_read_u32(np, "isil,ev-evienb", &evienb)) {
792                         if (evienb)
793                                 rc |= ISL1219_REG_EV_EVIENB;
794                         else
795                                 rc &= ~ISL1219_REG_EV_EVIENB;
796                 }
797                 rc = i2c_smbus_write_byte_data(client, ISL1219_REG_EV, rc);
798                 if (rc < 0) {
799                         dev_err(&client->dev, "could not enable tamper detection\n");
800                         return rc;
801                 }
802                 rc = rtc_add_group(rtc, &isl1219_rtc_sysfs_files);
803                 if (rc)
804                         return rc;
805                 evdet_irq = of_irq_get_byname(np, "evdet");
806         }
807
808         rc = rtc_add_group(rtc, &isl1208_rtc_sysfs_files);
809         if (rc)
810                 return rc;
811
812         if (client->irq > 0)
813                 rc = isl1208_setup_irq(client, client->irq);
814         if (rc)
815                 return rc;
816
817         if (evdet_irq > 0 && evdet_irq != client->irq)
818                 rc = isl1208_setup_irq(client, evdet_irq);
819         if (rc)
820                 return rc;
821
822         return rtc_register_device(rtc);
823 }
824
825 static const struct i2c_device_id isl1208_id[] = {
826         { "isl1208", TYPE_ISL1208 },
827         { "isl1218", TYPE_ISL1218 },
828         { "isl1219", TYPE_ISL1219 },
829         { }
830 };
831 MODULE_DEVICE_TABLE(i2c, isl1208_id);
832
833 static const struct of_device_id isl1208_of_match[] = {
834         { .compatible = "isil,isl1208" },
835         { .compatible = "isil,isl1218" },
836         { .compatible = "isil,isl1219" },
837         { }
838 };
839 MODULE_DEVICE_TABLE(of, isl1208_of_match);
840
841 static struct i2c_driver isl1208_driver = {
842         .driver = {
843                 .name = "rtc-isl1208",
844                 .of_match_table = of_match_ptr(isl1208_of_match),
845         },
846         .probe = isl1208_probe,
847         .id_table = isl1208_id,
848 };
849
850 module_i2c_driver(isl1208_driver);
851
852 MODULE_AUTHOR("Herbert Valerio Riedel <hvr@gnu.org>");
853 MODULE_DESCRIPTION("Intersil ISL1208 RTC driver");
854 MODULE_LICENSE("GPL");