i.MX6ULL驱动开发 | 26 - Linux内核的RTC驱动

Posted Mculover666

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了i.MX6ULL驱动开发 | 26 - Linux内核的RTC驱动相关的知识,希望对你有一定的参考价值。

一、RTC时间查看与设置

1. 内核启动日志

查看Linux内核启动时与RTC相关的日志:

dmesg | grep rtc

2. 查看与设置当前系统时间

(1)查看当前时间:

date


(2)设置当前时间

date -s "2022-07-02 13:47:00"

3. 设置当前时间到RTC外设

hwclock -w

使用此命令设置时间到RTC外设,有纽扣电池的情况下,时间掉电不丢失。

二、Linux内核提供的RTC驱动

1. rtc_device结构体

RTC设备驱动是一个标准的字符驱动设备,应用程序通过open、release、read、write和ioctl接口完成对RTC设备的操作。

Linux内核将RTC设备抽象为rtc_device结构体,因此RTC设备驱动就是申请并初始化rtc_device,最后将rtc_device注册到Linux内核里面,这样内核中就有了一个rtc设备节点。

rtc_device结构体定义在文件include/linux/rtc.h中,如下:

struct rtc_device

	struct device dev;
	struct module *owner;

	int id;
	char name[RTC_DEVICE_NAME_SIZE];

	const struct rtc_class_ops *ops;
	struct mutex ops_lock;

	struct cdev char_dev;
	unsigned long flags;

	unsigned long irq_data;
	spinlock_t irq_lock;
	wait_queue_head_t irq_queue;
	struct fasync_struct *async_queue;

	struct rtc_task *irq_task;
	spinlock_t irq_task_lock;
	int irq_freq;
	int max_user_freq;

	struct timerqueue_head timerqueue;
	struct rtc_timer aie_timer;
	struct rtc_timer uie_rtctimer;
	struct hrtimer pie_timer; /* sub second exp, so needs hrtimer */
	int pie_enabled;
	struct work_struct irqwork;
	/* Some hardware can't support UIE mode */
	int uie_unsupported;

#ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
	struct work_struct uie_task;
	struct timer_list uie_timer;
	/* Those fields are protected by rtc->irq_lock */
	unsigned int oldsecs;
	unsigned int uie_irq_active:1;
	unsigned int stop_uie_polling:1;
	unsigned int uie_task_active:1;
	unsigned int uie_timer_active:1;
#endif
;

2. 对下向驱动适配——rtc_class_ops

rtc_device结构体中ops成员变量是rtc_classs_ops类型,定义在``文件中,如下:

/*
 * For these RTC methods the device parameter is the physical device
 * on whatever bus holds the hardware (I2C, Platform, SPI, etc), which
 * was passed to rtc_device_register().  Its driver_data normally holds
 * device state, including the rtc_device pointer for the RTC.
 *
 * Most of these methods are called with rtc_device.ops_lock held,
 * through the rtc_*(struct rtc_device *, ...) calls.
 *
 * The (current) exceptions are mostly filesystem hooks:
 *   - the proc() hook for procfs
 *   - non-ioctl() chardev hooks:  open(), release(), read_callback()
 *
 * REVISIT those periodic irq calls *do* have ops_lock when they're
 * issued through ioctl() ...
 */
struct rtc_class_ops 
	int (*open)(struct device *);
	void (*release)(struct device *);
	int (*ioctl)(struct device *, unsigned int, unsigned long);
	int (*read_time)(struct device *, struct rtc_time *);
	int (*set_time)(struct device *, struct rtc_time *);
	int (*read_alarm)(struct device *, struct rtc_wkalrm *);
	int (*set_alarm)(struct device *, struct rtc_wkalrm *);
	int (*proc)(struct device *, struct seq_file *);
	int (*set_mmss64)(struct device *, time64_t secs);
	int (*set_mmss)(struct device *, unsigned long secs);
	int (*read_callback)(struct device *, int data);
	int (*alarm_irq_enable)(struct device *, unsigned int enabled);
;

需要注意,这些函数是提供给底层驱动适配的RTC设备操作函数,并不是提供给应用层的操作函数。

3. 对上向应用提供操作接口

Linux内核提供了一个RTC通用字符设备驱动文件:drivers/rtc/rtc-dev.c

该文件中提供了 file_operations 函数操作集:

其中最重要的为 rtc_dev_ioctl API,该函数中实现了很多对应命令的操作,实现如下:

通过该API可以完成对硬件RTC的操作,命令定义在rtc.h中,如下:

/*
 * ioctl calls that are permitted to the /dev/rtc interface, if
 * any of the RTC drivers are enabled.
 */

#define RTC_AIE_ON	_IO('p', 0x01)	/* Alarm int. enable on		*/
#define RTC_AIE_OFF	_IO('p', 0x02)	/* ... off			*/
#define RTC_UIE_ON	_IO('p', 0x03)	/* Update int. enable on	*/
#define RTC_UIE_OFF	_IO('p', 0x04)	/* ... off			*/
#define RTC_PIE_ON	_IO('p', 0x05)	/* Periodic int. enable on	*/
#define RTC_PIE_OFF	_IO('p', 0x06)	/* ... off			*/
#define RTC_WIE_ON	_IO('p', 0x0f)  /* Watchdog int. enable on	*/
#define RTC_WIE_OFF	_IO('p', 0x10)  /* ... off			*/

#define RTC_ALM_SET	_IOW('p', 0x07, struct rtc_time) /* Set alarm time  */
#define RTC_ALM_READ	_IOR('p', 0x08, struct rtc_time) /* Read alarm time */
#define RTC_RD_TIME	_IOR('p', 0x09, struct rtc_time) /* Read RTC time   */
#define RTC_SET_TIME	_IOW('p', 0x0a, struct rtc_time) /* Set RTC time    */
#define RTC_IRQP_READ	_IOR('p', 0x0b, unsigned long)	 /* Read IRQ rate   */
#define RTC_IRQP_SET	_IOW('p', 0x0c, unsigned long)	 /* Set IRQ rate    */
#define RTC_EPOCH_READ	_IOR('p', 0x0d, unsigned long)	 /* Read epoch      */
#define RTC_EPOCH_SET	_IOW('p', 0x0e, unsigned long)	 /* Set epoch       */

#define RTC_WKALM_SET	_IOW('p', 0x0f, struct rtc_wkalrm)/* Set wakeup alarm*/
#define RTC_WKALM_RD	_IOR('p', 0x10, struct rtc_wkalrm)/* Get wakeup alarm*/

#define RTC_PLL_GET	_IOR('p', 0x11, struct rtc_pll_info)  /* Get PLL correction */
#define RTC_PLL_SET	_IOW('p', 0x12, struct rtc_pll_info)  /* Set PLL correction */

#define RTC_VL_READ	_IOR('p', 0x13, int)	/* Voltage low detector */
#define RTC_VL_CLR	_IO('p', 0x14)		/* Clear voltage low information */

三、NXP原厂提供的RTC外设驱动

1. 设备树描述

RTC外设属于SOC芯片级外设,所以在imx6ull通用描述文件arch/arm/boot/dts/imx6ull.dtsi中寻找:

2. 驱动源码

根据兼容性"fsl,sec-v4.0-mon-rtc-lp"寻找对应的驱动源码:

grep -nR "fsl,sec-v4.0-mon-rtc-lp" *


找到驱动文件为:drivers/rtc/rtc-snvs.c

3. 驱动文件浅析

可以看到RTC外设驱动也是按照platform平台驱动框架所写:

(1)在probe函数中,驱动通过使用regmap机制完成对硬件寄存器的操作。

(2)之后向内核申请了一个中断作为rtc闹钟中断:

中断处理函数如下:

(3)向内核注册一个rtc设备:

注册的操作函数指针如下:

这些实际操作的函数指针中,完成对RTC外设的操作,以snvs_rtc_set_time为例:

四、RTC驱动整体调用流程

以上是关于i.MX6ULL驱动开发 | 26 - Linux内核的RTC驱动的主要内容,如果未能解决你的问题,请参考以下文章

i.MX6ULL驱动开发1——字符设备开发模板

i.MX6ULL驱动开发 | 15 - Linux UART 驱动框架

i.MX6ULL驱动开发 | 09 -基于Linux自带的LED驱动点亮LED

i.MX6ULL驱动开发 | 08 -基于pinctrl子系统和gpio子系统点亮LED

i.MX6ULL驱动开发 | 08 -基于pinctrl子系统和gpio子系统点亮LED

i.MX6ULL驱动开发 | 13 - Linux SPI 驱动框架