Guest User

Untitled

a guest
Apr 20th, 2018
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 13.57 KB | None | 0 0
  1. /*
  2.  * drivers/i2c/bus/i2c-lf1000.c
  3.  *
  4.  * Copyright 2007 LeapFrog Enterprises Inc.
  5.  *
  6.  * Andrey Yurovsky <andrey@cozybit.com>
  7.  * Scott Esters <sesters@leapfrog.com>
  8.  *
  9.  * I2C bus driver for the LF1000 CPUs.  Based heavily on
  10.  * i2c-at91.c and i2c-mpc.c
  11.  *
  12.  * TODO: more pin configurations (for the rest of the channels)
  13.  *
  14.  * This program is free software; you can redistribute it and/or modify
  15.  * it under the terms of the GNU General Public License as published by
  16.  * the Free Software Foundation.
  17.  */
  18.  
  19. #include <linux/module.h>
  20. #include <linux/version.h>
  21. #include <linux/kernel.h>
  22. #include <linux/slab.h>
  23. #include <linux/pci.h>
  24. #include <linux/types.h>
  25. #include <linux/i2c.h>
  26. #include <linux/init.h>
  27. #include <linux/wait.h>
  28. #include <linux/delay.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/spinlock.h>
  31. #include <linux/platform_device.h>
  32.  
  33. #include <asm/io.h>
  34. #include <mach/platform.h>
  35. #include <mach/common.h>
  36. #include <mach/i2c.h>
  37. #include <mach/gpio.h>
  38.  
  39. #define DRIVER_NAME     "lf1000-i2c"
  40. #define I2C_CHANNEL     CONFIG_I2C_LF1000_CHANNEL
  41. #define LF1000_I2C_TIMEOUT  10 /* (in jiffies) */
  42. #define LF1000_I2C_RATE_HZ  100000
  43.  
  44. /* FIXME: add channel 0 settings, choose based on I2C_CHANNEL */
  45. #define I2C_SCL0_PORT   GPIO_PORT_A
  46. #define I2C_SCL0_PIN    GPIO_PIN26
  47. #define I2C_SCL0_FN GPIO_ALT1
  48. #define I2C_SDA0_PORT   GPIO_PORT_A
  49. #define I2C_SDA0_PIN    GPIO_PIN27
  50. #define I2C_SDA0_FN GPIO_ALT1
  51.  
  52. #define I2C_SCL1_PORT   GPIO_PORT_A
  53. #define I2C_SCL1_PIN    GPIO_PIN28
  54. #define I2C_SCL1_FN GPIO_ALT1
  55. #define I2C_SDA1_PORT   GPIO_PORT_A
  56. #define I2C_SDA1_PIN    GPIO_PIN29
  57. #define I2C_SDA1_FN GPIO_ALT1
  58.  
  59. enum lf1000_i2c_state {
  60.     I2C_SEND_ADDR,
  61.     I2C_SEND_DATA,
  62.     I2C_SEND_DONE,
  63. };
  64.  
  65. struct lf1000_i2c {
  66.     int         div;
  67.  
  68.     /* device access */
  69.     wait_queue_head_t   wait;
  70.     int         ready;
  71.  
  72.     /* bus access */
  73.     wait_queue_head_t   bus_access;
  74.     int         busy;
  75.     struct i2c_adapter  adap;
  76.    
  77.     void __iomem        *reg_base;
  78.  
  79.     unsigned long       iobase;
  80.     unsigned long       iosize;
  81.  
  82.     int         irq;
  83. };
  84.  
  85. static irqreturn_t lf1000_i2c_irq(int irq, void *dev_id)
  86. {
  87.     struct lf1000_i2c *i2c = dev_id;
  88.     u32 tmp = ioread32(i2c->reg_base+IRQ_PEND);
  89.  
  90.     if(!(tmp & (1<<PEND))) /* sanity check */
  91.         return IRQ_NONE;
  92.  
  93.     /* clear pending interrupt */
  94.     tmp |= (1<<PEND);
  95.     iowrite32(tmp, i2c->reg_base+IRQ_PEND);
  96.  
  97.     i2c->ready = 1;
  98.     wake_up_interruptible(&i2c->wait); /* wake up anyone that is waiting */
  99.     return IRQ_HANDLED;
  100. }
  101.  
  102. static int lf1000_i2c_wait(struct lf1000_i2c *i2c)
  103. {
  104.     int ret = wait_event_interruptible_timeout(i2c->wait, (i2c->ready),
  105.                            LF1000_I2C_TIMEOUT);
  106.  
  107.     if(unlikely(ret < 0))
  108.         printk(KERN_INFO "i2c: interrupted\n");
  109.     else if(unlikely(!(i2c->ready)))
  110.         return -ETIMEDOUT;
  111.  
  112.     i2c->ready = 0;
  113.     return 0;
  114. }
  115.  
  116. static int i2c_bus_available(struct lf1000_i2c *i2c)
  117. {
  118.     unsigned long flags;
  119.     int ret;
  120.  
  121.     spin_lock_irqsave(&i2c->bus_access, flags);
  122.     ret = !(i2c->busy);
  123.     spin_unlock_irqrestore(&i2c->bus_access, flags);
  124.  
  125.     return ret;
  126. }
  127.  
  128. static void start_stop_condition(struct lf1000_i2c *i2c)
  129. {
  130.     u32 tmp = ioread32(i2c->reg_base+IRQ_PEND);
  131.     tmp |= (1<<OP_HOLD); /* generate a condition */
  132.     iowrite32(tmp, i2c->reg_base+IRQ_PEND);
  133. }
  134.  
  135. static void lf1000_i2c_clock(struct lf1000_i2c *i2c, char en)
  136. {
  137.     u32 tmp = ioread32(i2c->reg_base+I2C_CLKENB);
  138.  
  139.     en ? BIT_SET(tmp, 3) :BIT_CLR(tmp, 3);
  140.  
  141.     iowrite32(tmp, i2c->reg_base+I2C_CLKENB);
  142. }
  143.  
  144. /* initialize the I2C hardware, see page 17-6 in the MP2530 data book */
  145. static void lf1000_i2c_hwinit(struct lf1000_i2c *i2c)
  146. {
  147.     /* clear control registers */
  148.     iowrite32(0, i2c->reg_base+ICCR);
  149.     iowrite32(0, i2c->reg_base+BURST_CTRL);
  150.  
  151.     /* Pclk/256/div, enable interrupts */
  152.     iowrite32((1<<CLK_SRC)|((i2c->div-1)<<CLK_SCALER)|(1<<IRQ_ENB),
  153.             i2c->reg_base+ICCR);
  154.  
  155.     iowrite32((1<<CNT_MAX), i2c->reg_base+QCNT_MAX);
  156.  
  157.     iowrite32(0x1010, i2c->reg_base+ICSR);
  158.  
  159.     start_stop_condition(i2c); /* STOP */
  160. }
  161.  
  162. static void xfer_start(struct lf1000_i2c *i2c, bool transmit)
  163. {
  164.     u32 tmp;
  165.  
  166.     /* configure for master transmit or receive mode */
  167.     tmp = ioread32(i2c->reg_base+ICSR);
  168.     tmp &= 0x1F0F;
  169.     tmp |= ((1<<ST_ENB)|(1<<MASTER_SLV)|(1<<ST_BUSY)|(1<<TXRX_ENB));
  170.     if (transmit)
  171.         tmp |= (1<<TX_RX); /* transmitter */
  172.     iowrite32(tmp, i2c->reg_base+ICSR);
  173.  
  174.     start_stop_condition(i2c); /*START*/
  175. }
  176.  
  177. static void xfer_stop(struct lf1000_i2c *i2c, bool transmit)
  178. {
  179.     u32 tmp;
  180.  
  181.     /* set up to generate STOP condition */
  182.     tmp = ioread32(i2c->reg_base+ICSR);
  183.     tmp &= 0x1F0F;
  184.     tmp |= ((1<<ST_ENB)|(1<<MASTER_SLV)|(1<<TXRX_ENB));
  185.     if (transmit)
  186.         tmp |= (1<<TX_RX);
  187.     iowrite32(tmp, i2c->reg_base+ICSR);
  188.  
  189.     start_stop_condition(i2c); /* STOP */
  190. }
  191.  
  192. /* write to a slave device, see page 17-6 in the MP2530 data book */
  193. static int xfer_write(struct lf1000_i2c *i2c, unsigned char *buf, int length)
  194. {
  195.     u32 tmp;
  196.     int ret;
  197.     enum lf1000_i2c_state state = I2C_SEND_ADDR;
  198.  
  199.     while(1) {
  200.         switch(state) {
  201.             case I2C_SEND_ADDR:
  202.             ret = lf1000_i2c_wait(i2c);
  203.             if (ret != 0)
  204.                 goto done_write;
  205.  
  206.             tmp = readl(i2c->reg_base+ICSR);
  207.             if (tmp & (1<<ACK_STATUS)) {
  208.                 dev_err(&i2c->adap.dev, "no ACK in %s\n",
  209.                         __FUNCTION__);
  210.                 ret = -EFAULT;
  211.                 goto done_write;
  212.             }
  213.  
  214.             state = I2C_SEND_DATA;
  215.             break;
  216.  
  217.             case I2C_SEND_DATA:
  218.             writel(*buf++, i2c->reg_base+IDSR); /* write data */
  219.             start_stop_condition(i2c); /* START */
  220.             ret = lf1000_i2c_wait(i2c); /* wait for IRQ */
  221.             if (ret != 0)
  222.                 goto done_write;
  223.  
  224.             tmp = readl(i2c->reg_base+ICSR);
  225.             if (tmp & (1<<ACK_STATUS)) {
  226.                 dev_err(&i2c->adap.dev, "no DATA ACK in %s\n",
  227.                         __FUNCTION__);
  228.                 ret = -EFAULT;
  229.                 goto done_write;
  230.             }
  231.  
  232.             if (--length <= 0)
  233.                 state = I2C_SEND_DONE;
  234.             break;
  235.  
  236.             case I2C_SEND_DONE:
  237.             ret = 0;
  238.             goto done_write;
  239.         }
  240.     }
  241.  
  242. done_write:
  243.     return 0;
  244. }
  245.  
  246. /* read from a slave device, see page 17-7 in the MP2530 data book */
  247. static int xfer_read(struct lf1000_i2c *i2c, unsigned char *buf, int length)
  248. {
  249.     u32 tmp;
  250.     int ret;
  251.     enum lf1000_i2c_state state = I2C_SEND_ADDR;
  252.  
  253.     while(1) {
  254.         switch(state) {
  255.             case I2C_SEND_ADDR:
  256.             ret = lf1000_i2c_wait(i2c);
  257.             if(ret != 0)
  258.                 goto done_read;
  259.             tmp = ioread32(i2c->reg_base+ICSR); /* check for an ACK */
  260.             if(tmp & (1<<ACK_STATUS)) {
  261.                 printk(KERN_INFO "i2c: no ACK in xfer_read\n");
  262.                 ret = -EFAULT;
  263.                 goto done_read;
  264.             }
  265.             /* master generates ACK for received bytes */
  266.             tmp = readl(i2c->reg_base+ICCR);
  267.             tmp |= (1<<ACK_GEN);
  268.             writel(tmp, i2c->reg_base+ICCR);
  269.             state = I2C_SEND_DATA;
  270.             break;
  271.  
  272.             case I2C_SEND_DATA:
  273.             *buf++ = ioread32(i2c->reg_base+IDSR); /* get data */
  274.             /* master stops generating ACK on last byte */
  275.             if (length <= 1) {
  276.                 tmp = readl(i2c->reg_base+ICCR);
  277.                 tmp &= ~(1<<ACK_GEN);
  278.                 writel(tmp, i2c->reg_base+ICCR);
  279.             }
  280.             start_stop_condition(i2c); /* START (request more data) */
  281.             ret = lf1000_i2c_wait(i2c); /* wait for IRQ */
  282.             if(ret != 0)
  283.                 goto done_read;
  284.  
  285.             if(--length <= 0)
  286.                 state = I2C_SEND_DONE;
  287.             break;
  288.  
  289.             case I2C_SEND_DONE:
  290.             ret = 0;
  291.             goto done_read;
  292.         }
  293.     }
  294.  
  295. done_read:
  296.     return ret;
  297. }
  298.  
  299. /* generic I2C master transfer entrypoint */
  300. static int lf1000_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
  301. {
  302.     struct lf1000_i2c *i2c = adap->algo_data;
  303.     int i, ret;
  304.     unsigned long flags;
  305.  
  306.     /* wait to get access to the bus */
  307.     spin_lock_irqsave(&i2c->bus_access, flags);
  308.     while(i2c->busy) {
  309.         spin_unlock_irqrestore(&i2c->bus_access, flags);
  310.         if(wait_event_interruptible(i2c->bus_access,
  311.                         i2c_bus_available(i2c))) {
  312.             return -ERESTARTSYS;
  313.         }
  314.         spin_lock_irqsave(&i2c->bus_access, flags);
  315.     }
  316.     i2c->busy = 1; /* got the bus */
  317.     spin_unlock_irqrestore(&i2c->bus_access, flags);
  318.  
  319.     lf1000_i2c_clock(i2c, 1);
  320.  
  321.     lf1000_i2c_hwinit(i2c);
  322.  
  323.     for(i = 0; i < num; i++) {
  324.         /* set slave device address */
  325.         iowrite32(msgs[i].addr | ((msgs[i].flags & I2C_M_RD) ? 1 : 0),
  326.                   i2c->reg_base+IDSR);
  327.  
  328.         /* signal start for each message part */
  329.         xfer_start(i2c, (msgs[i].flags & I2C_M_RD) ? 0 : 1);
  330.  
  331.         if(msgs[i].len && msgs[i].buf) {
  332.             if(msgs[i].flags & I2C_M_RD)
  333.                 ret = xfer_read(i2c, msgs[i].buf, msgs[i].len);
  334.             else
  335.                 ret = xfer_write(i2c, msgs[i].buf, msgs[i].len);
  336.  
  337.             if(ret != 0)
  338.                 goto xfer_done;
  339.         }
  340.     }
  341.     ret = i;
  342.  
  343. xfer_done:
  344.     /* signal stop at end of message */
  345.     xfer_stop(i2c, (msgs[i].flags & I2C_M_RD) ? 0 : 1);
  346.  
  347.     /* turn off I2C controller */
  348.     iowrite32(0, i2c->reg_base+ICSR);
  349.  
  350.     lf1000_i2c_clock(i2c, 0);
  351.  
  352.     /* realease the bus */
  353.     spin_lock_irqsave(&i2c->bus_access, flags);
  354.     i2c->busy = 0;
  355.     spin_unlock_irqrestore(&i2c->bus_access, flags);
  356.     return ret;
  357. }
  358.  
  359. /*
  360.  * Return list of supported functionality.
  361.  */
  362. static u32 lf1000_func(struct i2c_adapter *adapter)
  363. {
  364.     return I2C_FUNC_I2C;
  365. }
  366.  
  367. static struct i2c_algorithm lf1000_algorithm = {
  368.     .master_xfer    = lf1000_xfer,
  369.     .functionality  = lf1000_func,
  370. };
  371.  
  372. #define res_len(r)              ((r)->end - (r)->start + 1)
  373. static int lf1000_i2c_probe(struct platform_device *dev)
  374. {
  375.     struct lf1000_i2c *i2c;
  376.     struct resource *res;
  377.     int ret = 0;
  378.     unsigned int pclk_hz;
  379.     int irq;
  380.  
  381.     res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  382.     if (res == NULL) {
  383.         printk(KERN_ERR "i2c: failed to get memory resource\n");
  384.         return -ENODEV;
  385.     }
  386.  
  387.     irq = platform_get_irq(dev, 0);
  388.  
  389.     if (irq < 0) {
  390.         printk(KERN_ERR "i2c: failed to get irq resource\n");
  391.         return -ENODEV;
  392.     }
  393.    
  394.     if(!request_mem_region(res->start, res_len(res), res->name)) {
  395.         printk(KERN_ERR "i2c: failed to request memory region\n");
  396.         return -ENOMEM;
  397.     }
  398.  
  399.     i2c = kzalloc(sizeof(struct lf1000_i2c), GFP_KERNEL);
  400.     if (!i2c) {
  401.         printk(KERN_ERR "i2c: failed to allocate memory\n");
  402.         ret = -ENOMEM;
  403.         goto fail_emalloc;
  404.     }
  405.  
  406.     i2c->adap.owner   = THIS_MODULE;
  407.     i2c->adap.retries = 5;
  408.    
  409.     /* set up I2C IRQ wait queue */
  410.     init_waitqueue_head(&i2c->wait);
  411.  
  412.     /* set up I2C bus access queue */
  413.     init_waitqueue_head(&i2c->bus_access);
  414.  
  415.         /*
  416.      * If "dev->id" is negative we consider it as zero.
  417.      * The reason to do so is to avoid sysfs names that only make
  418.      * sense when there are multiple adapters.
  419.      */
  420.     i2c->adap.nr = dev->id !=-1 ? dev->id : 0;
  421.     snprintf(i2c->adap.name, sizeof(i2c->adap.name),"lf1000_i2c-i2c.%u",
  422.          i2c->adap.nr);
  423.  
  424.     i2c->reg_base = ioremap(res->start, res_len(res));
  425.     if(!i2c->reg_base) {
  426.         ret = -EIO;
  427.         goto fail_eremap;
  428.     }
  429.  
  430.     i2c->iobase = res->start;
  431.     i2c->iosize = res_len(res);
  432.  
  433.     i2c->adap.algo = &lf1000_algorithm;
  434.     i2c->adap.class = I2C_CLASS_HWMON;
  435.  
  436.     platform_set_drvdata(dev, i2c);
  437.  
  438.     /* set up IRQ handler */
  439.     i2c->irq = irq;
  440.     if(i2c->irq < 0) {
  441.         printk(KERN_ERR "i2c: failed to get an IRQ\n");
  442.         ret = i2c->irq;
  443.         goto fail_irq;
  444.     }
  445.     ret = request_irq(i2c->irq, lf1000_i2c_irq,
  446.             IRQF_DISABLED, i2c->adap.name, i2c);
  447.     if(ret) {
  448.         printk(KERN_ERR "i2c: requesting IRQ failed\n" );
  449.         goto fail_irq;
  450.     }
  451.  
  452.     pclk_hz = get_pll_freq(PCLK_PLL)/2;
  453.     i2c->div = lf1000_CalcDivider(pclk_hz/256, LF1000_I2C_RATE_HZ);
  454.     if(i2c->div < 0) {
  455.         printk(KERN_ALERT "i2c: failed to get divider, using 16\n");
  456.         i2c->div = 16;
  457.     }
  458.     else if(i2c->div > 16) {
  459.         printk(KERN_ALERT "i2c: divider too high, using 16\n");
  460.         i2c->div = 16;
  461.     }
  462.  
  463.     if (dev->id < 0)
  464.         dev->id = 0;
  465.  
  466.     /* set up IO pins */
  467.     if (dev->id == 0) {
  468.         gpio_configure_pin(I2C_SCL0_PORT, I2C_SCL0_PIN, I2C_SCL0_FN,
  469.                 1, 0, 0);
  470.         gpio_configure_pin(I2C_SDA0_PORT, I2C_SDA0_PIN, I2C_SDA0_FN,
  471.                 1, 0, 0);
  472.     } else if (dev->id == 1) {
  473.         gpio_configure_pin(I2C_SCL1_PORT, I2C_SCL1_PIN, I2C_SCL1_FN,
  474.                 1, 0, 0);
  475.         gpio_configure_pin(I2C_SDA1_PORT, I2C_SDA1_PIN, I2C_SDA1_FN,
  476.                 1, 0, 0);
  477.     }
  478.  
  479.     /* initialize I2C hardware */
  480.     lf1000_i2c_hwinit(i2c);
  481.     lf1000_i2c_clock(i2c,1);
  482.  
  483.     i2c->adap.algo_data = i2c;
  484.     i2c->adap.dev.parent = &dev->dev;
  485.    
  486.     ret = i2c_add_numbered_adapter(&i2c->adap);
  487.     if(ret != 0) {
  488.         printk(KERN_ERR "i2c: failed to add adapter\n");
  489.         goto fail_register;
  490.     }
  491.  
  492.     return 0;
  493.  
  494. fail_register:
  495.     lf1000_i2c_clock(i2c, 0);
  496.     platform_set_drvdata(dev, NULL);
  497. fail_irq:
  498.     free_irq(i2c->irq, NULL);
  499.     i2c->irq = -1;
  500.     iounmap(i2c->reg_base);
  501. fail_eremap:
  502.     kfree(i2c);
  503. fail_emalloc:
  504.     release_mem_region(res->start, (res->end - res->start) + 1);
  505.     printk(KERN_DEBUG "%s.%s:%d: error loading driver",
  506.         __FILE__, __FUNCTION__, __LINE__);
  507.     return ret;
  508. }
  509.  
  510. static int lf1000_i2c_remove(struct platform_device *dev)
  511. {
  512.     struct lf1000_i2c *i2c = platform_get_drvdata(dev);
  513.  
  514.     platform_set_drvdata(dev, NULL);
  515.  
  516.     i2c_del_adapter(&i2c->adap);
  517.     free_irq(i2c->irq, i2c);
  518.    
  519.     lf1000_i2c_clock(i2c,0);
  520.  
  521.     iounmap(i2c->reg_base);
  522.     release_mem_region(i2c->iobase, i2c->iosize);
  523.     kfree(i2c);
  524.  
  525.     return 0;
  526. }
  527.  
  528. #ifdef CONFIG_PM
  529. static int lf1000_i2c_suspend(struct platform_device *dev, pm_message_t mesg)
  530. {
  531.     struct lf1000_i2c *i2c = platform_get_drvdata(dev);
  532.     lf1000_i2c_clock(i2c,0);
  533.     return 0;
  534. }
  535.  
  536. static int lf1000_i2c_resume(struct platform_device *dev)
  537. {
  538.     struct lf1000_i2c *i2c = platform_get_drvdata(dev);
  539.     lf1000_i2c_clock(i2c,1);
  540.     return 0;
  541. }
  542. #else
  543. #define lf1000_i2c_suspend  NULL
  544. #define lf1000_i2c_resume   NULL
  545. #endif
  546.  
  547. static struct platform_driver lf1000_i2c_driver = {
  548.     .probe      = lf1000_i2c_probe,
  549.     .remove     = lf1000_i2c_remove,
  550.     .suspend    = lf1000_i2c_suspend,
  551.     .resume     = lf1000_i2c_resume,
  552.     .driver     = {
  553.         .name   = DRIVER_NAME,
  554.         .owner  = THIS_MODULE,
  555.     },
  556. };
  557.  
  558. static int __init lf1000_i2c_init(void)
  559. {
  560.     return platform_driver_register(&lf1000_i2c_driver);
  561. }
  562.  
  563. static void __exit lf1000_i2c_exit(void)
  564. {
  565.     return platform_driver_unregister(&lf1000_i2c_driver);
  566. }
  567.  
  568. module_init(lf1000_i2c_init);
  569. module_exit(lf1000_i2c_exit);
  570.  
  571. MODULE_AUTHOR("Andrey Yurovsky");
  572. MODULE_AUTHOR("Scott Esters");
  573. MODULE_DESCRIPTION("I2C driver for LF1000");
  574. MODULE_LICENSE("GPL");
Add Comment
Please, Sign In to add comment