android 串口扩展(wk2xxx)移植


1.驱动移植
 将wk2xxx_i2c.c wk2xxx.h 放到lichee\linux-3.4\drivers\hwmon目录下
1.makefile
obj-$(CONFIG_SERIAL_I2C_WK2XXX) += wk2xxx_i2c.o
2. kconfig
 config SERIAL_I2C_WK2XXX
tristate "i2c to 4 uart for wk2xxx chips "
select I2C  
select SERIAL_CORE
depends on HWMON
default m
help
i2c to 4 uart for wk2xxx chips
说明:由于是串口扩展,所以需要选择 SERIAL_CORE,基于i2c,所以要select i2c


3.i2c irq中断配置
在sysconfig.fex中添加相关的io配置
;-------------------------------------------------------------------------------
;wk2 irq configuration
;-------------------------------------------------------------------------------
[wk2_para]
wk2_used            = 1
wk2_twi_id          = 1
wk2_twi_addr        = 0x10
wk2_freq            = 400000
wk2_irq_port        = port:PI16<6>




4.驱动代码的移植
4.1. init函数
在这个函数中首先要获取sysconfig中的相关的配置,同时将设备注册到相应的i2c总线上
int ret;
printk("wk2xxx_init-------------------------\n");
        int retval;
ret=wk2xxx_fetch_sysconfig_para();
if(ret < 0){
printk("wk2xxx_fetch_sysconfig_para failed in init \n");
}
// wk2xxx_i2c_driver.detect = wk2xxx_detect;
        struct i2c_board_info info;
      struct i2c_adapter *adapter;
      struct i2c_client *client;
 memset(&info, 0, sizeof(struct i2c_board_info));
 info.addr = 0x10;
  strlcpy(info.type, "wk2xxx_i2c", I2C_NAME_SIZE);
  adapter = i2c_get_adapter(1);

  if (!adapter) {
  printk("*******get_adapter error!********\n");
  }
  client = i2c_new_device(adapter, &info);
 wk2xxx_detect(client, &info);



/*   i2c_add_driver(&msg21xx_ts_driver);
  if (msg21xx_device_check == 0) {
  i2c_del_driver(&msg21xx_ts_driver);
  printk("msg21xx_device_check is 0 =========================\n");
  }
  return ret;
struct i2c_board_info bi = {I2C_BOARD_INFO("wk2xxx_i2c", 0x10),};
i2c_new_device(i2c_get_adapter( wk2xxx_i2c_pdata.bus_num), &bi);
*/    
 retval = i2c_add_driver(&wk2xxx_i2c_driver);
if (retval !=0)
{
printk("======wk2xxx_i2c_driver init fail, ret=0x%x======\n", retval);
i2c_del_driver(&wk2xxx_i2c_driver);
return ret;
}






static int wk2xxx_detect(struct i2c_client *client, struct i2c_board_info *info)
{
printk("wk2xxx_detect-----\n");
struct i2c_adapter *adapter = client->adapter;
int ret;


        if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
                return -ENODEV;


if(twi_id == adapter->nr){
                printk("%s: addr= %x\n",__func__,client->addr);
               ret = i2c_test(client);
                if(!ret){
        printk("%s:I2C connection might be something wrong \n",__func__);
        return -ENODEV;
       
}else{
strlcpy(info->type, "wk2xxx_i2c", I2C_NAME_SIZE);
return 0;
       }



}else{
return -ENODEV;
}
}




4.2 获取配置文件相关信息函数


static int wk2xxx_fetch_sysconfig_para(void){
printk("wk2xxx_fetch_sysconfig_para-------------------\n");
int ret =  -1;
int i = -1;
int device_used = -1;
script_item_u val;
script_item_value_type_e  type;
type = script_get_item("wk2_para", "wk2_used", &val);
 if(SCIRPT_ITEM_VALUE_TYPE_INT != type){
  printk("%s, type err device_used =%d \n", __func__, val.val);
goto script_get_err;
  }
device_used = val.val;
if(1 == device_used){
type = script_get_item("wk2_para", "wk2_twi_id", &val);
if(SCIRPT_ITEM_VALUE_TYPE_INT != type){
printk("%s: type err twi_id = %d. \n", __func__, val.val);
goto script_get_err;
  }
  wk2xxx_i2c_pdata.bus_num = val.val;
printk("wk2xxx_i2c_pdata.bus_num =%d\n",wk2xxx_i2c_pdata.bus_num);
  
  type = script_get_item("wk2_para", "wk2_twi_addr", &val);
if(SCIRPT_ITEM_VALUE_TYPE_INT != type){
printk("%s: type err twi_id = %d. \n", __func__, val.val);
goto script_get_err;
  }
  wk2xxx_i2c_pdata.slave_addr = val.val;   
  
  printk("wk2xxx_i2c_pdata.slave_addr =%d\n",wk2xxx_i2c_pdata.slave_addr);


  
  type = script_get_item("wk2_para", "wk2_freq", &val);   
  if(SCIRPT_ITEM_VALUE_TYPE_INT != type){
printk("%s: type err twi_id = %d. \n", __func__, val.val);
  goto script_get_err;
 }
 wk2xxx_i2c_pdata.frequency = val.val;
 printk("wk2 frequency=%d\n",wk2xxx_i2c_pdata.frequency);
 
type = script_get_item("wk2_para", "wk2_irq_port", &val);
if (type != SCIRPT_ITEM_VALUE_TYPE_PIO) {
printk("get uarts IO(uart_irq) failed\n");
goto script_get_err;
}
                      int_number= val.gpio.gpio;
printk("wk2 int_number=%d\n",int_number);
/* i =  i2c_register_board_info( wk2xxx_i2c_pdata.bus_num, wk2xxx_i2c_board_info,
 ARRAY_SIZE(wk2xxx_i2c_board_info));
if(i < 0){
printk("wk2 register i2c device failed \n");
goto script_get_err;
}*/
printk("wk2xxx_fetch_sysconfig_para--is ok----------\n");
  return 0;
  
  }else{


printk("wk2xxx get sysconfig failed \n");
ret = -1;
  }
script_get_err:
printk("get erro");
return ret;
 


}


4.3 构建board info
static struct wk2xxx_platform_data  wk2xxx_i2c_pdata = {
.flag  = 0,
};


4.4 中断函数的修改
由于a20对中断函数进行了重新的封装,所以要对应平台函数为
 if(sw_gpio_irq_request(s->port.irq, TRIG_EDGE_NEGATIVE , (peint_handle)wk2xxx_irq, s) <0)
同时使能和关闭中断函数也要分别进行修改
 //disable_irq_nosync(s->port.irq);
      --------》  sw_gpio_eint_set_enable(s->port.irq, 0);


  //enable_irq(s->port.irq);
 ===========》           sw_gpio_eint_set_enable(s->port.irq, 1);




/static irqreturn_t wk2xxx_irq(int irq, void *dev_id)//
----------------》static u32 wk2xxx_irq(struct wk2xxx_port  *dev_id){
return 0;
}


4.5 还有就是一些头文件的修改了


4.6 编译成ko 或者build-in都ok,如果是ko,在android init 的时候记得要insod






5.总结的关键点
1.a20的对中断函数进行了重新的封装,对应于代码中要注意这一系列函数的参数以及
返回值的使用
2.其他就是一些细节问题了
3.最开始调试的是uart to uart 但是datasheet对于寄存器的设置流程基本无描述。
所以这个是非常难的一个部分







你可能感兴趣的:(allwiner)