CP2528触摸按键驱动(Linux 轮询)

/*
 * Driver for keys on CP2528 I2C IO expander
 *
 * Copyright (C) 2016 Qualvision.cn Instruments
 *
 * Author : cong.he@qualvision.cn
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */


#include <linux/types.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/slab.h>
#include <linux/interrupt.h>
#include <linux/workqueue.h>
#include <linux/gpio.h>
#include <linux/i2c.h>
#include <linux/input.h>
#include <linux/cp2528.h>




// we should swap low 8-bit and high 8-bit in transe for cp2528 data format
#ifndef swab16(x)
#define swab16(x) ((x&0x00ff) << 8 | (x&0xff00) >> 8)
#endif




#define CP2528_KEY_ADDR       0x31
#define CP2528_WP_ADDR     0x60
#define CP2528_WP_VALUE   0x5500
#define CP2528_RESET_ADDR 0x01
#define CP2528_RESET_VALUE 0x8000
#define CP2528_SLEEP_ADDR 0x01
#define CP2528_SLEEP_VALUE 0xf800
#define     CP2528_KEY_ADDR       0x31
#define     CP2528_CHIP_ID        0x2528
#define     CP2528_BG_LED         0x02
#define     CP2528_TIM_LED         0x28
#define     CP2528_OUT_LED         0x20
#define     CP2528_EN_LED         0x21




#define INF(fmt...) \
{ printk(KERN_INFO "%s %d",__FUNCTION__,__LINE__);\
printk(KERN_INFO fmt);}


#define DBG(fmt...) \
{ printk(KERN_DEBUG "%s %d",__FUNCTION__,__LINE__);\
printk(KERN_DEBUG fmt);}


static const struct i2c_device_id cp2528_id[] = {
{ "cp2528-keys", 16, },
{ }
};
MODULE_DEVICE_TABLE(i2c, cp2528_id);






struct cp2528_drv_data {
struct input_dev *input;
struct cp2528_button data[0];
};


struct cp2528_keypad_chip {
uint16_t reg_output;
uint16_t reg_direction;
uint16_t reg_input;


struct i2c_client *client;
struct input_dev *input;
struct delayed_work dwork;
u16 pinmask;
int irqnum;
bool use_polling;
struct cp2528_button buttons[0];
};


static int cp2528_write_reg(struct cp2528_keypad_chip *chip, u16 reg, u32 val)
{
int error;


error = i2c_smbus_write_word_data(chip->client, reg , swab16(val));
if (error < 0) {
dev_err(&chip->client->dev,
"%s failed, reg: %d, val: %d, error: %d\n",
__func__, reg, val, error);
return error;
}


return 0;
}


static int cp2528_read_reg(struct cp2528_keypad_chip *chip, u16 reg, u16 *val)
{
int retval;


retval = i2c_smbus_read_word_data(chip->client,reg);
if (retval < 0) {
dev_err(&chip->client->dev, "%s failed, reg: %d, error: %d\n",
__func__, reg, retval);
return retval;
}


*val = (u16)swab16(retval);
return 0;
}


static int  cp2528_read_registers(struct cp2528_keypad_chip *chip)
{
u16 i,error,value;


INF(" ---------- start read the regs -------- \n");
for (i=0; i<0x22; i++)
{
error = cp2528_read_reg(chip,i,&value);
INF("OK read reg is %d  value is %4X\n",i,value);
}
INF(" ----------  end  read the regs -------- \n");


return 0;
}


static int  cp2528_read_wp_enable(struct cp2528_keypad_chip *chip)
{
int error;
u16 value;


error = cp2528_read_reg(chip,CP2528_WP_ADDR,&value);


if(value == CP2528_WP_VALUE){ //write enable


INF("OK read CP2528_WP_VALUE:: %d  value:%4X\n",error,value);
return 0;
}
else{


INF("ERR read CP2528_WP_VALUE:: %d value:%4X\n",error,value);
return 1;
}


}


static int  cp2528_setup_leds(struct cp2528_keypad_chip *chip)
{
int error;
u16 value;


error = cp2528_read_wp_enable(chip);


if(error)
{
INF("Error in write enable\n");
return 1;
}


value = 0x1f; // setup Led as common gpio
error = cp2528_write_reg(chip, CP2528_EN_LED, value);
INF("OK write CP2528_BG_LED: %d  value:%4X\n",error,value);
msleep(50);
error = cp2528_read_reg(chip,CP2528_EN_LED,&value);
INF("OK read CP2528_BG_LED: %d  value:%4X\n",error,value);


//value = value ?0:0xff;
value = 0x1f; // Lighten led
error = cp2528_write_reg(chip, CP2528_OUT_LED, value);
INF("OK write CP2528_OUT_LED: %d  value:%4X\n",error,value);
msleep(50);
error = cp2528_read_reg(chip,CP2528_OUT_LED,&value);
INF("OK read CP2528_OUT_LED: %d  value:%4X\n",error,value);


return 0;
}


static int  cp2528_reset_registers(struct cp2528_keypad_chip *chip)
{
int error;
u16 value;
int i=0;


cp2528_read_registers(chip); // read regs


msleep(500);


do // reset
{
error = cp2528_write_reg(chip, CP2528_WP_ADDR, CP2528_WP_VALUE);
INF("WRITE WP VALUE %d ",error);


msleep(50);
error = cp2528_write_reg(chip, CP2528_RESET_ADDR, CP2528_RESET_VALUE);
INF("WRITE RESET %d ",error);


msleep(200);
i++;
error = cp2528_read_reg(chip,CP2528_WP_ADDR,&value);
INF("READ WP error:%d value:%4X\n",error,value);


msleep(500);


} while( (value!=CP2528_WP_VALUE) && (i<8));


INF("reset times %d\n",i);
msleep(500);
cp2528_setup_leds(chip); //setup leds as gpio


return 0;
}


static int cp2528_led_on_key(struct cp2528_keypad_chip *chip, u16 value)
{
int error=0;
static u8 times,bak_times;


// something wrong , reset
if(value >= 0x1f)
{
INF("!-----------> Error in cp2528 keyboard %4X <-------\n",value);
INF("!-----------> Error in cp2528 keyboard <-------\n");


cp2528_write_reg(chip, CP2528_OUT_LED, 0x1f);


cp2528_reset_registers(chip);


return 0;
}


// light/die leds 2 times when value status are the same
if(value == 0)
{
times &= 0x03;


if(times == 0)
times = 1<<0;
else
times  |= 1<<1;
}
else
{
times &= 0x0c;


if(times == 0)
times = 1<<2;
else
times |= 1<<3;
}


value = 0x1f ^ value;


if(bak_times != times)
{
DBG("%2X",value);
error = cp2528_write_reg(chip, CP2528_OUT_LED, value);
bak_times = times;
}


return error;
}


static void cp2528_keys_scan(struct cp2528_keypad_chip *chip)
{
struct input_dev *input = chip->input;
u16 reg_val, val, led_val;
static u16 bak_reg, bak_led,led_count=0;
int error, i, pin_index;


led_count++;


error = cp2528_read_reg(chip, CP2528_KEY_ADDR, &reg_val);
if (error)
return;


if(reg_val > 0x1f) //invalid value error
{
INF("read value > 0x1f %4X,reset ...\n",reg_val);
cp2528_led_on_key(chip,0x3f);
return ;
}




if( (led_count%20) == 0) //cheeck led gpio func
{
error = cp2528_read_reg(chip,CP2528_EN_LED,&led_val);
if(error)
return;
if(led_val != 0x1f)
{
INF("Led error %4X,reset ...\n",led_val);
cp2528_led_on_key(chip,0x2f);
return ;
}
}


reg_val &= chip->pinmask;


if (bak_reg != reg_val)
{
DBG("!##########!read_value=%d!############!\n",reg_val);
bak_reg = reg_val;
}

if(reg_val)
DBG("per read reg_value=%d\n",reg_val);
/* Figure out which lines have changed */
val = reg_val ^ chip->reg_input;
chip->reg_input = reg_val;




for (i = 0, pin_index = 0; i < 16; i++) {


cp2528_led_on_key(chip,reg_val);


if (val & (1 << i)) {


struct cp2528_button *button = &chip->buttons[pin_index];
unsigned int type = button->type ?: EV_KEY;
int state = ((reg_val & (1 << i)) ? 1 : 0)
^ button->active_low;


DBG("value=%d\n",reg_val);
input_event(input, type, button->code, !!state);
input_sync(input);
}


if (chip->pinmask & (1 << i))
pin_index++;
}
}




static void cp2528_keys_work_func(struct work_struct *work)
{
struct cp2528_keypad_chip *chip =
container_of(work, struct cp2528_keypad_chip, dwork.work);


cp2528_keys_scan(chip);
schedule_delayed_work(&chip->dwork, msecs_to_jiffies(100));
}


static int cp2528_keys_open(struct input_dev *dev)
{
struct cp2528_keypad_chip *chip = input_get_drvdata(dev);


/* Get initial device state in case it has switches */
cp2528_keys_scan(chip);


if (chip->use_polling)
schedule_delayed_work(&chip->dwork, msecs_to_jiffies(100));


return 0;
}


static void cp2528_keys_close(struct input_dev *dev)
{
struct cp2528_keypad_chip *chip = input_get_drvdata(dev);


if (chip->use_polling)
cancel_delayed_work_sync(&chip->dwork);
}


static int __devinit cp2528_setup_registers(struct cp2528_keypad_chip *chip)
{
int error;
u16 value;
u8  low_8bit;
u8  high_8bit;
int i=0;


cp2528_read_reg(chip, 0, &value);


if(value == CP2528_CHIP_ID ){


INF("chip id ok\n");
}
else{


INF("chip id error \n");
return -1;
}


do // reset
{
error = cp2528_write_reg(chip, CP2528_WP_ADDR, CP2528_WP_VALUE);
INF("WRITE WP VALUE %d ",error);


msleep(50);
error = cp2528_write_reg(chip, CP2528_RESET_ADDR, CP2528_RESET_VALUE);
INF("WRITE RESET %d ",error);


msleep(800);
i++;
error = cp2528_read_reg(chip,CP2528_WP_ADDR,&value);
INF("READ WP error:%d value:%4X\n",error,value);


} while( (value!=CP2528_WP_VALUE) && (i<8));


cp2528_write_reg(chip, CP2528_WP_ADDR, CP2528_WP_VALUE);


low_8bit = 0x1f;
value = (low_8bit) | (high_8bit<<8);
msleep(100);
error = cp2528_write_reg(chip, CP2528_EN_LED, value);
DBG("OK write CP2528_BG_LED: %d  value:%2X\n",error,value);
error = cp2528_read_reg(chip,CP2528_EN_LED,&value);
DBG("OK read CP2528_BG_LED: %d  value:%2X\n",error,value);


//value = value ?0:0xff;
value = 0x1f;
error = cp2528_write_reg(chip, CP2528_OUT_LED, value);
DBG("OK write CP2528_OUT_LED: %d  value:%2X\n",error,value);
error = cp2528_read_reg(chip,CP2528_OUT_LED,&value);
DBG("OK read CP2528_OUT_LED: %d  value:%2X\n",error,value);
msleep(100);


chip->reg_input &= chip->pinmask;


return 0;
}


static int __devinit cp2528_keypad_probe(struct i2c_client *client,
  const struct i2c_device_id *id)
{
struct cp2528_keys_platform_data *pdata;
struct cp2528_keypad_chip *chip;
struct input_dev *input;
int error;
int i;


DBG("%d\n",__LINE__);
/* Check functionality */
if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE)) {
dev_err(&client->dev, "%s adapter not supported\n",
dev_driver_string(&client->adapter->dev));
return -ENODEV;
}
DBG("%d\n",__LINE__);


pdata = client->dev.platform_data;
if (!pdata) {
dev_dbg(&client->dev, "no platform data\n");
return -EINVAL;
}
DBG("%d\n",__LINE__);


chip = kzalloc(sizeof(struct cp2528_keypad_chip) +
      pdata->nbuttons * sizeof(struct cp2528_button),
      GFP_KERNEL);
input = input_allocate_device();
if (!chip || !input) {
error = -ENOMEM;
goto fail;
}
DBG("%d\n",__LINE__);


chip->client = client;
chip->input = input;
chip->pinmask = pdata->pinmask;
chip->use_polling = pdata->use_polling;
DBG("%d\n",__LINE__);


INIT_DELAYED_WORK(&chip->dwork, cp2528_keys_work_func);
DBG("%d\n",__LINE__);


input->phys = "cp2528-keys/input0";
input->name = client->name;
input->dev.parent = &client->dev;


input->open = cp2528_keys_open;
input->close = cp2528_keys_close;


input->id.bustype = BUS_HOST;
input->id.vendor = 0x0001;
input->id.product = 0x0001;
input->id.version = 0x0100;
DBG("%d\n",__LINE__);


/* Enable auto repeat feature of Linux input subsystem */
if (pdata->rep)
__set_bit(EV_REP, input->evbit);


for (i = 0; i < pdata->nbuttons; i++) {
unsigned int type;


chip->buttons[i] = pdata->buttons[i];
type = (pdata->buttons[i].type) ?: EV_KEY;
input_set_capability(input, type, pdata->buttons[i].code);
}
DBG("%d\n",__LINE__);


input_set_drvdata(input, chip);


/*
* Initialize cached registers from their original values.
* we can't share this chip with another i2c master.
*/
error = cp2528_setup_registers(chip);
if (error)
goto fail;
DBG("%d\n",__LINE__);


error = input_register_device(input);
if (error) {
dev_dbg(&client->dev,
"Unable to register input device, error: %d\n", error);
goto fail;
}
DBG("%d\n",__LINE__);


i2c_set_clientdata(client, chip);
DBG("%d\n",__LINE__);


return 0;


fail:
input_free_device(input);
kfree(chip);
return error;
}


static int __devexit cp2528_keypad_remove(struct i2c_client *client)
{
struct cp2528_keypad_chip *chip = i2c_get_clientdata(client);




input_unregister_device(chip->input);
kfree(chip);


i2c_set_clientdata(client, NULL);


return 0;
}




static struct i2c_driver cp2528_keypad_driver = {
.driver = {
.name = "cp2528-keys",
},
.probe = cp2528_keypad_probe,
.remove = __devexit_p(cp2528_keypad_remove),
.id_table = cp2528_id,
};


static int __init cp2528_keypad_init(void)
{
return i2c_add_driver(&cp2528_keypad_driver);
}


late_initcall(cp2528_keypad_init);


static void __exit cp2528_keypad_exit(void)
{
i2c_del_driver(&cp2528_keypad_driver);
}
module_exit(cp2528_keypad_exit);


MODULE_AUTHOR("hecong cong.he@qualvision.cn");
MODULE_DESCRIPTION("Keypad driver over cp2528 IO expander");
MODULE_LICENSE("GPL");
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值