blob: b34ed63bf051f9d4f4fbbd36a16ae1549b495b7c [file] [log] [blame]
Meng Yi188417c2016-11-30 15:47:31 +08001/*
2 * Copyright (C) 2016 by NXP Semiconductors Inc.
3 * Date & Time support for PCF2127 RTC
4 */
5
6/* #define DEBUG */
7
8#include <common.h>
9#include <command.h>
10#include <dm.h>
11#include <i2c.h>
12#include <rtc.h>
13
Meng Yie21f5c22017-01-09 11:24:51 -070014#define PCF2127_REG_CTRL1 0x00
15#define PCF2127_REG_CTRL2 0x01
16#define PCF2127_REG_CTRL3 0x02
17#define PCF2127_REG_SC 0x03
18#define PCF2127_REG_MN 0x04
19#define PCF2127_REG_HR 0x05
20#define PCF2127_REG_DM 0x06
21#define PCF2127_REG_DW 0x07
22#define PCF2127_REG_MO 0x08
23#define PCF2127_REG_YR 0x09
Meng Yi188417c2016-11-30 15:47:31 +080024
Chuanhua Handb452132019-07-08 11:45:25 +080025static int pcf2127_read_reg(struct udevice *dev, uint offset,
26 u8 *buffer, int len)
27{
28 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
29 struct i2c_msg msg;
30 int ret;
31
32 /* Set the address of the start register to be read */
33 ret = dm_i2c_write(dev, offset, NULL, 0);
34 if (ret < 0)
35 return ret;
36
37 /* Read register's data */
38 msg.addr = chip->chip_addr;
39 msg.flags |= I2C_M_RD;
40 msg.len = len;
41 msg.buf = buffer;
42
43 return dm_i2c_xfer(dev, &msg, 1);
44}
45
Meng Yi188417c2016-11-30 15:47:31 +080046static int pcf2127_rtc_set(struct udevice *dev, const struct rtc_time *tm)
47{
Chuanhua Handb452132019-07-08 11:45:25 +080048 uchar buf[7] = {0};
Meng Yie21f5c22017-01-09 11:24:51 -070049 int i = 0, ret;
Meng Yi188417c2016-11-30 15:47:31 +080050
Meng Yi188417c2016-11-30 15:47:31 +080051 /* hours, minutes and seconds */
52 buf[i++] = bin2bcd(tm->tm_sec);
53 buf[i++] = bin2bcd(tm->tm_min);
54 buf[i++] = bin2bcd(tm->tm_hour);
55 buf[i++] = bin2bcd(tm->tm_mday);
56 buf[i++] = tm->tm_wday & 0x07;
57
58 /* month, 1 - 12 */
Rasmus Villemoes697ab3d2020-05-01 15:24:50 +020059 buf[i++] = bin2bcd(tm->tm_mon);
Meng Yi188417c2016-11-30 15:47:31 +080060
61 /* year */
62 buf[i++] = bin2bcd(tm->tm_year % 100);
63
64 /* write register's data */
Chuanhua Handb452132019-07-08 11:45:25 +080065 ret = dm_i2c_write(dev, PCF2127_REG_SC, buf, i);
Meng Yi188417c2016-11-30 15:47:31 +080066
Meng Yie21f5c22017-01-09 11:24:51 -070067 return ret;
Meng Yi188417c2016-11-30 15:47:31 +080068}
69
70static int pcf2127_rtc_get(struct udevice *dev, struct rtc_time *tm)
71{
Meng Yie21f5c22017-01-09 11:24:51 -070072 int ret = 0;
Meng Yi188417c2016-11-30 15:47:31 +080073 uchar buf[10] = { PCF2127_REG_CTRL1 };
74
Chuanhua Handb452132019-07-08 11:45:25 +080075 ret = pcf2127_read_reg(dev, PCF2127_REG_CTRL1, buf, sizeof(buf));
Meng Yie21f5c22017-01-09 11:24:51 -070076 if (ret < 0)
77 return ret;
Meng Yi188417c2016-11-30 15:47:31 +080078
79 if (buf[PCF2127_REG_CTRL3] & 0x04)
80 puts("### Warning: RTC Low Voltage - date/time not reliable\n");
81
82 tm->tm_sec = bcd2bin(buf[PCF2127_REG_SC] & 0x7F);
83 tm->tm_min = bcd2bin(buf[PCF2127_REG_MN] & 0x7F);
84 tm->tm_hour = bcd2bin(buf[PCF2127_REG_HR] & 0x3F);
85 tm->tm_mday = bcd2bin(buf[PCF2127_REG_DM] & 0x3F);
Rasmus Villemoes697ab3d2020-05-01 15:24:50 +020086 tm->tm_mon = bcd2bin(buf[PCF2127_REG_MO] & 0x1F);
Meng Yi188417c2016-11-30 15:47:31 +080087 tm->tm_year = bcd2bin(buf[PCF2127_REG_YR]) + 1900;
88 if (tm->tm_year < 1970)
89 tm->tm_year += 100; /* assume we are in 1970...2069 */
90 tm->tm_wday = buf[PCF2127_REG_DW] & 0x07;
91 tm->tm_yday = 0;
92 tm->tm_isdst = 0;
93
94 debug("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
95 tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_wday,
96 tm->tm_hour, tm->tm_min, tm->tm_sec);
97
Meng Yie21f5c22017-01-09 11:24:51 -070098 return ret;
Meng Yi188417c2016-11-30 15:47:31 +080099}
100
101static int pcf2127_rtc_reset(struct udevice *dev)
102{
103 /*Doing nothing here*/
Meng Yie21f5c22017-01-09 11:24:51 -0700104
Meng Yi188417c2016-11-30 15:47:31 +0800105 return 0;
106}
107
108static const struct rtc_ops pcf2127_rtc_ops = {
109 .get = pcf2127_rtc_get,
110 .set = pcf2127_rtc_set,
111 .reset = pcf2127_rtc_reset,
112};
113
114static const struct udevice_id pcf2127_rtc_ids[] = {
115 { .compatible = "pcf2127-rtc" },
116 { }
117};
118
119U_BOOT_DRIVER(rtc_pcf2127) = {
120 .name = "rtc-pcf2127",
121 .id = UCLASS_RTC,
122 .of_match = pcf2127_rtc_ids,
123 .ops = &pcf2127_rtc_ops,
124};