板级文件:
arch/arm/mach-s3c64xx/mach-mini6410.c 中修改的内容
/*add by zl*/
#include <linux/input/lm8333.h>
#include <linux/input/matrix_keypad.h>
static const unsigned int matrix_keypad_cols[] = {128,129,130,/*131,132*/};
static const unsigned int matrix_keypad_rows[] = {120,/*121,122,123,124,125,126,127*/};
static uint32_t keymap[] __initdata =
{
//KEY(row, col, keycode)
KEY(0,0, KEY_F1), KEY(0,1, KEY_1), KEY(0,2, KEY_4), /*KEY(0,3, KEY_7), KEY(0,4,KEY_PROG1),
KEY(1,0, KEY_F2), KEY(1,1, KEY_2), KEY(1,2, KEY_5), KEY(1,3, KEY_8),KEY(1,4,KEY_PROG2),
KEY(2,0, KEY_F3), KEY(2,1, KEY_3), KEY(2,2, KEY_6), KEY(2,3, KEY_9),KEY(2,4,KEY_PROG3),
KEY(3,0, KEY_F4),KEY(3,1, KEY_0),KEY(3,2,KEY_DOT),KEY(3,3,KEY_HELP),KEY(3,4,KEY_PROG4),
KEY(4,0, KEY_F5), KEY(4,1, KEY_DELETE), KEY(4,2, KEY_INSERT), KEY(4,3, KEY_SETUP), KEY(4,4, KEY_B),
KEY(5,0, KEY_F6), KEY(5,1, KEY_HOMEPAGE), KEY(5,2, KEY_ESC), KEY(5,3, KEY_LEFT), KEY(5,4, KEY_W),
KEY(6,0, KEY_F7), KEY(6,1, KEY_END), KEY(6,2, KEY_UP), KEY(6,3, KEY_ENTER), KEY(6,4, KEY_DOWN),
KEY(7,0, KEY_F8), KEY(7,1, KEY_PAGEUP), KEY(7,2, KEY_PAGEDOWN), KEY(7,3, KEY_RIGHT), KEY(7,4, KEY_POWER),*/
};
static struct matrix_keymap_data lm8333_keymap_data __initdata =
{
.keymap0 = &keymap[0],
.keymap1 = &keymap[1],
.keymap2 = &keymap[2],
.keymap_size = 3,//ARRAY_SIZE(keymap),
};
static struct lm8333_platform_data lm8333_pdata = {
.matrix_data = &lm8333_keymap_data,
.active_time = 0,
.debounce_time= 20,
};
/*
struct i2c_board_info {
char type[I2C_NAME_SIZE];
unsigned short flags;
unsigned short addr;
void *platform_data;
struct dev_archdata *archdata;
int irq;
};
*/
static struct i2c_board_info i2c_devs0[] __initdata = {
//{ I2C_BOARD_INFO("ov965x", 0x30),},
{ I2C_BOARD_INFO("lm8333", 0x50),
.platform_data = &lm8333_pdata,
/*其中可以加irq*/
},/*add by zl*/
};
static struct i2c_board_info i2c_devs1[] __initdata = {
/* Add your i2c device here */
};
if (ARRAY_SIZE(i2c_devs0)) {
i2c_register_board_info(0, i2c_devs0, ARRAY_SIZE(i2c_devs0));
/* 0表示该I2C设备挂在I2C-0上 ,注册I2C adapt时相应的id = 0,这个0 与 (lm8333.c中的)id_table中的0对应 */
}
if (ARRAY_SIZE(i2c_devs1)) {
i2c_register_board_info(1, i2c_devs1, ARRAY_SIZE(i2c_devs1));
}
LM8333设备驱动文件
lm8333.c
#include <linux/module.h>
#include <linux/irq.h>
#include <linux/i2c.h>
#include <linux/interrupt.h>
#include <linux/input/matrix_keypad.h>
#include <linux/input/lm8333.h>
#define LM8333_FIFO_READ 0x20
#define LM8333_DEBOUNCE 0x22
#define LM8333_READ_INT 0xD0
#define LM8333_ACTIVE 0xE4
#define LM8333_READ_ERROR 0xF0
#define LM8333_KEYPAD_IRQ (1 << 0)
#define LM8333_ERROR_IRQ (1 << 3)
#define LM8333_ERROR_KEYOVR 0x04
#define LM8333_ERROR_FIFOOVR 0x40
#define LM8333_FIFO_TRANSFER_SIZE 16
#define LM8333_ROW_SHIFT 4
#define LM8333_NUM_ROWS 8
struct lm8333 {
struct i2c_client *client;
struct input_dev *input;
unsigned short keycodes[LM8333_NUM_ROWS << LM8333_ROW_SHIFT];//128
};
/* The accessors try twice because the first access may be needed for wakeup */
int lm8333_read8(struct lm8333 *lm8333, u8 cmd)
{
int repeated = 0, ret;
do {
ret = i2c_smbus_read_byte_data(lm8333->client, cmd);
} while (ret < 0 && !repeated++);
return ret;
}
EXPORT_SYMBOL_GPL(lm8333_read8);/*导出lm8333_read8()给有GPL认证的模块使用*/
int lm8333_write8(struct lm8333 *lm8333, u8 cmd, u8 val)
{
int repeated = 0, ret;
do {
ret = i2c_smbus_write_byte_data(lm8333->client, cmd, val);
} while (ret < 0 && !repeated++);
return ret;
}
EXPORT_SYMBOL_GPL(lm8333_write8);
int lm8333_read_block(struct lm8333 *lm8333, u8 cmd, u8 len, u8 *buf)
{
int repeated = 0, ret;
do {
ret = i2c_smbus_read_i2c_block_data(lm8333->client, cmd, len, buf);
} while (ret < 0 && !repeated++);
return ret;
}
EXPORT_SYMBOL_GPL(lm8333_read_block);
static void lm8333_key_handler(struct lm8333 *lm8333)
{
u8 keys[LM8333_FIFO_TRANSFER_SIZE];//LM8333_FIFO_TRANSFER_SIZE=16
u8 code, pressed;
int i, ret;
ret = lm8333_read_block(lm8333, LM8333_FIFO_READ, LM8333_FIFO_TRANSFER_SIZE,keys);
if (ret != LM8333_FIFO_TRANSFER_SIZE) {
dev_err(&lm8333->client->dev, "Error %d while reading FIFO\n", ret);
return;
}
for (i = 0; keys[i] && i < LM8333_FIFO_TRANSFER_SIZE; i++) {
pressed = !!(keys[i] & 0x80);
code = keys[i] & 0x7f;
input_event(lm8333->input, EV_MSC, MSC_SCAN, code);
input_report_key(lm8333->input, lm8333->keycodes[code], pressed);
}
input_sync(lm8333->input);
}
static irqreturn_t lm8333_irq_thread(int irq, void *data)
{
struct lm8333 *lm8333 = data;
printk("come in IRQ");
u8 status = lm8333_read8(lm8333, LM8333_READ_INT);
if (!status)
return IRQ_NONE;
if (status & LM8333_ERROR_IRQ) {
u8 err = lm8333_read8(lm8333, LM8333_READ_ERROR);
if (err & (LM8333_ERROR_KEYOVR | LM8333_ERROR_FIFOOVR)) {
u8 dummy[LM8333_FIFO_TRANSFER_SIZE];
lm8333_read_block(lm8333, LM8333_FIFO_READ,
LM8333_FIFO_TRANSFER_SIZE, dummy);
}
dev_err(&lm8333->client->dev, "Got error %02x\n", err);
}
if (status & LM8333_KEYPAD_IRQ)
lm8333_key_handler(lm8333);
return IRQ_HANDLED;
}
static int __devinit lm8333_probe(struct i2c_client *client,
const struct i2c_device_id *id){
int k;
printk("\n\npreparing to register lm8333\n\n");
struct lm8333_platform_data *pdata = client->dev.platform_data; /*这个地方花了点时间检测,但是可以通过
printk("\n\n%d\n\n",pdata->debounce_time);
k = (int)pdata->matrix_data->keymap0; /*大问题 测试的时候用,这个地方花了半天的时间才解决,基础不牢固 */
printk("\n\nkeymap[0]=%d\n\n",k);
k = (int)pdata->matrix_data->keymap1; /*大问题*/
printk("\n\nkeymap[1]=%d\n\n",k);
k = (int)pdata->matrix_data->keymap2; /*大问题*/
printk("\n\nkeymap[2]=%d\n\n",k);
struct lm8333 *lm8333;
struct input_dev *input;
int err, active_time;
if (!pdata)
{
return -EINVAL;
}
active_time = pdata->active_time ?: 500;
if (active_time / 3 <= pdata->debounce_time / 3)
{
dev_err(&client->dev, "Active time not big enough!\n");
return -EINVAL;
}
lm8333 = devm_kzalloc(&client->dev, sizeof(*lm8333), GFP_KERNEL);
input = input_allocate_device();
if (!lm8333 || !input)
{
return -ENOMEM;
}
input->name = client->name;
input->dev.parent = &client->dev;
input->id.bustype = BUS_I2C;
lm8333->client = client;
lm8333->input = input;
i2c_set_clientdata(client, lm8333);
input->keycode = lm8333->keycodes;;//存储按键值的数组首地址
input->keycodesize = sizeof(lm8333->keycodes[0]);;//每个键值的字节数
input->keycodemax = ARRAY_SIZE(lm8333->keycodes);;//支持的按键值的个数
input->evbit[0] = BIT_MASK(EV_KEY);// set_bit(EV_KEY,input->evbit);这一条与前面的同意
input_set_capability(input, EV_MSC, MSC_SCAN);//很重要
set_bit(n,input->keybit);//注册n值可以上报给应用程序,非常重要,这一句卡了我很长时间,调试了很久
printk("\nv10\n");
matrix_keypad_build_keymap(pdata->matrix_data, LM8333_ROW_SHIFT,
input->keycode, input->keybit); //keybit记录支持的按键值的位图
printk("\nv11\n");
if (pdata->debounce_time) {
err = lm8333_write8(lm8333, LM8333_DEBOUNCE, pdata->debounce_time / 3);
if (err)
{
dev_warn(&client->dev, "Unable to set debounce time\n");
}
}
if (pdata->active_time)
{
err = lm8333_write8(lm8333, LM8333_ACTIVE, pdata->active_time / 3);
if (err)
{
dev_warn(&client->dev, "Unable to set active time\n");
}
}
/*err = devm_request_threaded_irq(&client->dev, client->irq, NULL,lm8333_irq_thread,
IRQF_TRIGGER_FALLING | IRQF_ONESHOT, "lm8333", lm8333);*/
err = devm_request_threaded_irq(&client->dev, 101, NULL,lm8333_irq_thread,
IRQF_TRIGGER_FALLING | IRQF_ONESHOT, "lm8333", lm8333);
if (err)
{
goto free_input;
}
printk("\n\n&&&&&&&&&&&&&&&&&\nrequire IRQ successful\n \n\n");
err = input_register_device(input);
if (err)
{
goto free_input;
}
printk("\n\nregister lm8333 successful \n\n");
return 0;
free_input:
input_free_device(input);
printk("\n\nregister lm8333 failed !\n\n");
return err;
}
static int __devexit lm8333_remove(struct i2c_client *client)
{
struct lm8333 *lm8333 = i2c_get_clientdata(client);
input_unregister_device(lm8333->input);
input_free_device(lm8333->input);
return 0;
}
static const struct i2c_device_id lm8333_id[] = {
{ "lm8333", 0 },
{ }
};
MODULE_DEVICE_TABLE(i2c, lm8333_id);
static struct i2c_driver lm8333_driver = {
.driver = {
.name = "lm8333",
.owner = THIS_MODULE,
},
.probe = lm8333_probe,
.remove = __devexit_p(lm8333_remove),
.id_table = lm8333_id,
};
static int __init lm8333_init(void)
{
printk("\n\n register lm8333 \n\n");
return i2c_add_driver(&lm8333_driver);
}
module_init(lm8333_init);
static void __exit lm8333_exit(void)
{
i2c_del_driver(&lm8333_driver);
}
module_exit(lm8333_exit);
MODULE_AUTHOR("Wolfram Sang <w.s...@pengutronix.de>");
MODULE_DESCRIPTION("LM8333 keyboard driver");
MODULE_LICENSE("GPL v2");
3、修改 键盘矩阵文件 matrix_keypad.h
修改预定义:struct matrix_keymap_data {
const uint32_t *keymap0;
const uint32_t *keymap1;
const uint32_t *keymap2;
unsigned int keymap_size;
};
修改函数 static inline void
matrix_keypad_build_keymap(const struct matrix_keymap_data *keymap_data,
unsigned int row_shift,
unsigned short *keymap, unsigned long *keybit)
可以与android linux源码对比
static inline void
matrix_keypad_build_keymap(const struct matrix_keymap_data *keymap_data,
unsigned int row_shift,
unsigned short *keymap, unsigned long *keybit)
{
int i;
//for (i = 0; i < keymap_data->keymap_size; i++) {
for (i = 0; i < 3; i++) {
unsigned int key;
switch (i)
{
case 0:
key = keymap_data->keymap0;break;
case 1:
key = keymap_data->keymap1;break;
case 2:
key = keymap_data->keymap2;break;
default :break;
}
unsigned int row = KEY_ROW(key);
unsigned int col = KEY_COL(key);
unsigned short code = KEY_VAL(key);
keymap[MATRIX_SCAN_CODE(row, col, row_shift)] = code;//定义键盘
__set_bit(code, keybit);
}
__clear_bit(KEY_RESERVED, keybit);
}