xref: /openbmc/linux/drivers/i2c/busses/i2c-iop3xx.c (revision a3664b51c783aaa0dde1c95334d1a670d6d54590)
11da177e4SLinus Torvalds /* ------------------------------------------------------------------------- */
21da177e4SLinus Torvalds /* i2c-iop3xx.c i2c driver algorithms for Intel XScale IOP3xx & IXP46x       */
31da177e4SLinus Torvalds /* ------------------------------------------------------------------------- */
41da177e4SLinus Torvalds /* Copyright (C) 2003 Peter Milne, D-TACQ Solutions Ltd
51da177e4SLinus Torvalds  *                    <Peter dot Milne at D hyphen TACQ dot com>
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  * With acknowledgements to i2c-algo-ibm_ocp.c by
81da177e4SLinus Torvalds  * Ian DaSilva, MontaVista Software, Inc. idasilva@mvista.com
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  * And i2c-algo-pcf.c, which was created by Simon G. Vogl and Hans Berglund:
111da177e4SLinus Torvalds  *
121da177e4SLinus Torvalds  * Copyright (C) 1995-1997 Simon G. Vogl, 1998-2000 Hans Berglund
131da177e4SLinus Torvalds  *
14fbd9a6d7SDan Williams  * And which acknowledged Kyösti Mälkki <kmalkki@cc.hut.fi>,
151da177e4SLinus Torvalds  * Frodo Looijaard <frodol@dds.nl>, Martin Bailey<mbailey@littlefeet-inc.com>
161da177e4SLinus Torvalds  *
171da177e4SLinus Torvalds  * Major cleanup by Deepak Saxena <dsaxena@plexity.net>, 01/2005:
181da177e4SLinus Torvalds  *
191da177e4SLinus Torvalds  * - Use driver model to pass per-chip info instead of hardcoding and #ifdefs
201da177e4SLinus Torvalds  * - Use ioremap/__raw_readl/__raw_writel instead of direct dereference
211da177e4SLinus Torvalds  * - Make it work with IXP46x chips
221da177e4SLinus Torvalds  * - Cleanup function names, coding style, etc
231da177e4SLinus Torvalds  *
2439288e1aSPeter Milne  * - writing to slave address causes latchup on iop331.
2539288e1aSPeter Milne  *	fix: driver refuses to address self.
2639288e1aSPeter Milne  *
271da177e4SLinus Torvalds  * This program is free software; you can redistribute it and/or modify
281da177e4SLinus Torvalds  * it under the terms of the GNU General Public License as published by
291da177e4SLinus Torvalds  * the Free Software Foundation, version 2.
301da177e4SLinus Torvalds  */
311da177e4SLinus Torvalds 
321da177e4SLinus Torvalds #include <linux/interrupt.h>
331da177e4SLinus Torvalds #include <linux/kernel.h>
341da177e4SLinus Torvalds #include <linux/module.h>
351da177e4SLinus Torvalds #include <linux/delay.h>
361da177e4SLinus Torvalds #include <linux/slab.h>
371da177e4SLinus Torvalds #include <linux/init.h>
381da177e4SLinus Torvalds #include <linux/errno.h>
39d052d1beSRussell King #include <linux/platform_device.h>
401da177e4SLinus Torvalds #include <linux/i2c.h>
4121782180SH Hartley Sweeten #include <linux/io.h>
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds #include "i2c-iop3xx.h"
441da177e4SLinus Torvalds 
451da177e4SLinus Torvalds /* global unit counter */
4660507095SJean Delvare static int i2c_id;
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds static inline unsigned char
491da177e4SLinus Torvalds iic_cook_addr(struct i2c_msg *msg)
501da177e4SLinus Torvalds {
511da177e4SLinus Torvalds 	unsigned char addr;
521da177e4SLinus Torvalds 
531da177e4SLinus Torvalds 	addr = (msg->addr << 1);
541da177e4SLinus Torvalds 
551da177e4SLinus Torvalds 	if (msg->flags & I2C_M_RD)
561da177e4SLinus Torvalds 		addr |= 1;
571da177e4SLinus Torvalds 
581da177e4SLinus Torvalds 	return addr;
591da177e4SLinus Torvalds }
601da177e4SLinus Torvalds 
611da177e4SLinus Torvalds static void
621da177e4SLinus Torvalds iop3xx_i2c_reset(struct i2c_algo_iop3xx_data *iop3xx_adap)
631da177e4SLinus Torvalds {
641da177e4SLinus Torvalds 	/* Follows devman 9.3 */
651da177e4SLinus Torvalds 	__raw_writel(IOP3XX_ICR_UNIT_RESET, iop3xx_adap->ioaddr + CR_OFFSET);
661da177e4SLinus Torvalds 	__raw_writel(IOP3XX_ISR_CLEARBITS, iop3xx_adap->ioaddr + SR_OFFSET);
671da177e4SLinus Torvalds 	__raw_writel(0, iop3xx_adap->ioaddr + CR_OFFSET);
681da177e4SLinus Torvalds }
691da177e4SLinus Torvalds 
701da177e4SLinus Torvalds static void
711da177e4SLinus Torvalds iop3xx_i2c_enable(struct i2c_algo_iop3xx_data *iop3xx_adap)
721da177e4SLinus Torvalds {
731da177e4SLinus Torvalds 	u32 cr = IOP3XX_ICR_GCD | IOP3XX_ICR_SCLEN | IOP3XX_ICR_UE;
741da177e4SLinus Torvalds 
751da177e4SLinus Torvalds 	/*
761da177e4SLinus Torvalds 	 * Every time unit enable is asserted, GPOD needs to be cleared
7772edd84aSLennert Buytenhek 	 * on IOP3XX to avoid data corruption on the bus.
781da177e4SLinus Torvalds 	 */
79588ef769SDan Williams #if defined(CONFIG_ARCH_IOP32X) || defined(CONFIG_ARCH_IOP33X)
8072edd84aSLennert Buytenhek 	if (iop3xx_adap->id == 0) {
8172edd84aSLennert Buytenhek 		gpio_line_set(IOP3XX_GPIO_LINE(7), GPIO_LOW);
8272edd84aSLennert Buytenhek 		gpio_line_set(IOP3XX_GPIO_LINE(6), GPIO_LOW);
8372edd84aSLennert Buytenhek 	} else {
8472edd84aSLennert Buytenhek 		gpio_line_set(IOP3XX_GPIO_LINE(5), GPIO_LOW);
8572edd84aSLennert Buytenhek 		gpio_line_set(IOP3XX_GPIO_LINE(4), GPIO_LOW);
8672edd84aSLennert Buytenhek 	}
871da177e4SLinus Torvalds #endif
881da177e4SLinus Torvalds 	/* NB SR bits not same position as CR IE bits :-( */
891da177e4SLinus Torvalds 	iop3xx_adap->SR_enabled =
901da177e4SLinus Torvalds 		IOP3XX_ISR_ALD | IOP3XX_ISR_BERRD |
911da177e4SLinus Torvalds 		IOP3XX_ISR_RXFULL | IOP3XX_ISR_TXEMPTY;
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds 	cr |= IOP3XX_ICR_ALD_IE | IOP3XX_ICR_BERR_IE |
941da177e4SLinus Torvalds 		IOP3XX_ICR_RXFULL_IE | IOP3XX_ICR_TXEMPTY_IE;
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds 	__raw_writel(cr, iop3xx_adap->ioaddr + CR_OFFSET);
971da177e4SLinus Torvalds }
981da177e4SLinus Torvalds 
991da177e4SLinus Torvalds static void
1001da177e4SLinus Torvalds iop3xx_i2c_transaction_cleanup(struct i2c_algo_iop3xx_data *iop3xx_adap)
1011da177e4SLinus Torvalds {
1021da177e4SLinus Torvalds 	unsigned long cr = __raw_readl(iop3xx_adap->ioaddr + CR_OFFSET);
1031da177e4SLinus Torvalds 
1041da177e4SLinus Torvalds 	cr &= ~(IOP3XX_ICR_MSTART | IOP3XX_ICR_TBYTE |
1051da177e4SLinus Torvalds 		IOP3XX_ICR_MSTOP | IOP3XX_ICR_SCLEN);
1061da177e4SLinus Torvalds 
1071da177e4SLinus Torvalds 	__raw_writel(cr, iop3xx_adap->ioaddr + CR_OFFSET);
1081da177e4SLinus Torvalds }
1091da177e4SLinus Torvalds 
1101da177e4SLinus Torvalds /*
1111da177e4SLinus Torvalds  * NB: the handler has to clear the source of the interrupt!
1121da177e4SLinus Torvalds  * Then it passes the SR flags of interest to BH via adap data
1131da177e4SLinus Torvalds  */
1141da177e4SLinus Torvalds static irqreturn_t
1157d12e780SDavid Howells iop3xx_i2c_irq_handler(int this_irq, void *dev_id)
1161da177e4SLinus Torvalds {
1171da177e4SLinus Torvalds 	struct i2c_algo_iop3xx_data *iop3xx_adap = dev_id;
1181da177e4SLinus Torvalds 	u32 sr = __raw_readl(iop3xx_adap->ioaddr + SR_OFFSET);
1191da177e4SLinus Torvalds 
1201da177e4SLinus Torvalds 	if ((sr &= iop3xx_adap->SR_enabled)) {
1211da177e4SLinus Torvalds 		__raw_writel(sr, iop3xx_adap->ioaddr + SR_OFFSET);
1221da177e4SLinus Torvalds 		iop3xx_adap->SR_received |= sr;
1231da177e4SLinus Torvalds 		wake_up_interruptible(&iop3xx_adap->waitq);
1241da177e4SLinus Torvalds 	}
1251da177e4SLinus Torvalds 	return IRQ_HANDLED;
1261da177e4SLinus Torvalds }
1271da177e4SLinus Torvalds 
1281da177e4SLinus Torvalds /* check all error conditions, clear them , report most important */
1291da177e4SLinus Torvalds static int
1301da177e4SLinus Torvalds iop3xx_i2c_error(u32 sr)
1311da177e4SLinus Torvalds {
1321da177e4SLinus Torvalds 	int rc = 0;
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds 	if ((sr & IOP3XX_ISR_BERRD)) {
1351da177e4SLinus Torvalds 		if ( !rc ) rc = -I2C_ERR_BERR;
1361da177e4SLinus Torvalds 	}
1371da177e4SLinus Torvalds 	if ((sr & IOP3XX_ISR_ALD)) {
1381da177e4SLinus Torvalds 		if ( !rc ) rc = -I2C_ERR_ALD;
1391da177e4SLinus Torvalds 	}
1401da177e4SLinus Torvalds 	return rc;
1411da177e4SLinus Torvalds }
1421da177e4SLinus Torvalds 
1431da177e4SLinus Torvalds static inline u32
1441da177e4SLinus Torvalds iop3xx_i2c_get_srstat(struct i2c_algo_iop3xx_data *iop3xx_adap)
1451da177e4SLinus Torvalds {
1461da177e4SLinus Torvalds 	unsigned long flags;
1471da177e4SLinus Torvalds 	u32 sr;
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds 	spin_lock_irqsave(&iop3xx_adap->lock, flags);
1501da177e4SLinus Torvalds 	sr = iop3xx_adap->SR_received;
1511da177e4SLinus Torvalds 	iop3xx_adap->SR_received = 0;
1521da177e4SLinus Torvalds 	spin_unlock_irqrestore(&iop3xx_adap->lock, flags);
1531da177e4SLinus Torvalds 
1541da177e4SLinus Torvalds 	return sr;
1551da177e4SLinus Torvalds }
1561da177e4SLinus Torvalds 
1571da177e4SLinus Torvalds /*
1581da177e4SLinus Torvalds  * sleep until interrupted, then recover and analyse the SR
1591da177e4SLinus Torvalds  * saved by handler
1601da177e4SLinus Torvalds  */
1611da177e4SLinus Torvalds typedef int (* compare_func)(unsigned test, unsigned mask);
1621da177e4SLinus Torvalds /* returns 1 on correct comparison */
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds static int
1651da177e4SLinus Torvalds iop3xx_i2c_wait_event(struct i2c_algo_iop3xx_data *iop3xx_adap,
1661da177e4SLinus Torvalds 			  unsigned flags, unsigned* status,
1671da177e4SLinus Torvalds 			  compare_func compare)
1681da177e4SLinus Torvalds {
1691da177e4SLinus Torvalds 	unsigned sr = 0;
1701da177e4SLinus Torvalds 	int interrupted;
1711da177e4SLinus Torvalds 	int done;
1721da177e4SLinus Torvalds 	int rc = 0;
1731da177e4SLinus Torvalds 
1741da177e4SLinus Torvalds 	do {
1751da177e4SLinus Torvalds 		interrupted = wait_event_interruptible_timeout (
1761da177e4SLinus Torvalds 			iop3xx_adap->waitq,
1771da177e4SLinus Torvalds 			(done = compare( sr = iop3xx_i2c_get_srstat(iop3xx_adap) ,flags )),
1781da177e4SLinus Torvalds 			1 * HZ;
1791da177e4SLinus Torvalds 			);
1801da177e4SLinus Torvalds 		if ((rc = iop3xx_i2c_error(sr)) < 0) {
1811da177e4SLinus Torvalds 			*status = sr;
1821da177e4SLinus Torvalds 			return rc;
1831da177e4SLinus Torvalds 		} else if (!interrupted) {
1841da177e4SLinus Torvalds 			*status = sr;
1851da177e4SLinus Torvalds 			return -ETIMEDOUT;
1861da177e4SLinus Torvalds 		}
1871da177e4SLinus Torvalds 	} while(!done);
1881da177e4SLinus Torvalds 
1891da177e4SLinus Torvalds 	*status = sr;
1901da177e4SLinus Torvalds 
1911da177e4SLinus Torvalds 	return 0;
1921da177e4SLinus Torvalds }
1931da177e4SLinus Torvalds 
1941da177e4SLinus Torvalds /*
1951da177e4SLinus Torvalds  * Concrete compare_funcs
1961da177e4SLinus Torvalds  */
1971da177e4SLinus Torvalds static int
1981da177e4SLinus Torvalds all_bits_clear(unsigned test, unsigned mask)
1991da177e4SLinus Torvalds {
2001da177e4SLinus Torvalds 	return (test & mask) == 0;
2011da177e4SLinus Torvalds }
2021da177e4SLinus Torvalds 
2031da177e4SLinus Torvalds static int
2041da177e4SLinus Torvalds any_bits_set(unsigned test, unsigned mask)
2051da177e4SLinus Torvalds {
2061da177e4SLinus Torvalds 	return (test & mask) != 0;
2071da177e4SLinus Torvalds }
2081da177e4SLinus Torvalds 
2091da177e4SLinus Torvalds static int
2101da177e4SLinus Torvalds iop3xx_i2c_wait_tx_done(struct i2c_algo_iop3xx_data *iop3xx_adap, int *status)
2111da177e4SLinus Torvalds {
2121da177e4SLinus Torvalds 	return iop3xx_i2c_wait_event(
2131da177e4SLinus Torvalds 		iop3xx_adap,
2141da177e4SLinus Torvalds 	        IOP3XX_ISR_TXEMPTY | IOP3XX_ISR_ALD | IOP3XX_ISR_BERRD,
2151da177e4SLinus Torvalds 		status, any_bits_set);
2161da177e4SLinus Torvalds }
2171da177e4SLinus Torvalds 
2181da177e4SLinus Torvalds static int
2191da177e4SLinus Torvalds iop3xx_i2c_wait_rx_done(struct i2c_algo_iop3xx_data *iop3xx_adap, int *status)
2201da177e4SLinus Torvalds {
2211da177e4SLinus Torvalds 	return iop3xx_i2c_wait_event(
2221da177e4SLinus Torvalds 		iop3xx_adap,
2231da177e4SLinus Torvalds 		IOP3XX_ISR_RXFULL | IOP3XX_ISR_ALD | IOP3XX_ISR_BERRD,
2241da177e4SLinus Torvalds 		status,	any_bits_set);
2251da177e4SLinus Torvalds }
2261da177e4SLinus Torvalds 
2271da177e4SLinus Torvalds static int
2281da177e4SLinus Torvalds iop3xx_i2c_wait_idle(struct i2c_algo_iop3xx_data *iop3xx_adap, int *status)
2291da177e4SLinus Torvalds {
2301da177e4SLinus Torvalds 	return iop3xx_i2c_wait_event(
2311da177e4SLinus Torvalds 		iop3xx_adap, IOP3XX_ISR_UNITBUSY, status, all_bits_clear);
2321da177e4SLinus Torvalds }
2331da177e4SLinus Torvalds 
2341da177e4SLinus Torvalds static int
2351da177e4SLinus Torvalds iop3xx_i2c_send_target_addr(struct i2c_algo_iop3xx_data *iop3xx_adap,
2361da177e4SLinus Torvalds 				struct i2c_msg* msg)
2371da177e4SLinus Torvalds {
2381da177e4SLinus Torvalds 	unsigned long cr = __raw_readl(iop3xx_adap->ioaddr + CR_OFFSET);
2391da177e4SLinus Torvalds 	int status;
2401da177e4SLinus Torvalds 	int rc;
2411da177e4SLinus Torvalds 
24239288e1aSPeter Milne 	/* avoid writing to my slave address (hangs on 80331),
24339288e1aSPeter Milne 	 * forbidden in Intel developer manual
24439288e1aSPeter Milne 	 */
24539288e1aSPeter Milne 	if (msg->addr == MYSAR) {
24639288e1aSPeter Milne 		return -EBUSY;
24739288e1aSPeter Milne 	}
24839288e1aSPeter Milne 
2491da177e4SLinus Torvalds 	__raw_writel(iic_cook_addr(msg), iop3xx_adap->ioaddr + DBR_OFFSET);
2501da177e4SLinus Torvalds 
2511da177e4SLinus Torvalds 	cr &= ~(IOP3XX_ICR_MSTOP | IOP3XX_ICR_NACK);
2521da177e4SLinus Torvalds 	cr |= IOP3XX_ICR_MSTART | IOP3XX_ICR_TBYTE;
2531da177e4SLinus Torvalds 
2541da177e4SLinus Torvalds 	__raw_writel(cr, iop3xx_adap->ioaddr + CR_OFFSET);
2551da177e4SLinus Torvalds 	rc = iop3xx_i2c_wait_tx_done(iop3xx_adap, &status);
2561da177e4SLinus Torvalds 
2571da177e4SLinus Torvalds 	return rc;
2581da177e4SLinus Torvalds }
2591da177e4SLinus Torvalds 
2601da177e4SLinus Torvalds static int
2611da177e4SLinus Torvalds iop3xx_i2c_write_byte(struct i2c_algo_iop3xx_data *iop3xx_adap, char byte,
2621da177e4SLinus Torvalds 				int stop)
2631da177e4SLinus Torvalds {
2641da177e4SLinus Torvalds 	unsigned long cr = __raw_readl(iop3xx_adap->ioaddr + CR_OFFSET);
2651da177e4SLinus Torvalds 	int status;
2661da177e4SLinus Torvalds 	int rc = 0;
2671da177e4SLinus Torvalds 
2681da177e4SLinus Torvalds 	__raw_writel(byte, iop3xx_adap->ioaddr + DBR_OFFSET);
2691da177e4SLinus Torvalds 	cr &= ~IOP3XX_ICR_MSTART;
2701da177e4SLinus Torvalds 	if (stop) {
2711da177e4SLinus Torvalds 		cr |= IOP3XX_ICR_MSTOP;
2721da177e4SLinus Torvalds 	} else {
2731da177e4SLinus Torvalds 		cr &= ~IOP3XX_ICR_MSTOP;
2741da177e4SLinus Torvalds 	}
2751da177e4SLinus Torvalds 	cr |= IOP3XX_ICR_TBYTE;
2761da177e4SLinus Torvalds 	__raw_writel(cr, iop3xx_adap->ioaddr + CR_OFFSET);
2771da177e4SLinus Torvalds 	rc = iop3xx_i2c_wait_tx_done(iop3xx_adap, &status);
2781da177e4SLinus Torvalds 
2791da177e4SLinus Torvalds 	return rc;
2801da177e4SLinus Torvalds }
2811da177e4SLinus Torvalds 
2821da177e4SLinus Torvalds static int
2831da177e4SLinus Torvalds iop3xx_i2c_read_byte(struct i2c_algo_iop3xx_data *iop3xx_adap, char* byte,
2841da177e4SLinus Torvalds 				int stop)
2851da177e4SLinus Torvalds {
2861da177e4SLinus Torvalds 	unsigned long cr = __raw_readl(iop3xx_adap->ioaddr + CR_OFFSET);
2871da177e4SLinus Torvalds 	int status;
2881da177e4SLinus Torvalds 	int rc = 0;
2891da177e4SLinus Torvalds 
2901da177e4SLinus Torvalds 	cr &= ~IOP3XX_ICR_MSTART;
2911da177e4SLinus Torvalds 
2921da177e4SLinus Torvalds 	if (stop) {
2931da177e4SLinus Torvalds 		cr |= IOP3XX_ICR_MSTOP | IOP3XX_ICR_NACK;
2941da177e4SLinus Torvalds 	} else {
2951da177e4SLinus Torvalds 		cr &= ~(IOP3XX_ICR_MSTOP | IOP3XX_ICR_NACK);
2961da177e4SLinus Torvalds 	}
2971da177e4SLinus Torvalds 	cr |= IOP3XX_ICR_TBYTE;
2981da177e4SLinus Torvalds 	__raw_writel(cr, iop3xx_adap->ioaddr + CR_OFFSET);
2991da177e4SLinus Torvalds 
3001da177e4SLinus Torvalds 	rc = iop3xx_i2c_wait_rx_done(iop3xx_adap, &status);
3011da177e4SLinus Torvalds 
3021da177e4SLinus Torvalds 	*byte = __raw_readl(iop3xx_adap->ioaddr + DBR_OFFSET);
3031da177e4SLinus Torvalds 
3041da177e4SLinus Torvalds 	return rc;
3051da177e4SLinus Torvalds }
3061da177e4SLinus Torvalds 
3071da177e4SLinus Torvalds static int
3081da177e4SLinus Torvalds iop3xx_i2c_writebytes(struct i2c_adapter *i2c_adap, const char *buf, int count)
3091da177e4SLinus Torvalds {
3101da177e4SLinus Torvalds 	struct i2c_algo_iop3xx_data *iop3xx_adap = i2c_adap->algo_data;
3111da177e4SLinus Torvalds 	int ii;
3121da177e4SLinus Torvalds 	int rc = 0;
3131da177e4SLinus Torvalds 
3141da177e4SLinus Torvalds 	for (ii = 0; rc == 0 && ii != count; ++ii)
3151da177e4SLinus Torvalds 		rc = iop3xx_i2c_write_byte(iop3xx_adap, buf[ii], ii==count-1);
3161da177e4SLinus Torvalds 	return rc;
3171da177e4SLinus Torvalds }
3181da177e4SLinus Torvalds 
3191da177e4SLinus Torvalds static int
3201da177e4SLinus Torvalds iop3xx_i2c_readbytes(struct i2c_adapter *i2c_adap, char *buf, int count)
3211da177e4SLinus Torvalds {
3221da177e4SLinus Torvalds 	struct i2c_algo_iop3xx_data *iop3xx_adap = i2c_adap->algo_data;
3231da177e4SLinus Torvalds 	int ii;
3241da177e4SLinus Torvalds 	int rc = 0;
3251da177e4SLinus Torvalds 
3261da177e4SLinus Torvalds 	for (ii = 0; rc == 0 && ii != count; ++ii)
3271da177e4SLinus Torvalds 		rc = iop3xx_i2c_read_byte(iop3xx_adap, &buf[ii], ii==count-1);
3281da177e4SLinus Torvalds 
3291da177e4SLinus Torvalds 	return rc;
3301da177e4SLinus Torvalds }
3311da177e4SLinus Torvalds 
3321da177e4SLinus Torvalds /*
3331da177e4SLinus Torvalds  * Description:  This function implements combined transactions.  Combined
3341da177e4SLinus Torvalds  * transactions consist of combinations of reading and writing blocks of data.
3351da177e4SLinus Torvalds  * FROM THE SAME ADDRESS
3361da177e4SLinus Torvalds  * Each transfer (i.e. a read or a write) is separated by a repeated start
3371da177e4SLinus Torvalds  * condition.
3381da177e4SLinus Torvalds  */
3391da177e4SLinus Torvalds static int
3401da177e4SLinus Torvalds iop3xx_i2c_handle_msg(struct i2c_adapter *i2c_adap, struct i2c_msg* pmsg)
3411da177e4SLinus Torvalds {
3421da177e4SLinus Torvalds 	struct i2c_algo_iop3xx_data *iop3xx_adap = i2c_adap->algo_data;
3431da177e4SLinus Torvalds 	int rc;
3441da177e4SLinus Torvalds 
3451da177e4SLinus Torvalds 	rc = iop3xx_i2c_send_target_addr(iop3xx_adap, pmsg);
3461da177e4SLinus Torvalds 	if (rc < 0) {
3471da177e4SLinus Torvalds 		return rc;
3481da177e4SLinus Torvalds 	}
3491da177e4SLinus Torvalds 
3501da177e4SLinus Torvalds 	if ((pmsg->flags&I2C_M_RD)) {
3511da177e4SLinus Torvalds 		return iop3xx_i2c_readbytes(i2c_adap, pmsg->buf, pmsg->len);
3521da177e4SLinus Torvalds 	} else {
3531da177e4SLinus Torvalds 		return iop3xx_i2c_writebytes(i2c_adap, pmsg->buf, pmsg->len);
3541da177e4SLinus Torvalds 	}
3551da177e4SLinus Torvalds }
3561da177e4SLinus Torvalds 
3571da177e4SLinus Torvalds /*
3581da177e4SLinus Torvalds  * master_xfer() - main read/write entry
3591da177e4SLinus Torvalds  */
3601da177e4SLinus Torvalds static int
3611da177e4SLinus Torvalds iop3xx_i2c_master_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs,
3621da177e4SLinus Torvalds 				int num)
3631da177e4SLinus Torvalds {
3641da177e4SLinus Torvalds 	struct i2c_algo_iop3xx_data *iop3xx_adap = i2c_adap->algo_data;
3651da177e4SLinus Torvalds 	int im = 0;
3661da177e4SLinus Torvalds 	int ret = 0;
3671da177e4SLinus Torvalds 	int status;
3681da177e4SLinus Torvalds 
3691da177e4SLinus Torvalds 	iop3xx_i2c_wait_idle(iop3xx_adap, &status);
3701da177e4SLinus Torvalds 	iop3xx_i2c_reset(iop3xx_adap);
3711da177e4SLinus Torvalds 	iop3xx_i2c_enable(iop3xx_adap);
3721da177e4SLinus Torvalds 
3731da177e4SLinus Torvalds 	for (im = 0; ret == 0 && im != num; im++) {
3741da177e4SLinus Torvalds 		ret = iop3xx_i2c_handle_msg(i2c_adap, &msgs[im]);
3751da177e4SLinus Torvalds 	}
3761da177e4SLinus Torvalds 
3771da177e4SLinus Torvalds 	iop3xx_i2c_transaction_cleanup(iop3xx_adap);
3781da177e4SLinus Torvalds 
3791da177e4SLinus Torvalds 	if(ret)
3801da177e4SLinus Torvalds 		return ret;
3811da177e4SLinus Torvalds 
3821da177e4SLinus Torvalds 	return im;
3831da177e4SLinus Torvalds }
3841da177e4SLinus Torvalds 
3851da177e4SLinus Torvalds static u32
3861da177e4SLinus Torvalds iop3xx_i2c_func(struct i2c_adapter *adap)
3871da177e4SLinus Torvalds {
3881da177e4SLinus Torvalds 	return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
3891da177e4SLinus Torvalds }
3901da177e4SLinus Torvalds 
3918f9082c5SJean Delvare static const struct i2c_algorithm iop3xx_i2c_algo = {
3921da177e4SLinus Torvalds 	.master_xfer	= iop3xx_i2c_master_xfer,
3931da177e4SLinus Torvalds 	.functionality	= iop3xx_i2c_func,
3941da177e4SLinus Torvalds };
3951da177e4SLinus Torvalds 
3961da177e4SLinus Torvalds static int
3973ae5eaecSRussell King iop3xx_i2c_remove(struct platform_device *pdev)
3981da177e4SLinus Torvalds {
3993ae5eaecSRussell King 	struct i2c_adapter *padapter = platform_get_drvdata(pdev);
4001da177e4SLinus Torvalds 	struct i2c_algo_iop3xx_data *adapter_data =
4011da177e4SLinus Torvalds 		(struct i2c_algo_iop3xx_data *)padapter->algo_data;
4021da177e4SLinus Torvalds 	struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
4031da177e4SLinus Torvalds 	unsigned long cr = __raw_readl(adapter_data->ioaddr + CR_OFFSET);
4041da177e4SLinus Torvalds 
4051da177e4SLinus Torvalds 	/*
4061da177e4SLinus Torvalds 	 * Disable the actual HW unit
4071da177e4SLinus Torvalds 	 */
4081da177e4SLinus Torvalds 	cr &= ~(IOP3XX_ICR_ALD_IE | IOP3XX_ICR_BERR_IE |
4091da177e4SLinus Torvalds 		IOP3XX_ICR_RXFULL_IE | IOP3XX_ICR_TXEMPTY_IE);
4101da177e4SLinus Torvalds 	__raw_writel(cr, adapter_data->ioaddr + CR_OFFSET);
4111da177e4SLinus Torvalds 
412747fcc91SAaro Koskinen 	iounmap(adapter_data->ioaddr);
4131da177e4SLinus Torvalds 	release_mem_region(res->start, IOP3XX_I2C_IO_SIZE);
4141da177e4SLinus Torvalds 	kfree(adapter_data);
4151da177e4SLinus Torvalds 	kfree(padapter);
4161da177e4SLinus Torvalds 
4173ae5eaecSRussell King 	platform_set_drvdata(pdev, NULL);
4181da177e4SLinus Torvalds 
4191da177e4SLinus Torvalds 	return 0;
4201da177e4SLinus Torvalds }
4211da177e4SLinus Torvalds 
4221da177e4SLinus Torvalds static int
4233ae5eaecSRussell King iop3xx_i2c_probe(struct platform_device *pdev)
4241da177e4SLinus Torvalds {
4251da177e4SLinus Torvalds 	struct resource *res;
42648944738SDavid Vrabel 	int ret, irq;
4271da177e4SLinus Torvalds 	struct i2c_adapter *new_adapter;
4281da177e4SLinus Torvalds 	struct i2c_algo_iop3xx_data *adapter_data;
4291da177e4SLinus Torvalds 
4305263ebb5SDeepak Saxena 	new_adapter = kzalloc(sizeof(struct i2c_adapter), GFP_KERNEL);
4311da177e4SLinus Torvalds 	if (!new_adapter) {
4321da177e4SLinus Torvalds 		ret = -ENOMEM;
4331da177e4SLinus Torvalds 		goto out;
4341da177e4SLinus Torvalds 	}
4351da177e4SLinus Torvalds 
4365263ebb5SDeepak Saxena 	adapter_data = kzalloc(sizeof(struct i2c_algo_iop3xx_data), GFP_KERNEL);
4371da177e4SLinus Torvalds 	if (!adapter_data) {
4381da177e4SLinus Torvalds 		ret = -ENOMEM;
4391da177e4SLinus Torvalds 		goto free_adapter;
4401da177e4SLinus Torvalds 	}
4411da177e4SLinus Torvalds 
4421da177e4SLinus Torvalds 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
4431da177e4SLinus Torvalds 	if (!res) {
4441da177e4SLinus Torvalds 		ret = -ENODEV;
4451da177e4SLinus Torvalds 		goto free_both;
4461da177e4SLinus Torvalds 	}
4471da177e4SLinus Torvalds 
4481da177e4SLinus Torvalds 	if (!request_mem_region(res->start, IOP3XX_I2C_IO_SIZE, pdev->name)) {
4491da177e4SLinus Torvalds 		ret = -EBUSY;
4501da177e4SLinus Torvalds 		goto free_both;
4511da177e4SLinus Torvalds 	}
4521da177e4SLinus Torvalds 
4531da177e4SLinus Torvalds 	/* set the adapter enumeration # */
4541da177e4SLinus Torvalds 	adapter_data->id = i2c_id++;
4551da177e4SLinus Torvalds 
456747fcc91SAaro Koskinen 	adapter_data->ioaddr = ioremap(res->start, IOP3XX_I2C_IO_SIZE);
4571da177e4SLinus Torvalds 	if (!adapter_data->ioaddr) {
4581da177e4SLinus Torvalds 		ret = -ENOMEM;
4591da177e4SLinus Torvalds 		goto release_region;
4601da177e4SLinus Torvalds 	}
4611da177e4SLinus Torvalds 
46248944738SDavid Vrabel 	irq = platform_get_irq(pdev, 0);
46348944738SDavid Vrabel 	if (irq < 0) {
46448944738SDavid Vrabel 		ret = -ENXIO;
46548944738SDavid Vrabel 		goto unmap;
46648944738SDavid Vrabel 	}
46748944738SDavid Vrabel 	ret = request_irq(irq, iop3xx_i2c_irq_handler, 0,
4681da177e4SLinus Torvalds 				pdev->name, adapter_data);
469fbd9a6d7SDan Williams 
470fbd9a6d7SDan Williams 	if (ret) {
4711da177e4SLinus Torvalds 		ret = -EIO;
4721da177e4SLinus Torvalds 		goto unmap;
4731da177e4SLinus Torvalds 	}
4741da177e4SLinus Torvalds 
4751da177e4SLinus Torvalds 	memcpy(new_adapter->name, pdev->name, strlen(pdev->name));
4761da177e4SLinus Torvalds 	new_adapter->owner = THIS_MODULE;
4773401b2ffSJean Delvare 	new_adapter->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
4781da177e4SLinus Torvalds 	new_adapter->dev.parent = &pdev->dev;
479757ba4c6SMartin Michlmayr 	new_adapter->nr = pdev->id;
4801da177e4SLinus Torvalds 
4811da177e4SLinus Torvalds 	/*
4821da177e4SLinus Torvalds 	 * Default values...should these come in from board code?
4831da177e4SLinus Torvalds 	 */
4848a52c6b4SJean Delvare 	new_adapter->timeout = HZ;
4851da177e4SLinus Torvalds 	new_adapter->algo = &iop3xx_i2c_algo;
4861da177e4SLinus Torvalds 
4871da177e4SLinus Torvalds 	init_waitqueue_head(&adapter_data->waitq);
4881da177e4SLinus Torvalds 	spin_lock_init(&adapter_data->lock);
4891da177e4SLinus Torvalds 
4901da177e4SLinus Torvalds 	iop3xx_i2c_reset(adapter_data);
4911da177e4SLinus Torvalds 	iop3xx_i2c_enable(adapter_data);
4921da177e4SLinus Torvalds 
4933ae5eaecSRussell King 	platform_set_drvdata(pdev, new_adapter);
4941da177e4SLinus Torvalds 	new_adapter->algo_data = adapter_data;
4951da177e4SLinus Torvalds 
496757ba4c6SMartin Michlmayr 	i2c_add_numbered_adapter(new_adapter);
4971da177e4SLinus Torvalds 
4981da177e4SLinus Torvalds 	return 0;
4991da177e4SLinus Torvalds 
5001da177e4SLinus Torvalds unmap:
501747fcc91SAaro Koskinen 	iounmap(adapter_data->ioaddr);
5021da177e4SLinus Torvalds 
5031da177e4SLinus Torvalds release_region:
5041da177e4SLinus Torvalds 	release_mem_region(res->start, IOP3XX_I2C_IO_SIZE);
5051da177e4SLinus Torvalds 
5061da177e4SLinus Torvalds free_both:
5071da177e4SLinus Torvalds 	kfree(adapter_data);
5081da177e4SLinus Torvalds 
5091da177e4SLinus Torvalds free_adapter:
5101da177e4SLinus Torvalds 	kfree(new_adapter);
5111da177e4SLinus Torvalds 
5121da177e4SLinus Torvalds out:
5131da177e4SLinus Torvalds 	return ret;
5141da177e4SLinus Torvalds }
5151da177e4SLinus Torvalds 
5161da177e4SLinus Torvalds 
5173ae5eaecSRussell King static struct platform_driver iop3xx_i2c_driver = {
5183ae5eaecSRussell King 	.probe		= iop3xx_i2c_probe,
5193ae5eaecSRussell King 	.remove		= iop3xx_i2c_remove,
5203ae5eaecSRussell King 	.driver		= {
5210cf36281SLaurent Riffard 		.owner	= THIS_MODULE,
5221da177e4SLinus Torvalds 		.name	= "IOP3xx-I2C",
5233ae5eaecSRussell King 	},
5241da177e4SLinus Torvalds };
5251da177e4SLinus Torvalds 
526*a3664b51SAxel Lin module_platform_driver(iop3xx_i2c_driver);
5271da177e4SLinus Torvalds 
5281da177e4SLinus Torvalds MODULE_AUTHOR("D-TACQ Solutions Ltd <www.d-tacq.com>");
5291da177e4SLinus Torvalds MODULE_DESCRIPTION("IOP3xx iic algorithm and driver");
5301da177e4SLinus Torvalds MODULE_LICENSE("GPL");
531add8eda7SKay Sievers MODULE_ALIAS("platform:IOP3xx-I2C");
532