smart210驱动(3)led驱动

Makfile

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
*/

#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, ... */

#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;

/*
* 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
*/

struct leds_dev
{
	volatile unsigned long con;
	volatile unsigned long data;
	volatile unsigned long pud;
	volatile unsigned long drv;
};

#define LED_BASE_ADDR           0xE0200280
static volatile unsigned long *gpjcon;
static volatile unsigned long *gpjdat;

static struct leds_dev *leds_base;

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

	/* set as output */
	*gpjcon |= (1 << 4 * 0) + (1 << 4 * 1) + (1 << 4 * 2) + (1 << 4 * 3);
	
	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(cmd == 0)    // close
        {       
                *gpjdat |= (1 << opt);
        }
        else if(1 == cmd)  // open
        {       
                *gpjdat &= ~(1 << opt);
        }
        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;
	
	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);


	gpjcon = (volatile unsigned long *)ioremap(LED_BASE_ADDR,0x10);
	if(!gpjcon){
		goto remap_err;
	}	
	gpjdat = gpjcon + 1;

	/* default high */
	*gpjdat = 0x0f;

	return 0;

remap_err:
	device_destroy(priv_data->priv_class, priv_data->dev);
	class_destroy(priv_data->priv_class);

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)
{
	pr_info("%s called.\n", __func__);

	iounmap(gpjcon);

	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");

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值