Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  *
0004  * Driver for ST M41T93 SPI RTC
0005  *
0006  * (c) 2010 Nikolaus Voss, Weinmann Medical GmbH
0007  */
0008 
0009 #include <linux/bcd.h>
0010 #include <linux/kernel.h>
0011 #include <linux/module.h>
0012 #include <linux/platform_device.h>
0013 #include <linux/rtc.h>
0014 #include <linux/spi/spi.h>
0015 
0016 #define M41T93_REG_SSEC         0
0017 #define M41T93_REG_ST_SEC       1
0018 #define M41T93_REG_MIN          2
0019 #define M41T93_REG_CENT_HOUR        3
0020 #define M41T93_REG_WDAY         4
0021 #define M41T93_REG_DAY          5
0022 #define M41T93_REG_MON          6
0023 #define M41T93_REG_YEAR         7
0024 
0025 
0026 #define M41T93_REG_ALM_HOUR_HT      0xc
0027 #define M41T93_REG_FLAGS        0xf
0028 
0029 #define M41T93_FLAG_ST          (1 << 7)
0030 #define M41T93_FLAG_OF          (1 << 2)
0031 #define M41T93_FLAG_BL          (1 << 4)
0032 #define M41T93_FLAG_HT          (1 << 6)
0033 
0034 static inline int m41t93_set_reg(struct spi_device *spi, u8 addr, u8 data)
0035 {
0036     u8 buf[2];
0037 
0038     /* MSB must be '1' to write */
0039     buf[0] = addr | 0x80;
0040     buf[1] = data;
0041 
0042     return spi_write(spi, buf, sizeof(buf));
0043 }
0044 
0045 static int m41t93_set_time(struct device *dev, struct rtc_time *tm)
0046 {
0047     struct spi_device *spi = to_spi_device(dev);
0048     int tmp;
0049     u8 buf[9] = {0x80};        /* write cmd + 8 data bytes */
0050     u8 * const data = &buf[1]; /* ptr to first data byte */
0051 
0052     dev_dbg(dev, "%s secs=%d, mins=%d, "
0053         "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
0054         "write", tm->tm_sec, tm->tm_min,
0055         tm->tm_hour, tm->tm_mday,
0056         tm->tm_mon, tm->tm_year, tm->tm_wday);
0057 
0058     if (tm->tm_year < 100) {
0059         dev_warn(&spi->dev, "unsupported date (before 2000-01-01).\n");
0060         return -EINVAL;
0061     }
0062 
0063     tmp = spi_w8r8(spi, M41T93_REG_FLAGS);
0064     if (tmp < 0)
0065         return tmp;
0066 
0067     if (tmp & M41T93_FLAG_OF) {
0068         dev_warn(&spi->dev, "OF bit is set, resetting.\n");
0069         m41t93_set_reg(spi, M41T93_REG_FLAGS, tmp & ~M41T93_FLAG_OF);
0070 
0071         tmp = spi_w8r8(spi, M41T93_REG_FLAGS);
0072         if (tmp < 0) {
0073             return tmp;
0074         } else if (tmp & M41T93_FLAG_OF) {
0075             /* OF cannot be immediately reset: oscillator has to be
0076              * restarted. */
0077             u8 reset_osc = buf[M41T93_REG_ST_SEC] | M41T93_FLAG_ST;
0078 
0079             dev_warn(&spi->dev,
0080                  "OF bit is still set, kickstarting clock.\n");
0081             m41t93_set_reg(spi, M41T93_REG_ST_SEC, reset_osc);
0082             reset_osc &= ~M41T93_FLAG_ST;
0083             m41t93_set_reg(spi, M41T93_REG_ST_SEC, reset_osc);
0084         }
0085     }
0086 
0087     data[M41T93_REG_SSEC]       = 0;
0088     data[M41T93_REG_ST_SEC]     = bin2bcd(tm->tm_sec);
0089     data[M41T93_REG_MIN]        = bin2bcd(tm->tm_min);
0090     data[M41T93_REG_CENT_HOUR]  = bin2bcd(tm->tm_hour) |
0091                         ((tm->tm_year/100-1) << 6);
0092     data[M41T93_REG_DAY]        = bin2bcd(tm->tm_mday);
0093     data[M41T93_REG_WDAY]       = bin2bcd(tm->tm_wday + 1);
0094     data[M41T93_REG_MON]        = bin2bcd(tm->tm_mon + 1);
0095     data[M41T93_REG_YEAR]       = bin2bcd(tm->tm_year % 100);
0096 
0097     return spi_write(spi, buf, sizeof(buf));
0098 }
0099 
0100 
0101 static int m41t93_get_time(struct device *dev, struct rtc_time *tm)
0102 {
0103     struct spi_device *spi = to_spi_device(dev);
0104     const u8 start_addr = 0;
0105     u8 buf[8];
0106     int century_after_1900;
0107     int tmp;
0108     int ret = 0;
0109 
0110     /* Check status of clock. Two states must be considered:
0111        1. halt bit (HT) is set: the clock is running but update of readout
0112           registers has been disabled due to power failure. This is normal
0113           case after poweron. Time is valid after resetting HT bit.
0114        2. oscillator fail bit (OF) is set: time is invalid.
0115     */
0116     tmp = spi_w8r8(spi, M41T93_REG_ALM_HOUR_HT);
0117     if (tmp < 0)
0118         return tmp;
0119 
0120     if (tmp & M41T93_FLAG_HT) {
0121         dev_dbg(&spi->dev, "HT bit is set, reenable clock update.\n");
0122         m41t93_set_reg(spi, M41T93_REG_ALM_HOUR_HT,
0123                    tmp & ~M41T93_FLAG_HT);
0124     }
0125 
0126     tmp = spi_w8r8(spi, M41T93_REG_FLAGS);
0127     if (tmp < 0)
0128         return tmp;
0129 
0130     if (tmp & M41T93_FLAG_OF) {
0131         ret = -EINVAL;
0132         dev_warn(&spi->dev, "OF bit is set, write time to restart.\n");
0133     }
0134 
0135     if (tmp & M41T93_FLAG_BL)
0136         dev_warn(&spi->dev, "BL bit is set, replace battery.\n");
0137 
0138     /* read actual time/date */
0139     tmp = spi_write_then_read(spi, &start_addr, 1, buf, sizeof(buf));
0140     if (tmp < 0)
0141         return tmp;
0142 
0143     tm->tm_sec  = bcd2bin(buf[M41T93_REG_ST_SEC]);
0144     tm->tm_min  = bcd2bin(buf[M41T93_REG_MIN]);
0145     tm->tm_hour = bcd2bin(buf[M41T93_REG_CENT_HOUR] & 0x3f);
0146     tm->tm_mday = bcd2bin(buf[M41T93_REG_DAY]);
0147     tm->tm_mon  = bcd2bin(buf[M41T93_REG_MON]) - 1;
0148     tm->tm_wday = bcd2bin(buf[M41T93_REG_WDAY] & 0x0f) - 1;
0149 
0150     century_after_1900 = (buf[M41T93_REG_CENT_HOUR] >> 6) + 1;
0151     tm->tm_year = bcd2bin(buf[M41T93_REG_YEAR]) + century_after_1900 * 100;
0152 
0153     dev_dbg(dev, "%s secs=%d, mins=%d, "
0154         "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
0155         "read", tm->tm_sec, tm->tm_min,
0156         tm->tm_hour, tm->tm_mday,
0157         tm->tm_mon, tm->tm_year, tm->tm_wday);
0158 
0159     return ret;
0160 }
0161 
0162 
0163 static const struct rtc_class_ops m41t93_rtc_ops = {
0164     .read_time  = m41t93_get_time,
0165     .set_time   = m41t93_set_time,
0166 };
0167 
0168 static struct spi_driver m41t93_driver;
0169 
0170 static int m41t93_probe(struct spi_device *spi)
0171 {
0172     struct rtc_device *rtc;
0173     int res;
0174 
0175     spi->bits_per_word = 8;
0176     spi_setup(spi);
0177 
0178     res = spi_w8r8(spi, M41T93_REG_WDAY);
0179     if (res < 0 || (res & 0xf8) != 0) {
0180         dev_err(&spi->dev, "not found 0x%x.\n", res);
0181         return -ENODEV;
0182     }
0183 
0184     rtc = devm_rtc_device_register(&spi->dev, m41t93_driver.driver.name,
0185                     &m41t93_rtc_ops, THIS_MODULE);
0186     if (IS_ERR(rtc))
0187         return PTR_ERR(rtc);
0188 
0189     spi_set_drvdata(spi, rtc);
0190 
0191     return 0;
0192 }
0193 
0194 static struct spi_driver m41t93_driver = {
0195     .driver = {
0196         .name   = "rtc-m41t93",
0197     },
0198     .probe  = m41t93_probe,
0199 };
0200 
0201 module_spi_driver(m41t93_driver);
0202 
0203 MODULE_AUTHOR("Nikolaus Voss <n.voss@weinmann.de>");
0204 MODULE_DESCRIPTION("Driver for ST M41T93 SPI RTC");
0205 MODULE_LICENSE("GPL");
0206 MODULE_ALIAS("spi:rtc-m41t93");