\documentclass[aspectratio=169,obeyspaces,spaces,hyphens,dvipsnames]{beamer}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}% http://ctan.org/pkg/lm
\usepackage{minted}
\usepackage{hyperref}
\usepackage{xcolor}
\usepackage{pgfplots}
\usepackage{tikz}
\usepackage[normalem]{ulem}
\usepackage{wrapfig}

\mode<presentation>
\usetheme{Bootlin}

\def\signed #1{{\leavevmode\unskip\nobreak\hfil\penalty50\hskip2em
  \hbox{}\nobreak\hfil(#1)
  \parfillskip=0pt \finalhyphendemerits=0 \endgraf}}

\newsavebox\mybox
\newenvironment{aquote}[1]
  {\savebox\mybox{#1}\begin{quotation}}
  {\signed{\usebox\mybox}\end{quotation}}

\title{RTC subsystem, recent changes and where it is heading}
\authors{Alexandre Belloni}
\email{alexandre.belloni@bootlin.com}
\slidesurl{https://bootlin.com/pub/conferences/2019/elce/belloni-rtc}
\institute{Bootlin}
\conference{ELCE 2019}

\begin{document}

\addtocontents{toc}{\protect\setcounter{tocdepth}{-1}}
\section{RTC subsystem, recent changes and where it is heading}
\addtocontents{toc}{\protect\setcounter{tocdepth}{2}}

\begin{frame}{Alexandre Belloni}
  \begin{itemize}
  \item Embedded Linux engineer at Bootlin
    \begin{itemize}
    \item Embedded Linux {\bf expertise}
    \item {\bf Development}, consulting and training
    \item Strong open-source focus
    \end{itemize}
  \item Open-source contributor
      \begin{itemize}
      \item Maintainer for the Linux kernel {\bf RTC subsystem}
      \item Co-Maintainer of {\bf kernel support for Microchip ARM
        SoCs}
      \end{itemize}
  \end{itemize}
\end{frame}

\begin{frame}{The RTC subsystem}
  \begin{itemize}
  \item Introduced in 2.6.17 (March 2006) by Alessandro Zummo
  \item Taken over by Alexandre Belloni since 4.1 (April 2015)
  \item Mailing list was on googlegroups until Google decided that it
    was sending to much spam on the 4th of May 2017.
  \item linux-rtc@vger.kernel.org created on 11th of May 2017
  \item Archives are at \url{https://lore.kernel.org/linux-rtc/}
  \item Patchwork at \url{http://patchwork.ozlabs.org/project/rtc-linux/list/}
  \item git repository on kernel.org: \url{git://git.kernel.org/pub/scm/linux/kernel/git/abelloni/linux.git}
  \end{itemize}
\end{frame}

\begin{frame}{Mailing list statistics}
  \begin{center}
    \includegraphics[height=0.8\textheight]{emails-stats.pdf}
  \end{center}
\end{frame}

\begin{frame}{Commits statistics}
  %3.10: use devm_*() functions, convert spear_rtc_driver to
  %  dev_pm_ops, use devm_rtc_device_register(), se
  %  module_platform_driver_probe(), add __init/__exit annotation
  %3.11: remove unnecessary platform_set_drvdata(), remove empty function
  \begin{center}
    \includegraphics[height=0.8\textheight]{commits-stats.pdf}
  \end{center}
\end{frame}

\begin{frame}{Recent changes: new drivers since 4.0}
    \fontsize{8}{6}\selectfont
  \begin{columns}
    \column{0.5\textwidth}
    \begin{itemize}
    \item Abracon AB-RTCMC-32.768kHz-EOZ9
    \item Alphascale ASM9260
    \item Amlogic Meson RTC
    \item Amlogic Virtual Wake
    \item Android emulator (goldfish) RTC
    \item Aspeed BMC SoC RTC
    \item Broadcom STB wake-timer
    \item Cadence RTC IP
    \item Chrome OS EC RTC
    \item Cortina Gemini
    \item Dialog DA9062
    \item Epson RX6110SA
    \item Epson RX8010SJ
    \item Epson RX8130CE
    \item Epson Toyocom rtc-7301sf/dg
    \item Freescale FlexTimer Module alarm
    \item Freescale i.MX system controller RTC
    \item Intersil ISL12026
    \end{itemize}
    \column{0.5\textwidth}
    \begin{itemize}
    \item Maxim IC DS1308
    \item Maxim MAX77620
    \item Maxim MAX6916
    \item Mediatek MT6397
    \item Mediatek MT7622 RTC
    \item Microchip PIC32
    \item Microcrystal RV1805
    \item Microcrystal RV3028
    \item Microcrystal RV8803
    \item Motorola CPCAP PMIC RTC
    \item NXP i.MX53 SRTC
    \item NXP LPC24xx
    \item NXP PCF2127/PCF2129
    \item NXP PCF85263/PCF85363
    \item Realtek RTD1295
    \item Spreadtrum SC27xx PMIC RTC
    \item STMicroelectronics STM32
    \item Whwave sd3078
    \item Xilinx Zynq MP
    \end{itemize}
  \end{columns}
\end{frame}

\begin{frame}{Recent changes: crystal offset}
  \begin{itemize}
  \item The RTC has either an in-package or external crystal, usually
    running 32kHz.
  \item There is variance in the exact frequency because of
    manufacturing, temperature and aging.
  \item Some RTCs can add or subtract correction pulses to make
    seconds quicker or longer.
  \item New sysfs interface \code{/sys/class/rtc/rtcX/offset}
  \item The unit is part per billion, positive value adds pulses and
    negative values subtracts pulses.
  \item The name is coming from the NXP datasheets, it is also called
    digital trimming.
  \end{itemize}
\end{frame}

\begin{frame}{Recent changes: crystal offset}
  \begin{itemize}
  \item An other way to accommodate crystal variance is to change the
    load capacitance.
  \item Some RTC oscillators can change the apparent load on X1/X2
  \item New device tree property \code{quartz-load-femtofarads}
  \item It is also called analog trimming.
  \end{itemize}
\end{frame}

\begin{frame}{Recent changes: non volatile memory}
  \begin{itemize}
  \item Some RTCs have a small amount of RAM.
  \item Because the RTC is always on, the RAM is non volatile.
  \item This memory is now exposed using the \code{nvmem} framework.
  \item New registration function: \code{int rtc_nvmem_register(struct rtc_device *rtc, struct nvmem_config *nvmem_config)}
  \item Multiple calls are allowed so if EEPROM is available, it can
    also be exposed through \code{nvmem}.
  \item \code{rtc->nvram_old_abi} handles the legacy ABI for drivers
    that were exposing a file named \code{nvram} in sysfs. It issues
    deprecation warning.

  \end{itemize}
\end{frame}

\begin{frame}{Recent changes: rtc registration}
  \begin{itemize}
  \item With \code{devm_rtc_device_register}, it was difficult to
    write a probe function without race conditions.
  \item The registration is now split in two parts:
    \code{devm_rtc_allocate_device} and \code{rtc_register_device}.
  \item This allows to handle the \code{struct rtc_device} before
    registering it.
  \item Non devm managed registration has been removed from the driver
    API.
  \end{itemize}
\end{frame}

\begin{frame}{Recent changes: sysfs attributes registration}
  \begin{itemize}
  \item sysfs attributes were created after
    \code{devm_rtc_device_register}. This opened up a possible race
    condition because the rtc folder appeared before the extra
    attributes.
  \item New functions: \code{int rtc_add_group(struct rtc_device *rtc,
    const struct attribute_group *grp)} and \code{int
    rtc_add_groups(struct rtc_device *rtc, const struct
    attribute_group **grps)}.
  \item Used between \code{devm_rtc_allocate_device} and
    \code{rtc_register_device}, they ensure all the RTC attributes are
    available at the same time.
  \end{itemize}
\end{frame}

\begin{frame}{Recent changes: RTC range}
  \begin{itemize}
  \item New members of \code{struct rtc_device}: \code{range_min} and
    \code{range_max}.
  \item They allow the driver to declare the date and time range
    supported by the RTC hardware.
  \item No discontinuity is allowed in this range.
  \item The core will check \code{struct rtc_tm} is in the range
    before passing it to the driver.
  \end{itemize}
\end{frame}

\begin{frame}{Recent changes: RTC offset}
  \begin{itemize}
  \item To solve RTC end of time issues, the core is now able to
    offset and window the hardware range.
  \item The offset is added or subtracted before calling the driver
    callbacks.
  \item Note that because the RTC time is always positive, there are
    almost no RTCs failing  in 2038 (currently only 3). The most
    common failing date is 2100, followed by 2106.
  \item This is still useful to handle dates before 2000 as it seem to
    be mandatory for Android.
  \item New device tree property: \code{start-year}
  \end{itemize}
\end{frame}

\begingroup
\catcode`\%=13
\gdef\myformat{\path{%ptR}}
\endgroup

\begin{frame}{Recent changes: other improvements}
  \begin{itemize}
  \item New {\usebeamercolor[fg]{code} \myformat} printk format
  \item Tracepoints
  \item \code{time_t} overflow is now properly handled in \code{rtc_hctosys}
    to avoid breaking 32bit platforms.
  \item \code{rtc-range} tool to test the continuous range of an rtc
    \url{git://git.kernel.org/pub/scm/linux/kernel/git/abelloni/rtc-tools.git}
  \item Timestamping
  \item Trickle charging
  \end{itemize}
\end{frame}

\begin{frame}{Recent changes: cleanups}
  \begin{itemize}
  \item Unnecessary \code{rtc_valid_tm} calls have been removed.
  \item \code{open}, \code{release}, \code{set_mmss} and
    \code{set_mmss64}, \code{read_callback} members of \code{struct
    rtc_class_ops} have been removed.
  \item \code{struct rtc_class_ops} are now \code{const} were
    possible.
  \item \code{rtc_control} API has been removed. It has been replaced
    by hrtimer.
  \item \code{rtc_irq_register} has been removed.
  \item Core files have been renamed for consistency.
  \end{itemize}
\end{frame}

\begin{frame}{Future changes: voltage detection}
  \begin{itemize}
  \item Current voltage drop detection ABI is not working well for
    more advanced RTCs.
  \item \code{RTC_VL_READ} returns a currently undocumented value.
  \item There are different types of monitored voltages: main supply,
    battery or auxiliary supply.
  \item They can have multiple states: OK, low, low and
    functionalities are disabled, data lost.
  \item A new ioctl has to be developed.
  \end{itemize}
\end{frame}

\begin{frame}{Future changes: timestamping}
  \begin{itemize}
  \item Some RTCs can store one or multiple timestamp when an event
    happens.
  \item Currently exposed through
    \code{/sys/class/rtc/rtcX/timestampX}
  \item This is open coded in each driver. There is room for
    factorization in the core.
  \item Timestamp events will probably need to be configured in the
    future (timestamp first event, last event, input pin).
  \item Not sure whether this will be using a new ioctl or more sysfs
    files.
  \end{itemize}
\end{frame}

\begin{frame}{Future changes: backup switch mode}
  \begin{itemize}
  \item It is possible to select the backup switch policy for some
    RTCs.
  \item The common policies are:
    \begin{itemize}
    \item disabled
    \item direct (VDD < VAUX)
    \item standby 
    \item level (VDD < threshold)
    \end{itemize}
  \item This will be implemented through a device tree property
    because it directly depends on the type of auxiliary voltage
    provided by the board.
  \item It is necessary to avoid hardcoding in the driver because it
    may have been set properly by the bootloader.
  \end{itemize}
\end{frame}

\begin{frame}{Future changes: alarm handling}
  \begin{itemize}
  \item Alarm support detection. Many drivers modify the \code{struct
    rtc_ops} when alarms are not present. This prevents
    constification.
  \item Alarms with minute granularity. Support is currently open
    coded in each driver.
  \item Wakeup support: many parameters have to be taken into account
    to know whether the RTC can wakeup the system, there is potential
    for factorization in the core.
  \item Alarm routing:
    \begin{itemize}
    \item Some RTCs have multiple interrupt pins and can configure
      which interrupt goes to which pin.
    \item This is useful to route alarms to a CPU (wakeup) or a PMIC
      (powerup)
    \item Device tree properties will be used.
    \end{itemize}
  \end{itemize}
\end{frame}

\begin{frame}{Future changes: BCD RTCs}
  \begin{itemize}
  \item Many RTCs store the date and time in BCD in a somewhat common
    format.
  \item There is potential for code factorization, especially when
    using regmap to access the RTC registers.
  \end{itemize}
\end{frame}

\begin{frame}{Future changes: timers}
  \begin{itemize}
  \item Some timers are now able to wakeup the platform.
  \item They don't actually handle the system time, either because the
    counter is too small or it is counting downward or it is
    impossible to read it.
  \item The drivers currently open code the dummy \code{read_time} and
    \code{set_time} to keep the RTC core happy.
  \end{itemize}
\end{frame}

\begin{frame}{Future changes: other topics}
  \begin{itemize}
  \item Revisit the \code{RTC_EPOCH_READ} and \code{RTC_EPOCH_SET} to
    change the RTC offset. This will allow changing the RTC offset at
    runtime.
  \item Write documentation on how to write an RTC driver and avoid
    the common pitfalls.
  \item Split up the ds1307 frankendriver.
  \end{itemize}
\end{frame}

\questionslide

\end{document}
