Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /* drivers/rtc/rtc-max6902.c
0003  *
0004  * Copyright (C) 2006 8D Technologies inc.
0005  * Copyright (C) 2004 Compulab Ltd.
0006  *
0007  * Driver for MAX6902 spi RTC
0008  */
0009 
0010 #include <linux/module.h>
0011 #include <linux/kernel.h>
0012 #include <linux/platform_device.h>
0013 #include <linux/init.h>
0014 #include <linux/rtc.h>
0015 #include <linux/spi/spi.h>
0016 #include <linux/bcd.h>
0017 
0018 #define MAX6902_REG_SECONDS     0x01
0019 #define MAX6902_REG_MINUTES     0x03
0020 #define MAX6902_REG_HOURS       0x05
0021 #define MAX6902_REG_DATE        0x07
0022 #define MAX6902_REG_MONTH       0x09
0023 #define MAX6902_REG_DAY         0x0B
0024 #define MAX6902_REG_YEAR        0x0D
0025 #define MAX6902_REG_CONTROL     0x0F
0026 #define MAX6902_REG_CENTURY     0x13
0027 
0028 static int max6902_set_reg(struct device *dev, unsigned char address,
0029                 unsigned char data)
0030 {
0031     struct spi_device *spi = to_spi_device(dev);
0032     unsigned char buf[2];
0033 
0034     /* MSB must be '0' to write */
0035     buf[0] = address & 0x7f;
0036     buf[1] = data;
0037 
0038     return spi_write_then_read(spi, buf, 2, NULL, 0);
0039 }
0040 
0041 static int max6902_get_reg(struct device *dev, unsigned char address,
0042                 unsigned char *data)
0043 {
0044     struct spi_device *spi = to_spi_device(dev);
0045 
0046     /* Set MSB to indicate read */
0047     *data = address | 0x80;
0048 
0049     return spi_write_then_read(spi, data, 1, data, 1);
0050 }
0051 
0052 static int max6902_read_time(struct device *dev, struct rtc_time *dt)
0053 {
0054     int err, century;
0055     struct spi_device *spi = to_spi_device(dev);
0056     unsigned char buf[8];
0057 
0058     buf[0] = 0xbf;  /* Burst read */
0059 
0060     err = spi_write_then_read(spi, buf, 1, buf, 8);
0061     if (err != 0)
0062         return err;
0063 
0064     /* The chip sends data in this order:
0065      * Seconds, Minutes, Hours, Date, Month, Day, Year */
0066     dt->tm_sec  = bcd2bin(buf[0]);
0067     dt->tm_min  = bcd2bin(buf[1]);
0068     dt->tm_hour = bcd2bin(buf[2]);
0069     dt->tm_mday = bcd2bin(buf[3]);
0070     dt->tm_mon  = bcd2bin(buf[4]) - 1;
0071     dt->tm_wday = bcd2bin(buf[5]);
0072     dt->tm_year = bcd2bin(buf[6]);
0073 
0074     /* Read century */
0075     err = max6902_get_reg(dev, MAX6902_REG_CENTURY, &buf[0]);
0076     if (err != 0)
0077         return err;
0078 
0079     century = bcd2bin(buf[0]) * 100;
0080 
0081     dt->tm_year += century;
0082     dt->tm_year -= 1900;
0083 
0084     return 0;
0085 }
0086 
0087 static int max6902_set_time(struct device *dev, struct rtc_time *dt)
0088 {
0089     dt->tm_year = dt->tm_year + 1900;
0090 
0091     /* Remove write protection */
0092     max6902_set_reg(dev, MAX6902_REG_CONTROL, 0);
0093 
0094     max6902_set_reg(dev, MAX6902_REG_SECONDS, bin2bcd(dt->tm_sec));
0095     max6902_set_reg(dev, MAX6902_REG_MINUTES, bin2bcd(dt->tm_min));
0096     max6902_set_reg(dev, MAX6902_REG_HOURS, bin2bcd(dt->tm_hour));
0097 
0098     max6902_set_reg(dev, MAX6902_REG_DATE, bin2bcd(dt->tm_mday));
0099     max6902_set_reg(dev, MAX6902_REG_MONTH, bin2bcd(dt->tm_mon + 1));
0100     max6902_set_reg(dev, MAX6902_REG_DAY, bin2bcd(dt->tm_wday));
0101     max6902_set_reg(dev, MAX6902_REG_YEAR, bin2bcd(dt->tm_year % 100));
0102     max6902_set_reg(dev, MAX6902_REG_CENTURY, bin2bcd(dt->tm_year / 100));
0103 
0104     /* Compulab used a delay here. However, the datasheet
0105      * does not mention a delay being required anywhere... */
0106     /* delay(2000); */
0107 
0108     /* Write protect */
0109     max6902_set_reg(dev, MAX6902_REG_CONTROL, 0x80);
0110 
0111     return 0;
0112 }
0113 
0114 static const struct rtc_class_ops max6902_rtc_ops = {
0115     .read_time  = max6902_read_time,
0116     .set_time   = max6902_set_time,
0117 };
0118 
0119 static int max6902_probe(struct spi_device *spi)
0120 {
0121     struct rtc_device *rtc;
0122     unsigned char tmp;
0123     int res;
0124 
0125     spi->mode = SPI_MODE_3;
0126     spi->bits_per_word = 8;
0127     spi_setup(spi);
0128 
0129     res = max6902_get_reg(&spi->dev, MAX6902_REG_SECONDS, &tmp);
0130     if (res != 0)
0131         return res;
0132 
0133     rtc = devm_rtc_device_register(&spi->dev, "max6902",
0134                 &max6902_rtc_ops, THIS_MODULE);
0135     if (IS_ERR(rtc))
0136         return PTR_ERR(rtc);
0137 
0138     spi_set_drvdata(spi, rtc);
0139     return 0;
0140 }
0141 
0142 static struct spi_driver max6902_driver = {
0143     .driver = {
0144         .name   = "rtc-max6902",
0145     },
0146     .probe  = max6902_probe,
0147 };
0148 
0149 module_spi_driver(max6902_driver);
0150 
0151 MODULE_DESCRIPTION("max6902 spi RTC driver");
0152 MODULE_AUTHOR("Raphael Assenat");
0153 MODULE_LICENSE("GPL");
0154 MODULE_ALIAS("spi:rtc-max6902");