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 <mach/gpio.h>
26 #include <mach/iomux.h>
27 #include "rtc-HYM8563.h"
29 #define RTC_SPEED 100 * 1000
33 struct i2c_client *client;
34 struct work_struct work;
36 struct rtc_device *rtc;
38 struct rtc_wkalrm alarm;
39 struct wake_lock wake_lock;
41 static struct i2c_client *gClient = NULL;
42 static int hym8563_i2c_read_regs(struct i2c_client *client, u8 reg, u8 buf[], unsigned len)
45 ret = i2c_master_reg8_recv(client, reg, buf, len, RTC_SPEED);
49 static int hym8563_i2c_set_regs(struct i2c_client *client, u8 reg, u8 const buf[], __u16 len)
52 ret = i2c_master_reg8_send(client, reg, buf, (int)len, RTC_SPEED);
56 /*the init of the hym8563 at first time */
57 static int hym8563_init_device(struct i2c_client *client)
59 struct hym8563 *hym8563 = i2c_get_clientdata(client);
63 mutex_lock(&hym8563->mutex);
65 hym8563_i2c_set_regs(client, RTC_CTL1, regs, 1);
69 hym8563_i2c_set_regs(client, RTC_CLKOUT, regs, 1);
70 /*enable alarm interrupt*/
71 hym8563_i2c_read_regs(client, RTC_CTL2, regs, 1);
74 hym8563_i2c_set_regs(client, RTC_CTL2, regs, 1);
75 hym8563_i2c_read_regs(client, RTC_CTL2, regs, 1);
77 sr = hym8563_i2c_read_regs(client, RTC_CTL2, regs, 1);
79 pr_err("read CTL2 err\n");
85 hym8563_i2c_set_regs(client, RTC_CTL2, regs, 1);
87 mutex_unlock(&hym8563->mutex);
91 static int hym8563_read_datetime(struct i2c_client *client, struct rtc_time *tm)
93 struct hym8563 *hym8563 = i2c_get_clientdata(client);
94 u8 i,regs[HYM8563_RTC_SECTION_LEN] = { 0, };
96 mutex_lock(&hym8563->mutex);
97 // for (i = 0; i < HYM8563_RTC_SECTION_LEN; i++) {
98 // hym8563_i2c_read_regs(client, RTC_SEC+i, ®s[i], 1);
100 hym8563_i2c_read_regs(client, RTC_SEC, regs, HYM8563_RTC_SECTION_LEN);
102 mutex_unlock(&hym8563->mutex);
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);
110 tm->tm_mon = bcd2bin(regs[0x05] & 0x1F) ;
111 tm->tm_mon -= 1; //inorder to cooperate the systerm time
113 tm->tm_year = bcd2bin(regs[0x06] & 0xFF);
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
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);
132 static int hym8563_rtc_read_time(struct device *dev, struct rtc_time *tm)
134 return hym8563_read_datetime(to_i2c_client(dev), tm);
137 static int hym8563_set_time(struct i2c_client *client, struct rtc_time *tm)
139 struct hym8563 *hym8563 = i2c_get_clientdata(client);
140 u8 regs[HYM8563_RTC_SECTION_LEN] = { 0, };
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);
148 mon_day = rtc_month_days((tm->tm_mon), tm->tm_year + 1900);
150 if(tm->tm_sec >= 60 || tm->tm_sec < 0 ) //set sec
151 regs[0x00] = bin2bcd(0x00);
153 regs[0x00] = bin2bcd(tm->tm_sec);
155 if(tm->tm_min >= 60 || tm->tm_min < 0 ) //set min
156 regs[0x01] = bin2bcd(0x00);
158 regs[0x01] = bin2bcd(tm->tm_min);
160 if(tm->tm_hour >= 24 || tm->tm_hour < 0 ) //set hour
161 regs[0x02] = bin2bcd(0x00);
163 regs[0x02] = bin2bcd(tm->tm_hour);
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);
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);
179 }else{ // year < 1900
180 regs[0x06] = bin2bcd(0); //year = 1900
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
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, ®s[i], 1);
190 hym8563_i2c_set_regs(client, RTC_SEC, regs, HYM8563_RTC_SECTION_LEN);
192 mutex_unlock(&hym8563->mutex);
197 static int hym8563_rtc_set_time(struct device *dev, struct rtc_time *tm)
199 return hym8563_set_time(to_i2c_client(dev), tm);
202 static int hym8563_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *tm)
204 struct i2c_client *client = to_i2c_client(dev);
205 struct hym8563 *hym8563 = i2c_get_clientdata(client);
209 mutex_lock(&hym8563->mutex);
210 hym8563_i2c_read_regs(client, RTC_A_MIN, regs, 4);
212 hym8563_i2c_set_regs(client, RTC_CTL2, regs, 1);
213 mutex_unlock(&hym8563->mutex);
217 static int hym8563_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
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;
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);
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);
239 mutex_lock(&hym8563->mutex);
240 hym8563->alarm = *alarm;
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);
247 if (tm->tm_min >= 60 || tm->tm_min < 0) //set min
248 regs[0x00] = bin2bcd(0x00) & 0x7f;
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;
254 regs[0x01] = bin2bcd(tm->tm_hour) & 0x7f;
255 regs[0x03] = bin2bcd (tm->tm_wday) & 0x7f;
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;
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)
272 hym8563_i2c_set_regs(client, RTC_CTL2, regs, 1);
273 hym8563_i2c_read_regs(client, RTC_CTL2, regs, 1);
275 mutex_unlock(&hym8563->mutex);
278 #ifdef CONFIG_HDMI_SAVE_DATA
279 int hdmi_get_data(void)
283 hym8563_i2c_read_regs(gClient, RTC_T_COUNT, ®s, 1);
286 printk("%s rtc has no init\n",__func__);
289 if(regs==0 || regs==0xff){
290 printk("%s rtc has no hdmi data\n",__func__);
296 int hdmi_set_data(int data)
298 u8 regs = (data+1)&0xff;
300 hym8563_i2c_set_regs(gClient, RTC_T_COUNT, ®s, 1);
303 printk("%s rtc has no init\n",__func__);
309 EXPORT_SYMBOL(hdmi_get_data);
310 EXPORT_SYMBOL(hdmi_set_data);
312 #if defined(CONFIG_RTC_INTF_DEV) || defined(CONFIG_RTC_INTF_DEV_MODULE)
313 static int hym8563_i2c_open_alarm(struct i2c_client *client)
316 hym8563_i2c_read_regs(client, RTC_CTL2, &data, 1);
318 hym8563_i2c_set_regs(client, RTC_CTL2, &data, 1);
323 static int hym8563_i2c_close_alarm(struct i2c_client *client)
326 hym8563_i2c_read_regs(client, RTC_CTL2, &data, 1);
328 hym8563_i2c_set_regs(client, RTC_CTL2, &data, 1);
333 static int hym8563_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
335 struct i2c_client *client = to_i2c_client(dev);
339 if(hym8563_i2c_close_alarm(client) < 0)
343 if(hym8563_i2c_open_alarm(client))
354 #define hym8563_rtc_ioctl NULL
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)
363 #define hym8563_rtc_proc NULL
366 static irqreturn_t hym8563_wakeup_irq(int irq, void *dev_id)
368 struct hym8563 *hym8563 = (struct hym8563 *)dev_id;
370 disable_irq_nosync(irq);
371 schedule_work(&hym8563->work);
375 static void hym8563_work_func(struct work_struct *work)
377 struct hym8563 *hym8563 = container_of(work, struct hym8563, work);
378 struct i2c_client *client = hym8563->client;
384 mutex_lock(&hym8563->mutex);
385 hym8563_i2c_read_regs(client, RTC_CTL2, &data, 1);
387 hym8563_i2c_set_regs(client, RTC_CTL2, &data, 1);
388 mutex_unlock(&hym8563->mutex);
390 hym8563_read_datetime(client, &now);
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);
399 if (!hym8563->exiting)
400 enable_irq(hym8563->irq);
401 mutex_unlock(&hym8563->mutex);
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
413 static int __devinit hym8563_probe(struct i2c_client *client, const struct i2c_device_id *id)
417 struct hym8563 *hym8563;
418 struct rtc_device *rtc = NULL;
419 struct rtc_time tm_read, tm = {
429 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
432 hym8563 = kzalloc(sizeof(struct hym8563), GFP_KERNEL);
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);
443 hym8563_init_device(client);
446 hym8563_i2c_read_regs(client,RTC_SEC,®,1);
448 dev_info(&client->dev, "clock/calendar information is no longer guaranteed\n");
449 hym8563_set_time(client, &tm);
452 hym8563_read_datetime(client, &tm_read); //read time from hym8563
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
456 hym8563_set_time(client, &tm); //initialize the hym8563
459 if(gpio_request(client->irq, "rtc gpio"))
461 dev_err(&client->dev, "gpio request fail\n");
462 gpio_free(client->irq);
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)
470 printk("unable to request rtc irq\n");
473 enable_irq_wake(hym8563->irq);
475 rtc = rtc_device_register(client->name, &client->dev,
476 &hym8563_rtc_ops, THIS_MODULE);
488 rtc_device_unregister(rtc);
494 static int __devexit hym8563_remove(struct i2c_client *client)
496 struct hym8563 *hym8563 = i2c_get_clientdata(client);
498 if (hym8563->irq > 0) {
499 mutex_lock(&hym8563->mutex);
500 hym8563->exiting = 1;
501 mutex_unlock(&hym8563->mutex);
503 free_irq(hym8563->irq, hym8563);
504 cancel_work_sync(&hym8563->work);
507 rtc_device_unregister(hym8563->rtc);
508 wake_lock_destroy(&hym8563->wake_lock);
516 void hym8563_shutdown(struct i2c_client * client)
521 ret=hym8563_i2c_set_regs(client, RTC_CLKOUT, regs, 1);
523 printk("rtc shutdown is error\n");
528 static const struct i2c_device_id hym8563_id[] = {
529 { "rtc_hym8563", 0 },
532 MODULE_DEVICE_TABLE(i2c, hym8563_id);
534 static struct i2c_driver hym8563_driver = {
536 .name = "rtc_hym8563",
537 .owner = THIS_MODULE,
539 .probe = hym8563_probe,
540 .remove = __devexit_p(hym8563_remove),
541 #if defined(CONFIG_ARCH_RK3066B)
542 //.shutdown=hym8563_shutdown,
544 .shutdown=hym8563_shutdown,
546 .id_table = hym8563_id,
549 static int __init hym8563_init(void)
551 return i2c_add_driver(&hym8563_driver);
554 static void __exit hym8563_exit(void)
556 i2c_del_driver(&hym8563_driver);
559 MODULE_AUTHOR("lhh lhh@rock-chips.com");
560 MODULE_DESCRIPTION("HYM8563 RTC driver");
561 MODULE_LICENSE("GPL");
563 module_init(hym8563_init);
564 module_exit(hym8563_exit);