Path: blob/master/drivers/i2c/busses/i2c-nomadik.c
15111 views
/*1* Copyright (C) 2009 ST-Ericsson SA2* Copyright (C) 2009 STMicroelectronics3*4* I2C master mode controller driver, used in Nomadik 88155* and Ux500 platforms.6*7* Author: Srinidhi Kasagar <[email protected]>8* Author: Sachin Verma <[email protected]>9*10* This program is free software; you can redistribute it and/or modify11* it under the terms of the GNU General Public License version 2, as12* published by the Free Software Foundation.13*/14#include <linux/init.h>15#include <linux/module.h>16#include <linux/platform_device.h>17#include <linux/slab.h>18#include <linux/interrupt.h>19#include <linux/i2c.h>20#include <linux/err.h>21#include <linux/clk.h>22#include <linux/io.h>23#include <linux/regulator/consumer.h>24#include <linux/pm_runtime.h>2526#include <plat/i2c.h>2728#define DRIVER_NAME "nmk-i2c"2930/* I2C Controller register offsets */31#define I2C_CR (0x000)32#define I2C_SCR (0x004)33#define I2C_HSMCR (0x008)34#define I2C_MCR (0x00C)35#define I2C_TFR (0x010)36#define I2C_SR (0x014)37#define I2C_RFR (0x018)38#define I2C_TFTR (0x01C)39#define I2C_RFTR (0x020)40#define I2C_DMAR (0x024)41#define I2C_BRCR (0x028)42#define I2C_IMSCR (0x02C)43#define I2C_RISR (0x030)44#define I2C_MISR (0x034)45#define I2C_ICR (0x038)4647/* Control registers */48#define I2C_CR_PE (0x1 << 0) /* Peripheral Enable */49#define I2C_CR_OM (0x3 << 1) /* Operating mode */50#define I2C_CR_SAM (0x1 << 3) /* Slave addressing mode */51#define I2C_CR_SM (0x3 << 4) /* Speed mode */52#define I2C_CR_SGCM (0x1 << 6) /* Slave general call mode */53#define I2C_CR_FTX (0x1 << 7) /* Flush Transmit */54#define I2C_CR_FRX (0x1 << 8) /* Flush Receive */55#define I2C_CR_DMA_TX_EN (0x1 << 9) /* DMA Tx enable */56#define I2C_CR_DMA_RX_EN (0x1 << 10) /* DMA Rx Enable */57#define I2C_CR_DMA_SLE (0x1 << 11) /* DMA sync. logic enable */58#define I2C_CR_LM (0x1 << 12) /* Loopback mode */59#define I2C_CR_FON (0x3 << 13) /* Filtering on */60#define I2C_CR_FS (0x3 << 15) /* Force stop enable */6162/* Master controller (MCR) register */63#define I2C_MCR_OP (0x1 << 0) /* Operation */64#define I2C_MCR_A7 (0x7f << 1) /* 7-bit address */65#define I2C_MCR_EA10 (0x7 << 8) /* 10-bit Extended address */66#define I2C_MCR_SB (0x1 << 11) /* Extended address */67#define I2C_MCR_AM (0x3 << 12) /* Address type */68#define I2C_MCR_STOP (0x1 << 14) /* Stop condition */69#define I2C_MCR_LENGTH (0x7ff << 15) /* Transaction length */7071/* Status register (SR) */72#define I2C_SR_OP (0x3 << 0) /* Operation */73#define I2C_SR_STATUS (0x3 << 2) /* controller status */74#define I2C_SR_CAUSE (0x7 << 4) /* Abort cause */75#define I2C_SR_TYPE (0x3 << 7) /* Receive type */76#define I2C_SR_LENGTH (0x7ff << 9) /* Transfer length */7778/* Interrupt mask set/clear (IMSCR) bits */79#define I2C_IT_TXFE (0x1 << 0)80#define I2C_IT_TXFNE (0x1 << 1)81#define I2C_IT_TXFF (0x1 << 2)82#define I2C_IT_TXFOVR (0x1 << 3)83#define I2C_IT_RXFE (0x1 << 4)84#define I2C_IT_RXFNF (0x1 << 5)85#define I2C_IT_RXFF (0x1 << 6)86#define I2C_IT_RFSR (0x1 << 16)87#define I2C_IT_RFSE (0x1 << 17)88#define I2C_IT_WTSR (0x1 << 18)89#define I2C_IT_MTD (0x1 << 19)90#define I2C_IT_STD (0x1 << 20)91#define I2C_IT_MAL (0x1 << 24)92#define I2C_IT_BERR (0x1 << 25)93#define I2C_IT_MTDWS (0x1 << 28)9495#define GEN_MASK(val, mask, sb) (((val) << (sb)) & (mask))9697/* some bits in ICR are reserved */98#define I2C_CLEAR_ALL_INTS 0x131f007f99100/* first three msb bits are reserved */101#define IRQ_MASK(mask) (mask & 0x1fffffff)102103/* maximum threshold value */104#define MAX_I2C_FIFO_THRESHOLD 15105106enum i2c_status {107I2C_NOP,108I2C_ON_GOING,109I2C_OK,110I2C_ABORT111};112113/* operation */114enum i2c_operation {115I2C_NO_OPERATION = 0xff,116I2C_WRITE = 0x00,117I2C_READ = 0x01118};119120/**121* struct i2c_nmk_client - client specific data122* @slave_adr: 7-bit slave address123* @count: no. bytes to be transferred124* @buffer: client data buffer125* @xfer_bytes: bytes transferred till now126* @operation: current I2C operation127*/128struct i2c_nmk_client {129unsigned short slave_adr;130unsigned long count;131unsigned char *buffer;132unsigned long xfer_bytes;133enum i2c_operation operation;134};135136/**137* struct nmk_i2c_dev - private data structure of the controller138* @pdev: parent platform device139* @adap: corresponding I2C adapter140* @irq: interrupt line for the controller141* @virtbase: virtual io memory area142* @clk: hardware i2c block clock143* @cfg: machine provided controller configuration144* @cli: holder of client specific data145* @stop: stop condition146* @xfer_complete: acknowledge completion for a I2C message147* @result: controller propogated result148* @busy: Busy doing transfer149*/150struct nmk_i2c_dev {151struct platform_device *pdev;152struct i2c_adapter adap;153int irq;154void __iomem *virtbase;155struct clk *clk;156struct nmk_i2c_controller cfg;157struct i2c_nmk_client cli;158int stop;159struct completion xfer_complete;160int result;161struct regulator *regulator;162bool busy;163};164165/* controller's abort causes */166static const char *abort_causes[] = {167"no ack received after address transmission",168"no ack received during data phase",169"ack received after xmission of master code",170"master lost arbitration",171"slave restarts",172"slave reset",173"overflow, maxsize is 2047 bytes",174};175176static inline void i2c_set_bit(void __iomem *reg, u32 mask)177{178writel(readl(reg) | mask, reg);179}180181static inline void i2c_clr_bit(void __iomem *reg, u32 mask)182{183writel(readl(reg) & ~mask, reg);184}185186/**187* flush_i2c_fifo() - This function flushes the I2C FIFO188* @dev: private data of I2C Driver189*190* This function flushes the I2C Tx and Rx FIFOs. It returns191* 0 on successful flushing of FIFO192*/193static int flush_i2c_fifo(struct nmk_i2c_dev *dev)194{195#define LOOP_ATTEMPTS 10196int i;197unsigned long timeout;198199/*200* flush the transmit and receive FIFO. The flushing201* operation takes several cycles before to be completed.202* On the completion, the I2C internal logic clears these203* bits, until then no one must access Tx, Rx FIFO and204* should poll on these bits waiting for the completion.205*/206writel((I2C_CR_FTX | I2C_CR_FRX), dev->virtbase + I2C_CR);207208for (i = 0; i < LOOP_ATTEMPTS; i++) {209timeout = jiffies + dev->adap.timeout;210211while (!time_after(jiffies, timeout)) {212if ((readl(dev->virtbase + I2C_CR) &213(I2C_CR_FTX | I2C_CR_FRX)) == 0)214return 0;215}216}217218dev_err(&dev->pdev->dev, "flushing operation timed out "219"giving up after %d attempts", LOOP_ATTEMPTS);220221return -ETIMEDOUT;222}223224/**225* disable_all_interrupts() - Disable all interrupts of this I2c Bus226* @dev: private data of I2C Driver227*/228static void disable_all_interrupts(struct nmk_i2c_dev *dev)229{230u32 mask = IRQ_MASK(0);231writel(mask, dev->virtbase + I2C_IMSCR);232}233234/**235* clear_all_interrupts() - Clear all interrupts of I2C Controller236* @dev: private data of I2C Driver237*/238static void clear_all_interrupts(struct nmk_i2c_dev *dev)239{240u32 mask;241mask = IRQ_MASK(I2C_CLEAR_ALL_INTS);242writel(mask, dev->virtbase + I2C_ICR);243}244245/**246* init_hw() - initialize the I2C hardware247* @dev: private data of I2C Driver248*/249static int init_hw(struct nmk_i2c_dev *dev)250{251int stat;252253stat = flush_i2c_fifo(dev);254if (stat)255goto exit;256257/* disable the controller */258i2c_clr_bit(dev->virtbase + I2C_CR , I2C_CR_PE);259260disable_all_interrupts(dev);261262clear_all_interrupts(dev);263264dev->cli.operation = I2C_NO_OPERATION;265266exit:267return stat;268}269270/* enable peripheral, master mode operation */271#define DEFAULT_I2C_REG_CR ((1 << 1) | I2C_CR_PE)272273/**274* load_i2c_mcr_reg() - load the MCR register275* @dev: private data of controller276*/277static u32 load_i2c_mcr_reg(struct nmk_i2c_dev *dev)278{279u32 mcr = 0;280281/* 7-bit address transaction */282mcr |= GEN_MASK(1, I2C_MCR_AM, 12);283mcr |= GEN_MASK(dev->cli.slave_adr, I2C_MCR_A7, 1);284285/* start byte procedure not applied */286mcr |= GEN_MASK(0, I2C_MCR_SB, 11);287288/* check the operation, master read/write? */289if (dev->cli.operation == I2C_WRITE)290mcr |= GEN_MASK(I2C_WRITE, I2C_MCR_OP, 0);291else292mcr |= GEN_MASK(I2C_READ, I2C_MCR_OP, 0);293294/* stop or repeated start? */295if (dev->stop)296mcr |= GEN_MASK(1, I2C_MCR_STOP, 14);297else298mcr &= ~(GEN_MASK(1, I2C_MCR_STOP, 14));299300mcr |= GEN_MASK(dev->cli.count, I2C_MCR_LENGTH, 15);301302return mcr;303}304305/**306* setup_i2c_controller() - setup the controller307* @dev: private data of controller308*/309static void setup_i2c_controller(struct nmk_i2c_dev *dev)310{311u32 brcr1, brcr2;312u32 i2c_clk, div;313314writel(0x0, dev->virtbase + I2C_CR);315writel(0x0, dev->virtbase + I2C_HSMCR);316writel(0x0, dev->virtbase + I2C_TFTR);317writel(0x0, dev->virtbase + I2C_RFTR);318writel(0x0, dev->virtbase + I2C_DMAR);319320/*321* set the slsu:322*323* slsu defines the data setup time after SCL clock324* stretching in terms of i2c clk cycles. The325* needed setup time for the three modes are 250ns,326* 100ns, 10ns respectively thus leading to the values327* of 14, 6, 2 for a 48 MHz i2c clk.328*/329writel(dev->cfg.slsu << 16, dev->virtbase + I2C_SCR);330331i2c_clk = clk_get_rate(dev->clk);332333/* fallback to std. mode if machine has not provided it */334if (dev->cfg.clk_freq == 0)335dev->cfg.clk_freq = 100000;336337/*338* The spec says, in case of std. mode the divider is339* 2 whereas it is 3 for fast and fastplus mode of340* operation. TODO - high speed support.341*/342div = (dev->cfg.clk_freq > 100000) ? 3 : 2;343344/*345* generate the mask for baud rate counters. The controller346* has two baud rate counters. One is used for High speed347* operation, and the other is for std, fast mode, fast mode348* plus operation. Currently we do not supprt high speed mode349* so set brcr1 to 0.350*/351brcr1 = 0 << 16;352brcr2 = (i2c_clk/(dev->cfg.clk_freq * div)) & 0xffff;353354/* set the baud rate counter register */355writel((brcr1 | brcr2), dev->virtbase + I2C_BRCR);356357/*358* set the speed mode. Currently we support359* only standard and fast mode of operation360* TODO - support for fast mode plus (up to 1Mb/s)361* and high speed (up to 3.4 Mb/s)362*/363if (dev->cfg.sm > I2C_FREQ_MODE_FAST) {364dev_err(&dev->pdev->dev, "do not support this mode "365"defaulting to std. mode\n");366brcr2 = i2c_clk/(100000 * 2) & 0xffff;367writel((brcr1 | brcr2), dev->virtbase + I2C_BRCR);368writel(I2C_FREQ_MODE_STANDARD << 4,369dev->virtbase + I2C_CR);370}371writel(dev->cfg.sm << 4, dev->virtbase + I2C_CR);372373/* set the Tx and Rx FIFO threshold */374writel(dev->cfg.tft, dev->virtbase + I2C_TFTR);375writel(dev->cfg.rft, dev->virtbase + I2C_RFTR);376}377378/**379* read_i2c() - Read from I2C client device380* @dev: private data of I2C Driver381*382* This function reads from i2c client device when controller is in383* master mode. There is a completion timeout. If there is no transfer384* before timeout error is returned.385*/386static int read_i2c(struct nmk_i2c_dev *dev)387{388u32 status = 0;389u32 mcr;390u32 irq_mask = 0;391int timeout;392393mcr = load_i2c_mcr_reg(dev);394writel(mcr, dev->virtbase + I2C_MCR);395396/* load the current CR value */397writel(readl(dev->virtbase + I2C_CR) | DEFAULT_I2C_REG_CR,398dev->virtbase + I2C_CR);399400/* enable the controller */401i2c_set_bit(dev->virtbase + I2C_CR, I2C_CR_PE);402403init_completion(&dev->xfer_complete);404405/* enable interrupts by setting the mask */406irq_mask = (I2C_IT_RXFNF | I2C_IT_RXFF |407I2C_IT_MAL | I2C_IT_BERR);408409if (dev->stop)410irq_mask |= I2C_IT_MTD;411else412irq_mask |= I2C_IT_MTDWS;413414irq_mask = I2C_CLEAR_ALL_INTS & IRQ_MASK(irq_mask);415416writel(readl(dev->virtbase + I2C_IMSCR) | irq_mask,417dev->virtbase + I2C_IMSCR);418419timeout = wait_for_completion_interruptible_timeout(420&dev->xfer_complete, dev->adap.timeout);421422if (timeout < 0) {423dev_err(&dev->pdev->dev,424"wait_for_completion_interruptible_timeout"425"returned %d waiting for event\n", timeout);426status = timeout;427}428429if (timeout == 0) {430/* Controller timed out */431dev_err(&dev->pdev->dev, "read from slave 0x%x timed out\n",432dev->cli.slave_adr);433status = -ETIMEDOUT;434}435return status;436}437438static void fill_tx_fifo(struct nmk_i2c_dev *dev, int no_bytes)439{440int count;441442for (count = (no_bytes - 2);443(count > 0) &&444(dev->cli.count != 0);445count--) {446/* write to the Tx FIFO */447writeb(*dev->cli.buffer,448dev->virtbase + I2C_TFR);449dev->cli.buffer++;450dev->cli.count--;451dev->cli.xfer_bytes++;452}453454}455456/**457* write_i2c() - Write data to I2C client.458* @dev: private data of I2C Driver459*460* This function writes data to I2C client461*/462static int write_i2c(struct nmk_i2c_dev *dev)463{464u32 status = 0;465u32 mcr;466u32 irq_mask = 0;467int timeout;468469mcr = load_i2c_mcr_reg(dev);470471writel(mcr, dev->virtbase + I2C_MCR);472473/* load the current CR value */474writel(readl(dev->virtbase + I2C_CR) | DEFAULT_I2C_REG_CR,475dev->virtbase + I2C_CR);476477/* enable the controller */478i2c_set_bit(dev->virtbase + I2C_CR , I2C_CR_PE);479480init_completion(&dev->xfer_complete);481482/* enable interrupts by settings the masks */483irq_mask = (I2C_IT_TXFOVR | I2C_IT_MAL | I2C_IT_BERR);484485/* Fill the TX FIFO with transmit data */486fill_tx_fifo(dev, MAX_I2C_FIFO_THRESHOLD);487488if (dev->cli.count != 0)489irq_mask |= I2C_IT_TXFNE;490491/*492* check if we want to transfer a single or multiple bytes, if so493* set the MTDWS bit (Master Transaction Done Without Stop)494* to start repeated start operation495*/496if (dev->stop)497irq_mask |= I2C_IT_MTD;498else499irq_mask |= I2C_IT_MTDWS;500501irq_mask = I2C_CLEAR_ALL_INTS & IRQ_MASK(irq_mask);502503writel(readl(dev->virtbase + I2C_IMSCR) | irq_mask,504dev->virtbase + I2C_IMSCR);505506timeout = wait_for_completion_interruptible_timeout(507&dev->xfer_complete, dev->adap.timeout);508509if (timeout < 0) {510dev_err(&dev->pdev->dev,511"wait_for_completion_interruptible_timeout"512"returned %d waiting for event\n", timeout);513status = timeout;514}515516if (timeout == 0) {517/* Controller timed out */518dev_err(&dev->pdev->dev, "write to slave 0x%x timed out\n",519dev->cli.slave_adr);520status = -ETIMEDOUT;521}522523return status;524}525526/**527* nmk_i2c_xfer_one() - transmit a single I2C message528* @dev: device with a message encoded into it529* @flags: message flags530*/531static int nmk_i2c_xfer_one(struct nmk_i2c_dev *dev, u16 flags)532{533int status;534535if (flags & I2C_M_RD) {536/* read operation */537dev->cli.operation = I2C_READ;538status = read_i2c(dev);539} else {540/* write operation */541dev->cli.operation = I2C_WRITE;542status = write_i2c(dev);543}544545if (status || (dev->result)) {546u32 i2c_sr;547u32 cause;548549i2c_sr = readl(dev->virtbase + I2C_SR);550/*551* Check if the controller I2C operation status552* is set to ABORT(11b).553*/554if (((i2c_sr >> 2) & 0x3) == 0x3) {555/* get the abort cause */556cause = (i2c_sr >> 4) & 0x7;557dev_err(&dev->pdev->dev, "%s\n", cause558>= ARRAY_SIZE(abort_causes) ?559"unknown reason" :560abort_causes[cause]);561}562563(void) init_hw(dev);564565status = status ? status : dev->result;566}567568return status;569}570571/**572* nmk_i2c_xfer() - I2C transfer function used by kernel framework573* @i2c_adap: Adapter pointer to the controller574* @msgs: Pointer to data to be written.575* @num_msgs: Number of messages to be executed576*577* This is the function called by the generic kernel i2c_transfer()578* or i2c_smbus...() API calls. Note that this code is protected by the579* semaphore set in the kernel i2c_transfer() function.580*581* NOTE:582* READ TRANSFER : We impose a restriction of the first message to be the583* index message for any read transaction.584* - a no index is coded as '0',585* - 2byte big endian index is coded as '3'586* !!! msg[0].buf holds the actual index.587* This is compatible with generic messages of smbus emulator588* that send a one byte index.589* eg. a I2C transation to read 2 bytes from index 0590* idx = 0;591* msg[0].addr = client->addr;592* msg[0].flags = 0x0;593* msg[0].len = 1;594* msg[0].buf = &idx;595*596* msg[1].addr = client->addr;597* msg[1].flags = I2C_M_RD;598* msg[1].len = 2;599* msg[1].buf = rd_buff600* i2c_transfer(adap, msg, 2);601*602* WRITE TRANSFER : The I2C standard interface interprets all data as payload.603* If you want to emulate an SMBUS write transaction put the604* index as first byte(or first and second) in the payload.605* eg. a I2C transation to write 2 bytes from index 1606* wr_buff[0] = 0x1;607* wr_buff[1] = 0x23;608* wr_buff[2] = 0x46;609* msg[0].flags = 0x0;610* msg[0].len = 3;611* msg[0].buf = wr_buff;612* i2c_transfer(adap, msg, 1);613*614* To read or write a block of data (multiple bytes) using SMBUS emulation615* please use the i2c_smbus_read_i2c_block_data()616* or i2c_smbus_write_i2c_block_data() API617*/618static int nmk_i2c_xfer(struct i2c_adapter *i2c_adap,619struct i2c_msg msgs[], int num_msgs)620{621int status;622int i;623struct nmk_i2c_dev *dev = i2c_get_adapdata(i2c_adap);624int j;625626dev->busy = true;627628if (dev->regulator)629regulator_enable(dev->regulator);630pm_runtime_get_sync(&dev->pdev->dev);631632clk_enable(dev->clk);633634status = init_hw(dev);635if (status)636goto out;637638/* Attempt three times to send the message queue */639for (j = 0; j < 3; j++) {640/* setup the i2c controller */641setup_i2c_controller(dev);642643for (i = 0; i < num_msgs; i++) {644if (unlikely(msgs[i].flags & I2C_M_TEN)) {645dev_err(&dev->pdev->dev, "10 bit addressing"646"not supported\n");647648status = -EINVAL;649goto out;650}651dev->cli.slave_adr = msgs[i].addr;652dev->cli.buffer = msgs[i].buf;653dev->cli.count = msgs[i].len;654dev->stop = (i < (num_msgs - 1)) ? 0 : 1;655dev->result = 0;656657status = nmk_i2c_xfer_one(dev, msgs[i].flags);658if (status != 0)659break;660}661if (status == 0)662break;663}664665out:666clk_disable(dev->clk);667pm_runtime_put_sync(&dev->pdev->dev);668if (dev->regulator)669regulator_disable(dev->regulator);670671dev->busy = false;672673/* return the no. messages processed */674if (status)675return status;676else677return num_msgs;678}679680/**681* disable_interrupts() - disable the interrupts682* @dev: private data of controller683* @irq: interrupt number684*/685static int disable_interrupts(struct nmk_i2c_dev *dev, u32 irq)686{687irq = IRQ_MASK(irq);688writel(readl(dev->virtbase + I2C_IMSCR) & ~(I2C_CLEAR_ALL_INTS & irq),689dev->virtbase + I2C_IMSCR);690return 0;691}692693/**694* i2c_irq_handler() - interrupt routine695* @irq: interrupt number696* @arg: data passed to the handler697*698* This is the interrupt handler for the i2c driver. Currently699* it handles the major interrupts like Rx & Tx FIFO management700* interrupts, master transaction interrupts, arbitration and701* bus error interrupts. The rest of the interrupts are treated as702* unhandled.703*/704static irqreturn_t i2c_irq_handler(int irq, void *arg)705{706struct nmk_i2c_dev *dev = arg;707u32 tft, rft;708u32 count;709u32 misr;710u32 src = 0;711712/* load Tx FIFO and Rx FIFO threshold values */713tft = readl(dev->virtbase + I2C_TFTR);714rft = readl(dev->virtbase + I2C_RFTR);715716/* read interrupt status register */717misr = readl(dev->virtbase + I2C_MISR);718719src = __ffs(misr);720switch ((1 << src)) {721722/* Transmit FIFO nearly empty interrupt */723case I2C_IT_TXFNE:724{725if (dev->cli.operation == I2C_READ) {726/*727* in read operation why do we care for writing?728* so disable the Transmit FIFO interrupt729*/730disable_interrupts(dev, I2C_IT_TXFNE);731} else {732fill_tx_fifo(dev, (MAX_I2C_FIFO_THRESHOLD - tft));733/*734* if done, close the transfer by disabling the735* corresponding TXFNE interrupt736*/737if (dev->cli.count == 0)738disable_interrupts(dev, I2C_IT_TXFNE);739}740}741break;742743/*744* Rx FIFO nearly full interrupt.745* This is set when the numer of entries in Rx FIFO is746* greater or equal than the threshold value programmed747* in RFT748*/749case I2C_IT_RXFNF:750for (count = rft; count > 0; count--) {751/* Read the Rx FIFO */752*dev->cli.buffer = readb(dev->virtbase + I2C_RFR);753dev->cli.buffer++;754}755dev->cli.count -= rft;756dev->cli.xfer_bytes += rft;757break;758759/* Rx FIFO full */760case I2C_IT_RXFF:761for (count = MAX_I2C_FIFO_THRESHOLD; count > 0; count--) {762*dev->cli.buffer = readb(dev->virtbase + I2C_RFR);763dev->cli.buffer++;764}765dev->cli.count -= MAX_I2C_FIFO_THRESHOLD;766dev->cli.xfer_bytes += MAX_I2C_FIFO_THRESHOLD;767break;768769/* Master Transaction Done with/without stop */770case I2C_IT_MTD:771case I2C_IT_MTDWS:772if (dev->cli.operation == I2C_READ) {773while (!(readl(dev->virtbase + I2C_RISR)774& I2C_IT_RXFE)) {775if (dev->cli.count == 0)776break;777*dev->cli.buffer =778readb(dev->virtbase + I2C_RFR);779dev->cli.buffer++;780dev->cli.count--;781dev->cli.xfer_bytes++;782}783}784785disable_all_interrupts(dev);786clear_all_interrupts(dev);787788if (dev->cli.count) {789dev->result = -EIO;790dev_err(&dev->pdev->dev, "%lu bytes still remain to be"791"xfered\n", dev->cli.count);792(void) init_hw(dev);793}794complete(&dev->xfer_complete);795796break;797798/* Master Arbitration lost interrupt */799case I2C_IT_MAL:800dev->result = -EIO;801(void) init_hw(dev);802803i2c_set_bit(dev->virtbase + I2C_ICR, I2C_IT_MAL);804complete(&dev->xfer_complete);805806break;807808/*809* Bus Error interrupt.810* This happens when an unexpected start/stop condition occurs811* during the transaction.812*/813case I2C_IT_BERR:814dev->result = -EIO;815/* get the status */816if (((readl(dev->virtbase + I2C_SR) >> 2) & 0x3) == I2C_ABORT)817(void) init_hw(dev);818819i2c_set_bit(dev->virtbase + I2C_ICR, I2C_IT_BERR);820complete(&dev->xfer_complete);821822break;823824/*825* Tx FIFO overrun interrupt.826* This is set when a write operation in Tx FIFO is performed and827* the Tx FIFO is full.828*/829case I2C_IT_TXFOVR:830dev->result = -EIO;831(void) init_hw(dev);832833dev_err(&dev->pdev->dev, "Tx Fifo Over run\n");834complete(&dev->xfer_complete);835836break;837838/* unhandled interrupts by this driver - TODO*/839case I2C_IT_TXFE:840case I2C_IT_TXFF:841case I2C_IT_RXFE:842case I2C_IT_RFSR:843case I2C_IT_RFSE:844case I2C_IT_WTSR:845case I2C_IT_STD:846dev_err(&dev->pdev->dev, "unhandled Interrupt\n");847break;848default:849dev_err(&dev->pdev->dev, "spurious Interrupt..\n");850break;851}852853return IRQ_HANDLED;854}855856857#ifdef CONFIG_PM858static int nmk_i2c_suspend(struct device *dev)859{860struct platform_device *pdev = to_platform_device(dev);861struct nmk_i2c_dev *nmk_i2c = platform_get_drvdata(pdev);862863if (nmk_i2c->busy)864return -EBUSY;865866return 0;867}868869static int nmk_i2c_resume(struct device *dev)870{871return 0;872}873#else874#define nmk_i2c_suspend NULL875#define nmk_i2c_resume NULL876#endif877878/*879* We use noirq so that we suspend late and resume before the wakeup interrupt880* to ensure that we do the !pm_runtime_suspended() check in resume before881* there has been a regular pm runtime resume (via pm_runtime_get_sync()).882*/883static const struct dev_pm_ops nmk_i2c_pm = {884.suspend_noirq = nmk_i2c_suspend,885.resume_noirq = nmk_i2c_resume,886};887888static unsigned int nmk_i2c_functionality(struct i2c_adapter *adap)889{890return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;891}892893static const struct i2c_algorithm nmk_i2c_algo = {894.master_xfer = nmk_i2c_xfer,895.functionality = nmk_i2c_functionality896};897898static int __devinit nmk_i2c_probe(struct platform_device *pdev)899{900int ret = 0;901struct resource *res;902struct nmk_i2c_controller *pdata =903pdev->dev.platform_data;904struct nmk_i2c_dev *dev;905struct i2c_adapter *adap;906907dev = kzalloc(sizeof(struct nmk_i2c_dev), GFP_KERNEL);908if (!dev) {909dev_err(&pdev->dev, "cannot allocate memory\n");910ret = -ENOMEM;911goto err_no_mem;912}913dev->busy = false;914dev->pdev = pdev;915platform_set_drvdata(pdev, dev);916917res = platform_get_resource(pdev, IORESOURCE_MEM, 0);918if (!res) {919ret = -ENOENT;920goto err_no_resource;921}922923if (request_mem_region(res->start, resource_size(res),924DRIVER_NAME "I/O region") == NULL) {925ret = -EBUSY;926goto err_no_region;927}928929dev->virtbase = ioremap(res->start, resource_size(res));930if (!dev->virtbase) {931ret = -ENOMEM;932goto err_no_ioremap;933}934935dev->irq = platform_get_irq(pdev, 0);936ret = request_irq(dev->irq, i2c_irq_handler, IRQF_DISABLED,937DRIVER_NAME, dev);938if (ret) {939dev_err(&pdev->dev, "cannot claim the irq %d\n", dev->irq);940goto err_irq;941}942943dev->regulator = regulator_get(&pdev->dev, "v-i2c");944if (IS_ERR(dev->regulator)) {945dev_warn(&pdev->dev, "could not get i2c regulator\n");946dev->regulator = NULL;947}948949pm_suspend_ignore_children(&pdev->dev, true);950pm_runtime_enable(&pdev->dev);951952dev->clk = clk_get(&pdev->dev, NULL);953if (IS_ERR(dev->clk)) {954dev_err(&pdev->dev, "could not get i2c clock\n");955ret = PTR_ERR(dev->clk);956goto err_no_clk;957}958959adap = &dev->adap;960adap->dev.parent = &pdev->dev;961adap->owner = THIS_MODULE;962adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;963adap->algo = &nmk_i2c_algo;964adap->timeout = pdata->timeout ? msecs_to_jiffies(pdata->timeout) :965msecs_to_jiffies(20000);966snprintf(adap->name, sizeof(adap->name),967"Nomadik I2C%d at %lx", pdev->id, (unsigned long)res->start);968969/* fetch the controller id */970adap->nr = pdev->id;971972/* fetch the controller configuration from machine */973dev->cfg.clk_freq = pdata->clk_freq;974dev->cfg.slsu = pdata->slsu;975dev->cfg.tft = pdata->tft;976dev->cfg.rft = pdata->rft;977dev->cfg.sm = pdata->sm;978979i2c_set_adapdata(adap, dev);980981dev_info(&pdev->dev, "initialize %s on virtual "982"base %p\n", adap->name, dev->virtbase);983984ret = i2c_add_numbered_adapter(adap);985if (ret) {986dev_err(&pdev->dev, "failed to add adapter\n");987goto err_add_adap;988}989990return 0;991992err_add_adap:993clk_put(dev->clk);994err_no_clk:995if (dev->regulator)996regulator_put(dev->regulator);997pm_runtime_disable(&pdev->dev);998free_irq(dev->irq, dev);999err_irq:1000iounmap(dev->virtbase);1001err_no_ioremap:1002release_mem_region(res->start, resource_size(res));1003err_no_region:1004platform_set_drvdata(pdev, NULL);1005err_no_resource:1006kfree(dev);1007err_no_mem:10081009return ret;1010}10111012static int __devexit nmk_i2c_remove(struct platform_device *pdev)1013{1014struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);1015struct nmk_i2c_dev *dev = platform_get_drvdata(pdev);10161017i2c_del_adapter(&dev->adap);1018flush_i2c_fifo(dev);1019disable_all_interrupts(dev);1020clear_all_interrupts(dev);1021/* disable the controller */1022i2c_clr_bit(dev->virtbase + I2C_CR, I2C_CR_PE);1023free_irq(dev->irq, dev);1024iounmap(dev->virtbase);1025if (res)1026release_mem_region(res->start, resource_size(res));1027clk_put(dev->clk);1028if (dev->regulator)1029regulator_put(dev->regulator);1030pm_runtime_disable(&pdev->dev);1031platform_set_drvdata(pdev, NULL);1032kfree(dev);10331034return 0;1035}10361037static struct platform_driver nmk_i2c_driver = {1038.driver = {1039.owner = THIS_MODULE,1040.name = DRIVER_NAME,1041.pm = &nmk_i2c_pm,1042},1043.probe = nmk_i2c_probe,1044.remove = __devexit_p(nmk_i2c_remove),1045};10461047static int __init nmk_i2c_init(void)1048{1049return platform_driver_register(&nmk_i2c_driver);1050}10511052static void __exit nmk_i2c_exit(void)1053{1054platform_driver_unregister(&nmk_i2c_driver);1055}10561057subsys_initcall(nmk_i2c_init);1058module_exit(nmk_i2c_exit);10591060MODULE_AUTHOR("Sachin Verma, Srinidhi KASAGAR");1061MODULE_DESCRIPTION("Nomadik/Ux500 I2C driver");1062MODULE_LICENSE("GPL");1063MODULE_ALIAS("platform:" DRIVER_NAME);106410651066