1a10e763bSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /* ------------------------------------------------------------------------- */
31da177e4SLinus Torvalds /* i2c-iop3xx.c i2c driver algorithms for Intel XScale IOP3xx & IXP46x */
41da177e4SLinus Torvalds /* ------------------------------------------------------------------------- */
51da177e4SLinus Torvalds /* Copyright (C) 2003 Peter Milne, D-TACQ Solutions Ltd
61da177e4SLinus Torvalds * <Peter dot Milne at D hyphen TACQ dot com>
71da177e4SLinus Torvalds *
81da177e4SLinus Torvalds * With acknowledgements to i2c-algo-ibm_ocp.c by
91da177e4SLinus Torvalds * Ian DaSilva, MontaVista Software, Inc. idasilva@mvista.com
101da177e4SLinus Torvalds *
111da177e4SLinus Torvalds * And i2c-algo-pcf.c, which was created by Simon G. Vogl and Hans Berglund:
121da177e4SLinus Torvalds *
131da177e4SLinus Torvalds * Copyright (C) 1995-1997 Simon G. Vogl, 1998-2000 Hans Berglund
141da177e4SLinus Torvalds *
15fbd9a6d7SDan Williams * And which acknowledged Kyösti Mälkki <kmalkki@cc.hut.fi>,
161da177e4SLinus Torvalds * Frodo Looijaard <frodol@dds.nl>, Martin Bailey<mbailey@littlefeet-inc.com>
171da177e4SLinus Torvalds *
181da177e4SLinus Torvalds * Major cleanup by Deepak Saxena <dsaxena@plexity.net>, 01/2005:
191da177e4SLinus Torvalds *
201da177e4SLinus Torvalds * - Use driver model to pass per-chip info instead of hardcoding and #ifdefs
211da177e4SLinus Torvalds * - Use ioremap/__raw_readl/__raw_writel instead of direct dereference
221da177e4SLinus Torvalds * - Make it work with IXP46x chips
231da177e4SLinus Torvalds * - Cleanup function names, coding style, etc
241da177e4SLinus Torvalds *
2539288e1aSPeter Milne * - writing to slave address causes latchup on iop331.
2639288e1aSPeter Milne * fix: driver refuses to address self.
271da177e4SLinus Torvalds */
281da177e4SLinus Torvalds
291da177e4SLinus Torvalds #include <linux/interrupt.h>
301da177e4SLinus Torvalds #include <linux/kernel.h>
311da177e4SLinus Torvalds #include <linux/module.h>
321da177e4SLinus Torvalds #include <linux/delay.h>
331da177e4SLinus Torvalds #include <linux/slab.h>
341da177e4SLinus Torvalds #include <linux/errno.h>
35d052d1beSRussell King #include <linux/platform_device.h>
361da177e4SLinus Torvalds #include <linux/i2c.h>
3721782180SH Hartley Sweeten #include <linux/io.h>
38fdb7e884SLinus Walleij #include <linux/gpio/consumer.h>
391da177e4SLinus Torvalds
401da177e4SLinus Torvalds #include "i2c-iop3xx.h"
411da177e4SLinus Torvalds
421da177e4SLinus Torvalds /* global unit counter */
4360507095SJean Delvare static int i2c_id;
441da177e4SLinus Torvalds
451da177e4SLinus Torvalds static inline unsigned char
iic_cook_addr(struct i2c_msg * msg)461da177e4SLinus Torvalds iic_cook_addr(struct i2c_msg *msg)
471da177e4SLinus Torvalds {
481da177e4SLinus Torvalds unsigned char addr;
491da177e4SLinus Torvalds
50515e240bSWolfram Sang addr = i2c_8bit_addr_from_msg(msg);
511da177e4SLinus Torvalds
521da177e4SLinus Torvalds return addr;
531da177e4SLinus Torvalds }
541da177e4SLinus Torvalds
551da177e4SLinus Torvalds static void
iop3xx_i2c_reset(struct i2c_algo_iop3xx_data * iop3xx_adap)561da177e4SLinus Torvalds iop3xx_i2c_reset(struct i2c_algo_iop3xx_data *iop3xx_adap)
571da177e4SLinus Torvalds {
581da177e4SLinus Torvalds /* Follows devman 9.3 */
591da177e4SLinus Torvalds __raw_writel(IOP3XX_ICR_UNIT_RESET, iop3xx_adap->ioaddr + CR_OFFSET);
601da177e4SLinus Torvalds __raw_writel(IOP3XX_ISR_CLEARBITS, iop3xx_adap->ioaddr + SR_OFFSET);
611da177e4SLinus Torvalds __raw_writel(0, iop3xx_adap->ioaddr + CR_OFFSET);
621da177e4SLinus Torvalds }
631da177e4SLinus Torvalds
641da177e4SLinus Torvalds static void
iop3xx_i2c_enable(struct i2c_algo_iop3xx_data * iop3xx_adap)651da177e4SLinus Torvalds iop3xx_i2c_enable(struct i2c_algo_iop3xx_data *iop3xx_adap)
661da177e4SLinus Torvalds {
671da177e4SLinus Torvalds u32 cr = IOP3XX_ICR_GCD | IOP3XX_ICR_SCLEN | IOP3XX_ICR_UE;
681da177e4SLinus Torvalds
691da177e4SLinus Torvalds /*
701da177e4SLinus Torvalds * Every time unit enable is asserted, GPOD needs to be cleared
71fdb7e884SLinus Walleij * on IOP3XX to avoid data corruption on the bus. We use the
72fdb7e884SLinus Walleij * gpiod_set_raw_value() to make sure the 0 hits the hardware
73fdb7e884SLinus Walleij * GPOD register. These descriptors are only passed along to
74fdb7e884SLinus Walleij * the device if this is necessary.
751da177e4SLinus Torvalds */
76fdb7e884SLinus Walleij if (iop3xx_adap->gpio_scl)
77fdb7e884SLinus Walleij gpiod_set_raw_value(iop3xx_adap->gpio_scl, 0);
78fdb7e884SLinus Walleij if (iop3xx_adap->gpio_sda)
79fdb7e884SLinus Walleij gpiod_set_raw_value(iop3xx_adap->gpio_sda, 0);
80fdb7e884SLinus Walleij
811da177e4SLinus Torvalds /* NB SR bits not same position as CR IE bits :-( */
821da177e4SLinus Torvalds iop3xx_adap->SR_enabled =
831da177e4SLinus Torvalds IOP3XX_ISR_ALD | IOP3XX_ISR_BERRD |
841da177e4SLinus Torvalds IOP3XX_ISR_RXFULL | IOP3XX_ISR_TXEMPTY;
851da177e4SLinus Torvalds
861da177e4SLinus Torvalds cr |= IOP3XX_ICR_ALD_IE | IOP3XX_ICR_BERR_IE |
871da177e4SLinus Torvalds IOP3XX_ICR_RXFULL_IE | IOP3XX_ICR_TXEMPTY_IE;
881da177e4SLinus Torvalds
891da177e4SLinus Torvalds __raw_writel(cr, iop3xx_adap->ioaddr + CR_OFFSET);
901da177e4SLinus Torvalds }
911da177e4SLinus Torvalds
921da177e4SLinus Torvalds static void
iop3xx_i2c_transaction_cleanup(struct i2c_algo_iop3xx_data * iop3xx_adap)931da177e4SLinus Torvalds iop3xx_i2c_transaction_cleanup(struct i2c_algo_iop3xx_data *iop3xx_adap)
941da177e4SLinus Torvalds {
951da177e4SLinus Torvalds unsigned long cr = __raw_readl(iop3xx_adap->ioaddr + CR_OFFSET);
961da177e4SLinus Torvalds
971da177e4SLinus Torvalds cr &= ~(IOP3XX_ICR_MSTART | IOP3XX_ICR_TBYTE |
981da177e4SLinus Torvalds IOP3XX_ICR_MSTOP | IOP3XX_ICR_SCLEN);
991da177e4SLinus Torvalds
1001da177e4SLinus Torvalds __raw_writel(cr, iop3xx_adap->ioaddr + CR_OFFSET);
1011da177e4SLinus Torvalds }
1021da177e4SLinus Torvalds
1031da177e4SLinus Torvalds /*
1041da177e4SLinus Torvalds * NB: the handler has to clear the source of the interrupt!
1051da177e4SLinus Torvalds * Then it passes the SR flags of interest to BH via adap data
1061da177e4SLinus Torvalds */
1071da177e4SLinus Torvalds static irqreturn_t
iop3xx_i2c_irq_handler(int this_irq,void * dev_id)1087d12e780SDavid Howells iop3xx_i2c_irq_handler(int this_irq, void *dev_id)
1091da177e4SLinus Torvalds {
1101da177e4SLinus Torvalds struct i2c_algo_iop3xx_data *iop3xx_adap = dev_id;
1111da177e4SLinus Torvalds u32 sr = __raw_readl(iop3xx_adap->ioaddr + SR_OFFSET);
1121da177e4SLinus Torvalds
1131da177e4SLinus Torvalds if ((sr &= iop3xx_adap->SR_enabled)) {
1141da177e4SLinus Torvalds __raw_writel(sr, iop3xx_adap->ioaddr + SR_OFFSET);
1151da177e4SLinus Torvalds iop3xx_adap->SR_received |= sr;
1161da177e4SLinus Torvalds wake_up_interruptible(&iop3xx_adap->waitq);
1171da177e4SLinus Torvalds }
1181da177e4SLinus Torvalds return IRQ_HANDLED;
1191da177e4SLinus Torvalds }
1201da177e4SLinus Torvalds
1211da177e4SLinus Torvalds /* check all error conditions, clear them , report most important */
1221da177e4SLinus Torvalds static int
iop3xx_i2c_error(u32 sr)1231da177e4SLinus Torvalds iop3xx_i2c_error(u32 sr)
1241da177e4SLinus Torvalds {
1251da177e4SLinus Torvalds int rc = 0;
1261da177e4SLinus Torvalds
1271da177e4SLinus Torvalds if ((sr & IOP3XX_ISR_BERRD)) {
12887c2de5fSTian Tao if (!rc)
12987c2de5fSTian Tao rc = -I2C_ERR_BERR;
1301da177e4SLinus Torvalds }
1311da177e4SLinus Torvalds if ((sr & IOP3XX_ISR_ALD)) {
13287c2de5fSTian Tao if (!rc)
13387c2de5fSTian Tao rc = -I2C_ERR_ALD;
1341da177e4SLinus Torvalds }
1351da177e4SLinus Torvalds return rc;
1361da177e4SLinus Torvalds }
1371da177e4SLinus Torvalds
1381da177e4SLinus Torvalds static inline u32
iop3xx_i2c_get_srstat(struct i2c_algo_iop3xx_data * iop3xx_adap)1391da177e4SLinus Torvalds iop3xx_i2c_get_srstat(struct i2c_algo_iop3xx_data *iop3xx_adap)
1401da177e4SLinus Torvalds {
1411da177e4SLinus Torvalds unsigned long flags;
1421da177e4SLinus Torvalds u32 sr;
1431da177e4SLinus Torvalds
1441da177e4SLinus Torvalds spin_lock_irqsave(&iop3xx_adap->lock, flags);
1451da177e4SLinus Torvalds sr = iop3xx_adap->SR_received;
1461da177e4SLinus Torvalds iop3xx_adap->SR_received = 0;
1471da177e4SLinus Torvalds spin_unlock_irqrestore(&iop3xx_adap->lock, flags);
1481da177e4SLinus Torvalds
1491da177e4SLinus Torvalds return sr;
1501da177e4SLinus Torvalds }
1511da177e4SLinus Torvalds
1521da177e4SLinus Torvalds /*
1531da177e4SLinus Torvalds * sleep until interrupted, then recover and analyse the SR
1541da177e4SLinus Torvalds * saved by handler
1551da177e4SLinus Torvalds */
1561da177e4SLinus Torvalds typedef int (*compare_func)(unsigned test, unsigned mask);
1571da177e4SLinus Torvalds /* returns 1 on correct comparison */
1581da177e4SLinus Torvalds
1591da177e4SLinus Torvalds static int
iop3xx_i2c_wait_event(struct i2c_algo_iop3xx_data * iop3xx_adap,unsigned flags,unsigned * status,compare_func compare)1601da177e4SLinus Torvalds iop3xx_i2c_wait_event(struct i2c_algo_iop3xx_data *iop3xx_adap,
1611da177e4SLinus Torvalds unsigned flags, unsigned *status,
1621da177e4SLinus Torvalds compare_func compare)
1631da177e4SLinus Torvalds {
1641da177e4SLinus Torvalds unsigned sr = 0;
1651da177e4SLinus Torvalds int interrupted;
1661da177e4SLinus Torvalds int done;
1671da177e4SLinus Torvalds int rc = 0;
1681da177e4SLinus Torvalds
1691da177e4SLinus Torvalds do {
1701da177e4SLinus Torvalds interrupted = wait_event_interruptible_timeout (
1711da177e4SLinus Torvalds iop3xx_adap->waitq,
1721da177e4SLinus Torvalds (done = compare(sr = iop3xx_i2c_get_srstat(iop3xx_adap), flags)),
17388a8e4aaSKevin Hilman 1 * HZ
1741da177e4SLinus Torvalds );
1751da177e4SLinus Torvalds if ((rc = iop3xx_i2c_error(sr)) < 0) {
1761da177e4SLinus Torvalds *status = sr;
1771da177e4SLinus Torvalds return rc;
1781da177e4SLinus Torvalds } else if (!interrupted) {
1791da177e4SLinus Torvalds *status = sr;
1801da177e4SLinus Torvalds return -ETIMEDOUT;
1811da177e4SLinus Torvalds }
1821da177e4SLinus Torvalds } while (!done);
1831da177e4SLinus Torvalds
1841da177e4SLinus Torvalds *status = sr;
1851da177e4SLinus Torvalds
1861da177e4SLinus Torvalds return 0;
1871da177e4SLinus Torvalds }
1881da177e4SLinus Torvalds
1891da177e4SLinus Torvalds /*
1901da177e4SLinus Torvalds * Concrete compare_funcs
1911da177e4SLinus Torvalds */
1921da177e4SLinus Torvalds static int
all_bits_clear(unsigned test,unsigned mask)1931da177e4SLinus Torvalds all_bits_clear(unsigned test, unsigned mask)
1941da177e4SLinus Torvalds {
1951da177e4SLinus Torvalds return (test & mask) == 0;
1961da177e4SLinus Torvalds }
1971da177e4SLinus Torvalds
1981da177e4SLinus Torvalds static int
any_bits_set(unsigned test,unsigned mask)1991da177e4SLinus Torvalds any_bits_set(unsigned test, unsigned mask)
2001da177e4SLinus Torvalds {
2011da177e4SLinus Torvalds return (test & mask) != 0;
2021da177e4SLinus Torvalds }
2031da177e4SLinus Torvalds
2041da177e4SLinus Torvalds static int
iop3xx_i2c_wait_tx_done(struct i2c_algo_iop3xx_data * iop3xx_adap,int * status)2051da177e4SLinus Torvalds iop3xx_i2c_wait_tx_done(struct i2c_algo_iop3xx_data *iop3xx_adap, int *status)
2061da177e4SLinus Torvalds {
2071da177e4SLinus Torvalds return iop3xx_i2c_wait_event(
2081da177e4SLinus Torvalds iop3xx_adap,
2091da177e4SLinus Torvalds IOP3XX_ISR_TXEMPTY | IOP3XX_ISR_ALD | IOP3XX_ISR_BERRD,
2101da177e4SLinus Torvalds status, any_bits_set);
2111da177e4SLinus Torvalds }
2121da177e4SLinus Torvalds
2131da177e4SLinus Torvalds static int
iop3xx_i2c_wait_rx_done(struct i2c_algo_iop3xx_data * iop3xx_adap,int * status)2141da177e4SLinus Torvalds iop3xx_i2c_wait_rx_done(struct i2c_algo_iop3xx_data *iop3xx_adap, int *status)
2151da177e4SLinus Torvalds {
2161da177e4SLinus Torvalds return iop3xx_i2c_wait_event(
2171da177e4SLinus Torvalds iop3xx_adap,
2181da177e4SLinus Torvalds IOP3XX_ISR_RXFULL | IOP3XX_ISR_ALD | IOP3XX_ISR_BERRD,
2191da177e4SLinus Torvalds status, any_bits_set);
2201da177e4SLinus Torvalds }
2211da177e4SLinus Torvalds
2221da177e4SLinus Torvalds static int
iop3xx_i2c_wait_idle(struct i2c_algo_iop3xx_data * iop3xx_adap,int * status)2231da177e4SLinus Torvalds iop3xx_i2c_wait_idle(struct i2c_algo_iop3xx_data *iop3xx_adap, int *status)
2241da177e4SLinus Torvalds {
2251da177e4SLinus Torvalds return iop3xx_i2c_wait_event(
2261da177e4SLinus Torvalds iop3xx_adap, IOP3XX_ISR_UNITBUSY, status, all_bits_clear);
2271da177e4SLinus Torvalds }
2281da177e4SLinus Torvalds
2291da177e4SLinus Torvalds static int
iop3xx_i2c_send_target_addr(struct i2c_algo_iop3xx_data * iop3xx_adap,struct i2c_msg * msg)2301da177e4SLinus Torvalds iop3xx_i2c_send_target_addr(struct i2c_algo_iop3xx_data *iop3xx_adap,
2311da177e4SLinus Torvalds struct i2c_msg *msg)
2321da177e4SLinus Torvalds {
2331da177e4SLinus Torvalds unsigned long cr = __raw_readl(iop3xx_adap->ioaddr + CR_OFFSET);
2341da177e4SLinus Torvalds int status;
2351da177e4SLinus Torvalds int rc;
2361da177e4SLinus Torvalds
23739288e1aSPeter Milne /* avoid writing to my slave address (hangs on 80331),
23839288e1aSPeter Milne * forbidden in Intel developer manual
23939288e1aSPeter Milne */
24039288e1aSPeter Milne if (msg->addr == MYSAR) {
24139288e1aSPeter Milne return -EBUSY;
24239288e1aSPeter Milne }
24339288e1aSPeter Milne
2441da177e4SLinus Torvalds __raw_writel(iic_cook_addr(msg), iop3xx_adap->ioaddr + DBR_OFFSET);
2451da177e4SLinus Torvalds
2461da177e4SLinus Torvalds cr &= ~(IOP3XX_ICR_MSTOP | IOP3XX_ICR_NACK);
2471da177e4SLinus Torvalds cr |= IOP3XX_ICR_MSTART | IOP3XX_ICR_TBYTE;
2481da177e4SLinus Torvalds
2491da177e4SLinus Torvalds __raw_writel(cr, iop3xx_adap->ioaddr + CR_OFFSET);
2501da177e4SLinus Torvalds rc = iop3xx_i2c_wait_tx_done(iop3xx_adap, &status);
2511da177e4SLinus Torvalds
2521da177e4SLinus Torvalds return rc;
2531da177e4SLinus Torvalds }
2541da177e4SLinus Torvalds
2551da177e4SLinus Torvalds static int
iop3xx_i2c_write_byte(struct i2c_algo_iop3xx_data * iop3xx_adap,char byte,int stop)2561da177e4SLinus Torvalds iop3xx_i2c_write_byte(struct i2c_algo_iop3xx_data *iop3xx_adap, char byte,
2571da177e4SLinus Torvalds int stop)
2581da177e4SLinus Torvalds {
2591da177e4SLinus Torvalds unsigned long cr = __raw_readl(iop3xx_adap->ioaddr + CR_OFFSET);
2601da177e4SLinus Torvalds int status;
2611da177e4SLinus Torvalds int rc = 0;
2621da177e4SLinus Torvalds
2631da177e4SLinus Torvalds __raw_writel(byte, iop3xx_adap->ioaddr + DBR_OFFSET);
2641da177e4SLinus Torvalds cr &= ~IOP3XX_ICR_MSTART;
2651da177e4SLinus Torvalds if (stop) {
2661da177e4SLinus Torvalds cr |= IOP3XX_ICR_MSTOP;
2671da177e4SLinus Torvalds } else {
2681da177e4SLinus Torvalds cr &= ~IOP3XX_ICR_MSTOP;
2691da177e4SLinus Torvalds }
2701da177e4SLinus Torvalds cr |= IOP3XX_ICR_TBYTE;
2711da177e4SLinus Torvalds __raw_writel(cr, iop3xx_adap->ioaddr + CR_OFFSET);
2721da177e4SLinus Torvalds rc = iop3xx_i2c_wait_tx_done(iop3xx_adap, &status);
2731da177e4SLinus Torvalds
2741da177e4SLinus Torvalds return rc;
2751da177e4SLinus Torvalds }
2761da177e4SLinus Torvalds
2771da177e4SLinus Torvalds static int
iop3xx_i2c_read_byte(struct i2c_algo_iop3xx_data * iop3xx_adap,char * byte,int stop)2781da177e4SLinus Torvalds iop3xx_i2c_read_byte(struct i2c_algo_iop3xx_data *iop3xx_adap, char *byte,
2791da177e4SLinus Torvalds int stop)
2801da177e4SLinus Torvalds {
2811da177e4SLinus Torvalds unsigned long cr = __raw_readl(iop3xx_adap->ioaddr + CR_OFFSET);
2821da177e4SLinus Torvalds int status;
2831da177e4SLinus Torvalds int rc = 0;
2841da177e4SLinus Torvalds
2851da177e4SLinus Torvalds cr &= ~IOP3XX_ICR_MSTART;
2861da177e4SLinus Torvalds
2871da177e4SLinus Torvalds if (stop) {
2881da177e4SLinus Torvalds cr |= IOP3XX_ICR_MSTOP | IOP3XX_ICR_NACK;
2891da177e4SLinus Torvalds } else {
2901da177e4SLinus Torvalds cr &= ~(IOP3XX_ICR_MSTOP | IOP3XX_ICR_NACK);
2911da177e4SLinus Torvalds }
2921da177e4SLinus Torvalds cr |= IOP3XX_ICR_TBYTE;
2931da177e4SLinus Torvalds __raw_writel(cr, iop3xx_adap->ioaddr + CR_OFFSET);
2941da177e4SLinus Torvalds
2951da177e4SLinus Torvalds rc = iop3xx_i2c_wait_rx_done(iop3xx_adap, &status);
2961da177e4SLinus Torvalds
2971da177e4SLinus Torvalds *byte = __raw_readl(iop3xx_adap->ioaddr + DBR_OFFSET);
2981da177e4SLinus Torvalds
2991da177e4SLinus Torvalds return rc;
3001da177e4SLinus Torvalds }
3011da177e4SLinus Torvalds
3021da177e4SLinus Torvalds static int
iop3xx_i2c_writebytes(struct i2c_adapter * i2c_adap,const char * buf,int count)3031da177e4SLinus Torvalds iop3xx_i2c_writebytes(struct i2c_adapter *i2c_adap, const char *buf, int count)
3041da177e4SLinus Torvalds {
3051da177e4SLinus Torvalds struct i2c_algo_iop3xx_data *iop3xx_adap = i2c_adap->algo_data;
3061da177e4SLinus Torvalds int ii;
3071da177e4SLinus Torvalds int rc = 0;
3081da177e4SLinus Torvalds
3091da177e4SLinus Torvalds for (ii = 0; rc == 0 && ii != count; ++ii)
3101da177e4SLinus Torvalds rc = iop3xx_i2c_write_byte(iop3xx_adap, buf[ii], ii == count-1);
3111da177e4SLinus Torvalds return rc;
3121da177e4SLinus Torvalds }
3131da177e4SLinus Torvalds
3141da177e4SLinus Torvalds static int
iop3xx_i2c_readbytes(struct i2c_adapter * i2c_adap,char * buf,int count)3151da177e4SLinus Torvalds iop3xx_i2c_readbytes(struct i2c_adapter *i2c_adap, char *buf, int count)
3161da177e4SLinus Torvalds {
3171da177e4SLinus Torvalds struct i2c_algo_iop3xx_data *iop3xx_adap = i2c_adap->algo_data;
3181da177e4SLinus Torvalds int ii;
3191da177e4SLinus Torvalds int rc = 0;
3201da177e4SLinus Torvalds
3211da177e4SLinus Torvalds for (ii = 0; rc == 0 && ii != count; ++ii)
3221da177e4SLinus Torvalds rc = iop3xx_i2c_read_byte(iop3xx_adap, &buf[ii], ii == count-1);
3231da177e4SLinus Torvalds
3241da177e4SLinus Torvalds return rc;
3251da177e4SLinus Torvalds }
3261da177e4SLinus Torvalds
3271da177e4SLinus Torvalds /*
3281da177e4SLinus Torvalds * Description: This function implements combined transactions. Combined
3291da177e4SLinus Torvalds * transactions consist of combinations of reading and writing blocks of data.
3301da177e4SLinus Torvalds * FROM THE SAME ADDRESS
3311da177e4SLinus Torvalds * Each transfer (i.e. a read or a write) is separated by a repeated start
3321da177e4SLinus Torvalds * condition.
3331da177e4SLinus Torvalds */
3341da177e4SLinus Torvalds static int
iop3xx_i2c_handle_msg(struct i2c_adapter * i2c_adap,struct i2c_msg * pmsg)3351da177e4SLinus Torvalds iop3xx_i2c_handle_msg(struct i2c_adapter *i2c_adap, struct i2c_msg *pmsg)
3361da177e4SLinus Torvalds {
3371da177e4SLinus Torvalds struct i2c_algo_iop3xx_data *iop3xx_adap = i2c_adap->algo_data;
3381da177e4SLinus Torvalds int rc;
3391da177e4SLinus Torvalds
3401da177e4SLinus Torvalds rc = iop3xx_i2c_send_target_addr(iop3xx_adap, pmsg);
3411da177e4SLinus Torvalds if (rc < 0) {
3421da177e4SLinus Torvalds return rc;
3431da177e4SLinus Torvalds }
3441da177e4SLinus Torvalds
3451da177e4SLinus Torvalds if ((pmsg->flags&I2C_M_RD)) {
3461da177e4SLinus Torvalds return iop3xx_i2c_readbytes(i2c_adap, pmsg->buf, pmsg->len);
3471da177e4SLinus Torvalds } else {
3481da177e4SLinus Torvalds return iop3xx_i2c_writebytes(i2c_adap, pmsg->buf, pmsg->len);
3491da177e4SLinus Torvalds }
3501da177e4SLinus Torvalds }
3511da177e4SLinus Torvalds
3521da177e4SLinus Torvalds /*
3531da177e4SLinus Torvalds * master_xfer() - main read/write entry
3541da177e4SLinus Torvalds */
3551da177e4SLinus Torvalds static int
iop3xx_i2c_master_xfer(struct i2c_adapter * i2c_adap,struct i2c_msg * msgs,int num)3561da177e4SLinus Torvalds iop3xx_i2c_master_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs,
3571da177e4SLinus Torvalds int num)
3581da177e4SLinus Torvalds {
3591da177e4SLinus Torvalds struct i2c_algo_iop3xx_data *iop3xx_adap = i2c_adap->algo_data;
3601da177e4SLinus Torvalds int im = 0;
3611da177e4SLinus Torvalds int ret = 0;
3621da177e4SLinus Torvalds int status;
3631da177e4SLinus Torvalds
3641da177e4SLinus Torvalds iop3xx_i2c_wait_idle(iop3xx_adap, &status);
3651da177e4SLinus Torvalds iop3xx_i2c_reset(iop3xx_adap);
3661da177e4SLinus Torvalds iop3xx_i2c_enable(iop3xx_adap);
3671da177e4SLinus Torvalds
3681da177e4SLinus Torvalds for (im = 0; ret == 0 && im != num; im++) {
3691da177e4SLinus Torvalds ret = iop3xx_i2c_handle_msg(i2c_adap, &msgs[im]);
3701da177e4SLinus Torvalds }
3711da177e4SLinus Torvalds
3721da177e4SLinus Torvalds iop3xx_i2c_transaction_cleanup(iop3xx_adap);
3731da177e4SLinus Torvalds
3741da177e4SLinus Torvalds if (ret)
3751da177e4SLinus Torvalds return ret;
3761da177e4SLinus Torvalds
3771da177e4SLinus Torvalds return im;
3781da177e4SLinus Torvalds }
3791da177e4SLinus Torvalds
3801da177e4SLinus Torvalds static u32
iop3xx_i2c_func(struct i2c_adapter * adap)3811da177e4SLinus Torvalds iop3xx_i2c_func(struct i2c_adapter *adap)
3821da177e4SLinus Torvalds {
3831da177e4SLinus Torvalds return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
3841da177e4SLinus Torvalds }
3851da177e4SLinus Torvalds
3868f9082c5SJean Delvare static const struct i2c_algorithm iop3xx_i2c_algo = {
3871da177e4SLinus Torvalds .master_xfer = iop3xx_i2c_master_xfer,
3881da177e4SLinus Torvalds .functionality = iop3xx_i2c_func,
3891da177e4SLinus Torvalds };
3901da177e4SLinus Torvalds
391*e190a0c3SUwe Kleine-König static void
iop3xx_i2c_remove(struct platform_device * pdev)3923ae5eaecSRussell King iop3xx_i2c_remove(struct platform_device *pdev)
3931da177e4SLinus Torvalds {
3943ae5eaecSRussell King struct i2c_adapter *padapter = platform_get_drvdata(pdev);
3951da177e4SLinus Torvalds struct i2c_algo_iop3xx_data *adapter_data =
3961da177e4SLinus Torvalds (struct i2c_algo_iop3xx_data *)padapter->algo_data;
3971da177e4SLinus Torvalds struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3981da177e4SLinus Torvalds unsigned long cr = __raw_readl(adapter_data->ioaddr + CR_OFFSET);
3991da177e4SLinus Torvalds
4001da177e4SLinus Torvalds /*
4011da177e4SLinus Torvalds * Disable the actual HW unit
4021da177e4SLinus Torvalds */
4031da177e4SLinus Torvalds cr &= ~(IOP3XX_ICR_ALD_IE | IOP3XX_ICR_BERR_IE |
4041da177e4SLinus Torvalds IOP3XX_ICR_RXFULL_IE | IOP3XX_ICR_TXEMPTY_IE);
4051da177e4SLinus Torvalds __raw_writel(cr, adapter_data->ioaddr + CR_OFFSET);
4061da177e4SLinus Torvalds
407747fcc91SAaro Koskinen iounmap(adapter_data->ioaddr);
4081da177e4SLinus Torvalds release_mem_region(res->start, IOP3XX_I2C_IO_SIZE);
4091da177e4SLinus Torvalds kfree(adapter_data);
4101da177e4SLinus Torvalds kfree(padapter);
4111da177e4SLinus Torvalds }
4121da177e4SLinus Torvalds
4131da177e4SLinus Torvalds static int
iop3xx_i2c_probe(struct platform_device * pdev)4143ae5eaecSRussell King iop3xx_i2c_probe(struct platform_device *pdev)
4151da177e4SLinus Torvalds {
4161da177e4SLinus Torvalds struct resource *res;
41748944738SDavid Vrabel int ret, irq;
4181da177e4SLinus Torvalds struct i2c_adapter *new_adapter;
4191da177e4SLinus Torvalds struct i2c_algo_iop3xx_data *adapter_data;
4201da177e4SLinus Torvalds
4215263ebb5SDeepak Saxena new_adapter = kzalloc(sizeof(struct i2c_adapter), GFP_KERNEL);
4221da177e4SLinus Torvalds if (!new_adapter) {
4231da177e4SLinus Torvalds ret = -ENOMEM;
4241da177e4SLinus Torvalds goto out;
4251da177e4SLinus Torvalds }
4261da177e4SLinus Torvalds
4275263ebb5SDeepak Saxena adapter_data = kzalloc(sizeof(struct i2c_algo_iop3xx_data), GFP_KERNEL);
4281da177e4SLinus Torvalds if (!adapter_data) {
4291da177e4SLinus Torvalds ret = -ENOMEM;
4301da177e4SLinus Torvalds goto free_adapter;
4311da177e4SLinus Torvalds }
4321da177e4SLinus Torvalds
433fdb7e884SLinus Walleij adapter_data->gpio_scl = devm_gpiod_get_optional(&pdev->dev,
434fdb7e884SLinus Walleij "scl",
435fdb7e884SLinus Walleij GPIOD_ASIS);
436e6417577SKrzysztof Kozlowski if (IS_ERR(adapter_data->gpio_scl)) {
437e6417577SKrzysztof Kozlowski ret = PTR_ERR(adapter_data->gpio_scl);
438e6417577SKrzysztof Kozlowski goto free_both;
439e6417577SKrzysztof Kozlowski }
440fdb7e884SLinus Walleij adapter_data->gpio_sda = devm_gpiod_get_optional(&pdev->dev,
441fdb7e884SLinus Walleij "sda",
442fdb7e884SLinus Walleij GPIOD_ASIS);
443e6417577SKrzysztof Kozlowski if (IS_ERR(adapter_data->gpio_sda)) {
444e6417577SKrzysztof Kozlowski ret = PTR_ERR(adapter_data->gpio_sda);
445e6417577SKrzysztof Kozlowski goto free_both;
446e6417577SKrzysztof Kozlowski }
447fdb7e884SLinus Walleij
4481da177e4SLinus Torvalds res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
4491da177e4SLinus Torvalds if (!res) {
4501da177e4SLinus Torvalds ret = -ENODEV;
4511da177e4SLinus Torvalds goto free_both;
4521da177e4SLinus Torvalds }
4531da177e4SLinus Torvalds
4541da177e4SLinus Torvalds if (!request_mem_region(res->start, IOP3XX_I2C_IO_SIZE, pdev->name)) {
4551da177e4SLinus Torvalds ret = -EBUSY;
4561da177e4SLinus Torvalds goto free_both;
4571da177e4SLinus Torvalds }
4581da177e4SLinus Torvalds
4591da177e4SLinus Torvalds /* set the adapter enumeration # */
4601da177e4SLinus Torvalds adapter_data->id = i2c_id++;
4611da177e4SLinus Torvalds
462747fcc91SAaro Koskinen adapter_data->ioaddr = ioremap(res->start, IOP3XX_I2C_IO_SIZE);
4631da177e4SLinus Torvalds if (!adapter_data->ioaddr) {
4641da177e4SLinus Torvalds ret = -ENOMEM;
4651da177e4SLinus Torvalds goto release_region;
4661da177e4SLinus Torvalds }
4671da177e4SLinus Torvalds
46848944738SDavid Vrabel irq = platform_get_irq(pdev, 0);
46948944738SDavid Vrabel if (irq < 0) {
470a1299505SSergey Shtylyov ret = irq;
47148944738SDavid Vrabel goto unmap;
47248944738SDavid Vrabel }
47348944738SDavid Vrabel ret = request_irq(irq, iop3xx_i2c_irq_handler, 0,
4741da177e4SLinus Torvalds pdev->name, adapter_data);
475fbd9a6d7SDan Williams
476a1299505SSergey Shtylyov if (ret)
4771da177e4SLinus Torvalds goto unmap;
4781da177e4SLinus Torvalds
4791da177e4SLinus Torvalds memcpy(new_adapter->name, pdev->name, strlen(pdev->name));
4801da177e4SLinus Torvalds new_adapter->owner = THIS_MODULE;
4813401b2ffSJean Delvare new_adapter->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
4821da177e4SLinus Torvalds new_adapter->dev.parent = &pdev->dev;
483cac28ac2SLinus Walleij new_adapter->dev.of_node = pdev->dev.of_node;
484757ba4c6SMartin Michlmayr new_adapter->nr = pdev->id;
4851da177e4SLinus Torvalds
4861da177e4SLinus Torvalds /*
4871da177e4SLinus Torvalds * Default values...should these come in from board code?
4881da177e4SLinus Torvalds */
4898a52c6b4SJean Delvare new_adapter->timeout = HZ;
4901da177e4SLinus Torvalds new_adapter->algo = &iop3xx_i2c_algo;
4911da177e4SLinus Torvalds
4921da177e4SLinus Torvalds init_waitqueue_head(&adapter_data->waitq);
4931da177e4SLinus Torvalds spin_lock_init(&adapter_data->lock);
4941da177e4SLinus Torvalds
4951da177e4SLinus Torvalds iop3xx_i2c_reset(adapter_data);
4961da177e4SLinus Torvalds iop3xx_i2c_enable(adapter_data);
4971da177e4SLinus Torvalds
4983ae5eaecSRussell King platform_set_drvdata(pdev, new_adapter);
4991da177e4SLinus Torvalds new_adapter->algo_data = adapter_data;
5001da177e4SLinus Torvalds
501757ba4c6SMartin Michlmayr i2c_add_numbered_adapter(new_adapter);
5021da177e4SLinus Torvalds
5031da177e4SLinus Torvalds return 0;
5041da177e4SLinus Torvalds
5051da177e4SLinus Torvalds unmap:
506747fcc91SAaro Koskinen iounmap(adapter_data->ioaddr);
5071da177e4SLinus Torvalds
5081da177e4SLinus Torvalds release_region:
5091da177e4SLinus Torvalds release_mem_region(res->start, IOP3XX_I2C_IO_SIZE);
5101da177e4SLinus Torvalds
5111da177e4SLinus Torvalds free_both:
5121da177e4SLinus Torvalds kfree(adapter_data);
5131da177e4SLinus Torvalds
5141da177e4SLinus Torvalds free_adapter:
5151da177e4SLinus Torvalds kfree(new_adapter);
5161da177e4SLinus Torvalds
5171da177e4SLinus Torvalds out:
5181da177e4SLinus Torvalds return ret;
5191da177e4SLinus Torvalds }
5201da177e4SLinus Torvalds
521cac28ac2SLinus Walleij static const struct of_device_id i2c_iop3xx_match[] = {
522cac28ac2SLinus Walleij { .compatible = "intel,iop3xx-i2c", },
523cac28ac2SLinus Walleij { .compatible = "intel,ixp4xx-i2c", },
524cac28ac2SLinus Walleij {},
525cac28ac2SLinus Walleij };
526cac28ac2SLinus Walleij MODULE_DEVICE_TABLE(of, i2c_iop3xx_match);
5271da177e4SLinus Torvalds
5283ae5eaecSRussell King static struct platform_driver iop3xx_i2c_driver = {
5293ae5eaecSRussell King .probe = iop3xx_i2c_probe,
530*e190a0c3SUwe Kleine-König .remove_new = iop3xx_i2c_remove,
5313ae5eaecSRussell King .driver = {
5321da177e4SLinus Torvalds .name = "IOP3xx-I2C",
533cac28ac2SLinus Walleij .of_match_table = i2c_iop3xx_match,
5343ae5eaecSRussell King },
5351da177e4SLinus Torvalds };
5361da177e4SLinus Torvalds
537a3664b51SAxel Lin module_platform_driver(iop3xx_i2c_driver);
5381da177e4SLinus Torvalds
5391da177e4SLinus Torvalds MODULE_AUTHOR("D-TACQ Solutions Ltd <www.d-tacq.com>");
5401da177e4SLinus Torvalds MODULE_DESCRIPTION("IOP3xx iic algorithm and driver");
5411da177e4SLinus Torvalds MODULE_LICENSE("GPL");
542add8eda7SKay Sievers MODULE_ALIAS("platform:IOP3xx-I2C");
543