Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Real Time Clock driver for Marvell 88PM80x PMIC
0004  *
0005  * Copyright (c) 2012 Marvell International Ltd.
0006  *  Wenzeng Chen<wzch@marvell.com>
0007  *  Qiao Zhou <zhouqiao@marvell.com>
0008  */
0009 
0010 #include <linux/kernel.h>
0011 #include <linux/module.h>
0012 #include <linux/slab.h>
0013 #include <linux/regmap.h>
0014 #include <linux/mfd/core.h>
0015 #include <linux/mfd/88pm80x.h>
0016 #include <linux/rtc.h>
0017 
0018 #define PM800_RTC_COUNTER1      (0xD1)
0019 #define PM800_RTC_COUNTER2      (0xD2)
0020 #define PM800_RTC_COUNTER3      (0xD3)
0021 #define PM800_RTC_COUNTER4      (0xD4)
0022 #define PM800_RTC_EXPIRE1_1     (0xD5)
0023 #define PM800_RTC_EXPIRE1_2     (0xD6)
0024 #define PM800_RTC_EXPIRE1_3     (0xD7)
0025 #define PM800_RTC_EXPIRE1_4     (0xD8)
0026 #define PM800_RTC_TRIM1         (0xD9)
0027 #define PM800_RTC_TRIM2         (0xDA)
0028 #define PM800_RTC_TRIM3         (0xDB)
0029 #define PM800_RTC_TRIM4         (0xDC)
0030 #define PM800_RTC_EXPIRE2_1     (0xDD)
0031 #define PM800_RTC_EXPIRE2_2     (0xDE)
0032 #define PM800_RTC_EXPIRE2_3     (0xDF)
0033 #define PM800_RTC_EXPIRE2_4     (0xE0)
0034 
0035 #define PM800_POWER_DOWN_LOG1   (0xE5)
0036 #define PM800_POWER_DOWN_LOG2   (0xE6)
0037 
0038 struct pm80x_rtc_info {
0039     struct pm80x_chip *chip;
0040     struct regmap *map;
0041     struct rtc_device *rtc_dev;
0042     struct device *dev;
0043 
0044     int irq;
0045 };
0046 
0047 static irqreturn_t rtc_update_handler(int irq, void *data)
0048 {
0049     struct pm80x_rtc_info *info = (struct pm80x_rtc_info *)data;
0050     int mask;
0051 
0052     mask = PM800_ALARM | PM800_ALARM_WAKEUP;
0053     regmap_update_bits(info->map, PM800_RTC_CONTROL, mask | PM800_ALARM1_EN,
0054                mask);
0055     rtc_update_irq(info->rtc_dev, 1, RTC_AF);
0056     return IRQ_HANDLED;
0057 }
0058 
0059 static int pm80x_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
0060 {
0061     struct pm80x_rtc_info *info = dev_get_drvdata(dev);
0062 
0063     if (enabled)
0064         regmap_update_bits(info->map, PM800_RTC_CONTROL,
0065                    PM800_ALARM1_EN, PM800_ALARM1_EN);
0066     else
0067         regmap_update_bits(info->map, PM800_RTC_CONTROL,
0068                    PM800_ALARM1_EN, 0);
0069     return 0;
0070 }
0071 
0072 /*
0073  * Calculate the next alarm time given the requested alarm time mask
0074  * and the current time.
0075  */
0076 static void rtc_next_alarm_time(struct rtc_time *next, struct rtc_time *now,
0077                 struct rtc_time *alrm)
0078 {
0079     unsigned long next_time;
0080     unsigned long now_time;
0081 
0082     next->tm_year = now->tm_year;
0083     next->tm_mon = now->tm_mon;
0084     next->tm_mday = now->tm_mday;
0085     next->tm_hour = alrm->tm_hour;
0086     next->tm_min = alrm->tm_min;
0087     next->tm_sec = alrm->tm_sec;
0088 
0089     now_time = rtc_tm_to_time64(now);
0090     next_time = rtc_tm_to_time64(next);
0091 
0092     if (next_time < now_time) {
0093         /* Advance one day */
0094         next_time += 60 * 60 * 24;
0095         rtc_time64_to_tm(next_time, next);
0096     }
0097 }
0098 
0099 static int pm80x_rtc_read_time(struct device *dev, struct rtc_time *tm)
0100 {
0101     struct pm80x_rtc_info *info = dev_get_drvdata(dev);
0102     unsigned char buf[4];
0103     unsigned long ticks, base, data;
0104     regmap_raw_read(info->map, PM800_RTC_EXPIRE2_1, buf, 4);
0105     base = ((unsigned long)buf[3] << 24) | (buf[2] << 16) |
0106         (buf[1] << 8) | buf[0];
0107     dev_dbg(info->dev, "%x-%x-%x-%x\n", buf[0], buf[1], buf[2], buf[3]);
0108 
0109     /* load 32-bit read-only counter */
0110     regmap_raw_read(info->map, PM800_RTC_COUNTER1, buf, 4);
0111     data = ((unsigned long)buf[3] << 24) | (buf[2] << 16) |
0112         (buf[1] << 8) | buf[0];
0113     ticks = base + data;
0114     dev_dbg(info->dev, "get base:0x%lx, RO count:0x%lx, ticks:0x%lx\n",
0115         base, data, ticks);
0116     rtc_time64_to_tm(ticks, tm);
0117     return 0;
0118 }
0119 
0120 static int pm80x_rtc_set_time(struct device *dev, struct rtc_time *tm)
0121 {
0122     struct pm80x_rtc_info *info = dev_get_drvdata(dev);
0123     unsigned char buf[4];
0124     unsigned long ticks, base, data;
0125 
0126     ticks = rtc_tm_to_time64(tm);
0127 
0128     /* load 32-bit read-only counter */
0129     regmap_raw_read(info->map, PM800_RTC_COUNTER1, buf, 4);
0130     data = ((unsigned long)buf[3] << 24) | (buf[2] << 16) |
0131         (buf[1] << 8) | buf[0];
0132     base = ticks - data;
0133     dev_dbg(info->dev, "set base:0x%lx, RO count:0x%lx, ticks:0x%lx\n",
0134         base, data, ticks);
0135     buf[0] = base & 0xFF;
0136     buf[1] = (base >> 8) & 0xFF;
0137     buf[2] = (base >> 16) & 0xFF;
0138     buf[3] = (base >> 24) & 0xFF;
0139     regmap_raw_write(info->map, PM800_RTC_EXPIRE2_1, buf, 4);
0140 
0141     return 0;
0142 }
0143 
0144 static int pm80x_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
0145 {
0146     struct pm80x_rtc_info *info = dev_get_drvdata(dev);
0147     unsigned char buf[4];
0148     unsigned long ticks, base, data;
0149     int ret;
0150 
0151     regmap_raw_read(info->map, PM800_RTC_EXPIRE2_1, buf, 4);
0152     base = ((unsigned long)buf[3] << 24) | (buf[2] << 16) |
0153         (buf[1] << 8) | buf[0];
0154     dev_dbg(info->dev, "%x-%x-%x-%x\n", buf[0], buf[1], buf[2], buf[3]);
0155 
0156     regmap_raw_read(info->map, PM800_RTC_EXPIRE1_1, buf, 4);
0157     data = ((unsigned long)buf[3] << 24) | (buf[2] << 16) |
0158         (buf[1] << 8) | buf[0];
0159     ticks = base + data;
0160     dev_dbg(info->dev, "get base:0x%lx, RO count:0x%lx, ticks:0x%lx\n",
0161         base, data, ticks);
0162 
0163     rtc_time64_to_tm(ticks, &alrm->time);
0164     regmap_read(info->map, PM800_RTC_CONTROL, &ret);
0165     alrm->enabled = (ret & PM800_ALARM1_EN) ? 1 : 0;
0166     alrm->pending = (ret & (PM800_ALARM | PM800_ALARM_WAKEUP)) ? 1 : 0;
0167     return 0;
0168 }
0169 
0170 static int pm80x_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
0171 {
0172     struct pm80x_rtc_info *info = dev_get_drvdata(dev);
0173     struct rtc_time now_tm, alarm_tm;
0174     unsigned long ticks, base, data;
0175     unsigned char buf[4];
0176     int mask;
0177 
0178     regmap_update_bits(info->map, PM800_RTC_CONTROL, PM800_ALARM1_EN, 0);
0179 
0180     regmap_raw_read(info->map, PM800_RTC_EXPIRE2_1, buf, 4);
0181     base = ((unsigned long)buf[3] << 24) | (buf[2] << 16) |
0182         (buf[1] << 8) | buf[0];
0183     dev_dbg(info->dev, "%x-%x-%x-%x\n", buf[0], buf[1], buf[2], buf[3]);
0184 
0185     /* load 32-bit read-only counter */
0186     regmap_raw_read(info->map, PM800_RTC_COUNTER1, buf, 4);
0187     data = ((unsigned long)buf[3] << 24) | (buf[2] << 16) |
0188         (buf[1] << 8) | buf[0];
0189     ticks = base + data;
0190     dev_dbg(info->dev, "get base:0x%lx, RO count:0x%lx, ticks:0x%lx\n",
0191         base, data, ticks);
0192 
0193     rtc_time64_to_tm(ticks, &now_tm);
0194     dev_dbg(info->dev, "%s, now time : %lu\n", __func__, ticks);
0195     rtc_next_alarm_time(&alarm_tm, &now_tm, &alrm->time);
0196     /* get new ticks for alarm in 24 hours */
0197     ticks = rtc_tm_to_time64(&alarm_tm);
0198     dev_dbg(info->dev, "%s, alarm time: %lu\n", __func__, ticks);
0199     data = ticks - base;
0200 
0201     buf[0] = data & 0xff;
0202     buf[1] = (data >> 8) & 0xff;
0203     buf[2] = (data >> 16) & 0xff;
0204     buf[3] = (data >> 24) & 0xff;
0205     regmap_raw_write(info->map, PM800_RTC_EXPIRE1_1, buf, 4);
0206     if (alrm->enabled) {
0207         mask = PM800_ALARM | PM800_ALARM_WAKEUP | PM800_ALARM1_EN;
0208         regmap_update_bits(info->map, PM800_RTC_CONTROL, mask, mask);
0209     } else {
0210         mask = PM800_ALARM | PM800_ALARM_WAKEUP | PM800_ALARM1_EN;
0211         regmap_update_bits(info->map, PM800_RTC_CONTROL, mask,
0212                    PM800_ALARM | PM800_ALARM_WAKEUP);
0213     }
0214     return 0;
0215 }
0216 
0217 static const struct rtc_class_ops pm80x_rtc_ops = {
0218     .read_time = pm80x_rtc_read_time,
0219     .set_time = pm80x_rtc_set_time,
0220     .read_alarm = pm80x_rtc_read_alarm,
0221     .set_alarm = pm80x_rtc_set_alarm,
0222     .alarm_irq_enable = pm80x_rtc_alarm_irq_enable,
0223 };
0224 
0225 #ifdef CONFIG_PM_SLEEP
0226 static int pm80x_rtc_suspend(struct device *dev)
0227 {
0228     return pm80x_dev_suspend(dev);
0229 }
0230 
0231 static int pm80x_rtc_resume(struct device *dev)
0232 {
0233     return pm80x_dev_resume(dev);
0234 }
0235 #endif
0236 
0237 static SIMPLE_DEV_PM_OPS(pm80x_rtc_pm_ops, pm80x_rtc_suspend, pm80x_rtc_resume);
0238 
0239 static int pm80x_rtc_probe(struct platform_device *pdev)
0240 {
0241     struct pm80x_chip *chip = dev_get_drvdata(pdev->dev.parent);
0242     struct pm80x_rtc_pdata *pdata = dev_get_platdata(&pdev->dev);
0243     struct pm80x_rtc_info *info;
0244     struct device_node *node = pdev->dev.of_node;
0245     int ret;
0246 
0247     if (!pdata && !node) {
0248         dev_err(&pdev->dev,
0249             "pm80x-rtc requires platform data or of_node\n");
0250         return -EINVAL;
0251     }
0252 
0253     if (!pdata) {
0254         pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
0255         if (!pdata) {
0256             dev_err(&pdev->dev, "failed to allocate memory\n");
0257             return -ENOMEM;
0258         }
0259     }
0260 
0261     info =
0262         devm_kzalloc(&pdev->dev, sizeof(struct pm80x_rtc_info), GFP_KERNEL);
0263     if (!info)
0264         return -ENOMEM;
0265     info->irq = platform_get_irq(pdev, 0);
0266     if (info->irq < 0) {
0267         ret = -EINVAL;
0268         goto out;
0269     }
0270 
0271     info->chip = chip;
0272     info->map = chip->regmap;
0273     if (!info->map) {
0274         dev_err(&pdev->dev, "no regmap!\n");
0275         ret = -EINVAL;
0276         goto out;
0277     }
0278 
0279     info->dev = &pdev->dev;
0280     dev_set_drvdata(&pdev->dev, info);
0281 
0282     info->rtc_dev = devm_rtc_allocate_device(&pdev->dev);
0283     if (IS_ERR(info->rtc_dev))
0284         return PTR_ERR(info->rtc_dev);
0285 
0286     ret = pm80x_request_irq(chip, info->irq, rtc_update_handler,
0287                 IRQF_ONESHOT, "rtc", info);
0288     if (ret < 0) {
0289         dev_err(chip->dev, "Failed to request IRQ: #%d: %d\n",
0290             info->irq, ret);
0291         goto out;
0292     }
0293 
0294     info->rtc_dev->ops = &pm80x_rtc_ops;
0295     info->rtc_dev->range_max = U32_MAX;
0296 
0297     ret = devm_rtc_register_device(info->rtc_dev);
0298     if (ret)
0299         goto out_rtc;
0300 
0301     /*
0302      * enable internal XO instead of internal 3.25MHz clock since it can
0303      * free running in PMIC power-down state.
0304      */
0305     regmap_update_bits(info->map, PM800_RTC_CONTROL, PM800_RTC1_USE_XO,
0306                PM800_RTC1_USE_XO);
0307 
0308     /* remember whether this power up is caused by PMIC RTC or not */
0309     info->rtc_dev->dev.platform_data = &pdata->rtc_wakeup;
0310 
0311     device_init_wakeup(&pdev->dev, 1);
0312 
0313     return 0;
0314 out_rtc:
0315     pm80x_free_irq(chip, info->irq, info);
0316 out:
0317     return ret;
0318 }
0319 
0320 static int pm80x_rtc_remove(struct platform_device *pdev)
0321 {
0322     struct pm80x_rtc_info *info = platform_get_drvdata(pdev);
0323     pm80x_free_irq(info->chip, info->irq, info);
0324     return 0;
0325 }
0326 
0327 static struct platform_driver pm80x_rtc_driver = {
0328     .driver = {
0329            .name = "88pm80x-rtc",
0330            .pm = &pm80x_rtc_pm_ops,
0331            },
0332     .probe = pm80x_rtc_probe,
0333     .remove = pm80x_rtc_remove,
0334 };
0335 
0336 module_platform_driver(pm80x_rtc_driver);
0337 
0338 MODULE_LICENSE("GPL");
0339 MODULE_DESCRIPTION("Marvell 88PM80x RTC driver");
0340 MODULE_AUTHOR("Qiao Zhou <zhouqiao@marvell.com>");
0341 MODULE_ALIAS("platform:88pm80x-rtc");