I2C Driver Instance resolution

Source: Internet
Author: User
Tags goto

Simply put, I2C drive is also three steps, register I2C device, register I2C device driver, create SYSFS file for Upper call.


1. Register I2C equipment.

First define a I2c_board_info

static struct I2c_board_info __initdata xxxx_i2c_info[] = {    
        i2c_board_info ("xxxx", 0x1d),    
        . platform_ data = &xxxx_platform_data,    
        },     

Re-registration, will generally register a client.

I2c_register_board_info ()

Strongly suggest interested can look at this function of the source code, can learn a lot.


2. Register I2C Equipment Driver

Define re-registration

static struct I2c_driver Xxx_i2c_driver = {
	. id_table = xxx_i2c_id,
	. Probe  = Xxx_i2c_probe,
	. remove = __ Exit_p (Xxx_i2c_remove),
	. Driver = {
		. name = "xxx",
	},
};

Registration function

I2c_add_driver (&xxx_i2c_driver);

In the registration process, the main call probe function, how to run to the probe function also need to follow the code. Not in detail here. Talk about the main probe function.

static int xxx_i2c_probe (struct i2c_client *client,
		const struct I2C_DEVICE_ID *id)
{
	int rc = 0;
	PRINTK ("%s called!\n", __func__);

	if (!i2c_check_functionality (Client->adapter, I2C_FUNC_I2C)) {
		printk ("i2c_check_functionality failed\n");
		Goto probe_failure;
	}

	XXX_SENSORW = kzalloc (sizeof (struct xxx_work_t), gfp_kernel);
	if (!XXX_SENSORW) {
		printk ("Kzalloc failed.\n");
		rc =-enomem;
		Goto probe_failure;
	}

	I2c_set_clientdata (client, XXX_SENSORW);
	Xxx_init_client (client);
	xxx_client = client;

	Msleep (m);

	PRINTK ("%s successed! rc =%d\n ", __func__, RC);
	return 0;

Probe_failure:
	printk ("%s failed! rc =%d\n ", __func__, RC);
	return RC;
}

The probe function is primarily to obtain the client and then transmit the data by means of a adapter in the client:

static int Xxx_i2c_txdata (const uint16_t saddr,
		uint8_t *txdata, const uint16_t length)
{
	struct i2c_msg msg [] = {
			. addr = saddr,
			. Flags = 0,
			. Len = length,
			. buf = TxData,
		},
	};
	if (I2c_transfer (Xxx_client->adapter, MSG, 1) < 0) {
		printk ("Xxx_i2c_txdata faild 0x%x\n", saddr);
		Return-eio;
	}

	return 0;
}

3. Create SYSFS for Upper call

You can pass the function

Sysfs_create_group (& (Pdev->dev.kobj), &xxx_attribute_group);


To create.

The above is briefly said, mainly to see the source code can be a deeper understanding of these processes.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.