?? tw2835.c
字號:
#include <linux/module.h>#include <linux/config.h>#include <linux/sched.h>#include <linux/kernel.h>#include <linux/init.h>#include <linux/interrupt.h>#include <linux/spinlock.h>#include <linux/proc_fs.h>#include <asm/uaccess.h>#include <linux/miscdevice.h>#include "tw2835.h"MODULE_AUTHOR("GM Corp.");MODULE_LICENSE("GM License");#define IPMODULE MULTI#define IPNAME TW2835DECLARE_MUTEX(bus_lock);static int tw2835_open(struct inode *inode, struct file *file){ return 0;}static int tw2835_release(struct inode *inode, struct file *file){ return 0;}void tw2835_data_write(u8 chip, u8 bank, u8 reg, int num, unsigned char *buf){ int i; u8 dev = 0; volatile u8 *base; if(dev >= IP_COUNT) return; base = (u8*)IP_va_base[dev]; down(&bus_lock); switch (chip)
{
case 0:
*(base) = 0xFC | bank;
break;
case 1:
*(base) = 0xF3 | (bank << 2);
break;
case 2:
*(base) = 0xCF | (bank << 4);
break; case 3: *(base) = 0x3F | (bank << 6); break;
default: break;
} *(base+0x400) = reg; for(i = 0; i < num; i++, buf++) *(base + 0x800) = *buf; up(&bus_lock); }EXPORT_SYMBOL(tw2835_data_write);void tw2835_data_read(u8 chip, u8 bank, u8 reg, int num, unsigned char *buf){ int i; u8 dev = 0; volatile u8 *base; if(dev >= IP_COUNT) return; base = (u8*)IP_va_base[dev]; down(&bus_lock); switch (chip)
{
case 0:
*(base) = 0xFC | bank;
break;
case 1:
*(base) = 0xF3 | (bank << 2);
break;
case 2:
*(base) = 0xCF | (bank << 4);
break; case 3: *(base) = 0x3F | (bank << 6); break;
default: break;
} *(base+0x400) = reg; for(i = 0; i < num; i++, buf++) *buf = *(base + 0xC00); up(&bus_lock);}EXPORT_SYMBOL(tw2835_data_read);static int tw2835_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg){ tw2835_params gm; switch (cmd) { case tw2835_ioctl_write: if (copy_from_user (&gm, (struct tw2835_params*)arg, sizeof(tw2835_params))) return -EFAULT; tw2835_data_write(gm.chip,gm.bank,gm.reg,gm.num,gm.buf); return 0; case tw2835_ioctl_read: if (copy_from_user (&gm, (struct tw2835_params*)arg, sizeof(tw2835_params))) return -EFAULT; tw2835_data_read(gm.chip,gm.bank,gm.reg,gm.num,gm.buf); return 0; default: printk("GPIO ioctl function error\n"); return -EINVAL; }}static const struct file_operations tw2835_fops = { .owner=THIS_MODULE, .ioctl=tw2835_ioctl, .open=tw2835_open,};static struct miscdevice tw2835_miscdev = { .minor = MISC_DYNAMIC_MINOR, .name = "tw2835", .fops = &tw2835_fops,};static int __init tw2835_init(void){ int ret; unsigned char buf_1[10]={0xa0,0xa1,0xa2,0xa3,0xa4,0xa5,0xa6,0xa7,0xa8,0xa9}, buf_2[10]; int i; ret = misc_register(&tw2835_miscdev); if (ret < 0) { printk(KERN_ERR "tlclk: misc_register returns %d.\n", ret); return ret; }#if 0 //for debug tw2835_data_write(0, 0x2, 0x20, 10, buf_1); tw2835_data_read(0, 0x2, 0x20, 10, buf_2); for(i = 0; i < 10; i++) if(buf_1[i] != buf_2[i]) printk("Compare data fail, read data = 0x%x, correct data = 0x%x\n",buf_2[i],buf_1[i]); printk("Test finished\n");#endif return 0;}static void __exit tw2835_exit(void){ misc_deregister (&tw2835_miscdev);}module_init(tw2835_init);module_exit(tw2835_exit);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -