Tested on with the RK808-D on a rk3399 board.
Does this sequence of CTRL_STOP being set/cleared cause the rtc's internal sub-second counters to reset to zero? If so, then after registering the clock in the attach code, you should call clock_schedule(dev, 1), so that rk805_settime() is scheduled to run right after top-of-second so that the rtc's internal phase matches the kernel clock's phase. Some rtc chips restart their counters at the halfway point (.5 sec), in which case you should use something like clock_schedule(dev, 500000000).
There are 2 obvious options for validity detection:
- Bit 7 of RTC_STATUS_REG is set on a reset. That could be read in rk805_gettime() (and cleared in rk805_settime()).
- According to the datasheets, the RK805 resets to Monday, 13-01-21 0850 and the RK808 resets to Thursday, 16-01-21 0850. Since those dates predate support for these devices, a date sanity check could be used - eg if the year reads as less than 19 then the clock isn't valid. This won't any special code to clear the "clock invalid" state because just setting the clock to a sane time will pass that test.
Presumably any board that will accept a battery will have a crystal installed (rather than using the RC fallback that exists in at least the RK805). Detecting that the RTC is not running correctly is a significantly harder task than simply detecting that the time looks sane.
This code assumes that the RTC is configured for direct access to the dynamic time registers (which might imply that there's a race condition reading the time - the datasheets don't mention one way or the other). Since this function is (normally) only called once on boot (from inittodr()), it would seem preferable to transfer the time to the static shadow registers and read them (this requires an additional 1 I2C read and 3 writes).
Actually, the calls to clock_bcd_to_ts() and clock_ts_to_bcd() both assume that the RTC is running in 12h mode. That said, I agree that the code should force the RTC mode to match the time format.
Unfortunately, the neither the RK805 nor RK808 datasheets that I've found on the web document this. I suspect it will need experimentation.
There should probably be a comment that states: