blob: 68fa415d9ad552a93e8cf65164b184bd6a76bae1 [file] [log] [blame]
Vitaly Wool41561f22006-12-10 21:21:29 +01001/*
2 * Provides I2C support for Philips PNX010x/PNX4008 boards.
3 *
4 * Authors: Dennis Kovalev <dkovalev@ru.mvista.com>
5 * Vitaly Wool <vwool@ru.mvista.com>
6 *
7 * 2004-2006 (c) MontaVista Software, Inc. This file is licensed under
8 * the terms of the GNU General Public License version 2. This program
9 * is licensed "as is" without any warranty of any kind, whether express
10 * or implied.
11 */
12
13#include <linux/module.h>
14#include <linux/interrupt.h>
15#include <linux/ioport.h>
16#include <linux/delay.h>
17#include <linux/i2c.h>
18#include <linux/timer.h>
19#include <linux/completion.h>
20#include <linux/platform_device.h>
21#include <linux/i2c-pnx.h>
Kevin Wellsa7d73d82009-11-12 00:25:52 +010022#include <linux/io.h>
Russell King0321cb82009-11-20 11:12:26 +000023#include <linux/err.h>
24#include <linux/clk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Russell King0321cb82009-11-20 11:12:26 +000026
Russell Kinga09e64f2008-08-05 16:14:15 +010027#include <mach/hardware.h>
Kevin Wellsa7d73d82009-11-12 00:25:52 +010028#include <mach/i2c.h>
Vitaly Wool41561f22006-12-10 21:21:29 +010029
30#define I2C_PNX_TIMEOUT 10 /* msec */
31#define I2C_PNX_SPEED_KHZ 100
32#define I2C_PNX_REGION_SIZE 0x100
Vitaly Wool41561f22006-12-10 21:21:29 +010033
34static inline int wait_timeout(long timeout, struct i2c_pnx_algo_data *data)
35{
36 while (timeout > 0 &&
37 (ioread32(I2C_REG_STS(data)) & mstatus_active)) {
38 mdelay(1);
39 timeout--;
40 }
41 return (timeout <= 0);
42}
43
44static inline int wait_reset(long timeout, struct i2c_pnx_algo_data *data)
45{
46 while (timeout > 0 &&
47 (ioread32(I2C_REG_CTL(data)) & mcntrl_reset)) {
48 mdelay(1);
49 timeout--;
50 }
51 return (timeout <= 0);
52}
53
Russell King81d67242009-11-21 12:40:00 +000054static inline void i2c_pnx_arm_timer(struct i2c_pnx_algo_data *alg_data)
Vitaly Wool41561f22006-12-10 21:21:29 +010055{
Russell King81d67242009-11-21 12:40:00 +000056 struct timer_list *timer = &alg_data->mif.timer;
Russell Kingeed18b52009-11-21 12:58:13 +000057 unsigned long expires = msecs_to_jiffies(I2C_PNX_TIMEOUT);
Vitaly Wool41561f22006-12-10 21:21:29 +010058
Kevin Wellsb2f125b2009-11-12 00:28:13 +010059 if (expires <= 1)
60 expires = 2;
61
Vitaly Wool41561f22006-12-10 21:21:29 +010062 del_timer_sync(timer);
63
Russell Kingeed18b52009-11-21 12:58:13 +000064 dev_dbg(&alg_data->adapter.dev, "Timer armed at %lu plus %lu jiffies.\n",
Vitaly Wool41561f22006-12-10 21:21:29 +010065 jiffies, expires);
66
67 timer->expires = jiffies + expires;
Russell King81d67242009-11-21 12:40:00 +000068 timer->data = (unsigned long)&alg_data;
Vitaly Wool41561f22006-12-10 21:21:29 +010069
70 add_timer(timer);
71}
72
73/**
74 * i2c_pnx_start - start a device
75 * @slave_addr: slave address
76 * @adap: pointer to adapter structure
77 *
78 * Generate a START signal in the desired mode.
79 */
Russell King81d67242009-11-21 12:40:00 +000080static int i2c_pnx_start(unsigned char slave_addr,
81 struct i2c_pnx_algo_data *alg_data)
Vitaly Wool41561f22006-12-10 21:21:29 +010082{
Russell King81d67242009-11-21 12:40:00 +000083 dev_dbg(&alg_data->adapter.dev, "%s(): addr 0x%x mode %d\n", __func__,
Vitaly Wool41561f22006-12-10 21:21:29 +010084 slave_addr, alg_data->mif.mode);
85
86 /* Check for 7 bit slave addresses only */
87 if (slave_addr & ~0x7f) {
Russell King4be53db2009-11-21 12:46:31 +000088 dev_err(&alg_data->adapter.dev,
89 "%s: Invalid slave address %x. Only 7-bit addresses are supported\n",
90 alg_data->adapter.name, slave_addr);
Vitaly Wool41561f22006-12-10 21:21:29 +010091 return -EINVAL;
92 }
93
94 /* First, make sure bus is idle */
95 if (wait_timeout(I2C_PNX_TIMEOUT, alg_data)) {
96 /* Somebody else is monopolizing the bus */
Russell King4be53db2009-11-21 12:46:31 +000097 dev_err(&alg_data->adapter.dev,
98 "%s: Bus busy. Slave addr = %02x, cntrl = %x, stat = %x\n",
99 alg_data->adapter.name, slave_addr,
100 ioread32(I2C_REG_CTL(alg_data)),
101 ioread32(I2C_REG_STS(alg_data)));
Vitaly Wool41561f22006-12-10 21:21:29 +0100102 return -EBUSY;
103 } else if (ioread32(I2C_REG_STS(alg_data)) & mstatus_afi) {
104 /* Sorry, we lost the bus */
Russell King4be53db2009-11-21 12:46:31 +0000105 dev_err(&alg_data->adapter.dev,
106 "%s: Arbitration failure. Slave addr = %02x\n",
107 alg_data->adapter.name, slave_addr);
Vitaly Wool41561f22006-12-10 21:21:29 +0100108 return -EIO;
109 }
110
111 /*
112 * OK, I2C is enabled and we have the bus.
113 * Clear the current TDI and AFI status flags.
114 */
115 iowrite32(ioread32(I2C_REG_STS(alg_data)) | mstatus_tdi | mstatus_afi,
116 I2C_REG_STS(alg_data));
117
Russell King81d67242009-11-21 12:40:00 +0000118 dev_dbg(&alg_data->adapter.dev, "%s(): sending %#x\n", __func__,
Vitaly Wool41561f22006-12-10 21:21:29 +0100119 (slave_addr << 1) | start_bit | alg_data->mif.mode);
120
121 /* Write the slave address, START bit and R/W bit */
122 iowrite32((slave_addr << 1) | start_bit | alg_data->mif.mode,
123 I2C_REG_TX(alg_data));
124
Russell King81d67242009-11-21 12:40:00 +0000125 dev_dbg(&alg_data->adapter.dev, "%s(): exit\n", __func__);
Vitaly Wool41561f22006-12-10 21:21:29 +0100126
127 return 0;
128}
129
130/**
131 * i2c_pnx_stop - stop a device
132 * @adap: pointer to I2C adapter structure
133 *
134 * Generate a STOP signal to terminate the master transaction.
135 */
Russell King81d67242009-11-21 12:40:00 +0000136static void i2c_pnx_stop(struct i2c_pnx_algo_data *alg_data)
Vitaly Wool41561f22006-12-10 21:21:29 +0100137{
Vitaly Wool41561f22006-12-10 21:21:29 +0100138 /* Only 1 msec max timeout due to interrupt context */
139 long timeout = 1000;
140
Russell King81d67242009-11-21 12:40:00 +0000141 dev_dbg(&alg_data->adapter.dev, "%s(): entering: stat = %04x.\n",
Harvey Harrison08882d22008-04-22 22:16:47 +0200142 __func__, ioread32(I2C_REG_STS(alg_data)));
Vitaly Wool41561f22006-12-10 21:21:29 +0100143
144 /* Write a STOP bit to TX FIFO */
145 iowrite32(0xff | stop_bit, I2C_REG_TX(alg_data));
146
147 /* Wait until the STOP is seen. */
148 while (timeout > 0 &&
149 (ioread32(I2C_REG_STS(alg_data)) & mstatus_active)) {
150 /* may be called from interrupt context */
151 udelay(1);
152 timeout--;
153 }
154
Russell King81d67242009-11-21 12:40:00 +0000155 dev_dbg(&alg_data->adapter.dev, "%s(): exiting: stat = %04x.\n",
Harvey Harrison08882d22008-04-22 22:16:47 +0200156 __func__, ioread32(I2C_REG_STS(alg_data)));
Vitaly Wool41561f22006-12-10 21:21:29 +0100157}
158
159/**
160 * i2c_pnx_master_xmit - transmit data to slave
161 * @adap: pointer to I2C adapter structure
162 *
163 * Sends one byte of data to the slave
164 */
Russell King81d67242009-11-21 12:40:00 +0000165static int i2c_pnx_master_xmit(struct i2c_pnx_algo_data *alg_data)
Vitaly Wool41561f22006-12-10 21:21:29 +0100166{
Vitaly Wool41561f22006-12-10 21:21:29 +0100167 u32 val;
168
Russell King81d67242009-11-21 12:40:00 +0000169 dev_dbg(&alg_data->adapter.dev, "%s(): entering: stat = %04x.\n",
Harvey Harrison08882d22008-04-22 22:16:47 +0200170 __func__, ioread32(I2C_REG_STS(alg_data)));
Vitaly Wool41561f22006-12-10 21:21:29 +0100171
172 if (alg_data->mif.len > 0) {
173 /* We still have something to talk about... */
174 val = *alg_data->mif.buf++;
175
Vitaly Wool41561f22006-12-10 21:21:29 +0100176 alg_data->mif.len--;
177 iowrite32(val, I2C_REG_TX(alg_data));
178
Russell King4be53db2009-11-21 12:46:31 +0000179 dev_dbg(&alg_data->adapter.dev, "%s(): xmit %#x [%d]\n",
180 __func__, val, alg_data->mif.len + 1);
Vitaly Wool41561f22006-12-10 21:21:29 +0100181
182 if (alg_data->mif.len == 0) {
183 if (alg_data->last) {
184 /* Wait until the STOP is seen. */
185 if (wait_timeout(I2C_PNX_TIMEOUT, alg_data))
Russell King4be53db2009-11-21 12:46:31 +0000186 dev_err(&alg_data->adapter.dev,
187 "The bus is still active after timeout\n");
Vitaly Wool41561f22006-12-10 21:21:29 +0100188 }
189 /* Disable master interrupts */
190 iowrite32(ioread32(I2C_REG_CTL(alg_data)) &
191 ~(mcntrl_afie | mcntrl_naie | mcntrl_drmie),
192 I2C_REG_CTL(alg_data));
193
194 del_timer_sync(&alg_data->mif.timer);
195
Russell King4be53db2009-11-21 12:46:31 +0000196 dev_dbg(&alg_data->adapter.dev,
197 "%s(): Waking up xfer routine.\n",
Harvey Harrison08882d22008-04-22 22:16:47 +0200198 __func__);
Vitaly Wool41561f22006-12-10 21:21:29 +0100199
200 complete(&alg_data->mif.complete);
201 }
202 } else if (alg_data->mif.len == 0) {
203 /* zero-sized transfer */
Russell King81d67242009-11-21 12:40:00 +0000204 i2c_pnx_stop(alg_data);
Vitaly Wool41561f22006-12-10 21:21:29 +0100205
206 /* Disable master interrupts. */
207 iowrite32(ioread32(I2C_REG_CTL(alg_data)) &
208 ~(mcntrl_afie | mcntrl_naie | mcntrl_drmie),
209 I2C_REG_CTL(alg_data));
210
211 /* Stop timer. */
212 del_timer_sync(&alg_data->mif.timer);
Russell King4be53db2009-11-21 12:46:31 +0000213 dev_dbg(&alg_data->adapter.dev,
214 "%s(): Waking up xfer routine after zero-xfer.\n",
215 __func__);
Vitaly Wool41561f22006-12-10 21:21:29 +0100216
217 complete(&alg_data->mif.complete);
218 }
219
Russell King81d67242009-11-21 12:40:00 +0000220 dev_dbg(&alg_data->adapter.dev, "%s(): exiting: stat = %04x.\n",
Harvey Harrison08882d22008-04-22 22:16:47 +0200221 __func__, ioread32(I2C_REG_STS(alg_data)));
Vitaly Wool41561f22006-12-10 21:21:29 +0100222
223 return 0;
224}
225
226/**
227 * i2c_pnx_master_rcv - receive data from slave
228 * @adap: pointer to I2C adapter structure
229 *
230 * Reads one byte data from the slave
231 */
Russell King81d67242009-11-21 12:40:00 +0000232static int i2c_pnx_master_rcv(struct i2c_pnx_algo_data *alg_data)
Vitaly Wool41561f22006-12-10 21:21:29 +0100233{
Vitaly Wool41561f22006-12-10 21:21:29 +0100234 unsigned int val = 0;
235 u32 ctl = 0;
236
Russell King81d67242009-11-21 12:40:00 +0000237 dev_dbg(&alg_data->adapter.dev, "%s(): entering: stat = %04x.\n",
Harvey Harrison08882d22008-04-22 22:16:47 +0200238 __func__, ioread32(I2C_REG_STS(alg_data)));
Vitaly Wool41561f22006-12-10 21:21:29 +0100239
240 /* Check, whether there is already data,
241 * or we didn't 'ask' for it yet.
242 */
243 if (ioread32(I2C_REG_STS(alg_data)) & mstatus_rfe) {
Russell King4be53db2009-11-21 12:46:31 +0000244 dev_dbg(&alg_data->adapter.dev,
245 "%s(): Write dummy data to fill Rx-fifo...\n",
246 __func__);
Vitaly Wool41561f22006-12-10 21:21:29 +0100247
248 if (alg_data->mif.len == 1) {
Vitaly Wool41561f22006-12-10 21:21:29 +0100249 /*
250 * Enable interrupt RFDAIE (data in Rx fifo),
251 * and disable DRMIE (need data for Tx)
252 */
253 ctl = ioread32(I2C_REG_CTL(alg_data));
254 ctl |= mcntrl_rffie | mcntrl_daie;
255 ctl &= ~mcntrl_drmie;
256 iowrite32(ctl, I2C_REG_CTL(alg_data));
257 }
258
259 /*
260 * Now we'll 'ask' for data:
261 * For each byte we want to receive, we must
262 * write a (dummy) byte to the Tx-FIFO.
263 */
264 iowrite32(val, I2C_REG_TX(alg_data));
265
266 return 0;
267 }
268
269 /* Handle data. */
270 if (alg_data->mif.len > 0) {
271 val = ioread32(I2C_REG_RX(alg_data));
272 *alg_data->mif.buf++ = (u8) (val & 0xff);
Russell King4be53db2009-11-21 12:46:31 +0000273 dev_dbg(&alg_data->adapter.dev, "%s(): rcv 0x%x [%d]\n",
274 __func__, val, alg_data->mif.len);
Vitaly Wool41561f22006-12-10 21:21:29 +0100275
276 alg_data->mif.len--;
277 if (alg_data->mif.len == 0) {
278 if (alg_data->last)
279 /* Wait until the STOP is seen. */
280 if (wait_timeout(I2C_PNX_TIMEOUT, alg_data))
Russell King4be53db2009-11-21 12:46:31 +0000281 dev_err(&alg_data->adapter.dev,
282 "The bus is still active after timeout\n");
Vitaly Wool41561f22006-12-10 21:21:29 +0100283
284 /* Disable master interrupts */
285 ctl = ioread32(I2C_REG_CTL(alg_data));
286 ctl &= ~(mcntrl_afie | mcntrl_naie | mcntrl_rffie |
287 mcntrl_drmie | mcntrl_daie);
288 iowrite32(ctl, I2C_REG_CTL(alg_data));
289
290 /* Kill timer. */
291 del_timer_sync(&alg_data->mif.timer);
292 complete(&alg_data->mif.complete);
293 }
294 }
295
Russell King81d67242009-11-21 12:40:00 +0000296 dev_dbg(&alg_data->adapter.dev, "%s(): exiting: stat = %04x.\n",
Harvey Harrison08882d22008-04-22 22:16:47 +0200297 __func__, ioread32(I2C_REG_STS(alg_data)));
Vitaly Wool41561f22006-12-10 21:21:29 +0100298
299 return 0;
300}
301
Vitaly Wool6c566fb2007-01-04 13:07:03 +0100302static irqreturn_t i2c_pnx_interrupt(int irq, void *dev_id)
Vitaly Wool41561f22006-12-10 21:21:29 +0100303{
Russell King81d67242009-11-21 12:40:00 +0000304 struct i2c_pnx_algo_data *alg_data = dev_id;
Vitaly Wool41561f22006-12-10 21:21:29 +0100305 u32 stat, ctl;
Vitaly Wool41561f22006-12-10 21:21:29 +0100306
Russell King4be53db2009-11-21 12:46:31 +0000307 dev_dbg(&alg_data->adapter.dev,
308 "%s(): mstat = %x mctrl = %x, mode = %d\n",
Harvey Harrison08882d22008-04-22 22:16:47 +0200309 __func__,
Vitaly Wool41561f22006-12-10 21:21:29 +0100310 ioread32(I2C_REG_STS(alg_data)),
311 ioread32(I2C_REG_CTL(alg_data)),
312 alg_data->mif.mode);
313 stat = ioread32(I2C_REG_STS(alg_data));
314
315 /* let's see what kind of event this is */
316 if (stat & mstatus_afi) {
317 /* We lost arbitration in the midst of a transfer */
318 alg_data->mif.ret = -EIO;
319
320 /* Disable master interrupts. */
321 ctl = ioread32(I2C_REG_CTL(alg_data));
322 ctl &= ~(mcntrl_afie | mcntrl_naie | mcntrl_rffie |
323 mcntrl_drmie);
324 iowrite32(ctl, I2C_REG_CTL(alg_data));
325
326 /* Stop timer, to prevent timeout. */
327 del_timer_sync(&alg_data->mif.timer);
328 complete(&alg_data->mif.complete);
329 } else if (stat & mstatus_nai) {
330 /* Slave did not acknowledge, generate a STOP */
Russell King4be53db2009-11-21 12:46:31 +0000331 dev_dbg(&alg_data->adapter.dev,
332 "%s(): Slave did not acknowledge, generating a STOP.\n",
Harvey Harrison08882d22008-04-22 22:16:47 +0200333 __func__);
Russell King81d67242009-11-21 12:40:00 +0000334 i2c_pnx_stop(alg_data);
Vitaly Wool41561f22006-12-10 21:21:29 +0100335
336 /* Disable master interrupts. */
337 ctl = ioread32(I2C_REG_CTL(alg_data));
338 ctl &= ~(mcntrl_afie | mcntrl_naie | mcntrl_rffie |
339 mcntrl_drmie);
340 iowrite32(ctl, I2C_REG_CTL(alg_data));
341
342 /* Our return value. */
343 alg_data->mif.ret = -EIO;
344
345 /* Stop timer, to prevent timeout. */
346 del_timer_sync(&alg_data->mif.timer);
347 complete(&alg_data->mif.complete);
348 } else {
349 /*
350 * Two options:
351 * - Master Tx needs data.
352 * - There is data in the Rx-fifo
353 * The latter is only the case if we have requested for data,
354 * via a dummy write. (See 'i2c_pnx_master_rcv'.)
355 * We therefore check, as a sanity check, whether that interrupt
356 * has been enabled.
357 */
358 if ((stat & mstatus_drmi) || !(stat & mstatus_rfe)) {
359 if (alg_data->mif.mode == I2C_SMBUS_WRITE) {
Russell King81d67242009-11-21 12:40:00 +0000360 i2c_pnx_master_xmit(alg_data);
Vitaly Wool41561f22006-12-10 21:21:29 +0100361 } else if (alg_data->mif.mode == I2C_SMBUS_READ) {
Russell King81d67242009-11-21 12:40:00 +0000362 i2c_pnx_master_rcv(alg_data);
Vitaly Wool41561f22006-12-10 21:21:29 +0100363 }
364 }
365 }
366
367 /* Clear TDI and AFI bits */
368 stat = ioread32(I2C_REG_STS(alg_data));
369 iowrite32(stat | mstatus_tdi | mstatus_afi, I2C_REG_STS(alg_data));
370
Russell King4be53db2009-11-21 12:46:31 +0000371 dev_dbg(&alg_data->adapter.dev,
372 "%s(): exiting, stat = %x ctrl = %x.\n",
Harvey Harrison08882d22008-04-22 22:16:47 +0200373 __func__, ioread32(I2C_REG_STS(alg_data)),
Vitaly Wool41561f22006-12-10 21:21:29 +0100374 ioread32(I2C_REG_CTL(alg_data)));
375
376 return IRQ_HANDLED;
377}
378
379static void i2c_pnx_timeout(unsigned long data)
380{
Russell King81d67242009-11-21 12:40:00 +0000381 struct i2c_pnx_algo_data *alg_data = (struct i2c_pnx_algo_data *)data;
Vitaly Wool41561f22006-12-10 21:21:29 +0100382 u32 ctl;
383
Russell King4be53db2009-11-21 12:46:31 +0000384 dev_err(&alg_data->adapter.dev,
385 "Master timed out. stat = %04x, cntrl = %04x. Resetting master...\n",
386 ioread32(I2C_REG_STS(alg_data)),
387 ioread32(I2C_REG_CTL(alg_data)));
Vitaly Wool41561f22006-12-10 21:21:29 +0100388
389 /* Reset master and disable interrupts */
390 ctl = ioread32(I2C_REG_CTL(alg_data));
391 ctl &= ~(mcntrl_afie | mcntrl_naie | mcntrl_rffie | mcntrl_drmie);
392 iowrite32(ctl, I2C_REG_CTL(alg_data));
393
394 ctl |= mcntrl_reset;
395 iowrite32(ctl, I2C_REG_CTL(alg_data));
396 wait_reset(I2C_PNX_TIMEOUT, alg_data);
397 alg_data->mif.ret = -EIO;
398 complete(&alg_data->mif.complete);
399}
400
Russell King81d67242009-11-21 12:40:00 +0000401static inline void bus_reset_if_active(struct i2c_pnx_algo_data *alg_data)
Vitaly Wool41561f22006-12-10 21:21:29 +0100402{
Vitaly Wool41561f22006-12-10 21:21:29 +0100403 u32 stat;
404
405 if ((stat = ioread32(I2C_REG_STS(alg_data))) & mstatus_active) {
Russell King81d67242009-11-21 12:40:00 +0000406 dev_err(&alg_data->adapter.dev,
Vitaly Wool41561f22006-12-10 21:21:29 +0100407 "%s: Bus is still active after xfer. Reset it...\n",
Russell King4be53db2009-11-21 12:46:31 +0000408 alg_data->adapter.name);
Vitaly Wool41561f22006-12-10 21:21:29 +0100409 iowrite32(ioread32(I2C_REG_CTL(alg_data)) | mcntrl_reset,
410 I2C_REG_CTL(alg_data));
411 wait_reset(I2C_PNX_TIMEOUT, alg_data);
412 } else if (!(stat & mstatus_rfe) || !(stat & mstatus_tfe)) {
413 /* If there is data in the fifo's after transfer,
414 * flush fifo's by reset.
415 */
416 iowrite32(ioread32(I2C_REG_CTL(alg_data)) | mcntrl_reset,
417 I2C_REG_CTL(alg_data));
418 wait_reset(I2C_PNX_TIMEOUT, alg_data);
419 } else if (stat & mstatus_nai) {
420 iowrite32(ioread32(I2C_REG_CTL(alg_data)) | mcntrl_reset,
421 I2C_REG_CTL(alg_data));
422 wait_reset(I2C_PNX_TIMEOUT, alg_data);
423 }
424}
425
426/**
427 * i2c_pnx_xfer - generic transfer entry point
428 * @adap: pointer to I2C adapter structure
429 * @msgs: array of messages
430 * @num: number of messages
431 *
432 * Initiates the transfer
433 */
434static int
435i2c_pnx_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
436{
437 struct i2c_msg *pmsg;
438 int rc = 0, completed = 0, i;
439 struct i2c_pnx_algo_data *alg_data = adap->algo_data;
440 u32 stat = ioread32(I2C_REG_STS(alg_data));
441
Russell King4be53db2009-11-21 12:46:31 +0000442 dev_dbg(&alg_data->adapter.dev,
443 "%s(): entering: %d messages, stat = %04x.\n",
Harvey Harrison08882d22008-04-22 22:16:47 +0200444 __func__, num, ioread32(I2C_REG_STS(alg_data)));
Vitaly Wool41561f22006-12-10 21:21:29 +0100445
Russell King81d67242009-11-21 12:40:00 +0000446 bus_reset_if_active(alg_data);
Vitaly Wool41561f22006-12-10 21:21:29 +0100447
448 /* Process transactions in a loop. */
449 for (i = 0; rc >= 0 && i < num; i++) {
450 u8 addr;
451
452 pmsg = &msgs[i];
453 addr = pmsg->addr;
454
455 if (pmsg->flags & I2C_M_TEN) {
Russell King81d67242009-11-21 12:40:00 +0000456 dev_err(&alg_data->adapter.dev,
Vitaly Wool41561f22006-12-10 21:21:29 +0100457 "%s: 10 bits addr not supported!\n",
Russell King81d67242009-11-21 12:40:00 +0000458 alg_data->adapter.name);
Vitaly Wool41561f22006-12-10 21:21:29 +0100459 rc = -EINVAL;
460 break;
461 }
462
463 alg_data->mif.buf = pmsg->buf;
464 alg_data->mif.len = pmsg->len;
465 alg_data->mif.mode = (pmsg->flags & I2C_M_RD) ?
466 I2C_SMBUS_READ : I2C_SMBUS_WRITE;
467 alg_data->mif.ret = 0;
468 alg_data->last = (i == num - 1);
469
Russell King4be53db2009-11-21 12:46:31 +0000470 dev_dbg(&alg_data->adapter.dev, "%s(): mode %d, %d bytes\n",
471 __func__, alg_data->mif.mode, alg_data->mif.len);
Vitaly Wool41561f22006-12-10 21:21:29 +0100472
Russell King81d67242009-11-21 12:40:00 +0000473 i2c_pnx_arm_timer(alg_data);
Vitaly Wool41561f22006-12-10 21:21:29 +0100474
475 /* initialize the completion var */
476 init_completion(&alg_data->mif.complete);
477
478 /* Enable master interrupt */
479 iowrite32(ioread32(I2C_REG_CTL(alg_data)) | mcntrl_afie |
480 mcntrl_naie | mcntrl_drmie,
481 I2C_REG_CTL(alg_data));
482
483 /* Put start-code and slave-address on the bus. */
Russell King81d67242009-11-21 12:40:00 +0000484 rc = i2c_pnx_start(addr, alg_data);
Vitaly Wool41561f22006-12-10 21:21:29 +0100485 if (rc < 0)
486 break;
487
488 /* Wait for completion */
489 wait_for_completion(&alg_data->mif.complete);
490
491 if (!(rc = alg_data->mif.ret))
492 completed++;
Russell King4be53db2009-11-21 12:46:31 +0000493 dev_dbg(&alg_data->adapter.dev,
494 "%s(): Complete, return code = %d.\n",
Harvey Harrison08882d22008-04-22 22:16:47 +0200495 __func__, rc);
Vitaly Wool41561f22006-12-10 21:21:29 +0100496
497 /* Clear TDI and AFI bits in case they are set. */
498 if ((stat = ioread32(I2C_REG_STS(alg_data))) & mstatus_tdi) {
Russell King81d67242009-11-21 12:40:00 +0000499 dev_dbg(&alg_data->adapter.dev,
Vitaly Wool41561f22006-12-10 21:21:29 +0100500 "%s: TDI still set... clearing now.\n",
Russell King81d67242009-11-21 12:40:00 +0000501 alg_data->adapter.name);
Vitaly Wool41561f22006-12-10 21:21:29 +0100502 iowrite32(stat, I2C_REG_STS(alg_data));
503 }
504 if ((stat = ioread32(I2C_REG_STS(alg_data))) & mstatus_afi) {
Russell King81d67242009-11-21 12:40:00 +0000505 dev_dbg(&alg_data->adapter.dev,
Vitaly Wool41561f22006-12-10 21:21:29 +0100506 "%s: AFI still set... clearing now.\n",
Russell King81d67242009-11-21 12:40:00 +0000507 alg_data->adapter.name);
Vitaly Wool41561f22006-12-10 21:21:29 +0100508 iowrite32(stat, I2C_REG_STS(alg_data));
509 }
510 }
511
Russell King81d67242009-11-21 12:40:00 +0000512 bus_reset_if_active(alg_data);
Vitaly Wool41561f22006-12-10 21:21:29 +0100513
514 /* Cleanup to be sure... */
515 alg_data->mif.buf = NULL;
516 alg_data->mif.len = 0;
517
Russell King81d67242009-11-21 12:40:00 +0000518 dev_dbg(&alg_data->adapter.dev, "%s(): exiting, stat = %x\n",
Harvey Harrison08882d22008-04-22 22:16:47 +0200519 __func__, ioread32(I2C_REG_STS(alg_data)));
Vitaly Wool41561f22006-12-10 21:21:29 +0100520
521 if (completed != num)
522 return ((rc < 0) ? rc : -EREMOTEIO);
523
524 return num;
525}
526
527static u32 i2c_pnx_func(struct i2c_adapter *adapter)
528{
529 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
530}
531
532static struct i2c_algorithm pnx_algorithm = {
533 .master_xfer = i2c_pnx_xfer,
534 .functionality = i2c_pnx_func,
535};
536
Russell Kinga0dcf192009-11-20 10:50:34 +0000537#ifdef CONFIG_PM
Vitaly Wool41561f22006-12-10 21:21:29 +0100538static int i2c_pnx_controller_suspend(struct platform_device *pdev,
539 pm_message_t state)
540{
Russell King9d7f7362009-11-21 12:25:27 +0000541 struct i2c_pnx_algo_data *alg_data = platform_get_drvdata(pdev);
Russell King0321cb82009-11-20 11:12:26 +0000542
Russell Kingebdbbf22009-11-20 11:44:46 +0000543 /* FIXME: shouldn't this be clk_disable? */
544 clk_enable(alg_data->clk);
Russell King0321cb82009-11-20 11:12:26 +0000545
546 return 0;
Vitaly Wool41561f22006-12-10 21:21:29 +0100547}
548
549static int i2c_pnx_controller_resume(struct platform_device *pdev)
550{
Russell King9d7f7362009-11-21 12:25:27 +0000551 struct i2c_pnx_algo_data *alg_data = platform_get_drvdata(pdev);
Russell King0321cb82009-11-20 11:12:26 +0000552
Russell Kingebdbbf22009-11-20 11:44:46 +0000553 return clk_enable(alg_data->clk);
Vitaly Wool41561f22006-12-10 21:21:29 +0100554}
Russell Kinga0dcf192009-11-20 10:50:34 +0000555#else
556#define i2c_pnx_controller_suspend NULL
557#define i2c_pnx_controller_resume NULL
558#endif
Vitaly Wool41561f22006-12-10 21:21:29 +0100559
560static int __devinit i2c_pnx_probe(struct platform_device *pdev)
561{
562 unsigned long tmp;
563 int ret = 0;
564 struct i2c_pnx_algo_data *alg_data;
Russell King6fff3da2009-11-20 12:46:07 +0000565 unsigned long freq;
Vitaly Wool41561f22006-12-10 21:21:29 +0100566 struct i2c_pnx_data *i2c_pnx = pdev->dev.platform_data;
567
Russell King9d7f7362009-11-21 12:25:27 +0000568 if (!i2c_pnx || !i2c_pnx->name) {
Vitaly Wool41561f22006-12-10 21:21:29 +0100569 dev_err(&pdev->dev, "%s: no platform data supplied\n",
Harvey Harrison08882d22008-04-22 22:16:47 +0200570 __func__);
Vitaly Wool41561f22006-12-10 21:21:29 +0100571 ret = -EINVAL;
572 goto out;
573 }
574
Russell King44c5d732009-11-21 12:10:54 +0000575 alg_data = kzalloc(sizeof(*alg_data), GFP_KERNEL);
576 if (!alg_data) {
577 ret = -ENOMEM;
578 goto err_kzalloc;
579 }
580
Russell King9d7f7362009-11-21 12:25:27 +0000581 platform_set_drvdata(pdev, alg_data);
Vitaly Wool41561f22006-12-10 21:21:29 +0100582
Russell King9d7f7362009-11-21 12:25:27 +0000583 strlcpy(alg_data->adapter.name, i2c_pnx->name,
584 sizeof(alg_data->adapter.name));
585 alg_data->adapter.dev.parent = &pdev->dev;
586 alg_data->adapter.algo = &pnx_algorithm;
587 alg_data->adapter.algo_data = alg_data;
588 alg_data->adapter.nr = pdev->id;
589 alg_data->i2c_pnx = i2c_pnx;
Russell King0321cb82009-11-20 11:12:26 +0000590
591 alg_data->clk = clk_get(&pdev->dev, NULL);
592 if (IS_ERR(alg_data->clk)) {
593 ret = PTR_ERR(alg_data->clk);
594 goto out_drvdata;
595 }
596
Vitaly Wool41561f22006-12-10 21:21:29 +0100597 init_timer(&alg_data->mif.timer);
598 alg_data->mif.timer.function = i2c_pnx_timeout;
Russell King81d67242009-11-21 12:40:00 +0000599 alg_data->mif.timer.data = (unsigned long)alg_data;
Vitaly Wool41561f22006-12-10 21:21:29 +0100600
601 /* Register I/O resource */
Russell King44c5d732009-11-21 12:10:54 +0000602 if (!request_mem_region(i2c_pnx->base, I2C_PNX_REGION_SIZE,
Julia Lawall449d2c72009-09-18 22:45:51 +0200603 pdev->name)) {
Vitaly Wool41561f22006-12-10 21:21:29 +0100604 dev_err(&pdev->dev,
605 "I/O region 0x%08x for I2C already in use.\n",
Russell King44c5d732009-11-21 12:10:54 +0000606 i2c_pnx->base);
Vitaly Wool41561f22006-12-10 21:21:29 +0100607 ret = -ENODEV;
Russell King0321cb82009-11-20 11:12:26 +0000608 goto out_clkget;
Vitaly Wool41561f22006-12-10 21:21:29 +0100609 }
610
Russell King44c5d732009-11-21 12:10:54 +0000611 alg_data->ioaddr = ioremap(i2c_pnx->base, I2C_PNX_REGION_SIZE);
Russell King88d968b2009-11-21 11:58:36 +0000612 if (!alg_data->ioaddr) {
Vitaly Wool41561f22006-12-10 21:21:29 +0100613 dev_err(&pdev->dev, "Couldn't ioremap I2C I/O region\n");
614 ret = -ENOMEM;
615 goto out_release;
616 }
617
Russell Kingebdbbf22009-11-20 11:44:46 +0000618 ret = clk_enable(alg_data->clk);
619 if (ret)
620 goto out_unmap;
Vitaly Wool41561f22006-12-10 21:21:29 +0100621
Russell King6fff3da2009-11-20 12:46:07 +0000622 freq = clk_get_rate(alg_data->clk);
623
Vitaly Wool41561f22006-12-10 21:21:29 +0100624 /*
625 * Clock Divisor High This value is the number of system clocks
626 * the serial clock (SCL) will be high.
627 * For example, if the system clock period is 50 ns and the maximum
628 * desired serial period is 10000 ns (100 kHz), then CLKHI would be
629 * set to 0.5*(f_sys/f_i2c)-2=0.5*(20e6/100e3)-2=98. The actual value
630 * programmed into CLKHI will vary from this slightly due to
631 * variations in the output pad's rise and fall times as well as
632 * the deglitching filter length.
633 */
634
Russell King6fff3da2009-11-20 12:46:07 +0000635 tmp = ((freq / 1000) / I2C_PNX_SPEED_KHZ) / 2 - 2;
Kevin Wellsbe80dba2010-03-16 15:55:36 -0700636 if (tmp > 0x3FF)
637 tmp = 0x3FF;
Vitaly Wool41561f22006-12-10 21:21:29 +0100638 iowrite32(tmp, I2C_REG_CKH(alg_data));
639 iowrite32(tmp, I2C_REG_CKL(alg_data));
640
641 iowrite32(mcntrl_reset, I2C_REG_CTL(alg_data));
642 if (wait_reset(I2C_PNX_TIMEOUT, alg_data)) {
643 ret = -ENODEV;
Russell Kingebdbbf22009-11-20 11:44:46 +0000644 goto out_clock;
Vitaly Wool41561f22006-12-10 21:21:29 +0100645 }
646 init_completion(&alg_data->mif.complete);
647
Russell King44c5d732009-11-21 12:10:54 +0000648 ret = request_irq(i2c_pnx->irq, i2c_pnx_interrupt,
Russell King81d67242009-11-21 12:40:00 +0000649 0, pdev->name, alg_data);
Vitaly Wool41561f22006-12-10 21:21:29 +0100650 if (ret)
651 goto out_clock;
652
653 /* Register this adapter with the I2C subsystem */
Russell King9d7f7362009-11-21 12:25:27 +0000654 ret = i2c_add_numbered_adapter(&alg_data->adapter);
Vitaly Wool41561f22006-12-10 21:21:29 +0100655 if (ret < 0) {
656 dev_err(&pdev->dev, "I2C: Failed to add bus\n");
657 goto out_irq;
658 }
659
660 dev_dbg(&pdev->dev, "%s: Master at %#8x, irq %d.\n",
Russell King9d7f7362009-11-21 12:25:27 +0000661 alg_data->adapter.name, i2c_pnx->base, i2c_pnx->irq);
Vitaly Wool41561f22006-12-10 21:21:29 +0100662
663 return 0;
664
665out_irq:
Russell King81d67242009-11-21 12:40:00 +0000666 free_irq(i2c_pnx->irq, alg_data);
Vitaly Wool41561f22006-12-10 21:21:29 +0100667out_clock:
Russell Kingebdbbf22009-11-20 11:44:46 +0000668 clk_disable(alg_data->clk);
Vitaly Wool41561f22006-12-10 21:21:29 +0100669out_unmap:
Russell King88d968b2009-11-21 11:58:36 +0000670 iounmap(alg_data->ioaddr);
Vitaly Wool41561f22006-12-10 21:21:29 +0100671out_release:
Russell King44c5d732009-11-21 12:10:54 +0000672 release_mem_region(i2c_pnx->base, I2C_PNX_REGION_SIZE);
Russell King0321cb82009-11-20 11:12:26 +0000673out_clkget:
674 clk_put(alg_data->clk);
Vitaly Wool41561f22006-12-10 21:21:29 +0100675out_drvdata:
Russell King44c5d732009-11-21 12:10:54 +0000676 kfree(alg_data);
677err_kzalloc:
Vitaly Wool41561f22006-12-10 21:21:29 +0100678 platform_set_drvdata(pdev, NULL);
679out:
680 return ret;
681}
682
683static int __devexit i2c_pnx_remove(struct platform_device *pdev)
684{
Russell King9d7f7362009-11-21 12:25:27 +0000685 struct i2c_pnx_algo_data *alg_data = platform_get_drvdata(pdev);
686 struct i2c_pnx_data *i2c_pnx = alg_data->i2c_pnx;
Vitaly Wool41561f22006-12-10 21:21:29 +0100687
Russell King81d67242009-11-21 12:40:00 +0000688 free_irq(i2c_pnx->irq, alg_data);
Russell King9d7f7362009-11-21 12:25:27 +0000689 i2c_del_adapter(&alg_data->adapter);
Russell Kingebdbbf22009-11-20 11:44:46 +0000690 clk_disable(alg_data->clk);
Russell King88d968b2009-11-21 11:58:36 +0000691 iounmap(alg_data->ioaddr);
Russell King44c5d732009-11-21 12:10:54 +0000692 release_mem_region(i2c_pnx->base, I2C_PNX_REGION_SIZE);
Russell King0321cb82009-11-20 11:12:26 +0000693 clk_put(alg_data->clk);
Russell King44c5d732009-11-21 12:10:54 +0000694 kfree(alg_data);
Vitaly Wool41561f22006-12-10 21:21:29 +0100695 platform_set_drvdata(pdev, NULL);
696
697 return 0;
698}
699
700static struct platform_driver i2c_pnx_driver = {
701 .driver = {
702 .name = "pnx-i2c",
703 .owner = THIS_MODULE,
704 },
705 .probe = i2c_pnx_probe,
706 .remove = __devexit_p(i2c_pnx_remove),
707 .suspend = i2c_pnx_controller_suspend,
708 .resume = i2c_pnx_controller_resume,
709};
710
711static int __init i2c_adap_pnx_init(void)
712{
713 return platform_driver_register(&i2c_pnx_driver);
714}
715
716static void __exit i2c_adap_pnx_exit(void)
717{
718 platform_driver_unregister(&i2c_pnx_driver);
719}
720
721MODULE_AUTHOR("Vitaly Wool, Dennis Kovalev <source@mvista.com>");
722MODULE_DESCRIPTION("I2C driver for Philips IP3204-based I2C busses");
723MODULE_LICENSE("GPL");
Kay Sieversadd8eda2008-04-22 22:16:49 +0200724MODULE_ALIAS("platform:pnx-i2c");
Vitaly Wool41561f22006-12-10 21:21:29 +0100725
Vitaly Wool41561f22006-12-10 21:21:29 +0100726/* We need to make sure I2C is initialized before USB */
727subsys_initcall(i2c_adap_pnx_init);
Vitaly Wool41561f22006-12-10 21:21:29 +0100728module_exit(i2c_adap_pnx_exit);