Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * The Netronix embedded controller is a microcontroller found in some
0004  * e-book readers designed by the original design manufacturer Netronix, Inc.
0005  * It contains RTC, battery monitoring, system power management, and PWM
0006  * functionality.
0007  *
0008  * This driver implements access to the RTC time and date.
0009  *
0010  * Copyright 2020 Jonathan Neuschäfer <j.neuschaefer@gmx.net>
0011  */
0012 
0013 #include <linux/mfd/ntxec.h>
0014 #include <linux/module.h>
0015 #include <linux/platform_device.h>
0016 #include <linux/regmap.h>
0017 #include <linux/rtc.h>
0018 #include <linux/types.h>
0019 
0020 struct ntxec_rtc {
0021     struct device *dev;
0022     struct ntxec *ec;
0023 };
0024 
0025 #define NTXEC_REG_WRITE_YEAR    0x10
0026 #define NTXEC_REG_WRITE_MONTH   0x11
0027 #define NTXEC_REG_WRITE_DAY 0x12
0028 #define NTXEC_REG_WRITE_HOUR    0x13
0029 #define NTXEC_REG_WRITE_MINUTE  0x14
0030 #define NTXEC_REG_WRITE_SECOND  0x15
0031 
0032 #define NTXEC_REG_READ_YEAR_MONTH   0x20
0033 #define NTXEC_REG_READ_MDAY_HOUR    0x21
0034 #define NTXEC_REG_READ_MINUTE_SECOND    0x23
0035 
0036 static int ntxec_read_time(struct device *dev, struct rtc_time *tm)
0037 {
0038     struct ntxec_rtc *rtc = dev_get_drvdata(dev);
0039     unsigned int value;
0040     int res;
0041 
0042 retry:
0043     res = regmap_read(rtc->ec->regmap, NTXEC_REG_READ_MINUTE_SECOND, &value);
0044     if (res < 0)
0045         return res;
0046 
0047     tm->tm_min = value >> 8;
0048     tm->tm_sec = value & 0xff;
0049 
0050     res = regmap_read(rtc->ec->regmap, NTXEC_REG_READ_MDAY_HOUR, &value);
0051     if (res < 0)
0052         return res;
0053 
0054     tm->tm_mday = value >> 8;
0055     tm->tm_hour = value & 0xff;
0056 
0057     res = regmap_read(rtc->ec->regmap, NTXEC_REG_READ_YEAR_MONTH, &value);
0058     if (res < 0)
0059         return res;
0060 
0061     tm->tm_year = (value >> 8) + 100;
0062     tm->tm_mon = (value & 0xff) - 1;
0063 
0064     /*
0065      * Read the minutes/seconds field again. If it changed since the first
0066      * read, we can't assume that the values read so far are consistent,
0067      * and should start from the beginning.
0068      */
0069     res = regmap_read(rtc->ec->regmap, NTXEC_REG_READ_MINUTE_SECOND, &value);
0070     if (res < 0)
0071         return res;
0072 
0073     if (tm->tm_min != value >> 8 || tm->tm_sec != (value & 0xff))
0074         goto retry;
0075 
0076     return 0;
0077 }
0078 
0079 static int ntxec_set_time(struct device *dev, struct rtc_time *tm)
0080 {
0081     struct ntxec_rtc *rtc = dev_get_drvdata(dev);
0082 
0083     /*
0084      * To avoid time overflows while we're writing the full date/time,
0085      * set the seconds field to zero before doing anything else. For the
0086      * next 59 seconds (plus however long it takes until the RTC's next
0087      * update of the second field), the seconds field will not overflow
0088      * into the other fields.
0089      */
0090     struct reg_sequence regs[] = {
0091         { NTXEC_REG_WRITE_SECOND, ntxec_reg8(0) },
0092         { NTXEC_REG_WRITE_YEAR, ntxec_reg8(tm->tm_year - 100) },
0093         { NTXEC_REG_WRITE_MONTH, ntxec_reg8(tm->tm_mon + 1) },
0094         { NTXEC_REG_WRITE_DAY, ntxec_reg8(tm->tm_mday) },
0095         { NTXEC_REG_WRITE_HOUR, ntxec_reg8(tm->tm_hour) },
0096         { NTXEC_REG_WRITE_MINUTE, ntxec_reg8(tm->tm_min) },
0097         { NTXEC_REG_WRITE_SECOND, ntxec_reg8(tm->tm_sec) },
0098     };
0099 
0100     return regmap_multi_reg_write(rtc->ec->regmap, regs, ARRAY_SIZE(regs));
0101 }
0102 
0103 static const struct rtc_class_ops ntxec_rtc_ops = {
0104     .read_time = ntxec_read_time,
0105     .set_time = ntxec_set_time,
0106 };
0107 
0108 static int ntxec_rtc_probe(struct platform_device *pdev)
0109 {
0110     struct rtc_device *dev;
0111     struct ntxec_rtc *rtc;
0112 
0113     pdev->dev.of_node = pdev->dev.parent->of_node;
0114 
0115     rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL);
0116     if (!rtc)
0117         return -ENOMEM;
0118 
0119     rtc->dev = &pdev->dev;
0120     rtc->ec = dev_get_drvdata(pdev->dev.parent);
0121     platform_set_drvdata(pdev, rtc);
0122 
0123     dev = devm_rtc_allocate_device(&pdev->dev);
0124     if (IS_ERR(dev))
0125         return PTR_ERR(dev);
0126 
0127     dev->ops = &ntxec_rtc_ops;
0128     dev->range_min = RTC_TIMESTAMP_BEGIN_2000;
0129     dev->range_max = 9025257599LL; /* 2255-12-31 23:59:59 */
0130 
0131     return devm_rtc_register_device(dev);
0132 }
0133 
0134 static struct platform_driver ntxec_rtc_driver = {
0135     .driver = {
0136         .name = "ntxec-rtc",
0137     },
0138     .probe = ntxec_rtc_probe,
0139 };
0140 module_platform_driver(ntxec_rtc_driver);
0141 
0142 MODULE_AUTHOR("Jonathan Neuschäfer <j.neuschaefer@gmx.net>");
0143 MODULE_DESCRIPTION("RTC driver for Netronix EC");
0144 MODULE_LICENSE("GPL");
0145 MODULE_ALIAS("platform:ntxec-rtc");