0
votes

I am beginner to embedded device driver writing. I am trying to write a custom device driver for my embedded device. as I am familiar with writing Char drivers, UIO, MISC frameworks. let's say I want to write my own kernel device driver for RTC (real-time clock)module.

Declare custom device struct

struct rtc685_device{
struct device dev;
struct module *owner;
int id 
const struct rtc685_class_ops *ops
.....
}

Declare RTC custom struct operations

struct rtc685_class_ops{
int (*open) (struct device*)
void(*release)(struct device*)
int(*get_time)(struct device *, struct rtc685_data*)
....
}

Register device with kernel

 struct rtc685_device *rtc685_device_register(const char *name,
struct device *dev, const struct rtc685_class_ops *ops,
struct module *owner)

my question is how to write source code for this function ? i can see some random code for this register function . but i can't understand . please help me to understand the steps to create full custom device driver registration function. lastly, I am a hardware guy and don't understand much about kernel things. Thanks

1

1 Answers

3
votes

Please, do not roll such a custom driver. For RTCS, there is an RTC subsystem that will handle all the kernel and userspace communication. It is located in drivers/rtc. For ADCs, there is also a subsystem, IIO, located in drivers/iio/adc.

They both provide driver specific structures that you need to fill out. For RTCs, this is struct rtc_device and struct rtc_class_ops. rtc_class_ops contains all the callbacks that you may (or may not) implement to get your RTC working, such as read_time, set_time, read_alarm and set_alarm. These callbacks will be called when necessary (e.g. when a userspace application reads the time) by the subsystem.

Once you've filled out both structures, you have to register your driver using the subsystem registration function, rtc_register_device.

You can have a look at the rtc-pl030 driver which is very simple to follow.