2007-10-14 05:56:30 +08:00
|
|
|
/*
|
|
|
|
* TI DAVINCI I2C adapter driver.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006 Texas Instruments.
|
|
|
|
* Copyright (C) 2007 MontaVista Software Inc.
|
|
|
|
*
|
|
|
|
* Updated by Vinod & Sudhakar Feb 2005
|
|
|
|
*
|
|
|
|
* ----------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
* ----------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/delay.h>
|
|
|
|
#include <linux/i2c.h>
|
|
|
|
#include <linux/clk.h>
|
|
|
|
#include <linux/errno.h>
|
|
|
|
#include <linux/sched.h>
|
|
|
|
#include <linux/err.h>
|
|
|
|
#include <linux/interrupt.h>
|
|
|
|
#include <linux/platform_device.h>
|
|
|
|
#include <linux/io.h>
|
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 16:04:11 +08:00
|
|
|
#include <linux/slab.h>
|
2010-01-06 17:25:00 +08:00
|
|
|
#include <linux/cpufreq.h>
|
2010-01-12 01:09:44 +08:00
|
|
|
#include <linux/gpio.h>
|
2012-07-30 15:21:12 +08:00
|
|
|
#include <linux/of_device.h>
|
2012-08-24 21:11:34 +08:00
|
|
|
#include <linux/platform_data/i2c-davinci.h>
|
2007-10-14 05:56:30 +08:00
|
|
|
|
|
|
|
/* ----- global defines ----------------------------------------------- */
|
|
|
|
|
|
|
|
#define DAVINCI_I2C_TIMEOUT (1*HZ)
|
2010-01-12 01:09:44 +08:00
|
|
|
#define DAVINCI_I2C_MAX_TRIES 2
|
2007-10-14 05:56:30 +08:00
|
|
|
#define I2C_DAVINCI_INTR_ALL (DAVINCI_I2C_IMR_AAS | \
|
|
|
|
DAVINCI_I2C_IMR_SCD | \
|
|
|
|
DAVINCI_I2C_IMR_ARDY | \
|
|
|
|
DAVINCI_I2C_IMR_NACK | \
|
|
|
|
DAVINCI_I2C_IMR_AL)
|
|
|
|
|
|
|
|
#define DAVINCI_I2C_OAR_REG 0x00
|
|
|
|
#define DAVINCI_I2C_IMR_REG 0x04
|
|
|
|
#define DAVINCI_I2C_STR_REG 0x08
|
|
|
|
#define DAVINCI_I2C_CLKL_REG 0x0c
|
|
|
|
#define DAVINCI_I2C_CLKH_REG 0x10
|
|
|
|
#define DAVINCI_I2C_CNT_REG 0x14
|
|
|
|
#define DAVINCI_I2C_DRR_REG 0x18
|
|
|
|
#define DAVINCI_I2C_SAR_REG 0x1c
|
|
|
|
#define DAVINCI_I2C_DXR_REG 0x20
|
|
|
|
#define DAVINCI_I2C_MDR_REG 0x24
|
|
|
|
#define DAVINCI_I2C_IVR_REG 0x28
|
|
|
|
#define DAVINCI_I2C_EMDR_REG 0x2c
|
|
|
|
#define DAVINCI_I2C_PSC_REG 0x30
|
|
|
|
|
|
|
|
#define DAVINCI_I2C_IVR_AAS 0x07
|
|
|
|
#define DAVINCI_I2C_IVR_SCD 0x06
|
|
|
|
#define DAVINCI_I2C_IVR_XRDY 0x05
|
|
|
|
#define DAVINCI_I2C_IVR_RDR 0x04
|
|
|
|
#define DAVINCI_I2C_IVR_ARDY 0x03
|
|
|
|
#define DAVINCI_I2C_IVR_NACK 0x02
|
|
|
|
#define DAVINCI_I2C_IVR_AL 0x01
|
|
|
|
|
2010-01-06 17:24:57 +08:00
|
|
|
#define DAVINCI_I2C_STR_BB BIT(12)
|
|
|
|
#define DAVINCI_I2C_STR_RSFULL BIT(11)
|
|
|
|
#define DAVINCI_I2C_STR_SCD BIT(5)
|
|
|
|
#define DAVINCI_I2C_STR_ARDY BIT(2)
|
|
|
|
#define DAVINCI_I2C_STR_NACK BIT(1)
|
|
|
|
#define DAVINCI_I2C_STR_AL BIT(0)
|
|
|
|
|
|
|
|
#define DAVINCI_I2C_MDR_NACK BIT(15)
|
|
|
|
#define DAVINCI_I2C_MDR_STT BIT(13)
|
|
|
|
#define DAVINCI_I2C_MDR_STP BIT(11)
|
|
|
|
#define DAVINCI_I2C_MDR_MST BIT(10)
|
|
|
|
#define DAVINCI_I2C_MDR_TRX BIT(9)
|
|
|
|
#define DAVINCI_I2C_MDR_XA BIT(8)
|
|
|
|
#define DAVINCI_I2C_MDR_RM BIT(7)
|
|
|
|
#define DAVINCI_I2C_MDR_IRS BIT(5)
|
|
|
|
|
|
|
|
#define DAVINCI_I2C_IMR_AAS BIT(6)
|
|
|
|
#define DAVINCI_I2C_IMR_SCD BIT(5)
|
|
|
|
#define DAVINCI_I2C_IMR_XRDY BIT(4)
|
|
|
|
#define DAVINCI_I2C_IMR_RRDY BIT(3)
|
|
|
|
#define DAVINCI_I2C_IMR_ARDY BIT(2)
|
|
|
|
#define DAVINCI_I2C_IMR_NACK BIT(1)
|
|
|
|
#define DAVINCI_I2C_IMR_AL BIT(0)
|
2007-10-14 05:56:30 +08:00
|
|
|
|
|
|
|
struct davinci_i2c_dev {
|
|
|
|
struct device *dev;
|
|
|
|
void __iomem *base;
|
|
|
|
struct completion cmd_complete;
|
|
|
|
struct clk *clk;
|
|
|
|
int cmd_err;
|
|
|
|
u8 *buf;
|
|
|
|
size_t buf_len;
|
|
|
|
int irq;
|
2008-03-28 13:16:12 +08:00
|
|
|
int stop;
|
2008-07-15 04:38:21 +08:00
|
|
|
u8 terminate;
|
2007-10-14 05:56:30 +08:00
|
|
|
struct i2c_adapter adapter;
|
2010-01-06 17:25:00 +08:00
|
|
|
#ifdef CONFIG_CPU_FREQ
|
|
|
|
struct completion xfr_complete;
|
|
|
|
struct notifier_block freq_transition;
|
|
|
|
#endif
|
2012-07-30 15:21:12 +08:00
|
|
|
struct davinci_i2c_platform_data *pdata;
|
2007-10-14 05:56:30 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/* default platform data to use if not supplied in the platform_device */
|
|
|
|
static struct davinci_i2c_platform_data davinci_i2c_platform_data_default = {
|
|
|
|
.bus_freq = 100,
|
|
|
|
.bus_delay = 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
static inline void davinci_i2c_write_reg(struct davinci_i2c_dev *i2c_dev,
|
|
|
|
int reg, u16 val)
|
|
|
|
{
|
2013-11-21 02:23:44 +08:00
|
|
|
writew_relaxed(val, i2c_dev->base + reg);
|
2007-10-14 05:56:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline u16 davinci_i2c_read_reg(struct davinci_i2c_dev *i2c_dev, int reg)
|
|
|
|
{
|
2013-11-21 02:23:44 +08:00
|
|
|
return readw_relaxed(i2c_dev->base + reg);
|
2007-10-14 05:56:30 +08:00
|
|
|
}
|
|
|
|
|
2010-01-12 01:09:44 +08:00
|
|
|
/* Generate a pulse on the i2c clock pin. */
|
2013-03-29 18:23:41 +08:00
|
|
|
static void davinci_i2c_clock_pulse(unsigned int scl_pin)
|
2010-01-12 01:09:44 +08:00
|
|
|
{
|
|
|
|
u16 i;
|
|
|
|
|
|
|
|
if (scl_pin) {
|
|
|
|
/* Send high and low on the SCL line */
|
|
|
|
for (i = 0; i < 9; i++) {
|
|
|
|
gpio_set_value(scl_pin, 0);
|
|
|
|
udelay(20);
|
|
|
|
gpio_set_value(scl_pin, 1);
|
|
|
|
udelay(20);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This routine does i2c bus recovery as specified in the
|
|
|
|
* i2c protocol Rev. 03 section 3.16 titled "Bus clear"
|
|
|
|
*/
|
2013-03-29 18:23:41 +08:00
|
|
|
static void davinci_i2c_recover_bus(struct davinci_i2c_dev *dev)
|
2010-01-12 01:09:44 +08:00
|
|
|
{
|
|
|
|
u32 flag = 0;
|
2012-07-30 15:21:12 +08:00
|
|
|
struct davinci_i2c_platform_data *pdata = dev->pdata;
|
2010-01-12 01:09:44 +08:00
|
|
|
|
|
|
|
dev_err(dev->dev, "initiating i2c bus recovery\n");
|
|
|
|
/* Send NACK to the slave */
|
|
|
|
flag = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG);
|
|
|
|
flag |= DAVINCI_I2C_MDR_NACK;
|
|
|
|
/* write the data into mode register */
|
|
|
|
davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, flag);
|
2013-03-29 18:23:41 +08:00
|
|
|
davinci_i2c_clock_pulse(pdata->scl_pin);
|
2010-01-12 01:09:44 +08:00
|
|
|
/* Send STOP */
|
|
|
|
flag = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG);
|
|
|
|
flag |= DAVINCI_I2C_MDR_STP;
|
|
|
|
davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, flag);
|
|
|
|
}
|
|
|
|
|
2010-01-06 17:24:58 +08:00
|
|
|
static inline void davinci_i2c_reset_ctrl(struct davinci_i2c_dev *i2c_dev,
|
|
|
|
int val)
|
|
|
|
{
|
|
|
|
u16 w;
|
|
|
|
|
|
|
|
w = davinci_i2c_read_reg(i2c_dev, DAVINCI_I2C_MDR_REG);
|
|
|
|
if (!val) /* put I2C into reset */
|
|
|
|
w &= ~DAVINCI_I2C_MDR_IRS;
|
|
|
|
else /* take I2C out of reset */
|
|
|
|
w |= DAVINCI_I2C_MDR_IRS;
|
|
|
|
|
|
|
|
davinci_i2c_write_reg(i2c_dev, DAVINCI_I2C_MDR_REG, w);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void i2c_davinci_calc_clk_dividers(struct davinci_i2c_dev *dev)
|
2007-10-14 05:56:30 +08:00
|
|
|
{
|
2012-07-30 15:21:12 +08:00
|
|
|
struct davinci_i2c_platform_data *pdata = dev->pdata;
|
2007-10-14 05:56:30 +08:00
|
|
|
u16 psc;
|
|
|
|
u32 clk;
|
2008-07-15 04:38:20 +08:00
|
|
|
u32 d;
|
2007-10-14 05:56:30 +08:00
|
|
|
u32 clkh;
|
|
|
|
u32 clkl;
|
|
|
|
u32 input_clock = clk_get_rate(dev->clk);
|
|
|
|
|
|
|
|
/* NOTE: I2C Clock divider programming info
|
|
|
|
* As per I2C specs the following formulas provide prescaler
|
|
|
|
* and low/high divider values
|
|
|
|
* input clk --> PSC Div -----------> ICCL/H Div --> output clock
|
|
|
|
* module clk
|
|
|
|
*
|
|
|
|
* output clk = module clk / (PSC + 1) [ (ICCL + d) + (ICCH + d) ]
|
|
|
|
*
|
|
|
|
* Thus,
|
|
|
|
* (ICCL + ICCH) = clk = (input clk / ((psc +1) * output clk)) - 2d;
|
|
|
|
*
|
|
|
|
* where if PSC == 0, d = 7,
|
|
|
|
* if PSC == 1, d = 6
|
|
|
|
* if PSC > 1 , d = 5
|
|
|
|
*/
|
|
|
|
|
2008-07-15 04:38:20 +08:00
|
|
|
/* get minimum of 7 MHz clock, but max of 12 MHz */
|
|
|
|
psc = (input_clock / 7000000) - 1;
|
|
|
|
if ((input_clock / (psc + 1)) > 12000000)
|
|
|
|
psc++; /* better to run under spec than over */
|
|
|
|
d = (psc >= 2) ? 5 : 7 - psc;
|
2007-10-14 05:56:30 +08:00
|
|
|
|
2008-07-15 04:38:20 +08:00
|
|
|
clk = ((input_clock / (psc + 1)) / (pdata->bus_freq * 1000)) - (d << 1);
|
|
|
|
clkh = clk >> 1;
|
2007-10-14 05:56:30 +08:00
|
|
|
clkl = clk - clkh;
|
|
|
|
|
|
|
|
davinci_i2c_write_reg(dev, DAVINCI_I2C_PSC_REG, psc);
|
|
|
|
davinci_i2c_write_reg(dev, DAVINCI_I2C_CLKH_REG, clkh);
|
|
|
|
davinci_i2c_write_reg(dev, DAVINCI_I2C_CLKL_REG, clkl);
|
|
|
|
|
2010-01-06 17:24:58 +08:00
|
|
|
dev_dbg(dev->dev, "input_clock = %d, CLK = %d\n", input_clock, clk);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function configures I2C and brings I2C out of reset.
|
|
|
|
* This function is called during I2C init function. This function
|
|
|
|
* also gets called if I2C encounters any errors.
|
|
|
|
*/
|
|
|
|
static int i2c_davinci_init(struct davinci_i2c_dev *dev)
|
|
|
|
{
|
2012-07-30 15:21:12 +08:00
|
|
|
struct davinci_i2c_platform_data *pdata = dev->pdata;
|
2010-01-06 17:24:58 +08:00
|
|
|
|
|
|
|
/* put I2C into reset */
|
|
|
|
davinci_i2c_reset_ctrl(dev, 0);
|
|
|
|
|
|
|
|
/* compute clock dividers */
|
|
|
|
i2c_davinci_calc_clk_dividers(dev);
|
|
|
|
|
2009-07-07 06:48:36 +08:00
|
|
|
/* Respond at reserved "SMBus Host" slave address" (and zero);
|
|
|
|
* we seem to have no option to not respond...
|
|
|
|
*/
|
|
|
|
davinci_i2c_write_reg(dev, DAVINCI_I2C_OAR_REG, 0x08);
|
|
|
|
|
2007-10-14 05:56:30 +08:00
|
|
|
dev_dbg(dev->dev, "PSC = %d\n",
|
|
|
|
davinci_i2c_read_reg(dev, DAVINCI_I2C_PSC_REG));
|
|
|
|
dev_dbg(dev->dev, "CLKL = %d\n",
|
|
|
|
davinci_i2c_read_reg(dev, DAVINCI_I2C_CLKL_REG));
|
|
|
|
dev_dbg(dev->dev, "CLKH = %d\n",
|
|
|
|
davinci_i2c_read_reg(dev, DAVINCI_I2C_CLKH_REG));
|
2008-07-15 04:38:20 +08:00
|
|
|
dev_dbg(dev->dev, "bus_freq = %dkHz, bus_delay = %d\n",
|
|
|
|
pdata->bus_freq, pdata->bus_delay);
|
2007-10-14 05:56:30 +08:00
|
|
|
|
2012-07-30 15:21:12 +08:00
|
|
|
|
2007-10-14 05:56:30 +08:00
|
|
|
/* Take the I2C module out of reset: */
|
2010-01-06 17:24:58 +08:00
|
|
|
davinci_i2c_reset_ctrl(dev, 1);
|
2007-10-14 05:56:30 +08:00
|
|
|
|
|
|
|
/* Enable interrupts */
|
|
|
|
davinci_i2c_write_reg(dev, DAVINCI_I2C_IMR_REG, I2C_DAVINCI_INTR_ALL);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Waiting for bus not busy
|
|
|
|
*/
|
|
|
|
static int i2c_davinci_wait_bus_not_busy(struct davinci_i2c_dev *dev,
|
|
|
|
char allow_sleep)
|
|
|
|
{
|
|
|
|
unsigned long timeout;
|
2010-01-12 01:09:44 +08:00
|
|
|
static u16 to_cnt;
|
2007-10-14 05:56:30 +08:00
|
|
|
|
2009-03-29 04:34:43 +08:00
|
|
|
timeout = jiffies + dev->adapter.timeout;
|
2007-10-14 05:56:30 +08:00
|
|
|
while (davinci_i2c_read_reg(dev, DAVINCI_I2C_STR_REG)
|
|
|
|
& DAVINCI_I2C_STR_BB) {
|
2010-01-12 01:09:44 +08:00
|
|
|
if (to_cnt <= DAVINCI_I2C_MAX_TRIES) {
|
|
|
|
if (time_after(jiffies, timeout)) {
|
|
|
|
dev_warn(dev->dev,
|
|
|
|
"timeout waiting for bus ready\n");
|
|
|
|
to_cnt++;
|
|
|
|
return -ETIMEDOUT;
|
|
|
|
} else {
|
|
|
|
to_cnt = 0;
|
2013-03-29 18:23:41 +08:00
|
|
|
davinci_i2c_recover_bus(dev);
|
2010-01-12 01:09:44 +08:00
|
|
|
i2c_davinci_init(dev);
|
|
|
|
}
|
2007-10-14 05:56:30 +08:00
|
|
|
}
|
|
|
|
if (allow_sleep)
|
|
|
|
schedule_timeout(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Low level master read/write transaction. This function is called
|
|
|
|
* from i2c_davinci_xfer.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
i2c_davinci_xfer_msg(struct i2c_adapter *adap, struct i2c_msg *msg, int stop)
|
|
|
|
{
|
|
|
|
struct davinci_i2c_dev *dev = i2c_get_adapdata(adap);
|
2012-07-30 15:21:12 +08:00
|
|
|
struct davinci_i2c_platform_data *pdata = dev->pdata;
|
2007-10-14 05:56:30 +08:00
|
|
|
u32 flag;
|
|
|
|
u16 w;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
/* Introduce a delay, required for some boards (e.g Davinci EVM) */
|
|
|
|
if (pdata->bus_delay)
|
|
|
|
udelay(pdata->bus_delay);
|
|
|
|
|
|
|
|
/* set the slave address */
|
|
|
|
davinci_i2c_write_reg(dev, DAVINCI_I2C_SAR_REG, msg->addr);
|
|
|
|
|
|
|
|
dev->buf = msg->buf;
|
|
|
|
dev->buf_len = msg->len;
|
2008-03-28 13:16:12 +08:00
|
|
|
dev->stop = stop;
|
2007-10-14 05:56:30 +08:00
|
|
|
|
|
|
|
davinci_i2c_write_reg(dev, DAVINCI_I2C_CNT_REG, dev->buf_len);
|
|
|
|
|
2013-11-15 06:32:02 +08:00
|
|
|
reinit_completion(&dev->cmd_complete);
|
2007-10-14 05:56:30 +08:00
|
|
|
dev->cmd_err = 0;
|
|
|
|
|
2010-10-12 12:47:05 +08:00
|
|
|
/* Take I2C out of reset and configure it as master */
|
|
|
|
flag = DAVINCI_I2C_MDR_IRS | DAVINCI_I2C_MDR_MST;
|
2007-10-14 05:56:30 +08:00
|
|
|
|
|
|
|
/* if the slave address is ten bit address, enable XA bit */
|
|
|
|
if (msg->flags & I2C_M_TEN)
|
|
|
|
flag |= DAVINCI_I2C_MDR_XA;
|
|
|
|
if (!(msg->flags & I2C_M_RD))
|
|
|
|
flag |= DAVINCI_I2C_MDR_TRX;
|
2010-10-12 12:47:05 +08:00
|
|
|
if (msg->len == 0)
|
2008-03-28 13:16:12 +08:00
|
|
|
flag |= DAVINCI_I2C_MDR_RM;
|
2007-10-14 05:56:30 +08:00
|
|
|
|
|
|
|
/* Enable receive or transmit interrupts */
|
|
|
|
w = davinci_i2c_read_reg(dev, DAVINCI_I2C_IMR_REG);
|
|
|
|
if (msg->flags & I2C_M_RD)
|
2010-01-06 17:24:57 +08:00
|
|
|
w |= DAVINCI_I2C_IMR_RRDY;
|
2007-10-14 05:56:30 +08:00
|
|
|
else
|
2010-01-06 17:24:57 +08:00
|
|
|
w |= DAVINCI_I2C_IMR_XRDY;
|
2007-10-14 05:56:30 +08:00
|
|
|
davinci_i2c_write_reg(dev, DAVINCI_I2C_IMR_REG, w);
|
|
|
|
|
2008-07-15 04:38:21 +08:00
|
|
|
dev->terminate = 0;
|
2008-03-28 13:16:12 +08:00
|
|
|
|
2010-10-12 12:47:05 +08:00
|
|
|
/*
|
|
|
|
* Write mode register first as needed for correct behaviour
|
|
|
|
* on OMAP-L138, but don't set STT yet to avoid a race with XRDY
|
2011-03-31 09:57:33 +08:00
|
|
|
* occurring before we have loaded DXR
|
2010-10-12 12:47:05 +08:00
|
|
|
*/
|
|
|
|
davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, flag);
|
|
|
|
|
2008-03-28 13:16:12 +08:00
|
|
|
/*
|
|
|
|
* First byte should be set here, not after interrupt,
|
|
|
|
* because transmit-data-ready interrupt can come before
|
|
|
|
* NACK-interrupt during sending of previous message and
|
|
|
|
* ICDXR may have wrong data
|
2010-10-12 12:47:05 +08:00
|
|
|
* It also saves us one interrupt, slightly faster
|
2008-03-28 13:16:12 +08:00
|
|
|
*/
|
|
|
|
if ((!(msg->flags & I2C_M_RD)) && dev->buf_len) {
|
|
|
|
davinci_i2c_write_reg(dev, DAVINCI_I2C_DXR_REG, *dev->buf++);
|
|
|
|
dev->buf_len--;
|
|
|
|
}
|
|
|
|
|
2010-10-12 12:47:05 +08:00
|
|
|
/* Set STT to begin transmit now DXR is loaded */
|
|
|
|
flag |= DAVINCI_I2C_MDR_STT;
|
|
|
|
if (stop && msg->len != 0)
|
|
|
|
flag |= DAVINCI_I2C_MDR_STP;
|
2010-09-17 11:02:11 +08:00
|
|
|
davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, flag);
|
|
|
|
|
2007-10-14 05:56:30 +08:00
|
|
|
r = wait_for_completion_interruptible_timeout(&dev->cmd_complete,
|
2009-03-29 04:34:43 +08:00
|
|
|
dev->adapter.timeout);
|
2007-10-14 05:56:30 +08:00
|
|
|
if (r == 0) {
|
|
|
|
dev_err(dev->dev, "controller timed out\n");
|
2013-03-29 18:23:41 +08:00
|
|
|
davinci_i2c_recover_bus(dev);
|
2007-10-14 05:56:30 +08:00
|
|
|
i2c_davinci_init(dev);
|
2008-07-15 04:38:21 +08:00
|
|
|
dev->buf_len = 0;
|
2007-10-14 05:56:30 +08:00
|
|
|
return -ETIMEDOUT;
|
|
|
|
}
|
2008-07-15 04:38:21 +08:00
|
|
|
if (dev->buf_len) {
|
|
|
|
/* This should be 0 if all bytes were transferred
|
|
|
|
* or dev->cmd_err denotes an error.
|
|
|
|
* A signal may have aborted the transfer.
|
|
|
|
*/
|
|
|
|
if (r >= 0) {
|
|
|
|
dev_err(dev->dev, "abnormal termination buf_len=%i\n",
|
|
|
|
dev->buf_len);
|
|
|
|
r = -EREMOTEIO;
|
|
|
|
}
|
|
|
|
dev->terminate = 1;
|
|
|
|
wmb();
|
|
|
|
dev->buf_len = 0;
|
|
|
|
}
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2007-10-14 05:56:30 +08:00
|
|
|
|
|
|
|
/* no error */
|
|
|
|
if (likely(!dev->cmd_err))
|
|
|
|
return msg->len;
|
|
|
|
|
|
|
|
/* We have an error */
|
|
|
|
if (dev->cmd_err & DAVINCI_I2C_STR_AL) {
|
|
|
|
i2c_davinci_init(dev);
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dev->cmd_err & DAVINCI_I2C_STR_NACK) {
|
|
|
|
if (msg->flags & I2C_M_IGNORE_NAK)
|
|
|
|
return msg->len;
|
|
|
|
if (stop) {
|
|
|
|
w = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG);
|
2010-01-06 17:24:57 +08:00
|
|
|
w |= DAVINCI_I2C_MDR_STP;
|
2007-10-14 05:56:30 +08:00
|
|
|
davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, w);
|
|
|
|
}
|
|
|
|
return -EREMOTEIO;
|
|
|
|
}
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Prepare controller for a transaction and call i2c_davinci_xfer_msg
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
i2c_davinci_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
|
|
|
|
{
|
|
|
|
struct davinci_i2c_dev *dev = i2c_get_adapdata(adap);
|
|
|
|
int i;
|
|
|
|
int ret;
|
|
|
|
|
2008-04-23 04:16:47 +08:00
|
|
|
dev_dbg(dev->dev, "%s: msgs: %d\n", __func__, num);
|
2007-10-14 05:56:30 +08:00
|
|
|
|
|
|
|
ret = i2c_davinci_wait_bus_not_busy(dev, 1);
|
|
|
|
if (ret < 0) {
|
|
|
|
dev_warn(dev->dev, "timeout waiting for bus ready\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < num; i++) {
|
|
|
|
ret = i2c_davinci_xfer_msg(adap, &msgs[i], (i == (num - 1)));
|
2008-07-15 04:38:20 +08:00
|
|
|
dev_dbg(dev->dev, "%s [%d/%d] ret: %d\n", __func__, i + 1, num,
|
|
|
|
ret);
|
2007-10-14 05:56:30 +08:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
2010-01-06 17:25:00 +08:00
|
|
|
|
|
|
|
#ifdef CONFIG_CPU_FREQ
|
|
|
|
complete(&dev->xfr_complete);
|
|
|
|
#endif
|
|
|
|
|
2007-10-14 05:56:30 +08:00
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
|
|
|
static u32 i2c_davinci_func(struct i2c_adapter *adap)
|
|
|
|
{
|
2008-03-28 13:16:12 +08:00
|
|
|
return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
|
2007-10-14 05:56:30 +08:00
|
|
|
}
|
|
|
|
|
2008-07-15 04:38:21 +08:00
|
|
|
static void terminate_read(struct davinci_i2c_dev *dev)
|
|
|
|
{
|
|
|
|
u16 w = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG);
|
|
|
|
w |= DAVINCI_I2C_MDR_NACK;
|
|
|
|
davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, w);
|
|
|
|
|
|
|
|
/* Throw away data */
|
|
|
|
davinci_i2c_read_reg(dev, DAVINCI_I2C_DRR_REG);
|
|
|
|
if (!dev->terminate)
|
|
|
|
dev_err(dev->dev, "RDR IRQ while no data requested\n");
|
|
|
|
}
|
|
|
|
static void terminate_write(struct davinci_i2c_dev *dev)
|
|
|
|
{
|
|
|
|
u16 w = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG);
|
|
|
|
w |= DAVINCI_I2C_MDR_RM | DAVINCI_I2C_MDR_STP;
|
|
|
|
davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, w);
|
|
|
|
|
|
|
|
if (!dev->terminate)
|
2009-07-07 06:48:36 +08:00
|
|
|
dev_dbg(dev->dev, "TDR IRQ while no data to send\n");
|
2008-07-15 04:38:21 +08:00
|
|
|
}
|
|
|
|
|
2007-10-14 05:56:30 +08:00
|
|
|
/*
|
|
|
|
* Interrupt service routine. This gets called whenever an I2C interrupt
|
|
|
|
* occurs.
|
|
|
|
*/
|
|
|
|
static irqreturn_t i2c_davinci_isr(int this_irq, void *dev_id)
|
|
|
|
{
|
|
|
|
struct davinci_i2c_dev *dev = dev_id;
|
|
|
|
u32 stat;
|
|
|
|
int count = 0;
|
|
|
|
u16 w;
|
|
|
|
|
|
|
|
while ((stat = davinci_i2c_read_reg(dev, DAVINCI_I2C_IVR_REG))) {
|
2008-04-23 04:16:47 +08:00
|
|
|
dev_dbg(dev->dev, "%s: stat=0x%x\n", __func__, stat);
|
2007-10-14 05:56:30 +08:00
|
|
|
if (count++ == 100) {
|
|
|
|
dev_warn(dev->dev, "Too much work in one IRQ\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (stat) {
|
|
|
|
case DAVINCI_I2C_IVR_AL:
|
2008-07-15 04:38:21 +08:00
|
|
|
/* Arbitration lost, must retry */
|
2007-10-14 05:56:30 +08:00
|
|
|
dev->cmd_err |= DAVINCI_I2C_STR_AL;
|
2008-07-15 04:38:21 +08:00
|
|
|
dev->buf_len = 0;
|
2007-10-14 05:56:30 +08:00
|
|
|
complete(&dev->cmd_complete);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DAVINCI_I2C_IVR_NACK:
|
|
|
|
dev->cmd_err |= DAVINCI_I2C_STR_NACK;
|
2008-07-15 04:38:21 +08:00
|
|
|
dev->buf_len = 0;
|
2007-10-14 05:56:30 +08:00
|
|
|
complete(&dev->cmd_complete);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DAVINCI_I2C_IVR_ARDY:
|
2008-04-11 18:07:05 +08:00
|
|
|
davinci_i2c_write_reg(dev,
|
|
|
|
DAVINCI_I2C_STR_REG, DAVINCI_I2C_STR_ARDY);
|
2008-03-28 13:16:12 +08:00
|
|
|
if (((dev->buf_len == 0) && (dev->stop != 0)) ||
|
|
|
|
(dev->cmd_err & DAVINCI_I2C_STR_NACK)) {
|
|
|
|
w = davinci_i2c_read_reg(dev,
|
|
|
|
DAVINCI_I2C_MDR_REG);
|
|
|
|
w |= DAVINCI_I2C_MDR_STP;
|
|
|
|
davinci_i2c_write_reg(dev,
|
|
|
|
DAVINCI_I2C_MDR_REG, w);
|
|
|
|
}
|
2007-10-14 05:56:30 +08:00
|
|
|
complete(&dev->cmd_complete);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DAVINCI_I2C_IVR_RDR:
|
|
|
|
if (dev->buf_len) {
|
|
|
|
*dev->buf++ =
|
|
|
|
davinci_i2c_read_reg(dev,
|
|
|
|
DAVINCI_I2C_DRR_REG);
|
|
|
|
dev->buf_len--;
|
|
|
|
if (dev->buf_len)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
davinci_i2c_write_reg(dev,
|
2008-04-11 18:07:05 +08:00
|
|
|
DAVINCI_I2C_STR_REG,
|
|
|
|
DAVINCI_I2C_IMR_RRDY);
|
2008-07-15 04:38:21 +08:00
|
|
|
} else {
|
|
|
|
/* signal can terminate transfer */
|
|
|
|
terminate_read(dev);
|
|
|
|
}
|
2007-10-14 05:56:30 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DAVINCI_I2C_IVR_XRDY:
|
|
|
|
if (dev->buf_len) {
|
|
|
|
davinci_i2c_write_reg(dev, DAVINCI_I2C_DXR_REG,
|
|
|
|
*dev->buf++);
|
|
|
|
dev->buf_len--;
|
|
|
|
if (dev->buf_len)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
w = davinci_i2c_read_reg(dev,
|
|
|
|
DAVINCI_I2C_IMR_REG);
|
2010-01-06 17:24:57 +08:00
|
|
|
w &= ~DAVINCI_I2C_IMR_XRDY;
|
2007-10-14 05:56:30 +08:00
|
|
|
davinci_i2c_write_reg(dev,
|
|
|
|
DAVINCI_I2C_IMR_REG,
|
|
|
|
w);
|
2008-07-15 04:38:21 +08:00
|
|
|
} else {
|
|
|
|
/* signal can terminate transfer */
|
|
|
|
terminate_write(dev);
|
|
|
|
}
|
2007-10-14 05:56:30 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DAVINCI_I2C_IVR_SCD:
|
2008-04-11 18:07:05 +08:00
|
|
|
davinci_i2c_write_reg(dev,
|
|
|
|
DAVINCI_I2C_STR_REG, DAVINCI_I2C_STR_SCD);
|
2007-10-14 05:56:30 +08:00
|
|
|
complete(&dev->cmd_complete);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DAVINCI_I2C_IVR_AAS:
|
2009-07-07 06:48:36 +08:00
|
|
|
dev_dbg(dev->dev, "Address as slave interrupt\n");
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
dev_warn(dev->dev, "Unrecognized irq stat %d\n", stat);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2007-10-14 05:56:30 +08:00
|
|
|
|
|
|
|
return count ? IRQ_HANDLED : IRQ_NONE;
|
|
|
|
}
|
|
|
|
|
2010-01-06 17:25:00 +08:00
|
|
|
#ifdef CONFIG_CPU_FREQ
|
|
|
|
static int i2c_davinci_cpufreq_transition(struct notifier_block *nb,
|
|
|
|
unsigned long val, void *data)
|
|
|
|
{
|
|
|
|
struct davinci_i2c_dev *dev;
|
|
|
|
|
|
|
|
dev = container_of(nb, struct davinci_i2c_dev, freq_transition);
|
|
|
|
if (val == CPUFREQ_PRECHANGE) {
|
|
|
|
wait_for_completion(&dev->xfr_complete);
|
|
|
|
davinci_i2c_reset_ctrl(dev, 0);
|
|
|
|
} else if (val == CPUFREQ_POSTCHANGE) {
|
|
|
|
i2c_davinci_calc_clk_dividers(dev);
|
|
|
|
davinci_i2c_reset_ctrl(dev, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int i2c_davinci_cpufreq_register(struct davinci_i2c_dev *dev)
|
|
|
|
{
|
|
|
|
dev->freq_transition.notifier_call = i2c_davinci_cpufreq_transition;
|
|
|
|
|
|
|
|
return cpufreq_register_notifier(&dev->freq_transition,
|
|
|
|
CPUFREQ_TRANSITION_NOTIFIER);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void i2c_davinci_cpufreq_deregister(struct davinci_i2c_dev *dev)
|
|
|
|
{
|
|
|
|
cpufreq_unregister_notifier(&dev->freq_transition,
|
|
|
|
CPUFREQ_TRANSITION_NOTIFIER);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
static inline int i2c_davinci_cpufreq_register(struct davinci_i2c_dev *dev)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void i2c_davinci_cpufreq_deregister(struct davinci_i2c_dev *dev)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-10-14 05:56:30 +08:00
|
|
|
static struct i2c_algorithm i2c_davinci_algo = {
|
|
|
|
.master_xfer = i2c_davinci_xfer,
|
|
|
|
.functionality = i2c_davinci_func,
|
|
|
|
};
|
|
|
|
|
2012-07-30 15:21:12 +08:00
|
|
|
static const struct of_device_id davinci_i2c_of_match[] = {
|
|
|
|
{.compatible = "ti,davinci-i2c", },
|
|
|
|
{},
|
|
|
|
};
|
|
|
|
MODULE_DEVICE_TABLE(of, davinci_i2c_of_match);
|
|
|
|
|
2007-10-14 05:56:30 +08:00
|
|
|
static int davinci_i2c_probe(struct platform_device *pdev)
|
|
|
|
{
|
|
|
|
struct davinci_i2c_dev *dev;
|
|
|
|
struct i2c_adapter *adap;
|
2013-03-05 09:44:45 +08:00
|
|
|
struct resource *mem, *irq;
|
2007-10-14 05:56:30 +08:00
|
|
|
int r;
|
|
|
|
|
|
|
|
irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
|
|
|
|
if (!irq) {
|
|
|
|
dev_err(&pdev->dev, "no irq resource?\n");
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
2013-03-05 09:44:45 +08:00
|
|
|
dev = devm_kzalloc(&pdev->dev, sizeof(struct davinci_i2c_dev),
|
|
|
|
GFP_KERNEL);
|
2007-10-14 05:56:30 +08:00
|
|
|
if (!dev) {
|
2013-03-05 09:44:45 +08:00
|
|
|
dev_err(&pdev->dev, "Memory allocation failed\n");
|
|
|
|
return -ENOMEM;
|
2007-10-14 05:56:30 +08:00
|
|
|
}
|
|
|
|
|
2008-07-15 04:38:21 +08:00
|
|
|
init_completion(&dev->cmd_complete);
|
2010-01-06 17:25:00 +08:00
|
|
|
#ifdef CONFIG_CPU_FREQ
|
|
|
|
init_completion(&dev->xfr_complete);
|
|
|
|
#endif
|
2013-04-18 21:43:07 +08:00
|
|
|
dev->dev = &pdev->dev;
|
2007-10-14 05:56:30 +08:00
|
|
|
dev->irq = irq->start;
|
2013-09-06 03:23:44 +08:00
|
|
|
dev->pdata = dev_get_platdata(&pdev->dev);
|
2007-10-14 05:56:30 +08:00
|
|
|
platform_set_drvdata(pdev, dev);
|
|
|
|
|
2012-07-30 15:21:12 +08:00
|
|
|
if (!dev->pdata && pdev->dev.of_node) {
|
|
|
|
u32 prop;
|
|
|
|
|
|
|
|
dev->pdata = devm_kzalloc(&pdev->dev,
|
|
|
|
sizeof(struct davinci_i2c_platform_data), GFP_KERNEL);
|
2013-04-18 21:43:07 +08:00
|
|
|
if (!dev->pdata)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2012-07-30 15:21:12 +08:00
|
|
|
memcpy(dev->pdata, &davinci_i2c_platform_data_default,
|
|
|
|
sizeof(struct davinci_i2c_platform_data));
|
|
|
|
if (!of_property_read_u32(pdev->dev.of_node, "clock-frequency",
|
|
|
|
&prop))
|
|
|
|
dev->pdata->bus_freq = prop / 1000;
|
|
|
|
} else if (!dev->pdata) {
|
|
|
|
dev->pdata = &davinci_i2c_platform_data_default;
|
|
|
|
}
|
|
|
|
|
2013-03-05 09:44:45 +08:00
|
|
|
dev->clk = devm_clk_get(&pdev->dev, NULL);
|
2013-04-18 21:43:07 +08:00
|
|
|
if (IS_ERR(dev->clk))
|
|
|
|
return -ENODEV;
|
2012-08-31 02:10:36 +08:00
|
|
|
clk_prepare_enable(dev->clk);
|
2007-10-14 05:56:30 +08:00
|
|
|
|
2013-05-10 16:16:54 +08:00
|
|
|
mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
|
2013-03-05 09:44:45 +08:00
|
|
|
dev->base = devm_ioremap_resource(&pdev->dev, mem);
|
|
|
|
if (IS_ERR(dev->base)) {
|
|
|
|
r = PTR_ERR(dev->base);
|
|
|
|
goto err_unuse_clocks;
|
2010-01-06 17:24:57 +08:00
|
|
|
}
|
|
|
|
|
2007-10-14 05:56:30 +08:00
|
|
|
i2c_davinci_init(dev);
|
|
|
|
|
2013-03-05 09:44:45 +08:00
|
|
|
r = devm_request_irq(&pdev->dev, dev->irq, i2c_davinci_isr, 0,
|
|
|
|
pdev->name, dev);
|
2007-10-14 05:56:30 +08:00
|
|
|
if (r) {
|
|
|
|
dev_err(&pdev->dev, "failure requesting irq %i\n", dev->irq);
|
|
|
|
goto err_unuse_clocks;
|
|
|
|
}
|
|
|
|
|
2010-01-06 17:25:00 +08:00
|
|
|
r = i2c_davinci_cpufreq_register(dev);
|
|
|
|
if (r) {
|
|
|
|
dev_err(&pdev->dev, "failed to register cpufreq\n");
|
2013-03-05 09:44:45 +08:00
|
|
|
goto err_unuse_clocks;
|
2010-01-06 17:25:00 +08:00
|
|
|
}
|
|
|
|
|
2007-10-14 05:56:30 +08:00
|
|
|
adap = &dev->adapter;
|
|
|
|
i2c_set_adapdata(adap, dev);
|
|
|
|
adap->owner = THIS_MODULE;
|
2014-07-10 19:46:25 +08:00
|
|
|
adap->class = I2C_CLASS_DEPRECATED;
|
2007-10-14 05:56:30 +08:00
|
|
|
strlcpy(adap->name, "DaVinci I2C adapter", sizeof(adap->name));
|
|
|
|
adap->algo = &i2c_davinci_algo;
|
|
|
|
adap->dev.parent = &pdev->dev;
|
2009-03-29 04:34:43 +08:00
|
|
|
adap->timeout = DAVINCI_I2C_TIMEOUT;
|
2012-07-30 15:21:12 +08:00
|
|
|
adap->dev.of_node = pdev->dev.of_node;
|
2007-10-14 05:56:30 +08:00
|
|
|
|
|
|
|
adap->nr = pdev->id;
|
|
|
|
r = i2c_add_numbered_adapter(adap);
|
|
|
|
if (r) {
|
|
|
|
dev_err(&pdev->dev, "failure adding adapter\n");
|
2013-03-05 09:44:45 +08:00
|
|
|
goto err_unuse_clocks;
|
2007-10-14 05:56:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
err_unuse_clocks:
|
2012-08-31 02:10:36 +08:00
|
|
|
clk_disable_unprepare(dev->clk);
|
2007-10-14 05:56:30 +08:00
|
|
|
dev->clk = NULL;
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int davinci_i2c_remove(struct platform_device *pdev)
|
|
|
|
{
|
|
|
|
struct davinci_i2c_dev *dev = platform_get_drvdata(pdev);
|
|
|
|
|
2010-01-06 17:25:00 +08:00
|
|
|
i2c_davinci_cpufreq_deregister(dev);
|
|
|
|
|
2007-10-14 05:56:30 +08:00
|
|
|
i2c_del_adapter(&dev->adapter);
|
|
|
|
|
2012-08-31 02:10:36 +08:00
|
|
|
clk_disable_unprepare(dev->clk);
|
2007-10-14 05:56:30 +08:00
|
|
|
dev->clk = NULL;
|
|
|
|
|
|
|
|
davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, 0);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-01-06 17:24:59 +08:00
|
|
|
#ifdef CONFIG_PM
|
|
|
|
static int davinci_i2c_suspend(struct device *dev)
|
|
|
|
{
|
|
|
|
struct platform_device *pdev = to_platform_device(dev);
|
|
|
|
struct davinci_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
|
|
|
|
|
|
|
|
/* put I2C into reset */
|
|
|
|
davinci_i2c_reset_ctrl(i2c_dev, 0);
|
2012-08-31 02:10:36 +08:00
|
|
|
clk_disable_unprepare(i2c_dev->clk);
|
2010-01-06 17:24:59 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int davinci_i2c_resume(struct device *dev)
|
|
|
|
{
|
|
|
|
struct platform_device *pdev = to_platform_device(dev);
|
|
|
|
struct davinci_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
|
|
|
|
|
2012-08-31 02:10:36 +08:00
|
|
|
clk_prepare_enable(i2c_dev->clk);
|
2010-01-06 17:24:59 +08:00
|
|
|
/* take I2C out of reset */
|
|
|
|
davinci_i2c_reset_ctrl(i2c_dev, 1);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct dev_pm_ops davinci_i2c_pm = {
|
|
|
|
.suspend = davinci_i2c_suspend,
|
|
|
|
.resume = davinci_i2c_resume,
|
|
|
|
};
|
|
|
|
|
|
|
|
#define davinci_i2c_pm_ops (&davinci_i2c_pm)
|
|
|
|
#else
|
|
|
|
#define davinci_i2c_pm_ops NULL
|
|
|
|
#endif
|
|
|
|
|
2008-04-23 04:16:49 +08:00
|
|
|
/* work with hotplug and coldplug */
|
|
|
|
MODULE_ALIAS("platform:i2c_davinci");
|
|
|
|
|
2007-10-14 05:56:30 +08:00
|
|
|
static struct platform_driver davinci_i2c_driver = {
|
|
|
|
.probe = davinci_i2c_probe,
|
|
|
|
.remove = davinci_i2c_remove,
|
|
|
|
.driver = {
|
|
|
|
.name = "i2c_davinci",
|
|
|
|
.owner = THIS_MODULE,
|
2010-01-06 17:24:59 +08:00
|
|
|
.pm = davinci_i2c_pm_ops,
|
2013-09-30 11:34:25 +08:00
|
|
|
.of_match_table = davinci_i2c_of_match,
|
2007-10-14 05:56:30 +08:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
/* I2C may be needed to bring up other drivers */
|
|
|
|
static int __init davinci_i2c_init_driver(void)
|
|
|
|
{
|
|
|
|
return platform_driver_register(&davinci_i2c_driver);
|
|
|
|
}
|
|
|
|
subsys_initcall(davinci_i2c_init_driver);
|
|
|
|
|
|
|
|
static void __exit davinci_i2c_exit_driver(void)
|
|
|
|
{
|
|
|
|
platform_driver_unregister(&davinci_i2c_driver);
|
|
|
|
}
|
|
|
|
module_exit(davinci_i2c_exit_driver);
|
|
|
|
|
|
|
|
MODULE_AUTHOR("Texas Instruments India");
|
|
|
|
MODULE_DESCRIPTION("TI DaVinci I2C bus adapter");
|
|
|
|
MODULE_LICENSE("GPL");
|