rk3288 设备树led驱动程序

写设备树需要注意的点

① 设备树要有 compatible 属性,它的值是一个字符串
② platform_driver 中要有 of_match_table,其中一项的.compatible 成员设置为一个字符串
③ 上述 2 个字符串要一致。
如:
示例

修改思路

board_A_led.c中定义的资源需要放到设备树中去定义。
chip_demo_gpio.c中的probe和remove中通过of_property_read_u32读取设备树中定义的引脚值。
通过引脚值去创建字符设备。

源码

  1. Makefile
KERN_DIR = /home/book/Documents/100ask_firefly-rk3288/linux-4.4

all:
	make -C $(KERN_DIR) M=`pwd` modules
	$(CROSS_COMPILE)gcc -o led_app led_app.c  -Wno-error=unused-but-set-variable

clean:
	make -C $(KERN_DIR) M=`pwd` modules clean
	rm -rf modules.order
	rm -f hello_drv_test


obj-m += chip_demoo_gpio.o led_drv.o
  1. myled.dts
#define GROUP_PIN(g, p) ((g<<16| (p)))
/{
	myled@0{
		compatible = "myled,led_drv";
		pin = <GROUP_PIN(8, 1)>;
	};
	myled@1{
		compatible = "myled,led_drv";
		pin = <GROUP_PIN(8, 2)>;
	};
}
  1. led_drv.h
#ifndef __LED_DRV_H_          
#define __LED_DRV_H_ 

void led_class_create_device(int index);
void led_class_destroy_device(int index);
void register_led_operations(struct led_operations *led_opr);

#endif
  1. led_drv.c
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/major.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/miscdevice.h>
#include <linux/uaccess.h>

#include "led_ops.h"
#include "led_drv.h"

static int major = 0;
static struct class *led_class;
struct led_operations *p_led_ops;

void led_class_create_device(int index)
{
	device_create(led_class, NULL, MKDEV(major, index), NULL, "myled%d", index);
}

void led_class_destroy_device(int index)
{
	device_destroy(led_class, MKDEV(major, index));
}

void register_led_operations(struct led_operations *led_opr)
{
	p_led_ops = led_opr;
}

EXPORT_SYMBOL(led_class_create_device);
EXPORT_SYMBOL(led_class_destroy_device);
EXPORT_SYMBOL(register_led_operations);

static int led_drv_open(struct inode *node, struct file *file)
{
	int minor = iminor(node);
	printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);

	p_led_ops->init(minor);	
	return 0;
}

static ssize_t led_drv_read(struct file *file, char __user *buf, size_t size, loff_t *offset)
{
	printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
	return 0;
}

static ssize_t led_drv_write(struct file *file, const char __user *buf, size_t size, loff_t *offset)
{
	int err;
	char status;
	struct inode *inode = file_inode(file);
	int minor = iminor(inode);

	printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
	err = copy_from_user(&status, buf, 1);
	printk("write status:%x \n", status);

	p_led_ops->ctl(minor, status);
	
	return 1;
}

static int led_drv_close(struct inode *node, struct file *file)
{
	printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
	return 0;
}


static struct file_operations led_drv = {
	.owner = THIS_MODULE,
	.open = led_drv_open,
	.read = led_drv_read,
	.write = led_drv_write,
	.release = led_drv_close,
};

static int __init led_drv_init(void)
{
	int err;
	major = register_chrdev(0, "myled", &led_drv);

	led_class = class_create(THIS_MODULE, "led_class");
	err = PTR_ERR(led_class);
	if(IS_ERR(led_class)) {
		unregister_chrdev(major, "myled");	
		printk(KERN_WARNING "class creatge failed %d\n", err);
		return -1;
	}
	
	printk("%s %sled_drv_init\n", __FILE__, __FUNCTION__);
	return 0;
}

static void __exit led_drv_exit(void)
{
	printk("%s %sled_drv_exit\n", __FILE__, __FUNCTION__);

	class_destroy(led_class); 
	unregister_chrdev(major, "myled");	
}

module_init(led_drv_init);
module_exit(led_drv_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("chen");
  1. led_ops.h
#ifndef __LED_OPS_H_
#define __LED_OPS_H_

struct led_operations {
	int (*init)(int which);			/* init led, which:led num  */
	int (*ctl)(int which, char status);		/* control led, whic:led num,status:0-On 1-Off */
};

struct led_operations *get_board_led_ops(void);

#endif
  1. chip_demoo_gpio.c
#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/io.h>
#include <linux/platform_device.h>
#include <linux/of.h>
#include "led_resource.h"
#include "led_ops.h"
#include "led_drv.h"

static unsigned int led_pins[100];
static int resPinCount;

#define CRU_BASE_PHY_ADDRESS   ((unsigned long)(0xff760000))
#define GRF_BASE_PHY_ADDRESS   ((unsigned long)(0xff770000))
#define GPIO8_BASE_PHY_ADDRESS ((unsigned long)(0xff7f0000))


#define CRU_CLKGATE14_PHY_CON (0x0198)
#define GRF_GPIO8A_PHY_IOMUX  (0x0080)
#define GPIO_SWPORTA_PHY_DR   (0x0000)
#define GPIO_SWPORTA_PHY_DDR  (0x0004)

static volatile unsigned int *CRU_CLKGATE14_CON;
static volatile unsigned int *GRF_GPIO8A_IOMUX;
static volatile unsigned int *GPIO8_SWPORTA_DDR;
static volatile unsigned int *GPIO8_SWPORTA_DR;

static int board_demo_led_init(int which)
{
    
	printk("%s which %d", __FUNCTION__, which);
	if(GROUP(led_pins[which]) == 8) {
		if(!CRU_CLKGATE14_CON) {
			CRU_CLKGATE14_CON = ioremap(CRU_BASE_PHY_ADDRESS + CRU_CLKGATE14_PHY_CON, 4);
			GRF_GPIO8A_IOMUX  = ioremap(GRF_BASE_PHY_ADDRESS + GRF_GPIO8A_PHY_IOMUX, 4);
			GPIO8_SWPORTA_DDR = ioremap(GPIO8_BASE_PHY_ADDRESS + GPIO_SWPORTA_PHY_DDR, 4);
			GPIO8_SWPORTA_DR  = ioremap(GPIO8_BASE_PHY_ADDRESS + GPIO_SWPORTA_PHY_DR, 4);
		}

		if(PIN(led_pins[which]) == 1) {
			*CRU_CLKGATE14_CON  = (1<<(8+16)) | (0<<8);
			*GRF_GPIO8A_IOMUX  |= (3<<(2+16)) | (0<<2);
			*GPIO8_SWPORTA_DDR |= (1<<1);
		} else if(PIN(led_pins[which]) == 2) {
			*CRU_CLKGATE14_CON  = (1<<(8+16)) | (0<<8);
			*GRF_GPIO8A_IOMUX  |= (3<<(4+16)) | (0<<4);
			*GPIO8_SWPORTA_DDR |= (1<<2);
		}
	}

	return 0;

}

static int board_demo_led_ctrl(int which, char status)
{
	printk("%s which %d, status %c", __FUNCTION__, which, status);
	if(PIN(led_pins[which]) == 1) {
		if(status) {		/* on: output 0 */
			*GPIO8_SWPORTA_DR &= ~(1<<1);
		} else {			/* off: output 1 */
			*GPIO8_SWPORTA_DR |= (1<<1);
		}
	} else if(PIN(led_pins[which]) == 2) {
		if(status) {
			*GPIO8_SWPORTA_DR &= ~(1<<2);
		} else {
			*GPIO8_SWPORTA_DR |= (1<<2);
		}
	}
	return 0;
}

static struct led_operations led_opr = {
	.init = board_demo_led_init,
	.ctl = board_demo_led_ctrl,
};

struct led_operations *get_board_led_ops(void)
{
	return &led_opr; 
}

static int led_platform_driver_probe(struct platform_device *pdev)
{
	int err = 0;
	struct device_node *np;
	int led_pin;

	np = pdev->dev.of_node;
	if(!np) {
		printk("%s node is null\n", __FUNCTION__);
		return -1;
	}

	err = of_property_read_u32(np, "pin", &led_pin);
	if(err !=0) {
		printk("%s of_property_read_u32 return err:%d\n", __FUNCTION__, err);
		return -1;
	}

	led_pins[resPinCount] = led_pin;
	led_class_create_device(resPinCount);
	resPinCount++;

	return 0;
}

static int led_platform_driver_remove(struct platform_device *pdev)
{
	int err = 0;
	int led_pin;
	int i=0;
	int hasPinProbed = 0;
	struct device_node *np;

	np = pdev->dev.of_node;
	if(!np) {
		return -1;
	}

	err = of_property_read_u32(np, "pin", &led_pin);
	if(err != 0)
		return -1;

	for(i=0;i<resPinCount;i++) {
		if(led_pins[i] == led_pin) {
			led_class_destroy_device(i);
			led_pins[i] = -1;
			break;
		}
	}
	
	for(i=0;i<=resPinCount;i++) {
		if(led_pins[i] != -1) {
			hasPinProbed = 1;
			break;
		}
	}

	if(hasPinProbed == 0)
		resPinCount = 0;

	return 0;
}

static const struct of_device_id myleds[] = {
	{ .compatible = "myled,led_drv" },
};

static struct platform_driver led_driver = {
	.driver = {
		.name = "myled",
		.of_match_table = myleds,
	},
	.probe = led_platform_driver_probe,
	.remove = led_platform_driver_remove,
};

static int led_platform_driver_init(void)
{
	int err;
	err = platform_driver_register(&led_driver);
	register_led_operations(&led_opr); 
	return 0;
}

static void led_platform_driver_exit(void)
{
	platform_driver_unregister(&led_driver);
}

module_init(led_platform_driver_init);
module_exit(led_platform_driver_exit);
MODULE_LICENSE("GPL");
  1. led_app.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>

void showUsage(void)
{
	printf("app [dev_path] [on,off]\n");
}

int main(int argc, char *argv[])
{
	char status;
	if(argc < 3) {
		showUsage();
		return -1;
	}

	int fd = open(argv[1], O_RDWR);
	if(fd < 0) {
		printf("app open device failed path:%s", argv[1]);
		return -1;
	}

	if(0 == strcmp(argv[2], "on")) {
		status = 1;
		int ret = write(fd, &status, 1);
		if(ret <= 0) {
			printf("app write device fialed %s",argv[2]);
			return -1;
		} else {
			printf("app write device %x", status);
		}

	} else if(0 == strcmp(argv[2], "off")) {
		status = 0;
		int ret = write(fd, &status, 1);
		if(ret <= 0) {
			printf("app write device fialed %s",argv[2]);
			return -1;
		} else {
			printf("app write device %x", status);
		}
	}


	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

习惯就好zz

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值