開發平台 芯靈思Sinlinx A64
記憶體: 1GB 存儲: 4GB
詳細參數
https://m.tb.cn/h.3wMaSKm開發闆交流群 641395230
全志A64裝置樹結構體
#include <linux/of.h>
//裝置樹裡的每個裝置及每個裝置子節點都用此結構體描述
struct device_node
{
const char *name;
const char *type;
phandle phandle;
const char *full_name;
struct property *properties; //屬性
struct property *deadprops; /* removed properties */
struct device_node *parent; //在裝置子節點對象,指向屬于的裝置對象
struct device_node *child; //在裝置對象,指向子節點
struct device_node *sibling; //指向同級的下一個對象.
struct device_node *next; /* next device of same type */ //應是指向device_type是同樣的對象
struct device_node *allnext; /* next in list of all nodes */ ...
};
//下面函數用于擷取裝置樹裡的裝置節點及裝置子節點
extern struct device_node *of_find_node_by_name(struct device_node *from, const char *name);
//通過名字查找相應的裝置節點
static inline int of_get_child_count(const struct device_node *np);
//擷取指定裝置的子節點個數
extern struct device_node *of_find_node_by_path(const char *path);
//通過路徑來擷取裝置節點,可用于擷取裝置子節點
extern struct device_node *of_find_node_by_type(struct device_node *from, const char *type);
//通過指定的device_type來擷取裝置節點
//下面函數用于擷取裝置節點或裝置子節點的屬性
static inline int of_property_read_u32(const struct device_node *np, const char *propname, u32 *out_value)
extern int of_property_read_u32_index(const struct device_node *np, const char *propname, u32 index, u32 *out_value);
extern int of_property_read_u8_array(const struct device_node *np, const char *propname, u8 *out_values, size_t sz);
extern int of_property_read_u16_array(const struct device_node *np, const char *propname, u16 *out_values, size_t sz);
extern int of_property_read_u32_array(const struct device_node *np, const char *propname, u32 *out_values, size_t sz);
extern int of_property_read_u64(const struct device_node *np, const char *propname, u64 *out_value);
extern int of_property_read_string(struct device_node *np, const char *propname, const char **out_string)
首先增加節點,修改dtsi檔案。
vim /lichee/linux-3.10/arch/arm64/boot/dts/sun50iw1p1-pinctrl.dtsi
驅動代碼:
#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <linux/slab.h>
#include <linux/cdev.h>
#include <asm/uaccess.h>
#include <linux/io.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/gpio.h>
#include <linux/sys_config.h>
#define MY_DEVICE_NAME "my_led_device"
// 擷取到裝置樹中到節點
static int gpio = -1;
int get_irqno_from_node(void)
{
struct gpio_config config;
struct device_node *np = of_find_node_by_path("/leds");
if(np){
printk("find node ok\n");
}
else{
printk("find node failed\n");
}
gpio = of_get_named_gpio_flags(nd, "gpios", i, (enum of_gpio_flags *)&config);// 從裝置樹中讀取gpios的GPIO配置編号和标志
if(!gpio_is_valid(gpio)){
//判斷該 GPIO 編号是否有效,有效gpio_request 則申請占用該 GPIO。如果初始化過程出錯,需要調用 gpio_free 來釋放之前申請過且成功的 GPIO
printk("gpio isn't valid\n");
return -1;
}
if(gpio_request(gpio, "leds") < 0)
printk("gpio request failed %d\n", gpio);
gpio_direction_output(gpio, 1); //關燈
return 0;
}
static int my_open (struct inode *node, struct file *filp)
{
if(gpio)
{
printk("open ok\n");
}
else
{
return -EINVAL;
}
return 0;
}
static ssize_t my_write (struct file *filp, const char __user *buf, size_t size, loff_t *off)
{
unsigned char val;
copy_from_user(&val, buf, 1);
printk(" gpl_dat address 0x%x\n",gpl_dat);
if (val)
{
gpio_direction_output(gpio, 0); //關燈
printk("led on\n");
}
else
{
gpio_direction_output(gpio, 1); //關燈
printk("led off\n");
}
return 1;
}
static const struct file_operations my_led_fops = {
//step 1 :定義file_operations結構體
.open = my_open,
.write = my_write,
};
//step 1 :
static struct class *led_class;
static struct cdev *pcdev; //定義一個cdev指針
static dev_t n_dev; //第一個裝置号(包含了主和次)
static int __init led_device_init(void)
{//step 2 :注冊
int ret = -1;
pcdev = cdev_alloc();//配置設定cdev結構空間
if(pcdev == NULL) {
printk(KERN_EMERG" cdev_alloc error\n");
ret = -ENOMEM; /* 配置設定失敗 */
return ret;
}
//2. 動态申請裝置号
ret = alloc_chrdev_region(&n_dev, 0 , 2, MY_DEVICE_NAME);
if(ret < 0 ) {
//釋放前面成功的資源
kfree(pcdev); /*釋放cdev結構空間 */
printk(KERN_EMERG"alloc_chrdev_region error\n");
return ret;
}
cdev_init(pcdev, &my_led_fops); //初始化cdev結構 /* 建立cdev和file_operations之間的連接配接 */
/*
或這樣初始化cdev結構
pcdev->owner = THIS_MODULE;
pcdev->ops = &my_led_fops;
*/
ret = cdev_add(pcdev, n_dev, 2) ;// 向核心裡面添加一個驅動,注冊驅動
if(ret < 0 ) {
//釋放前面成功的資源
unregister_chrdev_region(n_dev, 2); /* 釋放前面申請的調和号*/
kfree(pcdev); /* 釋放cdev結構空間 */
printk(KERN_EMERG"alloc_chrdev_region error\n");
return ret;
}
/*自動建立裝置節點/dev/SinlinxA64_LED*/
led_class = class_create(THIS_MODULE, "myled");
device_create(led_class, NULL, n_dev, NULL, "SinlinxA64_LED");
get_irqno_from_node();
printk(KERN_EMERG"cdev ok\n");
return 0;
}
static void __exit led_device_exit(void)
{ //step 2 :登出
//登出cdev結構
cdev_del(pcdev);
//釋放裝置号
unregister_chrdev_region(n_dev, 2); /*起始裝置号(主、次) 連續的次裝置号數量*/
//釋放cdev結構空間
kfree(pcdev);
device_destroy(led_class, n_dev);
class_destroy(led_class);
gpio_free(gpio);
printk(KERN_EMERG"cdev_del ok\n");
}
module_init(led_device_init);
module_exit(led_device_exit);
MODULE_LICENSE("GPL");
參考文章:
https://blog.csdn.net/jklinux/article/details/82382066