当前位置: 技术问答>linux和unix
dm6446 LED 驱动程序
来源: 互联网 发布时间:2016-11-29
本文导语: 本帖最后由 GEN216IUS 于 2010-11-25 14:52:08 编辑 LED驱动davinci_led.c /* * Copyright (C) 2006 Texas Instruments Inc * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public Lic...
/*
* Copyright (C) 2006 Texas Instruments Inc
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
/opt/mv_pro_4.0/montavista/pro/devkit/lsp/ti-davinci/include/asm-arm/arch
*/
/* include Linux files */
#include
#include
#include
#include /* printk() */
#include /* kmalloc() */
#include /* everything... */
#include /* error codes */
#include /* size_t */
#include /* Used for struct cdev */
#include /* For class_simple_create */
#include /* For IRQ_HANDLED and irqreturn_t */
#include /* for VERIFY_READ/VERIFY_WRITE/
copy_from_user */
#include
#include /* for devfs */
#include
//#include
#include
#include
/* ----- global defines ----------------------------------------------- */
#define DRIVER_NAME_LED "led"
//#define DAVINCI_PWM_TIMEOUT (1*HZ)
#define GPIO_LED_MAJOR 100 /*使用 cat /proc/devices查看不要和存在的char节点重复*/
/* For sysctl stuff */
#define DAVINCI_LED_DEBUG 1
#ifdef DAVINCI_LED_DEBUG
#define DEBLED(format, arg...) printk(KERN_ALERT DRIVER_NAME_LED " DEBUG: " format "n", ## arg )
#else
#define DEBLED(format, args...)
#endif
#define LED_suc(format, arg...) printk(KERN_INFO DRIVER_NAME_LED " SUCCESS: " format "n", ## arg )
#define LED_err(format, arg...) printk(KERN_ERR DRIVER_NAME_LED " ERROR: " format "n", ## arg )
#define LED_warn(format, arg...) printk(KERN_WARNING DRIVER_NAME_LED " WARNING: " format "n", ## arg )
#define DM6446_GPIO_LED0 11 /*GPIO11*/
#define DM6446_GPIO_LED1 12 /*GPIO12*/
/*
struct pwm_davinci_device *pwm_dev_get_by_minor(unsigned index)
{
struct pwm_davinci_device *pwm_dev;
spin_lock(&pwm_dev_array_lock);
pwm_dev = pwm_dev_array[index];
spin_unlock(&pwm_dev_array_lock);
return pwm_dev;
}
*/
static int davinci_dm644x_gpio_open(struct inode *inode, struct file *file)
{
DEBLED("DM6446 %d direction output!n",DM6446_GPIO_LED0);
gpio_set_direction(DM6446_GPIO_LED0, GIO_DIR_OUTPUT);//将GPIO改为输出数据方向
DEBLED("DM6446 %d direction output!n",DM6446_GPIO_LED1);
gpio_set_direction(DM6446_GPIO_LED1, GIO_DIR_OUTPUT);//将GPIO改为输出数据方向
return 0;/*该函数可以什么都不做,也可以加入类似初始化的设置*/
}
//输出数据
static int gpio_output_data(unsigned int gpio_pin, int signal_state)
{
if(DM6446_GPIO_LED0 != gpio_pin && DM6446_GPIO_LED1 != gpio_pin)
{
DEBLED("gpio_pin is not belong to LED!", gpio_pin);
return -ENXIO;
}
if(0 == signal_state)
{
DEBLED("DM6446 %d output data is low !n",gpio_pin);
gpio_clear(gpio_pin);//输出低电平
}
else
{
DEBLED("DM6446 %d output data is high !n",gpio_pin);
gpio_set(gpio_pin);//输出高电平
}
DEBLED("DM6446 %d direction output!n",gpio_pin);
gpio_set_direction(gpio_pin, GIO_DIR_OUTPUT);//将GPIO改为输出数据方向
return 0;
}
static int davinci_dm644x_gpio_ioctl(
struct inode *inode,
struct file *file,
unsigned int cmd,
unsigned long arg)
{
DEBLED("LED%d state is %d !n",arg,cmd);
//DEBLED("LED%d !", arg);
switch(cmd) /*cmd 表示应用程序传入的led动作,是on 还是off*/
{
case 0://灯off
if(0 == arg)
{
gpio_output_data(DM6446_GPIO_LED0, GIO_STATE_LOW);
}
else if(1 == arg)
{
gpio_output_data(DM6446_GPIO_LED1, GIO_STATE_LOW);
}
else
{
return -EINVAL;
}
break;
case 1://灯on
if(0 == arg)
{
gpio_output_data(DM6446_GPIO_LED0, GIO_STATE_HIGH);
}
else if(1 == arg)
{
gpio_output_data(DM6446_GPIO_LED1, GIO_STATE_HIGH);
}
else
{
return -EINVAL;
}
break;
default:
return -EINVAL;
break;
}
return 0;
}
/*定义驱动设备文件API,在linux系统当中,任何设备都可以当做文件的方式操作*/
static const struct file_operations davinci_dm644x_gpio_fileops = {
.owner = THIS_MODULE,
.open = davinci_dm644x_gpio_open,
.ioctl = davinci_dm644x_gpio_ioctl,
};
//内核初始化会调用该函数
static int __init davinci_dm644x_gpio_init(void)
{
int ret;
ret = register_chrdev(GPIO_LED_MAJOR, DRIVER_NAME_LED, &davinci_dm644x_gpio_fileops);
if(ret