smart210驱动(4)leds-gpiolib

Makefile

TARGET	:= leds_drv

obj-m	+= $(TARGET).o

ROOTFS	= /home/flinn/smart210-SDK/fs/drv
KERNEL	= /home/flinn/smart210-SDK/linux-3.10.79

all:
	make -C $(KERNEL) M=`pwd` modules

clean:
	make -C $(KERNEL) M=`pwd` clean

install:
	cp $(TARGET).ko $(ROOTFS)

 

make.sh

#!/bin/bash

echo -e "\e[1;31m##make clean \e[0m"
make clean

echo -e "\e[1;31m##make \e[0m"
make

echo -e "\e[1;31m##make install \e[0m"
make install

leds_drv.c

/*
* linux-3.10.27
* arm-linux-gcc-4.5.1
*
* @ leds driver with gpiolib
*/

#include <linux/module.h>
#include <linux/init.h>   /* module_init, ... */
#include <linux/kernel.h> /* everything */

#include <linux/cdev.h>   /* cdev_init, ... */
#include <linux/fs.h>     /* file_operations,  */
#include <linux/device.h>  /* class_create,... */
#include <linux/platform_device.h>
#include <linux/slab.h>   /* kmalloc, ... */
#include <asm/io.h>       /* ioremap,... */

#include <linux/uaccess.h>   /* copy_from_user, ... */

#include <linux/gpio.h>

/*
 *driver/gpio/gpio-samsung.c
	core_initcall(samsung_gpiolib_init);	
		exynos4_gpiolib_init();
*/

#define DEVICE_NAME		"myleds"
#define DEF_MAJOR		0
struct priv_data
{
	char *name;
	int major;

	dev_t dev;
	struct cdev *cdev;
	struct class *priv_class;
};

static struct priv_data *priv_data;

struct led_data {
	char *name;
	unsigned gpio;
	unsigned state;
};


/*
* led resource :
*   led1 - GPJ2_0
*   led2 - GPJ2_1
*   led3 - GPJ2_2
*   led4 - GPJ2_3
*
* GPJ2CON - 0xE0200280 
*  
* GPJ2CON[0] [3:0] 
*      0000 = Input  
*      0001 = Output 
*      0010 = MSM_DATA[0] 
*      0011 = KP_COL[1] 
*      0100 = CF_DATA[0] 
*      0101 = MHL_D7 
*      0110 ~ 1110 = Reserved 
*      1111 = GPJ2_INT[0]
* ---
* GPJ2DAT - 0xE0200284
*/

static struct led_data leds[] = 
{
	{"led1", S5PV210_GPJ2(0), 1},
	{"led2", S5PV210_GPJ2(1), 1},
	{"led3", S5PV210_GPJ2(2), 1},
	{"led4", S5PV210_GPJ2(3), 1},
};

static int leds_open (struct inode *inode, struct file *file)
{
	pr_info("%s called.\n", __func__);

	
	return 0;
}

static int leds_close (struct inode *inode, struct file *file)
{
	pr_info("%s called.\n", __func__);

	return 0;
}


static ssize_t led_dev_write (struct file *pfile, const char __user *usrbuf, 
	size_t len, loff_t *offset )
{       
    int revbuf[8];
    int cmd, opt;
       
    printk(KERN_INFO "write \n");   
    if(copy_from_user(revbuf,usrbuf,8))
    {       
        return -EFAULT;
    }
       
    cmd = revbuf[0];
    opt = revbuf[1];
    //opt = opt & 0xf;
        
    printk(KERN_NOTICE "cmd : %d opt : %d \n", cmd, opt);
	if(opt > ARRAY_SIZE(leds) - 1)
		return -EINVAL;
	
    if(cmd == 0)    // close
    {        	
		gpio_set_value(leds[opt].gpio, 1);
    }
    else if(1 == cmd)  // open
    {       
        gpio_set_value(leds[opt].gpio, 0);
    }
    else
    {       
        // do nothing .
    }
		
    return 0;
}


static const struct file_operations fops = {
	.owner = THIS_MODULE,
	.open = leds_open,
	.release = leds_close,
	.write = led_dev_write,
};

static int leds_drv_init(void)
{
	int ret = 0;
	int i = 0;
	
	pr_info("%s called.\n", __func__);

	priv_data = kmalloc(sizeof(struct priv_data), GFP_KERNEL);
	if(!priv_data){
		pr_err("no mem.\n");
		return -ENOMEM;
	}

	priv_data->name = DEVICE_NAME;
	priv_data->major = DEF_MAJOR;

	if(priv_data->major)
	{
		priv_data->dev = MKDEV(priv_data->major, 0);
		ret = register_chrdev_region(priv_data->dev, 1, priv_data->name);
	}
	else 
	{
		ret = alloc_chrdev_region(&priv_data->dev, 1, 1, priv_data->name);
		priv_data->major = MAJOR(priv_data->dev);
	}

	if(ret < 0){
		pr_err("register chrdev err.\n");
		goto chrdev_err;
	}

	/* add cdev */
	priv_data->cdev = cdev_alloc();
	cdev_init(priv_data->cdev, &fops);
	priv_data->cdev->owner = THIS_MODULE;
	priv_data->cdev->ops = &fops;
	cdev_add(priv_data->cdev, priv_data->dev, 1);
	
	/* add class */
	priv_data->priv_class = class_create(THIS_MODULE, priv_data->name);
	if(!priv_data->priv_class){
		pr_err("class create fail.\n");
		goto class_err;
	}
	
	/* create the device below /dev/ */
	device_create(priv_data->priv_class, NULL, priv_data->dev, NULL, priv_data->name);

	for(i = 0; i < ARRAY_SIZE(leds); i++){
		ret = gpio_request(leds[i].gpio, leds[i].name);
		if(ret){
			pr_err("request err.\n");
			break;
		}
		gpio_direction_output(leds[i].gpio, leds[i].state);
		gpio_set_value(leds[i].gpio, leds[i].state);
	}
	
	return 0;

class_err:
	cdev_del(priv_data->cdev);
	register_chrdev_region(priv_data->dev, 1, priv_data->name);
	
chrdev_err:
	kfree(priv_data);
	return -EFAULT;
}

static void leds_drv_exit(void)
{
	int i = 0;
	pr_info("%s called.\n", __func__);

	for(i = 0; i < ARRAY_SIZE(leds); i++){
		gpio_free(leds[i].gpio);
	}

	device_destroy(priv_data->priv_class, priv_data->dev);
	class_destroy(priv_data->priv_class);
	cdev_del(priv_data->cdev);
	unregister_chrdev_region(priv_data->dev, 1);
	kfree(priv_data);
}


module_init(leds_drv_init);
module_exit(leds_drv_exit);
MODULE_LICENSE("GPL");

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值