lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | /* 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
|
| 38 | static struct platform_device *g_rtcdev;
|
| 39 |
|
| 40 | static int zx234290_rtc_enable(struct device *dev, int enable);
|
| 41 | int zx234290_rtc_settimer(int sec);
|
| 42 | int 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))
|
| 47 | static struct rtc_device *g_rtc;
|
| 48 | int CycleTimes = 0;
|
| 49 | int surplus = 0;
|
| 50 | int timer_remain=0;
|
| 51 | //#define ZX234290_GPIO_NUM 10
|
| 52 |
|
| 53 | typedef 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 |
|
| 63 | typedef 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 |
|
| 73 | typedef 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 |
|
| 85 | typedef struct _zx234290_rtc
|
| 86 | {
|
| 87 | struct zx234290 *zx234290;
|
| 88 | }zx234290_rtc_data;
|
| 89 |
|
| 90 | zx234290_rtc_data zx234290_rtc;
|
| 91 | static int zx234290_rtc_irqno = NO_IRQ;
|
| 92 |
|
| 93 | static 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 |
|
| 97 | struct semaphore timerSemaphore;
|
| 98 |
|
| 99 |
|
| 100 | struct rtc_time tempTime = {0};
|
| 101 |
|
| 102 | int zx234290_rtc_gettime(struct device *dev, struct rtc_time *rtc_tm);
|
| 103 | static int zx234290_rtc_getalarm(struct device *dev, struct rtc_wkalrm *alrm);
|
| 104 |
|
| 105 | static 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 |
|
| 128 | static 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.
|
| 143 | void 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 |
|
| 170 | void 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 | }
|
| 183 | void 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 | }
|
| 195 | void 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*/
|
| 212 | static 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,®_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 |
|
| 266 | static 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 | }
|
| 274 | static 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 | }
|
xf.li | 7ccf837 | 2024-03-07 00:08:02 -0800 | [diff] [blame] | 282 | /*alarm enable/disable, 1:disable 0:enable*/
|
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 283 | int 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*/
|
| 316 | int 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 | */
|
| 335 | int 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 |
|
| 348 | static 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);
|
xf.li | 7ccf837 | 2024-03-07 00:08:02 -0800 | [diff] [blame] | 354 |
|
| 355 | if(0==enabled)//disable alarm
|
| 356 | {
|
| 357 | zx234290_rtc_alarm_enable(ALARM_SECOND, 1);
|
| 358 | zx234290_rtc_alarm_enable(ALARM_MINUTE, 1);
|
| 359 | zx234290_rtc_alarm_enable(ALARM_HOUR, 1);
|
| 360 | zx234290_rtc_alarm_enable(ALARM_DAY, 1);
|
| 361 | zx234290_rtc_alarm_enable(ALARM_WEEKDAY, 1);
|
| 362 | };
|
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 363 | /*enable/disable AIE bit*/
|
| 364 | mask = ZX234290_BITFMASK(ZX234290_RTC_AIE_WID, ZX234290_RTC_AIE_LSH);
|
| 365 | reg_val= ZX234290_BITFVAL(enabled, ZX234290_RTC_AIE_LSH);
|
| 366 | ret = zx234290_rtc_write_register(ZX234290_REG_ADDR_RTC_CTRL2, reg_val, mask);
|
| 367 | if (ret != 0)
|
| 368 | {
|
| 369 | return ret;
|
| 370 | }
|
| 371 |
|
| 372 | /*clear AF bit*/
|
| 373 | mask = ZX234290_BITFMASK(ZX234290_RTC_AF_WID, ZX234290_RTC_AF_LSH);
|
| 374 | reg_val= ZX234290_BITFVAL(0, ZX234290_RTC_AF_LSH);
|
| 375 | ret = zx234290_rtc_write_register(ZX234290_REG_ADDR_RTC_CTRL2, reg_val, mask);
|
| 376 | if (ret != 0)
|
| 377 | {
|
| 378 | return ret;
|
| 379 | }
|
| 380 |
|
| 381 | /*mask/unmask alarm int*/
|
| 382 | /*
|
| 383 | mask = ZX234290_BITFMASK(ZX234290_RTC_ALARM_INT_MASK_WID, ZX234290_RTC_ALARM_INT_MASK_LSH);
|
| 384 | if(enabled)
|
| 385 | {
|
| 386 | reg_val= ZX234290_BITFVAL(0, ZX234290_RTC_ALARM_INT_MASK_LSH);
|
| 387 | }
|
| 388 | else
|
| 389 | {
|
| 390 | reg_val= ZX234290_BITFVAL(1, ZX234290_RTC_ALARM_INT_MASK_LSH);
|
| 391 | }
|
| 392 |
|
| 393 | ret = zx234290_rtc_write_register(ZX234290_REG_ADDR_INTB_MASK, reg_val, mask);
|
| 394 | if (ret != 0)
|
| 395 | {
|
| 396 | return ret;
|
| 397 | }
|
| 398 | */
|
| 399 |
|
| 400 | return ret;
|
| 401 | }
|
| 402 |
|
| 403 | static int zx234290_rtc_settie(struct device *dev, unsigned int enabled)
|
| 404 | {
|
| 405 | int ret = 0;
|
| 406 | int reg_val=0, mask=0;
|
| 407 | /*enable/disable TIE bit*/
|
| 408 | mask = ZX234290_BITFMASK(ZX234290_RTC_TIE_WID, ZX234290_RTC_TIE_LSH);
|
| 409 | reg_val= ZX234290_BITFVAL(enabled, ZX234290_RTC_TIE_LSH);
|
| 410 | ret = zx234290_rtc_write_register(ZX234290_REG_ADDR_RTC_CTRL2, reg_val, mask);
|
| 411 | if (ret != 0)
|
| 412 | {
|
| 413 | return ret;
|
| 414 | }
|
| 415 | return 0;
|
| 416 | }
|
| 417 | static int zx234290_rtc_setuie(struct device *dev, unsigned int enabled)
|
| 418 | {
|
| 419 | int nRet = 0;
|
| 420 | int reg_val=0, mask=0;
|
| 421 | pr_debug("%s: uie=%d\n", __func__, enabled);
|
| 422 | /*
|
| 423 | mask = ZX234290_BITFMASK(ZX234290_RTC_TIMER_INT_EN_WID, ZX234290_RTC_TIMER_INT_EN_LSH);
|
| 424 | reg_val= ZX234290_BITFVAL(enabled, ZX234290_RTC_TIMER_INT_EN_LSH);
|
| 425 | nRet = zx234290_rtc_write_register(ZX234290_REG_ADDR_RTC_CTRL2, reg_val, mask);
|
| 426 | if (nRet != 0)
|
| 427 | {
|
| 428 | return nRet;
|
| 429 | }
|
| 430 | */
|
| 431 | mask = ZX234290_BITFMASK(ZX234290_RTC_ONE_MINUTE_INT_MASK_WID, ZX234290_RTC_ONE_MINUTE_INT_MASK_LSH);
|
| 432 | if(enabled)
|
| 433 | {
|
| 434 | reg_val= ZX234290_BITFVAL(0, ZX234290_RTC_ONE_MINUTE_INT_MASK_LSH);
|
| 435 | }
|
| 436 | else
|
| 437 | {
|
| 438 | reg_val= ZX234290_BITFVAL(1, ZX234290_RTC_ONE_MINUTE_INT_MASK_LSH);
|
| 439 | }
|
| 440 |
|
| 441 | nRet = zx234290_rtc_write_register(ZX234290_REG_ADDR_INTB_MASK, reg_val, mask);
|
| 442 |
|
| 443 | mask = ZX234290_BITFMASK(ZX234290_RTC_ONE_HOUR_INT_MASK_WID, ZX234290_RTC_ONE_HOUR_INT_MASK_LSH);
|
| 444 | if(enabled)
|
| 445 | {
|
| 446 | reg_val= ZX234290_BITFVAL(0, ZX234290_RTC_ONE_HOUR_INT_MASK_LSH);
|
| 447 | }
|
| 448 | else
|
| 449 | {
|
| 450 | reg_val= ZX234290_BITFVAL(1, ZX234290_RTC_ONE_HOUR_INT_MASK_LSH);
|
| 451 | }
|
| 452 |
|
| 453 | nRet = zx234290_rtc_write_register(ZX234290_REG_ADDR_INTB_MASK, reg_val, mask);
|
| 454 |
|
| 455 | return nRet;
|
| 456 | }
|
| 457 |
|
| 458 | static int zx234290_rtc_getvalue(unsigned char reg, unsigned char *dest, unsigned char width, unsigned char offset)
|
| 459 | {
|
| 460 | int nRet = 0;
|
| 461 | unsigned char val;
|
| 462 | nRet = zx234290_rtc_read_register(reg, dest);
|
| 463 | if (nRet != 0)
|
| 464 | {
|
| 465 | return nRet;
|
| 466 | }
|
| 467 | val = *dest;
|
| 468 | val = ZX234290_BITFEXT(val,width, offset);
|
| 469 | *dest = val;
|
| 470 | return nRet;
|
| 471 | }
|
| 472 |
|
| 473 | static int zx234290_rtc_setvalue(unsigned char addr, unsigned char data, unsigned char width, unsigned char offset)
|
| 474 | {
|
| 475 | int nRet = 0;
|
| 476 | unsigned char reg_val=0, mask=0;
|
| 477 | reg_val = ZX234290_BITFVAL(data, offset);
|
| 478 | mask = ZX234290_BITFMASK(width, offset);
|
| 479 | nRet = zx234290_rtc_write_register(addr, reg_val, mask);
|
| 480 | return nRet;
|
| 481 | }
|
| 482 |
|
| 483 |
|
| 484 | int zx234290_rtc_gettime(struct device *dev, struct rtc_time *rtc_tm)
|
| 485 | {
|
| 486 | unsigned char day, mon, year, wday, sec, min, hour;
|
| 487 | unsigned char CenturyIndicate = 0;
|
| 488 | unsigned int have_retried = 0;
|
| 489 | if(rtc_tm == NULL)
|
| 490 | {
|
| 491 | return -1;
|
| 492 | }
|
| 493 | retry_get_time:
|
| 494 |
|
| 495 | zx234290_rtc_getvalue(ZX234290_REG_ADDR_SECONDS, &sec, ZX234290_RTC_TIME_SECONDS_WID, ZX234290_RTC_TIME_SECONDS_LSH);
|
| 496 | zx234290_rtc_getvalue(ZX234290_REG_ADDR_MINUTES, &min, ZX234290_RTC_TIME_MINUTES_WID, ZX234290_RTC_TIME_MINUTES_LSH);
|
| 497 | zx234290_rtc_getvalue(ZX234290_REG_ADDR_HOURS, &hour, ZX234290_RTC_TIME_HOURS_WID, ZX234290_RTC_TIME_HOURS_LSH);
|
| 498 | zx234290_rtc_getvalue(ZX234290_REG_ADDR_DAY, &day, ZX234290_RTC_TIME_DAYS_WID, ZX234290_RTC_TIME_DAYS_LSH);
|
| 499 | zx234290_rtc_getvalue(ZX234290_REG_ADDR_MONTH, &mon, ZX234290_RTC_TIME_MONTHS_WID, ZX234290_RTC_TIME_MONTHS_LSH);
|
| 500 | zx234290_rtc_getvalue(ZX234290_REG_ADDR_YEAR, &year, ZX234290_RTC_TIME_YEAR_WID, ZX234290_RTC_TIME_YEAR_LSH);
|
| 501 | zx234290_rtc_getvalue(ZX234290_REG_ADDR_WEEK, &wday, ZX234290_RTC_TIME_WEEKDAY_WID, ZX234290_RTC_TIME_WEEKDAY_LSH);
|
| 502 | zx234290_rtc_getvalue(ZX234290_REG_ADDR_MONTH, &CenturyIndicate, ZX234290_RTC_TIME_CENTURY_WID, ZX234290_RTC_TIME_CENTURY_LSH);
|
| 503 |
|
| 504 |
|
| 505 | if (sec == 0 && !have_retried)
|
| 506 | {
|
| 507 | have_retried = 1;
|
| 508 | goto retry_get_time;
|
| 509 | }
|
| 510 | rtc_tm->tm_sec = bcd2bin(sec);
|
| 511 | rtc_tm->tm_min = bcd2bin(min);
|
| 512 | rtc_tm->tm_hour = bcd2bin(hour);
|
| 513 | rtc_tm->tm_mday = bcd2bin(day);
|
| 514 | rtc_tm->tm_mon = bcd2bin(mon);
|
| 515 | rtc_tm->tm_year = bcd2bin(year);//+2000;
|
| 516 | rtc_tm->tm_wday = bcd2bin(wday);
|
| 517 | rtc_tm->tm_yday = rtc_year_days(rtc_tm->tm_mday, rtc_tm->tm_mon , rtc_tm->tm_year);
|
| 518 | if(CenturyIndicate == 0)
|
| 519 | {
|
| 520 | rtc_tm->tm_year += 100;
|
| 521 | }
|
| 522 | rtc_tm->tm_mon -= 1;
|
| 523 | rtc_tm->tm_isdst = 0;
|
| 524 | pr_debug("read time %04d.%02d.%02d %02d:%02d:%02d\n",
|
| 525 | rtc_tm->tm_year, rtc_tm->tm_mon+1, rtc_tm->tm_mday,
|
| 526 | rtc_tm->tm_hour, rtc_tm->tm_min, rtc_tm->tm_sec);
|
| 527 |
|
| 528 | return rtc_valid_tm(rtc_tm);
|
| 529 | }
|
| 530 |
|
| 531 | static int zx234290_rtc_settime(struct device *dev, struct rtc_time *tm)
|
| 532 | {
|
| 533 | int ret = 0;
|
| 534 | unsigned char day, mon, year, wday, sec, min, hour, CenturyIndicate;
|
| 535 | unsigned char reg_val=0, mask=0;
|
| 536 | //int year = tm->tm_year - 100;
|
| 537 | pr_debug("set time %04d.%02d.%02d %02d:%02d:%02d\n",
|
| 538 | tm->tm_year, tm->tm_mon+1, tm->tm_mday,
|
| 539 | tm->tm_hour, tm->tm_min, tm->tm_sec);
|
| 540 | if(tm->tm_year >= 200)
|
| 541 | {
|
| 542 | return -1;
|
| 543 |
|
| 544 | }else if(tm->tm_year >= 100)
|
| 545 | {
|
| 546 | year = tm->tm_year - 100;
|
| 547 | CenturyIndicate = 0; /*indicate 21 ÊÀ¼Í 20xx*/
|
| 548 | }
|
| 549 | else
|
| 550 | {
|
| 551 | year = tm->tm_year;
|
| 552 | CenturyIndicate = 1; /*indicate 20 ÊÀ¼Í 19xx*/
|
| 553 | }
|
| 554 | /* we get around y2k by simply not supporting it */
|
| 555 | sec = bin2bcd(tm->tm_sec);
|
| 556 | reg_val = ZX234290_BITFVAL(sec, ZX234290_RTC_TIME_SECONDS_LSH);
|
| 557 | mask = ZX234290_BITFMASK(ZX234290_RTC_TIME_SECONDS_WID, ZX234290_RTC_TIME_SECONDS_LSH);
|
| 558 | ret = zx234290_rtc_write_register(ZX234290_REG_ADDR_SECONDS, reg_val, mask);
|
| 559 |
|
| 560 | min = bin2bcd(tm->tm_min);
|
| 561 | reg_val = ZX234290_BITFVAL(min, ZX234290_RTC_TIME_MINUTES_LSH);
|
| 562 | mask = ZX234290_BITFMASK(ZX234290_RTC_TIME_MINUTES_WID, ZX234290_RTC_TIME_MINUTES_LSH);
|
| 563 | ret += zx234290_rtc_write_register(ZX234290_REG_ADDR_MINUTES, reg_val, mask);
|
| 564 |
|
| 565 | hour = bin2bcd(tm->tm_hour);
|
| 566 | reg_val = ZX234290_BITFVAL(hour, ZX234290_RTC_TIME_HOURS_LSH);
|
| 567 | mask = ZX234290_BITFMASK(ZX234290_RTC_TIME_HOURS_WID, ZX234290_RTC_TIME_HOURS_LSH);
|
| 568 | ret += zx234290_rtc_write_register(ZX234290_REG_ADDR_HOURS, reg_val, mask);
|
| 569 |
|
| 570 | day = bin2bcd(tm->tm_mday);
|
| 571 | reg_val = ZX234290_BITFVAL(day, ZX234290_RTC_TIME_DAYS_LSH);
|
| 572 | mask = ZX234290_BITFMASK(ZX234290_RTC_TIME_DAYS_WID, ZX234290_RTC_TIME_DAYS_LSH);
|
| 573 | ret += zx234290_rtc_write_register(ZX234290_REG_ADDR_DAY, reg_val, mask);
|
| 574 |
|
| 575 | mon = bin2bcd(tm->tm_mon+1);
|
| 576 | reg_val = ZX234290_BITFVAL(mon, ZX234290_RTC_TIME_MONTHS_LSH);
|
| 577 | mask = ZX234290_BITFMASK(ZX234290_RTC_TIME_MONTHS_WID, ZX234290_RTC_TIME_MONTHS_LSH);
|
| 578 | ret += zx234290_rtc_write_register(ZX234290_REG_ADDR_MONTH, reg_val, mask);
|
| 579 |
|
| 580 | reg_val = ZX234290_BITFVAL(CenturyIndicate, ZX234290_RTC_TIME_CENTURY_LSH);
|
| 581 | mask = ZX234290_BITFMASK(ZX234290_RTC_TIME_CENTURY_WID, ZX234290_RTC_TIME_CENTURY_LSH);
|
| 582 | ret += zx234290_rtc_write_register(ZX234290_REG_ADDR_MONTH, reg_val, mask);
|
| 583 |
|
| 584 | year = bin2bcd(year);
|
| 585 | reg_val = ZX234290_BITFVAL(year, ZX234290_RTC_TIME_YEAR_LSH);
|
| 586 | mask = ZX234290_BITFMASK(ZX234290_RTC_TIME_YEAR_WID, ZX234290_RTC_TIME_YEAR_LSH);
|
| 587 | ret += zx234290_rtc_write_register(ZX234290_REG_ADDR_YEAR, reg_val, mask);
|
| 588 |
|
| 589 | wday = bin2bcd(tm->tm_wday);
|
| 590 | reg_val = ZX234290_BITFVAL(wday, ZX234290_RTC_TIME_WEEKDAY_LSH);
|
| 591 | mask = ZX234290_BITFMASK(ZX234290_RTC_TIME_WEEKDAY_WID, ZX234290_RTC_TIME_WEEKDAY_LSH);
|
| 592 | ret += zx234290_rtc_write_register(ZX234290_REG_ADDR_WEEK, reg_val, mask);
|
| 593 |
|
| 594 | return ret;
|
| 595 | }
|
| 596 |
|
| 597 | static int zx234290_rtc_getalarm(struct device *dev, struct rtc_wkalrm *alrm)
|
| 598 | {
|
| 599 | struct rtc_time *alm_tm = &alrm->time;
|
| 600 | unsigned char second, day, wday, min, hour;
|
| 601 | unsigned char reg_val=0, mask=0;
|
| 602 | if(alrm == NULL)
|
| 603 | {
|
| 604 | return -1;
|
| 605 | }
|
| 606 | zx234290_rtc_getvalue(ZX234290_REG_ADDR_ALARM_SECOND, &second, ZX234290_RTC_AlARM_SECOND_WID, ZX234290_RTC_AlARM_SECOND_LSH);
|
| 607 | zx234290_rtc_getvalue(ZX234290_REG_ADDR_ALARM_MINUTE, &min, ZX234290_RTC_AlARM_MINUTES_WID, ZX234290_RTC_AlARM_MINUTES_LSH);
|
| 608 | zx234290_rtc_getvalue(ZX234290_REG_ADDR_ALARM_HOUR, &hour, ZX234290_RTC_AlARM_HOURS_WID, ZX234290_RTC_AlARM_HOURS_LSH);
|
| 609 | zx234290_rtc_getvalue(ZX234290_REG_ADDR_ALARM_DAY, &day, ZX234290_RTC_AlARM_DAYS_WID, ZX234290_RTC_AlARM_DAYS_LSH);
|
| 610 | zx234290_rtc_getvalue(ZX234290_REG_ADDR_ALARM_WEEK, &wday, ZX234290_RTC_AlARM_WEEKDAY_WID, ZX234290_RTC_AlARM_WEEKDAY_LSH);
|
| 611 |
|
| 612 | mask = ZX234290_BITFMASK(ZX234290_RTC_ALARM_INT_EN_WID, ZX234290_RTC_ALARM_INT_EN_LSH);
|
| 613 | zx234290_rtc_read_register(ZX234290_REG_ADDR_RTC_CTRL2, ®_val);
|
| 614 | alrm->enabled = (reg_val & mask)? 1 : 0;
|
| 615 |
|
| 616 | pr_debug("read alarm %d, %04d.%02d.%02d %02d:%02d:%02d\n",
|
| 617 | alrm->enabled,
|
| 618 | 1900 + alm_tm->tm_year, alm_tm->tm_mon+1, alm_tm->tm_mday,
|
| 619 | alm_tm->tm_hour, alm_tm->tm_min, alm_tm->tm_sec);
|
| 620 | /* decode the alarm enable field */
|
| 621 | if(alrm->enabled)
|
| 622 | {
|
| 623 | //alm_tm->tm_sec = 0; //bcd2bin(alm_tm->tm_sec);
|
| 624 | alm_tm->tm_sec = bcd2bin(second);//yuxiang
|
| 625 | alm_tm->tm_min = bcd2bin(min);
|
| 626 | alm_tm->tm_hour = bcd2bin(hour);
|
| 627 | alm_tm->tm_mday = bcd2bin(day);
|
| 628 | alm_tm->tm_wday = bcd2bin(wday);
|
| 629 | }
|
| 630 | else
|
| 631 | {
|
| 632 | alm_tm->tm_sec = -1;
|
| 633 | alm_tm->tm_min = -1;
|
| 634 | alm_tm->tm_hour = -1;
|
| 635 | alm_tm->tm_mday = -1;
|
| 636 | alm_tm->tm_mon = -1;
|
| 637 | alm_tm->tm_year = -1;
|
| 638 | }
|
| 639 |
|
| 640 | return 0;
|
| 641 | }
|
| 642 |
|
| 643 | static int zx234290_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)
|
| 644 | {
|
| 645 | struct rtc_time *tm = &alrm->time;
|
| 646 | //unsigned long expires;
|
| 647 | unsigned char sec, min, hour, mday, wday;
|
| 648 | //rtc_tm_to_time(tm, &expires);
|
| 649 | //expires = roundup(expires, 60);
|
| 650 | //rtc_time_to_tm(expires, tm);
|
| 651 | //tm->tm_sec = 0;
|
| 652 |
|
| 653 | if (tm->tm_sec < 60 && tm->tm_sec >= 0)//yuxiang
|
| 654 | {
|
| 655 | sec = bin2bcd(tm->tm_sec);
|
| 656 | zx234290_rtc_setvalue(ZX234290_REG_ADDR_ALARM_SECOND, sec, ZX234290_RTC_AlARM_SECOND_WID, ZX234290_RTC_AlARM_SECOND_LSH);
|
| 657 | zx234290_rtc_alarm_enable(ALARM_SECOND, 0);
|
| 658 | }
|
| 659 | else
|
| 660 | {
|
| 661 | zx234290_rtc_alarm_enable(ALARM_SECOND, 1);
|
| 662 | }
|
| 663 |
|
| 664 | if (tm->tm_min < 60 && tm->tm_min >= 0)
|
| 665 | {
|
| 666 | min = bin2bcd(tm->tm_min);
|
| 667 | zx234290_rtc_setvalue(ZX234290_REG_ADDR_ALARM_MINUTE, min, ZX234290_RTC_AlARM_MINUTES_WID, ZX234290_RTC_AlARM_MINUTES_LSH);
|
| 668 | zx234290_rtc_alarm_enable(ALARM_MINUTE, 0);
|
| 669 | }
|
| 670 | else
|
| 671 | {
|
| 672 | zx234290_rtc_alarm_enable(ALARM_MINUTE, 1);
|
| 673 | }
|
| 674 | if (tm->tm_hour < 24 && tm->tm_hour >= 0)
|
| 675 | {
|
| 676 | hour = bin2bcd(tm->tm_hour);
|
| 677 | zx234290_rtc_setvalue(ZX234290_REG_ADDR_ALARM_HOUR, hour, ZX234290_RTC_AlARM_HOURS_WID, ZX234290_RTC_AlARM_HOURS_LSH);
|
| 678 | zx234290_rtc_alarm_enable(ALARM_HOUR, 0);
|
| 679 | }
|
| 680 | else
|
| 681 | {
|
| 682 | zx234290_rtc_alarm_enable(ALARM_HOUR, 1);
|
| 683 | }
|
| 684 | if(tm->tm_mday < 32 && tm->tm_mday > 0)
|
| 685 | {
|
| 686 | mday = bin2bcd(tm->tm_mday);
|
| 687 | zx234290_rtc_setvalue(ZX234290_REG_ADDR_ALARM_DAY, mday, ZX234290_RTC_AlARM_DAYS_WID, ZX234290_RTC_AlARM_DAYS_LSH);
|
| 688 | zx234290_rtc_alarm_enable(ALARM_DAY, 0);
|
| 689 | }
|
| 690 | else
|
| 691 | {
|
| 692 | zx234290_rtc_alarm_enable(ALARM_DAY, 1);
|
| 693 | }
|
| 694 | if(tm->tm_wday < 7 && tm->tm_wday >= 0)
|
| 695 | {
|
| 696 | wday = bin2bcd(tm->tm_wday);
|
| 697 | zx234290_rtc_setvalue(ZX234290_REG_ADDR_ALARM_WEEK, wday, ZX234290_RTC_AlARM_WEEKDAY_WID, ZX234290_RTC_AlARM_WEEKDAY_LSH);
|
| 698 | zx234290_rtc_alarm_enable(ALARM_WEEKDAY, 0);
|
| 699 | }
|
| 700 | else
|
| 701 | {
|
| 702 | zx234290_rtc_alarm_enable(ALARM_WEEKDAY, 1);
|
| 703 | }
|
| 704 | zx234290_rtc_setaie(dev, alrm->enabled);
|
| 705 | return 0;
|
| 706 | }
|
| 707 |
|
| 708 | static int zx234290_rtc_request_irq(struct platform_device *pdev, struct rtc_device *rtc)
|
| 709 | {
|
| 710 | int ret = -1;
|
| 711 | //zx234290_rtc.zx234290->irq_base = PMIC_INT_START;
|
| 712 | ret = request_threaded_irq(zx234290_rtc.zx234290->irq_base + ZX234290_INT_RTC_ALRM,
|
| 713 | NULL, zx234290_rtc_alarmirq, 0, "zx234290-rtc alarm", rtc);
|
| 714 | if (ret)
|
| 715 | {
|
| 716 | dev_err(&pdev->dev, "IRQ%d error %d\n", zx234290_rtc.zx234290->irq_base + ZX234290_INT_RTC_ALRM, ret);
|
| 717 | goto err_alarm_irq;
|
| 718 | }
|
| 719 | ret = request_threaded_irq(zx234290_rtc.zx234290->irq_base + ZX234290_INT_RTC_HOUR,
|
| 720 | NULL, zx234290_rtc_hourirq, 0, "zx234290-rtc hour", rtc);
|
| 721 | if (ret)
|
| 722 | {
|
| 723 | dev_err(&pdev->dev, "IRQ%d error %d\n", zx234290_rtc.zx234290->irq_base + ZX234290_INT_RTC_HOUR, ret);
|
| 724 | goto err_hour_irq;
|
| 725 | }
|
| 726 | ret = request_threaded_irq(zx234290_rtc.zx234290->irq_base + ZX234290_INT_RTC_MIN,
|
| 727 | NULL, zx234290_rtc_minirq, 0, "zx234290-rtc min", rtc);
|
| 728 | if (ret)
|
| 729 | {
|
| 730 | dev_err(&pdev->dev, "IRQ%d error %d\n", zx234290_rtc.zx234290->irq_base + ZX234290_INT_RTC_MIN, ret);
|
| 731 | goto err_min_irq;
|
| 732 | }
|
| 733 |
|
| 734 | return 0;
|
| 735 |
|
| 736 | err_min_irq:
|
| 737 | free_irq(zx234290_rtc.zx234290->irq_base + ZX234290_INT_RTC_MIN, rtc);
|
| 738 | err_hour_irq:
|
| 739 | free_irq(zx234290_rtc.zx234290->irq_base + ZX234290_INT_RTC_HOUR, rtc);
|
| 740 | err_alarm_irq:
|
| 741 | free_irq(zx234290_rtc.zx234290->irq_base + ZX234290_INT_RTC_ALRM, rtc);
|
| 742 |
|
| 743 | return ret;
|
| 744 | }
|
| 745 |
|
| 746 | #if 1
|
| 747 |
|
| 748 | static int zx234290_rtc_set_second_alarm_int(struct device *dev, unsigned int enabled)
|
| 749 | {
|
| 750 | int nRet = 0;
|
| 751 |
|
| 752 | int ret = 0;
|
| 753 | int reg_addr=0, mask=0;
|
| 754 | unsigned char reg_val=0;
|
| 755 | int bIsOn = 0;
|
| 756 |
|
| 757 | #if 0
|
| 758 | pr_debug("%s: aie=%d\n", __func__, enabled);
|
| 759 | mask = ZX234290_BITFMASK(ZX234290_RTC_ALARM_INT_EN_WID, ZX234290_RTC_ALARM_INT_EN_LSH);
|
| 760 | reg_val= ZX234290_BITFVAL(enabled, ZX234290_RTC_ALARM_INT_EN_LSH);
|
| 761 | nRet = zx234290_rtc_write_register(ZX234290_REG_ADDR_RTC_CTRL2, reg_val, mask);
|
| 762 | if (nRet != 0)
|
| 763 | {
|
| 764 | return nRet;
|
| 765 | }
|
| 766 | mask = ZX234290_BITFMASK(ZX234290_RTC_ALARM_AF_WID, ZX234290_RTC_ALARM_AF_LSH);
|
| 767 | reg_val= ZX234290_BITFVAL(0, ZX234290_RTC_ALARM_AF_LSH);
|
| 768 | zx234290_rtc_write_register(ZX234290_REG_ADDR_RTC_CTRL2, reg_val, mask);
|
| 769 | mask = ZX234290_BITFMASK(ZX234290_RTC_ALARM_INT_MASK_WID, ZX234290_RTC_ALARM_INT_MASK_LSH);
|
| 770 | if(enabled)
|
| 771 | {
|
| 772 | reg_val= ZX234290_BITFVAL(0, ZX234290_RTC_ALARM_INT_MASK_LSH);
|
| 773 | }
|
| 774 | else
|
| 775 | {
|
| 776 | reg_val= ZX234290_BITFVAL(1, ZX234290_RTC_ALARM_INT_MASK_LSH);
|
| 777 | }
|
| 778 | nRet = zx234290_rtc_write_register(ZX234290_REG_ADDR_INTB_MASK, reg_val, mask);
|
| 779 | #endif
|
| 780 | if(enabled)
|
| 781 | bIsOn = 0;//active
|
| 782 | else
|
| 783 | bIsOn = 1;//inactive
|
| 784 |
|
| 785 | reg_addr = ZX234290_REG_ADDR_INTB_MASK;
|
| 786 | reg_val = ZX234290_BITFVAL(enabled, ZX234290_RTC_ALARM_INT_MASK_LSH);
|
| 787 | mask = ZX234290_BITFMASK(ZX234290_RTC_ALARM_INT_MASK_WID, ZX234290_RTC_ALARM_INT_MASK_LSH);
|
| 788 | ret = zx234290_rtc_write_register(reg_addr,reg_val, mask);
|
| 789 | zx234290_rtc_read_register(0x03, ®_val);
|
| 790 |
|
| 791 | // printk(KERN_INFO "zx234290_rtc_ioctl:0x03 = %x.\n",reg_val);
|
| 792 | #if 0
|
| 793 | reg_addr = ZX234290_REG_ADDR_RTC_CTRL2;
|
| 794 | reg_val = ZX234290_BITFVAL(enabled, ZX234290_RTC_ALARM_INT_EN_LSH);
|
| 795 | mask = ZX234290_BITFMASK(ZX234290_RTC_ALARM_INT_EN_WID, ZX234290_RTC_ALARM_INT_EN_LSH);
|
| 796 | ret = zx234290_rtc_write_register(slv_addr,reg_val, mask);
|
| 797 | reg_addr = ZX234290_REG_ADDR_RTC_CTRL2;
|
| 798 | reg_val = ZX234290_BITFVAL(enabled, 2);
|
| 799 | mask = ZX234290_BITFMASK(1, 2);
|
| 800 | ret = zx234290_rtc_write_register(slv_addr,reg_val, mask);
|
| 801 | #endif
|
| 802 | return nRet;
|
| 803 | }
|
| 804 |
|
| 805 | //static DECLARE_MUTEX(timerSemaphore);
|
| 806 |
|
| 807 |
|
| 808 |
|
| 809 | int zx234290_rtc_gettimer(void)
|
| 810 | {
|
| 811 | int ret = -EPERM;
|
| 812 |
|
| 813 | if(!test_and_set_bit(TIMER_SEMA_WAIT_BIT, &timer_sema_flag))
|
| 814 | ret = down_interruptible(&timerSemaphore);
|
| 815 |
|
| 816 | clear_bit(TIMER_SEMA_WAIT_BIT, &timer_sema_flag);
|
| 817 |
|
| 818 | if(!ret){
|
| 819 | // printk(KERN_INFO "zx234290_rtc_gettimer:get semaphore success.\n");
|
| 820 |
|
| 821 | zx234290_rtc_enable(g_rtc->dev.parent, 0);
|
| 822 | zx234290_rtc_settie(g_rtc->dev.parent, 0);
|
| 823 | zx234290_rtc_set_second_alarm_int(g_rtc->dev.parent, 1);
|
| 824 |
|
| 825 | //zx234290_rtc_set_clock_frequency(TIMER_COUNT_1);
|
| 826 | return 0;
|
| 827 | }
|
| 828 | else{
|
| 829 | // printk(KERN_INFO "zx234290_rtc_gettimer:get semaphore ret is %d.\n", ret);
|
| 830 |
|
| 831 | return ret;
|
| 832 | }
|
| 833 | }
|
| 834 |
|
| 835 | int zx234290_rtc_set_second_timer(int seconds)
|
| 836 | {
|
| 837 | int mask = 0;
|
| 838 | unsigned char reg_val=0;
|
| 839 | //int value = 0;
|
| 840 |
|
| 841 | mask = ZX234290_BITFMASK(ZX234290_RTC_TIMER_INT_EN_WID, 2);
|
| 842 | reg_val= ZX234290_BITFVAL(0, 2);
|
| 843 | zx234290_rtc_write_register(ZX234290_REG_ADDR_RTC_CTRL2, reg_val, mask);
|
| 844 | zx234290_rtc_set_timercounter(seconds);
|
| 845 | zx234290_rtc_set_clock_frequency(TIMER_COUNT_1);
|
| 846 | zx234290_rtc_enable(g_rtc->dev.parent, 1);
|
| 847 | zx234290_rtc_settie(g_rtc->dev.parent, 1);
|
| 848 | zx234290_rtc_set_second_alarm_int(g_rtc->dev.parent, 0);
|
| 849 | #if 0
|
| 850 | zx234290_rtc_read_register(0x01, &value);
|
| 851 | printk(KERN_INFO "zx234290_rtc_ioctl:0x01 = %x.\n",value);
|
| 852 | zx234290_rtc_read_register(0x03, &value);
|
| 853 | printk(KERN_INFO "zx234290_rtc_ioctl:0x03 = %x.\n",value);
|
| 854 | zx234290_rtc_read_register(0x05, &value);
|
| 855 | printk(KERN_INFO "zx234290_rtc_ioctl:0x05 = %x.\n",value);
|
| 856 | zx234290_rtc_read_register(0x30, &value);
|
| 857 | printk(KERN_INFO "zx234290_rtc_ioctl:0x30 = %x.\n",value);
|
| 858 | zx234290_rtc_read_register(0x31, &value);
|
| 859 | printk(KERN_INFO "zx234290_rtc_ioctl:0x31 = %x.\n",value);
|
| 860 | zx234290_rtc_read_register(0x3E, &value);
|
| 861 | printk(KERN_INFO "zx234290_rtc_ioctl:0x3E = %x.\n",value);
|
| 862 | zx234290_rtc_read_register(0x3F, &value);
|
| 863 | printk(KERN_INFO "zx234290_rtc_ioctl:0x3F = %x.\n",value);
|
| 864 | #endif
|
| 865 | return 0;
|
| 866 | }
|
| 867 |
|
| 868 | void printf_rtc_log(void)
|
| 869 | {
|
| 870 | unsigned char value = 0;
|
| 871 |
|
| 872 | zx234290_rtc_read_register(0x01, &value);
|
| 873 | printk(KERN_INFO "printf_rtc_log:0x01 = %x.\n",value);
|
| 874 | zx234290_rtc_read_register(0x03, &value);
|
| 875 | printk(KERN_INFO "printf_rtc_log:0x03 = %x.\n",value);
|
| 876 | zx234290_rtc_read_register(0x05, &value);
|
| 877 | printk(KERN_INFO "printf_rtc_log:0x05 = %x.\n",value);
|
| 878 | zx234290_rtc_read_register(0x30, &value);
|
| 879 | printk(KERN_INFO "printf_rtc_log:0x30 = %x.\n",value);
|
| 880 | zx234290_rtc_read_register(0x31, &value);
|
| 881 | printk(KERN_INFO "printf_rtc_log:0x31 = %x.\n",value);
|
| 882 | zx234290_rtc_read_register(0x3E, &value);
|
| 883 | printk(KERN_INFO "printf_rtc_log:0x3E = %x.\n",value);
|
| 884 | zx234290_rtc_read_register(0x3F, &value);
|
| 885 | printk(KERN_INFO "printf_rtc_log:0x3F = %x.\n",value);
|
| 886 | }
|
| 887 |
|
| 888 | static int zx234290_rtc_set_alarm_enable(struct device *dev, unsigned int enabled)
|
| 889 | {
|
| 890 | int nRet = 0;
|
| 891 | int reg_val=0, mask=0;
|
| 892 |
|
| 893 | mask = ZX234290_BITFMASK(ZX234290_RTC_ALARM_INT_EN_WID, ZX234290_RTC_ALARM_INT_EN_LSH);
|
| 894 | reg_val= ZX234290_BITFVAL(enabled, ZX234290_RTC_ALARM_INT_EN_LSH);
|
| 895 |
|
| 896 | nRet = zx234290_rtc_write_register(ZX234290_REG_ADDR_RTC_CTRL2, reg_val, mask);
|
| 897 |
|
| 898 | mask = ZX234290_BITFMASK(1, 0);
|
| 899 | reg_val= ZX234290_BITFVAL(0, 0);
|
| 900 |
|
| 901 | nRet = zx234290_rtc_write_register(0x03, reg_val, mask);
|
| 902 |
|
| 903 | mask = ZX234290_BITFMASK(2, 2);
|
| 904 | reg_val= ZX234290_BITFVAL(0, 2);
|
| 905 |
|
| 906 | nRet = zx234290_rtc_write_register(0x31, reg_val, mask);
|
| 907 | return 0;
|
| 908 | }
|
| 909 |
|
| 910 |
|
| 911 | int zx234290_rtc_disable_timer_alarm(void)
|
| 912 | {
|
| 913 | zx234290_rtc_enable(g_rtc->dev.parent, 0);
|
| 914 | zx234290_rtc_settie(g_rtc->dev.parent, 0);
|
| 915 | zx234290_rtc_set_alarm_enable(g_rtc->dev.parent, 0);
|
| 916 | zx234290_rtc_alarm_enable(ALARM_MINUTE,1);
|
| 917 | zx234290_rtc_alarm_enable(ALARM_HOUR,1);
|
| 918 | zx234290_rtc_alarm_enable(ALARM_DAY,1);
|
| 919 | zx234290_rtc_alarm_enable(ALARM_WEEKDAY,1);
|
| 920 | return 0;
|
| 921 | }
|
| 922 | EXPORT_SYMBOL_GPL(zx234290_rtc_disable_timer_alarm);
|
| 923 |
|
| 924 | /****get value of rtc timer cnt ****/
|
| 925 | unsigned char zx234290_rtc_gettimer_cnt_remain(void)
|
| 926 | {
|
| 927 | unsigned char timercnt_remain=0;
|
| 928 | zx234290_rtc_getvalue(ZX234290_REG_ADDR_TIMER_CNT, &timercnt_remain, ZX234290_RTC_TIMER_CNT_WID, ZX234290_RTC_TIMER_CNT_LSH);
|
| 929 | return timercnt_remain;
|
| 930 | }
|
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
|
| 935 | static int zx234290_rtc_ioctl(struct device *dev,zx234290_rtc_timer cmd,unsigned long arg)
|
| 936 | {
|
| 937 | int err = 0;
|
| 938 | unsigned int seconds;
|
| 939 | unsigned char timercnt_remain = 0;
|
| 940 | unsigned int seconds_remain = 0;
|
| 941 | void __user *uarg = (void __user *) arg;
|
| 942 | // 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);
|
| 943 | switch(cmd)
|
| 944 | {
|
| 945 | case ZX234290_SET_TIMER:
|
| 946 | {
|
| 947 | seconds = arg;
|
| 948 |
|
| 949 | if(seconds){
|
| 950 | set_bit(TIMER_SEMA_START_BIT, &timer_sema_flag);
|
| 951 |
|
| 952 | timer_remain = seconds;
|
| 953 |
|
| 954 | zx234290_rtc_set_second_timer(min(timer_remain,255));
|
| 955 | timer_remain -= min(timer_remain,255);
|
| 956 | // printk(KERN_INFO "ZX234290_SET_TIMER:set timer=%d, remain=%d.\n", seconds,timer_remain);
|
| 957 | }
|
| 958 |
|
| 959 | else/* if(seconds == 0)*/{
|
| 960 | // printk(KERN_INFO "ZX234290_SET_TIMER(seconds == %ld):timerSemaphore=0x%x.\n",seconds,timerSemaphore);
|
| 961 | zx234290_rtc_enable(g_rtc->dev.parent, 0);
|
| 962 | zx234290_rtc_settie(g_rtc->dev.parent, 0);
|
| 963 | // printf_rtc_log();
|
| 964 | {
|
| 965 | if(test_and_clear_bit(TIMER_SEMA_WAIT_BIT, &timer_sema_flag))
|
| 966 | up(&timerSemaphore);
|
| 967 | }
|
| 968 |
|
| 969 | clear_bit(TIMER_SEMA_START_BIT, &timer_sema_flag);
|
| 970 | }
|
| 971 |
|
| 972 | seconds = 0;
|
| 973 | break;
|
| 974 | }
|
| 975 | #if 1
|
| 976 | case ZX234290_GET_TIMER:
|
| 977 | {
|
| 978 |
|
| 979 | if(test_bit(TIMER_SEMA_START_BIT, &timer_sema_flag)){
|
| 980 | err = zx234290_rtc_gettimer();
|
| 981 | // printk(KERN_INFO "ZX234290_GET_TIMER.ret=%d\n",err);
|
| 982 | }
|
| 983 | //else
|
| 984 | // printk(KERN_INFO "ZX234290_GET_TIMER.has not setting timer, return=%d\n",err);
|
| 985 |
|
| 986 | break;
|
| 987 | }
|
| 988 | #endif
|
| 989 |
|
| 990 | case ZX234290_TIMER_ENABLED:
|
| 991 | {
|
| 992 | seconds = arg;
|
| 993 | // printk(KERN_INFO "ZX234290_TIMER_ENABLED:cmd=%d,senconds=%d.\n", cmd,seconds);
|
| 994 | if(seconds == 0) {
|
| 995 | zx234290_rtc_enable(g_rtc->dev.parent, 0);
|
| 996 | zx234290_rtc_settie(g_rtc->dev.parent, 0);
|
| 997 | // printf_rtc_log();
|
| 998 | if(test_and_clear_bit(TIMER_SEMA_WAIT_BIT, &timer_sema_flag))
|
| 999 | up(&timerSemaphore);
|
| 1000 |
|
| 1001 | clear_bit(TIMER_SEMA_START_BIT, &timer_sema_flag);
|
| 1002 | }
|
| 1003 | break;
|
| 1004 | }
|
| 1005 | case ZX234290_GET_TIMER_REMAIN:
|
| 1006 | {
|
| 1007 | timercnt_remain = zx234290_rtc_gettimer_cnt_remain();
|
| 1008 |
|
| 1009 | seconds_remain = timer_remain + timercnt_remain;
|
| 1010 | // printk(KERN_INFO "ZX234290_GET_TIMER_REMAIN:cmd=%d.remain=%d\n", cmd,seconds_remain);
|
| 1011 | err = put_user(seconds_remain,(unsigned long __user *)uarg);
|
| 1012 | break;
|
| 1013 | }
|
| 1014 | case ZX234290_CYCLE_ENABLE:
|
| 1015 | {
|
| 1016 | seconds = arg;
|
| 1017 | // printk(KERN_INFO "ZX234290_CYCLE_ENABLE:cmd=%d,enable=%d.\n", cmd,seconds);
|
| 1018 | zx234290_rtc_setuie(g_rtc->dev.parent, seconds);
|
| 1019 | break;
|
| 1020 | }
|
| 1021 | default:
|
| 1022 | err=ENOIOCTLCMD;
|
| 1023 | break;
|
| 1024 | }
|
| 1025 | return err;
|
| 1026 | }
|
| 1027 |
|
| 1028 | static int zx234290_rtc_enable(struct device *dev, int enable)
|
| 1029 | {
|
| 1030 | int ret = 0;
|
| 1031 | int reg_val=0, mask=0;
|
| 1032 |
|
| 1033 | reg_val = ZX234290_BITFVAL(enable, ZX234290_RTC_TIMER_EN_LSH);
|
| 1034 | mask = ZX234290_BITFMASK(ZX234290_RTC_TIMER_EN_WID, ZX234290_RTC_TIMER_EN_LSH);
|
| 1035 | ret = zx234290_rtc_write_register(ZX234290_REG_ADDR_TIMER_CTRL, reg_val, mask);
|
| 1036 |
|
| 1037 | return ret;
|
| 1038 | }
|
| 1039 |
|
| 1040 | /* ÉèÖÃRTCÄÚ²¿µ¹¼ÆÊ±¶¨Ê±Æ÷,ÓÃÓÚʵÏֹػúºó¶¨Ê±¿ª»ú */
|
| 1041 | int zx234290_rtc_settimer(int sec)
|
| 1042 | {
|
| 1043 | int mask = 0;
|
| 1044 | unsigned char reg_val=0;
|
| 1045 | //int value = 0;
|
| 1046 |
|
| 1047 | mask = ZX234290_BITFMASK(ZX234290_RTC_TIMER_INT_EN_WID, 2);
|
| 1048 | reg_val= ZX234290_BITFVAL(0, 2);
|
| 1049 | zx234290_rtc_write_register(ZX234290_REG_ADDR_RTC_CTRL2, reg_val, mask);
|
| 1050 | zx234290_rtc_set_timercounter(sec);
|
| 1051 | zx234290_rtc_enable(g_rtc->dev.parent, 1);
|
| 1052 | zx234290_rtc_settie(g_rtc->dev.parent, 1);
|
| 1053 | zx234290_rtc_setaie(g_rtc->dev.parent, 0);
|
| 1054 | #if 0
|
| 1055 | zx234290_rtc_read_register(0x30, &value);
|
| 1056 | printk(KERN_INFO "zx234290_rtc_ioctl:0x30 = %x.\n",value);
|
| 1057 | zx234290_rtc_read_register(0x31, &value);
|
| 1058 | printk(KERN_INFO "zx234290_rtc_ioctl:0x31 = %x.\n",value);
|
| 1059 | zx234290_rtc_read_register(0x3E, &value);
|
| 1060 | printk(KERN_INFO "zx234290_rtc_ioctl:0x3E = %x.\n",value);
|
| 1061 | zx234290_rtc_read_register(0x3F, &value);
|
| 1062 | printk(KERN_INFO "zx234290_rtc_ioctl:0x3F = %x.\n",value);
|
| 1063 | //printk("zx234290_rtc_settimer is called! \n");
|
| 1064 | #endif
|
| 1065 | return 0;
|
| 1066 | }
|
| 1067 | EXPORT_SYMBOL_GPL(zx234290_rtc_settimer);
|
| 1068 |
|
| 1069 | #endif
|
| 1070 |
|
| 1071 | static const struct rtc_class_ops zx234290_rtcops = {
|
| 1072 | .read_time = zx234290_rtc_gettime,
|
| 1073 | .set_time = zx234290_rtc_settime,
|
| 1074 | .read_alarm = zx234290_rtc_getalarm,
|
| 1075 | .set_alarm = zx234290_rtc_setalarm,
|
| 1076 | .alarm_irq_enable = zx234290_rtc_setaie,
|
| 1077 | .ioctl = zx234290_rtc_ioctl,
|
| 1078 | };
|
| 1079 | static int zx234290_rtc_probe(struct platform_device *pdev)
|
| 1080 | {
|
| 1081 | struct rtc_device *rtc;
|
| 1082 | struct rtc_time rtc_tm;
|
| 1083 | int ret;
|
| 1084 | g_rtcdev = pdev;
|
| 1085 | zx234290_rtc.zx234290 = dev_get_drvdata(pdev->dev.parent);
|
| 1086 | if(NULL==zx234290_rtc.zx234290)
|
| 1087 | return -EINVAL;
|
| 1088 |
|
| 1089 | zx234290_rtc_set_timercounter(128);
|
| 1090 | zx234290_rtc_set_clock_frequency(TIMER_COUNT_1);
|
| 1091 | device_init_wakeup(&pdev->dev, 1);
|
| 1092 | rtc = rtc_device_register("zx234290", &pdev->dev, &zx234290_rtcops, THIS_MODULE); /* register RTC and exit */
|
| 1093 | if (IS_ERR(rtc))
|
| 1094 | {
|
| 1095 | dev_err(&pdev->dev, "cannot attach rtc\n");
|
| 1096 | ret = PTR_ERR(rtc);
|
| 1097 | goto err_nortc;
|
| 1098 | }
|
| 1099 | g_rtc = rtc;
|
| 1100 | zx234290_rtc_gettime(NULL, &rtc_tm); /* Check RTC Time */
|
| 1101 | tempTime = rtc_tm;
|
| 1102 | if (rtc_valid_tm(&rtc_tm) < 0)
|
| 1103 | {
|
| 1104 | rtc_tm.tm_year = 100;
|
| 1105 | rtc_tm.tm_mon = 0;
|
| 1106 | rtc_tm.tm_mday = 1;
|
| 1107 | rtc_tm.tm_hour = 0;
|
| 1108 | rtc_tm.tm_min = 0;
|
| 1109 | rtc_tm.tm_sec = 0;
|
| 1110 | rtc_tm.tm_wday= 6;
|
| 1111 | zx234290_rtc_settime(NULL, &rtc_tm);
|
| 1112 | dev_warn(&pdev->dev, "warning: invalid RTC value so initializing it\n");
|
| 1113 | }
|
| 1114 | else
|
| 1115 | {
|
| 1116 | rtc_tm = rtc_ktime_to_tm(rtc_tm_to_ktime(rtc_tm));
|
| 1117 | zx234290_rtc_settime(NULL, &rtc_tm);
|
| 1118 | }
|
| 1119 | rtc->max_user_freq = 64; //32768;//32k clock
|
| 1120 | zx234290_rtc_irqno = zx234290_rtc.zx234290->chip_irq;
|
| 1121 | platform_set_drvdata(pdev, rtc);
|
xf.li | 6c8fc1e | 2023-08-12 00:11:09 -0700 | [diff] [blame] | 1122 | #ifdef _USE_VEHICLE_DC
|
| 1123 | printk("rtc get time only probe ok!\n");
|
| 1124 | #else
|
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1125 | ret = zx234290_rtc_request_irq(pdev, rtc);
|
| 1126 | if (ret)
|
| 1127 | {
|
| 1128 | dev_err(&pdev->dev, "IRQ request error!\n");
|
| 1129 | goto err_irq;
|
| 1130 | }
|
| 1131 | zx234290_rtc_write_register(ZX234290_REG_ADDR_RTC_CTRL1, 0, 0xff);
|
| 1132 | zx234290_rtc_setuie(&pdev->dev, 0);
|
| 1133 | zx234290_rtc_enable(&pdev->dev, 0);
|
xf.li | 6c8fc1e | 2023-08-12 00:11:09 -0700 | [diff] [blame] | 1134 | #endif
|
| 1135 |
|
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1136 | sema_init(&timerSemaphore,0);
|
| 1137 | //zx234290_rtc_ioctl(NULL,ZX234290_SET_TIMER,20);
|
| 1138 | return 0;
|
| 1139 | err_irq:
|
| 1140 | platform_set_drvdata(pdev, NULL);
|
| 1141 | err_nortc:
|
| 1142 | rtc_device_unregister(rtc);
|
| 1143 | return ret;
|
| 1144 | }
|
| 1145 | static int zx234290_rtc_remove(struct platform_device *pdev)
|
| 1146 | {
|
| 1147 | struct rtc_device *rtc = platform_get_drvdata(pdev);
|
| 1148 |
|
| 1149 | platform_set_drvdata(pdev, NULL);
|
| 1150 | rtc_device_unregister(rtc);
|
| 1151 | zx234290_rtc_setuie(&pdev->dev, 0);
|
| 1152 | zx234290_rtc.zx234290 = NULL;
|
| 1153 |
|
| 1154 | return 0;
|
| 1155 | }
|
| 1156 | static int zx234290_rtc_resume(struct platform_device *pdev)
|
| 1157 | {
|
| 1158 | int irq=0;
|
| 1159 | struct zx234290_board *pmic_plat_data = zx234290_rtc.zx234290->dev->platform_data;
|
| 1160 | irq=gpio_to_irq(pmic_plat_data->irq_gpio_num);
|
| 1161 | enable_irq(irq);
|
| 1162 | zx234290_rtc_setuie(&pdev->dev, 1);
|
| 1163 |
|
| 1164 | return 0;
|
| 1165 | }
|
| 1166 | static int zx234290_rtc_suspend(struct platform_device *pdev, pm_message_t state)
|
| 1167 | {
|
| 1168 | int irq=0;
|
| 1169 | struct zx234290_board *pmic_plat_data = zx234290_rtc.zx234290->dev->platform_data;
|
| 1170 | //irq=gpio_to_irq(PIN_PMU_INT);
|
| 1171 | irq=gpio_to_irq(pmic_plat_data->irq_gpio_num);
|
| 1172 | disable_irq_nosync(irq);
|
| 1173 | zx234290_rtc_setuie(&pdev->dev, 0);
|
| 1174 |
|
| 1175 | return 0;
|
| 1176 | }
|
| 1177 |
|
| 1178 |
|
| 1179 | static struct platform_driver zx234290_rtc_driver =
|
| 1180 | {
|
| 1181 | .probe = zx234290_rtc_probe,
|
| 1182 | .remove = zx234290_rtc_remove,
|
| 1183 | .suspend = zx234290_rtc_suspend,
|
| 1184 | .resume = zx234290_rtc_resume,
|
| 1185 | .driver = {
|
| 1186 | .name = "zx234290-rtc",
|
| 1187 | .owner = THIS_MODULE,
|
| 1188 | },
|
| 1189 | };
|
| 1190 |
|
| 1191 |
|
| 1192 | module_platform_driver(zx234290_rtc_driver);
|
| 1193 |
|
| 1194 | MODULE_DESCRIPTION("ZX234290 RTC Driver");
|
| 1195 | MODULE_AUTHOR("yuxiang");
|
| 1196 | MODULE_LICENSE("GPL");
|
| 1197 | MODULE_ALIAS("platform:zx234290-rtc");
|
| 1198 |
|