Revert "temp revert rk change"
[firefly-linux-kernel-4.4.55.git] / drivers / rtc / rtc-HYM8563.c
1 /* drivers/rtc/rtc-HYM8563.c - driver for HYM8563
2  *
3  * Copyright (C) 2010 ROCKCHIP, Inc.
4  *
5  * This software is licensed under the terms of the GNU General Public
6  * License version 2, as published by the Free Software Foundation, and
7  * may be copied, distributed, and modified under those terms.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14
15 //#define DEBUG
16 #define pr_fmt(fmt) "rtc: %s: " fmt, __func__
17
18 #include <linux/module.h>
19 #include <linux/i2c.h>
20 #include <linux/bcd.h>
21 #include <linux/rtc.h>
22 #include <linux/delay.h>
23 #include <linux/wakelock.h>
24 #include <linux/slab.h>
25 #include <mach/gpio.h>
26 #include <mach/iomux.h>
27 #include "rtc-HYM8563.h"
28
29 #define RTC_SPEED       100 * 1000
30
31 struct hym8563 {
32         int irq;
33         struct i2c_client *client;
34         struct work_struct work;
35         struct mutex mutex;
36         struct rtc_device *rtc;
37         int exiting;
38         struct rtc_wkalrm alarm;
39         struct wake_lock wake_lock;
40 };
41
42 static int hym8563_i2c_read_regs(struct i2c_client *client, u8 reg, u8 buf[], unsigned len)
43 {
44         int ret; 
45         ret = i2c_master_reg8_recv(client, reg, buf, len, RTC_SPEED);
46         return ret; 
47 }
48
49 static int hym8563_i2c_set_regs(struct i2c_client *client, u8 reg, u8 const buf[], __u16 len)
50 {
51         int ret; 
52         ret = i2c_master_reg8_send(client, reg, buf, (int)len, RTC_SPEED);
53         return ret;
54 }
55
56 /*the init of the hym8563 at first time */
57 static int hym8563_init_device(struct i2c_client *client)       
58 {
59         struct hym8563 *hym8563 = i2c_get_clientdata(client);
60         u8 regs[2];
61         int sr;
62
63         mutex_lock(&hym8563->mutex);
64         regs[0]=0;
65         hym8563_i2c_set_regs(client, RTC_CTL1, regs, 1);                
66         
67         //disable clkout
68         regs[0] = 0x80;
69         hym8563_i2c_set_regs(client, RTC_CLKOUT, regs, 1);
70         /*enable alarm interrupt*/
71         hym8563_i2c_read_regs(client, RTC_CTL2, regs, 1);
72         regs[0] = 0x0;
73         regs[0] |= AIE;
74         hym8563_i2c_set_regs(client, RTC_CTL2, regs, 1);
75         hym8563_i2c_read_regs(client, RTC_CTL2, regs, 1);
76
77         sr = hym8563_i2c_read_regs(client, RTC_CTL2, regs, 1);
78         if (sr < 0) {
79                 pr_err("read CTL2 err\n");
80         }
81         
82         if(regs[0] & (AF|TF))
83         {
84                 regs[0] &= ~(AF|TF);
85                 hym8563_i2c_set_regs(client, RTC_CTL2, regs, 1);
86         }
87         mutex_unlock(&hym8563->mutex);
88         return 0;
89 }
90
91 static int hym8563_read_datetime(struct i2c_client *client, struct rtc_time *tm)
92 {
93         struct hym8563 *hym8563 = i2c_get_clientdata(client);
94         u8 i,regs[HYM8563_RTC_SECTION_LEN] = { 0, };
95     
96         mutex_lock(&hym8563->mutex);
97 //      for (i = 0; i < HYM8563_RTC_SECTION_LEN; i++) {
98 //              hym8563_i2c_read_regs(client, RTC_SEC+i, &regs[i], 1);
99 //      }
100         hym8563_i2c_read_regs(client, RTC_SEC, regs, HYM8563_RTC_SECTION_LEN);
101
102         mutex_unlock(&hym8563->mutex);
103         
104         tm->tm_sec = bcd2bin(regs[0x00] & 0x7F);
105         tm->tm_min = bcd2bin(regs[0x01] & 0x7F);
106         tm->tm_hour = bcd2bin(regs[0x02] & 0x3F);
107         tm->tm_mday = bcd2bin(regs[0x03] & 0x3F);
108         tm->tm_wday = bcd2bin(regs[0x04] & 0x07);       
109         
110         tm->tm_mon = bcd2bin(regs[0x05] & 0x1F) ; 
111         tm->tm_mon -= 1;                        //inorder to cooperate the systerm time
112         
113         tm->tm_year = bcd2bin(regs[0x06] & 0xFF);
114         if(regs[5] & 0x80)
115                 tm->tm_year += 1900;
116         else
117                 tm->tm_year += 2000;
118                 
119         tm->tm_yday = rtc_year_days(tm->tm_mday, tm->tm_mon, tm->tm_year);      
120         tm->tm_year -= 1900;                    //inorder to cooperate the systerm time 
121         if(tm->tm_year < 0)
122                 tm->tm_year = 0;        
123         tm->tm_isdst = 0;       
124
125         pr_debug("%4d-%02d-%02d(%d) %02d:%02d:%02d\n",
126                 1900 + tm->tm_year, tm->tm_mon + 1, tm->tm_mday, tm->tm_wday,
127                 tm->tm_hour, tm->tm_min, tm->tm_sec);
128
129         return 0;
130 }
131
132 static int hym8563_rtc_read_time(struct device *dev, struct rtc_time *tm)
133 {
134         return hym8563_read_datetime(to_i2c_client(dev), tm);
135 }
136
137 static int hym8563_set_time(struct i2c_client *client, struct rtc_time *tm)     
138 {
139         struct hym8563 *hym8563 = i2c_get_clientdata(client);
140         u8 regs[HYM8563_RTC_SECTION_LEN] = { 0, };
141         u8 mon_day,i;
142         u8 ret = 0;
143
144         pr_debug("%4d-%02d-%02d(%d) %02d:%02d:%02d\n",
145                 1900 + tm->tm_year, tm->tm_mon + 1, tm->tm_mday, tm->tm_wday,
146                 tm->tm_hour, tm->tm_min, tm->tm_sec);
147
148         mon_day = rtc_month_days((tm->tm_mon), tm->tm_year + 1900);
149         
150         if(tm->tm_sec >= 60 || tm->tm_sec < 0 )         //set  sec
151                 regs[0x00] = bin2bcd(0x00);
152         else
153                 regs[0x00] = bin2bcd(tm->tm_sec);
154         
155         if(tm->tm_min >= 60 || tm->tm_min < 0 )         //set  min      
156                 regs[0x01] = bin2bcd(0x00);
157         else
158                 regs[0x01] = bin2bcd(tm->tm_min);
159
160         if(tm->tm_hour >= 24 || tm->tm_hour < 0 )               //set  hour
161                 regs[0x02] = bin2bcd(0x00);
162         else
163                 regs[0x02] = bin2bcd(tm->tm_hour);
164         
165         if((tm->tm_mday) > mon_day)                             //if the input month day is bigger than the biggest day of this month, set the biggest day 
166                 regs[0x03] = bin2bcd(mon_day);
167         else if((tm->tm_mday) > 0)
168                 regs[0x03] = bin2bcd(tm->tm_mday);
169         else if((tm->tm_mday) <= 0)
170                 regs[0x03] = bin2bcd(0x01);
171
172         if( tm->tm_year >= 200)         // year >= 2100
173                 regs[0x06] = bin2bcd(99);       //year = 2099
174         else if(tm->tm_year >= 100)                     // 2000 <= year < 2100
175                 regs[0x06] = bin2bcd(tm->tm_year - 100);
176         else if(tm->tm_year >= 0){                              // 1900 <= year < 2000
177                 regs[0x06] = bin2bcd(tm->tm_year);      
178                 regs[0x05] |= 0x80;     
179         }else{                                                                  // year < 1900
180                 regs[0x06] = bin2bcd(0);        //year = 1900   
181                 regs[0x05] |= 0x80;     
182         }       
183         regs[0x04] = bin2bcd(tm->tm_wday);              //set  the  weekday
184         regs[0x05] = (regs[0x05] & 0x80)| (bin2bcd(tm->tm_mon + 1) & 0x7F);             //set  the  month
185         
186         mutex_lock(&hym8563->mutex);
187 //      for(i=0;i<HYM8563_RTC_SECTION_LEN;i++){
188 //              ret = hym8563_i2c_set_regs(client, RTC_SEC+i, &regs[i], 1);
189 //      }
190         hym8563_i2c_set_regs(client, RTC_SEC, regs, HYM8563_RTC_SECTION_LEN);
191
192         mutex_unlock(&hym8563->mutex);
193
194         return 0;
195 }
196
197 static int hym8563_rtc_set_time(struct device *dev, struct rtc_time *tm)
198 {
199         return hym8563_set_time(to_i2c_client(dev), tm);
200 }
201
202 static int hym8563_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *tm)
203 {
204         struct i2c_client *client = to_i2c_client(dev);
205         struct hym8563 *hym8563 = i2c_get_clientdata(client);
206         u8 regs[4] = { 0, };
207         
208         pr_debug("enter\n");
209         mutex_lock(&hym8563->mutex);
210         hym8563_i2c_read_regs(client, RTC_A_MIN, regs, 4);
211         regs[0] = 0x0;
212         hym8563_i2c_set_regs(client, RTC_CTL2, regs, 1);
213         mutex_unlock(&hym8563->mutex);
214         return 0;
215 }
216
217 static int hym8563_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
218 {       
219         struct i2c_client *client = to_i2c_client(dev);
220         struct hym8563 *hym8563 = i2c_get_clientdata(client);
221         struct rtc_time now, *tm = &alarm->time;
222         u8 regs[4] = { 0, };
223         u8 mon_day;
224
225         pr_debug("%4d-%02d-%02d(%d) %02d:%02d:%02d enabled %d\n",
226                 1900 + tm->tm_year, tm->tm_mon + 1, tm->tm_mday, tm->tm_wday,
227                 tm->tm_hour, tm->tm_min, tm->tm_sec, alarm->enabled);
228
229         hym8563_read_datetime(client, &now);
230         if (alarm->enabled && now.tm_year == tm->tm_year &&
231             now.tm_mon == tm->tm_mon && now.tm_mday == tm->tm_mday &&
232             now.tm_hour == tm->tm_hour && now.tm_min == tm->tm_min &&
233             tm->tm_sec > now.tm_sec) {
234                 long timeout = tm->tm_sec - now.tm_sec + 1;
235                 pr_info("stay awake %lds\n", timeout);
236                 wake_lock_timeout(&hym8563->wake_lock, timeout * HZ);
237         }
238
239         mutex_lock(&hym8563->mutex);
240         hym8563->alarm = *alarm;
241
242         regs[0] = 0x0;
243         hym8563_i2c_set_regs(client, RTC_CTL2, regs, 1);
244         mon_day = rtc_month_days(tm->tm_mon, tm->tm_year + 1900);
245         hym8563_i2c_read_regs(client, RTC_A_MIN, regs, 4);
246         
247         if (tm->tm_min >= 60 || tm->tm_min < 0)         //set  min
248                 regs[0x00] = bin2bcd(0x00) & 0x7f;
249         else
250                 regs[0x00] = bin2bcd(tm->tm_min) & 0x7f;
251         if (tm->tm_hour >= 24 || tm->tm_hour < 0)       //set  hour
252                 regs[0x01] = bin2bcd(0x00) & 0x7f;
253         else
254                 regs[0x01] = bin2bcd(tm->tm_hour) & 0x7f;
255         regs[0x03] = bin2bcd (tm->tm_wday) & 0x7f;
256
257         /* if the input month day is bigger than the biggest day of this month, set the biggest day */
258         if (tm->tm_mday > mon_day)
259                 regs[0x02] = bin2bcd(mon_day) & 0x7f;
260         else if (tm->tm_mday > 0)
261                 regs[0x02] = bin2bcd(tm->tm_mday) & 0x7f;
262         else if (tm->tm_mday <= 0)
263                 regs[0x02] = bin2bcd(0x01) & 0x7f;
264
265         hym8563_i2c_set_regs(client, RTC_A_MIN, regs, 4);       
266         hym8563_i2c_read_regs(client, RTC_A_MIN, regs, 4);      
267         hym8563_i2c_read_regs(client, RTC_CTL2, regs, 1);
268         if (alarm->enabled == 1)
269                 regs[0] |= AIE;
270         else
271                 regs[0] &= 0x0;
272         hym8563_i2c_set_regs(client, RTC_CTL2, regs, 1);
273         hym8563_i2c_read_regs(client, RTC_CTL2, regs, 1);
274
275         mutex_unlock(&hym8563->mutex);
276         return 0;
277 }
278
279 #if defined(CONFIG_RTC_INTF_DEV) || defined(CONFIG_RTC_INTF_DEV_MODULE)
280 static int hym8563_i2c_open_alarm(struct i2c_client *client)
281 {
282         u8 data;
283         hym8563_i2c_read_regs(client, RTC_CTL2, &data, 1);
284         data |= AIE;
285         hym8563_i2c_set_regs(client, RTC_CTL2, &data, 1);
286
287         return 0;
288 }
289
290 static int hym8563_i2c_close_alarm(struct i2c_client *client)
291 {
292         u8 data;
293         hym8563_i2c_read_regs(client, RTC_CTL2, &data, 1);
294         data &= ~AIE;
295         hym8563_i2c_set_regs(client, RTC_CTL2, &data, 1);
296
297         return 0;
298 }
299
300 static int hym8563_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
301 {
302         struct i2c_client *client = to_i2c_client(dev);
303         
304         switch (cmd) {
305         case RTC_AIE_OFF:
306                 if(hym8563_i2c_close_alarm(client) < 0)
307                         goto err;
308                 break;
309         case RTC_AIE_ON:
310                 if(hym8563_i2c_open_alarm(client))
311                         goto err;
312                 break;
313         default:
314                 return -ENOIOCTLCMD;
315         }       
316         return 0;
317 err:
318         return -EIO;
319 }
320 #else
321 #define hym8563_rtc_ioctl NULL
322 #endif
323
324 #if defined(CONFIG_RTC_INTF_PROC) || defined(CONFIG_RTC_INTF_PROC_MODULE)
325 static int hym8563_rtc_proc(struct device *dev, struct seq_file *seq)
326 {
327         return 0;
328 }
329 #else
330 #define hym8563_rtc_proc NULL
331 #endif
332
333 static irqreturn_t hym8563_wakeup_irq(int irq, void *dev_id)
334 {       
335         struct hym8563 *hym8563 = (struct hym8563 *)dev_id;
336         pr_debug("enter\n");
337         disable_irq_nosync(irq);
338         schedule_work(&hym8563->work);
339         return IRQ_HANDLED;
340 }
341
342 static void hym8563_work_func(struct work_struct *work)
343 {       
344         struct hym8563 *hym8563 = container_of(work, struct hym8563, work);
345         struct i2c_client *client = hym8563->client;
346         struct rtc_time now;
347         u8 data;
348
349         pr_debug("enter\n");
350
351         mutex_lock(&hym8563->mutex);
352         hym8563_i2c_read_regs(client, RTC_CTL2, &data, 1);
353         data &= ~AF;
354         hym8563_i2c_set_regs(client, RTC_CTL2, &data, 1);
355         mutex_unlock(&hym8563->mutex);
356
357         hym8563_read_datetime(client, &now);
358
359         mutex_lock(&hym8563->mutex);
360         if (hym8563->alarm.enabled && hym8563->alarm.time.tm_sec > now.tm_sec) {
361                 long timeout = hym8563->alarm.time.tm_sec - now.tm_sec + 1;
362                 pr_info("stay awake %lds\n", timeout);
363                 wake_lock_timeout(&hym8563->wake_lock, timeout * HZ);
364         }
365
366         if (!hym8563->exiting)
367                 enable_irq(hym8563->irq);
368         mutex_unlock(&hym8563->mutex);
369 }
370
371 static const struct rtc_class_ops hym8563_rtc_ops = {
372         .read_time      = hym8563_rtc_read_time,
373         .set_time       = hym8563_rtc_set_time,
374         .read_alarm     = hym8563_rtc_read_alarm,
375         .set_alarm      = hym8563_rtc_set_alarm,
376         .ioctl          = hym8563_rtc_ioctl,
377         .proc           = hym8563_rtc_proc
378 };
379
380 static int __devinit hym8563_probe(struct i2c_client *client, const struct i2c_device_id *id)
381 {
382         int rc = 0;
383         u8 reg = 0;
384         struct hym8563 *hym8563;
385         struct rtc_device *rtc = NULL;
386         struct rtc_time tm_read, tm = {
387                 .tm_wday = 6,
388                 .tm_year = 111,
389                 .tm_mon = 0,
390                 .tm_mday = 1,
391                 .tm_hour = 12,
392                 .tm_min = 0,
393                 .tm_sec = 0,
394         };      
395         
396         if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
397                 return -ENODEV;
398                 
399         hym8563 = kzalloc(sizeof(struct hym8563), GFP_KERNEL);
400         if (!hym8563) {
401                 return -ENOMEM;
402         }
403                 
404         hym8563->client = client;
405         mutex_init(&hym8563->mutex);
406         wake_lock_init(&hym8563->wake_lock, WAKE_LOCK_SUSPEND, "rtc_hym8563");
407         INIT_WORK(&hym8563->work, hym8563_work_func);
408         i2c_set_clientdata(client, hym8563);
409
410         hym8563_init_device(client);
411
412         // check power down 
413         hym8563_i2c_read_regs(client,RTC_SEC,&reg,1);
414         if (reg&0x80) {
415                 dev_info(&client->dev, "clock/calendar information is no longer guaranteed\n");
416                 hym8563_set_time(client, &tm);
417         }
418
419         hym8563_read_datetime(client, &tm_read);        //read time from hym8563
420         
421         if(((tm_read.tm_year < 70) | (tm_read.tm_year > 137 )) | (tm_read.tm_mon == -1) | (rtc_valid_tm(&tm_read) != 0)) //if the hym8563 haven't initialized
422         {
423                 hym8563_set_time(client, &tm);  //initialize the hym8563 
424         }       
425         
426         if(gpio_request(client->irq, "rtc gpio"))
427         {
428                 dev_err(&client->dev, "gpio request fail\n");
429                 gpio_free(client->irq);
430                 goto exit;
431         }
432         
433         hym8563->irq = gpio_to_irq(client->irq);
434         gpio_pull_updown(client->irq,GPIOPullUp);
435         if (request_irq(hym8563->irq, hym8563_wakeup_irq, IRQF_TRIGGER_FALLING, client->dev.driver->name, hym8563) < 0)
436         {
437                 printk("unable to request rtc irq\n");
438                 goto exit;
439         }       
440         enable_irq_wake(hym8563->irq);
441
442         rtc = rtc_device_register(client->name, &client->dev,
443                                   &hym8563_rtc_ops, THIS_MODULE);
444         if (IS_ERR(rtc)) {
445                 rc = PTR_ERR(rtc);
446                 rtc = NULL;
447                 goto exit;
448         }
449         hym8563->rtc = rtc;
450
451         return 0;
452
453 exit:
454         if (rtc)
455                 rtc_device_unregister(rtc);
456         if (hym8563)
457                 kfree(hym8563);
458         return rc;
459 }
460
461 static int __devexit hym8563_remove(struct i2c_client *client)
462 {
463         struct hym8563 *hym8563 = i2c_get_clientdata(client);
464
465         if (hym8563->irq > 0) {
466                 mutex_lock(&hym8563->mutex);
467                 hym8563->exiting = 1;
468                 mutex_unlock(&hym8563->mutex);
469
470                 free_irq(hym8563->irq, hym8563);
471                 cancel_work_sync(&hym8563->work);
472         }
473
474         rtc_device_unregister(hym8563->rtc);
475         wake_lock_destroy(&hym8563->wake_lock);
476         kfree(hym8563);
477         hym8563 = NULL;
478
479         return 0;
480 }
481
482
483 void hym8563_shutdown(struct i2c_client * client)
484 {       u8 regs[2];     
485     int ret;    
486     //disable clkout    
487     regs[0] = 0x00;     
488     ret=hym8563_i2c_set_regs(client, RTC_CLKOUT, regs, 1);      
489     if(ret<0)   
490         printk("rtc shutdown is error\n");
491 }
492
493
494
495 static const struct i2c_device_id hym8563_id[] = {
496         { "rtc_hym8563", 0 },
497         { }
498 };
499 MODULE_DEVICE_TABLE(i2c, hym8563_id);
500
501 static struct i2c_driver hym8563_driver = {
502         .driver         = {
503                 .name   = "rtc_hym8563",
504                 .owner  = THIS_MODULE,
505         },
506         .probe          = hym8563_probe,
507         .remove         = __devexit_p(hym8563_remove),
508         .shutdown=hym8563_shutdown,
509         .id_table       = hym8563_id,
510 };
511
512 static int __init hym8563_init(void)
513 {
514         return i2c_add_driver(&hym8563_driver);
515 }
516
517 static void __exit hym8563_exit(void)
518 {
519         i2c_del_driver(&hym8563_driver);
520 }
521
522 MODULE_AUTHOR("lhh lhh@rock-chips.com");
523 MODULE_DESCRIPTION("HYM8563 RTC driver");
524 MODULE_LICENSE("GPL");
525
526 module_init(hym8563_init);
527 module_exit(hym8563_exit);
528