2 #include <minix/drivers.h>
4 #include <minix/i2cdriver.h>
12 /* logging - use with log_warn(), log_info(), log_debug(), log_trace(), etc */
13 static struct log log
= {
14 .name
= "tps65950.rtc",
15 .log_level
= LEVEL_INFO
,
16 .log_func
= default_log
19 static int bcd_to_dec(int n
);
20 static int dec_to_bcd(int n
);
29 r
= i2creg_set_bits8(bus_endpoint
, addresses
[ID4
], RTC_CTRL_REG
,
32 log_warn(&log
, "Failed to start RTC\n");
36 r
= i2creg_read8(bus_endpoint
, addresses
[ID4
], RTC_STATUS_REG
, &val
);
38 log_warn(&log
, "Failed to read RTC_STATUS_REG\n");
42 if ((val
& (1 << RUN_BIT
)) != (1 << RUN_BIT
)) {
43 log_warn(&log
, "RTC did not start. Bad MSECURE?\n");
47 log_debug(&log
, "RTC Started\n");
53 rtc_get_time(struct tm
*t
, int flags
)
58 memset(t
, '\0', sizeof(struct tm
));
60 /* Write GET_TIME_BIT to RTC_CTRL_REG to latch the RTC values into
61 * the RTC registers. This is required before each read.
63 r
= i2creg_set_bits8(bus_endpoint
, addresses
[ID4
], RTC_CTRL_REG
,
69 /* Read and Convert BCD to binary (default RTC mode). */
71 /* Seconds - 0 to 59 */
72 r
= i2creg_read8(bus_endpoint
, addresses
[ID4
], SECONDS_REG
, &val
);
76 t
->tm_sec
= bcd_to_dec(val
& 0x7f);
78 /* Minutes - 0 to 59 */
79 r
= i2creg_read8(bus_endpoint
, addresses
[ID4
], MINUTES_REG
, &val
);
83 t
->tm_min
= bcd_to_dec(val
& 0x7f);
86 r
= i2creg_read8(bus_endpoint
, addresses
[ID4
], HOURS_REG
, &val
);
90 t
->tm_hour
= bcd_to_dec(val
& 0x3f);
93 r
= i2creg_read8(bus_endpoint
, addresses
[ID4
], DAYS_REG
, &val
);
97 t
->tm_mday
= bcd_to_dec(val
& 0x3f);
99 /* Months - Jan=1 to Dec=12 */
100 r
= i2creg_read8(bus_endpoint
, addresses
[ID4
], MONTHS_REG
, &val
);
104 t
->tm_mon
= bcd_to_dec(val
& 0x1f) - 1;
106 /* Years - last 2 digits of year */
107 r
= i2creg_read8(bus_endpoint
, addresses
[ID4
], YEARS_REG
, &val
);
111 t
->tm_year
= bcd_to_dec(val
& 0x1f) + 100;
113 if (t
->tm_year
== 100) {
114 /* Cold start - no date/time set - default to 2013-01-01 */
122 rtc_set_time(t
, RTCDEV_NOFLAGS
);
129 rtc_set_time(struct tm
*t
, int flags
)
133 /* Write the date/time to the RTC registers. */
134 r
= i2creg_write8(bus_endpoint
, addresses
[ID4
], SECONDS_REG
,
135 (dec_to_bcd(t
->tm_sec
) & 0x7f));
140 r
= i2creg_write8(bus_endpoint
, addresses
[ID4
], MINUTES_REG
,
141 (dec_to_bcd(t
->tm_min
) & 0x7f));
146 r
= i2creg_write8(bus_endpoint
, addresses
[ID4
], HOURS_REG
,
147 (dec_to_bcd(t
->tm_hour
) & 0x3f));
152 r
= i2creg_write8(bus_endpoint
, addresses
[ID4
], DAYS_REG
,
153 (dec_to_bcd(t
->tm_mday
) & 0x3f));
158 r
= i2creg_write8(bus_endpoint
, addresses
[ID4
], MONTHS_REG
,
159 (dec_to_bcd(t
->tm_mon
+ 1) & 0x1f));
164 r
= i2creg_write8(bus_endpoint
, addresses
[ID4
], YEARS_REG
,
165 (dec_to_bcd(t
->tm_year
% 100) & 0xff));
182 return ((n
>> 4) & 0x0F) * 10 + (n
& 0x0F);
188 return ((n
/ 10) << 4) | (n
% 10);