Monday, January 13, 2014

Sample I2c Driver

 Sample I2c Client Driver
 
#include < linux/kernel.h >
#include < linux/module.h >
#include < linux/init.h >
#include < linux/i2c.h >
#include < linux/interrupt.h >

#include

struct sample_device {
    struct i2c_client *client;
    /* TODO */
};


/* TODO */

static int __devinit sample_i2c_probe(struct i2c_client *client,
        const struct i2c_device_id *id)
{
    struct sample_device *dev;
    int ret;

    if (!i2c_check_functionality(client->adapter,
        I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
        I2C_FUNC_SMBUS_I2C_BLOCK)) {
        printk(KERN_ERR "%s: needed i2c functionality is not supported\n", __func__);
        return -ENODEV;
    }
   
    dev = kzalloc(sizeof(struct sample_device), GFP_KERNEL);
    if (dev == NULL) {
        printk(KERN_ERR "%s: no memory\n", __func__);
        return -ENOMEM;
    }

    dev->client = client;
    i2c_set_clientdata(client, dev);

    // pdata = client->dev.platform_data;
   
    /* TODO: do something */

    return 0;
}

static int __devexit sample_i2c_remove(struct i2c_client *client)
{
    struct sample_client *dev = i2c_get_clientdata(client);

    /* TODO: do something */

    kfree(dev);
    return 0;
}

#ifdef CONFIG_PM
static int sample_i2c_suspend(struct i2c_client *client, pm_message_t msg)
{
    struct sample_device *dev = i2c_get_clientdata(client);
   
    return 0;
}

static int sample_i2c_resume(struct i2c_client *client)
{
    struct sample_device *dev = i2c_get_clientdata(client);

    return 0;
}

__________________________________________________________________________

Sample-I2c-Client-Board Driver

 #include
#include

/* you might add a irq or platform_data if need
 *
 * { I2C_BOARD_INFO("sample_i2c_client", 0x50),
 *   .irq = 100,
 *   .platform_data = &sample_i2c_2_pdata,
 * }
 */
static struct i2c_board_info i2c_2_sample_devs[] __initdata = {
    { I2C_BOARD_INFO("sample_i2c_client", 0x50), },

};

int machine_init(void)
{
    i2c_register_board_info(2, i2c_2_sample_devs,
            ARRAY_SIZE(i2c_2_sample_devs));

}



No comments:

Post a Comment