Merge tag 'for-v2020.04' of https://gitlab.denx.de/u-boot/custodians/u-boot-i2c
[oweals/u-boot.git] / drivers / rtc / rs5c372.c
index 3d1346eaa7a4e44113089f5ee90b3b91acd9e1fb..97ec001aef56f1cecd3540922874e180ea5f1061 100644 (file)
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License version 2 as
  * published by the Free Software Foundation.
- *
- * 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>
@@ -34,7 +24,6 @@
 #include <rtc.h>
 #include <i2c.h>
 
-#if defined(CONFIG_RTC_RS5C372A) && defined(CONFIG_CMD_DATE)
 /*
  * Reads are always done starting with register 15, which requires some
  * jumping-through-hoops to access the data correctly.
@@ -50,8 +39,8 @@ static unsigned int rtc_debug = DEBUG;
 #define rtc_debug 0    /* gcc will remove all the debug code for us */
 #endif
 
-#ifndef CFG_I2C_RTC_ADDR
-#define CFG_I2C_RTC_ADDR 0x32
+#ifndef CONFIG_SYS_I2C_RTC_ADDR
+#define CONFIG_SYS_I2C_RTC_ADDR 0x32
 #endif
 
 #define RS5C372_RAM_SIZE 0x10
@@ -67,9 +56,6 @@ static unsigned int rtc_debug = DEBUG;
 #define HOURS_24(n)    bcd2bin((n) & 0x3F)
 
 
-static uchar bin2bcd (unsigned int n);
-static unsigned bcd2bin (uchar c);
-
 static int setup_done = 0;
 
 static int
@@ -77,7 +63,7 @@ rs5c372_readram(unsigned char *buf, int len)
 {
        int ret;
 
-       ret = i2c_read(CFG_I2C_RTC_ADDR, 0, 0, buf, len);
+       ret = i2c_read(CONFIG_SYS_I2C_RTC_ADDR, 0, 0, buf, len);
        if (ret != 0) {
                printf("%s: failed to read\n", __FUNCTION__);
                return ret;
@@ -117,7 +103,7 @@ rs5c372_enable(void)
        buf[14] = 0; /* reg. 13 */
        buf[15] = 0; /* reg. 14 */
        buf[16] = USE_24HOUR_MODE; /* reg. 15 */
-       ret = i2c_write(CFG_I2C_RTC_ADDR, 0, 0, buf, RS5C372_RAM_SIZE+1);
+       ret = i2c_write(CONFIG_SYS_I2C_RTC_ADDR, 0, 0, buf, RS5C372_RAM_SIZE+1);
        if (ret != 0) {
                printf("%s: failed\n", __FUNCTION__);
                return;
@@ -166,7 +152,7 @@ rs5c372_convert_to_time(struct rtc_time *dt, unsigned char *buf)
 /*
  * Get the current time from the RTC
  */
-void
+int
 rtc_get (struct rtc_time *tmp)
 {
        unsigned char buf[RS5C372_RAM_SIZE];
@@ -176,7 +162,7 @@ rtc_get (struct rtc_time *tmp)
                rs5c372_enable();
 
        if (!setup_done)
-               return;
+               return -1;
 
        memset(buf, 0, sizeof(buf));
 
@@ -184,19 +170,18 @@ rtc_get (struct rtc_time *tmp)
        ret = rs5c372_readram(buf, RS5C372_RAM_SIZE);
        if (ret != 0) {
                printf("%s: failed\n", __FUNCTION__);
-               return;
+               return -1;
        }
 
        rs5c372_convert_to_time(tmp, buf);
 
-       return;
+       return 0;
 }
 
 /*
  * Set the RTC
  */
-void
-rtc_set (struct rtc_time *tmp)
+int rtc_set (struct rtc_time *tmp)
 {
        unsigned char buf[8], reg15;
        int ret;
@@ -205,7 +190,7 @@ rtc_set (struct rtc_time *tmp)
                rs5c372_enable();
 
        if (!setup_done)
-               return;
+               return -1;
 
        if(rtc_debug > 2) {
                printf("rtc_set: tm_year = %d\n", tmp->tm_year);
@@ -219,7 +204,7 @@ rtc_set (struct rtc_time *tmp)
        memset(buf, 0, sizeof(buf));
 
        /* only read register 15 */
-       ret = i2c_read(CFG_I2C_RTC_ADDR, 0, 0, buf, 1);
+       ret = i2c_read(CONFIG_SYS_I2C_RTC_ADDR, 0, 0, buf, 1);
 
        if (ret == 0) {
                /* need to save register 15 */
@@ -248,55 +233,24 @@ rtc_set (struct rtc_time *tmp)
                        printf("WARNING: year should be between 1970 and 2069!\n");
                buf[7] = bin2bcd(tmp->tm_year % 100);
 
-               ret = i2c_write(CFG_I2C_RTC_ADDR, 0, 0, buf, 8);
-               if (ret != 0)
+               ret = i2c_write(CONFIG_SYS_I2C_RTC_ADDR, 0, 0, buf, 8);
+               if (ret != 0) {
                        printf("rs5c372_set_datetime(), i2c_master_send() returned %d\n",ret);
+                       return -1;
+               }
+       } else {
+               return -1;
        }
 
-       return;
+       return 0;
 }
 
 /*
- * Reset the RTC. We set the date back to 1970-01-01.
+ * Reset the RTC.
  */
 void
 rtc_reset (void)
 {
-       struct rtc_time tmp;
-
        if (!setup_done)
                rs5c372_enable();
-
-       if (!setup_done)
-               return;
-
-       tmp.tm_year = 1970;
-       tmp.tm_mon = 1;
-       /* Jan. 1, 1970 was a Thursday */
-       tmp.tm_wday= 4;
-       tmp.tm_mday= 1;
-       tmp.tm_hour = 0;
-       tmp.tm_min = 0;
-       tmp.tm_sec = 0;
-
-       rtc_set(&tmp);
-
-       printf ("RTC:   %4d-%02d-%02d %2d:%02d:%02d UTC\n",
-               tmp.tm_year, tmp.tm_mon, tmp.tm_mday,
-               tmp.tm_hour, tmp.tm_min, tmp.tm_sec);
-
-       return;
 }
-
-static unsigned int
-bcd2bin (unsigned char n)
-{
-       return ((((n >> 4) & 0x0F) * 10) + (n & 0x0F));
-}
-
-static unsigned char
-bin2bcd (unsigned int n)
-{
-       return (((n / 10) << 4) | (n % 10));
-}
-#endif