之前实现的一个adc驱动,贴一下
#include <linux/kernel.h>#include <linux/init.h>
#include <linux/module.h>
#include <linux/delay.h>
#include <linux/gpio.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
#include <linux/interrupt.h>
#include <asm/irq.h>
#include <linux/iio/consumer.h>
#ifdef CONFIG_OF
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/of_platform.h>
#endif
#define DESC(x) 1
#if DESC("global area")
static int global_major;
static struct class *adc0_class;
static struct device *adc0_device;
static struct device *adc0_dev = NULL;
#endif
static int adc0_probe(struct platform_device *pdev)
{
/*Get io channel*/
adc0_dev = &pdev->dev;
printk("%s \n", __FUNCTION__);
return 0;
}
static int adc0_remove(struct platform_device *pdev)
{
return 0;
}
static const struct of_device_id of_adc0_match[] = {
{ .compatible = "sunrex,adc_test" },
{ /* Sentinel */ }
};
static struct platform_driver adc0_driver = {
.probe = adc0_probe,
.remove = adc0_remove,
.driver = {
.owner = THIS_MODULE,
.name= "adc0",
#ifdef CONFIG_OF
.of_match_table= of_adc0_match,
#endif
},
};
static ssize_t adc0_read(struct file *filep, char __user *usrbuf, size_t size, loff_t *offset)
{
int val,ret;
struct iio_channel *chan; //定义 IIO 通道结构体
printk("%s \n", __FUNCTION__);
chan = iio_channel_get(adc0_dev, NULL); //获取 IIO 通道结构体
ret = iio_read_channel_raw(chan, &val);
printk("adc0 value = %u \n", val);
if (ret < 0)
{
printk("Read channel failed. \n");
return EIO;
}
copy_to_user(usrbuf, (void *)&val, size);
return 0;
}
static ssize_t adc0_write(struct file *filep, const char __user *usrbuf, size_t size, loff_t *offset)
{
return 0;
}
static int adc0_open(struct inode *inode, struct file *filep)
{
return 0;
}
static int adc0_release(struct inode *inode, struct file *filep)
{
return 0;
}
static struct file_operations motor_fops = {
.owner = THIS_MODULE,
.read= adc0_read,
.write = adc0_write,
.open= adc0_open,
.release = adc0_release,
};
static int __init adc0_init(void)
{
/*system will auto allocate major*/
global_major = register_chrdev(0, "adc0", &motor_fops);
/*create class*/
adc0_class = class_create(THIS_MODULE, "adc0_drv");
/*create device for app*/
adc0_device = device_create(adc0_class, NULL, MKDEV(global_major, 0), NULL, "adc0");
//if (ret < 0)
// return ret;
platform_driver_register(&adc0_driver);
return 0;
}
static void __exit adc0_exit(void)
{
platform_driver_unregister(&adc0_driver);
device_destroy(adc0_class,MKDEV(global_major, 0));
class_destroy(adc0_class);
//unregister_chrdev_region(MKDEV(global_major, 0), 1);
unregister_chrdev(global_major, "adc0");
}
module_init(adc0_init);
module_exit(adc0_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("zhang jiawei");
正在看这方面的内容,先收藏!谢谢! ADC MARK
页:
[1]