Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Linux Device Drivers Development

You're reading from  Linux Device Drivers Development

Product type Book
Published in Oct 2017
Publisher Packt
ISBN-13 9781785280009
Pages 586 pages
Edition 1st Edition
Languages
Author (1):
John Madieu John Madieu
Profile icon John Madieu

Table of Contents (23) Chapters

Preface 1. Introduction to Kernel Development 2. Device Driver Basis 3. Kernel Facilities and Helper Functions 4. Character Device Drivers 5. Platform Device Drivers 6. The Concept of Device Tree 7. I2C Client Drivers 8. SPI Device Drivers 9. Regmap API – A Register Map Abstraction 10. IIO Framework 11. Kernel Memory Management 12. DMA – Direct Memory Access 13. The Linux Device Model 14. Pin Control and GPIO Subsystem 15. GPIO Controller Drivers – gpio_chip 16. Advanced IRQ Management 17. Input Devices Drivers 18. RTC Drivers 19. PWM Drivers 20. Regulator Framework 21. Framebuffer Drivers 22. Network Interface Card Drivers

RTC Drivers

Real Time Clocks (RTCs) are devices used to track absolute time in nonvolatile memory, which may be internal to the processor or externally connected through the I2C or SPI bus.

You may use an RTC to do the following:

  • Read and set the absolute clock, and generate interrupts during clock updates
  • Generate periodic interrupts
  • Set alarms

RTCs and the system clock have different purposes. The former is a hardware clock that maintains absolute time and date in a nonvolatile manner, whereas the latter is a software clock maintained by the kernel and used to implement the gettimeofday(2) and time(2) system calls, as well as setting timestamps on files and so on. The system clock reports seconds and microseconds from a start point, defined to be the POSIX epoch: 1970-01-01 00:00:00 +0000 (UTC).

In this chapter, we will cover the following topics:

  • Introducing...

RTC framework data structures

There are three main data structures used by the RTC framework on Linux systems. They are the strcut rtc_time, struct rtc_device, and struct rtc_class_ops structures. The former is an opaque structure that represents a given date and time; the second structure represents the physical RTC device; and the last one represents a set of operations exposed by the driver and used by the RTC core to read/update a device's date/time/alarm.

The only header needed to pull RTC functions from within your driver is:

#include <linux/rtc.h> 

The same file contains all of the three structures enumerated in the preceding section:

struct rtc_time { 
   int tm_sec;  /* seconds after the minute */ 
   int tm_min;  /* minutes after the hour - [0, 59] */ 
   int tm_hour; /* hours since midnight - [0, 23] */ 
   int tm_mday; /* day of the month - [1, 31]...

RTCs and user space

On Linux systems, there are two kernel options you needs to care about in order to properly manage RTCs from the user space. These are CONFIG_RTC_HCTOSYS and CONFIG_RTC_HCTOSYS_DEVICE.

CONFIG_RTC_HCTOSYS includes the drivers/rtc/hctosys.c code file in the kernel build process, which sets the system time from the RTC on startup and resume. Once this option is enabled, the system time will be set using the value read from the specified RTC device. RTC devices should be specified in CONFIG_RTC_HCTOSYS_DEVICE:

CONFIG_RTC_HCTOSYS=y 
CONFIG_RTC_HCTOSYS_DEVICE="rtc0" 

In the preceding example, we tell the kernel to set the system time from the RTC, and we specify that the RTC to use is rtc0.

The sysfs interface

...

Summary

This chapter introduced you to the RTC framework and its API. Its reduced set of functions and data structures make it the most lightweight framework and easy to master. Using the skills described in this chapter, you will be able to develop a driver for most existing RTC chips, and even go further and handle such devices from the user space, easily setting up date and time, as well as alarms. The next chapter, PWM Drivers, has nothing in common with this one, but is a must-know for embedded engineers.

lock icon The rest of the chapter is locked
You have been reading a chapter from
Linux Device Drivers Development
Published in: Oct 2017 Publisher: Packt ISBN-13: 9781785280009
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}