MSM8909上pinctrl & gpio子系统 点亮LED(方法一:不用platform设备驱动)
学习目标
不用platform设备驱动,在MSM8909上用pinctrl & gpio子系统点亮LED。
主要内容
1、 修改设备树 2、 LED驱动程序 3、 测试APP1、 设备树DTS
1)设备树apq8009w-openq2500.dts中找到yellow灯设备节点: node path:/soc/leds/yellowleds {
compatible = "gpio-leds";
yellow {
gpios = <&msm_gpio 49 0>;//GPIO49
linux,default-trigger = "mmc0";
};
blue {
gpios = <&msm_gpio 83 0>;
default-state = "on";
linux,default-trigger = "boot-indication";
};
};
获取设备节点:
gpioled.nd = of_find_node_by_path("/soc/leds/yellow");
2)pinctrl节点
pin的初始化工作由pinctrl子系统完成,源码目录在drivers/pinctrl。
没leds对应的pinctrl信息。
3)检查 PIN 是否被其他外设使用
因为用了开发板的源码,很多PIN已经定义好了和被使用了。对DTS就不修改了,直接用现成的leds节点。
2、 LED驱动程序
以下驱动程序参考正点原子开发部的LED驱动程序修改。
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/ide.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/gpio.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_gpio.h>
#include <asm/mach/map.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#define GPIOLED_CNT 1
#define GPIOLED_NAME "gpioled"
#define LEDOFF 0
#define LEDON 1
/* gpioled 设备结构体 */
struct gpioled_dev{
dev_t devid; /* 设备号 */
struct cdev cdev; /* cdev */
struct class *class; /* 类 */
struct device *device; /* 设备 */
int major; /* 主设备号 */
int minor; /* 次设备号 */
struct device_node *nd; /* 设备节点 */
int led_gpio; /* led 所使用的 GPIO 编号 */
};
struct gpioled_dev gpioled; /* led 设备 */
static int led_open(struct inode *inode, struct file *filp)
{
filp->private_data = &gpioled;
return 0;
}
static ssize_t led_read(struct file *filp, char __user *buf, size_t cnt, loff_t *offt)
{
return 0;
}
static ssize_t led_write(struct file *filp, const char __user *buf, size_t cnt, loff_t *offt)
{
int retvalue;
unsigned char databuf[1];
unsigned char ledstat;
struct gpioled_dev *dev = filp->private_data;
retvalue = copy_from_user(databuf, buf, cnt);
if(retvalue < 0) {
printk("kernel write failed!\r\n");
return -EFAULT;
}
ledstat = databuf[0];
if(ledstat == LEDON) {
gpio_set_value(dev->led_gpio, 1);//led on
} else if(ledstat == LEDOFF) {
gpio_set_value(dev->led_gpio, 0);//led off
}
return 0;
}
static int led_release(struct inode *inode, struct file *filp)
{
return 0;
}
/* 设备操作函数 */
static struct file_operations gpioled_fops ={
.owner = THIS_MODULE,
.open = led_open,
.read = led_read,
.write = led_write,
.release = led_release,
};
/* 驱动入口 */
static int __init led_init(void)
{
int ret = 0;
gpioled.nd = of_find_node_by_path("/soc/leds/yellow");//通过路径查找设备节点
if(gpioled.nd == NULL) {
printk("gpioled node cant not found!\r\n");
return -EINVAL;
}else{
printk("gpioled node has been found!\r\n");
}
gpioled.led_gpio = of_get_named_gpio(gpioled.nd, "gpios", 0);
if(gpioled.led_gpio < 0) {
printk("can't get led-gpio");
return -EINVAL;
}
printk("led-gpio num = %d\r\n", gpioled.led_gpio);
ret = gpio_direction_output(gpioled.led_gpio, 1);
if(ret < 0) {
printk("can't set gpio!\r\n");
}
/* 1、创建设备号 */
//自动创建设备号
if (gpioled.major) {
gpioled.devid = MKDEV(gpioled.major, 0);
register_chrdev_region(gpioled.devid, GPIOLED_CNT, GPIOLED_NAME);
} else {
alloc_chrdev_region(&gpioled.devid, 0, GPIOLED_CNT, GPIOLED_NAME);
gpioled.major = MAJOR(gpioled.devid);
gpioled.minor = MINOR(gpioled.devid);
}
printk("gpioled major=%d,minor=%d\r\n",gpioled.major, gpioled.minor);
/* 2、初始化 cdev */
gpioled.cdev.owner = THIS_MODULE;
cdev_init(&gpioled.cdev, &gpioled_fops);
/* 3、添加一个 cdev */
cdev_add(&gpioled.cdev, gpioled.devid, GPIOLED_CNT);
/* 4、创建类 */
gpioled.class = class_create(THIS_MODULE, GPIOLED_NAME);
if (IS_ERR(gpioled.class)) {
return PTR_ERR(gpioled.class);
}
/* 5、创建设备 */
gpioled.device = device_create(gpioled.class, NULL, gpioled.devid, NULL, GPIOLED_NAME);
if (IS_ERR(gpioled.device)) {
return PTR_ERR(gpioled.device);
}
return 0;
}
/* exit */
static void __exit led_exit(void)
{
cdev_del(&gpioled.cdev); /* 删除 cdev */
unregister_chrdev_region(gpioled.devid, GPIOLED_CNT); /* 注销 */
device_destroy(gpioled.class, gpioled.devid);
class_destroy(gpioled.class);
printk("led_exit\r\n");
}
/*register and unregister*/
module_init(led_init);
module_exit(led_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("ZRR");
3、测试APP
同样参考正点原子开发部的代码
#include "stdio.h"
#include "unistd.h"
#include "sys/types.h"
#include "sys/stat.h"
#include "fcntl.h"
#include "stdlib.h"
#include "string.h"
#define LEDOFF 0
#define LEDON 1
int main(int argc, char *argv[])
{
int fd, retvalue;
char *filename;
unsigned char databuf[1];
if(argc != 3){
printf("Error Usage!\r\n");
return -1;
}
filename = argv[1];
/* 打开led驱动 */
fd = open(filename, O_RDWR);
if(fd < 0){
printf("file %s open failed!\r\n", argv[1]);
return -1;
}
databuf[0] = atoi(argv[2]); /* 要执行的操作:打开或关闭 */
/* 向/dev/led文件写入数据 */
retvalue = write(fd, databuf, sizeof(databuf));
if(retvalue < 0){
printf("LED Control Failed!\r\n");
close(fd);
return -1;
}
retvalue = close(fd); /* 关闭文件 */
if(retvalue < 0){
printf("file %s close failed!\r\n", argv[1]);
return -1;
}
return 0;
}
4、Makefile
KERNEL_DIR = /home/msm8909-source-tree/out/target/product/msm8909w/obj/KERNEL_OBJ
CUR_DIR = $(shell pwd)
ARCH=arm
CROSS_COMPILE= /home/ubuntu/Public/gcc-linaro-4.9.4-2017.01-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabihf-
export ARCH CROSS_COMPILE
obj-m := ledtest.o
out1 = ledapp
all :
make -C $(KERNEL_DIR) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) M=$(CUR_DIR) modules
$(CROSS_COMPILE)gcc ledapp.c -o $(out1) -static
.PHONY:clean
clean :
make -C $(KERNEL_DIR) M=$(CUR_DIR) clean
rm $(out1)
5) 编译驱动程序和测试
a)make编译
得到ledtest.ko和ledapp。
如果单独编译ledtest.ko出现签名问题,请看之前的文章:
msm8909 android 系统 insmod: failed to load ledtest.ko: Required key not available解决签名问题
如果ledapp出现error: No such file or directory,请看之前的文章:
解决:/system/bin/sh: ./hello: No such file or directory
b)运行测试
安装系统中,ledtest.ko放在/system/lib/modules目录下:
msm8909w:/system/lib/modules # ls
ansi_cprng.ko gpio_output.ko mtd_readtest.ko
bg-codec.ko hello.ko mtd_speedtest.ko
ledtest.ko mtd_subpagetest.k
....
装载模块:
msm8909w:/system/lib/modules # insmod ledtest.ko
msm8909w:/system/lib/modules # lsmod
Module Size Used by
ledtest 12823 0
cat /proc/devices 查看设备号:
msm8909w:/system/lib/modules # cat /proc/devices
Character devices:
...
230 gpioled
....
用ledapp点灯测试:
./ledapp /dev/gpioled 1 //打开led
./ledapp /dev/gpioled 1 //关闭led
说明:驱动代码和测试APP程序参考正点原子开发部例程修改。