Results 1 to 1 of 1
Hi everybody !
I'm trying to write a driver in order to control an i2c controlled audio chip.
I only have to send a few commands...
the problem is that ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 03-06-2009 #1Just Joined!
- Join Date
- Mar 2009
- Posts
- 1
Writing an I2c driver
Hi everybody !
I'm trying to write a driver in order to control an i2c controlled audio chip.
I only have to send a few commands...
the problem is that I don't understand how I have to do this...
i've read both articles about I2C drivers programming from the linuxjournal.com (I2C drivers part 1 and 2)
but nothing...I still don't get it...
I don't understand how I can choose an i2c bus (I have 3 busses i2c-0, i2c-1 and i2c-2)
here is what I've done for now, but I'm not sure at all that I'm in the good way...
Code:#include <linux/init.h> #include <linux/module.h> #include <linux/kernel.h> #include <linux/cdev.h> #include <linux/fs.h> #include <linux/proc_fs.h> #include <linux/device.h> #include <linux/i2c.h> //allocation dynamique des nombres majeurs; 0 #define MTV7109AUDIO_MAJOR 0 MODULE_LICENSE("x"); static unsigned short normal_i2c[] = { I2C_CLIENT_END }; static unsigned short normal_i2c_range[] = { 0x00, 0x15, I2C_CLIENT_END }; struct w5094_dev{ struct cdev cdev; struct i2c_client clt; }; I2C_CLIENT_INSMOD_1(w5094); static int w5094_attach_adapter(struct i2c_adapter *adapter); static int w5094_detach_client(struct i2c_client * client); int MTV7109Audio_open(struct inode * inode, struct file *filp); int MTV7109Audio_ioctl(struct inode * inode, struct file *filp, unsigned int cmd, unsigned long arg); static struct i2c_driver i2c_w5094_driver = { .driver = { .name = "w5094", }, .attach_adapter = w5094_attach_adapter, .detach_client = w5094_detach_client, }; static int w5094_attach_adapter(struct i2c_adapter *adapter) { if (!(adapter->class & I2C_CLASS_SOUND )) //Sound device return 0; return 0;//i2c_detect(adapter, &addr_data, w5094_detect); } static int w5094_detach_client(struct i2c_client * client) { i2c_detach_client(client); kfree(i2c_get_clientdata(client)); return 0; } int mtv7109audio_major=MTV7109AUDIO_MAJOR; int mtv7109audio_minor=0; struct device mtv7109audio_dev; struct file_operations mtv7109audio_fops = { .owner = THIS_MODULE, .open = MTV7109Audio_open, .ioctl = MTV7109Audio_ioctl, }; int MTV7109Audio_open(struct inode * inode, struct file *filp) { struct w5094_dev * dev; dev = container_of(inode->i_cdev, struct w5094_dev, cdev); filp->private_data = dev; return 0; } int MTV7109Audio_ioctl(struct inode * inode, struct file *filp, unsigned int cmd, unsigned long arg) { //switch sur les commandes return 0; } static int MTV7109Audio_init(void) { int result, i; dev_t dev=0; if(mtv7109audio_major) { dev = MKDEV(mtv7109audio_major,mtv7109audio_minor); result = register_chrdev_region(dev,1,"MTV7109Audio"); } else { result=alloc_chrdev_region(&dev,mtv7109audio_minor,1,"MTV7109Audio"); mtv7109audio_major=MAJOR(dev); } if (result<0) { printk (KERN_WARNING "MTV7109Audio: can't get major %d\n",mtv7109audio_major); return result; } return i2c_add_driver(&i2c_w5094_driver); } static void MTV7109Audio_exit(void) { dev_t dev = MKDEV(mtv7109audio_major,mtv7109audio_minor); unregister_chrdev_region(dev,1); i2c_del_driver(&i2c_w5094_driver); } module_init(MTV7109Audio_init); module_exit(MTV7109Audio_exit);


Reply With Quote
