Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 1 | /* |
| 2 | * i2c_adap_pxa.c |
| 3 | * |
| 4 | * I2C adapter for the PXA I2C bus access. |
| 5 | * |
| 6 | * Copyright (C) 2002 Intrinsyc Software Inc. |
| 7 | * Copyright (C) 2004-2005 Deep Blue Solutions Ltd. |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License version 2 as |
| 11 | * published by the Free Software Foundation. |
| 12 | * |
| 13 | * History: |
| 14 | * Apr 2002: Initial version [CS] |
Daniel Mack | 3ad2f3f | 2010-02-03 08:01:28 +0800 | [diff] [blame] | 15 | * Jun 2002: Properly separated algo/adap [FB] |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 16 | * Jan 2003: Fixed several bugs concerning interrupt handling [Kai-Uwe Bloem] |
| 17 | * Jan 2003: added limited signal handling [Kai-Uwe Bloem] |
| 18 | * Sep 2004: Major rework to ensure efficient bus handling [RMK] |
| 19 | * Dec 2004: Added support for PXA27x and slave device probing [Liam Girdwood] |
| 20 | * Feb 2005: Rework slave mode handling [RMK] |
| 21 | */ |
| 22 | #include <linux/kernel.h> |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/i2c.h> |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 25 | #include <linux/init.h> |
| 26 | #include <linux/time.h> |
| 27 | #include <linux/sched.h> |
| 28 | #include <linux/delay.h> |
| 29 | #include <linux/errno.h> |
| 30 | #include <linux/interrupt.h> |
| 31 | #include <linux/i2c-pxa.h> |
Russell King | d052d1b | 2005-10-29 19:07:23 +0100 | [diff] [blame] | 32 | #include <linux/platform_device.h> |
Russell King | c3cef3f | 2007-08-20 10:19:10 +0100 | [diff] [blame] | 33 | #include <linux/err.h> |
| 34 | #include <linux/clk.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 35 | #include <linux/slab.h> |
H Hartley Sweeten | 2178218 | 2010-05-21 18:41:01 +0200 | [diff] [blame] | 36 | #include <linux/io.h> |
Sebastian Andrzej Siewior | b459396 | 2011-02-23 12:38:16 +0100 | [diff] [blame] | 37 | #include <linux/i2c/pxa-i2c.h> |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 38 | |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 39 | #include <asm/irq.h> |
Eric Miao | 283afa0 | 2008-09-08 14:15:08 +0800 | [diff] [blame] | 40 | |
Sebastian Andrzej Siewior | 7e94dd1 | 2011-03-02 11:26:53 +0100 | [diff] [blame^] | 41 | #ifndef CONFIG_HAVE_CLK |
| 42 | #define clk_get(dev, id) NULL |
| 43 | #define clk_put(clk) do { } while (0) |
| 44 | #define clk_disable(clk) do { } while (0) |
| 45 | #define clk_enable(clk) do { } while (0) |
| 46 | #endif |
| 47 | |
Sebastian Andrzej Siewior | d6668c7 | 2011-02-23 12:38:15 +0100 | [diff] [blame] | 48 | struct pxa_reg_layout { |
| 49 | u32 ibmr; |
| 50 | u32 idbr; |
| 51 | u32 icr; |
| 52 | u32 isr; |
| 53 | u32 isar; |
| 54 | }; |
| 55 | |
| 56 | enum pxa_i2c_types { |
| 57 | REGS_PXA2XX, |
| 58 | REGS_PXA3XX, |
Sebastian Andrzej Siewior | 7e94dd1 | 2011-03-02 11:26:53 +0100 | [diff] [blame^] | 59 | REGS_CE4100, |
Sebastian Andrzej Siewior | d6668c7 | 2011-02-23 12:38:15 +0100 | [diff] [blame] | 60 | }; |
| 61 | |
Eric Miao | 283afa0 | 2008-09-08 14:15:08 +0800 | [diff] [blame] | 62 | /* |
Sebastian Andrzej Siewior | d6668c7 | 2011-02-23 12:38:15 +0100 | [diff] [blame] | 63 | * I2C registers definitions |
Eric Miao | f23d491 | 2009-04-13 14:43:25 +0800 | [diff] [blame] | 64 | */ |
Sebastian Andrzej Siewior | d6668c7 | 2011-02-23 12:38:15 +0100 | [diff] [blame] | 65 | static struct pxa_reg_layout pxa_reg_layout[] = { |
| 66 | [REGS_PXA2XX] = { |
| 67 | .ibmr = 0x00, |
| 68 | .idbr = 0x10, |
| 69 | .icr = 0x20, |
| 70 | .isr = 0x30, |
| 71 | .isar = 0x40, |
| 72 | }, |
| 73 | [REGS_PXA3XX] = { |
| 74 | .ibmr = 0x00, |
| 75 | .idbr = 0x08, |
| 76 | .icr = 0x10, |
| 77 | .isr = 0x18, |
| 78 | .isar = 0x20, |
| 79 | }, |
Sebastian Andrzej Siewior | 7e94dd1 | 2011-03-02 11:26:53 +0100 | [diff] [blame^] | 80 | [REGS_CE4100] = { |
| 81 | .ibmr = 0x14, |
| 82 | .idbr = 0x0c, |
| 83 | .icr = 0x00, |
| 84 | .isr = 0x04, |
| 85 | /* no isar register */ |
| 86 | }, |
Sebastian Andrzej Siewior | d6668c7 | 2011-02-23 12:38:15 +0100 | [diff] [blame] | 87 | }; |
Eric Miao | f23d491 | 2009-04-13 14:43:25 +0800 | [diff] [blame] | 88 | |
| 89 | static const struct platform_device_id i2c_pxa_id_table[] = { |
Sebastian Andrzej Siewior | d6668c7 | 2011-02-23 12:38:15 +0100 | [diff] [blame] | 90 | { "pxa2xx-i2c", REGS_PXA2XX }, |
| 91 | { "pxa3xx-pwri2c", REGS_PXA3XX }, |
Sebastian Andrzej Siewior | 7e94dd1 | 2011-03-02 11:26:53 +0100 | [diff] [blame^] | 92 | { "ce4100-i2c", REGS_CE4100 }, |
Eric Miao | f23d491 | 2009-04-13 14:43:25 +0800 | [diff] [blame] | 93 | { }, |
| 94 | }; |
| 95 | MODULE_DEVICE_TABLE(platform, i2c_pxa_id_table); |
| 96 | |
| 97 | /* |
Sebastian Andrzej Siewior | d6668c7 | 2011-02-23 12:38:15 +0100 | [diff] [blame] | 98 | * I2C bit definitions |
Eric Miao | 283afa0 | 2008-09-08 14:15:08 +0800 | [diff] [blame] | 99 | */ |
Eric Miao | 283afa0 | 2008-09-08 14:15:08 +0800 | [diff] [blame] | 100 | |
| 101 | #define ICR_START (1 << 0) /* start bit */ |
| 102 | #define ICR_STOP (1 << 1) /* stop bit */ |
| 103 | #define ICR_ACKNAK (1 << 2) /* send ACK(0) or NAK(1) */ |
| 104 | #define ICR_TB (1 << 3) /* transfer byte bit */ |
| 105 | #define ICR_MA (1 << 4) /* master abort */ |
| 106 | #define ICR_SCLE (1 << 5) /* master clock enable */ |
| 107 | #define ICR_IUE (1 << 6) /* unit enable */ |
| 108 | #define ICR_GCD (1 << 7) /* general call disable */ |
| 109 | #define ICR_ITEIE (1 << 8) /* enable tx interrupts */ |
| 110 | #define ICR_IRFIE (1 << 9) /* enable rx interrupts */ |
| 111 | #define ICR_BEIE (1 << 10) /* enable bus error ints */ |
| 112 | #define ICR_SSDIE (1 << 11) /* slave STOP detected int enable */ |
| 113 | #define ICR_ALDIE (1 << 12) /* enable arbitration interrupt */ |
| 114 | #define ICR_SADIE (1 << 13) /* slave address detected int enable */ |
| 115 | #define ICR_UR (1 << 14) /* unit reset */ |
| 116 | #define ICR_FM (1 << 15) /* fast mode */ |
| 117 | |
| 118 | #define ISR_RWM (1 << 0) /* read/write mode */ |
| 119 | #define ISR_ACKNAK (1 << 1) /* ack/nak status */ |
| 120 | #define ISR_UB (1 << 2) /* unit busy */ |
| 121 | #define ISR_IBB (1 << 3) /* bus busy */ |
| 122 | #define ISR_SSD (1 << 4) /* slave stop detected */ |
| 123 | #define ISR_ALD (1 << 5) /* arbitration loss detected */ |
| 124 | #define ISR_ITE (1 << 6) /* tx buffer empty */ |
| 125 | #define ISR_IRF (1 << 7) /* rx buffer full */ |
| 126 | #define ISR_GCAD (1 << 8) /* general call address detected */ |
| 127 | #define ISR_SAD (1 << 9) /* slave address detected */ |
| 128 | #define ISR_BED (1 << 10) /* bus error no ACK/NAK */ |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 129 | |
| 130 | struct pxa_i2c { |
| 131 | spinlock_t lock; |
| 132 | wait_queue_head_t wait; |
| 133 | struct i2c_msg *msg; |
| 134 | unsigned int msg_num; |
| 135 | unsigned int msg_idx; |
| 136 | unsigned int msg_ptr; |
| 137 | unsigned int slave_addr; |
| 138 | |
| 139 | struct i2c_adapter adap; |
Russell King | c3cef3f | 2007-08-20 10:19:10 +0100 | [diff] [blame] | 140 | struct clk *clk; |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 141 | #ifdef CONFIG_I2C_PXA_SLAVE |
| 142 | struct i2c_slave_client *slave; |
| 143 | #endif |
| 144 | |
| 145 | unsigned int irqlogidx; |
| 146 | u32 isrlog[32]; |
| 147 | u32 icrlog[32]; |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 148 | |
| 149 | void __iomem *reg_base; |
Sebastian Andrzej Siewior | d6668c7 | 2011-02-23 12:38:15 +0100 | [diff] [blame] | 150 | void __iomem *reg_ibmr; |
| 151 | void __iomem *reg_idbr; |
| 152 | void __iomem *reg_icr; |
| 153 | void __iomem *reg_isr; |
| 154 | void __iomem *reg_isar; |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 155 | |
| 156 | unsigned long iobase; |
| 157 | unsigned long iosize; |
| 158 | |
| 159 | int irq; |
Jonathan Cameron | c46c948 | 2008-10-03 15:07:36 +0100 | [diff] [blame] | 160 | unsigned int use_pio :1; |
| 161 | unsigned int fast_mode :1; |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 162 | }; |
| 163 | |
Sebastian Andrzej Siewior | d6668c7 | 2011-02-23 12:38:15 +0100 | [diff] [blame] | 164 | #define _IBMR(i2c) ((i2c)->reg_ibmr) |
| 165 | #define _IDBR(i2c) ((i2c)->reg_idbr) |
| 166 | #define _ICR(i2c) ((i2c)->reg_icr) |
| 167 | #define _ISR(i2c) ((i2c)->reg_isr) |
| 168 | #define _ISAR(i2c) ((i2c)->reg_isar) |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 169 | |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 170 | /* |
| 171 | * I2C Slave mode address |
| 172 | */ |
| 173 | #define I2C_PXA_SLAVE_ADDR 0x1 |
| 174 | |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 175 | #ifdef DEBUG |
| 176 | |
| 177 | struct bits { |
| 178 | u32 mask; |
| 179 | const char *set; |
| 180 | const char *unset; |
| 181 | }; |
Jiri Slaby | ed11399 | 2007-10-18 23:40:28 -0700 | [diff] [blame] | 182 | #define PXA_BIT(m, s, u) { .mask = m, .set = s, .unset = u } |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 183 | |
| 184 | static inline void |
| 185 | decode_bits(const char *prefix, const struct bits *bits, int num, u32 val) |
| 186 | { |
| 187 | printk("%s %08x: ", prefix, val); |
| 188 | while (num--) { |
| 189 | const char *str = val & bits->mask ? bits->set : bits->unset; |
| 190 | if (str) |
| 191 | printk("%s ", str); |
| 192 | bits++; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | static const struct bits isr_bits[] = { |
Jiri Slaby | ed11399 | 2007-10-18 23:40:28 -0700 | [diff] [blame] | 197 | PXA_BIT(ISR_RWM, "RX", "TX"), |
| 198 | PXA_BIT(ISR_ACKNAK, "NAK", "ACK"), |
| 199 | PXA_BIT(ISR_UB, "Bsy", "Rdy"), |
| 200 | PXA_BIT(ISR_IBB, "BusBsy", "BusRdy"), |
| 201 | PXA_BIT(ISR_SSD, "SlaveStop", NULL), |
| 202 | PXA_BIT(ISR_ALD, "ALD", NULL), |
| 203 | PXA_BIT(ISR_ITE, "TxEmpty", NULL), |
| 204 | PXA_BIT(ISR_IRF, "RxFull", NULL), |
| 205 | PXA_BIT(ISR_GCAD, "GenCall", NULL), |
| 206 | PXA_BIT(ISR_SAD, "SlaveAddr", NULL), |
| 207 | PXA_BIT(ISR_BED, "BusErr", NULL), |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 208 | }; |
| 209 | |
| 210 | static void decode_ISR(unsigned int val) |
| 211 | { |
Russell King | 6fd60fa | 2005-09-08 21:04:58 +0100 | [diff] [blame] | 212 | decode_bits(KERN_DEBUG "ISR", isr_bits, ARRAY_SIZE(isr_bits), val); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 213 | printk("\n"); |
| 214 | } |
| 215 | |
| 216 | static const struct bits icr_bits[] = { |
Jiri Slaby | ed11399 | 2007-10-18 23:40:28 -0700 | [diff] [blame] | 217 | PXA_BIT(ICR_START, "START", NULL), |
| 218 | PXA_BIT(ICR_STOP, "STOP", NULL), |
| 219 | PXA_BIT(ICR_ACKNAK, "ACKNAK", NULL), |
| 220 | PXA_BIT(ICR_TB, "TB", NULL), |
| 221 | PXA_BIT(ICR_MA, "MA", NULL), |
| 222 | PXA_BIT(ICR_SCLE, "SCLE", "scle"), |
| 223 | PXA_BIT(ICR_IUE, "IUE", "iue"), |
| 224 | PXA_BIT(ICR_GCD, "GCD", NULL), |
| 225 | PXA_BIT(ICR_ITEIE, "ITEIE", NULL), |
| 226 | PXA_BIT(ICR_IRFIE, "IRFIE", NULL), |
| 227 | PXA_BIT(ICR_BEIE, "BEIE", NULL), |
| 228 | PXA_BIT(ICR_SSDIE, "SSDIE", NULL), |
| 229 | PXA_BIT(ICR_ALDIE, "ALDIE", NULL), |
| 230 | PXA_BIT(ICR_SADIE, "SADIE", NULL), |
| 231 | PXA_BIT(ICR_UR, "UR", "ur"), |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 232 | }; |
| 233 | |
Holger Schurig | d6a7b5f | 2008-02-11 16:51:41 +0100 | [diff] [blame] | 234 | #ifdef CONFIG_I2C_PXA_SLAVE |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 235 | static void decode_ICR(unsigned int val) |
| 236 | { |
Russell King | 6fd60fa | 2005-09-08 21:04:58 +0100 | [diff] [blame] | 237 | decode_bits(KERN_DEBUG "ICR", icr_bits, ARRAY_SIZE(icr_bits), val); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 238 | printk("\n"); |
| 239 | } |
Holger Schurig | d6a7b5f | 2008-02-11 16:51:41 +0100 | [diff] [blame] | 240 | #endif |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 241 | |
| 242 | static unsigned int i2c_debug = DEBUG; |
| 243 | |
| 244 | static void i2c_pxa_show_state(struct pxa_i2c *i2c, int lno, const char *fname) |
| 245 | { |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 246 | dev_dbg(&i2c->adap.dev, "state:%s:%d: ISR=%08x, ICR=%08x, IBMR=%02x\n", fname, lno, |
| 247 | readl(_ISR(i2c)), readl(_ICR(i2c)), readl(_IBMR(i2c))); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 248 | } |
| 249 | |
Harvey Harrison | 08882d2 | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 250 | #define show_state(i2c) i2c_pxa_show_state(i2c, __LINE__, __func__) |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 251 | |
| 252 | static void i2c_pxa_scream_blue_murder(struct pxa_i2c *i2c, const char *why) |
| 253 | { |
| 254 | unsigned int i; |
Frank Seidel | 154d22b | 2009-03-28 21:34:42 +0100 | [diff] [blame] | 255 | printk(KERN_ERR "i2c: error: %s\n", why); |
| 256 | printk(KERN_ERR "i2c: msg_num: %d msg_idx: %d msg_ptr: %d\n", |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 257 | i2c->msg_num, i2c->msg_idx, i2c->msg_ptr); |
Frank Seidel | 154d22b | 2009-03-28 21:34:42 +0100 | [diff] [blame] | 258 | printk(KERN_ERR "i2c: ICR: %08x ISR: %08x\n", |
| 259 | readl(_ICR(i2c)), readl(_ISR(i2c))); |
| 260 | printk(KERN_DEBUG "i2c: log: "); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 261 | for (i = 0; i < i2c->irqlogidx; i++) |
| 262 | printk("[%08x:%08x] ", i2c->isrlog[i], i2c->icrlog[i]); |
| 263 | printk("\n"); |
| 264 | } |
| 265 | |
Wolfram Sang | 0d813d9 | 2009-11-03 12:53:41 +0100 | [diff] [blame] | 266 | #else /* ifdef DEBUG */ |
| 267 | |
| 268 | #define i2c_debug 0 |
| 269 | |
| 270 | #define show_state(i2c) do { } while (0) |
| 271 | #define decode_ISR(val) do { } while (0) |
| 272 | #define decode_ICR(val) do { } while (0) |
| 273 | #define i2c_pxa_scream_blue_murder(i2c, why) do { } while (0) |
| 274 | |
| 275 | #endif /* ifdef DEBUG / else */ |
| 276 | |
| 277 | static void i2c_pxa_master_complete(struct pxa_i2c *i2c, int ret); |
| 278 | static irqreturn_t i2c_pxa_handler(int this_irq, void *dev_id); |
| 279 | |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 280 | static inline int i2c_pxa_is_slavemode(struct pxa_i2c *i2c) |
| 281 | { |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 282 | return !(readl(_ICR(i2c)) & ICR_SCLE); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | static void i2c_pxa_abort(struct pxa_i2c *i2c) |
| 286 | { |
Dmitry Baryshkov | 387fa6a | 2008-08-18 14:38:48 +0100 | [diff] [blame] | 287 | int i = 250; |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 288 | |
| 289 | if (i2c_pxa_is_slavemode(i2c)) { |
Russell King | 6fd60fa | 2005-09-08 21:04:58 +0100 | [diff] [blame] | 290 | dev_dbg(&i2c->adap.dev, "%s: called in slave mode\n", __func__); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 291 | return; |
| 292 | } |
| 293 | |
Dmitry Baryshkov | 387fa6a | 2008-08-18 14:38:48 +0100 | [diff] [blame] | 294 | while ((i > 0) && (readl(_IBMR(i2c)) & 0x1) == 0) { |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 295 | unsigned long icr = readl(_ICR(i2c)); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 296 | |
| 297 | icr &= ~ICR_START; |
| 298 | icr |= ICR_ACKNAK | ICR_STOP | ICR_TB; |
| 299 | |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 300 | writel(icr, _ICR(i2c)); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 301 | |
| 302 | show_state(i2c); |
| 303 | |
Dmitry Baryshkov | 387fa6a | 2008-08-18 14:38:48 +0100 | [diff] [blame] | 304 | mdelay(1); |
| 305 | i --; |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 306 | } |
| 307 | |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 308 | writel(readl(_ICR(i2c)) & ~(ICR_MA | ICR_START | ICR_STOP), |
| 309 | _ICR(i2c)); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | static int i2c_pxa_wait_bus_not_busy(struct pxa_i2c *i2c) |
| 313 | { |
| 314 | int timeout = DEF_TIMEOUT; |
| 315 | |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 316 | while (timeout-- && readl(_ISR(i2c)) & (ISR_IBB | ISR_UB)) { |
| 317 | if ((readl(_ISR(i2c)) & ISR_SAD) != 0) |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 318 | timeout += 4; |
| 319 | |
| 320 | msleep(2); |
| 321 | show_state(i2c); |
| 322 | } |
| 323 | |
Roel Kluin | d10db3a | 2009-04-23 16:27:39 +0200 | [diff] [blame] | 324 | if (timeout < 0) |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 325 | show_state(i2c); |
| 326 | |
Roel Kluin | d10db3a | 2009-04-23 16:27:39 +0200 | [diff] [blame] | 327 | return timeout < 0 ? I2C_RETRY : 0; |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | static int i2c_pxa_wait_master(struct pxa_i2c *i2c) |
| 331 | { |
| 332 | unsigned long timeout = jiffies + HZ*4; |
| 333 | |
| 334 | while (time_before(jiffies, timeout)) { |
| 335 | if (i2c_debug > 1) |
Russell King | 6fd60fa | 2005-09-08 21:04:58 +0100 | [diff] [blame] | 336 | 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] | 337 | __func__, (long)jiffies, readl(_ISR(i2c)), readl(_ICR(i2c)), readl(_IBMR(i2c))); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 338 | |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 339 | if (readl(_ISR(i2c)) & ISR_SAD) { |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 340 | if (i2c_debug > 0) |
Russell King | 6fd60fa | 2005-09-08 21:04:58 +0100 | [diff] [blame] | 341 | dev_dbg(&i2c->adap.dev, "%s: Slave detected\n", __func__); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 342 | goto out; |
| 343 | } |
| 344 | |
| 345 | /* wait for unit and bus being not busy, and we also do a |
| 346 | * quick check of the i2c lines themselves to ensure they've |
| 347 | * gone high... |
| 348 | */ |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 349 | if ((readl(_ISR(i2c)) & (ISR_UB | ISR_IBB)) == 0 && readl(_IBMR(i2c)) == 3) { |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 350 | if (i2c_debug > 0) |
Russell King | 6fd60fa | 2005-09-08 21:04:58 +0100 | [diff] [blame] | 351 | dev_dbg(&i2c->adap.dev, "%s: done\n", __func__); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 352 | return 1; |
| 353 | } |
| 354 | |
| 355 | msleep(1); |
| 356 | } |
| 357 | |
| 358 | if (i2c_debug > 0) |
Russell King | 6fd60fa | 2005-09-08 21:04:58 +0100 | [diff] [blame] | 359 | dev_dbg(&i2c->adap.dev, "%s: did not free\n", __func__); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 360 | out: |
| 361 | return 0; |
| 362 | } |
| 363 | |
| 364 | static int i2c_pxa_set_master(struct pxa_i2c *i2c) |
| 365 | { |
| 366 | if (i2c_debug) |
Russell King | 6fd60fa | 2005-09-08 21:04:58 +0100 | [diff] [blame] | 367 | dev_dbg(&i2c->adap.dev, "setting to bus master\n"); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 368 | |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 369 | if ((readl(_ISR(i2c)) & (ISR_UB | ISR_IBB)) != 0) { |
Russell King | 6fd60fa | 2005-09-08 21:04:58 +0100 | [diff] [blame] | 370 | dev_dbg(&i2c->adap.dev, "%s: unit is busy\n", __func__); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 371 | if (!i2c_pxa_wait_master(i2c)) { |
Russell King | 6fd60fa | 2005-09-08 21:04:58 +0100 | [diff] [blame] | 372 | dev_dbg(&i2c->adap.dev, "%s: error: unit busy\n", __func__); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 373 | return I2C_RETRY; |
| 374 | } |
| 375 | } |
| 376 | |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 377 | writel(readl(_ICR(i2c)) | ICR_SCLE, _ICR(i2c)); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 378 | return 0; |
| 379 | } |
| 380 | |
| 381 | #ifdef CONFIG_I2C_PXA_SLAVE |
| 382 | static int i2c_pxa_wait_slave(struct pxa_i2c *i2c) |
| 383 | { |
| 384 | unsigned long timeout = jiffies + HZ*1; |
| 385 | |
| 386 | /* wait for stop */ |
| 387 | |
| 388 | show_state(i2c); |
| 389 | |
| 390 | while (time_before(jiffies, timeout)) { |
| 391 | if (i2c_debug > 1) |
Russell King | 6fd60fa | 2005-09-08 21:04:58 +0100 | [diff] [blame] | 392 | 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] | 393 | __func__, (long)jiffies, readl(_ISR(i2c)), readl(_ICR(i2c)), readl(_IBMR(i2c))); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 394 | |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 395 | if ((readl(_ISR(i2c)) & (ISR_UB|ISR_IBB)) == 0 || |
| 396 | (readl(_ISR(i2c)) & ISR_SAD) != 0 || |
| 397 | (readl(_ICR(i2c)) & ICR_SCLE) == 0) { |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 398 | if (i2c_debug > 1) |
Russell King | 6fd60fa | 2005-09-08 21:04:58 +0100 | [diff] [blame] | 399 | dev_dbg(&i2c->adap.dev, "%s: done\n", __func__); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 400 | return 1; |
| 401 | } |
| 402 | |
| 403 | msleep(1); |
| 404 | } |
| 405 | |
| 406 | if (i2c_debug > 0) |
Russell King | 6fd60fa | 2005-09-08 21:04:58 +0100 | [diff] [blame] | 407 | dev_dbg(&i2c->adap.dev, "%s: did not free\n", __func__); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 408 | return 0; |
| 409 | } |
| 410 | |
| 411 | /* |
| 412 | * clear the hold on the bus, and take of anything else |
| 413 | * that has been configured |
| 414 | */ |
| 415 | static void i2c_pxa_set_slave(struct pxa_i2c *i2c, int errcode) |
| 416 | { |
| 417 | show_state(i2c); |
| 418 | |
| 419 | if (errcode < 0) { |
| 420 | udelay(100); /* simple delay */ |
| 421 | } else { |
| 422 | /* we need to wait for the stop condition to end */ |
| 423 | |
| 424 | /* if we where in stop, then clear... */ |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 425 | if (readl(_ICR(i2c)) & ICR_STOP) { |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 426 | udelay(100); |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 427 | writel(readl(_ICR(i2c)) & ~ICR_STOP, _ICR(i2c)); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | if (!i2c_pxa_wait_slave(i2c)) { |
Russell King | 6fd60fa | 2005-09-08 21:04:58 +0100 | [diff] [blame] | 431 | dev_err(&i2c->adap.dev, "%s: wait timedout\n", |
| 432 | __func__); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 433 | return; |
| 434 | } |
| 435 | } |
| 436 | |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 437 | writel(readl(_ICR(i2c)) & ~(ICR_STOP|ICR_ACKNAK|ICR_MA), _ICR(i2c)); |
| 438 | writel(readl(_ICR(i2c)) & ~ICR_SCLE, _ICR(i2c)); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 439 | |
| 440 | if (i2c_debug) { |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 441 | dev_dbg(&i2c->adap.dev, "ICR now %08x, ISR %08x\n", readl(_ICR(i2c)), readl(_ISR(i2c))); |
| 442 | decode_ICR(readl(_ICR(i2c))); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 443 | } |
| 444 | } |
| 445 | #else |
| 446 | #define i2c_pxa_set_slave(i2c, err) do { } while (0) |
| 447 | #endif |
| 448 | |
| 449 | static void i2c_pxa_reset(struct pxa_i2c *i2c) |
| 450 | { |
| 451 | pr_debug("Resetting I2C Controller Unit\n"); |
| 452 | |
| 453 | /* abort any transfer currently under way */ |
| 454 | i2c_pxa_abort(i2c); |
| 455 | |
| 456 | /* reset according to 9.8 */ |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 457 | writel(ICR_UR, _ICR(i2c)); |
| 458 | writel(I2C_ISR_INIT, _ISR(i2c)); |
| 459 | writel(readl(_ICR(i2c)) & ~ICR_UR, _ICR(i2c)); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 460 | |
Sebastian Andrzej Siewior | 7e94dd1 | 2011-03-02 11:26:53 +0100 | [diff] [blame^] | 461 | if (i2c->reg_isar) |
| 462 | writel(i2c->slave_addr, _ISAR(i2c)); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 463 | |
| 464 | /* set control register values */ |
Jonathan Cameron | c46c948 | 2008-10-03 15:07:36 +0100 | [diff] [blame] | 465 | writel(I2C_ICR_INIT | (i2c->fast_mode ? ICR_FM : 0), _ICR(i2c)); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 466 | |
| 467 | #ifdef CONFIG_I2C_PXA_SLAVE |
Russell King | 6fd60fa | 2005-09-08 21:04:58 +0100 | [diff] [blame] | 468 | dev_info(&i2c->adap.dev, "Enabling slave mode\n"); |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 469 | writel(readl(_ICR(i2c)) | ICR_SADIE | ICR_ALDIE | ICR_SSDIE, _ICR(i2c)); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 470 | #endif |
| 471 | |
| 472 | i2c_pxa_set_slave(i2c, 0); |
| 473 | |
| 474 | /* enable unit */ |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 475 | writel(readl(_ICR(i2c)) | ICR_IUE, _ICR(i2c)); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 476 | udelay(100); |
| 477 | } |
| 478 | |
| 479 | |
| 480 | #ifdef CONFIG_I2C_PXA_SLAVE |
| 481 | /* |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 482 | * PXA I2C Slave mode |
| 483 | */ |
| 484 | |
| 485 | static void i2c_pxa_slave_txempty(struct pxa_i2c *i2c, u32 isr) |
| 486 | { |
| 487 | if (isr & ISR_BED) { |
| 488 | /* what should we do here? */ |
| 489 | } else { |
Russell King | 84b5abe | 2006-10-28 22:30:17 +0100 | [diff] [blame] | 490 | int ret = 0; |
| 491 | |
| 492 | if (i2c->slave != NULL) |
| 493 | ret = i2c->slave->read(i2c->slave->data); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 494 | |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 495 | writel(ret, _IDBR(i2c)); |
| 496 | writel(readl(_ICR(i2c)) | ICR_TB, _ICR(i2c)); /* allow next byte */ |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 497 | } |
| 498 | } |
| 499 | |
| 500 | static void i2c_pxa_slave_rxfull(struct pxa_i2c *i2c, u32 isr) |
| 501 | { |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 502 | unsigned int byte = readl(_IDBR(i2c)); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 503 | |
| 504 | if (i2c->slave != NULL) |
| 505 | i2c->slave->write(i2c->slave->data, byte); |
| 506 | |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 507 | writel(readl(_ICR(i2c)) | ICR_TB, _ICR(i2c)); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | static void i2c_pxa_slave_start(struct pxa_i2c *i2c, u32 isr) |
| 511 | { |
| 512 | int timeout; |
| 513 | |
| 514 | if (i2c_debug > 0) |
Russell King | 6fd60fa | 2005-09-08 21:04:58 +0100 | [diff] [blame] | 515 | dev_dbg(&i2c->adap.dev, "SAD, mode is slave-%cx\n", |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 516 | (isr & ISR_RWM) ? 'r' : 't'); |
| 517 | |
| 518 | if (i2c->slave != NULL) |
| 519 | i2c->slave->event(i2c->slave->data, |
| 520 | (isr & ISR_RWM) ? I2C_SLAVE_EVENT_START_READ : I2C_SLAVE_EVENT_START_WRITE); |
| 521 | |
| 522 | /* |
| 523 | * slave could interrupt in the middle of us generating a |
| 524 | * start condition... if this happens, we'd better back off |
| 525 | * and stop holding the poor thing up |
| 526 | */ |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 527 | writel(readl(_ICR(i2c)) & ~(ICR_START|ICR_STOP), _ICR(i2c)); |
| 528 | writel(readl(_ICR(i2c)) | ICR_TB, _ICR(i2c)); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 529 | |
| 530 | timeout = 0x10000; |
| 531 | |
| 532 | while (1) { |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 533 | if ((readl(_IBMR(i2c)) & 2) == 2) |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 534 | break; |
| 535 | |
| 536 | timeout--; |
| 537 | |
| 538 | if (timeout <= 0) { |
Russell King | 6fd60fa | 2005-09-08 21:04:58 +0100 | [diff] [blame] | 539 | dev_err(&i2c->adap.dev, "timeout waiting for SCL high\n"); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 540 | break; |
| 541 | } |
| 542 | } |
| 543 | |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 544 | writel(readl(_ICR(i2c)) & ~ICR_SCLE, _ICR(i2c)); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | static void i2c_pxa_slave_stop(struct pxa_i2c *i2c) |
| 548 | { |
| 549 | if (i2c_debug > 2) |
Russell King | 6fd60fa | 2005-09-08 21:04:58 +0100 | [diff] [blame] | 550 | dev_dbg(&i2c->adap.dev, "ISR: SSD (Slave Stop)\n"); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 551 | |
| 552 | if (i2c->slave != NULL) |
| 553 | i2c->slave->event(i2c->slave->data, I2C_SLAVE_EVENT_STOP); |
| 554 | |
| 555 | if (i2c_debug > 2) |
Russell King | 6fd60fa | 2005-09-08 21:04:58 +0100 | [diff] [blame] | 556 | dev_dbg(&i2c->adap.dev, "ISR: SSD (Slave Stop) acked\n"); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 557 | |
| 558 | /* |
| 559 | * If we have a master-mode message waiting, |
| 560 | * kick it off now that the slave has completed. |
| 561 | */ |
| 562 | if (i2c->msg) |
| 563 | i2c_pxa_master_complete(i2c, I2C_RETRY); |
| 564 | } |
| 565 | #else |
| 566 | static void i2c_pxa_slave_txempty(struct pxa_i2c *i2c, u32 isr) |
| 567 | { |
| 568 | if (isr & ISR_BED) { |
| 569 | /* what should we do here? */ |
| 570 | } else { |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 571 | writel(0, _IDBR(i2c)); |
| 572 | writel(readl(_ICR(i2c)) | ICR_TB, _ICR(i2c)); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 573 | } |
| 574 | } |
| 575 | |
| 576 | static void i2c_pxa_slave_rxfull(struct pxa_i2c *i2c, u32 isr) |
| 577 | { |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 578 | writel(readl(_ICR(i2c)) | ICR_TB | ICR_ACKNAK, _ICR(i2c)); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | static void i2c_pxa_slave_start(struct pxa_i2c *i2c, u32 isr) |
| 582 | { |
| 583 | int timeout; |
| 584 | |
| 585 | /* |
| 586 | * slave could interrupt in the middle of us generating a |
| 587 | * start condition... if this happens, we'd better back off |
| 588 | * and stop holding the poor thing up |
| 589 | */ |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 590 | writel(readl(_ICR(i2c)) & ~(ICR_START|ICR_STOP), _ICR(i2c)); |
| 591 | writel(readl(_ICR(i2c)) | ICR_TB | ICR_ACKNAK, _ICR(i2c)); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 592 | |
| 593 | timeout = 0x10000; |
| 594 | |
| 595 | while (1) { |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 596 | if ((readl(_IBMR(i2c)) & 2) == 2) |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 597 | break; |
| 598 | |
| 599 | timeout--; |
| 600 | |
| 601 | if (timeout <= 0) { |
Russell King | 6fd60fa | 2005-09-08 21:04:58 +0100 | [diff] [blame] | 602 | dev_err(&i2c->adap.dev, "timeout waiting for SCL high\n"); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 603 | break; |
| 604 | } |
| 605 | } |
| 606 | |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 607 | writel(readl(_ICR(i2c)) & ~ICR_SCLE, _ICR(i2c)); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | static void i2c_pxa_slave_stop(struct pxa_i2c *i2c) |
| 611 | { |
| 612 | if (i2c->msg) |
| 613 | i2c_pxa_master_complete(i2c, I2C_RETRY); |
| 614 | } |
| 615 | #endif |
| 616 | |
| 617 | /* |
| 618 | * PXA I2C Master mode |
| 619 | */ |
| 620 | |
| 621 | static inline unsigned int i2c_pxa_addr_byte(struct i2c_msg *msg) |
| 622 | { |
| 623 | unsigned int addr = (msg->addr & 0x7f) << 1; |
| 624 | |
| 625 | if (msg->flags & I2C_M_RD) |
| 626 | addr |= 1; |
| 627 | |
| 628 | return addr; |
| 629 | } |
| 630 | |
| 631 | static inline void i2c_pxa_start_message(struct pxa_i2c *i2c) |
| 632 | { |
| 633 | u32 icr; |
| 634 | |
| 635 | /* |
| 636 | * Step 1: target slave address into IDBR |
| 637 | */ |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 638 | writel(i2c_pxa_addr_byte(i2c->msg), _IDBR(i2c)); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 639 | |
| 640 | /* |
| 641 | * Step 2: initiate the write. |
| 642 | */ |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 643 | icr = readl(_ICR(i2c)) & ~(ICR_STOP | ICR_ALDIE); |
| 644 | writel(icr | ICR_START | ICR_TB, _ICR(i2c)); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 645 | } |
| 646 | |
Jean Delvare | 7d05481 | 2007-05-01 23:26:33 +0200 | [diff] [blame] | 647 | static inline void i2c_pxa_stop_message(struct pxa_i2c *i2c) |
| 648 | { |
| 649 | u32 icr; |
| 650 | |
| 651 | /* |
| 652 | * Clear the STOP and ACK flags |
| 653 | */ |
| 654 | icr = readl(_ICR(i2c)); |
| 655 | icr &= ~(ICR_STOP | ICR_ACKNAK); |
Russell King | 0cfe61e | 2007-05-10 03:15:32 -0700 | [diff] [blame] | 656 | writel(icr, _ICR(i2c)); |
Jean Delvare | 7d05481 | 2007-05-01 23:26:33 +0200 | [diff] [blame] | 657 | } |
| 658 | |
Mike Rapoport | b7a3670 | 2008-01-27 18:14:50 +0100 | [diff] [blame] | 659 | static int i2c_pxa_pio_set_master(struct pxa_i2c *i2c) |
| 660 | { |
| 661 | /* make timeout the same as for interrupt based functions */ |
| 662 | long timeout = 2 * DEF_TIMEOUT; |
| 663 | |
| 664 | /* |
| 665 | * Wait for the bus to become free. |
| 666 | */ |
| 667 | while (timeout-- && readl(_ISR(i2c)) & (ISR_IBB | ISR_UB)) { |
| 668 | udelay(1000); |
| 669 | show_state(i2c); |
| 670 | } |
| 671 | |
Roel Kluin | d10db3a | 2009-04-23 16:27:39 +0200 | [diff] [blame] | 672 | if (timeout < 0) { |
Mike Rapoport | b7a3670 | 2008-01-27 18:14:50 +0100 | [diff] [blame] | 673 | show_state(i2c); |
| 674 | dev_err(&i2c->adap.dev, |
| 675 | "i2c_pxa: timeout waiting for bus free\n"); |
| 676 | return I2C_RETRY; |
| 677 | } |
| 678 | |
| 679 | /* |
| 680 | * Set master mode. |
| 681 | */ |
| 682 | writel(readl(_ICR(i2c)) | ICR_SCLE, _ICR(i2c)); |
| 683 | |
| 684 | return 0; |
| 685 | } |
| 686 | |
| 687 | static int i2c_pxa_do_pio_xfer(struct pxa_i2c *i2c, |
| 688 | struct i2c_msg *msg, int num) |
| 689 | { |
| 690 | unsigned long timeout = 500000; /* 5 seconds */ |
| 691 | int ret = 0; |
| 692 | |
| 693 | ret = i2c_pxa_pio_set_master(i2c); |
| 694 | if (ret) |
| 695 | goto out; |
| 696 | |
| 697 | i2c->msg = msg; |
| 698 | i2c->msg_num = num; |
| 699 | i2c->msg_idx = 0; |
| 700 | i2c->msg_ptr = 0; |
| 701 | i2c->irqlogidx = 0; |
| 702 | |
| 703 | i2c_pxa_start_message(i2c); |
| 704 | |
Roel Kluin | a746b57 | 2009-02-24 19:19:48 +0100 | [diff] [blame] | 705 | while (i2c->msg_num > 0 && --timeout) { |
Mike Rapoport | b7a3670 | 2008-01-27 18:14:50 +0100 | [diff] [blame] | 706 | i2c_pxa_handler(0, i2c); |
| 707 | udelay(10); |
| 708 | } |
| 709 | |
| 710 | i2c_pxa_stop_message(i2c); |
| 711 | |
| 712 | /* |
| 713 | * We place the return code in i2c->msg_idx. |
| 714 | */ |
| 715 | ret = i2c->msg_idx; |
| 716 | |
| 717 | out: |
| 718 | if (timeout == 0) |
| 719 | i2c_pxa_scream_blue_murder(i2c, "timeout"); |
| 720 | |
| 721 | return ret; |
| 722 | } |
| 723 | |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 724 | /* |
Jean Delvare | 3fb9a65 | 2006-01-18 23:17:01 +0100 | [diff] [blame] | 725 | * We are protected by the adapter bus mutex. |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 726 | */ |
| 727 | static int i2c_pxa_do_xfer(struct pxa_i2c *i2c, struct i2c_msg *msg, int num) |
| 728 | { |
| 729 | long timeout; |
| 730 | int ret; |
| 731 | |
| 732 | /* |
| 733 | * Wait for the bus to become free. |
| 734 | */ |
| 735 | ret = i2c_pxa_wait_bus_not_busy(i2c); |
| 736 | if (ret) { |
Russell King | 6fd60fa | 2005-09-08 21:04:58 +0100 | [diff] [blame] | 737 | dev_err(&i2c->adap.dev, "i2c_pxa: timeout waiting for bus free\n"); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 738 | goto out; |
| 739 | } |
| 740 | |
| 741 | /* |
| 742 | * Set master mode. |
| 743 | */ |
| 744 | ret = i2c_pxa_set_master(i2c); |
| 745 | if (ret) { |
Russell King | 6fd60fa | 2005-09-08 21:04:58 +0100 | [diff] [blame] | 746 | dev_err(&i2c->adap.dev, "i2c_pxa_set_master: error %d\n", ret); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 747 | goto out; |
| 748 | } |
| 749 | |
| 750 | spin_lock_irq(&i2c->lock); |
| 751 | |
| 752 | i2c->msg = msg; |
| 753 | i2c->msg_num = num; |
| 754 | i2c->msg_idx = 0; |
| 755 | i2c->msg_ptr = 0; |
| 756 | i2c->irqlogidx = 0; |
| 757 | |
| 758 | i2c_pxa_start_message(i2c); |
| 759 | |
| 760 | spin_unlock_irq(&i2c->lock); |
| 761 | |
| 762 | /* |
| 763 | * The rest of the processing occurs in the interrupt handler. |
| 764 | */ |
| 765 | timeout = wait_event_timeout(i2c->wait, i2c->msg_num == 0, HZ * 5); |
Jean Delvare | 7d05481 | 2007-05-01 23:26:33 +0200 | [diff] [blame] | 766 | i2c_pxa_stop_message(i2c); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 767 | |
| 768 | /* |
| 769 | * We place the return code in i2c->msg_idx. |
| 770 | */ |
| 771 | ret = i2c->msg_idx; |
| 772 | |
| 773 | if (timeout == 0) |
| 774 | i2c_pxa_scream_blue_murder(i2c, "timeout"); |
| 775 | |
| 776 | out: |
| 777 | return ret; |
| 778 | } |
| 779 | |
Mike Rapoport | b7a3670 | 2008-01-27 18:14:50 +0100 | [diff] [blame] | 780 | static int i2c_pxa_pio_xfer(struct i2c_adapter *adap, |
| 781 | struct i2c_msg msgs[], int num) |
| 782 | { |
| 783 | struct pxa_i2c *i2c = adap->algo_data; |
| 784 | int ret, i; |
| 785 | |
| 786 | /* If the I2C controller is disabled we need to reset it |
| 787 | (probably due to a suspend/resume destroying state). We do |
| 788 | this here as we can then avoid worrying about resuming the |
| 789 | controller before its users. */ |
| 790 | if (!(readl(_ICR(i2c)) & ICR_IUE)) |
| 791 | i2c_pxa_reset(i2c); |
| 792 | |
| 793 | for (i = adap->retries; i >= 0; i--) { |
| 794 | ret = i2c_pxa_do_pio_xfer(i2c, msgs, num); |
| 795 | if (ret != I2C_RETRY) |
| 796 | goto out; |
| 797 | |
| 798 | if (i2c_debug) |
| 799 | dev_dbg(&adap->dev, "Retrying transmission\n"); |
| 800 | udelay(100); |
| 801 | } |
| 802 | i2c_pxa_scream_blue_murder(i2c, "exhausted retries"); |
| 803 | ret = -EREMOTEIO; |
| 804 | out: |
| 805 | i2c_pxa_set_slave(i2c, ret); |
| 806 | return ret; |
| 807 | } |
| 808 | |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 809 | /* |
| 810 | * i2c_pxa_master_complete - complete the message and wake up. |
| 811 | */ |
| 812 | static void i2c_pxa_master_complete(struct pxa_i2c *i2c, int ret) |
| 813 | { |
| 814 | i2c->msg_ptr = 0; |
| 815 | i2c->msg = NULL; |
| 816 | i2c->msg_idx ++; |
| 817 | i2c->msg_num = 0; |
| 818 | if (ret) |
| 819 | i2c->msg_idx = ret; |
Mike Rapoport | b7a3670 | 2008-01-27 18:14:50 +0100 | [diff] [blame] | 820 | if (!i2c->use_pio) |
| 821 | wake_up(&i2c->wait); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 822 | } |
| 823 | |
| 824 | static void i2c_pxa_irq_txempty(struct pxa_i2c *i2c, u32 isr) |
| 825 | { |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 826 | 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] | 827 | |
| 828 | again: |
| 829 | /* |
| 830 | * If ISR_ALD is set, we lost arbitration. |
| 831 | */ |
| 832 | if (isr & ISR_ALD) { |
| 833 | /* |
| 834 | * Do we need to do anything here? The PXA docs |
| 835 | * are vague about what happens. |
| 836 | */ |
| 837 | i2c_pxa_scream_blue_murder(i2c, "ALD set"); |
| 838 | |
| 839 | /* |
| 840 | * We ignore this error. We seem to see spurious ALDs |
| 841 | * for seemingly no reason. If we handle them as I think |
| 842 | * they should, we end up causing an I2C error, which |
| 843 | * is painful for some systems. |
| 844 | */ |
| 845 | return; /* ignore */ |
| 846 | } |
| 847 | |
| 848 | if (isr & ISR_BED) { |
| 849 | int ret = BUS_ERROR; |
| 850 | |
| 851 | /* |
| 852 | * I2C bus error - either the device NAK'd us, or |
| 853 | * something more serious happened. If we were NAK'd |
| 854 | * on the initial address phase, we can retry. |
| 855 | */ |
| 856 | if (isr & ISR_ACKNAK) { |
| 857 | if (i2c->msg_ptr == 0 && i2c->msg_idx == 0) |
| 858 | ret = I2C_RETRY; |
| 859 | else |
| 860 | ret = XFER_NAKED; |
| 861 | } |
| 862 | i2c_pxa_master_complete(i2c, ret); |
| 863 | } else if (isr & ISR_RWM) { |
| 864 | /* |
| 865 | * Read mode. We have just sent the address byte, and |
| 866 | * now we must initiate the transfer. |
| 867 | */ |
| 868 | if (i2c->msg_ptr == i2c->msg->len - 1 && |
| 869 | i2c->msg_idx == i2c->msg_num - 1) |
| 870 | icr |= ICR_STOP | ICR_ACKNAK; |
| 871 | |
| 872 | icr |= ICR_ALDIE | ICR_TB; |
| 873 | } else if (i2c->msg_ptr < i2c->msg->len) { |
| 874 | /* |
| 875 | * Write mode. Write the next data byte. |
| 876 | */ |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 877 | writel(i2c->msg->buf[i2c->msg_ptr++], _IDBR(i2c)); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 878 | |
| 879 | icr |= ICR_ALDIE | ICR_TB; |
| 880 | |
| 881 | /* |
| 882 | * If this is the last byte of the last message, send |
| 883 | * a STOP. |
| 884 | */ |
| 885 | if (i2c->msg_ptr == i2c->msg->len && |
| 886 | i2c->msg_idx == i2c->msg_num - 1) |
| 887 | icr |= ICR_STOP; |
| 888 | } else if (i2c->msg_idx < i2c->msg_num - 1) { |
| 889 | /* |
| 890 | * Next segment of the message. |
| 891 | */ |
| 892 | i2c->msg_ptr = 0; |
| 893 | i2c->msg_idx ++; |
| 894 | i2c->msg++; |
| 895 | |
| 896 | /* |
| 897 | * If we aren't doing a repeated start and address, |
| 898 | * go back and try to send the next byte. Note that |
| 899 | * we do not support switching the R/W direction here. |
| 900 | */ |
| 901 | if (i2c->msg->flags & I2C_M_NOSTART) |
| 902 | goto again; |
| 903 | |
| 904 | /* |
| 905 | * Write the next address. |
| 906 | */ |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 907 | writel(i2c_pxa_addr_byte(i2c->msg), _IDBR(i2c)); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 908 | |
| 909 | /* |
| 910 | * And trigger a repeated start, and send the byte. |
| 911 | */ |
| 912 | icr &= ~ICR_ALDIE; |
| 913 | icr |= ICR_START | ICR_TB; |
| 914 | } else { |
| 915 | if (i2c->msg->len == 0) { |
| 916 | /* |
| 917 | * Device probes have a message length of zero |
| 918 | * and need the bus to be reset before it can |
| 919 | * be used again. |
| 920 | */ |
| 921 | i2c_pxa_reset(i2c); |
| 922 | } |
| 923 | i2c_pxa_master_complete(i2c, 0); |
| 924 | } |
| 925 | |
| 926 | i2c->icrlog[i2c->irqlogidx-1] = icr; |
| 927 | |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 928 | writel(icr, _ICR(i2c)); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 929 | show_state(i2c); |
| 930 | } |
| 931 | |
| 932 | static void i2c_pxa_irq_rxfull(struct pxa_i2c *i2c, u32 isr) |
| 933 | { |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 934 | 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] | 935 | |
| 936 | /* |
| 937 | * Read the byte. |
| 938 | */ |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 939 | i2c->msg->buf[i2c->msg_ptr++] = readl(_IDBR(i2c)); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 940 | |
| 941 | if (i2c->msg_ptr < i2c->msg->len) { |
| 942 | /* |
| 943 | * If this is the last byte of the last |
| 944 | * message, send a STOP. |
| 945 | */ |
| 946 | if (i2c->msg_ptr == i2c->msg->len - 1) |
| 947 | icr |= ICR_STOP | ICR_ACKNAK; |
| 948 | |
| 949 | icr |= ICR_ALDIE | ICR_TB; |
| 950 | } else { |
| 951 | i2c_pxa_master_complete(i2c, 0); |
| 952 | } |
| 953 | |
| 954 | i2c->icrlog[i2c->irqlogidx-1] = icr; |
| 955 | |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 956 | writel(icr, _ICR(i2c)); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 957 | } |
| 958 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 959 | static irqreturn_t i2c_pxa_handler(int this_irq, void *dev_id) |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 960 | { |
| 961 | struct pxa_i2c *i2c = dev_id; |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 962 | u32 isr = readl(_ISR(i2c)); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 963 | |
| 964 | if (i2c_debug > 2 && 0) { |
Russell King | 6fd60fa | 2005-09-08 21:04:58 +0100 | [diff] [blame] | 965 | 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] | 966 | __func__, isr, readl(_ICR(i2c)), readl(_IBMR(i2c))); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 967 | decode_ISR(isr); |
| 968 | } |
| 969 | |
Tobias Klauser | 7e3d7db | 2006-01-09 23:19:51 +0100 | [diff] [blame] | 970 | if (i2c->irqlogidx < ARRAY_SIZE(i2c->isrlog)) |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 971 | i2c->isrlog[i2c->irqlogidx++] = isr; |
| 972 | |
| 973 | show_state(i2c); |
| 974 | |
| 975 | /* |
| 976 | * Always clear all pending IRQs. |
| 977 | */ |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 978 | writel(isr & (ISR_SSD|ISR_ALD|ISR_ITE|ISR_IRF|ISR_SAD|ISR_BED), _ISR(i2c)); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 979 | |
| 980 | if (isr & ISR_SAD) |
| 981 | i2c_pxa_slave_start(i2c, isr); |
| 982 | if (isr & ISR_SSD) |
| 983 | i2c_pxa_slave_stop(i2c); |
| 984 | |
| 985 | if (i2c_pxa_is_slavemode(i2c)) { |
| 986 | if (isr & ISR_ITE) |
| 987 | i2c_pxa_slave_txempty(i2c, isr); |
| 988 | if (isr & ISR_IRF) |
| 989 | i2c_pxa_slave_rxfull(i2c, isr); |
| 990 | } else if (i2c->msg) { |
| 991 | if (isr & ISR_ITE) |
| 992 | i2c_pxa_irq_txempty(i2c, isr); |
| 993 | if (isr & ISR_IRF) |
| 994 | i2c_pxa_irq_rxfull(i2c, isr); |
| 995 | } else { |
| 996 | i2c_pxa_scream_blue_murder(i2c, "spurious irq"); |
| 997 | } |
| 998 | |
| 999 | return IRQ_HANDLED; |
| 1000 | } |
| 1001 | |
| 1002 | |
| 1003 | static int i2c_pxa_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num) |
| 1004 | { |
| 1005 | struct pxa_i2c *i2c = adap->algo_data; |
| 1006 | int ret, i; |
| 1007 | |
| 1008 | for (i = adap->retries; i >= 0; i--) { |
| 1009 | ret = i2c_pxa_do_xfer(i2c, msgs, num); |
| 1010 | if (ret != I2C_RETRY) |
| 1011 | goto out; |
| 1012 | |
| 1013 | if (i2c_debug) |
Russell King | 6fd60fa | 2005-09-08 21:04:58 +0100 | [diff] [blame] | 1014 | dev_dbg(&adap->dev, "Retrying transmission\n"); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 1015 | udelay(100); |
| 1016 | } |
| 1017 | i2c_pxa_scream_blue_murder(i2c, "exhausted retries"); |
| 1018 | ret = -EREMOTEIO; |
| 1019 | out: |
| 1020 | i2c_pxa_set_slave(i2c, ret); |
| 1021 | return ret; |
| 1022 | } |
| 1023 | |
Russell King | da16e32 | 2005-09-14 22:54:45 +0100 | [diff] [blame] | 1024 | static u32 i2c_pxa_functionality(struct i2c_adapter *adap) |
| 1025 | { |
| 1026 | return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL; |
| 1027 | } |
| 1028 | |
Jean Delvare | 8f9082c | 2006-09-03 22:39:46 +0200 | [diff] [blame] | 1029 | static const struct i2c_algorithm i2c_pxa_algorithm = { |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 1030 | .master_xfer = i2c_pxa_xfer, |
Russell King | da16e32 | 2005-09-14 22:54:45 +0100 | [diff] [blame] | 1031 | .functionality = i2c_pxa_functionality, |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 1032 | }; |
| 1033 | |
Mike Rapoport | b7a3670 | 2008-01-27 18:14:50 +0100 | [diff] [blame] | 1034 | static const struct i2c_algorithm i2c_pxa_pio_algorithm = { |
| 1035 | .master_xfer = i2c_pxa_pio_xfer, |
| 1036 | .functionality = i2c_pxa_functionality, |
| 1037 | }; |
| 1038 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1039 | static int i2c_pxa_probe(struct platform_device *dev) |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 1040 | { |
Enrico Scholz | 6776f3d | 2007-05-21 12:29:40 +0100 | [diff] [blame] | 1041 | struct pxa_i2c *i2c; |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 1042 | struct resource *res; |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1043 | struct i2c_pxa_platform_data *plat = dev->dev.platform_data; |
Marc Kleine-Budde | 21e2ecf | 2010-06-15 10:56:45 +0200 | [diff] [blame] | 1044 | const struct platform_device_id *id = platform_get_device_id(dev); |
Sebastian Andrzej Siewior | d6668c7 | 2011-02-23 12:38:15 +0100 | [diff] [blame] | 1045 | enum pxa_i2c_types i2c_type = id->driver_data; |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 1046 | int ret; |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 1047 | int irq; |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 1048 | |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 1049 | res = platform_get_resource(dev, IORESOURCE_MEM, 0); |
| 1050 | irq = platform_get_irq(dev, 0); |
| 1051 | if (res == NULL || irq < 0) |
| 1052 | return -ENODEV; |
| 1053 | |
Linus Walleij | c6ffdde | 2009-06-14 00:20:36 +0200 | [diff] [blame] | 1054 | if (!request_mem_region(res->start, resource_size(res), res->name)) |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 1055 | return -ENOMEM; |
| 1056 | |
Enrico Scholz | 6776f3d | 2007-05-21 12:29:40 +0100 | [diff] [blame] | 1057 | i2c = kzalloc(sizeof(struct pxa_i2c), GFP_KERNEL); |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 1058 | if (!i2c) { |
| 1059 | ret = -ENOMEM; |
| 1060 | goto emalloc; |
| 1061 | } |
| 1062 | |
Enrico Scholz | 6776f3d | 2007-05-21 12:29:40 +0100 | [diff] [blame] | 1063 | i2c->adap.owner = THIS_MODULE; |
Enrico Scholz | 6776f3d | 2007-05-21 12:29:40 +0100 | [diff] [blame] | 1064 | i2c->adap.retries = 5; |
| 1065 | |
| 1066 | spin_lock_init(&i2c->lock); |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 1067 | init_waitqueue_head(&i2c->wait); |
Enrico Scholz | 6776f3d | 2007-05-21 12:29:40 +0100 | [diff] [blame] | 1068 | |
Wolfram Sang | a92b36e | 2008-02-24 20:03:42 +0100 | [diff] [blame] | 1069 | /* |
| 1070 | * If "dev->id" is negative we consider it as zero. |
| 1071 | * The reason to do so is to avoid sysfs names that only make |
| 1072 | * sense when there are multiple adapters. |
| 1073 | */ |
| 1074 | i2c->adap.nr = dev->id != -1 ? dev->id : 0; |
| 1075 | snprintf(i2c->adap.name, sizeof(i2c->adap.name), "pxa_i2c-i2c.%u", |
| 1076 | i2c->adap.nr); |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 1077 | |
Russell King | e0d8b13 | 2008-11-11 17:52:32 +0000 | [diff] [blame] | 1078 | i2c->clk = clk_get(&dev->dev, NULL); |
Russell King | c3cef3f | 2007-08-20 10:19:10 +0100 | [diff] [blame] | 1079 | if (IS_ERR(i2c->clk)) { |
| 1080 | ret = PTR_ERR(i2c->clk); |
| 1081 | goto eclk; |
| 1082 | } |
| 1083 | |
Linus Walleij | c6ffdde | 2009-06-14 00:20:36 +0200 | [diff] [blame] | 1084 | i2c->reg_base = ioremap(res->start, resource_size(res)); |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 1085 | if (!i2c->reg_base) { |
| 1086 | ret = -EIO; |
| 1087 | goto eremap; |
| 1088 | } |
Sebastian Andrzej Siewior | d6668c7 | 2011-02-23 12:38:15 +0100 | [diff] [blame] | 1089 | |
| 1090 | i2c->reg_ibmr = i2c->reg_base + pxa_reg_layout[i2c_type].ibmr; |
| 1091 | i2c->reg_idbr = i2c->reg_base + pxa_reg_layout[i2c_type].idbr; |
| 1092 | i2c->reg_icr = i2c->reg_base + pxa_reg_layout[i2c_type].icr; |
| 1093 | i2c->reg_isr = i2c->reg_base + pxa_reg_layout[i2c_type].isr; |
Sebastian Andrzej Siewior | 7e94dd1 | 2011-03-02 11:26:53 +0100 | [diff] [blame^] | 1094 | if (i2c_type != REGS_CE4100) |
| 1095 | i2c->reg_isar = i2c->reg_base + pxa_reg_layout[i2c_type].isar; |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 1096 | |
| 1097 | i2c->iobase = res->start; |
Linus Walleij | c6ffdde | 2009-06-14 00:20:36 +0200 | [diff] [blame] | 1098 | i2c->iosize = resource_size(res); |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 1099 | |
| 1100 | i2c->irq = irq; |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 1101 | |
| 1102 | i2c->slave_addr = I2C_PXA_SLAVE_ADDR; |
| 1103 | |
| 1104 | #ifdef CONFIG_I2C_PXA_SLAVE |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 1105 | if (plat) { |
| 1106 | i2c->slave_addr = plat->slave_addr; |
Russell King | beea494 | 2006-11-07 21:03:20 +0000 | [diff] [blame] | 1107 | i2c->slave = plat->slave; |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 1108 | } |
| 1109 | #endif |
| 1110 | |
Russell King | c3cef3f | 2007-08-20 10:19:10 +0100 | [diff] [blame] | 1111 | clk_enable(i2c->clk); |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 1112 | |
Mike Rapoport | b7a3670 | 2008-01-27 18:14:50 +0100 | [diff] [blame] | 1113 | if (plat) { |
| 1114 | i2c->adap.class = plat->class; |
| 1115 | i2c->use_pio = plat->use_pio; |
Jonathan Cameron | c46c948 | 2008-10-03 15:07:36 +0100 | [diff] [blame] | 1116 | i2c->fast_mode = plat->fast_mode; |
Mike Rapoport | b7a3670 | 2008-01-27 18:14:50 +0100 | [diff] [blame] | 1117 | } |
| 1118 | |
| 1119 | if (i2c->use_pio) { |
| 1120 | i2c->adap.algo = &i2c_pxa_pio_algorithm; |
| 1121 | } else { |
| 1122 | i2c->adap.algo = &i2c_pxa_algorithm; |
| 1123 | ret = request_irq(irq, i2c_pxa_handler, IRQF_DISABLED, |
| 1124 | i2c->adap.name, i2c); |
| 1125 | if (ret) |
| 1126 | goto ereqirq; |
| 1127 | } |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 1128 | |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 1129 | i2c_pxa_reset(i2c); |
| 1130 | |
| 1131 | i2c->adap.algo_data = i2c; |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1132 | i2c->adap.dev.parent = &dev->dev; |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 1133 | |
Sebastian Andrzej Siewior | 7e94dd1 | 2011-03-02 11:26:53 +0100 | [diff] [blame^] | 1134 | if (i2c_type == REGS_CE4100) |
| 1135 | ret = i2c_add_adapter(&i2c->adap); |
| 1136 | else |
| 1137 | ret = i2c_add_numbered_adapter(&i2c->adap); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 1138 | if (ret < 0) { |
| 1139 | printk(KERN_INFO "I2C: Failed to add bus\n"); |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 1140 | goto eadapt; |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 1141 | } |
| 1142 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1143 | platform_set_drvdata(dev, i2c); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 1144 | |
| 1145 | #ifdef CONFIG_I2C_PXA_SLAVE |
| 1146 | printk(KERN_INFO "I2C: %s: PXA I2C adapter, slave address %d\n", |
Jean Delvare | 22e965c | 2009-01-07 14:29:16 +0100 | [diff] [blame] | 1147 | dev_name(&i2c->adap.dev), i2c->slave_addr); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 1148 | #else |
| 1149 | printk(KERN_INFO "I2C: %s: PXA I2C adapter\n", |
Jean Delvare | 22e965c | 2009-01-07 14:29:16 +0100 | [diff] [blame] | 1150 | dev_name(&i2c->adap.dev)); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 1151 | #endif |
| 1152 | return 0; |
| 1153 | |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 1154 | eadapt: |
Mike Rapoport | b7a3670 | 2008-01-27 18:14:50 +0100 | [diff] [blame] | 1155 | if (!i2c->use_pio) |
| 1156 | free_irq(irq, i2c); |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 1157 | ereqirq: |
Russell King | c3cef3f | 2007-08-20 10:19:10 +0100 | [diff] [blame] | 1158 | clk_disable(i2c->clk); |
Wolfram Sang | a92b36e | 2008-02-24 20:03:42 +0100 | [diff] [blame] | 1159 | iounmap(i2c->reg_base); |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 1160 | eremap: |
Russell King | c3cef3f | 2007-08-20 10:19:10 +0100 | [diff] [blame] | 1161 | clk_put(i2c->clk); |
| 1162 | eclk: |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 1163 | kfree(i2c); |
| 1164 | emalloc: |
Linus Walleij | c6ffdde | 2009-06-14 00:20:36 +0200 | [diff] [blame] | 1165 | release_mem_region(res->start, resource_size(res)); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 1166 | return ret; |
| 1167 | } |
| 1168 | |
Wolfram Sang | a92b36e | 2008-02-24 20:03:42 +0100 | [diff] [blame] | 1169 | static int __exit i2c_pxa_remove(struct platform_device *dev) |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 1170 | { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1171 | struct pxa_i2c *i2c = platform_get_drvdata(dev); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 1172 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1173 | platform_set_drvdata(dev, NULL); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 1174 | |
| 1175 | i2c_del_adapter(&i2c->adap); |
Mike Rapoport | b7a3670 | 2008-01-27 18:14:50 +0100 | [diff] [blame] | 1176 | if (!i2c->use_pio) |
| 1177 | free_irq(i2c->irq, i2c); |
Russell King | c3cef3f | 2007-08-20 10:19:10 +0100 | [diff] [blame] | 1178 | |
| 1179 | clk_disable(i2c->clk); |
| 1180 | clk_put(i2c->clk); |
Russell King | c3cef3f | 2007-08-20 10:19:10 +0100 | [diff] [blame] | 1181 | |
Wolfram Sang | a92b36e | 2008-02-24 20:03:42 +0100 | [diff] [blame] | 1182 | iounmap(i2c->reg_base); |
Guennadi Liakhovetski | a7b4e55 | 2007-02-08 09:43:26 +0100 | [diff] [blame] | 1183 | release_mem_region(i2c->iobase, i2c->iosize); |
| 1184 | kfree(i2c); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 1185 | |
| 1186 | return 0; |
| 1187 | } |
| 1188 | |
Russell King | e7d48fa | 2008-08-26 10:40:50 +0100 | [diff] [blame] | 1189 | #ifdef CONFIG_PM |
Magnus Damm | 57f4d4f | 2009-07-08 13:22:39 +0200 | [diff] [blame] | 1190 | static int i2c_pxa_suspend_noirq(struct device *dev) |
Russell King | e7d48fa | 2008-08-26 10:40:50 +0100 | [diff] [blame] | 1191 | { |
Magnus Damm | 57f4d4f | 2009-07-08 13:22:39 +0200 | [diff] [blame] | 1192 | struct platform_device *pdev = to_platform_device(dev); |
| 1193 | struct pxa_i2c *i2c = platform_get_drvdata(pdev); |
| 1194 | |
Russell King | e7d48fa | 2008-08-26 10:40:50 +0100 | [diff] [blame] | 1195 | clk_disable(i2c->clk); |
Magnus Damm | 57f4d4f | 2009-07-08 13:22:39 +0200 | [diff] [blame] | 1196 | |
Russell King | e7d48fa | 2008-08-26 10:40:50 +0100 | [diff] [blame] | 1197 | return 0; |
| 1198 | } |
| 1199 | |
Magnus Damm | 57f4d4f | 2009-07-08 13:22:39 +0200 | [diff] [blame] | 1200 | static int i2c_pxa_resume_noirq(struct device *dev) |
Russell King | e7d48fa | 2008-08-26 10:40:50 +0100 | [diff] [blame] | 1201 | { |
Magnus Damm | 57f4d4f | 2009-07-08 13:22:39 +0200 | [diff] [blame] | 1202 | struct platform_device *pdev = to_platform_device(dev); |
| 1203 | struct pxa_i2c *i2c = platform_get_drvdata(pdev); |
Russell King | e7d48fa | 2008-08-26 10:40:50 +0100 | [diff] [blame] | 1204 | |
| 1205 | clk_enable(i2c->clk); |
| 1206 | i2c_pxa_reset(i2c); |
| 1207 | |
| 1208 | return 0; |
| 1209 | } |
Magnus Damm | 57f4d4f | 2009-07-08 13:22:39 +0200 | [diff] [blame] | 1210 | |
Alexey Dobriyan | 4714521 | 2009-12-14 18:00:08 -0800 | [diff] [blame] | 1211 | static const struct dev_pm_ops i2c_pxa_dev_pm_ops = { |
Magnus Damm | 57f4d4f | 2009-07-08 13:22:39 +0200 | [diff] [blame] | 1212 | .suspend_noirq = i2c_pxa_suspend_noirq, |
| 1213 | .resume_noirq = i2c_pxa_resume_noirq, |
| 1214 | }; |
| 1215 | |
| 1216 | #define I2C_PXA_DEV_PM_OPS (&i2c_pxa_dev_pm_ops) |
Russell King | e7d48fa | 2008-08-26 10:40:50 +0100 | [diff] [blame] | 1217 | #else |
Magnus Damm | 57f4d4f | 2009-07-08 13:22:39 +0200 | [diff] [blame] | 1218 | #define I2C_PXA_DEV_PM_OPS NULL |
Russell King | e7d48fa | 2008-08-26 10:40:50 +0100 | [diff] [blame] | 1219 | #endif |
| 1220 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1221 | static struct platform_driver i2c_pxa_driver = { |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 1222 | .probe = i2c_pxa_probe, |
Wolfram Sang | a92b36e | 2008-02-24 20:03:42 +0100 | [diff] [blame] | 1223 | .remove = __exit_p(i2c_pxa_remove), |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1224 | .driver = { |
| 1225 | .name = "pxa2xx-i2c", |
Wolfram Sang | a92b36e | 2008-02-24 20:03:42 +0100 | [diff] [blame] | 1226 | .owner = THIS_MODULE, |
Magnus Damm | 57f4d4f | 2009-07-08 13:22:39 +0200 | [diff] [blame] | 1227 | .pm = I2C_PXA_DEV_PM_OPS, |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1228 | }, |
Eric Miao | f23d491 | 2009-04-13 14:43:25 +0800 | [diff] [blame] | 1229 | .id_table = i2c_pxa_id_table, |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 1230 | }; |
| 1231 | |
| 1232 | static int __init i2c_adap_pxa_init(void) |
| 1233 | { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1234 | return platform_driver_register(&i2c_pxa_driver); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 1235 | } |
| 1236 | |
Wolfram Sang | a92b36e | 2008-02-24 20:03:42 +0100 | [diff] [blame] | 1237 | static void __exit i2c_adap_pxa_exit(void) |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 1238 | { |
Holger Schurig | d6a7b5f | 2008-02-11 16:51:41 +0100 | [diff] [blame] | 1239 | platform_driver_unregister(&i2c_pxa_driver); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 1240 | } |
| 1241 | |
Richard Purdie | ece5f7b | 2006-01-12 16:30:23 +0000 | [diff] [blame] | 1242 | MODULE_LICENSE("GPL"); |
Kay Sievers | add8eda | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 1243 | MODULE_ALIAS("platform:pxa2xx-i2c"); |
Richard Purdie | ece5f7b | 2006-01-12 16:30:23 +0000 | [diff] [blame] | 1244 | |
Uli Luckas | 47a9b13 | 2008-07-14 22:38:30 +0200 | [diff] [blame] | 1245 | subsys_initcall(i2c_adap_pxa_init); |
Russell King | b652b43 | 2005-06-15 12:38:14 +0100 | [diff] [blame] | 1246 | module_exit(i2c_adap_pxa_exit); |