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