0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <linux/kernel.h>
0010 #include <linux/module.h>
0011 #include <linux/of.h>
0012 #include <linux/platform_device.h>
0013 #include <linux/slab.h>
0014 #include <linux/mutex.h>
0015 #include <linux/rtc.h>
0016 #include <linux/delay.h>
0017 #include <linux/mfd/core.h>
0018 #include <linux/mfd/88pm860x.h>
0019
0020 #define VRTC_CALIBRATION
0021
0022 struct pm860x_rtc_info {
0023 struct pm860x_chip *chip;
0024 struct i2c_client *i2c;
0025 struct rtc_device *rtc_dev;
0026 struct device *dev;
0027 struct delayed_work calib_work;
0028
0029 int irq;
0030 int vrtc;
0031 };
0032
0033 #define REG_VRTC_MEAS1 0x7D
0034
0035 #define REG0_ADDR 0xB0
0036 #define REG1_ADDR 0xB2
0037 #define REG2_ADDR 0xB4
0038 #define REG3_ADDR 0xB6
0039
0040 #define REG0_DATA 0xB1
0041 #define REG1_DATA 0xB3
0042 #define REG2_DATA 0xB5
0043 #define REG3_DATA 0xB7
0044
0045
0046 #define MEAS2_VRTC (1 << 0)
0047
0048
0049 #define ALARM_EN (1 << 3)
0050 #define ALARM_WAKEUP (1 << 4)
0051 #define ALARM (1 << 5)
0052 #define RTC1_USE_XO (1 << 7)
0053
0054 #define VRTC_CALIB_INTERVAL (HZ * 60 * 10)
0055
0056 static irqreturn_t rtc_update_handler(int irq, void *data)
0057 {
0058 struct pm860x_rtc_info *info = (struct pm860x_rtc_info *)data;
0059 int mask;
0060
0061 mask = ALARM | ALARM_WAKEUP;
0062 pm860x_set_bits(info->i2c, PM8607_RTC1, mask | ALARM_EN, mask);
0063 rtc_update_irq(info->rtc_dev, 1, RTC_AF);
0064 return IRQ_HANDLED;
0065 }
0066
0067 static int pm860x_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
0068 {
0069 struct pm860x_rtc_info *info = dev_get_drvdata(dev);
0070
0071 if (enabled)
0072 pm860x_set_bits(info->i2c, PM8607_RTC1, ALARM_EN, ALARM_EN);
0073 else
0074 pm860x_set_bits(info->i2c, PM8607_RTC1, ALARM_EN, 0);
0075 return 0;
0076 }
0077
0078 static int pm860x_rtc_read_time(struct device *dev, struct rtc_time *tm)
0079 {
0080 struct pm860x_rtc_info *info = dev_get_drvdata(dev);
0081 unsigned char buf[8];
0082 unsigned long ticks, base, data;
0083
0084 pm860x_page_bulk_read(info->i2c, REG0_ADDR, 8, buf);
0085 dev_dbg(info->dev, "%x-%x-%x-%x-%x-%x-%x-%x\n", buf[0], buf[1],
0086 buf[2], buf[3], buf[4], buf[5], buf[6], buf[7]);
0087 base = ((unsigned long)buf[1] << 24) | (buf[3] << 16) |
0088 (buf[5] << 8) | buf[7];
0089
0090
0091 pm860x_bulk_read(info->i2c, PM8607_RTC_COUNTER1, 4, buf);
0092 data = ((unsigned long)buf[3] << 24) | (buf[2] << 16) |
0093 (buf[1] << 8) | buf[0];
0094 ticks = base + data;
0095 dev_dbg(info->dev, "get base:0x%lx, RO count:0x%lx, ticks:0x%lx\n",
0096 base, data, ticks);
0097
0098 rtc_time64_to_tm(ticks, tm);
0099
0100 return 0;
0101 }
0102
0103 static int pm860x_rtc_set_time(struct device *dev, struct rtc_time *tm)
0104 {
0105 struct pm860x_rtc_info *info = dev_get_drvdata(dev);
0106 unsigned char buf[4];
0107 unsigned long ticks, base, data;
0108
0109 ticks = rtc_tm_to_time64(tm);
0110
0111
0112 pm860x_bulk_read(info->i2c, PM8607_RTC_COUNTER1, 4, buf);
0113 data = ((unsigned long)buf[3] << 24) | (buf[2] << 16) |
0114 (buf[1] << 8) | buf[0];
0115 base = ticks - data;
0116 dev_dbg(info->dev, "set base:0x%lx, RO count:0x%lx, ticks:0x%lx\n",
0117 base, data, ticks);
0118
0119 pm860x_page_reg_write(info->i2c, REG0_DATA, (base >> 24) & 0xFF);
0120 pm860x_page_reg_write(info->i2c, REG1_DATA, (base >> 16) & 0xFF);
0121 pm860x_page_reg_write(info->i2c, REG2_DATA, (base >> 8) & 0xFF);
0122 pm860x_page_reg_write(info->i2c, REG3_DATA, base & 0xFF);
0123
0124 return 0;
0125 }
0126
0127 static int pm860x_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
0128 {
0129 struct pm860x_rtc_info *info = dev_get_drvdata(dev);
0130 unsigned char buf[8];
0131 unsigned long ticks, base, data;
0132 int ret;
0133
0134 pm860x_page_bulk_read(info->i2c, REG0_ADDR, 8, buf);
0135 dev_dbg(info->dev, "%x-%x-%x-%x-%x-%x-%x-%x\n", buf[0], buf[1],
0136 buf[2], buf[3], buf[4], buf[5], buf[6], buf[7]);
0137 base = ((unsigned long)buf[1] << 24) | (buf[3] << 16) |
0138 (buf[5] << 8) | buf[7];
0139
0140 pm860x_bulk_read(info->i2c, PM8607_RTC_EXPIRE1, 4, buf);
0141 data = ((unsigned long)buf[3] << 24) | (buf[2] << 16) |
0142 (buf[1] << 8) | buf[0];
0143 ticks = base + data;
0144 dev_dbg(info->dev, "get base:0x%lx, RO count:0x%lx, ticks:0x%lx\n",
0145 base, data, ticks);
0146
0147 rtc_time64_to_tm(ticks, &alrm->time);
0148 ret = pm860x_reg_read(info->i2c, PM8607_RTC1);
0149 alrm->enabled = (ret & ALARM_EN) ? 1 : 0;
0150 alrm->pending = (ret & (ALARM | ALARM_WAKEUP)) ? 1 : 0;
0151 return 0;
0152 }
0153
0154 static int pm860x_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
0155 {
0156 struct pm860x_rtc_info *info = dev_get_drvdata(dev);
0157 unsigned long ticks, base, data;
0158 unsigned char buf[8];
0159 int mask;
0160
0161 pm860x_set_bits(info->i2c, PM8607_RTC1, ALARM_EN, 0);
0162
0163 pm860x_page_bulk_read(info->i2c, REG0_ADDR, 8, buf);
0164 dev_dbg(info->dev, "%x-%x-%x-%x-%x-%x-%x-%x\n", buf[0], buf[1],
0165 buf[2], buf[3], buf[4], buf[5], buf[6], buf[7]);
0166 base = ((unsigned long)buf[1] << 24) | (buf[3] << 16) |
0167 (buf[5] << 8) | buf[7];
0168
0169 ticks = rtc_tm_to_time64(&alrm->time);
0170 data = ticks - base;
0171
0172 buf[0] = data & 0xff;
0173 buf[1] = (data >> 8) & 0xff;
0174 buf[2] = (data >> 16) & 0xff;
0175 buf[3] = (data >> 24) & 0xff;
0176 pm860x_bulk_write(info->i2c, PM8607_RTC_EXPIRE1, 4, buf);
0177 if (alrm->enabled) {
0178 mask = ALARM | ALARM_WAKEUP | ALARM_EN;
0179 pm860x_set_bits(info->i2c, PM8607_RTC1, mask, mask);
0180 } else {
0181 mask = ALARM | ALARM_WAKEUP | ALARM_EN;
0182 pm860x_set_bits(info->i2c, PM8607_RTC1, mask,
0183 ALARM | ALARM_WAKEUP);
0184 }
0185 return 0;
0186 }
0187
0188 static const struct rtc_class_ops pm860x_rtc_ops = {
0189 .read_time = pm860x_rtc_read_time,
0190 .set_time = pm860x_rtc_set_time,
0191 .read_alarm = pm860x_rtc_read_alarm,
0192 .set_alarm = pm860x_rtc_set_alarm,
0193 .alarm_irq_enable = pm860x_rtc_alarm_irq_enable,
0194 };
0195
0196 #ifdef VRTC_CALIBRATION
0197 static void calibrate_vrtc_work(struct work_struct *work)
0198 {
0199 struct pm860x_rtc_info *info = container_of(work,
0200 struct pm860x_rtc_info, calib_work.work);
0201 unsigned char buf[2];
0202 unsigned int sum, data, mean, vrtc_set;
0203 int i;
0204
0205 for (i = 0, sum = 0; i < 16; i++) {
0206 msleep(100);
0207 pm860x_bulk_read(info->i2c, REG_VRTC_MEAS1, 2, buf);
0208 data = (buf[0] << 4) | buf[1];
0209 data = (data * 5400) >> 12;
0210 sum += data;
0211 }
0212 mean = sum >> 4;
0213 vrtc_set = 2700 + (info->vrtc & 0x3) * 200;
0214 dev_dbg(info->dev, "mean:%d, vrtc_set:%d\n", mean, vrtc_set);
0215
0216 sum = pm860x_reg_read(info->i2c, PM8607_RTC_MISC1);
0217 data = sum & 0x3;
0218 if ((mean + 200) < vrtc_set) {
0219
0220 if (++data == 4)
0221 goto out;
0222 data = (sum & 0xf8) | (data & 0x3);
0223 pm860x_reg_write(info->i2c, PM8607_RTC_MISC1, data);
0224 } else if ((mean - 200) > vrtc_set) {
0225
0226 if (data-- == 0)
0227 goto out;
0228 data = (sum & 0xf8) | (data & 0x3);
0229 pm860x_reg_write(info->i2c, PM8607_RTC_MISC1, data);
0230 } else
0231 goto out;
0232 dev_dbg(info->dev, "set 0x%x to RTC_MISC1\n", data);
0233
0234 schedule_delayed_work(&info->calib_work, VRTC_CALIB_INTERVAL);
0235 return;
0236 out:
0237
0238 pm860x_set_bits(info->i2c, PM8607_MEAS_EN2, MEAS2_VRTC, 0);
0239 dev_dbg(info->dev, "finish VRTC calibration\n");
0240 return;
0241 }
0242 #endif
0243
0244 #ifdef CONFIG_OF
0245 static int pm860x_rtc_dt_init(struct platform_device *pdev,
0246 struct pm860x_rtc_info *info)
0247 {
0248 struct device_node *np = pdev->dev.parent->of_node;
0249 int ret;
0250 if (!np)
0251 return -ENODEV;
0252 np = of_get_child_by_name(np, "rtc");
0253 if (!np) {
0254 dev_err(&pdev->dev, "failed to find rtc node\n");
0255 return -ENODEV;
0256 }
0257 ret = of_property_read_u32(np, "marvell,88pm860x-vrtc", &info->vrtc);
0258 if (ret)
0259 info->vrtc = 0;
0260 of_node_put(np);
0261 return 0;
0262 }
0263 #else
0264 #define pm860x_rtc_dt_init(x, y) do { } while (0)
0265 #endif
0266
0267 static int pm860x_rtc_probe(struct platform_device *pdev)
0268 {
0269 struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent);
0270 struct pm860x_rtc_info *info;
0271 int ret;
0272
0273 info = devm_kzalloc(&pdev->dev, sizeof(struct pm860x_rtc_info),
0274 GFP_KERNEL);
0275 if (!info)
0276 return -ENOMEM;
0277 info->irq = platform_get_irq(pdev, 0);
0278 if (info->irq < 0)
0279 return info->irq;
0280
0281 info->chip = chip;
0282 info->i2c = (chip->id == CHIP_PM8607) ? chip->client : chip->companion;
0283 info->dev = &pdev->dev;
0284 dev_set_drvdata(&pdev->dev, info);
0285
0286 info->rtc_dev = devm_rtc_allocate_device(&pdev->dev);
0287 if (IS_ERR(info->rtc_dev))
0288 return PTR_ERR(info->rtc_dev);
0289
0290 ret = devm_request_threaded_irq(&pdev->dev, info->irq, NULL,
0291 rtc_update_handler, IRQF_ONESHOT, "rtc",
0292 info);
0293 if (ret < 0) {
0294 dev_err(chip->dev, "Failed to request IRQ: #%d: %d\n",
0295 info->irq, ret);
0296 return ret;
0297 }
0298
0299
0300 pm860x_page_reg_write(info->i2c, REG0_ADDR, REG0_DATA);
0301 pm860x_page_reg_write(info->i2c, REG1_ADDR, REG1_DATA);
0302 pm860x_page_reg_write(info->i2c, REG2_ADDR, REG2_DATA);
0303 pm860x_page_reg_write(info->i2c, REG3_ADDR, REG3_DATA);
0304
0305 pm860x_rtc_dt_init(pdev, info);
0306
0307 info->rtc_dev->ops = &pm860x_rtc_ops;
0308 info->rtc_dev->range_max = U32_MAX;
0309
0310 ret = devm_rtc_register_device(info->rtc_dev);
0311 if (ret)
0312 return ret;
0313
0314
0315
0316
0317
0318 pm860x_set_bits(info->i2c, PM8607_RTC1, RTC1_USE_XO, RTC1_USE_XO);
0319
0320 #ifdef VRTC_CALIBRATION
0321
0322 pm860x_set_bits(info->i2c, PM8607_MEAS_EN2, MEAS2_VRTC, MEAS2_VRTC);
0323
0324
0325 INIT_DELAYED_WORK(&info->calib_work, calibrate_vrtc_work);
0326 schedule_delayed_work(&info->calib_work, VRTC_CALIB_INTERVAL);
0327 #endif
0328
0329 device_init_wakeup(&pdev->dev, 1);
0330
0331 return 0;
0332 }
0333
0334 static int pm860x_rtc_remove(struct platform_device *pdev)
0335 {
0336 struct pm860x_rtc_info *info = platform_get_drvdata(pdev);
0337
0338 #ifdef VRTC_CALIBRATION
0339 cancel_delayed_work_sync(&info->calib_work);
0340
0341 pm860x_set_bits(info->i2c, PM8607_MEAS_EN2, MEAS2_VRTC, 0);
0342 #endif
0343
0344 return 0;
0345 }
0346
0347 #ifdef CONFIG_PM_SLEEP
0348 static int pm860x_rtc_suspend(struct device *dev)
0349 {
0350 struct platform_device *pdev = to_platform_device(dev);
0351 struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent);
0352
0353 if (device_may_wakeup(dev))
0354 chip->wakeup_flag |= 1 << PM8607_IRQ_RTC;
0355 return 0;
0356 }
0357 static int pm860x_rtc_resume(struct device *dev)
0358 {
0359 struct platform_device *pdev = to_platform_device(dev);
0360 struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent);
0361
0362 if (device_may_wakeup(dev))
0363 chip->wakeup_flag &= ~(1 << PM8607_IRQ_RTC);
0364 return 0;
0365 }
0366 #endif
0367
0368 static SIMPLE_DEV_PM_OPS(pm860x_rtc_pm_ops, pm860x_rtc_suspend, pm860x_rtc_resume);
0369
0370 static struct platform_driver pm860x_rtc_driver = {
0371 .driver = {
0372 .name = "88pm860x-rtc",
0373 .pm = &pm860x_rtc_pm_ops,
0374 },
0375 .probe = pm860x_rtc_probe,
0376 .remove = pm860x_rtc_remove,
0377 };
0378
0379 module_platform_driver(pm860x_rtc_driver);
0380
0381 MODULE_DESCRIPTION("Marvell 88PM860x RTC driver");
0382 MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
0383 MODULE_LICENSE("GPL");