#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <asm/io.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/bitmap.h>
#include <asm/gpio.h>
#include <linux/platform_device.h>
#include <linux/slab.h>
#include <linux/sched.h>
#include <linux/of.h> static volatile unsigned long *ledcon;
static volatile unsigned long *leddat;
static struct class *led_class;
static unsigned int pin_th;
dev_t led_dev;
struct cdev *cdev; /* 打开led灯 */
static int led_open(struct inode *inode, struct file *file)
{
*ledcon &= ~(0x3 << (pin_th * ));
*ledcon |= (0x1 << (pin_th * ));
*leddat &= ~( << pin_th);
return ;
} static struct file_operations led_fops = {
.owner = THIS_MODULE,
.open = led_open,
}; static int led_probe(struct platform_device *pdev)
{
int pin_info;
int phy_addr;
int io_base;
of_property_read_s32(pdev->dev.of_node, "pin", &pin_info);
printk("pin_info = 0x%x\n", pin_info);
of_property_read_s32(pdev->dev.of_node, "iobase", &io_base);
printk("io_base = 0x%x\n", io_base);
pin_th = pin_info & (0xffff);
phy_addr = (io_base | ((pin_info>>)<<));
ledcon = ioremap(phy_addr, );
leddat = ledcon + ; if (alloc_chrdev_region(&led_dev, MINOR(led_dev), , "led"))
return -;
cdev = cdev_alloc();
if (!cdev)
return -;
cdev_init(cdev, &led_fops);
cdev_add(cdev, led_dev, ); led_class = class_create(THIS_MODULE, "led");
device_create(led_class,NULL,led_dev,NULL,"led0"); // is /dev/led0 return ; } int led_remove(struct platform_device *pdev)
{
device_destroy(led_class, led_dev);
class_destroy(led_class);
cdev_del(cdev);
unregister_chrdev_region(led_dev, );
iounmap(ledcon);
return ;
} static struct of_device_id led_id[] = {
{.compatible = "jz2440_led"},
{},
}; static struct platform_driver led_drv = {
.probe = led_probe,
.remove = led_remove,
.driver = {
.name = "jz2440_led",
.of_match_table = led_id,
},
}; static int led_init(void)
{
platform_driver_register(&led_drv);
return ;
} static void led_exit(void)
{
platform_driver_unregister(&led_drv);
} module_init(led_init);
module_exit(led_exit); MODULE_LICENSE("GPL");

以上是驱动程序,下面是设备树dts文件:

 #define S3C2440_GPA(_nr)    ((0<<16) + (_nr))
#define S3C2440_GPB(_nr) ((1<<16) + (_nr))
#define S3C2440_GPC(_nr) ((2<<16) + (_nr))
#define S3C2440_GPD(_nr) ((3<<16) + (_nr))
#define S3C2440_GPE(_nr) ((4<<16) + (_nr))
#define S3C2440_GPF(_nr) ((5<<16) + (_nr))
#define S3C2440_GPG(_nr) ((6<<16) + (_nr))
#define S3C2440_GPH(_nr) ((7<<16) + (_nr))
#define S3C2440_GPJ(_nr) ((8<<16) + (_nr))
#define S3C2440_GPK(_nr) ((9<<16) + (_nr))
#define S3C2440_GPL(_nr) ((10<<16) + (_nr))
#define S3C2440_GPM(_nr) ((11<<16) + (_nr)) /dts-v1/; / {
model = "SMDK2440";
compatible = "samsung,smdk2440"; #address-cells = <>;
#size-cells = <>; memory@ {
device_type = "memory";
reg = <0x30000000 0x4000000>;
};

chosen {
bootargs = "noinitrd root=/dev/mtdblock4 rw init=/linuxrc console=ttySAC0,115200";
}; led {
compatible = "jz2440_led";
pin = <S3C2440_GPF()>;
iobase = <0x56000000>;
};
};
05-08 08:29