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