3.9.6内核驱动移植

tiny210v2上有4个led,可以通过gpio直接控制,今天尝试最简单的led驱动移植,主要通过对比友善给的linux-3.0.8来进行移植。

第一步:

查得对应GPIO为GPJ_0---GPJ_3;

在drivers/char/ 下新建smdkv210_led.c

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/miscdevice.h>
#include <linux/fs.h>
#include <linux/types.h>
#include <linux/moduleparam.h>
#include <linux/slab.h>
#include <linux/ioctl.h>
#include <linux/cdev.h>
#include <linux/delay.h>

#include <mach/gpio.h>
#include <mach/regs-gpio.h>
#include <plat/gpio-cfg.h>


#define DEVICE_NAME "leds"

static int led_gpios[] = {
  S5PV210_GPJ2(0),
  S5PV210_GPJ2(1),
  S5PV210_GPJ2(2),
  S5PV210_GPJ2(3),
};

#defineLED_NUM      ARRAY_SIZE(led_gpios)


static long smdkv210_leds_ioctl(struct file *filp, unsigned intcmd,
      unsignedlong arg)
{
    switch(cmd){
      case0:
      case1:
          if (arg> LED_NUM) {
            return-EINVAL;
          }

gpio_set_value(led_gpios[arg], !cmd);
        //printk(DEVICE_NAME": %d %d\n", arg, cmd);
          break;

default:
          return-EINVAL;
    }

return0;
}

static struct file_operations smdkv210_led_dev_fops = {
  .owner          =THIS_MODULE,
  .unlocked_ioctl  = smdkv210_leds_ioctl,
};

static struct miscdevice smdkv210_led_dev = {
  .minor          =MISC_DYNAMIC_MINOR,
  .name          =DEVICE_NAME,
  .fops          =&smdkv210_led_dev_fops,
};

static int __init smdkv210_led_dev_init(void) {
    intret;
    int i;

for (i = 0;i < LED_NUM; i++) {
      ret =gpio_request(led_gpios[i], "LED");
      if (ret){
          printk("%s:request GPIO %d for LED failed, ret = %d\n", DEVICE_NAME,
              led_gpios[i], ret);
          returnret;
      }

s3c_gpio_cfgpin(led_gpios[i], S3C_GPIO_OUTPUT);
      gpio_set_value(led_gpios[i], 1);
    }

ret =misc_register(&smdkv210_led_dev);

printk(DEVICE_NAME"\tinitialized\n");

returnret;
}

static void __exit smdkv210_led_dev_exit(void) {
    int i;

for (i = 0;i < LED_NUM; i++) {
      gpio_free(led_gpios[i]);
    }

misc_deregister(&smdkv210_led_dev);
}

module_init(smdkv210_led_dev_init);
module_exit(smdkv210_led_dev_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("FriendlyARM Inc.");

修改该目录下的Kconfig文件
添加:
config SMDKV210_LEDS
    tristate"LED Support for FriendlyARM Mini210 GPIO LEDs"
    depends onMACH_SMDKV210
    defaulty
    help
    This option enables support for LEDs connectedto GPIO lines
    on tiny210v2 boards.
修改该目录下的Makefile文件
添加:
 obj-$(CONFIG_SMDKV210_LEDS)  += smdkv210_leds.o
修改arch/arm/mach-s5pv210/include/mach/gpio.h
  添加:
      #definegpio_get_value  __gpio_get_value
    #definegpio_set_value  __gpio_set_value
    #definegpio_cansleep  __gpio_cansleep
    #definegpio_to_irq  __gpio_to_irq
    #include <asm-generic/gpio.h>

#include <plat/gpio-cfg.h>
保存make,led驱动就编译到内核了。

友善的测试程序:

#include<stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
int main(int argc, char **argv)
{
int on;
int led_no;
int fd;

if (argc != 3 || sscanf(argv[1], "%d", &led_no) !=1 || sscanf(argv[2],"%d", &on) != 1 ||
on < 0 || on > 1 || led_no< 0 || led_no > 3) {
fprintf(stderr, "Usage: leds led_no 0|1\n");
exit(1);
}

fd = open("/dev/leds0", 0);
if (fd < 0) {
fd = open("/dev/leds", 0);
}
if (fd < 0) {
perror("open device leds");
exit(1);
}

ioctl(fd, on, led_no);

close(fd);
return 0;
}

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/4eec2197978e18c5525ca90941f1e2ae.html