Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* ------------------------------------------------------------------------- */ |
| 2 | /* i2c-iop3xx.c i2c driver algorithms for Intel XScale IOP3xx & IXP46x */ |
| 3 | /* ------------------------------------------------------------------------- */ |
| 4 | /* Copyright (C) 2003 Peter Milne, D-TACQ Solutions Ltd |
| 5 | * <Peter dot Milne at D hyphen TACQ dot com> |
| 6 | * |
Rob Herring | 67ac88f | 2012-07-04 14:14:16 -0500 | [diff] [blame] | 7 | * With acknowledgements to i2c-algo-ibm_ocp.c by |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * Ian DaSilva, MontaVista Software, Inc. idasilva@mvista.com |
| 9 | * |
| 10 | * And i2c-algo-pcf.c, which was created by Simon G. Vogl and Hans Berglund: |
| 11 | * |
| 12 | * Copyright (C) 1995-1997 Simon G. Vogl, 1998-2000 Hans Berglund |
Rob Herring | 67ac88f | 2012-07-04 14:14:16 -0500 | [diff] [blame] | 13 | * |
Dan Williams | fbd9a6d | 2005-11-01 22:31:12 +0000 | [diff] [blame] | 14 | * And which acknowledged Kyösti Mälkki <kmalkki@cc.hut.fi>, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | * Frodo Looijaard <frodol@dds.nl>, Martin Bailey<mbailey@littlefeet-inc.com> |
| 16 | * |
| 17 | * Major cleanup by Deepak Saxena <dsaxena@plexity.net>, 01/2005: |
| 18 | * |
| 19 | * - Use driver model to pass per-chip info instead of hardcoding and #ifdefs |
| 20 | * - Use ioremap/__raw_readl/__raw_writel instead of direct dereference |
| 21 | * - Make it work with IXP46x chips |
| 22 | * - Cleanup function names, coding style, etc |
| 23 | * |
Peter Milne | 39288e1 | 2006-07-01 17:03:20 +0200 | [diff] [blame] | 24 | * - writing to slave address causes latchup on iop331. |
| 25 | * fix: driver refuses to address self. |
| 26 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | * This program is free software; you can redistribute it and/or modify |
| 28 | * it under the terms of the GNU General Public License as published by |
| 29 | * the Free Software Foundation, version 2. |
| 30 | */ |
| 31 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include <linux/interrupt.h> |
| 33 | #include <linux/kernel.h> |
| 34 | #include <linux/module.h> |
| 35 | #include <linux/delay.h> |
| 36 | #include <linux/slab.h> |
| 37 | #include <linux/init.h> |
| 38 | #include <linux/errno.h> |
Russell King | d052d1b | 2005-10-29 19:07:23 +0100 | [diff] [blame] | 39 | #include <linux/platform_device.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #include <linux/i2c.h> |
H Hartley Sweeten | 2178218 | 2010-05-21 18:41:01 +0200 | [diff] [blame] | 41 | #include <linux/io.h> |
Rob Herring | 701eb26 | 2012-07-04 14:16:12 -0500 | [diff] [blame] | 42 | #include <linux/gpio.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
| 44 | #include "i2c-iop3xx.h" |
| 45 | |
| 46 | /* global unit counter */ |
Jean Delvare | 6050709 | 2005-09-25 16:23:07 +0200 | [diff] [blame] | 47 | static int i2c_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
Rob Herring | 67ac88f | 2012-07-04 14:14:16 -0500 | [diff] [blame] | 49 | static inline unsigned char |
| 50 | iic_cook_addr(struct i2c_msg *msg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | { |
| 52 | unsigned char addr; |
| 53 | |
| 54 | addr = (msg->addr << 1); |
| 55 | |
| 56 | if (msg->flags & I2C_M_RD) |
| 57 | addr |= 1; |
| 58 | |
Rob Herring | 67ac88f | 2012-07-04 14:14:16 -0500 | [diff] [blame] | 59 | return addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Rob Herring | 67ac88f | 2012-07-04 14:14:16 -0500 | [diff] [blame] | 62 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | iop3xx_i2c_reset(struct i2c_algo_iop3xx_data *iop3xx_adap) |
| 64 | { |
| 65 | /* Follows devman 9.3 */ |
| 66 | __raw_writel(IOP3XX_ICR_UNIT_RESET, iop3xx_adap->ioaddr + CR_OFFSET); |
| 67 | __raw_writel(IOP3XX_ISR_CLEARBITS, iop3xx_adap->ioaddr + SR_OFFSET); |
| 68 | __raw_writel(0, iop3xx_adap->ioaddr + CR_OFFSET); |
Rob Herring | 67ac88f | 2012-07-04 14:14:16 -0500 | [diff] [blame] | 69 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | |
Rob Herring | 67ac88f | 2012-07-04 14:14:16 -0500 | [diff] [blame] | 71 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | iop3xx_i2c_enable(struct i2c_algo_iop3xx_data *iop3xx_adap) |
| 73 | { |
| 74 | u32 cr = IOP3XX_ICR_GCD | IOP3XX_ICR_SCLEN | IOP3XX_ICR_UE; |
| 75 | |
Rob Herring | 67ac88f | 2012-07-04 14:14:16 -0500 | [diff] [blame] | 76 | /* |
Steven Cole | 44bbe87 | 2005-05-03 18:21:25 -0600 | [diff] [blame] | 77 | * Every time unit enable is asserted, GPOD needs to be cleared |
Lennert Buytenhek | 72edd84 | 2006-09-18 23:23:07 +0100 | [diff] [blame] | 78 | * on IOP3XX to avoid data corruption on the bus. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | */ |
Dan Williams | 588ef76 | 2007-02-13 17:12:04 +0100 | [diff] [blame] | 80 | #if defined(CONFIG_ARCH_IOP32X) || defined(CONFIG_ARCH_IOP33X) |
Lennert Buytenhek | 72edd84 | 2006-09-18 23:23:07 +0100 | [diff] [blame] | 81 | if (iop3xx_adap->id == 0) { |
Rob Herring | 701eb26 | 2012-07-04 14:16:12 -0500 | [diff] [blame] | 82 | gpio_set_value(7, 0); |
| 83 | gpio_set_value(6, 0); |
Lennert Buytenhek | 72edd84 | 2006-09-18 23:23:07 +0100 | [diff] [blame] | 84 | } else { |
Rob Herring | 701eb26 | 2012-07-04 14:16:12 -0500 | [diff] [blame] | 85 | gpio_set_value(5, 0); |
| 86 | gpio_set_value(4, 0); |
Lennert Buytenhek | 72edd84 | 2006-09-18 23:23:07 +0100 | [diff] [blame] | 87 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | #endif |
| 89 | /* NB SR bits not same position as CR IE bits :-( */ |
Rob Herring | 67ac88f | 2012-07-04 14:14:16 -0500 | [diff] [blame] | 90 | iop3xx_adap->SR_enabled = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | IOP3XX_ISR_ALD | IOP3XX_ISR_BERRD | |
| 92 | IOP3XX_ISR_RXFULL | IOP3XX_ISR_TXEMPTY; |
| 93 | |
| 94 | cr |= IOP3XX_ICR_ALD_IE | IOP3XX_ICR_BERR_IE | |
| 95 | IOP3XX_ICR_RXFULL_IE | IOP3XX_ICR_TXEMPTY_IE; |
| 96 | |
| 97 | __raw_writel(cr, iop3xx_adap->ioaddr + CR_OFFSET); |
| 98 | } |
| 99 | |
Rob Herring | 67ac88f | 2012-07-04 14:14:16 -0500 | [diff] [blame] | 100 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | iop3xx_i2c_transaction_cleanup(struct i2c_algo_iop3xx_data *iop3xx_adap) |
| 102 | { |
| 103 | unsigned long cr = __raw_readl(iop3xx_adap->ioaddr + CR_OFFSET); |
Rob Herring | 67ac88f | 2012-07-04 14:14:16 -0500 | [diff] [blame] | 104 | |
| 105 | cr &= ~(IOP3XX_ICR_MSTART | IOP3XX_ICR_TBYTE | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | IOP3XX_ICR_MSTOP | IOP3XX_ICR_SCLEN); |
| 107 | |
| 108 | __raw_writel(cr, iop3xx_adap->ioaddr + CR_OFFSET); |
| 109 | } |
| 110 | |
Rob Herring | 67ac88f | 2012-07-04 14:14:16 -0500 | [diff] [blame] | 111 | /* |
| 112 | * NB: the handler has to clear the source of the interrupt! |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | * Then it passes the SR flags of interest to BH via adap data |
| 114 | */ |
Rob Herring | 67ac88f | 2012-07-04 14:14:16 -0500 | [diff] [blame] | 115 | static irqreturn_t |
| 116 | iop3xx_i2c_irq_handler(int this_irq, void *dev_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | { |
| 118 | struct i2c_algo_iop3xx_data *iop3xx_adap = dev_id; |
| 119 | u32 sr = __raw_readl(iop3xx_adap->ioaddr + SR_OFFSET); |
| 120 | |
| 121 | if ((sr &= iop3xx_adap->SR_enabled)) { |
| 122 | __raw_writel(sr, iop3xx_adap->ioaddr + SR_OFFSET); |
| 123 | iop3xx_adap->SR_received |= sr; |
| 124 | wake_up_interruptible(&iop3xx_adap->waitq); |
| 125 | } |
| 126 | return IRQ_HANDLED; |
| 127 | } |
| 128 | |
| 129 | /* check all error conditions, clear them , report most important */ |
Rob Herring | 67ac88f | 2012-07-04 14:14:16 -0500 | [diff] [blame] | 130 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | iop3xx_i2c_error(u32 sr) |
| 132 | { |
| 133 | int rc = 0; |
| 134 | |
| 135 | if ((sr & IOP3XX_ISR_BERRD)) { |
| 136 | if ( !rc ) rc = -I2C_ERR_BERR; |
| 137 | } |
| 138 | if ((sr & IOP3XX_ISR_ALD)) { |
Rob Herring | 67ac88f | 2012-07-04 14:14:16 -0500 | [diff] [blame] | 139 | if ( !rc ) rc = -I2C_ERR_ALD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | } |
Rob Herring | 67ac88f | 2012-07-04 14:14:16 -0500 | [diff] [blame] | 141 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Rob Herring | 67ac88f | 2012-07-04 14:14:16 -0500 | [diff] [blame] | 144 | static inline u32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | iop3xx_i2c_get_srstat(struct i2c_algo_iop3xx_data *iop3xx_adap) |
| 146 | { |
| 147 | unsigned long flags; |
| 148 | u32 sr; |
| 149 | |
| 150 | spin_lock_irqsave(&iop3xx_adap->lock, flags); |
| 151 | sr = iop3xx_adap->SR_received; |
| 152 | iop3xx_adap->SR_received = 0; |
| 153 | spin_unlock_irqrestore(&iop3xx_adap->lock, flags); |
| 154 | |
| 155 | return sr; |
| 156 | } |
| 157 | |
| 158 | /* |
| 159 | * sleep until interrupted, then recover and analyse the SR |
| 160 | * saved by handler |
| 161 | */ |
| 162 | typedef int (* compare_func)(unsigned test, unsigned mask); |
| 163 | /* returns 1 on correct comparison */ |
| 164 | |
Rob Herring | 67ac88f | 2012-07-04 14:14:16 -0500 | [diff] [blame] | 165 | static int |
| 166 | iop3xx_i2c_wait_event(struct i2c_algo_iop3xx_data *iop3xx_adap, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | unsigned flags, unsigned* status, |
| 168 | compare_func compare) |
| 169 | { |
| 170 | unsigned sr = 0; |
| 171 | int interrupted; |
| 172 | int done; |
| 173 | int rc = 0; |
| 174 | |
| 175 | do { |
| 176 | interrupted = wait_event_interruptible_timeout ( |
| 177 | iop3xx_adap->waitq, |
Dan Williams | fbd9a6d | 2005-11-01 22:31:12 +0000 | [diff] [blame] | 178 | (done = compare( sr = iop3xx_i2c_get_srstat(iop3xx_adap) ,flags )), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | 1 * HZ; |
| 180 | ); |
| 181 | if ((rc = iop3xx_i2c_error(sr)) < 0) { |
| 182 | *status = sr; |
| 183 | return rc; |
| 184 | } else if (!interrupted) { |
| 185 | *status = sr; |
| 186 | return -ETIMEDOUT; |
| 187 | } |
| 188 | } while(!done); |
| 189 | |
| 190 | *status = sr; |
| 191 | |
| 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | /* |
Rob Herring | 67ac88f | 2012-07-04 14:14:16 -0500 | [diff] [blame] | 196 | * Concrete compare_funcs |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | */ |
Rob Herring | 67ac88f | 2012-07-04 14:14:16 -0500 | [diff] [blame] | 198 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | all_bits_clear(unsigned test, unsigned mask) |
| 200 | { |
| 201 | return (test & mask) == 0; |
| 202 | } |
| 203 | |
Rob Herring | 67ac88f | 2012-07-04 14:14:16 -0500 | [diff] [blame] | 204 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | any_bits_set(unsigned test, unsigned mask) |
| 206 | { |
| 207 | return (test & mask) != 0; |
| 208 | } |
| 209 | |
Rob Herring | 67ac88f | 2012-07-04 14:14:16 -0500 | [diff] [blame] | 210 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | iop3xx_i2c_wait_tx_done(struct i2c_algo_iop3xx_data *iop3xx_adap, int *status) |
| 212 | { |
Rob Herring | 67ac88f | 2012-07-04 14:14:16 -0500 | [diff] [blame] | 213 | return iop3xx_i2c_wait_event( |
| 214 | iop3xx_adap, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | IOP3XX_ISR_TXEMPTY | IOP3XX_ISR_ALD | IOP3XX_ISR_BERRD, |
| 216 | status, any_bits_set); |
| 217 | } |
| 218 | |
Rob Herring | 67ac88f | 2012-07-04 14:14:16 -0500 | [diff] [blame] | 219 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | iop3xx_i2c_wait_rx_done(struct i2c_algo_iop3xx_data *iop3xx_adap, int *status) |
| 221 | { |
Rob Herring | 67ac88f | 2012-07-04 14:14:16 -0500 | [diff] [blame] | 222 | return iop3xx_i2c_wait_event( |
| 223 | iop3xx_adap, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | IOP3XX_ISR_RXFULL | IOP3XX_ISR_ALD | IOP3XX_ISR_BERRD, |
| 225 | status, any_bits_set); |
| 226 | } |
| 227 | |
Rob Herring | 67ac88f | 2012-07-04 14:14:16 -0500 | [diff] [blame] | 228 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | iop3xx_i2c_wait_idle(struct i2c_algo_iop3xx_data *iop3xx_adap, int *status) |
| 230 | { |
Rob Herring | 67ac88f | 2012-07-04 14:14:16 -0500 | [diff] [blame] | 231 | return iop3xx_i2c_wait_event( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | iop3xx_adap, IOP3XX_ISR_UNITBUSY, status, all_bits_clear); |
| 233 | } |
| 234 | |
Rob Herring | 67ac88f | 2012-07-04 14:14:16 -0500 | [diff] [blame] | 235 | static int |
| 236 | iop3xx_i2c_send_target_addr(struct i2c_algo_iop3xx_data *iop3xx_adap, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | struct i2c_msg* msg) |
| 238 | { |
| 239 | unsigned long cr = __raw_readl(iop3xx_adap->ioaddr + CR_OFFSET); |
| 240 | int status; |
| 241 | int rc; |
| 242 | |
Peter Milne | 39288e1 | 2006-07-01 17:03:20 +0200 | [diff] [blame] | 243 | /* avoid writing to my slave address (hangs on 80331), |
| 244 | * forbidden in Intel developer manual |
| 245 | */ |
| 246 | if (msg->addr == MYSAR) { |
| 247 | return -EBUSY; |
| 248 | } |
| 249 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | __raw_writel(iic_cook_addr(msg), iop3xx_adap->ioaddr + DBR_OFFSET); |
Rob Herring | 67ac88f | 2012-07-04 14:14:16 -0500 | [diff] [blame] | 251 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | cr &= ~(IOP3XX_ICR_MSTOP | IOP3XX_ICR_NACK); |
| 253 | cr |= IOP3XX_ICR_MSTART | IOP3XX_ICR_TBYTE; |
| 254 | |
| 255 | __raw_writel(cr, iop3xx_adap->ioaddr + CR_OFFSET); |
| 256 | rc = iop3xx_i2c_wait_tx_done(iop3xx_adap, &status); |
| 257 | |
| 258 | return rc; |
| 259 | } |
| 260 | |
Rob Herring | 67ac88f | 2012-07-04 14:14:16 -0500 | [diff] [blame] | 261 | static int |
| 262 | iop3xx_i2c_write_byte(struct i2c_algo_iop3xx_data *iop3xx_adap, char byte, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | int stop) |
| 264 | { |
| 265 | unsigned long cr = __raw_readl(iop3xx_adap->ioaddr + CR_OFFSET); |
| 266 | int status; |
| 267 | int rc = 0; |
| 268 | |
| 269 | __raw_writel(byte, iop3xx_adap->ioaddr + DBR_OFFSET); |
| 270 | cr &= ~IOP3XX_ICR_MSTART; |
| 271 | if (stop) { |
| 272 | cr |= IOP3XX_ICR_MSTOP; |
| 273 | } else { |
| 274 | cr &= ~IOP3XX_ICR_MSTOP; |
| 275 | } |
| 276 | cr |= IOP3XX_ICR_TBYTE; |
| 277 | __raw_writel(cr, iop3xx_adap->ioaddr + CR_OFFSET); |
| 278 | rc = iop3xx_i2c_wait_tx_done(iop3xx_adap, &status); |
| 279 | |
| 280 | return rc; |
Rob Herring | 67ac88f | 2012-07-04 14:14:16 -0500 | [diff] [blame] | 281 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | |
Rob Herring | 67ac88f | 2012-07-04 14:14:16 -0500 | [diff] [blame] | 283 | static int |
| 284 | iop3xx_i2c_read_byte(struct i2c_algo_iop3xx_data *iop3xx_adap, char* byte, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | int stop) |
| 286 | { |
| 287 | unsigned long cr = __raw_readl(iop3xx_adap->ioaddr + CR_OFFSET); |
| 288 | int status; |
| 289 | int rc = 0; |
| 290 | |
| 291 | cr &= ~IOP3XX_ICR_MSTART; |
| 292 | |
| 293 | if (stop) { |
| 294 | cr |= IOP3XX_ICR_MSTOP | IOP3XX_ICR_NACK; |
| 295 | } else { |
| 296 | cr &= ~(IOP3XX_ICR_MSTOP | IOP3XX_ICR_NACK); |
| 297 | } |
| 298 | cr |= IOP3XX_ICR_TBYTE; |
| 299 | __raw_writel(cr, iop3xx_adap->ioaddr + CR_OFFSET); |
| 300 | |
| 301 | rc = iop3xx_i2c_wait_rx_done(iop3xx_adap, &status); |
| 302 | |
| 303 | *byte = __raw_readl(iop3xx_adap->ioaddr + DBR_OFFSET); |
| 304 | |
| 305 | return rc; |
| 306 | } |
| 307 | |
Rob Herring | 67ac88f | 2012-07-04 14:14:16 -0500 | [diff] [blame] | 308 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | iop3xx_i2c_writebytes(struct i2c_adapter *i2c_adap, const char *buf, int count) |
| 310 | { |
| 311 | struct i2c_algo_iop3xx_data *iop3xx_adap = i2c_adap->algo_data; |
| 312 | int ii; |
| 313 | int rc = 0; |
| 314 | |
Rob Herring | 67ac88f | 2012-07-04 14:14:16 -0500 | [diff] [blame] | 315 | for (ii = 0; rc == 0 && ii != count; ++ii) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | rc = iop3xx_i2c_write_byte(iop3xx_adap, buf[ii], ii==count-1); |
| 317 | return rc; |
| 318 | } |
| 319 | |
Rob Herring | 67ac88f | 2012-07-04 14:14:16 -0500 | [diff] [blame] | 320 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | iop3xx_i2c_readbytes(struct i2c_adapter *i2c_adap, char *buf, int count) |
| 322 | { |
| 323 | struct i2c_algo_iop3xx_data *iop3xx_adap = i2c_adap->algo_data; |
| 324 | int ii; |
| 325 | int rc = 0; |
| 326 | |
| 327 | for (ii = 0; rc == 0 && ii != count; ++ii) |
| 328 | rc = iop3xx_i2c_read_byte(iop3xx_adap, &buf[ii], ii==count-1); |
Rob Herring | 67ac88f | 2012-07-04 14:14:16 -0500 | [diff] [blame] | 329 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | return rc; |
| 331 | } |
| 332 | |
| 333 | /* |
| 334 | * Description: This function implements combined transactions. Combined |
| 335 | * transactions consist of combinations of reading and writing blocks of data. |
| 336 | * FROM THE SAME ADDRESS |
| 337 | * Each transfer (i.e. a read or a write) is separated by a repeated start |
| 338 | * condition. |
| 339 | */ |
Rob Herring | 67ac88f | 2012-07-04 14:14:16 -0500 | [diff] [blame] | 340 | static int |
| 341 | iop3xx_i2c_handle_msg(struct i2c_adapter *i2c_adap, struct i2c_msg* pmsg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | { |
| 343 | struct i2c_algo_iop3xx_data *iop3xx_adap = i2c_adap->algo_data; |
| 344 | int rc; |
| 345 | |
| 346 | rc = iop3xx_i2c_send_target_addr(iop3xx_adap, pmsg); |
| 347 | if (rc < 0) { |
| 348 | return rc; |
| 349 | } |
| 350 | |
| 351 | if ((pmsg->flags&I2C_M_RD)) { |
| 352 | return iop3xx_i2c_readbytes(i2c_adap, pmsg->buf, pmsg->len); |
| 353 | } else { |
| 354 | return iop3xx_i2c_writebytes(i2c_adap, pmsg->buf, pmsg->len); |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | /* |
| 359 | * master_xfer() - main read/write entry |
| 360 | */ |
Rob Herring | 67ac88f | 2012-07-04 14:14:16 -0500 | [diff] [blame] | 361 | static int |
| 362 | iop3xx_i2c_master_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | int num) |
| 364 | { |
| 365 | struct i2c_algo_iop3xx_data *iop3xx_adap = i2c_adap->algo_data; |
| 366 | int im = 0; |
| 367 | int ret = 0; |
| 368 | int status; |
| 369 | |
| 370 | iop3xx_i2c_wait_idle(iop3xx_adap, &status); |
| 371 | iop3xx_i2c_reset(iop3xx_adap); |
| 372 | iop3xx_i2c_enable(iop3xx_adap); |
| 373 | |
| 374 | for (im = 0; ret == 0 && im != num; im++) { |
| 375 | ret = iop3xx_i2c_handle_msg(i2c_adap, &msgs[im]); |
| 376 | } |
| 377 | |
| 378 | iop3xx_i2c_transaction_cleanup(iop3xx_adap); |
Rob Herring | 67ac88f | 2012-07-04 14:14:16 -0500 | [diff] [blame] | 379 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | if(ret) |
| 381 | return ret; |
| 382 | |
Rob Herring | 67ac88f | 2012-07-04 14:14:16 -0500 | [diff] [blame] | 383 | return im; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | } |
| 385 | |
Rob Herring | 67ac88f | 2012-07-04 14:14:16 -0500 | [diff] [blame] | 386 | static u32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | iop3xx_i2c_func(struct i2c_adapter *adap) |
| 388 | { |
| 389 | return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL; |
| 390 | } |
| 391 | |
Jean Delvare | 8f9082c | 2006-09-03 22:39:46 +0200 | [diff] [blame] | 392 | static const struct i2c_algorithm iop3xx_i2c_algo = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | .master_xfer = iop3xx_i2c_master_xfer, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | .functionality = iop3xx_i2c_func, |
| 395 | }; |
| 396 | |
Rob Herring | 67ac88f | 2012-07-04 14:14:16 -0500 | [diff] [blame] | 397 | static int |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 398 | iop3xx_i2c_remove(struct platform_device *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 400 | struct i2c_adapter *padapter = platform_get_drvdata(pdev); |
Rob Herring | 67ac88f | 2012-07-04 14:14:16 -0500 | [diff] [blame] | 401 | struct i2c_algo_iop3xx_data *adapter_data = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | (struct i2c_algo_iop3xx_data *)padapter->algo_data; |
| 403 | struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 404 | unsigned long cr = __raw_readl(adapter_data->ioaddr + CR_OFFSET); |
| 405 | |
| 406 | /* |
| 407 | * Disable the actual HW unit |
| 408 | */ |
| 409 | cr &= ~(IOP3XX_ICR_ALD_IE | IOP3XX_ICR_BERR_IE | |
| 410 | IOP3XX_ICR_RXFULL_IE | IOP3XX_ICR_TXEMPTY_IE); |
| 411 | __raw_writel(cr, adapter_data->ioaddr + CR_OFFSET); |
| 412 | |
Aaro Koskinen | 747fcc9 | 2010-11-29 12:33:39 +0200 | [diff] [blame] | 413 | iounmap(adapter_data->ioaddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | release_mem_region(res->start, IOP3XX_I2C_IO_SIZE); |
| 415 | kfree(adapter_data); |
| 416 | kfree(padapter); |
| 417 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | return 0; |
| 419 | } |
| 420 | |
Rob Herring | 67ac88f | 2012-07-04 14:14:16 -0500 | [diff] [blame] | 421 | static int |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 422 | iop3xx_i2c_probe(struct platform_device *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | struct resource *res; |
David Vrabel | 4894473 | 2006-01-19 17:56:29 +0000 | [diff] [blame] | 425 | int ret, irq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | struct i2c_adapter *new_adapter; |
| 427 | struct i2c_algo_iop3xx_data *adapter_data; |
| 428 | |
Deepak Saxena | 5263ebb | 2005-10-17 23:09:43 +0200 | [diff] [blame] | 429 | new_adapter = kzalloc(sizeof(struct i2c_adapter), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | if (!new_adapter) { |
| 431 | ret = -ENOMEM; |
| 432 | goto out; |
| 433 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | |
Deepak Saxena | 5263ebb | 2005-10-17 23:09:43 +0200 | [diff] [blame] | 435 | adapter_data = kzalloc(sizeof(struct i2c_algo_iop3xx_data), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | if (!adapter_data) { |
| 437 | ret = -ENOMEM; |
| 438 | goto free_adapter; |
| 439 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | |
| 441 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 442 | if (!res) { |
| 443 | ret = -ENODEV; |
| 444 | goto free_both; |
| 445 | } |
| 446 | |
| 447 | if (!request_mem_region(res->start, IOP3XX_I2C_IO_SIZE, pdev->name)) { |
| 448 | ret = -EBUSY; |
| 449 | goto free_both; |
| 450 | } |
| 451 | |
| 452 | /* set the adapter enumeration # */ |
| 453 | adapter_data->id = i2c_id++; |
| 454 | |
Aaro Koskinen | 747fcc9 | 2010-11-29 12:33:39 +0200 | [diff] [blame] | 455 | adapter_data->ioaddr = ioremap(res->start, IOP3XX_I2C_IO_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | if (!adapter_data->ioaddr) { |
| 457 | ret = -ENOMEM; |
| 458 | goto release_region; |
| 459 | } |
| 460 | |
David Vrabel | 4894473 | 2006-01-19 17:56:29 +0000 | [diff] [blame] | 461 | irq = platform_get_irq(pdev, 0); |
| 462 | if (irq < 0) { |
| 463 | ret = -ENXIO; |
| 464 | goto unmap; |
| 465 | } |
| 466 | ret = request_irq(irq, iop3xx_i2c_irq_handler, 0, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | pdev->name, adapter_data); |
Dan Williams | fbd9a6d | 2005-11-01 22:31:12 +0000 | [diff] [blame] | 468 | |
| 469 | if (ret) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | ret = -EIO; |
| 471 | goto unmap; |
| 472 | } |
| 473 | |
| 474 | memcpy(new_adapter->name, pdev->name, strlen(pdev->name)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | new_adapter->owner = THIS_MODULE; |
Jean Delvare | 3401b2f | 2008-07-14 22:38:29 +0200 | [diff] [blame] | 476 | new_adapter->class = I2C_CLASS_HWMON | I2C_CLASS_SPD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | new_adapter->dev.parent = &pdev->dev; |
Martin Michlmayr | 757ba4c | 2007-07-12 14:12:30 +0200 | [diff] [blame] | 478 | new_adapter->nr = pdev->id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | |
| 480 | /* |
| 481 | * Default values...should these come in from board code? |
| 482 | */ |
Jean Delvare | 8a52c6b | 2009-03-28 21:34:43 +0100 | [diff] [blame] | 483 | new_adapter->timeout = HZ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | new_adapter->algo = &iop3xx_i2c_algo; |
| 485 | |
| 486 | init_waitqueue_head(&adapter_data->waitq); |
| 487 | spin_lock_init(&adapter_data->lock); |
| 488 | |
| 489 | iop3xx_i2c_reset(adapter_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | iop3xx_i2c_enable(adapter_data); |
| 491 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 492 | platform_set_drvdata(pdev, new_adapter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | new_adapter->algo_data = adapter_data; |
| 494 | |
Martin Michlmayr | 757ba4c | 2007-07-12 14:12:30 +0200 | [diff] [blame] | 495 | i2c_add_numbered_adapter(new_adapter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | |
| 497 | return 0; |
| 498 | |
| 499 | unmap: |
Aaro Koskinen | 747fcc9 | 2010-11-29 12:33:39 +0200 | [diff] [blame] | 500 | iounmap(adapter_data->ioaddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | |
| 502 | release_region: |
| 503 | release_mem_region(res->start, IOP3XX_I2C_IO_SIZE); |
| 504 | |
| 505 | free_both: |
| 506 | kfree(adapter_data); |
| 507 | |
| 508 | free_adapter: |
| 509 | kfree(new_adapter); |
| 510 | |
| 511 | out: |
| 512 | return ret; |
| 513 | } |
| 514 | |
| 515 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 516 | static struct platform_driver iop3xx_i2c_driver = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | .probe = iop3xx_i2c_probe, |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 518 | .remove = iop3xx_i2c_remove, |
| 519 | .driver = { |
| 520 | .owner = THIS_MODULE, |
| 521 | .name = "IOP3xx-I2C", |
| 522 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | }; |
| 524 | |
Axel Lin | a3664b5 | 2012-01-12 20:32:04 +0100 | [diff] [blame] | 525 | module_platform_driver(iop3xx_i2c_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | |
| 527 | MODULE_AUTHOR("D-TACQ Solutions Ltd <www.d-tacq.com>"); |
| 528 | MODULE_DESCRIPTION("IOP3xx iic algorithm and driver"); |
| 529 | MODULE_LICENSE("GPL"); |
Kay Sievers | add8eda | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 530 | MODULE_ALIAS("platform:IOP3xx-I2C"); |