��ǰλ��:��ҳ > Ƕ��ʽ��ѵ > Ƕ��ʽѧϰ > ��ʦ���� >
ʵ������linux�ں�I2C��ϵ�ṹ��2��
ʱ�䣺2018-08-16���ߣ�����Զ��

�ġ����ں���дi2c�豸���������ַ�ʽ

����ʵ������linux�ں�I2C��ϵ�ṹ��1����һ�Ľ���������/dev/i2c-0��Ӧ�ò���ɶ�i2c�豸�IJ��������ܶ�ʱ�����ǻ���ϰ��Ϊi2c�豸���ں˲��д��������Ŀǰ�ں�֧�����ֱ�дi2c��������ķ�ʽ������ֱ���������ַ�ʽ��ʵ�֡�����ֱ�������ַ�ʽΪ“Adapter��ʽ��LEGACY��”��“Probe��ʽ��new style��”��

��1�� Adapter��ʽ��LEGACY��

�������ʵ����������2.6.27�ں˵�pca953x.c�������޸ĵģ�ԭʼ������õ��DZ��Ľ�Ҫ���۵ĵ�2�ַ�ʽ����Probe��ʽ��

��    ����i2c_driver

static struct i2c_driver pca953x_driver = {
                .driver = {
                                    .name= "pca953x", //����
                                },
                .id= ID_PCA9555,//id��
                .attach_adapter= pca953x_attach_adapter, //���������������豸
                .detach_client= pca953x_detach_client,//���豸����������
        };

��    ע��i2c_driver

static int __init pca953x_init(void)
        {
                return i2c_add_driver(&pca953x_driver);
        }
        module_init(pca953x_init);

��    attach_adapter����

ִ��i2c_add_driver(&pca953x_driver)��ᣬ����ں����Ѿ�ע����i2c����������˳�������Щ���������������ǵ�i2c�豸���˹�����ͨ������i2c_driver�е�attach_adapter������ɵġ�����ʵ����ʽ���£�

static int pca953x_attach_adapter(struct i2c_adapter *adapter)
        {
                return i2c_probe(adapter, &addr_data, pca953x_detect);
                /*
                adapter:������
                addr_data:��ַ��Ϣ
                pca953x_detect��̽�⵽�豸����õĺ���
                */
        }

��ַ��Ϣaddr_data�����������ָ���ġ�
        /* Addresses to scan */
        static unsigned short normal_i2c[] = {0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,I2C_CLIENT_END};
        I2C_CLIENT_INSMOD;

ע�⣺normal_i2c��ĵ�ַ��������i2cоƬ�ĵ�ַ�������޷���ȷ̽�⵽�豸����I2C_ CLIENT_INSMOD��һ���꣬��������normal_i2c����addr_data��

��    ����i2c_client����ע���ַ��豸����

i2c_probe��̽�⵽Ŀ���豸�󣬺����pca953x_detect�����ѵ�ʱ��̽���ַaddress��Ϊ�������롣

static int pca953x_detect(struct i2c_adapter *adapter, int address, int kind)
        {
                struct i2c_client *new_client;
                struct pca953x_chip *chip; //�豸�ṹ��
                int err = 0,result;
                dev_t pca953x_dev=MKDEV(pca953x_major,0);//�����豸�ţ����ݾ�������趨��������ֻ������normal_i2c��ֻ��һ����ַƥ��������
                if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA| I2C_FUNC_SMBUS_WORD_DATA))//�ж�����������
                goto exit;
                if (!(chip = kzalloc(sizeof(struct pca953x_chip), GFP_KERNEL))) {
                        err = -ENOMEM;
                        goto exit;
                }
                /****����i2c-client****/
                chip->client=kzalloc(sizeof(struct i2c_client),GFP_KERNEL);
                new_client = chip->client;
                i2c_set_clientdata(new_client, chip);
                new_client->addr = address;
                new_client->adapter = adapter;
                new_client->driver = &pca953x_driver;
                new_client->flags = 0;
                strlcpy(new_client->name, "pca953x", I2C_NAME_SIZE);
                if ((err = i2c_attach_client(new_client)))//ע��i2c_client
                goto exit_kfree;
                if (err)
                goto exit_detach;
                if(pca953x_major)
                {
                        result=register_chrdev_region(pca953x_dev,1,"pca953x");
                }
                else{
                        result=alloc_chrdev_region(&pca953x_dev,0,1,"pca953x");
                        pca953x_major=MAJOR(pca953x_dev);
                }
                if (result < 0) {
                        printk(KERN_NOTICE "Unable to get pca953x region, error %d\n", result);
                        return result;
                }
                pca953x_setup_cdev(chip,0); //ע���ַ��豸���˴������
                return 0;
                exit_detach:
                i2c_detach_client(new_client);
        exit_kfree:
                kfree(chip);
        exit:
                return err;
        }

i2c_check_functionality�����ж�����������������һ��dz���Ҫ����Ҳ����ֱ�Ӳ鿴��Ӧ����������������

static const struct i2c_algorithm smbus_algorithm = {
                .smbus_xfer= i801_access,
                .functionality= i801_func,
        };
        static u32 i801_func(struct i2c_adapter *adapter)
        {
                        return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
                    I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
                I2C_FUNC_SMBUS_BLOCK_DATA | I2C_FUNC_SMBUS_WRITE_I2C_BLOCK
                                | (isich4 ? I2C_FUNC_SMBUS_HWPEC_CALC : 0);
        }

��    �ַ������ľ���ʵ��

struct file_operations pca953x_fops = {
                .owner = THIS_MODULE,
                .ioctl= pca953x_ioctl, 
                .open= pca953x_open, 
                .release =pca953x_release, 
        };

�ַ��豸��������û��ʲô��˵�ģ�������Ҫ��˵һ�£�����������е���i2c������������������ݴ��䡣

Ŀǰ��������Ҫ֧�����ִ��䷽����smbus_xfer��master_xfer��һ����˵�����������֧����master_xfer��ô��Ҳ����ģ��֧��smbus�Ĵ��䡣�����ֻʵ��smbus_xfer����֧��һЩi2c�Ĵ��䡣

int (*master_xfer)(struct i2c_adapter *adap,struct i2c_msg *msgs,int num);
        int (*smbus_xfer) (struct i2c_adapter *adap, u16 addr,
                                                                                unsigned short flags, char read_write,
                                                                u8 command, int size, union i2c_smbus_data * data);

master_xfer�еIJ������ã���ǰ����û��ռ���һ�¡�����ֻ��Ҫ�������й�����صIJ���Ȼ�����i2c_transfer����ɴ���ȿɡ�

int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg *msgs, int num)

smbus_xfer�еIJ������ü����÷������£�

static int pca953x_write_reg(struct pca953x_chip *chip, int reg, uint16_t val)
        {
                int ret;
                ret = i2c_smbus_write_word_data(chip->client, reg << 1, val);
                if (ret < 0) {
                                dev_err(&chip->client->dev, "failed writing register\n");
                                        return -EIO;
                                }
                return 0;
        }

���溯�������оƬ�ĵ�ַΪreg�ļĴ���дһ��16bit�����ݡ�i2c_smbus_write_word_data��ʵ�����£�

s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
        {
                union i2c_smbus_data data;
                data.word = value;
                return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
                                                                        I2C_SMBUS_WRITE,command,
                                                                        I2C_SMBUS_WORD_DATA,&data);
        }

���п��Կ���smbus����һ��16λ���ݵķ��������������磺�ַ�д���ַ������ֶ���������ȣ����Բο��ں˵�i2c-core.c���ṩ�ķ�����

��    ע��i2c_driver

static void __exit pca953x_exit(void)
        {
                i2c_del_driver(&pca953x_driver);
        }
        module_exit(pca953x_exit);

��    detach_client����

˳������ں���ע������������Ͽ�����ע�����i2c�豸���˹���ͨ������i2c_driver�е�attach_adapter������ɵġ�����ʵ����ʽ���£�

static int pca953x_detach_client(struct i2c_client *client)
        {
                int err;
                struct pca953x_chip *data;
                if ((err = i2c_detach_client(client)))//�Ͽ�i2c_client
                return err;
                data=i2c_get_clientdata(client);
                cdev_del(&(data->cdev));
                unregister_chrdev_region(MKDEV(pca953x_major, 0), 1);
                kfree(data->client);
                kfree(data);
                return 0;
        }

��2�� Probe��ʽ��new style��

��    ����i2c_driver

��LEGACY��ʽһ����Ҳ��Ҫ����i2c_driver����������������ͬ��

static struct i2c_driver pca953x_driver = {
                .driver = {
                        .name= "pca953x",
                        },
                        .probe= pca953x_probe, //����i2c_client��i2c_driverƥ��ʱ����
                        .remove= pca953x_remove,//ע��ʱ����
                        .id_table= pca953x_id,//ƥ�����
        };

��    ע��i2c_driver

static int __init pca953x_init(void)
        {
                return i2c_add_driver(&pca953x_driver);
        }
        module_init(pca953x_init);

��ע��i2c_driver�Ĺ����У��ǽ�driverע�ᵽ��i2c_bus_type�������ϡ������ߵ�ƥ������ǣ�

static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
                                                                                                const struct i2c_client *client)
        {
                while (id->name[0]) {
                        if (strcmp(client->name, id->name) == 0)
                                return id;
                        id++;
                }
                return NULL;
        }

���Կ���������i2c_client�����ƺ�id_table�е�������ƥ��ġ��������е�id_tableΪ

static const struct i2c_device_id pca953x_id[] = {
                { "pca9534", 8, },
                { "pca9535", 16, },
                { "pca9536", 4, },
                { "pca9537", 4, },
                { "pca9538", 8, },
                { "pca9539", 16, },
                { "pca9554", 8, },
                { "pca9555", 16, },
                { "pca9557", 8, },
                { "max7310", 8, },
                { }
        };

������������Ӧ�û������������ʣ���Adapterģʽ�У�i2c_client�������Լ���������ģ������ڵ�i2c_client�Ǵ��������أ���������Ľ���

��    ע��i2c_board_info

����Probeģʽ��ͨ����ƽ̨������Ҫ���i2c_board_info��ע�ᡣ�������£�

static struct i2c_board_info __initdata test_i2c_devices[] = {
                {
                        I2C_BOARD_INFO("pca9555", 0x27),//pca9555ΪоƬ���ƣ�0x27ΪоƬ��ַ
                        .platform_data = &pca9555_data,
                }, {
                        I2C_BOARD_INFO("mt9v022", 0x48),
                        .platform_data = &iclink[0], /* With extender */
                }, {
                        I2C_BOARD_INFO("mt9m001", 0x5d),
                        .platform_data = &iclink[0], /* With extender */
                },
        };
        i2c_register_board_info(0, test_i2c_devices,ARRAY_SIZE(test_i2c_devices)); //ע��

i2c_client������ע������й����ġ�����һ����Ҫע�����i2c_register_board_info��û��EXPORT_SYMBOL��ģ��ʹ�á�

��    �ַ�����ע��

��Probe��ʽ�£�����ַ�������λ����pca953x_probe�С�

static int __devinit pca953x_probe(struct i2c_client *client,const struct i2c_device_id *id)
        {
                        ……
                        /****�ַ��豸����ע��λ��****/
                        ……
                        return 0;
        }

��    ע��i2c_driver

static void __exit pca953x_exit(void)
        {
                i2c_del_driver(&pca953x_driver);
        }
        module_exit(pca953x_exit);

��    ע���ַ��豸����

��Probe��ʽ�£�ע���ַ�������λ����pca953x_remove�С�

static int __devinit pca953x_remove (struct i2c_client *client)
        {
                ……
                /****�ַ��豸����ע����λ��****/
                ……
                return 0;
        }

��    I2C�豸�����ݽ�������(�������������������豸�ķ���)��Adapter��ʽ����ͬ��


��������

ȫ����ѯ�绰��400-611-6270��˫���ռ��ڼ������µ�ֵ���ֻ���15010390966

������ѯ�� ����ʦQQ��3337544669���� ����ʦQQ��1462495461���� ����ʦ QQ��3108687497��

��ҵ��ѵǢ̸ר�ߣ�010-82600901��ԺУ����Ǣ̸ר�ߣ�010-82600350��������ѯ��QQ��248856300��

Copyright 2004-2018 ����Զ���������� ��Ȩ���� ����ICP��16055225�ţ�������������11010802025203��