1ed3403ff8ac23ae1c9c2923b76f3f8aff1c9c11
[linux-2.6-microblaze.git] / drivers / rtc / rtc-rx8010.c
1 /*
2  * Driver for the Epson RTC module RX-8010 SJ
3  *
4  * Copyright(C) Timesys Corporation 2015
5  * Copyright(C) General Electric Company 2015
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  *
11  */
12
13 #include <linux/bcd.h>
14 #include <linux/bitops.h>
15 #include <linux/i2c.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/rtc.h>
19
20 #define RX8010_SEC     0x10
21 #define RX8010_MIN     0x11
22 #define RX8010_HOUR    0x12
23 #define RX8010_WDAY    0x13
24 #define RX8010_MDAY    0x14
25 #define RX8010_MONTH   0x15
26 #define RX8010_YEAR    0x16
27 #define RX8010_YEAR    0x16
28 #define RX8010_RESV17  0x17
29 #define RX8010_ALMIN   0x18
30 #define RX8010_ALHOUR  0x19
31 #define RX8010_ALWDAY  0x1A
32 #define RX8010_TCOUNT0 0x1B
33 #define RX8010_TCOUNT1 0x1C
34 #define RX8010_EXT     0x1D
35 #define RX8010_FLAG    0x1E
36 #define RX8010_CTRL    0x1F
37 /* 0x20 to 0x2F are user registers */
38 #define RX8010_RESV30  0x30
39 #define RX8010_RESV31  0x32
40 #define RX8010_IRQ     0x32
41
42 #define RX8010_EXT_WADA  BIT(3)
43
44 #define RX8010_FLAG_VLF  BIT(1)
45 #define RX8010_FLAG_AF   BIT(3)
46 #define RX8010_FLAG_TF   BIT(4)
47 #define RX8010_FLAG_UF   BIT(5)
48
49 #define RX8010_CTRL_AIE  BIT(3)
50 #define RX8010_CTRL_UIE  BIT(5)
51 #define RX8010_CTRL_STOP BIT(6)
52 #define RX8010_CTRL_TEST BIT(7)
53
54 #define RX8010_ALARM_AE  BIT(7)
55
56 static const struct i2c_device_id rx8010_id[] = {
57         { "rx8010", 0 },
58         { }
59 };
60 MODULE_DEVICE_TABLE(i2c, rx8010_id);
61
62 static const struct of_device_id rx8010_of_match[] = {
63         { .compatible = "epson,rx8010" },
64         { }
65 };
66 MODULE_DEVICE_TABLE(of, rx8010_of_match);
67
68 struct rx8010_data {
69         struct i2c_client *client;
70         struct rtc_device *rtc;
71         u8 ctrlreg;
72 };
73
74 static irqreturn_t rx8010_irq_1_handler(int irq, void *dev_id)
75 {
76         struct i2c_client *client = dev_id;
77         struct rx8010_data *rx8010 = i2c_get_clientdata(client);
78         int flagreg;
79
80         mutex_lock(&rx8010->rtc->ops_lock);
81
82         flagreg = i2c_smbus_read_byte_data(client, RX8010_FLAG);
83
84         if (flagreg <= 0) {
85                 mutex_unlock(&rx8010->rtc->ops_lock);
86                 return IRQ_NONE;
87         }
88
89         if (flagreg & RX8010_FLAG_VLF)
90                 dev_warn(&client->dev, "Frequency stop detected\n");
91
92         if (flagreg & RX8010_FLAG_TF) {
93                 flagreg &= ~RX8010_FLAG_TF;
94                 rtc_update_irq(rx8010->rtc, 1, RTC_PF | RTC_IRQF);
95         }
96
97         if (flagreg & RX8010_FLAG_AF) {
98                 flagreg &= ~RX8010_FLAG_AF;
99                 rtc_update_irq(rx8010->rtc, 1, RTC_AF | RTC_IRQF);
100         }
101
102         if (flagreg & RX8010_FLAG_UF) {
103                 flagreg &= ~RX8010_FLAG_UF;
104                 rtc_update_irq(rx8010->rtc, 1, RTC_UF | RTC_IRQF);
105         }
106
107         i2c_smbus_write_byte_data(client, RX8010_FLAG, flagreg);
108
109         mutex_unlock(&rx8010->rtc->ops_lock);
110         return IRQ_HANDLED;
111 }
112
113 static int rx8010_get_time(struct device *dev, struct rtc_time *dt)
114 {
115         struct rx8010_data *rx8010 = dev_get_drvdata(dev);
116         u8 date[7];
117         int flagreg;
118         int err;
119
120         flagreg = i2c_smbus_read_byte_data(rx8010->client, RX8010_FLAG);
121         if (flagreg < 0)
122                 return flagreg;
123
124         if (flagreg & RX8010_FLAG_VLF) {
125                 dev_warn(dev, "Frequency stop detected\n");
126                 return -EINVAL;
127         }
128
129         err = i2c_smbus_read_i2c_block_data(rx8010->client, RX8010_SEC,
130                                             7, date);
131         if (err != 7)
132                 return err < 0 ? err : -EIO;
133
134         dt->tm_sec = bcd2bin(date[RX8010_SEC - RX8010_SEC] & 0x7f);
135         dt->tm_min = bcd2bin(date[RX8010_MIN - RX8010_SEC] & 0x7f);
136         dt->tm_hour = bcd2bin(date[RX8010_HOUR - RX8010_SEC] & 0x3f);
137         dt->tm_mday = bcd2bin(date[RX8010_MDAY - RX8010_SEC] & 0x3f);
138         dt->tm_mon = bcd2bin(date[RX8010_MONTH - RX8010_SEC] & 0x1f) - 1;
139         dt->tm_year = bcd2bin(date[RX8010_YEAR - RX8010_SEC]) + 100;
140         dt->tm_wday = ffs(date[RX8010_WDAY - RX8010_SEC] & 0x7f);
141
142         return rtc_valid_tm(dt);
143 }
144
145 static int rx8010_set_time(struct device *dev, struct rtc_time *dt)
146 {
147         struct rx8010_data *rx8010 = dev_get_drvdata(dev);
148         u8 date[7];
149         int ctrl, flagreg;
150         int ret;
151
152         if ((dt->tm_year < 100) || (dt->tm_year > 199))
153                 return -EINVAL;
154
155         /* set STOP bit before changing clock/calendar */
156         ctrl = i2c_smbus_read_byte_data(rx8010->client, RX8010_CTRL);
157         if (ctrl < 0)
158                 return ctrl;
159         rx8010->ctrlreg = ctrl | RX8010_CTRL_STOP;
160         ret = i2c_smbus_write_byte_data(rx8010->client, RX8010_CTRL,
161                                         rx8010->ctrlreg);
162         if (ret < 0)
163                 return ret;
164
165         date[RX8010_SEC - RX8010_SEC] = bin2bcd(dt->tm_sec);
166         date[RX8010_MIN - RX8010_SEC] = bin2bcd(dt->tm_min);
167         date[RX8010_HOUR - RX8010_SEC] = bin2bcd(dt->tm_hour);
168         date[RX8010_MDAY - RX8010_SEC] = bin2bcd(dt->tm_mday);
169         date[RX8010_MONTH - RX8010_SEC] = bin2bcd(dt->tm_mon + 1);
170         date[RX8010_YEAR - RX8010_SEC] = bin2bcd(dt->tm_year - 100);
171         date[RX8010_WDAY - RX8010_SEC] = bin2bcd(1 << dt->tm_wday);
172
173         ret = i2c_smbus_write_i2c_block_data(rx8010->client,
174                                              RX8010_SEC, 7, date);
175         if (ret < 0)
176                 return ret;
177
178         /* clear STOP bit after changing clock/calendar */
179         ctrl = i2c_smbus_read_byte_data(rx8010->client, RX8010_CTRL);
180         if (ctrl < 0)
181                 return ctrl;
182         rx8010->ctrlreg = ctrl & ~RX8010_CTRL_STOP;
183         ret = i2c_smbus_write_byte_data(rx8010->client, RX8010_CTRL,
184                                         rx8010->ctrlreg);
185         if (ret < 0)
186                 return ret;
187
188         flagreg = i2c_smbus_read_byte_data(rx8010->client, RX8010_FLAG);
189         if (flagreg < 0) {
190                 return flagreg;
191         }
192
193         if (flagreg & RX8010_FLAG_VLF)
194                 ret = i2c_smbus_write_byte_data(rx8010->client, RX8010_FLAG,
195                                                 flagreg & ~RX8010_FLAG_VLF);
196
197         return 0;
198 }
199
200 static int rx8010_init_client(struct i2c_client *client)
201 {
202         struct rx8010_data *rx8010 = i2c_get_clientdata(client);
203         u8 ctrl[2];
204         int need_clear = 0, err = 0;
205
206         /* Initialize reserved registers as specified in datasheet */
207         err = i2c_smbus_write_byte_data(client, RX8010_RESV17, 0xD8);
208         if (err < 0)
209                 return err;
210
211         err = i2c_smbus_write_byte_data(client, RX8010_RESV30, 0x00);
212         if (err < 0)
213                 return err;
214
215         err = i2c_smbus_write_byte_data(client, RX8010_RESV31, 0x08);
216         if (err < 0)
217                 return err;
218
219         err = i2c_smbus_write_byte_data(client, RX8010_IRQ, 0x00);
220         if (err < 0)
221                 return err;
222
223         err = i2c_smbus_read_i2c_block_data(rx8010->client, RX8010_FLAG,
224                                             2, ctrl);
225         if (err != 2)
226                 return err < 0 ? err : -EIO;
227
228         if (ctrl[0] & RX8010_FLAG_VLF)
229                 dev_warn(&client->dev, "Frequency stop was detected\n");
230
231         if (ctrl[0] & RX8010_FLAG_AF) {
232                 dev_warn(&client->dev, "Alarm was detected\n");
233                 need_clear = 1;
234         }
235
236         if (ctrl[0] & RX8010_FLAG_TF)
237                 need_clear = 1;
238
239         if (ctrl[0] & RX8010_FLAG_UF)
240                 need_clear = 1;
241
242         if (need_clear) {
243                 ctrl[0] &= ~(RX8010_FLAG_AF | RX8010_FLAG_TF | RX8010_FLAG_UF);
244                 err = i2c_smbus_write_byte_data(client, RX8010_FLAG, ctrl[0]);
245                 if (err < 0)
246                         return err;
247         }
248
249         rx8010->ctrlreg = (ctrl[1] & ~RX8010_CTRL_TEST);
250
251         return err;
252 }
253
254 static int rx8010_read_alarm(struct device *dev, struct rtc_wkalrm *t)
255 {
256         struct rx8010_data *rx8010 = dev_get_drvdata(dev);
257         struct i2c_client *client = rx8010->client;
258         u8 alarmvals[3];
259         int flagreg;
260         int err;
261
262         err = i2c_smbus_read_i2c_block_data(client, RX8010_ALMIN, 3, alarmvals);
263         if (err != 3)
264                 return err < 0 ? err : -EIO;
265
266         flagreg = i2c_smbus_read_byte_data(client, RX8010_FLAG);
267         if (flagreg < 0)
268                 return flagreg;
269
270         t->time.tm_sec = 0;
271         t->time.tm_min = bcd2bin(alarmvals[0] & 0x7f);
272         t->time.tm_hour = bcd2bin(alarmvals[1] & 0x3f);
273
274         if (!(alarmvals[2] & RX8010_ALARM_AE))
275                 t->time.tm_mday = bcd2bin(alarmvals[2] & 0x7f);
276
277         t->enabled = !!(rx8010->ctrlreg & RX8010_CTRL_AIE);
278         t->pending = (flagreg & RX8010_FLAG_AF) && t->enabled;
279
280         return err;
281 }
282
283 static int rx8010_set_alarm(struct device *dev, struct rtc_wkalrm *t)
284 {
285         struct i2c_client *client = to_i2c_client(dev);
286         struct rx8010_data *rx8010 = dev_get_drvdata(dev);
287         u8 alarmvals[3];
288         int extreg, flagreg;
289         int err;
290
291         flagreg = i2c_smbus_read_byte_data(client, RX8010_FLAG);
292         if (flagreg < 0) {
293                 return flagreg;
294         }
295
296         if (rx8010->ctrlreg & (RX8010_CTRL_AIE | RX8010_CTRL_UIE)) {
297                 rx8010->ctrlreg &= ~(RX8010_CTRL_AIE | RX8010_CTRL_UIE);
298                 err = i2c_smbus_write_byte_data(rx8010->client, RX8010_CTRL,
299                                                 rx8010->ctrlreg);
300                 if (err < 0) {
301                         return err;
302                 }
303         }
304
305         flagreg &= ~RX8010_FLAG_AF;
306         err = i2c_smbus_write_byte_data(rx8010->client, RX8010_FLAG, flagreg);
307         if (err < 0)
308                 return err;
309
310         alarmvals[0] = bin2bcd(t->time.tm_min);
311         alarmvals[1] = bin2bcd(t->time.tm_hour);
312         alarmvals[2] = bin2bcd(t->time.tm_mday);
313
314         err = i2c_smbus_write_i2c_block_data(rx8010->client, RX8010_ALMIN,
315                                              2, alarmvals);
316         if (err < 0)
317                 return err;
318
319         extreg = i2c_smbus_read_byte_data(client, RX8010_EXT);
320         if (extreg < 0)
321                 return extreg;
322
323         extreg |= RX8010_EXT_WADA;
324         err = i2c_smbus_write_byte_data(rx8010->client, RX8010_EXT, extreg);
325         if (err < 0)
326                 return err;
327
328         if (alarmvals[2] == 0)
329                 alarmvals[2] |= RX8010_ALARM_AE;
330
331         err = i2c_smbus_write_byte_data(rx8010->client, RX8010_ALWDAY,
332                                         alarmvals[2]);
333         if (err < 0)
334                 return err;
335
336         if (t->enabled) {
337                 if (rx8010->rtc->uie_rtctimer.enabled)
338                         rx8010->ctrlreg |= RX8010_CTRL_UIE;
339                 if (rx8010->rtc->aie_timer.enabled)
340                         rx8010->ctrlreg |=
341                                 (RX8010_CTRL_AIE | RX8010_CTRL_UIE);
342
343                 err = i2c_smbus_write_byte_data(rx8010->client, RX8010_CTRL,
344                                                 rx8010->ctrlreg);
345                 if (err < 0)
346                         return err;
347         }
348
349         return 0;
350 }
351
352 static int rx8010_alarm_irq_enable(struct device *dev,
353                                    unsigned int enabled)
354 {
355         struct i2c_client *client = to_i2c_client(dev);
356         struct rx8010_data *rx8010 = dev_get_drvdata(dev);
357         int flagreg;
358         u8 ctrl;
359         int err;
360
361         ctrl = rx8010->ctrlreg;
362
363         if (enabled) {
364                 if (rx8010->rtc->uie_rtctimer.enabled)
365                         ctrl |= RX8010_CTRL_UIE;
366                 if (rx8010->rtc->aie_timer.enabled)
367                         ctrl |= (RX8010_CTRL_AIE | RX8010_CTRL_UIE);
368         } else {
369                 if (!rx8010->rtc->uie_rtctimer.enabled)
370                         ctrl &= ~RX8010_CTRL_UIE;
371                 if (!rx8010->rtc->aie_timer.enabled)
372                         ctrl &= ~RX8010_CTRL_AIE;
373         }
374
375         flagreg = i2c_smbus_read_byte_data(client, RX8010_FLAG);
376         if (flagreg < 0)
377                 return flagreg;
378
379         flagreg &= ~RX8010_FLAG_AF;
380         err = i2c_smbus_write_byte_data(rx8010->client, RX8010_FLAG, flagreg);
381         if (err < 0)
382                 return err;
383
384         if (ctrl != rx8010->ctrlreg) {
385                 rx8010->ctrlreg = ctrl;
386                 err = i2c_smbus_write_byte_data(rx8010->client, RX8010_CTRL,
387                                                 rx8010->ctrlreg);
388                 if (err < 0)
389                         return err;
390         }
391
392         return 0;
393 }
394
395 static int rx8010_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
396 {
397         struct i2c_client *client = to_i2c_client(dev);
398         struct rx8010_data *rx8010 = dev_get_drvdata(dev);
399         int ret, tmp;
400         int flagreg;
401
402         switch (cmd) {
403         case RTC_VL_READ:
404                 flagreg = i2c_smbus_read_byte_data(rx8010->client, RX8010_FLAG);
405                 if (flagreg < 0)
406                         return flagreg;
407
408                 tmp = !!(flagreg & RX8010_FLAG_VLF);
409                 if (copy_to_user((void __user *)arg, &tmp, sizeof(int)))
410                         return -EFAULT;
411
412                 return 0;
413
414         case RTC_VL_CLR:
415                 flagreg = i2c_smbus_read_byte_data(rx8010->client, RX8010_FLAG);
416                 if (flagreg < 0) {
417                         return flagreg;
418                 }
419
420                 flagreg &= ~RX8010_FLAG_VLF;
421                 ret = i2c_smbus_write_byte_data(client, RX8010_FLAG, flagreg);
422                 if (ret < 0)
423                         return ret;
424
425                 return 0;
426
427         default:
428                 return -ENOIOCTLCMD;
429         }
430 }
431
432 static struct rtc_class_ops rx8010_rtc_ops = {
433         .read_time = rx8010_get_time,
434         .set_time = rx8010_set_time,
435         .ioctl = rx8010_ioctl,
436 };
437
438 static int rx8010_probe(struct i2c_client *client,
439                         const struct i2c_device_id *id)
440 {
441         struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
442         struct rx8010_data *rx8010;
443         int err = 0;
444
445         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA
446                 | I2C_FUNC_SMBUS_I2C_BLOCK)) {
447                 dev_err(&adapter->dev, "doesn't support required functionality\n");
448                 return -EIO;
449         }
450
451         rx8010 = devm_kzalloc(&client->dev, sizeof(struct rx8010_data),
452                               GFP_KERNEL);
453         if (!rx8010)
454                 return -ENOMEM;
455
456         rx8010->client = client;
457         i2c_set_clientdata(client, rx8010);
458
459         err = rx8010_init_client(client);
460         if (err)
461                 return err;
462
463         if (client->irq > 0) {
464                 dev_info(&client->dev, "IRQ %d supplied\n", client->irq);
465                 err = devm_request_threaded_irq(&client->dev, client->irq, NULL,
466                                                 rx8010_irq_1_handler,
467                                                 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
468                                                 "rx8010", client);
469
470                 if (err) {
471                         dev_err(&client->dev, "unable to request IRQ\n");
472                         client->irq = 0;
473                 } else {
474                         rx8010_rtc_ops.read_alarm = rx8010_read_alarm;
475                         rx8010_rtc_ops.set_alarm = rx8010_set_alarm;
476                         rx8010_rtc_ops.alarm_irq_enable = rx8010_alarm_irq_enable;
477                 }
478         }
479
480         rx8010->rtc = devm_rtc_device_register(&client->dev, client->name,
481                 &rx8010_rtc_ops, THIS_MODULE);
482
483         if (IS_ERR(rx8010->rtc)) {
484                 dev_err(&client->dev, "unable to register the class device\n");
485                 return PTR_ERR(rx8010->rtc);
486         }
487
488         rx8010->rtc->max_user_freq = 1;
489
490         return err;
491 }
492
493 static struct i2c_driver rx8010_driver = {
494         .driver = {
495                 .name = "rtc-rx8010",
496                 .of_match_table = of_match_ptr(rx8010_of_match),
497         },
498         .probe          = rx8010_probe,
499         .id_table       = rx8010_id,
500 };
501
502 module_i2c_driver(rx8010_driver);
503
504 MODULE_AUTHOR("Akshay Bhat <akshay.bhat@timesys.com>");
505 MODULE_DESCRIPTION("Epson RX8010SJ RTC driver");
506 MODULE_LICENSE("GPL v2");