22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/slab.h>
28 #include <linux/rtc.h>
30 #define PM800_RTC_COUNTER1 (0xD1)
31 #define PM800_RTC_COUNTER2 (0xD2)
32 #define PM800_RTC_COUNTER3 (0xD3)
33 #define PM800_RTC_COUNTER4 (0xD4)
34 #define PM800_RTC_EXPIRE1_1 (0xD5)
35 #define PM800_RTC_EXPIRE1_2 (0xD6)
36 #define PM800_RTC_EXPIRE1_3 (0xD7)
37 #define PM800_RTC_EXPIRE1_4 (0xD8)
38 #define PM800_RTC_TRIM1 (0xD9)
39 #define PM800_RTC_TRIM2 (0xDA)
40 #define PM800_RTC_TRIM3 (0xDB)
41 #define PM800_RTC_TRIM4 (0xDC)
42 #define PM800_RTC_EXPIRE2_1 (0xDD)
43 #define PM800_RTC_EXPIRE2_2 (0xDE)
44 #define PM800_RTC_EXPIRE2_3 (0xDF)
45 #define PM800_RTC_EXPIRE2_4 (0xE0)
47 #define PM800_POWER_DOWN_LOG1 (0xE5)
48 #define PM800_POWER_DOWN_LOG2 (0xE6)
73 static int pm80x_rtc_alarm_irq_enable(
struct device *
dev,
unsigned int enabled)
93 unsigned long next_time;
94 unsigned long now_time;
106 if (next_time < now_time) {
108 next_time += 60 * 60 * 24;
116 unsigned char buf[4];
119 base = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
120 dev_dbg(info->
dev,
"%x-%x-%x-%x\n", buf[0], buf[1], buf[2], buf[3]);
124 data = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
126 dev_dbg(info->
dev,
"get base:0x%lx, RO count:0x%lx, ticks:0x%lx\n",
132 static int pm80x_rtc_set_time(
struct device *dev,
struct rtc_time *tm)
135 unsigned char buf[4];
139 "Set time %d out of range. Please set time between 1970 to 2038.\n",
147 data = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
149 dev_dbg(info->
dev,
"set base:0x%lx, RO count:0x%lx, ticks:0x%lx\n",
151 buf[0] = base & 0xFF;
152 buf[1] = (base >> 8) & 0xFF;
153 buf[2] = (base >> 16) & 0xFF;
154 buf[3] = (base >> 24) & 0xFF;
160 static int pm80x_rtc_read_alarm(
struct device *dev,
struct rtc_wkalrm *alrm)
163 unsigned char buf[4];
168 base = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
169 dev_dbg(info->
dev,
"%x-%x-%x-%x\n", buf[0], buf[1], buf[2], buf[3]);
172 data = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
174 dev_dbg(info->
dev,
"get base:0x%lx, RO count:0x%lx, ticks:0x%lx\n",
189 unsigned char buf[4];
195 base = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
196 dev_dbg(info->
dev,
"%x-%x-%x-%x\n", buf[0], buf[1], buf[2], buf[3]);
200 data = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
202 dev_dbg(info->
dev,
"get base:0x%lx, RO count:0x%lx, ticks:0x%lx\n",
206 dev_dbg(info->
dev,
"%s, now time : %lu\n", __func__, ticks);
207 rtc_next_alarm_time(&alarm_tm, &now_tm, &alrm->
time);
210 dev_dbg(info->
dev,
"%s, alarm time: %lu\n", __func__, ticks);
213 buf[0] = data & 0xff;
214 buf[1] = (data >> 8) & 0xff;
215 buf[2] = (data >> 16) & 0xff;
216 buf[3] = (data >> 24) & 0xff;
230 .read_time = pm80x_rtc_read_time,
231 .set_time = pm80x_rtc_set_time,
232 .read_alarm = pm80x_rtc_read_alarm,
233 .set_alarm = pm80x_rtc_set_alarm,
234 .alarm_irq_enable = pm80x_rtc_alarm_irq_enable,
238 static int pm80x_rtc_suspend(
struct device *dev)
240 return pm80x_dev_suspend(dev);
243 static int pm80x_rtc_resume(
struct device *dev)
245 return pm80x_dev_resume(dev);
258 unsigned long ticks = 0;
261 pdata = pdev->
dev.platform_data;
287 ret = pm80x_request_irq(chip, info->
irq, rtc_update_handler,
290 dev_err(chip->
dev,
"Failed to request IRQ: #%d: %d\n",
295 ret = pm80x_rtc_read_time(&pdev->
dev, &tm);
297 dev_err(&pdev->
dev,
"Failed to read initial time.\n");
307 ret = pm80x_rtc_set_time(&pdev->
dev, &tm);
309 dev_err(&pdev->
dev,
"Failed to set initial time.\n");
319 dev_err(&pdev->
dev,
"Failed to register RTC device: %d\n", ret);
329 if (pdev->
dev.parent->platform_data) {
330 pm80x_pdata = pdev->
dev.parent->platform_data;
331 pdata = pm80x_pdata->
rtc;
340 pm80x_free_irq(chip, info->
irq, info);
348 platform_set_drvdata(pdev,
NULL);
350 pm80x_free_irq(info->
chip, info->
irq, info);
356 .name =
"88pm80x-rtc",
358 .pm = &pm80x_rtc_pm_ops,
360 .probe = pm80x_rtc_probe,