Merge remote-tracking branch 'stable/linux-3.0.y' into develop-3.0
[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 static struct i2c_client *gClient = NULL;
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 #ifdef CONFIG_HDMI_SAVE_DATA
279 int hdmi_get_data(void)
280 {
281     u8 regs=0;
282     if(gClient)
283         hym8563_i2c_read_regs(gClient, RTC_T_COUNT, &regs, 1);
284     else 
285     {
286         printk("%s rtc has no init\n",__func__);
287         return -1;
288     }
289     if(regs==0 || regs==0xff){
290         printk("%s rtc has no hdmi data\n",__func__);
291         return -1;
292     }
293     return (regs-1);
294 }
295
296 int hdmi_set_data(int data)
297 {
298     u8 regs = (data+1)&0xff;
299     if(gClient)
300         hym8563_i2c_set_regs(gClient, RTC_T_COUNT, &regs, 1);
301     else 
302     {
303         printk("%s rtc has no init\n",__func__);
304         return -1;
305     }   
306     return 0;
307 }
308
309 EXPORT_SYMBOL(hdmi_get_data);
310 EXPORT_SYMBOL(hdmi_set_data);
311 #endif
312 #if defined(CONFIG_RTC_INTF_DEV) || defined(CONFIG_RTC_INTF_DEV_MODULE)
313 static int hym8563_i2c_open_alarm(struct i2c_client *client)
314 {
315         u8 data;
316         hym8563_i2c_read_regs(client, RTC_CTL2, &data, 1);
317         data |= AIE;
318         hym8563_i2c_set_regs(client, RTC_CTL2, &data, 1);
319
320         return 0;
321 }
322
323 static int hym8563_i2c_close_alarm(struct i2c_client *client)
324 {
325         u8 data;
326         hym8563_i2c_read_regs(client, RTC_CTL2, &data, 1);
327         data &= ~AIE;
328         hym8563_i2c_set_regs(client, RTC_CTL2, &data, 1);
329
330         return 0;
331 }
332
333 static int hym8563_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
334 {
335         struct i2c_client *client = to_i2c_client(dev);
336         
337         switch (cmd) {
338         case RTC_AIE_OFF:
339                 if(hym8563_i2c_close_alarm(client) < 0)
340                         goto err;
341                 break;
342         case RTC_AIE_ON:
343                 if(hym8563_i2c_open_alarm(client))
344                         goto err;
345                 break;
346         default:
347                 return -ENOIOCTLCMD;
348         }       
349         return 0;
350 err:
351         return -EIO;
352 }
353 #else
354 #define hym8563_rtc_ioctl NULL
355 #endif
356
357 #if defined(CONFIG_RTC_INTF_PROC) || defined(CONFIG_RTC_INTF_PROC_MODULE)
358 static int hym8563_rtc_proc(struct device *dev, struct seq_file *seq)
359 {
360         return 0;
361 }
362 #else
363 #define hym8563_rtc_proc NULL
364 #endif
365
366 static irqreturn_t hym8563_wakeup_irq(int irq, void *dev_id)
367 {       
368         struct hym8563 *hym8563 = (struct hym8563 *)dev_id;
369         pr_debug("enter\n");
370         disable_irq_nosync(irq);
371         schedule_work(&hym8563->work);
372         return IRQ_HANDLED;
373 }
374
375 static void hym8563_work_func(struct work_struct *work)
376 {       
377         struct hym8563 *hym8563 = container_of(work, struct hym8563, work);
378         struct i2c_client *client = hym8563->client;
379         struct rtc_time now;
380         u8 data;
381
382         pr_debug("enter\n");
383
384         mutex_lock(&hym8563->mutex);
385         hym8563_i2c_read_regs(client, RTC_CTL2, &data, 1);
386         data &= ~AF;
387         hym8563_i2c_set_regs(client, RTC_CTL2, &data, 1);
388         mutex_unlock(&hym8563->mutex);
389
390         hym8563_read_datetime(client, &now);
391
392         mutex_lock(&hym8563->mutex);
393         if (hym8563->alarm.enabled && hym8563->alarm.time.tm_sec > now.tm_sec) {
394                 long timeout = hym8563->alarm.time.tm_sec - now.tm_sec + 1;
395                 pr_info("stay awake %lds\n", timeout);
396                 wake_lock_timeout(&hym8563->wake_lock, timeout * HZ);
397         }
398
399         if (!hym8563->exiting)
400                 enable_irq(hym8563->irq);
401         mutex_unlock(&hym8563->mutex);
402 }
403
404 static const struct rtc_class_ops hym8563_rtc_ops = {
405         .read_time      = hym8563_rtc_read_time,
406         .set_time       = hym8563_rtc_set_time,
407         .read_alarm     = hym8563_rtc_read_alarm,
408         .set_alarm      = hym8563_rtc_set_alarm,
409         .ioctl          = hym8563_rtc_ioctl,
410         .proc           = hym8563_rtc_proc
411 };
412
413 static int __devinit hym8563_probe(struct i2c_client *client, const struct i2c_device_id *id)
414 {
415         int rc = 0;
416         u8 reg = 0;
417         struct hym8563 *hym8563;
418         struct rtc_device *rtc = NULL;
419         struct rtc_time tm_read, tm = {
420                 .tm_wday = 6,
421                 .tm_year = 111,
422                 .tm_mon = 0,
423                 .tm_mday = 1,
424                 .tm_hour = 12,
425                 .tm_min = 0,
426                 .tm_sec = 0,
427         };      
428         
429         if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
430                 return -ENODEV;
431                 
432         hym8563 = kzalloc(sizeof(struct hym8563), GFP_KERNEL);
433         if (!hym8563) {
434                 return -ENOMEM;
435         }
436         gClient = client;       
437         hym8563->client = client;
438         mutex_init(&hym8563->mutex);
439         wake_lock_init(&hym8563->wake_lock, WAKE_LOCK_SUSPEND, "rtc_hym8563");
440         INIT_WORK(&hym8563->work, hym8563_work_func);
441         i2c_set_clientdata(client, hym8563);
442
443         hym8563_init_device(client);
444
445         // check power down 
446         hym8563_i2c_read_regs(client,RTC_SEC,&reg,1);
447         if (reg&0x80) {
448                 dev_info(&client->dev, "clock/calendar information is no longer guaranteed\n");
449                 hym8563_set_time(client, &tm);
450         }
451
452         hym8563_read_datetime(client, &tm_read);        //read time from hym8563
453         
454         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
455         {
456                 hym8563_set_time(client, &tm);  //initialize the hym8563 
457         }       
458         
459         if(gpio_request(client->irq, "rtc gpio"))
460         {
461                 dev_err(&client->dev, "gpio request fail\n");
462                 gpio_free(client->irq);
463                 goto exit;
464         }
465         
466         hym8563->irq = gpio_to_irq(client->irq);
467         gpio_pull_updown(client->irq,GPIOPullUp);
468         if (request_irq(hym8563->irq, hym8563_wakeup_irq, IRQF_TRIGGER_FALLING, client->dev.driver->name, hym8563) < 0)
469         {
470                 printk("unable to request rtc irq\n");
471                 goto exit;
472         }       
473         enable_irq_wake(hym8563->irq);
474
475         rtc = rtc_device_register(client->name, &client->dev,
476                                   &hym8563_rtc_ops, THIS_MODULE);
477         if (IS_ERR(rtc)) {
478                 rc = PTR_ERR(rtc);
479                 rtc = NULL;
480                 goto exit;
481         }
482         hym8563->rtc = rtc;
483
484         return 0;
485
486 exit:
487         if (rtc)
488                 rtc_device_unregister(rtc);
489         if (hym8563)
490                 kfree(hym8563);
491         return rc;
492 }
493
494 static int __devexit hym8563_remove(struct i2c_client *client)
495 {
496         struct hym8563 *hym8563 = i2c_get_clientdata(client);
497
498         if (hym8563->irq > 0) {
499                 mutex_lock(&hym8563->mutex);
500                 hym8563->exiting = 1;
501                 mutex_unlock(&hym8563->mutex);
502
503                 free_irq(hym8563->irq, hym8563);
504                 cancel_work_sync(&hym8563->work);
505         }
506
507         rtc_device_unregister(hym8563->rtc);
508         wake_lock_destroy(&hym8563->wake_lock);
509         kfree(hym8563);
510         hym8563 = NULL;
511
512         return 0;
513 }
514
515
516 void hym8563_shutdown(struct i2c_client * client)
517 {       u8 regs[2];     
518     int ret;    
519     //disable clkout    
520     regs[0] = 0x00;     
521     ret=hym8563_i2c_set_regs(client, RTC_CLKOUT, regs, 1);      
522     if(ret<0)   
523         printk("rtc shutdown is error\n");
524 }
525
526
527
528 static const struct i2c_device_id hym8563_id[] = {
529         { "rtc_hym8563", 0 },
530         { }
531 };
532 MODULE_DEVICE_TABLE(i2c, hym8563_id);
533
534 static struct i2c_driver hym8563_driver = {
535         .driver         = {
536                 .name   = "rtc_hym8563",
537                 .owner  = THIS_MODULE,
538         },
539         .probe          = hym8563_probe,
540         .remove         = __devexit_p(hym8563_remove),
541 #if defined(CONFIG_ARCH_RK3066B)
542         //.shutdown=hym8563_shutdown,
543 #else
544         .shutdown=hym8563_shutdown,
545 #endif
546         .id_table       = hym8563_id,
547 };
548
549 static int __init hym8563_init(void)
550 {
551         return i2c_add_driver(&hym8563_driver);
552 }
553
554 static void __exit hym8563_exit(void)
555 {
556         i2c_del_driver(&hym8563_driver);
557 }
558
559 MODULE_AUTHOR("lhh lhh@rock-chips.com");
560 MODULE_DESCRIPTION("HYM8563 RTC driver");
561 MODULE_LICENSE("GPL");
562
563 module_init(hym8563_init);
564 module_exit(hym8563_exit);
565