static void omap_i2c_idle(struct omap_i2c_dev *dev)
{
	struct platform_device *pdev;
	struct omap_i2c_bus_platform_data *pdata;
	u16 iv;

	WARN_ON(dev->idle);

	pdev = to_platform_device(dev->dev);
	pdata = pdev->dev.platform_data;

	dev->iestate = omap_i2c_read_reg(dev, OMAP_I2C_IE_REG);
	if (dev->rev >= OMAP_I2C_REV_ON_4430)
		omap_i2c_write_reg(dev, OMAP_I2C_IRQENABLE_CLR, 1);
	else
		omap_i2c_write_reg(dev, OMAP_I2C_IE_REG, 0);

	if (dev->rev < OMAP_I2C_REV_2) {
		iv = omap_i2c_read_reg(dev, OMAP_I2C_IV_REG); /* Read clears */
	} else {
		omap_i2c_write_reg(dev, OMAP_I2C_STAT_REG, dev->iestate);

		/* Flush posted write before the dev->idle store occurs */
		omap_i2c_read_reg(dev, OMAP_I2C_STAT_REG);
	}
	dev->idle = 1;

	pm_runtime_put_sync(&pdev->dev);
}
Esempio n. 2
0
static int omap_i2c_reset(struct omap_i2c_dev *dev)
{
	unsigned long timeout;
	u16 sysc;

	if (dev->rev >= OMAP_I2C_OMAP1_REV_2) {
		sysc = omap_i2c_read_reg(dev, OMAP_I2C_SYSC_REG);

		/* Disable I2C controller before soft reset */
		omap_i2c_write_reg(dev, OMAP_I2C_CON_REG,
			omap_i2c_read_reg(dev, OMAP_I2C_CON_REG) &
				~(OMAP_I2C_CON_EN));

		omap_i2c_write_reg(dev, OMAP_I2C_SYSC_REG, SYSC_SOFTRESET_MASK);
		/* For some reason we need to set the EN bit before the
		 * reset done bit gets set. */
		timeout = jiffies + OMAP_I2C_TIMEOUT;
		omap_i2c_write_reg(dev, OMAP_I2C_CON_REG, OMAP_I2C_CON_EN);
		while (!(omap_i2c_read_reg(dev, OMAP_I2C_SYSS_REG) &
			 SYSS_RESETDONE_MASK)) {
			if (time_after(jiffies, timeout)) {
				dev_warn(dev->dev, "timeout waiting "
						"for controller reset\n");
				return -ETIMEDOUT;
			}
			msleep(1);
		}

		/* SYSC register is cleared by the reset; rewrite it */
		omap_i2c_write_reg(dev, OMAP_I2C_SYSC_REG, sysc);

	}
	return 0;
}
static inline void i2c_omap_errata_i207(struct omap_i2c_dev *dev, u16 stat)
{
	/*
	 * I2C Errata(Errata Nos. OMAP2: 1.67, OMAP3: 1.8)
	 * Not applicable for OMAP4.
	 * Under certain rare conditions, RDR could be set again
	 * when the bus is busy, then ignore the interrupt and
	 * clear the interrupt.
	 */
	if (stat & OMAP_I2C_STAT_RDR) {
		/* Step 1: If RDR is set, clear it */
		omap_i2c_ack_stat(dev, OMAP_I2C_STAT_RDR);

		/* Step 2: */
		if (!(omap_i2c_read_reg(dev, OMAP_I2C_STAT_REG)
						& OMAP_I2C_STAT_BB)) {

			/* Step 3: */
			if (omap_i2c_read_reg(dev, OMAP_I2C_STAT_REG)
						& OMAP_I2C_STAT_RDR) {
				omap_i2c_ack_stat(dev, OMAP_I2C_STAT_RDR);
				dev_dbg(dev->dev, "RDR when bus is busy.\n");
			}

		}
	}
}
static irqreturn_t
omap_i2c_rev1_isr(int this_irq, void *dev_id)
{
	struct omap_i2c_dev *dev = dev_id;
	u16 iv, w;

	if (dev->idle)
		return IRQ_NONE;

	iv = omap_i2c_read_reg(dev, OMAP_I2C_IV_REG);
	switch (iv) {
	case 0x00:	/* None */
		break;
	case 0x01:	/* Arbitration lost */
		dev_err(dev->dev, "Arbitration lost\n");
		omap_i2c_complete_cmd(dev, OMAP_I2C_STAT_AL);
		break;
	case 0x02:	/* No acknowledgement */
		omap_i2c_complete_cmd(dev, OMAP_I2C_STAT_NACK);
		omap_i2c_write_reg(dev, OMAP_I2C_CON_REG, OMAP_I2C_CON_STP);
		break;
	case 0x03:	/* Register access ready */
		omap_i2c_complete_cmd(dev, 0);
		break;
	case 0x04:	/* Receive data ready */
		if (dev->buf_len) {
			w = omap_i2c_read_reg(dev, OMAP_I2C_DATA_REG);
			*dev->buf++ = w;
			dev->buf_len--;
			if (dev->buf_len) {
				*dev->buf++ = w >> 8;
				dev->buf_len--;
			}
		} else
Esempio n. 5
0
/*
 * Wait while BB-bit doesn't reflect the I2C bus state
 *
 * In a multimaster environment, after IP software reset, BB-bit value doesn't
 * correspond to the current bus state. It may happen what BB-bit will be 0,
 * while the bus is busy due to another I2C master activity.
 * Here are BB-bit values after reset:
 *     SDA   SCL   BB   NOTES
 *       0     0    0   1, 2
 *       1     0    0   1, 2
 *       0     1    1
 *       1     1    0   3
 * Later, if IP detect SDA=0 and SCL=1 (ACK) or SDA 1->0 while SCL=1 (START)
 * combinations on the bus, it set BB-bit to 1.
 * If IP detect SDA 0->1 while SCL=1 (STOP) combination on the bus,
 * it set BB-bit to 0 and BF to 1.
 * BB and BF bits correctly tracks the bus state while IP is suspended
 * BB bit became valid on the next FCLK clock after CON_EN bit set
 *
 * NOTES:
 * 1. Any transfer started when BB=0 and bus is busy wouldn't be
 *    completed by IP and results in controller timeout.
 * 2. Any transfer started when BB=0 and SCL=0 results in IP
 *    starting to drive SDA low. In that case IP corrupt data
 *    on the bus.
 * 3. Any transfer started in the middle of another master's transfer
 *    results in unpredictable results and data corruption
 */
static int omap_i2c_wait_for_bb_valid(struct omap_i2c_dev *dev)
{
	unsigned long bus_free_timeout = 0;
	unsigned long timeout;
	int bus_free = 0;
	u16 stat, systest;

	if (dev->bb_valid)
		return 0;

	timeout = jiffies + OMAP_I2C_TIMEOUT;
	while (1) {
		stat = omap_i2c_read_reg(dev, OMAP_I2C_STAT_REG);
		/*
		 * We will see BB or BF event in a case IP had detected any
		 * activity on the I2C bus. Now IP correctly tracks the bus
		 * state. BB-bit value is valid.
		 */
		if (stat & (OMAP_I2C_STAT_BB | OMAP_I2C_STAT_BF))
			break;

		/*
		 * Otherwise, we must look signals on the bus to make
		 * the right decision.
		 */
		systest = omap_i2c_read_reg(dev, OMAP_I2C_SYSTEST_REG);
		if ((systest & OMAP_I2C_SYSTEST_SCL_I_FUNC) &&
		    (systest & OMAP_I2C_SYSTEST_SDA_I_FUNC)) {
			if (!bus_free) {
				bus_free_timeout = jiffies +
					OMAP_I2C_BUS_FREE_TIMEOUT;
				bus_free = 1;
			}

			/*
			 * SDA and SCL lines was high for 10 ms without bus
			 * activity detected. The bus is free. Consider
			 * BB-bit value is valid.
			 */
			if (time_after(jiffies, bus_free_timeout))
				break;
		} else {
			bus_free = 0;
		}

		if (time_after(jiffies, timeout)) {
			dev_warn(dev->dev, "timeout waiting for bus ready\n");
			return -ETIMEDOUT;
		}

		msleep(1);
	}

	dev->bb_valid = 1;
	return 0;
}
Esempio n. 6
0
static int omap_i2c_reset(struct omap_i2c_dev *dev)
{
	unsigned long timeout;
	/* TTX-897: Fix Wi-Fi on/off causes system mysteriously reboots
	 * TTX-3735: Battery capacity show 0% which against actual battery
	 *		capacity while doing manual power on/off test
	 * TTX-2798: Device stuck on boot screen after factory reset loop
	 *		or reboot loop test or flashing loop test
	 * The arch/arm/mach-omap2/i2c.c: omap_i2c_reset() function that
	 * this function used to call is not restoring the OMAP_I2C_SYSC_REG
	 * register after a soft reset of I2C module. As a result, the clocks
	 * for the I2C module were getting cut even after we call
	 * pm_runtime_get_sync().
	 * So, we explicity soft reset the I2C module ourselves and restore
	 * the SYSC register
	 */
	/* Disable I2C controller before soft reset */
	omap_i2c_write_reg(dev, OMAP_I2C_CON_REG,
		omap_i2c_read_reg(dev, OMAP_I2C_CON_REG) &
			~(OMAP_I2C_CON_EN));

	omap_i2c_write_reg(dev, OMAP_I2C_SYSC_REG, SYSC_SOFTRESET_MASK);
	/* According to TRM 23.1.4.3 HS I2C Software Reset we need to set
	 * the EN bit before the before we start checking on reset done
	 */
	timeout = jiffies + OMAP_I2C_TIMEOUT;
	omap_i2c_write_reg(dev, OMAP_I2C_CON_REG, OMAP_I2C_CON_EN);
	while (!(omap_i2c_read_reg(dev, OMAP_I2C_SYSS_REG) &
			SYSS_RESETDONE_MASK)) {
		if (time_after(jiffies, timeout)) {
			dev_warn(dev->dev, "timeout waiting "
				"for controller reset\n");
			return -ETIMEDOUT;
		}
		mdelay(1);
	}

	/* SYSC register is cleared by the reset; rewrite it */
	if (dev->rev == OMAP_I2C_REV_ON_2430) {
		omap_i2c_write_reg(dev, OMAP_I2C_SYSC_REG,
					SYSC_AUTOIDLE_MASK);
	} else if (dev->rev >= OMAP_I2C_REV_ON_3430) {
		dev->syscstate = SYSC_AUTOIDLE_MASK;
		dev->syscstate |= SYSC_ENAWAKEUP_MASK;
		dev->syscstate |= (SYSC_IDLEMODE_SMARTWKUP <<
					__ffs(SYSC_SIDLEMODE_MASK));
		dev->syscstate |= (SYSC_CLOCKACTIVITY_FCLK <<
					__ffs(SYSC_CLOCKACTIVITY_MASK));
		omap_i2c_write_reg(dev, OMAP_I2C_SYSC_REG,
					dev->syscstate);
	}
	return 0;
}
Esempio n. 7
0
static void omap_i2c_idle(struct omap_i2c_dev *dev)
{
	u16 iv;

	dev->idle = 1;
	dev->iestate = omap_i2c_read_reg(dev, OMAP_I2C_IE_REG);
	omap_i2c_write_reg(dev, OMAP_I2C_IE_REG, 0);
	if (dev->rev1)
		iv = omap_i2c_read_reg(dev, OMAP_I2C_IV_REG);
	else
		omap_i2c_write_reg(dev, OMAP_I2C_STAT_REG, dev->iestate);
	clk_disable(dev->fclk);
	if (dev->iclk != NULL)
		clk_disable(dev->iclk);
}
Esempio n. 8
0
static int omap_i2c_dpll_notifier(struct notifier_block *nb,
					unsigned long val, void *data)
{
	struct omap_i2c_dev *dev = container_of(nb, struct omap_i2c_dev, nb);
	struct clk_notifier_data *cnd = (struct clk_notifier_data *)data;
	unsigned int count = 0;

	spin_lock(&dev->dpll_lock);

	if (val == CLK_POST_RATE_CHANGE &&
		cnd->old_rate == OMAP_I2C_MASTER_CLOCK)
		dev->dpll_entry = 1;
	else if (val == CLK_PRE_RATE_CHANGE &&
		cnd->old_rate == OMAP_I2C_DPLL_CLOCK) {
		/*
		 * If the device is not idle in the DPLL exit
		 * wait for the bus to become free.
		 */
		if (0 == dev->idle) {
			while (omap_i2c_read_reg(dev,
				OMAP_I2C_STAT_REG) & OMAP_I2C_STAT_BB) {
				if (count++ == 100) {
					dev_warn(dev->dev,
				"I2C is busy during DPLL cascading exit\n");
					break;
				}
			}
		}
		dev->dpll_exit = 1;
	}

	spin_unlock(&dev->dpll_lock);

	return 0;
}
Esempio n. 9
0
/*
 * Bus Clear
 */
static int omap_i2c_bus_clear(struct omap_i2c_dev *dev)
{
	u16 w;

	/* Per the I2C specification, if we are stuck in a bus busy state
	 * we can attempt a bus clear to try and recover the bus by sending
	 * at least 9 clock pulses on SCL. Put the I2C in a test mode so it
	 * will output a continuous clock on SCL.
	 */
	disable_irq(dev->irq);

	w = omap_i2c_read_reg(dev, OMAP_I2C_SYSTEST_REG);
	omap_i2c_write_reg(dev, OMAP_I2C_CON_REG, OMAP_I2C_CON_EN);
	omap_i2c_write_reg(dev, OMAP_I2C_SYSTEST_REG,
		(OMAP_I2C_SYSTEST_ST_EN | OMAP_I2C_SYSTEST_TMODE_TEST));
	msleep(1);
	omap_i2c_write_reg(dev, OMAP_I2C_SYSTEST_REG, w);
	omap_i2c_write_reg(dev, OMAP_I2C_CON_REG, 0);
	omap_i2c_reset(dev);
	omap_i2c_init(dev);

	enable_irq(dev->irq);

	return omap_i2c_wait_for_bb(dev);
}
Esempio n. 10
0
static int omap_i2c_reset(struct omap_i2c_dev *dev)
{
	unsigned long timeout;
	if (dev->rev >= OMAP_I2C_OMAP1_REV_2) {
		/* Disable I2C controller before soft reset */
		omap_i2c_write_reg(dev, OMAP_I2C_CON_REG,
			omap_i2c_read_reg(dev, OMAP_I2C_CON_REG) &
				~(OMAP_I2C_CON_EN));

		omap_i2c_write_reg(dev, OMAP_I2C_SYSC_REG, SYSC_SOFTRESET_MASK);
		/* For some reason we need to set the EN bit before the
		 * reset done bit gets set. */
		timeout = jiffies + OMAP_I2C_TIMEOUT;
		omap_i2c_write_reg(dev, OMAP_I2C_CON_REG, OMAP_I2C_CON_EN);
		while (!(omap_i2c_read_reg(dev, OMAP_I2C_SYSS_REG) &
			 SYSS_RESETDONE_MASK)) {
			if (time_after(jiffies, timeout)) {
				dev_warn(dev->dev, "timeout waiting "
						"for controller reset\n");
				return -ETIMEDOUT;
			}
			mdelay(1);
		}

		/* SYSC register is cleared by the reset; rewrite it */
		if (dev->rev == OMAP_I2C_REV_ON_2430) {

			omap_i2c_write_reg(dev, OMAP_I2C_SYSC_REG,
					   SYSC_AUTOIDLE_MASK);

		} else if (dev->rev >= OMAP_I2C_REV_ON_3430_3530) {
			dev->syscstate = SYSC_AUTOIDLE_MASK;
			dev->syscstate |= SYSC_ENAWAKEUP_MASK;
			dev->syscstate |= (SYSC_IDLEMODE_SMART <<
			      __ffs(SYSC_SIDLEMODE_MASK));
			dev->syscstate |= (SYSC_CLOCKACTIVITY_FCLK <<
			      __ffs(SYSC_CLOCKACTIVITY_MASK));

			omap_i2c_write_reg(dev, OMAP_I2C_SYSC_REG,
							dev->syscstate);
		}
	}
	return 0;
}
Esempio n. 11
0
static void omap_i2c_idle(struct omap_i2c_dev *dev)
{
	u16 iv;

	dev->iestate = omap_i2c_read_reg(dev, OMAP_I2C_IE_REG);
	if (dev->dtrev == OMAP_I2C_IP_VERSION_2)
		omap_i2c_write_reg(dev, OMAP_I2C_IP_V2_IRQENABLE_CLR, 1);
	else
		omap_i2c_write_reg(dev, OMAP_I2C_IE_REG, 0);

	if (dev->rev < OMAP_I2C_OMAP1_REV_2) {
		iv = omap_i2c_read_reg(dev, OMAP_I2C_IV_REG); /* Read clears */
	} else {
		omap_i2c_write_reg(dev, OMAP_I2C_STAT_REG, dev->iestate);

		/* Flush posted write */
		omap_i2c_read_reg(dev, OMAP_I2C_STAT_REG);
	}
}
Esempio n. 12
0
static void omap_i2c_idle(struct omap_i2c_dev *dev)
{
	u16 iv;

	WARN_ON(dev->idle);

	dev->iestate = omap_i2c_read_reg(dev, OMAP_I2C_IE_REG);
	omap_i2c_write_reg(dev, OMAP_I2C_IE_REG, 0);
	if (dev->rev < OMAP_I2C_REV_2) {
		iv = omap_i2c_read_reg(dev, OMAP_I2C_IV_REG); /* Read clears */
	} else {
		omap_i2c_write_reg(dev, OMAP_I2C_STAT_REG, dev->iestate);

		/* Flush posted write before the dev->idle store occurs */
		omap_i2c_read_reg(dev, OMAP_I2C_STAT_REG);
	}
	dev->idle = 1;
	clk_disable(dev->fclk);
	clk_disable(dev->iclk);
}
Esempio n. 13
0
static void omap_i2c_idle(struct omap_i2c_dev *dev)
{
	struct omap_i2c_bus_platform_data *pdata;
	u16 iv;

	pdata = dev->dev->platform_data;

	dev->iestate = omap_i2c_read_reg(dev, OMAP_I2C_IE_REG);

	omap_i2c_write_reg(dev, OMAP_I2C_IE_REG, 0);

	if (dev->rev < OMAP_I2C_OMAP1_REV_2) {
		iv = omap_i2c_read_reg(dev, OMAP_I2C_IV_REG); /* Read clears */
	} else {
		omap_i2c_write_reg(dev, OMAP_I2C_STAT_REG, dev->iestate);

		/* Flush posted write */
		omap_i2c_read_reg(dev, OMAP_I2C_STAT_REG);
	}
}
Esempio n. 14
0
/*
 * Prepare controller for a transaction and call omap_i2c_xfer_msg
 * to do the work during IRQ processing.
 */
static int
omap_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
{
	struct omap_i2c_dev *dev = i2c_get_adapdata(adap);
	int i;
	int r;
//--[[ LGE_UBIQUIX_MODIFIED_START : [email protected] [2011.11.16] - In case a peripheral is holding the DATA bus low, reset the I2C controller
	u16 val;
//--]] LGE_UBIQUIX_MODIFIED_END : [email protected] [2011.11.16]- In case a peripheral is holding the DATA bus low, reset the I2C controller

	/*
	 * hwspinlock is used to time share the I2C module between A9 and Ducati
	 * on OMAP4. To avoid spurious IRQ due to I2C transaction initiated on
	 * Ducati sub system I2C IRQ is enabled and disabled on i2c transfers.
	 */

	omap_i2c_hwspinlock_lock(dev);
	omap_i2c_unidle(dev);
	enable_irq(dev->irq);

	r = omap_i2c_wait_for_bb(dev);
//--[[ LGE_UBIQUIX_MODIFIED_START : [email protected] [2011.11.16] - In case a peripheral is holding the DATA bus low, reset the I2C controller
	/* If timeout, try to again check after soft reset of I2C block */
	if (WARN_ON(r == -ETIMEDOUT)) {
		/* Provide a permanent clock to recover the peripheral */
		val = omap_i2c_read_reg(dev, OMAP_I2C_SYSTEST_REG);
		val |= (OMAP_I2C_SYSTEST_ST_EN |
		OMAP_I2C_SYSTEST_FREE |
		(2 << OMAP_I2C_SYSTEST_TMODE_SHIFT));
		omap_i2c_write_reg(dev, OMAP_I2C_SYSTEST_REG, val);
		msleep(1);
		omap_i2c_init(dev);
		r = omap_i2c_wait_for_bb(dev);
	}
//--]] LGE_UBIQUIX_MODIFIED_END : [email protected] [2011.11.16]- In case a peripheral is holding the DATA bus low, reset the I2C controller
	if (r < 0)
		goto out;

	for (i = 0; i < num; i++) {
		r = omap_i2c_xfer_msg(adap, &msgs[i], (i == (num - 1)));
		if (r != 0)
			break;
	}

	if (r == 0)
		r = num;

	omap_i2c_wait_for_bb(dev);
out:
	disable_irq_nosync(dev->irq);
	omap_i2c_idle(dev);
	omap_i2c_hwspinlock_unlock(dev);
	return r;
}
Esempio n. 15
0
/*
 * Prepare controller for a transaction and call omap_i2c_xfer_msg
 * to do the work during IRQ processing.
 */
static int
omap_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
{
	struct omap_i2c_dev *dev = i2c_get_adapdata(adap);
	int i;
	int r;
	u16 val;

	omap_i2c_unidle(dev);

	r = omap_i2c_wait_for_bb(dev);
	/* If timeout, try to again check after soft reset of I2C block */
	if (WARN_ON(r == -ETIMEDOUT)) {
		/* Provide a permanent clock to recover the peripheral */
		val = omap_i2c_read_reg(dev, OMAP_I2C_SYSTEST_REG);
		val |= (OMAP_I2C_SYSTEST_ST_EN |
				OMAP_I2C_SYSTEST_FREE |
				(2 << OMAP_I2C_SYSTEST_TMODE_SHIFT));
		omap_i2c_write_reg(dev, OMAP_I2C_SYSTEST_REG, val);
		msleep(1);
		omap_i2c_init(dev);
		r = omap_i2c_wait_for_bb(dev);
	}
	if (r < 0)
		goto out;

	/*
	 * When waiting for completion of a i2c transfer, we need to
	 * set a wake up latency constraint for the MPU. This is to
	 * ensure quick enough wakeup from idle, when transfer
	 * completes.
	 */
	if (dev->pm_qos)
		pm_qos_update_request(dev->pm_qos, dev->latency);

	for (i = 0; i < num; i++) {
		r = omap_i2c_xfer_msg(adap, &msgs[i], (i == (num - 1)));
		if (r != 0)
			break;
	}

	if (dev->pm_qos)
		pm_qos_update_request(dev->pm_qos, PM_QOS_DEFAULT_VALUE);

	if (r == 0)
		r = num;

	omap_i2c_wait_for_bb(dev);
out:
	omap_i2c_idle(dev);
	return r;
}
Esempio n. 16
0
/*
 * Waiting on Bus Busy
 */
static int omap_i2c_wait_for_bb(struct omap_i2c_dev *dev)
{
	unsigned long timeout;

	timeout = jiffies + OMAP_I2C_TIMEOUT;
	while (omap_i2c_read_reg(dev, OMAP_I2C_STAT_REG) & OMAP_I2C_STAT_BB) {
		if (time_after(jiffies, timeout))
			return i2c_recover_bus(&dev->adapter);
		msleep(1);
	}

	return 0;
}
Esempio n. 17
0
static irqreturn_t
omap_i2c_rev1_isr(int this_irq, void *dev_id)
{
	struct omap_i2c_dev *dev = dev_id;
	u16 iv, w;

	if (dev->idle)
		return IRQ_NONE;

	iv = omap_i2c_read_reg(dev, OMAP_I2C_IV_REG);
	switch (iv) {
	case 0x00:	/* None */
		break;
/* <-- [email protected] delete arbitration mechanism for solving mg touch suspend/resume issue*/		
#if 0		
	case 0x01:	/* Arbitration lost */
		dev_err(dev->dev, "Arbitration lost\n");
		omap_i2c_complete_cmd(dev, OMAP_I2C_STAT_AL);
		break;
#endif		
/*  [email protected] delete arbitration mechanism for solving mg touch suspend/resume issue -->*/		
	case 0x02:	/* No acknowledgement */
		omap_i2c_complete_cmd(dev, OMAP_I2C_STAT_NACK);
		omap_i2c_write_reg(dev, OMAP_I2C_CON_REG, OMAP_I2C_CON_STP);
		break;
	case 0x03:	/* Register access ready */
		omap_i2c_complete_cmd(dev, 0);
		break;
	case 0x04:	/* Receive data ready */
		if (dev->buf_len) {
			w = omap_i2c_read_reg(dev, OMAP_I2C_DATA_REG);
			*dev->buf++ = w;
			dev->buf_len--;
			if (dev->buf_len) {
				*dev->buf++ = w >> 8;
				dev->buf_len--;
			}
		} else
Esempio n. 18
0
static int omap_i2c_unidle(struct omap_i2c_dev *dev)
{
	struct platform_device *pdev;
	struct omap_i2c_bus_platform_data *pdata;
        int ret = 0;

	WARN_ON(!dev->idle);

	pdev = to_platform_device(dev->dev);
	pdata = pdev->dev.platform_data;

	ret = pm_runtime_get_sync(&pdev->dev);
	if (IS_ERR_VALUE(ret)) {
		dev_err(&pdev->dev,"%s: failed to get sync\n", __func__);
		return ret;
	}

	if (cpu_is_omap34xx() || cpu_is_omap44xx()) {
		unsigned long delay;

		omap_i2c_write_reg(dev, OMAP_I2C_CON_REG, 0);
		omap_i2c_write_reg(dev, OMAP_I2C_PSC_REG, dev->pscstate);
		omap_i2c_write_reg(dev, OMAP_I2C_SCLL_REG, dev->scllstate);
		omap_i2c_write_reg(dev, OMAP_I2C_SCLH_REG, dev->sclhstate);
		omap_i2c_write_reg(dev, OMAP_I2C_BUF_REG, dev->bufstate);
		omap_i2c_write_reg(dev, OMAP_I2C_WE_REG, dev->westate);
		omap_i2c_write_reg(dev, OMAP_I2C_CON_REG, OMAP_I2C_CON_EN);

		delay = jiffies + OMAP_I2C_TIMEOUT;
		while (!(omap_i2c_read_reg(dev, OMAP_I2C_SYSS_REG)
				& OMAP_I2C_SYSS_RDONE)) {
			if (time_after(jiffies, delay)) {
				dev_err(dev->dev, "omap i2c unidle timeout\n");
				return -ETIMEDOUT;
			}
			cpu_relax();
		}
	}
	dev->idle = 0;

	if (cpu_is_omap44xx() && dev->rev >= OMAP_I2C_REV_ON_4430) {
		omap_i2c_write_reg(dev, OMAP_I2C_IRQENABLE_CLR,0x6FFF);
		omap_i2c_write_reg(dev, OMAP_I2C_IRQENABLE_SET, dev->iestate);
	} else {
		omap_i2c_write_reg(dev, OMAP_I2C_IE_REG, dev->iestate);
	}

	return ret;
}
Esempio n. 19
0
/*
 * Waiting on Bus Busy
 */
static int omap_i2c_wait_for_bb(struct omap_i2c_dev *dev)
{
	unsigned long timeout = 0;

	while (omap_i2c_read_reg(dev, OMAP_I2C_STAT_REG) & OMAP_I2C_STAT_BB) {
		if (timeout > OMAP_I2C_TIMEOUT) {
			dev_warn(dev->dev, "timeout waiting for bus ready\n");
			omap_i2c_dump(dev);
			return -ETIMEDOUT;
		}
		timeout += usleep_range(1000, 2000);
	}

	return 0;
}
/*
 * Waiting on Bus Busy
 */
static int omap_i2c_wait_for_bb(struct omap_i2c_dev *dev)
{
	unsigned long timeout;

	timeout = jiffies + OMAP_I2C_TIMEOUT;
	while (omap_i2c_read_reg(dev, OMAP_I2C_STAT_REG) & OMAP_I2C_STAT_BB) {
		if (time_after(jiffies, timeout)) {
			dev_warn(dev->dev, "timeout waiting for bus ready\n");
			return -ETIMEDOUT;
		}
		msleep(1);
	}

	return 0;
}
Esempio n. 21
0
/*
 * Prepare controller for a transaction and call omap_i2c_xfer_msg
 * to do the work during IRQ processing.
 */
static int
omap_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
{
	struct omap_i2c_dev *dev = i2c_get_adapdata(adap);
	int i;
	int r;
	u16 val;

	omap_i2c_unidle(dev);

	r = omap_i2c_wait_for_bb(dev);
	schedule_timeout_uninterruptible(5);// DCY - this seems to prevent lockup with Multi-Master system
	/* If timeout, try to again check after soft reset of I2C block */
	if (WARN_ON(r == -ETIMEDOUT)) {
		/* Provide a permanent clock to recover the peripheral */
		val = omap_i2c_read_reg(dev, OMAP_I2C_SYSTEST_REG);
		val |= (OMAP_I2C_SYSTEST_ST_EN |
				OMAP_I2C_SYSTEST_FREE |
				(2 << OMAP_I2C_SYSTEST_TMODE_SHIFT));
		omap_i2c_write_reg(dev, OMAP_I2C_SYSTEST_REG, val);
		msleep(1);
		omap_i2c_init(dev);
		r = omap_i2c_wait_for_bb(dev);
	}
	if (r < 0)
		goto out;

	for (i = 0; i < num; i++) {
		r = omap_i2c_xfer_msg(adap, &msgs[i], (i == (num - 1)));
		if (r != 0)
			break;
	}

	if (r == 0)
		r = num;

	omap_i2c_wait_for_bb(dev);
out:
	omap_i2c_idle(dev);
	return r;
}
Esempio n. 22
0
static void omap_i2c_resize_fifo(struct omap_i2c_dev *dev, u8 size, bool is_rx)
{
	u16		buf;

	if (dev->flags & OMAP_I2C_FLAG_NO_FIFO)
		return;

	/*
	 * Set up notification threshold based on message size. We're doing
	 * this to try and avoid draining feature as much as possible. Whenever
	 * we have big messages to transfer (bigger than our total fifo size)
	 * then we might use draining feature to transfer the remaining bytes.
	 */

	dev->threshold = clamp(size, (u8) 1, dev->fifo_size);

	buf = omap_i2c_read_reg(dev, OMAP_I2C_BUF_REG);

	if (is_rx) {
		/* Clear RX Threshold */
		buf &= ~(0x3f << 8);
		buf |= ((dev->threshold - 1) << 8) | OMAP_I2C_BUF_RXFIF_CLR;
	} else {
		/* Clear TX Threshold */
		buf &= ~0x3f;
		buf |= (dev->threshold - 1) | OMAP_I2C_BUF_TXFIF_CLR;
	}

	omap_i2c_write_reg(dev, OMAP_I2C_BUF_REG, buf);

	if (dev->rev < OMAP_I2C_REV_ON_3630)
		dev->b_hw = 1; /* Enable hardware fixes */

	/* calculate wakeup latency constraint for MPU */
	if (dev->set_mpu_wkup_lat != NULL)
		dev->latency = (1000000 * dev->threshold) /
			(1000 * dev->speed / 8);
}
Esempio n. 23
0
static void omap_i2c_idle(struct omap_i2c_dev *dev)
{
	struct platform_device *pdev;
	struct omap_i2c_bus_platform_data *pdata;
	u16 iv;

	WARN_ON(dev->idle);

	pdev = to_platform_device(dev->dev);
	pdata = pdev->dev.platform_data;

	if (cpu_is_omap44xx() && dev->rev >= OMAP_I2C_REV_ON_4430)
		omap_i2c_write_reg(dev, OMAP_I2C_IRQENABLE_CLR, 0x6FFF);
	else
		omap_i2c_write_reg(dev, OMAP_I2C_IE_REG, 0);

	if (dev->rev < OMAP_I2C_REV_2) {
		iv = omap_i2c_read_reg(dev, OMAP_I2C_IV_REG); /* Read clears */
	}
	dev->idle = 1;

	pm_runtime_put_sync(&pdev->dev);
}
Esempio n. 24
0
static int omap_i2c_xfer_msg(struct i2c_adapter *adap,
			     struct i2c_msg *msg, int stop)
{
	struct omap_i2c_dev *dev = i2c_get_adapdata(adap);
	int r;
	u16 w;
	struct pm_qos_request_list *qos_handle = NULL;

	dev_dbg(dev->dev, "addr: 0x%04x, len: %d, flags: 0x%x, stop: %d\n",
		msg->addr, msg->len, msg->flags, stop);

	if (msg->len == 0)
		return -EINVAL;

	omap_i2c_write_reg(dev, OMAP_I2C_SA_REG, msg->addr);

	/* REVISIT: Could the STB bit of I2C_CON be used with probing? */
	dev->buf = msg->buf;
	dev->buf_len = msg->len;

	omap_i2c_write_reg(dev, OMAP_I2C_CNT_REG, dev->buf_len);

	/* Clear the FIFO Buffers */
	w = omap_i2c_read_reg(dev, OMAP_I2C_BUF_REG);
	w |= OMAP_I2C_BUF_RXFIF_CLR | OMAP_I2C_BUF_TXFIF_CLR;
	omap_i2c_write_reg(dev, OMAP_I2C_BUF_REG, w);

	init_completion(&dev->cmd_complete);
	dev->cmd_err = 0;

	w = OMAP_I2C_CON_EN | OMAP_I2C_CON_MST | OMAP_I2C_CON_STT;

	/* High speed configuration */
	if (dev->speed > 400)
		w |= OMAP_I2C_CON_OPMODE_HS;

	if (msg->flags & I2C_M_TEN)
		w |= OMAP_I2C_CON_XA;
	if (!(msg->flags & I2C_M_RD))
		w |= OMAP_I2C_CON_TRX;

	if (!dev->b_hw && stop)
		w |= OMAP_I2C_CON_STP;

	omap_i2c_write_reg(dev, OMAP_I2C_CON_REG, w);

	/*
	 * Don't write stt and stp together on some hardware.
	 */
	if (dev->b_hw && stop) {
		unsigned long delay = jiffies + OMAP_I2C_TIMEOUT;
		u16 con = omap_i2c_read_reg(dev, OMAP_I2C_CON_REG);
		while (con & OMAP_I2C_CON_STT) {
			con = omap_i2c_read_reg(dev, OMAP_I2C_CON_REG);

			/* Let the user know if i2c is in a bad state */
			if (time_after(jiffies, delay)) {
				dev_err(dev->dev, "controller timed out "
				"waiting for start condition to finish\n");
				return -ETIMEDOUT;
			}
			cpu_relax();
		}

		w |= OMAP_I2C_CON_STP;
		w &= ~OMAP_I2C_CON_STT;
		omap_i2c_write_reg(dev, OMAP_I2C_CON_REG, w);
	}

	/*
	 * REVISIT: We should abort the transfer on signals, but the bus goes
	 * into arbitration and we're currently unable to recover from it.
	 */
	if (dev->set_mpu_wkup_lat != NULL)
		dev->set_mpu_wkup_lat(&qos_handle, dev->latency);
	r = wait_for_completion_timeout(&dev->cmd_complete,
					OMAP_I2C_TIMEOUT);
	if (dev->set_mpu_wkup_lat != NULL)
		dev->set_mpu_wkup_lat(&qos_handle, -1);
	dev->buf_len = 0;
	if (r < 0)
		return r;
 /* <-- [email protected] add kernel restart function in case of i2c bus failure*/		
	if (r == 0) {
		dev_err(dev->dev, "controller timed out\n");
		omap_i2c_init(dev);
		if (strcmp(dev_name(dev->dev), "i2c_omap.1") == 0)
			i2c1_fail_counter++;
		if (strcmp(dev_name(dev->dev), "i2c_omap.2") == 0)
			i2c2_fail_counter++;
		if  ((i2c1_fail_counter > 0x20) || (i2c2_fail_counter > 0x20))
			{
			i2c1_fail_counter=0;
			i2c2_fail_counter=0;
			error_kernel_restart();
			}
		return -ETIMEDOUT;
	}
	else
	{
		if (strcmp(dev_name(dev->dev), "i2c_omap.1") == 0)
			i2c1_fail_counter=0;
		if (strcmp(dev_name(dev->dev), "i2c_omap.2") == 0)
			i2c2_fail_counter=0;	
	}
 /* [email protected] add kernel restart function in case of i2c bus failure -->*/			
	if (likely(!dev->cmd_err))
		return 0;

	/* We have an error */
/* <-- [email protected] delete arbitration mechanism for solving mg touch suspend/resume issue*/		
#if 0
	if (dev->cmd_err & (OMAP_I2C_STAT_AL | OMAP_I2C_STAT_ROVR |
			    OMAP_I2C_STAT_XUDF)) {
#else
	if (dev->cmd_err & ( OMAP_I2C_STAT_ROVR |
			    OMAP_I2C_STAT_XUDF)) {
#endif
/*  [email protected] delete arbitration mechanism for solving mg touch suspend/resume issue -->*/		
		omap_i2c_init(dev);
		return -EAGAIN;
	}

	if (dev->cmd_err & OMAP_I2C_STAT_NACK) {
		if (msg->flags & I2C_M_IGNORE_NAK)
			return 0;
		if (stop) {
			w = omap_i2c_read_reg(dev, OMAP_I2C_CON_REG);
			w |= OMAP_I2C_CON_STP;
			omap_i2c_write_reg(dev, OMAP_I2C_CON_REG, w);
		}
		return -EREMOTEIO;
	}
	return -EIO;
}

static void
omap_i2c_dpll_configure(struct omap_i2c_dev *dev,
			struct omap_i2c_bus_platform_data *pdata,
			unsigned long fclk_rate) {
	unsigned long internal_clk;

	u16 psc = 0, scll = 0, sclh = 0;
	u16 fsscll = 0, fssclh = 0, hsscll = 0, hssclh = 0;

	if ((pdata->features & I2C_HAS_FASTMODE_PLUS)
			&& dev->speed > 1000)
		internal_clk = 96000;
	else if (dev->speed > 400)
		internal_clk = 19200;
	else if (dev->speed > 100)
		internal_clk = 9600;
	else
		internal_clk = 4000;

	psc = fclk_rate / internal_clk;
	psc = psc - 1;

	if (dev->speed > 400) {
		unsigned long scl;

		/* For first phase of HS mode */
		scl = internal_clk / 400;
		fsscll = scl - (scl / 3) - 7;
		fssclh = (scl / 3) - 5;

		/* For second phase of HS mode */
		scl = fclk_rate / dev->speed;
		hsscll = scl - (scl / 3) - 7;
		hssclh = (scl / 3) - 5;
	} else if (dev->speed > 100) {
		unsigned long scl;

		/* Fast mode */
		scl = internal_clk / dev->speed;
		fsscll = scl - (scl / 3) - 7;
		fssclh = (scl / 3) - 5;
	} else {
		/* Standard mode */
		fsscll = internal_clk / (dev->speed * 2) - 7;
		fssclh = internal_clk / (dev->speed * 2) - 5;
	}
	scll = (hsscll << OMAP_I2C_SCLL_HSSCLL) | fsscll;
	sclh = (hssclh << OMAP_I2C_SCLH_HSSCLH) | fssclh;

	dev->pscstate = psc;
	dev->scllstate = scll;
	dev->sclhstate = sclh;

}
Esempio n. 25
0
static int omap_i2c_init(struct omap_i2c_dev *dev)
{
	u16 psc = 0;
	unsigned long fclk_rate = 12000000;
	unsigned long timeout;

	if (!dev->rev1) {
		omap_i2c_write_reg(dev, OMAP_I2C_SYSC_REG, OMAP_I2C_SYSC_SRST);
		/* For some reason we need to set the EN bit before the
		 * reset done bit gets set. */
		timeout = jiffies + OMAP_I2C_TIMEOUT;
		omap_i2c_write_reg(dev, OMAP_I2C_CON_REG, OMAP_I2C_CON_EN);
		while (!(omap_i2c_read_reg(dev, OMAP_I2C_SYSS_REG) &
			 OMAP_I2C_SYSS_RDONE)) {
			if (time_after(jiffies, timeout)) {
				dev_warn(dev->dev, "timeout waiting "
						"for controller reset\n");
				return -ETIMEDOUT;
			}
			msleep(1);
		}
	}
	omap_i2c_write_reg(dev, OMAP_I2C_CON_REG, 0);

	if (cpu_class_is_omap1()) {
		struct clk *armxor_ck;

		armxor_ck = clk_get(NULL, "armxor_ck");
		if (IS_ERR(armxor_ck))
			dev_warn(dev->dev, "Could not get armxor_ck\n");
		else {
			fclk_rate = clk_get_rate(armxor_ck);
			clk_put(armxor_ck);
		}
		/* TRM for 5912 says the I2C clock must be prescaled to be
		 * between 7 - 12 MHz. The XOR input clock is typically
		 * 12, 13 or 19.2 MHz. So we should have code that produces:
		 *
		 * XOR MHz	Divider		Prescaler
		 * 12		1		0
		 * 13		2		1
		 * 19.2		2		1
		 */
		if (fclk_rate > 12000000)
			psc = fclk_rate / 12000000;
	}

	/* Setup clock prescaler to obtain approx 12MHz I2C module clock: */
	omap_i2c_write_reg(dev, OMAP_I2C_PSC_REG, psc);

	/* Program desired operating rate */
	fclk_rate /= (psc + 1) * 1000;
	if (psc > 2)
		psc = 2;

	omap_i2c_write_reg(dev, OMAP_I2C_SCLL_REG,
			   fclk_rate / (clock * 2) - 7 + psc);
	omap_i2c_write_reg(dev, OMAP_I2C_SCLH_REG,
			   fclk_rate / (clock * 2) - 7 + psc);

	/* Take the I2C module out of reset: */
	omap_i2c_write_reg(dev, OMAP_I2C_CON_REG, OMAP_I2C_CON_EN);

	/* Enable interrupts */
	omap_i2c_write_reg(dev, OMAP_I2C_IE_REG,
			   (OMAP_I2C_IE_XRDY | OMAP_I2C_IE_RRDY |
			    OMAP_I2C_IE_ARDY | OMAP_I2C_IE_NACK |
			    OMAP_I2C_IE_AL));
	return 0;
}
/*
 * Low level master read/write transaction.
 */
static int omap_i2c_xfer_msg(struct i2c_adapter *adap,
			     struct i2c_msg *msg, int stop)
{
	struct omap_i2c_dev *dev = i2c_get_adapdata(adap);
	int r;
	u16 w;

	dev_dbg(dev->dev, "addr: 0x%04x, len: %d, flags: 0x%x, stop: %d\n",
		msg->addr, msg->len, msg->flags, stop);

	if (msg->len == 0)
		return -EINVAL;

	omap_i2c_write_reg(dev, OMAP_I2C_SA_REG, msg->addr);

	/* REVISIT: Could the STB bit of I2C_CON be used with probing? */
	dev->buf = msg->buf;
	dev->buf_len = msg->len;

	omap_i2c_write_reg(dev, OMAP_I2C_CNT_REG, dev->buf_len);

	/* Clear the FIFO Buffers */
	w = omap_i2c_read_reg(dev, OMAP_I2C_BUF_REG);
	w |= OMAP_I2C_BUF_RXFIF_CLR | OMAP_I2C_BUF_TXFIF_CLR;
	omap_i2c_write_reg(dev, OMAP_I2C_BUF_REG, w);

	init_completion(&dev->cmd_complete);
	dev->cmd_err = 0;

	w = OMAP_I2C_CON_EN | OMAP_I2C_CON_MST | OMAP_I2C_CON_STT;

	/* High speed configuration */
	if (dev->speed > 400)
		w |= OMAP_I2C_CON_OPMODE_HS;

	if (msg->flags & I2C_M_TEN)
		w |= OMAP_I2C_CON_XA;
	if (!(msg->flags & I2C_M_RD))
		w |= OMAP_I2C_CON_TRX;

	if (!dev->b_hw && stop)
		w |= OMAP_I2C_CON_STP;

	omap_i2c_write_reg(dev, OMAP_I2C_CON_REG, w);

	/*
	 * Don't write stt and stp together on some hardware.
	 */
	if (dev->b_hw && stop) {
		unsigned long delay = jiffies + OMAP_I2C_TIMEOUT;
		u16 con = omap_i2c_read_reg(dev, OMAP_I2C_CON_REG);
		while (con & OMAP_I2C_CON_STT) {
			con = omap_i2c_read_reg(dev, OMAP_I2C_CON_REG);

			/* Let the user know if i2c is in a bad state */
			if (time_after(jiffies, delay)) {
				dev_err(dev->dev, "controller timed out "
				"waiting for start condition to finish\n");
				return -ETIMEDOUT;
			}
			cpu_relax();
		}

		w |= OMAP_I2C_CON_STP;
		w &= ~OMAP_I2C_CON_STT;
		omap_i2c_write_reg(dev, OMAP_I2C_CON_REG, w);
	}

	/*
	 * REVISIT: We should abort the transfer on signals, but the bus goes
	 * into arbitration and we're currently unable to recover from it.
	 */
	r = wait_for_completion_timeout(&dev->cmd_complete,
					OMAP_I2C_TIMEOUT);
	dev->buf_len = 0;
	if (r < 0)
		return r;
	if (r == 0) {
		dev_err(dev->dev, "controller timed out\n");
		omap_i2c_init(dev);
		return -ETIMEDOUT;
	}

	if (likely(!dev->cmd_err))
		return 0;

	/* We have an error */
	if (dev->cmd_err & (OMAP_I2C_STAT_AL | OMAP_I2C_STAT_ROVR |
			    OMAP_I2C_STAT_XUDF)) {
		omap_i2c_init(dev);
		return -EIO;
	}

	if (dev->cmd_err & OMAP_I2C_STAT_NACK) {
		if (msg->flags & I2C_M_IGNORE_NAK)
			return 0;
		if (stop) {
			w = omap_i2c_read_reg(dev, OMAP_I2C_CON_REG);
			w |= OMAP_I2C_CON_STP;
			omap_i2c_write_reg(dev, OMAP_I2C_CON_REG, w);
		}
		return -EREMOTEIO;
	}
	return -EIO;
}
Esempio n. 27
0
static void omap_i2c_dump(struct omap_i2c_dev *dev)
{
	struct clk *fclk;
	unsigned long fclk_rate;

	dev_info(dev->dev, "sysc=0x%04x stat=0x%04x syss=0x%04x con=0x%04x\n",
		 omap_i2c_read_reg(dev, OMAP_I2C_SYSC_REG),
		 omap_i2c_read_reg(dev, OMAP_I2C_STAT_REG),
		 omap_i2c_read_reg(dev, OMAP_I2C_SYSS_REG),
		 omap_i2c_read_reg(dev, OMAP_I2C_CON_REG));

	pr_info("we=0x%04x sa=0x%04x cnt=%d buf=0x%04x bufstat=0x%04x\n",
		omap_i2c_read_reg(dev, OMAP_I2C_WE_REG),
		omap_i2c_read_reg(dev, OMAP_I2C_SA_REG),
		omap_i2c_read_reg(dev, OMAP_I2C_CNT_REG),
		omap_i2c_read_reg(dev, OMAP_I2C_BUF_REG),
		omap_i2c_read_reg(dev, OMAP_I2C_BUFSTAT_REG));

	fclk = clk_get(dev->dev, "fck");
	fclk_rate = clk_get_rate(fclk);
	clk_put(fclk);

	pr_info("fclk=%lu"
#ifdef CONFIG_ARCH_OMAP4
		" gated=%s"
#endif
		" psc=0x%04x scll=0x%04x sclh=0x%04x\n",
		fclk_rate,
#ifdef CONFIG_ARCH_OMAP4
		__raw_readl(OMAP4430_CM_L4PER_CLKSTCTRL) &
		OMAP4430_CLKACTIVITY_PER_96M_GFCLK_MASK ? "no" : "yes",
#endif
		omap_i2c_read_reg(dev, OMAP_I2C_PSC_REG),
		omap_i2c_read_reg(dev, OMAP_I2C_SCLL_REG),
		omap_i2c_read_reg(dev, OMAP_I2C_SCLH_REG));

#ifdef CONFIG_ARCH_OMAP4
	pr_info("CLKCTRL: 1:0x%08x 2:0x%08x 3:0x%08x 4:0x%08x\n",
		__raw_readl(OMAP4430_CM_L4PER_I2C1_CLKCTRL),
		__raw_readl(OMAP4430_CM_L4PER_I2C2_CLKCTRL),
		__raw_readl(OMAP4430_CM_L4PER_I2C3_CLKCTRL),
		__raw_readl(OMAP4430_CM_L4PER_I2C4_CLKCTRL));
#endif
}
static int omap_i2c_init(struct omap_i2c_dev *dev)
{
	u16 psc = 0, scll = 0, sclh = 0, buf = 0;
	u16 fsscll = 0, fssclh = 0, hsscll = 0, hssclh = 0;
	unsigned long fclk_rate = 12000000;
	unsigned long timeout;
	unsigned long internal_clk = 0;
	struct clk *fclk;

	if (dev->rev >= OMAP_I2C_REV_2) {
		/* Disable I2C controller before soft reset */
		omap_i2c_write_reg(dev, OMAP_I2C_CON_REG,
			omap_i2c_read_reg(dev, OMAP_I2C_CON_REG) &
				~(OMAP_I2C_CON_EN));

		omap_i2c_write_reg(dev, OMAP_I2C_SYSC_REG, SYSC_SOFTRESET_MASK);
		/* For some reason we need to set the EN bit before the
		 * reset done bit gets set. */
		timeout = jiffies + OMAP_I2C_TIMEOUT;
		omap_i2c_write_reg(dev, OMAP_I2C_CON_REG, OMAP_I2C_CON_EN);
		while (!(omap_i2c_read_reg(dev, OMAP_I2C_SYSS_REG) &
			 SYSS_RESETDONE_MASK)) {
			if (time_after(jiffies, timeout)) {
				dev_warn(dev->dev, "timeout waiting "
						"for controller reset\n");
				return -ETIMEDOUT;
			}
			msleep(1);
		}

		/* SYSC register is cleared by the reset; rewrite it */
		if (dev->rev == OMAP_I2C_REV_ON_2430) {

			omap_i2c_write_reg(dev, OMAP_I2C_SYSC_REG,
					   SYSC_AUTOIDLE_MASK);

		} else if (dev->rev >= OMAP_I2C_REV_ON_3430) {
			dev->syscstate = SYSC_AUTOIDLE_MASK;
			dev->syscstate |= SYSC_ENAWAKEUP_MASK;
			dev->syscstate |= (SYSC_IDLEMODE_SMART <<
			      __ffs(SYSC_SIDLEMODE_MASK));
			dev->syscstate |= (SYSC_CLOCKACTIVITY_FCLK <<
			      __ffs(SYSC_CLOCKACTIVITY_MASK));

			omap_i2c_write_reg(dev, OMAP_I2C_SYSC_REG,
							dev->syscstate);
			/*
			 * Enabling all wakup sources to stop I2C freezing on
			 * WFI instruction.
			 * REVISIT: Some wkup sources might not be needed.
			 */
			dev->westate = OMAP_I2C_WE_ALL;
			omap_i2c_write_reg(dev, OMAP_I2C_WE_REG, dev->westate);
		}
	}
	omap_i2c_write_reg(dev, OMAP_I2C_CON_REG, 0);

	if (cpu_class_is_omap1()) {
		/*
		 * The I2C functional clock is the armxor_ck, so there's
		 * no need to get "armxor_ck" separately.  Now, if OMAP2420
		 * always returns 12MHz for the functional clock, we can
		 * do this bit unconditionally.
		 */
		fclk = clk_get(dev->dev, "fck");
		fclk_rate = clk_get_rate(fclk);
		clk_put(fclk);

		/* TRM for 5912 says the I2C clock must be prescaled to be
		 * between 7 - 12 MHz. The XOR input clock is typically
		 * 12, 13 or 19.2 MHz. So we should have code that produces:
		 *
		 * XOR MHz	Divider		Prescaler
		 * 12		1		0
		 * 13		2		1
		 * 19.2		2		1
		 */
		if (fclk_rate > 12000000)
			psc = fclk_rate / 12000000;
	}

	if (!(cpu_class_is_omap1() || cpu_is_omap2420())) {

		/*
		 * HSI2C controller internal clk rate should be 19.2 Mhz for
		 * HS and for all modes on 2430. On 34xx we can use lower rate
		 * to get longer filter period for better noise suppression.
		 * The filter is iclk (fclk for HS) period.
		 */
		if (dev->speed > 400 || cpu_is_omap2430())
			internal_clk = 19200;
		else if (dev->speed > 100)
			internal_clk = 9600;
		else
			internal_clk = 4000;
		fclk = clk_get(dev->dev, "fck");
		fclk_rate = clk_get_rate(fclk) / 1000;
		clk_put(fclk);

		/* Compute prescaler divisor */
		psc = fclk_rate / internal_clk;
		psc = psc - 1;

		/* If configured for High Speed */
		if (dev->speed > 400) {
			unsigned long scl;

			/* For first phase of HS mode */
			scl = internal_clk / 400;
			fsscll = scl - (scl / 3) - 7;
			fssclh = (scl / 3) - 5;

			/* For second phase of HS mode */
			scl = fclk_rate / dev->speed;
			hsscll = scl - (scl / 3) - 7;
			hssclh = (scl / 3) - 5;
		} else if (dev->speed > 100) {
			unsigned long scl;

			/* Fast mode */
			scl = internal_clk / dev->speed;
			fsscll = scl - (scl / 3) - 7;
			fssclh = (scl / 3) - 5;
		} else {
			/* Standard mode */
			fsscll = internal_clk / (dev->speed * 2) - 7;
			fssclh = internal_clk / (dev->speed * 2) - 5;
		}
		scll = (hsscll << OMAP_I2C_SCLL_HSSCLL) | fsscll;
		sclh = (hssclh << OMAP_I2C_SCLH_HSSCLH) | fssclh;
	} else {
		/* Program desired operating rate */
		fclk_rate /= (psc + 1) * 1000;
		if (psc > 2)
			psc = 2;
		scll = fclk_rate / (dev->speed * 2) - 7 + psc;
		sclh = fclk_rate / (dev->speed * 2) - 7 + psc;
	}

	/* Setup clock prescaler to obtain approx 12MHz I2C module clock: */
	omap_i2c_write_reg(dev, OMAP_I2C_PSC_REG, psc);

	/* SCL low and high time values */
	omap_i2c_write_reg(dev, OMAP_I2C_SCLL_REG, scll);
	omap_i2c_write_reg(dev, OMAP_I2C_SCLH_REG, sclh);

	if (dev->fifo_size) {
		/* Note: setup required fifo size - 1. RTRSH and XTRSH */
		buf = (dev->fifo_size - 1) << 8 | OMAP_I2C_BUF_RXFIF_CLR |
			(dev->fifo_size - 1) | OMAP_I2C_BUF_TXFIF_CLR;
		omap_i2c_write_reg(dev, OMAP_I2C_BUF_REG, buf);
	}

	/* Take the I2C module out of reset: */
	omap_i2c_write_reg(dev, OMAP_I2C_CON_REG, OMAP_I2C_CON_EN);

	dev->errata = 0;

	if (cpu_is_omap2430() || cpu_is_omap34xx())
		dev->errata |= I2C_OMAP_ERRATA_I207;

	/* Enable interrupts */
	dev->iestate = (OMAP_I2C_IE_XRDY | OMAP_I2C_IE_RRDY |
			OMAP_I2C_IE_ARDY | OMAP_I2C_IE_NACK |
			OMAP_I2C_IE_AL)  | ((dev->fifo_size) ?
				(OMAP_I2C_IE_RDR | OMAP_I2C_IE_XDR) : 0);
	omap_i2c_write_reg(dev, OMAP_I2C_IE_REG, dev->iestate);
	if (cpu_is_omap34xx()) {
		dev->pscstate = psc;
		dev->scllstate = scll;
		dev->sclhstate = sclh;
		dev->bufstate = buf;
	}
	return 0;
}
Esempio n. 29
0
/*
 * Low level master read/write transaction.
 */
static int omap_i2c_xfer_msg(struct i2c_adapter *adap,
			     struct i2c_msg *msg, int stop)
{
	struct omap_i2c_dev *dev = i2c_get_adapdata(adap);
#ifdef OMAP_HACK
	u8 zero_byte = 0;
#endif
	int r;
	u16 w;

	dev_dbg(dev->dev, "addr: 0x%04x, len: %d, flags: 0x%x, stop: %d\n",
		msg->addr, msg->len, msg->flags, stop);

#ifndef OMAP_HACK
	if (msg->len == 0)
		return -EINVAL;

	omap_i2c_write_reg(dev, OMAP_I2C_SA_REG, msg->addr);

	/* REVISIT: Could the STB bit of I2C_CON be used with probing? */
	dev->buf = msg->buf;
	dev->buf_len = msg->len;

#else

	omap_i2c_write_reg(dev, OMAP_I2C_SA_REG, msg->addr);
	/* REVISIT: Remove this hack when we can get I2C chips from board-*.c
	 *	    files
	 * Sigh, seems we can't do zero length transactions. Thus, we
	 * can't probe for devices w/o actually sending/receiving at least
	 * a single byte. So we'll set count to 1 for the zero length
	 * transaction case and hope we don't cause grief for some
	 * arbitrary device due to random byte write/read during
	 * probes.
	 */
	if (msg->len == 0) {
		dev->buf = &zero_byte;
		dev->buf_len = 1;
	} else {
		dev->buf = msg->buf;
		dev->buf_len = msg->len;
	}
#endif

	omap_i2c_write_reg(dev, OMAP_I2C_CNT_REG, dev->buf_len);

	/* Clear the FIFO Buffers */
	w = omap_i2c_read_reg(dev, OMAP_I2C_BUF_REG);
	w |= OMAP_I2C_BUF_RXFIF_CLR | OMAP_I2C_BUF_TXFIF_CLR;
	omap_i2c_write_reg(dev, OMAP_I2C_BUF_REG, w);

	init_completion(&dev->cmd_complete);
	dev->cmd_err = 0;

	w = OMAP_I2C_CON_EN | OMAP_I2C_CON_MST | OMAP_I2C_CON_STT;

	/* High speed configuration */
	if (dev->speed > 400)
		w |= OMAP_I2C_CON_OPMODE_HS;

	if (msg->flags & I2C_M_TEN)
		w |= OMAP_I2C_CON_XA;
	if (!(msg->flags & I2C_M_RD))
		w |= OMAP_I2C_CON_TRX;

	if (!dev->b_hw && stop)
		w |= OMAP_I2C_CON_STP;

	omap_i2c_write_reg(dev, OMAP_I2C_CON_REG, w);

	if (dev->b_hw && stop) {
		/* H/w behavior: dont write stt and stp together.. */
		while (omap_i2c_read_reg(dev, OMAP_I2C_CON_REG) & OMAP_I2C_CON_STT) {
			/* Dont do anything - this will come in a couple of loops at max*/
		}
		w |= OMAP_I2C_CON_STP;
		w &= ~OMAP_I2C_CON_STT;
		omap_i2c_write_reg(dev, OMAP_I2C_CON_REG, w);
	}
	r = wait_for_completion_timeout(&dev->cmd_complete,
					OMAP_I2C_TIMEOUT);
	dev->buf_len = 0;
	if (r < 0)
		return r;
	if (r == 0) {
		dev_err(dev->dev, "controller timed out\n");
		omap_i2c_init(dev);
		return -ETIMEDOUT;
	}

	if (likely(!dev->cmd_err))
		return 0;

	/* We have an error */
	if (dev->cmd_err & (OMAP_I2C_STAT_AL | OMAP_I2C_STAT_ROVR |
			    OMAP_I2C_STAT_XUDF)) {
		omap_i2c_init(dev);
		return -EIO;
	}

	if (dev->cmd_err & OMAP_I2C_STAT_NACK) {
		if (msg->flags & I2C_M_IGNORE_NAK)
			return 0;
		if (stop) {
			w = omap_i2c_read_reg(dev, OMAP_I2C_CON_REG);
			w |= OMAP_I2C_CON_STP;
			omap_i2c_write_reg(dev, OMAP_I2C_CON_REG, w);
		}
		return -EREMOTEIO;
	}
	return -EIO;
}
Esempio n. 30
0
/*
 * Low level master read/write transaction.
 */
static int omap_i2c_xfer_msg(struct i2c_adapter *adap,
			     struct i2c_msg *msg, int stop)
{
	struct omap_i2c_dev *dev = i2c_get_adapdata(adap);
	int r;
	u16 w;

	dev_dbg(dev->dev, "addr: 0x%04x, len: %d, flags: 0x%x, stop: %d\n",
		msg->addr, msg->len, msg->flags, stop);

	if (msg->len == 0)
		return -EINVAL;

	omap_i2c_write_reg(dev, OMAP_I2C_SA_REG, msg->addr);

	/* REVISIT: Could the STB bit of I2C_CON be used with probing? */
	dev->buf = msg->buf;
	dev->buf_len = msg->len;

	omap_i2c_write_reg(dev, OMAP_I2C_CNT_REG, dev->buf_len);

	init_completion(&dev->cmd_complete);
	dev->cmd_err = 0;

	w = OMAP_I2C_CON_EN | OMAP_I2C_CON_MST | OMAP_I2C_CON_STT;
	if (msg->flags & I2C_M_TEN)
		w |= OMAP_I2C_CON_XA;
	if (!(msg->flags & I2C_M_RD))
		w |= OMAP_I2C_CON_TRX;
	if (stop)
		w |= OMAP_I2C_CON_STP;
	omap_i2c_write_reg(dev, OMAP_I2C_CON_REG, w);

	r = wait_for_completion_interruptible_timeout(&dev->cmd_complete,
						      OMAP_I2C_TIMEOUT);
	dev->buf_len = 0;
	if (r < 0)
		return r;
	if (r == 0) {
		dev_err(dev->dev, "controller timed out\n");
		omap_i2c_init(dev);
		return -ETIMEDOUT;
	}

	if (likely(!dev->cmd_err))
		return 0;

	/* We have an error */
	if (dev->cmd_err & (OMAP_I2C_STAT_AL | OMAP_I2C_STAT_ROVR |
			    OMAP_I2C_STAT_XUDF)) {
		omap_i2c_init(dev);
		return -EIO;
	}

	if (dev->cmd_err & OMAP_I2C_STAT_NACK) {
		if (msg->flags & I2C_M_IGNORE_NAK)
			return 0;
		if (stop) {
			w = omap_i2c_read_reg(dev, OMAP_I2C_CON_REG);
			w |= OMAP_I2C_CON_STP;
			omap_i2c_write_reg(dev, OMAP_I2C_CON_REG, w);
		}
		return -EREMOTEIO;
	}
	return -EIO;
}