Advertisement
Guest User

Untitled

a guest
May 18th, 2012
514
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.03 KB | None | 0 0
  1. #include <linux/module.h>
  2. #include <linux/init.h>
  3. #include <linux/kernel.h>
  4. #include <linux/io.h>
  5. #include <linux/fs.h>
  6. #include <linux/device.h>
  7. #include <linux/sched.h>
  8. #include <linux/interrupt.h>
  9. #include <linux/i2c-dev.h>
  10. #include <asm/uaccess.h>
  11. #include <mach/irqs.h>
  12.  
  13. // base addresses of the 3 I2C units
  14. #define BSC0_BASE 0x20205000
  15. #define BSC1_BASE 0x20804000
  16. #define BSC2_BASE 0x20805000
  17.  
  18. // registers for each unit
  19. #define BSC_C 0
  20. #define BSC_S 1
  21. #define BSC_DLEN 2
  22. #define BSC_A 3
  23. #define BSC_FIFO 4
  24. #define BSC_DIV 5
  25. #define BSC_DEL 6
  26. #define BSC_CLKT 6
  27.  
  28. #define BSC_C_I2CEN (1 << 15)
  29. #define BSC_C_INTR (1 << 10)
  30. #define BSC_C_INTT (1 << 9)
  31. #define BSC_C_INTD (1 << 8)
  32. #define BSC_C_ST (1 << 7)
  33. #define BSC_C_CLEAR ((1 << 4) | (1 << 5))
  34. #define BSC_C_READ (1 << 0)
  35.  
  36. #define BSC_S_CLKT (1 << 9)
  37. #define BSC_S_ERR (1 << 8)
  38. #define BSC_S_RXF (1 << 7)
  39. #define BSC_S_TXE (1 << 6)
  40. #define BSC_S_RXD (1 << 5)
  41. #define BSC_S_TXD (1 << 4)
  42. #define BSC_S_RXR (1 << 3)
  43. #define BSC_S_TXW (1 << 2)
  44. #define BSC_S_DONE (1 << 1)
  45. #define BSC_S_TA (1 << 0)
  46.  
  47. MODULE_LICENSE("GPL");
  48. MODULE_AUTHOR("Frank Buss");
  49. MODULE_DESCRIPTION("I2C driver");
  50. MODULE_SUPPORTED_DEVICE("I2C");
  51.  
  52. const char *g_device_name = "i2c";
  53. static int g_major;
  54. static struct class *g_class_i2c;
  55. static struct device *g_dev_i2c;
  56.  
  57. static u32 *g_base0 = NULL;
  58. static u32 g_slaveAddress = 0;
  59.  
  60. // transfer timeout time in milliseconds
  61. #define TIMEOUT_MS 500
  62.  
  63. // in jiffies
  64. #define I2C_TIMEOUT_JIFFIES HZ*TIMEOUT_MS/1000
  65.  
  66. // maximum number of bytes for sending and receiving one message
  67. #define MAX_BYTE_COUNT 256
  68.  
  69. // IRQ locking and signaling
  70. static DECLARE_WAIT_QUEUE_HEAD(g_irqQueue);
  71. static int g_doneFlag = 0;
  72.  
  73. // send/receive buffer
  74. #define MAX_DATA_SIZE 256
  75. static uint8_t g_data[MAX_DATA_SIZE];
  76. static int g_len;
  77. static int g_index;
  78.  
  79. // set alternate function 0 on GPIO 0 and GPIO 1 for SDA0/SCL0
  80. static void set_i2c_function(void)
  81. {
  82.     // TODO: this is a really bad hack, maybe should call an exported function from bcm2708_gpio.c
  83.     #define GPIO_FSEL_ALT0 4
  84.     u32* gpio = (u32*) ioremap(0x20200000, SZ_16K);
  85.     gpio[0] = (gpio[0] & ~0x3f) | GPIO_FSEL_ALT0 | (GPIO_FSEL_ALT0 << 3);
  86.     iounmap((void*) gpio);
  87. }
  88.  
  89. static int i2c_open(struct inode *inode, struct file *filp)
  90. {
  91.     // TODO: maybe exclusive access
  92.     return 0;
  93. }
  94.  
  95. static int i2c_release(struct inode *inode, struct file *filp)
  96. {
  97.     return 0;
  98. }
  99.  
  100. static irqreturn_t i2c_interrupt(int irq, void *dev_id)
  101. {
  102.     unsigned long flags;
  103.     local_irq_save(flags);
  104.    
  105.     // send next bytes to FIFO
  106.     while (g_base0[BSC_S] & BSC_S_TXW) {
  107.         if (g_index == g_len) break;
  108.         g_base0[BSC_FIFO] = g_data[g_index++];
  109.     }
  110.    
  111.     // receive next bytes from FIFO
  112.     while (g_base0[BSC_S] & BSC_S_RXD) {
  113.         if (g_index == g_len) break;
  114.         g_data[g_index++] = g_base0[BSC_FIFO];
  115.     }
  116.    
  117.     // transfer finished
  118.     if (g_base0[BSC_S] & BSC_S_DONE) {
  119.         g_base0[BSC_S] = BSC_S_DONE;
  120.         g_doneFlag = 1;
  121.         wake_up_interruptible(&g_irqQueue);
  122.     }
  123.    
  124.     local_irq_restore(flags);
  125.     return IRQ_HANDLED;
  126. }
  127.  
  128. static ssize_t i2c_read(struct file *filp, char *buffer, size_t len, loff_t *offset)
  129. {
  130.     unsigned long flags;
  131.    
  132.     // test parameters
  133.     if (len > MAX_DATA_SIZE) {
  134.         return -EINVAL;
  135.     }
  136.    
  137.     // disable interrupts
  138.     local_irq_save(flags);
  139.  
  140.     // disable BSC
  141.     g_base0[BSC_C] = 0;
  142.    
  143.     // init interrupt data
  144.     g_len = len;
  145.     g_index = 0;
  146.     g_doneFlag = 0;
  147.  
  148.     // set slave address
  149.     g_base0[BSC_A] = g_slaveAddress;
  150.  
  151.     // set len and start transfer
  152.     g_base0[BSC_DLEN] = len;
  153.  
  154.     // enable interrupts
  155.     local_irq_restore(flags);
  156.  
  157.     // start transfer, interrupt on RXR and DONE
  158.     g_base0[BSC_C] = BSC_C_READ | BSC_C_I2CEN | BSC_C_CLEAR | BSC_C_ST | BSC_C_INTR | BSC_C_INTD;
  159.    
  160.     // non-blocking wait for transfer end
  161.     if (wait_event_interruptible_timeout(g_irqQueue, g_doneFlag, I2C_TIMEOUT_JIFFIES) == 0) {
  162.         printk(KERN_INFO "read timeout\n");
  163.         return -EFAULT;
  164.     }
  165.  
  166.     // read rest from FIFO, if there is data
  167.     while (g_base0[BSC_S] & BSC_S_RXD) {
  168.         if (g_index == g_len) break;
  169.         g_data[g_index++] = g_base0[BSC_FIFO];
  170.     }
  171.    
  172.     // reset flags
  173.     g_base0[BSC_S] = BSC_S_ERR | BSC_S_CLKT | BSC_S_DONE;
  174.  
  175.     // disable BSC
  176.     g_base0[BSC_C] = 0;
  177.    
  178.     // copy to user space
  179.     if (copy_to_user(buffer, g_data, len)) return -EFAULT;
  180.  
  181.     return len;
  182. }
  183.  
  184. static ssize_t i2c_write(struct file *filp, const char *buffer, size_t len, loff_t *off)
  185. {
  186.     unsigned long flags;
  187.  
  188.     // test parameters
  189.     if (len > MAX_DATA_SIZE) {
  190.         return -EINVAL;
  191.     }
  192.    
  193.     // copy to kernel space
  194.     if (copy_from_user(g_data, buffer, len)) return -EFAULT;
  195.  
  196.     // disable interrupts
  197.     local_irq_save(flags);
  198.  
  199.     // disable BSC
  200.     g_base0[BSC_C] = 0;
  201.    
  202.     // init interrupt data
  203.     g_len = len;
  204.     g_index = 0;
  205.     g_doneFlag = 0;
  206.  
  207.     // set slave address
  208.     g_base0[BSC_A] = g_slaveAddress;
  209.  
  210.     // set len and start transfer
  211.     g_base0[BSC_DLEN] = len;
  212.  
  213.     // enable interrupts
  214.     local_irq_restore(flags);
  215.  
  216.     // start transfer, interrupt on TXW and DONE
  217.     g_base0[BSC_C] = BSC_C_I2CEN | BSC_C_CLEAR | BSC_C_ST | BSC_C_INTT | BSC_C_INTD;
  218.    
  219.     // non-blocking wait for transfer end
  220.     if (wait_event_interruptible_timeout(g_irqQueue, g_doneFlag, I2C_TIMEOUT_JIFFIES) == 0) {
  221.         printk(KERN_INFO "write timeout\n");
  222.         return -EFAULT;
  223.     }
  224.  
  225.     // reset flags
  226.     g_base0[BSC_S] = BSC_S_ERR | BSC_S_CLKT | BSC_S_DONE;
  227.  
  228.     // disable BSC
  229.     g_base0[BSC_C] = 0;
  230.    
  231.     return len;
  232. }
  233.  
  234. static long i2c_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  235. {
  236.     switch (cmd) {
  237.         case I2C_SLAVE:
  238.             g_slaveAddress = arg;
  239.             return 0;
  240.        
  241.         default:
  242.             return -ENOIOCTLCMD;
  243.     }
  244. }
  245.  
  246. struct file_operations i2c_fops = {
  247.     owner: THIS_MODULE,
  248.     open: i2c_open,
  249.     release: i2c_release,
  250.     read: i2c_read,
  251.     write: i2c_write,
  252.     unlocked_ioctl: i2c_ioctl,
  253. };
  254.  
  255. static int __init i2c_init(void)
  256. {
  257.     int ret;
  258.  
  259.     // init irq
  260.     init_waitqueue_head(&g_irqQueue);
  261.     ret = request_irq(IRQ_I2C, i2c_interrupt, IRQF_DISABLED, "i2c", NULL);
  262.     if (ret) {
  263.         printk(KERN_INFO "request_irq failed: %i\n", ret);
  264.         return ret;
  265.     }
  266.  
  267.     // init hardware
  268.     set_i2c_function();
  269.     g_base0 = (u32*) ioremap(BSC0_BASE, SZ_16K);
  270.     if (!g_base0) {
  271.         printk(KERN_ERR "ioremap failed\n");
  272.         return -EINVAL;
  273.     }
  274.    
  275.     // register character device
  276.     g_major = register_chrdev(0, g_device_name, &i2c_fops);
  277.     if (g_major < 0) {
  278.         printk(KERN_ERR "register_chrdev failed with %d\n", g_major);
  279.         return g_major;
  280.     }
  281.     g_class_i2c = class_create(THIS_MODULE, g_device_name);
  282.     if (IS_ERR(g_class_i2c)) return PTR_ERR(g_class_i2c);
  283.     g_dev_i2c = device_create(g_class_i2c, NULL, MKDEV(g_major, 0), NULL, g_device_name);
  284.     if (IS_ERR(g_dev_i2c)) return PTR_ERR(g_dev_i2c);
  285.        
  286.     printk(KERN_INFO "initialized\n");
  287.    
  288.     return 0;
  289. }
  290.  
  291. static void __exit i2c_exit(void)
  292. {
  293.     // unregister character device
  294.     device_destroy(g_class_i2c, MKDEV(g_major, 0));
  295.     class_destroy(g_class_i2c);
  296.     unregister_chrdev(g_major, g_device_name);
  297.    
  298.     // unmap register area
  299.     if (g_base0) {
  300.         iounmap((void*) g_base0);
  301.         g_base0 = NULL;
  302.     }
  303.  
  304.     // release interrupt
  305.     free_irq(IRQ_I2C, NULL);
  306.  
  307.     printk(KERN_INFO "module unloaded\n");
  308. }
  309.  
  310. module_init(i2c_init);
  311. module_exit(i2c_exit);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement