[Beijing Xunwei] Platform driver experiment program under i.MX6ULL Terminator device tree

1 Modify the device tree file

The device tree file can directly use the gpioled sub-nodes added in Chapter 35, without repeating additions.

2 platform driver

The path of the experiment routine: i.MX6UL Terminator CD-ROM data/06_Linux driver routine/15_gpioled_dts to
create the led_driver.c file, the specific content is as follows:

 1 #include <linux/types.h>
  2 #include <linux/kernel.h>
  3 #include <linux/delay.h>
  4 #include <linux/ide.h>
  5 #include <linux/init.h>
  6 #include <linux/module.h>
  7 #include <linux/errno.h>
  8 #include <linux/gpio.h>
  9 #include <linux/cdev.h>
 10 #include <linux/device.h>
 11 #include <linux/of_gpio.h>
 12 #include <linux/semaphore.h>
 13 #include <linux/timer.h>
 14 #include <linux/irq.h>
 15 #include <linux/wait.h>
 16 #include <linux/poll.h>
 17 #include <linux/fs.h>
 18 #include <linux/fcntl.h>
 19 #include <linux/platform_device.h>
 20 #include <asm/mach/map.h>
 21 #include <asm/uaccess.h>
 22 #include <asm/io.h>
 23 
 24 #define LEDDEV_CNT              1                    /* 设备号长度   */
 25 #define LEDDEV_NAME             "dtsplatled"    /* 设备名字     */
 26 #define LEDOFF                  0
 27 #define LEDON                   1
 28 
 29 /* leddev设备结构体 */
 30 struct leddev_dev{
    
    
 31         dev_t devid;                            /* 设备号       */
 32         struct cdev cdev;                       /* cdev         */
 33         struct class *class;            /* 类           */
 34         struct device *device;          /* 设备 */
 35         int major;                     /* 主设备号     */
 36         struct device_node *node;       /* LED设备节点 */
 37         int led0;                      /* LED灯GPIO标号 */
 38 };
 39 
 40 struct leddev_dev leddev;               /* led设备 */
 41 
 42 /*
 43  * @description         : LED打开/关闭
 44  * @param - sta         : LEDON(0) 打开LED,LEDOFF(1) 关闭LED
 45  * @return                      : 无
 46  */
 47 void led0_switch(u8 sta)
 48 {
    
    
 49         if (sta == LEDON )
 50                 gpio_set_value(leddev.led0, 0);
 51         else if (sta == LEDOFF)
 52                 gpio_set_value(leddev.led0, 1);
 53 }
 54 
 55 /*
 56  * @description         : 打开设备
 57  * @param - inode       : 传递给驱动的inode
 58  * @param - filp        : 设备文件,file结构体有个叫做private_data的成员变量
 59  * 一般在open的时候将private_data指向设备结构体。
 60  * @return                      : 0 成功;其他 失败
 61  */
 62 static int led_open(struct inode *inode, struct file *filp)
 63 {
    
    
 64         filp->private_data = &leddev; /* 设置私有数据  */
 65         return 0;
 66 }
 67 
 68 /*
 69  * @description         : 向设备写数据 
 70  * @param - filp        : 设备文件,表示打开的文件描述符
 71  * @param - buf         : 要写给设备写入的数据
 72  * @param - cnt         : 要写入的数据长度
 73  * @param - offt        : 相对于文件首地址的偏移
 74  * @return                      : 写入的字节数,如果为负值,表示写入失败
 75  */
 76 static ssize_t led_write(struct file *filp, const char __user *buf, size_t cnt, loff_t *offt)
 77 {
    
    
 78         int retvalue;
 79         unsigned char databuf[2];
 80         unsigned char ledstat;
 81 
 82         retvalue = copy_from_user(databuf, buf, cnt);
 83         if(retvalue < 0) {
    
    
 84 
 85                 printk("kernel write failed!\r\n");
 86                 return -EFAULT;
 87         }
 88 
 89         ledstat = databuf[0];
 90         if (ledstat == LEDON) {
    
    
 91                 led0_switch(LEDON);
 92         } else if (ledstat == LEDOFF) {
    
    
 93                 led0_switch(LEDOFF);
 94         }
 95         return 0;
 96 }
 97 
 98 /* 设备操作函数 */
 99 static struct file_operations led_fops = {
    
    
100         .owner = THIS_MODULE,
101         .open = led_open,
102         .write = led_write,
103 };
104 
105 /*
106  * @description         : flatform驱动的probe函数,当驱动与
107  *                                        设备匹配以后此函数就会执行
108  * @param - dev         : platform设备
109  * @return                      : 0,成功;其他负值,失败
110  */
111 static int led_probe(struct platform_device *dev)
112 {
    
    
113         printk("led driver and device was matched!\r\n");
114         /* 1、设置设备号 */
115         if (leddev.major) {
    
    
116                 leddev.devid = MKDEV(leddev.major, 0);
117               register_chrdev_region(leddev.devid, LEDDEV_CNT, LEDDEV_NAME);
118         } else {
    
    
119              alloc_chrdev_region(&leddev.devid, 0, LEDDEV_CNT, LEDDEV_NAME);
120                 leddev.major = MAJOR(leddev.devid);
121         }
122 
123         /* 2、注册设备      */
124         cdev_init(&leddev.cdev, &led_fops);
125         cdev_add(&leddev.cdev, leddev.devid, LEDDEV_CNT);
126 
127         /* 3、创建类      */
128         leddev.class = class_create(THIS_MODULE, LEDDEV_NAME);
129         if (IS_ERR(leddev.class)) {
    
    
130                 return PTR_ERR(leddev.class);
131         }
132 
133         /* 4、创建设备 */
134         leddev.device = device_create(leddev.class, NULL, leddev.devid, 
NULL, LEDDEV_NAME);
135         if (IS_ERR(leddev.device)) {
    
    
136                 return PTR_ERR(leddev.device);
137         }
138 
139         /* 5、初始化IO */
140         leddev.node = of_find_node_by_path("/gpioled");
141         if (leddev.node == NULL){
    
    
142                 printk("gpioled node nost find!\r\n");
143                 return -EINVAL;
144         }
145 
146         leddev.led0 = of_get_named_gpio(leddev.node, "led-gpio", 0);
147         if (leddev.led0 < 0) {
    
    
148                 printk("can't get led-gpio\r\n");
149                 return -EINVAL;
150         }
151 
152         gpio_request(leddev.led0, "led0");
153     gpio_direction_output(leddev.led0, 1); /* led0 IO设置为输出,默认高电平 */
154         return 0;
155 }
156 
157 /*
158  * @description : platform驱动的remove函数,移除platform
驱动的时候此函数会执行
159  * @param - dev         : platform设备
160  * @return         : 0,成功;其他负值,失败
161  */
162 static int led_remove(struct platform_device *dev)
163 {
    
    
164         gpio_set_value(leddev.led0, 1);         /* 卸载驱动的时候关闭LED */
165 
166         cdev_del(&leddev.cdev);                         /*  删除cdev */
167         unregister_chrdev_region(leddev.devid, LEDDEV_CNT); /* 注销设备号 */
168         device_destroy(leddev.class, leddev.devid);
169         class_destroy(leddev.class);
170         return 0;
171 }
172 
173 /* 匹配列表 */
174 static const struct of_device_id led_of_match[] = {
    
    
175         {
    
     .compatible = "gpioled" },
176         {
    
     /* Sentinel */ }
177 };
178 
179 /* platform驱动结构体 */
180 static struct platform_driver led_driver = {
    
    
181         .driver         = {
    
    
182                 .name   = "imx6ul-led",     /* 驱动名字,用于和设备匹配 */
183                 .of_match_table = led_of_match, /* 设备树匹配表 */
184         },
185         .probe          = led_probe,
186         .remove         = led_remove,
187 };
188 
189 /*
190  * @description : 驱动模块加载函数
191  * @param               : 无
192  * @return              : 无
193  */
194 static int __init leddriver_init(void)
195 {
    
    
196         return platform_driver_register(&led_driver);
197 }
198 
199 /*
200  * @description : 驱动模块卸载函数
201  * @param               : 无
202  * @return              : 无
203  */
204 static void __exit leddriver_exit(void)
205 {
    
    
206         platform_driver_unregister(&led_driver);
207 }
208 
209 module_init(leddriver_init);
210 module_exit(leddriver_exit);
211 MODULE_LICENSE("GPL");
212 MODULE_AUTHOR("topeet");

Lines 174~177, the matching table, describe what kind of device the driver matches. Line 175 adds a compatible attribute value of "gpioled". When the compatible attribute value of a device node in the device tree is also When it is "gpioled", it will match this driver.
Lines 180~187, platform_driver driver structure, line 182 sets the name of this platform driver to "imx6ul-led", so when the driver is loaded successfully, there will be a name in the /sys/bus/platform/drivers/ directory The file is "imx6u-led". Line 183 sets of_match_table to led_of_match above.

3 Application test program

The application test program can directly use led_test.c in 44.3.2 of the previous chapter.

Insert picture description here

Guess you like

Origin blog.csdn.net/BeiJingXunWei/article/details/111884624