Advertisement
pou

bt125

pou
Jan 4th, 2017
519
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 23.33 KB | None | 0 0
  1. #include <linux/kernel.h>
  2. #include <linux/device.h>
  3. #include <linux/mutex.h>
  4. #include <linux/module.h>
  5. #include <linux/i2c.h>
  6. #include <linux/slab.h>
  7. #include <asm/byteorder.h>
  8. #include <linux/of.h>
  9. #include <linux/of_device.h>
  10.  
  11. #include <linux/cdev.h>
  12. #include <linux/errno.h>
  13. #include <linux/fs.h>
  14. #include <asm/uaccess.h>
  15. #include <linux/list.h>
  16. #include <linux/of_address.h>
  17.  
  18. #include <linux/of_gpio.h>
  19.  
  20.  
  21. #include <linux/poll.h>
  22. #include <linux/of_irq.h>
  23. #include <linux/interrupt.h>  // Required for the IRQ code
  24.  
  25.  
  26.  
  27.  
  28. #define DEV_COUNT 1
  29. #define MODNAME "bt125"
  30.  
  31. #define BT125_GPIO_IN_REG       0x00
  32. #define BT125_GPIO_OUT_REG      0x01
  33. #define BT125_PWR_CTRL_REG      0x04
  34. #define BT125_BKL_REG       0x08
  35. #define BT125_PWR_REG       0x09
  36. #define BT125_TACHO_REG     0x10
  37. #define BT125_WDT_REG       0x20
  38. #define BT125_FW_REG            0x3C
  39.  
  40.  
  41. #define BT125_EXP_GPIO      0x13
  42. #define BT125_EXP_LED       0x12
  43.  
  44.  
  45. #define BT125_ADP_OI_REG        0x2A
  46.  
  47. //https://code.google.com/p/ldd-templates/source/browse/drivers/i2c/sample-i2c-client.c
  48. //http://www.embedded-bits.co.uk/2009/i2c-in-the-2632-linux-kernel/
  49. //http://renjucnair.blogspot.ca/2012/01/writing-i2c-client-driver.html
  50. //http://lxr.free-electrons.com/source/drivers/hwmon/lm75.c#L325
  51.  
  52.  
  53.  
  54. enum bt125_func_modules{
  55.     bt125_IN,
  56.     bt125_OUT,
  57.     bt125_PWR_CTRL,
  58.     bt125_BKL,
  59.     bt125_PWR,
  60.     bt125_TACHO,
  61.     bt125_WDT, 
  62.     bt125_DIP,
  63.     bt125_ROTARY,
  64.     bt125_LED,
  65.     };
  66.  
  67. enum bt125_data_access{
  68.     bt125_BIT,
  69.     bt125_8B,
  70.     bt125_16B,
  71.     bt125_32B,
  72. };
  73.  
  74. enum bt125_access{
  75.     bt125_READ=0x01,
  76.     bt125_WRITE=0x02,
  77.     bt125_READWRITE=0x03,
  78. };
  79.  
  80. struct bt125_k{
  81.     u8 bt125_modul;
  82.     enum bt125_data_access data_access;
  83.     enum bt125_access access;
  84.     u8 i2c_addr;
  85.     int i2c_mask;//hodnota vyctena z I2C se vymaskuje touto maskou, u binarniho pristupu se ocekavaji pouze bity v teto masce
  86.     char * name;
  87. };
  88.    
  89. static const struct     bt125_k bt125_keys_exp[]={
  90.     {bt125_DIP,bt125_BIT,bt125_READ,BT125_EXP_GPIO,0xf0,"D:"},
  91.     {bt125_ROTARY,bt125_8B,bt125_READ,BT125_EXP_GPIO,0x0f,"R:"},
  92.     {bt125_LED,bt125_BIT,bt125_READWRITE,BT125_EXP_LED,0xf0,"L:"},
  93.     { }
  94. };
  95.  
  96. static const struct     bt125_k bt125_keys_cpu[]={
  97.     {bt125_IN,bt125_BIT,bt125_READ,BT125_GPIO_IN_REG,0x0f,"IN:"},
  98.     {bt125_OUT,bt125_BIT,bt125_READWRITE,BT125_GPIO_OUT_REG,0x01,"OUT:"},
  99.     {bt125_PWR_CTRL,bt125_BIT,bt125_READWRITE,BT125_PWR_CTRL_REG,0x01,"PWR_CTRL:"},
  100.     {bt125_BKL,bt125_8B,bt125_READ,BT125_BKL_REG,0xff,"BKL:"},
  101.     {bt125_PWR,bt125_8B,bt125_READ,BT125_PWR_REG,0xff,"PWR:"},
  102.     {bt125_TACHO,bt125_32B,bt125_READ,BT125_TACHO_REG,0xffffffff,"TCH:"},
  103.     {bt125_WDT,bt125_8B,bt125_WRITE,BT125_WDT_REG,0xff,"WDT:"},
  104.     { }
  105. };
  106.  
  107.  
  108. static const struct     bt125_k bt125_adp5589[]={
  109.     {bt125_LED,bt125_BIT,bt125_READWRITE,BT125_ADP_OI_REG,0x78,"L:"},
  110.     { }
  111. };
  112.  
  113.  
  114.  
  115. ////////////////////////
  116.  
  117. struct bt125_i2c_handler{
  118.     struct list_head list;
  119.     struct i2c_client  * client;
  120.     int minor;
  121.     int major;
  122. };
  123.  
  124.  
  125.  
  126. static DEFINE_SPINLOCK(bt125_i2c_list_lock);
  127. static LIST_HEAD(bt125_i2c_dev_list);
  128.  
  129. static struct bt125_i2c_handler *bt125_i2c_dev_get_by_minor(unsigned minor,unsigned major){
  130.     struct bt125_i2c_handler *handler;
  131.     //printk(KERN_INFO "%s %s Try find  i2c handler %d , %d \n",MODNAME,__func__,minor,major);
  132.     spin_lock(&bt125_i2c_list_lock);
  133.     list_for_each_entry(handler, &bt125_i2c_dev_list,list) {
  134.         if (handler->minor == minor && handler->major == major)
  135.             goto found;
  136.     }
  137.     handler = NULL;
  138. found:
  139.     spin_unlock(&bt125_i2c_list_lock);
  140.     return handler;
  141. }
  142.  
  143.  
  144.  
  145.  
  146. static struct bt125_i2c_handler *bt125_get_free_i2c_dev(struct i2c_client *client,int minor, int major){
  147.     struct bt125_i2c_handler *handler;
  148.  
  149.     //Sanity check (minor and major must be unique)
  150.     if(bt125_i2c_dev_get_by_minor(minor,major)!=NULL){
  151.         printk(KERN_ERR "%s %s Device allredy exist. DRIVER!ERRO!! \n",MODNAME,__func__);
  152.         return ERR_PTR(-ENXIO);
  153.     }
  154.    
  155.    
  156.     //printk(KERN_INFO "%s %s Add  i2c handler %d, %d \n",MODNAME,__func__,minor,major);
  157.     handler = kzalloc(sizeof(*handler), GFP_KERNEL);
  158.     if (!handler)
  159.         return ERR_PTR(-ENOMEM);
  160.     handler->client = client;
  161.     handler->minor = minor;
  162.     handler->major = major;
  163.    
  164.  
  165.     spin_lock(&bt125_i2c_list_lock);
  166.     list_add_tail(&handler->list, &bt125_i2c_dev_list);
  167.     spin_unlock(&bt125_i2c_list_lock);
  168.     return handler;
  169. }
  170.  
  171. static void bt125_return_i2c_dev(unsigned minor, unsigned major){
  172.     struct bt125_i2c_handler * handler;
  173.    
  174.     //printk(KERN_INFO "%s %s Destroying handler %d, %d \n",MODNAME,__func__,minor,major);
  175.     handler=bt125_i2c_dev_get_by_minor(minor,major);
  176.     if (!handler){
  177.         printk(KERN_ERR "%s %s Cannot find i2c handler! \n",MODNAME,__func__);
  178.         return;
  179.     }
  180.    
  181.  
  182.    
  183.     spin_lock(&bt125_i2c_list_lock);
  184.     list_del(&handler->list);
  185.     spin_unlock(&bt125_i2c_list_lock);
  186.     kfree(handler);
  187. }
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201. //////////////////
  202.  
  203. struct bt125{
  204.     struct i2c_client  * client;
  205.    
  206.     dev_t dev_num;
  207.     // ukazatel na strukturu cdev reprezentujici znakove zarizeni
  208.     struct cdev  cdev;
  209.      
  210.     // ukazatel na tridu zarizeni v adresari /sys/class
  211.     struct class *  sys_class_ptr;
  212.     struct device *   sys_dev_ptr;
  213.     const struct bt125_k *ops;
  214.     wait_queue_head_t wait_queue;
  215.    
  216.     //asi by melo byt chraneno mutexem protoze muzu pristupovat z vice vlakem, otazka je zda mi to vadi
  217.     int data_cookie;
  218. };
  219.  
  220.  
  221. int bt125_i2c_read_value(struct i2c_client *client, u8 reg){
  222.                return i2c_smbus_read_byte_data(client, reg);
  223. }
  224.  
  225. int bt125_i2c_write_value(struct i2c_client *client, u8 reg, u16 value){
  226.     return i2c_smbus_write_byte_data(client,reg,value);
  227. }
  228.  
  229.  
  230. int bt125_read_value(struct i2c_client *client, const struct bt125_k * modul){
  231.     int value=0;
  232.    
  233.     switch(modul->data_access){
  234.         case bt125_16B:
  235.             value=bt125_i2c_read_value(client, modul->i2c_addr);
  236.             value=value<<8;
  237.             value|=bt125_i2c_read_value(client, modul->i2c_addr+1);
  238.         break;
  239.         case bt125_32B:
  240.             value=bt125_i2c_read_value(client, modul->i2c_addr);
  241.             value=value<<8;
  242.             value|=bt125_i2c_read_value(client, modul->i2c_addr+1);
  243.             value=value<<8;
  244.             value|=bt125_i2c_read_value(client, modul->i2c_addr+2);
  245.             value=value<<8;
  246.             value|=bt125_i2c_read_value(client, modul->i2c_addr+3);
  247.         break;
  248.         case bt125_BIT:
  249.         case bt125_8B:
  250.         default:
  251.             value=bt125_i2c_read_value(client, modul->i2c_addr);
  252.         break;
  253.         }
  254.        
  255.     //printk(KERN_INFO "%s %d  %d  mod: %d \n",modul->name,modul->i2c_addr,bt125_i2c_read_value(client,modul->i2c_addr),modul->data_access);   
  256.     return value;
  257.    
  258. }
  259.  
  260. int bt125_write_value(struct i2c_client *client,const struct bt125_k * modul, int value){
  261.     int ret;
  262.     switch(modul->data_access){
  263.         case bt125_16B:
  264.             ret=bt125_i2c_write_value(client,modul->i2c_addr,value&0xff);
  265.             value=value>>8;
  266.             ret|=bt125_i2c_write_value(client,modul->i2c_addr+1,value&0xff);
  267.         break;
  268.         case bt125_32B:
  269.             ret=bt125_i2c_write_value(client,modul->i2c_addr,value&0xff);
  270.             value=value>>8;
  271.             ret|=bt125_i2c_write_value(client,modul->i2c_addr+1,value&0xff);
  272.             value=value>>8;
  273.             ret|=bt125_i2c_write_value(client,modul->i2c_addr+2,value&0xff);
  274.             value=value>>8;
  275.             ret|=bt125_i2c_write_value(client,modul->i2c_addr+3,value&0xff);
  276.         break;
  277.         case bt125_BIT:
  278.         case bt125_8B:
  279.         default:
  280.             return bt125_i2c_write_value(client,modul->i2c_addr,value);
  281.         break;
  282.         }
  283.        
  284.         return ret;
  285.  
  286. }
  287.  
  288.  
  289. /////////////////////
  290.  
  291. int bt125_read_module(const struct bt125_k * modul,char * buffer,int max_buffer,struct i2c_client *client){
  292.         int value;
  293.         int i;
  294.        
  295.         memset(buffer,0,max_buffer);
  296.         value=bt125_read_value(client,modul);
  297.        
  298.         //printk(KERN_INFO "%s %s Reading module %s value:  %d \n",MODNAME,__func__,modul->name,value);
  299.         //printk(KERN_INFO "%s %s %d  %d \n",MODNAME,__func__,modul->i2c_addr,bt125_i2c_read_value(client,modul->i2c_addr));
  300.        
  301.         switch(modul->data_access){
  302.             case bt125_BIT:
  303.                 sprintf(buffer, "%s", modul->name);
  304.                 for(i=0;i<8;i++){
  305.                     if((modul->i2c_mask&(0x01<<i))==0)
  306.                         continue;
  307.                     if(strlen(buffer)+20>max_buffer){
  308.                         printk(KERN_ERR "%s %s Not enought buffer size!   \n",MODNAME,__func__);
  309.                         return -ENOMEM;
  310.                     }
  311.                     sprintf(buffer+strlen(buffer), "%d,", value>>i&(0x01));
  312.                 }
  313.                 strcat(buffer,"\n");
  314.                
  315.                 break;
  316.         case bt125_16B:
  317.         case bt125_32B:
  318.         default://default je 8B
  319.                 if(max_buffer<20)
  320.                     return -ENOMEM;
  321.                 value&=modul->i2c_mask;
  322.                 sprintf(buffer, "%s%d\n", modul->name,value);
  323.                 break;
  324.         };
  325.  
  326. return 0;  
  327. }
  328.  
  329.  
  330.  
  331.  
  332. int bt125_write_module(const struct bt125_k * modul,char * msg,struct i2c_client * client){
  333.     int value;
  334.     int i;
  335.     int count;
  336.     int msg_len;
  337.     char *pch;
  338.    
  339.     value=bt125_read_value(client,modul);
  340.  
  341.  
  342.     pch = strstr (msg,"\n");
  343.     if(pch==NULL)
  344.             msg_len=strlen(msg);
  345.     else
  346.             msg_len=pch-msg;
  347.            
  348.     switch(modul->data_access){
  349.             case bt125_BIT:
  350.                 count=0;
  351.                 for(i=0;i<8;i++){
  352.                         if((modul->i2c_mask&(0x01<<i))==0)
  353.                             continue;
  354.                         if((count*2)>=msg_len)  {//mimo buffer
  355.                                 printk(KERN_ERR "%s %s Outside buffer!  %s shift %d   \n",MODNAME,__func__,modul->name,count);
  356.                                 return -EINVAL;
  357.                         }
  358.                        
  359.                         switch(msg[count*2]){
  360.                             case '0':
  361.                                 value&=~(0x01<<i);
  362.                                 break;
  363.                             case '1':
  364.                                 value|=(0x01<<i);
  365.                                 break;
  366.                             case 'x':
  367.                             case 'X':
  368.                                 break;
  369.                             default:
  370.                                 printk(KERN_ERR "%s %s Neocekavany znak %c  na pozici %d v stringu %s  \n",MODNAME,__func__,msg[count*2],(count*2),msg);
  371.                                 return -EINVAL;
  372.                                 break;
  373.                         };
  374.                         count++;
  375.                 }
  376.                
  377.                 if(count>1 && ((count*2)-1)!=msg_len)
  378.                     return -EINVAL;
  379.                 if(count==1 && count!=msg_len)
  380.                     return -EINVAL;
  381.                
  382.                 break;
  383.         default://default je INT
  384.                     if(sscanf(msg,"%d",&count)!=1)
  385.                         return -EINVAL;
  386.                     value=count;
  387.                 break;
  388.         };
  389.  
  390.     return bt125_write_value(client,modul,value);
  391. }
  392.  
  393.  
  394.  
  395.  
  396.  
  397. ///DEVICE OPS
  398.  
  399.  
  400. struct BT125_FILE{
  401.     int data_cookie;
  402.     struct bt125 * pbt125;
  403. };
  404.  
  405.  
  406.  
  407.  
  408. ssize_t bt125_dev_read(struct file * filp, char __user * buff, size_t count, loff_t * ppos){
  409.     const int txt_buff_len=50;
  410.     long result;
  411.     int ret;
  412.     int i;
  413.     char *txt;
  414.     char txt_buff[txt_buff_len];
  415.     int txt_len=150;
  416.     struct BT125_FILE *  file_info;
  417.     struct bt125 * handler;
  418.    
  419.     //printk("Ctu %d %llu \n",count,*ppos);
  420.    
  421.    
  422.     file_info=filp->private_data;
  423.     if(file_info==NULL){
  424.         return -ENXIO;
  425.     }
  426.    
  427.     handler=file_info->pbt125;
  428.    
  429.     if(handler==NULL){
  430.         return -ENXIO;
  431.     }
  432.    
  433.    
  434.     if(file_info->data_cookie!=handler->data_cookie){
  435.         file_info->data_cookie=handler->data_cookie;
  436.         *ppos=0;
  437.     }
  438.  
  439.     if((*ppos)>=txt_len){//mynule jsem nakopiroval vsechno
  440.         //printk("All data has been sended\n");
  441.         return 0;
  442.     }
  443.    
  444.    
  445.     txt = kzalloc(txt_len, GFP_KERNEL);
  446.     if (!txt)
  447.         return -ENOMEM;
  448.    
  449.    
  450.     for(i=0;handler->ops[i].name!=NULL;i++){
  451.         if((handler->ops[i].access&bt125_READ)==0)
  452.             continue;
  453.        
  454.  
  455.         //dev_err(&client->dev,"Reading %s\n",bt125_keys[i].name);
  456.        
  457.         ret=bt125_read_module(&handler->ops[i],txt_buff,txt_buff_len,handler->client);
  458.         if(ret!=0){
  459.             printk(KERN_ERR "%s %s Error reading module %s    \n",MODNAME,__func__,handler->ops[i].name);
  460.             goto End;
  461.         }
  462.         if(strlen(txt)+strlen(txt_buff)>=txt_len){
  463.             ret =-ENOMEM;
  464.             printk(KERN_ERR "%s %s Error out of memory    \n",MODNAME,__func__);
  465.             goto End;
  466.         }
  467.        
  468.         //printk(KERN_ERR "%s %s Message is %s   %s  \n",MODNAME,__func__,handler->ops[i].name,txt_buff);
  469.         strcat(txt,txt_buff);//*/
  470.     }
  471.    
  472.    
  473.    
  474.     if(*ppos>=strlen(txt)){//mynule jsem nakopiroval vsechno
  475.         //printk("All data has been sended\n");
  476.         ret=0;
  477.         goto End;
  478.     }
  479.    
  480.        
  481.      i=strlen(txt)-(*ppos);
  482.      if(i>=count)
  483.         i=count;
  484.        
  485.     //SANITY CHACK:
  486.     if((*ppos)+i>=txt_len){
  487.         printk(KERN_ERR "Out of memory! %llu + %u >= %u\n",*ppos,i,txt_len);
  488.         return -ENOMEM;
  489.     }
  490.    
  491.    
  492.     result=copy_to_user(buff,txt+(*ppos),i);
  493.    
  494.     if(result==0){
  495.         ret=i;
  496.         *ppos+=i;
  497.     }else
  498.         ret=-EFAULT;
  499.    
  500.    
  501.     //printk("Sending %u, cookie: %u\n",ret,file_info->data_cookie);
  502. End:
  503.    
  504.     kfree(txt);
  505.     return ret;
  506. }
  507.  
  508.  
  509.  
  510. ssize_t bt125_dev_write (struct file *filp, const char __user *buff, size_t count, loff_t *ppos){
  511.     int i;
  512.     int result;
  513.     char *txt;
  514.     char *pch;
  515.     int parsed_ok=0;
  516.     struct BT125_FILE *  file_info;
  517.     struct bt125 * handler;
  518.    
  519.     file_info=filp->private_data;
  520.    
  521.     if(file_info==NULL){
  522.         return -ENXIO;
  523.     }
  524.    
  525.     handler=file_info->pbt125;
  526.    
  527.     if(handler==NULL){
  528.         return -ENXIO;
  529.     }
  530.    
  531.     txt = kzalloc(count, GFP_KERNEL);
  532.     if (!txt)
  533.         return -ENOMEM;
  534.    
  535.    
  536.     result=copy_from_user(txt,buff,count);
  537.     if(result!=0)
  538.         goto End;
  539.    
  540.    
  541.     for(i=0;handler->ops[i].name!=NULL;i++){
  542.         pch = strstr (txt,handler->ops[i].name);
  543.         if(pch==NULL)
  544.             continue;
  545.            
  546.         if((handler->ops[i].access&bt125_WRITE)==0){
  547.             printk(KERN_ERR "%s %s Modul nema opraveni pro zapis %s   \n",MODNAME,__func__,handler->ops[i].name);
  548.             result=-EACCES;
  549.             goto End;
  550.         }
  551.            
  552.         if(bt125_write_module(&handler->ops[i],pch+strlen(handler->ops[i].name),handler->client)!=0){
  553.             printk(KERN_ERR "%s %s Nepodarilo se zapsat do modulu %s   \n",MODNAME,__func__,handler->ops[i].name);
  554.             result=-EINVAL;
  555.             goto End;
  556.         }
  557.         parsed_ok++;
  558.     }
  559.    
  560.     if(parsed_ok!=0)
  561.         result=count;
  562.     else
  563.         result=-EINVAL;
  564.    
  565. End:   
  566.      kfree(txt);
  567.      return result;
  568. }
  569.  
  570. unsigned int bt125_poll (struct file * filp, poll_table * wait){
  571.     struct BT125_FILE * file_info=filp->private_data;
  572.     unsigned int mask = 0;
  573.    
  574.     if(file_info==NULL){
  575.         printk(KERN_ERR "%s %s Handler is NULL! \n",MODNAME,__func__);
  576.         return mask;
  577.     }
  578.    
  579.     //printk("%s\tGoing to sleep\n",__func__);
  580.     poll_wait(filp,&file_info->pbt125->wait_queue,wait);
  581.    
  582.     if(file_info->data_cookie != file_info->pbt125->data_cookie){
  583.         mask |= POLLIN | POLLRDNORM;
  584.         printk("%s\tAwakening! cookie:%u x %u\n",__func__,file_info->data_cookie,file_info->pbt125->data_cookie);
  585.     }
  586.    
  587.     return mask;
  588. }
  589.  
  590. static int bt125_dev_open(struct inode * inode_ptr, struct file * file_ptr){
  591.     unsigned int minor;
  592.     unsigned int major;
  593.     int result = 0;
  594.     struct bt125_i2c_handler * i2c_handler;
  595.     struct BT125_FILE * file_info;
  596.  
  597.  
  598.  
  599.     minor= iminor(inode_ptr);
  600.     major= imajor(inode_ptr);
  601.     //printk(KERN_INFO "%s %s Openening file minor %d, %d\n",MODNAME,__func__,minor,major);
  602.     i2c_handler=bt125_i2c_dev_get_by_minor(minor,major);
  603.     if(!i2c_handler){
  604.         printk(KERN_ERR "%s %s Cannot find i2c handler! \n",MODNAME,__func__);
  605.         return -ENODEV;
  606.     }
  607.    
  608.     file_info= kzalloc(sizeof(struct BT125_FILE), GFP_KERNEL);
  609.    
  610.     if(file_info==NULL){
  611.         printk(KERN_ERR "%s %s Cannot get memory! \n",MODNAME,__func__);
  612.         return -ENOMEM;
  613.     }
  614.  
  615.  
  616.     //result = nonseekable_open(inode_ptr, file_ptr);
  617.     result=generic_file_open(inode_ptr, file_ptr);
  618.     if ( result < 0 ){
  619.         goto End;
  620.     }
  621.  
  622.     file_info->pbt125=i2c_get_clientdata(i2c_handler->client);
  623.     file_ptr->private_data = file_info;
  624.     //printk(KERN_INFO "%s %s Open file - return: %d \n",MODNAME,__func__,result);
  625.     return result;
  626. End:
  627.     kfree(file_info);
  628.     return -ENODEV;
  629. }
  630.  
  631.  
  632. static int bt125_dev_release(struct inode * inode_ptr, struct file * file_ptr){
  633.     struct BT125_FILE * file_info;
  634.     file_info=file_ptr->private_data;
  635.     if(file_info!=NULL)
  636.         kfree(file_info);
  637.     else
  638.         printk(KERN_ERR "%s %s Handler is NULL \n",MODNAME,__func__);
  639.        
  640.     file_ptr->private_data=NULL;
  641.     return 0;
  642. }
  643.  
  644.  
  645.  
  646. //pool:http://stackoverflow.com/questions/20646200/how-to-implement-poll-in-linux-driver-that-notices-fd-closed
  647. //pool http://www.makelinux.net/ldd3/chp-6-sect-3
  648. //pool: !! http://www.makelinux.net/ldd3/chp-6-sect-2
  649. //KOD google: "wait_queue_driver.c" gauss.ececs.uc.edu
  650. //http://www.makelinux.net/ldd3/chp-3-sect-3
  651. //http://www.cs.usfca.edu/~cruse/cs686s08/
  652.  
  653. static struct file_operations bt125_dev_file_ops ={
  654.      .owner = THIS_MODULE,
  655.      .open = &bt125_dev_open, //&dskel_open,
  656.      .release = &bt125_dev_release, //&dskel_release,
  657.      .read = &bt125_dev_read, //&dskel_read,
  658.      .write = &bt125_dev_write, //&dskel_write,
  659.      .poll = &bt125_poll
  660.      // modul nepodporuje metodu llseek
  661.      //.llseek = &no_llseek,
  662. };
  663.  
  664.  
  665.  
  666.  
  667.  
  668. #ifdef CONFIG_OF
  669.  
  670. static const struct of_device_id bt125_of_match[] = {
  671.     {
  672.         .compatible = "bustec,bt125",
  673.         .data = (void *) 0,
  674.     },
  675.     {
  676.         .compatible = "bustec,bt125_exp",
  677.         .data = (void *) 0,
  678.     },
  679.     {
  680.         .compatible = "bustec,bt125_adp5589",
  681.         .data = (void *) 0,
  682.     },
  683.     { },
  684. };
  685. MODULE_DEVICE_TABLE(of, bt125_of_match);
  686.  
  687. #endif /* CONFIG_OF */
  688.  
  689. static const struct i2c_device_id bt125_ids[] = {
  690.          { "bt125", 0, },
  691.          { "bt125_exp", 0, },
  692.          { "bt125_adp5589", 0, },
  693.          { /* LIST END */ }
  694.  };
  695. MODULE_DEVICE_TABLE(i2c, bt125_ids);
  696.  
  697.  
  698.  
  699. struct BT_MODULE_NAME{
  700.     char * of_name;
  701.     char * name;
  702.     };
  703.  
  704. static const struct  BT_MODULE_NAME BT_MODULES_NAME[]={
  705.     {"none","none"},
  706.     {"bustec,bt125","bt125"},
  707.     {"bustec,bt125_exp","bt125_exp"},
  708.     {"bustec,bt125_adp5589","bt125_adp5589"},
  709.     { }
  710. };
  711.  
  712.  
  713. enum BT_MODULES{
  714.     BT125_NONE,
  715.     BT125_CPU,
  716.     BT125_EXP,
  717.     BT125_ADP5589
  718. };
  719.  
  720.  
  721. static int bt125_Init_exp(struct i2c_client *client){
  722.     int ret=0;
  723.     ret|=bt125_i2c_write_value(client,0x00,0xff);//direction A
  724.     ret|=bt125_i2c_write_value(client,0x00,0x00);//direction B
  725.     ret|=bt125_i2c_write_value(client,0x0d,0xff);//pull UP B
  726.     return ret;
  727. }
  728.  
  729.  
  730. static int bt125_Init_ADP5889(struct i2c_client *client,unsigned char led_init){
  731.     int ret=0;
  732.     ret|=bt125_i2c_write_value(client,0x30,0xff);//GPIO - output
  733.     ret|=bt125_i2c_write_value(client,BT125_ADP_OI_REG,led_init);//direction B
  734.     return ret;
  735. }
  736.  
  737.  
  738.  
  739.  
  740. static irqreturn_t bt125_irq_handler(int irq, void *dev){
  741.     struct bt125 *handler;
  742.     handler=dev;
  743.     if(dev==NULL)
  744.         return IRQ_NONE;
  745.    
  746.     handler->data_cookie++;
  747.     wake_up_interruptible(&handler->wait_queue);
  748.    
  749.     printk(KERN_INFO "%s %s Interrupt!!!! .\n",MODNAME,__func__);
  750.     return IRQ_NONE;
  751.     //return IRQ_HANDLED;
  752. }
  753.  
  754.  
  755. static int bt125_probe(struct i2c_client *client,const struct i2c_device_id *id){
  756.     struct device_node *node ;
  757.     struct bt125 *data;
  758.     int modul;
  759.     int result=0;
  760.     const void * ptr;
  761.     unsigned int irq;
  762.     int value;
  763.     modul=BT125_NONE;
  764.    
  765.     printk(KERN_INFO "%s Probing ...  \n",MODNAME);
  766.    
  767.     node= client->dev.of_node;
  768.    
  769.    
  770.    
  771.     if(of_device_is_compatible(node, BT_MODULES_NAME[BT125_EXP].of_name)>0){
  772.                 printk(KERN_INFO "%s %s Compatible with bt125_exp  .\n",MODNAME,__func__);
  773.                 modul=BT125_EXP;
  774.                 if(bt125_Init_exp(client)){
  775.                     printk(KERN_ERR "%s %s Cannot inicialize I2C expander device!.\n",MODNAME,__func__);
  776.                     result=-EIO;
  777.                     goto End;
  778.                 }
  779.     }else if(of_device_is_compatible(node, BT_MODULES_NAME[BT125_CPU].of_name)>0){
  780.        
  781.    
  782.         printk(KERN_INFO "%s %s Compatible with bt125  .\n",MODNAME,__func__);
  783.         modul=BT125_CPU;
  784.         value=bt125_i2c_read_value(client,BT125_FW_REG);
  785.         printk(KERN_INFO "%s Version: %X \n",MODNAME,value);
  786.         if(value<0x02){
  787.             printk(KERN_ERR "%s %sSupport version 0x02 but get %i \n",MODNAME,__func__,value);
  788.             result= -ENODEV;
  789.             goto End;
  790.         }
  791.        
  792.        
  793.         bt125_i2c_write_value(client,BT125_PWR_CTRL_REG,0xff);//direction A
  794.        
  795.    
  796.     }else if(of_device_is_compatible(node, BT_MODULES_NAME[BT125_ADP5589].of_name)>0){
  797.         printk(KERN_INFO "%s %s Compatible with bt125 adp5589  .\n",MODNAME,__func__);
  798.         modul=BT125_ADP5589;
  799.        
  800.        
  801.         ptr=of_get_property(node,"led_init",NULL);
  802.         if(ptr!=NULL){
  803.             value=be32_to_cpup(ptr);
  804.             printk(KERN_INFO "%s %s VALUE IS 0x%x .\n",MODNAME,__func__,value);
  805.             bt125_Init_ADP5889(client,0xaf);
  806.             //bt125_Init_ADP5889(client,0x9f);
  807.         }else{
  808.             bt125_Init_ADP5889(client,0xff);
  809.         }
  810.        
  811.     }else{
  812.         printk(KERN_ERR "%s %sUnknown compatible string...\n",MODNAME,__func__);
  813.         result= -ENODEV;
  814.         goto End;
  815.     }
  816.    
  817.    
  818.     data = devm_kzalloc(&client->dev, sizeof(struct bt125), GFP_KERNEL);
  819.     if (!data){
  820.         result= -ENOMEM;
  821.         printk(KERN_INFO "%s %s Not enought memory.\n",MODNAME,__func__);
  822.         goto End;
  823.     }
  824.  
  825.     init_waitqueue_head(&data->wait_queue);
  826.    
  827.     if(modul==BT125_EXP){
  828.         data->ops=bt125_keys_exp;
  829.     }else if(modul==BT125_CPU){
  830.         data->ops=bt125_keys_cpu;
  831.     }else if(modul==BT125_ADP5589){
  832.         data->ops=bt125_adp5589;
  833.     }else{
  834.         printk(KERN_INFO "%s %s Modul not exist %d .\n",MODNAME,__func__,modul);
  835.         goto End;
  836.     }
  837.    
  838.     data->client=client;
  839.     i2c_set_clientdata(client, data);
  840.  
  841.     cdev_init(&data->cdev,&bt125_dev_file_ops);
  842.    
  843.    
  844.  
  845.     //alokuji minor major pary
  846.     result = alloc_chrdev_region(&data->dev_num, MINOR(data->dev_num),DEV_COUNT, BT_MODULES_NAME[modul].name);
  847.     if ( result < 0 ){
  848.         // jeste nic se nealokovalo, takze neni co vracet
  849.         printk(KERN_ERR "%s %s Cannot allocate dev pair.\n",MODNAME,__func__);
  850.         goto End;
  851.     }
  852.        
  853.    
  854.     // vytvorime si tridu v /sys/class
  855.     data->sys_class_ptr = class_create(THIS_MODULE, BT_MODULES_NAME[modul].name);
  856.     if ( IS_ERR(data->sys_class_ptr) ){
  857.         printk(KERN_ERR "%s %s Cannot create clas in /sys/class.\n",MODNAME,__func__);
  858.         result = PTR_ERR(data->sys_class_ptr);
  859.         goto Error_cdev;
  860.     }
  861.  
  862.  
  863.   // pridame nas modul do /sys/class/bt125_x
  864.     data->sys_dev_ptr = device_create(data->sys_class_ptr,&client->dev, data->dev_num, NULL, BT_MODULES_NAME[modul].name);
  865.     if ( IS_ERR(data->sys_dev_ptr) ){
  866.             printk(KERN_ERR "%s %s Module cannot be probe to system class.\n",MODNAME,__func__);
  867.             result = PTR_ERR(data->sys_dev_ptr);
  868.             goto Error_SysDev;
  869.     }
  870.    
  871.     if (IS_ERR(bt125_get_free_i2c_dev(client,MINOR(data->dev_num),MAJOR(data->dev_num)))){
  872.         printk(KERN_ERR "%s %s Cannot  add I2c client to the list.\n",MODNAME,__func__);
  873.         result=-ENOMEM;
  874.         goto Error_SysDev;
  875.     }
  876.    
  877.        
  878.     //vlozeni zarizeni do systemu
  879.     result =cdev_add(&data->cdev, data->dev_num, DEV_COUNT);
  880.     if ( result < 0 ){
  881.         printk(KERN_ERR "%s %s Cannot add device to system.\n",MODNAME,__func__);
  882.         goto Error_SysDev;
  883.     }
  884.  
  885.    
  886.     //Registr GPIO-IRQ
  887.     irq=irq_of_parse_and_map(node, 0);
  888.     if (irq != 0 ) {
  889.         //printk("%s %s IRQ is valid:%d\n", MODNAME,__func__,irq);
  890.         //result=devm_request_irq(&client->dev,irq,bt125_irq_handler,IRQF_TRIGGER_FALLING|IRQF_SHARED,"bt125_IRQ",data);
  891.         result=devm_request_irq(&client->dev,irq,bt125_irq_handler,IRQF_TRIGGER_FALLING|IRQF_SHARED,"bt125_IRQ",data);
  892.         if(result){
  893.             printk("%s %s Failed registry IRQ:%d,Err:%d, device: %d\n", MODNAME,__func__,irq,result,client->dev.id);
  894.             irq=0;
  895.         }else{
  896.             printk(KERN_INFO "%s %s Irq was activated %d\n",MODNAME,__func__,irq);
  897.         }
  898.     }else{
  899.         printk(KERN_INFO "%s %s Irq is not activated \n", MODNAME,__func__);
  900.     }
  901.      
  902.  
  903.   return 0;
  904.  
  905. Error_SysDev:
  906.     class_destroy(data->sys_class_ptr);
  907. Error_cdev:
  908.     unregister_chrdev_region(data->dev_num, DEV_COUNT);
  909. End:
  910.     return result;
  911.     }
  912.  
  913.  
  914.  
  915. static int bt125_remove(struct i2c_client *client){
  916.     struct bt125 *data;
  917.    
  918.     printk(KERN_ERR "%s:Stoper\n", __func__);
  919.     data = i2c_get_clientdata(client);
  920.    
  921.     cdev_del(&data->cdev);
  922.     bt125_return_i2c_dev(MINOR(data->dev_num),MAJOR(data->dev_num));
  923.     device_destroy(data->sys_class_ptr, data->dev_num);
  924.     class_destroy(data->sys_class_ptr);
  925.     unregister_chrdev_region(data->dev_num, DEV_COUNT);
  926.     return 0;
  927. }
  928.  
  929.  
  930.  
  931. static struct i2c_driver bt125_driver = {
  932.          .driver = {
  933.                  .name   = "bt125",
  934.                  .owner = THIS_MODULE,
  935.                  //.pm     = &foo_pm_ops,  /* optional  - power managment*/
  936.                  .of_match_table = of_match_ptr(bt125_of_match),
  937.          },
  938.  
  939.          .id_table       = bt125_ids,
  940.          .probe          = bt125_probe,
  941.          .remove         = bt125_remove,
  942.          
  943.          /* if device autodetection is needed: */
  944.          //.class          = I2C_CLASS_SOMETHING,
  945.  
  946.  
  947.          //.shutdown       = foo_shutdown, /* optional */
  948.          //.command        = foo_command,  /* optional, deprecated */
  949. };
  950.  
  951.  
  952. module_i2c_driver(bt125_driver);
  953.  
  954.  
  955. MODULE_AUTHOR("jan opravil <jan.opravil@jopr.cz>");
  956. MODULE_LICENSE("GPL");
  957. MODULE_DESCRIPTION("Driver for communicanion with 125 ");
  958. MODULE_VERSION("0.0.7");
  959. MODULE_LICENSE("GPL");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement