blob: ddfa78004f816b3878960503da7c15fec87fdf9c [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/* drivers/rtc/zx234290_rtc.c
2 *
3 * Copyright (c) 2013 Sanechips Co., Ltd.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * zx234290 Internal RTC Driver
10*/
11
12#include <linux/module.h>
13#include <linux/kernel.h>
14#include <linux/fs.h>
15#include <linux/string.h>
16#include <linux/init.h>
17#include <linux/platform_device.h>
18#include <linux/interrupt.h>
19#include <linux/rtc.h>
20#include <linux/bcd.h>
21#include <linux/clk.h>
22#include <linux/log2.h>
23#include <linux/of.h>
24#include <linux/delay.h>
25#include <linux/irq.h>
26#include <linux/mfd/zx234290.h>
27#include <asm/irq.h>
28#include "mach/iomap.h"
29#include <linux/gpio.h>
30#include <mach/gpio.h>
31#include "zx234290-rtc.h"
32
33#include <linux/semaphore.h>
34
35//#define GPIOFUNC_GPIO 0
36//#define GPIOFUNC_FUNC 1
37//#define PIN_PMU_INT ZX29_GPIO_47
38static struct platform_device *g_rtcdev;
39
40static int zx234290_rtc_enable(struct device *dev, int enable);
41int zx234290_rtc_settimer(int sec);
42int zx234290_rtc_set_second_timer(int seconds);
43
44#define ZX234290_BITFVAL(var, lsh) ( (var) << (lsh) )
45#define ZX234290_BITFMASK(wid, lsh) ( ((1U << (wid)) - 1) << (lsh) )
46#define ZX234290_BITFEXT(var, wid, lsh) ((var & ZX234290_BITFMASK(wid, lsh)) >> (lsh))
47static struct rtc_device *g_rtc;
48int CycleTimes = 0;
49int surplus = 0;
50int timer_remain=0;
51//#define ZX234290_GPIO_NUM 10
52
53typedef enum
54{
55 TIMER_COUNT_4096 = 0,
56 TIMER_COUNT_64 = 1, /* 64 DEFAULT */
57 TIMER_COUNT_1 = 2, /* 1 */
58 TIMER_COUNT_1_60 = 3, /* 1/60 */
59
60 TIMER_COUNT_MAX
61}zx234290_timercount;
62
63typedef enum
64{
65 ALARM_MINUTE = 0,
66 ALARM_HOUR = 1,
67 ALARM_DAY = 2,
68 ALARM_WEEKDAY = 3,
69 ALARM_SECOND = 4,
70 ALARM_MAX
71}zx234290_alarm_type;
72
73typedef enum
74{
75 ZX234290_SET_TIMER =( 'r'<<24|'t'<<16|'c'<<8|0),
76 ZX234290_GET_TIMER,
77 ZX234290_TIMER_ENABLED,
78 ZX234290_GET_TIMER_REMAIN,
79 ZX234290_GET_TIMER_STATUS,
80 ZX234290_CYCLE_ENABLE,
81 ZX234290_FUNCTION_MAX
82
83}zx234290_rtc_timer;
84
85typedef struct _zx234290_rtc
86{
87 struct zx234290 *zx234290;
88}zx234290_rtc_data;
89
90zx234290_rtc_data zx234290_rtc;
91static int zx234290_rtc_irqno = NO_IRQ;
92
93static volatile long timer_sema_flag=0;
94#define TIMER_SEMA_START_BIT 0 /*set timer before get timer.*/
95#define TIMER_SEMA_WAIT_BIT 1 /*mutex semaphore, up semaphore must be after down semaphore.*/
96
97struct semaphore timerSemaphore;
98
99
100struct rtc_time tempTime = {0};
101
102int zx234290_rtc_gettime(struct device *dev, struct rtc_time *rtc_tm);
103static int zx234290_rtc_getalarm(struct device *dev, struct rtc_wkalrm *alrm);
104
105static int zx234290_rtc_write_register(unsigned char addr, unsigned char data, unsigned char mask)
106{
107 int ret = 0;
108 unsigned char content = 0;
109
110 ret = zx234290_reg_read(zx234290_rtc.zx234290, addr);
111 if (ret < 0)
112 {
113 return -1;
114 }
115
116 content = (unsigned char)ret;
117 content &= ~mask;
118 content |= data & mask;
119 ret = zx234290_reg_write(zx234290_rtc.zx234290, addr, content);
120 if (ret != 0)
121 {
122 return ret;
123 }
124
125 return ret;
126}
127
128static int zx234290_rtc_read_register(unsigned char reg, unsigned char *dest)
129{
130 int ret;
131
132 ret = zx234290_reg_read(zx234290_rtc.zx234290, reg);
133 if (ret < 0)
134 return ret;
135
136 *dest = (unsigned char)ret;
137
138 return 0;
139}
140
141//++added by ys
142//send alarm irq event to app when alarm come.
143void zte_send_alarm_irq_event(void)
144{
145 char event_string[100] = {0};
146 char *envp[] = { event_string, NULL };
147
148 struct rtc_time rtc_tm;
149 struct rtc_wkalrm alrm;
150 unsigned long sec;
151// time_t sec;
152
153 zx234290_rtc_gettime(NULL, &rtc_tm);
154 zx234290_rtc_getalarm(NULL, &alrm);
155 alrm.time.tm_year = rtc_tm.tm_year;
156 alrm.time.tm_mon = rtc_tm.tm_mon;
157 rtc_tm_to_time(&(alrm.time), &sec);
158/*
159 printk("read alarm %04d.%02d.%02d %02d:%02d:%02d wday %d, sec %lu\n",
160 alrm.time.tm_year, alrm.time.tm_mon, alrm.time.tm_mday,
161 alrm.time.tm_hour, alrm.time.tm_min, alrm.time.tm_sec, alrm.time.tm_wday, sec);
162*/
163
164 sprintf(event_string,"PMIC RTC ALARM IRQ COME, sec:%lu", sec);
165
166 if(strlen(event_string))
167 kobject_uevent_env(&g_rtcdev->dev.kobj, KOBJ_CHANGE,envp);
168}
169
170void zte_send_timer_irq_event(void)
171{
172 char event_string[100] = {0};
173 char *envp[] = { event_string, NULL };
174// printk(KERN_INFO "zx234290_rtc: send_timer_irq_event\n" );
175
176 sprintf(event_string,"PMIC RTC TIMER IRQ COME");
177
178 if(strlen(event_string))
179 kobject_uevent_env(&g_rtcdev->dev.kobj, KOBJ_CHANGE,envp);
180
181 //printk(KERN_INFO "[yuwei]send_timer_irq_event. uevent\n");
182}
183void zte_send_min_irq_event(void)
184{
185 char event_string[100] = {0};
186 char *envp[] = { event_string, NULL };
187// printk(KERN_INFO "zx234290_rtc: send_min_irq_event\n" );
188
189 sprintf(event_string,"PMIC RTC MIN IRQ COME");
190
191 if(strlen(event_string))
192 kobject_uevent_env(&g_rtcdev->dev.kobj, KOBJ_CHANGE,envp);
193
194}
195void zte_send_hour_irq_event(void)
196{
197 char event_string[100] = {0};
198 char *envp[] = { event_string, NULL };
199// printk(KERN_INFO "zx234290_rtc: send_hour_irq_event\n" );
200
201 sprintf(event_string,"PMIC RTC HOUR IRQ COME");
202
203 if(strlen(event_string))
204 kobject_uevent_env(&g_rtcdev->dev.kobj, KOBJ_CHANGE,envp);
205
206}
207//++end
208
209
210
211/*alarm and timer countdown irq*/
212static irqreturn_t zx234290_rtc_alarmirq(int irq, void *id)
213{
214 struct rtc_device *rdev = id;
215 unsigned char reg_val = 0, mask = 0;
216 unsigned char reg_val_ctrl2 = 0;
217
218 zx234290_rtc_read_register(ZX234290_REG_ADDR_RTC_CTRL2,&reg_val_ctrl2);
219 //printk(KERN_INFO "zx234290_rtc_alarmirq:CycleTimes=%d,surplus=%d,timerFlag=%d.\n",CycleTimes,surplus,timerFlag);
220 //printk(KERN_INFO "zx234290_rtc_alarmirq:value(0x31) =%d.\n",reg_val_ctrl2);
221
222 /*alarm int*/
223 if((reg_val_ctrl2&0x08) == 0x08)
224 //if((reg_val_ctrl2>>ZX234290_RTC_AF_LSH)&0x1 == 0x1)
225 {
226 /*clear AF bit*/
227 mask = ZX234290_BITFMASK(ZX234290_RTC_AF_WID, ZX234290_RTC_AF_LSH);
228 reg_val= ZX234290_BITFVAL(0, ZX234290_RTC_AF_LSH);
229 zx234290_rtc_write_register(ZX234290_REG_ADDR_RTC_CTRL2, reg_val, mask);
230
231 rtc_update_irq(rdev, 1, RTC_AF | RTC_IRQF);
232
233 zte_send_alarm_irq_event();
234 }
235 /*timer int*/
236 if((reg_val_ctrl2&0x04) == 0x04)
237 //else if((reg_val_ctrl2>>ZX234290_RTC_TF_LSH)&0x1 == 0x1)
238 {
239 /*clear TF bit*/
240 mask = ZX234290_BITFMASK(ZX234290_RTC_TF_WID, ZX234290_RTC_TF_LSH);
241 reg_val= ZX234290_BITFVAL(0, ZX234290_RTC_TF_LSH);
242 zx234290_rtc_write_register(ZX234290_REG_ADDR_RTC_CTRL2, reg_val, mask);
243
244 rtc_update_irq(rdev, 1, RTC_AF | RTC_IRQF);
245
246// printk(KERN_INFO "zx234290_rtc_alarmirq ,remain seconds=%ld,flag=0x%lx\n",timer_remain,timer_sema_flag);
247 if(timer_remain){
248 zx234290_rtc_set_second_timer(min(timer_remain,255));
249 timer_remain -= min(timer_remain,255);
250 }
251 else{
252 if(test_and_clear_bit(TIMER_SEMA_WAIT_BIT, &timer_sema_flag)){
253 up(&timerSemaphore);
254 printk(KERN_INFO "zx234290_rtc_alarmirq semaphore.\n");
255 }
256 zte_send_timer_irq_event();
257 clear_bit(TIMER_SEMA_START_BIT, &timer_sema_flag);
258 }
259
260 }
261
262// printk(KERN_INFO "zx234290_rtc: irq = %d, REG(0x31) =0x%x.\n", irq, reg_val_ctrl2);
263 return IRQ_HANDLED;
264}
265
266static irqreturn_t zx234290_rtc_minirq(int irq, void *id)
267{
268 struct rtc_device *rdev = id;
269
270 rtc_update_irq(rdev, 1, RTC_UF | RTC_IRQF);
271 zte_send_min_irq_event();
272 return IRQ_HANDLED;
273}
274static irqreturn_t zx234290_rtc_hourirq(int irq, void *id)
275{
276 struct rtc_device *rdev = id;
277
278 rtc_update_irq(rdev, 1, RTC_UF | RTC_IRQF);
279 zte_send_hour_irq_event();
280 return IRQ_HANDLED;
281}
282/*alarm enable/disable, 0:disable 1:enable*/
283int zx234290_rtc_alarm_enable(zx234290_alarm_type type, unsigned int enabled)
284{
285 int ret = 0;
286 unsigned char reg_addr=0, reg_val=0, mask=0;
287 switch(type)
288 {
289 case ALARM_MINUTE:
290 reg_addr = ZX234290_REG_ADDR_ALARM_MINUTE;
291 break;
292 case ALARM_HOUR:
293 reg_addr = ZX234290_REG_ADDR_ALARM_HOUR;
294 break;
295 case ALARM_DAY:
296 reg_addr = ZX234290_REG_ADDR_ALARM_DAY;
297 break;
298 case ALARM_WEEKDAY:
299 reg_addr = ZX234290_REG_ADDR_ALARM_WEEK;
300 break;
301 case ALARM_SECOND:
302 reg_addr = ZX234290_REG_ADDR_ALARM_SECOND;
303 break;
304 default:
305 reg_addr = ZX234290_REG_ADDR_ALARM_MINUTE;
306 break;
307 }
308
309 reg_val = ZX234290_BITFVAL(enabled, ZX234290_RTC_AlARM_ACTIVATED_LSH);
310 mask = ZX234290_BITFMASK(ZX234290_RTC_AlARM_ACTIVATED_WID, ZX234290_RTC_AlARM_ACTIVATED_LSH);
311 ret = zx234290_rtc_write_register(reg_addr, reg_val, mask);
312 return ret;
313}
314
315/*timer counter set*/
316int zx234290_rtc_set_timercounter(unsigned char cnt)
317{
318 int ret = 0;
319 unsigned char reg_addr=0, reg_val=0, mask=0;
320
321 reg_addr = ZX234290_REG_ADDR_TIMER_CNT;
322 reg_val = ZX234290_BITFVAL(cnt, ZX234290_RTC_TIMER_CNT_LSH);
323 mask = ZX234290_BITFMASK(ZX234290_RTC_TIMER_CNT_WID, ZX234290_RTC_TIMER_CNT_LSH);
324 ret = zx234290_rtc_write_register(reg_addr, reg_val, mask);
325
326 return ret;
327}
328
329/*
330 TIMER_COUNT_4096 = 0,
331 TIMER_COUNT_64 = 1,
332 TIMER_COUNT_1 = 2,
333 TIMER_COUNT_1_60 = 3,
334*/
335int zx234290_rtc_set_clock_frequency(zx234290_timercount td)
336{
337 int ret = 0;
338 unsigned char reg_addr=0, reg_val=0, mask=0;
339
340 reg_addr = ZX234290_REG_ADDR_TIMER_CTRL;
341 reg_val = ZX234290_BITFVAL(td, ZX234290_RTC_TIMER_TD_LSH);
342 mask = ZX234290_BITFMASK(ZX234290_RTC_TIMER_TD_WID, ZX234290_RTC_TIMER_TD_LSH);
343 ret = zx234290_rtc_write_register(reg_addr, reg_val, mask);
344
345 return ret;
346}
347
348static int zx234290_rtc_setaie(struct device *dev, unsigned int enabled)
349{
350 int ret = 0;
351 int reg_val=0, mask=0;
352
353 pr_debug("%s: aie=%d\n", __func__, enabled);
354
355 /*enable/disable AIE bit*/
356 mask = ZX234290_BITFMASK(ZX234290_RTC_AIE_WID, ZX234290_RTC_AIE_LSH);
357 reg_val= ZX234290_BITFVAL(enabled, ZX234290_RTC_AIE_LSH);
358 ret = zx234290_rtc_write_register(ZX234290_REG_ADDR_RTC_CTRL2, reg_val, mask);
359 if (ret != 0)
360 {
361 return ret;
362 }
363
364 /*clear AF bit*/
365 mask = ZX234290_BITFMASK(ZX234290_RTC_AF_WID, ZX234290_RTC_AF_LSH);
366 reg_val= ZX234290_BITFVAL(0, ZX234290_RTC_AF_LSH);
367 ret = zx234290_rtc_write_register(ZX234290_REG_ADDR_RTC_CTRL2, reg_val, mask);
368 if (ret != 0)
369 {
370 return ret;
371 }
372
373 /*mask/unmask alarm int*/
374 /*
375 mask = ZX234290_BITFMASK(ZX234290_RTC_ALARM_INT_MASK_WID, ZX234290_RTC_ALARM_INT_MASK_LSH);
376 if(enabled)
377 {
378 reg_val= ZX234290_BITFVAL(0, ZX234290_RTC_ALARM_INT_MASK_LSH);
379 }
380 else
381 {
382 reg_val= ZX234290_BITFVAL(1, ZX234290_RTC_ALARM_INT_MASK_LSH);
383 }
384
385 ret = zx234290_rtc_write_register(ZX234290_REG_ADDR_INTB_MASK, reg_val, mask);
386 if (ret != 0)
387 {
388 return ret;
389 }
390 */
391
392 return ret;
393}
394
395static int zx234290_rtc_settie(struct device *dev, unsigned int enabled)
396{
397 int ret = 0;
398 int reg_val=0, mask=0;
399 /*enable/disable TIE bit*/
400 mask = ZX234290_BITFMASK(ZX234290_RTC_TIE_WID, ZX234290_RTC_TIE_LSH);
401 reg_val= ZX234290_BITFVAL(enabled, ZX234290_RTC_TIE_LSH);
402 ret = zx234290_rtc_write_register(ZX234290_REG_ADDR_RTC_CTRL2, reg_val, mask);
403 if (ret != 0)
404 {
405 return ret;
406 }
407 return 0;
408}
409static int zx234290_rtc_setuie(struct device *dev, unsigned int enabled)
410{
411 int nRet = 0;
412 int reg_val=0, mask=0;
413 pr_debug("%s: uie=%d\n", __func__, enabled);
414 /*
415 mask = ZX234290_BITFMASK(ZX234290_RTC_TIMER_INT_EN_WID, ZX234290_RTC_TIMER_INT_EN_LSH);
416 reg_val= ZX234290_BITFVAL(enabled, ZX234290_RTC_TIMER_INT_EN_LSH);
417 nRet = zx234290_rtc_write_register(ZX234290_REG_ADDR_RTC_CTRL2, reg_val, mask);
418 if (nRet != 0)
419 {
420 return nRet;
421 }
422 */
423 mask = ZX234290_BITFMASK(ZX234290_RTC_ONE_MINUTE_INT_MASK_WID, ZX234290_RTC_ONE_MINUTE_INT_MASK_LSH);
424 if(enabled)
425 {
426 reg_val= ZX234290_BITFVAL(0, ZX234290_RTC_ONE_MINUTE_INT_MASK_LSH);
427 }
428 else
429 {
430 reg_val= ZX234290_BITFVAL(1, ZX234290_RTC_ONE_MINUTE_INT_MASK_LSH);
431 }
432
433 nRet = zx234290_rtc_write_register(ZX234290_REG_ADDR_INTB_MASK, reg_val, mask);
434
435 mask = ZX234290_BITFMASK(ZX234290_RTC_ONE_HOUR_INT_MASK_WID, ZX234290_RTC_ONE_HOUR_INT_MASK_LSH);
436 if(enabled)
437 {
438 reg_val= ZX234290_BITFVAL(0, ZX234290_RTC_ONE_HOUR_INT_MASK_LSH);
439 }
440 else
441 {
442 reg_val= ZX234290_BITFVAL(1, ZX234290_RTC_ONE_HOUR_INT_MASK_LSH);
443 }
444
445 nRet = zx234290_rtc_write_register(ZX234290_REG_ADDR_INTB_MASK, reg_val, mask);
446
447 return nRet;
448}
449
450static int zx234290_rtc_getvalue(unsigned char reg, unsigned char *dest, unsigned char width, unsigned char offset)
451{
452 int nRet = 0;
453 unsigned char val;
454 nRet = zx234290_rtc_read_register(reg, dest);
455 if (nRet != 0)
456 {
457 return nRet;
458 }
459 val = *dest;
460 val = ZX234290_BITFEXT(val,width, offset);
461 *dest = val;
462 return nRet;
463}
464
465static int zx234290_rtc_setvalue(unsigned char addr, unsigned char data, unsigned char width, unsigned char offset)
466{
467 int nRet = 0;
468 unsigned char reg_val=0, mask=0;
469 reg_val = ZX234290_BITFVAL(data, offset);
470 mask = ZX234290_BITFMASK(width, offset);
471 nRet = zx234290_rtc_write_register(addr, reg_val, mask);
472 return nRet;
473}
474
475
476int zx234290_rtc_gettime(struct device *dev, struct rtc_time *rtc_tm)
477{
478 unsigned char day, mon, year, wday, sec, min, hour;
479 unsigned char CenturyIndicate = 0;
480 unsigned int have_retried = 0;
481 if(rtc_tm == NULL)
482 {
483 return -1;
484 }
485retry_get_time:
486
487 zx234290_rtc_getvalue(ZX234290_REG_ADDR_SECONDS, &sec, ZX234290_RTC_TIME_SECONDS_WID, ZX234290_RTC_TIME_SECONDS_LSH);
488 zx234290_rtc_getvalue(ZX234290_REG_ADDR_MINUTES, &min, ZX234290_RTC_TIME_MINUTES_WID, ZX234290_RTC_TIME_MINUTES_LSH);
489 zx234290_rtc_getvalue(ZX234290_REG_ADDR_HOURS, &hour, ZX234290_RTC_TIME_HOURS_WID, ZX234290_RTC_TIME_HOURS_LSH);
490 zx234290_rtc_getvalue(ZX234290_REG_ADDR_DAY, &day, ZX234290_RTC_TIME_DAYS_WID, ZX234290_RTC_TIME_DAYS_LSH);
491 zx234290_rtc_getvalue(ZX234290_REG_ADDR_MONTH, &mon, ZX234290_RTC_TIME_MONTHS_WID, ZX234290_RTC_TIME_MONTHS_LSH);
492 zx234290_rtc_getvalue(ZX234290_REG_ADDR_YEAR, &year, ZX234290_RTC_TIME_YEAR_WID, ZX234290_RTC_TIME_YEAR_LSH);
493 zx234290_rtc_getvalue(ZX234290_REG_ADDR_WEEK, &wday, ZX234290_RTC_TIME_WEEKDAY_WID, ZX234290_RTC_TIME_WEEKDAY_LSH);
494 zx234290_rtc_getvalue(ZX234290_REG_ADDR_MONTH, &CenturyIndicate, ZX234290_RTC_TIME_CENTURY_WID, ZX234290_RTC_TIME_CENTURY_LSH);
495
496
497 if (sec == 0 && !have_retried)
498 {
499 have_retried = 1;
500 goto retry_get_time;
501 }
502 rtc_tm->tm_sec = bcd2bin(sec);
503 rtc_tm->tm_min = bcd2bin(min);
504 rtc_tm->tm_hour = bcd2bin(hour);
505 rtc_tm->tm_mday = bcd2bin(day);
506 rtc_tm->tm_mon = bcd2bin(mon);
507 rtc_tm->tm_year = bcd2bin(year);//+2000;
508 rtc_tm->tm_wday = bcd2bin(wday);
509 rtc_tm->tm_yday = rtc_year_days(rtc_tm->tm_mday, rtc_tm->tm_mon , rtc_tm->tm_year);
510 if(CenturyIndicate == 0)
511 {
512 rtc_tm->tm_year += 100;
513 }
514 rtc_tm->tm_mon -= 1;
515 rtc_tm->tm_isdst = 0;
516 pr_debug("read time %04d.%02d.%02d %02d:%02d:%02d\n",
517 rtc_tm->tm_year, rtc_tm->tm_mon+1, rtc_tm->tm_mday,
518 rtc_tm->tm_hour, rtc_tm->tm_min, rtc_tm->tm_sec);
519
520 return rtc_valid_tm(rtc_tm);
521}
522
523static int zx234290_rtc_settime(struct device *dev, struct rtc_time *tm)
524{
525 int ret = 0;
526 unsigned char day, mon, year, wday, sec, min, hour, CenturyIndicate;
527 unsigned char reg_val=0, mask=0;
528 //int year = tm->tm_year - 100;
529 pr_debug("set time %04d.%02d.%02d %02d:%02d:%02d\n",
530 tm->tm_year, tm->tm_mon+1, tm->tm_mday,
531 tm->tm_hour, tm->tm_min, tm->tm_sec);
532 if(tm->tm_year >= 200)
533 {
534 return -1;
535
536 }else if(tm->tm_year >= 100)
537 {
538 year = tm->tm_year - 100;
539 CenturyIndicate = 0; /*indicate 21 ÊÀ¼Í 20xx*/
540 }
541 else
542 {
543 year = tm->tm_year;
544 CenturyIndicate = 1; /*indicate 20 ÊÀ¼Í 19xx*/
545 }
546 /* we get around y2k by simply not supporting it */
547 sec = bin2bcd(tm->tm_sec);
548 reg_val = ZX234290_BITFVAL(sec, ZX234290_RTC_TIME_SECONDS_LSH);
549 mask = ZX234290_BITFMASK(ZX234290_RTC_TIME_SECONDS_WID, ZX234290_RTC_TIME_SECONDS_LSH);
550 ret = zx234290_rtc_write_register(ZX234290_REG_ADDR_SECONDS, reg_val, mask);
551
552 min = bin2bcd(tm->tm_min);
553 reg_val = ZX234290_BITFVAL(min, ZX234290_RTC_TIME_MINUTES_LSH);
554 mask = ZX234290_BITFMASK(ZX234290_RTC_TIME_MINUTES_WID, ZX234290_RTC_TIME_MINUTES_LSH);
555 ret += zx234290_rtc_write_register(ZX234290_REG_ADDR_MINUTES, reg_val, mask);
556
557 hour = bin2bcd(tm->tm_hour);
558 reg_val = ZX234290_BITFVAL(hour, ZX234290_RTC_TIME_HOURS_LSH);
559 mask = ZX234290_BITFMASK(ZX234290_RTC_TIME_HOURS_WID, ZX234290_RTC_TIME_HOURS_LSH);
560 ret += zx234290_rtc_write_register(ZX234290_REG_ADDR_HOURS, reg_val, mask);
561
562 day = bin2bcd(tm->tm_mday);
563 reg_val = ZX234290_BITFVAL(day, ZX234290_RTC_TIME_DAYS_LSH);
564 mask = ZX234290_BITFMASK(ZX234290_RTC_TIME_DAYS_WID, ZX234290_RTC_TIME_DAYS_LSH);
565 ret += zx234290_rtc_write_register(ZX234290_REG_ADDR_DAY, reg_val, mask);
566
567 mon = bin2bcd(tm->tm_mon+1);
568 reg_val = ZX234290_BITFVAL(mon, ZX234290_RTC_TIME_MONTHS_LSH);
569 mask = ZX234290_BITFMASK(ZX234290_RTC_TIME_MONTHS_WID, ZX234290_RTC_TIME_MONTHS_LSH);
570 ret += zx234290_rtc_write_register(ZX234290_REG_ADDR_MONTH, reg_val, mask);
571
572 reg_val = ZX234290_BITFVAL(CenturyIndicate, ZX234290_RTC_TIME_CENTURY_LSH);
573 mask = ZX234290_BITFMASK(ZX234290_RTC_TIME_CENTURY_WID, ZX234290_RTC_TIME_CENTURY_LSH);
574 ret += zx234290_rtc_write_register(ZX234290_REG_ADDR_MONTH, reg_val, mask);
575
576 year = bin2bcd(year);
577 reg_val = ZX234290_BITFVAL(year, ZX234290_RTC_TIME_YEAR_LSH);
578 mask = ZX234290_BITFMASK(ZX234290_RTC_TIME_YEAR_WID, ZX234290_RTC_TIME_YEAR_LSH);
579 ret += zx234290_rtc_write_register(ZX234290_REG_ADDR_YEAR, reg_val, mask);
580
581 wday = bin2bcd(tm->tm_wday);
582 reg_val = ZX234290_BITFVAL(wday, ZX234290_RTC_TIME_WEEKDAY_LSH);
583 mask = ZX234290_BITFMASK(ZX234290_RTC_TIME_WEEKDAY_WID, ZX234290_RTC_TIME_WEEKDAY_LSH);
584 ret += zx234290_rtc_write_register(ZX234290_REG_ADDR_WEEK, reg_val, mask);
585
586 return ret;
587}
588
589static int zx234290_rtc_getalarm(struct device *dev, struct rtc_wkalrm *alrm)
590{
591 struct rtc_time *alm_tm = &alrm->time;
592 unsigned char second, day, wday, min, hour;
593 unsigned char reg_val=0, mask=0;
594 if(alrm == NULL)
595 {
596 return -1;
597 }
598 zx234290_rtc_getvalue(ZX234290_REG_ADDR_ALARM_SECOND, &second, ZX234290_RTC_AlARM_SECOND_WID, ZX234290_RTC_AlARM_SECOND_LSH);
599 zx234290_rtc_getvalue(ZX234290_REG_ADDR_ALARM_MINUTE, &min, ZX234290_RTC_AlARM_MINUTES_WID, ZX234290_RTC_AlARM_MINUTES_LSH);
600 zx234290_rtc_getvalue(ZX234290_REG_ADDR_ALARM_HOUR, &hour, ZX234290_RTC_AlARM_HOURS_WID, ZX234290_RTC_AlARM_HOURS_LSH);
601 zx234290_rtc_getvalue(ZX234290_REG_ADDR_ALARM_DAY, &day, ZX234290_RTC_AlARM_DAYS_WID, ZX234290_RTC_AlARM_DAYS_LSH);
602 zx234290_rtc_getvalue(ZX234290_REG_ADDR_ALARM_WEEK, &wday, ZX234290_RTC_AlARM_WEEKDAY_WID, ZX234290_RTC_AlARM_WEEKDAY_LSH);
603
604 mask = ZX234290_BITFMASK(ZX234290_RTC_ALARM_INT_EN_WID, ZX234290_RTC_ALARM_INT_EN_LSH);
605 zx234290_rtc_read_register(ZX234290_REG_ADDR_RTC_CTRL2, &reg_val);
606 alrm->enabled = (reg_val & mask)? 1 : 0;
607
608 pr_debug("read alarm %d, %04d.%02d.%02d %02d:%02d:%02d\n",
609 alrm->enabled,
610 1900 + alm_tm->tm_year, alm_tm->tm_mon+1, alm_tm->tm_mday,
611 alm_tm->tm_hour, alm_tm->tm_min, alm_tm->tm_sec);
612 /* decode the alarm enable field */
613 if(alrm->enabled)
614 {
615 //alm_tm->tm_sec = 0; //bcd2bin(alm_tm->tm_sec);
616 alm_tm->tm_sec = bcd2bin(second);//yuxiang
617 alm_tm->tm_min = bcd2bin(min);
618 alm_tm->tm_hour = bcd2bin(hour);
619 alm_tm->tm_mday = bcd2bin(day);
620 alm_tm->tm_wday = bcd2bin(wday);
621 }
622 else
623 {
624 alm_tm->tm_sec = -1;
625 alm_tm->tm_min = -1;
626 alm_tm->tm_hour = -1;
627 alm_tm->tm_mday = -1;
628 alm_tm->tm_mon = -1;
629 alm_tm->tm_year = -1;
630 }
631
632 return 0;
633}
634
635static int zx234290_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)
636{
637 struct rtc_time *tm = &alrm->time;
638 //unsigned long expires;
639 unsigned char sec, min, hour, mday, wday;
640 //rtc_tm_to_time(tm, &expires);
641 //expires = roundup(expires, 60);
642 //rtc_time_to_tm(expires, tm);
643 //tm->tm_sec = 0;
644
645 if (tm->tm_sec < 60 && tm->tm_sec >= 0)//yuxiang
646 {
647 sec = bin2bcd(tm->tm_sec);
648 zx234290_rtc_setvalue(ZX234290_REG_ADDR_ALARM_SECOND, sec, ZX234290_RTC_AlARM_SECOND_WID, ZX234290_RTC_AlARM_SECOND_LSH);
649 zx234290_rtc_alarm_enable(ALARM_SECOND, 0);
650 }
651 else
652 {
653 zx234290_rtc_alarm_enable(ALARM_SECOND, 1);
654 }
655
656 if (tm->tm_min < 60 && tm->tm_min >= 0)
657 {
658 min = bin2bcd(tm->tm_min);
659 zx234290_rtc_setvalue(ZX234290_REG_ADDR_ALARM_MINUTE, min, ZX234290_RTC_AlARM_MINUTES_WID, ZX234290_RTC_AlARM_MINUTES_LSH);
660 zx234290_rtc_alarm_enable(ALARM_MINUTE, 0);
661 }
662 else
663 {
664 zx234290_rtc_alarm_enable(ALARM_MINUTE, 1);
665 }
666 if (tm->tm_hour < 24 && tm->tm_hour >= 0)
667 {
668 hour = bin2bcd(tm->tm_hour);
669 zx234290_rtc_setvalue(ZX234290_REG_ADDR_ALARM_HOUR, hour, ZX234290_RTC_AlARM_HOURS_WID, ZX234290_RTC_AlARM_HOURS_LSH);
670 zx234290_rtc_alarm_enable(ALARM_HOUR, 0);
671 }
672 else
673 {
674 zx234290_rtc_alarm_enable(ALARM_HOUR, 1);
675 }
676 if(tm->tm_mday < 32 && tm->tm_mday > 0)
677 {
678 mday = bin2bcd(tm->tm_mday);
679 zx234290_rtc_setvalue(ZX234290_REG_ADDR_ALARM_DAY, mday, ZX234290_RTC_AlARM_DAYS_WID, ZX234290_RTC_AlARM_DAYS_LSH);
680 zx234290_rtc_alarm_enable(ALARM_DAY, 0);
681 }
682 else
683 {
684 zx234290_rtc_alarm_enable(ALARM_DAY, 1);
685 }
686 if(tm->tm_wday < 7 && tm->tm_wday >= 0)
687 {
688 wday = bin2bcd(tm->tm_wday);
689 zx234290_rtc_setvalue(ZX234290_REG_ADDR_ALARM_WEEK, wday, ZX234290_RTC_AlARM_WEEKDAY_WID, ZX234290_RTC_AlARM_WEEKDAY_LSH);
690 zx234290_rtc_alarm_enable(ALARM_WEEKDAY, 0);
691 }
692 else
693 {
694 zx234290_rtc_alarm_enable(ALARM_WEEKDAY, 1);
695 }
696 zx234290_rtc_setaie(dev, alrm->enabled);
697 return 0;
698}
699
700static int zx234290_rtc_request_irq(struct platform_device *pdev, struct rtc_device *rtc)
701{
702 int ret = -1;
703 //zx234290_rtc.zx234290->irq_base = PMIC_INT_START;
704 ret = request_threaded_irq(zx234290_rtc.zx234290->irq_base + ZX234290_INT_RTC_ALRM,
705 NULL, zx234290_rtc_alarmirq, 0, "zx234290-rtc alarm", rtc);
706 if (ret)
707 {
708 dev_err(&pdev->dev, "IRQ%d error %d\n", zx234290_rtc.zx234290->irq_base + ZX234290_INT_RTC_ALRM, ret);
709 goto err_alarm_irq;
710 }
711 ret = request_threaded_irq(zx234290_rtc.zx234290->irq_base + ZX234290_INT_RTC_HOUR,
712 NULL, zx234290_rtc_hourirq, 0, "zx234290-rtc hour", rtc);
713 if (ret)
714 {
715 dev_err(&pdev->dev, "IRQ%d error %d\n", zx234290_rtc.zx234290->irq_base + ZX234290_INT_RTC_HOUR, ret);
716 goto err_hour_irq;
717 }
718 ret = request_threaded_irq(zx234290_rtc.zx234290->irq_base + ZX234290_INT_RTC_MIN,
719 NULL, zx234290_rtc_minirq, 0, "zx234290-rtc min", rtc);
720 if (ret)
721 {
722 dev_err(&pdev->dev, "IRQ%d error %d\n", zx234290_rtc.zx234290->irq_base + ZX234290_INT_RTC_MIN, ret);
723 goto err_min_irq;
724 }
725
726 return 0;
727
728 err_min_irq:
729 free_irq(zx234290_rtc.zx234290->irq_base + ZX234290_INT_RTC_MIN, rtc);
730 err_hour_irq:
731 free_irq(zx234290_rtc.zx234290->irq_base + ZX234290_INT_RTC_HOUR, rtc);
732 err_alarm_irq:
733 free_irq(zx234290_rtc.zx234290->irq_base + ZX234290_INT_RTC_ALRM, rtc);
734
735 return ret;
736}
737
738#if 1
739
740static int zx234290_rtc_set_second_alarm_int(struct device *dev, unsigned int enabled)
741{
742 int nRet = 0;
743
744 int ret = 0;
745 int reg_addr=0, mask=0;
746 unsigned char reg_val=0;
747 int bIsOn = 0;
748
749 #if 0
750 pr_debug("%s: aie=%d\n", __func__, enabled);
751 mask = ZX234290_BITFMASK(ZX234290_RTC_ALARM_INT_EN_WID, ZX234290_RTC_ALARM_INT_EN_LSH);
752 reg_val= ZX234290_BITFVAL(enabled, ZX234290_RTC_ALARM_INT_EN_LSH);
753 nRet = zx234290_rtc_write_register(ZX234290_REG_ADDR_RTC_CTRL2, reg_val, mask);
754 if (nRet != 0)
755 {
756 return nRet;
757 }
758 mask = ZX234290_BITFMASK(ZX234290_RTC_ALARM_AF_WID, ZX234290_RTC_ALARM_AF_LSH);
759 reg_val= ZX234290_BITFVAL(0, ZX234290_RTC_ALARM_AF_LSH);
760 zx234290_rtc_write_register(ZX234290_REG_ADDR_RTC_CTRL2, reg_val, mask);
761 mask = ZX234290_BITFMASK(ZX234290_RTC_ALARM_INT_MASK_WID, ZX234290_RTC_ALARM_INT_MASK_LSH);
762 if(enabled)
763 {
764 reg_val= ZX234290_BITFVAL(0, ZX234290_RTC_ALARM_INT_MASK_LSH);
765 }
766 else
767 {
768 reg_val= ZX234290_BITFVAL(1, ZX234290_RTC_ALARM_INT_MASK_LSH);
769 }
770 nRet = zx234290_rtc_write_register(ZX234290_REG_ADDR_INTB_MASK, reg_val, mask);
771 #endif
772 if(enabled)
773 bIsOn = 0;//active
774 else
775 bIsOn = 1;//inactive
776
777 reg_addr = ZX234290_REG_ADDR_INTB_MASK;
778 reg_val = ZX234290_BITFVAL(enabled, ZX234290_RTC_ALARM_INT_MASK_LSH);
779 mask = ZX234290_BITFMASK(ZX234290_RTC_ALARM_INT_MASK_WID, ZX234290_RTC_ALARM_INT_MASK_LSH);
780 ret = zx234290_rtc_write_register(reg_addr,reg_val, mask);
781 zx234290_rtc_read_register(0x03, &reg_val);
782
783// printk(KERN_INFO "zx234290_rtc_ioctl:0x03 = %x.\n",reg_val);
784#if 0
785 reg_addr = ZX234290_REG_ADDR_RTC_CTRL2;
786 reg_val = ZX234290_BITFVAL(enabled, ZX234290_RTC_ALARM_INT_EN_LSH);
787 mask = ZX234290_BITFMASK(ZX234290_RTC_ALARM_INT_EN_WID, ZX234290_RTC_ALARM_INT_EN_LSH);
788 ret = zx234290_rtc_write_register(slv_addr,reg_val, mask);
789 reg_addr = ZX234290_REG_ADDR_RTC_CTRL2;
790 reg_val = ZX234290_BITFVAL(enabled, 2);
791 mask = ZX234290_BITFMASK(1, 2);
792 ret = zx234290_rtc_write_register(slv_addr,reg_val, mask);
793#endif
794 return nRet;
795}
796
797//static DECLARE_MUTEX(timerSemaphore);
798
799
800
801int zx234290_rtc_gettimer(void)
802{
803 int ret = -EPERM;
804
805 if(!test_and_set_bit(TIMER_SEMA_WAIT_BIT, &timer_sema_flag))
806 ret = down_interruptible(&timerSemaphore);
807
808 clear_bit(TIMER_SEMA_WAIT_BIT, &timer_sema_flag);
809
810 if(!ret){
811// printk(KERN_INFO "zx234290_rtc_gettimer:get semaphore success.\n");
812
813 zx234290_rtc_enable(g_rtc->dev.parent, 0);
814 zx234290_rtc_settie(g_rtc->dev.parent, 0);
815 zx234290_rtc_set_second_alarm_int(g_rtc->dev.parent, 1);
816
817 //zx234290_rtc_set_clock_frequency(TIMER_COUNT_1);
818 return 0;
819 }
820 else{
821 // printk(KERN_INFO "zx234290_rtc_gettimer:get semaphore ret is %d.\n", ret);
822
823 return ret;
824 }
825}
826
827int zx234290_rtc_set_second_timer(int seconds)
828{
829 int mask = 0;
830 unsigned char reg_val=0;
831 //int value = 0;
832
833 mask = ZX234290_BITFMASK(ZX234290_RTC_TIMER_INT_EN_WID, 2);
834 reg_val= ZX234290_BITFVAL(0, 2);
835 zx234290_rtc_write_register(ZX234290_REG_ADDR_RTC_CTRL2, reg_val, mask);
836 zx234290_rtc_set_timercounter(seconds);
837 zx234290_rtc_set_clock_frequency(TIMER_COUNT_1);
838 zx234290_rtc_enable(g_rtc->dev.parent, 1);
839 zx234290_rtc_settie(g_rtc->dev.parent, 1);
840 zx234290_rtc_set_second_alarm_int(g_rtc->dev.parent, 0);
841#if 0
842 zx234290_rtc_read_register(0x01, &value);
843 printk(KERN_INFO "zx234290_rtc_ioctl:0x01 = %x.\n",value);
844 zx234290_rtc_read_register(0x03, &value);
845 printk(KERN_INFO "zx234290_rtc_ioctl:0x03 = %x.\n",value);
846 zx234290_rtc_read_register(0x05, &value);
847 printk(KERN_INFO "zx234290_rtc_ioctl:0x05 = %x.\n",value);
848 zx234290_rtc_read_register(0x30, &value);
849 printk(KERN_INFO "zx234290_rtc_ioctl:0x30 = %x.\n",value);
850 zx234290_rtc_read_register(0x31, &value);
851 printk(KERN_INFO "zx234290_rtc_ioctl:0x31 = %x.\n",value);
852 zx234290_rtc_read_register(0x3E, &value);
853 printk(KERN_INFO "zx234290_rtc_ioctl:0x3E = %x.\n",value);
854 zx234290_rtc_read_register(0x3F, &value);
855 printk(KERN_INFO "zx234290_rtc_ioctl:0x3F = %x.\n",value);
856#endif
857 return 0;
858}
859
860void printf_rtc_log(void)
861{
862 unsigned char value = 0;
863
864 zx234290_rtc_read_register(0x01, &value);
865 printk(KERN_INFO "printf_rtc_log:0x01 = %x.\n",value);
866 zx234290_rtc_read_register(0x03, &value);
867 printk(KERN_INFO "printf_rtc_log:0x03 = %x.\n",value);
868 zx234290_rtc_read_register(0x05, &value);
869 printk(KERN_INFO "printf_rtc_log:0x05 = %x.\n",value);
870 zx234290_rtc_read_register(0x30, &value);
871 printk(KERN_INFO "printf_rtc_log:0x30 = %x.\n",value);
872 zx234290_rtc_read_register(0x31, &value);
873 printk(KERN_INFO "printf_rtc_log:0x31 = %x.\n",value);
874 zx234290_rtc_read_register(0x3E, &value);
875 printk(KERN_INFO "printf_rtc_log:0x3E = %x.\n",value);
876 zx234290_rtc_read_register(0x3F, &value);
877 printk(KERN_INFO "printf_rtc_log:0x3F = %x.\n",value);
878}
879
880static int zx234290_rtc_set_alarm_enable(struct device *dev, unsigned int enabled)
881{
882 int nRet = 0;
883 int reg_val=0, mask=0;
884
885 mask = ZX234290_BITFMASK(ZX234290_RTC_ALARM_INT_EN_WID, ZX234290_RTC_ALARM_INT_EN_LSH);
886 reg_val= ZX234290_BITFVAL(enabled, ZX234290_RTC_ALARM_INT_EN_LSH);
887
888 nRet = zx234290_rtc_write_register(ZX234290_REG_ADDR_RTC_CTRL2, reg_val, mask);
889
890 mask = ZX234290_BITFMASK(1, 0);
891 reg_val= ZX234290_BITFVAL(0, 0);
892
893 nRet = zx234290_rtc_write_register(0x03, reg_val, mask);
894
895 mask = ZX234290_BITFMASK(2, 2);
896 reg_val= ZX234290_BITFVAL(0, 2);
897
898 nRet = zx234290_rtc_write_register(0x31, reg_val, mask);
899 return 0;
900}
901
902
903int zx234290_rtc_disable_timer_alarm(void)
904{
905 zx234290_rtc_enable(g_rtc->dev.parent, 0);
906 zx234290_rtc_settie(g_rtc->dev.parent, 0);
907 zx234290_rtc_set_alarm_enable(g_rtc->dev.parent, 0);
908 zx234290_rtc_alarm_enable(ALARM_MINUTE,1);
909 zx234290_rtc_alarm_enable(ALARM_HOUR,1);
910 zx234290_rtc_alarm_enable(ALARM_DAY,1);
911 zx234290_rtc_alarm_enable(ALARM_WEEKDAY,1);
912 return 0;
913}
914EXPORT_SYMBOL_GPL(zx234290_rtc_disable_timer_alarm);
915
916/****get value of rtc timer cnt ****/
917unsigned char zx234290_rtc_gettimer_cnt_remain(void)
918{
919 unsigned char timercnt_remain=0;
920 zx234290_rtc_getvalue(ZX234290_REG_ADDR_TIMER_CNT, &timercnt_remain, ZX234290_RTC_TIMER_CNT_WID, ZX234290_RTC_TIMER_CNT_LSH);
921 return timercnt_remain;
922}
923
924
925
926
927static int zx234290_rtc_ioctl(struct device *dev,zx234290_rtc_timer cmd,unsigned long arg)
928{
929 int err = 0;
930 unsigned int seconds;
931 unsigned char timercnt_remain = 0;
932 unsigned int seconds_remain = 0;
933 void __user *uarg = (void __user *) arg;
934// printk(KERN_INFO "zx234290_rtc_ioctl:cmd=%d[%c%c%c%d]arg=%ld.\n",cmd, (cmd>>24)&0xff,(cmd>>16)&0xff,(cmd>>8)&0xff,cmd&0xff,arg);
935 switch(cmd)
936 {
937 case ZX234290_SET_TIMER:
938 {
939 seconds = arg;
940
941 if(seconds){
942 set_bit(TIMER_SEMA_START_BIT, &timer_sema_flag);
943
944 timer_remain = seconds;
945
946 zx234290_rtc_set_second_timer(min(timer_remain,255));
947 timer_remain -= min(timer_remain,255);
948// printk(KERN_INFO "ZX234290_SET_TIMER:set timer=%d, remain=%d.\n", seconds,timer_remain);
949 }
950
951 else/* if(seconds == 0)*/{
952// printk(KERN_INFO "ZX234290_SET_TIMER(seconds == %ld):timerSemaphore=0x%x.\n",seconds,timerSemaphore);
953 zx234290_rtc_enable(g_rtc->dev.parent, 0);
954 zx234290_rtc_settie(g_rtc->dev.parent, 0);
955// printf_rtc_log();
956 {
957 if(test_and_clear_bit(TIMER_SEMA_WAIT_BIT, &timer_sema_flag))
958 up(&timerSemaphore);
959 }
960
961 clear_bit(TIMER_SEMA_START_BIT, &timer_sema_flag);
962 }
963
964 seconds = 0;
965 break;
966 }
967 #if 1
968 case ZX234290_GET_TIMER:
969 {
970
971 if(test_bit(TIMER_SEMA_START_BIT, &timer_sema_flag)){
972 err = zx234290_rtc_gettimer();
973// printk(KERN_INFO "ZX234290_GET_TIMER.ret=%d\n",err);
974 }
975 //else
976 // printk(KERN_INFO "ZX234290_GET_TIMER.has not setting timer, return=%d\n",err);
977
978 break;
979 }
980 #endif
981
982 case ZX234290_TIMER_ENABLED:
983 {
984 seconds = arg;
985 // printk(KERN_INFO "ZX234290_TIMER_ENABLED:cmd=%d,senconds=%d.\n", cmd,seconds);
986 if(seconds == 0) {
987 zx234290_rtc_enable(g_rtc->dev.parent, 0);
988 zx234290_rtc_settie(g_rtc->dev.parent, 0);
989// printf_rtc_log();
990 if(test_and_clear_bit(TIMER_SEMA_WAIT_BIT, &timer_sema_flag))
991 up(&timerSemaphore);
992
993 clear_bit(TIMER_SEMA_START_BIT, &timer_sema_flag);
994 }
995 break;
996 }
997 case ZX234290_GET_TIMER_REMAIN:
998 {
999 timercnt_remain = zx234290_rtc_gettimer_cnt_remain();
1000
1001 seconds_remain = timer_remain + timercnt_remain;
1002 // printk(KERN_INFO "ZX234290_GET_TIMER_REMAIN:cmd=%d.remain=%d\n", cmd,seconds_remain);
1003 err = put_user(seconds_remain,(unsigned long __user *)uarg);
1004 break;
1005 }
1006 case ZX234290_CYCLE_ENABLE:
1007 {
1008 seconds = arg;
1009 // printk(KERN_INFO "ZX234290_CYCLE_ENABLE:cmd=%d,enable=%d.\n", cmd,seconds);
1010 zx234290_rtc_setuie(g_rtc->dev.parent, seconds);
1011 break;
1012 }
1013 default:
1014 err=ENOIOCTLCMD;
1015 break;
1016 }
1017 return err;
1018}
1019
1020static int zx234290_rtc_enable(struct device *dev, int enable)
1021{
1022 int ret = 0;
1023 int reg_val=0, mask=0;
1024
1025 reg_val = ZX234290_BITFVAL(enable, ZX234290_RTC_TIMER_EN_LSH);
1026 mask = ZX234290_BITFMASK(ZX234290_RTC_TIMER_EN_WID, ZX234290_RTC_TIMER_EN_LSH);
1027 ret = zx234290_rtc_write_register(ZX234290_REG_ADDR_TIMER_CTRL, reg_val, mask);
1028
1029 return ret;
1030}
1031
1032/* ÉèÖÃRTCÄÚ²¿µ¹¼ÆÊ±¶¨Ê±Æ÷,ÓÃÓÚʵÏֹػúºó¶¨Ê±¿ª»ú */
1033int zx234290_rtc_settimer(int sec)
1034{
1035 int mask = 0;
1036 unsigned char reg_val=0;
1037 //int value = 0;
1038
1039 mask = ZX234290_BITFMASK(ZX234290_RTC_TIMER_INT_EN_WID, 2);
1040 reg_val= ZX234290_BITFVAL(0, 2);
1041 zx234290_rtc_write_register(ZX234290_REG_ADDR_RTC_CTRL2, reg_val, mask);
1042 zx234290_rtc_set_timercounter(sec);
1043 zx234290_rtc_enable(g_rtc->dev.parent, 1);
1044 zx234290_rtc_settie(g_rtc->dev.parent, 1);
1045 zx234290_rtc_setaie(g_rtc->dev.parent, 0);
1046 #if 0
1047 zx234290_rtc_read_register(0x30, &value);
1048 printk(KERN_INFO "zx234290_rtc_ioctl:0x30 = %x.\n",value);
1049 zx234290_rtc_read_register(0x31, &value);
1050 printk(KERN_INFO "zx234290_rtc_ioctl:0x31 = %x.\n",value);
1051 zx234290_rtc_read_register(0x3E, &value);
1052 printk(KERN_INFO "zx234290_rtc_ioctl:0x3E = %x.\n",value);
1053 zx234290_rtc_read_register(0x3F, &value);
1054 printk(KERN_INFO "zx234290_rtc_ioctl:0x3F = %x.\n",value);
1055 //printk("zx234290_rtc_settimer is called! \n");
1056 #endif
1057 return 0;
1058}
1059EXPORT_SYMBOL_GPL(zx234290_rtc_settimer);
1060
1061#endif
1062
1063static const struct rtc_class_ops zx234290_rtcops = {
1064 .read_time = zx234290_rtc_gettime,
1065 .set_time = zx234290_rtc_settime,
1066 .read_alarm = zx234290_rtc_getalarm,
1067 .set_alarm = zx234290_rtc_setalarm,
1068 .alarm_irq_enable = zx234290_rtc_setaie,
1069 .ioctl = zx234290_rtc_ioctl,
1070};
1071static int zx234290_rtc_probe(struct platform_device *pdev)
1072{
1073 struct rtc_device *rtc;
1074 struct rtc_time rtc_tm;
1075 int ret;
1076 g_rtcdev = pdev;
1077 zx234290_rtc.zx234290 = dev_get_drvdata(pdev->dev.parent);
1078 if(NULL==zx234290_rtc.zx234290)
1079 return -EINVAL;
1080
1081 zx234290_rtc_set_timercounter(128);
1082 zx234290_rtc_set_clock_frequency(TIMER_COUNT_1);
1083 device_init_wakeup(&pdev->dev, 1);
1084 rtc = rtc_device_register("zx234290", &pdev->dev, &zx234290_rtcops, THIS_MODULE); /* register RTC and exit */
1085 if (IS_ERR(rtc))
1086 {
1087 dev_err(&pdev->dev, "cannot attach rtc\n");
1088 ret = PTR_ERR(rtc);
1089 goto err_nortc;
1090 }
1091 g_rtc = rtc;
1092 zx234290_rtc_gettime(NULL, &rtc_tm); /* Check RTC Time */
1093 tempTime = rtc_tm;
1094 if (rtc_valid_tm(&rtc_tm) < 0)
1095 {
1096 rtc_tm.tm_year = 100;
1097 rtc_tm.tm_mon = 0;
1098 rtc_tm.tm_mday = 1;
1099 rtc_tm.tm_hour = 0;
1100 rtc_tm.tm_min = 0;
1101 rtc_tm.tm_sec = 0;
1102 rtc_tm.tm_wday= 6;
1103 zx234290_rtc_settime(NULL, &rtc_tm);
1104 dev_warn(&pdev->dev, "warning: invalid RTC value so initializing it\n");
1105 }
1106 else
1107 {
1108 rtc_tm = rtc_ktime_to_tm(rtc_tm_to_ktime(rtc_tm));
1109 zx234290_rtc_settime(NULL, &rtc_tm);
1110 }
1111 rtc->max_user_freq = 64; //32768;//32k clock
1112 zx234290_rtc_irqno = zx234290_rtc.zx234290->chip_irq;
1113 platform_set_drvdata(pdev, rtc);
1114 ret = zx234290_rtc_request_irq(pdev, rtc);
1115 if (ret)
1116 {
1117 dev_err(&pdev->dev, "IRQ request error!\n");
1118 goto err_irq;
1119 }
1120 zx234290_rtc_write_register(ZX234290_REG_ADDR_RTC_CTRL1, 0, 0xff);
1121 zx234290_rtc_setuie(&pdev->dev, 0);
1122 zx234290_rtc_enable(&pdev->dev, 0);
1123 sema_init(&timerSemaphore,0);
1124 //zx234290_rtc_ioctl(NULL,ZX234290_SET_TIMER,20);
1125 return 0;
1126 err_irq:
1127 platform_set_drvdata(pdev, NULL);
1128 err_nortc:
1129 rtc_device_unregister(rtc);
1130 return ret;
1131}
1132static int zx234290_rtc_remove(struct platform_device *pdev)
1133{
1134 struct rtc_device *rtc = platform_get_drvdata(pdev);
1135
1136 platform_set_drvdata(pdev, NULL);
1137 rtc_device_unregister(rtc);
1138 zx234290_rtc_setuie(&pdev->dev, 0);
1139 zx234290_rtc.zx234290 = NULL;
1140
1141 return 0;
1142}
1143static int zx234290_rtc_resume(struct platform_device *pdev)
1144{
1145 int irq=0;
1146 struct zx234290_board *pmic_plat_data = zx234290_rtc.zx234290->dev->platform_data;
1147 irq=gpio_to_irq(pmic_plat_data->irq_gpio_num);
1148 enable_irq(irq);
1149 zx234290_rtc_setuie(&pdev->dev, 1);
1150
1151 return 0;
1152}
1153static int zx234290_rtc_suspend(struct platform_device *pdev, pm_message_t state)
1154{
1155 int irq=0;
1156 struct zx234290_board *pmic_plat_data = zx234290_rtc.zx234290->dev->platform_data;
1157 //irq=gpio_to_irq(PIN_PMU_INT);
1158 irq=gpio_to_irq(pmic_plat_data->irq_gpio_num);
1159 disable_irq_nosync(irq);
1160 zx234290_rtc_setuie(&pdev->dev, 0);
1161
1162 return 0;
1163}
1164
1165
1166static struct platform_driver zx234290_rtc_driver =
1167{
1168 .probe = zx234290_rtc_probe,
1169 .remove = zx234290_rtc_remove,
1170 .suspend = zx234290_rtc_suspend,
1171 .resume = zx234290_rtc_resume,
1172 .driver = {
1173 .name = "zx234290-rtc",
1174 .owner = THIS_MODULE,
1175 },
1176};
1177
1178
1179module_platform_driver(zx234290_rtc_driver);
1180
1181MODULE_DESCRIPTION("ZX234290 RTC Driver");
1182MODULE_AUTHOR("yuxiang");
1183MODULE_LICENSE("GPL");
1184MODULE_ALIAS("platform:zx234290-rtc");
1185