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