Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (C) ST-Ericsson SA 2010
0004  *
0005  * Author: Virupax Sadashivpetimath <virupax.sadashivpetimath@stericsson.com>
0006  *
0007  * RTC clock driver for the RTC part of the AB8500 Power management chip.
0008  * Based on RTC clock driver for the AB3100 Analog Baseband Chip by
0009  * Linus Walleij <linus.walleij@stericsson.com>
0010  */
0011 
0012 #include <linux/module.h>
0013 #include <linux/kernel.h>
0014 #include <linux/init.h>
0015 #include <linux/platform_device.h>
0016 #include <linux/rtc.h>
0017 #include <linux/mfd/abx500.h>
0018 #include <linux/mfd/abx500/ab8500.h>
0019 #include <linux/delay.h>
0020 #include <linux/of.h>
0021 #include <linux/pm_wakeirq.h>
0022 
0023 #define AB8500_RTC_SOFF_STAT_REG    0x00
0024 #define AB8500_RTC_CC_CONF_REG      0x01
0025 #define AB8500_RTC_READ_REQ_REG     0x02
0026 #define AB8500_RTC_WATCH_TSECMID_REG    0x03
0027 #define AB8500_RTC_WATCH_TSECHI_REG 0x04
0028 #define AB8500_RTC_WATCH_TMIN_LOW_REG   0x05
0029 #define AB8500_RTC_WATCH_TMIN_MID_REG   0x06
0030 #define AB8500_RTC_WATCH_TMIN_HI_REG    0x07
0031 #define AB8500_RTC_ALRM_MIN_LOW_REG 0x08
0032 #define AB8500_RTC_ALRM_MIN_MID_REG 0x09
0033 #define AB8500_RTC_ALRM_MIN_HI_REG  0x0A
0034 #define AB8500_RTC_STAT_REG     0x0B
0035 #define AB8500_RTC_BKUP_CHG_REG     0x0C
0036 #define AB8500_RTC_FORCE_BKUP_REG   0x0D
0037 #define AB8500_RTC_CALIB_REG        0x0E
0038 #define AB8500_RTC_SWITCH_STAT_REG  0x0F
0039 
0040 /* RtcReadRequest bits */
0041 #define RTC_READ_REQUEST        0x01
0042 #define RTC_WRITE_REQUEST       0x02
0043 
0044 /* RtcCtrl bits */
0045 #define RTC_ALARM_ENA           0x04
0046 #define RTC_STATUS_DATA         0x01
0047 
0048 #define COUNTS_PER_SEC          (0xF000 / 60)
0049 
0050 static const u8 ab8500_rtc_time_regs[] = {
0051     AB8500_RTC_WATCH_TMIN_HI_REG, AB8500_RTC_WATCH_TMIN_MID_REG,
0052     AB8500_RTC_WATCH_TMIN_LOW_REG, AB8500_RTC_WATCH_TSECHI_REG,
0053     AB8500_RTC_WATCH_TSECMID_REG
0054 };
0055 
0056 static const u8 ab8500_rtc_alarm_regs[] = {
0057     AB8500_RTC_ALRM_MIN_HI_REG, AB8500_RTC_ALRM_MIN_MID_REG,
0058     AB8500_RTC_ALRM_MIN_LOW_REG
0059 };
0060 
0061 static int ab8500_rtc_read_time(struct device *dev, struct rtc_time *tm)
0062 {
0063     unsigned long timeout = jiffies + HZ;
0064     int retval, i;
0065     unsigned long mins, secs;
0066     unsigned char buf[ARRAY_SIZE(ab8500_rtc_time_regs)];
0067     u8 value;
0068 
0069     /* Request a data read */
0070     retval = abx500_set_register_interruptible(dev,
0071         AB8500_RTC, AB8500_RTC_READ_REQ_REG, RTC_READ_REQUEST);
0072     if (retval < 0)
0073         return retval;
0074 
0075     /* Wait for some cycles after enabling the rtc read in ab8500 */
0076     while (time_before(jiffies, timeout)) {
0077         retval = abx500_get_register_interruptible(dev,
0078             AB8500_RTC, AB8500_RTC_READ_REQ_REG, &value);
0079         if (retval < 0)
0080             return retval;
0081 
0082         if (!(value & RTC_READ_REQUEST))
0083             break;
0084 
0085         usleep_range(1000, 5000);
0086     }
0087 
0088     /* Read the Watchtime registers */
0089     for (i = 0; i < ARRAY_SIZE(ab8500_rtc_time_regs); i++) {
0090         retval = abx500_get_register_interruptible(dev,
0091             AB8500_RTC, ab8500_rtc_time_regs[i], &value);
0092         if (retval < 0)
0093             return retval;
0094         buf[i] = value;
0095     }
0096 
0097     mins = (buf[0] << 16) | (buf[1] << 8) | buf[2];
0098 
0099     secs =  (buf[3] << 8) | buf[4];
0100     secs =  secs / COUNTS_PER_SEC;
0101     secs =  secs + (mins * 60);
0102 
0103     rtc_time64_to_tm(secs, tm);
0104     return 0;
0105 }
0106 
0107 static int ab8500_rtc_set_time(struct device *dev, struct rtc_time *tm)
0108 {
0109     int retval, i;
0110     unsigned char buf[ARRAY_SIZE(ab8500_rtc_time_regs)];
0111     unsigned long no_secs, no_mins, secs = 0;
0112 
0113     secs = rtc_tm_to_time64(tm);
0114 
0115     no_mins = secs / 60;
0116 
0117     no_secs = secs % 60;
0118     /* Make the seconds count as per the RTC resolution */
0119     no_secs = no_secs * COUNTS_PER_SEC;
0120 
0121     buf[4] = no_secs & 0xFF;
0122     buf[3] = (no_secs >> 8) & 0xFF;
0123 
0124     buf[2] = no_mins & 0xFF;
0125     buf[1] = (no_mins >> 8) & 0xFF;
0126     buf[0] = (no_mins >> 16) & 0xFF;
0127 
0128     for (i = 0; i < ARRAY_SIZE(ab8500_rtc_time_regs); i++) {
0129         retval = abx500_set_register_interruptible(dev, AB8500_RTC,
0130             ab8500_rtc_time_regs[i], buf[i]);
0131         if (retval < 0)
0132             return retval;
0133     }
0134 
0135     /* Request a data write */
0136     return abx500_set_register_interruptible(dev, AB8500_RTC,
0137         AB8500_RTC_READ_REQ_REG, RTC_WRITE_REQUEST);
0138 }
0139 
0140 static int ab8500_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
0141 {
0142     int retval, i;
0143     u8 rtc_ctrl, value;
0144     unsigned char buf[ARRAY_SIZE(ab8500_rtc_alarm_regs)];
0145     unsigned long secs, mins;
0146 
0147     /* Check if the alarm is enabled or not */
0148     retval = abx500_get_register_interruptible(dev, AB8500_RTC,
0149         AB8500_RTC_STAT_REG, &rtc_ctrl);
0150     if (retval < 0)
0151         return retval;
0152 
0153     if (rtc_ctrl & RTC_ALARM_ENA)
0154         alarm->enabled = 1;
0155     else
0156         alarm->enabled = 0;
0157 
0158     alarm->pending = 0;
0159 
0160     for (i = 0; i < ARRAY_SIZE(ab8500_rtc_alarm_regs); i++) {
0161         retval = abx500_get_register_interruptible(dev, AB8500_RTC,
0162             ab8500_rtc_alarm_regs[i], &value);
0163         if (retval < 0)
0164             return retval;
0165         buf[i] = value;
0166     }
0167 
0168     mins = (buf[0] << 16) | (buf[1] << 8) | (buf[2]);
0169     secs = mins * 60;
0170 
0171     rtc_time64_to_tm(secs, &alarm->time);
0172 
0173     return 0;
0174 }
0175 
0176 static int ab8500_rtc_irq_enable(struct device *dev, unsigned int enabled)
0177 {
0178     return abx500_mask_and_set_register_interruptible(dev, AB8500_RTC,
0179         AB8500_RTC_STAT_REG, RTC_ALARM_ENA,
0180         enabled ? RTC_ALARM_ENA : 0);
0181 }
0182 
0183 static int ab8500_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
0184 {
0185     int retval, i;
0186     unsigned char buf[ARRAY_SIZE(ab8500_rtc_alarm_regs)];
0187     unsigned long mins;
0188 
0189     mins = (unsigned long)rtc_tm_to_time64(&alarm->time) / 60;
0190 
0191     buf[2] = mins & 0xFF;
0192     buf[1] = (mins >> 8) & 0xFF;
0193     buf[0] = (mins >> 16) & 0xFF;
0194 
0195     /* Set the alarm time */
0196     for (i = 0; i < ARRAY_SIZE(ab8500_rtc_alarm_regs); i++) {
0197         retval = abx500_set_register_interruptible(dev, AB8500_RTC,
0198             ab8500_rtc_alarm_regs[i], buf[i]);
0199         if (retval < 0)
0200             return retval;
0201     }
0202 
0203     return ab8500_rtc_irq_enable(dev, alarm->enabled);
0204 }
0205 
0206 static int ab8500_rtc_set_calibration(struct device *dev, int calibration)
0207 {
0208     int retval;
0209     u8  rtccal = 0;
0210 
0211     /*
0212      * Check that the calibration value (which is in units of 0.5
0213      * parts-per-million) is in the AB8500's range for RtcCalibration
0214      * register. -128 (0x80) is not permitted because the AB8500 uses
0215      * a sign-bit rather than two's complement, so 0x80 is just another
0216      * representation of zero.
0217      */
0218     if ((calibration < -127) || (calibration > 127)) {
0219         dev_err(dev, "RtcCalibration value outside permitted range\n");
0220         return -EINVAL;
0221     }
0222 
0223     /*
0224      * The AB8500 uses sign (in bit7) and magnitude (in bits0-7)
0225      * so need to convert to this sort of representation before writing
0226      * into RtcCalibration register...
0227      */
0228     if (calibration >= 0)
0229         rtccal = 0x7F & calibration;
0230     else
0231         rtccal = ~(calibration - 1) | 0x80;
0232 
0233     retval = abx500_set_register_interruptible(dev, AB8500_RTC,
0234             AB8500_RTC_CALIB_REG, rtccal);
0235 
0236     return retval;
0237 }
0238 
0239 static int ab8500_rtc_get_calibration(struct device *dev, int *calibration)
0240 {
0241     int retval;
0242     u8  rtccal = 0;
0243 
0244     retval =  abx500_get_register_interruptible(dev, AB8500_RTC,
0245             AB8500_RTC_CALIB_REG, &rtccal);
0246     if (retval >= 0) {
0247         /*
0248          * The AB8500 uses sign (in bit7) and magnitude (in bits0-7)
0249          * so need to convert value from RtcCalibration register into
0250          * a two's complement signed value...
0251          */
0252         if (rtccal & 0x80)
0253             *calibration = 0 - (rtccal & 0x7F);
0254         else
0255             *calibration = 0x7F & rtccal;
0256     }
0257 
0258     return retval;
0259 }
0260 
0261 static ssize_t ab8500_sysfs_store_rtc_calibration(struct device *dev,
0262                 struct device_attribute *attr,
0263                 const char *buf, size_t count)
0264 {
0265     int retval;
0266     int calibration = 0;
0267 
0268     if (sscanf(buf, " %i ", &calibration) != 1) {
0269         dev_err(dev, "Failed to store RTC calibration attribute\n");
0270         return -EINVAL;
0271     }
0272 
0273     retval = ab8500_rtc_set_calibration(dev, calibration);
0274 
0275     return retval ? retval : count;
0276 }
0277 
0278 static ssize_t ab8500_sysfs_show_rtc_calibration(struct device *dev,
0279                 struct device_attribute *attr, char *buf)
0280 {
0281     int  retval = 0;
0282     int  calibration = 0;
0283 
0284     retval = ab8500_rtc_get_calibration(dev, &calibration);
0285     if (retval < 0) {
0286         dev_err(dev, "Failed to read RTC calibration attribute\n");
0287         sprintf(buf, "0\n");
0288         return retval;
0289     }
0290 
0291     return sprintf(buf, "%d\n", calibration);
0292 }
0293 
0294 static DEVICE_ATTR(rtc_calibration, S_IRUGO | S_IWUSR,
0295            ab8500_sysfs_show_rtc_calibration,
0296            ab8500_sysfs_store_rtc_calibration);
0297 
0298 static struct attribute *ab8500_rtc_attrs[] = {
0299     &dev_attr_rtc_calibration.attr,
0300     NULL
0301 };
0302 
0303 static const struct attribute_group ab8500_rtc_sysfs_files = {
0304     .attrs  = ab8500_rtc_attrs,
0305 };
0306 
0307 static irqreturn_t rtc_alarm_handler(int irq, void *data)
0308 {
0309     struct rtc_device *rtc = data;
0310     unsigned long events = RTC_IRQF | RTC_AF;
0311 
0312     dev_dbg(&rtc->dev, "%s\n", __func__);
0313     rtc_update_irq(rtc, 1, events);
0314 
0315     return IRQ_HANDLED;
0316 }
0317 
0318 static const struct rtc_class_ops ab8500_rtc_ops = {
0319     .read_time      = ab8500_rtc_read_time,
0320     .set_time       = ab8500_rtc_set_time,
0321     .read_alarm     = ab8500_rtc_read_alarm,
0322     .set_alarm      = ab8500_rtc_set_alarm,
0323     .alarm_irq_enable   = ab8500_rtc_irq_enable,
0324 };
0325 
0326 static const struct platform_device_id ab85xx_rtc_ids[] = {
0327     { "ab8500-rtc", (kernel_ulong_t)&ab8500_rtc_ops, },
0328     { /* sentinel */ }
0329 };
0330 MODULE_DEVICE_TABLE(platform, ab85xx_rtc_ids);
0331 
0332 static int ab8500_rtc_probe(struct platform_device *pdev)
0333 {
0334     const struct platform_device_id *platid = platform_get_device_id(pdev);
0335     int err;
0336     struct rtc_device *rtc;
0337     u8 rtc_ctrl;
0338     int irq;
0339 
0340     irq = platform_get_irq_byname(pdev, "ALARM");
0341     if (irq < 0)
0342         return irq;
0343 
0344     /* For RTC supply test */
0345     err = abx500_mask_and_set_register_interruptible(&pdev->dev, AB8500_RTC,
0346         AB8500_RTC_STAT_REG, RTC_STATUS_DATA, RTC_STATUS_DATA);
0347     if (err < 0)
0348         return err;
0349 
0350     /* Wait for reset by the PorRtc */
0351     usleep_range(1000, 5000);
0352 
0353     err = abx500_get_register_interruptible(&pdev->dev, AB8500_RTC,
0354         AB8500_RTC_STAT_REG, &rtc_ctrl);
0355     if (err < 0)
0356         return err;
0357 
0358     /* Check if the RTC Supply fails */
0359     if (!(rtc_ctrl & RTC_STATUS_DATA)) {
0360         dev_err(&pdev->dev, "RTC supply failure\n");
0361         return -ENODEV;
0362     }
0363 
0364     device_init_wakeup(&pdev->dev, true);
0365 
0366     rtc = devm_rtc_allocate_device(&pdev->dev);
0367     if (IS_ERR(rtc))
0368         return PTR_ERR(rtc);
0369 
0370     rtc->ops = (struct rtc_class_ops *)platid->driver_data;
0371 
0372     err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
0373             rtc_alarm_handler, IRQF_ONESHOT,
0374             "ab8500-rtc", rtc);
0375     if (err < 0)
0376         return err;
0377 
0378     dev_pm_set_wake_irq(&pdev->dev, irq);
0379     platform_set_drvdata(pdev, rtc);
0380 
0381     set_bit(RTC_FEATURE_ALARM_RES_MINUTE, rtc->features);
0382     clear_bit(RTC_FEATURE_UPDATE_INTERRUPT, rtc->features);
0383 
0384     rtc->range_max = (1ULL << 24) * 60 - 1; // 24-bit minutes + 59 secs
0385     rtc->start_secs = RTC_TIMESTAMP_BEGIN_2000;
0386     rtc->set_start_time = true;
0387 
0388     err = rtc_add_group(rtc, &ab8500_rtc_sysfs_files);
0389     if (err)
0390         return err;
0391 
0392     return devm_rtc_register_device(rtc);
0393 }
0394 
0395 static int ab8500_rtc_remove(struct platform_device *pdev)
0396 {
0397     dev_pm_clear_wake_irq(&pdev->dev);
0398     device_init_wakeup(&pdev->dev, false);
0399 
0400     return 0;
0401 }
0402 
0403 static struct platform_driver ab8500_rtc_driver = {
0404     .driver = {
0405         .name = "ab8500-rtc",
0406     },
0407     .probe  = ab8500_rtc_probe,
0408     .remove = ab8500_rtc_remove,
0409     .id_table = ab85xx_rtc_ids,
0410 };
0411 
0412 module_platform_driver(ab8500_rtc_driver);
0413 
0414 MODULE_AUTHOR("Virupax Sadashivpetimath <virupax.sadashivpetimath@stericsson.com>");
0415 MODULE_DESCRIPTION("AB8500 RTC Driver");
0416 MODULE_LICENSE("GPL v2");