Linux device driver of the IIC (a)

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

Writing in the front.

      The IIC bus only need two lines can complete the read and write operation, and simple communication protocol, mount the multiple devices can be a bus, so it is widely used in. But the IIC bus has a drawback, that the transmission rate is low. Based on the Linux-2.6.36 version, the talk about the IIC subsystem in Linux.

     System structure diagram to borrow a book on the IIC subsystem.:

                            Linux IIC system architecture

       Below to begin the analysis of IIC system.

      In the drivers/i2c/i2c-core.c file in the i2c_init function of the IIC subsystem in the initialization:

00001221 static int __init i2c_init(void)
00001222 {
00001223     int retval;
00001224 
00001225     retval = bus_register(&i2c_bus_type);
00001226     if (retval)
00001227         return retval;
00001228 #ifdef CONFIG_I2C_COMPAT
00001229     i2c_adapter_compat_class = class_compat_register("i2c-adapter");
00001230     if (!i2c_adapter_compat_class) {
00001231         retval = -ENOMEM;
00001232         goto bus_err;
00001233     }
00001234 #endif
00001235     retval = i2c_add_driver(&dummy_driver);
00001236     if (retval)
00001237         goto class_err;
00001238     return 0;
00001239 
00001240 class_err:
00001241 #ifdef CONFIG_I2C_COMPAT
00001242     class_compat_unregister(i2c_adapter_compat_class);
00001243 bus_err:
00001244 #endif
00001245     bus_unregister(&i2c_bus_type);
00001246     return retval;
00001247 }

1225, registered IIC bus to the system, which is defined as the i2c_bus_type:

00000343 struct bus_type i2c_bus_type = {
00000344     .name        = "i2c",
00000345     .match        = i2c_device_match,
00000346     .probe        = i2c_device_probe,
00000347     .remove        = i2c_device_remove,
00000348     .shutdown    = i2c_device_shutdown,
00000349     .pm        = &i2c_device_pm_ops,
00000350 };

The 345 line, the i2c_device_match function is used to match the equipment and device on a IIC bus driver, see below under the definition of its:

00000068 static int i2c_device_match(struct device *dev, struct device_driver *drv)
00000069 {
00000070     struct i2c_client    *client = i2c_verify_client(dev);
00000071     struct i2c_driver    *driver;
00000072 
00000073     if (!client)
00000074         return 0;
00000075 
00000076     /* Attempt an OF style match */
00000077     if (of_driver_match_device(dev, drv))
00000078         return 1;
00000079 
00000080     driver = to_i2c_driver(drv);
00000081     /* match on an id table if there is one */
00000082     if (driver->id_table)
00000083         return i2c_match_id(driver->id_table, client) != NULL;
00000084 
00000085     return 0;
00000086 }

In the IIC system, using struct i2c_client to describe a specific IIC device (IIC from machine). The 73 line, if there is no IIC equipment if we return directly to 0, said the match is not successful.

The 77 line, with the way of matching, should be the specific device tree, have no idea.

The 82 line, if driven ID table i2c_match_id function is called the 83 line matching:

00000057 static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
00000058                         const struct i2c_client *client)
00000059 {
00000060     while (id->name[0]) {
00000061         if (strcmp(client->name, id->name) == 0)
00000062             return id;
00000063         id++;
00000064     }
00000065     return NULL;
00000066 }

Very simple, is to take the drive of ID table in each of a and i2c_client name members are compared, and if they have the same name as that match, or the failure to match, return NULL. As can be seen from here is the IIC bus and platform bus matching mode is not the same, in general, matching of IIC bus is according to the device name and drive in the ID table, and the matching platfrom bus is according to the device name and drive name. The i2c_device_probe function:

00000106 static int i2c_device_probe(struct device *dev)
00000107 {
00000108     struct i2c_client    *client = i2c_verify_client(dev);
00000109     struct i2c_driver    *driver;
00000110     int status;
00000111 
00000112     if (!client)
00000113         return 0;
00000114 
00000115     driver = to_i2c_driver(dev->driver);
00000116     if (!driver->probe || !driver->id_table)
00000117         return -ENODEV;
00000118     client->driver = driver;
00000119     if (!device_can_wakeup(&client->dev))
00000120         device_init_wakeup(&client->dev,
00000121                     client->flags & I2C_CLIENT_WAKE);
00000122     dev_dbg(dev, "probe\n");
00000123 
00000124     status = driver->probe(client, i2c_match_id(driver->id_table, client));
00000125     if (status) {
00000126         client->driver = NULL;
00000127         i2c_set_clientdata(client, NULL);
00000128     }
00000129     return status;
00000130 }

112, check whether there IIC equipment.

119 to 121 lines, power management, IIC in power management is good enough, are interested can look at.

The 124 line, important, call the IIC device driver in the probe function.

     The following tiny6410 for a specific platform to other content in IIC system. The IIC controller S3c6410 drive in the drivers/i2c/busses/i2c-s3c2410.c file, first look at the initialization function:

00001009 static struct platform_driver s3c24xx_i2c_driver = {
00001010     .probe        = s3c24xx_i2c_probe,
00001011     .remove        = s3c24xx_i2c_remove,
00001012     .id_table    = s3c24xx_driver_ids,
00001013     .driver        = {
00001014         .owner    = THIS_MODULE,
00001015         .name    = "s3c-i2c",
00001016         .pm    = S3C24XX_DEV_PM_OPS,
00001017     },
00001018 };
00001019 
00001020 static int __init i2c_adap_s3c_init(void)
00001021 {
00001022     return platform_driver_register(&s3c24xx_i2c_driver);
00001023 }
00001024 subsys_initcall(i2c_adap_s3c_init);

1022, registration platform drive, see definition under s3c24xx_i2c_driver can be found platform device definition of the.Driver.name value and board file name not the same, so here is the use of matching ID table. We know that, when the driving and equipment matching, the probe function will drive in is called, then the definition of probe function:

00000790 static int s3c24xx_i2c_probe(struct platform_device *pdev)
00000791 {
00000792     struct s3c24xx_i2c *i2c;
00000793     struct s3c2410_platform_i2c *pdata;
00000794     struct resource *res;
00000795     int ret;
00000796 
00000797     pdata = pdev->dev.platform_data;
00000798     if (!pdata) {
00000799         dev_err(&pdev->dev, "no platform data\n");
00000800         return -EINVAL;
00000801     }
00000802 
00000803     i2c = kzalloc(sizeof(struct s3c24xx_i2c), GFP_KERNEL);
00000804     if (!i2c) {
00000805         dev_err(&pdev->dev, "no memory for state\n");
00000806         return -ENOMEM;
00000807     }
00000808 
00000809     strlcpy(i2c->adap.name, "s3c2410-i2c", sizeof(i2c->adap.name));
00000810     i2c->adap.owner   = THIS_MODULE;
00000811     i2c->adap.algo    = &s3c24xx_i2c_algorithm;
00000812     i2c->adap.retries = 2;
00000813     i2c->adap.class   = I2C_CLASS_HWMON | I2C_CLASS_SPD;
00000814     i2c->tx_setup     = 50;
00000815 
00000816     spin_lock_init(&i2c->lock);
00000817     init_waitqueue_head(&i2c->wait);
00000818 
00000819     /* find the clock and enable it */
00000820 
00000821     i2c->dev = &pdev->dev;
00000822     i2c->clk = clk_get(&pdev->dev, "i2c");
00000823     if (IS_ERR(i2c->clk)) {
00000824         dev_err(&pdev->dev, "cannot get clock\n");
00000825         ret = -ENOENT;
00000826         goto err_noclk;
00000827     }
00000828 
00000829     dev_dbg(&pdev->dev, "clock source %p\n", i2c->clk);
00000830 
00000831     clk_enable(i2c->clk);
00000832 
00000833     /* map the registers */
00000834 
00000835     res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
00000836     if (res == NULL) {
00000837         dev_err(&pdev->dev, "cannot find IO resource\n");
00000838         ret = -ENOENT;
00000839         goto err_clk;
00000840     }
00000841 
00000842     i2c->ioarea = request_mem_region(res->start, resource_size(res),
00000843                      pdev->name);
00000844 
00000845     if (i2c->ioarea == NULL) {
00000846         dev_err(&pdev->dev, "cannot request IO\n");
00000847         ret = -ENXIO;
00000848         goto err_clk;
00000849     }
00000850 
00000851     i2c->regs = ioremap(res->start, resource_size(res));
00000852 
00000853     if (i2c->regs == NULL) {
00000854         dev_err(&pdev->dev, "cannot map IO\n");
00000855         ret = -ENXIO;
00000856         goto err_ioarea;
00000857     }
00000858 
00000859     dev_dbg(&pdev->dev, "registers %p (%p, %p)\n",
00000860         i2c->regs, i2c->ioarea, res);
00000861 
00000862     /* setup info block for the i2c core */
00000863 
00000864     i2c->adap.algo_data = i2c;
00000865     i2c->adap.dev.parent = &pdev->dev;
00000866 
00000867     /* initialise the i2c controller */
00000868 
00000869     ret = s3c24xx_i2c_init(i2c);
00000870     if (ret != 0)
00000871         goto err_iomap;
00000872 
00000873     /* find the IRQ for this unit (note, this relies on the init call to
00000874      * ensure no current IRQs pending
00000875      */
00000876 
00000877     i2c->irq = ret = platform_get_irq(pdev, 0);
00000878     if (ret <= 0) {
00000879         dev_err(&pdev->dev, "cannot find IRQ\n");
00000880         goto err_iomap;
00000881     }
00000882 
00000883     ret = request_irq(i2c->irq, s3c24xx_i2c_irq, IRQF_DISABLED,
00000884               dev_name(&pdev->dev), i2c);
00000885 
00000886     if (ret != 0) {
00000887         dev_err(&pdev->dev, "cannot claim IRQ %d\n", i2c->irq);
00000888         goto err_iomap;
00000889     }
00000890 
00000891     ret = s3c24xx_i2c_register_cpufreq(i2c);
00000892     if (ret <0) {
00000893         dev_err(&pdev->dev, "failed to register cpufreq notifier\n");
00000894         goto err_irq;
00000895     }
00000896 
00000897     /* Note, previous versions of the driver used i2c_add_adapter()
00000898      * to add the bus at any number. We now pass the bus number via
00000899      * the platform data, so if unset it will now default to always
00000900      * being bus 0.
00000901      */
00000902 
00000903     i2c->adap.nr = pdata->bus_num;
00000904 
00000905     ret = i2c_add_numbered_adapter(&i2c->adap);
00000906     if (ret <0) {
00000907         dev_err(&pdev->dev, "failed to add bus to i2c core\n");
00000908         goto err_cpufreq;
00000909     }
00000910 
00000911     platform_set_drvdata(pdev, i2c);
00000912 
00000913     dev_info(&pdev->dev, "%s: S3C I2C adapter\n", dev_name(&i2c->adap.dev));
00000914     return 0;
00000915 
00000916  err_cpufreq:
00000917     s3c24xx_i2c_deregister_cpufreq(i2c);
00000918 
00000919  err_irq:
00000920     free_irq(i2c->irq, i2c);
00000921 
00000922  err_iomap:
00000923     iounmap(i2c->regs);
00000924 
00000925  err_ioarea:
00000926     release_resource(i2c->ioarea);
00000927     kfree(i2c->ioarea);
00000928 
00000929  err_clk:
00000930     clk_disable(i2c->clk);
00000931     clk_put(i2c->clk);
00000932 
00000933  err_noclk:
00000934     kfree(i2c);
00000935     return ret;
00000936 }

797 to 801 lines, no platform data is not.

803 to 807 lines, application of memory, IIC controller data structure concrete platform in general, not only is the IIC controller, each controller will have a structure to describe. Struct s3c24xx_i2c is defined in drivers/i2c/busses/i2c-s3c2410.c:

00000060 struct s3c24xx_i2c {
00000061     spinlock_t        lock;
00000062     wait_queue_head_t    wait;
00000063     unsigned int        suspended:1;
00000064 
00000065     struct i2c_msg        *msg;
00000066     unsigned int        msg_num;
00000067     unsigned int        msg_idx;
00000068     unsigned int        msg_ptr;
00000069 
00000070     unsigned int        tx_setup;
00000071     unsigned int        irq;
00000072 
00000073     enum s3c24xx_i2c_state    state;
00000074     unsigned long        clkrate;
00000075 
00000076     void __iomem        *regs;
00000077     struct clk        *clk;
00000078     struct device        *dev;
00000079     struct resource        *ioarea;
00000080     struct i2c_adapter    adap;
00000081 
00000082 #ifdef CONFIG_CPU_FREQ
00000083     struct notifier_block    freq_transition;
00000084 #endif
00000085 };

The 63 line, indicating whether or not the IIC controller has been suspended, pending case will not be able to operate the IIC controller.

The 65 line, struct i2c_msg is used to describe a read and write operation contains information. Defined in include/linux/i2c.h, relatively simple:

00000507 struct i2c_msg {
00000508     __u16 addr;    /* slave address            */
00000509     __u16 flags;
00000510 #define I2C_M_TEN        0x0010    /* this is a ten bit chip address */
00000511 #define I2C_M_RD        0x0001    /* read data, from slave to master */
00000512 #define I2C_M_NOSTART        0x4000    /* if I2C_FUNC_PROTOCOL_MANGLING */
00000513 #define I2C_M_REV_DIR_ADDR    0x2000    /* if I2C_FUNC_PROTOCOL_MANGLING */
00000514 #define I2C_M_IGNORE_NAK    0x1000    /* if I2C_FUNC_PROTOCOL_MANGLING */
00000515 #define I2C_M_NO_RD_ACK        0x0800    /* if I2C_FUNC_PROTOCOL_MANGLING */
00000516 #define I2C_M_RECV_LEN        0x0400    /* length will be first received byte */
00000517     __u16 len;        /* msg length                */
00000518     __u8 *buf;        /* pointer to msg data            */
00000519 };

The 508 line, IIC from the machine.

The 509 line, the value of flags is 510 to 516 for these values.

The 517 line, the read and write operation of data length. The 518 line, read and write data.

      Back to struct s3c24xx_i2c, the 66 line, the number of message. The 67 line, the current is the first of several message. The 68 line, the buffer array member index value, representing the current to reading and writing is the first of several data.

The 70 line, when writing data to the IIC controller of the data shift register need how long after the time delay, in the S3C6410 unit is NS.

The 71 line, the IIC Controller Interrupt.

73, the state of a IIC control, specifically the following state:

00000047 enum s3c24xx_i2c_state {
00000048     STATE_IDLE,
00000049     STATE_START,
00000050     STATE_READ,
00000051     STATE_WRITE,
00000052     STATE_STOP
00000053 };

The 74 line, rate of IIC bus. The 76 line, the IIC controller registers the starting address.

The 77 line, IIC controller clock. The 78 line, equipment model related to. The 79 line, IO port resource.

80 lines, each corresponding to a adapter IIC controller. Struct i2c_adapter is also defined in the include/linux/i2c.h:

00000354 struct i2c_adapter {
00000355     struct module *owner;
00000356     unsigned int id;
00000357     unsigned int class;          /* classes to allow probing for */
00000358     const struct i2c_algorithm *algo; /* the algorithm to access the bus */
00000359     void *algo_data;
00000360 
00000361     /* data fields that are valid for all devices    */
00000362     struct rt_mutex bus_lock;
00000363 
00000364     int timeout;            /* in jiffies */
00000365     int retries;
00000366     struct device dev;        /* the adapter device */
00000367 
00000368     int nr;
00000369     char name[48];
00000370     struct completion dev_released;
00000371 
00000372     struct mutex userspace_clients_lock;
00000373     struct list_head userspace_clients;
00000374 };

The 355 line, module owner.

The 356 line, this adapter number, first adapters are numbered 0, and so on.

The 358 line, the algorithm? At first glance was almost frightened, is actually defines 3 pointers to functions, these functions to accomplish the read and write operations.

The 364 line, the timeout. The 365 line, the number of retries, a read and write operations do not succeed to try a few times. The 368 line, the adapter number. The 369 line, the adapter name.

     Return to the s3c24xx_i2c_probe function, the 809 line, set the adapter name.

The 811 line, the IIC controller S3C6410 is used to read and write operation logic, and met with said again.

The 813 line, said struct i2c_adapter is ignored when members of the class is assigned here.

814, for the IIC controller S3C6410, the data is written to the register need to delay 50ns.

822 to 831 line, get IIC clock and enable.

835 to 857 line, get IO port resource and mapping.

The 869 line, set the corresponding IO port for the IIC function, and set the IICADD and IICCON registers.

877 to 889 lines, to apply for IIC interrupt, the interrupt processing function for the s3c24xx_i2c_irq, will meet behind it,.

891 to 895 rows, CPU frequency dependent, skip.

The 905 line, “ major ” ah, register the IIC adapter and register the IIC equipment was on the inside, the i2c_add_numbered_adapter function is defined in the drivers/i2c/i2c-core.c.:

00000948 int i2c_add_numbered_adapter(struct i2c_adapter *adap)
00000949 {
00000950     int    id;
00000951     int    status;
00000952 
00000953     if (adap->nr & ~MAX_ID_MASK)
00000954         return -EINVAL;
00000955 
00000956 retry:
00000957     if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
00000958         return -ENOMEM;
00000959 
00000960     mutex_lock(&core_lock);
00000961     /* "above" here means "above or equal to", sigh;
00000962      * we need the "equal to" result to force the result
00000963      */
00000964     status = idr_get_new_above(&i2c_adapter_idr, adap, adap->nr, &id);
00000965     if (status == 0 && id != adap->nr) {
00000966         status = -EBUSY;
00000967         idr_remove(&i2c_adapter_idr, id);
00000968     }
00000969     mutex_unlock(&core_lock);
00000970     if (status == -EAGAIN)
00000971         goto retry;
00000972 
00000973     if (status == 0)
00000974         status = i2c_register_adapter(adap);
00000975     return status;
00000976 }


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Angus at November 10, 2013 - 11:24 PM