Merge branch '2020-01-27-master-imports'
[oweals/u-boot.git] / drivers / rtc / m41t60.c
index 7c80143e6846c9485a0c681890123634697ec39c..532d2105e12c79c871ae654a09159c8639e83292 100644 (file)
@@ -1,3 +1,4 @@
+// SPDX-License-Identifier: GPL-2.0+
 /*
  * (C) Copyright 2007
  * Larry Johnson, lrj@acm.org
@@ -6,21 +7,6 @@
  *
  * (C) Copyright 2002
  * Andrew May, Viasat Inc, amay@viasat.com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2 of
- * the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
- * MA 02111-1307 USA
  */
 
 /*
 
 #include <common.h>
 #include <command.h>
+#include <env.h>
 #include <rtc.h>
 #include <i2c.h>
 
-#if defined(CONFIG_RTC_M41T60) && defined(CFG_I2C_RTC_ADDR) && \
-       defined(CONFIG_CMD_DATE)
-
-static unsigned bcd2bin(uchar n)
-{
-       return ((((n >> 4) & 0x0F) * 10) + (n & 0x0F));
-}
-
-static unsigned char bin2bcd(unsigned int n)
-{
-       return (((n / 10) << 4) | (n % 10));
-}
-
 /*
  * Convert between century and "century bits" (CB1 and CB0).  These routines
  * assume years are in the range 1900 - 2299.
@@ -86,7 +60,7 @@ static void rtc_dump(char const *const label)
 {
        uchar data[8];
 
-       if (i2c_read(CFG_I2C_RTC_ADDR, 0, 1, data, sizeof(data))) {
+       if (i2c_read(CONFIG_SYS_I2C_RTC_ADDR, 0, 1, data, sizeof(data))) {
                printf("I2C read failed in rtc_dump()\n");
                return;
        }
@@ -115,7 +89,7 @@ static uchar *rtc_validate(void)
        uchar min, date, month, years;
 
        rtc_dump("begin validate");
-       if (i2c_read(CFG_I2C_RTC_ADDR, 0, 1, data, sizeof(data))) {
+       if (i2c_read(CONFIG_SYS_I2C_RTC_ADDR, 0, 1, data, sizeof(data))) {
                printf("I2C read failed in rtc_validate()\n");
                return 0;
        }
@@ -126,7 +100,7 @@ static uchar *rtc_validate(void)
        if (0x00 != (data[RTC_CTRL] & 0x80)) {
                printf("M41T60 RTC clock lost power.\n");
                data[RTC_SEC] = 0x80;
-               if (i2c_write(CFG_I2C_RTC_ADDR, RTC_SEC, 1, data, 1)) {
+               if (i2c_write(CONFIG_SYS_I2C_RTC_ADDR, RTC_SEC, 1, data, 1)) {
                        printf("I2C write failed in rtc_validate()\n");
                        return 0;
                }
@@ -162,7 +136,7 @@ static uchar *rtc_validate(void)
                data[RTC_YEAR] = 0x00;
                data[RTC_CTRL] &= 0x7F; /* reset OUT bit */
 
-               if (i2c_write(CFG_I2C_RTC_ADDR, 0, 1, data, sizeof(data))) {
+               if (i2c_write(CONFIG_SYS_I2C_RTC_ADDR, 0, 1, data, sizeof(data))) {
                        printf("I2C write failed in rtc_validate()\n");
                        return 0;
                }
@@ -170,12 +144,12 @@ static uchar *rtc_validate(void)
        return data;
 }
 
-void rtc_get(struct rtc_time *tmp)
+int rtc_get(struct rtc_time *tmp)
 {
        uchar const *const data = rtc_validate();
 
        if (!data)
-               return;
+               return -1;
 
        tmp->tm_sec = bcd2bin(data[RTC_SEC] & 0x7F);
        tmp->tm_min = bcd2bin(data[RTC_MIN] & 0x7F);
@@ -190,14 +164,16 @@ void rtc_get(struct rtc_time *tmp)
        debug("Get DATE: %4d-%02d-%02d (wday=%d)  TIME: %2d:%02d:%02d\n",
              tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
              tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
+
+       return 0;
 }
 
-void rtc_set(struct rtc_time *tmp)
+int rtc_set(struct rtc_time *tmp)
 {
        uchar *const data = rtc_validate();
 
        if (!data)
-               return;
+               return -1;
 
        debug("Set DATE: %4d-%02d-%02d (wday=%d)  TIME: %2d:%02d:%02d\n",
              tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
@@ -211,16 +187,18 @@ void rtc_set(struct rtc_time *tmp)
        data[RTC_YEAR] = bin2bcd(tmp->tm_year % 100);
        data[RTC_MONTH] |= year2cb(tmp->tm_year) << 6;
        data[RTC_DAY] = bin2bcd(tmp->tm_wday + 1) & 0x07;
-       if (i2c_write(CFG_I2C_RTC_ADDR, 0, 1, data, RTC_REG_CNT)) {
+       if (i2c_write(CONFIG_SYS_I2C_RTC_ADDR, 0, 1, data, RTC_REG_CNT)) {
                printf("I2C write failed in rtc_set()\n");
-               return;
+               return -1;
        }
+
+       return 0;
 }
 
 void rtc_reset(void)
 {
        uchar *const data = rtc_validate();
-       char const *const s = getenv("rtccal");
+       char const *const s = env_get("rtccal");
 
        if (!data)
                return;
@@ -239,7 +217,7 @@ void rtc_reset(void)
 
                if (l <= 0x3F) {
                        if ((data[RTC_CTRL] & 0x3F) != l) {
-                               printf("Setting RTC calibration to 0x%02X\n",
+                               printf("Setting RTC calibration to 0x%02lX\n",
                                       l);
                                data[RTC_CTRL] &= 0xC0;
                                data[RTC_CTRL] |= (uchar) l;
@@ -252,10 +230,9 @@ void rtc_reset(void)
         * Turn off frequency test.
         */
        data[RTC_CTRL] &= 0xBF;
-       if (i2c_write(CFG_I2C_RTC_ADDR, RTC_CTRL, 1, data + RTC_CTRL, 1)) {
+       if (i2c_write(CONFIG_SYS_I2C_RTC_ADDR, RTC_CTRL, 1, data + RTC_CTRL, 1)) {
                printf("I2C write failed in rtc_reset()\n");
                return;
        }
        rtc_dump("end reset");
 }
-#endif /* CONFIG_RTC_M41T60 && CFG_I2C_RTC_ADDR && CONFIG_CMD_DATE */