12 #include <linux/kernel.h>
13 #include <linux/module.h>
16 #include <linux/slab.h>
18 #include <linux/rtc.h>
23 #define VRTC_CALIBRATION
37 #define REG_VRTC_MEAS1 0x7D
39 #define REG0_ADDR 0xB0
40 #define REG1_ADDR 0xB2
41 #define REG2_ADDR 0xB4
42 #define REG3_ADDR 0xB6
44 #define REG0_DATA 0xB1
45 #define REG1_DATA 0xB3
46 #define REG2_DATA 0xB5
47 #define REG3_DATA 0xB7
50 #define MEAS2_VRTC (1 << 0)
53 #define ALARM_EN (1 << 3)
54 #define ALARM_WAKEUP (1 << 4)
55 #define ALARM (1 << 5)
56 #define RTC1_USE_XO (1 << 7)
58 #define VRTC_CALIB_INTERVAL (HZ * 60 * 10)
71 static int pm860x_rtc_alarm_irq_enable(
struct device *
dev,
unsigned int enabled)
89 unsigned long next_time;
90 unsigned long now_time;
102 if (next_time < now_time) {
104 next_time += 60 * 60 * 24;
112 unsigned char buf[8];
116 dev_dbg(info->
dev,
"%x-%x-%x-%x-%x-%x-%x-%x\n", buf[0], buf[1],
117 buf[2], buf[3], buf[4], buf[5], buf[6], buf[7]);
118 base = (buf[1] << 24) | (buf[3] << 16) | (buf[5] << 8) | buf[7];
122 data = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
124 dev_dbg(info->
dev,
"get base:0x%lx, RO count:0x%lx, ticks:0x%lx\n",
132 static int pm860x_rtc_set_time(
struct device *dev,
struct rtc_time *tm)
135 unsigned char buf[4];
139 dev_dbg(info->
dev,
"Set time %d out of range. "
140 "Please set time between 1970 to 2038.\n",
148 data = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
150 dev_dbg(info->
dev,
"set base:0x%lx, RO count:0x%lx, ticks:0x%lx\n",
163 static int pm860x_rtc_read_alarm(
struct device *dev,
struct rtc_wkalrm *alrm)
166 unsigned char buf[8];
171 dev_dbg(info->
dev,
"%x-%x-%x-%x-%x-%x-%x-%x\n", buf[0], buf[1],
172 buf[2], buf[3], buf[4], buf[5], buf[6], buf[7]);
173 base = (buf[1] << 24) | (buf[3] << 16) | (buf[5] << 8) | buf[7];
176 data = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
178 dev_dbg(info->
dev,
"get base:0x%lx, RO count:0x%lx, ticks:0x%lx\n",
188 static int pm860x_rtc_set_alarm(
struct device *dev,
struct rtc_wkalrm *alrm)
193 unsigned char buf[8];
199 dev_dbg(info->
dev,
"%x-%x-%x-%x-%x-%x-%x-%x\n", buf[0], buf[1],
200 buf[2], buf[3], buf[4], buf[5], buf[6], buf[7]);
201 base = (buf[1] << 24) | (buf[3] << 16) | (buf[5] << 8) | buf[7];
205 data = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
207 dev_dbg(info->
dev,
"get base:0x%lx, RO count:0x%lx, ticks:0x%lx\n",
211 rtc_next_alarm_time(&alarm_tm, &now_tm, &alrm->
time);
216 buf[0] = data & 0xff;
217 buf[1] = (data >> 8) & 0xff;
218 buf[2] = (data >> 16) & 0xff;
219 buf[3] = (data >> 24) & 0xff;
233 .read_time = pm860x_rtc_read_time,
234 .set_time = pm860x_rtc_set_time,
235 .read_alarm = pm860x_rtc_read_alarm,
236 .set_alarm = pm860x_rtc_set_alarm,
237 .alarm_irq_enable = pm860x_rtc_alarm_irq_enable,
240 #ifdef VRTC_CALIBRATION
245 unsigned char buf[2];
246 unsigned int sum,
data, mean, vrtc_set;
249 for (i = 0, sum = 0; i < 16; i++) {
252 data = (buf[0] << 4) | buf[1];
253 data = (data * 5400) >> 12;
257 vrtc_set = 2700 + (info->
vrtc & 0x3) * 200;
258 dev_dbg(info->
dev,
"mean:%d, vrtc_set:%d\n", mean, vrtc_set);
262 if ((mean + 200) < vrtc_set) {
266 data = (sum & 0xf8) | (data & 0x3);
268 }
else if ((mean - 200) > vrtc_set) {
272 data = (sum & 0xf8) | (data & 0x3);
276 dev_dbg(info->
dev,
"set 0x%x to RTC_MISC1\n", data);
283 dev_dbg(info->
dev,
"finish VRTC calibration\n");
298 dev_err(&pdev->
dev,
"failed to find rtc node\n");
301 ret = of_property_read_u32(np,
"marvell,88pm860x-vrtc", &info->
vrtc);
307 #define pm860x_rtc_dt_init(x, y) (-1)
316 unsigned long ticks = 0;
319 pdata = pdev->
dev.platform_data;
339 dev_err(chip->
dev,
"Failed to request IRQ: #%d: %d\n",
350 ret = pm860x_rtc_read_time(&pdev->
dev, &tm);
352 dev_err(&pdev->
dev,
"Failed to read initial time.\n");
362 ret = pm860x_rtc_set_time(&pdev->
dev, &tm);
364 dev_err(&pdev->
dev,
"Failed to set initial time.\n");
370 if (pdata && pdata->
sync) {
380 dev_err(&pdev->
dev,
"Failed to register RTC device: %d\n", ret);
390 #ifdef VRTC_CALIBRATION
393 if (pdata && pdata->
vrtc)
419 #ifdef VRTC_CALIBRATION
425 platform_set_drvdata(pdev,
NULL);
432 #ifdef CONFIG_PM_SLEEP
433 static int pm860x_rtc_suspend(
struct device *dev)
438 if (device_may_wakeup(dev))
442 static int pm860x_rtc_resume(
struct device *dev)
447 if (device_may_wakeup(dev))
453 static SIMPLE_DEV_PM_OPS(pm860x_rtc_pm_ops, pm860x_rtc_suspend, pm860x_rtc_resume);
457 .name =
"88pm860x-rtc",
459 .pm = &pm860x_rtc_pm_ops,
461 .probe = pm860x_rtc_probe,