linux i2c驱动注册流程

1. i2c platform_device设备注册

	static struct resource nuc970_i2c0_resource[] = {
        [0] = {
                .start = NUC970_PA_I2C0,
                .end   = NUC970_PA_I2C0 + NUC970_SZ_I2C0 - 1,
                .flags = IORESOURCE_MEM,
        },
        [1] = {
                .start = IRQ_I2C0,
                .end   = IRQ_I2C0,
                .flags = IORESOURCE_IRQ,
        }
	};

	static struct nuc970_platform_i2c nuc970_i2c0_data = {
		.bus_num = 0,
		.bus_freq = 100000,
	};

	struct platform_device nuc970_device_i2c0 = {
			.name		  = "nuc970-i2c0",
			.id		  = -1,
			.num_resources	  = ARRAY_SIZE(nuc970_i2c0_resource),
			.resource	  = nuc970_i2c0_resource,
			.dev = {
				.platform_data = &nuc970_i2c0_data,
			}
	};
	
	platform_add_devices(nuc970_device_i2c0, 1);

2. i2c platform_driver驱动注册

	static struct platform_driver nuc970_i2c0_driver = {
		.probe		= nuc970_i2c0_probe,
		.remove		= nuc970_i2c0_remove,
		.driver		= {
			.name	= "nuc970-i2c0",
			.owner	= THIS_MODULE,
		},
	};
	module_platform_driver(nuc970_i2c0_driver);

3. 注册i2c配置信息到全局链表上

	static struct i2c_board_info __initdata nuc970_i2c_clients0[] =
	{
		{I2C_BOARD_INFO("fm24cl16bg", 0x50),}, //i2c-0总线上
	};
	static struct i2c_board_info __initdata nuc970_i2c_clients1[] =
	{
		{I2C_BOARD_INFO("rx8025", 0x32),}, //i2c-1总线上
	};
	
	#ifdef CONFIG_I2C_BUS_NUC970_P0
		i2c_register_board_info(0, nuc970_i2c_clients0, sizeof(nuc970_i2c_clients0)/sizeof(struct i2c_board_info)); //注册到总线0上
	#endif

	#ifdef CONFIG_I2C_BUS_NUC970_P1
		i2c_register_board_info(1, nuc970_i2c_clients1, sizeof(nuc970_i2c_clients1)/sizeof(struct i2c_board_info)); //注册到总线1上
	#endif

4. 铁电或RTC注册(根据at24_ids结构体内部成员名称进行匹配)

	static const struct i2c_device_id at24_ids[] = {
		{ "fm24cl16bg", AT24_DEVICE_MAGIC(16384 / 8, 0) },
	};
	
	static struct i2c_driver at24_driver = {
		.driver = {
			.name = "at24",
			.owner = THIS_MODULE,
		},
		.probe = at24_probe,
		.remove = at24_remove,
		.id_table = at24_ids,
	};
	
	static int __init at24_init(void)
	{
		if (!io_limit) {
			pr_err("at24: io_limit must not be 0!\n");
			return -EINVAL;
		}

		FRM_WP_INIT();
		io_limit = rounddown_pow_of_two(io_limit);

		printk("%s init ok.\n", version_string);
		
		return i2c_add_driver(&at24_driver);
	}
	module_init(at24_init);

猜你喜欢

转载自blog.csdn.net/chenliang0224/article/details/80528336