1 /* drivers/rtc/rtc-HYM8563.c - driver for HYM8563
3 * Copyright (C) 2010 ROCKCHIP, Inc.
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.
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.
16 #define pr_fmt(fmt) "rtc: %s: " fmt, __func__
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 "rtc-HYM8563.h"
26 #include <linux/of_gpio.h>
27 #include <linux/irqdomain.h>
28 #define RTC_SPEED 200 * 1000
32 struct i2c_client *client;
34 struct rtc_device *rtc;
35 struct rtc_wkalrm alarm;
36 struct wake_lock wake_lock;
38 static struct i2c_client *gClient = NULL;
40 static int i2c_master_reg8_send(const struct i2c_client *client, const char reg, const char *buf, int count, int scl_rate)
42 struct i2c_adapter *adap=client->adapter;
45 char *tx_buf = (char *)kzalloc(count + 1, GFP_KERNEL);
49 memcpy(tx_buf+1, buf, count);
51 msg.addr = client->addr;
52 msg.flags = client->flags;
54 msg.buf = (char *)tx_buf;
55 msg.scl_rate = scl_rate;
57 ret = i2c_transfer(adap, &msg, 1);
59 return (ret == 1) ? count : ret;
63 static int i2c_master_reg8_recv(const struct i2c_client *client, const char reg, char *buf, int count, int scl_rate)
65 struct i2c_adapter *adap=client->adapter;
66 struct i2c_msg msgs[2];
70 msgs[0].addr = client->addr;
71 msgs[0].flags = client->flags;
73 msgs[0].buf = ®_buf;
74 msgs[0].scl_rate = scl_rate;
76 msgs[1].addr = client->addr;
77 msgs[1].flags = client->flags | I2C_M_RD;
79 msgs[1].buf = (char *)buf;
80 msgs[1].scl_rate = scl_rate;
82 ret = i2c_transfer(adap, msgs, 2);
84 return (ret == 2)? count : ret;
89 static int hym8563_i2c_read_regs(struct i2c_client *client, u8 reg, u8 buf[], unsigned len)
92 ret = i2c_master_reg8_recv(client, reg, buf, len, RTC_SPEED);
96 static int hym8563_i2c_set_regs(struct i2c_client *client, u8 reg, u8 const buf[], __u16 len)
99 ret = i2c_master_reg8_send(client, reg, buf, (int)len, RTC_SPEED);
104 int hym8563_enable_count(struct i2c_client *client, int en)
106 struct hym8563 *hym8563 = i2c_get_clientdata(client);
113 hym8563_i2c_read_regs(client, RTC_CTL2, regs, 1);
115 hym8563_i2c_set_regs(client, RTC_CTL2, regs, 1);
117 regs[0] |= (TE | TD1);
118 hym8563_i2c_set_regs(client, RTC_T_CTL, regs, 1);
121 hym8563_i2c_read_regs(client, RTC_CTL2, regs, 1);
123 hym8563_i2c_set_regs(client, RTC_CTL2, regs, 1);
125 regs[0] |= (TD0 | TD1);
126 hym8563_i2c_set_regs(client, RTC_T_CTL, regs, 1);
132 int hym8563_set_count(struct i2c_client *client, int sec)
134 struct hym8563 *hym8563 = i2c_get_clientdata(client);
147 hym8563_i2c_set_regs(client, RTC_T_COUNT, regs, 1);
153 /*the init of the hym8563 at first time */
154 static int hym8563_init_device(struct i2c_client *client)
156 struct hym8563 *hym8563 = i2c_get_clientdata(client);
160 mutex_lock(&hym8563->mutex);
162 sr = hym8563_i2c_set_regs(client, RTC_CTL1, regs, 1);
168 sr = hym8563_i2c_set_regs(client, RTC_CLKOUT, regs, 1);
172 /*enable alarm && count interrupt*/
173 sr = hym8563_i2c_read_regs(client, RTC_CTL2, regs, 1);
177 regs[0] |= (AIE | TIE);
178 sr = hym8563_i2c_set_regs(client, RTC_CTL2, regs, 1);
181 sr = hym8563_i2c_read_regs(client, RTC_CTL2, regs, 1);
185 sr = hym8563_i2c_read_regs(client, RTC_CTL2, regs, 1);
187 pr_err("read CTL2 err\n");
191 if(regs[0] & (AF|TF))
194 sr = hym8563_i2c_set_regs(client, RTC_CTL2, regs, 1);
198 mutex_unlock(&hym8563->mutex);
203 static int hym8563_read_datetime(struct i2c_client *client, struct rtc_time *tm)
205 struct hym8563 *hym8563 = i2c_get_clientdata(client);
206 u8 regs[HYM8563_RTC_SECTION_LEN] = { 0, };
207 mutex_lock(&hym8563->mutex);
208 // for (i = 0; i < HYM8563_RTC_SECTION_LEN; i++) {
209 // hym8563_i2c_read_regs(client, RTC_SEC+i, ®s[i], 1);
211 hym8563_i2c_read_regs(client, RTC_SEC, regs, HYM8563_RTC_SECTION_LEN);
213 mutex_unlock(&hym8563->mutex);
215 tm->tm_sec = bcd2bin(regs[0x00] & 0x7F);
216 tm->tm_min = bcd2bin(regs[0x01] & 0x7F);
217 tm->tm_hour = bcd2bin(regs[0x02] & 0x3F);
218 tm->tm_mday = bcd2bin(regs[0x03] & 0x3F);
219 tm->tm_wday = bcd2bin(regs[0x04] & 0x07);
221 tm->tm_mon = bcd2bin(regs[0x05] & 0x1F) ;
222 tm->tm_mon -= 1; //inorder to cooperate the systerm time
224 tm->tm_year = bcd2bin(regs[0x06] & 0xFF);
230 tm->tm_year -= 1900; //inorder to cooperate the systerm time
235 pr_debug("%4d-%02d-%02d(%d) %02d:%02d:%02d\n",
236 1900 + tm->tm_year, tm->tm_mon + 1, tm->tm_mday, tm->tm_wday,
237 tm->tm_hour, tm->tm_min, tm->tm_sec);
242 static int hym8563_rtc_read_time(struct device *dev, struct rtc_time *tm)
244 return hym8563_read_datetime(to_i2c_client(dev), tm);
247 static int hym8563_set_time(struct i2c_client *client, struct rtc_time *tm)
249 struct hym8563 *hym8563 = i2c_get_clientdata(client);
250 u8 regs[HYM8563_RTC_SECTION_LEN] = { 0, };
254 pr_debug("%4d-%02d-%02d(%d) %02d:%02d:%02d\n",
255 1900 + tm->tm_year, tm->tm_mon + 1, tm->tm_mday, tm->tm_wday,
256 tm->tm_hour, tm->tm_min, tm->tm_sec);
258 mon_day = rtc_month_days((tm->tm_mon), tm->tm_year + 1900);
260 if(tm->tm_sec >= 60 || tm->tm_sec < 0 ) //set sec
261 regs[0x00] = bin2bcd(0x00);
263 regs[0x00] = bin2bcd(tm->tm_sec);
265 if(tm->tm_min >= 60 || tm->tm_min < 0 ) //set min
266 regs[0x01] = bin2bcd(0x00);
268 regs[0x01] = bin2bcd(tm->tm_min);
270 if(tm->tm_hour >= 24 || tm->tm_hour < 0 ) //set hour
271 regs[0x02] = bin2bcd(0x00);
273 regs[0x02] = bin2bcd(tm->tm_hour);
275 if((tm->tm_mday) > mon_day) //if the input month day is bigger than the biggest day of this month, set the biggest day
276 regs[0x03] = bin2bcd(mon_day);
277 else if((tm->tm_mday) > 0)
278 regs[0x03] = bin2bcd(tm->tm_mday);
279 else if((tm->tm_mday) <= 0)
280 regs[0x03] = bin2bcd(0x01);
282 if( tm->tm_year >= 200) // year >= 2100
283 regs[0x06] = bin2bcd(99); //year = 2099
284 else if(tm->tm_year >= 100) // 2000 <= year < 2100
285 regs[0x06] = bin2bcd(tm->tm_year - 100);
286 else if(tm->tm_year >= 0){ // 1900 <= year < 2000
287 regs[0x06] = bin2bcd(tm->tm_year);
289 }else{ // year < 1900
290 regs[0x06] = bin2bcd(0); //year = 1900
293 regs[0x04] = bin2bcd(tm->tm_wday); //set the weekday
294 regs[0x05] = (regs[0x05] & 0x80)| (bin2bcd(tm->tm_mon + 1) & 0x7F); //set the month
296 mutex_lock(&hym8563->mutex);
297 // for(i=0;i<HYM8563_RTC_SECTION_LEN;i++){
298 // ret = hym8563_i2c_set_regs(client, RTC_SEC+i, ®s[i], 1);
300 hym8563_i2c_set_regs(client, RTC_SEC, regs, HYM8563_RTC_SECTION_LEN);
302 mutex_unlock(&hym8563->mutex);
307 static int hym8563_rtc_set_time(struct device *dev, struct rtc_time *tm)
309 return hym8563_set_time(to_i2c_client(dev), tm);
312 static int hym8563_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *tm)
314 struct i2c_client *client = to_i2c_client(dev);
315 struct hym8563 *hym8563 = i2c_get_clientdata(client);
319 mutex_lock(&hym8563->mutex);
320 hym8563_i2c_read_regs(client, RTC_A_MIN, regs, 4);
323 hym8563_i2c_set_regs(client, RTC_CTL2, regs, 1);
324 mutex_unlock(&hym8563->mutex);
328 static int hym8563_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
330 struct i2c_client *client = to_i2c_client(dev);
331 struct hym8563 *hym8563 = i2c_get_clientdata(client);
332 struct rtc_time now, *tm = &alarm->time;
335 unsigned long alarm_sec, now_sec;
338 pr_debug("%4d-%02d-%02d(%d) %02d:%02d:%02d enabled %d\n",
339 1900 + tm->tm_year, tm->tm_mon + 1, tm->tm_mday, tm->tm_wday,
340 tm->tm_hour, tm->tm_min, tm->tm_sec, alarm->enabled);
343 hym8563_read_datetime(client, &now);
346 mutex_lock(&hym8563->mutex);
347 rtc_tm_to_time(tm, &alarm_sec);
348 rtc_tm_to_time(&now, &now_sec);
350 diff_sec = alarm_sec - now_sec;
352 if((diff_sec > 0) && (diff_sec < 256))
354 printk("%s:diff_sec= %ds , use time\n",__func__, diff_sec);
356 if (alarm->enabled == 1)
358 hym8563_set_count(client, diff_sec);
359 hym8563_enable_count(client, 1);
364 hym8563_enable_count(client, 0);
370 printk("%s:diff_sec= %ds , use alarm\n",__func__, diff_sec);
371 hym8563_enable_count(client, 0);
375 rtc_tm_to_time(tm, &alarm_sec);
376 rtc_time_to_tm(alarm_sec, tm);
379 hym8563->alarm = *alarm;
382 hym8563_i2c_set_regs(client, RTC_CTL2, regs, 1);
383 mon_day = rtc_month_days(tm->tm_mon, tm->tm_year + 1900);
384 hym8563_i2c_read_regs(client, RTC_A_MIN, regs, 4);
386 if (tm->tm_min >= 60 || tm->tm_min < 0) //set min
387 regs[0x00] = bin2bcd(0x00) & 0x7f;
389 regs[0x00] = bin2bcd(tm->tm_min) & 0x7f;
390 if (tm->tm_hour >= 24 || tm->tm_hour < 0) //set hour
391 regs[0x01] = bin2bcd(0x00) & 0x7f;
393 regs[0x01] = bin2bcd(tm->tm_hour) & 0x7f;
394 regs[0x03] = bin2bcd (tm->tm_wday) & 0x7f;
396 /* if the input month day is bigger than the biggest day of this month, set the biggest day */
397 if (tm->tm_mday > mon_day)
398 regs[0x02] = bin2bcd(mon_day) & 0x7f;
399 else if (tm->tm_mday > 0)
400 regs[0x02] = bin2bcd(tm->tm_mday) & 0x7f;
401 else if (tm->tm_mday <= 0)
402 regs[0x02] = bin2bcd(0x01) & 0x7f;
404 hym8563_i2c_set_regs(client, RTC_A_MIN, regs, 4);
405 hym8563_i2c_read_regs(client, RTC_A_MIN, regs, 4);
406 hym8563_i2c_read_regs(client, RTC_CTL2, regs, 1);
407 if (alarm->enabled == 1)
411 hym8563_i2c_set_regs(client, RTC_CTL2, regs, 1);
412 hym8563_i2c_read_regs(client, RTC_CTL2, regs, 1);
416 pr_info("alarm sec <= now sec\n");
421 mutex_unlock(&hym8563->mutex);
425 #ifdef CONFIG_HDMI_SAVE_DATA
426 int hdmi_get_data(void)
430 hym8563_i2c_read_regs(gClient, RTC_T_COUNT, ®s, 1);
433 printk("%s rtc has no init\n",__func__);
436 if(regs==0 || regs==0xff){
437 printk("%s rtc has no hdmi data\n",__func__);
443 int hdmi_set_data(int data)
445 u8 regs = (data+1)&0xff;
447 hym8563_i2c_set_regs(gClient, RTC_T_COUNT, ®s, 1);
450 printk("%s rtc has no init\n",__func__);
456 EXPORT_SYMBOL(hdmi_get_data);
457 EXPORT_SYMBOL(hdmi_set_data);
459 #if defined(CONFIG_RTC_INTF_DEV) || defined(CONFIG_RTC_INTF_DEV_MODULE)
460 static int hym8563_i2c_open_alarm(struct i2c_client *client)
463 hym8563_i2c_read_regs(client, RTC_CTL2, &data, 1);
465 hym8563_i2c_set_regs(client, RTC_CTL2, &data, 1);
470 static int hym8563_i2c_close_alarm(struct i2c_client *client)
473 hym8563_i2c_read_regs(client, RTC_CTL2, &data, 1);
475 hym8563_i2c_set_regs(client, RTC_CTL2, &data, 1);
480 static int hym8563_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
482 struct i2c_client *client = to_i2c_client(dev);
486 if(hym8563_i2c_close_alarm(client) < 0)
490 if(hym8563_i2c_open_alarm(client))
501 #define hym8563_rtc_ioctl NULL
504 #if defined(CONFIG_RTC_INTF_PROC) || defined(CONFIG_RTC_INTF_PROC_MODULE)
505 static int hym8563_rtc_proc(struct device *dev, struct seq_file *seq)
510 #define hym8563_rtc_proc NULL
513 static irqreturn_t hym8563_wakeup_irq(int irq, void *data)
515 struct hym8563 *hym8563 = data;
516 struct i2c_client *client = hym8563->client;
519 mutex_lock(&hym8563->mutex);
520 hym8563_i2c_read_regs(client, RTC_CTL2, &value, 1);
522 hym8563_i2c_set_regs(client, RTC_CTL2, &value, 1);
523 mutex_unlock(&hym8563->mutex);
525 rtc_update_irq(hym8563->rtc, 1, RTC_IRQF | RTC_AF | RTC_UF);
527 //printk("%s:irq=%d\n",__func__,irq);
531 static const struct rtc_class_ops hym8563_rtc_ops = {
532 .read_time = hym8563_rtc_read_time,
533 .set_time = hym8563_rtc_set_time,
534 .read_alarm = hym8563_rtc_read_alarm,
535 .set_alarm = hym8563_rtc_set_alarm,
536 .ioctl = hym8563_rtc_ioctl,
537 .proc = hym8563_rtc_proc
540 static int hym8563_probe(struct i2c_client *client, const struct i2c_device_id *id)
544 struct hym8563 *hym8563;
545 struct rtc_device *rtc = NULL;
546 struct rtc_time tm_read, tm = {
556 struct device_node *np = client->dev.of_node;
557 unsigned long irq_flags;
560 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
563 hym8563 = devm_kzalloc(&client->dev,sizeof(*hym8563), GFP_KERNEL);
569 hym8563->client = client;
570 hym8563->alarm.enabled = 0;
572 mutex_init(&hym8563->mutex);
573 wake_lock_init(&hym8563->wake_lock, WAKE_LOCK_SUSPEND, "rtc_hym8563");
574 i2c_set_clientdata(client, hym8563);
576 hym8563_init_device(client);
577 hym8563_enable_count(client, 0);
580 hym8563_i2c_read_regs(client,RTC_SEC,®,1);
582 dev_info(&client->dev, "clock/calendar information is no longer guaranteed\n");
583 hym8563_set_time(client, &tm);
586 hym8563_read_datetime(client, &tm_read); //read time from hym8563
588 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
590 hym8563_set_time(client, &tm); //initialize the hym8563
593 client->irq = of_get_named_gpio_flags(np, "irq_gpio", 0,(enum of_gpio_flags *)&irq_flags);
596 hym8563->irq = gpio_to_irq(client->irq);
597 result = devm_request_threaded_irq(&client->dev, hym8563->irq, NULL, hym8563_wakeup_irq, irq_flags | IRQF_ONESHOT, client->dev.driver->name,hym8563 );
599 printk(KERN_ERR "%s:fail to request irq = %d, ret = 0x%x\n",__func__, hym8563->irq, result);
602 enable_irq_wake(hym8563->irq);
603 device_init_wakeup(&client->dev, 1);
605 rtc = devm_rtc_device_register(&client->dev,
607 &hym8563_rtc_ops, THIS_MODULE);
619 wake_lock_destroy(&hym8563->wake_lock);
624 static int hym8563_remove(struct i2c_client *client)
626 struct hym8563 *hym8563 = i2c_get_clientdata(client);
628 wake_lock_destroy(&hym8563->wake_lock);
634 void hym8563_shutdown(struct i2c_client * client)
639 ret=hym8563_i2c_set_regs(client, RTC_CLKOUT, regs, 1);
641 printk("rtc shutdown is error\n");
646 static const struct i2c_device_id hym8563_id[] = {
647 { "rtc_hym8563", 0 },
650 MODULE_DEVICE_TABLE(i2c, hym8563_id);
652 static struct of_device_id rtc_dt_ids[] = {
653 { .compatible = "rtc,hym8563" },
657 struct i2c_driver hym8563_driver = {
659 .name = "rtc_hym8563",
660 .owner = THIS_MODULE,
661 .of_match_table = of_match_ptr(rtc_dt_ids),
663 .probe = hym8563_probe,
664 .remove = hym8563_remove,
665 //.shutdown=hym8563_shutdown,
666 .id_table = hym8563_id,
669 static int __init hym8563_init(void)
671 return i2c_add_driver(&hym8563_driver);
674 static void __exit hym8563_exit(void)
676 i2c_del_driver(&hym8563_driver);
679 MODULE_AUTHOR("lhh lhh@rock-chips.com");
680 MODULE_DESCRIPTION("HYM8563 RTC driver");
681 MODULE_LICENSE("GPL");
683 module_init(hym8563_init);
684 module_exit(hym8563_exit);