Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 2 | /* |
| 3 | * i2c_adap_pxa.c |
| 4 | * |
| 5 | * I2C adapter for the PXA I2C bus access. |
| 6 | * |
| 7 | * Copyright (C) 2002 Intrinsyc Software Inc. |
| 8 | * Copyright (C) 2004-2005 Deep Blue Solutions Ltd. |
| 9 | * |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 10 | * History: |
| 11 | * Apr 2002: Initial version [CS] |
Daniel Mack | 3ad2f3fb | 2010-02-03 08:01:28 +0800 | [diff] [blame] | 12 | * Jun 2002: Properly separated algo/adap [FB] |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 13 | * Jan 2003: Fixed several bugs concerning interrupt handling [Kai-Uwe Bloem] |
| 14 | * Jan 2003: added limited signal handling [Kai-Uwe Bloem] |
| 15 | * Sep 2004: Major rework to ensure efficient bus handling [RMK] |
| 16 | * Dec 2004: Added support for PXA27x and slave device probing [Liam Girdwood] |
| 17 | * Feb 2005: Rework slave mode handling [RMK] |
| 18 | */ |
Russell King | 8de32da | 2020-04-27 19:48:46 +0100 | [diff] [blame] | 19 | #include <linux/clk.h> |
| 20 | #include <linux/delay.h> |
| 21 | #include <linux/err.h> |
| 22 | #include <linux/errno.h> |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 23 | #include <linux/i2c.h> |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 24 | #include <linux/init.h> |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 25 | #include <linux/interrupt.h> |
Russell King | 8de32da | 2020-04-27 19:48:46 +0100 | [diff] [blame] | 26 | #include <linux/io.h> |
| 27 | #include <linux/kernel.h> |
| 28 | #include <linux/module.h> |
Haojian Zhuang | 63fe122 | 2012-03-01 13:04:44 +0800 | [diff] [blame] | 29 | #include <linux/of.h> |
| 30 | #include <linux/of_device.h> |
Russell King | d052d1b | 2005-10-29 19:07:23 +0100 | [diff] [blame] | 31 | #include <linux/platform_device.h> |
Wolfram Sang | f15fc9b | 2017-11-13 18:27:39 +0100 | [diff] [blame] | 32 | #include <linux/platform_data/i2c-pxa.h> |
Russell King | 8de32da | 2020-04-27 19:48:46 +0100 | [diff] [blame] | 33 | #include <linux/slab.h> |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 34 | |
Russell King | 940695a | 2020-04-27 19:48:56 +0100 | [diff] [blame] | 35 | /* I2C register field definitions */ |
Russell King | f8e5d3c | 2020-04-27 19:49:01 +0100 | [diff] [blame] | 36 | #define IBMR_SDAS (1 << 0) |
| 37 | #define IBMR_SCLS (1 << 1) |
| 38 | |
Russell King | 940695a | 2020-04-27 19:48:56 +0100 | [diff] [blame] | 39 | #define ICR_START (1 << 0) /* start bit */ |
| 40 | #define ICR_STOP (1 << 1) /* stop bit */ |
| 41 | #define ICR_ACKNAK (1 << 2) /* send ACK(0) or NAK(1) */ |
| 42 | #define ICR_TB (1 << 3) /* transfer byte bit */ |
| 43 | #define ICR_MA (1 << 4) /* master abort */ |
| 44 | #define ICR_SCLE (1 << 5) /* master clock enable */ |
| 45 | #define ICR_IUE (1 << 6) /* unit enable */ |
| 46 | #define ICR_GCD (1 << 7) /* general call disable */ |
| 47 | #define ICR_ITEIE (1 << 8) /* enable tx interrupts */ |
| 48 | #define ICR_IRFIE (1 << 9) /* enable rx interrupts */ |
| 49 | #define ICR_BEIE (1 << 10) /* enable bus error ints */ |
| 50 | #define ICR_SSDIE (1 << 11) /* slave STOP detected int enable */ |
| 51 | #define ICR_ALDIE (1 << 12) /* enable arbitration interrupt */ |
| 52 | #define ICR_SADIE (1 << 13) /* slave address detected int enable */ |
| 53 | #define ICR_UR (1 << 14) /* unit reset */ |
| 54 | #define ICR_FM (1 << 15) /* fast mode */ |
| 55 | #define ICR_HS (1 << 16) /* High Speed mode */ |
| 56 | #define ICR_A3700_FM (1 << 16) /* fast mode for armada-3700 */ |
| 57 | #define ICR_A3700_HS (1 << 17) /* high speed mode for armada-3700 */ |
| 58 | #define ICR_GPIOEN (1 << 19) /* enable GPIO mode for SCL in HS */ |
| 59 | |
| 60 | #define ISR_RWM (1 << 0) /* read/write mode */ |
| 61 | #define ISR_ACKNAK (1 << 1) /* ack/nak status */ |
| 62 | #define ISR_UB (1 << 2) /* unit busy */ |
| 63 | #define ISR_IBB (1 << 3) /* bus busy */ |
| 64 | #define ISR_SSD (1 << 4) /* slave stop detected */ |
| 65 | #define ISR_ALD (1 << 5) /* arbitration loss detected */ |
| 66 | #define ISR_ITE (1 << 6) /* tx buffer empty */ |
| 67 | #define ISR_IRF (1 << 7) /* rx buffer full */ |
| 68 | #define ISR_GCAD (1 << 8) /* general call address detected */ |
| 69 | #define ISR_SAD (1 << 9) /* slave address detected */ |
| 70 | #define ISR_BED (1 << 10) /* bus error no ACK/NAK */ |
| 71 | |
| 72 | #define ILCR_SLV_SHIFT 0 |
| 73 | #define ILCR_SLV_MASK (0x1FF << ILCR_SLV_SHIFT) |
| 74 | #define ILCR_FLV_SHIFT 9 |
| 75 | #define ILCR_FLV_MASK (0x1FF << ILCR_FLV_SHIFT) |
| 76 | #define ILCR_HLVL_SHIFT 18 |
| 77 | #define ILCR_HLVL_MASK (0x1FF << ILCR_HLVL_SHIFT) |
| 78 | #define ILCR_HLVH_SHIFT 27 |
| 79 | #define ILCR_HLVH_MASK (0x1F << ILCR_HLVH_SHIFT) |
| 80 | |
| 81 | #define IWCR_CNT_SHIFT 0 |
| 82 | #define IWCR_CNT_MASK (0x1F << IWCR_CNT_SHIFT) |
| 83 | #define IWCR_HS_CNT1_SHIFT 5 |
| 84 | #define IWCR_HS_CNT1_MASK (0x1F << IWCR_HS_CNT1_SHIFT) |
| 85 | #define IWCR_HS_CNT2_SHIFT 10 |
| 86 | #define IWCR_HS_CNT2_MASK (0x1F << IWCR_HS_CNT2_SHIFT) |
| 87 | |
Russell King | 79622f3 | 2020-04-27 19:49:12 +0100 | [diff] [blame] | 88 | /* need a longer timeout if we're dealing with the fact we may well be |
| 89 | * looking at a multi-master environment |
| 90 | */ |
| 91 | #define DEF_TIMEOUT 32 |
| 92 | |
| 93 | #define BUS_ERROR (-EREMOTEIO) |
| 94 | #define XFER_NAKED (-ECONNREFUSED) |
| 95 | #define I2C_RETRY (-2000) /* an error has occurred retry transmit */ |
| 96 | |
| 97 | /* ICR initialize bit values |
| 98 | * |
| 99 | * 15 FM 0 (100 kHz operation) |
| 100 | * 14 UR 0 (No unit reset) |
| 101 | * 13 SADIE 0 (Disables the unit from interrupting on slave addresses |
| 102 | * matching its slave address) |
| 103 | * 12 ALDIE 0 (Disables the unit from interrupt when it loses arbitration |
| 104 | * in master mode) |
| 105 | * 11 SSDIE 0 (Disables interrupts from a slave stop detected, in slave mode) |
| 106 | * 10 BEIE 1 (Enable interrupts from detected bus errors, no ACK sent) |
| 107 | * 9 IRFIE 1 (Enable interrupts from full buffer received) |
| 108 | * 8 ITEIE 1 (Enables the I2C unit to interrupt when transmit buffer empty) |
| 109 | * 7 GCD 1 (Disables i2c unit response to general call messages as a slave) |
| 110 | * 6 IUE 0 (Disable unit until we change settings) |
| 111 | * 5 SCLE 1 (Enables the i2c clock output for master mode (drives SCL) |
| 112 | * 4 MA 0 (Only send stop with the ICR stop bit) |
| 113 | * 3 TB 0 (We are not transmitting a byte initially) |
| 114 | * 2 ACKNAK 0 (Send an ACK after the unit receives a byte) |
| 115 | * 1 STOP 0 (Do not send a STOP) |
| 116 | * 0 START 0 (Do not send a START) |
| 117 | */ |
| 118 | #define I2C_ICR_INIT (ICR_BEIE | ICR_IRFIE | ICR_ITEIE | ICR_GCD | ICR_SCLE) |
| 119 | |
| 120 | /* I2C status register init values |
| 121 | * |
| 122 | * 10 BED 1 (Clear bus error detected) |
| 123 | * 9 SAD 1 (Clear slave address detected) |
| 124 | * 7 IRF 1 (Clear IDBR Receive Full) |
| 125 | * 6 ITE 1 (Clear IDBR Transmit Empty) |
| 126 | * 5 ALD 1 (Clear Arbitration Loss Detected) |
| 127 | * 4 SSD 1 (Clear Slave Stop Detected) |
| 128 | */ |
| 129 | #define I2C_ISR_INIT 0x7FF /* status register init */ |
| 130 | |
Sebastian Andrzej Siewior | d6668c7 | 2011-02-23 12:38:15 +0100 | [diff] [blame] | 131 | struct pxa_reg_layout { |
| 132 | u32 ibmr; |
| 133 | u32 idbr; |
| 134 | u32 icr; |
| 135 | u32 isr; |
| 136 | u32 isar; |
Vaibhav Hiremath | c5fa6fc | 2015-08-24 11:29:36 +0530 | [diff] [blame] | 137 | u32 ilcr; |
| 138 | u32 iwcr; |
Romain Perier | 6c14bda | 2016-12-01 12:04:37 +0100 | [diff] [blame] | 139 | u32 fm; |
| 140 | u32 hs; |
Sebastian Andrzej Siewior | d6668c7 | 2011-02-23 12:38:15 +0100 | [diff] [blame] | 141 | }; |
| 142 | |
| 143 | enum pxa_i2c_types { |
| 144 | REGS_PXA2XX, |
| 145 | REGS_PXA3XX, |
Sebastian Andrzej Siewior | 7e94dd1 | 2011-03-02 11:26:53 +0100 | [diff] [blame] | 146 | REGS_CE4100, |
Vaibhav Hiremath | c5fa6fc | 2015-08-24 11:29:36 +0530 | [diff] [blame] | 147 | REGS_PXA910, |
Romain Perier | 294be03 | 2016-12-01 12:04:38 +0100 | [diff] [blame] | 148 | REGS_A3700, |
Sebastian Andrzej Siewior | d6668c7 | 2011-02-23 12:38:15 +0100 | [diff] [blame] | 149 | }; |
| 150 | |
Russell King | 940695a | 2020-04-27 19:48:56 +0100 | [diff] [blame] | 151 | /* I2C register layout definitions */ |
Sebastian Andrzej Siewior | d6668c7 | 2011-02-23 12:38:15 +0100 | [diff] [blame] | 152 | static struct pxa_reg_layout pxa_reg_layout[] = { |
| 153 | [REGS_PXA2XX] = { |
| 154 | .ibmr = 0x00, |
Sebastian Andrzej Siewior | d6668c7 | 2011-02-23 12:38:15 +0100 | [diff] [blame] | 155 | .idbr = 0x08, |
| 156 | .icr = 0x10, |
| 157 | .isr = 0x18, |
| 158 | .isar = 0x20, |
Russell King | ee47893 | 2020-04-27 19:49:07 +0100 | [diff] [blame] | 159 | .fm = ICR_FM, |
| 160 | .hs = ICR_HS, |
Sebastian Andrzej Siewior | d6668c7 | 2011-02-23 12:38:15 +0100 | [diff] [blame] | 161 | }, |
Vasily Khoruzhick | 23e74a8 | 2011-03-13 15:53:28 +0200 | [diff] [blame] | 162 | [REGS_PXA3XX] = { |
| 163 | .ibmr = 0x00, |
| 164 | .idbr = 0x04, |
| 165 | .icr = 0x08, |
| 166 | .isr = 0x0c, |
| 167 | .isar = 0x10, |
Russell King | ee47893 | 2020-04-27 19:49:07 +0100 | [diff] [blame] | 168 | .fm = ICR_FM, |
| 169 | .hs = ICR_HS, |
Vasily Khoruzhick | 23e74a8 | 2011-03-13 15:53:28 +0200 | [diff] [blame] | 170 | }, |
Sebastian Andrzej Siewior | 7e94dd1 | 2011-03-02 11:26:53 +0100 | [diff] [blame] | 171 | [REGS_CE4100] = { |
| 172 | .ibmr = 0x14, |
| 173 | .idbr = 0x0c, |
| 174 | .icr = 0x00, |
| 175 | .isr = 0x04, |
| 176 | /* no isar register */ |
Russell King | ee47893 | 2020-04-27 19:49:07 +0100 | [diff] [blame] | 177 | .fm = ICR_FM, |
| 178 | .hs = ICR_HS, |
Sebastian Andrzej Siewior | 7e94dd1 | 2011-03-02 11:26:53 +0100 | [diff] [blame] | 179 | }, |
Vaibhav Hiremath | c5fa6fc | 2015-08-24 11:29:36 +0530 | [diff] [blame] | 180 | [REGS_PXA910] = { |
| 181 | .ibmr = 0x00, |
| 182 | .idbr = 0x08, |
| 183 | .icr = 0x10, |
| 184 | .isr = 0x18, |
| 185 | .isar = 0x20, |
| 186 | .ilcr = 0x28, |
| 187 | .iwcr = 0x30, |
Russell King | ee47893 | 2020-04-27 19:49:07 +0100 | [diff] [blame] | 188 | .fm = ICR_FM, |
| 189 | .hs = ICR_HS, |
Vaibhav Hiremath | c5fa6fc | 2015-08-24 11:29:36 +0530 | [diff] [blame] | 190 | }, |
Romain Perier | 294be03 | 2016-12-01 12:04:38 +0100 | [diff] [blame] | 191 | [REGS_A3700] = { |
| 192 | .ibmr = 0x00, |
| 193 | .idbr = 0x04, |
| 194 | .icr = 0x08, |
| 195 | .isr = 0x0c, |
| 196 | .isar = 0x10, |
Russell King | 940695a | 2020-04-27 19:48:56 +0100 | [diff] [blame] | 197 | .fm = ICR_A3700_FM, |
| 198 | .hs = ICR_A3700_HS, |
Romain Perier | 294be03 | 2016-12-01 12:04:38 +0100 | [diff] [blame] | 199 | }, |
Sebastian Andrzej Siewior | d6668c7 | 2011-02-23 12:38:15 +0100 | [diff] [blame] | 200 | }; |
Eric Miao | f23d491 | 2009-04-13 14:43:25 +0800 | [diff] [blame] | 201 | |
Russell King | 70aee28 | 2020-04-27 19:49:17 +0100 | [diff] [blame] | 202 | static const struct of_device_id i2c_pxa_dt_ids[] = { |
| 203 | { .compatible = "mrvl,pxa-i2c", .data = (void *)REGS_PXA2XX }, |
| 204 | { .compatible = "mrvl,pwri2c", .data = (void *)REGS_PXA3XX }, |
| 205 | { .compatible = "mrvl,mmp-twsi", .data = (void *)REGS_PXA910 }, |
| 206 | { .compatible = "marvell,armada-3700-i2c", .data = (void *)REGS_A3700 }, |
| 207 | {} |
| 208 | }; |
| 209 | MODULE_DEVICE_TABLE(of, i2c_pxa_dt_ids); |
| 210 | |
Eric Miao | f23d491 | 2009-04-13 14:43:25 +0800 | [diff] [blame] | 211 | static const struct platform_device_id i2c_pxa_id_table[] = { |
Sebastian Andrzej Siewior | d6668c7 | 2011-02-23 12:38:15 +0100 | [diff] [blame] | 212 | { "pxa2xx-i2c", REGS_PXA2XX }, |
| 213 | { "pxa3xx-pwri2c", REGS_PXA3XX }, |
Sebastian Andrzej Siewior | 7e94dd1 | 2011-03-02 11:26:53 +0100 | [diff] [blame] | 214 | { "ce4100-i2c", REGS_CE4100 }, |
Vaibhav Hiremath | c5fa6fc | 2015-08-24 11:29:36 +0530 | [diff] [blame] | 215 | { "pxa910-i2c", REGS_PXA910 }, |
Romain Perier | 294be03 | 2016-12-01 12:04:38 +0100 | [diff] [blame] | 216 | { "armada-3700-i2c", REGS_A3700 }, |
Eric Miao | f23d491 | 2009-04-13 14:43:25 +0800 | [diff] [blame] | 217 | { }, |
| 218 | }; |
| 219 | MODULE_DEVICE_TABLE(platform, i2c_pxa_id_table); |
| 220 | |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 221 | struct pxa_i2c { |
| 222 | spinlock_t lock; |
| 223 | wait_queue_head_t wait; |
| 224 | struct i2c_msg *msg; |
| 225 | unsigned int msg_num; |
| 226 | unsigned int msg_idx; |
| 227 | unsigned int msg_ptr; |
| 228 | unsigned int slave_addr; |
Vaibhav Hiremath | 3a2dc16 | 2015-07-14 13:06:44 +0530 | [diff] [blame] | 229 | unsigned int req_slave_addr; |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 230 | |
| 231 | struct i2c_adapter adap; |
Russell King | c3cef3f | 2007-08-20 10:19:10 +0100 | [diff] [blame] | 232 | struct clk *clk; |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 233 | #ifdef CONFIG_I2C_PXA_SLAVE |
Patrick Williams | 4d51b4c | 2019-10-01 10:59:59 -0500 | [diff] [blame] | 234 | struct i2c_client *slave; |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 235 | #endif |
| 236 | |
| 237 | unsigned int irqlogidx; |
| 238 | u32 isrlog[32]; |
| 239 | u32 icrlog[32]; |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 240 | |
| 241 | void __iomem *reg_base; |
Sebastian Andrzej Siewior | d6668c7 | 2011-02-23 12:38:15 +0100 | [diff] [blame] | 242 | void __iomem *reg_ibmr; |
| 243 | void __iomem *reg_idbr; |
| 244 | void __iomem *reg_icr; |
| 245 | void __iomem *reg_isr; |
| 246 | void __iomem *reg_isar; |
Vaibhav Hiremath | c5fa6fc | 2015-08-24 11:29:36 +0530 | [diff] [blame] | 247 | void __iomem *reg_ilcr; |
| 248 | void __iomem *reg_iwcr; |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 249 | |
| 250 | unsigned long iobase; |
| 251 | unsigned long iosize; |
| 252 | |
| 253 | int irq; |
Jonathan Cameron | c46c948 | 2008-10-03 15:07:36 +0100 | [diff] [blame] | 254 | unsigned int use_pio :1; |
| 255 | unsigned int fast_mode :1; |
Leilei Shang | 9d3dda5 | 2013-06-07 14:38:17 +0800 | [diff] [blame] | 256 | unsigned int high_mode:1; |
| 257 | unsigned char master_code; |
| 258 | unsigned long rate; |
| 259 | bool highmode_enter; |
Romain Perier | 6c14bda | 2016-12-01 12:04:37 +0100 | [diff] [blame] | 260 | u32 fm_mask; |
| 261 | u32 hs_mask; |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 262 | }; |
| 263 | |
Sebastian Andrzej Siewior | d6668c7 | 2011-02-23 12:38:15 +0100 | [diff] [blame] | 264 | #define _IBMR(i2c) ((i2c)->reg_ibmr) |
| 265 | #define _IDBR(i2c) ((i2c)->reg_idbr) |
| 266 | #define _ICR(i2c) ((i2c)->reg_icr) |
| 267 | #define _ISR(i2c) ((i2c)->reg_isr) |
| 268 | #define _ISAR(i2c) ((i2c)->reg_isar) |
Vaibhav Hiremath | c5fa6fc | 2015-08-24 11:29:36 +0530 | [diff] [blame] | 269 | #define _ILCR(i2c) ((i2c)->reg_ilcr) |
| 270 | #define _IWCR(i2c) ((i2c)->reg_iwcr) |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 271 | |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 272 | /* |
| 273 | * I2C Slave mode address |
| 274 | */ |
| 275 | #define I2C_PXA_SLAVE_ADDR 0x1 |
| 276 | |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 277 | #ifdef DEBUG |
| 278 | |
| 279 | struct bits { |
| 280 | u32 mask; |
| 281 | const char *set; |
| 282 | const char *unset; |
| 283 | }; |
Jiri Slaby | ed11399 | 2007-10-18 23:40:28 -0700 | [diff] [blame] | 284 | #define PXA_BIT(m, s, u) { .mask = m, .set = s, .unset = u } |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 285 | |
| 286 | static inline void |
| 287 | decode_bits(const char *prefix, const struct bits *bits, int num, u32 val) |
| 288 | { |
Russell King | bb82ba6 | 2020-04-27 19:49:27 +0100 | [diff] [blame] | 289 | printk("%s %08x:", prefix, val); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 290 | while (num--) { |
| 291 | const char *str = val & bits->mask ? bits->set : bits->unset; |
| 292 | if (str) |
Russell King | bb82ba6 | 2020-04-27 19:49:27 +0100 | [diff] [blame] | 293 | pr_cont(" %s", str); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 294 | bits++; |
| 295 | } |
Russell King | bb82ba6 | 2020-04-27 19:49:27 +0100 | [diff] [blame] | 296 | pr_cont("\n"); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | static const struct bits isr_bits[] = { |
Jiri Slaby | ed11399 | 2007-10-18 23:40:28 -0700 | [diff] [blame] | 300 | PXA_BIT(ISR_RWM, "RX", "TX"), |
| 301 | PXA_BIT(ISR_ACKNAK, "NAK", "ACK"), |
| 302 | PXA_BIT(ISR_UB, "Bsy", "Rdy"), |
| 303 | PXA_BIT(ISR_IBB, "BusBsy", "BusRdy"), |
| 304 | PXA_BIT(ISR_SSD, "SlaveStop", NULL), |
| 305 | PXA_BIT(ISR_ALD, "ALD", NULL), |
| 306 | PXA_BIT(ISR_ITE, "TxEmpty", NULL), |
| 307 | PXA_BIT(ISR_IRF, "RxFull", NULL), |
| 308 | PXA_BIT(ISR_GCAD, "GenCall", NULL), |
| 309 | PXA_BIT(ISR_SAD, "SlaveAddr", NULL), |
| 310 | PXA_BIT(ISR_BED, "BusErr", NULL), |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 311 | }; |
| 312 | |
| 313 | static void decode_ISR(unsigned int val) |
| 314 | { |
Russell King | 6fd60fa | 2005-09-08 21:04:58 +0100 | [diff] [blame] | 315 | decode_bits(KERN_DEBUG "ISR", isr_bits, ARRAY_SIZE(isr_bits), val); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | static const struct bits icr_bits[] = { |
Jiri Slaby | ed11399 | 2007-10-18 23:40:28 -0700 | [diff] [blame] | 319 | PXA_BIT(ICR_START, "START", NULL), |
| 320 | PXA_BIT(ICR_STOP, "STOP", NULL), |
| 321 | PXA_BIT(ICR_ACKNAK, "ACKNAK", NULL), |
| 322 | PXA_BIT(ICR_TB, "TB", NULL), |
| 323 | PXA_BIT(ICR_MA, "MA", NULL), |
| 324 | PXA_BIT(ICR_SCLE, "SCLE", "scle"), |
| 325 | PXA_BIT(ICR_IUE, "IUE", "iue"), |
| 326 | PXA_BIT(ICR_GCD, "GCD", NULL), |
| 327 | PXA_BIT(ICR_ITEIE, "ITEIE", NULL), |
| 328 | PXA_BIT(ICR_IRFIE, "IRFIE", NULL), |
| 329 | PXA_BIT(ICR_BEIE, "BEIE", NULL), |
| 330 | PXA_BIT(ICR_SSDIE, "SSDIE", NULL), |
| 331 | PXA_BIT(ICR_ALDIE, "ALDIE", NULL), |
| 332 | PXA_BIT(ICR_SADIE, "SADIE", NULL), |
| 333 | PXA_BIT(ICR_UR, "UR", "ur"), |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 334 | }; |
| 335 | |
Holger Schurig | d6a7b5f | 2008-02-11 16:51:41 +0100 | [diff] [blame] | 336 | #ifdef CONFIG_I2C_PXA_SLAVE |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 337 | static void decode_ICR(unsigned int val) |
| 338 | { |
Russell King | 6fd60fa | 2005-09-08 21:04:58 +0100 | [diff] [blame] | 339 | decode_bits(KERN_DEBUG "ICR", icr_bits, ARRAY_SIZE(icr_bits), val); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 340 | } |
Holger Schurig | d6a7b5f | 2008-02-11 16:51:41 +0100 | [diff] [blame] | 341 | #endif |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 342 | |
| 343 | static unsigned int i2c_debug = DEBUG; |
| 344 | |
| 345 | static void i2c_pxa_show_state(struct pxa_i2c *i2c, int lno, const char *fname) |
| 346 | { |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 347 | dev_dbg(&i2c->adap.dev, "state:%s:%d: ISR=%08x, ICR=%08x, IBMR=%02x\n", fname, lno, |
| 348 | readl(_ISR(i2c)), readl(_ICR(i2c)), readl(_IBMR(i2c))); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 349 | } |
| 350 | |
Harvey Harrison | 08882d2 | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 351 | #define show_state(i2c) i2c_pxa_show_state(i2c, __LINE__, __func__) |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 352 | |
| 353 | static void i2c_pxa_scream_blue_murder(struct pxa_i2c *i2c, const char *why) |
| 354 | { |
| 355 | unsigned int i; |
Vaibhav Hiremath | 3a2dc16 | 2015-07-14 13:06:44 +0530 | [diff] [blame] | 356 | struct device *dev = &i2c->adap.dev; |
| 357 | |
| 358 | dev_err(dev, "slave_0x%x error: %s\n", |
| 359 | i2c->req_slave_addr >> 1, why); |
| 360 | dev_err(dev, "msg_num: %d msg_idx: %d msg_ptr: %d\n", |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 361 | i2c->msg_num, i2c->msg_idx, i2c->msg_ptr); |
Vaibhav Hiremath | 3a2dc16 | 2015-07-14 13:06:44 +0530 | [diff] [blame] | 362 | dev_err(dev, "IBMR: %08x IDBR: %08x ICR: %08x ISR: %08x\n", |
| 363 | readl(_IBMR(i2c)), readl(_IDBR(i2c)), readl(_ICR(i2c)), |
| 364 | readl(_ISR(i2c))); |
Russell King | 88b73ee | 2020-04-27 19:49:22 +0100 | [diff] [blame] | 365 | dev_err(dev, "log:"); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 366 | for (i = 0; i < i2c->irqlogidx; i++) |
Russell King | 88b73ee | 2020-04-27 19:49:22 +0100 | [diff] [blame] | 367 | pr_cont(" [%03x:%05x]", i2c->isrlog[i], i2c->icrlog[i]); |
| 368 | pr_cont("\n"); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 369 | } |
| 370 | |
Wolfram Sang | 0d813d9 | 2009-11-03 12:53:41 +0100 | [diff] [blame] | 371 | #else /* ifdef DEBUG */ |
| 372 | |
| 373 | #define i2c_debug 0 |
| 374 | |
| 375 | #define show_state(i2c) do { } while (0) |
| 376 | #define decode_ISR(val) do { } while (0) |
| 377 | #define decode_ICR(val) do { } while (0) |
| 378 | #define i2c_pxa_scream_blue_murder(i2c, why) do { } while (0) |
| 379 | |
| 380 | #endif /* ifdef DEBUG / else */ |
| 381 | |
| 382 | static void i2c_pxa_master_complete(struct pxa_i2c *i2c, int ret); |
Wolfram Sang | 0d813d9 | 2009-11-03 12:53:41 +0100 | [diff] [blame] | 383 | |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 384 | static inline int i2c_pxa_is_slavemode(struct pxa_i2c *i2c) |
| 385 | { |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 386 | return !(readl(_ICR(i2c)) & ICR_SCLE); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | static void i2c_pxa_abort(struct pxa_i2c *i2c) |
| 390 | { |
Dmitry Baryshkov | 387fa6a | 2008-08-18 14:38:48 +0100 | [diff] [blame] | 391 | int i = 250; |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 392 | |
| 393 | if (i2c_pxa_is_slavemode(i2c)) { |
Russell King | 6fd60fa | 2005-09-08 21:04:58 +0100 | [diff] [blame] | 394 | dev_dbg(&i2c->adap.dev, "%s: called in slave mode\n", __func__); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 395 | return; |
| 396 | } |
| 397 | |
Russell King | f8e5d3c | 2020-04-27 19:49:01 +0100 | [diff] [blame] | 398 | while ((i > 0) && (readl(_IBMR(i2c)) & IBMR_SDAS) == 0) { |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 399 | unsigned long icr = readl(_ICR(i2c)); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 400 | |
| 401 | icr &= ~ICR_START; |
| 402 | icr |= ICR_ACKNAK | ICR_STOP | ICR_TB; |
| 403 | |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 404 | writel(icr, _ICR(i2c)); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 405 | |
| 406 | show_state(i2c); |
| 407 | |
Dmitry Baryshkov | 387fa6a | 2008-08-18 14:38:48 +0100 | [diff] [blame] | 408 | mdelay(1); |
| 409 | i --; |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 410 | } |
| 411 | |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 412 | writel(readl(_ICR(i2c)) & ~(ICR_MA | ICR_START | ICR_STOP), |
| 413 | _ICR(i2c)); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | static int i2c_pxa_wait_bus_not_busy(struct pxa_i2c *i2c) |
| 417 | { |
| 418 | int timeout = DEF_TIMEOUT; |
Russell King | e896be5 | 2020-04-27 19:49:32 +0100 | [diff] [blame^] | 419 | u32 isr; |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 420 | |
Russell King | e896be5 | 2020-04-27 19:49:32 +0100 | [diff] [blame^] | 421 | while (1) { |
| 422 | isr = readl(_ISR(i2c)); |
| 423 | if (!(isr & (ISR_IBB | ISR_UB))) |
| 424 | return 0; |
| 425 | |
| 426 | if (isr & ISR_SAD) |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 427 | timeout += 4; |
| 428 | |
Russell King | e896be5 | 2020-04-27 19:49:32 +0100 | [diff] [blame^] | 429 | if (!timeout--) |
| 430 | break; |
| 431 | |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 432 | msleep(2); |
| 433 | show_state(i2c); |
| 434 | } |
| 435 | |
Russell King | e896be5 | 2020-04-27 19:49:32 +0100 | [diff] [blame^] | 436 | show_state(i2c); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 437 | |
Russell King | e896be5 | 2020-04-27 19:49:32 +0100 | [diff] [blame^] | 438 | return I2C_RETRY; |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | static int i2c_pxa_wait_master(struct pxa_i2c *i2c) |
| 442 | { |
| 443 | unsigned long timeout = jiffies + HZ*4; |
| 444 | |
| 445 | while (time_before(jiffies, timeout)) { |
| 446 | if (i2c_debug > 1) |
Russell King | 6fd60fa | 2005-09-08 21:04:58 +0100 | [diff] [blame] | 447 | dev_dbg(&i2c->adap.dev, "%s: %ld: ISR=%08x, ICR=%08x, IBMR=%02x\n", |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 448 | __func__, (long)jiffies, readl(_ISR(i2c)), readl(_ICR(i2c)), readl(_IBMR(i2c))); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 449 | |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 450 | if (readl(_ISR(i2c)) & ISR_SAD) { |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 451 | if (i2c_debug > 0) |
Russell King | 6fd60fa | 2005-09-08 21:04:58 +0100 | [diff] [blame] | 452 | dev_dbg(&i2c->adap.dev, "%s: Slave detected\n", __func__); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 453 | goto out; |
| 454 | } |
| 455 | |
| 456 | /* wait for unit and bus being not busy, and we also do a |
| 457 | * quick check of the i2c lines themselves to ensure they've |
| 458 | * gone high... |
| 459 | */ |
Russell King | f8e5d3c | 2020-04-27 19:49:01 +0100 | [diff] [blame] | 460 | if ((readl(_ISR(i2c)) & (ISR_UB | ISR_IBB)) == 0 && |
| 461 | readl(_IBMR(i2c)) == (IBMR_SCLS | IBMR_SDAS)) { |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 462 | if (i2c_debug > 0) |
Russell King | 6fd60fa | 2005-09-08 21:04:58 +0100 | [diff] [blame] | 463 | dev_dbg(&i2c->adap.dev, "%s: done\n", __func__); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 464 | return 1; |
| 465 | } |
| 466 | |
| 467 | msleep(1); |
| 468 | } |
| 469 | |
| 470 | if (i2c_debug > 0) |
Russell King | 6fd60fa | 2005-09-08 21:04:58 +0100 | [diff] [blame] | 471 | dev_dbg(&i2c->adap.dev, "%s: did not free\n", __func__); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 472 | out: |
| 473 | return 0; |
| 474 | } |
| 475 | |
| 476 | static int i2c_pxa_set_master(struct pxa_i2c *i2c) |
| 477 | { |
| 478 | if (i2c_debug) |
Russell King | 6fd60fa | 2005-09-08 21:04:58 +0100 | [diff] [blame] | 479 | dev_dbg(&i2c->adap.dev, "setting to bus master\n"); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 480 | |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 481 | if ((readl(_ISR(i2c)) & (ISR_UB | ISR_IBB)) != 0) { |
Russell King | 6fd60fa | 2005-09-08 21:04:58 +0100 | [diff] [blame] | 482 | dev_dbg(&i2c->adap.dev, "%s: unit is busy\n", __func__); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 483 | if (!i2c_pxa_wait_master(i2c)) { |
Russell King | 6fd60fa | 2005-09-08 21:04:58 +0100 | [diff] [blame] | 484 | dev_dbg(&i2c->adap.dev, "%s: error: unit busy\n", __func__); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 485 | return I2C_RETRY; |
| 486 | } |
| 487 | } |
| 488 | |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 489 | writel(readl(_ICR(i2c)) | ICR_SCLE, _ICR(i2c)); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 490 | return 0; |
| 491 | } |
| 492 | |
| 493 | #ifdef CONFIG_I2C_PXA_SLAVE |
| 494 | static int i2c_pxa_wait_slave(struct pxa_i2c *i2c) |
| 495 | { |
| 496 | unsigned long timeout = jiffies + HZ*1; |
| 497 | |
| 498 | /* wait for stop */ |
| 499 | |
| 500 | show_state(i2c); |
| 501 | |
| 502 | while (time_before(jiffies, timeout)) { |
| 503 | if (i2c_debug > 1) |
Russell King | 6fd60fa | 2005-09-08 21:04:58 +0100 | [diff] [blame] | 504 | dev_dbg(&i2c->adap.dev, "%s: %ld: ISR=%08x, ICR=%08x, IBMR=%02x\n", |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 505 | __func__, (long)jiffies, readl(_ISR(i2c)), readl(_ICR(i2c)), readl(_IBMR(i2c))); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 506 | |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 507 | if ((readl(_ISR(i2c)) & (ISR_UB|ISR_IBB)) == 0 || |
| 508 | (readl(_ISR(i2c)) & ISR_SAD) != 0 || |
| 509 | (readl(_ICR(i2c)) & ICR_SCLE) == 0) { |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 510 | if (i2c_debug > 1) |
Russell King | 6fd60fa | 2005-09-08 21:04:58 +0100 | [diff] [blame] | 511 | dev_dbg(&i2c->adap.dev, "%s: done\n", __func__); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 512 | return 1; |
| 513 | } |
| 514 | |
| 515 | msleep(1); |
| 516 | } |
| 517 | |
| 518 | if (i2c_debug > 0) |
Russell King | 6fd60fa | 2005-09-08 21:04:58 +0100 | [diff] [blame] | 519 | dev_dbg(&i2c->adap.dev, "%s: did not free\n", __func__); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 520 | return 0; |
| 521 | } |
| 522 | |
| 523 | /* |
| 524 | * clear the hold on the bus, and take of anything else |
| 525 | * that has been configured |
| 526 | */ |
| 527 | static void i2c_pxa_set_slave(struct pxa_i2c *i2c, int errcode) |
| 528 | { |
| 529 | show_state(i2c); |
| 530 | |
| 531 | if (errcode < 0) { |
| 532 | udelay(100); /* simple delay */ |
| 533 | } else { |
| 534 | /* we need to wait for the stop condition to end */ |
| 535 | |
| 536 | /* if we where in stop, then clear... */ |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 537 | if (readl(_ICR(i2c)) & ICR_STOP) { |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 538 | udelay(100); |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 539 | writel(readl(_ICR(i2c)) & ~ICR_STOP, _ICR(i2c)); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | if (!i2c_pxa_wait_slave(i2c)) { |
Russell King | 6fd60fa | 2005-09-08 21:04:58 +0100 | [diff] [blame] | 543 | dev_err(&i2c->adap.dev, "%s: wait timedout\n", |
| 544 | __func__); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 545 | return; |
| 546 | } |
| 547 | } |
| 548 | |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 549 | writel(readl(_ICR(i2c)) & ~(ICR_STOP|ICR_ACKNAK|ICR_MA), _ICR(i2c)); |
| 550 | writel(readl(_ICR(i2c)) & ~ICR_SCLE, _ICR(i2c)); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 551 | |
| 552 | if (i2c_debug) { |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 553 | dev_dbg(&i2c->adap.dev, "ICR now %08x, ISR %08x\n", readl(_ICR(i2c)), readl(_ISR(i2c))); |
| 554 | decode_ICR(readl(_ICR(i2c))); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 555 | } |
| 556 | } |
| 557 | #else |
| 558 | #define i2c_pxa_set_slave(i2c, err) do { } while (0) |
| 559 | #endif |
| 560 | |
| 561 | static void i2c_pxa_reset(struct pxa_i2c *i2c) |
| 562 | { |
| 563 | pr_debug("Resetting I2C Controller Unit\n"); |
| 564 | |
| 565 | /* abort any transfer currently under way */ |
| 566 | i2c_pxa_abort(i2c); |
| 567 | |
| 568 | /* reset according to 9.8 */ |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 569 | writel(ICR_UR, _ICR(i2c)); |
| 570 | writel(I2C_ISR_INIT, _ISR(i2c)); |
| 571 | writel(readl(_ICR(i2c)) & ~ICR_UR, _ICR(i2c)); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 572 | |
Vaibhav Hiremath | e087b42 | 2015-07-14 13:06:41 +0530 | [diff] [blame] | 573 | if (i2c->reg_isar && IS_ENABLED(CONFIG_I2C_PXA_SLAVE)) |
Sebastian Andrzej Siewior | 7e94dd1 | 2011-03-02 11:26:53 +0100 | [diff] [blame] | 574 | writel(i2c->slave_addr, _ISAR(i2c)); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 575 | |
| 576 | /* set control register values */ |
Romain Perier | 6c14bda | 2016-12-01 12:04:37 +0100 | [diff] [blame] | 577 | writel(I2C_ICR_INIT | (i2c->fast_mode ? i2c->fm_mask : 0), _ICR(i2c)); |
| 578 | writel(readl(_ICR(i2c)) | (i2c->high_mode ? i2c->hs_mask : 0), _ICR(i2c)); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 579 | |
| 580 | #ifdef CONFIG_I2C_PXA_SLAVE |
Russell King | 6fd60fa | 2005-09-08 21:04:58 +0100 | [diff] [blame] | 581 | dev_info(&i2c->adap.dev, "Enabling slave mode\n"); |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 582 | writel(readl(_ICR(i2c)) | ICR_SADIE | ICR_ALDIE | ICR_SSDIE, _ICR(i2c)); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 583 | #endif |
| 584 | |
| 585 | i2c_pxa_set_slave(i2c, 0); |
| 586 | |
| 587 | /* enable unit */ |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 588 | writel(readl(_ICR(i2c)) | ICR_IUE, _ICR(i2c)); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 589 | udelay(100); |
| 590 | } |
| 591 | |
| 592 | |
| 593 | #ifdef CONFIG_I2C_PXA_SLAVE |
| 594 | /* |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 595 | * PXA I2C Slave mode |
| 596 | */ |
| 597 | |
| 598 | static void i2c_pxa_slave_txempty(struct pxa_i2c *i2c, u32 isr) |
| 599 | { |
| 600 | if (isr & ISR_BED) { |
| 601 | /* what should we do here? */ |
| 602 | } else { |
Patrick Williams | 4d51b4c | 2019-10-01 10:59:59 -0500 | [diff] [blame] | 603 | u8 byte = 0; |
Russell King | 84b5abe | 2006-10-28 22:30:17 +0100 | [diff] [blame] | 604 | |
| 605 | if (i2c->slave != NULL) |
Patrick Williams | 4d51b4c | 2019-10-01 10:59:59 -0500 | [diff] [blame] | 606 | i2c_slave_event(i2c->slave, I2C_SLAVE_READ_PROCESSED, |
| 607 | &byte); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 608 | |
Patrick Williams | 4d51b4c | 2019-10-01 10:59:59 -0500 | [diff] [blame] | 609 | writel(byte, _IDBR(i2c)); |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 610 | writel(readl(_ICR(i2c)) | ICR_TB, _ICR(i2c)); /* allow next byte */ |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 611 | } |
| 612 | } |
| 613 | |
| 614 | static void i2c_pxa_slave_rxfull(struct pxa_i2c *i2c, u32 isr) |
| 615 | { |
Patrick Williams | 4d51b4c | 2019-10-01 10:59:59 -0500 | [diff] [blame] | 616 | u8 byte = readl(_IDBR(i2c)); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 617 | |
| 618 | if (i2c->slave != NULL) |
Patrick Williams | 4d51b4c | 2019-10-01 10:59:59 -0500 | [diff] [blame] | 619 | i2c_slave_event(i2c->slave, I2C_SLAVE_WRITE_RECEIVED, &byte); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 620 | |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 621 | writel(readl(_ICR(i2c)) | ICR_TB, _ICR(i2c)); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | static void i2c_pxa_slave_start(struct pxa_i2c *i2c, u32 isr) |
| 625 | { |
| 626 | int timeout; |
| 627 | |
| 628 | if (i2c_debug > 0) |
Russell King | 6fd60fa | 2005-09-08 21:04:58 +0100 | [diff] [blame] | 629 | dev_dbg(&i2c->adap.dev, "SAD, mode is slave-%cx\n", |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 630 | (isr & ISR_RWM) ? 'r' : 't'); |
| 631 | |
Patrick Williams | 4d51b4c | 2019-10-01 10:59:59 -0500 | [diff] [blame] | 632 | if (i2c->slave != NULL) { |
| 633 | if (isr & ISR_RWM) { |
| 634 | u8 byte = 0; |
| 635 | |
| 636 | i2c_slave_event(i2c->slave, I2C_SLAVE_READ_REQUESTED, |
| 637 | &byte); |
| 638 | writel(byte, _IDBR(i2c)); |
| 639 | } else { |
| 640 | i2c_slave_event(i2c->slave, I2C_SLAVE_WRITE_REQUESTED, |
| 641 | NULL); |
| 642 | } |
| 643 | } |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 644 | |
| 645 | /* |
| 646 | * slave could interrupt in the middle of us generating a |
| 647 | * start condition... if this happens, we'd better back off |
| 648 | * and stop holding the poor thing up |
| 649 | */ |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 650 | writel(readl(_ICR(i2c)) & ~(ICR_START|ICR_STOP), _ICR(i2c)); |
| 651 | writel(readl(_ICR(i2c)) | ICR_TB, _ICR(i2c)); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 652 | |
| 653 | timeout = 0x10000; |
| 654 | |
| 655 | while (1) { |
Russell King | f8e5d3c | 2020-04-27 19:49:01 +0100 | [diff] [blame] | 656 | if ((readl(_IBMR(i2c)) & IBMR_SCLS) == IBMR_SCLS) |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 657 | break; |
| 658 | |
| 659 | timeout--; |
| 660 | |
| 661 | if (timeout <= 0) { |
Russell King | 6fd60fa | 2005-09-08 21:04:58 +0100 | [diff] [blame] | 662 | dev_err(&i2c->adap.dev, "timeout waiting for SCL high\n"); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 663 | break; |
| 664 | } |
| 665 | } |
| 666 | |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 667 | writel(readl(_ICR(i2c)) & ~ICR_SCLE, _ICR(i2c)); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | static void i2c_pxa_slave_stop(struct pxa_i2c *i2c) |
| 671 | { |
| 672 | if (i2c_debug > 2) |
Russell King | 6fd60fa | 2005-09-08 21:04:58 +0100 | [diff] [blame] | 673 | dev_dbg(&i2c->adap.dev, "ISR: SSD (Slave Stop)\n"); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 674 | |
| 675 | if (i2c->slave != NULL) |
Patrick Williams | 4d51b4c | 2019-10-01 10:59:59 -0500 | [diff] [blame] | 676 | i2c_slave_event(i2c->slave, I2C_SLAVE_STOP, NULL); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 677 | |
| 678 | if (i2c_debug > 2) |
Russell King | 6fd60fa | 2005-09-08 21:04:58 +0100 | [diff] [blame] | 679 | dev_dbg(&i2c->adap.dev, "ISR: SSD (Slave Stop) acked\n"); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 680 | |
| 681 | /* |
| 682 | * If we have a master-mode message waiting, |
| 683 | * kick it off now that the slave has completed. |
| 684 | */ |
| 685 | if (i2c->msg) |
| 686 | i2c_pxa_master_complete(i2c, I2C_RETRY); |
| 687 | } |
Patrick Williams | 4d51b4c | 2019-10-01 10:59:59 -0500 | [diff] [blame] | 688 | |
| 689 | static int i2c_pxa_slave_reg(struct i2c_client *slave) |
| 690 | { |
| 691 | struct pxa_i2c *i2c = slave->adapter->algo_data; |
| 692 | |
| 693 | if (i2c->slave) |
| 694 | return -EBUSY; |
| 695 | |
| 696 | if (!i2c->reg_isar) |
| 697 | return -EAFNOSUPPORT; |
| 698 | |
| 699 | i2c->slave = slave; |
| 700 | i2c->slave_addr = slave->addr; |
| 701 | |
| 702 | writel(i2c->slave_addr, _ISAR(i2c)); |
| 703 | |
| 704 | return 0; |
| 705 | } |
| 706 | |
| 707 | static int i2c_pxa_slave_unreg(struct i2c_client *slave) |
| 708 | { |
| 709 | struct pxa_i2c *i2c = slave->adapter->algo_data; |
| 710 | |
| 711 | WARN_ON(!i2c->slave); |
| 712 | |
| 713 | i2c->slave_addr = I2C_PXA_SLAVE_ADDR; |
| 714 | writel(i2c->slave_addr, _ISAR(i2c)); |
| 715 | |
| 716 | i2c->slave = NULL; |
| 717 | |
| 718 | return 0; |
| 719 | } |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 720 | #else |
| 721 | static void i2c_pxa_slave_txempty(struct pxa_i2c *i2c, u32 isr) |
| 722 | { |
| 723 | if (isr & ISR_BED) { |
| 724 | /* what should we do here? */ |
| 725 | } else { |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 726 | writel(0, _IDBR(i2c)); |
| 727 | writel(readl(_ICR(i2c)) | ICR_TB, _ICR(i2c)); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 728 | } |
| 729 | } |
| 730 | |
| 731 | static void i2c_pxa_slave_rxfull(struct pxa_i2c *i2c, u32 isr) |
| 732 | { |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 733 | writel(readl(_ICR(i2c)) | ICR_TB | ICR_ACKNAK, _ICR(i2c)); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | static void i2c_pxa_slave_start(struct pxa_i2c *i2c, u32 isr) |
| 737 | { |
| 738 | int timeout; |
| 739 | |
| 740 | /* |
| 741 | * slave could interrupt in the middle of us generating a |
| 742 | * start condition... if this happens, we'd better back off |
| 743 | * and stop holding the poor thing up |
| 744 | */ |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 745 | writel(readl(_ICR(i2c)) & ~(ICR_START|ICR_STOP), _ICR(i2c)); |
| 746 | writel(readl(_ICR(i2c)) | ICR_TB | ICR_ACKNAK, _ICR(i2c)); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 747 | |
| 748 | timeout = 0x10000; |
| 749 | |
| 750 | while (1) { |
Russell King | f8e5d3c | 2020-04-27 19:49:01 +0100 | [diff] [blame] | 751 | if ((readl(_IBMR(i2c)) & IBMR_SCLS) == IBMR_SCLS) |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 752 | break; |
| 753 | |
| 754 | timeout--; |
| 755 | |
| 756 | if (timeout <= 0) { |
Russell King | 6fd60fa | 2005-09-08 21:04:58 +0100 | [diff] [blame] | 757 | dev_err(&i2c->adap.dev, "timeout waiting for SCL high\n"); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 758 | break; |
| 759 | } |
| 760 | } |
| 761 | |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 762 | writel(readl(_ICR(i2c)) & ~ICR_SCLE, _ICR(i2c)); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | static void i2c_pxa_slave_stop(struct pxa_i2c *i2c) |
| 766 | { |
| 767 | if (i2c->msg) |
| 768 | i2c_pxa_master_complete(i2c, I2C_RETRY); |
| 769 | } |
| 770 | #endif |
| 771 | |
| 772 | /* |
| 773 | * PXA I2C Master mode |
| 774 | */ |
| 775 | |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 776 | static inline void i2c_pxa_start_message(struct pxa_i2c *i2c) |
| 777 | { |
| 778 | u32 icr; |
| 779 | |
| 780 | /* |
| 781 | * Step 1: target slave address into IDBR |
| 782 | */ |
Russell King | 868d4d3 | 2020-04-27 19:48:36 +0100 | [diff] [blame] | 783 | i2c->req_slave_addr = i2c_8bit_addr_from_msg(i2c->msg); |
| 784 | writel(i2c->req_slave_addr, _IDBR(i2c)); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 785 | |
| 786 | /* |
| 787 | * Step 2: initiate the write. |
| 788 | */ |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 789 | icr = readl(_ICR(i2c)) & ~(ICR_STOP | ICR_ALDIE); |
| 790 | writel(icr | ICR_START | ICR_TB, _ICR(i2c)); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 791 | } |
| 792 | |
Jean Delvare | 7d05481 | 2007-05-01 23:26:33 +0200 | [diff] [blame] | 793 | static inline void i2c_pxa_stop_message(struct pxa_i2c *i2c) |
| 794 | { |
| 795 | u32 icr; |
| 796 | |
| 797 | /* |
| 798 | * Clear the STOP and ACK flags |
| 799 | */ |
| 800 | icr = readl(_ICR(i2c)); |
| 801 | icr &= ~(ICR_STOP | ICR_ACKNAK); |
Russell King | 0cfe61e | 2007-05-10 03:15:32 -0700 | [diff] [blame] | 802 | writel(icr, _ICR(i2c)); |
Jean Delvare | 7d05481 | 2007-05-01 23:26:33 +0200 | [diff] [blame] | 803 | } |
| 804 | |
Leilei Shang | 9d3dda5 | 2013-06-07 14:38:17 +0800 | [diff] [blame] | 805 | /* |
| 806 | * PXA I2C send master code |
| 807 | * 1. Load master code to IDBR and send it. |
| 808 | * Note for HS mode, set ICR [GPIOEN]. |
| 809 | * 2. Wait until win arbitration. |
| 810 | */ |
| 811 | static int i2c_pxa_send_mastercode(struct pxa_i2c *i2c) |
| 812 | { |
| 813 | u32 icr; |
| 814 | long timeout; |
| 815 | |
| 816 | spin_lock_irq(&i2c->lock); |
| 817 | i2c->highmode_enter = true; |
| 818 | writel(i2c->master_code, _IDBR(i2c)); |
| 819 | |
| 820 | icr = readl(_ICR(i2c)) & ~(ICR_STOP | ICR_ALDIE); |
| 821 | icr |= ICR_GPIOEN | ICR_START | ICR_TB | ICR_ITEIE; |
| 822 | writel(icr, _ICR(i2c)); |
| 823 | |
| 824 | spin_unlock_irq(&i2c->lock); |
| 825 | timeout = wait_event_timeout(i2c->wait, |
| 826 | i2c->highmode_enter == false, HZ * 1); |
| 827 | |
| 828 | i2c->highmode_enter = false; |
| 829 | |
| 830 | return (timeout == 0) ? I2C_RETRY : 0; |
| 831 | } |
| 832 | |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 833 | /* |
| 834 | * i2c_pxa_master_complete - complete the message and wake up. |
| 835 | */ |
| 836 | static void i2c_pxa_master_complete(struct pxa_i2c *i2c, int ret) |
| 837 | { |
| 838 | i2c->msg_ptr = 0; |
| 839 | i2c->msg = NULL; |
| 840 | i2c->msg_idx ++; |
| 841 | i2c->msg_num = 0; |
| 842 | if (ret) |
| 843 | i2c->msg_idx = ret; |
Mike Rapoport | b7a3670 | 2008-01-27 18:14:50 +0100 | [diff] [blame] | 844 | if (!i2c->use_pio) |
| 845 | wake_up(&i2c->wait); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 846 | } |
| 847 | |
| 848 | static void i2c_pxa_irq_txempty(struct pxa_i2c *i2c, u32 isr) |
| 849 | { |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 850 | u32 icr = readl(_ICR(i2c)) & ~(ICR_START|ICR_STOP|ICR_ACKNAK|ICR_TB); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 851 | |
| 852 | again: |
| 853 | /* |
| 854 | * If ISR_ALD is set, we lost arbitration. |
| 855 | */ |
| 856 | if (isr & ISR_ALD) { |
| 857 | /* |
| 858 | * Do we need to do anything here? The PXA docs |
| 859 | * are vague about what happens. |
| 860 | */ |
| 861 | i2c_pxa_scream_blue_murder(i2c, "ALD set"); |
| 862 | |
| 863 | /* |
| 864 | * We ignore this error. We seem to see spurious ALDs |
| 865 | * for seemingly no reason. If we handle them as I think |
| 866 | * they should, we end up causing an I2C error, which |
| 867 | * is painful for some systems. |
| 868 | */ |
| 869 | return; /* ignore */ |
| 870 | } |
| 871 | |
Petr Cvek | 86261fd | 2014-11-25 06:05:33 +0100 | [diff] [blame] | 872 | if ((isr & ISR_BED) && |
| 873 | (!((i2c->msg->flags & I2C_M_IGNORE_NAK) && |
| 874 | (isr & ISR_ACKNAK)))) { |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 875 | int ret = BUS_ERROR; |
| 876 | |
| 877 | /* |
| 878 | * I2C bus error - either the device NAK'd us, or |
| 879 | * something more serious happened. If we were NAK'd |
| 880 | * on the initial address phase, we can retry. |
| 881 | */ |
| 882 | if (isr & ISR_ACKNAK) { |
| 883 | if (i2c->msg_ptr == 0 && i2c->msg_idx == 0) |
| 884 | ret = I2C_RETRY; |
| 885 | else |
| 886 | ret = XFER_NAKED; |
| 887 | } |
| 888 | i2c_pxa_master_complete(i2c, ret); |
| 889 | } else if (isr & ISR_RWM) { |
| 890 | /* |
| 891 | * Read mode. We have just sent the address byte, and |
| 892 | * now we must initiate the transfer. |
| 893 | */ |
| 894 | if (i2c->msg_ptr == i2c->msg->len - 1 && |
| 895 | i2c->msg_idx == i2c->msg_num - 1) |
| 896 | icr |= ICR_STOP | ICR_ACKNAK; |
| 897 | |
| 898 | icr |= ICR_ALDIE | ICR_TB; |
| 899 | } else if (i2c->msg_ptr < i2c->msg->len) { |
| 900 | /* |
| 901 | * Write mode. Write the next data byte. |
| 902 | */ |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 903 | writel(i2c->msg->buf[i2c->msg_ptr++], _IDBR(i2c)); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 904 | |
| 905 | icr |= ICR_ALDIE | ICR_TB; |
| 906 | |
| 907 | /* |
Petr Cvek | 86261fd | 2014-11-25 06:05:33 +0100 | [diff] [blame] | 908 | * If this is the last byte of the last message or last byte |
| 909 | * of any message with I2C_M_STOP (e.g. SCCB), send a STOP. |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 910 | */ |
Petr Cvek | 86261fd | 2014-11-25 06:05:33 +0100 | [diff] [blame] | 911 | if ((i2c->msg_ptr == i2c->msg->len) && |
| 912 | ((i2c->msg->flags & I2C_M_STOP) || |
| 913 | (i2c->msg_idx == i2c->msg_num - 1))) |
| 914 | icr |= ICR_STOP; |
| 915 | |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 916 | } else if (i2c->msg_idx < i2c->msg_num - 1) { |
| 917 | /* |
| 918 | * Next segment of the message. |
| 919 | */ |
| 920 | i2c->msg_ptr = 0; |
| 921 | i2c->msg_idx ++; |
| 922 | i2c->msg++; |
| 923 | |
| 924 | /* |
| 925 | * If we aren't doing a repeated start and address, |
| 926 | * go back and try to send the next byte. Note that |
| 927 | * we do not support switching the R/W direction here. |
| 928 | */ |
| 929 | if (i2c->msg->flags & I2C_M_NOSTART) |
| 930 | goto again; |
| 931 | |
| 932 | /* |
| 933 | * Write the next address. |
| 934 | */ |
Russell King | 868d4d3 | 2020-04-27 19:48:36 +0100 | [diff] [blame] | 935 | i2c->req_slave_addr = i2c_8bit_addr_from_msg(i2c->msg); |
| 936 | writel(i2c->req_slave_addr, _IDBR(i2c)); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 937 | |
| 938 | /* |
| 939 | * And trigger a repeated start, and send the byte. |
| 940 | */ |
| 941 | icr &= ~ICR_ALDIE; |
| 942 | icr |= ICR_START | ICR_TB; |
| 943 | } else { |
| 944 | if (i2c->msg->len == 0) { |
| 945 | /* |
| 946 | * Device probes have a message length of zero |
| 947 | * and need the bus to be reset before it can |
| 948 | * be used again. |
| 949 | */ |
| 950 | i2c_pxa_reset(i2c); |
| 951 | } |
| 952 | i2c_pxa_master_complete(i2c, 0); |
| 953 | } |
| 954 | |
| 955 | i2c->icrlog[i2c->irqlogidx-1] = icr; |
| 956 | |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 957 | writel(icr, _ICR(i2c)); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 958 | show_state(i2c); |
| 959 | } |
| 960 | |
| 961 | static void i2c_pxa_irq_rxfull(struct pxa_i2c *i2c, u32 isr) |
| 962 | { |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 963 | u32 icr = readl(_ICR(i2c)) & ~(ICR_START|ICR_STOP|ICR_ACKNAK|ICR_TB); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 964 | |
| 965 | /* |
| 966 | * Read the byte. |
| 967 | */ |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 968 | i2c->msg->buf[i2c->msg_ptr++] = readl(_IDBR(i2c)); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 969 | |
| 970 | if (i2c->msg_ptr < i2c->msg->len) { |
| 971 | /* |
| 972 | * If this is the last byte of the last |
| 973 | * message, send a STOP. |
| 974 | */ |
| 975 | if (i2c->msg_ptr == i2c->msg->len - 1) |
| 976 | icr |= ICR_STOP | ICR_ACKNAK; |
| 977 | |
| 978 | icr |= ICR_ALDIE | ICR_TB; |
| 979 | } else { |
| 980 | i2c_pxa_master_complete(i2c, 0); |
| 981 | } |
| 982 | |
| 983 | i2c->icrlog[i2c->irqlogidx-1] = icr; |
| 984 | |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 985 | writel(icr, _ICR(i2c)); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 986 | } |
| 987 | |
Sebastian Andrzej Siewior | c66dc52 | 2011-02-23 12:38:18 +0100 | [diff] [blame] | 988 | #define VALID_INT_SOURCE (ISR_SSD | ISR_ALD | ISR_ITE | ISR_IRF | \ |
| 989 | ISR_SAD | ISR_BED) |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 990 | static irqreturn_t i2c_pxa_handler(int this_irq, void *dev_id) |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 991 | { |
| 992 | struct pxa_i2c *i2c = dev_id; |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 993 | u32 isr = readl(_ISR(i2c)); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 994 | |
Vasily Khoruzhick | 97491ba | 2011-03-13 15:53:29 +0200 | [diff] [blame] | 995 | if (!(isr & VALID_INT_SOURCE)) |
Sebastian Andrzej Siewior | c66dc52 | 2011-02-23 12:38:18 +0100 | [diff] [blame] | 996 | return IRQ_NONE; |
| 997 | |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 998 | if (i2c_debug > 2 && 0) { |
Russell King | 6fd60fa | 2005-09-08 21:04:58 +0100 | [diff] [blame] | 999 | dev_dbg(&i2c->adap.dev, "%s: ISR=%08x, ICR=%08x, IBMR=%02x\n", |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 1000 | __func__, isr, readl(_ICR(i2c)), readl(_IBMR(i2c))); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 1001 | decode_ISR(isr); |
| 1002 | } |
| 1003 | |
Tobias Klauser | 7e3d7db | 2006-01-09 23:19:51 +0100 | [diff] [blame] | 1004 | if (i2c->irqlogidx < ARRAY_SIZE(i2c->isrlog)) |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 1005 | i2c->isrlog[i2c->irqlogidx++] = isr; |
| 1006 | |
| 1007 | show_state(i2c); |
| 1008 | |
| 1009 | /* |
| 1010 | * Always clear all pending IRQs. |
| 1011 | */ |
Vasily Khoruzhick | 97491ba | 2011-03-13 15:53:29 +0200 | [diff] [blame] | 1012 | writel(isr & VALID_INT_SOURCE, _ISR(i2c)); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 1013 | |
| 1014 | if (isr & ISR_SAD) |
| 1015 | i2c_pxa_slave_start(i2c, isr); |
| 1016 | if (isr & ISR_SSD) |
| 1017 | i2c_pxa_slave_stop(i2c); |
| 1018 | |
| 1019 | if (i2c_pxa_is_slavemode(i2c)) { |
| 1020 | if (isr & ISR_ITE) |
| 1021 | i2c_pxa_slave_txempty(i2c, isr); |
| 1022 | if (isr & ISR_IRF) |
| 1023 | i2c_pxa_slave_rxfull(i2c, isr); |
Leilei Shang | 9d3dda5 | 2013-06-07 14:38:17 +0800 | [diff] [blame] | 1024 | } else if (i2c->msg && (!i2c->highmode_enter)) { |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 1025 | if (isr & ISR_ITE) |
| 1026 | i2c_pxa_irq_txempty(i2c, isr); |
| 1027 | if (isr & ISR_IRF) |
| 1028 | i2c_pxa_irq_rxfull(i2c, isr); |
Leilei Shang | 9d3dda5 | 2013-06-07 14:38:17 +0800 | [diff] [blame] | 1029 | } else if ((isr & ISR_ITE) && i2c->highmode_enter) { |
| 1030 | i2c->highmode_enter = false; |
| 1031 | wake_up(&i2c->wait); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 1032 | } else { |
| 1033 | i2c_pxa_scream_blue_murder(i2c, "spurious irq"); |
| 1034 | } |
| 1035 | |
| 1036 | return IRQ_HANDLED; |
| 1037 | } |
| 1038 | |
Russell King | 1ae49a1 | 2020-04-27 19:48:51 +0100 | [diff] [blame] | 1039 | /* |
| 1040 | * We are protected by the adapter bus mutex. |
| 1041 | */ |
| 1042 | static int i2c_pxa_do_xfer(struct pxa_i2c *i2c, struct i2c_msg *msg, int num) |
| 1043 | { |
| 1044 | long timeout; |
| 1045 | int ret; |
| 1046 | |
| 1047 | /* |
| 1048 | * Wait for the bus to become free. |
| 1049 | */ |
| 1050 | ret = i2c_pxa_wait_bus_not_busy(i2c); |
| 1051 | if (ret) { |
| 1052 | dev_err(&i2c->adap.dev, "i2c_pxa: timeout waiting for bus free\n"); |
| 1053 | goto out; |
| 1054 | } |
| 1055 | |
| 1056 | /* |
| 1057 | * Set master mode. |
| 1058 | */ |
| 1059 | ret = i2c_pxa_set_master(i2c); |
| 1060 | if (ret) { |
| 1061 | dev_err(&i2c->adap.dev, "i2c_pxa_set_master: error %d\n", ret); |
| 1062 | goto out; |
| 1063 | } |
| 1064 | |
| 1065 | if (i2c->high_mode) { |
| 1066 | ret = i2c_pxa_send_mastercode(i2c); |
| 1067 | if (ret) { |
| 1068 | dev_err(&i2c->adap.dev, "i2c_pxa_send_mastercode timeout\n"); |
| 1069 | goto out; |
| 1070 | } |
| 1071 | } |
| 1072 | |
| 1073 | spin_lock_irq(&i2c->lock); |
| 1074 | |
| 1075 | i2c->msg = msg; |
| 1076 | i2c->msg_num = num; |
| 1077 | i2c->msg_idx = 0; |
| 1078 | i2c->msg_ptr = 0; |
| 1079 | i2c->irqlogidx = 0; |
| 1080 | |
| 1081 | i2c_pxa_start_message(i2c); |
| 1082 | |
| 1083 | spin_unlock_irq(&i2c->lock); |
| 1084 | |
| 1085 | /* |
| 1086 | * The rest of the processing occurs in the interrupt handler. |
| 1087 | */ |
| 1088 | timeout = wait_event_timeout(i2c->wait, i2c->msg_num == 0, HZ * 5); |
| 1089 | i2c_pxa_stop_message(i2c); |
| 1090 | |
| 1091 | /* |
| 1092 | * We place the return code in i2c->msg_idx. |
| 1093 | */ |
| 1094 | ret = i2c->msg_idx; |
| 1095 | |
| 1096 | if (!timeout && i2c->msg_num) { |
| 1097 | i2c_pxa_scream_blue_murder(i2c, "timeout"); |
| 1098 | ret = I2C_RETRY; |
| 1099 | } |
| 1100 | |
| 1101 | out: |
| 1102 | return ret; |
| 1103 | } |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 1104 | |
| 1105 | static int i2c_pxa_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num) |
| 1106 | { |
| 1107 | struct pxa_i2c *i2c = adap->algo_data; |
| 1108 | int ret, i; |
| 1109 | |
| 1110 | for (i = adap->retries; i >= 0; i--) { |
| 1111 | ret = i2c_pxa_do_xfer(i2c, msgs, num); |
| 1112 | if (ret != I2C_RETRY) |
| 1113 | goto out; |
| 1114 | |
| 1115 | if (i2c_debug) |
Russell King | 6fd60fa | 2005-09-08 21:04:58 +0100 | [diff] [blame] | 1116 | dev_dbg(&adap->dev, "Retrying transmission\n"); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 1117 | udelay(100); |
| 1118 | } |
| 1119 | i2c_pxa_scream_blue_murder(i2c, "exhausted retries"); |
| 1120 | ret = -EREMOTEIO; |
| 1121 | out: |
| 1122 | i2c_pxa_set_slave(i2c, ret); |
| 1123 | return ret; |
| 1124 | } |
| 1125 | |
Russell King | da16e32 | 2005-09-14 22:54:45 +0100 | [diff] [blame] | 1126 | static u32 i2c_pxa_functionality(struct i2c_adapter *adap) |
| 1127 | { |
Petr Cvek | 86261fd | 2014-11-25 06:05:33 +0100 | [diff] [blame] | 1128 | return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | |
| 1129 | I2C_FUNC_PROTOCOL_MANGLING | I2C_FUNC_NOSTART; |
Russell King | da16e32 | 2005-09-14 22:54:45 +0100 | [diff] [blame] | 1130 | } |
| 1131 | |
Jean Delvare | 8f9082c | 2006-09-03 22:39:46 +0200 | [diff] [blame] | 1132 | static const struct i2c_algorithm i2c_pxa_algorithm = { |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 1133 | .master_xfer = i2c_pxa_xfer, |
Russell King | da16e32 | 2005-09-14 22:54:45 +0100 | [diff] [blame] | 1134 | .functionality = i2c_pxa_functionality, |
Patrick Williams | 4d51b4c | 2019-10-01 10:59:59 -0500 | [diff] [blame] | 1135 | #ifdef CONFIG_I2C_PXA_SLAVE |
| 1136 | .reg_slave = i2c_pxa_slave_reg, |
| 1137 | .unreg_slave = i2c_pxa_slave_unreg, |
| 1138 | #endif |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 1139 | }; |
| 1140 | |
Russell King | 1ae49a1 | 2020-04-27 19:48:51 +0100 | [diff] [blame] | 1141 | /* Non-interrupt mode support */ |
| 1142 | static int i2c_pxa_pio_set_master(struct pxa_i2c *i2c) |
| 1143 | { |
| 1144 | /* make timeout the same as for interrupt based functions */ |
| 1145 | long timeout = 2 * DEF_TIMEOUT; |
| 1146 | |
| 1147 | /* |
| 1148 | * Wait for the bus to become free. |
| 1149 | */ |
| 1150 | while (timeout-- && readl(_ISR(i2c)) & (ISR_IBB | ISR_UB)) { |
| 1151 | udelay(1000); |
| 1152 | show_state(i2c); |
| 1153 | } |
| 1154 | |
| 1155 | if (timeout < 0) { |
| 1156 | show_state(i2c); |
| 1157 | dev_err(&i2c->adap.dev, |
| 1158 | "i2c_pxa: timeout waiting for bus free\n"); |
| 1159 | return I2C_RETRY; |
| 1160 | } |
| 1161 | |
| 1162 | /* |
| 1163 | * Set master mode. |
| 1164 | */ |
| 1165 | writel(readl(_ICR(i2c)) | ICR_SCLE, _ICR(i2c)); |
| 1166 | |
| 1167 | return 0; |
| 1168 | } |
| 1169 | |
| 1170 | static int i2c_pxa_do_pio_xfer(struct pxa_i2c *i2c, |
| 1171 | struct i2c_msg *msg, int num) |
| 1172 | { |
| 1173 | unsigned long timeout = 500000; /* 5 seconds */ |
| 1174 | int ret = 0; |
| 1175 | |
| 1176 | ret = i2c_pxa_pio_set_master(i2c); |
| 1177 | if (ret) |
| 1178 | goto out; |
| 1179 | |
| 1180 | i2c->msg = msg; |
| 1181 | i2c->msg_num = num; |
| 1182 | i2c->msg_idx = 0; |
| 1183 | i2c->msg_ptr = 0; |
| 1184 | i2c->irqlogidx = 0; |
| 1185 | |
| 1186 | i2c_pxa_start_message(i2c); |
| 1187 | |
| 1188 | while (i2c->msg_num > 0 && --timeout) { |
| 1189 | i2c_pxa_handler(0, i2c); |
| 1190 | udelay(10); |
| 1191 | } |
| 1192 | |
| 1193 | i2c_pxa_stop_message(i2c); |
| 1194 | |
| 1195 | /* |
| 1196 | * We place the return code in i2c->msg_idx. |
| 1197 | */ |
| 1198 | ret = i2c->msg_idx; |
| 1199 | |
| 1200 | out: |
| 1201 | if (timeout == 0) { |
| 1202 | i2c_pxa_scream_blue_murder(i2c, "timeout"); |
| 1203 | ret = I2C_RETRY; |
| 1204 | } |
| 1205 | |
| 1206 | return ret; |
| 1207 | } |
| 1208 | |
| 1209 | static int i2c_pxa_pio_xfer(struct i2c_adapter *adap, |
| 1210 | struct i2c_msg msgs[], int num) |
| 1211 | { |
| 1212 | struct pxa_i2c *i2c = adap->algo_data; |
| 1213 | int ret, i; |
| 1214 | |
| 1215 | /* If the I2C controller is disabled we need to reset it |
| 1216 | (probably due to a suspend/resume destroying state). We do |
| 1217 | this here as we can then avoid worrying about resuming the |
| 1218 | controller before its users. */ |
| 1219 | if (!(readl(_ICR(i2c)) & ICR_IUE)) |
| 1220 | i2c_pxa_reset(i2c); |
| 1221 | |
| 1222 | for (i = adap->retries; i >= 0; i--) { |
| 1223 | ret = i2c_pxa_do_pio_xfer(i2c, msgs, num); |
| 1224 | if (ret != I2C_RETRY) |
| 1225 | goto out; |
| 1226 | |
| 1227 | if (i2c_debug) |
| 1228 | dev_dbg(&adap->dev, "Retrying transmission\n"); |
| 1229 | udelay(100); |
| 1230 | } |
| 1231 | i2c_pxa_scream_blue_murder(i2c, "exhausted retries"); |
| 1232 | ret = -EREMOTEIO; |
| 1233 | out: |
| 1234 | i2c_pxa_set_slave(i2c, ret); |
| 1235 | return ret; |
| 1236 | } |
| 1237 | |
Mike Rapoport | b7a3670 | 2008-01-27 18:14:50 +0100 | [diff] [blame] | 1238 | static const struct i2c_algorithm i2c_pxa_pio_algorithm = { |
| 1239 | .master_xfer = i2c_pxa_pio_xfer, |
| 1240 | .functionality = i2c_pxa_functionality, |
Patrick Williams | 4d51b4c | 2019-10-01 10:59:59 -0500 | [diff] [blame] | 1241 | #ifdef CONFIG_I2C_PXA_SLAVE |
| 1242 | .reg_slave = i2c_pxa_slave_reg, |
| 1243 | .unreg_slave = i2c_pxa_slave_unreg, |
| 1244 | #endif |
Mike Rapoport | b7a3670 | 2008-01-27 18:14:50 +0100 | [diff] [blame] | 1245 | }; |
| 1246 | |
Haojian Zhuang | 63fe122 | 2012-03-01 13:04:44 +0800 | [diff] [blame] | 1247 | static int i2c_pxa_probe_dt(struct platform_device *pdev, struct pxa_i2c *i2c, |
| 1248 | enum pxa_i2c_types *i2c_types) |
| 1249 | { |
| 1250 | struct device_node *np = pdev->dev.of_node; |
| 1251 | const struct of_device_id *of_id = |
| 1252 | of_match_device(i2c_pxa_dt_ids, &pdev->dev); |
Haojian Zhuang | 63fe122 | 2012-03-01 13:04:44 +0800 | [diff] [blame] | 1253 | |
| 1254 | if (!of_id) |
| 1255 | return 1; |
Doug Anderson | fe69c55 | 2013-03-01 06:57:32 +0000 | [diff] [blame] | 1256 | |
| 1257 | /* For device tree we always use the dynamic or alias-assigned ID */ |
| 1258 | i2c->adap.nr = -1; |
| 1259 | |
Haojian Zhuang | 63fe122 | 2012-03-01 13:04:44 +0800 | [diff] [blame] | 1260 | if (of_get_property(np, "mrvl,i2c-polling", NULL)) |
| 1261 | i2c->use_pio = 1; |
| 1262 | if (of_get_property(np, "mrvl,i2c-fast-mode", NULL)) |
| 1263 | i2c->fast_mode = 1; |
Yipeng Yao | e2b498f | 2015-07-14 13:06:43 +0530 | [diff] [blame] | 1264 | |
| 1265 | *i2c_types = (enum pxa_i2c_types)(of_id->data); |
| 1266 | |
Haojian Zhuang | 63fe122 | 2012-03-01 13:04:44 +0800 | [diff] [blame] | 1267 | return 0; |
| 1268 | } |
| 1269 | |
| 1270 | static int i2c_pxa_probe_pdata(struct platform_device *pdev, |
| 1271 | struct pxa_i2c *i2c, |
| 1272 | enum pxa_i2c_types *i2c_types) |
| 1273 | { |
Jingoo Han | 6d4028c | 2013-07-30 16:59:33 +0900 | [diff] [blame] | 1274 | struct i2c_pxa_platform_data *plat = dev_get_platdata(&pdev->dev); |
Haojian Zhuang | 63fe122 | 2012-03-01 13:04:44 +0800 | [diff] [blame] | 1275 | const struct platform_device_id *id = platform_get_device_id(pdev); |
| 1276 | |
| 1277 | *i2c_types = id->driver_data; |
| 1278 | if (plat) { |
| 1279 | i2c->use_pio = plat->use_pio; |
| 1280 | i2c->fast_mode = plat->fast_mode; |
Leilei Shang | 9d3dda5 | 2013-06-07 14:38:17 +0800 | [diff] [blame] | 1281 | i2c->high_mode = plat->high_mode; |
| 1282 | i2c->master_code = plat->master_code; |
| 1283 | if (!i2c->master_code) |
| 1284 | i2c->master_code = 0xe; |
| 1285 | i2c->rate = plat->rate; |
Haojian Zhuang | 63fe122 | 2012-03-01 13:04:44 +0800 | [diff] [blame] | 1286 | } |
| 1287 | return 0; |
| 1288 | } |
| 1289 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1290 | static int i2c_pxa_probe(struct platform_device *dev) |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 1291 | { |
Jingoo Han | 6d4028c | 2013-07-30 16:59:33 +0900 | [diff] [blame] | 1292 | struct i2c_pxa_platform_data *plat = dev_get_platdata(&dev->dev); |
Haojian Zhuang | 63fe122 | 2012-03-01 13:04:44 +0800 | [diff] [blame] | 1293 | enum pxa_i2c_types i2c_type; |
| 1294 | struct pxa_i2c *i2c; |
| 1295 | struct resource *res = NULL; |
| 1296 | int ret, irq; |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 1297 | |
Vaibhav Hiremath | 51fcce8 | 2015-07-14 13:06:45 +0530 | [diff] [blame] | 1298 | i2c = devm_kzalloc(&dev->dev, sizeof(struct pxa_i2c), GFP_KERNEL); |
| 1299 | if (!i2c) |
| 1300 | return -ENOMEM; |
| 1301 | |
| 1302 | res = platform_get_resource(dev, IORESOURCE_MEM, 0); |
| 1303 | i2c->reg_base = devm_ioremap_resource(&dev->dev, res); |
| 1304 | if (IS_ERR(i2c->reg_base)) |
| 1305 | return PTR_ERR(i2c->reg_base); |
| 1306 | |
| 1307 | irq = platform_get_irq(dev, 0); |
Dejin Zheng | e42688e | 2020-04-16 23:23:45 +0800 | [diff] [blame] | 1308 | if (irq < 0) |
Vaibhav Hiremath | 51fcce8 | 2015-07-14 13:06:45 +0530 | [diff] [blame] | 1309 | return irq; |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 1310 | |
Doug Anderson | fe69c55 | 2013-03-01 06:57:32 +0000 | [diff] [blame] | 1311 | /* Default adapter num to device id; i2c_pxa_probe_dt can override. */ |
| 1312 | i2c->adap.nr = dev->id; |
| 1313 | |
Haojian Zhuang | 63fe122 | 2012-03-01 13:04:44 +0800 | [diff] [blame] | 1314 | ret = i2c_pxa_probe_dt(dev, i2c, &i2c_type); |
| 1315 | if (ret > 0) |
| 1316 | ret = i2c_pxa_probe_pdata(dev, i2c, &i2c_type); |
| 1317 | if (ret < 0) |
Vaibhav Hiremath | 51fcce8 | 2015-07-14 13:06:45 +0530 | [diff] [blame] | 1318 | return ret; |
Haojian Zhuang | 63fe122 | 2012-03-01 13:04:44 +0800 | [diff] [blame] | 1319 | |
Enrico Scholz | 6776f3d | 2007-05-21 12:29:40 +0100 | [diff] [blame] | 1320 | i2c->adap.owner = THIS_MODULE; |
Enrico Scholz | 6776f3d | 2007-05-21 12:29:40 +0100 | [diff] [blame] | 1321 | i2c->adap.retries = 5; |
| 1322 | |
| 1323 | spin_lock_init(&i2c->lock); |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 1324 | init_waitqueue_head(&i2c->wait); |
Enrico Scholz | 6776f3d | 2007-05-21 12:29:40 +0100 | [diff] [blame] | 1325 | |
Doug Anderson | fe69c55 | 2013-03-01 06:57:32 +0000 | [diff] [blame] | 1326 | strlcpy(i2c->adap.name, "pxa_i2c-i2c", sizeof(i2c->adap.name)); |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 1327 | |
Vaibhav Hiremath | 51fcce8 | 2015-07-14 13:06:45 +0530 | [diff] [blame] | 1328 | i2c->clk = devm_clk_get(&dev->dev, NULL); |
Russell King | c3cef3f | 2007-08-20 10:19:10 +0100 | [diff] [blame] | 1329 | if (IS_ERR(i2c->clk)) { |
Vaibhav Hiremath | 51fcce8 | 2015-07-14 13:06:45 +0530 | [diff] [blame] | 1330 | dev_err(&dev->dev, "failed to get the clk: %ld\n", PTR_ERR(i2c->clk)); |
| 1331 | return PTR_ERR(i2c->clk); |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 1332 | } |
Sebastian Andrzej Siewior | d6668c7 | 2011-02-23 12:38:15 +0100 | [diff] [blame] | 1333 | |
| 1334 | i2c->reg_ibmr = i2c->reg_base + pxa_reg_layout[i2c_type].ibmr; |
| 1335 | i2c->reg_idbr = i2c->reg_base + pxa_reg_layout[i2c_type].idbr; |
| 1336 | i2c->reg_icr = i2c->reg_base + pxa_reg_layout[i2c_type].icr; |
| 1337 | i2c->reg_isr = i2c->reg_base + pxa_reg_layout[i2c_type].isr; |
Russell King | ee47893 | 2020-04-27 19:49:07 +0100 | [diff] [blame] | 1338 | i2c->fm_mask = pxa_reg_layout[i2c_type].fm; |
| 1339 | i2c->hs_mask = pxa_reg_layout[i2c_type].hs; |
Romain Perier | 6c14bda | 2016-12-01 12:04:37 +0100 | [diff] [blame] | 1340 | |
Sebastian Andrzej Siewior | 7e94dd1 | 2011-03-02 11:26:53 +0100 | [diff] [blame] | 1341 | if (i2c_type != REGS_CE4100) |
| 1342 | i2c->reg_isar = i2c->reg_base + pxa_reg_layout[i2c_type].isar; |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 1343 | |
Vaibhav Hiremath | c5fa6fc | 2015-08-24 11:29:36 +0530 | [diff] [blame] | 1344 | if (i2c_type == REGS_PXA910) { |
| 1345 | i2c->reg_ilcr = i2c->reg_base + pxa_reg_layout[i2c_type].ilcr; |
| 1346 | i2c->reg_iwcr = i2c->reg_base + pxa_reg_layout[i2c_type].iwcr; |
| 1347 | } |
| 1348 | |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 1349 | i2c->iobase = res->start; |
Linus Walleij | c6ffdde | 2009-06-14 00:20:36 +0200 | [diff] [blame] | 1350 | i2c->iosize = resource_size(res); |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 1351 | |
| 1352 | i2c->irq = irq; |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 1353 | |
| 1354 | i2c->slave_addr = I2C_PXA_SLAVE_ADDR; |
Leilei Shang | 9d3dda5 | 2013-06-07 14:38:17 +0800 | [diff] [blame] | 1355 | i2c->highmode_enter = false; |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 1356 | |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 1357 | if (plat) { |
Haojian Zhuang | 63fe122 | 2012-03-01 13:04:44 +0800 | [diff] [blame] | 1358 | i2c->adap.class = plat->class; |
| 1359 | } |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 1360 | |
Leilei Shang | 9d3dda5 | 2013-06-07 14:38:17 +0800 | [diff] [blame] | 1361 | if (i2c->high_mode) { |
| 1362 | if (i2c->rate) { |
| 1363 | clk_set_rate(i2c->clk, i2c->rate); |
| 1364 | pr_info("i2c: <%s> set rate to %ld\n", |
| 1365 | i2c->adap.name, clk_get_rate(i2c->clk)); |
| 1366 | } else |
| 1367 | pr_warn("i2c: <%s> clock rate not set\n", |
| 1368 | i2c->adap.name); |
| 1369 | } |
| 1370 | |
Daniel Drake | 7a10f47 | 2013-06-17 11:30:36 -0400 | [diff] [blame] | 1371 | clk_prepare_enable(i2c->clk); |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 1372 | |
Mike Rapoport | b7a3670 | 2008-01-27 18:14:50 +0100 | [diff] [blame] | 1373 | if (i2c->use_pio) { |
| 1374 | i2c->adap.algo = &i2c_pxa_pio_algorithm; |
| 1375 | } else { |
| 1376 | i2c->adap.algo = &i2c_pxa_algorithm; |
Vaibhav Hiremath | 51fcce8 | 2015-07-14 13:06:45 +0530 | [diff] [blame] | 1377 | ret = devm_request_irq(&dev->dev, irq, i2c_pxa_handler, |
Leilei Shang | abf8a1f | 2015-07-14 13:06:40 +0530 | [diff] [blame] | 1378 | IRQF_SHARED | IRQF_NO_SUSPEND, |
| 1379 | dev_name(&dev->dev), i2c); |
Vaibhav Hiremath | 51fcce8 | 2015-07-14 13:06:45 +0530 | [diff] [blame] | 1380 | if (ret) { |
| 1381 | dev_err(&dev->dev, "failed to request irq: %d\n", ret); |
Mike Rapoport | b7a3670 | 2008-01-27 18:14:50 +0100 | [diff] [blame] | 1382 | goto ereqirq; |
Vaibhav Hiremath | 51fcce8 | 2015-07-14 13:06:45 +0530 | [diff] [blame] | 1383 | } |
Mike Rapoport | b7a3670 | 2008-01-27 18:14:50 +0100 | [diff] [blame] | 1384 | } |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 1385 | |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 1386 | i2c_pxa_reset(i2c); |
| 1387 | |
| 1388 | i2c->adap.algo_data = i2c; |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1389 | i2c->adap.dev.parent = &dev->dev; |
Sebastian Andrzej Siewior | baa8cab | 2011-02-23 12:38:20 +0100 | [diff] [blame] | 1390 | #ifdef CONFIG_OF |
| 1391 | i2c->adap.dev.of_node = dev->dev.of_node; |
| 1392 | #endif |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 1393 | |
Grant Likely | 488bf31 | 2011-07-25 17:49:43 +0200 | [diff] [blame] | 1394 | ret = i2c_add_numbered_adapter(&i2c->adap); |
Wolfram Sang | ea73440 | 2016-08-09 13:36:17 +0200 | [diff] [blame] | 1395 | if (ret < 0) |
Vaibhav Hiremath | 51fcce8 | 2015-07-14 13:06:45 +0530 | [diff] [blame] | 1396 | goto ereqirq; |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 1397 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1398 | platform_set_drvdata(dev, i2c); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 1399 | |
| 1400 | #ifdef CONFIG_I2C_PXA_SLAVE |
Vaibhav Hiremath | 51fcce8 | 2015-07-14 13:06:45 +0530 | [diff] [blame] | 1401 | dev_info(&i2c->adap.dev, " PXA I2C adapter, slave address %d\n", |
| 1402 | i2c->slave_addr); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 1403 | #else |
Vaibhav Hiremath | 51fcce8 | 2015-07-14 13:06:45 +0530 | [diff] [blame] | 1404 | dev_info(&i2c->adap.dev, " PXA I2C adapter\n"); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 1405 | #endif |
| 1406 | return 0; |
| 1407 | |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 1408 | ereqirq: |
Daniel Drake | 7a10f47 | 2013-06-17 11:30:36 -0400 | [diff] [blame] | 1409 | clk_disable_unprepare(i2c->clk); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 1410 | return ret; |
| 1411 | } |
| 1412 | |
Dmitry Torokhov | 0a6d224 | 2013-02-19 22:50:10 +0000 | [diff] [blame] | 1413 | static int i2c_pxa_remove(struct platform_device *dev) |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 1414 | { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1415 | struct pxa_i2c *i2c = platform_get_drvdata(dev); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 1416 | |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 1417 | i2c_del_adapter(&i2c->adap); |
Russell King | c3cef3f | 2007-08-20 10:19:10 +0100 | [diff] [blame] | 1418 | |
Daniel Drake | 7a10f47 | 2013-06-17 11:30:36 -0400 | [diff] [blame] | 1419 | clk_disable_unprepare(i2c->clk); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 1420 | |
| 1421 | return 0; |
| 1422 | } |
| 1423 | |
Russell King | e7d48fa | 2008-08-26 10:40:50 +0100 | [diff] [blame] | 1424 | #ifdef CONFIG_PM |
Magnus Damm | 57f4d4f | 2009-07-08 13:22:39 +0200 | [diff] [blame] | 1425 | static int i2c_pxa_suspend_noirq(struct device *dev) |
Russell King | e7d48fa | 2008-08-26 10:40:50 +0100 | [diff] [blame] | 1426 | { |
Masahiro Yamada | 9242e72 | 2017-07-28 01:16:24 +0900 | [diff] [blame] | 1427 | struct pxa_i2c *i2c = dev_get_drvdata(dev); |
Magnus Damm | 57f4d4f | 2009-07-08 13:22:39 +0200 | [diff] [blame] | 1428 | |
Russell King | e7d48fa | 2008-08-26 10:40:50 +0100 | [diff] [blame] | 1429 | clk_disable(i2c->clk); |
Magnus Damm | 57f4d4f | 2009-07-08 13:22:39 +0200 | [diff] [blame] | 1430 | |
Russell King | e7d48fa | 2008-08-26 10:40:50 +0100 | [diff] [blame] | 1431 | return 0; |
| 1432 | } |
| 1433 | |
Magnus Damm | 57f4d4f | 2009-07-08 13:22:39 +0200 | [diff] [blame] | 1434 | static int i2c_pxa_resume_noirq(struct device *dev) |
Russell King | e7d48fa | 2008-08-26 10:40:50 +0100 | [diff] [blame] | 1435 | { |
Masahiro Yamada | 9242e72 | 2017-07-28 01:16:24 +0900 | [diff] [blame] | 1436 | struct pxa_i2c *i2c = dev_get_drvdata(dev); |
Russell King | e7d48fa | 2008-08-26 10:40:50 +0100 | [diff] [blame] | 1437 | |
| 1438 | clk_enable(i2c->clk); |
| 1439 | i2c_pxa_reset(i2c); |
| 1440 | |
| 1441 | return 0; |
| 1442 | } |
Magnus Damm | 57f4d4f | 2009-07-08 13:22:39 +0200 | [diff] [blame] | 1443 | |
Alexey Dobriyan | 4714521 | 2009-12-14 18:00:08 -0800 | [diff] [blame] | 1444 | static const struct dev_pm_ops i2c_pxa_dev_pm_ops = { |
Magnus Damm | 57f4d4f | 2009-07-08 13:22:39 +0200 | [diff] [blame] | 1445 | .suspend_noirq = i2c_pxa_suspend_noirq, |
| 1446 | .resume_noirq = i2c_pxa_resume_noirq, |
| 1447 | }; |
| 1448 | |
| 1449 | #define I2C_PXA_DEV_PM_OPS (&i2c_pxa_dev_pm_ops) |
Russell King | e7d48fa | 2008-08-26 10:40:50 +0100 | [diff] [blame] | 1450 | #else |
Magnus Damm | 57f4d4f | 2009-07-08 13:22:39 +0200 | [diff] [blame] | 1451 | #define I2C_PXA_DEV_PM_OPS NULL |
Russell King | e7d48fa | 2008-08-26 10:40:50 +0100 | [diff] [blame] | 1452 | #endif |
| 1453 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1454 | static struct platform_driver i2c_pxa_driver = { |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 1455 | .probe = i2c_pxa_probe, |
Dmitry Torokhov | 0a6d224 | 2013-02-19 22:50:10 +0000 | [diff] [blame] | 1456 | .remove = i2c_pxa_remove, |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1457 | .driver = { |
| 1458 | .name = "pxa2xx-i2c", |
Magnus Damm | 57f4d4f | 2009-07-08 13:22:39 +0200 | [diff] [blame] | 1459 | .pm = I2C_PXA_DEV_PM_OPS, |
Haojian Zhuang | 63fe122 | 2012-03-01 13:04:44 +0800 | [diff] [blame] | 1460 | .of_match_table = i2c_pxa_dt_ids, |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1461 | }, |
Eric Miao | f23d491 | 2009-04-13 14:43:25 +0800 | [diff] [blame] | 1462 | .id_table = i2c_pxa_id_table, |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 1463 | }; |
| 1464 | |
| 1465 | static int __init i2c_adap_pxa_init(void) |
| 1466 | { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1467 | return platform_driver_register(&i2c_pxa_driver); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 1468 | } |
| 1469 | |
Wolfram Sang | a92b36e | 2008-02-24 20:03:42 +0100 | [diff] [blame] | 1470 | static void __exit i2c_adap_pxa_exit(void) |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 1471 | { |
Holger Schurig | d6a7b5f | 2008-02-11 16:51:41 +0100 | [diff] [blame] | 1472 | platform_driver_unregister(&i2c_pxa_driver); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 1473 | } |
| 1474 | |
Richard Purdie | ece5f7b | 2006-01-12 16:30:23 +0000 | [diff] [blame] | 1475 | MODULE_LICENSE("GPL"); |
Kay Sievers | add8eda | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 1476 | MODULE_ALIAS("platform:pxa2xx-i2c"); |
Richard Purdie | ece5f7b | 2006-01-12 16:30:23 +0000 | [diff] [blame] | 1477 | |
Uli Luckas | 47a9b13 | 2008-07-14 22:38:30 +0200 | [diff] [blame] | 1478 | subsys_initcall(i2c_adap_pxa_init); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 1479 | module_exit(i2c_adap_pxa_exit); |