Path: blob/master/drivers/i2c/busses/i2c-designware.c
15109 views
/*1* Synopsys DesignWare I2C adapter driver (master only).2*3* Based on the TI DAVINCI I2C adapter driver.4*5* Copyright (C) 2006 Texas Instruments.6* Copyright (C) 2007 MontaVista Software Inc.7* Copyright (C) 2009 Provigent Ltd.8*9* ----------------------------------------------------------------------------10*11* This program is free software; you can redistribute it and/or modify12* it under the terms of the GNU General Public License as published by13* the Free Software Foundation; either version 2 of the License, or14* (at your option) any later version.15*16* This program is distributed in the hope that it will be useful,17* but WITHOUT ANY WARRANTY; without even the implied warranty of18* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the19* GNU General Public License for more details.20*21* You should have received a copy of the GNU General Public License22* along with this program; if not, write to the Free Software23* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.24* ----------------------------------------------------------------------------25*26*/27#include <linux/kernel.h>28#include <linux/module.h>29#include <linux/delay.h>30#include <linux/i2c.h>31#include <linux/clk.h>32#include <linux/errno.h>33#include <linux/sched.h>34#include <linux/err.h>35#include <linux/interrupt.h>36#include <linux/platform_device.h>37#include <linux/io.h>38#include <linux/slab.h>3940/*41* Registers offset42*/43#define DW_IC_CON 0x044#define DW_IC_TAR 0x445#define DW_IC_DATA_CMD 0x1046#define DW_IC_SS_SCL_HCNT 0x1447#define DW_IC_SS_SCL_LCNT 0x1848#define DW_IC_FS_SCL_HCNT 0x1c49#define DW_IC_FS_SCL_LCNT 0x2050#define DW_IC_INTR_STAT 0x2c51#define DW_IC_INTR_MASK 0x3052#define DW_IC_RAW_INTR_STAT 0x3453#define DW_IC_RX_TL 0x3854#define DW_IC_TX_TL 0x3c55#define DW_IC_CLR_INTR 0x4056#define DW_IC_CLR_RX_UNDER 0x4457#define DW_IC_CLR_RX_OVER 0x4858#define DW_IC_CLR_TX_OVER 0x4c59#define DW_IC_CLR_RD_REQ 0x5060#define DW_IC_CLR_TX_ABRT 0x5461#define DW_IC_CLR_RX_DONE 0x5862#define DW_IC_CLR_ACTIVITY 0x5c63#define DW_IC_CLR_STOP_DET 0x6064#define DW_IC_CLR_START_DET 0x6465#define DW_IC_CLR_GEN_CALL 0x6866#define DW_IC_ENABLE 0x6c67#define DW_IC_STATUS 0x7068#define DW_IC_TXFLR 0x7469#define DW_IC_RXFLR 0x7870#define DW_IC_COMP_PARAM_1 0xf471#define DW_IC_TX_ABRT_SOURCE 0x807273#define DW_IC_CON_MASTER 0x174#define DW_IC_CON_SPEED_STD 0x275#define DW_IC_CON_SPEED_FAST 0x476#define DW_IC_CON_10BITADDR_MASTER 0x1077#define DW_IC_CON_RESTART_EN 0x2078#define DW_IC_CON_SLAVE_DISABLE 0x407980#define DW_IC_INTR_RX_UNDER 0x00181#define DW_IC_INTR_RX_OVER 0x00282#define DW_IC_INTR_RX_FULL 0x00483#define DW_IC_INTR_TX_OVER 0x00884#define DW_IC_INTR_TX_EMPTY 0x01085#define DW_IC_INTR_RD_REQ 0x02086#define DW_IC_INTR_TX_ABRT 0x04087#define DW_IC_INTR_RX_DONE 0x08088#define DW_IC_INTR_ACTIVITY 0x10089#define DW_IC_INTR_STOP_DET 0x20090#define DW_IC_INTR_START_DET 0x40091#define DW_IC_INTR_GEN_CALL 0x8009293#define DW_IC_INTR_DEFAULT_MASK (DW_IC_INTR_RX_FULL | \94DW_IC_INTR_TX_EMPTY | \95DW_IC_INTR_TX_ABRT | \96DW_IC_INTR_STOP_DET)9798#define DW_IC_STATUS_ACTIVITY 0x199100#define DW_IC_ERR_TX_ABRT 0x1101102/*103* status codes104*/105#define STATUS_IDLE 0x0106#define STATUS_WRITE_IN_PROGRESS 0x1107#define STATUS_READ_IN_PROGRESS 0x2108109#define TIMEOUT 20 /* ms */110111/*112* hardware abort codes from the DW_IC_TX_ABRT_SOURCE register113*114* only expected abort codes are listed here115* refer to the datasheet for the full list116*/117#define ABRT_7B_ADDR_NOACK 0118#define ABRT_10ADDR1_NOACK 1119#define ABRT_10ADDR2_NOACK 2120#define ABRT_TXDATA_NOACK 3121#define ABRT_GCALL_NOACK 4122#define ABRT_GCALL_READ 5123#define ABRT_SBYTE_ACKDET 7124#define ABRT_SBYTE_NORSTRT 9125#define ABRT_10B_RD_NORSTRT 10126#define ABRT_MASTER_DIS 11127#define ARB_LOST 12128129#define DW_IC_TX_ABRT_7B_ADDR_NOACK (1UL << ABRT_7B_ADDR_NOACK)130#define DW_IC_TX_ABRT_10ADDR1_NOACK (1UL << ABRT_10ADDR1_NOACK)131#define DW_IC_TX_ABRT_10ADDR2_NOACK (1UL << ABRT_10ADDR2_NOACK)132#define DW_IC_TX_ABRT_TXDATA_NOACK (1UL << ABRT_TXDATA_NOACK)133#define DW_IC_TX_ABRT_GCALL_NOACK (1UL << ABRT_GCALL_NOACK)134#define DW_IC_TX_ABRT_GCALL_READ (1UL << ABRT_GCALL_READ)135#define DW_IC_TX_ABRT_SBYTE_ACKDET (1UL << ABRT_SBYTE_ACKDET)136#define DW_IC_TX_ABRT_SBYTE_NORSTRT (1UL << ABRT_SBYTE_NORSTRT)137#define DW_IC_TX_ABRT_10B_RD_NORSTRT (1UL << ABRT_10B_RD_NORSTRT)138#define DW_IC_TX_ABRT_MASTER_DIS (1UL << ABRT_MASTER_DIS)139#define DW_IC_TX_ARB_LOST (1UL << ARB_LOST)140141#define DW_IC_TX_ABRT_NOACK (DW_IC_TX_ABRT_7B_ADDR_NOACK | \142DW_IC_TX_ABRT_10ADDR1_NOACK | \143DW_IC_TX_ABRT_10ADDR2_NOACK | \144DW_IC_TX_ABRT_TXDATA_NOACK | \145DW_IC_TX_ABRT_GCALL_NOACK)146147static char *abort_sources[] = {148[ABRT_7B_ADDR_NOACK] =149"slave address not acknowledged (7bit mode)",150[ABRT_10ADDR1_NOACK] =151"first address byte not acknowledged (10bit mode)",152[ABRT_10ADDR2_NOACK] =153"second address byte not acknowledged (10bit mode)",154[ABRT_TXDATA_NOACK] =155"data not acknowledged",156[ABRT_GCALL_NOACK] =157"no acknowledgement for a general call",158[ABRT_GCALL_READ] =159"read after general call",160[ABRT_SBYTE_ACKDET] =161"start byte acknowledged",162[ABRT_SBYTE_NORSTRT] =163"trying to send start byte when restart is disabled",164[ABRT_10B_RD_NORSTRT] =165"trying to read when restart is disabled (10bit mode)",166[ABRT_MASTER_DIS] =167"trying to use disabled adapter",168[ARB_LOST] =169"lost arbitration",170};171172/**173* struct dw_i2c_dev - private i2c-designware data174* @dev: driver model device node175* @base: IO registers pointer176* @cmd_complete: tx completion indicator177* @lock: protect this struct and IO registers178* @clk: input reference clock179* @cmd_err: run time hadware error code180* @msgs: points to an array of messages currently being transferred181* @msgs_num: the number of elements in msgs182* @msg_write_idx: the element index of the current tx message in the msgs183* array184* @tx_buf_len: the length of the current tx buffer185* @tx_buf: the current tx buffer186* @msg_read_idx: the element index of the current rx message in the msgs187* array188* @rx_buf_len: the length of the current rx buffer189* @rx_buf: the current rx buffer190* @msg_err: error status of the current transfer191* @status: i2c master status, one of STATUS_*192* @abort_source: copy of the TX_ABRT_SOURCE register193* @irq: interrupt number for the i2c master194* @adapter: i2c subsystem adapter node195* @tx_fifo_depth: depth of the hardware tx fifo196* @rx_fifo_depth: depth of the hardware rx fifo197*/198struct dw_i2c_dev {199struct device *dev;200void __iomem *base;201struct completion cmd_complete;202struct mutex lock;203struct clk *clk;204int cmd_err;205struct i2c_msg *msgs;206int msgs_num;207int msg_write_idx;208u32 tx_buf_len;209u8 *tx_buf;210int msg_read_idx;211u32 rx_buf_len;212u8 *rx_buf;213int msg_err;214unsigned int status;215u32 abort_source;216int irq;217struct i2c_adapter adapter;218unsigned int tx_fifo_depth;219unsigned int rx_fifo_depth;220};221222static u32223i2c_dw_scl_hcnt(u32 ic_clk, u32 tSYMBOL, u32 tf, int cond, int offset)224{225/*226* DesignWare I2C core doesn't seem to have solid strategy to meet227* the tHD;STA timing spec. Configuring _HCNT based on tHIGH spec228* will result in violation of the tHD;STA spec.229*/230if (cond)231/*232* Conditional expression:233*234* IC_[FS]S_SCL_HCNT + (1+4+3) >= IC_CLK * tHIGH235*236* This is based on the DW manuals, and represents an ideal237* configuration. The resulting I2C bus speed will be238* faster than any of the others.239*240* If your hardware is free from tHD;STA issue, try this one.241*/242return (ic_clk * tSYMBOL + 5000) / 10000 - 8 + offset;243else244/*245* Conditional expression:246*247* IC_[FS]S_SCL_HCNT + 3 >= IC_CLK * (tHD;STA + tf)248*249* This is just experimental rule; the tHD;STA period turned250* out to be proportinal to (_HCNT + 3). With this setting,251* we could meet both tHIGH and tHD;STA timing specs.252*253* If unsure, you'd better to take this alternative.254*255* The reason why we need to take into account "tf" here,256* is the same as described in i2c_dw_scl_lcnt().257*/258return (ic_clk * (tSYMBOL + tf) + 5000) / 10000 - 3 + offset;259}260261static u32 i2c_dw_scl_lcnt(u32 ic_clk, u32 tLOW, u32 tf, int offset)262{263/*264* Conditional expression:265*266* IC_[FS]S_SCL_LCNT + 1 >= IC_CLK * (tLOW + tf)267*268* DW I2C core starts counting the SCL CNTs for the LOW period269* of the SCL clock (tLOW) as soon as it pulls the SCL line.270* In order to meet the tLOW timing spec, we need to take into271* account the fall time of SCL signal (tf). Default tf value272* should be 0.3 us, for safety.273*/274return ((ic_clk * (tLOW + tf) + 5000) / 10000) - 1 + offset;275}276277/**278* i2c_dw_init() - initialize the designware i2c master hardware279* @dev: device private data280*281* This functions configures and enables the I2C master.282* This function is called during I2C init function, and in case of timeout at283* run time.284*/285static void i2c_dw_init(struct dw_i2c_dev *dev)286{287u32 input_clock_khz = clk_get_rate(dev->clk) / 1000;288u32 ic_con, hcnt, lcnt;289290/* Disable the adapter */291writel(0, dev->base + DW_IC_ENABLE);292293/* set standard and fast speed deviders for high/low periods */294295/* Standard-mode */296hcnt = i2c_dw_scl_hcnt(input_clock_khz,29740, /* tHD;STA = tHIGH = 4.0 us */2983, /* tf = 0.3 us */2990, /* 0: DW default, 1: Ideal */3000); /* No offset */301lcnt = i2c_dw_scl_lcnt(input_clock_khz,30247, /* tLOW = 4.7 us */3033, /* tf = 0.3 us */3040); /* No offset */305writel(hcnt, dev->base + DW_IC_SS_SCL_HCNT);306writel(lcnt, dev->base + DW_IC_SS_SCL_LCNT);307dev_dbg(dev->dev, "Standard-mode HCNT:LCNT = %d:%d\n", hcnt, lcnt);308309/* Fast-mode */310hcnt = i2c_dw_scl_hcnt(input_clock_khz,3116, /* tHD;STA = tHIGH = 0.6 us */3123, /* tf = 0.3 us */3130, /* 0: DW default, 1: Ideal */3140); /* No offset */315lcnt = i2c_dw_scl_lcnt(input_clock_khz,31613, /* tLOW = 1.3 us */3173, /* tf = 0.3 us */3180); /* No offset */319writel(hcnt, dev->base + DW_IC_FS_SCL_HCNT);320writel(lcnt, dev->base + DW_IC_FS_SCL_LCNT);321dev_dbg(dev->dev, "Fast-mode HCNT:LCNT = %d:%d\n", hcnt, lcnt);322323/* Configure Tx/Rx FIFO threshold levels */324writel(dev->tx_fifo_depth - 1, dev->base + DW_IC_TX_TL);325writel(0, dev->base + DW_IC_RX_TL);326327/* configure the i2c master */328ic_con = DW_IC_CON_MASTER | DW_IC_CON_SLAVE_DISABLE |329DW_IC_CON_RESTART_EN | DW_IC_CON_SPEED_FAST;330writel(ic_con, dev->base + DW_IC_CON);331}332333/*334* Waiting for bus not busy335*/336static int i2c_dw_wait_bus_not_busy(struct dw_i2c_dev *dev)337{338int timeout = TIMEOUT;339340while (readl(dev->base + DW_IC_STATUS) & DW_IC_STATUS_ACTIVITY) {341if (timeout <= 0) {342dev_warn(dev->dev, "timeout waiting for bus ready\n");343return -ETIMEDOUT;344}345timeout--;346mdelay(1);347}348349return 0;350}351352static void i2c_dw_xfer_init(struct dw_i2c_dev *dev)353{354struct i2c_msg *msgs = dev->msgs;355u32 ic_con;356357/* Disable the adapter */358writel(0, dev->base + DW_IC_ENABLE);359360/* set the slave (target) address */361writel(msgs[dev->msg_write_idx].addr, dev->base + DW_IC_TAR);362363/* if the slave address is ten bit address, enable 10BITADDR */364ic_con = readl(dev->base + DW_IC_CON);365if (msgs[dev->msg_write_idx].flags & I2C_M_TEN)366ic_con |= DW_IC_CON_10BITADDR_MASTER;367else368ic_con &= ~DW_IC_CON_10BITADDR_MASTER;369writel(ic_con, dev->base + DW_IC_CON);370371/* Enable the adapter */372writel(1, dev->base + DW_IC_ENABLE);373374/* Enable interrupts */375writel(DW_IC_INTR_DEFAULT_MASK, dev->base + DW_IC_INTR_MASK);376}377378/*379* Initiate (and continue) low level master read/write transaction.380* This function is only called from i2c_dw_isr, and pumping i2c_msg381* messages into the tx buffer. Even if the size of i2c_msg data is382* longer than the size of the tx buffer, it handles everything.383*/384static void385i2c_dw_xfer_msg(struct dw_i2c_dev *dev)386{387struct i2c_msg *msgs = dev->msgs;388u32 intr_mask;389int tx_limit, rx_limit;390u32 addr = msgs[dev->msg_write_idx].addr;391u32 buf_len = dev->tx_buf_len;392u8 *buf = dev->tx_buf;;393394intr_mask = DW_IC_INTR_DEFAULT_MASK;395396for (; dev->msg_write_idx < dev->msgs_num; dev->msg_write_idx++) {397/*398* if target address has changed, we need to399* reprogram the target address in the i2c400* adapter when we are done with this transfer401*/402if (msgs[dev->msg_write_idx].addr != addr) {403dev_err(dev->dev,404"%s: invalid target address\n", __func__);405dev->msg_err = -EINVAL;406break;407}408409if (msgs[dev->msg_write_idx].len == 0) {410dev_err(dev->dev,411"%s: invalid message length\n", __func__);412dev->msg_err = -EINVAL;413break;414}415416if (!(dev->status & STATUS_WRITE_IN_PROGRESS)) {417/* new i2c_msg */418buf = msgs[dev->msg_write_idx].buf;419buf_len = msgs[dev->msg_write_idx].len;420}421422tx_limit = dev->tx_fifo_depth - readl(dev->base + DW_IC_TXFLR);423rx_limit = dev->rx_fifo_depth - readl(dev->base + DW_IC_RXFLR);424425while (buf_len > 0 && tx_limit > 0 && rx_limit > 0) {426if (msgs[dev->msg_write_idx].flags & I2C_M_RD) {427writel(0x100, dev->base + DW_IC_DATA_CMD);428rx_limit--;429} else430writel(*buf++, dev->base + DW_IC_DATA_CMD);431tx_limit--; buf_len--;432}433434dev->tx_buf = buf;435dev->tx_buf_len = buf_len;436437if (buf_len > 0) {438/* more bytes to be written */439dev->status |= STATUS_WRITE_IN_PROGRESS;440break;441} else442dev->status &= ~STATUS_WRITE_IN_PROGRESS;443}444445/*446* If i2c_msg index search is completed, we don't need TX_EMPTY447* interrupt any more.448*/449if (dev->msg_write_idx == dev->msgs_num)450intr_mask &= ~DW_IC_INTR_TX_EMPTY;451452if (dev->msg_err)453intr_mask = 0;454455writel(intr_mask, dev->base + DW_IC_INTR_MASK);456}457458static void459i2c_dw_read(struct dw_i2c_dev *dev)460{461struct i2c_msg *msgs = dev->msgs;462int rx_valid;463464for (; dev->msg_read_idx < dev->msgs_num; dev->msg_read_idx++) {465u32 len;466u8 *buf;467468if (!(msgs[dev->msg_read_idx].flags & I2C_M_RD))469continue;470471if (!(dev->status & STATUS_READ_IN_PROGRESS)) {472len = msgs[dev->msg_read_idx].len;473buf = msgs[dev->msg_read_idx].buf;474} else {475len = dev->rx_buf_len;476buf = dev->rx_buf;477}478479rx_valid = readl(dev->base + DW_IC_RXFLR);480481for (; len > 0 && rx_valid > 0; len--, rx_valid--)482*buf++ = readl(dev->base + DW_IC_DATA_CMD);483484if (len > 0) {485dev->status |= STATUS_READ_IN_PROGRESS;486dev->rx_buf_len = len;487dev->rx_buf = buf;488return;489} else490dev->status &= ~STATUS_READ_IN_PROGRESS;491}492}493494static int i2c_dw_handle_tx_abort(struct dw_i2c_dev *dev)495{496unsigned long abort_source = dev->abort_source;497int i;498499if (abort_source & DW_IC_TX_ABRT_NOACK) {500for_each_set_bit(i, &abort_source, ARRAY_SIZE(abort_sources))501dev_dbg(dev->dev,502"%s: %s\n", __func__, abort_sources[i]);503return -EREMOTEIO;504}505506for_each_set_bit(i, &abort_source, ARRAY_SIZE(abort_sources))507dev_err(dev->dev, "%s: %s\n", __func__, abort_sources[i]);508509if (abort_source & DW_IC_TX_ARB_LOST)510return -EAGAIN;511else if (abort_source & DW_IC_TX_ABRT_GCALL_READ)512return -EINVAL; /* wrong msgs[] data */513else514return -EIO;515}516517/*518* Prepare controller for a transaction and call i2c_dw_xfer_msg519*/520static int521i2c_dw_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)522{523struct dw_i2c_dev *dev = i2c_get_adapdata(adap);524int ret;525526dev_dbg(dev->dev, "%s: msgs: %d\n", __func__, num);527528mutex_lock(&dev->lock);529530INIT_COMPLETION(dev->cmd_complete);531dev->msgs = msgs;532dev->msgs_num = num;533dev->cmd_err = 0;534dev->msg_write_idx = 0;535dev->msg_read_idx = 0;536dev->msg_err = 0;537dev->status = STATUS_IDLE;538dev->abort_source = 0;539540ret = i2c_dw_wait_bus_not_busy(dev);541if (ret < 0)542goto done;543544/* start the transfers */545i2c_dw_xfer_init(dev);546547/* wait for tx to complete */548ret = wait_for_completion_interruptible_timeout(&dev->cmd_complete, HZ);549if (ret == 0) {550dev_err(dev->dev, "controller timed out\n");551i2c_dw_init(dev);552ret = -ETIMEDOUT;553goto done;554} else if (ret < 0)555goto done;556557if (dev->msg_err) {558ret = dev->msg_err;559goto done;560}561562/* no error */563if (likely(!dev->cmd_err)) {564/* Disable the adapter */565writel(0, dev->base + DW_IC_ENABLE);566ret = num;567goto done;568}569570/* We have an error */571if (dev->cmd_err == DW_IC_ERR_TX_ABRT) {572ret = i2c_dw_handle_tx_abort(dev);573goto done;574}575ret = -EIO;576577done:578mutex_unlock(&dev->lock);579580return ret;581}582583static u32 i2c_dw_func(struct i2c_adapter *adap)584{585return I2C_FUNC_I2C |586I2C_FUNC_10BIT_ADDR |587I2C_FUNC_SMBUS_BYTE |588I2C_FUNC_SMBUS_BYTE_DATA |589I2C_FUNC_SMBUS_WORD_DATA |590I2C_FUNC_SMBUS_I2C_BLOCK;591}592593static u32 i2c_dw_read_clear_intrbits(struct dw_i2c_dev *dev)594{595u32 stat;596597/*598* The IC_INTR_STAT register just indicates "enabled" interrupts.599* Ths unmasked raw version of interrupt status bits are available600* in the IC_RAW_INTR_STAT register.601*602* That is,603* stat = readl(IC_INTR_STAT);604* equals to,605* stat = readl(IC_RAW_INTR_STAT) & readl(IC_INTR_MASK);606*607* The raw version might be useful for debugging purposes.608*/609stat = readl(dev->base + DW_IC_INTR_STAT);610611/*612* Do not use the IC_CLR_INTR register to clear interrupts, or613* you'll miss some interrupts, triggered during the period from614* readl(IC_INTR_STAT) to readl(IC_CLR_INTR).615*616* Instead, use the separately-prepared IC_CLR_* registers.617*/618if (stat & DW_IC_INTR_RX_UNDER)619readl(dev->base + DW_IC_CLR_RX_UNDER);620if (stat & DW_IC_INTR_RX_OVER)621readl(dev->base + DW_IC_CLR_RX_OVER);622if (stat & DW_IC_INTR_TX_OVER)623readl(dev->base + DW_IC_CLR_TX_OVER);624if (stat & DW_IC_INTR_RD_REQ)625readl(dev->base + DW_IC_CLR_RD_REQ);626if (stat & DW_IC_INTR_TX_ABRT) {627/*628* The IC_TX_ABRT_SOURCE register is cleared whenever629* the IC_CLR_TX_ABRT is read. Preserve it beforehand.630*/631dev->abort_source = readl(dev->base + DW_IC_TX_ABRT_SOURCE);632readl(dev->base + DW_IC_CLR_TX_ABRT);633}634if (stat & DW_IC_INTR_RX_DONE)635readl(dev->base + DW_IC_CLR_RX_DONE);636if (stat & DW_IC_INTR_ACTIVITY)637readl(dev->base + DW_IC_CLR_ACTIVITY);638if (stat & DW_IC_INTR_STOP_DET)639readl(dev->base + DW_IC_CLR_STOP_DET);640if (stat & DW_IC_INTR_START_DET)641readl(dev->base + DW_IC_CLR_START_DET);642if (stat & DW_IC_INTR_GEN_CALL)643readl(dev->base + DW_IC_CLR_GEN_CALL);644645return stat;646}647648/*649* Interrupt service routine. This gets called whenever an I2C interrupt650* occurs.651*/652static irqreturn_t i2c_dw_isr(int this_irq, void *dev_id)653{654struct dw_i2c_dev *dev = dev_id;655u32 stat;656657stat = i2c_dw_read_clear_intrbits(dev);658dev_dbg(dev->dev, "%s: stat=0x%x\n", __func__, stat);659660if (stat & DW_IC_INTR_TX_ABRT) {661dev->cmd_err |= DW_IC_ERR_TX_ABRT;662dev->status = STATUS_IDLE;663664/*665* Anytime TX_ABRT is set, the contents of the tx/rx666* buffers are flushed. Make sure to skip them.667*/668writel(0, dev->base + DW_IC_INTR_MASK);669goto tx_aborted;670}671672if (stat & DW_IC_INTR_RX_FULL)673i2c_dw_read(dev);674675if (stat & DW_IC_INTR_TX_EMPTY)676i2c_dw_xfer_msg(dev);677678/*679* No need to modify or disable the interrupt mask here.680* i2c_dw_xfer_msg() will take care of it according to681* the current transmit status.682*/683684tx_aborted:685if ((stat & (DW_IC_INTR_TX_ABRT | DW_IC_INTR_STOP_DET)) || dev->msg_err)686complete(&dev->cmd_complete);687688return IRQ_HANDLED;689}690691static struct i2c_algorithm i2c_dw_algo = {692.master_xfer = i2c_dw_xfer,693.functionality = i2c_dw_func,694};695696static int __devinit dw_i2c_probe(struct platform_device *pdev)697{698struct dw_i2c_dev *dev;699struct i2c_adapter *adap;700struct resource *mem, *ioarea;701int irq, r;702703/* NOTE: driver uses the static register mapping */704mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);705if (!mem) {706dev_err(&pdev->dev, "no mem resource?\n");707return -EINVAL;708}709710irq = platform_get_irq(pdev, 0);711if (irq < 0) {712dev_err(&pdev->dev, "no irq resource?\n");713return irq; /* -ENXIO */714}715716ioarea = request_mem_region(mem->start, resource_size(mem),717pdev->name);718if (!ioarea) {719dev_err(&pdev->dev, "I2C region already claimed\n");720return -EBUSY;721}722723dev = kzalloc(sizeof(struct dw_i2c_dev), GFP_KERNEL);724if (!dev) {725r = -ENOMEM;726goto err_release_region;727}728729init_completion(&dev->cmd_complete);730mutex_init(&dev->lock);731dev->dev = get_device(&pdev->dev);732dev->irq = irq;733platform_set_drvdata(pdev, dev);734735dev->clk = clk_get(&pdev->dev, NULL);736if (IS_ERR(dev->clk)) {737r = -ENODEV;738goto err_free_mem;739}740clk_enable(dev->clk);741742dev->base = ioremap(mem->start, resource_size(mem));743if (dev->base == NULL) {744dev_err(&pdev->dev, "failure mapping io resources\n");745r = -EBUSY;746goto err_unuse_clocks;747}748{749u32 param1 = readl(dev->base + DW_IC_COMP_PARAM_1);750751dev->tx_fifo_depth = ((param1 >> 16) & 0xff) + 1;752dev->rx_fifo_depth = ((param1 >> 8) & 0xff) + 1;753}754i2c_dw_init(dev);755756writel(0, dev->base + DW_IC_INTR_MASK); /* disable IRQ */757r = request_irq(dev->irq, i2c_dw_isr, IRQF_DISABLED, pdev->name, dev);758if (r) {759dev_err(&pdev->dev, "failure requesting irq %i\n", dev->irq);760goto err_iounmap;761}762763adap = &dev->adapter;764i2c_set_adapdata(adap, dev);765adap->owner = THIS_MODULE;766adap->class = I2C_CLASS_HWMON;767strlcpy(adap->name, "Synopsys DesignWare I2C adapter",768sizeof(adap->name));769adap->algo = &i2c_dw_algo;770adap->dev.parent = &pdev->dev;771772adap->nr = pdev->id;773r = i2c_add_numbered_adapter(adap);774if (r) {775dev_err(&pdev->dev, "failure adding adapter\n");776goto err_free_irq;777}778779return 0;780781err_free_irq:782free_irq(dev->irq, dev);783err_iounmap:784iounmap(dev->base);785err_unuse_clocks:786clk_disable(dev->clk);787clk_put(dev->clk);788dev->clk = NULL;789err_free_mem:790platform_set_drvdata(pdev, NULL);791put_device(&pdev->dev);792kfree(dev);793err_release_region:794release_mem_region(mem->start, resource_size(mem));795796return r;797}798799static int __devexit dw_i2c_remove(struct platform_device *pdev)800{801struct dw_i2c_dev *dev = platform_get_drvdata(pdev);802struct resource *mem;803804platform_set_drvdata(pdev, NULL);805i2c_del_adapter(&dev->adapter);806put_device(&pdev->dev);807808clk_disable(dev->clk);809clk_put(dev->clk);810dev->clk = NULL;811812writel(0, dev->base + DW_IC_ENABLE);813free_irq(dev->irq, dev);814kfree(dev);815816mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);817release_mem_region(mem->start, resource_size(mem));818return 0;819}820821/* work with hotplug and coldplug */822MODULE_ALIAS("platform:i2c_designware");823824static struct platform_driver dw_i2c_driver = {825.remove = __devexit_p(dw_i2c_remove),826.driver = {827.name = "i2c_designware",828.owner = THIS_MODULE,829},830};831832static int __init dw_i2c_init_driver(void)833{834return platform_driver_probe(&dw_i2c_driver, dw_i2c_probe);835}836module_init(dw_i2c_init_driver);837838static void __exit dw_i2c_exit_driver(void)839{840platform_driver_unregister(&dw_i2c_driver);841}842module_exit(dw_i2c_exit_driver);843844MODULE_AUTHOR("Baruch Siach <[email protected]>");845MODULE_DESCRIPTION("Synopsys DesignWare I2C bus adapter");846MODULE_LICENSE("GPL");847848849