何时通过I2C使用platform_driver?

时间:2011-11-21 13:41:28

标签: linux-kernel i2c

我正在寻找代码xxx设备是一个使用I2C进行通信的IC。不知何故,它是使用平台设备编写的。要与IC通信,它使用“microp_i2c_write()”(请参阅​​以下函数write_xxx_register())。我认为这个代码很难在不同的平台上维护。这是一个导航产品。

问题: 1.我相信我的问题是如何包含struct i2c_client * client?进入这种情况?那么这种方式更容易移植到不同的linux / andriod内核? 2.为什么&什么时候platform_driver首选?有什么特别的原因吗?

static struct platform_driver xxx_device_driver = {
.probe    = xxx_probe,
.remove   = __devexit_p(xxx_remove),
.driver   = {
    .name   = XXX_NAME,
    .owner  = THIS_MODULE,
}
};

static void write_xxx_register(uint8_t reg, uint8_t val)
{
uint8_t data[BURST_DATA_SIZE];

data[0] = reg;
data[1] = val;
microp_i2c_write(OJ_REGISTER_WRITE, data, 2);
 }

static int __devinit XXX_probe(struct platform_device *pdev)
{
struct XXX_platform_data *oj = pdev->dev.platform_data;
int err;
int i;

err = -ENOMEM;
my_oj = oj;

INIT_WORK(&oj->work, XXX_work_func);

XXX__wq = create_singlethread_workqueue("XXX__wq");
if (!XXX__wq) {
    err = -ENOMEM;
    goto fail;
}

oj->input_dev = input_allocate_device();
if (!oj->input_dev) {
    printk(KERN_ERR "Unable to allocate device for OJ\n");
    err = -ENOMEM;
    goto fail;
}

oj->input_dev->name = "XXX_-oj";
oj->input_dev->evbit[0] = BIT_MASK(EV_KEY) |  BIT_MASK(EV_ABS)| BIT_MASK(EV_SYN )|BIT_MASK(EV_REL) ;
input_set_capability(oj->input_dev, EV_KEY, BTN_MOUSE);
input_set_capability(oj->input_dev, EV_KEY, BTN_TOUCH);
oj->input_dev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
oj->input_dev->absbit[0] = BIT_MASK(ABS_X) | BIT_MASK(ABS_Y);
input_set_abs_params(oj->input_dev, ABS_X, 8, 4024-8 , 0, 0);
input_set_abs_params(oj->input_dev, ABS_Y, 41, 6604-41, 0, 0);
input_set_abs_params(oj->input_dev, ABS_PRESSURE, 0, 255, 0, 0);
input_set_abs_params(oj->input_dev, ABS_TOOL_WIDTH, 0, 15, 0, 0);
input_set_abs_params(oj->input_dev, ABS_HAT0X, 8, 4024-8, 0, 0);
input_set_abs_params(oj->input_dev, ABS_HAT0Y, 41, 6604-41, 0, 0);
input_set_abs_params(oj->input_dev, ABS_MT_POSITION_X, 8, 4024-8, 0, 0);
input_set_abs_params(oj->input_dev, ABS_MT_POSITION_Y, 41, 6604-41, 0, 0);
input_set_abs_params(oj->input_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
input_set_abs_params(oj->input_dev, ABS_MT_WIDTH_MAJOR, 0, 15, 0, 0);

set_bit(EV_SYN, oj->input_dev->evbit);
set_bit(EV_KEY, oj->input_dev->evbit);
set_bit(BTN_TOUCH, oj->input_dev->keybit);
set_bit(EV_ABS, oj->input_dev->evbit);

for(i = 0; i < ARRAY_SIZE(keymap); i++)
    set_bit(keymap[i], oj->input_dev->keybit);

err = input_register_device(oj->input_dev);
if (err) {
    printk(KERN_ERR "Unable to register %s input device\n", oj->input_dev->name);
    goto fail;
}

err = request_irq(my_oj->irq, XXX_irq_handler,
          IRQF_TRIGGER_NONE, XXX_NAME, oj);
if (err < 0) {
    err = -ENOMEM;
    printk(KERN_ERR "request_irq failed\n");
    goto fail;
}


normal_th = my_oj->normal_th;
xy_ratio = my_oj->xy_ratio;
interval = my_oj->interval;
polling_delay = my_oj->mdelay_time;
debugflag = my_oj->debugflag;
ap_code = my_oj->ap_code;

printk(KERN_INFO "driver loaded\n");
return 0;

}

1 个答案:

答案 0 :(得分:1)

如果您的设备连接到I2C总线,那么您应该注册i2c_driver结构而不是platform_driver结构。 grep for drivers / input / touchscreen /中的i2C_driver。